xref: /freebsd/sys/amd64/include/vmm_instruction_emul.h (revision ec965063070e5753c166cf592c9336444b74720a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef	_VMM_INSTRUCTION_EMUL_H_
32 #define	_VMM_INSTRUCTION_EMUL_H_
33 
34 #include <sys/mman.h>
35 
36 /*
37  * Allow for different arguments to identify vCPUs in userspace vs the
38  * kernel.  Eventually we should add struct vcpu in userland and
39  * always use the kernel arguments removing these macros.
40  */
41 #ifdef _KERNEL
42 #define	VCPU_DECL	struct vcpu *vcpu
43 #define	VCPU_ARGS	vcpu
44 #else
45 #define	VCPU_DECL	void *vm, int vcpuid
46 #define	VCPU_ARGS	vm, vcpuid
47 #endif
48 
49 /*
50  * Callback functions to read and write memory regions.
51  */
52 typedef int (*mem_region_read_t)(VCPU_DECL, uint64_t gpa,
53 				 uint64_t *rval, int rsize, void *arg);
54 
55 typedef int (*mem_region_write_t)(VCPU_DECL, uint64_t gpa,
56 				  uint64_t wval, int wsize, void *arg);
57 
58 /*
59  * Emulate the decoded 'vie' instruction.
60  *
61  * The callbacks 'mrr' and 'mrw' emulate reads and writes to the memory region
62  * containing 'gpa'. 'mrarg' is an opaque argument that is passed into the
63  * callback functions.
64  *
65  * 'void *vm' should be 'struct vm *' when called from kernel context and
66  * 'struct vmctx *' when called from user context.
67  * s
68  */
69 int vmm_emulate_instruction(VCPU_DECL, uint64_t gpa, struct vie *vie,
70     struct vm_guest_paging *paging, mem_region_read_t mrr,
71     mem_region_write_t mrw, void *mrarg);
72 
73 int vie_update_register(VCPU_DECL, enum vm_reg_name reg,
74     uint64_t val, int size);
75 
76 /*
77  * Returns 1 if an alignment check exception should be injected and 0 otherwise.
78  */
79 int vie_alignment_check(int cpl, int operand_size, uint64_t cr0,
80     uint64_t rflags, uint64_t gla);
81 
82 /* Returns 1 if the 'gla' is not canonical and 0 otherwise. */
83 int vie_canonical_check(enum vm_cpu_mode cpu_mode, uint64_t gla);
84 
85 uint64_t vie_size2mask(int size);
86 
87 int vie_calculate_gla(enum vm_cpu_mode cpu_mode, enum vm_reg_name seg,
88     struct seg_desc *desc, uint64_t off, int length, int addrsize, int prot,
89     uint64_t *gla);
90 
91 #ifdef _KERNEL
92 /*
93  * APIs to fetch and decode the instruction from nested page fault handler.
94  *
95  * 'vie' must be initialized before calling 'vmm_fetch_instruction()'
96  */
97 int vmm_fetch_instruction(struct vcpu *vcpu,
98 			  struct vm_guest_paging *guest_paging,
99 			  uint64_t rip, int inst_length, struct vie *vie,
100 			  int *is_fault);
101 
102 /*
103  * Translate the guest linear address 'gla' to a guest physical address.
104  *
105  * retval	is_fault	Interpretation
106  *   0		   0		'gpa' contains result of the translation
107  *   0		   1		An exception was injected into the guest
108  * EFAULT	  N/A		An unrecoverable hypervisor error occurred
109  */
110 int vm_gla2gpa(struct vcpu *vcpu, struct vm_guest_paging *paging,
111     uint64_t gla, int prot, uint64_t *gpa, int *is_fault);
112 
113 /*
114  * Like vm_gla2gpa, but no exceptions are injected into the guest and
115  * PTEs are not changed.
116  */
117 int vm_gla2gpa_nofault(struct vcpu *vcpu, struct vm_guest_paging *paging,
118     uint64_t gla, int prot, uint64_t *gpa, int *is_fault);
119 #endif /* _KERNEL */
120 
121 void vie_restart(struct vie *vie);
122 void vie_init(struct vie *vie, const char *inst_bytes, int inst_length);
123 
124 /*
125  * Decode the instruction fetched into 'vie' so it can be emulated.
126  *
127  * 'gla' is the guest linear address provided by the hardware assist
128  * that caused the nested page table fault. It is used to verify that
129  * the software instruction decoding is in agreement with the hardware.
130  *
131  * Some hardware assists do not provide the 'gla' to the hypervisor.
132  * To skip the 'gla' verification for this or any other reason pass
133  * in VIE_INVALID_GLA instead.
134  */
135 #ifdef _KERNEL
136 #define	VIE_INVALID_GLA		(1UL << 63)	/* a non-canonical address */
137 int vmm_decode_instruction(struct vcpu *vcpu, uint64_t gla,
138 			   enum vm_cpu_mode cpu_mode, int csd, struct vie *vie);
139 #else /* !_KERNEL */
140 /*
141  * Permit instruction decoding logic to be compiled outside of the kernel for
142  * rapid iteration and validation.  No GLA validation is performed, obviously.
143  */
144 int vmm_decode_instruction(enum vm_cpu_mode cpu_mode, int csd,
145     struct vie *vie);
146 #endif	/* _KERNEL */
147 
148 #endif	/* _VMM_INSTRUCTION_EMUL_H_ */
149