Added NvAPI support for querying current engine clock and current memory clock
[hashcat.git] / src / ext_nvapi.c
1 /**
2 * Authors.....: Jens Steube <jens.steube@gmail.com>
3 * Gabriele Gristina <matrix@hashcat.net>
4 *
5 * License.....: MIT
6 */
7
8 #include <ext_nvapi.h>
9
10 int nvapi_init (NVAPI_PTR *nvapi)
11 {
12 if (!nvapi) return (-1);
13
14 memset (nvapi, 0, sizeof (NVAPI_PTR));
15
16 #if __x86_64__
17 nvapi->lib = hc_dlopen ("nvapi64.dll");
18 #elif __x86__
19 nvapi->lib = hc_dlopen ("nvapi.dll");
20 #endif
21
22 if (!nvapi->lib)
23 {
24 //if (data.quiet == 0)
25 // log_info ("WARNING: load NVAPI library failed, proceed without NVAPI HWMon enabled.");
26
27 return (-1);
28 }
29
30 HC_LOAD_FUNC(nvapi, nvapi_QueryInterface, NVAPI_QUERYINTERFACE, NVAPI, 0)
31 HC_LOAD_ADDR(nvapi, NvAPI_Initialize, NVAPI_INITIALIZE, nvapi_QueryInterface, 0x0150E828, NVAPI, 0)
32 HC_LOAD_ADDR(nvapi, NvAPI_Unload, NVAPI_UNLOAD, nvapi_QueryInterface, 0xD22BDD7E, NVAPI, 0)
33 HC_LOAD_ADDR(nvapi, NvAPI_GetErrorMessage, NVAPI_GETERRORMESSAGE, nvapi_QueryInterface, 0x6C2D048C, NVAPI, 0)
34 HC_LOAD_ADDR(nvapi, NvAPI_GPU_GetDynamicPstatesInfoEx, NVAPI_GPU_GETDYNAMICPSTATESINFOEX, nvapi_QueryInterface, 0x60DED2ED, NVAPI, 0)
35 HC_LOAD_ADDR(nvapi, NvAPI_EnumPhysicalGPUs, NVAPI_ENUMPHYSICALGPUS, nvapi_QueryInterface, 0xE5AC921F, NVAPI, 0)
36 HC_LOAD_ADDR(nvapi, NvAPI_GPU_GetThermalSettings, NVAPI_GPU_GETTHERMALSETTINGS, nvapi_QueryInterface, 0xE3640A56, NVAPI, 0)
37 HC_LOAD_ADDR(nvapi, NvAPI_GPU_GetTachReading, NVAPI_GPU_GETTACHREADING, nvapi_QueryInterface, 0x5F608315, NVAPI, 0)
38 HC_LOAD_ADDR(nvapi, NvAPI_GPU_GetCoolerSettings, NVAPI_GPU_GETCOOLERSETTINGS, nvapi_QueryInterface, 0xDA141340, NVAPI, 0)
39 HC_LOAD_ADDR(nvapi, NvAPI_GPU_GetAllClockFrequencies, NVAPI_GPU_GETALLCLOCKFREQUENCIES, nvapi_QueryInterface, 0xDCB616C3, NVAPI, 0)
40
41 return 0;
42 }
43
44 void nvapi_close (NVAPI_PTR *nvapi)
45 {
46 if (nvapi)
47 {
48 if (nvapi->lib)
49 hc_dlclose (nvapi->lib);
50
51 myfree (nvapi);
52 }
53 }
54
55 int hm_NvAPI_Initialize (NVAPI_PTR *nvapi)
56 {
57 if (!nvapi) return (-1);
58
59 NvAPI_Status NvAPI_rc = nvapi->NvAPI_Initialize ();
60
61 if (NvAPI_rc == NVAPI_LIBRARY_NOT_FOUND) NvAPI_rc = NVAPI_OK; // not a bug
62
63 if (NvAPI_rc != NVAPI_OK)
64 {
65 NvAPI_ShortString string = { 0 };
66
67 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
68
69 log_info ("WARN: %s %d %s\n", "NvAPI_Initialize()", NvAPI_rc, string);
70 }
71
72 return NvAPI_rc;
73 }
74
75 int hm_NvAPI_Unload (NVAPI_PTR *nvapi)
76 {
77 if (!nvapi) return (-1);
78
79 NvAPI_Status NvAPI_rc = nvapi->NvAPI_Unload ();
80
81 if (NvAPI_rc != NVAPI_OK)
82 {
83 NvAPI_ShortString string = { 0 };
84
85 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
86
87 log_info ("WARN: %s %d %s\n", "NvAPI_Unload()", NvAPI_rc, string);
88 }
89
90 return NvAPI_rc;
91 }
92
93 int hm_NvAPI_GetErrorMessage (NVAPI_PTR *nvapi, NvAPI_Status NvAPI_rc, NvAPI_ShortString string)
94 {
95 if (!nvapi) return (-1);
96
97 return nvapi->NvAPI_GetErrorMessage (NvAPI_rc, string);
98 }
99
100 int hm_NvAPI_EnumPhysicalGPUs (NVAPI_PTR *nvapi, NvPhysicalGpuHandle nvGPUHandle[NVAPI_MAX_PHYSICAL_GPUS], NvU32 *pGpuCount)
101 {
102 if (!nvapi) return (-1);
103
104 NvAPI_Status NvAPI_rc = nvapi->NvAPI_EnumPhysicalGPUs (nvGPUHandle, pGpuCount);
105
106 if (NvAPI_rc != NVAPI_OK)
107 {
108 NvAPI_ShortString string = { 0 };
109
110 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
111
112 log_info ("WARN: %s %d %s\n", "NvAPI_EnumPhysicalGPUs()", NvAPI_rc, string);
113 }
114
115 return NvAPI_rc;
116 }
117
118 int hm_NvAPI_GPU_GetThermalSettings (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhysicalGpu, NvU32 sensorIndex, NV_GPU_THERMAL_SETTINGS *pThermalSettings)
119 {
120 if (!nvapi) return (-1);
121
122 NvAPI_Status NvAPI_rc = nvapi->NvAPI_GPU_GetThermalSettings (hPhysicalGpu, sensorIndex, pThermalSettings);
123
124 if (NvAPI_rc != NVAPI_OK)
125 {
126 NvAPI_ShortString string = { 0 };
127
128 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
129
130 log_info ("WARN: %s %d %s\n", "NvAPI_GPU_GetThermalSettings()", NvAPI_rc, string);
131 }
132
133 return NvAPI_rc;
134 }
135
136 int hm_NvAPI_GPU_GetTachReading (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhysicalGPU, NvU32 *pValue)
137 {
138 if (!nvapi) return (-1);
139
140 NvAPI_Status NvAPI_rc = nvapi->NvAPI_GPU_GetTachReading (hPhysicalGPU, pValue);
141
142 if (NvAPI_rc != NVAPI_OK)
143 {
144 NvAPI_ShortString string = { 0 };
145
146 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
147
148 log_info ("WARN: %s %d %s\n", "NvAPI_GPU_GetTachReading()", NvAPI_rc, string);
149 }
150
151 return NvAPI_rc;
152 }
153
154 int hm_NvAPI_GPU_GetCoolerSettings (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhysicalGpu, NvU32 coolerIndex, NV_GPU_COOLER_SETTINGS *pCoolerSettings)
155 {
156 if (!nvapi) return (-1);
157
158 NvAPI_Status NvAPI_rc = nvapi->NvAPI_GPU_GetCoolerSettings (hPhysicalGpu, coolerIndex, pCoolerSettings);
159
160 if (NvAPI_rc != NVAPI_OK)
161 {
162 NvAPI_ShortString string = { 0 };
163
164 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
165
166 log_info ("WARN: %s %d %s\n", "NvAPI_GPU_GetCoolerSettings()", NvAPI_rc, string);
167 }
168
169 return NvAPI_rc;
170 }
171
172 int hm_NvAPI_GPU_GetDynamicPstatesInfoEx (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhysicalGpu, NV_GPU_DYNAMIC_PSTATES_INFO_EX *pDynamicPstatesInfoEx)
173 {
174 if (!nvapi) return (-1);
175
176 NvAPI_Status NvAPI_rc = nvapi->NvAPI_GPU_GetDynamicPstatesInfoEx (hPhysicalGpu, pDynamicPstatesInfoEx);
177
178 if (NvAPI_rc != NVAPI_OK)
179 {
180 NvAPI_ShortString string = { 0 };
181
182 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
183
184 log_info ("WARN: %s %d %s\n", "NvAPI_GPU_GetDynamicPstatesInfoEx()", NvAPI_rc, string);
185 }
186
187 return NvAPI_rc;
188 }
189
190 int hm_NvAPI_GPU_GetAllClockFrequencies (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhysicalGpu, NV_GPU_CLOCK_FREQUENCIES *pClkFreqs)
191 {
192 if (!nvapi) return (-1);
193
194 NvAPI_Status NvAPI_rc = nvapi->NvAPI_GPU_GetAllClockFrequencies (hPhysicalGpu, pClkFreqs);
195
196 if (NvAPI_rc != NVAPI_OK)
197 {
198 NvAPI_ShortString string = { 0 };
199
200 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
201
202 log_info ("WARN: %s %d %s\n", "NvAPI_GPU_GetAllClockFrequencies()", NvAPI_rc, string);
203 }
204
205 return NvAPI_rc;
206 }
207
208 #ifdef __MINGW64__
209
210 void __security_check_cookie (uintptr_t _StackCookie)
211 {
212 (void) _StackCookie;
213 }
214
215 void __GSHandlerCheck ()
216 {
217 }
218
219 #endif