~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/powerpc/kvm/book3s_64_mmu_host.c

Version: ~ [ linux-5.13-rc7 ] ~ [ linux-5.12.12 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.45 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.127 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.195 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.237 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.273 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.273 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp