38326380d99fdff5f2790577384a203b15c6d6d9
[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 #ifdef _WIN
17 #if __x86_64__
18 nvapi->lib = hc_dlopen ("nvapi64.dll");
19 #elif __x86__
20 nvapi->lib = hc_dlopen ("nvapi.dll");
21 #endif
22 #else
23 nvapi->lib = hc_dlopen ("nvapi.so", RTLD_NOW); // uhm yes, but .. yeah
24 #endif
25
26 if (!nvapi->lib)
27 {
28 //if (data.quiet == 0)
29 // log_info ("WARNING: load NVAPI library failed, proceed without NVAPI HWMon enabled.");
30
31 return (-1);
32 }
33
34 HC_LOAD_FUNC(nvapi, nvapi_QueryInterface, NVAPI_QUERYINTERFACE, NVAPI, 0)
35 HC_LOAD_ADDR(nvapi, NvAPI_Initialize, NVAPI_INITIALIZE, nvapi_QueryInterface, 0x0150E828, NVAPI, 0)
36 HC_LOAD_ADDR(nvapi, NvAPI_Unload, NVAPI_UNLOAD, nvapi_QueryInterface, 0xD22BDD7E, NVAPI, 0)
37 HC_LOAD_ADDR(nvapi, NvAPI_GetErrorMessage, NVAPI_GETERRORMESSAGE, nvapi_QueryInterface, 0x6C2D048C, NVAPI, 0)
38 HC_LOAD_ADDR(nvapi, NvAPI_EnumPhysicalGPUs, NVAPI_ENUMPHYSICALGPUS, nvapi_QueryInterface, 0xE5AC921F, NVAPI, 0)
39 HC_LOAD_ADDR(nvapi, NvAPI_GPU_GetPerfPoliciesInfo, NVAPI_GPU_GETPERFPOLICIESINFO, nvapi_QueryInterface, 0x409D9841, NVAPI, 0)
40 HC_LOAD_ADDR(nvapi, NvAPI_GPU_GetPerfPoliciesStatus, NVAPI_GPU_GETPERFPOLICIESSTATUS, nvapi_QueryInterface, 0x3D358A0C, NVAPI, 0)
41
42 return 0;
43 }
44
45 void nvapi_close (NVAPI_PTR *nvapi)
46 {
47 if (nvapi)
48 {
49 if (nvapi->lib)
50 hc_dlclose (nvapi->lib);
51
52 myfree (nvapi);
53 }
54 }
55
56 int hm_NvAPI_Initialize (NVAPI_PTR *nvapi)
57 {
58 if (!nvapi) return (-1);
59
60 NvAPI_Status NvAPI_rc = nvapi->NvAPI_Initialize ();
61
62 if (NvAPI_rc == NVAPI_LIBRARY_NOT_FOUND) NvAPI_rc = NVAPI_OK; // not a bug
63
64 if (NvAPI_rc != NVAPI_OK)
65 {
66 NvAPI_ShortString string = { 0 };
67
68 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
69
70 log_info ("WARN: %s %d %s\n", "NvAPI_Initialize()", NvAPI_rc, string);
71 }
72
73 return NvAPI_rc;
74 }
75
76 int hm_NvAPI_Unload (NVAPI_PTR *nvapi)
77 {
78 if (!nvapi) return (-1);
79
80 NvAPI_Status NvAPI_rc = nvapi->NvAPI_Unload ();
81
82 if (NvAPI_rc != NVAPI_OK)
83 {
84 NvAPI_ShortString string = { 0 };
85
86 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
87
88 log_info ("WARN: %s %d %s\n", "NvAPI_Unload()", NvAPI_rc, string);
89 }
90
91 return NvAPI_rc;
92 }
93
94 int hm_NvAPI_GetErrorMessage (NVAPI_PTR *nvapi, NvAPI_Status NvAPI_rc, NvAPI_ShortString string)
95 {
96 if (!nvapi) return (-1);
97
98 return nvapi->NvAPI_GetErrorMessage (NvAPI_rc, string);
99 }
100
101 int hm_NvAPI_EnumPhysicalGPUs (NVAPI_PTR *nvapi, NvPhysicalGpuHandle nvGPUHandle[NVAPI_MAX_PHYSICAL_GPUS], NvU32 *pGpuCount)
102 {
103 if (!nvapi) return (-1);
104
105 NvAPI_Status NvAPI_rc = nvapi->NvAPI_EnumPhysicalGPUs (nvGPUHandle, pGpuCount);
106
107 if (NvAPI_rc != NVAPI_OK)
108 {
109 NvAPI_ShortString string = { 0 };
110
111 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
112
113 log_info ("WARN: %s %d %s\n", "NvAPI_EnumPhysicalGPUs()", NvAPI_rc, string);
114 }
115
116 return NvAPI_rc;
117 }
118
119 int hm_NvAPI_GPU_GetPerfPoliciesInfo (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhysicalGpu, NV_GPU_PERF_POLICIES_INFO_PARAMS_V1 *perfPolicies_info)
120 {
121 if (!nvapi) return (-1);
122
123 NvAPI_Status NvAPI_rc = nvapi->NvAPI_GPU_GetPerfPoliciesInfo (hPhysicalGpu, perfPolicies_info);
124
125 if (NvAPI_rc != NVAPI_OK)
126 {
127 NvAPI_ShortString string = { 0 };
128
129 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
130
131 log_info ("WARN: %s %d %s\n", "NvAPI_GPU_GetPerfPoliciesInfo()", NvAPI_rc, string);
132 }
133
134 return NvAPI_rc;
135 }
136
137 int hm_NvAPI_GPU_GetPerfPoliciesStatus (NVAPI_PTR *nvapi, NvPhysicalGpuHandle hPhysicalGpu, NV_GPU_PERF_POLICIES_STATUS_PARAMS_V1 *perfPolicies_status)
138 {
139 if (!nvapi) return (-1);
140
141 NvAPI_Status NvAPI_rc = nvapi->NvAPI_GPU_GetPerfPoliciesStatus (hPhysicalGpu, perfPolicies_status);
142
143 if (NvAPI_rc != NVAPI_OK)
144 {
145 NvAPI_ShortString string = { 0 };
146
147 hm_NvAPI_GetErrorMessage (nvapi, NvAPI_rc, string);
148
149 log_info ("WARN: %s %d %s\n", "NvAPI_GPU_GetPerfPoliciesStatus()", NvAPI_rc, string);
150 }
151
152 return NvAPI_rc;
153 }
154
155 #ifdef __MINGW64__
156
157 void __security_check_cookie (uintptr_t _StackCookie)
158 {
159 (void) _StackCookie;
160 }
161
162 void __GSHandlerCheck ()
163 {
164 }
165
166 #endif