vmx.h revision 331722
1/*-
2 * Copyright (c) 2011 NetApp, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: stable/11/sys/amd64/vmm/intel/vmx.h 331722 2018-03-29 02:50:57Z eadler $
27 */
28
29#ifndef _VMX_H_
30#define	_VMX_H_
31
32#include "vmcs.h"
33
34struct pmap;
35
36struct vmxctx {
37	register_t	guest_rdi;		/* Guest state */
38	register_t	guest_rsi;
39	register_t	guest_rdx;
40	register_t	guest_rcx;
41	register_t	guest_r8;
42	register_t	guest_r9;
43	register_t	guest_rax;
44	register_t	guest_rbx;
45	register_t	guest_rbp;
46	register_t	guest_r10;
47	register_t	guest_r11;
48	register_t	guest_r12;
49	register_t	guest_r13;
50	register_t	guest_r14;
51	register_t	guest_r15;
52	register_t	guest_cr2;
53	register_t	guest_dr0;
54	register_t	guest_dr1;
55	register_t	guest_dr2;
56	register_t	guest_dr3;
57	register_t	guest_dr6;
58
59	register_t	host_r15;		/* Host state */
60	register_t	host_r14;
61	register_t	host_r13;
62	register_t	host_r12;
63	register_t	host_rbp;
64	register_t	host_rsp;
65	register_t	host_rbx;
66	register_t	host_dr0;
67	register_t	host_dr1;
68	register_t	host_dr2;
69	register_t	host_dr3;
70	register_t	host_dr6;
71	register_t	host_dr7;
72	uint64_t	host_debugctl;
73	int		host_tf;
74
75	int		inst_fail_status;
76
77	/*
78	 * The pmap needs to be deactivated in vmx_enter_guest()
79	 * so keep a copy of the 'pmap' in each vmxctx.
80	 */
81	struct pmap	*pmap;
82};
83
84struct vmxcap {
85	int	set;
86	uint32_t proc_ctls;
87	uint32_t proc_ctls2;
88};
89
90struct vmxstate {
91	uint64_t nextrip;	/* next instruction to be executed by guest */
92	int	lastcpu;	/* host cpu that this 'vcpu' last ran on */
93	uint16_t vpid;
94};
95
96struct apic_page {
97	uint32_t reg[PAGE_SIZE / 4];
98};
99CTASSERT(sizeof(struct apic_page) == PAGE_SIZE);
100
101/* Posted Interrupt Descriptor (described in section 29.6 of the Intel SDM) */
102struct pir_desc {
103	uint64_t	pir[4];
104	uint64_t	pending;
105	uint64_t	unused[3];
106} __aligned(64);
107CTASSERT(sizeof(struct pir_desc) == 64);
108
109/* Index into the 'guest_msrs[]' array */
110enum {
111	IDX_MSR_LSTAR,
112	IDX_MSR_CSTAR,
113	IDX_MSR_STAR,
114	IDX_MSR_SF_MASK,
115	IDX_MSR_KGSBASE,
116	IDX_MSR_PAT,
117	GUEST_MSR_NUM		/* must be the last enumeration */
118};
119
120/* virtual machine softc */
121struct vmx {
122	struct vmcs	vmcs[VM_MAXCPU];	/* one vmcs per virtual cpu */
123	struct apic_page apic_page[VM_MAXCPU];	/* one apic page per vcpu */
124	char		msr_bitmap[PAGE_SIZE];
125	struct pir_desc	pir_desc[VM_MAXCPU];
126	uint64_t	guest_msrs[VM_MAXCPU][GUEST_MSR_NUM];
127	struct vmxctx	ctx[VM_MAXCPU];
128	struct vmxcap	cap[VM_MAXCPU];
129	struct vmxstate	state[VM_MAXCPU];
130	uint64_t	eptp;
131	struct vm	*vm;
132	long		eptgen[MAXCPU];		/* cached pmap->pm_eptgen */
133};
134CTASSERT((offsetof(struct vmx, vmcs) & PAGE_MASK) == 0);
135CTASSERT((offsetof(struct vmx, msr_bitmap) & PAGE_MASK) == 0);
136CTASSERT((offsetof(struct vmx, pir_desc[0]) & 63) == 0);
137
138#define	VMX_GUEST_VMEXIT	0
139#define	VMX_VMRESUME_ERROR	1
140#define	VMX_VMLAUNCH_ERROR	2
141#define	VMX_INVEPT_ERROR	3
142int	vmx_enter_guest(struct vmxctx *ctx, struct vmx *vmx, int launched);
143void	vmx_call_isr(uintptr_t entry);
144
145u_long	vmx_fix_cr0(u_long cr0);
146u_long	vmx_fix_cr4(u_long cr4);
147
148int	vmx_set_tsc_offset(struct vmx *vmx, int vcpu, uint64_t offset);
149
150extern char	vmx_exit_guest[];
151extern char	vmx_exit_guest_flush_rsb[];
152
153#endif
154