book3s_64_mmu_hv.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. struct revmap_entry *rev = kvm->arch.revmap;
  681. unsigned long head, i, j;
  682. unsigned long *hptep;
  683. int ret = 0;
  684. retry:
  685. lock_rmap(rmapp);
  686. if (*rmapp & KVMPPC_RMAP_REFERENCED) {
  687. *rmapp &= ~KVMPPC_RMAP_REFERENCED;
  688. ret = 1;
  689. }
  690. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  691. unlock_rmap(rmapp);
  692. return ret;
  693. }
  694. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  695. do {
  696. hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
  697. j = rev[i].forw;
  698. /* If this HPTE isn't referenced, ignore it */
  699. if (!(hptep[1] & HPTE_R_R))
  700. continue;
  701. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  702. /* unlock rmap before spinning on the HPTE lock */
  703. unlock_rmap(rmapp);
  704. while (hptep[0] & HPTE_V_HVLOCK)
  705. cpu_relax();
  706. goto retry;
  707. }
  708. /* Now check and modify the HPTE */
  709. if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_R)) {
  710. kvmppc_clear_ref_hpte(kvm, hptep, i);
  711. rev[i].guest_rpte |= HPTE_R_R;
  712. ret = 1;
  713. }
  714. hptep[0] &= ~HPTE_V_HVLOCK;
  715. } while ((i = j) != head);
  716. unlock_rmap(rmapp);
  717. return ret;
  718. }
  719. int kvm_age_hva(struct kvm *kvm, unsigned long hva)
  720. {
  721. if (!kvm->arch.using_mmu_notifiers)
  722. return 0;
  723. return kvm_handle_hva(kvm, hva, kvm_age_rmapp);
  724. }
  725. static int kvm_test_age_rmapp(struct kvm *kvm, unsigned long *rmapp,
  726. unsigned long gfn)
  727. {
  728. struct revmap_entry *rev = kvm->arch.revmap;
  729. unsigned long head, i, j;
  730. unsigned long *hp;
  731. int ret = 1;
  732. if (*rmapp & KVMPPC_RMAP_REFERENCED)
  733. return 1;
  734. lock_rmap(rmapp);
  735. if (*rmapp & KVMPPC_RMAP_REFERENCED)
  736. goto out;
  737. if (*rmapp & KVMPPC_RMAP_PRESENT) {
  738. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  739. do {
  740. hp = (unsigned long *)(kvm->arch.hpt_virt + (i << 4));
  741. j = rev[i].forw;
  742. if (hp[1] & HPTE_R_R)
  743. goto out;
  744. } while ((i = j) != head);
  745. }
  746. ret = 0;
  747. out:
  748. unlock_rmap(rmapp);
  749. return ret;
  750. }
  751. int kvm_test_age_hva(struct kvm *kvm, unsigned long hva)
  752. {
  753. if (!kvm->arch.using_mmu_notifiers)
  754. return 0;
  755. return kvm_handle_hva(kvm, hva, kvm_test_age_rmapp);
  756. }
  757. void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte)
  758. {
  759. if (!kvm->arch.using_mmu_notifiers)
  760. return;
  761. kvm_handle_hva(kvm, hva, kvm_unmap_rmapp);
  762. }
  763. static int kvm_test_clear_dirty(struct kvm *kvm, unsigned long *rmapp)
  764. {
  765. struct revmap_entry *rev = kvm->arch.revmap;
  766. unsigned long head, i, j;
  767. unsigned long *hptep;
  768. int ret = 0;
  769. retry:
  770. lock_rmap(rmapp);
  771. if (*rmapp & KVMPPC_RMAP_CHANGED) {
  772. *rmapp &= ~KVMPPC_RMAP_CHANGED;
  773. ret = 1;
  774. }
  775. if (!(*rmapp & KVMPPC_RMAP_PRESENT)) {
  776. unlock_rmap(rmapp);
  777. return ret;
  778. }
  779. i = head = *rmapp & KVMPPC_RMAP_INDEX;
  780. do {
  781. hptep = (unsigned long *) (kvm->arch.hpt_virt + (i << 4));
  782. j = rev[i].forw;
  783. if (!(hptep[1] & HPTE_R_C))
  784. continue;
  785. if (!try_lock_hpte(hptep, HPTE_V_HVLOCK)) {
  786. /* unlock rmap before spinning on the HPTE lock */
  787. unlock_rmap(rmapp);
  788. while (hptep[0] & HPTE_V_HVLOCK)
  789. cpu_relax();
  790. goto retry;
  791. }
  792. /* Now check and modify the HPTE */
  793. if ((hptep[0] & HPTE_V_VALID) && (hptep[1] & HPTE_R_C)) {
  794. /* need to make it temporarily absent to clear C */
  795. hptep[0] |= HPTE_V_ABSENT;
  796. kvmppc_invalidate_hpte(kvm, hptep, i);
  797. hptep[1] &= ~HPTE_R_C;
  798. eieio();
  799. hptep[0] = (hptep[0] & ~HPTE_V_ABSENT) | HPTE_V_VALID;
  800. rev[i].guest_rpte |= HPTE_R_C;
  801. ret = 1;
  802. }
  803. hptep[0] &= ~HPTE_V_HVLOCK;
  804. } while ((i = j) != head);
  805. unlock_rmap(rmapp);
  806. return ret;
  807. }
  808. long kvmppc_hv_get_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
  809. {
  810. unsigned long i;
  811. unsigned long *rmapp, *map;
  812. preempt_disable();
  813. rmapp = memslot->rmap;
  814. map = memslot->dirty_bitmap;
  815. for (i = 0; i < memslot->npages; ++i) {
  816. if (kvm_test_clear_dirty(kvm, rmapp))
  817. __set_bit_le(i, map);
  818. ++rmapp;
  819. }
  820. preempt_enable();
  821. return 0;
  822. }
  823. void *kvmppc_pin_guest_page(struct kvm *kvm, unsigned long gpa,
  824. unsigned long *nb_ret)
  825. {
  826. struct kvm_memory_slot *memslot;
  827. unsigned long gfn = gpa >> PAGE_SHIFT;
  828. struct page *page, *pages[1];
  829. int npages;
  830. unsigned long hva, psize, offset;
  831. unsigned long pa;
  832. unsigned long *physp;
  833. memslot = gfn_to_memslot(kvm, gfn);
  834. if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
  835. return NULL;
  836. if (!kvm->arch.using_mmu_notifiers) {
  837. physp = kvm->arch.slot_phys[memslot->id];
  838. if (!physp)
  839. return NULL;
  840. physp += gfn - memslot->base_gfn;
  841. pa = *physp;
  842. if (!pa) {
  843. if (kvmppc_get_guest_page(kvm, gfn, memslot,
  844. PAGE_SIZE) < 0)
  845. return NULL;
  846. pa = *physp;
  847. }
  848. page = pfn_to_page(pa >> PAGE_SHIFT);
  849. } else {
  850. hva = gfn_to_hva_memslot(memslot, gfn);
  851. npages = get_user_pages_fast(hva, 1, 1, pages);
  852. if (npages < 1)
  853. return NULL;
  854. page = pages[0];
  855. }
  856. psize = PAGE_SIZE;
  857. if (PageHuge(page)) {
  858. page = compound_head(page);
  859. psize <<= compound_order(page);
  860. }
  861. if (!kvm->arch.using_mmu_notifiers)
  862. get_page(page);
  863. offset = gpa & (psize - 1);
  864. if (nb_ret)
  865. *nb_ret = psize - offset;
  866. return page_address(page) + offset;
  867. }
  868. void kvmppc_unpin_guest_page(struct kvm *kvm, void *va)
  869. {
  870. struct page *page = virt_to_page(va);
  871. page = compound_head(page);
  872. put_page(page);
  873. }
  874. void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
  875. {
  876. struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
  877. if (cpu_has_feature(CPU_FTR_ARCH_206))
  878. vcpu->arch.slb_nr = 32; /* POWER7 */
  879. else
  880. vcpu->arch.slb_nr = 64;
  881. mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
  882. mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
  883. vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
  884. }