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