book3s_hv_rm_mmu.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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 void do_tlbies(struct kvm *kvm, unsigned long *rbvalues,
  398. long npages, int global, bool need_sync)
  399. {
  400. long i;
  401. /*
  402. * We use the POWER9 5-operand versions of tlbie and tlbiel here.
  403. * Since we are using RIC=0 PRS=0 R=0, and P7/P8 tlbiel ignores
  404. * the RS field, this is backwards-compatible with P7 and P8.
  405. */
  406. if (global) {
  407. if (need_sync)
  408. asm volatile("ptesync" : : : "memory");
  409. for (i = 0; i < npages; ++i) {
  410. asm volatile(PPC_TLBIE_5(%0,%1,0,0,0) : :
  411. "r" (rbvalues[i]), "r" (kvm->arch.lpid));
  412. }
  413. if (cpu_has_feature(CPU_FTR_P9_TLBIE_BUG)) {
  414. /*
  415. * Need the extra ptesync to make sure we don't
  416. * re-order the tlbie
  417. */
  418. asm volatile("ptesync": : :"memory");
  419. asm volatile(PPC_TLBIE_5(%0,%1,0,0,0) : :
  420. "r" (rbvalues[0]), "r" (kvm->arch.lpid));
  421. }
  422. asm volatile("eieio; tlbsync; ptesync" : : : "memory");
  423. } else {
  424. if (need_sync)
  425. asm volatile("ptesync" : : : "memory");
  426. for (i = 0; i < npages; ++i) {
  427. asm volatile(PPC_TLBIEL(%0,%1,0,0,0) : :
  428. "r" (rbvalues[i]), "r" (0));
  429. }
  430. asm volatile("ptesync" : : : "memory");
  431. }
  432. }
  433. long kvmppc_do_h_remove(struct kvm *kvm, unsigned long flags,
  434. unsigned long pte_index, unsigned long avpn,
  435. unsigned long *hpret)
  436. {
  437. __be64 *hpte;
  438. unsigned long v, r, rb;
  439. struct revmap_entry *rev;
  440. u64 pte, orig_pte, pte_r;
  441. if (kvm_is_radix(kvm))
  442. return H_FUNCTION;
  443. if (pte_index >= kvmppc_hpt_npte(&kvm->arch.hpt))
  444. return H_PARAMETER;
  445. hpte = (__be64 *)(kvm->arch.hpt.virt + (pte_index << 4));
  446. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  447. cpu_relax();
  448. pte = orig_pte = be64_to_cpu(hpte[0]);
  449. pte_r = be64_to_cpu(hpte[1]);
  450. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  451. pte = hpte_new_to_old_v(pte, pte_r);
  452. pte_r = hpte_new_to_old_r(pte_r);
  453. }
  454. if ((pte & (HPTE_V_ABSENT | HPTE_V_VALID)) == 0 ||
  455. ((flags & H_AVPN) && (pte & ~0x7fUL) != avpn) ||
  456. ((flags & H_ANDCOND) && (pte & avpn) != 0)) {
  457. __unlock_hpte(hpte, orig_pte);
  458. return H_NOT_FOUND;
  459. }
  460. rev = real_vmalloc_addr(&kvm->arch.hpt.rev[pte_index]);
  461. v = pte & ~HPTE_V_HVLOCK;
  462. if (v & HPTE_V_VALID) {
  463. hpte[0] &= ~cpu_to_be64(HPTE_V_VALID);
  464. rb = compute_tlbie_rb(v, pte_r, pte_index);
  465. do_tlbies(kvm, &rb, 1, global_invalidates(kvm), true);
  466. /*
  467. * The reference (R) and change (C) bits in a HPT
  468. * entry can be set by hardware at any time up until
  469. * the HPTE is invalidated and the TLB invalidation
  470. * sequence has completed. This means that when
  471. * removing a HPTE, we need to re-read the HPTE after
  472. * the invalidation sequence has completed in order to
  473. * obtain reliable values of R and C.
  474. */
  475. remove_revmap_chain(kvm, pte_index, rev, v,
  476. be64_to_cpu(hpte[1]));
  477. }
  478. r = rev->guest_rpte & ~HPTE_GR_RESERVED;
  479. note_hpte_modification(kvm, rev);
  480. unlock_hpte(hpte, 0);
  481. if (is_mmio_hpte(v, pte_r))
  482. atomic64_inc(&kvm->arch.mmio_update);
  483. if (v & HPTE_V_ABSENT)
  484. v = (v & ~HPTE_V_ABSENT) | HPTE_V_VALID;
  485. hpret[0] = v;
  486. hpret[1] = r;
  487. return H_SUCCESS;
  488. }
  489. EXPORT_SYMBOL_GPL(kvmppc_do_h_remove);
  490. long kvmppc_h_remove(struct kvm_vcpu *vcpu, unsigned long flags,
  491. unsigned long pte_index, unsigned long avpn)
  492. {
  493. return kvmppc_do_h_remove(vcpu->kvm, flags, pte_index, avpn,
  494. &vcpu->arch.regs.gpr[4]);
  495. }
  496. long kvmppc_h_bulk_remove(struct kvm_vcpu *vcpu)
  497. {
  498. struct kvm *kvm = vcpu->kvm;
  499. unsigned long *args = &vcpu->arch.regs.gpr[4];
  500. __be64 *hp, *hptes[4];
  501. unsigned long tlbrb[4];
  502. long int i, j, k, n, found, indexes[4];
  503. unsigned long flags, req, pte_index, rcbits;
  504. int global;
  505. long int ret = H_SUCCESS;
  506. struct revmap_entry *rev, *revs[4];
  507. u64 hp0, hp1;
  508. if (kvm_is_radix(kvm))
  509. return H_FUNCTION;
  510. global = global_invalidates(kvm);
  511. for (i = 0; i < 4 && ret == H_SUCCESS; ) {
  512. n = 0;
  513. for (; i < 4; ++i) {
  514. j = i * 2;
  515. pte_index = args[j];
  516. flags = pte_index >> 56;
  517. pte_index &= ((1ul << 56) - 1);
  518. req = flags >> 6;
  519. flags &= 3;
  520. if (req == 3) { /* no more requests */
  521. i = 4;
  522. break;
  523. }
  524. if (req != 1 || flags == 3 ||
  525. pte_index >= kvmppc_hpt_npte(&kvm->arch.hpt)) {
  526. /* parameter error */
  527. args[j] = ((0xa0 | flags) << 56) + pte_index;
  528. ret = H_PARAMETER;
  529. break;
  530. }
  531. hp = (__be64 *) (kvm->arch.hpt.virt + (pte_index << 4));
  532. /* to avoid deadlock, don't spin except for first */
  533. if (!try_lock_hpte(hp, HPTE_V_HVLOCK)) {
  534. if (n)
  535. break;
  536. while (!try_lock_hpte(hp, HPTE_V_HVLOCK))
  537. cpu_relax();
  538. }
  539. found = 0;
  540. hp0 = be64_to_cpu(hp[0]);
  541. hp1 = be64_to_cpu(hp[1]);
  542. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  543. hp0 = hpte_new_to_old_v(hp0, hp1);
  544. hp1 = hpte_new_to_old_r(hp1);
  545. }
  546. if (hp0 & (HPTE_V_ABSENT | HPTE_V_VALID)) {
  547. switch (flags & 3) {
  548. case 0: /* absolute */
  549. found = 1;
  550. break;
  551. case 1: /* andcond */
  552. if (!(hp0 & args[j + 1]))
  553. found = 1;
  554. break;
  555. case 2: /* AVPN */
  556. if ((hp0 & ~0x7fUL) == args[j + 1])
  557. found = 1;
  558. break;
  559. }
  560. }
  561. if (!found) {
  562. hp[0] &= ~cpu_to_be64(HPTE_V_HVLOCK);
  563. args[j] = ((0x90 | flags) << 56) + pte_index;
  564. continue;
  565. }
  566. args[j] = ((0x80 | flags) << 56) + pte_index;
  567. rev = real_vmalloc_addr(&kvm->arch.hpt.rev[pte_index]);
  568. note_hpte_modification(kvm, rev);
  569. if (!(hp0 & HPTE_V_VALID)) {
  570. /* insert R and C bits from PTE */
  571. rcbits = rev->guest_rpte & (HPTE_R_R|HPTE_R_C);
  572. args[j] |= rcbits << (56 - 5);
  573. hp[0] = 0;
  574. if (is_mmio_hpte(hp0, hp1))
  575. atomic64_inc(&kvm->arch.mmio_update);
  576. continue;
  577. }
  578. /* leave it locked */
  579. hp[0] &= ~cpu_to_be64(HPTE_V_VALID);
  580. tlbrb[n] = compute_tlbie_rb(hp0, hp1, pte_index);
  581. indexes[n] = j;
  582. hptes[n] = hp;
  583. revs[n] = rev;
  584. ++n;
  585. }
  586. if (!n)
  587. break;
  588. /* Now that we've collected a batch, do the tlbies */
  589. do_tlbies(kvm, tlbrb, n, global, true);
  590. /* Read PTE low words after tlbie to get final R/C values */
  591. for (k = 0; k < n; ++k) {
  592. j = indexes[k];
  593. pte_index = args[j] & ((1ul << 56) - 1);
  594. hp = hptes[k];
  595. rev = revs[k];
  596. remove_revmap_chain(kvm, pte_index, rev,
  597. be64_to_cpu(hp[0]), be64_to_cpu(hp[1]));
  598. rcbits = rev->guest_rpte & (HPTE_R_R|HPTE_R_C);
  599. args[j] |= rcbits << (56 - 5);
  600. __unlock_hpte(hp, 0);
  601. }
  602. }
  603. return ret;
  604. }
  605. long kvmppc_h_protect(struct kvm_vcpu *vcpu, unsigned long flags,
  606. unsigned long pte_index, unsigned long avpn,
  607. unsigned long va)
  608. {
  609. struct kvm *kvm = vcpu->kvm;
  610. __be64 *hpte;
  611. struct revmap_entry *rev;
  612. unsigned long v, r, rb, mask, bits;
  613. u64 pte_v, pte_r;
  614. if (kvm_is_radix(kvm))
  615. return H_FUNCTION;
  616. if (pte_index >= kvmppc_hpt_npte(&kvm->arch.hpt))
  617. return H_PARAMETER;
  618. hpte = (__be64 *)(kvm->arch.hpt.virt + (pte_index << 4));
  619. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  620. cpu_relax();
  621. v = pte_v = be64_to_cpu(hpte[0]);
  622. if (cpu_has_feature(CPU_FTR_ARCH_300))
  623. v = hpte_new_to_old_v(v, be64_to_cpu(hpte[1]));
  624. if ((v & (HPTE_V_ABSENT | HPTE_V_VALID)) == 0 ||
  625. ((flags & H_AVPN) && (v & ~0x7fUL) != avpn)) {
  626. __unlock_hpte(hpte, pte_v);
  627. return H_NOT_FOUND;
  628. }
  629. pte_r = be64_to_cpu(hpte[1]);
  630. bits = (flags << 55) & HPTE_R_PP0;
  631. bits |= (flags << 48) & HPTE_R_KEY_HI;
  632. bits |= flags & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO);
  633. /* Update guest view of 2nd HPTE dword */
  634. mask = HPTE_R_PP0 | HPTE_R_PP | HPTE_R_N |
  635. HPTE_R_KEY_HI | HPTE_R_KEY_LO;
  636. rev = real_vmalloc_addr(&kvm->arch.hpt.rev[pte_index]);
  637. if (rev) {
  638. r = (rev->guest_rpte & ~mask) | bits;
  639. rev->guest_rpte = r;
  640. note_hpte_modification(kvm, rev);
  641. }
  642. /* Update HPTE */
  643. if (v & HPTE_V_VALID) {
  644. /*
  645. * If the page is valid, don't let it transition from
  646. * readonly to writable. If it should be writable, we'll
  647. * take a trap and let the page fault code sort it out.
  648. */
  649. r = (pte_r & ~mask) | bits;
  650. if (hpte_is_writable(r) && !hpte_is_writable(pte_r))
  651. r = hpte_make_readonly(r);
  652. /* If the PTE is changing, invalidate it first */
  653. if (r != pte_r) {
  654. rb = compute_tlbie_rb(v, r, pte_index);
  655. hpte[0] = cpu_to_be64((pte_v & ~HPTE_V_VALID) |
  656. HPTE_V_ABSENT);
  657. do_tlbies(kvm, &rb, 1, global_invalidates(kvm), true);
  658. /* Don't lose R/C bit updates done by hardware */
  659. r |= be64_to_cpu(hpte[1]) & (HPTE_R_R | HPTE_R_C);
  660. hpte[1] = cpu_to_be64(r);
  661. }
  662. }
  663. unlock_hpte(hpte, pte_v & ~HPTE_V_HVLOCK);
  664. asm volatile("ptesync" : : : "memory");
  665. if (is_mmio_hpte(v, pte_r))
  666. atomic64_inc(&kvm->arch.mmio_update);
  667. return H_SUCCESS;
  668. }
  669. long kvmppc_h_read(struct kvm_vcpu *vcpu, unsigned long flags,
  670. unsigned long pte_index)
  671. {
  672. struct kvm *kvm = vcpu->kvm;
  673. __be64 *hpte;
  674. unsigned long v, r;
  675. int i, n = 1;
  676. struct revmap_entry *rev = NULL;
  677. if (kvm_is_radix(kvm))
  678. return H_FUNCTION;
  679. if (pte_index >= kvmppc_hpt_npte(&kvm->arch.hpt))
  680. return H_PARAMETER;
  681. if (flags & H_READ_4) {
  682. pte_index &= ~3;
  683. n = 4;
  684. }
  685. rev = real_vmalloc_addr(&kvm->arch.hpt.rev[pte_index]);
  686. for (i = 0; i < n; ++i, ++pte_index) {
  687. hpte = (__be64 *)(kvm->arch.hpt.virt + (pte_index << 4));
  688. v = be64_to_cpu(hpte[0]) & ~HPTE_V_HVLOCK;
  689. r = be64_to_cpu(hpte[1]);
  690. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  691. v = hpte_new_to_old_v(v, r);
  692. r = hpte_new_to_old_r(r);
  693. }
  694. if (v & HPTE_V_ABSENT) {
  695. v &= ~HPTE_V_ABSENT;
  696. v |= HPTE_V_VALID;
  697. }
  698. if (v & HPTE_V_VALID) {
  699. r = rev[i].guest_rpte | (r & (HPTE_R_R | HPTE_R_C));
  700. r &= ~HPTE_GR_RESERVED;
  701. }
  702. vcpu->arch.regs.gpr[4 + i * 2] = v;
  703. vcpu->arch.regs.gpr[5 + i * 2] = r;
  704. }
  705. return H_SUCCESS;
  706. }
  707. long kvmppc_h_clear_ref(struct kvm_vcpu *vcpu, unsigned long flags,
  708. unsigned long pte_index)
  709. {
  710. struct kvm *kvm = vcpu->kvm;
  711. __be64 *hpte;
  712. unsigned long v, r, gr;
  713. struct revmap_entry *rev;
  714. unsigned long *rmap;
  715. long ret = H_NOT_FOUND;
  716. if (kvm_is_radix(kvm))
  717. return H_FUNCTION;
  718. if (pte_index >= kvmppc_hpt_npte(&kvm->arch.hpt))
  719. return H_PARAMETER;
  720. rev = real_vmalloc_addr(&kvm->arch.hpt.rev[pte_index]);
  721. hpte = (__be64 *)(kvm->arch.hpt.virt + (pte_index << 4));
  722. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  723. cpu_relax();
  724. v = be64_to_cpu(hpte[0]);
  725. r = be64_to_cpu(hpte[1]);
  726. if (!(v & (HPTE_V_VALID | HPTE_V_ABSENT)))
  727. goto out;
  728. gr = rev->guest_rpte;
  729. if (rev->guest_rpte & HPTE_R_R) {
  730. rev->guest_rpte &= ~HPTE_R_R;
  731. note_hpte_modification(kvm, rev);
  732. }
  733. if (v & HPTE_V_VALID) {
  734. gr |= r & (HPTE_R_R | HPTE_R_C);
  735. if (r & HPTE_R_R) {
  736. kvmppc_clear_ref_hpte(kvm, hpte, pte_index);
  737. rmap = revmap_for_hpte(kvm, v, gr, NULL, NULL);
  738. if (rmap) {
  739. lock_rmap(rmap);
  740. *rmap |= KVMPPC_RMAP_REFERENCED;
  741. unlock_rmap(rmap);
  742. }
  743. }
  744. }
  745. vcpu->arch.regs.gpr[4] = gr;
  746. ret = H_SUCCESS;
  747. out:
  748. unlock_hpte(hpte, v & ~HPTE_V_HVLOCK);
  749. return ret;
  750. }
  751. long kvmppc_h_clear_mod(struct kvm_vcpu *vcpu, unsigned long flags,
  752. unsigned long pte_index)
  753. {
  754. struct kvm *kvm = vcpu->kvm;
  755. __be64 *hpte;
  756. unsigned long v, r, gr;
  757. struct revmap_entry *rev;
  758. long ret = H_NOT_FOUND;
  759. if (kvm_is_radix(kvm))
  760. return H_FUNCTION;
  761. if (pte_index >= kvmppc_hpt_npte(&kvm->arch.hpt))
  762. return H_PARAMETER;
  763. rev = real_vmalloc_addr(&kvm->arch.hpt.rev[pte_index]);
  764. hpte = (__be64 *)(kvm->arch.hpt.virt + (pte_index << 4));
  765. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  766. cpu_relax();
  767. v = be64_to_cpu(hpte[0]);
  768. r = be64_to_cpu(hpte[1]);
  769. if (!(v & (HPTE_V_VALID | HPTE_V_ABSENT)))
  770. goto out;
  771. gr = rev->guest_rpte;
  772. if (gr & HPTE_R_C) {
  773. rev->guest_rpte &= ~HPTE_R_C;
  774. note_hpte_modification(kvm, rev);
  775. }
  776. if (v & HPTE_V_VALID) {
  777. /* need to make it temporarily absent so C is stable */
  778. hpte[0] |= cpu_to_be64(HPTE_V_ABSENT);
  779. kvmppc_invalidate_hpte(kvm, hpte, pte_index);
  780. r = be64_to_cpu(hpte[1]);
  781. gr |= r & (HPTE_R_R | HPTE_R_C);
  782. if (r & HPTE_R_C) {
  783. hpte[1] = cpu_to_be64(r & ~HPTE_R_C);
  784. eieio();
  785. kvmppc_set_dirty_from_hpte(kvm, v, gr);
  786. }
  787. }
  788. vcpu->arch.regs.gpr[4] = gr;
  789. ret = H_SUCCESS;
  790. out:
  791. unlock_hpte(hpte, v & ~HPTE_V_HVLOCK);
  792. return ret;
  793. }
  794. void kvmppc_invalidate_hpte(struct kvm *kvm, __be64 *hptep,
  795. unsigned long pte_index)
  796. {
  797. unsigned long rb;
  798. u64 hp0, hp1;
  799. hptep[0] &= ~cpu_to_be64(HPTE_V_VALID);
  800. hp0 = be64_to_cpu(hptep[0]);
  801. hp1 = be64_to_cpu(hptep[1]);
  802. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  803. hp0 = hpte_new_to_old_v(hp0, hp1);
  804. hp1 = hpte_new_to_old_r(hp1);
  805. }
  806. rb = compute_tlbie_rb(hp0, hp1, pte_index);
  807. do_tlbies(kvm, &rb, 1, 1, true);
  808. }
  809. EXPORT_SYMBOL_GPL(kvmppc_invalidate_hpte);
  810. void kvmppc_clear_ref_hpte(struct kvm *kvm, __be64 *hptep,
  811. unsigned long pte_index)
  812. {
  813. unsigned long rb;
  814. unsigned char rbyte;
  815. u64 hp0, hp1;
  816. hp0 = be64_to_cpu(hptep[0]);
  817. hp1 = be64_to_cpu(hptep[1]);
  818. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  819. hp0 = hpte_new_to_old_v(hp0, hp1);
  820. hp1 = hpte_new_to_old_r(hp1);
  821. }
  822. rb = compute_tlbie_rb(hp0, hp1, pte_index);
  823. rbyte = (be64_to_cpu(hptep[1]) & ~HPTE_R_R) >> 8;
  824. /* modify only the second-last byte, which contains the ref bit */
  825. *((char *)hptep + 14) = rbyte;
  826. do_tlbies(kvm, &rb, 1, 1, false);
  827. }
  828. EXPORT_SYMBOL_GPL(kvmppc_clear_ref_hpte);
  829. static int slb_base_page_shift[4] = {
  830. 24, /* 16M */
  831. 16, /* 64k */
  832. 34, /* 16G */
  833. 20, /* 1M, unsupported */
  834. };
  835. static struct mmio_hpte_cache_entry *mmio_cache_search(struct kvm_vcpu *vcpu,
  836. unsigned long eaddr, unsigned long slb_v, long mmio_update)
  837. {
  838. struct mmio_hpte_cache_entry *entry = NULL;
  839. unsigned int pshift;
  840. unsigned int i;
  841. for (i = 0; i < MMIO_HPTE_CACHE_SIZE; i++) {
  842. entry = &vcpu->arch.mmio_cache.entry[i];
  843. if (entry->mmio_update == mmio_update) {
  844. pshift = entry->slb_base_pshift;
  845. if ((entry->eaddr >> pshift) == (eaddr >> pshift) &&
  846. entry->slb_v == slb_v)
  847. return entry;
  848. }
  849. }
  850. return NULL;
  851. }
  852. static struct mmio_hpte_cache_entry *
  853. next_mmio_cache_entry(struct kvm_vcpu *vcpu)
  854. {
  855. unsigned int index = vcpu->arch.mmio_cache.index;
  856. vcpu->arch.mmio_cache.index++;
  857. if (vcpu->arch.mmio_cache.index == MMIO_HPTE_CACHE_SIZE)
  858. vcpu->arch.mmio_cache.index = 0;
  859. return &vcpu->arch.mmio_cache.entry[index];
  860. }
  861. /* When called from virtmode, this func should be protected by
  862. * preempt_disable(), otherwise, the holding of HPTE_V_HVLOCK
  863. * can trigger deadlock issue.
  864. */
  865. long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr, unsigned long slb_v,
  866. unsigned long valid)
  867. {
  868. unsigned int i;
  869. unsigned int pshift;
  870. unsigned long somask;
  871. unsigned long vsid, hash;
  872. unsigned long avpn;
  873. __be64 *hpte;
  874. unsigned long mask, val;
  875. unsigned long v, r, orig_v;
  876. /* Get page shift, work out hash and AVPN etc. */
  877. mask = SLB_VSID_B | HPTE_V_AVPN | HPTE_V_SECONDARY;
  878. val = 0;
  879. pshift = 12;
  880. if (slb_v & SLB_VSID_L) {
  881. mask |= HPTE_V_LARGE;
  882. val |= HPTE_V_LARGE;
  883. pshift = slb_base_page_shift[(slb_v & SLB_VSID_LP) >> 4];
  884. }
  885. if (slb_v & SLB_VSID_B_1T) {
  886. somask = (1UL << 40) - 1;
  887. vsid = (slb_v & ~SLB_VSID_B) >> SLB_VSID_SHIFT_1T;
  888. vsid ^= vsid << 25;
  889. } else {
  890. somask = (1UL << 28) - 1;
  891. vsid = (slb_v & ~SLB_VSID_B) >> SLB_VSID_SHIFT;
  892. }
  893. hash = (vsid ^ ((eaddr & somask) >> pshift)) & kvmppc_hpt_mask(&kvm->arch.hpt);
  894. avpn = slb_v & ~(somask >> 16); /* also includes B */
  895. avpn |= (eaddr & somask) >> 16;
  896. if (pshift >= 24)
  897. avpn &= ~((1UL << (pshift - 16)) - 1);
  898. else
  899. avpn &= ~0x7fUL;
  900. val |= avpn;
  901. for (;;) {
  902. hpte = (__be64 *)(kvm->arch.hpt.virt + (hash << 7));
  903. for (i = 0; i < 16; i += 2) {
  904. /* Read the PTE racily */
  905. v = be64_to_cpu(hpte[i]) & ~HPTE_V_HVLOCK;
  906. if (cpu_has_feature(CPU_FTR_ARCH_300))
  907. v = hpte_new_to_old_v(v, be64_to_cpu(hpte[i+1]));
  908. /* Check valid/absent, hash, segment size and AVPN */
  909. if (!(v & valid) || (v & mask) != val)
  910. continue;
  911. /* Lock the PTE and read it under the lock */
  912. while (!try_lock_hpte(&hpte[i], HPTE_V_HVLOCK))
  913. cpu_relax();
  914. v = orig_v = be64_to_cpu(hpte[i]) & ~HPTE_V_HVLOCK;
  915. r = be64_to_cpu(hpte[i+1]);
  916. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  917. v = hpte_new_to_old_v(v, r);
  918. r = hpte_new_to_old_r(r);
  919. }
  920. /*
  921. * Check the HPTE again, including base page size
  922. */
  923. if ((v & valid) && (v & mask) == val &&
  924. kvmppc_hpte_base_page_shift(v, r) == pshift)
  925. /* Return with the HPTE still locked */
  926. return (hash << 3) + (i >> 1);
  927. __unlock_hpte(&hpte[i], orig_v);
  928. }
  929. if (val & HPTE_V_SECONDARY)
  930. break;
  931. val |= HPTE_V_SECONDARY;
  932. hash = hash ^ kvmppc_hpt_mask(&kvm->arch.hpt);
  933. }
  934. return -1;
  935. }
  936. EXPORT_SYMBOL(kvmppc_hv_find_lock_hpte);
  937. /*
  938. * Called in real mode to check whether an HPTE not found fault
  939. * is due to accessing a paged-out page or an emulated MMIO page,
  940. * or if a protection fault is due to accessing a page that the
  941. * guest wanted read/write access to but which we made read-only.
  942. * Returns a possibly modified status (DSISR) value if not
  943. * (i.e. pass the interrupt to the guest),
  944. * -1 to pass the fault up to host kernel mode code, -2 to do that
  945. * and also load the instruction word (for MMIO emulation),
  946. * or 0 if we should make the guest retry the access.
  947. */
  948. long kvmppc_hpte_hv_fault(struct kvm_vcpu *vcpu, unsigned long addr,
  949. unsigned long slb_v, unsigned int status, bool data)
  950. {
  951. struct kvm *kvm = vcpu->kvm;
  952. long int index;
  953. unsigned long v, r, gr, orig_v;
  954. __be64 *hpte;
  955. unsigned long valid;
  956. struct revmap_entry *rev;
  957. unsigned long pp, key;
  958. struct mmio_hpte_cache_entry *cache_entry = NULL;
  959. long mmio_update = 0;
  960. /* For protection fault, expect to find a valid HPTE */
  961. valid = HPTE_V_VALID;
  962. if (status & DSISR_NOHPTE) {
  963. valid |= HPTE_V_ABSENT;
  964. mmio_update = atomic64_read(&kvm->arch.mmio_update);
  965. cache_entry = mmio_cache_search(vcpu, addr, slb_v, mmio_update);
  966. }
  967. if (cache_entry) {
  968. index = cache_entry->pte_index;
  969. v = cache_entry->hpte_v;
  970. r = cache_entry->hpte_r;
  971. gr = cache_entry->rpte;
  972. } else {
  973. index = kvmppc_hv_find_lock_hpte(kvm, addr, slb_v, valid);
  974. if (index < 0) {
  975. if (status & DSISR_NOHPTE)
  976. return status; /* there really was no HPTE */
  977. return 0; /* for prot fault, HPTE disappeared */
  978. }
  979. hpte = (__be64 *)(kvm->arch.hpt.virt + (index << 4));
  980. v = orig_v = be64_to_cpu(hpte[0]) & ~HPTE_V_HVLOCK;
  981. r = be64_to_cpu(hpte[1]);
  982. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  983. v = hpte_new_to_old_v(v, r);
  984. r = hpte_new_to_old_r(r);
  985. }
  986. rev = real_vmalloc_addr(&kvm->arch.hpt.rev[index]);
  987. gr = rev->guest_rpte;
  988. unlock_hpte(hpte, orig_v);
  989. }
  990. /* For not found, if the HPTE is valid by now, retry the instruction */
  991. if ((status & DSISR_NOHPTE) && (v & HPTE_V_VALID))
  992. return 0;
  993. /* Check access permissions to the page */
  994. pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
  995. key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
  996. status &= ~DSISR_NOHPTE; /* DSISR_NOHPTE == SRR1_ISI_NOPT */
  997. if (!data) {
  998. if (gr & (HPTE_R_N | HPTE_R_G))
  999. return status | SRR1_ISI_N_OR_G;
  1000. if (!hpte_read_permission(pp, slb_v & key))
  1001. return status | SRR1_ISI_PROT;
  1002. } else if (status & DSISR_ISSTORE) {
  1003. /* check write permission */
  1004. if (!hpte_write_permission(pp, slb_v & key))
  1005. return status | DSISR_PROTFAULT;
  1006. } else {
  1007. if (!hpte_read_permission(pp, slb_v & key))
  1008. return status | DSISR_PROTFAULT;
  1009. }
  1010. /* Check storage key, if applicable */
  1011. if (data && (vcpu->arch.shregs.msr & MSR_DR)) {
  1012. unsigned int perm = hpte_get_skey_perm(gr, vcpu->arch.amr);
  1013. if (status & DSISR_ISSTORE)
  1014. perm >>= 1;
  1015. if (perm & 1)
  1016. return status | DSISR_KEYFAULT;
  1017. }
  1018. /* Save HPTE info for virtual-mode handler */
  1019. vcpu->arch.pgfault_addr = addr;
  1020. vcpu->arch.pgfault_index = index;
  1021. vcpu->arch.pgfault_hpte[0] = v;
  1022. vcpu->arch.pgfault_hpte[1] = r;
  1023. vcpu->arch.pgfault_cache = cache_entry;
  1024. /* Check the storage key to see if it is possibly emulated MMIO */
  1025. if ((r & (HPTE_R_KEY_HI | HPTE_R_KEY_LO)) ==
  1026. (HPTE_R_KEY_HI | HPTE_R_KEY_LO)) {
  1027. if (!cache_entry) {
  1028. unsigned int pshift = 12;
  1029. unsigned int pshift_index;
  1030. if (slb_v & SLB_VSID_L) {
  1031. pshift_index = ((slb_v & SLB_VSID_LP) >> 4);
  1032. pshift = slb_base_page_shift[pshift_index];
  1033. }
  1034. cache_entry = next_mmio_cache_entry(vcpu);
  1035. cache_entry->eaddr = addr;
  1036. cache_entry->slb_base_pshift = pshift;
  1037. cache_entry->pte_index = index;
  1038. cache_entry->hpte_v = v;
  1039. cache_entry->hpte_r = r;
  1040. cache_entry->rpte = gr;
  1041. cache_entry->slb_v = slb_v;
  1042. cache_entry->mmio_update = mmio_update;
  1043. }
  1044. if (data && (vcpu->arch.shregs.msr & MSR_IR))
  1045. return -2; /* MMIO emulation - load instr word */
  1046. }
  1047. return -1; /* send fault up to host kernel mode */
  1048. }