github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/src/runtime/os1_linux.go (about) 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 package runtime 6 7 import ( 8 "runtime/internal/sys" 9 "unsafe" 10 ) 11 12 // Linux futex. 13 // 14 // futexsleep(uint32 *addr, uint32 val) 15 // futexwakeup(uint32 *addr) 16 // 17 // Futexsleep atomically checks if *addr == val and if so, sleeps on addr. 18 // Futexwakeup wakes up threads sleeping on addr. 19 // Futexsleep is allowed to wake up spuriously. 20 21 const ( 22 _FUTEX_WAIT = 0 23 _FUTEX_WAKE = 1 24 ) 25 26 // Atomically, 27 // if(*addr == val) sleep 28 // Might be woken up spuriously; that's allowed. 29 // Don't sleep longer than ns; ns < 0 means forever. 30 //go:nosplit 31 func futexsleep(addr *uint32, val uint32, ns int64) { 32 var ts timespec 33 34 // Some Linux kernels have a bug where futex of 35 // FUTEX_WAIT returns an internal error code 36 // as an errno. Libpthread ignores the return value 37 // here, and so can we: as it says a few lines up, 38 // spurious wakeups are allowed. 39 if ns < 0 { 40 futex(unsafe.Pointer(addr), _FUTEX_WAIT, val, nil, nil, 0) 41 return 42 } 43 44 // It's difficult to live within the no-split stack limits here. 45 // On ARM and 386, a 64-bit divide invokes a general software routine 46 // that needs more stack than we can afford. So we use timediv instead. 47 // But on real 64-bit systems, where words are larger but the stack limit 48 // is not, even timediv is too heavy, and we really need to use just an 49 // ordinary machine instruction. 50 if sys.PtrSize == 8 { 51 ts.set_sec(ns / 1000000000) 52 ts.set_nsec(int32(ns % 1000000000)) 53 } else { 54 ts.tv_nsec = 0 55 ts.set_sec(int64(timediv(ns, 1000000000, (*int32)(unsafe.Pointer(&ts.tv_nsec))))) 56 } 57 futex(unsafe.Pointer(addr), _FUTEX_WAIT, val, unsafe.Pointer(&ts), nil, 0) 58 } 59 60 // If any procs are sleeping on addr, wake up at most cnt. 61 //go:nosplit 62 func futexwakeup(addr *uint32, cnt uint32) { 63 ret := futex(unsafe.Pointer(addr), _FUTEX_WAKE, cnt, nil, nil, 0) 64 if ret >= 0 { 65 return 66 } 67 68 // I don't know that futex wakeup can return 69 // EAGAIN or EINTR, but if it does, it would be 70 // safe to loop and call futex again. 71 systemstack(func() { 72 print("futexwakeup addr=", addr, " returned ", ret, "\n") 73 }) 74 75 *(*int32)(unsafe.Pointer(uintptr(0x1006))) = 0x1006 76 } 77 78 func getproccount() int32 { 79 // This buffer is huge (8 kB) but we are on the system stack 80 // and there should be plenty of space (64 kB). 81 // Also this is a leaf, so we're not holding up the memory for long. 82 // See golang.org/issue/11823. 83 // The suggested behavior here is to keep trying with ever-larger 84 // buffers, but we don't have a dynamic memory allocator at the 85 // moment, so that's a bit tricky and seems like overkill. 86 const maxCPUs = 64 * 1024 87 var buf [maxCPUs / (sys.PtrSize * 8)]uintptr 88 r := sched_getaffinity(0, unsafe.Sizeof(buf), &buf[0]) 89 n := int32(0) 90 for _, v := range buf[:r/sys.PtrSize] { 91 for v != 0 { 92 n += int32(v & 1) 93 v >>= 1 94 } 95 } 96 if n == 0 { 97 n = 1 98 } 99 return n 100 } 101 102 // Clone, the Linux rfork. 103 const ( 104 _CLONE_VM = 0x100 105 _CLONE_FS = 0x200 106 _CLONE_FILES = 0x400 107 _CLONE_SIGHAND = 0x800 108 _CLONE_PTRACE = 0x2000 109 _CLONE_VFORK = 0x4000 110 _CLONE_PARENT = 0x8000 111 _CLONE_THREAD = 0x10000 112 _CLONE_NEWNS = 0x20000 113 _CLONE_SYSVSEM = 0x40000 114 _CLONE_SETTLS = 0x80000 115 _CLONE_PARENT_SETTID = 0x100000 116 _CLONE_CHILD_CLEARTID = 0x200000 117 _CLONE_UNTRACED = 0x800000 118 _CLONE_CHILD_SETTID = 0x1000000 119 _CLONE_STOPPED = 0x2000000 120 _CLONE_NEWUTS = 0x4000000 121 _CLONE_NEWIPC = 0x8000000 122 123 cloneFlags = _CLONE_VM | /* share memory */ 124 _CLONE_FS | /* share cwd, etc */ 125 _CLONE_FILES | /* share fd table */ 126 _CLONE_SIGHAND | /* share sig handler table */ 127 _CLONE_THREAD /* revisit - okay for now */ 128 ) 129 130 // May run with m.p==nil, so write barriers are not allowed. 131 //go:nowritebarrier 132 func newosproc(mp *m, stk unsafe.Pointer) { 133 /* 134 * note: strace gets confused if we use CLONE_PTRACE here. 135 */ 136 if false { 137 print("newosproc stk=", stk, " m=", mp, " g=", mp.g0, " clone=", funcPC(clone), " id=", mp.id, " ostk=", &mp, "\n") 138 } 139 140 // Disable signals during clone, so that the new thread starts 141 // with signals disabled. It will enable them in minit. 142 var oset sigset 143 rtsigprocmask(_SIG_SETMASK, &sigset_all, &oset, int32(unsafe.Sizeof(oset))) 144 ret := clone(cloneFlags, stk, unsafe.Pointer(mp), unsafe.Pointer(mp.g0), unsafe.Pointer(funcPC(mstart))) 145 rtsigprocmask(_SIG_SETMASK, &oset, nil, int32(unsafe.Sizeof(oset))) 146 147 if ret < 0 { 148 print("runtime: failed to create new OS thread (have ", mcount(), " already; errno=", -ret, ")\n") 149 throw("newosproc") 150 } 151 } 152 153 // Version of newosproc that doesn't require a valid G. 154 //go:nosplit 155 func newosproc0(stacksize uintptr, fn unsafe.Pointer) { 156 stack := sysAlloc(stacksize, &memstats.stacks_sys) 157 if stack == nil { 158 write(2, unsafe.Pointer(&failallocatestack[0]), int32(len(failallocatestack))) 159 exit(1) 160 } 161 ret := clone(cloneFlags, unsafe.Pointer(uintptr(stack)+stacksize), nil, nil, fn) 162 if ret < 0 { 163 write(2, unsafe.Pointer(&failthreadcreate[0]), int32(len(failthreadcreate))) 164 exit(1) 165 } 166 } 167 168 var failallocatestack = []byte("runtime: failed to allocate stack for the new OS thread\n") 169 var failthreadcreate = []byte("runtime: failed to create new OS thread\n") 170 171 func osinit() { 172 ncpu = getproccount() 173 } 174 175 var urandom_dev = []byte("/dev/urandom\x00") 176 177 func getRandomData(r []byte) { 178 if startupRandomData != nil { 179 n := copy(r, startupRandomData) 180 extendRandom(r, n) 181 return 182 } 183 fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0) 184 n := read(fd, unsafe.Pointer(&r[0]), int32(len(r))) 185 closefd(fd) 186 extendRandom(r, int(n)) 187 } 188 189 func goenvs() { 190 goenvs_unix() 191 } 192 193 // Called to do synchronous initialization of Go code built with 194 // -buildmode=c-archive or -buildmode=c-shared. 195 // None of the Go runtime is initialized. 196 //go:nosplit 197 //go:nowritebarrierrec 198 func libpreinit() { 199 initsig(true) 200 } 201 202 // Called to initialize a new m (including the bootstrap m). 203 // Called on the parent thread (main thread in case of bootstrap), can allocate memory. 204 func mpreinit(mp *m) { 205 mp.gsignal = malg(32 * 1024) // Linux wants >= 2K 206 mp.gsignal.m = mp 207 } 208 209 //go:nosplit 210 func msigsave(mp *m) { 211 smask := &mp.sigmask 212 rtsigprocmask(_SIG_SETMASK, nil, smask, int32(unsafe.Sizeof(*smask))) 213 } 214 215 //go:nosplit 216 func msigrestore(sigmask sigset) { 217 rtsigprocmask(_SIG_SETMASK, &sigmask, nil, int32(unsafe.Sizeof(sigmask))) 218 } 219 220 //go:nosplit 221 func sigblock() { 222 rtsigprocmask(_SIG_SETMASK, &sigset_all, nil, int32(unsafe.Sizeof(sigset_all))) 223 } 224 225 func gettid() uint32 226 227 // Called to initialize a new m (including the bootstrap m). 228 // Called on the new thread, cannot allocate memory. 229 func minit() { 230 // Initialize signal handling. 231 _g_ := getg() 232 233 var st sigaltstackt 234 sigaltstack(nil, &st) 235 if st.ss_flags&_SS_DISABLE != 0 { 236 signalstack(&_g_.m.gsignal.stack) 237 _g_.m.newSigstack = true 238 } else { 239 // Use existing signal stack. 240 stsp := uintptr(unsafe.Pointer(st.ss_sp)) 241 _g_.m.gsignal.stack.lo = stsp 242 _g_.m.gsignal.stack.hi = stsp + st.ss_size 243 _g_.m.gsignal.stackguard0 = stsp + _StackGuard 244 _g_.m.gsignal.stackguard1 = stsp + _StackGuard 245 _g_.m.gsignal.stackAlloc = st.ss_size 246 _g_.m.newSigstack = false 247 } 248 249 // for debuggers, in case cgo created the thread 250 _g_.m.procid = uint64(gettid()) 251 252 // restore signal mask from m.sigmask and unblock essential signals 253 nmask := _g_.m.sigmask 254 for i := range sigtable { 255 if sigtable[i].flags&_SigUnblock != 0 { 256 sigdelset(&nmask, i) 257 } 258 } 259 rtsigprocmask(_SIG_SETMASK, &nmask, nil, int32(unsafe.Sizeof(nmask))) 260 } 261 262 // Called from dropm to undo the effect of an minit. 263 //go:nosplit 264 func unminit() { 265 if getg().m.newSigstack { 266 signalstack(nil) 267 } 268 } 269 270 func memlimit() uintptr { 271 /* 272 TODO: Convert to Go when something actually uses the result. 273 274 Rlimit rl; 275 extern byte runtime·text[], runtime·end[]; 276 uintptr used; 277 278 if(runtime·getrlimit(RLIMIT_AS, &rl) != 0) 279 return 0; 280 if(rl.rlim_cur >= 0x7fffffff) 281 return 0; 282 283 // Estimate our VM footprint excluding the heap. 284 // Not an exact science: use size of binary plus 285 // some room for thread stacks. 286 used = runtime·end - runtime·text + (64<<20); 287 if(used >= rl.rlim_cur) 288 return 0; 289 290 // If there's not at least 16 MB left, we're probably 291 // not going to be able to do much. Treat as no limit. 292 rl.rlim_cur -= used; 293 if(rl.rlim_cur < (16<<20)) 294 return 0; 295 296 return rl.rlim_cur - used; 297 */ 298 299 return 0 300 } 301 302 //#ifdef GOARCH_386 303 //#define sa_handler k_sa_handler 304 //#endif 305 306 func sigreturn() 307 func sigtramp() 308 func cgoSigtramp() 309 310 //go:nosplit 311 //go:nowritebarrierrec 312 func setsig(i int32, fn uintptr, restart bool) { 313 var sa sigactiont 314 memclr(unsafe.Pointer(&sa), unsafe.Sizeof(sa)) 315 sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK | _SA_RESTORER 316 if restart { 317 sa.sa_flags |= _SA_RESTART 318 } 319 sigfillset(&sa.sa_mask) 320 // Although Linux manpage says "sa_restorer element is obsolete and 321 // should not be used". x86_64 kernel requires it. Only use it on 322 // x86. 323 if GOARCH == "386" || GOARCH == "amd64" { 324 sa.sa_restorer = funcPC(sigreturn) 325 } 326 if fn == funcPC(sighandler) { 327 if iscgo { 328 fn = funcPC(cgoSigtramp) 329 } else { 330 fn = funcPC(sigtramp) 331 } 332 } 333 sa.sa_handler = fn 334 rt_sigaction(uintptr(i), &sa, nil, unsafe.Sizeof(sa.sa_mask)) 335 } 336 337 //go:nosplit 338 //go:nowritebarrierrec 339 func setsigstack(i int32) { 340 var sa sigactiont 341 if rt_sigaction(uintptr(i), nil, &sa, unsafe.Sizeof(sa.sa_mask)) != 0 { 342 throw("rt_sigaction failure") 343 } 344 if sa.sa_handler == 0 || sa.sa_handler == _SIG_DFL || sa.sa_handler == _SIG_IGN || sa.sa_flags&_SA_ONSTACK != 0 { 345 return 346 } 347 sa.sa_flags |= _SA_ONSTACK 348 if rt_sigaction(uintptr(i), &sa, nil, unsafe.Sizeof(sa.sa_mask)) != 0 { 349 throw("rt_sigaction failure") 350 } 351 } 352 353 //go:nosplit 354 //go:nowritebarrierrec 355 func getsig(i int32) uintptr { 356 var sa sigactiont 357 358 memclr(unsafe.Pointer(&sa), unsafe.Sizeof(sa)) 359 if rt_sigaction(uintptr(i), nil, &sa, unsafe.Sizeof(sa.sa_mask)) != 0 { 360 throw("rt_sigaction read failure") 361 } 362 if sa.sa_handler == funcPC(sigtramp) || sa.sa_handler == funcPC(cgoSigtramp) { 363 return funcPC(sighandler) 364 } 365 return sa.sa_handler 366 } 367 368 //go:nosplit 369 func signalstack(s *stack) { 370 var st sigaltstackt 371 if s == nil { 372 st.ss_flags = _SS_DISABLE 373 } else { 374 st.ss_sp = (*byte)(unsafe.Pointer(s.lo)) 375 st.ss_size = s.hi - s.lo 376 st.ss_flags = 0 377 } 378 sigaltstack(&st, nil) 379 } 380 381 //go:nosplit 382 //go:nowritebarrierrec 383 func updatesigmask(m sigmask) { 384 var mask sigset 385 sigcopyset(&mask, m) 386 rtsigprocmask(_SIG_SETMASK, &mask, nil, int32(unsafe.Sizeof(mask))) 387 } 388 389 func unblocksig(sig int32) { 390 var mask sigset 391 sigaddset(&mask, int(sig)) 392 rtsigprocmask(_SIG_UNBLOCK, &mask, nil, int32(unsafe.Sizeof(mask))) 393 }