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> |
Paul Gortmaker | 66b15db | 2011-05-27 10:46:24 -0400 | [diff] [blame] | 19 | #include <linux/export.h> |
Stephen Rothwell | 329d20b | 2010-04-27 15:49:17 +1000 | [diff] [blame] | 20 | #include <linux/slab.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> |
Paul Mackerras | 149dbdb | 2011-06-29 00:16:42 +0000 | [diff] [blame] | 31 | #include <asm/page.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 32 | #include <linux/gfp.h> |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 33 | #include <linux/sched.h> |
| 34 | #include <linux/vmalloc.h> |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 35 | #include <linux/highmem.h> |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 36 | |
Paul Mackerras | c4befc5 | 2011-06-29 00:17:33 +0000 | [diff] [blame] | 37 | #include "trace.h" |
| 38 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 39 | #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU |
| 40 | |
| 41 | /* #define EXIT_DEBUG */ |
Alexander Graf | 07b0907 | 2010-04-16 00:11:53 +0200 | [diff] [blame] | 42 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 43 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
| 44 | { "exits", VCPU_STAT(sum_exits) }, |
| 45 | { "mmio", VCPU_STAT(mmio_exits) }, |
| 46 | { "sig", VCPU_STAT(signal_exits) }, |
| 47 | { "sysc", VCPU_STAT(syscall_exits) }, |
| 48 | { "inst_emu", VCPU_STAT(emulated_inst_exits) }, |
| 49 | { "dec", VCPU_STAT(dec_exits) }, |
| 50 | { "ext_intr", VCPU_STAT(ext_intr_exits) }, |
| 51 | { "queue_intr", VCPU_STAT(queue_intr) }, |
| 52 | { "halt_wakeup", VCPU_STAT(halt_wakeup) }, |
| 53 | { "pf_storage", VCPU_STAT(pf_storage) }, |
| 54 | { "sp_storage", VCPU_STAT(sp_storage) }, |
| 55 | { "pf_instruc", VCPU_STAT(pf_instruc) }, |
| 56 | { "sp_instruc", VCPU_STAT(sp_instruc) }, |
| 57 | { "ld", VCPU_STAT(ld) }, |
| 58 | { "ld_slow", VCPU_STAT(ld_slow) }, |
| 59 | { "st", VCPU_STAT(st) }, |
| 60 | { "st_slow", VCPU_STAT(st_slow) }, |
| 61 | { NULL } |
| 62 | }; |
| 63 | |
| 64 | void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu) |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu) |
| 69 | { |
| 70 | } |
| 71 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 72 | void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags) |
| 73 | { |
Alexander Graf | de7906c | 2010-07-29 14:47:46 +0200 | [diff] [blame] | 74 | vcpu->arch.shared->srr0 = kvmppc_get_pc(vcpu); |
| 75 | vcpu->arch.shared->srr1 = vcpu->arch.shared->msr | flags; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 76 | kvmppc_set_pc(vcpu, kvmppc_interrupt_offset(vcpu) + vec); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 77 | vcpu->arch.mmu.reset_msr(vcpu); |
| 78 | } |
| 79 | |
Alexander Graf | 583617b | 2009-12-21 20:21:23 +0100 | [diff] [blame] | 80 | static int kvmppc_book3s_vec2irqprio(unsigned int vec) |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 81 | { |
| 82 | unsigned int prio; |
| 83 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 84 | switch (vec) { |
| 85 | case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break; |
| 86 | case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break; |
| 87 | case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break; |
| 88 | case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break; |
| 89 | case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break; |
| 90 | case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break; |
| 91 | case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break; |
Alexander Graf | 17bd158 | 2010-08-30 10:44:15 +0200 | [diff] [blame] | 92 | case 0x501: prio = BOOK3S_IRQPRIO_EXTERNAL_LEVEL; break; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 93 | case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break; |
| 94 | case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break; |
| 95 | case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break; |
| 96 | case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break; |
| 97 | case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break; |
| 98 | case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break; |
| 99 | case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break; |
| 100 | case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break; |
| 101 | default: prio = BOOK3S_IRQPRIO_MAX; break; |
| 102 | } |
| 103 | |
Alexander Graf | 583617b | 2009-12-21 20:21:23 +0100 | [diff] [blame] | 104 | return prio; |
| 105 | } |
| 106 | |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 107 | void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu, |
Alexander Graf | 7706664d | 2009-12-21 20:21:24 +0100 | [diff] [blame] | 108 | unsigned int vec) |
| 109 | { |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 110 | unsigned long old_pending = vcpu->arch.pending_exceptions; |
| 111 | |
Alexander Graf | 7706664d | 2009-12-21 20:21:24 +0100 | [diff] [blame] | 112 | clear_bit(kvmppc_book3s_vec2irqprio(vec), |
| 113 | &vcpu->arch.pending_exceptions); |
Alexander Graf | 9ee18b1 | 2010-08-05 12:24:40 +0200 | [diff] [blame] | 114 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 115 | kvmppc_update_int_pending(vcpu, vcpu->arch.pending_exceptions, |
| 116 | old_pending); |
Alexander Graf | 7706664d | 2009-12-21 20:21:24 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Alexander Graf | 583617b | 2009-12-21 20:21:23 +0100 | [diff] [blame] | 119 | void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec) |
| 120 | { |
| 121 | vcpu->stat.queue_intr++; |
| 122 | |
| 123 | set_bit(kvmppc_book3s_vec2irqprio(vec), |
| 124 | &vcpu->arch.pending_exceptions); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 125 | #ifdef EXIT_DEBUG |
| 126 | printk(KERN_INFO "Queueing interrupt %x\n", vec); |
| 127 | #endif |
| 128 | } |
| 129 | |
| 130 | |
Alexander Graf | 25a8a02 | 2010-01-08 02:58:07 +0100 | [diff] [blame] | 131 | void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags) |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 132 | { |
Paul Mackerras | 3cf658b | 2011-06-29 00:18:52 +0000 | [diff] [blame] | 133 | /* might as well deliver this straight away */ |
| 134 | kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_PROGRAM, flags); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu) |
| 138 | { |
| 139 | kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER); |
| 140 | } |
| 141 | |
| 142 | int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu) |
| 143 | { |
Paul Mackerras | 44075d9 | 2011-05-11 00:38:50 +0000 | [diff] [blame] | 144 | return test_bit(BOOK3S_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Alexander Graf | 7706664d | 2009-12-21 20:21:24 +0100 | [diff] [blame] | 147 | void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu) |
| 148 | { |
| 149 | kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER); |
| 150 | } |
| 151 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 152 | void kvmppc_core_queue_external(struct kvm_vcpu *vcpu, |
| 153 | struct kvm_interrupt *irq) |
| 154 | { |
Alexander Graf | 17bd158 | 2010-08-30 10:44:15 +0200 | [diff] [blame] | 155 | unsigned int vec = BOOK3S_INTERRUPT_EXTERNAL; |
| 156 | |
| 157 | if (irq->irq == KVM_INTERRUPT_SET_LEVEL) |
| 158 | vec = BOOK3S_INTERRUPT_EXTERNAL_LEVEL; |
| 159 | |
| 160 | kvmppc_book3s_queue_irqprio(vcpu, vec); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Paul Mackerras | 4fe27d2 | 2013-02-14 14:00:25 +0000 | [diff] [blame] | 163 | void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu) |
Alexander Graf | 1897876 | 2010-03-24 21:48:18 +0100 | [diff] [blame] | 164 | { |
| 165 | kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL); |
Alexander Graf | 17bd158 | 2010-08-30 10:44:15 +0200 | [diff] [blame] | 166 | kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL_LEVEL); |
Alexander Graf | 1897876 | 2010-03-24 21:48:18 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 169 | int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority) |
| 170 | { |
| 171 | int deliver = 1; |
| 172 | int vec = 0; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 173 | bool crit = kvmppc_critical_section(vcpu); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 174 | |
| 175 | switch (priority) { |
| 176 | case BOOK3S_IRQPRIO_DECREMENTER: |
Alexander Graf | 5c6cedf | 2010-07-29 14:47:49 +0200 | [diff] [blame] | 177 | deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 178 | vec = BOOK3S_INTERRUPT_DECREMENTER; |
| 179 | break; |
| 180 | case BOOK3S_IRQPRIO_EXTERNAL: |
Alexander Graf | 17bd158 | 2010-08-30 10:44:15 +0200 | [diff] [blame] | 181 | case BOOK3S_IRQPRIO_EXTERNAL_LEVEL: |
Alexander Graf | 5c6cedf | 2010-07-29 14:47:49 +0200 | [diff] [blame] | 182 | deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 183 | vec = BOOK3S_INTERRUPT_EXTERNAL; |
| 184 | break; |
| 185 | case BOOK3S_IRQPRIO_SYSTEM_RESET: |
| 186 | vec = BOOK3S_INTERRUPT_SYSTEM_RESET; |
| 187 | break; |
| 188 | case BOOK3S_IRQPRIO_MACHINE_CHECK: |
| 189 | vec = BOOK3S_INTERRUPT_MACHINE_CHECK; |
| 190 | break; |
| 191 | case BOOK3S_IRQPRIO_DATA_STORAGE: |
| 192 | vec = BOOK3S_INTERRUPT_DATA_STORAGE; |
| 193 | break; |
| 194 | case BOOK3S_IRQPRIO_INST_STORAGE: |
| 195 | vec = BOOK3S_INTERRUPT_INST_STORAGE; |
| 196 | break; |
| 197 | case BOOK3S_IRQPRIO_DATA_SEGMENT: |
| 198 | vec = BOOK3S_INTERRUPT_DATA_SEGMENT; |
| 199 | break; |
| 200 | case BOOK3S_IRQPRIO_INST_SEGMENT: |
| 201 | vec = BOOK3S_INTERRUPT_INST_SEGMENT; |
| 202 | break; |
| 203 | case BOOK3S_IRQPRIO_ALIGNMENT: |
| 204 | vec = BOOK3S_INTERRUPT_ALIGNMENT; |
| 205 | break; |
| 206 | case BOOK3S_IRQPRIO_PROGRAM: |
| 207 | vec = BOOK3S_INTERRUPT_PROGRAM; |
| 208 | break; |
| 209 | case BOOK3S_IRQPRIO_VSX: |
| 210 | vec = BOOK3S_INTERRUPT_VSX; |
| 211 | break; |
| 212 | case BOOK3S_IRQPRIO_ALTIVEC: |
| 213 | vec = BOOK3S_INTERRUPT_ALTIVEC; |
| 214 | break; |
| 215 | case BOOK3S_IRQPRIO_FP_UNAVAIL: |
| 216 | vec = BOOK3S_INTERRUPT_FP_UNAVAIL; |
| 217 | break; |
| 218 | case BOOK3S_IRQPRIO_SYSCALL: |
| 219 | vec = BOOK3S_INTERRUPT_SYSCALL; |
| 220 | break; |
| 221 | case BOOK3S_IRQPRIO_DEBUG: |
| 222 | vec = BOOK3S_INTERRUPT_TRACE; |
| 223 | break; |
| 224 | case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR: |
| 225 | vec = BOOK3S_INTERRUPT_PERFMON; |
| 226 | break; |
| 227 | default: |
| 228 | deliver = 0; |
| 229 | printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority); |
| 230 | break; |
| 231 | } |
| 232 | |
| 233 | #if 0 |
| 234 | printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver); |
| 235 | #endif |
| 236 | |
| 237 | if (deliver) |
Paul Mackerras | 3cf658b | 2011-06-29 00:18:52 +0000 | [diff] [blame] | 238 | kvmppc_inject_interrupt(vcpu, vec, 0); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 239 | |
| 240 | return deliver; |
| 241 | } |
| 242 | |
Alexander Graf | 17bd158 | 2010-08-30 10:44:15 +0200 | [diff] [blame] | 243 | /* |
| 244 | * This function determines if an irqprio should be cleared once issued. |
| 245 | */ |
| 246 | static bool clear_irqprio(struct kvm_vcpu *vcpu, unsigned int priority) |
| 247 | { |
| 248 | switch (priority) { |
| 249 | case BOOK3S_IRQPRIO_DECREMENTER: |
| 250 | /* DEC interrupts get cleared by mtdec */ |
| 251 | return false; |
| 252 | case BOOK3S_IRQPRIO_EXTERNAL_LEVEL: |
| 253 | /* External interrupts get cleared by userspace */ |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | return true; |
| 258 | } |
| 259 | |
Alexander Graf | a8e4ef8 | 2012-02-16 14:07:37 +0000 | [diff] [blame] | 260 | int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu) |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 261 | { |
| 262 | unsigned long *pending = &vcpu->arch.pending_exceptions; |
Alexander Graf | 90bba35 | 2010-07-29 14:47:51 +0200 | [diff] [blame] | 263 | unsigned long old_pending = vcpu->arch.pending_exceptions; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 264 | unsigned int priority; |
| 265 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 266 | #ifdef EXIT_DEBUG |
| 267 | if (vcpu->arch.pending_exceptions) |
| 268 | printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions); |
| 269 | #endif |
| 270 | priority = __ffs(*pending); |
Alexander Graf | ada7ba1 | 2010-04-16 00:11:56 +0200 | [diff] [blame] | 271 | while (priority < BOOK3S_IRQPRIO_MAX) { |
Alexander Graf | 7706664d | 2009-12-21 20:21:24 +0100 | [diff] [blame] | 272 | if (kvmppc_book3s_irqprio_deliver(vcpu, priority) && |
Alexander Graf | 17bd158 | 2010-08-30 10:44:15 +0200 | [diff] [blame] | 273 | clear_irqprio(vcpu, priority)) { |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 274 | clear_bit(priority, &vcpu->arch.pending_exceptions); |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | priority = find_next_bit(pending, |
| 279 | BITS_PER_BYTE * sizeof(*pending), |
| 280 | priority + 1); |
| 281 | } |
Alexander Graf | 90bba35 | 2010-07-29 14:47:51 +0200 | [diff] [blame] | 282 | |
| 283 | /* Tell the guest about our interrupt status */ |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 284 | kvmppc_update_int_pending(vcpu, *pending, old_pending); |
Alexander Graf | a8e4ef8 | 2012-02-16 14:07:37 +0000 | [diff] [blame] | 285 | |
| 286 | return 0; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Alexander Graf | e850894 | 2010-07-29 14:47:54 +0200 | [diff] [blame] | 289 | pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 290 | { |
| 291 | ulong mp_pa = vcpu->arch.magic_page_pa; |
| 292 | |
Benjamin Herrenschmidt | bbcc9c0 | 2012-03-13 21:52:44 +0000 | [diff] [blame] | 293 | if (!(vcpu->arch.shared->msr & MSR_SF)) |
| 294 | mp_pa = (uint32_t)mp_pa; |
| 295 | |
Alexander Graf | e850894 | 2010-07-29 14:47:54 +0200 | [diff] [blame] | 296 | /* Magic page override */ |
| 297 | if (unlikely(mp_pa) && |
| 298 | unlikely(((gfn << PAGE_SHIFT) & KVM_PAM) == |
| 299 | ((mp_pa & PAGE_MASK) & KVM_PAM))) { |
| 300 | ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK; |
| 301 | pfn_t pfn; |
| 302 | |
| 303 | pfn = (pfn_t)virt_to_phys((void*)shared_page) >> PAGE_SHIFT; |
| 304 | get_page(pfn_to_page(pfn)); |
| 305 | return pfn; |
| 306 | } |
| 307 | |
| 308 | return gfn_to_pfn(vcpu->kvm, gfn); |
| 309 | } |
| 310 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 311 | static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data, |
| 312 | struct kvmppc_pte *pte) |
| 313 | { |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 314 | int relocated = (vcpu->arch.shared->msr & (data ? MSR_DR : MSR_IR)); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 315 | int r; |
| 316 | |
| 317 | if (relocated) { |
| 318 | r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data); |
| 319 | } else { |
| 320 | pte->eaddr = eaddr; |
Alexander Graf | 28e83b4 | 2010-07-29 14:47:52 +0200 | [diff] [blame] | 321 | pte->raddr = eaddr & KVM_PAM; |
Alexander Graf | 3eeafd7 | 2010-03-24 21:48:17 +0100 | [diff] [blame] | 322 | pte->vpage = VSID_REAL | eaddr >> 12; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 323 | pte->may_read = true; |
| 324 | pte->may_write = true; |
| 325 | pte->may_execute = true; |
| 326 | r = 0; |
| 327 | } |
| 328 | |
| 329 | return r; |
| 330 | } |
| 331 | |
| 332 | static hva_t kvmppc_bad_hva(void) |
| 333 | { |
| 334 | return PAGE_OFFSET; |
| 335 | } |
| 336 | |
| 337 | static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte, |
| 338 | bool read) |
| 339 | { |
| 340 | hva_t hpage; |
| 341 | |
| 342 | if (read && !pte->may_read) |
| 343 | goto err; |
| 344 | |
| 345 | if (!read && !pte->may_write) |
| 346 | goto err; |
| 347 | |
| 348 | hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT); |
| 349 | if (kvm_is_error_hva(hpage)) |
| 350 | goto err; |
| 351 | |
| 352 | return hpage | (pte->raddr & ~PAGE_MASK); |
| 353 | err: |
| 354 | return kvmppc_bad_hva(); |
| 355 | } |
| 356 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 357 | int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, |
| 358 | bool data) |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 359 | { |
| 360 | struct kvmppc_pte pte; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 361 | |
| 362 | vcpu->stat.st++; |
| 363 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 364 | if (kvmppc_xlate(vcpu, *eaddr, data, &pte)) |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 365 | return -ENOENT; |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 366 | |
| 367 | *eaddr = pte.raddr; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 368 | |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 369 | if (!pte.may_write) |
| 370 | return -EPERM; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 371 | |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 372 | if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size)) |
| 373 | return EMULATE_DO_MMIO; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 374 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 375 | return EMULATE_DONE; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 378 | 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] | 379 | bool data) |
| 380 | { |
| 381 | struct kvmppc_pte pte; |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 382 | hva_t hva = *eaddr; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 383 | |
| 384 | vcpu->stat.ld++; |
| 385 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 386 | if (kvmppc_xlate(vcpu, *eaddr, data, &pte)) |
| 387 | goto nopte; |
| 388 | |
| 389 | *eaddr = pte.raddr; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 390 | |
| 391 | hva = kvmppc_pte_to_hva(vcpu, &pte, true); |
| 392 | if (kvm_is_error_hva(hva)) |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 393 | goto mmio; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 394 | |
| 395 | if (copy_from_user(ptr, (void __user *)hva, size)) { |
| 396 | printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva); |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 397 | goto mmio; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 400 | return EMULATE_DONE; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 401 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 402 | nopte: |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 403 | return -ENOENT; |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 404 | mmio: |
| 405 | return EMULATE_DO_MMIO; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 408 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
| 409 | { |
| 410 | return 0; |
| 411 | } |
| 412 | |
Bharat Bhushan | f61c94b | 2012-08-08 20:38:19 +0000 | [diff] [blame] | 413 | int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu) |
| 414 | { |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 419 | { |
| 420 | } |
| 421 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 422 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 423 | { |
| 424 | int i; |
| 425 | |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 426 | regs->pc = kvmppc_get_pc(vcpu); |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 427 | regs->cr = kvmppc_get_cr(vcpu); |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 428 | regs->ctr = kvmppc_get_ctr(vcpu); |
| 429 | regs->lr = kvmppc_get_lr(vcpu); |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 430 | regs->xer = kvmppc_get_xer(vcpu); |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 431 | regs->msr = vcpu->arch.shared->msr; |
Alexander Graf | de7906c | 2010-07-29 14:47:46 +0200 | [diff] [blame] | 432 | regs->srr0 = vcpu->arch.shared->srr0; |
| 433 | regs->srr1 = vcpu->arch.shared->srr1; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 434 | regs->pid = vcpu->arch.pid; |
Alexander Graf | a73a959 | 2010-07-29 14:47:47 +0200 | [diff] [blame] | 435 | regs->sprg0 = vcpu->arch.shared->sprg0; |
| 436 | regs->sprg1 = vcpu->arch.shared->sprg1; |
| 437 | regs->sprg2 = vcpu->arch.shared->sprg2; |
| 438 | regs->sprg3 = vcpu->arch.shared->sprg3; |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 439 | regs->sprg4 = vcpu->arch.shared->sprg4; |
| 440 | regs->sprg5 = vcpu->arch.shared->sprg5; |
| 441 | regs->sprg6 = vcpu->arch.shared->sprg6; |
| 442 | regs->sprg7 = vcpu->arch.shared->sprg7; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 443 | |
| 444 | for (i = 0; i < ARRAY_SIZE(regs->gpr); i++) |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 445 | regs->gpr[i] = kvmppc_get_gpr(vcpu, i); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 451 | { |
| 452 | int i; |
| 453 | |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 454 | kvmppc_set_pc(vcpu, regs->pc); |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 455 | kvmppc_set_cr(vcpu, regs->cr); |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 456 | kvmppc_set_ctr(vcpu, regs->ctr); |
| 457 | kvmppc_set_lr(vcpu, regs->lr); |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 458 | kvmppc_set_xer(vcpu, regs->xer); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 459 | kvmppc_set_msr(vcpu, regs->msr); |
Alexander Graf | de7906c | 2010-07-29 14:47:46 +0200 | [diff] [blame] | 460 | vcpu->arch.shared->srr0 = regs->srr0; |
| 461 | vcpu->arch.shared->srr1 = regs->srr1; |
Alexander Graf | a73a959 | 2010-07-29 14:47:47 +0200 | [diff] [blame] | 462 | vcpu->arch.shared->sprg0 = regs->sprg0; |
| 463 | vcpu->arch.shared->sprg1 = regs->sprg1; |
| 464 | vcpu->arch.shared->sprg2 = regs->sprg2; |
| 465 | vcpu->arch.shared->sprg3 = regs->sprg3; |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 466 | vcpu->arch.shared->sprg4 = regs->sprg4; |
| 467 | vcpu->arch.shared->sprg5 = regs->sprg5; |
| 468 | vcpu->arch.shared->sprg6 = regs->sprg6; |
| 469 | vcpu->arch.shared->sprg7 = regs->sprg7; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 470 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 471 | for (i = 0; i < ARRAY_SIZE(regs->gpr); i++) |
| 472 | kvmppc_set_gpr(vcpu, i, regs->gpr[i]); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 477 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 478 | { |
| 479 | return -ENOTSUPP; |
| 480 | } |
| 481 | |
| 482 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 483 | { |
| 484 | return -ENOTSUPP; |
| 485 | } |
| 486 | |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 487 | int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) |
| 488 | { |
| 489 | int r; |
| 490 | union kvmppc_one_reg val; |
| 491 | int size; |
Paul Mackerras | a8bd19e | 2012-09-25 20:32:30 +0000 | [diff] [blame] | 492 | long int i; |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 493 | |
| 494 | size = one_reg_size(reg->id); |
| 495 | if (size > sizeof(val)) |
| 496 | return -EINVAL; |
| 497 | |
| 498 | r = kvmppc_get_one_reg(vcpu, reg->id, &val); |
| 499 | |
| 500 | if (r == -EINVAL) { |
| 501 | r = 0; |
| 502 | switch (reg->id) { |
| 503 | case KVM_REG_PPC_DAR: |
| 504 | val = get_reg_val(reg->id, vcpu->arch.shared->dar); |
| 505 | break; |
| 506 | case KVM_REG_PPC_DSISR: |
| 507 | val = get_reg_val(reg->id, vcpu->arch.shared->dsisr); |
| 508 | break; |
Paul Mackerras | a8bd19e | 2012-09-25 20:32:30 +0000 | [diff] [blame] | 509 | case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31: |
| 510 | i = reg->id - KVM_REG_PPC_FPR0; |
| 511 | val = get_reg_val(reg->id, vcpu->arch.fpr[i]); |
| 512 | break; |
| 513 | case KVM_REG_PPC_FPSCR: |
| 514 | val = get_reg_val(reg->id, vcpu->arch.fpscr); |
| 515 | break; |
| 516 | #ifdef CONFIG_ALTIVEC |
| 517 | case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31: |
| 518 | if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { |
| 519 | r = -ENXIO; |
| 520 | break; |
| 521 | } |
| 522 | val.vval = vcpu->arch.vr[reg->id - KVM_REG_PPC_VR0]; |
| 523 | break; |
| 524 | case KVM_REG_PPC_VSCR: |
| 525 | if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { |
| 526 | r = -ENXIO; |
| 527 | break; |
| 528 | } |
| 529 | val = get_reg_val(reg->id, vcpu->arch.vscr.u[3]); |
| 530 | break; |
| 531 | #endif /* CONFIG_ALTIVEC */ |
Bharat Bhushan | 8c32a2e | 2013-03-20 20:24:58 +0000 | [diff] [blame] | 532 | case KVM_REG_PPC_DEBUG_INST: { |
| 533 | u32 opcode = INS_TW; |
| 534 | r = copy_to_user((u32 __user *)(long)reg->addr, |
| 535 | &opcode, sizeof(u32)); |
| 536 | break; |
| 537 | } |
Paul Mackerras | 8b78645 | 2013-04-17 20:32:26 +0000 | [diff] [blame] | 538 | #ifdef CONFIG_KVM_XICS |
| 539 | case KVM_REG_PPC_ICP_STATE: |
| 540 | if (!vcpu->arch.icp) { |
| 541 | r = -ENXIO; |
| 542 | break; |
| 543 | } |
| 544 | val = get_reg_val(reg->id, kvmppc_xics_get_icp(vcpu)); |
| 545 | break; |
| 546 | #endif /* CONFIG_KVM_XICS */ |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 547 | default: |
| 548 | r = -EINVAL; |
| 549 | break; |
| 550 | } |
| 551 | } |
| 552 | if (r) |
| 553 | return r; |
| 554 | |
| 555 | if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size)) |
| 556 | r = -EFAULT; |
| 557 | |
| 558 | return r; |
| 559 | } |
| 560 | |
| 561 | int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) |
| 562 | { |
| 563 | int r; |
| 564 | union kvmppc_one_reg val; |
| 565 | int size; |
Paul Mackerras | a8bd19e | 2012-09-25 20:32:30 +0000 | [diff] [blame] | 566 | long int i; |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 567 | |
| 568 | size = one_reg_size(reg->id); |
| 569 | if (size > sizeof(val)) |
| 570 | return -EINVAL; |
| 571 | |
| 572 | if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size)) |
| 573 | return -EFAULT; |
| 574 | |
| 575 | r = kvmppc_set_one_reg(vcpu, reg->id, &val); |
| 576 | |
| 577 | if (r == -EINVAL) { |
| 578 | r = 0; |
| 579 | switch (reg->id) { |
| 580 | case KVM_REG_PPC_DAR: |
| 581 | vcpu->arch.shared->dar = set_reg_val(reg->id, val); |
| 582 | break; |
| 583 | case KVM_REG_PPC_DSISR: |
| 584 | vcpu->arch.shared->dsisr = set_reg_val(reg->id, val); |
| 585 | break; |
Paul Mackerras | a8bd19e | 2012-09-25 20:32:30 +0000 | [diff] [blame] | 586 | case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31: |
| 587 | i = reg->id - KVM_REG_PPC_FPR0; |
| 588 | vcpu->arch.fpr[i] = set_reg_val(reg->id, val); |
| 589 | break; |
| 590 | case KVM_REG_PPC_FPSCR: |
| 591 | vcpu->arch.fpscr = set_reg_val(reg->id, val); |
| 592 | break; |
| 593 | #ifdef CONFIG_ALTIVEC |
| 594 | case KVM_REG_PPC_VR0 ... KVM_REG_PPC_VR31: |
| 595 | if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { |
| 596 | r = -ENXIO; |
| 597 | break; |
| 598 | } |
| 599 | vcpu->arch.vr[reg->id - KVM_REG_PPC_VR0] = val.vval; |
| 600 | break; |
| 601 | case KVM_REG_PPC_VSCR: |
| 602 | if (!cpu_has_feature(CPU_FTR_ALTIVEC)) { |
| 603 | r = -ENXIO; |
| 604 | break; |
| 605 | } |
| 606 | vcpu->arch.vscr.u[3] = set_reg_val(reg->id, val); |
| 607 | break; |
| 608 | #endif /* CONFIG_ALTIVEC */ |
Paul Mackerras | 8b78645 | 2013-04-17 20:32:26 +0000 | [diff] [blame] | 609 | #ifdef CONFIG_KVM_XICS |
| 610 | case KVM_REG_PPC_ICP_STATE: |
| 611 | if (!vcpu->arch.icp) { |
| 612 | r = -ENXIO; |
| 613 | break; |
| 614 | } |
| 615 | r = kvmppc_xics_set_icp(vcpu, |
| 616 | set_reg_val(reg->id, val)); |
| 617 | break; |
| 618 | #endif /* CONFIG_KVM_XICS */ |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 619 | default: |
| 620 | r = -EINVAL; |
| 621 | break; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | return r; |
| 626 | } |
| 627 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 628 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 629 | struct kvm_translation *tr) |
| 630 | { |
| 631 | return 0; |
| 632 | } |
| 633 | |
Bharat Bhushan | 092d62e | 2013-04-08 00:32:12 +0000 | [diff] [blame] | 634 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| 635 | struct kvm_guest_debug *dbg) |
| 636 | { |
| 637 | return -EINVAL; |
| 638 | } |
| 639 | |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 640 | void kvmppc_decrementer_func(unsigned long data) |
| 641 | { |
| 642 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; |
| 643 | |
| 644 | kvmppc_core_queue_dec(vcpu); |
| 645 | kvm_vcpu_kick(vcpu); |
| 646 | } |