kvm_host.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. #include <asm/cacheflush.h>
  35. #define KVM_MAX_VCPUS NR_CPUS
  36. #define KVM_MAX_VCORES NR_CPUS
  37. #define KVM_USER_MEM_SLOTS 32
  38. #define KVM_MEM_SLOTS_NUM KVM_USER_MEM_SLOTS
  39. #ifdef CONFIG_KVM_MMIO
  40. #define KVM_COALESCED_MMIO_PAGE_OFFSET 1
  41. #endif
  42. #if !defined(CONFIG_KVM_440)
  43. #include <linux/mmu_notifier.h>
  44. #define KVM_ARCH_WANT_MMU_NOTIFIER
  45. struct kvm;
  46. extern int kvm_unmap_hva(struct kvm *kvm, unsigned long hva);
  47. extern int kvm_unmap_hva_range(struct kvm *kvm,
  48. unsigned long start, unsigned long end);
  49. extern int kvm_age_hva(struct kvm *kvm, unsigned long hva);
  50. extern int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);
  51. extern void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte);
  52. #endif
  53. /* We don't currently support large pages. */
  54. #define KVM_HPAGE_GFN_SHIFT(x) 0
  55. #define KVM_NR_PAGE_SIZES 1
  56. #define KVM_PAGES_PER_HPAGE(x) (1UL<<31)
  57. #define HPTEG_CACHE_NUM (1 << 15)
  58. #define HPTEG_HASH_BITS_PTE 13
  59. #define HPTEG_HASH_BITS_PTE_LONG 12
  60. #define HPTEG_HASH_BITS_VPTE 13
  61. #define HPTEG_HASH_BITS_VPTE_LONG 5
  62. #define HPTEG_HASH_NUM_PTE (1 << HPTEG_HASH_BITS_PTE)
  63. #define HPTEG_HASH_NUM_PTE_LONG (1 << HPTEG_HASH_BITS_PTE_LONG)
  64. #define HPTEG_HASH_NUM_VPTE (1 << HPTEG_HASH_BITS_VPTE)
  65. #define HPTEG_HASH_NUM_VPTE_LONG (1 << HPTEG_HASH_BITS_VPTE_LONG)
  66. /* Physical Address Mask - allowed range of real mode RAM access */
  67. #define KVM_PAM 0x0fffffffffffffffULL
  68. struct kvm;
  69. struct kvm_run;
  70. struct kvm_vcpu;
  71. struct lppaca;
  72. struct slb_shadow;
  73. struct dtl_entry;
  74. struct kvm_vm_stat {
  75. u32 remote_tlb_flush;
  76. };
  77. struct kvm_vcpu_stat {
  78. u32 sum_exits;
  79. u32 mmio_exits;
  80. u32 dcr_exits;
  81. u32 signal_exits;
  82. u32 light_exits;
  83. /* Account for special types of light exits: */
  84. u32 itlb_real_miss_exits;
  85. u32 itlb_virt_miss_exits;
  86. u32 dtlb_real_miss_exits;
  87. u32 dtlb_virt_miss_exits;
  88. u32 syscall_exits;
  89. u32 isi_exits;
  90. u32 dsi_exits;
  91. u32 emulated_inst_exits;
  92. u32 dec_exits;
  93. u32 ext_intr_exits;
  94. u32 halt_wakeup;
  95. u32 dbell_exits;
  96. u32 gdbell_exits;
  97. #ifdef CONFIG_PPC_BOOK3S
  98. u32 pf_storage;
  99. u32 pf_instruc;
  100. u32 sp_storage;
  101. u32 sp_instruc;
  102. u32 queue_intr;
  103. u32 ld;
  104. u32 ld_slow;
  105. u32 st;
  106. u32 st_slow;
  107. #endif
  108. };
  109. enum kvm_exit_types {
  110. MMIO_EXITS,
  111. DCR_EXITS,
  112. SIGNAL_EXITS,
  113. ITLB_REAL_MISS_EXITS,
  114. ITLB_VIRT_MISS_EXITS,
  115. DTLB_REAL_MISS_EXITS,
  116. DTLB_VIRT_MISS_EXITS,
  117. SYSCALL_EXITS,
  118. ISI_EXITS,
  119. DSI_EXITS,
  120. EMULATED_INST_EXITS,
  121. EMULATED_MTMSRWE_EXITS,
  122. EMULATED_WRTEE_EXITS,
  123. EMULATED_MTSPR_EXITS,
  124. EMULATED_MFSPR_EXITS,
  125. EMULATED_MTMSR_EXITS,
  126. EMULATED_MFMSR_EXITS,
  127. EMULATED_TLBSX_EXITS,
  128. EMULATED_TLBWE_EXITS,
  129. EMULATED_RFI_EXITS,
  130. EMULATED_RFCI_EXITS,
  131. DEC_EXITS,
  132. EXT_INTR_EXITS,
  133. HALT_WAKEUP,
  134. USR_PR_INST,
  135. FP_UNAVAIL,
  136. DEBUG_EXITS,
  137. TIMEINGUEST,
  138. DBELL_EXITS,
  139. GDBELL_EXITS,
  140. __NUMBER_OF_KVM_EXIT_TYPES
  141. };
  142. /* allow access to big endian 32bit upper/lower parts and 64bit var */
  143. struct kvmppc_exit_timing {
  144. union {
  145. u64 tv64;
  146. struct {
  147. u32 tbu, tbl;
  148. } tv32;
  149. };
  150. };
  151. struct kvmppc_pginfo {
  152. unsigned long pfn;
  153. atomic_t refcnt;
  154. };
  155. struct kvmppc_spapr_tce_table {
  156. struct list_head list;
  157. struct kvm *kvm;
  158. u64 liobn;
  159. u32 window_size;
  160. struct page *pages[0];
  161. };
  162. struct kvmppc_linear_info {
  163. void *base_virt;
  164. unsigned long base_pfn;
  165. unsigned long npages;
  166. struct list_head list;
  167. atomic_t use_count;
  168. int type;
  169. };
  170. /*
  171. * The reverse mapping array has one entry for each HPTE,
  172. * which stores the guest's view of the second word of the HPTE
  173. * (including the guest physical address of the mapping),
  174. * plus forward and backward pointers in a doubly-linked ring
  175. * of HPTEs that map the same host page. The pointers in this
  176. * ring are 32-bit HPTE indexes, to save space.
  177. */
  178. struct revmap_entry {
  179. unsigned long guest_rpte;
  180. unsigned int forw, back;
  181. };
  182. /*
  183. * We use the top bit of each memslot->arch.rmap entry as a lock bit,
  184. * and bit 32 as a present flag. The bottom 32 bits are the
  185. * index in the guest HPT of a HPTE that points to the page.
  186. */
  187. #define KVMPPC_RMAP_LOCK_BIT 63
  188. #define KVMPPC_RMAP_RC_SHIFT 32
  189. #define KVMPPC_RMAP_REFERENCED (HPTE_R_R << KVMPPC_RMAP_RC_SHIFT)
  190. #define KVMPPC_RMAP_CHANGED (HPTE_R_C << KVMPPC_RMAP_RC_SHIFT)
  191. #define KVMPPC_RMAP_PRESENT 0x100000000ul
  192. #define KVMPPC_RMAP_INDEX 0xfffffffful
  193. /* Low-order bits in memslot->arch.slot_phys[] */
  194. #define KVMPPC_PAGE_ORDER_MASK 0x1f
  195. #define KVMPPC_PAGE_NO_CACHE HPTE_R_I /* 0x20 */
  196. #define KVMPPC_PAGE_WRITETHRU HPTE_R_W /* 0x40 */
  197. #define KVMPPC_GOT_PAGE 0x80
  198. struct kvm_arch_memory_slot {
  199. #ifdef CONFIG_KVM_BOOK3S_64_HV
  200. unsigned long *rmap;
  201. unsigned long *slot_phys;
  202. #endif /* CONFIG_KVM_BOOK3S_64_HV */
  203. };
  204. struct kvm_arch {
  205. unsigned int lpid;
  206. #ifdef CONFIG_KVM_BOOK3S_64_HV
  207. unsigned long hpt_virt;
  208. struct revmap_entry *revmap;
  209. unsigned int host_lpid;
  210. unsigned long host_lpcr;
  211. unsigned long sdr1;
  212. unsigned long host_sdr1;
  213. int tlbie_lock;
  214. unsigned long lpcr;
  215. unsigned long rmor;
  216. struct kvmppc_linear_info *rma;
  217. unsigned long vrma_slb_v;
  218. int rma_setup_done;
  219. int using_mmu_notifiers;
  220. u32 hpt_order;
  221. atomic_t vcpus_running;
  222. u32 online_vcores;
  223. unsigned long hpt_npte;
  224. unsigned long hpt_mask;
  225. atomic_t hpte_mod_interest;
  226. spinlock_t slot_phys_lock;
  227. cpumask_t need_tlb_flush;
  228. struct kvmppc_vcore *vcores[KVM_MAX_VCORES];
  229. struct kvmppc_linear_info *hpt_li;
  230. #endif /* CONFIG_KVM_BOOK3S_64_HV */
  231. #ifdef CONFIG_PPC_BOOK3S_64
  232. struct list_head spapr_tce_tables;
  233. #endif
  234. };
  235. /*
  236. * Struct for a virtual core.
  237. * Note: entry_exit_count combines an entry count in the bottom 8 bits
  238. * and an exit count in the next 8 bits. This is so that we can
  239. * atomically increment the entry count iff the exit count is 0
  240. * without taking the lock.
  241. */
  242. struct kvmppc_vcore {
  243. int n_runnable;
  244. int n_busy;
  245. int num_threads;
  246. int entry_exit_count;
  247. int n_woken;
  248. int nap_count;
  249. int napping_threads;
  250. u16 pcpu;
  251. u16 last_cpu;
  252. u8 vcore_state;
  253. u8 in_guest;
  254. struct list_head runnable_threads;
  255. spinlock_t lock;
  256. wait_queue_head_t wq;
  257. u64 stolen_tb;
  258. u64 preempt_tb;
  259. struct kvm_vcpu *runner;
  260. };
  261. #define VCORE_ENTRY_COUNT(vc) ((vc)->entry_exit_count & 0xff)
  262. #define VCORE_EXIT_COUNT(vc) ((vc)->entry_exit_count >> 8)
  263. /* Values for vcore_state */
  264. #define VCORE_INACTIVE 0
  265. #define VCORE_SLEEPING 1
  266. #define VCORE_STARTING 2
  267. #define VCORE_RUNNING 3
  268. #define VCORE_EXITING 4
  269. /*
  270. * Struct used to manage memory for a virtual processor area
  271. * registered by a PAPR guest. There are three types of area
  272. * that a guest can register.
  273. */
  274. struct kvmppc_vpa {
  275. void *pinned_addr; /* Address in kernel linear mapping */
  276. void *pinned_end; /* End of region */
  277. unsigned long next_gpa; /* Guest phys addr for update */
  278. unsigned long len; /* Number of bytes required */
  279. u8 update_pending; /* 1 => update pinned_addr from next_gpa */
  280. };
  281. struct kvmppc_pte {
  282. ulong eaddr;
  283. u64 vpage;
  284. ulong raddr;
  285. bool may_read : 1;
  286. bool may_write : 1;
  287. bool may_execute : 1;
  288. };
  289. struct kvmppc_mmu {
  290. /* book3s_64 only */
  291. void (*slbmte)(struct kvm_vcpu *vcpu, u64 rb, u64 rs);
  292. u64 (*slbmfee)(struct kvm_vcpu *vcpu, u64 slb_nr);
  293. u64 (*slbmfev)(struct kvm_vcpu *vcpu, u64 slb_nr);
  294. void (*slbie)(struct kvm_vcpu *vcpu, u64 slb_nr);
  295. void (*slbia)(struct kvm_vcpu *vcpu);
  296. /* book3s */
  297. void (*mtsrin)(struct kvm_vcpu *vcpu, u32 srnum, ulong value);
  298. u32 (*mfsrin)(struct kvm_vcpu *vcpu, u32 srnum);
  299. int (*xlate)(struct kvm_vcpu *vcpu, gva_t eaddr, struct kvmppc_pte *pte, bool data);
  300. void (*reset_msr)(struct kvm_vcpu *vcpu);
  301. void (*tlbie)(struct kvm_vcpu *vcpu, ulong addr, bool large);
  302. int (*esid_to_vsid)(struct kvm_vcpu *vcpu, ulong esid, u64 *vsid);
  303. u64 (*ea_to_vp)(struct kvm_vcpu *vcpu, gva_t eaddr, bool data);
  304. bool (*is_dcbz32)(struct kvm_vcpu *vcpu);
  305. };
  306. struct kvmppc_slb {
  307. u64 esid;
  308. u64 vsid;
  309. u64 orige;
  310. u64 origv;
  311. bool valid : 1;
  312. bool Ks : 1;
  313. bool Kp : 1;
  314. bool nx : 1;
  315. bool large : 1; /* PTEs are 16MB */
  316. bool tb : 1; /* 1TB segment */
  317. bool class : 1;
  318. };
  319. # ifdef CONFIG_PPC_FSL_BOOK3E
  320. #define KVMPPC_BOOKE_IAC_NUM 2
  321. #define KVMPPC_BOOKE_DAC_NUM 2
  322. # else
  323. #define KVMPPC_BOOKE_IAC_NUM 4
  324. #define KVMPPC_BOOKE_DAC_NUM 2
  325. # endif
  326. #define KVMPPC_BOOKE_MAX_IAC 4
  327. #define KVMPPC_BOOKE_MAX_DAC 2
  328. struct kvmppc_booke_debug_reg {
  329. u32 dbcr0;
  330. u32 dbcr1;
  331. u32 dbcr2;
  332. #ifdef CONFIG_KVM_E500MC
  333. u32 dbcr4;
  334. #endif
  335. u64 iac[KVMPPC_BOOKE_MAX_IAC];
  336. u64 dac[KVMPPC_BOOKE_MAX_DAC];
  337. };
  338. struct kvm_vcpu_arch {
  339. ulong host_stack;
  340. u32 host_pid;
  341. #ifdef CONFIG_PPC_BOOK3S
  342. struct kvmppc_slb slb[64];
  343. int slb_max; /* 1 + index of last valid entry in slb[] */
  344. int slb_nr; /* total number of entries in SLB */
  345. struct kvmppc_mmu mmu;
  346. #endif
  347. ulong gpr[32];
  348. u64 fpr[32];
  349. u64 fpscr;
  350. #ifdef CONFIG_SPE
  351. ulong evr[32];
  352. ulong spefscr;
  353. ulong host_spefscr;
  354. u64 acc;
  355. #endif
  356. #ifdef CONFIG_ALTIVEC
  357. vector128 vr[32];
  358. vector128 vscr;
  359. #endif
  360. #ifdef CONFIG_VSX
  361. u64 vsr[64];
  362. #endif
  363. #ifdef CONFIG_KVM_BOOKE_HV
  364. u32 host_mas4;
  365. u32 host_mas6;
  366. u32 shadow_epcr;
  367. u32 shadow_msrp;
  368. u32 eplc;
  369. u32 epsc;
  370. u32 oldpir;
  371. #endif
  372. #if defined(CONFIG_BOOKE)
  373. #if defined(CONFIG_KVM_BOOKE_HV) || defined(CONFIG_64BIT)
  374. u32 epcr;
  375. #endif
  376. #endif
  377. #ifdef CONFIG_PPC_BOOK3S
  378. /* For Gekko paired singles */
  379. u32 qpr[32];
  380. #endif
  381. ulong pc;
  382. ulong ctr;
  383. ulong lr;
  384. ulong xer;
  385. u32 cr;
  386. #ifdef CONFIG_PPC_BOOK3S
  387. ulong hflags;
  388. ulong guest_owned_ext;
  389. ulong purr;
  390. ulong spurr;
  391. ulong dscr;
  392. ulong amr;
  393. ulong uamor;
  394. u32 ctrl;
  395. ulong dabr;
  396. ulong cfar;
  397. #endif
  398. u32 vrsave; /* also USPRG0 */
  399. u32 mmucr;
  400. /* shadow_msr is unused for BookE HV */
  401. ulong shadow_msr;
  402. ulong csrr0;
  403. ulong csrr1;
  404. ulong dsrr0;
  405. ulong dsrr1;
  406. ulong mcsrr0;
  407. ulong mcsrr1;
  408. ulong mcsr;
  409. u32 dec;
  410. #ifdef CONFIG_BOOKE
  411. u32 decar;
  412. #endif
  413. u32 tbl;
  414. u32 tbu;
  415. u32 tcr;
  416. ulong tsr; /* we need to perform set/clr_bits() which requires ulong */
  417. u32 ivor[64];
  418. ulong ivpr;
  419. u32 pvr;
  420. u32 shadow_pid;
  421. u32 shadow_pid1;
  422. u32 pid;
  423. u32 swap_pid;
  424. u32 ccr0;
  425. u32 ccr1;
  426. u32 dbsr;
  427. u64 mmcr[3];
  428. u32 pmc[8];
  429. #ifdef CONFIG_KVM_EXIT_TIMING
  430. struct mutex exit_timing_lock;
  431. struct kvmppc_exit_timing timing_exit;
  432. struct kvmppc_exit_timing timing_last_enter;
  433. u32 last_exit_type;
  434. u32 timing_count_type[__NUMBER_OF_KVM_EXIT_TYPES];
  435. u64 timing_sum_duration[__NUMBER_OF_KVM_EXIT_TYPES];
  436. u64 timing_sum_quad_duration[__NUMBER_OF_KVM_EXIT_TYPES];
  437. u64 timing_min_duration[__NUMBER_OF_KVM_EXIT_TYPES];
  438. u64 timing_max_duration[__NUMBER_OF_KVM_EXIT_TYPES];
  439. u64 timing_last_exit;
  440. struct dentry *debugfs_exit_timing;
  441. #endif
  442. #ifdef CONFIG_PPC_BOOK3S
  443. ulong fault_dar;
  444. u32 fault_dsisr;
  445. #endif
  446. #ifdef CONFIG_BOOKE
  447. ulong fault_dear;
  448. ulong fault_esr;
  449. ulong queued_dear;
  450. ulong queued_esr;
  451. spinlock_t wdt_lock;
  452. struct timer_list wdt_timer;
  453. u32 tlbcfg[4];
  454. u32 mmucfg;
  455. u32 epr;
  456. struct kvmppc_booke_debug_reg dbg_reg;
  457. #endif
  458. gpa_t paddr_accessed;
  459. gva_t vaddr_accessed;
  460. u8 io_gpr; /* GPR used as IO source/target */
  461. u8 mmio_is_bigendian;
  462. u8 mmio_sign_extend;
  463. u8 dcr_needed;
  464. u8 dcr_is_write;
  465. u8 osi_needed;
  466. u8 osi_enabled;
  467. u8 papr_enabled;
  468. u8 watchdog_enabled;
  469. u8 sane;
  470. u8 cpu_type;
  471. u8 hcall_needed;
  472. u8 epr_enabled;
  473. u8 epr_needed;
  474. u32 cpr0_cfgaddr; /* holds the last set cpr0_cfgaddr */
  475. struct hrtimer dec_timer;
  476. struct tasklet_struct tasklet;
  477. u64 dec_jiffies;
  478. u64 dec_expires;
  479. unsigned long pending_exceptions;
  480. u8 ceded;
  481. u8 prodded;
  482. u32 last_inst;
  483. wait_queue_head_t *wqp;
  484. struct kvmppc_vcore *vcore;
  485. int ret;
  486. int trap;
  487. int state;
  488. int ptid;
  489. bool timer_running;
  490. wait_queue_head_t cpu_run;
  491. struct kvm_vcpu_arch_shared *shared;
  492. unsigned long magic_page_pa; /* phys addr to map the magic page to */
  493. unsigned long magic_page_ea; /* effect. addr to map the magic page to */
  494. #ifdef CONFIG_KVM_BOOK3S_64_HV
  495. struct kvm_vcpu_arch_shared shregs;
  496. unsigned long pgfault_addr;
  497. long pgfault_index;
  498. unsigned long pgfault_hpte[2];
  499. struct list_head run_list;
  500. struct task_struct *run_task;
  501. struct kvm_run *kvm_run;
  502. pgd_t *pgdir;
  503. spinlock_t vpa_update_lock;
  504. struct kvmppc_vpa vpa;
  505. struct kvmppc_vpa dtl;
  506. struct dtl_entry *dtl_ptr;
  507. unsigned long dtl_index;
  508. u64 stolen_logged;
  509. struct kvmppc_vpa slb_shadow;
  510. spinlock_t tbacct_lock;
  511. u64 busy_stolen;
  512. u64 busy_preempt;
  513. #endif
  514. };
  515. /* Values for vcpu->arch.state */
  516. #define KVMPPC_VCPU_NOTREADY 0
  517. #define KVMPPC_VCPU_RUNNABLE 1
  518. #define KVMPPC_VCPU_BUSY_IN_HOST 2
  519. /* Values for vcpu->arch.io_gpr */
  520. #define KVM_MMIO_REG_MASK 0x001f
  521. #define KVM_MMIO_REG_EXT_MASK 0xffe0
  522. #define KVM_MMIO_REG_GPR 0x0000
  523. #define KVM_MMIO_REG_FPR 0x0020
  524. #define KVM_MMIO_REG_QPR 0x0040
  525. #define KVM_MMIO_REG_FQPR 0x0060
  526. #define __KVM_HAVE_ARCH_WQP
  527. #endif /* __POWERPC_KVM_HOST_H__ */