blob: d6227ff0ceae7e8e8a75dbcee7d636ed6c8260f4 [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;
340 unsigned int priority;
341
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000342#ifdef EXIT_DEBUG
343 if (vcpu->arch.pending_exceptions)
344 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
345#endif
346 priority = __ffs(*pending);
Alexander Grafada7ba12010-04-16 00:11:56 +0200347 while (priority < BOOK3S_IRQPRIO_MAX) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100348 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
349 (priority != BOOK3S_IRQPRIO_DECREMENTER)) {
350 /* DEC interrupts get cleared by mtdec */
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000351 clear_bit(priority, &vcpu->arch.pending_exceptions);
352 break;
353 }
354
355 priority = find_next_bit(pending,
356 BITS_PER_BYTE * sizeof(*pending),
357 priority + 1);
358 }
359}
360
361void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
362{
Alexander Grafb83d4a92010-04-20 02:49:54 +0200363 u32 host_pvr;
364
Alexander Grafe15a1132009-11-30 03:02:02 +0000365 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000366 vcpu->arch.pvr = pvr;
Alexander Graf07b09072010-04-16 00:11:53 +0200367#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000368 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
369 kvmppc_mmu_book3s_64_init(vcpu);
370 to_book3s(vcpu)->hior = 0xfff00000;
371 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
Alexander Graf07b09072010-04-16 00:11:53 +0200372 } else
373#endif
374 {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000375 kvmppc_mmu_book3s_32_init(vcpu);
376 to_book3s(vcpu)->hior = 0;
377 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
378 }
379
380 /* If we are in hypervisor level on 970, we can tell the CPU to
381 * treat DCBZ as 32 bytes store */
382 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
383 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
384 !strcmp(cur_cpu_spec->platform, "ppc970"))
385 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
386
Alexander Graf05b0ab12010-03-24 21:48:37 +0100387 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
388 really needs them in a VM on Cell and force disable them. */
389 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
390 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
Alexander Graf07b09072010-04-16 00:11:53 +0200391
392#ifdef CONFIG_PPC_BOOK3S_32
393 /* 32 bit Book3S always has 32 byte dcbz */
394 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
395#endif
Alexander Grafb83d4a92010-04-20 02:49:54 +0200396
397 /* On some CPUs we can execute paired single operations natively */
398 asm ( "mfpvr %0" : "=r"(host_pvr));
399 switch (host_pvr) {
400 case 0x00080200: /* lonestar 2.0 */
401 case 0x00088202: /* lonestar 2.2 */
402 case 0x70000100: /* gekko 1.0 */
403 case 0x00080100: /* gekko 2.0 */
404 case 0x00083203: /* gekko 2.3a */
405 case 0x00083213: /* gekko 2.3b */
406 case 0x00083204: /* gekko 2.4 */
407 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
408 case 0x00087200: /* broadway */
409 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
410 /* Enable HID2.PSE - in case we need it later */
411 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
412 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000413}
414
415/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
416 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
417 * emulate 32 bytes dcbz length.
418 *
419 * The Book3s_64 inventors also realized this case and implemented a special bit
420 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
421 *
422 * My approach here is to patch the dcbz instruction on executing pages.
423 */
424static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
425{
Alexander Graf9fb244a2010-03-24 21:48:32 +0100426 struct page *hpage;
427 u64 hpage_offset;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000428 u32 *page;
429 int i;
430
Alexander Graf9fb244a2010-03-24 21:48:32 +0100431 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
432 if (is_error_page(hpage))
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000433 return;
434
Alexander Graf9fb244a2010-03-24 21:48:32 +0100435 hpage_offset = pte->raddr & ~PAGE_MASK;
436 hpage_offset &= ~0xFFFULL;
437 hpage_offset /= 4;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000438
Alexander Graf9fb244a2010-03-24 21:48:32 +0100439 get_page(hpage);
440 page = kmap_atomic(hpage, KM_USER0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000441
Alexander Graf9fb244a2010-03-24 21:48:32 +0100442 /* patch dcbz into reserved instruction, so we trap */
443 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
444 if ((page[i] & 0xff0007ff) == INS_DCBZ)
445 page[i] &= 0xfffffff7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000446
Alexander Graf9fb244a2010-03-24 21:48:32 +0100447 kunmap_atomic(page, KM_USER0);
448 put_page(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000449}
450
451static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
452 struct kvmppc_pte *pte)
453{
Alexander Graf666e7252010-07-29 14:47:43 +0200454 int relocated = (vcpu->arch.shared->msr & (data ? MSR_DR : MSR_IR));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000455 int r;
456
457 if (relocated) {
458 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
459 } else {
460 pte->eaddr = eaddr;
461 pte->raddr = eaddr & 0xffffffff;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100462 pte->vpage = VSID_REAL | eaddr >> 12;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000463 pte->may_read = true;
464 pte->may_write = true;
465 pte->may_execute = true;
466 r = 0;
467 }
468
469 return r;
470}
471
472static hva_t kvmppc_bad_hva(void)
473{
474 return PAGE_OFFSET;
475}
476
477static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
478 bool read)
479{
480 hva_t hpage;
481
482 if (read && !pte->may_read)
483 goto err;
484
485 if (!read && !pte->may_write)
486 goto err;
487
488 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
489 if (kvm_is_error_hva(hpage))
490 goto err;
491
492 return hpage | (pte->raddr & ~PAGE_MASK);
493err:
494 return kvmppc_bad_hva();
495}
496
Alexander Graf5467a972010-02-19 11:00:38 +0100497int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
498 bool data)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000499{
500 struct kvmppc_pte pte;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000501
502 vcpu->stat.st++;
503
Alexander Graf5467a972010-02-19 11:00:38 +0100504 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
Alexander Graf9fb244a2010-03-24 21:48:32 +0100505 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100506
507 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000508
Alexander Graf9fb244a2010-03-24 21:48:32 +0100509 if (!pte.may_write)
510 return -EPERM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000511
Alexander Graf9fb244a2010-03-24 21:48:32 +0100512 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
513 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000514
Alexander Graf5467a972010-02-19 11:00:38 +0100515 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000516}
517
Alexander Graf5467a972010-02-19 11:00:38 +0100518int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000519 bool data)
520{
521 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100522 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000523
524 vcpu->stat.ld++;
525
Alexander Graf5467a972010-02-19 11:00:38 +0100526 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
527 goto nopte;
528
529 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000530
531 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
532 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100533 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000534
535 if (copy_from_user(ptr, (void __user *)hva, size)) {
536 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100537 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000538 }
539
Alexander Graf5467a972010-02-19 11:00:38 +0100540 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000541
Alexander Graf5467a972010-02-19 11:00:38 +0100542nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000543 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100544mmio:
545 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000546}
547
548static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
549{
550 return kvm_is_visible_gfn(vcpu->kvm, gfn);
551}
552
553int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
554 ulong eaddr, int vec)
555{
556 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
557 int r = RESUME_GUEST;
558 int relocated;
559 int page_found = 0;
560 struct kvmppc_pte pte;
561 bool is_mmio = false;
Alexander Graf666e7252010-07-29 14:47:43 +0200562 bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
563 bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
Alexander Graff7bc74e2010-04-20 02:49:48 +0200564 u64 vsid;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000565
Alexander Graf3eeafd72010-03-24 21:48:17 +0100566 relocated = data ? dr : ir;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000567
568 /* Resolve real address if translation turned on */
569 if (relocated) {
570 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
571 } else {
572 pte.may_execute = true;
573 pte.may_read = true;
574 pte.may_write = true;
575 pte.raddr = eaddr & 0xffffffff;
576 pte.eaddr = eaddr;
577 pte.vpage = eaddr >> 12;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100578 }
579
Alexander Graf666e7252010-07-29 14:47:43 +0200580 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
Alexander Graf3eeafd72010-03-24 21:48:17 +0100581 case 0:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200582 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
Alexander Graf3eeafd72010-03-24 21:48:17 +0100583 break;
584 case MSR_DR:
Alexander Graf3eeafd72010-03-24 21:48:17 +0100585 case MSR_IR:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200586 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
587
Alexander Graf666e7252010-07-29 14:47:43 +0200588 if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
Alexander Graff7bc74e2010-04-20 02:49:48 +0200589 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
590 else
591 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
592 pte.vpage |= vsid;
593
594 if (vsid == -1)
595 page_found = -EINVAL;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100596 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000597 }
598
599 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
600 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
601 /*
602 * If we do the dcbz hack, we have to NX on every execution,
603 * so we can patch the executing code. This renders our guest
604 * NX-less.
605 */
606 pte.may_execute = !data;
607 }
608
609 if (page_found == -ENOENT) {
610 /* Page not found in guest PTE entries */
Alexander Graf5e030182010-07-29 14:47:45 +0200611 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Grafd562de42010-07-29 14:47:44 +0200612 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf666e7252010-07-29 14:47:43 +0200613 vcpu->arch.shared->msr |=
614 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000615 kvmppc_book3s_queue_irqprio(vcpu, vec);
616 } else if (page_found == -EPERM) {
617 /* Storage protection */
Alexander Graf5e030182010-07-29 14:47:45 +0200618 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Grafd562de42010-07-29 14:47:44 +0200619 vcpu->arch.shared->dsisr =
620 to_svcpu(vcpu)->fault_dsisr & ~DSISR_NOHPTE;
621 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
Alexander Graf666e7252010-07-29 14:47:43 +0200622 vcpu->arch.shared->msr |=
623 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000624 kvmppc_book3s_queue_irqprio(vcpu, vec);
625 } else if (page_found == -EINVAL) {
626 /* Page not found in guest SLB */
Alexander Graf5e030182010-07-29 14:47:45 +0200627 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000628 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
629 } else if (!is_mmio &&
630 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
631 /* The guest's PTE is not mapped yet. Map on the host */
632 kvmppc_mmu_map_page(vcpu, &pte);
633 if (data)
634 vcpu->stat.sp_storage++;
635 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
636 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
637 kvmppc_patch_dcbz(vcpu, &pte);
638 } else {
639 /* MMIO */
640 vcpu->stat.mmio_exits++;
641 vcpu->arch.paddr_accessed = pte.raddr;
642 r = kvmppc_emulate_mmio(run, vcpu);
643 if ( r == RESUME_HOST_NV )
644 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000645 }
646
647 return r;
648}
649
Alexander Graf180a34d2010-01-15 14:49:11 +0100650static inline int get_fpr_index(int i)
651{
652#ifdef CONFIG_VSX
653 i *= 2;
654#endif
655 return i;
656}
657
658/* Give up external provider (FPU, Altivec, VSX) */
Alexander Grafaba3bd72010-02-19 11:00:39 +0100659void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
Alexander Graf180a34d2010-01-15 14:49:11 +0100660{
661 struct thread_struct *t = &current->thread;
662 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100663#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100664 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100665#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100666 u64 *thread_fpr = (u64*)t->fpr;
667 int i;
668
669 if (!(vcpu->arch.guest_owned_ext & msr))
670 return;
671
672#ifdef DEBUG_EXT
673 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
674#endif
675
676 switch (msr) {
677 case MSR_FP:
678 giveup_fpu(current);
679 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
680 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
681
682 vcpu->arch.fpscr = t->fpscr.val;
683 break;
684 case MSR_VEC:
685#ifdef CONFIG_ALTIVEC
686 giveup_altivec(current);
687 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
688 vcpu->arch.vscr = t->vscr;
689#endif
690 break;
691 case MSR_VSX:
692#ifdef CONFIG_VSX
693 __giveup_vsx(current);
694 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
695 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
696#endif
697 break;
698 default:
699 BUG();
700 }
701
702 vcpu->arch.guest_owned_ext &= ~msr;
703 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100704 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100705}
706
Alexander Graf89632212010-03-24 21:48:21 +0100707static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100708{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200709 ulong srr0 = kvmppc_get_pc(vcpu);
710 u32 last_inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100711 int ret;
712
Alexander Grafc7f38f42010-04-16 00:11:40 +0200713 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100714 if (ret == -ENOENT) {
Alexander Graf666e7252010-07-29 14:47:43 +0200715 ulong msr = vcpu->arch.shared->msr;
716
717 msr = kvmppc_set_field(msr, 33, 33, 1);
718 msr = kvmppc_set_field(msr, 34, 36, 0);
719 vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100720 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
Alexander Graf89632212010-03-24 21:48:21 +0100721 return EMULATE_AGAIN;
722 }
723
724 return EMULATE_DONE;
725}
726
727static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
728{
729
730 /* Need to do paired single emulation? */
731 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
732 return EMULATE_DONE;
733
734 /* Read out the instruction */
735 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100736 /* Need to emulate */
737 return EMULATE_FAIL;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100738
739 return EMULATE_AGAIN;
740}
741
Alexander Graf180a34d2010-01-15 14:49:11 +0100742/* Handle external providers (FPU, Altivec, VSX) */
743static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
744 ulong msr)
745{
746 struct thread_struct *t = &current->thread;
747 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100748#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100749 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100750#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100751 u64 *thread_fpr = (u64*)t->fpr;
752 int i;
753
Alexander Graf3c402a72010-02-19 11:00:32 +0100754 /* When we have paired singles, we emulate in software */
755 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
756 return RESUME_GUEST;
757
Alexander Graf666e7252010-07-29 14:47:43 +0200758 if (!(vcpu->arch.shared->msr & msr)) {
Alexander Graf180a34d2010-01-15 14:49:11 +0100759 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
760 return RESUME_GUEST;
761 }
762
Alexander Grafc2453692010-03-24 21:48:22 +0100763 /* We already own the ext */
764 if (vcpu->arch.guest_owned_ext & msr) {
765 return RESUME_GUEST;
766 }
767
Alexander Graf180a34d2010-01-15 14:49:11 +0100768#ifdef DEBUG_EXT
769 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
770#endif
771
772 current->thread.regs->msr |= msr;
773
774 switch (msr) {
775 case MSR_FP:
776 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
777 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
778
779 t->fpscr.val = vcpu->arch.fpscr;
780 t->fpexc_mode = 0;
781 kvmppc_load_up_fpu();
782 break;
783 case MSR_VEC:
784#ifdef CONFIG_ALTIVEC
785 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
786 t->vscr = vcpu->arch.vscr;
787 t->vrsave = -1;
788 kvmppc_load_up_altivec();
789#endif
790 break;
791 case MSR_VSX:
792#ifdef CONFIG_VSX
793 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
794 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
795 kvmppc_load_up_vsx();
796#endif
797 break;
798 default:
799 BUG();
800 }
801
802 vcpu->arch.guest_owned_ext |= msr;
803
Alexander Grafa76f8492010-01-15 14:49:14 +0100804 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100805
806 return RESUME_GUEST;
807}
808
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000809int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
810 unsigned int exit_nr)
811{
812 int r = RESUME_HOST;
813
814 vcpu->stat.sum_exits++;
815
816 run->exit_reason = KVM_EXIT_UNKNOWN;
817 run->ready_for_interrupt_injection = 1;
818#ifdef EXIT_DEBUG
819 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 +0200820 exit_nr, kvmppc_get_pc(vcpu), kvmppc_get_fault_dar(vcpu),
821 kvmppc_get_dec(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000822#elif defined (EXIT_DEBUG_SIMPLE)
823 if ((exit_nr != 0x900) && (exit_nr != 0x500))
824 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200825 exit_nr, kvmppc_get_pc(vcpu), kvmppc_get_fault_dar(vcpu),
Alexander Graf666e7252010-07-29 14:47:43 +0200826 vcpu->arch.shared->msr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000827#endif
828 kvm_resched(vcpu);
829 switch (exit_nr) {
830 case BOOK3S_INTERRUPT_INST_STORAGE:
831 vcpu->stat.pf_instruc++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200832
833#ifdef CONFIG_PPC_BOOK3S_32
834 /* We set segments as unused segments when invalidating them. So
835 * treat the respective fault as segment fault. */
836 if (to_svcpu(vcpu)->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT]
837 == SR_INVALID) {
838 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
839 r = RESUME_GUEST;
840 break;
841 }
842#endif
843
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000844 /* only care about PTEG not found errors, but leave NX alone */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200845 if (to_svcpu(vcpu)->shadow_srr1 & 0x40000000) {
846 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000847 vcpu->stat.sp_instruc++;
848 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
849 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
850 /*
851 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
852 * so we can't use the NX bit inside the guest. Let's cross our fingers,
853 * that no guest that needs the dcbz hack does NX.
854 */
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200855 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100856 r = RESUME_GUEST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000857 } else {
Alexander Graf666e7252010-07-29 14:47:43 +0200858 vcpu->arch.shared->msr |=
859 to_svcpu(vcpu)->shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000860 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200861 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000862 r = RESUME_GUEST;
863 }
864 break;
865 case BOOK3S_INTERRUPT_DATA_STORAGE:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200866 {
867 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000868 vcpu->stat.pf_storage++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200869
870#ifdef CONFIG_PPC_BOOK3S_32
871 /* We set segments as unused segments when invalidating them. So
872 * treat the respective fault as segment fault. */
873 if ((to_svcpu(vcpu)->sr[dar >> SID_SHIFT]) == SR_INVALID) {
874 kvmppc_mmu_map_segment(vcpu, dar);
875 r = RESUME_GUEST;
876 break;
877 }
878#endif
879
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000880 /* The only case we need to handle is missing shadow PTEs */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200881 if (to_svcpu(vcpu)->fault_dsisr & DSISR_NOHPTE) {
882 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000883 } else {
Alexander Graf5e030182010-07-29 14:47:45 +0200884 vcpu->arch.shared->dar = dar;
Alexander Grafd562de42010-07-29 14:47:44 +0200885 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000886 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Graf5e030182010-07-29 14:47:45 +0200887 kvmppc_mmu_pte_flush(vcpu, dar, ~0xFFFUL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000888 r = RESUME_GUEST;
889 }
890 break;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200891 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000892 case BOOK3S_INTERRUPT_DATA_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200893 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
Alexander Graf5e030182010-07-29 14:47:45 +0200894 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000895 kvmppc_book3s_queue_irqprio(vcpu,
896 BOOK3S_INTERRUPT_DATA_SEGMENT);
897 }
898 r = RESUME_GUEST;
899 break;
900 case BOOK3S_INTERRUPT_INST_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200901 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000902 kvmppc_book3s_queue_irqprio(vcpu,
903 BOOK3S_INTERRUPT_INST_SEGMENT);
904 }
905 r = RESUME_GUEST;
906 break;
907 /* We're good on these - the host merely wanted to get our attention */
908 case BOOK3S_INTERRUPT_DECREMENTER:
909 vcpu->stat.dec_exits++;
910 r = RESUME_GUEST;
911 break;
912 case BOOK3S_INTERRUPT_EXTERNAL:
913 vcpu->stat.ext_intr_exits++;
914 r = RESUME_GUEST;
915 break;
Alexander Graf7fdaec92010-04-20 02:49:47 +0200916 case BOOK3S_INTERRUPT_PERFMON:
917 r = RESUME_GUEST;
918 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000919 case BOOK3S_INTERRUPT_PROGRAM:
920 {
921 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100922 ulong flags;
923
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100924program_interrupt:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200925 flags = to_svcpu(vcpu)->shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000926
Alexander Graf666e7252010-07-29 14:47:43 +0200927 if (vcpu->arch.shared->msr & MSR_PR) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000928#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +0200929 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 +0000930#endif
Alexander Grafc7f38f42010-04-16 00:11:40 +0200931 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000932 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100933 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000934 r = RESUME_GUEST;
935 break;
936 }
937 }
938
939 vcpu->stat.emulated_inst_exits++;
940 er = kvmppc_emulate_instruction(run, vcpu);
941 switch (er) {
942 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100943 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000944 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100945 case EMULATE_AGAIN:
946 r = RESUME_GUEST;
947 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000948 case EMULATE_FAIL:
949 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200950 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100951 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000952 r = RESUME_GUEST;
953 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +0100954 case EMULATE_DO_MMIO:
955 run->exit_reason = KVM_EXIT_MMIO;
956 r = RESUME_HOST_NV;
957 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000958 default:
959 BUG();
960 }
961 break;
962 }
963 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafad0a0482010-03-24 21:48:30 +0100964 if (vcpu->arch.osi_enabled &&
965 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
966 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
Alexander Graf2a342ed2010-07-29 14:47:48 +0200967 /* MOL hypercalls */
Alexander Grafad0a0482010-03-24 21:48:30 +0100968 u64 *gprs = run->osi.gprs;
969 int i;
970
971 run->exit_reason = KVM_EXIT_OSI;
972 for (i = 0; i < 32; i++)
973 gprs[i] = kvmppc_get_gpr(vcpu, i);
974 vcpu->arch.osi_needed = 1;
975 r = RESUME_HOST_NV;
Alexander Graf2a342ed2010-07-29 14:47:48 +0200976 } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
977 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
978 /* KVM PV hypercalls */
979 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
980 r = RESUME_GUEST;
Alexander Grafad0a0482010-03-24 21:48:30 +0100981 } else {
Alexander Graf2a342ed2010-07-29 14:47:48 +0200982 /* Guest syscalls */
Alexander Grafad0a0482010-03-24 21:48:30 +0100983 vcpu->stat.syscall_exits++;
984 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
985 r = RESUME_GUEST;
986 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000987 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000988 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000989 case BOOK3S_INTERRUPT_ALTIVEC:
990 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100991 {
992 int ext_msr = 0;
993
994 switch (exit_nr) {
995 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
996 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
997 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
998 }
999
1000 switch (kvmppc_check_ext(vcpu, exit_nr)) {
1001 case EMULATE_DONE:
1002 /* everything ok - let's enable the ext */
1003 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
1004 break;
1005 case EMULATE_FAIL:
1006 /* we need to emulate this instruction */
1007 goto program_interrupt;
1008 break;
1009 default:
1010 /* nothing to worry about - go again */
1011 break;
1012 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001013 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +01001014 }
Alexander Grafca7f4202010-03-24 21:48:28 +01001015 case BOOK3S_INTERRUPT_ALIGNMENT:
1016 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
Alexander Grafd562de42010-07-29 14:47:44 +02001017 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001018 kvmppc_get_last_inst(vcpu));
Alexander Graf5e030182010-07-29 14:47:45 +02001019 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001020 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +01001021 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1022 }
1023 r = RESUME_GUEST;
1024 break;
Alexander Graf180a34d2010-01-15 14:49:11 +01001025 case BOOK3S_INTERRUPT_MACHINE_CHECK:
1026 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001027 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1028 r = RESUME_GUEST;
1029 break;
1030 default:
1031 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +01001032 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +02001033 exit_nr, kvmppc_get_pc(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001034 r = RESUME_HOST;
1035 BUG();
1036 break;
1037 }
1038
1039
1040 if (!(r & RESUME_HOST)) {
1041 /* To avoid clobbering exit_reason, only check for signals if
1042 * we aren't already exiting to userspace for some other
1043 * reason. */
1044 if (signal_pending(current)) {
1045#ifdef EXIT_DEBUG
1046 printk(KERN_EMERG "KVM: Going back to host\n");
1047#endif
1048 vcpu->stat.signal_exits++;
1049 run->exit_reason = KVM_EXIT_INTR;
1050 r = -EINTR;
1051 } else {
1052 /* In case an interrupt came in that was triggered
1053 * from userspace (like DEC), we need to check what
1054 * to inject now! */
1055 kvmppc_core_deliver_interrupts(vcpu);
1056 }
1057 }
1058
1059#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +02001060 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 +00001061#endif
1062
1063 return r;
1064}
1065
1066int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1067{
1068 return 0;
1069}
1070
1071int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1072{
1073 int i;
1074
Alexander Grafc7f38f42010-04-16 00:11:40 +02001075 regs->pc = kvmppc_get_pc(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001076 regs->cr = kvmppc_get_cr(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001077 regs->ctr = kvmppc_get_ctr(vcpu);
1078 regs->lr = kvmppc_get_lr(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001079 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001080 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +02001081 regs->srr0 = vcpu->arch.shared->srr0;
1082 regs->srr1 = vcpu->arch.shared->srr1;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001083 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001084 regs->sprg0 = vcpu->arch.shared->sprg0;
1085 regs->sprg1 = vcpu->arch.shared->sprg1;
1086 regs->sprg2 = vcpu->arch.shared->sprg2;
1087 regs->sprg3 = vcpu->arch.shared->sprg3;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001088 regs->sprg5 = vcpu->arch.sprg4;
1089 regs->sprg6 = vcpu->arch.sprg5;
1090 regs->sprg7 = vcpu->arch.sprg6;
1091
1092 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001093 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001094
1095 return 0;
1096}
1097
1098int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1099{
1100 int i;
1101
Alexander Grafc7f38f42010-04-16 00:11:40 +02001102 kvmppc_set_pc(vcpu, regs->pc);
Alexander Graf992b5b22010-01-08 02:58:02 +01001103 kvmppc_set_cr(vcpu, regs->cr);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001104 kvmppc_set_ctr(vcpu, regs->ctr);
1105 kvmppc_set_lr(vcpu, regs->lr);
Alexander Graf992b5b22010-01-08 02:58:02 +01001106 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001107 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +02001108 vcpu->arch.shared->srr0 = regs->srr0;
1109 vcpu->arch.shared->srr1 = regs->srr1;
Alexander Grafa73a9592010-07-29 14:47:47 +02001110 vcpu->arch.shared->sprg0 = regs->sprg0;
1111 vcpu->arch.shared->sprg1 = regs->sprg1;
1112 vcpu->arch.shared->sprg2 = regs->sprg2;
1113 vcpu->arch.shared->sprg3 = regs->sprg3;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001114 vcpu->arch.sprg5 = regs->sprg4;
1115 vcpu->arch.sprg6 = regs->sprg5;
1116 vcpu->arch.sprg7 = regs->sprg6;
1117
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001118 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1119 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001120
1121 return 0;
1122}
1123
1124int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1125 struct kvm_sregs *sregs)
1126{
Alexander Grafe15a1132009-11-30 03:02:02 +00001127 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1128 int i;
1129
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001130 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +00001131
1132 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1133 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1134 for (i = 0; i < 64; i++) {
1135 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
1136 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1137 }
1138 } else {
1139 for (i = 0; i < 16; i++) {
1140 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1141 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1142 }
1143 for (i = 0; i < 8; i++) {
1144 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1145 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1146 }
1147 }
Avi Kivity98001d82010-05-13 11:05:49 +03001148
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001149 return 0;
1150}
1151
1152int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1153 struct kvm_sregs *sregs)
1154{
Alexander Grafe15a1132009-11-30 03:02:02 +00001155 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1156 int i;
1157
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001158 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001159
1160 vcpu3s->sdr1 = sregs->u.s.sdr1;
1161 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1162 for (i = 0; i < 64; i++) {
1163 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1164 sregs->u.s.ppc64.slb[i].slbe);
1165 }
1166 } else {
1167 for (i = 0; i < 16; i++) {
1168 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1169 }
1170 for (i = 0; i < 8; i++) {
1171 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1172 (u32)sregs->u.s.ppc32.ibat[i]);
1173 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1174 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1175 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1176 (u32)sregs->u.s.ppc32.dbat[i]);
1177 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1178 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1179 }
1180 }
1181
1182 /* Flush the MMU after messing with the segments */
1183 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Avi Kivity98001d82010-05-13 11:05:49 +03001184
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001185 return 0;
1186}
1187
1188int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1189{
1190 return -ENOTSUPP;
1191}
1192
1193int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1194{
1195 return -ENOTSUPP;
1196}
1197
1198int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1199 struct kvm_translation *tr)
1200{
1201 return 0;
1202}
1203
1204/*
1205 * Get (and clear) the dirty memory log for a memory slot.
1206 */
1207int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1208 struct kvm_dirty_log *log)
1209{
1210 struct kvm_memory_slot *memslot;
1211 struct kvm_vcpu *vcpu;
1212 ulong ga, ga_end;
1213 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001214 int r;
1215 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001216
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001217 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001218
1219 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1220 if (r)
1221 goto out;
1222
1223 /* If nothing is dirty, don't bother messing with page tables. */
1224 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001225 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001226
1227 ga = memslot->base_gfn << PAGE_SHIFT;
1228 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1229
1230 kvm_for_each_vcpu(n, vcpu, kvm)
1231 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1232
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001233 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001234 memset(memslot->dirty_bitmap, 0, n);
1235 }
1236
1237 r = 0;
1238out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001239 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001240 return r;
1241}
1242
1243int kvmppc_core_check_processor_compat(void)
1244{
1245 return 0;
1246}
1247
1248struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1249{
1250 struct kvmppc_vcpu_book3s *vcpu_book3s;
1251 struct kvm_vcpu *vcpu;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001252 int err = -ENOMEM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001253
Alexander Graf032c3402010-02-19 12:24:33 +01001254 vcpu_book3s = vmalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Grafc7f38f42010-04-16 00:11:40 +02001255 if (!vcpu_book3s)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001256 goto out;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001257
Alexander Graf7e821d32010-02-22 16:52:08 +01001258 memset(vcpu_book3s, 0, sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001259
Alexander Grafc7f38f42010-04-16 00:11:40 +02001260 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
1261 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
1262 if (!vcpu_book3s->shadow_vcpu)
1263 goto free_vcpu;
1264
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001265 vcpu = &vcpu_book3s->vcpu;
1266 err = kvm_vcpu_init(vcpu, kvm, id);
1267 if (err)
Alexander Grafc7f38f42010-04-16 00:11:40 +02001268 goto free_shadow_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001269
Alexander Graf96bc4512010-07-29 14:47:42 +02001270 vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO);
1271 if (!vcpu->arch.shared)
1272 goto uninit_vcpu;
1273
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001274 vcpu->arch.host_retip = kvm_return_point;
1275 vcpu->arch.host_msr = mfmsr();
Alexander Graf07b09072010-04-16 00:11:53 +02001276#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001277 /* default to book3s_64 (970fx) */
1278 vcpu->arch.pvr = 0x3C0301;
Alexander Graf07b09072010-04-16 00:11:53 +02001279#else
1280 /* default to book3s_32 (750) */
1281 vcpu->arch.pvr = 0x84202;
1282#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001283 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1284 vcpu_book3s->slb_nr = 64;
1285
1286 /* remember where some real-mode handlers are */
1287 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1288 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1289 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf07b09072010-04-16 00:11:53 +02001290#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf021ec9c2010-01-08 02:58:06 +01001291 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf07b09072010-04-16 00:11:53 +02001292#else
1293 vcpu->arch.rmcall = (ulong)kvmppc_rmcall;
1294#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001295
1296 vcpu->arch.shadow_msr = MSR_USER64;
1297
Alexander Graf9cc5e952010-04-16 00:11:45 +02001298 err = kvmppc_mmu_init(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001299 if (err < 0)
Alexander Graf96bc4512010-07-29 14:47:42 +02001300 goto uninit_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001301
1302 return vcpu;
1303
Alexander Graf96bc4512010-07-29 14:47:42 +02001304uninit_vcpu:
1305 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001306free_shadow_vcpu:
1307 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001308free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001309 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001310out:
1311 return ERR_PTR(err);
1312}
1313
1314void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1315{
1316 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1317
Alexander Graf96bc4512010-07-29 14:47:42 +02001318 free_page((unsigned long)vcpu->arch.shared);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001319 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001320 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001321 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001322}
1323
1324extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1325int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1326{
1327 int ret;
Andreas Schwab49f6be82010-05-31 21:59:13 +02001328 double fpr[32][TS_FPRWIDTH];
1329 unsigned int fpscr;
1330 int fpexc_mode;
Alexander Grafa2b07662010-03-24 21:48:31 +01001331#ifdef CONFIG_ALTIVEC
Andreas Schwab49f6be82010-05-31 21:59:13 +02001332 vector128 vr[32];
1333 vector128 vscr;
1334 unsigned long uninitialized_var(vrsave);
1335 int used_vr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001336#endif
1337#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001338 int used_vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001339#endif
Alexander Graf180a34d2010-01-15 14:49:11 +01001340 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001341
1342 /* No need to go into the guest when all we do is going out */
1343 if (signal_pending(current)) {
1344 kvm_run->exit_reason = KVM_EXIT_INTR;
1345 return -EINTR;
1346 }
1347
Alexander Graf180a34d2010-01-15 14:49:11 +01001348 /* Save FPU state in stack */
1349 if (current->thread.regs->msr & MSR_FP)
1350 giveup_fpu(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001351 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
1352 fpscr = current->thread.fpscr.val;
1353 fpexc_mode = current->thread.fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001354
1355#ifdef CONFIG_ALTIVEC
1356 /* Save Altivec state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001357 used_vr = current->thread.used_vr;
1358 if (used_vr) {
Alexander Graf180a34d2010-01-15 14:49:11 +01001359 if (current->thread.regs->msr & MSR_VEC)
1360 giveup_altivec(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001361 memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
1362 vscr = current->thread.vscr;
1363 vrsave = current->thread.vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001364 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001365#endif
1366
1367#ifdef CONFIG_VSX
1368 /* Save VSX state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001369 used_vsr = current->thread.used_vsr;
1370 if (used_vsr && (current->thread.regs->msr & MSR_VSX))
Alexander Graf180a34d2010-01-15 14:49:11 +01001371 __giveup_vsx(current);
Alexander Graf180a34d2010-01-15 14:49:11 +01001372#endif
1373
1374 /* Remember the MSR with disabled extensions */
1375 ext_msr = current->thread.regs->msr;
1376
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001377 /* XXX we get called with irq disabled - change that! */
1378 local_irq_enable();
1379
Alexander Grafd1bab742010-02-19 11:00:35 +01001380 /* Preload FPU if it's enabled */
Alexander Graf666e7252010-07-29 14:47:43 +02001381 if (vcpu->arch.shared->msr & MSR_FP)
Alexander Grafd1bab742010-02-19 11:00:35 +01001382 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1383
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001384 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1385
1386 local_irq_disable();
1387
Alexander Graf180a34d2010-01-15 14:49:11 +01001388 current->thread.regs->msr = ext_msr;
1389
1390 /* Make sure we save the guest FPU/Altivec/VSX state */
1391 kvmppc_giveup_ext(vcpu, MSR_FP);
1392 kvmppc_giveup_ext(vcpu, MSR_VEC);
1393 kvmppc_giveup_ext(vcpu, MSR_VSX);
1394
1395 /* Restore FPU state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001396 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
1397 current->thread.fpscr.val = fpscr;
1398 current->thread.fpexc_mode = fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001399
1400#ifdef CONFIG_ALTIVEC
1401 /* Restore Altivec state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001402 if (used_vr && current->thread.used_vr) {
1403 memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
1404 current->thread.vscr = vscr;
1405 current->thread.vrsave = vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001406 }
Andreas Schwab49f6be82010-05-31 21:59:13 +02001407 current->thread.used_vr = used_vr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001408#endif
1409
1410#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001411 current->thread.used_vsr = used_vsr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001412#endif
1413
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001414 return ret;
1415}
1416
1417static int kvmppc_book3s_init(void)
1418{
Alexander Graffef093b2010-06-30 15:18:46 +02001419 int r;
1420
1421 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
1422 THIS_MODULE);
1423
1424 if (r)
1425 return r;
1426
1427 r = kvmppc_mmu_hpte_sysinit();
1428
1429 return r;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001430}
1431
1432static void kvmppc_book3s_exit(void)
1433{
Alexander Graffef093b2010-06-30 15:18:46 +02001434 kvmppc_mmu_hpte_sysexit();
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001435 kvm_exit();
1436}
1437
1438module_init(kvmppc_book3s_init);
1439module_exit(kvmppc_book3s_exit);