kvm_host.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. *
  15. * Copyright IBM Corp. 2007
  16. *
  17. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  18. */
  19. #ifndef __POWERPC_KVM_HOST_H__
  20. #define __POWERPC_KVM_HOST_H__
  21. #include <linux/mutex.h>
  22. #include <linux/hrtimer.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/types.h>
  25. #include <linux/kvm_types.h>
  26. #include <linux/threads.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/kvm_para.h>
  29. #include <linux/list.h>
  30. #include <linux/atomic.h>
  31. #include <asm/kvm_asm.h>
  32. #include <asm/processor.h>
  33. #include <asm/page.h>
  34. #define KVM_MAX_VCPUS NR_CPUS
  35. #define KVM_MAX_VCORES NR_CPUS
  36. #define KVM_MEMORY_SLOTS 32
  37. /* memory slots that does not exposed to userspace */
  38. #define KVM_PRIVATE_MEM_SLOTS 4
  39. #define KVM_MEM_SLOTS_NUM (KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS)
  40. #ifdef CONFIG_KVM_MMIO
  41. #define KVM_COALESCED_MMIO_PAGE_OFFSET 1
  42. #endif
  43. #ifdef CONFIG_KVM_BOOK3S_64_HV
  44. #include <linux/mmu_notifier.h>
  45. #define KVM_ARCH_WANT_MMU_NOTIFIER
  46. struct kvm;
  47. extern int kvm_unmap_hva(struct kvm *kvm, unsigned long hva);
  48. extern int kvm_age_hva(struct kvm *kvm, unsigned long hva);
  49. extern int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);
  50. extern void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte);
  51. #endif
  52. /* We don't currently support large pages. */
  53. #define KVM_HPAGE_GFN_SHIFT(x) 0
  54. #define KVM_NR_PAGE_SIZES 1
  55. #define KVM_PAGES_PER_HPAGE(x) (1UL<<31)
  56. #define HPTEG_CACHE_NUM (1 << 15)
  57. #define HPTEG_HASH_BITS_PTE 13
  58. #define HPTEG_HASH_BITS_PTE_LONG 12
  59. #define HPTEG_HASH_BITS_VPTE 13
  60. #define HPTEG_HASH_BITS_VPTE_LONG 5
  61. #define HPTEG_HASH_NUM_PTE (1 << HPTEG_HASH_BITS_PTE)
  62. #define HPTEG_HASH_NUM_PTE_LONG (1 << HPTEG_HASH_BITS_PTE_LONG)
  63. #define HPTEG_HASH_NUM_VPTE (1 << HPTEG_HASH_BITS_VPTE)
  64. #define HPTEG_HASH_NUM_VPTE_LONG (1 << HPTEG_HASH_BITS_VPTE_LONG)
  65. /* Physical Address Mask - allowed range of real mode RAM access */
  66. #define KVM_PAM 0x0fffffffffffffffULL
  67. struct kvm;
  68. struct kvm_run;
  69. struct kvm_vcpu;
  70. struct lppaca;
  71. struct slb_shadow;
  72. struct dtl;
  73. struct kvm_vm_stat {
  74. u32 remote_tlb_flush;
  75. };
  76. struct kvm_vcpu_stat {
  77. u32 sum_exits;
  78. u32 mmio_exits;
  79. u32 dcr_exits;
  80. u32 signal_exits;
  81. u32 light_exits;
  82. /* Account for special types of light exits: */
  83. u32 itlb_real_miss_exits;
  84. u32 itlb_virt_miss_exits;
  85. u32 dtlb_real_miss_exits;
  86. u32 dtlb_virt_miss_exits;
  87. u32 syscall_exits;
  88. u32 isi_exits;
  89. u32 dsi_exits;
  90. u32 emulated_inst_exits;
  91. u32 dec_exits;
  92. u32 ext_intr_exits;
  93. u32 halt_wakeup;
  94. #ifdef CONFIG_PPC_BOOK3S
  95. u32 pf_storage;
  96. u32 pf_instruc;
  97. u32 sp_storage;
  98. u32 sp_instruc;
  99. u32 queue_intr;
  100. u32 ld;
  101. u32 ld_slow;
  102. u32 st;
  103. u32 st_slow;
  104. #endif
  105. };
  106. enum kvm_exit_types {
  107. MMIO_EXITS,
  108. DCR_EXITS,
  109. SIGNAL_EXITS,
  110. ITLB_REAL_MISS_EXITS,
  111. ITLB_VIRT_MISS_EXITS,
  112. DTLB_REAL_MISS_EXITS,
  113. DTLB_VIRT_MISS_EXITS,
  114. SYSCALL_EXITS,
  115. ISI_EXITS,
  116. DSI_EXITS,
  117. EMULATED_INST_EXITS,
  118. EMULATED_MTMSRWE_EXITS,
  119. EMULATED_WRTEE_EXITS,
  120. EMULATED_MTSPR_EXITS,
  121. EMULATED_MFSPR_EXITS,
  122. EMULATED_MTMSR_EXITS,
  123. EMULATED_MFMSR_EXITS,
  124. EMULATED_TLBSX_EXITS,
  125. EMULATED_TLBWE_EXITS,
  126. EMULATED_RFI_EXITS,
  127. DEC_EXITS,
  128. EXT_INTR_EXITS,
  129. HALT_WAKEUP,
  130. USR_PR_INST,
  131. FP_UNAVAIL,
  132. DEBUG_EXITS,
  133. TIMEINGUEST,
  134. __NUMBER_OF_KVM_EXIT_TYPES
  135. };
  136. /* allow access to big endian 32bit upper/lower parts and 64bit var */
  137. struct kvmppc_exit_timing {
  138. union {
  139. u64 tv64;
  140. struct {
  141. u32 tbu, tbl;
  142. } tv32;
  143. };
  144. };
  145. struct kvmppc_pginfo {
  146. unsigned long pfn;
  147. atomic_t refcnt;
  148. };
  149. struct kvmppc_spapr_tce_table {
  150. struct list_head list;
  151. struct kvm *kvm;
  152. u64 liobn;
  153. u32 window_size;
  154. struct page *pages[0];
  155. };
  156. struct kvmppc_linear_info {
  157. void *base_virt;
  158. unsigned long base_pfn;
  159. unsigned long npages;
  160. struct list_head list;
  161. atomic_t use_count;
  162. int type;
  163. };
  164. /*
  165. * The reverse mapping array has one entry for each HPTE,
  166. * which stores the guest's view of the second word of the HPTE
  167. * (including the guest physical address of the mapping),
  168. * plus forward and backward pointers in a doubly-linked ring
  169. * of HPTEs that map the same host page. The pointers in this
  170. * ring are 32-bit HPTE indexes, to save space.
  171. */
  172. struct revmap_entry {
  173. unsigned long guest_rpte;
  174. unsigned int forw, back;
  175. };
  176. /*
  177. * We use the top bit of each memslot->rmap entry as a lock bit,
  178. * and bit 32 as a present flag. The bottom 32 bits are the
  179. * index in the guest HPT of a HPTE that points to the page.
  180. */
  181. #define KVMPPC_RMAP_LOCK_BIT 63
  182. #define KVMPPC_RMAP_RC_SHIFT 32
  183. #define KVMPPC_RMAP_REFERENCED (HPTE_R_R << KVMPPC_RMAP_RC_SHIFT)
  184. #define KVMPPC_RMAP_CHANGED (HPTE_R_C << KVMPPC_RMAP_RC_SHIFT)
  185. #define KVMPPC_RMAP_PRESENT 0x100000000ul
  186. #define KVMPPC_RMAP_INDEX 0xfffffffful
  187. /* Low-order bits in kvm->arch.slot_phys[][] */
  188. #define KVMPPC_PAGE_ORDER_MASK 0x1f
  189. #define KVMPPC_PAGE_NO_CACHE HPTE_R_I /* 0x20 */
  190. #define KVMPPC_PAGE_WRITETHRU HPTE_R_W /* 0x40 */
  191. #define KVMPPC_GOT_PAGE 0x80
  192. struct kvm_arch_memory_slot {
  193. };
  194. struct kvm_arch {
  195. #ifdef CONFIG_KVM_BOOK3S_64_HV
  196. unsigned long hpt_virt;
  197. struct revmap_entry *revmap;
  198. unsigned int lpid;
  199. unsigned int host_lpid;
  200. unsigned long host_lpcr;
  201. unsigned long sdr1;
  202. unsigned long host_sdr1;
  203. int tlbie_lock;
  204. unsigned long lpcr;
  205. unsigned long rmor;
  206. struct kvmppc_linear_info *rma;
  207. unsigned long vrma_slb_v;
  208. int rma_setup_done;
  209. int using_mmu_notifiers;
  210. struct list_head spapr_tce_tables;
  211. spinlock_t slot_phys_lock;
  212. unsigned long *slot_phys[KVM_MEM_SLOTS_NUM];
  213. int slot_npages[KVM_MEM_SLOTS_NUM];
  214. unsigned short last_vcpu[NR_CPUS];
  215. struct kvmppc_vcore *vcores[KVM_MAX_VCORES];
  216. struct kvmppc_linear_info *hpt_li;
  217. #endif /* CONFIG_KVM_BOOK3S_64_HV */
  218. };
  219. /*
  220. * Struct for a virtual core.
  221. * Note: entry_exit_count combines an entry count in the bottom 8 bits
  222. * and an exit count in the next 8 bits. This is so that we can
  223. * atomically increment the entry count iff the exit count is 0
  224. * without taking the lock.
  225. */
  226. struct kvmppc_vcore {
  227. int n_runnable;
  228. int n_busy;
  229. int num_threads;
  230. int entry_exit_count;
  231. int n_woken;
  232. int nap_count;
  233. int napping_threads;
  234. u16 pcpu;
  235. u8 vcore_state;
  236. u8 in_guest;
  237. struct list_head runnable_threads;
  238. spinlock_t lock;
  239. wait_queue_head_t wq;
  240. };
  241. #define VCORE_ENTRY_COUNT(vc) ((vc)->entry_exit_count & 0xff)
  242. #define VCORE_EXIT_COUNT(vc) ((vc)->entry_exit_count >> 8)
  243. /* Values for vcore_state */
  244. #define VCORE_INACTIVE 0
  245. #define VCORE_RUNNING 1
  246. #define VCORE_EXITING 2
  247. #define VCORE_SLEEPING 3
  248. struct kvmppc_pte {
  249. ulong eaddr;
  250. u64 vpage;
  251. ulong raddr;
  252. bool may_read : 1;
  253. bool may_write : 1;
  254. bool may_execute : 1;
  255. };
  256. struct kvmppc_mmu {
  257. /* book3s_64 only */
  258. void (*slbmte)(struct kvm_vcpu *vcpu, u64 rb, u64 rs);
  259. u64 (*slbmfee)(struct kvm_vcpu *vcpu, u64 slb_nr);
  260. u64 (*slbmfev)(struct kvm_vcpu *vcpu, u64 slb_nr);
  261. void (*slbie)(struct kvm_vcpu *vcpu, u64 slb_nr);
  262. void (*slbia)(struct kvm_vcpu *vcpu);
  263. /* book3s */
  264. void (*mtsrin)(struct kvm_vcpu *vcpu, u32 srnum, ulong value);
  265. u32 (*mfsrin)(struct kvm_vcpu *vcpu, u32 srnum);
  266. int (*xlate)(struct kvm_vcpu *vcpu, gva_t eaddr, struct kvmppc_pte *pte, bool data);
  267. void (*reset_msr)(struct kvm_vcpu *vcpu);
  268. void (*tlbie)(struct kvm_vcpu *vcpu, ulong addr, bool large);
  269. int (*esid_to_vsid)(struct kvm_vcpu *vcpu, ulong esid, u64 *vsid);
  270. u64 (*ea_to_vp)(struct kvm_vcpu *vcpu, gva_t eaddr, bool data);
  271. bool (*is_dcbz32)(struct kvm_vcpu *vcpu);
  272. };
  273. struct kvmppc_slb {
  274. u64 esid;
  275. u64 vsid;
  276. u64 orige;
  277. u64 origv;
  278. bool valid : 1;
  279. bool Ks : 1;
  280. bool Kp : 1;
  281. bool nx : 1;
  282. bool large : 1; /* PTEs are 16MB */
  283. bool tb : 1; /* 1TB segment */
  284. bool class : 1;
  285. };
  286. struct kvm_vcpu_arch {
  287. ulong host_stack;
  288. u32 host_pid;
  289. #ifdef CONFIG_PPC_BOOK3S
  290. struct kvmppc_slb slb[64];
  291. int slb_max; /* 1 + index of last valid entry in slb[] */
  292. int slb_nr; /* total number of entries in SLB */
  293. struct kvmppc_mmu mmu;
  294. #endif
  295. ulong gpr[32];
  296. u64 fpr[32];
  297. u64 fpscr;
  298. #ifdef CONFIG_SPE
  299. ulong evr[32];
  300. ulong spefscr;
  301. ulong host_spefscr;
  302. u64 acc;
  303. #endif
  304. #ifdef CONFIG_ALTIVEC
  305. vector128 vr[32];
  306. vector128 vscr;
  307. #endif
  308. #ifdef CONFIG_VSX
  309. u64 vsr[64];
  310. #endif
  311. #ifdef CONFIG_PPC_BOOK3S
  312. /* For Gekko paired singles */
  313. u32 qpr[32];
  314. #endif
  315. ulong pc;
  316. ulong ctr;
  317. ulong lr;
  318. ulong xer;
  319. u32 cr;
  320. #ifdef CONFIG_PPC_BOOK3S
  321. ulong hflags;
  322. ulong guest_owned_ext;
  323. ulong purr;
  324. ulong spurr;
  325. ulong dscr;
  326. ulong amr;
  327. ulong uamor;
  328. u32 ctrl;
  329. ulong dabr;
  330. #endif
  331. u32 vrsave; /* also USPRG0 */
  332. u32 mmucr;
  333. ulong shadow_msr;
  334. ulong csrr0;
  335. ulong csrr1;
  336. ulong dsrr0;
  337. ulong dsrr1;
  338. ulong mcsrr0;
  339. ulong mcsrr1;
  340. ulong mcsr;
  341. u32 dec;
  342. u32 decar;
  343. u32 tbl;
  344. u32 tbu;
  345. u32 tcr;
  346. ulong tsr; /* we need to perform set/clr_bits() which requires ulong */
  347. u32 ivor[64];
  348. ulong ivpr;
  349. u32 pvr;
  350. u32 shadow_pid;
  351. u32 shadow_pid1;
  352. u32 pid;
  353. u32 swap_pid;
  354. u32 ccr0;
  355. u32 ccr1;
  356. u32 dbcr0;
  357. u32 dbcr1;
  358. u32 dbsr;
  359. u64 mmcr[3];
  360. u32 pmc[8];
  361. #ifdef CONFIG_KVM_EXIT_TIMING
  362. struct mutex exit_timing_lock;
  363. struct kvmppc_exit_timing timing_exit;
  364. struct kvmppc_exit_timing timing_last_enter;
  365. u32 last_exit_type;
  366. u32 timing_count_type[__NUMBER_OF_KVM_EXIT_TYPES];
  367. u64 timing_sum_duration[__NUMBER_OF_KVM_EXIT_TYPES];
  368. u64 timing_sum_quad_duration[__NUMBER_OF_KVM_EXIT_TYPES];
  369. u64 timing_min_duration[__NUMBER_OF_KVM_EXIT_TYPES];
  370. u64 timing_max_duration[__NUMBER_OF_KVM_EXIT_TYPES];
  371. u64 timing_last_exit;
  372. struct dentry *debugfs_exit_timing;
  373. #endif
  374. #ifdef CONFIG_PPC_BOOK3S
  375. ulong fault_dar;
  376. u32 fault_dsisr;
  377. #endif
  378. #ifdef CONFIG_BOOKE
  379. ulong fault_dear;
  380. ulong fault_esr;
  381. ulong queued_dear;
  382. ulong queued_esr;
  383. #endif
  384. gpa_t paddr_accessed;
  385. u8 io_gpr; /* GPR used as IO source/target */
  386. u8 mmio_is_bigendian;
  387. u8 mmio_sign_extend;
  388. u8 dcr_needed;
  389. u8 dcr_is_write;
  390. u8 osi_needed;
  391. u8 osi_enabled;
  392. u8 papr_enabled;
  393. u8 sane;
  394. u8 cpu_type;
  395. u8 hcall_needed;
  396. u32 cpr0_cfgaddr; /* holds the last set cpr0_cfgaddr */
  397. struct hrtimer dec_timer;
  398. struct tasklet_struct tasklet;
  399. u64 dec_jiffies;
  400. u64 dec_expires;
  401. unsigned long pending_exceptions;
  402. u16 last_cpu;
  403. u8 ceded;
  404. u8 prodded;
  405. u32 last_inst;
  406. struct lppaca *vpa;
  407. struct slb_shadow *slb_shadow;
  408. struct dtl *dtl;
  409. struct dtl *dtl_end;
  410. wait_queue_head_t *wqp;
  411. struct kvmppc_vcore *vcore;
  412. int ret;
  413. int trap;
  414. int state;
  415. int ptid;
  416. bool timer_running;
  417. wait_queue_head_t cpu_run;
  418. struct kvm_vcpu_arch_shared *shared;
  419. unsigned long magic_page_pa; /* phys addr to map the magic page to */
  420. unsigned long magic_page_ea; /* effect. addr to map the magic page to */
  421. #ifdef CONFIG_KVM_BOOK3S_64_HV
  422. struct kvm_vcpu_arch_shared shregs;
  423. unsigned long pgfault_addr;
  424. long pgfault_index;
  425. unsigned long pgfault_hpte[2];
  426. struct list_head run_list;
  427. struct task_struct *run_task;
  428. struct kvm_run *kvm_run;
  429. pgd_t *pgdir;
  430. #endif
  431. };
  432. /* Values for vcpu->arch.state */
  433. #define KVMPPC_VCPU_STOPPED 0
  434. #define KVMPPC_VCPU_BUSY_IN_HOST 1
  435. #define KVMPPC_VCPU_RUNNABLE 2
  436. /* Values for vcpu->arch.io_gpr */
  437. #define KVM_MMIO_REG_MASK 0x001f
  438. #define KVM_MMIO_REG_EXT_MASK 0xffe0
  439. #define KVM_MMIO_REG_GPR 0x0000
  440. #define KVM_MMIO_REG_FPR 0x0020
  441. #define KVM_MMIO_REG_QPR 0x0040
  442. #define KVM_MMIO_REG_FQPR 0x0060
  443. #endif /* __POWERPC_KVM_HOST_H__ */