blob: c961de40c676b0d59c230fba9bc99b6d33389cca [file] [log] [blame]
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001/*
2 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
3 *
4 * Authors:
5 * Alexander Graf <agraf@suse.de>
6 * Kevin Wolf <mail@kevin-wolf.de>
7 *
8 * Description:
9 * This file is derived from arch/powerpc/kvm/44x.c,
10 * by Hollis Blanchard <hollisb@us.ibm.com>.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License, version 2, as
14 * published by the Free Software Foundation.
15 */
16
17#include <linux/kvm_host.h>
18#include <linux/err.h>
Stephen Rothwell329d20b2010-04-27 15:49:17 +100019#include <linux/slab.h>
Alexander Grafbed1ed92010-08-02 11:06:26 +020020#include "trace.h"
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000021
22#include <asm/reg.h>
23#include <asm/cputable.h>
24#include <asm/cacheflush.h>
25#include <asm/tlbflush.h>
26#include <asm/uaccess.h>
27#include <asm/io.h>
28#include <asm/kvm_ppc.h>
29#include <asm/kvm_book3s.h>
30#include <asm/mmu_context.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/gfp.h>
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000032#include <linux/sched.h>
33#include <linux/vmalloc.h>
Alexander Graf9fb244a2010-03-24 21:48:32 +010034#include <linux/highmem.h>
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000035
36#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
37
38/* #define EXIT_DEBUG */
Alexander Graf180a34d2010-01-15 14:49:11 +010039/* #define DEBUG_EXT */
40
Alexander Grafc8c0b6f2010-02-19 11:00:34 +010041static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
42 ulong msr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000043
Alexander Graf07b09072010-04-16 00:11:53 +020044/* Some compatibility defines */
45#ifdef CONFIG_PPC_BOOK3S_32
46#define MSR_USER32 MSR_USER
47#define MSR_USER64 MSR_USER
48#define HW_PAGE_SIZE PAGE_SIZE
49#endif
50
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000051struct kvm_stats_debugfs_item debugfs_entries[] = {
52 { "exits", VCPU_STAT(sum_exits) },
53 { "mmio", VCPU_STAT(mmio_exits) },
54 { "sig", VCPU_STAT(signal_exits) },
55 { "sysc", VCPU_STAT(syscall_exits) },
56 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
57 { "dec", VCPU_STAT(dec_exits) },
58 { "ext_intr", VCPU_STAT(ext_intr_exits) },
59 { "queue_intr", VCPU_STAT(queue_intr) },
60 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
61 { "pf_storage", VCPU_STAT(pf_storage) },
62 { "sp_storage", VCPU_STAT(sp_storage) },
63 { "pf_instruc", VCPU_STAT(pf_instruc) },
64 { "sp_instruc", VCPU_STAT(sp_instruc) },
65 { "ld", VCPU_STAT(ld) },
66 { "ld_slow", VCPU_STAT(ld_slow) },
67 { "st", VCPU_STAT(st) },
68 { "st_slow", VCPU_STAT(st_slow) },
69 { NULL }
70};
71
72void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
73{
74}
75
76void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
77{
78}
79
80void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
81{
Alexander Grafc7f38f42010-04-16 00:11:40 +020082#ifdef CONFIG_PPC_BOOK3S_64
83 memcpy(to_svcpu(vcpu)->slb, to_book3s(vcpu)->slb_shadow, sizeof(to_svcpu(vcpu)->slb));
84 memcpy(&get_paca()->shadow_vcpu, to_book3s(vcpu)->shadow_vcpu,
Alexander Graf7e57cba2010-01-08 02:58:03 +010085 sizeof(get_paca()->shadow_vcpu));
Alexander Grafc7f38f42010-04-16 00:11:40 +020086 to_svcpu(vcpu)->slb_max = to_book3s(vcpu)->slb_shadow_max;
87#endif
88
89#ifdef CONFIG_PPC_BOOK3S_32
90 current->thread.kvm_shadow_vcpu = to_book3s(vcpu)->shadow_vcpu;
91#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000092}
93
94void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
95{
Alexander Grafc7f38f42010-04-16 00:11:40 +020096#ifdef CONFIG_PPC_BOOK3S_64
97 memcpy(to_book3s(vcpu)->slb_shadow, to_svcpu(vcpu)->slb, sizeof(to_svcpu(vcpu)->slb));
98 memcpy(to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
Alexander Graf7e57cba2010-01-08 02:58:03 +010099 sizeof(get_paca()->shadow_vcpu));
Alexander Grafc7f38f42010-04-16 00:11:40 +0200100 to_book3s(vcpu)->slb_shadow_max = to_svcpu(vcpu)->slb_max;
101#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100102
103 kvmppc_giveup_ext(vcpu, MSR_FP);
104 kvmppc_giveup_ext(vcpu, MSR_VEC);
105 kvmppc_giveup_ext(vcpu, MSR_VSX);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000106}
107
Alexander Grafa76f8492010-01-15 14:49:14 +0100108static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
109{
Alexander Graf666e7252010-07-29 14:47:43 +0200110 ulong smsr = vcpu->arch.shared->msr;
111
Alexander Grafa76f8492010-01-15 14:49:14 +0100112 /* Guest MSR values */
Alexander Graf666e7252010-07-29 14:47:43 +0200113 smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE | MSR_DE;
Alexander Grafa76f8492010-01-15 14:49:14 +0100114 /* Process MSR values */
Alexander Graf666e7252010-07-29 14:47:43 +0200115 smsr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | MSR_EE;
Alexander Grafa76f8492010-01-15 14:49:14 +0100116 /* External providers the guest reserved */
Alexander Graf666e7252010-07-29 14:47:43 +0200117 smsr |= (vcpu->arch.shared->msr & vcpu->arch.guest_owned_ext);
Alexander Grafa76f8492010-01-15 14:49:14 +0100118 /* 64-bit Process MSR values */
119#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf666e7252010-07-29 14:47:43 +0200120 smsr |= MSR_ISF | MSR_HV;
Alexander Grafa76f8492010-01-15 14:49:14 +0100121#endif
Alexander Graf666e7252010-07-29 14:47:43 +0200122 vcpu->arch.shadow_msr = smsr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100123}
124
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000125void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
126{
Alexander Graf666e7252010-07-29 14:47:43 +0200127 ulong old_msr = vcpu->arch.shared->msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000128
129#ifdef EXIT_DEBUG
130 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
131#endif
Alexander Grafa76f8492010-01-15 14:49:14 +0100132
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000133 msr &= to_book3s(vcpu)->msr_mask;
Alexander Graf666e7252010-07-29 14:47:43 +0200134 vcpu->arch.shared->msr = msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100135 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000136
Alexander Graf296c19d2010-08-15 08:39:19 +0200137 if (msr & MSR_POW) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000138 if (!vcpu->arch.pending_exceptions) {
139 kvm_vcpu_block(vcpu);
140 vcpu->stat.halt_wakeup++;
Alexander Graf296c19d2010-08-15 08:39:19 +0200141
142 /* Unset POW bit after we woke up */
143 msr &= ~MSR_POW;
144 vcpu->arch.shared->msr = msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000145 }
146 }
147
Alexander Graf666e7252010-07-29 14:47:43 +0200148 if ((vcpu->arch.shared->msr & (MSR_PR|MSR_IR|MSR_DR)) !=
Alexander Graff7bc74e2010-04-20 02:49:48 +0200149 (old_msr & (MSR_PR|MSR_IR|MSR_DR))) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000150 kvmppc_mmu_flush_segments(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +0200151 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
Alexander Graf4cb6b7e2010-08-02 16:08:22 +0200152
153 /* Preload magic page segment when in kernel mode */
154 if (!(msr & MSR_PR) && vcpu->arch.magic_page_pa) {
155 struct kvm_vcpu_arch *a = &vcpu->arch;
156
157 if (msr & MSR_DR)
158 kvmppc_mmu_map_segment(vcpu, a->magic_page_ea);
159 else
160 kvmppc_mmu_map_segment(vcpu, a->magic_page_pa);
161 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000162 }
Alexander Grafd1bab742010-02-19 11:00:35 +0100163
164 /* Preload FPU if it's enabled */
Alexander Graf666e7252010-07-29 14:47:43 +0200165 if (vcpu->arch.shared->msr & MSR_FP)
Alexander Grafd1bab742010-02-19 11:00:35 +0100166 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000167}
168
169void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
170{
Alexander Grafde7906c2010-07-29 14:47:46 +0200171 vcpu->arch.shared->srr0 = kvmppc_get_pc(vcpu);
172 vcpu->arch.shared->srr1 = vcpu->arch.shared->msr | flags;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200173 kvmppc_set_pc(vcpu, to_book3s(vcpu)->hior + vec);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000174 vcpu->arch.mmu.reset_msr(vcpu);
175}
176
Alexander Graf583617b2009-12-21 20:21:23 +0100177static int kvmppc_book3s_vec2irqprio(unsigned int vec)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000178{
179 unsigned int prio;
180
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000181 switch (vec) {
182 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
183 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
184 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
185 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
186 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
187 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
188 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
Alexander Graf17bd1582010-08-30 10:44:15 +0200189 case 0x501: prio = BOOK3S_IRQPRIO_EXTERNAL_LEVEL; break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000190 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
191 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
192 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
193 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
194 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
195 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
196 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
197 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
198 default: prio = BOOK3S_IRQPRIO_MAX; break;
199 }
200
Alexander Graf583617b2009-12-21 20:21:23 +0100201 return prio;
202}
203
Alexander Graf7706664d2009-12-21 20:21:24 +0100204static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
205 unsigned int vec)
206{
207 clear_bit(kvmppc_book3s_vec2irqprio(vec),
208 &vcpu->arch.pending_exceptions);
Alexander Graf9ee18b12010-08-05 12:24:40 +0200209
210 if (!vcpu->arch.pending_exceptions)
211 vcpu->arch.shared->int_pending = 0;
Alexander Graf7706664d2009-12-21 20:21:24 +0100212}
213
Alexander Graf583617b2009-12-21 20:21:23 +0100214void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
215{
216 vcpu->stat.queue_intr++;
217
218 set_bit(kvmppc_book3s_vec2irqprio(vec),
219 &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000220#ifdef EXIT_DEBUG
221 printk(KERN_INFO "Queueing interrupt %x\n", vec);
222#endif
223}
224
225
Alexander Graf25a8a022010-01-08 02:58:07 +0100226void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000227{
Alexander Graf25a8a022010-01-08 02:58:07 +0100228 to_book3s(vcpu)->prog_flags = flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000229 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM);
230}
231
232void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
233{
234 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
235}
236
237int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
238{
239 return test_bit(BOOK3S_INTERRUPT_DECREMENTER >> 7, &vcpu->arch.pending_exceptions);
240}
241
Alexander Graf7706664d2009-12-21 20:21:24 +0100242void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
243{
244 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
245}
246
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000247void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
248 struct kvm_interrupt *irq)
249{
Alexander Graf17bd1582010-08-30 10:44:15 +0200250 unsigned int vec = BOOK3S_INTERRUPT_EXTERNAL;
251
252 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
253 vec = BOOK3S_INTERRUPT_EXTERNAL_LEVEL;
254
255 kvmppc_book3s_queue_irqprio(vcpu, vec);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000256}
257
Alexander Graf18978762010-03-24 21:48:18 +0100258void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
259 struct kvm_interrupt *irq)
260{
261 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
Alexander Graf17bd1582010-08-30 10:44:15 +0200262 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL_LEVEL);
Alexander Graf18978762010-03-24 21:48:18 +0100263}
264
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000265int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
266{
267 int deliver = 1;
268 int vec = 0;
Alexander Graf25a8a022010-01-08 02:58:07 +0100269 ulong flags = 0ULL;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200270 ulong crit_raw = vcpu->arch.shared->critical;
271 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
272 bool crit;
273
274 /* Truncate crit indicators in 32 bit mode */
275 if (!(vcpu->arch.shared->msr & MSR_SF)) {
276 crit_raw &= 0xffffffff;
277 crit_r1 &= 0xffffffff;
278 }
279
280 /* Critical section when crit == r1 */
281 crit = (crit_raw == crit_r1);
282 /* ... and we're in supervisor mode */
283 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000284
285 switch (priority) {
286 case BOOK3S_IRQPRIO_DECREMENTER:
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200287 deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000288 vec = BOOK3S_INTERRUPT_DECREMENTER;
289 break;
290 case BOOK3S_IRQPRIO_EXTERNAL:
Alexander Graf17bd1582010-08-30 10:44:15 +0200291 case BOOK3S_IRQPRIO_EXTERNAL_LEVEL:
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200292 deliver = (vcpu->arch.shared->msr & MSR_EE) && !crit;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000293 vec = BOOK3S_INTERRUPT_EXTERNAL;
294 break;
295 case BOOK3S_IRQPRIO_SYSTEM_RESET:
296 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
297 break;
298 case BOOK3S_IRQPRIO_MACHINE_CHECK:
299 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
300 break;
301 case BOOK3S_IRQPRIO_DATA_STORAGE:
302 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
303 break;
304 case BOOK3S_IRQPRIO_INST_STORAGE:
305 vec = BOOK3S_INTERRUPT_INST_STORAGE;
306 break;
307 case BOOK3S_IRQPRIO_DATA_SEGMENT:
308 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
309 break;
310 case BOOK3S_IRQPRIO_INST_SEGMENT:
311 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
312 break;
313 case BOOK3S_IRQPRIO_ALIGNMENT:
314 vec = BOOK3S_INTERRUPT_ALIGNMENT;
315 break;
316 case BOOK3S_IRQPRIO_PROGRAM:
317 vec = BOOK3S_INTERRUPT_PROGRAM;
Alexander Graf25a8a022010-01-08 02:58:07 +0100318 flags = to_book3s(vcpu)->prog_flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000319 break;
320 case BOOK3S_IRQPRIO_VSX:
321 vec = BOOK3S_INTERRUPT_VSX;
322 break;
323 case BOOK3S_IRQPRIO_ALTIVEC:
324 vec = BOOK3S_INTERRUPT_ALTIVEC;
325 break;
326 case BOOK3S_IRQPRIO_FP_UNAVAIL:
327 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
328 break;
329 case BOOK3S_IRQPRIO_SYSCALL:
330 vec = BOOK3S_INTERRUPT_SYSCALL;
331 break;
332 case BOOK3S_IRQPRIO_DEBUG:
333 vec = BOOK3S_INTERRUPT_TRACE;
334 break;
335 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
336 vec = BOOK3S_INTERRUPT_PERFMON;
337 break;
338 default:
339 deliver = 0;
340 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
341 break;
342 }
343
344#if 0
345 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
346#endif
347
348 if (deliver)
Alexander Graf25a8a022010-01-08 02:58:07 +0100349 kvmppc_inject_interrupt(vcpu, vec, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000350
351 return deliver;
352}
353
Alexander Graf17bd1582010-08-30 10:44:15 +0200354/*
355 * This function determines if an irqprio should be cleared once issued.
356 */
357static bool clear_irqprio(struct kvm_vcpu *vcpu, unsigned int priority)
358{
359 switch (priority) {
360 case BOOK3S_IRQPRIO_DECREMENTER:
361 /* DEC interrupts get cleared by mtdec */
362 return false;
363 case BOOK3S_IRQPRIO_EXTERNAL_LEVEL:
364 /* External interrupts get cleared by userspace */
365 return false;
366 }
367
368 return true;
369}
370
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000371void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
372{
373 unsigned long *pending = &vcpu->arch.pending_exceptions;
Alexander Graf90bba352010-07-29 14:47:51 +0200374 unsigned long old_pending = vcpu->arch.pending_exceptions;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000375 unsigned int priority;
376
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000377#ifdef EXIT_DEBUG
378 if (vcpu->arch.pending_exceptions)
379 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
380#endif
381 priority = __ffs(*pending);
Alexander Grafada7ba12010-04-16 00:11:56 +0200382 while (priority < BOOK3S_IRQPRIO_MAX) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100383 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
Alexander Graf17bd1582010-08-30 10:44:15 +0200384 clear_irqprio(vcpu, priority)) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000385 clear_bit(priority, &vcpu->arch.pending_exceptions);
386 break;
387 }
388
389 priority = find_next_bit(pending,
390 BITS_PER_BYTE * sizeof(*pending),
391 priority + 1);
392 }
Alexander Graf90bba352010-07-29 14:47:51 +0200393
394 /* Tell the guest about our interrupt status */
395 if (*pending)
396 vcpu->arch.shared->int_pending = 1;
397 else if (old_pending)
398 vcpu->arch.shared->int_pending = 0;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000399}
400
401void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
402{
Alexander Grafb83d4a92010-04-20 02:49:54 +0200403 u32 host_pvr;
404
Alexander Grafe15a1132009-11-30 03:02:02 +0000405 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000406 vcpu->arch.pvr = pvr;
Alexander Graf07b09072010-04-16 00:11:53 +0200407#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000408 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
409 kvmppc_mmu_book3s_64_init(vcpu);
410 to_book3s(vcpu)->hior = 0xfff00000;
411 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
Alexander Graf07b09072010-04-16 00:11:53 +0200412 } else
413#endif
414 {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000415 kvmppc_mmu_book3s_32_init(vcpu);
416 to_book3s(vcpu)->hior = 0;
417 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
418 }
419
420 /* If we are in hypervisor level on 970, we can tell the CPU to
421 * treat DCBZ as 32 bytes store */
422 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
423 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
424 !strcmp(cur_cpu_spec->platform, "ppc970"))
425 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
426
Alexander Graf05b0ab12010-03-24 21:48:37 +0100427 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
428 really needs them in a VM on Cell and force disable them. */
429 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
430 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
Alexander Graf07b09072010-04-16 00:11:53 +0200431
432#ifdef CONFIG_PPC_BOOK3S_32
433 /* 32 bit Book3S always has 32 byte dcbz */
434 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
435#endif
Alexander Grafb83d4a92010-04-20 02:49:54 +0200436
437 /* On some CPUs we can execute paired single operations natively */
438 asm ( "mfpvr %0" : "=r"(host_pvr));
439 switch (host_pvr) {
440 case 0x00080200: /* lonestar 2.0 */
441 case 0x00088202: /* lonestar 2.2 */
442 case 0x70000100: /* gekko 1.0 */
443 case 0x00080100: /* gekko 2.0 */
444 case 0x00083203: /* gekko 2.3a */
445 case 0x00083213: /* gekko 2.3b */
446 case 0x00083204: /* gekko 2.4 */
447 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
448 case 0x00087200: /* broadway */
449 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
450 /* Enable HID2.PSE - in case we need it later */
451 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
452 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000453}
454
Alexander Grafe8508942010-07-29 14:47:54 +0200455pfn_t kvmppc_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn)
456{
457 ulong mp_pa = vcpu->arch.magic_page_pa;
458
459 /* Magic page override */
460 if (unlikely(mp_pa) &&
461 unlikely(((gfn << PAGE_SHIFT) & KVM_PAM) ==
462 ((mp_pa & PAGE_MASK) & KVM_PAM))) {
463 ulong shared_page = ((ulong)vcpu->arch.shared) & PAGE_MASK;
464 pfn_t pfn;
465
466 pfn = (pfn_t)virt_to_phys((void*)shared_page) >> PAGE_SHIFT;
467 get_page(pfn_to_page(pfn));
468 return pfn;
469 }
470
471 return gfn_to_pfn(vcpu->kvm, gfn);
472}
473
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000474/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
475 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
476 * emulate 32 bytes dcbz length.
477 *
478 * The Book3s_64 inventors also realized this case and implemented a special bit
479 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
480 *
481 * My approach here is to patch the dcbz instruction on executing pages.
482 */
483static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
484{
Alexander Graf9fb244a2010-03-24 21:48:32 +0100485 struct page *hpage;
486 u64 hpage_offset;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000487 u32 *page;
488 int i;
489
Alexander Graf9fb244a2010-03-24 21:48:32 +0100490 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
Wei Yongjun646bab52010-08-17 10:08:52 +0800491 if (is_error_page(hpage)) {
492 kvm_release_page_clean(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000493 return;
Wei Yongjun646bab52010-08-17 10:08:52 +0800494 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000495
Alexander Graf9fb244a2010-03-24 21:48:32 +0100496 hpage_offset = pte->raddr & ~PAGE_MASK;
497 hpage_offset &= ~0xFFFULL;
498 hpage_offset /= 4;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000499
Alexander Graf9fb244a2010-03-24 21:48:32 +0100500 get_page(hpage);
501 page = kmap_atomic(hpage, KM_USER0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000502
Alexander Graf9fb244a2010-03-24 21:48:32 +0100503 /* patch dcbz into reserved instruction, so we trap */
504 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
505 if ((page[i] & 0xff0007ff) == INS_DCBZ)
506 page[i] &= 0xfffffff7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000507
Alexander Graf9fb244a2010-03-24 21:48:32 +0100508 kunmap_atomic(page, KM_USER0);
509 put_page(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000510}
511
512static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
513 struct kvmppc_pte *pte)
514{
Alexander Graf666e7252010-07-29 14:47:43 +0200515 int relocated = (vcpu->arch.shared->msr & (data ? MSR_DR : MSR_IR));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000516 int r;
517
518 if (relocated) {
519 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
520 } else {
521 pte->eaddr = eaddr;
Alexander Graf28e83b42010-07-29 14:47:52 +0200522 pte->raddr = eaddr & KVM_PAM;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100523 pte->vpage = VSID_REAL | eaddr >> 12;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000524 pte->may_read = true;
525 pte->may_write = true;
526 pte->may_execute = true;
527 r = 0;
528 }
529
530 return r;
531}
532
533static hva_t kvmppc_bad_hva(void)
534{
535 return PAGE_OFFSET;
536}
537
538static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
539 bool read)
540{
541 hva_t hpage;
542
543 if (read && !pte->may_read)
544 goto err;
545
546 if (!read && !pte->may_write)
547 goto err;
548
549 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
550 if (kvm_is_error_hva(hpage))
551 goto err;
552
553 return hpage | (pte->raddr & ~PAGE_MASK);
554err:
555 return kvmppc_bad_hva();
556}
557
Alexander Graf5467a972010-02-19 11:00:38 +0100558int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
559 bool data)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000560{
561 struct kvmppc_pte pte;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000562
563 vcpu->stat.st++;
564
Alexander Graf5467a972010-02-19 11:00:38 +0100565 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
Alexander Graf9fb244a2010-03-24 21:48:32 +0100566 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100567
568 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000569
Alexander Graf9fb244a2010-03-24 21:48:32 +0100570 if (!pte.may_write)
571 return -EPERM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000572
Alexander Graf9fb244a2010-03-24 21:48:32 +0100573 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
574 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000575
Alexander Graf5467a972010-02-19 11:00:38 +0100576 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000577}
578
Alexander Graf5467a972010-02-19 11:00:38 +0100579int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000580 bool data)
581{
582 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100583 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000584
585 vcpu->stat.ld++;
586
Alexander Graf5467a972010-02-19 11:00:38 +0100587 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
588 goto nopte;
589
590 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000591
592 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
593 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100594 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000595
596 if (copy_from_user(ptr, (void __user *)hva, size)) {
597 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100598 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000599 }
600
Alexander Graf5467a972010-02-19 11:00:38 +0100601 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000602
Alexander Graf5467a972010-02-19 11:00:38 +0100603nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000604 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100605mmio:
606 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000607}
608
609static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
610{
Alexander Grafe8508942010-07-29 14:47:54 +0200611 ulong mp_pa = vcpu->arch.magic_page_pa;
612
613 if (unlikely(mp_pa) &&
614 unlikely((mp_pa & KVM_PAM) >> PAGE_SHIFT == gfn)) {
615 return 1;
616 }
617
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000618 return kvm_is_visible_gfn(vcpu->kvm, gfn);
619}
620
621int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
622 ulong eaddr, int vec)
623{
624 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
625 int r = RESUME_GUEST;
626 int relocated;
627 int page_found = 0;
628 struct kvmppc_pte pte;
629 bool is_mmio = false;
Alexander Graf666e7252010-07-29 14:47:43 +0200630 bool dr = (vcpu->arch.shared->msr & MSR_DR) ? true : false;
631 bool ir = (vcpu->arch.shared->msr & MSR_IR) ? true : false;
Alexander Graff7bc74e2010-04-20 02:49:48 +0200632 u64 vsid;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000633
Alexander Graf3eeafd72010-03-24 21:48:17 +0100634 relocated = data ? dr : ir;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000635
636 /* Resolve real address if translation turned on */
637 if (relocated) {
638 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
639 } else {
640 pte.may_execute = true;
641 pte.may_read = true;
642 pte.may_write = true;
Alexander Graf28e83b42010-07-29 14:47:52 +0200643 pte.raddr = eaddr & KVM_PAM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000644 pte.eaddr = eaddr;
645 pte.vpage = eaddr >> 12;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100646 }
647
Alexander Graf666e7252010-07-29 14:47:43 +0200648 switch (vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) {
Alexander Graf3eeafd72010-03-24 21:48:17 +0100649 case 0:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200650 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
Alexander Graf3eeafd72010-03-24 21:48:17 +0100651 break;
652 case MSR_DR:
Alexander Graf3eeafd72010-03-24 21:48:17 +0100653 case MSR_IR:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200654 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
655
Alexander Graf666e7252010-07-29 14:47:43 +0200656 if ((vcpu->arch.shared->msr & (MSR_DR|MSR_IR)) == MSR_DR)
Alexander Graff7bc74e2010-04-20 02:49:48 +0200657 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
658 else
659 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
660 pte.vpage |= vsid;
661
662 if (vsid == -1)
663 page_found = -EINVAL;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100664 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000665 }
666
667 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
668 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
669 /*
670 * If we do the dcbz hack, we have to NX on every execution,
671 * so we can patch the executing code. This renders our guest
672 * NX-less.
673 */
674 pte.may_execute = !data;
675 }
676
677 if (page_found == -ENOENT) {
678 /* Page not found in guest PTE entries */
Alexander Graf5e030182010-07-29 14:47:45 +0200679 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Grafd562de42010-07-29 14:47:44 +0200680 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf666e7252010-07-29 14:47:43 +0200681 vcpu->arch.shared->msr |=
682 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000683 kvmppc_book3s_queue_irqprio(vcpu, vec);
684 } else if (page_found == -EPERM) {
685 /* Storage protection */
Alexander Graf5e030182010-07-29 14:47:45 +0200686 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Grafd562de42010-07-29 14:47:44 +0200687 vcpu->arch.shared->dsisr =
688 to_svcpu(vcpu)->fault_dsisr & ~DSISR_NOHPTE;
689 vcpu->arch.shared->dsisr |= DSISR_PROTFAULT;
Alexander Graf666e7252010-07-29 14:47:43 +0200690 vcpu->arch.shared->msr |=
691 (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000692 kvmppc_book3s_queue_irqprio(vcpu, vec);
693 } else if (page_found == -EINVAL) {
694 /* Page not found in guest SLB */
Alexander Graf5e030182010-07-29 14:47:45 +0200695 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000696 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
697 } else if (!is_mmio &&
698 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
699 /* The guest's PTE is not mapped yet. Map on the host */
700 kvmppc_mmu_map_page(vcpu, &pte);
701 if (data)
702 vcpu->stat.sp_storage++;
703 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
704 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
705 kvmppc_patch_dcbz(vcpu, &pte);
706 } else {
707 /* MMIO */
708 vcpu->stat.mmio_exits++;
709 vcpu->arch.paddr_accessed = pte.raddr;
710 r = kvmppc_emulate_mmio(run, vcpu);
711 if ( r == RESUME_HOST_NV )
712 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000713 }
714
715 return r;
716}
717
Alexander Graf180a34d2010-01-15 14:49:11 +0100718static inline int get_fpr_index(int i)
719{
720#ifdef CONFIG_VSX
721 i *= 2;
722#endif
723 return i;
724}
725
726/* Give up external provider (FPU, Altivec, VSX) */
Alexander Grafaba3bd72010-02-19 11:00:39 +0100727void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
Alexander Graf180a34d2010-01-15 14:49:11 +0100728{
729 struct thread_struct *t = &current->thread;
730 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100731#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100732 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100733#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100734 u64 *thread_fpr = (u64*)t->fpr;
735 int i;
736
737 if (!(vcpu->arch.guest_owned_ext & msr))
738 return;
739
740#ifdef DEBUG_EXT
741 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
742#endif
743
744 switch (msr) {
745 case MSR_FP:
746 giveup_fpu(current);
747 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
748 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
749
750 vcpu->arch.fpscr = t->fpscr.val;
751 break;
752 case MSR_VEC:
753#ifdef CONFIG_ALTIVEC
754 giveup_altivec(current);
755 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
756 vcpu->arch.vscr = t->vscr;
757#endif
758 break;
759 case MSR_VSX:
760#ifdef CONFIG_VSX
761 __giveup_vsx(current);
762 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
763 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
764#endif
765 break;
766 default:
767 BUG();
768 }
769
770 vcpu->arch.guest_owned_ext &= ~msr;
771 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100772 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100773}
774
Alexander Graf89632212010-03-24 21:48:21 +0100775static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100776{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200777 ulong srr0 = kvmppc_get_pc(vcpu);
778 u32 last_inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100779 int ret;
780
Alexander Grafc7f38f42010-04-16 00:11:40 +0200781 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100782 if (ret == -ENOENT) {
Alexander Graf666e7252010-07-29 14:47:43 +0200783 ulong msr = vcpu->arch.shared->msr;
784
785 msr = kvmppc_set_field(msr, 33, 33, 1);
786 msr = kvmppc_set_field(msr, 34, 36, 0);
787 vcpu->arch.shared->msr = kvmppc_set_field(msr, 42, 47, 0);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100788 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
Alexander Graf89632212010-03-24 21:48:21 +0100789 return EMULATE_AGAIN;
790 }
791
792 return EMULATE_DONE;
793}
794
795static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
796{
797
798 /* Need to do paired single emulation? */
799 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
800 return EMULATE_DONE;
801
802 /* Read out the instruction */
803 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100804 /* Need to emulate */
805 return EMULATE_FAIL;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100806
807 return EMULATE_AGAIN;
808}
809
Alexander Graf180a34d2010-01-15 14:49:11 +0100810/* Handle external providers (FPU, Altivec, VSX) */
811static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
812 ulong msr)
813{
814 struct thread_struct *t = &current->thread;
815 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100816#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100817 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100818#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100819 u64 *thread_fpr = (u64*)t->fpr;
820 int i;
821
Alexander Graf3c402a72010-02-19 11:00:32 +0100822 /* When we have paired singles, we emulate in software */
823 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
824 return RESUME_GUEST;
825
Alexander Graf666e7252010-07-29 14:47:43 +0200826 if (!(vcpu->arch.shared->msr & msr)) {
Alexander Graf180a34d2010-01-15 14:49:11 +0100827 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
828 return RESUME_GUEST;
829 }
830
Alexander Grafc2453692010-03-24 21:48:22 +0100831 /* We already own the ext */
832 if (vcpu->arch.guest_owned_ext & msr) {
833 return RESUME_GUEST;
834 }
835
Alexander Graf180a34d2010-01-15 14:49:11 +0100836#ifdef DEBUG_EXT
837 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
838#endif
839
840 current->thread.regs->msr |= msr;
841
842 switch (msr) {
843 case MSR_FP:
844 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
845 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
846
847 t->fpscr.val = vcpu->arch.fpscr;
848 t->fpexc_mode = 0;
849 kvmppc_load_up_fpu();
850 break;
851 case MSR_VEC:
852#ifdef CONFIG_ALTIVEC
853 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
854 t->vscr = vcpu->arch.vscr;
855 t->vrsave = -1;
856 kvmppc_load_up_altivec();
857#endif
858 break;
859 case MSR_VSX:
860#ifdef CONFIG_VSX
861 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
862 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
863 kvmppc_load_up_vsx();
864#endif
865 break;
866 default:
867 BUG();
868 }
869
870 vcpu->arch.guest_owned_ext |= msr;
871
Alexander Grafa76f8492010-01-15 14:49:14 +0100872 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100873
874 return RESUME_GUEST;
875}
876
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000877int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
878 unsigned int exit_nr)
879{
880 int r = RESUME_HOST;
881
882 vcpu->stat.sum_exits++;
883
884 run->exit_reason = KVM_EXIT_UNKNOWN;
885 run->ready_for_interrupt_injection = 1;
Alexander Grafbed1ed92010-08-02 11:06:26 +0200886
887 trace_kvm_book3s_exit(exit_nr, vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000888 kvm_resched(vcpu);
889 switch (exit_nr) {
890 case BOOK3S_INTERRUPT_INST_STORAGE:
891 vcpu->stat.pf_instruc++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200892
893#ifdef CONFIG_PPC_BOOK3S_32
894 /* We set segments as unused segments when invalidating them. So
895 * treat the respective fault as segment fault. */
896 if (to_svcpu(vcpu)->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT]
897 == SR_INVALID) {
898 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
899 r = RESUME_GUEST;
900 break;
901 }
902#endif
903
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000904 /* only care about PTEG not found errors, but leave NX alone */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200905 if (to_svcpu(vcpu)->shadow_srr1 & 0x40000000) {
906 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000907 vcpu->stat.sp_instruc++;
908 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
909 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
910 /*
911 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
912 * so we can't use the NX bit inside the guest. Let's cross our fingers,
913 * that no guest that needs the dcbz hack does NX.
914 */
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200915 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100916 r = RESUME_GUEST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000917 } else {
Alexander Graf666e7252010-07-29 14:47:43 +0200918 vcpu->arch.shared->msr |=
919 to_svcpu(vcpu)->shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000920 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000921 r = RESUME_GUEST;
922 }
923 break;
924 case BOOK3S_INTERRUPT_DATA_STORAGE:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200925 {
926 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000927 vcpu->stat.pf_storage++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200928
929#ifdef CONFIG_PPC_BOOK3S_32
930 /* We set segments as unused segments when invalidating them. So
931 * treat the respective fault as segment fault. */
932 if ((to_svcpu(vcpu)->sr[dar >> SID_SHIFT]) == SR_INVALID) {
933 kvmppc_mmu_map_segment(vcpu, dar);
934 r = RESUME_GUEST;
935 break;
936 }
937#endif
938
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000939 /* The only case we need to handle is missing shadow PTEs */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200940 if (to_svcpu(vcpu)->fault_dsisr & DSISR_NOHPTE) {
941 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000942 } else {
Alexander Graf5e030182010-07-29 14:47:45 +0200943 vcpu->arch.shared->dar = dar;
Alexander Grafd562de42010-07-29 14:47:44 +0200944 vcpu->arch.shared->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000945 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000946 r = RESUME_GUEST;
947 }
948 break;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200949 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000950 case BOOK3S_INTERRUPT_DATA_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200951 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
Alexander Graf5e030182010-07-29 14:47:45 +0200952 vcpu->arch.shared->dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000953 kvmppc_book3s_queue_irqprio(vcpu,
954 BOOK3S_INTERRUPT_DATA_SEGMENT);
955 }
956 r = RESUME_GUEST;
957 break;
958 case BOOK3S_INTERRUPT_INST_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200959 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000960 kvmppc_book3s_queue_irqprio(vcpu,
961 BOOK3S_INTERRUPT_INST_SEGMENT);
962 }
963 r = RESUME_GUEST;
964 break;
965 /* We're good on these - the host merely wanted to get our attention */
966 case BOOK3S_INTERRUPT_DECREMENTER:
967 vcpu->stat.dec_exits++;
968 r = RESUME_GUEST;
969 break;
970 case BOOK3S_INTERRUPT_EXTERNAL:
971 vcpu->stat.ext_intr_exits++;
972 r = RESUME_GUEST;
973 break;
Alexander Graf7fdaec92010-04-20 02:49:47 +0200974 case BOOK3S_INTERRUPT_PERFMON:
975 r = RESUME_GUEST;
976 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000977 case BOOK3S_INTERRUPT_PROGRAM:
978 {
979 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100980 ulong flags;
981
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100982program_interrupt:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200983 flags = to_svcpu(vcpu)->shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000984
Alexander Graf666e7252010-07-29 14:47:43 +0200985 if (vcpu->arch.shared->msr & MSR_PR) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000986#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +0200987 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 +0000988#endif
Alexander Grafc7f38f42010-04-16 00:11:40 +0200989 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000990 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100991 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000992 r = RESUME_GUEST;
993 break;
994 }
995 }
996
997 vcpu->stat.emulated_inst_exits++;
998 er = kvmppc_emulate_instruction(run, vcpu);
999 switch (er) {
1000 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +01001001 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001002 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +01001003 case EMULATE_AGAIN:
1004 r = RESUME_GUEST;
1005 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001006 case EMULATE_FAIL:
1007 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +02001008 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
Alexander Grafff1ca3f2010-01-08 02:58:09 +01001009 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001010 r = RESUME_GUEST;
1011 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +01001012 case EMULATE_DO_MMIO:
1013 run->exit_reason = KVM_EXIT_MMIO;
1014 r = RESUME_HOST_NV;
1015 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001016 default:
1017 BUG();
1018 }
1019 break;
1020 }
1021 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafad0a0482010-03-24 21:48:30 +01001022 if (vcpu->arch.osi_enabled &&
1023 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
1024 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
Alexander Graf2a342ed2010-07-29 14:47:48 +02001025 /* MOL hypercalls */
Alexander Grafad0a0482010-03-24 21:48:30 +01001026 u64 *gprs = run->osi.gprs;
1027 int i;
1028
1029 run->exit_reason = KVM_EXIT_OSI;
1030 for (i = 0; i < 32; i++)
1031 gprs[i] = kvmppc_get_gpr(vcpu, i);
1032 vcpu->arch.osi_needed = 1;
1033 r = RESUME_HOST_NV;
Alexander Graf2a342ed2010-07-29 14:47:48 +02001034 } else if (!(vcpu->arch.shared->msr & MSR_PR) &&
1035 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
1036 /* KVM PV hypercalls */
1037 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1038 r = RESUME_GUEST;
Alexander Grafad0a0482010-03-24 21:48:30 +01001039 } else {
Alexander Graf2a342ed2010-07-29 14:47:48 +02001040 /* Guest syscalls */
Alexander Grafad0a0482010-03-24 21:48:30 +01001041 vcpu->stat.syscall_exits++;
1042 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1043 r = RESUME_GUEST;
1044 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001045 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001046 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001047 case BOOK3S_INTERRUPT_ALTIVEC:
1048 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +01001049 {
1050 int ext_msr = 0;
1051
1052 switch (exit_nr) {
1053 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
1054 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
1055 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
1056 }
1057
1058 switch (kvmppc_check_ext(vcpu, exit_nr)) {
1059 case EMULATE_DONE:
1060 /* everything ok - let's enable the ext */
1061 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
1062 break;
1063 case EMULATE_FAIL:
1064 /* we need to emulate this instruction */
1065 goto program_interrupt;
1066 break;
1067 default:
1068 /* nothing to worry about - go again */
1069 break;
1070 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001071 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +01001072 }
Alexander Grafca7f4202010-03-24 21:48:28 +01001073 case BOOK3S_INTERRUPT_ALIGNMENT:
1074 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
Alexander Grafd562de42010-07-29 14:47:44 +02001075 vcpu->arch.shared->dsisr = kvmppc_alignment_dsisr(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001076 kvmppc_get_last_inst(vcpu));
Alexander Graf5e030182010-07-29 14:47:45 +02001077 vcpu->arch.shared->dar = kvmppc_alignment_dar(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +02001078 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +01001079 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1080 }
1081 r = RESUME_GUEST;
1082 break;
Alexander Graf180a34d2010-01-15 14:49:11 +01001083 case BOOK3S_INTERRUPT_MACHINE_CHECK:
1084 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001085 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1086 r = RESUME_GUEST;
1087 break;
1088 default:
1089 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +01001090 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +02001091 exit_nr, kvmppc_get_pc(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001092 r = RESUME_HOST;
1093 BUG();
1094 break;
1095 }
1096
1097
1098 if (!(r & RESUME_HOST)) {
1099 /* To avoid clobbering exit_reason, only check for signals if
1100 * we aren't already exiting to userspace for some other
1101 * reason. */
1102 if (signal_pending(current)) {
1103#ifdef EXIT_DEBUG
1104 printk(KERN_EMERG "KVM: Going back to host\n");
1105#endif
1106 vcpu->stat.signal_exits++;
1107 run->exit_reason = KVM_EXIT_INTR;
1108 r = -EINTR;
1109 } else {
1110 /* In case an interrupt came in that was triggered
1111 * from userspace (like DEC), we need to check what
1112 * to inject now! */
1113 kvmppc_core_deliver_interrupts(vcpu);
1114 }
1115 }
1116
Alexander Grafbed1ed92010-08-02 11:06:26 +02001117 trace_kvm_book3s_reenter(r, vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001118
1119 return r;
1120}
1121
1122int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1123{
1124 return 0;
1125}
1126
1127int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1128{
1129 int i;
1130
Alexander Grafc7f38f42010-04-16 00:11:40 +02001131 regs->pc = kvmppc_get_pc(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001132 regs->cr = kvmppc_get_cr(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001133 regs->ctr = kvmppc_get_ctr(vcpu);
1134 regs->lr = kvmppc_get_lr(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001135 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001136 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +02001137 regs->srr0 = vcpu->arch.shared->srr0;
1138 regs->srr1 = vcpu->arch.shared->srr1;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001139 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001140 regs->sprg0 = vcpu->arch.shared->sprg0;
1141 regs->sprg1 = vcpu->arch.shared->sprg1;
1142 regs->sprg2 = vcpu->arch.shared->sprg2;
1143 regs->sprg3 = vcpu->arch.shared->sprg3;
Peter Tyserbc9c1932010-12-29 13:51:25 -06001144 regs->sprg4 = vcpu->arch.sprg4;
1145 regs->sprg5 = vcpu->arch.sprg5;
1146 regs->sprg6 = vcpu->arch.sprg6;
1147 regs->sprg7 = vcpu->arch.sprg7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001148
1149 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001150 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001151
1152 return 0;
1153}
1154
1155int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1156{
1157 int i;
1158
Alexander Grafc7f38f42010-04-16 00:11:40 +02001159 kvmppc_set_pc(vcpu, regs->pc);
Alexander Graf992b5b22010-01-08 02:58:02 +01001160 kvmppc_set_cr(vcpu, regs->cr);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001161 kvmppc_set_ctr(vcpu, regs->ctr);
1162 kvmppc_set_lr(vcpu, regs->lr);
Alexander Graf992b5b22010-01-08 02:58:02 +01001163 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001164 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +02001165 vcpu->arch.shared->srr0 = regs->srr0;
1166 vcpu->arch.shared->srr1 = regs->srr1;
Alexander Grafa73a9592010-07-29 14:47:47 +02001167 vcpu->arch.shared->sprg0 = regs->sprg0;
1168 vcpu->arch.shared->sprg1 = regs->sprg1;
1169 vcpu->arch.shared->sprg2 = regs->sprg2;
1170 vcpu->arch.shared->sprg3 = regs->sprg3;
Peter Tyserbc9c1932010-12-29 13:51:25 -06001171 vcpu->arch.sprg4 = regs->sprg4;
1172 vcpu->arch.sprg5 = regs->sprg5;
1173 vcpu->arch.sprg6 = regs->sprg6;
1174 vcpu->arch.sprg7 = regs->sprg7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001175
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001176 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1177 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001178
1179 return 0;
1180}
1181
1182int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1183 struct kvm_sregs *sregs)
1184{
Alexander Grafe15a1132009-11-30 03:02:02 +00001185 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1186 int i;
1187
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001188 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +00001189
1190 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1191 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1192 for (i = 0; i < 64; i++) {
1193 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
1194 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1195 }
1196 } else {
Alexander Grafdf1bfa22010-08-03 02:29:27 +02001197 for (i = 0; i < 16; i++)
1198 sregs->u.s.ppc32.sr[i] = vcpu->arch.shared->sr[i];
1199
Alexander Grafe15a1132009-11-30 03:02:02 +00001200 for (i = 0; i < 8; i++) {
1201 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1202 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1203 }
1204 }
Avi Kivity98001d82010-05-13 11:05:49 +03001205
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001206 return 0;
1207}
1208
1209int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1210 struct kvm_sregs *sregs)
1211{
Alexander Grafe15a1132009-11-30 03:02:02 +00001212 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1213 int i;
1214
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001215 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001216
1217 vcpu3s->sdr1 = sregs->u.s.sdr1;
1218 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1219 for (i = 0; i < 64; i++) {
1220 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1221 sregs->u.s.ppc64.slb[i].slbe);
1222 }
1223 } else {
1224 for (i = 0; i < 16; i++) {
1225 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1226 }
1227 for (i = 0; i < 8; i++) {
1228 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1229 (u32)sregs->u.s.ppc32.ibat[i]);
1230 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1231 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1232 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1233 (u32)sregs->u.s.ppc32.dbat[i]);
1234 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1235 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1236 }
1237 }
1238
1239 /* Flush the MMU after messing with the segments */
1240 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Avi Kivity98001d82010-05-13 11:05:49 +03001241
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001242 return 0;
1243}
1244
1245int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1246{
1247 return -ENOTSUPP;
1248}
1249
1250int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1251{
1252 return -ENOTSUPP;
1253}
1254
1255int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1256 struct kvm_translation *tr)
1257{
1258 return 0;
1259}
1260
1261/*
1262 * Get (and clear) the dirty memory log for a memory slot.
1263 */
1264int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1265 struct kvm_dirty_log *log)
1266{
1267 struct kvm_memory_slot *memslot;
1268 struct kvm_vcpu *vcpu;
1269 ulong ga, ga_end;
1270 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001271 int r;
1272 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001273
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001274 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001275
1276 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1277 if (r)
1278 goto out;
1279
1280 /* If nothing is dirty, don't bother messing with page tables. */
1281 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001282 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001283
1284 ga = memslot->base_gfn << PAGE_SHIFT;
1285 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1286
1287 kvm_for_each_vcpu(n, vcpu, kvm)
1288 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1289
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001290 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001291 memset(memslot->dirty_bitmap, 0, n);
1292 }
1293
1294 r = 0;
1295out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001296 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001297 return r;
1298}
1299
1300int kvmppc_core_check_processor_compat(void)
1301{
1302 return 0;
1303}
1304
1305struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1306{
1307 struct kvmppc_vcpu_book3s *vcpu_book3s;
1308 struct kvm_vcpu *vcpu;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001309 int err = -ENOMEM;
Alexander Grafe8508942010-07-29 14:47:54 +02001310 unsigned long p;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001311
Takuya Yoshikawa26535032010-11-02 10:49:34 +09001312 vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Grafc7f38f42010-04-16 00:11:40 +02001313 if (!vcpu_book3s)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001314 goto out;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001315
Alexander Grafc7f38f42010-04-16 00:11:40 +02001316 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
1317 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
1318 if (!vcpu_book3s->shadow_vcpu)
1319 goto free_vcpu;
1320
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001321 vcpu = &vcpu_book3s->vcpu;
1322 err = kvm_vcpu_init(vcpu, kvm, id);
1323 if (err)
Alexander Grafc7f38f42010-04-16 00:11:40 +02001324 goto free_shadow_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001325
Alexander Grafe8508942010-07-29 14:47:54 +02001326 p = __get_free_page(GFP_KERNEL|__GFP_ZERO);
1327 /* the real shared page fills the last 4k of our page */
1328 vcpu->arch.shared = (void*)(p + PAGE_SIZE - 4096);
1329 if (!p)
Alexander Graf96bc4512010-07-29 14:47:42 +02001330 goto uninit_vcpu;
1331
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001332 vcpu->arch.host_retip = kvm_return_point;
1333 vcpu->arch.host_msr = mfmsr();
Alexander Graf07b09072010-04-16 00:11:53 +02001334#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001335 /* default to book3s_64 (970fx) */
1336 vcpu->arch.pvr = 0x3C0301;
Alexander Graf07b09072010-04-16 00:11:53 +02001337#else
1338 /* default to book3s_32 (750) */
1339 vcpu->arch.pvr = 0x84202;
1340#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001341 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1342 vcpu_book3s->slb_nr = 64;
1343
1344 /* remember where some real-mode handlers are */
1345 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1346 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1347 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf07b09072010-04-16 00:11:53 +02001348#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf021ec9c2010-01-08 02:58:06 +01001349 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf07b09072010-04-16 00:11:53 +02001350#else
1351 vcpu->arch.rmcall = (ulong)kvmppc_rmcall;
1352#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001353
1354 vcpu->arch.shadow_msr = MSR_USER64;
1355
Alexander Graf9cc5e952010-04-16 00:11:45 +02001356 err = kvmppc_mmu_init(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001357 if (err < 0)
Alexander Graf96bc4512010-07-29 14:47:42 +02001358 goto uninit_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001359
1360 return vcpu;
1361
Alexander Graf96bc4512010-07-29 14:47:42 +02001362uninit_vcpu:
1363 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001364free_shadow_vcpu:
1365 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001366free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001367 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001368out:
1369 return ERR_PTR(err);
1370}
1371
1372void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1373{
1374 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1375
Alexander Grafe8508942010-07-29 14:47:54 +02001376 free_page((unsigned long)vcpu->arch.shared & PAGE_MASK);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001377 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001378 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001379 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001380}
1381
1382extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1383int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1384{
1385 int ret;
Andreas Schwab49f6be82010-05-31 21:59:13 +02001386 double fpr[32][TS_FPRWIDTH];
1387 unsigned int fpscr;
1388 int fpexc_mode;
Alexander Grafa2b07662010-03-24 21:48:31 +01001389#ifdef CONFIG_ALTIVEC
Andreas Schwab49f6be82010-05-31 21:59:13 +02001390 vector128 vr[32];
1391 vector128 vscr;
1392 unsigned long uninitialized_var(vrsave);
1393 int used_vr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001394#endif
1395#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001396 int used_vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001397#endif
Alexander Graf180a34d2010-01-15 14:49:11 +01001398 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001399
1400 /* No need to go into the guest when all we do is going out */
1401 if (signal_pending(current)) {
1402 kvm_run->exit_reason = KVM_EXIT_INTR;
1403 return -EINTR;
1404 }
1405
Alexander Graf180a34d2010-01-15 14:49:11 +01001406 /* Save FPU state in stack */
1407 if (current->thread.regs->msr & MSR_FP)
1408 giveup_fpu(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001409 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
1410 fpscr = current->thread.fpscr.val;
1411 fpexc_mode = current->thread.fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001412
1413#ifdef CONFIG_ALTIVEC
1414 /* Save Altivec state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001415 used_vr = current->thread.used_vr;
1416 if (used_vr) {
Alexander Graf180a34d2010-01-15 14:49:11 +01001417 if (current->thread.regs->msr & MSR_VEC)
1418 giveup_altivec(current);
Andreas Schwab49f6be82010-05-31 21:59:13 +02001419 memcpy(vr, current->thread.vr, sizeof(current->thread.vr));
1420 vscr = current->thread.vscr;
1421 vrsave = current->thread.vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001422 }
Alexander Graf180a34d2010-01-15 14:49:11 +01001423#endif
1424
1425#ifdef CONFIG_VSX
1426 /* Save VSX state in stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001427 used_vsr = current->thread.used_vsr;
1428 if (used_vsr && (current->thread.regs->msr & MSR_VSX))
Alexander Graf180a34d2010-01-15 14:49:11 +01001429 __giveup_vsx(current);
Alexander Graf180a34d2010-01-15 14:49:11 +01001430#endif
1431
1432 /* Remember the MSR with disabled extensions */
1433 ext_msr = current->thread.regs->msr;
1434
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001435 /* XXX we get called with irq disabled - change that! */
1436 local_irq_enable();
1437
Alexander Grafd1bab742010-02-19 11:00:35 +01001438 /* Preload FPU if it's enabled */
Alexander Graf666e7252010-07-29 14:47:43 +02001439 if (vcpu->arch.shared->msr & MSR_FP)
Alexander Grafd1bab742010-02-19 11:00:35 +01001440 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1441
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001442 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1443
1444 local_irq_disable();
1445
Alexander Graf180a34d2010-01-15 14:49:11 +01001446 current->thread.regs->msr = ext_msr;
1447
1448 /* Make sure we save the guest FPU/Altivec/VSX state */
1449 kvmppc_giveup_ext(vcpu, MSR_FP);
1450 kvmppc_giveup_ext(vcpu, MSR_VEC);
1451 kvmppc_giveup_ext(vcpu, MSR_VSX);
1452
1453 /* Restore FPU state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001454 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
1455 current->thread.fpscr.val = fpscr;
1456 current->thread.fpexc_mode = fpexc_mode;
Alexander Graf180a34d2010-01-15 14:49:11 +01001457
1458#ifdef CONFIG_ALTIVEC
1459 /* Restore Altivec state from stack */
Andreas Schwab49f6be82010-05-31 21:59:13 +02001460 if (used_vr && current->thread.used_vr) {
1461 memcpy(current->thread.vr, vr, sizeof(current->thread.vr));
1462 current->thread.vscr = vscr;
1463 current->thread.vrsave = vrsave;
Alexander Graf180a34d2010-01-15 14:49:11 +01001464 }
Andreas Schwab49f6be82010-05-31 21:59:13 +02001465 current->thread.used_vr = used_vr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001466#endif
1467
1468#ifdef CONFIG_VSX
Andreas Schwab49f6be82010-05-31 21:59:13 +02001469 current->thread.used_vsr = used_vsr;
Alexander Graf180a34d2010-01-15 14:49:11 +01001470#endif
1471
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001472 return ret;
1473}
1474
1475static int kvmppc_book3s_init(void)
1476{
Alexander Graffef093be2010-06-30 15:18:46 +02001477 int r;
1478
1479 r = kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), 0,
1480 THIS_MODULE);
1481
1482 if (r)
1483 return r;
1484
1485 r = kvmppc_mmu_hpte_sysinit();
1486
1487 return r;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001488}
1489
1490static void kvmppc_book3s_exit(void)
1491{
Alexander Graffef093be2010-06-30 15:18:46 +02001492 kvmppc_mmu_hpte_sysexit();
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001493 kvm_exit();
1494}
1495
1496module_init(kvmppc_book3s_init);
1497module_exit(kvmppc_book3s_exit);