blob: 55c38e5982801058812ef76312aef9d2d1e49427 [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 Grafc8c0b6f2010-02-19 11:00:34 +010040static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
41 ulong msr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000042
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000043struct kvm_stats_debugfs_item debugfs_entries[] = {
44 { "exits", VCPU_STAT(sum_exits) },
45 { "mmio", VCPU_STAT(mmio_exits) },
46 { "sig", VCPU_STAT(signal_exits) },
47 { "sysc", VCPU_STAT(syscall_exits) },
48 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
49 { "dec", VCPU_STAT(dec_exits) },
50 { "ext_intr", VCPU_STAT(ext_intr_exits) },
51 { "queue_intr", VCPU_STAT(queue_intr) },
52 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
53 { "pf_storage", VCPU_STAT(pf_storage) },
54 { "sp_storage", VCPU_STAT(sp_storage) },
55 { "pf_instruc", VCPU_STAT(pf_instruc) },
56 { "sp_instruc", VCPU_STAT(sp_instruc) },
57 { "ld", VCPU_STAT(ld) },
58 { "ld_slow", VCPU_STAT(ld_slow) },
59 { "st", VCPU_STAT(st) },
60 { "st_slow", VCPU_STAT(st_slow) },
61 { NULL }
62};
63
64void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
65{
66}
67
68void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
69{
70}
71
72void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
73{
74 memcpy(get_paca()->kvm_slb, to_book3s(vcpu)->slb_shadow, sizeof(get_paca()->kvm_slb));
Alexander Graf7e57cba2010-01-08 02:58:03 +010075 memcpy(&get_paca()->shadow_vcpu, &to_book3s(vcpu)->shadow_vcpu,
76 sizeof(get_paca()->shadow_vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000077 get_paca()->kvm_slb_max = to_book3s(vcpu)->slb_shadow_max;
78}
79
80void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
81{
82 memcpy(to_book3s(vcpu)->slb_shadow, get_paca()->kvm_slb, sizeof(get_paca()->kvm_slb));
Alexander Graf7e57cba2010-01-08 02:58:03 +010083 memcpy(&to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
84 sizeof(get_paca()->shadow_vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000085 to_book3s(vcpu)->slb_shadow_max = get_paca()->kvm_slb_max;
Alexander Graf180a34d2010-01-15 14:49:11 +010086
87 kvmppc_giveup_ext(vcpu, MSR_FP);
88 kvmppc_giveup_ext(vcpu, MSR_VEC);
89 kvmppc_giveup_ext(vcpu, MSR_VSX);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000090}
91
Alexander Graf0bb1fb72009-12-21 20:21:25 +010092#if defined(EXIT_DEBUG)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000093static u32 kvmppc_get_dec(struct kvm_vcpu *vcpu)
94{
95 u64 jd = mftb() - vcpu->arch.dec_jiffies;
96 return vcpu->arch.dec - jd;
97}
98#endif
99
Alexander Grafa76f8492010-01-15 14:49:14 +0100100static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
101{
102 vcpu->arch.shadow_msr = vcpu->arch.msr;
103 /* Guest MSR values */
104 vcpu->arch.shadow_msr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE |
105 MSR_BE | MSR_DE;
106 /* Process MSR values */
107 vcpu->arch.shadow_msr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR |
108 MSR_EE;
109 /* External providers the guest reserved */
110 vcpu->arch.shadow_msr |= (vcpu->arch.msr & vcpu->arch.guest_owned_ext);
111 /* 64-bit Process MSR values */
112#ifdef CONFIG_PPC_BOOK3S_64
113 vcpu->arch.shadow_msr |= MSR_ISF | MSR_HV;
114#endif
115}
116
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000117void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
118{
119 ulong old_msr = vcpu->arch.msr;
120
121#ifdef EXIT_DEBUG
122 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
123#endif
Alexander Grafa76f8492010-01-15 14:49:14 +0100124
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000125 msr &= to_book3s(vcpu)->msr_mask;
126 vcpu->arch.msr = msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100127 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000128
129 if (msr & (MSR_WE|MSR_POW)) {
130 if (!vcpu->arch.pending_exceptions) {
131 kvm_vcpu_block(vcpu);
132 vcpu->stat.halt_wakeup++;
133 }
134 }
135
136 if (((vcpu->arch.msr & (MSR_IR|MSR_DR)) != (old_msr & (MSR_IR|MSR_DR))) ||
137 (vcpu->arch.msr & MSR_PR) != (old_msr & MSR_PR)) {
138 kvmppc_mmu_flush_segments(vcpu);
139 kvmppc_mmu_map_segment(vcpu, vcpu->arch.pc);
140 }
Alexander Grafd1bab742010-02-19 11:00:35 +0100141
142 /* Preload FPU if it's enabled */
143 if (vcpu->arch.msr & MSR_FP)
144 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000145}
146
147void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
148{
149 vcpu->arch.srr0 = vcpu->arch.pc;
150 vcpu->arch.srr1 = vcpu->arch.msr | flags;
151 vcpu->arch.pc = to_book3s(vcpu)->hior + vec;
152 vcpu->arch.mmu.reset_msr(vcpu);
153}
154
Alexander Graf583617b2009-12-21 20:21:23 +0100155static int kvmppc_book3s_vec2irqprio(unsigned int vec)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000156{
157 unsigned int prio;
158
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000159 switch (vec) {
160 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
161 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
162 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
163 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
164 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
165 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
166 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
167 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
168 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
169 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
170 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
171 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
172 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
173 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
174 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
175 default: prio = BOOK3S_IRQPRIO_MAX; break;
176 }
177
Alexander Graf583617b2009-12-21 20:21:23 +0100178 return prio;
179}
180
Alexander Graf7706664d2009-12-21 20:21:24 +0100181static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
182 unsigned int vec)
183{
184 clear_bit(kvmppc_book3s_vec2irqprio(vec),
185 &vcpu->arch.pending_exceptions);
186}
187
Alexander Graf583617b2009-12-21 20:21:23 +0100188void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
189{
190 vcpu->stat.queue_intr++;
191
192 set_bit(kvmppc_book3s_vec2irqprio(vec),
193 &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000194#ifdef EXIT_DEBUG
195 printk(KERN_INFO "Queueing interrupt %x\n", vec);
196#endif
197}
198
199
Alexander Graf25a8a022010-01-08 02:58:07 +0100200void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000201{
Alexander Graf25a8a022010-01-08 02:58:07 +0100202 to_book3s(vcpu)->prog_flags = flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000203 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM);
204}
205
206void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
207{
208 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
209}
210
211int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
212{
213 return test_bit(BOOK3S_INTERRUPT_DECREMENTER >> 7, &vcpu->arch.pending_exceptions);
214}
215
Alexander Graf7706664d2009-12-21 20:21:24 +0100216void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
217{
218 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
219}
220
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000221void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
222 struct kvm_interrupt *irq)
223{
224 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
225}
226
227int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
228{
229 int deliver = 1;
230 int vec = 0;
Alexander Graf25a8a022010-01-08 02:58:07 +0100231 ulong flags = 0ULL;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000232
233 switch (priority) {
234 case BOOK3S_IRQPRIO_DECREMENTER:
235 deliver = vcpu->arch.msr & MSR_EE;
236 vec = BOOK3S_INTERRUPT_DECREMENTER;
237 break;
238 case BOOK3S_IRQPRIO_EXTERNAL:
239 deliver = vcpu->arch.msr & MSR_EE;
240 vec = BOOK3S_INTERRUPT_EXTERNAL;
241 break;
242 case BOOK3S_IRQPRIO_SYSTEM_RESET:
243 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
244 break;
245 case BOOK3S_IRQPRIO_MACHINE_CHECK:
246 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
247 break;
248 case BOOK3S_IRQPRIO_DATA_STORAGE:
249 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
250 break;
251 case BOOK3S_IRQPRIO_INST_STORAGE:
252 vec = BOOK3S_INTERRUPT_INST_STORAGE;
253 break;
254 case BOOK3S_IRQPRIO_DATA_SEGMENT:
255 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
256 break;
257 case BOOK3S_IRQPRIO_INST_SEGMENT:
258 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
259 break;
260 case BOOK3S_IRQPRIO_ALIGNMENT:
261 vec = BOOK3S_INTERRUPT_ALIGNMENT;
262 break;
263 case BOOK3S_IRQPRIO_PROGRAM:
264 vec = BOOK3S_INTERRUPT_PROGRAM;
Alexander Graf25a8a022010-01-08 02:58:07 +0100265 flags = to_book3s(vcpu)->prog_flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000266 break;
267 case BOOK3S_IRQPRIO_VSX:
268 vec = BOOK3S_INTERRUPT_VSX;
269 break;
270 case BOOK3S_IRQPRIO_ALTIVEC:
271 vec = BOOK3S_INTERRUPT_ALTIVEC;
272 break;
273 case BOOK3S_IRQPRIO_FP_UNAVAIL:
274 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
275 break;
276 case BOOK3S_IRQPRIO_SYSCALL:
277 vec = BOOK3S_INTERRUPT_SYSCALL;
278 break;
279 case BOOK3S_IRQPRIO_DEBUG:
280 vec = BOOK3S_INTERRUPT_TRACE;
281 break;
282 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
283 vec = BOOK3S_INTERRUPT_PERFMON;
284 break;
285 default:
286 deliver = 0;
287 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
288 break;
289 }
290
291#if 0
292 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
293#endif
294
295 if (deliver)
Alexander Graf25a8a022010-01-08 02:58:07 +0100296 kvmppc_inject_interrupt(vcpu, vec, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000297
298 return deliver;
299}
300
301void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
302{
303 unsigned long *pending = &vcpu->arch.pending_exceptions;
304 unsigned int priority;
305
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000306#ifdef EXIT_DEBUG
307 if (vcpu->arch.pending_exceptions)
308 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
309#endif
310 priority = __ffs(*pending);
311 while (priority <= (sizeof(unsigned int) * 8)) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100312 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
313 (priority != BOOK3S_IRQPRIO_DECREMENTER)) {
314 /* DEC interrupts get cleared by mtdec */
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000315 clear_bit(priority, &vcpu->arch.pending_exceptions);
316 break;
317 }
318
319 priority = find_next_bit(pending,
320 BITS_PER_BYTE * sizeof(*pending),
321 priority + 1);
322 }
323}
324
325void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
326{
Alexander Grafe15a1132009-11-30 03:02:02 +0000327 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000328 vcpu->arch.pvr = pvr;
329 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
330 kvmppc_mmu_book3s_64_init(vcpu);
331 to_book3s(vcpu)->hior = 0xfff00000;
332 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
333 } else {
334 kvmppc_mmu_book3s_32_init(vcpu);
335 to_book3s(vcpu)->hior = 0;
336 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
337 }
338
339 /* If we are in hypervisor level on 970, we can tell the CPU to
340 * treat DCBZ as 32 bytes store */
341 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
342 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
343 !strcmp(cur_cpu_spec->platform, "ppc970"))
344 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
345
346}
347
348/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
349 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
350 * emulate 32 bytes dcbz length.
351 *
352 * The Book3s_64 inventors also realized this case and implemented a special bit
353 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
354 *
355 * My approach here is to patch the dcbz instruction on executing pages.
356 */
357static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
358{
359 bool touched = false;
360 hva_t hpage;
361 u32 *page;
362 int i;
363
364 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
365 if (kvm_is_error_hva(hpage))
366 return;
367
368 hpage |= pte->raddr & ~PAGE_MASK;
369 hpage &= ~0xFFFULL;
370
371 page = vmalloc(HW_PAGE_SIZE);
372
373 if (copy_from_user(page, (void __user *)hpage, HW_PAGE_SIZE))
374 goto out;
375
376 for (i=0; i < HW_PAGE_SIZE / 4; i++)
377 if ((page[i] & 0xff0007ff) == INS_DCBZ) {
378 page[i] &= 0xfffffff7; // reserved instruction, so we trap
379 touched = true;
380 }
381
382 if (touched)
383 copy_to_user((void __user *)hpage, page, HW_PAGE_SIZE);
384
385out:
386 vfree(page);
387}
388
389static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
390 struct kvmppc_pte *pte)
391{
392 int relocated = (vcpu->arch.msr & (data ? MSR_DR : MSR_IR));
393 int r;
394
395 if (relocated) {
396 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
397 } else {
398 pte->eaddr = eaddr;
399 pte->raddr = eaddr & 0xffffffff;
400 pte->vpage = eaddr >> 12;
401 switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
402 case 0:
403 pte->vpage |= VSID_REAL;
404 case MSR_DR:
405 pte->vpage |= VSID_REAL_DR;
406 case MSR_IR:
407 pte->vpage |= VSID_REAL_IR;
408 }
409 pte->may_read = true;
410 pte->may_write = true;
411 pte->may_execute = true;
412 r = 0;
413 }
414
415 return r;
416}
417
418static hva_t kvmppc_bad_hva(void)
419{
420 return PAGE_OFFSET;
421}
422
423static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
424 bool read)
425{
426 hva_t hpage;
427
428 if (read && !pte->may_read)
429 goto err;
430
431 if (!read && !pte->may_write)
432 goto err;
433
434 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
435 if (kvm_is_error_hva(hpage))
436 goto err;
437
438 return hpage | (pte->raddr & ~PAGE_MASK);
439err:
440 return kvmppc_bad_hva();
441}
442
443int kvmppc_st(struct kvm_vcpu *vcpu, ulong eaddr, int size, void *ptr)
444{
445 struct kvmppc_pte pte;
446 hva_t hva = eaddr;
447
448 vcpu->stat.st++;
449
450 if (kvmppc_xlate(vcpu, eaddr, false, &pte))
451 goto err;
452
453 hva = kvmppc_pte_to_hva(vcpu, &pte, false);
454 if (kvm_is_error_hva(hva))
455 goto err;
456
457 if (copy_to_user((void __user *)hva, ptr, size)) {
458 printk(KERN_INFO "kvmppc_st at 0x%lx failed\n", hva);
459 goto err;
460 }
461
462 return 0;
463
464err:
465 return -ENOENT;
466}
467
468int kvmppc_ld(struct kvm_vcpu *vcpu, ulong eaddr, int size, void *ptr,
469 bool data)
470{
471 struct kvmppc_pte pte;
472 hva_t hva = eaddr;
473
474 vcpu->stat.ld++;
475
476 if (kvmppc_xlate(vcpu, eaddr, data, &pte))
477 goto err;
478
479 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
480 if (kvm_is_error_hva(hva))
481 goto err;
482
483 if (copy_from_user(ptr, (void __user *)hva, size)) {
484 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
485 goto err;
486 }
487
488 return 0;
489
490err:
491 return -ENOENT;
492}
493
494static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
495{
496 return kvm_is_visible_gfn(vcpu->kvm, gfn);
497}
498
499int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
500 ulong eaddr, int vec)
501{
502 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
503 int r = RESUME_GUEST;
504 int relocated;
505 int page_found = 0;
506 struct kvmppc_pte pte;
507 bool is_mmio = false;
508
509 if ( vec == BOOK3S_INTERRUPT_DATA_STORAGE ) {
510 relocated = (vcpu->arch.msr & MSR_DR);
511 } else {
512 relocated = (vcpu->arch.msr & MSR_IR);
513 }
514
515 /* Resolve real address if translation turned on */
516 if (relocated) {
517 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
518 } else {
519 pte.may_execute = true;
520 pte.may_read = true;
521 pte.may_write = true;
522 pte.raddr = eaddr & 0xffffffff;
523 pte.eaddr = eaddr;
524 pte.vpage = eaddr >> 12;
525 switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
526 case 0:
527 pte.vpage |= VSID_REAL;
528 case MSR_DR:
529 pte.vpage |= VSID_REAL_DR;
530 case MSR_IR:
531 pte.vpage |= VSID_REAL_IR;
532 }
533 }
534
535 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
536 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
537 /*
538 * If we do the dcbz hack, we have to NX on every execution,
539 * so we can patch the executing code. This renders our guest
540 * NX-less.
541 */
542 pte.may_execute = !data;
543 }
544
545 if (page_found == -ENOENT) {
546 /* Page not found in guest PTE entries */
547 vcpu->arch.dear = vcpu->arch.fault_dear;
548 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr;
Alexander Graff7adbba2010-01-15 14:49:13 +0100549 vcpu->arch.msr |= (vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000550 kvmppc_book3s_queue_irqprio(vcpu, vec);
551 } else if (page_found == -EPERM) {
552 /* Storage protection */
553 vcpu->arch.dear = vcpu->arch.fault_dear;
554 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr & ~DSISR_NOHPTE;
555 to_book3s(vcpu)->dsisr |= DSISR_PROTFAULT;
Alexander Graff7adbba2010-01-15 14:49:13 +0100556 vcpu->arch.msr |= (vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000557 kvmppc_book3s_queue_irqprio(vcpu, vec);
558 } else if (page_found == -EINVAL) {
559 /* Page not found in guest SLB */
560 vcpu->arch.dear = vcpu->arch.fault_dear;
561 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
562 } else if (!is_mmio &&
563 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
564 /* The guest's PTE is not mapped yet. Map on the host */
565 kvmppc_mmu_map_page(vcpu, &pte);
566 if (data)
567 vcpu->stat.sp_storage++;
568 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
569 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
570 kvmppc_patch_dcbz(vcpu, &pte);
571 } else {
572 /* MMIO */
573 vcpu->stat.mmio_exits++;
574 vcpu->arch.paddr_accessed = pte.raddr;
575 r = kvmppc_emulate_mmio(run, vcpu);
576 if ( r == RESUME_HOST_NV )
577 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000578 }
579
580 return r;
581}
582
Alexander Graf180a34d2010-01-15 14:49:11 +0100583static inline int get_fpr_index(int i)
584{
585#ifdef CONFIG_VSX
586 i *= 2;
587#endif
588 return i;
589}
590
591/* Give up external provider (FPU, Altivec, VSX) */
592static void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
593{
594 struct thread_struct *t = &current->thread;
595 u64 *vcpu_fpr = vcpu->arch.fpr;
596 u64 *vcpu_vsx = vcpu->arch.vsr;
597 u64 *thread_fpr = (u64*)t->fpr;
598 int i;
599
600 if (!(vcpu->arch.guest_owned_ext & msr))
601 return;
602
603#ifdef DEBUG_EXT
604 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
605#endif
606
607 switch (msr) {
608 case MSR_FP:
609 giveup_fpu(current);
610 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
611 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
612
613 vcpu->arch.fpscr = t->fpscr.val;
614 break;
615 case MSR_VEC:
616#ifdef CONFIG_ALTIVEC
617 giveup_altivec(current);
618 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
619 vcpu->arch.vscr = t->vscr;
620#endif
621 break;
622 case MSR_VSX:
623#ifdef CONFIG_VSX
624 __giveup_vsx(current);
625 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
626 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
627#endif
628 break;
629 default:
630 BUG();
631 }
632
633 vcpu->arch.guest_owned_ext &= ~msr;
634 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100635 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100636}
637
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100638static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
639{
640 ulong srr0 = vcpu->arch.pc;
641 int ret;
642
643 /* Need to do paired single emulation? */
644 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
645 return EMULATE_DONE;
646
647 /* Read out the instruction */
648 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &vcpu->arch.last_inst, false);
649 if (ret == -ENOENT) {
650 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 33, 33, 1);
651 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 34, 36, 0);
652 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 42, 47, 0);
653 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
654 } else if(ret == EMULATE_DONE) {
655 /* Need to emulate */
656 return EMULATE_FAIL;
657 }
658
659 return EMULATE_AGAIN;
660}
661
Alexander Graf180a34d2010-01-15 14:49:11 +0100662/* Handle external providers (FPU, Altivec, VSX) */
663static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
664 ulong msr)
665{
666 struct thread_struct *t = &current->thread;
667 u64 *vcpu_fpr = vcpu->arch.fpr;
668 u64 *vcpu_vsx = vcpu->arch.vsr;
669 u64 *thread_fpr = (u64*)t->fpr;
670 int i;
671
Alexander Graf3c402a72010-02-19 11:00:32 +0100672 /* When we have paired singles, we emulate in software */
673 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
674 return RESUME_GUEST;
675
Alexander Graf180a34d2010-01-15 14:49:11 +0100676 if (!(vcpu->arch.msr & msr)) {
677 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
678 return RESUME_GUEST;
679 }
680
681#ifdef DEBUG_EXT
682 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
683#endif
684
685 current->thread.regs->msr |= msr;
686
687 switch (msr) {
688 case MSR_FP:
689 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
690 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
691
692 t->fpscr.val = vcpu->arch.fpscr;
693 t->fpexc_mode = 0;
694 kvmppc_load_up_fpu();
695 break;
696 case MSR_VEC:
697#ifdef CONFIG_ALTIVEC
698 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
699 t->vscr = vcpu->arch.vscr;
700 t->vrsave = -1;
701 kvmppc_load_up_altivec();
702#endif
703 break;
704 case MSR_VSX:
705#ifdef CONFIG_VSX
706 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
707 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
708 kvmppc_load_up_vsx();
709#endif
710 break;
711 default:
712 BUG();
713 }
714
715 vcpu->arch.guest_owned_ext |= msr;
716
Alexander Grafa76f8492010-01-15 14:49:14 +0100717 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100718
719 return RESUME_GUEST;
720}
721
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000722int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
723 unsigned int exit_nr)
724{
725 int r = RESUME_HOST;
726
727 vcpu->stat.sum_exits++;
728
729 run->exit_reason = KVM_EXIT_UNKNOWN;
730 run->ready_for_interrupt_injection = 1;
731#ifdef EXIT_DEBUG
732 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | dec=0x%x | msr=0x%lx\n",
733 exit_nr, vcpu->arch.pc, vcpu->arch.fault_dear,
734 kvmppc_get_dec(vcpu), vcpu->arch.msr);
735#elif defined (EXIT_DEBUG_SIMPLE)
736 if ((exit_nr != 0x900) && (exit_nr != 0x500))
737 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | msr=0x%lx\n",
738 exit_nr, vcpu->arch.pc, vcpu->arch.fault_dear,
739 vcpu->arch.msr);
740#endif
741 kvm_resched(vcpu);
742 switch (exit_nr) {
743 case BOOK3S_INTERRUPT_INST_STORAGE:
744 vcpu->stat.pf_instruc++;
745 /* only care about PTEG not found errors, but leave NX alone */
Alexander Graff7adbba2010-01-15 14:49:13 +0100746 if (vcpu->arch.shadow_srr1 & 0x40000000) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000747 r = kvmppc_handle_pagefault(run, vcpu, vcpu->arch.pc, exit_nr);
748 vcpu->stat.sp_instruc++;
749 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
750 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
751 /*
752 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
753 * so we can't use the NX bit inside the guest. Let's cross our fingers,
754 * that no guest that needs the dcbz hack does NX.
755 */
756 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.pc, ~0xFFFULL);
757 } else {
Alexander Graff7adbba2010-01-15 14:49:13 +0100758 vcpu->arch.msr |= vcpu->arch.shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000759 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
760 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.pc, ~0xFFFULL);
761 r = RESUME_GUEST;
762 }
763 break;
764 case BOOK3S_INTERRUPT_DATA_STORAGE:
765 vcpu->stat.pf_storage++;
766 /* The only case we need to handle is missing shadow PTEs */
767 if (vcpu->arch.fault_dsisr & DSISR_NOHPTE) {
768 r = kvmppc_handle_pagefault(run, vcpu, vcpu->arch.fault_dear, exit_nr);
769 } else {
770 vcpu->arch.dear = vcpu->arch.fault_dear;
771 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr;
772 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
773 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.dear, ~0xFFFULL);
774 r = RESUME_GUEST;
775 }
776 break;
777 case BOOK3S_INTERRUPT_DATA_SEGMENT:
778 if (kvmppc_mmu_map_segment(vcpu, vcpu->arch.fault_dear) < 0) {
779 vcpu->arch.dear = vcpu->arch.fault_dear;
780 kvmppc_book3s_queue_irqprio(vcpu,
781 BOOK3S_INTERRUPT_DATA_SEGMENT);
782 }
783 r = RESUME_GUEST;
784 break;
785 case BOOK3S_INTERRUPT_INST_SEGMENT:
786 if (kvmppc_mmu_map_segment(vcpu, vcpu->arch.pc) < 0) {
787 kvmppc_book3s_queue_irqprio(vcpu,
788 BOOK3S_INTERRUPT_INST_SEGMENT);
789 }
790 r = RESUME_GUEST;
791 break;
792 /* We're good on these - the host merely wanted to get our attention */
793 case BOOK3S_INTERRUPT_DECREMENTER:
794 vcpu->stat.dec_exits++;
795 r = RESUME_GUEST;
796 break;
797 case BOOK3S_INTERRUPT_EXTERNAL:
798 vcpu->stat.ext_intr_exits++;
799 r = RESUME_GUEST;
800 break;
801 case BOOK3S_INTERRUPT_PROGRAM:
802 {
803 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100804 ulong flags;
805
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100806program_interrupt:
Alexander Graff7adbba2010-01-15 14:49:13 +0100807 flags = vcpu->arch.shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000808
809 if (vcpu->arch.msr & MSR_PR) {
810#ifdef EXIT_DEBUG
811 printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", vcpu->arch.pc, vcpu->arch.last_inst);
812#endif
813 if ((vcpu->arch.last_inst & 0xff0007ff) !=
814 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100815 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000816 r = RESUME_GUEST;
817 break;
818 }
819 }
820
821 vcpu->stat.emulated_inst_exits++;
822 er = kvmppc_emulate_instruction(run, vcpu);
823 switch (er) {
824 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100825 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000826 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100827 case EMULATE_AGAIN:
828 r = RESUME_GUEST;
829 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000830 case EMULATE_FAIL:
831 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
832 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100833 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000834 r = RESUME_GUEST;
835 break;
836 default:
837 BUG();
838 }
839 break;
840 }
841 case BOOK3S_INTERRUPT_SYSCALL:
842#ifdef EXIT_DEBUG
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100843 printk(KERN_INFO "Syscall Nr %d\n", (int)kvmppc_get_gpr(vcpu, 0));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000844#endif
845 vcpu->stat.syscall_exits++;
846 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
847 r = RESUME_GUEST;
848 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000849 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000850 case BOOK3S_INTERRUPT_ALTIVEC:
851 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100852 {
853 int ext_msr = 0;
854
855 switch (exit_nr) {
856 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
857 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
858 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
859 }
860
861 switch (kvmppc_check_ext(vcpu, exit_nr)) {
862 case EMULATE_DONE:
863 /* everything ok - let's enable the ext */
864 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
865 break;
866 case EMULATE_FAIL:
867 /* we need to emulate this instruction */
868 goto program_interrupt;
869 break;
870 default:
871 /* nothing to worry about - go again */
872 break;
873 }
Alexander Graf180a34d2010-01-15 14:49:11 +0100874 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100875 }
Alexander Graf180a34d2010-01-15 14:49:11 +0100876 case BOOK3S_INTERRUPT_MACHINE_CHECK:
877 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000878 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
879 r = RESUME_GUEST;
880 break;
881 default:
882 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +0100883 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
884 exit_nr, vcpu->arch.pc, vcpu->arch.shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000885 r = RESUME_HOST;
886 BUG();
887 break;
888 }
889
890
891 if (!(r & RESUME_HOST)) {
892 /* To avoid clobbering exit_reason, only check for signals if
893 * we aren't already exiting to userspace for some other
894 * reason. */
895 if (signal_pending(current)) {
896#ifdef EXIT_DEBUG
897 printk(KERN_EMERG "KVM: Going back to host\n");
898#endif
899 vcpu->stat.signal_exits++;
900 run->exit_reason = KVM_EXIT_INTR;
901 r = -EINTR;
902 } else {
903 /* In case an interrupt came in that was triggered
904 * from userspace (like DEC), we need to check what
905 * to inject now! */
906 kvmppc_core_deliver_interrupts(vcpu);
907 }
908 }
909
910#ifdef EXIT_DEBUG
911 printk(KERN_EMERG "KVM exit: vcpu=0x%p pc=0x%lx r=0x%x\n", vcpu, vcpu->arch.pc, r);
912#endif
913
914 return r;
915}
916
917int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
918{
919 return 0;
920}
921
922int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
923{
924 int i;
925
926 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100927 regs->cr = kvmppc_get_cr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000928 regs->ctr = vcpu->arch.ctr;
929 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100930 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000931 regs->msr = vcpu->arch.msr;
932 regs->srr0 = vcpu->arch.srr0;
933 regs->srr1 = vcpu->arch.srr1;
934 regs->pid = vcpu->arch.pid;
935 regs->sprg0 = vcpu->arch.sprg0;
936 regs->sprg1 = vcpu->arch.sprg1;
937 regs->sprg2 = vcpu->arch.sprg2;
938 regs->sprg3 = vcpu->arch.sprg3;
939 regs->sprg5 = vcpu->arch.sprg4;
940 regs->sprg6 = vcpu->arch.sprg5;
941 regs->sprg7 = vcpu->arch.sprg6;
942
943 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100944 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000945
946 return 0;
947}
948
949int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
950{
951 int i;
952
953 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100954 kvmppc_set_cr(vcpu, regs->cr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000955 vcpu->arch.ctr = regs->ctr;
956 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100957 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000958 kvmppc_set_msr(vcpu, regs->msr);
959 vcpu->arch.srr0 = regs->srr0;
960 vcpu->arch.srr1 = regs->srr1;
961 vcpu->arch.sprg0 = regs->sprg0;
962 vcpu->arch.sprg1 = regs->sprg1;
963 vcpu->arch.sprg2 = regs->sprg2;
964 vcpu->arch.sprg3 = regs->sprg3;
965 vcpu->arch.sprg5 = regs->sprg4;
966 vcpu->arch.sprg6 = regs->sprg5;
967 vcpu->arch.sprg7 = regs->sprg6;
968
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100969 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
970 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000971
972 return 0;
973}
974
975int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
976 struct kvm_sregs *sregs)
977{
Alexander Grafe15a1132009-11-30 03:02:02 +0000978 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
979 int i;
980
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000981 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +0000982
983 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
984 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
985 for (i = 0; i < 64; i++) {
986 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
987 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
988 }
989 } else {
990 for (i = 0; i < 16; i++) {
991 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
992 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
993 }
994 for (i = 0; i < 8; i++) {
995 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
996 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
997 }
998 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000999 return 0;
1000}
1001
1002int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1003 struct kvm_sregs *sregs)
1004{
Alexander Grafe15a1132009-11-30 03:02:02 +00001005 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1006 int i;
1007
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001008 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001009
1010 vcpu3s->sdr1 = sregs->u.s.sdr1;
1011 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1012 for (i = 0; i < 64; i++) {
1013 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1014 sregs->u.s.ppc64.slb[i].slbe);
1015 }
1016 } else {
1017 for (i = 0; i < 16; i++) {
1018 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1019 }
1020 for (i = 0; i < 8; i++) {
1021 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1022 (u32)sregs->u.s.ppc32.ibat[i]);
1023 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1024 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1025 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1026 (u32)sregs->u.s.ppc32.dbat[i]);
1027 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1028 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1029 }
1030 }
1031
1032 /* Flush the MMU after messing with the segments */
1033 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001034 return 0;
1035}
1036
1037int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1038{
1039 return -ENOTSUPP;
1040}
1041
1042int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1043{
1044 return -ENOTSUPP;
1045}
1046
1047int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1048 struct kvm_translation *tr)
1049{
1050 return 0;
1051}
1052
1053/*
1054 * Get (and clear) the dirty memory log for a memory slot.
1055 */
1056int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1057 struct kvm_dirty_log *log)
1058{
1059 struct kvm_memory_slot *memslot;
1060 struct kvm_vcpu *vcpu;
1061 ulong ga, ga_end;
1062 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001063 int r;
1064 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001065
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001066 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001067
1068 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1069 if (r)
1070 goto out;
1071
1072 /* If nothing is dirty, don't bother messing with page tables. */
1073 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001074 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001075
1076 ga = memslot->base_gfn << PAGE_SHIFT;
1077 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1078
1079 kvm_for_each_vcpu(n, vcpu, kvm)
1080 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1081
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001082 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001083 memset(memslot->dirty_bitmap, 0, n);
1084 }
1085
1086 r = 0;
1087out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001088 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001089 return r;
1090}
1091
1092int kvmppc_core_check_processor_compat(void)
1093{
1094 return 0;
1095}
1096
1097struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1098{
1099 struct kvmppc_vcpu_book3s *vcpu_book3s;
1100 struct kvm_vcpu *vcpu;
1101 int err;
1102
1103 vcpu_book3s = (struct kvmppc_vcpu_book3s *)__get_free_pages( GFP_KERNEL | __GFP_ZERO,
1104 get_order(sizeof(struct kvmppc_vcpu_book3s)));
1105 if (!vcpu_book3s) {
1106 err = -ENOMEM;
1107 goto out;
1108 }
1109
1110 vcpu = &vcpu_book3s->vcpu;
1111 err = kvm_vcpu_init(vcpu, kvm, id);
1112 if (err)
1113 goto free_vcpu;
1114
1115 vcpu->arch.host_retip = kvm_return_point;
1116 vcpu->arch.host_msr = mfmsr();
1117 /* default to book3s_64 (970fx) */
1118 vcpu->arch.pvr = 0x3C0301;
1119 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1120 vcpu_book3s->slb_nr = 64;
1121
1122 /* remember where some real-mode handlers are */
1123 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1124 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1125 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf021ec9c2010-01-08 02:58:06 +01001126 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001127
1128 vcpu->arch.shadow_msr = MSR_USER64;
1129
1130 err = __init_new_context();
1131 if (err < 0)
1132 goto free_vcpu;
1133 vcpu_book3s->context_id = err;
1134
1135 vcpu_book3s->vsid_max = ((vcpu_book3s->context_id + 1) << USER_ESID_BITS) - 1;
1136 vcpu_book3s->vsid_first = vcpu_book3s->context_id << USER_ESID_BITS;
1137 vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
1138
1139 return vcpu;
1140
1141free_vcpu:
1142 free_pages((long)vcpu_book3s, get_order(sizeof(struct kvmppc_vcpu_book3s)));
1143out:
1144 return ERR_PTR(err);
1145}
1146
1147void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1148{
1149 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1150
1151 __destroy_context(vcpu_book3s->context_id);
1152 kvm_vcpu_uninit(vcpu);
1153 free_pages((long)vcpu_book3s, get_order(sizeof(struct kvmppc_vcpu_book3s)));
1154}
1155
1156extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1157int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1158{
1159 int ret;
Alexander Graf180a34d2010-01-15 14:49:11 +01001160 struct thread_struct ext_bkp;
1161 bool save_vec = current->thread.used_vr;
1162 bool save_vsx = current->thread.used_vsr;
1163 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001164
1165 /* No need to go into the guest when all we do is going out */
1166 if (signal_pending(current)) {
1167 kvm_run->exit_reason = KVM_EXIT_INTR;
1168 return -EINTR;
1169 }
1170
Alexander Graf180a34d2010-01-15 14:49:11 +01001171 /* Save FPU state in stack */
1172 if (current->thread.regs->msr & MSR_FP)
1173 giveup_fpu(current);
1174 memcpy(ext_bkp.fpr, current->thread.fpr, sizeof(current->thread.fpr));
1175 ext_bkp.fpscr = current->thread.fpscr;
1176 ext_bkp.fpexc_mode = current->thread.fpexc_mode;
1177
1178#ifdef CONFIG_ALTIVEC
1179 /* Save Altivec state in stack */
1180 if (save_vec) {
1181 if (current->thread.regs->msr & MSR_VEC)
1182 giveup_altivec(current);
1183 memcpy(ext_bkp.vr, current->thread.vr, sizeof(ext_bkp.vr));
1184 ext_bkp.vscr = current->thread.vscr;
1185 ext_bkp.vrsave = current->thread.vrsave;
1186 }
1187 ext_bkp.used_vr = current->thread.used_vr;
1188#endif
1189
1190#ifdef CONFIG_VSX
1191 /* Save VSX state in stack */
1192 if (save_vsx && (current->thread.regs->msr & MSR_VSX))
1193 __giveup_vsx(current);
1194 ext_bkp.used_vsr = current->thread.used_vsr;
1195#endif
1196
1197 /* Remember the MSR with disabled extensions */
1198 ext_msr = current->thread.regs->msr;
1199
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001200 /* XXX we get called with irq disabled - change that! */
1201 local_irq_enable();
1202
Alexander Grafd1bab742010-02-19 11:00:35 +01001203 /* Preload FPU if it's enabled */
1204 if (vcpu->arch.msr & MSR_FP)
1205 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1206
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001207 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1208
1209 local_irq_disable();
1210
Alexander Graf180a34d2010-01-15 14:49:11 +01001211 current->thread.regs->msr = ext_msr;
1212
1213 /* Make sure we save the guest FPU/Altivec/VSX state */
1214 kvmppc_giveup_ext(vcpu, MSR_FP);
1215 kvmppc_giveup_ext(vcpu, MSR_VEC);
1216 kvmppc_giveup_ext(vcpu, MSR_VSX);
1217
1218 /* Restore FPU state from stack */
1219 memcpy(current->thread.fpr, ext_bkp.fpr, sizeof(ext_bkp.fpr));
1220 current->thread.fpscr = ext_bkp.fpscr;
1221 current->thread.fpexc_mode = ext_bkp.fpexc_mode;
1222
1223#ifdef CONFIG_ALTIVEC
1224 /* Restore Altivec state from stack */
1225 if (save_vec && current->thread.used_vr) {
1226 memcpy(current->thread.vr, ext_bkp.vr, sizeof(ext_bkp.vr));
1227 current->thread.vscr = ext_bkp.vscr;
1228 current->thread.vrsave= ext_bkp.vrsave;
1229 }
1230 current->thread.used_vr = ext_bkp.used_vr;
1231#endif
1232
1233#ifdef CONFIG_VSX
1234 current->thread.used_vsr = ext_bkp.used_vsr;
1235#endif
1236
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001237 return ret;
1238}
1239
1240static int kvmppc_book3s_init(void)
1241{
1242 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), THIS_MODULE);
1243}
1244
1245static void kvmppc_book3s_exit(void)
1246{
1247 kvm_exit();
1248}
1249
1250module_init(kvmppc_book3s_init);
1251module_exit(kvmppc_book3s_exit);