Home | History | Annotate | Download | only in cgo
      1 // Copyright 2012 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 <sys/types.h>
      6 #include <machine/sysarch.h>
      7 #include <sys/signalvar.h>
      8 #include <pthread.h>
      9 #include <signal.h>
     10 #include <string.h>
     11 #include "libcgo.h"
     12 
     13 #ifdef ARM_TP_ADDRESS
     14 // ARM_TP_ADDRESS is (ARM_VECTORS_HIGH + 0x1000) or 0xffff1000
     15 // and is known to runtime.read_tls_fallback. Verify it with
     16 // cpp.
     17 #if ARM_TP_ADDRESS != 0xffff1000
     18 #error Wrong ARM_TP_ADDRESS!
     19 #endif
     20 #endif
     21 
     22 static void *threadentry(void*);
     23 
     24 static void (*setg_gcc)(void*);
     25 
     26 void
     27 x_cgo_init(G *g, void (*setg)(void*))
     28 {
     29 	pthread_attr_t attr;
     30 	size_t size;
     31 
     32 	setg_gcc = setg;
     33 	pthread_attr_init(&attr);
     34 	pthread_attr_getstacksize(&attr, &size);
     35 	g->stacklo = (uintptr)&attr - size + 4096;
     36 	pthread_attr_destroy(&attr);
     37 }
     38 
     39 
     40 void
     41 _cgo_sys_thread_start(ThreadStart *ts)
     42 {
     43 	pthread_attr_t attr;
     44 	sigset_t ign, oset;
     45 	pthread_t p;
     46 	size_t size;
     47 	int err;
     48 
     49 	SIGFILLSET(ign);
     50 	pthread_sigmask(SIG_SETMASK, &ign, &oset);
     51 
     52 	// Not sure why the memset is necessary here,
     53 	// but without it, we get a bogus stack size
     54 	// out of pthread_attr_getstacksize.  C'est la Linux.
     55 	memset(&attr, 0, sizeof attr);
     56 	pthread_attr_init(&attr);
     57 	size = 0;
     58 	pthread_attr_getstacksize(&attr, &size);
     59 	// Leave stacklo=0 and set stackhi=size; mstack will do the rest.
     60 	ts->g->stackhi = size;
     61 	err = pthread_create(&p, &attr, threadentry, ts);
     62 
     63 	pthread_sigmask(SIG_SETMASK, &oset, nil);
     64 
     65 	if (err != 0) {
     66 		fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
     67 		abort();
     68 	}
     69 }
     70 
     71 extern void crosscall_arm1(void (*fn)(void), void (*setg_gcc)(void*), void *g);
     72 static void*
     73 threadentry(void *v)
     74 {
     75 	ThreadStart ts;
     76 
     77 	ts = *(ThreadStart*)v;
     78 	free(v);
     79 
     80 	crosscall_arm1(ts.fn, setg_gcc, (void*)ts.g);
     81 	return nil;
     82 }
     83