book3s_hv_rm_mmu.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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 <asm/tlbflush.h>
  15. #include <asm/kvm_ppc.h>
  16. #include <asm/kvm_book3s.h>
  17. #include <asm/mmu-hash64.h>
  18. #include <asm/hvcall.h>
  19. #include <asm/synch.h>
  20. #include <asm/ppc-opcode.h>
  21. /* Translate address of a vmalloc'd thing to a linear map address */
  22. static void *real_vmalloc_addr(void *x)
  23. {
  24. unsigned long addr = (unsigned long) x;
  25. pte_t *p;
  26. /*
  27. * assume we don't have huge pages in vmalloc space...
  28. * So don't worry about THP collapse/split. Called
  29. * Only in realmode, hence won't need irq_save/restore.
  30. */
  31. p = __find_linux_pte_or_hugepte(swapper_pg_dir, addr, NULL);
  32. if (!p || !pte_present(*p))
  33. return NULL;
  34. addr = (pte_pfn(*p) << PAGE_SHIFT) | (addr & ~PAGE_MASK);
  35. return __va(addr);
  36. }
  37. /* Return 1 if we need to do a global tlbie, 0 if we can use tlbiel */
  38. static int global_invalidates(struct kvm *kvm, unsigned long flags)
  39. {
  40. int global;
  41. /*
  42. * If there is only one vcore, and it's currently running,
  43. * as indicated by local_paca->kvm_hstate.kvm_vcpu being set,
  44. * we can use tlbiel as long as we mark all other physical
  45. * cores as potentially having stale TLB entries for this lpid.
  46. * Otherwise, don't use tlbiel.
  47. */
  48. if (kvm->arch.online_vcores == 1 && local_paca->kvm_hstate.kvm_vcpu)
  49. global = 0;
  50. else
  51. global = 1;
  52. if (!global) {
  53. /* any other core might now have stale TLB entries... */
  54. smp_wmb();
  55. cpumask_setall(&kvm->arch.need_tlb_flush);
  56. cpumask_clear_cpu(local_paca->kvm_hstate.kvm_vcore->pcpu,
  57. &kvm->arch.need_tlb_flush);
  58. }
  59. return global;
  60. }
  61. /*
  62. * Add this HPTE into the chain for the real page.
  63. * Must be called with the chain locked; it unlocks the chain.
  64. */
  65. void kvmppc_add_revmap_chain(struct kvm *kvm, struct revmap_entry *rev,
  66. unsigned long *rmap, long pte_index, int realmode)
  67. {
  68. struct revmap_entry *head, *tail;
  69. unsigned long i;
  70. if (*rmap & KVMPPC_RMAP_PRESENT) {
  71. i = *rmap & KVMPPC_RMAP_INDEX;
  72. head = &kvm->arch.revmap[i];
  73. if (realmode)
  74. head = real_vmalloc_addr(head);
  75. tail = &kvm->arch.revmap[head->back];
  76. if (realmode)
  77. tail = real_vmalloc_addr(tail);
  78. rev->forw = i;
  79. rev->back = head->back;
  80. tail->forw = pte_index;
  81. head->back = pte_index;
  82. } else {
  83. rev->forw = rev->back = pte_index;
  84. *rmap = (*rmap & ~KVMPPC_RMAP_INDEX) |
  85. pte_index | KVMPPC_RMAP_PRESENT;
  86. }
  87. unlock_rmap(rmap);
  88. }
  89. EXPORT_SYMBOL_GPL(kvmppc_add_revmap_chain);
  90. /* Remove this HPTE from the chain for a real page */
  91. static void remove_revmap_chain(struct kvm *kvm, long pte_index,
  92. struct revmap_entry *rev,
  93. unsigned long hpte_v, unsigned long hpte_r)
  94. {
  95. struct revmap_entry *next, *prev;
  96. unsigned long gfn, ptel, head;
  97. struct kvm_memory_slot *memslot;
  98. unsigned long *rmap;
  99. unsigned long rcbits;
  100. rcbits = hpte_r & (HPTE_R_R | HPTE_R_C);
  101. ptel = rev->guest_rpte |= rcbits;
  102. gfn = hpte_rpn(ptel, hpte_page_size(hpte_v, ptel));
  103. memslot = __gfn_to_memslot(kvm_memslots_raw(kvm), gfn);
  104. if (!memslot)
  105. return;
  106. rmap = real_vmalloc_addr(&memslot->arch.rmap[gfn - memslot->base_gfn]);
  107. lock_rmap(rmap);
  108. head = *rmap & KVMPPC_RMAP_INDEX;
  109. next = real_vmalloc_addr(&kvm->arch.revmap[rev->forw]);
  110. prev = real_vmalloc_addr(&kvm->arch.revmap[rev->back]);
  111. next->back = rev->back;
  112. prev->forw = rev->forw;
  113. if (head == pte_index) {
  114. head = rev->forw;
  115. if (head == pte_index)
  116. *rmap &= ~(KVMPPC_RMAP_PRESENT | KVMPPC_RMAP_INDEX);
  117. else
  118. *rmap = (*rmap & ~KVMPPC_RMAP_INDEX) | head;
  119. }
  120. *rmap |= rcbits << KVMPPC_RMAP_RC_SHIFT;
  121. unlock_rmap(rmap);
  122. }
  123. long kvmppc_do_h_enter(struct kvm *kvm, unsigned long flags,
  124. long pte_index, unsigned long pteh, unsigned long ptel,
  125. pgd_t *pgdir, bool realmode, unsigned long *pte_idx_ret)
  126. {
  127. unsigned long i, pa, gpa, gfn, psize;
  128. unsigned long slot_fn, hva;
  129. __be64 *hpte;
  130. struct revmap_entry *rev;
  131. unsigned long g_ptel;
  132. struct kvm_memory_slot *memslot;
  133. unsigned hpage_shift;
  134. unsigned long is_io;
  135. unsigned long *rmap;
  136. pte_t *ptep;
  137. unsigned int writing;
  138. unsigned long mmu_seq;
  139. unsigned long rcbits, irq_flags = 0;
  140. psize = hpte_page_size(pteh, ptel);
  141. if (!psize)
  142. return H_PARAMETER;
  143. writing = hpte_is_writable(ptel);
  144. pteh &= ~(HPTE_V_HVLOCK | HPTE_V_ABSENT | HPTE_V_VALID);
  145. ptel &= ~HPTE_GR_RESERVED;
  146. g_ptel = ptel;
  147. /* used later to detect if we might have been invalidated */
  148. mmu_seq = kvm->mmu_notifier_seq;
  149. smp_rmb();
  150. /* Find the memslot (if any) for this address */
  151. gpa = (ptel & HPTE_R_RPN) & ~(psize - 1);
  152. gfn = gpa >> PAGE_SHIFT;
  153. memslot = __gfn_to_memslot(kvm_memslots_raw(kvm), gfn);
  154. pa = 0;
  155. is_io = ~0ul;
  156. rmap = NULL;
  157. if (!(memslot && !(memslot->flags & KVM_MEMSLOT_INVALID))) {
  158. /* Emulated MMIO - mark this with key=31 */
  159. pteh |= HPTE_V_ABSENT;
  160. ptel |= HPTE_R_KEY_HI | HPTE_R_KEY_LO;
  161. goto do_insert;
  162. }
  163. /* Check if the requested page fits entirely in the memslot. */
  164. if (!slot_is_aligned(memslot, psize))
  165. return H_PARAMETER;
  166. slot_fn = gfn - memslot->base_gfn;
  167. rmap = &memslot->arch.rmap[slot_fn];
  168. /* Translate to host virtual address */
  169. hva = __gfn_to_hva_memslot(memslot, gfn);
  170. /*
  171. * If we had a page table table change after lookup, we would
  172. * retry via mmu_notifier_retry.
  173. */
  174. if (realmode)
  175. ptep = __find_linux_pte_or_hugepte(pgdir, hva, &hpage_shift);
  176. else {
  177. local_irq_save(irq_flags);
  178. ptep = find_linux_pte_or_hugepte(pgdir, hva, &hpage_shift);
  179. }
  180. if (ptep) {
  181. pte_t pte;
  182. unsigned int host_pte_size;
  183. if (hpage_shift)
  184. host_pte_size = 1ul << hpage_shift;
  185. else
  186. host_pte_size = PAGE_SIZE;
  187. /*
  188. * We should always find the guest page size
  189. * to <= host page size, if host is using hugepage
  190. */
  191. if (host_pte_size < psize) {
  192. if (!realmode)
  193. local_irq_restore(flags);
  194. return H_PARAMETER;
  195. }
  196. pte = kvmppc_read_update_linux_pte(ptep, writing);
  197. if (pte_present(pte) && !pte_protnone(pte)) {
  198. if (writing && !pte_write(pte))
  199. /* make the actual HPTE be read-only */
  200. ptel = hpte_make_readonly(ptel);
  201. is_io = hpte_cache_bits(pte_val(pte));
  202. pa = pte_pfn(pte) << PAGE_SHIFT;
  203. pa |= hva & (host_pte_size - 1);
  204. pa |= gpa & ~PAGE_MASK;
  205. }
  206. }
  207. if (!realmode)
  208. local_irq_restore(irq_flags);
  209. ptel &= ~(HPTE_R_PP0 - psize);
  210. ptel |= pa;
  211. if (pa)
  212. pteh |= HPTE_V_VALID;
  213. else
  214. pteh |= HPTE_V_ABSENT;
  215. /* Check WIMG */
  216. if (is_io != ~0ul && !hpte_cache_flags_ok(ptel, is_io)) {
  217. if (is_io)
  218. return H_PARAMETER;
  219. /*
  220. * Allow guest to map emulated device memory as
  221. * uncacheable, but actually make it cacheable.
  222. */
  223. ptel &= ~(HPTE_R_W|HPTE_R_I|HPTE_R_G);
  224. ptel |= HPTE_R_M;
  225. }
  226. /* Find and lock the HPTEG slot to use */
  227. do_insert:
  228. if (pte_index >= kvm->arch.hpt_npte)
  229. return H_PARAMETER;
  230. if (likely((flags & H_EXACT) == 0)) {
  231. pte_index &= ~7UL;
  232. hpte = (__be64 *)(kvm->arch.hpt_virt + (pte_index << 4));
  233. for (i = 0; i < 8; ++i) {
  234. if ((be64_to_cpu(*hpte) & HPTE_V_VALID) == 0 &&
  235. try_lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID |
  236. HPTE_V_ABSENT))
  237. break;
  238. hpte += 2;
  239. }
  240. if (i == 8) {
  241. /*
  242. * Since try_lock_hpte doesn't retry (not even stdcx.
  243. * failures), it could be that there is a free slot
  244. * but we transiently failed to lock it. Try again,
  245. * actually locking each slot and checking it.
  246. */
  247. hpte -= 16;
  248. for (i = 0; i < 8; ++i) {
  249. u64 pte;
  250. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  251. cpu_relax();
  252. pte = be64_to_cpu(hpte[0]);
  253. if (!(pte & (HPTE_V_VALID | HPTE_V_ABSENT)))
  254. break;
  255. __unlock_hpte(hpte, pte);
  256. hpte += 2;
  257. }
  258. if (i == 8)
  259. return H_PTEG_FULL;
  260. }
  261. pte_index += i;
  262. } else {
  263. hpte = (__be64 *)(kvm->arch.hpt_virt + (pte_index << 4));
  264. if (!try_lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID |
  265. HPTE_V_ABSENT)) {
  266. /* Lock the slot and check again */
  267. u64 pte;
  268. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  269. cpu_relax();
  270. pte = be64_to_cpu(hpte[0]);
  271. if (pte & (HPTE_V_VALID | HPTE_V_ABSENT)) {
  272. __unlock_hpte(hpte, pte);
  273. return H_PTEG_FULL;
  274. }
  275. }
  276. }
  277. /* Save away the guest's idea of the second HPTE dword */
  278. rev = &kvm->arch.revmap[pte_index];
  279. if (realmode)
  280. rev = real_vmalloc_addr(rev);
  281. if (rev) {
  282. rev->guest_rpte = g_ptel;
  283. note_hpte_modification(kvm, rev);
  284. }
  285. /* Link HPTE into reverse-map chain */
  286. if (pteh & HPTE_V_VALID) {
  287. if (realmode)
  288. rmap = real_vmalloc_addr(rmap);
  289. lock_rmap(rmap);
  290. /* Check for pending invalidations under the rmap chain lock */
  291. if (mmu_notifier_retry(kvm, mmu_seq)) {
  292. /* inval in progress, write a non-present HPTE */
  293. pteh |= HPTE_V_ABSENT;
  294. pteh &= ~HPTE_V_VALID;
  295. unlock_rmap(rmap);
  296. } else {
  297. kvmppc_add_revmap_chain(kvm, rev, rmap, pte_index,
  298. realmode);
  299. /* Only set R/C in real HPTE if already set in *rmap */
  300. rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
  301. ptel &= rcbits | ~(HPTE_R_R | HPTE_R_C);
  302. }
  303. }
  304. hpte[1] = cpu_to_be64(ptel);
  305. /* Write the first HPTE dword, unlocking the HPTE and making it valid */
  306. eieio();
  307. __unlock_hpte(hpte, pteh);
  308. asm volatile("ptesync" : : : "memory");
  309. *pte_idx_ret = pte_index;
  310. return H_SUCCESS;
  311. }
  312. EXPORT_SYMBOL_GPL(kvmppc_do_h_enter);
  313. long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
  314. long pte_index, unsigned long pteh, unsigned long ptel)
  315. {
  316. return kvmppc_do_h_enter(vcpu->kvm, flags, pte_index, pteh, ptel,
  317. vcpu->arch.pgdir, true, &vcpu->arch.gpr[4]);
  318. }
  319. #ifdef __BIG_ENDIAN__
  320. #define LOCK_TOKEN (*(u32 *)(&get_paca()->lock_token))
  321. #else
  322. #define LOCK_TOKEN (*(u32 *)(&get_paca()->paca_index))
  323. #endif
  324. static inline int try_lock_tlbie(unsigned int *lock)
  325. {
  326. unsigned int tmp, old;
  327. unsigned int token = LOCK_TOKEN;
  328. asm volatile("1:lwarx %1,0,%2\n"
  329. " cmpwi cr0,%1,0\n"
  330. " bne 2f\n"
  331. " stwcx. %3,0,%2\n"
  332. " bne- 1b\n"
  333. " isync\n"
  334. "2:"
  335. : "=&r" (tmp), "=&r" (old)
  336. : "r" (lock), "r" (token)
  337. : "cc", "memory");
  338. return old == 0;
  339. }
  340. static void do_tlbies(struct kvm *kvm, unsigned long *rbvalues,
  341. long npages, int global, bool need_sync)
  342. {
  343. long i;
  344. if (global) {
  345. while (!try_lock_tlbie(&kvm->arch.tlbie_lock))
  346. cpu_relax();
  347. if (need_sync)
  348. asm volatile("ptesync" : : : "memory");
  349. for (i = 0; i < npages; ++i)
  350. asm volatile(PPC_TLBIE(%1,%0) : :
  351. "r" (rbvalues[i]), "r" (kvm->arch.lpid));
  352. asm volatile("eieio; tlbsync; ptesync" : : : "memory");
  353. kvm->arch.tlbie_lock = 0;
  354. } else {
  355. if (need_sync)
  356. asm volatile("ptesync" : : : "memory");
  357. for (i = 0; i < npages; ++i)
  358. asm volatile("tlbiel %0" : : "r" (rbvalues[i]));
  359. asm volatile("ptesync" : : : "memory");
  360. }
  361. }
  362. long kvmppc_do_h_remove(struct kvm *kvm, unsigned long flags,
  363. unsigned long pte_index, unsigned long avpn,
  364. unsigned long *hpret)
  365. {
  366. __be64 *hpte;
  367. unsigned long v, r, rb;
  368. struct revmap_entry *rev;
  369. u64 pte;
  370. if (pte_index >= kvm->arch.hpt_npte)
  371. return H_PARAMETER;
  372. hpte = (__be64 *)(kvm->arch.hpt_virt + (pte_index << 4));
  373. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  374. cpu_relax();
  375. pte = be64_to_cpu(hpte[0]);
  376. if ((pte & (HPTE_V_ABSENT | HPTE_V_VALID)) == 0 ||
  377. ((flags & H_AVPN) && (pte & ~0x7fUL) != avpn) ||
  378. ((flags & H_ANDCOND) && (pte & avpn) != 0)) {
  379. __unlock_hpte(hpte, pte);
  380. return H_NOT_FOUND;
  381. }
  382. rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
  383. v = pte & ~HPTE_V_HVLOCK;
  384. if (v & HPTE_V_VALID) {
  385. u64 pte1;
  386. pte1 = be64_to_cpu(hpte[1]);
  387. hpte[0] &= ~cpu_to_be64(HPTE_V_VALID);
  388. rb = compute_tlbie_rb(v, pte1, pte_index);
  389. do_tlbies(kvm, &rb, 1, global_invalidates(kvm, flags), true);
  390. /* Read PTE low word after tlbie to get final R/C values */
  391. remove_revmap_chain(kvm, pte_index, rev, v, pte1);
  392. }
  393. r = rev->guest_rpte & ~HPTE_GR_RESERVED;
  394. note_hpte_modification(kvm, rev);
  395. unlock_hpte(hpte, 0);
  396. hpret[0] = v;
  397. hpret[1] = r;
  398. return H_SUCCESS;
  399. }
  400. EXPORT_SYMBOL_GPL(kvmppc_do_h_remove);
  401. long kvmppc_h_remove(struct kvm_vcpu *vcpu, unsigned long flags,
  402. unsigned long pte_index, unsigned long avpn)
  403. {
  404. return kvmppc_do_h_remove(vcpu->kvm, flags, pte_index, avpn,
  405. &vcpu->arch.gpr[4]);
  406. }
  407. long kvmppc_h_bulk_remove(struct kvm_vcpu *vcpu)
  408. {
  409. struct kvm *kvm = vcpu->kvm;
  410. unsigned long *args = &vcpu->arch.gpr[4];
  411. __be64 *hp, *hptes[4];
  412. unsigned long tlbrb[4];
  413. long int i, j, k, n, found, indexes[4];
  414. unsigned long flags, req, pte_index, rcbits;
  415. int global;
  416. long int ret = H_SUCCESS;
  417. struct revmap_entry *rev, *revs[4];
  418. u64 hp0;
  419. global = global_invalidates(kvm, 0);
  420. for (i = 0; i < 4 && ret == H_SUCCESS; ) {
  421. n = 0;
  422. for (; i < 4; ++i) {
  423. j = i * 2;
  424. pte_index = args[j];
  425. flags = pte_index >> 56;
  426. pte_index &= ((1ul << 56) - 1);
  427. req = flags >> 6;
  428. flags &= 3;
  429. if (req == 3) { /* no more requests */
  430. i = 4;
  431. break;
  432. }
  433. if (req != 1 || flags == 3 ||
  434. pte_index >= kvm->arch.hpt_npte) {
  435. /* parameter error */
  436. args[j] = ((0xa0 | flags) << 56) + pte_index;
  437. ret = H_PARAMETER;
  438. break;
  439. }
  440. hp = (__be64 *) (kvm->arch.hpt_virt + (pte_index << 4));
  441. /* to avoid deadlock, don't spin except for first */
  442. if (!try_lock_hpte(hp, HPTE_V_HVLOCK)) {
  443. if (n)
  444. break;
  445. while (!try_lock_hpte(hp, HPTE_V_HVLOCK))
  446. cpu_relax();
  447. }
  448. found = 0;
  449. hp0 = be64_to_cpu(hp[0]);
  450. if (hp0 & (HPTE_V_ABSENT | HPTE_V_VALID)) {
  451. switch (flags & 3) {
  452. case 0: /* absolute */
  453. found = 1;
  454. break;
  455. case 1: /* andcond */
  456. if (!(hp0 & args[j + 1]))
  457. found = 1;
  458. break;
  459. case 2: /* AVPN */
  460. if ((hp0 & ~0x7fUL) == args[j + 1])
  461. found = 1;
  462. break;
  463. }
  464. }
  465. if (!found) {
  466. hp[0] &= ~cpu_to_be64(HPTE_V_HVLOCK);
  467. args[j] = ((0x90 | flags) << 56) + pte_index;
  468. continue;
  469. }
  470. args[j] = ((0x80 | flags) << 56) + pte_index;
  471. rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
  472. note_hpte_modification(kvm, rev);
  473. if (!(hp0 & HPTE_V_VALID)) {
  474. /* insert R and C bits from PTE */
  475. rcbits = rev->guest_rpte & (HPTE_R_R|HPTE_R_C);
  476. args[j] |= rcbits << (56 - 5);
  477. hp[0] = 0;
  478. continue;
  479. }
  480. /* leave it locked */
  481. hp[0] &= ~cpu_to_be64(HPTE_V_VALID);
  482. tlbrb[n] = compute_tlbie_rb(be64_to_cpu(hp[0]),
  483. be64_to_cpu(hp[1]), pte_index);
  484. indexes[n] = j;
  485. hptes[n] = hp;
  486. revs[n] = rev;
  487. ++n;
  488. }
  489. if (!n)
  490. break;
  491. /* Now that we've collected a batch, do the tlbies */
  492. do_tlbies(kvm, tlbrb, n, global, true);
  493. /* Read PTE low words after tlbie to get final R/C values */
  494. for (k = 0; k < n; ++k) {
  495. j = indexes[k];
  496. pte_index = args[j] & ((1ul << 56) - 1);
  497. hp = hptes[k];
  498. rev = revs[k];
  499. remove_revmap_chain(kvm, pte_index, rev,
  500. be64_to_cpu(hp[0]), be64_to_cpu(hp[1]));
  501. rcbits = rev->guest_rpte & (HPTE_R_R|HPTE_R_C);
  502. args[j] |= rcbits << (56 - 5);
  503. __unlock_hpte(hp, 0);
  504. }
  505. }
  506. return ret;
  507. }
  508. long kvmppc_h_protect(struct kvm_vcpu *vcpu, unsigned long flags,
  509. unsigned long pte_index, unsigned long avpn,
  510. unsigned long va)
  511. {
  512. struct kvm *kvm = vcpu->kvm;
  513. __be64 *hpte;
  514. struct revmap_entry *rev;
  515. unsigned long v, r, rb, mask, bits;
  516. u64 pte;
  517. if (pte_index >= kvm->arch.hpt_npte)
  518. return H_PARAMETER;
  519. hpte = (__be64 *)(kvm->arch.hpt_virt + (pte_index << 4));
  520. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  521. cpu_relax();
  522. pte = be64_to_cpu(hpte[0]);
  523. if ((pte & (HPTE_V_ABSENT | HPTE_V_VALID)) == 0 ||
  524. ((flags & H_AVPN) && (pte & ~0x7fUL) != avpn)) {
  525. __unlock_hpte(hpte, pte);
  526. return H_NOT_FOUND;
  527. }
  528. v = pte;
  529. bits = (flags << 55) & HPTE_R_PP0;
  530. bits |= (flags << 48) & HPTE_R_KEY_HI;
  531. bits |= flags & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO);
  532. /* Update guest view of 2nd HPTE dword */
  533. mask = HPTE_R_PP0 | HPTE_R_PP | HPTE_R_N |
  534. HPTE_R_KEY_HI | HPTE_R_KEY_LO;
  535. rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
  536. if (rev) {
  537. r = (rev->guest_rpte & ~mask) | bits;
  538. rev->guest_rpte = r;
  539. note_hpte_modification(kvm, rev);
  540. }
  541. /* Update HPTE */
  542. if (v & HPTE_V_VALID) {
  543. /*
  544. * If the page is valid, don't let it transition from
  545. * readonly to writable. If it should be writable, we'll
  546. * take a trap and let the page fault code sort it out.
  547. */
  548. pte = be64_to_cpu(hpte[1]);
  549. r = (pte & ~mask) | bits;
  550. if (hpte_is_writable(r) && !hpte_is_writable(pte))
  551. r = hpte_make_readonly(r);
  552. /* If the PTE is changing, invalidate it first */
  553. if (r != pte) {
  554. rb = compute_tlbie_rb(v, r, pte_index);
  555. hpte[0] = cpu_to_be64((v & ~HPTE_V_VALID) |
  556. HPTE_V_ABSENT);
  557. do_tlbies(kvm, &rb, 1, global_invalidates(kvm, flags),
  558. true);
  559. hpte[1] = cpu_to_be64(r);
  560. }
  561. }
  562. unlock_hpte(hpte, v & ~HPTE_V_HVLOCK);
  563. asm volatile("ptesync" : : : "memory");
  564. return H_SUCCESS;
  565. }
  566. long kvmppc_h_read(struct kvm_vcpu *vcpu, unsigned long flags,
  567. unsigned long pte_index)
  568. {
  569. struct kvm *kvm = vcpu->kvm;
  570. __be64 *hpte;
  571. unsigned long v, r;
  572. int i, n = 1;
  573. struct revmap_entry *rev = NULL;
  574. if (pte_index >= kvm->arch.hpt_npte)
  575. return H_PARAMETER;
  576. if (flags & H_READ_4) {
  577. pte_index &= ~3;
  578. n = 4;
  579. }
  580. rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
  581. for (i = 0; i < n; ++i, ++pte_index) {
  582. hpte = (__be64 *)(kvm->arch.hpt_virt + (pte_index << 4));
  583. v = be64_to_cpu(hpte[0]) & ~HPTE_V_HVLOCK;
  584. r = be64_to_cpu(hpte[1]);
  585. if (v & HPTE_V_ABSENT) {
  586. v &= ~HPTE_V_ABSENT;
  587. v |= HPTE_V_VALID;
  588. }
  589. if (v & HPTE_V_VALID) {
  590. r = rev[i].guest_rpte | (r & (HPTE_R_R | HPTE_R_C));
  591. r &= ~HPTE_GR_RESERVED;
  592. }
  593. vcpu->arch.gpr[4 + i * 2] = v;
  594. vcpu->arch.gpr[5 + i * 2] = r;
  595. }
  596. return H_SUCCESS;
  597. }
  598. void kvmppc_invalidate_hpte(struct kvm *kvm, __be64 *hptep,
  599. unsigned long pte_index)
  600. {
  601. unsigned long rb;
  602. hptep[0] &= ~cpu_to_be64(HPTE_V_VALID);
  603. rb = compute_tlbie_rb(be64_to_cpu(hptep[0]), be64_to_cpu(hptep[1]),
  604. pte_index);
  605. do_tlbies(kvm, &rb, 1, 1, true);
  606. }
  607. EXPORT_SYMBOL_GPL(kvmppc_invalidate_hpte);
  608. void kvmppc_clear_ref_hpte(struct kvm *kvm, __be64 *hptep,
  609. unsigned long pte_index)
  610. {
  611. unsigned long rb;
  612. unsigned char rbyte;
  613. rb = compute_tlbie_rb(be64_to_cpu(hptep[0]), be64_to_cpu(hptep[1]),
  614. pte_index);
  615. rbyte = (be64_to_cpu(hptep[1]) & ~HPTE_R_R) >> 8;
  616. /* modify only the second-last byte, which contains the ref bit */
  617. *((char *)hptep + 14) = rbyte;
  618. do_tlbies(kvm, &rb, 1, 1, false);
  619. }
  620. EXPORT_SYMBOL_GPL(kvmppc_clear_ref_hpte);
  621. static int slb_base_page_shift[4] = {
  622. 24, /* 16M */
  623. 16, /* 64k */
  624. 34, /* 16G */
  625. 20, /* 1M, unsupported */
  626. };
  627. /* When called from virtmode, this func should be protected by
  628. * preempt_disable(), otherwise, the holding of HPTE_V_HVLOCK
  629. * can trigger deadlock issue.
  630. */
  631. long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr, unsigned long slb_v,
  632. unsigned long valid)
  633. {
  634. unsigned int i;
  635. unsigned int pshift;
  636. unsigned long somask;
  637. unsigned long vsid, hash;
  638. unsigned long avpn;
  639. __be64 *hpte;
  640. unsigned long mask, val;
  641. unsigned long v, r;
  642. /* Get page shift, work out hash and AVPN etc. */
  643. mask = SLB_VSID_B | HPTE_V_AVPN | HPTE_V_SECONDARY;
  644. val = 0;
  645. pshift = 12;
  646. if (slb_v & SLB_VSID_L) {
  647. mask |= HPTE_V_LARGE;
  648. val |= HPTE_V_LARGE;
  649. pshift = slb_base_page_shift[(slb_v & SLB_VSID_LP) >> 4];
  650. }
  651. if (slb_v & SLB_VSID_B_1T) {
  652. somask = (1UL << 40) - 1;
  653. vsid = (slb_v & ~SLB_VSID_B) >> SLB_VSID_SHIFT_1T;
  654. vsid ^= vsid << 25;
  655. } else {
  656. somask = (1UL << 28) - 1;
  657. vsid = (slb_v & ~SLB_VSID_B) >> SLB_VSID_SHIFT;
  658. }
  659. hash = (vsid ^ ((eaddr & somask) >> pshift)) & kvm->arch.hpt_mask;
  660. avpn = slb_v & ~(somask >> 16); /* also includes B */
  661. avpn |= (eaddr & somask) >> 16;
  662. if (pshift >= 24)
  663. avpn &= ~((1UL << (pshift - 16)) - 1);
  664. else
  665. avpn &= ~0x7fUL;
  666. val |= avpn;
  667. for (;;) {
  668. hpte = (__be64 *)(kvm->arch.hpt_virt + (hash << 7));
  669. for (i = 0; i < 16; i += 2) {
  670. /* Read the PTE racily */
  671. v = be64_to_cpu(hpte[i]) & ~HPTE_V_HVLOCK;
  672. /* Check valid/absent, hash, segment size and AVPN */
  673. if (!(v & valid) || (v & mask) != val)
  674. continue;
  675. /* Lock the PTE and read it under the lock */
  676. while (!try_lock_hpte(&hpte[i], HPTE_V_HVLOCK))
  677. cpu_relax();
  678. v = be64_to_cpu(hpte[i]) & ~HPTE_V_HVLOCK;
  679. r = be64_to_cpu(hpte[i+1]);
  680. /*
  681. * Check the HPTE again, including base page size
  682. */
  683. if ((v & valid) && (v & mask) == val &&
  684. hpte_base_page_size(v, r) == (1ul << pshift))
  685. /* Return with the HPTE still locked */
  686. return (hash << 3) + (i >> 1);
  687. __unlock_hpte(&hpte[i], v);
  688. }
  689. if (val & HPTE_V_SECONDARY)
  690. break;
  691. val |= HPTE_V_SECONDARY;
  692. hash = hash ^ kvm->arch.hpt_mask;
  693. }
  694. return -1;
  695. }
  696. EXPORT_SYMBOL(kvmppc_hv_find_lock_hpte);
  697. /*
  698. * Called in real mode to check whether an HPTE not found fault
  699. * is due to accessing a paged-out page or an emulated MMIO page,
  700. * or if a protection fault is due to accessing a page that the
  701. * guest wanted read/write access to but which we made read-only.
  702. * Returns a possibly modified status (DSISR) value if not
  703. * (i.e. pass the interrupt to the guest),
  704. * -1 to pass the fault up to host kernel mode code, -2 to do that
  705. * and also load the instruction word (for MMIO emulation),
  706. * or 0 if we should make the guest retry the access.
  707. */
  708. long kvmppc_hpte_hv_fault(struct kvm_vcpu *vcpu, unsigned long addr,
  709. unsigned long slb_v, unsigned int status, bool data)
  710. {
  711. struct kvm *kvm = vcpu->kvm;
  712. long int index;
  713. unsigned long v, r, gr;
  714. __be64 *hpte;
  715. unsigned long valid;
  716. struct revmap_entry *rev;
  717. unsigned long pp, key;
  718. /* For protection fault, expect to find a valid HPTE */
  719. valid = HPTE_V_VALID;
  720. if (status & DSISR_NOHPTE)
  721. valid |= HPTE_V_ABSENT;
  722. index = kvmppc_hv_find_lock_hpte(kvm, addr, slb_v, valid);
  723. if (index < 0) {
  724. if (status & DSISR_NOHPTE)
  725. return status; /* there really was no HPTE */
  726. return 0; /* for prot fault, HPTE disappeared */
  727. }
  728. hpte = (__be64 *)(kvm->arch.hpt_virt + (index << 4));
  729. v = be64_to_cpu(hpte[0]) & ~HPTE_V_HVLOCK;
  730. r = be64_to_cpu(hpte[1]);
  731. rev = real_vmalloc_addr(&kvm->arch.revmap[index]);
  732. gr = rev->guest_rpte;
  733. unlock_hpte(hpte, v);
  734. /* For not found, if the HPTE is valid by now, retry the instruction */
  735. if ((status & DSISR_NOHPTE) && (v & HPTE_V_VALID))
  736. return 0;
  737. /* Check access permissions to the page */
  738. pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
  739. key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
  740. status &= ~DSISR_NOHPTE; /* DSISR_NOHPTE == SRR1_ISI_NOPT */
  741. if (!data) {
  742. if (gr & (HPTE_R_N | HPTE_R_G))
  743. return status | SRR1_ISI_N_OR_G;
  744. if (!hpte_read_permission(pp, slb_v & key))
  745. return status | SRR1_ISI_PROT;
  746. } else if (status & DSISR_ISSTORE) {
  747. /* check write permission */
  748. if (!hpte_write_permission(pp, slb_v & key))
  749. return status | DSISR_PROTFAULT;
  750. } else {
  751. if (!hpte_read_permission(pp, slb_v & key))
  752. return status | DSISR_PROTFAULT;
  753. }
  754. /* Check storage key, if applicable */
  755. if (data && (vcpu->arch.shregs.msr & MSR_DR)) {
  756. unsigned int perm = hpte_get_skey_perm(gr, vcpu->arch.amr);
  757. if (status & DSISR_ISSTORE)
  758. perm >>= 1;
  759. if (perm & 1)
  760. return status | DSISR_KEYFAULT;
  761. }
  762. /* Save HPTE info for virtual-mode handler */
  763. vcpu->arch.pgfault_addr = addr;
  764. vcpu->arch.pgfault_index = index;
  765. vcpu->arch.pgfault_hpte[0] = v;
  766. vcpu->arch.pgfault_hpte[1] = r;
  767. /* Check the storage key to see if it is possibly emulated MMIO */
  768. if (data && (vcpu->arch.shregs.msr & MSR_IR) &&
  769. (r & (HPTE_R_KEY_HI | HPTE_R_KEY_LO)) ==
  770. (HPTE_R_KEY_HI | HPTE_R_KEY_LO))
  771. return -2; /* MMIO emulation - load instr word */
  772. return -1; /* send fault up to host kernel mode */
  773. }