-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.c
144 lines (117 loc) · 2.89 KB
/
main.c
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// [5/2/2015 uty]
// [10/10/2017 rohan kumbhar]
#include <ntddk.h>
#include "vmminitstate.h"
#include "amd64/common-asm.h"
#include "amd64/vmx-asm.h"
#include "vmx.h"
#include "ept.h"
//-----------------------------------------------------------------------------//
typedef void (*PFUNC)(void);
//-----------------------------------------------------------------------------//
NTSTATUS
RunOnProcessor (
__in ULONG ProcessorNumber,
__in PFUNC Routine
)
{
KIRQL OldIrql;
KeSetSystemAffinityThread((KAFFINITY)(1 << ProcessorNumber));
OldIrql = KeRaiseIrqlToDpcLevel();
//
// Initialize VMX on every CPU
//
Routine();
KeLowerIrql(OldIrql);
KeRevertToUserAffinityThread();
return STATUS_SUCCESS;
}
//-----------------------------------------------------------------------------//
//DEV_EXT g_DevExt = {0};
//-----------------------------------------------------------------------------//
NTSTATUS
ExitVMM (
VOID
)
{
NTSTATUS Status = STATUS_UNSUCCESSFUL;
KAFFINITY Affinity = 0;
LONG i = 0;
Affinity = KeQueryActiveProcessors();
DbgPrint("KeQueryActiveProcessors: %p\n", Affinity);
for (i = 0; i < 64; i++)
{
if (TRUE == _bittest64(&Affinity, i))
{
DbgPrint("CPU %d\n", i);
Status = RunOnProcessor(i, StopVMX);
if (STATUS_SUCCESS != Status)
{
DbgPrint("RunOnProcessor failed on processor %d\n", i);
break;
}
}
}
return Status;
}
//-----------------------------------------------------------------------------//
NTSTATUS
EnterVMM (
VOID
)
{
NTSTATUS Status = STATUS_UNSUCCESSFUL;
KAFFINITY Affinity = 0;
LONG i = 0;
Affinity = KeQueryActiveProcessors();
DbgPrint("KeQueryActiveProcessors: %p\n", Affinity);
for (i = 0; i < 64; i++)
{
if (TRUE == _bittest64(&Affinity, i))
{
Status = InitializeVMMInitState(&g_VMMInitState[i]);
DbgPrint("CPU %d\n", i);
Status = RunOnProcessor(i, StartVMX);
if (STATUS_SUCCESS != Status)
{
DbgPrint("RunOnProcessor failed on processor %d\n", i);
break;
}
}
}
return Status;
}
//-----------------------------------------------------------------------------//
NTSTATUS
DriverEntry (
__in PDRIVER_OBJECT DriverObject,
__in PUNICODE_STRING RegistryPath
)
{
NTSTATUS Status = STATUS_UNSUCCESSFUL;
LONG i = 0;
Status = GetE820FromRegistry(&g_TotalPhysicalMemory);
if (STATUS_SUCCESS != Status)
{
//DbgPrint("GetE820FromRegistry fail 0x%x\n", Status);
goto Exit0;
}
Status = InitEptTable();
if (STATUS_SUCCESS != Status)
{
goto Exit0;
}
EnterVMM();
DbgPrint("VMM running!\n");
Status = STATUS_SUCCESS;
Exit0:
if (STATUS_SUCCESS != Status)
{
for (i = 0; i < KeNumberProcessors; i++)
{
UninitializeVMMInitState(&g_VMMInitState[i]);
}
}
return Status;
}
//-----------------------------------------------------------------------------//