github.com/epfl-dcsl/gotee@v0.0.0-20200909122901-014b35f5e5e9/src/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 "unsafe"
     8  
     9  type mOS struct {
    10  	machport uint32 // return address for mach ipc
    11  	waitsema uint32 // semaphore for parking on locks
    12  }
    13  
    14  var darwinVersion int
    15  
    16  func bsdthread_create(stk, arg unsafe.Pointer, fn uintptr) int32
    17  func bsdthread_register() int32
    18  
    19  //go:noescape
    20  func mach_msg_trap(h unsafe.Pointer, op int32, send_size, rcv_size, rcv_name, timeout, notify uint32) int32
    21  
    22  func mach_reply_port() uint32
    23  func mach_task_self() uint32
    24  func mach_thread_self() uint32
    25  
    26  //go:noescape
    27  func sysctl(mib *uint32, miblen uint32, out *byte, size *uintptr, dst *byte, ndst uintptr) int32
    28  
    29  func unimplemented(name string) {
    30  	println(name, "not implemented")
    31  	*(*int)(unsafe.Pointer(uintptr(1231))) = 1231
    32  }
    33  
    34  //go:nosplit
    35  func semawakeup(mp *m) {
    36  	mach_semrelease(mp.waitsema)
    37  }
    38  
    39  //go:nosplit
    40  func semacreate(mp *m) {
    41  	if mp.waitsema != 0 {
    42  		return
    43  	}
    44  	systemstack(func() {
    45  		mp.waitsema = mach_semcreate()
    46  	})
    47  }
    48  
    49  // BSD interface for threading.
    50  func osinit() {
    51  	// bsdthread_register delayed until end of goenvs so that we
    52  	// can look at the environment first.
    53  
    54  	ncpu = getncpu()
    55  	physPageSize = getPageSize()
    56  	darwinVersion = getDarwinVersion()
    57  }
    58  
    59  const (
    60  	_CTL_KERN       = 1
    61  	_CTL_HW         = 6
    62  	_KERN_OSRELEASE = 2
    63  	_HW_NCPU        = 3
    64  	_HW_PAGESIZE    = 7
    65  )
    66  
    67  func getDarwinVersion() int {
    68  	// Use sysctl to fetch kern.osrelease
    69  	mib := [2]uint32{_CTL_KERN, _KERN_OSRELEASE}
    70  	var out [32]byte
    71  	nout := unsafe.Sizeof(out)
    72  	ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0)
    73  	if ret >= 0 {
    74  		ver := 0
    75  		for i := 0; i < int(nout) && out[i] >= '0' && out[i] <= '9'; i++ {
    76  			ver *= 10
    77  			ver += int(out[i] - '0')
    78  		}
    79  		return ver
    80  	}
    81  	return 17 // should not happen: default to a newish version
    82  }
    83  
    84  func getncpu() int32 {
    85  	// Use sysctl to fetch hw.ncpu.
    86  	mib := [2]uint32{_CTL_HW, _HW_NCPU}
    87  	out := uint32(0)
    88  	nout := unsafe.Sizeof(out)
    89  	ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0)
    90  	if ret >= 0 && int32(out) > 0 {
    91  		return int32(out)
    92  	}
    93  	return 1
    94  }
    95  
    96  func getPageSize() uintptr {
    97  	// Use sysctl to fetch hw.pagesize.
    98  	mib := [2]uint32{_CTL_HW, _HW_PAGESIZE}
    99  	out := uint32(0)
   100  	nout := unsafe.Sizeof(out)
   101  	ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0)
   102  	if ret >= 0 && int32(out) > 0 {
   103  		return uintptr(out)
   104  	}
   105  	return 0
   106  }
   107  
   108  var urandom_dev = []byte("/dev/urandom\x00")
   109  
   110  //go:nosplit
   111  func getRandomData(r []byte) {
   112  	fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0)
   113  	n := read(fd, unsafe.Pointer(&r[0]), int32(len(r)))
   114  	closefd(fd)
   115  	extendRandom(r, int(n))
   116  }
   117  
   118  func goenvs() {
   119  	goenvs_unix()
   120  
   121  	// Register our thread-creation callback (see sys_darwin_{amd64,386}.s)
   122  	// but only if we're not using cgo. If we are using cgo we need
   123  	// to let the C pthread library install its own thread-creation callback.
   124  	if !iscgo {
   125  		if bsdthread_register() != 0 {
   126  			if gogetenv("DYLD_INSERT_LIBRARIES") != "" {
   127  				throw("runtime: bsdthread_register error (unset DYLD_INSERT_LIBRARIES)")
   128  			}
   129  			throw("runtime: bsdthread_register error")
   130  		}
   131  	}
   132  }
   133  
   134  // May run with m.p==nil, so write barriers are not allowed.
   135  //go:nowritebarrier
   136  func newosproc(mp *m, stk unsafe.Pointer) {
   137  	if false {
   138  		print("newosproc stk=", stk, " m=", mp, " g=", mp.g0, " id=", mp.id, " ostk=", &mp, "\n")
   139  	}
   140  
   141  	var oset sigset
   142  	sigprocmask(_SIG_SETMASK, &sigset_all, &oset)
   143  	errno := bsdthread_create(stk, unsafe.Pointer(mp), funcPC(mstart))
   144  	sigprocmask(_SIG_SETMASK, &oset, nil)
   145  
   146  	if errno < 0 {
   147  		print("runtime: failed to create new OS thread (have ", mcount(), " already; errno=", -errno, ")\n")
   148  		throw("runtime.newosproc")
   149  	}
   150  }
   151  
   152  // newosproc0 is a version of newosproc that can be called before the runtime
   153  // is initialized.
   154  //
   155  // As Go uses bsdthread_register when running without cgo, this function is
   156  // not safe to use after initialization as it does not pass an M as fnarg.
   157  //
   158  //go:nosplit
   159  func newosproc0(stacksize uintptr, fn uintptr) {
   160  	stack := sysAlloc(stacksize, &memstats.stacks_sys)
   161  	if stack == nil {
   162  		write(2, unsafe.Pointer(&failallocatestack[0]), int32(len(failallocatestack)))
   163  		exit(1)
   164  	}
   165  	stk := unsafe.Pointer(uintptr(stack) + stacksize)
   166  
   167  	var oset sigset
   168  	sigprocmask(_SIG_SETMASK, &sigset_all, &oset)
   169  	errno := bsdthread_create(stk, nil, fn)
   170  	sigprocmask(_SIG_SETMASK, &oset, nil)
   171  
   172  	if errno < 0 {
   173  		write(2, unsafe.Pointer(&failthreadcreate[0]), int32(len(failthreadcreate)))
   174  		exit(1)
   175  	}
   176  }
   177  
   178  var failallocatestack = []byte("runtime: failed to allocate stack for the new OS thread\n")
   179  var failthreadcreate = []byte("runtime: failed to create new OS thread\n")
   180  
   181  // Called to do synchronous initialization of Go code built with
   182  // -buildmode=c-archive or -buildmode=c-shared.
   183  // None of the Go runtime is initialized.
   184  //go:nosplit
   185  //go:nowritebarrierrec
   186  func libpreinit() {
   187  	initsig(true)
   188  }
   189  
   190  // Called to initialize a new m (including the bootstrap m).
   191  // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
   192  func mpreinit(mp *m) {
   193  	mp.gsignal = malg(32 * 1024) // OS X wants >= 8K
   194  	mp.gsignal.m = mp
   195  }
   196  
   197  // Called to initialize a new m (including the bootstrap m).
   198  // Called on the new thread, cannot allocate memory.
   199  func minit() {
   200  	// The alternate signal stack is buggy on arm and arm64.
   201  	// The signal handler handles it directly.
   202  	// The sigaltstack assembly function does nothing.
   203  	if GOARCH != "arm" && GOARCH != "arm64" {
   204  		minitSignalStack()
   205  	}
   206  	minitSignalMask()
   207  }
   208  
   209  // Called from dropm to undo the effect of an minit.
   210  //go:nosplit
   211  func unminit() {
   212  	// The alternate signal stack is buggy on arm and arm64.
   213  	// See minit.
   214  	if GOARCH != "arm" && GOARCH != "arm64" {
   215  		unminitSignals()
   216  	}
   217  }
   218  
   219  // Mach IPC, to get at semaphores
   220  // Definitions are in /usr/include/mach on a Mac.
   221  
   222  func macherror(r int32, fn string) {
   223  	print("mach error ", fn, ": ", r, "\n")
   224  	throw("mach error")
   225  }
   226  
   227  const _DebugMach = false
   228  
   229  var zerondr machndr
   230  
   231  func mach_msgh_bits(a, b uint32) uint32 {
   232  	return a | b<<8
   233  }
   234  
   235  func mach_msg(h *machheader, op int32, send_size, rcv_size, rcv_name, timeout, notify uint32) int32 {
   236  	// TODO: Loop on interrupt.
   237  	return mach_msg_trap(unsafe.Pointer(h), op, send_size, rcv_size, rcv_name, timeout, notify)
   238  }
   239  
   240  // Mach RPC (MIG)
   241  const (
   242  	_MinMachMsg = 48
   243  	_MachReply  = 100
   244  )
   245  
   246  type codemsg struct {
   247  	h    machheader
   248  	ndr  machndr
   249  	code int32
   250  }
   251  
   252  func machcall(h *machheader, maxsize int32, rxsize int32) int32 {
   253  	_g_ := getg()
   254  	port := _g_.m.machport
   255  	if port == 0 {
   256  		port = mach_reply_port()
   257  		_g_.m.machport = port
   258  	}
   259  
   260  	h.msgh_bits |= mach_msgh_bits(_MACH_MSG_TYPE_COPY_SEND, _MACH_MSG_TYPE_MAKE_SEND_ONCE)
   261  	h.msgh_local_port = port
   262  	h.msgh_reserved = 0
   263  	id := h.msgh_id
   264  
   265  	if _DebugMach {
   266  		p := (*[10000]unsafe.Pointer)(unsafe.Pointer(h))
   267  		print("send:\t")
   268  		var i uint32
   269  		for i = 0; i < h.msgh_size/uint32(unsafe.Sizeof(p[0])); i++ {
   270  			print(" ", p[i])
   271  			if i%8 == 7 {
   272  				print("\n\t")
   273  			}
   274  		}
   275  		if i%8 != 0 {
   276  			print("\n")
   277  		}
   278  	}
   279  	ret := mach_msg(h, _MACH_SEND_MSG|_MACH_RCV_MSG, h.msgh_size, uint32(maxsize), port, 0, 0)
   280  	if ret != 0 {
   281  		if _DebugMach {
   282  			print("mach_msg error ", ret, "\n")
   283  		}
   284  		return ret
   285  	}
   286  	if _DebugMach {
   287  		p := (*[10000]unsafe.Pointer)(unsafe.Pointer(h))
   288  		var i uint32
   289  		for i = 0; i < h.msgh_size/uint32(unsafe.Sizeof(p[0])); i++ {
   290  			print(" ", p[i])
   291  			if i%8 == 7 {
   292  				print("\n\t")
   293  			}
   294  		}
   295  		if i%8 != 0 {
   296  			print("\n")
   297  		}
   298  	}
   299  	if h.msgh_id != id+_MachReply {
   300  		if _DebugMach {
   301  			print("mach_msg _MachReply id mismatch ", h.msgh_id, " != ", id+_MachReply, "\n")
   302  		}
   303  		return -303 // MIG_REPLY_MISMATCH
   304  	}
   305  	// Look for a response giving the return value.
   306  	// Any call can send this back with an error,
   307  	// and some calls only have return values so they
   308  	// send it back on success too. I don't quite see how
   309  	// you know it's one of these and not the full response
   310  	// format, so just look if the message is right.
   311  	c := (*codemsg)(unsafe.Pointer(h))
   312  	if uintptr(h.msgh_size) == unsafe.Sizeof(*c) && h.msgh_bits&_MACH_MSGH_BITS_COMPLEX == 0 {
   313  		if _DebugMach {
   314  			print("mig result ", c.code, "\n")
   315  		}
   316  		return c.code
   317  	}
   318  	if h.msgh_size != uint32(rxsize) {
   319  		if _DebugMach {
   320  			print("mach_msg _MachReply size mismatch ", h.msgh_size, " != ", rxsize, "\n")
   321  		}
   322  		return -307 // MIG_ARRAY_TOO_LARGE
   323  	}
   324  	return 0
   325  }
   326  
   327  // Semaphores!
   328  
   329  const (
   330  	tmach_semcreate = 3418
   331  	rmach_semcreate = tmach_semcreate + _MachReply
   332  
   333  	tmach_semdestroy = 3419
   334  	rmach_semdestroy = tmach_semdestroy + _MachReply
   335  
   336  	_KERN_ABORTED             = 14
   337  	_KERN_OPERATION_TIMED_OUT = 49
   338  )
   339  
   340  type tmach_semcreatemsg struct {
   341  	h      machheader
   342  	ndr    machndr
   343  	policy int32
   344  	value  int32
   345  }
   346  
   347  type rmach_semcreatemsg struct {
   348  	h         machheader
   349  	body      machbody
   350  	semaphore machport
   351  }
   352  
   353  type tmach_semdestroymsg struct {
   354  	h         machheader
   355  	body      machbody
   356  	semaphore machport
   357  }
   358  
   359  func mach_semcreate() uint32 {
   360  	var m [256]uint8
   361  	tx := (*tmach_semcreatemsg)(unsafe.Pointer(&m))
   362  	rx := (*rmach_semcreatemsg)(unsafe.Pointer(&m))
   363  
   364  	tx.h.msgh_bits = 0
   365  	tx.h.msgh_size = uint32(unsafe.Sizeof(*tx))
   366  	tx.h.msgh_remote_port = mach_task_self()
   367  	tx.h.msgh_id = tmach_semcreate
   368  	tx.ndr = zerondr
   369  
   370  	tx.policy = 0 // 0 = SYNC_POLICY_FIFO
   371  	tx.value = 0
   372  
   373  	for {
   374  		r := machcall(&tx.h, int32(unsafe.Sizeof(m)), int32(unsafe.Sizeof(*rx)))
   375  		if r == 0 {
   376  			break
   377  		}
   378  		if r == _KERN_ABORTED { // interrupted
   379  			continue
   380  		}
   381  		macherror(r, "semaphore_create")
   382  	}
   383  	if rx.body.msgh_descriptor_count != 1 {
   384  		unimplemented("mach_semcreate desc count")
   385  	}
   386  	return rx.semaphore.name
   387  }
   388  
   389  func mach_semdestroy(sem uint32) {
   390  	var m [256]uint8
   391  	tx := (*tmach_semdestroymsg)(unsafe.Pointer(&m))
   392  
   393  	tx.h.msgh_bits = _MACH_MSGH_BITS_COMPLEX
   394  	tx.h.msgh_size = uint32(unsafe.Sizeof(*tx))
   395  	tx.h.msgh_remote_port = mach_task_self()
   396  	tx.h.msgh_id = tmach_semdestroy
   397  	tx.body.msgh_descriptor_count = 1
   398  	tx.semaphore.name = sem
   399  	tx.semaphore.disposition = _MACH_MSG_TYPE_MOVE_SEND
   400  	tx.semaphore._type = 0
   401  
   402  	for {
   403  		r := machcall(&tx.h, int32(unsafe.Sizeof(m)), 0)
   404  		if r == 0 {
   405  			break
   406  		}
   407  		if r == _KERN_ABORTED { // interrupted
   408  			continue
   409  		}
   410  		macherror(r, "semaphore_destroy")
   411  	}
   412  }
   413  
   414  // The other calls have simple system call traps in sys_darwin_{amd64,386}.s
   415  
   416  func mach_semaphore_wait(sema uint32) int32
   417  func mach_semaphore_timedwait(sema, sec, nsec uint32) int32
   418  func mach_semaphore_signal(sema uint32) int32
   419  func mach_semaphore_signal_all(sema uint32) int32
   420  
   421  func semasleep1(ns int64) int32 {
   422  	_g_ := getg()
   423  
   424  	if ns >= 0 {
   425  		var nsecs int32
   426  		secs := timediv(ns, 1000000000, &nsecs)
   427  		r := mach_semaphore_timedwait(_g_.m.waitsema, uint32(secs), uint32(nsecs))
   428  		if r == _KERN_ABORTED || r == _KERN_OPERATION_TIMED_OUT {
   429  			return -1
   430  		}
   431  		if r != 0 {
   432  			macherror(r, "semaphore_wait")
   433  		}
   434  		return 0
   435  	}
   436  
   437  	for {
   438  		r := mach_semaphore_wait(_g_.m.waitsema)
   439  		if r == 0 {
   440  			break
   441  		}
   442  		// Note: We don't know how this call (with no timeout) can get _KERN_OPERATION_TIMED_OUT,
   443  		// but it does reliably, though at a very low rate, on OS X 10.8, 10.9, 10.10, and 10.11.
   444  		// See golang.org/issue/17161.
   445  		if r == _KERN_ABORTED || r == _KERN_OPERATION_TIMED_OUT { // interrupted
   446  			continue
   447  		}
   448  		macherror(r, "semaphore_wait")
   449  	}
   450  	return 0
   451  }
   452  
   453  //go:nosplit
   454  func semasleep(ns int64) int32 {
   455  	var r int32
   456  	systemstack(func() {
   457  		r = semasleep1(ns)
   458  	})
   459  	return r
   460  }
   461  
   462  //go:nosplit
   463  func mach_semrelease(sem uint32) {
   464  	for {
   465  		r := mach_semaphore_signal(sem)
   466  		if r == 0 {
   467  			break
   468  		}
   469  		if r == _KERN_ABORTED { // interrupted
   470  			continue
   471  		}
   472  
   473  		// mach_semrelease must be completely nosplit,
   474  		// because it is called from Go code.
   475  		// If we're going to die, start that process on the system stack
   476  		// to avoid a Go stack split.
   477  		systemstack(func() { macherror(r, "semaphore_signal") })
   478  	}
   479  }
   480  
   481  //go:nosplit
   482  func osyield() {
   483  	usleep(1)
   484  }
   485  
   486  func memlimit() uintptr {
   487  	// NOTE(rsc): Could use getrlimit here,
   488  	// like on FreeBSD or Linux, but Darwin doesn't enforce
   489  	// ulimit -v, so it's unclear why we'd try to stay within
   490  	// the limit.
   491  	return 0
   492  }
   493  
   494  const (
   495  	_NSIG        = 32
   496  	_SI_USER     = 0 /* empirically true, but not what headers say */
   497  	_SIG_BLOCK   = 1
   498  	_SIG_UNBLOCK = 2
   499  	_SIG_SETMASK = 3
   500  	_SS_DISABLE  = 4
   501  )
   502  
   503  //go:noescape
   504  func sigprocmask(how int32, new, old *sigset)
   505  
   506  //go:noescape
   507  func sigaction(mode uint32, new *sigactiont, old *usigactiont)
   508  
   509  //go:noescape
   510  func sigaltstack(new, old *stackt)
   511  
   512  // darwin/arm64 uses registers instead of stack-based arguments.
   513  // TODO: does this matter?
   514  func sigtramp(fn uintptr, infostyle, sig uint32, info *siginfo, ctx unsafe.Pointer)
   515  
   516  //go:noescape
   517  func setitimer(mode int32, new, old *itimerval)
   518  
   519  func raise(sig uint32)
   520  func raiseproc(sig uint32)
   521  
   522  //extern SigTabTT runtimeĀ·sigtab[];
   523  
   524  type sigset uint32
   525  
   526  var sigset_all = ^sigset(0)
   527  
   528  //go:nosplit
   529  //go:nowritebarrierrec
   530  func setsig(i uint32, fn uintptr) {
   531  	var sa sigactiont
   532  	sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK | _SA_RESTART
   533  	sa.sa_mask = ^uint32(0)
   534  	sa.sa_tramp = unsafe.Pointer(funcPC(sigtramp)) // runtimeĀ·sigtramp's job is to call into real handler
   535  	*(*uintptr)(unsafe.Pointer(&sa.__sigaction_u)) = fn
   536  	sigaction(i, &sa, nil)
   537  }
   538  
   539  //go:nosplit
   540  //go:nowritebarrierrec
   541  func setsigstack(i uint32) {
   542  	var osa usigactiont
   543  	sigaction(i, nil, &osa)
   544  	handler := *(*uintptr)(unsafe.Pointer(&osa.__sigaction_u))
   545  	if osa.sa_flags&_SA_ONSTACK != 0 {
   546  		return
   547  	}
   548  	var sa sigactiont
   549  	*(*uintptr)(unsafe.Pointer(&sa.__sigaction_u)) = handler
   550  	sa.sa_tramp = unsafe.Pointer(funcPC(sigtramp))
   551  	sa.sa_mask = osa.sa_mask
   552  	sa.sa_flags = osa.sa_flags | _SA_ONSTACK
   553  	sigaction(i, &sa, nil)
   554  }
   555  
   556  //go:nosplit
   557  //go:nowritebarrierrec
   558  func getsig(i uint32) uintptr {
   559  	var sa usigactiont
   560  	sigaction(i, nil, &sa)
   561  	return *(*uintptr)(unsafe.Pointer(&sa.__sigaction_u))
   562  }
   563  
   564  // setSignaltstackSP sets the ss_sp field of a stackt.
   565  //go:nosplit
   566  func setSignalstackSP(s *stackt, sp uintptr) {
   567  	*(*uintptr)(unsafe.Pointer(&s.ss_sp)) = sp
   568  }
   569  
   570  //go:nosplit
   571  //go:nowritebarrierrec
   572  func sigaddset(mask *sigset, i int) {
   573  	*mask |= 1 << (uint32(i) - 1)
   574  }
   575  
   576  func sigdelset(mask *sigset, i int) {
   577  	*mask &^= 1 << (uint32(i) - 1)
   578  }
   579  
   580  //go:linkname executablePath os.executablePath
   581  var executablePath string
   582  
   583  func sysargs(argc int32, argv **byte) {
   584  	// skip over argv, envv and the first string will be the path
   585  	n := argc + 1
   586  	for argv_index(argv, n) != nil {
   587  		n++
   588  	}
   589  	executablePath = gostringnocopy(argv_index(argv, n+1))
   590  
   591  	// strip "executable_path=" prefix if available, it's added after OS X 10.11.
   592  	const prefix = "executable_path="
   593  	if len(executablePath) > len(prefix) && executablePath[:len(prefix)] == prefix {
   594  		executablePath = executablePath[len(prefix):]
   595  	}
   596  }