blob: b1762957a2e07d86ad6a9699126eb3dddfdd12db [file] [log] [blame]
Austin Clementsaf7ca8d2014-12-16 18:34:55 -05001// Copyright 2014 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// +build ppc64 ppc64le
6
7#include <pthread.h>
8#include <string.h>
9#include <signal.h>
10#include "libcgo.h"
11
12static void *threadentry(void*);
13
14void (*x_cgo_inittls)(void **tlsg, void **tlsbase);
15static void (*setg_gcc)(void*);
16
17void
18x_cgo_init(G *g, void (*setg)(void*), void **tlsbase)
19{
20 pthread_attr_t attr;
21 size_t size;
22
23 setg_gcc = setg;
24 pthread_attr_init(&attr);
25 pthread_attr_getstacksize(&attr, &size);
26 g->stacklo = (uintptr)&attr - size + 4096;
27 pthread_attr_destroy(&attr);
28}
29
30void
31_cgo_sys_thread_start(ThreadStart *ts)
32{
33 pthread_attr_t attr;
34 sigset_t ign, oset;
35 pthread_t p;
36 size_t size;
37 int err;
38
39 sigfillset(&ign);
40 pthread_sigmask(SIG_SETMASK, &ign, &oset);
41
42 pthread_attr_init(&attr);
43 pthread_attr_getstacksize(&attr, &size);
44 // Leave stacklo=0 and set stackhi=size; mstack will do the rest.
45 ts->g->stackhi = size;
46 err = pthread_create(&p, &attr, threadentry, ts);
47
48 pthread_sigmask(SIG_SETMASK, &oset, nil);
49
50 if (err != 0) {
51 fatalf("pthread_create failed: %s", strerror(err));
52 }
53}
54
55extern void crosscall_ppc64(void (*fn)(void), void *g);
56
57static void*
58threadentry(void *v)
59{
60 ThreadStart ts;
61
62 ts = *(ThreadStart*)v;
63 free(v);
64
65 // Save g for this thread in C TLS
66 setg_gcc((void*)ts.g);
67
68 crosscall_ppc64(ts.fn, (void*)ts.g);
69 return nil;
70}