book3s_hv_rm_mmu.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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. * Copyright 2010-2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/string.h>
  10. #include <linux/kvm.h>
  11. #include <linux/kvm_host.h>
  12. #include <linux/hugetlb.h>
  13. #include <linux/module.h>
  14. #include <linux/log2.h>
  15. #include <asm/trace.h>
  16. #include <asm/kvm_ppc.h>
  17. #include <asm/kvm_book3s.h>
  18. #include <asm/book3s/64/mmu-hash.h>
  19. #include <asm/hvcall.h>
  20. #include <asm/synch.h>
  21. #include <asm/ppc-opcode.h>
  22. #include <asm/pte-walk.h>
  23. /* Translate address of a vmalloc'd thing to a linear map address */
  24. static void *real_vmalloc_addr(void *x)
  25. {
  26. unsigned long addr = (unsigned long) x;
  27. pte_t *p;
  28. /*
  29. * assume we don't have huge pages in vmalloc space...
  30. * So don't worry about THP collapse/split. Called
  31. * Only in realmode with MSR_EE = 0, hence won't need irq_save/restore.
  32. */
  33. p = find_init_mm_pte(addr, NULL);
  34. if (!p || !pte_present(*p))
  35. return NULL;
  36. addr = (pte_pfn(*p) << PAGE_SHIFT) | (addr & ~PAGE_MASK);
  37. return __va(addr);
  38. }
  39. /* Return 1 if we need to do a global tlbie, 0 if we can use tlbiel */
  40. static int global_invalidates(struct kvm *kvm)
  41. {
  42. int global;
  43. int cpu;
  44. /*
  45. * If there is only one vcore, and it's currently running,
  46. * as indicated by local_paca->kvm_hstate.kvm_vcpu being set,
  47. * we can use tlbiel as long as we mark all other physical
  48. * cores as potentially having stale TLB entries for this lpid.
  49. * Otherwise, don't use tlbiel.
  50. */
  51. if (kvm->arch.online_vcores == 1 && local_paca->kvm_hstate.kvm_vcpu)
  52. global = 0;
  53. else
  54. global = 1;
  55. if (!global) {
  56. /* any other core might now have stale TLB entries... */
  57. smp_wmb();
  58. cpumask_setall(&kvm->arch.need_tlb_flush);
  59. cpu = local_paca->kvm_hstate.kvm_vcore->pcpu;
  60. /*
  61. * On POWER9, threads are independent but the TLB is shared,
  62. * so use the bit for the first thread to represent the core.
  63. */
  64. if (cpu_has_feature(CPU_FTR_ARCH_300))
  65. cpu = cpu_first_thread_sibling(cpu);
  66. cpumask_clear_cpu(cpu, &kvm->arch.need_tlb_flush);
  67. }
  68. return global;
  69. }
  70. /*
  71. * Add this HPTE into the chain for the real page.
  72. * Must be called with the chain locked; it unlocks the chain.
  73. */
  74. void kvmppc_add_revmap_chain(struct kvm *kvm, struct revmap_entry *rev,
  75. unsigned long *rmap, long pte_index, int realmode)
  76. {
  77. struct revmap_entry *head, *tail;
  78. unsigned long i;
  79. if (*rmap & KVMPPC_RMAP_PRESENT) {
  80. i = *rmap & KVMPPC_RMAP_INDEX;
  81. head = &kvm->arch.hpt.rev[i];
  82. if (realmode)
  83. head = real_vmalloc_addr(head);
  84. tail = &kvm->arch.hpt.rev[head->back];
  85. if (realmode)
  86. tail = real_vmalloc_addr(tail);
  87. rev->forw = i;
  88. rev->back = head->back;
  89. tail->forw = pte_index;
  90. head->back = pte_index;
  91. } else {
  92. rev->forw = rev->back = pte_index;
  93. *rmap = (*rmap & ~KVMPPC_RMAP_INDEX) |
  94. pte_index | KVMPPC_RMAP_PRESENT;
  95. }
  96. unlock_rmap(rmap);
  97. }
  98. EXPORT_SYMBOL_GPL(kvmppc_add_revmap_chain);
  99. /* Update the dirty bitmap of a memslot */
  100. void kvmppc_update_dirty_map(struct kvm_memory_slot *memslot,
  101. unsigned long gfn, unsigned long psize)
  102. {
  103. unsigned long npages;
  104. if (!psize || !memslot->dirty_bitmap)
  105. return;
  106. npages = (psize + PAGE_SIZE - 1) / PAGE_SIZE;
  107. gfn -= memslot->base_gfn;
  108. set_dirty_bits_atomic(memslot->dirty_bitmap, gfn, npages);
  109. }
  110. EXPORT_SYMBOL_GPL(kvmppc_update_dirty_map);
  111. static void kvmppc_set_dirty_from_hpte(struct kvm *kvm,
  112. unsigned long hpte_v, unsigned long hpte_gr)
  113. {
  114. struct kvm_memory_slot *memslot;
  115. unsigned long gfn;
  116. unsigned long psize;
  117. psize = kvmppc_actual_pgsz(hpte_v, hpte_gr);
  118. gfn = hpte_rpn(hpte_gr, psize);
  119. memslot = __gfn_to_memslot(kvm_memslots_raw(kvm), gfn);
  120. if (memslot && memslot->dirty_bitmap)
  121. kvmppc_update_dirty_map(memslot, gfn, psize);
  122. }
  123. /* Returns a pointer to the revmap entry for the page mapped by a HPTE */
  124. static unsigned long *revmap_for_hpte(struct kvm *kvm, unsigned long hpte_v,
  125. unsigned long hpte_gr,
  126. struct kvm_memory_slot **memslotp,
  127. unsigned long *gfnp)
  128. {
  129. struct kvm_memory_slot *memslot;
  130. unsigned long *rmap;
  131. unsigned long gfn;
  132. gfn = hpte_rpn(hpte_gr, kvmppc_actual_pgsz(hpte_v, hpte_gr));
  133. memslot = __gfn_to_memslot(kvm_memslots_raw(kvm), gfn);
  134. if (memslotp)
  135. *memslotp = memslot;
  136. if (gfnp)
  137. *gfnp = gfn;
  138. if (!memslot)
  139. return NULL;
  140. rmap = real_vmalloc_addr(&memslot->arch.rmap[gfn - memslot->base_gfn]);
  141. return rmap;
  142. }
  143. /* Remove this HPTE from the chain for a real page */
  144. static void remove_revmap_chain(struct kvm *kvm, long pte_index,
  145. struct revmap_entry *rev,
  146. unsigned long hpte_v, unsigned long hpte_r)
  147. {
  148. struct revmap_entry *next, *prev;
  149. unsigned long ptel, head;
  150. unsigned long *rmap;
  151. unsigned long rcbits;
  152. struct kvm_memory_slot *memslot;
  153. unsigned long gfn;
  154. rcbits = hpte_r & (HPTE_R_R | HPTE_R_C);
  155. ptel = rev->guest_rpte |= rcbits;
  156. rmap = revmap_for_hpte(kvm, hpte_v, ptel, &memslot, &gfn);
  157. if (!rmap)
  158. return;
  159. lock_rmap(rmap);
  160. head = *rmap & KVMPPC_RMAP_INDEX;
  161. next = real_vmalloc_addr(&kvm->arch.hpt.rev[rev->forw]);
  162. prev = real_vmalloc_addr(&kvm->arch.hpt.rev[rev->back]);
  163. next->back = rev->back;
  164. prev->forw = rev->forw;
  165. if (head == pte_index) {
  166. head = rev->forw;
  167. if (head == pte_index)
  168. *rmap &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
  169. else
  170. *rmap = (*rmap & ~KVMPPC_RMAP_INDEX) | head;
  171. }
  172. *rmap |= rcbits << KVMPPC_RMAP_RC_SHIFT;
  173. if (rcbits & HPTE_R_C)
  174. kvmppc_update_dirty_map(memslot, gfn,
  175. kvmppc_actual_pgsz(hpte_v, hpte_r));
  176. unlock_rmap(rmap);
  177. }
  178. long kvmppc_do_h_enter(struct kvm *kvm, unsigned long flags,
  179. long pte_index, unsigned long pteh, unsigned long ptel,
  180. pgd_t *pgdir, bool realmode, unsigned long *pte_idx_ret)
  181. {
  182. unsigned long i, pa, gpa, gfn, psize;
  183. unsigned long slot_fn, hva;
  184. __be64 *hpte;
  185. struct revmap_entry *rev;
  186. unsigned long g_ptel;
  187. struct kvm_memory_slot *memslot;
  188. unsigned hpage_shift;
  189. bool is_ci;
  190. unsigned long *rmap;
  191. pte_t *ptep;
  192. unsigned int writing;
  193. unsigned long mmu_seq;
  194. unsigned long rcbits, irq_flags = 0;
  195. if (kvm_is_radix(kvm))
  196. return H_FUNCTION;
  197. psize = kvmppc_actual_pgsz(pteh, ptel);
  198. if (!psize)
  199. return H_PARAMETER;
  200. writing = hpte_is_writable(ptel);
  201. pteh &= ~(HPTE_V_HVLOCK | HPTE_V_ABSENT | HPTE_V_VALID);
  202. ptel &= ~HPTE_GR_RESERVED;
  203. g_ptel = ptel;
  204. /* used later to detect if we might have been invalidated */
  205. mmu_seq = kvm->mmu_notifier_seq;
  206. smp_rmb();
  207. /* Find the memslot (if any) for this address */
  208. gpa = (ptel & HPTE_R_RPN) & ~(psize - 1);
  209. gfn = gpa >> PAGE_SHIFT;
  210. memslot = __gfn_to_memslot(kvm_memslots_raw(kvm), gfn);
  211. pa = 0;
  212. is_ci = false;
  213. rmap = NULL;
  214. if (!(memslot && !(memslot->flags & KVM_MEMSLOT_INVALID))) {
  215. /* Emulated MMIO - mark this with key=31 */
  216. pteh |= HPTE_V_ABSENT;
  217. ptel |= HPTE_R_KEY_HI | HPTE_R_KEY_LO;
  218. goto do_insert;
  219. }
  220. /* Check if the requested page fits entirely in the memslot. */
  221. if (!slot_is_aligned(memslot, psize))
  222. return H_PARAMETER;
  223. slot_fn = gfn - memslot->base_gfn;
  224. rmap = &memslot->arch.rmap[slot_fn];
  225. /* Translate to host virtual address */
  226. hva = __gfn_to_hva_memslot(memslot, gfn);
  227. /*
  228. * If we had a page table table change after lookup, we would
  229. * retry via mmu_notifier_retry.
  230. */
  231. if (!realmode)
  232. local_irq_save(irq_flags);
  233. /*
  234. * If called in real mode we have MSR_EE = 0. Otherwise
  235. * we disable irq above.
  236. */
  237. ptep = __find_linux_pte(pgdir, hva, NULL, &hpage_shift);
  238. if (ptep) {
  239. pte_t pte;
  240. unsigned int host_pte_size;
  241. if (hpage_shift)
  242. host_pte_size = 1ul << hpage_shift;
  243. else
  244. host_pte_size = PAGE_SIZE;
  245. /*
  246. * We should always find the guest page size
  247. * to <= host page size, if host is using hugepage
  248. */
  249. if (host_pte_size < psize) {
  250. if (!realmode)
  251. local_irq_restore(flags);
  252. return H_PARAMETER;
  253. }
  254. pte = kvmppc_read_update_linux_pte(ptep, writing);
  255. if (pte_present(pte) && !pte_protnone(pte)) {
  256. if (writing && !__pte_write(pte))
  257. /* make the actual HPTE be read-only */
  258. ptel = hpte_make_readonly(ptel);
  259. is_ci = pte_ci(pte);
  260. pa = pte_pfn(pte) << PAGE_SHIFT;
  261. pa |= hva & (host_pte_size - 1);
  262. pa |= gpa & ~PAGE_MASK;
  263. }
  264. }
  265. if (!realmode)
  266. local_irq_restore(irq_flags);
  267. ptel &= HPTE_R_KEY | HPTE_R_PP0 | (psize-1);
  268. ptel |= pa;
  269. if (pa)
  270. pteh |= HPTE_V_VALID;
  271. else {
  272. pteh |= HPTE_V_ABSENT;
  273. ptel &= ~(HPTE_R_KEY_HI | HPTE_R_KEY_LO);
  274. }
  275. /*If we had host pte mapping then Check WIMG */
  276. if (ptep && !hpte_cache_flags_ok(ptel, is_ci)) {
  277. if (is_ci)
  278. return H_PARAMETER;
  279. /*
  280. * Allow guest to map emulated device memory as
  281. * uncacheable, but actually make it cacheable.
  282. */
  283. ptel &= ~(HPTE_R_W|HPTE_R_I|HPTE_R_G);
  284. ptel |= HPTE_R_M;
  285. }
  286. /* Find and lock the HPTEG slot to use */
  287. do_insert:
  288. if (pte_index >= kvmppc_hpt_npte(&kvm->arch.hpt))
  289. return H_PARAMETER;
  290. if (likely((flags & H_EXACT) == 0)) {
  291. pte_index &= ~7UL;
  292. hpte = (__be64 *)(kvm->arch.hpt.virt + (pte_index << 4));
  293. for (i = 0; i < 8; ++i) {
  294. if ((be64_to_cpu(*hpte) & HPTE_V_VALID) == 0 &&
  295. try_lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID |
  296. HPTE_V_ABSENT))
  297. break;
  298. hpte += 2;
  299. }
  300. if (i == 8) {
  301. /*
  302. * Since try_lock_hpte doesn't retry (not even stdcx.
  303. * failures), it could be that there is a free slot
  304. * but we transiently failed to lock it. Try again,
  305. * actually locking each slot and checking it.
  306. */
  307. hpte -= 16;
  308. for (i = 0; i < 8; ++i) {
  309. u64 pte;
  310. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  311. cpu_relax();
  312. pte = be64_to_cpu(hpte[0]);
  313. if (!(pte & (HPTE_V_VALID | HPTE_V_ABSENT)))
  314. break;
  315. __unlock_hpte(hpte, pte);
  316. hpte += 2;
  317. }
  318. if (i == 8)
  319. return H_PTEG_FULL;
  320. }
  321. pte_index += i;
  322. } else {
  323. hpte = (__be64 *)(kvm->arch.hpt.virt + (pte_index << 4));
  324. if (!try_lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID |
  325. HPTE_V_ABSENT)) {
  326. /* Lock the slot and check again */
  327. u64 pte;
  328. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  329. cpu_relax();
  330. pte = be64_to_cpu(hpte[0]);
  331. if (pte & (HPTE_V_VALID | HPTE_V_ABSENT)) {
  332. __unlock_hpte(hpte, pte);
  333. return H_PTEG_FULL;
  334. }
  335. }
  336. }
  337. /* Save away the guest's idea of the second HPTE dword */
  338. rev = &kvm->arch.hpt.rev[pte_index];
  339. if (realmode)
  340. rev = real_vmalloc_addr(rev);
  341. if (rev) {
  342. rev->guest_rpte = g_ptel;
  343. note_hpte_modification(kvm, rev);
  344. }
  345. /* Link HPTE into reverse-map chain */
  346. if (pteh & HPTE_V_VALID) {
  347. if (realmode)
  348. rmap = real_vmalloc_addr(rmap);
  349. lock_rmap(rmap);
  350. /* Check for pending invalidations under the rmap chain lock */
  351. if (mmu_notifier_retry(kvm, mmu_seq)) {
  352. /* inval in progress, write a non-present HPTE */
  353. pteh |= HPTE_V_ABSENT;
  354. pteh &= ~HPTE_V_VALID;
  355. ptel &= ~(HPTE_R_KEY_HI | HPTE_R_KEY_LO);
  356. unlock_rmap(rmap);
  357. } else {
  358. kvmppc_add_revmap_chain(kvm, rev, rmap, pte_index,
  359. realmode);
  360. /* Only set R/C in real HPTE if already set in *rmap */
  361. rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
  362. ptel &= rcbits | ~(HPTE_R_R | HPTE_R_C);
  363. }
  364. }
  365. /* Convert to new format on P9 */
  366. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  367. ptel = hpte_old_to_new_r(pteh, ptel);
  368. pteh = hpte_old_to_new_v(pteh);
  369. }
  370. hpte[1] = cpu_to_be64(ptel);
  371. /* Write the first HPTE dword, unlocking the HPTE and making it valid */
  372. eieio();
  373. __unlock_hpte(hpte, pteh);
  374. asm volatile("ptesync" : : : "memory");
  375. *pte_idx_ret = pte_index;
  376. return H_SUCCESS;
  377. }
  378. EXPORT_SYMBOL_GPL(kvmppc_do_h_enter);
  379. long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
  380. long pte_index, unsigned long pteh, unsigned long ptel)
  381. {
  382. return kvmppc_do_h_enter(vcpu->kvm, flags, pte_index, pteh, ptel,
  383. vcpu->arch.pgdir, true,
  384. &vcpu->arch.regs.gpr[4]);
  385. }
  386. #ifdef __BIG_ENDIAN__
  387. #define LOCK_TOKEN (*(u32 *)(&get_paca()->lock_token))
  388. #else
  389. #define LOCK_TOKEN (*(u32 *)(&get_paca()->paca_index))
  390. #endif
  391. static inline int is_mmio_hpte(unsigned long v, unsigned long r)
  392. {
  393. return ((v & HPTE_V_ABSENT) &&
  394. (r & (HPTE_R_KEY_HI | HPTE_R_KEY_LO)) ==
  395. (HPTE_R_KEY_HI | HPTE_R_KEY_LO));
  396. }
  397. static inline void fixup_tlbie_lpid(unsigned long rb_value, unsigned long lpid)
  398. {
  399. if (cpu_has_feature(CPU_FTR_P9_TLBIE_ERAT_BUG)) {
  400. /* Radix flush for a hash guest */
  401. unsigned long rb,rs,prs,r,ric;
  402. rb = PPC_BIT(52); /* IS = 2 */
  403. rs = 0; /* lpid = 0 */
  404. prs = 0; /* partition scoped */
  405. r = 1; /* radix format */
  406. ric = 0; /* RIC_FLSUH_TLB */
  407. /*
  408. * Need the extra ptesync to make sure we don't
  409. * re-order the tlbie
  410. */
  411. asm volatile("ptesync": : :"memory");
  412. asm volatile(PPC_TLBIE_5(%0, %4, %3, %2, %1)
  413. : : "r"(rb), "i"(r), "i"(prs),
  414. "i"(ric), "r"(rs) : "memory");
  415. }
  416. if (cpu_has_feature(CPU_FTR_P9_TLBIE_STQ_BUG)) {
  417. asm volatile("ptesync": : :"memory");
  418. asm volatile(PPC_TLBIE_5(%0,%1,0,0,0) : :
  419. "r" (rb_value), "r" (lpid));
  420. }
  421. }
  422. static void do_tlbies(struct kvm *kvm, unsigned long *rbvalues,
  423. long npages, int global, bool need_sync)
  424. {
  425. long i;
  426. /*
  427. * We use the POWER9 5-operand versions of tlbie and tlbiel here.
  428. * Since we are using RIC=0 PRS=0 R=0, and P7/P8 tlbiel ignores
  429. * the RS field, this is backwards-compatible with P7 and P8.
  430. */
  431. if (global) {
  432. if (need_sync)
  433. asm volatile("ptesync" : : : "memory");
  434. for (i = 0; i < npages; ++i) {
  435. asm volatile(PPC_TLBIE_5(%0,%1,0,0,0) : :
  436. "r" (rbvalues[i]), "r" (kvm->arch.lpid));
  437. }
  438. fixup_tlbie_lpid(rbvalues[i - 1], kvm->arch.lpid);
  439. asm volatile("eieio; tlbsync; ptesync" : : : "memory");
  440. } else {
  441. if (need_sync)
  442. asm volatile("ptesync" : : : "memory");
  443. for (i = 0; i < npages; ++i) {
  444. asm volatile(PPC_TLBIEL(%0,%1,0,0,0) : :
  445. "r" (rbvalues[i]), "r" (0));
  446. }
  447. asm volatile("ptesync" : : : "memory");
  448. }
  449. }
  450. long kvmppc_do_h_remove(struct kvm *kvm, unsigned long flags,
  451. unsigned long pte_index, unsigned long avpn,
  452. unsigned long *hpret)
  453. {
  454. __be64 *hpte;
  455. unsigned long v, r, rb;
  456. struct revmap_entry *rev;
  457. u64 pte, orig_pte, pte_r;
  458. if (kvm_is_radix(kvm))
  459. return H_FUNCTION;
  460. if (pte_index >= kvmppc_hpt_npte(&kvm->arch.hpt))
  461. return H_PARAMETER;
  462. hpte = (__be64 *)(kvm->arch.hpt.virt + (pte_index << 4));
  463. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  464. cpu_relax();
  465. pte = orig_pte = be64_to_cpu(hpte[0]);
  466. pte_r = be64_to_cpu(hpte[1]);
  467. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  468. pte = hpte_new_to_old_v(pte, pte_r);
  469. pte_r = hpte_new_to_old_r(pte_r);
  470. }
  471. if ((pte & (HPTE_V_ABSENT | HPTE_V_VALID)) == 0 ||
  472. ((flags & H_AVPN) && (pte & ~0x7fUL) != avpn) ||
  473. ((flags & H_ANDCOND) && (pte & avpn) != 0)) {
  474. __unlock_hpte(hpte, orig_pte);
  475. return H_NOT_FOUND;
  476. }
  477. rev = real_vmalloc_addr(&kvm->arch.hpt.rev[pte_index]);
  478. v = pte & ~HPTE_V_HVLOCK;
  479. if (v & HPTE_V_VALID) {
  480. hpte[0] &= ~cpu_to_be64(HPTE_V_VALID);
  481. rb = compute_tlbie_rb(v, pte_r, pte_index);
  482. do_tlbies(kvm, &rb, 1, global_invalidates(kvm), true);
  483. /*
  484. * The reference (R) and change (C) bits in a HPT
  485. * entry can be set by hardware at any time up until
  486. * the HPTE is invalidated and the TLB invalidation
  487. * sequence has completed. This means that when
  488. * removing a HPTE, we need to re-read the HPTE after
  489. * the invalidation sequence has completed in order to
  490. * obtain reliable values of R and C.
  491. */
  492. remove_revmap_chain(kvm, pte_index, rev, v,
  493. be64_to_cpu(hpte[1]));
  494. }
  495. r = rev->guest_rpte & ~HPTE_GR_RESERVED;
  496. note_hpte_modification(kvm, rev);
  497. unlock_hpte(hpte, 0);
  498. if (is_mmio_hpte(v, pte_r))
  499. atomic64_inc(&kvm->arch.mmio_update);
  500. if (v & HPTE_V_ABSENT)
  501. v = (v & ~HPTE_V_ABSENT) | HPTE_V_VALID;
  502. hpret[0] = v;
  503. hpret[1] = r;
  504. return H_SUCCESS;
  505. }
  506. EXPORT_SYMBOL_GPL(kvmppc_do_h_remove);
  507. long kvmppc_h_remove(struct kvm_vcpu *vcpu, unsigned long flags,
  508. unsigned long pte_index, unsigned long avpn)
  509. {
  510. return kvmppc_do_h_remove(vcpu->kvm, flags, pte_index, avpn,
  511. &vcpu->arch.regs.gpr[4]);
  512. }
  513. long kvmppc_h_bulk_remove(struct kvm_vcpu *vcpu)
  514. {
  515. struct kvm *kvm = vcpu->kvm;
  516. unsigned long *args = &vcpu->arch.regs.gpr[4];
  517. __be64 *hp, *hptes[4];
  518. unsigned long tlbrb[4];
  519. long int i, j, k, n, found, indexes[4];
  520. unsigned long flags, req, pte_index, rcbits;
  521. int global;
  522. long int ret = H_SUCCESS;
  523. struct revmap_entry *rev, *revs[4];
  524. u64 hp0, hp1;
  525. if (kvm_is_radix(kvm))
  526. return H_FUNCTION;
  527. global = global_invalidates(kvm);
  528. for (i = 0; i < 4 && ret == H_SUCCESS; ) {
  529. n = 0;
  530. for (; i < 4; ++i) {
  531. j = i * 2;
  532. pte_index = args[j];
  533. flags = pte_index >> 56;
  534. pte_index &= ((1ul << 56) - 1);
  535. req = flags >> 6;
  536. flags &= 3;
  537. if (req == 3) { /* no more requests */
  538. i = 4;
  539. break;
  540. }
  541. if (req != 1 || flags == 3 ||
  542. pte_index >= kvmppc_hpt_npte(&kvm->arch.hpt)) {
  543. /* parameter error */
  544. args[j] = ((0xa0 | flags) << 56) + pte_index;
  545. ret = H_PARAMETER;
  546. break;
  547. }
  548. hp = (__be64 *) (kvm->arch.hpt.virt + (pte_index << 4));
  549. /* to avoid deadlock, don't spin except for first */
  550. if (!try_lock_hpte(hp, HPTE_V_HVLOCK)) {
  551. if (n)
  552. break;
  553. while (!try_lock_hpte(hp, HPTE_V_HVLOCK))
  554. cpu_relax();
  555. }
  556. found = 0;
  557. hp0 = be64_to_cpu(hp[0]);
  558. hp1 = be64_to_cpu(hp[1]);
  559. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  560. hp0 = hpte_new_to_old_v(hp0, hp1);
  561. hp1 = hpte_new_to_old_r(hp1);
  562. }
  563. if (hp0 & (HPTE_V_ABSENT | HPTE_V_VALID)) {
  564. switch (flags & 3) {
  565. case 0: /* absolute */
  566. found = 1;
  567. break;
  568. case 1: /* andcond */
  569. if (!(hp0 & args[j + 1]))
  570. found = 1;
  571. break;
  572. case 2: /* AVPN */
  573. if ((hp0 & ~0x7fUL) == args[j + 1])
  574. found = 1;
  575. break;
  576. }
  577. }
  578. if (!found) {
  579. hp[0] &= ~cpu_to_be64(HPTE_V_HVLOCK);
  580. args[j] = ((0x90 | flags) << 56) + pte_index;
  581. continue;
  582. }
  583. args[j] = ((0x80 | flags) << 56) + pte_index;
  584. rev = real_vmalloc_addr(&kvm->arch.hpt.rev[pte_index]);
  585. note_hpte_modification(kvm, rev);
  586. if (!(hp0 & HPTE_V_VALID)) {
  587. /* insert R and C bits from PTE */
  588. rcbits = rev->guest_rpte & (HPTE_R_R|HPTE_R_C);
  589. args[j] |= rcbits << (56 - 5);
  590. hp[0] = 0;
  591. if (is_mmio_hpte(hp0, hp1))
  592. atomic64_inc(&kvm->arch.mmio_update);
  593. continue;
  594. }
  595. /* leave it locked */
  596. hp[0] &= ~cpu_to_be64(HPTE_V_VALID);
  597. tlbrb[n] = compute_tlbie_rb(hp0, hp1, pte_index);
  598. indexes[n] = j;
  599. hptes[n] = hp;
  600. revs[n] = rev;
  601. ++n;
  602. }
  603. if (!n)
  604. break;
  605. /* Now that we've collected a batch, do the tlbies */
  606. do_tlbies(kvm, tlbrb, n, global, true);
  607. /* Read PTE low words after tlbie to get final R/C values */
  608. for (k = 0; k < n; ++k) {
  609. j = indexes[k];
  610. pte_index = args[j] & ((1ul << 56) - 1);
  611. hp = hptes[k];
  612. rev = revs[k];
  613. remove_revmap_chain(kvm, pte_index, rev,
  614. be64_to_cpu(hp[0]), be64_to_cpu(hp[1]));
  615. rcbits = rev->guest_rpte & (HPTE_R_R|HPTE_R_C);
  616. args[j] |= rcbits << (56 - 5);
  617. __unlock_hpte(hp, 0);
  618. }
  619. }
  620. return ret;
  621. }
  622. long kvmppc_h_protect(struct kvm_vcpu *vcpu, unsigned long flags,
  623. unsigned long pte_index, unsigned long avpn,
  624. unsigned long va)
  625. {
  626. struct kvm *kvm = vcpu->kvm;
  627. __be64 *hpte;
  628. struct revmap_entry *rev;
  629. unsigned long v, r, rb, mask, bits;
  630. u64 pte_v, pte_r;
  631. if (kvm_is_radix(kvm))
  632. return H_FUNCTION;
  633. if (pte_index >= kvmppc_hpt_npte(&kvm->arch.hpt))
  634. return H_PARAMETER;
  635. hpte = (__be64 *)(kvm->arch.hpt.virt + (pte_index << 4));
  636. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  637. cpu_relax();
  638. v = pte_v = be64_to_cpu(hpte[0]);
  639. if (cpu_has_feature(CPU_FTR_ARCH_300))
  640. v = hpte_new_to_old_v(v, be64_to_cpu(hpte[1]));
  641. if ((v & (HPTE_V_ABSENT | HPTE_V_VALID)) == 0 ||
  642. ((flags & H_AVPN) && (v & ~0x7fUL) != avpn)) {
  643. __unlock_hpte(hpte, pte_v);
  644. return H_NOT_FOUND;
  645. }
  646. pte_r = be64_to_cpu(hpte[1]);
  647. bits = (flags << 55) & HPTE_R_PP0;
  648. bits |= (flags << 48) & HPTE_R_KEY_HI;
  649. bits |= flags & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO);
  650. /* Update guest view of 2nd HPTE dword */
  651. mask = HPTE_R_PP0 | HPTE_R_PP | HPTE_R_N |
  652. HPTE_R_KEY_HI | HPTE_R_KEY_LO;
  653. rev = real_vmalloc_addr(&kvm->arch.hpt.rev[pte_index]);
  654. if (rev) {
  655. r = (rev->guest_rpte & ~mask) | bits;
  656. rev->guest_rpte = r;
  657. note_hpte_modification(kvm, rev);
  658. }
  659. /* Update HPTE */
  660. if (v & HPTE_V_VALID) {
  661. /*
  662. * If the page is valid, don't let it transition from
  663. * readonly to writable. If it should be writable, we'll
  664. * take a trap and let the page fault code sort it out.
  665. */
  666. r = (pte_r & ~mask) | bits;
  667. if (hpte_is_writable(r) && !hpte_is_writable(pte_r))
  668. r = hpte_make_readonly(r);
  669. /* If the PTE is changing, invalidate it first */
  670. if (r != pte_r) {
  671. rb = compute_tlbie_rb(v, r, pte_index);
  672. hpte[0] = cpu_to_be64((pte_v & ~HPTE_V_VALID) |
  673. HPTE_V_ABSENT);
  674. do_tlbies(kvm, &rb, 1, global_invalidates(kvm), true);
  675. /* Don't lose R/C bit updates done by hardware */
  676. r |= be64_to_cpu(hpte[1]) & (HPTE_R_R | HPTE_R_C);
  677. hpte[1] = cpu_to_be64(r);
  678. }
  679. }
  680. unlock_hpte(hpte, pte_v & ~HPTE_V_HVLOCK);
  681. asm volatile("ptesync" : : : "memory");
  682. if (is_mmio_hpte(v, pte_r))
  683. atomic64_inc(&kvm->arch.mmio_update);
  684. return H_SUCCESS;
  685. }
  686. long kvmppc_h_read(struct kvm_vcpu *vcpu, unsigned long flags,
  687. unsigned long pte_index)
  688. {
  689. struct kvm *kvm = vcpu->kvm;
  690. __be64 *hpte;
  691. unsigned long v, r;
  692. int i, n = 1;
  693. struct revmap_entry *rev = NULL;
  694. if (kvm_is_radix(kvm))
  695. return H_FUNCTION;
  696. if (pte_index >= kvmppc_hpt_npte(&kvm->arch.hpt))
  697. return H_PARAMETER;
  698. if (flags & H_READ_4) {
  699. pte_index &= ~3;
  700. n = 4;
  701. }
  702. rev = real_vmalloc_addr(&kvm->arch.hpt.rev[pte_index]);
  703. for (i = 0; i < n; ++i, ++pte_index) {
  704. hpte = (__be64 *)(kvm->arch.hpt.virt + (pte_index << 4));
  705. v = be64_to_cpu(hpte[0]) & ~HPTE_V_HVLOCK;
  706. r = be64_to_cpu(hpte[1]);
  707. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  708. v = hpte_new_to_old_v(v, r);
  709. r = hpte_new_to_old_r(r);
  710. }
  711. if (v & HPTE_V_ABSENT) {
  712. v &= ~HPTE_V_ABSENT;
  713. v |= HPTE_V_VALID;
  714. }
  715. if (v & HPTE_V_VALID) {
  716. r = rev[i].guest_rpte | (r & (HPTE_R_R | HPTE_R_C));
  717. r &= ~HPTE_GR_RESERVED;
  718. }
  719. vcpu->arch.regs.gpr[4 + i * 2] = v;
  720. vcpu->arch.regs.gpr[5 + i * 2] = r;
  721. }
  722. return H_SUCCESS;
  723. }
  724. long kvmppc_h_clear_ref(struct kvm_vcpu *vcpu, unsigned long flags,
  725. unsigned long pte_index)
  726. {
  727. struct kvm *kvm = vcpu->kvm;
  728. __be64 *hpte;
  729. unsigned long v, r, gr;
  730. struct revmap_entry *rev;
  731. unsigned long *rmap;
  732. long ret = H_NOT_FOUND;
  733. if (kvm_is_radix(kvm))
  734. return H_FUNCTION;
  735. if (pte_index >= kvmppc_hpt_npte(&kvm->arch.hpt))
  736. return H_PARAMETER;
  737. rev = real_vmalloc_addr(&kvm->arch.hpt.rev[pte_index]);
  738. hpte = (__be64 *)(kvm->arch.hpt.virt + (pte_index << 4));
  739. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  740. cpu_relax();
  741. v = be64_to_cpu(hpte[0]);
  742. r = be64_to_cpu(hpte[1]);
  743. if (!(v & (HPTE_V_VALID | HPTE_V_ABSENT)))
  744. goto out;
  745. gr = rev->guest_rpte;
  746. if (rev->guest_rpte & HPTE_R_R) {
  747. rev->guest_rpte &= ~HPTE_R_R;
  748. note_hpte_modification(kvm, rev);
  749. }
  750. if (v & HPTE_V_VALID) {
  751. gr |= r & (HPTE_R_R | HPTE_R_C);
  752. if (r & HPTE_R_R) {
  753. kvmppc_clear_ref_hpte(kvm, hpte, pte_index);
  754. rmap = revmap_for_hpte(kvm, v, gr, NULL, NULL);
  755. if (rmap) {
  756. lock_rmap(rmap);
  757. *rmap |= KVMPPC_RMAP_REFERENCED;
  758. unlock_rmap(rmap);
  759. }
  760. }
  761. }
  762. vcpu->arch.regs.gpr[4] = gr;
  763. ret = H_SUCCESS;
  764. out:
  765. unlock_hpte(hpte, v & ~HPTE_V_HVLOCK);
  766. return ret;
  767. }
  768. long kvmppc_h_clear_mod(struct kvm_vcpu *vcpu, unsigned long flags,
  769. unsigned long pte_index)
  770. {
  771. struct kvm *kvm = vcpu->kvm;
  772. __be64 *hpte;
  773. unsigned long v, r, gr;
  774. struct revmap_entry *rev;
  775. long ret = H_NOT_FOUND;
  776. if (kvm_is_radix(kvm))
  777. return H_FUNCTION;
  778. if (pte_index >= kvmppc_hpt_npte(&kvm->arch.hpt))
  779. return H_PARAMETER;
  780. rev = real_vmalloc_addr(&kvm->arch.hpt.rev[pte_index]);
  781. hpte = (__be64 *)(kvm->arch.hpt.virt + (pte_index << 4));
  782. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  783. cpu_relax();
  784. v = be64_to_cpu(hpte[0]);
  785. r = be64_to_cpu(hpte[1]);
  786. if (!(v & (HPTE_V_VALID | HPTE_V_ABSENT)))
  787. goto out;
  788. gr = rev->guest_rpte;
  789. if (gr & HPTE_R_C) {
  790. rev->guest_rpte &= ~HPTE_R_C;
  791. note_hpte_modification(kvm, rev);
  792. }
  793. if (v & HPTE_V_VALID) {
  794. /* need to make it temporarily absent so C is stable */
  795. hpte[0] |= cpu_to_be64(HPTE_V_ABSENT);
  796. kvmppc_invalidate_hpte(kvm, hpte, pte_index);
  797. r = be64_to_cpu(hpte[1]);
  798. gr |= r & (HPTE_R_R | HPTE_R_C);
  799. if (r & HPTE_R_C) {
  800. hpte[1] = cpu_to_be64(r & ~HPTE_R_C);
  801. eieio();
  802. kvmppc_set_dirty_from_hpte(kvm, v, gr);
  803. }
  804. }
  805. vcpu->arch.regs.gpr[4] = gr;
  806. ret = H_SUCCESS;
  807. out:
  808. unlock_hpte(hpte, v & ~HPTE_V_HVLOCK);
  809. return ret;
  810. }
  811. void kvmppc_invalidate_hpte(struct kvm *kvm, __be64 *hptep,
  812. unsigned long pte_index)
  813. {
  814. unsigned long rb;
  815. u64 hp0, hp1;
  816. hptep[0] &= ~cpu_to_be64(HPTE_V_VALID);
  817. hp0 = be64_to_cpu(hptep[0]);
  818. hp1 = be64_to_cpu(hptep[1]);
  819. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  820. hp0 = hpte_new_to_old_v(hp0, hp1);
  821. hp1 = hpte_new_to_old_r(hp1);
  822. }
  823. rb = compute_tlbie_rb(hp0, hp1, pte_index);
  824. do_tlbies(kvm, &rb, 1, 1, true);
  825. }
  826. EXPORT_SYMBOL_GPL(kvmppc_invalidate_hpte);
  827. void kvmppc_clear_ref_hpte(struct kvm *kvm, __be64 *hptep,
  828. unsigned long pte_index)
  829. {
  830. unsigned long rb;
  831. unsigned char rbyte;
  832. u64 hp0, hp1;
  833. hp0 = be64_to_cpu(hptep[0]);
  834. hp1 = be64_to_cpu(hptep[1]);
  835. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  836. hp0 = hpte_new_to_old_v(hp0, hp1);
  837. hp1 = hpte_new_to_old_r(hp1);
  838. }
  839. rb = compute_tlbie_rb(hp0, hp1, pte_index);
  840. rbyte = (be64_to_cpu(hptep[1]) & ~HPTE_R_R) >> 8;
  841. /* modify only the second-last byte, which contains the ref bit */
  842. *((char *)hptep + 14) = rbyte;
  843. do_tlbies(kvm, &rb, 1, 1, false);
  844. }
  845. EXPORT_SYMBOL_GPL(kvmppc_clear_ref_hpte);
  846. static int slb_base_page_shift[4] = {
  847. 24, /* 16M */
  848. 16, /* 64k */
  849. 34, /* 16G */
  850. 20, /* 1M, unsupported */
  851. };
  852. static struct mmio_hpte_cache_entry *mmio_cache_search(struct kvm_vcpu *vcpu,
  853. unsigned long eaddr, unsigned long slb_v, long mmio_update)
  854. {
  855. struct mmio_hpte_cache_entry *entry = NULL;
  856. unsigned int pshift;
  857. unsigned int i;
  858. for (i = 0; i < MMIO_HPTE_CACHE_SIZE; i++) {
  859. entry = &vcpu->arch.mmio_cache.entry[i];
  860. if (entry->mmio_update == mmio_update) {
  861. pshift = entry->slb_base_pshift;
  862. if ((entry->eaddr >> pshift) == (eaddr >> pshift) &&
  863. entry->slb_v == slb_v)
  864. return entry;
  865. }
  866. }
  867. return NULL;
  868. }
  869. static struct mmio_hpte_cache_entry *
  870. next_mmio_cache_entry(struct kvm_vcpu *vcpu)
  871. {
  872. unsigned int index = vcpu->arch.mmio_cache.index;
  873. vcpu->arch.mmio_cache.index++;
  874. if (vcpu->arch.mmio_cache.index == MMIO_HPTE_CACHE_SIZE)
  875. vcpu->arch.mmio_cache.index = 0;
  876. return &vcpu->arch.mmio_cache.entry[index];
  877. }
  878. /* When called from virtmode, this func should be protected by
  879. * preempt_disable(), otherwise, the holding of HPTE_V_HVLOCK
  880. * can trigger deadlock issue.
  881. */
  882. long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr, unsigned long slb_v,
  883. unsigned long valid)
  884. {
  885. unsigned int i;
  886. unsigned int pshift;
  887. unsigned long somask;
  888. unsigned long vsid, hash;
  889. unsigned long avpn;
  890. __be64 *hpte;
  891. unsigned long mask, val;
  892. unsigned long v, r, orig_v;
  893. /* Get page shift, work out hash and AVPN etc. */
  894. mask = SLB_VSID_B | HPTE_V_AVPN | HPTE_V_SECONDARY;
  895. val = 0;
  896. pshift = 12;
  897. if (slb_v & SLB_VSID_L) {
  898. mask |= HPTE_V_LARGE;
  899. val |= HPTE_V_LARGE;
  900. pshift = slb_base_page_shift[(slb_v & SLB_VSID_LP) >> 4];
  901. }
  902. if (slb_v & SLB_VSID_B_1T) {
  903. somask = (1UL << 40) - 1;
  904. vsid = (slb_v & ~SLB_VSID_B) >> SLB_VSID_SHIFT_1T;
  905. vsid ^= vsid << 25;
  906. } else {
  907. somask = (1UL << 28) - 1;
  908. vsid = (slb_v & ~SLB_VSID_B) >> SLB_VSID_SHIFT;
  909. }
  910. hash = (vsid ^ ((eaddr & somask) >> pshift)) & kvmppc_hpt_mask(&kvm->arch.hpt);
  911. avpn = slb_v & ~(somask >> 16); /* also includes B */
  912. avpn |= (eaddr & somask) >> 16;
  913. if (pshift >= 24)
  914. avpn &= ~((1UL << (pshift - 16)) - 1);
  915. else
  916. avpn &= ~0x7fUL;
  917. val |= avpn;
  918. for (;;) {
  919. hpte = (__be64 *)(kvm->arch.hpt.virt + (hash << 7));
  920. for (i = 0; i < 16; i += 2) {
  921. /* Read the PTE racily */
  922. v = be64_to_cpu(hpte[i]) & ~HPTE_V_HVLOCK;
  923. if (cpu_has_feature(CPU_FTR_ARCH_300))
  924. v = hpte_new_to_old_v(v, be64_to_cpu(hpte[i+1]));
  925. /* Check valid/absent, hash, segment size and AVPN */
  926. if (!(v & valid) || (v & mask) != val)
  927. continue;
  928. /* Lock the PTE and read it under the lock */
  929. while (!try_lock_hpte(&hpte[i], HPTE_V_HVLOCK))
  930. cpu_relax();
  931. v = orig_v = be64_to_cpu(hpte[i]) & ~HPTE_V_HVLOCK;
  932. r = be64_to_cpu(hpte[i+1]);
  933. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  934. v = hpte_new_to_old_v(v, r);
  935. r = hpte_new_to_old_r(r);
  936. }
  937. /*
  938. * Check the HPTE again, including base page size
  939. */
  940. if ((v & valid) && (v & mask) == val &&
  941. kvmppc_hpte_base_page_shift(v, r) == pshift)
  942. /* Return with the HPTE still locked */
  943. return (hash << 3) + (i >> 1);
  944. __unlock_hpte(&hpte[i], orig_v);
  945. }
  946. if (val & HPTE_V_SECONDARY)
  947. break;
  948. val |= HPTE_V_SECONDARY;
  949. hash = hash ^ kvmppc_hpt_mask(&kvm->arch.hpt);
  950. }
  951. return -1;
  952. }
  953. EXPORT_SYMBOL(kvmppc_hv_find_lock_hpte);
  954. /*
  955. * Called in real mode to check whether an HPTE not found fault
  956. * is due to accessing a paged-out page or an emulated MMIO page,
  957. * or if a protection fault is due to accessing a page that the
  958. * guest wanted read/write access to but which we made read-only.
  959. * Returns a possibly modified status (DSISR) value if not
  960. * (i.e. pass the interrupt to the guest),
  961. * -1 to pass the fault up to host kernel mode code, -2 to do that
  962. * and also load the instruction word (for MMIO emulation),
  963. * or 0 if we should make the guest retry the access.
  964. */
  965. long kvmppc_hpte_hv_fault(struct kvm_vcpu *vcpu, unsigned long addr,
  966. unsigned long slb_v, unsigned int status, bool data)
  967. {
  968. struct kvm *kvm = vcpu->kvm;
  969. long int index;
  970. unsigned long v, r, gr, orig_v;
  971. __be64 *hpte;
  972. unsigned long valid;
  973. struct revmap_entry *rev;
  974. unsigned long pp, key;
  975. struct mmio_hpte_cache_entry *cache_entry = NULL;
  976. long mmio_update = 0;
  977. /* For protection fault, expect to find a valid HPTE */
  978. valid = HPTE_V_VALID;
  979. if (status & DSISR_NOHPTE) {
  980. valid |= HPTE_V_ABSENT;
  981. mmio_update = atomic64_read(&kvm->arch.mmio_update);
  982. cache_entry = mmio_cache_search(vcpu, addr, slb_v, mmio_update);
  983. }
  984. if (cache_entry) {
  985. index = cache_entry->pte_index;
  986. v = cache_entry->hpte_v;
  987. r = cache_entry->hpte_r;
  988. gr = cache_entry->rpte;
  989. } else {
  990. index = kvmppc_hv_find_lock_hpte(kvm, addr, slb_v, valid);
  991. if (index < 0) {
  992. if (status & DSISR_NOHPTE)
  993. return status; /* there really was no HPTE */
  994. return 0; /* for prot fault, HPTE disappeared */
  995. }
  996. hpte = (__be64 *)(kvm->arch.hpt.virt + (index << 4));
  997. v = orig_v = be64_to_cpu(hpte[0]) & ~HPTE_V_HVLOCK;
  998. r = be64_to_cpu(hpte[1]);
  999. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  1000. v = hpte_new_to_old_v(v, r);
  1001. r = hpte_new_to_old_r(r);
  1002. }
  1003. rev = real_vmalloc_addr(&kvm->arch.hpt.rev[index]);
  1004. gr = rev->guest_rpte;
  1005. unlock_hpte(hpte, orig_v);
  1006. }
  1007. /* For not found, if the HPTE is valid by now, retry the instruction */
  1008. if ((status & DSISR_NOHPTE) && (v & HPTE_V_VALID))
  1009. return 0;
  1010. /* Check access permissions to the page */
  1011. pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
  1012. key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
  1013. status &= ~DSISR_NOHPTE; /* DSISR_NOHPTE == SRR1_ISI_NOPT */
  1014. if (!data) {
  1015. if (gr & (HPTE_R_N | HPTE_R_G))
  1016. return status | SRR1_ISI_N_OR_G;
  1017. if (!hpte_read_permission(pp, slb_v & key))
  1018. return status | SRR1_ISI_PROT;
  1019. } else if (status & DSISR_ISSTORE) {
  1020. /* check write permission */
  1021. if (!hpte_write_permission(pp, slb_v & key))
  1022. return status | DSISR_PROTFAULT;
  1023. } else {
  1024. if (!hpte_read_permission(pp, slb_v & key))
  1025. return status | DSISR_PROTFAULT;
  1026. }
  1027. /* Check storage key, if applicable */
  1028. if (data && (vcpu->arch.shregs.msr & MSR_DR)) {
  1029. unsigned int perm = hpte_get_skey_perm(gr, vcpu->arch.amr);
  1030. if (status & DSISR_ISSTORE)
  1031. perm >>= 1;
  1032. if (perm & 1)
  1033. return status | DSISR_KEYFAULT;
  1034. }
  1035. /* Save HPTE info for virtual-mode handler */
  1036. vcpu->arch.pgfault_addr = addr;
  1037. vcpu->arch.pgfault_index = index;
  1038. vcpu->arch.pgfault_hpte[0] = v;
  1039. vcpu->arch.pgfault_hpte[1] = r;
  1040. vcpu->arch.pgfault_cache = cache_entry;
  1041. /* Check the storage key to see if it is possibly emulated MMIO */
  1042. if ((r & (HPTE_R_KEY_HI | HPTE_R_KEY_LO)) ==
  1043. (HPTE_R_KEY_HI | HPTE_R_KEY_LO)) {
  1044. if (!cache_entry) {
  1045. unsigned int pshift = 12;
  1046. unsigned int pshift_index;
  1047. if (slb_v & SLB_VSID_L) {
  1048. pshift_index = ((slb_v & SLB_VSID_LP) >> 4);
  1049. pshift = slb_base_page_shift[pshift_index];
  1050. }
  1051. cache_entry = next_mmio_cache_entry(vcpu);
  1052. cache_entry->eaddr = addr;
  1053. cache_entry->slb_base_pshift = pshift;
  1054. cache_entry->pte_index = index;
  1055. cache_entry->hpte_v = v;
  1056. cache_entry->hpte_r = r;
  1057. cache_entry->rpte = gr;
  1058. cache_entry->slb_v = slb_v;
  1059. cache_entry->mmio_update = mmio_update;
  1060. }
  1061. if (data && (vcpu->arch.shregs.msr & MSR_IR))
  1062. return -2; /* MMIO emulation - load instr word */
  1063. }
  1064. return -1; /* send fault up to host kernel mode */
  1065. }