-
Notifications
You must be signed in to change notification settings - Fork 8
/
os_linux.go.patch
267 lines (250 loc) · 6.31 KB
/
os_linux.go.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//--from
import (
"runtime/internal/sys"
"unsafe"
)
//--to
import (
"internal/abi"
"unsafe"
)
//--from
//go:noescape
func futex(addr unsafe.Pointer, op int32, val uint32, ts, addr2 unsafe.Pointer, val3 uint32) int32
//--to
//go:nosplit
//go:cgo_unsafe_args
func futex(addr unsafe.Pointer, op int32, val uint32, ts, addr2 unsafe.Pointer, val3 uint32) int32 {
ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(futex_trampoline)), unsafe.Pointer(&addr))
return ret
}
func futex_trampoline(addr unsafe.Pointer, op int32, val uint32, ts, addr2 unsafe.Pointer, val3 uint32) int32
//--from
func sysargs(argc int32, argv **byte) {
n := argc + 1
// skip over argv, envp to get to auxv
for argv_index(argv, n) != nil {
n++
}
// skip NULL separator
n++
// now argv+n is auxv
auxv := (*[1 << 28]uintptr)(add(unsafe.Pointer(argv), uintptr(n)*sys.PtrSize))
if sysauxv(auxv[:]) != 0 {
return
}
// In some situations we don't get a loader-provided
// auxv, such as when loaded as a library on Android.
// Fall back to /proc/self/auxv.
fd := open(&procAuxv[0], 0 /* O_RDONLY */, 0)
if fd < 0 {
// On Android, /proc/self/auxv might be unreadable (issue 9229), so we fallback to
// try using mincore to detect the physical page size.
// mincore should return EINVAL when address is not a multiple of system page size.
const size = 256 << 10 // size of memory region to allocate
p, err := mmap(nil, size, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
if err != 0 {
return
}
var n uintptr
for n = 4 << 10; n < size; n <<= 1 {
err := mincore(unsafe.Pointer(uintptr(p)+n), 1, &addrspace_vec[0])
if err == 0 {
physPageSize = n
break
}
}
if physPageSize == 0 {
physPageSize = size
}
munmap(p, size)
return
}
var buf [128]uintptr
n = read(fd, noescape(unsafe.Pointer(&buf[0])), int32(unsafe.Sizeof(buf)))
closefd(fd)
if n < 0 {
return
}
// Make sure buf is terminated, even if we didn't read
// the whole file.
buf[len(buf)-2] = _AT_NULL
sysauxv(buf[:])
}
//--to
func sysargs(argc int32, argv **byte) {
// argc/argv is not reliable on some machines.
// Skip analysing them.
// In some situations we don't get a loader-provided
// auxv, such as when loaded as a library on Android.
// Fall back to /proc/self/auxv.
fd := open(&procAuxv[0], 0 /* O_RDONLY */, 0)
if fd < 0 {
// On Android, /proc/self/auxv might be unreadable (issue 9229), so we fallback to
// try using mincore to detect the physical page size.
// mincore should return EINVAL when address is not a multiple of system page size.
const size = 256 << 10 // size of memory region to allocate
p, err := mmap(nil, size, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
if err != 0 {
return
}
var n uintptr
for n = 4 << 10; n < size; n <<= 1 {
err := mincore(unsafe.Pointer(uintptr(p)+n), 1, &addrspace_vec[0])
if err == 0 {
physPageSize = n
break
}
}
if physPageSize == 0 {
physPageSize = size
}
munmap(p, size)
return
}
var buf [128]uintptr
n := read(fd, noescape(unsafe.Pointer(&buf[0])), int32(unsafe.Sizeof(buf)))
closefd(fd)
if n < 0 {
return
}
// Make sure buf is terminated, even if we didn't read
// the whole file.
buf[len(buf)-2] = _AT_NULL
sysauxv(buf[:])
}
//--from
func getRandomData(r []byte) {
if startupRandomData != nil {
n := copy(r, startupRandomData)
extendRandom(r, n)
return
}
fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0)
n := read(fd, unsafe.Pointer(&r[0]), int32(len(r)))
closefd(fd)
extendRandom(r, int(n))
}
//--to
// Use getRandomData in os_plan9.go.
//go:nosplit
func getRandomData(r []byte) {
// inspired by wyrand see hash32.go for detail
t := nanotime()
v := getg().m.procid ^ uint64(t)
for len(r) > 0 {
v ^= 0xa0761d6478bd642f
v *= 0xe7037ed1a0b428db
size := 8
if len(r) < 8 {
size = len(r)
}
for i := 0; i < size; i++ {
r[i] = byte(v >> (8 * i))
}
r = r[size:]
v = v>>32 | v<<32
}
}
//--from
func gettid() uint32
//--to
//go:nosplit
//go:cgo_unsafe_args
func gettid() (tid uint32) {
libcCall(unsafe.Pointer(abi.FuncPCABI0(gettid_trampoline)), unsafe.Pointer(&tid))
return
}
func gettid_trampoline() uint32
//--from
//go:noescape
func sigaltstack(new, old *stackt)
//--to
func sigaltstack(new, old *stackt) {
// Do nothing.
}
//--from
func sigprocmask(how int32, new, old *sigset) {
rtsigprocmask(how, new, old, int32(unsafe.Sizeof(*new)))
}
//--to
func sigprocmask(how int32, new, old *sigset) {
// Do nothing.
// rtsigprocmask(how, new, old, int32(unsafe.Sizeof(*new)))
}
//--from
func raise(sig uint32)
//--to
func raise(sig uint32) {
// Do nothing
}
//--from
func raiseproc(sig uint32)
//--to
func raiseproc(sig uint32) {
// Do nothing
}
//--from
//go:noescape
func sched_getaffinity(pid, len uintptr, buf *byte) int32
//--to
//go:nosplit
//go:cgo_unsafe_args
func sched_getaffinity(pid, len uintptr, buf *byte) int32 {
return libcCall(unsafe.Pointer(abi.FuncPCABI0(sched_getaffinity_trampoline)), unsafe.Pointer(&pid))
}
func sched_getaffinity_trampoline(pid, len uintptr, buf *byte) int32
//--from
func osyield()
//--to
//go:nosplit
//go:cgo_unsafe_args
func osyield() {
libcCall(unsafe.Pointer(abi.FuncPCABI0(osyield_trampoline)), nil)
}
func osyield_trampoline()
//--from
func signalM(mp *m, sig int) {
tgkill(getpid(), int(mp.procid), sig)
}
//--to
func signalM(mp *m, sig int) {
// Do nothiing.
}
//--append
//go:linkname c_calloc c_calloc
//go:cgo_import_static c_calloc
var c_calloc byte
//go:linkname c_closefd c_closefd
//go:cgo_import_static c_closefd
var c_closefd byte
//go:linkname c_exit c_exit
//go:cgo_import_static c_exit
var c_exit byte
//go:linkname c_gettid c_gettid
//go:cgo_import_static c_gettid
var c_gettid byte
//go:linkname c_nanotime1 c_nanotime1
//go:cgo_import_static c_nanotime1
var c_nanotime1 byte
//go:linkname c_open c_open
//go:cgo_import_static c_open
var c_open byte
//go:linkname c_osyield c_osyield
//go:cgo_import_static c_osyield
var c_osyield byte
//go:linkname c_read c_read
//go:cgo_import_static c_read
var c_read byte
//go:linkname c_sched_getaffinity c_sched_getaffinity
//go:cgo_import_static c_sched_getaffinity
var c_sched_getaffinity byte
//go:linkname c_usleep c_usleep
//go:cgo_import_static c_usleep
var c_usleep byte
//go:linkname c_walltime1 c_walltime1
//go:cgo_import_static c_walltime1
var c_walltime1 byte
//go:linkname c_write1 c_write1
//go:cgo_import_static c_write1
var c_write1 byte