ref: 314c3c94556aa09b38282a450783db776e92b4e4
parent: a5c3eeb24a73c6b5602393bd90573ba2ad3ca789
author: Ori Bernstein <[email protected]>
date: Wed Dec 24 14:10:02 EST 2014
Make a hello world using syscalls work on Plan 9. YAY!
--- a/libstd/alloc.myr
+++ b/libstd/alloc.myr
@@ -138,7 +138,7 @@
addr = p castto(size)
addr -= align(sizeof(slheader), Align)
phdr = addr castto(slheader#)
- assert(phdr.magic == (0xdeadbeefbadf00d castto(size)), "corrupt memory\n")
+ assert(phdr.magic == (0xdeadbeef2badf00d castto(size)), "corrupt memory\n")
}
/* Frees a slice */
--- a/libstd/sys+plan9-x64.myr
+++ b/libstd/sys+plan9-x64.myr
@@ -1,8 +1,8 @@
use "systypes.use"
pkg sys =
+ type scno = int64 /* syscall */
type pid = int32 /* process id */
- type scno = int32 /* syscall */
type fdopt = int32 /* fd options */
type fd = int32 /* fd */
type rflags = int32 /* rfork flags */
@@ -166,44 +166,49 @@
extern const alloca : (sz : size -> byte#)
-generic ptr = {x : @t; -> x castto(byte#)}
-generic len32 = {a : @t[:]; -> a.len castto(uint32)}
+/*
+ABI mismatch: Plan 9 aligns all arguments individually to
+8 bytes, Myrddin uses natural alignment (min(sizeof(t), 16).
+Cast to a 64 bit type to paper over this.
+*/
+generic a = {a : @t; -> a castto(uint64)}
+generic s = {a : @t; -> a castto(int64)}
const sysr1 = {; -> syscall(Syssysr1)}
const bind = {name, old; -> syscall(Sysbind, cstring(name), cstring(old))}
const chdir = {dir; -> syscall(Syschdir, cstring(dir)) }
-const close = {fd; -> syscall(Sysclose, fd)}
-const dup = {ofd, nfd; -> syscall(Sysdup, ofd, nfd) castto(fd)}
-const alarm = {msec; -> syscall(Sysalarm, msec)}
+const close = {fd; -> syscall(Sysclose, a(fd))}
+const dup = {ofd, nfd; -> syscall(Sysdup, a(ofd), a(nfd)) castto(fd)}
+const alarm = {msec; -> syscall(Sysalarm, a(msec))}
const exits = {msg; -> syscall(Sysexits, cstring(msg))}
-const fauth = {fd, aname; -> syscall(Sysfauth, fd, cstring(aname))}
-const segbrk = {saddr, addr; -> syscall(Syssegbrk, saddr, addr)}
-const open = {path, opt; -> syscall(Sysopen, cstring(path), opt) castto(fd)}
-const sleep = {msec; -> syscall(Syssleep, msec)}
-const rfork = {rflags; -> syscall(Sysrfork, rflags) castto(pid)}
-const pipe = {fds; -> syscall(Syspipe, ptr(fds))}
-const create = {path, mode, perm; -> syscall(Syscreate, cstring(path), mode, perm) castto(fd)}
-const fd2path = {fd, buf; -> syscall(Sysfd2path, fd, ptr(buf), len32(buf))}
+const fauth = {fd, aname; -> syscall(Sysfauth, a(fd), cstring(aname))}
+const segbrk = {saddr, addr; -> syscall(Syssegbrk, a(saddr), a(addr))}
+const open = {path, opt; -> syscall(Sysopen, cstring(path), a(opt)) castto(fd)}
+const sleep = {msec; -> syscall(Syssleep, a(msec))}
+const rfork = {rflags; -> syscall(Sysrfork, a(rflags)) castto(pid)}
+const pipe = {fds; -> syscall(Syspipe, a(fds))}
+const create = {path, mode, perm; -> syscall(Syscreate, cstring(path), a(mode), a(perm)) castto(fd)}
+const fd2path = {fd, buf; -> syscall(Sysfd2path, a(fd), a(buf), a(buf.len))}
const remove = {path; -> syscall(Sysremove, cstring(path))}
-const notify = {fn; -> syscall(Sysnotify, fn)} /* FIXME: this is likely to break... */
-const noted = {v; -> syscall(Sysnoted, v)}
-const segattach = {attr, class, va, len; -> syscall(Syssegattach, attr, cstring(class), va, len)}
-const segdetach = {va; -> syscall(Syssegdetach, va)}
-const segfree = {va, len; -> syscall(Syssegfree, va, len)}
-const segflush = {va, len; -> syscall(Syssegfree, va, len)}
+const notify = {fn; -> syscall(Sysnotify, fn)} /* FIXME: this is likely to break when we do closures... */
+const noted = {v; -> syscall(Sysnoted, a(v))}
+const segattach = {attr, class, va, len; -> syscall(Syssegattach, a(attr), cstring(class), a(va), a(len))}
+const segdetach = {va; -> syscall(Syssegdetach, a(va))}
+const segfree = {va, len; -> syscall(Syssegfree, a(va), a(len))}
+const segflush = {va, len; -> syscall(Syssegfree, a(va), a(len))}
const unmount = {name, old; -> syscall(Sysunmount, cstring(name), cstring(old))}
-const errstr = {buf; -> syscall(Syserrstr, ptr(buf), len32(buf))}
-const stat = {name, edir; -> syscall(Sysstat, cstring(name), ptr(edir), len32(edir))}
-const fstat = {fd, edir; -> syscall(Sysstat, fd, ptr(edir), len32(edir))}
-const wstat = {name, edir; -> syscall(Syswstat, cstring(name), ptr(edir), len32(edir))}
-const fwstat = {fd, edir; -> syscall(Sysfwstat, fd, ptr(edir), len32(edir))}
-const mount = {fd, afd, old, flag, aname; -> syscall(Sysmount, fd, afd, cstring(old), flag, cstring(aname))}
-const await = {buf; -> syscall(Sysawait, ptr(buf), len32(buf))}
-const pread = {fd, buf, off; -> syscall(Syspread, fd, ptr(buf), len32(buf), off) castto(size)}
-const pwrite = {fd, buf, off; -> syscall(Syspwrite, fd, ptr(buf), len32(buf), off) castto(size)}
+const errstr = {buf; -> syscall(Syserrstr, a(buf), a(buf.len))}
+const stat = {name, edir; -> syscall(Sysstat, cstring(name), a(edir), a(edir))}
+const fstat = {fd, edir; -> syscall(Sysstat, a(fd), a(edir), a(edir))}
+const wstat = {name, edir; -> syscall(Syswstat, cstring(name), a(edir), a(edir))}
+const fwstat = {fd, edir; -> syscall(Sysfwstat, a(fd), a(edir), a(edir))}
+const mount = {fd, afd, old, flag, aname; -> syscall(Sysmount, a(fd), a(afd), cstring(old), a(flag), cstring(aname))}
+const await = {buf; -> syscall(Sysawait, a(buf), a(buf.len))}
+const pread = {fd, buf, off; -> syscall(Syspread, a(fd), a(buf), a(buf.len), off) castto(size)}
+const pwrite = {fd, buf, off; -> syscall(Syspwrite, a(fd), a(buf), a(buf.len), s(off)) castto(size)}
const seek = {fd, n, ty
var ret : off
- syscall(Sysseek, &ret, fd, n, ty)
+ syscall(Sysseek, a(&ret), a(fd), a(n), a(ty))
-> ret
}
const exec = {bin, args
@@ -211,12 +216,12 @@
/* we need an array of C strings. */
p = alloca((args.len + 1)*sizeof(byte#))
- cargs = (ptr(p) castto(byte##))[:args.len + 1]
+ cargs = (a(p) castto(byte##))[:args.len + 1]
for i = 0; i < args.len; i++
cargs[i] = cstring(args[i])
;;
- cargs[args.len] = ptr(0)
- -> syscall(Sysexec, cstring(bin), cargs)
+ cargs[args.len] = 0 castto(byte#)
+ -> syscall(Sysexec, cstring(bin), a(cargs))
}
/* ??? do I care for now?
const brk_ = { }
--- a/libstd/syscall+plan9-x64.s
+++ b/libstd/syscall+plan9-x64.s
@@ -10,38 +10,39 @@
and we have 256 bytes of gap if we get a note.
*/
TEXT sys$syscall+0(SB),1,$0
- MOVQ BX,-16(SP)
- MOVQ CX,-24(SP)
- MOVQ DX,-32(SP)
- MOVQ SI,-40(SP)
- MOVQ DI,-48(SP)
- MOVQ BP,-56(SP)
- MOVQ R8,-64(SP)
- MOVQ R9,-72(SP)
- MOVQ R10,-80(SP)
- MOVQ R11,-88(SP)
- MOVQ R12,-104(SP)
- MOVQ R13,-112(SP)
- MOVQ R14,-120(SP)
- MOVQ R15,-128(SP)
- ADDQ $8,SP
+ MOVQ BX,-16(SP)
+ MOVQ CX,-24(SP)
+ MOVQ DX,-32(SP)
+ MOVQ SI,-40(SP)
+ MOVQ DI,-48(SP)
+ MOVQ BP,-56(SP)
+ MOVQ R8,-64(SP)
+ MOVQ R9,-72(SP)
+ MOVQ R10,-80(SP)
+ MOVQ R11,-88(SP)
+ MOVQ R12,-104(SP)
+ MOVQ R13,-112(SP)
+ MOVQ R14,-120(SP)
+ MOVQ R15,-128(SP)
+ MOVQ 8(SP),RARG
+ ADDQ $8,SP
SYSCALL
- SUBQ $8,SP
- MOVQ -16(SP),BX
- MOVQ -24(SP),CX
- MOVQ -32(SP),DX
- MOVQ -40(SP),SI
- MOVQ -48(SP),DI
- MOVQ -56(SP),BP
- MOVQ -64(SP),R8
- MOVQ -72(SP),R9
- MOVQ -80(SP),R10
- MOVQ -88(SP),R11
- MOVQ -104(SP),R12
- MOVQ -112(SP),R13
- MOVQ -120(SP),R14
- MOVQ -128(SP),R15
+ SUBQ $8,SP
+ MOVQ -16(SP),BX
+ MOVQ -24(SP),CX
+ MOVQ -32(SP),DX
+ MOVQ -40(SP),SI
+ MOVQ -48(SP),DI
+ MOVQ -56(SP),BP
+ MOVQ -64(SP),R8
+ MOVQ -72(SP),R9
+ MOVQ -80(SP),R10
+ MOVQ -88(SP),R11
+ MOVQ -104(SP),R12
+ MOVQ -112(SP),R13
+ MOVQ -120(SP),R14
+ MOVQ -128(SP),R15
RET