blob: 83500fb62c83c3622d97ff7da14419828080d2fe [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 Grafbed1ed92010-08-02 11:06:26 +020020#include "trace.h"
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000021
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 Mackerras149dbdb2011-06-29 00:16:42 +000031#include <asm/page.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/gfp.h>
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000033#include <linux/sched.h>
34#include <linux/vmalloc.h>
Alexander Graf9fb244a2010-03-24 21:48:32 +010035#include <linux/highmem.h>
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000036
37#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
38
39/* #define EXIT_DEBUG */
Alexander Graf180a34d2010-01-15 14:49:11 +010040/* #define DEBUG_EXT */
41
Alexander Grafc8c0b6f2010-02-19 11:00:34 +010042static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
43 ulong msr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000044
Alexander Graf07b09072010-04-16 00:11:53 +020045/* Some compatibility defines */
46#ifdef CONFIG_PPC_BOOK3S_32
47#define MSR_USER32 MSR_USER
48#define MSR_USER64 MSR_USER
49#define HW_PAGE_SIZE PAGE_SIZE
50#endif
51
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000052struct kvm_stats_debugfs_item debugfs_entries[] = {
53 { "exits", VCPU_STAT(sum_exits) },
54 { "mmio", VCPU_STAT(mmio_exits) },
55 { "sig", VCPU_STAT(signal_exits) },
56 { "sysc", VCPU_STAT(syscall_exits) },
57 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
58 { "dec", VCPU_STAT(dec_exits) },
59 { "ext_intr", VCPU_STAT(ext_intr_exits) },
60 { "queue_intr", VCPU_STAT(queue_intr) },
61 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
62 { "pf_storage", VCPU_STAT(pf_storage) },
63 { "sp_storage", VCPU_STAT(sp_storage) },
64 { "pf_instruc", VCPU_STAT(pf_instruc) },
65 { "sp_instruc", VCPU_STAT(sp_instruc) },
66 { "ld", VCPU_STAT(ld) },
67 { "ld_slow", VCPU_STAT(ld_slow) },
68 { "st", VCPU_STAT(st) },
69 { "st_slow", VCPU_STAT(st_slow) },
70 { NULL }
71};
72
73void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
74{
75}
76
77void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
78{
79}
80
81void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
82{
Alexander Grafc7f38f42010-04-16 00:11:40 +020083#ifdef CONFIG_PPC_BOOK3S_64
84 memcpy(to_svcpu(vcpu)->slb, to_book3s(vcpu)->slb_shadow, sizeof(to_svcpu(vcpu)->slb));
85 memcpy(&get_paca()->shadow_vcpu, to_book3s(vcpu)->shadow_vcpu,
Alexander Graf7e57cba2010-01-08 02:58:03 +010086 sizeof(get_paca()->shadow_vcpu));
Alexander Grafc7f38f42010-04-16 00:11:40 +020087 to_svcpu(vcpu)->slb_max = to_book3s(vcpu)->slb_shadow_max;
88#endif
89
90#ifdef CONFIG_PPC_BOOK3S_32
91 current->thread.kvm_shadow_vcpu = to_book3s(vcpu)->shadow_vcpu;
92#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000093}
94
95void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
96{
Alexander Grafc7f38f42010-04-16 00:11:40 +020097#ifdef CONFIG_PPC_BOOK3S_64
98 memcpy(to_book3s(vcpu)->slb_shadow, to_svcpu(vcpu)->slb, sizeof(to_svcpu(vcpu)->slb));
99 memcpy(to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
Alexander Graf7e57cba2010-01-08 02:58:03 +0100100 sizeof(get_paca()->shadow_vcpu));
Alexander Grafc7f38f42010-04-16 00:11:40 +0200101 to_book3s(vcpu)->slb_shadow_max = to_svcpu(vcpu)->slb_max;
102#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100103
104 kvmppc_giveup_ext(vcpu, MSR_FP);
105 kvmppc_giveup_ext(vcpu, MSR_VEC);
106 kvmppc_giveup_ext(vcpu, MSR_VSX);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000107}
108
Alexander Grafa76f8492010-01-15 14:49:14 +0100109static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
110{
Alexander Graf666e7252010-07-29 14:47:43 +0200111 ulong smsr = vcpu->arch.shared->msr;
112
Alexander Grafa76f8492010-01-15 14:49:14 +0100113 /* Guest MSR values */
Alexander Graf666e7252010-07-29 14:47:43 +0200114 smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE | MSR_DE;
Alexander Grafa76f8492010-01-15 14:49:14 +0100115 /* Process MSR values */
Alexander Graf666e7252010-07-29 14:47:43 +0200116 smsr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | MSR_EE;
Alexander Grafa76f8492010-01-15 14:49:14 +0100117 /* External providers the guest reserved */
Alexander Graf666e7252010-07-29 14:47:43 +0200118 smsr |= (vcpu->arch.shared->msr & vcpu->arch.guest_owned_ext);
Alexander Grafa76f8492010-01-15 14:49:14 +0100119 /* 64-bit Process MSR values */
120#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf666e7252010-07-29 14:47:43 +0200121 smsr |= MSR_ISF | MSR_HV;
Alexander Grafa76f8492010-01-15 14:49:14 +0100122#endif
Alexander Graf666e7252010-07-29 14:47:43 +0200123 vcpu->arch.shadow_msr = smsr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100124}
125
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000126void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
127{
Alexander Graf666e7252010-07-29 14:47:43 +0200128 ulong old_msr = vcpu->arch.shared->msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000129
130#ifdef EXIT_DEBUG
131 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
132#endif
Alexander Grafa76f8492010-01-15 14:49:14 +0100133
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000134 msr &= to_book3s(vcpu)->msr_mask;
Alexander Graf666e7252010-07-29 14:47:43 +0200135 vcpu->arch.shared->msr = msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100136 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000137
Alexander Graf296c19d2010-08-15 08:39:19 +0200138 if (msr & MSR_POW) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000139 if (!vcpu->arch.pending_exceptions) {
140 kvm_vcpu_block(vcpu);
141 vcpu->stat.halt_wakeup++;
Alexander Graf296c19d2010-08-15 08:39:19 +0200142
143 /* Unset POW bit after we woke up */
144 msr &= ~MSR_POW;
145 vcpu->arch.shared->msr = msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000146 }
147 }
148
Alexander Graf666e7252010-07-29 14:47:43 +0200149 if ((vcpu->arch.shared->msr & (MSR_PR|MSR_IR|MSR_DR)) !=
Alexander Graff7bc74e2010-04-20 02:49:48 +0200150 (old_msr & (MSR_PR|MSR_IR|MSR_DR))) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000151 kvmppc_mmu_flush_segments(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +0200152 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
Alexander Graf4cb6b7e2010-08-02 16:08:22 +0200153
154 /* Preload magic page segment when in kernel mode */
155 if (!(msr & MSR_PR) && vcpu->arch.magic_page_pa) {
156 struct kvm_vcpu_arch *a = &vcpu->arch;
157
158 if (msr & MSR_DR)
159 kvmppc_mmu_map_segment(vcpu, a->magic_page_ea);
160 else
161 kvmppc_mmu_map_segment(vcpu, a->magic_page_pa);
162 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000163 }
Alexander Grafd1bab742010-02-19 11:00:35 +0100164
165 /* Preload FPU if it's enabled */
Alexander Graf666e7252010-07-29 14:47:43 +0200166 if (vcpu->arch.shared->msr & MSR_FP)
Alexander Grafd1bab742010-02-19 11:00:35 +0100167 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000168}
169
170void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
171{
Alexander Grafde7906c2010-07-29 14:47:46 +0200172 vcpu->arch.shared->srr0 = kvmppc_get_pc(vcpu);
173 vcpu->arch.shared->srr1 = vcpu->arch.shared->msr | flags;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200174 kvmppc_set_pc(vcpu, to_book3s(vcpu)->hior + vec);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000175 vcpu->arch.mmu.reset_msr(vcpu);
176}
177
Alexander Graf583617b2009-12-21 20:21:23 +0100178static int kvmppc_book3s_vec2irqprio(unsigned int vec)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000179{
180 unsigned int prio;
181
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000182 switch (vec) {
183 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
184 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
185 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
186 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
187 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
188 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
189 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
Alexander Graf17bd1582010-08-30 10:44:15 +0200190 case 0x501: prio = BOOK3S_IRQPRIO_EXTERNAL_LEVEL; break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000191 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
192 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
193 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
194 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
195 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
196 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
197 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
198 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
199 default: prio = BOOK3S_IRQPRIO_MAX; break;
200 }
201
Alexander Graf583617b2009-12-21 20:21:23 +0100202 return prio;
203}
204
Alexander Graf7706664d2009-12-21 20:21:24 +0100205static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
206 unsigned int vec)
207{
208 clear_bit(kvmppc_book3s_vec2irqprio(vec),
209 &vcpu->arch.pending_exceptions);
Alexander Graf9ee18b12010-08-05 12:24:40 +0200210
211 if (!vcpu->arch.pending_exceptions)
212 vcpu->arch.shared->int_pending = 0;
Alexander Graf7706664d2009-12-21 20:21:24 +0100213}
214
Alexander Graf583617b2009-12-21 20:21:23 +0100215void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
216{
217 vcpu->stat.queue_intr++;
218
219 set_bit(kvmppc_book3s_vec2irqprio(vec),
220 &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000221#ifdef EXIT_DEBUG
222 printk(KERN_INFO "Queueing interrupt %x\n", vec);
223#endif
224}
225
226
Alexander Graf25a8a022010-01-08 02:58:07 +0100227void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000228{
Alexander Graf25a8a022010-01-08 02:58:07 +0100229 to_book3s(vcpu)->prog_flags = flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000230 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM);
231}
232
233void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
234{
235 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
236}
237
238int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
239{
Paul Mackerras44075d92011-05-11 00:38:50 +0000240 return test_bit(BOOK3S_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000241}
242
Alexander Graf7706664d2009-12-21 20:21:24 +0100243void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
244{
245 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
246}
247
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000248void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
249 struct kvm_interrupt *irq)
250{
Alexander Graf17bd1582010-08-30 10:44:15 +0200251 unsigned int vec = BOOK3S_INTERRUPT_EXTERNAL;
252
253 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
254 vec = BOOK3S_INTERRUPT_EXTERNAL_LEVEL;
255
256 kvmppc_book3s_queue_irqprio(vcpu, vec);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000257}
258
Alexander Graf18978762010-03-24 21:48:18 +0100259void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
260 struct kvm_interrupt *irq)
261{
262 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
Alexander Graf17bd1582010-08-30 10:44:15 +0200263 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
Alexander Graf18978762010-03-24 21:48:18 +0100264}
265
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000266int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
267{
268 int deliver = 1;
269 int vec = 0;
Alexander Graf25a8a022010-01-08 02:58:07 +0100270 ulong flags = 0ULL;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200271 ulong crit_raw = vcpu->arch.shared->critical;
272 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
273 bool crit;
274
275 /* Truncate crit indicators in 32 bit mode */
276 if (!(vcpu->arch.shared->msr & MSR_SF)) {
277 crit_raw &= 0xffffffff;
278 crit_r1 &= 0xffffffff;
279 }
280
281 /* Critical section when crit == r1 */
282 crit = (crit_raw == crit_r1);
283 /* ... and we're in supervisor mode */
284 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000285
286 switch (priority) {
287 case BOOK3S_IRQPRIO_DECREMENTER:
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200288 deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000289 vec = BOOK3S_INTERRUPT_DECREMENTER;
290 break;
291 case BOOK3S_IRQPRIO_EXTERNAL:
Alexander Graf17bd1582010-08-30 10:44:15 +0200292 case BOOK3S_IRQPRIO_EXTERNAL_LEVEL:
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200293 deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000294 vec = BOOK3S_INTERRUPT_EXTERNAL;
295 break;
296 case BOOK3S_IRQPRIO_SYSTEM_RESET:
297 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
298 break;
299 case BOOK3S_IRQPRIO_MACHINE_CHECK:
300 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
301 break;
302 case BOOK3S_IRQPRIO_DATA_STORAGE:
303 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
304 break;
305 case BOOK3S_IRQPRIO_INST_STORAGE:
306 vec = BOOK3S_INTERRUPT_INST_STORAGE;
307 break;
308 case BOOK3S_IRQPRIO_DATA_SEGMENT:
309 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
310 break;
311 case BOOK3S_IRQPRIO_INST_SEGMENT:
312 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
313 break;
314 case BOOK3S_IRQPRIO_ALIGNMENT:
315 vec = BOOK3S_INTERRUPT_ALIGNMENT;
316 break;
317 case BOOK3S_IRQPRIO_PROGRAM:
318 vec = BOOK3S_INTERRUPT_PROGRAM;
Alexander Graf25a8a022010-01-08 02:58:07 +0100319 flags = to_book3s(vcpu)->prog_flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000320 break;
321 case BOOK3S_IRQPRIO_VSX:
322 vec = BOOK3S_INTERRUPT_VSX;
323 break;
324 case BOOK3S_IRQPRIO_ALTIVEC:
325 vec = BOOK3S_INTERRUPT_ALTIVEC;
326 break;
327 case BOOK3S_IRQPRIO_FP_UNAVAIL:
328 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
329 break;
330 case BOOK3S_IRQPRIO_SYSCALL:
331 vec = BOOK3S_INTERRUPT_SYSCALL;
332 break;
333 case BOOK3S_IRQPRIO_DEBUG:
334 vec = BOOK3S_INTERRUPT_TRACE;
335 break;
336 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
337 vec = BOOK3S_INTERRUPT_PERFMON;
338 break;
339 default:
340 deliver = 0;
341 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
342 break;
343 }
344
345#if 0
346 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
347#endif
348
349 if (deliver)
Alexander Graf25a8a022010-01-08 02:58:07 +0100350 kvmppc_inject_interrupt(vcpu, vec, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000351
352 return deliver;
353}
354
Alexander Graf17bd1582010-08-30 10:44:15 +0200355/*
356 * This function determines if an irqprio should be cleared once issued.
357 */
358static bool clear_irqprio(struct kvm_vcpu *vcpu, unsigned int priority)
359{
360 switch (priority) {
361 case BOOK3S_IRQPRIO_DECREMENTER:
362 /* DEC interrupts get cleared by mtdec */
363 return false;
364 case BOOK3S_IRQPRIO_EXTERNAL_LEVEL:
365 /* External interrupts get cleared by userspace */
366 return false;
367 }
368
369 return true;
370}
371
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000372void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
373{
374 unsigned long *pending = &vcpu->arch.pending_exceptions;
Alexander Graf90bba352010-07-29 14:47:51 +0200375 unsigned long old_pending = vcpu->arch.pending_exceptions;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000376 unsigned int priority;
377
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000378#ifdef EXIT_DEBUG
379 if (vcpu->arch.pending_exceptions)
380 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
381#endif
382 priority = __ffs(*pending);
Alexander Grafada7ba12010-04-16 00:11:56 +0200383 while (priority < BOOK3S_IRQPRIO_MAX) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100384 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
Alexander Graf17bd1582010-08-30 10:44:15 +0200385 clear_irqprio(vcpu, priority)) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000386 clear_bit(priority, &vcpu->arch.pending_exceptions);
387 break;
388 }
389
390 priority = find_next_bit(pending,
391 BITS_PER_BYTE * sizeof(*pending),
392 priority + 1);
393 }
Alexander Graf90bba352010-07-29 14:47:51 +0200394
395 /* Tell the guest about our interrupt status */
396 if (*pending)
397 vcpu->arch.shared->int_pending = 1;
398 else if (old_pending)
399 vcpu->arch.shared->int_pending = 0;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000400}
401
402void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
403{
Alexander Grafb83d4a92010-04-20 02:49:54 +0200404 u32 host_pvr;
405
Alexander Grafe15a1132009-11-30 03:02:02 +0000406 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000407 vcpu->arch.pvr = pvr;
Alexander Graf07b09072010-04-16 00:11:53 +0200408#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000409 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
410 kvmppc_mmu_book3s_64_init(vcpu);
411 to_book3s(vcpu)->hior = 0xfff00000;
412 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
Alexander Graf07b09072010-04-16 00:11:53 +0200413 } else
414#endif
415 {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000416 kvmppc_mmu_book3s_32_init(vcpu);
417 to_book3s(vcpu)->hior = 0;
418 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
419 }
420
421 /* If we are in hypervisor level on 970, we can tell the CPU to
422 * treat DCBZ as 32 bytes store */
423 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
424 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
425 !strcmp(cur_cpu_spec->platform, "ppc970"))
426 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
427
Alexander Graf05b0ab12010-03-24 21:48:37 +0100428 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
429 really needs them in a VM on Cell and force disable them. */
430 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
431 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
Alexander Graf07b09072010-04-16 00:11:53 +0200432
433#ifdef CONFIG_PPC_BOOK3S_32
434 /* 32 bit Book3S always has 32 byte dcbz */
435 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
436#endif
Alexander Grafb83d4a92010-04-20 02:49:54 +0200437
438 /* On some CPUs we can execute paired single operations natively */
439 asm ( "mfpvr %0" : "=r"(host_pvr));
440 switch (host_pvr) {
441 case 0x00080200: /* lonestar 2.0 */
442 case 0x00088202: /* lonestar 2.2 */
443 case 0x70000100: /* gekko 1.0 */
444 case 0x00080100: /* gekko 2.0 */
445 case 0x00083203: /* gekko 2.3a */
446 case 0x00083213: /* gekko 2.3b */
447 case 0x00083204: /* gekko 2.4 */
448 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
449 case 0x00087200: /* broadway */
450 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
451 /* Enable HID2.PSE - in case we need it later */
452 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
453 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000454}
455
Alexander Grafe8508942010-07-29 14:47:54 +0200456pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn)
457{
458 ulong mp_pa = vcpu->arch.magic_page_pa;
459
460 /* Magic page override */
461 if (unlikely(mp_pa) &&
462 unlikely(((gfn << PAGE_SHIFT) & KVM_PAM) ==
463 ((mp_pa & PAGE_MASK) & KVM_PAM))) {
464 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
465 pfn_t pfn;
466
467 pfn = (pfn_t)virt_to_phys((void*)shared_page) >> PAGE_SHIFT;
468 get_page(pfn_to_page(pfn));
469 return pfn;
470 }
471
472 return gfn_to_pfn(vcpu->kvm, gfn);
473}
474
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000475/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
476 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
477 * emulate 32 bytes dcbz length.
478 *
479 * The Book3s_64 inventors also realized this case and implemented a special bit
480 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
481 *
482 * My approach here is to patch the dcbz instruction on executing pages.
483 */
484static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
485{
Alexander Graf9fb244a2010-03-24 21:48:32 +0100486 struct page *hpage;
487 u64 hpage_offset;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000488 u32 *page;
489 int i;
490
Alexander Graf9fb244a2010-03-24 21:48:32 +0100491 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
Wei Yongjun646bab52010-08-17 10:08:52 +0800492 if (is_error_page(hpage)) {
493 kvm_release_page_clean(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000494 return;
Wei Yongjun646bab52010-08-17 10:08:52 +0800495 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000496
Alexander Graf9fb244a2010-03-24 21:48:32 +0100497 hpage_offset = pte->raddr & ~PAGE_MASK;
498 hpage_offset &= ~0xFFFULL;
499 hpage_offset /= 4;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000500
Alexander Graf9fb244a2010-03-24 21:48:32 +0100501 get_page(hpage);
502 page = kmap_atomic(hpage, KM_USER0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000503
Alexander Graf9fb244a2010-03-24 21:48:32 +0100504 /* patch dcbz into reserved instruction, so we trap */
505 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
506 if ((page[i] & 0xff0007ff) == INS_DCBZ)
507 page[i] &= 0xfffffff7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000508
Alexander Graf9fb244a2010-03-24 21:48:32 +0100509 kunmap_atomic(page, KM_USER0);
510 put_page(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000511}
512
513static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
514 struct kvmppc_pte *pte)
515{
Alexander Graf666e7252010-07-29 14:47:43 +0200516 int relocated = (vcpu->arch.shared->msr & (data ? MSR_DR : MSR_IR));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000517 int r;
518
519 if (relocated) {
520 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
521 } else {
522 pte->eaddr = eaddr;
Alexander Graf28e83b42010-07-29 14:47:52 +0200523 pte->raddr = eaddr & KVM_PAM;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100524 pte->vpage = VSID_REAL | eaddr >> 12;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000525 pte->may_read = true;
526 pte->may_write = true;
527 pte->may_execute = true;
528 r = 0;
529 }
530
531 return r;
532}
533
534static hva_t kvmppc_bad_hva(void)
535{
536 return PAGE_OFFSET;
537}
538
539static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
540 bool read)
541{
542 hva_t hpage;
543
544 if (read && !pte->may_read)
545 goto err;
546
547 if (!read && !pte->may_write)
548 goto err;
549
550 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
551 if (kvm_is_error_hva(hpage))
552 goto err;
553
554 return hpage | (pte->raddr & ~PAGE_MASK);
555err:
556 return kvmppc_bad_hva();
557}
558
Alexander Graf5467a972010-02-19 11:00:38 +0100559int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
560 bool data)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000561{
562 struct kvmppc_pte pte;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000563
564 vcpu->stat.st++;
565
Alexander Graf5467a972010-02-19 11:00:38 +0100566 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
Alexander Graf9fb244a2010-03-24 21:48:32 +0100567 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100568
569 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000570
Alexander Graf9fb244a2010-03-24 21:48:32 +0100571 if (!pte.may_write)
572 return -EPERM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000573
Alexander Graf9fb244a2010-03-24 21:48:32 +0100574 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
575 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000576
Alexander Graf5467a972010-02-19 11:00:38 +0100577 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000578}
579
Alexander Graf5467a972010-02-19 11:00:38 +0100580int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000581 bool data)
582{
583 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100584 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000585
586 vcpu->stat.ld++;
587
Alexander Graf5467a972010-02-19 11:00:38 +0100588 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
589 goto nopte;
590
591 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000592
593 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
594 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100595 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000596
597 if (copy_from_user(ptr, (void __user *)hva, size)) {
598 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100599 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000600 }
601
Alexander Graf5467a972010-02-19 11:00:38 +0100602 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000603
Alexander Graf5467a972010-02-19 11:00:38 +0100604nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000605 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100606mmio:
607 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000608}
609
610static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
611{
Alexander Grafe8508942010-07-29 14:47:54 +0200612 ulong mp_pa = vcpu->arch.magic_page_pa;
613
614 if (unlikely(mp_pa) &&
615 unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) {
616 return 1;
617 }
618
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000619 return kvm_is_visible_gfn(vcpu->kvm, gfn);
620}
621
622int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
623 ulong eaddr, int vec)
624{
625 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
626 int r = RESUME_GUEST;
627 int relocated;
628 int page_found = 0;
629 struct kvmppc_pte pte;
630 bool is_mmio = false;
Alexander Graf666e7252010-07-29 14:47:43 +0200631 bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
632 bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
Alexander Graff7bc74e2010-04-20 02:49:48 +0200633 u64 vsid;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000634
Alexander Graf3eeafd72010-03-24 21:48:17 +0100635 relocated = data ? dr : ir;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000636
637 /* Resolve real address if translation turned on */
638 if (relocated) {
639 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
640 } else {
641 pte.may_execute = true;
642 pte.may_read = true;
643 pte.may_write = true;
Alexander Graf28e83b42010-07-29 14:47:52 +0200644 pte.raddr = eaddr & KVM_PAM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000645 pte.eaddr = eaddr;
646 pte.vpage = eaddr >> 12;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100647 }
648
Alexander Graf666e7252010-07-29 14:47:43 +0200649 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
Alexander Graf3eeafd72010-03-24 21:48:17 +0100650 case 0:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200651 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
Alexander Graf3eeafd72010-03-24 21:48:17 +0100652 break;
653 case MSR_DR:
Alexander Graf3eeafd72010-03-24 21:48:17 +0100654 case MSR_IR:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200655 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
656
Alexander Graf666e7252010-07-29 14:47:43 +0200657 if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
Alexander Graff7bc74e2010-04-20 02:49:48 +0200658 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
659 else
660 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
661 pte.vpage |= vsid;
662
663 if (vsid == -1)
664 page_found = -EINVAL;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100665 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000666 }
667
668 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
669 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
670 /*
671 * If we do the dcbz hack, we have to NX on every execution,
672 * so we can patch the executing code. This renders our guest
673 * NX-less.
674 */
675 pte.may_execute = !data;
676 }
677
678 if (page_found == -ENOENT) {
679 /* Page not found in guest PTE entries */
Alexander Graf5e030182010-07-29 14:47:45 +0200680 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Grafd562de42010-07-29 14:47:44 +0200681 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf666e7252010-07-29 14:47:43 +0200682 vcpu->arch.shared->msr |=
683 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000684 kvmppc_book3s_queue_irqprio(vcpu, vec);
685 } else if (page_found == -EPERM) {
686 /* Storage protection */
Alexander Graf5e030182010-07-29 14:47:45 +0200687 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Grafd562de42010-07-29 14:47:44 +0200688 vcpu->arch.shared->dsisr =
689 to_svcpu(vcpu)->fault_dsisr & ~DSISR_NOHPTE;
690 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
Alexander Graf666e7252010-07-29 14:47:43 +0200691 vcpu->arch.shared->msr |=
692 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000693 kvmppc_book3s_queue_irqprio(vcpu, vec);
694 } else if (page_found == -EINVAL) {
695 /* Page not found in guest SLB */
Alexander Graf5e030182010-07-29 14:47:45 +0200696 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000697 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
698 } else if (!is_mmio &&
699 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
700 /* The guest's PTE is not mapped yet. Map on the host */
701 kvmppc_mmu_map_page(vcpu, &pte);
702 if (data)
703 vcpu->stat.sp_storage++;
704 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
705 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
706 kvmppc_patch_dcbz(vcpu, &pte);
707 } else {
708 /* MMIO */
709 vcpu->stat.mmio_exits++;
710 vcpu->arch.paddr_accessed = pte.raddr;
711 r = kvmppc_emulate_mmio(run, vcpu);
712 if ( r == RESUME_HOST_NV )
713 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000714 }
715
716 return r;
717}
718
Alexander Graf180a34d2010-01-15 14:49:11 +0100719static inline int get_fpr_index(int i)
720{
721#ifdef CONFIG_VSX
722 i *= 2;
723#endif
724 return i;
725}
726
727/* Give up external provider (FPU, Altivec, VSX) */
Alexander Grafaba3bd72010-02-19 11:00:39 +0100728void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
Alexander Graf180a34d2010-01-15 14:49:11 +0100729{
730 struct thread_struct *t = &current->thread;
731 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100732#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100733 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100734#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100735 u64 *thread_fpr = (u64*)t->fpr;
736 int i;
737
738 if (!(vcpu->arch.guest_owned_ext & msr))
739 return;
740
741#ifdef DEBUG_EXT
742 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
743#endif
744
745 switch (msr) {
746 case MSR_FP:
747 giveup_fpu(current);
748 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
749 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
750
751 vcpu->arch.fpscr = t->fpscr.val;
752 break;
753 case MSR_VEC:
754#ifdef CONFIG_ALTIVEC
755 giveup_altivec(current);
756 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
757 vcpu->arch.vscr = t->vscr;
758#endif
759 break;
760 case MSR_VSX:
761#ifdef CONFIG_VSX
762 __giveup_vsx(current);
763 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
764 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
765#endif
766 break;
767 default:
768 BUG();
769 }
770
771 vcpu->arch.guest_owned_ext &= ~msr;
772 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100773 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100774}
775
Alexander Graf89632212010-03-24 21:48:21 +0100776static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100777{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200778 ulong srr0 = kvmppc_get_pc(vcpu);
779 u32 last_inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100780 int ret;
781
Alexander Grafc7f38f42010-04-16 00:11:40 +0200782 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100783 if (ret == -ENOENT) {
Alexander Graf666e7252010-07-29 14:47:43 +0200784 ulong msr = vcpu->arch.shared->msr;
785
786 msr = kvmppc_set_field(msr, 33, 33, 1);
787 msr = kvmppc_set_field(msr, 34, 36, 0);
788 vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100789 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
Alexander Graf89632212010-03-24 21:48:21 +0100790 return EMULATE_AGAIN;
791 }
792
793 return EMULATE_DONE;
794}
795
796static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
797{
798
799 /* Need to do paired single emulation? */
800 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
801 return EMULATE_DONE;
802
803 /* Read out the instruction */
804 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100805 /* Need to emulate */
806 return EMULATE_FAIL;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100807
808 return EMULATE_AGAIN;
809}
810
Alexander Graf180a34d2010-01-15 14:49:11 +0100811/* Handle external providers (FPU, Altivec, VSX) */
812static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
813 ulong msr)
814{
815 struct thread_struct *t = &current->thread;
816 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100817#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100818 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100819#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100820 u64 *thread_fpr = (u64*)t->fpr;
821 int i;
822
Alexander Graf3c402a72010-02-19 11:00:32 +0100823 /* When we have paired singles, we emulate in software */
824 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
825 return RESUME_GUEST;
826
Alexander Graf666e7252010-07-29 14:47:43 +0200827 if (!(vcpu->arch.shared->msr & msr)) {
Alexander Graf180a34d2010-01-15 14:49:11 +0100828 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
829 return RESUME_GUEST;
830 }
831
Alexander Grafc2453692010-03-24 21:48:22 +0100832 /* We already own the ext */
833 if (vcpu->arch.guest_owned_ext & msr) {
834 return RESUME_GUEST;
835 }
836
Alexander Graf180a34d2010-01-15 14:49:11 +0100837#ifdef DEBUG_EXT
838 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
839#endif
840
841 current->thread.regs->msr |= msr;
842
843 switch (msr) {
844 case MSR_FP:
845 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
846 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
847
848 t->fpscr.val = vcpu->arch.fpscr;
849 t->fpexc_mode = 0;
850 kvmppc_load_up_fpu();
851 break;
852 case MSR_VEC:
853#ifdef CONFIG_ALTIVEC
854 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
855 t->vscr = vcpu->arch.vscr;
856 t->vrsave = -1;
857 kvmppc_load_up_altivec();
858#endif
859 break;
860 case MSR_VSX:
861#ifdef CONFIG_VSX
862 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
863 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
864 kvmppc_load_up_vsx();
865#endif
866 break;
867 default:
868 BUG();
869 }
870
871 vcpu->arch.guest_owned_ext |= msr;
872
Alexander Grafa76f8492010-01-15 14:49:14 +0100873 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100874
875 return RESUME_GUEST;
876}
877
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000878int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
879 unsigned int exit_nr)
880{
881 int r = RESUME_HOST;
882
883 vcpu->stat.sum_exits++;
884
885 run->exit_reason = KVM_EXIT_UNKNOWN;
886 run->ready_for_interrupt_injection = 1;
Alexander Grafbed1ed92010-08-02 11:06:26 +0200887
888 trace_kvm_book3s_exit(exit_nr, vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000889 kvm_resched(vcpu);
890 switch (exit_nr) {
891 case BOOK3S_INTERRUPT_INST_STORAGE:
892 vcpu->stat.pf_instruc++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200893
894#ifdef CONFIG_PPC_BOOK3S_32
895 /* We set segments as unused segments when invalidating them. So
896 * treat the respective fault as segment fault. */
897 if (to_svcpu(vcpu)->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT]
898 == SR_INVALID) {
899 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
900 r = RESUME_GUEST;
901 break;
902 }
903#endif
904
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000905 /* only care about PTEG not found errors, but leave NX alone */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200906 if (to_svcpu(vcpu)->shadow_srr1 & 0x40000000) {
907 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000908 vcpu->stat.sp_instruc++;
909 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
910 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
911 /*
912 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
913 * so we can't use the NX bit inside the guest. Let's cross our fingers,
914 * that no guest that needs the dcbz hack does NX.
915 */
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200916 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100917 r = RESUME_GUEST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000918 } else {
Alexander Graf666e7252010-07-29 14:47:43 +0200919 vcpu->arch.shared->msr |=
920 to_svcpu(vcpu)->shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000921 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000922 r = RESUME_GUEST;
923 }
924 break;
925 case BOOK3S_INTERRUPT_DATA_STORAGE:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200926 {
927 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000928 vcpu->stat.pf_storage++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200929
930#ifdef CONFIG_PPC_BOOK3S_32
931 /* We set segments as unused segments when invalidating them. So
932 * treat the respective fault as segment fault. */
933 if ((to_svcpu(vcpu)->sr[dar >> SID_SHIFT]) == SR_INVALID) {
934 kvmppc_mmu_map_segment(vcpu, dar);
935 r = RESUME_GUEST;
936 break;
937 }
938#endif
939
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000940 /* The only case we need to handle is missing shadow PTEs */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200941 if (to_svcpu(vcpu)->fault_dsisr & DSISR_NOHPTE) {
942 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000943 } else {
Alexander Graf5e030182010-07-29 14:47:45 +0200944 vcpu->arch.shared->dar = dar;
Alexander Grafd562de42010-07-29 14:47:44 +0200945 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000946 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000947 r = RESUME_GUEST;
948 }
949 break;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200950 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000951 case BOOK3S_INTERRUPT_DATA_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200952 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
Alexander Graf5e030182010-07-29 14:47:45 +0200953 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000954 kvmppc_book3s_queue_irqprio(vcpu,
955 BOOK3S_INTERRUPT_DATA_SEGMENT);
956 }
957 r = RESUME_GUEST;
958 break;
959 case BOOK3S_INTERRUPT_INST_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200960 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000961 kvmppc_book3s_queue_irqprio(vcpu,
962 BOOK3S_INTERRUPT_INST_SEGMENT);
963 }
964 r = RESUME_GUEST;
965 break;
966 /* We're good on these - the host merely wanted to get our attention */
967 case BOOK3S_INTERRUPT_DECREMENTER:
968 vcpu->stat.dec_exits++;
969 r = RESUME_GUEST;
970 break;
971 case BOOK3S_INTERRUPT_EXTERNAL:
972 vcpu->stat.ext_intr_exits++;
973 r = RESUME_GUEST;
974 break;
Alexander Graf7fdaec92010-04-20 02:49:47 +0200975 case BOOK3S_INTERRUPT_PERFMON:
976 r = RESUME_GUEST;
977 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000978 case BOOK3S_INTERRUPT_PROGRAM:
979 {
980 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100981 ulong flags;
982
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100983program_interrupt:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200984 flags = to_svcpu(vcpu)->shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000985
Alexander Graf666e7252010-07-29 14:47:43 +0200986 if (vcpu->arch.shared->msr & MSR_PR) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000987#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +0200988 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 +0000989#endif
Alexander Grafc7f38f42010-04-16 00:11:40 +0200990 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000991 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100992 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000993 r = RESUME_GUEST;
994 break;
995 }
996 }
997
998 vcpu->stat.emulated_inst_exits++;
999 er = kvmppc_emulate_instruction(run, vcpu);
1000 switch (er) {
1001 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +01001002 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001003 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +01001004 case EMULATE_AGAIN:
1005 r = RESUME_GUEST;
1006 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001007 case EMULATE_FAIL:
1008 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +02001009 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
Alexander Grafff1ca3f2010-01-08 02:58:09 +01001010 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001011 r = RESUME_GUEST;
1012 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +01001013 case EMULATE_DO_MMIO:
1014 run->exit_reason = KVM_EXIT_MMIO;
1015 r = RESUME_HOST_NV;
1016 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001017 default:
1018 BUG();
1019 }
1020 break;
1021 }
1022 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafad0a0482010-03-24 21:48:30 +01001023 if (vcpu->arch.osi_enabled &&
1024 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
1025 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
Alexander Graf2a342ed2010-07-29 14:47:48 +02001026 /* MOL hypercalls */
Alexander Grafad0a0482010-03-24 21:48:30 +01001027 u64 *gprs = run->osi.gprs;
1028 int i;
1029
1030 run->exit_reason = KVM_EXIT_OSI;
1031 for (i = 0; i < 32; i++)
1032 gprs[i] = kvmppc_get_gpr(vcpu, i);
1033 vcpu->arch.osi_needed = 1;
1034 r = RESUME_HOST_NV;
Alexander Graf2a342ed2010-07-29 14:47:48 +02001035 } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
1036 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
1037 /* KVM PV hypercalls */
1038 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1039 r = RESUME_GUEST;
Alexander Grafad0a0482010-03-24 21:48:30 +01001040 } else {
Alexander Graf2a342ed2010-07-29 14:47:48 +02001041 /* Guest syscalls */
Alexander Grafad0a0482010-03-24 21:48:30 +01001042 vcpu->stat.syscall_exits++;
1043 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1044 r = RESUME_GUEST;
1045 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001046 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001047 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001048 case BOOK3S_INTERRUPT_ALTIVEC:
1049 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +01001050 {
1051 int ext_msr = 0;
1052
1053 switch (exit_nr) {
1054 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
1055 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
1056 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
1057 }
1058
1059 switch (kvmppc_check_ext(vcpu, exit_nr)) {
1060 case EMULATE_DONE:
1061 /* everything ok - let's enable the ext */
1062 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
1063 break;
1064 case EMULATE_FAIL:
1065 /* we need to emulate this instruction */
1066 goto program_interrupt;
1067 break;
1068 default:
1069 /* nothing to worry about - go again */
1070 break;
1071 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001072 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +01001073 }
Alexander Grafca7f4202010-03-24 21:48:28 +01001074 case BOOK3S_INTERRUPT_ALIGNMENT:
1075 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
Alexander Grafd562de42010-07-29 14:47:44 +02001076 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001077 kvmppc_get_last_inst(vcpu));
Alexander Graf5e030182010-07-29 14:47:45 +02001078 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001079 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +01001080 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1081 }
1082 r = RESUME_GUEST;
1083 break;
Alexander Graf180a34d2010-01-15 14:49:11 +01001084 case BOOK3S_INTERRUPT_MACHINE_CHECK:
1085 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001086 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1087 r = RESUME_GUEST;
1088 break;
1089 default:
1090 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +01001091 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +02001092 exit_nr, kvmppc_get_pc(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001093 r = RESUME_HOST;
1094 BUG();
1095 break;
1096 }
1097
1098
1099 if (!(r & RESUME_HOST)) {
1100 /* To avoid clobbering exit_reason, only check for signals if
1101 * we aren't already exiting to userspace for some other
1102 * reason. */
1103 if (signal_pending(current)) {
1104#ifdef EXIT_DEBUG
1105 printk(KERN_EMERG "KVM: Going back to host\n");
1106#endif
1107 vcpu->stat.signal_exits++;
1108 run->exit_reason = KVM_EXIT_INTR;
1109 r = -EINTR;
1110 } else {
1111 /* In case an interrupt came in that was triggered
1112 * from userspace (like DEC), we need to check what
1113 * to inject now! */
1114 kvmppc_core_deliver_interrupts(vcpu);
1115 }
1116 }
1117
Alexander Grafbed1ed92010-08-02 11:06:26 +02001118 trace_kvm_book3s_reenter(r, vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001119
1120 return r;
1121}
1122
1123int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1124{
1125 return 0;
1126}
1127
1128int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1129{
1130 int i;
1131
Alexander Grafc7f38f42010-04-16 00:11:40 +02001132 regs->pc = kvmppc_get_pc(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001133 regs->cr = kvmppc_get_cr(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001134 regs->ctr = kvmppc_get_ctr(vcpu);
1135 regs->lr = kvmppc_get_lr(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001136 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001137 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +02001138 regs->srr0 = vcpu->arch.shared->srr0;
1139 regs->srr1 = vcpu->arch.shared->srr1;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001140 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001141 regs->sprg0 = vcpu->arch.shared->sprg0;
1142 regs->sprg1 = vcpu->arch.shared->sprg1;
1143 regs->sprg2 = vcpu->arch.shared->sprg2;
1144 regs->sprg3 = vcpu->arch.shared->sprg3;
Peter Tyserbc9c1932010-12-29 13:51:25 -06001145 regs->sprg4 = vcpu->arch.sprg4;
1146 regs->sprg5 = vcpu->arch.sprg5;
1147 regs->sprg6 = vcpu->arch.sprg6;
1148 regs->sprg7 = vcpu->arch.sprg7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001149
1150 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001151 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001152
1153 return 0;
1154}
1155
1156int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1157{
1158 int i;
1159
Alexander Grafc7f38f42010-04-16 00:11:40 +02001160 kvmppc_set_pc(vcpu, regs->pc);
Alexander Graf992b5b22010-01-08 02:58:02 +01001161 kvmppc_set_cr(vcpu, regs->cr);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001162 kvmppc_set_ctr(vcpu, regs->ctr);
1163 kvmppc_set_lr(vcpu, regs->lr);
Alexander Graf992b5b22010-01-08 02:58:02 +01001164 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001165 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +02001166 vcpu->arch.shared->srr0 = regs->srr0;
1167 vcpu->arch.shared->srr1 = regs->srr1;
Alexander Grafa73a9592010-07-29 14:47:47 +02001168 vcpu->arch.shared->sprg0 = regs->sprg0;
1169 vcpu->arch.shared->sprg1 = regs->sprg1;
1170 vcpu->arch.shared->sprg2 = regs->sprg2;
1171 vcpu->arch.shared->sprg3 = regs->sprg3;
Peter Tyserbc9c1932010-12-29 13:51:25 -06001172 vcpu->arch.sprg4 = regs->sprg4;
1173 vcpu->arch.sprg5 = regs->sprg5;
1174 vcpu->arch.sprg6 = regs->sprg6;
1175 vcpu->arch.sprg7 = regs->sprg7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001176
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001177 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1178 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001179
1180 return 0;
1181}
1182
1183int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1184 struct kvm_sregs *sregs)
1185{
Alexander Grafe15a1132009-11-30 03:02:02 +00001186 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1187 int i;
1188
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001189 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +00001190
1191 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1192 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1193 for (i = 0; i < 64; i++) {
1194 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
1195 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1196 }
1197 } else {
Alexander Grafdf1bfa22010-08-03 02:29:27 +02001198 for (i = 0; i < 16; i++)
1199 sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i];
1200
Alexander Grafe15a1132009-11-30 03:02:02 +00001201 for (i = 0; i < 8; i++) {
1202 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1203 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1204 }
1205 }
Avi Kivity98001d82010-05-13 11:05:49 +03001206
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001207 return 0;
1208}
1209
1210int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1211 struct kvm_sregs *sregs)
1212{
Alexander Grafe15a1132009-11-30 03:02:02 +00001213 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1214 int i;
1215
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001216 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001217
1218 vcpu3s->sdr1 = sregs->u.s.sdr1;
1219 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1220 for (i = 0; i < 64; i++) {
1221 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1222 sregs->u.s.ppc64.slb[i].slbe);
1223 }
1224 } else {
1225 for (i = 0; i < 16; i++) {
1226 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1227 }
1228 for (i = 0; i < 8; i++) {
1229 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1230 (u32)sregs->u.s.ppc32.ibat[i]);
1231 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1232 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1233 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1234 (u32)sregs->u.s.ppc32.dbat[i]);
1235 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1236 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1237 }
1238 }
1239
1240 /* Flush the MMU after messing with the segments */
1241 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Avi Kivity98001d82010-05-13 11:05:49 +03001242
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001243 return 0;
1244}
1245
1246int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1247{
1248 return -ENOTSUPP;
1249}
1250
1251int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1252{
1253 return -ENOTSUPP;
1254}
1255
1256int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1257 struct kvm_translation *tr)
1258{
1259 return 0;
1260}
1261
1262/*
1263 * Get (and clear) the dirty memory log for a memory slot.
1264 */
1265int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1266 struct kvm_dirty_log *log)
1267{
1268 struct kvm_memory_slot *memslot;
1269 struct kvm_vcpu *vcpu;
1270 ulong ga, ga_end;
1271 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001272 int r;
1273 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001274
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001275 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001276
1277 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1278 if (r)
1279 goto out;
1280
1281 /* If nothing is dirty, don't bother messing with page tables. */
1282 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001283 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001284
1285 ga = memslot->base_gfn << PAGE_SHIFT;
1286 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1287
1288 kvm_for_each_vcpu(n, vcpu, kvm)
1289 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1290
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001291 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001292 memset(memslot->dirty_bitmap, 0, n);
1293 }
1294
1295 r = 0;
1296out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001297 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001298 return r;
1299}
1300
1301int kvmppc_core_check_processor_compat(void)
1302{
1303 return 0;
1304}
1305
1306struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1307{
1308 struct kvmppc_vcpu_book3s *vcpu_book3s;
1309 struct kvm_vcpu *vcpu;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001310 int err = -ENOMEM;
Alexander Grafe8508942010-07-29 14:47:54 +02001311 unsigned long p;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001312
Takuya Yoshikawa26535032010-11-02 10:49:34 +09001313 vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Grafc7f38f42010-04-16 00:11:40 +02001314 if (!vcpu_book3s)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001315 goto out;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001316
Alexander Grafc7f38f42010-04-16 00:11:40 +02001317 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
1318 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
1319 if (!vcpu_book3s->shadow_vcpu)
1320 goto free_vcpu;
1321
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001322 vcpu = &vcpu_book3s->vcpu;
1323 err = kvm_vcpu_init(vcpu, kvm, id);
1324 if (err)
Alexander Grafc7f38f42010-04-16 00:11:40 +02001325 goto free_shadow_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001326
Alexander Grafe8508942010-07-29 14:47:54 +02001327 p = __get_free_page(GFP_KERNEL|__GFP_ZERO);
1328 /* the real shared page fills the last 4k of our page */
1329 vcpu->arch.shared = (void*)(p + PAGE_SIZE - 4096);
1330 if (!p)
Alexander Graf96bc4512010-07-29 14:47:42 +02001331 goto uninit_vcpu;
1332
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001333 vcpu->arch.host_retip = kvm_return_point;
1334 vcpu->arch.host_msr = mfmsr();
Alexander Graf07b09072010-04-16 00:11:53 +02001335#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001336 /* default to book3s_64 (970fx) */
1337 vcpu->arch.pvr = 0x3C0301;
Alexander Graf07b09072010-04-16 00:11:53 +02001338#else
1339 /* default to book3s_32 (750) */
1340 vcpu->arch.pvr = 0x84202;
1341#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001342 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1343 vcpu_book3s->slb_nr = 64;
1344
1345 /* remember where some real-mode handlers are */
Paul Mackerras149dbdb2011-06-29 00:16:42 +00001346 vcpu->arch.trampoline_lowmem = __pa(kvmppc_handler_lowmem_trampoline);
1347 vcpu->arch.trampoline_enter = __pa(kvmppc_handler_trampoline_enter);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001348 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf07b09072010-04-16 00:11:53 +02001349#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf021ec9c2010-01-08 02:58:06 +01001350 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf07b09072010-04-16 00:11:53 +02001351#else
1352 vcpu->arch.rmcall = (ulong)kvmppc_rmcall;
1353#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001354
1355 vcpu->arch.shadow_msr = MSR_USER64;
1356
Alexander Graf9cc5e952010-04-16 00:11:45 +02001357 err = kvmppc_mmu_init(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001358 if (err < 0)
Alexander Graf96bc4512010-07-29 14:47:42 +02001359 goto uninit_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001360
1361 return vcpu;
1362
Alexander Graf96bc4512010-07-29 14:47:42 +02001363uninit_vcpu:
1364 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001365free_shadow_vcpu:
1366 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001367free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001368 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001369out:
1370 return ERR_PTR(err);
1371}
1372
1373void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1374{
1375 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1376
Alexander Grafe8508942010-07-29 14:47:54 +02001377 free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001378 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001379 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001380 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001381}
1382
1383extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1384int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1385{
1386 int ret;
Andreas Schwab49f6be82010-05-31 21:59:13 +02001387 double fpr[32][TS_FPRWIDTH];
1388 unsigned int fpscr;
1389 int fpexc_mode;
Alexander Grafa2b07662010-03-24 21:48:31 +01001390#ifdef CONFIG_ALTIVEC
Andreas Schwab49f6be82010-05-31 21:59:13 +02001391 vector128 vr[32];
1392 vector128 vscr;
1393 unsigned long uninitialized_var(vrsave);
1394 int used_vr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001395#endif
1396#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001397 int used_vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001398#endif
Alexander Graf180a34d2010-01-15 14:49:11 +01001399 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001400
1401 /* No need to go into the guest when all we do is going out */
1402 if (signal_pending(current)) {
1403 kvm_run->exit_reason = KVM_EXIT_INTR;
1404 return -EINTR;
1405 }
1406
Alexander Graf180a34d2010-01-15 14:49:11 +01001407 /* Save FPU state in stack */
1408 if (current->thread.regs->msr & MSR_FP)
1409 giveup_fpu(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001410 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
1411 fpscr = current->thread.fpscr.val;
1412 fpexc_mode = current->thread.fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001413
1414#ifdef CONFIG_ALTIVEC
1415 /* Save Altivec state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001416 used_vr = current->thread.used_vr;
1417 if (used_vr) {
Alexander Graf180a34d2010-01-15 14:49:11 +01001418 if (current->thread.regs->msr & MSR_VEC)
1419 giveup_altivec(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001420 memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
1421 vscr = current->thread.vscr;
1422 vrsave = current->thread.vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001423 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001424#endif
1425
1426#ifdef CONFIG_VSX
1427 /* Save VSX state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001428 used_vsr = current->thread.used_vsr;
1429 if (used_vsr && (current->thread.regs->msr & MSR_VSX))
Alexander Graf180a34d2010-01-15 14:49:11 +01001430 __giveup_vsx(current);
Alexander Graf180a34d2010-01-15 14:49:11 +01001431#endif
1432
1433 /* Remember the MSR with disabled extensions */
1434 ext_msr = current->thread.regs->msr;
1435
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001436 /* XXX we get called with irq disabled - change that! */
1437 local_irq_enable();
1438
Alexander Grafd1bab742010-02-19 11:00:35 +01001439 /* Preload FPU if it's enabled */
Alexander Graf666e7252010-07-29 14:47:43 +02001440 if (vcpu->arch.shared->msr & MSR_FP)
Alexander Grafd1bab742010-02-19 11:00:35 +01001441 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1442
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001443 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1444
1445 local_irq_disable();
1446
Alexander Graf180a34d2010-01-15 14:49:11 +01001447 current->thread.regs->msr = ext_msr;
1448
1449 /* Make sure we save the guest FPU/Altivec/VSX state */
1450 kvmppc_giveup_ext(vcpu, MSR_FP);
1451 kvmppc_giveup_ext(vcpu, MSR_VEC);
1452 kvmppc_giveup_ext(vcpu, MSR_VSX);
1453
1454 /* Restore FPU state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001455 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
1456 current->thread.fpscr.val = fpscr;
1457 current->thread.fpexc_mode = fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001458
1459#ifdef CONFIG_ALTIVEC
1460 /* Restore Altivec state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001461 if (used_vr && current->thread.used_vr) {
1462 memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
1463 current->thread.vscr = vscr;
1464 current->thread.vrsave = vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001465 }
Andreas Schwab49f6be82010-05-31 21:59:13 +02001466 current->thread.used_vr = used_vr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001467#endif
1468
1469#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001470 current->thread.used_vsr = used_vsr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001471#endif
1472
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001473 return ret;
1474}
1475
1476static int kvmppc_book3s_init(void)
1477{
Alexander Graffef093b2010-06-30 15:18:46 +02001478 int r;
1479
1480 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
1481 THIS_MODULE);
1482
1483 if (r)
1484 return r;
1485
1486 r = kvmppc_mmu_hpte_sysinit();
1487
1488 return r;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001489}
1490
1491static void kvmppc_book3s_exit(void)
1492{
Alexander Graffef093b2010-06-30 15:18:46 +02001493 kvmppc_mmu_hpte_sysexit();
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001494 kvm_exit();
1495}
1496
1497module_init(kvmppc_book3s_init);
1498module_exit(kvmppc_book3s_exit);