kvm_host.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. #/*
  2. * Kernel-based Virtual Machine driver for Linux
  3. *
  4. * This header defines architecture specific interfaces, x86 version
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2. See
  7. * the COPYING file in the top-level directory.
  8. *
  9. */
  10. #ifndef ASM_KVM_HOST_H
  11. #define ASM_KVM_HOST_H
  12. #include <linux/types.h>
  13. #include <linux/mm.h>
  14. #include <linux/kvm.h>
  15. #include <linux/kvm_para.h>
  16. #include <linux/kvm_types.h>
  17. #include <asm/desc.h>
  18. #define CR3_PAE_RESERVED_BITS ((X86_CR3_PWT | X86_CR3_PCD) - 1)
  19. #define CR3_NONPAE_RESERVED_BITS ((PAGE_SIZE-1) & ~(X86_CR3_PWT | X86_CR3_PCD))
  20. #define CR3_L_MODE_RESERVED_BITS (CR3_NONPAE_RESERVED_BITS | \
  21. 0xFFFFFF0000000000ULL)
  22. #define KVM_GUEST_CR0_MASK \
  23. (X86_CR0_PG | X86_CR0_PE | X86_CR0_WP | X86_CR0_NE \
  24. | X86_CR0_NW | X86_CR0_CD)
  25. #define KVM_VM_CR0_ALWAYS_ON \
  26. (X86_CR0_PG | X86_CR0_PE | X86_CR0_WP | X86_CR0_NE | X86_CR0_TS \
  27. | X86_CR0_MP)
  28. #define KVM_GUEST_CR4_MASK \
  29. (X86_CR4_VME | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_PGE | X86_CR4_VMXE)
  30. #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
  31. #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
  32. #define INVALID_PAGE (~(hpa_t)0)
  33. #define UNMAPPED_GVA (~(gpa_t)0)
  34. #define DE_VECTOR 0
  35. #define UD_VECTOR 6
  36. #define NM_VECTOR 7
  37. #define DF_VECTOR 8
  38. #define TS_VECTOR 10
  39. #define NP_VECTOR 11
  40. #define SS_VECTOR 12
  41. #define GP_VECTOR 13
  42. #define PF_VECTOR 14
  43. #define SELECTOR_TI_MASK (1 << 2)
  44. #define SELECTOR_RPL_MASK 0x03
  45. #define IOPL_SHIFT 12
  46. #define KVM_ALIAS_SLOTS 4
  47. #define KVM_PERMILLE_MMU_PAGES 20
  48. #define KVM_MIN_ALLOC_MMU_PAGES 64
  49. #define KVM_NUM_MMU_PAGES 1024
  50. #define KVM_MIN_FREE_MMU_PAGES 5
  51. #define KVM_REFILL_PAGES 25
  52. #define KVM_MAX_CPUID_ENTRIES 40
  53. extern spinlock_t kvm_lock;
  54. extern struct list_head vm_list;
  55. struct kvm_vcpu;
  56. struct kvm;
  57. enum {
  58. VCPU_REGS_RAX = 0,
  59. VCPU_REGS_RCX = 1,
  60. VCPU_REGS_RDX = 2,
  61. VCPU_REGS_RBX = 3,
  62. VCPU_REGS_RSP = 4,
  63. VCPU_REGS_RBP = 5,
  64. VCPU_REGS_RSI = 6,
  65. VCPU_REGS_RDI = 7,
  66. #ifdef CONFIG_X86_64
  67. VCPU_REGS_R8 = 8,
  68. VCPU_REGS_R9 = 9,
  69. VCPU_REGS_R10 = 10,
  70. VCPU_REGS_R11 = 11,
  71. VCPU_REGS_R12 = 12,
  72. VCPU_REGS_R13 = 13,
  73. VCPU_REGS_R14 = 14,
  74. VCPU_REGS_R15 = 15,
  75. #endif
  76. NR_VCPU_REGS
  77. };
  78. enum {
  79. VCPU_SREG_CS,
  80. VCPU_SREG_DS,
  81. VCPU_SREG_ES,
  82. VCPU_SREG_FS,
  83. VCPU_SREG_GS,
  84. VCPU_SREG_SS,
  85. VCPU_SREG_TR,
  86. VCPU_SREG_LDTR,
  87. };
  88. #include <asm/kvm_x86_emulate.h>
  89. #define KVM_NR_MEM_OBJS 40
  90. /*
  91. * We don't want allocation failures within the mmu code, so we preallocate
  92. * enough memory for a single page fault in a cache.
  93. */
  94. struct kvm_mmu_memory_cache {
  95. int nobjs;
  96. void *objects[KVM_NR_MEM_OBJS];
  97. };
  98. #define NR_PTE_CHAIN_ENTRIES 5
  99. struct kvm_pte_chain {
  100. u64 *parent_ptes[NR_PTE_CHAIN_ENTRIES];
  101. struct hlist_node link;
  102. };
  103. /*
  104. * kvm_mmu_page_role, below, is defined as:
  105. *
  106. * bits 0:3 - total guest paging levels (2-4, or zero for real mode)
  107. * bits 4:7 - page table level for this shadow (1-4)
  108. * bits 8:9 - page table quadrant for 2-level guests
  109. * bit 16 - "metaphysical" - gfn is not a real page (huge page/real mode)
  110. * bits 17:19 - common access permissions for all ptes in this shadow page
  111. */
  112. union kvm_mmu_page_role {
  113. unsigned word;
  114. struct {
  115. unsigned glevels:4;
  116. unsigned level:4;
  117. unsigned quadrant:2;
  118. unsigned pad_for_nice_hex_output:6;
  119. unsigned metaphysical:1;
  120. unsigned access:3;
  121. };
  122. };
  123. struct kvm_mmu_page {
  124. struct list_head link;
  125. struct hlist_node hash_link;
  126. /*
  127. * The following two entries are used to key the shadow page in the
  128. * hash table.
  129. */
  130. gfn_t gfn;
  131. union kvm_mmu_page_role role;
  132. u64 *spt;
  133. /* hold the gfn of each spte inside spt */
  134. gfn_t *gfns;
  135. unsigned long slot_bitmap; /* One bit set per slot which has memory
  136. * in this shadow page.
  137. */
  138. int multimapped; /* More than one parent_pte? */
  139. int root_count; /* Currently serving as active root */
  140. union {
  141. u64 *parent_pte; /* !multimapped */
  142. struct hlist_head parent_ptes; /* multimapped, kvm_pte_chain */
  143. };
  144. };
  145. /*
  146. * x86 supports 3 paging modes (4-level 64-bit, 3-level 64-bit, and 2-level
  147. * 32-bit). The kvm_mmu structure abstracts the details of the current mmu
  148. * mode.
  149. */
  150. struct kvm_mmu {
  151. void (*new_cr3)(struct kvm_vcpu *vcpu);
  152. int (*page_fault)(struct kvm_vcpu *vcpu, gva_t gva, u32 err);
  153. void (*free)(struct kvm_vcpu *vcpu);
  154. gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva);
  155. void (*prefetch_page)(struct kvm_vcpu *vcpu,
  156. struct kvm_mmu_page *page);
  157. hpa_t root_hpa;
  158. int root_level;
  159. int shadow_root_level;
  160. u64 *pae_root;
  161. };
  162. struct kvm_vcpu_arch {
  163. u64 host_tsc;
  164. int interrupt_window_open;
  165. unsigned long irq_summary; /* bit vector: 1 per word in irq_pending */
  166. DECLARE_BITMAP(irq_pending, KVM_NR_INTERRUPTS);
  167. unsigned long regs[NR_VCPU_REGS]; /* for rsp: vcpu_load_rsp_rip() */
  168. unsigned long rip; /* needs vcpu_load_rsp_rip() */
  169. unsigned long cr0;
  170. unsigned long cr2;
  171. unsigned long cr3;
  172. unsigned long cr4;
  173. unsigned long cr8;
  174. u64 pdptrs[4]; /* pae */
  175. u64 shadow_efer;
  176. u64 apic_base;
  177. struct kvm_lapic *apic; /* kernel irqchip context */
  178. #define VCPU_MP_STATE_RUNNABLE 0
  179. #define VCPU_MP_STATE_UNINITIALIZED 1
  180. #define VCPU_MP_STATE_INIT_RECEIVED 2
  181. #define VCPU_MP_STATE_SIPI_RECEIVED 3
  182. #define VCPU_MP_STATE_HALTED 4
  183. int mp_state;
  184. int sipi_vector;
  185. u64 ia32_misc_enable_msr;
  186. bool tpr_access_reporting;
  187. struct kvm_mmu mmu;
  188. struct kvm_mmu_memory_cache mmu_pte_chain_cache;
  189. struct kvm_mmu_memory_cache mmu_rmap_desc_cache;
  190. struct kvm_mmu_memory_cache mmu_page_cache;
  191. struct kvm_mmu_memory_cache mmu_page_header_cache;
  192. gfn_t last_pt_write_gfn;
  193. int last_pt_write_count;
  194. u64 *last_pte_updated;
  195. struct {
  196. gfn_t gfn; /* presumed gfn during guest pte update */
  197. struct page *page; /* page corresponding to that gfn */
  198. } update_pte;
  199. struct i387_fxsave_struct host_fx_image;
  200. struct i387_fxsave_struct guest_fx_image;
  201. gva_t mmio_fault_cr2;
  202. struct kvm_pio_request pio;
  203. void *pio_data;
  204. struct kvm_queued_exception {
  205. bool pending;
  206. bool has_error_code;
  207. u8 nr;
  208. u32 error_code;
  209. } exception;
  210. struct {
  211. int active;
  212. u8 save_iopl;
  213. struct kvm_save_segment {
  214. u16 selector;
  215. unsigned long base;
  216. u32 limit;
  217. u32 ar;
  218. } tr, es, ds, fs, gs;
  219. } rmode;
  220. int halt_request; /* real mode on Intel only */
  221. int cpuid_nent;
  222. struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES];
  223. /* emulate context */
  224. struct x86_emulate_ctxt emulate_ctxt;
  225. };
  226. struct kvm_mem_alias {
  227. gfn_t base_gfn;
  228. unsigned long npages;
  229. gfn_t target_gfn;
  230. };
  231. struct kvm_arch{
  232. int naliases;
  233. struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
  234. unsigned int n_free_mmu_pages;
  235. unsigned int n_requested_mmu_pages;
  236. unsigned int n_alloc_mmu_pages;
  237. struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
  238. /*
  239. * Hash table of struct kvm_mmu_page.
  240. */
  241. struct list_head active_mmu_pages;
  242. struct kvm_pic *vpic;
  243. struct kvm_ioapic *vioapic;
  244. int round_robin_prev_vcpu;
  245. unsigned int tss_addr;
  246. struct page *apic_access_page;
  247. };
  248. struct kvm_vm_stat {
  249. u32 mmu_shadow_zapped;
  250. u32 mmu_pte_write;
  251. u32 mmu_pte_updated;
  252. u32 mmu_pde_zapped;
  253. u32 mmu_flooded;
  254. u32 mmu_recycled;
  255. u32 mmu_cache_miss;
  256. u32 remote_tlb_flush;
  257. };
  258. struct kvm_vcpu_stat {
  259. u32 pf_fixed;
  260. u32 pf_guest;
  261. u32 tlb_flush;
  262. u32 invlpg;
  263. u32 exits;
  264. u32 io_exits;
  265. u32 mmio_exits;
  266. u32 signal_exits;
  267. u32 irq_window_exits;
  268. u32 halt_exits;
  269. u32 halt_wakeup;
  270. u32 request_irq_exits;
  271. u32 irq_exits;
  272. u32 host_state_reload;
  273. u32 efer_reload;
  274. u32 fpu_reload;
  275. u32 insn_emulation;
  276. u32 insn_emulation_fail;
  277. };
  278. struct descriptor_table {
  279. u16 limit;
  280. unsigned long base;
  281. } __attribute__((packed));
  282. struct kvm_x86_ops {
  283. int (*cpu_has_kvm_support)(void); /* __init */
  284. int (*disabled_by_bios)(void); /* __init */
  285. void (*hardware_enable)(void *dummy); /* __init */
  286. void (*hardware_disable)(void *dummy);
  287. void (*check_processor_compatibility)(void *rtn);
  288. int (*hardware_setup)(void); /* __init */
  289. void (*hardware_unsetup)(void); /* __exit */
  290. bool (*cpu_has_accelerated_tpr)(void);
  291. /* Create, but do not attach this VCPU */
  292. struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned id);
  293. void (*vcpu_free)(struct kvm_vcpu *vcpu);
  294. int (*vcpu_reset)(struct kvm_vcpu *vcpu);
  295. void (*prepare_guest_switch)(struct kvm_vcpu *vcpu);
  296. void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
  297. void (*vcpu_put)(struct kvm_vcpu *vcpu);
  298. void (*vcpu_decache)(struct kvm_vcpu *vcpu);
  299. int (*set_guest_debug)(struct kvm_vcpu *vcpu,
  300. struct kvm_debug_guest *dbg);
  301. void (*guest_debug_pre)(struct kvm_vcpu *vcpu);
  302. int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
  303. int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
  304. u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
  305. void (*get_segment)(struct kvm_vcpu *vcpu,
  306. struct kvm_segment *var, int seg);
  307. void (*set_segment)(struct kvm_vcpu *vcpu,
  308. struct kvm_segment *var, int seg);
  309. void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
  310. void (*decache_cr4_guest_bits)(struct kvm_vcpu *vcpu);
  311. void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
  312. void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
  313. void (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
  314. void (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
  315. void (*get_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  316. void (*set_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  317. void (*get_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  318. void (*set_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  319. unsigned long (*get_dr)(struct kvm_vcpu *vcpu, int dr);
  320. void (*set_dr)(struct kvm_vcpu *vcpu, int dr, unsigned long value,
  321. int *exception);
  322. void (*cache_regs)(struct kvm_vcpu *vcpu);
  323. void (*decache_regs)(struct kvm_vcpu *vcpu);
  324. unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
  325. void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
  326. void (*tlb_flush)(struct kvm_vcpu *vcpu);
  327. void (*run)(struct kvm_vcpu *vcpu, struct kvm_run *run);
  328. int (*handle_exit)(struct kvm_run *run, struct kvm_vcpu *vcpu);
  329. void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
  330. void (*patch_hypercall)(struct kvm_vcpu *vcpu,
  331. unsigned char *hypercall_addr);
  332. int (*get_irq)(struct kvm_vcpu *vcpu);
  333. void (*set_irq)(struct kvm_vcpu *vcpu, int vec);
  334. void (*queue_exception)(struct kvm_vcpu *vcpu, unsigned nr,
  335. bool has_error_code, u32 error_code);
  336. bool (*exception_injected)(struct kvm_vcpu *vcpu);
  337. void (*inject_pending_irq)(struct kvm_vcpu *vcpu);
  338. void (*inject_pending_vectors)(struct kvm_vcpu *vcpu,
  339. struct kvm_run *run);
  340. int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
  341. };
  342. extern struct kvm_x86_ops *kvm_x86_ops;
  343. int kvm_mmu_module_init(void);
  344. void kvm_mmu_module_exit(void);
  345. void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
  346. int kvm_mmu_create(struct kvm_vcpu *vcpu);
  347. int kvm_mmu_setup(struct kvm_vcpu *vcpu);
  348. void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte);
  349. int kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
  350. void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot);
  351. void kvm_mmu_zap_all(struct kvm *kvm);
  352. unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm);
  353. void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages);
  354. enum emulation_result {
  355. EMULATE_DONE, /* no further processing */
  356. EMULATE_DO_MMIO, /* kvm_run filled with mmio request */
  357. EMULATE_FAIL, /* can't emulate this instruction */
  358. };
  359. #define EMULTYPE_NO_DECODE (1 << 0)
  360. #define EMULTYPE_TRAP_UD (1 << 1)
  361. int emulate_instruction(struct kvm_vcpu *vcpu, struct kvm_run *run,
  362. unsigned long cr2, u16 error_code, int emulation_type);
  363. void kvm_report_emulation_failure(struct kvm_vcpu *cvpu, const char *context);
  364. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
  365. void realmode_lidt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
  366. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  367. unsigned long *rflags);
  368. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr);
  369. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long value,
  370. unsigned long *rflags);
  371. int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *data);
  372. int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
  373. struct x86_emulate_ctxt;
  374. int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  375. int size, unsigned port);
  376. int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
  377. int size, unsigned long count, int down,
  378. gva_t address, int rep, unsigned port);
  379. void kvm_emulate_cpuid(struct kvm_vcpu *vcpu);
  380. int kvm_emulate_halt(struct kvm_vcpu *vcpu);
  381. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address);
  382. int emulate_clts(struct kvm_vcpu *vcpu);
  383. int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
  384. unsigned long *dest);
  385. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
  386. unsigned long value);
  387. void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
  388. void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr0);
  389. void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr0);
  390. void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr0);
  391. unsigned long get_cr8(struct kvm_vcpu *vcpu);
  392. void lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
  393. void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l);
  394. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
  395. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data);
  396. void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr);
  397. void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);
  398. void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long cr2,
  399. u32 error_code);
  400. void fx_init(struct kvm_vcpu *vcpu);
  401. int emulator_read_std(unsigned long addr,
  402. void *val,
  403. unsigned int bytes,
  404. struct kvm_vcpu *vcpu);
  405. int emulator_write_emulated(unsigned long addr,
  406. const void *val,
  407. unsigned int bytes,
  408. struct kvm_vcpu *vcpu);
  409. unsigned long segment_base(u16 selector);
  410. void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu);
  411. void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
  412. const u8 *new, int bytes);
  413. int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva);
  414. void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);
  415. int kvm_mmu_load(struct kvm_vcpu *vcpu);
  416. void kvm_mmu_unload(struct kvm_vcpu *vcpu);
  417. int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
  418. int kvm_fix_hypercall(struct kvm_vcpu *vcpu);
  419. int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva, u32 error_code);
  420. int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3);
  421. int complete_pio(struct kvm_vcpu *vcpu);
  422. static inline struct kvm_mmu_page *page_header(hpa_t shadow_page)
  423. {
  424. struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
  425. return (struct kvm_mmu_page *)page_private(page);
  426. }
  427. static inline u16 read_fs(void)
  428. {
  429. u16 seg;
  430. asm("mov %%fs, %0" : "=g"(seg));
  431. return seg;
  432. }
  433. static inline u16 read_gs(void)
  434. {
  435. u16 seg;
  436. asm("mov %%gs, %0" : "=g"(seg));
  437. return seg;
  438. }
  439. static inline u16 read_ldt(void)
  440. {
  441. u16 ldt;
  442. asm("sldt %0" : "=g"(ldt));
  443. return ldt;
  444. }
  445. static inline void load_fs(u16 sel)
  446. {
  447. asm("mov %0, %%fs" : : "rm"(sel));
  448. }
  449. static inline void load_gs(u16 sel)
  450. {
  451. asm("mov %0, %%gs" : : "rm"(sel));
  452. }
  453. #ifndef load_ldt
  454. static inline void load_ldt(u16 sel)
  455. {
  456. asm("lldt %0" : : "rm"(sel));
  457. }
  458. #endif
  459. static inline void get_idt(struct descriptor_table *table)
  460. {
  461. asm("sidt %0" : "=m"(*table));
  462. }
  463. static inline void get_gdt(struct descriptor_table *table)
  464. {
  465. asm("sgdt %0" : "=m"(*table));
  466. }
  467. static inline unsigned long read_tr_base(void)
  468. {
  469. u16 tr;
  470. asm("str %0" : "=g"(tr));
  471. return segment_base(tr);
  472. }
  473. #ifdef CONFIG_X86_64
  474. static inline unsigned long read_msr(unsigned long msr)
  475. {
  476. u64 value;
  477. rdmsrl(msr, value);
  478. return value;
  479. }
  480. #endif
  481. static inline void fx_save(struct i387_fxsave_struct *image)
  482. {
  483. asm("fxsave (%0)":: "r" (image));
  484. }
  485. static inline void fx_restore(struct i387_fxsave_struct *image)
  486. {
  487. asm("fxrstor (%0)":: "r" (image));
  488. }
  489. static inline void fpu_init(void)
  490. {
  491. asm("finit");
  492. }
  493. static inline u32 get_rdx_init_val(void)
  494. {
  495. return 0x600; /* P6 family */
  496. }
  497. static inline void kvm_inject_gp(struct kvm_vcpu *vcpu, u32 error_code)
  498. {
  499. kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
  500. }
  501. #define ASM_VMX_VMCLEAR_RAX ".byte 0x66, 0x0f, 0xc7, 0x30"
  502. #define ASM_VMX_VMLAUNCH ".byte 0x0f, 0x01, 0xc2"
  503. #define ASM_VMX_VMRESUME ".byte 0x0f, 0x01, 0xc3"
  504. #define ASM_VMX_VMPTRLD_RAX ".byte 0x0f, 0xc7, 0x30"
  505. #define ASM_VMX_VMREAD_RDX_RAX ".byte 0x0f, 0x78, 0xd0"
  506. #define ASM_VMX_VMWRITE_RAX_RDX ".byte 0x0f, 0x79, 0xd0"
  507. #define ASM_VMX_VMWRITE_RSP_RDX ".byte 0x0f, 0x79, 0xd4"
  508. #define ASM_VMX_VMXOFF ".byte 0x0f, 0x01, 0xc4"
  509. #define ASM_VMX_VMXON_RAX ".byte 0xf3, 0x0f, 0xc7, 0x30"
  510. #define MSR_IA32_TIME_STAMP_COUNTER 0x010
  511. #define TSS_IOPB_BASE_OFFSET 0x66
  512. #define TSS_BASE_SIZE 0x68
  513. #define TSS_IOPB_SIZE (65536 / 8)
  514. #define TSS_REDIRECTION_SIZE (256 / 8)
  515. #define RMODE_TSS_SIZE \
  516. (TSS_BASE_SIZE + TSS_REDIRECTION_SIZE + TSS_IOPB_SIZE + 1)
  517. #endif