Source file src/runtime/os_windows.go

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

View as plain text