1 // Copyright 2018 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.
9 // Call fn with arg as its argument. Return what fn returns.
10 // fn is the raw pc value of the entry point of the desired function.
11 // Switches to the system stack, if not already there.
12 // Preserves the calling point as the location where a profiler traceback will begin.
14 func libcCall(fn
, arg unsafe
.Pointer
) int32 {
15 // Leave caller's PC/SP/G around for traceback.
21 if mp
!= nil && mp
.libcallsp
== 0 {
23 mp
.libcallpc
= getcallerpc()
24 // sp must be the last, because once async cpu profiler finds
25 // all three values to be non-zero, it will use them
26 mp
.libcallsp
= getcallersp()
28 // Make sure we don't reset libcallsp. This makes
29 // libcCall reentrant; We remember the g/pc/sp for the
30 // first call on an M, until that libcCall instance
31 // returns. Reentrance only matters for signals, as
32 // libc never calls back into Go. The tricky case is
33 // where we call libcX from an M and record g/pc/sp.
34 // Before that call returns, a signal arrives on the
35 // same M and the signal handling code calls another
36 // libc function. We don't want that second libcCall
37 // from within the handler to be recorded, and we
38 // don't want that call's completion to zero
40 // We don't need to set libcall* while we're in a sighandler
41 // (even if we're not currently in libc) because we block all
42 // signals while we're handling a signal. That includes the
43 // profile signal, which is the one that uses the libcall* info.
46 res
:= asmcgocall(fn
, arg
)
53 // The *_trampoline functions convert from the Go calling convention to the C calling convention
54 // and then call the underlying libc function. They are defined in sys_darwin_$ARCH.s.
58 func pthread_attr_init(attr
*pthreadattr
) int32 {
59 return libcCall(unsafe
.Pointer(funcPC(pthread_attr_init_trampoline
)), unsafe
.Pointer(&attr
))
61 func pthread_attr_init_trampoline()
65 func pthread_attr_setstacksize(attr
*pthreadattr
, size
uintptr) int32 {
66 return libcCall(unsafe
.Pointer(funcPC(pthread_attr_setstacksize_trampoline
)), unsafe
.Pointer(&attr
))
68 func pthread_attr_setstacksize_trampoline()
72 func pthread_attr_setdetachstate(attr
*pthreadattr
, state
int) int32 {
73 return libcCall(unsafe
.Pointer(funcPC(pthread_attr_setdetachstate_trampoline
)), unsafe
.Pointer(&attr
))
75 func pthread_attr_setdetachstate_trampoline()
79 func pthread_create(attr
*pthreadattr
, start
uintptr, arg unsafe
.Pointer
) int32 {
80 return libcCall(unsafe
.Pointer(funcPC(pthread_create_trampoline
)), unsafe
.Pointer(&attr
))
82 func pthread_create_trampoline()
86 func raise(sig
uint32) {
87 libcCall(unsafe
.Pointer(funcPC(raise_trampoline
)), unsafe
.Pointer(&sig
))
89 func raise_trampoline()
93 func pthread_self() (t pthread
) {
94 libcCall(unsafe
.Pointer(funcPC(pthread_self_trampoline
)), unsafe
.Pointer(&t
))
97 func pthread_self_trampoline()
99 func mmap(addr unsafe
.Pointer
, n
uintptr, prot
, flags
, fd
int32, off
uint32) (unsafe
.Pointer
, int) {
103 prot
, flags
, fd
int32
107 }{addr
, n
, prot
, flags
, fd
, off
, nil, 0}
108 libcCall(unsafe
.Pointer(funcPC(mmap_trampoline
)), unsafe
.Pointer(&args
))
109 return args
.ret1
, args
.ret2
111 func mmap_trampoline()
115 func munmap(addr unsafe
.Pointer
, n
uintptr) {
116 libcCall(unsafe
.Pointer(funcPC(munmap_trampoline
)), unsafe
.Pointer(&addr
))
118 func munmap_trampoline()
122 func madvise(addr unsafe
.Pointer
, n
uintptr, flags
int32) {
123 libcCall(unsafe
.Pointer(funcPC(madvise_trampoline
)), unsafe
.Pointer(&addr
))
125 func madvise_trampoline()
129 func read(fd
int32, p unsafe
.Pointer
, n
int32) int32 {
130 return libcCall(unsafe
.Pointer(funcPC(read_trampoline
)), unsafe
.Pointer(&fd
))
132 func read_trampoline()
136 func closefd(fd
int32) int32 {
137 return libcCall(unsafe
.Pointer(funcPC(close_trampoline
)), unsafe
.Pointer(&fd
))
139 func close_trampoline()
143 func exit(code
int32) {
144 libcCall(unsafe
.Pointer(funcPC(exit_trampoline
)), unsafe
.Pointer(&code
))
146 func exit_trampoline()
150 func usleep(usec
uint32) {
151 libcCall(unsafe
.Pointer(funcPC(usleep_trampoline
)), unsafe
.Pointer(&usec
))
153 func usleep_trampoline()
157 func write(fd
uintptr, p unsafe
.Pointer
, n
int32) int32 {
158 return libcCall(unsafe
.Pointer(funcPC(write_trampoline
)), unsafe
.Pointer(&fd
))
160 func write_trampoline()
164 func open(name
*byte, mode
, perm
int32) (ret
int32) {
165 return libcCall(unsafe
.Pointer(funcPC(open_trampoline
)), unsafe
.Pointer(&name
))
167 func open_trampoline()
171 func nanotime() int64 {
174 numer
, denom
uint32 // conversion factors. nanoseconds = t * numer / denom.
176 libcCall(unsafe
.Pointer(funcPC(nanotime_trampoline
)), unsafe
.Pointer(&r
))
177 // Note: Apple seems unconcerned about overflow here. See
178 // https://developer.apple.com/library/content/qa/qa1398/_index.html
179 // Note also, numer == denom == 1 is common.
189 func nanotime_trampoline()
193 func walltime() (int64, int32) {
195 libcCall(unsafe
.Pointer(funcPC(walltime_trampoline
)), unsafe
.Pointer(&t
))
196 return int64(t
.tv_sec
), 1000 * t
.tv_usec
198 func walltime_trampoline()
202 func sigaction(sig
uint32, new *usigactiont
, old
*usigactiont
) {
203 libcCall(unsafe
.Pointer(funcPC(sigaction_trampoline
)), unsafe
.Pointer(&sig
))
205 func sigaction_trampoline()
209 func sigprocmask(how
uint32, new *sigset
, old
*sigset
) {
210 libcCall(unsafe
.Pointer(funcPC(sigprocmask_trampoline
)), unsafe
.Pointer(&how
))
212 func sigprocmask_trampoline()
216 func sigaltstack(new *stackt
, old
*stackt
) {
217 if new != nil && new.ss_flags
&_SS_DISABLE
!= 0 && new.ss_size
== 0 {
218 // Despite the fact that Darwin's sigaltstack man page says it ignores the size
219 // when SS_DISABLE is set, it doesn't. sigaltstack returns ENOMEM
220 // if we don't give it a reasonable size.
221 // ref: http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20140421/214296.html
224 libcCall(unsafe
.Pointer(funcPC(sigaltstack_trampoline
)), unsafe
.Pointer(&new))
226 func sigaltstack_trampoline()
230 func raiseproc(sig
uint32) {
231 libcCall(unsafe
.Pointer(funcPC(raiseproc_trampoline
)), unsafe
.Pointer(&sig
))
233 func raiseproc_trampoline()
237 func setitimer(mode
int32, new, old
*itimerval
) {
238 libcCall(unsafe
.Pointer(funcPC(setitimer_trampoline
)), unsafe
.Pointer(&mode
))
240 func setitimer_trampoline()
244 func sysctl(mib
*uint32, miblen
uint32, out
*byte, size
*uintptr, dst
*byte, ndst
uintptr) int32 {
245 return libcCall(unsafe
.Pointer(funcPC(sysctl_trampoline
)), unsafe
.Pointer(&mib
))
247 func sysctl_trampoline()
251 func fcntl(fd
, cmd
, arg
int32) int32 {
252 return libcCall(unsafe
.Pointer(funcPC(fcntl_trampoline
)), unsafe
.Pointer(&fd
))
254 func fcntl_trampoline()
258 func kqueue() int32 {
259 v
:= libcCall(unsafe
.Pointer(funcPC(kqueue_trampoline
)), nil)
262 func kqueue_trampoline()
266 func kevent(kq
int32, ch
*keventt
, nch
int32, ev
*keventt
, nev
int32, ts
*timespec
) int32 {
267 return libcCall(unsafe
.Pointer(funcPC(kevent_trampoline
)), unsafe
.Pointer(&kq
))
269 func kevent_trampoline()
273 func pthread_mutex_init(m
*pthreadmutex
, attr
*pthreadmutexattr
) int32 {
274 return libcCall(unsafe
.Pointer(funcPC(pthread_mutex_init_trampoline
)), unsafe
.Pointer(&m
))
276 func pthread_mutex_init_trampoline()
280 func pthread_mutex_lock(m
*pthreadmutex
) int32 {
281 return libcCall(unsafe
.Pointer(funcPC(pthread_mutex_lock_trampoline
)), unsafe
.Pointer(&m
))
283 func pthread_mutex_lock_trampoline()
287 func pthread_mutex_unlock(m
*pthreadmutex
) int32 {
288 return libcCall(unsafe
.Pointer(funcPC(pthread_mutex_unlock_trampoline
)), unsafe
.Pointer(&m
))
290 func pthread_mutex_unlock_trampoline()
294 func pthread_cond_init(c
*pthreadcond
, attr
*pthreadcondattr
) int32 {
295 return libcCall(unsafe
.Pointer(funcPC(pthread_cond_init_trampoline
)), unsafe
.Pointer(&c
))
297 func pthread_cond_init_trampoline()
301 func pthread_cond_wait(c
*pthreadcond
, m
*pthreadmutex
) int32 {
302 return libcCall(unsafe
.Pointer(funcPC(pthread_cond_wait_trampoline
)), unsafe
.Pointer(&c
))
304 func pthread_cond_wait_trampoline()
308 func pthread_cond_timedwait_relative_np(c
*pthreadcond
, m
*pthreadmutex
, t
*timespec
) int32 {
309 return libcCall(unsafe
.Pointer(funcPC(pthread_cond_timedwait_relative_np_trampoline
)), unsafe
.Pointer(&c
))
311 func pthread_cond_timedwait_relative_np_trampoline()
315 func pthread_cond_signal(c
*pthreadcond
) int32 {
316 return libcCall(unsafe
.Pointer(funcPC(pthread_cond_signal_trampoline
)), unsafe
.Pointer(&c
))
318 func pthread_cond_signal_trampoline()
320 // Not used on Darwin, but must be defined.
321 func exitThread(wait
*uint32) {
325 func closeonexec(fd
int32) {
326 fcntl(fd
, _F_SETFD
, _FD_CLOEXEC
)
329 // Tell the linker that the libc_* functions are to be found
330 // in a system library, with the libc_ prefix missing.
332 //go:cgo_import_dynamic libc_pthread_attr_init pthread_attr_init "/usr/lib/libSystem.B.dylib"
333 //go:cgo_import_dynamic libc_pthread_attr_setstacksize pthread_attr_setstacksize "/usr/lib/libSystem.B.dylib"
334 //go:cgo_import_dynamic libc_pthread_attr_setdetachstate pthread_attr_setdetachstate "/usr/lib/libSystem.B.dylib"
335 //go:cgo_import_dynamic libc_pthread_create pthread_create "/usr/lib/libSystem.B.dylib"
336 //go:cgo_import_dynamic libc_exit exit "/usr/lib/libSystem.B.dylib"
337 //go:cgo_import_dynamic libc_raise raise "/usr/lib/libSystem.B.dylib"
339 //go:cgo_import_dynamic libc_open open "/usr/lib/libSystem.B.dylib"
340 //go:cgo_import_dynamic libc_close close "/usr/lib/libSystem.B.dylib"
341 //go:cgo_import_dynamic libc_read read "/usr/lib/libSystem.B.dylib"
342 //go:cgo_import_dynamic libc_write write "/usr/lib/libSystem.B.dylib"
344 //go:cgo_import_dynamic libc_mmap mmap "/usr/lib/libSystem.B.dylib"
345 //go:cgo_import_dynamic libc_munmap munmap "/usr/lib/libSystem.B.dylib"
346 //go:cgo_import_dynamic libc_madvise madvise "/usr/lib/libSystem.B.dylib"
347 //go:cgo_import_dynamic libc_error __error "/usr/lib/libSystem.B.dylib"
348 //go:cgo_import_dynamic libc_usleep usleep "/usr/lib/libSystem.B.dylib"
350 //go:cgo_import_dynamic libc_mach_timebase_info mach_timebase_info "/usr/lib/libSystem.B.dylib"
351 //go:cgo_import_dynamic libc_mach_absolute_time mach_absolute_time "/usr/lib/libSystem.B.dylib"
352 //go:cgo_import_dynamic libc_gettimeofday gettimeofday "/usr/lib/libSystem.B.dylib"
353 //go:cgo_import_dynamic libc_sigaction sigaction "/usr/lib/libSystem.B.dylib"
354 //go:cgo_import_dynamic libc_pthread_sigmask pthread_sigmask "/usr/lib/libSystem.B.dylib"
355 //go:cgo_import_dynamic libc_sigaltstack sigaltstack "/usr/lib/libSystem.B.dylib"
356 //go:cgo_import_dynamic libc_getpid getpid "/usr/lib/libSystem.B.dylib"
357 //go:cgo_import_dynamic libc_kill kill "/usr/lib/libSystem.B.dylib"
358 //go:cgo_import_dynamic libc_setitimer setitimer "/usr/lib/libSystem.B.dylib"
359 //go:cgo_import_dynamic libc_sysctl sysctl "/usr/lib/libSystem.B.dylib"
360 //go:cgo_import_dynamic libc_fcntl fcntl "/usr/lib/libSystem.B.dylib"
361 //go:cgo_import_dynamic libc_kqueue kqueue "/usr/lib/libSystem.B.dylib"
362 //go:cgo_import_dynamic libc_kevent kevent "/usr/lib/libSystem.B.dylib"
364 //go:cgo_import_dynamic libc_pthread_mutex_init pthread_mutex_init "/usr/lib/libSystem.B.dylib"
365 //go:cgo_import_dynamic libc_pthread_mutex_lock pthread_mutex_lock "/usr/lib/libSystem.B.dylib"
366 //go:cgo_import_dynamic libc_pthread_mutex_unlock pthread_mutex_unlock "/usr/lib/libSystem.B.dylib"
367 //go:cgo_import_dynamic libc_pthread_cond_init pthread_cond_init "/usr/lib/libSystem.B.dylib"
368 //go:cgo_import_dynamic libc_pthread_cond_wait pthread_cond_wait "/usr/lib/libSystem.B.dylib"
369 //go:cgo_import_dynamic libc_pthread_cond_timedwait_relative_np pthread_cond_timedwait_relative_np "/usr/lib/libSystem.B.dylib"
370 //go:cgo_import_dynamic libc_pthread_cond_signal pthread_cond_signal "/usr/lib/libSystem.B.dylib"
372 // Magic incantation to get libSystem actually dynamically linked.
373 // TODO: Why does the code require this? See cmd/compile/internal/ld/go.go:210
374 //go:cgo_import_dynamic _ _ "/usr/lib/libSystem.B.dylib"