svm_softc.h revision 271203
1249967Sneel/*-
2249967Sneel * Copyright (c) 2013 Anish Gupta (akgupt3@gmail.com)
3249967Sneel * All rights reserved.
4249967Sneel *
5249967Sneel * Redistribution and use in source and binary forms, with or without
6249967Sneel * modification, are permitted provided that the following conditions
7249967Sneel * are met:
8249967Sneel * 1. Redistributions of source code must retain the above copyright
9249967Sneel *    notice unmodified, this list of conditions, and the following
10249967Sneel *    disclaimer.
11249967Sneel * 2. Redistributions in binary form must reproduce the above copyright
12249967Sneel *    notice, this list of conditions and the following disclaimer in the
13249967Sneel *    documentation and/or other materials provided with the distribution.
14249967Sneel *
15249967Sneel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16249967Sneel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17249967Sneel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18249967Sneel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19249967Sneel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20249967Sneel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21249967Sneel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22249967Sneel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23249967Sneel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24249967Sneel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25249967Sneel *
26249967Sneel * $FreeBSD: projects/bhyve_svm/sys/amd64/vmm/amd/svm_softc.h 271203 2014-09-06 19:02:52Z neel $
27249967Sneel */
28249967Sneel
29249967Sneel#ifndef _SVM_SOFTC_H_
30249967Sneel#define _SVM_SOFTC_H_
31249967Sneel
32249967Sneel#define SVM_IO_BITMAP_SIZE	(3 * PAGE_SIZE)
33249967Sneel#define SVM_MSR_BITMAP_SIZE	(2 * PAGE_SIZE)
34249967Sneel
35271203Sneelstruct asid {
36271203Sneel	uint64_t	gen;	/* range is [1, ~0UL] */
37271203Sneel	uint32_t	num;	/* range is [1, nasid - 1] */
38271203Sneel};
39271203Sneel
40249967Sneel/*
41249967Sneel * svm_vpcu contains SVM VMCB state and vcpu register state.
42249967Sneel */
43249967Sneelstruct svm_vcpu {
44271203Sneel	struct vmcb	vmcb;	 /* hardware saved vcpu context */
45271203Sneel	struct svm_regctx swctx; /* software saved vcpu context */
46271203Sneel	uint64_t	vmcb_pa; /* VMCB physical address */
47271203Sneel	uint64_t	loop;	 /* loop count for vcpu */
48271203Sneel        int		lastcpu; /* host cpu that the vcpu last ran on */
49271203Sneel	uint32_t	dirty;	 /* state cache bits that must be cleared */
50271203Sneel	long		eptgen;	 /* pmap->pm_eptgen when the vcpu last ran */
51271203Sneel	struct asid	asid;
52249967Sneel} __aligned(PAGE_SIZE);
53249967Sneel
54249967Sneel/*
55249967Sneel * SVM softc, one per virtual machine.
56249967Sneel */
57249967Sneelstruct svm_softc {
58249967Sneel	/*
59249967Sneel	 * IO permission map, VMCB.ctrl.iopm_base_pa should point to this.
60249967Sneel	 * If a bit is set, access to I/O port is intercepted.
61249967Sneel	 */
62249967Sneel	uint8_t iopm_bitmap[SVM_IO_BITMAP_SIZE];
63249967Sneel
64249967Sneel	/*
65249967Sneel	 * MSR permission bitmap, VMCB.ctrl.msrpm_base_pa should point to this.
66249967Sneel	 * Two bits are used for each MSR with the LSB used for read access
67249967Sneel	 * and the MSB used for write access. A value of '1' indicates that
68249967Sneel	 * the operation is intercepted.
69249967Sneel	 */
70249967Sneel	uint8_t	msr_bitmap[SVM_MSR_BITMAP_SIZE];
71249967Sneel
72267003Sgrehan	uint8_t apic_page[VM_MAXCPU][PAGE_SIZE];
73249967Sneel	/* Nested Paging */
74259579Sgrehan	vm_offset_t 	nptp;
75249967Sneel
76249967Sneel	/* Virtual machine pointer. */
77249967Sneel	struct vm	*vm;
78249967Sneel
79249967Sneel	/* Guest VCPU h/w and s/w context. */
80249967Sneel	struct svm_vcpu vcpu[VM_MAXCPU];
81249967Sneel
82249967Sneel	uint32_t	svm_feature;	/* SVM features from CPUID.*/
83249967Sneel
84249967Sneel	int 		vcpu_cnt;	/* number of VCPUs for this guest.*/
85249967Sneel} __aligned(PAGE_SIZE);
86249967Sneel
87259579SgrehanCTASSERT((offsetof(struct svm_softc, nptp) & PAGE_MASK) == 0);
88249967Sneel
89249967Sneelstatic __inline struct svm_vcpu *
90249967Sneelsvm_get_vcpu(struct svm_softc *sc, int vcpu)
91249967Sneel{
92249967Sneel
93249967Sneel	return (&(sc->vcpu[vcpu]));
94249967Sneel}
95249967Sneel
96249967Sneelstatic __inline struct vmcb *
97249967Sneelsvm_get_vmcb(struct svm_softc *sc, int vcpu)
98249967Sneel{
99249967Sneel
100249967Sneel	return (&(sc->vcpu[vcpu].vmcb));
101249967Sneel}
102249967Sneel
103249967Sneelstatic __inline struct vmcb_state *
104249967Sneelsvm_get_vmcb_state(struct svm_softc *sc, int vcpu)
105249967Sneel{
106249967Sneel
107249967Sneel	return (&(sc->vcpu[vcpu].vmcb.state));
108249967Sneel}
109249967Sneel
110249967Sneelstatic __inline struct vmcb_ctrl *
111249967Sneelsvm_get_vmcb_ctrl(struct svm_softc *sc, int vcpu)
112249967Sneel{
113249967Sneel
114249967Sneel	return (&(sc->vcpu[vcpu].vmcb.ctrl));
115249967Sneel}
116249967Sneel
117249967Sneelstatic __inline struct svm_regctx *
118249967Sneelsvm_get_guest_regctx(struct svm_softc *sc, int vcpu)
119249967Sneel{
120249967Sneel
121249967Sneel	return (&(sc->vcpu[vcpu].swctx));
122249967Sneel}
123249967Sneel
124249967Sneelvoid svm_dump_vmcb(struct svm_softc *svm_sc, int vcpu);
125249967Sneel#endif /* _SVM_SOFTC_H_ */
126