Rob Pike | aeb4398 | 2008-06-21 15:36:23 -0700 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | #include "runtime.h" |
Russ Cox | 851f301 | 2011-12-16 15:33:58 -0500 | [diff] [blame] | 6 | #include "defs_GOOS_GOARCH.h" |
| 7 | #include "os_GOOS.h" |
Russ Cox | 820dc9f | 2011-02-24 13:46:44 -0800 | [diff] [blame] | 8 | #include "stack.h" |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 9 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 10 | extern SigTab runtime·sigtab[]; |
Russ Cox | 5963dba | 2010-04-08 18:15:30 -0700 | [diff] [blame] | 11 | |
Russ Cox | 224f05b | 2012-02-23 14:44:06 -0500 | [diff] [blame] | 12 | static Sigset sigset_all = ~(Sigset)0; |
| 13 | static Sigset sigset_none; |
Russ Cox | 6e2ae0a | 2012-02-28 16:18:24 -0500 | [diff] [blame] | 14 | static Sigset sigset_prof = 1<<(SIGPROF-1); |
Russ Cox | 224f05b | 2012-02-23 14:44:06 -0500 | [diff] [blame] | 15 | |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 16 | static void |
| 17 | unimplemented(int8 *name) |
| 18 | { |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 19 | runtime·prints(name); |
| 20 | runtime·prints(" not implemented\n"); |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 21 | *(int32*)1231 = 1231; |
| 22 | } |
| 23 | |
Russ Cox | 3b86026 | 2011-11-09 15:17:05 -0500 | [diff] [blame] | 24 | int32 |
| 25 | runtime·semasleep(int64 ns) |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 26 | { |
Russ Cox | 6e2ae0a | 2012-02-28 16:18:24 -0500 | [diff] [blame] | 27 | int32 v; |
| 28 | |
| 29 | if(m->profilehz > 0) |
| 30 | runtime·setprof(false); |
| 31 | v = runtime·mach_semacquire(m->waitsema, ns); |
| 32 | if(m->profilehz > 0) |
| 33 | runtime·setprof(true); |
| 34 | return v; |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void |
Dmitriy Vyukov | ee24bfc | 2011-11-02 16:42:01 +0300 | [diff] [blame] | 38 | runtime·semawakeup(M *mp) |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 39 | { |
Dmitriy Vyukov | ee24bfc | 2011-11-02 16:42:01 +0300 | [diff] [blame] | 40 | runtime·mach_semrelease(mp->waitsema); |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Dmitriy Vyukov | ee24bfc | 2011-11-02 16:42:01 +0300 | [diff] [blame] | 43 | uintptr |
| 44 | runtime·semacreate(void) |
Russ Cox | 62d627f | 2010-02-08 21:41:54 -0800 | [diff] [blame] | 45 | { |
Dmitriy Vyukov | ee24bfc | 2011-11-02 16:42:01 +0300 | [diff] [blame] | 46 | return runtime·mach_semcreate(); |
Russ Cox | 62d627f | 2010-02-08 21:41:54 -0800 | [diff] [blame] | 47 | } |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 48 | |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 49 | // BSD interface for threading. |
| 50 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 51 | runtime·osinit(void) |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 52 | { |
Shenghou Ma | 44fd1d1 | 2012-04-30 15:55:07 -0400 | [diff] [blame^] | 53 | // bsdthread_register delayed until end of goenvs so that we |
| 54 | // can look at the environment first. |
Russ Cox | d324f21 | 2011-09-30 09:40:01 -0400 | [diff] [blame] | 55 | |
| 56 | // Use sysctl to fetch hw.ncpu. |
| 57 | uint32 mib[2]; |
| 58 | uint32 out; |
| 59 | int32 ret; |
| 60 | uintptr nout; |
| 61 | |
| 62 | mib[0] = 6; |
| 63 | mib[1] = 3; |
| 64 | nout = sizeof out; |
| 65 | out = 0; |
| 66 | ret = runtime·sysctl(mib, 2, (byte*)&out, &nout, nil, 0); |
| 67 | if(ret >= 0) |
| 68 | runtime·ncpu = out; |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void |
Alex Brainman | a41d854 | 2011-01-12 11:48:15 +1100 | [diff] [blame] | 72 | runtime·goenvs(void) |
| 73 | { |
| 74 | runtime·goenvs_unix(); |
Shenghou Ma | 44fd1d1 | 2012-04-30 15:55:07 -0400 | [diff] [blame^] | 75 | |
| 76 | // Register our thread-creation callback (see sys_darwin_{amd64,386}.s) |
| 77 | // but only if we're not using cgo. If we are using cgo we need |
| 78 | // to let the C pthread libary install its own thread-creation callback. |
| 79 | if(!runtime·iscgo) { |
| 80 | if(runtime·bsdthread_register() != 0) { |
| 81 | if(runtime·getenv("DYLD_INSERT_LIBRARIES")) |
| 82 | runtime·throw("runtime: bsdthread_register error (unset DYLD_INSERT_LIBRARIES)"); |
| 83 | runtime·throw("runtime: bsdthread_register error"); |
| 84 | } |
| 85 | } |
| 86 | |
Alex Brainman | a41d854 | 2011-01-12 11:48:15 +1100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 90 | runtime·newosproc(M *m, G *g, void *stk, void (*fn)(void)) |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 91 | { |
Russ Cox | 4608feb | 2011-01-28 15:03:26 -0500 | [diff] [blame] | 92 | int32 errno; |
Russ Cox | 224f05b | 2012-02-23 14:44:06 -0500 | [diff] [blame] | 93 | Sigset oset; |
Russ Cox | 4608feb | 2011-01-28 15:03:26 -0500 | [diff] [blame] | 94 | |
Russ Cox | 3a0df4c | 2009-06-04 11:16:03 -0700 | [diff] [blame] | 95 | m->tls[0] = m->id; // so 386 asm can find it |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 96 | if(0){ |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 97 | runtime·printf("newosproc stk=%p m=%p g=%p fn=%p id=%d/%d ostk=%p\n", |
Russ Cox | 8522a47 | 2009-06-17 15:15:55 -0700 | [diff] [blame] | 98 | stk, m, g, fn, m->id, m->tls[0], &m); |
| 99 | } |
Russ Cox | 224f05b | 2012-02-23 14:44:06 -0500 | [diff] [blame] | 100 | |
| 101 | runtime·sigprocmask(SIG_SETMASK, &sigset_all, &oset); |
| 102 | errno = runtime·bsdthread_create(stk, m, g, fn); |
| 103 | runtime·sigprocmask(SIG_SETMASK, &oset, nil); |
Russ Cox | 6e2ae0a | 2012-02-28 16:18:24 -0500 | [diff] [blame] | 104 | |
Russ Cox | 224f05b | 2012-02-23 14:44:06 -0500 | [diff] [blame] | 105 | if(errno < 0) { |
Russ Cox | 4608feb | 2011-01-28 15:03:26 -0500 | [diff] [blame] | 106 | runtime·printf("runtime: failed to create new OS thread (have %d already; errno=%d)\n", runtime·mcount(), -errno); |
| 107 | runtime·throw("runtime.newosproc"); |
| 108 | } |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Russ Cox | a67258f | 2008-09-18 15:56:46 -0700 | [diff] [blame] | 111 | // Called to initialize a new m (including the bootstrap m). |
| 112 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 113 | runtime·minit(void) |
Russ Cox | a67258f | 2008-09-18 15:56:46 -0700 | [diff] [blame] | 114 | { |
| 115 | // Initialize signal handling. |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 116 | m->gsignal = runtime·malg(32*1024); // OS X wants >=8K, Linux >=2K |
Russ Cox | 820dc9f | 2011-02-24 13:46:44 -0800 | [diff] [blame] | 117 | runtime·signalstack(m->gsignal->stackguard - StackGuard, 32*1024); |
Russ Cox | 6e2ae0a | 2012-02-28 16:18:24 -0500 | [diff] [blame] | 118 | |
| 119 | if(m->profilehz > 0) |
| 120 | runtime·sigprocmask(SIG_SETMASK, &sigset_none, nil); |
| 121 | else |
| 122 | runtime·sigprocmask(SIG_SETMASK, &sigset_prof, nil); |
Russ Cox | a67258f | 2008-09-18 15:56:46 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 125 | // Mach IPC, to get at semaphores |
| 126 | // Definitions are in /usr/include/mach on a Mac. |
| 127 | |
| 128 | static void |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 129 | macherror(int32 r, int8 *fn) |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 130 | { |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 131 | runtime·printf("mach error %s: %d\n", fn, r); |
| 132 | runtime·throw("mach error"); |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | enum |
| 136 | { |
| 137 | DebugMach = 0 |
| 138 | }; |
| 139 | |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 140 | static MachNDR zerondr; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 141 | |
| 142 | #define MACH_MSGH_BITS(a, b) ((a) | ((b)<<8)) |
| 143 | |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 144 | static int32 |
| 145 | mach_msg(MachHeader *h, |
| 146 | int32 op, |
| 147 | uint32 send_size, |
| 148 | uint32 rcv_size, |
| 149 | uint32 rcv_name, |
| 150 | uint32 timeout, |
| 151 | uint32 notify) |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 152 | { |
| 153 | // TODO: Loop on interrupt. |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 154 | return runtime·mach_msg_trap(h, op, send_size, rcv_size, rcv_name, timeout, notify); |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 157 | // Mach RPC (MIG) |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 158 | |
| 159 | enum |
| 160 | { |
| 161 | MinMachMsg = 48, |
| 162 | Reply = 100, |
| 163 | }; |
| 164 | |
| 165 | #pragma pack on |
| 166 | typedef struct CodeMsg CodeMsg; |
| 167 | struct CodeMsg |
| 168 | { |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 169 | MachHeader h; |
| 170 | MachNDR NDR; |
| 171 | int32 code; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 172 | }; |
| 173 | #pragma pack off |
| 174 | |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 175 | static int32 |
| 176 | machcall(MachHeader *h, int32 maxsize, int32 rxsize) |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 177 | { |
| 178 | uint32 *p; |
| 179 | int32 i, ret, id; |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 180 | uint32 port; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 181 | CodeMsg *c; |
| 182 | |
| 183 | if((port = m->machport) == 0){ |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 184 | port = runtime·mach_reply_port(); |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 185 | m->machport = port; |
| 186 | } |
| 187 | |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 188 | h->msgh_bits |= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE); |
| 189 | h->msgh_local_port = port; |
| 190 | h->msgh_reserved = 0; |
| 191 | id = h->msgh_id; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 192 | |
| 193 | if(DebugMach){ |
| 194 | p = (uint32*)h; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 195 | runtime·prints("send:\t"); |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 196 | for(i=0; i<h->msgh_size/sizeof(p[0]); i++){ |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 197 | runtime·prints(" "); |
| 198 | runtime·printpointer((void*)p[i]); |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 199 | if(i%8 == 7) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 200 | runtime·prints("\n\t"); |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 201 | } |
| 202 | if(i%8) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 203 | runtime·prints("\n"); |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | ret = mach_msg(h, MACH_SEND_MSG|MACH_RCV_MSG, |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 207 | h->msgh_size, maxsize, port, 0, 0); |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 208 | if(ret != 0){ |
| 209 | if(DebugMach){ |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 210 | runtime·prints("mach_msg error "); |
| 211 | runtime·printint(ret); |
| 212 | runtime·prints("\n"); |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 213 | } |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | if(DebugMach){ |
| 218 | p = (uint32*)h; |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 219 | runtime·prints("recv:\t"); |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 220 | for(i=0; i<h->msgh_size/sizeof(p[0]); i++){ |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 221 | runtime·prints(" "); |
| 222 | runtime·printpointer((void*)p[i]); |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 223 | if(i%8 == 7) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 224 | runtime·prints("\n\t"); |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 225 | } |
| 226 | if(i%8) |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 227 | runtime·prints("\n"); |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 230 | if(h->msgh_id != id+Reply){ |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 231 | if(DebugMach){ |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 232 | runtime·prints("mach_msg reply id mismatch "); |
| 233 | runtime·printint(h->msgh_id); |
| 234 | runtime·prints(" != "); |
| 235 | runtime·printint(id+Reply); |
| 236 | runtime·prints("\n"); |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 237 | } |
| 238 | return -303; // MIG_REPLY_MISMATCH |
| 239 | } |
| 240 | |
| 241 | // Look for a response giving the return value. |
| 242 | // Any call can send this back with an error, |
| 243 | // and some calls only have return values so they |
| 244 | // send it back on success too. I don't quite see how |
| 245 | // you know it's one of these and not the full response |
| 246 | // format, so just look if the message is right. |
| 247 | c = (CodeMsg*)h; |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 248 | if(h->msgh_size == sizeof(CodeMsg) |
| 249 | && !(h->msgh_bits & MACH_MSGH_BITS_COMPLEX)){ |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 250 | if(DebugMach){ |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 251 | runtime·prints("mig result "); |
| 252 | runtime·printint(c->code); |
| 253 | runtime·prints("\n"); |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 254 | } |
| 255 | return c->code; |
| 256 | } |
| 257 | |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 258 | if(h->msgh_size != rxsize){ |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 259 | if(DebugMach){ |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 260 | runtime·prints("mach_msg reply size mismatch "); |
| 261 | runtime·printint(h->msgh_size); |
| 262 | runtime·prints(" != "); |
| 263 | runtime·printint(rxsize); |
| 264 | runtime·prints("\n"); |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 265 | } |
| 266 | return -307; // MIG_ARRAY_TOO_LARGE |
| 267 | } |
| 268 | |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 269 | return 0; |
| 270 | } |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 271 | |
| 272 | |
| 273 | // Semaphores! |
| 274 | |
| 275 | enum |
| 276 | { |
Russ Cox | 1f8a40d | 2009-01-22 16:23:44 -0800 | [diff] [blame] | 277 | Tmach_semcreate = 3418, |
| 278 | Rmach_semcreate = Tmach_semcreate + Reply, |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 279 | |
Russ Cox | 1f8a40d | 2009-01-22 16:23:44 -0800 | [diff] [blame] | 280 | Tmach_semdestroy = 3419, |
| 281 | Rmach_semdestroy = Tmach_semdestroy + Reply, |
Russ Cox | cd80000 | 2009-06-08 14:03:21 -0700 | [diff] [blame] | 282 | |
Russ Cox | 925183c | 2009-06-08 14:09:04 -0700 | [diff] [blame] | 283 | // Mach calls that get interrupted by Unix signals |
| 284 | // return this error code. We retry them. |
Russ Cox | cd80000 | 2009-06-08 14:03:21 -0700 | [diff] [blame] | 285 | KERN_ABORTED = 14, |
Russ Cox | 3b86026 | 2011-11-09 15:17:05 -0500 | [diff] [blame] | 286 | KERN_OPERATION_TIMED_OUT = 49, |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 287 | }; |
| 288 | |
Russ Cox | 1f8a40d | 2009-01-22 16:23:44 -0800 | [diff] [blame] | 289 | typedef struct Tmach_semcreateMsg Tmach_semcreateMsg; |
| 290 | typedef struct Rmach_semcreateMsg Rmach_semcreateMsg; |
| 291 | typedef struct Tmach_semdestroyMsg Tmach_semdestroyMsg; |
| 292 | // Rmach_semdestroyMsg = CodeMsg |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 293 | |
| 294 | #pragma pack on |
Russ Cox | 1f8a40d | 2009-01-22 16:23:44 -0800 | [diff] [blame] | 295 | struct Tmach_semcreateMsg |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 296 | { |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 297 | MachHeader h; |
| 298 | MachNDR ndr; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 299 | int32 policy; |
| 300 | int32 value; |
| 301 | }; |
| 302 | |
Russ Cox | 1f8a40d | 2009-01-22 16:23:44 -0800 | [diff] [blame] | 303 | struct Rmach_semcreateMsg |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 304 | { |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 305 | MachHeader h; |
| 306 | MachBody body; |
| 307 | MachPort semaphore; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 308 | }; |
| 309 | |
Russ Cox | 1f8a40d | 2009-01-22 16:23:44 -0800 | [diff] [blame] | 310 | struct Tmach_semdestroyMsg |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 311 | { |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 312 | MachHeader h; |
| 313 | MachBody body; |
| 314 | MachPort semaphore; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 315 | }; |
| 316 | #pragma pack off |
| 317 | |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 318 | uint32 |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 319 | runtime·mach_semcreate(void) |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 320 | { |
| 321 | union { |
Russ Cox | 1f8a40d | 2009-01-22 16:23:44 -0800 | [diff] [blame] | 322 | Tmach_semcreateMsg tx; |
| 323 | Rmach_semcreateMsg rx; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 324 | uint8 pad[MinMachMsg]; |
| 325 | } m; |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 326 | int32 r; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 327 | |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 328 | m.tx.h.msgh_bits = 0; |
| 329 | m.tx.h.msgh_size = sizeof(m.tx); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 330 | m.tx.h.msgh_remote_port = runtime·mach_task_self(); |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 331 | m.tx.h.msgh_id = Tmach_semcreate; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 332 | m.tx.ndr = zerondr; |
| 333 | |
| 334 | m.tx.policy = 0; // 0 = SYNC_POLICY_FIFO |
| 335 | m.tx.value = 0; |
| 336 | |
Russ Cox | cd80000 | 2009-06-08 14:03:21 -0700 | [diff] [blame] | 337 | while((r = machcall(&m.tx.h, sizeof m, sizeof(m.rx))) != 0){ |
| 338 | if(r == KERN_ABORTED) // interrupted |
| 339 | continue; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 340 | macherror(r, "semaphore_create"); |
Russ Cox | cd80000 | 2009-06-08 14:03:21 -0700 | [diff] [blame] | 341 | } |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 342 | if(m.rx.body.msgh_descriptor_count != 1) |
Russ Cox | 1f8a40d | 2009-01-22 16:23:44 -0800 | [diff] [blame] | 343 | unimplemented("mach_semcreate desc count"); |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 344 | return m.rx.semaphore.name; |
| 345 | } |
| 346 | |
| 347 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 348 | runtime·mach_semdestroy(uint32 sem) |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 349 | { |
| 350 | union { |
Russ Cox | 1f8a40d | 2009-01-22 16:23:44 -0800 | [diff] [blame] | 351 | Tmach_semdestroyMsg tx; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 352 | uint8 pad[MinMachMsg]; |
| 353 | } m; |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 354 | int32 r; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 355 | |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 356 | m.tx.h.msgh_bits = MACH_MSGH_BITS_COMPLEX; |
| 357 | m.tx.h.msgh_size = sizeof(m.tx); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 358 | m.tx.h.msgh_remote_port = runtime·mach_task_self(); |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 359 | m.tx.h.msgh_id = Tmach_semdestroy; |
| 360 | m.tx.body.msgh_descriptor_count = 1; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 361 | m.tx.semaphore.name = sem; |
| 362 | m.tx.semaphore.disposition = MACH_MSG_TYPE_MOVE_SEND; |
| 363 | m.tx.semaphore.type = 0; |
| 364 | |
Russ Cox | cd80000 | 2009-06-08 14:03:21 -0700 | [diff] [blame] | 365 | while((r = machcall(&m.tx.h, sizeof m, 0)) != 0){ |
Russ Cox | 3ff5e72 | 2009-07-27 10:59:59 -0700 | [diff] [blame] | 366 | if(r == KERN_ABORTED) // interrupted |
| 367 | continue; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 368 | macherror(r, "semaphore_destroy"); |
Russ Cox | cd80000 | 2009-06-08 14:03:21 -0700 | [diff] [blame] | 369 | } |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Shenghou Ma | e021357 | 2012-01-22 10:34:17 -0800 | [diff] [blame] | 372 | // The other calls have simple system call traps in sys_darwin_{amd64,386}.s |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 373 | int32 runtime·mach_semaphore_wait(uint32 sema); |
| 374 | int32 runtime·mach_semaphore_timedwait(uint32 sema, uint32 sec, uint32 nsec); |
| 375 | int32 runtime·mach_semaphore_signal(uint32 sema); |
| 376 | int32 runtime·mach_semaphore_signal_all(uint32 sema); |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 377 | |
Russ Cox | 3b86026 | 2011-11-09 15:17:05 -0500 | [diff] [blame] | 378 | int32 |
| 379 | runtime·mach_semacquire(uint32 sem, int64 ns) |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 380 | { |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 381 | int32 r; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 382 | |
Russ Cox | 3b86026 | 2011-11-09 15:17:05 -0500 | [diff] [blame] | 383 | if(ns >= 0) { |
| 384 | r = runtime·mach_semaphore_timedwait(sem, ns/1000000000LL, ns%1000000000LL); |
| 385 | if(r == KERN_ABORTED || r == KERN_OPERATION_TIMED_OUT) |
| 386 | return -1; |
| 387 | if(r != 0) |
| 388 | macherror(r, "semaphore_wait"); |
| 389 | return 0; |
| 390 | } |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 391 | while((r = runtime·mach_semaphore_wait(sem)) != 0) { |
Russ Cox | cd80000 | 2009-06-08 14:03:21 -0700 | [diff] [blame] | 392 | if(r == KERN_ABORTED) // interrupted |
| 393 | continue; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 394 | macherror(r, "semaphore_wait"); |
Russ Cox | cd80000 | 2009-06-08 14:03:21 -0700 | [diff] [blame] | 395 | } |
Russ Cox | 3b86026 | 2011-11-09 15:17:05 -0500 | [diff] [blame] | 396 | return 0; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 400 | runtime·mach_semrelease(uint32 sem) |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 401 | { |
Russ Cox | 08cfcd1 | 2009-03-24 13:51:48 -0700 | [diff] [blame] | 402 | int32 r; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 403 | |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 404 | while((r = runtime·mach_semaphore_signal(sem)) != 0) { |
Russ Cox | cd80000 | 2009-06-08 14:03:21 -0700 | [diff] [blame] | 405 | if(r == KERN_ABORTED) // interrupted |
| 406 | continue; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 407 | macherror(r, "semaphore_signal"); |
Russ Cox | cd80000 | 2009-06-08 14:03:21 -0700 | [diff] [blame] | 408 | } |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Russ Cox | 5963dba | 2010-04-08 18:15:30 -0700 | [diff] [blame] | 411 | void |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 412 | runtime·sigpanic(void) |
Russ Cox | 5963dba | 2010-04-08 18:15:30 -0700 | [diff] [blame] | 413 | { |
| 414 | switch(g->sig) { |
| 415 | case SIGBUS: |
Russ Cox | 5032a7d | 2012-01-10 11:46:57 -0800 | [diff] [blame] | 416 | if(g->sigcode0 == BUS_ADRERR && g->sigcode1 < 0x1000) { |
| 417 | if(g->sigpc == 0) |
| 418 | runtime·panicstring("call of nil func value"); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 419 | runtime·panicstring("invalid memory address or nil pointer dereference"); |
Russ Cox | 5032a7d | 2012-01-10 11:46:57 -0800 | [diff] [blame] | 420 | } |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 421 | runtime·printf("unexpected fault address %p\n", g->sigcode1); |
| 422 | runtime·throw("fault"); |
Russ Cox | 5963dba | 2010-04-08 18:15:30 -0700 | [diff] [blame] | 423 | case SIGSEGV: |
Russ Cox | 5032a7d | 2012-01-10 11:46:57 -0800 | [diff] [blame] | 424 | if((g->sigcode0 == 0 || g->sigcode0 == SEGV_MAPERR || g->sigcode0 == SEGV_ACCERR) && g->sigcode1 < 0x1000) { |
| 425 | if(g->sigpc == 0) |
| 426 | runtime·panicstring("call of nil func value"); |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 427 | runtime·panicstring("invalid memory address or nil pointer dereference"); |
Russ Cox | 5032a7d | 2012-01-10 11:46:57 -0800 | [diff] [blame] | 428 | } |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 429 | runtime·printf("unexpected fault address %p\n", g->sigcode1); |
| 430 | runtime·throw("fault"); |
Russ Cox | 5963dba | 2010-04-08 18:15:30 -0700 | [diff] [blame] | 431 | case SIGFPE: |
| 432 | switch(g->sigcode0) { |
| 433 | case FPE_INTDIV: |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 434 | runtime·panicstring("integer divide by zero"); |
Russ Cox | 5963dba | 2010-04-08 18:15:30 -0700 | [diff] [blame] | 435 | case FPE_INTOVF: |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 436 | runtime·panicstring("integer overflow"); |
Russ Cox | 5963dba | 2010-04-08 18:15:30 -0700 | [diff] [blame] | 437 | } |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 438 | runtime·panicstring("floating point error"); |
Russ Cox | 5963dba | 2010-04-08 18:15:30 -0700 | [diff] [blame] | 439 | } |
Russ Cox | 68b4255 | 2010-11-04 14:00:19 -0400 | [diff] [blame] | 440 | runtime·panicstring(runtime·sigtab[g->sig].name); |
Russ Cox | 5963dba | 2010-04-08 18:15:30 -0700 | [diff] [blame] | 441 | } |
Rob Pike | 40c26ff | 2011-09-30 10:52:36 -0700 | [diff] [blame] | 442 | |
| 443 | // TODO(rsc): place holder to fix build. |
| 444 | void |
| 445 | runtime·osyield(void) |
| 446 | { |
| 447 | } |
Russ Cox | 102274a | 2012-02-24 15:28:51 -0500 | [diff] [blame] | 448 | |
| 449 | uintptr |
| 450 | runtime·memlimit(void) |
| 451 | { |
| 452 | // NOTE(rsc): Could use getrlimit here, |
| 453 | // like on FreeBSD or Linux, but Darwin doesn't enforce |
| 454 | // ulimit -v, so it's unclear why we'd try to stay within |
| 455 | // the limit. |
| 456 | return 0; |
| 457 | } |
Russ Cox | 6e2ae0a | 2012-02-28 16:18:24 -0500 | [diff] [blame] | 458 | |
| 459 | // NOTE(rsc): On OS X, when the CPU profiling timer expires, the SIGPROF |
| 460 | // signal is not guaranteed to be sent to the thread that was executing to |
| 461 | // cause it to expire. It can and often does go to a sleeping thread, which is |
| 462 | // not interesting for our profile. This is filed Apple Bug Report #9177434, |
| 463 | // copied to http://code.google.com/p/go/source/detail?r=35b716c94225. |
| 464 | // To work around this bug, we disable receipt of the profiling signal on |
| 465 | // a thread while in blocking system calls. This forces the kernel to deliver |
| 466 | // the profiling signal to an executing thread. |
| 467 | // |
| 468 | // The workaround fails on OS X machines using a 64-bit Snow Leopard kernel. |
| 469 | // In that configuration, the kernel appears to want to deliver SIGPROF to the |
| 470 | // sleeping threads regardless of signal mask and, worse, does not deliver |
| 471 | // the signal until the thread wakes up on its own. |
| 472 | // |
| 473 | // If necessary, we can switch to using ITIMER_REAL for OS X and handle |
| 474 | // the kernel-generated SIGALRM by generating our own SIGALRMs to deliver |
| 475 | // to all the running threads. SIGALRM does not appear to be affected by |
| 476 | // the 64-bit Snow Leopard bug. However, as of this writing Mountain Lion |
| 477 | // is in preview, making Snow Leopard two versions old, so it is unclear how |
| 478 | // much effort we need to spend on one buggy kernel. |
| 479 | |
| 480 | // Control whether profiling signal can be delivered to this thread. |
| 481 | void |
| 482 | runtime·setprof(bool on) |
| 483 | { |
| 484 | if(on) |
| 485 | runtime·sigprocmask(SIG_UNBLOCK, &sigset_prof, nil); |
| 486 | else |
| 487 | runtime·sigprocmask(SIG_BLOCK, &sigset_prof, nil); |
| 488 | } |
Russ Cox | 9b73238 | 2012-03-08 12:12:40 -0500 | [diff] [blame] | 489 | |
| 490 | static int8 badcallback[] = "runtime: cgo callback on thread not created by Go.\n"; |
| 491 | |
| 492 | // This runs on a foreign stack, without an m or a g. No stack split. |
| 493 | #pragma textflag 7 |
| 494 | void |
| 495 | runtime·badcallback(void) |
| 496 | { |
| 497 | runtime·write(2, badcallback, sizeof badcallback - 1); |
| 498 | } |
Russ Cox | b236911 | 2012-03-12 15:55:18 -0400 | [diff] [blame] | 499 | |
| 500 | static int8 badsignal[] = "runtime: signal received on thread not created by Go.\n"; |
| 501 | |
| 502 | // This runs on a foreign stack, without an m or a g. No stack split. |
| 503 | #pragma textflag 7 |
| 504 | void |
| 505 | runtime·badsignal(void) |
| 506 | { |
| 507 | runtime·write(2, badsignal, sizeof badsignal - 1); |
| 508 | } |