Repository navigation
Expand file tree
/
Copy pathcommon.c
More file actions
173 lines (136 loc) · 3.84 KB
/
Copy pathcommon.c
File metadata and controls
173 lines (136 loc) · 3.84 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright holder: Invisible Things Lab
*/
#include "common.h"
#include "hvm.h"
NTSTATUS CmInitializeSegmentSelector (
SEGMENT_SELECTOR * SegmentSelector,
USHORT Selector,
PUCHAR GdtBase
)
{
PSEGMENT_DESCRIPTOR SegDesc;
if (!SegmentSelector)
return STATUS_INVALID_PARAMETER;
if (Selector & 0x4) {
KdPrint (("CmInitializeSegmentSelector(): Given selector (0x%X) points to LDT\n", Selector));
return STATUS_INVALID_PARAMETER;
}
SegDesc = (PSEGMENT_DESCRIPTOR) ((PUCHAR) GdtBase + (Selector & ~0x7));
SegmentSelector->sel = Selector;
SegmentSelector->base = SegDesc->BaseLow | SegDesc->BaseMid << 16 | SegDesc->BaseHigh << 24;
SegmentSelector->limit = SegDesc->LimitLow | SegDesc->LimitHigh << 16;
SegmentSelector->attributes = SegDesc->AttributesLow | SegDesc->AttributesHigh << 8;
if (!(SegDesc->AttributesLow & LA_STANDARD)) {
ULONG64 tmp;
// this is a TSS or callgate etc, save the base high part
tmp = (*(PULONG64) ((PUCHAR) SegDesc + 8));
SegmentSelector->base = (SegmentSelector->base & 0xffffffff) | (tmp << 32);
}
#define IS_GRANULARITY_4KB (1 << 0xB)
if ( SegmentSelector->attributes & IS_GRANULARITY_4KB ) {
// 4096-bit granularity is enabled for this segment, scale the limit
SegmentSelector->limit = (SegmentSelector->limit << 12) | 0xfff;
}
return STATUS_SUCCESS;
}
NTSTATUS CmGenerateMovReg (
PUCHAR pCode,
PULONG pGeneratedCodeLength,
ULONG Register,
ULONG64 Value
)
{
ULONG uCodeLength;
if (!pCode || !pGeneratedCodeLength)
return STATUS_INVALID_PARAMETER;
switch (Register & ~REG_MASK) {
case REG_GP:
pCode[0] = 0x48;
pCode[1] = 0xb8 | (UCHAR) (Register & REG_MASK);
memcpy (&pCode[2], &Value, 8);
uCodeLength = 10;
break;
case REG_GP_ADDITIONAL:
pCode[0] = 0x49;
pCode[1] = 0xb8 | (UCHAR) (Register & REG_MASK);
memcpy (&pCode[2], &Value, 8);
uCodeLength = 10;
break;
case REG_CONTROL:
uCodeLength = *pGeneratedCodeLength;
CmGenerateMovReg (pCode, pGeneratedCodeLength, REG_RAX, Value);
// calc the size of the "mov rax, value"
uCodeLength = *pGeneratedCodeLength - uCodeLength;
pCode += uCodeLength;
uCodeLength = 0;
if (Register == (REG_CR8)) {
// build 0x44 0x0f 0x22 0xc0
pCode[0] = 0x44;
uCodeLength = 1;
pCode++;
Register = 0;
}
// mov crX, rax
pCode[0] = 0x0f;
pCode[1] = 0x22;
pCode[2] = 0xc0 | (UCHAR) ((Register & REG_MASK) << 3);
// *pGeneratedCodeLength has already been adjusted to the length of the "mov rax"
uCodeLength += 3;
}
if (pGeneratedCodeLength)
*pGeneratedCodeLength += uCodeLength;
return STATUS_SUCCESS;
}
NTSTATUS CmGenerateCallReg (
PUCHAR pCode,
PULONG pGeneratedCodeLength,
ULONG Register
)
{
ULONG uCodeLength;
if (!pCode || !pGeneratedCodeLength)
return STATUS_INVALID_PARAMETER;
switch (Register & ~REG_MASK) {
case REG_GP:
pCode[0] = 0xff;
pCode[1] = 0xd0 | (UCHAR) (Register & REG_MASK);
uCodeLength = 2;
break;
case REG_GP_ADDITIONAL:
pCode[0] = 0x41;
pCode[1] = 0xff;
pCode[1] = 0xd0 | (UCHAR) (Register & REG_MASK);
uCodeLength = 3;
break;
}
if (pGeneratedCodeLength)
*pGeneratedCodeLength += uCodeLength;
return STATUS_SUCCESS;
}
NTSTATUS CmGeneratePushReg (
PUCHAR pCode,
PULONG pGeneratedCodeLength,
ULONG Register
)
{
if (!pCode || !pGeneratedCodeLength)
return STATUS_INVALID_PARAMETER;
if ((Register & ~REG_MASK) != REG_GP)
return STATUS_NOT_SUPPORTED;
pCode[0] = 0x50 | (UCHAR) (Register & REG_MASK);
*pGeneratedCodeLength += 1;
return STATUS_SUCCESS;
}
NTSTATUS CmGenerateIretq (
PUCHAR pCode,
PULONG pGeneratedCodeLength
)
{
if (!pCode || !pGeneratedCodeLength)
return STATUS_INVALID_PARAMETER;
pCode[0] = 0x48;
pCode[1] = 0xcf;
*pGeneratedCodeLength += 2;
return STATUS_SUCCESS;
}