blob: 5d0babefe9137d0552cc949f9dfa3e8bc6ec6b12 [file] [log] [blame]
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001/*
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 Rothwell329d20b2010-04-27 15:49:17 +100019#include <linux/slab.h>
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000020
21#include <asm/reg.h>
22#include <asm/cputable.h>
23#include <asm/cacheflush.h>
24#include <asm/tlbflush.h>
25#include <asm/uaccess.h>
26#include <asm/io.h>
27#include <asm/kvm_ppc.h>
28#include <asm/kvm_book3s.h>
29#include <asm/mmu_context.h>
Paul Mackerras149dbdb2011-06-29 00:16:42 +000030#include <asm/page.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/gfp.h>
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000032#include <linux/sched.h>
33#include <linux/vmalloc.h>
Alexander Graf9fb244a2010-03-24 21:48:32 +010034#include <linux/highmem.h>
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000035
Paul Mackerrasc4befc52011-06-29 00:17:33 +000036#include "trace.h"
37
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000038#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
39
40/* #define EXIT_DEBUG */
Alexander Graf180a34d2010-01-15 14:49:11 +010041/* #define DEBUG_EXT */
42
Alexander Grafc8c0b6f2010-02-19 11:00:34 +010043static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
44 ulong msr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000045
Alexander Graf07b09072010-04-16 00:11:53 +020046/* Some compatibility defines */
47#ifdef CONFIG_PPC_BOOK3S_32
48#define MSR_USER32 MSR_USER
49#define MSR_USER64 MSR_USER
50#define HW_PAGE_SIZE PAGE_SIZE
51#endif
52
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000053struct kvm_stats_debugfs_item debugfs_entries[] = {
54 { "exits", VCPU_STAT(sum_exits) },
55 { "mmio", VCPU_STAT(mmio_exits) },
56 { "sig", VCPU_STAT(signal_exits) },
57 { "sysc", VCPU_STAT(syscall_exits) },
58 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
59 { "dec", VCPU_STAT(dec_exits) },
60 { "ext_intr", VCPU_STAT(ext_intr_exits) },
61 { "queue_intr", VCPU_STAT(queue_intr) },
62 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
63 { "pf_storage", VCPU_STAT(pf_storage) },
64 { "sp_storage", VCPU_STAT(sp_storage) },
65 { "pf_instruc", VCPU_STAT(pf_instruc) },
66 { "sp_instruc", VCPU_STAT(sp_instruc) },
67 { "ld", VCPU_STAT(ld) },
68 { "ld_slow", VCPU_STAT(ld_slow) },
69 { "st", VCPU_STAT(st) },
70 { "st_slow", VCPU_STAT(st_slow) },
71 { NULL }
72};
73
74void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
75{
76}
77
78void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
79{
80}
81
82void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
83{
Alexander Grafc7f38f42010-04-16 00:11:40 +020084#ifdef CONFIG_PPC_BOOK3S_64
85 memcpy(to_svcpu(vcpu)->slb, to_book3s(vcpu)->slb_shadow, sizeof(to_svcpu(vcpu)->slb));
86 memcpy(&get_paca()->shadow_vcpu, to_book3s(vcpu)->shadow_vcpu,
Alexander Graf7e57cba2010-01-08 02:58:03 +010087 sizeof(get_paca()->shadow_vcpu));
Alexander Grafc7f38f42010-04-16 00:11:40 +020088 to_svcpu(vcpu)->slb_max = to_book3s(vcpu)->slb_shadow_max;
89#endif
90
91#ifdef CONFIG_PPC_BOOK3S_32
92 current->thread.kvm_shadow_vcpu = to_book3s(vcpu)->shadow_vcpu;
93#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000094}
95
96void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
97{
Alexander Grafc7f38f42010-04-16 00:11:40 +020098#ifdef CONFIG_PPC_BOOK3S_64
99 memcpy(to_book3s(vcpu)->slb_shadow, to_svcpu(vcpu)->slb, sizeof(to_svcpu(vcpu)->slb));
100 memcpy(to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
Alexander Graf7e57cba2010-01-08 02:58:03 +0100101 sizeof(get_paca()->shadow_vcpu));
Alexander Grafc7f38f42010-04-16 00:11:40 +0200102 to_book3s(vcpu)->slb_shadow_max = to_svcpu(vcpu)->slb_max;
103#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100104
105 kvmppc_giveup_ext(vcpu, MSR_FP);
106 kvmppc_giveup_ext(vcpu, MSR_VEC);
107 kvmppc_giveup_ext(vcpu, MSR_VSX);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000108}
109
Alexander Grafa76f8492010-01-15 14:49:14 +0100110static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
111{
Alexander Graf666e7252010-07-29 14:47:43 +0200112 ulong smsr = vcpu->arch.shared->msr;
113
Alexander Grafa76f8492010-01-15 14:49:14 +0100114 /* Guest MSR values */
Alexander Graf666e7252010-07-29 14:47:43 +0200115 smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE | MSR_DE;
Alexander Grafa76f8492010-01-15 14:49:14 +0100116 /* Process MSR values */
Alexander Graf666e7252010-07-29 14:47:43 +0200117 smsr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | MSR_EE;
Alexander Grafa76f8492010-01-15 14:49:14 +0100118 /* External providers the guest reserved */
Alexander Graf666e7252010-07-29 14:47:43 +0200119 smsr |= (vcpu->arch.shared->msr & vcpu->arch.guest_owned_ext);
Alexander Grafa76f8492010-01-15 14:49:14 +0100120 /* 64-bit Process MSR values */
121#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf666e7252010-07-29 14:47:43 +0200122 smsr |= MSR_ISF | MSR_HV;
Alexander Grafa76f8492010-01-15 14:49:14 +0100123#endif
Alexander Graf666e7252010-07-29 14:47:43 +0200124 vcpu->arch.shadow_msr = smsr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100125}
126
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000127void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
128{
Alexander Graf666e7252010-07-29 14:47:43 +0200129 ulong old_msr = vcpu->arch.shared->msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000130
131#ifdef EXIT_DEBUG
132 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
133#endif
Alexander Grafa76f8492010-01-15 14:49:14 +0100134
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000135 msr &= to_book3s(vcpu)->msr_mask;
Alexander Graf666e7252010-07-29 14:47:43 +0200136 vcpu->arch.shared->msr = msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100137 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000138
Alexander Graf296c19d2010-08-15 08:39:19 +0200139 if (msr & MSR_POW) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000140 if (!vcpu->arch.pending_exceptions) {
141 kvm_vcpu_block(vcpu);
142 vcpu->stat.halt_wakeup++;
Alexander Graf296c19d2010-08-15 08:39:19 +0200143
144 /* Unset POW bit after we woke up */
145 msr &= ~MSR_POW;
146 vcpu->arch.shared->msr = msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000147 }
148 }
149
Alexander Graf666e7252010-07-29 14:47:43 +0200150 if ((vcpu->arch.shared->msr & (MSR_PR|MSR_IR|MSR_DR)) !=
Alexander Graff7bc74e2010-04-20 02:49:48 +0200151 (old_msr & (MSR_PR|MSR_IR|MSR_DR))) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000152 kvmppc_mmu_flush_segments(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +0200153 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
Alexander Graf4cb6b7e2010-08-02 16:08:22 +0200154
155 /* Preload magic page segment when in kernel mode */
156 if (!(msr & MSR_PR) && vcpu->arch.magic_page_pa) {
157 struct kvm_vcpu_arch *a = &vcpu->arch;
158
159 if (msr & MSR_DR)
160 kvmppc_mmu_map_segment(vcpu, a->magic_page_ea);
161 else
162 kvmppc_mmu_map_segment(vcpu, a->magic_page_pa);
163 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000164 }
Alexander Grafd1bab742010-02-19 11:00:35 +0100165
166 /* Preload FPU if it's enabled */
Alexander Graf666e7252010-07-29 14:47:43 +0200167 if (vcpu->arch.shared->msr & MSR_FP)
Alexander Grafd1bab742010-02-19 11:00:35 +0100168 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000169}
170
171void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
172{
Alexander Grafde7906c2010-07-29 14:47:46 +0200173 vcpu->arch.shared->srr0 = kvmppc_get_pc(vcpu);
174 vcpu->arch.shared->srr1 = vcpu->arch.shared->msr | flags;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200175 kvmppc_set_pc(vcpu, to_book3s(vcpu)->hior + vec);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000176 vcpu->arch.mmu.reset_msr(vcpu);
177}
178
Alexander Graf583617b2009-12-21 20:21:23 +0100179static int kvmppc_book3s_vec2irqprio(unsigned int vec)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000180{
181 unsigned int prio;
182
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000183 switch (vec) {
184 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
185 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
186 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
187 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
188 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
189 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
190 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
Alexander Graf17bd1582010-08-30 10:44:15 +0200191 case 0x501: prio = BOOK3S_IRQPRIO_EXTERNAL_LEVEL; break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000192 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
193 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
194 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
195 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
196 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
197 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
198 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
199 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
200 default: prio = BOOK3S_IRQPRIO_MAX; break;
201 }
202
Alexander Graf583617b2009-12-21 20:21:23 +0100203 return prio;
204}
205
Alexander Graf7706664d2009-12-21 20:21:24 +0100206static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
207 unsigned int vec)
208{
209 clear_bit(kvmppc_book3s_vec2irqprio(vec),
210 &vcpu->arch.pending_exceptions);
Alexander Graf9ee18b12010-08-05 12:24:40 +0200211
212 if (!vcpu->arch.pending_exceptions)
213 vcpu->arch.shared->int_pending = 0;
Alexander Graf7706664d2009-12-21 20:21:24 +0100214}
215
Alexander Graf583617b2009-12-21 20:21:23 +0100216void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
217{
218 vcpu->stat.queue_intr++;
219
220 set_bit(kvmppc_book3s_vec2irqprio(vec),
221 &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000222#ifdef EXIT_DEBUG
223 printk(KERN_INFO "Queueing interrupt %x\n", vec);
224#endif
225}
226
227
Alexander Graf25a8a022010-01-08 02:58:07 +0100228void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000229{
Alexander Graf25a8a022010-01-08 02:58:07 +0100230 to_book3s(vcpu)->prog_flags = flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000231 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM);
232}
233
234void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
235{
236 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
237}
238
239int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
240{
Paul Mackerras44075d92011-05-11 00:38:50 +0000241 return test_bit(BOOK3S_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000242}
243
Alexander Graf7706664d2009-12-21 20:21:24 +0100244void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
245{
246 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
247}
248
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000249void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
250 struct kvm_interrupt *irq)
251{
Alexander Graf17bd1582010-08-30 10:44:15 +0200252 unsigned int vec = BOOK3S_INTERRUPT_EXTERNAL;
253
254 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
255 vec = BOOK3S_INTERRUPT_EXTERNAL_LEVEL;
256
257 kvmppc_book3s_queue_irqprio(vcpu, vec);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000258}
259
Alexander Graf18978762010-03-24 21:48:18 +0100260void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
261 struct kvm_interrupt *irq)
262{
263 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
Alexander Graf17bd1582010-08-30 10:44:15 +0200264 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
Alexander Graf18978762010-03-24 21:48:18 +0100265}
266
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000267int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
268{
269 int deliver = 1;
270 int vec = 0;
Alexander Graf25a8a022010-01-08 02:58:07 +0100271 ulong flags = 0ULL;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200272 ulong crit_raw = vcpu->arch.shared->critical;
273 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
274 bool crit;
275
276 /* Truncate crit indicators in 32 bit mode */
277 if (!(vcpu->arch.shared->msr & MSR_SF)) {
278 crit_raw &= 0xffffffff;
279 crit_r1 &= 0xffffffff;
280 }
281
282 /* Critical section when crit == r1 */
283 crit = (crit_raw == crit_r1);
284 /* ... and we're in supervisor mode */
285 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000286
287 switch (priority) {
288 case BOOK3S_IRQPRIO_DECREMENTER:
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200289 deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000290 vec = BOOK3S_INTERRUPT_DECREMENTER;
291 break;
292 case BOOK3S_IRQPRIO_EXTERNAL:
Alexander Graf17bd1582010-08-30 10:44:15 +0200293 case BOOK3S_IRQPRIO_EXTERNAL_LEVEL:
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200294 deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000295 vec = BOOK3S_INTERRUPT_EXTERNAL;
296 break;
297 case BOOK3S_IRQPRIO_SYSTEM_RESET:
298 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
299 break;
300 case BOOK3S_IRQPRIO_MACHINE_CHECK:
301 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
302 break;
303 case BOOK3S_IRQPRIO_DATA_STORAGE:
304 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
305 break;
306 case BOOK3S_IRQPRIO_INST_STORAGE:
307 vec = BOOK3S_INTERRUPT_INST_STORAGE;
308 break;
309 case BOOK3S_IRQPRIO_DATA_SEGMENT:
310 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
311 break;
312 case BOOK3S_IRQPRIO_INST_SEGMENT:
313 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
314 break;
315 case BOOK3S_IRQPRIO_ALIGNMENT:
316 vec = BOOK3S_INTERRUPT_ALIGNMENT;
317 break;
318 case BOOK3S_IRQPRIO_PROGRAM:
319 vec = BOOK3S_INTERRUPT_PROGRAM;
Alexander Graf25a8a022010-01-08 02:58:07 +0100320 flags = to_book3s(vcpu)->prog_flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000321 break;
322 case BOOK3S_IRQPRIO_VSX:
323 vec = BOOK3S_INTERRUPT_VSX;
324 break;
325 case BOOK3S_IRQPRIO_ALTIVEC:
326 vec = BOOK3S_INTERRUPT_ALTIVEC;
327 break;
328 case BOOK3S_IRQPRIO_FP_UNAVAIL:
329 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
330 break;
331 case BOOK3S_IRQPRIO_SYSCALL:
332 vec = BOOK3S_INTERRUPT_SYSCALL;
333 break;
334 case BOOK3S_IRQPRIO_DEBUG:
335 vec = BOOK3S_INTERRUPT_TRACE;
336 break;
337 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
338 vec = BOOK3S_INTERRUPT_PERFMON;
339 break;
340 default:
341 deliver = 0;
342 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
343 break;
344 }
345
346#if 0
347 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
348#endif
349
350 if (deliver)
Alexander Graf25a8a022010-01-08 02:58:07 +0100351 kvmppc_inject_interrupt(vcpu, vec, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000352
353 return deliver;
354}
355
Alexander Graf17bd1582010-08-30 10:44:15 +0200356/*
357 * This function determines if an irqprio should be cleared once issued.
358 */
359static bool clear_irqprio(struct kvm_vcpu *vcpu, unsigned int priority)
360{
361 switch (priority) {
362 case BOOK3S_IRQPRIO_DECREMENTER:
363 /* DEC interrupts get cleared by mtdec */
364 return false;
365 case BOOK3S_IRQPRIO_EXTERNAL_LEVEL:
366 /* External interrupts get cleared by userspace */
367 return false;
368 }
369
370 return true;
371}
372
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000373void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
374{
375 unsigned long *pending = &vcpu->arch.pending_exceptions;
Alexander Graf90bba352010-07-29 14:47:51 +0200376 unsigned long old_pending = vcpu->arch.pending_exceptions;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000377 unsigned int priority;
378
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000379#ifdef EXIT_DEBUG
380 if (vcpu->arch.pending_exceptions)
381 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
382#endif
383 priority = __ffs(*pending);
Alexander Grafada7ba12010-04-16 00:11:56 +0200384 while (priority < BOOK3S_IRQPRIO_MAX) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100385 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
Alexander Graf17bd1582010-08-30 10:44:15 +0200386 clear_irqprio(vcpu, priority)) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000387 clear_bit(priority, &vcpu->arch.pending_exceptions);
388 break;
389 }
390
391 priority = find_next_bit(pending,
392 BITS_PER_BYTE * sizeof(*pending),
393 priority + 1);
394 }
Alexander Graf90bba352010-07-29 14:47:51 +0200395
396 /* Tell the guest about our interrupt status */
397 if (*pending)
398 vcpu->arch.shared->int_pending = 1;
399 else if (old_pending)
400 vcpu->arch.shared->int_pending = 0;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000401}
402
403void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
404{
Alexander Grafb83d4a92010-04-20 02:49:54 +0200405 u32 host_pvr;
406
Alexander Grafe15a1132009-11-30 03:02:02 +0000407 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000408 vcpu->arch.pvr = pvr;
Alexander Graf07b09072010-04-16 00:11:53 +0200409#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000410 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
411 kvmppc_mmu_book3s_64_init(vcpu);
412 to_book3s(vcpu)->hior = 0xfff00000;
413 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
Alexander Graf07b09072010-04-16 00:11:53 +0200414 } else
415#endif
416 {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000417 kvmppc_mmu_book3s_32_init(vcpu);
418 to_book3s(vcpu)->hior = 0;
419 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
420 }
421
422 /* If we are in hypervisor level on 970, we can tell the CPU to
423 * treat DCBZ as 32 bytes store */
424 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
425 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
426 !strcmp(cur_cpu_spec->platform, "ppc970"))
427 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
428
Alexander Graf05b0ab12010-03-24 21:48:37 +0100429 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
430 really needs them in a VM on Cell and force disable them. */
431 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
432 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
Alexander Graf07b09072010-04-16 00:11:53 +0200433
434#ifdef CONFIG_PPC_BOOK3S_32
435 /* 32 bit Book3S always has 32 byte dcbz */
436 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
437#endif
Alexander Grafb83d4a92010-04-20 02:49:54 +0200438
439 /* On some CPUs we can execute paired single operations natively */
440 asm ( "mfpvr %0" : "=r"(host_pvr));
441 switch (host_pvr) {
442 case 0x00080200: /* lonestar 2.0 */
443 case 0x00088202: /* lonestar 2.2 */
444 case 0x70000100: /* gekko 1.0 */
445 case 0x00080100: /* gekko 2.0 */
446 case 0x00083203: /* gekko 2.3a */
447 case 0x00083213: /* gekko 2.3b */
448 case 0x00083204: /* gekko 2.4 */
449 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
450 case 0x00087200: /* broadway */
451 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
452 /* Enable HID2.PSE - in case we need it later */
453 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
454 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000455}
456
Alexander Grafe8508942010-07-29 14:47:54 +0200457pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn)
458{
459 ulong mp_pa = vcpu->arch.magic_page_pa;
460
461 /* Magic page override */
462 if (unlikely(mp_pa) &&
463 unlikely(((gfn << PAGE_SHIFT) & KVM_PAM) ==
464 ((mp_pa & PAGE_MASK) & KVM_PAM))) {
465 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
466 pfn_t pfn;
467
468 pfn = (pfn_t)virt_to_phys((void*)shared_page) >> PAGE_SHIFT;
469 get_page(pfn_to_page(pfn));
470 return pfn;
471 }
472
473 return gfn_to_pfn(vcpu->kvm, gfn);
474}
475
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000476/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
477 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
478 * emulate 32 bytes dcbz length.
479 *
480 * The Book3s_64 inventors also realized this case and implemented a special bit
481 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
482 *
483 * My approach here is to patch the dcbz instruction on executing pages.
484 */
485static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
486{
Alexander Graf9fb244a2010-03-24 21:48:32 +0100487 struct page *hpage;
488 u64 hpage_offset;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000489 u32 *page;
490 int i;
491
Alexander Graf9fb244a2010-03-24 21:48:32 +0100492 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
Wei Yongjun646bab52010-08-17 10:08:52 +0800493 if (is_error_page(hpage)) {
494 kvm_release_page_clean(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000495 return;
Wei Yongjun646bab52010-08-17 10:08:52 +0800496 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000497
Alexander Graf9fb244a2010-03-24 21:48:32 +0100498 hpage_offset = pte->raddr & ~PAGE_MASK;
499 hpage_offset &= ~0xFFFULL;
500 hpage_offset /= 4;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000501
Alexander Graf9fb244a2010-03-24 21:48:32 +0100502 get_page(hpage);
503 page = kmap_atomic(hpage, KM_USER0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000504
Alexander Graf9fb244a2010-03-24 21:48:32 +0100505 /* patch dcbz into reserved instruction, so we trap */
506 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
507 if ((page[i] & 0xff0007ff) == INS_DCBZ)
508 page[i] &= 0xfffffff7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000509
Alexander Graf9fb244a2010-03-24 21:48:32 +0100510 kunmap_atomic(page, KM_USER0);
511 put_page(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000512}
513
514static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
515 struct kvmppc_pte *pte)
516{
Alexander Graf666e7252010-07-29 14:47:43 +0200517 int relocated = (vcpu->arch.shared->msr & (data ? MSR_DR : MSR_IR));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000518 int r;
519
520 if (relocated) {
521 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
522 } else {
523 pte->eaddr = eaddr;
Alexander Graf28e83b42010-07-29 14:47:52 +0200524 pte->raddr = eaddr & KVM_PAM;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100525 pte->vpage = VSID_REAL | eaddr >> 12;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000526 pte->may_read = true;
527 pte->may_write = true;
528 pte->may_execute = true;
529 r = 0;
530 }
531
532 return r;
533}
534
535static hva_t kvmppc_bad_hva(void)
536{
537 return PAGE_OFFSET;
538}
539
540static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
541 bool read)
542{
543 hva_t hpage;
544
545 if (read && !pte->may_read)
546 goto err;
547
548 if (!read && !pte->may_write)
549 goto err;
550
551 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
552 if (kvm_is_error_hva(hpage))
553 goto err;
554
555 return hpage | (pte->raddr & ~PAGE_MASK);
556err:
557 return kvmppc_bad_hva();
558}
559
Alexander Graf5467a972010-02-19 11:00:38 +0100560int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
561 bool data)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000562{
563 struct kvmppc_pte pte;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000564
565 vcpu->stat.st++;
566
Alexander Graf5467a972010-02-19 11:00:38 +0100567 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
Alexander Graf9fb244a2010-03-24 21:48:32 +0100568 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100569
570 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000571
Alexander Graf9fb244a2010-03-24 21:48:32 +0100572 if (!pte.may_write)
573 return -EPERM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000574
Alexander Graf9fb244a2010-03-24 21:48:32 +0100575 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
576 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000577
Alexander Graf5467a972010-02-19 11:00:38 +0100578 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000579}
580
Alexander Graf5467a972010-02-19 11:00:38 +0100581int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000582 bool data)
583{
584 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100585 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000586
587 vcpu->stat.ld++;
588
Alexander Graf5467a972010-02-19 11:00:38 +0100589 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
590 goto nopte;
591
592 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000593
594 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
595 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100596 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000597
598 if (copy_from_user(ptr, (void __user *)hva, size)) {
599 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100600 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000601 }
602
Alexander Graf5467a972010-02-19 11:00:38 +0100603 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000604
Alexander Graf5467a972010-02-19 11:00:38 +0100605nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000606 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100607mmio:
608 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000609}
610
611static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
612{
Alexander Grafe8508942010-07-29 14:47:54 +0200613 ulong mp_pa = vcpu->arch.magic_page_pa;
614
615 if (unlikely(mp_pa) &&
616 unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) {
617 return 1;
618 }
619
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000620 return kvm_is_visible_gfn(vcpu->kvm, gfn);
621}
622
623int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
624 ulong eaddr, int vec)
625{
626 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
627 int r = RESUME_GUEST;
628 int relocated;
629 int page_found = 0;
630 struct kvmppc_pte pte;
631 bool is_mmio = false;
Alexander Graf666e7252010-07-29 14:47:43 +0200632 bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
633 bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
Alexander Graff7bc74e2010-04-20 02:49:48 +0200634 u64 vsid;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000635
Alexander Graf3eeafd72010-03-24 21:48:17 +0100636 relocated = data ? dr : ir;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000637
638 /* Resolve real address if translation turned on */
639 if (relocated) {
640 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
641 } else {
642 pte.may_execute = true;
643 pte.may_read = true;
644 pte.may_write = true;
Alexander Graf28e83b42010-07-29 14:47:52 +0200645 pte.raddr = eaddr & KVM_PAM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000646 pte.eaddr = eaddr;
647 pte.vpage = eaddr >> 12;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100648 }
649
Alexander Graf666e7252010-07-29 14:47:43 +0200650 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
Alexander Graf3eeafd72010-03-24 21:48:17 +0100651 case 0:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200652 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
Alexander Graf3eeafd72010-03-24 21:48:17 +0100653 break;
654 case MSR_DR:
Alexander Graf3eeafd72010-03-24 21:48:17 +0100655 case MSR_IR:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200656 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
657
Alexander Graf666e7252010-07-29 14:47:43 +0200658 if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
Alexander Graff7bc74e2010-04-20 02:49:48 +0200659 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
660 else
661 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
662 pte.vpage |= vsid;
663
664 if (vsid == -1)
665 page_found = -EINVAL;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100666 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000667 }
668
669 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
670 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
671 /*
672 * If we do the dcbz hack, we have to NX on every execution,
673 * so we can patch the executing code. This renders our guest
674 * NX-less.
675 */
676 pte.may_execute = !data;
677 }
678
679 if (page_found == -ENOENT) {
680 /* Page not found in guest PTE entries */
Alexander Graf5e030182010-07-29 14:47:45 +0200681 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Grafd562de42010-07-29 14:47:44 +0200682 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf666e7252010-07-29 14:47:43 +0200683 vcpu->arch.shared->msr |=
684 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000685 kvmppc_book3s_queue_irqprio(vcpu, vec);
686 } else if (page_found == -EPERM) {
687 /* Storage protection */
Alexander Graf5e030182010-07-29 14:47:45 +0200688 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Grafd562de42010-07-29 14:47:44 +0200689 vcpu->arch.shared->dsisr =
690 to_svcpu(vcpu)->fault_dsisr & ~DSISR_NOHPTE;
691 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
Alexander Graf666e7252010-07-29 14:47:43 +0200692 vcpu->arch.shared->msr |=
693 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000694 kvmppc_book3s_queue_irqprio(vcpu, vec);
695 } else if (page_found == -EINVAL) {
696 /* Page not found in guest SLB */
Alexander Graf5e030182010-07-29 14:47:45 +0200697 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000698 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
699 } else if (!is_mmio &&
700 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
701 /* The guest's PTE is not mapped yet. Map on the host */
702 kvmppc_mmu_map_page(vcpu, &pte);
703 if (data)
704 vcpu->stat.sp_storage++;
705 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
706 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
707 kvmppc_patch_dcbz(vcpu, &pte);
708 } else {
709 /* MMIO */
710 vcpu->stat.mmio_exits++;
711 vcpu->arch.paddr_accessed = pte.raddr;
712 r = kvmppc_emulate_mmio(run, vcpu);
713 if ( r == RESUME_HOST_NV )
714 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000715 }
716
717 return r;
718}
719
Alexander Graf180a34d2010-01-15 14:49:11 +0100720static inline int get_fpr_index(int i)
721{
722#ifdef CONFIG_VSX
723 i *= 2;
724#endif
725 return i;
726}
727
728/* Give up external provider (FPU, Altivec, VSX) */
Alexander Grafaba3bd72010-02-19 11:00:39 +0100729void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
Alexander Graf180a34d2010-01-15 14:49:11 +0100730{
731 struct thread_struct *t = &current->thread;
732 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100733#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100734 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100735#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100736 u64 *thread_fpr = (u64*)t->fpr;
737 int i;
738
739 if (!(vcpu->arch.guest_owned_ext & msr))
740 return;
741
742#ifdef DEBUG_EXT
743 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
744#endif
745
746 switch (msr) {
747 case MSR_FP:
748 giveup_fpu(current);
749 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
750 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
751
752 vcpu->arch.fpscr = t->fpscr.val;
753 break;
754 case MSR_VEC:
755#ifdef CONFIG_ALTIVEC
756 giveup_altivec(current);
757 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
758 vcpu->arch.vscr = t->vscr;
759#endif
760 break;
761 case MSR_VSX:
762#ifdef CONFIG_VSX
763 __giveup_vsx(current);
764 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
765 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
766#endif
767 break;
768 default:
769 BUG();
770 }
771
772 vcpu->arch.guest_owned_ext &= ~msr;
773 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100774 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100775}
776
Alexander Graf89632212010-03-24 21:48:21 +0100777static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100778{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200779 ulong srr0 = kvmppc_get_pc(vcpu);
780 u32 last_inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100781 int ret;
782
Alexander Grafc7f38f42010-04-16 00:11:40 +0200783 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100784 if (ret == -ENOENT) {
Alexander Graf666e7252010-07-29 14:47:43 +0200785 ulong msr = vcpu->arch.shared->msr;
786
787 msr = kvmppc_set_field(msr, 33, 33, 1);
788 msr = kvmppc_set_field(msr, 34, 36, 0);
789 vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100790 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
Alexander Graf89632212010-03-24 21:48:21 +0100791 return EMULATE_AGAIN;
792 }
793
794 return EMULATE_DONE;
795}
796
797static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
798{
799
800 /* Need to do paired single emulation? */
801 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
802 return EMULATE_DONE;
803
804 /* Read out the instruction */
805 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100806 /* Need to emulate */
807 return EMULATE_FAIL;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100808
809 return EMULATE_AGAIN;
810}
811
Alexander Graf180a34d2010-01-15 14:49:11 +0100812/* Handle external providers (FPU, Altivec, VSX) */
813static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
814 ulong msr)
815{
816 struct thread_struct *t = &current->thread;
817 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100818#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100819 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100820#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100821 u64 *thread_fpr = (u64*)t->fpr;
822 int i;
823
Alexander Graf3c402a72010-02-19 11:00:32 +0100824 /* When we have paired singles, we emulate in software */
825 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
826 return RESUME_GUEST;
827
Alexander Graf666e7252010-07-29 14:47:43 +0200828 if (!(vcpu->arch.shared->msr & msr)) {
Alexander Graf180a34d2010-01-15 14:49:11 +0100829 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
830 return RESUME_GUEST;
831 }
832
Alexander Grafc2453692010-03-24 21:48:22 +0100833 /* We already own the ext */
834 if (vcpu->arch.guest_owned_ext & msr) {
835 return RESUME_GUEST;
836 }
837
Alexander Graf180a34d2010-01-15 14:49:11 +0100838#ifdef DEBUG_EXT
839 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
840#endif
841
842 current->thread.regs->msr |= msr;
843
844 switch (msr) {
845 case MSR_FP:
846 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
847 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
848
849 t->fpscr.val = vcpu->arch.fpscr;
850 t->fpexc_mode = 0;
851 kvmppc_load_up_fpu();
852 break;
853 case MSR_VEC:
854#ifdef CONFIG_ALTIVEC
855 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
856 t->vscr = vcpu->arch.vscr;
857 t->vrsave = -1;
858 kvmppc_load_up_altivec();
859#endif
860 break;
861 case MSR_VSX:
862#ifdef CONFIG_VSX
863 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
864 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
865 kvmppc_load_up_vsx();
866#endif
867 break;
868 default:
869 BUG();
870 }
871
872 vcpu->arch.guest_owned_ext |= msr;
873
Alexander Grafa76f8492010-01-15 14:49:14 +0100874 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100875
876 return RESUME_GUEST;
877}
878
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000879int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
880 unsigned int exit_nr)
881{
882 int r = RESUME_HOST;
883
884 vcpu->stat.sum_exits++;
885
886 run->exit_reason = KVM_EXIT_UNKNOWN;
887 run->ready_for_interrupt_injection = 1;
Alexander Grafbed1ed92010-08-02 11:06:26 +0200888
889 trace_kvm_book3s_exit(exit_nr, vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000890 kvm_resched(vcpu);
891 switch (exit_nr) {
892 case BOOK3S_INTERRUPT_INST_STORAGE:
893 vcpu->stat.pf_instruc++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200894
895#ifdef CONFIG_PPC_BOOK3S_32
896 /* We set segments as unused segments when invalidating them. So
897 * treat the respective fault as segment fault. */
898 if (to_svcpu(vcpu)->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT]
899 == SR_INVALID) {
900 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
901 r = RESUME_GUEST;
902 break;
903 }
904#endif
905
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000906 /* only care about PTEG not found errors, but leave NX alone */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200907 if (to_svcpu(vcpu)->shadow_srr1 & 0x40000000) {
908 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000909 vcpu->stat.sp_instruc++;
910 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
911 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
912 /*
913 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
914 * so we can't use the NX bit inside the guest. Let's cross our fingers,
915 * that no guest that needs the dcbz hack does NX.
916 */
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200917 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100918 r = RESUME_GUEST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000919 } else {
Alexander Graf666e7252010-07-29 14:47:43 +0200920 vcpu->arch.shared->msr |=
921 to_svcpu(vcpu)->shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000922 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000923 r = RESUME_GUEST;
924 }
925 break;
926 case BOOK3S_INTERRUPT_DATA_STORAGE:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200927 {
928 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000929 vcpu->stat.pf_storage++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200930
931#ifdef CONFIG_PPC_BOOK3S_32
932 /* We set segments as unused segments when invalidating them. So
933 * treat the respective fault as segment fault. */
934 if ((to_svcpu(vcpu)->sr[dar >> SID_SHIFT]) == SR_INVALID) {
935 kvmppc_mmu_map_segment(vcpu, dar);
936 r = RESUME_GUEST;
937 break;
938 }
939#endif
940
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000941 /* The only case we need to handle is missing shadow PTEs */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200942 if (to_svcpu(vcpu)->fault_dsisr & DSISR_NOHPTE) {
943 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000944 } else {
Alexander Graf5e030182010-07-29 14:47:45 +0200945 vcpu->arch.shared->dar = dar;
Alexander Grafd562de42010-07-29 14:47:44 +0200946 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000947 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000948 r = RESUME_GUEST;
949 }
950 break;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200951 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000952 case BOOK3S_INTERRUPT_DATA_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200953 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
Alexander Graf5e030182010-07-29 14:47:45 +0200954 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000955 kvmppc_book3s_queue_irqprio(vcpu,
956 BOOK3S_INTERRUPT_DATA_SEGMENT);
957 }
958 r = RESUME_GUEST;
959 break;
960 case BOOK3S_INTERRUPT_INST_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200961 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000962 kvmppc_book3s_queue_irqprio(vcpu,
963 BOOK3S_INTERRUPT_INST_SEGMENT);
964 }
965 r = RESUME_GUEST;
966 break;
967 /* We're good on these - the host merely wanted to get our attention */
968 case BOOK3S_INTERRUPT_DECREMENTER:
969 vcpu->stat.dec_exits++;
970 r = RESUME_GUEST;
971 break;
972 case BOOK3S_INTERRUPT_EXTERNAL:
973 vcpu->stat.ext_intr_exits++;
974 r = RESUME_GUEST;
975 break;
Alexander Graf7fdaec92010-04-20 02:49:47 +0200976 case BOOK3S_INTERRUPT_PERFMON:
977 r = RESUME_GUEST;
978 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000979 case BOOK3S_INTERRUPT_PROGRAM:
980 {
981 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100982 ulong flags;
983
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100984program_interrupt:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200985 flags = to_svcpu(vcpu)->shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000986
Alexander Graf666e7252010-07-29 14:47:43 +0200987 if (vcpu->arch.shared->msr & MSR_PR) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000988#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +0200989 printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000990#endif
Alexander Grafc7f38f42010-04-16 00:11:40 +0200991 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000992 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100993 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000994 r = RESUME_GUEST;
995 break;
996 }
997 }
998
999 vcpu->stat.emulated_inst_exits++;
1000 er = kvmppc_emulate_instruction(run, vcpu);
1001 switch (er) {
1002 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +01001003 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001004 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +01001005 case EMULATE_AGAIN:
1006 r = RESUME_GUEST;
1007 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001008 case EMULATE_FAIL:
1009 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +02001010 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
Alexander Grafff1ca3f2010-01-08 02:58:09 +01001011 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001012 r = RESUME_GUEST;
1013 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +01001014 case EMULATE_DO_MMIO:
1015 run->exit_reason = KVM_EXIT_MMIO;
1016 r = RESUME_HOST_NV;
1017 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001018 default:
1019 BUG();
1020 }
1021 break;
1022 }
1023 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafad0a0482010-03-24 21:48:30 +01001024 if (vcpu->arch.osi_enabled &&
1025 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
1026 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
Alexander Graf2a342ed2010-07-29 14:47:48 +02001027 /* MOL hypercalls */
Alexander Grafad0a0482010-03-24 21:48:30 +01001028 u64 *gprs = run->osi.gprs;
1029 int i;
1030
1031 run->exit_reason = KVM_EXIT_OSI;
1032 for (i = 0; i < 32; i++)
1033 gprs[i] = kvmppc_get_gpr(vcpu, i);
1034 vcpu->arch.osi_needed = 1;
1035 r = RESUME_HOST_NV;
Alexander Graf2a342ed2010-07-29 14:47:48 +02001036 } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
1037 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
1038 /* KVM PV hypercalls */
1039 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1040 r = RESUME_GUEST;
Alexander Grafad0a0482010-03-24 21:48:30 +01001041 } else {
Alexander Graf2a342ed2010-07-29 14:47:48 +02001042 /* Guest syscalls */
Alexander Grafad0a0482010-03-24 21:48:30 +01001043 vcpu->stat.syscall_exits++;
1044 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1045 r = RESUME_GUEST;
1046 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001047 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001048 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001049 case BOOK3S_INTERRUPT_ALTIVEC:
1050 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +01001051 {
1052 int ext_msr = 0;
1053
1054 switch (exit_nr) {
1055 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
1056 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
1057 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
1058 }
1059
1060 switch (kvmppc_check_ext(vcpu, exit_nr)) {
1061 case EMULATE_DONE:
1062 /* everything ok - let's enable the ext */
1063 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
1064 break;
1065 case EMULATE_FAIL:
1066 /* we need to emulate this instruction */
1067 goto program_interrupt;
1068 break;
1069 default:
1070 /* nothing to worry about - go again */
1071 break;
1072 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001073 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +01001074 }
Alexander Grafca7f4202010-03-24 21:48:28 +01001075 case BOOK3S_INTERRUPT_ALIGNMENT:
1076 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
Alexander Grafd562de42010-07-29 14:47:44 +02001077 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001078 kvmppc_get_last_inst(vcpu));
Alexander Graf5e030182010-07-29 14:47:45 +02001079 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001080 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +01001081 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1082 }
1083 r = RESUME_GUEST;
1084 break;
Alexander Graf180a34d2010-01-15 14:49:11 +01001085 case BOOK3S_INTERRUPT_MACHINE_CHECK:
1086 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001087 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1088 r = RESUME_GUEST;
1089 break;
1090 default:
1091 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +01001092 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +02001093 exit_nr, kvmppc_get_pc(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001094 r = RESUME_HOST;
1095 BUG();
1096 break;
1097 }
1098
1099
1100 if (!(r & RESUME_HOST)) {
1101 /* To avoid clobbering exit_reason, only check for signals if
1102 * we aren't already exiting to userspace for some other
1103 * reason. */
1104 if (signal_pending(current)) {
1105#ifdef EXIT_DEBUG
1106 printk(KERN_EMERG "KVM: Going back to host\n");
1107#endif
1108 vcpu->stat.signal_exits++;
1109 run->exit_reason = KVM_EXIT_INTR;
1110 r = -EINTR;
1111 } else {
1112 /* In case an interrupt came in that was triggered
1113 * from userspace (like DEC), we need to check what
1114 * to inject now! */
1115 kvmppc_core_deliver_interrupts(vcpu);
1116 }
1117 }
1118
Alexander Grafbed1ed92010-08-02 11:06:26 +02001119 trace_kvm_book3s_reenter(r, vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001120
1121 return r;
1122}
1123
1124int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1125{
1126 return 0;
1127}
1128
1129int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1130{
1131 int i;
1132
Alexander Grafc7f38f42010-04-16 00:11:40 +02001133 regs->pc = kvmppc_get_pc(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001134 regs->cr = kvmppc_get_cr(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001135 regs->ctr = kvmppc_get_ctr(vcpu);
1136 regs->lr = kvmppc_get_lr(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001137 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001138 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +02001139 regs->srr0 = vcpu->arch.shared->srr0;
1140 regs->srr1 = vcpu->arch.shared->srr1;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001141 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001142 regs->sprg0 = vcpu->arch.shared->sprg0;
1143 regs->sprg1 = vcpu->arch.shared->sprg1;
1144 regs->sprg2 = vcpu->arch.shared->sprg2;
1145 regs->sprg3 = vcpu->arch.shared->sprg3;
Peter Tyserbc9c1932010-12-29 13:51:25 -06001146 regs->sprg4 = vcpu->arch.sprg4;
1147 regs->sprg5 = vcpu->arch.sprg5;
1148 regs->sprg6 = vcpu->arch.sprg6;
1149 regs->sprg7 = vcpu->arch.sprg7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001150
1151 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001152 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001153
1154 return 0;
1155}
1156
1157int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1158{
1159 int i;
1160
Alexander Grafc7f38f42010-04-16 00:11:40 +02001161 kvmppc_set_pc(vcpu, regs->pc);
Alexander Graf992b5b22010-01-08 02:58:02 +01001162 kvmppc_set_cr(vcpu, regs->cr);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001163 kvmppc_set_ctr(vcpu, regs->ctr);
1164 kvmppc_set_lr(vcpu, regs->lr);
Alexander Graf992b5b22010-01-08 02:58:02 +01001165 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001166 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +02001167 vcpu->arch.shared->srr0 = regs->srr0;
1168 vcpu->arch.shared->srr1 = regs->srr1;
Alexander Grafa73a9592010-07-29 14:47:47 +02001169 vcpu->arch.shared->sprg0 = regs->sprg0;
1170 vcpu->arch.shared->sprg1 = regs->sprg1;
1171 vcpu->arch.shared->sprg2 = regs->sprg2;
1172 vcpu->arch.shared->sprg3 = regs->sprg3;
Peter Tyserbc9c1932010-12-29 13:51:25 -06001173 vcpu->arch.sprg4 = regs->sprg4;
1174 vcpu->arch.sprg5 = regs->sprg5;
1175 vcpu->arch.sprg6 = regs->sprg6;
1176 vcpu->arch.sprg7 = regs->sprg7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001177
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001178 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1179 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001180
1181 return 0;
1182}
1183
1184int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1185 struct kvm_sregs *sregs)
1186{
Alexander Grafe15a1132009-11-30 03:02:02 +00001187 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1188 int i;
1189
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001190 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +00001191
1192 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1193 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1194 for (i = 0; i < 64; i++) {
Paul Mackerrasc4befc52011-06-29 00:17:33 +00001195 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige | i;
1196 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
Alexander Grafe15a1132009-11-30 03:02:02 +00001197 }
1198 } else {
Alexander Grafdf1bfa22010-08-03 02:29:27 +02001199 for (i = 0; i < 16; i++)
1200 sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i];
1201
Alexander Grafe15a1132009-11-30 03:02:02 +00001202 for (i = 0; i < 8; i++) {
1203 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1204 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1205 }
1206 }
Avi Kivity98001d82010-05-13 11:05:49 +03001207
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001208 return 0;
1209}
1210
1211int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1212 struct kvm_sregs *sregs)
1213{
Alexander Grafe15a1132009-11-30 03:02:02 +00001214 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1215 int i;
1216
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001217 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001218
1219 vcpu3s->sdr1 = sregs->u.s.sdr1;
1220 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1221 for (i = 0; i < 64; i++) {
1222 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1223 sregs->u.s.ppc64.slb[i].slbe);
1224 }
1225 } else {
1226 for (i = 0; i < 16; i++) {
1227 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1228 }
1229 for (i = 0; i < 8; i++) {
1230 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1231 (u32)sregs->u.s.ppc32.ibat[i]);
1232 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1233 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1234 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1235 (u32)sregs->u.s.ppc32.dbat[i]);
1236 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1237 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1238 }
1239 }
1240
1241 /* Flush the MMU after messing with the segments */
1242 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Avi Kivity98001d82010-05-13 11:05:49 +03001243
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001244 return 0;
1245}
1246
1247int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1248{
1249 return -ENOTSUPP;
1250}
1251
1252int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1253{
1254 return -ENOTSUPP;
1255}
1256
1257int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1258 struct kvm_translation *tr)
1259{
1260 return 0;
1261}
1262
1263/*
1264 * Get (and clear) the dirty memory log for a memory slot.
1265 */
1266int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1267 struct kvm_dirty_log *log)
1268{
1269 struct kvm_memory_slot *memslot;
1270 struct kvm_vcpu *vcpu;
1271 ulong ga, ga_end;
1272 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001273 int r;
1274 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001275
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001276 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001277
1278 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1279 if (r)
1280 goto out;
1281
1282 /* If nothing is dirty, don't bother messing with page tables. */
1283 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001284 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001285
1286 ga = memslot->base_gfn << PAGE_SHIFT;
1287 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1288
1289 kvm_for_each_vcpu(n, vcpu, kvm)
1290 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1291
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001292 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001293 memset(memslot->dirty_bitmap, 0, n);
1294 }
1295
1296 r = 0;
1297out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001298 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001299 return r;
1300}
1301
1302int kvmppc_core_check_processor_compat(void)
1303{
1304 return 0;
1305}
1306
1307struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1308{
1309 struct kvmppc_vcpu_book3s *vcpu_book3s;
1310 struct kvm_vcpu *vcpu;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001311 int err = -ENOMEM;
Alexander Grafe8508942010-07-29 14:47:54 +02001312 unsigned long p;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001313
Takuya Yoshikawa26535032010-11-02 10:49:34 +09001314 vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Grafc7f38f42010-04-16 00:11:40 +02001315 if (!vcpu_book3s)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001316 goto out;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001317
Alexander Grafc7f38f42010-04-16 00:11:40 +02001318 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
1319 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
1320 if (!vcpu_book3s->shadow_vcpu)
1321 goto free_vcpu;
1322
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001323 vcpu = &vcpu_book3s->vcpu;
1324 err = kvm_vcpu_init(vcpu, kvm, id);
1325 if (err)
Alexander Grafc7f38f42010-04-16 00:11:40 +02001326 goto free_shadow_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001327
Alexander Grafe8508942010-07-29 14:47:54 +02001328 p = __get_free_page(GFP_KERNEL|__GFP_ZERO);
1329 /* the real shared page fills the last 4k of our page */
1330 vcpu->arch.shared = (void*)(p + PAGE_SIZE - 4096);
1331 if (!p)
Alexander Graf96bc4512010-07-29 14:47:42 +02001332 goto uninit_vcpu;
1333
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001334 vcpu->arch.host_retip = kvm_return_point;
1335 vcpu->arch.host_msr = mfmsr();
Alexander Graf07b09072010-04-16 00:11:53 +02001336#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001337 /* default to book3s_64 (970fx) */
1338 vcpu->arch.pvr = 0x3C0301;
Alexander Graf07b09072010-04-16 00:11:53 +02001339#else
1340 /* default to book3s_32 (750) */
1341 vcpu->arch.pvr = 0x84202;
1342#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001343 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
Paul Mackerrasc4befc52011-06-29 00:17:33 +00001344 vcpu->arch.slb_nr = 64;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001345
1346 /* remember where some real-mode handlers are */
Paul Mackerras149dbdb2011-06-29 00:16:42 +00001347 vcpu->arch.trampoline_lowmem = __pa(kvmppc_handler_lowmem_trampoline);
1348 vcpu->arch.trampoline_enter = __pa(kvmppc_handler_trampoline_enter);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001349 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf07b09072010-04-16 00:11:53 +02001350#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf021ec9c2010-01-08 02:58:06 +01001351 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf07b09072010-04-16 00:11:53 +02001352#else
1353 vcpu->arch.rmcall = (ulong)kvmppc_rmcall;
1354#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001355
1356 vcpu->arch.shadow_msr = MSR_USER64;
1357
Alexander Graf9cc5e952010-04-16 00:11:45 +02001358 err = kvmppc_mmu_init(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001359 if (err < 0)
Alexander Graf96bc4512010-07-29 14:47:42 +02001360 goto uninit_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001361
1362 return vcpu;
1363
Alexander Graf96bc4512010-07-29 14:47:42 +02001364uninit_vcpu:
1365 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001366free_shadow_vcpu:
1367 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001368free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001369 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001370out:
1371 return ERR_PTR(err);
1372}
1373
1374void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1375{
1376 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1377
Alexander Grafe8508942010-07-29 14:47:54 +02001378 free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001379 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001380 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001381 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001382}
1383
1384extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1385int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1386{
1387 int ret;
Andreas Schwab49f6be82010-05-31 21:59:13 +02001388 double fpr[32][TS_FPRWIDTH];
1389 unsigned int fpscr;
1390 int fpexc_mode;
Alexander Grafa2b07662010-03-24 21:48:31 +01001391#ifdef CONFIG_ALTIVEC
Andreas Schwab49f6be82010-05-31 21:59:13 +02001392 vector128 vr[32];
1393 vector128 vscr;
1394 unsigned long uninitialized_var(vrsave);
1395 int used_vr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001396#endif
1397#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001398 int used_vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001399#endif
Alexander Graf180a34d2010-01-15 14:49:11 +01001400 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001401
1402 /* No need to go into the guest when all we do is going out */
1403 if (signal_pending(current)) {
1404 kvm_run->exit_reason = KVM_EXIT_INTR;
1405 return -EINTR;
1406 }
1407
Alexander Graf180a34d2010-01-15 14:49:11 +01001408 /* Save FPU state in stack */
1409 if (current->thread.regs->msr & MSR_FP)
1410 giveup_fpu(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001411 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
1412 fpscr = current->thread.fpscr.val;
1413 fpexc_mode = current->thread.fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001414
1415#ifdef CONFIG_ALTIVEC
1416 /* Save Altivec state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001417 used_vr = current->thread.used_vr;
1418 if (used_vr) {
Alexander Graf180a34d2010-01-15 14:49:11 +01001419 if (current->thread.regs->msr & MSR_VEC)
1420 giveup_altivec(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001421 memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
1422 vscr = current->thread.vscr;
1423 vrsave = current->thread.vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001424 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001425#endif
1426
1427#ifdef CONFIG_VSX
1428 /* Save VSX state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001429 used_vsr = current->thread.used_vsr;
1430 if (used_vsr && (current->thread.regs->msr & MSR_VSX))
Alexander Graf180a34d2010-01-15 14:49:11 +01001431 __giveup_vsx(current);
Alexander Graf180a34d2010-01-15 14:49:11 +01001432#endif
1433
1434 /* Remember the MSR with disabled extensions */
1435 ext_msr = current->thread.regs->msr;
1436
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001437 /* XXX we get called with irq disabled - change that! */
1438 local_irq_enable();
1439
Alexander Grafd1bab742010-02-19 11:00:35 +01001440 /* Preload FPU if it's enabled */
Alexander Graf666e7252010-07-29 14:47:43 +02001441 if (vcpu->arch.shared->msr & MSR_FP)
Alexander Grafd1bab742010-02-19 11:00:35 +01001442 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1443
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001444 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1445
1446 local_irq_disable();
1447
Alexander Graf180a34d2010-01-15 14:49:11 +01001448 current->thread.regs->msr = ext_msr;
1449
1450 /* Make sure we save the guest FPU/Altivec/VSX state */
1451 kvmppc_giveup_ext(vcpu, MSR_FP);
1452 kvmppc_giveup_ext(vcpu, MSR_VEC);
1453 kvmppc_giveup_ext(vcpu, MSR_VSX);
1454
1455 /* Restore FPU state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001456 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
1457 current->thread.fpscr.val = fpscr;
1458 current->thread.fpexc_mode = fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001459
1460#ifdef CONFIG_ALTIVEC
1461 /* Restore Altivec state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001462 if (used_vr && current->thread.used_vr) {
1463 memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
1464 current->thread.vscr = vscr;
1465 current->thread.vrsave = vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001466 }
Andreas Schwab49f6be82010-05-31 21:59:13 +02001467 current->thread.used_vr = used_vr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001468#endif
1469
1470#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001471 current->thread.used_vsr = used_vsr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001472#endif
1473
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001474 return ret;
1475}
1476
1477static int kvmppc_book3s_init(void)
1478{
Alexander Graffef093be2010-06-30 15:18:46 +02001479 int r;
1480
1481 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
1482 THIS_MODULE);
1483
1484 if (r)
1485 return r;
1486
1487 r = kvmppc_mmu_hpte_sysinit();
1488
1489 return r;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001490}
1491
1492static void kvmppc_book3s_exit(void)
1493{
Alexander Graffef093be2010-06-30 15:18:46 +02001494 kvmppc_mmu_hpte_sysexit();
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001495 kvm_exit();
1496}
1497
1498module_init(kvmppc_book3s_init);
1499module_exit(kvmppc_book3s_exit);