1 // Copyright 2009 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 freebsd && (386 || arm || arm64 || riscv64)
6
7 #include <sys/types.h>
8 #include <sys/signalvar.h>
9 #include <machine/sysarch.h>
10 #include <pthread.h>
11 #include <signal.h>
12 #include <string.h>
13 #include "libcgo.h"
14 #include "libcgo_unix.h"
15
16 #ifdef ARM_TP_ADDRESS
17 // ARM_TP_ADDRESS is (ARM_VECTORS_HIGH + 0x1000) or 0xffff1000
18 // and is known to runtime.read_tls_fallback. Verify it with
19 // cpp.
20 #if ARM_TP_ADDRESS != 0xffff1000
21 #error Wrong ARM_TP_ADDRESS!
22 #endif
23 #endif
24
25 static void* threadentry(void*);
26 static void (*setg_gcc)(void*);
27
28 void
29 x_cgo_init(G *g, void (*setg)(void*))
30 {
31 setg_gcc = setg;
32 _cgo_set_stacklo(g, NULL);
33 }
34
35 void
36 _cgo_sys_thread_start(ThreadStart *ts)
37 {
38 pthread_attr_t attr;
39 sigset_t ign, oset;
40 pthread_t p;
41 size_t size;
42 int err;
43
44 SIGFILLSET(ign);
45 pthread_sigmask(SIG_SETMASK, &ign, &oset);
46
47 pthread_attr_init(&attr);
48 pthread_attr_getstacksize(&attr, &size);
49 // Leave stacklo=0 and set stackhi=size; mstart will do the rest.
50 ts->g->stackhi = size;
51 err = _cgo_try_pthread_create(&p, &attr, threadentry, ts);
52
53 pthread_sigmask(SIG_SETMASK, &oset, nil);
54
55 if (err != 0) {
56 fatalf("pthread_create failed: %s", strerror(err));
57 }
58 }
59
60 extern void crosscall1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
61 static void*
62 threadentry(void *v)
63 {
64 ThreadStart ts;
65
66 ts = *(ThreadStart*)v;
67 free(v);
68
69 crosscall1(ts.fn, setg_gcc, ts.g);
70 return nil;
71 }
72
View as plain text