| // 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. | |
| package runtime | |
| import ( | |
| "internal/abi" | |
| "internal/runtime/atomic" | |
| "internal/runtime/sys" | |
| "internal/runtime/syscall/windows" | |
| "unsafe" | |
| ) | |
| // TODO(brainman): should not need those | |
| const ( | |
| _NSIG = 65 | |
| ) | |
| //go:cgo_import_dynamic runtime._AddVectoredContinueHandler AddVectoredContinueHandler%2 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._AddVectoredExceptionHandler AddVectoredExceptionHandler%2 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._CloseHandle CloseHandle%1 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._CreateEventA CreateEventA%4 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._CreateIoCompletionPort CreateIoCompletionPort%4 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._CreateThread CreateThread%6 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._CreateWaitableTimerA CreateWaitableTimerA%3 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._CreateWaitableTimerExW CreateWaitableTimerExW%4 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._DuplicateHandle DuplicateHandle%7 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._ExitProcess ExitProcess%1 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._FreeEnvironmentStringsW FreeEnvironmentStringsW%1 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._GetConsoleMode GetConsoleMode%2 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._GetCurrentThreadId GetCurrentThreadId%0 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._GetEnvironmentStringsW GetEnvironmentStringsW%0 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._GetErrorMode GetErrorMode%0 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._GetProcAddress GetProcAddress%2 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._GetProcessAffinityMask GetProcessAffinityMask%3 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._GetQueuedCompletionStatusEx GetQueuedCompletionStatusEx%6 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._GetStdHandle GetStdHandle%1 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._GetSystemDirectoryA GetSystemDirectoryA%2 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._GetSystemInfo GetSystemInfo%1 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._GetThreadContext GetThreadContext%2 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._IsProcessorFeaturePresent IsProcessorFeaturePresent%1 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._SetThreadContext SetThreadContext%2 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._LoadLibraryExW LoadLibraryExW%3 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._PostQueuedCompletionStatus PostQueuedCompletionStatus%4 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._QueryPerformanceCounter QueryPerformanceCounter%1 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._QueryPerformanceFrequency QueryPerformanceFrequency%1 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._RaiseFailFastException RaiseFailFastException%3 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._ResumeThread ResumeThread%1 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._RtlLookupFunctionEntry RtlLookupFunctionEntry%3 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._RtlVirtualUnwind RtlVirtualUnwind%8 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._SetConsoleCtrlHandler SetConsoleCtrlHandler%2 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._SetErrorMode SetErrorMode%1 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._SetEvent SetEvent%1 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._SetProcessPriorityBoost SetProcessPriorityBoost%2 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._SetThreadPriority SetThreadPriority%2 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._SetUnhandledExceptionFilter SetUnhandledExceptionFilter%1 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._SetWaitableTimer SetWaitableTimer%6 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._SuspendThread SuspendThread%1 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._SwitchToThread SwitchToThread%0 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._VirtualAlloc VirtualAlloc%4 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._VirtualFree VirtualFree%3 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._VirtualQuery VirtualQuery%3 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._WaitForSingleObject WaitForSingleObject%2 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._WaitForMultipleObjects WaitForMultipleObjects%4 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._WerGetFlags WerGetFlags%2 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._WerSetFlags WerSetFlags%1 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._WriteConsoleW WriteConsoleW%5 "kernel32.dll" | |
| //go:cgo_import_dynamic runtime._WriteFile WriteFile%5 "kernel32.dll" | |
| type stdFunction unsafe.Pointer | |
| var ( | |
| // Following syscalls are available on every Windows PC. | |
| // All these variables are set by the Windows executable | |
| // loader before the Go program starts. | |
| _AddVectoredContinueHandler, | |
| _AddVectoredExceptionHandler, | |
| _CloseHandle, | |
| _CreateEventA, | |
| _CreateIoCompletionPort, | |
| _CreateThread, | |
| _CreateWaitableTimerA, | |
| _CreateWaitableTimerExW, | |
| _DuplicateHandle, | |
| _ExitProcess, | |
| _FreeEnvironmentStringsW, | |
| _GetConsoleMode, | |
| _GetCurrentThreadId, | |
| _GetEnvironmentStringsW, | |
| _GetErrorMode, | |
| _GetProcAddress, | |
| _GetProcessAffinityMask, | |
| _GetQueuedCompletionStatusEx, | |
| _GetStdHandle, | |
| _GetSystemDirectoryA, | |
| _GetSystemInfo, | |
| _GetThreadContext, | |
| _IsProcessorFeaturePresent, | |
| _SetThreadContext, | |
| _LoadLibraryExW, | |
| _PostQueuedCompletionStatus, | |
| _QueryPerformanceCounter, | |
| _QueryPerformanceFrequency, | |
| _RaiseFailFastException, | |
| _ResumeThread, | |
| _RtlLookupFunctionEntry, | |
| _RtlVirtualUnwind, | |
| _SetConsoleCtrlHandler, | |
| _SetErrorMode, | |
| _SetEvent, | |
| _SetProcessPriorityBoost, | |
| _SetThreadPriority, | |
| _SetUnhandledExceptionFilter, | |
| _SetWaitableTimer, | |
| _SuspendThread, | |
| _SwitchToThread, | |
| _VirtualAlloc, | |
| _VirtualFree, | |
| _VirtualQuery, | |
| _WaitForSingleObject, | |
| _WaitForMultipleObjects, | |
| _WerGetFlags, | |
| _WerSetFlags, | |
| _WriteConsoleW, | |
| _WriteFile, | |
| _ stdFunction | |
| // Use ProcessPrng to generate cryptographically random data. | |
| _ProcessPrng stdFunction | |
| // Load ntdll.dll manually during startup, otherwise Mingw | |
| // links wrong printf function to cgo executable (see issue | |
| // 12030 for details). | |
| _NtCreateWaitCompletionPacket stdFunction | |
| _NtAssociateWaitCompletionPacket stdFunction | |
| _NtCancelWaitCompletionPacket stdFunction | |
| _RtlGetCurrentPeb stdFunction | |
| _RtlGetVersion stdFunction | |
| // These are from non-kernel32.dll, so we prefer to LoadLibraryEx them. | |
| _timeBeginPeriod, | |
| _timeEndPeriod, | |
| _ stdFunction | |
| ) | |
| var ( | |
| bcryptprimitivesdll = [...]uint16{'b', 'c', 'r', 'y', 'p', 't', 'p', 'r', 'i', 'm', 'i', 't', 'i', 'v', 'e', 's', '.', 'd', 'l', 'l', 0} | |
| ntdlldll = [...]uint16{'n', 't', 'd', 'l', 'l', '.', 'd', 'l', 'l', 0} | |
| powrprofdll = [...]uint16{'p', 'o', 'w', 'r', 'p', 'r', 'o', 'f', '.', 'd', 'l', 'l', 0} | |
| winmmdll = [...]uint16{'w', 'i', 'n', 'm', 'm', '.', 'd', 'l', 'l', 0} | |
| ) | |
| // Function to be called by windows CreateThread | |
| // to start new os thread. | |
| func tstart_stdcall(newm *m) | |
| type mOS struct { | |
| // This is here to avoid using the G stack so the stack can move during the call. | |
| stdCallInfo windows.StdCallInfo | |
| threadLock mutex // protects "thread" and prevents closing | |
| thread uintptr // thread handle | |
| waitsema uintptr // semaphore for parking on locks | |
| resumesema uintptr // semaphore to indicate suspend/resume | |
| highResTimer uintptr // high resolution timer handle used in usleep | |
| waitIocpTimer uintptr // high resolution timer handle used in netpoll | |
| waitIocpHandle uintptr // wait completion handle used in netpoll | |
| } | |
| // Stubs so tests can link correctly. These should never be called. | |
| func open(name *byte, mode, perm int32) int32 { | |
| throw("unimplemented") | |
| return -1 | |
| } | |
| func closefd(fd int32) int32 { | |
| throw("unimplemented") | |
| return -1 | |
| } | |
| func read(fd int32, p unsafe.Pointer, n int32) int32 { | |
| throw("unimplemented") | |
| return -1 | |
| } | |
| type sigset struct{} | |
| var asmstdcallAddr unsafe.Pointer | |
| type winlibcall windows.StdCallInfo | |
| func windowsFindfunc(lib uintptr, name []byte) stdFunction { | |
| if name[len(name)-1] != 0 { | |
| throw("usage") | |
| } | |
| f := stdcall(_GetProcAddress, lib, uintptr(unsafe.Pointer(&name[0]))) | |
| return stdFunction(unsafe.Pointer(f)) | |
| } | |
| const _MAX_PATH = 260 // https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation | |
| var sysDirectory [_MAX_PATH + 1]byte | |
| var sysDirectoryLen uintptr | |
| func initSysDirectory() { | |
| l := stdcall(_GetSystemDirectoryA, uintptr(unsafe.Pointer(&sysDirectory[0])), uintptr(len(sysDirectory)-1)) | |
| if l == 0 || l > uintptr(len(sysDirectory)-1) { | |
| throw("Unable to determine system directory") | |
| } | |
| sysDirectory[l] = '\\' | |
| sysDirectoryLen = l + 1 | |
| } | |
| //go:linkname windows_GetSystemDirectory internal/syscall/windows.GetSystemDirectory | |
| func windows_GetSystemDirectory() string { | |
| return unsafe.String(&sysDirectory[0], sysDirectoryLen) | |
| } | |
| func windowsLoadSystemLib(name []uint16) uintptr { | |
| const _LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800 | |
| return stdcall(_LoadLibraryExW, uintptr(unsafe.Pointer(&name[0])), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32) | |
| } | |
| //go:linkname windows_QueryPerformanceCounter internal/syscall/windows.QueryPerformanceCounter | |
| func windows_QueryPerformanceCounter() int64 { | |
| var counter int64 | |
| stdcall(_QueryPerformanceCounter, uintptr(unsafe.Pointer(&counter))) | |
| return counter | |
| } | |
| //go:linkname windows_QueryPerformanceFrequency internal/syscall/windows.QueryPerformanceFrequency | |
| func windows_QueryPerformanceFrequency() int64 { | |
| var frequency int64 | |
| stdcall(_QueryPerformanceFrequency, uintptr(unsafe.Pointer(&frequency))) | |
| return frequency | |
| } | |
| //go:linknamestd cpu_isProcessorFeaturePresent internal/cpu.isProcessorFeaturePresent | |
| func cpu_isProcessorFeaturePresent(processorFeature uint32) bool { | |
| ret := stdcall(_IsProcessorFeaturePresent, uintptr(processorFeature)) | |
| return ret != 0 | |
| } | |
| func loadOptionalSyscalls() { | |
| bcryptPrimitives := windowsLoadSystemLib(bcryptprimitivesdll[:]) | |
| if bcryptPrimitives == 0 { | |
| throw("bcryptprimitives.dll not found") | |
| } | |
| _ProcessPrng = windowsFindfunc(bcryptPrimitives, []byte("ProcessPrng\000")) | |
| n32 := windowsLoadSystemLib(ntdlldll[:]) | |
| if n32 == 0 { | |
| throw("ntdll.dll not found") | |
| } | |
| _NtCreateWaitCompletionPacket = windowsFindfunc(n32, []byte("NtCreateWaitCompletionPacket\000")) | |
| if _NtCreateWaitCompletionPacket != nil { | |
| // These functions should exists if NtCreateWaitCompletionPacket exists. | |
| _NtAssociateWaitCompletionPacket = windowsFindfunc(n32, []byte("NtAssociateWaitCompletionPacket\000")) | |
| if _NtAssociateWaitCompletionPacket == nil { | |
| throw("NtCreateWaitCompletionPacket exists but NtAssociateWaitCompletionPacket does not") | |
| } | |
| _NtCancelWaitCompletionPacket = windowsFindfunc(n32, []byte("NtCancelWaitCompletionPacket\000")) | |
| if _NtCancelWaitCompletionPacket == nil { | |
| throw("NtCreateWaitCompletionPacket exists but NtCancelWaitCompletionPacket does not") | |
| } | |
| } | |
| _RtlGetCurrentPeb = windowsFindfunc(n32, []byte("RtlGetCurrentPeb\000")) | |
| _RtlGetVersion = windowsFindfunc(n32, []byte("RtlGetVersion\000")) | |
| } | |
| func monitorSuspendResume() { | |
| const ( | |
| _DEVICE_NOTIFY_CALLBACK = 2 | |
| ) | |
| type _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS struct { | |
| callback uintptr | |
| context uintptr | |
| } | |
| powrprof := windowsLoadSystemLib(powrprofdll[:]) | |
| if powrprof == 0 { | |
| return // Running on Windows 7, where we don't need it anyway. | |
| } | |
| powerRegisterSuspendResumeNotification := windowsFindfunc(powrprof, []byte("PowerRegisterSuspendResumeNotification\000")) | |
| if powerRegisterSuspendResumeNotification == nil { | |
| return // Running on Windows 7, where we don't need it anyway. | |
| } | |
| var fn any = func(context uintptr, changeType uint32, setting uintptr) uintptr { | |
| for mp := (*m)(atomic.Loadp(unsafe.Pointer(&allm))); mp != nil; mp = mp.alllink { | |
| if mp.resumesema != 0 { | |
| stdcall(_SetEvent, mp.resumesema) | |
| } | |
| } | |
| return 0 | |
| } | |
| params := _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS{ | |
| callback: compileCallback(*efaceOf(&fn), true), | |
| } | |
| handle := uintptr(0) | |
| stdcall(powerRegisterSuspendResumeNotification, _DEVICE_NOTIFY_CALLBACK, | |
| uintptr(unsafe.Pointer(¶ms)), uintptr(unsafe.Pointer(&handle))) | |
| } | |
| func getCPUCount() int32 { | |
| var mask, sysmask uintptr | |
| ret := stdcall(_GetProcessAffinityMask, windows.CurrentProcess, uintptr(unsafe.Pointer(&mask)), uintptr(unsafe.Pointer(&sysmask))) | |
| if ret != 0 { | |
| n := 0 | |
| maskbits := int(unsafe.Sizeof(mask) * 8) | |
| for i := 0; i < maskbits; i++ { | |
| if mask&(1<<uint(i)) != 0 { | |
| n++ | |
| } | |
| } | |
| if n != 0 { | |
| return int32(n) | |
| } | |
| } | |
| // use GetSystemInfo if GetProcessAffinityMask fails | |
| var info windows.SystemInfo | |
| stdcall(_GetSystemInfo, uintptr(unsafe.Pointer(&info))) | |
| return int32(info.NumberOfProcessors) | |
| } | |
| func getPageSize() uintptr { | |
| var info windows.SystemInfo | |
| stdcall(_GetSystemInfo, uintptr(unsafe.Pointer(&info))) | |
| return uintptr(info.PageSize) | |
| } | |
| // in sys_windows_386.s and sys_windows_amd64.s: | |
| func getlasterror() uint32 | |
| var timeBeginPeriodRetValue uint32 | |
| // osRelaxMinNS indicates that sysmon shouldn't osRelax if the next | |
| // timer is less than 60 ms from now. Since osRelaxing may reduce | |
| // timer resolution to 15.6 ms, this keeps timer error under roughly 1 | |
| // part in 4. | |
| const osRelaxMinNS = 60 * 1e6 | |
| // osRelax is called by the scheduler when transitioning to and from | |
| // all Ps being idle. | |
| // | |
| // Some versions of Windows have high resolution timer. For those | |
| // versions osRelax is noop. | |
| // For Windows versions without high resolution timer, osRelax | |
| // adjusts the system-wide timer resolution. Go needs a | |
| // high resolution timer while running and there's little extra cost | |
| // if we're already using the CPU, but if all Ps are idle there's no | |
| // need to consume extra power to drive the high-res timer. | |
| func osRelax(relax bool) uint32 { | |
| if haveHighResTimer { | |
| // If the high resolution timer is available, the runtime uses the timer | |
| // to sleep for short durations. This means there's no need to adjust | |
| // the global clock frequency. | |
| return 0 | |
| } | |
| if relax { | |
| return uint32(stdcall(_timeEndPeriod, 1)) | |
| } else { | |
| return uint32(stdcall(_timeBeginPeriod, 1)) | |
| } | |
| } | |
| // haveHighResTimer indicates that the CreateWaitableTimerEx | |
| // CREATE_WAITABLE_TIMER_HIGH_RESOLUTION flag is available. | |
| var haveHighResTimer = false | |
| // haveHighResSleep indicates that NtCreateWaitCompletionPacket | |
| // exists and haveHighResTimer is true. | |
| // NtCreateWaitCompletionPacket has been available since Windows 10, | |
| // but has just been publicly documented, so some platforms, like Wine, | |
| // doesn't support it yet. | |
| var haveHighResSleep = false | |
| // createHighResTimer calls CreateWaitableTimerEx with | |
| // CREATE_WAITABLE_TIMER_HIGH_RESOLUTION flag to create high | |
| // resolution timer. createHighResTimer returns new timer | |
| // handle or 0, if CreateWaitableTimerEx failed. | |
| func createHighResTimer() uintptr { | |
| // As per @jstarks, see | |
| // https://github.com/golang/go/issues/8687#issuecomment-656259353 | |
| return stdcall(_CreateWaitableTimerExW, 0, 0, | |
| windows.CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, | |
| windows.SYNCHRONIZE|windows.TIMER_QUERY_STATE|windows.TIMER_MODIFY_STATE) | |
| } | |
| func initHighResTimer() { | |
| h := createHighResTimer() | |
| if h != 0 { | |
| haveHighResTimer = true | |
| haveHighResSleep = _NtCreateWaitCompletionPacket != nil | |
| stdcall(_CloseHandle, h) | |
| } else { | |
| // Only load winmm.dll if we need it. | |
| // This avoids a dependency on winmm.dll for Go programs | |
| // that run on new Windows versions. | |
| m32 := windowsLoadSystemLib(winmmdll[:]) | |
| if m32 == 0 { | |
| print("runtime: LoadLibraryExW failed; errno=", getlasterror(), "\n") | |
| throw("winmm.dll not found") | |
| } | |
| _timeBeginPeriod = windowsFindfunc(m32, []byte("timeBeginPeriod\000")) | |
| _timeEndPeriod = windowsFindfunc(m32, []byte("timeEndPeriod\000")) | |
| if _timeBeginPeriod == nil || _timeEndPeriod == nil { | |
| print("runtime: GetProcAddress failed; errno=", getlasterror(), "\n") | |
| throw("timeBegin/EndPeriod not found") | |
| } | |
| } | |
| } | |
| //go:linkname canUseLongPaths internal/syscall/windows.CanUseLongPaths | |
| var canUseLongPaths bool | |
| // initLongPathSupport enables long path support. | |
| func initLongPathSupport() { | |
| const ( | |
| IsLongPathAwareProcess = 0x80 | |
| PebBitFieldOffset = 3 | |
| ) | |
| // Check that we're ≥ 10.0.15063. | |
| info := windows.OSVERSIONINFOW{} | |
| info.OSVersionInfoSize = uint32(unsafe.Sizeof(info)) | |
| stdcall(_RtlGetVersion, uintptr(unsafe.Pointer(&info))) | |
| if info.MajorVersion < 10 || (info.MajorVersion == 10 && info.MinorVersion == 0 && info.BuildNumber < 15063) { | |
| return | |
| } | |
| // Set the IsLongPathAwareProcess flag of the PEB's bit field. | |
| // This flag is not documented, but it's known to be used | |
| // by Windows to enable long path support. | |
| bitField := (*byte)(unsafe.Pointer(stdcall(_RtlGetCurrentPeb) + PebBitFieldOffset)) | |
| *bitField |= IsLongPathAwareProcess | |
| canUseLongPaths = true | |
| } | |
| func osinit() { | |
| asmstdcallAddr = unsafe.Pointer(windows.AsmStdCallAddr()) | |
| loadOptionalSyscalls() | |
| preventErrorDialogs() | |
| initExceptionHandler() | |
| initHighResTimer() | |
| timeBeginPeriodRetValue = osRelax(false) | |
| initSysDirectory() | |
| initLongPathSupport() | |
| numCPUStartup = getCPUCount() | |
| physPageSize = getPageSize() | |
| // Windows dynamic priority boosting assumes that a process has different types | |
| // of dedicated threads -- GUI, IO, computational, etc. Go processes use | |
| // equivalent threads that all do a mix of GUI, IO, computations, etc. | |
| // In such context dynamic priority boosting does nothing but harm, so we turn it off. | |
| stdcall(_SetProcessPriorityBoost, windows.CurrentProcess, 1) | |
| } | |
| //go:nosplit | |
| func readRandom(r []byte) int { | |
| n := 0 | |
| if stdcall(_ProcessPrng, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 { | |
| n = len(r) | |
| } | |
| return n | |
| } | |
| func goenvs() { | |
| // strings is a pointer to environment variable pairs in the form: | |
| // "envA=valA\x00envB=valB\x00\x00" (in UTF-16) | |
| // Two consecutive zero bytes end the list. | |
| strings := unsafe.Pointer(stdcall(_GetEnvironmentStringsW)) | |
| p := (*[1 << 24]uint16)(strings)[:] | |
| n := 0 | |
| for from, i := 0, 0; true; i++ { | |
| if p[i] == 0 { | |
| // empty string marks the end | |
| if i == from { | |
| break | |
| } | |
| from = i + 1 | |
| n++ | |
| } | |
| } | |
| envs = make([]string, n) | |
| for i := range envs { | |
| envs[i] = gostringw(&p[0]) | |
| for p[0] != 0 { | |
| p = p[1:] | |
| } | |
| p = p[1:] // skip nil byte | |
| } | |
| stdcall(_FreeEnvironmentStringsW, uintptr(strings)) | |
| // We call these all the way here, late in init, so that malloc works | |
| // for the callback functions these generate. | |
| var fn any = ctrlHandler | |
| ctrlHandlerPC := compileCallback(*efaceOf(&fn), true) | |
| stdcall(_SetConsoleCtrlHandler, ctrlHandlerPC, 1) | |
| monitorSuspendResume() | |
| } | |
| // exiting is set to non-zero when the process is exiting. | |
| var exiting uint32 | |
| //go:nosplit | |
| func exit(code int32) { | |
| // Disallow thread suspension for preemption. Otherwise, | |
| // ExitProcess and SuspendThread can race: SuspendThread | |
| // queues a suspension request for this thread, ExitProcess | |
| // kills the suspending thread, and then this thread suspends. | |
| lock(&suspendLock) | |
| atomic.Store(&exiting, 1) | |
| stdcall(_ExitProcess, uintptr(code)) | |
| } | |
| // write1 must be nosplit because it's used as a last resort in | |
| // functions like badmorestackg0. In such cases, we'll always take the | |
| // ASCII path. | |
| // | |
| //go:nosplit | |
| func write1(fd uintptr, buf unsafe.Pointer, n int32) int32 { | |
| const ( | |
| _STD_OUTPUT_HANDLE = ^uintptr(10) // -11 | |
| _STD_ERROR_HANDLE = ^uintptr(11) // -12 | |
| ) | |
| var handle uintptr | |
| switch fd { | |
| case 1: | |
| handle = stdcall(_GetStdHandle, _STD_OUTPUT_HANDLE) | |
| case 2: | |
| handle = stdcall(_GetStdHandle, _STD_ERROR_HANDLE) | |
| default: | |
| // assume fd is real windows handle. | |
| handle = fd | |
| } | |
| isASCII := true | |
| b := (*[1 << 30]byte)(buf)[:n] | |
| for _, x := range b { | |
| if x >= 0x80 { | |
| isASCII = false | |
| break | |
| } | |
| } | |
| if !isASCII { | |
| var m uint32 | |
| isConsole := stdcall(_GetConsoleMode, handle, uintptr(unsafe.Pointer(&m))) != 0 | |
| // If this is a console output, various non-unicode code pages can be in use. | |
| // Use the dedicated WriteConsole call to ensure unicode is printed correctly. | |
| if isConsole { | |
| return int32(writeConsole(handle, buf, n)) | |
| } | |
| } | |
| var written uint32 | |
| stdcall(_WriteFile, handle, uintptr(buf), uintptr(n), uintptr(unsafe.Pointer(&written)), 0) | |
| return int32(written) | |
| } | |
| var ( | |
| utf16ConsoleBack [1000]uint16 | |
| utf16ConsoleBackLock mutex | |
| ) | |
| // writeConsole writes bufLen bytes from buf to the console File. | |
| // It returns the number of bytes written. | |
| func writeConsole(handle uintptr, buf unsafe.Pointer, bufLen int32) int { | |
| const surr2 = (surrogateMin + surrogateMax + 1) / 2 | |
| // Do not use defer for unlock. May cause issues when printing a panic. | |
| lock(&utf16ConsoleBackLock) | |
| b := (*[1 << 30]byte)(buf)[:bufLen] | |
| s := *(*string)(unsafe.Pointer(&b)) | |
| utf16tmp := utf16ConsoleBack[:] | |
| total := len(s) | |
| w := 0 | |
| for _, r := range s { | |
| if w >= len(utf16tmp)-2 { | |
| writeConsoleUTF16(handle, utf16tmp[:w]) | |
| w = 0 | |
| } | |
| if r < 0x10000 { | |
| utf16tmp[w] = uint16(r) | |
| w++ | |
| } else { | |
| r -= 0x10000 | |
| utf16tmp[w] = surrogateMin + uint16(r>>10)&0x3ff | |
| utf16tmp[w+1] = surr2 + uint16(r)&0x3ff | |
| w += 2 | |
| } | |
| } | |
| writeConsoleUTF16(handle, utf16tmp[:w]) | |
| unlock(&utf16ConsoleBackLock) | |
| return total | |
| } | |
| // writeConsoleUTF16 is the dedicated windows calls that correctly prints | |
| // to the console regardless of the current code page. Input is utf-16 code points. | |
| // The handle must be a console handle. | |
| func writeConsoleUTF16(handle uintptr, b []uint16) { | |
| l := uint32(len(b)) | |
| if l == 0 { | |
| return | |
| } | |
| var written uint32 | |
| stdcall(_WriteConsoleW, | |
| handle, | |
| uintptr(unsafe.Pointer(&b[0])), | |
| uintptr(l), | |
| uintptr(unsafe.Pointer(&written)), | |
| 0, | |
| ) | |
| return | |
| } | |
| //go:nosplit | |
| func semasleep(ns int64) int32 { | |
| const ( | |
| _WAIT_ABANDONED = 0x00000080 | |
| _WAIT_OBJECT_0 = 0x00000000 | |
| _WAIT_TIMEOUT = 0x00000102 | |
| _WAIT_FAILED = 0xFFFFFFFF | |
| ) | |
| var result uintptr | |
| if ns < 0 { | |
| result = stdcall(_WaitForSingleObject, getg().m.waitsema, uintptr(windows.INFINITE)) | |
| } else { | |
| start := nanotime() | |
| elapsed := int64(0) | |
| for { | |
| ms := (ns - elapsed) / 1000000 | |
| if ms == 0 { | |
| ms = 1 | |
| } | |
| result = stdcall(_WaitForMultipleObjects, 2, | |
| uintptr(unsafe.Pointer(&[2]uintptr{getg().m.waitsema, getg().m.resumesema})), | |
| 0, uintptr(ms)) | |
| if result != _WAIT_OBJECT_0+1 { | |
| // Not a suspend/resume event | |
| break | |
| } | |
| elapsed = nanotime() - start | |
| if elapsed >= ns { | |
| return -1 | |
| } | |
| } | |
| } | |
| switch result { | |
| case _WAIT_OBJECT_0: // Signaled | |
| return 0 | |
| case _WAIT_TIMEOUT: | |
| return -1 | |
| case _WAIT_ABANDONED: | |
| systemstack(func() { | |
| throw("runtime.semasleep wait_abandoned") | |
| }) | |
| case _WAIT_FAILED: | |
| systemstack(func() { | |
| print("runtime: waitforsingleobject wait_failed; errno=", getlasterror(), "\n") | |
| throw("runtime.semasleep wait_failed") | |
| }) | |
| default: | |
| systemstack(func() { | |
| print("runtime: waitforsingleobject unexpected; result=", result, "\n") | |
| throw("runtime.semasleep unexpected") | |
| }) | |
| } | |
| return -1 // unreachable | |
| } | |
| //go:nosplit | |
| func semawakeup(mp *m) { | |
| if stdcall(_SetEvent, mp.waitsema) == 0 { | |
| systemstack(func() { | |
| print("runtime: setevent failed; errno=", getlasterror(), "\n") | |
| throw("runtime.semawakeup") | |
| }) | |
| } | |
| } | |
| //go:nosplit | |
| func semacreate(mp *m) { | |
| if mp.waitsema != 0 { | |
| return | |
| } | |
| mp.waitsema = stdcall(_CreateEventA, 0, 0, 0, 0) | |
| if mp.waitsema == 0 { | |
| systemstack(func() { | |
| print("runtime: createevent failed; errno=", getlasterror(), "\n") | |
| throw("runtime.semacreate") | |
| }) | |
| } | |
| mp.resumesema = stdcall(_CreateEventA, 0, 0, 0, 0) | |
| if mp.resumesema == 0 { | |
| systemstack(func() { | |
| print("runtime: createevent failed; errno=", getlasterror(), "\n") | |
| throw("runtime.semacreate") | |
| }) | |
| stdcall(_CloseHandle, mp.waitsema) | |
| mp.waitsema = 0 | |
| } | |
| } | |
| // May run with m.p==nil, so write barriers are not allowed. | |
| // | |
| //go:nowritebarrierrec | |
| func newosproc(mp *m) { | |
| // LockOSThread can reach newosproc on a goroutine stack when starting | |
| // the template thread. createThread does not switch stacks itself. | |
| var thandle uintptr | |
| var err uint32 | |
| systemstack(func() { | |
| thandle, err = createThread(0, unsafe.Pointer(abi.FuncPCABI0(tstart_stdcall)), unsafe.Pointer(mp)) | |
| }) | |
| if thandle == 0 { | |
| if atomic.Load(&exiting) != 0 { | |
| // CreateThread may fail if called | |
| // concurrently with ExitProcess. If this | |
| // happens, just freeze this thread and let | |
| // the process exit. See issue #18253. | |
| lock(&deadlock) | |
| lock(&deadlock) | |
| } | |
| print("runtime: failed to create new OS thread (have ", mcount(), " already; errno=", err, ")\n") | |
| throw("runtime.newosproc") | |
| } | |
| // Close thandle to avoid leaking the thread object if it exits. | |
| stdcall(_CloseHandle, thandle) | |
| } | |
| // poll_runtime_startThread starts fn on a new OS thread, locked for the entire | |
| // lifetime of fn. The thread never runs another goroutine and exits when fn | |
| // returns. fn must not call UnlockOSThread. | |
| // | |
| // Unlike a Windows callback, this can run before package initialization has | |
| // finished. Unlike go followed by LockOSThread, it cannot acquire an existing | |
| // thread with outstanding I/O from an earlier goroutine. | |
| // | |
| //go:linkname poll_runtime_startThread internal/poll.runtime_startThread | |
| func poll_runtime_startThread(fn func()) { | |
| // A locked worker may need the template thread to replace its M when it | |
| // parks. Set this up before publishing a worker, as LockOSThread does. | |
| if atomic.Load(&newmHandoff.haveTemplateThread) == 0 { | |
| startTemplateThread() | |
| } | |
| gp := getg() | |
| pc := sys.GetCallerPC() | |
| systemstack(func() { | |
| mp := acquirem() | |
| worker := newproc1(*(**funcval)(unsafe.Pointer(&fn)), gp, pc, false, waitReasonZero) | |
| // A shared worker must not retain its creator's profiling labels. | |
| worker.labels = nil | |
| thread := allocm(nil, pollStartThread, -1) | |
| thread.syncIOWorker = true | |
| thread.sigmask = initSigmask | |
| thread.lockedExt = 1 | |
| thread.lockedg.set(worker) | |
| worker.lockedm.set(thread) | |
| newm1(thread) | |
| releasem(mp) | |
| }) | |
| } | |
| // poll_runtime_threadHandle lends the current worker's thread handle to poll. | |
| // minit initialized it before the worker started. The worker is permanently | |
| // locked to its thread, so unminit cannot close the handle until it returns. | |
| // | |
| //go:linkname poll_runtime_threadHandle internal/poll.runtime_threadHandle | |
| //go:nosplit | |
| func poll_runtime_threadHandle() uintptr { | |
| gp := getg() | |
| if !gp.m.syncIOWorker || gp.lockedm.ptr() != gp.m { | |
| throw("runtime: not a synchronous I/O worker") | |
| } | |
| return gp.m.thread | |
| } | |
| // pollStartThread starts without a P. Publish the locked goroutine and wait | |
| // for the scheduler to hand a P to this thread, as in stoplockedm. | |
| func pollStartThread() { | |
| mp := getg().m | |
| lock(&sched.lock) | |
| // No checkdead is needed: this balances the increase in mcount from | |
| // allocating this M, so there is no net decrease in running Ms. | |
| sched.nmidlelocked++ | |
| globrunqput(mp.lockedg.ptr()) | |
| startm(nil, false, true) | |
| unlock(&sched.lock) | |
| mPark() | |
| acquirep(mp.nextp.ptr()) | |
| mp.nextp = 0 | |
| execute(mp.lockedg.ptr(), false) | |
| } | |
| // Used by the C library build mode. On Linux this function would allocate a | |
| // stack, but that's not necessary for Windows. No stack guards are present | |
| // and the GC has not been initialized, so write barriers will fail. | |
| // | |
| //go:nowritebarrierrec | |
| //go:nosplit | |
| func newosproc0(stacksize uintptr, fn unsafe.Pointer) { | |
| thandle, err := createThread(stacksize, fn, nil) | |
| if thandle == 0 { | |
| print("runtime: failed to create new OS thread (errno=", err, ")\n") | |
| throw("runtime: failed to create new OS thread\n") | |
| } | |
| stdcall_no_g(_CloseHandle, thandle) | |
| } | |
| // createThread calls CreateThread to create a new thread. | |
| // | |
| //go:nowritebarrierrec | |
| //go:nosplit | |
| func createThread(stackSize uintptr, fn unsafe.Pointer, arg unsafe.Pointer) (handle uintptr, err uint32) { | |
| for tries := range 20 { | |
| // We pass 0 for the stack size to use the default for this binary. | |
| handle = stdcall_no_g(_CreateThread, 0, stackSize, uintptr(fn), uintptr(arg), 0, 0) | |
| if handle == 0 { | |
| err = getlasterror() | |
| } | |
| if handle == 0 && err == windows.ERROR_ACCESS_DENIED { | |
| // "Insufficient resources". Yield, then back off a bit before retrying. | |
| usleep_no_g(uint32(tries) * 1000) | |
| continue | |
| } | |
| break | |
| } | |
| return handle, err | |
| } | |
| //go:nosplit | |
| //go:nowritebarrierrec | |
| func libpreinit() {} | |
| func exitThread(wait *atomic.Uint32) { | |
| // We should never reach exitThread on Windows because we let | |
| // the OS clean up threads. | |
| throw("exitThread") | |
| } | |
| // Called to initialize a new m (including the bootstrap m). | |
| // Called on the parent thread (main thread in case of bootstrap), can allocate memory. | |
| func mpreinit(mp *m) { | |
| } | |
| //go:nosplit | |
| func sigsave(p *sigset) { | |
| } | |
| //go:nosplit | |
| func msigrestore(sigmask sigset) { | |
| } | |
| //go:nosplit | |
| //go:nowritebarrierrec | |
| func clearSignalHandlers() { | |
| } | |
| //go:nosplit | |
| func sigblock(exiting bool) { | |
| } | |
| // Called to initialize a new m (including the bootstrap m). | |
| // Called on the new thread, cannot allocate Go memory. | |
| func minit() { | |
| var thandle uintptr | |
| if stdcall(_DuplicateHandle, windows.CurrentProcess, windows.CurrentThread, windows.CurrentProcess, uintptr(unsafe.Pointer(&thandle)), 0, 0, windows.DUPLICATE_SAME_ACCESS) == 0 { | |
| print("runtime.minit: duplicatehandle failed; errno=", getlasterror(), "\n") | |
| throw("runtime.minit: duplicatehandle failed") | |
| } | |
| mp := getg().m | |
| lock(&mp.threadLock) | |
| mp.thread = thandle | |
| mp.procid = uint64(stdcall(_GetCurrentThreadId)) | |
| // Configure usleep timer, if possible. | |
| if mp.highResTimer == 0 && haveHighResTimer { | |
| mp.highResTimer = createHighResTimer() | |
| if mp.highResTimer == 0 { | |
| print("runtime: CreateWaitableTimerEx failed; errno=", getlasterror(), "\n") | |
| throw("CreateWaitableTimerEx when creating timer failed") | |
| } | |
| } | |
| if mp.waitIocpHandle == 0 && haveHighResSleep { | |
| mp.waitIocpTimer = createHighResTimer() | |
| if mp.waitIocpTimer == 0 { | |
| print("runtime: CreateWaitableTimerEx failed; errno=", getlasterror(), "\n") | |
| throw("CreateWaitableTimerEx when creating timer failed") | |
| } | |
| const GENERIC_ALL = 0x10000000 | |
| errno := stdcall(_NtCreateWaitCompletionPacket, uintptr(unsafe.Pointer(&mp.waitIocpHandle)), GENERIC_ALL, 0) | |
| if mp.waitIocpHandle == 0 { | |
| print("runtime: NtCreateWaitCompletionPacket failed; errno=", errno, "\n") | |
| throw("NtCreateWaitCompletionPacket failed") | |
| } | |
| } | |
| unlock(&mp.threadLock) | |
| // Query the true stack base from the OS. Currently we're | |
| // running on a small assumed stack. | |
| var mbi windows.MemoryBasicInformation | |
| res := stdcall(_VirtualQuery, uintptr(unsafe.Pointer(&mbi)), uintptr(unsafe.Pointer(&mbi)), unsafe.Sizeof(mbi)) | |
| if res == 0 { | |
| print("runtime: VirtualQuery failed; errno=", getlasterror(), "\n") | |
| throw("VirtualQuery for stack base failed") | |
| } | |
| // The system leaves an 8K PAGE_GUARD region at the bottom of | |
| // the stack (in theory VirtualQuery isn't supposed to include | |
| // that, but it does). Add an additional 8K of slop for | |
| // calling C functions that don't have stack checks and for | |
| // lastcontinuehandler. We shouldn't be anywhere near this | |
| // bound anyway. | |
| base := mbi.AllocationBase + 16<<10 | |
| // Sanity check the stack bounds. | |
| g0 := getg() | |
| if base > g0.stack.hi || g0.stack.hi-base > 64<<20 { | |
| print("runtime: g0 stack [", hex(base), ",", hex(g0.stack.hi), ")\n") | |
| throw("bad g0 stack") | |
| } | |
| g0.stack.lo = base | |
| g0.stackguard0 = g0.stack.lo + stackGuard | |
| g0.stackguard1 = g0.stackguard0 | |
| // Sanity check the SP. | |
| stackcheck() | |
| } | |
| // Called from dropm to undo the effect of an minit. | |
| // | |
| //go:nosplit | |
| func unminit() { | |
| mp := getg().m | |
| lock(&mp.threadLock) | |
| if mp.thread != 0 { | |
| stdcall(_CloseHandle, mp.thread) | |
| mp.thread = 0 | |
| } | |
| unlock(&mp.threadLock) | |
| mp.procid = 0 | |
| } | |
| // Called from mexit, but not from dropm, to undo the effect of thread-owned | |
| // resources in minit, semacreate, or elsewhere. Do not take locks after calling this. | |
| // | |
| // This always runs without a P, so //go:nowritebarrierrec is required. | |
| // | |
| //go:nowritebarrierrec | |
| //go:nosplit | |
| func mdestroy(mp *m) { | |
| if mp.highResTimer != 0 { | |
| stdcall(_CloseHandle, mp.highResTimer) | |
| mp.highResTimer = 0 | |
| } | |
| if mp.waitIocpTimer != 0 { | |
| stdcall(_CloseHandle, mp.waitIocpTimer) | |
| mp.waitIocpTimer = 0 | |
| } | |
| if mp.waitIocpHandle != 0 { | |
| stdcall(_CloseHandle, mp.waitIocpHandle) | |
| mp.waitIocpHandle = 0 | |
| } | |
| if mp.waitsema != 0 { | |
| stdcall(_CloseHandle, mp.waitsema) | |
| mp.waitsema = 0 | |
| } | |
| if mp.resumesema != 0 { | |
| stdcall(_CloseHandle, mp.resumesema) | |
| mp.resumesema = 0 | |
| } | |
| } | |
| // stdcall_no_g is like [stdcall] but can be called without a G. | |
| // | |
| //go:nowritebarrier | |
| //go:nosplit | |
| //go:uintptrkeepalive | |
| func stdcall_no_g(fn stdFunction, args ...uintptr) uintptr { | |
| call := windows.StdCallInfo{ | |
| Fn: uintptr(unsafe.Pointer(fn)), | |
| N: uintptr(len(args)), | |
| } | |
| if len(args) > 0 { | |
| call.Args = uintptr(abi.NoEscape(unsafe.Pointer(&args[0]))) | |
| } | |
| windows.StdCall(&call) | |
| return call.R1 | |
| } | |
| // stdcall calls fn with the given arguments using the stdcall calling convention. | |
| // Must be called from the system stack. | |
| // May run during STW, so write barriers are not allowed. | |
| // | |
| //go:nowritebarrier | |
| //go:nosplit | |
| //go:uintptrkeepalive | |
| func stdcall(fn stdFunction, args ...uintptr) uintptr { | |
| gp := getg() | |
| mp := gp.m | |
| mp.stdCallInfo.Fn = uintptr(unsafe.Pointer(fn)) | |
| mp.stdCallInfo.N = uintptr(len(args)) | |
| if len(args) > 0 { | |
| mp.stdCallInfo.Args = uintptr(abi.NoEscape(unsafe.Pointer(&args[0]))) | |
| } | |
| resetLibcall := false | |
| if mp.profilehz != 0 && mp.libcallsp == 0 { | |
| // leave pc/sp for cpu profiler | |
| mp.libcallg.set(gp) | |
| mp.libcallpc = sys.GetCallerPC() | |
| // sp must be the last, because once async cpu profiler finds | |
| // all three values to be non-zero, it will use them | |
| mp.libcallsp = sys.GetCallerSP() | |
| resetLibcall = true // See comment in sys_darwin.go:libcCall | |
| } | |
| asmcgocall(asmstdcallAddr, unsafe.Pointer(&mp.stdCallInfo)) | |
| if resetLibcall { | |
| mp.libcallsp = 0 | |
| } | |
| return mp.stdCallInfo.R1 | |
| } | |
| // These must run on the system stack only. | |
| //go:nosplit | |
| func osyield_no_g() { | |
| stdcall_no_g(_SwitchToThread) | |
| } | |
| //go:nosplit | |
| func osyield() { | |
| systemstack(func() { | |
| stdcall(_SwitchToThread) | |
| }) | |
| } | |
| //go:nosplit | |
| func usleep_no_g(us uint32) { | |
| timeout := uintptr(us) / 1000 // ms units | |
| stdcall_no_g(_WaitForSingleObject, windows.INVALID_HANDLE_VALUE, timeout) | |
| } | |
| //go:nosplit | |
| func usleep(us uint32) { | |
| systemstack(func() { | |
| var h, timeout uintptr | |
| // If the high-res timer is available and its handle has been allocated for this m, use it. | |
| // Otherwise fall back to the low-res one, which doesn't need a handle. | |
| if haveHighResTimer && getg().m.highResTimer != 0 { | |
| h = getg().m.highResTimer | |
| dt := -10 * int64(us) // relative sleep (negative), 100ns units | |
| stdcall(_SetWaitableTimer, h, uintptr(unsafe.Pointer(&dt)), 0, 0, 0, 0) | |
| timeout = windows.INFINITE | |
| } else { | |
| h = windows.INVALID_HANDLE_VALUE | |
| timeout = uintptr(us) / 1000 // ms units | |
| } | |
| stdcall(_WaitForSingleObject, h, timeout) | |
| }) | |
| } | |
| func ctrlHandler(_type uint32) uintptr { | |
| var s uint32 | |
| switch _type { | |
| case windows.CTRL_C_EVENT, windows.CTRL_BREAK_EVENT: | |
| s = windows.SIGINT | |
| case windows.CTRL_CLOSE_EVENT, windows.CTRL_LOGOFF_EVENT, windows.CTRL_SHUTDOWN_EVENT: | |
| s = windows.SIGTERM | |
| default: | |
| return 0 | |
| } | |
| if sigsend(s) { | |
| if s == windows.SIGTERM { | |
| // Windows terminates the process after this handler returns. | |
| // Block indefinitely to give signal handlers a chance to clean up, | |
| // but make sure to be properly parked first, so the rest of the | |
| // program can continue executing. | |
| block() | |
| } | |
| return 1 | |
| } | |
| return 0 | |
| } | |
| // called from zcallback_windows_*.s to sys_windows_*.s | |
| func callbackasm1() | |
| var profiletimer uintptr | |
| func profilem(mp *m, thread uintptr) { | |
| // Align Context to 16 bytes. | |
| var c *windows.Context | |
| var cbuf [unsafe.Sizeof(*c) + 15]byte | |
| c = (*windows.Context)(unsafe.Pointer((uintptr(unsafe.Pointer(&cbuf[15]))) &^ 15)) | |
| c.ContextFlags = windows.CONTEXT_CONTROL | |
| stdcall(_GetThreadContext, thread, uintptr(unsafe.Pointer(c))) | |
| gp := gFromSP(mp, c.SP()) | |
| sigprof(c.PC(), c.SP(), c.LR(), gp, mp) | |
| } | |
| func gFromSP(mp *m, sp uintptr) *g { | |
| if gp := mp.g0; gp != nil && gp.stack.lo < sp && sp < gp.stack.hi { | |
| return gp | |
| } | |
| if gp := mp.gsignal; gp != nil && gp.stack.lo < sp && sp < gp.stack.hi { | |
| return gp | |
| } | |
| if gp := mp.curg; gp != nil && gp.stack.lo < sp && sp < gp.stack.hi { | |
| return gp | |
| } | |
| return nil | |
| } | |
| func profileLoop() { | |
| stdcall(_SetThreadPriority, windows.CurrentThread, windows.THREAD_PRIORITY_HIGHEST) | |
| for { | |
| stdcall(_WaitForSingleObject, profiletimer, windows.INFINITE) | |
| first := (*m)(atomic.Loadp(unsafe.Pointer(&allm))) | |
| for mp := first; mp != nil; mp = mp.alllink { | |
| if mp == getg().m { | |
| // Don't profile ourselves. | |
| continue | |
| } | |
| lock(&mp.threadLock) | |
| // Do not profile threads blocked on Notes, | |
| // this includes idle worker threads, | |
| // idle timer thread, idle heap scavenger, etc. | |
| if mp.thread == 0 || mp.profilehz == 0 || mp.blocked { | |
| unlock(&mp.threadLock) | |
| continue | |
| } | |
| // Acquire our own handle to the thread. | |
| var thread uintptr | |
| if stdcall(_DuplicateHandle, windows.CurrentProcess, mp.thread, windows.CurrentProcess, uintptr(unsafe.Pointer(&thread)), 0, 0, windows.DUPLICATE_SAME_ACCESS) == 0 { | |
| print("runtime: duplicatehandle failed; errno=", getlasterror(), "\n") | |
| throw("duplicatehandle failed") | |
| } | |
| unlock(&mp.threadLock) | |
| // mp may exit between the DuplicateHandle | |
| // above and the SuspendThread. The handle | |
| // will remain valid, but SuspendThread may | |
| // fail. | |
| if int32(stdcall(_SuspendThread, thread)) == -1 { | |
| // The thread no longer exists. | |
| stdcall(_CloseHandle, thread) | |
| continue | |
| } | |
| if mp.profilehz != 0 && !mp.blocked { | |
| // Pass the thread handle in case mp | |
| // was in the process of shutting down. | |
| profilem(mp, thread) | |
| } | |
| stdcall(_ResumeThread, thread) | |
| stdcall(_CloseHandle, thread) | |
| } | |
| } | |
| } | |
| func setProcessCPUProfiler(hz int32) { | |
| if profiletimer == 0 { | |
| var timer uintptr | |
| if haveHighResTimer { | |
| timer = createHighResTimer() | |
| } else { | |
| timer = stdcall(_CreateWaitableTimerA, 0, 0, 0) | |
| } | |
| atomic.Storeuintptr(&profiletimer, timer) | |
| newm(profileLoop, nil, -1) | |
| } | |
| } | |
| func setThreadCPUProfiler(hz int32) { | |
| ms := int32(0) | |
| due := ^int64(^uint64(1 << 63)) | |
| if hz > 0 { | |
| ms = 1000 / hz | |
| if ms == 0 { | |
| ms = 1 | |
| } | |
| due = int64(ms) * -10000 | |
| } | |
| stdcall(_SetWaitableTimer, profiletimer, uintptr(unsafe.Pointer(&due)), uintptr(ms), 0, 0, 0) | |
| atomic.Store((*uint32)(unsafe.Pointer(&getg().m.profilehz)), uint32(hz)) | |
| } | |
| const preemptMSupported = true | |
| // suspendLock protects simultaneous SuspendThread operations from | |
| // suspending each other. | |
| var suspendLock mutex | |
| // threadPausedLockRank warns the runtime against acquiring new locks on this | |
| // thread while it has another thread paused. The holder of this lock rank is | |
| // effectively a signal handler for a paused thread, which may be holding | |
| // arbitrary locks. | |
| // | |
| // The warning takes two concrete forms: First, unlock2 will see that this | |
| // thread is holding other locks, so won't try to flush the mutex contention | |
| // buffer (which can involve acquiring another lock). Second, builds with | |
| // GOEXPERIMENT=staticlockranking (currently broken, https://go.dev/issue/76058) | |
| // will report a rank violation. | |
| // | |
| // suspendLock serializes the transition into "suspended", but it's fine for | |
| // multiple threads to be in the suspended state concurrently, with each of | |
| // their sponsors independently using threadPausedLockRank to discourage | |
| // themselves from acquiring new locks. | |
| const threadPausedLockRank = lockRankLeafRank | |
| // preemptM requests preemption of the M running gp. | |
| // gp.preempt must be set, and gp must be in _Gscanrunning, with its _Gscan | |
| // bit held by the caller. | |
| // preemptM releases the _Gscan bit before returning. | |
| func preemptM(gp *g) { | |
| mp := gp.m | |
| if mp == getg().m { | |
| throw("self-preempt") | |
| } | |
| // Keep gp in _Gscanrunning so it can't enter _Gsyscall and external | |
| // code before SuspendThread takes effect. External code may call | |
| // ExitProcess, which can otherwise kill this thread before the target | |
| // is suspended. | |
| // Acquire our own handle to mp's thread. | |
| lock(&mp.threadLock) | |
| if mp.thread == 0 { | |
| // The M hasn't been minit'd yet (or was just unminit'd). | |
| unlock(&mp.threadLock) | |
| casfrom_Gscanstatus(gp, _Gscanrunning, _Grunning) | |
| mp.preemptGen.Add(1) | |
| return | |
| } | |
| var thread uintptr | |
| if stdcall(_DuplicateHandle, windows.CurrentProcess, mp.thread, windows.CurrentProcess, uintptr(unsafe.Pointer(&thread)), 0, 0, windows.DUPLICATE_SAME_ACCESS) == 0 { | |
| print("runtime.preemptM: duplicatehandle failed; errno=", getlasterror(), "\n") | |
| throw("runtime.preemptM: duplicatehandle failed") | |
| } | |
| unlock(&mp.threadLock) | |
| // Prepare thread context buffer. This must be aligned to 16 bytes. | |
| var c *windows.Context | |
| var cbuf [unsafe.Sizeof(*c) + 15]byte | |
| c = (*windows.Context)(unsafe.Pointer((uintptr(unsafe.Pointer(&cbuf[15]))) &^ 15)) | |
| // CONTEXT_EXCEPTION_REQUEST reports whether the thread is in the kernel. | |
| c.ContextFlags = windows.CONTEXT_CONTROL | windows.CONTEXT_EXCEPTION_REQUEST | |
| // Serialize thread suspension. SuspendThread is asynchronous, | |
| // so it's otherwise possible for two threads to suspend each | |
| // other and deadlock. We must hold this lock until after | |
| // GetThreadContext, since that blocks until the thread is | |
| // actually suspended. | |
| lock(&suspendLock) | |
| // Signal handler rules are in effect; don't acquire new locks. | |
| acquireLockRankAndM(threadPausedLockRank) | |
| // Suspend the thread. | |
| if int32(stdcall(_SuspendThread, thread)) == -1 { | |
| unlock(&suspendLock) | |
| stdcall(_CloseHandle, thread) | |
| // The thread no longer exists. This shouldn't be | |
| // possible, but just acknowledge the request. | |
| mp.preemptGen.Add(1) | |
| releaseLockRankAndM(threadPausedLockRank) | |
| casfrom_Gscanstatus(gp, _Gscanrunning, _Grunning) | |
| return | |
| } | |
| // We have to be very careful between this point and once | |
| // we've shown mp is at an async safe-point. This is like a | |
| // signal handler in the sense that mp could have been doing | |
| // anything when we stopped it, including holding arbitrary | |
| // locks. | |
| // We have to get the thread context before inspecting the M | |
| // because SuspendThread only requests a suspend. | |
| // GetThreadContext actually blocks until it's suspended. | |
| stdcall(_GetThreadContext, thread, uintptr(unsafe.Pointer(c))) | |
| unlock(&suspendLock) | |
| // Don't redirect a thread that is in the kernel handling an exception | |
| // or system call; Windows may not apply the new context correctly | |
| // (#79249). Without CONTEXT_EXCEPTION_REPORTING we can't tell. | |
| safe := c.ContextFlags&windows.CONTEXT_EXCEPTION_REPORTING != 0 && | |
| c.ContextFlags&(windows.CONTEXT_EXCEPTION_ACTIVE|windows.CONTEXT_SERVICE_ACTIVE) == 0 | |
| c.ContextFlags = windows.CONTEXT_CONTROL | |
| // Is it safe to preempt? | |
| if safe && gFromSP(mp, c.SP()) == gp { | |
| if ok, resumePC := isAsyncSafePoint(gp, c.PC(), c.SP(), c.LR()); ok { | |
| // Inject call to asyncPreempt | |
| targetPC := abi.FuncPCABI0(asyncPreempt) | |
| c.PushCall(targetPC, resumePC) | |
| stdcall(_SetThreadContext, thread, uintptr(unsafe.Pointer(c))) | |
| } | |
| } | |
| // Acknowledge the preemption. | |
| mp.preemptGen.Add(1) | |
| stdcall(_ResumeThread, thread) | |
| stdcall(_CloseHandle, thread) | |
| releaseLockRankAndM(threadPausedLockRank) | |
| casfrom_Gscanstatus(gp, _Gscanrunning, _Grunning) | |
| } |