Source file src/runtime/sys_openbsd2.go

     1  // Copyright 2020 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  //go:build openbsd && !mips64
     6  
     7  package runtime
     8  
     9  import (
    10  	"internal/abi"
    11  	"unsafe"
    12  )
    13  
    14  // This is exported via linkname to assembly in runtime/cgo.
    15  //
    16  //go:linkname exit
    17  //go:nosplit
    18  //go:cgo_unsafe_args
    19  func exit(code int32) {
    20  	libcCall(unsafe.Pointer(abi.FuncPCABI0(exit_trampoline)), unsafe.Pointer(&code))
    21  }
    22  func exit_trampoline()
    23  
    24  //go:nosplit
    25  //go:cgo_unsafe_args
    26  func getthrid() (tid int32) {
    27  	libcCall(unsafe.Pointer(abi.FuncPCABI0(getthrid_trampoline)), unsafe.Pointer(&tid))
    28  	return
    29  }
    30  func getthrid_trampoline()
    31  
    32  //go:nosplit
    33  //go:cgo_unsafe_args
    34  func raiseproc(sig uint32) {
    35  	libcCall(unsafe.Pointer(abi.FuncPCABI0(raiseproc_trampoline)), unsafe.Pointer(&sig))
    36  }
    37  func raiseproc_trampoline()
    38  
    39  //go:nosplit
    40  //go:cgo_unsafe_args
    41  func thrkill(tid int32, sig int) {
    42  	libcCall(unsafe.Pointer(abi.FuncPCABI0(thrkill_trampoline)), unsafe.Pointer(&tid))
    43  }
    44  func thrkill_trampoline()
    45  
    46  // mmap is used to do low-level memory allocation via mmap. Don't allow stack
    47  // splits, since this function (used by sysAlloc) is called in a lot of low-level
    48  // parts of the runtime and callers often assume it won't acquire any locks.
    49  //
    50  //go:nosplit
    51  func mmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (unsafe.Pointer, int) {
    52  	args := struct {
    53  		addr            unsafe.Pointer
    54  		n               uintptr
    55  		prot, flags, fd int32
    56  		off             uint32
    57  		ret1            unsafe.Pointer
    58  		ret2            int
    59  	}{addr, n, prot, flags, fd, off, nil, 0}
    60  	libcCall(unsafe.Pointer(abi.FuncPCABI0(mmap_trampoline)), unsafe.Pointer(&args))
    61  	KeepAlive(addr) // Just for consistency. Hopefully addr is not a Go address.
    62  	return args.ret1, args.ret2
    63  }
    64  func mmap_trampoline()
    65  
    66  //go:nosplit
    67  //go:cgo_unsafe_args
    68  func munmap(addr unsafe.Pointer, n uintptr) {
    69  	libcCall(unsafe.Pointer(abi.FuncPCABI0(munmap_trampoline)), unsafe.Pointer(&addr))
    70  	KeepAlive(addr) // Just for consistency. Hopefully addr is not a Go address.
    71  }
    72  func munmap_trampoline()
    73  
    74  //go:nosplit
    75  //go:cgo_unsafe_args
    76  func madvise(addr unsafe.Pointer, n uintptr, flags int32) {
    77  	libcCall(unsafe.Pointer(abi.FuncPCABI0(madvise_trampoline)), unsafe.Pointer(&addr))
    78  	KeepAlive(addr) // Just for consistency. Hopefully addr is not a Go address.
    79  }
    80  func madvise_trampoline()
    81  
    82  //go:nosplit
    83  //go:cgo_unsafe_args
    84  func open(name *byte, mode, perm int32) (ret int32) {
    85  	ret = libcCall(unsafe.Pointer(abi.FuncPCABI0(open_trampoline)), unsafe.Pointer(&name))
    86  	KeepAlive(name)
    87  	return
    88  }
    89  func open_trampoline()
    90  
    91  //go:nosplit
    92  //go:cgo_unsafe_args
    93  func closefd(fd int32) int32 {
    94  	return libcCall(unsafe.Pointer(abi.FuncPCABI0(close_trampoline)), unsafe.Pointer(&fd))
    95  }
    96  func close_trampoline()
    97  
    98  //go:nosplit
    99  //go:cgo_unsafe_args
   100  func read(fd int32, p unsafe.Pointer, n int32) int32 {
   101  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(read_trampoline)), unsafe.Pointer(&fd))
   102  	KeepAlive(p)
   103  	return ret
   104  }
   105  func read_trampoline()
   106  
   107  //go:nosplit
   108  //go:cgo_unsafe_args
   109  func write1(fd uintptr, p unsafe.Pointer, n int32) int32 {
   110  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(write_trampoline)), unsafe.Pointer(&fd))
   111  	KeepAlive(p)
   112  	return ret
   113  }
   114  func write_trampoline()
   115  
   116  func pipe2(flags int32) (r, w int32, errno int32) {
   117  	var p [2]int32
   118  	args := struct {
   119  		p     unsafe.Pointer
   120  		flags int32
   121  	}{noescape(unsafe.Pointer(&p)), flags}
   122  	errno = libcCall(unsafe.Pointer(abi.FuncPCABI0(pipe2_trampoline)), unsafe.Pointer(&args))
   123  	return p[0], p[1], errno
   124  }
   125  func pipe2_trampoline()
   126  
   127  //go:nosplit
   128  //go:cgo_unsafe_args
   129  func setitimer(mode int32, new, old *itimerval) {
   130  	libcCall(unsafe.Pointer(abi.FuncPCABI0(setitimer_trampoline)), unsafe.Pointer(&mode))
   131  	KeepAlive(new)
   132  	KeepAlive(old)
   133  }
   134  func setitimer_trampoline()
   135  
   136  //go:nosplit
   137  //go:cgo_unsafe_args
   138  func usleep(usec uint32) {
   139  	libcCall(unsafe.Pointer(abi.FuncPCABI0(usleep_trampoline)), unsafe.Pointer(&usec))
   140  }
   141  func usleep_trampoline()
   142  
   143  //go:nosplit
   144  //go:cgo_unsafe_args
   145  func usleep_no_g(usec uint32) {
   146  	asmcgocall_no_g(unsafe.Pointer(abi.FuncPCABI0(usleep_trampoline)), unsafe.Pointer(&usec))
   147  }
   148  
   149  //go:nosplit
   150  //go:cgo_unsafe_args
   151  func sysctl(mib *uint32, miblen uint32, out *byte, size *uintptr, dst *byte, ndst uintptr) int32 {
   152  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(sysctl_trampoline)), unsafe.Pointer(&mib))
   153  	KeepAlive(mib)
   154  	KeepAlive(out)
   155  	KeepAlive(size)
   156  	KeepAlive(dst)
   157  	return ret
   158  }
   159  func sysctl_trampoline()
   160  
   161  //go:nosplit
   162  //go:cgo_unsafe_args
   163  func fcntl(fd, cmd, arg int32) int32 {
   164  	return libcCall(unsafe.Pointer(abi.FuncPCABI0(fcntl_trampoline)), unsafe.Pointer(&fd))
   165  }
   166  func fcntl_trampoline()
   167  
   168  //go:nosplit
   169  func nanotime1() int64 {
   170  	var ts timespec
   171  	args := struct {
   172  		clock_id int32
   173  		tp       unsafe.Pointer
   174  	}{_CLOCK_MONOTONIC, unsafe.Pointer(&ts)}
   175  	if errno := libcCall(unsafe.Pointer(abi.FuncPCABI0(clock_gettime_trampoline)), unsafe.Pointer(&args)); errno < 0 {
   176  		// Avoid growing the nosplit stack.
   177  		systemstack(func() {
   178  			println("runtime: errno", -errno)
   179  			throw("clock_gettime failed")
   180  		})
   181  	}
   182  	return ts.tv_sec*1e9 + int64(ts.tv_nsec)
   183  }
   184  func clock_gettime_trampoline()
   185  
   186  //go:nosplit
   187  func walltime() (int64, int32) {
   188  	var ts timespec
   189  	args := struct {
   190  		clock_id int32
   191  		tp       unsafe.Pointer
   192  	}{_CLOCK_REALTIME, unsafe.Pointer(&ts)}
   193  	if errno := libcCall(unsafe.Pointer(abi.FuncPCABI0(clock_gettime_trampoline)), unsafe.Pointer(&args)); errno < 0 {
   194  		// Avoid growing the nosplit stack.
   195  		systemstack(func() {
   196  			println("runtime: errno", -errno)
   197  			throw("clock_gettime failed")
   198  		})
   199  	}
   200  	return ts.tv_sec, int32(ts.tv_nsec)
   201  }
   202  
   203  //go:nosplit
   204  //go:cgo_unsafe_args
   205  func kqueue() int32 {
   206  	return libcCall(unsafe.Pointer(abi.FuncPCABI0(kqueue_trampoline)), nil)
   207  }
   208  func kqueue_trampoline()
   209  
   210  //go:nosplit
   211  //go:cgo_unsafe_args
   212  func kevent(kq int32, ch *keventt, nch int32, ev *keventt, nev int32, ts *timespec) int32 {
   213  	ret := libcCall(unsafe.Pointer(abi.FuncPCABI0(kevent_trampoline)), unsafe.Pointer(&kq))
   214  	KeepAlive(ch)
   215  	KeepAlive(ev)
   216  	KeepAlive(ts)
   217  	return ret
   218  }
   219  func kevent_trampoline()
   220  
   221  //go:nosplit
   222  //go:cgo_unsafe_args
   223  func sigaction(sig uint32, new *sigactiont, old *sigactiont) {
   224  	libcCall(unsafe.Pointer(abi.FuncPCABI0(sigaction_trampoline)), unsafe.Pointer(&sig))
   225  	KeepAlive(new)
   226  	KeepAlive(old)
   227  }
   228  func sigaction_trampoline()
   229  
   230  //go:nosplit
   231  //go:cgo_unsafe_args
   232  func sigprocmask(how uint32, new *sigset, old *sigset) {
   233  	// sigprocmask is called from sigsave, which is called from needm.
   234  	// As such, we have to be able to run with no g here.
   235  	asmcgocall_no_g(unsafe.Pointer(abi.FuncPCABI0(sigprocmask_trampoline)), unsafe.Pointer(&how))
   236  	KeepAlive(new)
   237  	KeepAlive(old)
   238  }
   239  func sigprocmask_trampoline()
   240  
   241  //go:nosplit
   242  //go:cgo_unsafe_args
   243  func sigaltstack(new *stackt, old *stackt) {
   244  	libcCall(unsafe.Pointer(abi.FuncPCABI0(sigaltstack_trampoline)), unsafe.Pointer(&new))
   245  	KeepAlive(new)
   246  	KeepAlive(old)
   247  }
   248  func sigaltstack_trampoline()
   249  
   250  // Not used on OpenBSD, but must be defined.
   251  func exitThread(wait *uint32) {
   252  }
   253  
   254  //go:nosplit
   255  func closeonexec(fd int32) {
   256  	fcntl(fd, _F_SETFD, _FD_CLOEXEC)
   257  }
   258  
   259  // Tell the linker that the libc_* functions are to be found
   260  // in a system library, with the libc_ prefix missing.
   261  
   262  //go:cgo_import_dynamic libc_errno __errno "libc.so"
   263  //go:cgo_import_dynamic libc_exit exit "libc.so"
   264  //go:cgo_import_dynamic libc_getthrid getthrid "libc.so"
   265  //go:cgo_import_dynamic libc_sched_yield sched_yield "libc.so"
   266  //go:cgo_import_dynamic libc_thrkill thrkill "libc.so"
   267  
   268  //go:cgo_import_dynamic libc_mmap mmap "libc.so"
   269  //go:cgo_import_dynamic libc_munmap munmap "libc.so"
   270  //go:cgo_import_dynamic libc_madvise madvise "libc.so"
   271  
   272  //go:cgo_import_dynamic libc_open open "libc.so"
   273  //go:cgo_import_dynamic libc_close close "libc.so"
   274  //go:cgo_import_dynamic libc_read read "libc.so"
   275  //go:cgo_import_dynamic libc_write write "libc.so"
   276  //go:cgo_import_dynamic libc_pipe2 pipe2 "libc.so"
   277  
   278  //go:cgo_import_dynamic libc_clock_gettime clock_gettime "libc.so"
   279  //go:cgo_import_dynamic libc_setitimer setitimer "libc.so"
   280  //go:cgo_import_dynamic libc_usleep usleep "libc.so"
   281  //go:cgo_import_dynamic libc_sysctl sysctl "libc.so"
   282  //go:cgo_import_dynamic libc_fcntl fcntl "libc.so"
   283  //go:cgo_import_dynamic libc_getpid getpid "libc.so"
   284  //go:cgo_import_dynamic libc_kill kill "libc.so"
   285  //go:cgo_import_dynamic libc_kqueue kqueue "libc.so"
   286  //go:cgo_import_dynamic libc_kevent kevent "libc.so"
   287  
   288  //go:cgo_import_dynamic libc_sigaction sigaction "libc.so"
   289  //go:cgo_import_dynamic libc_sigaltstack sigaltstack "libc.so"
   290  
   291  //go:cgo_import_dynamic _ _ "libc.so"
   292  

View as plain text