...

Source file src/runtime/sys_darwin.go

     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.
     4	
     5	package runtime
     6	
     7	import "unsafe"
     8	
     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.
    13	//go:nosplit
    14	func libcCall(fn, arg unsafe.Pointer) int32 {
    15		// Leave caller's PC/SP/G around for traceback.
    16		gp := getg()
    17		var mp *m
    18		if gp != nil {
    19			mp = gp.m
    20		}
    21		if mp != nil && mp.libcallsp == 0 {
    22			mp.libcallg.set(gp)
    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()
    27		} else {
    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
    39			// libcallsp.
    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.
    44			mp = nil
    45		}
    46		res := asmcgocall(fn, arg)
    47		if mp != nil {
    48			mp.libcallsp = 0
    49		}
    50		return res
    51	}
    52	
    53	// The X versions of syscall expect the libc call to return a 64-bit result.
    54	// Otherwise (the non-X version) expects a 32-bit result.
    55	// This distinction is required because an error is indicated by returning -1,
    56	// and we need to know whether to check 32 or 64 bits of the result.
    57	// (Some libc functions that return 32 bits put junk in the upper 32 bits of AX.)
    58	
    59	//go:linkname syscall_syscall syscall.syscall
    60	//go:nosplit
    61	//go:cgo_unsafe_args
    62	func syscall_syscall(fn, a1, a2, a3 uintptr) (r1, r2, err uintptr) {
    63		entersyscall()
    64		libcCall(unsafe.Pointer(funcPC(syscall)), unsafe.Pointer(&fn))
    65		exitsyscall()
    66		return
    67	}
    68	func syscall()
    69	
    70	//go:linkname syscall_syscall6 syscall.syscall6
    71	//go:nosplit
    72	//go:cgo_unsafe_args
    73	func syscall_syscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr) {
    74		entersyscall()
    75		libcCall(unsafe.Pointer(funcPC(syscall6)), unsafe.Pointer(&fn))
    76		exitsyscall()
    77		return
    78	}
    79	func syscall6()
    80	
    81	//go:linkname syscall_syscall6X syscall.syscall6X
    82	//go:nosplit
    83	//go:cgo_unsafe_args
    84	func syscall_syscall6X(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr) {
    85		entersyscall()
    86		libcCall(unsafe.Pointer(funcPC(syscall6X)), unsafe.Pointer(&fn))
    87		exitsyscall()
    88		return
    89	}
    90	func syscall6X()
    91	
    92	//go:linkname syscall_syscallPtr syscall.syscallPtr
    93	//go:nosplit
    94	//go:cgo_unsafe_args
    95	func syscall_syscallPtr(fn, a1, a2, a3 uintptr) (r1, r2, err uintptr) {
    96		entersyscall()
    97		libcCall(unsafe.Pointer(funcPC(syscallPtr)), unsafe.Pointer(&fn))
    98		exitsyscall()
    99		return
   100	}
   101	func syscallPtr()
   102	
   103	//go:linkname syscall_rawSyscall syscall.rawSyscall
   104	//go:nosplit
   105	//go:cgo_unsafe_args
   106	func syscall_rawSyscall(fn, a1, a2, a3 uintptr) (r1, r2, err uintptr) {
   107		libcCall(unsafe.Pointer(funcPC(syscall)), unsafe.Pointer(&fn))
   108		return
   109	}
   110	
   111	//go:linkname syscall_rawSyscall6 syscall.rawSyscall6
   112	//go:nosplit
   113	//go:cgo_unsafe_args
   114	func syscall_rawSyscall6(fn, a1, a2, a3, a4, a5, a6 uintptr) (r1, r2, err uintptr) {
   115		libcCall(unsafe.Pointer(funcPC(syscall6)), unsafe.Pointer(&fn))
   116		return
   117	}
   118	
   119	// The *_trampoline functions convert from the Go calling convention to the C calling convention
   120	// and then call the underlying libc function.  They are defined in sys_darwin_$ARCH.s.
   121	
   122	//go:nosplit
   123	//go:cgo_unsafe_args
   124	func pthread_attr_init(attr *pthreadattr) int32 {
   125		return libcCall(unsafe.Pointer(funcPC(pthread_attr_init_trampoline)), unsafe.Pointer(&attr))
   126	}
   127	func pthread_attr_init_trampoline()
   128	
   129	//go:nosplit
   130	//go:cgo_unsafe_args
   131	func pthread_attr_getstacksize(attr *pthreadattr, size *uintptr) int32 {
   132		return libcCall(unsafe.Pointer(funcPC(pthread_attr_getstacksize_trampoline)), unsafe.Pointer(&attr))
   133	}
   134	func pthread_attr_getstacksize_trampoline()
   135	
   136	//go:nosplit
   137	//go:cgo_unsafe_args
   138	func pthread_attr_setdetachstate(attr *pthreadattr, state int) int32 {
   139		return libcCall(unsafe.Pointer(funcPC(pthread_attr_setdetachstate_trampoline)), unsafe.Pointer(&attr))
   140	}
   141	func pthread_attr_setdetachstate_trampoline()
   142	
   143	//go:nosplit
   144	//go:cgo_unsafe_args
   145	func pthread_create(attr *pthreadattr, start uintptr, arg unsafe.Pointer) int32 {
   146		return libcCall(unsafe.Pointer(funcPC(pthread_create_trampoline)), unsafe.Pointer(&attr))
   147	}
   148	func pthread_create_trampoline()
   149	
   150	//go:nosplit
   151	//go:cgo_unsafe_args
   152	func raise(sig uint32) {
   153		libcCall(unsafe.Pointer(funcPC(raise_trampoline)), unsafe.Pointer(&sig))
   154	}
   155	func raise_trampoline()
   156	
   157	//go:nosplit
   158	//go:cgo_unsafe_args
   159	func pthread_self() (t pthread) {
   160		libcCall(unsafe.Pointer(funcPC(pthread_self_trampoline)), unsafe.Pointer(&t))
   161		return
   162	}
   163	func pthread_self_trampoline()
   164	
   165	func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (unsafe.Pointer, int) {
   166		args := struct {
   167			addr            unsafe.Pointer
   168			n               uintptr
   169			prot, flags, fd int32
   170			off             uint32
   171			ret1            unsafe.Pointer
   172			ret2            int
   173		}{addr, n, prot, flags, fd, off, nil, 0}
   174		libcCall(unsafe.Pointer(funcPC(mmap_trampoline)), unsafe.Pointer(&args))
   175		return args.ret1, args.ret2
   176	}
   177	func mmap_trampoline()
   178	
   179	//go:nosplit
   180	//go:cgo_unsafe_args
   181	func munmap(addr unsafe.Pointer, n uintptr) {
   182		libcCall(unsafe.Pointer(funcPC(munmap_trampoline)), unsafe.Pointer(&addr))
   183	}
   184	func munmap_trampoline()
   185	
   186	//go:nosplit
   187	//go:cgo_unsafe_args
   188	func madvise(addr unsafe.Pointer, n uintptr, flags int32) {
   189		libcCall(unsafe.Pointer(funcPC(madvise_trampoline)), unsafe.Pointer(&addr))
   190	}
   191	func madvise_trampoline()
   192	
   193	//go:nosplit
   194	//go:cgo_unsafe_args
   195	func read(fd int32, p unsafe.Pointer, n int32) int32 {
   196		return libcCall(unsafe.Pointer(funcPC(read_trampoline)), unsafe.Pointer(&fd))
   197	}
   198	func read_trampoline()
   199	
   200	func pipe() (r, w int32, errno int32) {
   201		var p [2]int32
   202		errno = libcCall(unsafe.Pointer(funcPC(pipe_trampoline)), noescape(unsafe.Pointer(&p)))
   203		return p[0], p[1], errno
   204	}
   205	func pipe_trampoline()
   206	
   207	//go:nosplit
   208	//go:cgo_unsafe_args
   209	func closefd(fd int32) int32 {
   210		return libcCall(unsafe.Pointer(funcPC(close_trampoline)), unsafe.Pointer(&fd))
   211	}
   212	func close_trampoline()
   213	
   214	//go:nosplit
   215	//go:cgo_unsafe_args
   216	//
   217	// This is exported via linkname to assembly in runtime/cgo.
   218	//go:linkname exit
   219	func exit(code int32) {
   220		libcCall(unsafe.Pointer(funcPC(exit_trampoline)), unsafe.Pointer(&code))
   221	}
   222	func exit_trampoline()
   223	
   224	//go:nosplit
   225	//go:cgo_unsafe_args
   226	func usleep(usec uint32) {
   227		libcCall(unsafe.Pointer(funcPC(usleep_trampoline)), unsafe.Pointer(&usec))
   228	}
   229	func usleep_trampoline()
   230	
   231	//go:nosplit
   232	//go:cgo_unsafe_args
   233	func write(fd uintptr, p unsafe.Pointer, n int32) int32 {
   234		return libcCall(unsafe.Pointer(funcPC(write_trampoline)), unsafe.Pointer(&fd))
   235	}
   236	func write_trampoline()
   237	
   238	//go:nosplit
   239	//go:cgo_unsafe_args
   240	func open(name *byte, mode, perm int32) (ret int32) {
   241		return libcCall(unsafe.Pointer(funcPC(open_trampoline)), unsafe.Pointer(&name))
   242	}
   243	func open_trampoline()
   244	
   245	//go:nosplit
   246	//go:cgo_unsafe_args
   247	func nanotime() int64 {
   248		var r struct {
   249			t            int64  // raw timer
   250			numer, denom uint32 // conversion factors. nanoseconds = t * numer / denom.
   251		}
   252		libcCall(unsafe.Pointer(funcPC(nanotime_trampoline)), unsafe.Pointer(&r))
   253		// Note: Apple seems unconcerned about overflow here. See
   254		// https://developer.apple.com/library/content/qa/qa1398/_index.html
   255		// Note also, numer == denom == 1 is common.
   256		t := r.t
   257		if r.numer != 1 {
   258			t *= int64(r.numer)
   259		}
   260		if r.denom != 1 {
   261			t /= int64(r.denom)
   262		}
   263		return t
   264	}
   265	func nanotime_trampoline()
   266	
   267	//go:nosplit
   268	//go:cgo_unsafe_args
   269	func walltime() (int64, int32) {
   270		var t timeval
   271		libcCall(unsafe.Pointer(funcPC(walltime_trampoline)), unsafe.Pointer(&t))
   272		return int64(t.tv_sec), 1000 * t.tv_usec
   273	}
   274	func walltime_trampoline()
   275	
   276	//go:nosplit
   277	//go:cgo_unsafe_args
   278	func sigaction(sig uint32, new *usigactiont, old *usigactiont) {
   279		libcCall(unsafe.Pointer(funcPC(sigaction_trampoline)), unsafe.Pointer(&sig))
   280	}
   281	func sigaction_trampoline()
   282	
   283	//go:nosplit
   284	//go:cgo_unsafe_args
   285	func sigprocmask(how uint32, new *sigset, old *sigset) {
   286		libcCall(unsafe.Pointer(funcPC(sigprocmask_trampoline)), unsafe.Pointer(&how))
   287	}
   288	func sigprocmask_trampoline()
   289	
   290	//go:nosplit
   291	//go:cgo_unsafe_args
   292	func sigaltstack(new *stackt, old *stackt) {
   293		if new != nil && new.ss_flags&_SS_DISABLE != 0 && new.ss_size == 0 {
   294			// Despite the fact that Darwin's sigaltstack man page says it ignores the size
   295			// when SS_DISABLE is set, it doesn't. sigaltstack returns ENOMEM
   296			// if we don't give it a reasonable size.
   297			// ref: http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20140421/214296.html
   298			new.ss_size = 32768
   299		}
   300		libcCall(unsafe.Pointer(funcPC(sigaltstack_trampoline)), unsafe.Pointer(&new))
   301	}
   302	func sigaltstack_trampoline()
   303	
   304	//go:nosplit
   305	//go:cgo_unsafe_args
   306	func raiseproc(sig uint32) {
   307		libcCall(unsafe.Pointer(funcPC(raiseproc_trampoline)), unsafe.Pointer(&sig))
   308	}
   309	func raiseproc_trampoline()
   310	
   311	//go:nosplit
   312	//go:cgo_unsafe_args
   313	func setitimer(mode int32, new, old *itimerval) {
   314		libcCall(unsafe.Pointer(funcPC(setitimer_trampoline)), unsafe.Pointer(&mode))
   315	}
   316	func setitimer_trampoline()
   317	
   318	//go:nosplit
   319	//go:cgo_unsafe_args
   320	func sysctl(mib *uint32, miblen uint32, out *byte, size *uintptr, dst *byte, ndst uintptr) int32 {
   321		return libcCall(unsafe.Pointer(funcPC(sysctl_trampoline)), unsafe.Pointer(&mib))
   322	}
   323	func sysctl_trampoline()
   324	
   325	//go:nosplit
   326	//go:cgo_unsafe_args
   327	func fcntl(fd, cmd, arg int32) int32 {
   328		return libcCall(unsafe.Pointer(funcPC(fcntl_trampoline)), unsafe.Pointer(&fd))
   329	}
   330	func fcntl_trampoline()
   331	
   332	//go:nosplit
   333	//go:cgo_unsafe_args
   334	func kqueue() int32 {
   335		v := libcCall(unsafe.Pointer(funcPC(kqueue_trampoline)), nil)
   336		return v
   337	}
   338	func kqueue_trampoline()
   339	
   340	//go:nosplit
   341	//go:cgo_unsafe_args
   342	func kevent(kq int32, ch *keventt, nch int32, ev *keventt, nev int32, ts *timespec) int32 {
   343		return libcCall(unsafe.Pointer(funcPC(kevent_trampoline)), unsafe.Pointer(&kq))
   344	}
   345	func kevent_trampoline()
   346	
   347	//go:nosplit
   348	//go:cgo_unsafe_args
   349	func pthread_mutex_init(m *pthreadmutex, attr *pthreadmutexattr) int32 {
   350		return libcCall(unsafe.Pointer(funcPC(pthread_mutex_init_trampoline)), unsafe.Pointer(&m))
   351	}
   352	func pthread_mutex_init_trampoline()
   353	
   354	//go:nosplit
   355	//go:cgo_unsafe_args
   356	func pthread_mutex_lock(m *pthreadmutex) int32 {
   357		return libcCall(unsafe.Pointer(funcPC(pthread_mutex_lock_trampoline)), unsafe.Pointer(&m))
   358	}
   359	func pthread_mutex_lock_trampoline()
   360	
   361	//go:nosplit
   362	//go:cgo_unsafe_args
   363	func pthread_mutex_unlock(m *pthreadmutex) int32 {
   364		return libcCall(unsafe.Pointer(funcPC(pthread_mutex_unlock_trampoline)), unsafe.Pointer(&m))
   365	}
   366	func pthread_mutex_unlock_trampoline()
   367	
   368	//go:nosplit
   369	//go:cgo_unsafe_args
   370	func pthread_cond_init(c *pthreadcond, attr *pthreadcondattr) int32 {
   371		return libcCall(unsafe.Pointer(funcPC(pthread_cond_init_trampoline)), unsafe.Pointer(&c))
   372	}
   373	func pthread_cond_init_trampoline()
   374	
   375	//go:nosplit
   376	//go:cgo_unsafe_args
   377	func pthread_cond_wait(c *pthreadcond, m *pthreadmutex) int32 {
   378		return libcCall(unsafe.Pointer(funcPC(pthread_cond_wait_trampoline)), unsafe.Pointer(&c))
   379	}
   380	func pthread_cond_wait_trampoline()
   381	
   382	//go:nosplit
   383	//go:cgo_unsafe_args
   384	func pthread_cond_timedwait_relative_np(c *pthreadcond, m *pthreadmutex, t *timespec) int32 {
   385		return libcCall(unsafe.Pointer(funcPC(pthread_cond_timedwait_relative_np_trampoline)), unsafe.Pointer(&c))
   386	}
   387	func pthread_cond_timedwait_relative_np_trampoline()
   388	
   389	//go:nosplit
   390	//go:cgo_unsafe_args
   391	func pthread_cond_signal(c *pthreadcond) int32 {
   392		return libcCall(unsafe.Pointer(funcPC(pthread_cond_signal_trampoline)), unsafe.Pointer(&c))
   393	}
   394	func pthread_cond_signal_trampoline()
   395	
   396	// Not used on Darwin, but must be defined.
   397	func exitThread(wait *uint32) {
   398	}
   399	
   400	//go:nosplit
   401	func closeonexec(fd int32) {
   402		fcntl(fd, _F_SETFD, _FD_CLOEXEC)
   403	}
   404	
   405	//go:nosplit
   406	func setNonblock(fd int32) {
   407		flags := fcntl(fd, _F_GETFL, 0)
   408		fcntl(fd, _F_SETFL, flags|_O_NONBLOCK)
   409	}
   410	
   411	// Tell the linker that the libc_* functions are to be found
   412	// in a system library, with the libc_ prefix missing.
   413	
   414	//go:cgo_import_dynamic libc_pthread_attr_init pthread_attr_init "/usr/lib/libSystem.B.dylib"
   415	//go:cgo_import_dynamic libc_pthread_attr_getstacksize pthread_attr_getstacksize "/usr/lib/libSystem.B.dylib"
   416	//go:cgo_import_dynamic libc_pthread_attr_setdetachstate pthread_attr_setdetachstate "/usr/lib/libSystem.B.dylib"
   417	//go:cgo_import_dynamic libc_pthread_create pthread_create "/usr/lib/libSystem.B.dylib"
   418	//go:cgo_import_dynamic libc_exit exit "/usr/lib/libSystem.B.dylib"
   419	//go:cgo_import_dynamic libc_raise raise "/usr/lib/libSystem.B.dylib"
   420	
   421	//go:cgo_import_dynamic libc_open open "/usr/lib/libSystem.B.dylib"
   422	//go:cgo_import_dynamic libc_close close "/usr/lib/libSystem.B.dylib"
   423	//go:cgo_import_dynamic libc_read read "/usr/lib/libSystem.B.dylib"
   424	//go:cgo_import_dynamic libc_write write "/usr/lib/libSystem.B.dylib"
   425	//go:cgo_import_dynamic libc_pipe pipe "/usr/lib/libSystem.B.dylib"
   426	
   427	//go:cgo_import_dynamic libc_mmap mmap "/usr/lib/libSystem.B.dylib"
   428	//go:cgo_import_dynamic libc_munmap munmap "/usr/lib/libSystem.B.dylib"
   429	//go:cgo_import_dynamic libc_madvise madvise "/usr/lib/libSystem.B.dylib"
   430	//go:cgo_import_dynamic libc_error __error "/usr/lib/libSystem.B.dylib"
   431	//go:cgo_import_dynamic libc_usleep usleep "/usr/lib/libSystem.B.dylib"
   432	
   433	//go:cgo_import_dynamic libc_mach_timebase_info mach_timebase_info "/usr/lib/libSystem.B.dylib"
   434	//go:cgo_import_dynamic libc_mach_absolute_time mach_absolute_time "/usr/lib/libSystem.B.dylib"
   435	//go:cgo_import_dynamic libc_gettimeofday gettimeofday "/usr/lib/libSystem.B.dylib"
   436	//go:cgo_import_dynamic libc_sigaction sigaction "/usr/lib/libSystem.B.dylib"
   437	//go:cgo_import_dynamic libc_pthread_sigmask pthread_sigmask "/usr/lib/libSystem.B.dylib"
   438	//go:cgo_import_dynamic libc_sigaltstack sigaltstack "/usr/lib/libSystem.B.dylib"
   439	//go:cgo_import_dynamic libc_getpid getpid "/usr/lib/libSystem.B.dylib"
   440	//go:cgo_import_dynamic libc_kill kill "/usr/lib/libSystem.B.dylib"
   441	//go:cgo_import_dynamic libc_setitimer setitimer "/usr/lib/libSystem.B.dylib"
   442	//go:cgo_import_dynamic libc_sysctl sysctl "/usr/lib/libSystem.B.dylib"
   443	//go:cgo_import_dynamic libc_fcntl fcntl "/usr/lib/libSystem.B.dylib"
   444	//go:cgo_import_dynamic libc_kqueue kqueue "/usr/lib/libSystem.B.dylib"
   445	//go:cgo_import_dynamic libc_kevent kevent "/usr/lib/libSystem.B.dylib"
   446	
   447	//go:cgo_import_dynamic libc_pthread_mutex_init pthread_mutex_init "/usr/lib/libSystem.B.dylib"
   448	//go:cgo_import_dynamic libc_pthread_mutex_lock pthread_mutex_lock "/usr/lib/libSystem.B.dylib"
   449	//go:cgo_import_dynamic libc_pthread_mutex_unlock pthread_mutex_unlock "/usr/lib/libSystem.B.dylib"
   450	//go:cgo_import_dynamic libc_pthread_cond_init pthread_cond_init "/usr/lib/libSystem.B.dylib"
   451	//go:cgo_import_dynamic libc_pthread_cond_wait pthread_cond_wait "/usr/lib/libSystem.B.dylib"
   452	//go:cgo_import_dynamic libc_pthread_cond_timedwait_relative_np pthread_cond_timedwait_relative_np "/usr/lib/libSystem.B.dylib"
   453	//go:cgo_import_dynamic libc_pthread_cond_signal pthread_cond_signal "/usr/lib/libSystem.B.dylib"
   454	
   455	// Magic incantation to get libSystem actually dynamically linked.
   456	// TODO: Why does the code require this?  See cmd/link/internal/ld/go.go
   457	//go:cgo_import_dynamic _ _ "/usr/lib/libSystem.B.dylib"
   458	

View as plain text