forked from TritonDataCenter/illumos-kvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvm_vmx.c
4714 lines (3966 loc) · 115 KB
/
kvm_vmx.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
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Kernel-based Virtual Machine driver for Linux
*
* This module enables machines with Intel VT-x extensions to run virtual
* machines without emulation or binary translation.
*
* Copyright (C) 2006 Qumranet, Inc.
*
* Authors:
* Avi Kivity <[email protected]>
* Yaniv Kamay <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2. See
* the COPYING file in the top-level directory.
*
* Copyright (c) 2015 Joyent, Inc. All rights reserved.
*/
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <sys/mach_mmu.h>
#include <asm/cpu.h>
#include <sys/x86_archext.h>
#include <sys/xc_levels.h>
#include <sys/machsystm.h>
#include "kvm_bitops.h"
#include "kvm_msr.h"
#include "kvm_cpuid.h"
#include "kvm_impl.h"
#include "kvm_x86impl.h"
#include "kvm_cache_regs.h"
#include "kvm_host.h"
#include "kvm_iodev.h"
#include "kvm_irq.h"
#include "kvm_mmu.h"
#include "kvm_vmx.h"
/*
* Globals
*/
struct kvm_shared_msrs **shared_msrs;
#define VMX_NR_VPIDS (1 << 16)
static kmutex_t vmx_vpid_lock;
static ulong_t *vmx_vpid_bitmap;
static size_t vpid_bitmap_words;
static int bypass_guest_pf = 1;
static int enable_vpid = 1;
static int flexpriority_enabled = 1;
static int enable_ept = 1;
static int kvm_vmx_ept_required = 1;
static int enable_unrestricted_guest = 1;
static int emulate_invalid_guest_state = 0;
static kmem_cache_t *kvm_vcpu_cache;
#ifdef XXX_KVM_DECLARATION
static unsigned long *vmx_io_bitmap_a;
static unsigned long *vmx_io_bitmap_b;
static unsigned long *vmx_msr_bitmap_legacy;
static unsigned long *vmx_msr_bitmap_longmode;
#else
/* make these arrays to try to force into low 4GB memory... */
/* also need to be aligned... */
__attribute__((__aligned__(PAGESIZE)))static unsigned long
vmx_io_bitmap_a[PAGESIZE / sizeof (unsigned long)];
__attribute__((__aligned__(PAGESIZE)))static unsigned long
vmx_io_bitmap_b[PAGESIZE / sizeof (unsigned long)];
__attribute__((__aligned__(PAGESIZE)))static unsigned long
vmx_msr_bitmap_legacy[PAGESIZE / sizeof (unsigned long)];
__attribute__((__aligned__(PAGESIZE)))static unsigned long
vmx_msr_bitmap_longmode[PAGESIZE / sizeof (unsigned long)];
#endif
static struct vmcs **vmxarea; /* 1 per cpu */
static struct vmcs **current_vmcs;
static uint64_t *vmxarea_pa; /* physical address of each vmxarea */
static int vmx_has_kvm_support_override = 0;
#define KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST \
(X86_CR0_WP | X86_CR0_NE | X86_CR0_NW | X86_CR0_CD)
#define KVM_GUEST_CR0_MASK \
(KVM_GUEST_CR0_MASK_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST \
(X86_CR0_WP | X86_CR0_NE)
#define KVM_VM_CR0_ALWAYS_ON \
(KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | X86_CR0_PG | X86_CR0_PE)
#define KVM_CR4_GUEST_OWNED_BITS \
(X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
| X86_CR4_OSXMMEXCPT)
#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
#define __ex(x) __kvm_handle_fault_on_reboot(x)
#define page_to_phys(page) (page->p_pagenum << PAGESHIFT)
/*
* These 2 parameters are used to config the controls for Pause-Loop Exiting:
* ple_gap: upper bound on the amount of time between two successive
* executions of PAUSE in a loop. Also indicate if ple enabled.
* According to test, this time is usually small than 41 cycles.
* ple_window: upper bound on the amount of time a guest is allowed to execute
* in a PAUSE loop. Tests indicate that most spinlocks are held for
* less than 2^12 cycles
* Time is measured based on a counter that runs at the same rate as the TSC,
* refer SDM volume 3b section 21.6.13 & 22.1.3.
*/
#define KVM_VMX_DEFAULT_PLE_GAP 41
#define KVM_VMX_DEFAULT_PLE_WINDOW 4096
static int ple_gap = KVM_VMX_DEFAULT_PLE_GAP;
static int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
typedef struct vmcs {
uint32_t revision_id;
uint32_t abort;
char data[1]; /* size is read from MSR */
} vmcs_t;
typedef struct shared_msr_entry {
unsigned index;
uint64_t data;
uint64_t mask;
} shared_msr_entry_t;
typedef struct vcpu_vmx {
struct kvm_vcpu vcpu;
struct list_node local_vcpus_link;
unsigned long host_rsp;
int launched;
unsigned char fail;
uint32_t idt_vectoring_info;
struct shared_msr_entry *guest_msrs;
int nmsrs;
int save_nmsrs;
uint64_t msr_host_kernel_gs_base;
uint64_t msr_guest_kernel_gs_base;
struct vmcs *vmcs;
uint64_t vmcs_pa; /* physical address of vmx's vmcs */
struct {
int loaded;
unsigned short fs_sel, gs_sel, ldt_sel;
int gs_ldt_reload_needed;
int fs_reload_needed;
} host_state;
struct {
int vm86_active;
ulong save_rflags;
struct kvm_save_segment {
unsigned short selector;
unsigned long base;
uint32_t limit;
uint32_t ar;
} tr, es, ds, fs, gs;
struct {
char pending;
unsigned char vector;
unsigned rip;
} irq;
} rmode;
int vpid;
char emulation_required;
/* Support for vnmi-less CPUs */
int soft_vnmi_blocked;
time_t entry_time;
int64_t vnmi_blocked_time;
uint32_t exit_reason;
char rdtscp_enabled;
} vcpu_vmx_t;
static struct vcpu_vmx *
to_vmx(struct kvm_vcpu *vcpu)
{
return ((struct vcpu_vmx *)((uintptr_t)vcpu -
offsetof(struct vcpu_vmx, vcpu)));
}
typedef struct vmcs_config {
int size;
int order;
uint32_t revision_id;
uint32_t pin_based_exec_ctrl;
uint32_t cpu_based_exec_ctrl;
uint32_t cpu_based_2nd_exec_ctrl;
uint32_t vmexit_ctrl;
uint32_t vmentry_ctrl;
} vmcs_config_t;
typedef struct vmx_capability {
uint32_t ept;
uint32_t vpid;
} vmx_capability_t;
#define VMX_SEGMENT_FIELD(seg) \
[VCPU_SREG_##seg] = { \
.selector = GUEST_##seg##_SELECTOR, \
.base = GUEST_##seg##_BASE, \
.limit = GUEST_##seg##_LIMIT, \
.ar_bytes = GUEST_##seg##_AR_BYTES, \
}
typedef struct kvm_vmx_segment_field {
unsigned selector;
unsigned base;
unsigned limit;
unsigned ar_bytes;
} kvm_vmx_segment_field_t;
struct kvm_vmx_segment_field kvm_vmx_segment_fields[] = {
VMX_SEGMENT_FIELD(CS),
VMX_SEGMENT_FIELD(DS),
VMX_SEGMENT_FIELD(ES),
VMX_SEGMENT_FIELD(FS),
VMX_SEGMENT_FIELD(GS),
VMX_SEGMENT_FIELD(SS),
VMX_SEGMENT_FIELD(TR),
VMX_SEGMENT_FIELD(LDTR),
};
static vmcs_config_t vmcs_config;
static vmx_capability_t vmx_capability;
static uint64_t host_efer;
static void ept_save_pdptrs(struct kvm_vcpu *);
/*
* Keep MSR_K6_STAR at the end, as setup_msrs() will try to optimize it
* away by decrementing the array size.
*/
static const uint32_t vmx_msr_index[] = {
MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
MSR_EFER, MSR_TSC_AUX, MSR_K6_STAR,
};
#define NR_VMX_MSR ARRAY_SIZE(vmx_msr_index)
static void
native_load_tr_desc(void)
{
__asm__ volatile("ltr %w0"::"q" (KTSS_SEL));
}
#define load_TR_desc() native_load_tr_desc()
static int
is_page_fault(uint32_t intr_info)
{
return ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
INTR_INFO_VALID_MASK)) == (INTR_TYPE_HARD_EXCEPTION |
PF_VECTOR | INTR_INFO_VALID_MASK));
}
static int
is_no_device(uint32_t intr_info)
{
return ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
INTR_INFO_VALID_MASK)) == (INTR_TYPE_HARD_EXCEPTION | NM_VECTOR |
INTR_INFO_VALID_MASK));
}
static int
is_invalid_opcode(uint32_t intr_info)
{
return ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
INTR_INFO_VALID_MASK)) == (INTR_TYPE_HARD_EXCEPTION | UD_VECTOR |
INTR_INFO_VALID_MASK));
}
static int
is_external_interrupt(uint32_t intr_info)
{
return ((intr_info & (INTR_INFO_INTR_TYPE_MASK |
INTR_INFO_VALID_MASK)) == (INTR_TYPE_EXT_INTR |
INTR_INFO_VALID_MASK));
}
static int
is_machine_check(uint32_t intr_info)
{
return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK |
INTR_INFO_VALID_MASK)) == (INTR_TYPE_HARD_EXCEPTION |
MC_VECTOR | INTR_INFO_VALID_MASK);
}
static int
cpu_has_vmx_msr_bitmap(void)
{
return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_USE_MSR_BITMAPS);
}
static int
cpu_has_vmx_tpr_shadow(void)
{
return (vmcs_config.cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW);
}
static int
vm_need_tpr_shadow(struct kvm *kvm)
{
return ((cpu_has_vmx_tpr_shadow()) && (irqchip_in_kernel(kvm)));
}
static int
cpu_has_secondary_exec_ctrls(void)
{
return (vmcs_config.cpu_based_exec_ctrl &
CPU_BASED_ACTIVATE_SECONDARY_CONTROLS);
}
static int
cpu_has_vmx_virtualize_apic_accesses(void)
{
return (vmcs_config.cpu_based_2nd_exec_ctrl &
SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES);
}
static int
cpu_has_vmx_flexpriority(void)
{
return (cpu_has_vmx_tpr_shadow() &&
cpu_has_vmx_virtualize_apic_accesses());
}
static int
cpu_has_vmx_ept_execute_only(void)
{
return (!!(vmx_capability.ept & VMX_EPT_EXECUTE_ONLY_BIT));
}
static int
cpu_has_vmx_ept_2m_page(void)
{
return (!!(vmx_capability.ept & VMX_EPT_2MB_PAGE_BIT));
}
static int
cpu_has_vmx_ept_1g_page(void)
{
return (!!(vmx_capability.ept & VMX_EPT_1GB_PAGE_BIT));
}
static int
cpu_has_vmx_invept_context(void)
{
return (!!(vmx_capability.ept & VMX_EPT_EXTENT_CONTEXT_BIT));
}
static int
cpu_has_vmx_invept_global(void)
{
return (!!(vmx_capability.ept & VMX_EPT_EXTENT_GLOBAL_BIT));
}
static int
cpu_has_vmx_ept(void)
{
return (vmcs_config.cpu_based_2nd_exec_ctrl &
SECONDARY_EXEC_ENABLE_EPT);
}
static int
cpu_has_vmx_unrestricted_guest(void)
{
return (vmcs_config.cpu_based_2nd_exec_ctrl &
SECONDARY_EXEC_UNRESTRICTED_GUEST);
}
static int
cpu_has_vmx_ple(void)
{
return (vmcs_config.cpu_based_2nd_exec_ctrl &
SECONDARY_EXEC_PAUSE_LOOP_EXITING);
}
static int
vm_need_virtualize_apic_accesses(struct kvm *kvm)
{
return (flexpriority_enabled && irqchip_in_kernel(kvm));
}
static inline int
cpu_has_vmx_vpid(void)
{
return (vmcs_config.cpu_based_2nd_exec_ctrl &
SECONDARY_EXEC_ENABLE_VPID);
}
static int
cpu_has_vmx_rdtscp(void)
{
return (vmcs_config.cpu_based_2nd_exec_ctrl & SECONDARY_EXEC_RDTSCP);
}
static int
cpu_has_virtual_nmis(void)
{
return (vmcs_config.pin_based_exec_ctrl & PIN_BASED_VIRTUAL_NMIS);
}
static int
report_flexpriority(void)
{
return (flexpriority_enabled);
}
static int
__find_msr_index(struct vcpu_vmx *vmx, uint32_t msr)
{
int i;
for (i = 0; i < vmx->nmsrs; i++) {
if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
return (i);
}
return (-1);
}
/* XXX These used to have an __ex around them, maybe add it back? */
static inline void
__invvpid(int ext, uint16_t vpid, gva_t gva)
{
struct {
uint64_t vpid:16;
uint64_t rsvd:48;
uint64_t gva;
} operand = { vpid, 0, gva };
KVM_TRACE2(vmx__invvpid, int, vpid, uint64_t, gva);
/* BEGIN CSTYLED */
__asm__ volatile (ASM_VMX_INVVPID
/* CF==1 or ZF==1 --> rc = -1 */
"; ja 1f ; ud2 ; 1:"
: : "a"(&operand), "c"(ext) : "cc", "memory");
/* END CSTYLED */
}
static inline void
__invept(int ext, uint64_t eptp, gpa_t gpa)
{
struct {
uint64_t eptp, gpa;
} operand = {eptp, gpa};
KVM_TRACE2(vmx__invept, uint64_t, eptp, uint64_t, gpa);
/* BEGIN CSTYLED */
__asm__ volatile (ASM_VMX_INVEPT
/* CF==1 or ZF==1 --> rc = -1 */
"; ja 1f ; ud2 ; 1:\n"
: : "a" (&operand), "c" (ext) : "cc", "memory");
/* END CSTYLED */
}
static struct shared_msr_entry *
find_msr_entry(struct vcpu_vmx *vmx, uint32_t msr)
{
int i;
i = __find_msr_index(vmx, msr);
if (i >= 0)
return (&vmx->guest_msrs[i]);
return (NULL);
}
static void
vmcs_clear(uint64_t vmcs_pa)
{
unsigned char error;
KVM_TRACE1(vmx__vmclear, uint64_t, vmcs_pa);
/*CSTYLED*/
__asm__ volatile (__ex(ASM_VMX_VMCLEAR_RAX) "\n\tsetna %0\n"
: "=g"(error) : "a"(&vmcs_pa), "m"(vmcs_pa)
: "cc", "memory");
if (error)
cmn_err(CE_PANIC, "kvm: vmclear fail: %lx\n", vmcs_pa);
}
static void
__vcpu_clear(void *arg)
{
struct vcpu_vmx *vmx = arg;
int cpu = CPU->cpu_id;
vmx->vmcs->revision_id = vmcs_config.revision_id;
kvm_ringbuf_record(&vmx->vcpu.kvcpu_ringbuf,
KVM_RINGBUF_TAG_VCPUCLEAR, vmx->vcpu.cpu);
if (vmx->vcpu.cpu == cpu)
vmcs_clear(vmx->vmcs_pa);
if (current_vmcs[cpu] == vmx->vmcs)
current_vmcs[cpu] = NULL;
vmx->vcpu.cpu = -1;
vmx->launched = 0;
}
static void
vcpu_clear(struct vcpu_vmx *vmx)
{
if (vmx->vcpu.cpu == -1)
return;
kvm_xcall(vmx->vcpu.cpu, __vcpu_clear, vmx);
}
static void
vpid_sync_vcpu_all(struct vcpu_vmx *vmx)
{
if (vmx->vpid == 0)
return;
__invvpid(VMX_VPID_EXTENT_SINGLE_CONTEXT, vmx->vpid, 0);
}
static void
ept_sync_global(void)
{
if (cpu_has_vmx_invept_global())
__invept(VMX_EPT_EXTENT_GLOBAL, 0, 0);
}
static void
ept_sync_context(uint64_t eptp)
{
if (enable_ept) {
if (cpu_has_vmx_invept_context())
__invept(VMX_EPT_EXTENT_CONTEXT, eptp, 0);
else
ept_sync_global();
}
}
static unsigned long
vmcs_readl(unsigned long field)
{
unsigned long value;
/*CSTYLED*/
__asm__ volatile (ASM_VMX_VMREAD_RDX_RAX
: "=a"(value) : "d"(field) : "cc");
KVM_TRACE2(vmx__vmread, long, field, long, value);
return (value);
}
static uint16_t
vmcs_read16(unsigned long field)
{
return (vmcs_readl(field));
}
static uint32_t
vmcs_read32(unsigned long field)
{
return (vmcs_readl(field));
}
static uint64_t
vmcs_read64(unsigned long field)
{
return (vmcs_readl(field));
}
static void
vmwrite_error(unsigned long field, unsigned long value)
{
cmn_err(CE_WARN, "vmwrite error: reg %lx value %lx (err %x)\n",
field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
}
static void
__vmwrite(unsigned long field, unsigned long value)
{
uint8_t err = 0;
/*CSTYLED*/
__asm__ volatile ( ASM_VMX_VMWRITE_RAX_RDX "\n\t" "setna %0"
/* XXX: CF==1 or ZF==1 --> crash (ud2) */
/* "ja 1f ; ud2 ; 1:\n" */
: "=q"(err) : "a" (value), "d" (field)
: "cc", "memory");
KVM_TRACE3(vmx__vmwrite, long, field,
long, value, uint8_t, err);
/* XXX the following should be ifdef debug... */
if (err) {
#ifdef XXX
vmcs_read32(VM_INSTRUCTION_ERROR);
cmn_err(CE_WARN, "_vmwrite: error writing %lx to %lx: "
"error number = %d\n", value, field, err & 0xff);
#else
XXX_KVM_PROBE;
#endif
}
}
/* XXX Should be static! */
void
vmcs_writel(unsigned long field, unsigned long value)
{
unsigned char error = 0;
#ifndef XXX
/*CSTYLED*/
__asm__ volatile (ASM_VMX_VMWRITE_RAX_RDX "\n\tsetna %0"
: "=q"(error) : "a"(value), "d"(field) : "cc");
if ((error))
vmwrite_error(field, value);
#else
XXX_KVM_PROBE;
__vmwrite(field, value);
#endif
}
static void
vmcs_write16(unsigned long field, uint16_t value)
{
vmcs_writel(field, value);
}
static void
vmcs_write32(unsigned long field, uint32_t value)
{
vmcs_writel(field, value);
}
static void
vmcs_write64(unsigned long field, uint64_t value)
{
vmcs_writel(field, value);
}
static void
vmcs_clear_bits(unsigned long field, uint32_t mask)
{
vmcs_writel(field, vmcs_readl(field) & ~mask);
}
static void
vmcs_set_bits(unsigned long field, uint32_t mask)
{
vmcs_writel(field, vmcs_readl(field) | mask);
}
static void
update_exception_bitmap(struct kvm_vcpu *vcpu)
{
uint32_t eb;
eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
(1u << NM_VECTOR) | (1u << DB_VECTOR) | (1u <<AC_VECTOR);
#ifndef XXX
if ((vcpu->guest_debug &
(KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
(KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
eb |= 1u << BP_VECTOR;
#endif
if (to_vmx(vcpu)->rmode.vm86_active)
eb = ~0;
if (enable_ept)
eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
if (vcpu->fpu_active)
eb &= ~(1u << NM_VECTOR);
vmcs_write32(EXCEPTION_BITMAP, eb);
}
static void
reload_tss(void)
{
/*
* VT restores TR but not its size. Useless.
*/
struct descriptor_table gdt;
struct desc_struct *descs;
kvm_get_gdt(&gdt);
descs = (void *)gdt.base;
descs[GDT_KTSS].c.b.type = 9; /* available TSS */
load_TR_desc();
}
static int
update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
{
uint64_t guest_efer;
uint64_t ignore_bits;
guest_efer = vmx->vcpu.arch.efer;
/*
* NX is emulated; LMA and LME handled by hardware; SCE meaninless
* outside long mode
*/
ignore_bits = EFER_NX | EFER_SCE;
ignore_bits |= EFER_LMA | EFER_LME;
/* SCE is meaningful only in long mode on Intel */
if (guest_efer & EFER_LMA)
ignore_bits &= ~(uint64_t)EFER_SCE;
guest_efer &= ~ignore_bits;
guest_efer |= host_efer & ignore_bits;
vmx->guest_msrs[efer_offset].data = guest_efer;
vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
return (1);
}
static void
vmx_save_host_state(struct kvm_vcpu *vcpu)
{
struct vcpu_vmx *vmx = to_vmx(vcpu);
int i;
if (vmx->host_state.loaded)
return;
vmx->host_state.loaded = 1;
/*
* Set host fs and gs selectors. Unfortunately, 22.2.3 does not
* allow segment selectors with cpl > 0 or ti == 1.
*/
vmx->host_state.ldt_sel = kvm_read_ldt();
vmx->host_state.gs_ldt_reload_needed = vmx->host_state.ldt_sel;
vmx->host_state.fs_sel = kvm_read_fs();
if (!(vmx->host_state.fs_sel & 7)) {
vmcs_write16(HOST_FS_SELECTOR, vmx->host_state.fs_sel);
vmx->host_state.fs_reload_needed = 0;
} else {
vmcs_write16(HOST_FS_SELECTOR, 0);
vmx->host_state.fs_reload_needed = 1;
}
vmx->host_state.gs_sel = kvm_read_gs();
if (!(vmx->host_state.gs_sel & 7))
vmcs_write16(HOST_GS_SELECTOR, vmx->host_state.gs_sel);
else {
vmcs_write16(HOST_GS_SELECTOR, 0);
vmx->host_state.gs_ldt_reload_needed = 1;
}
vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE));
vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
if (is_long_mode(&vmx->vcpu)) {
rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
}
for (i = 0; i < vmx->save_nmsrs; i++) {
kvm_set_shared_msr(vcpu, vmx->guest_msrs[i].index,
vmx->guest_msrs[i].data, vmx->guest_msrs[i].mask);
}
}
static void
__vmx_load_host_state(struct vcpu_vmx *vmx)
{
unsigned long flags;
if (!vmx->host_state.loaded)
return;
KVM_VCPU_KSTAT_INC(&vmx->vcpu, kvmvs_host_state_reload);
vmx->host_state.loaded = 0;
if (vmx->host_state.fs_reload_needed)
kvm_load_fs(vmx->host_state.fs_sel);
if (vmx->host_state.gs_ldt_reload_needed) {
unsigned long gsbase;
kvm_load_ldt(vmx->host_state.ldt_sel);
/*
* If we have to reload GS, we must take care to preserve our
* GSBASE. Note that between the kvm_load_gs() and the
* completion of writing the MSR, GS is essentially in a
* corrupt state -- we cannot allow code to be revectored
* in this window. In particular, this means that we not
* hit a DTrace probe in this window (which will need the
* intact GS to get to the CPU pointer). Both kvm_load_gs()
* and wrmsrl() turn into inlines or non-instrumentable
* leaf routines, but vmcs_readl() has an SDT probe -- so we
* call vmcs_readl() to get the HOST_GS_BASE before the call
* to kvm_load_gs().
*/
cli();
gsbase = vmcs_readl(HOST_GS_BASE);
kvm_load_gs(vmx->host_state.gs_sel);
wrmsrl(MSR_GS_BASE, gsbase);
sti();
}
reload_tss();
if (is_long_mode(&vmx->vcpu)) {
rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
}
}
static void
vmx_load_host_state(struct vcpu_vmx *vmx)
{
kpreempt_disable();
__vmx_load_host_state(vmx);
kpreempt_enable();
}
/*
* Switches to specified vcpu, until a matching vcpu_put(), but assumes
* vcpu mutex is already taken.
*/
static void
vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
{
struct vcpu_vmx *vmx = to_vmx(vcpu);
uint64_t phys_addr = vmx->vmcs_pa;
uint64_t tsc_this, delta, new_offset;
if (vcpu->cpu != cpu) {
vcpu_clear(vmx);
set_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests);
}
if (current_vmcs[cpu] != vmx->vmcs) {
uint8_t error;
kvm_ringbuf_record(&vcpu->kvcpu_ringbuf,
KVM_RINGBUF_TAG_VMPTRLD, (uint64_t)current_vmcs[cpu]);
current_vmcs[cpu] = vmx->vmcs;
KVM_TRACE1(vmx__vmptrld, uint64_t, phys_addr);
/*CSTYLED*/
__asm__ volatile (ASM_VMX_VMPTRLD_RAX "; setna %0"
: "=g"(error) : "a"(&phys_addr), "m"(phys_addr)
: "cc");
}
if (vcpu->cpu != cpu) {
struct descriptor_table dt;
unsigned long sysenter_esp;
kvm_ringbuf_record(&vcpu->kvcpu_ringbuf,
KVM_RINGBUF_TAG_VCPUMIGRATE, vcpu->cpu);
vcpu->cpu = cpu;
/*
* We have a per-CPU TSS, GDT, IDT and GSBASE -- so we reset
* these in the VMCS when switching CPUs.
*/
vmcs_writel(HOST_TR_BASE, kvm_read_tr_base()); /* 22.2.4 */
kvm_get_gdt(&dt);
vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */
vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE));
kvm_get_idt(&dt);
vmcs_writel(HOST_IDTR_BASE, dt.base);
rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
/* We also have a per-CPU %cr3 if we're using kpti */
vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 */
/*
* Make sure that the TSC_OFFSET reflects both this CPU's tick
* delta and the guest's TSC offset.
*/
vmcs_write64(TSC_OFFSET, tsc_gethrtime_tick_delta() +
vcpu->arch.tsc_offset);
}
}
static void
vmx_vcpu_put(struct kvm_vcpu *vcpu)
{
__vmx_load_host_state(to_vmx(vcpu));
}
static void
vmx_fpu_activate(struct kvm_vcpu *vcpu)
{
ulong cr0;
if (vcpu->fpu_active)
return;
vcpu->fpu_active = 1;
cr0 = vmcs_readl(GUEST_CR0);
cr0 &= ~(X86_CR0_TS | X86_CR0_MP);
cr0 |= kvm_read_cr0_bits(vcpu, X86_CR0_TS | X86_CR0_MP);
vmcs_writel(GUEST_CR0, cr0);
update_exception_bitmap(vcpu);
vcpu->arch.cr0_guest_owned_bits = X86_CR0_TS;
vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
}
static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *);
static void
vmx_fpu_deactivate(struct kvm_vcpu *vcpu)
{
vmx_decache_cr0_guest_bits(vcpu);
vmcs_set_bits(GUEST_CR0, X86_CR0_TS | X86_CR0_MP);
update_exception_bitmap(vcpu);
vcpu->arch.cr0_guest_owned_bits = 0;
vmcs_writel(CR0_GUEST_HOST_MASK, ~vcpu->arch.cr0_guest_owned_bits);
vmcs_writel(CR0_READ_SHADOW, vcpu->arch.cr0);
}
static unsigned long
vmx_get_rflags(struct kvm_vcpu *vcpu)
{
unsigned long rflags, save_rflags;
rflags = vmcs_readl(GUEST_RFLAGS);
if (to_vmx(vcpu)->rmode.vm86_active) {
rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
save_rflags = to_vmx(vcpu)->rmode.save_rflags;
rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
}
return (rflags);
}
static void
vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
{
if (to_vmx(vcpu)->rmode.vm86_active) {
to_vmx(vcpu)->rmode.save_rflags = rflags;
rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
}
vmcs_writel(GUEST_RFLAGS, rflags);
}
static uint32_t
vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
{
uint32_t interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
int ret = 0;
if (interruptibility & GUEST_INTR_STATE_STI)
ret |= X86_SHADOW_INT_STI;
if (interruptibility & GUEST_INTR_STATE_MOV_SS)
ret |= X86_SHADOW_INT_MOV_SS;
return (ret & mask);
}
static void
vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
{
uint32_t old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
uint32_t interruptibility = old;
interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
if (mask & X86_SHADOW_INT_MOV_SS)
interruptibility |= GUEST_INTR_STATE_MOV_SS;
if (mask & X86_SHADOW_INT_STI)
interruptibility |= GUEST_INTR_STATE_STI;
if ((interruptibility != old))
vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
}
static void
skip_emulated_instruction(struct kvm_vcpu *vcpu)
{
unsigned long rip;
rip = kvm_rip_read(vcpu);
rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
kvm_rip_write(vcpu, rip);
/* skipping an emulated instruction also counts */
vmx_set_interrupt_shadow(vcpu, 0);
}