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 |
| 33 | #define VSID_ALL 0 |
| 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 | { |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 37 | ppc_md.hpte_invalidate(pte->slot, pte->host_va, |
| 38 | MMU_PAGE_4K, MMU_SEGSIZE_256M, |
| 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 | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 62 | if (vcpu->arch.shared->msr & 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 | |
| 82 | int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte) |
| 83 | { |
| 84 | pfn_t hpaddr; |
| 85 | ulong hash, hpteg, va; |
| 86 | u64 vsid; |
| 87 | int ret; |
| 88 | int rflags = 0x192; |
| 89 | int vflags = 0; |
| 90 | int attempt = 0; |
| 91 | struct kvmppc_sid_map *map; |
| 92 | |
| 93 | /* Get host physical address for gpa */ |
Alexander Graf | e850894 | 2010-07-29 14:47:54 +0200 | [diff] [blame] | 94 | hpaddr = kvmppc_gfn_to_pfn(vcpu, orig_pte->raddr >> PAGE_SHIFT); |
Gleb Natapov | 4945138 | 2010-07-29 15:04:18 +0200 | [diff] [blame] | 95 | if (is_error_pfn(hpaddr)) { |
Alexander Graf | af7b4d1 | 2010-04-20 02:49:46 +0200 | [diff] [blame] | 96 | printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n", orig_pte->eaddr); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 97 | return -EINVAL; |
| 98 | } |
| 99 | hpaddr <<= PAGE_SHIFT; |
Alexander Graf | e850894 | 2010-07-29 14:47:54 +0200 | [diff] [blame] | 100 | hpaddr |= orig_pte->raddr & (~0xfffULL & ~PAGE_MASK); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 101 | |
| 102 | /* and write the mapping ea -> hpa into the pt */ |
| 103 | vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid); |
| 104 | map = find_sid_vsid(vcpu, vsid); |
| 105 | if (!map) { |
Alexander Graf | ac21467 | 2010-04-20 02:49:50 +0200 | [diff] [blame] | 106 | ret = kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr); |
| 107 | WARN_ON(ret < 0); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 108 | map = find_sid_vsid(vcpu, vsid); |
| 109 | } |
Alexander Graf | ac21467 | 2010-04-20 02:49:50 +0200 | [diff] [blame] | 110 | if (!map) { |
| 111 | printk(KERN_ERR "KVM: Segment map for 0x%llx (0x%lx) failed\n", |
| 112 | vsid, orig_pte->eaddr); |
| 113 | WARN_ON(true); |
| 114 | return -EINVAL; |
| 115 | } |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 116 | |
| 117 | vsid = map->host_vsid; |
| 118 | va = hpt_va(orig_pte->eaddr, vsid, MMU_SEGSIZE_256M); |
| 119 | |
| 120 | if (!orig_pte->may_write) |
| 121 | rflags |= HPTE_R_PP; |
| 122 | else |
| 123 | mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT); |
| 124 | |
| 125 | if (!orig_pte->may_execute) |
| 126 | rflags |= HPTE_R_N; |
| 127 | |
| 128 | hash = hpt_hash(va, PTE_SIZE, MMU_SEGSIZE_256M); |
| 129 | |
| 130 | map_again: |
| 131 | hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP); |
| 132 | |
| 133 | /* In case we tried normal mapping already, let's nuke old entries */ |
| 134 | if (attempt > 1) |
| 135 | if (ppc_md.hpte_remove(hpteg) < 0) |
| 136 | return -1; |
| 137 | |
| 138 | ret = ppc_md.hpte_insert(hpteg, va, hpaddr, rflags, vflags, MMU_PAGE_4K, MMU_SEGSIZE_256M); |
| 139 | |
| 140 | if (ret < 0) { |
| 141 | /* If we couldn't map a primary PTE, try a secondary */ |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 142 | hash = ~hash; |
Alexander Graf | 20a340a | 2010-02-19 11:00:46 +0100 | [diff] [blame] | 143 | vflags ^= HPTE_V_SECONDARY; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 144 | attempt++; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 145 | goto map_again; |
| 146 | } else { |
Alexander Graf | fef093be | 2010-06-30 15:18:46 +0200 | [diff] [blame] | 147 | struct hpte_cache *pte = kvmppc_mmu_hpte_cache_next(vcpu); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 148 | |
Alexander Graf | 82fdee7 | 2010-08-02 11:38:54 +0200 | [diff] [blame] | 149 | trace_kvm_book3s_64_mmu_map(rflags, hpteg, va, hpaddr, orig_pte); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 150 | |
Alexander Graf | a1eda28 | 2010-03-24 21:48:34 +0100 | [diff] [blame] | 151 | /* The ppc_md code may give us a secondary entry even though we |
| 152 | asked for a primary. Fix up. */ |
| 153 | if ((ret & _PTEIDX_SECONDARY) && !(vflags & HPTE_V_SECONDARY)) { |
| 154 | hash = ~hash; |
| 155 | hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP); |
| 156 | } |
| 157 | |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 158 | pte->slot = hpteg + (ret & 7); |
| 159 | pte->host_va = va; |
| 160 | pte->pte = *orig_pte; |
| 161 | pte->pfn = hpaddr >> PAGE_SHIFT; |
Alexander Graf | fef093be | 2010-06-30 15:18:46 +0200 | [diff] [blame] | 162 | |
| 163 | kvmppc_mmu_hpte_cache_map(vcpu, pte); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid) |
| 170 | { |
| 171 | struct kvmppc_sid_map *map; |
| 172 | struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu); |
| 173 | u16 sid_map_mask; |
| 174 | static int backwards_map = 0; |
| 175 | |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 176 | if (vcpu->arch.shared->msr & MSR_PR) |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 177 | gvsid |= VSID_PR; |
| 178 | |
| 179 | /* We might get collisions that trap in preceding order, so let's |
| 180 | map them differently */ |
| 181 | |
| 182 | sid_map_mask = kvmppc_sid_hash(vcpu, gvsid); |
| 183 | if (backwards_map) |
| 184 | sid_map_mask = SID_MAP_MASK - sid_map_mask; |
| 185 | |
| 186 | map = &to_book3s(vcpu)->sid_map[sid_map_mask]; |
| 187 | |
| 188 | /* Make sure we're taking the other map next time */ |
| 189 | backwards_map = !backwards_map; |
| 190 | |
| 191 | /* Uh-oh ... out of mappings. Let's flush! */ |
| 192 | if (vcpu_book3s->vsid_next == vcpu_book3s->vsid_max) { |
| 193 | vcpu_book3s->vsid_next = vcpu_book3s->vsid_first; |
| 194 | memset(vcpu_book3s->sid_map, 0, |
| 195 | sizeof(struct kvmppc_sid_map) * SID_MAP_NUM); |
| 196 | kvmppc_mmu_pte_flush(vcpu, 0, 0); |
| 197 | kvmppc_mmu_flush_segments(vcpu); |
| 198 | } |
| 199 | map->host_vsid = vcpu_book3s->vsid_next++; |
| 200 | |
| 201 | map->guest_vsid = gvsid; |
| 202 | map->valid = true; |
| 203 | |
Alexander Graf | 928d78b | 2010-08-02 21:25:33 +0200 | [diff] [blame] | 204 | trace_kvm_book3s_slb_map(sid_map_mask, gvsid, map->host_vsid); |
Alexander Graf | 5156f27 | 2010-04-20 02:49:52 +0200 | [diff] [blame] | 205 | |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 206 | return map; |
| 207 | } |
| 208 | |
| 209 | static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid) |
| 210 | { |
| 211 | int i; |
| 212 | int max_slb_size = 64; |
| 213 | int found_inval = -1; |
| 214 | int r; |
| 215 | |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 216 | if (!to_svcpu(vcpu)->slb_max) |
| 217 | to_svcpu(vcpu)->slb_max = 1; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 218 | |
| 219 | /* Are we overwriting? */ |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 220 | for (i = 1; i < to_svcpu(vcpu)->slb_max; i++) { |
| 221 | if (!(to_svcpu(vcpu)->slb[i].esid & SLB_ESID_V)) |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 222 | found_inval = i; |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 223 | else if ((to_svcpu(vcpu)->slb[i].esid & ESID_MASK) == esid) |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 224 | return i; |
| 225 | } |
| 226 | |
| 227 | /* Found a spare entry that was invalidated before */ |
| 228 | if (found_inval > 0) |
| 229 | return found_inval; |
| 230 | |
| 231 | /* No spare invalid entry, so create one */ |
| 232 | |
| 233 | if (mmu_slb_size < 64) |
| 234 | max_slb_size = mmu_slb_size; |
| 235 | |
| 236 | /* Overflowing -> purge */ |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 237 | if ((to_svcpu(vcpu)->slb_max) == max_slb_size) |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 238 | kvmppc_mmu_flush_segments(vcpu); |
| 239 | |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 240 | r = to_svcpu(vcpu)->slb_max; |
| 241 | to_svcpu(vcpu)->slb_max++; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 242 | |
| 243 | return r; |
| 244 | } |
| 245 | |
| 246 | int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr) |
| 247 | { |
| 248 | u64 esid = eaddr >> SID_SHIFT; |
| 249 | u64 slb_esid = (eaddr & ESID_MASK) | SLB_ESID_V; |
| 250 | u64 slb_vsid = SLB_VSID_USER; |
| 251 | u64 gvsid; |
| 252 | int slb_index; |
| 253 | struct kvmppc_sid_map *map; |
| 254 | |
| 255 | slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK); |
| 256 | |
| 257 | if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) { |
| 258 | /* Invalidate an entry */ |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 259 | to_svcpu(vcpu)->slb[slb_index].esid = 0; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 260 | return -ENOENT; |
| 261 | } |
| 262 | |
| 263 | map = find_sid_vsid(vcpu, gvsid); |
| 264 | if (!map) |
| 265 | map = create_sid_map(vcpu, gvsid); |
| 266 | |
| 267 | map->guest_esid = esid; |
| 268 | |
| 269 | slb_vsid |= (map->host_vsid << 12); |
| 270 | slb_vsid &= ~SLB_VSID_KP; |
| 271 | slb_esid |= slb_index; |
| 272 | |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 273 | to_svcpu(vcpu)->slb[slb_index].esid = slb_esid; |
| 274 | to_svcpu(vcpu)->slb[slb_index].vsid = slb_vsid; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 275 | |
Alexander Graf | 928d78b | 2010-08-02 21:25:33 +0200 | [diff] [blame] | 276 | trace_kvm_book3s_slbmte(slb_vsid, slb_esid); |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu) |
| 282 | { |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 283 | to_svcpu(vcpu)->slb_max = 1; |
| 284 | to_svcpu(vcpu)->slb[0].esid = 0; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu) |
| 288 | { |
Alexander Graf | fef093be | 2010-06-30 15:18:46 +0200 | [diff] [blame] | 289 | kvmppc_mmu_hpte_destroy(vcpu); |
Alexander Graf | 9cc5e95 | 2010-04-16 00:11:45 +0200 | [diff] [blame] | 290 | __destroy_context(to_book3s(vcpu)->context_id); |
| 291 | } |
| 292 | |
| 293 | int kvmppc_mmu_init(struct kvm_vcpu *vcpu) |
| 294 | { |
| 295 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
| 296 | int err; |
| 297 | |
| 298 | err = __init_new_context(); |
| 299 | if (err < 0) |
| 300 | return -1; |
| 301 | vcpu3s->context_id = err; |
| 302 | |
| 303 | vcpu3s->vsid_max = ((vcpu3s->context_id + 1) << USER_ESID_BITS) - 1; |
| 304 | vcpu3s->vsid_first = vcpu3s->context_id << USER_ESID_BITS; |
| 305 | vcpu3s->vsid_next = vcpu3s->vsid_first; |
| 306 | |
Alexander Graf | fef093be | 2010-06-30 15:18:46 +0200 | [diff] [blame] | 307 | kvmppc_mmu_hpte_init(vcpu); |
| 308 | |
Alexander Graf | 9cc5e95 | 2010-04-16 00:11:45 +0200 | [diff] [blame] | 309 | return 0; |
Alexander Graf | 0d8dc68 | 2009-10-30 05:47:11 +0000 | [diff] [blame] | 310 | } |