xref: /illumos-gate/usr/src/uts/intel/io/vmm/intel/vmx.h (revision 32640292)
1bf21cd93STycho Nightingale /*-
2*32640292SAndy Fiddaman  * SPDX-License-Identifier: BSD-2-Clause
34c87aefeSPatrick Mooney  *
4bf21cd93STycho Nightingale  * Copyright (c) 2011 NetApp, Inc.
5bf21cd93STycho Nightingale  * All rights reserved.
6bf21cd93STycho Nightingale  *
7bf21cd93STycho Nightingale  * Redistribution and use in source and binary forms, with or without
8bf21cd93STycho Nightingale  * modification, are permitted provided that the following conditions
9bf21cd93STycho Nightingale  * are met:
10bf21cd93STycho Nightingale  * 1. Redistributions of source code must retain the above copyright
11bf21cd93STycho Nightingale  *    notice, this list of conditions and the following disclaimer.
12bf21cd93STycho Nightingale  * 2. Redistributions in binary form must reproduce the above copyright
13bf21cd93STycho Nightingale  *    notice, this list of conditions and the following disclaimer in the
14bf21cd93STycho Nightingale  *    documentation and/or other materials provided with the distribution.
15bf21cd93STycho Nightingale  *
16bf21cd93STycho Nightingale  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17bf21cd93STycho Nightingale  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18bf21cd93STycho Nightingale  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19bf21cd93STycho Nightingale  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20bf21cd93STycho Nightingale  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21bf21cd93STycho Nightingale  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22bf21cd93STycho Nightingale  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23bf21cd93STycho Nightingale  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24bf21cd93STycho Nightingale  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25bf21cd93STycho Nightingale  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26bf21cd93STycho Nightingale  * SUCH DAMAGE.
274c87aefeSPatrick Mooney  */
284c87aefeSPatrick Mooney 
294c87aefeSPatrick Mooney /*
30c3ae3afaSPatrick Mooney  * This file and its contents are supplied under the terms of the
31c3ae3afaSPatrick Mooney  * Common Development and Distribution License ("CDDL"), version 1.0.
32c3ae3afaSPatrick Mooney  * You may only use this file in accordance with the terms of version
33c3ae3afaSPatrick Mooney  * 1.0 of the CDDL.
34c3ae3afaSPatrick Mooney  *
35c3ae3afaSPatrick Mooney  * A full copy of the text of the CDDL should have accompanied this
36c3ae3afaSPatrick Mooney  * source.  A copy of the CDDL is also available via the Internet at
37c3ae3afaSPatrick Mooney  * http://www.illumos.org/license/CDDL.
38c3ae3afaSPatrick Mooney  *
394c87aefeSPatrick Mooney  * Copyright 2018 Joyent, Inc.
400153d828SPatrick Mooney  * Copyright 2021 Oxide Computer Company
41bf21cd93STycho Nightingale  */
42bf21cd93STycho Nightingale 
43bf21cd93STycho Nightingale #ifndef _VMX_H_
44bf21cd93STycho Nightingale #define	_VMX_H_
45bf21cd93STycho Nightingale 
46bf21cd93STycho Nightingale #include "vmcs.h"
47bf21cd93STycho Nightingale 
48bf21cd93STycho Nightingale struct vmxctx {
49db8733f5SPatrick Mooney 	uint64_t	guest_rdi;		/* Guest state */
50db8733f5SPatrick Mooney 	uint64_t	guest_rsi;
51db8733f5SPatrick Mooney 	uint64_t	guest_rdx;
52db8733f5SPatrick Mooney 	uint64_t	guest_rcx;
53db8733f5SPatrick Mooney 	uint64_t	guest_r8;
54db8733f5SPatrick Mooney 	uint64_t	guest_r9;
55db8733f5SPatrick Mooney 	uint64_t	guest_rax;
56db8733f5SPatrick Mooney 	uint64_t	guest_rbx;
57db8733f5SPatrick Mooney 	uint64_t	guest_rbp;
58db8733f5SPatrick Mooney 	uint64_t	guest_r10;
59db8733f5SPatrick Mooney 	uint64_t	guest_r11;
60db8733f5SPatrick Mooney 	uint64_t	guest_r12;
61db8733f5SPatrick Mooney 	uint64_t	guest_r13;
62db8733f5SPatrick Mooney 	uint64_t	guest_r14;
63db8733f5SPatrick Mooney 	uint64_t	guest_r15;
64db8733f5SPatrick Mooney 	uint64_t	guest_cr2;
65db8733f5SPatrick Mooney 	uint64_t	guest_dr0;
66db8733f5SPatrick Mooney 	uint64_t	guest_dr1;
67db8733f5SPatrick Mooney 	uint64_t	guest_dr2;
68db8733f5SPatrick Mooney 	uint64_t	guest_dr3;
69db8733f5SPatrick Mooney 	uint64_t	guest_dr6;
70bf21cd93STycho Nightingale 
71db8733f5SPatrick Mooney 	uint64_t	host_dr0;
72db8733f5SPatrick Mooney 	uint64_t	host_dr1;
73db8733f5SPatrick Mooney 	uint64_t	host_dr2;
74db8733f5SPatrick Mooney 	uint64_t	host_dr3;
75db8733f5SPatrick Mooney 	uint64_t	host_dr6;
76db8733f5SPatrick Mooney 	uint64_t	host_dr7;
774c87aefeSPatrick Mooney 	uint64_t	host_debugctl;
784c87aefeSPatrick Mooney 	int		host_tf;
794c87aefeSPatrick Mooney 
804c87aefeSPatrick Mooney 	int		inst_fail_status;
81bf21cd93STycho Nightingale };
82bf21cd93STycho Nightingale 
83bf21cd93STycho Nightingale struct vmxcap {
84bf21cd93STycho Nightingale 	int	set;
85bf21cd93STycho Nightingale 	uint32_t proc_ctls;
86bf21cd93STycho Nightingale 	uint32_t proc_ctls2;
87154972afSPatrick Mooney 	uint32_t exc_bitmap;
88bf21cd93STycho Nightingale };
89bf21cd93STycho Nightingale 
90bf21cd93STycho Nightingale struct vmxstate {
91bf21cd93STycho Nightingale 	uint64_t nextrip;	/* next instruction to be executed by guest */
92bf21cd93STycho Nightingale 	int	lastcpu;	/* host cpu that this 'vcpu' last ran on */
93bf21cd93STycho Nightingale 	uint16_t vpid;
94bf21cd93STycho Nightingale };
95bf21cd93STycho Nightingale 
96bf21cd93STycho Nightingale struct apic_page {
97bf21cd93STycho Nightingale 	uint32_t reg[PAGE_SIZE / 4];
98bf21cd93STycho Nightingale };
992699b94cSPatrick Mooney CTASSERT(sizeof (struct apic_page) == PAGE_SIZE);
100bf21cd93STycho Nightingale 
101bf21cd93STycho Nightingale /* Posted Interrupt Descriptor (described in section 29.6 of the Intel SDM) */
102bf21cd93STycho Nightingale struct pir_desc {
103c74a40a5SPatrick Mooney 	uint32_t	pir[8];
104bf21cd93STycho Nightingale 	uint64_t	pending;
105bf21cd93STycho Nightingale 	uint64_t	unused[3];
106bf21cd93STycho Nightingale } __aligned(64);
1072699b94cSPatrick Mooney CTASSERT(sizeof (struct pir_desc) == 64);
108bf21cd93STycho Nightingale 
109bf21cd93STycho Nightingale /* Index into the 'guest_msrs[]' array */
110bf21cd93STycho Nightingale enum {
111bf21cd93STycho Nightingale 	IDX_MSR_LSTAR,
112bf21cd93STycho Nightingale 	IDX_MSR_CSTAR,
113bf21cd93STycho Nightingale 	IDX_MSR_STAR,
114bf21cd93STycho Nightingale 	IDX_MSR_SF_MASK,
115bf21cd93STycho Nightingale 	IDX_MSR_KGSBASE,
1164c87aefeSPatrick Mooney 	IDX_MSR_PAT,
117bf21cd93STycho Nightingale 	GUEST_MSR_NUM		/* must be the last enumeration */
118bf21cd93STycho Nightingale };
119bf21cd93STycho Nightingale 
1204c87aefeSPatrick Mooney typedef enum {
1214c87aefeSPatrick Mooney 	VS_NONE		= 0x0,
1224c87aefeSPatrick Mooney 	VS_LAUNCHED	= 0x1,
1234c87aefeSPatrick Mooney 	VS_LOADED	= 0x2
1244c87aefeSPatrick Mooney } vmcs_state_t;
1254c87aefeSPatrick Mooney 
126bf21cd93STycho Nightingale /* virtual machine softc */
127bf21cd93STycho Nightingale struct vmx {
128bf21cd93STycho Nightingale 	struct vmcs	vmcs[VM_MAXCPU];	/* one vmcs per virtual cpu */
129bf21cd93STycho Nightingale 	struct apic_page apic_page[VM_MAXCPU];	/* one apic page per vcpu */
1306b641d7aSPatrick Mooney 	uint8_t		*msr_bitmap[VM_MAXCPU];	/* one MSR bitmap per vCPU */
131bf21cd93STycho Nightingale 	struct pir_desc	pir_desc[VM_MAXCPU];
132bf21cd93STycho Nightingale 	uint64_t	guest_msrs[VM_MAXCPU][GUEST_MSR_NUM];
1334c87aefeSPatrick Mooney 	uint64_t	host_msrs[VM_MAXCPU][GUEST_MSR_NUM];
1344c87aefeSPatrick Mooney 	uint64_t	tsc_offset_active[VM_MAXCPU];
1354c87aefeSPatrick Mooney 	vmcs_state_t	vmcs_state[VM_MAXCPU];
136007ca332SPatrick Mooney 	uintptr_t	vmcs_pa[VM_MAXCPU];
1376b641d7aSPatrick Mooney 	void		*apic_access_page;
138bf21cd93STycho Nightingale 	struct vmxctx	ctx[VM_MAXCPU];
139bf21cd93STycho Nightingale 	struct vmxcap	cap[VM_MAXCPU];
140bf21cd93STycho Nightingale 	struct vmxstate	state[VM_MAXCPU];
1414c87aefeSPatrick Mooney 	uint64_t	eptp;
142c3ae3afaSPatrick Mooney 	enum vmx_caps	vmx_caps;
143bf21cd93STycho Nightingale 	struct vm	*vm;
144d1c02647SPatrick Mooney 	/*
145d1c02647SPatrick Mooney 	 * Track the latest vmspace generation as it is run on a given host CPU.
146d1c02647SPatrick Mooney 	 * This allows us to react to modifications to the vmspace (such as
147d1c02647SPatrick Mooney 	 * unmap or changed protection) which necessitate flushing any
148d1c02647SPatrick Mooney 	 * guest-physical TLB entries tagged for this guest via 'invept'.
149d1c02647SPatrick Mooney 	 */
150d1c02647SPatrick Mooney 	uint64_t	eptgen[MAXCPU];
151bf21cd93STycho Nightingale };
152bf21cd93STycho Nightingale CTASSERT((offsetof(struct vmx, vmcs) & PAGE_MASK) == 0);
153bf21cd93STycho Nightingale CTASSERT((offsetof(struct vmx, msr_bitmap) & PAGE_MASK) == 0);
1544c87aefeSPatrick Mooney CTASSERT((offsetof(struct vmx, pir_desc[0]) & 63) == 0);
155bf21cd93STycho Nightingale 
156c3ae3afaSPatrick Mooney static __inline bool
vmx_cap_en(const struct vmx * vmx,enum vmx_caps cap)157c3ae3afaSPatrick Mooney vmx_cap_en(const struct vmx *vmx, enum vmx_caps cap)
158c3ae3afaSPatrick Mooney {
159c3ae3afaSPatrick Mooney 	return ((vmx->vmx_caps & cap) == cap);
160c3ae3afaSPatrick Mooney }
161c3ae3afaSPatrick Mooney 
162007ca332SPatrick Mooney 
163007ca332SPatrick Mooney /*
164007ca332SPatrick Mooney  * Section 5.2 "Conventions" from Intel Architecture Manual 2B.
165007ca332SPatrick Mooney  *
166007ca332SPatrick Mooney  *			error
167007ca332SPatrick Mooney  * VMsucceed		  0
168007ca332SPatrick Mooney  * VMFailInvalid	  1
169007ca332SPatrick Mooney  * VMFailValid		  2	see also VMCS VM-Instruction Error Field
170007ca332SPatrick Mooney  */
171007ca332SPatrick Mooney #define	VM_SUCCESS		0
172007ca332SPatrick Mooney #define	VM_FAIL_INVALID		1
173007ca332SPatrick Mooney #define	VM_FAIL_VALID		2
174007ca332SPatrick Mooney #define	VMX_SET_ERROR_CODE_ASM \
175007ca332SPatrick Mooney 	"	jnc 1f;"						\
176007ca332SPatrick Mooney 	"	mov $1, %[error];"	/* CF: error = 1 */		\
177007ca332SPatrick Mooney 	"	jmp 3f;"						\
178007ca332SPatrick Mooney 	"1:	jnz 2f;"						\
179007ca332SPatrick Mooney 	"	mov $2, %[error];"	/* ZF: error = 2 */		\
180007ca332SPatrick Mooney 	"	jmp 3f;"						\
181007ca332SPatrick Mooney 	"2:	mov $0, %[error];"					\
182007ca332SPatrick Mooney 	"3:"
183007ca332SPatrick Mooney 
184007ca332SPatrick Mooney 
1854c87aefeSPatrick Mooney #define	VMX_GUEST_VMEXIT	0
1864c87aefeSPatrick Mooney #define	VMX_VMRESUME_ERROR	1
1874c87aefeSPatrick Mooney #define	VMX_VMLAUNCH_ERROR	2
1884c87aefeSPatrick Mooney #define	VMX_INVEPT_ERROR	3
1894c87aefeSPatrick Mooney #define	VMX_VMWRITE_ERROR	4
190007ca332SPatrick Mooney 
1914c87aefeSPatrick Mooney int	vmx_enter_guest(struct vmxctx *ctx, struct vmx *vmx, int launched);
1924c87aefeSPatrick Mooney void	vmx_call_isr(uintptr_t entry);
193bf21cd93STycho Nightingale 
194bf21cd93STycho Nightingale int	vmx_set_tsc_offset(struct vmx *vmx, int vcpu, uint64_t offset);
195bf21cd93STycho Nightingale 
1964c87aefeSPatrick Mooney extern char	vmx_exit_guest[];
1974c87aefeSPatrick Mooney extern char	vmx_exit_guest_flush_rsb[];
1984c87aefeSPatrick Mooney 
199bf21cd93STycho Nightingale #endif
200