go_freebsd.go RawHistoryFolder
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
7package fakecgo
8
9import "unsafe"
10
11//go:nosplit
12func _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
42var x_threadentry_trampoline byte
43var threadentry_trampolineABI0 = &x_threadentry_trampoline
44
45//go:nosplit
46func threadentry(v unsafe.Pointer) unsafe.Pointer {
47 ts := *(*ThreadStart)(v)
48 free(v)
49
50 setg_trampoline(setg_func, uintptr(unsafe.Pointer(ts.g)))
51
52 callThreadEntryFn(ts.fn)
53
54 return nil
55}
56
57// here we will store a pointer to the provided setg func
58var setg_func uintptr
59
60// x_cgo_init(G *g, void (*setg)(void*)) (runtime/cgo/gcc_linux_amd64.c)
61// This get's called during startup, adjusts stacklo, and provides a pointer to setg_gcc for us
62// Additionally, if we set _cgo_init to non-null, go won't do it's own TLS setup
63// This function can't be go:systemstack since go is not in a state where the systemcheck would work.
64//
65//go:nosplit
66func x_cgo_init(g *G, setg uintptr) {
67 var size size_t
68 var attr pthread_attr_t
69
70 setg_func = setg
71 pthread_attr_init(&attr)
72 pthread_attr_getstacksize(&attr, &size)
73 // runtime/cgo uses __builtin_frame_address(0) instead of `uintptr(unsafe.Pointer(&size))`
74 // but this should be OK since we are taking the address of the first variable in this function.
75 g.stacklo = uintptr(unsafe.Pointer(&size)) - uintptr(size) + 4096
76 pthread_attr_destroy(&attr)
77}