book3s_hv_rm_mmu.c 31 KB

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