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