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 #define WIN64_LEAN_AND_MEAN
      6 #include <windows.h>
      7 #include <process.h>
      8 #include <stdlib.h>
      9 #include <stdio.h>
     10 #include "libcgo.h"
     11 
     12 static void threadentry(void*);
     13 
     14 /* 2MB is default stack size for 64-bit Windows.
     15    Allocation granularity on Windows is typically 64 KB.
     16    The constant is also hardcoded in cmd/ld/pe.c (keep synchronized). */
     17 #define STACKSIZE (2*1024*1024)
     18 
     19 void
     20 x_cgo_init(G *g)
     21 {
     22 	int tmp;
     23 	g->stacklo = (uintptr)&tmp - STACKSIZE + 8*1024;
     24 }
     25 
     26 
     27 void
     28 _cgo_sys_thread_start(ThreadStart *ts)
     29 {
     30 	uintptr_t thandle;
     31 
     32 	thandle = _beginthread(threadentry, 0, ts);
     33 	if(thandle == -1) {
     34 		fprintf(stderr, "runtime: failed to create new OS thread (%d)\n", errno);
     35 		abort();
     36 	}
     37 }
     38 
     39 static void
     40 threadentry(void *v)
     41 {
     42 	ThreadStart ts;
     43 
     44 	ts = *(ThreadStart*)v;
     45 	free(v);
     46 
     47 	ts.g->stackhi = (uintptr)&ts;
     48 	ts.g->stacklo = (uintptr)&ts - STACKSIZE + 8*1024;
     49 
     50 	/*
     51 	 * Set specific keys in thread local storage.
     52 	 */
     53 	asm volatile (
     54 	  "movq %0, %%gs:0x28\n"	// MOVL tls0, 0x28(GS)
     55 	  "movq %%gs:0x28, %%rax\n" // MOVQ 0x28(GS), tmp
     56 	  "movq %1, 0(%%rax)\n" // MOVQ g, 0(GS)
     57 	  :: "r"(ts.tls), "r"(ts.g) : "%rax"
     58 	);
     59 
     60 	crosscall_amd64(ts.fn);
     61 }
     62