book3s_64_mmu_hv.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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. /*
  443. * Real-mode code has already searched the HPT and found the
  444. * entry we're interested in. Lock the entry and check that
  445. * it hasn't changed. If it has, just return and re-execute the
  446. * instruction.
  447. */
  448. if (ea != vcpu->arch.pgfault_addr)
  449. return RESUME_GUEST;
  450. index = vcpu->arch.pgfault_index;
  451. hptep = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
  452. rev = &kvm->arch.revmap[index];
  453. preempt_disable();
  454. while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
  455. cpu_relax();
  456. hpte[0] = hptep[0] & ~HPTE_V_HVLOCK;
  457. hpte[1] = hptep[1];
  458. hpte[2] = r = rev->guest_rpte;
  459. asm volatile("lwsync" : : : "memory");
  460. hptep[0] = hpte[0];
  461. preempt_enable();
  462. if (hpte[0] != vcpu->arch.pgfault_hpte[0] ||
  463. hpte[1] != vcpu->arch.pgfault_hpte[1])
  464. return RESUME_GUEST;
  465. /* Translate the logical address and get the page */
  466. psize = hpte_page_size(hpte[0], r);
  467. gfn = hpte_rpn(r, psize);
  468. memslot = gfn_to_memslot(kvm, gfn);
  469. /* No memslot means it's an emulated MMIO region */
  470. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) {
  471. unsigned long gpa = (gfn << PAGE_SHIFT) | (ea & (psize - 1));
  472. return kvmppc_hv_emulate_mmio(run, vcpu, gpa,
  473. dsisr & DSISR_ISSTORE);
  474. }
  475. if (!kvm->arch.using_mmu_notifiers)
  476. return -EFAULT; /* should never get here */
  477. /* used to check for invalidations in progress */
  478. mmu_seq = kvm->mmu_notifier_seq;
  479. smp_rmb();
  480. is_io = 0;
  481. pfn = 0;
  482. page = NULL;
  483. pte_size = PAGE_SIZE;
  484. writing = (dsisr & DSISR_ISSTORE) != 0;
  485. /* If writing != 0, then the HPTE must allow writing, if we get here */
  486. write_ok = writing;
  487. hva = gfn_to_hva_memslot(memslot, gfn);
  488. npages = get_user_pages_fast(hva, 1, writing, pages);
  489. if (npages < 1) {
  490. /* Check if it's an I/O mapping */
  491. down_read(&current->mm->mmap_sem);
  492. vma = find_vma(current->mm, hva);
  493. if (vma && vma->vm_start <= hva && hva + psize <= vma->vm_end &&
  494. (vma->vm_flags & VM_PFNMAP)) {
  495. pfn = vma->vm_pgoff +
  496. ((hva - vma->vm_start) >> PAGE_SHIFT);
  497. pte_size = psize;
  498. is_io = hpte_cache_bits(pgprot_val(vma->vm_page_prot));
  499. write_ok = vma->vm_flags & VM_WRITE;
  500. }
  501. up_read(&current->mm->mmap_sem);
  502. if (!pfn)
  503. return -EFAULT;
  504. } else {
  505. page = pages[0];
  506. if (PageHuge(page)) {
  507. page = compound_head(page);
  508. pte_size <<= compound_order(page);
  509. }
  510. /* if the guest wants write access, see if that is OK */
  511. if (!writing && hpte_is_writable(r)) {
  512. pte_t *ptep, pte;
  513. /*
  514. * We need to protect against page table destruction
  515. * while looking up and updating the pte.
  516. */
  517. rcu_read_lock_sched();
  518. ptep = find_linux_pte_or_hugepte(current->mm->pgd,
  519. hva, NULL);
  520. if (ptep && pte_present(*ptep)) {
  521. pte = kvmppc_read_update_linux_pte(ptep, 1);
  522. if (pte_write(pte))
  523. write_ok = 1;
  524. }
  525. rcu_read_unlock_sched();
  526. }
  527. pfn = page_to_pfn(page);
  528. }
  529. ret = -EFAULT;
  530. if (psize > pte_size)
  531. goto out_put;
  532. /* Check WIMG vs. the actual page we're accessing */
  533. if (!hpte_cache_flags_ok(r, is_io)) {
  534. if (is_io)
  535. return -EFAULT;
  536. /*
  537. * Allow guest to map emulated device memory as
  538. * uncacheable, but actually make it cacheable.
  539. */
  540. r = (r & ~(HPTE_R_W|HPTE_R_I|HPTE_R_G)) | HPTE_R_M;
  541. }
  542. /* Set the HPTE to point to pfn */
  543. r = (r & ~(HPTE_R_PP0 - pte_size)) | (pfn << PAGE_SHIFT);
  544. if (hpte_is_writable(r) && !write_ok)
  545. r = hpte_make_readonly(r);
  546. ret = RESUME_GUEST;
  547. preempt_disable();
  548. while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
  549. cpu_relax();
  550. if ((hptep[0] & ~HPTE_V_HVLOCK) != hpte[0] || hptep[1] != hpte[1] ||
  551. rev->guest_rpte != hpte[2])
  552. /* HPTE has been changed under us; let the guest retry */
  553. goto out_unlock;
  554. hpte[0] = (hpte[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
  555. rmap = &memslot->rmap[gfn - memslot->base_gfn];
  556. lock_rmap(rmap);
  557. /* Check if we might have been invalidated; let the guest retry if so */
  558. ret = RESUME_GUEST;
  559. if (mmu_notifier_retry(vcpu, mmu_seq)) {
  560. unlock_rmap(rmap);
  561. goto out_unlock;
  562. }
  563. if (hptep[0] & HPTE_V_VALID) {
  564. /* HPTE was previously valid, so we need to invalidate it */
  565. unlock_rmap(rmap);
  566. hptep[0] |= HPTE_V_ABSENT;
  567. kvmppc_invalidate_hpte(kvm, hptep, index);
  568. } else {
  569. kvmppc_add_revmap_chain(kvm, rev, rmap, index, 0);
  570. }
  571. hptep[1] = r;
  572. eieio();
  573. hptep[0] = hpte[0];
  574. asm volatile("ptesync" : : : "memory");
  575. preempt_enable();
  576. if (page && hpte_is_writable(r))
  577. SetPageDirty(page);
  578. out_put:
  579. if (page)
  580. put_page(page);
  581. return ret;
  582. out_unlock:
  583. hptep[0] &= ~HPTE_V_HVLOCK;
  584. preempt_enable();
  585. goto out_put;
  586. }
  587. static int kvm_handle_hva(struct kvm *kvm, unsigned long hva,
  588. int (*handler)(struct kvm *kvm, unsigned long *rmapp,
  589. unsigned long gfn))
  590. {
  591. int ret;
  592. int retval = 0;
  593. struct kvm_memslots *slots;
  594. struct kvm_memory_slot *memslot;
  595. slots = kvm_memslots(kvm);
  596. kvm_for_each_memslot(memslot, slots) {
  597. unsigned long start = memslot->userspace_addr;
  598. unsigned long end;
  599. end = start + (memslot->npages << PAGE_SHIFT);
  600. if (hva >= start && hva < end) {
  601. gfn_t gfn_offset = (hva - start) >> PAGE_SHIFT;
  602. ret = handler(kvm, &memslot->rmap[gfn_offset],
  603. memslot->base_gfn + gfn_offset);
  604. retval |= ret;
  605. }
  606. }
  607. return retval;
  608. }
  609. static int kvm_unmap_rmapp(struct kvm *kvm, unsigned long *rmapp,
  610. unsigned long gfn)
  611. {
  612. struct revmap_entry *rev = kvm->arch.revmap;
  613. unsigned long h, i, j;
  614. unsigned long *hptep;
  615. unsigned long ptel, psize;
  616. for (;;) {
  617. while (test_and_set_bit_lock(KVMPPC_RMAP_LOCK_BIT, rmapp))
  618. cpu_relax();
  619. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  620. __clear_bit_unlock(KVMPPC_RMAP_LOCK_BIT, rmapp);
  621. break;
  622. }
  623. /*
  624. * To avoid an ABBA deadlock with the HPTE lock bit,
  625. * we have to unlock the rmap chain before locking the HPTE.
  626. * Thus we remove the first entry, unlock the rmap chain,
  627. * lock the HPTE and then check that it is for the
  628. * page we're unmapping before changing it to non-present.
  629. */
  630. i = *rmapp & KVMPPC_RMAP_INDEX;
  631. j = rev[i].forw;
  632. if (j == i) {
  633. /* chain is now empty */
  634. j = 0;
  635. } else {
  636. /* remove i from chain */
  637. h = rev[i].back;
  638. rev[h].forw = j;
  639. rev[j].back = h;
  640. rev[i].forw = rev[i].back = i;
  641. j |= KVMPPC_RMAP_PRESENT;
  642. }
  643. smp_wmb();
  644. *rmapp = j | (1ul << KVMPPC_RMAP_REF_BIT);
  645. /* Now lock, check and modify the HPTE */
  646. hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
  647. while (!try_lock_hpte(hptep, HPTE_V_HVLOCK))
  648. cpu_relax();
  649. ptel = rev[i].guest_rpte;
  650. psize = hpte_page_size(hptep[0], ptel);
  651. if ((hptep[0] & HPTE_V_VALID) &&
  652. hpte_rpn(ptel, psize) == gfn) {
  653. kvmppc_invalidate_hpte(kvm, hptep, i);
  654. hptep[0] |= HPTE_V_ABSENT;
  655. }
  656. hptep[0] &= ~HPTE_V_HVLOCK;
  657. }
  658. return 0;
  659. }
  660. int kvm_unmap_hva(struct kvm *kvm, unsigned long hva)
  661. {
  662. if (kvm->arch.using_mmu_notifiers)
  663. kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
  664. return 0;
  665. }
  666. static int kvm_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
  667. unsigned long gfn)
  668. {
  669. if (!kvm->arch.using_mmu_notifiers)
  670. return 0;
  671. if (!(*rmapp & KVMPPC_RMAP_REFERENCED))
  672. return 0;
  673. kvm_unmap_rmapp(kvm, rmapp, gfn);
  674. while (test_and_set_bit_lock(KVMPPC_RMAP_LOCK_BIT, rmapp))
  675. cpu_relax();
  676. __clear_bit(KVMPPC_RMAP_REF_BIT, rmapp);
  677. __clear_bit_unlock(KVMPPC_RMAP_LOCK_BIT, rmapp);
  678. return 1;
  679. }
  680. int kvm_age_hva(struct kvm *kvm, unsigned long hva)
  681. {
  682. if (!kvm->arch.using_mmu_notifiers)
  683. return 0;
  684. return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
  685. }
  686. static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
  687. unsigned long gfn)
  688. {
  689. return !!(*rmapp & KVMPPC_RMAP_REFERENCED);
  690. }
  691. int kvm_test_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_test_age_rmapp);
  696. }
  697. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  698. {
  699. if (!kvm->arch.using_mmu_notifiers)
  700. return;
  701. kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
  702. }
  703. void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
  704. unsigned long *nb_ret)
  705. {
  706. struct kvm_memory_slot *memslot;
  707. unsigned long gfn = gpa >> PAGE_SHIFT;
  708. struct page *page, *pages[1];
  709. int npages;
  710. unsigned long hva, psize, offset;
  711. unsigned long pa;
  712. unsigned long *physp;
  713. memslot = gfn_to_memslot(kvm, gfn);
  714. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  715. return NULL;
  716. if (!kvm->arch.using_mmu_notifiers) {
  717. physp = kvm->arch.slot_phys[memslot->id];
  718. if (!physp)
  719. return NULL;
  720. physp += gfn - memslot->base_gfn;
  721. pa = *physp;
  722. if (!pa) {
  723. if (kvmppc_get_guest_page(kvm, gfn, memslot,
  724. PAGE_SIZE) < 0)
  725. return NULL;
  726. pa = *physp;
  727. }
  728. page = pfn_to_page(pa >> PAGE_SHIFT);
  729. } else {
  730. hva = gfn_to_hva_memslot(memslot, gfn);
  731. npages = get_user_pages_fast(hva, 1, 1, pages);
  732. if (npages < 1)
  733. return NULL;
  734. page = pages[0];
  735. }
  736. psize = PAGE_SIZE;
  737. if (PageHuge(page)) {
  738. page = compound_head(page);
  739. psize <<= compound_order(page);
  740. }
  741. if (!kvm->arch.using_mmu_notifiers)
  742. get_page(page);
  743. offset = gpa & (psize - 1);
  744. if (nb_ret)
  745. *nb_ret = psize - offset;
  746. return page_address(page) + offset;
  747. }
  748. void kvmppc_unpin_guest_page(struct kvm *kvm, void *va)
  749. {
  750. struct page *page = virt_to_page(va);
  751. page = compound_head(page);
  752. put_page(page);
  753. }
  754. void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
  755. {
  756. struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
  757. if (cpu_has_feature(CPU_FTR_ARCH_206))
  758. vcpu->arch.slb_nr = 32; /* POWER7 */
  759. else
  760. vcpu->arch.slb_nr = 64;
  761. mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
  762. mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
  763. vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
  764. }