xref: /freebsd/sys/amd64/vmm/amd/svm_softc.h (revision 271171e0d97b88ba2a7c3bf750c9672b484c1c13)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2013 Anish Gupta (akgupt3@gmail.com)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef _SVM_SOFTC_H_
32 #define _SVM_SOFTC_H_
33 
34 #include "x86.h"
35 
36 #define SVM_IO_BITMAP_SIZE	(3 * PAGE_SIZE)
37 #define SVM_MSR_BITMAP_SIZE	(2 * PAGE_SIZE)
38 
39 struct asid {
40 	uint64_t	gen;	/* range is [1, ~0UL] */
41 	uint32_t	num;	/* range is [1, nasid - 1] */
42 };
43 
44 /*
45  * XXX separate out 'struct vmcb' from 'svm_vcpu' to avoid wasting space
46  * due to VMCB alignment requirements.
47  */
48 struct svm_vcpu {
49 	struct vmcb	vmcb;	 /* hardware saved vcpu context */
50 	struct svm_regctx swctx; /* software saved vcpu context */
51 	uint64_t	vmcb_pa; /* VMCB physical address */
52 	uint64_t	nextrip; /* next instruction to be executed by guest */
53         int		lastcpu; /* host cpu that the vcpu last ran on */
54 	uint32_t	dirty;	 /* state cache bits that must be cleared */
55 	long		eptgen;	 /* pmap->pm_eptgen when the vcpu last ran */
56 	struct asid	asid;
57 } __aligned(PAGE_SIZE);
58 
59 /*
60  * SVM softc, one per virtual machine.
61  */
62 struct svm_softc {
63 	uint8_t apic_page[VM_MAXCPU][PAGE_SIZE];
64 	struct svm_vcpu vcpu[VM_MAXCPU];
65 	vm_offset_t 	nptp;			    /* nested page table */
66 	uint8_t		*iopm_bitmap;    /* shared by all vcpus */
67 	uint8_t		*msr_bitmap;    /* shared by all vcpus */
68 	struct vm	*vm;
69 	struct vm_mtrr  mtrr[VM_MAXCPU];
70 };
71 
72 CTASSERT((offsetof(struct svm_softc, nptp) & PAGE_MASK) == 0);
73 
74 static __inline struct svm_vcpu *
75 svm_get_vcpu(struct svm_softc *sc, int vcpu)
76 {
77 
78 	return (&(sc->vcpu[vcpu]));
79 }
80 
81 static __inline struct vmcb *
82 svm_get_vmcb(struct svm_softc *sc, int vcpu)
83 {
84 
85 	return (&(sc->vcpu[vcpu].vmcb));
86 }
87 
88 static __inline struct vmcb_state *
89 svm_get_vmcb_state(struct svm_softc *sc, int vcpu)
90 {
91 
92 	return (&(sc->vcpu[vcpu].vmcb.state));
93 }
94 
95 static __inline struct vmcb_ctrl *
96 svm_get_vmcb_ctrl(struct svm_softc *sc, int vcpu)
97 {
98 
99 	return (&(sc->vcpu[vcpu].vmcb.ctrl));
100 }
101 
102 static __inline struct svm_regctx *
103 svm_get_guest_regctx(struct svm_softc *sc, int vcpu)
104 {
105 
106 	return (&(sc->vcpu[vcpu].swctx));
107 }
108 
109 static __inline void
110 svm_set_dirty(struct svm_softc *sc, int vcpu, uint32_t dirtybits)
111 {
112         struct svm_vcpu *vcpustate;
113 
114         vcpustate = svm_get_vcpu(sc, vcpu);
115 
116         vcpustate->dirty |= dirtybits;
117 }
118 
119 #endif /* _SVM_SOFTC_H_ */
120