KVM: PPC: Book3S HV: Return ENODEV error rather than EIO
[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;
29
12bc9f6f 30 p = find_linux_pte_or_hugepte(swapper_pg_dir, addr, NULL);
8936dda4
PM
31 if (!p || !pte_present(*p))
32 return NULL;
33 /* assume we don't have huge pages in vmalloc space... */
34 addr = (pte_pfn(*p) << PAGE_SHIFT) | (addr & ~PAGE_MASK);
35 return __va(addr);
36}
a8606e20 37
1b400ba0
PM
38/* Return 1 if we need to do a global tlbie, 0 if we can use tlbiel */
39static int global_invalidates(struct kvm *kvm, unsigned long flags)
40{
41 int global;
42
43 /*
44 * If there is only one vcore, and it's currently running,
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 * If we're not using MMU notifiers, we never take pages away
48 * from the guest, so we can use tlbiel if requested.
49 * Otherwise, don't use tlbiel.
50 */
51 if (kvm->arch.online_vcores == 1 && local_paca->kvm_hstate.kvm_vcore)
52 global = 0;
53 else if (kvm->arch.using_mmu_notifiers)
54 global = 1;
55 else
56 global = !(flags & H_LOCAL);
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));
9d4cba7f 114 memslot = __gfn_to_memslot(kvm_memslots(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
7c85e6b3 137static pte_t lookup_linux_pte_and_update(pgd_t *pgdir, unsigned long hva,
4cf302bc 138 int writing, unsigned long *pte_sizep)
342d3db7
PM
139{
140 pte_t *ptep;
141 unsigned long ps = *pte_sizep;
db7cb5b9 142 unsigned int hugepage_shift;
342d3db7 143
db7cb5b9 144 ptep = find_linux_pte_or_hugepte(pgdir, hva, &hugepage_shift);
342d3db7
PM
145 if (!ptep)
146 return __pte(0);
db7cb5b9
AK
147 if (hugepage_shift)
148 *pte_sizep = 1ul << hugepage_shift;
342d3db7
PM
149 else
150 *pte_sizep = PAGE_SIZE;
151 if (ps > *pte_sizep)
152 return __pte(0);
db7cb5b9 153 return kvmppc_read_update_linux_pte(ptep, writing, hugepage_shift);
342d3db7
PM
154}
155
a92bce95
PM
156static inline void unlock_hpte(unsigned long *hpte, unsigned long hpte_v)
157{
158 asm volatile(PPC_RELEASE_BARRIER "" : : : "memory");
159 hpte[0] = hpte_v;
160}
161
7ed661bf
PM
162long kvmppc_do_h_enter(struct kvm *kvm, unsigned long flags,
163 long pte_index, unsigned long pteh, unsigned long ptel,
164 pgd_t *pgdir, bool realmode, unsigned long *pte_idx_ret)
a8606e20 165{
c77162de 166 unsigned long i, pa, gpa, gfn, psize;
342d3db7 167 unsigned long slot_fn, hva;
a8606e20 168 unsigned long *hpte;
8936dda4 169 struct revmap_entry *rev;
44e5f6be 170 unsigned long g_ptel;
b2b2f165 171 struct kvm_memory_slot *memslot;
c77162de 172 unsigned long *physp, pte_size;
9d0ef5ea 173 unsigned long is_io;
06ce2c63 174 unsigned long *rmap;
342d3db7 175 pte_t pte;
4cf302bc 176 unsigned int writing;
342d3db7 177 unsigned long mmu_seq;
bad3b507 178 unsigned long rcbits;
c77162de
PM
179
180 psize = hpte_page_size(pteh, ptel);
181 if (!psize)
a8606e20 182 return H_PARAMETER;
4cf302bc 183 writing = hpte_is_writable(ptel);
697d3899 184 pteh &= ~(HPTE_V_HVLOCK | HPTE_V_ABSENT | HPTE_V_VALID);
44e5f6be
PM
185 ptel &= ~HPTE_GR_RESERVED;
186 g_ptel = ptel;
b2b2f165 187
342d3db7
PM
188 /* used later to detect if we might have been invalidated */
189 mmu_seq = kvm->mmu_notifier_seq;
190 smp_rmb();
191
c77162de
PM
192 /* Find the memslot (if any) for this address */
193 gpa = (ptel & HPTE_R_RPN) & ~(psize - 1);
194 gfn = gpa >> PAGE_SHIFT;
9d4cba7f 195 memslot = __gfn_to_memslot(kvm_memslots(kvm), gfn);
697d3899 196 pa = 0;
342d3db7 197 is_io = ~0ul;
697d3899
PM
198 rmap = NULL;
199 if (!(memslot && !(memslot->flags & KVM_MEMSLOT_INVALID))) {
200 /* PPC970 can't do emulated MMIO */
201 if (!cpu_has_feature(CPU_FTR_ARCH_206))
202 return H_PARAMETER;
203 /* Emulated MMIO - mark this with key=31 */
204 pteh |= HPTE_V_ABSENT;
205 ptel |= HPTE_R_KEY_HI | HPTE_R_KEY_LO;
206 goto do_insert;
207 }
da9d1d7f
PM
208
209 /* Check if the requested page fits entirely in the memslot. */
210 if (!slot_is_aligned(memslot, psize))
211 return H_PARAMETER;
c77162de 212 slot_fn = gfn - memslot->base_gfn;
d89cc617 213 rmap = &memslot->arch.rmap[slot_fn];
c77162de 214
342d3db7 215 if (!kvm->arch.using_mmu_notifiers) {
a66b48c3 216 physp = memslot->arch.slot_phys;
342d3db7
PM
217 if (!physp)
218 return H_PARAMETER;
219 physp += slot_fn;
220 if (realmode)
221 physp = real_vmalloc_addr(physp);
222 pa = *physp;
223 if (!pa)
224 return H_TOO_HARD;
225 is_io = pa & (HPTE_R_I | HPTE_R_W);
226 pte_size = PAGE_SIZE << (pa & KVMPPC_PAGE_ORDER_MASK);
227 pa &= PAGE_MASK;
caaa4c80 228 pa |= gpa & ~PAGE_MASK;
342d3db7
PM
229 } else {
230 /* Translate to host virtual address */
66a03505 231 hva = __gfn_to_hva_memslot(memslot, gfn);
342d3db7
PM
232
233 /* Look up the Linux PTE for the backing page */
234 pte_size = psize;
7c85e6b3
BB
235 pte = lookup_linux_pte_and_update(pgdir, hva, writing,
236 &pte_size);
342d3db7 237 if (pte_present(pte)) {
4cf302bc
PM
238 if (writing && !pte_write(pte))
239 /* make the actual HPTE be read-only */
240 ptel = hpte_make_readonly(ptel);
342d3db7
PM
241 is_io = hpte_cache_bits(pte_val(pte));
242 pa = pte_pfn(pte) << PAGE_SHIFT;
caaa4c80 243 pa |= hva & (pte_size - 1);
df9059bb 244 pa |= gpa & ~PAGE_MASK;
342d3db7
PM
245 }
246 }
7ed661bf 247
c77162de
PM
248 if (pte_size < psize)
249 return H_PARAMETER;
c77162de
PM
250
251 ptel &= ~(HPTE_R_PP0 - psize);
252 ptel |= pa;
342d3db7
PM
253
254 if (pa)
255 pteh |= HPTE_V_VALID;
256 else
257 pteh |= HPTE_V_ABSENT;
c77162de 258
a8606e20 259 /* Check WIMG */
342d3db7 260 if (is_io != ~0ul && !hpte_cache_flags_ok(ptel, is_io)) {
9d0ef5ea
PM
261 if (is_io)
262 return H_PARAMETER;
263 /*
264 * Allow guest to map emulated device memory as
265 * uncacheable, but actually make it cacheable.
266 */
267 ptel &= ~(HPTE_R_W|HPTE_R_I|HPTE_R_G);
268 ptel |= HPTE_R_M;
269 }
075295dd 270
342d3db7 271 /* Find and lock the HPTEG slot to use */
697d3899 272 do_insert:
32fad281 273 if (pte_index >= kvm->arch.hpt_npte)
a8606e20
PM
274 return H_PARAMETER;
275 if (likely((flags & H_EXACT) == 0)) {
276 pte_index &= ~7UL;
277 hpte = (unsigned long *)(kvm->arch.hpt_virt + (pte_index << 4));
075295dd 278 for (i = 0; i < 8; ++i) {
a8606e20 279 if ((*hpte & HPTE_V_VALID) == 0 &&
697d3899
PM
280 try_lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID |
281 HPTE_V_ABSENT))
a8606e20
PM
282 break;
283 hpte += 2;
284 }
075295dd
PM
285 if (i == 8) {
286 /*
287 * Since try_lock_hpte doesn't retry (not even stdcx.
288 * failures), it could be that there is a free slot
289 * but we transiently failed to lock it. Try again,
290 * actually locking each slot and checking it.
291 */
292 hpte -= 16;
293 for (i = 0; i < 8; ++i) {
294 while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
295 cpu_relax();
697d3899 296 if (!(*hpte & (HPTE_V_VALID | HPTE_V_ABSENT)))
075295dd
PM
297 break;
298 *hpte &= ~HPTE_V_HVLOCK;
299 hpte += 2;
300 }
301 if (i == 8)
302 return H_PTEG_FULL;
303 }
8936dda4 304 pte_index += i;
a8606e20 305 } else {
a8606e20 306 hpte = (unsigned long *)(kvm->arch.hpt_virt + (pte_index << 4));
697d3899
PM
307 if (!try_lock_hpte(hpte, HPTE_V_HVLOCK | HPTE_V_VALID |
308 HPTE_V_ABSENT)) {
075295dd
PM
309 /* Lock the slot and check again */
310 while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
311 cpu_relax();
697d3899 312 if (*hpte & (HPTE_V_VALID | HPTE_V_ABSENT)) {
075295dd
PM
313 *hpte &= ~HPTE_V_HVLOCK;
314 return H_PTEG_FULL;
315 }
316 }
a8606e20 317 }
8936dda4
PM
318
319 /* Save away the guest's idea of the second HPTE dword */
06ce2c63
PM
320 rev = &kvm->arch.revmap[pte_index];
321 if (realmode)
322 rev = real_vmalloc_addr(rev);
44e5f6be 323 if (rev) {
8936dda4 324 rev->guest_rpte = g_ptel;
44e5f6be
PM
325 note_hpte_modification(kvm, rev);
326 }
06ce2c63
PM
327
328 /* Link HPTE into reverse-map chain */
697d3899
PM
329 if (pteh & HPTE_V_VALID) {
330 if (realmode)
331 rmap = real_vmalloc_addr(rmap);
332 lock_rmap(rmap);
342d3db7
PM
333 /* Check for pending invalidations under the rmap chain lock */
334 if (kvm->arch.using_mmu_notifiers &&
7ed661bf 335 mmu_notifier_retry(kvm, mmu_seq)) {
342d3db7
PM
336 /* inval in progress, write a non-present HPTE */
337 pteh |= HPTE_V_ABSENT;
338 pteh &= ~HPTE_V_VALID;
339 unlock_rmap(rmap);
340 } else {
341 kvmppc_add_revmap_chain(kvm, rev, rmap, pte_index,
342 realmode);
bad3b507
PM
343 /* Only set R/C in real HPTE if already set in *rmap */
344 rcbits = *rmap >> KVMPPC_RMAP_RC_SHIFT;
345 ptel &= rcbits | ~(HPTE_R_R | HPTE_R_C);
342d3db7 346 }
697d3899 347 }
06ce2c63 348
a8606e20 349 hpte[1] = ptel;
06ce2c63
PM
350
351 /* Write the first HPTE dword, unlocking the HPTE and making it valid */
a8606e20
PM
352 eieio();
353 hpte[0] = pteh;
354 asm volatile("ptesync" : : : "memory");
06ce2c63 355
7ed661bf 356 *pte_idx_ret = pte_index;
a8606e20
PM
357 return H_SUCCESS;
358}
7ed661bf
PM
359EXPORT_SYMBOL_GPL(kvmppc_do_h_enter);
360
361long kvmppc_h_enter(struct kvm_vcpu *vcpu, unsigned long flags,
362 long pte_index, unsigned long pteh, unsigned long ptel)
363{
364 return kvmppc_do_h_enter(vcpu->kvm, flags, pte_index, pteh, ptel,
365 vcpu->arch.pgdir, true, &vcpu->arch.gpr[4]);
366}
a8606e20 367
54bb7f4b 368#ifdef __BIG_ENDIAN__
a8606e20 369#define LOCK_TOKEN (*(u32 *)(&get_paca()->lock_token))
54bb7f4b
AB
370#else
371#define LOCK_TOKEN (*(u32 *)(&get_paca()->paca_index))
372#endif
a8606e20
PM
373
374static inline int try_lock_tlbie(unsigned int *lock)
375{
376 unsigned int tmp, old;
377 unsigned int token = LOCK_TOKEN;
378
379 asm volatile("1:lwarx %1,0,%2\n"
380 " cmpwi cr0,%1,0\n"
381 " bne 2f\n"
382 " stwcx. %3,0,%2\n"
383 " bne- 1b\n"
384 " isync\n"
385 "2:"
386 : "=&r" (tmp), "=&r" (old)
387 : "r" (lock), "r" (token)
388 : "cc", "memory");
389 return old == 0;
390}
391
54480501
PM
392/*
393 * tlbie/tlbiel is a bit different on the PPC970 compared to later
394 * processors such as POWER7; the large page bit is in the instruction
395 * not RB, and the top 16 bits and the bottom 12 bits of the VA
396 * in RB must be 0.
397 */
398static void do_tlbies_970(struct kvm *kvm, unsigned long *rbvalues,
399 long npages, int global, bool need_sync)
400{
401 long i;
402
403 if (global) {
404 while (!try_lock_tlbie(&kvm->arch.tlbie_lock))
405 cpu_relax();
406 if (need_sync)
407 asm volatile("ptesync" : : : "memory");
408 for (i = 0; i < npages; ++i) {
409 unsigned long rb = rbvalues[i];
410
411 if (rb & 1) /* large page */
412 asm volatile("tlbie %0,1" : :
413 "r" (rb & 0x0000fffffffff000ul));
414 else
415 asm volatile("tlbie %0,0" : :
416 "r" (rb & 0x0000fffffffff000ul));
417 }
418 asm volatile("eieio; tlbsync; ptesync" : : : "memory");
419 kvm->arch.tlbie_lock = 0;
420 } else {
421 if (need_sync)
422 asm volatile("ptesync" : : : "memory");
423 for (i = 0; i < npages; ++i) {
424 unsigned long rb = rbvalues[i];
425
426 if (rb & 1) /* large page */
427 asm volatile("tlbiel %0,1" : :
428 "r" (rb & 0x0000fffffffff000ul));
429 else
430 asm volatile("tlbiel %0,0" : :
431 "r" (rb & 0x0000fffffffff000ul));
432 }
433 asm volatile("ptesync" : : : "memory");
434 }
435}
436
437static void do_tlbies(struct kvm *kvm, unsigned long *rbvalues,
438 long npages, int global, bool need_sync)
439{
440 long i;
441
442 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
443 /* PPC970 tlbie instruction is a bit different */
444 do_tlbies_970(kvm, rbvalues, npages, global, need_sync);
445 return;
446 }
447 if (global) {
448 while (!try_lock_tlbie(&kvm->arch.tlbie_lock))
449 cpu_relax();
450 if (need_sync)
451 asm volatile("ptesync" : : : "memory");
452 for (i = 0; i < npages; ++i)
453 asm volatile(PPC_TLBIE(%1,%0) : :
454 "r" (rbvalues[i]), "r" (kvm->arch.lpid));
455 asm volatile("eieio; tlbsync; ptesync" : : : "memory");
456 kvm->arch.tlbie_lock = 0;
457 } else {
458 if (need_sync)
459 asm volatile("ptesync" : : : "memory");
460 for (i = 0; i < npages; ++i)
461 asm volatile("tlbiel %0" : : "r" (rbvalues[i]));
462 asm volatile("ptesync" : : : "memory");
463 }
464}
465
6b445ad4
PM
466long kvmppc_do_h_remove(struct kvm *kvm, unsigned long flags,
467 unsigned long pte_index, unsigned long avpn,
468 unsigned long *hpret)
a8606e20 469{
a8606e20
PM
470 unsigned long *hpte;
471 unsigned long v, r, rb;
a92bce95 472 struct revmap_entry *rev;
a8606e20 473
32fad281 474 if (pte_index >= kvm->arch.hpt_npte)
a8606e20
PM
475 return H_PARAMETER;
476 hpte = (unsigned long *)(kvm->arch.hpt_virt + (pte_index << 4));
075295dd 477 while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
a8606e20 478 cpu_relax();
697d3899 479 if ((hpte[0] & (HPTE_V_ABSENT | HPTE_V_VALID)) == 0 ||
a8606e20
PM
480 ((flags & H_AVPN) && (hpte[0] & ~0x7fUL) != avpn) ||
481 ((flags & H_ANDCOND) && (hpte[0] & avpn) != 0)) {
482 hpte[0] &= ~HPTE_V_HVLOCK;
483 return H_NOT_FOUND;
484 }
a92bce95
PM
485
486 rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
487 v = hpte[0] & ~HPTE_V_HVLOCK;
488 if (v & HPTE_V_VALID) {
489 hpte[0] &= ~HPTE_V_VALID;
490 rb = compute_tlbie_rb(v, hpte[1], pte_index);
54480501 491 do_tlbies(kvm, &rb, 1, global_invalidates(kvm, flags), true);
bad3b507
PM
492 /* Read PTE low word after tlbie to get final R/C values */
493 remove_revmap_chain(kvm, pte_index, rev, v, hpte[1]);
a8606e20 494 }
44e5f6be
PM
495 r = rev->guest_rpte & ~HPTE_GR_RESERVED;
496 note_hpte_modification(kvm, rev);
a92bce95
PM
497 unlock_hpte(hpte, 0);
498
6b445ad4
PM
499 hpret[0] = v;
500 hpret[1] = r;
a8606e20
PM
501 return H_SUCCESS;
502}
6b445ad4
PM
503EXPORT_SYMBOL_GPL(kvmppc_do_h_remove);
504
505long kvmppc_h_remove(struct kvm_vcpu *vcpu, unsigned long flags,
506 unsigned long pte_index, unsigned long avpn)
507{
508 return kvmppc_do_h_remove(vcpu->kvm, flags, pte_index, avpn,
509 &vcpu->arch.gpr[4]);
510}
a8606e20
PM
511
512long kvmppc_h_bulk_remove(struct kvm_vcpu *vcpu)
513{
514 struct kvm *kvm = vcpu->kvm;
515 unsigned long *args = &vcpu->arch.gpr[4];
a92bce95
PM
516 unsigned long *hp, *hptes[4], tlbrb[4];
517 long int i, j, k, n, found, indexes[4];
518 unsigned long flags, req, pte_index, rcbits;
54480501 519 int global;
a8606e20 520 long int ret = H_SUCCESS;
a92bce95 521 struct revmap_entry *rev, *revs[4];
a8606e20 522
54480501 523 global = global_invalidates(kvm, 0);
a92bce95
PM
524 for (i = 0; i < 4 && ret == H_SUCCESS; ) {
525 n = 0;
526 for (; i < 4; ++i) {
527 j = i * 2;
528 pte_index = args[j];
529 flags = pte_index >> 56;
530 pte_index &= ((1ul << 56) - 1);
531 req = flags >> 6;
532 flags &= 3;
533 if (req == 3) { /* no more requests */
534 i = 4;
a8606e20 535 break;
a92bce95 536 }
32fad281
PM
537 if (req != 1 || flags == 3 ||
538 pte_index >= kvm->arch.hpt_npte) {
a92bce95
PM
539 /* parameter error */
540 args[j] = ((0xa0 | flags) << 56) + pte_index;
541 ret = H_PARAMETER;
a8606e20 542 break;
a92bce95
PM
543 }
544 hp = (unsigned long *)
545 (kvm->arch.hpt_virt + (pte_index << 4));
546 /* to avoid deadlock, don't spin except for first */
547 if (!try_lock_hpte(hp, HPTE_V_HVLOCK)) {
548 if (n)
549 break;
550 while (!try_lock_hpte(hp, HPTE_V_HVLOCK))
551 cpu_relax();
552 }
553 found = 0;
554 if (hp[0] & (HPTE_V_ABSENT | HPTE_V_VALID)) {
555 switch (flags & 3) {
556 case 0: /* absolute */
a8606e20 557 found = 1;
a92bce95
PM
558 break;
559 case 1: /* andcond */
560 if (!(hp[0] & args[j + 1]))
561 found = 1;
562 break;
563 case 2: /* AVPN */
564 if ((hp[0] & ~0x7fUL) == args[j + 1])
565 found = 1;
566 break;
567 }
568 }
569 if (!found) {
570 hp[0] &= ~HPTE_V_HVLOCK;
571 args[j] = ((0x90 | flags) << 56) + pte_index;
572 continue;
a8606e20 573 }
a92bce95
PM
574
575 args[j] = ((0x80 | flags) << 56) + pte_index;
576 rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
44e5f6be 577 note_hpte_modification(kvm, rev);
a92bce95 578
bad3b507
PM
579 if (!(hp[0] & HPTE_V_VALID)) {
580 /* insert R and C bits from PTE */
581 rcbits = rev->guest_rpte & (HPTE_R_R|HPTE_R_C);
582 args[j] |= rcbits << (56 - 5);
51bfd299 583 hp[0] = 0;
a92bce95 584 continue;
bad3b507 585 }
a92bce95
PM
586
587 hp[0] &= ~HPTE_V_VALID; /* leave it locked */
588 tlbrb[n] = compute_tlbie_rb(hp[0], hp[1], pte_index);
589 indexes[n] = j;
590 hptes[n] = hp;
591 revs[n] = rev;
592 ++n;
a8606e20 593 }
a92bce95
PM
594
595 if (!n)
596 break;
597
598 /* Now that we've collected a batch, do the tlbies */
54480501 599 do_tlbies(kvm, tlbrb, n, global, true);
a92bce95 600
bad3b507 601 /* Read PTE low words after tlbie to get final R/C values */
a92bce95
PM
602 for (k = 0; k < n; ++k) {
603 j = indexes[k];
604 pte_index = args[j] & ((1ul << 56) - 1);
605 hp = hptes[k];
606 rev = revs[k];
bad3b507
PM
607 remove_revmap_chain(kvm, pte_index, rev, hp[0], hp[1]);
608 rcbits = rev->guest_rpte & (HPTE_R_R|HPTE_R_C);
609 args[j] |= rcbits << (56 - 5);
610 hp[0] = 0;
697d3899 611 }
a8606e20 612 }
a92bce95 613
a8606e20
PM
614 return ret;
615}
616
617long kvmppc_h_protect(struct kvm_vcpu *vcpu, unsigned long flags,
618 unsigned long pte_index, unsigned long avpn,
619 unsigned long va)
620{
621 struct kvm *kvm = vcpu->kvm;
622 unsigned long *hpte;
8936dda4
PM
623 struct revmap_entry *rev;
624 unsigned long v, r, rb, mask, bits;
a8606e20 625
32fad281 626 if (pte_index >= kvm->arch.hpt_npte)
a8606e20 627 return H_PARAMETER;
697d3899 628
a8606e20 629 hpte = (unsigned long *)(kvm->arch.hpt_virt + (pte_index << 4));
075295dd 630 while (!try_lock_hpte(hpte, HPTE_V_HVLOCK))
a8606e20 631 cpu_relax();
697d3899 632 if ((hpte[0] & (HPTE_V_ABSENT | HPTE_V_VALID)) == 0 ||
a8606e20
PM
633 ((flags & H_AVPN) && (hpte[0] & ~0x7fUL) != avpn)) {
634 hpte[0] &= ~HPTE_V_HVLOCK;
635 return H_NOT_FOUND;
636 }
697d3899 637
a8606e20 638 v = hpte[0];
8936dda4
PM
639 bits = (flags << 55) & HPTE_R_PP0;
640 bits |= (flags << 48) & HPTE_R_KEY_HI;
641 bits |= flags & (HPTE_R_PP | HPTE_R_N | HPTE_R_KEY_LO);
642
643 /* Update guest view of 2nd HPTE dword */
644 mask = HPTE_R_PP0 | HPTE_R_PP | HPTE_R_N |
645 HPTE_R_KEY_HI | HPTE_R_KEY_LO;
646 rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
647 if (rev) {
648 r = (rev->guest_rpte & ~mask) | bits;
649 rev->guest_rpte = r;
44e5f6be 650 note_hpte_modification(kvm, rev);
8936dda4
PM
651 }
652 r = (hpte[1] & ~mask) | bits;
653
654 /* Update HPTE */
697d3899
PM
655 if (v & HPTE_V_VALID) {
656 rb = compute_tlbie_rb(v, r, pte_index);
657 hpte[0] = v & ~HPTE_V_VALID;
54480501 658 do_tlbies(kvm, &rb, 1, global_invalidates(kvm, flags), true);
1cc8ed0b
PM
659 /*
660 * If the host has this page as readonly but the guest
661 * wants to make it read/write, reduce the permissions.
662 * Checking the host permissions involves finding the
663 * memslot and then the Linux PTE for the page.
664 */
665 if (hpte_is_writable(r) && kvm->arch.using_mmu_notifiers) {
666 unsigned long psize, gfn, hva;
667 struct kvm_memory_slot *memslot;
668 pgd_t *pgdir = vcpu->arch.pgdir;
669 pte_t pte;
670
671 psize = hpte_page_size(v, r);
672 gfn = ((r & HPTE_R_RPN) & ~(psize - 1)) >> PAGE_SHIFT;
673 memslot = __gfn_to_memslot(kvm_memslots(kvm), gfn);
674 if (memslot) {
675 hva = __gfn_to_hva_memslot(memslot, gfn);
7c85e6b3
BB
676 pte = lookup_linux_pte_and_update(pgdir, hva,
677 1, &psize);
1cc8ed0b
PM
678 if (pte_present(pte) && !pte_write(pte))
679 r = hpte_make_readonly(r);
680 }
681 }
a8606e20
PM
682 }
683 hpte[1] = r;
684 eieio();
685 hpte[0] = v & ~HPTE_V_HVLOCK;
686 asm volatile("ptesync" : : : "memory");
687 return H_SUCCESS;
688}
689
a8606e20
PM
690long kvmppc_h_read(struct kvm_vcpu *vcpu, unsigned long flags,
691 unsigned long pte_index)
692{
693 struct kvm *kvm = vcpu->kvm;
697d3899 694 unsigned long *hpte, v, r;
a8606e20 695 int i, n = 1;
8936dda4 696 struct revmap_entry *rev = NULL;
a8606e20 697
32fad281 698 if (pte_index >= kvm->arch.hpt_npte)
a8606e20
PM
699 return H_PARAMETER;
700 if (flags & H_READ_4) {
701 pte_index &= ~3;
702 n = 4;
703 }
bad3b507 704 rev = real_vmalloc_addr(&kvm->arch.revmap[pte_index]);
a8606e20
PM
705 for (i = 0; i < n; ++i, ++pte_index) {
706 hpte = (unsigned long *)(kvm->arch.hpt_virt + (pte_index << 4));
697d3899 707 v = hpte[0] & ~HPTE_V_HVLOCK;
a8606e20 708 r = hpte[1];
697d3899
PM
709 if (v & HPTE_V_ABSENT) {
710 v &= ~HPTE_V_ABSENT;
711 v |= HPTE_V_VALID;
712 }
44e5f6be 713 if (v & HPTE_V_VALID) {
bad3b507 714 r = rev[i].guest_rpte | (r & (HPTE_R_R | HPTE_R_C));
44e5f6be
PM
715 r &= ~HPTE_GR_RESERVED;
716 }
697d3899 717 vcpu->arch.gpr[4 + i * 2] = v;
a8606e20
PM
718 vcpu->arch.gpr[5 + i * 2] = r;
719 }
720 return H_SUCCESS;
721}
697d3899 722
342d3db7
PM
723void kvmppc_invalidate_hpte(struct kvm *kvm, unsigned long *hptep,
724 unsigned long pte_index)
725{
726 unsigned long rb;
727
728 hptep[0] &= ~HPTE_V_VALID;
729 rb = compute_tlbie_rb(hptep[0], hptep[1], pte_index);
54480501 730 do_tlbies(kvm, &rb, 1, 1, true);
342d3db7
PM
731}
732EXPORT_SYMBOL_GPL(kvmppc_invalidate_hpte);
733
55514893
PM
734void kvmppc_clear_ref_hpte(struct kvm *kvm, unsigned long *hptep,
735 unsigned long pte_index)
736{
737 unsigned long rb;
738 unsigned char rbyte;
739
740 rb = compute_tlbie_rb(hptep[0], hptep[1], pte_index);
741 rbyte = (hptep[1] & ~HPTE_R_R) >> 8;
742 /* modify only the second-last byte, which contains the ref bit */
743 *((char *)hptep + 14) = rbyte;
54480501 744 do_tlbies(kvm, &rb, 1, 1, false);
55514893
PM
745}
746EXPORT_SYMBOL_GPL(kvmppc_clear_ref_hpte);
747
697d3899
PM
748static int slb_base_page_shift[4] = {
749 24, /* 16M */
750 16, /* 64k */
751 34, /* 16G */
752 20, /* 1M, unsupported */
753};
754
91648ec0 755/* When called from virtmode, this func should be protected by
756 * preempt_disable(), otherwise, the holding of HPTE_V_HVLOCK
757 * can trigger deadlock issue.
758 */
697d3899
PM
759long kvmppc_hv_find_lock_hpte(struct kvm *kvm, gva_t eaddr, unsigned long slb_v,
760 unsigned long valid)
761{
762 unsigned int i;
763 unsigned int pshift;
764 unsigned long somask;
765 unsigned long vsid, hash;
766 unsigned long avpn;
767 unsigned long *hpte;
768 unsigned long mask, val;
769 unsigned long v, r;
770
771 /* Get page shift, work out hash and AVPN etc. */
772 mask = SLB_VSID_B | HPTE_V_AVPN | HPTE_V_SECONDARY;
773 val = 0;
774 pshift = 12;
775 if (slb_v & SLB_VSID_L) {
776 mask |= HPTE_V_LARGE;
777 val |= HPTE_V_LARGE;
778 pshift = slb_base_page_shift[(slb_v & SLB_VSID_LP) >> 4];
779 }
780 if (slb_v & SLB_VSID_B_1T) {
781 somask = (1UL << 40) - 1;
782 vsid = (slb_v & ~SLB_VSID_B) >> SLB_VSID_SHIFT_1T;
783 vsid ^= vsid << 25;
784 } else {
785 somask = (1UL << 28) - 1;
786 vsid = (slb_v & ~SLB_VSID_B) >> SLB_VSID_SHIFT;
787 }
32fad281 788 hash = (vsid ^ ((eaddr & somask) >> pshift)) & kvm->arch.hpt_mask;
697d3899
PM
789 avpn = slb_v & ~(somask >> 16); /* also includes B */
790 avpn |= (eaddr & somask) >> 16;
791
792 if (pshift >= 24)
793 avpn &= ~((1UL << (pshift - 16)) - 1);
794 else
795 avpn &= ~0x7fUL;
796 val |= avpn;
797
798 for (;;) {
799 hpte = (unsigned long *)(kvm->arch.hpt_virt + (hash << 7));
800
801 for (i = 0; i < 16; i += 2) {
802 /* Read the PTE racily */
803 v = hpte[i] & ~HPTE_V_HVLOCK;
804
805 /* Check valid/absent, hash, segment size and AVPN */
806 if (!(v & valid) || (v & mask) != val)
807 continue;
808
809 /* Lock the PTE and read it under the lock */
810 while (!try_lock_hpte(&hpte[i], HPTE_V_HVLOCK))
811 cpu_relax();
812 v = hpte[i] & ~HPTE_V_HVLOCK;
813 r = hpte[i+1];
814
815 /*
816 * Check the HPTE again, including large page size
817 * Since we don't currently allow any MPSS (mixed
818 * page-size segment) page sizes, it is sufficient
819 * to check against the actual page size.
820 */
821 if ((v & valid) && (v & mask) == val &&
822 hpte_page_size(v, r) == (1ul << pshift))
823 /* Return with the HPTE still locked */
824 return (hash << 3) + (i >> 1);
825
826 /* Unlock and move on */
827 hpte[i] = v;
828 }
829
830 if (val & HPTE_V_SECONDARY)
831 break;
832 val |= HPTE_V_SECONDARY;
32fad281 833 hash = hash ^ kvm->arch.hpt_mask;
697d3899
PM
834 }
835 return -1;
836}
837EXPORT_SYMBOL(kvmppc_hv_find_lock_hpte);
838
839/*
840 * Called in real mode to check whether an HPTE not found fault
4cf302bc
PM
841 * is due to accessing a paged-out page or an emulated MMIO page,
842 * or if a protection fault is due to accessing a page that the
843 * guest wanted read/write access to but which we made read-only.
697d3899
PM
844 * Returns a possibly modified status (DSISR) value if not
845 * (i.e. pass the interrupt to the guest),
846 * -1 to pass the fault up to host kernel mode code, -2 to do that
342d3db7 847 * and also load the instruction word (for MMIO emulation),
697d3899
PM
848 * or 0 if we should make the guest retry the access.
849 */
850long kvmppc_hpte_hv_fault(struct kvm_vcpu *vcpu, unsigned long addr,
342d3db7 851 unsigned long slb_v, unsigned int status, bool data)
697d3899
PM
852{
853 struct kvm *kvm = vcpu->kvm;
854 long int index;
855 unsigned long v, r, gr;
856 unsigned long *hpte;
857 unsigned long valid;
858 struct revmap_entry *rev;
859 unsigned long pp, key;
860
4cf302bc
PM
861 /* For protection fault, expect to find a valid HPTE */
862 valid = HPTE_V_VALID;
863 if (status & DSISR_NOHPTE)
864 valid |= HPTE_V_ABSENT;
342d3db7 865
697d3899 866 index = kvmppc_hv_find_lock_hpte(kvm, addr, slb_v, valid);
4cf302bc
PM
867 if (index < 0) {
868 if (status & DSISR_NOHPTE)
869 return status; /* there really was no HPTE */
870 return 0; /* for prot fault, HPTE disappeared */
871 }
697d3899
PM
872 hpte = (unsigned long *)(kvm->arch.hpt_virt + (index << 4));
873 v = hpte[0] & ~HPTE_V_HVLOCK;
874 r = hpte[1];
875 rev = real_vmalloc_addr(&kvm->arch.revmap[index]);
876 gr = rev->guest_rpte;
877
a92bce95 878 unlock_hpte(hpte, v);
697d3899 879
4cf302bc
PM
880 /* For not found, if the HPTE is valid by now, retry the instruction */
881 if ((status & DSISR_NOHPTE) && (v & HPTE_V_VALID))
697d3899
PM
882 return 0;
883
884 /* Check access permissions to the page */
885 pp = gr & (HPTE_R_PP0 | HPTE_R_PP);
886 key = (vcpu->arch.shregs.msr & MSR_PR) ? SLB_VSID_KP : SLB_VSID_KS;
342d3db7
PM
887 status &= ~DSISR_NOHPTE; /* DSISR_NOHPTE == SRR1_ISI_NOPT */
888 if (!data) {
889 if (gr & (HPTE_R_N | HPTE_R_G))
890 return status | SRR1_ISI_N_OR_G;
891 if (!hpte_read_permission(pp, slb_v & key))
892 return status | SRR1_ISI_PROT;
893 } else if (status & DSISR_ISSTORE) {
697d3899
PM
894 /* check write permission */
895 if (!hpte_write_permission(pp, slb_v & key))
342d3db7 896 return status | DSISR_PROTFAULT;
697d3899
PM
897 } else {
898 if (!hpte_read_permission(pp, slb_v & key))
342d3db7 899 return status | DSISR_PROTFAULT;
697d3899
PM
900 }
901
902 /* Check storage key, if applicable */
342d3db7 903 if (data && (vcpu->arch.shregs.msr & MSR_DR)) {
697d3899
PM
904 unsigned int perm = hpte_get_skey_perm(gr, vcpu->arch.amr);
905 if (status & DSISR_ISSTORE)
906 perm >>= 1;
907 if (perm & 1)
342d3db7 908 return status | DSISR_KEYFAULT;
697d3899
PM
909 }
910
911 /* Save HPTE info for virtual-mode handler */
912 vcpu->arch.pgfault_addr = addr;
913 vcpu->arch.pgfault_index = index;
914 vcpu->arch.pgfault_hpte[0] = v;
915 vcpu->arch.pgfault_hpte[1] = r;
916
342d3db7
PM
917 /* Check the storage key to see if it is possibly emulated MMIO */
918 if (data && (vcpu->arch.shregs.msr & MSR_IR) &&
919 (r & (HPTE_R_KEY_HI | HPTE_R_KEY_LO)) ==
920 (HPTE_R_KEY_HI | HPTE_R_KEY_LO))
697d3899
PM
921 return -2; /* MMIO emulation - load instr word */
922
923 return -1; /* send fault up to host kernel mode */
697d3899 924}