blob: cfd7fe5c3a6282f694f966f56a29e8dd70f26727 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/gfp.h>
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000031#include <linux/sched.h>
32#include <linux/vmalloc.h>
Alexander Graf9fb244a2010-03-24 21:48:32 +010033#include <linux/highmem.h>
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000034
35#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
36
37/* #define EXIT_DEBUG */
38/* #define EXIT_DEBUG_SIMPLE */
Alexander Graf180a34d2010-01-15 14:49:11 +010039/* #define DEBUG_EXT */
40
Alexander Grafc8c0b6f2010-02-19 11:00:34 +010041static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
42 ulong msr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000043
Alexander Graf07b09072010-04-16 00:11:53 +020044/* Some compatibility defines */
45#ifdef CONFIG_PPC_BOOK3S_32
46#define MSR_USER32 MSR_USER
47#define MSR_USER64 MSR_USER
48#define HW_PAGE_SIZE PAGE_SIZE
49#endif
50
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000051struct kvm_stats_debugfs_item debugfs_entries[] = {
52 { "exits", VCPU_STAT(sum_exits) },
53 { "mmio", VCPU_STAT(mmio_exits) },
54 { "sig", VCPU_STAT(signal_exits) },
55 { "sysc", VCPU_STAT(syscall_exits) },
56 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
57 { "dec", VCPU_STAT(dec_exits) },
58 { "ext_intr", VCPU_STAT(ext_intr_exits) },
59 { "queue_intr", VCPU_STAT(queue_intr) },
60 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
61 { "pf_storage", VCPU_STAT(pf_storage) },
62 { "sp_storage", VCPU_STAT(sp_storage) },
63 { "pf_instruc", VCPU_STAT(pf_instruc) },
64 { "sp_instruc", VCPU_STAT(sp_instruc) },
65 { "ld", VCPU_STAT(ld) },
66 { "ld_slow", VCPU_STAT(ld_slow) },
67 { "st", VCPU_STAT(st) },
68 { "st_slow", VCPU_STAT(st_slow) },
69 { NULL }
70};
71
72void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
73{
74}
75
76void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
77{
78}
79
80void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
81{
Alexander Grafc7f38f42010-04-16 00:11:40 +020082#ifdef CONFIG_PPC_BOOK3S_64
83 memcpy(to_svcpu(vcpu)->slb, to_book3s(vcpu)->slb_shadow, sizeof(to_svcpu(vcpu)->slb));
84 memcpy(&get_paca()->shadow_vcpu, to_book3s(vcpu)->shadow_vcpu,
Alexander Graf7e57cba2010-01-08 02:58:03 +010085 sizeof(get_paca()->shadow_vcpu));
Alexander Grafc7f38f42010-04-16 00:11:40 +020086 to_svcpu(vcpu)->slb_max = to_book3s(vcpu)->slb_shadow_max;
87#endif
88
89#ifdef CONFIG_PPC_BOOK3S_32
90 current->thread.kvm_shadow_vcpu = to_book3s(vcpu)->shadow_vcpu;
91#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000092}
93
94void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
95{
Alexander Grafc7f38f42010-04-16 00:11:40 +020096#ifdef CONFIG_PPC_BOOK3S_64
97 memcpy(to_book3s(vcpu)->slb_shadow, to_svcpu(vcpu)->slb, sizeof(to_svcpu(vcpu)->slb));
98 memcpy(to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
Alexander Graf7e57cba2010-01-08 02:58:03 +010099 sizeof(get_paca()->shadow_vcpu));
Alexander Grafc7f38f42010-04-16 00:11:40 +0200100 to_book3s(vcpu)->slb_shadow_max = to_svcpu(vcpu)->slb_max;
101#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100102
103 kvmppc_giveup_ext(vcpu, MSR_FP);
104 kvmppc_giveup_ext(vcpu, MSR_VEC);
105 kvmppc_giveup_ext(vcpu, MSR_VSX);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000106}
107
Alexander Graf0bb1fb72009-12-21 20:21:25 +0100108#if defined(EXIT_DEBUG)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000109static u32 kvmppc_get_dec(struct kvm_vcpu *vcpu)
110{
111 u64 jd = mftb() - vcpu->arch.dec_jiffies;
112 return vcpu->arch.dec - jd;
113}
114#endif
115
Alexander Grafa76f8492010-01-15 14:49:14 +0100116static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
117{
Alexander Graf666e7252010-07-29 14:47:43 +0200118 ulong smsr = vcpu->arch.shared->msr;
119
Alexander Grafa76f8492010-01-15 14:49:14 +0100120 /* Guest MSR values */
Alexander Graf666e7252010-07-29 14:47:43 +0200121 smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE | MSR_DE;
Alexander Grafa76f8492010-01-15 14:49:14 +0100122 /* Process MSR values */
Alexander Graf666e7252010-07-29 14:47:43 +0200123 smsr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | MSR_EE;
Alexander Grafa76f8492010-01-15 14:49:14 +0100124 /* External providers the guest reserved */
Alexander Graf666e7252010-07-29 14:47:43 +0200125 smsr |= (vcpu->arch.shared->msr & vcpu->arch.guest_owned_ext);
Alexander Grafa76f8492010-01-15 14:49:14 +0100126 /* 64-bit Process MSR values */
127#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf666e7252010-07-29 14:47:43 +0200128 smsr |= MSR_ISF | MSR_HV;
Alexander Grafa76f8492010-01-15 14:49:14 +0100129#endif
Alexander Graf666e7252010-07-29 14:47:43 +0200130 vcpu->arch.shadow_msr = smsr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100131}
132
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000133void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
134{
Alexander Graf666e7252010-07-29 14:47:43 +0200135 ulong old_msr = vcpu->arch.shared->msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000136
137#ifdef EXIT_DEBUG
138 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
139#endif
Alexander Grafa76f8492010-01-15 14:49:14 +0100140
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000141 msr &= to_book3s(vcpu)->msr_mask;
Alexander Graf666e7252010-07-29 14:47:43 +0200142 vcpu->arch.shared->msr = msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100143 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000144
145 if (msr & (MSR_WE|MSR_POW)) {
146 if (!vcpu->arch.pending_exceptions) {
147 kvm_vcpu_block(vcpu);
148 vcpu->stat.halt_wakeup++;
149 }
150 }
151
Alexander Graf666e7252010-07-29 14:47:43 +0200152 if ((vcpu->arch.shared->msr & (MSR_PR|MSR_IR|MSR_DR)) !=
Alexander Graff7bc74e2010-04-20 02:49:48 +0200153 (old_msr & (MSR_PR|MSR_IR|MSR_DR))) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000154 kvmppc_mmu_flush_segments(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +0200155 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000156 }
Alexander Grafd1bab742010-02-19 11:00:35 +0100157
158 /* Preload FPU if it's enabled */
Alexander Graf666e7252010-07-29 14:47:43 +0200159 if (vcpu->arch.shared->msr & MSR_FP)
Alexander Grafd1bab742010-02-19 11:00:35 +0100160 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000161}
162
163void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
164{
Alexander Grafde7906c2010-07-29 14:47:46 +0200165 vcpu->arch.shared->srr0 = kvmppc_get_pc(vcpu);
166 vcpu->arch.shared->srr1 = vcpu->arch.shared->msr | flags;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200167 kvmppc_set_pc(vcpu, to_book3s(vcpu)->hior + vec);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000168 vcpu->arch.mmu.reset_msr(vcpu);
169}
170
Alexander Graf583617b2009-12-21 20:21:23 +0100171static int kvmppc_book3s_vec2irqprio(unsigned int vec)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000172{
173 unsigned int prio;
174
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000175 switch (vec) {
176 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
177 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
178 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
179 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
180 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
181 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
182 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
183 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
184 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
185 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
186 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
187 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
188 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
189 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
190 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
191 default: prio = BOOK3S_IRQPRIO_MAX; break;
192 }
193
Alexander Graf583617b2009-12-21 20:21:23 +0100194 return prio;
195}
196
Alexander Graf7706664d2009-12-21 20:21:24 +0100197static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
198 unsigned int vec)
199{
200 clear_bit(kvmppc_book3s_vec2irqprio(vec),
201 &vcpu->arch.pending_exceptions);
202}
203
Alexander Graf583617b2009-12-21 20:21:23 +0100204void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
205{
206 vcpu->stat.queue_intr++;
207
208 set_bit(kvmppc_book3s_vec2irqprio(vec),
209 &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000210#ifdef EXIT_DEBUG
211 printk(KERN_INFO "Queueing interrupt %x\n", vec);
212#endif
213}
214
215
Alexander Graf25a8a022010-01-08 02:58:07 +0100216void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000217{
Alexander Graf25a8a022010-01-08 02:58:07 +0100218 to_book3s(vcpu)->prog_flags = flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000219 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM);
220}
221
222void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
223{
224 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
225}
226
227int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
228{
229 return test_bit(BOOK3S_INTERRUPT_DECREMENTER >> 7, &vcpu->arch.pending_exceptions);
230}
231
Alexander Graf7706664d2009-12-21 20:21:24 +0100232void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
233{
234 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
235}
236
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000237void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
238 struct kvm_interrupt *irq)
239{
240 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
241}
242
Alexander Graf18978762010-03-24 21:48:18 +0100243void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
244 struct kvm_interrupt *irq)
245{
246 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
247}
248
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000249int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
250{
251 int deliver = 1;
252 int vec = 0;
Alexander Graf25a8a022010-01-08 02:58:07 +0100253 ulong flags = 0ULL;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000254
255 switch (priority) {
256 case BOOK3S_IRQPRIO_DECREMENTER:
Alexander Graf666e7252010-07-29 14:47:43 +0200257 deliver = vcpu->arch.shared->msr & MSR_EE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000258 vec = BOOK3S_INTERRUPT_DECREMENTER;
259 break;
260 case BOOK3S_IRQPRIO_EXTERNAL:
Alexander Graf666e7252010-07-29 14:47:43 +0200261 deliver = vcpu->arch.shared->msr & MSR_EE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000262 vec = BOOK3S_INTERRUPT_EXTERNAL;
263 break;
264 case BOOK3S_IRQPRIO_SYSTEM_RESET:
265 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
266 break;
267 case BOOK3S_IRQPRIO_MACHINE_CHECK:
268 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
269 break;
270 case BOOK3S_IRQPRIO_DATA_STORAGE:
271 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
272 break;
273 case BOOK3S_IRQPRIO_INST_STORAGE:
274 vec = BOOK3S_INTERRUPT_INST_STORAGE;
275 break;
276 case BOOK3S_IRQPRIO_DATA_SEGMENT:
277 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
278 break;
279 case BOOK3S_IRQPRIO_INST_SEGMENT:
280 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
281 break;
282 case BOOK3S_IRQPRIO_ALIGNMENT:
283 vec = BOOK3S_INTERRUPT_ALIGNMENT;
284 break;
285 case BOOK3S_IRQPRIO_PROGRAM:
286 vec = BOOK3S_INTERRUPT_PROGRAM;
Alexander Graf25a8a022010-01-08 02:58:07 +0100287 flags = to_book3s(vcpu)->prog_flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000288 break;
289 case BOOK3S_IRQPRIO_VSX:
290 vec = BOOK3S_INTERRUPT_VSX;
291 break;
292 case BOOK3S_IRQPRIO_ALTIVEC:
293 vec = BOOK3S_INTERRUPT_ALTIVEC;
294 break;
295 case BOOK3S_IRQPRIO_FP_UNAVAIL:
296 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
297 break;
298 case BOOK3S_IRQPRIO_SYSCALL:
299 vec = BOOK3S_INTERRUPT_SYSCALL;
300 break;
301 case BOOK3S_IRQPRIO_DEBUG:
302 vec = BOOK3S_INTERRUPT_TRACE;
303 break;
304 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
305 vec = BOOK3S_INTERRUPT_PERFMON;
306 break;
307 default:
308 deliver = 0;
309 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
310 break;
311 }
312
313#if 0
314 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
315#endif
316
317 if (deliver)
Alexander Graf25a8a022010-01-08 02:58:07 +0100318 kvmppc_inject_interrupt(vcpu, vec, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000319
320 return deliver;
321}
322
323void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
324{
325 unsigned long *pending = &vcpu->arch.pending_exceptions;
326 unsigned int priority;
327
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000328#ifdef EXIT_DEBUG
329 if (vcpu->arch.pending_exceptions)
330 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
331#endif
332 priority = __ffs(*pending);
Alexander Grafada7ba12010-04-16 00:11:56 +0200333 while (priority < BOOK3S_IRQPRIO_MAX) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100334 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
335 (priority != BOOK3S_IRQPRIO_DECREMENTER)) {
336 /* DEC interrupts get cleared by mtdec */
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000337 clear_bit(priority, &vcpu->arch.pending_exceptions);
338 break;
339 }
340
341 priority = find_next_bit(pending,
342 BITS_PER_BYTE * sizeof(*pending),
343 priority + 1);
344 }
345}
346
347void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
348{
Alexander Grafb83d4a92010-04-20 02:49:54 +0200349 u32 host_pvr;
350
Alexander Grafe15a1132009-11-30 03:02:02 +0000351 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000352 vcpu->arch.pvr = pvr;
Alexander Graf07b09072010-04-16 00:11:53 +0200353#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000354 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
355 kvmppc_mmu_book3s_64_init(vcpu);
356 to_book3s(vcpu)->hior = 0xfff00000;
357 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
Alexander Graf07b09072010-04-16 00:11:53 +0200358 } else
359#endif
360 {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000361 kvmppc_mmu_book3s_32_init(vcpu);
362 to_book3s(vcpu)->hior = 0;
363 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
364 }
365
366 /* If we are in hypervisor level on 970, we can tell the CPU to
367 * treat DCBZ as 32 bytes store */
368 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
369 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
370 !strcmp(cur_cpu_spec->platform, "ppc970"))
371 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
372
Alexander Graf05b0ab12010-03-24 21:48:37 +0100373 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
374 really needs them in a VM on Cell and force disable them. */
375 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
376 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
Alexander Graf07b09072010-04-16 00:11:53 +0200377
378#ifdef CONFIG_PPC_BOOK3S_32
379 /* 32 bit Book3S always has 32 byte dcbz */
380 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
381#endif
Alexander Grafb83d4a92010-04-20 02:49:54 +0200382
383 /* On some CPUs we can execute paired single operations natively */
384 asm ( "mfpvr %0" : "=r"(host_pvr));
385 switch (host_pvr) {
386 case 0x00080200: /* lonestar 2.0 */
387 case 0x00088202: /* lonestar 2.2 */
388 case 0x70000100: /* gekko 1.0 */
389 case 0x00080100: /* gekko 2.0 */
390 case 0x00083203: /* gekko 2.3a */
391 case 0x00083213: /* gekko 2.3b */
392 case 0x00083204: /* gekko 2.4 */
393 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
394 case 0x00087200: /* broadway */
395 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
396 /* Enable HID2.PSE - in case we need it later */
397 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
398 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000399}
400
401/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
402 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
403 * emulate 32 bytes dcbz length.
404 *
405 * The Book3s_64 inventors also realized this case and implemented a special bit
406 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
407 *
408 * My approach here is to patch the dcbz instruction on executing pages.
409 */
410static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
411{
Alexander Graf9fb244a2010-03-24 21:48:32 +0100412 struct page *hpage;
413 u64 hpage_offset;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000414 u32 *page;
415 int i;
416
Alexander Graf9fb244a2010-03-24 21:48:32 +0100417 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
418 if (is_error_page(hpage))
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000419 return;
420
Alexander Graf9fb244a2010-03-24 21:48:32 +0100421 hpage_offset = pte->raddr & ~PAGE_MASK;
422 hpage_offset &= ~0xFFFULL;
423 hpage_offset /= 4;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000424
Alexander Graf9fb244a2010-03-24 21:48:32 +0100425 get_page(hpage);
426 page = kmap_atomic(hpage, KM_USER0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000427
Alexander Graf9fb244a2010-03-24 21:48:32 +0100428 /* patch dcbz into reserved instruction, so we trap */
429 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
430 if ((page[i] & 0xff0007ff) == INS_DCBZ)
431 page[i] &= 0xfffffff7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000432
Alexander Graf9fb244a2010-03-24 21:48:32 +0100433 kunmap_atomic(page, KM_USER0);
434 put_page(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000435}
436
437static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
438 struct kvmppc_pte *pte)
439{
Alexander Graf666e7252010-07-29 14:47:43 +0200440 int relocated = (vcpu->arch.shared->msr & (data ? MSR_DR : MSR_IR));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000441 int r;
442
443 if (relocated) {
444 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
445 } else {
446 pte->eaddr = eaddr;
447 pte->raddr = eaddr & 0xffffffff;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100448 pte->vpage = VSID_REAL | eaddr >> 12;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000449 pte->may_read = true;
450 pte->may_write = true;
451 pte->may_execute = true;
452 r = 0;
453 }
454
455 return r;
456}
457
458static hva_t kvmppc_bad_hva(void)
459{
460 return PAGE_OFFSET;
461}
462
463static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
464 bool read)
465{
466 hva_t hpage;
467
468 if (read && !pte->may_read)
469 goto err;
470
471 if (!read && !pte->may_write)
472 goto err;
473
474 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
475 if (kvm_is_error_hva(hpage))
476 goto err;
477
478 return hpage | (pte->raddr & ~PAGE_MASK);
479err:
480 return kvmppc_bad_hva();
481}
482
Alexander Graf5467a972010-02-19 11:00:38 +0100483int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
484 bool data)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000485{
486 struct kvmppc_pte pte;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000487
488 vcpu->stat.st++;
489
Alexander Graf5467a972010-02-19 11:00:38 +0100490 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
Alexander Graf9fb244a2010-03-24 21:48:32 +0100491 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100492
493 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000494
Alexander Graf9fb244a2010-03-24 21:48:32 +0100495 if (!pte.may_write)
496 return -EPERM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000497
Alexander Graf9fb244a2010-03-24 21:48:32 +0100498 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
499 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000500
Alexander Graf5467a972010-02-19 11:00:38 +0100501 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000502}
503
Alexander Graf5467a972010-02-19 11:00:38 +0100504int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000505 bool data)
506{
507 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100508 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000509
510 vcpu->stat.ld++;
511
Alexander Graf5467a972010-02-19 11:00:38 +0100512 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
513 goto nopte;
514
515 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000516
517 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
518 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100519 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000520
521 if (copy_from_user(ptr, (void __user *)hva, size)) {
522 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100523 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000524 }
525
Alexander Graf5467a972010-02-19 11:00:38 +0100526 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000527
Alexander Graf5467a972010-02-19 11:00:38 +0100528nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000529 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100530mmio:
531 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000532}
533
534static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
535{
536 return kvm_is_visible_gfn(vcpu->kvm, gfn);
537}
538
539int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
540 ulong eaddr, int vec)
541{
542 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
543 int r = RESUME_GUEST;
544 int relocated;
545 int page_found = 0;
546 struct kvmppc_pte pte;
547 bool is_mmio = false;
Alexander Graf666e7252010-07-29 14:47:43 +0200548 bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
549 bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
Alexander Graff7bc74e2010-04-20 02:49:48 +0200550 u64 vsid;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000551
Alexander Graf3eeafd72010-03-24 21:48:17 +0100552 relocated = data ? dr : ir;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000553
554 /* Resolve real address if translation turned on */
555 if (relocated) {
556 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
557 } else {
558 pte.may_execute = true;
559 pte.may_read = true;
560 pte.may_write = true;
561 pte.raddr = eaddr & 0xffffffff;
562 pte.eaddr = eaddr;
563 pte.vpage = eaddr >> 12;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100564 }
565
Alexander Graf666e7252010-07-29 14:47:43 +0200566 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
Alexander Graf3eeafd72010-03-24 21:48:17 +0100567 case 0:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200568 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
Alexander Graf3eeafd72010-03-24 21:48:17 +0100569 break;
570 case MSR_DR:
Alexander Graf3eeafd72010-03-24 21:48:17 +0100571 case MSR_IR:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200572 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
573
Alexander Graf666e7252010-07-29 14:47:43 +0200574 if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
Alexander Graff7bc74e2010-04-20 02:49:48 +0200575 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
576 else
577 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
578 pte.vpage |= vsid;
579
580 if (vsid == -1)
581 page_found = -EINVAL;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100582 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000583 }
584
585 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
586 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
587 /*
588 * If we do the dcbz hack, we have to NX on every execution,
589 * so we can patch the executing code. This renders our guest
590 * NX-less.
591 */
592 pte.may_execute = !data;
593 }
594
595 if (page_found == -ENOENT) {
596 /* Page not found in guest PTE entries */
Alexander Graf5e030182010-07-29 14:47:45 +0200597 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Grafd562de42010-07-29 14:47:44 +0200598 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf666e7252010-07-29 14:47:43 +0200599 vcpu->arch.shared->msr |=
600 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000601 kvmppc_book3s_queue_irqprio(vcpu, vec);
602 } else if (page_found == -EPERM) {
603 /* Storage protection */
Alexander Graf5e030182010-07-29 14:47:45 +0200604 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Grafd562de42010-07-29 14:47:44 +0200605 vcpu->arch.shared->dsisr =
606 to_svcpu(vcpu)->fault_dsisr & ~DSISR_NOHPTE;
607 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
Alexander Graf666e7252010-07-29 14:47:43 +0200608 vcpu->arch.shared->msr |=
609 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000610 kvmppc_book3s_queue_irqprio(vcpu, vec);
611 } else if (page_found == -EINVAL) {
612 /* Page not found in guest SLB */
Alexander Graf5e030182010-07-29 14:47:45 +0200613 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000614 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
615 } else if (!is_mmio &&
616 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
617 /* The guest's PTE is not mapped yet. Map on the host */
618 kvmppc_mmu_map_page(vcpu, &pte);
619 if (data)
620 vcpu->stat.sp_storage++;
621 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
622 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
623 kvmppc_patch_dcbz(vcpu, &pte);
624 } else {
625 /* MMIO */
626 vcpu->stat.mmio_exits++;
627 vcpu->arch.paddr_accessed = pte.raddr;
628 r = kvmppc_emulate_mmio(run, vcpu);
629 if ( r == RESUME_HOST_NV )
630 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000631 }
632
633 return r;
634}
635
Alexander Graf180a34d2010-01-15 14:49:11 +0100636static inline int get_fpr_index(int i)
637{
638#ifdef CONFIG_VSX
639 i *= 2;
640#endif
641 return i;
642}
643
644/* Give up external provider (FPU, Altivec, VSX) */
Alexander Grafaba3bd72010-02-19 11:00:39 +0100645void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
Alexander Graf180a34d2010-01-15 14:49:11 +0100646{
647 struct thread_struct *t = &current->thread;
648 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100649#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100650 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100651#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100652 u64 *thread_fpr = (u64*)t->fpr;
653 int i;
654
655 if (!(vcpu->arch.guest_owned_ext & msr))
656 return;
657
658#ifdef DEBUG_EXT
659 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
660#endif
661
662 switch (msr) {
663 case MSR_FP:
664 giveup_fpu(current);
665 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
666 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
667
668 vcpu->arch.fpscr = t->fpscr.val;
669 break;
670 case MSR_VEC:
671#ifdef CONFIG_ALTIVEC
672 giveup_altivec(current);
673 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
674 vcpu->arch.vscr = t->vscr;
675#endif
676 break;
677 case MSR_VSX:
678#ifdef CONFIG_VSX
679 __giveup_vsx(current);
680 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
681 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
682#endif
683 break;
684 default:
685 BUG();
686 }
687
688 vcpu->arch.guest_owned_ext &= ~msr;
689 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100690 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100691}
692
Alexander Graf89632212010-03-24 21:48:21 +0100693static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100694{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200695 ulong srr0 = kvmppc_get_pc(vcpu);
696 u32 last_inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100697 int ret;
698
Alexander Grafc7f38f42010-04-16 00:11:40 +0200699 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100700 if (ret == -ENOENT) {
Alexander Graf666e7252010-07-29 14:47:43 +0200701 ulong msr = vcpu->arch.shared->msr;
702
703 msr = kvmppc_set_field(msr, 33, 33, 1);
704 msr = kvmppc_set_field(msr, 34, 36, 0);
705 vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100706 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
Alexander Graf89632212010-03-24 21:48:21 +0100707 return EMULATE_AGAIN;
708 }
709
710 return EMULATE_DONE;
711}
712
713static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
714{
715
716 /* Need to do paired single emulation? */
717 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
718 return EMULATE_DONE;
719
720 /* Read out the instruction */
721 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100722 /* Need to emulate */
723 return EMULATE_FAIL;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100724
725 return EMULATE_AGAIN;
726}
727
Alexander Graf180a34d2010-01-15 14:49:11 +0100728/* Handle external providers (FPU, Altivec, VSX) */
729static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
730 ulong msr)
731{
732 struct thread_struct *t = &current->thread;
733 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100734#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100735 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100736#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100737 u64 *thread_fpr = (u64*)t->fpr;
738 int i;
739
Alexander Graf3c402a72010-02-19 11:00:32 +0100740 /* When we have paired singles, we emulate in software */
741 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
742 return RESUME_GUEST;
743
Alexander Graf666e7252010-07-29 14:47:43 +0200744 if (!(vcpu->arch.shared->msr & msr)) {
Alexander Graf180a34d2010-01-15 14:49:11 +0100745 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
746 return RESUME_GUEST;
747 }
748
Alexander Grafc2453692010-03-24 21:48:22 +0100749 /* We already own the ext */
750 if (vcpu->arch.guest_owned_ext & msr) {
751 return RESUME_GUEST;
752 }
753
Alexander Graf180a34d2010-01-15 14:49:11 +0100754#ifdef DEBUG_EXT
755 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
756#endif
757
758 current->thread.regs->msr |= msr;
759
760 switch (msr) {
761 case MSR_FP:
762 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
763 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
764
765 t->fpscr.val = vcpu->arch.fpscr;
766 t->fpexc_mode = 0;
767 kvmppc_load_up_fpu();
768 break;
769 case MSR_VEC:
770#ifdef CONFIG_ALTIVEC
771 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
772 t->vscr = vcpu->arch.vscr;
773 t->vrsave = -1;
774 kvmppc_load_up_altivec();
775#endif
776 break;
777 case MSR_VSX:
778#ifdef CONFIG_VSX
779 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
780 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
781 kvmppc_load_up_vsx();
782#endif
783 break;
784 default:
785 BUG();
786 }
787
788 vcpu->arch.guest_owned_ext |= msr;
789
Alexander Grafa76f8492010-01-15 14:49:14 +0100790 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100791
792 return RESUME_GUEST;
793}
794
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000795int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
796 unsigned int exit_nr)
797{
798 int r = RESUME_HOST;
799
800 vcpu->stat.sum_exits++;
801
802 run->exit_reason = KVM_EXIT_UNKNOWN;
803 run->ready_for_interrupt_injection = 1;
804#ifdef EXIT_DEBUG
805 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | dec=0x%x | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200806 exit_nr, kvmppc_get_pc(vcpu), kvmppc_get_fault_dar(vcpu),
807 kvmppc_get_dec(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000808#elif defined (EXIT_DEBUG_SIMPLE)
809 if ((exit_nr != 0x900) && (exit_nr != 0x500))
810 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200811 exit_nr, kvmppc_get_pc(vcpu), kvmppc_get_fault_dar(vcpu),
Alexander Graf666e7252010-07-29 14:47:43 +0200812 vcpu->arch.shared->msr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000813#endif
814 kvm_resched(vcpu);
815 switch (exit_nr) {
816 case BOOK3S_INTERRUPT_INST_STORAGE:
817 vcpu->stat.pf_instruc++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200818
819#ifdef CONFIG_PPC_BOOK3S_32
820 /* We set segments as unused segments when invalidating them. So
821 * treat the respective fault as segment fault. */
822 if (to_svcpu(vcpu)->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT]
823 == SR_INVALID) {
824 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
825 r = RESUME_GUEST;
826 break;
827 }
828#endif
829
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000830 /* only care about PTEG not found errors, but leave NX alone */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200831 if (to_svcpu(vcpu)->shadow_srr1 & 0x40000000) {
832 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000833 vcpu->stat.sp_instruc++;
834 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
835 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
836 /*
837 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
838 * so we can't use the NX bit inside the guest. Let's cross our fingers,
839 * that no guest that needs the dcbz hack does NX.
840 */
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200841 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100842 r = RESUME_GUEST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000843 } else {
Alexander Graf666e7252010-07-29 14:47:43 +0200844 vcpu->arch.shared->msr |=
845 to_svcpu(vcpu)->shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000846 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200847 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000848 r = RESUME_GUEST;
849 }
850 break;
851 case BOOK3S_INTERRUPT_DATA_STORAGE:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200852 {
853 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000854 vcpu->stat.pf_storage++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200855
856#ifdef CONFIG_PPC_BOOK3S_32
857 /* We set segments as unused segments when invalidating them. So
858 * treat the respective fault as segment fault. */
859 if ((to_svcpu(vcpu)->sr[dar >> SID_SHIFT]) == SR_INVALID) {
860 kvmppc_mmu_map_segment(vcpu, dar);
861 r = RESUME_GUEST;
862 break;
863 }
864#endif
865
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000866 /* The only case we need to handle is missing shadow PTEs */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200867 if (to_svcpu(vcpu)->fault_dsisr & DSISR_NOHPTE) {
868 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000869 } else {
Alexander Graf5e030182010-07-29 14:47:45 +0200870 vcpu->arch.shared->dar = dar;
Alexander Grafd562de42010-07-29 14:47:44 +0200871 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000872 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Graf5e030182010-07-29 14:47:45 +0200873 kvmppc_mmu_pte_flush(vcpu, dar, ~0xFFFUL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000874 r = RESUME_GUEST;
875 }
876 break;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200877 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000878 case BOOK3S_INTERRUPT_DATA_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200879 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
Alexander Graf5e030182010-07-29 14:47:45 +0200880 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000881 kvmppc_book3s_queue_irqprio(vcpu,
882 BOOK3S_INTERRUPT_DATA_SEGMENT);
883 }
884 r = RESUME_GUEST;
885 break;
886 case BOOK3S_INTERRUPT_INST_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200887 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000888 kvmppc_book3s_queue_irqprio(vcpu,
889 BOOK3S_INTERRUPT_INST_SEGMENT);
890 }
891 r = RESUME_GUEST;
892 break;
893 /* We're good on these - the host merely wanted to get our attention */
894 case BOOK3S_INTERRUPT_DECREMENTER:
895 vcpu->stat.dec_exits++;
896 r = RESUME_GUEST;
897 break;
898 case BOOK3S_INTERRUPT_EXTERNAL:
899 vcpu->stat.ext_intr_exits++;
900 r = RESUME_GUEST;
901 break;
Alexander Graf7fdaec92010-04-20 02:49:47 +0200902 case BOOK3S_INTERRUPT_PERFMON:
903 r = RESUME_GUEST;
904 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000905 case BOOK3S_INTERRUPT_PROGRAM:
906 {
907 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100908 ulong flags;
909
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100910program_interrupt:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200911 flags = to_svcpu(vcpu)->shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000912
Alexander Graf666e7252010-07-29 14:47:43 +0200913 if (vcpu->arch.shared->msr & MSR_PR) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000914#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +0200915 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 +0000916#endif
Alexander Grafc7f38f42010-04-16 00:11:40 +0200917 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000918 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100919 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000920 r = RESUME_GUEST;
921 break;
922 }
923 }
924
925 vcpu->stat.emulated_inst_exits++;
926 er = kvmppc_emulate_instruction(run, vcpu);
927 switch (er) {
928 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100929 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000930 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100931 case EMULATE_AGAIN:
932 r = RESUME_GUEST;
933 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000934 case EMULATE_FAIL:
935 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200936 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100937 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000938 r = RESUME_GUEST;
939 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +0100940 case EMULATE_DO_MMIO:
941 run->exit_reason = KVM_EXIT_MMIO;
942 r = RESUME_HOST_NV;
943 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000944 default:
945 BUG();
946 }
947 break;
948 }
949 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafad0a0482010-03-24 21:48:30 +0100950 // XXX make user settable
951 if (vcpu->arch.osi_enabled &&
952 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
953 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
954 u64 *gprs = run->osi.gprs;
955 int i;
956
957 run->exit_reason = KVM_EXIT_OSI;
958 for (i = 0; i < 32; i++)
959 gprs[i] = kvmppc_get_gpr(vcpu, i);
960 vcpu->arch.osi_needed = 1;
961 r = RESUME_HOST_NV;
962
963 } else {
964 vcpu->stat.syscall_exits++;
965 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
966 r = RESUME_GUEST;
967 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000968 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000969 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000970 case BOOK3S_INTERRUPT_ALTIVEC:
971 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100972 {
973 int ext_msr = 0;
974
975 switch (exit_nr) {
976 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
977 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
978 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
979 }
980
981 switch (kvmppc_check_ext(vcpu, exit_nr)) {
982 case EMULATE_DONE:
983 /* everything ok - let's enable the ext */
984 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
985 break;
986 case EMULATE_FAIL:
987 /* we need to emulate this instruction */
988 goto program_interrupt;
989 break;
990 default:
991 /* nothing to worry about - go again */
992 break;
993 }
Alexander Graf180a34d2010-01-15 14:49:11 +0100994 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100995 }
Alexander Grafca7f4202010-03-24 21:48:28 +0100996 case BOOK3S_INTERRUPT_ALIGNMENT:
997 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
Alexander Grafd562de42010-07-29 14:47:44 +0200998 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +0200999 kvmppc_get_last_inst(vcpu));
Alexander Graf5e030182010-07-29 14:47:45 +02001000 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001001 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +01001002 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1003 }
1004 r = RESUME_GUEST;
1005 break;
Alexander Graf180a34d2010-01-15 14:49:11 +01001006 case BOOK3S_INTERRUPT_MACHINE_CHECK:
1007 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001008 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1009 r = RESUME_GUEST;
1010 break;
1011 default:
1012 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +01001013 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +02001014 exit_nr, kvmppc_get_pc(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001015 r = RESUME_HOST;
1016 BUG();
1017 break;
1018 }
1019
1020
1021 if (!(r & RESUME_HOST)) {
1022 /* To avoid clobbering exit_reason, only check for signals if
1023 * we aren't already exiting to userspace for some other
1024 * reason. */
1025 if (signal_pending(current)) {
1026#ifdef EXIT_DEBUG
1027 printk(KERN_EMERG "KVM: Going back to host\n");
1028#endif
1029 vcpu->stat.signal_exits++;
1030 run->exit_reason = KVM_EXIT_INTR;
1031 r = -EINTR;
1032 } else {
1033 /* In case an interrupt came in that was triggered
1034 * from userspace (like DEC), we need to check what
1035 * to inject now! */
1036 kvmppc_core_deliver_interrupts(vcpu);
1037 }
1038 }
1039
1040#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +02001041 printk(KERN_EMERG "KVM exit: vcpu=0x%p pc=0x%lx r=0x%x\n", vcpu, kvmppc_get_pc(vcpu), r);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001042#endif
1043
1044 return r;
1045}
1046
1047int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1048{
1049 return 0;
1050}
1051
1052int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1053{
1054 int i;
1055
Alexander Grafc7f38f42010-04-16 00:11:40 +02001056 regs->pc = kvmppc_get_pc(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001057 regs->cr = kvmppc_get_cr(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001058 regs->ctr = kvmppc_get_ctr(vcpu);
1059 regs->lr = kvmppc_get_lr(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001060 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001061 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +02001062 regs->srr0 = vcpu->arch.shared->srr0;
1063 regs->srr1 = vcpu->arch.shared->srr1;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001064 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001065 regs->sprg0 = vcpu->arch.shared->sprg0;
1066 regs->sprg1 = vcpu->arch.shared->sprg1;
1067 regs->sprg2 = vcpu->arch.shared->sprg2;
1068 regs->sprg3 = vcpu->arch.shared->sprg3;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001069 regs->sprg5 = vcpu->arch.sprg4;
1070 regs->sprg6 = vcpu->arch.sprg5;
1071 regs->sprg7 = vcpu->arch.sprg6;
1072
1073 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001074 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001075
1076 return 0;
1077}
1078
1079int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1080{
1081 int i;
1082
Alexander Grafc7f38f42010-04-16 00:11:40 +02001083 kvmppc_set_pc(vcpu, regs->pc);
Alexander Graf992b5b22010-01-08 02:58:02 +01001084 kvmppc_set_cr(vcpu, regs->cr);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001085 kvmppc_set_ctr(vcpu, regs->ctr);
1086 kvmppc_set_lr(vcpu, regs->lr);
Alexander Graf992b5b22010-01-08 02:58:02 +01001087 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001088 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +02001089 vcpu->arch.shared->srr0 = regs->srr0;
1090 vcpu->arch.shared->srr1 = regs->srr1;
Alexander Grafa73a9592010-07-29 14:47:47 +02001091 vcpu->arch.shared->sprg0 = regs->sprg0;
1092 vcpu->arch.shared->sprg1 = regs->sprg1;
1093 vcpu->arch.shared->sprg2 = regs->sprg2;
1094 vcpu->arch.shared->sprg3 = regs->sprg3;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001095 vcpu->arch.sprg5 = regs->sprg4;
1096 vcpu->arch.sprg6 = regs->sprg5;
1097 vcpu->arch.sprg7 = regs->sprg6;
1098
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001099 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1100 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001101
1102 return 0;
1103}
1104
1105int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1106 struct kvm_sregs *sregs)
1107{
Alexander Grafe15a1132009-11-30 03:02:02 +00001108 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1109 int i;
1110
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001111 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +00001112
1113 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1114 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1115 for (i = 0; i < 64; i++) {
1116 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
1117 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1118 }
1119 } else {
1120 for (i = 0; i < 16; i++) {
1121 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1122 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1123 }
1124 for (i = 0; i < 8; i++) {
1125 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1126 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1127 }
1128 }
Avi Kivity98001d82010-05-13 11:05:49 +03001129
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001130 return 0;
1131}
1132
1133int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1134 struct kvm_sregs *sregs)
1135{
Alexander Grafe15a1132009-11-30 03:02:02 +00001136 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1137 int i;
1138
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001139 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001140
1141 vcpu3s->sdr1 = sregs->u.s.sdr1;
1142 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1143 for (i = 0; i < 64; i++) {
1144 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1145 sregs->u.s.ppc64.slb[i].slbe);
1146 }
1147 } else {
1148 for (i = 0; i < 16; i++) {
1149 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1150 }
1151 for (i = 0; i < 8; i++) {
1152 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1153 (u32)sregs->u.s.ppc32.ibat[i]);
1154 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1155 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1156 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1157 (u32)sregs->u.s.ppc32.dbat[i]);
1158 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1159 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1160 }
1161 }
1162
1163 /* Flush the MMU after messing with the segments */
1164 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Avi Kivity98001d82010-05-13 11:05:49 +03001165
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001166 return 0;
1167}
1168
1169int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1170{
1171 return -ENOTSUPP;
1172}
1173
1174int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1175{
1176 return -ENOTSUPP;
1177}
1178
1179int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1180 struct kvm_translation *tr)
1181{
1182 return 0;
1183}
1184
1185/*
1186 * Get (and clear) the dirty memory log for a memory slot.
1187 */
1188int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1189 struct kvm_dirty_log *log)
1190{
1191 struct kvm_memory_slot *memslot;
1192 struct kvm_vcpu *vcpu;
1193 ulong ga, ga_end;
1194 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001195 int r;
1196 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001197
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001198 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001199
1200 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1201 if (r)
1202 goto out;
1203
1204 /* If nothing is dirty, don't bother messing with page tables. */
1205 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001206 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001207
1208 ga = memslot->base_gfn << PAGE_SHIFT;
1209 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1210
1211 kvm_for_each_vcpu(n, vcpu, kvm)
1212 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1213
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001214 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001215 memset(memslot->dirty_bitmap, 0, n);
1216 }
1217
1218 r = 0;
1219out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001220 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001221 return r;
1222}
1223
1224int kvmppc_core_check_processor_compat(void)
1225{
1226 return 0;
1227}
1228
1229struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1230{
1231 struct kvmppc_vcpu_book3s *vcpu_book3s;
1232 struct kvm_vcpu *vcpu;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001233 int err = -ENOMEM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001234
Alexander Graf032c3402010-02-19 12:24:33 +01001235 vcpu_book3s = vmalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Grafc7f38f42010-04-16 00:11:40 +02001236 if (!vcpu_book3s)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001237 goto out;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001238
Alexander Graf7e821d32010-02-22 16:52:08 +01001239 memset(vcpu_book3s, 0, sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001240
Alexander Grafc7f38f42010-04-16 00:11:40 +02001241 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
1242 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
1243 if (!vcpu_book3s->shadow_vcpu)
1244 goto free_vcpu;
1245
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001246 vcpu = &vcpu_book3s->vcpu;
1247 err = kvm_vcpu_init(vcpu, kvm, id);
1248 if (err)
Alexander Grafc7f38f42010-04-16 00:11:40 +02001249 goto free_shadow_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001250
Alexander Graf96bc4512010-07-29 14:47:42 +02001251 vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO);
1252 if (!vcpu->arch.shared)
1253 goto uninit_vcpu;
1254
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001255 vcpu->arch.host_retip = kvm_return_point;
1256 vcpu->arch.host_msr = mfmsr();
Alexander Graf07b09072010-04-16 00:11:53 +02001257#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001258 /* default to book3s_64 (970fx) */
1259 vcpu->arch.pvr = 0x3C0301;
Alexander Graf07b09072010-04-16 00:11:53 +02001260#else
1261 /* default to book3s_32 (750) */
1262 vcpu->arch.pvr = 0x84202;
1263#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001264 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1265 vcpu_book3s->slb_nr = 64;
1266
1267 /* remember where some real-mode handlers are */
1268 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1269 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1270 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf07b09072010-04-16 00:11:53 +02001271#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf021ec9c2010-01-08 02:58:06 +01001272 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf07b09072010-04-16 00:11:53 +02001273#else
1274 vcpu->arch.rmcall = (ulong)kvmppc_rmcall;
1275#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001276
1277 vcpu->arch.shadow_msr = MSR_USER64;
1278
Alexander Graf9cc5e952010-04-16 00:11:45 +02001279 err = kvmppc_mmu_init(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001280 if (err < 0)
Alexander Graf96bc4512010-07-29 14:47:42 +02001281 goto uninit_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001282
1283 return vcpu;
1284
Alexander Graf96bc4512010-07-29 14:47:42 +02001285uninit_vcpu:
1286 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001287free_shadow_vcpu:
1288 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001289free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001290 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001291out:
1292 return ERR_PTR(err);
1293}
1294
1295void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1296{
1297 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1298
Alexander Graf96bc4512010-07-29 14:47:42 +02001299 free_page((unsigned long)vcpu->arch.shared);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001300 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001301 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001302 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001303}
1304
1305extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1306int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1307{
1308 int ret;
Andreas Schwab49f6be82010-05-31 21:59:13 +02001309 double fpr[32][TS_FPRWIDTH];
1310 unsigned int fpscr;
1311 int fpexc_mode;
Alexander Grafa2b07662010-03-24 21:48:31 +01001312#ifdef CONFIG_ALTIVEC
Andreas Schwab49f6be82010-05-31 21:59:13 +02001313 vector128 vr[32];
1314 vector128 vscr;
1315 unsigned long uninitialized_var(vrsave);
1316 int used_vr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001317#endif
1318#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001319 int used_vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001320#endif
Alexander Graf180a34d2010-01-15 14:49:11 +01001321 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001322
1323 /* No need to go into the guest when all we do is going out */
1324 if (signal_pending(current)) {
1325 kvm_run->exit_reason = KVM_EXIT_INTR;
1326 return -EINTR;
1327 }
1328
Alexander Graf180a34d2010-01-15 14:49:11 +01001329 /* Save FPU state in stack */
1330 if (current->thread.regs->msr & MSR_FP)
1331 giveup_fpu(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001332 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
1333 fpscr = current->thread.fpscr.val;
1334 fpexc_mode = current->thread.fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001335
1336#ifdef CONFIG_ALTIVEC
1337 /* Save Altivec state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001338 used_vr = current->thread.used_vr;
1339 if (used_vr) {
Alexander Graf180a34d2010-01-15 14:49:11 +01001340 if (current->thread.regs->msr & MSR_VEC)
1341 giveup_altivec(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001342 memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
1343 vscr = current->thread.vscr;
1344 vrsave = current->thread.vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001345 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001346#endif
1347
1348#ifdef CONFIG_VSX
1349 /* Save VSX state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001350 used_vsr = current->thread.used_vsr;
1351 if (used_vsr && (current->thread.regs->msr & MSR_VSX))
Alexander Graf180a34d2010-01-15 14:49:11 +01001352 __giveup_vsx(current);
Alexander Graf180a34d2010-01-15 14:49:11 +01001353#endif
1354
1355 /* Remember the MSR with disabled extensions */
1356 ext_msr = current->thread.regs->msr;
1357
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001358 /* XXX we get called with irq disabled - change that! */
1359 local_irq_enable();
1360
Alexander Grafd1bab742010-02-19 11:00:35 +01001361 /* Preload FPU if it's enabled */
Alexander Graf666e7252010-07-29 14:47:43 +02001362 if (vcpu->arch.shared->msr & MSR_FP)
Alexander Grafd1bab742010-02-19 11:00:35 +01001363 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1364
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001365 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1366
1367 local_irq_disable();
1368
Alexander Graf180a34d2010-01-15 14:49:11 +01001369 current->thread.regs->msr = ext_msr;
1370
1371 /* Make sure we save the guest FPU/Altivec/VSX state */
1372 kvmppc_giveup_ext(vcpu, MSR_FP);
1373 kvmppc_giveup_ext(vcpu, MSR_VEC);
1374 kvmppc_giveup_ext(vcpu, MSR_VSX);
1375
1376 /* Restore FPU state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001377 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
1378 current->thread.fpscr.val = fpscr;
1379 current->thread.fpexc_mode = fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001380
1381#ifdef CONFIG_ALTIVEC
1382 /* Restore Altivec state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001383 if (used_vr && current->thread.used_vr) {
1384 memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
1385 current->thread.vscr = vscr;
1386 current->thread.vrsave = vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001387 }
Andreas Schwab49f6be82010-05-31 21:59:13 +02001388 current->thread.used_vr = used_vr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001389#endif
1390
1391#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001392 current->thread.used_vsr = used_vsr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001393#endif
1394
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001395 return ret;
1396}
1397
1398static int kvmppc_book3s_init(void)
1399{
Alexander Graffef093be2010-06-30 15:18:46 +02001400 int r;
1401
1402 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
1403 THIS_MODULE);
1404
1405 if (r)
1406 return r;
1407
1408 r = kvmppc_mmu_hpte_sysinit();
1409
1410 return r;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001411}
1412
1413static void kvmppc_book3s_exit(void)
1414{
Alexander Graffef093be2010-06-30 15:18:46 +02001415 kvmppc_mmu_hpte_sysexit();
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001416 kvm_exit();
1417}
1418
1419module_init(kvmppc_book3s_init);
1420module_exit(kvmppc_book3s_exit);