-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathWinRing0.cpp
106 lines (85 loc) · 2.66 KB
/
WinRing0.cpp
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
/*
* Copyright (c) Martin Kinkelin
*
* See the "License.txt" file in the root directory for infos
* about permitted and prohibited uses of this code.
*/
#include <cstdio>
#include <cstdlib>
#include <fcntl.h>
#include <unistd.h>
#include "WinRing0.h"
#include "StringUtils.h"
using std::exception;
using std::string;
uint32_t ReadPciConfig(uint32_t device, uint32_t function, uint32_t regAddress) {
uint32_t result;
char path[255]= "\0";
sprintf(path, "/proc/bus/pci/00/%x.%x", device, function);
int pci = open(path, O_RDONLY);
if (pci == -1) {
perror("Failed to open pci device for reading");
exit(-1);
}
pread(pci, &result, sizeof(result), regAddress);
close(pci);
return result;
}
void WritePciConfig(uint32_t device, uint32_t function, uint32_t regAddress, uint32_t value) {
char path[255]= "\0";
sprintf(path, "/proc/bus/pci/00/%x.%x", device, function);
int pci = open(path, O_WRONLY);
if (pci == -1) {
perror("Failed to open pci device for writing");
exit(-1);
}
if(pwrite(pci, &value, sizeof(value), regAddress) != sizeof(value)) {
perror("Failed to write to pci device");
}
close(pci);
}
uint64_t Rdmsr(uint32_t index) {
uint64_t result;
int msr = open("/dev/cpu/0/msr", O_RDONLY);
if (msr == -1) {
perror("Failed to open msr device for reading");
exit(-1);
}
pread(msr, &result, sizeof(result), index);
close(msr);
return result;
}
int get_num_cpu() {
CpuidRegs regs = Cpuid(0x80000008);
return 1 + (regs.ecx&0xff);
}
void Wrmsr(uint32_t index, const uint64_t& value) {
char path[255]= "\0";
for (int i = 0; i < get_num_cpu(); i++) {
sprintf(path, "/dev/cpu/%d/msr", i);
int msr = open(path, O_WRONLY);
if (msr == -1) {
perror("Failed to open msr device for writing");
exit(-1);
}
if(pwrite(msr, &value, sizeof(value), index) != sizeof(value)) {
perror("Failed to write to msr device");
}
close(msr);
}
}
CpuidRegs Cpuid(uint32_t index) {
CpuidRegs result;
FILE* cpuid = fopen("/dev/cpu/0/cpuid", "r");
if (cpuid == NULL) {
perror("Failed to open cpuid device for reading");
exit(-1);
}
fseek(cpuid, index, SEEK_SET);
fread(&(result.eax), sizeof(result.eax), 1, cpuid);
fread(&(result.ebx), sizeof(result.ebx), 1, cpuid);
fread(&(result.ecx), sizeof(result.ecx), 1, cpuid);
fread(&(result.edx), sizeof(result.edx), 1, cpuid);
fclose(cpuid);
return result;
}