kvm_emulate.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /******************************************************************************
  3. * x86_emulate.h
  4. *
  5. * Generic x86 (32-bit and 64-bit) instruction decoder and emulator.
  6. *
  7. * Copyright (c) 2005 Keir Fraser
  8. *
  9. * From: xen-unstable 10676:af9809f51f81a3c43f276f00c81a52ef558afda4
  10. */
  11. #ifndef _ASM_X86_KVM_X86_EMULATE_H
  12. #define _ASM_X86_KVM_X86_EMULATE_H
  13. #include <asm/desc_defs.h>
  14. #include "fpu.h"
  15. struct x86_emulate_ctxt;
  16. enum x86_intercept;
  17. enum x86_intercept_stage;
  18. struct x86_exception {
  19. u8 vector;
  20. bool error_code_valid;
  21. u16 error_code;
  22. bool nested_page_fault;
  23. u64 address; /* cr2 or nested page fault gpa */
  24. u8 async_page_fault;
  25. unsigned long exit_qualification;
  26. };
  27. /*
  28. * This struct is used to carry enough information from the instruction
  29. * decoder to main KVM so that a decision can be made whether the
  30. * instruction needs to be intercepted or not.
  31. */
  32. struct x86_instruction_info {
  33. u8 intercept; /* which intercept */
  34. u8 rep_prefix; /* rep prefix? */
  35. u8 modrm_mod; /* mod part of modrm */
  36. u8 modrm_reg; /* index of register used */
  37. u8 modrm_rm; /* rm part of modrm */
  38. u64 src_val; /* value of source operand */
  39. u64 dst_val; /* value of destination operand */
  40. u8 src_bytes; /* size of source operand */
  41. u8 dst_bytes; /* size of destination operand */
  42. u8 ad_bytes; /* size of src/dst address */
  43. u64 next_rip; /* rip following the instruction */
  44. };
  45. /*
  46. * x86_emulate_ops:
  47. *
  48. * These operations represent the instruction emulator's interface to memory.
  49. * There are two categories of operation: those that act on ordinary memory
  50. * regions (*_std), and those that act on memory regions known to require
  51. * special treatment or emulation (*_emulated).
  52. *
  53. * The emulator assumes that an instruction accesses only one 'emulated memory'
  54. * location, that this location is the given linear faulting address (cr2), and
  55. * that this is one of the instruction's data operands. Instruction fetches and
  56. * stack operations are assumed never to access emulated memory. The emulator
  57. * automatically deduces which operand of a string-move operation is accessing
  58. * emulated memory, and assumes that the other operand accesses normal memory.
  59. *
  60. * NOTES:
  61. * 1. The emulator isn't very smart about emulated vs. standard memory.
  62. * 'Emulated memory' access addresses should be checked for sanity.
  63. * 'Normal memory' accesses may fault, and the caller must arrange to
  64. * detect and handle reentrancy into the emulator via recursive faults.
  65. * Accesses may be unaligned and may cross page boundaries.
  66. * 2. If the access fails (cannot emulate, or a standard access faults) then
  67. * it is up to the memop to propagate the fault to the guest VM via
  68. * some out-of-band mechanism, unknown to the emulator. The memop signals
  69. * failure by returning X86EMUL_PROPAGATE_FAULT to the emulator, which will
  70. * then immediately bail.
  71. * 3. Valid access sizes are 1, 2, 4 and 8 bytes. On x86/32 systems only
  72. * cmpxchg8b_emulated need support 8-byte accesses.
  73. * 4. The emulator cannot handle 64-bit mode emulation on an x86/32 system.
  74. */
  75. /* Access completed successfully: continue emulation as normal. */
  76. #define X86EMUL_CONTINUE 0
  77. /* Access is unhandleable: bail from emulation and return error to caller. */
  78. #define X86EMUL_UNHANDLEABLE 1
  79. /* Terminate emulation but return success to the caller. */
  80. #define X86EMUL_PROPAGATE_FAULT 2 /* propagate a generated fault to guest */
  81. #define X86EMUL_RETRY_INSTR 3 /* retry the instruction for some reason */
  82. #define X86EMUL_CMPXCHG_FAILED 4 /* cmpxchg did not see expected value */
  83. #define X86EMUL_IO_NEEDED 5 /* IO is needed to complete emulation */
  84. #define X86EMUL_INTERCEPTED 6 /* Intercepted by nested VMCB/VMCS */
  85. /* x86-specific emulation flags */
  86. #define X86EMUL_F_WRITE BIT(0)
  87. #define X86EMUL_F_FETCH BIT(1)
  88. #define X86EMUL_F_IMPLICIT BIT(2)
  89. #define X86EMUL_F_INVLPG BIT(3)
  90. struct x86_emulate_ops {
  91. void (*vm_bugged)(struct x86_emulate_ctxt *ctxt);
  92. /*
  93. * read_gpr: read a general purpose register (rax - r15)
  94. *
  95. * @reg: gpr number.
  96. */
  97. ulong (*read_gpr)(struct x86_emulate_ctxt *ctxt, unsigned reg);
  98. /*
  99. * write_gpr: write a general purpose register (rax - r15)
  100. *
  101. * @reg: gpr number.
  102. * @val: value to write.
  103. */
  104. void (*write_gpr)(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val);
  105. /*
  106. * read_std: Read bytes of standard (non-emulated/special) memory.
  107. * Used for descriptor reading.
  108. * @addr: [IN ] Linear address from which to read.
  109. * @val: [OUT] Value read from memory, zero-extended to 'u_long'.
  110. * @bytes: [IN ] Number of bytes to read from memory.
  111. * @system:[IN ] Whether the access is forced to be at CPL0.
  112. */
  113. int (*read_std)(struct x86_emulate_ctxt *ctxt,
  114. unsigned long addr, void *val,
  115. unsigned int bytes,
  116. struct x86_exception *fault, bool system);
  117. /*
  118. * write_std: Write bytes of standard (non-emulated/special) memory.
  119. * Used for descriptor writing.
  120. * @addr: [IN ] Linear address to which to write.
  121. * @val: [OUT] Value write to memory, zero-extended to 'u_long'.
  122. * @bytes: [IN ] Number of bytes to write to memory.
  123. * @system:[IN ] Whether the access is forced to be at CPL0.
  124. */
  125. int (*write_std)(struct x86_emulate_ctxt *ctxt,
  126. unsigned long addr, void *val, unsigned int bytes,
  127. struct x86_exception *fault, bool system);
  128. /*
  129. * fetch: Read bytes of standard (non-emulated/special) memory.
  130. * Used for instruction fetch.
  131. * @addr: [IN ] Linear address from which to read.
  132. * @val: [OUT] Value read from memory, zero-extended to 'u_long'.
  133. * @bytes: [IN ] Number of bytes to read from memory.
  134. */
  135. int (*fetch)(struct x86_emulate_ctxt *ctxt,
  136. unsigned long addr, void *val, unsigned int bytes,
  137. struct x86_exception *fault);
  138. /*
  139. * read_emulated: Read bytes from emulated/special memory area.
  140. * @addr: [IN ] Linear address from which to read.
  141. * @val: [OUT] Value read from memory, zero-extended to 'u_long'.
  142. * @bytes: [IN ] Number of bytes to read from memory.
  143. */
  144. int (*read_emulated)(struct x86_emulate_ctxt *ctxt,
  145. unsigned long addr, void *val, unsigned int bytes,
  146. struct x86_exception *fault);
  147. /*
  148. * write_emulated: Write bytes to emulated/special memory area.
  149. * @addr: [IN ] Linear address to which to write.
  150. * @val: [IN ] Value to write to memory (low-order bytes used as
  151. * required).
  152. * @bytes: [IN ] Number of bytes to write to memory.
  153. */
  154. int (*write_emulated)(struct x86_emulate_ctxt *ctxt,
  155. unsigned long addr, const void *val,
  156. unsigned int bytes,
  157. struct x86_exception *fault);
  158. /*
  159. * cmpxchg_emulated: Emulate an atomic (LOCKed) CMPXCHG operation on an
  160. * emulated/special memory area.
  161. * @addr: [IN ] Linear address to access.
  162. * @old: [IN ] Value expected to be current at @addr.
  163. * @new: [IN ] Value to write to @addr.
  164. * @bytes: [IN ] Number of bytes to access using CMPXCHG.
  165. */
  166. int (*cmpxchg_emulated)(struct x86_emulate_ctxt *ctxt,
  167. unsigned long addr,
  168. const void *old,
  169. const void *new,
  170. unsigned int bytes,
  171. struct x86_exception *fault);
  172. void (*invlpg)(struct x86_emulate_ctxt *ctxt, ulong addr);
  173. int (*pio_in_emulated)(struct x86_emulate_ctxt *ctxt,
  174. int size, unsigned short port, void *val,
  175. unsigned int count);
  176. int (*pio_out_emulated)(struct x86_emulate_ctxt *ctxt,
  177. int size, unsigned short port, const void *val,
  178. unsigned int count);
  179. bool (*get_segment)(struct x86_emulate_ctxt *ctxt, u16 *selector,
  180. struct desc_struct *desc, u32 *base3, int seg);
  181. void (*set_segment)(struct x86_emulate_ctxt *ctxt, u16 selector,
  182. struct desc_struct *desc, u32 base3, int seg);
  183. unsigned long (*get_cached_segment_base)(struct x86_emulate_ctxt *ctxt,
  184. int seg);
  185. void (*get_gdt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
  186. void (*get_idt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
  187. void (*set_gdt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
  188. void (*set_idt)(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt);
  189. ulong (*get_cr)(struct x86_emulate_ctxt *ctxt, int cr);
  190. int (*set_cr)(struct x86_emulate_ctxt *ctxt, int cr, ulong val);
  191. int (*cpl)(struct x86_emulate_ctxt *ctxt);
  192. ulong (*get_dr)(struct x86_emulate_ctxt *ctxt, int dr);
  193. int (*set_dr)(struct x86_emulate_ctxt *ctxt, int dr, ulong value);
  194. int (*set_msr_with_filter)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 data);
  195. int (*get_msr_with_filter)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 *pdata);
  196. int (*get_msr)(struct x86_emulate_ctxt *ctxt, u32 msr_index, u64 *pdata);
  197. int (*check_rdpmc_early)(struct x86_emulate_ctxt *ctxt, u32 pmc);
  198. int (*read_pmc)(struct x86_emulate_ctxt *ctxt, u32 pmc, u64 *pdata);
  199. void (*halt)(struct x86_emulate_ctxt *ctxt);
  200. void (*wbinvd)(struct x86_emulate_ctxt *ctxt);
  201. int (*fix_hypercall)(struct x86_emulate_ctxt *ctxt);
  202. int (*intercept)(struct x86_emulate_ctxt *ctxt,
  203. struct x86_instruction_info *info,
  204. enum x86_intercept_stage stage);
  205. bool (*get_cpuid)(struct x86_emulate_ctxt *ctxt, u32 *eax, u32 *ebx,
  206. u32 *ecx, u32 *edx, bool exact_only);
  207. bool (*guest_has_movbe)(struct x86_emulate_ctxt *ctxt);
  208. bool (*guest_has_fxsr)(struct x86_emulate_ctxt *ctxt);
  209. bool (*guest_has_rdpid)(struct x86_emulate_ctxt *ctxt);
  210. bool (*guest_cpuid_is_intel_compatible)(struct x86_emulate_ctxt *ctxt);
  211. void (*set_nmi_mask)(struct x86_emulate_ctxt *ctxt, bool masked);
  212. bool (*is_smm)(struct x86_emulate_ctxt *ctxt);
  213. bool (*is_guest_mode)(struct x86_emulate_ctxt *ctxt);
  214. int (*leave_smm)(struct x86_emulate_ctxt *ctxt);
  215. void (*triple_fault)(struct x86_emulate_ctxt *ctxt);
  216. int (*set_xcr)(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr);
  217. gva_t (*get_untagged_addr)(struct x86_emulate_ctxt *ctxt, gva_t addr,
  218. unsigned int flags);
  219. };
  220. /* Type, address-of, and value of an instruction's operand. */
  221. struct operand {
  222. enum { OP_REG, OP_MEM, OP_MEM_STR, OP_IMM, OP_XMM, OP_MM, OP_NONE } type;
  223. unsigned int bytes;
  224. unsigned int count;
  225. union {
  226. unsigned long orig_val;
  227. u64 orig_val64;
  228. };
  229. union {
  230. unsigned long *reg;
  231. struct segmented_address {
  232. ulong ea;
  233. unsigned seg;
  234. } mem;
  235. unsigned xmm;
  236. unsigned mm;
  237. } addr;
  238. union {
  239. unsigned long val;
  240. u64 val64;
  241. char valptr[sizeof(sse128_t)];
  242. sse128_t vec_val;
  243. u64 mm_val;
  244. void *data;
  245. };
  246. };
  247. struct fetch_cache {
  248. u8 data[15];
  249. u8 *ptr;
  250. u8 *end;
  251. };
  252. struct read_cache {
  253. u8 data[1024];
  254. unsigned long pos;
  255. unsigned long end;
  256. };
  257. /* Execution mode, passed to the emulator. */
  258. enum x86emul_mode {
  259. X86EMUL_MODE_REAL, /* Real mode. */
  260. X86EMUL_MODE_VM86, /* Virtual 8086 mode. */
  261. X86EMUL_MODE_PROT16, /* 16-bit protected mode. */
  262. X86EMUL_MODE_PROT32, /* 32-bit protected mode. */
  263. X86EMUL_MODE_PROT64, /* 64-bit (long) mode. */
  264. };
  265. /*
  266. * fastop functions are declared as taking a never-defined fastop parameter,
  267. * so they can't be called from C directly.
  268. */
  269. struct fastop;
  270. typedef void (*fastop_t)(struct fastop *);
  271. /*
  272. * The emulator's _regs array tracks only the GPRs, i.e. excludes RIP. RIP is
  273. * tracked/accessed via _eip, and except for RIP relative addressing, which
  274. * also uses _eip, RIP cannot be a register operand nor can it be an operand in
  275. * a ModRM or SIB byte.
  276. */
  277. #ifdef CONFIG_X86_64
  278. #define NR_EMULATOR_GPRS 16
  279. #else
  280. #define NR_EMULATOR_GPRS 8
  281. #endif
  282. struct x86_emulate_ctxt {
  283. void *vcpu;
  284. const struct x86_emulate_ops *ops;
  285. /* Register state before/after emulation. */
  286. unsigned long eflags;
  287. unsigned long eip; /* eip before instruction emulation */
  288. /* Emulated execution mode, represented by an X86EMUL_MODE value. */
  289. enum x86emul_mode mode;
  290. /* interruptibility state, as a result of execution of STI or MOV SS */
  291. int interruptibility;
  292. bool perm_ok; /* do not check permissions if true */
  293. bool tf; /* TF value before instruction (after for syscall/sysret) */
  294. bool have_exception;
  295. struct x86_exception exception;
  296. /* GPA available */
  297. bool gpa_available;
  298. gpa_t gpa_val;
  299. /*
  300. * decode cache
  301. */
  302. /* current opcode length in bytes */
  303. u8 opcode_len;
  304. u8 b;
  305. u8 intercept;
  306. u8 op_bytes;
  307. u8 ad_bytes;
  308. union {
  309. int (*execute)(struct x86_emulate_ctxt *ctxt);
  310. fastop_t fop;
  311. };
  312. int (*check_perm)(struct x86_emulate_ctxt *ctxt);
  313. bool rip_relative;
  314. u8 rex_prefix;
  315. u8 lock_prefix;
  316. u8 rep_prefix;
  317. /* bitmaps of registers in _regs[] that can be read */
  318. u16 regs_valid;
  319. /* bitmaps of registers in _regs[] that have been written */
  320. u16 regs_dirty;
  321. /* modrm */
  322. u8 modrm;
  323. u8 modrm_mod;
  324. u8 modrm_reg;
  325. u8 modrm_rm;
  326. u8 modrm_seg;
  327. u8 seg_override;
  328. u64 d;
  329. unsigned long _eip;
  330. /* Here begins the usercopy section. */
  331. struct operand src;
  332. struct operand src2;
  333. struct operand dst;
  334. struct operand memop;
  335. unsigned long _regs[NR_EMULATOR_GPRS];
  336. struct operand *memopp;
  337. struct fetch_cache fetch;
  338. struct read_cache io_read;
  339. struct read_cache mem_read;
  340. bool is_branch;
  341. };
  342. #define KVM_EMULATOR_BUG_ON(cond, ctxt) \
  343. ({ \
  344. int __ret = (cond); \
  345. \
  346. if (WARN_ON_ONCE(__ret)) \
  347. ctxt->ops->vm_bugged(ctxt); \
  348. unlikely(__ret); \
  349. })
  350. /* Repeat String Operation Prefix */
  351. #define REPE_PREFIX 0xf3
  352. #define REPNE_PREFIX 0xf2
  353. /* CPUID vendors */
  354. #define X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx 0x68747541
  355. #define X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx 0x444d4163
  356. #define X86EMUL_CPUID_VENDOR_AuthenticAMD_edx 0x69746e65
  357. #define X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx 0x69444d41
  358. #define X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx 0x21726574
  359. #define X86EMUL_CPUID_VENDOR_AMDisbetterI_edx 0x74656273
  360. #define X86EMUL_CPUID_VENDOR_HygonGenuine_ebx 0x6f677948
  361. #define X86EMUL_CPUID_VENDOR_HygonGenuine_ecx 0x656e6975
  362. #define X86EMUL_CPUID_VENDOR_HygonGenuine_edx 0x6e65476e
  363. #define X86EMUL_CPUID_VENDOR_GenuineIntel_ebx 0x756e6547
  364. #define X86EMUL_CPUID_VENDOR_GenuineIntel_ecx 0x6c65746e
  365. #define X86EMUL_CPUID_VENDOR_GenuineIntel_edx 0x49656e69
  366. #define X86EMUL_CPUID_VENDOR_CentaurHauls_ebx 0x746e6543
  367. #define X86EMUL_CPUID_VENDOR_CentaurHauls_ecx 0x736c7561
  368. #define X86EMUL_CPUID_VENDOR_CentaurHauls_edx 0x48727561
  369. static inline bool is_guest_vendor_intel(u32 ebx, u32 ecx, u32 edx)
  370. {
  371. return ebx == X86EMUL_CPUID_VENDOR_GenuineIntel_ebx &&
  372. ecx == X86EMUL_CPUID_VENDOR_GenuineIntel_ecx &&
  373. edx == X86EMUL_CPUID_VENDOR_GenuineIntel_edx;
  374. }
  375. static inline bool is_guest_vendor_amd(u32 ebx, u32 ecx, u32 edx)
  376. {
  377. return (ebx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ebx &&
  378. ecx == X86EMUL_CPUID_VENDOR_AuthenticAMD_ecx &&
  379. edx == X86EMUL_CPUID_VENDOR_AuthenticAMD_edx) ||
  380. (ebx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ebx &&
  381. ecx == X86EMUL_CPUID_VENDOR_AMDisbetterI_ecx &&
  382. edx == X86EMUL_CPUID_VENDOR_AMDisbetterI_edx);
  383. }
  384. static inline bool is_guest_vendor_hygon(u32 ebx, u32 ecx, u32 edx)
  385. {
  386. return ebx == X86EMUL_CPUID_VENDOR_HygonGenuine_ebx &&
  387. ecx == X86EMUL_CPUID_VENDOR_HygonGenuine_ecx &&
  388. edx == X86EMUL_CPUID_VENDOR_HygonGenuine_edx;
  389. }
  390. enum x86_intercept_stage {
  391. X86_ICTP_NONE = 0, /* Allow zero-init to not match anything */
  392. X86_ICPT_PRE_EXCEPT,
  393. X86_ICPT_POST_EXCEPT,
  394. X86_ICPT_POST_MEMACCESS,
  395. };
  396. enum x86_intercept {
  397. x86_intercept_none,
  398. x86_intercept_cr_read,
  399. x86_intercept_cr_write,
  400. x86_intercept_clts,
  401. x86_intercept_lmsw,
  402. x86_intercept_smsw,
  403. x86_intercept_dr_read,
  404. x86_intercept_dr_write,
  405. x86_intercept_lidt,
  406. x86_intercept_sidt,
  407. x86_intercept_lgdt,
  408. x86_intercept_sgdt,
  409. x86_intercept_lldt,
  410. x86_intercept_sldt,
  411. x86_intercept_ltr,
  412. x86_intercept_str,
  413. x86_intercept_rdtsc,
  414. x86_intercept_rdpmc,
  415. x86_intercept_pushf,
  416. x86_intercept_popf,
  417. x86_intercept_cpuid,
  418. x86_intercept_rsm,
  419. x86_intercept_iret,
  420. x86_intercept_intn,
  421. x86_intercept_invd,
  422. x86_intercept_pause,
  423. x86_intercept_hlt,
  424. x86_intercept_invlpg,
  425. x86_intercept_invlpga,
  426. x86_intercept_vmrun,
  427. x86_intercept_vmload,
  428. x86_intercept_vmsave,
  429. x86_intercept_vmmcall,
  430. x86_intercept_stgi,
  431. x86_intercept_clgi,
  432. x86_intercept_skinit,
  433. x86_intercept_rdtscp,
  434. x86_intercept_rdpid,
  435. x86_intercept_icebp,
  436. x86_intercept_wbinvd,
  437. x86_intercept_monitor,
  438. x86_intercept_mwait,
  439. x86_intercept_rdmsr,
  440. x86_intercept_wrmsr,
  441. x86_intercept_in,
  442. x86_intercept_ins,
  443. x86_intercept_out,
  444. x86_intercept_outs,
  445. x86_intercept_xsetbv,
  446. nr_x86_intercepts
  447. };
  448. /* Host execution mode. */
  449. #if defined(CONFIG_X86_32)
  450. #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT32
  451. #elif defined(CONFIG_X86_64)
  452. #define X86EMUL_MODE_HOST X86EMUL_MODE_PROT64
  453. #endif
  454. int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int emulation_type);
  455. bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt);
  456. #define EMULATION_FAILED -1
  457. #define EMULATION_OK 0
  458. #define EMULATION_RESTART 1
  459. #define EMULATION_INTERCEPTED 2
  460. void init_decode_cache(struct x86_emulate_ctxt *ctxt);
  461. int x86_emulate_insn(struct x86_emulate_ctxt *ctxt);
  462. int emulator_task_switch(struct x86_emulate_ctxt *ctxt,
  463. u16 tss_selector, int idt_index, int reason,
  464. bool has_error_code, u32 error_code);
  465. int emulate_int_real(struct x86_emulate_ctxt *ctxt, int irq);
  466. void emulator_invalidate_register_cache(struct x86_emulate_ctxt *ctxt);
  467. void emulator_writeback_register_cache(struct x86_emulate_ctxt *ctxt);
  468. bool emulator_can_use_gpa(struct x86_emulate_ctxt *ctxt);
  469. static inline ulong reg_read(struct x86_emulate_ctxt *ctxt, unsigned nr)
  470. {
  471. if (KVM_EMULATOR_BUG_ON(nr >= NR_EMULATOR_GPRS, ctxt))
  472. nr &= NR_EMULATOR_GPRS - 1;
  473. if (!(ctxt->regs_valid & (1 << nr))) {
  474. ctxt->regs_valid |= 1 << nr;
  475. ctxt->_regs[nr] = ctxt->ops->read_gpr(ctxt, nr);
  476. }
  477. return ctxt->_regs[nr];
  478. }
  479. static inline ulong *reg_write(struct x86_emulate_ctxt *ctxt, unsigned nr)
  480. {
  481. if (KVM_EMULATOR_BUG_ON(nr >= NR_EMULATOR_GPRS, ctxt))
  482. nr &= NR_EMULATOR_GPRS - 1;
  483. BUILD_BUG_ON(sizeof(ctxt->regs_dirty) * BITS_PER_BYTE < NR_EMULATOR_GPRS);
  484. BUILD_BUG_ON(sizeof(ctxt->regs_valid) * BITS_PER_BYTE < NR_EMULATOR_GPRS);
  485. ctxt->regs_valid |= 1 << nr;
  486. ctxt->regs_dirty |= 1 << nr;
  487. return &ctxt->_regs[nr];
  488. }
  489. static inline ulong *reg_rmw(struct x86_emulate_ctxt *ctxt, unsigned nr)
  490. {
  491. reg_read(ctxt, nr);
  492. return reg_write(ctxt, nr);
  493. }
  494. #endif /* _ASM_X86_KVM_X86_EMULATE_H */