github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/runtime/os2_aix.go (about)

     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  // This file contains main runtime AIX syscalls.
     6  // Pollset syscalls are in netpoll_aix.go.
     7  // The implementation is based on Solaris and Windows.
     8  // Each syscall is made by calling its libc symbol using asmcgocall and asmsyscall6
     9  // assembly functions.
    10  
    11  package runtime
    12  
    13  import (
    14  	"unsafe"
    15  )
    16  
    17  // Symbols imported for __start function.
    18  
    19  //go:cgo_import_dynamic libc___n_pthreads __n_pthreads "libpthread.a/shr_xpg5_64.o"
    20  //go:cgo_import_dynamic libc___mod_init __mod_init "libc.a/shr_64.o"
    21  //go:linkname libc___n_pthreads libc___n_pthread
    22  //go:linkname libc___mod_init libc___mod_init
    23  
    24  var (
    25  	libc___n_pthread,
    26  	libc___mod_init libFunc
    27  )
    28  
    29  // Syscalls
    30  
    31  //go:cgo_import_dynamic libc__Errno _Errno "libc.a/shr_64.o"
    32  //go:cgo_import_dynamic libc_clock_gettime clock_gettime "libc.a/shr_64.o"
    33  //go:cgo_import_dynamic libc_close close "libc.a/shr_64.o"
    34  //go:cgo_import_dynamic libc_exit exit "libc.a/shr_64.o"
    35  //go:cgo_import_dynamic libc_getpid getpid "libc.a/shr_64.o"
    36  //go:cgo_import_dynamic libc_getsystemcfg getsystemcfg "libc.a/shr_64.o"
    37  //go:cgo_import_dynamic libc_kill kill "libc.a/shr_64.o"
    38  //go:cgo_import_dynamic libc_madvise madvise "libc.a/shr_64.o"
    39  //go:cgo_import_dynamic libc_malloc malloc "libc.a/shr_64.o"
    40  //go:cgo_import_dynamic libc_mmap mmap "libc.a/shr_64.o"
    41  //go:cgo_import_dynamic libc_mprotect mprotect "libc.a/shr_64.o"
    42  //go:cgo_import_dynamic libc_munmap munmap "libc.a/shr_64.o"
    43  //go:cgo_import_dynamic libc_open open "libc.a/shr_64.o"
    44  //go:cgo_import_dynamic libc_pipe pipe "libc.a/shr_64.o"
    45  //go:cgo_import_dynamic libc_raise raise "libc.a/shr_64.o"
    46  //go:cgo_import_dynamic libc_read read "libc.a/shr_64.o"
    47  //go:cgo_import_dynamic libc_sched_yield sched_yield "libc.a/shr_64.o"
    48  //go:cgo_import_dynamic libc_sem_init sem_init "libc.a/shr_64.o"
    49  //go:cgo_import_dynamic libc_sem_post sem_post "libc.a/shr_64.o"
    50  //go:cgo_import_dynamic libc_sem_timedwait sem_timedwait "libc.a/shr_64.o"
    51  //go:cgo_import_dynamic libc_sem_wait sem_wait "libc.a/shr_64.o"
    52  //go:cgo_import_dynamic libc_setitimer setitimer "libc.a/shr_64.o"
    53  //go:cgo_import_dynamic libc_sigaction sigaction "libc.a/shr_64.o"
    54  //go:cgo_import_dynamic libc_sigaltstack sigaltstack "libc.a/shr_64.o"
    55  //go:cgo_import_dynamic libc_sysconf sysconf "libc.a/shr_64.o"
    56  //go:cgo_import_dynamic libc_usleep usleep "libc.a/shr_64.o"
    57  //go:cgo_import_dynamic libc_write write "libc.a/shr_64.o"
    58  
    59  //go:cgo_import_dynamic libpthread___pth_init __pth_init "libpthread.a/shr_xpg5_64.o"
    60  //go:cgo_import_dynamic libpthread_attr_destroy pthread_attr_destroy "libpthread.a/shr_xpg5_64.o"
    61  //go:cgo_import_dynamic libpthread_attr_init pthread_attr_init "libpthread.a/shr_xpg5_64.o"
    62  //go:cgo_import_dynamic libpthread_attr_getstacksize pthread_attr_getstacksize "libpthread.a/shr_xpg5_64.o"
    63  //go:cgo_import_dynamic libpthread_attr_setstacksize pthread_attr_setstacksize "libpthread.a/shr_xpg5_64.o"
    64  //go:cgo_import_dynamic libpthread_attr_setdetachstate pthread_attr_setdetachstate "libpthread.a/shr_xpg5_64.o"
    65  //go:cgo_import_dynamic libpthread_attr_setstackaddr pthread_attr_setstackaddr "libpthread.a/shr_xpg5_64.o"
    66  //go:cgo_import_dynamic libpthread_create pthread_create "libpthread.a/shr_xpg5_64.o"
    67  //go:cgo_import_dynamic libpthread_sigthreadmask sigthreadmask "libpthread.a/shr_xpg5_64.o"
    68  //go:cgo_import_dynamic libpthread_self pthread_self "libpthread.a/shr_xpg5_64.o"
    69  //go:cgo_import_dynamic libpthread_kill pthread_kill "libpthread.a/shr_xpg5_64.o"
    70  
    71  //go:linkname libc__Errno libc__Errno
    72  //go:linkname libc_clock_gettime libc_clock_gettime
    73  //go:linkname libc_close libc_close
    74  //go:linkname libc_exit libc_exit
    75  //go:linkname libc_getpid libc_getpid
    76  //go:linkname libc_getsystemcfg libc_getsystemcfg
    77  //go:linkname libc_kill libc_kill
    78  //go:linkname libc_madvise libc_madvise
    79  //go:linkname libc_malloc libc_malloc
    80  //go:linkname libc_mmap libc_mmap
    81  //go:linkname libc_mprotect libc_mprotect
    82  //go:linkname libc_munmap libc_munmap
    83  //go:linkname libc_open libc_open
    84  //go:linkname libc_pipe libc_pipe
    85  //go:linkname libc_raise libc_raise
    86  //go:linkname libc_read libc_read
    87  //go:linkname libc_sched_yield libc_sched_yield
    88  //go:linkname libc_sem_init libc_sem_init
    89  //go:linkname libc_sem_post libc_sem_post
    90  //go:linkname libc_sem_timedwait libc_sem_timedwait
    91  //go:linkname libc_sem_wait libc_sem_wait
    92  //go:linkname libc_setitimer libc_setitimer
    93  //go:linkname libc_sigaction libc_sigaction
    94  //go:linkname libc_sigaltstack libc_sigaltstack
    95  //go:linkname libc_sysconf libc_sysconf
    96  //go:linkname libc_usleep libc_usleep
    97  //go:linkname libc_write libc_write
    98  
    99  //go:linkname libpthread___pth_init libpthread___pth_init
   100  //go:linkname libpthread_attr_destroy libpthread_attr_destroy
   101  //go:linkname libpthread_attr_init libpthread_attr_init
   102  //go:linkname libpthread_attr_getstacksize libpthread_attr_getstacksize
   103  //go:linkname libpthread_attr_setstacksize libpthread_attr_setstacksize
   104  //go:linkname libpthread_attr_setdetachstate libpthread_attr_setdetachstate
   105  //go:linkname libpthread_attr_setstackaddr libpthread_attr_setstackaddr
   106  //go:linkname libpthread_create libpthread_create
   107  //go:linkname libpthread_sigthreadmask libpthread_sigthreadmask
   108  //go:linkname libpthread_self libpthread_self
   109  //go:linkname libpthread_kill libpthread_kill
   110  
   111  var (
   112  	//libc
   113  	libc__Errno,
   114  	libc_clock_gettime,
   115  	libc_close,
   116  	libc_exit,
   117  	libc_getpid,
   118  	libc_getsystemcfg,
   119  	libc_kill,
   120  	libc_madvise,
   121  	libc_malloc,
   122  	libc_mmap,
   123  	libc_mprotect,
   124  	libc_munmap,
   125  	libc_open,
   126  	libc_pipe,
   127  	libc_raise,
   128  	libc_read,
   129  	libc_sched_yield,
   130  	libc_sem_init,
   131  	libc_sem_post,
   132  	libc_sem_timedwait,
   133  	libc_sem_wait,
   134  	libc_setitimer,
   135  	libc_sigaction,
   136  	libc_sigaltstack,
   137  	libc_sysconf,
   138  	libc_usleep,
   139  	libc_write,
   140  	//libpthread
   141  	libpthread___pth_init,
   142  	libpthread_attr_destroy,
   143  	libpthread_attr_init,
   144  	libpthread_attr_getstacksize,
   145  	libpthread_attr_setstacksize,
   146  	libpthread_attr_setdetachstate,
   147  	libpthread_attr_setstackaddr,
   148  	libpthread_create,
   149  	libpthread_sigthreadmask,
   150  	libpthread_self,
   151  	libpthread_kill libFunc
   152  )
   153  
   154  type libFunc uintptr
   155  
   156  // asmsyscall6 calls the libc symbol using a C convention.
   157  // It's defined in sys_aix_ppc64.go.
   158  var asmsyscall6 libFunc
   159  
   160  // syscallX functions must always be called with g != nil and m != nil,
   161  // as it relies on g.m.libcall to pass arguments to asmcgocall.
   162  // The few cases where syscalls haven't a g or a m must call their equivalent
   163  // function in sys_aix_ppc64.s to handle them.
   164  
   165  //go:nowritebarrier
   166  //go:nosplit
   167  func syscall0(fn *libFunc) (r, err uintptr) {
   168  	gp := getg()
   169  	mp := gp.m
   170  	resetLibcall := true
   171  	if mp.libcallsp == 0 {
   172  		mp.libcallg.set(gp)
   173  		mp.libcallpc = getcallerpc()
   174  		// sp must be the last, because once async cpu profiler finds
   175  		// all three values to be non-zero, it will use them
   176  		mp.libcallsp = getcallersp()
   177  	} else {
   178  		resetLibcall = false // See comment in sys_darwin.go:libcCall
   179  	}
   180  
   181  	c := libcall{
   182  		fn:   uintptr(unsafe.Pointer(fn)),
   183  		n:    0,
   184  		args: uintptr(unsafe.Pointer(&fn)), // it's unused but must be non-nil, otherwise crashes
   185  	}
   186  
   187  	asmcgocall(unsafe.Pointer(&asmsyscall6), unsafe.Pointer(&c))
   188  
   189  	if resetLibcall {
   190  		mp.libcallsp = 0
   191  	}
   192  
   193  	return c.r1, c.err
   194  }
   195  
   196  //go:nowritebarrier
   197  //go:nosplit
   198  func syscall1(fn *libFunc, a0 uintptr) (r, err uintptr) {
   199  	gp := getg()
   200  	mp := gp.m
   201  	resetLibcall := true
   202  	if mp.libcallsp == 0 {
   203  		mp.libcallg.set(gp)
   204  		mp.libcallpc = getcallerpc()
   205  		// sp must be the last, because once async cpu profiler finds
   206  		// all three values to be non-zero, it will use them
   207  		mp.libcallsp = getcallersp()
   208  	} else {
   209  		resetLibcall = false // See comment in sys_darwin.go:libcCall
   210  	}
   211  
   212  	c := libcall{
   213  		fn:   uintptr(unsafe.Pointer(fn)),
   214  		n:    1,
   215  		args: uintptr(unsafe.Pointer(&a0)),
   216  	}
   217  
   218  	asmcgocall(unsafe.Pointer(&asmsyscall6), unsafe.Pointer(&c))
   219  
   220  	if resetLibcall {
   221  		mp.libcallsp = 0
   222  	}
   223  
   224  	return c.r1, c.err
   225  }
   226  
   227  //go:nowritebarrier
   228  //go:nosplit
   229  //go:cgo_unsafe_args
   230  func syscall2(fn *libFunc, a0, a1 uintptr) (r, err uintptr) {
   231  	gp := getg()
   232  	mp := gp.m
   233  	resetLibcall := true
   234  	if mp.libcallsp == 0 {
   235  		mp.libcallg.set(gp)
   236  		mp.libcallpc = getcallerpc()
   237  		// sp must be the last, because once async cpu profiler finds
   238  		// all three values to be non-zero, it will use them
   239  		mp.libcallsp = getcallersp()
   240  	} else {
   241  		resetLibcall = false // See comment in sys_darwin.go:libcCall
   242  	}
   243  
   244  	c := libcall{
   245  		fn:   uintptr(unsafe.Pointer(fn)),
   246  		n:    2,
   247  		args: uintptr(unsafe.Pointer(&a0)),
   248  	}
   249  
   250  	asmcgocall(unsafe.Pointer(&asmsyscall6), unsafe.Pointer(&c))
   251  
   252  	if resetLibcall {
   253  		mp.libcallsp = 0
   254  	}
   255  
   256  	return c.r1, c.err
   257  }
   258  
   259  //go:nowritebarrier
   260  //go:nosplit
   261  //go:cgo_unsafe_args
   262  func syscall3(fn *libFunc, a0, a1, a2 uintptr) (r, err uintptr) {
   263  	gp := getg()
   264  	mp := gp.m
   265  	resetLibcall := true
   266  	if mp.libcallsp == 0 {
   267  		mp.libcallg.set(gp)
   268  		mp.libcallpc = getcallerpc()
   269  		// sp must be the last, because once async cpu profiler finds
   270  		// all three values to be non-zero, it will use them
   271  		mp.libcallsp = getcallersp()
   272  	} else {
   273  		resetLibcall = false // See comment in sys_darwin.go:libcCall
   274  	}
   275  
   276  	c := libcall{
   277  		fn:   uintptr(unsafe.Pointer(fn)),
   278  		n:    3,
   279  		args: uintptr(unsafe.Pointer(&a0)),
   280  	}
   281  
   282  	asmcgocall(unsafe.Pointer(&asmsyscall6), unsafe.Pointer(&c))
   283  
   284  	if resetLibcall {
   285  		mp.libcallsp = 0
   286  	}
   287  
   288  	return c.r1, c.err
   289  }
   290  
   291  //go:nowritebarrier
   292  //go:nosplit
   293  //go:cgo_unsafe_args
   294  func syscall4(fn *libFunc, a0, a1, a2, a3 uintptr) (r, err uintptr) {
   295  	gp := getg()
   296  	mp := gp.m
   297  	resetLibcall := true
   298  	if mp.libcallsp == 0 {
   299  		mp.libcallg.set(gp)
   300  		mp.libcallpc = getcallerpc()
   301  		// sp must be the last, because once async cpu profiler finds
   302  		// all three values to be non-zero, it will use them
   303  		mp.libcallsp = getcallersp()
   304  	} else {
   305  		resetLibcall = false // See comment in sys_darwin.go:libcCall
   306  	}
   307  
   308  	c := libcall{
   309  		fn:   uintptr(unsafe.Pointer(fn)),
   310  		n:    4,
   311  		args: uintptr(unsafe.Pointer(&a0)),
   312  	}
   313  
   314  	asmcgocall(unsafe.Pointer(&asmsyscall6), unsafe.Pointer(&c))
   315  
   316  	if resetLibcall {
   317  		mp.libcallsp = 0
   318  	}
   319  
   320  	return c.r1, c.err
   321  }
   322  
   323  //go:nowritebarrier
   324  //go:nosplit
   325  //go:cgo_unsafe_args
   326  func syscall5(fn *libFunc, a0, a1, a2, a3, a4 uintptr) (r, err uintptr) {
   327  	gp := getg()
   328  	mp := gp.m
   329  	resetLibcall := true
   330  	if mp.libcallsp == 0 {
   331  		mp.libcallg.set(gp)
   332  		mp.libcallpc = getcallerpc()
   333  		// sp must be the last, because once async cpu profiler finds
   334  		// all three values to be non-zero, it will use them
   335  		mp.libcallsp = getcallersp()
   336  	} else {
   337  		resetLibcall = false // See comment in sys_darwin.go:libcCall
   338  	}
   339  
   340  	c := libcall{
   341  		fn:   uintptr(unsafe.Pointer(fn)),
   342  		n:    5,
   343  		args: uintptr(unsafe.Pointer(&a0)),
   344  	}
   345  
   346  	asmcgocall(unsafe.Pointer(&asmsyscall6), unsafe.Pointer(&c))
   347  
   348  	if resetLibcall {
   349  		mp.libcallsp = 0
   350  	}
   351  
   352  	return c.r1, c.err
   353  }
   354  
   355  //go:nowritebarrier
   356  //go:nosplit
   357  //go:cgo_unsafe_args
   358  func syscall6(fn *libFunc, a0, a1, a2, a3, a4, a5 uintptr) (r, err uintptr) {
   359  	gp := getg()
   360  	mp := gp.m
   361  	resetLibcall := true
   362  	if mp.libcallsp == 0 {
   363  		mp.libcallg.set(gp)
   364  		mp.libcallpc = getcallerpc()
   365  		// sp must be the last, because once async cpu profiler finds
   366  		// all three values to be non-zero, it will use them
   367  		mp.libcallsp = getcallersp()
   368  	} else {
   369  		resetLibcall = false // See comment in sys_darwin.go:libcCall
   370  	}
   371  
   372  	c := libcall{
   373  		fn:   uintptr(unsafe.Pointer(fn)),
   374  		n:    6,
   375  		args: uintptr(unsafe.Pointer(&a0)),
   376  	}
   377  
   378  	asmcgocall(unsafe.Pointer(&asmsyscall6), unsafe.Pointer(&c))
   379  
   380  	if resetLibcall {
   381  		mp.libcallsp = 0
   382  	}
   383  
   384  	return c.r1, c.err
   385  }
   386  
   387  func exit1(code int32)
   388  
   389  //go:nosplit
   390  func exit(code int32) {
   391  	_g_ := getg()
   392  
   393  	// Check the validity of g because without a g during
   394  	// newosproc0.
   395  	if _g_ != nil {
   396  		syscall1(&libc_exit, uintptr(code))
   397  		return
   398  	}
   399  	exit1(code)
   400  }
   401  
   402  func write2(fd, p uintptr, n int32) int32
   403  
   404  //go:nosplit
   405  func write1(fd uintptr, p unsafe.Pointer, n int32) int32 {
   406  	_g_ := getg()
   407  
   408  	// Check the validity of g because without a g during
   409  	// newosproc0.
   410  	if _g_ != nil {
   411  		r, errno := syscall3(&libc_write, uintptr(fd), uintptr(p), uintptr(n))
   412  		if int32(r) < 0 {
   413  			return -int32(errno)
   414  		}
   415  		return int32(r)
   416  	}
   417  	// Note that in this case we can't return a valid errno value.
   418  	return write2(fd, uintptr(p), n)
   419  
   420  }
   421  
   422  //go:nosplit
   423  func read(fd int32, p unsafe.Pointer, n int32) int32 {
   424  	r, errno := syscall3(&libc_read, uintptr(fd), uintptr(p), uintptr(n))
   425  	if int32(r) < 0 {
   426  		return -int32(errno)
   427  	}
   428  	return int32(r)
   429  }
   430  
   431  //go:nosplit
   432  func open(name *byte, mode, perm int32) int32 {
   433  	r, _ := syscall3(&libc_open, uintptr(unsafe.Pointer(name)), uintptr(mode), uintptr(perm))
   434  	return int32(r)
   435  }
   436  
   437  //go:nosplit
   438  func closefd(fd int32) int32 {
   439  	r, _ := syscall1(&libc_close, uintptr(fd))
   440  	return int32(r)
   441  }
   442  
   443  //go:nosplit
   444  func pipe() (r, w int32, errno int32) {
   445  	var p [2]int32
   446  	_, err := syscall1(&libc_pipe, uintptr(noescape(unsafe.Pointer(&p[0]))))
   447  	return p[0], p[1], int32(err)
   448  }
   449  
   450  // mmap calls the mmap system call.
   451  // We only pass the lower 32 bits of file offset to the
   452  // assembly routine; the higher bits (if required), should be provided
   453  // by the assembly routine as 0.
   454  // The err result is an OS error code such as ENOMEM.
   455  //go:nosplit
   456  func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (unsafe.Pointer, int) {
   457  	r, err0 := syscall6(&libc_mmap, uintptr(addr), uintptr(n), uintptr(prot), uintptr(flags), uintptr(fd), uintptr(off))
   458  	if r == ^uintptr(0) {
   459  		return nil, int(err0)
   460  	}
   461  	return unsafe.Pointer(r), int(err0)
   462  }
   463  
   464  //go:nosplit
   465  func mprotect(addr unsafe.Pointer, n uintptr, prot int32) (unsafe.Pointer, int) {
   466  	r, err0 := syscall3(&libc_mprotect, uintptr(addr), uintptr(n), uintptr(prot))
   467  	if r == ^uintptr(0) {
   468  		return nil, int(err0)
   469  	}
   470  	return unsafe.Pointer(r), int(err0)
   471  }
   472  
   473  //go:nosplit
   474  func munmap(addr unsafe.Pointer, n uintptr) {
   475  	r, err := syscall2(&libc_munmap, uintptr(addr), uintptr(n))
   476  	if int32(r) == -1 {
   477  		println("syscall munmap failed: ", hex(err))
   478  		throw("syscall munmap")
   479  	}
   480  }
   481  
   482  //go:nosplit
   483  func madvise(addr unsafe.Pointer, n uintptr, flags int32) {
   484  	r, err := syscall3(&libc_madvise, uintptr(addr), uintptr(n), uintptr(flags))
   485  	if int32(r) == -1 {
   486  		println("syscall madvise failed: ", hex(err))
   487  		throw("syscall madvise")
   488  	}
   489  }
   490  
   491  func sigaction1(sig, new, old uintptr)
   492  
   493  //go:nosplit
   494  func sigaction(sig uintptr, new, old *sigactiont) {
   495  	_g_ := getg()
   496  
   497  	// Check the validity of g because without a g during
   498  	// runtime.libpreinit.
   499  	if _g_ != nil {
   500  		r, err := syscall3(&libc_sigaction, sig, uintptr(unsafe.Pointer(new)), uintptr(unsafe.Pointer(old)))
   501  		if int32(r) == -1 {
   502  			println("Sigaction failed for sig: ", sig, " with error:", hex(err))
   503  			throw("syscall sigaction")
   504  		}
   505  		return
   506  	}
   507  
   508  	sigaction1(sig, uintptr(unsafe.Pointer(new)), uintptr(unsafe.Pointer(old)))
   509  }
   510  
   511  //go:nosplit
   512  func sigaltstack(new, old *stackt) {
   513  	r, err := syscall2(&libc_sigaltstack, uintptr(unsafe.Pointer(new)), uintptr(unsafe.Pointer(old)))
   514  	if int32(r) == -1 {
   515  		println("syscall sigaltstack failed: ", hex(err))
   516  		throw("syscall sigaltstack")
   517  	}
   518  }
   519  
   520  //go:nosplit
   521  //go:linkname internal_cpu_getsystemcfg internal/cpu.getsystemcfg
   522  func internal_cpu_getsystemcfg(label uint) uint {
   523  	r, _ := syscall1(&libc_getsystemcfg, uintptr(label))
   524  	return uint(r)
   525  }
   526  
   527  func usleep1(us uint32)
   528  
   529  //go:nosplit
   530  func usleep(us uint32) {
   531  	_g_ := getg()
   532  
   533  	// Check the validity of m because we might be called in cgo callback
   534  	// path early enough where there isn't a g or a m available yet.
   535  	if _g_ != nil && _g_.m != nil {
   536  		r, err := syscall1(&libc_usleep, uintptr(us))
   537  		if int32(r) == -1 {
   538  			println("syscall usleep failed: ", hex(err))
   539  			throw("syscall usleep")
   540  		}
   541  		return
   542  	}
   543  	usleep1(us)
   544  }
   545  
   546  //go:nosplit
   547  func clock_gettime(clockid int32, tp *timespec) int32 {
   548  	r, _ := syscall2(&libc_clock_gettime, uintptr(clockid), uintptr(unsafe.Pointer(tp)))
   549  	return int32(r)
   550  }
   551  
   552  //go:nosplit
   553  func setitimer(mode int32, new, old *itimerval) {
   554  	r, err := syscall3(&libc_setitimer, uintptr(mode), uintptr(unsafe.Pointer(new)), uintptr(unsafe.Pointer(old)))
   555  	if int32(r) == -1 {
   556  		println("syscall setitimer failed: ", hex(err))
   557  		throw("syscall setitimer")
   558  	}
   559  }
   560  
   561  //go:nosplit
   562  func malloc(size uintptr) unsafe.Pointer {
   563  	r, _ := syscall1(&libc_malloc, size)
   564  	return unsafe.Pointer(r)
   565  }
   566  
   567  //go:nosplit
   568  func sem_init(sem *semt, pshared int32, value uint32) int32 {
   569  	r, _ := syscall3(&libc_sem_init, uintptr(unsafe.Pointer(sem)), uintptr(pshared), uintptr(value))
   570  	return int32(r)
   571  }
   572  
   573  //go:nosplit
   574  func sem_wait(sem *semt) (int32, int32) {
   575  	r, err := syscall1(&libc_sem_wait, uintptr(unsafe.Pointer(sem)))
   576  	return int32(r), int32(err)
   577  }
   578  
   579  //go:nosplit
   580  func sem_post(sem *semt) int32 {
   581  	r, _ := syscall1(&libc_sem_post, uintptr(unsafe.Pointer(sem)))
   582  	return int32(r)
   583  }
   584  
   585  //go:nosplit
   586  func sem_timedwait(sem *semt, timeout *timespec) (int32, int32) {
   587  	r, err := syscall2(&libc_sem_timedwait, uintptr(unsafe.Pointer(sem)), uintptr(unsafe.Pointer(timeout)))
   588  	return int32(r), int32(err)
   589  }
   590  
   591  //go:nosplit
   592  func raise(sig uint32) {
   593  	r, err := syscall1(&libc_raise, uintptr(sig))
   594  	if int32(r) == -1 {
   595  		println("syscall raise failed: ", hex(err))
   596  		throw("syscall raise")
   597  	}
   598  }
   599  
   600  //go:nosplit
   601  func raiseproc(sig uint32) {
   602  	pid, err := syscall0(&libc_getpid)
   603  	if int32(pid) == -1 {
   604  		println("syscall getpid failed: ", hex(err))
   605  		throw("syscall raiseproc")
   606  	}
   607  
   608  	syscall2(&libc_kill, pid, uintptr(sig))
   609  }
   610  
   611  func osyield1()
   612  
   613  //go:nosplit
   614  func osyield() {
   615  	_g_ := getg()
   616  
   617  	// Check the validity of m because it might be called during a cgo
   618  	// callback early enough where m isn't available yet.
   619  	if _g_ != nil && _g_.m != nil {
   620  		r, err := syscall0(&libc_sched_yield)
   621  		if int32(r) == -1 {
   622  			println("syscall osyield failed: ", hex(err))
   623  			throw("syscall osyield")
   624  		}
   625  		return
   626  	}
   627  	osyield1()
   628  }
   629  
   630  //go:nosplit
   631  func sysconf(name int32) uintptr {
   632  	r, _ := syscall1(&libc_sysconf, uintptr(name))
   633  	if int32(r) == -1 {
   634  		throw("syscall sysconf")
   635  	}
   636  	return r
   637  
   638  }
   639  
   640  // pthread functions returns its error code in the main return value
   641  // Therefore, err returns by syscall means nothing and must not be used
   642  
   643  //go:nosplit
   644  func pthread_attr_destroy(attr *pthread_attr) int32 {
   645  	r, _ := syscall1(&libpthread_attr_destroy, uintptr(unsafe.Pointer(attr)))
   646  	return int32(r)
   647  }
   648  
   649  func pthread_attr_init1(attr uintptr) int32
   650  
   651  //go:nosplit
   652  func pthread_attr_init(attr *pthread_attr) int32 {
   653  	_g_ := getg()
   654  
   655  	// Check the validity of g because without a g during
   656  	// newosproc0.
   657  	if _g_ != nil {
   658  		r, _ := syscall1(&libpthread_attr_init, uintptr(unsafe.Pointer(attr)))
   659  		return int32(r)
   660  	}
   661  
   662  	return pthread_attr_init1(uintptr(unsafe.Pointer(attr)))
   663  }
   664  
   665  func pthread_attr_setdetachstate1(attr uintptr, state int32) int32
   666  
   667  //go:nosplit
   668  func pthread_attr_setdetachstate(attr *pthread_attr, state int32) int32 {
   669  	_g_ := getg()
   670  
   671  	// Check the validity of g because without a g during
   672  	// newosproc0.
   673  	if _g_ != nil {
   674  		r, _ := syscall2(&libpthread_attr_setdetachstate, uintptr(unsafe.Pointer(attr)), uintptr(state))
   675  		return int32(r)
   676  	}
   677  
   678  	return pthread_attr_setdetachstate1(uintptr(unsafe.Pointer(attr)), state)
   679  }
   680  
   681  //go:nosplit
   682  func pthread_attr_setstackaddr(attr *pthread_attr, stk unsafe.Pointer) int32 {
   683  	r, _ := syscall2(&libpthread_attr_setstackaddr, uintptr(unsafe.Pointer(attr)), uintptr(stk))
   684  	return int32(r)
   685  }
   686  
   687  //go:nosplit
   688  func pthread_attr_getstacksize(attr *pthread_attr, size *uint64) int32 {
   689  	r, _ := syscall2(&libpthread_attr_getstacksize, uintptr(unsafe.Pointer(attr)), uintptr(unsafe.Pointer(size)))
   690  	return int32(r)
   691  }
   692  
   693  func pthread_attr_setstacksize1(attr uintptr, size uint64) int32
   694  
   695  //go:nosplit
   696  func pthread_attr_setstacksize(attr *pthread_attr, size uint64) int32 {
   697  	_g_ := getg()
   698  
   699  	// Check the validity of g because without a g during
   700  	// newosproc0.
   701  	if _g_ != nil {
   702  		r, _ := syscall2(&libpthread_attr_setstacksize, uintptr(unsafe.Pointer(attr)), uintptr(size))
   703  		return int32(r)
   704  	}
   705  
   706  	return pthread_attr_setstacksize1(uintptr(unsafe.Pointer(attr)), size)
   707  }
   708  
   709  func pthread_create1(tid, attr, fn, arg uintptr) int32
   710  
   711  //go:nosplit
   712  func pthread_create(tid *pthread, attr *pthread_attr, fn *funcDescriptor, arg unsafe.Pointer) int32 {
   713  	_g_ := getg()
   714  
   715  	// Check the validity of g because without a g during
   716  	// newosproc0.
   717  	if _g_ != nil {
   718  		r, _ := syscall4(&libpthread_create, uintptr(unsafe.Pointer(tid)), uintptr(unsafe.Pointer(attr)), uintptr(unsafe.Pointer(fn)), uintptr(arg))
   719  		return int32(r)
   720  	}
   721  
   722  	return pthread_create1(uintptr(unsafe.Pointer(tid)), uintptr(unsafe.Pointer(attr)), uintptr(unsafe.Pointer(fn)), uintptr(arg))
   723  }
   724  
   725  // On multi-thread program, sigprocmask must not be called.
   726  // It's replaced by sigthreadmask.
   727  func sigprocmask1(how, new, old uintptr)
   728  
   729  //go:nosplit
   730  func sigprocmask(how int32, new, old *sigset) {
   731  	_g_ := getg()
   732  
   733  	// Check the validity of m because it might be called during a cgo
   734  	// callback early enough where m isn't available yet.
   735  	if _g_ != nil && _g_.m != nil {
   736  		r, err := syscall3(&libpthread_sigthreadmask, uintptr(how), uintptr(unsafe.Pointer(new)), uintptr(unsafe.Pointer(old)))
   737  		if int32(r) != 0 {
   738  			println("syscall sigthreadmask failed: ", hex(err))
   739  			throw("syscall sigthreadmask")
   740  		}
   741  		return
   742  	}
   743  	sigprocmask1(uintptr(how), uintptr(unsafe.Pointer(new)), uintptr(unsafe.Pointer(old)))
   744  
   745  }
   746  
   747  //go:nosplit
   748  func pthread_self() pthread {
   749  	r, _ := syscall0(&libpthread_self)
   750  	return pthread(r)
   751  }
   752  
   753  //go:nosplit
   754  func signalM(mp *m, sig int) {
   755  	syscall2(&libpthread_kill, uintptr(pthread(mp.procid)), uintptr(sig))
   756  }