blob: 54ca578239dbef22d811717ef726c290123ee0f4 [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
137 if (msr & (MSR_WE|MSR_POW)) {
138 if (!vcpu->arch.pending_exceptions) {
139 kvm_vcpu_block(vcpu);
140 vcpu->stat.halt_wakeup++;
141 }
142 }
143
Alexander Graf666e7252010-07-29 14:47:43 +0200144 if ((vcpu->arch.shared->msr & (MSR_PR|MSR_IR|MSR_DR)) !=
Alexander Graff7bc74e2010-04-20 02:49:48 +0200145 (old_msr & (MSR_PR|MSR_IR|MSR_DR))) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000146 kvmppc_mmu_flush_segments(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +0200147 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
Alexander Graf4cb6b7e2010-08-02 16:08:22 +0200148
149 /* Preload magic page segment when in kernel mode */
150 if (!(msr & MSR_PR) && vcpu->arch.magic_page_pa) {
151 struct kvm_vcpu_arch *a = &vcpu->arch;
152
153 if (msr & MSR_DR)
154 kvmppc_mmu_map_segment(vcpu, a->magic_page_ea);
155 else
156 kvmppc_mmu_map_segment(vcpu, a->magic_page_pa);
157 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000158 }
Alexander Grafd1bab742010-02-19 11:00:35 +0100159
160 /* Preload FPU if it's enabled */
Alexander Graf666e7252010-07-29 14:47:43 +0200161 if (vcpu->arch.shared->msr & MSR_FP)
Alexander Grafd1bab742010-02-19 11:00:35 +0100162 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000163}
164
165void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
166{
Alexander Grafde7906c2010-07-29 14:47:46 +0200167 vcpu->arch.shared->srr0 = kvmppc_get_pc(vcpu);
168 vcpu->arch.shared->srr1 = vcpu->arch.shared->msr | flags;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200169 kvmppc_set_pc(vcpu, to_book3s(vcpu)->hior + vec);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000170 vcpu->arch.mmu.reset_msr(vcpu);
171}
172
Alexander Graf583617b2009-12-21 20:21:23 +0100173static int kvmppc_book3s_vec2irqprio(unsigned int vec)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000174{
175 unsigned int prio;
176
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000177 switch (vec) {
178 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
179 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
180 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
181 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
182 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
183 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
184 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
185 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
186 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
187 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
188 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
189 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
190 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
191 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
192 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
193 default: prio = BOOK3S_IRQPRIO_MAX; break;
194 }
195
Alexander Graf583617b2009-12-21 20:21:23 +0100196 return prio;
197}
198
Alexander Graf7706664d2009-12-21 20:21:24 +0100199static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
200 unsigned int vec)
201{
202 clear_bit(kvmppc_book3s_vec2irqprio(vec),
203 &vcpu->arch.pending_exceptions);
204}
205
Alexander Graf583617b2009-12-21 20:21:23 +0100206void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
207{
208 vcpu->stat.queue_intr++;
209
210 set_bit(kvmppc_book3s_vec2irqprio(vec),
211 &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000212#ifdef EXIT_DEBUG
213 printk(KERN_INFO "Queueing interrupt %x\n", vec);
214#endif
215}
216
217
Alexander Graf25a8a022010-01-08 02:58:07 +0100218void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000219{
Alexander Graf25a8a022010-01-08 02:58:07 +0100220 to_book3s(vcpu)->prog_flags = flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000221 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM);
222}
223
224void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
225{
226 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
227}
228
229int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
230{
231 return test_bit(BOOK3S_INTERRUPT_DECREMENTER >> 7, &vcpu->arch.pending_exceptions);
232}
233
Alexander Graf7706664d2009-12-21 20:21:24 +0100234void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
235{
236 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
237}
238
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000239void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
240 struct kvm_interrupt *irq)
241{
242 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
243}
244
Alexander Graf18978762010-03-24 21:48:18 +0100245void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
246 struct kvm_interrupt *irq)
247{
248 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
249}
250
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000251int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
252{
253 int deliver = 1;
254 int vec = 0;
Alexander Graf25a8a022010-01-08 02:58:07 +0100255 ulong flags = 0ULL;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200256 ulong crit_raw = vcpu->arch.shared->critical;
257 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
258 bool crit;
259
260 /* Truncate crit indicators in 32 bit mode */
261 if (!(vcpu->arch.shared->msr & MSR_SF)) {
262 crit_raw &= 0xffffffff;
263 crit_r1 &= 0xffffffff;
264 }
265
266 /* Critical section when crit == r1 */
267 crit = (crit_raw == crit_r1);
268 /* ... and we're in supervisor mode */
269 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000270
271 switch (priority) {
272 case BOOK3S_IRQPRIO_DECREMENTER:
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200273 deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000274 vec = BOOK3S_INTERRUPT_DECREMENTER;
275 break;
276 case BOOK3S_IRQPRIO_EXTERNAL:
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200277 deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000278 vec = BOOK3S_INTERRUPT_EXTERNAL;
279 break;
280 case BOOK3S_IRQPRIO_SYSTEM_RESET:
281 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
282 break;
283 case BOOK3S_IRQPRIO_MACHINE_CHECK:
284 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
285 break;
286 case BOOK3S_IRQPRIO_DATA_STORAGE:
287 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
288 break;
289 case BOOK3S_IRQPRIO_INST_STORAGE:
290 vec = BOOK3S_INTERRUPT_INST_STORAGE;
291 break;
292 case BOOK3S_IRQPRIO_DATA_SEGMENT:
293 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
294 break;
295 case BOOK3S_IRQPRIO_INST_SEGMENT:
296 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
297 break;
298 case BOOK3S_IRQPRIO_ALIGNMENT:
299 vec = BOOK3S_INTERRUPT_ALIGNMENT;
300 break;
301 case BOOK3S_IRQPRIO_PROGRAM:
302 vec = BOOK3S_INTERRUPT_PROGRAM;
Alexander Graf25a8a022010-01-08 02:58:07 +0100303 flags = to_book3s(vcpu)->prog_flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000304 break;
305 case BOOK3S_IRQPRIO_VSX:
306 vec = BOOK3S_INTERRUPT_VSX;
307 break;
308 case BOOK3S_IRQPRIO_ALTIVEC:
309 vec = BOOK3S_INTERRUPT_ALTIVEC;
310 break;
311 case BOOK3S_IRQPRIO_FP_UNAVAIL:
312 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
313 break;
314 case BOOK3S_IRQPRIO_SYSCALL:
315 vec = BOOK3S_INTERRUPT_SYSCALL;
316 break;
317 case BOOK3S_IRQPRIO_DEBUG:
318 vec = BOOK3S_INTERRUPT_TRACE;
319 break;
320 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
321 vec = BOOK3S_INTERRUPT_PERFMON;
322 break;
323 default:
324 deliver = 0;
325 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
326 break;
327 }
328
329#if 0
330 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
331#endif
332
333 if (deliver)
Alexander Graf25a8a022010-01-08 02:58:07 +0100334 kvmppc_inject_interrupt(vcpu, vec, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000335
336 return deliver;
337}
338
339void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
340{
341 unsigned long *pending = &vcpu->arch.pending_exceptions;
Alexander Graf90bba352010-07-29 14:47:51 +0200342 unsigned long old_pending = vcpu->arch.pending_exceptions;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000343 unsigned int priority;
344
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000345#ifdef EXIT_DEBUG
346 if (vcpu->arch.pending_exceptions)
347 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
348#endif
349 priority = __ffs(*pending);
Alexander Grafada7ba12010-04-16 00:11:56 +0200350 while (priority < BOOK3S_IRQPRIO_MAX) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100351 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
352 (priority != BOOK3S_IRQPRIO_DECREMENTER)) {
353 /* DEC interrupts get cleared by mtdec */
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000354 clear_bit(priority, &vcpu->arch.pending_exceptions);
355 break;
356 }
357
358 priority = find_next_bit(pending,
359 BITS_PER_BYTE * sizeof(*pending),
360 priority + 1);
361 }
Alexander Graf90bba352010-07-29 14:47:51 +0200362
363 /* Tell the guest about our interrupt status */
364 if (*pending)
365 vcpu->arch.shared->int_pending = 1;
366 else if (old_pending)
367 vcpu->arch.shared->int_pending = 0;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000368}
369
370void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
371{
Alexander Grafb83d4a92010-04-20 02:49:54 +0200372 u32 host_pvr;
373
Alexander Grafe15a1132009-11-30 03:02:02 +0000374 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000375 vcpu->arch.pvr = pvr;
Alexander Graf07b09072010-04-16 00:11:53 +0200376#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000377 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
378 kvmppc_mmu_book3s_64_init(vcpu);
379 to_book3s(vcpu)->hior = 0xfff00000;
380 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
Alexander Graf07b09072010-04-16 00:11:53 +0200381 } else
382#endif
383 {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000384 kvmppc_mmu_book3s_32_init(vcpu);
385 to_book3s(vcpu)->hior = 0;
386 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
387 }
388
389 /* If we are in hypervisor level on 970, we can tell the CPU to
390 * treat DCBZ as 32 bytes store */
391 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
392 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
393 !strcmp(cur_cpu_spec->platform, "ppc970"))
394 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
395
Alexander Graf05b0ab12010-03-24 21:48:37 +0100396 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
397 really needs them in a VM on Cell and force disable them. */
398 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
399 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
Alexander Graf07b09072010-04-16 00:11:53 +0200400
401#ifdef CONFIG_PPC_BOOK3S_32
402 /* 32 bit Book3S always has 32 byte dcbz */
403 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
404#endif
Alexander Grafb83d4a92010-04-20 02:49:54 +0200405
406 /* On some CPUs we can execute paired single operations natively */
407 asm ( "mfpvr %0" : "=r"(host_pvr));
408 switch (host_pvr) {
409 case 0x00080200: /* lonestar 2.0 */
410 case 0x00088202: /* lonestar 2.2 */
411 case 0x70000100: /* gekko 1.0 */
412 case 0x00080100: /* gekko 2.0 */
413 case 0x00083203: /* gekko 2.3a */
414 case 0x00083213: /* gekko 2.3b */
415 case 0x00083204: /* gekko 2.4 */
416 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
417 case 0x00087200: /* broadway */
418 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
419 /* Enable HID2.PSE - in case we need it later */
420 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
421 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000422}
423
Alexander Grafe8508942010-07-29 14:47:54 +0200424pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn)
425{
426 ulong mp_pa = vcpu->arch.magic_page_pa;
427
428 /* Magic page override */
429 if (unlikely(mp_pa) &&
430 unlikely(((gfn << PAGE_SHIFT) & KVM_PAM) ==
431 ((mp_pa & PAGE_MASK) & KVM_PAM))) {
432 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
433 pfn_t pfn;
434
435 pfn = (pfn_t)virt_to_phys((void*)shared_page) >> PAGE_SHIFT;
436 get_page(pfn_to_page(pfn));
437 return pfn;
438 }
439
440 return gfn_to_pfn(vcpu->kvm, gfn);
441}
442
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000443/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
444 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
445 * emulate 32 bytes dcbz length.
446 *
447 * The Book3s_64 inventors also realized this case and implemented a special bit
448 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
449 *
450 * My approach here is to patch the dcbz instruction on executing pages.
451 */
452static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
453{
Alexander Graf9fb244a2010-03-24 21:48:32 +0100454 struct page *hpage;
455 u64 hpage_offset;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000456 u32 *page;
457 int i;
458
Alexander Graf9fb244a2010-03-24 21:48:32 +0100459 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
Wei Yongjun646bab52010-08-17 10:08:52 +0800460 if (is_error_page(hpage)) {
461 kvm_release_page_clean(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000462 return;
Wei Yongjun646bab52010-08-17 10:08:52 +0800463 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000464
Alexander Graf9fb244a2010-03-24 21:48:32 +0100465 hpage_offset = pte->raddr & ~PAGE_MASK;
466 hpage_offset &= ~0xFFFULL;
467 hpage_offset /= 4;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000468
Alexander Graf9fb244a2010-03-24 21:48:32 +0100469 get_page(hpage);
470 page = kmap_atomic(hpage, KM_USER0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000471
Alexander Graf9fb244a2010-03-24 21:48:32 +0100472 /* patch dcbz into reserved instruction, so we trap */
473 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
474 if ((page[i] & 0xff0007ff) == INS_DCBZ)
475 page[i] &= 0xfffffff7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000476
Alexander Graf9fb244a2010-03-24 21:48:32 +0100477 kunmap_atomic(page, KM_USER0);
478 put_page(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000479}
480
481static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
482 struct kvmppc_pte *pte)
483{
Alexander Graf666e7252010-07-29 14:47:43 +0200484 int relocated = (vcpu->arch.shared->msr & (data ? MSR_DR : MSR_IR));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000485 int r;
486
487 if (relocated) {
488 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
489 } else {
490 pte->eaddr = eaddr;
Alexander Graf28e83b42010-07-29 14:47:52 +0200491 pte->raddr = eaddr & KVM_PAM;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100492 pte->vpage = VSID_REAL | eaddr >> 12;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000493 pte->may_read = true;
494 pte->may_write = true;
495 pte->may_execute = true;
496 r = 0;
497 }
498
499 return r;
500}
501
502static hva_t kvmppc_bad_hva(void)
503{
504 return PAGE_OFFSET;
505}
506
507static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
508 bool read)
509{
510 hva_t hpage;
511
512 if (read && !pte->may_read)
513 goto err;
514
515 if (!read && !pte->may_write)
516 goto err;
517
518 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
519 if (kvm_is_error_hva(hpage))
520 goto err;
521
522 return hpage | (pte->raddr & ~PAGE_MASK);
523err:
524 return kvmppc_bad_hva();
525}
526
Alexander Graf5467a972010-02-19 11:00:38 +0100527int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
528 bool data)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000529{
530 struct kvmppc_pte pte;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000531
532 vcpu->stat.st++;
533
Alexander Graf5467a972010-02-19 11:00:38 +0100534 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
Alexander Graf9fb244a2010-03-24 21:48:32 +0100535 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100536
537 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000538
Alexander Graf9fb244a2010-03-24 21:48:32 +0100539 if (!pte.may_write)
540 return -EPERM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000541
Alexander Graf9fb244a2010-03-24 21:48:32 +0100542 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
543 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000544
Alexander Graf5467a972010-02-19 11:00:38 +0100545 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000546}
547
Alexander Graf5467a972010-02-19 11:00:38 +0100548int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000549 bool data)
550{
551 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100552 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000553
554 vcpu->stat.ld++;
555
Alexander Graf5467a972010-02-19 11:00:38 +0100556 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
557 goto nopte;
558
559 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000560
561 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
562 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100563 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000564
565 if (copy_from_user(ptr, (void __user *)hva, size)) {
566 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100567 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000568 }
569
Alexander Graf5467a972010-02-19 11:00:38 +0100570 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000571
Alexander Graf5467a972010-02-19 11:00:38 +0100572nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000573 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100574mmio:
575 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000576}
577
578static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
579{
Alexander Grafe8508942010-07-29 14:47:54 +0200580 ulong mp_pa = vcpu->arch.magic_page_pa;
581
582 if (unlikely(mp_pa) &&
583 unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) {
584 return 1;
585 }
586
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000587 return kvm_is_visible_gfn(vcpu->kvm, gfn);
588}
589
590int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
591 ulong eaddr, int vec)
592{
593 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
594 int r = RESUME_GUEST;
595 int relocated;
596 int page_found = 0;
597 struct kvmppc_pte pte;
598 bool is_mmio = false;
Alexander Graf666e7252010-07-29 14:47:43 +0200599 bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
600 bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
Alexander Graff7bc74e2010-04-20 02:49:48 +0200601 u64 vsid;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000602
Alexander Graf3eeafd72010-03-24 21:48:17 +0100603 relocated = data ? dr : ir;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000604
605 /* Resolve real address if translation turned on */
606 if (relocated) {
607 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
608 } else {
609 pte.may_execute = true;
610 pte.may_read = true;
611 pte.may_write = true;
Alexander Graf28e83b42010-07-29 14:47:52 +0200612 pte.raddr = eaddr & KVM_PAM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000613 pte.eaddr = eaddr;
614 pte.vpage = eaddr >> 12;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100615 }
616
Alexander Graf666e7252010-07-29 14:47:43 +0200617 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
Alexander Graf3eeafd72010-03-24 21:48:17 +0100618 case 0:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200619 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
Alexander Graf3eeafd72010-03-24 21:48:17 +0100620 break;
621 case MSR_DR:
Alexander Graf3eeafd72010-03-24 21:48:17 +0100622 case MSR_IR:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200623 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
624
Alexander Graf666e7252010-07-29 14:47:43 +0200625 if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
Alexander Graff7bc74e2010-04-20 02:49:48 +0200626 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
627 else
628 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
629 pte.vpage |= vsid;
630
631 if (vsid == -1)
632 page_found = -EINVAL;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100633 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000634 }
635
636 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
637 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
638 /*
639 * If we do the dcbz hack, we have to NX on every execution,
640 * so we can patch the executing code. This renders our guest
641 * NX-less.
642 */
643 pte.may_execute = !data;
644 }
645
646 if (page_found == -ENOENT) {
647 /* Page not found in guest PTE entries */
Alexander Graf5e030182010-07-29 14:47:45 +0200648 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Grafd562de42010-07-29 14:47:44 +0200649 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf666e7252010-07-29 14:47:43 +0200650 vcpu->arch.shared->msr |=
651 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000652 kvmppc_book3s_queue_irqprio(vcpu, vec);
653 } else if (page_found == -EPERM) {
654 /* Storage protection */
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 =
657 to_svcpu(vcpu)->fault_dsisr & ~DSISR_NOHPTE;
658 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
Alexander Graf666e7252010-07-29 14:47:43 +0200659 vcpu->arch.shared->msr |=
660 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000661 kvmppc_book3s_queue_irqprio(vcpu, vec);
662 } else if (page_found == -EINVAL) {
663 /* Page not found in guest SLB */
Alexander Graf5e030182010-07-29 14:47:45 +0200664 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000665 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
666 } else if (!is_mmio &&
667 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
668 /* The guest's PTE is not mapped yet. Map on the host */
669 kvmppc_mmu_map_page(vcpu, &pte);
670 if (data)
671 vcpu->stat.sp_storage++;
672 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
673 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
674 kvmppc_patch_dcbz(vcpu, &pte);
675 } else {
676 /* MMIO */
677 vcpu->stat.mmio_exits++;
678 vcpu->arch.paddr_accessed = pte.raddr;
679 r = kvmppc_emulate_mmio(run, vcpu);
680 if ( r == RESUME_HOST_NV )
681 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000682 }
683
684 return r;
685}
686
Alexander Graf180a34d2010-01-15 14:49:11 +0100687static inline int get_fpr_index(int i)
688{
689#ifdef CONFIG_VSX
690 i *= 2;
691#endif
692 return i;
693}
694
695/* Give up external provider (FPU, Altivec, VSX) */
Alexander Grafaba3bd72010-02-19 11:00:39 +0100696void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
Alexander Graf180a34d2010-01-15 14:49:11 +0100697{
698 struct thread_struct *t = &current->thread;
699 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100700#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100701 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100702#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100703 u64 *thread_fpr = (u64*)t->fpr;
704 int i;
705
706 if (!(vcpu->arch.guest_owned_ext & msr))
707 return;
708
709#ifdef DEBUG_EXT
710 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
711#endif
712
713 switch (msr) {
714 case MSR_FP:
715 giveup_fpu(current);
716 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
717 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
718
719 vcpu->arch.fpscr = t->fpscr.val;
720 break;
721 case MSR_VEC:
722#ifdef CONFIG_ALTIVEC
723 giveup_altivec(current);
724 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
725 vcpu->arch.vscr = t->vscr;
726#endif
727 break;
728 case MSR_VSX:
729#ifdef CONFIG_VSX
730 __giveup_vsx(current);
731 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
732 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
733#endif
734 break;
735 default:
736 BUG();
737 }
738
739 vcpu->arch.guest_owned_ext &= ~msr;
740 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100741 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100742}
743
Alexander Graf89632212010-03-24 21:48:21 +0100744static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100745{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200746 ulong srr0 = kvmppc_get_pc(vcpu);
747 u32 last_inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100748 int ret;
749
Alexander Grafc7f38f42010-04-16 00:11:40 +0200750 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100751 if (ret == -ENOENT) {
Alexander Graf666e7252010-07-29 14:47:43 +0200752 ulong msr = vcpu->arch.shared->msr;
753
754 msr = kvmppc_set_field(msr, 33, 33, 1);
755 msr = kvmppc_set_field(msr, 34, 36, 0);
756 vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100757 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
Alexander Graf89632212010-03-24 21:48:21 +0100758 return EMULATE_AGAIN;
759 }
760
761 return EMULATE_DONE;
762}
763
764static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
765{
766
767 /* Need to do paired single emulation? */
768 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
769 return EMULATE_DONE;
770
771 /* Read out the instruction */
772 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100773 /* Need to emulate */
774 return EMULATE_FAIL;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100775
776 return EMULATE_AGAIN;
777}
778
Alexander Graf180a34d2010-01-15 14:49:11 +0100779/* Handle external providers (FPU, Altivec, VSX) */
780static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
781 ulong msr)
782{
783 struct thread_struct *t = &current->thread;
784 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100785#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100786 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100787#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100788 u64 *thread_fpr = (u64*)t->fpr;
789 int i;
790
Alexander Graf3c402a72010-02-19 11:00:32 +0100791 /* When we have paired singles, we emulate in software */
792 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
793 return RESUME_GUEST;
794
Alexander Graf666e7252010-07-29 14:47:43 +0200795 if (!(vcpu->arch.shared->msr & msr)) {
Alexander Graf180a34d2010-01-15 14:49:11 +0100796 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
797 return RESUME_GUEST;
798 }
799
Alexander Grafc2453692010-03-24 21:48:22 +0100800 /* We already own the ext */
801 if (vcpu->arch.guest_owned_ext & msr) {
802 return RESUME_GUEST;
803 }
804
Alexander Graf180a34d2010-01-15 14:49:11 +0100805#ifdef DEBUG_EXT
806 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
807#endif
808
809 current->thread.regs->msr |= msr;
810
811 switch (msr) {
812 case MSR_FP:
813 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
814 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
815
816 t->fpscr.val = vcpu->arch.fpscr;
817 t->fpexc_mode = 0;
818 kvmppc_load_up_fpu();
819 break;
820 case MSR_VEC:
821#ifdef CONFIG_ALTIVEC
822 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
823 t->vscr = vcpu->arch.vscr;
824 t->vrsave = -1;
825 kvmppc_load_up_altivec();
826#endif
827 break;
828 case MSR_VSX:
829#ifdef CONFIG_VSX
830 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
831 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
832 kvmppc_load_up_vsx();
833#endif
834 break;
835 default:
836 BUG();
837 }
838
839 vcpu->arch.guest_owned_ext |= msr;
840
Alexander Grafa76f8492010-01-15 14:49:14 +0100841 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100842
843 return RESUME_GUEST;
844}
845
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000846int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
847 unsigned int exit_nr)
848{
849 int r = RESUME_HOST;
850
851 vcpu->stat.sum_exits++;
852
853 run->exit_reason = KVM_EXIT_UNKNOWN;
854 run->ready_for_interrupt_injection = 1;
Alexander Grafbed1ed92010-08-02 11:06:26 +0200855
856 trace_kvm_book3s_exit(exit_nr, vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000857 kvm_resched(vcpu);
858 switch (exit_nr) {
859 case BOOK3S_INTERRUPT_INST_STORAGE:
860 vcpu->stat.pf_instruc++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200861
862#ifdef CONFIG_PPC_BOOK3S_32
863 /* We set segments as unused segments when invalidating them. So
864 * treat the respective fault as segment fault. */
865 if (to_svcpu(vcpu)->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT]
866 == SR_INVALID) {
867 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
868 r = RESUME_GUEST;
869 break;
870 }
871#endif
872
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000873 /* only care about PTEG not found errors, but leave NX alone */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200874 if (to_svcpu(vcpu)->shadow_srr1 & 0x40000000) {
875 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000876 vcpu->stat.sp_instruc++;
877 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
878 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
879 /*
880 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
881 * so we can't use the NX bit inside the guest. Let's cross our fingers,
882 * that no guest that needs the dcbz hack does NX.
883 */
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200884 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100885 r = RESUME_GUEST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000886 } else {
Alexander Graf666e7252010-07-29 14:47:43 +0200887 vcpu->arch.shared->msr |=
888 to_svcpu(vcpu)->shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000889 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200890 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000891 r = RESUME_GUEST;
892 }
893 break;
894 case BOOK3S_INTERRUPT_DATA_STORAGE:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200895 {
896 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000897 vcpu->stat.pf_storage++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200898
899#ifdef CONFIG_PPC_BOOK3S_32
900 /* We set segments as unused segments when invalidating them. So
901 * treat the respective fault as segment fault. */
902 if ((to_svcpu(vcpu)->sr[dar >> SID_SHIFT]) == SR_INVALID) {
903 kvmppc_mmu_map_segment(vcpu, dar);
904 r = RESUME_GUEST;
905 break;
906 }
907#endif
908
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000909 /* The only case we need to handle is missing shadow PTEs */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200910 if (to_svcpu(vcpu)->fault_dsisr & DSISR_NOHPTE) {
911 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000912 } else {
Alexander Graf5e030182010-07-29 14:47:45 +0200913 vcpu->arch.shared->dar = dar;
Alexander Grafd562de42010-07-29 14:47:44 +0200914 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000915 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Graf5e030182010-07-29 14:47:45 +0200916 kvmppc_mmu_pte_flush(vcpu, dar, ~0xFFFUL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000917 r = RESUME_GUEST;
918 }
919 break;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200920 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000921 case BOOK3S_INTERRUPT_DATA_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200922 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
Alexander Graf5e030182010-07-29 14:47:45 +0200923 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000924 kvmppc_book3s_queue_irqprio(vcpu,
925 BOOK3S_INTERRUPT_DATA_SEGMENT);
926 }
927 r = RESUME_GUEST;
928 break;
929 case BOOK3S_INTERRUPT_INST_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200930 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000931 kvmppc_book3s_queue_irqprio(vcpu,
932 BOOK3S_INTERRUPT_INST_SEGMENT);
933 }
934 r = RESUME_GUEST;
935 break;
936 /* We're good on these - the host merely wanted to get our attention */
937 case BOOK3S_INTERRUPT_DECREMENTER:
938 vcpu->stat.dec_exits++;
939 r = RESUME_GUEST;
940 break;
941 case BOOK3S_INTERRUPT_EXTERNAL:
942 vcpu->stat.ext_intr_exits++;
943 r = RESUME_GUEST;
944 break;
Alexander Graf7fdaec92010-04-20 02:49:47 +0200945 case BOOK3S_INTERRUPT_PERFMON:
946 r = RESUME_GUEST;
947 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000948 case BOOK3S_INTERRUPT_PROGRAM:
949 {
950 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100951 ulong flags;
952
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100953program_interrupt:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200954 flags = to_svcpu(vcpu)->shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000955
Alexander Graf666e7252010-07-29 14:47:43 +0200956 if (vcpu->arch.shared->msr & MSR_PR) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000957#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +0200958 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 +0000959#endif
Alexander Grafc7f38f42010-04-16 00:11:40 +0200960 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000961 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100962 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000963 r = RESUME_GUEST;
964 break;
965 }
966 }
967
968 vcpu->stat.emulated_inst_exits++;
969 er = kvmppc_emulate_instruction(run, vcpu);
970 switch (er) {
971 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100972 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000973 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100974 case EMULATE_AGAIN:
975 r = RESUME_GUEST;
976 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000977 case EMULATE_FAIL:
978 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200979 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100980 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000981 r = RESUME_GUEST;
982 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +0100983 case EMULATE_DO_MMIO:
984 run->exit_reason = KVM_EXIT_MMIO;
985 r = RESUME_HOST_NV;
986 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000987 default:
988 BUG();
989 }
990 break;
991 }
992 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafad0a0482010-03-24 21:48:30 +0100993 if (vcpu->arch.osi_enabled &&
994 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
995 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
Alexander Graf2a342ed2010-07-29 14:47:48 +0200996 /* MOL hypercalls */
Alexander Grafad0a0482010-03-24 21:48:30 +0100997 u64 *gprs = run->osi.gprs;
998 int i;
999
1000 run->exit_reason = KVM_EXIT_OSI;
1001 for (i = 0; i < 32; i++)
1002 gprs[i] = kvmppc_get_gpr(vcpu, i);
1003 vcpu->arch.osi_needed = 1;
1004 r = RESUME_HOST_NV;
Alexander Graf2a342ed2010-07-29 14:47:48 +02001005 } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
1006 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
1007 /* KVM PV hypercalls */
1008 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1009 r = RESUME_GUEST;
Alexander Grafad0a0482010-03-24 21:48:30 +01001010 } else {
Alexander Graf2a342ed2010-07-29 14:47:48 +02001011 /* Guest syscalls */
Alexander Grafad0a0482010-03-24 21:48:30 +01001012 vcpu->stat.syscall_exits++;
1013 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1014 r = RESUME_GUEST;
1015 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001016 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001017 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001018 case BOOK3S_INTERRUPT_ALTIVEC:
1019 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +01001020 {
1021 int ext_msr = 0;
1022
1023 switch (exit_nr) {
1024 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
1025 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
1026 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
1027 }
1028
1029 switch (kvmppc_check_ext(vcpu, exit_nr)) {
1030 case EMULATE_DONE:
1031 /* everything ok - let's enable the ext */
1032 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
1033 break;
1034 case EMULATE_FAIL:
1035 /* we need to emulate this instruction */
1036 goto program_interrupt;
1037 break;
1038 default:
1039 /* nothing to worry about - go again */
1040 break;
1041 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001042 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +01001043 }
Alexander Grafca7f4202010-03-24 21:48:28 +01001044 case BOOK3S_INTERRUPT_ALIGNMENT:
1045 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
Alexander Grafd562de42010-07-29 14:47:44 +02001046 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001047 kvmppc_get_last_inst(vcpu));
Alexander Graf5e030182010-07-29 14:47:45 +02001048 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001049 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +01001050 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1051 }
1052 r = RESUME_GUEST;
1053 break;
Alexander Graf180a34d2010-01-15 14:49:11 +01001054 case BOOK3S_INTERRUPT_MACHINE_CHECK:
1055 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001056 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1057 r = RESUME_GUEST;
1058 break;
1059 default:
1060 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +01001061 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +02001062 exit_nr, kvmppc_get_pc(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001063 r = RESUME_HOST;
1064 BUG();
1065 break;
1066 }
1067
1068
1069 if (!(r & RESUME_HOST)) {
1070 /* To avoid clobbering exit_reason, only check for signals if
1071 * we aren't already exiting to userspace for some other
1072 * reason. */
1073 if (signal_pending(current)) {
1074#ifdef EXIT_DEBUG
1075 printk(KERN_EMERG "KVM: Going back to host\n");
1076#endif
1077 vcpu->stat.signal_exits++;
1078 run->exit_reason = KVM_EXIT_INTR;
1079 r = -EINTR;
1080 } else {
1081 /* In case an interrupt came in that was triggered
1082 * from userspace (like DEC), we need to check what
1083 * to inject now! */
1084 kvmppc_core_deliver_interrupts(vcpu);
1085 }
1086 }
1087
Alexander Grafbed1ed92010-08-02 11:06:26 +02001088 trace_kvm_book3s_reenter(r, vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001089
1090 return r;
1091}
1092
1093int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1094{
1095 return 0;
1096}
1097
1098int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1099{
1100 int i;
1101
Alexander Grafc7f38f42010-04-16 00:11:40 +02001102 regs->pc = kvmppc_get_pc(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001103 regs->cr = kvmppc_get_cr(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001104 regs->ctr = kvmppc_get_ctr(vcpu);
1105 regs->lr = kvmppc_get_lr(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001106 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001107 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +02001108 regs->srr0 = vcpu->arch.shared->srr0;
1109 regs->srr1 = vcpu->arch.shared->srr1;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001110 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001111 regs->sprg0 = vcpu->arch.shared->sprg0;
1112 regs->sprg1 = vcpu->arch.shared->sprg1;
1113 regs->sprg2 = vcpu->arch.shared->sprg2;
1114 regs->sprg3 = vcpu->arch.shared->sprg3;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001115 regs->sprg5 = vcpu->arch.sprg4;
1116 regs->sprg6 = vcpu->arch.sprg5;
1117 regs->sprg7 = vcpu->arch.sprg6;
1118
1119 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001120 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001121
1122 return 0;
1123}
1124
1125int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1126{
1127 int i;
1128
Alexander Grafc7f38f42010-04-16 00:11:40 +02001129 kvmppc_set_pc(vcpu, regs->pc);
Alexander Graf992b5b22010-01-08 02:58:02 +01001130 kvmppc_set_cr(vcpu, regs->cr);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001131 kvmppc_set_ctr(vcpu, regs->ctr);
1132 kvmppc_set_lr(vcpu, regs->lr);
Alexander Graf992b5b22010-01-08 02:58:02 +01001133 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001134 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +02001135 vcpu->arch.shared->srr0 = regs->srr0;
1136 vcpu->arch.shared->srr1 = regs->srr1;
Alexander Grafa73a9592010-07-29 14:47:47 +02001137 vcpu->arch.shared->sprg0 = regs->sprg0;
1138 vcpu->arch.shared->sprg1 = regs->sprg1;
1139 vcpu->arch.shared->sprg2 = regs->sprg2;
1140 vcpu->arch.shared->sprg3 = regs->sprg3;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001141 vcpu->arch.sprg5 = regs->sprg4;
1142 vcpu->arch.sprg6 = regs->sprg5;
1143 vcpu->arch.sprg7 = regs->sprg6;
1144
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001145 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1146 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001147
1148 return 0;
1149}
1150
1151int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1152 struct kvm_sregs *sregs)
1153{
Alexander Grafe15a1132009-11-30 03:02:02 +00001154 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1155 int i;
1156
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001157 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +00001158
1159 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1160 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1161 for (i = 0; i < 64; i++) {
1162 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
1163 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1164 }
1165 } else {
1166 for (i = 0; i < 16; i++) {
1167 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1168 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1169 }
1170 for (i = 0; i < 8; i++) {
1171 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1172 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1173 }
1174 }
Avi Kivity98001d82010-05-13 11:05:49 +03001175
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001176 return 0;
1177}
1178
1179int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1180 struct kvm_sregs *sregs)
1181{
Alexander Grafe15a1132009-11-30 03:02:02 +00001182 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1183 int i;
1184
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001185 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001186
1187 vcpu3s->sdr1 = sregs->u.s.sdr1;
1188 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1189 for (i = 0; i < 64; i++) {
1190 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1191 sregs->u.s.ppc64.slb[i].slbe);
1192 }
1193 } else {
1194 for (i = 0; i < 16; i++) {
1195 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1196 }
1197 for (i = 0; i < 8; i++) {
1198 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1199 (u32)sregs->u.s.ppc32.ibat[i]);
1200 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1201 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1202 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1203 (u32)sregs->u.s.ppc32.dbat[i]);
1204 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1205 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1206 }
1207 }
1208
1209 /* Flush the MMU after messing with the segments */
1210 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Avi Kivity98001d82010-05-13 11:05:49 +03001211
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001212 return 0;
1213}
1214
1215int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1216{
1217 return -ENOTSUPP;
1218}
1219
1220int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1221{
1222 return -ENOTSUPP;
1223}
1224
1225int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1226 struct kvm_translation *tr)
1227{
1228 return 0;
1229}
1230
1231/*
1232 * Get (and clear) the dirty memory log for a memory slot.
1233 */
1234int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1235 struct kvm_dirty_log *log)
1236{
1237 struct kvm_memory_slot *memslot;
1238 struct kvm_vcpu *vcpu;
1239 ulong ga, ga_end;
1240 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001241 int r;
1242 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001243
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001244 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001245
1246 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1247 if (r)
1248 goto out;
1249
1250 /* If nothing is dirty, don't bother messing with page tables. */
1251 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001252 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001253
1254 ga = memslot->base_gfn << PAGE_SHIFT;
1255 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1256
1257 kvm_for_each_vcpu(n, vcpu, kvm)
1258 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1259
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001260 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001261 memset(memslot->dirty_bitmap, 0, n);
1262 }
1263
1264 r = 0;
1265out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001266 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001267 return r;
1268}
1269
1270int kvmppc_core_check_processor_compat(void)
1271{
1272 return 0;
1273}
1274
1275struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1276{
1277 struct kvmppc_vcpu_book3s *vcpu_book3s;
1278 struct kvm_vcpu *vcpu;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001279 int err = -ENOMEM;
Alexander Grafe8508942010-07-29 14:47:54 +02001280 unsigned long p;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001281
Alexander Graf032c3402010-02-19 12:24:33 +01001282 vcpu_book3s = vmalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Grafc7f38f42010-04-16 00:11:40 +02001283 if (!vcpu_book3s)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001284 goto out;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001285
Alexander Graf7e821d32010-02-22 16:52:08 +01001286 memset(vcpu_book3s, 0, sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001287
Alexander Grafc7f38f42010-04-16 00:11:40 +02001288 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
1289 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
1290 if (!vcpu_book3s->shadow_vcpu)
1291 goto free_vcpu;
1292
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001293 vcpu = &vcpu_book3s->vcpu;
1294 err = kvm_vcpu_init(vcpu, kvm, id);
1295 if (err)
Alexander Grafc7f38f42010-04-16 00:11:40 +02001296 goto free_shadow_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001297
Alexander Grafe8508942010-07-29 14:47:54 +02001298 p = __get_free_page(GFP_KERNEL|__GFP_ZERO);
1299 /* the real shared page fills the last 4k of our page */
1300 vcpu->arch.shared = (void*)(p + PAGE_SIZE - 4096);
1301 if (!p)
Alexander Graf96bc4512010-07-29 14:47:42 +02001302 goto uninit_vcpu;
1303
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001304 vcpu->arch.host_retip = kvm_return_point;
1305 vcpu->arch.host_msr = mfmsr();
Alexander Graf07b09072010-04-16 00:11:53 +02001306#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001307 /* default to book3s_64 (970fx) */
1308 vcpu->arch.pvr = 0x3C0301;
Alexander Graf07b09072010-04-16 00:11:53 +02001309#else
1310 /* default to book3s_32 (750) */
1311 vcpu->arch.pvr = 0x84202;
1312#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001313 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1314 vcpu_book3s->slb_nr = 64;
1315
1316 /* remember where some real-mode handlers are */
1317 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1318 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1319 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf07b09072010-04-16 00:11:53 +02001320#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf021ec9c2010-01-08 02:58:06 +01001321 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf07b09072010-04-16 00:11:53 +02001322#else
1323 vcpu->arch.rmcall = (ulong)kvmppc_rmcall;
1324#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001325
1326 vcpu->arch.shadow_msr = MSR_USER64;
1327
Alexander Graf9cc5e952010-04-16 00:11:45 +02001328 err = kvmppc_mmu_init(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001329 if (err < 0)
Alexander Graf96bc4512010-07-29 14:47:42 +02001330 goto uninit_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001331
1332 return vcpu;
1333
Alexander Graf96bc4512010-07-29 14:47:42 +02001334uninit_vcpu:
1335 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001336free_shadow_vcpu:
1337 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001338free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001339 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001340out:
1341 return ERR_PTR(err);
1342}
1343
1344void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1345{
1346 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1347
Alexander Grafe8508942010-07-29 14:47:54 +02001348 free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001349 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001350 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001351 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001352}
1353
1354extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1355int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1356{
1357 int ret;
Andreas Schwab49f6be82010-05-31 21:59:13 +02001358 double fpr[32][TS_FPRWIDTH];
1359 unsigned int fpscr;
1360 int fpexc_mode;
Alexander Grafa2b07662010-03-24 21:48:31 +01001361#ifdef CONFIG_ALTIVEC
Andreas Schwab49f6be82010-05-31 21:59:13 +02001362 vector128 vr[32];
1363 vector128 vscr;
1364 unsigned long uninitialized_var(vrsave);
1365 int used_vr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001366#endif
1367#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001368 int used_vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001369#endif
Alexander Graf180a34d2010-01-15 14:49:11 +01001370 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001371
1372 /* No need to go into the guest when all we do is going out */
1373 if (signal_pending(current)) {
1374 kvm_run->exit_reason = KVM_EXIT_INTR;
1375 return -EINTR;
1376 }
1377
Alexander Graf180a34d2010-01-15 14:49:11 +01001378 /* Save FPU state in stack */
1379 if (current->thread.regs->msr & MSR_FP)
1380 giveup_fpu(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001381 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
1382 fpscr = current->thread.fpscr.val;
1383 fpexc_mode = current->thread.fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001384
1385#ifdef CONFIG_ALTIVEC
1386 /* Save Altivec state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001387 used_vr = current->thread.used_vr;
1388 if (used_vr) {
Alexander Graf180a34d2010-01-15 14:49:11 +01001389 if (current->thread.regs->msr & MSR_VEC)
1390 giveup_altivec(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001391 memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
1392 vscr = current->thread.vscr;
1393 vrsave = current->thread.vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001394 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001395#endif
1396
1397#ifdef CONFIG_VSX
1398 /* Save VSX state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001399 used_vsr = current->thread.used_vsr;
1400 if (used_vsr && (current->thread.regs->msr & MSR_VSX))
Alexander Graf180a34d2010-01-15 14:49:11 +01001401 __giveup_vsx(current);
Alexander Graf180a34d2010-01-15 14:49:11 +01001402#endif
1403
1404 /* Remember the MSR with disabled extensions */
1405 ext_msr = current->thread.regs->msr;
1406
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001407 /* XXX we get called with irq disabled - change that! */
1408 local_irq_enable();
1409
Alexander Grafd1bab742010-02-19 11:00:35 +01001410 /* Preload FPU if it's enabled */
Alexander Graf666e7252010-07-29 14:47:43 +02001411 if (vcpu->arch.shared->msr & MSR_FP)
Alexander Grafd1bab742010-02-19 11:00:35 +01001412 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1413
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001414 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1415
1416 local_irq_disable();
1417
Alexander Graf180a34d2010-01-15 14:49:11 +01001418 current->thread.regs->msr = ext_msr;
1419
1420 /* Make sure we save the guest FPU/Altivec/VSX state */
1421 kvmppc_giveup_ext(vcpu, MSR_FP);
1422 kvmppc_giveup_ext(vcpu, MSR_VEC);
1423 kvmppc_giveup_ext(vcpu, MSR_VSX);
1424
1425 /* Restore FPU state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001426 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
1427 current->thread.fpscr.val = fpscr;
1428 current->thread.fpexc_mode = fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001429
1430#ifdef CONFIG_ALTIVEC
1431 /* Restore Altivec state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001432 if (used_vr && current->thread.used_vr) {
1433 memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
1434 current->thread.vscr = vscr;
1435 current->thread.vrsave = vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001436 }
Andreas Schwab49f6be82010-05-31 21:59:13 +02001437 current->thread.used_vr = used_vr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001438#endif
1439
1440#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001441 current->thread.used_vsr = used_vsr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001442#endif
1443
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001444 return ret;
1445}
1446
1447static int kvmppc_book3s_init(void)
1448{
Alexander Graffef093be2010-06-30 15:18:46 +02001449 int r;
1450
1451 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
1452 THIS_MODULE);
1453
1454 if (r)
1455 return r;
1456
1457 r = kvmppc_mmu_hpte_sysinit();
1458
1459 return r;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001460}
1461
1462static void kvmppc_book3s_exit(void)
1463{
Alexander Graffef093be2010-06-30 15:18:46 +02001464 kvmppc_mmu_hpte_sysexit();
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001465 kvm_exit();
1466}
1467
1468module_init(kvmppc_book3s_init);
1469module_exit(kvmppc_book3s_exit);