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: releng/10.3/sys/amd64/vmm/amd/svm_softc.h 284894 2015-06-27 22:48:22Z 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/*
41272930Sneel * XXX separate out 'struct vmcb' from 'svm_vcpu' to avoid wasting space
42272930Sneel * due to VMCB alignment requirements.
43249967Sneel */
44249967Sneelstruct svm_vcpu {
45271203Sneel	struct vmcb	vmcb;	 /* hardware saved vcpu context */
46271203Sneel	struct svm_regctx swctx; /* software saved vcpu context */
47271203Sneel	uint64_t	vmcb_pa; /* VMCB physical address */
48284894Sneel	uint64_t	nextrip; /* next instruction to be executed by guest */
49271203Sneel        int		lastcpu; /* host cpu that the vcpu last ran on */
50271203Sneel	uint32_t	dirty;	 /* state cache bits that must be cleared */
51271203Sneel	long		eptgen;	 /* pmap->pm_eptgen when the vcpu last ran */
52271203Sneel	struct asid	asid;
53249967Sneel} __aligned(PAGE_SIZE);
54249967Sneel
55249967Sneel/*
56249967Sneel * SVM softc, one per virtual machine.
57249967Sneel */
58249967Sneelstruct svm_softc {
59272930Sneel	uint8_t iopm_bitmap[SVM_IO_BITMAP_SIZE];    /* shared by all vcpus */
60272930Sneel	uint8_t msr_bitmap[SVM_MSR_BITMAP_SIZE];    /* shared by all vcpus */
61267003Sgrehan	uint8_t apic_page[VM_MAXCPU][PAGE_SIZE];
62272930Sneel	struct svm_vcpu vcpu[VM_MAXCPU];
63272930Sneel	vm_offset_t 	nptp;			    /* nested page table */
64249967Sneel	struct vm	*vm;
65249967Sneel} __aligned(PAGE_SIZE);
66249967Sneel
67259579SgrehanCTASSERT((offsetof(struct svm_softc, nptp) & PAGE_MASK) == 0);
68249967Sneel
69249967Sneelstatic __inline struct svm_vcpu *
70249967Sneelsvm_get_vcpu(struct svm_softc *sc, int vcpu)
71249967Sneel{
72249967Sneel
73249967Sneel	return (&(sc->vcpu[vcpu]));
74249967Sneel}
75249967Sneel
76249967Sneelstatic __inline struct vmcb *
77249967Sneelsvm_get_vmcb(struct svm_softc *sc, int vcpu)
78249967Sneel{
79249967Sneel
80249967Sneel	return (&(sc->vcpu[vcpu].vmcb));
81249967Sneel}
82249967Sneel
83249967Sneelstatic __inline struct vmcb_state *
84249967Sneelsvm_get_vmcb_state(struct svm_softc *sc, int vcpu)
85249967Sneel{
86249967Sneel
87249967Sneel	return (&(sc->vcpu[vcpu].vmcb.state));
88249967Sneel}
89249967Sneel
90249967Sneelstatic __inline struct vmcb_ctrl *
91249967Sneelsvm_get_vmcb_ctrl(struct svm_softc *sc, int vcpu)
92249967Sneel{
93249967Sneel
94249967Sneel	return (&(sc->vcpu[vcpu].vmcb.ctrl));
95249967Sneel}
96249967Sneel
97249967Sneelstatic __inline struct svm_regctx *
98249967Sneelsvm_get_guest_regctx(struct svm_softc *sc, int vcpu)
99249967Sneel{
100249967Sneel
101249967Sneel	return (&(sc->vcpu[vcpu].swctx));
102249967Sneel}
103249967Sneel
104271939Sneelstatic __inline void
105271939Sneelsvm_set_dirty(struct svm_softc *sc, int vcpu, uint32_t dirtybits)
106271939Sneel{
107271939Sneel        struct svm_vcpu *vcpustate;
108271939Sneel
109271939Sneel        vcpustate = svm_get_vcpu(sc, vcpu);
110271939Sneel
111271939Sneel        vcpustate->dirty |= dirtybits;
112271939Sneel}
113271939Sneel
114249967Sneel#endif /* _SVM_SOFTC_H_ */
115