kvm_emulate.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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_std: Read bytes of standard (non-emulated/special) memory.
  83. * Used for descriptor reading.
  84. * @addr: [IN ] Linear address from which to read.
  85. * @val: [OUT] Value read from memory, zero-extended to 'u_long'.
  86. * @bytes: [IN ] Number of bytes to read from memory.
  87. */
  88. int (*read_std)(struct x86_emulate_ctxt *ctxt,
  89. unsigned long addr, void *val,
  90. unsigned int bytes,
  91. struct x86_exception *fault);
  92. /*
  93. * write_std: Write bytes of standard (non-emulated/special) memory.
  94. * Used for descriptor writing.
  95. * @addr: [IN ] Linear address to which to write.
  96. * @val: [OUT] Value write to memory, zero-extended to 'u_long'.
  97. * @bytes: [IN ] Number of bytes to write to memory.
  98. */
  99. int (*write_std)(struct x86_emulate_ctxt *ctxt,
  100. unsigned long addr, void *val, unsigned int bytes,
  101. struct x86_exception *fault);
  102. /*
  103. * fetch: Read bytes of standard (non-emulated/special) memory.
  104. * Used for instruction fetch.
  105. * @addr: [IN ] Linear address from which to read.
  106. * @val: [OUT] Value read from memory, zero-extended to 'u_long'.
  107. * @bytes: [IN ] Number of bytes to read from memory.
  108. */
  109. int (*fetch)(struct x86_emulate_ctxt *ctxt,
  110. unsigned long addr, void *val, unsigned int bytes,
  111. struct x86_exception *fault);
  112. /*
  113. * read_emulated: Read bytes from emulated/special memory area.
  114. * @addr: [IN ] Linear address from which to read.
  115. * @val: [OUT] Value read from memory, zero-extended to 'u_long'.
  116. * @bytes: [IN ] Number of bytes to read from memory.
  117. */
  118. int (*read_emulated)(struct x86_emulate_ctxt *ctxt,
  119. unsigned long addr, void *val, unsigned int bytes,
  120. struct x86_exception *fault);
  121. /*
  122. * write_emulated: Write bytes to emulated/special memory area.
  123. * @addr: [IN ] Linear address to which to write.
  124. * @val: [IN ] Value to write to memory (low-order bytes used as
  125. * required).
  126. * @bytes: [IN ] Number of bytes to write to memory.
  127. */
  128. int (*write_emulated)(struct x86_emulate_ctxt *ctxt,
  129. unsigned long addr, const void *val,
  130. unsigned int bytes,
  131. struct x86_exception *fault);
  132. /*
  133. * cmpxchg_emulated: Emulate an atomic (LOCKed) CMPXCHG operation on an
  134. * emulated/special memory area.
  135. * @addr: [IN ] Linear address to access.
  136. * @old: [IN ] Value expected to be current at @addr.
  137. * @new: [IN ] Value to write to @addr.
  138. * @bytes: [IN ] Number of bytes to access using CMPXCHG.
  139. */
  140. int (*cmpxchg_emulated)(struct x86_emulate_ctxt *ctxt,
  141. unsigned long addr,
  142. const void *old,
  143. const void *new,
  144. unsigned int bytes,
  145. struct x86_exception *fault);
  146. void (*invlpg)(struct x86_emulate_ctxt *ctxt, ulong addr);
  147. int (*pio_in_emulated)(struct x86_emulate_ctxt *ctxt,
  148. int size, unsigned short port, void *val,
  149. unsigned int count);
  150. int (*pio_out_emulated)(struct x86_emulate_ctxt *ctxt,
  151. int size, unsigned short port, const void *val,
  152. unsigned int count);
  153. bool (*get_segment)(struct x86_emulate_ctxt *ctxt, u16 *selector,
  154. struct desc_struct *desc, u32 *base3, int seg);
  155. void (*set_segment)(struct x86_emulate_ctxt *ctxt, u16 selector,
  156. struct desc_struct *desc, u32 base3, int seg);
  157. unsigned long (*get_cached_segment_base)(struct x86_emulate_ctxt *ctxt,
  158. int seg);
  159. void (*get_gdt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
  160. void (*get_idt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
  161. void (*set_gdt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
  162. void (*set_idt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
  163. ulong (*get_cr)(struct x86_emulate_ctxt *ctxt, int cr);
  164. int (*set_cr)(struct x86_emulate_ctxt *ctxt, int cr, ulong val);
  165. void (*set_rflags)(struct x86_emulate_ctxt *ctxt, ulong val);
  166. int (*cpl)(struct x86_emulate_ctxt *ctxt);
  167. int (*get_dr)(struct x86_emulate_ctxt *ctxt, int dr, ulong *dest);
  168. int (*set_dr)(struct x86_emulate_ctxt *ctxt, int dr, ulong value);
  169. int (*set_msr)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 data);
  170. int (*get_msr)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 *pdata);
  171. int (*read_pmc)(struct x86_emulate_ctxt *ctxt, u32 pmc, u64 *pdata);
  172. void (*halt)(struct x86_emulate_ctxt *ctxt);
  173. void (*wbinvd)(struct x86_emulate_ctxt *ctxt);
  174. int (*fix_hypercall)(struct x86_emulate_ctxt *ctxt);
  175. void (*get_fpu)(struct x86_emulate_ctxt *ctxt); /* disables preempt */
  176. void (*put_fpu)(struct x86_emulate_ctxt *ctxt); /* reenables preempt */
  177. int (*intercept)(struct x86_emulate_ctxt *ctxt,
  178. struct x86_instruction_info *info,
  179. enum x86_intercept_stage stage);
  180. bool (*get_cpuid)(struct x86_emulate_ctxt *ctxt,
  181. u32 *eax, u32 *ebx, u32 *ecx, u32 *edx);
  182. };
  183. typedef u32 __attribute__((vector_size(16))) sse128_t;
  184. /* Type, address-of, and value of an instruction's operand. */
  185. struct operand {
  186. enum { OP_REG, OP_MEM, OP_IMM, OP_XMM, OP_NONE } type;
  187. unsigned int bytes;
  188. union {
  189. unsigned long orig_val;
  190. u64 orig_val64;
  191. };
  192. union {
  193. unsigned long *reg;
  194. struct segmented_address {
  195. ulong ea;
  196. unsigned seg;
  197. } mem;
  198. unsigned xmm;
  199. } addr;
  200. union {
  201. unsigned long val;
  202. u64 val64;
  203. char valptr[sizeof(unsigned long) + 2];
  204. sse128_t vec_val;
  205. };
  206. };
  207. struct fetch_cache {
  208. u8 data[15];
  209. unsigned long start;
  210. unsigned long end;
  211. };
  212. struct read_cache {
  213. u8 data[1024];
  214. unsigned long pos;
  215. unsigned long end;
  216. };
  217. struct x86_emulate_ctxt {
  218. struct x86_emulate_ops *ops;
  219. /* Register state before/after emulation. */
  220. unsigned long eflags;
  221. unsigned long eip; /* eip before instruction emulation */
  222. /* Emulated execution mode, represented by an X86EMUL_MODE value. */
  223. int mode;
  224. /* interruptibility state, as a result of execution of STI or MOV SS */
  225. int interruptibility;
  226. bool guest_mode; /* guest running a nested guest */
  227. bool perm_ok; /* do not check permissions if true */
  228. bool only_vendor_specific_insn;
  229. bool have_exception;
  230. struct x86_exception exception;
  231. /* decode cache */
  232. u8 twobyte;
  233. u8 b;
  234. u8 intercept;
  235. u8 lock_prefix;
  236. u8 rep_prefix;
  237. u8 op_bytes;
  238. u8 ad_bytes;
  239. u8 rex_prefix;
  240. struct operand src;
  241. struct operand src2;
  242. struct operand dst;
  243. bool has_seg_override;
  244. u8 seg_override;
  245. u64 d;
  246. int (*execute)(struct x86_emulate_ctxt *ctxt);
  247. int (*check_perm)(struct x86_emulate_ctxt *ctxt);
  248. /* modrm */
  249. u8 modrm;
  250. u8 modrm_mod;
  251. u8 modrm_reg;
  252. u8 modrm_rm;
  253. u8 modrm_seg;
  254. bool rip_relative;
  255. unsigned long _eip;
  256. /* Fields above regs are cleared together. */
  257. unsigned long regs[NR_VCPU_REGS];
  258. struct operand memop;
  259. struct operand *memopp;
  260. struct fetch_cache fetch;
  261. struct read_cache io_read;
  262. struct read_cache mem_read;
  263. };
  264. /* Repeat String Operation Prefix */
  265. #define REPE_PREFIX 0xf3
  266. #define REPNE_PREFIX 0xf2
  267. /* Execution mode, passed to the emulator. */
  268. #define X86EMUL_MODE_REAL 0 /* Real mode. */
  269. #define X86EMUL_MODE_VM86 1 /* Virtual 8086 mode. */
  270. #define X86EMUL_MODE_PROT16 2 /* 16-bit protected mode. */
  271. #define X86EMUL_MODE_PROT32 4 /* 32-bit protected mode. */
  272. #define X86EMUL_MODE_PROT64 8 /* 64-bit (long) mode. */
  273. /* any protected mode */
  274. #define X86EMUL_MODE_PROT (X86EMUL_MODE_PROT16|X86EMUL_MODE_PROT32| \
  275. X86EMUL_MODE_PROT64)
  276. /* CPUID vendors */
  277. #define X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx 0x68747541
  278. #define X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx 0x444d4163
  279. #define X86EMUL_CPUID_VENDOR_AuthenticAMD_edx 0x69746e65
  280. #define X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx 0x69444d41
  281. #define X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx 0x21726574
  282. #define X86EMUL_CPUID_VENDOR_AMDisbetterI_edx 0x74656273
  283. #define X86EMUL_CPUID_VENDOR_GenuineIntel_ebx 0x756e6547
  284. #define X86EMUL_CPUID_VENDOR_GenuineIntel_ecx 0x6c65746e
  285. #define X86EMUL_CPUID_VENDOR_GenuineIntel_edx 0x49656e69
  286. enum x86_intercept_stage {
  287. X86_ICTP_NONE = 0, /* Allow zero-init to not match anything */
  288. X86_ICPT_PRE_EXCEPT,
  289. X86_ICPT_POST_EXCEPT,
  290. X86_ICPT_POST_MEMACCESS,
  291. };
  292. enum x86_intercept {
  293. x86_intercept_none,
  294. x86_intercept_cr_read,
  295. x86_intercept_cr_write,
  296. x86_intercept_clts,
  297. x86_intercept_lmsw,
  298. x86_intercept_smsw,
  299. x86_intercept_dr_read,
  300. x86_intercept_dr_write,
  301. x86_intercept_lidt,
  302. x86_intercept_sidt,
  303. x86_intercept_lgdt,
  304. x86_intercept_sgdt,
  305. x86_intercept_lldt,
  306. x86_intercept_sldt,
  307. x86_intercept_ltr,
  308. x86_intercept_str,
  309. x86_intercept_rdtsc,
  310. x86_intercept_rdpmc,
  311. x86_intercept_pushf,
  312. x86_intercept_popf,
  313. x86_intercept_cpuid,
  314. x86_intercept_rsm,
  315. x86_intercept_iret,
  316. x86_intercept_intn,
  317. x86_intercept_invd,
  318. x86_intercept_pause,
  319. x86_intercept_hlt,
  320. x86_intercept_invlpg,
  321. x86_intercept_invlpga,
  322. x86_intercept_vmrun,
  323. x86_intercept_vmload,
  324. x86_intercept_vmsave,
  325. x86_intercept_vmmcall,
  326. x86_intercept_stgi,
  327. x86_intercept_clgi,
  328. x86_intercept_skinit,
  329. x86_intercept_rdtscp,
  330. x86_intercept_icebp,
  331. x86_intercept_wbinvd,
  332. x86_intercept_monitor,
  333. x86_intercept_mwait,
  334. x86_intercept_rdmsr,
  335. x86_intercept_wrmsr,
  336. x86_intercept_in,
  337. x86_intercept_ins,
  338. x86_intercept_out,
  339. x86_intercept_outs,
  340. nr_x86_intercepts
  341. };
  342. /* Host execution mode. */
  343. #if defined(CONFIG_X86_32)
  344. #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT32
  345. #elif defined(CONFIG_X86_64)
  346. #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT64
  347. #endif
  348. int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len);
  349. bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt);
  350. #define EMULATION_FAILED -1
  351. #define EMULATION_OK 0
  352. #define EMULATION_RESTART 1
  353. #define EMULATION_INTERCEPTED 2
  354. int x86_emulate_insn(struct x86_emulate_ctxt *ctxt);
  355. int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
  356. u16 tss_selector, int idt_index, int reason,
  357. bool has_error_code, u32 error_code);
  358. int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq);
  359. #endif /* _ASM_X86_KVM_X86_EMULATE_H */