Home | History | Annotate | Download | only in cgo
      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 #include <string.h> /* for strerror */
      6 #include <pthread.h>
      7 #include <signal.h>
      8 #include "libcgo.h"
      9 
     10 static void* threadentry(void*);
     11 static pthread_key_t k1;
     12 
     13 #define magic1 (0x23581321U)
     14 
     15 static void
     16 inittls(void)
     17 {
     18 	uint32 x;
     19 	pthread_key_t tofree[128], k;
     20 	int i, ntofree;
     21 
     22 	/*
     23 	 * Allocate thread-local storage slot for g.
     24 	 * The key numbers start at 0x100, and we expect to be
     25 	 * one of the early calls to pthread_key_create, so we
     26 	 * should be able to get a pretty low number.
     27 	 *
     28 	 * In Darwin/386 pthreads, %gs points at the thread
     29 	 * structure, and each key is an index into the thread-local
     30 	 * storage array that begins at offset 0x48 within in that structure.
     31 	 * It may happen that we are not quite the first function to try
     32 	 * to allocate thread-local storage keys, so instead of depending
     33 	 * on getting 0x100, we try for 0x108, allocating keys until
     34 	 * we get the one we want and then freeing the ones we didn't want.
     35 	 *
     36 	 * Thus the final offset to use in %gs references is
     37 	 * 0x48+4*0x108 = 0x468.
     38 	 *
     39 	 * The linker and runtime hard-code this constant offset
     40 	 * from %gs where we expect to find g.
     41 	 * Known to ../../../liblink/sym.c:/468
     42 	 * and to ../sys_darwin_386.s:/468
     43 	 *
     44 	 * This is truly disgusting and a bit fragile, but taking care
     45 	 * of it here protects the rest of the system from damage.
     46 	 * The alternative would be to use a global variable that
     47 	 * held the offset and refer to that variable each time we
     48 	 * need a %gs variable (g).  That approach would
     49 	 * require an extra instruction and memory reference in
     50 	 * every stack growth prolog and would also require
     51 	 * rewriting the code that 8c generates for extern registers.
     52 	 *
     53 	 * Things get more disgusting on OS X 10.7 Lion.
     54 	 * The 0x48 base mentioned above is the offset of the tsd
     55 	 * array within the per-thread structure on Leopard and Snow Leopard.
     56 	 * On Lion, the base moved a little, so while the math above
     57 	 * still applies, the base is different.  Thus, we cannot
     58 	 * look for specific key values if we want to build binaries
     59 	 * that run on both systems.  Instead, forget about the
     60 	 * specific key values and just allocate and initialize per-thread
     61 	 * storage until we find a key that writes to the memory location
     62 	 * we want.  Then keep that key.
     63 	 */
     64 	ntofree = 0;
     65 	for(;;) {
     66 		if(pthread_key_create(&k, nil) < 0) {
     67 			fprintf(stderr, "runtime/cgo: pthread_key_create failed\n");
     68 			abort();
     69 		}
     70 		pthread_setspecific(k, (void*)magic1);
     71 		asm volatile("movl %%gs:0x468, %0" : "=r"(x));
     72 		pthread_setspecific(k, 0);
     73 		if(x == magic1) {
     74 			k1 = k;
     75 			break;
     76 		}
     77 		if(ntofree >= nelem(tofree)) {
     78 			fprintf(stderr, "runtime/cgo: could not obtain pthread_keys\n");
     79 			fprintf(stderr, "\ttried");
     80 			for(i=0; i<ntofree; i++)
     81 				fprintf(stderr, " %#x", (unsigned)tofree[i]);
     82 			fprintf(stderr, "\n");
     83 			abort();
     84 		}
     85 		tofree[ntofree++] = k;
     86 	}
     87 
     88 	/*
     89 	 * We got the key we wanted.  Free the others.
     90 	 */
     91 	for(i=0; i<ntofree; i++)
     92 		pthread_key_delete(tofree[i]);
     93 }
     94 
     95 void
     96 x_cgo_init(G *g)
     97 {
     98 	pthread_attr_t attr;
     99 	size_t size;
    100 
    101 	pthread_attr_init(&attr);
    102 	pthread_attr_getstacksize(&attr, &size);
    103 	g->stacklo = (uintptr)&attr - size + 4096;
    104 	pthread_attr_destroy(&attr);
    105 
    106 	inittls();
    107 }
    108 
    109 
    110 void
    111 _cgo_sys_thread_start(ThreadStart *ts)
    112 {
    113 	pthread_attr_t attr;
    114 	sigset_t ign, oset;
    115 	pthread_t p;
    116 	size_t size;
    117 	int err;
    118 
    119 	sigfillset(&ign);
    120 	pthread_sigmask(SIG_SETMASK, &ign, &oset);
    121 
    122 	pthread_attr_init(&attr);
    123 	pthread_attr_getstacksize(&attr, &size);
    124 	// Leave stacklo=0 and set stackhi=size; mstack will do the rest.
    125 	ts->g->stackhi = size;
    126 	err = pthread_create(&p, &attr, threadentry, ts);
    127 
    128 	pthread_sigmask(SIG_SETMASK, &oset, nil);
    129 
    130 	if (err != 0) {
    131 		fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
    132 		abort();
    133 	}
    134 }
    135 
    136 static void*
    137 threadentry(void *v)
    138 {
    139 	ThreadStart ts;
    140 
    141 	ts = *(ThreadStart*)v;
    142 	free(v);
    143 
    144 	pthread_setspecific(k1, (void*)ts.g);
    145 
    146 	crosscall_386(ts.fn);
    147 	return nil;
    148 }
    149