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