github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/src/runtime/os_freebsd.c (about) 1 // Copyright 2011 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" 6 #include "defs_GOOS_GOARCH.h" 7 #include "os_GOOS.h" 8 #include "signal_unix.h" 9 #include "stack.h" 10 #include "textflag.h" 11 12 extern SigTab runtime·sigtab[]; 13 extern int32 runtime·sys_umtx_op(uint32*, int32, uint32, void*, void*); 14 15 // From FreeBSD's <sys/sysctl.h> 16 #define CTL_HW 6 17 #define HW_NCPU 3 18 19 static Sigset sigset_none; 20 static Sigset sigset_all = { ~(uint32)0, ~(uint32)0, ~(uint32)0, ~(uint32)0, }; 21 22 static int32 23 getncpu(void) 24 { 25 uint32 mib[2]; 26 uint32 out; 27 int32 ret; 28 uintptr nout; 29 30 // Fetch hw.ncpu via sysctl. 31 mib[0] = CTL_HW; 32 mib[1] = HW_NCPU; 33 nout = sizeof out; 34 out = 0; 35 ret = runtime·sysctl(mib, 2, (byte*)&out, &nout, nil, 0); 36 if(ret >= 0) 37 return out; 38 else 39 return 1; 40 } 41 42 // FreeBSD's umtx_op syscall is effectively the same as Linux's futex, and 43 // thus the code is largely similar. See linux/thread.c and lock_futex.c for comments. 44 45 static void futexsleep(void); 46 47 #pragma textflag NOSPLIT 48 void 49 runtime·futexsleep(uint32 *addr, uint32 val, int64 ns) 50 { 51 void (*fn)(void); 52 53 g->m->ptrarg[0] = addr; 54 g->m->scalararg[0] = val; 55 g->m->ptrarg[1] = &ns; 56 57 fn = futexsleep; 58 runtime·onM(&fn); 59 } 60 61 static void 62 futexsleep(void) 63 { 64 uint32 *addr; 65 uint32 val; 66 int64 ns; 67 int32 ret; 68 Timespec ts; 69 70 addr = g->m->ptrarg[0]; 71 val = g->m->scalararg[0]; 72 ns = *(int64*)g->m->ptrarg[1]; 73 g->m->ptrarg[0] = nil; 74 g->m->scalararg[0] = 0; 75 g->m->ptrarg[1] = nil; 76 77 if(ns < 0) { 78 ret = runtime·sys_umtx_op(addr, UMTX_OP_WAIT_UINT_PRIVATE, val, nil, nil); 79 if(ret >= 0 || ret == -EINTR) 80 return; 81 goto fail; 82 } 83 // NOTE: tv_nsec is int64 on amd64, so this assumes a little-endian system. 84 ts.tv_nsec = 0; 85 ts.tv_sec = runtime·timediv(ns, 1000000000, (int32*)&ts.tv_nsec); 86 ret = runtime·sys_umtx_op(addr, UMTX_OP_WAIT_UINT_PRIVATE, val, nil, &ts); 87 if(ret >= 0 || ret == -EINTR) 88 return; 89 90 fail: 91 runtime·prints("umtx_wait addr="); 92 runtime·printpointer(addr); 93 runtime·prints(" val="); 94 runtime·printint(val); 95 runtime·prints(" ret="); 96 runtime·printint(ret); 97 runtime·prints("\n"); 98 *(int32*)0x1005 = 0x1005; 99 } 100 101 static void badfutexwakeup(void); 102 103 #pragma textflag NOSPLIT 104 void 105 runtime·futexwakeup(uint32 *addr, uint32 cnt) 106 { 107 int32 ret; 108 void (*fn)(void); 109 110 ret = runtime·sys_umtx_op(addr, UMTX_OP_WAKE_PRIVATE, cnt, nil, nil); 111 if(ret >= 0) 112 return; 113 114 g->m->ptrarg[0] = addr; 115 g->m->scalararg[0] = ret; 116 fn = badfutexwakeup; 117 if(g == g->m->gsignal) 118 fn(); 119 else 120 runtime·onM(&fn); 121 *(int32*)0x1006 = 0x1006; 122 } 123 124 static void 125 badfutexwakeup(void) 126 { 127 void *addr; 128 int32 ret; 129 130 addr = g->m->ptrarg[0]; 131 ret = g->m->scalararg[0]; 132 runtime·printf("umtx_wake addr=%p ret=%d\n", addr, ret); 133 } 134 135 void runtime·thr_start(void*); 136 137 void 138 runtime·newosproc(M *mp, void *stk) 139 { 140 ThrParam param; 141 Sigset oset; 142 143 if(0){ 144 runtime·printf("newosproc stk=%p m=%p g=%p id=%d/%d ostk=%p\n", 145 stk, mp, mp->g0, mp->id, (int32)mp->tls[0], &mp); 146 } 147 148 runtime·sigprocmask(&sigset_all, &oset); 149 runtime·memclr((byte*)¶m, sizeof param); 150 151 param.start_func = runtime·thr_start; 152 param.arg = (byte*)mp; 153 154 // NOTE(rsc): This code is confused. stackbase is the top of the stack 155 // and is equal to stk. However, it's working, so I'm not changing it. 156 param.stack_base = (void*)mp->g0->stack.hi; 157 param.stack_size = (byte*)stk - (byte*)mp->g0->stack.hi; 158 159 param.child_tid = (void*)&mp->procid; 160 param.parent_tid = nil; 161 param.tls_base = (void*)&mp->tls[0]; 162 param.tls_size = sizeof mp->tls; 163 164 mp->tls[0] = mp->id; // so 386 asm can find it 165 166 runtime·thr_new(¶m, sizeof param); 167 runtime·sigprocmask(&oset, nil); 168 } 169 170 void 171 runtime·osinit(void) 172 { 173 runtime·ncpu = getncpu(); 174 } 175 176 #pragma textflag NOSPLIT 177 void 178 runtime·get_random_data(byte **rnd, int32 *rnd_len) 179 { 180 #pragma dataflag NOPTR 181 static byte urandom_data[HashRandomBytes]; 182 int32 fd; 183 fd = runtime·open("/dev/urandom", 0 /* O_RDONLY */, 0); 184 if(runtime·read(fd, urandom_data, HashRandomBytes) == HashRandomBytes) { 185 *rnd = urandom_data; 186 *rnd_len = HashRandomBytes; 187 } else { 188 *rnd = nil; 189 *rnd_len = 0; 190 } 191 runtime·close(fd); 192 } 193 194 void 195 runtime·goenvs(void) 196 { 197 runtime·goenvs_unix(); 198 } 199 200 // Called to initialize a new m (including the bootstrap m). 201 // Called on the parent thread (main thread in case of bootstrap), can allocate memory. 202 void 203 runtime·mpreinit(M *mp) 204 { 205 mp->gsignal = runtime·malg(32*1024); 206 mp->gsignal->m = mp; 207 } 208 209 // Called to initialize a new m (including the bootstrap m). 210 // Called on the new thread, can not allocate memory. 211 void 212 runtime·minit(void) 213 { 214 // Initialize signal handling 215 runtime·signalstack((byte*)g->m->gsignal->stack.lo, 32*1024); 216 runtime·sigprocmask(&sigset_none, nil); 217 } 218 219 // Called from dropm to undo the effect of an minit. 220 void 221 runtime·unminit(void) 222 { 223 runtime·signalstack(nil, 0); 224 } 225 226 uintptr 227 runtime·memlimit(void) 228 { 229 Rlimit rl; 230 extern byte runtime·text[], runtime·end[]; 231 uintptr used; 232 233 if(runtime·getrlimit(RLIMIT_AS, &rl) != 0) 234 return 0; 235 if(rl.rlim_cur >= 0x7fffffff) 236 return 0; 237 238 // Estimate our VM footprint excluding the heap. 239 // Not an exact science: use size of binary plus 240 // some room for thread stacks. 241 used = runtime·end - runtime·text + (64<<20); 242 if(used >= rl.rlim_cur) 243 return 0; 244 245 // If there's not at least 16 MB left, we're probably 246 // not going to be able to do much. Treat as no limit. 247 rl.rlim_cur -= used; 248 if(rl.rlim_cur < (16<<20)) 249 return 0; 250 251 return rl.rlim_cur - used; 252 } 253 254 extern void runtime·sigtramp(void); 255 256 typedef struct sigaction { 257 union { 258 void (*__sa_handler)(int32); 259 void (*__sa_sigaction)(int32, Siginfo*, void *); 260 } __sigaction_u; /* signal handler */ 261 int32 sa_flags; /* see signal options below */ 262 Sigset sa_mask; /* signal mask to apply */ 263 } SigactionT; 264 265 void 266 runtime·setsig(int32 i, GoSighandler *fn, bool restart) 267 { 268 SigactionT sa; 269 270 runtime·memclr((byte*)&sa, sizeof sa); 271 sa.sa_flags = SA_SIGINFO|SA_ONSTACK; 272 if(restart) 273 sa.sa_flags |= SA_RESTART; 274 sa.sa_mask.__bits[0] = ~(uint32)0; 275 sa.sa_mask.__bits[1] = ~(uint32)0; 276 sa.sa_mask.__bits[2] = ~(uint32)0; 277 sa.sa_mask.__bits[3] = ~(uint32)0; 278 if(fn == runtime·sighandler) 279 fn = (void*)runtime·sigtramp; 280 sa.__sigaction_u.__sa_sigaction = (void*)fn; 281 runtime·sigaction(i, &sa, nil); 282 } 283 284 GoSighandler* 285 runtime·getsig(int32 i) 286 { 287 SigactionT sa; 288 289 runtime·memclr((byte*)&sa, sizeof sa); 290 runtime·sigaction(i, nil, &sa); 291 if((void*)sa.__sigaction_u.__sa_sigaction == runtime·sigtramp) 292 return runtime·sighandler; 293 return (void*)sa.__sigaction_u.__sa_sigaction; 294 } 295 296 void 297 runtime·signalstack(byte *p, int32 n) 298 { 299 StackT st; 300 301 st.ss_sp = (void*)p; 302 st.ss_size = n; 303 st.ss_flags = 0; 304 if(p == nil) 305 st.ss_flags = SS_DISABLE; 306 runtime·sigaltstack(&st, nil); 307 } 308 309 void 310 runtime·unblocksignals(void) 311 { 312 runtime·sigprocmask(&sigset_none, nil); 313 } 314 315 #pragma textflag NOSPLIT 316 int8* 317 runtime·signame(int32 sig) 318 { 319 return runtime·sigtab[sig].name; 320 }