kvm_emulate.h 9.2 KB

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