book3s_hv_rm_mmu.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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/mmu-hash64.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);
  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. unsigned long is_io;
  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_io = ~0ul;
  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, &hpage_shift);
  202. else {
  203. local_irq_save(irq_flags);
  204. ptep = find_linux_pte_or_hugepte(pgdir, hva, &hpage_shift);
  205. }
  206. if (ptep) {
  207. pte_t pte;
  208. unsigned int host_pte_size;
  209. if (hpage_shift)
  210. host_pte_size = 1ul << hpage_shift;
  211. else
  212. host_pte_size = PAGE_SIZE;
  213. /*
  214. * We should always find the guest page size
  215. * to <= host page size, if host is using hugepage
  216. */
  217. if (host_pte_size < psize) {
  218. if (!realmode)
  219. local_irq_restore(flags);
  220. return H_PARAMETER;
  221. }
  222. pte = kvmppc_read_update_linux_pte(ptep, writing);
  223. if (pte_present(pte) && !pte_protnone(pte)) {
  224. if (writing && !pte_write(pte))
  225. /* make the actual HPTE be read-only */
  226. ptel = hpte_make_readonly(ptel);
  227. is_io = hpte_cache_bits(pte_val(pte));
  228. pa = pte_pfn(pte) << PAGE_SHIFT;
  229. pa |= hva & (host_pte_size - 1);
  230. pa |= gpa & ~PAGE_MASK;
  231. }
  232. }
  233. if (!realmode)
  234. local_irq_restore(irq_flags);
  235. ptel &= ~(HPTE_R_PP0 - psize);
  236. ptel |= pa;
  237. if (pa)
  238. pteh |= HPTE_V_VALID;
  239. else
  240. pteh |= HPTE_V_ABSENT;
  241. /* Check WIMG */
  242. if (is_io != ~0ul && !hpte_cache_flags_ok(ptel, is_io)) {
  243. if (is_io)
  244. return H_PARAMETER;
  245. /*
  246. * Allow guest to map emulated device memory as
  247. * uncacheable, but actually make it cacheable.
  248. */
  249. ptel &= ~(HPTE_R_W|HPTE_R_I|HPTE_R_G);
  250. ptel |= HPTE_R_M;
  251. }
  252. /* Find and lock the HPTEG slot to use */
  253. do_insert:
  254. if (pte_index >= kvm->arch.hpt_npte)
  255. return H_PARAMETER;
  256. if (likely((flags & H_EXACT) == 0)) {
  257. pte_index &= ~7UL;
  258. hpte = (__be64 *)(kvm->arch.hpt_virt + (pte_index << 4));
  259. for (i = 0; i < 8; ++i) {
  260. if ((be64_to_cpu(*hpte) & HPTE_V_VALID) == 0 &&
  261. try_lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID |
  262. HPTE_V_ABSENT))
  263. break;
  264. hpte += 2;
  265. }
  266. if (i == 8) {
  267. /*
  268. * Since try_lock_hpte doesn't retry (not even stdcx.
  269. * failures), it could be that there is a free slot
  270. * but we transiently failed to lock it. Try again,
  271. * actually locking each slot and checking it.
  272. */
  273. hpte -= 16;
  274. for (i = 0; i < 8; ++i) {
  275. u64 pte;
  276. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  277. cpu_relax();
  278. pte = be64_to_cpu(hpte[0]);
  279. if (!(pte & (HPTE_V_VALID | HPTE_V_ABSENT)))
  280. break;
  281. __unlock_hpte(hpte, pte);
  282. hpte += 2;
  283. }
  284. if (i == 8)
  285. return H_PTEG_FULL;
  286. }
  287. pte_index += i;
  288. } else {
  289. hpte = (__be64 *)(kvm->arch.hpt_virt + (pte_index << 4));
  290. if (!try_lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID |
  291. HPTE_V_ABSENT)) {
  292. /* Lock the slot and check again */
  293. u64 pte;
  294. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  295. cpu_relax();
  296. pte = be64_to_cpu(hpte[0]);
  297. if (pte & (HPTE_V_VALID | HPTE_V_ABSENT)) {
  298. __unlock_hpte(hpte, pte);
  299. return H_PTEG_FULL;
  300. }
  301. }
  302. }
  303. /* Save away the guest's idea of the second HPTE dword */
  304. rev = &kvm->arch.revmap[pte_index];
  305. if (realmode)
  306. rev = real_vmalloc_addr(rev);
  307. if (rev) {
  308. rev->guest_rpte = g_ptel;
  309. note_hpte_modification(kvm, rev);
  310. }
  311. /* Link HPTE into reverse-map chain */
  312. if (pteh & HPTE_V_VALID) {
  313. if (realmode)
  314. rmap = real_vmalloc_addr(rmap);
  315. lock_rmap(rmap);
  316. /* Check for pending invalidations under the rmap chain lock */
  317. if (mmu_notifier_retry(kvm, mmu_seq)) {
  318. /* inval in progress, write a non-present HPTE */
  319. pteh |= HPTE_V_ABSENT;
  320. pteh &= ~HPTE_V_VALID;
  321. unlock_rmap(rmap);
  322. } else {
  323. kvmppc_add_revmap_chain(kvm, rev, rmap, pte_index,
  324. realmode);
  325. /* Only set R/C in real HPTE if already set in *rmap */
  326. rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
  327. ptel &= rcbits | ~(HPTE_R_R | HPTE_R_C);
  328. }
  329. }
  330. hpte[1] = cpu_to_be64(ptel);
  331. /* Write the first HPTE dword, unlocking the HPTE and making it valid */
  332. eieio();
  333. __unlock_hpte(hpte, pteh);
  334. asm volatile("ptesync" : : : "memory");
  335. *pte_idx_ret = pte_index;
  336. return H_SUCCESS;
  337. }
  338. EXPORT_SYMBOL_GPL(kvmppc_do_h_enter);
  339. long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
  340. long pte_index, unsigned long pteh, unsigned long ptel)
  341. {
  342. return kvmppc_do_h_enter(vcpu->kvm, flags, pte_index, pteh, ptel,
  343. vcpu->arch.pgdir, true, &vcpu->arch.gpr[4]);
  344. }
  345. #ifdef __BIG_ENDIAN__
  346. #define LOCK_TOKEN (*(u32 *)(&get_paca()->lock_token))
  347. #else
  348. #define LOCK_TOKEN (*(u32 *)(&get_paca()->paca_index))
  349. #endif
  350. static inline int try_lock_tlbie(unsigned int *lock)
  351. {
  352. unsigned int tmp, old;
  353. unsigned int token = LOCK_TOKEN;
  354. asm volatile("1:lwarx %1,0,%2\n"
  355. " cmpwi cr0,%1,0\n"
  356. " bne 2f\n"
  357. " stwcx. %3,0,%2\n"
  358. " bne- 1b\n"
  359. " isync\n"
  360. "2:"
  361. : "=&r" (tmp), "=&r" (old)
  362. : "r" (lock), "r" (token)
  363. : "cc", "memory");
  364. return old == 0;
  365. }
  366. static void do_tlbies(struct kvm *kvm, unsigned long *rbvalues,
  367. long npages, int global, bool need_sync)
  368. {
  369. long i;
  370. if (global) {
  371. while (!try_lock_tlbie(&kvm->arch.tlbie_lock))
  372. cpu_relax();
  373. if (need_sync)
  374. asm volatile("ptesync" : : : "memory");
  375. for (i = 0; i < npages; ++i)
  376. asm volatile(PPC_TLBIE(%1,%0) : :
  377. "r" (rbvalues[i]), "r" (kvm->arch.lpid));
  378. asm volatile("eieio; tlbsync; ptesync" : : : "memory");
  379. kvm->arch.tlbie_lock = 0;
  380. } else {
  381. if (need_sync)
  382. asm volatile("ptesync" : : : "memory");
  383. for (i = 0; i < npages; ++i)
  384. asm volatile("tlbiel %0" : : "r" (rbvalues[i]));
  385. asm volatile("ptesync" : : : "memory");
  386. }
  387. }
  388. long kvmppc_do_h_remove(struct kvm *kvm, unsigned long flags,
  389. unsigned long pte_index, unsigned long avpn,
  390. unsigned long *hpret)
  391. {
  392. __be64 *hpte;
  393. unsigned long v, r, rb;
  394. struct revmap_entry *rev;
  395. u64 pte;
  396. if (pte_index >= kvm->arch.hpt_npte)
  397. return H_PARAMETER;
  398. hpte = (__be64 *)(kvm->arch.hpt_virt + (pte_index << 4));
  399. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  400. cpu_relax();
  401. pte = be64_to_cpu(hpte[0]);
  402. if ((pte & (HPTE_V_ABSENT | HPTE_V_VALID)) == 0 ||
  403. ((flags & H_AVPN) && (pte & ~0x7fUL) != avpn) ||
  404. ((flags & H_ANDCOND) && (pte & avpn) != 0)) {
  405. __unlock_hpte(hpte, pte);
  406. return H_NOT_FOUND;
  407. }
  408. rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
  409. v = pte & ~HPTE_V_HVLOCK;
  410. if (v & HPTE_V_VALID) {
  411. hpte[0] &= ~cpu_to_be64(HPTE_V_VALID);
  412. rb = compute_tlbie_rb(v, be64_to_cpu(hpte[1]), pte_index);
  413. do_tlbies(kvm, &rb, 1, global_invalidates(kvm, flags), true);
  414. /*
  415. * The reference (R) and change (C) bits in a HPT
  416. * entry can be set by hardware at any time up until
  417. * the HPTE is invalidated and the TLB invalidation
  418. * sequence has completed. This means that when
  419. * removing a HPTE, we need to re-read the HPTE after
  420. * the invalidation sequence has completed in order to
  421. * obtain reliable values of R and C.
  422. */
  423. remove_revmap_chain(kvm, pte_index, rev, v,
  424. be64_to_cpu(hpte[1]));
  425. }
  426. r = rev->guest_rpte & ~HPTE_GR_RESERVED;
  427. note_hpte_modification(kvm, rev);
  428. unlock_hpte(hpte, 0);
  429. hpret[0] = v;
  430. hpret[1] = r;
  431. return H_SUCCESS;
  432. }
  433. EXPORT_SYMBOL_GPL(kvmppc_do_h_remove);
  434. long kvmppc_h_remove(struct kvm_vcpu *vcpu, unsigned long flags,
  435. unsigned long pte_index, unsigned long avpn)
  436. {
  437. return kvmppc_do_h_remove(vcpu->kvm, flags, pte_index, avpn,
  438. &vcpu->arch.gpr[4]);
  439. }
  440. long kvmppc_h_bulk_remove(struct kvm_vcpu *vcpu)
  441. {
  442. struct kvm *kvm = vcpu->kvm;
  443. unsigned long *args = &vcpu->arch.gpr[4];
  444. __be64 *hp, *hptes[4];
  445. unsigned long tlbrb[4];
  446. long int i, j, k, n, found, indexes[4];
  447. unsigned long flags, req, pte_index, rcbits;
  448. int global;
  449. long int ret = H_SUCCESS;
  450. struct revmap_entry *rev, *revs[4];
  451. u64 hp0;
  452. global = global_invalidates(kvm, 0);
  453. for (i = 0; i < 4 && ret == H_SUCCESS; ) {
  454. n = 0;
  455. for (; i < 4; ++i) {
  456. j = i * 2;
  457. pte_index = args[j];
  458. flags = pte_index >> 56;
  459. pte_index &= ((1ul << 56) - 1);
  460. req = flags >> 6;
  461. flags &= 3;
  462. if (req == 3) { /* no more requests */
  463. i = 4;
  464. break;
  465. }
  466. if (req != 1 || flags == 3 ||
  467. pte_index >= kvm->arch.hpt_npte) {
  468. /* parameter error */
  469. args[j] = ((0xa0 | flags) << 56) + pte_index;
  470. ret = H_PARAMETER;
  471. break;
  472. }
  473. hp = (__be64 *) (kvm->arch.hpt_virt + (pte_index << 4));
  474. /* to avoid deadlock, don't spin except for first */
  475. if (!try_lock_hpte(hp, HPTE_V_HVLOCK)) {
  476. if (n)
  477. break;
  478. while (!try_lock_hpte(hp, HPTE_V_HVLOCK))
  479. cpu_relax();
  480. }
  481. found = 0;
  482. hp0 = be64_to_cpu(hp[0]);
  483. if (hp0 & (HPTE_V_ABSENT | HPTE_V_VALID)) {
  484. switch (flags & 3) {
  485. case 0: /* absolute */
  486. found = 1;
  487. break;
  488. case 1: /* andcond */
  489. if (!(hp0 & args[j + 1]))
  490. found = 1;
  491. break;
  492. case 2: /* AVPN */
  493. if ((hp0 & ~0x7fUL) == args[j + 1])
  494. found = 1;
  495. break;
  496. }
  497. }
  498. if (!found) {
  499. hp[0] &= ~cpu_to_be64(HPTE_V_HVLOCK);
  500. args[j] = ((0x90 | flags) << 56) + pte_index;
  501. continue;
  502. }
  503. args[j] = ((0x80 | flags) << 56) + pte_index;
  504. rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
  505. note_hpte_modification(kvm, rev);
  506. if (!(hp0 & HPTE_V_VALID)) {
  507. /* insert R and C bits from PTE */
  508. rcbits = rev->guest_rpte & (HPTE_R_R|HPTE_R_C);
  509. args[j] |= rcbits << (56 - 5);
  510. hp[0] = 0;
  511. continue;
  512. }
  513. /* leave it locked */
  514. hp[0] &= ~cpu_to_be64(HPTE_V_VALID);
  515. tlbrb[n] = compute_tlbie_rb(be64_to_cpu(hp[0]),
  516. be64_to_cpu(hp[1]), pte_index);
  517. indexes[n] = j;
  518. hptes[n] = hp;
  519. revs[n] = rev;
  520. ++n;
  521. }
  522. if (!n)
  523. break;
  524. /* Now that we've collected a batch, do the tlbies */
  525. do_tlbies(kvm, tlbrb, n, global, true);
  526. /* Read PTE low words after tlbie to get final R/C values */
  527. for (k = 0; k < n; ++k) {
  528. j = indexes[k];
  529. pte_index = args[j] & ((1ul << 56) - 1);
  530. hp = hptes[k];
  531. rev = revs[k];
  532. remove_revmap_chain(kvm, pte_index, rev,
  533. be64_to_cpu(hp[0]), be64_to_cpu(hp[1]));
  534. rcbits = rev->guest_rpte & (HPTE_R_R|HPTE_R_C);
  535. args[j] |= rcbits << (56 - 5);
  536. __unlock_hpte(hp, 0);
  537. }
  538. }
  539. return ret;
  540. }
  541. long kvmppc_h_protect(struct kvm_vcpu *vcpu, unsigned long flags,
  542. unsigned long pte_index, unsigned long avpn,
  543. unsigned long va)
  544. {
  545. struct kvm *kvm = vcpu->kvm;
  546. __be64 *hpte;
  547. struct revmap_entry *rev;
  548. unsigned long v, r, rb, mask, bits;
  549. u64 pte;
  550. if (pte_index >= kvm->arch.hpt_npte)
  551. return H_PARAMETER;
  552. hpte = (__be64 *)(kvm->arch.hpt_virt + (pte_index << 4));
  553. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  554. cpu_relax();
  555. pte = be64_to_cpu(hpte[0]);
  556. if ((pte & (HPTE_V_ABSENT | HPTE_V_VALID)) == 0 ||
  557. ((flags & H_AVPN) && (pte & ~0x7fUL) != avpn)) {
  558. __unlock_hpte(hpte, pte);
  559. return H_NOT_FOUND;
  560. }
  561. v = pte;
  562. bits = (flags << 55) & HPTE_R_PP0;
  563. bits |= (flags << 48) & HPTE_R_KEY_HI;
  564. bits |= flags & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO);
  565. /* Update guest view of 2nd HPTE dword */
  566. mask = HPTE_R_PP0 | HPTE_R_PP | HPTE_R_N |
  567. HPTE_R_KEY_HI | HPTE_R_KEY_LO;
  568. rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
  569. if (rev) {
  570. r = (rev->guest_rpte & ~mask) | bits;
  571. rev->guest_rpte = r;
  572. note_hpte_modification(kvm, rev);
  573. }
  574. /* Update HPTE */
  575. if (v & HPTE_V_VALID) {
  576. /*
  577. * If the page is valid, don't let it transition from
  578. * readonly to writable. If it should be writable, we'll
  579. * take a trap and let the page fault code sort it out.
  580. */
  581. pte = be64_to_cpu(hpte[1]);
  582. r = (pte & ~mask) | bits;
  583. if (hpte_is_writable(r) && !hpte_is_writable(pte))
  584. r = hpte_make_readonly(r);
  585. /* If the PTE is changing, invalidate it first */
  586. if (r != pte) {
  587. rb = compute_tlbie_rb(v, r, pte_index);
  588. hpte[0] = cpu_to_be64((v & ~HPTE_V_VALID) |
  589. HPTE_V_ABSENT);
  590. do_tlbies(kvm, &rb, 1, global_invalidates(kvm, flags),
  591. true);
  592. hpte[1] = cpu_to_be64(r);
  593. }
  594. }
  595. unlock_hpte(hpte, v & ~HPTE_V_HVLOCK);
  596. asm volatile("ptesync" : : : "memory");
  597. return H_SUCCESS;
  598. }
  599. long kvmppc_h_read(struct kvm_vcpu *vcpu, unsigned long flags,
  600. unsigned long pte_index)
  601. {
  602. struct kvm *kvm = vcpu->kvm;
  603. __be64 *hpte;
  604. unsigned long v, r;
  605. int i, n = 1;
  606. struct revmap_entry *rev = NULL;
  607. if (pte_index >= kvm->arch.hpt_npte)
  608. return H_PARAMETER;
  609. if (flags & H_READ_4) {
  610. pte_index &= ~3;
  611. n = 4;
  612. }
  613. rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
  614. for (i = 0; i < n; ++i, ++pte_index) {
  615. hpte = (__be64 *)(kvm->arch.hpt_virt + (pte_index << 4));
  616. v = be64_to_cpu(hpte[0]) & ~HPTE_V_HVLOCK;
  617. r = be64_to_cpu(hpte[1]);
  618. if (v & HPTE_V_ABSENT) {
  619. v &= ~HPTE_V_ABSENT;
  620. v |= HPTE_V_VALID;
  621. }
  622. if (v & HPTE_V_VALID) {
  623. r = rev[i].guest_rpte | (r & (HPTE_R_R | HPTE_R_C));
  624. r &= ~HPTE_GR_RESERVED;
  625. }
  626. vcpu->arch.gpr[4 + i * 2] = v;
  627. vcpu->arch.gpr[5 + i * 2] = r;
  628. }
  629. return H_SUCCESS;
  630. }
  631. long kvmppc_h_clear_ref(struct kvm_vcpu *vcpu, unsigned long flags,
  632. unsigned long pte_index)
  633. {
  634. struct kvm *kvm = vcpu->kvm;
  635. __be64 *hpte;
  636. unsigned long v, r, gr;
  637. struct revmap_entry *rev;
  638. unsigned long *rmap;
  639. long ret = H_NOT_FOUND;
  640. if (pte_index >= kvm->arch.hpt_npte)
  641. return H_PARAMETER;
  642. rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
  643. hpte = (__be64 *)(kvm->arch.hpt_virt + (pte_index << 4));
  644. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  645. cpu_relax();
  646. v = be64_to_cpu(hpte[0]);
  647. r = be64_to_cpu(hpte[1]);
  648. if (!(v & (HPTE_V_VALID | HPTE_V_ABSENT)))
  649. goto out;
  650. gr = rev->guest_rpte;
  651. if (rev->guest_rpte & HPTE_R_R) {
  652. rev->guest_rpte &= ~HPTE_R_R;
  653. note_hpte_modification(kvm, rev);
  654. }
  655. if (v & HPTE_V_VALID) {
  656. gr |= r & (HPTE_R_R | HPTE_R_C);
  657. if (r & HPTE_R_R) {
  658. kvmppc_clear_ref_hpte(kvm, hpte, pte_index);
  659. rmap = revmap_for_hpte(kvm, v, gr);
  660. if (rmap) {
  661. lock_rmap(rmap);
  662. *rmap |= KVMPPC_RMAP_REFERENCED;
  663. unlock_rmap(rmap);
  664. }
  665. }
  666. }
  667. vcpu->arch.gpr[4] = gr;
  668. ret = H_SUCCESS;
  669. out:
  670. unlock_hpte(hpte, v & ~HPTE_V_HVLOCK);
  671. return ret;
  672. }
  673. long kvmppc_h_clear_mod(struct kvm_vcpu *vcpu, unsigned long flags,
  674. unsigned long pte_index)
  675. {
  676. struct kvm *kvm = vcpu->kvm;
  677. __be64 *hpte;
  678. unsigned long v, r, gr;
  679. struct revmap_entry *rev;
  680. unsigned long *rmap;
  681. long ret = H_NOT_FOUND;
  682. if (pte_index >= kvm->arch.hpt_npte)
  683. return H_PARAMETER;
  684. rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
  685. hpte = (__be64 *)(kvm->arch.hpt_virt + (pte_index << 4));
  686. while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
  687. cpu_relax();
  688. v = be64_to_cpu(hpte[0]);
  689. r = be64_to_cpu(hpte[1]);
  690. if (!(v & (HPTE_V_VALID | HPTE_V_ABSENT)))
  691. goto out;
  692. gr = rev->guest_rpte;
  693. if (gr & HPTE_R_C) {
  694. rev->guest_rpte &= ~HPTE_R_C;
  695. note_hpte_modification(kvm, rev);
  696. }
  697. if (v & HPTE_V_VALID) {
  698. /* need to make it temporarily absent so C is stable */
  699. hpte[0] |= cpu_to_be64(HPTE_V_ABSENT);
  700. kvmppc_invalidate_hpte(kvm, hpte, pte_index);
  701. r = be64_to_cpu(hpte[1]);
  702. gr |= r & (HPTE_R_R | HPTE_R_C);
  703. if (r & HPTE_R_C) {
  704. unsigned long psize = hpte_page_size(v, r);
  705. hpte[1] = cpu_to_be64(r & ~HPTE_R_C);
  706. eieio();
  707. rmap = revmap_for_hpte(kvm, v, gr);
  708. if (rmap) {
  709. lock_rmap(rmap);
  710. *rmap |= KVMPPC_RMAP_CHANGED;
  711. kvmppc_update_rmap_change(rmap, psize);
  712. unlock_rmap(rmap);
  713. }
  714. }
  715. }
  716. vcpu->arch.gpr[4] = gr;
  717. ret = H_SUCCESS;
  718. out:
  719. unlock_hpte(hpte, v & ~HPTE_V_HVLOCK);
  720. return ret;
  721. }
  722. void kvmppc_invalidate_hpte(struct kvm *kvm, __be64 *hptep,
  723. unsigned long pte_index)
  724. {
  725. unsigned long rb;
  726. hptep[0] &= ~cpu_to_be64(HPTE_V_VALID);
  727. rb = compute_tlbie_rb(be64_to_cpu(hptep[0]), be64_to_cpu(hptep[1]),
  728. pte_index);
  729. do_tlbies(kvm, &rb, 1, 1, true);
  730. }
  731. EXPORT_SYMBOL_GPL(kvmppc_invalidate_hpte);
  732. void kvmppc_clear_ref_hpte(struct kvm *kvm, __be64 *hptep,
  733. unsigned long pte_index)
  734. {
  735. unsigned long rb;
  736. unsigned char rbyte;
  737. rb = compute_tlbie_rb(be64_to_cpu(hptep[0]), be64_to_cpu(hptep[1]),
  738. pte_index);
  739. rbyte = (be64_to_cpu(hptep[1]) & ~HPTE_R_R) >> 8;
  740. /* modify only the second-last byte, which contains the ref bit */
  741. *((char *)hptep + 14) = rbyte;
  742. do_tlbies(kvm, &rb, 1, 1, false);
  743. }
  744. EXPORT_SYMBOL_GPL(kvmppc_clear_ref_hpte);
  745. static int slb_base_page_shift[4] = {
  746. 24, /* 16M */
  747. 16, /* 64k */
  748. 34, /* 16G */
  749. 20, /* 1M, unsupported */
  750. };
  751. /* When called from virtmode, this func should be protected by
  752. * preempt_disable(), otherwise, the holding of HPTE_V_HVLOCK
  753. * can trigger deadlock issue.
  754. */
  755. long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr, unsigned long slb_v,
  756. unsigned long valid)
  757. {
  758. unsigned int i;
  759. unsigned int pshift;
  760. unsigned long somask;
  761. unsigned long vsid, hash;
  762. unsigned long avpn;
  763. __be64 *hpte;
  764. unsigned long mask, val;
  765. unsigned long v, r;
  766. /* Get page shift, work out hash and AVPN etc. */
  767. mask = SLB_VSID_B | HPTE_V_AVPN | HPTE_V_SECONDARY;
  768. val = 0;
  769. pshift = 12;
  770. if (slb_v & SLB_VSID_L) {
  771. mask |= HPTE_V_LARGE;
  772. val |= HPTE_V_LARGE;
  773. pshift = slb_base_page_shift[(slb_v & SLB_VSID_LP) >> 4];
  774. }
  775. if (slb_v & SLB_VSID_B_1T) {
  776. somask = (1UL << 40) - 1;
  777. vsid = (slb_v & ~SLB_VSID_B) >> SLB_VSID_SHIFT_1T;
  778. vsid ^= vsid << 25;
  779. } else {
  780. somask = (1UL << 28) - 1;
  781. vsid = (slb_v & ~SLB_VSID_B) >> SLB_VSID_SHIFT;
  782. }
  783. hash = (vsid ^ ((eaddr & somask) >> pshift)) & kvm->arch.hpt_mask;
  784. avpn = slb_v & ~(somask >> 16); /* also includes B */
  785. avpn |= (eaddr & somask) >> 16;
  786. if (pshift >= 24)
  787. avpn &= ~((1UL << (pshift - 16)) - 1);
  788. else
  789. avpn &= ~0x7fUL;
  790. val |= avpn;
  791. for (;;) {
  792. hpte = (__be64 *)(kvm->arch.hpt_virt + (hash << 7));
  793. for (i = 0; i < 16; i += 2) {
  794. /* Read the PTE racily */
  795. v = be64_to_cpu(hpte[i]) & ~HPTE_V_HVLOCK;
  796. /* Check valid/absent, hash, segment size and AVPN */
  797. if (!(v & valid) || (v & mask) != val)
  798. continue;
  799. /* Lock the PTE and read it under the lock */
  800. while (!try_lock_hpte(&hpte[i], HPTE_V_HVLOCK))
  801. cpu_relax();
  802. v = be64_to_cpu(hpte[i]) & ~HPTE_V_HVLOCK;
  803. r = be64_to_cpu(hpte[i+1]);
  804. /*
  805. * Check the HPTE again, including base page size
  806. */
  807. if ((v & valid) && (v & mask) == val &&
  808. hpte_base_page_size(v, r) == (1ul << pshift))
  809. /* Return with the HPTE still locked */
  810. return (hash << 3) + (i >> 1);
  811. __unlock_hpte(&hpte[i], v);
  812. }
  813. if (val & HPTE_V_SECONDARY)
  814. break;
  815. val |= HPTE_V_SECONDARY;
  816. hash = hash ^ kvm->arch.hpt_mask;
  817. }
  818. return -1;
  819. }
  820. EXPORT_SYMBOL(kvmppc_hv_find_lock_hpte);
  821. /*
  822. * Called in real mode to check whether an HPTE not found fault
  823. * is due to accessing a paged-out page or an emulated MMIO page,
  824. * or if a protection fault is due to accessing a page that the
  825. * guest wanted read/write access to but which we made read-only.
  826. * Returns a possibly modified status (DSISR) value if not
  827. * (i.e. pass the interrupt to the guest),
  828. * -1 to pass the fault up to host kernel mode code, -2 to do that
  829. * and also load the instruction word (for MMIO emulation),
  830. * or 0 if we should make the guest retry the access.
  831. */
  832. long kvmppc_hpte_hv_fault(struct kvm_vcpu *vcpu, unsigned long addr,
  833. unsigned long slb_v, unsigned int status, bool data)
  834. {
  835. struct kvm *kvm = vcpu->kvm;
  836. long int index;
  837. unsigned long v, r, gr;
  838. __be64 *hpte;
  839. unsigned long valid;
  840. struct revmap_entry *rev;
  841. unsigned long pp, key;
  842. /* For protection fault, expect to find a valid HPTE */
  843. valid = HPTE_V_VALID;
  844. if (status & DSISR_NOHPTE)
  845. valid |= HPTE_V_ABSENT;
  846. index = kvmppc_hv_find_lock_hpte(kvm, addr, slb_v, valid);
  847. if (index < 0) {
  848. if (status & DSISR_NOHPTE)
  849. return status; /* there really was no HPTE */
  850. return 0; /* for prot fault, HPTE disappeared */
  851. }
  852. hpte = (__be64 *)(kvm->arch.hpt_virt + (index << 4));
  853. v = be64_to_cpu(hpte[0]) & ~HPTE_V_HVLOCK;
  854. r = be64_to_cpu(hpte[1]);
  855. rev = real_vmalloc_addr(&kvm->arch.revmap[index]);
  856. gr = rev->guest_rpte;
  857. unlock_hpte(hpte, v);
  858. /* For not found, if the HPTE is valid by now, retry the instruction */
  859. if ((status & DSISR_NOHPTE) && (v & HPTE_V_VALID))
  860. return 0;
  861. /* Check access permissions to the page */
  862. pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
  863. key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
  864. status &= ~DSISR_NOHPTE; /* DSISR_NOHPTE == SRR1_ISI_NOPT */
  865. if (!data) {
  866. if (gr & (HPTE_R_N | HPTE_R_G))
  867. return status | SRR1_ISI_N_OR_G;
  868. if (!hpte_read_permission(pp, slb_v & key))
  869. return status | SRR1_ISI_PROT;
  870. } else if (status & DSISR_ISSTORE) {
  871. /* check write permission */
  872. if (!hpte_write_permission(pp, slb_v & key))
  873. return status | DSISR_PROTFAULT;
  874. } else {
  875. if (!hpte_read_permission(pp, slb_v & key))
  876. return status | DSISR_PROTFAULT;
  877. }
  878. /* Check storage key, if applicable */
  879. if (data && (vcpu->arch.shregs.msr & MSR_DR)) {
  880. unsigned int perm = hpte_get_skey_perm(gr, vcpu->arch.amr);
  881. if (status & DSISR_ISSTORE)
  882. perm >>= 1;
  883. if (perm & 1)
  884. return status | DSISR_KEYFAULT;
  885. }
  886. /* Save HPTE info for virtual-mode handler */
  887. vcpu->arch.pgfault_addr = addr;
  888. vcpu->arch.pgfault_index = index;
  889. vcpu->arch.pgfault_hpte[0] = v;
  890. vcpu->arch.pgfault_hpte[1] = r;
  891. /* Check the storage key to see if it is possibly emulated MMIO */
  892. if (data && (vcpu->arch.shregs.msr & MSR_IR) &&
  893. (r & (HPTE_R_KEY_HI | HPTE_R_KEY_LO)) ==
  894. (HPTE_R_KEY_HI | HPTE_R_KEY_LO))
  895. return -2; /* MMIO emulation - load instr word */
  896. return -1; /* send fault up to host kernel mode */
  897. }