kvm_host.h 8.9 KB

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