This source file includes following definitions.
- unimplemented
- runtime·semawakeup
- runtime·semacreate
- runtime·osinit
- runtime·get_random_data
- runtime·goenvs
- runtime·newosproc
- runtime·mpreinit
- runtime·minit
- runtime·unminit
- macherror
- mach_msg
- machcall
- runtime·mach_semcreate
- runtime·mach_semdestroy
- runtime·semasleep
- runtime·mach_semrelease
- runtime·sigpanic
- runtime·osyield
- runtime·memlimit
- runtime·setsig
- runtime·getsig
- runtime·signalstack
- runtime·unblocksignals
#include "runtime.h"
#include "defs_GOOS_GOARCH.h"
#include "os_GOOS.h"
#include "signal_unix.h"
#include "stack.h"
#include "../../cmd/ld/textflag.h"
extern SigTab runtime·sigtab[];
static Sigset sigset_none;
static Sigset sigset_all = ~(Sigset)0;
static void
unimplemented(int8 *name)
{
runtime·prints(name);
runtime·prints(" not implemented\n");
*(int32*)1231 = 1231;
}
void
runtime·semawakeup(M *mp)
{
runtime·mach_semrelease(mp->waitsema);
}
uintptr
runtime·semacreate(void)
{
return runtime·mach_semcreate();
}
void
runtime·osinit(void)
{
uint32 mib[2];
uint32 out;
int32 ret;
uintptr nout;
mib[0] = 6;
mib[1] = 3;
nout = sizeof out;
out = 0;
ret = runtime·sysctl(mib, 2, (byte*)&out, &nout, nil, 0);
if(ret >= 0)
runtime·ncpu = out;
}
void
runtime·get_random_data(byte **rnd, int32 *rnd_len)
{
#pragma dataflag NOPTR
static byte urandom_data[HashRandomBytes];
int32 fd;
fd = runtime·open("/dev/urandom", 0 , 0);
if(runtime·read(fd, urandom_data, HashRandomBytes) == HashRandomBytes) {
*rnd = urandom_data;
*rnd_len = HashRandomBytes;
} else {
*rnd = nil;
*rnd_len = 0;
}
runtime·close(fd);
}
void
runtime·goenvs(void)
{
runtime·goenvs_unix();
if(!runtime·iscgo) {
if(runtime·bsdthread_register() != 0) {
if(runtime·getenv("DYLD_INSERT_LIBRARIES"))
runtime·throw("runtime: bsdthread_register error (unset DYLD_INSERT_LIBRARIES)");
runtime·throw("runtime: bsdthread_register error");
}
}
}
void
runtime·newosproc(M *mp, void *stk)
{
int32 errno;
Sigset oset;
mp->tls[0] = mp->id;
if(0){
runtime·printf("newosproc stk=%p m=%p g=%p id=%d/%d ostk=%p\n",
stk, mp, mp->g0, mp->id, (int32)mp->tls[0], &mp);
}
runtime·sigprocmask(SIG_SETMASK, &sigset_all, &oset);
errno = runtime·bsdthread_create(stk, mp, mp->g0, runtime·mstart);
runtime·sigprocmask(SIG_SETMASK, &oset, nil);
if(errno < 0) {
runtime·printf("runtime: failed to create new OS thread (have %d already; errno=%d)\n", runtime·mcount(), -errno);
runtime·throw("runtime.newosproc");
}
}
void
runtime·mpreinit(M *mp)
{
mp->gsignal = runtime·malg(32*1024);
}
void
runtime·minit(void)
{
runtime·signalstack((byte*)m->gsignal->stackguard - StackGuard, 32*1024);
runtime·sigprocmask(SIG_SETMASK, &sigset_none, nil);
}
void
runtime·unminit(void)
{
runtime·signalstack(nil, 0);
}
#pragma textflag NOSPLIT
static void
macherror(int32 r, int8 *fn)
{
runtime·prints("mach error ");
runtime·prints(fn);
runtime·prints(": ");
runtime·printint(r);
runtime·prints("\n");
runtime·throw("mach error");
}
enum
{
DebugMach = 0
};
static MachNDR zerondr;
#define MACH_MSGH_BITS(a, b) ((a) | ((b)<<8))
static int32
mach_msg(MachHeader *h,
int32 op,
uint32 send_size,
uint32 rcv_size,
uint32 rcv_name,
uint32 timeout,
uint32 notify)
{
return runtime·mach_msg_trap(h, op, send_size, rcv_size, rcv_name, timeout, notify);
}
enum
{
MinMachMsg = 48,
Reply = 100,
};
#pragma pack on
typedef struct CodeMsg CodeMsg;
struct CodeMsg
{
MachHeader h;
MachNDR NDR;
int32 code;
};
#pragma pack off
static int32
machcall(MachHeader *h, int32 maxsize, int32 rxsize)
{
uint32 *p;
int32 i, ret, id;
uint32 port;
CodeMsg *c;
if((port = m->machport) == 0){
port = runtime·mach_reply_port();
m->machport = port;
}
h->msgh_bits |= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE);
h->msgh_local_port = port;
h->msgh_reserved = 0;
id = h->msgh_id;
if(DebugMach){
p = (uint32*)h;
runtime·prints("send:\t");
for(i=0; i<h->msgh_size/sizeof(p[0]); i++){
runtime·prints(" ");
runtime·printpointer((void*)p[i]);
if(i%8 == 7)
runtime·prints("\n\t");
}
if(i%8)
runtime·prints("\n");
}
ret = mach_msg(h, MACH_SEND_MSG|MACH_RCV_MSG,
h->msgh_size, maxsize, port, 0, 0);
if(ret != 0){
if(DebugMach){
runtime·prints("mach_msg error ");
runtime·printint(ret);
runtime·prints("\n");
}
return ret;
}
if(DebugMach){
p = (uint32*)h;
runtime·prints("recv:\t");
for(i=0; i<h->msgh_size/sizeof(p[0]); i++){
runtime·prints(" ");
runtime·printpointer((void*)p[i]);
if(i%8 == 7)
runtime·prints("\n\t");
}
if(i%8)
runtime·prints("\n");
}
if(h->msgh_id != id+Reply){
if(DebugMach){
runtime·prints("mach_msg reply id mismatch ");
runtime·printint(h->msgh_id);
runtime·prints(" != ");
runtime·printint(id+Reply);
runtime·prints("\n");
}
return -303;
}
c = (CodeMsg*)h;
if(h->msgh_size == sizeof(CodeMsg)
&& !(h->msgh_bits & MACH_MSGH_BITS_COMPLEX)){
if(DebugMach){
runtime·prints("mig result ");
runtime·printint(c->code);
runtime·prints("\n");
}
return c->code;
}
if(h->msgh_size != rxsize){
if(DebugMach){
runtime·prints("mach_msg reply size mismatch ");
runtime·printint(h->msgh_size);
runtime·prints(" != ");
runtime·printint(rxsize);
runtime·prints("\n");
}
return -307;
}
return 0;
}
enum
{
Tmach_semcreate = 3418,
Rmach_semcreate = Tmach_semcreate + Reply,
Tmach_semdestroy = 3419,
Rmach_semdestroy = Tmach_semdestroy + Reply,
KERN_ABORTED = 14,
KERN_OPERATION_TIMED_OUT = 49,
};
typedef struct Tmach_semcreateMsg Tmach_semcreateMsg;
typedef struct Rmach_semcreateMsg Rmach_semcreateMsg;
typedef struct Tmach_semdestroyMsg Tmach_semdestroyMsg;
#pragma pack on
struct Tmach_semcreateMsg
{
MachHeader h;
MachNDR ndr;
int32 policy;
int32 value;
};
struct Rmach_semcreateMsg
{
MachHeader h;
MachBody body;
MachPort semaphore;
};
struct Tmach_semdestroyMsg
{
MachHeader h;
MachBody body;
MachPort semaphore;
};
#pragma pack off
uint32
runtime·mach_semcreate(void)
{
union {
Tmach_semcreateMsg tx;
Rmach_semcreateMsg rx;
uint8 pad[MinMachMsg];
} m;
int32 r;
m.tx.h.msgh_bits = 0;
m.tx.h.msgh_size = sizeof(m.tx);
m.tx.h.msgh_remote_port = runtime·mach_task_self();
m.tx.h.msgh_id = Tmach_semcreate;
m.tx.ndr = zerondr;
m.tx.policy = 0;
m.tx.value = 0;
while((r = machcall(&m.tx.h, sizeof m, sizeof(m.rx))) != 0){
if(r == KERN_ABORTED)
continue;
macherror(r, "semaphore_create");
}
if(m.rx.body.msgh_descriptor_count != 1)
unimplemented("mach_semcreate desc count");
return m.rx.semaphore.name;
}
void
runtime·mach_semdestroy(uint32 sem)
{
union {
Tmach_semdestroyMsg tx;
uint8 pad[MinMachMsg];
} m;
int32 r;
m.tx.h.msgh_bits = MACH_MSGH_BITS_COMPLEX;
m.tx.h.msgh_size = sizeof(m.tx);
m.tx.h.msgh_remote_port = runtime·mach_task_self();
m.tx.h.msgh_id = Tmach_semdestroy;
m.tx.body.msgh_descriptor_count = 1;
m.tx.semaphore.name = sem;
m.tx.semaphore.disposition = MACH_MSG_TYPE_MOVE_SEND;
m.tx.semaphore.type = 0;
while((r = machcall(&m.tx.h, sizeof m, 0)) != 0){
if(r == KERN_ABORTED)
continue;
macherror(r, "semaphore_destroy");
}
}
int32 runtime·mach_semaphore_wait(uint32 sema);
int32 runtime·mach_semaphore_timedwait(uint32 sema, uint32 sec, uint32 nsec);
int32 runtime·mach_semaphore_signal(uint32 sema);
int32 runtime·mach_semaphore_signal_all(uint32 sema);
#pragma textflag NOSPLIT
int32
runtime·semasleep(int64 ns)
{
int32 r, secs, nsecs;
if(ns >= 0) {
secs = runtime·timediv(ns, 1000000000, &nsecs);
r = runtime·mach_semaphore_timedwait(m->waitsema, secs, nsecs);
if(r == KERN_ABORTED || r == KERN_OPERATION_TIMED_OUT)
return -1;
if(r != 0)
macherror(r, "semaphore_wait");
return 0;
}
while((r = runtime·mach_semaphore_wait(m->waitsema)) != 0) {
if(r == KERN_ABORTED)
continue;
macherror(r, "semaphore_wait");
}
return 0;
}
void
runtime·mach_semrelease(uint32 sem)
{
int32 r;
while((r = runtime·mach_semaphore_signal(sem)) != 0) {
if(r == KERN_ABORTED)
continue;
macherror(r, "semaphore_signal");
}
}
void
runtime·sigpanic(void)
{
if(!runtime·canpanic(g))
runtime·throw("unexpected signal during runtime execution");
switch(g->sig) {
case SIGBUS:
if(g->sigcode0 == BUS_ADRERR && g->sigcode1 < 0x1000 || g->paniconfault) {
if(g->sigpc == 0)
runtime·panicstring("call of nil func value");
runtime·panicstring("invalid memory address or nil pointer dereference");
}
runtime·printf("unexpected fault address %p\n", g->sigcode1);
runtime·throw("fault");
case SIGSEGV:
if((g->sigcode0 == 0 || g->sigcode0 == SEGV_MAPERR || g->sigcode0 == SEGV_ACCERR) && g->sigcode1 < 0x1000 || g->paniconfault) {
if(g->sigpc == 0)
runtime·panicstring("call of nil func value");
runtime·panicstring("invalid memory address or nil pointer dereference");
}
runtime·printf("unexpected fault address %p\n", g->sigcode1);
runtime·throw("fault");
case SIGFPE:
switch(g->sigcode0) {
case FPE_INTDIV:
runtime·panicstring("integer divide by zero");
case FPE_INTOVF:
runtime·panicstring("integer overflow");
}
runtime·panicstring("floating point error");
}
runtime·panicstring(runtime·sigtab[g->sig].name);
}
#pragma textflag NOSPLIT
void
runtime·osyield(void)
{
runtime·usleep(1);
}
uintptr
runtime·memlimit(void)
{
return 0;
}
void
runtime·setsig(int32 i, GoSighandler *fn, bool restart)
{
Sigaction sa;
runtime·memclr((byte*)&sa, sizeof sa);
sa.sa_flags = SA_SIGINFO|SA_ONSTACK;
if(restart)
sa.sa_flags |= SA_RESTART;
sa.sa_mask = ~(uintptr)0;
sa.sa_tramp = (void*)runtime·sigtramp;
*(uintptr*)sa.__sigaction_u = (uintptr)fn;
runtime·sigaction(i, &sa, nil);
}
GoSighandler*
runtime·getsig(int32 i)
{
Sigaction sa;
runtime·memclr((byte*)&sa, sizeof sa);
runtime·sigaction(i, nil, &sa);
return *(void**)sa.__sigaction_u;
}
void
runtime·signalstack(byte *p, int32 n)
{
StackT st;
st.ss_sp = (void*)p;
st.ss_size = n;
st.ss_flags = 0;
if(p == nil)
st.ss_flags = SS_DISABLE;
runtime·sigaltstack(&st, nil);
}
void
runtime·unblocksignals(void)
{
runtime·sigprocmask(SIG_SETMASK, &sigset_none, nil);
}