Source file src/runtime/os2_aix.go

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

View as plain text