Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 SUSE Linux Products GmbH. All rights reserved. |
| 3 | * |
| 4 | * Authors: |
| 5 | * Alexander Graf <agraf@suse.de> |
| 6 | * Kevin Wolf <mail@kevin-wolf.de> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License, version 2, as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/kvm_host.h> |
| 23 | |
| 24 | #include <asm/kvm_ppc.h> |
| 25 | #include <asm/kvm_book3s.h> |
Aneesh Kumar K.V | f64e808 | 2016-03-01 12:59:20 +0530 | [diff] [blame] | 26 | #include <asm/book3s/64/mmu-hash.h> |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 27 | #include <asm/machdep.h> |
| 28 | #include <asm/mmu_context.h> |
| 29 | #include <asm/hw_irq.h> |
Aneesh Kumar K.V | 72c1253 | 2013-10-07 22:17:57 +0530 | [diff] [blame] | 30 | #include "trace_pr.h" |
Thomas Huth | 5358a96 | 2015-05-22 09:25:02 +0200 | [diff] [blame] | 31 | #include "book3s.h" |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 32 | |
| 33 | #define PTE_SIZE 12 |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 34 | |
Alexander Graf | fef093be | 2010-06-30 15:18:46 +0200 | [diff] [blame] | 35 | void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte) |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 36 | { |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 37 | ppc_md.hpte_invalidate(pte->slot, pte->host_vpn, |
Paul Mackerras | c9029c3 | 2013-09-20 14:52:45 +1000 | [diff] [blame] | 38 | pte->pagesize, pte->pagesize, MMU_SEGSIZE_256M, |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 39 | false); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using |
| 43 | * a hash, so we don't waste cycles on looping */ |
| 44 | static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid) |
| 45 | { |
Alexander Graf | b9877ce | 2010-08-02 21:48:53 +0200 | [diff] [blame] | 46 | return (u16)(((gvsid >> (SID_MAP_BITS * 7)) & SID_MAP_MASK) ^ |
| 47 | ((gvsid >> (SID_MAP_BITS * 6)) & SID_MAP_MASK) ^ |
| 48 | ((gvsid >> (SID_MAP_BITS * 5)) & SID_MAP_MASK) ^ |
| 49 | ((gvsid >> (SID_MAP_BITS * 4)) & SID_MAP_MASK) ^ |
| 50 | ((gvsid >> (SID_MAP_BITS * 3)) & SID_MAP_MASK) ^ |
| 51 | ((gvsid >> (SID_MAP_BITS * 2)) & SID_MAP_MASK) ^ |
| 52 | ((gvsid >> (SID_MAP_BITS * 1)) & SID_MAP_MASK) ^ |
| 53 | ((gvsid >> (SID_MAP_BITS * 0)) & SID_MAP_MASK)); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Alexander Graf | b9877ce | 2010-08-02 21:48:53 +0200 | [diff] [blame] | 56 | |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 57 | static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid) |
| 58 | { |
| 59 | struct kvmppc_sid_map *map; |
| 60 | u16 sid_map_mask; |
| 61 | |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 62 | if (kvmppc_get_msr(vcpu) & MSR_PR) |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 63 | gvsid |= VSID_PR; |
| 64 | |
| 65 | sid_map_mask = kvmppc_sid_hash(vcpu, gvsid); |
| 66 | map = &to_book3s(vcpu)->sid_map[sid_map_mask]; |
Alexander Graf | c22c319 | 2010-08-02 13:38:18 +0200 | [diff] [blame] | 67 | if (map->valid && (map->guest_vsid == gvsid)) { |
Alexander Graf | 928d78b | 2010-08-02 21:25:33 +0200 | [diff] [blame] | 68 | trace_kvm_book3s_slb_found(gvsid, map->host_vsid); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 69 | return map; |
| 70 | } |
| 71 | |
| 72 | map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask]; |
Alexander Graf | c22c319 | 2010-08-02 13:38:18 +0200 | [diff] [blame] | 73 | if (map->valid && (map->guest_vsid == gvsid)) { |
Alexander Graf | 928d78b | 2010-08-02 21:25:33 +0200 | [diff] [blame] | 74 | trace_kvm_book3s_slb_found(gvsid, map->host_vsid); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 75 | return map; |
| 76 | } |
| 77 | |
Alexander Graf | 928d78b | 2010-08-02 21:25:33 +0200 | [diff] [blame] | 78 | trace_kvm_book3s_slb_fail(sid_map_mask, gvsid); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 79 | return NULL; |
| 80 | } |
| 81 | |
Paul Mackerras | 93b159b | 2013-09-20 14:52:51 +1000 | [diff] [blame] | 82 | int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte, |
| 83 | bool iswrite) |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 84 | { |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 85 | unsigned long vpn; |
Dan Williams | ba049e9 | 2016-01-15 16:56:11 -0800 | [diff] [blame] | 86 | kvm_pfn_t hpaddr; |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 87 | ulong hash, hpteg; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 88 | u64 vsid; |
| 89 | int ret; |
| 90 | int rflags = 0x192; |
| 91 | int vflags = 0; |
| 92 | int attempt = 0; |
| 93 | struct kvmppc_sid_map *map; |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 94 | int r = 0; |
Paul Mackerras | c9029c3 | 2013-09-20 14:52:45 +1000 | [diff] [blame] | 95 | int hpsize = MMU_PAGE_4K; |
Paul Mackerras | 93b159b | 2013-09-20 14:52:51 +1000 | [diff] [blame] | 96 | bool writable; |
Paul Mackerras | d78bca7 | 2013-09-20 14:52:52 +1000 | [diff] [blame] | 97 | unsigned long mmu_seq; |
| 98 | struct kvm *kvm = vcpu->kvm; |
| 99 | struct hpte_cache *cpte; |
Paul Mackerras | adc0baf | 2013-09-20 14:52:53 +1000 | [diff] [blame] | 100 | unsigned long gfn = orig_pte->raddr >> PAGE_SHIFT; |
| 101 | unsigned long pfn; |
Paul Mackerras | d78bca7 | 2013-09-20 14:52:52 +1000 | [diff] [blame] | 102 | |
| 103 | /* used to check for invalidations in progress */ |
| 104 | mmu_seq = kvm->mmu_notifier_seq; |
| 105 | smp_rmb(); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 106 | |
| 107 | /* Get host physical address for gpa */ |
Alexander Graf | 89b68c9 | 2014-07-13 16:37:12 +0200 | [diff] [blame] | 108 | pfn = kvmppc_gpa_to_pfn(vcpu, orig_pte->raddr, iswrite, &writable); |
Paul Mackerras | adc0baf | 2013-09-20 14:52:53 +1000 | [diff] [blame] | 109 | if (is_error_noslot_pfn(pfn)) { |
Alexander Graf | 89b68c9 | 2014-07-13 16:37:12 +0200 | [diff] [blame] | 110 | printk(KERN_INFO "Couldn't get guest page for gpa %lx!\n", |
| 111 | orig_pte->raddr); |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 112 | r = -EINVAL; |
| 113 | goto out; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 114 | } |
Paul Mackerras | adc0baf | 2013-09-20 14:52:53 +1000 | [diff] [blame] | 115 | hpaddr = pfn << PAGE_SHIFT; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 116 | |
| 117 | /* and write the mapping ea -> hpa into the pt */ |
| 118 | vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid); |
| 119 | map = find_sid_vsid(vcpu, vsid); |
| 120 | if (!map) { |
Alexander Graf | ac21467 | 2010-04-20 02:49:50 +0200 | [diff] [blame] | 121 | ret = kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr); |
| 122 | WARN_ON(ret < 0); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 123 | map = find_sid_vsid(vcpu, vsid); |
| 124 | } |
Alexander Graf | ac21467 | 2010-04-20 02:49:50 +0200 | [diff] [blame] | 125 | if (!map) { |
| 126 | printk(KERN_ERR "KVM: Segment map for 0x%llx (0x%lx) failed\n", |
| 127 | vsid, orig_pte->eaddr); |
| 128 | WARN_ON(true); |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 129 | r = -EINVAL; |
| 130 | goto out; |
Alexander Graf | ac21467 | 2010-04-20 02:49:50 +0200 | [diff] [blame] | 131 | } |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 132 | |
Paul Mackerras | c9029c3 | 2013-09-20 14:52:45 +1000 | [diff] [blame] | 133 | vpn = hpt_vpn(orig_pte->eaddr, map->host_vsid, MMU_SEGSIZE_256M); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 134 | |
Paul Mackerras | adc0baf | 2013-09-20 14:52:53 +1000 | [diff] [blame] | 135 | kvm_set_pfn_accessed(pfn); |
Paul Mackerras | 93b159b | 2013-09-20 14:52:51 +1000 | [diff] [blame] | 136 | if (!orig_pte->may_write || !writable) |
Paul Mackerras | adc0baf | 2013-09-20 14:52:53 +1000 | [diff] [blame] | 137 | rflags |= PP_RXRX; |
| 138 | else { |
| 139 | mark_page_dirty(vcpu->kvm, gfn); |
| 140 | kvm_set_pfn_dirty(pfn); |
| 141 | } |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 142 | |
| 143 | if (!orig_pte->may_execute) |
| 144 | rflags |= HPTE_R_N; |
Alexander Graf | 249ba1e | 2012-08-03 13:56:33 +0200 | [diff] [blame] | 145 | else |
Paul Mackerras | adc0baf | 2013-09-20 14:52:53 +1000 | [diff] [blame] | 146 | kvmppc_mmu_flush_icache(pfn); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 147 | |
Paul Mackerras | c9029c3 | 2013-09-20 14:52:45 +1000 | [diff] [blame] | 148 | /* |
| 149 | * Use 64K pages if possible; otherwise, on 64K page kernels, |
| 150 | * we need to transfer 4 more bits from guest real to host real addr. |
| 151 | */ |
| 152 | if (vsid & VSID_64K) |
| 153 | hpsize = MMU_PAGE_64K; |
| 154 | else |
| 155 | hpaddr |= orig_pte->raddr & (~0xfffULL & ~PAGE_MASK); |
| 156 | |
| 157 | hash = hpt_hash(vpn, mmu_psize_defs[hpsize].shift, MMU_SEGSIZE_256M); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 158 | |
Paul Mackerras | d78bca7 | 2013-09-20 14:52:52 +1000 | [diff] [blame] | 159 | cpte = kvmppc_mmu_hpte_cache_next(vcpu); |
| 160 | |
| 161 | spin_lock(&kvm->mmu_lock); |
| 162 | if (!cpte || mmu_notifier_retry(kvm, mmu_seq)) { |
| 163 | r = -EAGAIN; |
| 164 | goto out_unlock; |
| 165 | } |
| 166 | |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 167 | map_again: |
| 168 | hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP); |
| 169 | |
| 170 | /* In case we tried normal mapping already, let's nuke old entries */ |
| 171 | if (attempt > 1) |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 172 | if (ppc_md.hpte_remove(hpteg) < 0) { |
| 173 | r = -1; |
Paul Mackerras | d78bca7 | 2013-09-20 14:52:52 +1000 | [diff] [blame] | 174 | goto out_unlock; |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 175 | } |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 176 | |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 177 | ret = ppc_md.hpte_insert(hpteg, vpn, hpaddr, rflags, vflags, |
Paul Mackerras | c9029c3 | 2013-09-20 14:52:45 +1000 | [diff] [blame] | 178 | hpsize, hpsize, MMU_SEGSIZE_256M); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 179 | |
| 180 | if (ret < 0) { |
| 181 | /* If we couldn't map a primary PTE, try a secondary */ |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 182 | hash = ~hash; |
Alexander Graf | 20a340a | 2010-02-19 11:00:46 +0100 | [diff] [blame] | 183 | vflags ^= HPTE_V_SECONDARY; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 184 | attempt++; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 185 | goto map_again; |
| 186 | } else { |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 187 | trace_kvm_book3s_64_mmu_map(rflags, hpteg, |
| 188 | vpn, hpaddr, orig_pte); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 189 | |
Alexander Graf | a1eda28 | 2010-03-24 21:48:34 +0100 | [diff] [blame] | 190 | /* The ppc_md code may give us a secondary entry even though we |
| 191 | asked for a primary. Fix up. */ |
| 192 | if ((ret & _PTEIDX_SECONDARY) && !(vflags & HPTE_V_SECONDARY)) { |
| 193 | hash = ~hash; |
| 194 | hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP); |
| 195 | } |
| 196 | |
Paul Mackerras | d78bca7 | 2013-09-20 14:52:52 +1000 | [diff] [blame] | 197 | cpte->slot = hpteg + (ret & 7); |
| 198 | cpte->host_vpn = vpn; |
| 199 | cpte->pte = *orig_pte; |
Paul Mackerras | adc0baf | 2013-09-20 14:52:53 +1000 | [diff] [blame] | 200 | cpte->pfn = pfn; |
Paul Mackerras | d78bca7 | 2013-09-20 14:52:52 +1000 | [diff] [blame] | 201 | cpte->pagesize = hpsize; |
Alexander Graf | fef093be | 2010-06-30 15:18:46 +0200 | [diff] [blame] | 202 | |
Paul Mackerras | d78bca7 | 2013-09-20 14:52:52 +1000 | [diff] [blame] | 203 | kvmppc_mmu_hpte_cache_map(vcpu, cpte); |
| 204 | cpte = NULL; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 205 | } |
Paul Mackerras | d78bca7 | 2013-09-20 14:52:52 +1000 | [diff] [blame] | 206 | |
| 207 | out_unlock: |
| 208 | spin_unlock(&kvm->mmu_lock); |
Paul Mackerras | adc0baf | 2013-09-20 14:52:53 +1000 | [diff] [blame] | 209 | kvm_release_pfn_clean(pfn); |
Paul Mackerras | d78bca7 | 2013-09-20 14:52:52 +1000 | [diff] [blame] | 210 | if (cpte) |
| 211 | kvmppc_mmu_hpte_cache_free(cpte); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 212 | |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 213 | out: |
| 214 | return r; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Paul Mackerras | 93b159b | 2013-09-20 14:52:51 +1000 | [diff] [blame] | 217 | void kvmppc_mmu_unmap_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte) |
| 218 | { |
| 219 | u64 mask = 0xfffffffffULL; |
| 220 | u64 vsid; |
| 221 | |
| 222 | vcpu->arch.mmu.esid_to_vsid(vcpu, pte->eaddr >> SID_SHIFT, &vsid); |
| 223 | if (vsid & VSID_64K) |
| 224 | mask = 0xffffffff0ULL; |
| 225 | kvmppc_mmu_pte_vflush(vcpu, pte->vpage, mask); |
| 226 | } |
| 227 | |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 228 | static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid) |
| 229 | { |
| 230 | struct kvmppc_sid_map *map; |
| 231 | struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu); |
| 232 | u16 sid_map_mask; |
| 233 | static int backwards_map = 0; |
| 234 | |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 235 | if (kvmppc_get_msr(vcpu) & MSR_PR) |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 236 | gvsid |= VSID_PR; |
| 237 | |
| 238 | /* We might get collisions that trap in preceding order, so let's |
| 239 | map them differently */ |
| 240 | |
| 241 | sid_map_mask = kvmppc_sid_hash(vcpu, gvsid); |
| 242 | if (backwards_map) |
| 243 | sid_map_mask = SID_MAP_MASK - sid_map_mask; |
| 244 | |
| 245 | map = &to_book3s(vcpu)->sid_map[sid_map_mask]; |
| 246 | |
| 247 | /* Make sure we're taking the other map next time */ |
| 248 | backwards_map = !backwards_map; |
| 249 | |
| 250 | /* Uh-oh ... out of mappings. Let's flush! */ |
Benjamin Herrenschmidt | ffe3649 | 2012-03-23 11:21:14 +1100 | [diff] [blame] | 251 | if (vcpu_book3s->proto_vsid_next == vcpu_book3s->proto_vsid_max) { |
| 252 | vcpu_book3s->proto_vsid_next = vcpu_book3s->proto_vsid_first; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 253 | memset(vcpu_book3s->sid_map, 0, |
| 254 | sizeof(struct kvmppc_sid_map) * SID_MAP_NUM); |
| 255 | kvmppc_mmu_pte_flush(vcpu, 0, 0); |
| 256 | kvmppc_mmu_flush_segments(vcpu); |
| 257 | } |
Benjamin Herrenschmidt | ffe3649 | 2012-03-23 11:21:14 +1100 | [diff] [blame] | 258 | map->host_vsid = vsid_scramble(vcpu_book3s->proto_vsid_next++, 256M); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 259 | |
| 260 | map->guest_vsid = gvsid; |
| 261 | map->valid = true; |
| 262 | |
Alexander Graf | 928d78b | 2010-08-02 21:25:33 +0200 | [diff] [blame] | 263 | trace_kvm_book3s_slb_map(sid_map_mask, gvsid, map->host_vsid); |
Alexander Graf | 5156f27 | 2010-04-20 02:49:52 +0200 | [diff] [blame] | 264 | |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 265 | return map; |
| 266 | } |
| 267 | |
| 268 | static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid) |
| 269 | { |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 270 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 271 | int i; |
| 272 | int max_slb_size = 64; |
| 273 | int found_inval = -1; |
| 274 | int r; |
| 275 | |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 276 | /* Are we overwriting? */ |
Alexander Graf | 207438d | 2014-05-15 14:36:05 +0200 | [diff] [blame] | 277 | for (i = 0; i < svcpu->slb_max; i++) { |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 278 | if (!(svcpu->slb[i].esid & SLB_ESID_V)) |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 279 | found_inval = i; |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 280 | else if ((svcpu->slb[i].esid & ESID_MASK) == esid) { |
| 281 | r = i; |
| 282 | goto out; |
| 283 | } |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* Found a spare entry that was invalidated before */ |
Alexander Graf | 207438d | 2014-05-15 14:36:05 +0200 | [diff] [blame] | 287 | if (found_inval >= 0) { |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 288 | r = found_inval; |
| 289 | goto out; |
| 290 | } |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 291 | |
| 292 | /* No spare invalid entry, so create one */ |
| 293 | |
| 294 | if (mmu_slb_size < 64) |
| 295 | max_slb_size = mmu_slb_size; |
| 296 | |
| 297 | /* Overflowing -> purge */ |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 298 | if ((svcpu->slb_max) == max_slb_size) |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 299 | kvmppc_mmu_flush_segments(vcpu); |
| 300 | |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 301 | r = svcpu->slb_max; |
| 302 | svcpu->slb_max++; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 303 | |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 304 | out: |
| 305 | svcpu_put(svcpu); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 306 | return r; |
| 307 | } |
| 308 | |
| 309 | int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr) |
| 310 | { |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 311 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 312 | u64 esid = eaddr >> SID_SHIFT; |
| 313 | u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V; |
| 314 | u64 slb_vsid = SLB_VSID_USER; |
| 315 | u64 gvsid; |
| 316 | int slb_index; |
| 317 | struct kvmppc_sid_map *map; |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 318 | int r = 0; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 319 | |
| 320 | slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK); |
| 321 | |
| 322 | if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) { |
| 323 | /* Invalidate an entry */ |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 324 | svcpu->slb[slb_index].esid = 0; |
| 325 | r = -ENOENT; |
| 326 | goto out; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | map = find_sid_vsid(vcpu, gvsid); |
| 330 | if (!map) |
| 331 | map = create_sid_map(vcpu, gvsid); |
| 332 | |
| 333 | map->guest_esid = esid; |
| 334 | |
| 335 | slb_vsid |= (map->host_vsid << 12); |
| 336 | slb_vsid &= ~SLB_VSID_KP; |
| 337 | slb_esid |= slb_index; |
| 338 | |
Paul Mackerras | c9029c3 | 2013-09-20 14:52:45 +1000 | [diff] [blame] | 339 | #ifdef CONFIG_PPC_64K_PAGES |
| 340 | /* Set host segment base page size to 64K if possible */ |
| 341 | if (gvsid & VSID_64K) |
| 342 | slb_vsid |= mmu_psize_defs[MMU_PAGE_64K].sllp; |
| 343 | #endif |
| 344 | |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 345 | svcpu->slb[slb_index].esid = slb_esid; |
| 346 | svcpu->slb[slb_index].vsid = slb_vsid; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 347 | |
Alexander Graf | 928d78b | 2010-08-02 21:25:33 +0200 | [diff] [blame] | 348 | trace_kvm_book3s_slbmte(slb_vsid, slb_esid); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 349 | |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 350 | out: |
| 351 | svcpu_put(svcpu); |
| 352 | return r; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Paul Mackerras | 0f29682 | 2013-06-22 17:16:32 +1000 | [diff] [blame] | 355 | void kvmppc_mmu_flush_segment(struct kvm_vcpu *vcpu, ulong ea, ulong seg_size) |
| 356 | { |
| 357 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
| 358 | ulong seg_mask = -seg_size; |
| 359 | int i; |
| 360 | |
Alexander Graf | 207438d | 2014-05-15 14:36:05 +0200 | [diff] [blame] | 361 | for (i = 0; i < svcpu->slb_max; i++) { |
Paul Mackerras | 0f29682 | 2013-06-22 17:16:32 +1000 | [diff] [blame] | 362 | if ((svcpu->slb[i].esid & SLB_ESID_V) && |
| 363 | (svcpu->slb[i].esid & seg_mask) == ea) { |
| 364 | /* Invalidate this entry */ |
| 365 | svcpu->slb[i].esid = 0; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | svcpu_put(svcpu); |
| 370 | } |
| 371 | |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 372 | void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu) |
| 373 | { |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 374 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
Alexander Graf | 207438d | 2014-05-15 14:36:05 +0200 | [diff] [blame] | 375 | svcpu->slb_max = 0; |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 376 | svcpu->slb[0].esid = 0; |
| 377 | svcpu_put(svcpu); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 380 | void kvmppc_mmu_destroy_pr(struct kvm_vcpu *vcpu) |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 381 | { |
Alexander Graf | fef093be | 2010-06-30 15:18:46 +0200 | [diff] [blame] | 382 | kvmppc_mmu_hpte_destroy(vcpu); |
Alexander Graf | 8b6db3b | 2010-08-15 08:04:24 +0200 | [diff] [blame] | 383 | __destroy_context(to_book3s(vcpu)->context_id[0]); |
Alexander Graf | 9cc5e95 | 2010-04-16 00:11:45 +0200 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | int kvmppc_mmu_init(struct kvm_vcpu *vcpu) |
| 387 | { |
| 388 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
| 389 | int err; |
| 390 | |
| 391 | err = __init_new_context(); |
| 392 | if (err < 0) |
| 393 | return -1; |
Alexander Graf | 8b6db3b | 2010-08-15 08:04:24 +0200 | [diff] [blame] | 394 | vcpu3s->context_id[0] = err; |
Alexander Graf | 9cc5e95 | 2010-04-16 00:11:45 +0200 | [diff] [blame] | 395 | |
Paul Mackerras | 8ed7b7e | 2013-06-22 17:13:32 +1000 | [diff] [blame] | 396 | vcpu3s->proto_vsid_max = ((u64)(vcpu3s->context_id[0] + 1) |
Aneesh Kumar K.V | af81d78 | 2013-03-13 03:34:55 +0000 | [diff] [blame] | 397 | << ESID_BITS) - 1; |
Paul Mackerras | 8ed7b7e | 2013-06-22 17:13:32 +1000 | [diff] [blame] | 398 | vcpu3s->proto_vsid_first = (u64)vcpu3s->context_id[0] << ESID_BITS; |
Benjamin Herrenschmidt | ffe3649 | 2012-03-23 11:21:14 +1100 | [diff] [blame] | 399 | vcpu3s->proto_vsid_next = vcpu3s->proto_vsid_first; |
Alexander Graf | 9cc5e95 | 2010-04-16 00:11:45 +0200 | [diff] [blame] | 400 | |
Alexander Graf | fef093be | 2010-06-30 15:18:46 +0200 | [diff] [blame] | 401 | kvmppc_mmu_hpte_init(vcpu); |
| 402 | |
Alexander Graf | 9cc5e95 | 2010-04-16 00:11:45 +0200 | [diff] [blame] | 403 | return 0; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 404 | } |