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