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> |
| 26 | #include <asm/mmu-hash64.h> |
| 27 | #include <asm/machdep.h> |
| 28 | #include <asm/mmu_context.h> |
| 29 | #include <asm/hw_irq.h> |
Alexander Graf | 82fdee7 | 2010-08-02 11:38:54 +0200 | [diff] [blame] | 30 | #include "trace.h" |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 31 | |
| 32 | #define PTE_SIZE 12 |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 33 | |
Alexander Graf | fef093be | 2010-06-30 15:18:46 +0200 | [diff] [blame] | 34 | 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] | 35 | { |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 36 | ppc_md.hpte_invalidate(pte->slot, pte->host_vpn, |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 37 | MMU_PAGE_4K, MMU_SEGSIZE_256M, |
| 38 | false); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using |
| 42 | * a hash, so we don't waste cycles on looping */ |
| 43 | static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid) |
| 44 | { |
Alexander Graf | b9877ce | 2010-08-02 21:48:53 +0200 | [diff] [blame] | 45 | return (u16)(((gvsid >> (SID_MAP_BITS * 7)) & SID_MAP_MASK) ^ |
| 46 | ((gvsid >> (SID_MAP_BITS * 6)) & SID_MAP_MASK) ^ |
| 47 | ((gvsid >> (SID_MAP_BITS * 5)) & SID_MAP_MASK) ^ |
| 48 | ((gvsid >> (SID_MAP_BITS * 4)) & SID_MAP_MASK) ^ |
| 49 | ((gvsid >> (SID_MAP_BITS * 3)) & SID_MAP_MASK) ^ |
| 50 | ((gvsid >> (SID_MAP_BITS * 2)) & SID_MAP_MASK) ^ |
| 51 | ((gvsid >> (SID_MAP_BITS * 1)) & SID_MAP_MASK) ^ |
| 52 | ((gvsid >> (SID_MAP_BITS * 0)) & SID_MAP_MASK)); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Alexander Graf | b9877ce | 2010-08-02 21:48:53 +0200 | [diff] [blame] | 55 | |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 56 | static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid) |
| 57 | { |
| 58 | struct kvmppc_sid_map *map; |
| 59 | u16 sid_map_mask; |
| 60 | |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 61 | if (vcpu->arch.shared->msr & MSR_PR) |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 62 | gvsid |= VSID_PR; |
| 63 | |
| 64 | sid_map_mask = kvmppc_sid_hash(vcpu, gvsid); |
| 65 | map = &to_book3s(vcpu)->sid_map[sid_map_mask]; |
Alexander Graf | c22c319 | 2010-08-02 13:38:18 +0200 | [diff] [blame] | 66 | if (map->valid && (map->guest_vsid == gvsid)) { |
Alexander Graf | 928d78b | 2010-08-02 21:25:33 +0200 | [diff] [blame] | 67 | trace_kvm_book3s_slb_found(gvsid, map->host_vsid); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 68 | return map; |
| 69 | } |
| 70 | |
| 71 | map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask]; |
Alexander Graf | c22c319 | 2010-08-02 13:38:18 +0200 | [diff] [blame] | 72 | if (map->valid && (map->guest_vsid == gvsid)) { |
Alexander Graf | 928d78b | 2010-08-02 21:25:33 +0200 | [diff] [blame] | 73 | trace_kvm_book3s_slb_found(gvsid, map->host_vsid); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 74 | return map; |
| 75 | } |
| 76 | |
Alexander Graf | 928d78b | 2010-08-02 21:25:33 +0200 | [diff] [blame] | 77 | trace_kvm_book3s_slb_fail(sid_map_mask, gvsid); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte) |
| 82 | { |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 83 | unsigned long vpn; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 84 | pfn_t hpaddr; |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 85 | ulong hash, hpteg; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 86 | u64 vsid; |
| 87 | int ret; |
| 88 | int rflags = 0x192; |
| 89 | int vflags = 0; |
| 90 | int attempt = 0; |
| 91 | struct kvmppc_sid_map *map; |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 92 | int r = 0; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 93 | |
| 94 | /* Get host physical address for gpa */ |
Alexander Graf | e850894 | 2010-07-29 14:47:54 +0200 | [diff] [blame] | 95 | hpaddr = kvmppc_gfn_to_pfn(vcpu, orig_pte->raddr >> PAGE_SHIFT); |
Xiao Guangrong | 81c52c5 | 2012-10-16 20:10:59 +0800 | [diff] [blame] | 96 | if (is_error_noslot_pfn(hpaddr)) { |
Alexander Graf | af7b4d1 | 2010-04-20 02:49:46 +0200 | [diff] [blame] | 97 | printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n", orig_pte->eaddr); |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 98 | r = -EINVAL; |
| 99 | goto out; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 100 | } |
| 101 | hpaddr <<= PAGE_SHIFT; |
Alexander Graf | e850894 | 2010-07-29 14:47:54 +0200 | [diff] [blame] | 102 | hpaddr |= orig_pte->raddr & (~0xfffULL & ~PAGE_MASK); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 103 | |
| 104 | /* and write the mapping ea -> hpa into the pt */ |
| 105 | vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid); |
| 106 | map = find_sid_vsid(vcpu, vsid); |
| 107 | if (!map) { |
Alexander Graf | ac21467 | 2010-04-20 02:49:50 +0200 | [diff] [blame] | 108 | ret = kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr); |
| 109 | WARN_ON(ret < 0); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 110 | map = find_sid_vsid(vcpu, vsid); |
| 111 | } |
Alexander Graf | ac21467 | 2010-04-20 02:49:50 +0200 | [diff] [blame] | 112 | if (!map) { |
| 113 | printk(KERN_ERR "KVM: Segment map for 0x%llx (0x%lx) failed\n", |
| 114 | vsid, orig_pte->eaddr); |
| 115 | WARN_ON(true); |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 116 | r = -EINVAL; |
| 117 | goto out; |
Alexander Graf | ac21467 | 2010-04-20 02:49:50 +0200 | [diff] [blame] | 118 | } |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 119 | |
| 120 | vsid = map->host_vsid; |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 121 | vpn = hpt_vpn(orig_pte->eaddr, vsid, MMU_SEGSIZE_256M); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 122 | |
| 123 | if (!orig_pte->may_write) |
| 124 | rflags |= HPTE_R_PP; |
| 125 | else |
| 126 | mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT); |
| 127 | |
| 128 | if (!orig_pte->may_execute) |
| 129 | rflags |= HPTE_R_N; |
Alexander Graf | 249ba1e | 2012-08-03 13:56:33 +0200 | [diff] [blame] | 130 | else |
| 131 | kvmppc_mmu_flush_icache(hpaddr >> PAGE_SHIFT); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 132 | |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 133 | hash = hpt_hash(vpn, PTE_SIZE, MMU_SEGSIZE_256M); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 134 | |
| 135 | map_again: |
| 136 | hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP); |
| 137 | |
| 138 | /* In case we tried normal mapping already, let's nuke old entries */ |
| 139 | if (attempt > 1) |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 140 | if (ppc_md.hpte_remove(hpteg) < 0) { |
| 141 | r = -1; |
| 142 | goto out; |
| 143 | } |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 144 | |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 145 | ret = ppc_md.hpte_insert(hpteg, vpn, hpaddr, rflags, vflags, |
Aneesh Kumar K.V | b1022fb | 2013-04-28 09:37:35 +0000 | [diff] [blame] | 146 | MMU_PAGE_4K, MMU_PAGE_4K, MMU_SEGSIZE_256M); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 147 | |
| 148 | if (ret < 0) { |
| 149 | /* If we couldn't map a primary PTE, try a secondary */ |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 150 | hash = ~hash; |
Alexander Graf | 20a340a | 2010-02-19 11:00:46 +0100 | [diff] [blame] | 151 | vflags ^= HPTE_V_SECONDARY; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 152 | attempt++; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 153 | goto map_again; |
| 154 | } else { |
Alexander Graf | fef093be | 2010-06-30 15:18:46 +0200 | [diff] [blame] | 155 | struct hpte_cache *pte = kvmppc_mmu_hpte_cache_next(vcpu); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 156 | |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 157 | trace_kvm_book3s_64_mmu_map(rflags, hpteg, |
| 158 | vpn, hpaddr, orig_pte); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 159 | |
Alexander Graf | a1eda28 | 2010-03-24 21:48:34 +0100 | [diff] [blame] | 160 | /* The ppc_md code may give us a secondary entry even though we |
| 161 | asked for a primary. Fix up. */ |
| 162 | if ((ret & _PTEIDX_SECONDARY) && !(vflags & HPTE_V_SECONDARY)) { |
| 163 | hash = ~hash; |
| 164 | hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP); |
| 165 | } |
| 166 | |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 167 | pte->slot = hpteg + (ret & 7); |
Aneesh Kumar K.V | 5524a27 | 2012-09-10 02:52:50 +0000 | [diff] [blame] | 168 | pte->host_vpn = vpn; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 169 | pte->pte = *orig_pte; |
| 170 | pte->pfn = hpaddr >> PAGE_SHIFT; |
Alexander Graf | fef093be | 2010-06-30 15:18:46 +0200 | [diff] [blame] | 171 | |
| 172 | kvmppc_mmu_hpte_cache_map(vcpu, pte); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 173 | } |
Alexander Graf | 9b0cb3c | 2012-08-10 13:23:55 +0200 | [diff] [blame] | 174 | kvm_release_pfn_clean(hpaddr >> PAGE_SHIFT); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 175 | |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 176 | out: |
| 177 | return r; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid) |
| 181 | { |
| 182 | struct kvmppc_sid_map *map; |
| 183 | struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu); |
| 184 | u16 sid_map_mask; |
| 185 | static int backwards_map = 0; |
| 186 | |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 187 | if (vcpu->arch.shared->msr & MSR_PR) |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 188 | gvsid |= VSID_PR; |
| 189 | |
| 190 | /* We might get collisions that trap in preceding order, so let's |
| 191 | map them differently */ |
| 192 | |
| 193 | sid_map_mask = kvmppc_sid_hash(vcpu, gvsid); |
| 194 | if (backwards_map) |
| 195 | sid_map_mask = SID_MAP_MASK - sid_map_mask; |
| 196 | |
| 197 | map = &to_book3s(vcpu)->sid_map[sid_map_mask]; |
| 198 | |
| 199 | /* Make sure we're taking the other map next time */ |
| 200 | backwards_map = !backwards_map; |
| 201 | |
| 202 | /* Uh-oh ... out of mappings. Let's flush! */ |
Benjamin Herrenschmidt | ffe3649 | 2012-03-23 11:21:14 +1100 | [diff] [blame] | 203 | if (vcpu_book3s->proto_vsid_next == vcpu_book3s->proto_vsid_max) { |
| 204 | vcpu_book3s->proto_vsid_next = vcpu_book3s->proto_vsid_first; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 205 | memset(vcpu_book3s->sid_map, 0, |
| 206 | sizeof(struct kvmppc_sid_map) * SID_MAP_NUM); |
| 207 | kvmppc_mmu_pte_flush(vcpu, 0, 0); |
| 208 | kvmppc_mmu_flush_segments(vcpu); |
| 209 | } |
Benjamin Herrenschmidt | ffe3649 | 2012-03-23 11:21:14 +1100 | [diff] [blame] | 210 | map->host_vsid = vsid_scramble(vcpu_book3s->proto_vsid_next++, 256M); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 211 | |
| 212 | map->guest_vsid = gvsid; |
| 213 | map->valid = true; |
| 214 | |
Alexander Graf | 928d78b | 2010-08-02 21:25:33 +0200 | [diff] [blame] | 215 | trace_kvm_book3s_slb_map(sid_map_mask, gvsid, map->host_vsid); |
Alexander Graf | 5156f27 | 2010-04-20 02:49:52 +0200 | [diff] [blame] | 216 | |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 217 | return map; |
| 218 | } |
| 219 | |
| 220 | static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid) |
| 221 | { |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 222 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 223 | int i; |
| 224 | int max_slb_size = 64; |
| 225 | int found_inval = -1; |
| 226 | int r; |
| 227 | |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 228 | if (!svcpu->slb_max) |
| 229 | svcpu->slb_max = 1; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 230 | |
| 231 | /* Are we overwriting? */ |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 232 | for (i = 1; i < svcpu->slb_max; i++) { |
| 233 | if (!(svcpu->slb[i].esid & SLB_ESID_V)) |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 234 | found_inval = i; |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 235 | else if ((svcpu->slb[i].esid & ESID_MASK) == esid) { |
| 236 | r = i; |
| 237 | goto out; |
| 238 | } |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* Found a spare entry that was invalidated before */ |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 242 | if (found_inval > 0) { |
| 243 | r = found_inval; |
| 244 | goto out; |
| 245 | } |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 246 | |
| 247 | /* No spare invalid entry, so create one */ |
| 248 | |
| 249 | if (mmu_slb_size < 64) |
| 250 | max_slb_size = mmu_slb_size; |
| 251 | |
| 252 | /* Overflowing -> purge */ |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 253 | if ((svcpu->slb_max) == max_slb_size) |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 254 | kvmppc_mmu_flush_segments(vcpu); |
| 255 | |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 256 | r = svcpu->slb_max; |
| 257 | svcpu->slb_max++; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 258 | |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 259 | out: |
| 260 | svcpu_put(svcpu); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 261 | return r; |
| 262 | } |
| 263 | |
| 264 | int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr) |
| 265 | { |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 266 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 267 | u64 esid = eaddr >> SID_SHIFT; |
| 268 | u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V; |
| 269 | u64 slb_vsid = SLB_VSID_USER; |
| 270 | u64 gvsid; |
| 271 | int slb_index; |
| 272 | struct kvmppc_sid_map *map; |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 273 | int r = 0; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 274 | |
| 275 | slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK); |
| 276 | |
| 277 | if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) { |
| 278 | /* Invalidate an entry */ |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 279 | svcpu->slb[slb_index].esid = 0; |
| 280 | r = -ENOENT; |
| 281 | goto out; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | map = find_sid_vsid(vcpu, gvsid); |
| 285 | if (!map) |
| 286 | map = create_sid_map(vcpu, gvsid); |
| 287 | |
| 288 | map->guest_esid = esid; |
| 289 | |
| 290 | slb_vsid |= (map->host_vsid << 12); |
| 291 | slb_vsid &= ~SLB_VSID_KP; |
| 292 | slb_esid |= slb_index; |
| 293 | |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 294 | svcpu->slb[slb_index].esid = slb_esid; |
| 295 | svcpu->slb[slb_index].vsid = slb_vsid; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 296 | |
Alexander Graf | 928d78b | 2010-08-02 21:25:33 +0200 | [diff] [blame] | 297 | trace_kvm_book3s_slbmte(slb_vsid, slb_esid); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 298 | |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 299 | out: |
| 300 | svcpu_put(svcpu); |
| 301 | return r; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu) |
| 305 | { |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 306 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
| 307 | svcpu->slb_max = 1; |
| 308 | svcpu->slb[0].esid = 0; |
| 309 | svcpu_put(svcpu); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu) |
| 313 | { |
Alexander Graf | fef093be | 2010-06-30 15:18:46 +0200 | [diff] [blame] | 314 | kvmppc_mmu_hpte_destroy(vcpu); |
Alexander Graf | 8b6db3b | 2010-08-15 08:04:24 +0200 | [diff] [blame] | 315 | __destroy_context(to_book3s(vcpu)->context_id[0]); |
Alexander Graf | 9cc5e95 | 2010-04-16 00:11:45 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | int kvmppc_mmu_init(struct kvm_vcpu *vcpu) |
| 319 | { |
| 320 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
| 321 | int err; |
| 322 | |
| 323 | err = __init_new_context(); |
| 324 | if (err < 0) |
| 325 | return -1; |
Alexander Graf | 8b6db3b | 2010-08-15 08:04:24 +0200 | [diff] [blame] | 326 | vcpu3s->context_id[0] = err; |
Alexander Graf | 9cc5e95 | 2010-04-16 00:11:45 +0200 | [diff] [blame] | 327 | |
Benjamin Herrenschmidt | ffe3649 | 2012-03-23 11:21:14 +1100 | [diff] [blame] | 328 | vcpu3s->proto_vsid_max = ((vcpu3s->context_id[0] + 1) |
Aneesh Kumar K.V | af81d78 | 2013-03-13 03:34:55 +0000 | [diff] [blame] | 329 | << ESID_BITS) - 1; |
| 330 | vcpu3s->proto_vsid_first = vcpu3s->context_id[0] << ESID_BITS; |
Benjamin Herrenschmidt | ffe3649 | 2012-03-23 11:21:14 +1100 | [diff] [blame] | 331 | vcpu3s->proto_vsid_next = vcpu3s->proto_vsid_first; |
Alexander Graf | 9cc5e95 | 2010-04-16 00:11:45 +0200 | [diff] [blame] | 332 | |
Alexander Graf | fef093be | 2010-06-30 15:18:46 +0200 | [diff] [blame] | 333 | kvmppc_mmu_hpte_init(vcpu); |
| 334 | |
Alexander Graf | 9cc5e95 | 2010-04-16 00:11:45 +0200 | [diff] [blame] | 335 | return 0; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 336 | } |