blob: 02a9cb165d53cff3e8c373fdd3dc6d5c42bbfae2 [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 Graf2f4cf5e2009-10-30 05:47:10 +0000890 r = RESUME_GUEST;
891 }
892 break;
893 case BOOK3S_INTERRUPT_DATA_STORAGE:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200894 {
895 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000896 vcpu->stat.pf_storage++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200897
898#ifdef CONFIG_PPC_BOOK3S_32
899 /* We set segments as unused segments when invalidating them. So
900 * treat the respective fault as segment fault. */
901 if ((to_svcpu(vcpu)->sr[dar >> SID_SHIFT]) == SR_INVALID) {
902 kvmppc_mmu_map_segment(vcpu, dar);
903 r = RESUME_GUEST;
904 break;
905 }
906#endif
907
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000908 /* The only case we need to handle is missing shadow PTEs */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200909 if (to_svcpu(vcpu)->fault_dsisr & DSISR_NOHPTE) {
910 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000911 } else {
Alexander Graf5e030182010-07-29 14:47:45 +0200912 vcpu->arch.shared->dar = dar;
Alexander Grafd562de42010-07-29 14:47:44 +0200913 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000914 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000915 r = RESUME_GUEST;
916 }
917 break;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200918 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000919 case BOOK3S_INTERRUPT_DATA_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200920 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
Alexander Graf5e030182010-07-29 14:47:45 +0200921 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000922 kvmppc_book3s_queue_irqprio(vcpu,
923 BOOK3S_INTERRUPT_DATA_SEGMENT);
924 }
925 r = RESUME_GUEST;
926 break;
927 case BOOK3S_INTERRUPT_INST_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200928 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000929 kvmppc_book3s_queue_irqprio(vcpu,
930 BOOK3S_INTERRUPT_INST_SEGMENT);
931 }
932 r = RESUME_GUEST;
933 break;
934 /* We're good on these - the host merely wanted to get our attention */
935 case BOOK3S_INTERRUPT_DECREMENTER:
936 vcpu->stat.dec_exits++;
937 r = RESUME_GUEST;
938 break;
939 case BOOK3S_INTERRUPT_EXTERNAL:
940 vcpu->stat.ext_intr_exits++;
941 r = RESUME_GUEST;
942 break;
Alexander Graf7fdaec92010-04-20 02:49:47 +0200943 case BOOK3S_INTERRUPT_PERFMON:
944 r = RESUME_GUEST;
945 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000946 case BOOK3S_INTERRUPT_PROGRAM:
947 {
948 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100949 ulong flags;
950
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100951program_interrupt:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200952 flags = to_svcpu(vcpu)->shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000953
Alexander Graf666e7252010-07-29 14:47:43 +0200954 if (vcpu->arch.shared->msr & MSR_PR) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000955#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +0200956 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 +0000957#endif
Alexander Grafc7f38f42010-04-16 00:11:40 +0200958 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000959 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100960 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000961 r = RESUME_GUEST;
962 break;
963 }
964 }
965
966 vcpu->stat.emulated_inst_exits++;
967 er = kvmppc_emulate_instruction(run, vcpu);
968 switch (er) {
969 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100970 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000971 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100972 case EMULATE_AGAIN:
973 r = RESUME_GUEST;
974 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000975 case EMULATE_FAIL:
976 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200977 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100978 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000979 r = RESUME_GUEST;
980 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +0100981 case EMULATE_DO_MMIO:
982 run->exit_reason = KVM_EXIT_MMIO;
983 r = RESUME_HOST_NV;
984 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000985 default:
986 BUG();
987 }
988 break;
989 }
990 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafad0a0482010-03-24 21:48:30 +0100991 if (vcpu->arch.osi_enabled &&
992 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
993 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
Alexander Graf2a342ed2010-07-29 14:47:48 +0200994 /* MOL hypercalls */
Alexander Grafad0a0482010-03-24 21:48:30 +0100995 u64 *gprs = run->osi.gprs;
996 int i;
997
998 run->exit_reason = KVM_EXIT_OSI;
999 for (i = 0; i < 32; i++)
1000 gprs[i] = kvmppc_get_gpr(vcpu, i);
1001 vcpu->arch.osi_needed = 1;
1002 r = RESUME_HOST_NV;
Alexander Graf2a342ed2010-07-29 14:47:48 +02001003 } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
1004 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
1005 /* KVM PV hypercalls */
1006 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1007 r = RESUME_GUEST;
Alexander Grafad0a0482010-03-24 21:48:30 +01001008 } else {
Alexander Graf2a342ed2010-07-29 14:47:48 +02001009 /* Guest syscalls */
Alexander Grafad0a0482010-03-24 21:48:30 +01001010 vcpu->stat.syscall_exits++;
1011 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1012 r = RESUME_GUEST;
1013 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001014 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001015 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001016 case BOOK3S_INTERRUPT_ALTIVEC:
1017 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +01001018 {
1019 int ext_msr = 0;
1020
1021 switch (exit_nr) {
1022 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
1023 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
1024 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
1025 }
1026
1027 switch (kvmppc_check_ext(vcpu, exit_nr)) {
1028 case EMULATE_DONE:
1029 /* everything ok - let's enable the ext */
1030 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
1031 break;
1032 case EMULATE_FAIL:
1033 /* we need to emulate this instruction */
1034 goto program_interrupt;
1035 break;
1036 default:
1037 /* nothing to worry about - go again */
1038 break;
1039 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001040 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +01001041 }
Alexander Grafca7f4202010-03-24 21:48:28 +01001042 case BOOK3S_INTERRUPT_ALIGNMENT:
1043 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
Alexander Grafd562de42010-07-29 14:47:44 +02001044 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001045 kvmppc_get_last_inst(vcpu));
Alexander Graf5e030182010-07-29 14:47:45 +02001046 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001047 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +01001048 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1049 }
1050 r = RESUME_GUEST;
1051 break;
Alexander Graf180a34d2010-01-15 14:49:11 +01001052 case BOOK3S_INTERRUPT_MACHINE_CHECK:
1053 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001054 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1055 r = RESUME_GUEST;
1056 break;
1057 default:
1058 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +01001059 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +02001060 exit_nr, kvmppc_get_pc(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001061 r = RESUME_HOST;
1062 BUG();
1063 break;
1064 }
1065
1066
1067 if (!(r & RESUME_HOST)) {
1068 /* To avoid clobbering exit_reason, only check for signals if
1069 * we aren't already exiting to userspace for some other
1070 * reason. */
1071 if (signal_pending(current)) {
1072#ifdef EXIT_DEBUG
1073 printk(KERN_EMERG "KVM: Going back to host\n");
1074#endif
1075 vcpu->stat.signal_exits++;
1076 run->exit_reason = KVM_EXIT_INTR;
1077 r = -EINTR;
1078 } else {
1079 /* In case an interrupt came in that was triggered
1080 * from userspace (like DEC), we need to check what
1081 * to inject now! */
1082 kvmppc_core_deliver_interrupts(vcpu);
1083 }
1084 }
1085
Alexander Grafbed1ed92010-08-02 11:06:26 +02001086 trace_kvm_book3s_reenter(r, vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001087
1088 return r;
1089}
1090
1091int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1092{
1093 return 0;
1094}
1095
1096int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1097{
1098 int i;
1099
Alexander Grafc7f38f42010-04-16 00:11:40 +02001100 regs->pc = kvmppc_get_pc(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001101 regs->cr = kvmppc_get_cr(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001102 regs->ctr = kvmppc_get_ctr(vcpu);
1103 regs->lr = kvmppc_get_lr(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001104 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001105 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +02001106 regs->srr0 = vcpu->arch.shared->srr0;
1107 regs->srr1 = vcpu->arch.shared->srr1;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001108 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001109 regs->sprg0 = vcpu->arch.shared->sprg0;
1110 regs->sprg1 = vcpu->arch.shared->sprg1;
1111 regs->sprg2 = vcpu->arch.shared->sprg2;
1112 regs->sprg3 = vcpu->arch.shared->sprg3;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001113 regs->sprg5 = vcpu->arch.sprg4;
1114 regs->sprg6 = vcpu->arch.sprg5;
1115 regs->sprg7 = vcpu->arch.sprg6;
1116
1117 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001118 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001119
1120 return 0;
1121}
1122
1123int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1124{
1125 int i;
1126
Alexander Grafc7f38f42010-04-16 00:11:40 +02001127 kvmppc_set_pc(vcpu, regs->pc);
Alexander Graf992b5b22010-01-08 02:58:02 +01001128 kvmppc_set_cr(vcpu, regs->cr);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001129 kvmppc_set_ctr(vcpu, regs->ctr);
1130 kvmppc_set_lr(vcpu, regs->lr);
Alexander Graf992b5b22010-01-08 02:58:02 +01001131 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001132 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +02001133 vcpu->arch.shared->srr0 = regs->srr0;
1134 vcpu->arch.shared->srr1 = regs->srr1;
Alexander Grafa73a9592010-07-29 14:47:47 +02001135 vcpu->arch.shared->sprg0 = regs->sprg0;
1136 vcpu->arch.shared->sprg1 = regs->sprg1;
1137 vcpu->arch.shared->sprg2 = regs->sprg2;
1138 vcpu->arch.shared->sprg3 = regs->sprg3;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001139 vcpu->arch.sprg5 = regs->sprg4;
1140 vcpu->arch.sprg6 = regs->sprg5;
1141 vcpu->arch.sprg7 = regs->sprg6;
1142
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001143 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1144 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001145
1146 return 0;
1147}
1148
1149int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1150 struct kvm_sregs *sregs)
1151{
Alexander Grafe15a1132009-11-30 03:02:02 +00001152 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1153 int i;
1154
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001155 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +00001156
1157 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1158 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1159 for (i = 0; i < 64; i++) {
1160 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
1161 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1162 }
1163 } else {
Alexander Grafdf1bfa22010-08-03 02:29:27 +02001164 for (i = 0; i < 16; i++)
1165 sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i];
1166
Alexander Grafe15a1132009-11-30 03:02:02 +00001167 for (i = 0; i < 8; i++) {
1168 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1169 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1170 }
1171 }
Avi Kivity98001d82010-05-13 11:05:49 +03001172
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001173 return 0;
1174}
1175
1176int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1177 struct kvm_sregs *sregs)
1178{
Alexander Grafe15a1132009-11-30 03:02:02 +00001179 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1180 int i;
1181
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001182 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001183
1184 vcpu3s->sdr1 = sregs->u.s.sdr1;
1185 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1186 for (i = 0; i < 64; i++) {
1187 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1188 sregs->u.s.ppc64.slb[i].slbe);
1189 }
1190 } else {
1191 for (i = 0; i < 16; i++) {
1192 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1193 }
1194 for (i = 0; i < 8; i++) {
1195 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1196 (u32)sregs->u.s.ppc32.ibat[i]);
1197 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1198 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1199 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1200 (u32)sregs->u.s.ppc32.dbat[i]);
1201 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1202 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1203 }
1204 }
1205
1206 /* Flush the MMU after messing with the segments */
1207 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Avi Kivity98001d82010-05-13 11:05:49 +03001208
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001209 return 0;
1210}
1211
1212int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1213{
1214 return -ENOTSUPP;
1215}
1216
1217int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1218{
1219 return -ENOTSUPP;
1220}
1221
1222int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1223 struct kvm_translation *tr)
1224{
1225 return 0;
1226}
1227
1228/*
1229 * Get (and clear) the dirty memory log for a memory slot.
1230 */
1231int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1232 struct kvm_dirty_log *log)
1233{
1234 struct kvm_memory_slot *memslot;
1235 struct kvm_vcpu *vcpu;
1236 ulong ga, ga_end;
1237 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001238 int r;
1239 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001240
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001241 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001242
1243 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1244 if (r)
1245 goto out;
1246
1247 /* If nothing is dirty, don't bother messing with page tables. */
1248 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001249 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001250
1251 ga = memslot->base_gfn << PAGE_SHIFT;
1252 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1253
1254 kvm_for_each_vcpu(n, vcpu, kvm)
1255 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1256
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001257 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001258 memset(memslot->dirty_bitmap, 0, n);
1259 }
1260
1261 r = 0;
1262out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001263 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001264 return r;
1265}
1266
1267int kvmppc_core_check_processor_compat(void)
1268{
1269 return 0;
1270}
1271
1272struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1273{
1274 struct kvmppc_vcpu_book3s *vcpu_book3s;
1275 struct kvm_vcpu *vcpu;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001276 int err = -ENOMEM;
Alexander Grafe8508942010-07-29 14:47:54 +02001277 unsigned long p;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001278
Alexander Graf032c3402010-02-19 12:24:33 +01001279 vcpu_book3s = vmalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Grafc7f38f42010-04-16 00:11:40 +02001280 if (!vcpu_book3s)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001281 goto out;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001282
Alexander Graf7e821d32010-02-22 16:52:08 +01001283 memset(vcpu_book3s, 0, sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001284
Alexander Grafc7f38f42010-04-16 00:11:40 +02001285 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
1286 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
1287 if (!vcpu_book3s->shadow_vcpu)
1288 goto free_vcpu;
1289
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001290 vcpu = &vcpu_book3s->vcpu;
1291 err = kvm_vcpu_init(vcpu, kvm, id);
1292 if (err)
Alexander Grafc7f38f42010-04-16 00:11:40 +02001293 goto free_shadow_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001294
Alexander Grafe8508942010-07-29 14:47:54 +02001295 p = __get_free_page(GFP_KERNEL|__GFP_ZERO);
1296 /* the real shared page fills the last 4k of our page */
1297 vcpu->arch.shared = (void*)(p + PAGE_SIZE - 4096);
1298 if (!p)
Alexander Graf96bc4512010-07-29 14:47:42 +02001299 goto uninit_vcpu;
1300
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001301 vcpu->arch.host_retip = kvm_return_point;
1302 vcpu->arch.host_msr = mfmsr();
Alexander Graf07b09072010-04-16 00:11:53 +02001303#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001304 /* default to book3s_64 (970fx) */
1305 vcpu->arch.pvr = 0x3C0301;
Alexander Graf07b09072010-04-16 00:11:53 +02001306#else
1307 /* default to book3s_32 (750) */
1308 vcpu->arch.pvr = 0x84202;
1309#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001310 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1311 vcpu_book3s->slb_nr = 64;
1312
1313 /* remember where some real-mode handlers are */
1314 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1315 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1316 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf07b09072010-04-16 00:11:53 +02001317#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf021ec9c2010-01-08 02:58:06 +01001318 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf07b09072010-04-16 00:11:53 +02001319#else
1320 vcpu->arch.rmcall = (ulong)kvmppc_rmcall;
1321#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001322
1323 vcpu->arch.shadow_msr = MSR_USER64;
1324
Alexander Graf9cc5e952010-04-16 00:11:45 +02001325 err = kvmppc_mmu_init(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001326 if (err < 0)
Alexander Graf96bc4512010-07-29 14:47:42 +02001327 goto uninit_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001328
1329 return vcpu;
1330
Alexander Graf96bc4512010-07-29 14:47:42 +02001331uninit_vcpu:
1332 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001333free_shadow_vcpu:
1334 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001335free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001336 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001337out:
1338 return ERR_PTR(err);
1339}
1340
1341void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1342{
1343 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1344
Alexander Grafe8508942010-07-29 14:47:54 +02001345 free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001346 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001347 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001348 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001349}
1350
1351extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1352int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1353{
1354 int ret;
Andreas Schwab49f6be82010-05-31 21:59:13 +02001355 double fpr[32][TS_FPRWIDTH];
1356 unsigned int fpscr;
1357 int fpexc_mode;
Alexander Grafa2b07662010-03-24 21:48:31 +01001358#ifdef CONFIG_ALTIVEC
Andreas Schwab49f6be82010-05-31 21:59:13 +02001359 vector128 vr[32];
1360 vector128 vscr;
1361 unsigned long uninitialized_var(vrsave);
1362 int used_vr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001363#endif
1364#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001365 int used_vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001366#endif
Alexander Graf180a34d2010-01-15 14:49:11 +01001367 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001368
1369 /* No need to go into the guest when all we do is going out */
1370 if (signal_pending(current)) {
1371 kvm_run->exit_reason = KVM_EXIT_INTR;
1372 return -EINTR;
1373 }
1374
Alexander Graf180a34d2010-01-15 14:49:11 +01001375 /* Save FPU state in stack */
1376 if (current->thread.regs->msr & MSR_FP)
1377 giveup_fpu(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001378 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
1379 fpscr = current->thread.fpscr.val;
1380 fpexc_mode = current->thread.fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001381
1382#ifdef CONFIG_ALTIVEC
1383 /* Save Altivec state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001384 used_vr = current->thread.used_vr;
1385 if (used_vr) {
Alexander Graf180a34d2010-01-15 14:49:11 +01001386 if (current->thread.regs->msr & MSR_VEC)
1387 giveup_altivec(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001388 memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
1389 vscr = current->thread.vscr;
1390 vrsave = current->thread.vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001391 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001392#endif
1393
1394#ifdef CONFIG_VSX
1395 /* Save VSX state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001396 used_vsr = current->thread.used_vsr;
1397 if (used_vsr && (current->thread.regs->msr & MSR_VSX))
Alexander Graf180a34d2010-01-15 14:49:11 +01001398 __giveup_vsx(current);
Alexander Graf180a34d2010-01-15 14:49:11 +01001399#endif
1400
1401 /* Remember the MSR with disabled extensions */
1402 ext_msr = current->thread.regs->msr;
1403
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001404 /* XXX we get called with irq disabled - change that! */
1405 local_irq_enable();
1406
Alexander Grafd1bab742010-02-19 11:00:35 +01001407 /* Preload FPU if it's enabled */
Alexander Graf666e7252010-07-29 14:47:43 +02001408 if (vcpu->arch.shared->msr & MSR_FP)
Alexander Grafd1bab742010-02-19 11:00:35 +01001409 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1410
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001411 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1412
1413 local_irq_disable();
1414
Alexander Graf180a34d2010-01-15 14:49:11 +01001415 current->thread.regs->msr = ext_msr;
1416
1417 /* Make sure we save the guest FPU/Altivec/VSX state */
1418 kvmppc_giveup_ext(vcpu, MSR_FP);
1419 kvmppc_giveup_ext(vcpu, MSR_VEC);
1420 kvmppc_giveup_ext(vcpu, MSR_VSX);
1421
1422 /* Restore FPU state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001423 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
1424 current->thread.fpscr.val = fpscr;
1425 current->thread.fpexc_mode = fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001426
1427#ifdef CONFIG_ALTIVEC
1428 /* Restore Altivec state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001429 if (used_vr && current->thread.used_vr) {
1430 memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
1431 current->thread.vscr = vscr;
1432 current->thread.vrsave = vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001433 }
Andreas Schwab49f6be82010-05-31 21:59:13 +02001434 current->thread.used_vr = used_vr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001435#endif
1436
1437#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001438 current->thread.used_vsr = used_vsr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001439#endif
1440
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001441 return ret;
1442}
1443
1444static int kvmppc_book3s_init(void)
1445{
Alexander Graffef093be2010-06-30 15:18:46 +02001446 int r;
1447
1448 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
1449 THIS_MODULE);
1450
1451 if (r)
1452 return r;
1453
1454 r = kvmppc_mmu_hpte_sysinit();
1455
1456 return r;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001457}
1458
1459static void kvmppc_book3s_exit(void)
1460{
Alexander Graffef093be2010-06-30 15:18:46 +02001461 kvmppc_mmu_hpte_sysexit();
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001462 kvm_exit();
1463}
1464
1465module_init(kvmppc_book3s_init);
1466module_exit(kvmppc_book3s_exit);