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 | |
Alexander Graf | 7706664d | 2009-12-21 20:21:24 +0100 | [diff] [blame] | 107 | static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu, |
| 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 | |
Alexander Graf | 1897876 | 2010-03-24 21:48:18 +0100 | [diff] [blame] | 163 | void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu, |
| 164 | struct kvm_interrupt *irq) |
| 165 | { |
| 166 | kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL); |
Alexander Graf | 17bd158 | 2010-08-30 10:44:15 +0200 | [diff] [blame] | 167 | kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL_LEVEL); |
Alexander Graf | 1897876 | 2010-03-24 21:48:18 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 170 | int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority) |
| 171 | { |
| 172 | int deliver = 1; |
| 173 | int vec = 0; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 174 | bool crit = kvmppc_critical_section(vcpu); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 175 | |
| 176 | switch (priority) { |
| 177 | case BOOK3S_IRQPRIO_DECREMENTER: |
Alexander Graf | 5c6cedf | 2010-07-29 14:47:49 +0200 | [diff] [blame] | 178 | deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 179 | vec = BOOK3S_INTERRUPT_DECREMENTER; |
| 180 | break; |
| 181 | case BOOK3S_IRQPRIO_EXTERNAL: |
Alexander Graf | 17bd158 | 2010-08-30 10:44:15 +0200 | [diff] [blame] | 182 | case BOOK3S_IRQPRIO_EXTERNAL_LEVEL: |
Alexander Graf | 5c6cedf | 2010-07-29 14:47:49 +0200 | [diff] [blame] | 183 | deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 184 | vec = BOOK3S_INTERRUPT_EXTERNAL; |
| 185 | break; |
| 186 | case BOOK3S_IRQPRIO_SYSTEM_RESET: |
| 187 | vec = BOOK3S_INTERRUPT_SYSTEM_RESET; |
| 188 | break; |
| 189 | case BOOK3S_IRQPRIO_MACHINE_CHECK: |
| 190 | vec = BOOK3S_INTERRUPT_MACHINE_CHECK; |
| 191 | break; |
| 192 | case BOOK3S_IRQPRIO_DATA_STORAGE: |
| 193 | vec = BOOK3S_INTERRUPT_DATA_STORAGE; |
| 194 | break; |
| 195 | case BOOK3S_IRQPRIO_INST_STORAGE: |
| 196 | vec = BOOK3S_INTERRUPT_INST_STORAGE; |
| 197 | break; |
| 198 | case BOOK3S_IRQPRIO_DATA_SEGMENT: |
| 199 | vec = BOOK3S_INTERRUPT_DATA_SEGMENT; |
| 200 | break; |
| 201 | case BOOK3S_IRQPRIO_INST_SEGMENT: |
| 202 | vec = BOOK3S_INTERRUPT_INST_SEGMENT; |
| 203 | break; |
| 204 | case BOOK3S_IRQPRIO_ALIGNMENT: |
| 205 | vec = BOOK3S_INTERRUPT_ALIGNMENT; |
| 206 | break; |
| 207 | case BOOK3S_IRQPRIO_PROGRAM: |
| 208 | vec = BOOK3S_INTERRUPT_PROGRAM; |
| 209 | break; |
| 210 | case BOOK3S_IRQPRIO_VSX: |
| 211 | vec = BOOK3S_INTERRUPT_VSX; |
| 212 | break; |
| 213 | case BOOK3S_IRQPRIO_ALTIVEC: |
| 214 | vec = BOOK3S_INTERRUPT_ALTIVEC; |
| 215 | break; |
| 216 | case BOOK3S_IRQPRIO_FP_UNAVAIL: |
| 217 | vec = BOOK3S_INTERRUPT_FP_UNAVAIL; |
| 218 | break; |
| 219 | case BOOK3S_IRQPRIO_SYSCALL: |
| 220 | vec = BOOK3S_INTERRUPT_SYSCALL; |
| 221 | break; |
| 222 | case BOOK3S_IRQPRIO_DEBUG: |
| 223 | vec = BOOK3S_INTERRUPT_TRACE; |
| 224 | break; |
| 225 | case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR: |
| 226 | vec = BOOK3S_INTERRUPT_PERFMON; |
| 227 | break; |
| 228 | default: |
| 229 | deliver = 0; |
| 230 | printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority); |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | #if 0 |
| 235 | printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver); |
| 236 | #endif |
| 237 | |
| 238 | if (deliver) |
Paul Mackerras | 3cf658b | 2011-06-29 00:18:52 +0000 | [diff] [blame] | 239 | kvmppc_inject_interrupt(vcpu, vec, 0); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 240 | |
| 241 | return deliver; |
| 242 | } |
| 243 | |
Alexander Graf | 17bd158 | 2010-08-30 10:44:15 +0200 | [diff] [blame] | 244 | /* |
| 245 | * This function determines if an irqprio should be cleared once issued. |
| 246 | */ |
| 247 | static bool clear_irqprio(struct kvm_vcpu *vcpu, unsigned int priority) |
| 248 | { |
| 249 | switch (priority) { |
| 250 | case BOOK3S_IRQPRIO_DECREMENTER: |
| 251 | /* DEC interrupts get cleared by mtdec */ |
| 252 | return false; |
| 253 | case BOOK3S_IRQPRIO_EXTERNAL_LEVEL: |
| 254 | /* External interrupts get cleared by userspace */ |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | return true; |
| 259 | } |
| 260 | |
Scott Wood | 7e28e60e | 2011-11-08 18:23:20 -0600 | [diff] [blame] | 261 | void kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu) |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 262 | { |
| 263 | unsigned long *pending = &vcpu->arch.pending_exceptions; |
Alexander Graf | 90bba35 | 2010-07-29 14:47:51 +0200 | [diff] [blame] | 264 | unsigned long old_pending = vcpu->arch.pending_exceptions; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 265 | unsigned int priority; |
| 266 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 267 | #ifdef EXIT_DEBUG |
| 268 | if (vcpu->arch.pending_exceptions) |
| 269 | printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions); |
| 270 | #endif |
| 271 | priority = __ffs(*pending); |
Alexander Graf | ada7ba1 | 2010-04-16 00:11:56 +0200 | [diff] [blame] | 272 | while (priority < BOOK3S_IRQPRIO_MAX) { |
Alexander Graf | 7706664d | 2009-12-21 20:21:24 +0100 | [diff] [blame] | 273 | if (kvmppc_book3s_irqprio_deliver(vcpu, priority) && |
Alexander Graf | 17bd158 | 2010-08-30 10:44:15 +0200 | [diff] [blame] | 274 | clear_irqprio(vcpu, priority)) { |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 275 | clear_bit(priority, &vcpu->arch.pending_exceptions); |
| 276 | break; |
| 277 | } |
| 278 | |
| 279 | priority = find_next_bit(pending, |
| 280 | BITS_PER_BYTE * sizeof(*pending), |
| 281 | priority + 1); |
| 282 | } |
Alexander Graf | 90bba35 | 2010-07-29 14:47:51 +0200 | [diff] [blame] | 283 | |
| 284 | /* Tell the guest about our interrupt status */ |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 285 | kvmppc_update_int_pending(vcpu, *pending, old_pending); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Alexander Graf | e850894 | 2010-07-29 14:47:54 +0200 | [diff] [blame] | 288 | pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn) |
| 289 | { |
| 290 | ulong mp_pa = vcpu->arch.magic_page_pa; |
| 291 | |
| 292 | /* Magic page override */ |
| 293 | if (unlikely(mp_pa) && |
| 294 | unlikely(((gfn << PAGE_SHIFT) & KVM_PAM) == |
| 295 | ((mp_pa & PAGE_MASK) & KVM_PAM))) { |
| 296 | ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK; |
| 297 | pfn_t pfn; |
| 298 | |
| 299 | pfn = (pfn_t)virt_to_phys((void*)shared_page) >> PAGE_SHIFT; |
| 300 | get_page(pfn_to_page(pfn)); |
| 301 | return pfn; |
| 302 | } |
| 303 | |
| 304 | return gfn_to_pfn(vcpu->kvm, gfn); |
| 305 | } |
| 306 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 307 | static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data, |
| 308 | struct kvmppc_pte *pte) |
| 309 | { |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 310 | int relocated = (vcpu->arch.shared->msr & (data ? MSR_DR : MSR_IR)); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 311 | int r; |
| 312 | |
| 313 | if (relocated) { |
| 314 | r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data); |
| 315 | } else { |
| 316 | pte->eaddr = eaddr; |
Alexander Graf | 28e83b4 | 2010-07-29 14:47:52 +0200 | [diff] [blame] | 317 | pte->raddr = eaddr & KVM_PAM; |
Alexander Graf | 3eeafd7 | 2010-03-24 21:48:17 +0100 | [diff] [blame] | 318 | pte->vpage = VSID_REAL | eaddr >> 12; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 319 | pte->may_read = true; |
| 320 | pte->may_write = true; |
| 321 | pte->may_execute = true; |
| 322 | r = 0; |
| 323 | } |
| 324 | |
| 325 | return r; |
| 326 | } |
| 327 | |
| 328 | static hva_t kvmppc_bad_hva(void) |
| 329 | { |
| 330 | return PAGE_OFFSET; |
| 331 | } |
| 332 | |
| 333 | static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte, |
| 334 | bool read) |
| 335 | { |
| 336 | hva_t hpage; |
| 337 | |
| 338 | if (read && !pte->may_read) |
| 339 | goto err; |
| 340 | |
| 341 | if (!read && !pte->may_write) |
| 342 | goto err; |
| 343 | |
| 344 | hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT); |
| 345 | if (kvm_is_error_hva(hpage)) |
| 346 | goto err; |
| 347 | |
| 348 | return hpage | (pte->raddr & ~PAGE_MASK); |
| 349 | err: |
| 350 | return kvmppc_bad_hva(); |
| 351 | } |
| 352 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 353 | int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr, |
| 354 | bool data) |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 355 | { |
| 356 | struct kvmppc_pte pte; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 357 | |
| 358 | vcpu->stat.st++; |
| 359 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 360 | if (kvmppc_xlate(vcpu, *eaddr, data, &pte)) |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 361 | return -ENOENT; |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 362 | |
| 363 | *eaddr = pte.raddr; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 364 | |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 365 | if (!pte.may_write) |
| 366 | return -EPERM; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 367 | |
Alexander Graf | 9fb244a | 2010-03-24 21:48:32 +0100 | [diff] [blame] | 368 | if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size)) |
| 369 | return EMULATE_DO_MMIO; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 370 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 371 | return EMULATE_DONE; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 374 | 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] | 375 | bool data) |
| 376 | { |
| 377 | struct kvmppc_pte pte; |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 378 | hva_t hva = *eaddr; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 379 | |
| 380 | vcpu->stat.ld++; |
| 381 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 382 | if (kvmppc_xlate(vcpu, *eaddr, data, &pte)) |
| 383 | goto nopte; |
| 384 | |
| 385 | *eaddr = pte.raddr; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 386 | |
| 387 | hva = kvmppc_pte_to_hva(vcpu, &pte, true); |
| 388 | if (kvm_is_error_hva(hva)) |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 389 | goto mmio; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 390 | |
| 391 | if (copy_from_user(ptr, (void __user *)hva, size)) { |
| 392 | printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", 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 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 396 | return EMULATE_DONE; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 397 | |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 398 | nopte: |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 399 | return -ENOENT; |
Alexander Graf | 5467a97 | 2010-02-19 11:00:38 +0100 | [diff] [blame] | 400 | mmio: |
| 401 | return EMULATE_DO_MMIO; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 404 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
| 405 | { |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 410 | { |
| 411 | int i; |
| 412 | |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 413 | regs->pc = kvmppc_get_pc(vcpu); |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 414 | regs->cr = kvmppc_get_cr(vcpu); |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 415 | regs->ctr = kvmppc_get_ctr(vcpu); |
| 416 | regs->lr = kvmppc_get_lr(vcpu); |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 417 | regs->xer = kvmppc_get_xer(vcpu); |
Alexander Graf | 666e725 | 2010-07-29 14:47:43 +0200 | [diff] [blame] | 418 | regs->msr = vcpu->arch.shared->msr; |
Alexander Graf | de7906c | 2010-07-29 14:47:46 +0200 | [diff] [blame] | 419 | regs->srr0 = vcpu->arch.shared->srr0; |
| 420 | regs->srr1 = vcpu->arch.shared->srr1; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 421 | regs->pid = vcpu->arch.pid; |
Alexander Graf | a73a959 | 2010-07-29 14:47:47 +0200 | [diff] [blame] | 422 | regs->sprg0 = vcpu->arch.shared->sprg0; |
| 423 | regs->sprg1 = vcpu->arch.shared->sprg1; |
| 424 | regs->sprg2 = vcpu->arch.shared->sprg2; |
| 425 | regs->sprg3 = vcpu->arch.shared->sprg3; |
Peter Tyser | bc9c193 | 2010-12-29 13:51:25 -0600 | [diff] [blame] | 426 | regs->sprg4 = vcpu->arch.sprg4; |
| 427 | regs->sprg5 = vcpu->arch.sprg5; |
| 428 | regs->sprg6 = vcpu->arch.sprg6; |
| 429 | regs->sprg7 = vcpu->arch.sprg7; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 430 | |
| 431 | for (i = 0; i < ARRAY_SIZE(regs->gpr); i++) |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 432 | regs->gpr[i] = kvmppc_get_gpr(vcpu, i); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 438 | { |
| 439 | int i; |
| 440 | |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 441 | kvmppc_set_pc(vcpu, regs->pc); |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 442 | kvmppc_set_cr(vcpu, regs->cr); |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 443 | kvmppc_set_ctr(vcpu, regs->ctr); |
| 444 | kvmppc_set_lr(vcpu, regs->lr); |
Alexander Graf | 992b5b2 | 2010-01-08 02:58:02 +0100 | [diff] [blame] | 445 | kvmppc_set_xer(vcpu, regs->xer); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 446 | kvmppc_set_msr(vcpu, regs->msr); |
Alexander Graf | de7906c | 2010-07-29 14:47:46 +0200 | [diff] [blame] | 447 | vcpu->arch.shared->srr0 = regs->srr0; |
| 448 | vcpu->arch.shared->srr1 = regs->srr1; |
Alexander Graf | a73a959 | 2010-07-29 14:47:47 +0200 | [diff] [blame] | 449 | vcpu->arch.shared->sprg0 = regs->sprg0; |
| 450 | vcpu->arch.shared->sprg1 = regs->sprg1; |
| 451 | vcpu->arch.shared->sprg2 = regs->sprg2; |
| 452 | vcpu->arch.shared->sprg3 = regs->sprg3; |
Peter Tyser | bc9c193 | 2010-12-29 13:51:25 -0600 | [diff] [blame] | 453 | vcpu->arch.sprg4 = regs->sprg4; |
| 454 | vcpu->arch.sprg5 = regs->sprg5; |
| 455 | vcpu->arch.sprg6 = regs->sprg6; |
| 456 | vcpu->arch.sprg7 = regs->sprg7; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 457 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 458 | for (i = 0; i < ARRAY_SIZE(regs->gpr); i++) |
| 459 | kvmppc_set_gpr(vcpu, i, regs->gpr[i]); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 464 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 465 | { |
| 466 | return -ENOTSUPP; |
| 467 | } |
| 468 | |
| 469 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 470 | { |
| 471 | return -ENOTSUPP; |
| 472 | } |
| 473 | |
| 474 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 475 | struct kvm_translation *tr) |
| 476 | { |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | /* |
| 481 | * Get (and clear) the dirty memory log for a memory slot. |
| 482 | */ |
| 483 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, |
| 484 | struct kvm_dirty_log *log) |
| 485 | { |
| 486 | struct kvm_memory_slot *memslot; |
| 487 | struct kvm_vcpu *vcpu; |
| 488 | ulong ga, ga_end; |
| 489 | int is_dirty = 0; |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 490 | int r; |
| 491 | unsigned long n; |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 492 | |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 493 | mutex_lock(&kvm->slots_lock); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 494 | |
| 495 | r = kvm_get_dirty_log(kvm, log, &is_dirty); |
| 496 | if (r) |
| 497 | goto out; |
| 498 | |
| 499 | /* If nothing is dirty, don't bother messing with page tables. */ |
| 500 | if (is_dirty) { |
Xiao Guangrong | 28a3754 | 2011-11-24 19:04:35 +0800 | [diff] [blame] | 501 | memslot = id_to_memslot(kvm->memslots, log->slot); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 502 | |
| 503 | ga = memslot->base_gfn << PAGE_SHIFT; |
| 504 | ga_end = ga + (memslot->npages << PAGE_SHIFT); |
| 505 | |
| 506 | kvm_for_each_vcpu(n, vcpu, kvm) |
| 507 | kvmppc_mmu_pte_pflush(vcpu, ga, ga_end); |
| 508 | |
Takuya Yoshikawa | 87bf6e7 | 2010-04-12 19:35:35 +0900 | [diff] [blame] | 509 | n = kvm_dirty_bitmap_bytes(memslot); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 510 | memset(memslot->dirty_bitmap, 0, n); |
| 511 | } |
| 512 | |
| 513 | r = 0; |
| 514 | out: |
Marcelo Tosatti | 79fac95 | 2009-12-23 14:35:26 -0200 | [diff] [blame] | 515 | mutex_unlock(&kvm->slots_lock); |
Alexander Graf | 2f4cf5e | 2009-10-30 05:47:10 +0000 | [diff] [blame] | 516 | return r; |
| 517 | } |