Commit 08cfcd1d authored by Russ Cox's avatar Russ Cox

convert darwin to use godefs-generated defs.h.

this change is much smaller if you ignore
the machine-generated defs.h.

TBR=r
OCL=26684
CL=26684
parent 8ee041dc
...@@ -7,7 +7,7 @@ O=6 ...@@ -7,7 +7,7 @@ O=6
CC=$(O)c CC=$(O)c
AS=$(O)a AS=$(O)a
LIB=lib_$(GOARCH)_$(GOOS).a LIB=lib.a
OFILES=\ OFILES=\
array.$O\ array.$O\
...@@ -41,11 +41,15 @@ OFILES=\ ...@@ -41,11 +41,15 @@ OFILES=\
thread.$O\ thread.$O\
traceback.$O\ traceback.$O\
OS_H=$(GOARCH)_$(GOOS).h HFILES=\
HFILES=runtime.h hashmap.h malloc.h $(OS_H_) runtime.h\
hashmap.h\
malloc.h\
$(GOOS)/os.h\
$(GOOS)/$(GOARCH)/defs.h\
install: $(LIB) runtime.acid install: $(LIB) runtime.acid
cp $(LIB) $(GOROOT)/lib cp $(LIB) $(GOROOT)/lib_$(GOARCH)_$(GOOS).a
cp runtime.acid $(GOROOT)/acid/runtime.acid cp runtime.acid $(GOROOT)/acid/runtime.acid
$(LIB): $(OFILES) $(LIB): $(OFILES)
......
/* // godefs -f -m64 defs.c
* System structs for Darwin, amd64
*/ // MACHINE GENERATED - DO NOT EDIT.
typedef uint32 dev_t; // Constants
typedef uint64 ino_t; enum {
typedef uint16 mode_t; PROT_NONE = 0,
typedef uint16 nlink_t; PROT_READ = 0x1,
typedef uint32 uid_t; PROT_WRITE = 0x2,
typedef uint32 gid_t; PROT_EXEC = 0x4,
typedef int64 off_t; MAP_ANON = 0x1000,
typedef int32 blksize_t; MAP_PRIVATE = 0x2,
typedef int64 blkcnt_t; MACH_MSG_TYPE_MOVE_RECEIVE = 0x10,
typedef int64 time_t; MACH_MSG_TYPE_MOVE_SEND = 0x11,
MACH_MSG_TYPE_MOVE_SEND_ONCE = 0x12,
struct timespec { MACH_MSG_TYPE_COPY_SEND = 0x13,
time_t tv_sec; MACH_MSG_TYPE_MAKE_SEND = 0x14,
int64 tv_nsec; MACH_MSG_TYPE_MAKE_SEND_ONCE = 0x15,
}; MACH_MSG_TYPE_COPY_RECEIVE = 0x16,
MACH_MSG_PORT_DESCRIPTOR = 0,
struct timeval { MACH_MSG_OOL_DESCRIPTOR = 0x1,
time_t tv_sec; MACH_MSG_OOL_PORTS_DESCRIPTOR = 0x2,
int64 tv_usec; MACH_MSG_OOL_VOLATILE_DESCRIPTOR = 0x3,
}; MACH_MSGH_BITS_COMPLEX = 0x80000000,
MACH_SEND_MSG = 0x1,
struct stat { // really a stat64 MACH_RCV_MSG = 0x2,
dev_t st_dev; MACH_RCV_LARGE = 0x4,
mode_t st_mode; MACH_SEND_TIMEOUT = 0x10,
nlink_t st_nlink; MACH_SEND_INTERRUPT = 0x40,
ino_t st_ino; MACH_SEND_CANCEL = 0x80,
uid_t st_uid; MACH_SEND_ALWAYS = 0x10000,
gid_t st_gid; MACH_SEND_TRAILER = 0x20000,
dev_t st_rdev; MACH_RCV_TIMEOUT = 0x100,
struct timespec st_atimespec; MACH_RCV_NOTIFY = 0x200,
struct timespec st_mtimespec; MACH_RCV_INTERRUPT = 0x400,
struct timespec st_ctimespec; MACH_RCV_OVERWRITE = 0x1000,
struct timespec st_birthtimespec; NDR_PROTOCOL_2_0 = 0,
off_t st_size; NDR_INT_BIG_ENDIAN = 0,
blkcnt_t st_blocks; NDR_INT_LITTLE_ENDIAN = 0x1,
blksize_t st_blksize; NDR_FLOAT_IEEE = 0,
uint32 st_flags; NDR_CHAR_ASCII = 0,
uint32 st_gen; SA_SIGINFO = 0x40,
int64 st_qspare[2]; SA_RESTART = 0x2,
}; SA_ONSTACK = 0x1,
SA_USERTRAMP = 0x100,
#define O_CREAT 0x0200 SA_64REGSET = 0x200,
};
void bsdthread_create(void*, M*, G*, void(*)(void));
void bsdthread_register(void); // Types
#pragma pack on
// Mach calls typedef struct MachBody MachBody;
struct MachBody {
typedef int32 kern_return_t; uint32 msgh_descriptor_count;
typedef uint32 mach_port_t; };
mach_port_t mach_semcreate(void); typedef struct MachHeader MachHeader;
void mach_semacquire(mach_port_t); struct MachHeader {
void mach_semrelease(mach_port_t); uint32 msgh_bits;
void mach_semreset(mach_port_t); uint32 msgh_size;
void mach_semdestroy(mach_port_t); uint32 msgh_remote_port;
uint32 msgh_local_port;
uint32 msgh_reserved;
int32 msgh_id;
};
typedef struct MachNDR MachNDR;
struct MachNDR {
uint8 mig_vers;
uint8 if_vers;
uint8 reserved1;
uint8 mig_encoding;
uint8 int_rep;
uint8 char_rep;
uint8 float_rep;
uint8 reserved2;
};
typedef struct MachPort MachPort;
struct MachPort {
uint32 name;
uint32 pad1;
uint32 pad2;
uint32 disposition;
uint32 type;
};
typedef struct StackT StackT;
struct StackT {
void *ss_sp;
uint64 ss_size;
int32 ss_flags;
byte pad0[4];
};
typedef union Sighandler Sighandler;
union Sighandler {
void *__sa_handler;
void *__sa_sigaction;
};
typedef struct Sigaction Sigaction;
struct Sigaction {
Sighandler __sigaction_u;
void *sa_tramp;
uint32 sa_mask;
int32 sa_flags;
};
typedef union Sigval Sigval;
union Sigval {
int32 sival_int;
void *sival_ptr;
};
typedef struct Siginfo Siginfo;
struct Siginfo {
int32 si_signo;
int32 si_errno;
int32 si_code;
int32 si_pid;
uint32 si_uid;
int32 si_status;
void *si_addr;
Sigval si_value;
int64 si_band;
uint64 __pad[7];
};
typedef struct FPControl FPControl;
struct FPControl {
byte pad0[2];
};
typedef struct FPStatus FPStatus;
struct FPStatus {
byte pad0[2];
};
typedef struct RegMMST RegMMST;
struct RegMMST {
int8 mmst_reg[10];
int8 mmst_rsrv[6];
};
typedef struct RegXMM RegXMM;
struct RegXMM {
int8 xmm_reg[16];
};
typedef struct Regs Regs;
struct Regs {
uint64 rax;
uint64 rbx;
uint64 rcx;
uint64 rdx;
uint64 rdi;
uint64 rsi;
uint64 rbp;
uint64 rsp;
uint64 r8;
uint64 r9;
uint64 r10;
uint64 r11;
uint64 r12;
uint64 r13;
uint64 r14;
uint64 r15;
uint64 rip;
uint64 rflags;
uint64 cs;
uint64 fs;
uint64 gs;
};
typedef struct FloatState FloatState;
struct FloatState {
int32 fpu_reserved[2];
FPControl fpu_fcw;
FPStatus fpu_fsw;
uint8 fpu_ftw;
uint8 fpu_rsrv1;
uint16 fpu_fop;
uint32 fpu_ip;
uint16 fpu_cs;
uint16 fpu_rsrv2;
uint32 fpu_dp;
uint16 fpu_ds;
uint16 fpu_rsrv3;
uint32 fpu_mxcsr;
uint32 fpu_mxcsrmask;
RegMMST fpu_stmm0;
RegMMST fpu_stmm1;
RegMMST fpu_stmm2;
RegMMST fpu_stmm3;
RegMMST fpu_stmm4;
RegMMST fpu_stmm5;
RegMMST fpu_stmm6;
RegMMST fpu_stmm7;
RegXMM fpu_xmm0;
RegXMM fpu_xmm1;
RegXMM fpu_xmm2;
RegXMM fpu_xmm3;
RegXMM fpu_xmm4;
RegXMM fpu_xmm5;
RegXMM fpu_xmm6;
RegXMM fpu_xmm7;
RegXMM fpu_xmm8;
RegXMM fpu_xmm9;
RegXMM fpu_xmm10;
RegXMM fpu_xmm11;
RegXMM fpu_xmm12;
RegXMM fpu_xmm13;
RegXMM fpu_xmm14;
RegXMM fpu_xmm15;
int8 fpu_rsrv4[96];
int32 fpu_reserved1;
};
typedef struct ExceptionState ExceptionState;
struct ExceptionState {
uint32 trapno;
uint32 err;
uint64 faultvaddr;
};
typedef struct Mcontext Mcontext;
struct Mcontext {
ExceptionState es;
Regs ss;
FloatState fs;
byte pad0[4];
};
typedef struct Ucontext Ucontext;
struct Ucontext {
int32 uc_onstack;
uint32 uc_sigmask;
StackT uc_stack;
Ucontext *uc_link;
uint64 uc_mcsize;
Mcontext *uc_mcontext;
};
#pragma pack off
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
* Input to godefs.
*
godefs -f -m64 defs.c >amd64/defs.h
godefs defs.c >386/defs.h
*/
#define __DARWIN_UNIX03 0
#include <mach/mach.h>
#include <mach/message.h>
#include <sys/types.h>
#include <sys/time.h>
#include <signal.h>
#include <sys/mman.h>
enum {
$PROT_NONE = PROT_NONE,
$PROT_READ = PROT_READ,
$PROT_WRITE = PROT_WRITE,
$PROT_EXEC = PROT_EXEC,
$MAP_ANON = MAP_ANON,
$MAP_PRIVATE = MAP_PRIVATE,
$MACH_MSG_TYPE_MOVE_RECEIVE = MACH_MSG_TYPE_MOVE_RECEIVE,
$MACH_MSG_TYPE_MOVE_SEND = MACH_MSG_TYPE_MOVE_SEND,
$MACH_MSG_TYPE_MOVE_SEND_ONCE = MACH_MSG_TYPE_MOVE_SEND_ONCE,
$MACH_MSG_TYPE_COPY_SEND = MACH_MSG_TYPE_COPY_SEND,
$MACH_MSG_TYPE_MAKE_SEND = MACH_MSG_TYPE_MAKE_SEND,
$MACH_MSG_TYPE_MAKE_SEND_ONCE = MACH_MSG_TYPE_MAKE_SEND_ONCE,
$MACH_MSG_TYPE_COPY_RECEIVE = MACH_MSG_TYPE_COPY_RECEIVE,
$MACH_MSG_PORT_DESCRIPTOR = MACH_MSG_PORT_DESCRIPTOR,
$MACH_MSG_OOL_DESCRIPTOR = MACH_MSG_OOL_DESCRIPTOR,
$MACH_MSG_OOL_PORTS_DESCRIPTOR = MACH_MSG_OOL_PORTS_DESCRIPTOR,
$MACH_MSG_OOL_VOLATILE_DESCRIPTOR = MACH_MSG_OOL_VOLATILE_DESCRIPTOR,
$MACH_MSGH_BITS_COMPLEX = MACH_MSGH_BITS_COMPLEX,
$MACH_SEND_MSG = MACH_SEND_MSG,
$MACH_RCV_MSG = MACH_RCV_MSG,
$MACH_RCV_LARGE = MACH_RCV_LARGE,
$MACH_SEND_TIMEOUT = MACH_SEND_TIMEOUT,
$MACH_SEND_INTERRUPT = MACH_SEND_INTERRUPT,
$MACH_SEND_CANCEL = MACH_SEND_CANCEL,
$MACH_SEND_ALWAYS = MACH_SEND_ALWAYS,
$MACH_SEND_TRAILER = MACH_SEND_TRAILER,
$MACH_RCV_TIMEOUT = MACH_RCV_TIMEOUT,
$MACH_RCV_NOTIFY = MACH_RCV_NOTIFY,
$MACH_RCV_INTERRUPT = MACH_RCV_INTERRUPT,
$MACH_RCV_OVERWRITE = MACH_RCV_OVERWRITE,
$NDR_PROTOCOL_2_0 = NDR_PROTOCOL_2_0,
$NDR_INT_BIG_ENDIAN = NDR_INT_BIG_ENDIAN,
$NDR_INT_LITTLE_ENDIAN = NDR_INT_LITTLE_ENDIAN,
$NDR_FLOAT_IEEE = NDR_FLOAT_IEEE,
$NDR_CHAR_ASCII = NDR_CHAR_ASCII,
$SA_SIGINFO = SA_SIGINFO,
$SA_RESTART = SA_RESTART,
$SA_ONSTACK = SA_ONSTACK,
$SA_USERTRAMP = SA_USERTRAMP,
$SA_64REGSET = SA_64REGSET,
};
typedef mach_msg_body_t $MachBody;
typedef mach_msg_header_t $MachHeader;
typedef NDR_record_t $MachNDR;
typedef mach_msg_port_descriptor_t $MachPort;
typedef stack_t $StackT;
typedef union __sigaction_u $Sighandler;
typedef struct __sigaction $Sigaction; // used in syscalls
// typedef struct sigaction $Sigaction; // used by the C library
typedef union sigval $Sigval;
typedef siginfo_t $Siginfo;
typedef struct fp_control $FPControl;
typedef struct fp_status $FPStatus;
typedef struct mmst_reg $RegMMST;
typedef struct xmm_reg $RegXMM;
#ifdef __LP64__
// amd64
typedef x86_thread_state64_t $Regs;
typedef x86_float_state64_t $FloatState;
typedef x86_exception_state64_t $ExceptionState;
typedef struct mcontext64 $Mcontext;
#else
// 386
typedef x86_thread_state32_t $Regs;
typedef x86_float_state32_t $FloatState;
typedef x86_exception_state32_t $ExceptionState;
typedef struct mcontext32 $Mcontext;
#endif
typedef ucontext_t $Ucontext;
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
void bsdthread_create(void*, M*, G*, void(*)(void));
void bsdthread_register(void);
int32 mach_msg_trap(MachHeader*, int32, uint32, uint32, uint32, uint32, uint32);
uint32 mach_reply_port(void);
void mach_semacquire(uint32);
uint32 mach_semcreate(void);
void mach_semdestroy(uint32);
void mach_semrelease(uint32);
void mach_semreset(uint32);
uint32 mach_task_self(void);
uint32 mach_task_self(void);
uint32 mach_thread_self(void);
uint32 mach_thread_self(void);
struct Sigaction;
void sigaction(int64, struct Sigaction*, struct Sigaction*);
struct StackT;
void sigaltstack(struct StackT*, struct StackT*);
void sigtramp(void);
...@@ -4,204 +4,100 @@ ...@@ -4,204 +4,100 @@
#include "runtime.h" #include "runtime.h"
#include "defs.h" #include "defs.h"
#include "os.h"
#include "signals.h" #include "signals.h"
typedef uint64 __uint64_t;
/* From /usr/include/mach/i386/_structs.h */
#define _STRUCT_X86_THREAD_STATE64 struct __darwin_x86_thread_state64
_STRUCT_X86_THREAD_STATE64
{
__uint64_t __rax;
__uint64_t __rbx;
__uint64_t __rcx;
__uint64_t __rdx;
__uint64_t __rdi;
__uint64_t __rsi;
__uint64_t __rbp;
__uint64_t __rsp;
__uint64_t __r8;
__uint64_t __r9;
__uint64_t __r10;
__uint64_t __r11;
__uint64_t __r12;
__uint64_t __r13;
__uint64_t __r14;
__uint64_t __r15;
__uint64_t __rip;
__uint64_t __rflags;
__uint64_t __cs;
__uint64_t __fs;
__uint64_t __gs;
};
void void
print_thread_state(_STRUCT_X86_THREAD_STATE64* ss) dumpregs(Regs *r)
{ {
prints("\nrax "); sys·printhex(ss->__rax); printf("rax %X\n", r->rax);
prints("\nrbx "); sys·printhex(ss->__rbx); printf("rbx %X\n", r->rbx);
prints("\nrcx "); sys·printhex(ss->__rcx); printf("rcx %X\n", r->rcx);
prints("\nrdx "); sys·printhex(ss->__rdx); printf("rdx %X\n", r->rdx);
prints("\nrdi "); sys·printhex(ss->__rdi); printf("rdi %X\n", r->rdi);
prints("\nrsi "); sys·printhex(ss->__rsi); printf("rsi %X\n", r->rsi);
prints("\nrbp "); sys·printhex(ss->__rbp); printf("rbp %X\n", r->rbp);
prints("\nrsp "); sys·printhex(ss->__rsp); printf("rsp %X\n", r->rsp);
prints("\nr8 "); sys·printhex(ss->__r8 ); printf("r8 %X\n", r->r8 );
prints("\nr9 "); sys·printhex(ss->__r9 ); printf("r9 %X\n", r->r9 );
prints("\nr10 "); sys·printhex(ss->__r10); printf("r10 %X\n", r->r10);
prints("\nr11 "); sys·printhex(ss->__r11); printf("r11 %X\n", r->r11);
prints("\nr12 "); sys·printhex(ss->__r12); printf("r12 %X\n", r->r12);
prints("\nr13 "); sys·printhex(ss->__r13); printf("r13 %X\n", r->r13);
prints("\nr14 "); sys·printhex(ss->__r14); printf("r14 %X\n", r->r14);
prints("\nr15 "); sys·printhex(ss->__r15); printf("r15 %X\n", r->r15);
prints("\nrip "); sys·printhex(ss->__rip); printf("rip %X\n", r->rip);
prints("\nrflags "); sys·printhex(ss->__rflags); printf("rflags %X\n", r->rflags);
prints("\ncs "); sys·printhex(ss->__cs); printf("cs %X\n", r->cs);
prints("\nfs "); sys·printhex(ss->__fs); printf("fs %X\n", r->fs);
prints("\ngs "); sys·printhex(ss->__gs); printf("gs %X\n", r->gs);
prints("\n");
}
/* Code generated via: g++ -m64 gen_signals_support.cc && a.out */
static void *adr_at(void *ptr, int32 offs) {
return (void *)((uint8 *)ptr + offs);
}
static void *ptr_at(void *ptr, int32 offs) {
return *(void **)((uint8 *)ptr + offs);
}
typedef void ucontext_t;
typedef void _STRUCT_MCONTEXT64;
typedef void _STRUCT_X86_EXCEPTION_STATE64;
typedef void _STRUCT_X86_FLOAT_STATE64;
static _STRUCT_MCONTEXT64 *get_uc_mcontext(ucontext_t *ptr) {
return (_STRUCT_MCONTEXT64 *)ptr_at(ptr, 48);
}
static _STRUCT_X86_EXCEPTION_STATE64 *get___es(_STRUCT_MCONTEXT64 *ptr) {
return (_STRUCT_X86_EXCEPTION_STATE64 *)adr_at(ptr, 0);
}
static _STRUCT_X86_THREAD_STATE64 *get___ss(_STRUCT_MCONTEXT64 *ptr) {
return (_STRUCT_X86_THREAD_STATE64 *)adr_at(ptr, 16);
} }
static _STRUCT_X86_FLOAT_STATE64 *get___fs(_STRUCT_MCONTEXT64 *ptr) {
return (_STRUCT_X86_FLOAT_STATE64 *)adr_at(ptr, 184);
}
/* End of generated code */
/*
* This assembler routine takes the args from registers, puts them on the stack,
* and calls the registered handler.
*/
extern void sigtramp(void);
/*
* Rudimentary reverse-engineered definition of signal interface.
* You'd think it would be documented.
*/
struct siginfo {
int32 si_signo; /* signal number */
int32 si_errno; /* errno association */
int32 si_code; /* signal code */
int32 si_pid; /* sending process */
int32 si_uid; /* sender's ruid */
int32 si_status; /* exit value */
void *si_addr; /* faulting address */
/* more stuff here */
};
struct sigaction {
void (*sa_handler)(int32, struct siginfo*, void*); // actual handler
void (*sa_trampoline)(void); // assembly trampoline
uint32 sa_mask; // signal mask during handler
int32 sa_flags; // flags below
};
void void
sighandler(int32 sig, struct siginfo *info, void *context) sighandler(int32 sig, Siginfo *info, void *context)
{ {
Ucontext *uc;
Mcontext *mc;
Regs *r;
if(panicking) // traceback already printed if(panicking) // traceback already printed
sys_Exit(2); sys_Exit(2);
panicking = 1; panicking = 1;
_STRUCT_MCONTEXT64 *uc_mcontext = get_uc_mcontext(context);
_STRUCT_X86_THREAD_STATE64 *ss = get___ss(uc_mcontext);
if(sig < 0 || sig >= NSIG){ if(sig < 0 || sig >= NSIG){
prints("Signal "); printf("Signal %d\n", sig);
sys·printint(sig);
}else{ }else{
prints(sigtab[sig].name); printf("%s\n", sigtab[sig].name);
} }
prints("\nFaulting address: "); sys·printpointer(info->si_addr); uc = context;
prints("\npc: "); sys·printhex(ss->__rip); mc = uc->uc_mcontext;
prints("\n\n"); r = &mc->ss;
printf("Faulting address: %p\n", info->si_addr);
printf("PC=%X\n", r->rip);
printf("\n");
if(gotraceback()){ if(gotraceback()){
traceback((void *)ss->__rip, (void *)ss->__rsp, (void*)ss->__r15); traceback((void*)r->rip, (void*)r->rsp, (void*)r->r15);
tracebackothers((void*)ss->__r15); tracebackothers((void*)r->r15);
print_thread_state(ss); dumpregs(r);
} }
sys_Exit(2); sys_Exit(2);
} }
void void
sigignore(int32, struct siginfo*, void*) sigignore(int32, Siginfo*, void*)
{ {
} }
struct stack_t {
byte *sp;
int64 size;
int32 flags;
};
void void
signalstack(byte *p, int32 n) signalstack(byte *p, int32 n)
{ {
struct stack_t st; StackT st;
st.sp = p; st.ss_sp = p;
st.size = n; st.ss_size = n;
st.flags = 0; st.ss_flags = 0;
sigaltstack(&st, nil); sigaltstack(&st, nil);
} }
void sigaction(int64, void*, void*);
enum {
SA_SIGINFO = 0x40,
SA_RESTART = 0x02,
SA_ONSTACK = 0x01,
SA_USERTRAMP = 0x100,
SA_64REGSET = 0x200,
};
void void
initsig(void) initsig(void)
{ {
int32 i; int32 i;
static struct sigaction sa; static Sigaction sa;
sa.sa_flags |= SA_SIGINFO|SA_ONSTACK; sa.sa_flags |= SA_SIGINFO|SA_ONSTACK;
sa.sa_mask = 0; // 0xFFFFFFFFU; sa.sa_mask = 0; // 0xFFFFFFFFU;
sa.sa_trampoline = sigtramp; sa.sa_tramp = sigtramp; // sigtramp's job is to call into real handler
for(i = 0; i<NSIG; i++) { for(i = 0; i<NSIG; i++) {
if(sigtab[i].flags) { if(sigtab[i].flags) {
if(sigtab[i].flags & SigCatch) { if(sigtab[i].flags & SigCatch) {
sa.sa_handler = sighandler; sa.__sigaction_u.__sa_sigaction = sighandler;
} else { } else {
sa.sa_handler = sigignore; sa.__sigaction_u.__sa_sigaction = sigignore;
} }
if(sigtab[i].flags & SigRestart) if(sigtab[i].flags & SigRestart)
sa.sa_flags |= SA_RESTART; sa.sa_flags |= SA_RESTART;
...@@ -212,47 +108,3 @@ initsig(void) ...@@ -212,47 +108,3 @@ initsig(void)
} }
} }
static void
unimplemented(int8 *name)
{
prints(name);
prints(" not implemented\n");
*(int32*)1231 = 1231;
}
// Thread-safe allocation of a semaphore.
// Psema points at a kernel semaphore key.
// It starts out zero, meaning no semaphore.
// Fill it in, being careful of others calling initsema
// simultaneously.
static void
initsema(uint32 *psema)
{
uint32 sema;
if(*psema != 0) // already have one
return;
sema = mach_semcreate();
if(!cas(psema, 0, sema)){
// Someone else filled it in. Use theirs.
mach_semdestroy(sema);
return;
}
}
// Atomic add and return new value.
static uint32
xadd(uint32 volatile *val, int32 delta)
{
uint32 oval, nval;
for(;;){
oval = *val;
nval = oval + delta;
if(cas(val, oval, nval))
return nval;
}
}
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "runtime.h" #include "runtime.h"
#include "defs.h" #include "defs.h"
#include "os.h"
static void static void
unimplemented(int8 *name) unimplemented(int8 *name)
...@@ -162,12 +163,11 @@ minit(void) ...@@ -162,12 +163,11 @@ minit(void)
signalstack(m->gsignal->stackguard, 32*1024); signalstack(m->gsignal->stackguard, 32*1024);
} }
// Mach IPC, to get at semaphores // Mach IPC, to get at semaphores
// Definitions are in /usr/include/mach on a Mac. // Definitions are in /usr/include/mach on a Mac.
static void static void
macherror(kern_return_t r, int8 *fn) macherror(int32 r, int8 *fn)
{ {
prints("mach error "); prints("mach error ");
prints(fn); prints(fn);
...@@ -182,129 +182,24 @@ enum ...@@ -182,129 +182,24 @@ enum
DebugMach = 0 DebugMach = 0
}; };
typedef int32 mach_msg_option_t; static MachNDR zerondr;
typedef uint32 mach_msg_bits_t;
typedef uint32 mach_msg_id_t;
typedef uint32 mach_msg_size_t;
typedef uint32 mach_msg_timeout_t;
typedef uint32 mach_port_name_t;
typedef uint64 mach_vm_address_t;
typedef struct mach_msg_header_t mach_msg_header_t;
typedef struct mach_msg_body_t mach_msg_body_t;
typedef struct mach_msg_port_descriptor_t mach_msg_port_descriptor_t;
typedef struct NDR_record_t NDR_record_t;
enum
{
MACH_MSG_TYPE_MOVE_RECEIVE = 16,
MACH_MSG_TYPE_MOVE_SEND = 17,
MACH_MSG_TYPE_MOVE_SEND_ONCE = 18,
MACH_MSG_TYPE_COPY_SEND = 19,
MACH_MSG_TYPE_MAKE_SEND = 20,
MACH_MSG_TYPE_MAKE_SEND_ONCE = 21,
MACH_MSG_TYPE_COPY_RECEIVE = 22,
MACH_MSG_PORT_DESCRIPTOR = 0,
MACH_MSG_OOL_DESCRIPTOR = 1,
MACH_MSG_OOL_PORTS_DESCRIPTOR = 2,
MACH_MSG_OOL_VOLATILE_DESCRIPTOR = 3,
MACH_MSGH_BITS_COMPLEX = 0x80000000,
MACH_SEND_MSG = 1,
MACH_RCV_MSG = 2,
MACH_RCV_LARGE = 4,
MACH_SEND_TIMEOUT = 0x10,
MACH_SEND_INTERRUPT = 0x40,
MACH_SEND_CANCEL = 0x80,
MACH_SEND_ALWAYS = 0x10000,
MACH_SEND_TRAILER = 0x20000,
MACH_RCV_TIMEOUT = 0x100,
MACH_RCV_NOTIFY = 0x200,
MACH_RCV_INTERRUPT = 0x400,
MACH_RCV_OVERWRITE = 0x1000,
};
mach_port_t mach_task_self(void);
mach_port_t mach_thread_self(void);
#pragma pack on
struct mach_msg_header_t
{
mach_msg_bits_t bits;
mach_msg_size_t size;
mach_port_t remote_port;
mach_port_t local_port;
mach_msg_size_t reserved;
mach_msg_id_t id;
};
struct mach_msg_body_t
{
uint32 descriptor_count;
};
struct mach_msg_port_descriptor_t
{
mach_port_t name;
uint32 pad1;
uint16 pad2;
uint8 disposition;
uint8 type;
};
enum
{
NDR_PROTOCOL_2_0 = 0,
NDR_INT_BIG_ENDIAN = 0,
NDR_INT_LITTLE_ENDIAN = 1,
NDR_FLOAT_IEEE = 0,
NDR_CHAR_ASCII = 0
};
struct NDR_record_t
{
uint8 mig_vers;
uint8 if_vers;
uint8 reserved1;
uint8 mig_encoding;
uint8 int_rep;
uint8 char_rep;
uint8 float_rep;
uint8 reserved2;
};
#pragma pack off
static NDR_record_t zerondr;
#define MACH_MSGH_BITS(a, b) ((a) | ((b)<<8)) #define MACH_MSGH_BITS(a, b) ((a) | ((b)<<8))
// Mach system calls (in sys_amd64_darwin.s) static int32
kern_return_t mach_msg_trap(mach_msg_header_t*, mach_msg(MachHeader *h,
mach_msg_option_t, mach_msg_size_t, mach_msg_size_t, int32 op,
mach_port_name_t, mach_msg_timeout_t, mach_port_name_t); uint32 send_size,
mach_port_t mach_reply_port(void); uint32 rcv_size,
mach_port_t mach_task_self(void); uint32 rcv_name,
mach_port_t mach_thread_self(void); uint32 timeout,
uint32 notify)
static kern_return_t
mach_msg(mach_msg_header_t *h,
mach_msg_option_t op,
mach_msg_size_t send_size,
mach_msg_size_t rcv_size,
mach_port_name_t rcv_name,
mach_msg_timeout_t timeout,
mach_port_name_t notify)
{ {
// TODO: Loop on interrupt. // TODO: Loop on interrupt.
return mach_msg_trap(h, op, send_size, rcv_size, rcv_name, timeout, notify); return mach_msg_trap(h, op, send_size, rcv_size, rcv_name, timeout, notify);
} }
// Mach RPC (MIG) // Mach RPC (MIG)
// I'm not using the Mach names anymore. They're too long.
enum enum
{ {
...@@ -316,18 +211,18 @@ enum ...@@ -316,18 +211,18 @@ enum
typedef struct CodeMsg CodeMsg; typedef struct CodeMsg CodeMsg;
struct CodeMsg struct CodeMsg
{ {
mach_msg_header_t h; MachHeader h;
NDR_record_t NDR; MachNDR NDR;
kern_return_t code; int32 code;
}; };
#pragma pack off #pragma pack off
static kern_return_t static int32
machcall(mach_msg_header_t *h, int32 maxsize, int32 rxsize) machcall(MachHeader *h, int32 maxsize, int32 rxsize)
{ {
uint32 *p; uint32 *p;
int32 i, ret, id; int32 i, ret, id;
mach_port_t port; uint32 port;
CodeMsg *c; CodeMsg *c;
if((port = m->machport) == 0){ if((port = m->machport) == 0){
...@@ -335,15 +230,15 @@ machcall(mach_msg_header_t *h, int32 maxsize, int32 rxsize) ...@@ -335,15 +230,15 @@ machcall(mach_msg_header_t *h, int32 maxsize, int32 rxsize)
m->machport = port; m->machport = port;
} }
h->bits |= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE); h->msgh_bits |= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE);
h->local_port = port; h->msgh_local_port = port;
h->reserved = 0; h->msgh_reserved = 0;
id = h->id; id = h->msgh_id;
if(DebugMach){ if(DebugMach){
p = (uint32*)h; p = (uint32*)h;
prints("send:\t"); prints("send:\t");
for(i=0; i<h->size/sizeof(p[0]); i++){ for(i=0; i<h->msgh_size/sizeof(p[0]); i++){
prints(" "); prints(" ");
sys·printpointer((void*)p[i]); sys·printpointer((void*)p[i]);
if(i%8 == 7) if(i%8 == 7)
...@@ -354,7 +249,7 @@ machcall(mach_msg_header_t *h, int32 maxsize, int32 rxsize) ...@@ -354,7 +249,7 @@ machcall(mach_msg_header_t *h, int32 maxsize, int32 rxsize)
} }
ret = mach_msg(h, MACH_SEND_MSG|MACH_RCV_MSG, ret = mach_msg(h, MACH_SEND_MSG|MACH_RCV_MSG,
h->size, maxsize, port, 0, 0); h->msgh_size, maxsize, port, 0, 0);
if(ret != 0){ if(ret != 0){
if(DebugMach){ if(DebugMach){
prints("mach_msg error "); prints("mach_msg error ");
...@@ -367,7 +262,7 @@ machcall(mach_msg_header_t *h, int32 maxsize, int32 rxsize) ...@@ -367,7 +262,7 @@ machcall(mach_msg_header_t *h, int32 maxsize, int32 rxsize)
if(DebugMach){ if(DebugMach){
p = (uint32*)h; p = (uint32*)h;
prints("recv:\t"); prints("recv:\t");
for(i=0; i<h->size/sizeof(p[0]); i++){ for(i=0; i<h->msgh_size/sizeof(p[0]); i++){
prints(" "); prints(" ");
sys·printpointer((void*)p[i]); sys·printpointer((void*)p[i]);
if(i%8 == 7) if(i%8 == 7)
...@@ -377,10 +272,10 @@ machcall(mach_msg_header_t *h, int32 maxsize, int32 rxsize) ...@@ -377,10 +272,10 @@ machcall(mach_msg_header_t *h, int32 maxsize, int32 rxsize)
prints("\n"); prints("\n");
} }
if(h->id != id+Reply){ if(h->msgh_id != id+Reply){
if(DebugMach){ if(DebugMach){
prints("mach_msg reply id mismatch "); prints("mach_msg reply id mismatch ");
sys·printint(h->id); sys·printint(h->msgh_id);
prints(" != "); prints(" != ");
sys·printint(id+Reply); sys·printint(id+Reply);
prints("\n"); prints("\n");
...@@ -395,8 +290,8 @@ machcall(mach_msg_header_t *h, int32 maxsize, int32 rxsize) ...@@ -395,8 +290,8 @@ machcall(mach_msg_header_t *h, int32 maxsize, int32 rxsize)
// you know it's one of these and not the full response // you know it's one of these and not the full response
// format, so just look if the message is right. // format, so just look if the message is right.
c = (CodeMsg*)h; c = (CodeMsg*)h;
if(h->size == sizeof(CodeMsg) if(h->msgh_size == sizeof(CodeMsg)
&& !(h->bits & MACH_MSGH_BITS_COMPLEX)){ && !(h->msgh_bits & MACH_MSGH_BITS_COMPLEX)){
if(DebugMach){ if(DebugMach){
prints("mig result "); prints("mig result ");
sys·printint(c->code); sys·printint(c->code);
...@@ -405,10 +300,10 @@ machcall(mach_msg_header_t *h, int32 maxsize, int32 rxsize) ...@@ -405,10 +300,10 @@ machcall(mach_msg_header_t *h, int32 maxsize, int32 rxsize)
return c->code; return c->code;
} }
if(h->size != rxsize){ if(h->msgh_size != rxsize){
if(DebugMach){ if(DebugMach){
prints("mach_msg reply size mismatch "); prints("mach_msg reply size mismatch ");
sys·printint(h->size); sys·printint(h->msgh_size);
prints(" != "); prints(" != ");
sys·printint(rxsize); sys·printint(rxsize);
prints("\n"); prints("\n");
...@@ -439,28 +334,28 @@ typedef struct Tmach_semdestroyMsg Tmach_semdestroyMsg; ...@@ -439,28 +334,28 @@ typedef struct Tmach_semdestroyMsg Tmach_semdestroyMsg;
#pragma pack on #pragma pack on
struct Tmach_semcreateMsg struct Tmach_semcreateMsg
{ {
mach_msg_header_t h; MachHeader h;
NDR_record_t ndr; MachNDR ndr;
int32 policy; int32 policy;
int32 value; int32 value;
}; };
struct Rmach_semcreateMsg struct Rmach_semcreateMsg
{ {
mach_msg_header_t h; MachHeader h;
mach_msg_body_t body; MachBody body;
mach_msg_port_descriptor_t semaphore; MachPort semaphore;
}; };
struct Tmach_semdestroyMsg struct Tmach_semdestroyMsg
{ {
mach_msg_header_t h; MachHeader h;
mach_msg_body_t body; MachBody body;
mach_msg_port_descriptor_t semaphore; MachPort semaphore;
}; };
#pragma pack off #pragma pack off
mach_port_t uint32
mach_semcreate(void) mach_semcreate(void)
{ {
union { union {
...@@ -468,12 +363,12 @@ mach_semcreate(void) ...@@ -468,12 +363,12 @@ mach_semcreate(void)
Rmach_semcreateMsg rx; Rmach_semcreateMsg rx;
uint8 pad[MinMachMsg]; uint8 pad[MinMachMsg];
} m; } m;
kern_return_t r; int32 r;
m.tx.h.bits = 0; m.tx.h.msgh_bits = 0;
m.tx.h.size = sizeof(m.tx); m.tx.h.msgh_size = sizeof(m.tx);
m.tx.h.remote_port = mach_task_self(); m.tx.h.msgh_remote_port = mach_task_self();
m.tx.h.id = Tmach_semcreate; m.tx.h.msgh_id = Tmach_semcreate;
m.tx.ndr = zerondr; m.tx.ndr = zerondr;
m.tx.policy = 0; // 0 = SYNC_POLICY_FIFO m.tx.policy = 0; // 0 = SYNC_POLICY_FIFO
...@@ -481,25 +376,25 @@ mach_semcreate(void) ...@@ -481,25 +376,25 @@ mach_semcreate(void)
if((r = machcall(&m.tx.h, sizeof m, sizeof(m.rx))) != 0) if((r = machcall(&m.tx.h, sizeof m, sizeof(m.rx))) != 0)
macherror(r, "semaphore_create"); macherror(r, "semaphore_create");
if(m.rx.body.descriptor_count != 1) if(m.rx.body.msgh_descriptor_count != 1)
unimplemented("mach_semcreate desc count"); unimplemented("mach_semcreate desc count");
return m.rx.semaphore.name; return m.rx.semaphore.name;
} }
void void
mach_semdestroy(mach_port_t sem) mach_semdestroy(uint32 sem)
{ {
union { union {
Tmach_semdestroyMsg tx; Tmach_semdestroyMsg tx;
uint8 pad[MinMachMsg]; uint8 pad[MinMachMsg];
} m; } m;
kern_return_t r; int32 r;
m.tx.h.bits = MACH_MSGH_BITS_COMPLEX; m.tx.h.msgh_bits = MACH_MSGH_BITS_COMPLEX;
m.tx.h.size = sizeof(m.tx); m.tx.h.msgh_size = sizeof(m.tx);
m.tx.h.remote_port = mach_task_self(); m.tx.h.msgh_remote_port = mach_task_self();
m.tx.h.id = Tmach_semdestroy; m.tx.h.msgh_id = Tmach_semdestroy;
m.tx.body.descriptor_count = 1; m.tx.body.msgh_descriptor_count = 1;
m.tx.semaphore.name = sem; m.tx.semaphore.name = sem;
m.tx.semaphore.disposition = MACH_MSG_TYPE_MOVE_SEND; m.tx.semaphore.disposition = MACH_MSG_TYPE_MOVE_SEND;
m.tx.semaphore.type = 0; m.tx.semaphore.type = 0;
...@@ -508,26 +403,25 @@ mach_semdestroy(mach_port_t sem) ...@@ -508,26 +403,25 @@ mach_semdestroy(mach_port_t sem)
macherror(r, "semaphore_destroy"); macherror(r, "semaphore_destroy");
} }
// The other calls have simple system call traps // The other calls have simple system call traps in sys.s
// in sys_amd64_darwin.s int32 mach_semaphore_wait(uint32 sema);
kern_return_t mach_semaphore_wait(uint32 sema); int32 mach_semaphore_timedwait(uint32 sema, uint32 sec, uint32 nsec);
kern_return_t mach_semaphore_timedwait(uint32 sema, uint32 sec, uint32 nsec); int32 mach_semaphore_signal(uint32 sema);
kern_return_t mach_semaphore_signal(uint32 sema); int32 mach_semaphore_signal_all(uint32 sema);
kern_return_t mach_semaphore_signal_all(uint32 sema);
void void
mach_semacquire(mach_port_t sem) mach_semacquire(uint32 sem)
{ {
kern_return_t r; int32 r;
if((r = mach_semaphore_wait(sem)) != 0) if((r = mach_semaphore_wait(sem)) != 0)
macherror(r, "semaphore_wait"); macherror(r, "semaphore_wait");
} }
void void
mach_semrelease(mach_port_t sem) mach_semrelease(uint32 sem)
{ {
kern_return_t r; int32 r;
if((r = mach_semaphore_signal(sem)) != 0) if((r = mach_semaphore_signal(sem)) != 0)
macherror(r, "semaphore_signal"); macherror(r, "semaphore_signal");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment