|
6 | 6 | #pragma alloc_text (INIT, DriverEntry)
|
7 | 7 | #endif
|
8 | 8 |
|
9 |
| -NTSTATUS |
10 |
| -DriverEntry( |
11 |
| - _In_ PDRIVER_OBJECT DriverObject, |
12 |
| - _In_ PUNICODE_STRING RegistryPath |
13 |
| - ) |
| 9 | +NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject, |
| 10 | + PUNICODE_STRING RegistryPath) |
14 | 11 | {
|
15 |
| - NTSTATUS status; |
16 | 12 | PDEVICE_OBJECT device_object;
|
17 | 13 | UNICODE_STRING device_name;
|
18 | 14 | UNICODE_STRING sym_name;
|
| 15 | + NTSTATUS status; |
19 | 16 |
|
20 | 17 | DbgPrint("Registry path address: %p\n", RegistryPath);
|
21 | 18 |
|
@@ -55,14 +52,16 @@ NTSTATUS DispatchCreate(PDEVICE_OBJECT device, PIRP irp)
|
55 | 52 | memset(manufacturer, 0, sizeof(manufacturer));
|
56 | 53 | __cpuid(cpu_regs, 0);
|
57 | 54 | memcpy(manufacturer, &cpu_regs[1], sizeof(unsigned __int32));
|
58 |
| - memcpy(manufacturer + sizeof(unsigned __int32), &cpu_regs[3], sizeof(unsigned __int32)); |
59 |
| - memcpy(manufacturer + 2 * sizeof(unsigned __int32), &cpu_regs[2], sizeof(unsigned __int32)); |
| 55 | + memcpy(manufacturer + sizeof(unsigned __int32), &cpu_regs[3], |
| 56 | + sizeof(unsigned __int32)); |
| 57 | + memcpy(manufacturer + 2 * sizeof(unsigned __int32), &cpu_regs[2], |
| 58 | + sizeof(unsigned __int32)); |
60 | 59 |
|
61 |
| - if (strncmp(manufacturer, "GenuineIntel", sizeof(manufacturer) - 1) == 0) |
| 60 | + if (!strncmp(manufacturer, "GenuineIntel", sizeof(manufacturer) - 1)) |
62 | 61 | machine_type = E_MACHINE_INTEL;
|
63 |
| - else if (strncmp(manufacturer, "AMDisbetter!", sizeof(manufacturer) - 1) == 0) |
| 62 | + else if (!strncmp(manufacturer, "AMDisbetter!", sizeof(manufacturer) - 1)) |
64 | 63 | machine_type = E_MACHINE_AMD;
|
65 |
| - else if (strncmp(manufacturer, "AuthenticAMD", sizeof(manufacturer) - 1) == 0) |
| 64 | + else if (!strncmp(manufacturer, "AuthenticAMD", sizeof(manufacturer) - 1)) |
66 | 65 | machine_type = E_MACHINE_AMD;
|
67 | 66 | else
|
68 | 67 | machine_type = E_MACHINE_UNK;
|
@@ -96,44 +95,60 @@ NTSTATUS DispatchCleanup(PDEVICE_OBJECT device, PIRP irp)
|
96 | 95 |
|
97 | 96 | NTSTATUS DispatchDeviceControl(PDEVICE_OBJECT device, PIRP irp)
|
98 | 97 | {
|
99 |
| - NTSTATUS ntStatus; |
100 |
| - UINT32 msrRegister; |
101 |
| - ULONG inputBufferLength; |
102 |
| - ULONG outputBufferLength; |
| 98 | + GROUP_AFFINITY affinity, old; |
| 99 | + PIO_STACK_LOCATION stackLoc; |
| 100 | + PROCESSOR_NUMBER pnumber; |
103 | 101 | ULONGLONG msrResult;
|
104 |
| - PIO_STACK_LOCATION stackLocation; |
| 102 | + NTSTATUS ntStatus; |
| 103 | + struct data data; |
| 104 | + size_t inLength; |
105 | 105 |
|
106 |
| - stackLocation = irp->Tail.Overlay.CurrentStackLocation; |
107 |
| - inputBufferLength = stackLocation->Parameters.DeviceIoControl.InputBufferLength; |
108 |
| - outputBufferLength = stackLocation->Parameters.DeviceIoControl.OutputBufferLength; |
| 106 | + stackLoc = irp->Tail.Overlay.CurrentStackLocation; |
| 107 | + inLength = stackLoc->Parameters.DeviceIoControl.InputBufferLength; |
109 | 108 |
|
110 | 109 | DbgPrint("Received event for driver %s... \n", device->DriverObject->DriverName);
|
111 | 110 |
|
112 |
| - /* METHOD_BUFFERED */ |
113 |
| - if (inputBufferLength == sizeof(ULONGLONG)) |
114 |
| - { |
115 |
| - /* MSR register codes provided by userland must not exceed 8 bytes */ |
116 |
| - memcpy(&msrRegister, irp->AssociatedIrp.SystemBuffer, sizeof(ULONGLONG)); |
117 |
| - if (validate_msr_lookup(msrRegister) != 0) |
118 |
| - { |
119 |
| - DbgPrint("Requested MSR register (%08x) access is not allowed!\n", msrRegister); |
120 |
| - ntStatus = STATUS_INVALID_DEVICE_REQUEST; |
121 |
| - } |
122 |
| - else |
123 |
| - { |
124 |
| - /* Call readmsr instruction */ |
125 |
| - msrResult = __readmsr(msrRegister); |
126 |
| - memcpy(irp->AssociatedIrp.SystemBuffer, &msrResult, sizeof(ULONGLONG)); |
127 |
| - ntStatus = STATUS_SUCCESS; |
128 |
| - irp->IoStatus.Information = sizeof(ULONGLONG); |
129 |
| - } |
| 111 | + if (inLength != sizeof(data)) { |
| 112 | + DbgPrint("Bad input length provided. Expected %zu bytes, got %zu.\n", |
| 113 | + sizeof(data), inLength); |
| 114 | + ntStatus = STATUS_INVALID_DEVICE_REQUEST; |
| 115 | + goto error; |
130 | 116 | }
|
131 |
| - else |
| 117 | + |
| 118 | + /* Convert input data into structure */ |
| 119 | + memcpy(&data, irp->AssociatedIrp.SystemBuffer, sizeof(data)); |
| 120 | + if (validate_msr_lookup(data.msrRegister) != 0) |
132 | 121 | {
|
133 |
| - DbgPrint("Bad input length provided. Expected %u bytes, got %u.\n", sizeof(ULONGLONG), inputBufferLength); |
| 122 | + DbgPrint("Requested MSR register (%04x) access is not allowed!\n", |
| 123 | + data.msrRegister); |
134 | 124 | ntStatus = STATUS_INVALID_DEVICE_REQUEST;
|
| 125 | + goto error; |
| 126 | + } |
| 127 | + |
| 128 | + /* Run code on the specified socket */ |
| 129 | + if ((ntStatus = KeGetProcessorNumberFromIndex(data.cpuIndex, &pnumber)) |
| 130 | + != STATUS_SUCCESS) { |
| 131 | + DbgPrint("Failed to get processor info!\n"); |
| 132 | + goto error; |
135 | 133 | }
|
136 | 134 |
|
| 135 | + /* Set affinity */ |
| 136 | + memset(&affinity, 0, sizeof(GROUP_AFFINITY)); |
| 137 | + affinity.Group = pnumber.Group; |
| 138 | + KeSetSystemGroupAffinityThread(&affinity, &old); |
| 139 | + |
| 140 | + /* Call readmsr instruction */ |
| 141 | + msrResult = __readmsr(data.msrRegister); |
| 142 | + |
| 143 | + /* Restore affinity */ |
| 144 | + KeRevertToUserGroupAffinityThread(&old); |
| 145 | + |
| 146 | + /* Save result */ |
| 147 | + memcpy(irp->AssociatedIrp.SystemBuffer, &msrResult, sizeof(data)); |
| 148 | + irp->IoStatus.Information = sizeof(data); |
| 149 | + ntStatus = STATUS_SUCCESS; |
| 150 | + |
| 151 | +error: |
137 | 152 | irp->IoStatus.Status = ntStatus;
|
138 | 153 | IofCompleteRequest(irp, IO_NO_INCREMENT);
|
139 | 154 |
|
|
0 commit comments