| 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 |
"internal/abi" |
| 9 |
"internal/runtime/atomic" |
| 10 |
"internal/runtime/sys" |
| 11 |
"internal/runtime/syscall/windows" |
| 12 |
"unsafe" |
| 13 |
) |
| 14 |
|
| 15 |
// TODO(brainman): should not need those |
| 16 |
const ( |
| 17 |
_NSIG = 65 |
| 18 |
) |
| 19 |
|
| 20 |
//go:cgo_import_dynamic runtime._AddVectoredContinueHandler AddVectoredContinueHandler%2 "kernel32.dll" |
| 21 |
//go:cgo_import_dynamic runtime._AddVectoredExceptionHandler AddVectoredExceptionHandler%2 "kernel32.dll" |
| 22 |
//go:cgo_import_dynamic runtime._CloseHandle CloseHandle%1 "kernel32.dll" |
| 23 |
//go:cgo_import_dynamic runtime._CreateEventA CreateEventA%4 "kernel32.dll" |
| 24 |
//go:cgo_import_dynamic runtime._CreateIoCompletionPort CreateIoCompletionPort%4 "kernel32.dll" |
| 25 |
//go:cgo_import_dynamic runtime._CreateThread CreateThread%6 "kernel32.dll" |
| 26 |
//go:cgo_import_dynamic runtime._CreateWaitableTimerA CreateWaitableTimerA%3 "kernel32.dll" |
| 27 |
//go:cgo_import_dynamic runtime._CreateWaitableTimerExW CreateWaitableTimerExW%4 "kernel32.dll" |
| 28 |
//go:cgo_import_dynamic runtime._DuplicateHandle DuplicateHandle%7 "kernel32.dll" |
| 29 |
//go:cgo_import_dynamic runtime._ExitProcess ExitProcess%1 "kernel32.dll" |
| 30 |
//go:cgo_import_dynamic runtime._FreeEnvironmentStringsW FreeEnvironmentStringsW%1 "kernel32.dll" |
| 31 |
//go:cgo_import_dynamic runtime._GetConsoleMode GetConsoleMode%2 "kernel32.dll" |
| 32 |
//go:cgo_import_dynamic runtime._GetCurrentThreadId GetCurrentThreadId%0 "kernel32.dll" |
| 33 |
//go:cgo_import_dynamic runtime._GetEnvironmentStringsW GetEnvironmentStringsW%0 "kernel32.dll" |
| 34 |
//go:cgo_import_dynamic runtime._GetErrorMode GetErrorMode%0 "kernel32.dll" |
| 35 |
//go:cgo_import_dynamic runtime._GetProcAddress GetProcAddress%2 "kernel32.dll" |
| 36 |
//go:cgo_import_dynamic runtime._GetProcessAffinityMask GetProcessAffinityMask%3 "kernel32.dll" |
| 37 |
//go:cgo_import_dynamic runtime._GetQueuedCompletionStatusEx GetQueuedCompletionStatusEx%6 "kernel32.dll" |
| 38 |
//go:cgo_import_dynamic runtime._GetStdHandle GetStdHandle%1 "kernel32.dll" |
| 39 |
//go:cgo_import_dynamic runtime._GetSystemDirectoryA GetSystemDirectoryA%2 "kernel32.dll" |
| 40 |
//go:cgo_import_dynamic runtime._GetSystemInfo GetSystemInfo%1 "kernel32.dll" |
| 41 |
//go:cgo_import_dynamic runtime._GetThreadContext GetThreadContext%2 "kernel32.dll" |
| 42 |
//go:cgo_import_dynamic runtime._SetThreadContext SetThreadContext%2 "kernel32.dll" |
| 43 |
//go:cgo_import_dynamic runtime._LoadLibraryExW LoadLibraryExW%3 "kernel32.dll" |
| 44 |
//go:cgo_import_dynamic runtime._PostQueuedCompletionStatus PostQueuedCompletionStatus%4 "kernel32.dll" |
| 45 |
//go:cgo_import_dynamic runtime._QueryPerformanceCounter QueryPerformanceCounter%1 "kernel32.dll" |
| 46 |
//go:cgo_import_dynamic runtime._QueryPerformanceFrequency QueryPerformanceFrequency%1 "kernel32.dll" |
| 47 |
//go:cgo_import_dynamic runtime._RaiseFailFastException RaiseFailFastException%3 "kernel32.dll" |
| 48 |
//go:cgo_import_dynamic runtime._ResumeThread ResumeThread%1 "kernel32.dll" |
| 49 |
//go:cgo_import_dynamic runtime._RtlLookupFunctionEntry RtlLookupFunctionEntry%3 "kernel32.dll" |
| 50 |
//go:cgo_import_dynamic runtime._RtlVirtualUnwind RtlVirtualUnwind%8 "kernel32.dll" |
| 51 |
//go:cgo_import_dynamic runtime._SetConsoleCtrlHandler SetConsoleCtrlHandler%2 "kernel32.dll" |
| 52 |
//go:cgo_import_dynamic runtime._SetErrorMode SetErrorMode%1 "kernel32.dll" |
| 53 |
//go:cgo_import_dynamic runtime._SetEvent SetEvent%1 "kernel32.dll" |
| 54 |
//go:cgo_import_dynamic runtime._SetProcessPriorityBoost SetProcessPriorityBoost%2 "kernel32.dll" |
| 55 |
//go:cgo_import_dynamic runtime._SetThreadPriority SetThreadPriority%2 "kernel32.dll" |
| 56 |
//go:cgo_import_dynamic runtime._SetUnhandledExceptionFilter SetUnhandledExceptionFilter%1 "kernel32.dll" |
| 57 |
//go:cgo_import_dynamic runtime._SetWaitableTimer SetWaitableTimer%6 "kernel32.dll" |
| 58 |
//go:cgo_import_dynamic runtime._SuspendThread SuspendThread%1 "kernel32.dll" |
| 59 |
//go:cgo_import_dynamic runtime._SwitchToThread SwitchToThread%0 "kernel32.dll" |
| 60 |
//go:cgo_import_dynamic runtime._TlsAlloc TlsAlloc%0 "kernel32.dll" |
| 61 |
//go:cgo_import_dynamic runtime._VirtualAlloc VirtualAlloc%4 "kernel32.dll" |
| 62 |
//go:cgo_import_dynamic runtime._VirtualFree VirtualFree%3 "kernel32.dll" |
| 63 |
//go:cgo_import_dynamic runtime._VirtualQuery VirtualQuery%3 "kernel32.dll" |
| 64 |
//go:cgo_import_dynamic runtime._WaitForSingleObject WaitForSingleObject%2 "kernel32.dll" |
| 65 |
//go:cgo_import_dynamic runtime._WaitForMultipleObjects WaitForMultipleObjects%4 "kernel32.dll" |
| 66 |
//go:cgo_import_dynamic runtime._WerGetFlags WerGetFlags%2 "kernel32.dll" |
| 67 |
//go:cgo_import_dynamic runtime._WerSetFlags WerSetFlags%1 "kernel32.dll" |
| 68 |
//go:cgo_import_dynamic runtime._WriteConsoleW WriteConsoleW%5 "kernel32.dll" |
| 69 |
//go:cgo_import_dynamic runtime._WriteFile WriteFile%5 "kernel32.dll" |
| 70 |
|
| 71 |
type stdFunction unsafe.Pointer |
| 72 |
|
| 73 |
var ( |
| 74 |
// Following syscalls are available on every Windows PC. |
| 75 |
// All these variables are set by the Windows executable |
| 76 |
// loader before the Go program starts. |
| 77 |
_AddVectoredContinueHandler, |
| 78 |
_AddVectoredExceptionHandler, |
| 79 |
_CloseHandle, |
| 80 |
_CreateEventA, |
| 81 |
_CreateIoCompletionPort, |
| 82 |
_CreateThread, |
| 83 |
_CreateWaitableTimerA, |
| 84 |
_CreateWaitableTimerExW, |
| 85 |
_DuplicateHandle, |
| 86 |
_ExitProcess, |
| 87 |
_FreeEnvironmentStringsW, |
| 88 |
_GetConsoleMode, |
| 89 |
_GetCurrentThreadId, |
| 90 |
_GetEnvironmentStringsW, |
| 91 |
_GetErrorMode, |
| 92 |
_GetProcAddress, |
| 93 |
_GetProcessAffinityMask, |
| 94 |
_GetQueuedCompletionStatusEx, |
| 95 |
_GetStdHandle, |
| 96 |
_GetSystemDirectoryA, |
| 97 |
_GetSystemInfo, |
| 98 |
_GetThreadContext, |
| 99 |
_SetThreadContext, |
| 100 |
_LoadLibraryExW, |
| 101 |
_PostQueuedCompletionStatus, |
| 102 |
_QueryPerformanceCounter, |
| 103 |
_QueryPerformanceFrequency, |
| 104 |
_RaiseFailFastException, |
| 105 |
_ResumeThread, |
| 106 |
_RtlLookupFunctionEntry, |
| 107 |
_RtlVirtualUnwind, |
| 108 |
_SetConsoleCtrlHandler, |
| 109 |
_SetErrorMode, |
| 110 |
_SetEvent, |
| 111 |
_SetProcessPriorityBoost, |
| 112 |
_SetThreadPriority, |
| 113 |
_SetUnhandledExceptionFilter, |
| 114 |
_SetWaitableTimer, |
| 115 |
_SuspendThread, |
| 116 |
_SwitchToThread, |
| 117 |
_TlsAlloc, |
| 118 |
_VirtualAlloc, |
| 119 |
_VirtualFree, |
| 120 |
_VirtualQuery, |
| 121 |
_WaitForSingleObject, |
| 122 |
_WaitForMultipleObjects, |
| 123 |
_WerGetFlags, |
| 124 |
_WerSetFlags, |
| 125 |
_WriteConsoleW, |
| 126 |
_WriteFile, |
| 127 |
_ stdFunction |
| 128 |
|
| 129 |
// Use ProcessPrng to generate cryptographically random data. |
| 130 |
_ProcessPrng stdFunction |
| 131 |
|
| 132 |
// Load ntdll.dll manually during startup, otherwise Mingw |
| 133 |
// links wrong printf function to cgo executable (see issue |
| 134 |
// 12030 for details). |
| 135 |
_NtCreateWaitCompletionPacket stdFunction |
| 136 |
_NtAssociateWaitCompletionPacket stdFunction |
| 137 |
_NtCancelWaitCompletionPacket stdFunction |
| 138 |
_RtlGetCurrentPeb stdFunction |
| 139 |
_RtlGetVersion stdFunction |
| 140 |
|
| 141 |
// These are from non-kernel32.dll, so we prefer to LoadLibraryEx them. |
| 142 |
_timeBeginPeriod, |
| 143 |
_timeEndPeriod, |
| 144 |
_ stdFunction |
| 145 |
) |
| 146 |
|
| 147 |
var ( |
| 148 |
bcryptprimitivesdll = [...]uint16{'b', 'c', 'r', 'y', 'p', 't', 'p', 'r', 'i', 'm', 'i', 't', 'i', 'v', 'e', 's', '.', 'd', 'l', 'l', 0} |
| 149 |
ntdlldll = [...]uint16{'n', 't', 'd', 'l', 'l', '.', 'd', 'l', 'l', 0} |
| 150 |
powrprofdll = [...]uint16{'p', 'o', 'w', 'r', 'p', 'r', 'o', 'f', '.', 'd', 'l', 'l', 0} |
| 151 |
winmmdll = [...]uint16{'w', 'i', 'n', 'm', 'm', '.', 'd', 'l', 'l', 0} |
| 152 |
) |
| 153 |
|
| 154 |
// Function to be called by windows CreateThread |
| 155 |
// to start new os thread. |
| 156 |
func tstart_stdcall(newm *m) |
| 157 |
|
| 158 |
// Init-time helper |
| 159 |
func wintls() |
| 160 |
|
| 161 |
type mOS struct { |
| 162 |
// This is here to avoid using the G stack so the stack can move during the call. |
| 163 |
stdCallInfo windows.StdCallInfo |
| 164 |
|
| 165 |
threadLock mutex // protects "thread" and prevents closing |
| 166 |
thread uintptr // thread handle |
| 167 |
|
| 168 |
waitsema uintptr // semaphore for parking on locks |
| 169 |
resumesema uintptr // semaphore to indicate suspend/resume |
| 170 |
|
| 171 |
highResTimer uintptr // high resolution timer handle used in usleep |
| 172 |
waitIocpTimer uintptr // high resolution timer handle used in netpoll |
| 173 |
waitIocpHandle uintptr // wait completion handle used in netpoll |
| 174 |
|
| 175 |
// preemptExtLock synchronizes preemptM with entry/exit from |
| 176 |
// external C code. |
| 177 |
// |
| 178 |
// This protects against races between preemptM calling |
| 179 |
// SuspendThread and external code on this thread calling |
| 180 |
// ExitProcess. If these happen concurrently, it's possible to |
| 181 |
// exit the suspending thread and suspend the exiting thread, |
| 182 |
// leading to deadlock. |
| 183 |
// |
| 184 |
// 0 indicates this M is not being preempted or in external |
| 185 |
// code. Entering external code CASes this from 0 to 1. If |
| 186 |
// this fails, a preemption is in progress, so the thread must |
| 187 |
// wait for the preemption. preemptM also CASes this from 0 to |
| 188 |
// 1. If this fails, the preemption fails (as it would if the |
| 189 |
// PC weren't in Go code). The value is reset to 0 when |
| 190 |
// returning from external code or after a preemption is |
| 191 |
// complete. |
| 192 |
// |
| 193 |
// TODO(austin): We may not need this if preemption were more |
| 194 |
// tightly synchronized on the G status and preemption |
| 195 |
// blocked transition into _Gsyscall. |
| 196 |
preemptExtLock uint32 |
| 197 |
} |
| 198 |
|
| 199 |
// Stubs so tests can link correctly. These should never be called. |
| 200 |
func open(name *byte, mode, perm int32) int32 { |
| 201 |
throw("unimplemented") |
| 202 |
return -1 |
| 203 |
} |
| 204 |
func closefd(fd int32) int32 { |
| 205 |
throw("unimplemented") |
| 206 |
return -1 |
| 207 |
} |
| 208 |
func read(fd int32, p unsafe.Pointer, n int32) int32 { |
| 209 |
throw("unimplemented") |
| 210 |
return -1 |
| 211 |
} |
| 212 |
|
| 213 |
type sigset struct{} |
| 214 |
|
| 215 |
var asmstdcallAddr unsafe.Pointer |
| 216 |
|
| 217 |
type winlibcall windows.StdCallInfo |
| 218 |
|
| 219 |
func windowsFindfunc(lib uintptr, name []byte) stdFunction { |
| 220 |
if name[len(name)-1] != 0 { |
| 221 |
throw("usage") |
| 222 |
} |
| 223 |
f := stdcall(_GetProcAddress, lib, uintptr(unsafe.Pointer(&name[0]))) |
| 224 |
return stdFunction(unsafe.Pointer(f)) |
| 225 |
} |
| 226 |
|
| 227 |
const _MAX_PATH = 260 // https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation |
| 228 |
var sysDirectory [_MAX_PATH + 1]byte |
| 229 |
var sysDirectoryLen uintptr |
| 230 |
|
| 231 |
func initSysDirectory() { |
| 232 |
l := stdcall(_GetSystemDirectoryA, uintptr(unsafe.Pointer(&sysDirectory[0])), uintptr(len(sysDirectory)-1)) |
| 233 |
if l == 0 || l > uintptr(len(sysDirectory)-1) { |
| 234 |
throw("Unable to determine system directory") |
| 235 |
} |
| 236 |
sysDirectory[l] = '\\' |
| 237 |
sysDirectoryLen = l + 1 |
| 238 |
} |
| 239 |
|
| 240 |
//go:linkname windows_GetSystemDirectory internal/syscall/windows.GetSystemDirectory |
| 241 |
func windows_GetSystemDirectory() string { |
| 242 |
return unsafe.String(&sysDirectory[0], sysDirectoryLen) |
| 243 |
} |
| 244 |
|
| 245 |
func windowsLoadSystemLib(name []uint16) uintptr { |
| 246 |
const _LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x00000800 |
| 247 |
return stdcall(_LoadLibraryExW, uintptr(unsafe.Pointer(&name[0])), 0, _LOAD_LIBRARY_SEARCH_SYSTEM32) |
| 248 |
} |
| 249 |
|
| 250 |
//go:linkname windows_QueryPerformanceCounter internal/syscall/windows.QueryPerformanceCounter |
| 251 |
func windows_QueryPerformanceCounter() int64 { |
| 252 |
var counter int64 |
| 253 |
stdcall(_QueryPerformanceCounter, uintptr(unsafe.Pointer(&counter))) |
| 254 |
return counter |
| 255 |
} |
| 256 |
|
| 257 |
//go:linkname windows_QueryPerformanceFrequency internal/syscall/windows.QueryPerformanceFrequency |
| 258 |
func windows_QueryPerformanceFrequency() int64 { |
| 259 |
var frequency int64 |
| 260 |
stdcall(_QueryPerformanceFrequency, uintptr(unsafe.Pointer(&frequency))) |
| 261 |
return frequency |
| 262 |
} |
| 263 |
|
| 264 |
func loadOptionalSyscalls() { |
| 265 |
bcryptPrimitives := windowsLoadSystemLib(bcryptprimitivesdll[:]) |
| 266 |
if bcryptPrimitives == 0 { |
| 267 |
throw("bcryptprimitives.dll not found") |
| 268 |
} |
| 269 |
_ProcessPrng = windowsFindfunc(bcryptPrimitives, []byte("ProcessPrng\000")) |
| 270 |
|
| 271 |
n32 := windowsLoadSystemLib(ntdlldll[:]) |
| 272 |
if n32 == 0 { |
| 273 |
throw("ntdll.dll not found") |
| 274 |
} |
| 275 |
_NtCreateWaitCompletionPacket = windowsFindfunc(n32, []byte("NtCreateWaitCompletionPacket\000")) |
| 276 |
if _NtCreateWaitCompletionPacket != nil { |
| 277 |
// These functions should exists if NtCreateWaitCompletionPacket exists. |
| 278 |
_NtAssociateWaitCompletionPacket = windowsFindfunc(n32, []byte("NtAssociateWaitCompletionPacket\000")) |
| 279 |
if _NtAssociateWaitCompletionPacket == nil { |
| 280 |
throw("NtCreateWaitCompletionPacket exists but NtAssociateWaitCompletionPacket does not") |
| 281 |
} |
| 282 |
_NtCancelWaitCompletionPacket = windowsFindfunc(n32, []byte("NtCancelWaitCompletionPacket\000")) |
| 283 |
if _NtCancelWaitCompletionPacket == nil { |
| 284 |
throw("NtCreateWaitCompletionPacket exists but NtCancelWaitCompletionPacket does not") |
| 285 |
} |
| 286 |
} |
| 287 |
_RtlGetCurrentPeb = windowsFindfunc(n32, []byte("RtlGetCurrentPeb\000")) |
| 288 |
_RtlGetVersion = windowsFindfunc(n32, []byte("RtlGetVersion\000")) |
| 289 |
} |
| 290 |
|
| 291 |
func monitorSuspendResume() { |
| 292 |
const ( |
| 293 |
_DEVICE_NOTIFY_CALLBACK = 2 |
| 294 |
) |
| 295 |
type _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS struct { |
| 296 |
callback uintptr |
| 297 |
context uintptr |
| 298 |
} |
| 299 |
|
| 300 |
powrprof := windowsLoadSystemLib(powrprofdll[:]) |
| 301 |
if powrprof == 0 { |
| 302 |
return // Running on Windows 7, where we don't need it anyway. |
| 303 |
} |
| 304 |
powerRegisterSuspendResumeNotification := windowsFindfunc(powrprof, []byte("PowerRegisterSuspendResumeNotification\000")) |
| 305 |
if powerRegisterSuspendResumeNotification == nil { |
| 306 |
return // Running on Windows 7, where we don't need it anyway. |
| 307 |
} |
| 308 |
var fn any = func(context uintptr, changeType uint32, setting uintptr) uintptr { |
| 309 |
for mp := (*m)(atomic.Loadp(unsafe.Pointer(&allm))); mp != nil; mp = mp.alllink { |
| 310 |
if mp.resumesema != 0 { |
| 311 |
stdcall(_SetEvent, mp.resumesema) |
| 312 |
} |
| 313 |
} |
| 314 |
return 0 |
| 315 |
} |
| 316 |
params := _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS{ |
| 317 |
callback: compileCallback(*efaceOf(&fn), true), |
| 318 |
} |
| 319 |
handle := uintptr(0) |
| 320 |
stdcall(powerRegisterSuspendResumeNotification, _DEVICE_NOTIFY_CALLBACK, |
| 321 |
uintptr(unsafe.Pointer(¶ms)), uintptr(unsafe.Pointer(&handle))) |
| 322 |
} |
| 323 |
|
| 324 |
func getCPUCount() int32 { |
| 325 |
var mask, sysmask uintptr |
| 326 |
ret := stdcall(_GetProcessAffinityMask, windows.CurrentProcess, uintptr(unsafe.Pointer(&mask)), uintptr(unsafe.Pointer(&sysmask))) |
| 327 |
if ret != 0 { |
| 328 |
n := 0 |
| 329 |
maskbits := int(unsafe.Sizeof(mask) * 8) |
| 330 |
for i := 0; i < maskbits; i++ { |
| 331 |
if mask&(1<<uint(i)) != 0 { |
| 332 |
n++ |
| 333 |
} |
| 334 |
} |
| 335 |
if n != 0 { |
| 336 |
return int32(n) |
| 337 |
} |
| 338 |
} |
| 339 |
// use GetSystemInfo if GetProcessAffinityMask fails |
| 340 |
var info windows.SystemInfo |
| 341 |
stdcall(_GetSystemInfo, uintptr(unsafe.Pointer(&info))) |
| 342 |
return int32(info.NumberOfProcessors) |
| 343 |
} |
| 344 |
|
| 345 |
func getPageSize() uintptr { |
| 346 |
var info windows.SystemInfo |
| 347 |
stdcall(_GetSystemInfo, uintptr(unsafe.Pointer(&info))) |
| 348 |
return uintptr(info.PageSize) |
| 349 |
} |
| 350 |
|
| 351 |
// in sys_windows_386.s and sys_windows_amd64.s: |
| 352 |
func getlasterror() uint32 |
| 353 |
|
| 354 |
var timeBeginPeriodRetValue uint32 |
| 355 |
|
| 356 |
// osRelaxMinNS indicates that sysmon shouldn't osRelax if the next |
| 357 |
// timer is less than 60 ms from now. Since osRelaxing may reduce |
| 358 |
// timer resolution to 15.6 ms, this keeps timer error under roughly 1 |
| 359 |
// part in 4. |
| 360 |
const osRelaxMinNS = 60 * 1e6 |
| 361 |
|
| 362 |
// osRelax is called by the scheduler when transitioning to and from |
| 363 |
// all Ps being idle. |
| 364 |
// |
| 365 |
// Some versions of Windows have high resolution timer. For those |
| 366 |
// versions osRelax is noop. |
| 367 |
// For Windows versions without high resolution timer, osRelax |
| 368 |
// adjusts the system-wide timer resolution. Go needs a |
| 369 |
// high resolution timer while running and there's little extra cost |
| 370 |
// if we're already using the CPU, but if all Ps are idle there's no |
| 371 |
// need to consume extra power to drive the high-res timer. |
| 372 |
func osRelax(relax bool) uint32 { |
| 373 |
if haveHighResTimer { |
| 374 |
// If the high resolution timer is available, the runtime uses the timer |
| 375 |
// to sleep for short durations. This means there's no need to adjust |
| 376 |
// the global clock frequency. |
| 377 |
return 0 |
| 378 |
} |
| 379 |
|
| 380 |
if relax { |
| 381 |
return uint32(stdcall(_timeEndPeriod, 1)) |
| 382 |
} else { |
| 383 |
return uint32(stdcall(_timeBeginPeriod, 1)) |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
// haveHighResTimer indicates that the CreateWaitableTimerEx |
| 388 |
// CREATE_WAITABLE_TIMER_HIGH_RESOLUTION flag is available. |
| 389 |
var haveHighResTimer = false |
| 390 |
|
| 391 |
// haveHighResSleep indicates that NtCreateWaitCompletionPacket |
| 392 |
// exists and haveHighResTimer is true. |
| 393 |
// NtCreateWaitCompletionPacket has been available since Windows 10, |
| 394 |
// but has just been publicly documented, so some platforms, like Wine, |
| 395 |
// doesn't support it yet. |
| 396 |
var haveHighResSleep = false |
| 397 |
|
| 398 |
// createHighResTimer calls CreateWaitableTimerEx with |
| 399 |
// CREATE_WAITABLE_TIMER_HIGH_RESOLUTION flag to create high |
| 400 |
// resolution timer. createHighResTimer returns new timer |
| 401 |
// handle or 0, if CreateWaitableTimerEx failed. |
| 402 |
func createHighResTimer() uintptr { |
| 403 |
// As per @jstarks, see |
| 404 |
// https://github.com/golang/go/issues/8687#issuecomment-656259353 |
| 405 |
return stdcall(_CreateWaitableTimerExW, 0, 0, |
| 406 |
windows.CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, |
| 407 |
windows.SYNCHRONIZE|windows.TIMER_QUERY_STATE|windows.TIMER_MODIFY_STATE) |
| 408 |
} |
| 409 |
|
| 410 |
func initHighResTimer() { |
| 411 |
h := createHighResTimer() |
| 412 |
if h != 0 { |
| 413 |
haveHighResTimer = true |
| 414 |
haveHighResSleep = _NtCreateWaitCompletionPacket != nil |
| 415 |
stdcall(_CloseHandle, h) |
| 416 |
} else { |
| 417 |
// Only load winmm.dll if we need it. |
| 418 |
// This avoids a dependency on winmm.dll for Go programs |
| 419 |
// that run on new Windows versions. |
| 420 |
m32 := windowsLoadSystemLib(winmmdll[:]) |
| 421 |
if m32 == 0 { |
| 422 |
print("runtime: LoadLibraryExW failed; errno=", getlasterror(), "\n") |
| 423 |
throw("winmm.dll not found") |
| 424 |
} |
| 425 |
_timeBeginPeriod = windowsFindfunc(m32, []byte("timeBeginPeriod\000")) |
| 426 |
_timeEndPeriod = windowsFindfunc(m32, []byte("timeEndPeriod\000")) |
| 427 |
if _timeBeginPeriod == nil || _timeEndPeriod == nil { |
| 428 |
print("runtime: GetProcAddress failed; errno=", getlasterror(), "\n") |
| 429 |
throw("timeBegin/EndPeriod not found") |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
//go:linkname canUseLongPaths internal/syscall/windows.CanUseLongPaths |
| 435 |
var canUseLongPaths bool |
| 436 |
|
| 437 |
// initLongPathSupport enables long path support. |
| 438 |
func initLongPathSupport() { |
| 439 |
const ( |
| 440 |
IsLongPathAwareProcess = 0x80 |
| 441 |
PebBitFieldOffset = 3 |
| 442 |
) |
| 443 |
|
| 444 |
// Check that we're ≥ 10.0.15063. |
| 445 |
info := windows.OSVERSIONINFOW{} |
| 446 |
info.OSVersionInfoSize = uint32(unsafe.Sizeof(info)) |
| 447 |
stdcall(_RtlGetVersion, uintptr(unsafe.Pointer(&info))) |
| 448 |
if info.MajorVersion < 10 || (info.MajorVersion == 10 && info.MinorVersion == 0 && info.BuildNumber < 15063) { |
| 449 |
return |
| 450 |
} |
| 451 |
|
| 452 |
// Set the IsLongPathAwareProcess flag of the PEB's bit field. |
| 453 |
// This flag is not documented, but it's known to be used |
| 454 |
// by Windows to enable long path support. |
| 455 |
bitField := (*byte)(unsafe.Pointer(stdcall(_RtlGetCurrentPeb) + PebBitFieldOffset)) |
| 456 |
*bitField |= IsLongPathAwareProcess |
| 457 |
|
| 458 |
canUseLongPaths = true |
| 459 |
} |
| 460 |
|
| 461 |
func osinit() { |
| 462 |
asmstdcallAddr = unsafe.Pointer(windows.AsmStdCallAddr()) |
| 463 |
|
| 464 |
loadOptionalSyscalls() |
| 465 |
|
| 466 |
preventErrorDialogs() |
| 467 |
|
| 468 |
initExceptionHandler() |
| 469 |
|
| 470 |
initHighResTimer() |
| 471 |
timeBeginPeriodRetValue = osRelax(false) |
| 472 |
|
| 473 |
initSysDirectory() |
| 474 |
initLongPathSupport() |
| 475 |
|
| 476 |
numCPUStartup = getCPUCount() |
| 477 |
|
| 478 |
physPageSize = getPageSize() |
| 479 |
|
| 480 |
// Windows dynamic priority boosting assumes that a process has different types |
| 481 |
// of dedicated threads -- GUI, IO, computational, etc. Go processes use |
| 482 |
// equivalent threads that all do a mix of GUI, IO, computations, etc. |
| 483 |
// In such context dynamic priority boosting does nothing but harm, so we turn it off. |
| 484 |
stdcall(_SetProcessPriorityBoost, windows.CurrentProcess, 1) |
| 485 |
} |
| 486 |
|
| 487 |
//go:nosplit |
| 488 |
func readRandom(r []byte) int { |
| 489 |
n := 0 |
| 490 |
if stdcall(_ProcessPrng, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 { |
| 491 |
n = len(r) |
| 492 |
} |
| 493 |
return n |
| 494 |
} |
| 495 |
|
| 496 |
func goenvs() { |
| 497 |
// strings is a pointer to environment variable pairs in the form: |
| 498 |
// "envA=valA\x00envB=valB\x00\x00" (in UTF-16) |
| 499 |
// Two consecutive zero bytes end the list. |
| 500 |
strings := unsafe.Pointer(stdcall(_GetEnvironmentStringsW)) |
| 501 |
p := (*[1 << 24]uint16)(strings)[:] |
| 502 |
|
| 503 |
n := 0 |
| 504 |
for from, i := 0, 0; true; i++ { |
| 505 |
if p[i] == 0 { |
| 506 |
// empty string marks the end |
| 507 |
if i == from { |
| 508 |
break |
| 509 |
} |
| 510 |
from = i + 1 |
| 511 |
n++ |
| 512 |
} |
| 513 |
} |
| 514 |
envs = make([]string, n) |
| 515 |
|
| 516 |
for i := range envs { |
| 517 |
envs[i] = gostringw(&p[0]) |
| 518 |
for p[0] != 0 { |
| 519 |
p = p[1:] |
| 520 |
} |
| 521 |
p = p[1:] // skip nil byte |
| 522 |
} |
| 523 |
|
| 524 |
stdcall(_FreeEnvironmentStringsW, uintptr(strings)) |
| 525 |
|
| 526 |
// We call these all the way here, late in init, so that malloc works |
| 527 |
// for the callback functions these generate. |
| 528 |
var fn any = ctrlHandler |
| 529 |
ctrlHandlerPC := compileCallback(*efaceOf(&fn), true) |
| 530 |
stdcall(_SetConsoleCtrlHandler, ctrlHandlerPC, 1) |
| 531 |
|
| 532 |
monitorSuspendResume() |
| 533 |
} |
| 534 |
|
| 535 |
// exiting is set to non-zero when the process is exiting. |
| 536 |
var exiting uint32 |
| 537 |
|
| 538 |
//go:nosplit |
| 539 |
func exit(code int32) { |
| 540 |
// Disallow thread suspension for preemption. Otherwise, |
| 541 |
// ExitProcess and SuspendThread can race: SuspendThread |
| 542 |
// queues a suspension request for this thread, ExitProcess |
| 543 |
// kills the suspending thread, and then this thread suspends. |
| 544 |
lock(&suspendLock) |
| 545 |
atomic.Store(&exiting, 1) |
| 546 |
stdcall(_ExitProcess, uintptr(code)) |
| 547 |
} |
| 548 |
|
| 549 |
// write1 must be nosplit because it's used as a last resort in |
| 550 |
// functions like badmorestackg0. In such cases, we'll always take the |
| 551 |
// ASCII path. |
| 552 |
// |
| 553 |
//go:nosplit |
| 554 |
func write1(fd uintptr, buf unsafe.Pointer, n int32) int32 { |
| 555 |
const ( |
| 556 |
_STD_OUTPUT_HANDLE = ^uintptr(10) // -11 |
| 557 |
_STD_ERROR_HANDLE = ^uintptr(11) // -12 |
| 558 |
) |
| 559 |
var handle uintptr |
| 560 |
switch fd { |
| 561 |
case 1: |
| 562 |
handle = stdcall(_GetStdHandle, _STD_OUTPUT_HANDLE) |
| 563 |
case 2: |
| 564 |
handle = stdcall(_GetStdHandle, _STD_ERROR_HANDLE) |
| 565 |
default: |
| 566 |
// assume fd is real windows handle. |
| 567 |
handle = fd |
| 568 |
} |
| 569 |
isASCII := true |
| 570 |
b := (*[1 << 30]byte)(buf)[:n] |
| 571 |
for _, x := range b { |
| 572 |
if x >= 0x80 { |
| 573 |
isASCII = false |
| 574 |
break |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
if !isASCII { |
| 579 |
var m uint32 |
| 580 |
isConsole := stdcall(_GetConsoleMode, handle, uintptr(unsafe.Pointer(&m))) != 0 |
| 581 |
// If this is a console output, various non-unicode code pages can be in use. |
| 582 |
// Use the dedicated WriteConsole call to ensure unicode is printed correctly. |
| 583 |
if isConsole { |
| 584 |
return int32(writeConsole(handle, buf, n)) |
| 585 |
} |
| 586 |
} |
| 587 |
var written uint32 |
| 588 |
stdcall(_WriteFile, handle, uintptr(buf), uintptr(n), uintptr(unsafe.Pointer(&written)), 0) |
| 589 |
return int32(written) |
| 590 |
} |
| 591 |
|
| 592 |
var ( |
| 593 |
utf16ConsoleBack [1000]uint16 |
| 594 |
utf16ConsoleBackLock mutex |
| 595 |
) |
| 596 |
|
| 597 |
// writeConsole writes bufLen bytes from buf to the console File. |
| 598 |
// It returns the number of bytes written. |
| 599 |
func writeConsole(handle uintptr, buf unsafe.Pointer, bufLen int32) int { |
| 600 |
const surr2 = (surrogateMin + surrogateMax + 1) / 2 |
| 601 |
|
| 602 |
// Do not use defer for unlock. May cause issues when printing a panic. |
| 603 |
lock(&utf16ConsoleBackLock) |
| 604 |
|
| 605 |
b := (*[1 << 30]byte)(buf)[:bufLen] |
| 606 |
s := *(*string)(unsafe.Pointer(&b)) |
| 607 |
|
| 608 |
utf16tmp := utf16ConsoleBack[:] |
| 609 |
|
| 610 |
total := len(s) |
| 611 |
w := 0 |
| 612 |
for _, r := range s { |
| 613 |
if w >= len(utf16tmp)-2 { |
| 614 |
writeConsoleUTF16(handle, utf16tmp[:w]) |
| 615 |
w = 0 |
| 616 |
} |
| 617 |
if r < 0x10000 { |
| 618 |
utf16tmp[w] = uint16(r) |
| 619 |
w++ |
| 620 |
} else { |
| 621 |
r -= 0x10000 |
| 622 |
utf16tmp[w] = surrogateMin + uint16(r>>10)&0x3ff |
| 623 |
utf16tmp[w+1] = surr2 + uint16(r)&0x3ff |
| 624 |
w += 2 |
| 625 |
} |
| 626 |
} |
| 627 |
writeConsoleUTF16(handle, utf16tmp[:w]) |
| 628 |
unlock(&utf16ConsoleBackLock) |
| 629 |
return total |
| 630 |
} |
| 631 |
|
| 632 |
// writeConsoleUTF16 is the dedicated windows calls that correctly prints |
| 633 |
// to the console regardless of the current code page. Input is utf-16 code points. |
| 634 |
// The handle must be a console handle. |
| 635 |
func writeConsoleUTF16(handle uintptr, b []uint16) { |
| 636 |
l := uint32(len(b)) |
| 637 |
if l == 0 { |
| 638 |
return |
| 639 |
} |
| 640 |
var written uint32 |
| 641 |
stdcall(_WriteConsoleW, |
| 642 |
handle, |
| 643 |
uintptr(unsafe.Pointer(&b[0])), |
| 644 |
uintptr(l), |
| 645 |
uintptr(unsafe.Pointer(&written)), |
| 646 |
0, |
| 647 |
) |
| 648 |
return |
| 649 |
} |
| 650 |
|
| 651 |
//go:nosplit |
| 652 |
func semasleep(ns int64) int32 { |
| 653 |
const ( |
| 654 |
_WAIT_ABANDONED = 0x00000080 |
| 655 |
_WAIT_OBJECT_0 = 0x00000000 |
| 656 |
_WAIT_TIMEOUT = 0x00000102 |
| 657 |
_WAIT_FAILED = 0xFFFFFFFF |
| 658 |
) |
| 659 |
|
| 660 |
var result uintptr |
| 661 |
if ns < 0 { |
| 662 |
result = stdcall(_WaitForSingleObject, getg().m.waitsema, uintptr(windows.INFINITE)) |
| 663 |
} else { |
| 664 |
start := nanotime() |
| 665 |
elapsed := int64(0) |
| 666 |
for { |
| 667 |
ms := (ns - elapsed) / 1000000 |
| 668 |
if ms == 0 { |
| 669 |
ms = 1 |
| 670 |
} |
| 671 |
result = stdcall(_WaitForMultipleObjects, 2, |
| 672 |
uintptr(unsafe.Pointer(&[2]uintptr{getg().m.waitsema, getg().m.resumesema})), |
| 673 |
0, uintptr(ms)) |
| 674 |
if result != _WAIT_OBJECT_0+1 { |
| 675 |
// Not a suspend/resume event |
| 676 |
break |
| 677 |
} |
| 678 |
elapsed = nanotime() - start |
| 679 |
if elapsed >= ns { |
| 680 |
return -1 |
| 681 |
} |
| 682 |
} |
| 683 |
} |
| 684 |
switch result { |
| 685 |
case _WAIT_OBJECT_0: // Signaled |
| 686 |
return 0 |
| 687 |
|
| 688 |
case _WAIT_TIMEOUT: |
| 689 |
return -1 |
| 690 |
|
| 691 |
case _WAIT_ABANDONED: |
| 692 |
systemstack(func() { |
| 693 |
throw("runtime.semasleep wait_abandoned") |
| 694 |
}) |
| 695 |
|
| 696 |
case _WAIT_FAILED: |
| 697 |
systemstack(func() { |
| 698 |
print("runtime: waitforsingleobject wait_failed; errno=", getlasterror(), "\n") |
| 699 |
throw("runtime.semasleep wait_failed") |
| 700 |
}) |
| 701 |
|
| 702 |
default: |
| 703 |
systemstack(func() { |
| 704 |
print("runtime: waitforsingleobject unexpected; result=", result, "\n") |
| 705 |
throw("runtime.semasleep unexpected") |
| 706 |
}) |
| 707 |
} |
| 708 |
|
| 709 |
return -1 // unreachable |
| 710 |
} |
| 711 |
|
| 712 |
//go:nosplit |
| 713 |
func semawakeup(mp *m) { |
| 714 |
if stdcall(_SetEvent, mp.waitsema) == 0 { |
| 715 |
systemstack(func() { |
| 716 |
print("runtime: setevent failed; errno=", getlasterror(), "\n") |
| 717 |
throw("runtime.semawakeup") |
| 718 |
}) |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
//go:nosplit |
| 723 |
func semacreate(mp *m) { |
| 724 |
if mp.waitsema != 0 { |
| 725 |
return |
| 726 |
} |
| 727 |
mp.waitsema = stdcall(_CreateEventA, 0, 0, 0, 0) |
| 728 |
if mp.waitsema == 0 { |
| 729 |
systemstack(func() { |
| 730 |
print("runtime: createevent failed; errno=", getlasterror(), "\n") |
| 731 |
throw("runtime.semacreate") |
| 732 |
}) |
| 733 |
} |
| 734 |
mp.resumesema = stdcall(_CreateEventA, 0, 0, 0, 0) |
| 735 |
if mp.resumesema == 0 { |
| 736 |
systemstack(func() { |
| 737 |
print("runtime: createevent failed; errno=", getlasterror(), "\n") |
| 738 |
throw("runtime.semacreate") |
| 739 |
}) |
| 740 |
stdcall(_CloseHandle, mp.waitsema) |
| 741 |
mp.waitsema = 0 |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
// May run with m.p==nil, so write barriers are not allowed. This |
| 746 |
// function is called by newosproc0, so it is also required to |
| 747 |
// operate without stack guards. |
| 748 |
// |
| 749 |
//go:nowritebarrierrec |
| 750 |
//go:nosplit |
| 751 |
func newosproc(mp *m) { |
| 752 |
// We pass 0 for the stack size to use the default for this binary. |
| 753 |
thandle := stdcall(_CreateThread, 0, 0, |
| 754 |
abi.FuncPCABI0(tstart_stdcall), uintptr(unsafe.Pointer(mp)), |
| 755 |
0, 0) |
| 756 |
|
| 757 |
if thandle == 0 { |
| 758 |
if atomic.Load(&exiting) != 0 { |
| 759 |
// CreateThread may fail if called |
| 760 |
// concurrently with ExitProcess. If this |
| 761 |
// happens, just freeze this thread and let |
| 762 |
// the process exit. See issue #18253. |
| 763 |
lock(&deadlock) |
| 764 |
lock(&deadlock) |
| 765 |
} |
| 766 |
print("runtime: failed to create new OS thread (have ", mcount(), " already; errno=", getlasterror(), ")\n") |
| 767 |
throw("runtime.newosproc") |
| 768 |
} |
| 769 |
|
| 770 |
// Close thandle to avoid leaking the thread object if it exits. |
| 771 |
stdcall(_CloseHandle, thandle) |
| 772 |
} |
| 773 |
|
| 774 |
// Used by the C library build mode. On Linux this function would allocate a |
| 775 |
// stack, but that's not necessary for Windows. No stack guards are present |
| 776 |
// and the GC has not been initialized, so write barriers will fail. |
| 777 |
// |
| 778 |
//go:nowritebarrierrec |
| 779 |
//go:nosplit |
| 780 |
func newosproc0(stacksize uintptr, fn uintptr) { |
| 781 |
throw("bad newosproc0") |
| 782 |
} |
| 783 |
|
| 784 |
//go:nosplit |
| 785 |
//go:nowritebarrierrec |
| 786 |
func libpreinit() {} |
| 787 |
|
| 788 |
func exitThread(wait *atomic.Uint32) { |
| 789 |
// We should never reach exitThread on Windows because we let |
| 790 |
// the OS clean up threads. |
| 791 |
throw("exitThread") |
| 792 |
} |
| 793 |
|
| 794 |
// Called to initialize a new m (including the bootstrap m). |
| 795 |
// Called on the parent thread (main thread in case of bootstrap), can allocate memory. |
| 796 |
func mpreinit(mp *m) { |
| 797 |
} |
| 798 |
|
| 799 |
//go:nosplit |
| 800 |
func sigsave(p *sigset) { |
| 801 |
} |
| 802 |
|
| 803 |
//go:nosplit |
| 804 |
func msigrestore(sigmask sigset) { |
| 805 |
} |
| 806 |
|
| 807 |
//go:nosplit |
| 808 |
//go:nowritebarrierrec |
| 809 |
func clearSignalHandlers() { |
| 810 |
} |
| 811 |
|
| 812 |
//go:nosplit |
| 813 |
func sigblock(exiting bool) { |
| 814 |
} |
| 815 |
|
| 816 |
// Called to initialize a new m (including the bootstrap m). |
| 817 |
// Called on the new thread, cannot allocate Go memory. |
| 818 |
func minit() { |
| 819 |
var thandle uintptr |
| 820 |
if stdcall(_DuplicateHandle, windows.CurrentProcess, windows.CurrentThread, windows.CurrentProcess, uintptr(unsafe.Pointer(&thandle)), 0, 0, windows.DUPLICATE_SAME_ACCESS) == 0 { |
| 821 |
print("runtime.minit: duplicatehandle failed; errno=", getlasterror(), "\n") |
| 822 |
throw("runtime.minit: duplicatehandle failed") |
| 823 |
} |
| 824 |
|
| 825 |
mp := getg().m |
| 826 |
lock(&mp.threadLock) |
| 827 |
mp.thread = thandle |
| 828 |
mp.procid = uint64(stdcall(_GetCurrentThreadId)) |
| 829 |
|
| 830 |
// Configure usleep timer, if possible. |
| 831 |
if mp.highResTimer == 0 && haveHighResTimer { |
| 832 |
mp.highResTimer = createHighResTimer() |
| 833 |
if mp.highResTimer == 0 { |
| 834 |
print("runtime: CreateWaitableTimerEx failed; errno=", getlasterror(), "\n") |
| 835 |
throw("CreateWaitableTimerEx when creating timer failed") |
| 836 |
} |
| 837 |
} |
| 838 |
if mp.waitIocpHandle == 0 && haveHighResSleep { |
| 839 |
mp.waitIocpTimer = createHighResTimer() |
| 840 |
if mp.waitIocpTimer == 0 { |
| 841 |
print("runtime: CreateWaitableTimerEx failed; errno=", getlasterror(), "\n") |
| 842 |
throw("CreateWaitableTimerEx when creating timer failed") |
| 843 |
} |
| 844 |
const GENERIC_ALL = 0x10000000 |
| 845 |
errno := stdcall(_NtCreateWaitCompletionPacket, uintptr(unsafe.Pointer(&mp.waitIocpHandle)), GENERIC_ALL, 0) |
| 846 |
if mp.waitIocpHandle == 0 { |
| 847 |
print("runtime: NtCreateWaitCompletionPacket failed; errno=", errno, "\n") |
| 848 |
throw("NtCreateWaitCompletionPacket failed") |
| 849 |
} |
| 850 |
} |
| 851 |
unlock(&mp.threadLock) |
| 852 |
|
| 853 |
// Query the true stack base from the OS. Currently we're |
| 854 |
// running on a small assumed stack. |
| 855 |
var mbi windows.MemoryBasicInformation |
| 856 |
res := stdcall(_VirtualQuery, uintptr(unsafe.Pointer(&mbi)), uintptr(unsafe.Pointer(&mbi)), unsafe.Sizeof(mbi)) |
| 857 |
if res == 0 { |
| 858 |
print("runtime: VirtualQuery failed; errno=", getlasterror(), "\n") |
| 859 |
throw("VirtualQuery for stack base failed") |
| 860 |
} |
| 861 |
// The system leaves an 8K PAGE_GUARD region at the bottom of |
| 862 |
// the stack (in theory VirtualQuery isn't supposed to include |
| 863 |
// that, but it does). Add an additional 8K of slop for |
| 864 |
// calling C functions that don't have stack checks and for |
| 865 |
// lastcontinuehandler. We shouldn't be anywhere near this |
| 866 |
// bound anyway. |
| 867 |
base := mbi.AllocationBase + 16<<10 |
| 868 |
// Sanity check the stack bounds. |
| 869 |
g0 := getg() |
| 870 |
if base > g0.stack.hi || g0.stack.hi-base > 64<<20 { |
| 871 |
print("runtime: g0 stack [", hex(base), ",", hex(g0.stack.hi), ")\n") |
| 872 |
throw("bad g0 stack") |
| 873 |
} |
| 874 |
g0.stack.lo = base |
| 875 |
g0.stackguard0 = g0.stack.lo + stackGuard |
| 876 |
g0.stackguard1 = g0.stackguard0 |
| 877 |
// Sanity check the SP. |
| 878 |
stackcheck() |
| 879 |
} |
| 880 |
|
| 881 |
// Called from dropm to undo the effect of an minit. |
| 882 |
// |
| 883 |
//go:nosplit |
| 884 |
func unminit() { |
| 885 |
mp := getg().m |
| 886 |
lock(&mp.threadLock) |
| 887 |
if mp.thread != 0 { |
| 888 |
stdcall(_CloseHandle, mp.thread) |
| 889 |
mp.thread = 0 |
| 890 |
} |
| 891 |
unlock(&mp.threadLock) |
| 892 |
|
| 893 |
mp.procid = 0 |
| 894 |
} |
| 895 |
|
| 896 |
// Called from mexit, but not from dropm, to undo the effect of thread-owned |
| 897 |
// resources in minit, semacreate, or elsewhere. Do not take locks after calling this. |
| 898 |
// |
| 899 |
// This always runs without a P, so //go:nowritebarrierrec is required. |
| 900 |
// |
| 901 |
//go:nowritebarrierrec |
| 902 |
//go:nosplit |
| 903 |
func mdestroy(mp *m) { |
| 904 |
if mp.highResTimer != 0 { |
| 905 |
stdcall(_CloseHandle, mp.highResTimer) |
| 906 |
mp.highResTimer = 0 |
| 907 |
} |
| 908 |
if mp.waitIocpTimer != 0 { |
| 909 |
stdcall(_CloseHandle, mp.waitIocpTimer) |
| 910 |
mp.waitIocpTimer = 0 |
| 911 |
} |
| 912 |
if mp.waitIocpHandle != 0 { |
| 913 |
stdcall(_CloseHandle, mp.waitIocpHandle) |
| 914 |
mp.waitIocpHandle = 0 |
| 915 |
} |
| 916 |
if mp.waitsema != 0 { |
| 917 |
stdcall(_CloseHandle, mp.waitsema) |
| 918 |
mp.waitsema = 0 |
| 919 |
} |
| 920 |
if mp.resumesema != 0 { |
| 921 |
stdcall(_CloseHandle, mp.resumesema) |
| 922 |
mp.resumesema = 0 |
| 923 |
} |
| 924 |
} |
| 925 |
|
| 926 |
// stdcall_no_g is like [stdcall] but can be called without a G. |
| 927 |
// |
| 928 |
//go:nowritebarrier |
| 929 |
//go:nosplit |
| 930 |
//go:uintptrkeepalive |
| 931 |
func stdcall_no_g(fn stdFunction, args ...uintptr) uintptr { |
| 932 |
call := windows.StdCallInfo{ |
| 933 |
Fn: uintptr(unsafe.Pointer(fn)), |
| 934 |
N: uintptr(len(args)), |
| 935 |
} |
| 936 |
if len(args) > 0 { |
| 937 |
call.Args = uintptr(abi.NoEscape(unsafe.Pointer(&args[0]))) |
| 938 |
} |
| 939 |
windows.StdCall(&call) |
| 940 |
return call.R1 |
| 941 |
} |
| 942 |
|
| 943 |
// stdcall calls fn with the given arguments using the stdcall calling convention. |
| 944 |
// Must be called from the system stack. |
| 945 |
// May run during STW, so write barriers are not allowed. |
| 946 |
// |
| 947 |
//go:nowritebarrier |
| 948 |
//go:nosplit |
| 949 |
//go:uintptrkeepalive |
| 950 |
func stdcall(fn stdFunction, args ...uintptr) uintptr { |
| 951 |
gp := getg() |
| 952 |
mp := gp.m |
| 953 |
mp.stdCallInfo.Fn = uintptr(unsafe.Pointer(fn)) |
| 954 |
mp.stdCallInfo.N = uintptr(len(args)) |
| 955 |
if len(args) > 0 { |
| 956 |
mp.stdCallInfo.Args = uintptr(abi.NoEscape(unsafe.Pointer(&args[0]))) |
| 957 |
} |
| 958 |
resetLibcall := false |
| 959 |
if mp.profilehz != 0 && mp.libcallsp == 0 { |
| 960 |
// leave pc/sp for cpu profiler |
| 961 |
mp.libcallg.set(gp) |
| 962 |
mp.libcallpc = sys.GetCallerPC() |
| 963 |
// sp must be the last, because once async cpu profiler finds |
| 964 |
// all three values to be non-zero, it will use them |
| 965 |
mp.libcallsp = sys.GetCallerSP() |
| 966 |
resetLibcall = true // See comment in sys_darwin.go:libcCall |
| 967 |
} |
| 968 |
asmcgocall(asmstdcallAddr, unsafe.Pointer(&mp.stdCallInfo)) |
| 969 |
if resetLibcall { |
| 970 |
mp.libcallsp = 0 |
| 971 |
} |
| 972 |
return mp.stdCallInfo.R1 |
| 973 |
} |
| 974 |
|
| 975 |
// These must run on the system stack only. |
| 976 |
|
| 977 |
//go:nosplit |
| 978 |
func osyield_no_g() { |
| 979 |
stdcall_no_g(_SwitchToThread) |
| 980 |
} |
| 981 |
|
| 982 |
//go:nosplit |
| 983 |
func osyield() { |
| 984 |
systemstack(func() { |
| 985 |
stdcall(_SwitchToThread) |
| 986 |
}) |
| 987 |
} |
| 988 |
|
| 989 |
//go:nosplit |
| 990 |
func usleep_no_g(us uint32) { |
| 991 |
timeout := uintptr(us) / 1000 // ms units |
| 992 |
stdcall_no_g(_WaitForSingleObject, windows.INVALID_HANDLE_VALUE, timeout) |
| 993 |
} |
| 994 |
|
| 995 |
//go:nosplit |
| 996 |
func usleep(us uint32) { |
| 997 |
systemstack(func() { |
| 998 |
var h, timeout uintptr |
| 999 |
// If the high-res timer is available and its handle has been allocated for this m, use it. |
| 1000 |
// Otherwise fall back to the low-res one, which doesn't need a handle. |
| 1001 |
if haveHighResTimer && getg().m.highResTimer != 0 { |
| 1002 |
h = getg().m.highResTimer |
| 1003 |
dt := -10 * int64(us) // relative sleep (negative), 100ns units |
| 1004 |
stdcall(_SetWaitableTimer, h, uintptr(unsafe.Pointer(&dt)), 0, 0, 0, 0) |
| 1005 |
timeout = windows.INFINITE |
| 1006 |
} else { |
| 1007 |
h = windows.INVALID_HANDLE_VALUE |
| 1008 |
timeout = uintptr(us) / 1000 // ms units |
| 1009 |
} |
| 1010 |
stdcall(_WaitForSingleObject, h, timeout) |
| 1011 |
}) |
| 1012 |
} |
| 1013 |
|
| 1014 |
func ctrlHandler(_type uint32) uintptr { |
| 1015 |
var s uint32 |
| 1016 |
|
| 1017 |
switch _type { |
| 1018 |
case windows.CTRL_C_EVENT, windows.CTRL_BREAK_EVENT: |
| 1019 |
s = windows.SIGINT |
| 1020 |
case windows.CTRL_CLOSE_EVENT, windows.CTRL_LOGOFF_EVENT, windows.CTRL_SHUTDOWN_EVENT: |
| 1021 |
s = windows.SIGTERM |
| 1022 |
default: |
| 1023 |
return 0 |
| 1024 |
} |
| 1025 |
|
| 1026 |
if sigsend(s) { |
| 1027 |
if s == windows.SIGTERM { |
| 1028 |
// Windows terminates the process after this handler returns. |
| 1029 |
// Block indefinitely to give signal handlers a chance to clean up, |
| 1030 |
// but make sure to be properly parked first, so the rest of the |
| 1031 |
// program can continue executing. |
| 1032 |
block() |
| 1033 |
} |
| 1034 |
return 1 |
| 1035 |
} |
| 1036 |
return 0 |
| 1037 |
} |
| 1038 |
|
| 1039 |
// called from zcallback_windows_*.s to sys_windows_*.s |
| 1040 |
func callbackasm1() |
| 1041 |
|
| 1042 |
var profiletimer uintptr |
| 1043 |
|
| 1044 |
func profilem(mp *m, thread uintptr) { |
| 1045 |
// Align Context to 16 bytes. |
| 1046 |
var c *windows.Context |
| 1047 |
var cbuf [unsafe.Sizeof(*c) + 15]byte |
| 1048 |
c = (*windows.Context)(unsafe.Pointer((uintptr(unsafe.Pointer(&cbuf[15]))) &^ 15)) |
| 1049 |
|
| 1050 |
c.ContextFlags = windows.CONTEXT_CONTROL |
| 1051 |
stdcall(_GetThreadContext, thread, uintptr(unsafe.Pointer(c))) |
| 1052 |
|
| 1053 |
gp := gFromSP(mp, c.SP()) |
| 1054 |
|
| 1055 |
sigprof(c.PC(), c.SP(), c.LR(), gp, mp) |
| 1056 |
} |
| 1057 |
|
| 1058 |
func gFromSP(mp *m, sp uintptr) *g { |
| 1059 |
if gp := mp.g0; gp != nil && gp.stack.lo < sp && sp < gp.stack.hi { |
| 1060 |
return gp |
| 1061 |
} |
| 1062 |
if gp := mp.gsignal; gp != nil && gp.stack.lo < sp && sp < gp.stack.hi { |
| 1063 |
return gp |
| 1064 |
} |
| 1065 |
if gp := mp.curg; gp != nil && gp.stack.lo < sp && sp < gp.stack.hi { |
| 1066 |
return gp |
| 1067 |
} |
| 1068 |
return nil |
| 1069 |
} |
| 1070 |
|
| 1071 |
func profileLoop() { |
| 1072 |
stdcall(_SetThreadPriority, windows.CurrentThread, windows.THREAD_PRIORITY_HIGHEST) |
| 1073 |
|
| 1074 |
for { |
| 1075 |
stdcall(_WaitForSingleObject, profiletimer, windows.INFINITE) |
| 1076 |
first := (*m)(atomic.Loadp(unsafe.Pointer(&allm))) |
| 1077 |
for mp := first; mp != nil; mp = mp.alllink { |
| 1078 |
if mp == getg().m { |
| 1079 |
// Don't profile ourselves. |
| 1080 |
continue |
| 1081 |
} |
| 1082 |
|
| 1083 |
lock(&mp.threadLock) |
| 1084 |
// Do not profile threads blocked on Notes, |
| 1085 |
// this includes idle worker threads, |
| 1086 |
// idle timer thread, idle heap scavenger, etc. |
| 1087 |
if mp.thread == 0 || mp.profilehz == 0 || mp.blocked { |
| 1088 |
unlock(&mp.threadLock) |
| 1089 |
continue |
| 1090 |
} |
| 1091 |
// Acquire our own handle to the thread. |
| 1092 |
var thread uintptr |
| 1093 |
if stdcall(_DuplicateHandle, windows.CurrentProcess, mp.thread, windows.CurrentProcess, uintptr(unsafe.Pointer(&thread)), 0, 0, windows.DUPLICATE_SAME_ACCESS) == 0 { |
| 1094 |
print("runtime: duplicatehandle failed; errno=", getlasterror(), "\n") |
| 1095 |
throw("duplicatehandle failed") |
| 1096 |
} |
| 1097 |
unlock(&mp.threadLock) |
| 1098 |
|
| 1099 |
// mp may exit between the DuplicateHandle |
| 1100 |
// above and the SuspendThread. The handle |
| 1101 |
// will remain valid, but SuspendThread may |
| 1102 |
// fail. |
| 1103 |
if int32(stdcall(_SuspendThread, thread)) == -1 { |
| 1104 |
// The thread no longer exists. |
| 1105 |
stdcall(_CloseHandle, thread) |
| 1106 |
continue |
| 1107 |
} |
| 1108 |
if mp.profilehz != 0 && !mp.blocked { |
| 1109 |
// Pass the thread handle in case mp |
| 1110 |
// was in the process of shutting down. |
| 1111 |
profilem(mp, thread) |
| 1112 |
} |
| 1113 |
stdcall(_ResumeThread, thread) |
| 1114 |
stdcall(_CloseHandle, thread) |
| 1115 |
} |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
func setProcessCPUProfiler(hz int32) { |
| 1120 |
if profiletimer == 0 { |
| 1121 |
var timer uintptr |
| 1122 |
if haveHighResTimer { |
| 1123 |
timer = createHighResTimer() |
| 1124 |
} else { |
| 1125 |
timer = stdcall(_CreateWaitableTimerA, 0, 0, 0) |
| 1126 |
} |
| 1127 |
atomic.Storeuintptr(&profiletimer, timer) |
| 1128 |
newm(profileLoop, nil, -1) |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
func setThreadCPUProfiler(hz int32) { |
| 1133 |
ms := int32(0) |
| 1134 |
due := ^int64(^uint64(1 << 63)) |
| 1135 |
if hz > 0 { |
| 1136 |
ms = 1000 / hz |
| 1137 |
if ms == 0 { |
| 1138 |
ms = 1 |
| 1139 |
} |
| 1140 |
due = int64(ms) * -10000 |
| 1141 |
} |
| 1142 |
stdcall(_SetWaitableTimer, profiletimer, uintptr(unsafe.Pointer(&due)), uintptr(ms), 0, 0, 0) |
| 1143 |
atomic.Store((*uint32)(unsafe.Pointer(&getg().m.profilehz)), uint32(hz)) |
| 1144 |
} |
| 1145 |
|
| 1146 |
const preemptMSupported = true |
| 1147 |
|
| 1148 |
// suspendLock protects simultaneous SuspendThread operations from |
| 1149 |
// suspending each other. |
| 1150 |
var suspendLock mutex |
| 1151 |
|
| 1152 |
func preemptM(mp *m) { |
| 1153 |
if mp == getg().m { |
| 1154 |
throw("self-preempt") |
| 1155 |
} |
| 1156 |
|
| 1157 |
// Synchronize with external code that may try to ExitProcess. |
| 1158 |
if !atomic.Cas(&mp.preemptExtLock, 0, 1) { |
| 1159 |
// External code is running. Fail the preemption |
| 1160 |
// attempt. |
| 1161 |
mp.preemptGen.Add(1) |
| 1162 |
return |
| 1163 |
} |
| 1164 |
|
| 1165 |
// Acquire our own handle to mp's thread. |
| 1166 |
lock(&mp.threadLock) |
| 1167 |
if mp.thread == 0 { |
| 1168 |
// The M hasn't been minit'd yet (or was just unminit'd). |
| 1169 |
unlock(&mp.threadLock) |
| 1170 |
atomic.Store(&mp.preemptExtLock, 0) |
| 1171 |
mp.preemptGen.Add(1) |
| 1172 |
return |
| 1173 |
} |
| 1174 |
var thread uintptr |
| 1175 |
if stdcall(_DuplicateHandle, windows.CurrentProcess, mp.thread, windows.CurrentProcess, uintptr(unsafe.Pointer(&thread)), 0, 0, windows.DUPLICATE_SAME_ACCESS) == 0 { |
| 1176 |
print("runtime.preemptM: duplicatehandle failed; errno=", getlasterror(), "\n") |
| 1177 |
throw("runtime.preemptM: duplicatehandle failed") |
| 1178 |
} |
| 1179 |
unlock(&mp.threadLock) |
| 1180 |
|
| 1181 |
// Prepare thread context buffer. This must be aligned to 16 bytes. |
| 1182 |
var c *windows.Context |
| 1183 |
var cbuf [unsafe.Sizeof(*c) + 15]byte |
| 1184 |
c = (*windows.Context)(unsafe.Pointer((uintptr(unsafe.Pointer(&cbuf[15]))) &^ 15)) |
| 1185 |
c.ContextFlags = windows.CONTEXT_CONTROL |
| 1186 |
|
| 1187 |
// Serialize thread suspension. SuspendThread is asynchronous, |
| 1188 |
// so it's otherwise possible for two threads to suspend each |
| 1189 |
// other and deadlock. We must hold this lock until after |
| 1190 |
// GetThreadContext, since that blocks until the thread is |
| 1191 |
// actually suspended. |
| 1192 |
lock(&suspendLock) |
| 1193 |
|
| 1194 |
// Suspend the thread. |
| 1195 |
if int32(stdcall(_SuspendThread, thread)) == -1 { |
| 1196 |
unlock(&suspendLock) |
| 1197 |
stdcall(_CloseHandle, thread) |
| 1198 |
atomic.Store(&mp.preemptExtLock, 0) |
| 1199 |
// The thread no longer exists. This shouldn't be |
| 1200 |
// possible, but just acknowledge the request. |
| 1201 |
mp.preemptGen.Add(1) |
| 1202 |
return |
| 1203 |
} |
| 1204 |
|
| 1205 |
// We have to be very careful between this point and once |
| 1206 |
// we've shown mp is at an async safe-point. This is like a |
| 1207 |
// signal handler in the sense that mp could have been doing |
| 1208 |
// anything when we stopped it, including holding arbitrary |
| 1209 |
// locks. |
| 1210 |
|
| 1211 |
// We have to get the thread context before inspecting the M |
| 1212 |
// because SuspendThread only requests a suspend. |
| 1213 |
// GetThreadContext actually blocks until it's suspended. |
| 1214 |
stdcall(_GetThreadContext, thread, uintptr(unsafe.Pointer(c))) |
| 1215 |
|
| 1216 |
unlock(&suspendLock) |
| 1217 |
|
| 1218 |
// Does it want a preemption and is it safe to preempt? |
| 1219 |
gp := gFromSP(mp, c.SP()) |
| 1220 |
if gp != nil && wantAsyncPreempt(gp) { |
| 1221 |
if ok, resumePC := isAsyncSafePoint(gp, c.PC(), c.SP(), c.LR()); ok { |
| 1222 |
// Inject call to asyncPreempt |
| 1223 |
targetPC := abi.FuncPCABI0(asyncPreempt) |
| 1224 |
c.PushCall(targetPC, resumePC) |
| 1225 |
stdcall(_SetThreadContext, thread, uintptr(unsafe.Pointer(c))) |
| 1226 |
} |
| 1227 |
} |
| 1228 |
|
| 1229 |
atomic.Store(&mp.preemptExtLock, 0) |
| 1230 |
|
| 1231 |
// Acknowledge the preemption. |
| 1232 |
mp.preemptGen.Add(1) |
| 1233 |
|
| 1234 |
stdcall(_ResumeThread, thread) |
| 1235 |
stdcall(_CloseHandle, thread) |
| 1236 |
} |
| 1237 |
|
| 1238 |
// osPreemptExtEnter is called before entering external code that may |
| 1239 |
// call ExitProcess. |
| 1240 |
// |
| 1241 |
// This must be nosplit because it may be called from a syscall with |
| 1242 |
// untyped stack slots, so the stack must not be grown or scanned. |
| 1243 |
// |
| 1244 |
//go:nosplit |
| 1245 |
func osPreemptExtEnter(mp *m) { |
| 1246 |
for !atomic.Cas(&mp.preemptExtLock, 0, 1) { |
| 1247 |
// An asynchronous preemption is in progress. It's not |
| 1248 |
// safe to enter external code because it may call |
| 1249 |
// ExitProcess and deadlock with SuspendThread. |
| 1250 |
// Ideally we would do the preemption ourselves, but |
| 1251 |
// can't since there may be untyped syscall arguments |
| 1252 |
// on the stack. Instead, just wait and encourage the |
| 1253 |
// SuspendThread APC to run. The preemption should be |
| 1254 |
// done shortly. |
| 1255 |
osyield() |
| 1256 |
} |
| 1257 |
// Asynchronous preemption is now blocked. |
| 1258 |
} |
| 1259 |
|
| 1260 |
// osPreemptExtExit is called after returning from external code that |
| 1261 |
// may call ExitProcess. |
| 1262 |
// |
| 1263 |
// See osPreemptExtEnter for why this is nosplit. |
| 1264 |
// |
| 1265 |
//go:nosplit |
| 1266 |
func osPreemptExtExit(mp *m) { |
| 1267 |
atomic.Store(&mp.preemptExtLock, 0) |
| 1268 |
} |