xref: /freebsd/sys/amd64/vmm/amd/svm_softc.h (revision e32fecd0c2c3ee37c47ee100f169e7eb0282a873)
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 svm_softc;
40 
41 struct asid {
42 	uint64_t	gen;	/* range is [1, ~0UL] */
43 	uint32_t	num;	/* range is [1, nasid - 1] */
44 };
45 
46 struct svm_vcpu {
47 	struct svm_softc *sc;
48 	struct vcpu	*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 	struct vm_mtrr  mtrr;
58 	int		vcpuid;
59 };
60 
61 /*
62  * SVM softc, one per virtual machine.
63  */
64 struct svm_softc {
65 	vm_paddr_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 };
70 
71 #define	SVM_CTR0(vcpu, format)						\
72 	VCPU_CTR0((vcpu)->sc->vm, (vcpu)->vcpuid, format)
73 
74 #define	SVM_CTR1(vcpu, format, p1)					\
75 	VCPU_CTR1((vcpu)->sc->vm, (vcpu)->vcpuid, format, p1)
76 
77 #define	SVM_CTR2(vcpu, format, p1, p2)					\
78 	VCPU_CTR2((vcpu)->sc->vm, (vcpu)->vcpuid, format, p1, p2)
79 
80 #define	SVM_CTR3(vcpu, format, p1, p2, p3)				\
81 	VCPU_CTR3((vcpu)->sc->vm, (vcpu)->vcpuid, format, p1, p2, p3)
82 
83 #define	SVM_CTR4(vcpu, format, p1, p2, p3, p4)				\
84 	VCPU_CTR4((vcpu)->sc->vm, (vcpu)->vcpuid, format, p1, p2, p3, p4)
85 
86 static __inline struct vmcb *
87 svm_get_vmcb(struct svm_vcpu *vcpu)
88 {
89 
90 	return (vcpu->vmcb);
91 }
92 
93 static __inline struct vmcb_state *
94 svm_get_vmcb_state(struct svm_vcpu *vcpu)
95 {
96 
97 	return (&vcpu->vmcb->state);
98 }
99 
100 static __inline struct vmcb_ctrl *
101 svm_get_vmcb_ctrl(struct svm_vcpu *vcpu)
102 {
103 
104 	return (&vcpu->vmcb->ctrl);
105 }
106 
107 static __inline struct svm_regctx *
108 svm_get_guest_regctx(struct svm_vcpu *vcpu)
109 {
110 
111 	return (&vcpu->swctx);
112 }
113 
114 static __inline void
115 svm_set_dirty(struct svm_vcpu *vcpu, uint32_t dirtybits)
116 {
117 
118         vcpu->dirty |= dirtybits;
119 }
120 
121 #endif /* _SVM_SOFTC_H_ */
122