Path: blob/master/arch/powerpc/kvm/book3s_32_mmu_host.c
10818 views
/*1* Copyright (C) 2010 SUSE Linux Products GmbH. All rights reserved.2*3* Authors:4* Alexander Graf <[email protected]>5*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License, version 2, as8* published by the Free Software Foundation.9*10* This program is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13* GNU General Public License for more details.14*15* You should have received a copy of the GNU General Public License16* along with this program; if not, write to the Free Software17* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.18*/1920#include <linux/kvm_host.h>2122#include <asm/kvm_ppc.h>23#include <asm/kvm_book3s.h>24#include <asm/mmu-hash32.h>25#include <asm/machdep.h>26#include <asm/mmu_context.h>27#include <asm/hw_irq.h>2829/* #define DEBUG_MMU */30/* #define DEBUG_SR */3132#ifdef DEBUG_MMU33#define dprintk_mmu(a, ...) printk(KERN_INFO a, __VA_ARGS__)34#else35#define dprintk_mmu(a, ...) do { } while(0)36#endif3738#ifdef DEBUG_SR39#define dprintk_sr(a, ...) printk(KERN_INFO a, __VA_ARGS__)40#else41#define dprintk_sr(a, ...) do { } while(0)42#endif4344#if PAGE_SHIFT != 1245#error Unknown page size46#endif4748#ifdef CONFIG_SMP49#error XXX need to grab mmu_hash_lock50#endif5152#ifdef CONFIG_PTE_64BIT53#error Only 32 bit pages are supported for now54#endif5556static ulong htab;57static u32 htabmask;5859void kvmppc_mmu_invalidate_pte(struct kvm_vcpu *vcpu, struct hpte_cache *pte)60{61volatile u32 *pteg;6263/* Remove from host HTAB */64pteg = (u32*)pte->slot;65pteg[0] = 0;6667/* And make sure it's gone from the TLB too */68asm volatile ("sync");69asm volatile ("tlbie %0" : : "r" (pte->pte.eaddr) : "memory");70asm volatile ("sync");71asm volatile ("tlbsync");72}7374/* We keep 512 gvsid->hvsid entries, mapping the guest ones to the array using75* a hash, so we don't waste cycles on looping */76static u16 kvmppc_sid_hash(struct kvm_vcpu *vcpu, u64 gvsid)77{78return (u16)(((gvsid >> (SID_MAP_BITS * 7)) & SID_MAP_MASK) ^79((gvsid >> (SID_MAP_BITS * 6)) & SID_MAP_MASK) ^80((gvsid >> (SID_MAP_BITS * 5)) & SID_MAP_MASK) ^81((gvsid >> (SID_MAP_BITS * 4)) & SID_MAP_MASK) ^82((gvsid >> (SID_MAP_BITS * 3)) & SID_MAP_MASK) ^83((gvsid >> (SID_MAP_BITS * 2)) & SID_MAP_MASK) ^84((gvsid >> (SID_MAP_BITS * 1)) & SID_MAP_MASK) ^85((gvsid >> (SID_MAP_BITS * 0)) & SID_MAP_MASK));86}878889static struct kvmppc_sid_map *find_sid_vsid(struct kvm_vcpu *vcpu, u64 gvsid)90{91struct kvmppc_sid_map *map;92u16 sid_map_mask;9394if (vcpu->arch.shared->msr & MSR_PR)95gvsid |= VSID_PR;9697sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);98map = &to_book3s(vcpu)->sid_map[sid_map_mask];99if (map->guest_vsid == gvsid) {100dprintk_sr("SR: Searching 0x%llx -> 0x%llx\n",101gvsid, map->host_vsid);102return map;103}104105map = &to_book3s(vcpu)->sid_map[SID_MAP_MASK - sid_map_mask];106if (map->guest_vsid == gvsid) {107dprintk_sr("SR: Searching 0x%llx -> 0x%llx\n",108gvsid, map->host_vsid);109return map;110}111112dprintk_sr("SR: Searching 0x%llx -> not found\n", gvsid);113return NULL;114}115116static u32 *kvmppc_mmu_get_pteg(struct kvm_vcpu *vcpu, u32 vsid, u32 eaddr,117bool primary)118{119u32 page, hash;120ulong pteg = htab;121122page = (eaddr & ~ESID_MASK) >> 12;123124hash = ((vsid ^ page) << 6);125if (!primary)126hash = ~hash;127128hash &= htabmask;129130pteg |= hash;131132dprintk_mmu("htab: %lx | hash: %x | htabmask: %x | pteg: %lx\n",133htab, hash, htabmask, pteg);134135return (u32*)pteg;136}137138extern char etext[];139140int kvmppc_mmu_map_page(struct kvm_vcpu *vcpu, struct kvmppc_pte *orig_pte)141{142pfn_t hpaddr;143u64 va;144u64 vsid;145struct kvmppc_sid_map *map;146volatile u32 *pteg;147u32 eaddr = orig_pte->eaddr;148u32 pteg0, pteg1;149register int rr = 0;150bool primary = false;151bool evict = false;152struct hpte_cache *pte;153154/* Get host physical address for gpa */155hpaddr = kvmppc_gfn_to_pfn(vcpu, orig_pte->raddr >> PAGE_SHIFT);156if (is_error_pfn(hpaddr)) {157printk(KERN_INFO "Couldn't get guest page for gfn %lx!\n",158orig_pte->eaddr);159return -EINVAL;160}161hpaddr <<= PAGE_SHIFT;162163/* and write the mapping ea -> hpa into the pt */164vcpu->arch.mmu.esid_to_vsid(vcpu, orig_pte->eaddr >> SID_SHIFT, &vsid);165map = find_sid_vsid(vcpu, vsid);166if (!map) {167kvmppc_mmu_map_segment(vcpu, eaddr);168map = find_sid_vsid(vcpu, vsid);169}170BUG_ON(!map);171172vsid = map->host_vsid;173va = (vsid << SID_SHIFT) | (eaddr & ~ESID_MASK);174175next_pteg:176if (rr == 16) {177primary = !primary;178evict = true;179rr = 0;180}181182pteg = kvmppc_mmu_get_pteg(vcpu, vsid, eaddr, primary);183184/* not evicting yet */185if (!evict && (pteg[rr] & PTE_V)) {186rr += 2;187goto next_pteg;188}189190dprintk_mmu("KVM: old PTEG: %p (%d)\n", pteg, rr);191dprintk_mmu("KVM: %08x - %08x\n", pteg[0], pteg[1]);192dprintk_mmu("KVM: %08x - %08x\n", pteg[2], pteg[3]);193dprintk_mmu("KVM: %08x - %08x\n", pteg[4], pteg[5]);194dprintk_mmu("KVM: %08x - %08x\n", pteg[6], pteg[7]);195dprintk_mmu("KVM: %08x - %08x\n", pteg[8], pteg[9]);196dprintk_mmu("KVM: %08x - %08x\n", pteg[10], pteg[11]);197dprintk_mmu("KVM: %08x - %08x\n", pteg[12], pteg[13]);198dprintk_mmu("KVM: %08x - %08x\n", pteg[14], pteg[15]);199200pteg0 = ((eaddr & 0x0fffffff) >> 22) | (vsid << 7) | PTE_V |201(primary ? 0 : PTE_SEC);202pteg1 = hpaddr | PTE_M | PTE_R | PTE_C;203204if (orig_pte->may_write) {205pteg1 |= PP_RWRW;206mark_page_dirty(vcpu->kvm, orig_pte->raddr >> PAGE_SHIFT);207} else {208pteg1 |= PP_RWRX;209}210211local_irq_disable();212213if (pteg[rr]) {214pteg[rr] = 0;215asm volatile ("sync");216}217pteg[rr + 1] = pteg1;218pteg[rr] = pteg0;219asm volatile ("sync");220221local_irq_enable();222223dprintk_mmu("KVM: new PTEG: %p\n", pteg);224dprintk_mmu("KVM: %08x - %08x\n", pteg[0], pteg[1]);225dprintk_mmu("KVM: %08x - %08x\n", pteg[2], pteg[3]);226dprintk_mmu("KVM: %08x - %08x\n", pteg[4], pteg[5]);227dprintk_mmu("KVM: %08x - %08x\n", pteg[6], pteg[7]);228dprintk_mmu("KVM: %08x - %08x\n", pteg[8], pteg[9]);229dprintk_mmu("KVM: %08x - %08x\n", pteg[10], pteg[11]);230dprintk_mmu("KVM: %08x - %08x\n", pteg[12], pteg[13]);231dprintk_mmu("KVM: %08x - %08x\n", pteg[14], pteg[15]);232233234/* Now tell our Shadow PTE code about the new page */235236pte = kvmppc_mmu_hpte_cache_next(vcpu);237238dprintk_mmu("KVM: %c%c Map 0x%llx: [%lx] 0x%llx (0x%llx) -> %lx\n",239orig_pte->may_write ? 'w' : '-',240orig_pte->may_execute ? 'x' : '-',241orig_pte->eaddr, (ulong)pteg, va,242orig_pte->vpage, hpaddr);243244pte->slot = (ulong)&pteg[rr];245pte->host_va = va;246pte->pte = *orig_pte;247pte->pfn = hpaddr >> PAGE_SHIFT;248249kvmppc_mmu_hpte_cache_map(vcpu, pte);250251return 0;252}253254static struct kvmppc_sid_map *create_sid_map(struct kvm_vcpu *vcpu, u64 gvsid)255{256struct kvmppc_sid_map *map;257struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);258u16 sid_map_mask;259static int backwards_map = 0;260261if (vcpu->arch.shared->msr & MSR_PR)262gvsid |= VSID_PR;263264/* We might get collisions that trap in preceding order, so let's265map them differently */266267sid_map_mask = kvmppc_sid_hash(vcpu, gvsid);268if (backwards_map)269sid_map_mask = SID_MAP_MASK - sid_map_mask;270271map = &to_book3s(vcpu)->sid_map[sid_map_mask];272273/* Make sure we're taking the other map next time */274backwards_map = !backwards_map;275276/* Uh-oh ... out of mappings. Let's flush! */277if (vcpu_book3s->vsid_next >= VSID_POOL_SIZE) {278vcpu_book3s->vsid_next = 0;279memset(vcpu_book3s->sid_map, 0,280sizeof(struct kvmppc_sid_map) * SID_MAP_NUM);281kvmppc_mmu_pte_flush(vcpu, 0, 0);282kvmppc_mmu_flush_segments(vcpu);283}284map->host_vsid = vcpu_book3s->vsid_pool[vcpu_book3s->vsid_next];285vcpu_book3s->vsid_next++;286287map->guest_vsid = gvsid;288map->valid = true;289290return map;291}292293int kvmppc_mmu_map_segment(struct kvm_vcpu *vcpu, ulong eaddr)294{295u32 esid = eaddr >> SID_SHIFT;296u64 gvsid;297u32 sr;298struct kvmppc_sid_map *map;299struct kvmppc_book3s_shadow_vcpu *svcpu = to_svcpu(vcpu);300301if (vcpu->arch.mmu.esid_to_vsid(vcpu, esid, &gvsid)) {302/* Invalidate an entry */303svcpu->sr[esid] = SR_INVALID;304return -ENOENT;305}306307map = find_sid_vsid(vcpu, gvsid);308if (!map)309map = create_sid_map(vcpu, gvsid);310311map->guest_esid = esid;312sr = map->host_vsid | SR_KP;313svcpu->sr[esid] = sr;314315dprintk_sr("MMU: mtsr %d, 0x%x\n", esid, sr);316317return 0;318}319320void kvmppc_mmu_flush_segments(struct kvm_vcpu *vcpu)321{322int i;323struct kvmppc_book3s_shadow_vcpu *svcpu = to_svcpu(vcpu);324325dprintk_sr("MMU: flushing all segments (%d)\n", ARRAY_SIZE(svcpu->sr));326for (i = 0; i < ARRAY_SIZE(svcpu->sr); i++)327svcpu->sr[i] = SR_INVALID;328}329330void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)331{332int i;333334kvmppc_mmu_hpte_destroy(vcpu);335preempt_disable();336for (i = 0; i < SID_CONTEXTS; i++)337__destroy_context(to_book3s(vcpu)->context_id[i]);338preempt_enable();339}340341/* From mm/mmu_context_hash32.c */342#define CTX_TO_VSID(c, id) ((((c) * (897 * 16)) + (id * 0x111)) & 0xffffff)343344int kvmppc_mmu_init(struct kvm_vcpu *vcpu)345{346struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);347int err;348ulong sdr1;349int i;350int j;351352for (i = 0; i < SID_CONTEXTS; i++) {353err = __init_new_context();354if (err < 0)355goto init_fail;356vcpu3s->context_id[i] = err;357358/* Remember context id for this combination */359for (j = 0; j < 16; j++)360vcpu3s->vsid_pool[(i * 16) + j] = CTX_TO_VSID(err, j);361}362363vcpu3s->vsid_next = 0;364365/* Remember where the HTAB is */366asm ( "mfsdr1 %0" : "=r"(sdr1) );367htabmask = ((sdr1 & 0x1FF) << 16) | 0xFFC0;368htab = (ulong)__va(sdr1 & 0xffff0000);369370kvmppc_mmu_hpte_init(vcpu);371372return 0;373374init_fail:375for (j = 0; j < i; j++) {376if (!vcpu3s->context_id[j])377continue;378379__destroy_context(to_book3s(vcpu)->context_id[j]);380}381382return -1;383}384385386