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