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