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