kvm_emulate.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. struct x86_exception {
  15. u8 vector;
  16. bool error_code_valid;
  17. u16 error_code;
  18. bool nested_page_fault;
  19. u64 address; /* cr2 or nested page fault gpa */
  20. };
  21. /*
  22. * x86_emulate_ops:
  23. *
  24. * These operations represent the instruction emulator's interface to memory.
  25. * There are two categories of operation: those that act on ordinary memory
  26. * regions (*_std), and those that act on memory regions known to require
  27. * special treatment or emulation (*_emulated).
  28. *
  29. * The emulator assumes that an instruction accesses only one 'emulated memory'
  30. * location, that this location is the given linear faulting address (cr2), and
  31. * that this is one of the instruction's data operands. Instruction fetches and
  32. * stack operations are assumed never to access emulated memory. The emulator
  33. * automatically deduces which operand of a string-move operation is accessing
  34. * emulated memory, and assumes that the other operand accesses normal memory.
  35. *
  36. * NOTES:
  37. * 1. The emulator isn't very smart about emulated vs. standard memory.
  38. * 'Emulated memory' access addresses should be checked for sanity.
  39. * 'Normal memory' accesses may fault, and the caller must arrange to
  40. * detect and handle reentrancy into the emulator via recursive faults.
  41. * Accesses may be unaligned and may cross page boundaries.
  42. * 2. If the access fails (cannot emulate, or a standard access faults) then
  43. * it is up to the memop to propagate the fault to the guest VM via
  44. * some out-of-band mechanism, unknown to the emulator. The memop signals
  45. * failure by returning X86EMUL_PROPAGATE_FAULT to the emulator, which will
  46. * then immediately bail.
  47. * 3. Valid access sizes are 1, 2, 4 and 8 bytes. On x86/32 systems only
  48. * cmpxchg8b_emulated need support 8-byte accesses.
  49. * 4. The emulator cannot handle 64-bit mode emulation on an x86/32 system.
  50. */
  51. /* Access completed successfully: continue emulation as normal. */
  52. #define X86EMUL_CONTINUE 0
  53. /* Access is unhandleable: bail from emulation and return error to caller. */
  54. #define X86EMUL_UNHANDLEABLE 1
  55. /* Terminate emulation but return success to the caller. */
  56. #define X86EMUL_PROPAGATE_FAULT 2 /* propagate a generated fault to guest */
  57. #define X86EMUL_RETRY_INSTR 3 /* retry the instruction for some reason */
  58. #define X86EMUL_CMPXCHG_FAILED 4 /* cmpxchg did not see expected value */
  59. #define X86EMUL_IO_NEEDED 5 /* IO is needed to complete emulation */
  60. struct x86_emulate_ops {
  61. /*
  62. * read_std: Read bytes of standard (non-emulated/special) memory.
  63. * Used for descriptor reading.
  64. * @addr: [IN ] Linear address from which to read.
  65. * @val: [OUT] Value read from memory, zero-extended to 'u_long'.
  66. * @bytes: [IN ] Number of bytes to read from memory.
  67. */
  68. int (*read_std)(unsigned long addr, void *val,
  69. unsigned int bytes, struct kvm_vcpu *vcpu,
  70. struct x86_exception *fault);
  71. /*
  72. * write_std: Write bytes of standard (non-emulated/special) memory.
  73. * Used for descriptor writing.
  74. * @addr: [IN ] Linear address to which to write.
  75. * @val: [OUT] Value write to memory, zero-extended to 'u_long'.
  76. * @bytes: [IN ] Number of bytes to write to memory.
  77. */
  78. int (*write_std)(unsigned long addr, void *val,
  79. unsigned int bytes, struct kvm_vcpu *vcpu,
  80. struct x86_exception *fault);
  81. /*
  82. * fetch: Read bytes of standard (non-emulated/special) memory.
  83. * Used for instruction fetch.
  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 (*fetch)(unsigned long addr, void *val,
  89. unsigned int bytes, struct kvm_vcpu *vcpu,
  90. struct x86_exception *fault);
  91. /*
  92. * read_emulated: Read bytes from emulated/special memory area.
  93. * @addr: [IN ] Linear address from which to read.
  94. * @val: [OUT] Value read from memory, zero-extended to 'u_long'.
  95. * @bytes: [IN ] Number of bytes to read from memory.
  96. */
  97. int (*read_emulated)(unsigned long addr,
  98. void *val,
  99. unsigned int bytes,
  100. struct x86_exception *fault,
  101. struct kvm_vcpu *vcpu);
  102. /*
  103. * write_emulated: Write bytes to emulated/special memory area.
  104. * @addr: [IN ] Linear address to which to write.
  105. * @val: [IN ] Value to write to memory (low-order bytes used as
  106. * required).
  107. * @bytes: [IN ] Number of bytes to write to memory.
  108. */
  109. int (*write_emulated)(unsigned long addr,
  110. const void *val,
  111. unsigned int bytes,
  112. struct x86_exception *fault,
  113. struct kvm_vcpu *vcpu);
  114. /*
  115. * cmpxchg_emulated: Emulate an atomic (LOCKed) CMPXCHG operation on an
  116. * emulated/special memory area.
  117. * @addr: [IN ] Linear address to access.
  118. * @old: [IN ] Value expected to be current at @addr.
  119. * @new: [IN ] Value to write to @addr.
  120. * @bytes: [IN ] Number of bytes to access using CMPXCHG.
  121. */
  122. int (*cmpxchg_emulated)(unsigned long addr,
  123. const void *old,
  124. const void *new,
  125. unsigned int bytes,
  126. struct x86_exception *fault,
  127. struct kvm_vcpu *vcpu);
  128. int (*pio_in_emulated)(int size, unsigned short port, void *val,
  129. unsigned int count, struct kvm_vcpu *vcpu);
  130. int (*pio_out_emulated)(int size, unsigned short port, const void *val,
  131. unsigned int count, struct kvm_vcpu *vcpu);
  132. bool (*get_cached_descriptor)(struct desc_struct *desc, u32 *base3,
  133. int seg, struct kvm_vcpu *vcpu);
  134. void (*set_cached_descriptor)(struct desc_struct *desc, u32 base3,
  135. int seg, struct kvm_vcpu *vcpu);
  136. u16 (*get_segment_selector)(int seg, struct kvm_vcpu *vcpu);
  137. void (*set_segment_selector)(u16 sel, int seg, struct kvm_vcpu *vcpu);
  138. unsigned long (*get_cached_segment_base)(int seg, struct kvm_vcpu *vcpu);
  139. void (*get_gdt)(struct desc_ptr *dt, struct kvm_vcpu *vcpu);
  140. void (*get_idt)(struct desc_ptr *dt, struct kvm_vcpu *vcpu);
  141. ulong (*get_cr)(int cr, struct kvm_vcpu *vcpu);
  142. int (*set_cr)(int cr, ulong val, struct kvm_vcpu *vcpu);
  143. int (*cpl)(struct kvm_vcpu *vcpu);
  144. int (*get_dr)(int dr, unsigned long *dest, struct kvm_vcpu *vcpu);
  145. int (*set_dr)(int dr, unsigned long value, struct kvm_vcpu *vcpu);
  146. int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
  147. int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
  148. };
  149. /* Type, address-of, and value of an instruction's operand. */
  150. struct operand {
  151. enum { OP_REG, OP_MEM, OP_IMM, OP_NONE } type;
  152. unsigned int bytes;
  153. union {
  154. unsigned long orig_val;
  155. u64 orig_val64;
  156. };
  157. union {
  158. unsigned long *reg;
  159. struct segmented_address {
  160. ulong ea;
  161. unsigned seg;
  162. } mem;
  163. } addr;
  164. union {
  165. unsigned long val;
  166. u64 val64;
  167. char valptr[sizeof(unsigned long) + 2];
  168. };
  169. };
  170. struct fetch_cache {
  171. u8 data[15];
  172. unsigned long start;
  173. unsigned long end;
  174. };
  175. struct read_cache {
  176. u8 data[1024];
  177. unsigned long pos;
  178. unsigned long end;
  179. };
  180. struct decode_cache {
  181. u8 twobyte;
  182. u8 b;
  183. u8 lock_prefix;
  184. u8 rep_prefix;
  185. u8 op_bytes;
  186. u8 ad_bytes;
  187. u8 rex_prefix;
  188. struct operand src;
  189. struct operand src2;
  190. struct operand dst;
  191. bool has_seg_override;
  192. u8 seg_override;
  193. unsigned int d;
  194. int (*execute)(struct x86_emulate_ctxt *ctxt);
  195. unsigned long regs[NR_VCPU_REGS];
  196. unsigned long eip;
  197. /* modrm */
  198. u8 modrm;
  199. u8 modrm_mod;
  200. u8 modrm_reg;
  201. u8 modrm_rm;
  202. u8 modrm_seg;
  203. bool rip_relative;
  204. struct fetch_cache fetch;
  205. struct read_cache io_read;
  206. struct read_cache mem_read;
  207. };
  208. struct x86_emulate_ctxt {
  209. struct x86_emulate_ops *ops;
  210. /* Register state before/after emulation. */
  211. struct kvm_vcpu *vcpu;
  212. unsigned long eflags;
  213. unsigned long eip; /* eip before instruction emulation */
  214. /* Emulated execution mode, represented by an X86EMUL_MODE value. */
  215. int mode;
  216. u32 cs_base;
  217. /* interruptibility state, as a result of execution of STI or MOV SS */
  218. int interruptibility;
  219. bool perm_ok; /* do not check permissions if true */
  220. bool only_vendor_specific_insn;
  221. bool have_exception;
  222. struct x86_exception exception;
  223. /* decode cache */
  224. struct decode_cache decode;
  225. };
  226. /* Repeat String Operation Prefix */
  227. #define REPE_PREFIX 1
  228. #define REPNE_PREFIX 2
  229. /* Execution mode, passed to the emulator. */
  230. #define X86EMUL_MODE_REAL 0 /* Real mode. */
  231. #define X86EMUL_MODE_VM86 1 /* Virtual 8086 mode. */
  232. #define X86EMUL_MODE_PROT16 2 /* 16-bit protected mode. */
  233. #define X86EMUL_MODE_PROT32 4 /* 32-bit protected mode. */
  234. #define X86EMUL_MODE_PROT64 8 /* 64-bit (long) mode. */
  235. /* Host execution mode. */
  236. #if defined(CONFIG_X86_32)
  237. #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT32
  238. #elif defined(CONFIG_X86_64)
  239. #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT64
  240. #endif
  241. int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len);
  242. #define EMULATION_FAILED -1
  243. #define EMULATION_OK 0
  244. #define EMULATION_RESTART 1
  245. int x86_emulate_insn(struct x86_emulate_ctxt *ctxt);
  246. int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
  247. u16 tss_selector, int reason,
  248. bool has_error_code, u32 error_code);
  249. int emulate_int_real(struct x86_emulate_ctxt *ctxt,
  250. struct x86_emulate_ops *ops, int irq);
  251. #endif /* _ASM_X86_KVM_X86_EMULATE_H */