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