-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathkernel32.go
202 lines (168 loc) · 6.63 KB
/
kernel32.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
// https://github.com/prometheus-community/windows_exporter/blob/74eac8f29b8083b9e6a4832d739748739e4e3fe0/headers/sysinfoapi/sysinfoapi.go#L44
import (
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
// wProcessorArchitecture is a wrapper for the union found in LP_SYSTEM_INFO
// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info
type wProcessorArchitecture struct {
WProcessorArchitecture uint16
WReserved uint16
}
// ProcessorArchitecture is an idiomatic wrapper for wProcessorArchitecture
type ProcessorArchitecture uint16
// Idiomatic values for wProcessorArchitecture
const (
AMD64 ProcessorArchitecture = 9
ARM ProcessorArchitecture = 5
ARM64 ProcessorArchitecture = 12
IA64 ProcessorArchitecture = 6
INTEL ProcessorArchitecture = 0
UNKNOWN ProcessorArchitecture = 0xffff
)
// SystemInfo is an idiomatic wrapper for LpSystemInfo
type SystemInfo struct {
Arch ProcessorArchitecture
PageSize uint32
MinimumApplicationAddress uintptr
MaximumApplicationAddress uintptr
ActiveProcessorMask uint
NumberOfProcessors uint32
ProcessorType uint32
AllocationGranularity uint32
ProcessorLevel uint16
ProcessorRevision uint16
}
// LpSystemInfo is a wrapper for LPSYSTEM_INFO
// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info
type lpSystemInfo struct {
Arch wProcessorArchitecture
DwPageSize uint32
LpMinimumApplicationAddress uintptr
LpMaximumApplicationAddress uintptr
DwActiveProcessorMask uint
DwNumberOfProcessors uint32
DwProcessorType uint32
DwAllocationGranularity uint32
WProcessorLevel uint16
WProcessorRevision uint16
}
var (
// Library
libKernel32 = windows.NewLazySystemDLL("kernel32.dll")
// Functions
getSystemCpuSetInformation = libKernel32.NewProc("GetSystemCpuSetInformation")
getSystemInfo = libKernel32.NewProc("GetSystemInfo")
)
// GetSystemInfo is an idiomatic wrapper for the GetSystemInfo function from sysinfoapi
// https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsysteminfo
func GetSystemInfo() SystemInfo {
var info lpSystemInfo
getSystemInfo.Call(uintptr(unsafe.Pointer(&info)))
return SystemInfo{
Arch: ProcessorArchitecture(info.Arch.WProcessorArchitecture),
PageSize: info.DwPageSize,
MinimumApplicationAddress: info.LpMinimumApplicationAddress,
MaximumApplicationAddress: info.LpMinimumApplicationAddress,
ActiveProcessorMask: info.DwActiveProcessorMask,
NumberOfProcessors: info.DwNumberOfProcessors,
ProcessorType: info.DwProcessorType,
AllocationGranularity: info.DwAllocationGranularity,
ProcessorLevel: info.WProcessorLevel,
ProcessorRevision: info.WProcessorRevision,
}
}
// The SystemInformationClass constants have been derived from the SYSTEM_INFORMATION_CLASS enum definition.
const (
SystemAllowedCpuSetsInformation = 0xA8
SystemCpuSetInformation = 0xAF
ProcessDefaultCpuSetsInformation = 0x42
// https://learn.microsoft.com/en-us/windows/win32/procthread/process-security-and-access-rights
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
PROCESS_SET_LIMITED_INFORMATION = 0x2000
)
const (
SYSTEM_CPU_SET_INFORMATION_PARKED uint32 = 0x1
SYSTEM_CPU_SET_INFORMATION_ALLOCATED uint32 = 0x2
SYSTEM_CPU_SET_INFORMATION_ALLOCATED_TO_TARGET_PROCESS uint32 = 0x4
SYSTEM_CPU_SET_INFORMATION_REALTIME uint32 = 0x8
)
// https://github.com/zzl/go-win32api/blob/d9f481c2ab64b5df06e8d62e1bac33dd9141de43/win32/System.SystemInformation.go
type SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous1_Anonymous struct {
Bitfield_ byte
}
type SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous1 struct {
SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous1_Anonymous
}
func (t *SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous1) AllFlags() *byte {
return (*byte)(unsafe.Pointer(t))
}
func (t *SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous1) AllFlagsVal() byte {
return *(*byte)(unsafe.Pointer(t))
}
func (t *SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous1) Anonymous() *SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous1_Anonymous {
return (*SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous1_Anonymous)(unsafe.Pointer(t))
}
func (t *SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous1) AnonymousVal() SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous1_Anonymous {
return *(*SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous1_Anonymous)(unsafe.Pointer(t))
}
type SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous2 struct {
Data [1]uint32
}
func (t *SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous2) Reserved() *uint32 {
return (*uint32)(unsafe.Pointer(t))
}
func (t *SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous2) ReservedVal() uint32 {
return *(*uint32)(unsafe.Pointer(t))
}
func (t *SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous2) SchedulingClass() *byte {
return (*byte)(unsafe.Pointer(t))
}
func (t *SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous2) SchedulingClassVal() byte {
return *(*byte)(unsafe.Pointer(t))
}
type SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet struct {
Id uint32
Group uint16
LogicalProcessorIndex byte
CoreIndex byte
LastLevelCacheIndex byte
NumaNodeIndex byte
EfficiencyClass byte
SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous1
SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet_Anonymous2
AllocationTag uint64
}
type SYSTEM_CPU_SET_INFORMATION_Anonymous struct {
Data [3]uint64
}
func (t *SYSTEM_CPU_SET_INFORMATION_Anonymous) CpuSet() *SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet {
return (*SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet)(unsafe.Pointer(t))
}
func (t *SYSTEM_CPU_SET_INFORMATION_Anonymous) CpuSetVal() SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet {
return *(*SYSTEM_CPU_SET_INFORMATION_Anonymous_CpuSet)(unsafe.Pointer(t))
}
type CPU_SET_INFORMATION_TYPE int32
type SYSTEM_CPU_SET_INFORMATION struct {
Size uint32
Type CPU_SET_INFORMATION_TYPE
SYSTEM_CPU_SET_INFORMATION_Anonymous
}
func GetSystemCpuSetInformation(
information *SYSTEM_CPU_SET_INFORMATION,
bufferLength uint32,
returnedLength *uint32,
process uintptr,
flags uint32,
) uint32 {
r1, _, _ := syscall.SyscallN(getSystemCpuSetInformation.Addr(),
uintptr(unsafe.Pointer(information)),
uintptr(bufferLength),
uintptr(unsafe.Pointer(returnedLength)),
process,
uintptr(flags),
)
return uint32(r1)
}