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