blob: 8cab902771a2aaa82b26a13ba12e10907244c99c [file] [log] [blame]
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001/*
2 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
3 *
4 * Authors:
5 * Alexander Graf <agraf@suse.de>
6 * Kevin Wolf <mail@kevin-wolf.de>
7 *
8 * Description:
9 * This file is derived from arch/powerpc/kvm/44x.c,
10 * by Hollis Blanchard <hollisb@us.ibm.com>.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License, version 2, as
14 * published by the Free Software Foundation.
15 */
16
17#include <linux/kvm_host.h>
18#include <linux/err.h>
19
20#include <asm/reg.h>
21#include <asm/cputable.h>
22#include <asm/cacheflush.h>
23#include <asm/tlbflush.h>
24#include <asm/uaccess.h>
25#include <asm/io.h>
26#include <asm/kvm_ppc.h>
27#include <asm/kvm_book3s.h>
28#include <asm/mmu_context.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/gfp.h>
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000030#include <linux/sched.h>
31#include <linux/vmalloc.h>
32
33#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
34
35/* #define EXIT_DEBUG */
36/* #define EXIT_DEBUG_SIMPLE */
Alexander Graf180a34d2010-01-15 14:49:11 +010037/* #define DEBUG_EXT */
38
Alexander Grafc8c0b6f2010-02-19 11:00:34 +010039static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
40 ulong msr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000041
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000042struct kvm_stats_debugfs_item debugfs_entries[] = {
43 { "exits", VCPU_STAT(sum_exits) },
44 { "mmio", VCPU_STAT(mmio_exits) },
45 { "sig", VCPU_STAT(signal_exits) },
46 { "sysc", VCPU_STAT(syscall_exits) },
47 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
48 { "dec", VCPU_STAT(dec_exits) },
49 { "ext_intr", VCPU_STAT(ext_intr_exits) },
50 { "queue_intr", VCPU_STAT(queue_intr) },
51 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
52 { "pf_storage", VCPU_STAT(pf_storage) },
53 { "sp_storage", VCPU_STAT(sp_storage) },
54 { "pf_instruc", VCPU_STAT(pf_instruc) },
55 { "sp_instruc", VCPU_STAT(sp_instruc) },
56 { "ld", VCPU_STAT(ld) },
57 { "ld_slow", VCPU_STAT(ld_slow) },
58 { "st", VCPU_STAT(st) },
59 { "st_slow", VCPU_STAT(st_slow) },
60 { NULL }
61};
62
63void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
64{
65}
66
67void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
68{
69}
70
71void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
72{
73 memcpy(get_paca()->kvm_slb, to_book3s(vcpu)->slb_shadow, sizeof(get_paca()->kvm_slb));
Alexander Graf7e57cba2010-01-08 02:58:03 +010074 memcpy(&get_paca()->shadow_vcpu, &to_book3s(vcpu)->shadow_vcpu,
75 sizeof(get_paca()->shadow_vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000076 get_paca()->kvm_slb_max = to_book3s(vcpu)->slb_shadow_max;
77}
78
79void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
80{
81 memcpy(to_book3s(vcpu)->slb_shadow, get_paca()->kvm_slb, sizeof(get_paca()->kvm_slb));
Alexander Graf7e57cba2010-01-08 02:58:03 +010082 memcpy(&to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
83 sizeof(get_paca()->shadow_vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000084 to_book3s(vcpu)->slb_shadow_max = get_paca()->kvm_slb_max;
Alexander Graf180a34d2010-01-15 14:49:11 +010085
86 kvmppc_giveup_ext(vcpu, MSR_FP);
87 kvmppc_giveup_ext(vcpu, MSR_VEC);
88 kvmppc_giveup_ext(vcpu, MSR_VSX);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000089}
90
Alexander Graf0bb1fb72009-12-21 20:21:25 +010091#if defined(EXIT_DEBUG)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000092static u32 kvmppc_get_dec(struct kvm_vcpu *vcpu)
93{
94 u64 jd = mftb() - vcpu->arch.dec_jiffies;
95 return vcpu->arch.dec - jd;
96}
97#endif
98
Alexander Grafa76f8492010-01-15 14:49:14 +010099static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
100{
101 vcpu->arch.shadow_msr = vcpu->arch.msr;
102 /* Guest MSR values */
103 vcpu->arch.shadow_msr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE |
104 MSR_BE | MSR_DE;
105 /* Process MSR values */
106 vcpu->arch.shadow_msr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR |
107 MSR_EE;
108 /* External providers the guest reserved */
109 vcpu->arch.shadow_msr |= (vcpu->arch.msr & vcpu->arch.guest_owned_ext);
110 /* 64-bit Process MSR values */
111#ifdef CONFIG_PPC_BOOK3S_64
112 vcpu->arch.shadow_msr |= MSR_ISF | MSR_HV;
113#endif
114}
115
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000116void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
117{
118 ulong old_msr = vcpu->arch.msr;
119
120#ifdef EXIT_DEBUG
121 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
122#endif
Alexander Grafa76f8492010-01-15 14:49:14 +0100123
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000124 msr &= to_book3s(vcpu)->msr_mask;
125 vcpu->arch.msr = msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100126 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000127
128 if (msr & (MSR_WE|MSR_POW)) {
129 if (!vcpu->arch.pending_exceptions) {
130 kvm_vcpu_block(vcpu);
131 vcpu->stat.halt_wakeup++;
132 }
133 }
134
135 if (((vcpu->arch.msr & (MSR_IR|MSR_DR)) != (old_msr & (MSR_IR|MSR_DR))) ||
136 (vcpu->arch.msr & MSR_PR) != (old_msr & MSR_PR)) {
137 kvmppc_mmu_flush_segments(vcpu);
138 kvmppc_mmu_map_segment(vcpu, vcpu->arch.pc);
139 }
Alexander Grafd1bab742010-02-19 11:00:35 +0100140
141 /* Preload FPU if it's enabled */
142 if (vcpu->arch.msr & MSR_FP)
143 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000144}
145
146void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
147{
148 vcpu->arch.srr0 = vcpu->arch.pc;
149 vcpu->arch.srr1 = vcpu->arch.msr | flags;
150 vcpu->arch.pc = to_book3s(vcpu)->hior + vec;
151 vcpu->arch.mmu.reset_msr(vcpu);
152}
153
Alexander Graf583617b2009-12-21 20:21:23 +0100154static int kvmppc_book3s_vec2irqprio(unsigned int vec)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000155{
156 unsigned int prio;
157
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000158 switch (vec) {
159 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
160 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
161 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
162 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
163 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
164 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
165 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
166 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
167 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
168 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
169 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
170 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
171 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
172 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
173 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
174 default: prio = BOOK3S_IRQPRIO_MAX; break;
175 }
176
Alexander Graf583617b2009-12-21 20:21:23 +0100177 return prio;
178}
179
Alexander Graf7706664d2009-12-21 20:21:24 +0100180static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
181 unsigned int vec)
182{
183 clear_bit(kvmppc_book3s_vec2irqprio(vec),
184 &vcpu->arch.pending_exceptions);
185}
186
Alexander Graf583617b2009-12-21 20:21:23 +0100187void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
188{
189 vcpu->stat.queue_intr++;
190
191 set_bit(kvmppc_book3s_vec2irqprio(vec),
192 &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000193#ifdef EXIT_DEBUG
194 printk(KERN_INFO "Queueing interrupt %x\n", vec);
195#endif
196}
197
198
Alexander Graf25a8a022010-01-08 02:58:07 +0100199void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000200{
Alexander Graf25a8a022010-01-08 02:58:07 +0100201 to_book3s(vcpu)->prog_flags = flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000202 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM);
203}
204
205void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
206{
207 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
208}
209
210int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
211{
212 return test_bit(BOOK3S_INTERRUPT_DECREMENTER >> 7, &vcpu->arch.pending_exceptions);
213}
214
Alexander Graf7706664d2009-12-21 20:21:24 +0100215void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
216{
217 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
218}
219
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000220void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
221 struct kvm_interrupt *irq)
222{
223 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
224}
225
226int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
227{
228 int deliver = 1;
229 int vec = 0;
Alexander Graf25a8a022010-01-08 02:58:07 +0100230 ulong flags = 0ULL;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000231
232 switch (priority) {
233 case BOOK3S_IRQPRIO_DECREMENTER:
234 deliver = vcpu->arch.msr & MSR_EE;
235 vec = BOOK3S_INTERRUPT_DECREMENTER;
236 break;
237 case BOOK3S_IRQPRIO_EXTERNAL:
238 deliver = vcpu->arch.msr & MSR_EE;
239 vec = BOOK3S_INTERRUPT_EXTERNAL;
240 break;
241 case BOOK3S_IRQPRIO_SYSTEM_RESET:
242 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
243 break;
244 case BOOK3S_IRQPRIO_MACHINE_CHECK:
245 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
246 break;
247 case BOOK3S_IRQPRIO_DATA_STORAGE:
248 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
249 break;
250 case BOOK3S_IRQPRIO_INST_STORAGE:
251 vec = BOOK3S_INTERRUPT_INST_STORAGE;
252 break;
253 case BOOK3S_IRQPRIO_DATA_SEGMENT:
254 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
255 break;
256 case BOOK3S_IRQPRIO_INST_SEGMENT:
257 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
258 break;
259 case BOOK3S_IRQPRIO_ALIGNMENT:
260 vec = BOOK3S_INTERRUPT_ALIGNMENT;
261 break;
262 case BOOK3S_IRQPRIO_PROGRAM:
263 vec = BOOK3S_INTERRUPT_PROGRAM;
Alexander Graf25a8a022010-01-08 02:58:07 +0100264 flags = to_book3s(vcpu)->prog_flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000265 break;
266 case BOOK3S_IRQPRIO_VSX:
267 vec = BOOK3S_INTERRUPT_VSX;
268 break;
269 case BOOK3S_IRQPRIO_ALTIVEC:
270 vec = BOOK3S_INTERRUPT_ALTIVEC;
271 break;
272 case BOOK3S_IRQPRIO_FP_UNAVAIL:
273 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
274 break;
275 case BOOK3S_IRQPRIO_SYSCALL:
276 vec = BOOK3S_INTERRUPT_SYSCALL;
277 break;
278 case BOOK3S_IRQPRIO_DEBUG:
279 vec = BOOK3S_INTERRUPT_TRACE;
280 break;
281 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
282 vec = BOOK3S_INTERRUPT_PERFMON;
283 break;
284 default:
285 deliver = 0;
286 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
287 break;
288 }
289
290#if 0
291 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
292#endif
293
294 if (deliver)
Alexander Graf25a8a022010-01-08 02:58:07 +0100295 kvmppc_inject_interrupt(vcpu, vec, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000296
297 return deliver;
298}
299
300void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
301{
302 unsigned long *pending = &vcpu->arch.pending_exceptions;
303 unsigned int priority;
304
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000305#ifdef EXIT_DEBUG
306 if (vcpu->arch.pending_exceptions)
307 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
308#endif
309 priority = __ffs(*pending);
310 while (priority <= (sizeof(unsigned int) * 8)) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100311 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
312 (priority != BOOK3S_IRQPRIO_DECREMENTER)) {
313 /* DEC interrupts get cleared by mtdec */
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000314 clear_bit(priority, &vcpu->arch.pending_exceptions);
315 break;
316 }
317
318 priority = find_next_bit(pending,
319 BITS_PER_BYTE * sizeof(*pending),
320 priority + 1);
321 }
322}
323
324void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
325{
Alexander Grafe15a1132009-11-30 03:02:02 +0000326 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000327 vcpu->arch.pvr = pvr;
328 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
329 kvmppc_mmu_book3s_64_init(vcpu);
330 to_book3s(vcpu)->hior = 0xfff00000;
331 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
332 } else {
333 kvmppc_mmu_book3s_32_init(vcpu);
334 to_book3s(vcpu)->hior = 0;
335 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
336 }
337
338 /* If we are in hypervisor level on 970, we can tell the CPU to
339 * treat DCBZ as 32 bytes store */
340 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
341 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
342 !strcmp(cur_cpu_spec->platform, "ppc970"))
343 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
344
345}
346
347/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
348 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
349 * emulate 32 bytes dcbz length.
350 *
351 * The Book3s_64 inventors also realized this case and implemented a special bit
352 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
353 *
354 * My approach here is to patch the dcbz instruction on executing pages.
355 */
356static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
357{
358 bool touched = false;
359 hva_t hpage;
360 u32 *page;
361 int i;
362
363 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
364 if (kvm_is_error_hva(hpage))
365 return;
366
367 hpage |= pte->raddr & ~PAGE_MASK;
368 hpage &= ~0xFFFULL;
369
370 page = vmalloc(HW_PAGE_SIZE);
371
372 if (copy_from_user(page, (void __user *)hpage, HW_PAGE_SIZE))
373 goto out;
374
375 for (i=0; i < HW_PAGE_SIZE / 4; i++)
376 if ((page[i] & 0xff0007ff) == INS_DCBZ) {
377 page[i] &= 0xfffffff7; // reserved instruction, so we trap
378 touched = true;
379 }
380
381 if (touched)
382 copy_to_user((void __user *)hpage, page, HW_PAGE_SIZE);
383
384out:
385 vfree(page);
386}
387
388static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
389 struct kvmppc_pte *pte)
390{
391 int relocated = (vcpu->arch.msr & (data ? MSR_DR : MSR_IR));
392 int r;
393
394 if (relocated) {
395 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
396 } else {
397 pte->eaddr = eaddr;
398 pte->raddr = eaddr & 0xffffffff;
399 pte->vpage = eaddr >> 12;
400 switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
401 case 0:
402 pte->vpage |= VSID_REAL;
403 case MSR_DR:
404 pte->vpage |= VSID_REAL_DR;
405 case MSR_IR:
406 pte->vpage |= VSID_REAL_IR;
407 }
408 pte->may_read = true;
409 pte->may_write = true;
410 pte->may_execute = true;
411 r = 0;
412 }
413
414 return r;
415}
416
417static hva_t kvmppc_bad_hva(void)
418{
419 return PAGE_OFFSET;
420}
421
422static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
423 bool read)
424{
425 hva_t hpage;
426
427 if (read && !pte->may_read)
428 goto err;
429
430 if (!read && !pte->may_write)
431 goto err;
432
433 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
434 if (kvm_is_error_hva(hpage))
435 goto err;
436
437 return hpage | (pte->raddr & ~PAGE_MASK);
438err:
439 return kvmppc_bad_hva();
440}
441
Alexander Graf5467a972010-02-19 11:00:38 +0100442int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
443 bool data)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000444{
445 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100446 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000447
448 vcpu->stat.st++;
449
Alexander Graf5467a972010-02-19 11:00:38 +0100450 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
451 goto nopte;
452
453 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000454
455 hva = kvmppc_pte_to_hva(vcpu, &pte, false);
456 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100457 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000458
459 if (copy_to_user((void __user *)hva, ptr, size)) {
460 printk(KERN_INFO "kvmppc_st at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100461 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000462 }
463
Alexander Graf5467a972010-02-19 11:00:38 +0100464 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000465
Alexander Graf5467a972010-02-19 11:00:38 +0100466nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000467 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100468mmio:
469 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000470}
471
Alexander Graf5467a972010-02-19 11:00:38 +0100472int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000473 bool data)
474{
475 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100476 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000477
478 vcpu->stat.ld++;
479
Alexander Graf5467a972010-02-19 11:00:38 +0100480 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
481 goto nopte;
482
483 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000484
485 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
486 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100487 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000488
489 if (copy_from_user(ptr, (void __user *)hva, size)) {
490 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100491 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000492 }
493
Alexander Graf5467a972010-02-19 11:00:38 +0100494 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000495
Alexander Graf5467a972010-02-19 11:00:38 +0100496nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000497 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100498mmio:
499 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000500}
501
502static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
503{
504 return kvm_is_visible_gfn(vcpu->kvm, gfn);
505}
506
507int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
508 ulong eaddr, int vec)
509{
510 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
511 int r = RESUME_GUEST;
512 int relocated;
513 int page_found = 0;
514 struct kvmppc_pte pte;
515 bool is_mmio = false;
516
517 if ( vec == BOOK3S_INTERRUPT_DATA_STORAGE ) {
518 relocated = (vcpu->arch.msr & MSR_DR);
519 } else {
520 relocated = (vcpu->arch.msr & MSR_IR);
521 }
522
523 /* Resolve real address if translation turned on */
524 if (relocated) {
525 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
526 } else {
527 pte.may_execute = true;
528 pte.may_read = true;
529 pte.may_write = true;
530 pte.raddr = eaddr & 0xffffffff;
531 pte.eaddr = eaddr;
532 pte.vpage = eaddr >> 12;
533 switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
534 case 0:
535 pte.vpage |= VSID_REAL;
536 case MSR_DR:
537 pte.vpage |= VSID_REAL_DR;
538 case MSR_IR:
539 pte.vpage |= VSID_REAL_IR;
540 }
541 }
542
543 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
544 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
545 /*
546 * If we do the dcbz hack, we have to NX on every execution,
547 * so we can patch the executing code. This renders our guest
548 * NX-less.
549 */
550 pte.may_execute = !data;
551 }
552
553 if (page_found == -ENOENT) {
554 /* Page not found in guest PTE entries */
555 vcpu->arch.dear = vcpu->arch.fault_dear;
556 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr;
Alexander Graff7adbba2010-01-15 14:49:13 +0100557 vcpu->arch.msr |= (vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000558 kvmppc_book3s_queue_irqprio(vcpu, vec);
559 } else if (page_found == -EPERM) {
560 /* Storage protection */
561 vcpu->arch.dear = vcpu->arch.fault_dear;
562 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr & ~DSISR_NOHPTE;
563 to_book3s(vcpu)->dsisr |= DSISR_PROTFAULT;
Alexander Graff7adbba2010-01-15 14:49:13 +0100564 vcpu->arch.msr |= (vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000565 kvmppc_book3s_queue_irqprio(vcpu, vec);
566 } else if (page_found == -EINVAL) {
567 /* Page not found in guest SLB */
568 vcpu->arch.dear = vcpu->arch.fault_dear;
569 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
570 } else if (!is_mmio &&
571 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
572 /* The guest's PTE is not mapped yet. Map on the host */
573 kvmppc_mmu_map_page(vcpu, &pte);
574 if (data)
575 vcpu->stat.sp_storage++;
576 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
577 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
578 kvmppc_patch_dcbz(vcpu, &pte);
579 } else {
580 /* MMIO */
581 vcpu->stat.mmio_exits++;
582 vcpu->arch.paddr_accessed = pte.raddr;
583 r = kvmppc_emulate_mmio(run, vcpu);
584 if ( r == RESUME_HOST_NV )
585 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000586 }
587
588 return r;
589}
590
Alexander Graf180a34d2010-01-15 14:49:11 +0100591static inline int get_fpr_index(int i)
592{
593#ifdef CONFIG_VSX
594 i *= 2;
595#endif
596 return i;
597}
598
599/* Give up external provider (FPU, Altivec, VSX) */
Alexander Grafaba3bd72010-02-19 11:00:39 +0100600void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
Alexander Graf180a34d2010-01-15 14:49:11 +0100601{
602 struct thread_struct *t = &current->thread;
603 u64 *vcpu_fpr = vcpu->arch.fpr;
604 u64 *vcpu_vsx = vcpu->arch.vsr;
605 u64 *thread_fpr = (u64*)t->fpr;
606 int i;
607
608 if (!(vcpu->arch.guest_owned_ext & msr))
609 return;
610
611#ifdef DEBUG_EXT
612 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
613#endif
614
615 switch (msr) {
616 case MSR_FP:
617 giveup_fpu(current);
618 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
619 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
620
621 vcpu->arch.fpscr = t->fpscr.val;
622 break;
623 case MSR_VEC:
624#ifdef CONFIG_ALTIVEC
625 giveup_altivec(current);
626 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
627 vcpu->arch.vscr = t->vscr;
628#endif
629 break;
630 case MSR_VSX:
631#ifdef CONFIG_VSX
632 __giveup_vsx(current);
633 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
634 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
635#endif
636 break;
637 default:
638 BUG();
639 }
640
641 vcpu->arch.guest_owned_ext &= ~msr;
642 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100643 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100644}
645
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100646static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
647{
648 ulong srr0 = vcpu->arch.pc;
649 int ret;
650
651 /* Need to do paired single emulation? */
652 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
653 return EMULATE_DONE;
654
655 /* Read out the instruction */
656 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &vcpu->arch.last_inst, false);
657 if (ret == -ENOENT) {
658 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 33, 33, 1);
659 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 34, 36, 0);
660 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 42, 47, 0);
661 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
662 } else if(ret == EMULATE_DONE) {
663 /* Need to emulate */
664 return EMULATE_FAIL;
665 }
666
667 return EMULATE_AGAIN;
668}
669
Alexander Graf180a34d2010-01-15 14:49:11 +0100670/* Handle external providers (FPU, Altivec, VSX) */
671static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
672 ulong msr)
673{
674 struct thread_struct *t = &current->thread;
675 u64 *vcpu_fpr = vcpu->arch.fpr;
676 u64 *vcpu_vsx = vcpu->arch.vsr;
677 u64 *thread_fpr = (u64*)t->fpr;
678 int i;
679
Alexander Graf3c402a72010-02-19 11:00:32 +0100680 /* When we have paired singles, we emulate in software */
681 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
682 return RESUME_GUEST;
683
Alexander Graf180a34d2010-01-15 14:49:11 +0100684 if (!(vcpu->arch.msr & msr)) {
685 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
686 return RESUME_GUEST;
687 }
688
689#ifdef DEBUG_EXT
690 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
691#endif
692
693 current->thread.regs->msr |= msr;
694
695 switch (msr) {
696 case MSR_FP:
697 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
698 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
699
700 t->fpscr.val = vcpu->arch.fpscr;
701 t->fpexc_mode = 0;
702 kvmppc_load_up_fpu();
703 break;
704 case MSR_VEC:
705#ifdef CONFIG_ALTIVEC
706 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
707 t->vscr = vcpu->arch.vscr;
708 t->vrsave = -1;
709 kvmppc_load_up_altivec();
710#endif
711 break;
712 case MSR_VSX:
713#ifdef CONFIG_VSX
714 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
715 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
716 kvmppc_load_up_vsx();
717#endif
718 break;
719 default:
720 BUG();
721 }
722
723 vcpu->arch.guest_owned_ext |= msr;
724
Alexander Grafa76f8492010-01-15 14:49:14 +0100725 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100726
727 return RESUME_GUEST;
728}
729
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000730int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
731 unsigned int exit_nr)
732{
733 int r = RESUME_HOST;
734
735 vcpu->stat.sum_exits++;
736
737 run->exit_reason = KVM_EXIT_UNKNOWN;
738 run->ready_for_interrupt_injection = 1;
739#ifdef EXIT_DEBUG
740 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | dec=0x%x | msr=0x%lx\n",
741 exit_nr, vcpu->arch.pc, vcpu->arch.fault_dear,
742 kvmppc_get_dec(vcpu), vcpu->arch.msr);
743#elif defined (EXIT_DEBUG_SIMPLE)
744 if ((exit_nr != 0x900) && (exit_nr != 0x500))
745 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | msr=0x%lx\n",
746 exit_nr, vcpu->arch.pc, vcpu->arch.fault_dear,
747 vcpu->arch.msr);
748#endif
749 kvm_resched(vcpu);
750 switch (exit_nr) {
751 case BOOK3S_INTERRUPT_INST_STORAGE:
752 vcpu->stat.pf_instruc++;
753 /* only care about PTEG not found errors, but leave NX alone */
Alexander Graff7adbba2010-01-15 14:49:13 +0100754 if (vcpu->arch.shadow_srr1 & 0x40000000) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000755 r = kvmppc_handle_pagefault(run, vcpu, vcpu->arch.pc, exit_nr);
756 vcpu->stat.sp_instruc++;
757 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
758 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
759 /*
760 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
761 * so we can't use the NX bit inside the guest. Let's cross our fingers,
762 * that no guest that needs the dcbz hack does NX.
763 */
764 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.pc, ~0xFFFULL);
765 } else {
Alexander Graff7adbba2010-01-15 14:49:13 +0100766 vcpu->arch.msr |= vcpu->arch.shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000767 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
768 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.pc, ~0xFFFULL);
769 r = RESUME_GUEST;
770 }
771 break;
772 case BOOK3S_INTERRUPT_DATA_STORAGE:
773 vcpu->stat.pf_storage++;
774 /* The only case we need to handle is missing shadow PTEs */
775 if (vcpu->arch.fault_dsisr & DSISR_NOHPTE) {
776 r = kvmppc_handle_pagefault(run, vcpu, vcpu->arch.fault_dear, exit_nr);
777 } else {
778 vcpu->arch.dear = vcpu->arch.fault_dear;
779 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr;
780 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
781 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.dear, ~0xFFFULL);
782 r = RESUME_GUEST;
783 }
784 break;
785 case BOOK3S_INTERRUPT_DATA_SEGMENT:
786 if (kvmppc_mmu_map_segment(vcpu, vcpu->arch.fault_dear) < 0) {
787 vcpu->arch.dear = vcpu->arch.fault_dear;
788 kvmppc_book3s_queue_irqprio(vcpu,
789 BOOK3S_INTERRUPT_DATA_SEGMENT);
790 }
791 r = RESUME_GUEST;
792 break;
793 case BOOK3S_INTERRUPT_INST_SEGMENT:
794 if (kvmppc_mmu_map_segment(vcpu, vcpu->arch.pc) < 0) {
795 kvmppc_book3s_queue_irqprio(vcpu,
796 BOOK3S_INTERRUPT_INST_SEGMENT);
797 }
798 r = RESUME_GUEST;
799 break;
800 /* We're good on these - the host merely wanted to get our attention */
801 case BOOK3S_INTERRUPT_DECREMENTER:
802 vcpu->stat.dec_exits++;
803 r = RESUME_GUEST;
804 break;
805 case BOOK3S_INTERRUPT_EXTERNAL:
806 vcpu->stat.ext_intr_exits++;
807 r = RESUME_GUEST;
808 break;
809 case BOOK3S_INTERRUPT_PROGRAM:
810 {
811 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100812 ulong flags;
813
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100814program_interrupt:
Alexander Graff7adbba2010-01-15 14:49:13 +0100815 flags = vcpu->arch.shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000816
817 if (vcpu->arch.msr & MSR_PR) {
818#ifdef EXIT_DEBUG
819 printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", vcpu->arch.pc, vcpu->arch.last_inst);
820#endif
821 if ((vcpu->arch.last_inst & 0xff0007ff) !=
822 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100823 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000824 r = RESUME_GUEST;
825 break;
826 }
827 }
828
829 vcpu->stat.emulated_inst_exits++;
830 er = kvmppc_emulate_instruction(run, vcpu);
831 switch (er) {
832 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100833 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000834 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100835 case EMULATE_AGAIN:
836 r = RESUME_GUEST;
837 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000838 case EMULATE_FAIL:
839 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
840 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100841 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000842 r = RESUME_GUEST;
843 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +0100844 case EMULATE_DO_MMIO:
845 run->exit_reason = KVM_EXIT_MMIO;
846 r = RESUME_HOST_NV;
847 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000848 default:
849 BUG();
850 }
851 break;
852 }
853 case BOOK3S_INTERRUPT_SYSCALL:
854#ifdef EXIT_DEBUG
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100855 printk(KERN_INFO "Syscall Nr %d\n", (int)kvmppc_get_gpr(vcpu, 0));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000856#endif
857 vcpu->stat.syscall_exits++;
858 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
859 r = RESUME_GUEST;
860 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000861 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000862 case BOOK3S_INTERRUPT_ALTIVEC:
863 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100864 {
865 int ext_msr = 0;
866
867 switch (exit_nr) {
868 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
869 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
870 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
871 }
872
873 switch (kvmppc_check_ext(vcpu, exit_nr)) {
874 case EMULATE_DONE:
875 /* everything ok - let's enable the ext */
876 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
877 break;
878 case EMULATE_FAIL:
879 /* we need to emulate this instruction */
880 goto program_interrupt;
881 break;
882 default:
883 /* nothing to worry about - go again */
884 break;
885 }
Alexander Graf180a34d2010-01-15 14:49:11 +0100886 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100887 }
Alexander Graf180a34d2010-01-15 14:49:11 +0100888 case BOOK3S_INTERRUPT_MACHINE_CHECK:
889 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000890 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
891 r = RESUME_GUEST;
892 break;
893 default:
894 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +0100895 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
896 exit_nr, vcpu->arch.pc, vcpu->arch.shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000897 r = RESUME_HOST;
898 BUG();
899 break;
900 }
901
902
903 if (!(r & RESUME_HOST)) {
904 /* To avoid clobbering exit_reason, only check for signals if
905 * we aren't already exiting to userspace for some other
906 * reason. */
907 if (signal_pending(current)) {
908#ifdef EXIT_DEBUG
909 printk(KERN_EMERG "KVM: Going back to host\n");
910#endif
911 vcpu->stat.signal_exits++;
912 run->exit_reason = KVM_EXIT_INTR;
913 r = -EINTR;
914 } else {
915 /* In case an interrupt came in that was triggered
916 * from userspace (like DEC), we need to check what
917 * to inject now! */
918 kvmppc_core_deliver_interrupts(vcpu);
919 }
920 }
921
922#ifdef EXIT_DEBUG
923 printk(KERN_EMERG "KVM exit: vcpu=0x%p pc=0x%lx r=0x%x\n", vcpu, vcpu->arch.pc, r);
924#endif
925
926 return r;
927}
928
929int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
930{
931 return 0;
932}
933
934int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
935{
936 int i;
937
938 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100939 regs->cr = kvmppc_get_cr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000940 regs->ctr = vcpu->arch.ctr;
941 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100942 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000943 regs->msr = vcpu->arch.msr;
944 regs->srr0 = vcpu->arch.srr0;
945 regs->srr1 = vcpu->arch.srr1;
946 regs->pid = vcpu->arch.pid;
947 regs->sprg0 = vcpu->arch.sprg0;
948 regs->sprg1 = vcpu->arch.sprg1;
949 regs->sprg2 = vcpu->arch.sprg2;
950 regs->sprg3 = vcpu->arch.sprg3;
951 regs->sprg5 = vcpu->arch.sprg4;
952 regs->sprg6 = vcpu->arch.sprg5;
953 regs->sprg7 = vcpu->arch.sprg6;
954
955 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100956 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000957
958 return 0;
959}
960
961int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
962{
963 int i;
964
965 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100966 kvmppc_set_cr(vcpu, regs->cr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000967 vcpu->arch.ctr = regs->ctr;
968 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100969 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000970 kvmppc_set_msr(vcpu, regs->msr);
971 vcpu->arch.srr0 = regs->srr0;
972 vcpu->arch.srr1 = regs->srr1;
973 vcpu->arch.sprg0 = regs->sprg0;
974 vcpu->arch.sprg1 = regs->sprg1;
975 vcpu->arch.sprg2 = regs->sprg2;
976 vcpu->arch.sprg3 = regs->sprg3;
977 vcpu->arch.sprg5 = regs->sprg4;
978 vcpu->arch.sprg6 = regs->sprg5;
979 vcpu->arch.sprg7 = regs->sprg6;
980
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100981 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
982 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000983
984 return 0;
985}
986
987int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
988 struct kvm_sregs *sregs)
989{
Alexander Grafe15a1132009-11-30 03:02:02 +0000990 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
991 int i;
992
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000993 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +0000994
995 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
996 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
997 for (i = 0; i < 64; i++) {
998 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
999 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1000 }
1001 } else {
1002 for (i = 0; i < 16; i++) {
1003 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1004 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1005 }
1006 for (i = 0; i < 8; i++) {
1007 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1008 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1009 }
1010 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001011 return 0;
1012}
1013
1014int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1015 struct kvm_sregs *sregs)
1016{
Alexander Grafe15a1132009-11-30 03:02:02 +00001017 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1018 int i;
1019
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001020 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001021
1022 vcpu3s->sdr1 = sregs->u.s.sdr1;
1023 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1024 for (i = 0; i < 64; i++) {
1025 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1026 sregs->u.s.ppc64.slb[i].slbe);
1027 }
1028 } else {
1029 for (i = 0; i < 16; i++) {
1030 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1031 }
1032 for (i = 0; i < 8; i++) {
1033 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1034 (u32)sregs->u.s.ppc32.ibat[i]);
1035 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1036 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1037 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1038 (u32)sregs->u.s.ppc32.dbat[i]);
1039 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1040 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1041 }
1042 }
1043
1044 /* Flush the MMU after messing with the segments */
1045 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001046 return 0;
1047}
1048
1049int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1050{
1051 return -ENOTSUPP;
1052}
1053
1054int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1055{
1056 return -ENOTSUPP;
1057}
1058
1059int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1060 struct kvm_translation *tr)
1061{
1062 return 0;
1063}
1064
1065/*
1066 * Get (and clear) the dirty memory log for a memory slot.
1067 */
1068int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1069 struct kvm_dirty_log *log)
1070{
1071 struct kvm_memory_slot *memslot;
1072 struct kvm_vcpu *vcpu;
1073 ulong ga, ga_end;
1074 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001075 int r;
1076 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001077
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001078 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001079
1080 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1081 if (r)
1082 goto out;
1083
1084 /* If nothing is dirty, don't bother messing with page tables. */
1085 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001086 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001087
1088 ga = memslot->base_gfn << PAGE_SHIFT;
1089 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1090
1091 kvm_for_each_vcpu(n, vcpu, kvm)
1092 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1093
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001094 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001095 memset(memslot->dirty_bitmap, 0, n);
1096 }
1097
1098 r = 0;
1099out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001100 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001101 return r;
1102}
1103
1104int kvmppc_core_check_processor_compat(void)
1105{
1106 return 0;
1107}
1108
1109struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1110{
1111 struct kvmppc_vcpu_book3s *vcpu_book3s;
1112 struct kvm_vcpu *vcpu;
1113 int err;
1114
Alexander Graf032c3402010-02-19 12:24:33 +01001115 vcpu_book3s = vmalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001116 if (!vcpu_book3s) {
1117 err = -ENOMEM;
1118 goto out;
1119 }
Alexander Graf7e821d32010-02-22 16:52:08 +01001120 memset(vcpu_book3s, 0, sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001121
1122 vcpu = &vcpu_book3s->vcpu;
1123 err = kvm_vcpu_init(vcpu, kvm, id);
1124 if (err)
1125 goto free_vcpu;
1126
1127 vcpu->arch.host_retip = kvm_return_point;
1128 vcpu->arch.host_msr = mfmsr();
1129 /* default to book3s_64 (970fx) */
1130 vcpu->arch.pvr = 0x3C0301;
1131 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1132 vcpu_book3s->slb_nr = 64;
1133
1134 /* remember where some real-mode handlers are */
1135 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1136 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1137 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf021ec9c2010-01-08 02:58:06 +01001138 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001139
1140 vcpu->arch.shadow_msr = MSR_USER64;
1141
1142 err = __init_new_context();
1143 if (err < 0)
1144 goto free_vcpu;
1145 vcpu_book3s->context_id = err;
1146
1147 vcpu_book3s->vsid_max = ((vcpu_book3s->context_id + 1) << USER_ESID_BITS) - 1;
1148 vcpu_book3s->vsid_first = vcpu_book3s->context_id << USER_ESID_BITS;
1149 vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
1150
1151 return vcpu;
1152
1153free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001154 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001155out:
1156 return ERR_PTR(err);
1157}
1158
1159void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1160{
1161 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1162
1163 __destroy_context(vcpu_book3s->context_id);
1164 kvm_vcpu_uninit(vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001165 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001166}
1167
1168extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1169int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1170{
1171 int ret;
Alexander Graf180a34d2010-01-15 14:49:11 +01001172 struct thread_struct ext_bkp;
1173 bool save_vec = current->thread.used_vr;
1174 bool save_vsx = current->thread.used_vsr;
1175 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001176
1177 /* No need to go into the guest when all we do is going out */
1178 if (signal_pending(current)) {
1179 kvm_run->exit_reason = KVM_EXIT_INTR;
1180 return -EINTR;
1181 }
1182
Alexander Graf180a34d2010-01-15 14:49:11 +01001183 /* Save FPU state in stack */
1184 if (current->thread.regs->msr & MSR_FP)
1185 giveup_fpu(current);
1186 memcpy(ext_bkp.fpr, current->thread.fpr, sizeof(current->thread.fpr));
1187 ext_bkp.fpscr = current->thread.fpscr;
1188 ext_bkp.fpexc_mode = current->thread.fpexc_mode;
1189
1190#ifdef CONFIG_ALTIVEC
1191 /* Save Altivec state in stack */
1192 if (save_vec) {
1193 if (current->thread.regs->msr & MSR_VEC)
1194 giveup_altivec(current);
1195 memcpy(ext_bkp.vr, current->thread.vr, sizeof(ext_bkp.vr));
1196 ext_bkp.vscr = current->thread.vscr;
1197 ext_bkp.vrsave = current->thread.vrsave;
1198 }
1199 ext_bkp.used_vr = current->thread.used_vr;
1200#endif
1201
1202#ifdef CONFIG_VSX
1203 /* Save VSX state in stack */
1204 if (save_vsx && (current->thread.regs->msr & MSR_VSX))
1205 __giveup_vsx(current);
1206 ext_bkp.used_vsr = current->thread.used_vsr;
1207#endif
1208
1209 /* Remember the MSR with disabled extensions */
1210 ext_msr = current->thread.regs->msr;
1211
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001212 /* XXX we get called with irq disabled - change that! */
1213 local_irq_enable();
1214
Alexander Grafd1bab742010-02-19 11:00:35 +01001215 /* Preload FPU if it's enabled */
1216 if (vcpu->arch.msr & MSR_FP)
1217 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1218
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001219 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1220
1221 local_irq_disable();
1222
Alexander Graf180a34d2010-01-15 14:49:11 +01001223 current->thread.regs->msr = ext_msr;
1224
1225 /* Make sure we save the guest FPU/Altivec/VSX state */
1226 kvmppc_giveup_ext(vcpu, MSR_FP);
1227 kvmppc_giveup_ext(vcpu, MSR_VEC);
1228 kvmppc_giveup_ext(vcpu, MSR_VSX);
1229
1230 /* Restore FPU state from stack */
1231 memcpy(current->thread.fpr, ext_bkp.fpr, sizeof(ext_bkp.fpr));
1232 current->thread.fpscr = ext_bkp.fpscr;
1233 current->thread.fpexc_mode = ext_bkp.fpexc_mode;
1234
1235#ifdef CONFIG_ALTIVEC
1236 /* Restore Altivec state from stack */
1237 if (save_vec && current->thread.used_vr) {
1238 memcpy(current->thread.vr, ext_bkp.vr, sizeof(ext_bkp.vr));
1239 current->thread.vscr = ext_bkp.vscr;
1240 current->thread.vrsave= ext_bkp.vrsave;
1241 }
1242 current->thread.used_vr = ext_bkp.used_vr;
1243#endif
1244
1245#ifdef CONFIG_VSX
1246 current->thread.used_vsr = ext_bkp.used_vsr;
1247#endif
1248
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001249 return ret;
1250}
1251
1252static int kvmppc_book3s_init(void)
1253{
1254 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), THIS_MODULE);
1255}
1256
1257static void kvmppc_book3s_exit(void)
1258{
1259 kvm_exit();
1260}
1261
1262module_init(kvmppc_book3s_init);
1263module_exit(kvmppc_book3s_exit);