kvm_host.h 9.8 KB

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