1d9fbd03dSHollis Blanchard /* 2d9fbd03dSHollis Blanchard * This program is free software; you can redistribute it and/or modify 3d9fbd03dSHollis Blanchard * it under the terms of the GNU General Public License, version 2, as 4d9fbd03dSHollis Blanchard * published by the Free Software Foundation. 5d9fbd03dSHollis Blanchard * 6d9fbd03dSHollis Blanchard * This program is distributed in the hope that it will be useful, 7d9fbd03dSHollis Blanchard * but WITHOUT ANY WARRANTY; without even the implied warranty of 8d9fbd03dSHollis Blanchard * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9d9fbd03dSHollis Blanchard * GNU General Public License for more details. 10d9fbd03dSHollis Blanchard * 11d9fbd03dSHollis Blanchard * You should have received a copy of the GNU General Public License 12d9fbd03dSHollis Blanchard * along with this program; if not, write to the Free Software 13d9fbd03dSHollis Blanchard * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 14d9fbd03dSHollis Blanchard * 15d9fbd03dSHollis Blanchard * Copyright IBM Corp. 2007 164cd35f67SScott Wood * Copyright 2010-2011 Freescale Semiconductor, Inc. 17d9fbd03dSHollis Blanchard * 18d9fbd03dSHollis Blanchard * Authors: Hollis Blanchard <hollisb@us.ibm.com> 19d9fbd03dSHollis Blanchard * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com> 20d30f6e48SScott Wood * Scott Wood <scottwood@freescale.com> 21d30f6e48SScott Wood * Varun Sethi <varun.sethi@freescale.com> 22d9fbd03dSHollis Blanchard */ 23d9fbd03dSHollis Blanchard 24d9fbd03dSHollis Blanchard #include <linux/errno.h> 25d9fbd03dSHollis Blanchard #include <linux/err.h> 26d9fbd03dSHollis Blanchard #include <linux/kvm_host.h> 275a0e3ad6STejun Heo #include <linux/gfp.h> 28d9fbd03dSHollis Blanchard #include <linux/module.h> 29d9fbd03dSHollis Blanchard #include <linux/vmalloc.h> 30d9fbd03dSHollis Blanchard #include <linux/fs.h> 317924bd41SHollis Blanchard 32d9fbd03dSHollis Blanchard #include <asm/cputable.h> 33d9fbd03dSHollis Blanchard #include <asm/uaccess.h> 34d9fbd03dSHollis Blanchard #include <asm/kvm_ppc.h> 35d9fbd03dSHollis Blanchard #include <asm/cacheflush.h> 36d30f6e48SScott Wood #include <asm/dbell.h> 37d30f6e48SScott Wood #include <asm/hw_irq.h> 38d30f6e48SScott Wood #include <asm/irq.h> 39b50df19cSMihai Caraman #include <asm/time.h> 40d9fbd03dSHollis Blanchard 41d30f6e48SScott Wood #include "timing.h" 4275f74f0dSHollis Blanchard #include "booke.h" 4397c95059SAlexander Graf #include "trace.h" 44d9fbd03dSHollis Blanchard 45d9fbd03dSHollis Blanchard unsigned long kvmppc_booke_handlers; 46d9fbd03dSHollis Blanchard 47d9fbd03dSHollis Blanchard #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM 48d9fbd03dSHollis Blanchard #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU 49d9fbd03dSHollis Blanchard 50d9fbd03dSHollis Blanchard struct kvm_stats_debugfs_item debugfs_entries[] = { 51d9fbd03dSHollis Blanchard { "mmio", VCPU_STAT(mmio_exits) }, 52d9fbd03dSHollis Blanchard { "dcr", VCPU_STAT(dcr_exits) }, 53d9fbd03dSHollis Blanchard { "sig", VCPU_STAT(signal_exits) }, 54d9fbd03dSHollis Blanchard { "itlb_r", VCPU_STAT(itlb_real_miss_exits) }, 55d9fbd03dSHollis Blanchard { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) }, 56d9fbd03dSHollis Blanchard { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) }, 57d9fbd03dSHollis Blanchard { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) }, 58d9fbd03dSHollis Blanchard { "sysc", VCPU_STAT(syscall_exits) }, 59d9fbd03dSHollis Blanchard { "isi", VCPU_STAT(isi_exits) }, 60d9fbd03dSHollis Blanchard { "dsi", VCPU_STAT(dsi_exits) }, 61d9fbd03dSHollis Blanchard { "inst_emu", VCPU_STAT(emulated_inst_exits) }, 62d9fbd03dSHollis Blanchard { "dec", VCPU_STAT(dec_exits) }, 63d9fbd03dSHollis Blanchard { "ext_intr", VCPU_STAT(ext_intr_exits) }, 64d9fbd03dSHollis Blanchard { "halt_wakeup", VCPU_STAT(halt_wakeup) }, 65d30f6e48SScott Wood { "doorbell", VCPU_STAT(dbell_exits) }, 66d30f6e48SScott Wood { "guest doorbell", VCPU_STAT(gdbell_exits) }, 67cf1c5ca4SAlexander Graf { "remote_tlb_flush", VM_STAT(remote_tlb_flush) }, 68d9fbd03dSHollis Blanchard { NULL } 69d9fbd03dSHollis Blanchard }; 70d9fbd03dSHollis Blanchard 71d9fbd03dSHollis Blanchard /* TODO: use vcpu_printf() */ 72d9fbd03dSHollis Blanchard void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu) 73d9fbd03dSHollis Blanchard { 74d9fbd03dSHollis Blanchard int i; 75d9fbd03dSHollis Blanchard 76666e7252SAlexander Graf printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr); 775cf8ca22SHollis Blanchard printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr); 78de7906c3SAlexander Graf printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0, 79de7906c3SAlexander Graf vcpu->arch.shared->srr1); 80d9fbd03dSHollis Blanchard 81d9fbd03dSHollis Blanchard printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions); 82d9fbd03dSHollis Blanchard 83d9fbd03dSHollis Blanchard for (i = 0; i < 32; i += 4) { 845cf8ca22SHollis Blanchard printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i, 858e5b26b5SAlexander Graf kvmppc_get_gpr(vcpu, i), 868e5b26b5SAlexander Graf kvmppc_get_gpr(vcpu, i+1), 878e5b26b5SAlexander Graf kvmppc_get_gpr(vcpu, i+2), 888e5b26b5SAlexander Graf kvmppc_get_gpr(vcpu, i+3)); 89d9fbd03dSHollis Blanchard } 90d9fbd03dSHollis Blanchard } 91d9fbd03dSHollis Blanchard 924cd35f67SScott Wood #ifdef CONFIG_SPE 934cd35f67SScott Wood void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu) 944cd35f67SScott Wood { 954cd35f67SScott Wood preempt_disable(); 964cd35f67SScott Wood enable_kernel_spe(); 974cd35f67SScott Wood kvmppc_save_guest_spe(vcpu); 984cd35f67SScott Wood vcpu->arch.shadow_msr &= ~MSR_SPE; 994cd35f67SScott Wood preempt_enable(); 1004cd35f67SScott Wood } 1014cd35f67SScott Wood 1024cd35f67SScott Wood static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu) 1034cd35f67SScott Wood { 1044cd35f67SScott Wood preempt_disable(); 1054cd35f67SScott Wood enable_kernel_spe(); 1064cd35f67SScott Wood kvmppc_load_guest_spe(vcpu); 1074cd35f67SScott Wood vcpu->arch.shadow_msr |= MSR_SPE; 1084cd35f67SScott Wood preempt_enable(); 1094cd35f67SScott Wood } 1104cd35f67SScott Wood 1114cd35f67SScott Wood static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu) 1124cd35f67SScott Wood { 1134cd35f67SScott Wood if (vcpu->arch.shared->msr & MSR_SPE) { 1144cd35f67SScott Wood if (!(vcpu->arch.shadow_msr & MSR_SPE)) 1154cd35f67SScott Wood kvmppc_vcpu_enable_spe(vcpu); 1164cd35f67SScott Wood } else if (vcpu->arch.shadow_msr & MSR_SPE) { 1174cd35f67SScott Wood kvmppc_vcpu_disable_spe(vcpu); 1184cd35f67SScott Wood } 1194cd35f67SScott Wood } 1204cd35f67SScott Wood #else 1214cd35f67SScott Wood static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu) 1224cd35f67SScott Wood { 1234cd35f67SScott Wood } 1244cd35f67SScott Wood #endif 1254cd35f67SScott Wood 1267a08c274SAlexander Graf static void kvmppc_vcpu_sync_fpu(struct kvm_vcpu *vcpu) 1277a08c274SAlexander Graf { 1287a08c274SAlexander Graf #if defined(CONFIG_PPC_FPU) && !defined(CONFIG_KVM_BOOKE_HV) 1297a08c274SAlexander Graf /* We always treat the FP bit as enabled from the host 1307a08c274SAlexander Graf perspective, so only need to adjust the shadow MSR */ 1317a08c274SAlexander Graf vcpu->arch.shadow_msr &= ~MSR_FP; 1327a08c274SAlexander Graf vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_FP; 1337a08c274SAlexander Graf #endif 1347a08c274SAlexander Graf } 1357a08c274SAlexander Graf 136dd9ebf1fSLiu Yu /* 137dd9ebf1fSLiu Yu * Helper function for "full" MSR writes. No need to call this if only 138dd9ebf1fSLiu Yu * EE/CE/ME/DE/RI are changing. 139dd9ebf1fSLiu Yu */ 1404cd35f67SScott Wood void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr) 1414cd35f67SScott Wood { 142dd9ebf1fSLiu Yu u32 old_msr = vcpu->arch.shared->msr; 1434cd35f67SScott Wood 144d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV 145d30f6e48SScott Wood new_msr |= MSR_GS; 146d30f6e48SScott Wood #endif 147d30f6e48SScott Wood 1484cd35f67SScott Wood vcpu->arch.shared->msr = new_msr; 1494cd35f67SScott Wood 150dd9ebf1fSLiu Yu kvmppc_mmu_msr_notify(vcpu, old_msr); 1514cd35f67SScott Wood kvmppc_vcpu_sync_spe(vcpu); 1527a08c274SAlexander Graf kvmppc_vcpu_sync_fpu(vcpu); 1534cd35f67SScott Wood } 1544cd35f67SScott Wood 155d4cf3892SHollis Blanchard static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu, 156d4cf3892SHollis Blanchard unsigned int priority) 1579dd921cfSHollis Blanchard { 1586346046cSAlexander Graf trace_kvm_booke_queue_irqprio(vcpu, priority); 1599dd921cfSHollis Blanchard set_bit(priority, &vcpu->arch.pending_exceptions); 1609dd921cfSHollis Blanchard } 1619dd921cfSHollis Blanchard 162daf5e271SLiu Yu static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu, 163daf5e271SLiu Yu ulong dear_flags, ulong esr_flags) 1649dd921cfSHollis Blanchard { 165daf5e271SLiu Yu vcpu->arch.queued_dear = dear_flags; 166daf5e271SLiu Yu vcpu->arch.queued_esr = esr_flags; 167daf5e271SLiu Yu kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS); 168daf5e271SLiu Yu } 169daf5e271SLiu Yu 170daf5e271SLiu Yu static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu, 171daf5e271SLiu Yu ulong dear_flags, ulong esr_flags) 172daf5e271SLiu Yu { 173daf5e271SLiu Yu vcpu->arch.queued_dear = dear_flags; 174daf5e271SLiu Yu vcpu->arch.queued_esr = esr_flags; 175daf5e271SLiu Yu kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE); 176daf5e271SLiu Yu } 177daf5e271SLiu Yu 178daf5e271SLiu Yu static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu, 179daf5e271SLiu Yu ulong esr_flags) 180daf5e271SLiu Yu { 181daf5e271SLiu Yu vcpu->arch.queued_esr = esr_flags; 182daf5e271SLiu Yu kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE); 183daf5e271SLiu Yu } 184daf5e271SLiu Yu 185daf5e271SLiu Yu void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags) 186daf5e271SLiu Yu { 187daf5e271SLiu Yu vcpu->arch.queued_esr = esr_flags; 188d4cf3892SHollis Blanchard kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM); 1899dd921cfSHollis Blanchard } 1909dd921cfSHollis Blanchard 1919dd921cfSHollis Blanchard void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu) 1929dd921cfSHollis Blanchard { 193d4cf3892SHollis Blanchard kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER); 1949dd921cfSHollis Blanchard } 1959dd921cfSHollis Blanchard 1969dd921cfSHollis Blanchard int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu) 1979dd921cfSHollis Blanchard { 198d4cf3892SHollis Blanchard return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions); 1999dd921cfSHollis Blanchard } 2009dd921cfSHollis Blanchard 2017706664dSAlexander Graf void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu) 2027706664dSAlexander Graf { 2037706664dSAlexander Graf clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions); 2047706664dSAlexander Graf } 2057706664dSAlexander Graf 2069dd921cfSHollis Blanchard void kvmppc_core_queue_external(struct kvm_vcpu *vcpu, 2079dd921cfSHollis Blanchard struct kvm_interrupt *irq) 2089dd921cfSHollis Blanchard { 209c5335f17SAlexander Graf unsigned int prio = BOOKE_IRQPRIO_EXTERNAL; 210c5335f17SAlexander Graf 211c5335f17SAlexander Graf if (irq->irq == KVM_INTERRUPT_SET_LEVEL) 212c5335f17SAlexander Graf prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL; 213c5335f17SAlexander Graf 214c5335f17SAlexander Graf kvmppc_booke_queue_irqprio(vcpu, prio); 2159dd921cfSHollis Blanchard } 2169dd921cfSHollis Blanchard 2174496f974SAlexander Graf void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu, 2184496f974SAlexander Graf struct kvm_interrupt *irq) 2194496f974SAlexander Graf { 2204496f974SAlexander Graf clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions); 221c5335f17SAlexander Graf clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions); 2224496f974SAlexander Graf } 2234496f974SAlexander Graf 224f61c94bbSBharat Bhushan static void kvmppc_core_queue_watchdog(struct kvm_vcpu *vcpu) 225f61c94bbSBharat Bhushan { 226f61c94bbSBharat Bhushan kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_WATCHDOG); 227f61c94bbSBharat Bhushan } 228f61c94bbSBharat Bhushan 229f61c94bbSBharat Bhushan static void kvmppc_core_dequeue_watchdog(struct kvm_vcpu *vcpu) 230f61c94bbSBharat Bhushan { 231f61c94bbSBharat Bhushan clear_bit(BOOKE_IRQPRIO_WATCHDOG, &vcpu->arch.pending_exceptions); 232f61c94bbSBharat Bhushan } 233f61c94bbSBharat Bhushan 234d30f6e48SScott Wood static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1) 235d30f6e48SScott Wood { 236d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV 237d30f6e48SScott Wood mtspr(SPRN_GSRR0, srr0); 238d30f6e48SScott Wood mtspr(SPRN_GSRR1, srr1); 239d30f6e48SScott Wood #else 240d30f6e48SScott Wood vcpu->arch.shared->srr0 = srr0; 241d30f6e48SScott Wood vcpu->arch.shared->srr1 = srr1; 242d30f6e48SScott Wood #endif 243d30f6e48SScott Wood } 244d30f6e48SScott Wood 245d30f6e48SScott Wood static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1) 246d30f6e48SScott Wood { 247d30f6e48SScott Wood vcpu->arch.csrr0 = srr0; 248d30f6e48SScott Wood vcpu->arch.csrr1 = srr1; 249d30f6e48SScott Wood } 250d30f6e48SScott Wood 251d30f6e48SScott Wood static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1) 252d30f6e48SScott Wood { 253d30f6e48SScott Wood if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) { 254d30f6e48SScott Wood vcpu->arch.dsrr0 = srr0; 255d30f6e48SScott Wood vcpu->arch.dsrr1 = srr1; 256d30f6e48SScott Wood } else { 257d30f6e48SScott Wood set_guest_csrr(vcpu, srr0, srr1); 258d30f6e48SScott Wood } 259d30f6e48SScott Wood } 260d30f6e48SScott Wood 261d30f6e48SScott Wood static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1) 262d30f6e48SScott Wood { 263d30f6e48SScott Wood vcpu->arch.mcsrr0 = srr0; 264d30f6e48SScott Wood vcpu->arch.mcsrr1 = srr1; 265d30f6e48SScott Wood } 266d30f6e48SScott Wood 267d30f6e48SScott Wood static unsigned long get_guest_dear(struct kvm_vcpu *vcpu) 268d30f6e48SScott Wood { 269d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV 270d30f6e48SScott Wood return mfspr(SPRN_GDEAR); 271d30f6e48SScott Wood #else 272d30f6e48SScott Wood return vcpu->arch.shared->dar; 273d30f6e48SScott Wood #endif 274d30f6e48SScott Wood } 275d30f6e48SScott Wood 276d30f6e48SScott Wood static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear) 277d30f6e48SScott Wood { 278d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV 279d30f6e48SScott Wood mtspr(SPRN_GDEAR, dear); 280d30f6e48SScott Wood #else 281d30f6e48SScott Wood vcpu->arch.shared->dar = dear; 282d30f6e48SScott Wood #endif 283d30f6e48SScott Wood } 284d30f6e48SScott Wood 285d30f6e48SScott Wood static unsigned long get_guest_esr(struct kvm_vcpu *vcpu) 286d30f6e48SScott Wood { 287d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV 288d30f6e48SScott Wood return mfspr(SPRN_GESR); 289d30f6e48SScott Wood #else 290d30f6e48SScott Wood return vcpu->arch.shared->esr; 291d30f6e48SScott Wood #endif 292d30f6e48SScott Wood } 293d30f6e48SScott Wood 294d30f6e48SScott Wood static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr) 295d30f6e48SScott Wood { 296d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV 297d30f6e48SScott Wood mtspr(SPRN_GESR, esr); 298d30f6e48SScott Wood #else 299d30f6e48SScott Wood vcpu->arch.shared->esr = esr; 300d30f6e48SScott Wood #endif 301d30f6e48SScott Wood } 302d30f6e48SScott Wood 303d4cf3892SHollis Blanchard /* Deliver the interrupt of the corresponding priority, if possible. */ 304d4cf3892SHollis Blanchard static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu, 305d4cf3892SHollis Blanchard unsigned int priority) 306d9fbd03dSHollis Blanchard { 307d4cf3892SHollis Blanchard int allowed = 0; 30879300f8cSAlexander Graf ulong msr_mask = 0; 309*1c810636SAlexander Graf bool update_esr = false, update_dear = false, update_epr = false; 3105c6cedf4SAlexander Graf ulong crit_raw = vcpu->arch.shared->critical; 3115c6cedf4SAlexander Graf ulong crit_r1 = kvmppc_get_gpr(vcpu, 1); 3125c6cedf4SAlexander Graf bool crit; 313c5335f17SAlexander Graf bool keep_irq = false; 314d30f6e48SScott Wood enum int_class int_class; 31595e90b43SMihai Caraman ulong new_msr = vcpu->arch.shared->msr; 3165c6cedf4SAlexander Graf 3175c6cedf4SAlexander Graf /* Truncate crit indicators in 32 bit mode */ 3185c6cedf4SAlexander Graf if (!(vcpu->arch.shared->msr & MSR_SF)) { 3195c6cedf4SAlexander Graf crit_raw &= 0xffffffff; 3205c6cedf4SAlexander Graf crit_r1 &= 0xffffffff; 3215c6cedf4SAlexander Graf } 3225c6cedf4SAlexander Graf 3235c6cedf4SAlexander Graf /* Critical section when crit == r1 */ 3245c6cedf4SAlexander Graf crit = (crit_raw == crit_r1); 3255c6cedf4SAlexander Graf /* ... and we're in supervisor mode */ 3265c6cedf4SAlexander Graf crit = crit && !(vcpu->arch.shared->msr & MSR_PR); 327d9fbd03dSHollis Blanchard 328c5335f17SAlexander Graf if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) { 329c5335f17SAlexander Graf priority = BOOKE_IRQPRIO_EXTERNAL; 330c5335f17SAlexander Graf keep_irq = true; 331c5335f17SAlexander Graf } 332c5335f17SAlexander Graf 333*1c810636SAlexander Graf if ((priority == BOOKE_IRQPRIO_EXTERNAL) && vcpu->arch.epr_enabled) 334*1c810636SAlexander Graf update_epr = true; 335*1c810636SAlexander Graf 336d4cf3892SHollis Blanchard switch (priority) { 337d4cf3892SHollis Blanchard case BOOKE_IRQPRIO_DTLB_MISS: 338daf5e271SLiu Yu case BOOKE_IRQPRIO_DATA_STORAGE: 339daf5e271SLiu Yu update_dear = true; 340daf5e271SLiu Yu /* fall through */ 341daf5e271SLiu Yu case BOOKE_IRQPRIO_INST_STORAGE: 342daf5e271SLiu Yu case BOOKE_IRQPRIO_PROGRAM: 343daf5e271SLiu Yu update_esr = true; 344daf5e271SLiu Yu /* fall through */ 345d4cf3892SHollis Blanchard case BOOKE_IRQPRIO_ITLB_MISS: 346d4cf3892SHollis Blanchard case BOOKE_IRQPRIO_SYSCALL: 347d4cf3892SHollis Blanchard case BOOKE_IRQPRIO_FP_UNAVAIL: 348bb3a8a17SHollis Blanchard case BOOKE_IRQPRIO_SPE_UNAVAIL: 349bb3a8a17SHollis Blanchard case BOOKE_IRQPRIO_SPE_FP_DATA: 350bb3a8a17SHollis Blanchard case BOOKE_IRQPRIO_SPE_FP_ROUND: 351d4cf3892SHollis Blanchard case BOOKE_IRQPRIO_AP_UNAVAIL: 352d4cf3892SHollis Blanchard case BOOKE_IRQPRIO_ALIGNMENT: 353d4cf3892SHollis Blanchard allowed = 1; 35479300f8cSAlexander Graf msr_mask = MSR_CE | MSR_ME | MSR_DE; 355d30f6e48SScott Wood int_class = INT_CLASS_NONCRIT; 356d9fbd03dSHollis Blanchard break; 357f61c94bbSBharat Bhushan case BOOKE_IRQPRIO_WATCHDOG: 358d4cf3892SHollis Blanchard case BOOKE_IRQPRIO_CRITICAL: 3594ab96919SAlexander Graf case BOOKE_IRQPRIO_DBELL_CRIT: 360666e7252SAlexander Graf allowed = vcpu->arch.shared->msr & MSR_CE; 361d30f6e48SScott Wood allowed = allowed && !crit; 36279300f8cSAlexander Graf msr_mask = MSR_ME; 363d30f6e48SScott Wood int_class = INT_CLASS_CRIT; 364d9fbd03dSHollis Blanchard break; 365d4cf3892SHollis Blanchard case BOOKE_IRQPRIO_MACHINE_CHECK: 366666e7252SAlexander Graf allowed = vcpu->arch.shared->msr & MSR_ME; 367d30f6e48SScott Wood allowed = allowed && !crit; 368d30f6e48SScott Wood int_class = INT_CLASS_MC; 369d9fbd03dSHollis Blanchard break; 370d4cf3892SHollis Blanchard case BOOKE_IRQPRIO_DECREMENTER: 371d4cf3892SHollis Blanchard case BOOKE_IRQPRIO_FIT: 372dfd4d47eSScott Wood keep_irq = true; 373dfd4d47eSScott Wood /* fall through */ 374dfd4d47eSScott Wood case BOOKE_IRQPRIO_EXTERNAL: 3754ab96919SAlexander Graf case BOOKE_IRQPRIO_DBELL: 376666e7252SAlexander Graf allowed = vcpu->arch.shared->msr & MSR_EE; 3775c6cedf4SAlexander Graf allowed = allowed && !crit; 37879300f8cSAlexander Graf msr_mask = MSR_CE | MSR_ME | MSR_DE; 379d30f6e48SScott Wood int_class = INT_CLASS_NONCRIT; 380d9fbd03dSHollis Blanchard break; 381d4cf3892SHollis Blanchard case BOOKE_IRQPRIO_DEBUG: 382666e7252SAlexander Graf allowed = vcpu->arch.shared->msr & MSR_DE; 383d30f6e48SScott Wood allowed = allowed && !crit; 38479300f8cSAlexander Graf msr_mask = MSR_ME; 385d30f6e48SScott Wood int_class = INT_CLASS_CRIT; 386d9fbd03dSHollis Blanchard break; 387d9fbd03dSHollis Blanchard } 388d9fbd03dSHollis Blanchard 389d4cf3892SHollis Blanchard if (allowed) { 390d30f6e48SScott Wood switch (int_class) { 391d30f6e48SScott Wood case INT_CLASS_NONCRIT: 392d30f6e48SScott Wood set_guest_srr(vcpu, vcpu->arch.pc, 393d30f6e48SScott Wood vcpu->arch.shared->msr); 394d30f6e48SScott Wood break; 395d30f6e48SScott Wood case INT_CLASS_CRIT: 396d30f6e48SScott Wood set_guest_csrr(vcpu, vcpu->arch.pc, 397d30f6e48SScott Wood vcpu->arch.shared->msr); 398d30f6e48SScott Wood break; 399d30f6e48SScott Wood case INT_CLASS_DBG: 400d30f6e48SScott Wood set_guest_dsrr(vcpu, vcpu->arch.pc, 401d30f6e48SScott Wood vcpu->arch.shared->msr); 402d30f6e48SScott Wood break; 403d30f6e48SScott Wood case INT_CLASS_MC: 404d30f6e48SScott Wood set_guest_mcsrr(vcpu, vcpu->arch.pc, 405d30f6e48SScott Wood vcpu->arch.shared->msr); 406d30f6e48SScott Wood break; 407d30f6e48SScott Wood } 408d30f6e48SScott Wood 409d4cf3892SHollis Blanchard vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority]; 410daf5e271SLiu Yu if (update_esr == true) 411d30f6e48SScott Wood set_guest_esr(vcpu, vcpu->arch.queued_esr); 412daf5e271SLiu Yu if (update_dear == true) 413d30f6e48SScott Wood set_guest_dear(vcpu, vcpu->arch.queued_dear); 414*1c810636SAlexander Graf if (update_epr == true) 415*1c810636SAlexander Graf kvm_make_request(KVM_REQ_EPR_EXIT, vcpu); 41695e90b43SMihai Caraman 41795e90b43SMihai Caraman new_msr &= msr_mask; 41895e90b43SMihai Caraman #if defined(CONFIG_64BIT) 41995e90b43SMihai Caraman if (vcpu->arch.epcr & SPRN_EPCR_ICM) 42095e90b43SMihai Caraman new_msr |= MSR_CM; 42195e90b43SMihai Caraman #endif 42295e90b43SMihai Caraman kvmppc_set_msr(vcpu, new_msr); 423d4cf3892SHollis Blanchard 424c5335f17SAlexander Graf if (!keep_irq) 425d4cf3892SHollis Blanchard clear_bit(priority, &vcpu->arch.pending_exceptions); 426d4cf3892SHollis Blanchard } 427d4cf3892SHollis Blanchard 428d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV 429d30f6e48SScott Wood /* 430d30f6e48SScott Wood * If an interrupt is pending but masked, raise a guest doorbell 431d30f6e48SScott Wood * so that we are notified when the guest enables the relevant 432d30f6e48SScott Wood * MSR bit. 433d30f6e48SScott Wood */ 434d30f6e48SScott Wood if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE) 435d30f6e48SScott Wood kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT); 436d30f6e48SScott Wood if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE) 437d30f6e48SScott Wood kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT); 438d30f6e48SScott Wood if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK) 439d30f6e48SScott Wood kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC); 440d30f6e48SScott Wood #endif 441d30f6e48SScott Wood 442d4cf3892SHollis Blanchard return allowed; 443d9fbd03dSHollis Blanchard } 444d9fbd03dSHollis Blanchard 445f61c94bbSBharat Bhushan /* 446f61c94bbSBharat Bhushan * Return the number of jiffies until the next timeout. If the timeout is 447f61c94bbSBharat Bhushan * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA 448f61c94bbSBharat Bhushan * because the larger value can break the timer APIs. 449f61c94bbSBharat Bhushan */ 450f61c94bbSBharat Bhushan static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu) 451f61c94bbSBharat Bhushan { 452f61c94bbSBharat Bhushan u64 tb, wdt_tb, wdt_ticks = 0; 453f61c94bbSBharat Bhushan u64 nr_jiffies = 0; 454f61c94bbSBharat Bhushan u32 period = TCR_GET_WP(vcpu->arch.tcr); 455f61c94bbSBharat Bhushan 456f61c94bbSBharat Bhushan wdt_tb = 1ULL << (63 - period); 457f61c94bbSBharat Bhushan tb = get_tb(); 458f61c94bbSBharat Bhushan /* 459f61c94bbSBharat Bhushan * The watchdog timeout will hapeen when TB bit corresponding 460f61c94bbSBharat Bhushan * to watchdog will toggle from 0 to 1. 461f61c94bbSBharat Bhushan */ 462f61c94bbSBharat Bhushan if (tb & wdt_tb) 463f61c94bbSBharat Bhushan wdt_ticks = wdt_tb; 464f61c94bbSBharat Bhushan 465f61c94bbSBharat Bhushan wdt_ticks += wdt_tb - (tb & (wdt_tb - 1)); 466f61c94bbSBharat Bhushan 467f61c94bbSBharat Bhushan /* Convert timebase ticks to jiffies */ 468f61c94bbSBharat Bhushan nr_jiffies = wdt_ticks; 469f61c94bbSBharat Bhushan 470f61c94bbSBharat Bhushan if (do_div(nr_jiffies, tb_ticks_per_jiffy)) 471f61c94bbSBharat Bhushan nr_jiffies++; 472f61c94bbSBharat Bhushan 473f61c94bbSBharat Bhushan return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA); 474f61c94bbSBharat Bhushan } 475f61c94bbSBharat Bhushan 476f61c94bbSBharat Bhushan static void arm_next_watchdog(struct kvm_vcpu *vcpu) 477f61c94bbSBharat Bhushan { 478f61c94bbSBharat Bhushan unsigned long nr_jiffies; 479f61c94bbSBharat Bhushan unsigned long flags; 480f61c94bbSBharat Bhushan 481f61c94bbSBharat Bhushan /* 482f61c94bbSBharat Bhushan * If TSR_ENW and TSR_WIS are not set then no need to exit to 483f61c94bbSBharat Bhushan * userspace, so clear the KVM_REQ_WATCHDOG request. 484f61c94bbSBharat Bhushan */ 485f61c94bbSBharat Bhushan if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS)) 486f61c94bbSBharat Bhushan clear_bit(KVM_REQ_WATCHDOG, &vcpu->requests); 487f61c94bbSBharat Bhushan 488f61c94bbSBharat Bhushan spin_lock_irqsave(&vcpu->arch.wdt_lock, flags); 489f61c94bbSBharat Bhushan nr_jiffies = watchdog_next_timeout(vcpu); 490f61c94bbSBharat Bhushan /* 491f61c94bbSBharat Bhushan * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA 492f61c94bbSBharat Bhushan * then do not run the watchdog timer as this can break timer APIs. 493f61c94bbSBharat Bhushan */ 494f61c94bbSBharat Bhushan if (nr_jiffies < NEXT_TIMER_MAX_DELTA) 495f61c94bbSBharat Bhushan mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies); 496f61c94bbSBharat Bhushan else 497f61c94bbSBharat Bhushan del_timer(&vcpu->arch.wdt_timer); 498f61c94bbSBharat Bhushan spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags); 499f61c94bbSBharat Bhushan } 500f61c94bbSBharat Bhushan 501f61c94bbSBharat Bhushan void kvmppc_watchdog_func(unsigned long data) 502f61c94bbSBharat Bhushan { 503f61c94bbSBharat Bhushan struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; 504f61c94bbSBharat Bhushan u32 tsr, new_tsr; 505f61c94bbSBharat Bhushan int final; 506f61c94bbSBharat Bhushan 507f61c94bbSBharat Bhushan do { 508f61c94bbSBharat Bhushan new_tsr = tsr = vcpu->arch.tsr; 509f61c94bbSBharat Bhushan final = 0; 510f61c94bbSBharat Bhushan 511f61c94bbSBharat Bhushan /* Time out event */ 512f61c94bbSBharat Bhushan if (tsr & TSR_ENW) { 513f61c94bbSBharat Bhushan if (tsr & TSR_WIS) 514f61c94bbSBharat Bhushan final = 1; 515f61c94bbSBharat Bhushan else 516f61c94bbSBharat Bhushan new_tsr = tsr | TSR_WIS; 517f61c94bbSBharat Bhushan } else { 518f61c94bbSBharat Bhushan new_tsr = tsr | TSR_ENW; 519f61c94bbSBharat Bhushan } 520f61c94bbSBharat Bhushan } while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr); 521f61c94bbSBharat Bhushan 522f61c94bbSBharat Bhushan if (new_tsr & TSR_WIS) { 523f61c94bbSBharat Bhushan smp_wmb(); 524f61c94bbSBharat Bhushan kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu); 525f61c94bbSBharat Bhushan kvm_vcpu_kick(vcpu); 526f61c94bbSBharat Bhushan } 527f61c94bbSBharat Bhushan 528f61c94bbSBharat Bhushan /* 529f61c94bbSBharat Bhushan * If this is final watchdog expiry and some action is required 530f61c94bbSBharat Bhushan * then exit to userspace. 531f61c94bbSBharat Bhushan */ 532f61c94bbSBharat Bhushan if (final && (vcpu->arch.tcr & TCR_WRC_MASK) && 533f61c94bbSBharat Bhushan vcpu->arch.watchdog_enabled) { 534f61c94bbSBharat Bhushan smp_wmb(); 535f61c94bbSBharat Bhushan kvm_make_request(KVM_REQ_WATCHDOG, vcpu); 536f61c94bbSBharat Bhushan kvm_vcpu_kick(vcpu); 537f61c94bbSBharat Bhushan } 538f61c94bbSBharat Bhushan 539f61c94bbSBharat Bhushan /* 540f61c94bbSBharat Bhushan * Stop running the watchdog timer after final expiration to 541f61c94bbSBharat Bhushan * prevent the host from being flooded with timers if the 542f61c94bbSBharat Bhushan * guest sets a short period. 543f61c94bbSBharat Bhushan * Timers will resume when TSR/TCR is updated next time. 544f61c94bbSBharat Bhushan */ 545f61c94bbSBharat Bhushan if (!final) 546f61c94bbSBharat Bhushan arm_next_watchdog(vcpu); 547f61c94bbSBharat Bhushan } 548f61c94bbSBharat Bhushan 549dfd4d47eSScott Wood static void update_timer_ints(struct kvm_vcpu *vcpu) 550dfd4d47eSScott Wood { 551dfd4d47eSScott Wood if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS)) 552dfd4d47eSScott Wood kvmppc_core_queue_dec(vcpu); 553dfd4d47eSScott Wood else 554dfd4d47eSScott Wood kvmppc_core_dequeue_dec(vcpu); 555f61c94bbSBharat Bhushan 556f61c94bbSBharat Bhushan if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS)) 557f61c94bbSBharat Bhushan kvmppc_core_queue_watchdog(vcpu); 558f61c94bbSBharat Bhushan else 559f61c94bbSBharat Bhushan kvmppc_core_dequeue_watchdog(vcpu); 560dfd4d47eSScott Wood } 561dfd4d47eSScott Wood 562c59a6a3eSScott Wood static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu) 563d9fbd03dSHollis Blanchard { 564d9fbd03dSHollis Blanchard unsigned long *pending = &vcpu->arch.pending_exceptions; 565d9fbd03dSHollis Blanchard unsigned int priority; 566d9fbd03dSHollis Blanchard 5679ab80843SHollis Blanchard priority = __ffs(*pending); 5688b3a00fcSAlexander Graf while (priority < BOOKE_IRQPRIO_MAX) { 569d4cf3892SHollis Blanchard if (kvmppc_booke_irqprio_deliver(vcpu, priority)) 570d9fbd03dSHollis Blanchard break; 571d9fbd03dSHollis Blanchard 572d9fbd03dSHollis Blanchard priority = find_next_bit(pending, 573d9fbd03dSHollis Blanchard BITS_PER_BYTE * sizeof(*pending), 574d9fbd03dSHollis Blanchard priority + 1); 575d9fbd03dSHollis Blanchard } 57690bba358SAlexander Graf 57790bba358SAlexander Graf /* Tell the guest about our interrupt status */ 57829ac26efSScott Wood vcpu->arch.shared->int_pending = !!*pending; 579d9fbd03dSHollis Blanchard } 580d9fbd03dSHollis Blanchard 581c59a6a3eSScott Wood /* Check pending exceptions and deliver one, if possible. */ 582a8e4ef84SAlexander Graf int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu) 583c59a6a3eSScott Wood { 584a8e4ef84SAlexander Graf int r = 0; 585c59a6a3eSScott Wood WARN_ON_ONCE(!irqs_disabled()); 586c59a6a3eSScott Wood 587c59a6a3eSScott Wood kvmppc_core_check_exceptions(vcpu); 588c59a6a3eSScott Wood 589b8c649a9SAlexander Graf if (vcpu->requests) { 590b8c649a9SAlexander Graf /* Exception delivery raised request; start over */ 591b8c649a9SAlexander Graf return 1; 592b8c649a9SAlexander Graf } 593b8c649a9SAlexander Graf 594c59a6a3eSScott Wood if (vcpu->arch.shared->msr & MSR_WE) { 595c59a6a3eSScott Wood local_irq_enable(); 596c59a6a3eSScott Wood kvm_vcpu_block(vcpu); 597966cd0f3SAlexander Graf clear_bit(KVM_REQ_UNHALT, &vcpu->requests); 598c59a6a3eSScott Wood local_irq_disable(); 599c59a6a3eSScott Wood 600c59a6a3eSScott Wood kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS); 601a8e4ef84SAlexander Graf r = 1; 602c59a6a3eSScott Wood }; 603a8e4ef84SAlexander Graf 604a8e4ef84SAlexander Graf return r; 605a8e4ef84SAlexander Graf } 606a8e4ef84SAlexander Graf 6077c973a2eSAlexander Graf int kvmppc_core_check_requests(struct kvm_vcpu *vcpu) 6084ffc6356SAlexander Graf { 6097c973a2eSAlexander Graf int r = 1; /* Indicate we want to get back into the guest */ 6107c973a2eSAlexander Graf 6114ffc6356SAlexander Graf if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu)) 6124ffc6356SAlexander Graf update_timer_ints(vcpu); 613862d31f7SAlexander Graf #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) 614862d31f7SAlexander Graf if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) 615862d31f7SAlexander Graf kvmppc_core_flush_tlb(vcpu); 616862d31f7SAlexander Graf #endif 6177c973a2eSAlexander Graf 618f61c94bbSBharat Bhushan if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) { 619f61c94bbSBharat Bhushan vcpu->run->exit_reason = KVM_EXIT_WATCHDOG; 620f61c94bbSBharat Bhushan r = 0; 621f61c94bbSBharat Bhushan } 622f61c94bbSBharat Bhushan 623*1c810636SAlexander Graf if (kvm_check_request(KVM_REQ_EPR_EXIT, vcpu)) { 624*1c810636SAlexander Graf vcpu->run->epr.epr = 0; 625*1c810636SAlexander Graf vcpu->arch.epr_needed = true; 626*1c810636SAlexander Graf vcpu->run->exit_reason = KVM_EXIT_EPR; 627*1c810636SAlexander Graf r = 0; 628*1c810636SAlexander Graf } 629*1c810636SAlexander Graf 6307c973a2eSAlexander Graf return r; 6314ffc6356SAlexander Graf } 6324ffc6356SAlexander Graf 633df6909e5SPaul Mackerras int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) 634df6909e5SPaul Mackerras { 6357ee78855SAlexander Graf int ret, s; 6368fae845fSScott Wood #ifdef CONFIG_PPC_FPU 6378fae845fSScott Wood unsigned int fpscr; 6388fae845fSScott Wood int fpexc_mode; 6398fae845fSScott Wood u64 fpr[32]; 6408fae845fSScott Wood #endif 641df6909e5SPaul Mackerras 642af8f38b3SAlexander Graf if (!vcpu->arch.sane) { 643af8f38b3SAlexander Graf kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 644af8f38b3SAlexander Graf return -EINVAL; 645af8f38b3SAlexander Graf } 646af8f38b3SAlexander Graf 647df6909e5SPaul Mackerras local_irq_disable(); 6487ee78855SAlexander Graf s = kvmppc_prepare_to_enter(vcpu); 6497ee78855SAlexander Graf if (s <= 0) { 65024afa37bSAlexander Graf local_irq_enable(); 6517ee78855SAlexander Graf ret = s; 6521d1ef222SScott Wood goto out; 6531d1ef222SScott Wood } 654bd2be683SAlexander Graf kvmppc_lazy_ee_enable(); 6551d1ef222SScott Wood 656df6909e5SPaul Mackerras kvm_guest_enter(); 6578fae845fSScott Wood 6588fae845fSScott Wood #ifdef CONFIG_PPC_FPU 6598fae845fSScott Wood /* Save userspace FPU state in stack */ 6608fae845fSScott Wood enable_kernel_fp(); 6618fae845fSScott Wood memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr)); 6628fae845fSScott Wood fpscr = current->thread.fpscr.val; 6638fae845fSScott Wood fpexc_mode = current->thread.fpexc_mode; 6648fae845fSScott Wood 6658fae845fSScott Wood /* Restore guest FPU state to thread */ 6668fae845fSScott Wood memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr)); 6678fae845fSScott Wood current->thread.fpscr.val = vcpu->arch.fpscr; 6688fae845fSScott Wood 6698fae845fSScott Wood /* 6708fae845fSScott Wood * Since we can't trap on MSR_FP in GS-mode, we consider the guest 6718fae845fSScott Wood * as always using the FPU. Kernel usage of FP (via 6728fae845fSScott Wood * enable_kernel_fp()) in this thread must not occur while 6738fae845fSScott Wood * vcpu->fpu_active is set. 6748fae845fSScott Wood */ 6758fae845fSScott Wood vcpu->fpu_active = 1; 6768fae845fSScott Wood 6778fae845fSScott Wood kvmppc_load_guest_fp(vcpu); 6788fae845fSScott Wood #endif 6798fae845fSScott Wood 680df6909e5SPaul Mackerras ret = __kvmppc_vcpu_run(kvm_run, vcpu); 6818fae845fSScott Wood 68224afa37bSAlexander Graf /* No need for kvm_guest_exit. It's done in handle_exit. 68324afa37bSAlexander Graf We also get here with interrupts enabled. */ 68424afa37bSAlexander Graf 6858fae845fSScott Wood #ifdef CONFIG_PPC_FPU 6868fae845fSScott Wood kvmppc_save_guest_fp(vcpu); 6878fae845fSScott Wood 6888fae845fSScott Wood vcpu->fpu_active = 0; 6898fae845fSScott Wood 6908fae845fSScott Wood /* Save guest FPU state from thread */ 6918fae845fSScott Wood memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr)); 6928fae845fSScott Wood vcpu->arch.fpscr = current->thread.fpscr.val; 6938fae845fSScott Wood 6948fae845fSScott Wood /* Restore userspace FPU state from stack */ 6958fae845fSScott Wood memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr)); 6968fae845fSScott Wood current->thread.fpscr.val = fpscr; 6978fae845fSScott Wood current->thread.fpexc_mode = fpexc_mode; 6988fae845fSScott Wood #endif 6998fae845fSScott Wood 7001d1ef222SScott Wood out: 701d69c6436SAlexander Graf vcpu->mode = OUTSIDE_GUEST_MODE; 702df6909e5SPaul Mackerras return ret; 703df6909e5SPaul Mackerras } 704df6909e5SPaul Mackerras 705d30f6e48SScott Wood static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu) 706d9fbd03dSHollis Blanchard { 707d9fbd03dSHollis Blanchard enum emulation_result er; 708d9fbd03dSHollis Blanchard 709d9fbd03dSHollis Blanchard er = kvmppc_emulate_instruction(run, vcpu); 710d9fbd03dSHollis Blanchard switch (er) { 711d9fbd03dSHollis Blanchard case EMULATE_DONE: 71273e75b41SHollis Blanchard /* don't overwrite subtypes, just account kvm_stats */ 7137b701591SHollis Blanchard kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS); 714d9fbd03dSHollis Blanchard /* Future optimization: only reload non-volatiles if 715d9fbd03dSHollis Blanchard * they were actually modified by emulation. */ 716d30f6e48SScott Wood return RESUME_GUEST_NV; 717d30f6e48SScott Wood 718d9fbd03dSHollis Blanchard case EMULATE_DO_DCR: 719d9fbd03dSHollis Blanchard run->exit_reason = KVM_EXIT_DCR; 720d30f6e48SScott Wood return RESUME_HOST; 721d30f6e48SScott Wood 722d9fbd03dSHollis Blanchard case EMULATE_FAIL: 7235cf8ca22SHollis Blanchard printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n", 724d9fbd03dSHollis Blanchard __func__, vcpu->arch.pc, vcpu->arch.last_inst); 725d9fbd03dSHollis Blanchard /* For debugging, encode the failing instruction and 726d9fbd03dSHollis Blanchard * report it to userspace. */ 727d9fbd03dSHollis Blanchard run->hw.hardware_exit_reason = ~0ULL << 32; 728d9fbd03dSHollis Blanchard run->hw.hardware_exit_reason |= vcpu->arch.last_inst; 729d1ff5499SAlexander Graf kvmppc_core_queue_program(vcpu, ESR_PIL); 730d30f6e48SScott Wood return RESUME_HOST; 731d30f6e48SScott Wood 732d9fbd03dSHollis Blanchard default: 733d9fbd03dSHollis Blanchard BUG(); 734d9fbd03dSHollis Blanchard } 735d30f6e48SScott Wood } 736d30f6e48SScott Wood 7374e642ccbSAlexander Graf static void kvmppc_fill_pt_regs(struct pt_regs *regs) 7384e642ccbSAlexander Graf { 7394e642ccbSAlexander Graf ulong r1, ip, msr, lr; 7404e642ccbSAlexander Graf 7414e642ccbSAlexander Graf asm("mr %0, 1" : "=r"(r1)); 7424e642ccbSAlexander Graf asm("mflr %0" : "=r"(lr)); 7434e642ccbSAlexander Graf asm("mfmsr %0" : "=r"(msr)); 7444e642ccbSAlexander Graf asm("bl 1f; 1: mflr %0" : "=r"(ip)); 7454e642ccbSAlexander Graf 7464e642ccbSAlexander Graf memset(regs, 0, sizeof(*regs)); 7474e642ccbSAlexander Graf regs->gpr[1] = r1; 7484e642ccbSAlexander Graf regs->nip = ip; 7494e642ccbSAlexander Graf regs->msr = msr; 7504e642ccbSAlexander Graf regs->link = lr; 7514e642ccbSAlexander Graf } 7524e642ccbSAlexander Graf 7536328e593SBharat Bhushan /* 7546328e593SBharat Bhushan * For interrupts needed to be handled by host interrupt handlers, 7556328e593SBharat Bhushan * corresponding host handler are called from here in similar way 7566328e593SBharat Bhushan * (but not exact) as they are called from low level handler 7576328e593SBharat Bhushan * (such as from arch/powerpc/kernel/head_fsl_booke.S). 7586328e593SBharat Bhushan */ 7594e642ccbSAlexander Graf static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu, 7604e642ccbSAlexander Graf unsigned int exit_nr) 7614e642ccbSAlexander Graf { 7624e642ccbSAlexander Graf struct pt_regs regs; 7634e642ccbSAlexander Graf 7644e642ccbSAlexander Graf switch (exit_nr) { 7654e642ccbSAlexander Graf case BOOKE_INTERRUPT_EXTERNAL: 7664e642ccbSAlexander Graf kvmppc_fill_pt_regs(®s); 7674e642ccbSAlexander Graf do_IRQ(®s); 7684e642ccbSAlexander Graf break; 7694e642ccbSAlexander Graf case BOOKE_INTERRUPT_DECREMENTER: 7704e642ccbSAlexander Graf kvmppc_fill_pt_regs(®s); 7714e642ccbSAlexander Graf timer_interrupt(®s); 7724e642ccbSAlexander Graf break; 7734e642ccbSAlexander Graf #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64) 7744e642ccbSAlexander Graf case BOOKE_INTERRUPT_DOORBELL: 7754e642ccbSAlexander Graf kvmppc_fill_pt_regs(®s); 7764e642ccbSAlexander Graf doorbell_exception(®s); 7774e642ccbSAlexander Graf break; 7784e642ccbSAlexander Graf #endif 7794e642ccbSAlexander Graf case BOOKE_INTERRUPT_MACHINE_CHECK: 7804e642ccbSAlexander Graf /* FIXME */ 7814e642ccbSAlexander Graf break; 7827cc1e8eeSAlexander Graf case BOOKE_INTERRUPT_PERFORMANCE_MONITOR: 7837cc1e8eeSAlexander Graf kvmppc_fill_pt_regs(®s); 7847cc1e8eeSAlexander Graf performance_monitor_exception(®s); 7857cc1e8eeSAlexander Graf break; 7866328e593SBharat Bhushan case BOOKE_INTERRUPT_WATCHDOG: 7876328e593SBharat Bhushan kvmppc_fill_pt_regs(®s); 7886328e593SBharat Bhushan #ifdef CONFIG_BOOKE_WDT 7896328e593SBharat Bhushan WatchdogException(®s); 7906328e593SBharat Bhushan #else 7916328e593SBharat Bhushan unknown_exception(®s); 7926328e593SBharat Bhushan #endif 7936328e593SBharat Bhushan break; 7946328e593SBharat Bhushan case BOOKE_INTERRUPT_CRITICAL: 7956328e593SBharat Bhushan unknown_exception(®s); 7966328e593SBharat Bhushan break; 7974e642ccbSAlexander Graf } 7984e642ccbSAlexander Graf } 7994e642ccbSAlexander Graf 800d30f6e48SScott Wood /** 801d30f6e48SScott Wood * kvmppc_handle_exit 802d30f6e48SScott Wood * 803d30f6e48SScott Wood * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV) 804d30f6e48SScott Wood */ 805d30f6e48SScott Wood int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, 806d30f6e48SScott Wood unsigned int exit_nr) 807d30f6e48SScott Wood { 808d30f6e48SScott Wood int r = RESUME_HOST; 8097ee78855SAlexander Graf int s; 810d30f6e48SScott Wood 811d30f6e48SScott Wood /* update before a new last_exit_type is rewritten */ 812d30f6e48SScott Wood kvmppc_update_timing_stats(vcpu); 813d30f6e48SScott Wood 8144e642ccbSAlexander Graf /* restart interrupts if they were meant for the host */ 8154e642ccbSAlexander Graf kvmppc_restart_interrupt(vcpu, exit_nr); 816d30f6e48SScott Wood 817d30f6e48SScott Wood local_irq_enable(); 818d30f6e48SScott Wood 81997c95059SAlexander Graf trace_kvm_exit(exit_nr, vcpu); 820706fb730SAlexander Graf kvm_guest_exit(); 82197c95059SAlexander Graf 822d30f6e48SScott Wood run->exit_reason = KVM_EXIT_UNKNOWN; 823d30f6e48SScott Wood run->ready_for_interrupt_injection = 1; 824d30f6e48SScott Wood 825d30f6e48SScott Wood switch (exit_nr) { 826d30f6e48SScott Wood case BOOKE_INTERRUPT_MACHINE_CHECK: 827c35c9d84SAlexander Graf printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR)); 828c35c9d84SAlexander Graf kvmppc_dump_vcpu(vcpu); 829c35c9d84SAlexander Graf /* For debugging, send invalid exit reason to user space */ 830c35c9d84SAlexander Graf run->hw.hardware_exit_reason = ~1ULL << 32; 831c35c9d84SAlexander Graf run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR); 832c35c9d84SAlexander Graf r = RESUME_HOST; 833d30f6e48SScott Wood break; 834d30f6e48SScott Wood 835d30f6e48SScott Wood case BOOKE_INTERRUPT_EXTERNAL: 836d30f6e48SScott Wood kvmppc_account_exit(vcpu, EXT_INTR_EXITS); 837d30f6e48SScott Wood r = RESUME_GUEST; 838d30f6e48SScott Wood break; 839d30f6e48SScott Wood 840d30f6e48SScott Wood case BOOKE_INTERRUPT_DECREMENTER: 841d30f6e48SScott Wood kvmppc_account_exit(vcpu, DEC_EXITS); 842d30f6e48SScott Wood r = RESUME_GUEST; 843d30f6e48SScott Wood break; 844d30f6e48SScott Wood 8456328e593SBharat Bhushan case BOOKE_INTERRUPT_WATCHDOG: 8466328e593SBharat Bhushan r = RESUME_GUEST; 8476328e593SBharat Bhushan break; 8486328e593SBharat Bhushan 849d30f6e48SScott Wood case BOOKE_INTERRUPT_DOORBELL: 850d30f6e48SScott Wood kvmppc_account_exit(vcpu, DBELL_EXITS); 851d30f6e48SScott Wood r = RESUME_GUEST; 852d30f6e48SScott Wood break; 853d30f6e48SScott Wood 854d30f6e48SScott Wood case BOOKE_INTERRUPT_GUEST_DBELL_CRIT: 855d30f6e48SScott Wood kvmppc_account_exit(vcpu, GDBELL_EXITS); 856d30f6e48SScott Wood 857d30f6e48SScott Wood /* 858d30f6e48SScott Wood * We are here because there is a pending guest interrupt 859d30f6e48SScott Wood * which could not be delivered as MSR_CE or MSR_ME was not 860d30f6e48SScott Wood * set. Once we break from here we will retry delivery. 861d30f6e48SScott Wood */ 862d30f6e48SScott Wood r = RESUME_GUEST; 863d30f6e48SScott Wood break; 864d30f6e48SScott Wood 865d30f6e48SScott Wood case BOOKE_INTERRUPT_GUEST_DBELL: 866d30f6e48SScott Wood kvmppc_account_exit(vcpu, GDBELL_EXITS); 867d30f6e48SScott Wood 868d30f6e48SScott Wood /* 869d30f6e48SScott Wood * We are here because there is a pending guest interrupt 870d30f6e48SScott Wood * which could not be delivered as MSR_EE was not set. Once 871d30f6e48SScott Wood * we break from here we will retry delivery. 872d30f6e48SScott Wood */ 873d30f6e48SScott Wood r = RESUME_GUEST; 874d30f6e48SScott Wood break; 875d30f6e48SScott Wood 87695f2e921SAlexander Graf case BOOKE_INTERRUPT_PERFORMANCE_MONITOR: 87795f2e921SAlexander Graf r = RESUME_GUEST; 87895f2e921SAlexander Graf break; 87995f2e921SAlexander Graf 880d30f6e48SScott Wood case BOOKE_INTERRUPT_HV_PRIV: 881d30f6e48SScott Wood r = emulation_exit(run, vcpu); 882d30f6e48SScott Wood break; 883d30f6e48SScott Wood 884d30f6e48SScott Wood case BOOKE_INTERRUPT_PROGRAM: 885d30f6e48SScott Wood if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) { 8860268597cSAlexander Graf /* 8870268597cSAlexander Graf * Program traps generated by user-level software must 8880268597cSAlexander Graf * be handled by the guest kernel. 8890268597cSAlexander Graf * 8900268597cSAlexander Graf * In GS mode, hypervisor privileged instructions trap 8910268597cSAlexander Graf * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are 8920268597cSAlexander Graf * actual program interrupts, handled by the guest. 8930268597cSAlexander Graf */ 894d30f6e48SScott Wood kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr); 895d30f6e48SScott Wood r = RESUME_GUEST; 896d30f6e48SScott Wood kvmppc_account_exit(vcpu, USR_PR_INST); 897d30f6e48SScott Wood break; 898d30f6e48SScott Wood } 899d30f6e48SScott Wood 900d30f6e48SScott Wood r = emulation_exit(run, vcpu); 901d9fbd03dSHollis Blanchard break; 902d9fbd03dSHollis Blanchard 903d9fbd03dSHollis Blanchard case BOOKE_INTERRUPT_FP_UNAVAIL: 904d4cf3892SHollis Blanchard kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL); 9057b701591SHollis Blanchard kvmppc_account_exit(vcpu, FP_UNAVAIL); 906d9fbd03dSHollis Blanchard r = RESUME_GUEST; 907d9fbd03dSHollis Blanchard break; 908d9fbd03dSHollis Blanchard 9094cd35f67SScott Wood #ifdef CONFIG_SPE 9104cd35f67SScott Wood case BOOKE_INTERRUPT_SPE_UNAVAIL: { 9114cd35f67SScott Wood if (vcpu->arch.shared->msr & MSR_SPE) 9124cd35f67SScott Wood kvmppc_vcpu_enable_spe(vcpu); 9134cd35f67SScott Wood else 9144cd35f67SScott Wood kvmppc_booke_queue_irqprio(vcpu, 9154cd35f67SScott Wood BOOKE_IRQPRIO_SPE_UNAVAIL); 916bb3a8a17SHollis Blanchard r = RESUME_GUEST; 917bb3a8a17SHollis Blanchard break; 9184cd35f67SScott Wood } 919bb3a8a17SHollis Blanchard 920bb3a8a17SHollis Blanchard case BOOKE_INTERRUPT_SPE_FP_DATA: 921bb3a8a17SHollis Blanchard kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA); 922bb3a8a17SHollis Blanchard r = RESUME_GUEST; 923bb3a8a17SHollis Blanchard break; 924bb3a8a17SHollis Blanchard 925bb3a8a17SHollis Blanchard case BOOKE_INTERRUPT_SPE_FP_ROUND: 926bb3a8a17SHollis Blanchard kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND); 927bb3a8a17SHollis Blanchard r = RESUME_GUEST; 928bb3a8a17SHollis Blanchard break; 9294cd35f67SScott Wood #else 9304cd35f67SScott Wood case BOOKE_INTERRUPT_SPE_UNAVAIL: 9314cd35f67SScott Wood /* 9324cd35f67SScott Wood * Guest wants SPE, but host kernel doesn't support it. Send 9334cd35f67SScott Wood * an "unimplemented operation" program check to the guest. 9344cd35f67SScott Wood */ 9354cd35f67SScott Wood kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV); 9364cd35f67SScott Wood r = RESUME_GUEST; 9374cd35f67SScott Wood break; 9384cd35f67SScott Wood 9394cd35f67SScott Wood /* 9404cd35f67SScott Wood * These really should never happen without CONFIG_SPE, 9414cd35f67SScott Wood * as we should never enable the real MSR[SPE] in the guest. 9424cd35f67SScott Wood */ 9434cd35f67SScott Wood case BOOKE_INTERRUPT_SPE_FP_DATA: 9444cd35f67SScott Wood case BOOKE_INTERRUPT_SPE_FP_ROUND: 9454cd35f67SScott Wood printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n", 9464cd35f67SScott Wood __func__, exit_nr, vcpu->arch.pc); 9474cd35f67SScott Wood run->hw.hardware_exit_reason = exit_nr; 9484cd35f67SScott Wood r = RESUME_HOST; 9494cd35f67SScott Wood break; 9504cd35f67SScott Wood #endif 951bb3a8a17SHollis Blanchard 952d9fbd03dSHollis Blanchard case BOOKE_INTERRUPT_DATA_STORAGE: 953daf5e271SLiu Yu kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear, 954daf5e271SLiu Yu vcpu->arch.fault_esr); 9557b701591SHollis Blanchard kvmppc_account_exit(vcpu, DSI_EXITS); 956d9fbd03dSHollis Blanchard r = RESUME_GUEST; 957d9fbd03dSHollis Blanchard break; 958d9fbd03dSHollis Blanchard 959d9fbd03dSHollis Blanchard case BOOKE_INTERRUPT_INST_STORAGE: 960daf5e271SLiu Yu kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr); 9617b701591SHollis Blanchard kvmppc_account_exit(vcpu, ISI_EXITS); 962d9fbd03dSHollis Blanchard r = RESUME_GUEST; 963d9fbd03dSHollis Blanchard break; 964d9fbd03dSHollis Blanchard 965d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV 966d30f6e48SScott Wood case BOOKE_INTERRUPT_HV_SYSCALL: 967d30f6e48SScott Wood if (!(vcpu->arch.shared->msr & MSR_PR)) { 968d30f6e48SScott Wood kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu)); 969d30f6e48SScott Wood } else { 970d30f6e48SScott Wood /* 971d30f6e48SScott Wood * hcall from guest userspace -- send privileged 972d30f6e48SScott Wood * instruction program check. 973d30f6e48SScott Wood */ 974d30f6e48SScott Wood kvmppc_core_queue_program(vcpu, ESR_PPR); 975d30f6e48SScott Wood } 976d30f6e48SScott Wood 977d30f6e48SScott Wood r = RESUME_GUEST; 978d30f6e48SScott Wood break; 979d30f6e48SScott Wood #else 980d9fbd03dSHollis Blanchard case BOOKE_INTERRUPT_SYSCALL: 9812a342ed5SAlexander Graf if (!(vcpu->arch.shared->msr & MSR_PR) && 9822a342ed5SAlexander Graf (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) { 9832a342ed5SAlexander Graf /* KVM PV hypercalls */ 9842a342ed5SAlexander Graf kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu)); 9852a342ed5SAlexander Graf r = RESUME_GUEST; 9862a342ed5SAlexander Graf } else { 9872a342ed5SAlexander Graf /* Guest syscalls */ 988d4cf3892SHollis Blanchard kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL); 9892a342ed5SAlexander Graf } 9907b701591SHollis Blanchard kvmppc_account_exit(vcpu, SYSCALL_EXITS); 991d9fbd03dSHollis Blanchard r = RESUME_GUEST; 992d9fbd03dSHollis Blanchard break; 993d30f6e48SScott Wood #endif 994d9fbd03dSHollis Blanchard 995d9fbd03dSHollis Blanchard case BOOKE_INTERRUPT_DTLB_MISS: { 996d9fbd03dSHollis Blanchard unsigned long eaddr = vcpu->arch.fault_dear; 9977924bd41SHollis Blanchard int gtlb_index; 998475e7cddSHollis Blanchard gpa_t gpaddr; 999d9fbd03dSHollis Blanchard gfn_t gfn; 1000d9fbd03dSHollis Blanchard 1001bf7ca4bdSAlexander Graf #ifdef CONFIG_KVM_E500V2 1002a4cd8b23SScott Wood if (!(vcpu->arch.shared->msr & MSR_PR) && 1003a4cd8b23SScott Wood (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) { 1004a4cd8b23SScott Wood kvmppc_map_magic(vcpu); 1005a4cd8b23SScott Wood kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS); 1006a4cd8b23SScott Wood r = RESUME_GUEST; 1007a4cd8b23SScott Wood 1008a4cd8b23SScott Wood break; 1009a4cd8b23SScott Wood } 1010a4cd8b23SScott Wood #endif 1011a4cd8b23SScott Wood 1012d9fbd03dSHollis Blanchard /* Check the guest TLB. */ 1013fa86b8ddSHollis Blanchard gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr); 10147924bd41SHollis Blanchard if (gtlb_index < 0) { 1015d9fbd03dSHollis Blanchard /* The guest didn't have a mapping for it. */ 1016daf5e271SLiu Yu kvmppc_core_queue_dtlb_miss(vcpu, 1017daf5e271SLiu Yu vcpu->arch.fault_dear, 1018daf5e271SLiu Yu vcpu->arch.fault_esr); 1019b52a638cSHollis Blanchard kvmppc_mmu_dtlb_miss(vcpu); 10207b701591SHollis Blanchard kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS); 1021d9fbd03dSHollis Blanchard r = RESUME_GUEST; 1022d9fbd03dSHollis Blanchard break; 1023d9fbd03dSHollis Blanchard } 1024d9fbd03dSHollis Blanchard 1025be8d1caeSHollis Blanchard gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr); 1026475e7cddSHollis Blanchard gfn = gpaddr >> PAGE_SHIFT; 1027d9fbd03dSHollis Blanchard 1028d9fbd03dSHollis Blanchard if (kvm_is_visible_gfn(vcpu->kvm, gfn)) { 1029d9fbd03dSHollis Blanchard /* The guest TLB had a mapping, but the shadow TLB 1030d9fbd03dSHollis Blanchard * didn't, and it is RAM. This could be because: 1031d9fbd03dSHollis Blanchard * a) the entry is mapping the host kernel, or 1032d9fbd03dSHollis Blanchard * b) the guest used a large mapping which we're faking 1033d9fbd03dSHollis Blanchard * Either way, we need to satisfy the fault without 1034d9fbd03dSHollis Blanchard * invoking the guest. */ 103558a96214SHollis Blanchard kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index); 10367b701591SHollis Blanchard kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS); 1037d9fbd03dSHollis Blanchard r = RESUME_GUEST; 1038d9fbd03dSHollis Blanchard } else { 1039d9fbd03dSHollis Blanchard /* Guest has mapped and accessed a page which is not 1040d9fbd03dSHollis Blanchard * actually RAM. */ 1041475e7cddSHollis Blanchard vcpu->arch.paddr_accessed = gpaddr; 10426020c0f6SAlexander Graf vcpu->arch.vaddr_accessed = eaddr; 1043d9fbd03dSHollis Blanchard r = kvmppc_emulate_mmio(run, vcpu); 10447b701591SHollis Blanchard kvmppc_account_exit(vcpu, MMIO_EXITS); 1045d9fbd03dSHollis Blanchard } 1046d9fbd03dSHollis Blanchard 1047d9fbd03dSHollis Blanchard break; 1048d9fbd03dSHollis Blanchard } 1049d9fbd03dSHollis Blanchard 1050d9fbd03dSHollis Blanchard case BOOKE_INTERRUPT_ITLB_MISS: { 1051d9fbd03dSHollis Blanchard unsigned long eaddr = vcpu->arch.pc; 105289168618SHollis Blanchard gpa_t gpaddr; 1053d9fbd03dSHollis Blanchard gfn_t gfn; 10547924bd41SHollis Blanchard int gtlb_index; 1055d9fbd03dSHollis Blanchard 1056d9fbd03dSHollis Blanchard r = RESUME_GUEST; 1057d9fbd03dSHollis Blanchard 1058d9fbd03dSHollis Blanchard /* Check the guest TLB. */ 1059fa86b8ddSHollis Blanchard gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr); 10607924bd41SHollis Blanchard if (gtlb_index < 0) { 1061d9fbd03dSHollis Blanchard /* The guest didn't have a mapping for it. */ 1062d4cf3892SHollis Blanchard kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS); 1063b52a638cSHollis Blanchard kvmppc_mmu_itlb_miss(vcpu); 10647b701591SHollis Blanchard kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS); 1065d9fbd03dSHollis Blanchard break; 1066d9fbd03dSHollis Blanchard } 1067d9fbd03dSHollis Blanchard 10687b701591SHollis Blanchard kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS); 1069d9fbd03dSHollis Blanchard 1070be8d1caeSHollis Blanchard gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr); 107189168618SHollis Blanchard gfn = gpaddr >> PAGE_SHIFT; 1072d9fbd03dSHollis Blanchard 1073d9fbd03dSHollis Blanchard if (kvm_is_visible_gfn(vcpu->kvm, gfn)) { 1074d9fbd03dSHollis Blanchard /* The guest TLB had a mapping, but the shadow TLB 1075d9fbd03dSHollis Blanchard * didn't. This could be because: 1076d9fbd03dSHollis Blanchard * a) the entry is mapping the host kernel, or 1077d9fbd03dSHollis Blanchard * b) the guest used a large mapping which we're faking 1078d9fbd03dSHollis Blanchard * Either way, we need to satisfy the fault without 1079d9fbd03dSHollis Blanchard * invoking the guest. */ 108058a96214SHollis Blanchard kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index); 1081d9fbd03dSHollis Blanchard } else { 1082d9fbd03dSHollis Blanchard /* Guest mapped and leaped at non-RAM! */ 1083d4cf3892SHollis Blanchard kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK); 1084d9fbd03dSHollis Blanchard } 1085d9fbd03dSHollis Blanchard 1086d9fbd03dSHollis Blanchard break; 1087d9fbd03dSHollis Blanchard } 1088d9fbd03dSHollis Blanchard 1089d9fbd03dSHollis Blanchard case BOOKE_INTERRUPT_DEBUG: { 1090d9fbd03dSHollis Blanchard u32 dbsr; 1091d9fbd03dSHollis Blanchard 1092d9fbd03dSHollis Blanchard vcpu->arch.pc = mfspr(SPRN_CSRR0); 1093d9fbd03dSHollis Blanchard 1094d9fbd03dSHollis Blanchard /* clear IAC events in DBSR register */ 1095d9fbd03dSHollis Blanchard dbsr = mfspr(SPRN_DBSR); 1096d9fbd03dSHollis Blanchard dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4; 1097d9fbd03dSHollis Blanchard mtspr(SPRN_DBSR, dbsr); 1098d9fbd03dSHollis Blanchard 1099d9fbd03dSHollis Blanchard run->exit_reason = KVM_EXIT_DEBUG; 11007b701591SHollis Blanchard kvmppc_account_exit(vcpu, DEBUG_EXITS); 1101d9fbd03dSHollis Blanchard r = RESUME_HOST; 1102d9fbd03dSHollis Blanchard break; 1103d9fbd03dSHollis Blanchard } 1104d9fbd03dSHollis Blanchard 1105d9fbd03dSHollis Blanchard default: 1106d9fbd03dSHollis Blanchard printk(KERN_EMERG "exit_nr %d\n", exit_nr); 1107d9fbd03dSHollis Blanchard BUG(); 1108d9fbd03dSHollis Blanchard } 1109d9fbd03dSHollis Blanchard 1110a8e4ef84SAlexander Graf /* 1111a8e4ef84SAlexander Graf * To avoid clobbering exit_reason, only check for signals if we 1112a8e4ef84SAlexander Graf * aren't already exiting to userspace for some other reason. 1113a8e4ef84SAlexander Graf */ 111403660ba2SAlexander Graf if (!(r & RESUME_HOST)) { 1115d9fbd03dSHollis Blanchard local_irq_disable(); 11167ee78855SAlexander Graf s = kvmppc_prepare_to_enter(vcpu); 11177ee78855SAlexander Graf if (s <= 0) { 111824afa37bSAlexander Graf local_irq_enable(); 11197ee78855SAlexander Graf r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV); 112024afa37bSAlexander Graf } else { 1121bd2be683SAlexander Graf kvmppc_lazy_ee_enable(); 112224afa37bSAlexander Graf } 112324afa37bSAlexander Graf } 1124706fb730SAlexander Graf 1125d9fbd03dSHollis Blanchard return r; 1126d9fbd03dSHollis Blanchard } 1127d9fbd03dSHollis Blanchard 1128d9fbd03dSHollis Blanchard /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */ 1129d9fbd03dSHollis Blanchard int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) 1130d9fbd03dSHollis Blanchard { 1131082decf2SHollis Blanchard int i; 1132af8f38b3SAlexander Graf int r; 1133082decf2SHollis Blanchard 1134d9fbd03dSHollis Blanchard vcpu->arch.pc = 0; 1135b5904972SScott Wood vcpu->arch.shared->pir = vcpu->vcpu_id; 11368e5b26b5SAlexander Graf kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */ 1137d30f6e48SScott Wood kvmppc_set_msr(vcpu, 0); 1138d9fbd03dSHollis Blanchard 1139d30f6e48SScott Wood #ifndef CONFIG_KVM_BOOKE_HV 1140d30f6e48SScott Wood vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS; 1141d9fbd03dSHollis Blanchard vcpu->arch.shadow_pid = 1; 1142d30f6e48SScott Wood vcpu->arch.shared->msr = 0; 1143d30f6e48SScott Wood #endif 1144d9fbd03dSHollis Blanchard 1145082decf2SHollis Blanchard /* Eye-catching numbers so we know if the guest takes an interrupt 1146082decf2SHollis Blanchard * before it's programmed its own IVPR/IVORs. */ 1147d9fbd03dSHollis Blanchard vcpu->arch.ivpr = 0x55550000; 1148082decf2SHollis Blanchard for (i = 0; i < BOOKE_IRQPRIO_MAX; i++) 1149082decf2SHollis Blanchard vcpu->arch.ivor[i] = 0x7700 | i * 4; 1150d9fbd03dSHollis Blanchard 115173e75b41SHollis Blanchard kvmppc_init_timing_stats(vcpu); 115273e75b41SHollis Blanchard 1153af8f38b3SAlexander Graf r = kvmppc_core_vcpu_setup(vcpu); 1154af8f38b3SAlexander Graf kvmppc_sanity_check(vcpu); 1155af8f38b3SAlexander Graf return r; 1156d9fbd03dSHollis Blanchard } 1157d9fbd03dSHollis Blanchard 1158f61c94bbSBharat Bhushan int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu) 1159f61c94bbSBharat Bhushan { 1160f61c94bbSBharat Bhushan /* setup watchdog timer once */ 1161f61c94bbSBharat Bhushan spin_lock_init(&vcpu->arch.wdt_lock); 1162f61c94bbSBharat Bhushan setup_timer(&vcpu->arch.wdt_timer, kvmppc_watchdog_func, 1163f61c94bbSBharat Bhushan (unsigned long)vcpu); 1164f61c94bbSBharat Bhushan 1165f61c94bbSBharat Bhushan return 0; 1166f61c94bbSBharat Bhushan } 1167f61c94bbSBharat Bhushan 1168f61c94bbSBharat Bhushan void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu) 1169f61c94bbSBharat Bhushan { 1170f61c94bbSBharat Bhushan del_timer_sync(&vcpu->arch.wdt_timer); 1171f61c94bbSBharat Bhushan } 1172f61c94bbSBharat Bhushan 1173d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 1174d9fbd03dSHollis Blanchard { 1175d9fbd03dSHollis Blanchard int i; 1176d9fbd03dSHollis Blanchard 1177d9fbd03dSHollis Blanchard regs->pc = vcpu->arch.pc; 1178992b5b29SAlexander Graf regs->cr = kvmppc_get_cr(vcpu); 1179d9fbd03dSHollis Blanchard regs->ctr = vcpu->arch.ctr; 1180d9fbd03dSHollis Blanchard regs->lr = vcpu->arch.lr; 1181992b5b29SAlexander Graf regs->xer = kvmppc_get_xer(vcpu); 1182666e7252SAlexander Graf regs->msr = vcpu->arch.shared->msr; 1183de7906c3SAlexander Graf regs->srr0 = vcpu->arch.shared->srr0; 1184de7906c3SAlexander Graf regs->srr1 = vcpu->arch.shared->srr1; 1185d9fbd03dSHollis Blanchard regs->pid = vcpu->arch.pid; 1186a73a9599SAlexander Graf regs->sprg0 = vcpu->arch.shared->sprg0; 1187a73a9599SAlexander Graf regs->sprg1 = vcpu->arch.shared->sprg1; 1188a73a9599SAlexander Graf regs->sprg2 = vcpu->arch.shared->sprg2; 1189a73a9599SAlexander Graf regs->sprg3 = vcpu->arch.shared->sprg3; 1190b5904972SScott Wood regs->sprg4 = vcpu->arch.shared->sprg4; 1191b5904972SScott Wood regs->sprg5 = vcpu->arch.shared->sprg5; 1192b5904972SScott Wood regs->sprg6 = vcpu->arch.shared->sprg6; 1193b5904972SScott Wood regs->sprg7 = vcpu->arch.shared->sprg7; 1194d9fbd03dSHollis Blanchard 1195d9fbd03dSHollis Blanchard for (i = 0; i < ARRAY_SIZE(regs->gpr); i++) 11968e5b26b5SAlexander Graf regs->gpr[i] = kvmppc_get_gpr(vcpu, i); 1197d9fbd03dSHollis Blanchard 1198d9fbd03dSHollis Blanchard return 0; 1199d9fbd03dSHollis Blanchard } 1200d9fbd03dSHollis Blanchard 1201d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 1202d9fbd03dSHollis Blanchard { 1203d9fbd03dSHollis Blanchard int i; 1204d9fbd03dSHollis Blanchard 1205d9fbd03dSHollis Blanchard vcpu->arch.pc = regs->pc; 1206992b5b29SAlexander Graf kvmppc_set_cr(vcpu, regs->cr); 1207d9fbd03dSHollis Blanchard vcpu->arch.ctr = regs->ctr; 1208d9fbd03dSHollis Blanchard vcpu->arch.lr = regs->lr; 1209992b5b29SAlexander Graf kvmppc_set_xer(vcpu, regs->xer); 1210b8fd68acSHollis Blanchard kvmppc_set_msr(vcpu, regs->msr); 1211de7906c3SAlexander Graf vcpu->arch.shared->srr0 = regs->srr0; 1212de7906c3SAlexander Graf vcpu->arch.shared->srr1 = regs->srr1; 12135ce941eeSScott Wood kvmppc_set_pid(vcpu, regs->pid); 1214a73a9599SAlexander Graf vcpu->arch.shared->sprg0 = regs->sprg0; 1215a73a9599SAlexander Graf vcpu->arch.shared->sprg1 = regs->sprg1; 1216a73a9599SAlexander Graf vcpu->arch.shared->sprg2 = regs->sprg2; 1217a73a9599SAlexander Graf vcpu->arch.shared->sprg3 = regs->sprg3; 1218b5904972SScott Wood vcpu->arch.shared->sprg4 = regs->sprg4; 1219b5904972SScott Wood vcpu->arch.shared->sprg5 = regs->sprg5; 1220b5904972SScott Wood vcpu->arch.shared->sprg6 = regs->sprg6; 1221b5904972SScott Wood vcpu->arch.shared->sprg7 = regs->sprg7; 1222d9fbd03dSHollis Blanchard 12238e5b26b5SAlexander Graf for (i = 0; i < ARRAY_SIZE(regs->gpr); i++) 12248e5b26b5SAlexander Graf kvmppc_set_gpr(vcpu, i, regs->gpr[i]); 1225d9fbd03dSHollis Blanchard 1226d9fbd03dSHollis Blanchard return 0; 1227d9fbd03dSHollis Blanchard } 1228d9fbd03dSHollis Blanchard 12295ce941eeSScott Wood static void get_sregs_base(struct kvm_vcpu *vcpu, 12305ce941eeSScott Wood struct kvm_sregs *sregs) 12315ce941eeSScott Wood { 12325ce941eeSScott Wood u64 tb = get_tb(); 12335ce941eeSScott Wood 12345ce941eeSScott Wood sregs->u.e.features |= KVM_SREGS_E_BASE; 12355ce941eeSScott Wood 12365ce941eeSScott Wood sregs->u.e.csrr0 = vcpu->arch.csrr0; 12375ce941eeSScott Wood sregs->u.e.csrr1 = vcpu->arch.csrr1; 12385ce941eeSScott Wood sregs->u.e.mcsr = vcpu->arch.mcsr; 1239d30f6e48SScott Wood sregs->u.e.esr = get_guest_esr(vcpu); 1240d30f6e48SScott Wood sregs->u.e.dear = get_guest_dear(vcpu); 12415ce941eeSScott Wood sregs->u.e.tsr = vcpu->arch.tsr; 12425ce941eeSScott Wood sregs->u.e.tcr = vcpu->arch.tcr; 12435ce941eeSScott Wood sregs->u.e.dec = kvmppc_get_dec(vcpu, tb); 12445ce941eeSScott Wood sregs->u.e.tb = tb; 12455ce941eeSScott Wood sregs->u.e.vrsave = vcpu->arch.vrsave; 12465ce941eeSScott Wood } 12475ce941eeSScott Wood 12485ce941eeSScott Wood static int set_sregs_base(struct kvm_vcpu *vcpu, 12495ce941eeSScott Wood struct kvm_sregs *sregs) 12505ce941eeSScott Wood { 12515ce941eeSScott Wood if (!(sregs->u.e.features & KVM_SREGS_E_BASE)) 12525ce941eeSScott Wood return 0; 12535ce941eeSScott Wood 12545ce941eeSScott Wood vcpu->arch.csrr0 = sregs->u.e.csrr0; 12555ce941eeSScott Wood vcpu->arch.csrr1 = sregs->u.e.csrr1; 12565ce941eeSScott Wood vcpu->arch.mcsr = sregs->u.e.mcsr; 1257d30f6e48SScott Wood set_guest_esr(vcpu, sregs->u.e.esr); 1258d30f6e48SScott Wood set_guest_dear(vcpu, sregs->u.e.dear); 12595ce941eeSScott Wood vcpu->arch.vrsave = sregs->u.e.vrsave; 1260dfd4d47eSScott Wood kvmppc_set_tcr(vcpu, sregs->u.e.tcr); 12615ce941eeSScott Wood 1262dfd4d47eSScott Wood if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) { 12635ce941eeSScott Wood vcpu->arch.dec = sregs->u.e.dec; 12645ce941eeSScott Wood kvmppc_emulate_dec(vcpu); 1265dfd4d47eSScott Wood } 12665ce941eeSScott Wood 12675ce941eeSScott Wood if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) { 1268f61c94bbSBharat Bhushan u32 old_tsr = vcpu->arch.tsr; 1269f61c94bbSBharat Bhushan 1270dfd4d47eSScott Wood vcpu->arch.tsr = sregs->u.e.tsr; 1271f61c94bbSBharat Bhushan 1272f61c94bbSBharat Bhushan if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS)) 1273f61c94bbSBharat Bhushan arm_next_watchdog(vcpu); 1274f61c94bbSBharat Bhushan 1275dfd4d47eSScott Wood update_timer_ints(vcpu); 12765ce941eeSScott Wood } 12775ce941eeSScott Wood 12785ce941eeSScott Wood return 0; 12795ce941eeSScott Wood } 12805ce941eeSScott Wood 12815ce941eeSScott Wood static void get_sregs_arch206(struct kvm_vcpu *vcpu, 12825ce941eeSScott Wood struct kvm_sregs *sregs) 12835ce941eeSScott Wood { 12845ce941eeSScott Wood sregs->u.e.features |= KVM_SREGS_E_ARCH206; 12855ce941eeSScott Wood 1286841741f2SScott Wood sregs->u.e.pir = vcpu->vcpu_id; 12875ce941eeSScott Wood sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0; 12885ce941eeSScott Wood sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1; 12895ce941eeSScott Wood sregs->u.e.decar = vcpu->arch.decar; 12905ce941eeSScott Wood sregs->u.e.ivpr = vcpu->arch.ivpr; 12915ce941eeSScott Wood } 12925ce941eeSScott Wood 12935ce941eeSScott Wood static int set_sregs_arch206(struct kvm_vcpu *vcpu, 12945ce941eeSScott Wood struct kvm_sregs *sregs) 12955ce941eeSScott Wood { 12965ce941eeSScott Wood if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206)) 12975ce941eeSScott Wood return 0; 12985ce941eeSScott Wood 1299841741f2SScott Wood if (sregs->u.e.pir != vcpu->vcpu_id) 13005ce941eeSScott Wood return -EINVAL; 13015ce941eeSScott Wood 13025ce941eeSScott Wood vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0; 13035ce941eeSScott Wood vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1; 13045ce941eeSScott Wood vcpu->arch.decar = sregs->u.e.decar; 13055ce941eeSScott Wood vcpu->arch.ivpr = sregs->u.e.ivpr; 13065ce941eeSScott Wood 13075ce941eeSScott Wood return 0; 13085ce941eeSScott Wood } 13095ce941eeSScott Wood 13105ce941eeSScott Wood void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) 13115ce941eeSScott Wood { 13125ce941eeSScott Wood sregs->u.e.features |= KVM_SREGS_E_IVOR; 13135ce941eeSScott Wood 13145ce941eeSScott Wood sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL]; 13155ce941eeSScott Wood sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK]; 13165ce941eeSScott Wood sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE]; 13175ce941eeSScott Wood sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE]; 13185ce941eeSScott Wood sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL]; 13195ce941eeSScott Wood sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT]; 13205ce941eeSScott Wood sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM]; 13215ce941eeSScott Wood sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL]; 13225ce941eeSScott Wood sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL]; 13235ce941eeSScott Wood sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL]; 13245ce941eeSScott Wood sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER]; 13255ce941eeSScott Wood sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT]; 13265ce941eeSScott Wood sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG]; 13275ce941eeSScott Wood sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS]; 13285ce941eeSScott Wood sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS]; 13295ce941eeSScott Wood sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG]; 13305ce941eeSScott Wood } 13315ce941eeSScott Wood 13325ce941eeSScott Wood int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) 13335ce941eeSScott Wood { 13345ce941eeSScott Wood if (!(sregs->u.e.features & KVM_SREGS_E_IVOR)) 13355ce941eeSScott Wood return 0; 13365ce941eeSScott Wood 13375ce941eeSScott Wood vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0]; 13385ce941eeSScott Wood vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1]; 13395ce941eeSScott Wood vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2]; 13405ce941eeSScott Wood vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3]; 13415ce941eeSScott Wood vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4]; 13425ce941eeSScott Wood vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5]; 13435ce941eeSScott Wood vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6]; 13445ce941eeSScott Wood vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7]; 13455ce941eeSScott Wood vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8]; 13465ce941eeSScott Wood vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9]; 13475ce941eeSScott Wood vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10]; 13485ce941eeSScott Wood vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11]; 13495ce941eeSScott Wood vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12]; 13505ce941eeSScott Wood vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13]; 13515ce941eeSScott Wood vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14]; 13525ce941eeSScott Wood vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15]; 13535ce941eeSScott Wood 13545ce941eeSScott Wood return 0; 13555ce941eeSScott Wood } 13565ce941eeSScott Wood 1357d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 1358d9fbd03dSHollis Blanchard struct kvm_sregs *sregs) 1359d9fbd03dSHollis Blanchard { 13605ce941eeSScott Wood sregs->pvr = vcpu->arch.pvr; 13615ce941eeSScott Wood 13625ce941eeSScott Wood get_sregs_base(vcpu, sregs); 13635ce941eeSScott Wood get_sregs_arch206(vcpu, sregs); 13645ce941eeSScott Wood kvmppc_core_get_sregs(vcpu, sregs); 13655ce941eeSScott Wood return 0; 1366d9fbd03dSHollis Blanchard } 1367d9fbd03dSHollis Blanchard 1368d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 1369d9fbd03dSHollis Blanchard struct kvm_sregs *sregs) 1370d9fbd03dSHollis Blanchard { 13715ce941eeSScott Wood int ret; 13725ce941eeSScott Wood 13735ce941eeSScott Wood if (vcpu->arch.pvr != sregs->pvr) 13745ce941eeSScott Wood return -EINVAL; 13755ce941eeSScott Wood 13765ce941eeSScott Wood ret = set_sregs_base(vcpu, sregs); 13775ce941eeSScott Wood if (ret < 0) 13785ce941eeSScott Wood return ret; 13795ce941eeSScott Wood 13805ce941eeSScott Wood ret = set_sregs_arch206(vcpu, sregs); 13815ce941eeSScott Wood if (ret < 0) 13825ce941eeSScott Wood return ret; 13835ce941eeSScott Wood 13845ce941eeSScott Wood return kvmppc_core_set_sregs(vcpu, sregs); 1385d9fbd03dSHollis Blanchard } 1386d9fbd03dSHollis Blanchard 138731f3438eSPaul Mackerras int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) 138831f3438eSPaul Mackerras { 13896df8d3fcSBharat Bhushan int r = -EINVAL; 13906df8d3fcSBharat Bhushan 13916df8d3fcSBharat Bhushan switch (reg->id) { 13926df8d3fcSBharat Bhushan case KVM_REG_PPC_IAC1: 13936df8d3fcSBharat Bhushan case KVM_REG_PPC_IAC2: 13946df8d3fcSBharat Bhushan case KVM_REG_PPC_IAC3: 13956df8d3fcSBharat Bhushan case KVM_REG_PPC_IAC4: { 13966df8d3fcSBharat Bhushan int iac = reg->id - KVM_REG_PPC_IAC1; 13976df8d3fcSBharat Bhushan r = copy_to_user((u64 __user *)(long)reg->addr, 13986df8d3fcSBharat Bhushan &vcpu->arch.dbg_reg.iac[iac], sizeof(u64)); 13996df8d3fcSBharat Bhushan break; 14006df8d3fcSBharat Bhushan } 14016df8d3fcSBharat Bhushan case KVM_REG_PPC_DAC1: 14026df8d3fcSBharat Bhushan case KVM_REG_PPC_DAC2: { 14036df8d3fcSBharat Bhushan int dac = reg->id - KVM_REG_PPC_DAC1; 14046df8d3fcSBharat Bhushan r = copy_to_user((u64 __user *)(long)reg->addr, 14056df8d3fcSBharat Bhushan &vcpu->arch.dbg_reg.dac[dac], sizeof(u64)); 14066df8d3fcSBharat Bhushan break; 14076df8d3fcSBharat Bhushan } 1408352df1deSMihai Caraman #if defined(CONFIG_64BIT) 1409352df1deSMihai Caraman case KVM_REG_PPC_EPCR: 1410352df1deSMihai Caraman r = put_user(vcpu->arch.epcr, (u32 __user *)(long)reg->addr); 1411352df1deSMihai Caraman break; 1412352df1deSMihai Caraman #endif 14136df8d3fcSBharat Bhushan default: 14146df8d3fcSBharat Bhushan break; 14156df8d3fcSBharat Bhushan } 14166df8d3fcSBharat Bhushan return r; 141731f3438eSPaul Mackerras } 141831f3438eSPaul Mackerras 141931f3438eSPaul Mackerras int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) 142031f3438eSPaul Mackerras { 14216df8d3fcSBharat Bhushan int r = -EINVAL; 14226df8d3fcSBharat Bhushan 14236df8d3fcSBharat Bhushan switch (reg->id) { 14246df8d3fcSBharat Bhushan case KVM_REG_PPC_IAC1: 14256df8d3fcSBharat Bhushan case KVM_REG_PPC_IAC2: 14266df8d3fcSBharat Bhushan case KVM_REG_PPC_IAC3: 14276df8d3fcSBharat Bhushan case KVM_REG_PPC_IAC4: { 14286df8d3fcSBharat Bhushan int iac = reg->id - KVM_REG_PPC_IAC1; 14296df8d3fcSBharat Bhushan r = copy_from_user(&vcpu->arch.dbg_reg.iac[iac], 14306df8d3fcSBharat Bhushan (u64 __user *)(long)reg->addr, sizeof(u64)); 14316df8d3fcSBharat Bhushan break; 14326df8d3fcSBharat Bhushan } 14336df8d3fcSBharat Bhushan case KVM_REG_PPC_DAC1: 14346df8d3fcSBharat Bhushan case KVM_REG_PPC_DAC2: { 14356df8d3fcSBharat Bhushan int dac = reg->id - KVM_REG_PPC_DAC1; 14366df8d3fcSBharat Bhushan r = copy_from_user(&vcpu->arch.dbg_reg.dac[dac], 14376df8d3fcSBharat Bhushan (u64 __user *)(long)reg->addr, sizeof(u64)); 14386df8d3fcSBharat Bhushan break; 14396df8d3fcSBharat Bhushan } 1440352df1deSMihai Caraman #if defined(CONFIG_64BIT) 1441352df1deSMihai Caraman case KVM_REG_PPC_EPCR: { 1442352df1deSMihai Caraman u32 new_epcr; 1443352df1deSMihai Caraman r = get_user(new_epcr, (u32 __user *)(long)reg->addr); 1444352df1deSMihai Caraman if (r == 0) 1445352df1deSMihai Caraman kvmppc_set_epcr(vcpu, new_epcr); 1446352df1deSMihai Caraman break; 1447352df1deSMihai Caraman } 1448352df1deSMihai Caraman #endif 14496df8d3fcSBharat Bhushan default: 14506df8d3fcSBharat Bhushan break; 14516df8d3fcSBharat Bhushan } 14526df8d3fcSBharat Bhushan return r; 145331f3438eSPaul Mackerras } 145431f3438eSPaul Mackerras 1455d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 1456d9fbd03dSHollis Blanchard { 1457d9fbd03dSHollis Blanchard return -ENOTSUPP; 1458d9fbd03dSHollis Blanchard } 1459d9fbd03dSHollis Blanchard 1460d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 1461d9fbd03dSHollis Blanchard { 1462d9fbd03dSHollis Blanchard return -ENOTSUPP; 1463d9fbd03dSHollis Blanchard } 1464d9fbd03dSHollis Blanchard 1465d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 1466d9fbd03dSHollis Blanchard struct kvm_translation *tr) 1467d9fbd03dSHollis Blanchard { 146898001d8dSAvi Kivity int r; 146998001d8dSAvi Kivity 147098001d8dSAvi Kivity r = kvmppc_core_vcpu_translate(vcpu, tr); 147198001d8dSAvi Kivity return r; 1472d9fbd03dSHollis Blanchard } 1473d9fbd03dSHollis Blanchard 14744e755758SAlexander Graf int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) 14754e755758SAlexander Graf { 14764e755758SAlexander Graf return -ENOTSUPP; 14774e755758SAlexander Graf } 14784e755758SAlexander Graf 1479a66b48c3SPaul Mackerras void kvmppc_core_free_memslot(struct kvm_memory_slot *free, 1480a66b48c3SPaul Mackerras struct kvm_memory_slot *dont) 1481a66b48c3SPaul Mackerras { 1482a66b48c3SPaul Mackerras } 1483a66b48c3SPaul Mackerras 1484a66b48c3SPaul Mackerras int kvmppc_core_create_memslot(struct kvm_memory_slot *slot, 1485a66b48c3SPaul Mackerras unsigned long npages) 1486a66b48c3SPaul Mackerras { 1487a66b48c3SPaul Mackerras return 0; 1488a66b48c3SPaul Mackerras } 1489a66b48c3SPaul Mackerras 1490f9e0554dSPaul Mackerras int kvmppc_core_prepare_memory_region(struct kvm *kvm, 1491a66b48c3SPaul Mackerras struct kvm_memory_slot *memslot, 1492f9e0554dSPaul Mackerras struct kvm_userspace_memory_region *mem) 1493f9e0554dSPaul Mackerras { 1494f9e0554dSPaul Mackerras return 0; 1495f9e0554dSPaul Mackerras } 1496f9e0554dSPaul Mackerras 1497f9e0554dSPaul Mackerras void kvmppc_core_commit_memory_region(struct kvm *kvm, 1498dfe49dbdSPaul Mackerras struct kvm_userspace_memory_region *mem, 1499dfe49dbdSPaul Mackerras struct kvm_memory_slot old) 1500dfe49dbdSPaul Mackerras { 1501dfe49dbdSPaul Mackerras } 1502dfe49dbdSPaul Mackerras 1503dfe49dbdSPaul Mackerras void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot) 1504f9e0554dSPaul Mackerras { 1505f9e0554dSPaul Mackerras } 1506f9e0554dSPaul Mackerras 150738f98824SMihai Caraman void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr) 150838f98824SMihai Caraman { 150938f98824SMihai Caraman #if defined(CONFIG_64BIT) 151038f98824SMihai Caraman vcpu->arch.epcr = new_epcr; 151138f98824SMihai Caraman #ifdef CONFIG_KVM_BOOKE_HV 151238f98824SMihai Caraman vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM; 151338f98824SMihai Caraman if (vcpu->arch.epcr & SPRN_EPCR_ICM) 151438f98824SMihai Caraman vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM; 151538f98824SMihai Caraman #endif 151638f98824SMihai Caraman #endif 151738f98824SMihai Caraman } 151838f98824SMihai Caraman 1519dfd4d47eSScott Wood void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr) 1520dfd4d47eSScott Wood { 1521dfd4d47eSScott Wood vcpu->arch.tcr = new_tcr; 1522f61c94bbSBharat Bhushan arm_next_watchdog(vcpu); 1523dfd4d47eSScott Wood update_timer_ints(vcpu); 1524dfd4d47eSScott Wood } 1525dfd4d47eSScott Wood 1526dfd4d47eSScott Wood void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits) 1527dfd4d47eSScott Wood { 1528dfd4d47eSScott Wood set_bits(tsr_bits, &vcpu->arch.tsr); 1529dfd4d47eSScott Wood smp_wmb(); 1530dfd4d47eSScott Wood kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu); 1531dfd4d47eSScott Wood kvm_vcpu_kick(vcpu); 1532dfd4d47eSScott Wood } 1533dfd4d47eSScott Wood 1534dfd4d47eSScott Wood void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits) 1535dfd4d47eSScott Wood { 1536dfd4d47eSScott Wood clear_bits(tsr_bits, &vcpu->arch.tsr); 1537f61c94bbSBharat Bhushan 1538f61c94bbSBharat Bhushan /* 1539f61c94bbSBharat Bhushan * We may have stopped the watchdog due to 1540f61c94bbSBharat Bhushan * being stuck on final expiration. 1541f61c94bbSBharat Bhushan */ 1542f61c94bbSBharat Bhushan if (tsr_bits & (TSR_ENW | TSR_WIS)) 1543f61c94bbSBharat Bhushan arm_next_watchdog(vcpu); 1544f61c94bbSBharat Bhushan 1545dfd4d47eSScott Wood update_timer_ints(vcpu); 1546dfd4d47eSScott Wood } 1547dfd4d47eSScott Wood 1548dfd4d47eSScott Wood void kvmppc_decrementer_func(unsigned long data) 1549dfd4d47eSScott Wood { 1550dfd4d47eSScott Wood struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; 1551dfd4d47eSScott Wood 155221bd000aSBharat Bhushan if (vcpu->arch.tcr & TCR_ARE) { 155321bd000aSBharat Bhushan vcpu->arch.dec = vcpu->arch.decar; 155421bd000aSBharat Bhushan kvmppc_emulate_dec(vcpu); 155521bd000aSBharat Bhushan } 155621bd000aSBharat Bhushan 1557dfd4d47eSScott Wood kvmppc_set_tsr_bits(vcpu, TSR_DIS); 1558dfd4d47eSScott Wood } 1559dfd4d47eSScott Wood 156094fa9d99SScott Wood void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 156194fa9d99SScott Wood { 1562a47d72f3SPaul Mackerras vcpu->cpu = smp_processor_id(); 1563d30f6e48SScott Wood current->thread.kvm_vcpu = vcpu; 156494fa9d99SScott Wood } 156594fa9d99SScott Wood 156694fa9d99SScott Wood void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu) 156794fa9d99SScott Wood { 1568d30f6e48SScott Wood current->thread.kvm_vcpu = NULL; 1569a47d72f3SPaul Mackerras vcpu->cpu = -1; 157094fa9d99SScott Wood } 157194fa9d99SScott Wood 15722986b8c7SStephen Rothwell int __init kvmppc_booke_init(void) 1573d9fbd03dSHollis Blanchard { 1574d30f6e48SScott Wood #ifndef CONFIG_KVM_BOOKE_HV 1575d9fbd03dSHollis Blanchard unsigned long ivor[16]; 1576d9fbd03dSHollis Blanchard unsigned long max_ivor = 0; 1577d9fbd03dSHollis Blanchard int i; 1578d9fbd03dSHollis Blanchard 1579d9fbd03dSHollis Blanchard /* We install our own exception handlers by hijacking IVPR. IVPR must 1580d9fbd03dSHollis Blanchard * be 16-bit aligned, so we need a 64KB allocation. */ 1581d9fbd03dSHollis Blanchard kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO, 1582d9fbd03dSHollis Blanchard VCPU_SIZE_ORDER); 1583d9fbd03dSHollis Blanchard if (!kvmppc_booke_handlers) 1584d9fbd03dSHollis Blanchard return -ENOMEM; 1585d9fbd03dSHollis Blanchard 1586d9fbd03dSHollis Blanchard /* XXX make sure our handlers are smaller than Linux's */ 1587d9fbd03dSHollis Blanchard 1588d9fbd03dSHollis Blanchard /* Copy our interrupt handlers to match host IVORs. That way we don't 1589d9fbd03dSHollis Blanchard * have to swap the IVORs on every guest/host transition. */ 1590d9fbd03dSHollis Blanchard ivor[0] = mfspr(SPRN_IVOR0); 1591d9fbd03dSHollis Blanchard ivor[1] = mfspr(SPRN_IVOR1); 1592d9fbd03dSHollis Blanchard ivor[2] = mfspr(SPRN_IVOR2); 1593d9fbd03dSHollis Blanchard ivor[3] = mfspr(SPRN_IVOR3); 1594d9fbd03dSHollis Blanchard ivor[4] = mfspr(SPRN_IVOR4); 1595d9fbd03dSHollis Blanchard ivor[5] = mfspr(SPRN_IVOR5); 1596d9fbd03dSHollis Blanchard ivor[6] = mfspr(SPRN_IVOR6); 1597d9fbd03dSHollis Blanchard ivor[7] = mfspr(SPRN_IVOR7); 1598d9fbd03dSHollis Blanchard ivor[8] = mfspr(SPRN_IVOR8); 1599d9fbd03dSHollis Blanchard ivor[9] = mfspr(SPRN_IVOR9); 1600d9fbd03dSHollis Blanchard ivor[10] = mfspr(SPRN_IVOR10); 1601d9fbd03dSHollis Blanchard ivor[11] = mfspr(SPRN_IVOR11); 1602d9fbd03dSHollis Blanchard ivor[12] = mfspr(SPRN_IVOR12); 1603d9fbd03dSHollis Blanchard ivor[13] = mfspr(SPRN_IVOR13); 1604d9fbd03dSHollis Blanchard ivor[14] = mfspr(SPRN_IVOR14); 1605d9fbd03dSHollis Blanchard ivor[15] = mfspr(SPRN_IVOR15); 1606d9fbd03dSHollis Blanchard 1607d9fbd03dSHollis Blanchard for (i = 0; i < 16; i++) { 1608d9fbd03dSHollis Blanchard if (ivor[i] > max_ivor) 1609d9fbd03dSHollis Blanchard max_ivor = ivor[i]; 1610d9fbd03dSHollis Blanchard 1611d9fbd03dSHollis Blanchard memcpy((void *)kvmppc_booke_handlers + ivor[i], 1612d9fbd03dSHollis Blanchard kvmppc_handlers_start + i * kvmppc_handler_len, 1613d9fbd03dSHollis Blanchard kvmppc_handler_len); 1614d9fbd03dSHollis Blanchard } 1615d9fbd03dSHollis Blanchard flush_icache_range(kvmppc_booke_handlers, 1616d9fbd03dSHollis Blanchard kvmppc_booke_handlers + max_ivor + kvmppc_handler_len); 1617d30f6e48SScott Wood #endif /* !BOOKE_HV */ 1618db93f574SHollis Blanchard return 0; 1619d9fbd03dSHollis Blanchard } 1620d9fbd03dSHollis Blanchard 1621db93f574SHollis Blanchard void __exit kvmppc_booke_exit(void) 1622d9fbd03dSHollis Blanchard { 1623d9fbd03dSHollis Blanchard free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER); 1624d9fbd03dSHollis Blanchard kvm_exit(); 1625d9fbd03dSHollis Blanchard } 1626