1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2009 SUSE Linux Products GmbH. All rights reserved.
6 * Alexander Graf <agraf@suse.de>
7 * Kevin Wolf <mail@kevin-wolf.de>
10 #include <linux/kvm_host.h>
11 #include <linux/pkeys.h>
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>
24 void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)
26 mmu_hash_ops.hpte_invalidate(pte->slot, pte->host_vpn,
27 pte->pagesize, pte->pagesize,
28 MMU_SEGSIZE_256M, false);
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)
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));
46 static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)
48 struct kvmppc_sid_map *map;
51 if (kvmppc_get_msr(vcpu) & MSR_PR)
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);
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);
67 trace_kvm_book3s_slb_fail(sid_map_mask, gvsid);
71 int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte,
82 struct kvmppc_sid_map *map;
84 int hpsize = MMU_PAGE_4K;
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;
92 /* used to check for invalidations in progress */
93 mmu_seq = kvm->mmu_notifier_seq;
96 /* Get host physical address for gpa */
97 pfn = kvmppc_gpa_to_pfn(vcpu, orig_pte->raddr, iswrite, &writable);
98 if (is_error_noslot_pfn(pfn)) {
99 printk(KERN_INFO "Couldn't get guest page for gpa %lx!\n",
104 hpaddr = pfn << PAGE_SHIFT;
106 /* and write the mapping ea -> hpa into the pt */
107 vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);
108 map = find_sid_vsid(vcpu, vsid);
110 ret = kvmppc_mmu_map_segment(vcpu, orig_pte->eaddr);
112 map = find_sid_vsid(vcpu, vsid);
115 printk(KERN_ERR "KVM: Segment map for 0x%llx (0x%lx) failed\n",
116 vsid, orig_pte->eaddr);
122 vpn = hpt_vpn(orig_pte->eaddr, map->host_vsid, MMU_SEGSIZE_256M);
124 kvm_set_pfn_accessed(pfn);
125 if (!orig_pte->may_write || !writable)
128 mark_page_dirty(vcpu->kvm, gfn);
129 kvm_set_pfn_dirty(pfn);
132 if (!orig_pte->may_execute)
135 kvmppc_mmu_flush_icache(pfn);
137 rflags |= pte_to_hpte_pkey_bits(0, HPTE_USE_KERNEL_KEY);
138 rflags = (rflags & ~HPTE_R_WIMG) | orig_pte->wimg;
141 * Use 64K pages if possible; otherwise, on 64K page kernels,
142 * we need to transfer 4 more bits from guest real to host real addr.
145 hpsize = MMU_PAGE_64K;
147 hpaddr |= orig_pte->raddr & (~0xfffULL & ~PAGE_MASK);
149 hash = hpt_hash(vpn, mmu_psize_defs[hpsize].shift, MMU_SEGSIZE_256M);
151 cpte = kvmppc_mmu_hpte_cache_next(vcpu);
153 spin_lock(&kvm->mmu_lock);
154 if (!cpte || mmu_notifier_retry(kvm, mmu_seq)) {
160 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
162 /* In case we tried normal mapping already, let's nuke old entries */
164 if (mmu_hash_ops.hpte_remove(hpteg) < 0) {
169 ret = mmu_hash_ops.hpte_insert(hpteg, vpn, hpaddr, rflags, vflags,
170 hpsize, hpsize, MMU_SEGSIZE_256M);
173 /* If we couldn't map a primary PTE, try a secondary */
175 vflags ^= HPTE_V_SECONDARY;
178 } else if (ret < 0) {
182 trace_kvm_book3s_64_mmu_map(rflags, hpteg,
183 vpn, hpaddr, orig_pte);
186 * The mmu_hash_ops code may give us a secondary entry even
187 * though we asked for a primary. Fix up.
189 if ((ret & _PTEIDX_SECONDARY) && !(vflags & HPTE_V_SECONDARY)) {
191 hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
194 cpte->slot = hpteg + (ret & 7);
195 cpte->host_vpn = vpn;
196 cpte->pte = *orig_pte;
198 cpte->pagesize = hpsize;
200 kvmppc_mmu_hpte_cache_map(vcpu, cpte);
205 spin_unlock(&kvm->mmu_lock);
206 kvm_release_pfn_clean(pfn);
208 kvmppc_mmu_hpte_cache_free(cpte);
214 void kvmppc_mmu_unmap_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
216 u64 mask = 0xfffffffffULL;
219 vcpu->arch.mmu.esid_to_vsid(vcpu, pte->eaddr >> SID_SHIFT, &vsid);
221 mask = 0xffffffff0ULL;
222 kvmppc_mmu_pte_vflush(vcpu, pte->vpage, mask);
225 static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)
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);
231 static int backwards_map = 0;
233 if (kvmppc_get_msr(vcpu) & MSR_PR)
236 /* We might get collisions that trap in preceding order, so let's
237 map them differently */
239 sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);
241 sid_map_mask = SID_MAP_MASK - sid_map_mask;
243 map = &to_book3s(vcpu)->sid_map[sid_map_mask];
245 /* Make sure we're taking the other map next time */
246 backwards_map = !backwards_map;
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);
257 if (mmu_has_feature(MMU_FTR_68_BIT_VA))
258 vsid_bits = VSID_BITS_256M;
260 map->host_vsid = vsid_scramble(vcpu_book3s->proto_vsid_next++,
261 VSID_MULTIPLIER_256M, vsid_bits);
263 map->guest_vsid = gvsid;
266 trace_kvm_book3s_slb_map(sid_map_mask, gvsid, map->host_vsid);
271 static int kvmppc_mmu_next_segment(struct kvm_vcpu *vcpu, ulong esid)
273 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
275 int max_slb_size = 64;
276 int found_inval = -1;
279 /* Are we overwriting? */
280 for (i = 0; i < svcpu->slb_max; i++) {
281 if (!(svcpu->slb[i].esid & SLB_ESID_V))
283 else if ((svcpu->slb[i].esid & ESID_MASK) == esid) {
289 /* Found a spare entry that was invalidated before */
290 if (found_inval >= 0) {
295 /* No spare invalid entry, so create one */
297 if (mmu_slb_size < 64)
298 max_slb_size = mmu_slb_size;
300 /* Overflowing -> purge */
301 if ((svcpu->slb_max) == max_slb_size)
302 kvmppc_mmu_flush_segments(vcpu);
312 int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)
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;
320 struct kvmppc_sid_map *map;
323 slb_index = kvmppc_mmu_next_segment(vcpu, eaddr & ESID_MASK);
325 if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {
326 /* Invalidate an entry */
327 svcpu->slb[slb_index].esid = 0;
332 map = find_sid_vsid(vcpu, gvsid);
334 map = create_sid_map(vcpu, gvsid);
336 map->guest_esid = esid;
338 slb_vsid |= (map->host_vsid << 12);
339 slb_vsid &= ~SLB_VSID_KP;
340 slb_esid |= slb_index;
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;
348 svcpu->slb[slb_index].esid = slb_esid;
349 svcpu->slb[slb_index].vsid = slb_vsid;
351 trace_kvm_book3s_slbmte(slb_vsid, slb_esid);
358 void kvmppc_mmu_flush_segment(struct kvm_vcpu *vcpu, ulong ea, ulong seg_size)
360 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
361 ulong seg_mask = -seg_size;
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;
375 void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)
377 struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu);
379 svcpu->slb[0].esid = 0;
383 void kvmppc_mmu_destroy_pr(struct kvm_vcpu *vcpu)
385 kvmppc_mmu_hpte_destroy(vcpu);
386 __destroy_context(to_book3s(vcpu)->context_id[0]);
389 int kvmppc_mmu_init_pr(struct kvm_vcpu *vcpu)
391 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
394 err = hash__alloc_context_id();
397 vcpu3s->context_id[0] = err;
399 vcpu3s->proto_vsid_max = ((u64)(vcpu3s->context_id[0] + 1)
401 vcpu3s->proto_vsid_first = (u64)vcpu3s->context_id[0] << ESID_BITS;
402 vcpu3s->proto_vsid_next = vcpu3s->proto_vsid_first;
404 kvmppc_mmu_hpte_init(vcpu);