xref: /openbsd/sys/arch/amd64/include/vmmvar.h (revision fabcfecb)
1*fabcfecbSjsg /*	$OpenBSD: vmmvar.h,v 1.109 2024/10/22 21:50:02 jsg Exp $	*/
277d6d4a2Smlarkin /*
377d6d4a2Smlarkin  * Copyright (c) 2014 Mike Larkin <mlarkin@openbsd.org>
477d6d4a2Smlarkin  *
577d6d4a2Smlarkin  * Permission to use, copy, modify, and distribute this software for any
677d6d4a2Smlarkin  * purpose with or without fee is hereby granted, provided that the above
777d6d4a2Smlarkin  * copyright notice and this permission notice appear in all copies.
877d6d4a2Smlarkin  *
977d6d4a2Smlarkin  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1077d6d4a2Smlarkin  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1177d6d4a2Smlarkin  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1277d6d4a2Smlarkin  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1377d6d4a2Smlarkin  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1477d6d4a2Smlarkin  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1577d6d4a2Smlarkin  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1677d6d4a2Smlarkin  */
1777d6d4a2Smlarkin 
1877d6d4a2Smlarkin /*
1977d6d4a2Smlarkin  * CPU capabilities for VMM operation
2077d6d4a2Smlarkin  */
2177d6d4a2Smlarkin #ifndef _MACHINE_VMMVAR_H_
2277d6d4a2Smlarkin #define _MACHINE_VMMVAR_H_
2377d6d4a2Smlarkin 
2477d6d4a2Smlarkin #define VMM_HV_SIGNATURE 	"OpenBSDVMM58"
2577d6d4a2Smlarkin 
2677d6d4a2Smlarkin /* VMX: Basic Exit Reasons */
2777d6d4a2Smlarkin #define VMX_EXIT_NMI				0
2877d6d4a2Smlarkin #define VMX_EXIT_EXTINT				1
2977d6d4a2Smlarkin #define VMX_EXIT_TRIPLE_FAULT			2
3077d6d4a2Smlarkin #define VMX_EXIT_INIT				3
3177d6d4a2Smlarkin #define VMX_EXIT_SIPI				4
3277d6d4a2Smlarkin #define VMX_EXIT_IO_SMI				5
3377d6d4a2Smlarkin #define VMX_EXIT_OTHER_SMI			6
3477d6d4a2Smlarkin #define VMX_EXIT_INT_WINDOW			7
3577d6d4a2Smlarkin #define VMX_EXIT_NMI_WINDOW			8
3677d6d4a2Smlarkin #define VMX_EXIT_TASK_SWITCH			9
3777d6d4a2Smlarkin #define VMX_EXIT_CPUID				10
3877d6d4a2Smlarkin #define VMX_EXIT_GETSEC				11
3977d6d4a2Smlarkin #define VMX_EXIT_HLT				12
4077d6d4a2Smlarkin #define VMX_EXIT_INVD				13
4177d6d4a2Smlarkin #define VMX_EXIT_INVLPG				14
4277d6d4a2Smlarkin #define VMX_EXIT_RDPMC				15
4377d6d4a2Smlarkin #define VMX_EXIT_RDTSC				16
4477d6d4a2Smlarkin #define VMX_EXIT_RSM				17
4577d6d4a2Smlarkin #define VMX_EXIT_VMCALL				18
4677d6d4a2Smlarkin #define VMX_EXIT_VMCLEAR			19
4777d6d4a2Smlarkin #define VMX_EXIT_VMLAUNCH			20
4877d6d4a2Smlarkin #define VMX_EXIT_VMPTRLD			21
4977d6d4a2Smlarkin #define VMX_EXIT_VMPTRST			22
5077d6d4a2Smlarkin #define VMX_EXIT_VMREAD				23
5177d6d4a2Smlarkin #define VMX_EXIT_VMRESUME			24
5277d6d4a2Smlarkin #define VMX_EXIT_VMWRITE			25
5377d6d4a2Smlarkin #define VMX_EXIT_VMXOFF				26
5477d6d4a2Smlarkin #define VMX_EXIT_VMXON				27
5577d6d4a2Smlarkin #define VMX_EXIT_CR_ACCESS			28
5677d6d4a2Smlarkin #define VMX_EXIT_MOV_DR				29
5777d6d4a2Smlarkin #define VMX_EXIT_IO				30
5877d6d4a2Smlarkin #define VMX_EXIT_RDMSR				31
5977d6d4a2Smlarkin #define VMX_EXIT_WRMSR				32
6077d6d4a2Smlarkin #define VMX_EXIT_ENTRY_FAILED_GUEST_STATE	33
6177d6d4a2Smlarkin #define VMX_EXIT_ENTRY_FAILED_MSR_LOAD		34
6277d6d4a2Smlarkin #define VMX_EXIT_MWAIT				36
6377d6d4a2Smlarkin #define VMX_EXIT_MTF				37
6477d6d4a2Smlarkin #define VMX_EXIT_MONITOR			39
6577d6d4a2Smlarkin #define VMX_EXIT_PAUSE				40
6677d6d4a2Smlarkin #define VMX_EXIT_ENTRY_FAILED_MCE		41
6777d6d4a2Smlarkin #define VMX_EXIT_TPR_BELOW_THRESHOLD		43
6877d6d4a2Smlarkin #define VMX_EXIT_APIC_ACCESS			44
6977d6d4a2Smlarkin #define VMX_EXIT_VIRTUALIZED_EOI		45
7077d6d4a2Smlarkin #define VMX_EXIT_GDTR_IDTR			46
7177d6d4a2Smlarkin #define	VMX_EXIT_LDTR_TR			47
7277d6d4a2Smlarkin #define VMX_EXIT_EPT_VIOLATION			48
7377d6d4a2Smlarkin #define VMX_EXIT_EPT_MISCONFIGURATION		49
7477d6d4a2Smlarkin #define VMX_EXIT_INVEPT				50
7577d6d4a2Smlarkin #define VMX_EXIT_RDTSCP				51
7677d6d4a2Smlarkin #define VMX_EXIT_VMX_PREEMPTION_TIMER_EXPIRED	52
7777d6d4a2Smlarkin #define VMX_EXIT_INVVPID			53
7877d6d4a2Smlarkin #define VMX_EXIT_WBINVD				54
7977d6d4a2Smlarkin #define VMX_EXIT_XSETBV				55
8077d6d4a2Smlarkin #define VMX_EXIT_APIC_WRITE			56
8177d6d4a2Smlarkin #define VMX_EXIT_RDRAND				57
8277d6d4a2Smlarkin #define VMX_EXIT_INVPCID			58
8377d6d4a2Smlarkin #define VMX_EXIT_VMFUNC				59
849f12662aSmlarkin #define VMX_EXIT_RDSEED				61
859f12662aSmlarkin #define VMX_EXIT_XSAVES				63
869f12662aSmlarkin #define VMX_EXIT_XRSTORS			64
8777d6d4a2Smlarkin 
88c4fd4c5bSdv #define VM_EXIT_TERMINATED			0xFFFE
89c4fd4c5bSdv #define VM_EXIT_NONE				0xFFFF
90c4fd4c5bSdv 
91a39ad34dSmlarkin /*
92a39ad34dSmlarkin  * VMX: Misc defines
93a39ad34dSmlarkin  */
94a39ad34dSmlarkin #define VMX_MAX_CR3_TARGETS			256
950a894fa6Sdv #define VMX_VMCS_PA_CLEAR			0xFFFFFFFFFFFFFFFFUL
96a39ad34dSmlarkin 
971180136eSmlarkin /*
9886eaef11Smlarkin  * SVM: Intercept codes (exit reasons)
9986eaef11Smlarkin  */
10086eaef11Smlarkin #define SVM_VMEXIT_CR0_READ			0x00
10186eaef11Smlarkin #define SVM_VMEXIT_CR1_READ			0x01
10286eaef11Smlarkin #define SVM_VMEXIT_CR2_READ			0x02
10386eaef11Smlarkin #define SVM_VMEXIT_CR3_READ			0x03
10486eaef11Smlarkin #define SVM_VMEXIT_CR4_READ			0x04
10586eaef11Smlarkin #define SVM_VMEXIT_CR5_READ			0x05
10686eaef11Smlarkin #define SVM_VMEXIT_CR6_READ			0x06
10786eaef11Smlarkin #define SVM_VMEXIT_CR7_READ			0x07
10886eaef11Smlarkin #define SVM_VMEXIT_CR8_READ			0x08
10986eaef11Smlarkin #define SVM_VMEXIT_CR9_READ			0x09
11086eaef11Smlarkin #define SVM_VMEXIT_CR10_READ			0x0A
11186eaef11Smlarkin #define SVM_VMEXIT_CR11_READ			0x0B
11286eaef11Smlarkin #define SVM_VMEXIT_CR12_READ			0x0C
11386eaef11Smlarkin #define SVM_VMEXIT_CR13_READ			0x0D
11486eaef11Smlarkin #define SVM_VMEXIT_CR14_READ			0x0E
11586eaef11Smlarkin #define SVM_VMEXIT_CR15_READ			0x0F
11686eaef11Smlarkin #define SVM_VMEXIT_CR0_WRITE			0x10
11786eaef11Smlarkin #define SVM_VMEXIT_CR1_WRITE			0x11
11886eaef11Smlarkin #define SVM_VMEXIT_CR2_WRITE			0x12
11986eaef11Smlarkin #define SVM_VMEXIT_CR3_WRITE			0x13
12086eaef11Smlarkin #define SVM_VMEXIT_CR4_WRITE			0x14
12186eaef11Smlarkin #define SVM_VMEXIT_CR5_WRITE			0x15
12286eaef11Smlarkin #define SVM_VMEXIT_CR6_WRITE			0x16
12386eaef11Smlarkin #define SVM_VMEXIT_CR7_WRITE			0x17
12486eaef11Smlarkin #define SVM_VMEXIT_CR8_WRITE			0x18
12586eaef11Smlarkin #define SVM_VMEXIT_CR9_WRITE			0x19
12686eaef11Smlarkin #define SVM_VMEXIT_CR10_WRITE			0x1A
12786eaef11Smlarkin #define SVM_VMEXIT_CR11_WRITE			0x1B
12886eaef11Smlarkin #define SVM_VMEXIT_CR12_WRITE			0x1C
12986eaef11Smlarkin #define SVM_VMEXIT_CR13_WRITE			0x1D
13086eaef11Smlarkin #define SVM_VMEXIT_CR14_WRITE			0x1E
13186eaef11Smlarkin #define SVM_VMEXIT_CR15_WRITE			0x1F
13286eaef11Smlarkin #define SVM_VMEXIT_DR0_READ			0x20
13386eaef11Smlarkin #define SVM_VMEXIT_DR1_READ			0x21
13486eaef11Smlarkin #define SVM_VMEXIT_DR2_READ			0x22
13586eaef11Smlarkin #define SVM_VMEXIT_DR3_READ			0x23
13686eaef11Smlarkin #define SVM_VMEXIT_DR4_READ			0x24
13786eaef11Smlarkin #define SVM_VMEXIT_DR5_READ			0x25
13886eaef11Smlarkin #define SVM_VMEXIT_DR6_READ			0x26
13986eaef11Smlarkin #define SVM_VMEXIT_DR7_READ			0x27
14086eaef11Smlarkin #define SVM_VMEXIT_DR8_READ			0x28
14186eaef11Smlarkin #define SVM_VMEXIT_DR9_READ			0x29
14286eaef11Smlarkin #define SVM_VMEXIT_DR10_READ			0x2A
14386eaef11Smlarkin #define SVM_VMEXIT_DR11_READ			0x2B
14486eaef11Smlarkin #define SVM_VMEXIT_DR12_READ			0x2C
14586eaef11Smlarkin #define SVM_VMEXIT_DR13_READ			0x2D
14686eaef11Smlarkin #define SVM_VMEXIT_DR14_READ			0x2E
14786eaef11Smlarkin #define SVM_VMEXIT_DR15_READ			0x2F
14886eaef11Smlarkin #define SVM_VMEXIT_DR0_WRITE			0x30
14986eaef11Smlarkin #define SVM_VMEXIT_DR1_WRITE			0x31
15086eaef11Smlarkin #define SVM_VMEXIT_DR2_WRITE			0x32
15186eaef11Smlarkin #define SVM_VMEXIT_DR3_WRITE			0x33
15286eaef11Smlarkin #define SVM_VMEXIT_DR4_WRITE			0x34
15386eaef11Smlarkin #define SVM_VMEXIT_DR5_WRITE			0x35
15486eaef11Smlarkin #define SVM_VMEXIT_DR6_WRITE			0x36
15586eaef11Smlarkin #define SVM_VMEXIT_DR7_WRITE			0x37
15686eaef11Smlarkin #define SVM_VMEXIT_DR8_WRITE			0x38
15786eaef11Smlarkin #define SVM_VMEXIT_DR9_WRITE			0x39
15886eaef11Smlarkin #define SVM_VMEXIT_DR10_WRITE			0x3A
15986eaef11Smlarkin #define SVM_VMEXIT_DR11_WRITE			0x3B
16086eaef11Smlarkin #define SVM_VMEXIT_DR12_WRITE			0x3C
16186eaef11Smlarkin #define SVM_VMEXIT_DR13_WRITE			0x3D
16286eaef11Smlarkin #define SVM_VMEXIT_DR14_WRITE			0x3E
16386eaef11Smlarkin #define SVM_VMEXIT_DR15_WRITE			0x3F
16486eaef11Smlarkin #define SVM_VMEXIT_EXCP0			0x40
16586eaef11Smlarkin #define SVM_VMEXIT_EXCP1			0x41
16686eaef11Smlarkin #define SVM_VMEXIT_EXCP2			0x42
16786eaef11Smlarkin #define SVM_VMEXIT_EXCP3			0x43
16886eaef11Smlarkin #define SVM_VMEXIT_EXCP4			0x44
16986eaef11Smlarkin #define SVM_VMEXIT_EXCP5			0x45
17086eaef11Smlarkin #define SVM_VMEXIT_EXCP6			0x46
17186eaef11Smlarkin #define SVM_VMEXIT_EXCP7			0x47
17286eaef11Smlarkin #define SVM_VMEXIT_EXCP8			0x48
17386eaef11Smlarkin #define SVM_VMEXIT_EXCP9			0x49
17486eaef11Smlarkin #define SVM_VMEXIT_EXCP10			0x4A
17586eaef11Smlarkin #define SVM_VMEXIT_EXCP11			0x4B
17686eaef11Smlarkin #define SVM_VMEXIT_EXCP12			0x4C
17786eaef11Smlarkin #define SVM_VMEXIT_EXCP13			0x4D
17886eaef11Smlarkin #define SVM_VMEXIT_EXCP14			0x4E
17986eaef11Smlarkin #define SVM_VMEXIT_EXCP15			0x4F
18086eaef11Smlarkin #define SVM_VMEXIT_EXCP16			0x50
18186eaef11Smlarkin #define SVM_VMEXIT_EXCP17			0x51
18286eaef11Smlarkin #define SVM_VMEXIT_EXCP18			0x52
18386eaef11Smlarkin #define SVM_VMEXIT_EXCP19			0x53
18486eaef11Smlarkin #define SVM_VMEXIT_EXCP20			0x54
18586eaef11Smlarkin #define SVM_VMEXIT_EXCP21			0x55
18686eaef11Smlarkin #define SVM_VMEXIT_EXCP22			0x56
18786eaef11Smlarkin #define SVM_VMEXIT_EXCP23			0x57
18886eaef11Smlarkin #define SVM_VMEXIT_EXCP24			0x58
18986eaef11Smlarkin #define SVM_VMEXIT_EXCP25			0x59
19086eaef11Smlarkin #define SVM_VMEXIT_EXCP26			0x5A
19186eaef11Smlarkin #define SVM_VMEXIT_EXCP27			0x5B
19286eaef11Smlarkin #define SVM_VMEXIT_EXCP28			0x5C
19386eaef11Smlarkin #define SVM_VMEXIT_EXCP29			0x5D
19486eaef11Smlarkin #define SVM_VMEXIT_EXCP30			0x5E
19586eaef11Smlarkin #define SVM_VMEXIT_EXCP31			0x5F
19686eaef11Smlarkin #define SVM_VMEXIT_INTR				0x60
19786eaef11Smlarkin #define SVM_VMEXIT_NMI				0x61
19886eaef11Smlarkin #define SVM_VMEXIT_SMI				0x62
19986eaef11Smlarkin #define SVM_VMEXIT_INIT				0x63
20086eaef11Smlarkin #define SVM_VMEXIT_VINTR			0x64
20186eaef11Smlarkin #define SVM_VMEXIT_CR0_SEL_WRITE		0x65
20286eaef11Smlarkin #define SVM_VMEXIT_IDTR_READ			0x66
20386eaef11Smlarkin #define SVM_VMEXIT_GDTR_READ			0x67
20486eaef11Smlarkin #define SVM_VMEXIT_LDTR_READ			0x68
20586eaef11Smlarkin #define SVM_VMEXIT_TR_READ			0x69
20686eaef11Smlarkin #define SVM_VMEXIT_IDTR_WRITE			0x6A
20786eaef11Smlarkin #define SVM_VMEXIT_GDTR_WRITE			0x6B
20886eaef11Smlarkin #define SVM_VMEXIT_LDTR_WRITE			0x6C
20986eaef11Smlarkin #define SVM_VMEXIT_TR_WRITE			0x6D
21086eaef11Smlarkin #define SVM_VMEXIT_RDTSC			0x6E
21186eaef11Smlarkin #define SVM_VMEXIT_RDPMC			0x6F
21286eaef11Smlarkin #define SVM_VMEXIT_PUSHF			0x70
21386eaef11Smlarkin #define SVM_VMEXIT_POPF				0x71
21486eaef11Smlarkin #define SVM_VMEXIT_CPUID			0x72
21586eaef11Smlarkin #define SVM_VMEXIT_RSM				0x73
21686eaef11Smlarkin #define SVM_VMEXIT_IRET				0x74
21786eaef11Smlarkin #define SVM_VMEXIT_SWINT			0x75
21886eaef11Smlarkin #define SVM_VMEXIT_INVD				0x76
21986eaef11Smlarkin #define SVM_VMEXIT_PAUSE			0x77
22086eaef11Smlarkin #define SVM_VMEXIT_HLT				0x78
22186eaef11Smlarkin #define SVM_VMEXIT_INVLPG			0x79
22286eaef11Smlarkin #define SVM_VMEXIT_INVLPGA			0x7A
22386eaef11Smlarkin #define SVM_VMEXIT_IOIO				0x7B
22486eaef11Smlarkin #define SVM_VMEXIT_MSR				0x7C
22586eaef11Smlarkin #define SVM_VMEXIT_TASK_SWITCH			0x7D
22686eaef11Smlarkin #define SVM_VMEXIT_FERR_FREEZE			0x7E
22786eaef11Smlarkin #define SVM_VMEXIT_SHUTDOWN			0x7F
22886eaef11Smlarkin #define SVM_VMEXIT_VMRUN			0x80
22986eaef11Smlarkin #define SVM_VMEXIT_VMMCALL			0x81
23086eaef11Smlarkin #define SVM_VMEXIT_VMLOAD			0x82
23186eaef11Smlarkin #define SVM_VMEXIT_VMSAVE			0x83
23286eaef11Smlarkin #define SVM_VMEXIT_STGI				0x84
23386eaef11Smlarkin #define SVM_VMEXIT_CLGI				0x85
23486eaef11Smlarkin #define SVM_VMEXIT_SKINIT			0x86
23586eaef11Smlarkin #define SVM_VMEXIT_RDTSCP			0x87
23686eaef11Smlarkin #define SVM_VMEXIT_ICEBP			0x88
23786eaef11Smlarkin #define SVM_VMEXIT_WBINVD			0x89
23886eaef11Smlarkin #define SVM_VMEXIT_MONITOR			0x8A
23986eaef11Smlarkin #define SVM_VMEXIT_MWAIT			0x8B
24086eaef11Smlarkin #define SVM_VMEXIT_MWAIT_CONDITIONAL		0x8C
24178080c4eSmlarkin #define SVM_VMEXIT_XSETBV			0x8D
24278080c4eSmlarkin #define SVM_VMEXIT_EFER_WRITE_TRAP		0x8F
24378080c4eSmlarkin #define SVM_VMEXIT_CR0_WRITE_TRAP		0x90
24478080c4eSmlarkin #define SVM_VMEXIT_CR1_WRITE_TRAP		0x91
24578080c4eSmlarkin #define SVM_VMEXIT_CR2_WRITE_TRAP		0x92
24678080c4eSmlarkin #define SVM_VMEXIT_CR3_WRITE_TRAP		0x93
24778080c4eSmlarkin #define SVM_VMEXIT_CR4_WRITE_TRAP		0x94
24878080c4eSmlarkin #define SVM_VMEXIT_CR5_WRITE_TRAP		0x95
24978080c4eSmlarkin #define SVM_VMEXIT_CR6_WRITE_TRAP		0x96
25078080c4eSmlarkin #define SVM_VMEXIT_CR7_WRITE_TRAP		0x97
25178080c4eSmlarkin #define SVM_VMEXIT_CR8_WRITE_TRAP		0x98
25278080c4eSmlarkin #define SVM_VMEXIT_CR9_WRITE_TRAP		0x99
25378080c4eSmlarkin #define SVM_VMEXIT_CR10_WRITE_TRAP		0x9A
25478080c4eSmlarkin #define SVM_VMEXIT_CR11_WRITE_TRAP		0x9B
25578080c4eSmlarkin #define SVM_VMEXIT_CR12_WRITE_TRAP		0x9C
25678080c4eSmlarkin #define SVM_VMEXIT_CR13_WRITE_TRAP		0x9D
25778080c4eSmlarkin #define SVM_VMEXIT_CR14_WRITE_TRAP		0x9E
25878080c4eSmlarkin #define SVM_VMEXIT_CR15_WRITE_TRAP		0x9F
25986eaef11Smlarkin #define SVM_VMEXIT_NPF				0x400
26078080c4eSmlarkin #define SVM_AVIC_INCOMPLETE_IPI			0x401
26178080c4eSmlarkin #define SVM_AVIC_NOACCEL			0x402
26278080c4eSmlarkin #define SVM_VMEXIT_VMGEXIT			0x403
26386eaef11Smlarkin #define SVM_VMEXIT_INVALID			-1
26486eaef11Smlarkin 
26586eaef11Smlarkin /*
2664db7a0afSmlarkin  * Exception injection vectors (these correspond to the CPU exception types
2674db7a0afSmlarkin  * defined in the SDM.)
2684db7a0afSmlarkin  */
2694db7a0afSmlarkin #define VMM_EX_DE	0	/* Divide Error #DE */
2704db7a0afSmlarkin #define VMM_EX_DB	1	/* Debug Exception #DB */
2714db7a0afSmlarkin #define VMM_EX_NMI	2	/* NMI */
2724db7a0afSmlarkin #define VMM_EX_BP	3	/* Breakpoint #BP */
2734db7a0afSmlarkin #define VMM_EX_OF	4	/* Overflow #OF */
2744db7a0afSmlarkin #define VMM_EX_BR	5	/* Bound range exceeded #BR */
2754db7a0afSmlarkin #define VMM_EX_UD	6	/* Undefined opcode #UD */
2764db7a0afSmlarkin #define VMM_EX_NM	7	/* Device not available #NM */
2774db7a0afSmlarkin #define VMM_EX_DF	8	/* Double fault #DF */
2784db7a0afSmlarkin #define VMM_EX_CP	9	/* Coprocessor segment overrun (unused) */
2794db7a0afSmlarkin #define VMM_EX_TS	10	/* Invalid TSS #TS */
2804db7a0afSmlarkin #define VMM_EX_NP	11	/* Segment not present #NP */
2814db7a0afSmlarkin #define VMM_EX_SS	12	/* Stack segment fault #SS */
2824db7a0afSmlarkin #define VMM_EX_GP	13	/* General protection #GP */
2834db7a0afSmlarkin #define VMM_EX_PF	14	/* Page fault #PF */
2844db7a0afSmlarkin #define VMM_EX_MF	16	/* x87 FPU floating point error #MF */
2854db7a0afSmlarkin #define VMM_EX_AC	17	/* Alignment check #AC */
2864db7a0afSmlarkin #define VMM_EX_MC	18	/* Machine check #MC */
2874db7a0afSmlarkin #define VMM_EX_XM	19	/* SIMD floating point exception #XM */
2884db7a0afSmlarkin #define VMM_EX_VE	20	/* Virtualization exception #VE */
2894db7a0afSmlarkin 
29077d6d4a2Smlarkin enum {
29177d6d4a2Smlarkin 	VEI_DIR_OUT,
29277d6d4a2Smlarkin 	VEI_DIR_IN
29377d6d4a2Smlarkin };
29477d6d4a2Smlarkin 
295d154feeaSmlarkin enum {
29618126f0eSdv 	VEE_FAULT_INVALID = 0,
2978732c716Sdv 	VEE_FAULT_HANDLED,
29818126f0eSdv 	VEE_FAULT_MMIO_ASSIST,
2998732c716Sdv 	VEE_FAULT_PROTECT,
30083422d52Spd };
30183422d52Spd 
30283422d52Spd enum {
303d154feeaSmlarkin 	VMM_CPU_MODE_REAL,
304d154feeaSmlarkin 	VMM_CPU_MODE_PROT,
305d154feeaSmlarkin 	VMM_CPU_MODE_PROT32,
306d154feeaSmlarkin 	VMM_CPU_MODE_COMPAT,
307d154feeaSmlarkin 	VMM_CPU_MODE_LONG,
308d154feeaSmlarkin 	VMM_CPU_MODE_UNKNOWN,
309d154feeaSmlarkin };
310d154feeaSmlarkin 
3113a0db596Smlarkin struct vmm_softc_md {
3123a0db596Smlarkin 	/* Capabilities */
3133a0db596Smlarkin 	uint32_t		nr_rvi_cpus;	/* [I] */
3143a0db596Smlarkin 	uint32_t		nr_ept_cpus;	/* [I] */
3153a0db596Smlarkin 	uint8_t			pkru_enabled;	/* [I] */
3163a0db596Smlarkin };
3173a0db596Smlarkin 
31877d6d4a2Smlarkin /*
31977d6d4a2Smlarkin  * vm exit data
32077d6d4a2Smlarkin  *  vm_exit_inout		: describes an IN/OUT exit
32177d6d4a2Smlarkin  */
32277d6d4a2Smlarkin struct vm_exit_inout {
32377d6d4a2Smlarkin 	uint8_t			vei_size;	/* Size of access */
32477d6d4a2Smlarkin 	uint8_t			vei_dir;	/* Direction */
32577d6d4a2Smlarkin 	uint8_t			vei_rep;	/* REP prefix? */
32677d6d4a2Smlarkin 	uint8_t			vei_string;	/* string variety? */
32777d6d4a2Smlarkin 	uint8_t			vei_encoding;	/* operand encoding */
32877d6d4a2Smlarkin 	uint16_t		vei_port;	/* port */
32976e54dacSmlarkin 	uint32_t		vei_data;	/* data */
330cc847d2aSdv 	uint8_t			vei_insn_len;	/* Count of instruction bytes */
33177d6d4a2Smlarkin };
3321ebbcee8Sdv 
33383422d52Spd /*
33483422d52Spd  *  vm_exit_eptviolation	: describes an EPT VIOLATION exit
33583422d52Spd  */
33683422d52Spd struct vm_exit_eptviolation {
33718126f0eSdv 	uint8_t		vee_fault_type;		/* type of vm exit */
33818126f0eSdv 	uint8_t		vee_insn_info;		/* bitfield */
33918126f0eSdv #define VEE_LEN_VALID		0x1		/* vee_insn_len is valid */
34018126f0eSdv #define VEE_BYTES_VALID		0x2		/* vee_insn_bytes is valid */
34118126f0eSdv 	uint8_t		vee_insn_len;		/* [VMX] instruction length */
34218126f0eSdv 	uint8_t		vee_insn_bytes[15];	/* [SVM] bytes at {R,E,}IP */
34383422d52Spd };
34477d6d4a2Smlarkin 
34576e54dacSmlarkin /*
3461ebbcee8Sdv  * struct vcpu_inject_event	: describes an exception or interrupt to inject.
3471ebbcee8Sdv  */
3481ebbcee8Sdv struct vcpu_inject_event {
3491ebbcee8Sdv 	uint8_t		vie_vector;	/* Exception or interrupt vector. */
3501ebbcee8Sdv 	uint32_t	vie_errorcode;	/* Optional error code. */
3511ebbcee8Sdv 	uint8_t		vie_type;
3521ebbcee8Sdv #define VCPU_INJECT_NONE	0
3531ebbcee8Sdv #define VCPU_INJECT_INTR	1	/* External hardware interrupt. */
3541ebbcee8Sdv #define VCPU_INJECT_EX		2	/* HW or SW Exception */
3551ebbcee8Sdv #define VCPU_INJECT_NMI		3	/* Non-maskable Interrupt */
3561ebbcee8Sdv };
3571ebbcee8Sdv 
3581ebbcee8Sdv /*
35976e54dacSmlarkin  * struct vcpu_segment_info
36076e54dacSmlarkin  *
36176e54dacSmlarkin  * Describes a segment + selector set, used in constructing the initial vcpu
36276e54dacSmlarkin  * register content
3633475ba91Smlarkin  */
3643475ba91Smlarkin struct vcpu_segment_info {
3653475ba91Smlarkin 	uint16_t	vsi_sel;
3663475ba91Smlarkin 	uint32_t	vsi_limit;
3673475ba91Smlarkin 	uint32_t	vsi_ar;
3683475ba91Smlarkin 	uint64_t	vsi_base;
3693475ba91Smlarkin };
3703475ba91Smlarkin 
371bee70036Sdv /* The GPRS are ordered to assist instruction decode. */
37256794f78Sstefan #define VCPU_REGS_RAX		0
373bee70036Sdv #define VCPU_REGS_RCX		1
374bee70036Sdv #define VCPU_REGS_RDX		2
375bee70036Sdv #define VCPU_REGS_RBX		3
376bee70036Sdv #define VCPU_REGS_RSP		4
377bee70036Sdv #define VCPU_REGS_RBP		5
378bee70036Sdv #define VCPU_REGS_RSI		6
379bee70036Sdv #define VCPU_REGS_RDI		7
380bee70036Sdv #define VCPU_REGS_R8		8
381bee70036Sdv #define VCPU_REGS_R9		9
382bee70036Sdv #define VCPU_REGS_R10		10
383bee70036Sdv #define VCPU_REGS_R11		11
384bee70036Sdv #define VCPU_REGS_R12		12
385bee70036Sdv #define VCPU_REGS_R13		13
386bee70036Sdv #define VCPU_REGS_R14		14
387bee70036Sdv #define VCPU_REGS_R15		15
38856794f78Sstefan #define VCPU_REGS_RIP		16
38956794f78Sstefan #define VCPU_REGS_RFLAGS	17
39056794f78Sstefan #define VCPU_REGS_NGPRS		(VCPU_REGS_RFLAGS + 1)
39156794f78Sstefan 
39256794f78Sstefan #define VCPU_REGS_CR0		0
39356794f78Sstefan #define VCPU_REGS_CR2		1
39456794f78Sstefan #define VCPU_REGS_CR3		2
39556794f78Sstefan #define VCPU_REGS_CR4		3
39656794f78Sstefan #define VCPU_REGS_CR8		4
3976ff5f5b7Smlarkin #define VCPU_REGS_XCR0		5
398c96d1163Smlarkin #define VCPU_REGS_PDPTE0 	6
399c96d1163Smlarkin #define VCPU_REGS_PDPTE1 	7
400c96d1163Smlarkin #define VCPU_REGS_PDPTE2 	8
401c96d1163Smlarkin #define VCPU_REGS_PDPTE3 	9
402c96d1163Smlarkin #define VCPU_REGS_NCRS		(VCPU_REGS_PDPTE3 + 1)
40356794f78Sstefan 
404205ff018Sdv #define VCPU_REGS_ES		0
405205ff018Sdv #define VCPU_REGS_CS		1
406205ff018Sdv #define VCPU_REGS_SS		2
407205ff018Sdv #define VCPU_REGS_DS		3
408205ff018Sdv #define VCPU_REGS_FS		4
409205ff018Sdv #define VCPU_REGS_GS		5
41056794f78Sstefan #define VCPU_REGS_LDTR		6
41156794f78Sstefan #define VCPU_REGS_TR		7
41256794f78Sstefan #define VCPU_REGS_NSREGS	(VCPU_REGS_TR + 1)
41356794f78Sstefan 
414dbd8d95aSmlarkin #define VCPU_REGS_EFER   	0
415dbd8d95aSmlarkin #define VCPU_REGS_STAR   	1
416dbd8d95aSmlarkin #define VCPU_REGS_LSTAR  	2
417dbd8d95aSmlarkin #define VCPU_REGS_CSTAR  	3
418dbd8d95aSmlarkin #define VCPU_REGS_SFMASK 	4
419dbd8d95aSmlarkin #define VCPU_REGS_KGSBASE	5
420e12023a7Smlarkin #define VCPU_REGS_MISC_ENABLE	6
421e12023a7Smlarkin #define VCPU_REGS_NMSRS		(VCPU_REGS_MISC_ENABLE + 1)
422dbd8d95aSmlarkin 
42324c8facdSmlarkin #define VCPU_REGS_DR0		0
42424c8facdSmlarkin #define VCPU_REGS_DR1		1
42524c8facdSmlarkin #define VCPU_REGS_DR2		2
42624c8facdSmlarkin #define VCPU_REGS_DR3		3
42724c8facdSmlarkin #define VCPU_REGS_DR6		4
42824c8facdSmlarkin #define VCPU_REGS_DR7		5
42924c8facdSmlarkin #define VCPU_REGS_NDRS		(VCPU_REGS_DR7 + 1)
43024c8facdSmlarkin 
43156794f78Sstefan struct vcpu_reg_state {
43256794f78Sstefan 	uint64_t			vrs_gprs[VCPU_REGS_NGPRS];
43356794f78Sstefan 	uint64_t			vrs_crs[VCPU_REGS_NCRS];
434dbd8d95aSmlarkin 	uint64_t			vrs_msrs[VCPU_REGS_NMSRS];
43524c8facdSmlarkin 	uint64_t			vrs_drs[VCPU_REGS_NDRS];
43656794f78Sstefan 	struct vcpu_segment_info	vrs_sregs[VCPU_REGS_NSREGS];
43756794f78Sstefan 	struct vcpu_segment_info	vrs_gdtr;
43856794f78Sstefan 	struct vcpu_segment_info	vrs_idtr;
43956794f78Sstefan };
44056794f78Sstefan 
441c04d1b34Sguenther #define VCPU_HOST_REGS_EFER   		0
442c04d1b34Sguenther #define VCPU_HOST_REGS_STAR   		1
443c04d1b34Sguenther #define VCPU_HOST_REGS_LSTAR  		2
444c04d1b34Sguenther #define VCPU_HOST_REGS_CSTAR  		3
445c04d1b34Sguenther #define VCPU_HOST_REGS_SFMASK 		4
446c04d1b34Sguenther #define VCPU_HOST_REGS_KGSBASE		5
447c04d1b34Sguenther #define VCPU_HOST_REGS_MISC_ENABLE	6
448c04d1b34Sguenther #define VCPU_HOST_REGS_NMSRS		(VCPU_HOST_REGS_MISC_ENABLE + 1)
449c04d1b34Sguenther 
45002ee787fSmlarkin /*
45102ee787fSmlarkin  * struct vm_exit
45202ee787fSmlarkin  *
45302ee787fSmlarkin  * Contains VM exit information communicated to vmd(8). This information is
45402ee787fSmlarkin  * gathered by vmm(4) from the CPU on each exit that requires help from vmd.
45502ee787fSmlarkin  */
45602ee787fSmlarkin struct vm_exit {
45702ee787fSmlarkin 	union {
45802ee787fSmlarkin 		struct vm_exit_inout		vei;	/* IN/OUT exit */
45983422d52Spd 		struct vm_exit_eptviolation	vee;	/* EPT VIOLATION exit*/
46002ee787fSmlarkin 	};
46102ee787fSmlarkin 
46202ee787fSmlarkin 	struct vcpu_reg_state		vrs;
463c5043439Spd 	int				cpl;
46402ee787fSmlarkin };
46502ee787fSmlarkin 
466f3757d05Smlarkin struct vm_intr_params {
467f3757d05Smlarkin 	/* Input parameters to VMM_IOC_INTR */
468f3757d05Smlarkin 	uint32_t		vip_vm_id;
469f3757d05Smlarkin 	uint32_t		vip_vcpu_id;
4701180136eSmlarkin 	uint16_t		vip_intr;
471f3757d05Smlarkin };
472f3757d05Smlarkin 
47356794f78Sstefan #define VM_RWREGS_GPRS	0x1	/* read/write GPRs */
47456794f78Sstefan #define VM_RWREGS_SREGS	0x2	/* read/write segment registers */
47556794f78Sstefan #define VM_RWREGS_CRS	0x4	/* read/write CRs */
476dbd8d95aSmlarkin #define VM_RWREGS_MSRS	0x8	/* read/write MSRs */
47724c8facdSmlarkin #define VM_RWREGS_DRS	0x10	/* read/write DRs */
478dbd8d95aSmlarkin #define VM_RWREGS_ALL	(VM_RWREGS_GPRS | VM_RWREGS_SREGS | VM_RWREGS_CRS | \
47924c8facdSmlarkin     VM_RWREGS_MSRS | VM_RWREGS_DRS)
48056794f78Sstefan 
48156794f78Sstefan struct vm_rwregs_params {
48276e54dacSmlarkin 	/*
48376e54dacSmlarkin 	 * Input/output parameters to VMM_IOC_READREGS /
48476e54dacSmlarkin 	 * VMM_IOC_WRITEREGS
48576e54dacSmlarkin 	 */
48656794f78Sstefan 	uint32_t		vrwp_vm_id;
48756794f78Sstefan 	uint32_t		vrwp_vcpu_id;
48856794f78Sstefan 	uint64_t		vrwp_mask;
48956794f78Sstefan 	struct vcpu_reg_state	vrwp_regs;
49056794f78Sstefan };
49156794f78Sstefan 
49277d6d4a2Smlarkin /* IOCTL definitions */
4939d87b43bSstefan #define VMM_IOC_INTR _IOW('V', 6, struct vm_intr_params) /* Intr pending */
4942d671a23Spd 
4952d671a23Spd /* CPUID masks */
4962d671a23Spd /*
4972d671a23Spd  * clone host capabilities minus:
4982d671a23Spd  *  debug store (CPUIDECX_DTES64, CPUIDECX_DSCPL, CPUID_DS)
499927beb9eSbrynet  *  monitor/mwait (CPUIDECX_MWAIT, CPUIDECX_MWAITX)
500927beb9eSbrynet  *  vmx/svm (CPUIDECX_VMX, CPUIDECX_SVM)
5012d671a23Spd  *  smx (CPUIDECX_SMX)
5022d671a23Spd  *  speedstep (CPUIDECX_EST)
5032d671a23Spd  *  thermal (CPUIDECX_TM2, CPUID_ACPI, CPUID_TM)
5042d671a23Spd  *  context id (CPUIDECX_CNXTID)
5059b127661Smlarkin  *  machine check (CPUID_MCE, CPUID_MCA)
5062d671a23Spd  *  silicon debug (CPUIDECX_SDBG)
5072d671a23Spd  *  xTPR (CPUIDECX_XTPR)
5082d671a23Spd  *  perf/debug (CPUIDECX_PDCM)
5092d671a23Spd  *  pcid (CPUIDECX_PCID)
5102d671a23Spd  *  direct cache access (CPUIDECX_DCA)
5112d671a23Spd  *  x2APIC (CPUIDECX_X2APIC)
5122d671a23Spd  *  apic deadline (CPUIDECX_DEADLINE)
5132d671a23Spd  *  apic (CPUID_APIC)
5142d671a23Spd  *  psn (CPUID_PSN)
5152d671a23Spd  *  self snoop (CPUID_SS)
5162d671a23Spd  *  hyperthreading (CPUID_HTT)
5172d671a23Spd  *  pending break enabled (CPUID_PBE)
5182d671a23Spd  *  MTRR (CPUID_MTRR)
519dc68203cSmlarkin  *  Speculative execution control features (AMD)
5202d671a23Spd  */
5212d671a23Spd #define VMM_CPUIDECX_MASK ~(CPUIDECX_EST | CPUIDECX_TM2 | CPUIDECX_MWAIT | \
5222d671a23Spd     CPUIDECX_PDCM | CPUIDECX_VMX | CPUIDECX_DTES64 | \
5232d671a23Spd     CPUIDECX_DSCPL | CPUIDECX_SMX | CPUIDECX_CNXTID | \
5242d671a23Spd     CPUIDECX_SDBG | CPUIDECX_XTPR | CPUIDECX_PCID | \
5252d671a23Spd     CPUIDECX_DCA | CPUIDECX_X2APIC | CPUIDECX_DEADLINE)
526927beb9eSbrynet #define VMM_ECPUIDECX_MASK ~(CPUIDECX_SVM | CPUIDECX_MWAITX)
5272d671a23Spd #define VMM_CPUIDEDX_MASK ~(CPUID_ACPI | CPUID_TM | \
5282d671a23Spd     CPUID_HTT | CPUID_DS | CPUID_APIC | \
5292d671a23Spd     CPUID_PSN | CPUID_SS | CPUID_PBE | \
5309b127661Smlarkin     CPUID_MTRR | CPUID_MCE | CPUID_MCA)
531dc68203cSmlarkin #define VMM_AMDSPEC_EBX_MASK ~(CPUIDEBX_IBPB | CPUIDEBX_IBRS | \
532dc68203cSmlarkin     CPUIDEBX_STIBP | CPUIDEBX_IBRS_ALWAYSON | CPUIDEBX_STIBP_ALWAYSON | \
533dc68203cSmlarkin     CPUIDEBX_IBRS_PREF | CPUIDEBX_SSBD | CPUIDEBX_VIRT_SSBD | \
534dc68203cSmlarkin     CPUIDEBX_SSBD_NOTREQ)
53507b219feSmlarkin 
53607b219feSmlarkin /* This mask is an include list for bits we want to expose */
53707b219feSmlarkin #define VMM_APMI_EDX_INCLUDE_MASK (CPUIDEDX_ITSC)
5382d671a23Spd 
5392d671a23Spd /*
5402d671a23Spd  * SEFF flags - copy from host minus:
541594798daSdv  *  TSC_ADJUST (SEFF0EBX_TSC_ADJUST)
5422d671a23Spd  *  SGX (SEFF0EBX_SGX)
5432d671a23Spd  *  HLE (SEFF0EBX_HLE)
5442d671a23Spd  *  INVPCID (SEFF0EBX_INVPCID)
5452d671a23Spd  *  RTM (SEFF0EBX_RTM)
5462d671a23Spd  *  PQM (SEFF0EBX_PQM)
5472d671a23Spd  *  AVX512F (SEFF0EBX_AVX512F)
5482d671a23Spd  *  AVX512DQ (SEFF0EBX_AVX512DQ)
5492d671a23Spd  *  AVX512IFMA (SEFF0EBX_AVX512IFMA)
5502d671a23Spd  *  AVX512PF (SEFF0EBX_AVX512PF)
5512d671a23Spd  *  AVX512ER (SEFF0EBX_AVX512ER)
5522d671a23Spd  *  AVX512CD (SEFF0EBX_AVX512CD)
5532d671a23Spd  *  AVX512BW (SEFF0EBX_AVX512BW)
5542d671a23Spd  *  AVX512VL (SEFF0EBX_AVX512VL)
5552d671a23Spd  *  MPX (SEFF0EBX_MPX)
5562d671a23Spd  *  PCOMMIT (SEFF0EBX_PCOMMIT)
5572d671a23Spd  *  PT (SEFF0EBX_PT)
5582d671a23Spd  */
559594798daSdv #define VMM_SEFF0EBX_MASK ~(SEFF0EBX_TSC_ADJUST | SEFF0EBX_SGX | \
560594798daSdv     SEFF0EBX_HLE | SEFF0EBX_INVPCID | \
5612d671a23Spd     SEFF0EBX_RTM | SEFF0EBX_PQM | SEFF0EBX_MPX | \
5622d671a23Spd     SEFF0EBX_PCOMMIT | SEFF0EBX_PT | \
5632d671a23Spd     SEFF0EBX_AVX512F | SEFF0EBX_AVX512DQ | \
5642d671a23Spd     SEFF0EBX_AVX512IFMA | SEFF0EBX_AVX512PF | \
5652d671a23Spd     SEFF0EBX_AVX512ER | SEFF0EBX_AVX512CD | \
5662d671a23Spd     SEFF0EBX_AVX512BW | SEFF0EBX_AVX512VL)
567c2cbc5b2Sdv 
568c2cbc5b2Sdv /* ECX mask contains the bits to include */
569fbddb143Sdv #define VMM_SEFF0ECX_MASK (SEFF0ECX_UMIP)
5702d671a23Spd 
571a0dcb178Sguenther /* EDX mask contains the bits to include */
572a0dcb178Sguenther #define VMM_SEFF0EDX_MASK (SEFF0EDX_MD_CLEAR)
573a0dcb178Sguenther 
574bc6dc3e1Smlarkin /*
575bc6dc3e1Smlarkin  * Extended function flags - copy from host minus:
576bc6dc3e1Smlarkin  * 0x80000001  EDX:RDTSCP Support
577bc6dc3e1Smlarkin  */
578bc6dc3e1Smlarkin #define VMM_FEAT_EFLAGS_MASK ~(CPUID_RDTSCP)
579bc6dc3e1Smlarkin 
5808fd32492Smlarkin /*
5818fd32492Smlarkin  * CPUID[0x4] deterministic cache info
5828fd32492Smlarkin  */
5838fd32492Smlarkin #define VMM_CPUID4_CACHE_TOPOLOGY_MASK	0x3FF
5848fd32492Smlarkin 
58577d6d4a2Smlarkin #ifdef _KERNEL
58677d6d4a2Smlarkin 
58777d6d4a2Smlarkin #define VMX_FAIL_LAUNCH_UNKNOWN 	1
58877d6d4a2Smlarkin #define VMX_FAIL_LAUNCH_INVALID_VMCS	2
58977d6d4a2Smlarkin #define VMX_FAIL_LAUNCH_VALID_VMCS	3
59077d6d4a2Smlarkin 
591bb3bffd3Smlarkin /* MSR bitmap manipulation macros */
592036cbf87Smlarkin #define VMX_MSRIDX(m)			((m) / 8)
593036cbf87Smlarkin #define VMX_MSRBIT(m)			(1 << (m) % 8)
594bb3bffd3Smlarkin 
5950dd3b0e0Smlarkin #define SVM_MSRIDX(m)			((m) / 4)
5960dd3b0e0Smlarkin #define SVM_MSRBIT_R(m)			(1 << (((m) % 4) * 2))
5970dd3b0e0Smlarkin #define SVM_MSRBIT_W(m)			(1 << (((m) % 4) * 2 + 1))
5980dd3b0e0Smlarkin 
59977d6d4a2Smlarkin enum {
60077d6d4a2Smlarkin 	VMM_MODE_UNKNOWN,
60177d6d4a2Smlarkin 	VMM_MODE_EPT,
60277d6d4a2Smlarkin 	VMM_MODE_RVI
60377d6d4a2Smlarkin };
60477d6d4a2Smlarkin 
60577d6d4a2Smlarkin enum {
60677d6d4a2Smlarkin 	VMM_MEM_TYPE_REGULAR,
60718126f0eSdv 	VMM_MEM_TYPE_MMIO,
60877d6d4a2Smlarkin 	VMM_MEM_TYPE_UNKNOWN
60977d6d4a2Smlarkin };
61077d6d4a2Smlarkin 
61177d6d4a2Smlarkin /* Forward declarations */
61277d6d4a2Smlarkin struct vm;
613ebaf145fSbluhm struct vm_create_params;
61477d6d4a2Smlarkin 
61577d6d4a2Smlarkin /*
61677d6d4a2Smlarkin  * Implementation-specific cpu state
61777d6d4a2Smlarkin  */
618981492f0Smlarkin 
619981492f0Smlarkin struct vmcb_segment {
620981492f0Smlarkin 	uint16_t 			vs_sel;			/* 000h */
621981492f0Smlarkin 	uint16_t 			vs_attr;		/* 002h */
622981492f0Smlarkin 	uint32_t			vs_lim;			/* 004h */
623981492f0Smlarkin 	uint64_t			vs_base;		/* 008h */
624981492f0Smlarkin };
625981492f0Smlarkin 
626ebaf145fSbluhm #define SVM_ENABLE_NP	(1ULL << 0)
627ebaf145fSbluhm #define SVM_ENABLE_SEV	(1ULL << 1)
628ebaf145fSbluhm 
62977d6d4a2Smlarkin struct vmcb {
630981492f0Smlarkin 	union {
631981492f0Smlarkin 		struct {
632981492f0Smlarkin 			uint32_t	v_cr_rw;		/* 000h */
633981492f0Smlarkin 			uint32_t	v_dr_rw;		/* 004h */
634981492f0Smlarkin 			uint32_t	v_excp;			/* 008h */
635981492f0Smlarkin 			uint32_t	v_intercept1;		/* 00Ch */
636981492f0Smlarkin 			uint32_t	v_intercept2;		/* 010h */
637981492f0Smlarkin 			uint8_t		v_pad1[0x28];		/* 014h-03Bh */
638981492f0Smlarkin 			uint16_t	v_pause_thr;		/* 03Ch */
639981492f0Smlarkin 			uint16_t	v_pause_ct;		/* 03Eh */
640981492f0Smlarkin 			uint64_t	v_iopm_pa;		/* 040h */
641981492f0Smlarkin 			uint64_t	v_msrpm_pa;		/* 048h */
642981492f0Smlarkin 			uint64_t	v_tsc_offset;		/* 050h */
643981492f0Smlarkin 			uint32_t	v_asid;			/* 058h */
644981492f0Smlarkin 			uint8_t		v_tlb_control;		/* 05Ch */
645981492f0Smlarkin 			uint8_t		v_pad2[0x3];		/* 05Dh-05Fh */
646981492f0Smlarkin 			uint8_t		v_tpr;			/* 060h */
647981492f0Smlarkin 			uint8_t		v_irq;			/* 061h */
648e76e49e6Smlarkin 			uint8_t		v_intr_misc;		/* 062h */
649e76e49e6Smlarkin 			uint8_t		v_intr_masking;		/* 063h */
650e76e49e6Smlarkin 			uint8_t		v_intr_vector;		/* 064h */
651981492f0Smlarkin 			uint8_t		v_pad3[0x3];		/* 065h-067h */
652981492f0Smlarkin 			uint64_t	v_intr_shadow;		/* 068h */
653981492f0Smlarkin 			uint64_t	v_exitcode;		/* 070h */
654981492f0Smlarkin 			uint64_t	v_exitinfo1;		/* 078h */
655981492f0Smlarkin 			uint64_t	v_exitinfo2;		/* 080h */
656c0a6647eSmlarkin 			uint64_t	v_exitintinfo;		/* 088h */
657981492f0Smlarkin 			uint64_t	v_np_enable;		/* 090h */
658981492f0Smlarkin 			uint64_t	v_avic_apic_bar;	/* 098h */
659981492f0Smlarkin 			uint64_t	v_pad4;			/* 0A0h */
660981492f0Smlarkin 			uint64_t	v_eventinj;		/* 0A8h */
661981492f0Smlarkin 			uint64_t	v_n_cr3;		/* 0B0h */
662981492f0Smlarkin 			uint64_t	v_lbr_virt_enable;	/* 0B8h */
663981492f0Smlarkin 			uint64_t	v_vmcb_clean_bits;	/* 0C0h */
664981492f0Smlarkin 			uint64_t	v_nrip;			/* 0C8h */
665981492f0Smlarkin 			uint8_t		v_n_bytes_fetched;	/* 0D0h */
666981492f0Smlarkin 			uint8_t		v_guest_ins_bytes[0xf];	/* 0D1h-0DFh */
667981492f0Smlarkin 			uint64_t	v_avic_apic_back_page;	/* 0E0h */
668981492f0Smlarkin 			uint64_t	v_pad5;			/* 0E8h-0EFh */
669981492f0Smlarkin 			uint64_t	v_avic_logical_table;	/* 0F0h */
670981492f0Smlarkin 			uint64_t	v_avic_phys;		/* 0F8h */
671981492f0Smlarkin 
672981492f0Smlarkin 		};
673981492f0Smlarkin 		uint8_t			vmcb_control[0x400];
674981492f0Smlarkin 	};
675981492f0Smlarkin 
676981492f0Smlarkin 	union {
677981492f0Smlarkin 		struct {
678981492f0Smlarkin 			/* Offsets here are relative to start of VMCB SSA */
679981492f0Smlarkin 			struct vmcb_segment	v_es;		/* 000h */
680981492f0Smlarkin 			struct vmcb_segment	v_cs;		/* 010h */
681981492f0Smlarkin 			struct vmcb_segment	v_ss;		/* 020h */
682981492f0Smlarkin 			struct vmcb_segment	v_ds;		/* 030h */
683981492f0Smlarkin 			struct vmcb_segment	v_fs;		/* 040h */
684981492f0Smlarkin 			struct vmcb_segment	v_gs;		/* 050h */
685981492f0Smlarkin 			struct vmcb_segment	v_gdtr;		/* 060h */
686981492f0Smlarkin 			struct vmcb_segment	v_ldtr;		/* 070h */
687981492f0Smlarkin 			struct vmcb_segment	v_idtr;		/* 080h */
688981492f0Smlarkin 			struct vmcb_segment	v_tr;		/* 090h */
689981492f0Smlarkin 			uint8_t 		v_pad6[0x2B];	/* 0A0h-0CAh */
690981492f0Smlarkin 			uint8_t			v_cpl;		/* 0CBh */
691981492f0Smlarkin 			uint32_t		v_pad7;		/* 0CCh-0CFh */
692981492f0Smlarkin 			uint64_t		v_efer;		/* 0D0h */
693981492f0Smlarkin 			uint8_t			v_pad8[0x70];	/* 0D8h-147h */
694981492f0Smlarkin 			uint64_t		v_cr4;		/* 148h */
695981492f0Smlarkin 			uint64_t		v_cr3;		/* 150h */
696981492f0Smlarkin 			uint64_t		v_cr0;		/* 158h */
697981492f0Smlarkin 			uint64_t		v_dr7;		/* 160h */
698981492f0Smlarkin 			uint64_t		v_dr6;		/* 168h */
699981492f0Smlarkin 			uint64_t		v_rflags;	/* 170h */
700981492f0Smlarkin 			uint64_t		v_rip;		/* 178h */
701981492f0Smlarkin 			uint64_t		v_pad9[0xB];	/* 180h-1D7h */
702981492f0Smlarkin 			uint64_t		v_rsp;		/* 1D8h */
703981492f0Smlarkin 			uint64_t		v_pad10[0x3];	/* 1E0h-1F7h */
704981492f0Smlarkin 			uint64_t		v_rax;		/* 1F8h */
705981492f0Smlarkin 			uint64_t		v_star;		/* 200h */
706981492f0Smlarkin 			uint64_t		v_lstar;	/* 208h */
707981492f0Smlarkin 			uint64_t		v_cstar;	/* 210h */
708981492f0Smlarkin 			uint64_t		v_sfmask;	/* 218h */
709981492f0Smlarkin 			uint64_t		v_kgsbase;	/* 220h */
710981492f0Smlarkin 			uint64_t		v_sysenter_cs;	/* 228h */
711981492f0Smlarkin 			uint64_t		v_sysenter_esp;	/* 230h */
712981492f0Smlarkin 			uint64_t		v_sysenter_eip;	/* 238h */
713981492f0Smlarkin 			uint64_t		v_cr2;		/* 240h */
714981492f0Smlarkin 			uint64_t		v_pad11[0x4];	/* 248h-267h */
715981492f0Smlarkin 			uint64_t		v_g_pat;	/* 268h */
716981492f0Smlarkin 			uint64_t		v_dbgctl;	/* 270h */
717981492f0Smlarkin 			uint64_t		v_br_from;	/* 278h */
718981492f0Smlarkin 			uint64_t		v_br_to;	/* 280h */
719981492f0Smlarkin 			uint64_t		v_lastexcpfrom;	/* 288h */
720981492f0Smlarkin 			uint64_t		v_lastexcpto;	/* 290h */
721981492f0Smlarkin 		};
722981492f0Smlarkin 		uint8_t				vmcb_layout[PAGE_SIZE - 0x400];
723981492f0Smlarkin 	};
72477d6d4a2Smlarkin };
72577d6d4a2Smlarkin 
72677d6d4a2Smlarkin struct vmcs {
72777d6d4a2Smlarkin 	uint32_t	vmcs_revision;
72877d6d4a2Smlarkin };
72977d6d4a2Smlarkin 
730*fabcfecbSjsg struct vmx_invvpid_descriptor {
73141556e47Smlarkin 	uint64_t	vid_vpid;
73277d6d4a2Smlarkin 	uint64_t	vid_addr;
73377d6d4a2Smlarkin };
73477d6d4a2Smlarkin 
735*fabcfecbSjsg struct vmx_invept_descriptor {
73677d6d4a2Smlarkin 	uint64_t	vid_eptp;
73777d6d4a2Smlarkin 	uint64_t	vid_reserved;
73877d6d4a2Smlarkin };
73977d6d4a2Smlarkin 
740*fabcfecbSjsg struct vmx_msr_store {
74141556e47Smlarkin 	uint64_t	vms_index;
74277d6d4a2Smlarkin 	uint64_t	vms_data;
74377d6d4a2Smlarkin };
74477d6d4a2Smlarkin 
74577d6d4a2Smlarkin /*
74677d6d4a2Smlarkin  * Storage for guest registers not preserved in VMCS and various exit
74777d6d4a2Smlarkin  * information.
74877d6d4a2Smlarkin  *
749f63ef270Smlarkin  * Note that vmx/svm_enter_guest depend on the layout of this struct for
75077d6d4a2Smlarkin  * field access.
75177d6d4a2Smlarkin  */
752*fabcfecbSjsg struct vcpu_gueststate {
75377d6d4a2Smlarkin 	/* %rsi should be first */
75477d6d4a2Smlarkin 	uint64_t	vg_rsi;			/* 0x00 */
75577d6d4a2Smlarkin 	uint64_t	vg_rax;			/* 0x08 */
75677d6d4a2Smlarkin 	uint64_t	vg_rbx;			/* 0x10 */
75777d6d4a2Smlarkin 	uint64_t	vg_rcx;			/* 0x18 */
75877d6d4a2Smlarkin 	uint64_t	vg_rdx;			/* 0x20 */
75977d6d4a2Smlarkin 	uint64_t	vg_rdi;			/* 0x28 */
76077d6d4a2Smlarkin 	uint64_t	vg_rbp;			/* 0x30 */
76177d6d4a2Smlarkin 	uint64_t	vg_r8;			/* 0x38 */
76277d6d4a2Smlarkin 	uint64_t	vg_r9;			/* 0x40 */
76377d6d4a2Smlarkin 	uint64_t	vg_r10;			/* 0x48 */
76477d6d4a2Smlarkin 	uint64_t	vg_r11;			/* 0x50 */
76577d6d4a2Smlarkin 	uint64_t	vg_r12;			/* 0x58 */
76677d6d4a2Smlarkin 	uint64_t	vg_r13;			/* 0x60 */
76777d6d4a2Smlarkin 	uint64_t	vg_r14;			/* 0x68 */
76877d6d4a2Smlarkin 	uint64_t	vg_r15;			/* 0x70 */
76977d6d4a2Smlarkin 	uint64_t	vg_cr2;			/* 0x78 */
77077d6d4a2Smlarkin 	uint64_t	vg_rip;			/* 0x80 */
77177d6d4a2Smlarkin 	uint32_t	vg_exit_reason;		/* 0x88 */
772172bac09Smlarkin 	uint64_t	vg_rflags;		/* 0x90 */
773c86bb406Smlarkin 	uint64_t	vg_xcr0;		/* 0x98 */
77424c8facdSmlarkin 	/*
77524c8facdSmlarkin 	 * Debug registers
77624c8facdSmlarkin 	 * - %dr4/%dr5 are aliased to %dr6/%dr7 (or cause #DE)
77724c8facdSmlarkin 	 * - %dr7 is saved automatically in the VMCS
77824c8facdSmlarkin 	 */
77924c8facdSmlarkin 	uint64_t	vg_dr0;			/* 0xa0 */
78024c8facdSmlarkin 	uint64_t	vg_dr1;			/* 0xa8 */
78124c8facdSmlarkin 	uint64_t	vg_dr2;			/* 0xb0 */
78224c8facdSmlarkin 	uint64_t	vg_dr3;			/* 0xb8 */
78324c8facdSmlarkin 	uint64_t	vg_dr6;			/* 0xc0 */
78477d6d4a2Smlarkin };
78577d6d4a2Smlarkin 
78677d6d4a2Smlarkin /*
78777d6d4a2Smlarkin  * Virtual CPU
7883274ac03Sdv  *
7893274ac03Sdv  * Methods used to vcpu struct members:
7903274ac03Sdv  *	a	atomic operations
7913274ac03Sdv  *	I	immutable operations
7923274ac03Sdv  *	K	kernel lock
7933274ac03Sdv  *	r	reference count
7943274ac03Sdv  *	v	vcpu rwlock
7953274ac03Sdv  *	V	vm struct's vcpu list lock (vm_vcpu_lock)
79677d6d4a2Smlarkin  */
79777d6d4a2Smlarkin struct vcpu {
798c86bb406Smlarkin 	/*
799c86bb406Smlarkin 	 * Guest FPU state - this must remain as the first member of the struct
800c86bb406Smlarkin 	 * to ensure 64-byte alignment (set up during vcpu_pool init)
801c86bb406Smlarkin 	 */
8023274ac03Sdv 	struct savefpu vc_g_fpu;		/* [v] */
803c86bb406Smlarkin 
80477d6d4a2Smlarkin 	/* VMCS / VMCB pointer */
8053274ac03Sdv 	vaddr_t vc_control_va;			/* [I] */
8063274ac03Sdv 	paddr_t vc_control_pa;			/* [I] */
80777d6d4a2Smlarkin 
80877d6d4a2Smlarkin 	/* VLAPIC pointer */
8093274ac03Sdv 	vaddr_t vc_vlapic_va;			/* [I] */
8103274ac03Sdv 	uint64_t vc_vlapic_pa;			/* [I] */
81177d6d4a2Smlarkin 
81277d6d4a2Smlarkin 	/* MSR bitmap address */
8133274ac03Sdv 	vaddr_t vc_msr_bitmap_va;		/* [I] */
8143274ac03Sdv 	uint64_t vc_msr_bitmap_pa;		/* [I] */
81577d6d4a2Smlarkin 
8163274ac03Sdv 	struct vm *vc_parent;			/* [I] */
8173274ac03Sdv 	uint32_t vc_id;				/* [I] */
8183274ac03Sdv 	uint16_t vc_vpid;			/* [I] */
8193274ac03Sdv 	u_int vc_state;				/* [a] */
8203274ac03Sdv 	SLIST_ENTRY(vcpu) vc_vcpu_link;		/* [V] */
82177d6d4a2Smlarkin 
8223274ac03Sdv 	uint8_t vc_virt_mode;			/* [I] */
82377d6d4a2Smlarkin 
8240a894fa6Sdv 	struct rwlock vc_lock;
82577d6d4a2Smlarkin 
8260d8b5945Sdlg 	struct cpu_info *vc_curcpu;		/* [a] */
8273274ac03Sdv 	struct cpu_info *vc_last_pcpu;		/* [v] */
8283274ac03Sdv 	struct vm_exit vc_exit;			/* [v] */
829f3757d05Smlarkin 
8303274ac03Sdv 	uint16_t vc_intr;			/* [v] */
8313274ac03Sdv 	uint8_t vc_irqready;			/* [v] */
832c86bb406Smlarkin 
8333274ac03Sdv 	uint8_t vc_fpuinited;			/* [v] */
834c86bb406Smlarkin 
8353274ac03Sdv 	uint64_t vc_h_xcr0;			/* [v] */
8363274ac03Sdv 
8373274ac03Sdv 	struct vcpu_gueststate vc_gueststate;	/* [v] */
8381ebbcee8Sdv 	struct vcpu_inject_event vc_inject;	/* [v] */
8395e15c369Smlarkin 
8403274ac03Sdv 	uint32_t vc_pvclock_version;		/* [v] */
8413274ac03Sdv 	paddr_t vc_pvclock_system_gpa;		/* [v] */
8423274ac03Sdv 	uint32_t vc_pvclock_system_tsc_mul;	/* [v] */
843b5ec98ccSpd 
84453ca2301Sdv 	/* Shadowed MSRs */
8453274ac03Sdv 	uint64_t vc_shadow_pat;			/* [v] */
84653ca2301Sdv 
84789e94d10Sdv 	/* Userland Protection Keys */
84889e94d10Sdv 	uint32_t vc_pkru;			/* [v] */
84989e94d10Sdv 
8503274ac03Sdv 	/* VMX only (all requiring [v]) */
85177d6d4a2Smlarkin 	uint64_t vc_vmx_basic;
85277d6d4a2Smlarkin 	uint64_t vc_vmx_entry_ctls;
85377d6d4a2Smlarkin 	uint64_t vc_vmx_true_entry_ctls;
85477d6d4a2Smlarkin 	uint64_t vc_vmx_exit_ctls;
85577d6d4a2Smlarkin 	uint64_t vc_vmx_true_exit_ctls;
85677d6d4a2Smlarkin 	uint64_t vc_vmx_pinbased_ctls;
85777d6d4a2Smlarkin 	uint64_t vc_vmx_true_pinbased_ctls;
85877d6d4a2Smlarkin 	uint64_t vc_vmx_procbased_ctls;
85977d6d4a2Smlarkin 	uint64_t vc_vmx_true_procbased_ctls;
86077d6d4a2Smlarkin 	uint64_t vc_vmx_procbased2_ctls;
86177d6d4a2Smlarkin 	vaddr_t vc_vmx_msr_exit_save_va;
86277d6d4a2Smlarkin 	paddr_t vc_vmx_msr_exit_save_pa;
86377d6d4a2Smlarkin 	vaddr_t vc_vmx_msr_exit_load_va;
86477d6d4a2Smlarkin 	paddr_t vc_vmx_msr_exit_load_pa;
865c04d1b34Sguenther #if 0	/* XXX currently use msr_exit_save for msr_entry_load too */
86677d6d4a2Smlarkin 	vaddr_t vc_vmx_msr_entry_load_va;
86777d6d4a2Smlarkin 	paddr_t vc_vmx_msr_entry_load_pa;
868c04d1b34Sguenther #endif
869c3b10582Smlarkin 	uint8_t vc_vmx_vpid_enabled;
870446b7a28Spd 	uint64_t vc_vmx_cr0_fixed1;
871446b7a28Spd 	uint64_t vc_vmx_cr0_fixed0;
8723274ac03Sdv 	uint32_t vc_vmx_vmcs_state;		/* [a] */
8730a894fa6Sdv #define VMCS_CLEARED	0
8740a894fa6Sdv #define VMCS_LAUNCHED	1
8752439bcd4Smlarkin 
8763274ac03Sdv 	/* SVM only (all requiring [v]) */
8772439bcd4Smlarkin 	vaddr_t vc_svm_hsa_va;
8782439bcd4Smlarkin 	paddr_t vc_svm_hsa_pa;
8792439bcd4Smlarkin 	vaddr_t vc_svm_ioio_va;
8802439bcd4Smlarkin 	paddr_t vc_svm_ioio_pa;
881ebaf145fSbluhm 	int vc_sev;				/* [I] */
88277d6d4a2Smlarkin };
88377d6d4a2Smlarkin 
88477d6d4a2Smlarkin SLIST_HEAD(vcpu_head, vcpu);
88577d6d4a2Smlarkin 
88677d6d4a2Smlarkin void	vmm_dispatch_intr(vaddr_t);
88777d6d4a2Smlarkin int	vmxon(uint64_t *);
88877d6d4a2Smlarkin int	vmxoff(void);
8890a894fa6Sdv int	vmclear(paddr_t *);
8900a894fa6Sdv int	vmptrld(paddr_t *);
8910a894fa6Sdv int	vmptrst(paddr_t *);
89277d6d4a2Smlarkin int	vmwrite(uint64_t, uint64_t);
89377d6d4a2Smlarkin int	vmread(uint64_t, uint64_t *);
8948ff82707Sdv int	invvpid(uint64_t, struct vmx_invvpid_descriptor *);
8958ff82707Sdv int	invept(uint64_t, struct vmx_invept_descriptor *);
8960a894fa6Sdv int	vmx_enter_guest(paddr_t *, struct vcpu_gueststate *, int, uint8_t);
897f63ef270Smlarkin int	svm_enter_guest(uint64_t, struct vcpu_gueststate *,
898d307bd3fSmlarkin     struct region_descriptor *);
89977d6d4a2Smlarkin void	start_vmm_on_cpu(struct cpu_info *);
90077d6d4a2Smlarkin void	stop_vmm_on_cpu(struct cpu_info *);
9010a894fa6Sdv void	vmclear_on_cpu(struct cpu_info *);
9023a0db596Smlarkin void	vmm_attach_machdep(struct device *, struct device *, void *);
9033a0db596Smlarkin void	vmm_activate_machdep(struct device *, int);
9043a0db596Smlarkin int	vmmioctl_machdep(dev_t, u_long, caddr_t, int, struct proc *);
9053a0db596Smlarkin int	pledge_ioctl_vmm_machdep(struct proc *, long);
9063a0db596Smlarkin int	vmm_start(void);
9073a0db596Smlarkin int	vmm_stop(void);
9083a0db596Smlarkin int	vm_impl_init(struct vm *, struct proc *);
9093a0db596Smlarkin void	vm_impl_deinit(struct vm *);
910ebaf145fSbluhm int	vcpu_init(struct vcpu *, struct vm_create_params *);
9113a0db596Smlarkin void	vcpu_deinit(struct vcpu *);
9123a0db596Smlarkin int	vm_rwregs(struct vm_rwregs_params *, int);
9133a0db596Smlarkin int	vcpu_reset_regs(struct vcpu *, struct vcpu_reg_state *);
91477d6d4a2Smlarkin 
91577d6d4a2Smlarkin #endif /* _KERNEL */
91677d6d4a2Smlarkin 
91777d6d4a2Smlarkin #endif /* ! _MACHINE_VMMVAR_H_ */
918