go_netbsd.go 3.3 KB

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