Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License, version 2, as |
| 4 | * published by the Free Software Foundation. |
| 5 | * |
| 6 | * This program is distributed in the hope that it will be useful, |
| 7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 9 | * GNU General Public License for more details. |
| 10 | * |
| 11 | * You should have received a copy of the GNU General Public License |
| 12 | * along with this program; if not, write to the Free Software |
| 13 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 14 | * |
| 15 | * Copyright IBM Corp. 2007 |
Scott Wood | 4cd35f6 | 2011-06-14 18:34:31 -0500 | [diff] [blame] | 16 | * Copyright 2010-2011 Freescale Semiconductor, Inc. |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 17 | * |
| 18 | * Authors: Hollis Blanchard <hollisb@us.ibm.com> |
| 19 | * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com> |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 20 | * Scott Wood <scottwood@freescale.com> |
| 21 | * Varun Sethi <varun.sethi@freescale.com> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/err.h> |
| 26 | #include <linux/kvm_host.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 27 | #include <linux/gfp.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/vmalloc.h> |
| 30 | #include <linux/fs.h> |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 31 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 32 | #include <asm/cputable.h> |
| 33 | #include <asm/uaccess.h> |
| 34 | #include <asm/kvm_ppc.h> |
Hollis Blanchard | d9fbd03 | 2008-11-05 09:36:13 -0600 | [diff] [blame] | 35 | #include <asm/cacheflush.h> |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 36 | #include <asm/dbell.h> |
| 37 | #include <asm/hw_irq.h> |
| 38 | #include <asm/irq.h> |
Mihai Caraman | b50df19 | 2012-10-11 06:13:19 +0000 | [diff] [blame] | 39 | #include <asm/time.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 40 | |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 41 | #include "timing.h" |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 42 | #include "booke.h" |
Aneesh Kumar K.V | dba291f | 2013-10-07 22:17:58 +0530 | [diff] [blame] | 43 | |
| 44 | #define CREATE_TRACE_POINTS |
| 45 | #include "trace_booke.h" |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 46 | |
Hollis Blanchard | d9fbd03 | 2008-11-05 09:36:13 -0600 | [diff] [blame] | 47 | unsigned long kvmppc_booke_handlers; |
| 48 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 49 | #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM |
| 50 | #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU |
| 51 | |
| 52 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 53 | { "mmio", VCPU_STAT(mmio_exits) }, |
| 54 | { "dcr", VCPU_STAT(dcr_exits) }, |
| 55 | { "sig", VCPU_STAT(signal_exits) }, |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 56 | { "itlb_r", VCPU_STAT(itlb_real_miss_exits) }, |
| 57 | { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) }, |
| 58 | { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) }, |
| 59 | { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) }, |
| 60 | { "sysc", VCPU_STAT(syscall_exits) }, |
| 61 | { "isi", VCPU_STAT(isi_exits) }, |
| 62 | { "dsi", VCPU_STAT(dsi_exits) }, |
| 63 | { "inst_emu", VCPU_STAT(emulated_inst_exits) }, |
| 64 | { "dec", VCPU_STAT(dec_exits) }, |
| 65 | { "ext_intr", VCPU_STAT(ext_intr_exits) }, |
Hollis Blanchard | 45c5eb6 | 2008-04-25 17:55:49 -0500 | [diff] [blame] | 66 | { "halt_wakeup", VCPU_STAT(halt_wakeup) }, |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 67 | { "doorbell", VCPU_STAT(dbell_exits) }, |
| 68 | { "guest doorbell", VCPU_STAT(gdbell_exits) }, |
Alexander Graf | cf1c5ca | 2012-08-01 12:56:51 +0200 | [diff] [blame] | 69 | { "remote_tlb_flush", VM_STAT(remote_tlb_flush) }, |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 70 | { NULL } |
| 71 | }; |
| 72 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 73 | /* TODO: use vcpu_printf() */ |
| 74 | void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu) |
| 75 | { |
| 76 | int i; |
| 77 | |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 78 | printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr); |
Hollis Blanchard | 5cf8ca2 | 2008-11-05 09:36:19 -0600 | [diff] [blame] | 79 | printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr); |
Alexander Graf | de7906c | 2010-07-29 14:47:46 +0200 | [diff] [blame] | 80 | printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0, |
| 81 | vcpu->arch.shared->srr1); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 82 | |
| 83 | printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions); |
| 84 | |
| 85 | for (i = 0; i < 32; i += 4) { |
Hollis Blanchard | 5cf8ca2 | 2008-11-05 09:36:19 -0600 | [diff] [blame] | 86 | printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i, |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 87 | kvmppc_get_gpr(vcpu, i), |
| 88 | kvmppc_get_gpr(vcpu, i+1), |
| 89 | kvmppc_get_gpr(vcpu, i+2), |
| 90 | kvmppc_get_gpr(vcpu, i+3)); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Scott Wood | 4cd35f6 | 2011-06-14 18:34:31 -0500 | [diff] [blame] | 94 | #ifdef CONFIG_SPE |
| 95 | void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu) |
| 96 | { |
| 97 | preempt_disable(); |
| 98 | enable_kernel_spe(); |
| 99 | kvmppc_save_guest_spe(vcpu); |
| 100 | vcpu->arch.shadow_msr &= ~MSR_SPE; |
| 101 | preempt_enable(); |
| 102 | } |
| 103 | |
| 104 | static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu) |
| 105 | { |
| 106 | preempt_disable(); |
| 107 | enable_kernel_spe(); |
| 108 | kvmppc_load_guest_spe(vcpu); |
| 109 | vcpu->arch.shadow_msr |= MSR_SPE; |
| 110 | preempt_enable(); |
| 111 | } |
| 112 | |
| 113 | static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu) |
| 114 | { |
| 115 | if (vcpu->arch.shared->msr & MSR_SPE) { |
| 116 | if (!(vcpu->arch.shadow_msr & MSR_SPE)) |
| 117 | kvmppc_vcpu_enable_spe(vcpu); |
| 118 | } else if (vcpu->arch.shadow_msr & MSR_SPE) { |
| 119 | kvmppc_vcpu_disable_spe(vcpu); |
| 120 | } |
| 121 | } |
| 122 | #else |
| 123 | static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu) |
| 124 | { |
| 125 | } |
| 126 | #endif |
| 127 | |
Alexander Graf | 7a08c27 | 2012-08-16 13:10:16 +0200 | [diff] [blame] | 128 | static void kvmppc_vcpu_sync_fpu(struct kvm_vcpu *vcpu) |
| 129 | { |
| 130 | #if defined(CONFIG_PPC_FPU) && !defined(CONFIG_KVM_BOOKE_HV) |
| 131 | /* We always treat the FP bit as enabled from the host |
| 132 | perspective, so only need to adjust the shadow MSR */ |
| 133 | vcpu->arch.shadow_msr &= ~MSR_FP; |
| 134 | vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_FP; |
| 135 | #endif |
| 136 | } |
| 137 | |
Bharat Bhushan | ce11e48 | 2013-07-04 12:27:47 +0530 | [diff] [blame] | 138 | static void kvmppc_vcpu_sync_debug(struct kvm_vcpu *vcpu) |
| 139 | { |
| 140 | /* Synchronize guest's desire to get debug interrupts into shadow MSR */ |
| 141 | #ifndef CONFIG_KVM_BOOKE_HV |
| 142 | vcpu->arch.shadow_msr &= ~MSR_DE; |
| 143 | vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_DE; |
| 144 | #endif |
| 145 | |
| 146 | /* Force enable debug interrupts when user space wants to debug */ |
| 147 | if (vcpu->guest_debug) { |
| 148 | #ifdef CONFIG_KVM_BOOKE_HV |
| 149 | /* |
| 150 | * Since there is no shadow MSR, sync MSR_DE into the guest |
| 151 | * visible MSR. |
| 152 | */ |
| 153 | vcpu->arch.shared->msr |= MSR_DE; |
| 154 | #else |
| 155 | vcpu->arch.shadow_msr |= MSR_DE; |
| 156 | vcpu->arch.shared->msr &= ~MSR_DE; |
| 157 | #endif |
| 158 | } |
| 159 | } |
| 160 | |
Liu Yu | dd9ebf1f | 2011-06-14 18:35:14 -0500 | [diff] [blame] | 161 | /* |
| 162 | * Helper function for "full" MSR writes. No need to call this if only |
| 163 | * EE/CE/ME/DE/RI are changing. |
| 164 | */ |
Scott Wood | 4cd35f6 | 2011-06-14 18:34:31 -0500 | [diff] [blame] | 165 | void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr) |
| 166 | { |
Liu Yu | dd9ebf1f | 2011-06-14 18:35:14 -0500 | [diff] [blame] | 167 | u32 old_msr = vcpu->arch.shared->msr; |
Scott Wood | 4cd35f6 | 2011-06-14 18:34:31 -0500 | [diff] [blame] | 168 | |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 169 | #ifdef CONFIG_KVM_BOOKE_HV |
| 170 | new_msr |= MSR_GS; |
| 171 | #endif |
| 172 | |
Scott Wood | 4cd35f6 | 2011-06-14 18:34:31 -0500 | [diff] [blame] | 173 | vcpu->arch.shared->msr = new_msr; |
| 174 | |
Liu Yu | dd9ebf1f | 2011-06-14 18:35:14 -0500 | [diff] [blame] | 175 | kvmppc_mmu_msr_notify(vcpu, old_msr); |
Scott Wood | 4cd35f6 | 2011-06-14 18:34:31 -0500 | [diff] [blame] | 176 | kvmppc_vcpu_sync_spe(vcpu); |
Alexander Graf | 7a08c27 | 2012-08-16 13:10:16 +0200 | [diff] [blame] | 177 | kvmppc_vcpu_sync_fpu(vcpu); |
Bharat Bhushan | ce11e48 | 2013-07-04 12:27:47 +0530 | [diff] [blame] | 178 | kvmppc_vcpu_sync_debug(vcpu); |
Scott Wood | 4cd35f6 | 2011-06-14 18:34:31 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 181 | static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu, |
| 182 | unsigned int priority) |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 183 | { |
Alexander Graf | 6346046 | 2012-08-08 00:44:52 +0200 | [diff] [blame] | 184 | trace_kvm_booke_queue_irqprio(vcpu, priority); |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 185 | set_bit(priority, &vcpu->arch.pending_exceptions); |
| 186 | } |
| 187 | |
Liu Yu | daf5e27 | 2010-02-02 19:44:35 +0800 | [diff] [blame] | 188 | static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu, |
| 189 | ulong dear_flags, ulong esr_flags) |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 190 | { |
Liu Yu | daf5e27 | 2010-02-02 19:44:35 +0800 | [diff] [blame] | 191 | vcpu->arch.queued_dear = dear_flags; |
| 192 | vcpu->arch.queued_esr = esr_flags; |
| 193 | kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS); |
| 194 | } |
| 195 | |
| 196 | static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu, |
| 197 | ulong dear_flags, ulong esr_flags) |
| 198 | { |
| 199 | vcpu->arch.queued_dear = dear_flags; |
| 200 | vcpu->arch.queued_esr = esr_flags; |
| 201 | kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE); |
| 202 | } |
| 203 | |
| 204 | static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu, |
| 205 | ulong esr_flags) |
| 206 | { |
| 207 | vcpu->arch.queued_esr = esr_flags; |
| 208 | kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE); |
| 209 | } |
| 210 | |
Alexander Graf | 011da89 | 2013-01-31 14:17:38 +0100 | [diff] [blame] | 211 | static void kvmppc_core_queue_alignment(struct kvm_vcpu *vcpu, ulong dear_flags, |
| 212 | ulong esr_flags) |
| 213 | { |
| 214 | vcpu->arch.queued_dear = dear_flags; |
| 215 | vcpu->arch.queued_esr = esr_flags; |
| 216 | kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALIGNMENT); |
| 217 | } |
| 218 | |
Liu Yu | daf5e27 | 2010-02-02 19:44:35 +0800 | [diff] [blame] | 219 | void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags) |
| 220 | { |
| 221 | vcpu->arch.queued_esr = esr_flags; |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 222 | kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM); |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu) |
| 226 | { |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 227 | kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER); |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu) |
| 231 | { |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 232 | return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions); |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 233 | } |
| 234 | |
Alexander Graf | 7706664d | 2009-12-21 20:21:24 +0100 | [diff] [blame] | 235 | void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu) |
| 236 | { |
| 237 | clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions); |
| 238 | } |
| 239 | |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 240 | void kvmppc_core_queue_external(struct kvm_vcpu *vcpu, |
| 241 | struct kvm_interrupt *irq) |
| 242 | { |
Alexander Graf | c5335f1 | 2010-08-30 14:03:24 +0200 | [diff] [blame] | 243 | unsigned int prio = BOOKE_IRQPRIO_EXTERNAL; |
| 244 | |
| 245 | if (irq->irq == KVM_INTERRUPT_SET_LEVEL) |
| 246 | prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL; |
| 247 | |
| 248 | kvmppc_booke_queue_irqprio(vcpu, prio); |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 249 | } |
| 250 | |
Paul Mackerras | 4fe27d2 | 2013-02-14 14:00:25 +0000 | [diff] [blame] | 251 | void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu) |
Alexander Graf | 4496f97 | 2010-04-07 10:03:25 +0200 | [diff] [blame] | 252 | { |
| 253 | clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions); |
Alexander Graf | c5335f1 | 2010-08-30 14:03:24 +0200 | [diff] [blame] | 254 | clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions); |
Alexander Graf | 4496f97 | 2010-04-07 10:03:25 +0200 | [diff] [blame] | 255 | } |
| 256 | |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 257 | static void kvmppc_core_queue_watchdog(struct kvm_vcpu *vcpu) |
| 258 | { |
| 259 | kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_WATCHDOG); |
| 260 | } |
| 261 | |
| 262 | static void kvmppc_core_dequeue_watchdog(struct kvm_vcpu *vcpu) |
| 263 | { |
| 264 | clear_bit(BOOKE_IRQPRIO_WATCHDOG, &vcpu->arch.pending_exceptions); |
| 265 | } |
| 266 | |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 267 | static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1) |
| 268 | { |
| 269 | #ifdef CONFIG_KVM_BOOKE_HV |
| 270 | mtspr(SPRN_GSRR0, srr0); |
| 271 | mtspr(SPRN_GSRR1, srr1); |
| 272 | #else |
| 273 | vcpu->arch.shared->srr0 = srr0; |
| 274 | vcpu->arch.shared->srr1 = srr1; |
| 275 | #endif |
| 276 | } |
| 277 | |
| 278 | static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1) |
| 279 | { |
| 280 | vcpu->arch.csrr0 = srr0; |
| 281 | vcpu->arch.csrr1 = srr1; |
| 282 | } |
| 283 | |
| 284 | static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1) |
| 285 | { |
| 286 | if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) { |
| 287 | vcpu->arch.dsrr0 = srr0; |
| 288 | vcpu->arch.dsrr1 = srr1; |
| 289 | } else { |
| 290 | set_guest_csrr(vcpu, srr0, srr1); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1) |
| 295 | { |
| 296 | vcpu->arch.mcsrr0 = srr0; |
| 297 | vcpu->arch.mcsrr1 = srr1; |
| 298 | } |
| 299 | |
| 300 | static unsigned long get_guest_dear(struct kvm_vcpu *vcpu) |
| 301 | { |
| 302 | #ifdef CONFIG_KVM_BOOKE_HV |
| 303 | return mfspr(SPRN_GDEAR); |
| 304 | #else |
| 305 | return vcpu->arch.shared->dar; |
| 306 | #endif |
| 307 | } |
| 308 | |
| 309 | static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear) |
| 310 | { |
| 311 | #ifdef CONFIG_KVM_BOOKE_HV |
| 312 | mtspr(SPRN_GDEAR, dear); |
| 313 | #else |
| 314 | vcpu->arch.shared->dar = dear; |
| 315 | #endif |
| 316 | } |
| 317 | |
| 318 | static unsigned long get_guest_esr(struct kvm_vcpu *vcpu) |
| 319 | { |
| 320 | #ifdef CONFIG_KVM_BOOKE_HV |
| 321 | return mfspr(SPRN_GESR); |
| 322 | #else |
| 323 | return vcpu->arch.shared->esr; |
| 324 | #endif |
| 325 | } |
| 326 | |
| 327 | static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr) |
| 328 | { |
| 329 | #ifdef CONFIG_KVM_BOOKE_HV |
| 330 | mtspr(SPRN_GESR, esr); |
| 331 | #else |
| 332 | vcpu->arch.shared->esr = esr; |
| 333 | #endif |
| 334 | } |
| 335 | |
Alexander Graf | 324b3e6 | 2013-01-04 18:28:51 +0100 | [diff] [blame] | 336 | static unsigned long get_guest_epr(struct kvm_vcpu *vcpu) |
| 337 | { |
| 338 | #ifdef CONFIG_KVM_BOOKE_HV |
| 339 | return mfspr(SPRN_GEPR); |
| 340 | #else |
| 341 | return vcpu->arch.epr; |
| 342 | #endif |
| 343 | } |
| 344 | |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 345 | /* Deliver the interrupt of the corresponding priority, if possible. */ |
| 346 | static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu, |
| 347 | unsigned int priority) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 348 | { |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 349 | int allowed = 0; |
Alexander Graf | 79300f8 | 2012-02-15 19:12:29 +0000 | [diff] [blame] | 350 | ulong msr_mask = 0; |
Alexander Graf | 1c81063 | 2013-01-04 18:12:48 +0100 | [diff] [blame] | 351 | bool update_esr = false, update_dear = false, update_epr = false; |
Alexander Graf | 5c6cedf | 2010-07-29 14:47:49 +0200 | [diff] [blame] | 352 | ulong crit_raw = vcpu->arch.shared->critical; |
| 353 | ulong crit_r1 = kvmppc_get_gpr(vcpu, 1); |
| 354 | bool crit; |
Alexander Graf | c5335f1 | 2010-08-30 14:03:24 +0200 | [diff] [blame] | 355 | bool keep_irq = false; |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 356 | enum int_class int_class; |
Mihai Caraman | 95e90b4 | 2012-10-11 06:13:26 +0000 | [diff] [blame] | 357 | ulong new_msr = vcpu->arch.shared->msr; |
Alexander Graf | 5c6cedf | 2010-07-29 14:47:49 +0200 | [diff] [blame] | 358 | |
| 359 | /* Truncate crit indicators in 32 bit mode */ |
| 360 | if (!(vcpu->arch.shared->msr & MSR_SF)) { |
| 361 | crit_raw &= 0xffffffff; |
| 362 | crit_r1 &= 0xffffffff; |
| 363 | } |
| 364 | |
| 365 | /* Critical section when crit == r1 */ |
| 366 | crit = (crit_raw == crit_r1); |
| 367 | /* ... and we're in supervisor mode */ |
| 368 | crit = crit && !(vcpu->arch.shared->msr & MSR_PR); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 369 | |
Alexander Graf | c5335f1 | 2010-08-30 14:03:24 +0200 | [diff] [blame] | 370 | if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) { |
| 371 | priority = BOOKE_IRQPRIO_EXTERNAL; |
| 372 | keep_irq = true; |
| 373 | } |
| 374 | |
Scott Wood | 5df554ad | 2013-04-12 14:08:46 +0000 | [diff] [blame] | 375 | if ((priority == BOOKE_IRQPRIO_EXTERNAL) && vcpu->arch.epr_flags) |
Alexander Graf | 1c81063 | 2013-01-04 18:12:48 +0100 | [diff] [blame] | 376 | update_epr = true; |
| 377 | |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 378 | switch (priority) { |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 379 | case BOOKE_IRQPRIO_DTLB_MISS: |
Liu Yu | daf5e27 | 2010-02-02 19:44:35 +0800 | [diff] [blame] | 380 | case BOOKE_IRQPRIO_DATA_STORAGE: |
Alexander Graf | 011da89 | 2013-01-31 14:17:38 +0100 | [diff] [blame] | 381 | case BOOKE_IRQPRIO_ALIGNMENT: |
Liu Yu | daf5e27 | 2010-02-02 19:44:35 +0800 | [diff] [blame] | 382 | update_dear = true; |
| 383 | /* fall through */ |
| 384 | case BOOKE_IRQPRIO_INST_STORAGE: |
| 385 | case BOOKE_IRQPRIO_PROGRAM: |
| 386 | update_esr = true; |
| 387 | /* fall through */ |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 388 | case BOOKE_IRQPRIO_ITLB_MISS: |
| 389 | case BOOKE_IRQPRIO_SYSCALL: |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 390 | case BOOKE_IRQPRIO_FP_UNAVAIL: |
Hollis Blanchard | bb3a8a1 | 2009-01-03 16:23:13 -0600 | [diff] [blame] | 391 | case BOOKE_IRQPRIO_SPE_UNAVAIL: |
| 392 | case BOOKE_IRQPRIO_SPE_FP_DATA: |
| 393 | case BOOKE_IRQPRIO_SPE_FP_ROUND: |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 394 | case BOOKE_IRQPRIO_AP_UNAVAIL: |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 395 | allowed = 1; |
Alexander Graf | 79300f8 | 2012-02-15 19:12:29 +0000 | [diff] [blame] | 396 | msr_mask = MSR_CE | MSR_ME | MSR_DE; |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 397 | int_class = INT_CLASS_NONCRIT; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 398 | break; |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 399 | case BOOKE_IRQPRIO_WATCHDOG: |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 400 | case BOOKE_IRQPRIO_CRITICAL: |
Alexander Graf | 4ab9691 | 2012-02-15 13:28:48 +0000 | [diff] [blame] | 401 | case BOOKE_IRQPRIO_DBELL_CRIT: |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 402 | allowed = vcpu->arch.shared->msr & MSR_CE; |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 403 | allowed = allowed && !crit; |
Alexander Graf | 79300f8 | 2012-02-15 19:12:29 +0000 | [diff] [blame] | 404 | msr_mask = MSR_ME; |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 405 | int_class = INT_CLASS_CRIT; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 406 | break; |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 407 | case BOOKE_IRQPRIO_MACHINE_CHECK: |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 408 | allowed = vcpu->arch.shared->msr & MSR_ME; |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 409 | allowed = allowed && !crit; |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 410 | int_class = INT_CLASS_MC; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 411 | break; |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 412 | case BOOKE_IRQPRIO_DECREMENTER: |
| 413 | case BOOKE_IRQPRIO_FIT: |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 414 | keep_irq = true; |
| 415 | /* fall through */ |
| 416 | case BOOKE_IRQPRIO_EXTERNAL: |
Alexander Graf | 4ab9691 | 2012-02-15 13:28:48 +0000 | [diff] [blame] | 417 | case BOOKE_IRQPRIO_DBELL: |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 418 | allowed = vcpu->arch.shared->msr & MSR_EE; |
Alexander Graf | 5c6cedf | 2010-07-29 14:47:49 +0200 | [diff] [blame] | 419 | allowed = allowed && !crit; |
Alexander Graf | 79300f8 | 2012-02-15 19:12:29 +0000 | [diff] [blame] | 420 | msr_mask = MSR_CE | MSR_ME | MSR_DE; |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 421 | int_class = INT_CLASS_NONCRIT; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 422 | break; |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 423 | case BOOKE_IRQPRIO_DEBUG: |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 424 | allowed = vcpu->arch.shared->msr & MSR_DE; |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 425 | allowed = allowed && !crit; |
Alexander Graf | 79300f8 | 2012-02-15 19:12:29 +0000 | [diff] [blame] | 426 | msr_mask = MSR_ME; |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 427 | int_class = INT_CLASS_CRIT; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 428 | break; |
| 429 | } |
| 430 | |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 431 | if (allowed) { |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 432 | switch (int_class) { |
| 433 | case INT_CLASS_NONCRIT: |
| 434 | set_guest_srr(vcpu, vcpu->arch.pc, |
| 435 | vcpu->arch.shared->msr); |
| 436 | break; |
| 437 | case INT_CLASS_CRIT: |
| 438 | set_guest_csrr(vcpu, vcpu->arch.pc, |
| 439 | vcpu->arch.shared->msr); |
| 440 | break; |
| 441 | case INT_CLASS_DBG: |
| 442 | set_guest_dsrr(vcpu, vcpu->arch.pc, |
| 443 | vcpu->arch.shared->msr); |
| 444 | break; |
| 445 | case INT_CLASS_MC: |
| 446 | set_guest_mcsrr(vcpu, vcpu->arch.pc, |
| 447 | vcpu->arch.shared->msr); |
| 448 | break; |
| 449 | } |
| 450 | |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 451 | vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority]; |
Liu Yu | daf5e27 | 2010-02-02 19:44:35 +0800 | [diff] [blame] | 452 | if (update_esr == true) |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 453 | set_guest_esr(vcpu, vcpu->arch.queued_esr); |
Liu Yu | daf5e27 | 2010-02-02 19:44:35 +0800 | [diff] [blame] | 454 | if (update_dear == true) |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 455 | set_guest_dear(vcpu, vcpu->arch.queued_dear); |
Scott Wood | 5df554ad | 2013-04-12 14:08:46 +0000 | [diff] [blame] | 456 | if (update_epr == true) { |
| 457 | if (vcpu->arch.epr_flags & KVMPPC_EPR_USER) |
| 458 | kvm_make_request(KVM_REQ_EPR_EXIT, vcpu); |
Scott Wood | eb1e4f4 | 2013-04-12 14:08:47 +0000 | [diff] [blame] | 459 | else if (vcpu->arch.epr_flags & KVMPPC_EPR_KERNEL) { |
| 460 | BUG_ON(vcpu->arch.irq_type != KVMPPC_IRQ_MPIC); |
| 461 | kvmppc_mpic_set_epr(vcpu); |
| 462 | } |
Scott Wood | 5df554ad | 2013-04-12 14:08:46 +0000 | [diff] [blame] | 463 | } |
Mihai Caraman | 95e90b4 | 2012-10-11 06:13:26 +0000 | [diff] [blame] | 464 | |
| 465 | new_msr &= msr_mask; |
| 466 | #if defined(CONFIG_64BIT) |
| 467 | if (vcpu->arch.epcr & SPRN_EPCR_ICM) |
| 468 | new_msr |= MSR_CM; |
| 469 | #endif |
| 470 | kvmppc_set_msr(vcpu, new_msr); |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 471 | |
Alexander Graf | c5335f1 | 2010-08-30 14:03:24 +0200 | [diff] [blame] | 472 | if (!keep_irq) |
| 473 | clear_bit(priority, &vcpu->arch.pending_exceptions); |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 474 | } |
| 475 | |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 476 | #ifdef CONFIG_KVM_BOOKE_HV |
| 477 | /* |
| 478 | * If an interrupt is pending but masked, raise a guest doorbell |
| 479 | * so that we are notified when the guest enables the relevant |
| 480 | * MSR bit. |
| 481 | */ |
| 482 | if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE) |
| 483 | kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT); |
| 484 | if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE) |
| 485 | kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT); |
| 486 | if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK) |
| 487 | kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC); |
| 488 | #endif |
| 489 | |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 490 | return allowed; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 491 | } |
| 492 | |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 493 | /* |
| 494 | * Return the number of jiffies until the next timeout. If the timeout is |
| 495 | * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA |
| 496 | * because the larger value can break the timer APIs. |
| 497 | */ |
| 498 | static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu) |
| 499 | { |
| 500 | u64 tb, wdt_tb, wdt_ticks = 0; |
| 501 | u64 nr_jiffies = 0; |
| 502 | u32 period = TCR_GET_WP(vcpu->arch.tcr); |
| 503 | |
| 504 | wdt_tb = 1ULL << (63 - period); |
| 505 | tb = get_tb(); |
| 506 | /* |
| 507 | * The watchdog timeout will hapeen when TB bit corresponding |
| 508 | * to watchdog will toggle from 0 to 1. |
| 509 | */ |
| 510 | if (tb & wdt_tb) |
| 511 | wdt_ticks = wdt_tb; |
| 512 | |
| 513 | wdt_ticks += wdt_tb - (tb & (wdt_tb - 1)); |
| 514 | |
| 515 | /* Convert timebase ticks to jiffies */ |
| 516 | nr_jiffies = wdt_ticks; |
| 517 | |
| 518 | if (do_div(nr_jiffies, tb_ticks_per_jiffy)) |
| 519 | nr_jiffies++; |
| 520 | |
| 521 | return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA); |
| 522 | } |
| 523 | |
| 524 | static void arm_next_watchdog(struct kvm_vcpu *vcpu) |
| 525 | { |
| 526 | unsigned long nr_jiffies; |
| 527 | unsigned long flags; |
| 528 | |
| 529 | /* |
| 530 | * If TSR_ENW and TSR_WIS are not set then no need to exit to |
| 531 | * userspace, so clear the KVM_REQ_WATCHDOG request. |
| 532 | */ |
| 533 | if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS)) |
| 534 | clear_bit(KVM_REQ_WATCHDOG, &vcpu->requests); |
| 535 | |
| 536 | spin_lock_irqsave(&vcpu->arch.wdt_lock, flags); |
| 537 | nr_jiffies = watchdog_next_timeout(vcpu); |
| 538 | /* |
| 539 | * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA |
| 540 | * then do not run the watchdog timer as this can break timer APIs. |
| 541 | */ |
| 542 | if (nr_jiffies < NEXT_TIMER_MAX_DELTA) |
| 543 | mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies); |
| 544 | else |
| 545 | del_timer(&vcpu->arch.wdt_timer); |
| 546 | spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags); |
| 547 | } |
| 548 | |
| 549 | void kvmppc_watchdog_func(unsigned long data) |
| 550 | { |
| 551 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; |
| 552 | u32 tsr, new_tsr; |
| 553 | int final; |
| 554 | |
| 555 | do { |
| 556 | new_tsr = tsr = vcpu->arch.tsr; |
| 557 | final = 0; |
| 558 | |
| 559 | /* Time out event */ |
| 560 | if (tsr & TSR_ENW) { |
| 561 | if (tsr & TSR_WIS) |
| 562 | final = 1; |
| 563 | else |
| 564 | new_tsr = tsr | TSR_WIS; |
| 565 | } else { |
| 566 | new_tsr = tsr | TSR_ENW; |
| 567 | } |
| 568 | } while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr); |
| 569 | |
| 570 | if (new_tsr & TSR_WIS) { |
| 571 | smp_wmb(); |
| 572 | kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu); |
| 573 | kvm_vcpu_kick(vcpu); |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * If this is final watchdog expiry and some action is required |
| 578 | * then exit to userspace. |
| 579 | */ |
| 580 | if (final && (vcpu->arch.tcr & TCR_WRC_MASK) && |
| 581 | vcpu->arch.watchdog_enabled) { |
| 582 | smp_wmb(); |
| 583 | kvm_make_request(KVM_REQ_WATCHDOG, vcpu); |
| 584 | kvm_vcpu_kick(vcpu); |
| 585 | } |
| 586 | |
| 587 | /* |
| 588 | * Stop running the watchdog timer after final expiration to |
| 589 | * prevent the host from being flooded with timers if the |
| 590 | * guest sets a short period. |
| 591 | * Timers will resume when TSR/TCR is updated next time. |
| 592 | */ |
| 593 | if (!final) |
| 594 | arm_next_watchdog(vcpu); |
| 595 | } |
| 596 | |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 597 | static void update_timer_ints(struct kvm_vcpu *vcpu) |
| 598 | { |
| 599 | if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS)) |
| 600 | kvmppc_core_queue_dec(vcpu); |
| 601 | else |
| 602 | kvmppc_core_dequeue_dec(vcpu); |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 603 | |
| 604 | if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS)) |
| 605 | kvmppc_core_queue_watchdog(vcpu); |
| 606 | else |
| 607 | kvmppc_core_dequeue_watchdog(vcpu); |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Scott Wood | c59a6a3 | 2011-11-08 18:23:25 -0600 | [diff] [blame] | 610 | static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 611 | { |
| 612 | unsigned long *pending = &vcpu->arch.pending_exceptions; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 613 | unsigned int priority; |
| 614 | |
Hollis Blanchard | 9ab8084 | 2008-11-05 09:36:22 -0600 | [diff] [blame] | 615 | priority = __ffs(*pending); |
Alexander Graf | 8b3a00f | 2012-02-16 14:12:46 +0000 | [diff] [blame] | 616 | while (priority < BOOKE_IRQPRIO_MAX) { |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 617 | if (kvmppc_booke_irqprio_deliver(vcpu, priority)) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 618 | break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 619 | |
| 620 | priority = find_next_bit(pending, |
| 621 | BITS_PER_BYTE * sizeof(*pending), |
| 622 | priority + 1); |
| 623 | } |
Alexander Graf | 90bba35 | 2010-07-29 14:47:51 +0200 | [diff] [blame] | 624 | |
| 625 | /* Tell the guest about our interrupt status */ |
Scott Wood | 29ac26e | 2011-11-08 18:23:27 -0600 | [diff] [blame] | 626 | vcpu->arch.shared->int_pending = !!*pending; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 627 | } |
| 628 | |
Scott Wood | c59a6a3 | 2011-11-08 18:23:25 -0600 | [diff] [blame] | 629 | /* Check pending exceptions and deliver one, if possible. */ |
Alexander Graf | a8e4ef8 | 2012-02-16 14:07:37 +0000 | [diff] [blame] | 630 | int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu) |
Scott Wood | c59a6a3 | 2011-11-08 18:23:25 -0600 | [diff] [blame] | 631 | { |
Alexander Graf | a8e4ef8 | 2012-02-16 14:07:37 +0000 | [diff] [blame] | 632 | int r = 0; |
Scott Wood | c59a6a3 | 2011-11-08 18:23:25 -0600 | [diff] [blame] | 633 | WARN_ON_ONCE(!irqs_disabled()); |
| 634 | |
| 635 | kvmppc_core_check_exceptions(vcpu); |
| 636 | |
Alexander Graf | b8c649a | 2012-12-20 04:52:39 +0000 | [diff] [blame] | 637 | if (vcpu->requests) { |
| 638 | /* Exception delivery raised request; start over */ |
| 639 | return 1; |
| 640 | } |
| 641 | |
Scott Wood | c59a6a3 | 2011-11-08 18:23:25 -0600 | [diff] [blame] | 642 | if (vcpu->arch.shared->msr & MSR_WE) { |
| 643 | local_irq_enable(); |
| 644 | kvm_vcpu_block(vcpu); |
Alexander Graf | 966cd0f | 2012-03-14 16:55:08 +0100 | [diff] [blame] | 645 | clear_bit(KVM_REQ_UNHALT, &vcpu->requests); |
Scott Wood | c59a6a3 | 2011-11-08 18:23:25 -0600 | [diff] [blame] | 646 | local_irq_disable(); |
| 647 | |
| 648 | kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS); |
Alexander Graf | a8e4ef8 | 2012-02-16 14:07:37 +0000 | [diff] [blame] | 649 | r = 1; |
Scott Wood | c59a6a3 | 2011-11-08 18:23:25 -0600 | [diff] [blame] | 650 | }; |
Alexander Graf | a8e4ef8 | 2012-02-16 14:07:37 +0000 | [diff] [blame] | 651 | |
| 652 | return r; |
| 653 | } |
| 654 | |
Alexander Graf | 7c973a2 | 2012-08-13 12:50:35 +0200 | [diff] [blame] | 655 | int kvmppc_core_check_requests(struct kvm_vcpu *vcpu) |
Alexander Graf | 4ffc635 | 2012-08-08 20:31:13 +0200 | [diff] [blame] | 656 | { |
Alexander Graf | 7c973a2 | 2012-08-13 12:50:35 +0200 | [diff] [blame] | 657 | int r = 1; /* Indicate we want to get back into the guest */ |
| 658 | |
Alexander Graf | 2d8185d | 2012-08-10 12:31:12 +0200 | [diff] [blame] | 659 | if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu)) |
| 660 | update_timer_ints(vcpu); |
Alexander Graf | 862d31f | 2012-07-31 00:19:50 +0200 | [diff] [blame] | 661 | #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) |
Alexander Graf | 2d8185d | 2012-08-10 12:31:12 +0200 | [diff] [blame] | 662 | if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) |
| 663 | kvmppc_core_flush_tlb(vcpu); |
Alexander Graf | 862d31f | 2012-07-31 00:19:50 +0200 | [diff] [blame] | 664 | #endif |
Alexander Graf | 7c973a2 | 2012-08-13 12:50:35 +0200 | [diff] [blame] | 665 | |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 666 | if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) { |
| 667 | vcpu->run->exit_reason = KVM_EXIT_WATCHDOG; |
| 668 | r = 0; |
| 669 | } |
| 670 | |
Alexander Graf | 1c81063 | 2013-01-04 18:12:48 +0100 | [diff] [blame] | 671 | if (kvm_check_request(KVM_REQ_EPR_EXIT, vcpu)) { |
| 672 | vcpu->run->epr.epr = 0; |
| 673 | vcpu->arch.epr_needed = true; |
| 674 | vcpu->run->exit_reason = KVM_EXIT_EPR; |
| 675 | r = 0; |
| 676 | } |
| 677 | |
Alexander Graf | 7c973a2 | 2012-08-13 12:50:35 +0200 | [diff] [blame] | 678 | return r; |
Alexander Graf | 4ffc635 | 2012-08-08 20:31:13 +0200 | [diff] [blame] | 679 | } |
| 680 | |
Paul Mackerras | df6909e5 | 2011-06-29 00:19:50 +0000 | [diff] [blame] | 681 | int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) |
| 682 | { |
Alexander Graf | 7ee7885 | 2012-08-13 12:44:41 +0200 | [diff] [blame] | 683 | int ret, s; |
Scott Wood | f5f9721 | 2013-11-22 15:52:29 -0600 | [diff] [blame] | 684 | struct debug_reg debug; |
Scott Wood | 8fae845 | 2011-12-20 15:34:45 +0000 | [diff] [blame] | 685 | #ifdef CONFIG_PPC_FPU |
Paul Mackerras | de79f7b | 2013-09-10 20:20:42 +1000 | [diff] [blame] | 686 | struct thread_fp_state fp; |
Scott Wood | 8fae845 | 2011-12-20 15:34:45 +0000 | [diff] [blame] | 687 | int fpexc_mode; |
Scott Wood | 8fae845 | 2011-12-20 15:34:45 +0000 | [diff] [blame] | 688 | #endif |
Paul Mackerras | df6909e5 | 2011-06-29 00:19:50 +0000 | [diff] [blame] | 689 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 690 | if (!vcpu->arch.sane) { |
| 691 | kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 692 | return -EINVAL; |
| 693 | } |
| 694 | |
Paul Mackerras | df6909e5 | 2011-06-29 00:19:50 +0000 | [diff] [blame] | 695 | local_irq_disable(); |
Alexander Graf | 7ee7885 | 2012-08-13 12:44:41 +0200 | [diff] [blame] | 696 | s = kvmppc_prepare_to_enter(vcpu); |
| 697 | if (s <= 0) { |
Alexander Graf | 24afa37b | 2012-08-12 12:42:30 +0200 | [diff] [blame] | 698 | local_irq_enable(); |
Alexander Graf | 7ee7885 | 2012-08-13 12:44:41 +0200 | [diff] [blame] | 699 | ret = s; |
Scott Wood | 1d1ef22 | 2011-11-08 16:11:59 -0600 | [diff] [blame] | 700 | goto out; |
| 701 | } |
| 702 | |
Scott Wood | 8fae845 | 2011-12-20 15:34:45 +0000 | [diff] [blame] | 703 | #ifdef CONFIG_PPC_FPU |
| 704 | /* Save userspace FPU state in stack */ |
| 705 | enable_kernel_fp(); |
Paul Mackerras | de79f7b | 2013-09-10 20:20:42 +1000 | [diff] [blame] | 706 | fp = current->thread.fp_state; |
Scott Wood | 8fae845 | 2011-12-20 15:34:45 +0000 | [diff] [blame] | 707 | fpexc_mode = current->thread.fpexc_mode; |
| 708 | |
| 709 | /* Restore guest FPU state to thread */ |
Paul Mackerras | de79f7b | 2013-09-10 20:20:42 +1000 | [diff] [blame] | 710 | memcpy(current->thread.fp_state.fpr, vcpu->arch.fpr, |
| 711 | sizeof(vcpu->arch.fpr)); |
| 712 | current->thread.fp_state.fpscr = vcpu->arch.fpscr; |
Scott Wood | 8fae845 | 2011-12-20 15:34:45 +0000 | [diff] [blame] | 713 | |
| 714 | /* |
| 715 | * Since we can't trap on MSR_FP in GS-mode, we consider the guest |
| 716 | * as always using the FPU. Kernel usage of FP (via |
| 717 | * enable_kernel_fp()) in this thread must not occur while |
| 718 | * vcpu->fpu_active is set. |
| 719 | */ |
| 720 | vcpu->fpu_active = 1; |
| 721 | |
| 722 | kvmppc_load_guest_fp(vcpu); |
| 723 | #endif |
| 724 | |
Bharat Bhushan | ce11e48 | 2013-07-04 12:27:47 +0530 | [diff] [blame] | 725 | /* Switch to guest debug context */ |
Scott Wood | f5f9721 | 2013-11-22 15:52:29 -0600 | [diff] [blame] | 726 | debug = vcpu->arch.shadow_dbg_reg; |
| 727 | switch_booke_debug_regs(&debug); |
| 728 | debug = current->thread.debug; |
Bharat Bhushan | ce11e48 | 2013-07-04 12:27:47 +0530 | [diff] [blame] | 729 | current->thread.debug = vcpu->arch.shadow_dbg_reg; |
| 730 | |
Scott Wood | 5f1c248 | 2013-07-10 17:47:39 -0500 | [diff] [blame] | 731 | kvmppc_fix_ee_before_entry(); |
Scott Wood | f8941fbe | 2013-06-11 11:38:31 -0500 | [diff] [blame] | 732 | |
Paul Mackerras | df6909e5 | 2011-06-29 00:19:50 +0000 | [diff] [blame] | 733 | ret = __kvmppc_vcpu_run(kvm_run, vcpu); |
Scott Wood | 8fae845 | 2011-12-20 15:34:45 +0000 | [diff] [blame] | 734 | |
Alexander Graf | 24afa37b | 2012-08-12 12:42:30 +0200 | [diff] [blame] | 735 | /* No need for kvm_guest_exit. It's done in handle_exit. |
| 736 | We also get here with interrupts enabled. */ |
| 737 | |
Bharat Bhushan | ce11e48 | 2013-07-04 12:27:47 +0530 | [diff] [blame] | 738 | /* Switch back to user space debug context */ |
Scott Wood | f5f9721 | 2013-11-22 15:52:29 -0600 | [diff] [blame] | 739 | switch_booke_debug_regs(&debug); |
| 740 | current->thread.debug = debug; |
Bharat Bhushan | ce11e48 | 2013-07-04 12:27:47 +0530 | [diff] [blame] | 741 | |
Scott Wood | 8fae845 | 2011-12-20 15:34:45 +0000 | [diff] [blame] | 742 | #ifdef CONFIG_PPC_FPU |
| 743 | kvmppc_save_guest_fp(vcpu); |
| 744 | |
| 745 | vcpu->fpu_active = 0; |
| 746 | |
| 747 | /* Save guest FPU state from thread */ |
Paul Mackerras | de79f7b | 2013-09-10 20:20:42 +1000 | [diff] [blame] | 748 | memcpy(vcpu->arch.fpr, current->thread.fp_state.fpr, |
| 749 | sizeof(vcpu->arch.fpr)); |
| 750 | vcpu->arch.fpscr = current->thread.fp_state.fpscr; |
Scott Wood | 8fae845 | 2011-12-20 15:34:45 +0000 | [diff] [blame] | 751 | |
| 752 | /* Restore userspace FPU state from stack */ |
Paul Mackerras | de79f7b | 2013-09-10 20:20:42 +1000 | [diff] [blame] | 753 | current->thread.fp_state = fp; |
Scott Wood | 8fae845 | 2011-12-20 15:34:45 +0000 | [diff] [blame] | 754 | current->thread.fpexc_mode = fpexc_mode; |
| 755 | #endif |
| 756 | |
Scott Wood | 1d1ef22 | 2011-11-08 16:11:59 -0600 | [diff] [blame] | 757 | out: |
Alexander Graf | d69c643 | 2012-08-08 20:44:20 +0200 | [diff] [blame] | 758 | vcpu->mode = OUTSIDE_GUEST_MODE; |
Paul Mackerras | df6909e5 | 2011-06-29 00:19:50 +0000 | [diff] [blame] | 759 | return ret; |
| 760 | } |
| 761 | |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 762 | static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu) |
| 763 | { |
| 764 | enum emulation_result er; |
| 765 | |
| 766 | er = kvmppc_emulate_instruction(run, vcpu); |
| 767 | switch (er) { |
| 768 | case EMULATE_DONE: |
| 769 | /* don't overwrite subtypes, just account kvm_stats */ |
| 770 | kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS); |
| 771 | /* Future optimization: only reload non-volatiles if |
| 772 | * they were actually modified by emulation. */ |
| 773 | return RESUME_GUEST_NV; |
| 774 | |
| 775 | case EMULATE_DO_DCR: |
| 776 | run->exit_reason = KVM_EXIT_DCR; |
| 777 | return RESUME_HOST; |
| 778 | |
| 779 | case EMULATE_FAIL: |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 780 | printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n", |
| 781 | __func__, vcpu->arch.pc, vcpu->arch.last_inst); |
| 782 | /* For debugging, encode the failing instruction and |
| 783 | * report it to userspace. */ |
| 784 | run->hw.hardware_exit_reason = ~0ULL << 32; |
| 785 | run->hw.hardware_exit_reason |= vcpu->arch.last_inst; |
Alexander Graf | d1ff549 | 2012-02-16 13:24:03 +0000 | [diff] [blame] | 786 | kvmppc_core_queue_program(vcpu, ESR_PIL); |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 787 | return RESUME_HOST; |
| 788 | |
Bharat Bhushan | 9b4f530 | 2013-04-08 00:32:15 +0000 | [diff] [blame] | 789 | case EMULATE_EXIT_USER: |
| 790 | return RESUME_HOST; |
| 791 | |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 792 | default: |
| 793 | BUG(); |
| 794 | } |
| 795 | } |
| 796 | |
Bharat Bhushan | ce11e48 | 2013-07-04 12:27:47 +0530 | [diff] [blame] | 797 | static int kvmppc_handle_debug(struct kvm_run *run, struct kvm_vcpu *vcpu) |
| 798 | { |
| 799 | struct debug_reg *dbg_reg = &(vcpu->arch.shadow_dbg_reg); |
| 800 | u32 dbsr = vcpu->arch.dbsr; |
| 801 | |
| 802 | run->debug.arch.status = 0; |
| 803 | run->debug.arch.address = vcpu->arch.pc; |
| 804 | |
| 805 | if (dbsr & (DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4)) { |
| 806 | run->debug.arch.status |= KVMPPC_DEBUG_BREAKPOINT; |
| 807 | } else { |
| 808 | if (dbsr & (DBSR_DAC1W | DBSR_DAC2W)) |
| 809 | run->debug.arch.status |= KVMPPC_DEBUG_WATCH_WRITE; |
| 810 | else if (dbsr & (DBSR_DAC1R | DBSR_DAC2R)) |
| 811 | run->debug.arch.status |= KVMPPC_DEBUG_WATCH_READ; |
| 812 | if (dbsr & (DBSR_DAC1R | DBSR_DAC1W)) |
| 813 | run->debug.arch.address = dbg_reg->dac1; |
| 814 | else if (dbsr & (DBSR_DAC2R | DBSR_DAC2W)) |
| 815 | run->debug.arch.address = dbg_reg->dac2; |
| 816 | } |
| 817 | |
| 818 | return RESUME_HOST; |
| 819 | } |
| 820 | |
Alexander Graf | 4e642cc | 2012-02-20 23:57:26 +0100 | [diff] [blame] | 821 | static void kvmppc_fill_pt_regs(struct pt_regs *regs) |
| 822 | { |
| 823 | ulong r1, ip, msr, lr; |
| 824 | |
| 825 | asm("mr %0, 1" : "=r"(r1)); |
| 826 | asm("mflr %0" : "=r"(lr)); |
| 827 | asm("mfmsr %0" : "=r"(msr)); |
| 828 | asm("bl 1f; 1: mflr %0" : "=r"(ip)); |
| 829 | |
| 830 | memset(regs, 0, sizeof(*regs)); |
| 831 | regs->gpr[1] = r1; |
| 832 | regs->nip = ip; |
| 833 | regs->msr = msr; |
| 834 | regs->link = lr; |
| 835 | } |
| 836 | |
Bharat Bhushan | 6328e59 | 2012-06-20 05:56:53 +0000 | [diff] [blame] | 837 | /* |
| 838 | * For interrupts needed to be handled by host interrupt handlers, |
| 839 | * corresponding host handler are called from here in similar way |
| 840 | * (but not exact) as they are called from low level handler |
| 841 | * (such as from arch/powerpc/kernel/head_fsl_booke.S). |
| 842 | */ |
Alexander Graf | 4e642cc | 2012-02-20 23:57:26 +0100 | [diff] [blame] | 843 | static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu, |
| 844 | unsigned int exit_nr) |
| 845 | { |
| 846 | struct pt_regs regs; |
| 847 | |
| 848 | switch (exit_nr) { |
| 849 | case BOOKE_INTERRUPT_EXTERNAL: |
| 850 | kvmppc_fill_pt_regs(®s); |
| 851 | do_IRQ(®s); |
| 852 | break; |
| 853 | case BOOKE_INTERRUPT_DECREMENTER: |
| 854 | kvmppc_fill_pt_regs(®s); |
| 855 | timer_interrupt(®s); |
| 856 | break; |
Tiejun Chen | 5f17ce8 | 2013-05-13 10:00:45 +0800 | [diff] [blame] | 857 | #if defined(CONFIG_PPC_DOORBELL) |
Alexander Graf | 4e642cc | 2012-02-20 23:57:26 +0100 | [diff] [blame] | 858 | case BOOKE_INTERRUPT_DOORBELL: |
| 859 | kvmppc_fill_pt_regs(®s); |
| 860 | doorbell_exception(®s); |
| 861 | break; |
| 862 | #endif |
| 863 | case BOOKE_INTERRUPT_MACHINE_CHECK: |
| 864 | /* FIXME */ |
| 865 | break; |
Alexander Graf | 7cc1e8e | 2012-02-22 16:26:34 +0100 | [diff] [blame] | 866 | case BOOKE_INTERRUPT_PERFORMANCE_MONITOR: |
| 867 | kvmppc_fill_pt_regs(®s); |
| 868 | performance_monitor_exception(®s); |
| 869 | break; |
Bharat Bhushan | 6328e59 | 2012-06-20 05:56:53 +0000 | [diff] [blame] | 870 | case BOOKE_INTERRUPT_WATCHDOG: |
| 871 | kvmppc_fill_pt_regs(®s); |
| 872 | #ifdef CONFIG_BOOKE_WDT |
| 873 | WatchdogException(®s); |
| 874 | #else |
| 875 | unknown_exception(®s); |
| 876 | #endif |
| 877 | break; |
| 878 | case BOOKE_INTERRUPT_CRITICAL: |
| 879 | unknown_exception(®s); |
| 880 | break; |
Bharat Bhushan | ce11e48 | 2013-07-04 12:27:47 +0530 | [diff] [blame] | 881 | case BOOKE_INTERRUPT_DEBUG: |
| 882 | /* Save DBSR before preemption is enabled */ |
| 883 | vcpu->arch.dbsr = mfspr(SPRN_DBSR); |
| 884 | kvmppc_clear_dbsr(); |
| 885 | break; |
Alexander Graf | 4e642cc | 2012-02-20 23:57:26 +0100 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 889 | /** |
| 890 | * kvmppc_handle_exit |
| 891 | * |
| 892 | * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV) |
| 893 | */ |
| 894 | int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 895 | unsigned int exit_nr) |
| 896 | { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 897 | int r = RESUME_HOST; |
Alexander Graf | 7ee7885 | 2012-08-13 12:44:41 +0200 | [diff] [blame] | 898 | int s; |
Scott Wood | f1e8902 | 2013-06-06 19:16:31 -0500 | [diff] [blame] | 899 | int idx; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 900 | |
Scott Wood | 7c11c0c | 2013-06-06 19:16:32 -0500 | [diff] [blame] | 901 | #ifdef CONFIG_PPC64 |
| 902 | WARN_ON(local_paca->irq_happened != 0); |
| 903 | #endif |
| 904 | |
| 905 | /* |
| 906 | * We enter with interrupts disabled in hardware, but |
| 907 | * we need to call hard_irq_disable anyway to ensure that |
| 908 | * the software state is kept in sync. |
| 909 | */ |
| 910 | hard_irq_disable(); |
| 911 | |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 912 | /* update before a new last_exit_type is rewritten */ |
| 913 | kvmppc_update_timing_stats(vcpu); |
| 914 | |
Alexander Graf | 4e642cc | 2012-02-20 23:57:26 +0100 | [diff] [blame] | 915 | /* restart interrupts if they were meant for the host */ |
| 916 | kvmppc_restart_interrupt(vcpu, exit_nr); |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 917 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 918 | local_irq_enable(); |
| 919 | |
Alexander Graf | 97c9505 | 2012-08-02 15:10:00 +0200 | [diff] [blame] | 920 | trace_kvm_exit(exit_nr, vcpu); |
Alexander Graf | 706fb73 | 2012-08-12 11:29:09 +0200 | [diff] [blame] | 921 | kvm_guest_exit(); |
Alexander Graf | 97c9505 | 2012-08-02 15:10:00 +0200 | [diff] [blame] | 922 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 923 | run->exit_reason = KVM_EXIT_UNKNOWN; |
| 924 | run->ready_for_interrupt_injection = 1; |
| 925 | |
| 926 | switch (exit_nr) { |
| 927 | case BOOKE_INTERRUPT_MACHINE_CHECK: |
Alexander Graf | c35c9d8 | 2012-02-20 12:21:18 +0100 | [diff] [blame] | 928 | printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR)); |
| 929 | kvmppc_dump_vcpu(vcpu); |
| 930 | /* For debugging, send invalid exit reason to user space */ |
| 931 | run->hw.hardware_exit_reason = ~1ULL << 32; |
| 932 | run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR); |
| 933 | r = RESUME_HOST; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 934 | break; |
| 935 | |
| 936 | case BOOKE_INTERRUPT_EXTERNAL: |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 937 | kvmppc_account_exit(vcpu, EXT_INTR_EXITS); |
Hollis Blanchard | 1b6766c | 2008-11-05 09:36:21 -0600 | [diff] [blame] | 938 | r = RESUME_GUEST; |
| 939 | break; |
| 940 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 941 | case BOOKE_INTERRUPT_DECREMENTER: |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 942 | kvmppc_account_exit(vcpu, DEC_EXITS); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 943 | r = RESUME_GUEST; |
| 944 | break; |
| 945 | |
Bharat Bhushan | 6328e59 | 2012-06-20 05:56:53 +0000 | [diff] [blame] | 946 | case BOOKE_INTERRUPT_WATCHDOG: |
| 947 | r = RESUME_GUEST; |
| 948 | break; |
| 949 | |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 950 | case BOOKE_INTERRUPT_DOORBELL: |
| 951 | kvmppc_account_exit(vcpu, DBELL_EXITS); |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 952 | r = RESUME_GUEST; |
| 953 | break; |
| 954 | |
| 955 | case BOOKE_INTERRUPT_GUEST_DBELL_CRIT: |
| 956 | kvmppc_account_exit(vcpu, GDBELL_EXITS); |
| 957 | |
| 958 | /* |
| 959 | * We are here because there is a pending guest interrupt |
| 960 | * which could not be delivered as MSR_CE or MSR_ME was not |
| 961 | * set. Once we break from here we will retry delivery. |
| 962 | */ |
| 963 | r = RESUME_GUEST; |
| 964 | break; |
| 965 | |
| 966 | case BOOKE_INTERRUPT_GUEST_DBELL: |
| 967 | kvmppc_account_exit(vcpu, GDBELL_EXITS); |
| 968 | |
| 969 | /* |
| 970 | * We are here because there is a pending guest interrupt |
| 971 | * which could not be delivered as MSR_EE was not set. Once |
| 972 | * we break from here we will retry delivery. |
| 973 | */ |
| 974 | r = RESUME_GUEST; |
| 975 | break; |
| 976 | |
Alexander Graf | 95f2e92 | 2012-02-20 22:45:12 +0100 | [diff] [blame] | 977 | case BOOKE_INTERRUPT_PERFORMANCE_MONITOR: |
| 978 | r = RESUME_GUEST; |
| 979 | break; |
| 980 | |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 981 | case BOOKE_INTERRUPT_HV_PRIV: |
| 982 | r = emulation_exit(run, vcpu); |
| 983 | break; |
| 984 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 985 | case BOOKE_INTERRUPT_PROGRAM: |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 986 | if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) { |
Alexander Graf | 0268597c | 2012-02-20 12:33:22 +0100 | [diff] [blame] | 987 | /* |
| 988 | * Program traps generated by user-level software must |
| 989 | * be handled by the guest kernel. |
| 990 | * |
| 991 | * In GS mode, hypervisor privileged instructions trap |
| 992 | * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are |
| 993 | * actual program interrupts, handled by the guest. |
| 994 | */ |
Liu Yu | daf5e27 | 2010-02-02 19:44:35 +0800 | [diff] [blame] | 995 | kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 996 | r = RESUME_GUEST; |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 997 | kvmppc_account_exit(vcpu, USR_PR_INST); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 998 | break; |
| 999 | } |
| 1000 | |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 1001 | r = emulation_exit(run, vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1002 | break; |
| 1003 | |
Christian Ehrhardt | de368dc | 2008-04-29 18:18:23 +0200 | [diff] [blame] | 1004 | case BOOKE_INTERRUPT_FP_UNAVAIL: |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 1005 | kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL); |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 1006 | kvmppc_account_exit(vcpu, FP_UNAVAIL); |
Christian Ehrhardt | de368dc | 2008-04-29 18:18:23 +0200 | [diff] [blame] | 1007 | r = RESUME_GUEST; |
| 1008 | break; |
| 1009 | |
Scott Wood | 4cd35f6 | 2011-06-14 18:34:31 -0500 | [diff] [blame] | 1010 | #ifdef CONFIG_SPE |
| 1011 | case BOOKE_INTERRUPT_SPE_UNAVAIL: { |
| 1012 | if (vcpu->arch.shared->msr & MSR_SPE) |
| 1013 | kvmppc_vcpu_enable_spe(vcpu); |
| 1014 | else |
| 1015 | kvmppc_booke_queue_irqprio(vcpu, |
| 1016 | BOOKE_IRQPRIO_SPE_UNAVAIL); |
Hollis Blanchard | bb3a8a1 | 2009-01-03 16:23:13 -0600 | [diff] [blame] | 1017 | r = RESUME_GUEST; |
| 1018 | break; |
Scott Wood | 4cd35f6 | 2011-06-14 18:34:31 -0500 | [diff] [blame] | 1019 | } |
Hollis Blanchard | bb3a8a1 | 2009-01-03 16:23:13 -0600 | [diff] [blame] | 1020 | |
| 1021 | case BOOKE_INTERRUPT_SPE_FP_DATA: |
| 1022 | kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA); |
| 1023 | r = RESUME_GUEST; |
| 1024 | break; |
| 1025 | |
| 1026 | case BOOKE_INTERRUPT_SPE_FP_ROUND: |
| 1027 | kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND); |
| 1028 | r = RESUME_GUEST; |
| 1029 | break; |
Scott Wood | 4cd35f6 | 2011-06-14 18:34:31 -0500 | [diff] [blame] | 1030 | #else |
| 1031 | case BOOKE_INTERRUPT_SPE_UNAVAIL: |
| 1032 | /* |
| 1033 | * Guest wants SPE, but host kernel doesn't support it. Send |
| 1034 | * an "unimplemented operation" program check to the guest. |
| 1035 | */ |
| 1036 | kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV); |
| 1037 | r = RESUME_GUEST; |
| 1038 | break; |
| 1039 | |
| 1040 | /* |
| 1041 | * These really should never happen without CONFIG_SPE, |
| 1042 | * as we should never enable the real MSR[SPE] in the guest. |
| 1043 | */ |
| 1044 | case BOOKE_INTERRUPT_SPE_FP_DATA: |
| 1045 | case BOOKE_INTERRUPT_SPE_FP_ROUND: |
| 1046 | printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n", |
| 1047 | __func__, exit_nr, vcpu->arch.pc); |
| 1048 | run->hw.hardware_exit_reason = exit_nr; |
| 1049 | r = RESUME_HOST; |
| 1050 | break; |
| 1051 | #endif |
Hollis Blanchard | bb3a8a1 | 2009-01-03 16:23:13 -0600 | [diff] [blame] | 1052 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1053 | case BOOKE_INTERRUPT_DATA_STORAGE: |
Liu Yu | daf5e27 | 2010-02-02 19:44:35 +0800 | [diff] [blame] | 1054 | kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear, |
| 1055 | vcpu->arch.fault_esr); |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 1056 | kvmppc_account_exit(vcpu, DSI_EXITS); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1057 | r = RESUME_GUEST; |
| 1058 | break; |
| 1059 | |
| 1060 | case BOOKE_INTERRUPT_INST_STORAGE: |
Liu Yu | daf5e27 | 2010-02-02 19:44:35 +0800 | [diff] [blame] | 1061 | kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr); |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 1062 | kvmppc_account_exit(vcpu, ISI_EXITS); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1063 | r = RESUME_GUEST; |
| 1064 | break; |
| 1065 | |
Alexander Graf | 011da89 | 2013-01-31 14:17:38 +0100 | [diff] [blame] | 1066 | case BOOKE_INTERRUPT_ALIGNMENT: |
| 1067 | kvmppc_core_queue_alignment(vcpu, vcpu->arch.fault_dear, |
| 1068 | vcpu->arch.fault_esr); |
| 1069 | r = RESUME_GUEST; |
| 1070 | break; |
| 1071 | |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 1072 | #ifdef CONFIG_KVM_BOOKE_HV |
| 1073 | case BOOKE_INTERRUPT_HV_SYSCALL: |
| 1074 | if (!(vcpu->arch.shared->msr & MSR_PR)) { |
| 1075 | kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu)); |
| 1076 | } else { |
| 1077 | /* |
| 1078 | * hcall from guest userspace -- send privileged |
| 1079 | * instruction program check. |
| 1080 | */ |
| 1081 | kvmppc_core_queue_program(vcpu, ESR_PPR); |
| 1082 | } |
| 1083 | |
| 1084 | r = RESUME_GUEST; |
| 1085 | break; |
| 1086 | #else |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1087 | case BOOKE_INTERRUPT_SYSCALL: |
Alexander Graf | 2a342ed | 2010-07-29 14:47:48 +0200 | [diff] [blame] | 1088 | if (!(vcpu->arch.shared->msr & MSR_PR) && |
| 1089 | (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) { |
| 1090 | /* KVM PV hypercalls */ |
| 1091 | kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu)); |
| 1092 | r = RESUME_GUEST; |
| 1093 | } else { |
| 1094 | /* Guest syscalls */ |
| 1095 | kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL); |
| 1096 | } |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 1097 | kvmppc_account_exit(vcpu, SYSCALL_EXITS); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1098 | r = RESUME_GUEST; |
| 1099 | break; |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 1100 | #endif |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1101 | |
| 1102 | case BOOKE_INTERRUPT_DTLB_MISS: { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1103 | unsigned long eaddr = vcpu->arch.fault_dear; |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 1104 | int gtlb_index; |
Hollis Blanchard | 475e7cd | 2009-01-03 16:23:00 -0600 | [diff] [blame] | 1105 | gpa_t gpaddr; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1106 | gfn_t gfn; |
| 1107 | |
Alexander Graf | bf7ca4b | 2012-02-15 23:40:00 +0000 | [diff] [blame] | 1108 | #ifdef CONFIG_KVM_E500V2 |
Scott Wood | a4cd8b2 | 2011-06-14 18:34:41 -0500 | [diff] [blame] | 1109 | if (!(vcpu->arch.shared->msr & MSR_PR) && |
| 1110 | (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) { |
| 1111 | kvmppc_map_magic(vcpu); |
| 1112 | kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS); |
| 1113 | r = RESUME_GUEST; |
| 1114 | |
| 1115 | break; |
| 1116 | } |
| 1117 | #endif |
| 1118 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1119 | /* Check the guest TLB. */ |
Hollis Blanchard | fa86b8d | 2009-01-03 16:23:03 -0600 | [diff] [blame] | 1120 | gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr); |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 1121 | if (gtlb_index < 0) { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1122 | /* The guest didn't have a mapping for it. */ |
Liu Yu | daf5e27 | 2010-02-02 19:44:35 +0800 | [diff] [blame] | 1123 | kvmppc_core_queue_dtlb_miss(vcpu, |
| 1124 | vcpu->arch.fault_dear, |
| 1125 | vcpu->arch.fault_esr); |
Hollis Blanchard | b52a638 | 2009-01-03 16:23:11 -0600 | [diff] [blame] | 1126 | kvmppc_mmu_dtlb_miss(vcpu); |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 1127 | kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1128 | r = RESUME_GUEST; |
| 1129 | break; |
| 1130 | } |
| 1131 | |
Scott Wood | f1e8902 | 2013-06-06 19:16:31 -0500 | [diff] [blame] | 1132 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 1133 | |
Hollis Blanchard | be8d1ca | 2009-01-03 16:23:02 -0600 | [diff] [blame] | 1134 | gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr); |
Hollis Blanchard | 475e7cd | 2009-01-03 16:23:00 -0600 | [diff] [blame] | 1135 | gfn = gpaddr >> PAGE_SHIFT; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1136 | |
| 1137 | if (kvm_is_visible_gfn(vcpu->kvm, gfn)) { |
| 1138 | /* The guest TLB had a mapping, but the shadow TLB |
| 1139 | * didn't, and it is RAM. This could be because: |
| 1140 | * a) the entry is mapping the host kernel, or |
| 1141 | * b) the guest used a large mapping which we're faking |
| 1142 | * Either way, we need to satisfy the fault without |
| 1143 | * invoking the guest. */ |
Hollis Blanchard | 58a9621 | 2009-01-03 16:23:01 -0600 | [diff] [blame] | 1144 | kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index); |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 1145 | kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1146 | r = RESUME_GUEST; |
| 1147 | } else { |
| 1148 | /* Guest has mapped and accessed a page which is not |
| 1149 | * actually RAM. */ |
Hollis Blanchard | 475e7cd | 2009-01-03 16:23:00 -0600 | [diff] [blame] | 1150 | vcpu->arch.paddr_accessed = gpaddr; |
Alexander Graf | 6020c0f | 2012-03-12 02:26:30 +0100 | [diff] [blame] | 1151 | vcpu->arch.vaddr_accessed = eaddr; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1152 | r = kvmppc_emulate_mmio(run, vcpu); |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 1153 | kvmppc_account_exit(vcpu, MMIO_EXITS); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1154 | } |
| 1155 | |
Scott Wood | f1e8902 | 2013-06-06 19:16:31 -0500 | [diff] [blame] | 1156 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1157 | break; |
| 1158 | } |
| 1159 | |
| 1160 | case BOOKE_INTERRUPT_ITLB_MISS: { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1161 | unsigned long eaddr = vcpu->arch.pc; |
Hollis Blanchard | 8916861 | 2008-12-02 15:51:53 -0600 | [diff] [blame] | 1162 | gpa_t gpaddr; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1163 | gfn_t gfn; |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 1164 | int gtlb_index; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1165 | |
| 1166 | r = RESUME_GUEST; |
| 1167 | |
| 1168 | /* Check the guest TLB. */ |
Hollis Blanchard | fa86b8d | 2009-01-03 16:23:03 -0600 | [diff] [blame] | 1169 | gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr); |
Hollis Blanchard | 7924bd4 | 2008-12-02 15:51:55 -0600 | [diff] [blame] | 1170 | if (gtlb_index < 0) { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1171 | /* The guest didn't have a mapping for it. */ |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 1172 | kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS); |
Hollis Blanchard | b52a638 | 2009-01-03 16:23:11 -0600 | [diff] [blame] | 1173 | kvmppc_mmu_itlb_miss(vcpu); |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 1174 | kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1175 | break; |
| 1176 | } |
| 1177 | |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 1178 | kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1179 | |
Scott Wood | f1e8902 | 2013-06-06 19:16:31 -0500 | [diff] [blame] | 1180 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 1181 | |
Hollis Blanchard | be8d1ca | 2009-01-03 16:23:02 -0600 | [diff] [blame] | 1182 | gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr); |
Hollis Blanchard | 8916861 | 2008-12-02 15:51:53 -0600 | [diff] [blame] | 1183 | gfn = gpaddr >> PAGE_SHIFT; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1184 | |
| 1185 | if (kvm_is_visible_gfn(vcpu->kvm, gfn)) { |
| 1186 | /* The guest TLB had a mapping, but the shadow TLB |
| 1187 | * didn't. This could be because: |
| 1188 | * a) the entry is mapping the host kernel, or |
| 1189 | * b) the guest used a large mapping which we're faking |
| 1190 | * Either way, we need to satisfy the fault without |
| 1191 | * invoking the guest. */ |
Hollis Blanchard | 58a9621 | 2009-01-03 16:23:01 -0600 | [diff] [blame] | 1192 | kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1193 | } else { |
| 1194 | /* Guest mapped and leaped at non-RAM! */ |
Hollis Blanchard | d4cf389 | 2008-11-05 09:36:23 -0600 | [diff] [blame] | 1195 | kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1196 | } |
| 1197 | |
Scott Wood | f1e8902 | 2013-06-06 19:16:31 -0500 | [diff] [blame] | 1198 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1199 | break; |
| 1200 | } |
| 1201 | |
Hollis Blanchard | 6a0ab73 | 2008-07-25 13:54:49 -0500 | [diff] [blame] | 1202 | case BOOKE_INTERRUPT_DEBUG: { |
Bharat Bhushan | ce11e48 | 2013-07-04 12:27:47 +0530 | [diff] [blame] | 1203 | r = kvmppc_handle_debug(run, vcpu); |
| 1204 | if (r == RESUME_HOST) |
| 1205 | run->exit_reason = KVM_EXIT_DEBUG; |
Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 1206 | kvmppc_account_exit(vcpu, DEBUG_EXITS); |
Hollis Blanchard | 6a0ab73 | 2008-07-25 13:54:49 -0500 | [diff] [blame] | 1207 | break; |
| 1208 | } |
| 1209 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1210 | default: |
| 1211 | printk(KERN_EMERG "exit_nr %d\n", exit_nr); |
| 1212 | BUG(); |
| 1213 | } |
| 1214 | |
Alexander Graf | a8e4ef8 | 2012-02-16 14:07:37 +0000 | [diff] [blame] | 1215 | /* |
| 1216 | * To avoid clobbering exit_reason, only check for signals if we |
| 1217 | * aren't already exiting to userspace for some other reason. |
| 1218 | */ |
Alexander Graf | 03660ba | 2012-02-28 12:00:41 +0100 | [diff] [blame] | 1219 | if (!(r & RESUME_HOST)) { |
| 1220 | local_irq_disable(); |
Alexander Graf | 7ee7885 | 2012-08-13 12:44:41 +0200 | [diff] [blame] | 1221 | s = kvmppc_prepare_to_enter(vcpu); |
| 1222 | if (s <= 0) { |
Alexander Graf | 24afa37b | 2012-08-12 12:42:30 +0200 | [diff] [blame] | 1223 | local_irq_enable(); |
Alexander Graf | 7ee7885 | 2012-08-13 12:44:41 +0200 | [diff] [blame] | 1224 | r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV); |
Alexander Graf | 24afa37b | 2012-08-12 12:42:30 +0200 | [diff] [blame] | 1225 | } else { |
Scott Wood | 5f1c248 | 2013-07-10 17:47:39 -0500 | [diff] [blame] | 1226 | kvmppc_fix_ee_before_entry(); |
Alexander Graf | 03660ba | 2012-02-28 12:00:41 +0100 | [diff] [blame] | 1227 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | return r; |
| 1231 | } |
| 1232 | |
Bharat Bhushan | d26f22c | 2013-02-24 18:57:11 +0000 | [diff] [blame] | 1233 | static void kvmppc_set_tsr(struct kvm_vcpu *vcpu, u32 new_tsr) |
| 1234 | { |
| 1235 | u32 old_tsr = vcpu->arch.tsr; |
| 1236 | |
| 1237 | vcpu->arch.tsr = new_tsr; |
| 1238 | |
| 1239 | if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS)) |
| 1240 | arm_next_watchdog(vcpu); |
| 1241 | |
| 1242 | update_timer_ints(vcpu); |
| 1243 | } |
| 1244 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1245 | /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */ |
| 1246 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
| 1247 | { |
Hollis Blanchard | 082decf | 2010-08-07 10:33:56 -0700 | [diff] [blame] | 1248 | int i; |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 1249 | int r; |
Hollis Blanchard | 082decf | 2010-08-07 10:33:56 -0700 | [diff] [blame] | 1250 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1251 | vcpu->arch.pc = 0; |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 1252 | vcpu->arch.shared->pir = vcpu->vcpu_id; |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 1253 | kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */ |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 1254 | kvmppc_set_msr(vcpu, 0); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1255 | |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 1256 | #ifndef CONFIG_KVM_BOOKE_HV |
Bharat Bhushan | ce11e48 | 2013-07-04 12:27:47 +0530 | [diff] [blame] | 1257 | vcpu->arch.shadow_msr = MSR_USER | MSR_IS | MSR_DS; |
Hollis Blanchard | 49dd2c4 | 2008-07-25 13:54:53 -0500 | [diff] [blame] | 1258 | vcpu->arch.shadow_pid = 1; |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 1259 | vcpu->arch.shared->msr = 0; |
| 1260 | #endif |
Hollis Blanchard | 49dd2c4 | 2008-07-25 13:54:53 -0500 | [diff] [blame] | 1261 | |
Hollis Blanchard | 082decf | 2010-08-07 10:33:56 -0700 | [diff] [blame] | 1262 | /* Eye-catching numbers so we know if the guest takes an interrupt |
| 1263 | * before it's programmed its own IVPR/IVORs. */ |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1264 | vcpu->arch.ivpr = 0x55550000; |
Hollis Blanchard | 082decf | 2010-08-07 10:33:56 -0700 | [diff] [blame] | 1265 | for (i = 0; i < BOOKE_IRQPRIO_MAX; i++) |
| 1266 | vcpu->arch.ivor[i] = 0x7700 | i * 4; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1267 | |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 1268 | kvmppc_init_timing_stats(vcpu); |
| 1269 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 1270 | r = kvmppc_core_vcpu_setup(vcpu); |
| 1271 | kvmppc_sanity_check(vcpu); |
| 1272 | return r; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1273 | } |
| 1274 | |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 1275 | int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu) |
| 1276 | { |
| 1277 | /* setup watchdog timer once */ |
| 1278 | spin_lock_init(&vcpu->arch.wdt_lock); |
| 1279 | setup_timer(&vcpu->arch.wdt_timer, kvmppc_watchdog_func, |
| 1280 | (unsigned long)vcpu); |
| 1281 | |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 1286 | { |
| 1287 | del_timer_sync(&vcpu->arch.wdt_timer); |
| 1288 | } |
| 1289 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1290 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 1291 | { |
| 1292 | int i; |
| 1293 | |
| 1294 | regs->pc = vcpu->arch.pc; |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 1295 | regs->cr = kvmppc_get_cr(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1296 | regs->ctr = vcpu->arch.ctr; |
| 1297 | regs->lr = vcpu->arch.lr; |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 1298 | regs->xer = kvmppc_get_xer(vcpu); |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 1299 | regs->msr = vcpu->arch.shared->msr; |
Alexander Graf | de7906c | 2010-07-29 14:47:46 +0200 | [diff] [blame] | 1300 | regs->srr0 = vcpu->arch.shared->srr0; |
| 1301 | regs->srr1 = vcpu->arch.shared->srr1; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1302 | regs->pid = vcpu->arch.pid; |
Alexander Graf | a73a959 | 2010-07-29 14:47:47 +0200 | [diff] [blame] | 1303 | regs->sprg0 = vcpu->arch.shared->sprg0; |
| 1304 | regs->sprg1 = vcpu->arch.shared->sprg1; |
| 1305 | regs->sprg2 = vcpu->arch.shared->sprg2; |
| 1306 | regs->sprg3 = vcpu->arch.shared->sprg3; |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 1307 | regs->sprg4 = vcpu->arch.shared->sprg4; |
| 1308 | regs->sprg5 = vcpu->arch.shared->sprg5; |
| 1309 | regs->sprg6 = vcpu->arch.shared->sprg6; |
| 1310 | regs->sprg7 = vcpu->arch.shared->sprg7; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1311 | |
| 1312 | for (i = 0; i < ARRAY_SIZE(regs->gpr); i++) |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 1313 | regs->gpr[i] = kvmppc_get_gpr(vcpu, i); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1314 | |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
| 1318 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 1319 | { |
| 1320 | int i; |
| 1321 | |
| 1322 | vcpu->arch.pc = regs->pc; |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 1323 | kvmppc_set_cr(vcpu, regs->cr); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1324 | vcpu->arch.ctr = regs->ctr; |
| 1325 | vcpu->arch.lr = regs->lr; |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 1326 | kvmppc_set_xer(vcpu, regs->xer); |
Hollis Blanchard | b8fd68a | 2008-11-05 09:36:20 -0600 | [diff] [blame] | 1327 | kvmppc_set_msr(vcpu, regs->msr); |
Alexander Graf | de7906c | 2010-07-29 14:47:46 +0200 | [diff] [blame] | 1328 | vcpu->arch.shared->srr0 = regs->srr0; |
| 1329 | vcpu->arch.shared->srr1 = regs->srr1; |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 1330 | kvmppc_set_pid(vcpu, regs->pid); |
Alexander Graf | a73a959 | 2010-07-29 14:47:47 +0200 | [diff] [blame] | 1331 | vcpu->arch.shared->sprg0 = regs->sprg0; |
| 1332 | vcpu->arch.shared->sprg1 = regs->sprg1; |
| 1333 | vcpu->arch.shared->sprg2 = regs->sprg2; |
| 1334 | vcpu->arch.shared->sprg3 = regs->sprg3; |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 1335 | vcpu->arch.shared->sprg4 = regs->sprg4; |
| 1336 | vcpu->arch.shared->sprg5 = regs->sprg5; |
| 1337 | vcpu->arch.shared->sprg6 = regs->sprg6; |
| 1338 | vcpu->arch.shared->sprg7 = regs->sprg7; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1339 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 1340 | for (i = 0; i < ARRAY_SIZE(regs->gpr); i++) |
| 1341 | kvmppc_set_gpr(vcpu, i, regs->gpr[i]); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1342 | |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 1346 | static void get_sregs_base(struct kvm_vcpu *vcpu, |
| 1347 | struct kvm_sregs *sregs) |
| 1348 | { |
| 1349 | u64 tb = get_tb(); |
| 1350 | |
| 1351 | sregs->u.e.features |= KVM_SREGS_E_BASE; |
| 1352 | |
| 1353 | sregs->u.e.csrr0 = vcpu->arch.csrr0; |
| 1354 | sregs->u.e.csrr1 = vcpu->arch.csrr1; |
| 1355 | sregs->u.e.mcsr = vcpu->arch.mcsr; |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 1356 | sregs->u.e.esr = get_guest_esr(vcpu); |
| 1357 | sregs->u.e.dear = get_guest_dear(vcpu); |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 1358 | sregs->u.e.tsr = vcpu->arch.tsr; |
| 1359 | sregs->u.e.tcr = vcpu->arch.tcr; |
| 1360 | sregs->u.e.dec = kvmppc_get_dec(vcpu, tb); |
| 1361 | sregs->u.e.tb = tb; |
| 1362 | sregs->u.e.vrsave = vcpu->arch.vrsave; |
| 1363 | } |
| 1364 | |
| 1365 | static int set_sregs_base(struct kvm_vcpu *vcpu, |
| 1366 | struct kvm_sregs *sregs) |
| 1367 | { |
| 1368 | if (!(sregs->u.e.features & KVM_SREGS_E_BASE)) |
| 1369 | return 0; |
| 1370 | |
| 1371 | vcpu->arch.csrr0 = sregs->u.e.csrr0; |
| 1372 | vcpu->arch.csrr1 = sregs->u.e.csrr1; |
| 1373 | vcpu->arch.mcsr = sregs->u.e.mcsr; |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 1374 | set_guest_esr(vcpu, sregs->u.e.esr); |
| 1375 | set_guest_dear(vcpu, sregs->u.e.dear); |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 1376 | vcpu->arch.vrsave = sregs->u.e.vrsave; |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 1377 | kvmppc_set_tcr(vcpu, sregs->u.e.tcr); |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 1378 | |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 1379 | if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) { |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 1380 | vcpu->arch.dec = sregs->u.e.dec; |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 1381 | kvmppc_emulate_dec(vcpu); |
| 1382 | } |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 1383 | |
Bharat Bhushan | d26f22c | 2013-02-24 18:57:11 +0000 | [diff] [blame] | 1384 | if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) |
| 1385 | kvmppc_set_tsr(vcpu, sregs->u.e.tsr); |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 1386 | |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
| 1390 | static void get_sregs_arch206(struct kvm_vcpu *vcpu, |
| 1391 | struct kvm_sregs *sregs) |
| 1392 | { |
| 1393 | sregs->u.e.features |= KVM_SREGS_E_ARCH206; |
| 1394 | |
Scott Wood | 841741f | 2011-09-02 17:39:37 -0500 | [diff] [blame] | 1395 | sregs->u.e.pir = vcpu->vcpu_id; |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 1396 | sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0; |
| 1397 | sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1; |
| 1398 | sregs->u.e.decar = vcpu->arch.decar; |
| 1399 | sregs->u.e.ivpr = vcpu->arch.ivpr; |
| 1400 | } |
| 1401 | |
| 1402 | static int set_sregs_arch206(struct kvm_vcpu *vcpu, |
| 1403 | struct kvm_sregs *sregs) |
| 1404 | { |
| 1405 | if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206)) |
| 1406 | return 0; |
| 1407 | |
Scott Wood | 841741f | 2011-09-02 17:39:37 -0500 | [diff] [blame] | 1408 | if (sregs->u.e.pir != vcpu->vcpu_id) |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 1409 | return -EINVAL; |
| 1410 | |
| 1411 | vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0; |
| 1412 | vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1; |
| 1413 | vcpu->arch.decar = sregs->u.e.decar; |
| 1414 | vcpu->arch.ivpr = sregs->u.e.ivpr; |
| 1415 | |
| 1416 | return 0; |
| 1417 | } |
| 1418 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1419 | int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 1420 | { |
| 1421 | sregs->u.e.features |= KVM_SREGS_E_IVOR; |
| 1422 | |
| 1423 | sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL]; |
| 1424 | sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK]; |
| 1425 | sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE]; |
| 1426 | sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE]; |
| 1427 | sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL]; |
| 1428 | sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT]; |
| 1429 | sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM]; |
| 1430 | sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL]; |
| 1431 | sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL]; |
| 1432 | sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL]; |
| 1433 | sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER]; |
| 1434 | sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT]; |
| 1435 | sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG]; |
| 1436 | sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS]; |
| 1437 | sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS]; |
| 1438 | sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG]; |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1439 | return 0; |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
| 1443 | { |
| 1444 | if (!(sregs->u.e.features & KVM_SREGS_E_IVOR)) |
| 1445 | return 0; |
| 1446 | |
| 1447 | vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0]; |
| 1448 | vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1]; |
| 1449 | vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2]; |
| 1450 | vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3]; |
| 1451 | vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4]; |
| 1452 | vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5]; |
| 1453 | vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6]; |
| 1454 | vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7]; |
| 1455 | vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8]; |
| 1456 | vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9]; |
| 1457 | vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10]; |
| 1458 | vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11]; |
| 1459 | vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12]; |
| 1460 | vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13]; |
| 1461 | vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14]; |
| 1462 | vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15]; |
| 1463 | |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1467 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 1468 | struct kvm_sregs *sregs) |
| 1469 | { |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 1470 | sregs->pvr = vcpu->arch.pvr; |
| 1471 | |
| 1472 | get_sregs_base(vcpu, sregs); |
| 1473 | get_sregs_arch206(vcpu, sregs); |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 1474 | return vcpu->kvm->arch.kvm_ops->get_sregs(vcpu, sregs); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 1478 | struct kvm_sregs *sregs) |
| 1479 | { |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 1480 | int ret; |
| 1481 | |
| 1482 | if (vcpu->arch.pvr != sregs->pvr) |
| 1483 | return -EINVAL; |
| 1484 | |
| 1485 | ret = set_sregs_base(vcpu, sregs); |
| 1486 | if (ret < 0) |
| 1487 | return ret; |
| 1488 | |
| 1489 | ret = set_sregs_arch206(vcpu, sregs); |
| 1490 | if (ret < 0) |
| 1491 | return ret; |
| 1492 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 1493 | return vcpu->kvm->arch.kvm_ops->set_sregs(vcpu, sregs); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1494 | } |
| 1495 | |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1496 | int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) |
| 1497 | { |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1498 | int r = 0; |
| 1499 | union kvmppc_one_reg val; |
| 1500 | int size; |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1501 | |
| 1502 | size = one_reg_size(reg->id); |
| 1503 | if (size > sizeof(val)) |
| 1504 | return -EINVAL; |
Bharat Bhushan | 6df8d3f | 2012-08-08 21:17:55 +0000 | [diff] [blame] | 1505 | |
| 1506 | switch (reg->id) { |
| 1507 | case KVM_REG_PPC_IAC1: |
Bharat Bhushan | 547465e | 2013-07-04 12:27:46 +0530 | [diff] [blame] | 1508 | val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac1); |
Bharat Bhushan | 6df8d3f | 2012-08-08 21:17:55 +0000 | [diff] [blame] | 1509 | break; |
Bharat Bhushan | 547465e | 2013-07-04 12:27:46 +0530 | [diff] [blame] | 1510 | case KVM_REG_PPC_IAC2: |
| 1511 | val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac2); |
| 1512 | break; |
| 1513 | #if CONFIG_PPC_ADV_DEBUG_IACS > 2 |
| 1514 | case KVM_REG_PPC_IAC3: |
| 1515 | val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac3); |
| 1516 | break; |
| 1517 | case KVM_REG_PPC_IAC4: |
| 1518 | val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac4); |
| 1519 | break; |
| 1520 | #endif |
Bharat Bhushan | 6df8d3f | 2012-08-08 21:17:55 +0000 | [diff] [blame] | 1521 | case KVM_REG_PPC_DAC1: |
Bharat Bhushan | 547465e | 2013-07-04 12:27:46 +0530 | [diff] [blame] | 1522 | val = get_reg_val(reg->id, vcpu->arch.dbg_reg.dac1); |
| 1523 | break; |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1524 | case KVM_REG_PPC_DAC2: |
Bharat Bhushan | 547465e | 2013-07-04 12:27:46 +0530 | [diff] [blame] | 1525 | val = get_reg_val(reg->id, vcpu->arch.dbg_reg.dac2); |
Bharat Bhushan | 6df8d3f | 2012-08-08 21:17:55 +0000 | [diff] [blame] | 1526 | break; |
Alexander Graf | 324b3e6 | 2013-01-04 18:28:51 +0100 | [diff] [blame] | 1527 | case KVM_REG_PPC_EPR: { |
| 1528 | u32 epr = get_guest_epr(vcpu); |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1529 | val = get_reg_val(reg->id, epr); |
Alexander Graf | 324b3e6 | 2013-01-04 18:28:51 +0100 | [diff] [blame] | 1530 | break; |
| 1531 | } |
Mihai Caraman | 352df1d | 2012-10-11 06:13:29 +0000 | [diff] [blame] | 1532 | #if defined(CONFIG_64BIT) |
| 1533 | case KVM_REG_PPC_EPCR: |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1534 | val = get_reg_val(reg->id, vcpu->arch.epcr); |
Mihai Caraman | 352df1d | 2012-10-11 06:13:29 +0000 | [diff] [blame] | 1535 | break; |
| 1536 | #endif |
Bharat Bhushan | 78accda | 2013-02-24 18:57:12 +0000 | [diff] [blame] | 1537 | case KVM_REG_PPC_TCR: |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1538 | val = get_reg_val(reg->id, vcpu->arch.tcr); |
Bharat Bhushan | 78accda | 2013-02-24 18:57:12 +0000 | [diff] [blame] | 1539 | break; |
| 1540 | case KVM_REG_PPC_TSR: |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1541 | val = get_reg_val(reg->id, vcpu->arch.tsr); |
Bharat Bhushan | 78accda | 2013-02-24 18:57:12 +0000 | [diff] [blame] | 1542 | break; |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1543 | case KVM_REG_PPC_DEBUG_INST: |
Bharat Bhushan | b12c784 | 2013-07-04 12:27:45 +0530 | [diff] [blame] | 1544 | val = get_reg_val(reg->id, KVMPPC_INST_EHPRIV_DEBUG); |
Bharat Bhushan | 8c32a2e | 2013-03-20 20:24:58 +0000 | [diff] [blame] | 1545 | break; |
Paul Mackerras | 8b75cbb | 2013-09-20 14:52:37 +1000 | [diff] [blame] | 1546 | case KVM_REG_PPC_VRSAVE: |
| 1547 | val = get_reg_val(reg->id, vcpu->arch.vrsave); |
Bharat Bhushan | 8c32a2e | 2013-03-20 20:24:58 +0000 | [diff] [blame] | 1548 | break; |
Bharat Bhushan | 6df8d3f | 2012-08-08 21:17:55 +0000 | [diff] [blame] | 1549 | default: |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 1550 | r = vcpu->kvm->arch.kvm_ops->get_one_reg(vcpu, reg->id, &val); |
Bharat Bhushan | 6df8d3f | 2012-08-08 21:17:55 +0000 | [diff] [blame] | 1551 | break; |
| 1552 | } |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1553 | |
| 1554 | if (r) |
| 1555 | return r; |
| 1556 | |
| 1557 | if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size)) |
| 1558 | r = -EFAULT; |
| 1559 | |
Bharat Bhushan | 6df8d3f | 2012-08-08 21:17:55 +0000 | [diff] [blame] | 1560 | return r; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) |
| 1564 | { |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1565 | int r = 0; |
| 1566 | union kvmppc_one_reg val; |
| 1567 | int size; |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1568 | |
| 1569 | size = one_reg_size(reg->id); |
| 1570 | if (size > sizeof(val)) |
| 1571 | return -EINVAL; |
| 1572 | |
| 1573 | if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size)) |
| 1574 | return -EFAULT; |
Bharat Bhushan | 6df8d3f | 2012-08-08 21:17:55 +0000 | [diff] [blame] | 1575 | |
| 1576 | switch (reg->id) { |
| 1577 | case KVM_REG_PPC_IAC1: |
Bharat Bhushan | 547465e | 2013-07-04 12:27:46 +0530 | [diff] [blame] | 1578 | vcpu->arch.dbg_reg.iac1 = set_reg_val(reg->id, val); |
Bharat Bhushan | 6df8d3f | 2012-08-08 21:17:55 +0000 | [diff] [blame] | 1579 | break; |
Bharat Bhushan | 547465e | 2013-07-04 12:27:46 +0530 | [diff] [blame] | 1580 | case KVM_REG_PPC_IAC2: |
| 1581 | vcpu->arch.dbg_reg.iac2 = set_reg_val(reg->id, val); |
| 1582 | break; |
| 1583 | #if CONFIG_PPC_ADV_DEBUG_IACS > 2 |
| 1584 | case KVM_REG_PPC_IAC3: |
| 1585 | vcpu->arch.dbg_reg.iac3 = set_reg_val(reg->id, val); |
| 1586 | break; |
| 1587 | case KVM_REG_PPC_IAC4: |
| 1588 | vcpu->arch.dbg_reg.iac4 = set_reg_val(reg->id, val); |
| 1589 | break; |
| 1590 | #endif |
Bharat Bhushan | 6df8d3f | 2012-08-08 21:17:55 +0000 | [diff] [blame] | 1591 | case KVM_REG_PPC_DAC1: |
Bharat Bhushan | 547465e | 2013-07-04 12:27:46 +0530 | [diff] [blame] | 1592 | vcpu->arch.dbg_reg.dac1 = set_reg_val(reg->id, val); |
| 1593 | break; |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1594 | case KVM_REG_PPC_DAC2: |
Bharat Bhushan | 547465e | 2013-07-04 12:27:46 +0530 | [diff] [blame] | 1595 | vcpu->arch.dbg_reg.dac2 = set_reg_val(reg->id, val); |
Bharat Bhushan | 6df8d3f | 2012-08-08 21:17:55 +0000 | [diff] [blame] | 1596 | break; |
Alexander Graf | 324b3e6 | 2013-01-04 18:28:51 +0100 | [diff] [blame] | 1597 | case KVM_REG_PPC_EPR: { |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1598 | u32 new_epr = set_reg_val(reg->id, val); |
| 1599 | kvmppc_set_epr(vcpu, new_epr); |
Alexander Graf | 324b3e6 | 2013-01-04 18:28:51 +0100 | [diff] [blame] | 1600 | break; |
| 1601 | } |
Mihai Caraman | 352df1d | 2012-10-11 06:13:29 +0000 | [diff] [blame] | 1602 | #if defined(CONFIG_64BIT) |
| 1603 | case KVM_REG_PPC_EPCR: { |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1604 | u32 new_epcr = set_reg_val(reg->id, val); |
| 1605 | kvmppc_set_epcr(vcpu, new_epcr); |
Mihai Caraman | 352df1d | 2012-10-11 06:13:29 +0000 | [diff] [blame] | 1606 | break; |
| 1607 | } |
| 1608 | #endif |
Bharat Bhushan | 78accda | 2013-02-24 18:57:12 +0000 | [diff] [blame] | 1609 | case KVM_REG_PPC_OR_TSR: { |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1610 | u32 tsr_bits = set_reg_val(reg->id, val); |
Bharat Bhushan | 78accda | 2013-02-24 18:57:12 +0000 | [diff] [blame] | 1611 | kvmppc_set_tsr_bits(vcpu, tsr_bits); |
| 1612 | break; |
| 1613 | } |
| 1614 | case KVM_REG_PPC_CLEAR_TSR: { |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1615 | u32 tsr_bits = set_reg_val(reg->id, val); |
Bharat Bhushan | 78accda | 2013-02-24 18:57:12 +0000 | [diff] [blame] | 1616 | kvmppc_clr_tsr_bits(vcpu, tsr_bits); |
| 1617 | break; |
| 1618 | } |
| 1619 | case KVM_REG_PPC_TSR: { |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1620 | u32 tsr = set_reg_val(reg->id, val); |
Bharat Bhushan | 78accda | 2013-02-24 18:57:12 +0000 | [diff] [blame] | 1621 | kvmppc_set_tsr(vcpu, tsr); |
| 1622 | break; |
| 1623 | } |
| 1624 | case KVM_REG_PPC_TCR: { |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1625 | u32 tcr = set_reg_val(reg->id, val); |
Bharat Bhushan | 78accda | 2013-02-24 18:57:12 +0000 | [diff] [blame] | 1626 | kvmppc_set_tcr(vcpu, tcr); |
| 1627 | break; |
| 1628 | } |
Paul Mackerras | 8b75cbb | 2013-09-20 14:52:37 +1000 | [diff] [blame] | 1629 | case KVM_REG_PPC_VRSAVE: |
| 1630 | vcpu->arch.vrsave = set_reg_val(reg->id, val); |
| 1631 | break; |
Bharat Bhushan | 6df8d3f | 2012-08-08 21:17:55 +0000 | [diff] [blame] | 1632 | default: |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 1633 | r = vcpu->kvm->arch.kvm_ops->set_one_reg(vcpu, reg->id, &val); |
Bharat Bhushan | 6df8d3f | 2012-08-08 21:17:55 +0000 | [diff] [blame] | 1634 | break; |
| 1635 | } |
Mihai Caraman | 35b299e | 2013-04-11 00:03:07 +0000 | [diff] [blame] | 1636 | |
Bharat Bhushan | 6df8d3f | 2012-08-08 21:17:55 +0000 | [diff] [blame] | 1637 | return r; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1640 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 1641 | { |
| 1642 | return -ENOTSUPP; |
| 1643 | } |
| 1644 | |
| 1645 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 1646 | { |
| 1647 | return -ENOTSUPP; |
| 1648 | } |
| 1649 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1650 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 1651 | struct kvm_translation *tr) |
| 1652 | { |
Avi Kivity | 98001d8 | 2010-05-13 11:05:49 +0300 | [diff] [blame] | 1653 | int r; |
| 1654 | |
Avi Kivity | 98001d8 | 2010-05-13 11:05:49 +0300 | [diff] [blame] | 1655 | r = kvmppc_core_vcpu_translate(vcpu, tr); |
Avi Kivity | 98001d8 | 2010-05-13 11:05:49 +0300 | [diff] [blame] | 1656 | return r; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 1657 | } |
Hollis Blanchard | d9fbd03 | 2008-11-05 09:36:13 -0600 | [diff] [blame] | 1658 | |
Alexander Graf | 4e75575 | 2009-10-30 05:47:01 +0000 | [diff] [blame] | 1659 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) |
| 1660 | { |
| 1661 | return -ENOTSUPP; |
| 1662 | } |
| 1663 | |
Aneesh Kumar K.V | 5587027 | 2013-10-07 22:18:00 +0530 | [diff] [blame] | 1664 | void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free, |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 1665 | struct kvm_memory_slot *dont) |
| 1666 | { |
| 1667 | } |
| 1668 | |
Aneesh Kumar K.V | 5587027 | 2013-10-07 22:18:00 +0530 | [diff] [blame] | 1669 | int kvmppc_core_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 1670 | unsigned long npages) |
| 1671 | { |
| 1672 | return 0; |
| 1673 | } |
| 1674 | |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 1675 | int kvmppc_core_prepare_memory_region(struct kvm *kvm, |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 1676 | struct kvm_memory_slot *memslot, |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 1677 | struct kvm_userspace_memory_region *mem) |
| 1678 | { |
| 1679 | return 0; |
| 1680 | } |
| 1681 | |
| 1682 | void kvmppc_core_commit_memory_region(struct kvm *kvm, |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 1683 | struct kvm_userspace_memory_region *mem, |
Takuya Yoshikawa | 8482644 | 2013-02-27 19:45:25 +0900 | [diff] [blame] | 1684 | const struct kvm_memory_slot *old) |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 1685 | { |
| 1686 | } |
| 1687 | |
| 1688 | void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot) |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 1689 | { |
| 1690 | } |
| 1691 | |
Mihai Caraman | 38f9882 | 2012-10-11 06:13:27 +0000 | [diff] [blame] | 1692 | void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr) |
| 1693 | { |
| 1694 | #if defined(CONFIG_64BIT) |
| 1695 | vcpu->arch.epcr = new_epcr; |
| 1696 | #ifdef CONFIG_KVM_BOOKE_HV |
| 1697 | vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM; |
| 1698 | if (vcpu->arch.epcr & SPRN_EPCR_ICM) |
| 1699 | vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM; |
| 1700 | #endif |
| 1701 | #endif |
| 1702 | } |
| 1703 | |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 1704 | void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr) |
| 1705 | { |
| 1706 | vcpu->arch.tcr = new_tcr; |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 1707 | arm_next_watchdog(vcpu); |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 1708 | update_timer_ints(vcpu); |
| 1709 | } |
| 1710 | |
| 1711 | void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits) |
| 1712 | { |
| 1713 | set_bits(tsr_bits, &vcpu->arch.tsr); |
| 1714 | smp_wmb(); |
| 1715 | kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu); |
| 1716 | kvm_vcpu_kick(vcpu); |
| 1717 | } |
| 1718 | |
| 1719 | void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits) |
| 1720 | { |
| 1721 | clear_bits(tsr_bits, &vcpu->arch.tsr); |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 1722 | |
| 1723 | /* |
| 1724 | * We may have stopped the watchdog due to |
| 1725 | * being stuck on final expiration. |
| 1726 | */ |
| 1727 | if (tsr_bits & (TSR_ENW | TSR_WIS)) |
| 1728 | arm_next_watchdog(vcpu); |
| 1729 | |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 1730 | update_timer_ints(vcpu); |
| 1731 | } |
| 1732 | |
| 1733 | void kvmppc_decrementer_func(unsigned long data) |
| 1734 | { |
| 1735 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; |
| 1736 | |
Bharat Bhushan | 21bd000 | 2012-05-20 23:21:23 +0000 | [diff] [blame] | 1737 | if (vcpu->arch.tcr & TCR_ARE) { |
| 1738 | vcpu->arch.dec = vcpu->arch.decar; |
| 1739 | kvmppc_emulate_dec(vcpu); |
| 1740 | } |
| 1741 | |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 1742 | kvmppc_set_tsr_bits(vcpu, TSR_DIS); |
| 1743 | } |
| 1744 | |
Bharat Bhushan | ce11e48 | 2013-07-04 12:27:47 +0530 | [diff] [blame] | 1745 | static int kvmppc_booke_add_breakpoint(struct debug_reg *dbg_reg, |
| 1746 | uint64_t addr, int index) |
| 1747 | { |
| 1748 | switch (index) { |
| 1749 | case 0: |
| 1750 | dbg_reg->dbcr0 |= DBCR0_IAC1; |
| 1751 | dbg_reg->iac1 = addr; |
| 1752 | break; |
| 1753 | case 1: |
| 1754 | dbg_reg->dbcr0 |= DBCR0_IAC2; |
| 1755 | dbg_reg->iac2 = addr; |
| 1756 | break; |
| 1757 | #if CONFIG_PPC_ADV_DEBUG_IACS > 2 |
| 1758 | case 2: |
| 1759 | dbg_reg->dbcr0 |= DBCR0_IAC3; |
| 1760 | dbg_reg->iac3 = addr; |
| 1761 | break; |
| 1762 | case 3: |
| 1763 | dbg_reg->dbcr0 |= DBCR0_IAC4; |
| 1764 | dbg_reg->iac4 = addr; |
| 1765 | break; |
| 1766 | #endif |
| 1767 | default: |
| 1768 | return -EINVAL; |
| 1769 | } |
| 1770 | |
| 1771 | dbg_reg->dbcr0 |= DBCR0_IDM; |
| 1772 | return 0; |
| 1773 | } |
| 1774 | |
| 1775 | static int kvmppc_booke_add_watchpoint(struct debug_reg *dbg_reg, uint64_t addr, |
| 1776 | int type, int index) |
| 1777 | { |
| 1778 | switch (index) { |
| 1779 | case 0: |
| 1780 | if (type & KVMPPC_DEBUG_WATCH_READ) |
| 1781 | dbg_reg->dbcr0 |= DBCR0_DAC1R; |
| 1782 | if (type & KVMPPC_DEBUG_WATCH_WRITE) |
| 1783 | dbg_reg->dbcr0 |= DBCR0_DAC1W; |
| 1784 | dbg_reg->dac1 = addr; |
| 1785 | break; |
| 1786 | case 1: |
| 1787 | if (type & KVMPPC_DEBUG_WATCH_READ) |
| 1788 | dbg_reg->dbcr0 |= DBCR0_DAC2R; |
| 1789 | if (type & KVMPPC_DEBUG_WATCH_WRITE) |
| 1790 | dbg_reg->dbcr0 |= DBCR0_DAC2W; |
| 1791 | dbg_reg->dac2 = addr; |
| 1792 | break; |
| 1793 | default: |
| 1794 | return -EINVAL; |
| 1795 | } |
| 1796 | |
| 1797 | dbg_reg->dbcr0 |= DBCR0_IDM; |
| 1798 | return 0; |
| 1799 | } |
| 1800 | void kvm_guest_protect_msr(struct kvm_vcpu *vcpu, ulong prot_bitmap, bool set) |
| 1801 | { |
| 1802 | /* XXX: Add similar MSR protection for BookE-PR */ |
| 1803 | #ifdef CONFIG_KVM_BOOKE_HV |
| 1804 | BUG_ON(prot_bitmap & ~(MSRP_UCLEP | MSRP_DEP | MSRP_PMMP)); |
| 1805 | if (set) { |
| 1806 | if (prot_bitmap & MSR_UCLE) |
| 1807 | vcpu->arch.shadow_msrp |= MSRP_UCLEP; |
| 1808 | if (prot_bitmap & MSR_DE) |
| 1809 | vcpu->arch.shadow_msrp |= MSRP_DEP; |
| 1810 | if (prot_bitmap & MSR_PMM) |
| 1811 | vcpu->arch.shadow_msrp |= MSRP_PMMP; |
| 1812 | } else { |
| 1813 | if (prot_bitmap & MSR_UCLE) |
| 1814 | vcpu->arch.shadow_msrp &= ~MSRP_UCLEP; |
| 1815 | if (prot_bitmap & MSR_DE) |
| 1816 | vcpu->arch.shadow_msrp &= ~MSRP_DEP; |
| 1817 | if (prot_bitmap & MSR_PMM) |
| 1818 | vcpu->arch.shadow_msrp &= ~MSRP_PMMP; |
| 1819 | } |
| 1820 | #endif |
| 1821 | } |
| 1822 | |
| 1823 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| 1824 | struct kvm_guest_debug *dbg) |
| 1825 | { |
| 1826 | struct debug_reg *dbg_reg; |
| 1827 | int n, b = 0, w = 0; |
| 1828 | |
| 1829 | if (!(dbg->control & KVM_GUESTDBG_ENABLE)) { |
| 1830 | vcpu->arch.shadow_dbg_reg.dbcr0 = 0; |
| 1831 | vcpu->guest_debug = 0; |
| 1832 | kvm_guest_protect_msr(vcpu, MSR_DE, false); |
| 1833 | return 0; |
| 1834 | } |
| 1835 | |
| 1836 | kvm_guest_protect_msr(vcpu, MSR_DE, true); |
| 1837 | vcpu->guest_debug = dbg->control; |
| 1838 | vcpu->arch.shadow_dbg_reg.dbcr0 = 0; |
| 1839 | /* Set DBCR0_EDM in guest visible DBCR0 register. */ |
| 1840 | vcpu->arch.dbg_reg.dbcr0 = DBCR0_EDM; |
| 1841 | |
| 1842 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
| 1843 | vcpu->arch.shadow_dbg_reg.dbcr0 |= DBCR0_IDM | DBCR0_IC; |
| 1844 | |
| 1845 | /* Code below handles only HW breakpoints */ |
| 1846 | dbg_reg = &(vcpu->arch.shadow_dbg_reg); |
| 1847 | |
| 1848 | #ifdef CONFIG_KVM_BOOKE_HV |
| 1849 | /* |
| 1850 | * On BookE-HV (e500mc) the guest is always executed with MSR.GS=1 |
| 1851 | * DBCR1 and DBCR2 are set to trigger debug events when MSR.PR is 0 |
| 1852 | */ |
| 1853 | dbg_reg->dbcr1 = 0; |
| 1854 | dbg_reg->dbcr2 = 0; |
| 1855 | #else |
| 1856 | /* |
| 1857 | * On BookE-PR (e500v2) the guest is always executed with MSR.PR=1 |
| 1858 | * We set DBCR1 and DBCR2 to only trigger debug events when MSR.PR |
| 1859 | * is set. |
| 1860 | */ |
| 1861 | dbg_reg->dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US | DBCR1_IAC3US | |
| 1862 | DBCR1_IAC4US; |
| 1863 | dbg_reg->dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US; |
| 1864 | #endif |
| 1865 | |
| 1866 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) |
| 1867 | return 0; |
| 1868 | |
| 1869 | for (n = 0; n < (KVMPPC_BOOKE_IAC_NUM + KVMPPC_BOOKE_DAC_NUM); n++) { |
| 1870 | uint64_t addr = dbg->arch.bp[n].addr; |
| 1871 | uint32_t type = dbg->arch.bp[n].type; |
| 1872 | |
| 1873 | if (type == KVMPPC_DEBUG_NONE) |
| 1874 | continue; |
| 1875 | |
| 1876 | if (type & !(KVMPPC_DEBUG_WATCH_READ | |
| 1877 | KVMPPC_DEBUG_WATCH_WRITE | |
| 1878 | KVMPPC_DEBUG_BREAKPOINT)) |
| 1879 | return -EINVAL; |
| 1880 | |
| 1881 | if (type & KVMPPC_DEBUG_BREAKPOINT) { |
| 1882 | /* Setting H/W breakpoint */ |
| 1883 | if (kvmppc_booke_add_breakpoint(dbg_reg, addr, b++)) |
| 1884 | return -EINVAL; |
| 1885 | } else { |
| 1886 | /* Setting H/W watchpoint */ |
| 1887 | if (kvmppc_booke_add_watchpoint(dbg_reg, addr, |
| 1888 | type, w++)) |
| 1889 | return -EINVAL; |
| 1890 | } |
| 1891 | } |
| 1892 | |
| 1893 | return 0; |
| 1894 | } |
| 1895 | |
Scott Wood | 94fa9d9 | 2011-12-20 15:34:22 +0000 | [diff] [blame] | 1896 | void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 1897 | { |
Paul Mackerras | a47d72f | 2012-09-20 19:35:51 +0000 | [diff] [blame] | 1898 | vcpu->cpu = smp_processor_id(); |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 1899 | current->thread.kvm_vcpu = vcpu; |
Scott Wood | 94fa9d9 | 2011-12-20 15:34:22 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu) |
| 1903 | { |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 1904 | current->thread.kvm_vcpu = NULL; |
Paul Mackerras | a47d72f | 2012-09-20 19:35:51 +0000 | [diff] [blame] | 1905 | vcpu->cpu = -1; |
Bharat Bhushan | ce11e48 | 2013-07-04 12:27:47 +0530 | [diff] [blame] | 1906 | |
| 1907 | /* Clear pending debug event in DBSR */ |
| 1908 | kvmppc_clear_dbsr(); |
Scott Wood | 94fa9d9 | 2011-12-20 15:34:22 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1911 | void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu) |
| 1912 | { |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 1913 | vcpu->kvm->arch.kvm_ops->mmu_destroy(vcpu); |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | int kvmppc_core_init_vm(struct kvm *kvm) |
| 1917 | { |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 1918 | return kvm->arch.kvm_ops->init_vm(kvm); |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id) |
| 1922 | { |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 1923 | return kvm->arch.kvm_ops->vcpu_create(kvm, id); |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) |
| 1927 | { |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 1928 | vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu); |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | void kvmppc_core_destroy_vm(struct kvm *kvm) |
| 1932 | { |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 1933 | kvm->arch.kvm_ops->destroy_vm(kvm); |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 1937 | { |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 1938 | vcpu->kvm->arch.kvm_ops->vcpu_load(vcpu, cpu); |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu) |
| 1942 | { |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 1943 | vcpu->kvm->arch.kvm_ops->vcpu_put(vcpu); |
Hollis Blanchard | d9fbd03 | 2008-11-05 09:36:13 -0600 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | int __init kvmppc_booke_init(void) |
| 1947 | { |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 1948 | #ifndef CONFIG_KVM_BOOKE_HV |
Hollis Blanchard | d9fbd03 | 2008-11-05 09:36:13 -0600 | [diff] [blame] | 1949 | unsigned long ivor[16]; |
Bharat Bhushan | 1d542d9 | 2013-01-15 22:24:39 +0000 | [diff] [blame] | 1950 | unsigned long *handler = kvmppc_booke_handler_addr; |
Hollis Blanchard | d9fbd03 | 2008-11-05 09:36:13 -0600 | [diff] [blame] | 1951 | unsigned long max_ivor = 0; |
Bharat Bhushan | 1d542d9 | 2013-01-15 22:24:39 +0000 | [diff] [blame] | 1952 | unsigned long handler_len; |
Hollis Blanchard | d9fbd03 | 2008-11-05 09:36:13 -0600 | [diff] [blame] | 1953 | int i; |
| 1954 | |
| 1955 | /* We install our own exception handlers by hijacking IVPR. IVPR must |
| 1956 | * be 16-bit aligned, so we need a 64KB allocation. */ |
| 1957 | kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 1958 | VCPU_SIZE_ORDER); |
| 1959 | if (!kvmppc_booke_handlers) |
| 1960 | return -ENOMEM; |
| 1961 | |
| 1962 | /* XXX make sure our handlers are smaller than Linux's */ |
| 1963 | |
| 1964 | /* Copy our interrupt handlers to match host IVORs. That way we don't |
| 1965 | * have to swap the IVORs on every guest/host transition. */ |
| 1966 | ivor[0] = mfspr(SPRN_IVOR0); |
| 1967 | ivor[1] = mfspr(SPRN_IVOR1); |
| 1968 | ivor[2] = mfspr(SPRN_IVOR2); |
| 1969 | ivor[3] = mfspr(SPRN_IVOR3); |
| 1970 | ivor[4] = mfspr(SPRN_IVOR4); |
| 1971 | ivor[5] = mfspr(SPRN_IVOR5); |
| 1972 | ivor[6] = mfspr(SPRN_IVOR6); |
| 1973 | ivor[7] = mfspr(SPRN_IVOR7); |
| 1974 | ivor[8] = mfspr(SPRN_IVOR8); |
| 1975 | ivor[9] = mfspr(SPRN_IVOR9); |
| 1976 | ivor[10] = mfspr(SPRN_IVOR10); |
| 1977 | ivor[11] = mfspr(SPRN_IVOR11); |
| 1978 | ivor[12] = mfspr(SPRN_IVOR12); |
| 1979 | ivor[13] = mfspr(SPRN_IVOR13); |
| 1980 | ivor[14] = mfspr(SPRN_IVOR14); |
| 1981 | ivor[15] = mfspr(SPRN_IVOR15); |
| 1982 | |
| 1983 | for (i = 0; i < 16; i++) { |
| 1984 | if (ivor[i] > max_ivor) |
Bharat Bhushan | 1d542d9 | 2013-01-15 22:24:39 +0000 | [diff] [blame] | 1985 | max_ivor = i; |
Hollis Blanchard | d9fbd03 | 2008-11-05 09:36:13 -0600 | [diff] [blame] | 1986 | |
Bharat Bhushan | 1d542d9 | 2013-01-15 22:24:39 +0000 | [diff] [blame] | 1987 | handler_len = handler[i + 1] - handler[i]; |
Hollis Blanchard | d9fbd03 | 2008-11-05 09:36:13 -0600 | [diff] [blame] | 1988 | memcpy((void *)kvmppc_booke_handlers + ivor[i], |
Bharat Bhushan | 1d542d9 | 2013-01-15 22:24:39 +0000 | [diff] [blame] | 1989 | (void *)handler[i], handler_len); |
Hollis Blanchard | d9fbd03 | 2008-11-05 09:36:13 -0600 | [diff] [blame] | 1990 | } |
Bharat Bhushan | 1d542d9 | 2013-01-15 22:24:39 +0000 | [diff] [blame] | 1991 | |
| 1992 | handler_len = handler[max_ivor + 1] - handler[max_ivor]; |
| 1993 | flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers + |
| 1994 | ivor[max_ivor] + handler_len); |
Scott Wood | d30f6e4 | 2011-12-20 15:34:43 +0000 | [diff] [blame] | 1995 | #endif /* !BOOKE_HV */ |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 1996 | return 0; |
Hollis Blanchard | d9fbd03 | 2008-11-05 09:36:13 -0600 | [diff] [blame] | 1997 | } |
| 1998 | |
Hollis Blanchard | db93f57 | 2008-11-05 09:36:18 -0600 | [diff] [blame] | 1999 | void __exit kvmppc_booke_exit(void) |
Hollis Blanchard | d9fbd03 | 2008-11-05 09:36:13 -0600 | [diff] [blame] | 2000 | { |
| 2001 | free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER); |
| 2002 | kvm_exit(); |
| 2003 | } |