From bc4ce4cbeb84ec7df8b23645068b282ed7e3bebd Mon Sep 17 00:00:00 2001 From: Gabriele Gristina Date: Sat, 12 Jun 2021 20:13:31 +0200 Subject: [PATCH] Add support for CPU/GPU device temperature and fanspeed using iokit (Apple) --- docs/changes.txt | 1 + include/ext_iokit.h | 125 ++++++++++++++++++ include/types.h | 8 ++ src/Makefile | 4 +- src/backend.c | 11 ++ src/ext_iokit.c | 307 ++++++++++++++++++++++++++++++++++++++++++++ src/hwmon.c | 175 ++++++++++++++++++++++--- src/monitor.c | 14 ++ 8 files changed, 625 insertions(+), 20 deletions(-) create mode 100644 include/ext_iokit.h create mode 100644 src/ext_iokit.c diff --git a/docs/changes.txt b/docs/changes.txt index 074e83110..ad2a0fbae 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -46,6 +46,7 @@ - CUDA Backend: Do not warn about missing CUDA SDK installation if --stdout is used - Folder Management: Add support for XDG Base Directory specification if hashcat was installed using make install - Hardware Monitor: Add support for GPU device utilization readings from sysfs (AMD on Linux) +- Hardware Monitor: Add support for CPU/GPU device temperature and fanspeed using iokit (Apple) - OpenCL Backend: Use CL_DEVICE_BOARD_NAME_AMD instead of CL_DEVICE_NAME for device name in case OpenCL runtime supports this query - Performance Monitor: Add -S as a user suggestion to improve cracking performance in specific attack configurations - RAR3-p (Compressed): Fix workaround in unrar library in AES constant table generation to enable multi-threading support diff --git a/include/ext_iokit.h b/include/ext_iokit.h new file mode 100644 index 000000000..b6cd9c5ea --- /dev/null +++ b/include/ext_iokit.h @@ -0,0 +1,125 @@ +/** + * Author......: See docs/credits.txt + * License.....: MIT + */ + +#ifndef _EXT_IOKIT_H +#define _EXT_IOKIT_H + +#ifdef __APPLE__ +#include + +// Apple SMC Keys +#define HM_IOKIT_SMC_SENSOR_GRAPHICS_HOT "SGHT" +#define HM_IOKIT_SMC_CPU_PROXIMITY "TC0P" +#define HM_IOKIT_SMC_GPU_PROXIMITY "TG0P" +#define HM_IOKIT_SMC_PECI_GPU "TCGC" + +#define KERNEL_INDEX_SMC 2 + +#define DATATYPE_FPE2 "fpe2" +#define DATATYPE_UINT8 "ui8 " +#define DATATYPE_UINT16 "ui16" +#define DATATYPE_UINT32 "ui32" +#define DATATYPE_SP78 "sp78" + +typedef enum +{ + SMC_CMD_READ_BYTES = 5, + SMC_CMD_WRITE_BYTES = 6, + SMC_CMD_READ_INDEX = 8, + SMC_CMD_READ_KEYINFO = 9, + SMC_CMD_READ_PLIMIT = 11, + SMC_CMD_READ_VERS = 12 + +} SMCCommands_t; + +typedef struct +{ + char major; + char minor; + char build; + char reserved[1]; + UInt16 release; + +} SMCKeyData_vers_t; + +typedef struct +{ + UInt16 version; + UInt16 length; + UInt32 cpuPLimit; + UInt32 gpuPLimit; + UInt32 memPLimit; + +} SMCKeyData_pLimitData_t; + +typedef struct +{ + UInt32 dataSize; + UInt32 dataType; + + char dataAttributes; + +} SMCKeyData_keyInfo_t; + +typedef char SMCBytes_t[32]; + +typedef struct +{ + UInt32 key; + + SMCKeyData_vers_t vers; + SMCKeyData_pLimitData_t pLimitData; + SMCKeyData_keyInfo_t keyInfo; + + char result; + char status; + char data8; + + UInt32 data32; + SMCBytes_t bytes; + +} SMCKeyData_t; + +typedef char UInt32Char_t[5]; + +typedef struct +{ + UInt32Char_t key; + UInt32 dataSize; + UInt32Char_t dataType; + SMCBytes_t bytes; + +} SMCVal_t; + +#endif // __APPLE__ + +typedef int HM_ADAPTER_IOKIT; + +typedef void *IOKIT_LIB; + +typedef struct hm_iokit_lib +{ + #if defined(__APPLE__) + io_connect_t conn; + #endif // __APPLE__ + +} hm_iokit_lib_t; + +typedef hm_iokit_lib_t IOKIT_PTR; + +UInt32 hm_IOKIT_strtoul (char *str, int size, int base); +void hm_IOKIT_ultostr (char *str, UInt32 val); +kern_return_t hm_IOKIT_SMCOpen (void *hashcat_ctx, io_connect_t *conn); +kern_return_t hm_IOKIT_SMCClose (io_connect_t conn); +kern_return_t hm_IOKIT_SMCCall (int index, SMCKeyData_t *inData, SMCKeyData_t *outData, io_connect_t conn); +kern_return_t hm_IOKIT_SMCReadKey (UInt32Char_t key, SMCVal_t *val, io_connect_t conn); +int hm_IOKIT_SMCGetSensorGraphicHot (void *hashcat_ctx); +int hm_IOKIT_SMCGetTemperature (void *hashcat_ctx, char *key, double *temp); +bool hm_IOKIT_SMCGetFanRPM (char *key, io_connect_t conn, float *ret); +int hm_IOKIT_get_fan_speed_current (void *hashcat_ctx, int *fan_speed); +bool iokit_init (void *hashcat_ctx); +bool iokit_close (void *hashcat_ctx); + +#endif // _EXT_IOKIT_H diff --git a/include/types.h b/include/types.h index 2b240ba59..d85efcb43 100644 --- a/include/types.h +++ b/include/types.h @@ -611,7 +611,11 @@ typedef enum user_options_defaults DEBUG_MODE = 0, FORCE = false, HWMON_DISABLE = false, + #if defined (__APPLE__) + HWMON_TEMP_ABORT = 100, + #else HWMON_TEMP_ABORT = 90, + #endif HASH_INFO = false, HASH_MODE = 0, HCCAPX_MESSAGE_PAIR = 0, @@ -1589,6 +1593,7 @@ typedef struct backend_ctx bool need_nvml; bool need_nvapi; bool need_sysfs; + bool need_iokit; int comptime; @@ -1632,6 +1637,7 @@ typedef enum kernel_workload #include "ext_nvapi.h" #include "ext_nvml.h" #include "ext_sysfs.h" +#include "ext_iokit.h" typedef struct hm_attrs { @@ -1639,6 +1645,7 @@ typedef struct hm_attrs HM_ADAPTER_NVML nvml; HM_ADAPTER_NVAPI nvapi; HM_ADAPTER_SYSFS sysfs; + HM_ADAPTER_IOKIT iokit; int od_version; @@ -1663,6 +1670,7 @@ typedef struct hwmon_ctx void *hm_nvml; void *hm_nvapi; void *hm_sysfs; + void *hm_iokit; hm_attrs_t *hm_device; diff --git a/src/Makefile b/src/Makefile index 77029e3f9..33961ecae 100644 --- a/src/Makefile +++ b/src/Makefile @@ -315,6 +315,7 @@ endif # FreeBSD ifeq ($(UNAME),Darwin) export MACOSX_DEPLOYMENT_TARGET=10.9 CFLAGS_NATIVE := $(CFLAGS) +CFLAGS_NATIVE += -DWITH_HWMON ifeq ($(shell test $(DARWIN_VERSION) -le 15; echo $$?), 0) CFLAGS_NATIVE += -DMISSING_CLOCK_GETTIME @@ -322,6 +323,7 @@ endif LFLAGS_NATIVE := $(LFLAGS) LFLAGS_NATIVE += -framework OpenCL +LFLAGS_NATIVE += -framework IOKit LFLAGS_NATIVE += -lpthread LFLAGS_NATIVE += -liconv endif # Darwin @@ -364,7 +366,7 @@ EMU_OBJS_ALL += emu_inc_truecrypt_crc32 emu_inc_truecrypt_keyfile emu EMU_OBJS_ALL += emu_inc_hash_md4 emu_inc_hash_md5 emu_inc_hash_ripemd160 emu_inc_hash_sha1 emu_inc_hash_sha256 emu_inc_hash_sha384 emu_inc_hash_sha512 emu_inc_hash_streebog256 emu_inc_hash_streebog512 emu_inc_ecc_secp256k1 EMU_OBJS_ALL += emu_inc_cipher_aes emu_inc_cipher_camellia emu_inc_cipher_des emu_inc_cipher_kuznyechik emu_inc_cipher_serpent emu_inc_cipher_twofish -OBJS_ALL := affinity autotune backend benchmark bitmap bitops combinator common convert cpt cpu_crc32 debugfile dictstat dispatch dynloader event ext_ADL ext_cuda ext_nvapi ext_nvml ext_nvrtc ext_OpenCL ext_sysfs ext_lzma filehandling folder hashcat hashes hlfmt hwmon induct interface keyboard_layout locking logfile loopback memory monitor mpsp outfile_check outfile pidfile potfile restore rp rp_cpu selftest slow_candidates shared status stdout straight terminal thread timer tuningdb usage user_options wordlist $(EMU_OBJS_ALL) +OBJS_ALL := affinity autotune backend benchmark bitmap bitops combinator common convert cpt cpu_crc32 debugfile dictstat dispatch dynloader event ext_ADL ext_cuda ext_nvapi ext_nvml ext_nvrtc ext_OpenCL ext_sysfs ext_iokit ext_lzma filehandling folder hashcat hashes hlfmt hwmon induct interface keyboard_layout locking logfile loopback memory monitor mpsp outfile_check outfile pidfile potfile restore rp rp_cpu selftest slow_candidates shared status stdout straight terminal thread timer tuningdb usage user_options wordlist $(EMU_OBJS_ALL) ifeq ($(ENABLE_BRAIN),1) OBJS_ALL += brain diff --git a/src/backend.c b/src/backend.c index d868645b4..b58d8d433 100644 --- a/src/backend.c +++ b/src/backend.c @@ -5653,6 +5653,7 @@ int backend_ctx_devices_init (hashcat_ctx_t *hashcat_ctx, const int comptime) bool need_nvml = false; bool need_nvapi = false; bool need_sysfs = false; + bool need_iokit = false; int backend_devices_idx = 0; @@ -5901,10 +5902,12 @@ int backend_ctx_devices_init (hashcat_ctx_t *hashcat_ctx, const int comptime) device_param->skipped = true; } + #if !defined (__APPLE__) if ((backend_ctx->opencl_device_types_filter & CL_DEVICE_TYPE_GPU) == 0) { device_param->skipped = true; } + #endif if ((device_param->opencl_platform_vendor_id == VENDOR_ID_NV) && (device_param->opencl_device_vendor_id == VENDOR_ID_NV)) { @@ -6659,6 +6662,13 @@ int backend_ctx_devices_init (hashcat_ctx_t *hashcat_ctx, const int comptime) // vendor specific + #if defined (__APPLE__) + if (device_param->opencl_platform_vendor_id == VENDOR_ID_APPLE) + { + if (device_param->skipped == false) need_iokit = true; + } + #endif + if (device_param->opencl_device_type & CL_DEVICE_TYPE_GPU) { if ((device_param->opencl_platform_vendor_id == VENDOR_ID_AMD) && (device_param->opencl_device_vendor_id == VENDOR_ID_AMD)) @@ -7333,6 +7343,7 @@ int backend_ctx_devices_init (hashcat_ctx_t *hashcat_ctx, const int comptime) backend_ctx->need_nvml = need_nvml; backend_ctx->need_nvapi = need_nvapi; backend_ctx->need_sysfs = need_sysfs; + backend_ctx->need_iokit = need_iokit; backend_ctx->comptime = comptime; diff --git a/src/ext_iokit.c b/src/ext_iokit.c new file mode 100644 index 000000000..05b297ba3 --- /dev/null +++ b/src/ext_iokit.c @@ -0,0 +1,307 @@ +/** + * Author......: See docs/credits.txt + * License.....: MIT + */ + +#include "common.h" +#include "types.h" +#include "memory.h" +#include "shared.h" +#include "event.h" +#include "ext_iokit.h" + +#if defined (__APPLE__) +#include + +UInt32 hm_IOKIT_strtoul (char *str, int size, int base) +{ + int i; + + UInt32 total = 0; + + for (i = 0; i < size; i++) + { + if (base == 16) + { + total += str[i] << (size - 1 - i) * 8; + } + else + { + total += (unsigned char)(str[i] << (size - 1 - i) * 8); + } + } + return total; +} + +void hm_IOKIT_ultostr (char *str, UInt32 val) +{ + str[0] = '\0'; + + sprintf (str, "%c%c%c%c", (unsigned int)(val >> 24), (unsigned int)(val >> 16), (unsigned int)(val >> 8), (unsigned int)(val)); +} + +kern_return_t hm_IOKIT_SMCOpen (void *hashcat_ctx, io_connect_t *conn) +{ + kern_return_t result; + io_iterator_t iterator; + io_object_t device; + + CFMutableDictionaryRef matchingDictionary = IOServiceMatching ("AppleSMC"); + + result = IOServiceGetMatchingServices (kIOMasterPortDefault, matchingDictionary, &iterator); + + if (result != kIOReturnSuccess) + { + event_log_error (hashcat_ctx, "IOServiceGetMatchingServices(): %08x", result); + + return 1; + } + + device = IOIteratorNext (iterator); + + IOObjectRelease (iterator); + + if (device == 0) + { + event_log_error (hashcat_ctx, "hm_IOKIT_SMCOpen(): no SMC found."); + + return 1; + } + + result = IOServiceOpen (device, mach_task_self(), 0, conn); + + IOObjectRelease (device); + + if (result != kIOReturnSuccess) + { + event_log_error (hashcat_ctx, "IOServiceOpen(): %08x", result); + + return 1; + } + + return kIOReturnSuccess; +} + +kern_return_t hm_IOKIT_SMCClose (io_connect_t conn) +{ + return IOServiceClose (conn); +} + +kern_return_t hm_IOKIT_SMCCall (int index, SMCKeyData_t *inData, SMCKeyData_t *outData, io_connect_t conn) +{ + size_t inDataSize = sizeof (SMCKeyData_t); + size_t outDataSize = sizeof (SMCKeyData_t); + + #if MAC_OS_X_VERSION_10_5 + return IOConnectCallStructMethod (conn, index, inData, inDataSize, outData, &outDataSize); + #else + return IOConnectMethodStructureIStructureO (conn, index, inDataSize, &outDataSize, inData, outData); + #endif +} + +kern_return_t hm_IOKIT_SMCReadKey (UInt32Char_t key, SMCVal_t *val, io_connect_t conn) +{ + SMCKeyData_t inData; + SMCKeyData_t outData; + + memset (&inData, 0, sizeof (SMCKeyData_t)); + memset (&outData, 0, sizeof (SMCKeyData_t)); + memset (val, 0, sizeof (SMCVal_t)); + + inData.key = hm_IOKIT_strtoul (key, 4, 16); + + inData.data8 = SMC_CMD_READ_KEYINFO; + + if (hm_IOKIT_SMCCall (KERNEL_INDEX_SMC, &inData, &outData, conn) != kIOReturnSuccess) return 1; + + if (hm_IOKIT_SMCCall (KERNEL_INDEX_SMC, &inData, &outData, conn) != kIOReturnSuccess) return 1; + + val->dataSize = outData.keyInfo.dataSize; + + hm_IOKIT_ultostr (val->dataType, outData.keyInfo.dataType); + + inData.keyInfo.dataSize = val->dataSize; + + inData.data8 = SMC_CMD_READ_BYTES; + + if (hm_IOKIT_SMCCall (KERNEL_INDEX_SMC, &inData, &outData, conn) != kIOReturnSuccess) return 1; + + memcpy(val->bytes, outData.bytes, sizeof(outData.bytes)); + + return kIOReturnSuccess; +} + +int hm_IOKIT_SMCGetSensorGraphicHot (void *hashcat_ctx) +{ + hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx; + + IOKIT_PTR *iokit = hwmon_ctx->hm_iokit; + + SMCVal_t val; + + memset (&val, 0, sizeof (SMCVal_t)); + + int alarm = -1; + + if (hm_IOKIT_SMCReadKey (HM_IOKIT_SMC_SENSOR_GRAPHICS_HOT, &val, iokit->conn) == kIOReturnSuccess) + { + if (val.dataSize > 0) + { + if (strcmp(val.dataType, DATATYPE_UINT8) == 0) + { + alarm = hm_IOKIT_strtoul ((char *)val.bytes, val.dataSize, 10); + } + } + + return alarm; + } + + return -1; +} + +int hm_IOKIT_SMCGetTemperature (void *hashcat_ctx, char *key, double *temp) +{ + hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx; + + IOKIT_PTR *iokit = hwmon_ctx->hm_iokit; + + SMCVal_t val; + + memset (&val, 0, sizeof (SMCVal_t)); + + if (hm_IOKIT_SMCReadKey (key, &val, iokit->conn) == kIOReturnSuccess) + { + if (val.dataSize > 0) + { + if (strcmp(val.dataType, DATATYPE_SP78) == 0) + { + // convert sp78 value to temperature + int intValue = val.bytes[0] * 256 + (unsigned char)val.bytes[1]; + + *temp = (intValue / 256.0); + + return 1; + } + } + } + + // read failed + + *temp = 0.0; + + return -1; +} + +bool hm_IOKIT_SMCGetFanRPM (char *key, io_connect_t conn, float *ret) +{ + SMCVal_t val; + + memset (&val, 0, sizeof (SMCVal_t)); + + if (hm_IOKIT_SMCReadKey (key, &val, conn) == kIOReturnSuccess) + { + if (val.dataSize > 0) + { + if (strcmp(val.dataType, DATATYPE_FPE2) == 0) + { + // convert fpe2 value to RPM + *ret = ntohs (*(UInt16*)val.bytes) / 4.0; + + return true; + } + } + } + + // read failed + *ret = -1.f; + + return false; +} + +int hm_IOKIT_get_fan_speed_current (void *hashcat_ctx, int *fan_speed) +{ + *fan_speed = 0; + + hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx; + + IOKIT_PTR *iokit = hwmon_ctx->hm_iokit; + + SMCVal_t val; + + UInt32Char_t key; + + int i, totalFans = 0; + + memset (&val, 0, sizeof (SMCVal_t)); + + if (hm_IOKIT_SMCReadKey ("FNum", &val, iokit->conn) == kIOReturnSuccess) + { + totalFans = hm_IOKIT_strtoul ((char *)val.bytes, val.dataSize, 10); + + for (i = 0; i < totalFans; i++) + { + float actual_speed = 0.0f; + float minimum_speed = 0.0f; + float maximum_speed = 0.0f; + float rpm = 0.0f; + float pct = 0.0f; + + memset (&key, 0, sizeof (UInt32Char_t)); + sprintf (key, "F%dAc", i); + hm_IOKIT_SMCGetFanRPM (key, iokit->conn, &actual_speed); + if (actual_speed < 0.f) continue; + + memset (&key, 0, sizeof (UInt32Char_t)); + sprintf(key, "F%dMn", i); + hm_IOKIT_SMCGetFanRPM (key, iokit->conn, &minimum_speed); + if (minimum_speed < 0.f) continue; + + memset (&key, 0, sizeof (UInt32Char_t)); + sprintf (key, "F%dMx", i); + hm_IOKIT_SMCGetFanRPM (key, iokit->conn, &maximum_speed); + if (maximum_speed < 0.f) continue; + + rpm = actual_speed - minimum_speed; + if (rpm < 0.f) rpm = 0.f; + + pct = rpm / (maximum_speed - minimum_speed); + pct *= 100.f; + + *fan_speed = pct; + + break; + } + } + + return 1; +} + +bool iokit_init (void *hashcat_ctx) +{ + hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx; + + IOKIT_PTR *iokit = hwmon_ctx->hm_iokit; + + memset (iokit, 0, sizeof (IOKIT_PTR)); + + if (hm_IOKIT_SMCOpen (hashcat_ctx, &iokit->conn) == kIOReturnSuccess) return true; + + hcfree (hwmon_ctx->hm_iokit); + + hwmon_ctx->hm_iokit = NULL; + + return false; +} + +bool iokit_close (void *hashcat_ctx) +{ + hwmon_ctx_t *hwmon_ctx = ((hashcat_ctx_t *) hashcat_ctx)->hwmon_ctx; + + IOKIT_PTR *iokit = hwmon_ctx->hm_iokit; + + hm_IOKIT_SMCClose (iokit->conn); + + return true; +} + +#endif // __APPLE__ diff --git a/src/hwmon.c b/src/hwmon.c index 845f4c674..af9c7acfd 100644 --- a/src/hwmon.c +++ b/src/hwmon.c @@ -245,7 +245,39 @@ int hm_get_temperature_with_devices_idx (hashcat_ctx_t *hashcat_ctx, const int b if (backend_ctx->devices_param[backend_device_idx].is_opencl == true) { + #if defined (__APPLE__) + if (backend_ctx->devices_param[backend_device_idx].opencl_platform_vendor_id == VENDOR_ID_APPLE) + { + if (hwmon_ctx->hm_iokit) + { + double temperature = 0.0; + + char *key = NULL; + + if ((backend_ctx->devices_param[backend_device_idx].opencl_device_type & CL_DEVICE_TYPE_GPU) == 0) key = HM_IOKIT_SMC_CPU_PROXIMITY; + else + { + key = HM_IOKIT_SMC_GPU_PROXIMITY; + + if (backend_ctx->devices_param[backend_device_idx].opencl_device_vendor_id == VENDOR_ID_INTEL_BEIGNET) + { + key = HM_IOKIT_SMC_PECI_GPU; + } + } + + if (hm_IOKIT_SMCGetTemperature(hashcat_ctx, key, &temperature) == -1) + { + hwmon_ctx->hm_device[backend_device_idx].temperature_get_supported = false; + + return -1; + } + + return (int) temperature; + } + } + #else if ((backend_ctx->devices_param[backend_device_idx].opencl_device_type & CL_DEVICE_TYPE_GPU) == 0) return -1; + #endif if (backend_ctx->devices_param[backend_device_idx].opencl_device_vendor_id == VENDOR_ID_AMD) { @@ -414,7 +446,26 @@ int hm_get_fanspeed_with_devices_idx (hashcat_ctx_t *hashcat_ctx, const int back if (backend_ctx->devices_param[backend_device_idx].is_opencl == true) { + #if defined (__APPLE__) + if (backend_ctx->devices_param[backend_device_idx].opencl_platform_vendor_id == VENDOR_ID_APPLE) + { + if (hwmon_ctx->hm_iokit) + { + int speed = 0; + + if (hm_IOKIT_get_fan_speed_current (hashcat_ctx, &speed) == -1) + { + hwmon_ctx->hm_device[backend_device_idx].fanspeed_get_supported = false; + + return -1; + } + + return speed; + } + } + #else if ((backend_ctx->devices_param[backend_device_idx].opencl_device_type & CL_DEVICE_TYPE_GPU) == 0) return -1; + #endif if (backend_ctx->devices_param[backend_device_idx].opencl_device_vendor_id == VENDOR_ID_AMD) { @@ -987,6 +1038,7 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) hm_attrs_t *hm_adapters_nvapi = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t)); hm_attrs_t *hm_adapters_nvml = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t)); hm_attrs_t *hm_adapters_sysfs = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t)); + hm_attrs_t *hm_adapters_iokit = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t)); #define FREE_ADAPTERS \ do { \ @@ -994,6 +1046,7 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) hcfree (hm_adapters_nvapi); \ hcfree (hm_adapters_nvml); \ hcfree (hm_adapters_sysfs); \ + hcfree (hm_adapters_iokit); \ } while (0) if (backend_ctx->need_nvml == true) @@ -1053,6 +1106,27 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) } } + #if defined(__APPLE__) + if (backend_ctx->need_iokit == true) + { + hwmon_ctx->hm_iokit = (IOKIT_PTR *) hcmalloc (sizeof (IOKIT_PTR)); + + if (iokit_init (hashcat_ctx) == false) + { + hcfree (hwmon_ctx->hm_iokit); + + hwmon_ctx->hm_iokit = NULL; + } + + if (hwmon_ctx->hm_adl) + { + hcfree (hwmon_ctx->hm_iokit); + + hwmon_ctx->hm_iokit = NULL; + } + } + #endif + if (hwmon_ctx->hm_nvml) { if (hm_NVML_nvmlInit (hashcat_ctx) == 0) @@ -1283,7 +1357,7 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) } } - if (hwmon_ctx->hm_sysfs) + if (hwmon_ctx->hm_sysfs || hwmon_ctx->hm_iokit) { if (true) { @@ -1291,6 +1365,8 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) { hc_device_param_t *device_param = &backend_ctx->devices_param[backend_devices_idx]; + if (device_param->skipped == true) continue; + if (device_param->is_cuda == true) { // nothing to do @@ -1300,21 +1376,57 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) { const u32 device_id = device_param->device_id; + if ((device_param->opencl_platform_vendor_id == VENDOR_ID_APPLE) && (hwmon_ctx->hm_iokit)) + { + hm_adapters_iokit[device_id].buslanes_get_supported = false; + hm_adapters_iokit[device_id].corespeed_get_supported = false; + hm_adapters_iokit[device_id].fanspeed_get_supported = true; + hm_adapters_iokit[device_id].fanpolicy_get_supported = false; + hm_adapters_iokit[device_id].memoryspeed_get_supported = false; + hm_adapters_iokit[device_id].temperature_get_supported = true; + hm_adapters_iokit[device_id].utilization_get_supported = false; + } + if ((device_param->opencl_device_type & CL_DEVICE_TYPE_GPU) == 0) continue; - hm_adapters_sysfs[device_id].buslanes_get_supported = true; - hm_adapters_sysfs[device_id].corespeed_get_supported = true; - hm_adapters_sysfs[device_id].fanspeed_get_supported = true; - hm_adapters_sysfs[device_id].fanpolicy_get_supported = true; - hm_adapters_sysfs[device_id].memoryspeed_get_supported = true; - hm_adapters_sysfs[device_id].temperature_get_supported = true; - hm_adapters_sysfs[device_id].utilization_get_supported = true; + if (hwmon_ctx->hm_sysfs) + { + hm_adapters_sysfs[device_id].buslanes_get_supported = true; + hm_adapters_sysfs[device_id].corespeed_get_supported = true; + hm_adapters_sysfs[device_id].fanspeed_get_supported = true; + hm_adapters_sysfs[device_id].fanpolicy_get_supported = true; + hm_adapters_sysfs[device_id].memoryspeed_get_supported = true; + hm_adapters_sysfs[device_id].temperature_get_supported = true; + hm_adapters_sysfs[device_id].utilization_get_supported = true; + } } } } } - if (hwmon_ctx->hm_adl == NULL && hwmon_ctx->hm_nvml == NULL && hwmon_ctx->hm_sysfs == NULL) + #if defined(__APPLE__) + if (backend_ctx->need_iokit == true) + { + hwmon_ctx->hm_iokit = (IOKIT_PTR *) hcmalloc (sizeof (IOKIT_PTR)); + + if (iokit_init (hashcat_ctx) == false) + { + hcfree (hwmon_ctx->hm_iokit); + + hwmon_ctx->hm_iokit = NULL; + } + + if (hwmon_ctx->hm_adl) + { + hcfree (hwmon_ctx->hm_iokit); + + hwmon_ctx->hm_iokit = NULL; + } + } + #endif + + + if (hwmon_ctx->hm_adl == NULL && hwmon_ctx->hm_nvml == NULL && hwmon_ctx->hm_sysfs == NULL && hwmon_ctx->hm_iokit == NULL) { FREE_ADAPTERS; @@ -1345,13 +1457,17 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) const u32 device_id = device_param->device_id; + hwmon_ctx->hm_device[backend_devices_idx].adl = 0; + hwmon_ctx->hm_device[backend_devices_idx].sysfs = 0; + hwmon_ctx->hm_device[backend_devices_idx].iokit = 0; + hwmon_ctx->hm_device[backend_devices_idx].nvapi = 0; + hwmon_ctx->hm_device[backend_devices_idx].nvml = 0; + hwmon_ctx->hm_device[backend_devices_idx].od_version = 0; + if (device_param->is_cuda == true) { - hwmon_ctx->hm_device[backend_devices_idx].adl = 0; - hwmon_ctx->hm_device[backend_devices_idx].sysfs = 0; hwmon_ctx->hm_device[backend_devices_idx].nvapi = hm_adapters_nvapi[device_id].nvapi; hwmon_ctx->hm_device[backend_devices_idx].nvml = hm_adapters_nvml[device_id].nvml; - hwmon_ctx->hm_device[backend_devices_idx].od_version = 0; if (hwmon_ctx->hm_nvml) { @@ -1384,15 +1500,32 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) if (device_param->is_opencl == true) { + #if defined(__APPLE__) + if (device_param->opencl_platform_vendor_id == VENDOR_ID_APPLE) + { + if (hwmon_ctx->hm_iokit) + { + hwmon_ctx->hm_device[backend_devices_idx].iokit = hm_adapters_iokit[device_id].iokit; + hwmon_ctx->hm_device[backend_devices_idx].buslanes_get_supported |= hm_adapters_iokit[device_id].buslanes_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].corespeed_get_supported |= hm_adapters_iokit[device_id].corespeed_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].fanspeed_get_supported |= hm_adapters_iokit[device_id].fanspeed_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].fanpolicy_get_supported |= hm_adapters_iokit[device_id].fanpolicy_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].memoryspeed_get_supported |= hm_adapters_iokit[device_id].memoryspeed_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].temperature_get_supported |= hm_adapters_iokit[device_id].temperature_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].threshold_shutdown_get_supported |= hm_adapters_iokit[device_id].threshold_shutdown_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].threshold_slowdown_get_supported |= hm_adapters_iokit[device_id].threshold_slowdown_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].throttle_get_supported |= hm_adapters_iokit[device_id].throttle_get_supported; + hwmon_ctx->hm_device[backend_devices_idx].utilization_get_supported |= hm_adapters_iokit[device_id].utilization_get_supported; + } + } + #else if ((device_param->opencl_device_type & CL_DEVICE_TYPE_GPU) == 0) continue; + #endif if (device_param->opencl_device_vendor_id == VENDOR_ID_AMD) { hwmon_ctx->hm_device[backend_devices_idx].adl = hm_adapters_adl[device_id].adl; - hwmon_ctx->hm_device[backend_devices_idx].sysfs = hm_adapters_sysfs[device_id].sysfs; // not used - hwmon_ctx->hm_device[backend_devices_idx].nvapi = 0; - hwmon_ctx->hm_device[backend_devices_idx].nvml = 0; - hwmon_ctx->hm_device[backend_devices_idx].od_version = 0; + hwmon_ctx->hm_device[backend_devices_idx].sysfs = hm_adapters_sysfs[device_id].sysfs; if (hwmon_ctx->hm_adl) { @@ -1427,11 +1560,8 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) if (device_param->opencl_device_vendor_id == VENDOR_ID_NV) { - hwmon_ctx->hm_device[backend_devices_idx].adl = 0; - hwmon_ctx->hm_device[backend_devices_idx].sysfs = 0; hwmon_ctx->hm_device[backend_devices_idx].nvapi = hm_adapters_nvapi[device_id].nvapi; hwmon_ctx->hm_device[backend_devices_idx].nvml = hm_adapters_nvml[device_id].nvml; - hwmon_ctx->hm_device[backend_devices_idx].od_version = 0; if (hwmon_ctx->hm_nvml) { @@ -1517,6 +1647,13 @@ void hwmon_ctx_destroy (hashcat_ctx_t *hashcat_ctx) sysfs_close (hashcat_ctx); } + #if defined (__APPLE__) + if (hwmon_ctx->hm_iokit) + { + iokit_close (hashcat_ctx); + } + #endif + // free memory hcfree (hwmon_ctx->od_clock_mem_status); diff --git a/src/monitor.c b/src/monitor.c index 02f1ac07e..b61a41824 100644 --- a/src/monitor.c +++ b/src/monitor.c @@ -130,6 +130,20 @@ static int monitor (hashcat_ctx_t *hashcat_ctx) myabort (hashcat_ctx); } + #if defined (__APPLE__) + // experimental feature, check the "Sensor Graphic Hot" sensor through IOKIT/SMC to catch a GPU overtemp alarm + else if (temperature > (int) (user_options->hwmon_temp_abort - 10)) + { + if (hm_IOKIT_SMCGetSensorGraphicHot (hashcat_ctx) == 1) + { + event_log_error (hashcat_ctx, "hm_IOKIT_SMCGetSensorGraphicHot(): Sensor Graphics HoT, GPU Overtemp"); + + EVENT_DATA (EVENT_MONITOR_TEMP_ABORT, &backend_devices_idx, sizeof (int)); + + myabort (hashcat_ctx); + } + } + #endif } for (int backend_devices_idx = 0; backend_devices_idx < backend_ctx->backend_devices_cnt; backend_devices_idx++)