diff --git a/include/lib9.h b/include/lib9.h index dd624484..42f6f44b 100644 --- a/include/lib9.h +++ b/include/lib9.h @@ -46,6 +46,11 @@ extern "C" { # undef _NEEDUINT # endif #endif +#if defined(__APPLE__) +# include +# undef _NEEDUSHORT +# undef _NEEDUINT +#endif typedef signed char schar; typedef unsigned int u32int; @@ -213,7 +218,8 @@ extern int errstr(char*, uint); /* command line */ extern char *argv0; -#define ARGBEGIN for((argv0||(argv0=*argv)),argv++,argc--;\ +extern void __fixargv0(void); +#define ARGBEGIN for((argv0||(argv0=(__fixargv0(),*argv))),argv++,argc--;\ argv[0] && argv[0][0]=='-' && argv[0][1];\ argc--, argv++) {\ char *_args, *_argt;\ diff --git a/src/cmd/sam/unix.c b/src/cmd/sam/unix.c index cc15db44..205af4cd 100644 --- a/src/cmd/sam/unix.c +++ b/src/cmd/sam/unix.c @@ -270,3 +270,39 @@ int create(char *name, int omode, int perm) return fd; } + +/* SHOULD BE ELSEWHERE */ +#ifdef __APPLE__ +#include + +Lock plk; + +ulong +pread(int fd, void *buf, ulong n, ulong off) +{ + ulong rv; + + lock(&plk); + if (lseek(fd, off, 0) != off) + return -1; + rv = read(fd, buf, n); + unlock(&plk); + + return rv; +} + +ulong +pwrite(int fd, void *buf, ulong n, ulong off) +{ + ulong rv; + + lock(&plk); + if (lseek(fd, off, 0) != off) + return -1; + rv = write(fd, buf, n); + unlock(&plk); + + return rv; +} +#endif + diff --git a/src/lib9/Makefile b/src/lib9/Makefile index 5f8e5aee..ab694858 100644 --- a/src/lib9/Makefile +++ b/src/lib9/Makefile @@ -19,7 +19,7 @@ OFILES=\ nrand.$O\ qlock.$O\ readn.$O\ - rendez.$O\ + rendez-$(SYSNAME).$O\ strecpy.$O\ sysfatal.$O\ tas-$(OBJTYPE).$O\ @@ -31,7 +31,7 @@ OFILES=\ werrstr.$O\ HFILES=\ - lib9.h\ + $(9SRC)/include/lib9.h\ include $(9SRC)/Makesyslib diff --git a/src/lib9/argv0.c b/src/lib9/argv0.c index 2c846f4d..4c61f444 100644 --- a/src/lib9/argv0.c +++ b/src/lib9/argv0.c @@ -2,3 +2,8 @@ char *argv0; +/* + * Mac OS can't deal with files that only declare data. + * ARGBEGIN mentions this function so that this file gets pulled in. + */ +void __fixargv0(void) { } diff --git a/src/lib9/await.c b/src/lib9/await.c index 9df7faa5..49160e76 100644 --- a/src/lib9/await.c +++ b/src/lib9/await.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include diff --git a/src/lib9/ffork-FreeBSD.c b/src/lib9/ffork-FreeBSD.c index a7c82e64..3829bb2d 100644 --- a/src/lib9/ffork-FreeBSD.c +++ b/src/lib9/ffork-FreeBSD.c @@ -31,3 +31,9 @@ _spinlock(spinlock_t *lk) lock((Lock*)&lk->access_lock); } +int +getfforkid(void) +{ + return getpid(); +} + diff --git a/src/lib9/ffork-Linux.c b/src/lib9/ffork-Linux.c index aad80041..3b5c8ee0 100644 --- a/src/lib9/ffork-Linux.c +++ b/src/lib9/ffork-Linux.c @@ -37,3 +37,9 @@ ffork(int flags, void (*fn)(void*), void *arg) return pid; } +int +getfforkid(void) +{ + return getpid(); +} + diff --git a/src/lib9/lock.c b/src/lib9/lock.c index 2da73626..5d6f2f3e 100644 --- a/src/lib9/lock.c +++ b/src/lib9/lock.c @@ -8,12 +8,9 @@ _xtas(void *v) { int x; -_ntas++; + _ntas++; x = _tas(v); - if(x == 0 || x == 0xCAFEBABE) - return x; - fprint(2, "%d: tas %p got %ux\n", getpid(), v, x); - abort(); + return x; } int diff --git a/src/lib9/sysfatal.c b/src/lib9/sysfatal.c index f9ab6985..33a533b8 100644 --- a/src/lib9/sysfatal.c +++ b/src/lib9/sysfatal.c @@ -14,6 +14,7 @@ sysfatal(char *fmt, ...) vseprint(buf, buf+sizeof buf, fmt, arg); va_end(arg); + __fixargv0(); fprint(2, "%s; %s\n", argv0 ? argv0 : "", buf); exits("fatal"); } diff --git a/src/libthread/getpid.c b/src/libthread/getpid.c index da03bd3f..c4d23dad 100644 --- a/src/libthread/getpid.c +++ b/src/libthread/getpid.c @@ -1,8 +1,7 @@ #include "threadimpl.h" -#include - +extern int getfforkid(void); int _threadgetpid(void) { - return getpid(); + return getfforkid(); } diff --git a/src/libthread/label.h b/src/libthread/label.h index 0c9f3030..a1f6fa5a 100644 --- a/src/libthread/label.h +++ b/src/libthread/label.h @@ -17,6 +17,21 @@ struct Label ulong si; ulong di; }; +#elif defined(__APPLE__) +struct Label +{ + ulong pc; /* lr */ + ulong cr; /* mfcr */ + ulong ctr; /* mfcr */ + ulong xer; /* mfcr */ + ulong sp; /* callee saved: r1 */ + ulong toc; /* callee saved: r2 */ + ulong gpr[19]; /* callee saved: r13-r31 */ +// XXX: currently do not save vector registers or floating-point state +// ulong pad; +// uvlong fpr[18]; /* callee saved: f14-f31 */ +// ulong vr[4*12]; /* callee saved: v20-v31, 256-bits each */ +}; #else #error "Unknown or unsupported architecture" #endif diff --git a/src/libthread/trend.c b/src/libthread/trend.c index 6f8adcd5..1ccf1a79 100644 --- a/src/libthread/trend.c +++ b/src/libthread/trend.c @@ -25,7 +25,7 @@ threadmain(int argc, char **argv) c[1] = chancreate(sizeof(ulong), 1); c[2] = c[0]; - threadcreate(pingpong, c, 16384); + proccreate(pingpong, c, 16384); threadcreate(pingpong, c+1, 16384); sendul(c[0], atoi(argv[1])); }