kvm_emulate.h 14 KB

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