Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved. |
| 3 | * |
| 4 | * Authors: |
| 5 | * Alexander Graf <agraf@suse.de> |
| 6 | * Kevin Wolf <mail@kevin-wolf.de> |
| 7 | * |
| 8 | * Description: |
| 9 | * This file is derived from arch/powerpc/kvm/44x.c, |
| 10 | * by Hollis Blanchard <hollisb@us.ibm.com>. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License, version 2, as |
| 14 | * published by the Free Software Foundation. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/kvm_host.h> |
| 18 | #include <linux/err.h> |
| 19 | |
| 20 | #include <asm/reg.h> |
| 21 | #include <asm/cputable.h> |
| 22 | #include <asm/cacheflush.h> |
| 23 | #include <asm/tlbflush.h> |
| 24 | #include <asm/uaccess.h> |
| 25 | #include <asm/io.h> |
| 26 | #include <asm/kvm_ppc.h> |
| 27 | #include <asm/kvm_book3s.h> |
| 28 | #include <asm/mmu_context.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/gfp.h> |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 30 | #include <linux/sched.h> |
| 31 | #include <linux/vmalloc.h> |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 32 | #include <linux/highmem.h> |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 33 | |
| 34 | #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU |
| 35 | |
| 36 | /* #define EXIT_DEBUG */ |
| 37 | /* #define EXIT_DEBUG_SIMPLE */ |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 38 | /* #define DEBUG_EXT */ |
| 39 | |
Alexander Graf | c8c0b6f | 2010-02-19 11:00:34 +0100 | [diff] [blame] | 40 | static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr, |
| 41 | ulong msr); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 42 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 43 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
| 44 | { "exits", VCPU_STAT(sum_exits) }, |
| 45 | { "mmio", VCPU_STAT(mmio_exits) }, |
| 46 | { "sig", VCPU_STAT(signal_exits) }, |
| 47 | { "sysc", VCPU_STAT(syscall_exits) }, |
| 48 | { "inst_emu", VCPU_STAT(emulated_inst_exits) }, |
| 49 | { "dec", VCPU_STAT(dec_exits) }, |
| 50 | { "ext_intr", VCPU_STAT(ext_intr_exits) }, |
| 51 | { "queue_intr", VCPU_STAT(queue_intr) }, |
| 52 | { "halt_wakeup", VCPU_STAT(halt_wakeup) }, |
| 53 | { "pf_storage", VCPU_STAT(pf_storage) }, |
| 54 | { "sp_storage", VCPU_STAT(sp_storage) }, |
| 55 | { "pf_instruc", VCPU_STAT(pf_instruc) }, |
| 56 | { "sp_instruc", VCPU_STAT(sp_instruc) }, |
| 57 | { "ld", VCPU_STAT(ld) }, |
| 58 | { "ld_slow", VCPU_STAT(ld_slow) }, |
| 59 | { "st", VCPU_STAT(st) }, |
| 60 | { "st_slow", VCPU_STAT(st_slow) }, |
| 61 | { NULL } |
| 62 | }; |
| 63 | |
| 64 | void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu) |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu) |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
| 73 | { |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 74 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 75 | memcpy(to_svcpu(vcpu)->slb, to_book3s(vcpu)->slb_shadow, sizeof(to_svcpu(vcpu)->slb)); |
| 76 | memcpy(&get_paca()->shadow_vcpu, to_book3s(vcpu)->shadow_vcpu, |
Alexander Graf | 7e57cba | 2010-01-08 02:58:03 +0100 | [diff] [blame] | 77 | sizeof(get_paca()->shadow_vcpu)); |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 78 | to_svcpu(vcpu)->slb_max = to_book3s(vcpu)->slb_shadow_max; |
| 79 | #endif |
| 80 | |
| 81 | #ifdef CONFIG_PPC_BOOK3S_32 |
| 82 | current->thread.kvm_shadow_vcpu = to_book3s(vcpu)->shadow_vcpu; |
| 83 | #endif |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu) |
| 87 | { |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 88 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 89 | memcpy(to_book3s(vcpu)->slb_shadow, to_svcpu(vcpu)->slb, sizeof(to_svcpu(vcpu)->slb)); |
| 90 | memcpy(to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu, |
Alexander Graf | 7e57cba | 2010-01-08 02:58:03 +0100 | [diff] [blame] | 91 | sizeof(get_paca()->shadow_vcpu)); |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 92 | to_book3s(vcpu)->slb_shadow_max = to_svcpu(vcpu)->slb_max; |
| 93 | #endif |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 94 | |
| 95 | kvmppc_giveup_ext(vcpu, MSR_FP); |
| 96 | kvmppc_giveup_ext(vcpu, MSR_VEC); |
| 97 | kvmppc_giveup_ext(vcpu, MSR_VSX); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Alexander Graf | 0bb1fb7 | 2009-12-21 20:21:25 +0100 | [diff] [blame] | 100 | #if defined(EXIT_DEBUG) |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 101 | static u32 kvmppc_get_dec(struct kvm_vcpu *vcpu) |
| 102 | { |
| 103 | u64 jd = mftb() - vcpu->arch.dec_jiffies; |
| 104 | return vcpu->arch.dec - jd; |
| 105 | } |
| 106 | #endif |
| 107 | |
Alexander Graf | a76f849 | 2010-01-15 14:49:14 +0100 | [diff] [blame] | 108 | static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu) |
| 109 | { |
| 110 | vcpu->arch.shadow_msr = vcpu->arch.msr; |
| 111 | /* Guest MSR values */ |
| 112 | vcpu->arch.shadow_msr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | |
| 113 | MSR_BE | MSR_DE; |
| 114 | /* Process MSR values */ |
| 115 | vcpu->arch.shadow_msr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | |
| 116 | MSR_EE; |
| 117 | /* External providers the guest reserved */ |
| 118 | vcpu->arch.shadow_msr |= (vcpu->arch.msr & vcpu->arch.guest_owned_ext); |
| 119 | /* 64-bit Process MSR values */ |
| 120 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 121 | vcpu->arch.shadow_msr |= MSR_ISF | MSR_HV; |
| 122 | #endif |
| 123 | } |
| 124 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 125 | void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr) |
| 126 | { |
| 127 | ulong old_msr = vcpu->arch.msr; |
| 128 | |
| 129 | #ifdef EXIT_DEBUG |
| 130 | printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr); |
| 131 | #endif |
Alexander Graf | a76f849 | 2010-01-15 14:49:14 +0100 | [diff] [blame] | 132 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 133 | msr &= to_book3s(vcpu)->msr_mask; |
| 134 | vcpu->arch.msr = msr; |
Alexander Graf | a76f849 | 2010-01-15 14:49:14 +0100 | [diff] [blame] | 135 | kvmppc_recalc_shadow_msr(vcpu); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 136 | |
| 137 | if (msr & (MSR_WE|MSR_POW)) { |
| 138 | if (!vcpu->arch.pending_exceptions) { |
| 139 | kvm_vcpu_block(vcpu); |
| 140 | vcpu->stat.halt_wakeup++; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (((vcpu->arch.msr & (MSR_IR|MSR_DR)) != (old_msr & (MSR_IR|MSR_DR))) || |
| 145 | (vcpu->arch.msr & MSR_PR) != (old_msr & MSR_PR)) { |
Alexander Graf | 3eeafd7 | 2010-03-24 21:48:17 +0100 | [diff] [blame] | 146 | bool dr = (vcpu->arch.msr & MSR_DR) ? true : false; |
| 147 | bool ir = (vcpu->arch.msr & MSR_IR) ? true : false; |
| 148 | |
| 149 | /* Flush split mode PTEs */ |
| 150 | if (dr != ir) |
| 151 | kvmppc_mmu_pte_vflush(vcpu, VSID_SPLIT_MASK, |
| 152 | VSID_SPLIT_MASK); |
| 153 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 154 | kvmppc_mmu_flush_segments(vcpu); |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 155 | kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 156 | } |
Alexander Graf | d1bab74 | 2010-02-19 11:00:35 +0100 | [diff] [blame] | 157 | |
| 158 | /* Preload FPU if it's enabled */ |
| 159 | if (vcpu->arch.msr & MSR_FP) |
| 160 | kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags) |
| 164 | { |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 165 | vcpu->arch.srr0 = kvmppc_get_pc(vcpu); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 166 | vcpu->arch.srr1 = vcpu->arch.msr | flags; |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 167 | kvmppc_set_pc(vcpu, to_book3s(vcpu)->hior + vec); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 168 | vcpu->arch.mmu.reset_msr(vcpu); |
| 169 | } |
| 170 | |
Alexander Graf | 583617b | 2009-12-21 20:21:23 +0100 | [diff] [blame] | 171 | static int kvmppc_book3s_vec2irqprio(unsigned int vec) |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 172 | { |
| 173 | unsigned int prio; |
| 174 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 175 | switch (vec) { |
| 176 | case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break; |
| 177 | case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break; |
| 178 | case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break; |
| 179 | case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break; |
| 180 | case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break; |
| 181 | case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break; |
| 182 | case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break; |
| 183 | case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break; |
| 184 | case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break; |
| 185 | case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break; |
| 186 | case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break; |
| 187 | case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break; |
| 188 | case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break; |
| 189 | case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break; |
| 190 | case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break; |
| 191 | default: prio = BOOK3S_IRQPRIO_MAX; break; |
| 192 | } |
| 193 | |
Alexander Graf | 583617b | 2009-12-21 20:21:23 +0100 | [diff] [blame] | 194 | return prio; |
| 195 | } |
| 196 | |
Alexander Graf | 7706664d | 2009-12-21 20:21:24 +0100 | [diff] [blame] | 197 | static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu, |
| 198 | unsigned int vec) |
| 199 | { |
| 200 | clear_bit(kvmppc_book3s_vec2irqprio(vec), |
| 201 | &vcpu->arch.pending_exceptions); |
| 202 | } |
| 203 | |
Alexander Graf | 583617b | 2009-12-21 20:21:23 +0100 | [diff] [blame] | 204 | void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec) |
| 205 | { |
| 206 | vcpu->stat.queue_intr++; |
| 207 | |
| 208 | set_bit(kvmppc_book3s_vec2irqprio(vec), |
| 209 | &vcpu->arch.pending_exceptions); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 210 | #ifdef EXIT_DEBUG |
| 211 | printk(KERN_INFO "Queueing interrupt %x\n", vec); |
| 212 | #endif |
| 213 | } |
| 214 | |
| 215 | |
Alexander Graf | 25a8a02 | 2010-01-08 02:58:07 +0100 | [diff] [blame] | 216 | void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags) |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 217 | { |
Alexander Graf | 25a8a02 | 2010-01-08 02:58:07 +0100 | [diff] [blame] | 218 | to_book3s(vcpu)->prog_flags = flags; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 219 | kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM); |
| 220 | } |
| 221 | |
| 222 | void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu) |
| 223 | { |
| 224 | kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER); |
| 225 | } |
| 226 | |
| 227 | int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu) |
| 228 | { |
| 229 | return test_bit(BOOK3S_INTERRUPT_DECREMENTER >> 7, &vcpu->arch.pending_exceptions); |
| 230 | } |
| 231 | |
Alexander Graf | 7706664d | 2009-12-21 20:21:24 +0100 | [diff] [blame] | 232 | void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu) |
| 233 | { |
| 234 | kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER); |
| 235 | } |
| 236 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 237 | void kvmppc_core_queue_external(struct kvm_vcpu *vcpu, |
| 238 | struct kvm_interrupt *irq) |
| 239 | { |
| 240 | kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL); |
| 241 | } |
| 242 | |
Alexander Graf | 1897876 | 2010-03-24 21:48:18 +0100 | [diff] [blame] | 243 | void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu, |
| 244 | struct kvm_interrupt *irq) |
| 245 | { |
| 246 | kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL); |
| 247 | } |
| 248 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 249 | int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority) |
| 250 | { |
| 251 | int deliver = 1; |
| 252 | int vec = 0; |
Alexander Graf | 25a8a02 | 2010-01-08 02:58:07 +0100 | [diff] [blame] | 253 | ulong flags = 0ULL; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 254 | |
| 255 | switch (priority) { |
| 256 | case BOOK3S_IRQPRIO_DECREMENTER: |
| 257 | deliver = vcpu->arch.msr & MSR_EE; |
| 258 | vec = BOOK3S_INTERRUPT_DECREMENTER; |
| 259 | break; |
| 260 | case BOOK3S_IRQPRIO_EXTERNAL: |
| 261 | deliver = vcpu->arch.msr & MSR_EE; |
| 262 | vec = BOOK3S_INTERRUPT_EXTERNAL; |
| 263 | break; |
| 264 | case BOOK3S_IRQPRIO_SYSTEM_RESET: |
| 265 | vec = BOOK3S_INTERRUPT_SYSTEM_RESET; |
| 266 | break; |
| 267 | case BOOK3S_IRQPRIO_MACHINE_CHECK: |
| 268 | vec = BOOK3S_INTERRUPT_MACHINE_CHECK; |
| 269 | break; |
| 270 | case BOOK3S_IRQPRIO_DATA_STORAGE: |
| 271 | vec = BOOK3S_INTERRUPT_DATA_STORAGE; |
| 272 | break; |
| 273 | case BOOK3S_IRQPRIO_INST_STORAGE: |
| 274 | vec = BOOK3S_INTERRUPT_INST_STORAGE; |
| 275 | break; |
| 276 | case BOOK3S_IRQPRIO_DATA_SEGMENT: |
| 277 | vec = BOOK3S_INTERRUPT_DATA_SEGMENT; |
| 278 | break; |
| 279 | case BOOK3S_IRQPRIO_INST_SEGMENT: |
| 280 | vec = BOOK3S_INTERRUPT_INST_SEGMENT; |
| 281 | break; |
| 282 | case BOOK3S_IRQPRIO_ALIGNMENT: |
| 283 | vec = BOOK3S_INTERRUPT_ALIGNMENT; |
| 284 | break; |
| 285 | case BOOK3S_IRQPRIO_PROGRAM: |
| 286 | vec = BOOK3S_INTERRUPT_PROGRAM; |
Alexander Graf | 25a8a02 | 2010-01-08 02:58:07 +0100 | [diff] [blame] | 287 | flags = to_book3s(vcpu)->prog_flags; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 288 | break; |
| 289 | case BOOK3S_IRQPRIO_VSX: |
| 290 | vec = BOOK3S_INTERRUPT_VSX; |
| 291 | break; |
| 292 | case BOOK3S_IRQPRIO_ALTIVEC: |
| 293 | vec = BOOK3S_INTERRUPT_ALTIVEC; |
| 294 | break; |
| 295 | case BOOK3S_IRQPRIO_FP_UNAVAIL: |
| 296 | vec = BOOK3S_INTERRUPT_FP_UNAVAIL; |
| 297 | break; |
| 298 | case BOOK3S_IRQPRIO_SYSCALL: |
| 299 | vec = BOOK3S_INTERRUPT_SYSCALL; |
| 300 | break; |
| 301 | case BOOK3S_IRQPRIO_DEBUG: |
| 302 | vec = BOOK3S_INTERRUPT_TRACE; |
| 303 | break; |
| 304 | case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR: |
| 305 | vec = BOOK3S_INTERRUPT_PERFMON; |
| 306 | break; |
| 307 | default: |
| 308 | deliver = 0; |
| 309 | printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority); |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | #if 0 |
| 314 | printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver); |
| 315 | #endif |
| 316 | |
| 317 | if (deliver) |
Alexander Graf | 25a8a02 | 2010-01-08 02:58:07 +0100 | [diff] [blame] | 318 | kvmppc_inject_interrupt(vcpu, vec, flags); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 319 | |
| 320 | return deliver; |
| 321 | } |
| 322 | |
| 323 | void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu) |
| 324 | { |
| 325 | unsigned long *pending = &vcpu->arch.pending_exceptions; |
| 326 | unsigned int priority; |
| 327 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 328 | #ifdef EXIT_DEBUG |
| 329 | if (vcpu->arch.pending_exceptions) |
| 330 | printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions); |
| 331 | #endif |
| 332 | priority = __ffs(*pending); |
| 333 | while (priority <= (sizeof(unsigned int) * 8)) { |
Alexander Graf | 7706664d | 2009-12-21 20:21:24 +0100 | [diff] [blame] | 334 | if (kvmppc_book3s_irqprio_deliver(vcpu, priority) && |
| 335 | (priority != BOOK3S_IRQPRIO_DECREMENTER)) { |
| 336 | /* DEC interrupts get cleared by mtdec */ |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 337 | clear_bit(priority, &vcpu->arch.pending_exceptions); |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | priority = find_next_bit(pending, |
| 342 | BITS_PER_BYTE * sizeof(*pending), |
| 343 | priority + 1); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr) |
| 348 | { |
Alexander Graf | e15a113 | 2009-11-30 03:02:02 +0000 | [diff] [blame] | 349 | vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 350 | vcpu->arch.pvr = pvr; |
| 351 | if ((pvr >= 0x330000) && (pvr < 0x70330000)) { |
| 352 | kvmppc_mmu_book3s_64_init(vcpu); |
| 353 | to_book3s(vcpu)->hior = 0xfff00000; |
| 354 | to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL; |
| 355 | } else { |
| 356 | kvmppc_mmu_book3s_32_init(vcpu); |
| 357 | to_book3s(vcpu)->hior = 0; |
| 358 | to_book3s(vcpu)->msr_mask = 0xffffffffULL; |
| 359 | } |
| 360 | |
| 361 | /* If we are in hypervisor level on 970, we can tell the CPU to |
| 362 | * treat DCBZ as 32 bytes store */ |
| 363 | vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32; |
| 364 | if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) && |
| 365 | !strcmp(cur_cpu_spec->platform, "ppc970")) |
| 366 | vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32; |
| 367 | |
Alexander Graf | 05b0ab1 | 2010-03-24 21:48:37 +0100 | [diff] [blame] | 368 | /* Cell performs badly if MSR_FEx are set. So let's hope nobody |
| 369 | really needs them in a VM on Cell and force disable them. */ |
| 370 | if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be")) |
| 371 | to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To |
| 375 | * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to |
| 376 | * emulate 32 bytes dcbz length. |
| 377 | * |
| 378 | * The Book3s_64 inventors also realized this case and implemented a special bit |
| 379 | * in the HID5 register, which is a hypervisor ressource. Thus we can't use it. |
| 380 | * |
| 381 | * My approach here is to patch the dcbz instruction on executing pages. |
| 382 | */ |
| 383 | static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte) |
| 384 | { |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 385 | struct page *hpage; |
| 386 | u64 hpage_offset; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 387 | u32 *page; |
| 388 | int i; |
| 389 | |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 390 | hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT); |
| 391 | if (is_error_page(hpage)) |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 392 | return; |
| 393 | |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 394 | hpage_offset = pte->raddr & ~PAGE_MASK; |
| 395 | hpage_offset &= ~0xFFFULL; |
| 396 | hpage_offset /= 4; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 397 | |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 398 | get_page(hpage); |
| 399 | page = kmap_atomic(hpage, KM_USER0); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 400 | |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 401 | /* patch dcbz into reserved instruction, so we trap */ |
| 402 | for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++) |
| 403 | if ((page[i] & 0xff0007ff) == INS_DCBZ) |
| 404 | page[i] &= 0xfffffff7; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 405 | |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 406 | kunmap_atomic(page, KM_USER0); |
| 407 | put_page(hpage); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data, |
| 411 | struct kvmppc_pte *pte) |
| 412 | { |
| 413 | int relocated = (vcpu->arch.msr & (data ? MSR_DR : MSR_IR)); |
| 414 | int r; |
| 415 | |
| 416 | if (relocated) { |
| 417 | r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data); |
| 418 | } else { |
| 419 | pte->eaddr = eaddr; |
| 420 | pte->raddr = eaddr & 0xffffffff; |
Alexander Graf | 3eeafd7 | 2010-03-24 21:48:17 +0100 | [diff] [blame] | 421 | pte->vpage = VSID_REAL | eaddr >> 12; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 422 | pte->may_read = true; |
| 423 | pte->may_write = true; |
| 424 | pte->may_execute = true; |
| 425 | r = 0; |
| 426 | } |
| 427 | |
| 428 | return r; |
| 429 | } |
| 430 | |
| 431 | static hva_t kvmppc_bad_hva(void) |
| 432 | { |
| 433 | return PAGE_OFFSET; |
| 434 | } |
| 435 | |
| 436 | static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte, |
| 437 | bool read) |
| 438 | { |
| 439 | hva_t hpage; |
| 440 | |
| 441 | if (read && !pte->may_read) |
| 442 | goto err; |
| 443 | |
| 444 | if (!read && !pte->may_write) |
| 445 | goto err; |
| 446 | |
| 447 | hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT); |
| 448 | if (kvm_is_error_hva(hpage)) |
| 449 | goto err; |
| 450 | |
| 451 | return hpage | (pte->raddr & ~PAGE_MASK); |
| 452 | err: |
| 453 | return kvmppc_bad_hva(); |
| 454 | } |
| 455 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 456 | int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, |
| 457 | bool data) |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 458 | { |
| 459 | struct kvmppc_pte pte; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 460 | |
| 461 | vcpu->stat.st++; |
| 462 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 463 | if (kvmppc_xlate(vcpu, *eaddr, data, &pte)) |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 464 | return -ENOENT; |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 465 | |
| 466 | *eaddr = pte.raddr; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 467 | |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 468 | if (!pte.may_write) |
| 469 | return -EPERM; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 470 | |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 471 | if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size)) |
| 472 | return EMULATE_DO_MMIO; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 473 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 474 | return EMULATE_DONE; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 477 | int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 478 | bool data) |
| 479 | { |
| 480 | struct kvmppc_pte pte; |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 481 | hva_t hva = *eaddr; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 482 | |
| 483 | vcpu->stat.ld++; |
| 484 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 485 | if (kvmppc_xlate(vcpu, *eaddr, data, &pte)) |
| 486 | goto nopte; |
| 487 | |
| 488 | *eaddr = pte.raddr; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 489 | |
| 490 | hva = kvmppc_pte_to_hva(vcpu, &pte, true); |
| 491 | if (kvm_is_error_hva(hva)) |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 492 | goto mmio; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 493 | |
| 494 | if (copy_from_user(ptr, (void __user *)hva, size)) { |
| 495 | printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva); |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 496 | goto mmio; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 499 | return EMULATE_DONE; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 500 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 501 | nopte: |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 502 | return -ENOENT; |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 503 | mmio: |
| 504 | return EMULATE_DO_MMIO; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 508 | { |
| 509 | return kvm_is_visible_gfn(vcpu->kvm, gfn); |
| 510 | } |
| 511 | |
| 512 | int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 513 | ulong eaddr, int vec) |
| 514 | { |
| 515 | bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE); |
| 516 | int r = RESUME_GUEST; |
| 517 | int relocated; |
| 518 | int page_found = 0; |
| 519 | struct kvmppc_pte pte; |
| 520 | bool is_mmio = false; |
Alexander Graf | 3eeafd7 | 2010-03-24 21:48:17 +0100 | [diff] [blame] | 521 | bool dr = (vcpu->arch.msr & MSR_DR) ? true : false; |
| 522 | bool ir = (vcpu->arch.msr & MSR_IR) ? true : false; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 523 | |
Alexander Graf | 3eeafd7 | 2010-03-24 21:48:17 +0100 | [diff] [blame] | 524 | relocated = data ? dr : ir; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 525 | |
| 526 | /* Resolve real address if translation turned on */ |
| 527 | if (relocated) { |
| 528 | page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data); |
| 529 | } else { |
| 530 | pte.may_execute = true; |
| 531 | pte.may_read = true; |
| 532 | pte.may_write = true; |
| 533 | pte.raddr = eaddr & 0xffffffff; |
| 534 | pte.eaddr = eaddr; |
| 535 | pte.vpage = eaddr >> 12; |
Alexander Graf | 3eeafd7 | 2010-03-24 21:48:17 +0100 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) { |
| 539 | case 0: |
| 540 | pte.vpage |= VSID_REAL; |
| 541 | break; |
| 542 | case MSR_DR: |
| 543 | pte.vpage |= VSID_REAL_DR; |
| 544 | break; |
| 545 | case MSR_IR: |
| 546 | pte.vpage |= VSID_REAL_IR; |
| 547 | break; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | if (vcpu->arch.mmu.is_dcbz32(vcpu) && |
| 551 | (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) { |
| 552 | /* |
| 553 | * If we do the dcbz hack, we have to NX on every execution, |
| 554 | * so we can patch the executing code. This renders our guest |
| 555 | * NX-less. |
| 556 | */ |
| 557 | pte.may_execute = !data; |
| 558 | } |
| 559 | |
| 560 | if (page_found == -ENOENT) { |
| 561 | /* Page not found in guest PTE entries */ |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 562 | vcpu->arch.dear = kvmppc_get_fault_dar(vcpu); |
| 563 | to_book3s(vcpu)->dsisr = to_svcpu(vcpu)->fault_dsisr; |
| 564 | vcpu->arch.msr |= (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 565 | kvmppc_book3s_queue_irqprio(vcpu, vec); |
| 566 | } else if (page_found == -EPERM) { |
| 567 | /* Storage protection */ |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 568 | vcpu->arch.dear = kvmppc_get_fault_dar(vcpu); |
| 569 | to_book3s(vcpu)->dsisr = to_svcpu(vcpu)->fault_dsisr & ~DSISR_NOHPTE; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 570 | to_book3s(vcpu)->dsisr |= DSISR_PROTFAULT; |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 571 | vcpu->arch.msr |= (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 572 | kvmppc_book3s_queue_irqprio(vcpu, vec); |
| 573 | } else if (page_found == -EINVAL) { |
| 574 | /* Page not found in guest SLB */ |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 575 | vcpu->arch.dear = kvmppc_get_fault_dar(vcpu); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 576 | kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80); |
| 577 | } else if (!is_mmio && |
| 578 | kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) { |
| 579 | /* The guest's PTE is not mapped yet. Map on the host */ |
| 580 | kvmppc_mmu_map_page(vcpu, &pte); |
| 581 | if (data) |
| 582 | vcpu->stat.sp_storage++; |
| 583 | else if (vcpu->arch.mmu.is_dcbz32(vcpu) && |
| 584 | (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) |
| 585 | kvmppc_patch_dcbz(vcpu, &pte); |
| 586 | } else { |
| 587 | /* MMIO */ |
| 588 | vcpu->stat.mmio_exits++; |
| 589 | vcpu->arch.paddr_accessed = pte.raddr; |
| 590 | r = kvmppc_emulate_mmio(run, vcpu); |
| 591 | if ( r == RESUME_HOST_NV ) |
| 592 | r = RESUME_HOST; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | return r; |
| 596 | } |
| 597 | |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 598 | static inline int get_fpr_index(int i) |
| 599 | { |
| 600 | #ifdef CONFIG_VSX |
| 601 | i *= 2; |
| 602 | #endif |
| 603 | return i; |
| 604 | } |
| 605 | |
| 606 | /* Give up external provider (FPU, Altivec, VSX) */ |
Alexander Graf | aba3bd7 | 2010-02-19 11:00:39 +0100 | [diff] [blame] | 607 | void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr) |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 608 | { |
| 609 | struct thread_struct *t = ¤t->thread; |
| 610 | u64 *vcpu_fpr = vcpu->arch.fpr; |
Alexander Graf | a2b0766 | 2010-03-24 21:48:31 +0100 | [diff] [blame] | 611 | #ifdef CONFIG_VSX |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 612 | u64 *vcpu_vsx = vcpu->arch.vsr; |
Alexander Graf | a2b0766 | 2010-03-24 21:48:31 +0100 | [diff] [blame] | 613 | #endif |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 614 | u64 *thread_fpr = (u64*)t->fpr; |
| 615 | int i; |
| 616 | |
| 617 | if (!(vcpu->arch.guest_owned_ext & msr)) |
| 618 | return; |
| 619 | |
| 620 | #ifdef DEBUG_EXT |
| 621 | printk(KERN_INFO "Giving up ext 0x%lx\n", msr); |
| 622 | #endif |
| 623 | |
| 624 | switch (msr) { |
| 625 | case MSR_FP: |
| 626 | giveup_fpu(current); |
| 627 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++) |
| 628 | vcpu_fpr[i] = thread_fpr[get_fpr_index(i)]; |
| 629 | |
| 630 | vcpu->arch.fpscr = t->fpscr.val; |
| 631 | break; |
| 632 | case MSR_VEC: |
| 633 | #ifdef CONFIG_ALTIVEC |
| 634 | giveup_altivec(current); |
| 635 | memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr)); |
| 636 | vcpu->arch.vscr = t->vscr; |
| 637 | #endif |
| 638 | break; |
| 639 | case MSR_VSX: |
| 640 | #ifdef CONFIG_VSX |
| 641 | __giveup_vsx(current); |
| 642 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++) |
| 643 | vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1]; |
| 644 | #endif |
| 645 | break; |
| 646 | default: |
| 647 | BUG(); |
| 648 | } |
| 649 | |
| 650 | vcpu->arch.guest_owned_ext &= ~msr; |
| 651 | current->thread.regs->msr &= ~msr; |
Alexander Graf | a76f849 | 2010-01-15 14:49:14 +0100 | [diff] [blame] | 652 | kvmppc_recalc_shadow_msr(vcpu); |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 653 | } |
| 654 | |
Alexander Graf | 8963221 | 2010-03-24 21:48:21 +0100 | [diff] [blame] | 655 | static int kvmppc_read_inst(struct kvm_vcpu *vcpu) |
Alexander Graf | c8c0b6f | 2010-02-19 11:00:34 +0100 | [diff] [blame] | 656 | { |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 657 | ulong srr0 = kvmppc_get_pc(vcpu); |
| 658 | u32 last_inst = kvmppc_get_last_inst(vcpu); |
Alexander Graf | c8c0b6f | 2010-02-19 11:00:34 +0100 | [diff] [blame] | 659 | int ret; |
| 660 | |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 661 | ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false); |
Alexander Graf | c8c0b6f | 2010-02-19 11:00:34 +0100 | [diff] [blame] | 662 | if (ret == -ENOENT) { |
| 663 | vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 33, 33, 1); |
| 664 | vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 34, 36, 0); |
| 665 | vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 42, 47, 0); |
| 666 | kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE); |
Alexander Graf | 8963221 | 2010-03-24 21:48:21 +0100 | [diff] [blame] | 667 | return EMULATE_AGAIN; |
| 668 | } |
| 669 | |
| 670 | return EMULATE_DONE; |
| 671 | } |
| 672 | |
| 673 | static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr) |
| 674 | { |
| 675 | |
| 676 | /* Need to do paired single emulation? */ |
| 677 | if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)) |
| 678 | return EMULATE_DONE; |
| 679 | |
| 680 | /* Read out the instruction */ |
| 681 | if (kvmppc_read_inst(vcpu) == EMULATE_DONE) |
Alexander Graf | c8c0b6f | 2010-02-19 11:00:34 +0100 | [diff] [blame] | 682 | /* Need to emulate */ |
| 683 | return EMULATE_FAIL; |
Alexander Graf | c8c0b6f | 2010-02-19 11:00:34 +0100 | [diff] [blame] | 684 | |
| 685 | return EMULATE_AGAIN; |
| 686 | } |
| 687 | |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 688 | /* Handle external providers (FPU, Altivec, VSX) */ |
| 689 | static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr, |
| 690 | ulong msr) |
| 691 | { |
| 692 | struct thread_struct *t = ¤t->thread; |
| 693 | u64 *vcpu_fpr = vcpu->arch.fpr; |
Alexander Graf | a2b0766 | 2010-03-24 21:48:31 +0100 | [diff] [blame] | 694 | #ifdef CONFIG_VSX |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 695 | u64 *vcpu_vsx = vcpu->arch.vsr; |
Alexander Graf | a2b0766 | 2010-03-24 21:48:31 +0100 | [diff] [blame] | 696 | #endif |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 697 | u64 *thread_fpr = (u64*)t->fpr; |
| 698 | int i; |
| 699 | |
Alexander Graf | 3c402a7 | 2010-02-19 11:00:32 +0100 | [diff] [blame] | 700 | /* When we have paired singles, we emulate in software */ |
| 701 | if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE) |
| 702 | return RESUME_GUEST; |
| 703 | |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 704 | if (!(vcpu->arch.msr & msr)) { |
| 705 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); |
| 706 | return RESUME_GUEST; |
| 707 | } |
| 708 | |
Alexander Graf | c245369 | 2010-03-24 21:48:22 +0100 | [diff] [blame] | 709 | /* We already own the ext */ |
| 710 | if (vcpu->arch.guest_owned_ext & msr) { |
| 711 | return RESUME_GUEST; |
| 712 | } |
| 713 | |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 714 | #ifdef DEBUG_EXT |
| 715 | printk(KERN_INFO "Loading up ext 0x%lx\n", msr); |
| 716 | #endif |
| 717 | |
| 718 | current->thread.regs->msr |= msr; |
| 719 | |
| 720 | switch (msr) { |
| 721 | case MSR_FP: |
| 722 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++) |
| 723 | thread_fpr[get_fpr_index(i)] = vcpu_fpr[i]; |
| 724 | |
| 725 | t->fpscr.val = vcpu->arch.fpscr; |
| 726 | t->fpexc_mode = 0; |
| 727 | kvmppc_load_up_fpu(); |
| 728 | break; |
| 729 | case MSR_VEC: |
| 730 | #ifdef CONFIG_ALTIVEC |
| 731 | memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr)); |
| 732 | t->vscr = vcpu->arch.vscr; |
| 733 | t->vrsave = -1; |
| 734 | kvmppc_load_up_altivec(); |
| 735 | #endif |
| 736 | break; |
| 737 | case MSR_VSX: |
| 738 | #ifdef CONFIG_VSX |
| 739 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++) |
| 740 | thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i]; |
| 741 | kvmppc_load_up_vsx(); |
| 742 | #endif |
| 743 | break; |
| 744 | default: |
| 745 | BUG(); |
| 746 | } |
| 747 | |
| 748 | vcpu->arch.guest_owned_ext |= msr; |
| 749 | |
Alexander Graf | a76f849 | 2010-01-15 14:49:14 +0100 | [diff] [blame] | 750 | kvmppc_recalc_shadow_msr(vcpu); |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 751 | |
| 752 | return RESUME_GUEST; |
| 753 | } |
| 754 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 755 | int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 756 | unsigned int exit_nr) |
| 757 | { |
| 758 | int r = RESUME_HOST; |
| 759 | |
| 760 | vcpu->stat.sum_exits++; |
| 761 | |
| 762 | run->exit_reason = KVM_EXIT_UNKNOWN; |
| 763 | run->ready_for_interrupt_injection = 1; |
| 764 | #ifdef EXIT_DEBUG |
| 765 | printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | dec=0x%x | msr=0x%lx\n", |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 766 | exit_nr, kvmppc_get_pc(vcpu), kvmppc_get_fault_dar(vcpu), |
| 767 | kvmppc_get_dec(vcpu), to_svcpu(vcpu)->shadow_srr1); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 768 | #elif defined (EXIT_DEBUG_SIMPLE) |
| 769 | if ((exit_nr != 0x900) && (exit_nr != 0x500)) |
| 770 | printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | msr=0x%lx\n", |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 771 | exit_nr, kvmppc_get_pc(vcpu), kvmppc_get_fault_dar(vcpu), |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 772 | vcpu->arch.msr); |
| 773 | #endif |
| 774 | kvm_resched(vcpu); |
| 775 | switch (exit_nr) { |
| 776 | case BOOK3S_INTERRUPT_INST_STORAGE: |
| 777 | vcpu->stat.pf_instruc++; |
Alexander Graf | 61db97c | 2010-04-16 00:11:52 +0200 | [diff] [blame^] | 778 | |
| 779 | #ifdef CONFIG_PPC_BOOK3S_32 |
| 780 | /* We set segments as unused segments when invalidating them. So |
| 781 | * treat the respective fault as segment fault. */ |
| 782 | if (to_svcpu(vcpu)->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT] |
| 783 | == SR_INVALID) { |
| 784 | kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)); |
| 785 | r = RESUME_GUEST; |
| 786 | break; |
| 787 | } |
| 788 | #endif |
| 789 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 790 | /* only care about PTEG not found errors, but leave NX alone */ |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 791 | if (to_svcpu(vcpu)->shadow_srr1 & 0x40000000) { |
| 792 | r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 793 | vcpu->stat.sp_instruc++; |
| 794 | } else if (vcpu->arch.mmu.is_dcbz32(vcpu) && |
| 795 | (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) { |
| 796 | /* |
| 797 | * XXX If we do the dcbz hack we use the NX bit to flush&patch the page, |
| 798 | * so we can't use the NX bit inside the guest. Let's cross our fingers, |
| 799 | * that no guest that needs the dcbz hack does NX. |
| 800 | */ |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 801 | kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFULL); |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 802 | r = RESUME_GUEST; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 803 | } else { |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 804 | vcpu->arch.msr |= to_svcpu(vcpu)->shadow_srr1 & 0x58000000; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 805 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 806 | kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFULL); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 807 | r = RESUME_GUEST; |
| 808 | } |
| 809 | break; |
| 810 | case BOOK3S_INTERRUPT_DATA_STORAGE: |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 811 | { |
| 812 | ulong dar = kvmppc_get_fault_dar(vcpu); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 813 | vcpu->stat.pf_storage++; |
Alexander Graf | 61db97c | 2010-04-16 00:11:52 +0200 | [diff] [blame^] | 814 | |
| 815 | #ifdef CONFIG_PPC_BOOK3S_32 |
| 816 | /* We set segments as unused segments when invalidating them. So |
| 817 | * treat the respective fault as segment fault. */ |
| 818 | if ((to_svcpu(vcpu)->sr[dar >> SID_SHIFT]) == SR_INVALID) { |
| 819 | kvmppc_mmu_map_segment(vcpu, dar); |
| 820 | r = RESUME_GUEST; |
| 821 | break; |
| 822 | } |
| 823 | #endif |
| 824 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 825 | /* The only case we need to handle is missing shadow PTEs */ |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 826 | if (to_svcpu(vcpu)->fault_dsisr & DSISR_NOHPTE) { |
| 827 | r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 828 | } else { |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 829 | vcpu->arch.dear = dar; |
| 830 | to_book3s(vcpu)->dsisr = to_svcpu(vcpu)->fault_dsisr; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 831 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); |
| 832 | kvmppc_mmu_pte_flush(vcpu, vcpu->arch.dear, ~0xFFFULL); |
| 833 | r = RESUME_GUEST; |
| 834 | } |
| 835 | break; |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 836 | } |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 837 | case BOOK3S_INTERRUPT_DATA_SEGMENT: |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 838 | if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) { |
| 839 | vcpu->arch.dear = kvmppc_get_fault_dar(vcpu); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 840 | kvmppc_book3s_queue_irqprio(vcpu, |
| 841 | BOOK3S_INTERRUPT_DATA_SEGMENT); |
| 842 | } |
| 843 | r = RESUME_GUEST; |
| 844 | break; |
| 845 | case BOOK3S_INTERRUPT_INST_SEGMENT: |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 846 | if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) { |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 847 | kvmppc_book3s_queue_irqprio(vcpu, |
| 848 | BOOK3S_INTERRUPT_INST_SEGMENT); |
| 849 | } |
| 850 | r = RESUME_GUEST; |
| 851 | break; |
| 852 | /* We're good on these - the host merely wanted to get our attention */ |
| 853 | case BOOK3S_INTERRUPT_DECREMENTER: |
| 854 | vcpu->stat.dec_exits++; |
| 855 | r = RESUME_GUEST; |
| 856 | break; |
| 857 | case BOOK3S_INTERRUPT_EXTERNAL: |
| 858 | vcpu->stat.ext_intr_exits++; |
| 859 | r = RESUME_GUEST; |
| 860 | break; |
| 861 | case BOOK3S_INTERRUPT_PROGRAM: |
| 862 | { |
| 863 | enum emulation_result er; |
Alexander Graf | ff1ca3f | 2010-01-08 02:58:09 +0100 | [diff] [blame] | 864 | ulong flags; |
| 865 | |
Alexander Graf | c8c0b6f | 2010-02-19 11:00:34 +0100 | [diff] [blame] | 866 | program_interrupt: |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 867 | flags = to_svcpu(vcpu)->shadow_srr1 & 0x1f0000ull; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 868 | |
| 869 | if (vcpu->arch.msr & MSR_PR) { |
| 870 | #ifdef EXIT_DEBUG |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 871 | printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu)); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 872 | #endif |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 873 | if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) != |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 874 | (INS_DCBZ & 0xfffffff7)) { |
Alexander Graf | ff1ca3f | 2010-01-08 02:58:09 +0100 | [diff] [blame] | 875 | kvmppc_core_queue_program(vcpu, flags); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 876 | r = RESUME_GUEST; |
| 877 | break; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | vcpu->stat.emulated_inst_exits++; |
| 882 | er = kvmppc_emulate_instruction(run, vcpu); |
| 883 | switch (er) { |
| 884 | case EMULATE_DONE: |
Alexander Graf | 97c4cfb | 2010-01-04 22:19:25 +0100 | [diff] [blame] | 885 | r = RESUME_GUEST_NV; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 886 | break; |
Alexander Graf | 37f5bca | 2010-02-19 11:00:31 +0100 | [diff] [blame] | 887 | case EMULATE_AGAIN: |
| 888 | r = RESUME_GUEST; |
| 889 | break; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 890 | case EMULATE_FAIL: |
| 891 | printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n", |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 892 | __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu)); |
Alexander Graf | ff1ca3f | 2010-01-08 02:58:09 +0100 | [diff] [blame] | 893 | kvmppc_core_queue_program(vcpu, flags); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 894 | r = RESUME_GUEST; |
| 895 | break; |
Alexander Graf | e5c29e9 | 2010-02-19 11:00:43 +0100 | [diff] [blame] | 896 | case EMULATE_DO_MMIO: |
| 897 | run->exit_reason = KVM_EXIT_MMIO; |
| 898 | r = RESUME_HOST_NV; |
| 899 | break; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 900 | default: |
| 901 | BUG(); |
| 902 | } |
| 903 | break; |
| 904 | } |
| 905 | case BOOK3S_INTERRUPT_SYSCALL: |
Alexander Graf | ad0a048 | 2010-03-24 21:48:30 +0100 | [diff] [blame] | 906 | // XXX make user settable |
| 907 | if (vcpu->arch.osi_enabled && |
| 908 | (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) && |
| 909 | (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) { |
| 910 | u64 *gprs = run->osi.gprs; |
| 911 | int i; |
| 912 | |
| 913 | run->exit_reason = KVM_EXIT_OSI; |
| 914 | for (i = 0; i < 32; i++) |
| 915 | gprs[i] = kvmppc_get_gpr(vcpu, i); |
| 916 | vcpu->arch.osi_needed = 1; |
| 917 | r = RESUME_HOST_NV; |
| 918 | |
| 919 | } else { |
| 920 | vcpu->stat.syscall_exits++; |
| 921 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); |
| 922 | r = RESUME_GUEST; |
| 923 | } |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 924 | break; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 925 | case BOOK3S_INTERRUPT_FP_UNAVAIL: |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 926 | case BOOK3S_INTERRUPT_ALTIVEC: |
| 927 | case BOOK3S_INTERRUPT_VSX: |
Alexander Graf | c8c0b6f | 2010-02-19 11:00:34 +0100 | [diff] [blame] | 928 | { |
| 929 | int ext_msr = 0; |
| 930 | |
| 931 | switch (exit_nr) { |
| 932 | case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break; |
| 933 | case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break; |
| 934 | case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break; |
| 935 | } |
| 936 | |
| 937 | switch (kvmppc_check_ext(vcpu, exit_nr)) { |
| 938 | case EMULATE_DONE: |
| 939 | /* everything ok - let's enable the ext */ |
| 940 | r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr); |
| 941 | break; |
| 942 | case EMULATE_FAIL: |
| 943 | /* we need to emulate this instruction */ |
| 944 | goto program_interrupt; |
| 945 | break; |
| 946 | default: |
| 947 | /* nothing to worry about - go again */ |
| 948 | break; |
| 949 | } |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 950 | break; |
Alexander Graf | c8c0b6f | 2010-02-19 11:00:34 +0100 | [diff] [blame] | 951 | } |
Alexander Graf | ca7f420 | 2010-03-24 21:48:28 +0100 | [diff] [blame] | 952 | case BOOK3S_INTERRUPT_ALIGNMENT: |
| 953 | if (kvmppc_read_inst(vcpu) == EMULATE_DONE) { |
| 954 | to_book3s(vcpu)->dsisr = kvmppc_alignment_dsisr(vcpu, |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 955 | kvmppc_get_last_inst(vcpu)); |
Alexander Graf | ca7f420 | 2010-03-24 21:48:28 +0100 | [diff] [blame] | 956 | vcpu->arch.dear = kvmppc_alignment_dar(vcpu, |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 957 | kvmppc_get_last_inst(vcpu)); |
Alexander Graf | ca7f420 | 2010-03-24 21:48:28 +0100 | [diff] [blame] | 958 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); |
| 959 | } |
| 960 | r = RESUME_GUEST; |
| 961 | break; |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 962 | case BOOK3S_INTERRUPT_MACHINE_CHECK: |
| 963 | case BOOK3S_INTERRUPT_TRACE: |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 964 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); |
| 965 | r = RESUME_GUEST; |
| 966 | break; |
| 967 | default: |
| 968 | /* Ugh - bork here! What did we get? */ |
Alexander Graf | f7adbba | 2010-01-15 14:49:13 +0100 | [diff] [blame] | 969 | printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n", |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 970 | exit_nr, kvmppc_get_pc(vcpu), to_svcpu(vcpu)->shadow_srr1); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 971 | r = RESUME_HOST; |
| 972 | BUG(); |
| 973 | break; |
| 974 | } |
| 975 | |
| 976 | |
| 977 | if (!(r & RESUME_HOST)) { |
| 978 | /* To avoid clobbering exit_reason, only check for signals if |
| 979 | * we aren't already exiting to userspace for some other |
| 980 | * reason. */ |
| 981 | if (signal_pending(current)) { |
| 982 | #ifdef EXIT_DEBUG |
| 983 | printk(KERN_EMERG "KVM: Going back to host\n"); |
| 984 | #endif |
| 985 | vcpu->stat.signal_exits++; |
| 986 | run->exit_reason = KVM_EXIT_INTR; |
| 987 | r = -EINTR; |
| 988 | } else { |
| 989 | /* In case an interrupt came in that was triggered |
| 990 | * from userspace (like DEC), we need to check what |
| 991 | * to inject now! */ |
| 992 | kvmppc_core_deliver_interrupts(vcpu); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | #ifdef EXIT_DEBUG |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 997 | printk(KERN_EMERG "KVM exit: vcpu=0x%p pc=0x%lx r=0x%x\n", vcpu, kvmppc_get_pc(vcpu), r); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 998 | #endif |
| 999 | |
| 1000 | return r; |
| 1001 | } |
| 1002 | |
| 1003 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
| 1004 | { |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
| 1008 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 1009 | { |
| 1010 | int i; |
| 1011 | |
Alexander Graf | a56cf34 | 2010-03-24 21:48:23 +0100 | [diff] [blame] | 1012 | vcpu_load(vcpu); |
| 1013 | |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 1014 | regs->pc = kvmppc_get_pc(vcpu); |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 1015 | regs->cr = kvmppc_get_cr(vcpu); |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 1016 | regs->ctr = kvmppc_get_ctr(vcpu); |
| 1017 | regs->lr = kvmppc_get_lr(vcpu); |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 1018 | regs->xer = kvmppc_get_xer(vcpu); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1019 | regs->msr = vcpu->arch.msr; |
| 1020 | regs->srr0 = vcpu->arch.srr0; |
| 1021 | regs->srr1 = vcpu->arch.srr1; |
| 1022 | regs->pid = vcpu->arch.pid; |
| 1023 | regs->sprg0 = vcpu->arch.sprg0; |
| 1024 | regs->sprg1 = vcpu->arch.sprg1; |
| 1025 | regs->sprg2 = vcpu->arch.sprg2; |
| 1026 | regs->sprg3 = vcpu->arch.sprg3; |
| 1027 | regs->sprg5 = vcpu->arch.sprg4; |
| 1028 | regs->sprg6 = vcpu->arch.sprg5; |
| 1029 | regs->sprg7 = vcpu->arch.sprg6; |
| 1030 | |
| 1031 | for (i = 0; i < ARRAY_SIZE(regs->gpr); i++) |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 1032 | regs->gpr[i] = kvmppc_get_gpr(vcpu, i); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1033 | |
Alexander Graf | a56cf34 | 2010-03-24 21:48:23 +0100 | [diff] [blame] | 1034 | vcpu_put(vcpu); |
| 1035 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1036 | return 0; |
| 1037 | } |
| 1038 | |
| 1039 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 1040 | { |
| 1041 | int i; |
| 1042 | |
Alexander Graf | a56cf34 | 2010-03-24 21:48:23 +0100 | [diff] [blame] | 1043 | vcpu_load(vcpu); |
| 1044 | |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 1045 | kvmppc_set_pc(vcpu, regs->pc); |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 1046 | kvmppc_set_cr(vcpu, regs->cr); |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 1047 | kvmppc_set_ctr(vcpu, regs->ctr); |
| 1048 | kvmppc_set_lr(vcpu, regs->lr); |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 1049 | kvmppc_set_xer(vcpu, regs->xer); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1050 | kvmppc_set_msr(vcpu, regs->msr); |
| 1051 | vcpu->arch.srr0 = regs->srr0; |
| 1052 | vcpu->arch.srr1 = regs->srr1; |
| 1053 | vcpu->arch.sprg0 = regs->sprg0; |
| 1054 | vcpu->arch.sprg1 = regs->sprg1; |
| 1055 | vcpu->arch.sprg2 = regs->sprg2; |
| 1056 | vcpu->arch.sprg3 = regs->sprg3; |
| 1057 | vcpu->arch.sprg5 = regs->sprg4; |
| 1058 | vcpu->arch.sprg6 = regs->sprg5; |
| 1059 | vcpu->arch.sprg7 = regs->sprg6; |
| 1060 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 1061 | for (i = 0; i < ARRAY_SIZE(regs->gpr); i++) |
| 1062 | kvmppc_set_gpr(vcpu, i, regs->gpr[i]); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1063 | |
Alexander Graf | a56cf34 | 2010-03-24 21:48:23 +0100 | [diff] [blame] | 1064 | vcpu_put(vcpu); |
| 1065 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1066 | return 0; |
| 1067 | } |
| 1068 | |
| 1069 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 1070 | struct kvm_sregs *sregs) |
| 1071 | { |
Alexander Graf | e15a113 | 2009-11-30 03:02:02 +0000 | [diff] [blame] | 1072 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
| 1073 | int i; |
| 1074 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1075 | sregs->pvr = vcpu->arch.pvr; |
Alexander Graf | e15a113 | 2009-11-30 03:02:02 +0000 | [diff] [blame] | 1076 | |
| 1077 | sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1; |
| 1078 | if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) { |
| 1079 | for (i = 0; i < 64; i++) { |
| 1080 | sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i; |
| 1081 | sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv; |
| 1082 | } |
| 1083 | } else { |
| 1084 | for (i = 0; i < 16; i++) { |
| 1085 | sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw; |
| 1086 | sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw; |
| 1087 | } |
| 1088 | for (i = 0; i < 8; i++) { |
| 1089 | sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw; |
| 1090 | sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw; |
| 1091 | } |
| 1092 | } |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 1097 | struct kvm_sregs *sregs) |
| 1098 | { |
Alexander Graf | e15a113 | 2009-11-30 03:02:02 +0000 | [diff] [blame] | 1099 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
| 1100 | int i; |
| 1101 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1102 | kvmppc_set_pvr(vcpu, sregs->pvr); |
Alexander Graf | e15a113 | 2009-11-30 03:02:02 +0000 | [diff] [blame] | 1103 | |
| 1104 | vcpu3s->sdr1 = sregs->u.s.sdr1; |
| 1105 | if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) { |
| 1106 | for (i = 0; i < 64; i++) { |
| 1107 | vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv, |
| 1108 | sregs->u.s.ppc64.slb[i].slbe); |
| 1109 | } |
| 1110 | } else { |
| 1111 | for (i = 0; i < 16; i++) { |
| 1112 | vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]); |
| 1113 | } |
| 1114 | for (i = 0; i < 8; i++) { |
| 1115 | kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false, |
| 1116 | (u32)sregs->u.s.ppc32.ibat[i]); |
| 1117 | kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true, |
| 1118 | (u32)(sregs->u.s.ppc32.ibat[i] >> 32)); |
| 1119 | kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false, |
| 1120 | (u32)sregs->u.s.ppc32.dbat[i]); |
| 1121 | kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true, |
| 1122 | (u32)(sregs->u.s.ppc32.dbat[i] >> 32)); |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | /* Flush the MMU after messing with the segments */ |
| 1127 | kvmppc_mmu_pte_flush(vcpu, 0, 0); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 1132 | { |
| 1133 | return -ENOTSUPP; |
| 1134 | } |
| 1135 | |
| 1136 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 1137 | { |
| 1138 | return -ENOTSUPP; |
| 1139 | } |
| 1140 | |
| 1141 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 1142 | struct kvm_translation *tr) |
| 1143 | { |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | /* |
| 1148 | * Get (and clear) the dirty memory log for a memory slot. |
| 1149 | */ |
| 1150 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 1151 | struct kvm_dirty_log *log) |
| 1152 | { |
| 1153 | struct kvm_memory_slot *memslot; |
| 1154 | struct kvm_vcpu *vcpu; |
| 1155 | ulong ga, ga_end; |
| 1156 | int is_dirty = 0; |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 1157 | int r; |
| 1158 | unsigned long n; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1159 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 1160 | mutex_lock(&kvm->slots_lock); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1161 | |
| 1162 | r = kvm_get_dirty_log(kvm, log, &is_dirty); |
| 1163 | if (r) |
| 1164 | goto out; |
| 1165 | |
| 1166 | /* If nothing is dirty, don't bother messing with page tables. */ |
| 1167 | if (is_dirty) { |
Marcelo Tosatti | 46a26bf | 2009-12-23 14:35:16 -0200 | [diff] [blame] | 1168 | memslot = &kvm->memslots->memslots[log->slot]; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1169 | |
| 1170 | ga = memslot->base_gfn << PAGE_SHIFT; |
| 1171 | ga_end = ga + (memslot->npages << PAGE_SHIFT); |
| 1172 | |
| 1173 | kvm_for_each_vcpu(n, vcpu, kvm) |
| 1174 | kvmppc_mmu_pte_pflush(vcpu, ga, ga_end); |
| 1175 | |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 1176 | n = kvm_dirty_bitmap_bytes(memslot); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1177 | memset(memslot->dirty_bitmap, 0, n); |
| 1178 | } |
| 1179 | |
| 1180 | r = 0; |
| 1181 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 1182 | mutex_unlock(&kvm->slots_lock); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1183 | return r; |
| 1184 | } |
| 1185 | |
| 1186 | int kvmppc_core_check_processor_compat(void) |
| 1187 | { |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
| 1191 | struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id) |
| 1192 | { |
| 1193 | struct kvmppc_vcpu_book3s *vcpu_book3s; |
| 1194 | struct kvm_vcpu *vcpu; |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 1195 | int err = -ENOMEM; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1196 | |
Alexander Graf | 032c340 | 2010-02-19 12:24:33 +0100 | [diff] [blame] | 1197 | vcpu_book3s = vmalloc(sizeof(struct kvmppc_vcpu_book3s)); |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 1198 | if (!vcpu_book3s) |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1199 | goto out; |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 1200 | |
Alexander Graf | 7e821d3 | 2010-02-22 16:52:08 +0100 | [diff] [blame] | 1201 | memset(vcpu_book3s, 0, sizeof(struct kvmppc_vcpu_book3s)); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1202 | |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 1203 | vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *) |
| 1204 | kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL); |
| 1205 | if (!vcpu_book3s->shadow_vcpu) |
| 1206 | goto free_vcpu; |
| 1207 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1208 | vcpu = &vcpu_book3s->vcpu; |
| 1209 | err = kvm_vcpu_init(vcpu, kvm, id); |
| 1210 | if (err) |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 1211 | goto free_shadow_vcpu; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1212 | |
| 1213 | vcpu->arch.host_retip = kvm_return_point; |
| 1214 | vcpu->arch.host_msr = mfmsr(); |
| 1215 | /* default to book3s_64 (970fx) */ |
| 1216 | vcpu->arch.pvr = 0x3C0301; |
| 1217 | kvmppc_set_pvr(vcpu, vcpu->arch.pvr); |
| 1218 | vcpu_book3s->slb_nr = 64; |
| 1219 | |
| 1220 | /* remember where some real-mode handlers are */ |
| 1221 | vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem; |
| 1222 | vcpu->arch.trampoline_enter = kvmppc_trampoline_enter; |
| 1223 | vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem; |
Alexander Graf | 021ec9c | 2010-01-08 02:58:06 +0100 | [diff] [blame] | 1224 | vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1225 | |
| 1226 | vcpu->arch.shadow_msr = MSR_USER64; |
| 1227 | |
Alexander Graf | 9cc5e95 | 2010-04-16 00:11:45 +0200 | [diff] [blame] | 1228 | err = kvmppc_mmu_init(vcpu); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1229 | if (err < 0) |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 1230 | goto free_shadow_vcpu; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1231 | |
| 1232 | return vcpu; |
| 1233 | |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 1234 | free_shadow_vcpu: |
| 1235 | kfree(vcpu_book3s->shadow_vcpu); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1236 | free_vcpu: |
Alexander Graf | 032c340 | 2010-02-19 12:24:33 +0100 | [diff] [blame] | 1237 | vfree(vcpu_book3s); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1238 | out: |
| 1239 | return ERR_PTR(err); |
| 1240 | } |
| 1241 | |
| 1242 | void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) |
| 1243 | { |
| 1244 | struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu); |
| 1245 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1246 | kvm_vcpu_uninit(vcpu); |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 1247 | kfree(vcpu_book3s->shadow_vcpu); |
Alexander Graf | 032c340 | 2010-02-19 12:24:33 +0100 | [diff] [blame] | 1248 | vfree(vcpu_book3s); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu); |
| 1252 | int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) |
| 1253 | { |
| 1254 | int ret; |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 1255 | struct thread_struct ext_bkp; |
Alexander Graf | a2b0766 | 2010-03-24 21:48:31 +0100 | [diff] [blame] | 1256 | #ifdef CONFIG_ALTIVEC |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 1257 | bool save_vec = current->thread.used_vr; |
Alexander Graf | a2b0766 | 2010-03-24 21:48:31 +0100 | [diff] [blame] | 1258 | #endif |
| 1259 | #ifdef CONFIG_VSX |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 1260 | bool save_vsx = current->thread.used_vsr; |
Alexander Graf | a2b0766 | 2010-03-24 21:48:31 +0100 | [diff] [blame] | 1261 | #endif |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 1262 | ulong ext_msr; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1263 | |
| 1264 | /* No need to go into the guest when all we do is going out */ |
| 1265 | if (signal_pending(current)) { |
| 1266 | kvm_run->exit_reason = KVM_EXIT_INTR; |
| 1267 | return -EINTR; |
| 1268 | } |
| 1269 | |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 1270 | /* Save FPU state in stack */ |
| 1271 | if (current->thread.regs->msr & MSR_FP) |
| 1272 | giveup_fpu(current); |
| 1273 | memcpy(ext_bkp.fpr, current->thread.fpr, sizeof(current->thread.fpr)); |
| 1274 | ext_bkp.fpscr = current->thread.fpscr; |
| 1275 | ext_bkp.fpexc_mode = current->thread.fpexc_mode; |
| 1276 | |
| 1277 | #ifdef CONFIG_ALTIVEC |
| 1278 | /* Save Altivec state in stack */ |
| 1279 | if (save_vec) { |
| 1280 | if (current->thread.regs->msr & MSR_VEC) |
| 1281 | giveup_altivec(current); |
| 1282 | memcpy(ext_bkp.vr, current->thread.vr, sizeof(ext_bkp.vr)); |
| 1283 | ext_bkp.vscr = current->thread.vscr; |
| 1284 | ext_bkp.vrsave = current->thread.vrsave; |
| 1285 | } |
| 1286 | ext_bkp.used_vr = current->thread.used_vr; |
| 1287 | #endif |
| 1288 | |
| 1289 | #ifdef CONFIG_VSX |
| 1290 | /* Save VSX state in stack */ |
| 1291 | if (save_vsx && (current->thread.regs->msr & MSR_VSX)) |
| 1292 | __giveup_vsx(current); |
| 1293 | ext_bkp.used_vsr = current->thread.used_vsr; |
| 1294 | #endif |
| 1295 | |
| 1296 | /* Remember the MSR with disabled extensions */ |
| 1297 | ext_msr = current->thread.regs->msr; |
| 1298 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1299 | /* XXX we get called with irq disabled - change that! */ |
| 1300 | local_irq_enable(); |
| 1301 | |
Alexander Graf | d1bab74 | 2010-02-19 11:00:35 +0100 | [diff] [blame] | 1302 | /* Preload FPU if it's enabled */ |
| 1303 | if (vcpu->arch.msr & MSR_FP) |
| 1304 | kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP); |
| 1305 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1306 | ret = __kvmppc_vcpu_entry(kvm_run, vcpu); |
| 1307 | |
| 1308 | local_irq_disable(); |
| 1309 | |
Alexander Graf | 180a34d | 2010-01-15 14:49:11 +0100 | [diff] [blame] | 1310 | current->thread.regs->msr = ext_msr; |
| 1311 | |
| 1312 | /* Make sure we save the guest FPU/Altivec/VSX state */ |
| 1313 | kvmppc_giveup_ext(vcpu, MSR_FP); |
| 1314 | kvmppc_giveup_ext(vcpu, MSR_VEC); |
| 1315 | kvmppc_giveup_ext(vcpu, MSR_VSX); |
| 1316 | |
| 1317 | /* Restore FPU state from stack */ |
| 1318 | memcpy(current->thread.fpr, ext_bkp.fpr, sizeof(ext_bkp.fpr)); |
| 1319 | current->thread.fpscr = ext_bkp.fpscr; |
| 1320 | current->thread.fpexc_mode = ext_bkp.fpexc_mode; |
| 1321 | |
| 1322 | #ifdef CONFIG_ALTIVEC |
| 1323 | /* Restore Altivec state from stack */ |
| 1324 | if (save_vec && current->thread.used_vr) { |
| 1325 | memcpy(current->thread.vr, ext_bkp.vr, sizeof(ext_bkp.vr)); |
| 1326 | current->thread.vscr = ext_bkp.vscr; |
| 1327 | current->thread.vrsave= ext_bkp.vrsave; |
| 1328 | } |
| 1329 | current->thread.used_vr = ext_bkp.used_vr; |
| 1330 | #endif |
| 1331 | |
| 1332 | #ifdef CONFIG_VSX |
| 1333 | current->thread.used_vsr = ext_bkp.used_vsr; |
| 1334 | #endif |
| 1335 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 1336 | return ret; |
| 1337 | } |
| 1338 | |
| 1339 | static int kvmppc_book3s_init(void) |
| 1340 | { |
| 1341 | return kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), THIS_MODULE); |
| 1342 | } |
| 1343 | |
| 1344 | static void kvmppc_book3s_exit(void) |
| 1345 | { |
| 1346 | kvm_exit(); |
| 1347 | } |
| 1348 | |
| 1349 | module_init(kvmppc_book3s_init); |
| 1350 | module_exit(kvmppc_book3s_exit); |