kvm_emulate.h 14 KB

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