go_netbsd.go raw

   1  // Copyright 2011 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 !cgo && (amd64 || arm64)
   6  
   7  package fakecgo
   8  
   9  import "unsafe"
  10  
  11  //go:nosplit
  12  func _cgo_sys_thread_start(ts *ThreadStart) {
  13  	var attr pthread_attr_t
  14  	var ign, oset sigset_t
  15  	var p pthread_t
  16  	var size size_t
  17  	var err int
  18  
  19  	// fprintf(stderr, "runtime/cgo: _cgo_sys_thread_start: fn=%p, g=%p\n", ts->fn, ts->g); // debug
  20  	sigfillset(&ign)
  21  	pthread_sigmask(SIG_SETMASK, &ign, &oset)
  22  
  23  	pthread_attr_init(&attr)
  24  	pthread_attr_getstacksize(&attr, &size)
  25  	// Leave stacklo=0 and set stackhi=size; mstart will do the rest.
  26  	ts.g.stackhi = uintptr(size)
  27  
  28  	err = _cgo_try_pthread_create(&p, &attr, unsafe.Pointer(threadentry_trampolineABI0), ts)
  29  
  30  	pthread_sigmask(SIG_SETMASK, &oset, nil)
  31  
  32  	if err != 0 {
  33  		print("fakecgo: pthread_create failed: ")
  34  		println(err)
  35  		abort()
  36  	}
  37  }
  38  
  39  // threadentry_trampolineABI0 maps the C ABI to Go ABI then calls the Go function
  40  //
  41  //go:linkname x_threadentry_trampoline threadentry_trampoline
  42  var x_threadentry_trampoline byte
  43  var threadentry_trampolineABI0 = &x_threadentry_trampoline
  44  
  45  //go:nosplit
  46  func threadentry(v unsafe.Pointer) unsafe.Pointer {
  47  	var ss stack_t
  48  	ts := *(*ThreadStart)(v)
  49  	free(v)
  50  
  51  	// On NetBSD, a new thread inherits the signal stack of the
  52  	// creating thread. That confuses minit, so we remove that
  53  	// signal stack here before calling the regular mstart. It's
  54  	// a bit baroque to remove a signal stack here only to add one
  55  	// in minit, but it's a simple change that keeps NetBSD
  56  	// working like other OS's. At this point all signals are
  57  	// blocked, so there is no race.
  58  	ss.ss_flags = SS_DISABLE
  59  	sigaltstack(&ss, nil)
  60  
  61  	setg_trampoline(setg_func, uintptr(unsafe.Pointer(ts.g)))
  62  
  63  	// faking funcs in go is a bit a... involved - but the following works :)
  64  	fn := uintptr(unsafe.Pointer(&ts.fn))
  65  	(*(*func())(unsafe.Pointer(&fn)))()
  66  
  67  	return nil
  68  }
  69  
  70  // here we will store a pointer to the provided setg func
  71  var setg_func uintptr
  72  
  73  //go:nosplit
  74  func x_cgo_init(g *G, setg uintptr) {
  75  	var size size_t
  76  	var attr *pthread_attr_t
  77  
  78  	/* The memory sanitizer distributed with versions of clang
  79  	   before 3.8 has a bug: if you call mmap before malloc, mmap
  80  	   may return an address that is later overwritten by the msan
  81  	   library.  Avoid this problem by forcing a call to malloc
  82  	   here, before we ever call malloc.
  83  
  84  	   This is only required for the memory sanitizer, so it's
  85  	   unfortunate that we always run it.  It should be possible
  86  	   to remove this when we no longer care about versions of
  87  	   clang before 3.8.  The test for this is
  88  	   misc/cgo/testsanitizers.
  89  
  90  	   GCC works hard to eliminate a seemingly unnecessary call to
  91  	   malloc, so we actually use the memory we allocate.  */
  92  
  93  	setg_func = setg
  94  	attr = (*pthread_attr_t)(malloc(unsafe.Sizeof(*attr)))
  95  	if attr == nil {
  96  		println("fakecgo: malloc failed")
  97  		abort()
  98  	}
  99  	pthread_attr_init(attr)
 100  	pthread_attr_getstacksize(attr, &size)
 101  	// runtime/cgo uses __builtin_frame_address(0) instead of `uintptr(unsafe.Pointer(&size))`
 102  	// but this should be OK since we are taking the address of the first variable in this function.
 103  	g.stacklo = uintptr(unsafe.Pointer(&size)) - uintptr(size) + 4096
 104  	pthread_attr_destroy(attr)
 105  	free(unsafe.Pointer(attr))
 106  }
 107