xref: /OK3568_Linux_fs/kernel/arch/x86/kvm/kvm_emulate.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /******************************************************************************
3*4882a593Smuzhiyun  * x86_emulate.h
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (c) 2005 Keir Fraser
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #ifndef _ASM_X86_KVM_X86_EMULATE_H
13*4882a593Smuzhiyun #define _ASM_X86_KVM_X86_EMULATE_H
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <asm/desc_defs.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun struct x86_emulate_ctxt;
18*4882a593Smuzhiyun enum x86_intercept;
19*4882a593Smuzhiyun enum x86_intercept_stage;
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun struct x86_exception {
22*4882a593Smuzhiyun 	u8 vector;
23*4882a593Smuzhiyun 	bool error_code_valid;
24*4882a593Smuzhiyun 	u16 error_code;
25*4882a593Smuzhiyun 	bool nested_page_fault;
26*4882a593Smuzhiyun 	u64 address; /* cr2 or nested page fault gpa */
27*4882a593Smuzhiyun 	u8 async_page_fault;
28*4882a593Smuzhiyun };
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /*
31*4882a593Smuzhiyun  * This struct is used to carry enough information from the instruction
32*4882a593Smuzhiyun  * decoder to main KVM so that a decision can be made whether the
33*4882a593Smuzhiyun  * instruction needs to be intercepted or not.
34*4882a593Smuzhiyun  */
35*4882a593Smuzhiyun struct x86_instruction_info {
36*4882a593Smuzhiyun 	u8  intercept;          /* which intercept                      */
37*4882a593Smuzhiyun 	u8  rep_prefix;         /* rep prefix?                          */
38*4882a593Smuzhiyun 	u8  modrm_mod;		/* mod part of modrm			*/
39*4882a593Smuzhiyun 	u8  modrm_reg;          /* index of register used               */
40*4882a593Smuzhiyun 	u8  modrm_rm;		/* rm part of modrm			*/
41*4882a593Smuzhiyun 	u64 src_val;            /* value of source operand              */
42*4882a593Smuzhiyun 	u64 dst_val;            /* value of destination operand         */
43*4882a593Smuzhiyun 	u8  src_bytes;          /* size of source operand               */
44*4882a593Smuzhiyun 	u8  dst_bytes;          /* size of destination operand          */
45*4882a593Smuzhiyun 	u8  ad_bytes;           /* size of src/dst address              */
46*4882a593Smuzhiyun 	u64 next_rip;           /* rip following the instruction        */
47*4882a593Smuzhiyun };
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun /*
50*4882a593Smuzhiyun  * x86_emulate_ops:
51*4882a593Smuzhiyun  *
52*4882a593Smuzhiyun  * These operations represent the instruction emulator's interface to memory.
53*4882a593Smuzhiyun  * There are two categories of operation: those that act on ordinary memory
54*4882a593Smuzhiyun  * regions (*_std), and those that act on memory regions known to require
55*4882a593Smuzhiyun  * special treatment or emulation (*_emulated).
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * The emulator assumes that an instruction accesses only one 'emulated memory'
58*4882a593Smuzhiyun  * location, that this location is the given linear faulting address (cr2), and
59*4882a593Smuzhiyun  * that this is one of the instruction's data operands. Instruction fetches and
60*4882a593Smuzhiyun  * stack operations are assumed never to access emulated memory. The emulator
61*4882a593Smuzhiyun  * automatically deduces which operand of a string-move operation is accessing
62*4882a593Smuzhiyun  * emulated memory, and assumes that the other operand accesses normal memory.
63*4882a593Smuzhiyun  *
64*4882a593Smuzhiyun  * NOTES:
65*4882a593Smuzhiyun  *  1. The emulator isn't very smart about emulated vs. standard memory.
66*4882a593Smuzhiyun  *     'Emulated memory' access addresses should be checked for sanity.
67*4882a593Smuzhiyun  *     'Normal memory' accesses may fault, and the caller must arrange to
68*4882a593Smuzhiyun  *     detect and handle reentrancy into the emulator via recursive faults.
69*4882a593Smuzhiyun  *     Accesses may be unaligned and may cross page boundaries.
70*4882a593Smuzhiyun  *  2. If the access fails (cannot emulate, or a standard access faults) then
71*4882a593Smuzhiyun  *     it is up to the memop to propagate the fault to the guest VM via
72*4882a593Smuzhiyun  *     some out-of-band mechanism, unknown to the emulator. The memop signals
73*4882a593Smuzhiyun  *     failure by returning X86EMUL_PROPAGATE_FAULT to the emulator, which will
74*4882a593Smuzhiyun  *     then immediately bail.
75*4882a593Smuzhiyun  *  3. Valid access sizes are 1, 2, 4 and 8 bytes. On x86/32 systems only
76*4882a593Smuzhiyun  *     cmpxchg8b_emulated need support 8-byte accesses.
77*4882a593Smuzhiyun  *  4. The emulator cannot handle 64-bit mode emulation on an x86/32 system.
78*4882a593Smuzhiyun  */
79*4882a593Smuzhiyun /* Access completed successfully: continue emulation as normal. */
80*4882a593Smuzhiyun #define X86EMUL_CONTINUE        0
81*4882a593Smuzhiyun /* Access is unhandleable: bail from emulation and return error to caller. */
82*4882a593Smuzhiyun #define X86EMUL_UNHANDLEABLE    1
83*4882a593Smuzhiyun /* Terminate emulation but return success to the caller. */
84*4882a593Smuzhiyun #define X86EMUL_PROPAGATE_FAULT 2 /* propagate a generated fault to guest */
85*4882a593Smuzhiyun #define X86EMUL_RETRY_INSTR     3 /* retry the instruction for some reason */
86*4882a593Smuzhiyun #define X86EMUL_CMPXCHG_FAILED  4 /* cmpxchg did not see expected value */
87*4882a593Smuzhiyun #define X86EMUL_IO_NEEDED       5 /* IO is needed to complete emulation */
88*4882a593Smuzhiyun #define X86EMUL_INTERCEPTED     6 /* Intercepted by nested VMCB/VMCS */
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun struct x86_emulate_ops {
91*4882a593Smuzhiyun 	/*
92*4882a593Smuzhiyun 	 * read_gpr: read a general purpose register (rax - r15)
93*4882a593Smuzhiyun 	 *
94*4882a593Smuzhiyun 	 * @reg: gpr number.
95*4882a593Smuzhiyun 	 */
96*4882a593Smuzhiyun 	ulong (*read_gpr)(struct x86_emulate_ctxt *ctxt, unsigned reg);
97*4882a593Smuzhiyun 	/*
98*4882a593Smuzhiyun 	 * write_gpr: write a general purpose register (rax - r15)
99*4882a593Smuzhiyun 	 *
100*4882a593Smuzhiyun 	 * @reg: gpr number.
101*4882a593Smuzhiyun 	 * @val: value to write.
102*4882a593Smuzhiyun 	 */
103*4882a593Smuzhiyun 	void (*write_gpr)(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val);
104*4882a593Smuzhiyun 	/*
105*4882a593Smuzhiyun 	 * read_std: Read bytes of standard (non-emulated/special) memory.
106*4882a593Smuzhiyun 	 *           Used for descriptor reading.
107*4882a593Smuzhiyun 	 *  @addr:  [IN ] Linear address from which to read.
108*4882a593Smuzhiyun 	 *  @val:   [OUT] Value read from memory, zero-extended to 'u_long'.
109*4882a593Smuzhiyun 	 *  @bytes: [IN ] Number of bytes to read from memory.
110*4882a593Smuzhiyun 	 *  @system:[IN ] Whether the access is forced to be at CPL0.
111*4882a593Smuzhiyun 	 */
112*4882a593Smuzhiyun 	int (*read_std)(struct x86_emulate_ctxt *ctxt,
113*4882a593Smuzhiyun 			unsigned long addr, void *val,
114*4882a593Smuzhiyun 			unsigned int bytes,
115*4882a593Smuzhiyun 			struct x86_exception *fault, bool system);
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	/*
118*4882a593Smuzhiyun 	 * read_phys: Read bytes of standard (non-emulated/special) memory.
119*4882a593Smuzhiyun 	 *            Used for descriptor reading.
120*4882a593Smuzhiyun 	 *  @addr:  [IN ] Physical address from which to read.
121*4882a593Smuzhiyun 	 *  @val:   [OUT] Value read from memory.
122*4882a593Smuzhiyun 	 *  @bytes: [IN ] Number of bytes to read from memory.
123*4882a593Smuzhiyun 	 */
124*4882a593Smuzhiyun 	int (*read_phys)(struct x86_emulate_ctxt *ctxt, unsigned long addr,
125*4882a593Smuzhiyun 			void *val, unsigned int bytes);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	/*
128*4882a593Smuzhiyun 	 * write_std: Write bytes of standard (non-emulated/special) memory.
129*4882a593Smuzhiyun 	 *            Used for descriptor writing.
130*4882a593Smuzhiyun 	 *  @addr:  [IN ] Linear address to which to write.
131*4882a593Smuzhiyun 	 *  @val:   [OUT] Value write to memory, zero-extended to 'u_long'.
132*4882a593Smuzhiyun 	 *  @bytes: [IN ] Number of bytes to write to memory.
133*4882a593Smuzhiyun 	 *  @system:[IN ] Whether the access is forced to be at CPL0.
134*4882a593Smuzhiyun 	 */
135*4882a593Smuzhiyun 	int (*write_std)(struct x86_emulate_ctxt *ctxt,
136*4882a593Smuzhiyun 			 unsigned long addr, void *val, unsigned int bytes,
137*4882a593Smuzhiyun 			 struct x86_exception *fault, bool system);
138*4882a593Smuzhiyun 	/*
139*4882a593Smuzhiyun 	 * fetch: Read bytes of standard (non-emulated/special) memory.
140*4882a593Smuzhiyun 	 *        Used for instruction fetch.
141*4882a593Smuzhiyun 	 *  @addr:  [IN ] Linear address from which to read.
142*4882a593Smuzhiyun 	 *  @val:   [OUT] Value read from memory, zero-extended to 'u_long'.
143*4882a593Smuzhiyun 	 *  @bytes: [IN ] Number of bytes to read from memory.
144*4882a593Smuzhiyun 	 */
145*4882a593Smuzhiyun 	int (*fetch)(struct x86_emulate_ctxt *ctxt,
146*4882a593Smuzhiyun 		     unsigned long addr, void *val, unsigned int bytes,
147*4882a593Smuzhiyun 		     struct x86_exception *fault);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	/*
150*4882a593Smuzhiyun 	 * read_emulated: Read bytes from emulated/special memory area.
151*4882a593Smuzhiyun 	 *  @addr:  [IN ] Linear address from which to read.
152*4882a593Smuzhiyun 	 *  @val:   [OUT] Value read from memory, zero-extended to 'u_long'.
153*4882a593Smuzhiyun 	 *  @bytes: [IN ] Number of bytes to read from memory.
154*4882a593Smuzhiyun 	 */
155*4882a593Smuzhiyun 	int (*read_emulated)(struct x86_emulate_ctxt *ctxt,
156*4882a593Smuzhiyun 			     unsigned long addr, void *val, unsigned int bytes,
157*4882a593Smuzhiyun 			     struct x86_exception *fault);
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	/*
160*4882a593Smuzhiyun 	 * write_emulated: Write bytes to emulated/special memory area.
161*4882a593Smuzhiyun 	 *  @addr:  [IN ] Linear address to which to write.
162*4882a593Smuzhiyun 	 *  @val:   [IN ] Value to write to memory (low-order bytes used as
163*4882a593Smuzhiyun 	 *                required).
164*4882a593Smuzhiyun 	 *  @bytes: [IN ] Number of bytes to write to memory.
165*4882a593Smuzhiyun 	 */
166*4882a593Smuzhiyun 	int (*write_emulated)(struct x86_emulate_ctxt *ctxt,
167*4882a593Smuzhiyun 			      unsigned long addr, const void *val,
168*4882a593Smuzhiyun 			      unsigned int bytes,
169*4882a593Smuzhiyun 			      struct x86_exception *fault);
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	/*
172*4882a593Smuzhiyun 	 * cmpxchg_emulated: Emulate an atomic (LOCKed) CMPXCHG operation on an
173*4882a593Smuzhiyun 	 *                   emulated/special memory area.
174*4882a593Smuzhiyun 	 *  @addr:  [IN ] Linear address to access.
175*4882a593Smuzhiyun 	 *  @old:   [IN ] Value expected to be current at @addr.
176*4882a593Smuzhiyun 	 *  @new:   [IN ] Value to write to @addr.
177*4882a593Smuzhiyun 	 *  @bytes: [IN ] Number of bytes to access using CMPXCHG.
178*4882a593Smuzhiyun 	 */
179*4882a593Smuzhiyun 	int (*cmpxchg_emulated)(struct x86_emulate_ctxt *ctxt,
180*4882a593Smuzhiyun 				unsigned long addr,
181*4882a593Smuzhiyun 				const void *old,
182*4882a593Smuzhiyun 				const void *new,
183*4882a593Smuzhiyun 				unsigned int bytes,
184*4882a593Smuzhiyun 				struct x86_exception *fault);
185*4882a593Smuzhiyun 	void (*invlpg)(struct x86_emulate_ctxt *ctxt, ulong addr);
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	int (*pio_in_emulated)(struct x86_emulate_ctxt *ctxt,
188*4882a593Smuzhiyun 			       int size, unsigned short port, void *val,
189*4882a593Smuzhiyun 			       unsigned int count);
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	int (*pio_out_emulated)(struct x86_emulate_ctxt *ctxt,
192*4882a593Smuzhiyun 				int size, unsigned short port, const void *val,
193*4882a593Smuzhiyun 				unsigned int count);
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	bool (*get_segment)(struct x86_emulate_ctxt *ctxt, u16 *selector,
196*4882a593Smuzhiyun 			    struct desc_struct *desc, u32 *base3, int seg);
197*4882a593Smuzhiyun 	void (*set_segment)(struct x86_emulate_ctxt *ctxt, u16 selector,
198*4882a593Smuzhiyun 			    struct desc_struct *desc, u32 base3, int seg);
199*4882a593Smuzhiyun 	unsigned long (*get_cached_segment_base)(struct x86_emulate_ctxt *ctxt,
200*4882a593Smuzhiyun 						 int seg);
201*4882a593Smuzhiyun 	void (*get_gdt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
202*4882a593Smuzhiyun 	void (*get_idt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
203*4882a593Smuzhiyun 	void (*set_gdt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
204*4882a593Smuzhiyun 	void (*set_idt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
205*4882a593Smuzhiyun 	ulong (*get_cr)(struct x86_emulate_ctxt *ctxt, int cr);
206*4882a593Smuzhiyun 	int (*set_cr)(struct x86_emulate_ctxt *ctxt, int cr, ulong val);
207*4882a593Smuzhiyun 	int (*cpl)(struct x86_emulate_ctxt *ctxt);
208*4882a593Smuzhiyun 	int (*get_dr)(struct x86_emulate_ctxt *ctxt, int dr, ulong *dest);
209*4882a593Smuzhiyun 	int (*set_dr)(struct x86_emulate_ctxt *ctxt, int dr, ulong value);
210*4882a593Smuzhiyun 	u64 (*get_smbase)(struct x86_emulate_ctxt *ctxt);
211*4882a593Smuzhiyun 	void (*set_smbase)(struct x86_emulate_ctxt *ctxt, u64 smbase);
212*4882a593Smuzhiyun 	int (*set_msr)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 data);
213*4882a593Smuzhiyun 	int (*get_msr)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 *pdata);
214*4882a593Smuzhiyun 	int (*check_pmc)(struct x86_emulate_ctxt *ctxt, u32 pmc);
215*4882a593Smuzhiyun 	int (*read_pmc)(struct x86_emulate_ctxt *ctxt, u32 pmc, u64 *pdata);
216*4882a593Smuzhiyun 	void (*halt)(struct x86_emulate_ctxt *ctxt);
217*4882a593Smuzhiyun 	void (*wbinvd)(struct x86_emulate_ctxt *ctxt);
218*4882a593Smuzhiyun 	int (*fix_hypercall)(struct x86_emulate_ctxt *ctxt);
219*4882a593Smuzhiyun 	int (*intercept)(struct x86_emulate_ctxt *ctxt,
220*4882a593Smuzhiyun 			 struct x86_instruction_info *info,
221*4882a593Smuzhiyun 			 enum x86_intercept_stage stage);
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	bool (*get_cpuid)(struct x86_emulate_ctxt *ctxt, u32 *eax, u32 *ebx,
224*4882a593Smuzhiyun 			  u32 *ecx, u32 *edx, bool exact_only);
225*4882a593Smuzhiyun 	bool (*guest_has_long_mode)(struct x86_emulate_ctxt *ctxt);
226*4882a593Smuzhiyun 	bool (*guest_has_movbe)(struct x86_emulate_ctxt *ctxt);
227*4882a593Smuzhiyun 	bool (*guest_has_fxsr)(struct x86_emulate_ctxt *ctxt);
228*4882a593Smuzhiyun 	bool (*guest_has_rdpid)(struct x86_emulate_ctxt *ctxt);
229*4882a593Smuzhiyun 
230*4882a593Smuzhiyun 	void (*set_nmi_mask)(struct x86_emulate_ctxt *ctxt, bool masked);
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	unsigned (*get_hflags)(struct x86_emulate_ctxt *ctxt);
233*4882a593Smuzhiyun 	void (*set_hflags)(struct x86_emulate_ctxt *ctxt, unsigned hflags);
234*4882a593Smuzhiyun 	int (*pre_leave_smm)(struct x86_emulate_ctxt *ctxt,
235*4882a593Smuzhiyun 			     const char *smstate);
236*4882a593Smuzhiyun 	void (*post_leave_smm)(struct x86_emulate_ctxt *ctxt);
237*4882a593Smuzhiyun 	int (*set_xcr)(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr);
238*4882a593Smuzhiyun };
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun typedef u32 __attribute__((vector_size(16))) sse128_t;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun /* Type, address-of, and value of an instruction's operand. */
243*4882a593Smuzhiyun struct operand {
244*4882a593Smuzhiyun 	enum { OP_REG, OP_MEM, OP_MEM_STR, OP_IMM, OP_XMM, OP_MM, OP_NONE } type;
245*4882a593Smuzhiyun 	unsigned int bytes;
246*4882a593Smuzhiyun 	unsigned int count;
247*4882a593Smuzhiyun 	union {
248*4882a593Smuzhiyun 		unsigned long orig_val;
249*4882a593Smuzhiyun 		u64 orig_val64;
250*4882a593Smuzhiyun 	};
251*4882a593Smuzhiyun 	union {
252*4882a593Smuzhiyun 		unsigned long *reg;
253*4882a593Smuzhiyun 		struct segmented_address {
254*4882a593Smuzhiyun 			ulong ea;
255*4882a593Smuzhiyun 			unsigned seg;
256*4882a593Smuzhiyun 		} mem;
257*4882a593Smuzhiyun 		unsigned xmm;
258*4882a593Smuzhiyun 		unsigned mm;
259*4882a593Smuzhiyun 	} addr;
260*4882a593Smuzhiyun 	union {
261*4882a593Smuzhiyun 		unsigned long val;
262*4882a593Smuzhiyun 		u64 val64;
263*4882a593Smuzhiyun 		char valptr[sizeof(sse128_t)];
264*4882a593Smuzhiyun 		sse128_t vec_val;
265*4882a593Smuzhiyun 		u64 mm_val;
266*4882a593Smuzhiyun 		void *data;
267*4882a593Smuzhiyun 	};
268*4882a593Smuzhiyun };
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun struct fetch_cache {
271*4882a593Smuzhiyun 	u8 data[15];
272*4882a593Smuzhiyun 	u8 *ptr;
273*4882a593Smuzhiyun 	u8 *end;
274*4882a593Smuzhiyun };
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun struct read_cache {
277*4882a593Smuzhiyun 	u8 data[1024];
278*4882a593Smuzhiyun 	unsigned long pos;
279*4882a593Smuzhiyun 	unsigned long end;
280*4882a593Smuzhiyun };
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun /* Execution mode, passed to the emulator. */
283*4882a593Smuzhiyun enum x86emul_mode {
284*4882a593Smuzhiyun 	X86EMUL_MODE_REAL,	/* Real mode.             */
285*4882a593Smuzhiyun 	X86EMUL_MODE_VM86,	/* Virtual 8086 mode.     */
286*4882a593Smuzhiyun 	X86EMUL_MODE_PROT16,	/* 16-bit protected mode. */
287*4882a593Smuzhiyun 	X86EMUL_MODE_PROT32,	/* 32-bit protected mode. */
288*4882a593Smuzhiyun 	X86EMUL_MODE_PROT64,	/* 64-bit (long) mode.    */
289*4882a593Smuzhiyun };
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun /* These match some of the HF_* flags defined in kvm_host.h  */
292*4882a593Smuzhiyun #define X86EMUL_GUEST_MASK           (1 << 5) /* VCPU is in guest-mode */
293*4882a593Smuzhiyun #define X86EMUL_SMM_MASK             (1 << 6)
294*4882a593Smuzhiyun #define X86EMUL_SMM_INSIDE_NMI_MASK  (1 << 7)
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun /*
297*4882a593Smuzhiyun  * fastop functions are declared as taking a never-defined fastop parameter,
298*4882a593Smuzhiyun  * so they can't be called from C directly.
299*4882a593Smuzhiyun  */
300*4882a593Smuzhiyun struct fastop;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun typedef void (*fastop_t)(struct fastop *);
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun struct x86_emulate_ctxt {
305*4882a593Smuzhiyun 	void *vcpu;
306*4882a593Smuzhiyun 	const struct x86_emulate_ops *ops;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	/* Register state before/after emulation. */
309*4882a593Smuzhiyun 	unsigned long eflags;
310*4882a593Smuzhiyun 	unsigned long eip; /* eip before instruction emulation */
311*4882a593Smuzhiyun 	/* Emulated execution mode, represented by an X86EMUL_MODE value. */
312*4882a593Smuzhiyun 	enum x86emul_mode mode;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	/* interruptibility state, as a result of execution of STI or MOV SS */
315*4882a593Smuzhiyun 	int interruptibility;
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	bool perm_ok; /* do not check permissions if true */
318*4882a593Smuzhiyun 	bool ud;	/* inject an #UD if host doesn't support insn */
319*4882a593Smuzhiyun 	bool tf;	/* TF value before instruction (after for syscall/sysret) */
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	bool have_exception;
322*4882a593Smuzhiyun 	struct x86_exception exception;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	/* GPA available */
325*4882a593Smuzhiyun 	bool gpa_available;
326*4882a593Smuzhiyun 	gpa_t gpa_val;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	/*
329*4882a593Smuzhiyun 	 * decode cache
330*4882a593Smuzhiyun 	 */
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 	/* current opcode length in bytes */
333*4882a593Smuzhiyun 	u8 opcode_len;
334*4882a593Smuzhiyun 	u8 b;
335*4882a593Smuzhiyun 	u8 intercept;
336*4882a593Smuzhiyun 	u8 op_bytes;
337*4882a593Smuzhiyun 	u8 ad_bytes;
338*4882a593Smuzhiyun 	union {
339*4882a593Smuzhiyun 		int (*execute)(struct x86_emulate_ctxt *ctxt);
340*4882a593Smuzhiyun 		fastop_t fop;
341*4882a593Smuzhiyun 	};
342*4882a593Smuzhiyun 	int (*check_perm)(struct x86_emulate_ctxt *ctxt);
343*4882a593Smuzhiyun 	/*
344*4882a593Smuzhiyun 	 * The following six fields are cleared together,
345*4882a593Smuzhiyun 	 * the rest are initialized unconditionally in x86_decode_insn
346*4882a593Smuzhiyun 	 * or elsewhere
347*4882a593Smuzhiyun 	 */
348*4882a593Smuzhiyun 	bool rip_relative;
349*4882a593Smuzhiyun 	u8 rex_prefix;
350*4882a593Smuzhiyun 	u8 lock_prefix;
351*4882a593Smuzhiyun 	u8 rep_prefix;
352*4882a593Smuzhiyun 	/* bitmaps of registers in _regs[] that can be read */
353*4882a593Smuzhiyun 	u32 regs_valid;
354*4882a593Smuzhiyun 	/* bitmaps of registers in _regs[] that have been written */
355*4882a593Smuzhiyun 	u32 regs_dirty;
356*4882a593Smuzhiyun 	/* modrm */
357*4882a593Smuzhiyun 	u8 modrm;
358*4882a593Smuzhiyun 	u8 modrm_mod;
359*4882a593Smuzhiyun 	u8 modrm_reg;
360*4882a593Smuzhiyun 	u8 modrm_rm;
361*4882a593Smuzhiyun 	u8 modrm_seg;
362*4882a593Smuzhiyun 	u8 seg_override;
363*4882a593Smuzhiyun 	u64 d;
364*4882a593Smuzhiyun 	unsigned long _eip;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	/* Here begins the usercopy section. */
367*4882a593Smuzhiyun 	struct operand src;
368*4882a593Smuzhiyun 	struct operand src2;
369*4882a593Smuzhiyun 	struct operand dst;
370*4882a593Smuzhiyun 	struct operand memop;
371*4882a593Smuzhiyun 	unsigned long _regs[NR_VCPU_REGS];
372*4882a593Smuzhiyun 	struct operand *memopp;
373*4882a593Smuzhiyun 	struct fetch_cache fetch;
374*4882a593Smuzhiyun 	struct read_cache io_read;
375*4882a593Smuzhiyun 	struct read_cache mem_read;
376*4882a593Smuzhiyun };
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun /* Repeat String Operation Prefix */
379*4882a593Smuzhiyun #define REPE_PREFIX	0xf3
380*4882a593Smuzhiyun #define REPNE_PREFIX	0xf2
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun /* CPUID vendors */
383*4882a593Smuzhiyun #define X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx 0x68747541
384*4882a593Smuzhiyun #define X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx 0x444d4163
385*4882a593Smuzhiyun #define X86EMUL_CPUID_VENDOR_AuthenticAMD_edx 0x69746e65
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun #define X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx 0x69444d41
388*4882a593Smuzhiyun #define X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx 0x21726574
389*4882a593Smuzhiyun #define X86EMUL_CPUID_VENDOR_AMDisbetterI_edx 0x74656273
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun #define X86EMUL_CPUID_VENDOR_HygonGenuine_ebx 0x6f677948
392*4882a593Smuzhiyun #define X86EMUL_CPUID_VENDOR_HygonGenuine_ecx 0x656e6975
393*4882a593Smuzhiyun #define X86EMUL_CPUID_VENDOR_HygonGenuine_edx 0x6e65476e
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun #define X86EMUL_CPUID_VENDOR_GenuineIntel_ebx 0x756e6547
396*4882a593Smuzhiyun #define X86EMUL_CPUID_VENDOR_GenuineIntel_ecx 0x6c65746e
397*4882a593Smuzhiyun #define X86EMUL_CPUID_VENDOR_GenuineIntel_edx 0x49656e69
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun #define X86EMUL_CPUID_VENDOR_CentaurHauls_ebx 0x746e6543
400*4882a593Smuzhiyun #define X86EMUL_CPUID_VENDOR_CentaurHauls_ecx 0x736c7561
401*4882a593Smuzhiyun #define X86EMUL_CPUID_VENDOR_CentaurHauls_edx 0x48727561
402*4882a593Smuzhiyun 
is_guest_vendor_intel(u32 ebx,u32 ecx,u32 edx)403*4882a593Smuzhiyun static inline bool is_guest_vendor_intel(u32 ebx, u32 ecx, u32 edx)
404*4882a593Smuzhiyun {
405*4882a593Smuzhiyun 	return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
406*4882a593Smuzhiyun 	       ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
407*4882a593Smuzhiyun 	       edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun 
is_guest_vendor_amd(u32 ebx,u32 ecx,u32 edx)410*4882a593Smuzhiyun static inline bool is_guest_vendor_amd(u32 ebx, u32 ecx, u32 edx)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun 	return (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
413*4882a593Smuzhiyun 		ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
414*4882a593Smuzhiyun 		edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx) ||
415*4882a593Smuzhiyun 	       (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
416*4882a593Smuzhiyun 		ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
417*4882a593Smuzhiyun 		edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx);
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun 
is_guest_vendor_hygon(u32 ebx,u32 ecx,u32 edx)420*4882a593Smuzhiyun static inline bool is_guest_vendor_hygon(u32 ebx, u32 ecx, u32 edx)
421*4882a593Smuzhiyun {
422*4882a593Smuzhiyun 	return ebx == X86EMUL_CPUID_VENDOR_HygonGenuine_ebx &&
423*4882a593Smuzhiyun 	       ecx == X86EMUL_CPUID_VENDOR_HygonGenuine_ecx &&
424*4882a593Smuzhiyun 	       edx == X86EMUL_CPUID_VENDOR_HygonGenuine_edx;
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun enum x86_intercept_stage {
428*4882a593Smuzhiyun 	X86_ICTP_NONE = 0,   /* Allow zero-init to not match anything */
429*4882a593Smuzhiyun 	X86_ICPT_PRE_EXCEPT,
430*4882a593Smuzhiyun 	X86_ICPT_POST_EXCEPT,
431*4882a593Smuzhiyun 	X86_ICPT_POST_MEMACCESS,
432*4882a593Smuzhiyun };
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun enum x86_intercept {
435*4882a593Smuzhiyun 	x86_intercept_none,
436*4882a593Smuzhiyun 	x86_intercept_cr_read,
437*4882a593Smuzhiyun 	x86_intercept_cr_write,
438*4882a593Smuzhiyun 	x86_intercept_clts,
439*4882a593Smuzhiyun 	x86_intercept_lmsw,
440*4882a593Smuzhiyun 	x86_intercept_smsw,
441*4882a593Smuzhiyun 	x86_intercept_dr_read,
442*4882a593Smuzhiyun 	x86_intercept_dr_write,
443*4882a593Smuzhiyun 	x86_intercept_lidt,
444*4882a593Smuzhiyun 	x86_intercept_sidt,
445*4882a593Smuzhiyun 	x86_intercept_lgdt,
446*4882a593Smuzhiyun 	x86_intercept_sgdt,
447*4882a593Smuzhiyun 	x86_intercept_lldt,
448*4882a593Smuzhiyun 	x86_intercept_sldt,
449*4882a593Smuzhiyun 	x86_intercept_ltr,
450*4882a593Smuzhiyun 	x86_intercept_str,
451*4882a593Smuzhiyun 	x86_intercept_rdtsc,
452*4882a593Smuzhiyun 	x86_intercept_rdpmc,
453*4882a593Smuzhiyun 	x86_intercept_pushf,
454*4882a593Smuzhiyun 	x86_intercept_popf,
455*4882a593Smuzhiyun 	x86_intercept_cpuid,
456*4882a593Smuzhiyun 	x86_intercept_rsm,
457*4882a593Smuzhiyun 	x86_intercept_iret,
458*4882a593Smuzhiyun 	x86_intercept_intn,
459*4882a593Smuzhiyun 	x86_intercept_invd,
460*4882a593Smuzhiyun 	x86_intercept_pause,
461*4882a593Smuzhiyun 	x86_intercept_hlt,
462*4882a593Smuzhiyun 	x86_intercept_invlpg,
463*4882a593Smuzhiyun 	x86_intercept_invlpga,
464*4882a593Smuzhiyun 	x86_intercept_vmrun,
465*4882a593Smuzhiyun 	x86_intercept_vmload,
466*4882a593Smuzhiyun 	x86_intercept_vmsave,
467*4882a593Smuzhiyun 	x86_intercept_vmmcall,
468*4882a593Smuzhiyun 	x86_intercept_stgi,
469*4882a593Smuzhiyun 	x86_intercept_clgi,
470*4882a593Smuzhiyun 	x86_intercept_skinit,
471*4882a593Smuzhiyun 	x86_intercept_rdtscp,
472*4882a593Smuzhiyun 	x86_intercept_rdpid,
473*4882a593Smuzhiyun 	x86_intercept_icebp,
474*4882a593Smuzhiyun 	x86_intercept_wbinvd,
475*4882a593Smuzhiyun 	x86_intercept_monitor,
476*4882a593Smuzhiyun 	x86_intercept_mwait,
477*4882a593Smuzhiyun 	x86_intercept_rdmsr,
478*4882a593Smuzhiyun 	x86_intercept_wrmsr,
479*4882a593Smuzhiyun 	x86_intercept_in,
480*4882a593Smuzhiyun 	x86_intercept_ins,
481*4882a593Smuzhiyun 	x86_intercept_out,
482*4882a593Smuzhiyun 	x86_intercept_outs,
483*4882a593Smuzhiyun 	x86_intercept_xsetbv,
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	nr_x86_intercepts
486*4882a593Smuzhiyun };
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun /* Host execution mode. */
489*4882a593Smuzhiyun #if defined(CONFIG_X86_32)
490*4882a593Smuzhiyun #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT32
491*4882a593Smuzhiyun #elif defined(CONFIG_X86_64)
492*4882a593Smuzhiyun #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT64
493*4882a593Smuzhiyun #endif
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len);
496*4882a593Smuzhiyun bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt);
497*4882a593Smuzhiyun #define EMULATION_FAILED -1
498*4882a593Smuzhiyun #define EMULATION_OK 0
499*4882a593Smuzhiyun #define EMULATION_RESTART 1
500*4882a593Smuzhiyun #define EMULATION_INTERCEPTED 2
501*4882a593Smuzhiyun void init_decode_cache(struct x86_emulate_ctxt *ctxt);
502*4882a593Smuzhiyun int x86_emulate_insn(struct x86_emulate_ctxt *ctxt);
503*4882a593Smuzhiyun int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
504*4882a593Smuzhiyun 			 u16 tss_selector, int idt_index, int reason,
505*4882a593Smuzhiyun 			 bool has_error_code, u32 error_code);
506*4882a593Smuzhiyun int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq);
507*4882a593Smuzhiyun void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt);
508*4882a593Smuzhiyun void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt);
509*4882a593Smuzhiyun bool emulator_can_use_gpa(struct x86_emulate_ctxt *ctxt);
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun #endif /* _ASM_X86_KVM_X86_EMULATE_H */
512