kvm_host.h 9.3 KB

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