github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/src/runtime/os1_windows.go (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  package runtime
     6  
     7  import (
     8  	"runtime/internal/atomic"
     9  	"unsafe"
    10  )
    11  
    12  //go:cgo_import_dynamic runtime._AddVectoredExceptionHandler AddVectoredExceptionHandler%2 "kernel32.dll"
    13  //go:cgo_import_dynamic runtime._CloseHandle CloseHandle%1 "kernel32.dll"
    14  //go:cgo_import_dynamic runtime._CreateEventA CreateEventA%4 "kernel32.dll"
    15  //go:cgo_import_dynamic runtime._CreateIoCompletionPort CreateIoCompletionPort%4 "kernel32.dll"
    16  //go:cgo_import_dynamic runtime._CreateThread CreateThread%6 "kernel32.dll"
    17  //go:cgo_import_dynamic runtime._CreateWaitableTimerA CreateWaitableTimerA%3 "kernel32.dll"
    18  //go:cgo_import_dynamic runtime._CryptAcquireContextW CryptAcquireContextW%5 "advapi32.dll"
    19  //go:cgo_import_dynamic runtime._CryptGenRandom CryptGenRandom%3 "advapi32.dll"
    20  //go:cgo_import_dynamic runtime._CryptReleaseContext CryptReleaseContext%2 "advapi32.dll"
    21  //go:cgo_import_dynamic runtime._DuplicateHandle DuplicateHandle%7 "kernel32.dll"
    22  //go:cgo_import_dynamic runtime._ExitProcess ExitProcess%1 "kernel32.dll"
    23  //go:cgo_import_dynamic runtime._FreeEnvironmentStringsW FreeEnvironmentStringsW%1 "kernel32.dll"
    24  //go:cgo_import_dynamic runtime._GetConsoleMode GetConsoleMode%2 "kernel32.dll"
    25  //go:cgo_import_dynamic runtime._GetEnvironmentStringsW GetEnvironmentStringsW%0 "kernel32.dll"
    26  //go:cgo_import_dynamic runtime._GetProcAddress GetProcAddress%2 "kernel32.dll"
    27  //go:cgo_import_dynamic runtime._GetProcessAffinityMask GetProcessAffinityMask%3 "kernel32.dll"
    28  //go:cgo_import_dynamic runtime._GetQueuedCompletionStatus GetQueuedCompletionStatus%5 "kernel32.dll"
    29  //go:cgo_import_dynamic runtime._GetStdHandle GetStdHandle%1 "kernel32.dll"
    30  //go:cgo_import_dynamic runtime._GetSystemInfo GetSystemInfo%1 "kernel32.dll"
    31  //go:cgo_import_dynamic runtime._GetThreadContext GetThreadContext%2 "kernel32.dll"
    32  //go:cgo_import_dynamic runtime._LoadLibraryW LoadLibraryW%1 "kernel32.dll"
    33  //go:cgo_import_dynamic runtime._LoadLibraryA LoadLibraryA%1 "kernel32.dll"
    34  //go:cgo_import_dynamic runtime._NtWaitForSingleObject NtWaitForSingleObject%3 "ntdll.dll"
    35  //go:cgo_import_dynamic runtime._ResumeThread ResumeThread%1 "kernel32.dll"
    36  //go:cgo_import_dynamic runtime._SetConsoleCtrlHandler SetConsoleCtrlHandler%2 "kernel32.dll"
    37  //go:cgo_import_dynamic runtime._SetErrorMode SetErrorMode%1 "kernel32.dll"
    38  //go:cgo_import_dynamic runtime._SetEvent SetEvent%1 "kernel32.dll"
    39  //go:cgo_import_dynamic runtime._SetProcessPriorityBoost SetProcessPriorityBoost%2 "kernel32.dll"
    40  //go:cgo_import_dynamic runtime._SetThreadPriority SetThreadPriority%2 "kernel32.dll"
    41  //go:cgo_import_dynamic runtime._SetUnhandledExceptionFilter SetUnhandledExceptionFilter%1 "kernel32.dll"
    42  //go:cgo_import_dynamic runtime._SetWaitableTimer SetWaitableTimer%6 "kernel32.dll"
    43  //go:cgo_import_dynamic runtime._SuspendThread SuspendThread%1 "kernel32.dll"
    44  //go:cgo_import_dynamic runtime._VirtualAlloc VirtualAlloc%4 "kernel32.dll"
    45  //go:cgo_import_dynamic runtime._VirtualFree VirtualFree%3 "kernel32.dll"
    46  //go:cgo_import_dynamic runtime._WSAGetOverlappedResult WSAGetOverlappedResult%5 "ws2_32.dll"
    47  //go:cgo_import_dynamic runtime._WaitForSingleObject WaitForSingleObject%2 "kernel32.dll"
    48  //go:cgo_import_dynamic runtime._WriteConsoleW WriteConsoleW%5 "kernel32.dll"
    49  //go:cgo_import_dynamic runtime._WriteFile WriteFile%5 "kernel32.dll"
    50  
    51  var (
    52  	// Following syscalls are available on every Windows PC.
    53  	// All these variables are set by the Windows executable
    54  	// loader before the Go program starts.
    55  	_AddVectoredExceptionHandler,
    56  	_CloseHandle,
    57  	_CreateEventA,
    58  	_CreateIoCompletionPort,
    59  	_CreateThread,
    60  	_CreateWaitableTimerA,
    61  	_CryptAcquireContextW,
    62  	_CryptGenRandom,
    63  	_CryptReleaseContext,
    64  	_DuplicateHandle,
    65  	_ExitProcess,
    66  	_FreeEnvironmentStringsW,
    67  	_GetConsoleMode,
    68  	_GetEnvironmentStringsW,
    69  	_GetProcAddress,
    70  	_GetProcessAffinityMask,
    71  	_GetQueuedCompletionStatus,
    72  	_GetStdHandle,
    73  	_GetSystemInfo,
    74  	_GetThreadContext,
    75  	_LoadLibraryW,
    76  	_LoadLibraryA,
    77  	_NtWaitForSingleObject,
    78  	_ResumeThread,
    79  	_SetConsoleCtrlHandler,
    80  	_SetErrorMode,
    81  	_SetEvent,
    82  	_SetProcessPriorityBoost,
    83  	_SetThreadPriority,
    84  	_SetUnhandledExceptionFilter,
    85  	_SetWaitableTimer,
    86  	_SuspendThread,
    87  	_VirtualAlloc,
    88  	_VirtualFree,
    89  	_WSAGetOverlappedResult,
    90  	_WaitForSingleObject,
    91  	_WriteConsoleW,
    92  	_WriteFile stdFunction
    93  
    94  	// Following syscalls are only available on some Windows PCs.
    95  	// We will load syscalls, if available, before using them.
    96  	_AddVectoredContinueHandler,
    97  	_GetQueuedCompletionStatusEx stdFunction
    98  )
    99  
   100  type sigset struct{}
   101  
   102  // Call a Windows function with stdcall conventions,
   103  // and switch to os stack during the call.
   104  func asmstdcall(fn unsafe.Pointer)
   105  
   106  var asmstdcallAddr unsafe.Pointer
   107  
   108  func loadOptionalSyscalls() {
   109  	var buf [50]byte // large enough for longest string
   110  	strtoptr := func(s string) uintptr {
   111  		buf[copy(buf[:], s)] = 0 // nil-terminated for OS
   112  		return uintptr(noescape(unsafe.Pointer(&buf[0])))
   113  	}
   114  	l := stdcall1(_LoadLibraryA, strtoptr("kernel32.dll"))
   115  	findfunc := func(name string) stdFunction {
   116  		f := stdcall2(_GetProcAddress, l, strtoptr(name))
   117  		return stdFunction(unsafe.Pointer(f))
   118  	}
   119  	if l != 0 {
   120  		_AddVectoredContinueHandler = findfunc("AddVectoredContinueHandler")
   121  		_GetQueuedCompletionStatusEx = findfunc("GetQueuedCompletionStatusEx")
   122  	}
   123  }
   124  
   125  //go:nosplit
   126  func getLoadLibrary() uintptr {
   127  	return uintptr(unsafe.Pointer(_LoadLibraryW))
   128  }
   129  
   130  //go:nosplit
   131  func getGetProcAddress() uintptr {
   132  	return uintptr(unsafe.Pointer(_GetProcAddress))
   133  }
   134  
   135  func getproccount() int32 {
   136  	var mask, sysmask uintptr
   137  	ret := stdcall3(_GetProcessAffinityMask, currentProcess, uintptr(unsafe.Pointer(&mask)), uintptr(unsafe.Pointer(&sysmask)))
   138  	if ret != 0 {
   139  		n := 0
   140  		maskbits := int(unsafe.Sizeof(mask) * 8)
   141  		for i := 0; i < maskbits; i++ {
   142  			if mask&(1<<uint(i)) != 0 {
   143  				n++
   144  			}
   145  		}
   146  		if n != 0 {
   147  			return int32(n)
   148  		}
   149  	}
   150  	// use GetSystemInfo if GetProcessAffinityMask fails
   151  	var info systeminfo
   152  	stdcall1(_GetSystemInfo, uintptr(unsafe.Pointer(&info)))
   153  	return int32(info.dwnumberofprocessors)
   154  }
   155  
   156  const (
   157  	currentProcess = ^uintptr(0) // -1 = current process
   158  	currentThread  = ^uintptr(1) // -2 = current thread
   159  )
   160  
   161  // in sys_windows_386.s and sys_windows_amd64.s
   162  func externalthreadhandler()
   163  
   164  func osinit() {
   165  	asmstdcallAddr = unsafe.Pointer(funcPC(asmstdcall))
   166  
   167  	setBadSignalMsg()
   168  
   169  	loadOptionalSyscalls()
   170  
   171  	disableWER()
   172  
   173  	externalthreadhandlerp = funcPC(externalthreadhandler)
   174  
   175  	initExceptionHandler()
   176  
   177  	stdcall2(_SetConsoleCtrlHandler, funcPC(ctrlhandler), 1)
   178  
   179  	ncpu = getproccount()
   180  
   181  	// Windows dynamic priority boosting assumes that a process has different types
   182  	// of dedicated threads -- GUI, IO, computational, etc. Go processes use
   183  	// equivalent threads that all do a mix of GUI, IO, computations, etc.
   184  	// In such context dynamic priority boosting does nothing but harm, so we turn it off.
   185  	stdcall2(_SetProcessPriorityBoost, currentProcess, 1)
   186  }
   187  
   188  //go:nosplit
   189  func getRandomData(r []byte) {
   190  	const (
   191  		prov_rsa_full       = 1
   192  		crypt_verifycontext = 0xF0000000
   193  	)
   194  	var handle uintptr
   195  	n := 0
   196  	if stdcall5(_CryptAcquireContextW, uintptr(unsafe.Pointer(&handle)), 0, 0, prov_rsa_full, crypt_verifycontext) != 0 {
   197  		if stdcall3(_CryptGenRandom, handle, uintptr(len(r)), uintptr(unsafe.Pointer(&r[0]))) != 0 {
   198  			n = len(r)
   199  		}
   200  		stdcall2(_CryptReleaseContext, handle, 0)
   201  	}
   202  	extendRandom(r, n)
   203  }
   204  
   205  func goenvs() {
   206  	// strings is a pointer to environment variable pairs in the form:
   207  	//     "envA=valA\x00envB=valB\x00\x00" (in UTF-16)
   208  	// Two consecutive zero bytes end the list.
   209  	strings := unsafe.Pointer(stdcall0(_GetEnvironmentStringsW))
   210  	p := (*[1 << 24]uint16)(strings)[:]
   211  
   212  	n := 0
   213  	for from, i := 0, 0; true; i++ {
   214  		if p[i] == 0 {
   215  			// empty string marks the end
   216  			if i == from {
   217  				break
   218  			}
   219  			from = i + 1
   220  			n++
   221  		}
   222  	}
   223  	envs = make([]string, n)
   224  
   225  	for i := range envs {
   226  		envs[i] = gostringw(&p[0])
   227  		for p[0] != 0 {
   228  			p = p[1:]
   229  		}
   230  		p = p[1:] // skip nil byte
   231  	}
   232  
   233  	stdcall1(_FreeEnvironmentStringsW, uintptr(strings))
   234  }
   235  
   236  //go:nosplit
   237  func exit(code int32) {
   238  	stdcall1(_ExitProcess, uintptr(code))
   239  }
   240  
   241  //go:nosplit
   242  func write(fd uintptr, buf unsafe.Pointer, n int32) int32 {
   243  	const (
   244  		_STD_OUTPUT_HANDLE = ^uintptr(10) // -11
   245  		_STD_ERROR_HANDLE  = ^uintptr(11) // -12
   246  	)
   247  	var handle uintptr
   248  	switch fd {
   249  	case 1:
   250  		handle = stdcall1(_GetStdHandle, _STD_OUTPUT_HANDLE)
   251  	case 2:
   252  		handle = stdcall1(_GetStdHandle, _STD_ERROR_HANDLE)
   253  	default:
   254  		// assume fd is real windows handle.
   255  		handle = fd
   256  	}
   257  	isASCII := true
   258  	b := (*[1 << 30]byte)(buf)[:n]
   259  	for _, x := range b {
   260  		if x >= 0x80 {
   261  			isASCII = false
   262  			break
   263  		}
   264  	}
   265  
   266  	if !isASCII {
   267  		var m uint32
   268  		isConsole := stdcall2(_GetConsoleMode, handle, uintptr(unsafe.Pointer(&m))) != 0
   269  		// If this is a console output, various non-unicode code pages can be in use.
   270  		// Use the dedicated WriteConsole call to ensure unicode is printed correctly.
   271  		if isConsole {
   272  			return int32(writeConsole(handle, buf, n))
   273  		}
   274  	}
   275  	var written uint32
   276  	stdcall5(_WriteFile, handle, uintptr(buf), uintptr(n), uintptr(unsafe.Pointer(&written)), 0)
   277  	return int32(written)
   278  }
   279  
   280  var (
   281  	utf16ConsoleBack     [1000]uint16
   282  	utf16ConsoleBackLock mutex
   283  )
   284  
   285  // writeConsole writes bufLen bytes from buf to the console File.
   286  // It returns the number of bytes written.
   287  func writeConsole(handle uintptr, buf unsafe.Pointer, bufLen int32) int {
   288  	const surr2 = (surrogateMin + surrogateMax + 1) / 2
   289  
   290  	// Do not use defer for unlock. May cause issues when printing a panic.
   291  	lock(&utf16ConsoleBackLock)
   292  
   293  	b := (*[1 << 30]byte)(buf)[:bufLen]
   294  	s := *(*string)(unsafe.Pointer(&b))
   295  
   296  	utf16tmp := utf16ConsoleBack[:]
   297  
   298  	total := len(s)
   299  	w := 0
   300  	for len(s) > 0 {
   301  		if w >= len(utf16tmp)-2 {
   302  			writeConsoleUTF16(handle, utf16tmp[:w])
   303  			w = 0
   304  		}
   305  		r, n := charntorune(s)
   306  		s = s[n:]
   307  		if r < 0x10000 {
   308  			utf16tmp[w] = uint16(r)
   309  			w++
   310  		} else {
   311  			r -= 0x10000
   312  			utf16tmp[w] = surrogateMin + uint16(r>>10)&0x3ff
   313  			utf16tmp[w+1] = surr2 + uint16(r)&0x3ff
   314  			w += 2
   315  		}
   316  	}
   317  	writeConsoleUTF16(handle, utf16tmp[:w])
   318  	unlock(&utf16ConsoleBackLock)
   319  	return total
   320  }
   321  
   322  // writeConsoleUTF16 is the dedicated windows calls that correctly prints
   323  // to the console regardless of the current code page. Input is utf-16 code points.
   324  // The handle must be a console handle.
   325  func writeConsoleUTF16(handle uintptr, b []uint16) {
   326  	l := uint32(len(b))
   327  	if l == 0 {
   328  		return
   329  	}
   330  	var written uint32
   331  	stdcall5(_WriteConsoleW,
   332  		handle,
   333  		uintptr(unsafe.Pointer(&b[0])),
   334  		uintptr(l),
   335  		uintptr(unsafe.Pointer(&written)),
   336  		0,
   337  	)
   338  	return
   339  }
   340  
   341  //go:nosplit
   342  func semasleep(ns int64) int32 {
   343  	// store ms in ns to save stack space
   344  	if ns < 0 {
   345  		ns = _INFINITE
   346  	} else {
   347  		ns = int64(timediv(ns, 1000000, nil))
   348  		if ns == 0 {
   349  			ns = 1
   350  		}
   351  	}
   352  	if stdcall2(_WaitForSingleObject, getg().m.waitsema, uintptr(ns)) != 0 {
   353  		return -1 // timeout
   354  	}
   355  	return 0
   356  }
   357  
   358  //go:nosplit
   359  func semawakeup(mp *m) {
   360  	stdcall1(_SetEvent, mp.waitsema)
   361  }
   362  
   363  //go:nosplit
   364  func semacreate(mp *m) {
   365  	if mp.waitsema != 0 {
   366  		return
   367  	}
   368  	mp.waitsema = stdcall4(_CreateEventA, 0, 0, 0, 0)
   369  }
   370  
   371  // May run with m.p==nil, so write barriers are not allowed.
   372  //go:nowritebarrier
   373  func newosproc(mp *m, stk unsafe.Pointer) {
   374  	const _STACK_SIZE_PARAM_IS_A_RESERVATION = 0x00010000
   375  	thandle := stdcall6(_CreateThread, 0, 0x20000,
   376  		funcPC(tstart_stdcall), uintptr(unsafe.Pointer(mp)),
   377  		_STACK_SIZE_PARAM_IS_A_RESERVATION, 0)
   378  	if thandle == 0 {
   379  		print("runtime: failed to create new OS thread (have ", mcount(), " already; errno=", getlasterror(), ")\n")
   380  		throw("runtime.newosproc")
   381  	}
   382  }
   383  
   384  // Called to initialize a new m (including the bootstrap m).
   385  // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
   386  func mpreinit(mp *m) {
   387  }
   388  
   389  //go:nosplit
   390  func msigsave(mp *m) {
   391  }
   392  
   393  //go:nosplit
   394  func msigrestore(mp *m) {
   395  }
   396  
   397  //go:nosplit
   398  func sigblock() {
   399  }
   400  
   401  // Called to initialize a new m (including the bootstrap m).
   402  // Called on the new thread, can not allocate memory.
   403  func minit() {
   404  	var thandle uintptr
   405  	stdcall7(_DuplicateHandle, currentProcess, currentThread, currentProcess, uintptr(unsafe.Pointer(&thandle)), 0, 0, _DUPLICATE_SAME_ACCESS)
   406  	atomic.Storeuintptr(&getg().m.thread, thandle)
   407  }
   408  
   409  // Called from dropm to undo the effect of an minit.
   410  //go:nosplit
   411  func unminit() {
   412  	tp := &getg().m.thread
   413  	stdcall1(_CloseHandle, *tp)
   414  	*tp = 0
   415  }
   416  
   417  // Described in http://www.dcl.hpi.uni-potsdam.de/research/WRK/2007/08/getting-os-information-the-kuser_shared_data-structure/
   418  type _KSYSTEM_TIME struct {
   419  	LowPart   uint32
   420  	High1Time int32
   421  	High2Time int32
   422  }
   423  
   424  const (
   425  	_INTERRUPT_TIME = 0x7ffe0008
   426  	_SYSTEM_TIME    = 0x7ffe0014
   427  )
   428  
   429  //go:nosplit
   430  func systime(addr uintptr) int64 {
   431  	timeaddr := (*_KSYSTEM_TIME)(unsafe.Pointer(addr))
   432  
   433  	var t _KSYSTEM_TIME
   434  	for i := 1; i < 10000; i++ {
   435  		// these fields must be read in that order (see URL above)
   436  		t.High1Time = timeaddr.High1Time
   437  		t.LowPart = timeaddr.LowPart
   438  		t.High2Time = timeaddr.High2Time
   439  		if t.High1Time == t.High2Time {
   440  			return int64(t.High1Time)<<32 | int64(t.LowPart)
   441  		}
   442  		if (i % 100) == 0 {
   443  			osyield()
   444  		}
   445  	}
   446  	systemstack(func() {
   447  		throw("interrupt/system time is changing too fast")
   448  	})
   449  	return 0
   450  }
   451  
   452  //go:nosplit
   453  func unixnano() int64 {
   454  	return (systime(_SYSTEM_TIME) - 116444736000000000) * 100
   455  }
   456  
   457  //go:nosplit
   458  func nanotime() int64 {
   459  	return systime(_INTERRUPT_TIME) * 100
   460  }
   461  
   462  // Calling stdcall on os stack.
   463  // May run during STW, so write barriers are not allowed.
   464  //go:nowritebarrier
   465  //go:nosplit
   466  func stdcall(fn stdFunction) uintptr {
   467  	gp := getg()
   468  	mp := gp.m
   469  	mp.libcall.fn = uintptr(unsafe.Pointer(fn))
   470  
   471  	if mp.profilehz != 0 {
   472  		// leave pc/sp for cpu profiler
   473  		mp.libcallg.set(gp)
   474  		mp.libcallpc = getcallerpc(unsafe.Pointer(&fn))
   475  		// sp must be the last, because once async cpu profiler finds
   476  		// all three values to be non-zero, it will use them
   477  		mp.libcallsp = getcallersp(unsafe.Pointer(&fn))
   478  	}
   479  	asmcgocall(asmstdcallAddr, unsafe.Pointer(&mp.libcall))
   480  	mp.libcallsp = 0
   481  	return mp.libcall.r1
   482  }
   483  
   484  //go:nosplit
   485  func stdcall0(fn stdFunction) uintptr {
   486  	mp := getg().m
   487  	mp.libcall.n = 0
   488  	mp.libcall.args = uintptr(noescape(unsafe.Pointer(&fn))) // it's unused but must be non-nil, otherwise crashes
   489  	return stdcall(fn)
   490  }
   491  
   492  //go:nosplit
   493  func stdcall1(fn stdFunction, a0 uintptr) uintptr {
   494  	mp := getg().m
   495  	mp.libcall.n = 1
   496  	mp.libcall.args = uintptr(noescape(unsafe.Pointer(&a0)))
   497  	return stdcall(fn)
   498  }
   499  
   500  //go:nosplit
   501  func stdcall2(fn stdFunction, a0, a1 uintptr) uintptr {
   502  	mp := getg().m
   503  	mp.libcall.n = 2
   504  	mp.libcall.args = uintptr(noescape(unsafe.Pointer(&a0)))
   505  	return stdcall(fn)
   506  }
   507  
   508  //go:nosplit
   509  func stdcall3(fn stdFunction, a0, a1, a2 uintptr) uintptr {
   510  	mp := getg().m
   511  	mp.libcall.n = 3
   512  	mp.libcall.args = uintptr(noescape(unsafe.Pointer(&a0)))
   513  	return stdcall(fn)
   514  }
   515  
   516  //go:nosplit
   517  func stdcall4(fn stdFunction, a0, a1, a2, a3 uintptr) uintptr {
   518  	mp := getg().m
   519  	mp.libcall.n = 4
   520  	mp.libcall.args = uintptr(noescape(unsafe.Pointer(&a0)))
   521  	return stdcall(fn)
   522  }
   523  
   524  //go:nosplit
   525  func stdcall5(fn stdFunction, a0, a1, a2, a3, a4 uintptr) uintptr {
   526  	mp := getg().m
   527  	mp.libcall.n = 5
   528  	mp.libcall.args = uintptr(noescape(unsafe.Pointer(&a0)))
   529  	return stdcall(fn)
   530  }
   531  
   532  //go:nosplit
   533  func stdcall6(fn stdFunction, a0, a1, a2, a3, a4, a5 uintptr) uintptr {
   534  	mp := getg().m
   535  	mp.libcall.n = 6
   536  	mp.libcall.args = uintptr(noescape(unsafe.Pointer(&a0)))
   537  	return stdcall(fn)
   538  }
   539  
   540  //go:nosplit
   541  func stdcall7(fn stdFunction, a0, a1, a2, a3, a4, a5, a6 uintptr) uintptr {
   542  	mp := getg().m
   543  	mp.libcall.n = 7
   544  	mp.libcall.args = uintptr(noescape(unsafe.Pointer(&a0)))
   545  	return stdcall(fn)
   546  }
   547  
   548  // in sys_windows_386.s and sys_windows_amd64.s
   549  func usleep1(usec uint32)
   550  
   551  //go:nosplit
   552  func osyield() {
   553  	usleep1(1)
   554  }
   555  
   556  //go:nosplit
   557  func usleep(us uint32) {
   558  	// Have 1us units; want 100ns units.
   559  	usleep1(10 * us)
   560  }
   561  
   562  func ctrlhandler1(_type uint32) uint32 {
   563  	var s uint32
   564  
   565  	switch _type {
   566  	case _CTRL_C_EVENT, _CTRL_BREAK_EVENT:
   567  		s = _SIGINT
   568  	default:
   569  		return 0
   570  	}
   571  
   572  	if sigsend(s) {
   573  		return 1
   574  	}
   575  	exit(2) // SIGINT, SIGTERM, etc
   576  	return 0
   577  }
   578  
   579  // in sys_windows_386.s and sys_windows_amd64.s
   580  func profileloop()
   581  
   582  var profiletimer uintptr
   583  
   584  func profilem(mp *m) {
   585  	var r *context
   586  	rbuf := make([]byte, unsafe.Sizeof(*r)+15)
   587  
   588  	tls := &mp.tls[0]
   589  	gp := *((**g)(unsafe.Pointer(tls)))
   590  
   591  	// align Context to 16 bytes
   592  	r = (*context)(unsafe.Pointer((uintptr(unsafe.Pointer(&rbuf[15]))) &^ 15))
   593  	r.contextflags = _CONTEXT_CONTROL
   594  	stdcall2(_GetThreadContext, mp.thread, uintptr(unsafe.Pointer(r)))
   595  	sigprof(r.ip(), r.sp(), 0, gp, mp)
   596  }
   597  
   598  func profileloop1(param uintptr) uint32 {
   599  	stdcall2(_SetThreadPriority, currentThread, _THREAD_PRIORITY_HIGHEST)
   600  
   601  	for {
   602  		stdcall2(_WaitForSingleObject, profiletimer, _INFINITE)
   603  		first := (*m)(atomic.Loadp(unsafe.Pointer(&allm)))
   604  		for mp := first; mp != nil; mp = mp.alllink {
   605  			thread := atomic.Loaduintptr(&mp.thread)
   606  			// Do not profile threads blocked on Notes,
   607  			// this includes idle worker threads,
   608  			// idle timer thread, idle heap scavenger, etc.
   609  			if thread == 0 || mp.profilehz == 0 || mp.blocked {
   610  				continue
   611  			}
   612  			stdcall1(_SuspendThread, thread)
   613  			if mp.profilehz != 0 && !mp.blocked {
   614  				profilem(mp)
   615  			}
   616  			stdcall1(_ResumeThread, thread)
   617  		}
   618  	}
   619  }
   620  
   621  var cpuprofilerlock mutex
   622  
   623  func resetcpuprofiler(hz int32) {
   624  	lock(&cpuprofilerlock)
   625  	if profiletimer == 0 {
   626  		timer := stdcall3(_CreateWaitableTimerA, 0, 0, 0)
   627  		atomic.Storeuintptr(&profiletimer, timer)
   628  		thread := stdcall6(_CreateThread, 0, 0, funcPC(profileloop), 0, 0, 0)
   629  		stdcall2(_SetThreadPriority, thread, _THREAD_PRIORITY_HIGHEST)
   630  		stdcall1(_CloseHandle, thread)
   631  	}
   632  	unlock(&cpuprofilerlock)
   633  
   634  	ms := int32(0)
   635  	due := ^int64(^uint64(1 << 63))
   636  	if hz > 0 {
   637  		ms = 1000 / hz
   638  		if ms == 0 {
   639  			ms = 1
   640  		}
   641  		due = int64(ms) * -10000
   642  	}
   643  	stdcall6(_SetWaitableTimer, profiletimer, uintptr(unsafe.Pointer(&due)), uintptr(ms), 0, 0, 0)
   644  	atomic.Store((*uint32)(unsafe.Pointer(&getg().m.profilehz)), uint32(hz))
   645  }
   646  
   647  func memlimit() uintptr {
   648  	return 0
   649  }