book3s_hv_rm_mmu.c 27 KB

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