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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
|
/* $OpenBSD: vmm_support.S,v 1.14 2018/09/18 16:02:08 mlarkin Exp $ */
/*
* 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/param.h>
#include <machine/asm.h>
#include <machine/psl.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
.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)
.global _C_LABEL(svm_enter_guest)
.text
.code64
.align 16,0xcc
_C_LABEL(vmm_dispatch_intr):
movq %rsp, %r11 /* r11 = temporary register */
andq $0xFFFFFFFFFFFFFFF0, %rsp
movw %ss, %ax
pushq %rax
pushq %r11
pushfq
movw %cs, %ax
pushq %rax
cli
callq *%rdi
ret
_C_LABEL(vmxon):
RETGUARD_SETUP(vmxon, r11)
vmxon (%rdi)
jz failed_on
jc failed_on
xorq %rax, %rax
RETGUARD_CHECK(vmxon, r11)
ret
failed_on:
movq $0x01, %rax
RETGUARD_CHECK(vmxon, r11)
ret
_C_LABEL(vmxoff):
RETGUARD_SETUP(vmxoff, r11)
vmxoff
jz failed_off
jc failed_off
xorq %rax, %rax
RETGUARD_CHECK(vmxoff, r11)
ret
failed_off:
movq $0x01, %rax
RETGUARD_CHECK(vmxoff, r11)
ret
_C_LABEL(vmclear):
RETGUARD_SETUP(vmclear, r11)
vmclear (%rdi)
jz failed_clear
jc failed_clear
xorq %rax, %rax
RETGUARD_CHECK(vmclear, r11)
ret
failed_clear:
movq $0x01, %rax
RETGUARD_CHECK(vmclear, r11)
ret
_C_LABEL(vmptrld):
RETGUARD_SETUP(vmptrld, r11)
vmptrld (%rdi)
jz failed_ptrld
jc failed_ptrld
xorq %rax, %rax
RETGUARD_CHECK(vmptrld, r11)
ret
failed_ptrld:
movq $0x01, %rax
RETGUARD_CHECK(vmptrld, r11)
ret
_C_LABEL(vmptrst):
RETGUARD_SETUP(vmptrst, r11)
vmptrst (%rdi)
jz failed_ptrst
jc failed_ptrst
xorq %rax, %rax
RETGUARD_CHECK(vmptrst, r11)
ret
failed_ptrst:
movq $0x01, %rax
RETGUARD_CHECK(vmptrst, r11)
ret
_C_LABEL(vmwrite):
RETGUARD_SETUP(vmwrite, r11)
vmwrite %rsi, %rdi
jz failed_write
jc failed_write
xorq %rax, %rax
RETGUARD_CHECK(vmwrite, r11)
ret
failed_write:
movq $0x01, %rax
RETGUARD_CHECK(vmwrite, r11)
ret
_C_LABEL(vmread):
RETGUARD_SETUP(vmread, r11)
vmread %rdi, (%rsi)
jz failed_read
jc failed_read
xorq %rax, %rax
RETGUARD_CHECK(vmread, r11)
ret
failed_read:
movq $0x01, %rax
RETGUARD_CHECK(vmread, r11)
ret
_C_LABEL(invvpid):
RETGUARD_SETUP(invvpid, r11)
invvpid (%rsi), %rdi
RETGUARD_CHECK(invvpid, r11)
ret
_C_LABEL(invept):
RETGUARD_SETUP(invept, r11)
invept (%rsi), %rdi
RETGUARD_CHECK(invept, r11)
ret
_C_LABEL(vmx_enter_guest):
RETGUARD_SETUP(vmx_enter_guest, r11)
movq %rdx, %r8 /* resume flag */
movq %rcx, %r9 /* L1DF MSR support */
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
popq %rax
andq $(~PSL_I), %rax
pushq %rax
/*
* 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
RETGUARD_PUSH(r11)
/* 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 */
/*
* Intel L1TF vulnerability fix
*
* Certain Intel CPUs are broken and allow guest VMs to bypass
* EPT entirely as their address harvesting logic treats guest
* PTEs as host physical addresses. Flush L1 Dcache to prevent
* information leakage by command MSR or manually reading a
* bunch of junk in order to fill sizeof(L1 Dcache)*2.
*
* %r9 (inherited from parameter 4 in %rcx earlier)
* determines the flushing requirements
* 0 - use manual "junk read" flush
* 1 - use MSR command
* 2 (VMX_SKIP_L1D_FLUSH) - no flush required on this CPU
*/
cmpq $VMX_SKIP_L1D_FLUSH, %r9
je done_flush
testq %r9, %r9
jz no_l1df_msr
/* CPU has command MSR */
movq $MSR_FLUSH_CMD, %rcx
xorq %rdx, %rdx
movq $FLUSH_CMD_L1D_FLUSH, %rax
wrmsr
jmp done_flush
no_l1df_msr:
xorq %r9, %r9
l1df_tlb_loop:
/* XXX get the right L1 size from cpuid */
cmpq $VMX_L1D_FLUSH_SIZE, %r9
je l1df_tlb_done
movb l1tf_flush_region(%r9), %al
addq $PAGE_SIZE, %r9
jmp l1df_tlb_loop
l1df_tlb_done:
/*
* Serialize: ensure previous TLB loads don't pull PTDs
* or other PA-containing data into the L1D.
*/
xorq %rax, %rax
cpuid
xorq %r9, %r9
l1df_load_cache:
movb l1tf_flush_region(%r9), %al
/* XXX get the right cacheline size from cpuid */
addq $0x40, %r9
cmpq $VMX_L1D_FLUSH_SIZE, %r9
jne l1df_load_cache
lfence
done_flush:
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 %rsi, %r8
/* XXX get the right cacheline size from cpuid */
addq $0x40, %r8
clflush (%r8)
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
clflush (%rsi)
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 %rsi, %r8
/* XXX get the right cacheline size from cpuid */
addq $0x40, %r8
clflush (%r8)
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
clflush (%rsi)
movq 0x00(%rsi), %rsi
vmresume
fail_launch_or_resume:
RET_STACK_REFILL_WITH_RCX
/* 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
RET_STACK_REFILL_WITH_RCX
restore_host:
popq %rbx
popq %rbp
popq %r12
popq %r13
popq %r14
popq %r15
RETGUARD_POP(r11)
/*
* 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 accommodate possibly lazy-switched
* selectors from above
*/
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
popw %ax
movw %ax, %ss
popw %ax
movw %ax, %ds
popw %ax
movw %ax, %es
popfq
movq %rdi, %rax
RETGUARD_CHECK(vmx_enter_guest, r11)
ret
_C_LABEL(svm_enter_guest):
RETGUARD_SETUP(svm_enter_guest, r11)
clgi
movq %rdi, %r8
pushfq
pushq %rdx /* gdt pointer */
/*
* Save (possibly) lazy-switched selectors
*/
strw %ax
pushw %ax
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
RETGUARD_PUSH(r11)
/* 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 */
/* Restore guest registers */
movq %r8, %rax /* rax = vmcb pa */
movq 0x78(%rsi), %r8
movq %r8, %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
/* %rax at 0x08(%rsi) is not needed in SVM */
movq 0x00(%rsi), %rsi
vmload %rax
vmrun %rax
vmsave %rax
/* Preserve guest registers not saved in VMCB */
pushq %rsi
pushq %rdi
movq 0x10(%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %rsi, (%rdi)
popq %rdi
popq %rsi /* discard */
popq %rsi
/* %rax at 0x08(%rsi) is not needed in SVM */
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_svm:
popq %rbx
popq %rbp
popq %r12
popq %r13
popq %r14
popq %r15
RETGUARD_POP(r11)
/*
* 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 accommodate 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
popw %ax
movw %ax, %ss
popw %ax
movw %ax, %ds
popw %ax
movw %ax, %es
xorq %rax, %rax
lldtw %ax /* Host LDT is always 0 */
popw %ax /* ax = saved TR */
popq %rdx
addq $0x2, %rdx
movq (%rdx), %rdx
/* rdx = GDTR base addr */
andb $0xF9, 5(%rdx, %rax)
ltrw %ax
popfq
movq %rdi, %rax
RETGUARD_CHECK(svm_enter_guest, r11)
ret
|