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