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