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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/*
* Copyright (c) 2014 Mike Larkin <mlarkin@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "assym.h"
#include <machine/asm.h>
#include <machine/specialreg.h>
/*
* XXX duplicated in vmmvar.h due to song-and-dance with sys/rwlock.h inclusion
* here
*/
#define VMX_FAIL_LAUNCH_UNKNOWN 1
#define VMX_FAIL_LAUNCH_INVALID_VMCS 2
#define VMX_FAIL_LAUNCH_VALID_VMCS 3
.text
.code64
.align 16
.global _C_LABEL(vmxon)
.global _C_LABEL(vmxoff)
.global _C_LABEL(vmclear)
.global _C_LABEL(vmptrld)
.global _C_LABEL(vmptrst)
.global _C_LABEL(vmwrite)
.global _C_LABEL(vmread)
.global _C_LABEL(invvpid)
.global _C_LABEL(invept)
.global _C_LABEL(vmx_enter_guest)
.global _C_LABEL(vmm_dispatch_intr)
_C_LABEL(vmm_dispatch_intr):
movq %rsp, %r11 /* r11 = temporary register */
andq $0xFFFFFFFFFFFFFFF0, %rsp
movw %ss, %ax
pushq %ax
pushq %r11
pushfq
movw %cs, %ax
pushq %ax
cli
callq *%rdi
ret
_C_LABEL(vmxon):
vmxon (%rdi)
jz failed_on
jc failed_on
xorq %rax, %rax
ret
failed_on:
movq $0x01, %rax
ret
_C_LABEL(vmxoff):
vmxoff
jz failed_off
jc failed_off
xorq %rax, %rax
ret
failed_off:
movq $0x01, %rax
ret
_C_LABEL(vmclear):
vmclear (%rdi)
jz failed_clear
jc failed_clear
xorq %rax, %rax
ret
failed_clear:
movq $0x01, %rax
ret
_C_LABEL(vmptrld):
vmptrld (%rdi)
jz failed_ptrld
jc failed_ptrld
xorq %rax, %rax
ret
failed_ptrld:
movq $0x01, %rax
ret
_C_LABEL(vmptrst):
vmptrst (%rdi)
jz failed_ptrst
jc failed_ptrst
xorq %rax, %rax
ret
failed_ptrst:
movq $0x01, %rax
ret
_C_LABEL(vmwrite):
vmwrite %rsi, %rdi
jz failed_write
jc failed_write
xorq %rax, %rax
ret
failed_write:
movq $0x01, %rax
ret
_C_LABEL(vmread):
vmread %rdi, (%rsi)
jz failed_read
jc failed_read
xorq %rax, %rax
ret
failed_read:
movq $0x01, %rax
ret
_C_LABEL(invvpid):
invvpid (%rsi), %rdi
ret
_C_LABEL(invept):
invept (%rsi), %rdi
ret
_C_LABEL(vmx_enter_guest):
movq %rdx, %r8 /* resume flag */
testq %r8, %r8
jnz skip_init
/*
* XXX make vmx_exit_handler a global and put this in the per-vcpu
* init code
*/
movq $VMCS_HOST_IA32_RIP, %rdi
movq $vmx_exit_handler_asm, %rax
vmwrite %rax, %rdi /* Host RIP */
skip_init:
/*
* XXX use msr list here for restore instead of all this
* stack jiggery-pokery
*/
pushfq
/*
* Save (possibly) lazy-switched selectors
*/
movw %es, %ax
pushw %ax
movw %ds, %ax
pushw %ax
movw %ss, %ax
pushw %ax
movq $MSR_FSBASE, %rcx
rdmsr
pushq %rax
pushq %rdx
pushw %fs
movq $MSR_GSBASE, %rcx
rdmsr
pushq %rax
pushq %rdx
pushw %gs
movq $MSR_KERNELGSBASE, %rcx
rdmsr
pushq %rax
pushq %rdx
/*
* Save various MSRs
*/
movq $MSR_STAR, %rcx
rdmsr
pushq %rax
pushq %rdx
movq $MSR_LSTAR, %rcx
rdmsr
pushq %rax
pushq %rdx
/* XXX - unused? */
movq $MSR_CSTAR, %rcx
rdmsr
pushq %rax
pushq %rdx
movq $MSR_SFMASK, %rcx
rdmsr
pushq %rax
pushq %rdx
/* Preserve callee-preserved registers as per AMD64 ABI */
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbp
pushq %rbx
pushq %rsi /* Guest Regs Pointer */
movq $VMCS_HOST_IA32_RSP, %rdi
movq %rsp, %rax
vmwrite %rax, %rdi /* Host RSP */
testq %r8, %r8
jnz do_resume
/* Restore guest registers */
movq 0x78(%rsi), %rax
movq %rax, %cr2
movq 0x70(%rsi), %r15
movq 0x68(%rsi), %r14
movq 0x60(%rsi), %r13
movq 0x58(%rsi), %r12
movq 0x50(%rsi), %r11
movq 0x48(%rsi), %r10
movq 0x40(%rsi), %r9
movq 0x38(%rsi), %r8
movq 0x30(%rsi), %rbp
movq 0x28(%rsi), %rdi
movq 0x20(%rsi), %rdx
movq 0x18(%rsi), %rcx
movq 0x10(%rsi), %rbx
movq 0x08(%rsi), %rax
movq 0x00(%rsi), %rsi
vmlaunch
jmp fail_launch_or_resume
do_resume:
/* Restore guest registers */
movq 0x78(%rsi), %rax
movq %rax, %cr2
movq 0x70(%rsi), %r15
movq 0x68(%rsi), %r14
movq 0x60(%rsi), %r13
movq 0x58(%rsi), %r12
movq 0x50(%rsi), %r11
movq 0x48(%rsi), %r10
movq 0x40(%rsi), %r9
movq 0x38(%rsi), %r8
movq 0x30(%rsi), %rbp
movq 0x28(%rsi), %rdi
movq 0x20(%rsi), %rdx
movq 0x18(%rsi), %rcx
movq 0x10(%rsi), %rbx
movq 0x08(%rsi), %rax
movq 0x00(%rsi), %rsi
vmresume
fail_launch_or_resume:
/* Failed launch/resume (fell through) */
jc fail_launch_invalid_vmcs /* Invalid VMCS */
jz fail_launch_valid_vmcs /* Valid VMCS, failed launch/resume */
/* Unknown failure mode (not documented as per Intel SDM) */
fail_launch_unknown:
movq $VMX_FAIL_LAUNCH_UNKNOWN, %rdi
popq %rsi
jmp restore_host
fail_launch_invalid_vmcs:
movq $VMX_FAIL_LAUNCH_INVALID_VMCS, %rdi
popq %rsi
jmp restore_host
fail_launch_valid_vmcs:
movq $VMCS_INSTRUCTION_ERROR, %rdi
popq %rsi
vmread %rdi, %rax
/* XXX check failure of vmread */
movl %eax, 0x80(%rsi)
movq $VMX_FAIL_LAUNCH_VALID_VMCS, %rdi
jmp restore_host
vmx_exit_handler_asm:
/* Preserve guest registers not saved in VMCS */
pushq %rsi
pushq %rdi
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rsi, (%rdi)
popq %rdi
popq %rsi /* discard */
popq %rsi
movq %rax, 0x8(%rsi)
movq %rbx, 0x10(%rsi)
movq %rcx, 0x18(%rsi)
movq %rdx, 0x20(%rsi)
movq %rdi, 0x28(%rsi)
movq %rbp, 0x30(%rsi)
movq %r8, 0x38(%rsi)
movq %r9, 0x40(%rsi)
movq %r10, 0x48(%rsi)
movq %r11, 0x50(%rsi)
movq %r12, 0x58(%rsi)
movq %r13, 0x60(%rsi)
movq %r14, 0x68(%rsi)
movq %r15, 0x70(%rsi)
movq %cr2, %rax
movq %rax, 0x78(%rsi)
/* %rdi = 0 means we took an exit */
xorq %rdi, %rdi
restore_host:
popq %rbx
popq %rbp
popq %r12
popq %r13
popq %r14
popq %r15
/*
* Restore saved MSRs
*/
popq %rdx
popq %rax
movq $MSR_SFMASK, %rcx
wrmsr
/* XXX - unused? */
popq %rdx
popq %rax
movq $MSR_CSTAR, %rcx
wrmsr
popq %rdx
popq %rax
movq $MSR_LSTAR, %rcx
wrmsr
popq %rdx
popq %rax
movq $MSR_STAR, %rcx
wrmsr
/*
* popw %gs will reset gsbase to 0, so preserve it
* first. This is to accomodate possibly lazy-switched
* selectors from above
*/
cli
popq %rdx
popq %rax
movq $MSR_KERNELGSBASE, %rcx
wrmsr
popw %gs
popq %rdx
popq %rax
movq $MSR_GSBASE, %rcx
wrmsr
popw %fs
popq %rdx
popq %rax
movq $MSR_FSBASE, %rcx
wrmsr
sti
popw %ax
movw %ax, %ss
popw %ax
movw %ax, %ds
popw %ax
movw %ax, %es
popfq
movq %rdi, %rax
ret
|