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