/
vmm.c
9132 lines (8105 loc) · 239 KB
/
vmm.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
/* $OpenBSD: vmm.c,v 1.298 2021/12/07 07:58:56 anton 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 <sys/param.h>
#include <sys/systm.h>
#include <sys/signalvar.h>
#include <sys/malloc.h>
#include <sys/device.h>
#include <sys/pool.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/ioctl.h>
#include <sys/queue.h>
#include <sys/rwlock.h>
#include <sys/pledge.h>
#include <sys/memrange.h>
#include <sys/tracepoint.h>
#include <uvm/uvm_extern.h>
#include <machine/fpu.h>
#include <machine/pmap.h>
#include <machine/biosvar.h>
#include <machine/segments.h>
#include <machine/cpufunc.h>
#include <machine/vmmvar.h>
#include <dev/isa/isareg.h>
#include <dev/pv/pvreg.h>
/* #define VMM_DEBUG */
void *l1tf_flush_region;
#ifdef VMM_DEBUG
#define DPRINTF(x...) do { printf(x); } while(0)
#else
#define DPRINTF(x...)
#endif /* VMM_DEBUG */
#define DEVNAME(s) ((s)->sc_dev.dv_xname)
#define CTRL_DUMP(x,y,z) printf(" %s: Can set:%s Can clear:%s\n", #z , \
vcpu_vmx_check_cap(x, IA32_VMX_##y ##_CTLS, \
IA32_VMX_##z, 1) ? "Yes" : "No", \
vcpu_vmx_check_cap(x, IA32_VMX_##y ##_CTLS, \
IA32_VMX_##z, 0) ? "Yes" : "No");
#define VMX_EXIT_INFO_HAVE_RIP 0x1
#define VMX_EXIT_INFO_HAVE_REASON 0x2
#define VMX_EXIT_INFO_COMPLETE \
(VMX_EXIT_INFO_HAVE_RIP | VMX_EXIT_INFO_HAVE_REASON)
struct vm {
struct vmspace *vm_vmspace;
vm_map_t vm_map;
uint32_t vm_id;
pid_t vm_creator_pid;
size_t vm_nmemranges;
size_t vm_memory_size;
char vm_name[VMM_MAX_NAME_LEN];
struct vm_mem_range vm_memranges[VMM_MAX_MEM_RANGES];
struct vcpu_head vm_vcpu_list;
uint32_t vm_vcpu_ct;
u_int vm_vcpus_running;
struct rwlock vm_vcpu_lock;
SLIST_ENTRY(vm) vm_link;
};
SLIST_HEAD(vmlist_head, vm);
struct vmm_softc {
struct device sc_dev;
/* Capabilities */
uint32_t nr_vmx_cpus;
uint32_t nr_svm_cpus;
uint32_t nr_rvi_cpus;
uint32_t nr_ept_cpus;
/* Managed VMs */
struct vmlist_head vm_list;
int mode;
size_t vcpu_ct;
size_t vcpu_max;
struct rwlock vm_lock;
size_t vm_ct; /* number of in-memory VMs */
size_t vm_idx; /* next unique VM index */
struct rwlock vpid_lock;
uint16_t max_vpid;
uint8_t vpids[512]; /* bitmap of used VPID/ASIDs */
};
void vmx_dump_vmcs_field(uint16_t, const char *);
int vmm_enabled(void);
int vmm_probe(struct device *, void *, void *);
void vmm_attach(struct device *, struct device *, void *);
int vmmopen(dev_t, int, int, struct proc *);
int vmmioctl(dev_t, u_long, caddr_t, int, struct proc *);
int vmmclose(dev_t, int, int, struct proc *);
int vmm_start(void);
int vmm_stop(void);
size_t vm_create_check_mem_ranges(struct vm_create_params *);
int vm_create(struct vm_create_params *, struct proc *);
int vm_run(struct vm_run_params *);
int vm_terminate(struct vm_terminate_params *);
int vm_get_info(struct vm_info_params *);
int vm_resetcpu(struct vm_resetcpu_params *);
int vm_intr_pending(struct vm_intr_params *);
int vm_rwregs(struct vm_rwregs_params *, int);
int vm_mprotect_ept(struct vm_mprotect_ept_params *);
int vm_rwvmparams(struct vm_rwvmparams_params *, int);
int vm_find(uint32_t, struct vm **);
int vcpu_readregs_vmx(struct vcpu *, uint64_t, struct vcpu_reg_state *);
int vcpu_readregs_svm(struct vcpu *, uint64_t, struct vcpu_reg_state *);
int vcpu_writeregs_vmx(struct vcpu *, uint64_t, int, struct vcpu_reg_state *);
int vcpu_writeregs_svm(struct vcpu *, uint64_t, struct vcpu_reg_state *);
int vcpu_reset_regs(struct vcpu *, struct vcpu_reg_state *);
int vcpu_reset_regs_vmx(struct vcpu *, struct vcpu_reg_state *);
int vcpu_reset_regs_svm(struct vcpu *, struct vcpu_reg_state *);
int vcpu_reload_vmcs_vmx(struct vcpu *);
int vcpu_init(struct vcpu *);
int vcpu_init_vmx(struct vcpu *);
int vcpu_init_svm(struct vcpu *);
int vcpu_must_stop(struct vcpu *);
int vcpu_run_vmx(struct vcpu *, struct vm_run_params *);
int vcpu_run_svm(struct vcpu *, struct vm_run_params *);
void vcpu_deinit(struct vcpu *);
void vcpu_deinit_vmx(struct vcpu *);
void vcpu_deinit_svm(struct vcpu *);
int vm_impl_init(struct vm *, struct proc *);
int vm_impl_init_vmx(struct vm *, struct proc *);
int vm_impl_init_svm(struct vm *, struct proc *);
void vm_impl_deinit(struct vm *);
void vm_impl_deinit_vmx(struct vm *);
void vm_impl_deinit_svm(struct vm *);
void vm_teardown(struct vm *);
int vcpu_vmx_check_cap(struct vcpu *, uint32_t, uint32_t, int);
int vcpu_vmx_compute_ctrl(uint64_t, uint16_t, uint32_t, uint32_t, uint32_t *);
int vmx_get_exit_info(uint64_t *, uint64_t *);
int vmx_load_pdptes(struct vcpu *);
int vmx_handle_exit(struct vcpu *);
int svm_handle_exit(struct vcpu *);
int svm_handle_msr(struct vcpu *);
int vmm_handle_xsetbv(struct vcpu *, uint64_t *);
int vmx_handle_xsetbv(struct vcpu *);
int svm_handle_xsetbv(struct vcpu *);
int vmm_handle_cpuid(struct vcpu *);
int vmx_handle_rdmsr(struct vcpu *);
int vmx_handle_wrmsr(struct vcpu *);
int vmx_handle_cr0_write(struct vcpu *, uint64_t);
int vmx_handle_cr4_write(struct vcpu *, uint64_t);
int vmx_handle_cr(struct vcpu *);
int svm_handle_inout(struct vcpu *);
int vmx_handle_inout(struct vcpu *);
int svm_handle_hlt(struct vcpu *);
int vmx_handle_hlt(struct vcpu *);
int vmm_inject_ud(struct vcpu *);
int vmm_inject_gp(struct vcpu *);
int vmm_inject_db(struct vcpu *);
void vmx_handle_intr(struct vcpu *);
void vmx_handle_intwin(struct vcpu *);
void vmx_handle_misc_enable_msr(struct vcpu *);
int vmm_get_guest_memtype(struct vm *, paddr_t);
int vmx_get_guest_faulttype(void);
int svm_get_guest_faulttype(struct vmcb *);
int vmx_get_exit_qualification(uint64_t *);
int vmm_get_guest_cpu_cpl(struct vcpu *);
int vmm_get_guest_cpu_mode(struct vcpu *);
int svm_fault_page(struct vcpu *, paddr_t);
int vmx_fault_page(struct vcpu *, paddr_t);
int vmx_handle_np_fault(struct vcpu *);
int svm_handle_np_fault(struct vcpu *);
int vmx_mprotect_ept(vm_map_t, paddr_t, paddr_t, int);
pt_entry_t *vmx_pmap_find_pte_ept(pmap_t, paddr_t);
int vmm_alloc_vpid(uint16_t *);
void vmm_free_vpid(uint16_t);
const char *vcpu_state_decode(u_int);
const char *vmx_exit_reason_decode(uint32_t);
const char *svm_exit_reason_decode(uint32_t);
const char *vmx_instruction_error_decode(uint32_t);
void svm_setmsrbr(struct vcpu *, uint32_t);
void svm_setmsrbw(struct vcpu *, uint32_t);
void svm_setmsrbrw(struct vcpu *, uint32_t);
void vmx_setmsrbr(struct vcpu *, uint32_t);
void vmx_setmsrbw(struct vcpu *, uint32_t);
void vmx_setmsrbrw(struct vcpu *, uint32_t);
void svm_set_clean(struct vcpu *, uint32_t);
void svm_set_dirty(struct vcpu *, uint32_t);
int vmm_gpa_is_valid(struct vcpu *vcpu, paddr_t gpa, size_t obj_size);
void vmm_init_pvclock(struct vcpu *, paddr_t);
int vmm_update_pvclock(struct vcpu *);
int vmm_pat_is_valid(uint64_t);
#ifdef MULTIPROCESSOR
static int vmx_remote_vmclear(struct cpu_info*, struct vcpu *);
#endif
#ifdef VMM_DEBUG
void dump_vcpu(struct vcpu *);
void vmx_vcpu_dump_regs(struct vcpu *);
void vmx_dump_vmcs(struct vcpu *);
const char *msr_name_decode(uint32_t);
void vmm_segment_desc_decode(uint64_t);
void vmm_decode_cr0(uint64_t);
void vmm_decode_cr3(uint64_t);
void vmm_decode_cr4(uint64_t);
void vmm_decode_msr_value(uint64_t, uint64_t);
void vmm_decode_apicbase_msr_value(uint64_t);
void vmm_decode_ia32_fc_value(uint64_t);
void vmm_decode_mtrrcap_value(uint64_t);
void vmm_decode_perf_status_value(uint64_t);
void vmm_decode_perf_ctl_value(uint64_t);
void vmm_decode_mtrrdeftype_value(uint64_t);
void vmm_decode_efer_value(uint64_t);
void vmm_decode_rflags(uint64_t);
void vmm_decode_misc_enable_value(uint64_t);
const char *vmm_decode_cpu_mode(struct vcpu *);
extern int mtrr2mrt(int);
struct vmm_reg_debug_info {
uint64_t vrdi_bit;
const char *vrdi_present;
const char *vrdi_absent;
};
#endif /* VMM_DEBUG */
extern uint64_t tsc_frequency;
extern int tsc_is_invariant;
const char *vmm_hv_signature = VMM_HV_SIGNATURE;
const struct kmem_pa_mode vmm_kp_contig = {
.kp_constraint = &no_constraint,
.kp_maxseg = 1,
.kp_align = 4096,
.kp_zero = 1,
};
struct cfdriver vmm_cd = {
NULL, "vmm", DV_DULL, CD_SKIPHIBERNATE
};
const struct cfattach vmm_ca = {
sizeof(struct vmm_softc), vmm_probe, vmm_attach, NULL, NULL
};
/*
* Helper struct to easily get the VMCS field IDs needed in vmread/vmwrite
* to access the individual fields of the guest segment registers. This
* struct is indexed by VCPU_REGS_* id.
*/
const struct {
uint64_t selid;
uint64_t limitid;
uint64_t arid;
uint64_t baseid;
} vmm_vmx_sreg_vmcs_fields[] = {
{ VMCS_GUEST_IA32_CS_SEL, VMCS_GUEST_IA32_CS_LIMIT,
VMCS_GUEST_IA32_CS_AR, VMCS_GUEST_IA32_CS_BASE },
{ VMCS_GUEST_IA32_DS_SEL, VMCS_GUEST_IA32_DS_LIMIT,
VMCS_GUEST_IA32_DS_AR, VMCS_GUEST_IA32_DS_BASE },
{ VMCS_GUEST_IA32_ES_SEL, VMCS_GUEST_IA32_ES_LIMIT,
VMCS_GUEST_IA32_ES_AR, VMCS_GUEST_IA32_ES_BASE },
{ VMCS_GUEST_IA32_FS_SEL, VMCS_GUEST_IA32_FS_LIMIT,
VMCS_GUEST_IA32_FS_AR, VMCS_GUEST_IA32_FS_BASE },
{ VMCS_GUEST_IA32_GS_SEL, VMCS_GUEST_IA32_GS_LIMIT,
VMCS_GUEST_IA32_GS_AR, VMCS_GUEST_IA32_GS_BASE },
{ VMCS_GUEST_IA32_SS_SEL, VMCS_GUEST_IA32_SS_LIMIT,
VMCS_GUEST_IA32_SS_AR, VMCS_GUEST_IA32_SS_BASE },
{ VMCS_GUEST_IA32_LDTR_SEL, VMCS_GUEST_IA32_LDTR_LIMIT,
VMCS_GUEST_IA32_LDTR_AR, VMCS_GUEST_IA32_LDTR_BASE },
{ VMCS_GUEST_IA32_TR_SEL, VMCS_GUEST_IA32_TR_LIMIT,
VMCS_GUEST_IA32_TR_AR, VMCS_GUEST_IA32_TR_BASE }
};
/* Pools for VMs and VCPUs */
struct pool vm_pool;
struct pool vcpu_pool;
struct vmm_softc *vmm_softc;
/* IDT information used when populating host state area */
extern vaddr_t idt_vaddr;
extern struct gate_descriptor *idt;
/* Constants used in "CR access exit" */
#define CR_WRITE 0
#define CR_READ 1
#define CR_CLTS 2
#define CR_LMSW 3
/*
* vmm_enabled
*
* Checks if we have at least one CPU with either VMX or SVM.
* Returns 1 if we have at least one of either type, but not both, 0 otherwise.
*/
int
vmm_enabled(void)
{
struct cpu_info *ci;
CPU_INFO_ITERATOR cii;
int found_vmx = 0, found_svm = 0;
/* Check if we have at least one CPU with either VMX or SVM */
CPU_INFO_FOREACH(cii, ci) {
if (ci->ci_vmm_flags & CI_VMM_VMX)
found_vmx = 1;
if (ci->ci_vmm_flags & CI_VMM_SVM)
found_svm = 1;
}
/* Don't support both SVM and VMX at the same time */
if (found_vmx && found_svm)
return (0);
if (found_vmx || found_svm)
return 1;
return 0;
}
int
vmm_probe(struct device *parent, void *match, void *aux)
{
const char **busname = (const char **)aux;
if (strcmp(*busname, vmm_cd.cd_name) != 0)
return (0);
return (1);
}
/*
* vmm_attach
*
* Calculates how many of each type of CPU we have, prints this into dmesg
* during attach. Initializes various locks, pools, and list structures for the
* VMM.
*/
void
vmm_attach(struct device *parent, struct device *self, void *aux)
{
struct vmm_softc *sc = (struct vmm_softc *)self;
struct cpu_info *ci;
CPU_INFO_ITERATOR cii;
sc->nr_vmx_cpus = 0;
sc->nr_svm_cpus = 0;
sc->nr_rvi_cpus = 0;
sc->nr_ept_cpus = 0;
sc->vcpu_ct = 0;
sc->vm_ct = 0;
sc->vm_idx = 0;
/* Calculate CPU features */
CPU_INFO_FOREACH(cii, ci) {
if (ci->ci_vmm_flags & CI_VMM_VMX)
sc->nr_vmx_cpus++;
if (ci->ci_vmm_flags & CI_VMM_SVM)
sc->nr_svm_cpus++;
if (ci->ci_vmm_flags & CI_VMM_RVI)
sc->nr_rvi_cpus++;
if (ci->ci_vmm_flags & CI_VMM_EPT)
sc->nr_ept_cpus++;
}
SLIST_INIT(&sc->vm_list);
rw_init(&sc->vm_lock, "vm_list");
if (sc->nr_ept_cpus) {
printf(": VMX/EPT");
sc->mode = VMM_MODE_EPT;
} else if (sc->nr_vmx_cpus) {
printf(": VMX");
sc->mode = VMM_MODE_VMX;
} else if (sc->nr_rvi_cpus) {
printf(": SVM/RVI");
sc->mode = VMM_MODE_RVI;
} else if (sc->nr_svm_cpus) {
printf(": SVM");
sc->mode = VMM_MODE_SVM;
} else {
printf(": unknown");
sc->mode = VMM_MODE_UNKNOWN;
}
if (sc->mode == VMM_MODE_EPT || sc->mode == VMM_MODE_VMX) {
if (!(curcpu()->ci_vmm_cap.vcc_vmx.vmx_has_l1_flush_msr)) {
l1tf_flush_region = km_alloc(VMX_L1D_FLUSH_SIZE,
&kv_any, &vmm_kp_contig, &kd_waitok);
if (!l1tf_flush_region) {
printf(" (failing, no memory)");
sc->mode = VMM_MODE_UNKNOWN;
} else {
printf(" (using slow L1TF mitigation)");
memset(l1tf_flush_region, 0xcc,
VMX_L1D_FLUSH_SIZE);
}
}
}
printf("\n");
if (sc->mode == VMM_MODE_SVM || sc->mode == VMM_MODE_RVI) {
sc->max_vpid = curcpu()->ci_vmm_cap.vcc_svm.svm_max_asid;
} else {
sc->max_vpid = 0xFFF;
}
bzero(&sc->vpids, sizeof(sc->vpids));
rw_init(&sc->vpid_lock, "vpid");
pool_init(&vm_pool, sizeof(struct vm), 0, IPL_MPFLOOR, PR_WAITOK,
"vmpool", NULL);
pool_init(&vcpu_pool, sizeof(struct vcpu), 64, IPL_MPFLOOR, PR_WAITOK,
"vcpupl", NULL);
vmm_softc = sc;
}
/*
* vmmopen
*
* Called during open of /dev/vmm.
*
* Parameters:
* dev, flag, mode, p: These come from the character device and are
* all unused for this function
*
* Return values:
* ENODEV: if vmm(4) didn't attach or no supported CPUs detected
* 0: successful open
*/
int
vmmopen(dev_t dev, int flag, int mode, struct proc *p)
{
/* Don't allow open if we didn't attach */
if (vmm_softc == NULL)
return (ENODEV);
/* Don't allow open if we didn't detect any supported CPUs */
if (vmm_softc->mode != VMM_MODE_EPT && vmm_softc->mode != VMM_MODE_RVI)
return (ENODEV);
return 0;
}
/*
* vmmioctl
*
* Main ioctl dispatch routine for /dev/vmm. Parses ioctl type and calls
* appropriate lower level handler routine. Returns result to ioctl caller.
*/
int
vmmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
{
int ret;
KERNEL_UNLOCK();
switch (cmd) {
case VMM_IOC_CREATE:
if ((ret = vmm_start()) != 0) {
vmm_stop();
break;
}
ret = vm_create((struct vm_create_params *)data, p);
break;
case VMM_IOC_RUN:
ret = vm_run((struct vm_run_params *)data);
break;
case VMM_IOC_INFO:
ret = vm_get_info((struct vm_info_params *)data);
break;
case VMM_IOC_TERM:
ret = vm_terminate((struct vm_terminate_params *)data);
break;
case VMM_IOC_RESETCPU:
ret = vm_resetcpu((struct vm_resetcpu_params *)data);
break;
case VMM_IOC_INTR:
ret = vm_intr_pending((struct vm_intr_params *)data);
break;
case VMM_IOC_READREGS:
ret = vm_rwregs((struct vm_rwregs_params *)data, 0);
break;
case VMM_IOC_WRITEREGS:
ret = vm_rwregs((struct vm_rwregs_params *)data, 1);
break;
case VMM_IOC_MPROTECT_EPT:
ret = vm_mprotect_ept((struct vm_mprotect_ept_params *)data);
break;
case VMM_IOC_READVMPARAMS:
ret = vm_rwvmparams((struct vm_rwvmparams_params *)data, 0);
break;
case VMM_IOC_WRITEVMPARAMS:
ret = vm_rwvmparams((struct vm_rwvmparams_params *)data, 1);
break;
default:
DPRINTF("%s: unknown ioctl code 0x%lx\n", __func__, cmd);
ret = ENOTTY;
}
KERNEL_LOCK();
return (ret);
}
/*
* pledge_ioctl_vmm
*
* Restrict the allowed ioctls in a pledged process context.
* Is called from pledge_ioctl().
*/
int
pledge_ioctl_vmm(struct proc *p, long com)
{
switch (com) {
case VMM_IOC_CREATE:
case VMM_IOC_INFO:
/* The "parent" process in vmd forks and manages VMs */
if (p->p_p->ps_pledge & PLEDGE_PROC)
return (0);
break;
case VMM_IOC_TERM:
/* XXX VM processes should only terminate themselves */
case VMM_IOC_RUN:
case VMM_IOC_RESETCPU:
case VMM_IOC_INTR:
case VMM_IOC_READREGS:
case VMM_IOC_WRITEREGS:
case VMM_IOC_MPROTECT_EPT:
case VMM_IOC_READVMPARAMS:
case VMM_IOC_WRITEVMPARAMS:
return (0);
}
return (EPERM);
}
/*
* vmmclose
*
* Called when /dev/vmm is closed. Presently unused.
*/
int
vmmclose(dev_t dev, int flag, int mode, struct proc *p)
{
return 0;
}
/*
* vm_find_vcpu
*
* Lookup VMM VCPU by ID number
*
* Parameters:
* vm: vm structure
* id: index id of vcpu
*
* Returns pointer to vcpu structure if successful, NULL otherwise
*/
static struct vcpu *
vm_find_vcpu(struct vm *vm, uint32_t id)
{
struct vcpu *vcpu;
if (vm == NULL)
return NULL;
rw_enter_read(&vm->vm_vcpu_lock);
SLIST_FOREACH(vcpu, &vm->vm_vcpu_list, vc_vcpu_link) {
if (vcpu->vc_id == id)
break;
}
rw_exit_read(&vm->vm_vcpu_lock);
return vcpu;
}
/*
* vm_resetcpu
*
* Resets the vcpu defined in 'vrp' to power-on-init register state
*
* Parameters:
* vrp: ioctl structure defining the vcpu to reset (see vmmvar.h)
*
* Returns 0 if successful, or various error codes on failure:
* ENOENT if the VM id contained in 'vrp' refers to an unknown VM or
* if vrp describes an unknown vcpu for this VM
* EBUSY if the indicated VCPU is not stopped
* EIO if the indicated VCPU failed to reset
*/
int
vm_resetcpu(struct vm_resetcpu_params *vrp)
{
struct vm *vm;
struct vcpu *vcpu;
int error;
/* Find the desired VM */
rw_enter_read(&vmm_softc->vm_lock);
error = vm_find(vrp->vrp_vm_id, &vm);
rw_exit_read(&vmm_softc->vm_lock);
/* Not found? exit. */
if (error != 0) {
DPRINTF("%s: vm id %u not found\n", __func__,
vrp->vrp_vm_id);
return (error);
}
vcpu = vm_find_vcpu(vm, vrp->vrp_vcpu_id);
if (vcpu == NULL) {
DPRINTF("%s: vcpu id %u of vm %u not found\n", __func__,
vrp->vrp_vcpu_id, vrp->vrp_vm_id);
return (ENOENT);
}
rw_enter_write(&vcpu->vc_lock);
if (vcpu->vc_state != VCPU_STATE_STOPPED) {
DPRINTF("%s: reset of vcpu %u on vm %u attempted "
"while vcpu was in state %u (%s)\n", __func__,
vrp->vrp_vcpu_id, vrp->vrp_vm_id, vcpu->vc_state,
vcpu_state_decode(vcpu->vc_state));
rw_exit_write(&vcpu->vc_lock);
return (EBUSY);
}
DPRINTF("%s: resetting vm %d vcpu %d to power on defaults\n", __func__,
vm->vm_id, vcpu->vc_id);
if (vcpu_reset_regs(vcpu, &vrp->vrp_init_state)) {
printf("%s: failed\n", __func__);
#ifdef VMM_DEBUG
dump_vcpu(vcpu);
#endif /* VMM_DEBUG */
rw_exit_write(&vcpu->vc_lock);
return (EIO);
}
rw_exit_write(&vcpu->vc_lock);
return (0);
}
/*
* vm_intr_pending
*
* IOCTL handler routine for VMM_IOC_INTR messages, sent from vmd when an
* interrupt is pending and needs acknowledgment
*
* Parameters:
* vip: Describes the vm/vcpu for which the interrupt is pending
*
* Return values:
* 0: if successful
* ENOENT: if the VM/VCPU defined by 'vip' cannot be found
*/
int
vm_intr_pending(struct vm_intr_params *vip)
{
struct vm *vm;
struct vcpu *vcpu;
int error;
/* Find the desired VM */
rw_enter_read(&vmm_softc->vm_lock);
error = vm_find(vip->vip_vm_id, &vm);
/* Not found? exit. */
if (error != 0) {
rw_exit_read(&vmm_softc->vm_lock);
return (error);
}
vcpu = vm_find_vcpu(vm, vip->vip_vcpu_id);
rw_exit_read(&vmm_softc->vm_lock);
if (vcpu == NULL)
return (ENOENT);
rw_enter_write(&vcpu->vc_lock);
vcpu->vc_intr = vip->vip_intr;
rw_exit_write(&vcpu->vc_lock);
return (0);
}
/*
* vm_rwvmparams
*
* IOCTL handler to read/write the current vmm params like pvclock gpa, pvclock
* version, etc.
*
* Parameters:
* vrwp: Describes the VM and VCPU to get/set the params from
* dir: 0 for reading, 1 for writing
*
* Return values:
* 0: if successful
* ENOENT: if the VM/VCPU defined by 'vpp' cannot be found
* EINVAL: if an error occurred reading the registers of the guest
*/
int
vm_rwvmparams(struct vm_rwvmparams_params *vpp, int dir) {
struct vm *vm;
struct vcpu *vcpu;
int error;
/* Find the desired VM */
rw_enter_read(&vmm_softc->vm_lock);
error = vm_find(vpp->vpp_vm_id, &vm);
/* Not found? exit. */
if (error != 0) {
rw_exit_read(&vmm_softc->vm_lock);
return (error);
}
vcpu = vm_find_vcpu(vm, vpp->vpp_vcpu_id);
rw_exit_read(&vmm_softc->vm_lock);
if (vcpu == NULL)
return (ENOENT);
if (dir == 0) {
if (vpp->vpp_mask & VM_RWVMPARAMS_PVCLOCK_VERSION)
vpp->vpp_pvclock_version = vcpu->vc_pvclock_version;
if (vpp->vpp_mask & VM_RWVMPARAMS_PVCLOCK_SYSTEM_GPA)
vpp->vpp_pvclock_system_gpa = \
vcpu->vc_pvclock_system_gpa;
return (0);
}
if (vpp->vpp_mask & VM_RWVMPARAMS_PVCLOCK_VERSION)
vcpu->vc_pvclock_version = vpp->vpp_pvclock_version;
if (vpp->vpp_mask & VM_RWVMPARAMS_PVCLOCK_SYSTEM_GPA) {
vmm_init_pvclock(vcpu, vpp->vpp_pvclock_system_gpa);
}
return (0);
}
/*
* vm_readregs
*
* IOCTL handler to read/write the current register values of a guest VCPU.
* The VCPU must not be running.
*
* Parameters:
* vrwp: Describes the VM and VCPU to get/set the registers from. The
* register values are returned here as well.
* dir: 0 for reading, 1 for writing
*
* Return values:
* 0: if successful
* ENOENT: if the VM/VCPU defined by 'vrwp' cannot be found
* EINVAL: if an error occurred accessing the registers of the guest
* EPERM: if the vm cannot be accessed from the calling process
*/
int
vm_rwregs(struct vm_rwregs_params *vrwp, int dir)
{
struct vm *vm;
struct vcpu *vcpu;
struct vcpu_reg_state *vrs = &vrwp->vrwp_regs;
int error, ret;
/* Find the desired VM */
rw_enter_read(&vmm_softc->vm_lock);
error = vm_find(vrwp->vrwp_vm_id, &vm);
/* Not found? exit. */
if (error != 0) {
rw_exit_read(&vmm_softc->vm_lock);
return (error);
}
vcpu = vm_find_vcpu(vm, vrwp->vrwp_vcpu_id);
rw_exit_read(&vmm_softc->vm_lock);
if (vcpu == NULL)
return (ENOENT);
rw_enter_write(&vcpu->vc_lock);
if (vmm_softc->mode == VMM_MODE_VMX ||
vmm_softc->mode == VMM_MODE_EPT)
ret = (dir == 0) ?
vcpu_readregs_vmx(vcpu, vrwp->vrwp_mask, vrs) :
vcpu_writeregs_vmx(vcpu, vrwp->vrwp_mask, 1, vrs);
else if (vmm_softc->mode == VMM_MODE_SVM ||
vmm_softc->mode == VMM_MODE_RVI)
ret = (dir == 0) ?
vcpu_readregs_svm(vcpu, vrwp->vrwp_mask, vrs) :
vcpu_writeregs_svm(vcpu, vrwp->vrwp_mask, vrs);
else {
DPRINTF("%s: unknown vmm mode", __func__);
ret = EINVAL;
}
rw_exit_write(&vcpu->vc_lock);
return (ret);
}
/*
* vm_mprotect_ept
*
* IOCTL handler to sets the access protections of the ept
*
* Parameters:
* vmep: describes the memory for which the protect will be applied..
*
* Return values:
* 0: if successful
* ENOENT: if the VM defined by 'vmep' cannot be found
* EINVAL: if the sgpa or size is not page aligned, the prot is invalid,
* size is too large (512GB), there is wraparound
* (like start = 512GB-1 and end = 512GB-2),
* the address specified is not within the vm's mem range
* or the address lies inside reserved (MMIO) memory
*/
int
vm_mprotect_ept(struct vm_mprotect_ept_params *vmep)
{
struct vm *vm;
struct vcpu *vcpu;
vaddr_t sgpa;
size_t size;
vm_prot_t prot;
uint64_t msr;
int ret, memtype;
/* If not EPT or RVI, nothing to do here */
if (!(vmm_softc->mode == VMM_MODE_EPT
|| vmm_softc->mode == VMM_MODE_RVI))
return (0);
/* Find the desired VM */
rw_enter_read(&vmm_softc->vm_lock);
ret = vm_find(vmep->vmep_vm_id, &vm);
rw_exit_read(&vmm_softc->vm_lock);
/* Not found? exit. */
if (ret != 0) {
DPRINTF("%s: vm id %u not found\n", __func__,
vmep->vmep_vm_id);
return (ret);
}
vcpu = vm_find_vcpu(vm, vmep->vmep_vcpu_id);
if (vcpu == NULL) {
DPRINTF("%s: vcpu id %u of vm %u not found\n", __func__,
vmep->vmep_vcpu_id, vmep->vmep_vm_id);
return (ENOENT);
}
if (vcpu->vc_state != VCPU_STATE_STOPPED) {
DPRINTF("%s: mprotect_ept %u on vm %u attempted "
"while vcpu was in state %u (%s)\n", __func__,
vmep->vmep_vcpu_id, vmep->vmep_vm_id, vcpu->vc_state,
vcpu_state_decode(vcpu->vc_state));
return (EBUSY);
}
/* Only proceed if the pmap is in the correct mode */
KASSERT((vmm_softc->mode == VMM_MODE_EPT &&
vm->vm_map->pmap->pm_type == PMAP_TYPE_EPT) ||
(vmm_softc->mode == VMM_MODE_RVI &&
vm->vm_map->pmap->pm_type == PMAP_TYPE_RVI));
sgpa = vmep->vmep_sgpa;
size = vmep->vmep_size;
prot = vmep->vmep_prot;
/* No W^X permissions */
if ((prot & PROT_MASK) != prot &&
(prot & (PROT_WRITE | PROT_EXEC)) == (PROT_WRITE | PROT_EXEC)) {
DPRINTF("%s: W+X permission requested\n", __func__);
return (EINVAL);
}
/* No Write only permissions */
if ((prot & (PROT_READ | PROT_WRITE | PROT_EXEC)) == PROT_WRITE) {
DPRINTF("%s: No Write only permissions\n", __func__);
return (EINVAL);
}
/* No empty permissions */
if (prot == 0) {
DPRINTF("%s: No empty permissions\n", __func__);
return (EINVAL);
}
/* No execute only on EPT CPUs that don't have that capability */
if (vmm_softc->mode == VMM_MODE_EPT) {
msr = rdmsr(IA32_VMX_EPT_VPID_CAP);
if (prot == PROT_EXEC &&
(msr & IA32_EPT_VPID_CAP_XO_TRANSLATIONS) == 0) {
DPRINTF("%s: Execute only permissions unsupported,"
" adding read permission\n", __func__);
prot |= PROT_READ;
}
}
/* Must be page aligned */
if ((sgpa & PAGE_MASK) || (size & PAGE_MASK) || size == 0)
return (EINVAL);
/* size must be less then 512GB */
if (size >= NBPD_L4)
return (EINVAL);
/* no wraparound */
if (sgpa + size < sgpa)
return (EINVAL);
/*
* Specifying addresses within the PCI MMIO space is forbidden.
* Disallow addresses that start inside the MMIO space:
* [VMM_PCI_MMIO_BAR_BASE .. VMM_PCI_MMIO_BAR_END]
*/
if (sgpa >= VMM_PCI_MMIO_BAR_BASE && sgpa <= VMM_PCI_MMIO_BAR_END)
return (EINVAL);
/*
* ... and disallow addresses that end inside the MMIO space:
* (VMM_PCI_MMIO_BAR_BASE .. VMM_PCI_MMIO_BAR_END]
*/
if (sgpa + size > VMM_PCI_MMIO_BAR_BASE &&
sgpa + size <= VMM_PCI_MMIO_BAR_END)
return (EINVAL);
memtype = vmm_get_guest_memtype(vm, sgpa);
if (memtype == VMM_MEM_TYPE_UNKNOWN)
return (EINVAL);
if (vmm_softc->mode == VMM_MODE_EPT)
ret = vmx_mprotect_ept(vm->vm_map, sgpa, sgpa + size, prot);
else if (vmm_softc->mode == VMM_MODE_RVI) {
pmap_write_protect(vm->vm_map->pmap, sgpa, sgpa + size, prot);
/* XXX requires a invlpga */
ret = 0;
} else
return (EINVAL);
return (ret);
}
/*
* vmx_mprotect_ept
*
* apply the ept protections to the requested pages, faulting in the page if
* required.
*/
int
vmx_mprotect_ept(vm_map_t vm_map, paddr_t sgpa, paddr_t egpa, int prot)
{
struct vmx_invept_descriptor vid;
pmap_t pmap;
pt_entry_t *pte;
paddr_t addr;
int ret = 0;
pmap = vm_map->pmap;
KERNEL_LOCK();
for (addr = sgpa; addr < egpa; addr += PAGE_SIZE) {
pte = vmx_pmap_find_pte_ept(pmap, addr);
if (pte == NULL) {
ret = uvm_fault(vm_map, addr, VM_FAULT_WIRE,