Source file src/runtime/os3_solaris.go

     1  // Copyright 2011 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  	"internal/goarch"
    10  	"unsafe"
    11  )
    12  
    13  //go:cgo_export_dynamic runtime.end _end
    14  //go:cgo_export_dynamic runtime.etext _etext
    15  //go:cgo_export_dynamic runtime.edata _edata
    16  
    17  //go:cgo_import_dynamic libc____errno ___errno "libc.so"
    18  //go:cgo_import_dynamic libc_clock_gettime clock_gettime "libc.so"
    19  //go:cgo_import_dynamic libc_exit exit "libc.so"
    20  //go:cgo_import_dynamic libc_getcontext getcontext "libc.so"
    21  //go:cgo_import_dynamic libc_kill kill "libc.so"
    22  //go:cgo_import_dynamic libc_madvise madvise "libc.so"
    23  //go:cgo_import_dynamic libc_malloc malloc "libc.so"
    24  //go:cgo_import_dynamic libc_mmap mmap "libc.so"
    25  //go:cgo_import_dynamic libc_munmap munmap "libc.so"
    26  //go:cgo_import_dynamic libc_open open "libc.so"
    27  //go:cgo_import_dynamic libc_pthread_attr_destroy pthread_attr_destroy "libc.so"
    28  //go:cgo_import_dynamic libc_pthread_attr_getstack pthread_attr_getstack "libc.so"
    29  //go:cgo_import_dynamic libc_pthread_attr_init pthread_attr_init "libc.so"
    30  //go:cgo_import_dynamic libc_pthread_attr_setdetachstate pthread_attr_setdetachstate "libc.so"
    31  //go:cgo_import_dynamic libc_pthread_attr_setstack pthread_attr_setstack "libc.so"
    32  //go:cgo_import_dynamic libc_pthread_create pthread_create "libc.so"
    33  //go:cgo_import_dynamic libc_pthread_self pthread_self "libc.so"
    34  //go:cgo_import_dynamic libc_pthread_kill pthread_kill "libc.so"
    35  //go:cgo_import_dynamic libc_raise raise "libc.so"
    36  //go:cgo_import_dynamic libc_read read "libc.so"
    37  //go:cgo_import_dynamic libc_select select "libc.so"
    38  //go:cgo_import_dynamic libc_sched_yield sched_yield "libc.so"
    39  //go:cgo_import_dynamic libc_sem_init sem_init "libc.so"
    40  //go:cgo_import_dynamic libc_sem_post sem_post "libc.so"
    41  //go:cgo_import_dynamic libc_sem_reltimedwait_np sem_reltimedwait_np "libc.so"
    42  //go:cgo_import_dynamic libc_sem_wait sem_wait "libc.so"
    43  //go:cgo_import_dynamic libc_setitimer setitimer "libc.so"
    44  //go:cgo_import_dynamic libc_sigaction sigaction "libc.so"
    45  //go:cgo_import_dynamic libc_sigaltstack sigaltstack "libc.so"
    46  //go:cgo_import_dynamic libc_sigprocmask sigprocmask "libc.so"
    47  //go:cgo_import_dynamic libc_sysconf sysconf "libc.so"
    48  //go:cgo_import_dynamic libc_usleep usleep "libc.so"
    49  //go:cgo_import_dynamic libc_write write "libc.so"
    50  //go:cgo_import_dynamic libc_pipe2 pipe2 "libc.so"
    51  
    52  //go:linkname libc____errno libc____errno
    53  //go:linkname libc_clock_gettime libc_clock_gettime
    54  //go:linkname libc_exit libc_exit
    55  //go:linkname libc_getcontext libc_getcontext
    56  //go:linkname libc_kill libc_kill
    57  //go:linkname libc_madvise libc_madvise
    58  //go:linkname libc_malloc libc_malloc
    59  //go:linkname libc_mmap libc_mmap
    60  //go:linkname libc_munmap libc_munmap
    61  //go:linkname libc_open libc_open
    62  //go:linkname libc_pthread_attr_destroy libc_pthread_attr_destroy
    63  //go:linkname libc_pthread_attr_getstack libc_pthread_attr_getstack
    64  //go:linkname libc_pthread_attr_init libc_pthread_attr_init
    65  //go:linkname libc_pthread_attr_setdetachstate libc_pthread_attr_setdetachstate
    66  //go:linkname libc_pthread_attr_setstack libc_pthread_attr_setstack
    67  //go:linkname libc_pthread_create libc_pthread_create
    68  //go:linkname libc_pthread_self libc_pthread_self
    69  //go:linkname libc_pthread_kill libc_pthread_kill
    70  //go:linkname libc_raise libc_raise
    71  //go:linkname libc_read libc_read
    72  //go:linkname libc_select libc_select
    73  //go:linkname libc_sched_yield libc_sched_yield
    74  //go:linkname libc_sem_init libc_sem_init
    75  //go:linkname libc_sem_post libc_sem_post
    76  //go:linkname libc_sem_reltimedwait_np libc_sem_reltimedwait_np
    77  //go:linkname libc_sem_wait libc_sem_wait
    78  //go:linkname libc_setitimer libc_setitimer
    79  //go:linkname libc_sigaction libc_sigaction
    80  //go:linkname libc_sigaltstack libc_sigaltstack
    81  //go:linkname libc_sigprocmask libc_sigprocmask
    82  //go:linkname libc_sysconf libc_sysconf
    83  //go:linkname libc_usleep libc_usleep
    84  //go:linkname libc_write libc_write
    85  //go:linkname libc_pipe2 libc_pipe2
    86  
    87  var (
    88  	libc____errno,
    89  	libc_clock_gettime,
    90  	libc_exit,
    91  	libc_getcontext,
    92  	libc_kill,
    93  	libc_madvise,
    94  	libc_malloc,
    95  	libc_mmap,
    96  	libc_munmap,
    97  	libc_open,
    98  	libc_pthread_attr_destroy,
    99  	libc_pthread_attr_getstack,
   100  	libc_pthread_attr_init,
   101  	libc_pthread_attr_setdetachstate,
   102  	libc_pthread_attr_setstack,
   103  	libc_pthread_create,
   104  	libc_pthread_self,
   105  	libc_pthread_kill,
   106  	libc_raise,
   107  	libc_read,
   108  	libc_sched_yield,
   109  	libc_select,
   110  	libc_sem_init,
   111  	libc_sem_post,
   112  	libc_sem_reltimedwait_np,
   113  	libc_sem_wait,
   114  	libc_setitimer,
   115  	libc_sigaction,
   116  	libc_sigaltstack,
   117  	libc_sigprocmask,
   118  	libc_sysconf,
   119  	libc_usleep,
   120  	libc_write,
   121  	libc_pipe2 libcFunc
   122  )
   123  
   124  var sigset_all = sigset{[4]uint32{^uint32(0), ^uint32(0), ^uint32(0), ^uint32(0)}}
   125  
   126  func getPageSize() uintptr {
   127  	n := int32(sysconf(__SC_PAGESIZE))
   128  	if n <= 0 {
   129  		return 0
   130  	}
   131  	return uintptr(n)
   132  }
   133  
   134  func osinit() {
   135  	ncpu = getncpu()
   136  	if physPageSize == 0 {
   137  		physPageSize = getPageSize()
   138  	}
   139  }
   140  
   141  func tstart_sysvicall(newm *m) uint32
   142  
   143  // May run with m.p==nil, so write barriers are not allowed.
   144  //
   145  //go:nowritebarrier
   146  func newosproc(mp *m) {
   147  	var (
   148  		attr pthreadattr
   149  		oset sigset
   150  		tid  pthread
   151  		ret  int32
   152  		size uint64
   153  	)
   154  
   155  	if pthread_attr_init(&attr) != 0 {
   156  		throw("pthread_attr_init")
   157  	}
   158  	// Allocate a new 2MB stack.
   159  	if pthread_attr_setstack(&attr, 0, 0x200000) != 0 {
   160  		throw("pthread_attr_setstack")
   161  	}
   162  	// Read back the allocated stack.
   163  	if pthread_attr_getstack(&attr, unsafe.Pointer(&mp.g0.stack.hi), &size) != 0 {
   164  		throw("pthread_attr_getstack")
   165  	}
   166  	mp.g0.stack.lo = mp.g0.stack.hi - uintptr(size)
   167  	if pthread_attr_setdetachstate(&attr, _PTHREAD_CREATE_DETACHED) != 0 {
   168  		throw("pthread_attr_setdetachstate")
   169  	}
   170  
   171  	// Disable signals during create, so that the new thread starts
   172  	// with signals disabled. It will enable them in minit.
   173  	sigprocmask(_SIG_SETMASK, &sigset_all, &oset)
   174  	ret = pthread_create(&tid, &attr, abi.FuncPCABI0(tstart_sysvicall), unsafe.Pointer(mp))
   175  	sigprocmask(_SIG_SETMASK, &oset, nil)
   176  	if ret != 0 {
   177  		print("runtime: failed to create new OS thread (have ", mcount(), " already; errno=", ret, ")\n")
   178  		if ret == -_EAGAIN {
   179  			println("runtime: may need to increase max user processes (ulimit -u)")
   180  		}
   181  		throw("newosproc")
   182  	}
   183  }
   184  
   185  func exitThread(wait *uint32) {
   186  	// We should never reach exitThread on Solaris because we let
   187  	// libc clean up threads.
   188  	throw("exitThread")
   189  }
   190  
   191  var urandom_dev = []byte("/dev/urandom\x00")
   192  
   193  //go:nosplit
   194  func getRandomData(r []byte) {
   195  	fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0)
   196  	n := read(fd, unsafe.Pointer(&r[0]), int32(len(r)))
   197  	closefd(fd)
   198  	extendRandom(r, int(n))
   199  }
   200  
   201  func goenvs() {
   202  	goenvs_unix()
   203  }
   204  
   205  // Called to initialize a new m (including the bootstrap m).
   206  // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
   207  func mpreinit(mp *m) {
   208  	mp.gsignal = malg(32 * 1024)
   209  	mp.gsignal.m = mp
   210  }
   211  
   212  func miniterrno()
   213  
   214  // Called to initialize a new m (including the bootstrap m).
   215  // Called on the new thread, cannot allocate memory.
   216  func minit() {
   217  	asmcgocall(unsafe.Pointer(abi.FuncPCABI0(miniterrno)), unsafe.Pointer(&libc____errno))
   218  
   219  	minitSignals()
   220  
   221  	getg().m.procid = uint64(pthread_self())
   222  }
   223  
   224  // Called from dropm to undo the effect of an minit.
   225  func unminit() {
   226  	unminitSignals()
   227  }
   228  
   229  // Called from exitm, but not from drop, to undo the effect of thread-owned
   230  // resources in minit, semacreate, or elsewhere. Do not take locks after calling this.
   231  func mdestroy(mp *m) {
   232  }
   233  
   234  func sigtramp()
   235  
   236  //go:nosplit
   237  //go:nowritebarrierrec
   238  func setsig(i uint32, fn uintptr) {
   239  	var sa sigactiont
   240  
   241  	sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK | _SA_RESTART
   242  	sa.sa_mask = sigset_all
   243  	if fn == abi.FuncPCABIInternal(sighandler) { // abi.FuncPCABIInternal(sighandler) matches the callers in signal_unix.go
   244  		fn = abi.FuncPCABI0(sigtramp)
   245  	}
   246  	*((*uintptr)(unsafe.Pointer(&sa._funcptr))) = fn
   247  	sigaction(i, &sa, nil)
   248  }
   249  
   250  //go:nosplit
   251  //go:nowritebarrierrec
   252  func setsigstack(i uint32) {
   253  	var sa sigactiont
   254  	sigaction(i, nil, &sa)
   255  	if sa.sa_flags&_SA_ONSTACK != 0 {
   256  		return
   257  	}
   258  	sa.sa_flags |= _SA_ONSTACK
   259  	sigaction(i, &sa, nil)
   260  }
   261  
   262  //go:nosplit
   263  //go:nowritebarrierrec
   264  func getsig(i uint32) uintptr {
   265  	var sa sigactiont
   266  	sigaction(i, nil, &sa)
   267  	return *((*uintptr)(unsafe.Pointer(&sa._funcptr)))
   268  }
   269  
   270  // setSignaltstackSP sets the ss_sp field of a stackt.
   271  //
   272  //go:nosplit
   273  func setSignalstackSP(s *stackt, sp uintptr) {
   274  	*(*uintptr)(unsafe.Pointer(&s.ss_sp)) = sp
   275  }
   276  
   277  //go:nosplit
   278  //go:nowritebarrierrec
   279  func sigaddset(mask *sigset, i int) {
   280  	mask.__sigbits[(i-1)/32] |= 1 << ((uint32(i) - 1) & 31)
   281  }
   282  
   283  func sigdelset(mask *sigset, i int) {
   284  	mask.__sigbits[(i-1)/32] &^= 1 << ((uint32(i) - 1) & 31)
   285  }
   286  
   287  //go:nosplit
   288  func (c *sigctxt) fixsigcode(sig uint32) {
   289  }
   290  
   291  func setProcessCPUProfiler(hz int32) {
   292  	setProcessCPUProfilerTimer(hz)
   293  }
   294  
   295  func setThreadCPUProfiler(hz int32) {
   296  	setThreadCPUProfilerHz(hz)
   297  }
   298  
   299  //go:nosplit
   300  func validSIGPROF(mp *m, c *sigctxt) bool {
   301  	return true
   302  }
   303  
   304  //go:nosplit
   305  func semacreate(mp *m) {
   306  	if mp.waitsema != 0 {
   307  		return
   308  	}
   309  
   310  	var sem *semt
   311  	_g_ := getg()
   312  
   313  	// Call libc's malloc rather than malloc. This will
   314  	// allocate space on the C heap. We can't call malloc
   315  	// here because it could cause a deadlock.
   316  	_g_.m.libcall.fn = uintptr(unsafe.Pointer(&libc_malloc))
   317  	_g_.m.libcall.n = 1
   318  	_g_.m.scratch = mscratch{}
   319  	_g_.m.scratch.v[0] = unsafe.Sizeof(*sem)
   320  	_g_.m.libcall.args = uintptr(unsafe.Pointer(&_g_.m.scratch))
   321  	asmcgocall(unsafe.Pointer(&asmsysvicall6x), unsafe.Pointer(&_g_.m.libcall))
   322  	sem = (*semt)(unsafe.Pointer(_g_.m.libcall.r1))
   323  	if sem_init(sem, 0, 0) != 0 {
   324  		throw("sem_init")
   325  	}
   326  	mp.waitsema = uintptr(unsafe.Pointer(sem))
   327  }
   328  
   329  //go:nosplit
   330  func semasleep(ns int64) int32 {
   331  	mp := getg().m
   332  	if ns >= 0 {
   333  		mp.ts.tv_sec = ns / 1000000000
   334  		mp.ts.tv_nsec = ns % 1000000000
   335  
   336  		mp.libcall.fn = uintptr(unsafe.Pointer(&libc_sem_reltimedwait_np))
   337  		mp.libcall.n = 2
   338  		mp.scratch = mscratch{}
   339  		mp.scratch.v[0] = mp.waitsema
   340  		mp.scratch.v[1] = uintptr(unsafe.Pointer(&mp.ts))
   341  		mp.libcall.args = uintptr(unsafe.Pointer(&mp.scratch))
   342  		asmcgocall(unsafe.Pointer(&asmsysvicall6x), unsafe.Pointer(&mp.libcall))
   343  		if *mp.perrno != 0 {
   344  			if *mp.perrno == _ETIMEDOUT || *mp.perrno == _EAGAIN || *mp.perrno == _EINTR {
   345  				return -1
   346  			}
   347  			throw("sem_reltimedwait_np")
   348  		}
   349  		return 0
   350  	}
   351  	for {
   352  		mp.libcall.fn = uintptr(unsafe.Pointer(&libc_sem_wait))
   353  		mp.libcall.n = 1
   354  		mp.scratch = mscratch{}
   355  		mp.scratch.v[0] = mp.waitsema
   356  		mp.libcall.args = uintptr(unsafe.Pointer(&mp.scratch))
   357  		asmcgocall(unsafe.Pointer(&asmsysvicall6x), unsafe.Pointer(&mp.libcall))
   358  		if mp.libcall.r1 == 0 {
   359  			break
   360  		}
   361  		if *mp.perrno == _EINTR {
   362  			continue
   363  		}
   364  		throw("sem_wait")
   365  	}
   366  	return 0
   367  }
   368  
   369  //go:nosplit
   370  func semawakeup(mp *m) {
   371  	if sem_post((*semt)(unsafe.Pointer(mp.waitsema))) != 0 {
   372  		throw("sem_post")
   373  	}
   374  }
   375  
   376  //go:nosplit
   377  func closefd(fd int32) int32 {
   378  	return int32(sysvicall1(&libc_close, uintptr(fd)))
   379  }
   380  
   381  //go:nosplit
   382  func exit(r int32) {
   383  	sysvicall1(&libc_exit, uintptr(r))
   384  }
   385  
   386  //go:nosplit
   387  func getcontext(context *ucontext) /* int32 */ {
   388  	sysvicall1(&libc_getcontext, uintptr(unsafe.Pointer(context)))
   389  }
   390  
   391  //go:nosplit
   392  func madvise(addr unsafe.Pointer, n uintptr, flags int32) {
   393  	sysvicall3(&libc_madvise, uintptr(addr), uintptr(n), uintptr(flags))
   394  }
   395  
   396  //go:nosplit
   397  func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (unsafe.Pointer, int) {
   398  	p, err := doMmap(uintptr(addr), n, uintptr(prot), uintptr(flags), uintptr(fd), uintptr(off))
   399  	if p == ^uintptr(0) {
   400  		return nil, int(err)
   401  	}
   402  	return unsafe.Pointer(p), 0
   403  }
   404  
   405  //go:nosplit
   406  //go:cgo_unsafe_args
   407  func doMmap(addr, n, prot, flags, fd, off uintptr) (uintptr, uintptr) {
   408  	var libcall libcall
   409  	libcall.fn = uintptr(unsafe.Pointer(&libc_mmap))
   410  	libcall.n = 6
   411  	libcall.args = uintptr(noescape(unsafe.Pointer(&addr)))
   412  	asmcgocall(unsafe.Pointer(&asmsysvicall6x), unsafe.Pointer(&libcall))
   413  	return libcall.r1, libcall.err
   414  }
   415  
   416  //go:nosplit
   417  func munmap(addr unsafe.Pointer, n uintptr) {
   418  	sysvicall2(&libc_munmap, uintptr(addr), uintptr(n))
   419  }
   420  
   421  const (
   422  	_CLOCK_REALTIME  = 3
   423  	_CLOCK_MONOTONIC = 4
   424  )
   425  
   426  //go:nosplit
   427  func nanotime1() int64 {
   428  	var ts mts
   429  	sysvicall2(&libc_clock_gettime, _CLOCK_MONOTONIC, uintptr(unsafe.Pointer(&ts)))
   430  	return ts.tv_sec*1e9 + ts.tv_nsec
   431  }
   432  
   433  //go:nosplit
   434  func open(path *byte, mode, perm int32) int32 {
   435  	return int32(sysvicall3(&libc_open, uintptr(unsafe.Pointer(path)), uintptr(mode), uintptr(perm)))
   436  }
   437  
   438  func pthread_attr_destroy(attr *pthreadattr) int32 {
   439  	return int32(sysvicall1(&libc_pthread_attr_destroy, uintptr(unsafe.Pointer(attr))))
   440  }
   441  
   442  func pthread_attr_getstack(attr *pthreadattr, addr unsafe.Pointer, size *uint64) int32 {
   443  	return int32(sysvicall3(&libc_pthread_attr_getstack, uintptr(unsafe.Pointer(attr)), uintptr(addr), uintptr(unsafe.Pointer(size))))
   444  }
   445  
   446  func pthread_attr_init(attr *pthreadattr) int32 {
   447  	return int32(sysvicall1(&libc_pthread_attr_init, uintptr(unsafe.Pointer(attr))))
   448  }
   449  
   450  func pthread_attr_setdetachstate(attr *pthreadattr, state int32) int32 {
   451  	return int32(sysvicall2(&libc_pthread_attr_setdetachstate, uintptr(unsafe.Pointer(attr)), uintptr(state)))
   452  }
   453  
   454  func pthread_attr_setstack(attr *pthreadattr, addr uintptr, size uint64) int32 {
   455  	return int32(sysvicall3(&libc_pthread_attr_setstack, uintptr(unsafe.Pointer(attr)), uintptr(addr), uintptr(size)))
   456  }
   457  
   458  func pthread_create(thread *pthread, attr *pthreadattr, fn uintptr, arg unsafe.Pointer) int32 {
   459  	return int32(sysvicall4(&libc_pthread_create, uintptr(unsafe.Pointer(thread)), uintptr(unsafe.Pointer(attr)), uintptr(fn), uintptr(arg)))
   460  }
   461  
   462  func pthread_self() pthread {
   463  	return pthread(sysvicall0(&libc_pthread_self))
   464  }
   465  
   466  func signalM(mp *m, sig int) {
   467  	sysvicall2(&libc_pthread_kill, uintptr(pthread(mp.procid)), uintptr(sig))
   468  }
   469  
   470  //go:nosplit
   471  //go:nowritebarrierrec
   472  func raise(sig uint32) /* int32 */ {
   473  	sysvicall1(&libc_raise, uintptr(sig))
   474  }
   475  
   476  func raiseproc(sig uint32) /* int32 */ {
   477  	pid := sysvicall0(&libc_getpid)
   478  	sysvicall2(&libc_kill, pid, uintptr(sig))
   479  }
   480  
   481  //go:nosplit
   482  func read(fd int32, buf unsafe.Pointer, nbyte int32) int32 {
   483  	r1, err := sysvicall3Err(&libc_read, uintptr(fd), uintptr(buf), uintptr(nbyte))
   484  	if c := int32(r1); c >= 0 {
   485  		return c
   486  	}
   487  	return -int32(err)
   488  }
   489  
   490  //go:nosplit
   491  func sem_init(sem *semt, pshared int32, value uint32) int32 {
   492  	return int32(sysvicall3(&libc_sem_init, uintptr(unsafe.Pointer(sem)), uintptr(pshared), uintptr(value)))
   493  }
   494  
   495  //go:nosplit
   496  func sem_post(sem *semt) int32 {
   497  	return int32(sysvicall1(&libc_sem_post, uintptr(unsafe.Pointer(sem))))
   498  }
   499  
   500  //go:nosplit
   501  func sem_reltimedwait_np(sem *semt, timeout *timespec) int32 {
   502  	return int32(sysvicall2(&libc_sem_reltimedwait_np, uintptr(unsafe.Pointer(sem)), uintptr(unsafe.Pointer(timeout))))
   503  }
   504  
   505  //go:nosplit
   506  func sem_wait(sem *semt) int32 {
   507  	return int32(sysvicall1(&libc_sem_wait, uintptr(unsafe.Pointer(sem))))
   508  }
   509  
   510  func setitimer(which int32, value *itimerval, ovalue *itimerval) /* int32 */ {
   511  	sysvicall3(&libc_setitimer, uintptr(which), uintptr(unsafe.Pointer(value)), uintptr(unsafe.Pointer(ovalue)))
   512  }
   513  
   514  //go:nosplit
   515  //go:nowritebarrierrec
   516  func sigaction(sig uint32, act *sigactiont, oact *sigactiont) /* int32 */ {
   517  	sysvicall3(&libc_sigaction, uintptr(sig), uintptr(unsafe.Pointer(act)), uintptr(unsafe.Pointer(oact)))
   518  }
   519  
   520  //go:nosplit
   521  //go:nowritebarrierrec
   522  func sigaltstack(ss *stackt, oss *stackt) /* int32 */ {
   523  	sysvicall2(&libc_sigaltstack, uintptr(unsafe.Pointer(ss)), uintptr(unsafe.Pointer(oss)))
   524  }
   525  
   526  //go:nosplit
   527  //go:nowritebarrierrec
   528  func sigprocmask(how int32, set *sigset, oset *sigset) /* int32 */ {
   529  	sysvicall3(&libc_sigprocmask, uintptr(how), uintptr(unsafe.Pointer(set)), uintptr(unsafe.Pointer(oset)))
   530  }
   531  
   532  func sysconf(name int32) int64 {
   533  	return int64(sysvicall1(&libc_sysconf, uintptr(name)))
   534  }
   535  
   536  func usleep1(usec uint32)
   537  
   538  //go:nosplit
   539  func usleep_no_g(µs uint32) {
   540  	usleep1(µs)
   541  }
   542  
   543  //go:nosplit
   544  func usleep(µs uint32) {
   545  	usleep1(µs)
   546  }
   547  
   548  func walltime() (sec int64, nsec int32) {
   549  	var ts mts
   550  	sysvicall2(&libc_clock_gettime, _CLOCK_REALTIME, uintptr(unsafe.Pointer(&ts)))
   551  	return ts.tv_sec, int32(ts.tv_nsec)
   552  }
   553  
   554  //go:nosplit
   555  func write1(fd uintptr, buf unsafe.Pointer, nbyte int32) int32 {
   556  	r1, err := sysvicall3Err(&libc_write, fd, uintptr(buf), uintptr(nbyte))
   557  	if c := int32(r1); c >= 0 {
   558  		return c
   559  	}
   560  	return -int32(err)
   561  }
   562  
   563  //go:nosplit
   564  func pipe2(flags int32) (r, w int32, errno int32) {
   565  	var p [2]int32
   566  	_, e := sysvicall2Err(&libc_pipe2, uintptr(noescape(unsafe.Pointer(&p))), uintptr(flags))
   567  	return p[0], p[1], int32(e)
   568  }
   569  
   570  //go:nosplit
   571  func closeonexec(fd int32) {
   572  	fcntl(fd, _F_SETFD, _FD_CLOEXEC)
   573  }
   574  
   575  func osyield1()
   576  
   577  //go:nosplit
   578  func osyield_no_g() {
   579  	osyield1()
   580  }
   581  
   582  //go:nosplit
   583  func osyield() {
   584  	sysvicall0(&libc_sched_yield)
   585  }
   586  
   587  //go:linkname executablePath os.executablePath
   588  var executablePath string
   589  
   590  func sysargs(argc int32, argv **byte) {
   591  	n := argc + 1
   592  
   593  	// skip over argv, envp to get to auxv
   594  	for argv_index(argv, n) != nil {
   595  		n++
   596  	}
   597  
   598  	// skip NULL separator
   599  	n++
   600  
   601  	// now argv+n is auxv
   602  	auxv := (*[1 << 28]uintptr)(add(unsafe.Pointer(argv), uintptr(n)*goarch.PtrSize))
   603  	sysauxv(auxv[:])
   604  }
   605  
   606  const (
   607  	_AT_NULL         = 0    // Terminates the vector
   608  	_AT_PAGESZ       = 6    // Page size in bytes
   609  	_AT_SUN_EXECNAME = 2014 // exec() path name
   610  )
   611  
   612  func sysauxv(auxv []uintptr) {
   613  	for i := 0; auxv[i] != _AT_NULL; i += 2 {
   614  		tag, val := auxv[i], auxv[i+1]
   615  		switch tag {
   616  		case _AT_PAGESZ:
   617  			physPageSize = val
   618  		case _AT_SUN_EXECNAME:
   619  			executablePath = gostringnocopy((*byte)(unsafe.Pointer(val)))
   620  		}
   621  	}
   622  }
   623  
   624  // sigPerThreadSyscall is only used on linux, so we assign a bogus signal
   625  // number.
   626  const sigPerThreadSyscall = 1 << 31
   627  
   628  //go:nosplit
   629  func runPerThreadSyscall() {
   630  	throw("runPerThreadSyscall only valid on linux")
   631  }
   632  

View as plain text