gcc_darwin_amd64.c 2.93 KB
Newer Older
Russ Cox's avatar
Russ Cox committed
1 2 3 4
// Copyright 2009 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

Russ Cox's avatar
Russ Cox committed
5
#include <string.h> /* for strerror */
Russ Cox's avatar
Russ Cox committed
6
#include <pthread.h>
7
#include <signal.h>
Russ Cox's avatar
Russ Cox committed
8 9 10 11 12
#include "libcgo.h"

static void* threadentry(void*);
static pthread_key_t k1, k2;

Russ Cox's avatar
Russ Cox committed
13 14
#define magic1 (0x23581321345589ULL)

Russ Cox's avatar
Russ Cox committed
15 16 17 18
static void
inittls(void)
{
	uint64 x, y;
Russ Cox's avatar
Russ Cox committed
19
	pthread_key_t tofree[128], k;
Russ Cox's avatar
Russ Cox committed
20 21 22 23 24
	int i, ntofree;
	int havek1, havek2;

	/*
	 * Same logic, code as darwin_386.c:/inittls, except that words
Russ Cox's avatar
Russ Cox committed
25 26
	 * are 8 bytes long now, and the thread-local storage starts
	 * at 0x60 on Leopard / Snow Leopard. So the offsets are
Russ Cox's avatar
Russ Cox committed
27 28 29
	 * 0x60+8*0x108 = 0x8a0 and 0x60+8*0x109 = 0x8a8.
	 *
	 * The linker and runtime hard-code these constant offsets
Russ Cox's avatar
Russ Cox committed
30
	 * from %gs where we expect to find m and g.
31 32
	 * Known to ../../../cmd/6l/obj.c:/8a0
	 * and to ../sys_darwin_amd64.s:/8a0
Russ Cox's avatar
Russ Cox committed
33 34 35 36 37 38 39 40
	 *
	 * As disgusting as on the 386; same justification.
	 */
	havek1 = 0;
	havek2 = 0;
	ntofree = 0;
	while(!havek1 || !havek2) {
		if(pthread_key_create(&k, nil) < 0) {
Russ Cox's avatar
Russ Cox committed
41
			fprintf(stderr, "runtime/cgo: pthread_key_create failed\n");
Russ Cox's avatar
Russ Cox committed
42 43
			abort();
		}
Russ Cox's avatar
Russ Cox committed
44 45 46 47
		pthread_setspecific(k, (void*)magic1);
		asm volatile("movq %%gs:0x8a0, %0" : "=r"(x));
		asm volatile("movq %%gs:0x8a8, %0" : "=r"(y));
		if(x == magic1) {
Russ Cox's avatar
Russ Cox committed
48 49
			havek1 = 1;
			k1 = k;
Russ Cox's avatar
Russ Cox committed
50
		} else if(y == magic1) {
Russ Cox's avatar
Russ Cox committed
51 52
			havek2 = 1;
			k2 = k;
Russ Cox's avatar
Russ Cox committed
53 54 55 56 57 58 59 60 61 62
		} else {
			if(ntofree >= nelem(tofree)) {
				fprintf(stderr, "runtime/cgo: could not obtain pthread_keys\n");
				fprintf(stderr, "\ttried");
				for(i=0; i<ntofree; i++)
					fprintf(stderr, " %#x", (unsigned)tofree[i]);
				fprintf(stderr, "\n");
				abort();
			}
			tofree[ntofree++] = k;
Russ Cox's avatar
Russ Cox committed
63
		}
Russ Cox's avatar
Russ Cox committed
64
		pthread_setspecific(k, 0);
Russ Cox's avatar
Russ Cox committed
65 66 67
	}

	/*
Russ Cox's avatar
Russ Cox committed
68
	 * We got the keys we wanted.  Free the others.
Russ Cox's avatar
Russ Cox committed
69
	 */
Russ Cox's avatar
Russ Cox committed
70 71
	for(i=0; i<ntofree; i++)
		pthread_key_delete(tofree[i]);
Russ Cox's avatar
Russ Cox committed
72 73 74
}

void
Dmitriy Vyukov's avatar
Dmitriy Vyukov committed
75
xinitcgo(G *g)
Russ Cox's avatar
Russ Cox committed
76
{
Dmitriy Vyukov's avatar
Dmitriy Vyukov committed
77 78 79 80 81 82 83 84
	pthread_attr_t attr;
	size_t size;

	pthread_attr_init(&attr);
	pthread_attr_getstacksize(&attr, &size);
	g->stackguard = (uintptr)&attr - size + 4096;
	pthread_attr_destroy(&attr);

Russ Cox's avatar
Russ Cox committed
85 86 87
	inittls();
}

Dmitriy Vyukov's avatar
Dmitriy Vyukov committed
88
void (*initcgo)(G*) = xinitcgo;
Russ Cox's avatar
Russ Cox committed
89 90 91 92 93

void
libcgo_sys_thread_start(ThreadStart *ts)
{
	pthread_attr_t attr;
94
	sigset_t ign, oset;
Russ Cox's avatar
Russ Cox committed
95 96
	pthread_t p;
	size_t size;
97
	int err;
Russ Cox's avatar
Russ Cox committed
98

99 100 101
	sigfillset(&ign);
	sigprocmask(SIG_SETMASK, &ign, &oset);

Russ Cox's avatar
Russ Cox committed
102 103 104
	pthread_attr_init(&attr);
	pthread_attr_getstacksize(&attr, &size);
	ts->g->stackguard = size;
105
	err = pthread_create(&p, &attr, threadentry, ts);
106 107 108

	sigprocmask(SIG_SETMASK, &oset, nil);

109 110 111 112
	if (err != 0) {
		fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
		abort();
	}
Russ Cox's avatar
Russ Cox committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
}

static void*
threadentry(void *v)
{
	ThreadStart ts;

	ts = *(ThreadStart*)v;
	free(v);

	ts.g->stackbase = (uintptr)&ts;

	/*
	 * libcgo_sys_thread_start set stackguard to stack size;
	 * change to actual guard pointer.
	 */
	ts.g->stackguard = (uintptr)&ts - ts.g->stackguard + 4096;

	pthread_setspecific(k1, (void*)ts.g);
	pthread_setspecific(k2, (void*)ts.m);

	crosscall_amd64(ts.fn);
	return nil;
}