| 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 |
//go:build aix |
| 6 |
|
| 7 |
package runtime |
| 8 |
|
| 9 |
import ( |
| 10 |
"internal/abi" |
| 11 |
"internal/runtime/atomic" |
| 12 |
"unsafe" |
| 13 |
) |
| 14 |
|
| 15 |
const ( |
| 16 |
threadStackSize = 0x100000 // size of a thread stack allocated by OS |
| 17 |
) |
| 18 |
|
| 19 |
// funcDescriptor is a structure representing a function descriptor |
| 20 |
// A variable with this type is always created in assembler |
| 21 |
type funcDescriptor struct { |
| 22 |
fn uintptr |
| 23 |
toc uintptr |
| 24 |
envPointer uintptr // unused in Golang |
| 25 |
} |
| 26 |
|
| 27 |
type mOS struct { |
| 28 |
waitsema uintptr // semaphore for parking on locks |
| 29 |
perrno uintptr // pointer to tls errno |
| 30 |
libcall libcall |
| 31 |
} |
| 32 |
|
| 33 |
//go:nosplit |
| 34 |
func semacreate(mp *m) { |
| 35 |
if mp.waitsema != 0 { |
| 36 |
return |
| 37 |
} |
| 38 |
|
| 39 |
var sem *semt |
| 40 |
|
| 41 |
// Call libc's malloc rather than malloc. This will |
| 42 |
// allocate space on the C heap. We can't call mallocgc |
| 43 |
// here because it could cause a deadlock. |
| 44 |
sem = (*semt)(malloc(unsafe.Sizeof(*sem))) |
| 45 |
if sem_init(sem, 0, 0) != 0 { |
| 46 |
throw("sem_init") |
| 47 |
} |
| 48 |
mp.waitsema = uintptr(unsafe.Pointer(sem)) |
| 49 |
} |
| 50 |
|
| 51 |
//go:nosplit |
| 52 |
func semasleep(ns int64) int32 { |
| 53 |
mp := getg().m |
| 54 |
if ns >= 0 { |
| 55 |
var ts timespec |
| 56 |
|
| 57 |
if clock_gettime(_CLOCK_REALTIME, &ts) != 0 { |
| 58 |
throw("clock_gettime") |
| 59 |
} |
| 60 |
ts.tv_sec += ns / 1e9 |
| 61 |
ts.tv_nsec += ns % 1e9 |
| 62 |
if ts.tv_nsec >= 1e9 { |
| 63 |
ts.tv_sec++ |
| 64 |
ts.tv_nsec -= 1e9 |
| 65 |
} |
| 66 |
|
| 67 |
if r, err := sem_timedwait((*semt)(unsafe.Pointer(mp.waitsema)), &ts); r != 0 { |
| 68 |
if err == _ETIMEDOUT || err == _EAGAIN || err == _EINTR { |
| 69 |
return -1 |
| 70 |
} |
| 71 |
println("sem_timedwait err ", err, " ts.tv_sec ", ts.tv_sec, " ts.tv_nsec ", ts.tv_nsec, " ns ", ns, " id ", mp.id) |
| 72 |
throw("sem_timedwait") |
| 73 |
} |
| 74 |
return 0 |
| 75 |
} |
| 76 |
for { |
| 77 |
r1, err := sem_wait((*semt)(unsafe.Pointer(mp.waitsema))) |
| 78 |
if r1 == 0 { |
| 79 |
break |
| 80 |
} |
| 81 |
if err == _EINTR { |
| 82 |
continue |
| 83 |
} |
| 84 |
throw("sem_wait") |
| 85 |
} |
| 86 |
return 0 |
| 87 |
} |
| 88 |
|
| 89 |
//go:nosplit |
| 90 |
func semawakeup(mp *m) { |
| 91 |
if sem_post((*semt)(unsafe.Pointer(mp.waitsema))) != 0 { |
| 92 |
throw("sem_post") |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
func osinit() { |
| 97 |
// Call miniterrno so that we can safely make system calls |
| 98 |
// before calling minit on m0. |
| 99 |
miniterrno() |
| 100 |
|
| 101 |
numCPUStartup = getCPUCount() |
| 102 |
physPageSize = sysconf(__SC_PAGE_SIZE) |
| 103 |
} |
| 104 |
|
| 105 |
func getCPUCount() int32 { |
| 106 |
return int32(sysconf(__SC_NPROCESSORS_ONLN)) |
| 107 |
} |
| 108 |
|
| 109 |
// newosproc0 is a version of newosproc that can be called before the runtime |
| 110 |
// is initialized. |
| 111 |
// |
| 112 |
// This function is not safe to use after initialization as it does not pass an M as fnarg. |
| 113 |
// |
| 114 |
//go:nosplit |
| 115 |
func newosproc0(stacksize uintptr, fn *funcDescriptor) { |
| 116 |
var ( |
| 117 |
attr pthread_attr |
| 118 |
oset sigset |
| 119 |
tid pthread |
| 120 |
) |
| 121 |
|
| 122 |
if pthread_attr_init(&attr) != 0 { |
| 123 |
writeErrStr(failthreadcreate) |
| 124 |
exit(1) |
| 125 |
} |
| 126 |
|
| 127 |
if pthread_attr_setstacksize(&attr, threadStackSize) != 0 { |
| 128 |
writeErrStr(failthreadcreate) |
| 129 |
exit(1) |
| 130 |
} |
| 131 |
|
| 132 |
if pthread_attr_setdetachstate(&attr, _PTHREAD_CREATE_DETACHED) != 0 { |
| 133 |
writeErrStr(failthreadcreate) |
| 134 |
exit(1) |
| 135 |
} |
| 136 |
|
| 137 |
// Disable signals during create, so that the new thread starts |
| 138 |
// with signals disabled. It will enable them in minit. |
| 139 |
sigprocmask(_SIG_SETMASK, &sigset_all, &oset) |
| 140 |
var ret int32 |
| 141 |
for tries := 0; tries < 20; tries++ { |
| 142 |
// pthread_create can fail with EAGAIN for no reasons |
| 143 |
// but it will be ok if it retries. |
| 144 |
ret = pthread_create(&tid, &attr, fn, nil) |
| 145 |
if ret != _EAGAIN { |
| 146 |
break |
| 147 |
} |
| 148 |
usleep(uint32(tries+1) * 1000) // Milliseconds. |
| 149 |
} |
| 150 |
sigprocmask(_SIG_SETMASK, &oset, nil) |
| 151 |
if ret != 0 { |
| 152 |
writeErrStr(failthreadcreate) |
| 153 |
exit(1) |
| 154 |
} |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
// Called to do synchronous initialization of Go code built with |
| 159 |
// -buildmode=c-archive or -buildmode=c-shared. |
| 160 |
// None of the Go runtime is initialized. |
| 161 |
// |
| 162 |
//go:nosplit |
| 163 |
//go:nowritebarrierrec |
| 164 |
func libpreinit() { |
| 165 |
initsig(true) |
| 166 |
} |
| 167 |
|
| 168 |
// Ms related functions |
| 169 |
func mpreinit(mp *m) { |
| 170 |
mp.gsignal = malg(32 * 1024) // AIX wants >= 8K |
| 171 |
mp.gsignal.m = mp |
| 172 |
} |
| 173 |
|
| 174 |
// errno address must be retrieved by calling _Errno libc function. |
| 175 |
// This will return a pointer to errno. |
| 176 |
func miniterrno() { |
| 177 |
mp := getg().m |
| 178 |
r, _ := syscall0(&libc__Errno) |
| 179 |
mp.perrno = r |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
func minit() { |
| 184 |
miniterrno() |
| 185 |
minitSignals() |
| 186 |
getg().m.procid = uint64(pthread_self()) |
| 187 |
} |
| 188 |
|
| 189 |
func unminit() { |
| 190 |
unminitSignals() |
| 191 |
getg().m.procid = 0 |
| 192 |
} |
| 193 |
|
| 194 |
// Called from mexit, but not from dropm, to undo the effect of thread-owned |
| 195 |
// resources in minit, semacreate, or elsewhere. Do not take locks after calling this. |
| 196 |
// |
| 197 |
// This always runs without a P, so //go:nowritebarrierrec is required. |
| 198 |
// |
| 199 |
//go:nowritebarrierrec |
| 200 |
func mdestroy(mp *m) { |
| 201 |
} |
| 202 |
|
| 203 |
// tstart is a function descriptor to _tstart defined in assembly. |
| 204 |
var tstart funcDescriptor |
| 205 |
|
| 206 |
func newosproc(mp *m) { |
| 207 |
var ( |
| 208 |
attr pthread_attr |
| 209 |
oset sigset |
| 210 |
tid pthread |
| 211 |
) |
| 212 |
|
| 213 |
if pthread_attr_init(&attr) != 0 { |
| 214 |
throw("pthread_attr_init") |
| 215 |
} |
| 216 |
|
| 217 |
if pthread_attr_setstacksize(&attr, threadStackSize) != 0 { |
| 218 |
throw("pthread_attr_getstacksize") |
| 219 |
} |
| 220 |
|
| 221 |
if pthread_attr_setdetachstate(&attr, _PTHREAD_CREATE_DETACHED) != 0 { |
| 222 |
throw("pthread_attr_setdetachstate") |
| 223 |
} |
| 224 |
|
| 225 |
// Disable signals during create, so that the new thread starts |
| 226 |
// with signals disabled. It will enable them in minit. |
| 227 |
sigprocmask(_SIG_SETMASK, &sigset_all, &oset) |
| 228 |
ret := retryOnEAGAIN(func() int32 { |
| 229 |
return pthread_create(&tid, &attr, &tstart, unsafe.Pointer(mp)) |
| 230 |
}) |
| 231 |
sigprocmask(_SIG_SETMASK, &oset, nil) |
| 232 |
if ret != 0 { |
| 233 |
print("runtime: failed to create new OS thread (have ", mcount(), " already; errno=", ret, ")\n") |
| 234 |
if ret == _EAGAIN { |
| 235 |
println("runtime: may need to increase max user processes (ulimit -u)") |
| 236 |
} |
| 237 |
throw("newosproc") |
| 238 |
} |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
func exitThread(wait *atomic.Uint32) { |
| 243 |
// We should never reach exitThread on AIX because we let |
| 244 |
// libc clean up threads. |
| 245 |
throw("exitThread") |
| 246 |
} |
| 247 |
|
| 248 |
var urandom_dev = []byte("/dev/urandom\x00") |
| 249 |
|
| 250 |
//go:nosplit |
| 251 |
func readRandom(r []byte) int { |
| 252 |
fd := open(&urandom_dev[0], 0 /* O_RDONLY */, 0) |
| 253 |
n := read(fd, unsafe.Pointer(&r[0]), int32(len(r))) |
| 254 |
closefd(fd) |
| 255 |
return int(n) |
| 256 |
} |
| 257 |
|
| 258 |
func goenvs() { |
| 259 |
goenvs_unix() |
| 260 |
} |
| 261 |
|
| 262 |
/* SIGNAL */ |
| 263 |
|
| 264 |
const ( |
| 265 |
_NSIG = 256 |
| 266 |
) |
| 267 |
|
| 268 |
// sigtramp is a function descriptor to _sigtramp defined in assembly |
| 269 |
var sigtramp funcDescriptor |
| 270 |
|
| 271 |
//go:nosplit |
| 272 |
//go:nowritebarrierrec |
| 273 |
func setsig(i uint32, fn uintptr) { |
| 274 |
var sa sigactiont |
| 275 |
sa.sa_flags = _SA_SIGINFO | _SA_ONSTACK | _SA_RESTART |
| 276 |
sa.sa_mask = sigset_all |
| 277 |
if fn == abi.FuncPCABIInternal(sighandler) { // abi.FuncPCABIInternal(sighandler) matches the callers in signal_unix.go |
| 278 |
fn = uintptr(unsafe.Pointer(&sigtramp)) |
| 279 |
} |
| 280 |
sa.sa_handler = fn |
| 281 |
sigaction(uintptr(i), &sa, nil) |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
//go:nosplit |
| 286 |
//go:nowritebarrierrec |
| 287 |
func setsigstack(i uint32) { |
| 288 |
var sa sigactiont |
| 289 |
sigaction(uintptr(i), nil, &sa) |
| 290 |
if sa.sa_flags&_SA_ONSTACK != 0 { |
| 291 |
return |
| 292 |
} |
| 293 |
sa.sa_flags |= _SA_ONSTACK |
| 294 |
sigaction(uintptr(i), &sa, nil) |
| 295 |
} |
| 296 |
|
| 297 |
//go:nosplit |
| 298 |
//go:nowritebarrierrec |
| 299 |
func getsig(i uint32) uintptr { |
| 300 |
var sa sigactiont |
| 301 |
sigaction(uintptr(i), nil, &sa) |
| 302 |
return sa.sa_handler |
| 303 |
} |
| 304 |
|
| 305 |
// setSignalstackSP sets the ss_sp field of a stackt. |
| 306 |
// |
| 307 |
//go:nosplit |
| 308 |
func setSignalstackSP(s *stackt, sp uintptr) { |
| 309 |
*(*uintptr)(unsafe.Pointer(&s.ss_sp)) = sp |
| 310 |
} |
| 311 |
|
| 312 |
//go:nosplit |
| 313 |
func (c *sigctxt) fixsigcode(sig uint32) { |
| 314 |
switch sig { |
| 315 |
case _SIGPIPE: |
| 316 |
// For SIGPIPE, c.sigcode() isn't set to _SI_USER as on Linux. |
| 317 |
// Therefore, raisebadsignal won't raise SIGPIPE again if |
| 318 |
// it was deliver in a non-Go thread. |
| 319 |
c.set_sigcode(_SI_USER) |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
//go:nosplit |
| 324 |
//go:nowritebarrierrec |
| 325 |
func sigaddset(mask *sigset, i int) { |
| 326 |
(*mask)[(i-1)/64] |= 1 << ((uint32(i) - 1) & 63) |
| 327 |
} |
| 328 |
|
| 329 |
func sigdelset(mask *sigset, i int) { |
| 330 |
(*mask)[(i-1)/64] &^= 1 << ((uint32(i) - 1) & 63) |
| 331 |
} |
| 332 |
|
| 333 |
func setProcessCPUProfiler(hz int32) { |
| 334 |
setProcessCPUProfilerTimer(hz) |
| 335 |
} |
| 336 |
|
| 337 |
func setThreadCPUProfiler(hz int32) { |
| 338 |
setThreadCPUProfilerHz(hz) |
| 339 |
} |
| 340 |
|
| 341 |
//go:nosplit |
| 342 |
func validSIGPROF(mp *m, c *sigctxt) bool { |
| 343 |
return true |
| 344 |
} |
| 345 |
|
| 346 |
const ( |
| 347 |
_CLOCK_REALTIME = 9 |
| 348 |
_CLOCK_MONOTONIC = 10 |
| 349 |
) |
| 350 |
|
| 351 |
//go:nosplit |
| 352 |
func nanotime1() int64 { |
| 353 |
tp := ×pec{} |
| 354 |
if clock_gettime(_CLOCK_REALTIME, tp) != 0 { |
| 355 |
throw("syscall clock_gettime failed") |
| 356 |
} |
| 357 |
return tp.tv_sec*1000000000 + tp.tv_nsec |
| 358 |
} |
| 359 |
|
| 360 |
func walltime() (sec int64, nsec int32) { |
| 361 |
ts := ×pec{} |
| 362 |
if clock_gettime(_CLOCK_REALTIME, ts) != 0 { |
| 363 |
throw("syscall clock_gettime failed") |
| 364 |
} |
| 365 |
return ts.tv_sec, int32(ts.tv_nsec) |
| 366 |
} |
| 367 |
|
| 368 |
//go:nosplit |
| 369 |
func fcntl(fd, cmd, arg int32) (int32, int32) { |
| 370 |
r, errno := syscall3(&libc_fcntl, uintptr(fd), uintptr(cmd), uintptr(arg)) |
| 371 |
return int32(r), int32(errno) |
| 372 |
} |
| 373 |
|
| 374 |
//go:nosplit |
| 375 |
func setNonblock(fd int32) { |
| 376 |
flags, _ := fcntl(fd, _F_GETFL, 0) |
| 377 |
if flags != -1 { |
| 378 |
fcntl(fd, _F_SETFL, flags|_O_NONBLOCK) |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
// sigPerThreadSyscall is only used on linux, so we assign a bogus signal |
| 383 |
// number. |
| 384 |
const sigPerThreadSyscall = 1 << 31 |
| 385 |
|
| 386 |
//go:nosplit |
| 387 |
func runPerThreadSyscall() { |
| 388 |
throw("runPerThreadSyscall only valid on linux") |
| 389 |
} |
| 390 |
|
| 391 |
//go:nosplit |
| 392 |
func getuid() int32 { |
| 393 |
r, errno := syscall0(&libc_getuid) |
| 394 |
if errno != 0 { |
| 395 |
print("getuid failed ", errno) |
| 396 |
throw("getuid") |
| 397 |
} |
| 398 |
return int32(r) |
| 399 |
} |
| 400 |
|
| 401 |
//go:nosplit |
| 402 |
func geteuid() int32 { |
| 403 |
r, errno := syscall0(&libc_geteuid) |
| 404 |
if errno != 0 { |
| 405 |
print("geteuid failed ", errno) |
| 406 |
throw("geteuid") |
| 407 |
} |
| 408 |
return int32(r) |
| 409 |
} |
| 410 |
|
| 411 |
//go:nosplit |
| 412 |
func getgid() int32 { |
| 413 |
r, errno := syscall0(&libc_getgid) |
| 414 |
if errno != 0 { |
| 415 |
print("getgid failed ", errno) |
| 416 |
throw("getgid") |
| 417 |
} |
| 418 |
return int32(r) |
| 419 |
} |
| 420 |
|
| 421 |
//go:nosplit |
| 422 |
func getegid() int32 { |
| 423 |
r, errno := syscall0(&libc_getegid) |
| 424 |
if errno != 0 { |
| 425 |
print("getegid failed ", errno) |
| 426 |
throw("getegid") |
| 427 |
} |
| 428 |
return int32(r) |
| 429 |
} |