github.com/fjballest/golang@v0.0.0-20151209143359-e4c5fe594ca8/src/runtime/os3_solaris.go (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 package runtime 6 7 import "unsafe" 8 9 //go:cgo_export_dynamic runtime.end _end 10 //go:cgo_export_dynamic runtime.etext _etext 11 //go:cgo_export_dynamic runtime.edata _edata 12 13 //go:cgo_import_dynamic libc____errno ___errno "libc.so" 14 //go:cgo_import_dynamic libc_clock_gettime clock_gettime "libc.so" 15 //go:cgo_import_dynamic libc_close close "libc.so" 16 //go:cgo_import_dynamic libc_exit exit "libc.so" 17 //go:cgo_import_dynamic libc_fstat fstat "libc.so" 18 //go:cgo_import_dynamic libc_getcontext getcontext "libc.so" 19 //go:cgo_import_dynamic libc_getrlimit getrlimit "libc.so" 20 //go:cgo_import_dynamic libc_kill kill "libc.so" 21 //go:cgo_import_dynamic libc_madvise madvise "libc.so" 22 //go:cgo_import_dynamic libc_malloc malloc "libc.so" 23 //go:cgo_import_dynamic libc_mmap mmap "libc.so" 24 //go:cgo_import_dynamic libc_munmap munmap "libc.so" 25 //go:cgo_import_dynamic libc_open open "libc.so" 26 //go:cgo_import_dynamic libc_pthread_attr_destroy pthread_attr_destroy "libc.so" 27 //go:cgo_import_dynamic libc_pthread_attr_getstack pthread_attr_getstack "libc.so" 28 //go:cgo_import_dynamic libc_pthread_attr_init pthread_attr_init "libc.so" 29 //go:cgo_import_dynamic libc_pthread_attr_setdetachstate pthread_attr_setdetachstate "libc.so" 30 //go:cgo_import_dynamic libc_pthread_attr_setstack pthread_attr_setstack "libc.so" 31 //go:cgo_import_dynamic libc_pthread_create pthread_create "libc.so" 32 //go:cgo_import_dynamic libc_raise raise "libc.so" 33 //go:cgo_import_dynamic libc_read read "libc.so" 34 //go:cgo_import_dynamic libc_select select "libc.so" 35 //go:cgo_import_dynamic libc_sched_yield sched_yield "libc.so" 36 //go:cgo_import_dynamic libc_sem_init sem_init "libc.so" 37 //go:cgo_import_dynamic libc_sem_post sem_post "libc.so" 38 //go:cgo_import_dynamic libc_sem_reltimedwait_np sem_reltimedwait_np "libc.so" 39 //go:cgo_import_dynamic libc_sem_wait sem_wait "libc.so" 40 //go:cgo_import_dynamic libc_setitimer setitimer "libc.so" 41 //go:cgo_import_dynamic libc_sigaction sigaction "libc.so" 42 //go:cgo_import_dynamic libc_sigaltstack sigaltstack "libc.so" 43 //go:cgo_import_dynamic libc_sigprocmask sigprocmask "libc.so" 44 //go:cgo_import_dynamic libc_sysconf sysconf "libc.so" 45 //go:cgo_import_dynamic libc_usleep usleep "libc.so" 46 //go:cgo_import_dynamic libc_write write "libc.so" 47 48 //go:linkname libc____errno libc____errno 49 //go:linkname libc_clock_gettime libc_clock_gettime 50 //go:linkname libc_close libc_close 51 //go:linkname libc_exit libc_exit 52 //go:linkname libc_fstat libc_fstat 53 //go:linkname libc_getcontext libc_getcontext 54 //go:linkname libc_getrlimit libc_getrlimit 55 //go:linkname libc_kill libc_kill 56 //go:linkname libc_madvise libc_madvise 57 //go:linkname libc_malloc libc_malloc 58 //go:linkname libc_mmap libc_mmap 59 //go:linkname libc_munmap libc_munmap 60 //go:linkname libc_open libc_open 61 //go:linkname libc_pthread_attr_destroy libc_pthread_attr_destroy 62 //go:linkname libc_pthread_attr_getstack libc_pthread_attr_getstack 63 //go:linkname libc_pthread_attr_init libc_pthread_attr_init 64 //go:linkname libc_pthread_attr_setdetachstate libc_pthread_attr_setdetachstate 65 //go:linkname libc_pthread_attr_setstack libc_pthread_attr_setstack 66 //go:linkname libc_pthread_create libc_pthread_create 67 //go:linkname libc_raise libc_raise 68 //go:linkname libc_read libc_read 69 //go:linkname libc_select libc_select 70 //go:linkname libc_sched_yield libc_sched_yield 71 //go:linkname libc_sem_init libc_sem_init 72 //go:linkname libc_sem_post libc_sem_post 73 //go:linkname libc_sem_reltimedwait_np libc_sem_reltimedwait_np 74 //go:linkname libc_sem_wait libc_sem_wait 75 //go:linkname libc_setitimer libc_setitimer 76 //go:linkname libc_sigaction libc_sigaction 77 //go:linkname libc_sigaltstack libc_sigaltstack 78 //go:linkname libc_sigprocmask libc_sigprocmask 79 //go:linkname libc_sysconf libc_sysconf 80 //go:linkname libc_usleep libc_usleep 81 //go:linkname libc_write libc_write 82 83 var ( 84 libc____errno, 85 libc_clock_gettime, 86 libc_close, 87 libc_exit, 88 libc_fstat, 89 libc_getcontext, 90 libc_getrlimit, 91 libc_kill, 92 libc_madvise, 93 libc_malloc, 94 libc_mmap, 95 libc_munmap, 96 libc_open, 97 libc_pthread_attr_destroy, 98 libc_pthread_attr_getstack, 99 libc_pthread_attr_init, 100 libc_pthread_attr_setdetachstate, 101 libc_pthread_attr_setstack, 102 libc_pthread_create, 103 libc_raise, 104 libc_read, 105 libc_sched_yield, 106 libc_select, 107 libc_sem_init, 108 libc_sem_post, 109 libc_sem_reltimedwait_np, 110 libc_sem_wait, 111 libc_setitimer, 112 libc_sigaction, 113 libc_sigaltstack, 114 libc_sigprocmask, 115 libc_sysconf, 116 libc_usleep, 117 libc_write libcFunc 118 ) 119 120 var sigset_all = sigset{[4]uint32{^uint32(0), ^uint32(0), ^uint32(0), ^uint32(0)}} 121 122 func getncpu() int32 { 123 n := int32(sysconf(__SC_NPROCESSORS_ONLN)) 124 if n < 1 { 125 return 1 126 } 127 return n 128 } 129 130 func osinit() { 131 ncpu = getncpu() 132 } 133 134 func tstart_sysvicall() 135 136 // May run with m.p==nil, so write barriers are not allowed. 137 //go:nowritebarrier 138 func newosproc(mp *m, _ unsafe.Pointer) { 139 var ( 140 attr pthreadattr 141 oset sigset 142 tid pthread 143 ret int32 144 size uint64 145 ) 146 147 if pthread_attr_init(&attr) != 0 { 148 throw("pthread_attr_init") 149 } 150 if pthread_attr_setstack(&attr, 0, 0x200000) != 0 { 151 throw("pthread_attr_setstack") 152 } 153 if pthread_attr_getstack(&attr, unsafe.Pointer(&mp.g0.stack.hi), &size) != 0 { 154 throw("pthread_attr_getstack") 155 } 156 mp.g0.stack.lo = mp.g0.stack.hi - uintptr(size) 157 if pthread_attr_setdetachstate(&attr, _PTHREAD_CREATE_DETACHED) != 0 { 158 throw("pthread_attr_setdetachstate") 159 } 160 161 // Disable signals during create, so that the new thread starts 162 // with signals disabled. It will enable them in minit. 163 sigprocmask(_SIG_SETMASK, &sigset_all, &oset) 164 ret = pthread_create(&tid, &attr, funcPC(tstart_sysvicall), unsafe.Pointer(mp)) 165 sigprocmask(_SIG_SETMASK, &oset, nil) 166 if ret != 0 { 167 print("runtime: failed to create new OS thread (have ", mcount(), " already; errno=", ret, ")\n") 168 throw("newosproc") 169 } 170 } 171 172 var urandom_dev = []byte("/dev/urandom\x00") 173 174 //go:nosplit 175 func getRandomData(r []byte) { 176 fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0) 177 n := read(fd, unsafe.Pointer(&r[0]), int32(len(r))) 178 closefd(fd) 179 extendRandom(r, int(n)) 180 } 181 182 func goenvs() { 183 goenvs_unix() 184 } 185 186 // Called to initialize a new m (including the bootstrap m). 187 // Called on the parent thread (main thread in case of bootstrap), can allocate memory. 188 func mpreinit(mp *m) { 189 mp.gsignal = malg(32 * 1024) 190 mp.gsignal.m = mp 191 } 192 193 func miniterrno() 194 195 //go:nosplit 196 func msigsave(mp *m) { 197 sigprocmask(_SIG_SETMASK, nil, &mp.sigmask) 198 } 199 200 //go:nosplit 201 func msigrestore(mp *m) { 202 sigprocmask(_SIG_SETMASK, &mp.sigmask, nil) 203 } 204 205 //go:nosplit 206 func sigblock() { 207 sigprocmask(_SIG_SETMASK, &sigset_all, nil) 208 } 209 210 // Called to initialize a new m (including the bootstrap m). 211 // Called on the new thread, can not allocate memory. 212 func minit() { 213 _g_ := getg() 214 asmcgocall(unsafe.Pointer(funcPC(miniterrno)), unsafe.Pointer(&libc____errno)) 215 // Initialize signal handling 216 signalstack(&_g_.m.gsignal.stack) 217 218 // restore signal mask from m.sigmask and unblock essential signals 219 nmask := _g_.m.sigmask 220 for i := range sigtable { 221 if sigtable[i].flags&_SigUnblock != 0 { 222 nmask.__sigbits[(i-1)/32] &^= 1 << ((uint32(i) - 1) & 31) 223 } 224 } 225 sigprocmask(_SIG_SETMASK, &nmask, nil) 226 } 227 228 // Called from dropm to undo the effect of an minit. 229 func unminit() { 230 signalstack(nil) 231 } 232 233 func memlimit() uintptr { 234 /* 235 TODO: Convert to Go when something actually uses the result. 236 Rlimit rl; 237 extern byte runtime·text[], runtime·end[]; 238 uintptr used; 239 240 if(runtime·getrlimit(RLIMIT_AS, &rl) != 0) 241 return 0; 242 if(rl.rlim_cur >= 0x7fffffff) 243 return 0; 244 245 // Estimate our VM footprint excluding the heap. 246 // Not an exact science: use size of binary plus 247 // some room for thread stacks. 248 used = runtime·end - runtime·text + (64<<20); 249 if(used >= rl.rlim_cur) 250 return 0; 251 252 // If there's not at least 16 MB left, we're probably 253 // not going to be able to do much. Treat as no limit. 254 rl.rlim_cur -= used; 255 if(rl.rlim_cur < (16<<20)) 256 return 0; 257 258 return rl.rlim_cur - used; 259 */ 260 261 return 0 262 } 263 264 func sigtramp() 265 266 func setsig(i int32, fn uintptr, restart bool) { 267 var sa sigactiont 268 269 sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK 270 sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK 271 if restart { 272 sa.sa_flags |= _SA_RESTART 273 } 274 sa.sa_mask = sigset_all 275 if fn == funcPC(sighandler) { 276 fn = funcPC(sigtramp) 277 } 278 *((*uintptr)(unsafe.Pointer(&sa._funcptr))) = fn 279 sigaction(i, &sa, nil) 280 } 281 282 func setsigstack(i int32) { 283 throw("setsigstack") 284 } 285 286 func getsig(i int32) uintptr { 287 var sa sigactiont 288 sigaction(i, nil, &sa) 289 if *((*uintptr)(unsafe.Pointer(&sa._funcptr))) == funcPC(sigtramp) { 290 return funcPC(sighandler) 291 } 292 return *((*uintptr)(unsafe.Pointer(&sa._funcptr))) 293 } 294 295 //go:nosplit 296 func signalstack(s *stack) { 297 var st sigaltstackt 298 if s == nil { 299 st.ss_flags = _SS_DISABLE 300 } else { 301 st.ss_sp = (*byte)(unsafe.Pointer(s.lo)) 302 st.ss_size = uint64(s.hi - s.lo) 303 st.ss_flags = 0 304 } 305 sigaltstack(&st, nil) 306 } 307 308 func updatesigmask(m sigmask) { 309 var mask sigset 310 copy(mask.__sigbits[:], m[:]) 311 sigprocmask(_SIG_SETMASK, &mask, nil) 312 } 313 314 func unblocksig(sig int32) { 315 var mask sigset 316 mask.__sigbits[(sig-1)/32] |= 1 << ((uint32(sig) - 1) & 31) 317 sigprocmask(_SIG_UNBLOCK, &mask, nil) 318 } 319 320 //go:nosplit 321 func semacreate(mp *m) { 322 if mp.waitsema != 0 { 323 return 324 } 325 326 var sem *semt 327 _g_ := getg() 328 329 // Call libc's malloc rather than malloc. This will 330 // allocate space on the C heap. We can't call malloc 331 // here because it could cause a deadlock. 332 _g_.m.libcall.fn = uintptr(unsafe.Pointer(&libc_malloc)) 333 _g_.m.libcall.n = 1 334 memclr(unsafe.Pointer(&_g_.m.scratch), uintptr(len(_g_.m.scratch.v))) 335 _g_.m.scratch.v[0] = unsafe.Sizeof(*sem) 336 _g_.m.libcall.args = uintptr(unsafe.Pointer(&_g_.m.scratch)) 337 asmcgocall(unsafe.Pointer(&asmsysvicall6), unsafe.Pointer(&_g_.m.libcall)) 338 sem = (*semt)(unsafe.Pointer(_g_.m.libcall.r1)) 339 if sem_init(sem, 0, 0) != 0 { 340 throw("sem_init") 341 } 342 mp.waitsema = uintptr(unsafe.Pointer(sem)) 343 } 344 345 //go:nosplit 346 func semasleep(ns int64) int32 { 347 _m_ := getg().m 348 if ns >= 0 { 349 _m_.ts.tv_sec = ns / 1000000000 350 _m_.ts.tv_nsec = ns % 1000000000 351 352 _m_.libcall.fn = uintptr(unsafe.Pointer(&libc_sem_reltimedwait_np)) 353 _m_.libcall.n = 2 354 memclr(unsafe.Pointer(&_m_.scratch), uintptr(len(_m_.scratch.v))) 355 _m_.scratch.v[0] = _m_.waitsema 356 _m_.scratch.v[1] = uintptr(unsafe.Pointer(&_m_.ts)) 357 _m_.libcall.args = uintptr(unsafe.Pointer(&_m_.scratch)) 358 asmcgocall(unsafe.Pointer(&asmsysvicall6), unsafe.Pointer(&_m_.libcall)) 359 if *_m_.perrno != 0 { 360 if *_m_.perrno == _ETIMEDOUT || *_m_.perrno == _EAGAIN || *_m_.perrno == _EINTR { 361 return -1 362 } 363 throw("sem_reltimedwait_np") 364 } 365 return 0 366 } 367 for { 368 _m_.libcall.fn = uintptr(unsafe.Pointer(&libc_sem_wait)) 369 _m_.libcall.n = 1 370 memclr(unsafe.Pointer(&_m_.scratch), uintptr(len(_m_.scratch.v))) 371 _m_.scratch.v[0] = _m_.waitsema 372 _m_.libcall.args = uintptr(unsafe.Pointer(&_m_.scratch)) 373 asmcgocall(unsafe.Pointer(&asmsysvicall6), unsafe.Pointer(&_m_.libcall)) 374 if _m_.libcall.r1 == 0 { 375 break 376 } 377 if *_m_.perrno == _EINTR { 378 continue 379 } 380 throw("sem_wait") 381 } 382 return 0 383 } 384 385 //go:nosplit 386 func semawakeup(mp *m) { 387 if sem_post((*semt)(unsafe.Pointer(mp.waitsema))) != 0 { 388 throw("sem_post") 389 } 390 } 391 392 //go:nosplit 393 func closefd(fd int32) int32 { 394 return int32(sysvicall1(&libc_close, uintptr(fd))) 395 } 396 397 //go:nosplit 398 func exit(r int32) { 399 sysvicall1(&libc_exit, uintptr(r)) 400 } 401 402 //go:nosplit 403 func getcontext(context *ucontext) /* int32 */ { 404 sysvicall1(&libc_getcontext, uintptr(unsafe.Pointer(context))) 405 } 406 407 //go:nosplit 408 func madvise(addr unsafe.Pointer, n uintptr, flags int32) { 409 sysvicall3(&libc_madvise, uintptr(addr), uintptr(n), uintptr(flags)) 410 } 411 412 //go:nosplit 413 func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) unsafe.Pointer { 414 return unsafe.Pointer(sysvicall6(&libc_mmap, uintptr(addr), uintptr(n), uintptr(prot), uintptr(flags), uintptr(fd), uintptr(off))) 415 } 416 417 //go:nosplit 418 func munmap(addr unsafe.Pointer, n uintptr) { 419 sysvicall2(&libc_munmap, uintptr(addr), uintptr(n)) 420 } 421 422 func nanotime1() 423 424 //go:nosplit 425 func nanotime() int64 { 426 return int64(sysvicall0((*libcFunc)(unsafe.Pointer(funcPC(nanotime1))))) 427 } 428 429 //go:nosplit 430 func open(path *byte, mode, perm int32) int32 { 431 return int32(sysvicall3(&libc_open, uintptr(unsafe.Pointer(path)), uintptr(mode), uintptr(perm))) 432 } 433 434 func pthread_attr_destroy(attr *pthreadattr) int32 { 435 return int32(sysvicall1(&libc_pthread_attr_destroy, uintptr(unsafe.Pointer(attr)))) 436 } 437 438 func pthread_attr_getstack(attr *pthreadattr, addr unsafe.Pointer, size *uint64) int32 { 439 return int32(sysvicall3(&libc_pthread_attr_getstack, uintptr(unsafe.Pointer(attr)), uintptr(addr), uintptr(unsafe.Pointer(size)))) 440 } 441 442 func pthread_attr_init(attr *pthreadattr) int32 { 443 return int32(sysvicall1(&libc_pthread_attr_init, uintptr(unsafe.Pointer(attr)))) 444 } 445 446 func pthread_attr_setdetachstate(attr *pthreadattr, state int32) int32 { 447 return int32(sysvicall2(&libc_pthread_attr_setdetachstate, uintptr(unsafe.Pointer(attr)), uintptr(state))) 448 } 449 450 func pthread_attr_setstack(attr *pthreadattr, addr uintptr, size uint64) int32 { 451 return int32(sysvicall3(&libc_pthread_attr_setstack, uintptr(unsafe.Pointer(attr)), uintptr(addr), uintptr(size))) 452 } 453 454 func pthread_create(thread *pthread, attr *pthreadattr, fn uintptr, arg unsafe.Pointer) int32 { 455 return int32(sysvicall4(&libc_pthread_create, uintptr(unsafe.Pointer(thread)), uintptr(unsafe.Pointer(attr)), uintptr(fn), uintptr(arg))) 456 } 457 458 func raise(sig int32) /* int32 */ { 459 sysvicall1(&libc_raise, uintptr(sig)) 460 } 461 462 func raiseproc(sig int32) /* int32 */ { 463 pid := sysvicall0(&libc_getpid) 464 sysvicall2(&libc_kill, pid, uintptr(sig)) 465 } 466 467 //go:nosplit 468 func read(fd int32, buf unsafe.Pointer, nbyte int32) int32 { 469 return int32(sysvicall3(&libc_read, uintptr(fd), uintptr(buf), uintptr(nbyte))) 470 } 471 472 //go:nosplit 473 func sem_init(sem *semt, pshared int32, value uint32) int32 { 474 return int32(sysvicall3(&libc_sem_init, uintptr(unsafe.Pointer(sem)), uintptr(pshared), uintptr(value))) 475 } 476 477 //go:nosplit 478 func sem_post(sem *semt) int32 { 479 return int32(sysvicall1(&libc_sem_post, uintptr(unsafe.Pointer(sem)))) 480 } 481 482 //go:nosplit 483 func sem_reltimedwait_np(sem *semt, timeout *timespec) int32 { 484 return int32(sysvicall2(&libc_sem_reltimedwait_np, uintptr(unsafe.Pointer(sem)), uintptr(unsafe.Pointer(timeout)))) 485 } 486 487 //go:nosplit 488 func sem_wait(sem *semt) int32 { 489 return int32(sysvicall1(&libc_sem_wait, uintptr(unsafe.Pointer(sem)))) 490 } 491 492 func setitimer(which int32, value *itimerval, ovalue *itimerval) /* int32 */ { 493 sysvicall3(&libc_setitimer, uintptr(which), uintptr(unsafe.Pointer(value)), uintptr(unsafe.Pointer(ovalue))) 494 } 495 496 func sigaction(sig int32, act *sigactiont, oact *sigactiont) /* int32 */ { 497 sysvicall3(&libc_sigaction, uintptr(sig), uintptr(unsafe.Pointer(act)), uintptr(unsafe.Pointer(oact))) 498 } 499 500 func sigaltstack(ss *sigaltstackt, oss *sigaltstackt) /* int32 */ { 501 sysvicall2(&libc_sigaltstack, uintptr(unsafe.Pointer(ss)), uintptr(unsafe.Pointer(oss))) 502 } 503 504 //go:nosplit 505 func sigprocmask(how int32, set *sigset, oset *sigset) /* int32 */ { 506 sysvicall3(&libc_sigprocmask, uintptr(how), uintptr(unsafe.Pointer(set)), uintptr(unsafe.Pointer(oset))) 507 } 508 509 func sysconf(name int32) int64 { 510 return int64(sysvicall1(&libc_sysconf, uintptr(name))) 511 } 512 513 func usleep1(uint32) 514 515 //go:nosplit 516 func usleep(µs uint32) { 517 usleep1(µs) 518 } 519 520 //go:nosplit 521 func write(fd uintptr, buf unsafe.Pointer, nbyte int32) int32 { 522 return int32(sysvicall3(&libc_write, uintptr(fd), uintptr(buf), uintptr(nbyte))) 523 } 524 525 func osyield1() 526 527 //go:nosplit 528 func osyield() { 529 _g_ := getg() 530 531 // Check the validity of m because we might be called in cgo callback 532 // path early enough where there isn't a m available yet. 533 if _g_ != nil && _g_.m != nil { 534 sysvicall0(&libc_sched_yield) 535 return 536 } 537 osyield1() 538 }