book3s_64_mmu_hv.c 26 KB

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