github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/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 <pthread.h>
     8  #include <string.h>
     9  #include "libcgo.h"
    10  
    11  #ifdef ARM_TP_ADDRESS
    12  // ARM_TP_ADDRESS is (ARM_VECTORS_HIGH + 0x1000) or 0xffff1000
    13  // and is known to runtime.read_tls_fallback. Verify it with
    14  // cpp.
    15  #if ARM_TP_ADDRESS != 0xffff1000
    16  #error Wrong ARM_TP_ADDRESS!
    17  #endif
    18  #endif
    19  
    20  static void *threadentry(void*);
    21  
    22  static void (*setmg_gcc)(void*, void*);
    23  
    24  void
    25  x_cgo_init(G *g, void (*setmg)(void*, void*))
    26  {
    27  	pthread_attr_t attr;
    28  	size_t size;
    29  
    30  	setmg_gcc = setmg;
    31  	pthread_attr_init(&attr);
    32  	pthread_attr_getstacksize(&attr, &size);
    33  	g->stackguard = (uintptr)&attr - size + 4096;
    34  	pthread_attr_destroy(&attr);
    35  }
    36  
    37  
    38  void
    39  _cgo_sys_thread_start(ThreadStart *ts)
    40  {
    41  	pthread_attr_t attr;
    42  	pthread_t p;
    43  	size_t size;
    44  	int err;
    45  
    46  	// Not sure why the memset is necessary here,
    47  	// but without it, we get a bogus stack size
    48  	// out of pthread_attr_getstacksize.  C'est la Linux.
    49  	memset(&attr, 0, sizeof attr);
    50  	pthread_attr_init(&attr);
    51  	size = 0;
    52  	pthread_attr_getstacksize(&attr, &size);
    53  	ts->g->stackguard = size;
    54  	err = pthread_create(&p, &attr, threadentry, ts);
    55  	if (err != 0) {
    56  		fprintf(stderr, "runtime/cgo: pthread_create failed: %s\n", strerror(err));
    57  		abort();
    58  	}
    59  }
    60  
    61  extern void crosscall_arm2(void (*fn)(void), void (*setmg_gcc)(void*, void*), void *g, void *m);
    62  static void*
    63  threadentry(void *v)
    64  {
    65  	ThreadStart ts;
    66  
    67  	ts = *(ThreadStart*)v;
    68  	free(v);
    69  
    70  	ts.g->stackbase = (uintptr)&ts;
    71  
    72  	/*
    73  	 * _cgo_sys_thread_start set stackguard to stack size;
    74  	 * change to actual guard pointer.
    75  	 */
    76  	ts.g->stackguard = (uintptr)&ts - ts.g->stackguard + 4096 * 2;
    77  
    78  	crosscall_arm2(ts.fn, setmg_gcc, (void*)ts.m, (void*)ts.g);
    79  	return nil;
    80  }