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

     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 (*setmg_gcc)(void*, void*);
    25  
    26  void
    27  x_cgo_init(G *g, void (*setmg)(void*, void*))
    28  {
    29  	pthread_attr_t attr;
    30  	size_t size;
    31  
    32  	setmg_gcc = setmg;
    33  	pthread_attr_init(&attr);
    34  	pthread_attr_getstacksize(&attr, &size);
    35  	g->stackguard = (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  	ts->g->stackguard = size;
    60  	err = pthread_create(&p, &attr, threadentry, ts);
    61  
    62  	pthread_sigmask(SIG_SETMASK, &oset, nil);
    63  
    64  	if (err != 0) {
    65  		fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
    66  		abort();
    67  	}
    68  }
    69  
    70  extern void crosscall_arm2(void (*fn)(void), void (*setmg_gcc)(void*, void*), void *g, void *m);
    71  static void*
    72  threadentry(void *v)
    73  {
    74  	ThreadStart ts;
    75  
    76  	ts = *(ThreadStart*)v;
    77  	free(v);
    78  
    79  	ts.g->stackbase = (uintptr)&ts;
    80  
    81  	/*
    82  	 * _cgo_sys_thread_start set stackguard to stack size;
    83  	 * change to actual guard pointer.
    84  	 */
    85  	ts.g->stackguard = (uintptr)&ts - ts.g->stackguard + 4096 * 2;
    86  
    87  	crosscall_arm2(ts.fn, setmg_gcc, (void*)ts.m, (void*)ts.g);
    88  	return nil;
    89  }