-
Notifications
You must be signed in to change notification settings - Fork 182
/
vmx.S
345 lines (309 loc) · 6.66 KB
/
vmx.S
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* ksm - a really simple and fast x64 hypervisor
* Copyright (C) 2016, 2017 Ahmed Samy <[email protected]>
*
* Assembly stuff for GCC (AT&T aka GAS) only. See vmx.asm for MASM.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef __ASSEMBLY__
#error __ASSEMBLY__ is not defined...
#endif
#include "compiler.h"
#ifdef __linux__
/* Arguments */
#define REG_A1 %rdi
#define REG_A2 %rsi
#define REG_A3 %rdx
#define REG_A4 %rcx
/* Volatile registers (caller-saved) */
#define REG_V1 %rdx
#define REG_V2 %rcx
#define REG_V3 %rax
#define REG_V4 %r8
#define REG_V5 %r9
#define REG_V6 %r10
#define REG_V7 %r11
#define REG_V8 %rdi
#define REG_V9 %rsi
#else
/* Arguments */
#define REG_A1 %rcx
#define REG_A2 %rdx
#define REG_A3 %r8
#define REG_A4 %r9
/* Volatile registers (caller-saved) */
#define REG_V1 %rax
#define REG_V2 %rcx
#define REG_V3 %rdx
#define REG_V4 %r8
#define REG_V5 %r9
#define REG_V6 %r10
#define REG_V7 %r11
#endif
.macro PUSH_REGS
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %r11
pushq %r10
pushq %r9
pushq %r8
pushq %rdi
pushq %rsi
pushq %rbp
subq $8, %rsp /* placeholder */
pushq %rbx
pushq %rdx
pushq %rcx
pushq %rax
.endm
.macro POP_REGS
popq %rax
popq %rcx
popq %rdx
popq %rbx
addq $8, %rsp
popq %rbp
popq %rsi
popq %rdi
popq %r8
popq %r9
popq %r10
popq %r11
popq %r12
popq %r13
popq %r14
popq %r15
.endm
#define KFRAME_RPL -0x66
#define KFRAME_CSR -0x64
#define KFRAME_V1 -0x60
#define KFRAME_V2 -0x58
#define KFRAME_V3 -0x50
#define KFRAME_V4 -0x48
#define KFRAME_V5 -0x40
#define KFRAME_V6 -0x38
#define KFRAME_V7 -0x30
#define KFRAME_V8 -0x28
#define KFRAME_V9 -0x20
#define KFRAME_XMM0 -0x10
#define KFRAME_XMM1 0x00
#define KFRAME_XMM2 0x10
#define KFRAME_XMM3 0x20
#define KFRAME_XMM4 0x30
#define KFRAME_XMM5 0x40
#define KFRAME_SS 0x108
#define KFRAME_RSP 0x100
#define KFRAME_FLGS 0x0F8
#define KFRAME_CS 0x0F0
#define KFRAME_EC 0x0E0
#define KFRAME_IP 0x0E8
.macro TRAP_SAVE_GPR
pushq %rbp
subq $0x158, %rsp
leaq 0x80(%rsp), %rbp
movq REG_V1, KFRAME_V1(%rbp)
movq REG_V2, KFRAME_V2(%rbp)
movq REG_V3, KFRAME_V3(%rbp)
movq REG_V4, KFRAME_V4(%rbp)
movq REG_V5, KFRAME_V5(%rbp)
movq REG_V6, KFRAME_V6(%rbp)
movq REG_V7, KFRAME_V7(%rbp)
#ifdef REG_V8
movq REG_V8, KFRAME_V8(%rbp)
#endif
#ifdef REG_V9
movq REG_V9, KFRAME_V9(%rbp)
#endif
.endm
.macro TRAP_REST_GPR
#ifdef REG_V9
movq KFRAME_V9(%rbp), REG_V9
#endif
#ifdef REG_V8
movq KFRAME_V8(%rbp), REG_V8
#endif
movq KFRAME_V7(%rbp), REG_V7
movq KFRAME_V6(%rbp), REG_V6
movq KFRAME_V5(%rbp), REG_V5
movq KFRAME_V4(%rbp), REG_V4
movq KFRAME_V3(%rbp), REG_V3
movq KFRAME_V2(%rbp), REG_V2
movq KFRAME_V1(%rbp), REG_V1
movq %rbp, %rsp
movq 0xD8(%rbp), %rbp
addq $0xE8, %rsp
.endm
.macro TRAP_SAVE_XMM
#ifndef __linux__
stmxcsr KFRAME_CSR(%rbp)
ldmxcsr %gs:0x180
movaps %xmm0, KFRAME_XMM0(%rbp)
movaps %xmm1, KFRAME_XMM1(%rbp)
movaps %xmm2, KFRAME_XMM2(%rbp)
movaps %xmm3, KFRAME_XMM3(%rbp)
movaps %xmm4, KFRAME_XMM4(%rbp)
movaps %xmm5, KFRAME_XMM5(%rbp)
#endif
.endm
.macro TRAP_REST_XMM
#ifndef __linux__
ldmxcsr KFRAME_CSR(%rbp)
movaps KFRAME_XMM0(%rbp), %xmm0
movaps KFRAME_XMM1(%rbp), %xmm1
movaps KFRAME_XMM2(%rbp), %xmm2
movaps KFRAME_XMM3(%rbp), %xmm3
movaps KFRAME_XMM4(%rbp), %xmm4
movaps KFRAME_XMM5(%rbp), %xmm5
#endif
.endm
.macro TRAP_ENTER
TRAP_SAVE_GPR
movw KFRAME_CS(%rbp), %ax
andb $1, %al
movb %al, KFRAME_RPL(%rbp)
jz 1f
swapgs
1:
cld
TRAP_SAVE_XMM
.endm
.macro TRAP_EXIT
testb $1, KFRAME_RPL(%rbp)
jz 1f
TRAP_REST_XMM
TRAP_REST_GPR
swapgs
iretq
1:
TRAP_REST_XMM
TRAP_REST_GPR
iretq
.endm
.macro irq_entry symbol:req handler:req no_error_code:req
.globl \symbol
\symbol:
.if \no_error_code
subq $8, %rsp
.endif
TRAP_ENTER
movq KFRAME_CS(%rbp), REG_A1
movq KFRAME_IP(%rbp), REG_A2
subq $0x20, %rsp
call \handler
addq $0x20, %rsp
TRAP_EXIT
.endm
.text
/* EPT violations */
irq_entry __ept_violation __ept_handle_violation 1
.globl __vmx_vminit
__vmx_vminit:
pushfq
PUSH_REGS
/* parameter one is pass-through (vcpu). */
movq %rsp, REG_A2
movabs $do_resume, REG_A3
subq $0x20, %rsp
call vcpu_run
addq $0x20, %rsp
/* If we get here, we failed */
POP_REGS
popfq
movl ERR_DENIED, %eax
ret
do_resume:
/* Succeeded */
POP_REGS
popfq
xorl %eax, %eax
ret
.globl __vmx_entrypoint
__vmx_entrypoint:
/*
* Host entry point (aka VMX root mode).
* Note: all interrupts are disabled at this point.
*
* Save all guest general purpose registers, then let the C handler do
* the rest.
*
* This is how the stack looks like after PUSH_REGS:
* +0 = %rax
* +8 = %rcx
* +16 = %rdx
* +24 = %rbx
* +32 = %rsp (garbage, overwritten with real value in
* vcpu_handle_exit())
* +40 = %rbp
* +48 = %rsi
* +56 = %rdi
* +64 = %r8
* +72 = %r9
* +80 = %r10
* +88 = %r11
* +96 = %r12
* +104 = %r13
* +112 = %r14
* +120 = %r15
* +128 = vcpu (set in vcpu_create())
*
* Note: we pass vcpu->stack -8 to HOST_RSP, this is because
* at this specific area, it's set to the vcpu pointer. So basically,
* a pop here (before PUSH_REGS) will pop vcpu pointer, but the
* registers in this specific context are guest registers, we need
* their values as-is untouched, we may also modify them later on
* depending on the event (e.g. cpuid, etc.)
*/
PUSH_REGS
movq %rsp, REG_A1
subq $0x20, %rsp
call vcpu_handle_exit
addq $0x20, %rsp
testb %al, %al
jz 1f
POP_REGS
vmresume
/* Something went wrong during re-entry to guest... */
jmp 2f
1:
/* See exit.c (vcpu_do_exit) */
POP_REGS
vmxoff
jna 2f
/* Give them their stack pointer */
movq %rdx, %rsp
/* and their rflags (adjusted to indicate successful vmcall) */
pushq %rax
popfq
/*
* rcx contains return address, which is guest's rip + instr_len (aka
* VMCALL length)
*/
pushq %rcx
ret
2:
/* Either vmresume or vmxoff failure... */
nop
pushfq
PUSH_REGS
movq %rsp, REG_A1
subq $0x20, %rsp
call vcpu_handle_fail
addq $0x20, %rsp
3: /* shouldn't come here, but incase it does, hlt forever: */
hlt
jmp 3b