blob: 7656b6df0d8bcc934f1bb4507cad30b7001f314c [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 Graf5c6cedf2010-07-29 14:47:49 +0200254 ulong crit_raw = vcpu->arch.shared->critical;
255 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
256 bool crit;
257
258 /* Truncate crit indicators in 32 bit mode */
259 if (!(vcpu->arch.shared->msr & MSR_SF)) {
260 crit_raw &= 0xffffffff;
261 crit_r1 &= 0xffffffff;
262 }
263
264 /* Critical section when crit == r1 */
265 crit = (crit_raw == crit_r1);
266 /* ... and we're in supervisor mode */
267 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000268
269 switch (priority) {
270 case BOOK3S_IRQPRIO_DECREMENTER:
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200271 deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000272 vec = BOOK3S_INTERRUPT_DECREMENTER;
273 break;
274 case BOOK3S_IRQPRIO_EXTERNAL:
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200275 deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000276 vec = BOOK3S_INTERRUPT_EXTERNAL;
277 break;
278 case BOOK3S_IRQPRIO_SYSTEM_RESET:
279 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
280 break;
281 case BOOK3S_IRQPRIO_MACHINE_CHECK:
282 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
283 break;
284 case BOOK3S_IRQPRIO_DATA_STORAGE:
285 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
286 break;
287 case BOOK3S_IRQPRIO_INST_STORAGE:
288 vec = BOOK3S_INTERRUPT_INST_STORAGE;
289 break;
290 case BOOK3S_IRQPRIO_DATA_SEGMENT:
291 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
292 break;
293 case BOOK3S_IRQPRIO_INST_SEGMENT:
294 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
295 break;
296 case BOOK3S_IRQPRIO_ALIGNMENT:
297 vec = BOOK3S_INTERRUPT_ALIGNMENT;
298 break;
299 case BOOK3S_IRQPRIO_PROGRAM:
300 vec = BOOK3S_INTERRUPT_PROGRAM;
Alexander Graf25a8a022010-01-08 02:58:07 +0100301 flags = to_book3s(vcpu)->prog_flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000302 break;
303 case BOOK3S_IRQPRIO_VSX:
304 vec = BOOK3S_INTERRUPT_VSX;
305 break;
306 case BOOK3S_IRQPRIO_ALTIVEC:
307 vec = BOOK3S_INTERRUPT_ALTIVEC;
308 break;
309 case BOOK3S_IRQPRIO_FP_UNAVAIL:
310 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
311 break;
312 case BOOK3S_IRQPRIO_SYSCALL:
313 vec = BOOK3S_INTERRUPT_SYSCALL;
314 break;
315 case BOOK3S_IRQPRIO_DEBUG:
316 vec = BOOK3S_INTERRUPT_TRACE;
317 break;
318 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
319 vec = BOOK3S_INTERRUPT_PERFMON;
320 break;
321 default:
322 deliver = 0;
323 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
324 break;
325 }
326
327#if 0
328 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
329#endif
330
331 if (deliver)
Alexander Graf25a8a022010-01-08 02:58:07 +0100332 kvmppc_inject_interrupt(vcpu, vec, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000333
334 return deliver;
335}
336
337void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
338{
339 unsigned long *pending = &vcpu->arch.pending_exceptions;
Alexander Graf90bba352010-07-29 14:47:51 +0200340 unsigned long old_pending = vcpu->arch.pending_exceptions;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000341 unsigned int priority;
342
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000343#ifdef EXIT_DEBUG
344 if (vcpu->arch.pending_exceptions)
345 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
346#endif
347 priority = __ffs(*pending);
Alexander Grafada7ba12010-04-16 00:11:56 +0200348 while (priority < BOOK3S_IRQPRIO_MAX) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100349 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
350 (priority != BOOK3S_IRQPRIO_DECREMENTER)) {
351 /* DEC interrupts get cleared by mtdec */
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000352 clear_bit(priority, &vcpu->arch.pending_exceptions);
353 break;
354 }
355
356 priority = find_next_bit(pending,
357 BITS_PER_BYTE * sizeof(*pending),
358 priority + 1);
359 }
Alexander Graf90bba352010-07-29 14:47:51 +0200360
361 /* Tell the guest about our interrupt status */
362 if (*pending)
363 vcpu->arch.shared->int_pending = 1;
364 else if (old_pending)
365 vcpu->arch.shared->int_pending = 0;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000366}
367
368void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
369{
Alexander Grafb83d4a92010-04-20 02:49:54 +0200370 u32 host_pvr;
371
Alexander Grafe15a1132009-11-30 03:02:02 +0000372 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000373 vcpu->arch.pvr = pvr;
Alexander Graf07b09072010-04-16 00:11:53 +0200374#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000375 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
376 kvmppc_mmu_book3s_64_init(vcpu);
377 to_book3s(vcpu)->hior = 0xfff00000;
378 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
Alexander Graf07b09072010-04-16 00:11:53 +0200379 } else
380#endif
381 {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000382 kvmppc_mmu_book3s_32_init(vcpu);
383 to_book3s(vcpu)->hior = 0;
384 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
385 }
386
387 /* If we are in hypervisor level on 970, we can tell the CPU to
388 * treat DCBZ as 32 bytes store */
389 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
390 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
391 !strcmp(cur_cpu_spec->platform, "ppc970"))
392 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
393
Alexander Graf05b0ab12010-03-24 21:48:37 +0100394 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
395 really needs them in a VM on Cell and force disable them. */
396 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
397 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
Alexander Graf07b09072010-04-16 00:11:53 +0200398
399#ifdef CONFIG_PPC_BOOK3S_32
400 /* 32 bit Book3S always has 32 byte dcbz */
401 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
402#endif
Alexander Grafb83d4a92010-04-20 02:49:54 +0200403
404 /* On some CPUs we can execute paired single operations natively */
405 asm ( "mfpvr %0" : "=r"(host_pvr));
406 switch (host_pvr) {
407 case 0x00080200: /* lonestar 2.0 */
408 case 0x00088202: /* lonestar 2.2 */
409 case 0x70000100: /* gekko 1.0 */
410 case 0x00080100: /* gekko 2.0 */
411 case 0x00083203: /* gekko 2.3a */
412 case 0x00083213: /* gekko 2.3b */
413 case 0x00083204: /* gekko 2.4 */
414 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
415 case 0x00087200: /* broadway */
416 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
417 /* Enable HID2.PSE - in case we need it later */
418 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
419 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000420}
421
Alexander Grafe8508942010-07-29 14:47:54 +0200422pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn)
423{
424 ulong mp_pa = vcpu->arch.magic_page_pa;
425
426 /* Magic page override */
427 if (unlikely(mp_pa) &&
428 unlikely(((gfn << PAGE_SHIFT) & KVM_PAM) ==
429 ((mp_pa & PAGE_MASK) & KVM_PAM))) {
430 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
431 pfn_t pfn;
432
433 pfn = (pfn_t)virt_to_phys((void*)shared_page) >> PAGE_SHIFT;
434 get_page(pfn_to_page(pfn));
435 return pfn;
436 }
437
438 return gfn_to_pfn(vcpu->kvm, gfn);
439}
440
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000441/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
442 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
443 * emulate 32 bytes dcbz length.
444 *
445 * The Book3s_64 inventors also realized this case and implemented a special bit
446 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
447 *
448 * My approach here is to patch the dcbz instruction on executing pages.
449 */
450static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
451{
Alexander Graf9fb244a2010-03-24 21:48:32 +0100452 struct page *hpage;
453 u64 hpage_offset;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000454 u32 *page;
455 int i;
456
Alexander Graf9fb244a2010-03-24 21:48:32 +0100457 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
Wei Yongjun646bab52010-08-17 10:08:52 +0800458 if (is_error_page(hpage)) {
459 kvm_release_page_clean(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000460 return;
Wei Yongjun646bab52010-08-17 10:08:52 +0800461 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000462
Alexander Graf9fb244a2010-03-24 21:48:32 +0100463 hpage_offset = pte->raddr & ~PAGE_MASK;
464 hpage_offset &= ~0xFFFULL;
465 hpage_offset /= 4;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000466
Alexander Graf9fb244a2010-03-24 21:48:32 +0100467 get_page(hpage);
468 page = kmap_atomic(hpage, KM_USER0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000469
Alexander Graf9fb244a2010-03-24 21:48:32 +0100470 /* patch dcbz into reserved instruction, so we trap */
471 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
472 if ((page[i] & 0xff0007ff) == INS_DCBZ)
473 page[i] &= 0xfffffff7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000474
Alexander Graf9fb244a2010-03-24 21:48:32 +0100475 kunmap_atomic(page, KM_USER0);
476 put_page(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000477}
478
479static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
480 struct kvmppc_pte *pte)
481{
Alexander Graf666e7252010-07-29 14:47:43 +0200482 int relocated = (vcpu->arch.shared->msr & (data ? MSR_DR : MSR_IR));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000483 int r;
484
485 if (relocated) {
486 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
487 } else {
488 pte->eaddr = eaddr;
Alexander Graf28e83b42010-07-29 14:47:52 +0200489 pte->raddr = eaddr & KVM_PAM;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100490 pte->vpage = VSID_REAL | eaddr >> 12;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000491 pte->may_read = true;
492 pte->may_write = true;
493 pte->may_execute = true;
494 r = 0;
495 }
496
497 return r;
498}
499
500static hva_t kvmppc_bad_hva(void)
501{
502 return PAGE_OFFSET;
503}
504
505static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
506 bool read)
507{
508 hva_t hpage;
509
510 if (read && !pte->may_read)
511 goto err;
512
513 if (!read && !pte->may_write)
514 goto err;
515
516 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
517 if (kvm_is_error_hva(hpage))
518 goto err;
519
520 return hpage | (pte->raddr & ~PAGE_MASK);
521err:
522 return kvmppc_bad_hva();
523}
524
Alexander Graf5467a972010-02-19 11:00:38 +0100525int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
526 bool data)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000527{
528 struct kvmppc_pte pte;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000529
530 vcpu->stat.st++;
531
Alexander Graf5467a972010-02-19 11:00:38 +0100532 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
Alexander Graf9fb244a2010-03-24 21:48:32 +0100533 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100534
535 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000536
Alexander Graf9fb244a2010-03-24 21:48:32 +0100537 if (!pte.may_write)
538 return -EPERM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000539
Alexander Graf9fb244a2010-03-24 21:48:32 +0100540 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
541 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000542
Alexander Graf5467a972010-02-19 11:00:38 +0100543 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000544}
545
Alexander Graf5467a972010-02-19 11:00:38 +0100546int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000547 bool data)
548{
549 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100550 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000551
552 vcpu->stat.ld++;
553
Alexander Graf5467a972010-02-19 11:00:38 +0100554 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
555 goto nopte;
556
557 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000558
559 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
560 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100561 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000562
563 if (copy_from_user(ptr, (void __user *)hva, size)) {
564 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100565 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000566 }
567
Alexander Graf5467a972010-02-19 11:00:38 +0100568 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000569
Alexander Graf5467a972010-02-19 11:00:38 +0100570nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000571 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100572mmio:
573 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000574}
575
576static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
577{
Alexander Grafe8508942010-07-29 14:47:54 +0200578 ulong mp_pa = vcpu->arch.magic_page_pa;
579
580 if (unlikely(mp_pa) &&
581 unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) {
582 return 1;
583 }
584
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000585 return kvm_is_visible_gfn(vcpu->kvm, gfn);
586}
587
588int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
589 ulong eaddr, int vec)
590{
591 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
592 int r = RESUME_GUEST;
593 int relocated;
594 int page_found = 0;
595 struct kvmppc_pte pte;
596 bool is_mmio = false;
Alexander Graf666e7252010-07-29 14:47:43 +0200597 bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
598 bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
Alexander Graff7bc74e2010-04-20 02:49:48 +0200599 u64 vsid;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000600
Alexander Graf3eeafd72010-03-24 21:48:17 +0100601 relocated = data ? dr : ir;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000602
603 /* Resolve real address if translation turned on */
604 if (relocated) {
605 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
606 } else {
607 pte.may_execute = true;
608 pte.may_read = true;
609 pte.may_write = true;
Alexander Graf28e83b42010-07-29 14:47:52 +0200610 pte.raddr = eaddr & KVM_PAM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000611 pte.eaddr = eaddr;
612 pte.vpage = eaddr >> 12;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100613 }
614
Alexander Graf666e7252010-07-29 14:47:43 +0200615 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
Alexander Graf3eeafd72010-03-24 21:48:17 +0100616 case 0:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200617 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
Alexander Graf3eeafd72010-03-24 21:48:17 +0100618 break;
619 case MSR_DR:
Alexander Graf3eeafd72010-03-24 21:48:17 +0100620 case MSR_IR:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200621 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
622
Alexander Graf666e7252010-07-29 14:47:43 +0200623 if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
Alexander Graff7bc74e2010-04-20 02:49:48 +0200624 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
625 else
626 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
627 pte.vpage |= vsid;
628
629 if (vsid == -1)
630 page_found = -EINVAL;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100631 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000632 }
633
634 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
635 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
636 /*
637 * If we do the dcbz hack, we have to NX on every execution,
638 * so we can patch the executing code. This renders our guest
639 * NX-less.
640 */
641 pte.may_execute = !data;
642 }
643
644 if (page_found == -ENOENT) {
645 /* Page not found in guest PTE entries */
Alexander Graf5e030182010-07-29 14:47:45 +0200646 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Grafd562de42010-07-29 14:47:44 +0200647 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf666e7252010-07-29 14:47:43 +0200648 vcpu->arch.shared->msr |=
649 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000650 kvmppc_book3s_queue_irqprio(vcpu, vec);
651 } else if (page_found == -EPERM) {
652 /* Storage protection */
Alexander Graf5e030182010-07-29 14:47:45 +0200653 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Grafd562de42010-07-29 14:47:44 +0200654 vcpu->arch.shared->dsisr =
655 to_svcpu(vcpu)->fault_dsisr & ~DSISR_NOHPTE;
656 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
Alexander Graf666e7252010-07-29 14:47:43 +0200657 vcpu->arch.shared->msr |=
658 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000659 kvmppc_book3s_queue_irqprio(vcpu, vec);
660 } else if (page_found == -EINVAL) {
661 /* Page not found in guest SLB */
Alexander Graf5e030182010-07-29 14:47:45 +0200662 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000663 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
664 } else if (!is_mmio &&
665 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
666 /* The guest's PTE is not mapped yet. Map on the host */
667 kvmppc_mmu_map_page(vcpu, &pte);
668 if (data)
669 vcpu->stat.sp_storage++;
670 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
671 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
672 kvmppc_patch_dcbz(vcpu, &pte);
673 } else {
674 /* MMIO */
675 vcpu->stat.mmio_exits++;
676 vcpu->arch.paddr_accessed = pte.raddr;
677 r = kvmppc_emulate_mmio(run, vcpu);
678 if ( r == RESUME_HOST_NV )
679 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000680 }
681
682 return r;
683}
684
Alexander Graf180a34d2010-01-15 14:49:11 +0100685static inline int get_fpr_index(int i)
686{
687#ifdef CONFIG_VSX
688 i *= 2;
689#endif
690 return i;
691}
692
693/* Give up external provider (FPU, Altivec, VSX) */
Alexander Grafaba3bd72010-02-19 11:00:39 +0100694void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
Alexander Graf180a34d2010-01-15 14:49:11 +0100695{
696 struct thread_struct *t = &current->thread;
697 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100698#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100699 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100700#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100701 u64 *thread_fpr = (u64*)t->fpr;
702 int i;
703
704 if (!(vcpu->arch.guest_owned_ext & msr))
705 return;
706
707#ifdef DEBUG_EXT
708 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
709#endif
710
711 switch (msr) {
712 case MSR_FP:
713 giveup_fpu(current);
714 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
715 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
716
717 vcpu->arch.fpscr = t->fpscr.val;
718 break;
719 case MSR_VEC:
720#ifdef CONFIG_ALTIVEC
721 giveup_altivec(current);
722 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
723 vcpu->arch.vscr = t->vscr;
724#endif
725 break;
726 case MSR_VSX:
727#ifdef CONFIG_VSX
728 __giveup_vsx(current);
729 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
730 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
731#endif
732 break;
733 default:
734 BUG();
735 }
736
737 vcpu->arch.guest_owned_ext &= ~msr;
738 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100739 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100740}
741
Alexander Graf89632212010-03-24 21:48:21 +0100742static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100743{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200744 ulong srr0 = kvmppc_get_pc(vcpu);
745 u32 last_inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100746 int ret;
747
Alexander Grafc7f38f42010-04-16 00:11:40 +0200748 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100749 if (ret == -ENOENT) {
Alexander Graf666e7252010-07-29 14:47:43 +0200750 ulong msr = vcpu->arch.shared->msr;
751
752 msr = kvmppc_set_field(msr, 33, 33, 1);
753 msr = kvmppc_set_field(msr, 34, 36, 0);
754 vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100755 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
Alexander Graf89632212010-03-24 21:48:21 +0100756 return EMULATE_AGAIN;
757 }
758
759 return EMULATE_DONE;
760}
761
762static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
763{
764
765 /* Need to do paired single emulation? */
766 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
767 return EMULATE_DONE;
768
769 /* Read out the instruction */
770 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100771 /* Need to emulate */
772 return EMULATE_FAIL;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100773
774 return EMULATE_AGAIN;
775}
776
Alexander Graf180a34d2010-01-15 14:49:11 +0100777/* Handle external providers (FPU, Altivec, VSX) */
778static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
779 ulong msr)
780{
781 struct thread_struct *t = &current->thread;
782 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100783#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100784 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100785#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100786 u64 *thread_fpr = (u64*)t->fpr;
787 int i;
788
Alexander Graf3c402a72010-02-19 11:00:32 +0100789 /* When we have paired singles, we emulate in software */
790 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
791 return RESUME_GUEST;
792
Alexander Graf666e7252010-07-29 14:47:43 +0200793 if (!(vcpu->arch.shared->msr & msr)) {
Alexander Graf180a34d2010-01-15 14:49:11 +0100794 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
795 return RESUME_GUEST;
796 }
797
Alexander Grafc2453692010-03-24 21:48:22 +0100798 /* We already own the ext */
799 if (vcpu->arch.guest_owned_ext & msr) {
800 return RESUME_GUEST;
801 }
802
Alexander Graf180a34d2010-01-15 14:49:11 +0100803#ifdef DEBUG_EXT
804 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
805#endif
806
807 current->thread.regs->msr |= msr;
808
809 switch (msr) {
810 case MSR_FP:
811 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
812 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
813
814 t->fpscr.val = vcpu->arch.fpscr;
815 t->fpexc_mode = 0;
816 kvmppc_load_up_fpu();
817 break;
818 case MSR_VEC:
819#ifdef CONFIG_ALTIVEC
820 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
821 t->vscr = vcpu->arch.vscr;
822 t->vrsave = -1;
823 kvmppc_load_up_altivec();
824#endif
825 break;
826 case MSR_VSX:
827#ifdef CONFIG_VSX
828 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
829 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
830 kvmppc_load_up_vsx();
831#endif
832 break;
833 default:
834 BUG();
835 }
836
837 vcpu->arch.guest_owned_ext |= msr;
838
Alexander Grafa76f8492010-01-15 14:49:14 +0100839 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100840
841 return RESUME_GUEST;
842}
843
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000844int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
845 unsigned int exit_nr)
846{
847 int r = RESUME_HOST;
848
849 vcpu->stat.sum_exits++;
850
851 run->exit_reason = KVM_EXIT_UNKNOWN;
852 run->ready_for_interrupt_injection = 1;
853#ifdef EXIT_DEBUG
854 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 +0200855 exit_nr, kvmppc_get_pc(vcpu), kvmppc_get_fault_dar(vcpu),
856 kvmppc_get_dec(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000857#elif defined (EXIT_DEBUG_SIMPLE)
858 if ((exit_nr != 0x900) && (exit_nr != 0x500))
859 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200860 exit_nr, kvmppc_get_pc(vcpu), kvmppc_get_fault_dar(vcpu),
Alexander Graf666e7252010-07-29 14:47:43 +0200861 vcpu->arch.shared->msr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000862#endif
863 kvm_resched(vcpu);
864 switch (exit_nr) {
865 case BOOK3S_INTERRUPT_INST_STORAGE:
866 vcpu->stat.pf_instruc++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200867
868#ifdef CONFIG_PPC_BOOK3S_32
869 /* We set segments as unused segments when invalidating them. So
870 * treat the respective fault as segment fault. */
871 if (to_svcpu(vcpu)->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT]
872 == SR_INVALID) {
873 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
874 r = RESUME_GUEST;
875 break;
876 }
877#endif
878
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000879 /* only care about PTEG not found errors, but leave NX alone */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200880 if (to_svcpu(vcpu)->shadow_srr1 & 0x40000000) {
881 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000882 vcpu->stat.sp_instruc++;
883 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
884 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
885 /*
886 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
887 * so we can't use the NX bit inside the guest. Let's cross our fingers,
888 * that no guest that needs the dcbz hack does NX.
889 */
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200890 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100891 r = RESUME_GUEST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000892 } else {
Alexander Graf666e7252010-07-29 14:47:43 +0200893 vcpu->arch.shared->msr |=
894 to_svcpu(vcpu)->shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000895 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200896 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000897 r = RESUME_GUEST;
898 }
899 break;
900 case BOOK3S_INTERRUPT_DATA_STORAGE:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200901 {
902 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000903 vcpu->stat.pf_storage++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200904
905#ifdef CONFIG_PPC_BOOK3S_32
906 /* We set segments as unused segments when invalidating them. So
907 * treat the respective fault as segment fault. */
908 if ((to_svcpu(vcpu)->sr[dar >> SID_SHIFT]) == SR_INVALID) {
909 kvmppc_mmu_map_segment(vcpu, dar);
910 r = RESUME_GUEST;
911 break;
912 }
913#endif
914
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000915 /* The only case we need to handle is missing shadow PTEs */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200916 if (to_svcpu(vcpu)->fault_dsisr & DSISR_NOHPTE) {
917 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000918 } else {
Alexander Graf5e030182010-07-29 14:47:45 +0200919 vcpu->arch.shared->dar = dar;
Alexander Grafd562de42010-07-29 14:47:44 +0200920 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000921 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Graf5e030182010-07-29 14:47:45 +0200922 kvmppc_mmu_pte_flush(vcpu, dar, ~0xFFFUL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000923 r = RESUME_GUEST;
924 }
925 break;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200926 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000927 case BOOK3S_INTERRUPT_DATA_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200928 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
Alexander Graf5e030182010-07-29 14:47:45 +0200929 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000930 kvmppc_book3s_queue_irqprio(vcpu,
931 BOOK3S_INTERRUPT_DATA_SEGMENT);
932 }
933 r = RESUME_GUEST;
934 break;
935 case BOOK3S_INTERRUPT_INST_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200936 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000937 kvmppc_book3s_queue_irqprio(vcpu,
938 BOOK3S_INTERRUPT_INST_SEGMENT);
939 }
940 r = RESUME_GUEST;
941 break;
942 /* We're good on these - the host merely wanted to get our attention */
943 case BOOK3S_INTERRUPT_DECREMENTER:
944 vcpu->stat.dec_exits++;
945 r = RESUME_GUEST;
946 break;
947 case BOOK3S_INTERRUPT_EXTERNAL:
948 vcpu->stat.ext_intr_exits++;
949 r = RESUME_GUEST;
950 break;
Alexander Graf7fdaec92010-04-20 02:49:47 +0200951 case BOOK3S_INTERRUPT_PERFMON:
952 r = RESUME_GUEST;
953 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000954 case BOOK3S_INTERRUPT_PROGRAM:
955 {
956 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100957 ulong flags;
958
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100959program_interrupt:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200960 flags = to_svcpu(vcpu)->shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000961
Alexander Graf666e7252010-07-29 14:47:43 +0200962 if (vcpu->arch.shared->msr & MSR_PR) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000963#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +0200964 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 +0000965#endif
Alexander Grafc7f38f42010-04-16 00:11:40 +0200966 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000967 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100968 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000969 r = RESUME_GUEST;
970 break;
971 }
972 }
973
974 vcpu->stat.emulated_inst_exits++;
975 er = kvmppc_emulate_instruction(run, vcpu);
976 switch (er) {
977 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100978 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000979 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100980 case EMULATE_AGAIN:
981 r = RESUME_GUEST;
982 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000983 case EMULATE_FAIL:
984 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200985 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100986 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000987 r = RESUME_GUEST;
988 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +0100989 case EMULATE_DO_MMIO:
990 run->exit_reason = KVM_EXIT_MMIO;
991 r = RESUME_HOST_NV;
992 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000993 default:
994 BUG();
995 }
996 break;
997 }
998 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafad0a0482010-03-24 21:48:30 +0100999 if (vcpu->arch.osi_enabled &&
1000 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
1001 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
Alexander Graf2a342ed2010-07-29 14:47:48 +02001002 /* MOL hypercalls */
Alexander Grafad0a0482010-03-24 21:48:30 +01001003 u64 *gprs = run->osi.gprs;
1004 int i;
1005
1006 run->exit_reason = KVM_EXIT_OSI;
1007 for (i = 0; i < 32; i++)
1008 gprs[i] = kvmppc_get_gpr(vcpu, i);
1009 vcpu->arch.osi_needed = 1;
1010 r = RESUME_HOST_NV;
Alexander Graf2a342ed2010-07-29 14:47:48 +02001011 } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
1012 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
1013 /* KVM PV hypercalls */
1014 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1015 r = RESUME_GUEST;
Alexander Grafad0a0482010-03-24 21:48:30 +01001016 } else {
Alexander Graf2a342ed2010-07-29 14:47:48 +02001017 /* Guest syscalls */
Alexander Grafad0a0482010-03-24 21:48:30 +01001018 vcpu->stat.syscall_exits++;
1019 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1020 r = RESUME_GUEST;
1021 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001022 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001023 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001024 case BOOK3S_INTERRUPT_ALTIVEC:
1025 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +01001026 {
1027 int ext_msr = 0;
1028
1029 switch (exit_nr) {
1030 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
1031 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
1032 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
1033 }
1034
1035 switch (kvmppc_check_ext(vcpu, exit_nr)) {
1036 case EMULATE_DONE:
1037 /* everything ok - let's enable the ext */
1038 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
1039 break;
1040 case EMULATE_FAIL:
1041 /* we need to emulate this instruction */
1042 goto program_interrupt;
1043 break;
1044 default:
1045 /* nothing to worry about - go again */
1046 break;
1047 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001048 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +01001049 }
Alexander Grafca7f4202010-03-24 21:48:28 +01001050 case BOOK3S_INTERRUPT_ALIGNMENT:
1051 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
Alexander Grafd562de42010-07-29 14:47:44 +02001052 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001053 kvmppc_get_last_inst(vcpu));
Alexander Graf5e030182010-07-29 14:47:45 +02001054 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001055 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +01001056 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1057 }
1058 r = RESUME_GUEST;
1059 break;
Alexander Graf180a34d2010-01-15 14:49:11 +01001060 case BOOK3S_INTERRUPT_MACHINE_CHECK:
1061 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001062 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1063 r = RESUME_GUEST;
1064 break;
1065 default:
1066 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +01001067 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +02001068 exit_nr, kvmppc_get_pc(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001069 r = RESUME_HOST;
1070 BUG();
1071 break;
1072 }
1073
1074
1075 if (!(r & RESUME_HOST)) {
1076 /* To avoid clobbering exit_reason, only check for signals if
1077 * we aren't already exiting to userspace for some other
1078 * reason. */
1079 if (signal_pending(current)) {
1080#ifdef EXIT_DEBUG
1081 printk(KERN_EMERG "KVM: Going back to host\n");
1082#endif
1083 vcpu->stat.signal_exits++;
1084 run->exit_reason = KVM_EXIT_INTR;
1085 r = -EINTR;
1086 } else {
1087 /* In case an interrupt came in that was triggered
1088 * from userspace (like DEC), we need to check what
1089 * to inject now! */
1090 kvmppc_core_deliver_interrupts(vcpu);
1091 }
1092 }
1093
1094#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +02001095 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 +00001096#endif
1097
1098 return r;
1099}
1100
1101int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1102{
1103 return 0;
1104}
1105
1106int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1107{
1108 int i;
1109
Alexander Grafc7f38f42010-04-16 00:11:40 +02001110 regs->pc = kvmppc_get_pc(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001111 regs->cr = kvmppc_get_cr(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001112 regs->ctr = kvmppc_get_ctr(vcpu);
1113 regs->lr = kvmppc_get_lr(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001114 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001115 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +02001116 regs->srr0 = vcpu->arch.shared->srr0;
1117 regs->srr1 = vcpu->arch.shared->srr1;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001118 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001119 regs->sprg0 = vcpu->arch.shared->sprg0;
1120 regs->sprg1 = vcpu->arch.shared->sprg1;
1121 regs->sprg2 = vcpu->arch.shared->sprg2;
1122 regs->sprg3 = vcpu->arch.shared->sprg3;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001123 regs->sprg5 = vcpu->arch.sprg4;
1124 regs->sprg6 = vcpu->arch.sprg5;
1125 regs->sprg7 = vcpu->arch.sprg6;
1126
1127 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001128 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001129
1130 return 0;
1131}
1132
1133int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1134{
1135 int i;
1136
Alexander Grafc7f38f42010-04-16 00:11:40 +02001137 kvmppc_set_pc(vcpu, regs->pc);
Alexander Graf992b5b22010-01-08 02:58:02 +01001138 kvmppc_set_cr(vcpu, regs->cr);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001139 kvmppc_set_ctr(vcpu, regs->ctr);
1140 kvmppc_set_lr(vcpu, regs->lr);
Alexander Graf992b5b22010-01-08 02:58:02 +01001141 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001142 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +02001143 vcpu->arch.shared->srr0 = regs->srr0;
1144 vcpu->arch.shared->srr1 = regs->srr1;
Alexander Grafa73a9592010-07-29 14:47:47 +02001145 vcpu->arch.shared->sprg0 = regs->sprg0;
1146 vcpu->arch.shared->sprg1 = regs->sprg1;
1147 vcpu->arch.shared->sprg2 = regs->sprg2;
1148 vcpu->arch.shared->sprg3 = regs->sprg3;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001149 vcpu->arch.sprg5 = regs->sprg4;
1150 vcpu->arch.sprg6 = regs->sprg5;
1151 vcpu->arch.sprg7 = regs->sprg6;
1152
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001153 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1154 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001155
1156 return 0;
1157}
1158
1159int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1160 struct kvm_sregs *sregs)
1161{
Alexander Grafe15a1132009-11-30 03:02:02 +00001162 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1163 int i;
1164
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001165 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +00001166
1167 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1168 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1169 for (i = 0; i < 64; i++) {
1170 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
1171 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1172 }
1173 } else {
1174 for (i = 0; i < 16; i++) {
1175 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1176 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1177 }
1178 for (i = 0; i < 8; i++) {
1179 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1180 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1181 }
1182 }
Avi Kivity98001d82010-05-13 11:05:49 +03001183
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001184 return 0;
1185}
1186
1187int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1188 struct kvm_sregs *sregs)
1189{
Alexander Grafe15a1132009-11-30 03:02:02 +00001190 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1191 int i;
1192
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001193 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001194
1195 vcpu3s->sdr1 = sregs->u.s.sdr1;
1196 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1197 for (i = 0; i < 64; i++) {
1198 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1199 sregs->u.s.ppc64.slb[i].slbe);
1200 }
1201 } else {
1202 for (i = 0; i < 16; i++) {
1203 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1204 }
1205 for (i = 0; i < 8; i++) {
1206 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1207 (u32)sregs->u.s.ppc32.ibat[i]);
1208 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1209 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1210 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1211 (u32)sregs->u.s.ppc32.dbat[i]);
1212 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1213 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1214 }
1215 }
1216
1217 /* Flush the MMU after messing with the segments */
1218 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Avi Kivity98001d82010-05-13 11:05:49 +03001219
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001220 return 0;
1221}
1222
1223int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1224{
1225 return -ENOTSUPP;
1226}
1227
1228int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1229{
1230 return -ENOTSUPP;
1231}
1232
1233int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1234 struct kvm_translation *tr)
1235{
1236 return 0;
1237}
1238
1239/*
1240 * Get (and clear) the dirty memory log for a memory slot.
1241 */
1242int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1243 struct kvm_dirty_log *log)
1244{
1245 struct kvm_memory_slot *memslot;
1246 struct kvm_vcpu *vcpu;
1247 ulong ga, ga_end;
1248 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001249 int r;
1250 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001251
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001252 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001253
1254 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1255 if (r)
1256 goto out;
1257
1258 /* If nothing is dirty, don't bother messing with page tables. */
1259 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001260 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001261
1262 ga = memslot->base_gfn << PAGE_SHIFT;
1263 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1264
1265 kvm_for_each_vcpu(n, vcpu, kvm)
1266 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1267
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001268 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001269 memset(memslot->dirty_bitmap, 0, n);
1270 }
1271
1272 r = 0;
1273out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001274 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001275 return r;
1276}
1277
1278int kvmppc_core_check_processor_compat(void)
1279{
1280 return 0;
1281}
1282
1283struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1284{
1285 struct kvmppc_vcpu_book3s *vcpu_book3s;
1286 struct kvm_vcpu *vcpu;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001287 int err = -ENOMEM;
Alexander Grafe8508942010-07-29 14:47:54 +02001288 unsigned long p;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001289
Alexander Graf032c3402010-02-19 12:24:33 +01001290 vcpu_book3s = vmalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Grafc7f38f42010-04-16 00:11:40 +02001291 if (!vcpu_book3s)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001292 goto out;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001293
Alexander Graf7e821d32010-02-22 16:52:08 +01001294 memset(vcpu_book3s, 0, sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001295
Alexander Grafc7f38f42010-04-16 00:11:40 +02001296 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
1297 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
1298 if (!vcpu_book3s->shadow_vcpu)
1299 goto free_vcpu;
1300
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001301 vcpu = &vcpu_book3s->vcpu;
1302 err = kvm_vcpu_init(vcpu, kvm, id);
1303 if (err)
Alexander Grafc7f38f42010-04-16 00:11:40 +02001304 goto free_shadow_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001305
Alexander Grafe8508942010-07-29 14:47:54 +02001306 p = __get_free_page(GFP_KERNEL|__GFP_ZERO);
1307 /* the real shared page fills the last 4k of our page */
1308 vcpu->arch.shared = (void*)(p + PAGE_SIZE - 4096);
1309 if (!p)
Alexander Graf96bc4512010-07-29 14:47:42 +02001310 goto uninit_vcpu;
1311
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001312 vcpu->arch.host_retip = kvm_return_point;
1313 vcpu->arch.host_msr = mfmsr();
Alexander Graf07b09072010-04-16 00:11:53 +02001314#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001315 /* default to book3s_64 (970fx) */
1316 vcpu->arch.pvr = 0x3C0301;
Alexander Graf07b09072010-04-16 00:11:53 +02001317#else
1318 /* default to book3s_32 (750) */
1319 vcpu->arch.pvr = 0x84202;
1320#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001321 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1322 vcpu_book3s->slb_nr = 64;
1323
1324 /* remember where some real-mode handlers are */
1325 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1326 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1327 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf07b09072010-04-16 00:11:53 +02001328#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf021ec9c2010-01-08 02:58:06 +01001329 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf07b09072010-04-16 00:11:53 +02001330#else
1331 vcpu->arch.rmcall = (ulong)kvmppc_rmcall;
1332#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001333
1334 vcpu->arch.shadow_msr = MSR_USER64;
1335
Alexander Graf9cc5e952010-04-16 00:11:45 +02001336 err = kvmppc_mmu_init(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001337 if (err < 0)
Alexander Graf96bc4512010-07-29 14:47:42 +02001338 goto uninit_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001339
1340 return vcpu;
1341
Alexander Graf96bc4512010-07-29 14:47:42 +02001342uninit_vcpu:
1343 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001344free_shadow_vcpu:
1345 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001346free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001347 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001348out:
1349 return ERR_PTR(err);
1350}
1351
1352void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1353{
1354 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1355
Alexander Grafe8508942010-07-29 14:47:54 +02001356 free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001357 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001358 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001359 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001360}
1361
1362extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1363int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1364{
1365 int ret;
Andreas Schwab49f6be82010-05-31 21:59:13 +02001366 double fpr[32][TS_FPRWIDTH];
1367 unsigned int fpscr;
1368 int fpexc_mode;
Alexander Grafa2b07662010-03-24 21:48:31 +01001369#ifdef CONFIG_ALTIVEC
Andreas Schwab49f6be82010-05-31 21:59:13 +02001370 vector128 vr[32];
1371 vector128 vscr;
1372 unsigned long uninitialized_var(vrsave);
1373 int used_vr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001374#endif
1375#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001376 int used_vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001377#endif
Alexander Graf180a34d2010-01-15 14:49:11 +01001378 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001379
1380 /* No need to go into the guest when all we do is going out */
1381 if (signal_pending(current)) {
1382 kvm_run->exit_reason = KVM_EXIT_INTR;
1383 return -EINTR;
1384 }
1385
Alexander Graf180a34d2010-01-15 14:49:11 +01001386 /* Save FPU state in stack */
1387 if (current->thread.regs->msr & MSR_FP)
1388 giveup_fpu(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001389 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
1390 fpscr = current->thread.fpscr.val;
1391 fpexc_mode = current->thread.fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001392
1393#ifdef CONFIG_ALTIVEC
1394 /* Save Altivec state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001395 used_vr = current->thread.used_vr;
1396 if (used_vr) {
Alexander Graf180a34d2010-01-15 14:49:11 +01001397 if (current->thread.regs->msr & MSR_VEC)
1398 giveup_altivec(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001399 memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
1400 vscr = current->thread.vscr;
1401 vrsave = current->thread.vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001402 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001403#endif
1404
1405#ifdef CONFIG_VSX
1406 /* Save VSX state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001407 used_vsr = current->thread.used_vsr;
1408 if (used_vsr && (current->thread.regs->msr & MSR_VSX))
Alexander Graf180a34d2010-01-15 14:49:11 +01001409 __giveup_vsx(current);
Alexander Graf180a34d2010-01-15 14:49:11 +01001410#endif
1411
1412 /* Remember the MSR with disabled extensions */
1413 ext_msr = current->thread.regs->msr;
1414
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001415 /* XXX we get called with irq disabled - change that! */
1416 local_irq_enable();
1417
Alexander Grafd1bab742010-02-19 11:00:35 +01001418 /* Preload FPU if it's enabled */
Alexander Graf666e7252010-07-29 14:47:43 +02001419 if (vcpu->arch.shared->msr & MSR_FP)
Alexander Grafd1bab742010-02-19 11:00:35 +01001420 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1421
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001422 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1423
1424 local_irq_disable();
1425
Alexander Graf180a34d2010-01-15 14:49:11 +01001426 current->thread.regs->msr = ext_msr;
1427
1428 /* Make sure we save the guest FPU/Altivec/VSX state */
1429 kvmppc_giveup_ext(vcpu, MSR_FP);
1430 kvmppc_giveup_ext(vcpu, MSR_VEC);
1431 kvmppc_giveup_ext(vcpu, MSR_VSX);
1432
1433 /* Restore FPU state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001434 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
1435 current->thread.fpscr.val = fpscr;
1436 current->thread.fpexc_mode = fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001437
1438#ifdef CONFIG_ALTIVEC
1439 /* Restore Altivec state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001440 if (used_vr && current->thread.used_vr) {
1441 memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
1442 current->thread.vscr = vscr;
1443 current->thread.vrsave = vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001444 }
Andreas Schwab49f6be82010-05-31 21:59:13 +02001445 current->thread.used_vr = used_vr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001446#endif
1447
1448#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001449 current->thread.used_vsr = used_vsr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001450#endif
1451
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001452 return ret;
1453}
1454
1455static int kvmppc_book3s_init(void)
1456{
Alexander Graffef093b2010-06-30 15:18:46 +02001457 int r;
1458
1459 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
1460 THIS_MODULE);
1461
1462 if (r)
1463 return r;
1464
1465 r = kvmppc_mmu_hpte_sysinit();
1466
1467 return r;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001468}
1469
1470static void kvmppc_book3s_exit(void)
1471{
Alexander Graffef093b2010-06-30 15:18:46 +02001472 kvmppc_mmu_hpte_sysexit();
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001473 kvm_exit();
1474}
1475
1476module_init(kvmppc_book3s_init);
1477module_exit(kvmppc_book3s_exit);