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