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