@@ -148,32 +148,35 @@ type libFunc uintptr
148
148
// It's defined in sys_aix_ppc64.go.
149
149
var asmsyscall6 libFunc
150
150
151
+ // syscallX functions must always be called with g != nil and m != nil,
152
+ // as it relies on g.m.libcall to pass arguments to asmcgocall.
153
+ // The few cases where syscalls haven't a g or a m must call their equivalent
154
+ // function in sys_aix_ppc64.s to handle them.
155
+
151
156
//go:nowritebarrier
152
157
//go:nosplit
153
158
func syscall0 (fn * libFunc ) (r , err uintptr ) {
154
159
gp := getg ()
155
- var mp * m
156
- if gp != nil {
157
- mp = gp .m
158
- }
159
- if mp != nil && mp .libcallsp == 0 {
160
+ mp := gp .m
161
+ resetLibcall := true
162
+ if mp .libcallsp == 0 {
160
163
mp .libcallg .set (gp )
161
164
mp .libcallpc = getcallerpc ()
162
165
// sp must be the last, because once async cpu profiler finds
163
166
// all three values to be non-zero, it will use them
164
167
mp .libcallsp = getcallersp ()
165
168
} else {
166
- mp = nil // See comment in sys_darwin.go:libcCall
169
+ resetLibcall = false // See comment in sys_darwin.go:libcCall
167
170
}
168
171
169
- c := & gp . m .libcall
172
+ c := & mp .libcall
170
173
c .fn = uintptr (unsafe .Pointer (fn ))
171
174
c .n = 0
172
175
c .args = uintptr (noescape (unsafe .Pointer (& fn ))) // it's unused but must be non-nil, otherwise crashes
173
176
174
177
asmcgocall (unsafe .Pointer (& asmsyscall6 ), unsafe .Pointer (c ))
175
178
176
- if mp != nil {
179
+ if resetLibcall {
177
180
mp .libcallsp = 0
178
181
}
179
182
@@ -184,18 +187,16 @@ func syscall0(fn *libFunc) (r, err uintptr) {
184
187
//go:nosplit
185
188
func syscall1 (fn * libFunc , a0 uintptr ) (r , err uintptr ) {
186
189
gp := getg ()
187
- var mp * m
188
- if gp != nil {
189
- mp = gp .m
190
- }
191
- if mp != nil && mp .libcallsp == 0 {
190
+ mp := gp .m
191
+ resetLibcall := true
192
+ if mp .libcallsp == 0 {
192
193
mp .libcallg .set (gp )
193
194
mp .libcallpc = getcallerpc ()
194
195
// sp must be the last, because once async cpu profiler finds
195
196
// all three values to be non-zero, it will use them
196
197
mp .libcallsp = getcallersp ()
197
198
} else {
198
- mp = nil // See comment in sys_darwin.go:libcCall
199
+ resetLibcall = false // See comment in sys_darwin.go:libcCall
199
200
}
200
201
201
202
c := & gp .m .libcall
@@ -205,7 +206,7 @@ func syscall1(fn *libFunc, a0 uintptr) (r, err uintptr) {
205
206
206
207
asmcgocall (unsafe .Pointer (& asmsyscall6 ), unsafe .Pointer (c ))
207
208
208
- if mp != nil {
209
+ if resetLibcall {
209
210
mp .libcallsp = 0
210
211
}
211
212
@@ -216,18 +217,16 @@ func syscall1(fn *libFunc, a0 uintptr) (r, err uintptr) {
216
217
//go:nosplit
217
218
func syscall2 (fn * libFunc , a0 , a1 uintptr ) (r , err uintptr ) {
218
219
gp := getg ()
219
- var mp * m
220
- if gp != nil {
221
- mp = gp .m
222
- }
223
- if mp != nil && mp .libcallsp == 0 {
220
+ mp := gp .m
221
+ resetLibcall := true
222
+ if mp .libcallsp == 0 {
224
223
mp .libcallg .set (gp )
225
224
mp .libcallpc = getcallerpc ()
226
225
// sp must be the last, because once async cpu profiler finds
227
226
// all three values to be non-zero, it will use them
228
227
mp .libcallsp = getcallersp ()
229
228
} else {
230
- mp = nil // See comment in sys_darwin.go:libcCall
229
+ resetLibcall = false // See comment in sys_darwin.go:libcCall
231
230
}
232
231
233
232
c := & gp .m .libcall
@@ -237,7 +236,7 @@ func syscall2(fn *libFunc, a0, a1 uintptr) (r, err uintptr) {
237
236
238
237
asmcgocall (unsafe .Pointer (& asmsyscall6 ), unsafe .Pointer (c ))
239
238
240
- if mp != nil {
239
+ if resetLibcall {
241
240
mp .libcallsp = 0
242
241
}
243
242
@@ -248,18 +247,16 @@ func syscall2(fn *libFunc, a0, a1 uintptr) (r, err uintptr) {
248
247
//go:nosplit
249
248
func syscall3 (fn * libFunc , a0 , a1 , a2 uintptr ) (r , err uintptr ) {
250
249
gp := getg ()
251
- var mp * m
252
- if gp != nil {
253
- mp = gp .m
254
- }
255
- if mp != nil && mp .libcallsp == 0 {
250
+ mp := gp .m
251
+ resetLibcall := true
252
+ if mp .libcallsp == 0 {
256
253
mp .libcallg .set (gp )
257
254
mp .libcallpc = getcallerpc ()
258
255
// sp must be the last, because once async cpu profiler finds
259
256
// all three values to be non-zero, it will use them
260
257
mp .libcallsp = getcallersp ()
261
258
} else {
262
- mp = nil // See comment in sys_darwin.go:libcCall
259
+ resetLibcall = false // See comment in sys_darwin.go:libcCall
263
260
}
264
261
265
262
c := & gp .m .libcall
@@ -269,7 +266,7 @@ func syscall3(fn *libFunc, a0, a1, a2 uintptr) (r, err uintptr) {
269
266
270
267
asmcgocall (unsafe .Pointer (& asmsyscall6 ), unsafe .Pointer (c ))
271
268
272
- if mp != nil {
269
+ if resetLibcall {
273
270
mp .libcallsp = 0
274
271
}
275
272
@@ -280,18 +277,16 @@ func syscall3(fn *libFunc, a0, a1, a2 uintptr) (r, err uintptr) {
280
277
//go:nosplit
281
278
func syscall4 (fn * libFunc , a0 , a1 , a2 , a3 uintptr ) (r , err uintptr ) {
282
279
gp := getg ()
283
- var mp * m
284
- if gp != nil {
285
- mp = gp .m
286
- }
287
- if mp != nil && mp .libcallsp == 0 {
280
+ mp := gp .m
281
+ resetLibcall := true
282
+ if mp .libcallsp == 0 {
288
283
mp .libcallg .set (gp )
289
284
mp .libcallpc = getcallerpc ()
290
285
// sp must be the last, because once async cpu profiler finds
291
286
// all three values to be non-zero, it will use them
292
287
mp .libcallsp = getcallersp ()
293
288
} else {
294
- mp = nil // See comment in sys_darwin.go:libcCall
289
+ resetLibcall = false // See comment in sys_darwin.go:libcCall
295
290
}
296
291
297
292
c := & gp .m .libcall
@@ -301,7 +296,7 @@ func syscall4(fn *libFunc, a0, a1, a2, a3 uintptr) (r, err uintptr) {
301
296
302
297
asmcgocall (unsafe .Pointer (& asmsyscall6 ), unsafe .Pointer (c ))
303
298
304
- if mp != nil {
299
+ if resetLibcall {
305
300
mp .libcallsp = 0
306
301
}
307
302
@@ -312,18 +307,16 @@ func syscall4(fn *libFunc, a0, a1, a2, a3 uintptr) (r, err uintptr) {
312
307
//go:nosplit
313
308
func syscall5 (fn * libFunc , a0 , a1 , a2 , a3 , a4 uintptr ) (r , err uintptr ) {
314
309
gp := getg ()
315
- var mp * m
316
- if gp != nil {
317
- mp = gp .m
318
- }
319
- if mp != nil && mp .libcallsp == 0 {
310
+ mp := gp .m
311
+ resetLibcall := true
312
+ if mp .libcallsp == 0 {
320
313
mp .libcallg .set (gp )
321
314
mp .libcallpc = getcallerpc ()
322
315
// sp must be the last, because once async cpu profiler finds
323
316
// all three values to be non-zero, it will use them
324
317
mp .libcallsp = getcallersp ()
325
318
} else {
326
- mp = nil // See comment in sys_darwin.go:libcCall
319
+ resetLibcall = false // See comment in sys_darwin.go:libcCall
327
320
}
328
321
329
322
c := & gp .m .libcall
@@ -333,7 +326,7 @@ func syscall5(fn *libFunc, a0, a1, a2, a3, a4 uintptr) (r, err uintptr) {
333
326
334
327
asmcgocall (unsafe .Pointer (& asmsyscall6 ), unsafe .Pointer (c ))
335
328
336
- if mp != nil {
329
+ if resetLibcall {
337
330
mp .libcallsp = 0
338
331
}
339
332
@@ -344,18 +337,16 @@ func syscall5(fn *libFunc, a0, a1, a2, a3, a4 uintptr) (r, err uintptr) {
344
337
//go:nosplit
345
338
func syscall6 (fn * libFunc , a0 , a1 , a2 , a3 , a4 , a5 uintptr ) (r , err uintptr ) {
346
339
gp := getg ()
347
- var mp * m
348
- if gp != nil {
349
- mp = gp .m
350
- }
351
- if mp != nil && mp .libcallsp == 0 {
340
+ mp := gp .m
341
+ resetLibcall := true
342
+ if mp .libcallsp == 0 {
352
343
mp .libcallg .set (gp )
353
344
mp .libcallpc = getcallerpc ()
354
345
// sp must be the last, because once async cpu profiler finds
355
346
// all three values to be non-zero, it will use them
356
347
mp .libcallsp = getcallersp ()
357
348
} else {
358
- mp = nil // See comment in sys_darwin.go:libcCall
349
+ resetLibcall = false // See comment in sys_darwin.go:libcCall
359
350
}
360
351
361
352
c := & gp .m .libcall
@@ -365,7 +356,7 @@ func syscall6(fn *libFunc, a0, a1, a2, a3, a4, a5 uintptr) (r, err uintptr) {
365
356
366
357
asmcgocall (unsafe .Pointer (& asmsyscall6 ), unsafe .Pointer (c ))
367
358
368
- if mp != nil {
359
+ if resetLibcall {
369
360
mp .libcallsp = 0
370
361
}
371
362
@@ -461,13 +452,23 @@ func getsystemcfg(label uint) uintptr {
461
452
return r
462
453
}
463
454
455
+ func usleep1 (us uint32 )
456
+
464
457
//go:nosplit
465
458
func usleep (us uint32 ) {
466
- r , err := syscall1 (& libc_usleep , uintptr (us ))
467
- if int32 (r ) == - 1 {
468
- println ("syscall usleep failed: " , hex (err ))
469
- throw ("syscall usleep" )
459
+ _g_ := getg ()
460
+
461
+ // Check the validity of m because we might be called in cgo callback
462
+ // path early enough where there isn't a g or a m available yet.
463
+ if _g_ != nil && _g_ .m != nil {
464
+ r , err := syscall1 (& libc_usleep , uintptr (us ))
465
+ if int32 (r ) == - 1 {
466
+ println ("syscall usleep failed: " , hex (err ))
467
+ throw ("syscall usleep" )
468
+ }
469
+ return
470
470
}
471
+ usleep1 (us )
471
472
}
472
473
473
474
//go:nosplit
@@ -541,8 +542,8 @@ func osyield1()
541
542
func osyield () {
542
543
_g_ := getg ()
543
544
544
- // Check the validity of m because we might be called in cgo callback
545
- // path early enough where there isn't a m available yet.
545
+ // Check the validity of m because it might be called during a cgo
546
+ // callback early enough where m isn't available yet.
546
547
if _g_ != nil && _g_ .m != nil {
547
548
r , err := syscall0 (& libc_sched_yield )
548
549
if int32 (r ) == - 1 {
@@ -611,11 +612,22 @@ func pthread_create(tid *pthread, attr *pthread_attr, fn *funcDescriptor, arg un
611
612
612
613
// On multi-thread program, sigprocmask must not be called.
613
614
// It's replaced by sigthreadmask.
615
+ func sigprocmask1 (how , new , old uintptr )
616
+
614
617
//go:nosplit
615
618
func sigprocmask (how int32 , new , old * sigset ) {
616
- r , err := syscall3 (& libpthread_sigthreadmask , uintptr (how ), uintptr (unsafe .Pointer (new )), uintptr (unsafe .Pointer (old )))
617
- if int32 (r ) != 0 {
618
- println ("syscall sigthreadmask failed: " , hex (err ))
619
- throw ("syscall sigthreadmask" )
619
+ _g_ := getg ()
620
+
621
+ // Check the validity of m because it might be called during a cgo
622
+ // callback early enough where m isn't available yet.
623
+ if _g_ != nil && _g_ .m != nil {
624
+ r , err := syscall3 (& libpthread_sigthreadmask , uintptr (how ), uintptr (unsafe .Pointer (new )), uintptr (unsafe .Pointer (old )))
625
+ if int32 (r ) != 0 {
626
+ println ("syscall sigthreadmask failed: " , hex (err ))
627
+ throw ("syscall sigthreadmask" )
628
+ }
629
+ return
620
630
}
631
+ sigprocmask1 (uintptr (how ), uintptr (unsafe .Pointer (new )), uintptr (unsafe .Pointer (old )))
632
+
621
633
}
0 commit comments