book3s_hv_rm_mmu.c 30 KB

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