book3s_64_mmu_hv.c 23 KB

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