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