blob: 7adea6320654e9ee539047ac366db933ab4200d1 [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);
Alexander Graf9ee18b12010-08-05 12:24:40 +0200204
205 if (!vcpu->arch.pending_exceptions)
206 vcpu->arch.shared->int_pending = 0;
Alexander Graf7706664d2009-12-21 20:21:24 +0100207}
208
Alexander Graf583617b2009-12-21 20:21:23 +0100209void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
210{
211 vcpu->stat.queue_intr++;
212
213 set_bit(kvmppc_book3s_vec2irqprio(vec),
214 &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000215#ifdef EXIT_DEBUG
216 printk(KERN_INFO "Queueing interrupt %x\n", vec);
217#endif
218}
219
220
Alexander Graf25a8a022010-01-08 02:58:07 +0100221void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000222{
Alexander Graf25a8a022010-01-08 02:58:07 +0100223 to_book3s(vcpu)->prog_flags = flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000224 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM);
225}
226
227void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
228{
229 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
230}
231
232int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
233{
234 return test_bit(BOOK3S_INTERRUPT_DECREMENTER >> 7, &vcpu->arch.pending_exceptions);
235}
236
Alexander Graf7706664d2009-12-21 20:21:24 +0100237void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
238{
239 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
240}
241
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000242void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
243 struct kvm_interrupt *irq)
244{
245 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
246}
247
Alexander Graf18978762010-03-24 21:48:18 +0100248void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
249 struct kvm_interrupt *irq)
250{
251 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
252}
253
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000254int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
255{
256 int deliver = 1;
257 int vec = 0;
Alexander Graf25a8a022010-01-08 02:58:07 +0100258 ulong flags = 0ULL;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200259 ulong crit_raw = vcpu->arch.shared->critical;
260 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
261 bool crit;
262
263 /* Truncate crit indicators in 32 bit mode */
264 if (!(vcpu->arch.shared->msr & MSR_SF)) {
265 crit_raw &= 0xffffffff;
266 crit_r1 &= 0xffffffff;
267 }
268
269 /* Critical section when crit == r1 */
270 crit = (crit_raw == crit_r1);
271 /* ... and we're in supervisor mode */
272 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000273
274 switch (priority) {
275 case BOOK3S_IRQPRIO_DECREMENTER:
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200276 deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000277 vec = BOOK3S_INTERRUPT_DECREMENTER;
278 break;
279 case BOOK3S_IRQPRIO_EXTERNAL:
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200280 deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000281 vec = BOOK3S_INTERRUPT_EXTERNAL;
282 break;
283 case BOOK3S_IRQPRIO_SYSTEM_RESET:
284 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
285 break;
286 case BOOK3S_IRQPRIO_MACHINE_CHECK:
287 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
288 break;
289 case BOOK3S_IRQPRIO_DATA_STORAGE:
290 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
291 break;
292 case BOOK3S_IRQPRIO_INST_STORAGE:
293 vec = BOOK3S_INTERRUPT_INST_STORAGE;
294 break;
295 case BOOK3S_IRQPRIO_DATA_SEGMENT:
296 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
297 break;
298 case BOOK3S_IRQPRIO_INST_SEGMENT:
299 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
300 break;
301 case BOOK3S_IRQPRIO_ALIGNMENT:
302 vec = BOOK3S_INTERRUPT_ALIGNMENT;
303 break;
304 case BOOK3S_IRQPRIO_PROGRAM:
305 vec = BOOK3S_INTERRUPT_PROGRAM;
Alexander Graf25a8a022010-01-08 02:58:07 +0100306 flags = to_book3s(vcpu)->prog_flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000307 break;
308 case BOOK3S_IRQPRIO_VSX:
309 vec = BOOK3S_INTERRUPT_VSX;
310 break;
311 case BOOK3S_IRQPRIO_ALTIVEC:
312 vec = BOOK3S_INTERRUPT_ALTIVEC;
313 break;
314 case BOOK3S_IRQPRIO_FP_UNAVAIL:
315 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
316 break;
317 case BOOK3S_IRQPRIO_SYSCALL:
318 vec = BOOK3S_INTERRUPT_SYSCALL;
319 break;
320 case BOOK3S_IRQPRIO_DEBUG:
321 vec = BOOK3S_INTERRUPT_TRACE;
322 break;
323 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
324 vec = BOOK3S_INTERRUPT_PERFMON;
325 break;
326 default:
327 deliver = 0;
328 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
329 break;
330 }
331
332#if 0
333 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
334#endif
335
336 if (deliver)
Alexander Graf25a8a022010-01-08 02:58:07 +0100337 kvmppc_inject_interrupt(vcpu, vec, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000338
339 return deliver;
340}
341
342void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
343{
344 unsigned long *pending = &vcpu->arch.pending_exceptions;
Alexander Graf90bba352010-07-29 14:47:51 +0200345 unsigned long old_pending = vcpu->arch.pending_exceptions;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000346 unsigned int priority;
347
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000348#ifdef EXIT_DEBUG
349 if (vcpu->arch.pending_exceptions)
350 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
351#endif
352 priority = __ffs(*pending);
Alexander Grafada7ba12010-04-16 00:11:56 +0200353 while (priority < BOOK3S_IRQPRIO_MAX) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100354 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
355 (priority != BOOK3S_IRQPRIO_DECREMENTER)) {
356 /* DEC interrupts get cleared by mtdec */
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000357 clear_bit(priority, &vcpu->arch.pending_exceptions);
358 break;
359 }
360
361 priority = find_next_bit(pending,
362 BITS_PER_BYTE * sizeof(*pending),
363 priority + 1);
364 }
Alexander Graf90bba352010-07-29 14:47:51 +0200365
366 /* Tell the guest about our interrupt status */
367 if (*pending)
368 vcpu->arch.shared->int_pending = 1;
369 else if (old_pending)
370 vcpu->arch.shared->int_pending = 0;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000371}
372
373void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
374{
Alexander Grafb83d4a92010-04-20 02:49:54 +0200375 u32 host_pvr;
376
Alexander Grafe15a1132009-11-30 03:02:02 +0000377 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000378 vcpu->arch.pvr = pvr;
Alexander Graf07b09072010-04-16 00:11:53 +0200379#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000380 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
381 kvmppc_mmu_book3s_64_init(vcpu);
382 to_book3s(vcpu)->hior = 0xfff00000;
383 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
Alexander Graf07b09072010-04-16 00:11:53 +0200384 } else
385#endif
386 {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000387 kvmppc_mmu_book3s_32_init(vcpu);
388 to_book3s(vcpu)->hior = 0;
389 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
390 }
391
392 /* If we are in hypervisor level on 970, we can tell the CPU to
393 * treat DCBZ as 32 bytes store */
394 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
395 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
396 !strcmp(cur_cpu_spec->platform, "ppc970"))
397 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
398
Alexander Graf05b0ab12010-03-24 21:48:37 +0100399 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
400 really needs them in a VM on Cell and force disable them. */
401 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
402 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
Alexander Graf07b09072010-04-16 00:11:53 +0200403
404#ifdef CONFIG_PPC_BOOK3S_32
405 /* 32 bit Book3S always has 32 byte dcbz */
406 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
407#endif
Alexander Grafb83d4a92010-04-20 02:49:54 +0200408
409 /* On some CPUs we can execute paired single operations natively */
410 asm ( "mfpvr %0" : "=r"(host_pvr));
411 switch (host_pvr) {
412 case 0x00080200: /* lonestar 2.0 */
413 case 0x00088202: /* lonestar 2.2 */
414 case 0x70000100: /* gekko 1.0 */
415 case 0x00080100: /* gekko 2.0 */
416 case 0x00083203: /* gekko 2.3a */
417 case 0x00083213: /* gekko 2.3b */
418 case 0x00083204: /* gekko 2.4 */
419 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
420 case 0x00087200: /* broadway */
421 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
422 /* Enable HID2.PSE - in case we need it later */
423 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
424 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000425}
426
Alexander Grafe8508942010-07-29 14:47:54 +0200427pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn)
428{
429 ulong mp_pa = vcpu->arch.magic_page_pa;
430
431 /* Magic page override */
432 if (unlikely(mp_pa) &&
433 unlikely(((gfn << PAGE_SHIFT) & KVM_PAM) ==
434 ((mp_pa & PAGE_MASK) & KVM_PAM))) {
435 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
436 pfn_t pfn;
437
438 pfn = (pfn_t)virt_to_phys((void*)shared_page) >> PAGE_SHIFT;
439 get_page(pfn_to_page(pfn));
440 return pfn;
441 }
442
443 return gfn_to_pfn(vcpu->kvm, gfn);
444}
445
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000446/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
447 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
448 * emulate 32 bytes dcbz length.
449 *
450 * The Book3s_64 inventors also realized this case and implemented a special bit
451 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
452 *
453 * My approach here is to patch the dcbz instruction on executing pages.
454 */
455static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
456{
Alexander Graf9fb244a2010-03-24 21:48:32 +0100457 struct page *hpage;
458 u64 hpage_offset;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000459 u32 *page;
460 int i;
461
Alexander Graf9fb244a2010-03-24 21:48:32 +0100462 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
Wei Yongjun646bab52010-08-17 10:08:52 +0800463 if (is_error_page(hpage)) {
464 kvm_release_page_clean(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000465 return;
Wei Yongjun646bab52010-08-17 10:08:52 +0800466 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000467
Alexander Graf9fb244a2010-03-24 21:48:32 +0100468 hpage_offset = pte->raddr & ~PAGE_MASK;
469 hpage_offset &= ~0xFFFULL;
470 hpage_offset /= 4;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000471
Alexander Graf9fb244a2010-03-24 21:48:32 +0100472 get_page(hpage);
473 page = kmap_atomic(hpage, KM_USER0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000474
Alexander Graf9fb244a2010-03-24 21:48:32 +0100475 /* patch dcbz into reserved instruction, so we trap */
476 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
477 if ((page[i] & 0xff0007ff) == INS_DCBZ)
478 page[i] &= 0xfffffff7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000479
Alexander Graf9fb244a2010-03-24 21:48:32 +0100480 kunmap_atomic(page, KM_USER0);
481 put_page(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000482}
483
484static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
485 struct kvmppc_pte *pte)
486{
Alexander Graf666e7252010-07-29 14:47:43 +0200487 int relocated = (vcpu->arch.shared->msr & (data ? MSR_DR : MSR_IR));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000488 int r;
489
490 if (relocated) {
491 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
492 } else {
493 pte->eaddr = eaddr;
Alexander Graf28e83b42010-07-29 14:47:52 +0200494 pte->raddr = eaddr & KVM_PAM;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100495 pte->vpage = VSID_REAL | eaddr >> 12;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000496 pte->may_read = true;
497 pte->may_write = true;
498 pte->may_execute = true;
499 r = 0;
500 }
501
502 return r;
503}
504
505static hva_t kvmppc_bad_hva(void)
506{
507 return PAGE_OFFSET;
508}
509
510static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
511 bool read)
512{
513 hva_t hpage;
514
515 if (read && !pte->may_read)
516 goto err;
517
518 if (!read && !pte->may_write)
519 goto err;
520
521 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
522 if (kvm_is_error_hva(hpage))
523 goto err;
524
525 return hpage | (pte->raddr & ~PAGE_MASK);
526err:
527 return kvmppc_bad_hva();
528}
529
Alexander Graf5467a972010-02-19 11:00:38 +0100530int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
531 bool data)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000532{
533 struct kvmppc_pte pte;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000534
535 vcpu->stat.st++;
536
Alexander Graf5467a972010-02-19 11:00:38 +0100537 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
Alexander Graf9fb244a2010-03-24 21:48:32 +0100538 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100539
540 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000541
Alexander Graf9fb244a2010-03-24 21:48:32 +0100542 if (!pte.may_write)
543 return -EPERM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000544
Alexander Graf9fb244a2010-03-24 21:48:32 +0100545 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
546 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000547
Alexander Graf5467a972010-02-19 11:00:38 +0100548 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000549}
550
Alexander Graf5467a972010-02-19 11:00:38 +0100551int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000552 bool data)
553{
554 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100555 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000556
557 vcpu->stat.ld++;
558
Alexander Graf5467a972010-02-19 11:00:38 +0100559 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
560 goto nopte;
561
562 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000563
564 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
565 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100566 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000567
568 if (copy_from_user(ptr, (void __user *)hva, size)) {
569 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100570 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000571 }
572
Alexander Graf5467a972010-02-19 11:00:38 +0100573 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000574
Alexander Graf5467a972010-02-19 11:00:38 +0100575nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000576 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100577mmio:
578 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000579}
580
581static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
582{
Alexander Grafe8508942010-07-29 14:47:54 +0200583 ulong mp_pa = vcpu->arch.magic_page_pa;
584
585 if (unlikely(mp_pa) &&
586 unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) {
587 return 1;
588 }
589
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000590 return kvm_is_visible_gfn(vcpu->kvm, gfn);
591}
592
593int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
594 ulong eaddr, int vec)
595{
596 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
597 int r = RESUME_GUEST;
598 int relocated;
599 int page_found = 0;
600 struct kvmppc_pte pte;
601 bool is_mmio = false;
Alexander Graf666e7252010-07-29 14:47:43 +0200602 bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
603 bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
Alexander Graff7bc74e2010-04-20 02:49:48 +0200604 u64 vsid;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000605
Alexander Graf3eeafd72010-03-24 21:48:17 +0100606 relocated = data ? dr : ir;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000607
608 /* Resolve real address if translation turned on */
609 if (relocated) {
610 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
611 } else {
612 pte.may_execute = true;
613 pte.may_read = true;
614 pte.may_write = true;
Alexander Graf28e83b42010-07-29 14:47:52 +0200615 pte.raddr = eaddr & KVM_PAM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000616 pte.eaddr = eaddr;
617 pte.vpage = eaddr >> 12;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100618 }
619
Alexander Graf666e7252010-07-29 14:47:43 +0200620 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
Alexander Graf3eeafd72010-03-24 21:48:17 +0100621 case 0:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200622 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
Alexander Graf3eeafd72010-03-24 21:48:17 +0100623 break;
624 case MSR_DR:
Alexander Graf3eeafd72010-03-24 21:48:17 +0100625 case MSR_IR:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200626 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
627
Alexander Graf666e7252010-07-29 14:47:43 +0200628 if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
Alexander Graff7bc74e2010-04-20 02:49:48 +0200629 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
630 else
631 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
632 pte.vpage |= vsid;
633
634 if (vsid == -1)
635 page_found = -EINVAL;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100636 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000637 }
638
639 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
640 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
641 /*
642 * If we do the dcbz hack, we have to NX on every execution,
643 * so we can patch the executing code. This renders our guest
644 * NX-less.
645 */
646 pte.may_execute = !data;
647 }
648
649 if (page_found == -ENOENT) {
650 /* Page not found in guest PTE entries */
Alexander Graf5e030182010-07-29 14:47:45 +0200651 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Grafd562de42010-07-29 14:47:44 +0200652 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf666e7252010-07-29 14:47:43 +0200653 vcpu->arch.shared->msr |=
654 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000655 kvmppc_book3s_queue_irqprio(vcpu, vec);
656 } else if (page_found == -EPERM) {
657 /* Storage protection */
Alexander Graf5e030182010-07-29 14:47:45 +0200658 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Grafd562de42010-07-29 14:47:44 +0200659 vcpu->arch.shared->dsisr =
660 to_svcpu(vcpu)->fault_dsisr & ~DSISR_NOHPTE;
661 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
Alexander Graf666e7252010-07-29 14:47:43 +0200662 vcpu->arch.shared->msr |=
663 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000664 kvmppc_book3s_queue_irqprio(vcpu, vec);
665 } else if (page_found == -EINVAL) {
666 /* Page not found in guest SLB */
Alexander Graf5e030182010-07-29 14:47:45 +0200667 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000668 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
669 } else if (!is_mmio &&
670 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
671 /* The guest's PTE is not mapped yet. Map on the host */
672 kvmppc_mmu_map_page(vcpu, &pte);
673 if (data)
674 vcpu->stat.sp_storage++;
675 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
676 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
677 kvmppc_patch_dcbz(vcpu, &pte);
678 } else {
679 /* MMIO */
680 vcpu->stat.mmio_exits++;
681 vcpu->arch.paddr_accessed = pte.raddr;
682 r = kvmppc_emulate_mmio(run, vcpu);
683 if ( r == RESUME_HOST_NV )
684 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000685 }
686
687 return r;
688}
689
Alexander Graf180a34d2010-01-15 14:49:11 +0100690static inline int get_fpr_index(int i)
691{
692#ifdef CONFIG_VSX
693 i *= 2;
694#endif
695 return i;
696}
697
698/* Give up external provider (FPU, Altivec, VSX) */
Alexander Grafaba3bd72010-02-19 11:00:39 +0100699void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
Alexander Graf180a34d2010-01-15 14:49:11 +0100700{
701 struct thread_struct *t = &current->thread;
702 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100703#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100704 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100705#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100706 u64 *thread_fpr = (u64*)t->fpr;
707 int i;
708
709 if (!(vcpu->arch.guest_owned_ext & msr))
710 return;
711
712#ifdef DEBUG_EXT
713 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
714#endif
715
716 switch (msr) {
717 case MSR_FP:
718 giveup_fpu(current);
719 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
720 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
721
722 vcpu->arch.fpscr = t->fpscr.val;
723 break;
724 case MSR_VEC:
725#ifdef CONFIG_ALTIVEC
726 giveup_altivec(current);
727 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
728 vcpu->arch.vscr = t->vscr;
729#endif
730 break;
731 case MSR_VSX:
732#ifdef CONFIG_VSX
733 __giveup_vsx(current);
734 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
735 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
736#endif
737 break;
738 default:
739 BUG();
740 }
741
742 vcpu->arch.guest_owned_ext &= ~msr;
743 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100744 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100745}
746
Alexander Graf89632212010-03-24 21:48:21 +0100747static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100748{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200749 ulong srr0 = kvmppc_get_pc(vcpu);
750 u32 last_inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100751 int ret;
752
Alexander Grafc7f38f42010-04-16 00:11:40 +0200753 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100754 if (ret == -ENOENT) {
Alexander Graf666e7252010-07-29 14:47:43 +0200755 ulong msr = vcpu->arch.shared->msr;
756
757 msr = kvmppc_set_field(msr, 33, 33, 1);
758 msr = kvmppc_set_field(msr, 34, 36, 0);
759 vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100760 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
Alexander Graf89632212010-03-24 21:48:21 +0100761 return EMULATE_AGAIN;
762 }
763
764 return EMULATE_DONE;
765}
766
767static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
768{
769
770 /* Need to do paired single emulation? */
771 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
772 return EMULATE_DONE;
773
774 /* Read out the instruction */
775 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100776 /* Need to emulate */
777 return EMULATE_FAIL;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100778
779 return EMULATE_AGAIN;
780}
781
Alexander Graf180a34d2010-01-15 14:49:11 +0100782/* Handle external providers (FPU, Altivec, VSX) */
783static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
784 ulong msr)
785{
786 struct thread_struct *t = &current->thread;
787 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100788#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100789 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100790#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100791 u64 *thread_fpr = (u64*)t->fpr;
792 int i;
793
Alexander Graf3c402a72010-02-19 11:00:32 +0100794 /* When we have paired singles, we emulate in software */
795 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
796 return RESUME_GUEST;
797
Alexander Graf666e7252010-07-29 14:47:43 +0200798 if (!(vcpu->arch.shared->msr & msr)) {
Alexander Graf180a34d2010-01-15 14:49:11 +0100799 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
800 return RESUME_GUEST;
801 }
802
Alexander Grafc2453692010-03-24 21:48:22 +0100803 /* We already own the ext */
804 if (vcpu->arch.guest_owned_ext & msr) {
805 return RESUME_GUEST;
806 }
807
Alexander Graf180a34d2010-01-15 14:49:11 +0100808#ifdef DEBUG_EXT
809 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
810#endif
811
812 current->thread.regs->msr |= msr;
813
814 switch (msr) {
815 case MSR_FP:
816 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
817 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
818
819 t->fpscr.val = vcpu->arch.fpscr;
820 t->fpexc_mode = 0;
821 kvmppc_load_up_fpu();
822 break;
823 case MSR_VEC:
824#ifdef CONFIG_ALTIVEC
825 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
826 t->vscr = vcpu->arch.vscr;
827 t->vrsave = -1;
828 kvmppc_load_up_altivec();
829#endif
830 break;
831 case MSR_VSX:
832#ifdef CONFIG_VSX
833 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
834 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
835 kvmppc_load_up_vsx();
836#endif
837 break;
838 default:
839 BUG();
840 }
841
842 vcpu->arch.guest_owned_ext |= msr;
843
Alexander Grafa76f8492010-01-15 14:49:14 +0100844 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100845
846 return RESUME_GUEST;
847}
848
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000849int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
850 unsigned int exit_nr)
851{
852 int r = RESUME_HOST;
853
854 vcpu->stat.sum_exits++;
855
856 run->exit_reason = KVM_EXIT_UNKNOWN;
857 run->ready_for_interrupt_injection = 1;
Alexander Grafbed1ed92010-08-02 11:06:26 +0200858
859 trace_kvm_book3s_exit(exit_nr, vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000860 kvm_resched(vcpu);
861 switch (exit_nr) {
862 case BOOK3S_INTERRUPT_INST_STORAGE:
863 vcpu->stat.pf_instruc++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200864
865#ifdef CONFIG_PPC_BOOK3S_32
866 /* We set segments as unused segments when invalidating them. So
867 * treat the respective fault as segment fault. */
868 if (to_svcpu(vcpu)->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT]
869 == SR_INVALID) {
870 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
871 r = RESUME_GUEST;
872 break;
873 }
874#endif
875
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000876 /* only care about PTEG not found errors, but leave NX alone */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200877 if (to_svcpu(vcpu)->shadow_srr1 & 0x40000000) {
878 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000879 vcpu->stat.sp_instruc++;
880 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
881 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
882 /*
883 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
884 * so we can't use the NX bit inside the guest. Let's cross our fingers,
885 * that no guest that needs the dcbz hack does NX.
886 */
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200887 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100888 r = RESUME_GUEST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000889 } else {
Alexander Graf666e7252010-07-29 14:47:43 +0200890 vcpu->arch.shared->msr |=
891 to_svcpu(vcpu)->shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000892 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000893 r = RESUME_GUEST;
894 }
895 break;
896 case BOOK3S_INTERRUPT_DATA_STORAGE:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200897 {
898 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000899 vcpu->stat.pf_storage++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200900
901#ifdef CONFIG_PPC_BOOK3S_32
902 /* We set segments as unused segments when invalidating them. So
903 * treat the respective fault as segment fault. */
904 if ((to_svcpu(vcpu)->sr[dar >> SID_SHIFT]) == SR_INVALID) {
905 kvmppc_mmu_map_segment(vcpu, dar);
906 r = RESUME_GUEST;
907 break;
908 }
909#endif
910
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000911 /* The only case we need to handle is missing shadow PTEs */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200912 if (to_svcpu(vcpu)->fault_dsisr & DSISR_NOHPTE) {
913 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000914 } else {
Alexander Graf5e030182010-07-29 14:47:45 +0200915 vcpu->arch.shared->dar = dar;
Alexander Grafd562de42010-07-29 14:47:44 +0200916 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000917 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000918 r = RESUME_GUEST;
919 }
920 break;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200921 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000922 case BOOK3S_INTERRUPT_DATA_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200923 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
Alexander Graf5e030182010-07-29 14:47:45 +0200924 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000925 kvmppc_book3s_queue_irqprio(vcpu,
926 BOOK3S_INTERRUPT_DATA_SEGMENT);
927 }
928 r = RESUME_GUEST;
929 break;
930 case BOOK3S_INTERRUPT_INST_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200931 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000932 kvmppc_book3s_queue_irqprio(vcpu,
933 BOOK3S_INTERRUPT_INST_SEGMENT);
934 }
935 r = RESUME_GUEST;
936 break;
937 /* We're good on these - the host merely wanted to get our attention */
938 case BOOK3S_INTERRUPT_DECREMENTER:
939 vcpu->stat.dec_exits++;
940 r = RESUME_GUEST;
941 break;
942 case BOOK3S_INTERRUPT_EXTERNAL:
943 vcpu->stat.ext_intr_exits++;
944 r = RESUME_GUEST;
945 break;
Alexander Graf7fdaec92010-04-20 02:49:47 +0200946 case BOOK3S_INTERRUPT_PERFMON:
947 r = RESUME_GUEST;
948 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000949 case BOOK3S_INTERRUPT_PROGRAM:
950 {
951 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100952 ulong flags;
953
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100954program_interrupt:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200955 flags = to_svcpu(vcpu)->shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000956
Alexander Graf666e7252010-07-29 14:47:43 +0200957 if (vcpu->arch.shared->msr & MSR_PR) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000958#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +0200959 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 +0000960#endif
Alexander Grafc7f38f42010-04-16 00:11:40 +0200961 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000962 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100963 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000964 r = RESUME_GUEST;
965 break;
966 }
967 }
968
969 vcpu->stat.emulated_inst_exits++;
970 er = kvmppc_emulate_instruction(run, vcpu);
971 switch (er) {
972 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100973 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000974 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100975 case EMULATE_AGAIN:
976 r = RESUME_GUEST;
977 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000978 case EMULATE_FAIL:
979 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200980 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100981 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000982 r = RESUME_GUEST;
983 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +0100984 case EMULATE_DO_MMIO:
985 run->exit_reason = KVM_EXIT_MMIO;
986 r = RESUME_HOST_NV;
987 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000988 default:
989 BUG();
990 }
991 break;
992 }
993 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafad0a0482010-03-24 21:48:30 +0100994 if (vcpu->arch.osi_enabled &&
995 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
996 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
Alexander Graf2a342ed2010-07-29 14:47:48 +0200997 /* MOL hypercalls */
Alexander Grafad0a0482010-03-24 21:48:30 +0100998 u64 *gprs = run->osi.gprs;
999 int i;
1000
1001 run->exit_reason = KVM_EXIT_OSI;
1002 for (i = 0; i < 32; i++)
1003 gprs[i] = kvmppc_get_gpr(vcpu, i);
1004 vcpu->arch.osi_needed = 1;
1005 r = RESUME_HOST_NV;
Alexander Graf2a342ed2010-07-29 14:47:48 +02001006 } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
1007 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
1008 /* KVM PV hypercalls */
1009 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1010 r = RESUME_GUEST;
Alexander Grafad0a0482010-03-24 21:48:30 +01001011 } else {
Alexander Graf2a342ed2010-07-29 14:47:48 +02001012 /* Guest syscalls */
Alexander Grafad0a0482010-03-24 21:48:30 +01001013 vcpu->stat.syscall_exits++;
1014 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1015 r = RESUME_GUEST;
1016 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001017 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001018 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001019 case BOOK3S_INTERRUPT_ALTIVEC:
1020 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +01001021 {
1022 int ext_msr = 0;
1023
1024 switch (exit_nr) {
1025 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
1026 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
1027 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
1028 }
1029
1030 switch (kvmppc_check_ext(vcpu, exit_nr)) {
1031 case EMULATE_DONE:
1032 /* everything ok - let's enable the ext */
1033 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
1034 break;
1035 case EMULATE_FAIL:
1036 /* we need to emulate this instruction */
1037 goto program_interrupt;
1038 break;
1039 default:
1040 /* nothing to worry about - go again */
1041 break;
1042 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001043 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +01001044 }
Alexander Grafca7f4202010-03-24 21:48:28 +01001045 case BOOK3S_INTERRUPT_ALIGNMENT:
1046 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
Alexander Grafd562de42010-07-29 14:47:44 +02001047 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001048 kvmppc_get_last_inst(vcpu));
Alexander Graf5e030182010-07-29 14:47:45 +02001049 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001050 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +01001051 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1052 }
1053 r = RESUME_GUEST;
1054 break;
Alexander Graf180a34d2010-01-15 14:49:11 +01001055 case BOOK3S_INTERRUPT_MACHINE_CHECK:
1056 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001057 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1058 r = RESUME_GUEST;
1059 break;
1060 default:
1061 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +01001062 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +02001063 exit_nr, kvmppc_get_pc(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001064 r = RESUME_HOST;
1065 BUG();
1066 break;
1067 }
1068
1069
1070 if (!(r & RESUME_HOST)) {
1071 /* To avoid clobbering exit_reason, only check for signals if
1072 * we aren't already exiting to userspace for some other
1073 * reason. */
1074 if (signal_pending(current)) {
1075#ifdef EXIT_DEBUG
1076 printk(KERN_EMERG "KVM: Going back to host\n");
1077#endif
1078 vcpu->stat.signal_exits++;
1079 run->exit_reason = KVM_EXIT_INTR;
1080 r = -EINTR;
1081 } else {
1082 /* In case an interrupt came in that was triggered
1083 * from userspace (like DEC), we need to check what
1084 * to inject now! */
1085 kvmppc_core_deliver_interrupts(vcpu);
1086 }
1087 }
1088
Alexander Grafbed1ed92010-08-02 11:06:26 +02001089 trace_kvm_book3s_reenter(r, vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001090
1091 return r;
1092}
1093
1094int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1095{
1096 return 0;
1097}
1098
1099int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1100{
1101 int i;
1102
Alexander Grafc7f38f42010-04-16 00:11:40 +02001103 regs->pc = kvmppc_get_pc(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001104 regs->cr = kvmppc_get_cr(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001105 regs->ctr = kvmppc_get_ctr(vcpu);
1106 regs->lr = kvmppc_get_lr(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001107 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001108 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +02001109 regs->srr0 = vcpu->arch.shared->srr0;
1110 regs->srr1 = vcpu->arch.shared->srr1;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001111 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001112 regs->sprg0 = vcpu->arch.shared->sprg0;
1113 regs->sprg1 = vcpu->arch.shared->sprg1;
1114 regs->sprg2 = vcpu->arch.shared->sprg2;
1115 regs->sprg3 = vcpu->arch.shared->sprg3;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001116 regs->sprg5 = vcpu->arch.sprg4;
1117 regs->sprg6 = vcpu->arch.sprg5;
1118 regs->sprg7 = vcpu->arch.sprg6;
1119
1120 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001121 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001122
1123 return 0;
1124}
1125
1126int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1127{
1128 int i;
1129
Alexander Grafc7f38f42010-04-16 00:11:40 +02001130 kvmppc_set_pc(vcpu, regs->pc);
Alexander Graf992b5b22010-01-08 02:58:02 +01001131 kvmppc_set_cr(vcpu, regs->cr);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001132 kvmppc_set_ctr(vcpu, regs->ctr);
1133 kvmppc_set_lr(vcpu, regs->lr);
Alexander Graf992b5b22010-01-08 02:58:02 +01001134 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001135 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +02001136 vcpu->arch.shared->srr0 = regs->srr0;
1137 vcpu->arch.shared->srr1 = regs->srr1;
Alexander Grafa73a9592010-07-29 14:47:47 +02001138 vcpu->arch.shared->sprg0 = regs->sprg0;
1139 vcpu->arch.shared->sprg1 = regs->sprg1;
1140 vcpu->arch.shared->sprg2 = regs->sprg2;
1141 vcpu->arch.shared->sprg3 = regs->sprg3;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001142 vcpu->arch.sprg5 = regs->sprg4;
1143 vcpu->arch.sprg6 = regs->sprg5;
1144 vcpu->arch.sprg7 = regs->sprg6;
1145
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001146 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1147 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001148
1149 return 0;
1150}
1151
1152int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1153 struct kvm_sregs *sregs)
1154{
Alexander Grafe15a1132009-11-30 03:02:02 +00001155 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1156 int i;
1157
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001158 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +00001159
1160 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1161 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1162 for (i = 0; i < 64; i++) {
1163 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
1164 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1165 }
1166 } else {
Alexander Grafdf1bfa22010-08-03 02:29:27 +02001167 for (i = 0; i < 16; i++)
1168 sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i];
1169
Alexander Grafe15a1132009-11-30 03:02:02 +00001170 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 Graffef093b2010-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 Graffef093b2010-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);