kvm_emulate.h 15 KB

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