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