kvm.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. #ifndef __KVM_H
  2. #define __KVM_H
  3. /*
  4. * This work is licensed under the terms of the GNU GPL, version 2. See
  5. * the COPYING file in the top-level directory.
  6. */
  7. #include <linux/types.h>
  8. #include <linux/list.h>
  9. #include <linux/mutex.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/mm.h>
  12. #include "vmx.h"
  13. #include <linux/kvm.h>
  14. #include <linux/kvm_para.h>
  15. #define CR0_PE_MASK (1ULL << 0)
  16. #define CR0_TS_MASK (1ULL << 3)
  17. #define CR0_NE_MASK (1ULL << 5)
  18. #define CR0_WP_MASK (1ULL << 16)
  19. #define CR0_NW_MASK (1ULL << 29)
  20. #define CR0_CD_MASK (1ULL << 30)
  21. #define CR0_PG_MASK (1ULL << 31)
  22. #define CR3_WPT_MASK (1ULL << 3)
  23. #define CR3_PCD_MASK (1ULL << 4)
  24. #define CR3_RESEVED_BITS 0x07ULL
  25. #define CR3_L_MODE_RESEVED_BITS (~((1ULL << 40) - 1) | 0x0fe7ULL)
  26. #define CR3_FLAGS_MASK ((1ULL << 5) - 1)
  27. #define CR4_VME_MASK (1ULL << 0)
  28. #define CR4_PSE_MASK (1ULL << 4)
  29. #define CR4_PAE_MASK (1ULL << 5)
  30. #define CR4_PGE_MASK (1ULL << 7)
  31. #define CR4_VMXE_MASK (1ULL << 13)
  32. #define KVM_GUEST_CR0_MASK \
  33. (CR0_PG_MASK | CR0_PE_MASK | CR0_WP_MASK | CR0_NE_MASK \
  34. | CR0_NW_MASK | CR0_CD_MASK)
  35. #define KVM_VM_CR0_ALWAYS_ON \
  36. (CR0_PG_MASK | CR0_PE_MASK | CR0_WP_MASK | CR0_NE_MASK)
  37. #define KVM_GUEST_CR4_MASK \
  38. (CR4_PSE_MASK | CR4_PAE_MASK | CR4_PGE_MASK | CR4_VMXE_MASK | CR4_VME_MASK)
  39. #define KVM_PMODE_VM_CR4_ALWAYS_ON (CR4_VMXE_MASK | CR4_PAE_MASK)
  40. #define KVM_RMODE_VM_CR4_ALWAYS_ON (CR4_VMXE_MASK | CR4_PAE_MASK | CR4_VME_MASK)
  41. #define INVALID_PAGE (~(hpa_t)0)
  42. #define UNMAPPED_GVA (~(gpa_t)0)
  43. #define KVM_MAX_VCPUS 1
  44. #define KVM_MEMORY_SLOTS 4
  45. #define KVM_NUM_MMU_PAGES 256
  46. #define KVM_MIN_FREE_MMU_PAGES 5
  47. #define KVM_REFILL_PAGES 25
  48. #define FX_IMAGE_SIZE 512
  49. #define FX_IMAGE_ALIGN 16
  50. #define FX_BUF_SIZE (2 * FX_IMAGE_SIZE + FX_IMAGE_ALIGN)
  51. #define DE_VECTOR 0
  52. #define DF_VECTOR 8
  53. #define TS_VECTOR 10
  54. #define NP_VECTOR 11
  55. #define SS_VECTOR 12
  56. #define GP_VECTOR 13
  57. #define PF_VECTOR 14
  58. #define SELECTOR_TI_MASK (1 << 2)
  59. #define SELECTOR_RPL_MASK 0x03
  60. #define IOPL_SHIFT 12
  61. /*
  62. * Address types:
  63. *
  64. * gva - guest virtual address
  65. * gpa - guest physical address
  66. * gfn - guest frame number
  67. * hva - host virtual address
  68. * hpa - host physical address
  69. * hfn - host frame number
  70. */
  71. typedef unsigned long gva_t;
  72. typedef u64 gpa_t;
  73. typedef unsigned long gfn_t;
  74. typedef unsigned long hva_t;
  75. typedef u64 hpa_t;
  76. typedef unsigned long hfn_t;
  77. #define NR_PTE_CHAIN_ENTRIES 5
  78. struct kvm_pte_chain {
  79. u64 *parent_ptes[NR_PTE_CHAIN_ENTRIES];
  80. struct hlist_node link;
  81. };
  82. /*
  83. * kvm_mmu_page_role, below, is defined as:
  84. *
  85. * bits 0:3 - total guest paging levels (2-4, or zero for real mode)
  86. * bits 4:7 - page table level for this shadow (1-4)
  87. * bits 8:9 - page table quadrant for 2-level guests
  88. * bit 16 - "metaphysical" - gfn is not a real page (huge page/real mode)
  89. */
  90. union kvm_mmu_page_role {
  91. unsigned word;
  92. struct {
  93. unsigned glevels : 4;
  94. unsigned level : 4;
  95. unsigned quadrant : 2;
  96. unsigned pad_for_nice_hex_output : 6;
  97. unsigned metaphysical : 1;
  98. };
  99. };
  100. struct kvm_mmu_page {
  101. struct list_head link;
  102. struct hlist_node hash_link;
  103. /*
  104. * The following two entries are used to key the shadow page in the
  105. * hash table.
  106. */
  107. gfn_t gfn;
  108. union kvm_mmu_page_role role;
  109. hpa_t page_hpa;
  110. unsigned long slot_bitmap; /* One bit set per slot which has memory
  111. * in this shadow page.
  112. */
  113. int global; /* Set if all ptes in this page are global */
  114. int multimapped; /* More than one parent_pte? */
  115. int root_count; /* Currently serving as active root */
  116. union {
  117. u64 *parent_pte; /* !multimapped */
  118. struct hlist_head parent_ptes; /* multimapped, kvm_pte_chain */
  119. };
  120. };
  121. struct vmcs {
  122. u32 revision_id;
  123. u32 abort;
  124. char data[0];
  125. };
  126. #define vmx_msr_entry kvm_msr_entry
  127. struct kvm_vcpu;
  128. /*
  129. * x86 supports 3 paging modes (4-level 64-bit, 3-level 64-bit, and 2-level
  130. * 32-bit). The kvm_mmu structure abstracts the details of the current mmu
  131. * mode.
  132. */
  133. struct kvm_mmu {
  134. void (*new_cr3)(struct kvm_vcpu *vcpu);
  135. int (*page_fault)(struct kvm_vcpu *vcpu, gva_t gva, u32 err);
  136. void (*free)(struct kvm_vcpu *vcpu);
  137. gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva);
  138. hpa_t root_hpa;
  139. int root_level;
  140. int shadow_root_level;
  141. u64 *pae_root;
  142. };
  143. #define KVM_NR_MEM_OBJS 20
  144. struct kvm_mmu_memory_cache {
  145. int nobjs;
  146. void *objects[KVM_NR_MEM_OBJS];
  147. };
  148. /*
  149. * We don't want allocation failures within the mmu code, so we preallocate
  150. * enough memory for a single page fault in a cache.
  151. */
  152. struct kvm_guest_debug {
  153. int enabled;
  154. unsigned long bp[4];
  155. int singlestep;
  156. };
  157. enum {
  158. VCPU_REGS_RAX = 0,
  159. VCPU_REGS_RCX = 1,
  160. VCPU_REGS_RDX = 2,
  161. VCPU_REGS_RBX = 3,
  162. VCPU_REGS_RSP = 4,
  163. VCPU_REGS_RBP = 5,
  164. VCPU_REGS_RSI = 6,
  165. VCPU_REGS_RDI = 7,
  166. #ifdef CONFIG_X86_64
  167. VCPU_REGS_R8 = 8,
  168. VCPU_REGS_R9 = 9,
  169. VCPU_REGS_R10 = 10,
  170. VCPU_REGS_R11 = 11,
  171. VCPU_REGS_R12 = 12,
  172. VCPU_REGS_R13 = 13,
  173. VCPU_REGS_R14 = 14,
  174. VCPU_REGS_R15 = 15,
  175. #endif
  176. NR_VCPU_REGS
  177. };
  178. enum {
  179. VCPU_SREG_CS,
  180. VCPU_SREG_DS,
  181. VCPU_SREG_ES,
  182. VCPU_SREG_FS,
  183. VCPU_SREG_GS,
  184. VCPU_SREG_SS,
  185. VCPU_SREG_TR,
  186. VCPU_SREG_LDTR,
  187. };
  188. struct kvm_vcpu {
  189. struct kvm *kvm;
  190. union {
  191. struct vmcs *vmcs;
  192. struct vcpu_svm *svm;
  193. };
  194. struct mutex mutex;
  195. int cpu;
  196. int launched;
  197. int interrupt_window_open;
  198. unsigned long irq_summary; /* bit vector: 1 per word in irq_pending */
  199. #define NR_IRQ_WORDS KVM_IRQ_BITMAP_SIZE(unsigned long)
  200. unsigned long irq_pending[NR_IRQ_WORDS];
  201. unsigned long regs[NR_VCPU_REGS]; /* for rsp: vcpu_load_rsp_rip() */
  202. unsigned long rip; /* needs vcpu_load_rsp_rip() */
  203. unsigned long cr0;
  204. unsigned long cr2;
  205. unsigned long cr3;
  206. gpa_t para_state_gpa;
  207. struct page *para_state_page;
  208. gpa_t hypercall_gpa;
  209. unsigned long cr4;
  210. unsigned long cr8;
  211. u64 pdptrs[4]; /* pae */
  212. u64 shadow_efer;
  213. u64 apic_base;
  214. u64 ia32_misc_enable_msr;
  215. int nmsrs;
  216. struct vmx_msr_entry *guest_msrs;
  217. struct vmx_msr_entry *host_msrs;
  218. struct list_head free_pages;
  219. struct kvm_mmu_page page_header_buf[KVM_NUM_MMU_PAGES];
  220. struct kvm_mmu mmu;
  221. struct kvm_mmu_memory_cache mmu_pte_chain_cache;
  222. struct kvm_mmu_memory_cache mmu_rmap_desc_cache;
  223. gfn_t last_pt_write_gfn;
  224. int last_pt_write_count;
  225. struct kvm_guest_debug guest_debug;
  226. char fx_buf[FX_BUF_SIZE];
  227. char *host_fx_image;
  228. char *guest_fx_image;
  229. int mmio_needed;
  230. int mmio_read_completed;
  231. int mmio_is_write;
  232. int mmio_size;
  233. unsigned char mmio_data[8];
  234. gpa_t mmio_phys_addr;
  235. struct {
  236. int active;
  237. u8 save_iopl;
  238. struct kvm_save_segment {
  239. u16 selector;
  240. unsigned long base;
  241. u32 limit;
  242. u32 ar;
  243. } tr, es, ds, fs, gs;
  244. } rmode;
  245. };
  246. struct kvm_memory_slot {
  247. gfn_t base_gfn;
  248. unsigned long npages;
  249. unsigned long flags;
  250. struct page **phys_mem;
  251. unsigned long *dirty_bitmap;
  252. };
  253. struct kvm {
  254. spinlock_t lock; /* protects everything except vcpus */
  255. int nmemslots;
  256. struct kvm_memory_slot memslots[KVM_MEMORY_SLOTS];
  257. /*
  258. * Hash table of struct kvm_mmu_page.
  259. */
  260. struct list_head active_mmu_pages;
  261. int n_free_mmu_pages;
  262. struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
  263. struct kvm_vcpu vcpus[KVM_MAX_VCPUS];
  264. int memory_config_version;
  265. int busy;
  266. unsigned long rmap_overflow;
  267. struct list_head vm_list;
  268. struct file *filp;
  269. };
  270. struct kvm_stat {
  271. u32 pf_fixed;
  272. u32 pf_guest;
  273. u32 tlb_flush;
  274. u32 invlpg;
  275. u32 exits;
  276. u32 io_exits;
  277. u32 mmio_exits;
  278. u32 signal_exits;
  279. u32 irq_window_exits;
  280. u32 halt_exits;
  281. u32 request_irq_exits;
  282. u32 irq_exits;
  283. };
  284. struct descriptor_table {
  285. u16 limit;
  286. unsigned long base;
  287. } __attribute__((packed));
  288. struct kvm_arch_ops {
  289. int (*cpu_has_kvm_support)(void); /* __init */
  290. int (*disabled_by_bios)(void); /* __init */
  291. void (*hardware_enable)(void *dummy); /* __init */
  292. void (*hardware_disable)(void *dummy);
  293. int (*hardware_setup)(void); /* __init */
  294. void (*hardware_unsetup)(void); /* __exit */
  295. int (*vcpu_create)(struct kvm_vcpu *vcpu);
  296. void (*vcpu_free)(struct kvm_vcpu *vcpu);
  297. void (*vcpu_load)(struct kvm_vcpu *vcpu);
  298. void (*vcpu_put)(struct kvm_vcpu *vcpu);
  299. void (*vcpu_decache)(struct kvm_vcpu *vcpu);
  300. int (*set_guest_debug)(struct kvm_vcpu *vcpu,
  301. struct kvm_debug_guest *dbg);
  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_cr0_cr4_guest_bits)(struct kvm_vcpu *vcpu);
  311. void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
  312. void (*set_cr0_no_modeswitch)(struct kvm_vcpu *vcpu,
  313. unsigned long cr0);
  314. void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
  315. void (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
  316. void (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
  317. void (*get_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  318. void (*set_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  319. void (*get_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  320. void (*set_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
  321. unsigned long (*get_dr)(struct kvm_vcpu *vcpu, int dr);
  322. void (*set_dr)(struct kvm_vcpu *vcpu, int dr, unsigned long value,
  323. int *exception);
  324. void (*cache_regs)(struct kvm_vcpu *vcpu);
  325. void (*decache_regs)(struct kvm_vcpu *vcpu);
  326. unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
  327. void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
  328. void (*invlpg)(struct kvm_vcpu *vcpu, gva_t addr);
  329. void (*tlb_flush)(struct kvm_vcpu *vcpu);
  330. void (*inject_page_fault)(struct kvm_vcpu *vcpu,
  331. unsigned long addr, u32 err_code);
  332. void (*inject_gp)(struct kvm_vcpu *vcpu, unsigned err_code);
  333. int (*run)(struct kvm_vcpu *vcpu, struct kvm_run *run);
  334. int (*vcpu_setup)(struct kvm_vcpu *vcpu);
  335. void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
  336. void (*patch_hypercall)(struct kvm_vcpu *vcpu,
  337. unsigned char *hypercall_addr);
  338. };
  339. extern struct kvm_stat kvm_stat;
  340. extern struct kvm_arch_ops *kvm_arch_ops;
  341. #define kvm_printf(kvm, fmt ...) printk(KERN_DEBUG fmt)
  342. #define vcpu_printf(vcpu, fmt...) kvm_printf(vcpu->kvm, fmt)
  343. int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module);
  344. void kvm_exit_arch(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. int kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
  349. void kvm_mmu_slot_remove_write_access(struct kvm_vcpu *vcpu, int slot);
  350. hpa_t gpa_to_hpa(struct kvm_vcpu *vcpu, gpa_t gpa);
  351. #define HPA_MSB ((sizeof(hpa_t) * 8) - 1)
  352. #define HPA_ERR_MASK ((hpa_t)1 << HPA_MSB)
  353. static inline int is_error_hpa(hpa_t hpa) { return hpa >> HPA_MSB; }
  354. hpa_t gva_to_hpa(struct kvm_vcpu *vcpu, gva_t gva);
  355. void kvm_emulator_want_group7_invlpg(void);
  356. extern hpa_t bad_page_address;
  357. static inline struct page *gfn_to_page(struct kvm_memory_slot *slot, gfn_t gfn)
  358. {
  359. return slot->phys_mem[gfn - slot->base_gfn];
  360. }
  361. struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn);
  362. void mark_page_dirty(struct kvm *kvm, gfn_t gfn);
  363. enum emulation_result {
  364. EMULATE_DONE, /* no further processing */
  365. EMULATE_DO_MMIO, /* kvm_run filled with mmio request */
  366. EMULATE_FAIL, /* can't emulate this instruction */
  367. };
  368. int emulate_instruction(struct kvm_vcpu *vcpu, struct kvm_run *run,
  369. unsigned long cr2, u16 error_code);
  370. void realmode_lgdt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
  371. void realmode_lidt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
  372. void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
  373. unsigned long *rflags);
  374. unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr);
  375. void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long value,
  376. unsigned long *rflags);
  377. struct x86_emulate_ctxt;
  378. int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address);
  379. int emulate_clts(struct kvm_vcpu *vcpu);
  380. int emulator_get_dr(struct x86_emulate_ctxt* ctxt, int dr,
  381. unsigned long *dest);
  382. int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
  383. unsigned long value);
  384. void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
  385. void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr0);
  386. void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr0);
  387. void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr0);
  388. void lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
  389. int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
  390. int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data);
  391. void fx_init(struct kvm_vcpu *vcpu);
  392. void load_msrs(struct vmx_msr_entry *e, int n);
  393. void save_msrs(struct vmx_msr_entry *e, int n);
  394. void kvm_resched(struct kvm_vcpu *vcpu);
  395. int kvm_read_guest(struct kvm_vcpu *vcpu,
  396. gva_t addr,
  397. unsigned long size,
  398. void *dest);
  399. int kvm_write_guest(struct kvm_vcpu *vcpu,
  400. gva_t addr,
  401. unsigned long size,
  402. void *data);
  403. unsigned long segment_base(u16 selector);
  404. void kvm_mmu_pre_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes);
  405. void kvm_mmu_post_write(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes);
  406. int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva);
  407. void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);
  408. int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run);
  409. static inline int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
  410. u32 error_code)
  411. {
  412. if (unlikely(vcpu->kvm->n_free_mmu_pages < KVM_MIN_FREE_MMU_PAGES))
  413. kvm_mmu_free_some_pages(vcpu);
  414. return vcpu->mmu.page_fault(vcpu, gva, error_code);
  415. }
  416. static inline struct page *_gfn_to_page(struct kvm *kvm, gfn_t gfn)
  417. {
  418. struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
  419. return (slot) ? slot->phys_mem[gfn - slot->base_gfn] : NULL;
  420. }
  421. static inline int is_long_mode(struct kvm_vcpu *vcpu)
  422. {
  423. #ifdef CONFIG_X86_64
  424. return vcpu->shadow_efer & EFER_LME;
  425. #else
  426. return 0;
  427. #endif
  428. }
  429. static inline int is_pae(struct kvm_vcpu *vcpu)
  430. {
  431. return vcpu->cr4 & CR4_PAE_MASK;
  432. }
  433. static inline int is_pse(struct kvm_vcpu *vcpu)
  434. {
  435. return vcpu->cr4 & CR4_PSE_MASK;
  436. }
  437. static inline int is_paging(struct kvm_vcpu *vcpu)
  438. {
  439. return vcpu->cr0 & CR0_PG_MASK;
  440. }
  441. static inline int memslot_id(struct kvm *kvm, struct kvm_memory_slot *slot)
  442. {
  443. return slot - kvm->memslots;
  444. }
  445. static inline struct kvm_mmu_page *page_header(hpa_t shadow_page)
  446. {
  447. struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
  448. return (struct kvm_mmu_page *)page_private(page);
  449. }
  450. static inline u16 read_fs(void)
  451. {
  452. u16 seg;
  453. asm ("mov %%fs, %0" : "=g"(seg));
  454. return seg;
  455. }
  456. static inline u16 read_gs(void)
  457. {
  458. u16 seg;
  459. asm ("mov %%gs, %0" : "=g"(seg));
  460. return seg;
  461. }
  462. static inline u16 read_ldt(void)
  463. {
  464. u16 ldt;
  465. asm ("sldt %0" : "=g"(ldt));
  466. return ldt;
  467. }
  468. static inline void load_fs(u16 sel)
  469. {
  470. asm ("mov %0, %%fs" : : "rm"(sel));
  471. }
  472. static inline void load_gs(u16 sel)
  473. {
  474. asm ("mov %0, %%gs" : : "rm"(sel));
  475. }
  476. #ifndef load_ldt
  477. static inline void load_ldt(u16 sel)
  478. {
  479. asm ("lldt %0" : : "rm"(sel));
  480. }
  481. #endif
  482. static inline void get_idt(struct descriptor_table *table)
  483. {
  484. asm ("sidt %0" : "=m"(*table));
  485. }
  486. static inline void get_gdt(struct descriptor_table *table)
  487. {
  488. asm ("sgdt %0" : "=m"(*table));
  489. }
  490. static inline unsigned long read_tr_base(void)
  491. {
  492. u16 tr;
  493. asm ("str %0" : "=g"(tr));
  494. return segment_base(tr);
  495. }
  496. #ifdef CONFIG_X86_64
  497. static inline unsigned long read_msr(unsigned long msr)
  498. {
  499. u64 value;
  500. rdmsrl(msr, value);
  501. return value;
  502. }
  503. #endif
  504. static inline void fx_save(void *image)
  505. {
  506. asm ("fxsave (%0)":: "r" (image));
  507. }
  508. static inline void fx_restore(void *image)
  509. {
  510. asm ("fxrstor (%0)":: "r" (image));
  511. }
  512. static inline void fpu_init(void)
  513. {
  514. asm ("finit");
  515. }
  516. static inline u32 get_rdx_init_val(void)
  517. {
  518. return 0x600; /* P6 family */
  519. }
  520. #define ASM_VMX_VMCLEAR_RAX ".byte 0x66, 0x0f, 0xc7, 0x30"
  521. #define ASM_VMX_VMLAUNCH ".byte 0x0f, 0x01, 0xc2"
  522. #define ASM_VMX_VMRESUME ".byte 0x0f, 0x01, 0xc3"
  523. #define ASM_VMX_VMPTRLD_RAX ".byte 0x0f, 0xc7, 0x30"
  524. #define ASM_VMX_VMREAD_RDX_RAX ".byte 0x0f, 0x78, 0xd0"
  525. #define ASM_VMX_VMWRITE_RAX_RDX ".byte 0x0f, 0x79, 0xd0"
  526. #define ASM_VMX_VMWRITE_RSP_RDX ".byte 0x0f, 0x79, 0xd4"
  527. #define ASM_VMX_VMXOFF ".byte 0x0f, 0x01, 0xc4"
  528. #define ASM_VMX_VMXON_RAX ".byte 0xf3, 0x0f, 0xc7, 0x30"
  529. #define MSR_IA32_TIME_STAMP_COUNTER 0x010
  530. #define TSS_IOPB_BASE_OFFSET 0x66
  531. #define TSS_BASE_SIZE 0x68
  532. #define TSS_IOPB_SIZE (65536 / 8)
  533. #define TSS_REDIRECTION_SIZE (256 / 8)
  534. #define RMODE_TSS_SIZE (TSS_BASE_SIZE + TSS_REDIRECTION_SIZE + TSS_IOPB_SIZE + 1)
  535. #endif