book3s_64_mmu_hv.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  16. */
  17. #include <linux/types.h>
  18. #include <linux/string.h>
  19. #include <linux/kvm.h>
  20. #include <linux/kvm_host.h>
  21. #include <linux/highmem.h>
  22. #include <linux/gfp.h>
  23. #include <linux/slab.h>
  24. #include <linux/hugetlb.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/srcu.h>
  27. #include <asm/tlbflush.h>
  28. #include <asm/kvm_ppc.h>
  29. #include <asm/kvm_book3s.h>
  30. #include <asm/mmu-hash64.h>
  31. #include <asm/hvcall.h>
  32. #include <asm/synch.h>
  33. #include <asm/ppc-opcode.h>
  34. #include <asm/cputable.h>
  35. /* POWER7 has 10-bit LPIDs, PPC970 has 6-bit LPIDs */
  36. #define MAX_LPID_970 63
  37. /* Power architecture requires HPT is at least 256kB */
  38. #define PPC_MIN_HPT_ORDER 18
  39. long kvmppc_alloc_hpt(struct kvm *kvm, u32 *htab_orderp)
  40. {
  41. unsigned long hpt;
  42. struct revmap_entry *rev;
  43. struct kvmppc_linear_info *li;
  44. long order = kvm_hpt_order;
  45. if (htab_orderp) {
  46. order = *htab_orderp;
  47. if (order < PPC_MIN_HPT_ORDER)
  48. order = PPC_MIN_HPT_ORDER;
  49. }
  50. /*
  51. * If the user wants a different size from default,
  52. * try first to allocate it from the kernel page allocator.
  53. */
  54. hpt = 0;
  55. if (order != kvm_hpt_order) {
  56. hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|
  57. __GFP_NOWARN, order - PAGE_SHIFT);
  58. if (!hpt)
  59. --order;
  60. }
  61. /* Next try to allocate from the preallocated pool */
  62. if (!hpt) {
  63. li = kvm_alloc_hpt();
  64. if (li) {
  65. hpt = (ulong)li->base_virt;
  66. kvm->arch.hpt_li = li;
  67. order = kvm_hpt_order;
  68. }
  69. }
  70. /* Lastly try successively smaller sizes from the page allocator */
  71. while (!hpt && order > PPC_MIN_HPT_ORDER) {
  72. hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|
  73. __GFP_NOWARN, order - PAGE_SHIFT);
  74. if (!hpt)
  75. --order;
  76. }
  77. if (!hpt)
  78. return -ENOMEM;
  79. kvm->arch.hpt_virt = hpt;
  80. kvm->arch.hpt_order = order;
  81. /* HPTEs are 2**4 bytes long */
  82. kvm->arch.hpt_npte = 1ul << (order - 4);
  83. /* 128 (2**7) bytes in each HPTEG */
  84. kvm->arch.hpt_mask = (1ul << (order - 7)) - 1;
  85. /* Allocate reverse map array */
  86. rev = vmalloc(sizeof(struct revmap_entry) * kvm->arch.hpt_npte);
  87. if (!rev) {
  88. pr_err("kvmppc_alloc_hpt: Couldn't alloc reverse map array\n");
  89. goto out_freehpt;
  90. }
  91. kvm->arch.revmap = rev;
  92. kvm->arch.sdr1 = __pa(hpt) | (order - 18);
  93. pr_info("KVM guest htab at %lx (order %ld), LPID %x\n",
  94. hpt, order, kvm->arch.lpid);
  95. if (htab_orderp)
  96. *htab_orderp = order;
  97. return 0;
  98. out_freehpt:
  99. if (kvm->arch.hpt_li)
  100. kvm_release_hpt(kvm->arch.hpt_li);
  101. else
  102. free_pages(hpt, order - PAGE_SHIFT);
  103. return -ENOMEM;
  104. }
  105. long kvmppc_alloc_reset_hpt(struct kvm *kvm, u32 *htab_orderp)
  106. {
  107. long err = -EBUSY;
  108. long order;
  109. mutex_lock(&kvm->lock);
  110. if (kvm->arch.rma_setup_done) {
  111. kvm->arch.rma_setup_done = 0;
  112. /* order rma_setup_done vs. vcpus_running */
  113. smp_mb();
  114. if (atomic_read(&kvm->arch.vcpus_running)) {
  115. kvm->arch.rma_setup_done = 1;
  116. goto out;
  117. }
  118. }
  119. if (kvm->arch.hpt_virt) {
  120. order = kvm->arch.hpt_order;
  121. /* Set the entire HPT to 0, i.e. invalid HPTEs */
  122. memset((void *)kvm->arch.hpt_virt, 0, 1ul << order);
  123. /*
  124. * Set the whole last_vcpu array to an invalid vcpu number.
  125. * This ensures that each vcpu will flush its TLB on next entry.
  126. */
  127. memset(kvm->arch.last_vcpu, 0xff, sizeof(kvm->arch.last_vcpu));
  128. *htab_orderp = order;
  129. err = 0;
  130. } else {
  131. err = kvmppc_alloc_hpt(kvm, htab_orderp);
  132. order = *htab_orderp;
  133. }
  134. out:
  135. mutex_unlock(&kvm->lock);
  136. return err;
  137. }
  138. void kvmppc_free_hpt(struct kvm *kvm)
  139. {
  140. kvmppc_free_lpid(kvm->arch.lpid);
  141. vfree(kvm->arch.revmap);
  142. if (kvm->arch.hpt_li)
  143. kvm_release_hpt(kvm->arch.hpt_li);
  144. else
  145. free_pages(kvm->arch.hpt_virt,
  146. kvm->arch.hpt_order - PAGE_SHIFT);
  147. }
  148. /* Bits in first HPTE dword for pagesize 4k, 64k or 16M */
  149. static inline unsigned long hpte0_pgsize_encoding(unsigned long pgsize)
  150. {
  151. return (pgsize > 0x1000) ? HPTE_V_LARGE : 0;
  152. }
  153. /* Bits in second HPTE dword for pagesize 4k, 64k or 16M */
  154. static inline unsigned long hpte1_pgsize_encoding(unsigned long pgsize)
  155. {
  156. return (pgsize == 0x10000) ? 0x1000 : 0;
  157. }
  158. void kvmppc_map_vrma(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,
  159. unsigned long porder)
  160. {
  161. unsigned long i;
  162. unsigned long npages;
  163. unsigned long hp_v, hp_r;
  164. unsigned long addr, hash;
  165. unsigned long psize;
  166. unsigned long hp0, hp1;
  167. long ret;
  168. struct kvm *kvm = vcpu->kvm;
  169. psize = 1ul << porder;
  170. npages = memslot->npages >> (porder - PAGE_SHIFT);
  171. /* VRMA can't be > 1TB */
  172. if (npages > 1ul << (40 - porder))
  173. npages = 1ul << (40 - porder);
  174. /* Can't use more than 1 HPTE per HPTEG */
  175. if (npages > kvm->arch.hpt_mask + 1)
  176. npages = kvm->arch.hpt_mask + 1;
  177. hp0 = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
  178. HPTE_V_BOLTED | hpte0_pgsize_encoding(psize);
  179. hp1 = hpte1_pgsize_encoding(psize) |
  180. HPTE_R_R | HPTE_R_C | HPTE_R_M | PP_RWXX;
  181. for (i = 0; i < npages; ++i) {
  182. addr = i << porder;
  183. /* can't use hpt_hash since va > 64 bits */
  184. hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & kvm->arch.hpt_mask;
  185. /*
  186. * We assume that the hash table is empty and no
  187. * vcpus are using it at this stage. Since we create
  188. * at most one HPTE per HPTEG, we just assume entry 7
  189. * is available and use it.
  190. */
  191. hash = (hash << 3) + 7;
  192. hp_v = hp0 | ((addr >> 16) & ~0x7fUL);
  193. hp_r = hp1 | addr;
  194. ret = kvmppc_virtmode_h_enter(vcpu, H_EXACT, hash, hp_v, hp_r);
  195. if (ret != H_SUCCESS) {
  196. pr_err("KVM: map_vrma at %lx failed, ret=%ld\n",
  197. addr, ret);
  198. break;
  199. }
  200. }
  201. }
  202. int kvmppc_mmu_hv_init(void)
  203. {
  204. unsigned long host_lpid, rsvd_lpid;
  205. if (!cpu_has_feature(CPU_FTR_HVMODE))
  206. return -EINVAL;
  207. /* POWER7 has 10-bit LPIDs, PPC970 and e500mc have 6-bit LPIDs */
  208. if (cpu_has_feature(CPU_FTR_ARCH_206)) {
  209. host_lpid = mfspr(SPRN_LPID); /* POWER7 */
  210. rsvd_lpid = LPID_RSVD;
  211. } else {
  212. host_lpid = 0; /* PPC970 */
  213. rsvd_lpid = MAX_LPID_970;
  214. }
  215. kvmppc_init_lpid(rsvd_lpid + 1);
  216. kvmppc_claim_lpid(host_lpid);
  217. /* rsvd_lpid is reserved for use in partition switching */
  218. kvmppc_claim_lpid(rsvd_lpid);
  219. return 0;
  220. }
  221. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  222. {
  223. }
  224. static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
  225. {
  226. kvmppc_set_msr(vcpu, MSR_SF | MSR_ME);
  227. }
  228. /*
  229. * This is called to get a reference to a guest page if there isn't
  230. * one already in the memslot->arch.slot_phys[] array.
  231. */
  232. static long kvmppc_get_guest_page(struct kvm *kvm, unsigned long gfn,
  233. struct kvm_memory_slot *memslot,
  234. unsigned long psize)
  235. {
  236. unsigned long start;
  237. long np, err;
  238. struct page *page, *hpage, *pages[1];
  239. unsigned long s, pgsize;
  240. unsigned long *physp;
  241. unsigned int is_io, got, pgorder;
  242. struct vm_area_struct *vma;
  243. unsigned long pfn, i, npages;
  244. physp = memslot->arch.slot_phys;
  245. if (!physp)
  246. return -EINVAL;
  247. if (physp[gfn - memslot->base_gfn])
  248. return 0;
  249. is_io = 0;
  250. got = 0;
  251. page = NULL;
  252. pgsize = psize;
  253. err = -EINVAL;
  254. start = gfn_to_hva_memslot(memslot, gfn);
  255. /* Instantiate and get the page we want access to */
  256. np = get_user_pages_fast(start, 1, 1, pages);
  257. if (np != 1) {
  258. /* Look up the vma for the page */
  259. down_read(&current->mm->mmap_sem);
  260. vma = find_vma(current->mm, start);
  261. if (!vma || vma->vm_start > start ||
  262. start + psize > vma->vm_end ||
  263. !(vma->vm_flags & VM_PFNMAP))
  264. goto up_err;
  265. is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
  266. pfn = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT);
  267. /* check alignment of pfn vs. requested page size */
  268. if (psize > PAGE_SIZE && (pfn & ((psize >> PAGE_SHIFT) - 1)))
  269. goto up_err;
  270. up_read(&current->mm->mmap_sem);
  271. } else {
  272. page = pages[0];
  273. got = KVMPPC_GOT_PAGE;
  274. /* See if this is a large page */
  275. s = PAGE_SIZE;
  276. if (PageHuge(page)) {
  277. hpage = compound_head(page);
  278. s <<= compound_order(hpage);
  279. /* Get the whole large page if slot alignment is ok */
  280. if (s > psize && slot_is_aligned(memslot, s) &&
  281. !(memslot->userspace_addr & (s - 1))) {
  282. start &= ~(s - 1);
  283. pgsize = s;
  284. get_page(hpage);
  285. put_page(page);
  286. page = hpage;
  287. }
  288. }
  289. if (s < psize)
  290. goto out;
  291. pfn = page_to_pfn(page);
  292. }
  293. npages = pgsize >> PAGE_SHIFT;
  294. pgorder = __ilog2(npages);
  295. physp += (gfn - memslot->base_gfn) & ~(npages - 1);
  296. spin_lock(&kvm->arch.slot_phys_lock);
  297. for (i = 0; i < npages; ++i) {
  298. if (!physp[i]) {
  299. physp[i] = ((pfn + i) << PAGE_SHIFT) +
  300. got + is_io + pgorder;
  301. got = 0;
  302. }
  303. }
  304. spin_unlock(&kvm->arch.slot_phys_lock);
  305. err = 0;
  306. out:
  307. if (got)
  308. put_page(page);
  309. return err;
  310. up_err:
  311. up_read(&current->mm->mmap_sem);
  312. return err;
  313. }
  314. /*
  315. * We come here on a H_ENTER call from the guest when we are not
  316. * using mmu notifiers and we don't have the requested page pinned
  317. * already.
  318. */
  319. long kvmppc_virtmode_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
  320. long pte_index, unsigned long pteh, unsigned long ptel)
  321. {
  322. struct kvm *kvm = vcpu->kvm;
  323. unsigned long psize, gpa, gfn;
  324. struct kvm_memory_slot *memslot;
  325. long ret;
  326. if (kvm->arch.using_mmu_notifiers)
  327. goto do_insert;
  328. psize = hpte_page_size(pteh, ptel);
  329. if (!psize)
  330. return H_PARAMETER;
  331. pteh &= ~(HPTE_V_HVLOCK | HPTE_V_ABSENT | HPTE_V_VALID);
  332. /* Find the memslot (if any) for this address */
  333. gpa = (ptel & HPTE_R_RPN) & ~(psize - 1);
  334. gfn = gpa >> PAGE_SHIFT;
  335. memslot = gfn_to_memslot(kvm, gfn);
  336. if (memslot && !(memslot->flags & KVM_MEMSLOT_INVALID)) {
  337. if (!slot_is_aligned(memslot, psize))
  338. return H_PARAMETER;
  339. if (kvmppc_get_guest_page(kvm, gfn, memslot, psize) < 0)
  340. return H_PARAMETER;
  341. }
  342. do_insert:
  343. /* Protect linux PTE lookup from page table destruction */
  344. rcu_read_lock_sched(); /* this disables preemption too */
  345. vcpu->arch.pgdir = current->mm->pgd;
  346. ret = kvmppc_h_enter(vcpu, flags, pte_index, pteh, ptel);
  347. rcu_read_unlock_sched();
  348. if (ret == H_TOO_HARD) {
  349. /* this can't happen */
  350. pr_err("KVM: Oops, kvmppc_h_enter returned too hard!\n");
  351. ret = H_RESOURCE; /* or something */
  352. }
  353. return ret;
  354. }
  355. static struct kvmppc_slb *kvmppc_mmu_book3s_hv_find_slbe(struct kvm_vcpu *vcpu,
  356. gva_t eaddr)
  357. {
  358. u64 mask;
  359. int i;
  360. for (i = 0; i < vcpu->arch.slb_nr; i++) {
  361. if (!(vcpu->arch.slb[i].orige & SLB_ESID_V))
  362. continue;
  363. if (vcpu->arch.slb[i].origv & SLB_VSID_B_1T)
  364. mask = ESID_MASK_1T;
  365. else
  366. mask = ESID_MASK;
  367. if (((vcpu->arch.slb[i].orige ^ eaddr) & mask) == 0)
  368. return &vcpu->arch.slb[i];
  369. }
  370. return NULL;
  371. }
  372. static unsigned long kvmppc_mmu_get_real_addr(unsigned long v, unsigned long r,
  373. unsigned long ea)
  374. {
  375. unsigned long ra_mask;
  376. ra_mask = hpte_page_size(v, r) - 1;
  377. return (r & HPTE_R_RPN & ~ra_mask) | (ea & ra_mask);
  378. }
  379. static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
  380. struct kvmppc_pte *gpte, bool data)
  381. {
  382. struct kvm *kvm = vcpu->kvm;
  383. struct kvmppc_slb *slbe;
  384. unsigned long slb_v;
  385. unsigned long pp, key;
  386. unsigned long v, gr;
  387. unsigned long *hptep;
  388. int index;
  389. int virtmode = vcpu->arch.shregs.msr & (data ? MSR_DR : MSR_IR);
  390. /* Get SLB entry */
  391. if (virtmode) {
  392. slbe = kvmppc_mmu_book3s_hv_find_slbe(vcpu, eaddr);
  393. if (!slbe)
  394. return -EINVAL;
  395. slb_v = slbe->origv;
  396. } else {
  397. /* real mode access */
  398. slb_v = vcpu->kvm->arch.vrma_slb_v;
  399. }
  400. /* Find the HPTE in the hash table */
  401. index = kvmppc_hv_find_lock_hpte(kvm, eaddr, slb_v,
  402. HPTE_V_VALID | HPTE_V_ABSENT);
  403. if (index < 0)
  404. return -ENOENT;
  405. hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
  406. v = hptep[0] & ~HPTE_V_HVLOCK;
  407. gr = kvm->arch.revmap[index].guest_rpte;
  408. /* Unlock the HPTE */
  409. asm volatile("lwsync" : : : "memory");
  410. hptep[0] = v;
  411. gpte->eaddr = eaddr;
  412. gpte->vpage = ((v & HPTE_V_AVPN) << 4) | ((eaddr >> 12) & 0xfff);
  413. /* Get PP bits and key for permission check */
  414. pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
  415. key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
  416. key &= slb_v;
  417. /* Calculate permissions */
  418. gpte->may_read = hpte_read_permission(pp, key);
  419. gpte->may_write = hpte_write_permission(pp, key);
  420. gpte->may_execute = gpte->may_read && !(gr & (HPTE_R_N | HPTE_R_G));
  421. /* Storage key permission check for POWER7 */
  422. if (data && virtmode && cpu_has_feature(CPU_FTR_ARCH_206)) {
  423. int amrfield = hpte_get_skey_perm(gr, vcpu->arch.amr);
  424. if (amrfield & 1)
  425. gpte->may_read = 0;
  426. if (amrfield & 2)
  427. gpte->may_write = 0;
  428. }
  429. /* Get the guest physical address */
  430. gpte->raddr = kvmppc_mmu_get_real_addr(v, gr, eaddr);
  431. return 0;
  432. }
  433. /*
  434. * Quick test for whether an instruction is a load or a store.
  435. * If the instruction is a load or a store, then this will indicate
  436. * which it is, at least on server processors. (Embedded processors
  437. * have some external PID instructions that don't follow the rule
  438. * embodied here.) If the instruction isn't a load or store, then
  439. * this doesn't return anything useful.
  440. */
  441. static int instruction_is_store(unsigned int instr)
  442. {
  443. unsigned int mask;
  444. mask = 0x10000000;
  445. if ((instr & 0xfc000000) == 0x7c000000)
  446. mask = 0x100; /* major opcode 31 */
  447. return (instr & mask) != 0;
  448. }
  449. static int kvmppc_hv_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu,
  450. unsigned long gpa, gva_t ea, int is_store)
  451. {
  452. int ret;
  453. u32 last_inst;
  454. unsigned long srr0 = kvmppc_get_pc(vcpu);
  455. /* We try to load the last instruction. We don't let
  456. * emulate_instruction do it as it doesn't check what
  457. * kvmppc_ld returns.
  458. * If we fail, we just return to the guest and try executing it again.
  459. */
  460. if (vcpu->arch.last_inst == KVM_INST_FETCH_FAILED) {
  461. ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
  462. if (ret != EMULATE_DONE || last_inst == KVM_INST_FETCH_FAILED)
  463. return RESUME_GUEST;
  464. vcpu->arch.last_inst = last_inst;
  465. }
  466. /*
  467. * WARNING: We do not know for sure whether the instruction we just
  468. * read from memory is the same that caused the fault in the first
  469. * place. If the instruction we read is neither an load or a store,
  470. * then it can't access memory, so we don't need to worry about
  471. * enforcing access permissions. So, assuming it is a load or
  472. * store, we just check that its direction (load or store) is
  473. * consistent with the original fault, since that's what we
  474. * checked the access permissions against. If there is a mismatch
  475. * we just return and retry the instruction.
  476. */
  477. if (instruction_is_store(vcpu->arch.last_inst) != !!is_store)
  478. return RESUME_GUEST;
  479. /*
  480. * Emulated accesses are emulated by looking at the hash for
  481. * translation once, then performing the access later. The
  482. * translation could be invalidated in the meantime in which
  483. * point performing the subsequent memory access on the old
  484. * physical address could possibly be a security hole for the
  485. * guest (but not the host).
  486. *
  487. * This is less of an issue for MMIO stores since they aren't
  488. * globally visible. It could be an issue for MMIO loads to
  489. * a certain extent but we'll ignore it for now.
  490. */
  491. vcpu->arch.paddr_accessed = gpa;
  492. vcpu->arch.vaddr_accessed = ea;
  493. return kvmppc_emulate_mmio(run, vcpu);
  494. }
  495. int kvmppc_book3s_hv_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
  496. unsigned long ea, unsigned long dsisr)
  497. {
  498. struct kvm *kvm = vcpu->kvm;
  499. unsigned long *hptep, hpte[3], r;
  500. unsigned long mmu_seq, psize, pte_size;
  501. unsigned long gpa, gfn, hva, pfn;
  502. struct kvm_memory_slot *memslot;
  503. unsigned long *rmap;
  504. struct revmap_entry *rev;
  505. struct page *page, *pages[1];
  506. long index, ret, npages;
  507. unsigned long is_io;
  508. unsigned int writing, write_ok;
  509. struct vm_area_struct *vma;
  510. unsigned long rcbits;
  511. /*
  512. * Real-mode code has already searched the HPT and found the
  513. * entry we're interested in. Lock the entry and check that
  514. * it hasn't changed. If it has, just return and re-execute the
  515. * instruction.
  516. */
  517. if (ea != vcpu->arch.pgfault_addr)
  518. return RESUME_GUEST;
  519. index = vcpu->arch.pgfault_index;
  520. hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
  521. rev = &kvm->arch.revmap[index];
  522. preempt_disable();
  523. while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
  524. cpu_relax();
  525. hpte[0] = hptep[0] & ~HPTE_V_HVLOCK;
  526. hpte[1] = hptep[1];
  527. hpte[2] = r = rev->guest_rpte;
  528. asm volatile("lwsync" : : : "memory");
  529. hptep[0] = hpte[0];
  530. preempt_enable();
  531. if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
  532. hpte[1] != vcpu->arch.pgfault_hpte[1])
  533. return RESUME_GUEST;
  534. /* Translate the logical address and get the page */
  535. psize = hpte_page_size(hpte[0], r);
  536. gpa = (r & HPTE_R_RPN & ~(psize - 1)) | (ea & (psize - 1));
  537. gfn = gpa >> PAGE_SHIFT;
  538. memslot = gfn_to_memslot(kvm, gfn);
  539. /* No memslot means it's an emulated MMIO region */
  540. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  541. return kvmppc_hv_emulate_mmio(run, vcpu, gpa, ea,
  542. dsisr & DSISR_ISSTORE);
  543. if (!kvm->arch.using_mmu_notifiers)
  544. return -EFAULT; /* should never get here */
  545. /* used to check for invalidations in progress */
  546. mmu_seq = kvm->mmu_notifier_seq;
  547. smp_rmb();
  548. is_io = 0;
  549. pfn = 0;
  550. page = NULL;
  551. pte_size = PAGE_SIZE;
  552. writing = (dsisr & DSISR_ISSTORE) != 0;
  553. /* If writing != 0, then the HPTE must allow writing, if we get here */
  554. write_ok = writing;
  555. hva = gfn_to_hva_memslot(memslot, gfn);
  556. npages = get_user_pages_fast(hva, 1, writing, pages);
  557. if (npages < 1) {
  558. /* Check if it's an I/O mapping */
  559. down_read(&current->mm->mmap_sem);
  560. vma = find_vma(current->mm, hva);
  561. if (vma && vma->vm_start <= hva && hva + psize <= vma->vm_end &&
  562. (vma->vm_flags & VM_PFNMAP)) {
  563. pfn = vma->vm_pgoff +
  564. ((hva - vma->vm_start) >> PAGE_SHIFT);
  565. pte_size = psize;
  566. is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
  567. write_ok = vma->vm_flags & VM_WRITE;
  568. }
  569. up_read(&current->mm->mmap_sem);
  570. if (!pfn)
  571. return -EFAULT;
  572. } else {
  573. page = pages[0];
  574. if (PageHuge(page)) {
  575. page = compound_head(page);
  576. pte_size <<= compound_order(page);
  577. }
  578. /* if the guest wants write access, see if that is OK */
  579. if (!writing && hpte_is_writable(r)) {
  580. pte_t *ptep, pte;
  581. /*
  582. * We need to protect against page table destruction
  583. * while looking up and updating the pte.
  584. */
  585. rcu_read_lock_sched();
  586. ptep = find_linux_pte_or_hugepte(current->mm->pgd,
  587. hva, NULL);
  588. if (ptep && pte_present(*ptep)) {
  589. pte = kvmppc_read_update_linux_pte(ptep, 1);
  590. if (pte_write(pte))
  591. write_ok = 1;
  592. }
  593. rcu_read_unlock_sched();
  594. }
  595. pfn = page_to_pfn(page);
  596. }
  597. ret = -EFAULT;
  598. if (psize > pte_size)
  599. goto out_put;
  600. /* Check WIMG vs. the actual page we're accessing */
  601. if (!hpte_cache_flags_ok(r, is_io)) {
  602. if (is_io)
  603. return -EFAULT;
  604. /*
  605. * Allow guest to map emulated device memory as
  606. * uncacheable, but actually make it cacheable.
  607. */
  608. r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M;
  609. }
  610. /* Set the HPTE to point to pfn */
  611. r = (r & ~(HPTE_R_PP0 - pte_size)) | (pfn << PAGE_SHIFT);
  612. if (hpte_is_writable(r) && !write_ok)
  613. r = hpte_make_readonly(r);
  614. ret = RESUME_GUEST;
  615. preempt_disable();
  616. while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
  617. cpu_relax();
  618. if ((hptep[0] & ~HPTE_V_HVLOCK) != hpte[0] || hptep[1] != hpte[1] ||
  619. rev->guest_rpte != hpte[2])
  620. /* HPTE has been changed under us; let the guest retry */
  621. goto out_unlock;
  622. hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
  623. rmap = &memslot->arch.rmap[gfn - memslot->base_gfn];
  624. lock_rmap(rmap);
  625. /* Check if we might have been invalidated; let the guest retry if so */
  626. ret = RESUME_GUEST;
  627. if (mmu_notifier_retry(vcpu->kvm, mmu_seq)) {
  628. unlock_rmap(rmap);
  629. goto out_unlock;
  630. }
  631. /* Only set R/C in real HPTE if set in both *rmap and guest_rpte */
  632. rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
  633. r &= rcbits | ~(HPTE_R_R | HPTE_R_C);
  634. if (hptep[0] & HPTE_V_VALID) {
  635. /* HPTE was previously valid, so we need to invalidate it */
  636. unlock_rmap(rmap);
  637. hptep[0] |= HPTE_V_ABSENT;
  638. kvmppc_invalidate_hpte(kvm, hptep, index);
  639. /* don't lose previous R and C bits */
  640. r |= hptep[1] & (HPTE_R_R | HPTE_R_C);
  641. } else {
  642. kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
  643. }
  644. hptep[1] = r;
  645. eieio();
  646. hptep[0] = hpte[0];
  647. asm volatile("ptesync" : : : "memory");
  648. preempt_enable();
  649. if (page && hpte_is_writable(r))
  650. SetPageDirty(page);
  651. out_put:
  652. if (page) {
  653. /*
  654. * We drop pages[0] here, not page because page might
  655. * have been set to the head page of a compound, but
  656. * we have to drop the reference on the correct tail
  657. * page to match the get inside gup()
  658. */
  659. put_page(pages[0]);
  660. }
  661. return ret;
  662. out_unlock:
  663. hptep[0] &= ~HPTE_V_HVLOCK;
  664. preempt_enable();
  665. goto out_put;
  666. }
  667. static int kvm_handle_hva_range(struct kvm *kvm,
  668. unsigned long start,
  669. unsigned long end,
  670. int (*handler)(struct kvm *kvm,
  671. unsigned long *rmapp,
  672. unsigned long gfn))
  673. {
  674. int ret;
  675. int retval = 0;
  676. struct kvm_memslots *slots;
  677. struct kvm_memory_slot *memslot;
  678. slots = kvm_memslots(kvm);
  679. kvm_for_each_memslot(memslot, slots) {
  680. unsigned long hva_start, hva_end;
  681. gfn_t gfn, gfn_end;
  682. hva_start = max(start, memslot->userspace_addr);
  683. hva_end = min(end, memslot->userspace_addr +
  684. (memslot->npages << PAGE_SHIFT));
  685. if (hva_start >= hva_end)
  686. continue;
  687. /*
  688. * {gfn(page) | page intersects with [hva_start, hva_end)} =
  689. * {gfn, gfn+1, ..., gfn_end-1}.
  690. */
  691. gfn = hva_to_gfn_memslot(hva_start, memslot);
  692. gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot);
  693. for (; gfn < gfn_end; ++gfn) {
  694. gfn_t gfn_offset = gfn - memslot->base_gfn;
  695. ret = handler(kvm, &memslot->arch.rmap[gfn_offset], gfn);
  696. retval |= ret;
  697. }
  698. }
  699. return retval;
  700. }
  701. static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
  702. int (*handler)(struct kvm *kvm, unsigned long *rmapp,
  703. unsigned long gfn))
  704. {
  705. return kvm_handle_hva_range(kvm, hva, hva + 1, handler);
  706. }
  707. static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
  708. unsigned long gfn)
  709. {
  710. struct revmap_entry *rev = kvm->arch.revmap;
  711. unsigned long h, i, j;
  712. unsigned long *hptep;
  713. unsigned long ptel, psize, rcbits;
  714. for (;;) {
  715. lock_rmap(rmapp);
  716. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  717. unlock_rmap(rmapp);
  718. break;
  719. }
  720. /*
  721. * To avoid an ABBA deadlock with the HPTE lock bit,
  722. * we can't spin on the HPTE lock while holding the
  723. * rmap chain lock.
  724. */
  725. i = *rmapp & KVMPPC_RMAP_INDEX;
  726. hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
  727. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  728. /* unlock rmap before spinning on the HPTE lock */
  729. unlock_rmap(rmapp);
  730. while (hptep[0] & HPTE_V_HVLOCK)
  731. cpu_relax();
  732. continue;
  733. }
  734. j = rev[i].forw;
  735. if (j == i) {
  736. /* chain is now empty */
  737. *rmapp &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
  738. } else {
  739. /* remove i from chain */
  740. h = rev[i].back;
  741. rev[h].forw = j;
  742. rev[j].back = h;
  743. rev[i].forw = rev[i].back = i;
  744. *rmapp = (*rmapp & ~KVMPPC_RMAP_INDEX) | j;
  745. }
  746. /* Now check and modify the HPTE */
  747. ptel = rev[i].guest_rpte;
  748. psize = hpte_page_size(hptep[0], ptel);
  749. if ((hptep[0] & HPTE_V_VALID) &&
  750. hpte_rpn(ptel, psize) == gfn) {
  751. if (kvm->arch.using_mmu_notifiers)
  752. hptep[0] |= HPTE_V_ABSENT;
  753. kvmppc_invalidate_hpte(kvm, hptep, i);
  754. /* Harvest R and C */
  755. rcbits = hptep[1] & (HPTE_R_R | HPTE_R_C);
  756. *rmapp |= rcbits << KVMPPC_RMAP_RC_SHIFT;
  757. rev[i].guest_rpte = ptel | rcbits;
  758. }
  759. unlock_rmap(rmapp);
  760. hptep[0] &= ~HPTE_V_HVLOCK;
  761. }
  762. return 0;
  763. }
  764. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  765. {
  766. if (kvm->arch.using_mmu_notifiers)
  767. kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
  768. return 0;
  769. }
  770. int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end)
  771. {
  772. if (kvm->arch.using_mmu_notifiers)
  773. kvm_handle_hva_range(kvm, start, end, kvm_unmap_rmapp);
  774. return 0;
  775. }
  776. void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
  777. {
  778. unsigned long *rmapp;
  779. unsigned long gfn;
  780. unsigned long n;
  781. rmapp = memslot->arch.rmap;
  782. gfn = memslot->base_gfn;
  783. for (n = memslot->npages; n; --n) {
  784. /*
  785. * Testing the present bit without locking is OK because
  786. * the memslot has been marked invalid already, and hence
  787. * no new HPTEs referencing this page can be created,
  788. * thus the present bit can't go from 0 to 1.
  789. */
  790. if (*rmapp & KVMPPC_RMAP_PRESENT)
  791. kvm_unmap_rmapp(kvm, rmapp, gfn);
  792. ++rmapp;
  793. ++gfn;
  794. }
  795. }
  796. static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
  797. unsigned long gfn)
  798. {
  799. struct revmap_entry *rev = kvm->arch.revmap;
  800. unsigned long head, i, j;
  801. unsigned long *hptep;
  802. int ret = 0;
  803. retry:
  804. lock_rmap(rmapp);
  805. if (*rmapp & KVMPPC_RMAP_REFERENCED) {
  806. *rmapp &= ~KVMPPC_RMAP_REFERENCED;
  807. ret = 1;
  808. }
  809. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  810. unlock_rmap(rmapp);
  811. return ret;
  812. }
  813. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  814. do {
  815. hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
  816. j = rev[i].forw;
  817. /* If this HPTE isn't referenced, ignore it */
  818. if (!(hptep[1] & HPTE_R_R))
  819. continue;
  820. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  821. /* unlock rmap before spinning on the HPTE lock */
  822. unlock_rmap(rmapp);
  823. while (hptep[0] & HPTE_V_HVLOCK)
  824. cpu_relax();
  825. goto retry;
  826. }
  827. /* Now check and modify the HPTE */
  828. if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_R)) {
  829. kvmppc_clear_ref_hpte(kvm, hptep, i);
  830. rev[i].guest_rpte |= HPTE_R_R;
  831. ret = 1;
  832. }
  833. hptep[0] &= ~HPTE_V_HVLOCK;
  834. } while ((i = j) != head);
  835. unlock_rmap(rmapp);
  836. return ret;
  837. }
  838. int kvm_age_hva(struct kvm *kvm, unsigned long hva)
  839. {
  840. if (!kvm->arch.using_mmu_notifiers)
  841. return 0;
  842. return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
  843. }
  844. static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
  845. unsigned long gfn)
  846. {
  847. struct revmap_entry *rev = kvm->arch.revmap;
  848. unsigned long head, i, j;
  849. unsigned long *hp;
  850. int ret = 1;
  851. if (*rmapp & KVMPPC_RMAP_REFERENCED)
  852. return 1;
  853. lock_rmap(rmapp);
  854. if (*rmapp & KVMPPC_RMAP_REFERENCED)
  855. goto out;
  856. if (*rmapp & KVMPPC_RMAP_PRESENT) {
  857. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  858. do {
  859. hp = (unsigned long *)(kvm->arch.hpt_virt + (i << 4));
  860. j = rev[i].forw;
  861. if (hp[1] & HPTE_R_R)
  862. goto out;
  863. } while ((i = j) != head);
  864. }
  865. ret = 0;
  866. out:
  867. unlock_rmap(rmapp);
  868. return ret;
  869. }
  870. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  871. {
  872. if (!kvm->arch.using_mmu_notifiers)
  873. return 0;
  874. return kvm_handle_hva(kvm, hva, kvm_test_age_rmapp);
  875. }
  876. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  877. {
  878. if (!kvm->arch.using_mmu_notifiers)
  879. return;
  880. kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
  881. }
  882. static int kvm_test_clear_dirty(struct kvm *kvm, unsigned long *rmapp)
  883. {
  884. struct revmap_entry *rev = kvm->arch.revmap;
  885. unsigned long head, i, j;
  886. unsigned long *hptep;
  887. int ret = 0;
  888. retry:
  889. lock_rmap(rmapp);
  890. if (*rmapp & KVMPPC_RMAP_CHANGED) {
  891. *rmapp &= ~KVMPPC_RMAP_CHANGED;
  892. ret = 1;
  893. }
  894. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  895. unlock_rmap(rmapp);
  896. return ret;
  897. }
  898. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  899. do {
  900. hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
  901. j = rev[i].forw;
  902. if (!(hptep[1] & HPTE_R_C))
  903. continue;
  904. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  905. /* unlock rmap before spinning on the HPTE lock */
  906. unlock_rmap(rmapp);
  907. while (hptep[0] & HPTE_V_HVLOCK)
  908. cpu_relax();
  909. goto retry;
  910. }
  911. /* Now check and modify the HPTE */
  912. if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_C)) {
  913. /* need to make it temporarily absent to clear C */
  914. hptep[0] |= HPTE_V_ABSENT;
  915. kvmppc_invalidate_hpte(kvm, hptep, i);
  916. hptep[1] &= ~HPTE_R_C;
  917. eieio();
  918. hptep[0] = (hptep[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
  919. rev[i].guest_rpte |= HPTE_R_C;
  920. ret = 1;
  921. }
  922. hptep[0] &= ~HPTE_V_HVLOCK;
  923. } while ((i = j) != head);
  924. unlock_rmap(rmapp);
  925. return ret;
  926. }
  927. long kvmppc_hv_get_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot,
  928. unsigned long *map)
  929. {
  930. unsigned long i;
  931. unsigned long *rmapp;
  932. preempt_disable();
  933. rmapp = memslot->arch.rmap;
  934. for (i = 0; i < memslot->npages; ++i) {
  935. if (kvm_test_clear_dirty(kvm, rmapp) && map)
  936. __set_bit_le(i, map);
  937. ++rmapp;
  938. }
  939. preempt_enable();
  940. return 0;
  941. }
  942. void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
  943. unsigned long *nb_ret)
  944. {
  945. struct kvm_memory_slot *memslot;
  946. unsigned long gfn = gpa >> PAGE_SHIFT;
  947. struct page *page, *pages[1];
  948. int npages;
  949. unsigned long hva, psize, offset;
  950. unsigned long pa;
  951. unsigned long *physp;
  952. int srcu_idx;
  953. srcu_idx = srcu_read_lock(&kvm->srcu);
  954. memslot = gfn_to_memslot(kvm, gfn);
  955. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  956. goto err;
  957. if (!kvm->arch.using_mmu_notifiers) {
  958. physp = memslot->arch.slot_phys;
  959. if (!physp)
  960. goto err;
  961. physp += gfn - memslot->base_gfn;
  962. pa = *physp;
  963. if (!pa) {
  964. if (kvmppc_get_guest_page(kvm, gfn, memslot,
  965. PAGE_SIZE) < 0)
  966. goto err;
  967. pa = *physp;
  968. }
  969. page = pfn_to_page(pa >> PAGE_SHIFT);
  970. get_page(page);
  971. } else {
  972. hva = gfn_to_hva_memslot(memslot, gfn);
  973. npages = get_user_pages_fast(hva, 1, 1, pages);
  974. if (npages < 1)
  975. goto err;
  976. page = pages[0];
  977. }
  978. srcu_read_unlock(&kvm->srcu, srcu_idx);
  979. psize = PAGE_SIZE;
  980. if (PageHuge(page)) {
  981. page = compound_head(page);
  982. psize <<= compound_order(page);
  983. }
  984. offset = gpa & (psize - 1);
  985. if (nb_ret)
  986. *nb_ret = psize - offset;
  987. return page_address(page) + offset;
  988. err:
  989. srcu_read_unlock(&kvm->srcu, srcu_idx);
  990. return NULL;
  991. }
  992. void kvmppc_unpin_guest_page(struct kvm *kvm, void *va)
  993. {
  994. struct page *page = virt_to_page(va);
  995. put_page(page);
  996. }
  997. void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
  998. {
  999. struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
  1000. if (cpu_has_feature(CPU_FTR_ARCH_206))
  1001. vcpu->arch.slb_nr = 32; /* POWER7 */
  1002. else
  1003. vcpu->arch.slb_nr = 64;
  1004. mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
  1005. mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
  1006. vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
  1007. }