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