Black Lives Matter. Support the Equal Justice Initiative.

Source file src/runtime/os2_aix.go

Documentation: runtime

     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_pthreads
    22  //go:linkname libc___mod_init libc___mod_init
    23  
    24  var (
    25  	libc___n_pthreads,
    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_no_g(us uint32) {
   531  	usleep1(us)
   532  }
   533  
   534  //go:nosplit
   535  func usleep(us uint32) {
   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  }
   542  
   543  //go:nosplit
   544  func clock_gettime(clockid int32, tp *timespec) int32 {
   545  	r, _ := syscall2(&libc_clock_gettime, uintptr(clockid), uintptr(unsafe.Pointer(tp)))
   546  	return int32(r)
   547  }
   548  
   549  //go:nosplit
   550  func setitimer(mode int32, new, old *itimerval) {
   551  	r, err := syscall3(&libc_setitimer, uintptr(mode), uintptr(unsafe.Pointer(new)), uintptr(unsafe.Pointer(old)))
   552  	if int32(r) == -1 {
   553  		println("syscall setitimer failed: ", hex(err))
   554  		throw("syscall setitimer")
   555  	}
   556  }
   557  
   558  //go:nosplit
   559  func malloc(size uintptr) unsafe.Pointer {
   560  	r, _ := syscall1(&libc_malloc, size)
   561  	return unsafe.Pointer(r)
   562  }
   563  
   564  //go:nosplit
   565  func sem_init(sem *semt, pshared int32, value uint32) int32 {
   566  	r, _ := syscall3(&libc_sem_init, uintptr(unsafe.Pointer(sem)), uintptr(pshared), uintptr(value))
   567  	return int32(r)
   568  }
   569  
   570  //go:nosplit
   571  func sem_wait(sem *semt) (int32, int32) {
   572  	r, err := syscall1(&libc_sem_wait, uintptr(unsafe.Pointer(sem)))
   573  	return int32(r), int32(err)
   574  }
   575  
   576  //go:nosplit
   577  func sem_post(sem *semt) int32 {
   578  	r, _ := syscall1(&libc_sem_post, uintptr(unsafe.Pointer(sem)))
   579  	return int32(r)
   580  }
   581  
   582  //go:nosplit
   583  func sem_timedwait(sem *semt, timeout *timespec) (int32, int32) {
   584  	r, err := syscall2(&libc_sem_timedwait, uintptr(unsafe.Pointer(sem)), uintptr(unsafe.Pointer(timeout)))
   585  	return int32(r), int32(err)
   586  }
   587  
   588  //go:nosplit
   589  func raise(sig uint32) {
   590  	r, err := syscall1(&libc_raise, uintptr(sig))
   591  	if int32(r) == -1 {
   592  		println("syscall raise failed: ", hex(err))
   593  		throw("syscall raise")
   594  	}
   595  }
   596  
   597  //go:nosplit
   598  func raiseproc(sig uint32) {
   599  	pid, err := syscall0(&libc_getpid)
   600  	if int32(pid) == -1 {
   601  		println("syscall getpid failed: ", hex(err))
   602  		throw("syscall raiseproc")
   603  	}
   604  
   605  	syscall2(&libc_kill, pid, uintptr(sig))
   606  }
   607  
   608  func osyield1()
   609  
   610  //go:nosplit
   611  func osyield_no_g() {
   612  	osyield1()
   613  }
   614  
   615  //go:nosplit
   616  func osyield() {
   617  	r, err := syscall0(&libc_sched_yield)
   618  	if int32(r) == -1 {
   619  		println("syscall osyield failed: ", hex(err))
   620  		throw("syscall osyield")
   621  	}
   622  }
   623  
   624  //go:nosplit
   625  func sysconf(name int32) uintptr {
   626  	r, _ := syscall1(&libc_sysconf, uintptr(name))
   627  	if int32(r) == -1 {
   628  		throw("syscall sysconf")
   629  	}
   630  	return r
   631  
   632  }
   633  
   634  // pthread functions returns its error code in the main return value
   635  // Therefore, err returns by syscall means nothing and must not be used
   636  
   637  //go:nosplit
   638  func pthread_attr_destroy(attr *pthread_attr) int32 {
   639  	r, _ := syscall1(&libpthread_attr_destroy, uintptr(unsafe.Pointer(attr)))
   640  	return int32(r)
   641  }
   642  
   643  func pthread_attr_init1(attr uintptr) int32
   644  
   645  //go:nosplit
   646  func pthread_attr_init(attr *pthread_attr) int32 {
   647  	_g_ := getg()
   648  
   649  	// Check the validity of g because without a g during
   650  	// newosproc0.
   651  	if _g_ != nil {
   652  		r, _ := syscall1(&libpthread_attr_init, uintptr(unsafe.Pointer(attr)))
   653  		return int32(r)
   654  	}
   655  
   656  	return pthread_attr_init1(uintptr(unsafe.Pointer(attr)))
   657  }
   658  
   659  func pthread_attr_setdetachstate1(attr uintptr, state int32) int32
   660  
   661  //go:nosplit
   662  func pthread_attr_setdetachstate(attr *pthread_attr, state int32) int32 {
   663  	_g_ := getg()
   664  
   665  	// Check the validity of g because without a g during
   666  	// newosproc0.
   667  	if _g_ != nil {
   668  		r, _ := syscall2(&libpthread_attr_setdetachstate, uintptr(unsafe.Pointer(attr)), uintptr(state))
   669  		return int32(r)
   670  	}
   671  
   672  	return pthread_attr_setdetachstate1(uintptr(unsafe.Pointer(attr)), state)
   673  }
   674  
   675  //go:nosplit
   676  func pthread_attr_setstackaddr(attr *pthread_attr, stk unsafe.Pointer) int32 {
   677  	r, _ := syscall2(&libpthread_attr_setstackaddr, uintptr(unsafe.Pointer(attr)), uintptr(stk))
   678  	return int32(r)
   679  }
   680  
   681  //go:nosplit
   682  func pthread_attr_getstacksize(attr *pthread_attr, size *uint64) int32 {
   683  	r, _ := syscall2(&libpthread_attr_getstacksize, uintptr(unsafe.Pointer(attr)), uintptr(unsafe.Pointer(size)))
   684  	return int32(r)
   685  }
   686  
   687  func pthread_attr_setstacksize1(attr uintptr, size uint64) int32
   688  
   689  //go:nosplit
   690  func pthread_attr_setstacksize(attr *pthread_attr, size uint64) int32 {
   691  	_g_ := getg()
   692  
   693  	// Check the validity of g because without a g during
   694  	// newosproc0.
   695  	if _g_ != nil {
   696  		r, _ := syscall2(&libpthread_attr_setstacksize, uintptr(unsafe.Pointer(attr)), uintptr(size))
   697  		return int32(r)
   698  	}
   699  
   700  	return pthread_attr_setstacksize1(uintptr(unsafe.Pointer(attr)), size)
   701  }
   702  
   703  func pthread_create1(tid, attr, fn, arg uintptr) int32
   704  
   705  //go:nosplit
   706  func pthread_create(tid *pthread, attr *pthread_attr, fn *funcDescriptor, arg unsafe.Pointer) int32 {
   707  	_g_ := getg()
   708  
   709  	// Check the validity of g because without a g during
   710  	// newosproc0.
   711  	if _g_ != nil {
   712  		r, _ := syscall4(&libpthread_create, uintptr(unsafe.Pointer(tid)), uintptr(unsafe.Pointer(attr)), uintptr(unsafe.Pointer(fn)), uintptr(arg))
   713  		return int32(r)
   714  	}
   715  
   716  	return pthread_create1(uintptr(unsafe.Pointer(tid)), uintptr(unsafe.Pointer(attr)), uintptr(unsafe.Pointer(fn)), uintptr(arg))
   717  }
   718  
   719  // On multi-thread program, sigprocmask must not be called.
   720  // It's replaced by sigthreadmask.
   721  func sigprocmask1(how, new, old uintptr)
   722  
   723  //go:nosplit
   724  func sigprocmask(how int32, new, old *sigset) {
   725  	_g_ := getg()
   726  
   727  	// Check the validity of m because it might be called during a cgo
   728  	// callback early enough where m isn't available yet.
   729  	if _g_ != nil && _g_.m != nil {
   730  		r, err := syscall3(&libpthread_sigthreadmask, uintptr(how), uintptr(unsafe.Pointer(new)), uintptr(unsafe.Pointer(old)))
   731  		if int32(r) != 0 {
   732  			println("syscall sigthreadmask failed: ", hex(err))
   733  			throw("syscall sigthreadmask")
   734  		}
   735  		return
   736  	}
   737  	sigprocmask1(uintptr(how), uintptr(unsafe.Pointer(new)), uintptr(unsafe.Pointer(old)))
   738  
   739  }
   740  
   741  //go:nosplit
   742  func pthread_self() pthread {
   743  	r, _ := syscall0(&libpthread_self)
   744  	return pthread(r)
   745  }
   746  
   747  //go:nosplit
   748  func signalM(mp *m, sig int) {
   749  	syscall2(&libpthread_kill, uintptr(pthread(mp.procid)), uintptr(sig))
   750  }
   751  

View as plain text