kvm_emulate.h 8.9 KB

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