blob: c058f1a5c095ac7b2361c3939d47627fc52c2f39 [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>
19
20#include <asm/reg.h>
21#include <asm/cputable.h>
22#include <asm/cacheflush.h>
23#include <asm/tlbflush.h>
24#include <asm/uaccess.h>
25#include <asm/io.h>
26#include <asm/kvm_ppc.h>
27#include <asm/kvm_book3s.h>
28#include <asm/mmu_context.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/gfp.h>
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000030#include <linux/sched.h>
31#include <linux/vmalloc.h>
32
33#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
34
35/* #define EXIT_DEBUG */
36/* #define EXIT_DEBUG_SIMPLE */
Alexander Graf180a34d2010-01-15 14:49:11 +010037/* #define DEBUG_EXT */
38
Alexander Grafc8c0b6f2010-02-19 11:00:34 +010039static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
40 ulong msr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000041
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000042struct kvm_stats_debugfs_item debugfs_entries[] = {
43 { "exits", VCPU_STAT(sum_exits) },
44 { "mmio", VCPU_STAT(mmio_exits) },
45 { "sig", VCPU_STAT(signal_exits) },
46 { "sysc", VCPU_STAT(syscall_exits) },
47 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
48 { "dec", VCPU_STAT(dec_exits) },
49 { "ext_intr", VCPU_STAT(ext_intr_exits) },
50 { "queue_intr", VCPU_STAT(queue_intr) },
51 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
52 { "pf_storage", VCPU_STAT(pf_storage) },
53 { "sp_storage", VCPU_STAT(sp_storage) },
54 { "pf_instruc", VCPU_STAT(pf_instruc) },
55 { "sp_instruc", VCPU_STAT(sp_instruc) },
56 { "ld", VCPU_STAT(ld) },
57 { "ld_slow", VCPU_STAT(ld_slow) },
58 { "st", VCPU_STAT(st) },
59 { "st_slow", VCPU_STAT(st_slow) },
60 { NULL }
61};
62
63void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
64{
65}
66
67void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
68{
69}
70
71void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
72{
73 memcpy(get_paca()->kvm_slb, to_book3s(vcpu)->slb_shadow, sizeof(get_paca()->kvm_slb));
Alexander Graf7e57cba2010-01-08 02:58:03 +010074 memcpy(&get_paca()->shadow_vcpu, &to_book3s(vcpu)->shadow_vcpu,
75 sizeof(get_paca()->shadow_vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000076 get_paca()->kvm_slb_max = to_book3s(vcpu)->slb_shadow_max;
77}
78
79void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
80{
81 memcpy(to_book3s(vcpu)->slb_shadow, get_paca()->kvm_slb, sizeof(get_paca()->kvm_slb));
Alexander Graf7e57cba2010-01-08 02:58:03 +010082 memcpy(&to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
83 sizeof(get_paca()->shadow_vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000084 to_book3s(vcpu)->slb_shadow_max = get_paca()->kvm_slb_max;
Alexander Graf180a34d2010-01-15 14:49:11 +010085
86 kvmppc_giveup_ext(vcpu, MSR_FP);
87 kvmppc_giveup_ext(vcpu, MSR_VEC);
88 kvmppc_giveup_ext(vcpu, MSR_VSX);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000089}
90
Alexander Graf0bb1fb72009-12-21 20:21:25 +010091#if defined(EXIT_DEBUG)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000092static u32 kvmppc_get_dec(struct kvm_vcpu *vcpu)
93{
94 u64 jd = mftb() - vcpu->arch.dec_jiffies;
95 return vcpu->arch.dec - jd;
96}
97#endif
98
Alexander Grafa76f8492010-01-15 14:49:14 +010099static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
100{
101 vcpu->arch.shadow_msr = vcpu->arch.msr;
102 /* Guest MSR values */
103 vcpu->arch.shadow_msr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE |
104 MSR_BE | MSR_DE;
105 /* Process MSR values */
106 vcpu->arch.shadow_msr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR |
107 MSR_EE;
108 /* External providers the guest reserved */
109 vcpu->arch.shadow_msr |= (vcpu->arch.msr & vcpu->arch.guest_owned_ext);
110 /* 64-bit Process MSR values */
111#ifdef CONFIG_PPC_BOOK3S_64
112 vcpu->arch.shadow_msr |= MSR_ISF | MSR_HV;
113#endif
114}
115
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000116void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
117{
118 ulong old_msr = vcpu->arch.msr;
119
120#ifdef EXIT_DEBUG
121 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
122#endif
Alexander Grafa76f8492010-01-15 14:49:14 +0100123
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000124 msr &= to_book3s(vcpu)->msr_mask;
125 vcpu->arch.msr = msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100126 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000127
128 if (msr & (MSR_WE|MSR_POW)) {
129 if (!vcpu->arch.pending_exceptions) {
130 kvm_vcpu_block(vcpu);
131 vcpu->stat.halt_wakeup++;
132 }
133 }
134
135 if (((vcpu->arch.msr & (MSR_IR|MSR_DR)) != (old_msr & (MSR_IR|MSR_DR))) ||
136 (vcpu->arch.msr & MSR_PR) != (old_msr & MSR_PR)) {
Alexander Graf3eeafd72010-03-24 21:48:17 +0100137 bool dr = (vcpu->arch.msr & MSR_DR) ? true : false;
138 bool ir = (vcpu->arch.msr & MSR_IR) ? true : false;
139
140 /* Flush split mode PTEs */
141 if (dr != ir)
142 kvmppc_mmu_pte_vflush(vcpu, VSID_SPLIT_MASK,
143 VSID_SPLIT_MASK);
144
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000145 kvmppc_mmu_flush_segments(vcpu);
146 kvmppc_mmu_map_segment(vcpu, vcpu->arch.pc);
147 }
Alexander Grafd1bab742010-02-19 11:00:35 +0100148
149 /* Preload FPU if it's enabled */
150 if (vcpu->arch.msr & MSR_FP)
151 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000152}
153
154void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
155{
156 vcpu->arch.srr0 = vcpu->arch.pc;
157 vcpu->arch.srr1 = vcpu->arch.msr | flags;
158 vcpu->arch.pc = to_book3s(vcpu)->hior + vec;
159 vcpu->arch.mmu.reset_msr(vcpu);
160}
161
Alexander Graf583617b2009-12-21 20:21:23 +0100162static int kvmppc_book3s_vec2irqprio(unsigned int vec)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000163{
164 unsigned int prio;
165
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000166 switch (vec) {
167 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
168 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
169 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
170 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
171 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
172 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
173 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
174 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
175 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
176 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
177 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
178 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
179 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
180 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
181 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
182 default: prio = BOOK3S_IRQPRIO_MAX; break;
183 }
184
Alexander Graf583617b2009-12-21 20:21:23 +0100185 return prio;
186}
187
Alexander Graf7706664d2009-12-21 20:21:24 +0100188static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
189 unsigned int vec)
190{
191 clear_bit(kvmppc_book3s_vec2irqprio(vec),
192 &vcpu->arch.pending_exceptions);
193}
194
Alexander Graf583617b2009-12-21 20:21:23 +0100195void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
196{
197 vcpu->stat.queue_intr++;
198
199 set_bit(kvmppc_book3s_vec2irqprio(vec),
200 &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000201#ifdef EXIT_DEBUG
202 printk(KERN_INFO "Queueing interrupt %x\n", vec);
203#endif
204}
205
206
Alexander Graf25a8a022010-01-08 02:58:07 +0100207void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000208{
Alexander Graf25a8a022010-01-08 02:58:07 +0100209 to_book3s(vcpu)->prog_flags = flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000210 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM);
211}
212
213void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
214{
215 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
216}
217
218int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
219{
220 return test_bit(BOOK3S_INTERRUPT_DECREMENTER >> 7, &vcpu->arch.pending_exceptions);
221}
222
Alexander Graf7706664d2009-12-21 20:21:24 +0100223void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
224{
225 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
226}
227
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000228void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
229 struct kvm_interrupt *irq)
230{
231 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
232}
233
Alexander Graf18978762010-03-24 21:48:18 +0100234void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
235 struct kvm_interrupt *irq)
236{
237 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
238}
239
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000240int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
241{
242 int deliver = 1;
243 int vec = 0;
Alexander Graf25a8a022010-01-08 02:58:07 +0100244 ulong flags = 0ULL;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000245
246 switch (priority) {
247 case BOOK3S_IRQPRIO_DECREMENTER:
248 deliver = vcpu->arch.msr & MSR_EE;
249 vec = BOOK3S_INTERRUPT_DECREMENTER;
250 break;
251 case BOOK3S_IRQPRIO_EXTERNAL:
252 deliver = vcpu->arch.msr & MSR_EE;
253 vec = BOOK3S_INTERRUPT_EXTERNAL;
254 break;
255 case BOOK3S_IRQPRIO_SYSTEM_RESET:
256 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
257 break;
258 case BOOK3S_IRQPRIO_MACHINE_CHECK:
259 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
260 break;
261 case BOOK3S_IRQPRIO_DATA_STORAGE:
262 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
263 break;
264 case BOOK3S_IRQPRIO_INST_STORAGE:
265 vec = BOOK3S_INTERRUPT_INST_STORAGE;
266 break;
267 case BOOK3S_IRQPRIO_DATA_SEGMENT:
268 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
269 break;
270 case BOOK3S_IRQPRIO_INST_SEGMENT:
271 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
272 break;
273 case BOOK3S_IRQPRIO_ALIGNMENT:
274 vec = BOOK3S_INTERRUPT_ALIGNMENT;
275 break;
276 case BOOK3S_IRQPRIO_PROGRAM:
277 vec = BOOK3S_INTERRUPT_PROGRAM;
Alexander Graf25a8a022010-01-08 02:58:07 +0100278 flags = to_book3s(vcpu)->prog_flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000279 break;
280 case BOOK3S_IRQPRIO_VSX:
281 vec = BOOK3S_INTERRUPT_VSX;
282 break;
283 case BOOK3S_IRQPRIO_ALTIVEC:
284 vec = BOOK3S_INTERRUPT_ALTIVEC;
285 break;
286 case BOOK3S_IRQPRIO_FP_UNAVAIL:
287 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
288 break;
289 case BOOK3S_IRQPRIO_SYSCALL:
290 vec = BOOK3S_INTERRUPT_SYSCALL;
291 break;
292 case BOOK3S_IRQPRIO_DEBUG:
293 vec = BOOK3S_INTERRUPT_TRACE;
294 break;
295 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
296 vec = BOOK3S_INTERRUPT_PERFMON;
297 break;
298 default:
299 deliver = 0;
300 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
301 break;
302 }
303
304#if 0
305 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
306#endif
307
308 if (deliver)
Alexander Graf25a8a022010-01-08 02:58:07 +0100309 kvmppc_inject_interrupt(vcpu, vec, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000310
311 return deliver;
312}
313
314void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
315{
316 unsigned long *pending = &vcpu->arch.pending_exceptions;
317 unsigned int priority;
318
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000319#ifdef EXIT_DEBUG
320 if (vcpu->arch.pending_exceptions)
321 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
322#endif
323 priority = __ffs(*pending);
324 while (priority <= (sizeof(unsigned int) * 8)) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100325 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
326 (priority != BOOK3S_IRQPRIO_DECREMENTER)) {
327 /* DEC interrupts get cleared by mtdec */
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000328 clear_bit(priority, &vcpu->arch.pending_exceptions);
329 break;
330 }
331
332 priority = find_next_bit(pending,
333 BITS_PER_BYTE * sizeof(*pending),
334 priority + 1);
335 }
336}
337
338void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
339{
Alexander Grafe15a1132009-11-30 03:02:02 +0000340 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000341 vcpu->arch.pvr = pvr;
342 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
343 kvmppc_mmu_book3s_64_init(vcpu);
344 to_book3s(vcpu)->hior = 0xfff00000;
345 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
346 } else {
347 kvmppc_mmu_book3s_32_init(vcpu);
348 to_book3s(vcpu)->hior = 0;
349 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
350 }
351
352 /* If we are in hypervisor level on 970, we can tell the CPU to
353 * treat DCBZ as 32 bytes store */
354 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
355 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
356 !strcmp(cur_cpu_spec->platform, "ppc970"))
357 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
358
359}
360
361/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
362 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
363 * emulate 32 bytes dcbz length.
364 *
365 * The Book3s_64 inventors also realized this case and implemented a special bit
366 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
367 *
368 * My approach here is to patch the dcbz instruction on executing pages.
369 */
370static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
371{
372 bool touched = false;
373 hva_t hpage;
374 u32 *page;
375 int i;
376
377 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
378 if (kvm_is_error_hva(hpage))
379 return;
380
381 hpage |= pte->raddr & ~PAGE_MASK;
382 hpage &= ~0xFFFULL;
383
384 page = vmalloc(HW_PAGE_SIZE);
385
386 if (copy_from_user(page, (void __user *)hpage, HW_PAGE_SIZE))
387 goto out;
388
389 for (i=0; i < HW_PAGE_SIZE / 4; i++)
390 if ((page[i] & 0xff0007ff) == INS_DCBZ) {
391 page[i] &= 0xfffffff7; // reserved instruction, so we trap
392 touched = true;
393 }
394
395 if (touched)
396 copy_to_user((void __user *)hpage, page, HW_PAGE_SIZE);
397
398out:
399 vfree(page);
400}
401
402static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
403 struct kvmppc_pte *pte)
404{
405 int relocated = (vcpu->arch.msr & (data ? MSR_DR : MSR_IR));
406 int r;
407
408 if (relocated) {
409 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
410 } else {
411 pte->eaddr = eaddr;
412 pte->raddr = eaddr & 0xffffffff;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100413 pte->vpage = VSID_REAL | eaddr >> 12;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000414 pte->may_read = true;
415 pte->may_write = true;
416 pte->may_execute = true;
417 r = 0;
418 }
419
420 return r;
421}
422
423static hva_t kvmppc_bad_hva(void)
424{
425 return PAGE_OFFSET;
426}
427
428static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
429 bool read)
430{
431 hva_t hpage;
432
433 if (read && !pte->may_read)
434 goto err;
435
436 if (!read && !pte->may_write)
437 goto err;
438
439 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
440 if (kvm_is_error_hva(hpage))
441 goto err;
442
443 return hpage | (pte->raddr & ~PAGE_MASK);
444err:
445 return kvmppc_bad_hva();
446}
447
Alexander Graf5467a972010-02-19 11:00:38 +0100448int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
449 bool data)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000450{
451 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100452 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000453
454 vcpu->stat.st++;
455
Alexander Graf5467a972010-02-19 11:00:38 +0100456 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
457 goto nopte;
458
459 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000460
461 hva = kvmppc_pte_to_hva(vcpu, &pte, false);
462 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100463 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000464
465 if (copy_to_user((void __user *)hva, ptr, size)) {
466 printk(KERN_INFO "kvmppc_st at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100467 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000468 }
469
Alexander Graf5467a972010-02-19 11:00:38 +0100470 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000471
Alexander Graf5467a972010-02-19 11:00:38 +0100472nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000473 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100474mmio:
475 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000476}
477
Alexander Graf5467a972010-02-19 11:00:38 +0100478int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000479 bool data)
480{
481 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100482 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000483
484 vcpu->stat.ld++;
485
Alexander Graf5467a972010-02-19 11:00:38 +0100486 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
487 goto nopte;
488
489 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000490
491 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
492 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100493 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000494
495 if (copy_from_user(ptr, (void __user *)hva, size)) {
496 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100497 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000498 }
499
Alexander Graf5467a972010-02-19 11:00:38 +0100500 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000501
Alexander Graf5467a972010-02-19 11:00:38 +0100502nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000503 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100504mmio:
505 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000506}
507
508static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
509{
510 return kvm_is_visible_gfn(vcpu->kvm, gfn);
511}
512
513int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
514 ulong eaddr, int vec)
515{
516 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
517 int r = RESUME_GUEST;
518 int relocated;
519 int page_found = 0;
520 struct kvmppc_pte pte;
521 bool is_mmio = false;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100522 bool dr = (vcpu->arch.msr & MSR_DR) ? true : false;
523 bool ir = (vcpu->arch.msr & MSR_IR) ? true : false;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000524
Alexander Graf3eeafd72010-03-24 21:48:17 +0100525 relocated = data ? dr : ir;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000526
527 /* Resolve real address if translation turned on */
528 if (relocated) {
529 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
530 } else {
531 pte.may_execute = true;
532 pte.may_read = true;
533 pte.may_write = true;
534 pte.raddr = eaddr & 0xffffffff;
535 pte.eaddr = eaddr;
536 pte.vpage = eaddr >> 12;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100537 }
538
539 switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
540 case 0:
541 pte.vpage |= VSID_REAL;
542 break;
543 case MSR_DR:
544 pte.vpage |= VSID_REAL_DR;
545 break;
546 case MSR_IR:
547 pte.vpage |= VSID_REAL_IR;
548 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000549 }
550
551 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
552 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
553 /*
554 * If we do the dcbz hack, we have to NX on every execution,
555 * so we can patch the executing code. This renders our guest
556 * NX-less.
557 */
558 pte.may_execute = !data;
559 }
560
561 if (page_found == -ENOENT) {
562 /* Page not found in guest PTE entries */
563 vcpu->arch.dear = vcpu->arch.fault_dear;
564 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr;
Alexander Graff7adbba2010-01-15 14:49:13 +0100565 vcpu->arch.msr |= (vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000566 kvmppc_book3s_queue_irqprio(vcpu, vec);
567 } else if (page_found == -EPERM) {
568 /* Storage protection */
569 vcpu->arch.dear = vcpu->arch.fault_dear;
570 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr & ~DSISR_NOHPTE;
571 to_book3s(vcpu)->dsisr |= DSISR_PROTFAULT;
Alexander Graff7adbba2010-01-15 14:49:13 +0100572 vcpu->arch.msr |= (vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000573 kvmppc_book3s_queue_irqprio(vcpu, vec);
574 } else if (page_found == -EINVAL) {
575 /* Page not found in guest SLB */
576 vcpu->arch.dear = vcpu->arch.fault_dear;
577 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
578 } else if (!is_mmio &&
579 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
580 /* The guest's PTE is not mapped yet. Map on the host */
581 kvmppc_mmu_map_page(vcpu, &pte);
582 if (data)
583 vcpu->stat.sp_storage++;
584 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
585 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
586 kvmppc_patch_dcbz(vcpu, &pte);
587 } else {
588 /* MMIO */
589 vcpu->stat.mmio_exits++;
590 vcpu->arch.paddr_accessed = pte.raddr;
591 r = kvmppc_emulate_mmio(run, vcpu);
592 if ( r == RESUME_HOST_NV )
593 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000594 }
595
596 return r;
597}
598
Alexander Graf180a34d2010-01-15 14:49:11 +0100599static inline int get_fpr_index(int i)
600{
601#ifdef CONFIG_VSX
602 i *= 2;
603#endif
604 return i;
605}
606
607/* Give up external provider (FPU, Altivec, VSX) */
Alexander Grafaba3bd72010-02-19 11:00:39 +0100608void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
Alexander Graf180a34d2010-01-15 14:49:11 +0100609{
610 struct thread_struct *t = &current->thread;
611 u64 *vcpu_fpr = vcpu->arch.fpr;
612 u64 *vcpu_vsx = vcpu->arch.vsr;
613 u64 *thread_fpr = (u64*)t->fpr;
614 int i;
615
616 if (!(vcpu->arch.guest_owned_ext & msr))
617 return;
618
619#ifdef DEBUG_EXT
620 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
621#endif
622
623 switch (msr) {
624 case MSR_FP:
625 giveup_fpu(current);
626 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
627 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
628
629 vcpu->arch.fpscr = t->fpscr.val;
630 break;
631 case MSR_VEC:
632#ifdef CONFIG_ALTIVEC
633 giveup_altivec(current);
634 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
635 vcpu->arch.vscr = t->vscr;
636#endif
637 break;
638 case MSR_VSX:
639#ifdef CONFIG_VSX
640 __giveup_vsx(current);
641 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
642 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
643#endif
644 break;
645 default:
646 BUG();
647 }
648
649 vcpu->arch.guest_owned_ext &= ~msr;
650 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100651 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100652}
653
Alexander Graf89632212010-03-24 21:48:21 +0100654static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100655{
656 ulong srr0 = vcpu->arch.pc;
657 int ret;
658
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100659 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &vcpu->arch.last_inst, false);
660 if (ret == -ENOENT) {
661 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 33, 33, 1);
662 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 34, 36, 0);
663 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 42, 47, 0);
664 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
Alexander Graf89632212010-03-24 21:48:21 +0100665 return EMULATE_AGAIN;
666 }
667
668 return EMULATE_DONE;
669}
670
671static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
672{
673
674 /* Need to do paired single emulation? */
675 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
676 return EMULATE_DONE;
677
678 /* Read out the instruction */
679 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100680 /* Need to emulate */
681 return EMULATE_FAIL;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100682
683 return EMULATE_AGAIN;
684}
685
Alexander Graf180a34d2010-01-15 14:49:11 +0100686/* Handle external providers (FPU, Altivec, VSX) */
687static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
688 ulong msr)
689{
690 struct thread_struct *t = &current->thread;
691 u64 *vcpu_fpr = vcpu->arch.fpr;
692 u64 *vcpu_vsx = vcpu->arch.vsr;
693 u64 *thread_fpr = (u64*)t->fpr;
694 int i;
695
Alexander Graf3c402a72010-02-19 11:00:32 +0100696 /* When we have paired singles, we emulate in software */
697 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
698 return RESUME_GUEST;
699
Alexander Graf180a34d2010-01-15 14:49:11 +0100700 if (!(vcpu->arch.msr & msr)) {
701 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
702 return RESUME_GUEST;
703 }
704
Alexander Grafc2453692010-03-24 21:48:22 +0100705 /* We already own the ext */
706 if (vcpu->arch.guest_owned_ext & msr) {
707 return RESUME_GUEST;
708 }
709
Alexander Graf180a34d2010-01-15 14:49:11 +0100710#ifdef DEBUG_EXT
711 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
712#endif
713
714 current->thread.regs->msr |= msr;
715
716 switch (msr) {
717 case MSR_FP:
718 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
719 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
720
721 t->fpscr.val = vcpu->arch.fpscr;
722 t->fpexc_mode = 0;
723 kvmppc_load_up_fpu();
724 break;
725 case MSR_VEC:
726#ifdef CONFIG_ALTIVEC
727 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
728 t->vscr = vcpu->arch.vscr;
729 t->vrsave = -1;
730 kvmppc_load_up_altivec();
731#endif
732 break;
733 case MSR_VSX:
734#ifdef CONFIG_VSX
735 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
736 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
737 kvmppc_load_up_vsx();
738#endif
739 break;
740 default:
741 BUG();
742 }
743
744 vcpu->arch.guest_owned_ext |= msr;
745
Alexander Grafa76f8492010-01-15 14:49:14 +0100746 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100747
748 return RESUME_GUEST;
749}
750
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000751int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
752 unsigned int exit_nr)
753{
754 int r = RESUME_HOST;
755
756 vcpu->stat.sum_exits++;
757
758 run->exit_reason = KVM_EXIT_UNKNOWN;
759 run->ready_for_interrupt_injection = 1;
760#ifdef EXIT_DEBUG
761 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | dec=0x%x | msr=0x%lx\n",
762 exit_nr, vcpu->arch.pc, vcpu->arch.fault_dear,
763 kvmppc_get_dec(vcpu), vcpu->arch.msr);
764#elif defined (EXIT_DEBUG_SIMPLE)
765 if ((exit_nr != 0x900) && (exit_nr != 0x500))
766 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | msr=0x%lx\n",
767 exit_nr, vcpu->arch.pc, vcpu->arch.fault_dear,
768 vcpu->arch.msr);
769#endif
770 kvm_resched(vcpu);
771 switch (exit_nr) {
772 case BOOK3S_INTERRUPT_INST_STORAGE:
773 vcpu->stat.pf_instruc++;
774 /* only care about PTEG not found errors, but leave NX alone */
Alexander Graff7adbba2010-01-15 14:49:13 +0100775 if (vcpu->arch.shadow_srr1 & 0x40000000) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000776 r = kvmppc_handle_pagefault(run, vcpu, vcpu->arch.pc, exit_nr);
777 vcpu->stat.sp_instruc++;
778 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
779 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
780 /*
781 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
782 * so we can't use the NX bit inside the guest. Let's cross our fingers,
783 * that no guest that needs the dcbz hack does NX.
784 */
785 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.pc, ~0xFFFULL);
786 } else {
Alexander Graff7adbba2010-01-15 14:49:13 +0100787 vcpu->arch.msr |= vcpu->arch.shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000788 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
789 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.pc, ~0xFFFULL);
790 r = RESUME_GUEST;
791 }
792 break;
793 case BOOK3S_INTERRUPT_DATA_STORAGE:
794 vcpu->stat.pf_storage++;
795 /* The only case we need to handle is missing shadow PTEs */
796 if (vcpu->arch.fault_dsisr & DSISR_NOHPTE) {
797 r = kvmppc_handle_pagefault(run, vcpu, vcpu->arch.fault_dear, exit_nr);
798 } else {
799 vcpu->arch.dear = vcpu->arch.fault_dear;
800 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr;
801 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
802 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.dear, ~0xFFFULL);
803 r = RESUME_GUEST;
804 }
805 break;
806 case BOOK3S_INTERRUPT_DATA_SEGMENT:
807 if (kvmppc_mmu_map_segment(vcpu, vcpu->arch.fault_dear) < 0) {
808 vcpu->arch.dear = vcpu->arch.fault_dear;
809 kvmppc_book3s_queue_irqprio(vcpu,
810 BOOK3S_INTERRUPT_DATA_SEGMENT);
811 }
812 r = RESUME_GUEST;
813 break;
814 case BOOK3S_INTERRUPT_INST_SEGMENT:
815 if (kvmppc_mmu_map_segment(vcpu, vcpu->arch.pc) < 0) {
816 kvmppc_book3s_queue_irqprio(vcpu,
817 BOOK3S_INTERRUPT_INST_SEGMENT);
818 }
819 r = RESUME_GUEST;
820 break;
821 /* We're good on these - the host merely wanted to get our attention */
822 case BOOK3S_INTERRUPT_DECREMENTER:
823 vcpu->stat.dec_exits++;
824 r = RESUME_GUEST;
825 break;
826 case BOOK3S_INTERRUPT_EXTERNAL:
827 vcpu->stat.ext_intr_exits++;
828 r = RESUME_GUEST;
829 break;
830 case BOOK3S_INTERRUPT_PROGRAM:
831 {
832 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100833 ulong flags;
834
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100835program_interrupt:
Alexander Graff7adbba2010-01-15 14:49:13 +0100836 flags = vcpu->arch.shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000837
838 if (vcpu->arch.msr & MSR_PR) {
839#ifdef EXIT_DEBUG
840 printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", vcpu->arch.pc, vcpu->arch.last_inst);
841#endif
842 if ((vcpu->arch.last_inst & 0xff0007ff) !=
843 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100844 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000845 r = RESUME_GUEST;
846 break;
847 }
848 }
849
850 vcpu->stat.emulated_inst_exits++;
851 er = kvmppc_emulate_instruction(run, vcpu);
852 switch (er) {
853 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100854 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000855 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100856 case EMULATE_AGAIN:
857 r = RESUME_GUEST;
858 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000859 case EMULATE_FAIL:
860 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
861 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100862 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000863 r = RESUME_GUEST;
864 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +0100865 case EMULATE_DO_MMIO:
866 run->exit_reason = KVM_EXIT_MMIO;
867 r = RESUME_HOST_NV;
868 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000869 default:
870 BUG();
871 }
872 break;
873 }
874 case BOOK3S_INTERRUPT_SYSCALL:
875#ifdef EXIT_DEBUG
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100876 printk(KERN_INFO "Syscall Nr %d\n", (int)kvmppc_get_gpr(vcpu, 0));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000877#endif
878 vcpu->stat.syscall_exits++;
879 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
880 r = RESUME_GUEST;
881 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000882 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000883 case BOOK3S_INTERRUPT_ALTIVEC:
884 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100885 {
886 int ext_msr = 0;
887
888 switch (exit_nr) {
889 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
890 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
891 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
892 }
893
894 switch (kvmppc_check_ext(vcpu, exit_nr)) {
895 case EMULATE_DONE:
896 /* everything ok - let's enable the ext */
897 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
898 break;
899 case EMULATE_FAIL:
900 /* we need to emulate this instruction */
901 goto program_interrupt;
902 break;
903 default:
904 /* nothing to worry about - go again */
905 break;
906 }
Alexander Graf180a34d2010-01-15 14:49:11 +0100907 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100908 }
Alexander Graf180a34d2010-01-15 14:49:11 +0100909 case BOOK3S_INTERRUPT_MACHINE_CHECK:
910 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000911 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
912 r = RESUME_GUEST;
913 break;
914 default:
915 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +0100916 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
917 exit_nr, vcpu->arch.pc, vcpu->arch.shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000918 r = RESUME_HOST;
919 BUG();
920 break;
921 }
922
923
924 if (!(r & RESUME_HOST)) {
925 /* To avoid clobbering exit_reason, only check for signals if
926 * we aren't already exiting to userspace for some other
927 * reason. */
928 if (signal_pending(current)) {
929#ifdef EXIT_DEBUG
930 printk(KERN_EMERG "KVM: Going back to host\n");
931#endif
932 vcpu->stat.signal_exits++;
933 run->exit_reason = KVM_EXIT_INTR;
934 r = -EINTR;
935 } else {
936 /* In case an interrupt came in that was triggered
937 * from userspace (like DEC), we need to check what
938 * to inject now! */
939 kvmppc_core_deliver_interrupts(vcpu);
940 }
941 }
942
943#ifdef EXIT_DEBUG
944 printk(KERN_EMERG "KVM exit: vcpu=0x%p pc=0x%lx r=0x%x\n", vcpu, vcpu->arch.pc, r);
945#endif
946
947 return r;
948}
949
950int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
951{
952 return 0;
953}
954
955int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
956{
957 int i;
958
Alexander Grafa56cf342010-03-24 21:48:23 +0100959 vcpu_load(vcpu);
960
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000961 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100962 regs->cr = kvmppc_get_cr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000963 regs->ctr = vcpu->arch.ctr;
964 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100965 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000966 regs->msr = vcpu->arch.msr;
967 regs->srr0 = vcpu->arch.srr0;
968 regs->srr1 = vcpu->arch.srr1;
969 regs->pid = vcpu->arch.pid;
970 regs->sprg0 = vcpu->arch.sprg0;
971 regs->sprg1 = vcpu->arch.sprg1;
972 regs->sprg2 = vcpu->arch.sprg2;
973 regs->sprg3 = vcpu->arch.sprg3;
974 regs->sprg5 = vcpu->arch.sprg4;
975 regs->sprg6 = vcpu->arch.sprg5;
976 regs->sprg7 = vcpu->arch.sprg6;
977
978 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100979 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000980
Alexander Grafa56cf342010-03-24 21:48:23 +0100981 vcpu_put(vcpu);
982
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000983 return 0;
984}
985
986int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
987{
988 int i;
989
Alexander Grafa56cf342010-03-24 21:48:23 +0100990 vcpu_load(vcpu);
991
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000992 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100993 kvmppc_set_cr(vcpu, regs->cr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000994 vcpu->arch.ctr = regs->ctr;
995 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100996 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000997 kvmppc_set_msr(vcpu, regs->msr);
998 vcpu->arch.srr0 = regs->srr0;
999 vcpu->arch.srr1 = regs->srr1;
1000 vcpu->arch.sprg0 = regs->sprg0;
1001 vcpu->arch.sprg1 = regs->sprg1;
1002 vcpu->arch.sprg2 = regs->sprg2;
1003 vcpu->arch.sprg3 = regs->sprg3;
1004 vcpu->arch.sprg5 = regs->sprg4;
1005 vcpu->arch.sprg6 = regs->sprg5;
1006 vcpu->arch.sprg7 = regs->sprg6;
1007
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001008 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1009 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001010
Alexander Grafa56cf342010-03-24 21:48:23 +01001011 vcpu_put(vcpu);
1012
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001013 return 0;
1014}
1015
1016int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1017 struct kvm_sregs *sregs)
1018{
Alexander Grafe15a1132009-11-30 03:02:02 +00001019 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1020 int i;
1021
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001022 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +00001023
1024 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1025 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1026 for (i = 0; i < 64; i++) {
1027 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
1028 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1029 }
1030 } else {
1031 for (i = 0; i < 16; i++) {
1032 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1033 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1034 }
1035 for (i = 0; i < 8; i++) {
1036 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1037 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1038 }
1039 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001040 return 0;
1041}
1042
1043int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1044 struct kvm_sregs *sregs)
1045{
Alexander Grafe15a1132009-11-30 03:02:02 +00001046 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1047 int i;
1048
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001049 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001050
1051 vcpu3s->sdr1 = sregs->u.s.sdr1;
1052 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1053 for (i = 0; i < 64; i++) {
1054 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1055 sregs->u.s.ppc64.slb[i].slbe);
1056 }
1057 } else {
1058 for (i = 0; i < 16; i++) {
1059 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1060 }
1061 for (i = 0; i < 8; i++) {
1062 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1063 (u32)sregs->u.s.ppc32.ibat[i]);
1064 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1065 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1066 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1067 (u32)sregs->u.s.ppc32.dbat[i]);
1068 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1069 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1070 }
1071 }
1072
1073 /* Flush the MMU after messing with the segments */
1074 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001075 return 0;
1076}
1077
1078int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1079{
1080 return -ENOTSUPP;
1081}
1082
1083int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1084{
1085 return -ENOTSUPP;
1086}
1087
1088int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1089 struct kvm_translation *tr)
1090{
1091 return 0;
1092}
1093
1094/*
1095 * Get (and clear) the dirty memory log for a memory slot.
1096 */
1097int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1098 struct kvm_dirty_log *log)
1099{
1100 struct kvm_memory_slot *memslot;
1101 struct kvm_vcpu *vcpu;
1102 ulong ga, ga_end;
1103 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001104 int r;
1105 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001106
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001107 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001108
1109 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1110 if (r)
1111 goto out;
1112
1113 /* If nothing is dirty, don't bother messing with page tables. */
1114 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001115 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001116
1117 ga = memslot->base_gfn << PAGE_SHIFT;
1118 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1119
1120 kvm_for_each_vcpu(n, vcpu, kvm)
1121 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1122
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001123 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001124 memset(memslot->dirty_bitmap, 0, n);
1125 }
1126
1127 r = 0;
1128out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001129 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001130 return r;
1131}
1132
1133int kvmppc_core_check_processor_compat(void)
1134{
1135 return 0;
1136}
1137
1138struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1139{
1140 struct kvmppc_vcpu_book3s *vcpu_book3s;
1141 struct kvm_vcpu *vcpu;
1142 int err;
1143
Alexander Graf032c3402010-02-19 12:24:33 +01001144 vcpu_book3s = vmalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001145 if (!vcpu_book3s) {
1146 err = -ENOMEM;
1147 goto out;
1148 }
Alexander Graf7e821d32010-02-22 16:52:08 +01001149 memset(vcpu_book3s, 0, sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001150
1151 vcpu = &vcpu_book3s->vcpu;
1152 err = kvm_vcpu_init(vcpu, kvm, id);
1153 if (err)
1154 goto free_vcpu;
1155
1156 vcpu->arch.host_retip = kvm_return_point;
1157 vcpu->arch.host_msr = mfmsr();
1158 /* default to book3s_64 (970fx) */
1159 vcpu->arch.pvr = 0x3C0301;
1160 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1161 vcpu_book3s->slb_nr = 64;
1162
1163 /* remember where some real-mode handlers are */
1164 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1165 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1166 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf021ec9c2010-01-08 02:58:06 +01001167 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001168
1169 vcpu->arch.shadow_msr = MSR_USER64;
1170
1171 err = __init_new_context();
1172 if (err < 0)
1173 goto free_vcpu;
1174 vcpu_book3s->context_id = err;
1175
1176 vcpu_book3s->vsid_max = ((vcpu_book3s->context_id + 1) << USER_ESID_BITS) - 1;
1177 vcpu_book3s->vsid_first = vcpu_book3s->context_id << USER_ESID_BITS;
1178 vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
1179
1180 return vcpu;
1181
1182free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001183 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001184out:
1185 return ERR_PTR(err);
1186}
1187
1188void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1189{
1190 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1191
1192 __destroy_context(vcpu_book3s->context_id);
1193 kvm_vcpu_uninit(vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001194 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001195}
1196
1197extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1198int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1199{
1200 int ret;
Alexander Graf180a34d2010-01-15 14:49:11 +01001201 struct thread_struct ext_bkp;
1202 bool save_vec = current->thread.used_vr;
1203 bool save_vsx = current->thread.used_vsr;
1204 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001205
1206 /* No need to go into the guest when all we do is going out */
1207 if (signal_pending(current)) {
1208 kvm_run->exit_reason = KVM_EXIT_INTR;
1209 return -EINTR;
1210 }
1211
Alexander Graf180a34d2010-01-15 14:49:11 +01001212 /* Save FPU state in stack */
1213 if (current->thread.regs->msr & MSR_FP)
1214 giveup_fpu(current);
1215 memcpy(ext_bkp.fpr, current->thread.fpr, sizeof(current->thread.fpr));
1216 ext_bkp.fpscr = current->thread.fpscr;
1217 ext_bkp.fpexc_mode = current->thread.fpexc_mode;
1218
1219#ifdef CONFIG_ALTIVEC
1220 /* Save Altivec state in stack */
1221 if (save_vec) {
1222 if (current->thread.regs->msr & MSR_VEC)
1223 giveup_altivec(current);
1224 memcpy(ext_bkp.vr, current->thread.vr, sizeof(ext_bkp.vr));
1225 ext_bkp.vscr = current->thread.vscr;
1226 ext_bkp.vrsave = current->thread.vrsave;
1227 }
1228 ext_bkp.used_vr = current->thread.used_vr;
1229#endif
1230
1231#ifdef CONFIG_VSX
1232 /* Save VSX state in stack */
1233 if (save_vsx && (current->thread.regs->msr & MSR_VSX))
1234 __giveup_vsx(current);
1235 ext_bkp.used_vsr = current->thread.used_vsr;
1236#endif
1237
1238 /* Remember the MSR with disabled extensions */
1239 ext_msr = current->thread.regs->msr;
1240
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001241 /* XXX we get called with irq disabled - change that! */
1242 local_irq_enable();
1243
Alexander Grafd1bab742010-02-19 11:00:35 +01001244 /* Preload FPU if it's enabled */
1245 if (vcpu->arch.msr & MSR_FP)
1246 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1247
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001248 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1249
1250 local_irq_disable();
1251
Alexander Graf180a34d2010-01-15 14:49:11 +01001252 current->thread.regs->msr = ext_msr;
1253
1254 /* Make sure we save the guest FPU/Altivec/VSX state */
1255 kvmppc_giveup_ext(vcpu, MSR_FP);
1256 kvmppc_giveup_ext(vcpu, MSR_VEC);
1257 kvmppc_giveup_ext(vcpu, MSR_VSX);
1258
1259 /* Restore FPU state from stack */
1260 memcpy(current->thread.fpr, ext_bkp.fpr, sizeof(ext_bkp.fpr));
1261 current->thread.fpscr = ext_bkp.fpscr;
1262 current->thread.fpexc_mode = ext_bkp.fpexc_mode;
1263
1264#ifdef CONFIG_ALTIVEC
1265 /* Restore Altivec state from stack */
1266 if (save_vec && current->thread.used_vr) {
1267 memcpy(current->thread.vr, ext_bkp.vr, sizeof(ext_bkp.vr));
1268 current->thread.vscr = ext_bkp.vscr;
1269 current->thread.vrsave= ext_bkp.vrsave;
1270 }
1271 current->thread.used_vr = ext_bkp.used_vr;
1272#endif
1273
1274#ifdef CONFIG_VSX
1275 current->thread.used_vsr = ext_bkp.used_vsr;
1276#endif
1277
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001278 return ret;
1279}
1280
1281static int kvmppc_book3s_init(void)
1282{
1283 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), THIS_MODULE);
1284}
1285
1286static void kvmppc_book3s_exit(void)
1287{
1288 kvm_exit();
1289}
1290
1291module_init(kvmppc_book3s_init);
1292module_exit(kvmppc_book3s_exit);