kvm_emulate.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 2 /* retry the instruction for some reason */
  51. #define X86EMUL_CMPXCHG_FAILED 2 /* cmpxchg did not see expected value */
  52. struct x86_emulate_ops {
  53. /*
  54. * read_std: Read bytes of standard (non-emulated/special) memory.
  55. * Used for descriptor reading.
  56. * @addr: [IN ] Linear address from which to read.
  57. * @val: [OUT] Value read from memory, zero-extended to 'u_long'.
  58. * @bytes: [IN ] Number of bytes to read from memory.
  59. */
  60. int (*read_std)(unsigned long addr, void *val,
  61. unsigned int bytes, struct kvm_vcpu *vcpu, u32 *error);
  62. /*
  63. * write_std: Write bytes of standard (non-emulated/special) memory.
  64. * Used for descriptor writing.
  65. * @addr: [IN ] Linear address to which to write.
  66. * @val: [OUT] Value write to memory, zero-extended to 'u_long'.
  67. * @bytes: [IN ] Number of bytes to write to memory.
  68. */
  69. int (*write_std)(unsigned long addr, void *val,
  70. unsigned int bytes, struct kvm_vcpu *vcpu, u32 *error);
  71. /*
  72. * fetch: Read bytes of standard (non-emulated/special) memory.
  73. * Used for instruction fetch.
  74. * @addr: [IN ] Linear address from which to read.
  75. * @val: [OUT] Value read from memory, zero-extended to 'u_long'.
  76. * @bytes: [IN ] Number of bytes to read from memory.
  77. */
  78. int (*fetch)(unsigned long addr, void *val,
  79. unsigned int bytes, struct kvm_vcpu *vcpu, u32 *error);
  80. /*
  81. * read_emulated: Read bytes from emulated/special memory area.
  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 (*read_emulated)(unsigned long addr,
  87. void *val,
  88. unsigned int bytes,
  89. struct kvm_vcpu *vcpu);
  90. /*
  91. * write_emulated: Write bytes to emulated/special memory area.
  92. * @addr: [IN ] Linear address to which to write.
  93. * @val: [IN ] Value to write to memory (low-order bytes used as
  94. * required).
  95. * @bytes: [IN ] Number of bytes to write to memory.
  96. */
  97. int (*write_emulated)(unsigned long addr,
  98. const void *val,
  99. unsigned int bytes,
  100. struct kvm_vcpu *vcpu);
  101. /*
  102. * cmpxchg_emulated: Emulate an atomic (LOCKed) CMPXCHG operation on an
  103. * emulated/special memory area.
  104. * @addr: [IN ] Linear address to access.
  105. * @old: [IN ] Value expected to be current at @addr.
  106. * @new: [IN ] Value to write to @addr.
  107. * @bytes: [IN ] Number of bytes to access using CMPXCHG.
  108. */
  109. int (*cmpxchg_emulated)(unsigned long addr,
  110. const void *old,
  111. const void *new,
  112. unsigned int bytes,
  113. struct kvm_vcpu *vcpu);
  114. int (*pio_in_emulated)(int size, unsigned short port, void *val,
  115. unsigned int count, struct kvm_vcpu *vcpu);
  116. int (*pio_out_emulated)(int size, unsigned short port, const void *val,
  117. unsigned int count, struct kvm_vcpu *vcpu);
  118. bool (*get_cached_descriptor)(struct desc_struct *desc,
  119. int seg, struct kvm_vcpu *vcpu);
  120. void (*set_cached_descriptor)(struct desc_struct *desc,
  121. int seg, struct kvm_vcpu *vcpu);
  122. u16 (*get_segment_selector)(int seg, struct kvm_vcpu *vcpu);
  123. void (*set_segment_selector)(u16 sel, int seg, struct kvm_vcpu *vcpu);
  124. void (*get_gdt)(struct desc_ptr *dt, struct kvm_vcpu *vcpu);
  125. ulong (*get_cr)(int cr, struct kvm_vcpu *vcpu);
  126. void (*set_cr)(int cr, ulong val, struct kvm_vcpu *vcpu);
  127. int (*cpl)(struct kvm_vcpu *vcpu);
  128. void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
  129. };
  130. /* Type, address-of, and value of an instruction's operand. */
  131. struct operand {
  132. enum { OP_REG, OP_MEM, OP_IMM, OP_NONE } type;
  133. unsigned int bytes;
  134. unsigned long val, orig_val, *ptr;
  135. };
  136. struct fetch_cache {
  137. u8 data[15];
  138. unsigned long start;
  139. unsigned long end;
  140. };
  141. struct read_cache {
  142. u8 data[1024];
  143. unsigned long pos;
  144. unsigned long end;
  145. };
  146. struct decode_cache {
  147. u8 twobyte;
  148. u8 b;
  149. u8 lock_prefix;
  150. u8 rep_prefix;
  151. u8 op_bytes;
  152. u8 ad_bytes;
  153. u8 rex_prefix;
  154. struct operand src;
  155. struct operand src2;
  156. struct operand dst;
  157. bool has_seg_override;
  158. u8 seg_override;
  159. unsigned int d;
  160. unsigned long regs[NR_VCPU_REGS];
  161. unsigned long eip;
  162. /* modrm */
  163. u8 modrm;
  164. u8 modrm_mod;
  165. u8 modrm_reg;
  166. u8 modrm_rm;
  167. u8 use_modrm_ea;
  168. bool rip_relative;
  169. unsigned long modrm_ea;
  170. void *modrm_ptr;
  171. unsigned long modrm_val;
  172. struct fetch_cache fetch;
  173. struct read_cache io_read;
  174. };
  175. struct x86_emulate_ctxt {
  176. /* Register state before/after emulation. */
  177. struct kvm_vcpu *vcpu;
  178. unsigned long eflags;
  179. unsigned long eip; /* eip before instruction emulation */
  180. /* Emulated execution mode, represented by an X86EMUL_MODE value. */
  181. int mode;
  182. u32 cs_base;
  183. /* interruptibility state, as a result of execution of STI or MOV SS */
  184. int interruptibility;
  185. bool restart; /* restart string instruction after writeback */
  186. /* decode cache */
  187. struct decode_cache decode;
  188. };
  189. /* Repeat String Operation Prefix */
  190. #define REPE_PREFIX 1
  191. #define REPNE_PREFIX 2
  192. /* Execution mode, passed to the emulator. */
  193. #define X86EMUL_MODE_REAL 0 /* Real mode. */
  194. #define X86EMUL_MODE_VM86 1 /* Virtual 8086 mode. */
  195. #define X86EMUL_MODE_PROT16 2 /* 16-bit protected mode. */
  196. #define X86EMUL_MODE_PROT32 4 /* 32-bit protected mode. */
  197. #define X86EMUL_MODE_PROT64 8 /* 64-bit (long) mode. */
  198. /* Host execution mode. */
  199. #if defined(CONFIG_X86_32)
  200. #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT32
  201. #elif defined(CONFIG_X86_64)
  202. #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT64
  203. #endif
  204. int x86_decode_insn(struct x86_emulate_ctxt *ctxt,
  205. struct x86_emulate_ops *ops);
  206. int x86_emulate_insn(struct x86_emulate_ctxt *ctxt,
  207. struct x86_emulate_ops *ops);
  208. int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
  209. struct x86_emulate_ops *ops,
  210. u16 tss_selector, int reason,
  211. bool has_error_code, u32 error_code);
  212. #endif /* _ASM_X86_KVM_X86_EMULATE_H */