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

View as plain text