github.com/fjballest/golang@v0.0.0-20151209143359-e4c5fe594ca8/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 initialize a new m (including the bootstrap m). 194 // Called on the parent thread (main thread in case of bootstrap), can allocate memory. 195 func mpreinit(mp *m) { 196 mp.gsignal = malg(32 * 1024) // Linux wants >= 2K 197 mp.gsignal.m = mp 198 } 199 200 //go:nosplit 201 func msigsave(mp *m) { 202 smask := &mp.sigmask 203 rtsigprocmask(_SIG_SETMASK, nil, smask, int32(unsafe.Sizeof(*smask))) 204 } 205 206 //go:nosplit 207 func msigrestore(mp *m) { 208 smask := &mp.sigmask 209 rtsigprocmask(_SIG_SETMASK, smask, nil, int32(unsafe.Sizeof(*smask))) 210 } 211 212 //go:nosplit 213 func sigblock() { 214 rtsigprocmask(_SIG_SETMASK, &sigset_all, nil, int32(unsafe.Sizeof(sigset_all))) 215 } 216 217 func gettid() uint32 218 219 // Called to initialize a new m (including the bootstrap m). 220 // Called on the new thread, can not allocate memory. 221 func minit() { 222 // Initialize signal handling. 223 _g_ := getg() 224 signalstack(&_g_.m.gsignal.stack) 225 226 // for debuggers, in case cgo created the thread 227 _g_.m.procid = uint64(gettid()) 228 229 // restore signal mask from m.sigmask and unblock essential signals 230 nmask := _g_.m.sigmask 231 for i := range sigtable { 232 if sigtable[i].flags&_SigUnblock != 0 { 233 sigdelset(&nmask, i) 234 } 235 } 236 rtsigprocmask(_SIG_SETMASK, &nmask, nil, int32(unsafe.Sizeof(nmask))) 237 } 238 239 // Called from dropm to undo the effect of an minit. 240 //go:nosplit 241 func unminit() { 242 signalstack(nil) 243 } 244 245 func memlimit() uintptr { 246 /* 247 TODO: Convert to Go when something actually uses the result. 248 249 Rlimit rl; 250 extern byte runtime·text[], runtime·end[]; 251 uintptr used; 252 253 if(runtime·getrlimit(RLIMIT_AS, &rl) != 0) 254 return 0; 255 if(rl.rlim_cur >= 0x7fffffff) 256 return 0; 257 258 // Estimate our VM footprint excluding the heap. 259 // Not an exact science: use size of binary plus 260 // some room for thread stacks. 261 used = runtime·end - runtime·text + (64<<20); 262 if(used >= rl.rlim_cur) 263 return 0; 264 265 // If there's not at least 16 MB left, we're probably 266 // not going to be able to do much. Treat as no limit. 267 rl.rlim_cur -= used; 268 if(rl.rlim_cur < (16<<20)) 269 return 0; 270 271 return rl.rlim_cur - used; 272 */ 273 274 return 0 275 } 276 277 //#ifdef GOARCH_386 278 //#define sa_handler k_sa_handler 279 //#endif 280 281 func sigreturn() 282 func sigtramp() 283 284 func setsig(i int32, fn uintptr, restart bool) { 285 var sa sigactiont 286 memclr(unsafe.Pointer(&sa), unsafe.Sizeof(sa)) 287 sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK | _SA_RESTORER 288 if restart { 289 sa.sa_flags |= _SA_RESTART 290 } 291 sigfillset(&sa.sa_mask) 292 // Although Linux manpage says "sa_restorer element is obsolete and 293 // should not be used". x86_64 kernel requires it. Only use it on 294 // x86. 295 if GOARCH == "386" || GOARCH == "amd64" { 296 sa.sa_restorer = funcPC(sigreturn) 297 } 298 if fn == funcPC(sighandler) { 299 fn = funcPC(sigtramp) 300 } 301 sa.sa_handler = fn 302 // Qemu rejects rt_sigaction of SIGRTMAX (64). 303 if rt_sigaction(uintptr(i), &sa, nil, unsafe.Sizeof(sa.sa_mask)) != 0 && i != 64 { 304 throw("rt_sigaction failure") 305 } 306 } 307 308 func setsigstack(i int32) { 309 var sa sigactiont 310 if rt_sigaction(uintptr(i), nil, &sa, unsafe.Sizeof(sa.sa_mask)) != 0 { 311 throw("rt_sigaction failure") 312 } 313 if sa.sa_handler == 0 || sa.sa_handler == _SIG_DFL || sa.sa_handler == _SIG_IGN || sa.sa_flags&_SA_ONSTACK != 0 { 314 return 315 } 316 sa.sa_flags |= _SA_ONSTACK 317 if rt_sigaction(uintptr(i), &sa, nil, unsafe.Sizeof(sa.sa_mask)) != 0 { 318 throw("rt_sigaction failure") 319 } 320 } 321 322 func getsig(i int32) uintptr { 323 var sa sigactiont 324 325 memclr(unsafe.Pointer(&sa), unsafe.Sizeof(sa)) 326 if rt_sigaction(uintptr(i), nil, &sa, unsafe.Sizeof(sa.sa_mask)) != 0 { 327 throw("rt_sigaction read failure") 328 } 329 if sa.sa_handler == funcPC(sigtramp) { 330 return funcPC(sighandler) 331 } 332 return sa.sa_handler 333 } 334 335 //go:nosplit 336 func signalstack(s *stack) { 337 var st sigaltstackt 338 if s == nil { 339 st.ss_flags = _SS_DISABLE 340 } else { 341 st.ss_sp = (*byte)(unsafe.Pointer(s.lo)) 342 st.ss_size = s.hi - s.lo 343 st.ss_flags = 0 344 } 345 sigaltstack(&st, nil) 346 } 347 348 func updatesigmask(m sigmask) { 349 var mask sigset 350 sigcopyset(&mask, m) 351 rtsigprocmask(_SIG_SETMASK, &mask, nil, int32(unsafe.Sizeof(mask))) 352 } 353 354 func unblocksig(sig int32) { 355 var mask sigset 356 sigaddset(&mask, int(sig)) 357 rtsigprocmask(_SIG_UNBLOCK, &mask, nil, int32(unsafe.Sizeof(mask))) 358 }