github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/runtime/cgo/gcc_darwin_386.c (about)

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