2010-04-06 00:44:05 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2010-12-08 12:33:17 -07:00
|
|
|
#define WIN64_LEAN_AND_MEAN
|
2010-04-06 00:44:05 -06:00
|
|
|
#include <windows.h>
|
2012-11-17 11:06:08 -07:00
|
|
|
#include <process.h>
|
2010-04-06 00:44:05 -06:00
|
|
|
#include "libcgo.h"
|
|
|
|
|
2012-11-17 11:06:08 -07:00
|
|
|
static void threadentry(void*);
|
2010-04-06 00:44:05 -06:00
|
|
|
|
2011-12-07 06:53:17 -07:00
|
|
|
/* 2MB is default stack size for 64-bit Windows.
|
|
|
|
Allocation granularity on Windows is typically 64 KB.
|
|
|
|
The constant is also hardcoded in cmd/ld/pe.c (keep synchronized). */
|
2010-12-08 12:33:17 -07:00
|
|
|
#define STACKSIZE (2*1024*1024)
|
2010-04-06 00:44:05 -06:00
|
|
|
|
2010-12-08 14:35:05 -07:00
|
|
|
static void
|
2011-11-09 13:11:48 -07:00
|
|
|
xinitcgo(G *g)
|
2010-04-06 00:44:05 -06:00
|
|
|
{
|
2011-11-09 13:11:48 -07:00
|
|
|
int tmp;
|
2011-12-07 06:53:17 -07:00
|
|
|
g->stackguard = (uintptr)&tmp - STACKSIZE + 8*1024;
|
2010-04-06 00:44:05 -06:00
|
|
|
}
|
|
|
|
|
2011-11-09 13:11:48 -07:00
|
|
|
void (*initcgo)(G*) = xinitcgo;
|
2010-12-08 14:35:05 -07:00
|
|
|
|
2010-04-06 00:44:05 -06:00
|
|
|
void
|
|
|
|
libcgo_sys_thread_start(ThreadStart *ts)
|
|
|
|
{
|
2011-12-07 06:53:17 -07:00
|
|
|
_beginthread(threadentry, 0, ts);
|
2010-04-06 00:44:05 -06:00
|
|
|
}
|
|
|
|
|
2012-11-17 11:06:08 -07:00
|
|
|
static void
|
2010-04-06 00:44:05 -06:00
|
|
|
threadentry(void *v)
|
|
|
|
{
|
2010-09-27 07:44:56 -06:00
|
|
|
ThreadStart ts;
|
2011-07-19 08:47:33 -06:00
|
|
|
void *tls0;
|
2010-09-27 07:44:56 -06:00
|
|
|
|
|
|
|
ts = *(ThreadStart*)v;
|
|
|
|
free(v);
|
|
|
|
|
|
|
|
ts.g->stackbase = (uintptr)&ts;
|
2011-12-07 06:53:17 -07:00
|
|
|
ts.g->stackguard = (uintptr)&ts - STACKSIZE + 8*1024;
|
2010-09-27 07:44:56 -06:00
|
|
|
|
2011-06-29 01:37:56 -06:00
|
|
|
/*
|
|
|
|
* Set specific keys in thread local storage.
|
|
|
|
*/
|
2011-07-19 08:47:33 -06:00
|
|
|
tls0 = (void*)LocalAlloc(LPTR, 64);
|
2011-06-29 01:37:56 -06:00
|
|
|
asm volatile (
|
2012-01-08 17:23:07 -07:00
|
|
|
"movq %0, %%gs:0x28\n" // MOVL tls0, 0x28(GS)
|
|
|
|
"movq %%gs:0x28, %%rax\n" // MOVQ 0x28(GS), tmp
|
2011-07-19 08:47:33 -06:00
|
|
|
"movq %1, 0(%%rax)\n" // MOVQ g, 0(GS)
|
|
|
|
"movq %2, 8(%%rax)\n" // MOVQ m, 8(GS)
|
|
|
|
:: "r"(tls0), "r"(ts.g), "r"(ts.m) : "%rax"
|
2011-06-29 01:37:56 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
crosscall_amd64(ts.fn);
|
2010-04-06 00:44:05 -06:00
|
|
|
}
|