github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/runtime/os_darwin.go (about)

     1  // Copyright 2009 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 (
     8  	"internal/abi"
     9  	"unsafe"
    10  )
    11  
    12  type mOS struct {
    13  	initialized bool
    14  	mutex       pthreadmutex
    15  	cond        pthreadcond
    16  	count       int
    17  }
    18  
    19  func unimplemented(name string) {
    20  	println(name, "not implemented")
    21  	*(*int)(unsafe.Pointer(uintptr(1231))) = 1231
    22  }
    23  
    24  //go:nosplit
    25  func semacreate(mp *m) {
    26  	if mp.initialized {
    27  		return
    28  	}
    29  	mp.initialized = true
    30  	if err := pthread_mutex_init(&mp.mutex, nil); err != 0 {
    31  		throw("pthread_mutex_init")
    32  	}
    33  	if err := pthread_cond_init(&mp.cond, nil); err != 0 {
    34  		throw("pthread_cond_init")
    35  	}
    36  }
    37  
    38  //go:nosplit
    39  func semasleep(ns int64) int32 {
    40  	var start int64
    41  	if ns >= 0 {
    42  		start = nanotime()
    43  	}
    44  	mp := getg().m
    45  	pthread_mutex_lock(&mp.mutex)
    46  	for {
    47  		if mp.count > 0 {
    48  			mp.count--
    49  			pthread_mutex_unlock(&mp.mutex)
    50  			return 0
    51  		}
    52  		if ns >= 0 {
    53  			spent := nanotime() - start
    54  			if spent >= ns {
    55  				pthread_mutex_unlock(&mp.mutex)
    56  				return -1
    57  			}
    58  			var t timespec
    59  			t.setNsec(ns - spent)
    60  			err := pthread_cond_timedwait_relative_np(&mp.cond, &mp.mutex, &t)
    61  			if err == _ETIMEDOUT {
    62  				pthread_mutex_unlock(&mp.mutex)
    63  				return -1
    64  			}
    65  		} else {
    66  			pthread_cond_wait(&mp.cond, &mp.mutex)
    67  		}
    68  	}
    69  }
    70  
    71  //go:nosplit
    72  func semawakeup(mp *m) {
    73  	pthread_mutex_lock(&mp.mutex)
    74  	mp.count++
    75  	if mp.count > 0 {
    76  		pthread_cond_signal(&mp.cond)
    77  	}
    78  	pthread_mutex_unlock(&mp.mutex)
    79  }
    80  
    81  // The read and write file descriptors used by the sigNote functions.
    82  var sigNoteRead, sigNoteWrite int32
    83  
    84  // sigNoteSetup initializes an async-signal-safe note.
    85  //
    86  // The current implementation of notes on Darwin is not async-signal-safe,
    87  // because the functions pthread_mutex_lock, pthread_cond_signal, and
    88  // pthread_mutex_unlock, called by semawakeup, are not async-signal-safe.
    89  // There is only one case where we need to wake up a note from a signal
    90  // handler: the sigsend function. The signal handler code does not require
    91  // all the features of notes: it does not need to do a timed wait.
    92  // This is a separate implementation of notes, based on a pipe, that does
    93  // not support timed waits but is async-signal-safe.
    94  func sigNoteSetup(*note) {
    95  	if sigNoteRead != 0 || sigNoteWrite != 0 {
    96  		throw("duplicate sigNoteSetup")
    97  	}
    98  	var errno int32
    99  	sigNoteRead, sigNoteWrite, errno = pipe()
   100  	if errno != 0 {
   101  		throw("pipe failed")
   102  	}
   103  	closeonexec(sigNoteRead)
   104  	closeonexec(sigNoteWrite)
   105  
   106  	// Make the write end of the pipe non-blocking, so that if the pipe
   107  	// buffer is somehow full we will not block in the signal handler.
   108  	// Leave the read end of the pipe blocking so that we will block
   109  	// in sigNoteSleep.
   110  	setNonblock(sigNoteWrite)
   111  }
   112  
   113  // sigNoteWakeup wakes up a thread sleeping on a note created by sigNoteSetup.
   114  func sigNoteWakeup(*note) {
   115  	var b byte
   116  	write(uintptr(sigNoteWrite), unsafe.Pointer(&b), 1)
   117  }
   118  
   119  // sigNoteSleep waits for a note created by sigNoteSetup to be woken.
   120  func sigNoteSleep(*note) {
   121  	for {
   122  		var b byte
   123  		entersyscallblock()
   124  		n := read(sigNoteRead, unsafe.Pointer(&b), 1)
   125  		exitsyscall()
   126  		if n != -_EINTR {
   127  			return
   128  		}
   129  	}
   130  }
   131  
   132  // BSD interface for threading.
   133  func osinit() {
   134  	// pthread_create delayed until end of goenvs so that we
   135  	// can look at the environment first.
   136  
   137  	ncpu = getncpu()
   138  	physPageSize = getPageSize()
   139  
   140  	osinit_hack()
   141  }
   142  
   143  func sysctlbynameInt32(name []byte) (int32, int32) {
   144  	out := int32(0)
   145  	nout := unsafe.Sizeof(out)
   146  	ret := sysctlbyname(&name[0], (*byte)(unsafe.Pointer(&out)), &nout, nil, 0)
   147  	return ret, out
   148  }
   149  
   150  //go:linkname internal_cpu_getsysctlbyname internal/cpu.getsysctlbyname
   151  func internal_cpu_getsysctlbyname(name []byte) (int32, int32) {
   152  	return sysctlbynameInt32(name)
   153  }
   154  
   155  const (
   156  	_CTL_HW      = 6
   157  	_HW_NCPU     = 3
   158  	_HW_PAGESIZE = 7
   159  )
   160  
   161  func getncpu() int32 {
   162  	// Use sysctl to fetch hw.ncpu.
   163  	mib := [2]uint32{_CTL_HW, _HW_NCPU}
   164  	out := uint32(0)
   165  	nout := unsafe.Sizeof(out)
   166  	ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0)
   167  	if ret >= 0 && int32(out) > 0 {
   168  		return int32(out)
   169  	}
   170  	return 1
   171  }
   172  
   173  func getPageSize() uintptr {
   174  	// Use sysctl to fetch hw.pagesize.
   175  	mib := [2]uint32{_CTL_HW, _HW_PAGESIZE}
   176  	out := uint32(0)
   177  	nout := unsafe.Sizeof(out)
   178  	ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0)
   179  	if ret >= 0 && int32(out) > 0 {
   180  		return uintptr(out)
   181  	}
   182  	return 0
   183  }
   184  
   185  var urandom_dev = []byte("/dev/urandom\x00")
   186  
   187  //go:nosplit
   188  func getRandomData(r []byte) {
   189  	fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0)
   190  	n := read(fd, unsafe.Pointer(&r[0]), int32(len(r)))
   191  	closefd(fd)
   192  	extendRandom(r, int(n))
   193  }
   194  
   195  func goenvs() {
   196  	goenvs_unix()
   197  }
   198  
   199  // May run with m.p==nil, so write barriers are not allowed.
   200  //
   201  //go:nowritebarrierrec
   202  func newosproc(mp *m) {
   203  	stk := unsafe.Pointer(mp.g0.stack.hi)
   204  	if false {
   205  		print("newosproc stk=", stk, " m=", mp, " g=", mp.g0, " id=", mp.id, " ostk=", &mp, "\n")
   206  	}
   207  
   208  	// Initialize an attribute object.
   209  	var attr pthreadattr
   210  	var err int32
   211  	err = pthread_attr_init(&attr)
   212  	if err != 0 {
   213  		writeErrStr(failthreadcreate)
   214  		exit(1)
   215  	}
   216  
   217  	// Find out OS stack size for our own stack guard.
   218  	var stacksize uintptr
   219  	if pthread_attr_getstacksize(&attr, &stacksize) != 0 {
   220  		writeErrStr(failthreadcreate)
   221  		exit(1)
   222  	}
   223  	mp.g0.stack.hi = stacksize // for mstart
   224  
   225  	// Tell the pthread library we won't join with this thread.
   226  	if pthread_attr_setdetachstate(&attr, _PTHREAD_CREATE_DETACHED) != 0 {
   227  		writeErrStr(failthreadcreate)
   228  		exit(1)
   229  	}
   230  
   231  	// Finally, create the thread. It starts at mstart_stub, which does some low-level
   232  	// setup and then calls mstart.
   233  	var oset sigset
   234  	sigprocmask(_SIG_SETMASK, &sigset_all, &oset)
   235  	err = retryOnEAGAIN(func() int32 {
   236  		return pthread_create(&attr, abi.FuncPCABI0(mstart_stub), unsafe.Pointer(mp))
   237  	})
   238  	sigprocmask(_SIG_SETMASK, &oset, nil)
   239  	if err != 0 {
   240  		writeErrStr(failthreadcreate)
   241  		exit(1)
   242  	}
   243  }
   244  
   245  // glue code to call mstart from pthread_create.
   246  func mstart_stub()
   247  
   248  // newosproc0 is a version of newosproc that can be called before the runtime
   249  // is initialized.
   250  //
   251  // This function is not safe to use after initialization as it does not pass an M as fnarg.
   252  //
   253  //go:nosplit
   254  func newosproc0(stacksize uintptr, fn uintptr) {
   255  	// Initialize an attribute object.
   256  	var attr pthreadattr
   257  	var err int32
   258  	err = pthread_attr_init(&attr)
   259  	if err != 0 {
   260  		writeErrStr(failthreadcreate)
   261  		exit(1)
   262  	}
   263  
   264  	// The caller passes in a suggested stack size,
   265  	// from when we allocated the stack and thread ourselves,
   266  	// without libpthread. Now that we're using libpthread,
   267  	// we use the OS default stack size instead of the suggestion.
   268  	// Find out that stack size for our own stack guard.
   269  	if pthread_attr_getstacksize(&attr, &stacksize) != 0 {
   270  		writeErrStr(failthreadcreate)
   271  		exit(1)
   272  	}
   273  	g0.stack.hi = stacksize // for mstart
   274  	memstats.stacks_sys.add(int64(stacksize))
   275  
   276  	// Tell the pthread library we won't join with this thread.
   277  	if pthread_attr_setdetachstate(&attr, _PTHREAD_CREATE_DETACHED) != 0 {
   278  		writeErrStr(failthreadcreate)
   279  		exit(1)
   280  	}
   281  
   282  	// Finally, create the thread. It starts at mstart_stub, which does some low-level
   283  	// setup and then calls mstart.
   284  	var oset sigset
   285  	sigprocmask(_SIG_SETMASK, &sigset_all, &oset)
   286  	err = pthread_create(&attr, fn, nil)
   287  	sigprocmask(_SIG_SETMASK, &oset, nil)
   288  	if err != 0 {
   289  		writeErrStr(failthreadcreate)
   290  		exit(1)
   291  	}
   292  }
   293  
   294  // Called to do synchronous initialization of Go code built with
   295  // -buildmode=c-archive or -buildmode=c-shared.
   296  // None of the Go runtime is initialized.
   297  //
   298  //go:nosplit
   299  //go:nowritebarrierrec
   300  func libpreinit() {
   301  	initsig(true)
   302  }
   303  
   304  // Called to initialize a new m (including the bootstrap m).
   305  // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
   306  func mpreinit(mp *m) {
   307  	mp.gsignal = malg(32 * 1024) // OS X wants >= 8K
   308  	mp.gsignal.m = mp
   309  	if GOOS == "darwin" && GOARCH == "arm64" {
   310  		// mlock the signal stack to work around a kernel bug where it may
   311  		// SIGILL when the signal stack is not faulted in while a signal
   312  		// arrives. See issue 42774.
   313  		mlock(unsafe.Pointer(mp.gsignal.stack.hi-physPageSize), physPageSize)
   314  	}
   315  }
   316  
   317  // Called to initialize a new m (including the bootstrap m).
   318  // Called on the new thread, cannot allocate memory.
   319  func minit() {
   320  	// iOS does not support alternate signal stack.
   321  	// The signal handler handles it directly.
   322  	if !(GOOS == "ios" && GOARCH == "arm64") {
   323  		minitSignalStack()
   324  	}
   325  	minitSignalMask()
   326  	getg().m.procid = uint64(pthread_self())
   327  }
   328  
   329  // Called from dropm to undo the effect of an minit.
   330  //
   331  //go:nosplit
   332  func unminit() {
   333  	// iOS does not support alternate signal stack.
   334  	// See minit.
   335  	if !(GOOS == "ios" && GOARCH == "arm64") {
   336  		unminitSignals()
   337  	}
   338  }
   339  
   340  // Called from exitm, but not from drop, to undo the effect of thread-owned
   341  // resources in minit, semacreate, or elsewhere. Do not take locks after calling this.
   342  func mdestroy(mp *m) {
   343  }
   344  
   345  //go:nosplit
   346  func osyield_no_g() {
   347  	usleep_no_g(1)
   348  }
   349  
   350  //go:nosplit
   351  func osyield() {
   352  	usleep(1)
   353  }
   354  
   355  const (
   356  	_NSIG        = 32
   357  	_SI_USER     = 0 /* empirically true, but not what headers say */
   358  	_SIG_BLOCK   = 1
   359  	_SIG_UNBLOCK = 2
   360  	_SIG_SETMASK = 3
   361  	_SS_DISABLE  = 4
   362  )
   363  
   364  //extern SigTabTT runtimeĀ·sigtab[];
   365  
   366  type sigset uint32
   367  
   368  var sigset_all = ^sigset(0)
   369  
   370  //go:nosplit
   371  //go:nowritebarrierrec
   372  func setsig(i uint32, fn uintptr) {
   373  	var sa usigactiont
   374  	sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK | _SA_RESTART
   375  	sa.sa_mask = ^uint32(0)
   376  	if fn == abi.FuncPCABIInternal(sighandler) { // abi.FuncPCABIInternal(sighandler) matches the callers in signal_unix.go
   377  		if iscgo {
   378  			fn = abi.FuncPCABI0(cgoSigtramp)
   379  		} else {
   380  			fn = abi.FuncPCABI0(sigtramp)
   381  		}
   382  	}
   383  	*(*uintptr)(unsafe.Pointer(&sa.__sigaction_u)) = fn
   384  	sigaction(i, &sa, nil)
   385  }
   386  
   387  // sigtramp is the callback from libc when a signal is received.
   388  // It is called with the C calling convention.
   389  func sigtramp()
   390  func cgoSigtramp()
   391  
   392  //go:nosplit
   393  //go:nowritebarrierrec
   394  func setsigstack(i uint32) {
   395  	var osa usigactiont
   396  	sigaction(i, nil, &osa)
   397  	handler := *(*uintptr)(unsafe.Pointer(&osa.__sigaction_u))
   398  	if osa.sa_flags&_SA_ONSTACK != 0 {
   399  		return
   400  	}
   401  	var sa usigactiont
   402  	*(*uintptr)(unsafe.Pointer(&sa.__sigaction_u)) = handler
   403  	sa.sa_mask = osa.sa_mask
   404  	sa.sa_flags = osa.sa_flags | _SA_ONSTACK
   405  	sigaction(i, &sa, nil)
   406  }
   407  
   408  //go:nosplit
   409  //go:nowritebarrierrec
   410  func getsig(i uint32) uintptr {
   411  	var sa usigactiont
   412  	sigaction(i, nil, &sa)
   413  	return *(*uintptr)(unsafe.Pointer(&sa.__sigaction_u))
   414  }
   415  
   416  // setSignalstackSP sets the ss_sp field of a stackt.
   417  //
   418  //go:nosplit
   419  func setSignalstackSP(s *stackt, sp uintptr) {
   420  	*(*uintptr)(unsafe.Pointer(&s.ss_sp)) = sp
   421  }
   422  
   423  //go:nosplit
   424  //go:nowritebarrierrec
   425  func sigaddset(mask *sigset, i int) {
   426  	*mask |= 1 << (uint32(i) - 1)
   427  }
   428  
   429  func sigdelset(mask *sigset, i int) {
   430  	*mask &^= 1 << (uint32(i) - 1)
   431  }
   432  
   433  func setProcessCPUProfiler(hz int32) {
   434  	setProcessCPUProfilerTimer(hz)
   435  }
   436  
   437  func setThreadCPUProfiler(hz int32) {
   438  	setThreadCPUProfilerHz(hz)
   439  }
   440  
   441  //go:nosplit
   442  func validSIGPROF(mp *m, c *sigctxt) bool {
   443  	return true
   444  }
   445  
   446  //go:linkname executablePath os.executablePath
   447  var executablePath string
   448  
   449  func sysargs(argc int32, argv **byte) {
   450  	// skip over argv, envv and the first string will be the path
   451  	n := argc + 1
   452  	for argv_index(argv, n) != nil {
   453  		n++
   454  	}
   455  	executablePath = gostringnocopy(argv_index(argv, n+1))
   456  
   457  	// strip "executable_path=" prefix if available, it's added after OS X 10.11.
   458  	const prefix = "executable_path="
   459  	if len(executablePath) > len(prefix) && executablePath[:len(prefix)] == prefix {
   460  		executablePath = executablePath[len(prefix):]
   461  	}
   462  }
   463  
   464  func signalM(mp *m, sig int) {
   465  	pthread_kill(pthread(mp.procid), uint32(sig))
   466  }
   467  
   468  // sigPerThreadSyscall is only used on linux, so we assign a bogus signal
   469  // number.
   470  const sigPerThreadSyscall = 1 << 31
   471  
   472  //go:nosplit
   473  func runPerThreadSyscall() {
   474  	throw("runPerThreadSyscall only valid on linux")
   475  }