go_darwin_arm64.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. //go:norace
  9. func _cgo_sys_thread_start(ts *ThreadStart) {
  10. var attr pthread_attr_t
  11. var ign, oset sigset_t
  12. var p pthread_t
  13. var size size_t
  14. var err int
  15. sigfillset(&ign)
  16. pthread_sigmask(SIG_SETMASK, &ign, &oset)
  17. size = pthread_get_stacksize_np(pthread_self())
  18. pthread_attr_init(&attr)
  19. pthread_attr_setstacksize(&attr, size)
  20. // Leave stacklo=0 and set stackhi=size; mstart will do the rest.
  21. ts.g.stackhi = uintptr(size)
  22. err = _cgo_try_pthread_create(&p, &attr, unsafe.Pointer(threadentry_trampolineABI0), ts)
  23. pthread_sigmask(SIG_SETMASK, &oset, nil)
  24. if err != 0 {
  25. print("fakecgo: pthread_create failed: ")
  26. println(err)
  27. abort()
  28. }
  29. }
  30. // threadentry_trampolineABI0 maps the C ABI to Go ABI then calls the Go function
  31. //
  32. //go:linkname x_threadentry_trampoline threadentry_trampoline
  33. var x_threadentry_trampoline byte
  34. var threadentry_trampolineABI0 = &x_threadentry_trampoline
  35. //go:nosplit
  36. //go:norace
  37. func threadentry(v unsafe.Pointer) unsafe.Pointer {
  38. ts := *(*ThreadStart)(v)
  39. free(v)
  40. // TODO: support ios
  41. //#if TARGET_OS_IPHONE
  42. // darwin_arm_init_thread_exception_port();
  43. //#endif
  44. setg_trampoline(setg_func, uintptr(unsafe.Pointer(ts.g)))
  45. // faking funcs in go is a bit a... involved - but the following works :)
  46. fn := uintptr(unsafe.Pointer(&ts.fn))
  47. (*(*func())(unsafe.Pointer(&fn)))()
  48. return nil
  49. }
  50. // here we will store a pointer to the provided setg func
  51. var setg_func uintptr
  52. // x_cgo_init(G *g, void (*setg)(void*)) (runtime/cgo/gcc_linux_amd64.c)
  53. // This get's called during startup, adjusts stacklo, and provides a pointer to setg_gcc for us
  54. // Additionally, if we set _cgo_init to non-null, go won't do it's own TLS setup
  55. // This function can't be go:systemstack since go is not in a state where the systemcheck would work.
  56. //
  57. //go:nosplit
  58. //go:norace
  59. func x_cgo_init(g *G, setg uintptr) {
  60. var size size_t
  61. setg_func = setg
  62. size = pthread_get_stacksize_np(pthread_self())
  63. g.stacklo = uintptr(unsafe.Add(unsafe.Pointer(&size), -size+4096))
  64. //TODO: support ios
  65. //#if TARGET_OS_IPHONE
  66. // darwin_arm_init_mach_exception_handler();
  67. // darwin_arm_init_thread_exception_port();
  68. // init_working_dir();
  69. //#endif
  70. }