blob: 6416f227d34567c8737c39d37c3cfa201c0e1ace [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
39static void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000040
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000041struct kvm_stats_debugfs_item debugfs_entries[] = {
42 { "exits", VCPU_STAT(sum_exits) },
43 { "mmio", VCPU_STAT(mmio_exits) },
44 { "sig", VCPU_STAT(signal_exits) },
45 { "sysc", VCPU_STAT(syscall_exits) },
46 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
47 { "dec", VCPU_STAT(dec_exits) },
48 { "ext_intr", VCPU_STAT(ext_intr_exits) },
49 { "queue_intr", VCPU_STAT(queue_intr) },
50 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
51 { "pf_storage", VCPU_STAT(pf_storage) },
52 { "sp_storage", VCPU_STAT(sp_storage) },
53 { "pf_instruc", VCPU_STAT(pf_instruc) },
54 { "sp_instruc", VCPU_STAT(sp_instruc) },
55 { "ld", VCPU_STAT(ld) },
56 { "ld_slow", VCPU_STAT(ld_slow) },
57 { "st", VCPU_STAT(st) },
58 { "st_slow", VCPU_STAT(st_slow) },
59 { NULL }
60};
61
62void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
63{
64}
65
66void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
67{
68}
69
70void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
71{
72 memcpy(get_paca()->kvm_slb, to_book3s(vcpu)->slb_shadow, sizeof(get_paca()->kvm_slb));
Alexander Graf7e57cba2010-01-08 02:58:03 +010073 memcpy(&get_paca()->shadow_vcpu, &to_book3s(vcpu)->shadow_vcpu,
74 sizeof(get_paca()->shadow_vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000075 get_paca()->kvm_slb_max = to_book3s(vcpu)->slb_shadow_max;
76}
77
78void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
79{
80 memcpy(to_book3s(vcpu)->slb_shadow, get_paca()->kvm_slb, sizeof(get_paca()->kvm_slb));
Alexander Graf7e57cba2010-01-08 02:58:03 +010081 memcpy(&to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
82 sizeof(get_paca()->shadow_vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000083 to_book3s(vcpu)->slb_shadow_max = get_paca()->kvm_slb_max;
Alexander Graf180a34d2010-01-15 14:49:11 +010084
85 kvmppc_giveup_ext(vcpu, MSR_FP);
86 kvmppc_giveup_ext(vcpu, MSR_VEC);
87 kvmppc_giveup_ext(vcpu, MSR_VSX);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000088}
89
Alexander Graf0bb1fb72009-12-21 20:21:25 +010090#if defined(EXIT_DEBUG)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000091static u32 kvmppc_get_dec(struct kvm_vcpu *vcpu)
92{
93 u64 jd = mftb() - vcpu->arch.dec_jiffies;
94 return vcpu->arch.dec - jd;
95}
96#endif
97
Alexander Grafa76f8492010-01-15 14:49:14 +010098static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
99{
100 vcpu->arch.shadow_msr = vcpu->arch.msr;
101 /* Guest MSR values */
102 vcpu->arch.shadow_msr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE |
103 MSR_BE | MSR_DE;
104 /* Process MSR values */
105 vcpu->arch.shadow_msr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR |
106 MSR_EE;
107 /* External providers the guest reserved */
108 vcpu->arch.shadow_msr |= (vcpu->arch.msr & vcpu->arch.guest_owned_ext);
109 /* 64-bit Process MSR values */
110#ifdef CONFIG_PPC_BOOK3S_64
111 vcpu->arch.shadow_msr |= MSR_ISF | MSR_HV;
112#endif
113}
114
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000115void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
116{
117 ulong old_msr = vcpu->arch.msr;
118
119#ifdef EXIT_DEBUG
120 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
121#endif
Alexander Grafa76f8492010-01-15 14:49:14 +0100122
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000123 msr &= to_book3s(vcpu)->msr_mask;
124 vcpu->arch.msr = msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100125 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000126
127 if (msr & (MSR_WE|MSR_POW)) {
128 if (!vcpu->arch.pending_exceptions) {
129 kvm_vcpu_block(vcpu);
130 vcpu->stat.halt_wakeup++;
131 }
132 }
133
134 if (((vcpu->arch.msr & (MSR_IR|MSR_DR)) != (old_msr & (MSR_IR|MSR_DR))) ||
135 (vcpu->arch.msr & MSR_PR) != (old_msr & MSR_PR)) {
136 kvmppc_mmu_flush_segments(vcpu);
137 kvmppc_mmu_map_segment(vcpu, vcpu->arch.pc);
138 }
139}
140
141void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
142{
143 vcpu->arch.srr0 = vcpu->arch.pc;
144 vcpu->arch.srr1 = vcpu->arch.msr | flags;
145 vcpu->arch.pc = to_book3s(vcpu)->hior + vec;
146 vcpu->arch.mmu.reset_msr(vcpu);
147}
148
Alexander Graf583617b2009-12-21 20:21:23 +0100149static int kvmppc_book3s_vec2irqprio(unsigned int vec)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000150{
151 unsigned int prio;
152
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000153 switch (vec) {
154 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
155 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
156 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
157 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
158 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
159 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
160 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
161 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
162 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
163 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
164 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
165 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
166 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
167 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
168 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
169 default: prio = BOOK3S_IRQPRIO_MAX; break;
170 }
171
Alexander Graf583617b2009-12-21 20:21:23 +0100172 return prio;
173}
174
Alexander Graf7706664d2009-12-21 20:21:24 +0100175static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
176 unsigned int vec)
177{
178 clear_bit(kvmppc_book3s_vec2irqprio(vec),
179 &vcpu->arch.pending_exceptions);
180}
181
Alexander Graf583617b2009-12-21 20:21:23 +0100182void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
183{
184 vcpu->stat.queue_intr++;
185
186 set_bit(kvmppc_book3s_vec2irqprio(vec),
187 &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000188#ifdef EXIT_DEBUG
189 printk(KERN_INFO "Queueing interrupt %x\n", vec);
190#endif
191}
192
193
Alexander Graf25a8a022010-01-08 02:58:07 +0100194void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000195{
Alexander Graf25a8a022010-01-08 02:58:07 +0100196 to_book3s(vcpu)->prog_flags = flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000197 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM);
198}
199
200void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
201{
202 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
203}
204
205int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
206{
207 return test_bit(BOOK3S_INTERRUPT_DECREMENTER >> 7, &vcpu->arch.pending_exceptions);
208}
209
Alexander Graf7706664d2009-12-21 20:21:24 +0100210void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
211{
212 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
213}
214
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000215void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
216 struct kvm_interrupt *irq)
217{
218 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
219}
220
221int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
222{
223 int deliver = 1;
224 int vec = 0;
Alexander Graf25a8a022010-01-08 02:58:07 +0100225 ulong flags = 0ULL;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000226
227 switch (priority) {
228 case BOOK3S_IRQPRIO_DECREMENTER:
229 deliver = vcpu->arch.msr & MSR_EE;
230 vec = BOOK3S_INTERRUPT_DECREMENTER;
231 break;
232 case BOOK3S_IRQPRIO_EXTERNAL:
233 deliver = vcpu->arch.msr & MSR_EE;
234 vec = BOOK3S_INTERRUPT_EXTERNAL;
235 break;
236 case BOOK3S_IRQPRIO_SYSTEM_RESET:
237 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
238 break;
239 case BOOK3S_IRQPRIO_MACHINE_CHECK:
240 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
241 break;
242 case BOOK3S_IRQPRIO_DATA_STORAGE:
243 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
244 break;
245 case BOOK3S_IRQPRIO_INST_STORAGE:
246 vec = BOOK3S_INTERRUPT_INST_STORAGE;
247 break;
248 case BOOK3S_IRQPRIO_DATA_SEGMENT:
249 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
250 break;
251 case BOOK3S_IRQPRIO_INST_SEGMENT:
252 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
253 break;
254 case BOOK3S_IRQPRIO_ALIGNMENT:
255 vec = BOOK3S_INTERRUPT_ALIGNMENT;
256 break;
257 case BOOK3S_IRQPRIO_PROGRAM:
258 vec = BOOK3S_INTERRUPT_PROGRAM;
Alexander Graf25a8a022010-01-08 02:58:07 +0100259 flags = to_book3s(vcpu)->prog_flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000260 break;
261 case BOOK3S_IRQPRIO_VSX:
262 vec = BOOK3S_INTERRUPT_VSX;
263 break;
264 case BOOK3S_IRQPRIO_ALTIVEC:
265 vec = BOOK3S_INTERRUPT_ALTIVEC;
266 break;
267 case BOOK3S_IRQPRIO_FP_UNAVAIL:
268 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
269 break;
270 case BOOK3S_IRQPRIO_SYSCALL:
271 vec = BOOK3S_INTERRUPT_SYSCALL;
272 break;
273 case BOOK3S_IRQPRIO_DEBUG:
274 vec = BOOK3S_INTERRUPT_TRACE;
275 break;
276 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
277 vec = BOOK3S_INTERRUPT_PERFMON;
278 break;
279 default:
280 deliver = 0;
281 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
282 break;
283 }
284
285#if 0
286 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
287#endif
288
289 if (deliver)
Alexander Graf25a8a022010-01-08 02:58:07 +0100290 kvmppc_inject_interrupt(vcpu, vec, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000291
292 return deliver;
293}
294
295void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
296{
297 unsigned long *pending = &vcpu->arch.pending_exceptions;
298 unsigned int priority;
299
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000300#ifdef EXIT_DEBUG
301 if (vcpu->arch.pending_exceptions)
302 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
303#endif
304 priority = __ffs(*pending);
305 while (priority <= (sizeof(unsigned int) * 8)) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100306 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
307 (priority != BOOK3S_IRQPRIO_DECREMENTER)) {
308 /* DEC interrupts get cleared by mtdec */
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000309 clear_bit(priority, &vcpu->arch.pending_exceptions);
310 break;
311 }
312
313 priority = find_next_bit(pending,
314 BITS_PER_BYTE * sizeof(*pending),
315 priority + 1);
316 }
317}
318
319void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
320{
Alexander Grafe15a1132009-11-30 03:02:02 +0000321 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000322 vcpu->arch.pvr = pvr;
323 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
324 kvmppc_mmu_book3s_64_init(vcpu);
325 to_book3s(vcpu)->hior = 0xfff00000;
326 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
327 } else {
328 kvmppc_mmu_book3s_32_init(vcpu);
329 to_book3s(vcpu)->hior = 0;
330 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
331 }
332
333 /* If we are in hypervisor level on 970, we can tell the CPU to
334 * treat DCBZ as 32 bytes store */
335 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
336 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
337 !strcmp(cur_cpu_spec->platform, "ppc970"))
338 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
339
340}
341
342/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
343 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
344 * emulate 32 bytes dcbz length.
345 *
346 * The Book3s_64 inventors also realized this case and implemented a special bit
347 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
348 *
349 * My approach here is to patch the dcbz instruction on executing pages.
350 */
351static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
352{
353 bool touched = false;
354 hva_t hpage;
355 u32 *page;
356 int i;
357
358 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
359 if (kvm_is_error_hva(hpage))
360 return;
361
362 hpage |= pte->raddr & ~PAGE_MASK;
363 hpage &= ~0xFFFULL;
364
365 page = vmalloc(HW_PAGE_SIZE);
366
367 if (copy_from_user(page, (void __user *)hpage, HW_PAGE_SIZE))
368 goto out;
369
370 for (i=0; i < HW_PAGE_SIZE / 4; i++)
371 if ((page[i] & 0xff0007ff) == INS_DCBZ) {
372 page[i] &= 0xfffffff7; // reserved instruction, so we trap
373 touched = true;
374 }
375
376 if (touched)
377 copy_to_user((void __user *)hpage, page, HW_PAGE_SIZE);
378
379out:
380 vfree(page);
381}
382
383static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
384 struct kvmppc_pte *pte)
385{
386 int relocated = (vcpu->arch.msr & (data ? MSR_DR : MSR_IR));
387 int r;
388
389 if (relocated) {
390 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
391 } else {
392 pte->eaddr = eaddr;
393 pte->raddr = eaddr & 0xffffffff;
394 pte->vpage = eaddr >> 12;
395 switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
396 case 0:
397 pte->vpage |= VSID_REAL;
398 case MSR_DR:
399 pte->vpage |= VSID_REAL_DR;
400 case MSR_IR:
401 pte->vpage |= VSID_REAL_IR;
402 }
403 pte->may_read = true;
404 pte->may_write = true;
405 pte->may_execute = true;
406 r = 0;
407 }
408
409 return r;
410}
411
412static hva_t kvmppc_bad_hva(void)
413{
414 return PAGE_OFFSET;
415}
416
417static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
418 bool read)
419{
420 hva_t hpage;
421
422 if (read && !pte->may_read)
423 goto err;
424
425 if (!read && !pte->may_write)
426 goto err;
427
428 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
429 if (kvm_is_error_hva(hpage))
430 goto err;
431
432 return hpage | (pte->raddr & ~PAGE_MASK);
433err:
434 return kvmppc_bad_hva();
435}
436
437int kvmppc_st(struct kvm_vcpu *vcpu, ulong eaddr, int size, void *ptr)
438{
439 struct kvmppc_pte pte;
440 hva_t hva = eaddr;
441
442 vcpu->stat.st++;
443
444 if (kvmppc_xlate(vcpu, eaddr, false, &pte))
445 goto err;
446
447 hva = kvmppc_pte_to_hva(vcpu, &pte, false);
448 if (kvm_is_error_hva(hva))
449 goto err;
450
451 if (copy_to_user((void __user *)hva, ptr, size)) {
452 printk(KERN_INFO "kvmppc_st at 0x%lx failed\n", hva);
453 goto err;
454 }
455
456 return 0;
457
458err:
459 return -ENOENT;
460}
461
462int kvmppc_ld(struct kvm_vcpu *vcpu, ulong eaddr, int size, void *ptr,
463 bool data)
464{
465 struct kvmppc_pte pte;
466 hva_t hva = eaddr;
467
468 vcpu->stat.ld++;
469
470 if (kvmppc_xlate(vcpu, eaddr, data, &pte))
471 goto err;
472
473 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
474 if (kvm_is_error_hva(hva))
475 goto err;
476
477 if (copy_from_user(ptr, (void __user *)hva, size)) {
478 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
479 goto err;
480 }
481
482 return 0;
483
484err:
485 return -ENOENT;
486}
487
488static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
489{
490 return kvm_is_visible_gfn(vcpu->kvm, gfn);
491}
492
493int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
494 ulong eaddr, int vec)
495{
496 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
497 int r = RESUME_GUEST;
498 int relocated;
499 int page_found = 0;
500 struct kvmppc_pte pte;
501 bool is_mmio = false;
502
503 if ( vec == BOOK3S_INTERRUPT_DATA_STORAGE ) {
504 relocated = (vcpu->arch.msr & MSR_DR);
505 } else {
506 relocated = (vcpu->arch.msr & MSR_IR);
507 }
508
509 /* Resolve real address if translation turned on */
510 if (relocated) {
511 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
512 } else {
513 pte.may_execute = true;
514 pte.may_read = true;
515 pte.may_write = true;
516 pte.raddr = eaddr & 0xffffffff;
517 pte.eaddr = eaddr;
518 pte.vpage = eaddr >> 12;
519 switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
520 case 0:
521 pte.vpage |= VSID_REAL;
522 case MSR_DR:
523 pte.vpage |= VSID_REAL_DR;
524 case MSR_IR:
525 pte.vpage |= VSID_REAL_IR;
526 }
527 }
528
529 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
530 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
531 /*
532 * If we do the dcbz hack, we have to NX on every execution,
533 * so we can patch the executing code. This renders our guest
534 * NX-less.
535 */
536 pte.may_execute = !data;
537 }
538
539 if (page_found == -ENOENT) {
540 /* Page not found in guest PTE entries */
541 vcpu->arch.dear = vcpu->arch.fault_dear;
542 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr;
Alexander Graff7adbba2010-01-15 14:49:13 +0100543 vcpu->arch.msr |= (vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000544 kvmppc_book3s_queue_irqprio(vcpu, vec);
545 } else if (page_found == -EPERM) {
546 /* Storage protection */
547 vcpu->arch.dear = vcpu->arch.fault_dear;
548 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr & ~DSISR_NOHPTE;
549 to_book3s(vcpu)->dsisr |= DSISR_PROTFAULT;
Alexander Graff7adbba2010-01-15 14:49:13 +0100550 vcpu->arch.msr |= (vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000551 kvmppc_book3s_queue_irqprio(vcpu, vec);
552 } else if (page_found == -EINVAL) {
553 /* Page not found in guest SLB */
554 vcpu->arch.dear = vcpu->arch.fault_dear;
555 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
556 } else if (!is_mmio &&
557 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
558 /* The guest's PTE is not mapped yet. Map on the host */
559 kvmppc_mmu_map_page(vcpu, &pte);
560 if (data)
561 vcpu->stat.sp_storage++;
562 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
563 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
564 kvmppc_patch_dcbz(vcpu, &pte);
565 } else {
566 /* MMIO */
567 vcpu->stat.mmio_exits++;
568 vcpu->arch.paddr_accessed = pte.raddr;
569 r = kvmppc_emulate_mmio(run, vcpu);
570 if ( r == RESUME_HOST_NV )
571 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000572 }
573
574 return r;
575}
576
Alexander Graf180a34d2010-01-15 14:49:11 +0100577static inline int get_fpr_index(int i)
578{
579#ifdef CONFIG_VSX
580 i *= 2;
581#endif
582 return i;
583}
584
585/* Give up external provider (FPU, Altivec, VSX) */
586static void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
587{
588 struct thread_struct *t = &current->thread;
589 u64 *vcpu_fpr = vcpu->arch.fpr;
590 u64 *vcpu_vsx = vcpu->arch.vsr;
591 u64 *thread_fpr = (u64*)t->fpr;
592 int i;
593
594 if (!(vcpu->arch.guest_owned_ext & msr))
595 return;
596
597#ifdef DEBUG_EXT
598 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
599#endif
600
601 switch (msr) {
602 case MSR_FP:
603 giveup_fpu(current);
604 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
605 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
606
607 vcpu->arch.fpscr = t->fpscr.val;
608 break;
609 case MSR_VEC:
610#ifdef CONFIG_ALTIVEC
611 giveup_altivec(current);
612 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
613 vcpu->arch.vscr = t->vscr;
614#endif
615 break;
616 case MSR_VSX:
617#ifdef CONFIG_VSX
618 __giveup_vsx(current);
619 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
620 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
621#endif
622 break;
623 default:
624 BUG();
625 }
626
627 vcpu->arch.guest_owned_ext &= ~msr;
628 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100629 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100630}
631
632/* Handle external providers (FPU, Altivec, VSX) */
633static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
634 ulong msr)
635{
636 struct thread_struct *t = &current->thread;
637 u64 *vcpu_fpr = vcpu->arch.fpr;
638 u64 *vcpu_vsx = vcpu->arch.vsr;
639 u64 *thread_fpr = (u64*)t->fpr;
640 int i;
641
642 if (!(vcpu->arch.msr & msr)) {
643 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
644 return RESUME_GUEST;
645 }
646
647#ifdef DEBUG_EXT
648 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
649#endif
650
651 current->thread.regs->msr |= msr;
652
653 switch (msr) {
654 case MSR_FP:
655 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
656 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
657
658 t->fpscr.val = vcpu->arch.fpscr;
659 t->fpexc_mode = 0;
660 kvmppc_load_up_fpu();
661 break;
662 case MSR_VEC:
663#ifdef CONFIG_ALTIVEC
664 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
665 t->vscr = vcpu->arch.vscr;
666 t->vrsave = -1;
667 kvmppc_load_up_altivec();
668#endif
669 break;
670 case MSR_VSX:
671#ifdef CONFIG_VSX
672 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
673 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
674 kvmppc_load_up_vsx();
675#endif
676 break;
677 default:
678 BUG();
679 }
680
681 vcpu->arch.guest_owned_ext |= msr;
682
Alexander Grafa76f8492010-01-15 14:49:14 +0100683 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100684
685 return RESUME_GUEST;
686}
687
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000688int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
689 unsigned int exit_nr)
690{
691 int r = RESUME_HOST;
692
693 vcpu->stat.sum_exits++;
694
695 run->exit_reason = KVM_EXIT_UNKNOWN;
696 run->ready_for_interrupt_injection = 1;
697#ifdef EXIT_DEBUG
698 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | dec=0x%x | msr=0x%lx\n",
699 exit_nr, vcpu->arch.pc, vcpu->arch.fault_dear,
700 kvmppc_get_dec(vcpu), vcpu->arch.msr);
701#elif defined (EXIT_DEBUG_SIMPLE)
702 if ((exit_nr != 0x900) && (exit_nr != 0x500))
703 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | msr=0x%lx\n",
704 exit_nr, vcpu->arch.pc, vcpu->arch.fault_dear,
705 vcpu->arch.msr);
706#endif
707 kvm_resched(vcpu);
708 switch (exit_nr) {
709 case BOOK3S_INTERRUPT_INST_STORAGE:
710 vcpu->stat.pf_instruc++;
711 /* only care about PTEG not found errors, but leave NX alone */
Alexander Graff7adbba2010-01-15 14:49:13 +0100712 if (vcpu->arch.shadow_srr1 & 0x40000000) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000713 r = kvmppc_handle_pagefault(run, vcpu, vcpu->arch.pc, exit_nr);
714 vcpu->stat.sp_instruc++;
715 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
716 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
717 /*
718 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
719 * so we can't use the NX bit inside the guest. Let's cross our fingers,
720 * that no guest that needs the dcbz hack does NX.
721 */
722 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.pc, ~0xFFFULL);
723 } else {
Alexander Graff7adbba2010-01-15 14:49:13 +0100724 vcpu->arch.msr |= vcpu->arch.shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000725 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
726 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.pc, ~0xFFFULL);
727 r = RESUME_GUEST;
728 }
729 break;
730 case BOOK3S_INTERRUPT_DATA_STORAGE:
731 vcpu->stat.pf_storage++;
732 /* The only case we need to handle is missing shadow PTEs */
733 if (vcpu->arch.fault_dsisr & DSISR_NOHPTE) {
734 r = kvmppc_handle_pagefault(run, vcpu, vcpu->arch.fault_dear, exit_nr);
735 } else {
736 vcpu->arch.dear = vcpu->arch.fault_dear;
737 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr;
738 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
739 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.dear, ~0xFFFULL);
740 r = RESUME_GUEST;
741 }
742 break;
743 case BOOK3S_INTERRUPT_DATA_SEGMENT:
744 if (kvmppc_mmu_map_segment(vcpu, vcpu->arch.fault_dear) < 0) {
745 vcpu->arch.dear = vcpu->arch.fault_dear;
746 kvmppc_book3s_queue_irqprio(vcpu,
747 BOOK3S_INTERRUPT_DATA_SEGMENT);
748 }
749 r = RESUME_GUEST;
750 break;
751 case BOOK3S_INTERRUPT_INST_SEGMENT:
752 if (kvmppc_mmu_map_segment(vcpu, vcpu->arch.pc) < 0) {
753 kvmppc_book3s_queue_irqprio(vcpu,
754 BOOK3S_INTERRUPT_INST_SEGMENT);
755 }
756 r = RESUME_GUEST;
757 break;
758 /* We're good on these - the host merely wanted to get our attention */
759 case BOOK3S_INTERRUPT_DECREMENTER:
760 vcpu->stat.dec_exits++;
761 r = RESUME_GUEST;
762 break;
763 case BOOK3S_INTERRUPT_EXTERNAL:
764 vcpu->stat.ext_intr_exits++;
765 r = RESUME_GUEST;
766 break;
767 case BOOK3S_INTERRUPT_PROGRAM:
768 {
769 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100770 ulong flags;
771
Alexander Graff7adbba2010-01-15 14:49:13 +0100772 flags = vcpu->arch.shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000773
774 if (vcpu->arch.msr & MSR_PR) {
775#ifdef EXIT_DEBUG
776 printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", vcpu->arch.pc, vcpu->arch.last_inst);
777#endif
778 if ((vcpu->arch.last_inst & 0xff0007ff) !=
779 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100780 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000781 r = RESUME_GUEST;
782 break;
783 }
784 }
785
786 vcpu->stat.emulated_inst_exits++;
787 er = kvmppc_emulate_instruction(run, vcpu);
788 switch (er) {
789 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100790 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000791 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100792 case EMULATE_AGAIN:
793 r = RESUME_GUEST;
794 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000795 case EMULATE_FAIL:
796 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
797 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100798 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000799 r = RESUME_GUEST;
800 break;
801 default:
802 BUG();
803 }
804 break;
805 }
806 case BOOK3S_INTERRUPT_SYSCALL:
807#ifdef EXIT_DEBUG
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100808 printk(KERN_INFO "Syscall Nr %d\n", (int)kvmppc_get_gpr(vcpu, 0));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000809#endif
810 vcpu->stat.syscall_exits++;
811 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
812 r = RESUME_GUEST;
813 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000814 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf180a34d2010-01-15 14:49:11 +0100815 r = kvmppc_handle_ext(vcpu, exit_nr, MSR_FP);
816 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000817 case BOOK3S_INTERRUPT_ALTIVEC:
Alexander Graf180a34d2010-01-15 14:49:11 +0100818 r = kvmppc_handle_ext(vcpu, exit_nr, MSR_VEC);
819 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000820 case BOOK3S_INTERRUPT_VSX:
Alexander Graf180a34d2010-01-15 14:49:11 +0100821 r = kvmppc_handle_ext(vcpu, exit_nr, MSR_VSX);
822 break;
823 case BOOK3S_INTERRUPT_MACHINE_CHECK:
824 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000825 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
826 r = RESUME_GUEST;
827 break;
828 default:
829 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +0100830 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
831 exit_nr, vcpu->arch.pc, vcpu->arch.shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000832 r = RESUME_HOST;
833 BUG();
834 break;
835 }
836
837
838 if (!(r & RESUME_HOST)) {
839 /* To avoid clobbering exit_reason, only check for signals if
840 * we aren't already exiting to userspace for some other
841 * reason. */
842 if (signal_pending(current)) {
843#ifdef EXIT_DEBUG
844 printk(KERN_EMERG "KVM: Going back to host\n");
845#endif
846 vcpu->stat.signal_exits++;
847 run->exit_reason = KVM_EXIT_INTR;
848 r = -EINTR;
849 } else {
850 /* In case an interrupt came in that was triggered
851 * from userspace (like DEC), we need to check what
852 * to inject now! */
853 kvmppc_core_deliver_interrupts(vcpu);
854 }
855 }
856
857#ifdef EXIT_DEBUG
858 printk(KERN_EMERG "KVM exit: vcpu=0x%p pc=0x%lx r=0x%x\n", vcpu, vcpu->arch.pc, r);
859#endif
860
861 return r;
862}
863
864int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
865{
866 return 0;
867}
868
869int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
870{
871 int i;
872
873 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100874 regs->cr = kvmppc_get_cr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000875 regs->ctr = vcpu->arch.ctr;
876 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100877 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000878 regs->msr = vcpu->arch.msr;
879 regs->srr0 = vcpu->arch.srr0;
880 regs->srr1 = vcpu->arch.srr1;
881 regs->pid = vcpu->arch.pid;
882 regs->sprg0 = vcpu->arch.sprg0;
883 regs->sprg1 = vcpu->arch.sprg1;
884 regs->sprg2 = vcpu->arch.sprg2;
885 regs->sprg3 = vcpu->arch.sprg3;
886 regs->sprg5 = vcpu->arch.sprg4;
887 regs->sprg6 = vcpu->arch.sprg5;
888 regs->sprg7 = vcpu->arch.sprg6;
889
890 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100891 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000892
893 return 0;
894}
895
896int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
897{
898 int i;
899
900 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100901 kvmppc_set_cr(vcpu, regs->cr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000902 vcpu->arch.ctr = regs->ctr;
903 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100904 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000905 kvmppc_set_msr(vcpu, regs->msr);
906 vcpu->arch.srr0 = regs->srr0;
907 vcpu->arch.srr1 = regs->srr1;
908 vcpu->arch.sprg0 = regs->sprg0;
909 vcpu->arch.sprg1 = regs->sprg1;
910 vcpu->arch.sprg2 = regs->sprg2;
911 vcpu->arch.sprg3 = regs->sprg3;
912 vcpu->arch.sprg5 = regs->sprg4;
913 vcpu->arch.sprg6 = regs->sprg5;
914 vcpu->arch.sprg7 = regs->sprg6;
915
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100916 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
917 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000918
919 return 0;
920}
921
922int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
923 struct kvm_sregs *sregs)
924{
Alexander Grafe15a1132009-11-30 03:02:02 +0000925 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
926 int i;
927
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000928 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +0000929
930 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
931 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
932 for (i = 0; i < 64; i++) {
933 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
934 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
935 }
936 } else {
937 for (i = 0; i < 16; i++) {
938 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
939 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
940 }
941 for (i = 0; i < 8; i++) {
942 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
943 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
944 }
945 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000946 return 0;
947}
948
949int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
950 struct kvm_sregs *sregs)
951{
Alexander Grafe15a1132009-11-30 03:02:02 +0000952 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
953 int i;
954
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000955 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +0000956
957 vcpu3s->sdr1 = sregs->u.s.sdr1;
958 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
959 for (i = 0; i < 64; i++) {
960 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
961 sregs->u.s.ppc64.slb[i].slbe);
962 }
963 } else {
964 for (i = 0; i < 16; i++) {
965 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
966 }
967 for (i = 0; i < 8; i++) {
968 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
969 (u32)sregs->u.s.ppc32.ibat[i]);
970 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
971 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
972 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
973 (u32)sregs->u.s.ppc32.dbat[i]);
974 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
975 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
976 }
977 }
978
979 /* Flush the MMU after messing with the segments */
980 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000981 return 0;
982}
983
984int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
985{
986 return -ENOTSUPP;
987}
988
989int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
990{
991 return -ENOTSUPP;
992}
993
994int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
995 struct kvm_translation *tr)
996{
997 return 0;
998}
999
1000/*
1001 * Get (and clear) the dirty memory log for a memory slot.
1002 */
1003int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1004 struct kvm_dirty_log *log)
1005{
1006 struct kvm_memory_slot *memslot;
1007 struct kvm_vcpu *vcpu;
1008 ulong ga, ga_end;
1009 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001010 int r;
1011 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001012
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001013 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001014
1015 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1016 if (r)
1017 goto out;
1018
1019 /* If nothing is dirty, don't bother messing with page tables. */
1020 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001021 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001022
1023 ga = memslot->base_gfn << PAGE_SHIFT;
1024 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1025
1026 kvm_for_each_vcpu(n, vcpu, kvm)
1027 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1028
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001029 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001030 memset(memslot->dirty_bitmap, 0, n);
1031 }
1032
1033 r = 0;
1034out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001035 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001036 return r;
1037}
1038
1039int kvmppc_core_check_processor_compat(void)
1040{
1041 return 0;
1042}
1043
1044struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1045{
1046 struct kvmppc_vcpu_book3s *vcpu_book3s;
1047 struct kvm_vcpu *vcpu;
1048 int err;
1049
1050 vcpu_book3s = (struct kvmppc_vcpu_book3s *)__get_free_pages( GFP_KERNEL | __GFP_ZERO,
1051 get_order(sizeof(struct kvmppc_vcpu_book3s)));
1052 if (!vcpu_book3s) {
1053 err = -ENOMEM;
1054 goto out;
1055 }
1056
1057 vcpu = &vcpu_book3s->vcpu;
1058 err = kvm_vcpu_init(vcpu, kvm, id);
1059 if (err)
1060 goto free_vcpu;
1061
1062 vcpu->arch.host_retip = kvm_return_point;
1063 vcpu->arch.host_msr = mfmsr();
1064 /* default to book3s_64 (970fx) */
1065 vcpu->arch.pvr = 0x3C0301;
1066 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1067 vcpu_book3s->slb_nr = 64;
1068
1069 /* remember where some real-mode handlers are */
1070 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1071 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1072 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf021ec9c2010-01-08 02:58:06 +01001073 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001074
1075 vcpu->arch.shadow_msr = MSR_USER64;
1076
1077 err = __init_new_context();
1078 if (err < 0)
1079 goto free_vcpu;
1080 vcpu_book3s->context_id = err;
1081
1082 vcpu_book3s->vsid_max = ((vcpu_book3s->context_id + 1) << USER_ESID_BITS) - 1;
1083 vcpu_book3s->vsid_first = vcpu_book3s->context_id << USER_ESID_BITS;
1084 vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
1085
1086 return vcpu;
1087
1088free_vcpu:
1089 free_pages((long)vcpu_book3s, get_order(sizeof(struct kvmppc_vcpu_book3s)));
1090out:
1091 return ERR_PTR(err);
1092}
1093
1094void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1095{
1096 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1097
1098 __destroy_context(vcpu_book3s->context_id);
1099 kvm_vcpu_uninit(vcpu);
1100 free_pages((long)vcpu_book3s, get_order(sizeof(struct kvmppc_vcpu_book3s)));
1101}
1102
1103extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1104int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1105{
1106 int ret;
Alexander Graf180a34d2010-01-15 14:49:11 +01001107 struct thread_struct ext_bkp;
1108 bool save_vec = current->thread.used_vr;
1109 bool save_vsx = current->thread.used_vsr;
1110 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001111
1112 /* No need to go into the guest when all we do is going out */
1113 if (signal_pending(current)) {
1114 kvm_run->exit_reason = KVM_EXIT_INTR;
1115 return -EINTR;
1116 }
1117
Alexander Graf180a34d2010-01-15 14:49:11 +01001118 /* Save FPU state in stack */
1119 if (current->thread.regs->msr & MSR_FP)
1120 giveup_fpu(current);
1121 memcpy(ext_bkp.fpr, current->thread.fpr, sizeof(current->thread.fpr));
1122 ext_bkp.fpscr = current->thread.fpscr;
1123 ext_bkp.fpexc_mode = current->thread.fpexc_mode;
1124
1125#ifdef CONFIG_ALTIVEC
1126 /* Save Altivec state in stack */
1127 if (save_vec) {
1128 if (current->thread.regs->msr & MSR_VEC)
1129 giveup_altivec(current);
1130 memcpy(ext_bkp.vr, current->thread.vr, sizeof(ext_bkp.vr));
1131 ext_bkp.vscr = current->thread.vscr;
1132 ext_bkp.vrsave = current->thread.vrsave;
1133 }
1134 ext_bkp.used_vr = current->thread.used_vr;
1135#endif
1136
1137#ifdef CONFIG_VSX
1138 /* Save VSX state in stack */
1139 if (save_vsx && (current->thread.regs->msr & MSR_VSX))
1140 __giveup_vsx(current);
1141 ext_bkp.used_vsr = current->thread.used_vsr;
1142#endif
1143
1144 /* Remember the MSR with disabled extensions */
1145 ext_msr = current->thread.regs->msr;
1146
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001147 /* XXX we get called with irq disabled - change that! */
1148 local_irq_enable();
1149
1150 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1151
1152 local_irq_disable();
1153
Alexander Graf180a34d2010-01-15 14:49:11 +01001154 current->thread.regs->msr = ext_msr;
1155
1156 /* Make sure we save the guest FPU/Altivec/VSX state */
1157 kvmppc_giveup_ext(vcpu, MSR_FP);
1158 kvmppc_giveup_ext(vcpu, MSR_VEC);
1159 kvmppc_giveup_ext(vcpu, MSR_VSX);
1160
1161 /* Restore FPU state from stack */
1162 memcpy(current->thread.fpr, ext_bkp.fpr, sizeof(ext_bkp.fpr));
1163 current->thread.fpscr = ext_bkp.fpscr;
1164 current->thread.fpexc_mode = ext_bkp.fpexc_mode;
1165
1166#ifdef CONFIG_ALTIVEC
1167 /* Restore Altivec state from stack */
1168 if (save_vec && current->thread.used_vr) {
1169 memcpy(current->thread.vr, ext_bkp.vr, sizeof(ext_bkp.vr));
1170 current->thread.vscr = ext_bkp.vscr;
1171 current->thread.vrsave= ext_bkp.vrsave;
1172 }
1173 current->thread.used_vr = ext_bkp.used_vr;
1174#endif
1175
1176#ifdef CONFIG_VSX
1177 current->thread.used_vsr = ext_bkp.used_vsr;
1178#endif
1179
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001180 return ret;
1181}
1182
1183static int kvmppc_book3s_init(void)
1184{
1185 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), THIS_MODULE);
1186}
1187
1188static void kvmppc_book3s_exit(void)
1189{
1190 kvm_exit();
1191}
1192
1193module_init(kvmppc_book3s_init);
1194module_exit(kvmppc_book3s_exit);