2018-04-23 08:30:32 -06:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
|
|
|
import "unsafe"
|
|
|
|
|
2018-05-26 12:29:55 -06:00
|
|
|
// Call fn with arg as its argument. Return what fn returns.
|
|
|
|
// fn is the raw pc value of the entry point of the desired function.
|
|
|
|
// Switches to the system stack, if not already there.
|
|
|
|
// Preserves the calling point as the location where a profiler traceback will begin.
|
|
|
|
//go:nosplit
|
|
|
|
func libcCall(fn, arg unsafe.Pointer) int32 {
|
|
|
|
// Leave caller's PC/SP/G around for traceback.
|
|
|
|
gp := getg()
|
|
|
|
var mp *m
|
|
|
|
if gp != nil {
|
|
|
|
mp = gp.m
|
|
|
|
}
|
|
|
|
if mp != nil {
|
|
|
|
mp.libcallg.set(gp)
|
|
|
|
mp.libcallpc = getcallerpc()
|
|
|
|
// sp must be the last, because once async cpu profiler finds
|
|
|
|
// all three values to be non-zero, it will use them
|
|
|
|
mp.libcallsp = getcallersp()
|
|
|
|
}
|
|
|
|
res := asmcgocall(fn, arg)
|
|
|
|
if mp != nil {
|
|
|
|
mp.libcallsp = 0
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-04-23 08:30:32 -06:00
|
|
|
// The *_trampoline functions convert from the Go calling convention to the C calling convention
|
|
|
|
// and then call the underlying libc function. They are defined in sys_darwin_$ARCH.s.
|
|
|
|
|
2018-05-03 11:30:31 -06:00
|
|
|
//go:nosplit
|
|
|
|
//go:cgo_unsafe_args
|
|
|
|
func pthread_attr_init(attr *pthreadattr) int32 {
|
2018-05-26 12:29:55 -06:00
|
|
|
return libcCall(unsafe.Pointer(funcPC(pthread_attr_init_trampoline)), unsafe.Pointer(&attr))
|
2018-05-03 11:30:31 -06:00
|
|
|
}
|
|
|
|
func pthread_attr_init_trampoline()
|
|
|
|
|
|
|
|
//go:nosplit
|
|
|
|
//go:cgo_unsafe_args
|
|
|
|
func pthread_attr_setstacksize(attr *pthreadattr, size uintptr) int32 {
|
2018-05-26 12:29:55 -06:00
|
|
|
return libcCall(unsafe.Pointer(funcPC(pthread_attr_setstacksize_trampoline)), unsafe.Pointer(&attr))
|
2018-04-23 08:30:32 -06:00
|
|
|
}
|
2018-05-03 11:30:31 -06:00
|
|
|
func pthread_attr_setstacksize_trampoline()
|
2018-04-23 08:30:32 -06:00
|
|
|
|
2018-05-03 11:30:31 -06:00
|
|
|
//go:nosplit
|
|
|
|
//go:cgo_unsafe_args
|
|
|
|
func pthread_attr_setdetachstate(attr *pthreadattr, state int) int32 {
|
2018-05-26 12:29:55 -06:00
|
|
|
return libcCall(unsafe.Pointer(funcPC(pthread_attr_setdetachstate_trampoline)), unsafe.Pointer(&attr))
|
2018-05-03 11:30:31 -06:00
|
|
|
}
|
|
|
|
func pthread_attr_setdetachstate_trampoline()
|
2018-04-23 08:30:32 -06:00
|
|
|
|
2018-05-03 11:30:31 -06:00
|
|
|
//go:nosplit
|
|
|
|
//go:cgo_unsafe_args
|
|
|
|
func pthread_create(attr *pthreadattr, start uintptr, arg unsafe.Pointer) int32 {
|
2018-05-26 12:29:55 -06:00
|
|
|
return libcCall(unsafe.Pointer(funcPC(pthread_create_trampoline)), unsafe.Pointer(&attr))
|
2018-04-23 08:30:32 -06:00
|
|
|
}
|
2018-05-03 11:30:31 -06:00
|
|
|
func pthread_create_trampoline()
|
2018-04-23 08:30:32 -06:00
|
|
|
|
2018-05-03 11:30:31 -06:00
|
|
|
//go:nosplit
|
|
|
|
//go:cgo_unsafe_args
|
2018-05-21 07:33:20 -06:00
|
|
|
func raise(sig uint32) {
|
2018-05-26 12:29:55 -06:00
|
|
|
libcCall(unsafe.Pointer(funcPC(raise_trampoline)), unsafe.Pointer(&sig))
|
2018-05-03 11:30:31 -06:00
|
|
|
}
|
2018-05-21 07:33:20 -06:00
|
|
|
func raise_trampoline()
|
2018-04-23 08:30:32 -06:00
|
|
|
|
2018-05-03 11:30:31 -06:00
|
|
|
//go:nosplit
|
|
|
|
//go:cgo_unsafe_args
|
|
|
|
func pthread_self() (t pthread) {
|
2018-05-26 12:29:55 -06:00
|
|
|
libcCall(unsafe.Pointer(funcPC(pthread_self_trampoline)), unsafe.Pointer(&t))
|
2018-04-23 08:30:32 -06:00
|
|
|
return
|
|
|
|
}
|
2018-05-03 11:30:31 -06:00
|
|
|
func pthread_self_trampoline()
|
|
|
|
|
|
|
|
func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (unsafe.Pointer, int) {
|
|
|
|
args := struct {
|
|
|
|
addr unsafe.Pointer
|
|
|
|
n uintptr
|
|
|
|
prot, flags, fd int32
|
|
|
|
off uint32
|
|
|
|
ret1 unsafe.Pointer
|
|
|
|
ret2 int
|
|
|
|
}{addr, n, prot, flags, fd, off, nil, 0}
|
2018-05-26 12:29:55 -06:00
|
|
|
libcCall(unsafe.Pointer(funcPC(mmap_trampoline)), unsafe.Pointer(&args))
|
2018-05-03 11:30:31 -06:00
|
|
|
return args.ret1, args.ret2
|
|
|
|
}
|
|
|
|
func mmap_trampoline()
|
2018-04-23 08:30:32 -06:00
|
|
|
|
2018-05-03 11:30:31 -06:00
|
|
|
//go:nosplit
|
|
|
|
//go:cgo_unsafe_args
|
|
|
|
func munmap(addr unsafe.Pointer, n uintptr) {
|
2018-05-26 12:29:55 -06:00
|
|
|
libcCall(unsafe.Pointer(funcPC(munmap_trampoline)), unsafe.Pointer(&addr))
|
2018-05-03 11:30:31 -06:00
|
|
|
}
|
|
|
|
func munmap_trampoline()
|
2018-04-23 08:30:32 -06:00
|
|
|
|
2018-05-03 11:30:31 -06:00
|
|
|
//go:nosplit
|
|
|
|
//go:cgo_unsafe_args
|
|
|
|
func madvise(addr unsafe.Pointer, n uintptr, flags int32) {
|
2018-05-26 12:29:55 -06:00
|
|
|
libcCall(unsafe.Pointer(funcPC(madvise_trampoline)), unsafe.Pointer(&addr))
|
2018-04-23 08:30:32 -06:00
|
|
|
}
|
2018-05-03 11:30:31 -06:00
|
|
|
func madvise_trampoline()
|
2018-04-23 08:30:32 -06:00
|
|
|
|
2018-05-03 11:30:31 -06:00
|
|
|
//go:nosplit
|
|
|
|
//go:cgo_unsafe_args
|
|
|
|
func read(fd int32, p unsafe.Pointer, n int32) int32 {
|
2018-05-26 12:29:55 -06:00
|
|
|
return libcCall(unsafe.Pointer(funcPC(read_trampoline)), unsafe.Pointer(&fd))
|
2018-05-03 11:30:31 -06:00
|
|
|
}
|
|
|
|
func read_trampoline()
|
2018-04-23 08:30:32 -06:00
|
|
|
|
2018-05-03 11:30:31 -06:00
|
|
|
//go:nosplit
|
|
|
|
//go:cgo_unsafe_args
|
|
|
|
func closefd(fd int32) int32 {
|
2018-05-26 12:29:55 -06:00
|
|
|
return libcCall(unsafe.Pointer(funcPC(close_trampoline)), unsafe.Pointer(&fd))
|
2018-04-30 17:21:02 -06:00
|
|
|
}
|
2018-05-03 11:30:31 -06:00
|
|
|
func close_trampoline()
|
2018-04-30 17:21:02 -06:00
|
|
|
|
2018-05-03 11:30:31 -06:00
|
|
|
//go:nosplit
|
|
|
|
//go:cgo_unsafe_args
|
|
|
|
func exit(code int32) {
|
2018-05-26 12:29:55 -06:00
|
|
|
libcCall(unsafe.Pointer(funcPC(exit_trampoline)), unsafe.Pointer(&code))
|
2018-05-03 11:30:31 -06:00
|
|
|
}
|
|
|
|
func exit_trampoline()
|
2018-04-30 17:21:02 -06:00
|
|
|
|
2018-05-03 11:30:31 -06:00
|
|
|
//go:nosplit
|
|
|
|
//go:cgo_unsafe_args
|
|
|
|
func usleep(usec uint32) {
|
2018-05-26 12:29:55 -06:00
|
|
|
libcCall(unsafe.Pointer(funcPC(usleep_trampoline)), unsafe.Pointer(&usec))
|
2018-04-30 17:21:02 -06:00
|
|
|
}
|
2018-05-03 11:30:31 -06:00
|
|
|
func usleep_trampoline()
|
2018-04-30 17:21:02 -06:00
|
|
|
|
2018-05-03 11:30:31 -06:00
|
|
|
//go:nosplit
|
|
|
|
//go:cgo_unsafe_args
|
|
|
|
func write(fd uintptr, p unsafe.Pointer, n int32) int32 {
|
2018-05-26 12:29:55 -06:00
|
|
|
return libcCall(unsafe.Pointer(funcPC(write_trampoline)), unsafe.Pointer(&fd))
|
2018-05-03 11:30:31 -06:00
|
|
|
}
|
|
|
|
func write_trampoline()
|
|
|
|
|
|
|
|
//go:nosplit
|
|
|
|
//go:cgo_unsafe_args
|
|
|
|
func open(name *byte, mode, perm int32) (ret int32) {
|
2018-05-26 12:29:55 -06:00
|
|
|
return libcCall(unsafe.Pointer(funcPC(open_trampoline)), unsafe.Pointer(&name))
|
2018-05-03 11:30:31 -06:00
|
|
|
}
|
|
|
|
func open_trampoline()
|
|
|
|
|
2018-05-01 10:42:04 -06:00
|
|
|
//go:nosplit
|
|
|
|
//go:cgo_unsafe_args
|
|
|
|
func nanotime() int64 {
|
|
|
|
var r struct {
|
|
|
|
t int64 // raw timer
|
|
|
|
numer, denom uint32 // conversion factors. nanoseconds = t * numer / denom.
|
|
|
|
}
|
2018-05-26 12:29:55 -06:00
|
|
|
libcCall(unsafe.Pointer(funcPC(nanotime_trampoline)), unsafe.Pointer(&r))
|
2018-05-01 10:42:04 -06:00
|
|
|
// Note: Apple seems unconcerned about overflow here. See
|
|
|
|
// https://developer.apple.com/library/content/qa/qa1398/_index.html
|
|
|
|
// Note also, numer == denom == 1 is common.
|
|
|
|
t := r.t
|
|
|
|
if r.numer != 1 {
|
|
|
|
t *= int64(r.numer)
|
|
|
|
}
|
|
|
|
if r.denom != 1 {
|
|
|
|
t /= int64(r.denom)
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
func nanotime_trampoline()
|
|
|
|
|
2018-05-26 10:58:26 -06:00
|
|
|
//go:nosplit
|
|
|
|
//go:cgo_unsafe_args
|
|
|
|
func walltime() (int64, int32) {
|
|
|
|
var t timeval
|
|
|
|
libcCall(unsafe.Pointer(funcPC(walltime_trampoline)), unsafe.Pointer(&t))
|
|
|
|
return int64(t.tv_sec), 1000 * t.tv_usec
|
|
|
|
}
|
|
|
|
func walltime_trampoline()
|
|
|
|
|
2018-05-03 11:30:31 -06:00
|
|
|
// Not used on Darwin, but must be defined.
|
|
|
|
func exitThread(wait *uint32) {
|
|
|
|
}
|
2018-04-30 17:21:02 -06:00
|
|
|
|
2018-04-23 08:30:32 -06:00
|
|
|
// Tell the linker that the libc_* functions are to be found
|
|
|
|
// in a system library, with the libc_ prefix missing.
|
|
|
|
|
|
|
|
//go:cgo_import_dynamic libc_pthread_attr_init pthread_attr_init "/usr/lib/libSystem.B.dylib"
|
2018-04-30 08:50:10 -06:00
|
|
|
//go:cgo_import_dynamic libc_pthread_attr_setstacksize pthread_attr_setstacksize "/usr/lib/libSystem.B.dylib"
|
2018-04-23 08:30:32 -06:00
|
|
|
//go:cgo_import_dynamic libc_pthread_attr_setdetachstate pthread_attr_setdetachstate "/usr/lib/libSystem.B.dylib"
|
|
|
|
//go:cgo_import_dynamic libc_pthread_create pthread_create "/usr/lib/libSystem.B.dylib"
|
2018-04-30 08:50:10 -06:00
|
|
|
//go:cgo_import_dynamic libc_exit exit "/usr/lib/libSystem.B.dylib"
|
2018-05-21 07:33:20 -06:00
|
|
|
//go:cgo_import_dynamic libc_raise raise "/usr/lib/libSystem.B.dylib"
|
2018-04-23 08:30:32 -06:00
|
|
|
|
2018-04-30 20:55:41 -06:00
|
|
|
//go:cgo_import_dynamic libc_open open "/usr/lib/libSystem.B.dylib"
|
|
|
|
//go:cgo_import_dynamic libc_close close "/usr/lib/libSystem.B.dylib"
|
|
|
|
//go:cgo_import_dynamic libc_read read "/usr/lib/libSystem.B.dylib"
|
|
|
|
//go:cgo_import_dynamic libc_write write "/usr/lib/libSystem.B.dylib"
|
|
|
|
|
2018-04-30 20:25:46 -06:00
|
|
|
//go:cgo_import_dynamic libc_mmap mmap "/usr/lib/libSystem.B.dylib"
|
|
|
|
//go:cgo_import_dynamic libc_munmap munmap "/usr/lib/libSystem.B.dylib"
|
|
|
|
//go:cgo_import_dynamic libc_madvise madvise "/usr/lib/libSystem.B.dylib"
|
|
|
|
//go:cgo_import_dynamic libc_error __error "/usr/lib/libSystem.B.dylib"
|
|
|
|
//go:cgo_import_dynamic libc_usleep usleep "/usr/lib/libSystem.B.dylib"
|
|
|
|
|
2018-05-01 10:42:04 -06:00
|
|
|
//go:cgo_import_dynamic libc_mach_timebase_info mach_timebase_info "/usr/lib/libSystem.B.dylib"
|
|
|
|
//go:cgo_import_dynamic libc_mach_absolute_time mach_absolute_time "/usr/lib/libSystem.B.dylib"
|
2018-05-26 10:58:26 -06:00
|
|
|
//go:cgo_import_dynamic libc_gettimeofday gettimeofday "/usr/lib/libSystem.B.dylib"
|
2018-05-01 10:42:04 -06:00
|
|
|
|
2018-04-23 08:30:32 -06:00
|
|
|
// Magic incantation to get libSystem actually dynamically linked.
|
|
|
|
// TODO: Why does the code require this? See cmd/compile/internal/ld/go.go:210
|
|
|
|
//go:cgo_import_dynamic _ _ "/usr/lib/libSystem.B.dylib"
|