github.com/rohankumardubey/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/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 "../../cmd/ld/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 #pragma textflag NOSPLIT 46 void 47 runtime·futexsleep(uint32 *addr, uint32 val, int64 ns) 48 { 49 int32 ret; 50 Timespec ts; 51 52 if(ns < 0) { 53 ret = runtime·sys_umtx_op(addr, UMTX_OP_WAIT_UINT, val, nil, nil); 54 if(ret >= 0 || ret == -EINTR) 55 return; 56 goto fail; 57 } 58 // NOTE: tv_nsec is int64 on amd64, so this assumes a little-endian system. 59 ts.tv_nsec = 0; 60 ts.tv_sec = runtime·timediv(ns, 1000000000, (int32*)&ts.tv_nsec); 61 ret = runtime·sys_umtx_op(addr, UMTX_OP_WAIT_UINT, val, nil, &ts); 62 if(ret >= 0 || ret == -EINTR) 63 return; 64 65 fail: 66 runtime·prints("umtx_wait addr="); 67 runtime·printpointer(addr); 68 runtime·prints(" val="); 69 runtime·printint(val); 70 runtime·prints(" ret="); 71 runtime·printint(ret); 72 runtime·prints("\n"); 73 *(int32*)0x1005 = 0x1005; 74 } 75 76 void 77 runtime·futexwakeup(uint32 *addr, uint32 cnt) 78 { 79 int32 ret; 80 81 ret = runtime·sys_umtx_op(addr, UMTX_OP_WAKE, cnt, nil, nil); 82 if(ret >= 0) 83 return; 84 85 runtime·printf("umtx_wake addr=%p ret=%d\n", addr, ret); 86 *(int32*)0x1006 = 0x1006; 87 } 88 89 void runtime·thr_start(void*); 90 91 void 92 runtime·newosproc(M *mp, void *stk) 93 { 94 ThrParam param; 95 Sigset oset; 96 97 if(0){ 98 runtime·printf("newosproc stk=%p m=%p g=%p id=%d/%d ostk=%p\n", 99 stk, mp, mp->g0, mp->id, (int32)mp->tls[0], &mp); 100 } 101 102 runtime·sigprocmask(&sigset_all, &oset); 103 runtime·memclr((byte*)¶m, sizeof param); 104 105 param.start_func = runtime·thr_start; 106 param.arg = (byte*)mp; 107 108 // NOTE(rsc): This code is confused. stackbase is the top of the stack 109 // and is equal to stk. However, it's working, so I'm not changing it. 110 param.stack_base = (void*)mp->g0->stackbase; 111 param.stack_size = (byte*)stk - (byte*)mp->g0->stackbase; 112 113 param.child_tid = (intptr*)&mp->procid; 114 param.parent_tid = nil; 115 param.tls_base = (void*)&mp->tls[0]; 116 param.tls_size = sizeof mp->tls; 117 118 mp->tls[0] = mp->id; // so 386 asm can find it 119 120 runtime·thr_new(¶m, sizeof param); 121 runtime·sigprocmask(&oset, nil); 122 } 123 124 void 125 runtime·osinit(void) 126 { 127 runtime·ncpu = getncpu(); 128 } 129 130 void 131 runtime·get_random_data(byte **rnd, int32 *rnd_len) 132 { 133 static byte urandom_data[HashRandomBytes]; 134 int32 fd; 135 fd = runtime·open("/dev/urandom", 0 /* O_RDONLY */, 0); 136 if(runtime·read(fd, urandom_data, HashRandomBytes) == HashRandomBytes) { 137 *rnd = urandom_data; 138 *rnd_len = HashRandomBytes; 139 } else { 140 *rnd = nil; 141 *rnd_len = 0; 142 } 143 runtime·close(fd); 144 } 145 146 void 147 runtime·goenvs(void) 148 { 149 runtime·goenvs_unix(); 150 } 151 152 // Called to initialize a new m (including the bootstrap m). 153 // Called on the parent thread (main thread in case of bootstrap), can allocate memory. 154 void 155 runtime·mpreinit(M *mp) 156 { 157 mp->gsignal = runtime·malg(32*1024); 158 } 159 160 // Called to initialize a new m (including the bootstrap m). 161 // Called on the new thread, can not allocate memory. 162 void 163 runtime·minit(void) 164 { 165 // Initialize signal handling 166 runtime·signalstack((byte*)m->gsignal->stackguard - StackGuard, 32*1024); 167 runtime·sigprocmask(&sigset_none, nil); 168 } 169 170 // Called from dropm to undo the effect of an minit. 171 void 172 runtime·unminit(void) 173 { 174 runtime·signalstack(nil, 0); 175 } 176 177 void 178 runtime·sigpanic(void) 179 { 180 switch(g->sig) { 181 case SIGBUS: 182 if(g->sigcode0 == BUS_ADRERR && g->sigcode1 < 0x1000) { 183 if(g->sigpc == 0) 184 runtime·panicstring("call of nil func value"); 185 runtime·panicstring("invalid memory address or nil pointer dereference"); 186 } 187 runtime·printf("unexpected fault address %p\n", g->sigcode1); 188 runtime·throw("fault"); 189 case SIGSEGV: 190 if((g->sigcode0 == 0 || g->sigcode0 == SEGV_MAPERR || g->sigcode0 == SEGV_ACCERR) && g->sigcode1 < 0x1000) { 191 if(g->sigpc == 0) 192 runtime·panicstring("call of nil func value"); 193 runtime·panicstring("invalid memory address or nil pointer dereference"); 194 } 195 runtime·printf("unexpected fault address %p\n", g->sigcode1); 196 runtime·throw("fault"); 197 case SIGFPE: 198 switch(g->sigcode0) { 199 case FPE_INTDIV: 200 runtime·panicstring("integer divide by zero"); 201 case FPE_INTOVF: 202 runtime·panicstring("integer overflow"); 203 } 204 runtime·panicstring("floating point error"); 205 } 206 runtime·panicstring(runtime·sigtab[g->sig].name); 207 } 208 209 uintptr 210 runtime·memlimit(void) 211 { 212 Rlimit rl; 213 extern byte text[], end[]; 214 uintptr used; 215 216 if(runtime·getrlimit(RLIMIT_AS, &rl) != 0) 217 return 0; 218 if(rl.rlim_cur >= 0x7fffffff) 219 return 0; 220 221 // Estimate our VM footprint excluding the heap. 222 // Not an exact science: use size of binary plus 223 // some room for thread stacks. 224 used = end - text + (64<<20); 225 if(used >= rl.rlim_cur) 226 return 0; 227 228 // If there's not at least 16 MB left, we're probably 229 // not going to be able to do much. Treat as no limit. 230 rl.rlim_cur -= used; 231 if(rl.rlim_cur < (16<<20)) 232 return 0; 233 234 return rl.rlim_cur - used; 235 } 236 237 extern void runtime·sigtramp(void); 238 239 typedef struct sigaction { 240 union { 241 void (*__sa_handler)(int32); 242 void (*__sa_sigaction)(int32, Siginfo*, void *); 243 } __sigaction_u; /* signal handler */ 244 int32 sa_flags; /* see signal options below */ 245 Sigset sa_mask; /* signal mask to apply */ 246 } Sigaction; 247 248 void 249 runtime·setsig(int32 i, GoSighandler *fn, bool restart) 250 { 251 Sigaction sa; 252 253 runtime·memclr((byte*)&sa, sizeof sa); 254 sa.sa_flags = SA_SIGINFO|SA_ONSTACK; 255 if(restart) 256 sa.sa_flags |= SA_RESTART; 257 sa.sa_mask.__bits[0] = ~(uint32)0; 258 sa.sa_mask.__bits[1] = ~(uint32)0; 259 sa.sa_mask.__bits[2] = ~(uint32)0; 260 sa.sa_mask.__bits[3] = ~(uint32)0; 261 if(fn == runtime·sighandler) 262 fn = (void*)runtime·sigtramp; 263 sa.__sigaction_u.__sa_sigaction = (void*)fn; 264 runtime·sigaction(i, &sa, nil); 265 } 266 267 GoSighandler* 268 runtime·getsig(int32 i) 269 { 270 Sigaction sa; 271 272 runtime·memclr((byte*)&sa, sizeof sa); 273 runtime·sigaction(i, nil, &sa); 274 if((void*)sa.__sigaction_u.__sa_sigaction == runtime·sigtramp) 275 return runtime·sighandler; 276 return (void*)sa.__sigaction_u.__sa_sigaction; 277 } 278 279 void 280 runtime·signalstack(byte *p, int32 n) 281 { 282 StackT st; 283 284 st.ss_sp = (void*)p; 285 st.ss_size = n; 286 st.ss_flags = 0; 287 if(p == nil) 288 st.ss_flags = SS_DISABLE; 289 runtime·sigaltstack(&st, nil); 290 }