go_linux_arm64.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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
  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. ts := *(*ThreadStart)(v)
  37. free(v)
  38. setg_trampoline(setg_func, uintptr(unsafe.Pointer(ts.g)))
  39. // faking funcs in go is a bit a... involved - but the following works :)
  40. fn := uintptr(unsafe.Pointer(&ts.fn))
  41. (*(*func())(unsafe.Pointer(&fn)))()
  42. return nil
  43. }
  44. // here we will store a pointer to the provided setg func
  45. var setg_func uintptr
  46. // x_cgo_init(G *g, void (*setg)(void*)) (runtime/cgo/gcc_linux_amd64.c)
  47. // This get's called during startup, adjusts stacklo, and provides a pointer to setg_gcc for us
  48. // Additionally, if we set _cgo_init to non-null, go won't do it's own TLS setup
  49. // This function can't be go:systemstack since go is not in a state where the systemcheck would work.
  50. //
  51. //go:nosplit
  52. func x_cgo_init(g *G, setg uintptr) {
  53. var size size_t
  54. var attr *pthread_attr_t
  55. /* The memory sanitizer distributed with versions of clang
  56. before 3.8 has a bug: if you call mmap before malloc, mmap
  57. may return an address that is later overwritten by the msan
  58. library. Avoid this problem by forcing a call to malloc
  59. here, before we ever call malloc.
  60. This is only required for the memory sanitizer, so it's
  61. unfortunate that we always run it. It should be possible
  62. to remove this when we no longer care about versions of
  63. clang before 3.8. The test for this is
  64. misc/cgo/testsanitizers.
  65. GCC works hard to eliminate a seemingly unnecessary call to
  66. malloc, so we actually use the memory we allocate. */
  67. setg_func = setg
  68. attr = (*pthread_attr_t)(malloc(unsafe.Sizeof(*attr)))
  69. if attr == nil {
  70. println("fakecgo: malloc failed")
  71. abort()
  72. }
  73. pthread_attr_init(attr)
  74. pthread_attr_getstacksize(attr, &size)
  75. g.stacklo = uintptr(unsafe.Pointer(&size)) - uintptr(size) + 4096
  76. pthread_attr_destroy(attr)
  77. free(unsafe.Pointer(attr))
  78. }