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