blob: ff5a420582574f5776eb285d17d115a8e75065b4 [file] [log] [blame]
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001/*
2 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
3 *
4 * Authors:
5 * Alexander Graf <agraf@suse.de>
6 * Kevin Wolf <mail@kevin-wolf.de>
7 *
8 * Description:
9 * This file is derived from arch/powerpc/kvm/44x.c,
10 * by Hollis Blanchard <hollisb@us.ibm.com>.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License, version 2, as
14 * published by the Free Software Foundation.
15 */
16
17#include <linux/kvm_host.h>
18#include <linux/err.h>
19
20#include <asm/reg.h>
21#include <asm/cputable.h>
22#include <asm/cacheflush.h>
23#include <asm/tlbflush.h>
24#include <asm/uaccess.h>
25#include <asm/io.h>
26#include <asm/kvm_ppc.h>
27#include <asm/kvm_book3s.h>
28#include <asm/mmu_context.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/gfp.h>
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000030#include <linux/sched.h>
31#include <linux/vmalloc.h>
32
33#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
34
35/* #define EXIT_DEBUG */
36/* #define EXIT_DEBUG_SIMPLE */
Alexander Graf180a34d2010-01-15 14:49:11 +010037/* #define DEBUG_EXT */
38
Alexander Grafc8c0b6f2010-02-19 11:00:34 +010039static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
40 ulong msr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000041
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000042struct kvm_stats_debugfs_item debugfs_entries[] = {
43 { "exits", VCPU_STAT(sum_exits) },
44 { "mmio", VCPU_STAT(mmio_exits) },
45 { "sig", VCPU_STAT(signal_exits) },
46 { "sysc", VCPU_STAT(syscall_exits) },
47 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
48 { "dec", VCPU_STAT(dec_exits) },
49 { "ext_intr", VCPU_STAT(ext_intr_exits) },
50 { "queue_intr", VCPU_STAT(queue_intr) },
51 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
52 { "pf_storage", VCPU_STAT(pf_storage) },
53 { "sp_storage", VCPU_STAT(sp_storage) },
54 { "pf_instruc", VCPU_STAT(pf_instruc) },
55 { "sp_instruc", VCPU_STAT(sp_instruc) },
56 { "ld", VCPU_STAT(ld) },
57 { "ld_slow", VCPU_STAT(ld_slow) },
58 { "st", VCPU_STAT(st) },
59 { "st_slow", VCPU_STAT(st_slow) },
60 { NULL }
61};
62
63void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
64{
65}
66
67void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
68{
69}
70
71void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
72{
73 memcpy(get_paca()->kvm_slb, to_book3s(vcpu)->slb_shadow, sizeof(get_paca()->kvm_slb));
Alexander Graf7e57cba2010-01-08 02:58:03 +010074 memcpy(&get_paca()->shadow_vcpu, &to_book3s(vcpu)->shadow_vcpu,
75 sizeof(get_paca()->shadow_vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000076 get_paca()->kvm_slb_max = to_book3s(vcpu)->slb_shadow_max;
77}
78
79void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
80{
81 memcpy(to_book3s(vcpu)->slb_shadow, get_paca()->kvm_slb, sizeof(get_paca()->kvm_slb));
Alexander Graf7e57cba2010-01-08 02:58:03 +010082 memcpy(&to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
83 sizeof(get_paca()->shadow_vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000084 to_book3s(vcpu)->slb_shadow_max = get_paca()->kvm_slb_max;
Alexander Graf180a34d2010-01-15 14:49:11 +010085
86 kvmppc_giveup_ext(vcpu, MSR_FP);
87 kvmppc_giveup_ext(vcpu, MSR_VEC);
88 kvmppc_giveup_ext(vcpu, MSR_VSX);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000089}
90
Alexander Graf0bb1fb72009-12-21 20:21:25 +010091#if defined(EXIT_DEBUG)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000092static u32 kvmppc_get_dec(struct kvm_vcpu *vcpu)
93{
94 u64 jd = mftb() - vcpu->arch.dec_jiffies;
95 return vcpu->arch.dec - jd;
96}
97#endif
98
Alexander Grafa76f8492010-01-15 14:49:14 +010099static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
100{
101 vcpu->arch.shadow_msr = vcpu->arch.msr;
102 /* Guest MSR values */
103 vcpu->arch.shadow_msr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE |
104 MSR_BE | MSR_DE;
105 /* Process MSR values */
106 vcpu->arch.shadow_msr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR |
107 MSR_EE;
108 /* External providers the guest reserved */
109 vcpu->arch.shadow_msr |= (vcpu->arch.msr & vcpu->arch.guest_owned_ext);
110 /* 64-bit Process MSR values */
111#ifdef CONFIG_PPC_BOOK3S_64
112 vcpu->arch.shadow_msr |= MSR_ISF | MSR_HV;
113#endif
114}
115
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000116void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
117{
118 ulong old_msr = vcpu->arch.msr;
119
120#ifdef EXIT_DEBUG
121 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
122#endif
Alexander Grafa76f8492010-01-15 14:49:14 +0100123
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000124 msr &= to_book3s(vcpu)->msr_mask;
125 vcpu->arch.msr = msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100126 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000127
128 if (msr & (MSR_WE|MSR_POW)) {
129 if (!vcpu->arch.pending_exceptions) {
130 kvm_vcpu_block(vcpu);
131 vcpu->stat.halt_wakeup++;
132 }
133 }
134
135 if (((vcpu->arch.msr & (MSR_IR|MSR_DR)) != (old_msr & (MSR_IR|MSR_DR))) ||
136 (vcpu->arch.msr & MSR_PR) != (old_msr & MSR_PR)) {
Alexander Graf3eeafd72010-03-24 21:48:17 +0100137 bool dr = (vcpu->arch.msr & MSR_DR) ? true : false;
138 bool ir = (vcpu->arch.msr & MSR_IR) ? true : false;
139
140 /* Flush split mode PTEs */
141 if (dr != ir)
142 kvmppc_mmu_pte_vflush(vcpu, VSID_SPLIT_MASK,
143 VSID_SPLIT_MASK);
144
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000145 kvmppc_mmu_flush_segments(vcpu);
146 kvmppc_mmu_map_segment(vcpu, vcpu->arch.pc);
147 }
Alexander Grafd1bab742010-02-19 11:00:35 +0100148
149 /* Preload FPU if it's enabled */
150 if (vcpu->arch.msr & MSR_FP)
151 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000152}
153
154void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
155{
156 vcpu->arch.srr0 = vcpu->arch.pc;
157 vcpu->arch.srr1 = vcpu->arch.msr | flags;
158 vcpu->arch.pc = to_book3s(vcpu)->hior + vec;
159 vcpu->arch.mmu.reset_msr(vcpu);
160}
161
Alexander Graf583617b2009-12-21 20:21:23 +0100162static int kvmppc_book3s_vec2irqprio(unsigned int vec)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000163{
164 unsigned int prio;
165
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000166 switch (vec) {
167 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
168 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
169 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
170 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
171 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
172 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
173 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
174 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
175 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
176 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
177 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
178 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
179 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
180 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
181 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
182 default: prio = BOOK3S_IRQPRIO_MAX; break;
183 }
184
Alexander Graf583617b2009-12-21 20:21:23 +0100185 return prio;
186}
187
Alexander Graf7706664d2009-12-21 20:21:24 +0100188static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
189 unsigned int vec)
190{
191 clear_bit(kvmppc_book3s_vec2irqprio(vec),
192 &vcpu->arch.pending_exceptions);
193}
194
Alexander Graf583617b2009-12-21 20:21:23 +0100195void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
196{
197 vcpu->stat.queue_intr++;
198
199 set_bit(kvmppc_book3s_vec2irqprio(vec),
200 &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000201#ifdef EXIT_DEBUG
202 printk(KERN_INFO "Queueing interrupt %x\n", vec);
203#endif
204}
205
206
Alexander Graf25a8a022010-01-08 02:58:07 +0100207void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000208{
Alexander Graf25a8a022010-01-08 02:58:07 +0100209 to_book3s(vcpu)->prog_flags = flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000210 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM);
211}
212
213void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
214{
215 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
216}
217
218int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
219{
220 return test_bit(BOOK3S_INTERRUPT_DECREMENTER >> 7, &vcpu->arch.pending_exceptions);
221}
222
Alexander Graf7706664d2009-12-21 20:21:24 +0100223void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
224{
225 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
226}
227
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000228void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
229 struct kvm_interrupt *irq)
230{
231 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
232}
233
234int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
235{
236 int deliver = 1;
237 int vec = 0;
Alexander Graf25a8a022010-01-08 02:58:07 +0100238 ulong flags = 0ULL;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000239
240 switch (priority) {
241 case BOOK3S_IRQPRIO_DECREMENTER:
242 deliver = vcpu->arch.msr & MSR_EE;
243 vec = BOOK3S_INTERRUPT_DECREMENTER;
244 break;
245 case BOOK3S_IRQPRIO_EXTERNAL:
246 deliver = vcpu->arch.msr & MSR_EE;
247 vec = BOOK3S_INTERRUPT_EXTERNAL;
248 break;
249 case BOOK3S_IRQPRIO_SYSTEM_RESET:
250 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
251 break;
252 case BOOK3S_IRQPRIO_MACHINE_CHECK:
253 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
254 break;
255 case BOOK3S_IRQPRIO_DATA_STORAGE:
256 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
257 break;
258 case BOOK3S_IRQPRIO_INST_STORAGE:
259 vec = BOOK3S_INTERRUPT_INST_STORAGE;
260 break;
261 case BOOK3S_IRQPRIO_DATA_SEGMENT:
262 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
263 break;
264 case BOOK3S_IRQPRIO_INST_SEGMENT:
265 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
266 break;
267 case BOOK3S_IRQPRIO_ALIGNMENT:
268 vec = BOOK3S_INTERRUPT_ALIGNMENT;
269 break;
270 case BOOK3S_IRQPRIO_PROGRAM:
271 vec = BOOK3S_INTERRUPT_PROGRAM;
Alexander Graf25a8a022010-01-08 02:58:07 +0100272 flags = to_book3s(vcpu)->prog_flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000273 break;
274 case BOOK3S_IRQPRIO_VSX:
275 vec = BOOK3S_INTERRUPT_VSX;
276 break;
277 case BOOK3S_IRQPRIO_ALTIVEC:
278 vec = BOOK3S_INTERRUPT_ALTIVEC;
279 break;
280 case BOOK3S_IRQPRIO_FP_UNAVAIL:
281 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
282 break;
283 case BOOK3S_IRQPRIO_SYSCALL:
284 vec = BOOK3S_INTERRUPT_SYSCALL;
285 break;
286 case BOOK3S_IRQPRIO_DEBUG:
287 vec = BOOK3S_INTERRUPT_TRACE;
288 break;
289 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
290 vec = BOOK3S_INTERRUPT_PERFMON;
291 break;
292 default:
293 deliver = 0;
294 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
295 break;
296 }
297
298#if 0
299 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
300#endif
301
302 if (deliver)
Alexander Graf25a8a022010-01-08 02:58:07 +0100303 kvmppc_inject_interrupt(vcpu, vec, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000304
305 return deliver;
306}
307
308void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
309{
310 unsigned long *pending = &vcpu->arch.pending_exceptions;
311 unsigned int priority;
312
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000313#ifdef EXIT_DEBUG
314 if (vcpu->arch.pending_exceptions)
315 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
316#endif
317 priority = __ffs(*pending);
318 while (priority <= (sizeof(unsigned int) * 8)) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100319 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
320 (priority != BOOK3S_IRQPRIO_DECREMENTER)) {
321 /* DEC interrupts get cleared by mtdec */
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000322 clear_bit(priority, &vcpu->arch.pending_exceptions);
323 break;
324 }
325
326 priority = find_next_bit(pending,
327 BITS_PER_BYTE * sizeof(*pending),
328 priority + 1);
329 }
330}
331
332void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
333{
Alexander Grafe15a1132009-11-30 03:02:02 +0000334 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000335 vcpu->arch.pvr = pvr;
336 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
337 kvmppc_mmu_book3s_64_init(vcpu);
338 to_book3s(vcpu)->hior = 0xfff00000;
339 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
340 } else {
341 kvmppc_mmu_book3s_32_init(vcpu);
342 to_book3s(vcpu)->hior = 0;
343 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
344 }
345
346 /* If we are in hypervisor level on 970, we can tell the CPU to
347 * treat DCBZ as 32 bytes store */
348 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
349 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
350 !strcmp(cur_cpu_spec->platform, "ppc970"))
351 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
352
353}
354
355/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
356 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
357 * emulate 32 bytes dcbz length.
358 *
359 * The Book3s_64 inventors also realized this case and implemented a special bit
360 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
361 *
362 * My approach here is to patch the dcbz instruction on executing pages.
363 */
364static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
365{
366 bool touched = false;
367 hva_t hpage;
368 u32 *page;
369 int i;
370
371 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
372 if (kvm_is_error_hva(hpage))
373 return;
374
375 hpage |= pte->raddr & ~PAGE_MASK;
376 hpage &= ~0xFFFULL;
377
378 page = vmalloc(HW_PAGE_SIZE);
379
380 if (copy_from_user(page, (void __user *)hpage, HW_PAGE_SIZE))
381 goto out;
382
383 for (i=0; i < HW_PAGE_SIZE / 4; i++)
384 if ((page[i] & 0xff0007ff) == INS_DCBZ) {
385 page[i] &= 0xfffffff7; // reserved instruction, so we trap
386 touched = true;
387 }
388
389 if (touched)
390 copy_to_user((void __user *)hpage, page, HW_PAGE_SIZE);
391
392out:
393 vfree(page);
394}
395
396static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
397 struct kvmppc_pte *pte)
398{
399 int relocated = (vcpu->arch.msr & (data ? MSR_DR : MSR_IR));
400 int r;
401
402 if (relocated) {
403 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
404 } else {
405 pte->eaddr = eaddr;
406 pte->raddr = eaddr & 0xffffffff;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100407 pte->vpage = VSID_REAL | eaddr >> 12;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000408 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;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100516 bool dr = (vcpu->arch.msr & MSR_DR) ? true : false;
517 bool ir = (vcpu->arch.msr & MSR_IR) ? true : false;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000518
Alexander Graf3eeafd72010-03-24 21:48:17 +0100519 relocated = data ? dr : ir;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000520
521 /* Resolve real address if translation turned on */
522 if (relocated) {
523 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
524 } else {
525 pte.may_execute = true;
526 pte.may_read = true;
527 pte.may_write = true;
528 pte.raddr = eaddr & 0xffffffff;
529 pte.eaddr = eaddr;
530 pte.vpage = eaddr >> 12;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100531 }
532
533 switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
534 case 0:
535 pte.vpage |= VSID_REAL;
536 break;
537 case MSR_DR:
538 pte.vpage |= VSID_REAL_DR;
539 break;
540 case MSR_IR:
541 pte.vpage |= VSID_REAL_IR;
542 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000543 }
544
545 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
546 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
547 /*
548 * If we do the dcbz hack, we have to NX on every execution,
549 * so we can patch the executing code. This renders our guest
550 * NX-less.
551 */
552 pte.may_execute = !data;
553 }
554
555 if (page_found == -ENOENT) {
556 /* Page not found in guest PTE entries */
557 vcpu->arch.dear = vcpu->arch.fault_dear;
558 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr;
Alexander Graff7adbba2010-01-15 14:49:13 +0100559 vcpu->arch.msr |= (vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000560 kvmppc_book3s_queue_irqprio(vcpu, vec);
561 } else if (page_found == -EPERM) {
562 /* Storage protection */
563 vcpu->arch.dear = vcpu->arch.fault_dear;
564 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr & ~DSISR_NOHPTE;
565 to_book3s(vcpu)->dsisr |= DSISR_PROTFAULT;
Alexander Graff7adbba2010-01-15 14:49:13 +0100566 vcpu->arch.msr |= (vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000567 kvmppc_book3s_queue_irqprio(vcpu, vec);
568 } else if (page_found == -EINVAL) {
569 /* Page not found in guest SLB */
570 vcpu->arch.dear = vcpu->arch.fault_dear;
571 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
572 } else if (!is_mmio &&
573 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
574 /* The guest's PTE is not mapped yet. Map on the host */
575 kvmppc_mmu_map_page(vcpu, &pte);
576 if (data)
577 vcpu->stat.sp_storage++;
578 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
579 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
580 kvmppc_patch_dcbz(vcpu, &pte);
581 } else {
582 /* MMIO */
583 vcpu->stat.mmio_exits++;
584 vcpu->arch.paddr_accessed = pte.raddr;
585 r = kvmppc_emulate_mmio(run, vcpu);
586 if ( r == RESUME_HOST_NV )
587 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000588 }
589
590 return r;
591}
592
Alexander Graf180a34d2010-01-15 14:49:11 +0100593static inline int get_fpr_index(int i)
594{
595#ifdef CONFIG_VSX
596 i *= 2;
597#endif
598 return i;
599}
600
601/* Give up external provider (FPU, Altivec, VSX) */
Alexander Grafaba3bd72010-02-19 11:00:39 +0100602void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
Alexander Graf180a34d2010-01-15 14:49:11 +0100603{
604 struct thread_struct *t = &current->thread;
605 u64 *vcpu_fpr = vcpu->arch.fpr;
606 u64 *vcpu_vsx = vcpu->arch.vsr;
607 u64 *thread_fpr = (u64*)t->fpr;
608 int i;
609
610 if (!(vcpu->arch.guest_owned_ext & msr))
611 return;
612
613#ifdef DEBUG_EXT
614 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
615#endif
616
617 switch (msr) {
618 case MSR_FP:
619 giveup_fpu(current);
620 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
621 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
622
623 vcpu->arch.fpscr = t->fpscr.val;
624 break;
625 case MSR_VEC:
626#ifdef CONFIG_ALTIVEC
627 giveup_altivec(current);
628 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
629 vcpu->arch.vscr = t->vscr;
630#endif
631 break;
632 case MSR_VSX:
633#ifdef CONFIG_VSX
634 __giveup_vsx(current);
635 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
636 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
637#endif
638 break;
639 default:
640 BUG();
641 }
642
643 vcpu->arch.guest_owned_ext &= ~msr;
644 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100645 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100646}
647
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100648static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
649{
650 ulong srr0 = vcpu->arch.pc;
651 int ret;
652
653 /* Need to do paired single emulation? */
654 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
655 return EMULATE_DONE;
656
657 /* Read out the instruction */
658 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &vcpu->arch.last_inst, false);
659 if (ret == -ENOENT) {
660 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 33, 33, 1);
661 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 34, 36, 0);
662 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 42, 47, 0);
663 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
664 } else if(ret == EMULATE_DONE) {
665 /* Need to emulate */
666 return EMULATE_FAIL;
667 }
668
669 return EMULATE_AGAIN;
670}
671
Alexander Graf180a34d2010-01-15 14:49:11 +0100672/* Handle external providers (FPU, Altivec, VSX) */
673static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
674 ulong msr)
675{
676 struct thread_struct *t = &current->thread;
677 u64 *vcpu_fpr = vcpu->arch.fpr;
678 u64 *vcpu_vsx = vcpu->arch.vsr;
679 u64 *thread_fpr = (u64*)t->fpr;
680 int i;
681
Alexander Graf3c402a72010-02-19 11:00:32 +0100682 /* When we have paired singles, we emulate in software */
683 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
684 return RESUME_GUEST;
685
Alexander Graf180a34d2010-01-15 14:49:11 +0100686 if (!(vcpu->arch.msr & msr)) {
687 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
688 return RESUME_GUEST;
689 }
690
691#ifdef DEBUG_EXT
692 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
693#endif
694
695 current->thread.regs->msr |= msr;
696
697 switch (msr) {
698 case MSR_FP:
699 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
700 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
701
702 t->fpscr.val = vcpu->arch.fpscr;
703 t->fpexc_mode = 0;
704 kvmppc_load_up_fpu();
705 break;
706 case MSR_VEC:
707#ifdef CONFIG_ALTIVEC
708 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
709 t->vscr = vcpu->arch.vscr;
710 t->vrsave = -1;
711 kvmppc_load_up_altivec();
712#endif
713 break;
714 case MSR_VSX:
715#ifdef CONFIG_VSX
716 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
717 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
718 kvmppc_load_up_vsx();
719#endif
720 break;
721 default:
722 BUG();
723 }
724
725 vcpu->arch.guest_owned_ext |= msr;
726
Alexander Grafa76f8492010-01-15 14:49:14 +0100727 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100728
729 return RESUME_GUEST;
730}
731
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000732int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
733 unsigned int exit_nr)
734{
735 int r = RESUME_HOST;
736
737 vcpu->stat.sum_exits++;
738
739 run->exit_reason = KVM_EXIT_UNKNOWN;
740 run->ready_for_interrupt_injection = 1;
741#ifdef EXIT_DEBUG
742 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | dec=0x%x | msr=0x%lx\n",
743 exit_nr, vcpu->arch.pc, vcpu->arch.fault_dear,
744 kvmppc_get_dec(vcpu), vcpu->arch.msr);
745#elif defined (EXIT_DEBUG_SIMPLE)
746 if ((exit_nr != 0x900) && (exit_nr != 0x500))
747 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | msr=0x%lx\n",
748 exit_nr, vcpu->arch.pc, vcpu->arch.fault_dear,
749 vcpu->arch.msr);
750#endif
751 kvm_resched(vcpu);
752 switch (exit_nr) {
753 case BOOK3S_INTERRUPT_INST_STORAGE:
754 vcpu->stat.pf_instruc++;
755 /* only care about PTEG not found errors, but leave NX alone */
Alexander Graff7adbba2010-01-15 14:49:13 +0100756 if (vcpu->arch.shadow_srr1 & 0x40000000) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000757 r = kvmppc_handle_pagefault(run, vcpu, vcpu->arch.pc, exit_nr);
758 vcpu->stat.sp_instruc++;
759 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
760 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
761 /*
762 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
763 * so we can't use the NX bit inside the guest. Let's cross our fingers,
764 * that no guest that needs the dcbz hack does NX.
765 */
766 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.pc, ~0xFFFULL);
767 } else {
Alexander Graff7adbba2010-01-15 14:49:13 +0100768 vcpu->arch.msr |= vcpu->arch.shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000769 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
770 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.pc, ~0xFFFULL);
771 r = RESUME_GUEST;
772 }
773 break;
774 case BOOK3S_INTERRUPT_DATA_STORAGE:
775 vcpu->stat.pf_storage++;
776 /* The only case we need to handle is missing shadow PTEs */
777 if (vcpu->arch.fault_dsisr & DSISR_NOHPTE) {
778 r = kvmppc_handle_pagefault(run, vcpu, vcpu->arch.fault_dear, exit_nr);
779 } else {
780 vcpu->arch.dear = vcpu->arch.fault_dear;
781 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr;
782 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
783 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.dear, ~0xFFFULL);
784 r = RESUME_GUEST;
785 }
786 break;
787 case BOOK3S_INTERRUPT_DATA_SEGMENT:
788 if (kvmppc_mmu_map_segment(vcpu, vcpu->arch.fault_dear) < 0) {
789 vcpu->arch.dear = vcpu->arch.fault_dear;
790 kvmppc_book3s_queue_irqprio(vcpu,
791 BOOK3S_INTERRUPT_DATA_SEGMENT);
792 }
793 r = RESUME_GUEST;
794 break;
795 case BOOK3S_INTERRUPT_INST_SEGMENT:
796 if (kvmppc_mmu_map_segment(vcpu, vcpu->arch.pc) < 0) {
797 kvmppc_book3s_queue_irqprio(vcpu,
798 BOOK3S_INTERRUPT_INST_SEGMENT);
799 }
800 r = RESUME_GUEST;
801 break;
802 /* We're good on these - the host merely wanted to get our attention */
803 case BOOK3S_INTERRUPT_DECREMENTER:
804 vcpu->stat.dec_exits++;
805 r = RESUME_GUEST;
806 break;
807 case BOOK3S_INTERRUPT_EXTERNAL:
808 vcpu->stat.ext_intr_exits++;
809 r = RESUME_GUEST;
810 break;
811 case BOOK3S_INTERRUPT_PROGRAM:
812 {
813 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100814 ulong flags;
815
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100816program_interrupt:
Alexander Graff7adbba2010-01-15 14:49:13 +0100817 flags = vcpu->arch.shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000818
819 if (vcpu->arch.msr & MSR_PR) {
820#ifdef EXIT_DEBUG
821 printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", vcpu->arch.pc, vcpu->arch.last_inst);
822#endif
823 if ((vcpu->arch.last_inst & 0xff0007ff) !=
824 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100825 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000826 r = RESUME_GUEST;
827 break;
828 }
829 }
830
831 vcpu->stat.emulated_inst_exits++;
832 er = kvmppc_emulate_instruction(run, vcpu);
833 switch (er) {
834 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100835 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000836 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100837 case EMULATE_AGAIN:
838 r = RESUME_GUEST;
839 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000840 case EMULATE_FAIL:
841 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
842 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100843 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000844 r = RESUME_GUEST;
845 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +0100846 case EMULATE_DO_MMIO:
847 run->exit_reason = KVM_EXIT_MMIO;
848 r = RESUME_HOST_NV;
849 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000850 default:
851 BUG();
852 }
853 break;
854 }
855 case BOOK3S_INTERRUPT_SYSCALL:
856#ifdef EXIT_DEBUG
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100857 printk(KERN_INFO "Syscall Nr %d\n", (int)kvmppc_get_gpr(vcpu, 0));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000858#endif
859 vcpu->stat.syscall_exits++;
860 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
861 r = RESUME_GUEST;
862 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000863 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000864 case BOOK3S_INTERRUPT_ALTIVEC:
865 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100866 {
867 int ext_msr = 0;
868
869 switch (exit_nr) {
870 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
871 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
872 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
873 }
874
875 switch (kvmppc_check_ext(vcpu, exit_nr)) {
876 case EMULATE_DONE:
877 /* everything ok - let's enable the ext */
878 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
879 break;
880 case EMULATE_FAIL:
881 /* we need to emulate this instruction */
882 goto program_interrupt;
883 break;
884 default:
885 /* nothing to worry about - go again */
886 break;
887 }
Alexander Graf180a34d2010-01-15 14:49:11 +0100888 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100889 }
Alexander Graf180a34d2010-01-15 14:49:11 +0100890 case BOOK3S_INTERRUPT_MACHINE_CHECK:
891 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000892 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
893 r = RESUME_GUEST;
894 break;
895 default:
896 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +0100897 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
898 exit_nr, vcpu->arch.pc, vcpu->arch.shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000899 r = RESUME_HOST;
900 BUG();
901 break;
902 }
903
904
905 if (!(r & RESUME_HOST)) {
906 /* To avoid clobbering exit_reason, only check for signals if
907 * we aren't already exiting to userspace for some other
908 * reason. */
909 if (signal_pending(current)) {
910#ifdef EXIT_DEBUG
911 printk(KERN_EMERG "KVM: Going back to host\n");
912#endif
913 vcpu->stat.signal_exits++;
914 run->exit_reason = KVM_EXIT_INTR;
915 r = -EINTR;
916 } else {
917 /* In case an interrupt came in that was triggered
918 * from userspace (like DEC), we need to check what
919 * to inject now! */
920 kvmppc_core_deliver_interrupts(vcpu);
921 }
922 }
923
924#ifdef EXIT_DEBUG
925 printk(KERN_EMERG "KVM exit: vcpu=0x%p pc=0x%lx r=0x%x\n", vcpu, vcpu->arch.pc, r);
926#endif
927
928 return r;
929}
930
931int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
932{
933 return 0;
934}
935
936int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
937{
938 int i;
939
940 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100941 regs->cr = kvmppc_get_cr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000942 regs->ctr = vcpu->arch.ctr;
943 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100944 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000945 regs->msr = vcpu->arch.msr;
946 regs->srr0 = vcpu->arch.srr0;
947 regs->srr1 = vcpu->arch.srr1;
948 regs->pid = vcpu->arch.pid;
949 regs->sprg0 = vcpu->arch.sprg0;
950 regs->sprg1 = vcpu->arch.sprg1;
951 regs->sprg2 = vcpu->arch.sprg2;
952 regs->sprg3 = vcpu->arch.sprg3;
953 regs->sprg5 = vcpu->arch.sprg4;
954 regs->sprg6 = vcpu->arch.sprg5;
955 regs->sprg7 = vcpu->arch.sprg6;
956
957 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100958 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000959
960 return 0;
961}
962
963int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
964{
965 int i;
966
967 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100968 kvmppc_set_cr(vcpu, regs->cr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000969 vcpu->arch.ctr = regs->ctr;
970 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100971 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000972 kvmppc_set_msr(vcpu, regs->msr);
973 vcpu->arch.srr0 = regs->srr0;
974 vcpu->arch.srr1 = regs->srr1;
975 vcpu->arch.sprg0 = regs->sprg0;
976 vcpu->arch.sprg1 = regs->sprg1;
977 vcpu->arch.sprg2 = regs->sprg2;
978 vcpu->arch.sprg3 = regs->sprg3;
979 vcpu->arch.sprg5 = regs->sprg4;
980 vcpu->arch.sprg6 = regs->sprg5;
981 vcpu->arch.sprg7 = regs->sprg6;
982
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100983 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
984 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000985
986 return 0;
987}
988
989int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
990 struct kvm_sregs *sregs)
991{
Alexander Grafe15a1132009-11-30 03:02:02 +0000992 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
993 int i;
994
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000995 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +0000996
997 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
998 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
999 for (i = 0; i < 64; i++) {
1000 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
1001 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1002 }
1003 } else {
1004 for (i = 0; i < 16; i++) {
1005 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1006 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1007 }
1008 for (i = 0; i < 8; i++) {
1009 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1010 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1011 }
1012 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001013 return 0;
1014}
1015
1016int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1017 struct kvm_sregs *sregs)
1018{
Alexander Grafe15a1132009-11-30 03:02:02 +00001019 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1020 int i;
1021
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001022 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001023
1024 vcpu3s->sdr1 = sregs->u.s.sdr1;
1025 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1026 for (i = 0; i < 64; i++) {
1027 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1028 sregs->u.s.ppc64.slb[i].slbe);
1029 }
1030 } else {
1031 for (i = 0; i < 16; i++) {
1032 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1033 }
1034 for (i = 0; i < 8; i++) {
1035 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1036 (u32)sregs->u.s.ppc32.ibat[i]);
1037 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1038 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1039 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1040 (u32)sregs->u.s.ppc32.dbat[i]);
1041 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1042 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1043 }
1044 }
1045
1046 /* Flush the MMU after messing with the segments */
1047 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001048 return 0;
1049}
1050
1051int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1052{
1053 return -ENOTSUPP;
1054}
1055
1056int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1057{
1058 return -ENOTSUPP;
1059}
1060
1061int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1062 struct kvm_translation *tr)
1063{
1064 return 0;
1065}
1066
1067/*
1068 * Get (and clear) the dirty memory log for a memory slot.
1069 */
1070int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1071 struct kvm_dirty_log *log)
1072{
1073 struct kvm_memory_slot *memslot;
1074 struct kvm_vcpu *vcpu;
1075 ulong ga, ga_end;
1076 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001077 int r;
1078 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001079
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001080 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001081
1082 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1083 if (r)
1084 goto out;
1085
1086 /* If nothing is dirty, don't bother messing with page tables. */
1087 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001088 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001089
1090 ga = memslot->base_gfn << PAGE_SHIFT;
1091 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1092
1093 kvm_for_each_vcpu(n, vcpu, kvm)
1094 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1095
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001096 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001097 memset(memslot->dirty_bitmap, 0, n);
1098 }
1099
1100 r = 0;
1101out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001102 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001103 return r;
1104}
1105
1106int kvmppc_core_check_processor_compat(void)
1107{
1108 return 0;
1109}
1110
1111struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1112{
1113 struct kvmppc_vcpu_book3s *vcpu_book3s;
1114 struct kvm_vcpu *vcpu;
1115 int err;
1116
Alexander Graf032c3402010-02-19 12:24:33 +01001117 vcpu_book3s = vmalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001118 if (!vcpu_book3s) {
1119 err = -ENOMEM;
1120 goto out;
1121 }
Alexander Graf7e821d32010-02-22 16:52:08 +01001122 memset(vcpu_book3s, 0, sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001123
1124 vcpu = &vcpu_book3s->vcpu;
1125 err = kvm_vcpu_init(vcpu, kvm, id);
1126 if (err)
1127 goto free_vcpu;
1128
1129 vcpu->arch.host_retip = kvm_return_point;
1130 vcpu->arch.host_msr = mfmsr();
1131 /* default to book3s_64 (970fx) */
1132 vcpu->arch.pvr = 0x3C0301;
1133 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1134 vcpu_book3s->slb_nr = 64;
1135
1136 /* remember where some real-mode handlers are */
1137 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1138 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1139 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf021ec9c2010-01-08 02:58:06 +01001140 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001141
1142 vcpu->arch.shadow_msr = MSR_USER64;
1143
1144 err = __init_new_context();
1145 if (err < 0)
1146 goto free_vcpu;
1147 vcpu_book3s->context_id = err;
1148
1149 vcpu_book3s->vsid_max = ((vcpu_book3s->context_id + 1) << USER_ESID_BITS) - 1;
1150 vcpu_book3s->vsid_first = vcpu_book3s->context_id << USER_ESID_BITS;
1151 vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
1152
1153 return vcpu;
1154
1155free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001156 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001157out:
1158 return ERR_PTR(err);
1159}
1160
1161void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1162{
1163 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1164
1165 __destroy_context(vcpu_book3s->context_id);
1166 kvm_vcpu_uninit(vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001167 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001168}
1169
1170extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1171int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1172{
1173 int ret;
Alexander Graf180a34d2010-01-15 14:49:11 +01001174 struct thread_struct ext_bkp;
1175 bool save_vec = current->thread.used_vr;
1176 bool save_vsx = current->thread.used_vsr;
1177 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001178
1179 /* No need to go into the guest when all we do is going out */
1180 if (signal_pending(current)) {
1181 kvm_run->exit_reason = KVM_EXIT_INTR;
1182 return -EINTR;
1183 }
1184
Alexander Graf180a34d2010-01-15 14:49:11 +01001185 /* Save FPU state in stack */
1186 if (current->thread.regs->msr & MSR_FP)
1187 giveup_fpu(current);
1188 memcpy(ext_bkp.fpr, current->thread.fpr, sizeof(current->thread.fpr));
1189 ext_bkp.fpscr = current->thread.fpscr;
1190 ext_bkp.fpexc_mode = current->thread.fpexc_mode;
1191
1192#ifdef CONFIG_ALTIVEC
1193 /* Save Altivec state in stack */
1194 if (save_vec) {
1195 if (current->thread.regs->msr & MSR_VEC)
1196 giveup_altivec(current);
1197 memcpy(ext_bkp.vr, current->thread.vr, sizeof(ext_bkp.vr));
1198 ext_bkp.vscr = current->thread.vscr;
1199 ext_bkp.vrsave = current->thread.vrsave;
1200 }
1201 ext_bkp.used_vr = current->thread.used_vr;
1202#endif
1203
1204#ifdef CONFIG_VSX
1205 /* Save VSX state in stack */
1206 if (save_vsx && (current->thread.regs->msr & MSR_VSX))
1207 __giveup_vsx(current);
1208 ext_bkp.used_vsr = current->thread.used_vsr;
1209#endif
1210
1211 /* Remember the MSR with disabled extensions */
1212 ext_msr = current->thread.regs->msr;
1213
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001214 /* XXX we get called with irq disabled - change that! */
1215 local_irq_enable();
1216
Alexander Grafd1bab742010-02-19 11:00:35 +01001217 /* Preload FPU if it's enabled */
1218 if (vcpu->arch.msr & MSR_FP)
1219 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1220
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001221 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1222
1223 local_irq_disable();
1224
Alexander Graf180a34d2010-01-15 14:49:11 +01001225 current->thread.regs->msr = ext_msr;
1226
1227 /* Make sure we save the guest FPU/Altivec/VSX state */
1228 kvmppc_giveup_ext(vcpu, MSR_FP);
1229 kvmppc_giveup_ext(vcpu, MSR_VEC);
1230 kvmppc_giveup_ext(vcpu, MSR_VSX);
1231
1232 /* Restore FPU state from stack */
1233 memcpy(current->thread.fpr, ext_bkp.fpr, sizeof(ext_bkp.fpr));
1234 current->thread.fpscr = ext_bkp.fpscr;
1235 current->thread.fpexc_mode = ext_bkp.fpexc_mode;
1236
1237#ifdef CONFIG_ALTIVEC
1238 /* Restore Altivec state from stack */
1239 if (save_vec && current->thread.used_vr) {
1240 memcpy(current->thread.vr, ext_bkp.vr, sizeof(ext_bkp.vr));
1241 current->thread.vscr = ext_bkp.vscr;
1242 current->thread.vrsave= ext_bkp.vrsave;
1243 }
1244 current->thread.used_vr = ext_bkp.used_vr;
1245#endif
1246
1247#ifdef CONFIG_VSX
1248 current->thread.used_vsr = ext_bkp.used_vsr;
1249#endif
1250
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001251 return ret;
1252}
1253
1254static int kvmppc_book3s_init(void)
1255{
1256 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), THIS_MODULE);
1257}
1258
1259static void kvmppc_book3s_exit(void)
1260{
1261 kvm_exit();
1262}
1263
1264module_init(kvmppc_book3s_init);
1265module_exit(kvmppc_book3s_exit);