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