github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/runtime/cgo/gcc_windows_amd64.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  #define WIN64_LEAN_AND_MEAN
     6  #include <windows.h>
     7  #include <process.h>
     8  #include "libcgo.h"
     9  
    10  static void threadentry(void*);
    11  
    12  /* 2MB is default stack size for 64-bit Windows.
    13     Allocation granularity on Windows is typically 64 KB.
    14     The constant is also hardcoded in cmd/ld/pe.c (keep synchronized). */
    15  #define STACKSIZE (2*1024*1024)
    16  
    17  void
    18  x_cgo_init(G *g)
    19  {
    20  	int tmp;
    21  	g->stackguard = (uintptr)&tmp - STACKSIZE + 8*1024;
    22  }
    23  
    24  
    25  void
    26  _cgo_sys_thread_start(ThreadStart *ts)
    27  {
    28  	_beginthread(threadentry, 0, ts);
    29  }
    30  
    31  static void
    32  threadentry(void *v)
    33  {
    34  	ThreadStart ts;
    35  	void *tls0;
    36  
    37  	ts = *(ThreadStart*)v;
    38  	free(v);
    39  
    40  	ts.g->stackbase = (uintptr)&ts;
    41  	ts.g->stackguard = (uintptr)&ts - STACKSIZE + 8*1024;
    42  
    43  	/*
    44  	 * Set specific keys in thread local storage.
    45  	 */
    46  	tls0 = (void*)LocalAlloc(LPTR, 64);
    47  	asm volatile (
    48  	  "movq %0, %%gs:0x28\n"	// MOVL tls0, 0x28(GS)
    49  	  "movq %%gs:0x28, %%rax\n" // MOVQ 0x28(GS), tmp
    50  	  "movq %1, 0(%%rax)\n" // MOVQ g, 0(GS)
    51  	  "movq %2, 8(%%rax)\n" // MOVQ m, 8(GS)
    52  	  :: "r"(tls0), "r"(ts.g), "r"(ts.m) : "%rax"
    53  	);
    54  
    55  	crosscall_amd64(ts.fn);
    56  }