| 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 |
"unsafe" |
| 9 |
) |
| 10 |
|
| 11 |
// For C code to call: |
| 12 |
//go:linkname minit |
| 13 |
|
| 14 |
func goenvs() { |
| 15 |
goenvs_unix() |
| 16 |
} |
| 17 |
|
| 18 |
// Called to initialize a new m (including the bootstrap m). |
| 19 |
// Called on the parent thread (main thread in case of bootstrap), can allocate memory. |
| 20 |
func mpreinit(mp *m) { |
| 21 |
mp.gsignal = malg(true, true, &mp.gsignalstack, &mp.gsignalstacksize) |
| 22 |
mp.gsignal.m = mp |
| 23 |
} |
| 24 |
|
| 25 |
// minit is called to initialize a new m (including the bootstrap m). |
| 26 |
// Called on the new thread, cannot allocate memory. |
| 27 |
func minit() { |
| 28 |
minitSignals() |
| 29 |
|
| 30 |
getg().m.procid = getProcID() |
| 31 |
} |
| 32 |
|
| 33 |
// Called from dropm to undo the effect of an minit. |
| 34 |
//go:nosplit |
| 35 |
//go:nowritebarrierrec |
| 36 |
func unminit() { |
| 37 |
unminitSignals() |
| 38 |
} |
| 39 |
|
| 40 |
// Called from exitm, but not from drop, to undo the effect of thread-owned |
| 41 |
// resources in minit, semacreate, or elsewhere. Do not take locks after calling this. |
| 42 |
func mdestroy(mp *m) { |
| 43 |
} |
| 44 |
|
| 45 |
var urandom_dev = []byte("/dev/urandom\x00") |
| 46 |
|
| 47 |
func getRandomData(r []byte) { |
| 48 |
if startupRandomData != nil { |
| 49 |
n := copy(r, startupRandomData) |
| 50 |
extendRandom(r, n) |
| 51 |
return |
| 52 |
} |
| 53 |
fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0) |
| 54 |
n := read(fd, unsafe.Pointer(&r[0]), int32(len(r))) |
| 55 |
closefd(fd) |
| 56 |
extendRandom(r, int(n)) |
| 57 |
} |
| 58 |
|
| 59 |
//go:noescape |
| 60 |
//extern-sysinfo pipe |
| 61 |
func libcPipe(*[2]int32) int32 |
| 62 |
|
| 63 |
func pipe() (r, w int32, e int32) { |
| 64 |
var p [2]int32 |
| 65 |
res := libcPipe(&p) |
| 66 |
if res < 0 { |
| 67 |
e = int32(errno()) |
| 68 |
} |
| 69 |
return p[0], p[1], e |
| 70 |
} |
| 71 |
|
| 72 |
//go:noescape |
| 73 |
//extern-sysinfo pipe2 |
| 74 |
func libcPipe2(*[2]int32, int32) int32 |
| 75 |
|
| 76 |
func pipe2(flags int32) (r, w int32, e int32) { |
| 77 |
var p [2]int32 |
| 78 |
res := libcPipe2(&p, flags) |
| 79 |
if res < 0 { |
| 80 |
e = int32(errno()) |
| 81 |
} |
| 82 |
return p[0], p[1], e |
| 83 |
} |
| 84 |
|
| 85 |
//extern __go_fcntl_uintptr |
| 86 |
func fcntlUintptr(fd, cmd, arg uintptr) (uintptr, uintptr) |
| 87 |
|
| 88 |
//go:nosplit |
| 89 |
func closeonexec(fd int32) { |
| 90 |
fcntlUintptr(uintptr(fd), _F_SETFD, _FD_CLOEXEC) |
| 91 |
} |
| 92 |
|
| 93 |
//go:nosplit |
| 94 |
func setNonblock(fd int32) { |
| 95 |
flags, errno := fcntlUintptr(uintptr(fd), _F_GETFL, 0) |
| 96 |
if errno == 0 { |
| 97 |
fcntlUintptr(uintptr(fd), _F_SETFL, flags|_O_NONBLOCK) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// For gccgo this is in the C code. |
| 102 |
func osyield() |
| 103 |
|
| 104 |
//go:nosplit |
| 105 |
func osyield_no_g() { |
| 106 |
osyield() |
| 107 |
} |