blob: 5833df7e8ccc6f9dfc19cf76940d6de647ab845f [file] [log] [blame]
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001/*
2 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
3 *
4 * Authors:
5 * Alexander Graf <agraf@suse.de>
6 * Kevin Wolf <mail@kevin-wolf.de>
7 *
8 * Description:
9 * This file is derived from arch/powerpc/kvm/44x.c,
10 * by Hollis Blanchard <hollisb@us.ibm.com>.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License, version 2, as
14 * published by the Free Software Foundation.
15 */
16
17#include <linux/kvm_host.h>
18#include <linux/err.h>
Stephen Rothwell329d20b2010-04-27 15:49:17 +100019#include <linux/slab.h>
Alexander Grafbed1ed92010-08-02 11:06:26 +020020#include "trace.h"
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000021
22#include <asm/reg.h>
23#include <asm/cputable.h>
24#include <asm/cacheflush.h>
25#include <asm/tlbflush.h>
26#include <asm/uaccess.h>
27#include <asm/io.h>
28#include <asm/kvm_ppc.h>
29#include <asm/kvm_book3s.h>
30#include <asm/mmu_context.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/gfp.h>
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000032#include <linux/sched.h>
33#include <linux/vmalloc.h>
Alexander Graf9fb244a2010-03-24 21:48:32 +010034#include <linux/highmem.h>
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000035
36#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
37
38/* #define EXIT_DEBUG */
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 Grafa76f8492010-01-15 14:49:14 +0100108static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
109{
Alexander Graf666e7252010-07-29 14:47:43 +0200110 ulong smsr = vcpu->arch.shared->msr;
111
Alexander Grafa76f8492010-01-15 14:49:14 +0100112 /* Guest MSR values */
Alexander Graf666e7252010-07-29 14:47:43 +0200113 smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE | MSR_DE;
Alexander Grafa76f8492010-01-15 14:49:14 +0100114 /* Process MSR values */
Alexander Graf666e7252010-07-29 14:47:43 +0200115 smsr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | MSR_EE;
Alexander Grafa76f8492010-01-15 14:49:14 +0100116 /* External providers the guest reserved */
Alexander Graf666e7252010-07-29 14:47:43 +0200117 smsr |= (vcpu->arch.shared->msr & vcpu->arch.guest_owned_ext);
Alexander Grafa76f8492010-01-15 14:49:14 +0100118 /* 64-bit Process MSR values */
119#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf666e7252010-07-29 14:47:43 +0200120 smsr |= MSR_ISF | MSR_HV;
Alexander Grafa76f8492010-01-15 14:49:14 +0100121#endif
Alexander Graf666e7252010-07-29 14:47:43 +0200122 vcpu->arch.shadow_msr = smsr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100123}
124
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000125void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
126{
Alexander Graf666e7252010-07-29 14:47:43 +0200127 ulong old_msr = vcpu->arch.shared->msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000128
129#ifdef EXIT_DEBUG
130 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
131#endif
Alexander Grafa76f8492010-01-15 14:49:14 +0100132
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000133 msr &= to_book3s(vcpu)->msr_mask;
Alexander Graf666e7252010-07-29 14:47:43 +0200134 vcpu->arch.shared->msr = msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100135 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000136
Alexander Graf296c19d2010-08-15 08:39:19 +0200137 if (msr & MSR_POW) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000138 if (!vcpu->arch.pending_exceptions) {
139 kvm_vcpu_block(vcpu);
140 vcpu->stat.halt_wakeup++;
Alexander Graf296c19d2010-08-15 08:39:19 +0200141
142 /* Unset POW bit after we woke up */
143 msr &= ~MSR_POW;
144 vcpu->arch.shared->msr = msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000145 }
146 }
147
Alexander Graf666e7252010-07-29 14:47:43 +0200148 if ((vcpu->arch.shared->msr & (MSR_PR|MSR_IR|MSR_DR)) !=
Alexander Graff7bc74e2010-04-20 02:49:48 +0200149 (old_msr & (MSR_PR|MSR_IR|MSR_DR))) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000150 kvmppc_mmu_flush_segments(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +0200151 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
Alexander Graf4cb6b7e2010-08-02 16:08:22 +0200152
153 /* Preload magic page segment when in kernel mode */
154 if (!(msr & MSR_PR) && vcpu->arch.magic_page_pa) {
155 struct kvm_vcpu_arch *a = &vcpu->arch;
156
157 if (msr & MSR_DR)
158 kvmppc_mmu_map_segment(vcpu, a->magic_page_ea);
159 else
160 kvmppc_mmu_map_segment(vcpu, a->magic_page_pa);
161 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000162 }
Alexander Grafd1bab742010-02-19 11:00:35 +0100163
164 /* Preload FPU if it's enabled */
Alexander Graf666e7252010-07-29 14:47:43 +0200165 if (vcpu->arch.shared->msr & MSR_FP)
Alexander Grafd1bab742010-02-19 11:00:35 +0100166 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000167}
168
169void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
170{
Alexander Grafde7906c2010-07-29 14:47:46 +0200171 vcpu->arch.shared->srr0 = kvmppc_get_pc(vcpu);
172 vcpu->arch.shared->srr1 = vcpu->arch.shared->msr | flags;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200173 kvmppc_set_pc(vcpu, to_book3s(vcpu)->hior + vec);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000174 vcpu->arch.mmu.reset_msr(vcpu);
175}
176
Alexander Graf583617b2009-12-21 20:21:23 +0100177static int kvmppc_book3s_vec2irqprio(unsigned int vec)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000178{
179 unsigned int prio;
180
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000181 switch (vec) {
182 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
183 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
184 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
185 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
186 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
187 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
188 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
189 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
190 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
191 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
192 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
193 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
194 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
195 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
196 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
197 default: prio = BOOK3S_IRQPRIO_MAX; break;
198 }
199
Alexander Graf583617b2009-12-21 20:21:23 +0100200 return prio;
201}
202
Alexander Graf7706664d2009-12-21 20:21:24 +0100203static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
204 unsigned int vec)
205{
206 clear_bit(kvmppc_book3s_vec2irqprio(vec),
207 &vcpu->arch.pending_exceptions);
Alexander Graf9ee18b12010-08-05 12:24:40 +0200208
209 if (!vcpu->arch.pending_exceptions)
210 vcpu->arch.shared->int_pending = 0;
Alexander Graf7706664d2009-12-21 20:21:24 +0100211}
212
Alexander Graf583617b2009-12-21 20:21:23 +0100213void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
214{
215 vcpu->stat.queue_intr++;
216
217 set_bit(kvmppc_book3s_vec2irqprio(vec),
218 &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000219#ifdef EXIT_DEBUG
220 printk(KERN_INFO "Queueing interrupt %x\n", vec);
221#endif
222}
223
224
Alexander Graf25a8a022010-01-08 02:58:07 +0100225void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000226{
Alexander Graf25a8a022010-01-08 02:58:07 +0100227 to_book3s(vcpu)->prog_flags = flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000228 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM);
229}
230
231void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
232{
233 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
234}
235
236int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
237{
238 return test_bit(BOOK3S_INTERRUPT_DECREMENTER >> 7, &vcpu->arch.pending_exceptions);
239}
240
Alexander Graf7706664d2009-12-21 20:21:24 +0100241void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
242{
243 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
244}
245
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000246void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
247 struct kvm_interrupt *irq)
248{
249 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
250}
251
Alexander Graf18978762010-03-24 21:48:18 +0100252void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
253 struct kvm_interrupt *irq)
254{
255 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
256}
257
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000258int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
259{
260 int deliver = 1;
261 int vec = 0;
Alexander Graf25a8a022010-01-08 02:58:07 +0100262 ulong flags = 0ULL;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200263 ulong crit_raw = vcpu->arch.shared->critical;
264 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
265 bool crit;
266
267 /* Truncate crit indicators in 32 bit mode */
268 if (!(vcpu->arch.shared->msr & MSR_SF)) {
269 crit_raw &= 0xffffffff;
270 crit_r1 &= 0xffffffff;
271 }
272
273 /* Critical section when crit == r1 */
274 crit = (crit_raw == crit_r1);
275 /* ... and we're in supervisor mode */
276 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000277
278 switch (priority) {
279 case BOOK3S_IRQPRIO_DECREMENTER:
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200280 deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000281 vec = BOOK3S_INTERRUPT_DECREMENTER;
282 break;
283 case BOOK3S_IRQPRIO_EXTERNAL:
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200284 deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000285 vec = BOOK3S_INTERRUPT_EXTERNAL;
286 break;
287 case BOOK3S_IRQPRIO_SYSTEM_RESET:
288 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
289 break;
290 case BOOK3S_IRQPRIO_MACHINE_CHECK:
291 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
292 break;
293 case BOOK3S_IRQPRIO_DATA_STORAGE:
294 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
295 break;
296 case BOOK3S_IRQPRIO_INST_STORAGE:
297 vec = BOOK3S_INTERRUPT_INST_STORAGE;
298 break;
299 case BOOK3S_IRQPRIO_DATA_SEGMENT:
300 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
301 break;
302 case BOOK3S_IRQPRIO_INST_SEGMENT:
303 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
304 break;
305 case BOOK3S_IRQPRIO_ALIGNMENT:
306 vec = BOOK3S_INTERRUPT_ALIGNMENT;
307 break;
308 case BOOK3S_IRQPRIO_PROGRAM:
309 vec = BOOK3S_INTERRUPT_PROGRAM;
Alexander Graf25a8a022010-01-08 02:58:07 +0100310 flags = to_book3s(vcpu)->prog_flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000311 break;
312 case BOOK3S_IRQPRIO_VSX:
313 vec = BOOK3S_INTERRUPT_VSX;
314 break;
315 case BOOK3S_IRQPRIO_ALTIVEC:
316 vec = BOOK3S_INTERRUPT_ALTIVEC;
317 break;
318 case BOOK3S_IRQPRIO_FP_UNAVAIL:
319 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
320 break;
321 case BOOK3S_IRQPRIO_SYSCALL:
322 vec = BOOK3S_INTERRUPT_SYSCALL;
323 break;
324 case BOOK3S_IRQPRIO_DEBUG:
325 vec = BOOK3S_INTERRUPT_TRACE;
326 break;
327 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
328 vec = BOOK3S_INTERRUPT_PERFMON;
329 break;
330 default:
331 deliver = 0;
332 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
333 break;
334 }
335
336#if 0
337 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
338#endif
339
340 if (deliver)
Alexander Graf25a8a022010-01-08 02:58:07 +0100341 kvmppc_inject_interrupt(vcpu, vec, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000342
343 return deliver;
344}
345
346void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
347{
348 unsigned long *pending = &vcpu->arch.pending_exceptions;
Alexander Graf90bba352010-07-29 14:47:51 +0200349 unsigned long old_pending = vcpu->arch.pending_exceptions;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000350 unsigned int priority;
351
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000352#ifdef EXIT_DEBUG
353 if (vcpu->arch.pending_exceptions)
354 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
355#endif
356 priority = __ffs(*pending);
Alexander Grafada7ba12010-04-16 00:11:56 +0200357 while (priority < BOOK3S_IRQPRIO_MAX) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100358 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
359 (priority != BOOK3S_IRQPRIO_DECREMENTER)) {
360 /* DEC interrupts get cleared by mtdec */
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000361 clear_bit(priority, &vcpu->arch.pending_exceptions);
362 break;
363 }
364
365 priority = find_next_bit(pending,
366 BITS_PER_BYTE * sizeof(*pending),
367 priority + 1);
368 }
Alexander Graf90bba352010-07-29 14:47:51 +0200369
370 /* Tell the guest about our interrupt status */
371 if (*pending)
372 vcpu->arch.shared->int_pending = 1;
373 else if (old_pending)
374 vcpu->arch.shared->int_pending = 0;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000375}
376
377void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
378{
Alexander Grafb83d4a92010-04-20 02:49:54 +0200379 u32 host_pvr;
380
Alexander Grafe15a1132009-11-30 03:02:02 +0000381 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000382 vcpu->arch.pvr = pvr;
Alexander Graf07b09072010-04-16 00:11:53 +0200383#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000384 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
385 kvmppc_mmu_book3s_64_init(vcpu);
386 to_book3s(vcpu)->hior = 0xfff00000;
387 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
Alexander Graf07b09072010-04-16 00:11:53 +0200388 } else
389#endif
390 {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000391 kvmppc_mmu_book3s_32_init(vcpu);
392 to_book3s(vcpu)->hior = 0;
393 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
394 }
395
396 /* If we are in hypervisor level on 970, we can tell the CPU to
397 * treat DCBZ as 32 bytes store */
398 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
399 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
400 !strcmp(cur_cpu_spec->platform, "ppc970"))
401 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
402
Alexander Graf05b0ab12010-03-24 21:48:37 +0100403 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
404 really needs them in a VM on Cell and force disable them. */
405 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
406 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
Alexander Graf07b09072010-04-16 00:11:53 +0200407
408#ifdef CONFIG_PPC_BOOK3S_32
409 /* 32 bit Book3S always has 32 byte dcbz */
410 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
411#endif
Alexander Grafb83d4a92010-04-20 02:49:54 +0200412
413 /* On some CPUs we can execute paired single operations natively */
414 asm ( "mfpvr %0" : "=r"(host_pvr));
415 switch (host_pvr) {
416 case 0x00080200: /* lonestar 2.0 */
417 case 0x00088202: /* lonestar 2.2 */
418 case 0x70000100: /* gekko 1.0 */
419 case 0x00080100: /* gekko 2.0 */
420 case 0x00083203: /* gekko 2.3a */
421 case 0x00083213: /* gekko 2.3b */
422 case 0x00083204: /* gekko 2.4 */
423 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
424 case 0x00087200: /* broadway */
425 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
426 /* Enable HID2.PSE - in case we need it later */
427 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
428 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000429}
430
Alexander Grafe8508942010-07-29 14:47:54 +0200431pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn)
432{
433 ulong mp_pa = vcpu->arch.magic_page_pa;
434
435 /* Magic page override */
436 if (unlikely(mp_pa) &&
437 unlikely(((gfn << PAGE_SHIFT) & KVM_PAM) ==
438 ((mp_pa & PAGE_MASK) & KVM_PAM))) {
439 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
440 pfn_t pfn;
441
442 pfn = (pfn_t)virt_to_phys((void*)shared_page) >> PAGE_SHIFT;
443 get_page(pfn_to_page(pfn));
444 return pfn;
445 }
446
447 return gfn_to_pfn(vcpu->kvm, gfn);
448}
449
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000450/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
451 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
452 * emulate 32 bytes dcbz length.
453 *
454 * The Book3s_64 inventors also realized this case and implemented a special bit
455 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
456 *
457 * My approach here is to patch the dcbz instruction on executing pages.
458 */
459static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
460{
Alexander Graf9fb244a2010-03-24 21:48:32 +0100461 struct page *hpage;
462 u64 hpage_offset;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000463 u32 *page;
464 int i;
465
Alexander Graf9fb244a2010-03-24 21:48:32 +0100466 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
Wei Yongjun646bab52010-08-17 10:08:52 +0800467 if (is_error_page(hpage)) {
468 kvm_release_page_clean(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000469 return;
Wei Yongjun646bab52010-08-17 10:08:52 +0800470 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000471
Alexander Graf9fb244a2010-03-24 21:48:32 +0100472 hpage_offset = pte->raddr & ~PAGE_MASK;
473 hpage_offset &= ~0xFFFULL;
474 hpage_offset /= 4;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000475
Alexander Graf9fb244a2010-03-24 21:48:32 +0100476 get_page(hpage);
477 page = kmap_atomic(hpage, KM_USER0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000478
Alexander Graf9fb244a2010-03-24 21:48:32 +0100479 /* patch dcbz into reserved instruction, so we trap */
480 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
481 if ((page[i] & 0xff0007ff) == INS_DCBZ)
482 page[i] &= 0xfffffff7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000483
Alexander Graf9fb244a2010-03-24 21:48:32 +0100484 kunmap_atomic(page, KM_USER0);
485 put_page(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000486}
487
488static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
489 struct kvmppc_pte *pte)
490{
Alexander Graf666e7252010-07-29 14:47:43 +0200491 int relocated = (vcpu->arch.shared->msr & (data ? MSR_DR : MSR_IR));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000492 int r;
493
494 if (relocated) {
495 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
496 } else {
497 pte->eaddr = eaddr;
Alexander Graf28e83b42010-07-29 14:47:52 +0200498 pte->raddr = eaddr & KVM_PAM;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100499 pte->vpage = VSID_REAL | eaddr >> 12;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000500 pte->may_read = true;
501 pte->may_write = true;
502 pte->may_execute = true;
503 r = 0;
504 }
505
506 return r;
507}
508
509static hva_t kvmppc_bad_hva(void)
510{
511 return PAGE_OFFSET;
512}
513
514static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
515 bool read)
516{
517 hva_t hpage;
518
519 if (read && !pte->may_read)
520 goto err;
521
522 if (!read && !pte->may_write)
523 goto err;
524
525 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
526 if (kvm_is_error_hva(hpage))
527 goto err;
528
529 return hpage | (pte->raddr & ~PAGE_MASK);
530err:
531 return kvmppc_bad_hva();
532}
533
Alexander Graf5467a972010-02-19 11:00:38 +0100534int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
535 bool data)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000536{
537 struct kvmppc_pte pte;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000538
539 vcpu->stat.st++;
540
Alexander Graf5467a972010-02-19 11:00:38 +0100541 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
Alexander Graf9fb244a2010-03-24 21:48:32 +0100542 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100543
544 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000545
Alexander Graf9fb244a2010-03-24 21:48:32 +0100546 if (!pte.may_write)
547 return -EPERM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000548
Alexander Graf9fb244a2010-03-24 21:48:32 +0100549 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
550 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000551
Alexander Graf5467a972010-02-19 11:00:38 +0100552 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000553}
554
Alexander Graf5467a972010-02-19 11:00:38 +0100555int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000556 bool data)
557{
558 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100559 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000560
561 vcpu->stat.ld++;
562
Alexander Graf5467a972010-02-19 11:00:38 +0100563 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
564 goto nopte;
565
566 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000567
568 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
569 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100570 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000571
572 if (copy_from_user(ptr, (void __user *)hva, size)) {
573 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100574 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000575 }
576
Alexander Graf5467a972010-02-19 11:00:38 +0100577 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000578
Alexander Graf5467a972010-02-19 11:00:38 +0100579nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000580 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100581mmio:
582 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000583}
584
585static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
586{
Alexander Grafe8508942010-07-29 14:47:54 +0200587 ulong mp_pa = vcpu->arch.magic_page_pa;
588
589 if (unlikely(mp_pa) &&
590 unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) {
591 return 1;
592 }
593
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000594 return kvm_is_visible_gfn(vcpu->kvm, gfn);
595}
596
597int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
598 ulong eaddr, int vec)
599{
600 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
601 int r = RESUME_GUEST;
602 int relocated;
603 int page_found = 0;
604 struct kvmppc_pte pte;
605 bool is_mmio = false;
Alexander Graf666e7252010-07-29 14:47:43 +0200606 bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
607 bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
Alexander Graff7bc74e2010-04-20 02:49:48 +0200608 u64 vsid;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000609
Alexander Graf3eeafd72010-03-24 21:48:17 +0100610 relocated = data ? dr : ir;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000611
612 /* Resolve real address if translation turned on */
613 if (relocated) {
614 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
615 } else {
616 pte.may_execute = true;
617 pte.may_read = true;
618 pte.may_write = true;
Alexander Graf28e83b42010-07-29 14:47:52 +0200619 pte.raddr = eaddr & KVM_PAM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000620 pte.eaddr = eaddr;
621 pte.vpage = eaddr >> 12;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100622 }
623
Alexander Graf666e7252010-07-29 14:47:43 +0200624 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
Alexander Graf3eeafd72010-03-24 21:48:17 +0100625 case 0:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200626 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
Alexander Graf3eeafd72010-03-24 21:48:17 +0100627 break;
628 case MSR_DR:
Alexander Graf3eeafd72010-03-24 21:48:17 +0100629 case MSR_IR:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200630 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
631
Alexander Graf666e7252010-07-29 14:47:43 +0200632 if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
Alexander Graff7bc74e2010-04-20 02:49:48 +0200633 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
634 else
635 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
636 pte.vpage |= vsid;
637
638 if (vsid == -1)
639 page_found = -EINVAL;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100640 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000641 }
642
643 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
644 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
645 /*
646 * If we do the dcbz hack, we have to NX on every execution,
647 * so we can patch the executing code. This renders our guest
648 * NX-less.
649 */
650 pte.may_execute = !data;
651 }
652
653 if (page_found == -ENOENT) {
654 /* Page not found in guest PTE entries */
Alexander Graf5e030182010-07-29 14:47:45 +0200655 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Grafd562de42010-07-29 14:47:44 +0200656 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
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 == -EPERM) {
661 /* Storage protection */
Alexander Graf5e030182010-07-29 14:47:45 +0200662 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Grafd562de42010-07-29 14:47:44 +0200663 vcpu->arch.shared->dsisr =
664 to_svcpu(vcpu)->fault_dsisr & ~DSISR_NOHPTE;
665 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
Alexander Graf666e7252010-07-29 14:47:43 +0200666 vcpu->arch.shared->msr |=
667 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000668 kvmppc_book3s_queue_irqprio(vcpu, vec);
669 } else if (page_found == -EINVAL) {
670 /* Page not found in guest SLB */
Alexander Graf5e030182010-07-29 14:47:45 +0200671 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000672 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
673 } else if (!is_mmio &&
674 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
675 /* The guest's PTE is not mapped yet. Map on the host */
676 kvmppc_mmu_map_page(vcpu, &pte);
677 if (data)
678 vcpu->stat.sp_storage++;
679 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
680 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
681 kvmppc_patch_dcbz(vcpu, &pte);
682 } else {
683 /* MMIO */
684 vcpu->stat.mmio_exits++;
685 vcpu->arch.paddr_accessed = pte.raddr;
686 r = kvmppc_emulate_mmio(run, vcpu);
687 if ( r == RESUME_HOST_NV )
688 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000689 }
690
691 return r;
692}
693
Alexander Graf180a34d2010-01-15 14:49:11 +0100694static inline int get_fpr_index(int i)
695{
696#ifdef CONFIG_VSX
697 i *= 2;
698#endif
699 return i;
700}
701
702/* Give up external provider (FPU, Altivec, VSX) */
Alexander Grafaba3bd72010-02-19 11:00:39 +0100703void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
Alexander Graf180a34d2010-01-15 14:49:11 +0100704{
705 struct thread_struct *t = &current->thread;
706 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100707#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100708 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100709#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100710 u64 *thread_fpr = (u64*)t->fpr;
711 int i;
712
713 if (!(vcpu->arch.guest_owned_ext & msr))
714 return;
715
716#ifdef DEBUG_EXT
717 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
718#endif
719
720 switch (msr) {
721 case MSR_FP:
722 giveup_fpu(current);
723 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
724 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
725
726 vcpu->arch.fpscr = t->fpscr.val;
727 break;
728 case MSR_VEC:
729#ifdef CONFIG_ALTIVEC
730 giveup_altivec(current);
731 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
732 vcpu->arch.vscr = t->vscr;
733#endif
734 break;
735 case MSR_VSX:
736#ifdef CONFIG_VSX
737 __giveup_vsx(current);
738 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
739 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
740#endif
741 break;
742 default:
743 BUG();
744 }
745
746 vcpu->arch.guest_owned_ext &= ~msr;
747 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100748 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100749}
750
Alexander Graf89632212010-03-24 21:48:21 +0100751static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100752{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200753 ulong srr0 = kvmppc_get_pc(vcpu);
754 u32 last_inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100755 int ret;
756
Alexander Grafc7f38f42010-04-16 00:11:40 +0200757 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100758 if (ret == -ENOENT) {
Alexander Graf666e7252010-07-29 14:47:43 +0200759 ulong msr = vcpu->arch.shared->msr;
760
761 msr = kvmppc_set_field(msr, 33, 33, 1);
762 msr = kvmppc_set_field(msr, 34, 36, 0);
763 vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100764 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
Alexander Graf89632212010-03-24 21:48:21 +0100765 return EMULATE_AGAIN;
766 }
767
768 return EMULATE_DONE;
769}
770
771static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
772{
773
774 /* Need to do paired single emulation? */
775 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
776 return EMULATE_DONE;
777
778 /* Read out the instruction */
779 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100780 /* Need to emulate */
781 return EMULATE_FAIL;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100782
783 return EMULATE_AGAIN;
784}
785
Alexander Graf180a34d2010-01-15 14:49:11 +0100786/* Handle external providers (FPU, Altivec, VSX) */
787static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
788 ulong msr)
789{
790 struct thread_struct *t = &current->thread;
791 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100792#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100793 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100794#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100795 u64 *thread_fpr = (u64*)t->fpr;
796 int i;
797
Alexander Graf3c402a72010-02-19 11:00:32 +0100798 /* When we have paired singles, we emulate in software */
799 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
800 return RESUME_GUEST;
801
Alexander Graf666e7252010-07-29 14:47:43 +0200802 if (!(vcpu->arch.shared->msr & msr)) {
Alexander Graf180a34d2010-01-15 14:49:11 +0100803 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
804 return RESUME_GUEST;
805 }
806
Alexander Grafc2453692010-03-24 21:48:22 +0100807 /* We already own the ext */
808 if (vcpu->arch.guest_owned_ext & msr) {
809 return RESUME_GUEST;
810 }
811
Alexander Graf180a34d2010-01-15 14:49:11 +0100812#ifdef DEBUG_EXT
813 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
814#endif
815
816 current->thread.regs->msr |= msr;
817
818 switch (msr) {
819 case MSR_FP:
820 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
821 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
822
823 t->fpscr.val = vcpu->arch.fpscr;
824 t->fpexc_mode = 0;
825 kvmppc_load_up_fpu();
826 break;
827 case MSR_VEC:
828#ifdef CONFIG_ALTIVEC
829 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
830 t->vscr = vcpu->arch.vscr;
831 t->vrsave = -1;
832 kvmppc_load_up_altivec();
833#endif
834 break;
835 case MSR_VSX:
836#ifdef CONFIG_VSX
837 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
838 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
839 kvmppc_load_up_vsx();
840#endif
841 break;
842 default:
843 BUG();
844 }
845
846 vcpu->arch.guest_owned_ext |= msr;
847
Alexander Grafa76f8492010-01-15 14:49:14 +0100848 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100849
850 return RESUME_GUEST;
851}
852
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000853int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
854 unsigned int exit_nr)
855{
856 int r = RESUME_HOST;
857
858 vcpu->stat.sum_exits++;
859
860 run->exit_reason = KVM_EXIT_UNKNOWN;
861 run->ready_for_interrupt_injection = 1;
Alexander Grafbed1ed92010-08-02 11:06:26 +0200862
863 trace_kvm_book3s_exit(exit_nr, vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000864 kvm_resched(vcpu);
865 switch (exit_nr) {
866 case BOOK3S_INTERRUPT_INST_STORAGE:
867 vcpu->stat.pf_instruc++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200868
869#ifdef CONFIG_PPC_BOOK3S_32
870 /* We set segments as unused segments when invalidating them. So
871 * treat the respective fault as segment fault. */
872 if (to_svcpu(vcpu)->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT]
873 == SR_INVALID) {
874 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
875 r = RESUME_GUEST;
876 break;
877 }
878#endif
879
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000880 /* only care about PTEG not found errors, but leave NX alone */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200881 if (to_svcpu(vcpu)->shadow_srr1 & 0x40000000) {
882 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000883 vcpu->stat.sp_instruc++;
884 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
885 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
886 /*
887 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
888 * so we can't use the NX bit inside the guest. Let's cross our fingers,
889 * that no guest that needs the dcbz hack does NX.
890 */
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200891 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100892 r = RESUME_GUEST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000893 } else {
Alexander Graf666e7252010-07-29 14:47:43 +0200894 vcpu->arch.shared->msr |=
895 to_svcpu(vcpu)->shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000896 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
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 Graf2f4cf5e2009-10-30 05:47:10 +0000922 r = RESUME_GUEST;
923 }
924 break;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200925 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000926 case BOOK3S_INTERRUPT_DATA_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200927 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
Alexander Graf5e030182010-07-29 14:47:45 +0200928 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000929 kvmppc_book3s_queue_irqprio(vcpu,
930 BOOK3S_INTERRUPT_DATA_SEGMENT);
931 }
932 r = RESUME_GUEST;
933 break;
934 case BOOK3S_INTERRUPT_INST_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200935 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000936 kvmppc_book3s_queue_irqprio(vcpu,
937 BOOK3S_INTERRUPT_INST_SEGMENT);
938 }
939 r = RESUME_GUEST;
940 break;
941 /* We're good on these - the host merely wanted to get our attention */
942 case BOOK3S_INTERRUPT_DECREMENTER:
943 vcpu->stat.dec_exits++;
944 r = RESUME_GUEST;
945 break;
946 case BOOK3S_INTERRUPT_EXTERNAL:
947 vcpu->stat.ext_intr_exits++;
948 r = RESUME_GUEST;
949 break;
Alexander Graf7fdaec92010-04-20 02:49:47 +0200950 case BOOK3S_INTERRUPT_PERFMON:
951 r = RESUME_GUEST;
952 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000953 case BOOK3S_INTERRUPT_PROGRAM:
954 {
955 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100956 ulong flags;
957
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100958program_interrupt:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200959 flags = to_svcpu(vcpu)->shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000960
Alexander Graf666e7252010-07-29 14:47:43 +0200961 if (vcpu->arch.shared->msr & MSR_PR) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000962#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +0200963 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 +0000964#endif
Alexander Grafc7f38f42010-04-16 00:11:40 +0200965 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000966 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100967 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000968 r = RESUME_GUEST;
969 break;
970 }
971 }
972
973 vcpu->stat.emulated_inst_exits++;
974 er = kvmppc_emulate_instruction(run, vcpu);
975 switch (er) {
976 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100977 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000978 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100979 case EMULATE_AGAIN:
980 r = RESUME_GUEST;
981 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000982 case EMULATE_FAIL:
983 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200984 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100985 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000986 r = RESUME_GUEST;
987 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +0100988 case EMULATE_DO_MMIO:
989 run->exit_reason = KVM_EXIT_MMIO;
990 r = RESUME_HOST_NV;
991 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000992 default:
993 BUG();
994 }
995 break;
996 }
997 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafad0a0482010-03-24 21:48:30 +0100998 if (vcpu->arch.osi_enabled &&
999 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
1000 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
Alexander Graf2a342ed2010-07-29 14:47:48 +02001001 /* MOL hypercalls */
Alexander Grafad0a0482010-03-24 21:48:30 +01001002 u64 *gprs = run->osi.gprs;
1003 int i;
1004
1005 run->exit_reason = KVM_EXIT_OSI;
1006 for (i = 0; i < 32; i++)
1007 gprs[i] = kvmppc_get_gpr(vcpu, i);
1008 vcpu->arch.osi_needed = 1;
1009 r = RESUME_HOST_NV;
Alexander Graf2a342ed2010-07-29 14:47:48 +02001010 } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
1011 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
1012 /* KVM PV hypercalls */
1013 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1014 r = RESUME_GUEST;
Alexander Grafad0a0482010-03-24 21:48:30 +01001015 } else {
Alexander Graf2a342ed2010-07-29 14:47:48 +02001016 /* Guest syscalls */
Alexander Grafad0a0482010-03-24 21:48:30 +01001017 vcpu->stat.syscall_exits++;
1018 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1019 r = RESUME_GUEST;
1020 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001021 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001022 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001023 case BOOK3S_INTERRUPT_ALTIVEC:
1024 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +01001025 {
1026 int ext_msr = 0;
1027
1028 switch (exit_nr) {
1029 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
1030 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
1031 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
1032 }
1033
1034 switch (kvmppc_check_ext(vcpu, exit_nr)) {
1035 case EMULATE_DONE:
1036 /* everything ok - let's enable the ext */
1037 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
1038 break;
1039 case EMULATE_FAIL:
1040 /* we need to emulate this instruction */
1041 goto program_interrupt;
1042 break;
1043 default:
1044 /* nothing to worry about - go again */
1045 break;
1046 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001047 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +01001048 }
Alexander Grafca7f4202010-03-24 21:48:28 +01001049 case BOOK3S_INTERRUPT_ALIGNMENT:
1050 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
Alexander Grafd562de42010-07-29 14:47:44 +02001051 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001052 kvmppc_get_last_inst(vcpu));
Alexander Graf5e030182010-07-29 14:47:45 +02001053 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001054 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +01001055 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1056 }
1057 r = RESUME_GUEST;
1058 break;
Alexander Graf180a34d2010-01-15 14:49:11 +01001059 case BOOK3S_INTERRUPT_MACHINE_CHECK:
1060 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001061 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1062 r = RESUME_GUEST;
1063 break;
1064 default:
1065 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +01001066 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +02001067 exit_nr, kvmppc_get_pc(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001068 r = RESUME_HOST;
1069 BUG();
1070 break;
1071 }
1072
1073
1074 if (!(r & RESUME_HOST)) {
1075 /* To avoid clobbering exit_reason, only check for signals if
1076 * we aren't already exiting to userspace for some other
1077 * reason. */
1078 if (signal_pending(current)) {
1079#ifdef EXIT_DEBUG
1080 printk(KERN_EMERG "KVM: Going back to host\n");
1081#endif
1082 vcpu->stat.signal_exits++;
1083 run->exit_reason = KVM_EXIT_INTR;
1084 r = -EINTR;
1085 } else {
1086 /* In case an interrupt came in that was triggered
1087 * from userspace (like DEC), we need to check what
1088 * to inject now! */
1089 kvmppc_core_deliver_interrupts(vcpu);
1090 }
1091 }
1092
Alexander Grafbed1ed92010-08-02 11:06:26 +02001093 trace_kvm_book3s_reenter(r, vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001094
1095 return r;
1096}
1097
1098int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1099{
1100 return 0;
1101}
1102
1103int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1104{
1105 int i;
1106
Alexander Grafc7f38f42010-04-16 00:11:40 +02001107 regs->pc = kvmppc_get_pc(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001108 regs->cr = kvmppc_get_cr(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001109 regs->ctr = kvmppc_get_ctr(vcpu);
1110 regs->lr = kvmppc_get_lr(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001111 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001112 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +02001113 regs->srr0 = vcpu->arch.shared->srr0;
1114 regs->srr1 = vcpu->arch.shared->srr1;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001115 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001116 regs->sprg0 = vcpu->arch.shared->sprg0;
1117 regs->sprg1 = vcpu->arch.shared->sprg1;
1118 regs->sprg2 = vcpu->arch.shared->sprg2;
1119 regs->sprg3 = vcpu->arch.shared->sprg3;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001120 regs->sprg5 = vcpu->arch.sprg4;
1121 regs->sprg6 = vcpu->arch.sprg5;
1122 regs->sprg7 = vcpu->arch.sprg6;
1123
1124 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001125 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001126
1127 return 0;
1128}
1129
1130int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1131{
1132 int i;
1133
Alexander Grafc7f38f42010-04-16 00:11:40 +02001134 kvmppc_set_pc(vcpu, regs->pc);
Alexander Graf992b5b22010-01-08 02:58:02 +01001135 kvmppc_set_cr(vcpu, regs->cr);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001136 kvmppc_set_ctr(vcpu, regs->ctr);
1137 kvmppc_set_lr(vcpu, regs->lr);
Alexander Graf992b5b22010-01-08 02:58:02 +01001138 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001139 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +02001140 vcpu->arch.shared->srr0 = regs->srr0;
1141 vcpu->arch.shared->srr1 = regs->srr1;
Alexander Grafa73a9592010-07-29 14:47:47 +02001142 vcpu->arch.shared->sprg0 = regs->sprg0;
1143 vcpu->arch.shared->sprg1 = regs->sprg1;
1144 vcpu->arch.shared->sprg2 = regs->sprg2;
1145 vcpu->arch.shared->sprg3 = regs->sprg3;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001146 vcpu->arch.sprg5 = regs->sprg4;
1147 vcpu->arch.sprg6 = regs->sprg5;
1148 vcpu->arch.sprg7 = regs->sprg6;
1149
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001150 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1151 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001152
1153 return 0;
1154}
1155
1156int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1157 struct kvm_sregs *sregs)
1158{
Alexander Grafe15a1132009-11-30 03:02:02 +00001159 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1160 int i;
1161
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001162 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +00001163
1164 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1165 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1166 for (i = 0; i < 64; i++) {
1167 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
1168 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1169 }
1170 } else {
Alexander Grafdf1bfa22010-08-03 02:29:27 +02001171 for (i = 0; i < 16; i++)
1172 sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i];
1173
Alexander Grafe15a1132009-11-30 03:02:02 +00001174 for (i = 0; i < 8; i++) {
1175 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1176 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1177 }
1178 }
Avi Kivity98001d82010-05-13 11:05:49 +03001179
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001180 return 0;
1181}
1182
1183int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1184 struct kvm_sregs *sregs)
1185{
Alexander Grafe15a1132009-11-30 03:02:02 +00001186 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1187 int i;
1188
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001189 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001190
1191 vcpu3s->sdr1 = sregs->u.s.sdr1;
1192 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1193 for (i = 0; i < 64; i++) {
1194 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1195 sregs->u.s.ppc64.slb[i].slbe);
1196 }
1197 } else {
1198 for (i = 0; i < 16; i++) {
1199 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1200 }
1201 for (i = 0; i < 8; i++) {
1202 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1203 (u32)sregs->u.s.ppc32.ibat[i]);
1204 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1205 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1206 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1207 (u32)sregs->u.s.ppc32.dbat[i]);
1208 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1209 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1210 }
1211 }
1212
1213 /* Flush the MMU after messing with the segments */
1214 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Avi Kivity98001d82010-05-13 11:05:49 +03001215
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001216 return 0;
1217}
1218
1219int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1220{
1221 return -ENOTSUPP;
1222}
1223
1224int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1225{
1226 return -ENOTSUPP;
1227}
1228
1229int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1230 struct kvm_translation *tr)
1231{
1232 return 0;
1233}
1234
1235/*
1236 * Get (and clear) the dirty memory log for a memory slot.
1237 */
1238int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1239 struct kvm_dirty_log *log)
1240{
1241 struct kvm_memory_slot *memslot;
1242 struct kvm_vcpu *vcpu;
1243 ulong ga, ga_end;
1244 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001245 int r;
1246 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001247
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001248 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001249
1250 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1251 if (r)
1252 goto out;
1253
1254 /* If nothing is dirty, don't bother messing with page tables. */
1255 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001256 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001257
1258 ga = memslot->base_gfn << PAGE_SHIFT;
1259 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1260
1261 kvm_for_each_vcpu(n, vcpu, kvm)
1262 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1263
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001264 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001265 memset(memslot->dirty_bitmap, 0, n);
1266 }
1267
1268 r = 0;
1269out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001270 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001271 return r;
1272}
1273
1274int kvmppc_core_check_processor_compat(void)
1275{
1276 return 0;
1277}
1278
1279struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1280{
1281 struct kvmppc_vcpu_book3s *vcpu_book3s;
1282 struct kvm_vcpu *vcpu;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001283 int err = -ENOMEM;
Alexander Grafe8508942010-07-29 14:47:54 +02001284 unsigned long p;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001285
Alexander Graf032c3402010-02-19 12:24:33 +01001286 vcpu_book3s = vmalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Grafc7f38f42010-04-16 00:11:40 +02001287 if (!vcpu_book3s)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001288 goto out;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001289
Alexander Graf7e821d32010-02-22 16:52:08 +01001290 memset(vcpu_book3s, 0, sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001291
Alexander Grafc7f38f42010-04-16 00:11:40 +02001292 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
1293 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
1294 if (!vcpu_book3s->shadow_vcpu)
1295 goto free_vcpu;
1296
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001297 vcpu = &vcpu_book3s->vcpu;
1298 err = kvm_vcpu_init(vcpu, kvm, id);
1299 if (err)
Alexander Grafc7f38f42010-04-16 00:11:40 +02001300 goto free_shadow_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001301
Alexander Grafe8508942010-07-29 14:47:54 +02001302 p = __get_free_page(GFP_KERNEL|__GFP_ZERO);
1303 /* the real shared page fills the last 4k of our page */
1304 vcpu->arch.shared = (void*)(p + PAGE_SIZE - 4096);
1305 if (!p)
Alexander Graf96bc4512010-07-29 14:47:42 +02001306 goto uninit_vcpu;
1307
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001308 vcpu->arch.host_retip = kvm_return_point;
1309 vcpu->arch.host_msr = mfmsr();
Alexander Graf07b09072010-04-16 00:11:53 +02001310#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001311 /* default to book3s_64 (970fx) */
1312 vcpu->arch.pvr = 0x3C0301;
Alexander Graf07b09072010-04-16 00:11:53 +02001313#else
1314 /* default to book3s_32 (750) */
1315 vcpu->arch.pvr = 0x84202;
1316#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001317 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1318 vcpu_book3s->slb_nr = 64;
1319
1320 /* remember where some real-mode handlers are */
1321 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1322 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1323 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf07b09072010-04-16 00:11:53 +02001324#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf021ec9c2010-01-08 02:58:06 +01001325 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf07b09072010-04-16 00:11:53 +02001326#else
1327 vcpu->arch.rmcall = (ulong)kvmppc_rmcall;
1328#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001329
1330 vcpu->arch.shadow_msr = MSR_USER64;
1331
Alexander Graf9cc5e952010-04-16 00:11:45 +02001332 err = kvmppc_mmu_init(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001333 if (err < 0)
Alexander Graf96bc4512010-07-29 14:47:42 +02001334 goto uninit_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001335
1336 return vcpu;
1337
Alexander Graf96bc4512010-07-29 14:47:42 +02001338uninit_vcpu:
1339 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001340free_shadow_vcpu:
1341 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001342free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001343 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001344out:
1345 return ERR_PTR(err);
1346}
1347
1348void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1349{
1350 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1351
Alexander Grafe8508942010-07-29 14:47:54 +02001352 free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001353 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001354 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001355 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001356}
1357
1358extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1359int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1360{
1361 int ret;
Andreas Schwab49f6be82010-05-31 21:59:13 +02001362 double fpr[32][TS_FPRWIDTH];
1363 unsigned int fpscr;
1364 int fpexc_mode;
Alexander Grafa2b07662010-03-24 21:48:31 +01001365#ifdef CONFIG_ALTIVEC
Andreas Schwab49f6be82010-05-31 21:59:13 +02001366 vector128 vr[32];
1367 vector128 vscr;
1368 unsigned long uninitialized_var(vrsave);
1369 int used_vr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001370#endif
1371#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001372 int used_vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001373#endif
Alexander Graf180a34d2010-01-15 14:49:11 +01001374 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001375
1376 /* No need to go into the guest when all we do is going out */
1377 if (signal_pending(current)) {
1378 kvm_run->exit_reason = KVM_EXIT_INTR;
1379 return -EINTR;
1380 }
1381
Alexander Graf180a34d2010-01-15 14:49:11 +01001382 /* Save FPU state in stack */
1383 if (current->thread.regs->msr & MSR_FP)
1384 giveup_fpu(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001385 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
1386 fpscr = current->thread.fpscr.val;
1387 fpexc_mode = current->thread.fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001388
1389#ifdef CONFIG_ALTIVEC
1390 /* Save Altivec state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001391 used_vr = current->thread.used_vr;
1392 if (used_vr) {
Alexander Graf180a34d2010-01-15 14:49:11 +01001393 if (current->thread.regs->msr & MSR_VEC)
1394 giveup_altivec(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001395 memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
1396 vscr = current->thread.vscr;
1397 vrsave = current->thread.vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001398 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001399#endif
1400
1401#ifdef CONFIG_VSX
1402 /* Save VSX state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001403 used_vsr = current->thread.used_vsr;
1404 if (used_vsr && (current->thread.regs->msr & MSR_VSX))
Alexander Graf180a34d2010-01-15 14:49:11 +01001405 __giveup_vsx(current);
Alexander Graf180a34d2010-01-15 14:49:11 +01001406#endif
1407
1408 /* Remember the MSR with disabled extensions */
1409 ext_msr = current->thread.regs->msr;
1410
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001411 /* XXX we get called with irq disabled - change that! */
1412 local_irq_enable();
1413
Alexander Grafd1bab742010-02-19 11:00:35 +01001414 /* Preload FPU if it's enabled */
Alexander Graf666e7252010-07-29 14:47:43 +02001415 if (vcpu->arch.shared->msr & MSR_FP)
Alexander Grafd1bab742010-02-19 11:00:35 +01001416 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1417
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001418 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1419
1420 local_irq_disable();
1421
Alexander Graf180a34d2010-01-15 14:49:11 +01001422 current->thread.regs->msr = ext_msr;
1423
1424 /* Make sure we save the guest FPU/Altivec/VSX state */
1425 kvmppc_giveup_ext(vcpu, MSR_FP);
1426 kvmppc_giveup_ext(vcpu, MSR_VEC);
1427 kvmppc_giveup_ext(vcpu, MSR_VSX);
1428
1429 /* Restore FPU state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001430 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
1431 current->thread.fpscr.val = fpscr;
1432 current->thread.fpexc_mode = fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001433
1434#ifdef CONFIG_ALTIVEC
1435 /* Restore Altivec state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001436 if (used_vr && current->thread.used_vr) {
1437 memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
1438 current->thread.vscr = vscr;
1439 current->thread.vrsave = vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001440 }
Andreas Schwab49f6be82010-05-31 21:59:13 +02001441 current->thread.used_vr = used_vr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001442#endif
1443
1444#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001445 current->thread.used_vsr = used_vsr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001446#endif
1447
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001448 return ret;
1449}
1450
1451static int kvmppc_book3s_init(void)
1452{
Alexander Graffef093b2010-06-30 15:18:46 +02001453 int r;
1454
1455 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
1456 THIS_MODULE);
1457
1458 if (r)
1459 return r;
1460
1461 r = kvmppc_mmu_hpte_sysinit();
1462
1463 return r;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001464}
1465
1466static void kvmppc_book3s_exit(void)
1467{
Alexander Graffef093b2010-06-30 15:18:46 +02001468 kvmppc_mmu_hpte_sysexit();
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001469 kvm_exit();
1470}
1471
1472module_init(kvmppc_book3s_init);
1473module_exit(kvmppc_book3s_exit);