blob: ed57584963720fde842d155f3d33c4dfd64009bf [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>
Alexander Graf9fb244a2010-03-24 21:48:32 +010032#include <linux/highmem.h>
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000033
34#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
35
36/* #define EXIT_DEBUG */
37/* #define EXIT_DEBUG_SIMPLE */
Alexander Graf180a34d2010-01-15 14:49:11 +010038/* #define DEBUG_EXT */
39
Alexander Grafc8c0b6f2010-02-19 11:00:34 +010040static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
41 ulong msr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000042
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000043struct kvm_stats_debugfs_item debugfs_entries[] = {
44 { "exits", VCPU_STAT(sum_exits) },
45 { "mmio", VCPU_STAT(mmio_exits) },
46 { "sig", VCPU_STAT(signal_exits) },
47 { "sysc", VCPU_STAT(syscall_exits) },
48 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
49 { "dec", VCPU_STAT(dec_exits) },
50 { "ext_intr", VCPU_STAT(ext_intr_exits) },
51 { "queue_intr", VCPU_STAT(queue_intr) },
52 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
53 { "pf_storage", VCPU_STAT(pf_storage) },
54 { "sp_storage", VCPU_STAT(sp_storage) },
55 { "pf_instruc", VCPU_STAT(pf_instruc) },
56 { "sp_instruc", VCPU_STAT(sp_instruc) },
57 { "ld", VCPU_STAT(ld) },
58 { "ld_slow", VCPU_STAT(ld_slow) },
59 { "st", VCPU_STAT(st) },
60 { "st_slow", VCPU_STAT(st_slow) },
61 { NULL }
62};
63
64void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
65{
66}
67
68void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
69{
70}
71
72void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
73{
74 memcpy(get_paca()->kvm_slb, to_book3s(vcpu)->slb_shadow, sizeof(get_paca()->kvm_slb));
Alexander Graf7e57cba2010-01-08 02:58:03 +010075 memcpy(&get_paca()->shadow_vcpu, &to_book3s(vcpu)->shadow_vcpu,
76 sizeof(get_paca()->shadow_vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000077 get_paca()->kvm_slb_max = to_book3s(vcpu)->slb_shadow_max;
78}
79
80void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
81{
82 memcpy(to_book3s(vcpu)->slb_shadow, get_paca()->kvm_slb, sizeof(get_paca()->kvm_slb));
Alexander Graf7e57cba2010-01-08 02:58:03 +010083 memcpy(&to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
84 sizeof(get_paca()->shadow_vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000085 to_book3s(vcpu)->slb_shadow_max = get_paca()->kvm_slb_max;
Alexander Graf180a34d2010-01-15 14:49:11 +010086
87 kvmppc_giveup_ext(vcpu, MSR_FP);
88 kvmppc_giveup_ext(vcpu, MSR_VEC);
89 kvmppc_giveup_ext(vcpu, MSR_VSX);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000090}
91
Alexander Graf0bb1fb72009-12-21 20:21:25 +010092#if defined(EXIT_DEBUG)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000093static u32 kvmppc_get_dec(struct kvm_vcpu *vcpu)
94{
95 u64 jd = mftb() - vcpu->arch.dec_jiffies;
96 return vcpu->arch.dec - jd;
97}
98#endif
99
Alexander Grafa76f8492010-01-15 14:49:14 +0100100static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
101{
102 vcpu->arch.shadow_msr = vcpu->arch.msr;
103 /* Guest MSR values */
104 vcpu->arch.shadow_msr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE |
105 MSR_BE | MSR_DE;
106 /* Process MSR values */
107 vcpu->arch.shadow_msr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR |
108 MSR_EE;
109 /* External providers the guest reserved */
110 vcpu->arch.shadow_msr |= (vcpu->arch.msr & vcpu->arch.guest_owned_ext);
111 /* 64-bit Process MSR values */
112#ifdef CONFIG_PPC_BOOK3S_64
113 vcpu->arch.shadow_msr |= MSR_ISF | MSR_HV;
114#endif
115}
116
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000117void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
118{
119 ulong old_msr = vcpu->arch.msr;
120
121#ifdef EXIT_DEBUG
122 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
123#endif
Alexander Grafa76f8492010-01-15 14:49:14 +0100124
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000125 msr &= to_book3s(vcpu)->msr_mask;
126 vcpu->arch.msr = msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100127 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000128
129 if (msr & (MSR_WE|MSR_POW)) {
130 if (!vcpu->arch.pending_exceptions) {
131 kvm_vcpu_block(vcpu);
132 vcpu->stat.halt_wakeup++;
133 }
134 }
135
136 if (((vcpu->arch.msr & (MSR_IR|MSR_DR)) != (old_msr & (MSR_IR|MSR_DR))) ||
137 (vcpu->arch.msr & MSR_PR) != (old_msr & MSR_PR)) {
Alexander Graf3eeafd72010-03-24 21:48:17 +0100138 bool dr = (vcpu->arch.msr & MSR_DR) ? true : false;
139 bool ir = (vcpu->arch.msr & MSR_IR) ? true : false;
140
141 /* Flush split mode PTEs */
142 if (dr != ir)
143 kvmppc_mmu_pte_vflush(vcpu, VSID_SPLIT_MASK,
144 VSID_SPLIT_MASK);
145
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000146 kvmppc_mmu_flush_segments(vcpu);
147 kvmppc_mmu_map_segment(vcpu, vcpu->arch.pc);
148 }
Alexander Grafd1bab742010-02-19 11:00:35 +0100149
150 /* Preload FPU if it's enabled */
151 if (vcpu->arch.msr & MSR_FP)
152 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000153}
154
155void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
156{
157 vcpu->arch.srr0 = vcpu->arch.pc;
158 vcpu->arch.srr1 = vcpu->arch.msr | flags;
159 vcpu->arch.pc = to_book3s(vcpu)->hior + vec;
160 vcpu->arch.mmu.reset_msr(vcpu);
161}
162
Alexander Graf583617b2009-12-21 20:21:23 +0100163static int kvmppc_book3s_vec2irqprio(unsigned int vec)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000164{
165 unsigned int prio;
166
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000167 switch (vec) {
168 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
169 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
170 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
171 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
172 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
173 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
174 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
175 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
176 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
177 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
178 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
179 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
180 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
181 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
182 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
183 default: prio = BOOK3S_IRQPRIO_MAX; break;
184 }
185
Alexander Graf583617b2009-12-21 20:21:23 +0100186 return prio;
187}
188
Alexander Graf7706664d2009-12-21 20:21:24 +0100189static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
190 unsigned int vec)
191{
192 clear_bit(kvmppc_book3s_vec2irqprio(vec),
193 &vcpu->arch.pending_exceptions);
194}
195
Alexander Graf583617b2009-12-21 20:21:23 +0100196void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
197{
198 vcpu->stat.queue_intr++;
199
200 set_bit(kvmppc_book3s_vec2irqprio(vec),
201 &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000202#ifdef EXIT_DEBUG
203 printk(KERN_INFO "Queueing interrupt %x\n", vec);
204#endif
205}
206
207
Alexander Graf25a8a022010-01-08 02:58:07 +0100208void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000209{
Alexander Graf25a8a022010-01-08 02:58:07 +0100210 to_book3s(vcpu)->prog_flags = flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000211 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM);
212}
213
214void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
215{
216 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
217}
218
219int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
220{
221 return test_bit(BOOK3S_INTERRUPT_DECREMENTER >> 7, &vcpu->arch.pending_exceptions);
222}
223
Alexander Graf7706664d2009-12-21 20:21:24 +0100224void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
225{
226 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
227}
228
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000229void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
230 struct kvm_interrupt *irq)
231{
232 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
233}
234
Alexander Graf18978762010-03-24 21:48:18 +0100235void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
236 struct kvm_interrupt *irq)
237{
238 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
239}
240
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000241int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
242{
243 int deliver = 1;
244 int vec = 0;
Alexander Graf25a8a022010-01-08 02:58:07 +0100245 ulong flags = 0ULL;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000246
247 switch (priority) {
248 case BOOK3S_IRQPRIO_DECREMENTER:
249 deliver = vcpu->arch.msr & MSR_EE;
250 vec = BOOK3S_INTERRUPT_DECREMENTER;
251 break;
252 case BOOK3S_IRQPRIO_EXTERNAL:
253 deliver = vcpu->arch.msr & MSR_EE;
254 vec = BOOK3S_INTERRUPT_EXTERNAL;
255 break;
256 case BOOK3S_IRQPRIO_SYSTEM_RESET:
257 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
258 break;
259 case BOOK3S_IRQPRIO_MACHINE_CHECK:
260 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
261 break;
262 case BOOK3S_IRQPRIO_DATA_STORAGE:
263 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
264 break;
265 case BOOK3S_IRQPRIO_INST_STORAGE:
266 vec = BOOK3S_INTERRUPT_INST_STORAGE;
267 break;
268 case BOOK3S_IRQPRIO_DATA_SEGMENT:
269 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
270 break;
271 case BOOK3S_IRQPRIO_INST_SEGMENT:
272 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
273 break;
274 case BOOK3S_IRQPRIO_ALIGNMENT:
275 vec = BOOK3S_INTERRUPT_ALIGNMENT;
276 break;
277 case BOOK3S_IRQPRIO_PROGRAM:
278 vec = BOOK3S_INTERRUPT_PROGRAM;
Alexander Graf25a8a022010-01-08 02:58:07 +0100279 flags = to_book3s(vcpu)->prog_flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000280 break;
281 case BOOK3S_IRQPRIO_VSX:
282 vec = BOOK3S_INTERRUPT_VSX;
283 break;
284 case BOOK3S_IRQPRIO_ALTIVEC:
285 vec = BOOK3S_INTERRUPT_ALTIVEC;
286 break;
287 case BOOK3S_IRQPRIO_FP_UNAVAIL:
288 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
289 break;
290 case BOOK3S_IRQPRIO_SYSCALL:
291 vec = BOOK3S_INTERRUPT_SYSCALL;
292 break;
293 case BOOK3S_IRQPRIO_DEBUG:
294 vec = BOOK3S_INTERRUPT_TRACE;
295 break;
296 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
297 vec = BOOK3S_INTERRUPT_PERFMON;
298 break;
299 default:
300 deliver = 0;
301 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
302 break;
303 }
304
305#if 0
306 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
307#endif
308
309 if (deliver)
Alexander Graf25a8a022010-01-08 02:58:07 +0100310 kvmppc_inject_interrupt(vcpu, vec, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000311
312 return deliver;
313}
314
315void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
316{
317 unsigned long *pending = &vcpu->arch.pending_exceptions;
318 unsigned int priority;
319
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000320#ifdef EXIT_DEBUG
321 if (vcpu->arch.pending_exceptions)
322 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
323#endif
324 priority = __ffs(*pending);
325 while (priority <= (sizeof(unsigned int) * 8)) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100326 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
327 (priority != BOOK3S_IRQPRIO_DECREMENTER)) {
328 /* DEC interrupts get cleared by mtdec */
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000329 clear_bit(priority, &vcpu->arch.pending_exceptions);
330 break;
331 }
332
333 priority = find_next_bit(pending,
334 BITS_PER_BYTE * sizeof(*pending),
335 priority + 1);
336 }
337}
338
339void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
340{
Alexander Grafe15a1132009-11-30 03:02:02 +0000341 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000342 vcpu->arch.pvr = pvr;
343 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
344 kvmppc_mmu_book3s_64_init(vcpu);
345 to_book3s(vcpu)->hior = 0xfff00000;
346 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
347 } else {
348 kvmppc_mmu_book3s_32_init(vcpu);
349 to_book3s(vcpu)->hior = 0;
350 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
351 }
352
353 /* If we are in hypervisor level on 970, we can tell the CPU to
354 * treat DCBZ as 32 bytes store */
355 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
356 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
357 !strcmp(cur_cpu_spec->platform, "ppc970"))
358 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
359
360}
361
362/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
363 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
364 * emulate 32 bytes dcbz length.
365 *
366 * The Book3s_64 inventors also realized this case and implemented a special bit
367 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
368 *
369 * My approach here is to patch the dcbz instruction on executing pages.
370 */
371static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
372{
Alexander Graf9fb244a2010-03-24 21:48:32 +0100373 struct page *hpage;
374 u64 hpage_offset;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000375 u32 *page;
376 int i;
377
Alexander Graf9fb244a2010-03-24 21:48:32 +0100378 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
379 if (is_error_page(hpage))
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000380 return;
381
Alexander Graf9fb244a2010-03-24 21:48:32 +0100382 hpage_offset = pte->raddr & ~PAGE_MASK;
383 hpage_offset &= ~0xFFFULL;
384 hpage_offset /= 4;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000385
Alexander Graf9fb244a2010-03-24 21:48:32 +0100386 get_page(hpage);
387 page = kmap_atomic(hpage, KM_USER0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000388
Alexander Graf9fb244a2010-03-24 21:48:32 +0100389 /* patch dcbz into reserved instruction, so we trap */
390 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
391 if ((page[i] & 0xff0007ff) == INS_DCBZ)
392 page[i] &= 0xfffffff7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000393
Alexander Graf9fb244a2010-03-24 21:48:32 +0100394 kunmap_atomic(page, KM_USER0);
395 put_page(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000396}
397
398static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
399 struct kvmppc_pte *pte)
400{
401 int relocated = (vcpu->arch.msr & (data ? MSR_DR : MSR_IR));
402 int r;
403
404 if (relocated) {
405 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
406 } else {
407 pte->eaddr = eaddr;
408 pte->raddr = eaddr & 0xffffffff;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100409 pte->vpage = VSID_REAL | eaddr >> 12;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000410 pte->may_read = true;
411 pte->may_write = true;
412 pte->may_execute = true;
413 r = 0;
414 }
415
416 return r;
417}
418
419static hva_t kvmppc_bad_hva(void)
420{
421 return PAGE_OFFSET;
422}
423
424static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
425 bool read)
426{
427 hva_t hpage;
428
429 if (read && !pte->may_read)
430 goto err;
431
432 if (!read && !pte->may_write)
433 goto err;
434
435 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
436 if (kvm_is_error_hva(hpage))
437 goto err;
438
439 return hpage | (pte->raddr & ~PAGE_MASK);
440err:
441 return kvmppc_bad_hva();
442}
443
Alexander Graf5467a972010-02-19 11:00:38 +0100444int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
445 bool data)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000446{
447 struct kvmppc_pte pte;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000448
449 vcpu->stat.st++;
450
Alexander Graf5467a972010-02-19 11:00:38 +0100451 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
Alexander Graf9fb244a2010-03-24 21:48:32 +0100452 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100453
454 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000455
Alexander Graf9fb244a2010-03-24 21:48:32 +0100456 if (!pte.may_write)
457 return -EPERM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000458
Alexander Graf9fb244a2010-03-24 21:48:32 +0100459 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
460 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000461
Alexander Graf5467a972010-02-19 11:00:38 +0100462 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000463}
464
Alexander Graf5467a972010-02-19 11:00:38 +0100465int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000466 bool data)
467{
468 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100469 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000470
471 vcpu->stat.ld++;
472
Alexander Graf5467a972010-02-19 11:00:38 +0100473 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
474 goto nopte;
475
476 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000477
478 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
479 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100480 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000481
482 if (copy_from_user(ptr, (void __user *)hva, size)) {
483 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100484 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000485 }
486
Alexander Graf5467a972010-02-19 11:00:38 +0100487 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000488
Alexander Graf5467a972010-02-19 11:00:38 +0100489nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000490 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100491mmio:
492 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000493}
494
495static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
496{
497 return kvm_is_visible_gfn(vcpu->kvm, gfn);
498}
499
500int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
501 ulong eaddr, int vec)
502{
503 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
504 int r = RESUME_GUEST;
505 int relocated;
506 int page_found = 0;
507 struct kvmppc_pte pte;
508 bool is_mmio = false;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100509 bool dr = (vcpu->arch.msr & MSR_DR) ? true : false;
510 bool ir = (vcpu->arch.msr & MSR_IR) ? true : false;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000511
Alexander Graf3eeafd72010-03-24 21:48:17 +0100512 relocated = data ? dr : ir;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000513
514 /* Resolve real address if translation turned on */
515 if (relocated) {
516 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
517 } else {
518 pte.may_execute = true;
519 pte.may_read = true;
520 pte.may_write = true;
521 pte.raddr = eaddr & 0xffffffff;
522 pte.eaddr = eaddr;
523 pte.vpage = eaddr >> 12;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100524 }
525
526 switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
527 case 0:
528 pte.vpage |= VSID_REAL;
529 break;
530 case MSR_DR:
531 pte.vpage |= VSID_REAL_DR;
532 break;
533 case MSR_IR:
534 pte.vpage |= VSID_REAL_IR;
535 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000536 }
537
538 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
539 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
540 /*
541 * If we do the dcbz hack, we have to NX on every execution,
542 * so we can patch the executing code. This renders our guest
543 * NX-less.
544 */
545 pte.may_execute = !data;
546 }
547
548 if (page_found == -ENOENT) {
549 /* Page not found in guest PTE entries */
550 vcpu->arch.dear = vcpu->arch.fault_dear;
551 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr;
Alexander Graff7adbba2010-01-15 14:49:13 +0100552 vcpu->arch.msr |= (vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000553 kvmppc_book3s_queue_irqprio(vcpu, vec);
554 } else if (page_found == -EPERM) {
555 /* Storage protection */
556 vcpu->arch.dear = vcpu->arch.fault_dear;
557 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr & ~DSISR_NOHPTE;
558 to_book3s(vcpu)->dsisr |= DSISR_PROTFAULT;
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 == -EINVAL) {
562 /* Page not found in guest SLB */
563 vcpu->arch.dear = vcpu->arch.fault_dear;
564 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
565 } else if (!is_mmio &&
566 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
567 /* The guest's PTE is not mapped yet. Map on the host */
568 kvmppc_mmu_map_page(vcpu, &pte);
569 if (data)
570 vcpu->stat.sp_storage++;
571 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
572 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
573 kvmppc_patch_dcbz(vcpu, &pte);
574 } else {
575 /* MMIO */
576 vcpu->stat.mmio_exits++;
577 vcpu->arch.paddr_accessed = pte.raddr;
578 r = kvmppc_emulate_mmio(run, vcpu);
579 if ( r == RESUME_HOST_NV )
580 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000581 }
582
583 return r;
584}
585
Alexander Graf180a34d2010-01-15 14:49:11 +0100586static inline int get_fpr_index(int i)
587{
588#ifdef CONFIG_VSX
589 i *= 2;
590#endif
591 return i;
592}
593
594/* Give up external provider (FPU, Altivec, VSX) */
Alexander Grafaba3bd72010-02-19 11:00:39 +0100595void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
Alexander Graf180a34d2010-01-15 14:49:11 +0100596{
597 struct thread_struct *t = &current->thread;
598 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100599#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100600 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100601#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100602 u64 *thread_fpr = (u64*)t->fpr;
603 int i;
604
605 if (!(vcpu->arch.guest_owned_ext & msr))
606 return;
607
608#ifdef DEBUG_EXT
609 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
610#endif
611
612 switch (msr) {
613 case MSR_FP:
614 giveup_fpu(current);
615 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
616 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
617
618 vcpu->arch.fpscr = t->fpscr.val;
619 break;
620 case MSR_VEC:
621#ifdef CONFIG_ALTIVEC
622 giveup_altivec(current);
623 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
624 vcpu->arch.vscr = t->vscr;
625#endif
626 break;
627 case MSR_VSX:
628#ifdef CONFIG_VSX
629 __giveup_vsx(current);
630 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
631 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
632#endif
633 break;
634 default:
635 BUG();
636 }
637
638 vcpu->arch.guest_owned_ext &= ~msr;
639 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100640 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100641}
642
Alexander Graf89632212010-03-24 21:48:21 +0100643static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100644{
645 ulong srr0 = vcpu->arch.pc;
646 int ret;
647
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100648 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &vcpu->arch.last_inst, false);
649 if (ret == -ENOENT) {
650 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 33, 33, 1);
651 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 34, 36, 0);
652 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 42, 47, 0);
653 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
Alexander Graf89632212010-03-24 21:48:21 +0100654 return EMULATE_AGAIN;
655 }
656
657 return EMULATE_DONE;
658}
659
660static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
661{
662
663 /* Need to do paired single emulation? */
664 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
665 return EMULATE_DONE;
666
667 /* Read out the instruction */
668 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100669 /* Need to emulate */
670 return EMULATE_FAIL;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100671
672 return EMULATE_AGAIN;
673}
674
Alexander Graf180a34d2010-01-15 14:49:11 +0100675/* Handle external providers (FPU, Altivec, VSX) */
676static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
677 ulong msr)
678{
679 struct thread_struct *t = &current->thread;
680 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100681#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100682 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100683#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100684 u64 *thread_fpr = (u64*)t->fpr;
685 int i;
686
Alexander Graf3c402a72010-02-19 11:00:32 +0100687 /* When we have paired singles, we emulate in software */
688 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
689 return RESUME_GUEST;
690
Alexander Graf180a34d2010-01-15 14:49:11 +0100691 if (!(vcpu->arch.msr & msr)) {
692 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
693 return RESUME_GUEST;
694 }
695
Alexander Grafc2453692010-03-24 21:48:22 +0100696 /* We already own the ext */
697 if (vcpu->arch.guest_owned_ext & msr) {
698 return RESUME_GUEST;
699 }
700
Alexander Graf180a34d2010-01-15 14:49:11 +0100701#ifdef DEBUG_EXT
702 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
703#endif
704
705 current->thread.regs->msr |= msr;
706
707 switch (msr) {
708 case MSR_FP:
709 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
710 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
711
712 t->fpscr.val = vcpu->arch.fpscr;
713 t->fpexc_mode = 0;
714 kvmppc_load_up_fpu();
715 break;
716 case MSR_VEC:
717#ifdef CONFIG_ALTIVEC
718 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
719 t->vscr = vcpu->arch.vscr;
720 t->vrsave = -1;
721 kvmppc_load_up_altivec();
722#endif
723 break;
724 case MSR_VSX:
725#ifdef CONFIG_VSX
726 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
727 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
728 kvmppc_load_up_vsx();
729#endif
730 break;
731 default:
732 BUG();
733 }
734
735 vcpu->arch.guest_owned_ext |= msr;
736
Alexander Grafa76f8492010-01-15 14:49:14 +0100737 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100738
739 return RESUME_GUEST;
740}
741
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000742int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
743 unsigned int exit_nr)
744{
745 int r = RESUME_HOST;
746
747 vcpu->stat.sum_exits++;
748
749 run->exit_reason = KVM_EXIT_UNKNOWN;
750 run->ready_for_interrupt_injection = 1;
751#ifdef EXIT_DEBUG
752 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | dec=0x%x | msr=0x%lx\n",
753 exit_nr, vcpu->arch.pc, vcpu->arch.fault_dear,
754 kvmppc_get_dec(vcpu), vcpu->arch.msr);
755#elif defined (EXIT_DEBUG_SIMPLE)
756 if ((exit_nr != 0x900) && (exit_nr != 0x500))
757 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | msr=0x%lx\n",
758 exit_nr, vcpu->arch.pc, vcpu->arch.fault_dear,
759 vcpu->arch.msr);
760#endif
761 kvm_resched(vcpu);
762 switch (exit_nr) {
763 case BOOK3S_INTERRUPT_INST_STORAGE:
764 vcpu->stat.pf_instruc++;
765 /* only care about PTEG not found errors, but leave NX alone */
Alexander Graff7adbba2010-01-15 14:49:13 +0100766 if (vcpu->arch.shadow_srr1 & 0x40000000) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000767 r = kvmppc_handle_pagefault(run, vcpu, vcpu->arch.pc, exit_nr);
768 vcpu->stat.sp_instruc++;
769 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
770 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
771 /*
772 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
773 * so we can't use the NX bit inside the guest. Let's cross our fingers,
774 * that no guest that needs the dcbz hack does NX.
775 */
776 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.pc, ~0xFFFULL);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100777 r = RESUME_GUEST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000778 } else {
Alexander Graff7adbba2010-01-15 14:49:13 +0100779 vcpu->arch.msr |= vcpu->arch.shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000780 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
781 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.pc, ~0xFFFULL);
782 r = RESUME_GUEST;
783 }
784 break;
785 case BOOK3S_INTERRUPT_DATA_STORAGE:
786 vcpu->stat.pf_storage++;
787 /* The only case we need to handle is missing shadow PTEs */
788 if (vcpu->arch.fault_dsisr & DSISR_NOHPTE) {
789 r = kvmppc_handle_pagefault(run, vcpu, vcpu->arch.fault_dear, exit_nr);
790 } else {
791 vcpu->arch.dear = vcpu->arch.fault_dear;
792 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr;
793 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
794 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.dear, ~0xFFFULL);
795 r = RESUME_GUEST;
796 }
797 break;
798 case BOOK3S_INTERRUPT_DATA_SEGMENT:
799 if (kvmppc_mmu_map_segment(vcpu, vcpu->arch.fault_dear) < 0) {
800 vcpu->arch.dear = vcpu->arch.fault_dear;
801 kvmppc_book3s_queue_irqprio(vcpu,
802 BOOK3S_INTERRUPT_DATA_SEGMENT);
803 }
804 r = RESUME_GUEST;
805 break;
806 case BOOK3S_INTERRUPT_INST_SEGMENT:
807 if (kvmppc_mmu_map_segment(vcpu, vcpu->arch.pc) < 0) {
808 kvmppc_book3s_queue_irqprio(vcpu,
809 BOOK3S_INTERRUPT_INST_SEGMENT);
810 }
811 r = RESUME_GUEST;
812 break;
813 /* We're good on these - the host merely wanted to get our attention */
814 case BOOK3S_INTERRUPT_DECREMENTER:
815 vcpu->stat.dec_exits++;
816 r = RESUME_GUEST;
817 break;
818 case BOOK3S_INTERRUPT_EXTERNAL:
819 vcpu->stat.ext_intr_exits++;
820 r = RESUME_GUEST;
821 break;
822 case BOOK3S_INTERRUPT_PROGRAM:
823 {
824 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100825 ulong flags;
826
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100827program_interrupt:
Alexander Graff7adbba2010-01-15 14:49:13 +0100828 flags = vcpu->arch.shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000829
830 if (vcpu->arch.msr & MSR_PR) {
831#ifdef EXIT_DEBUG
832 printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", vcpu->arch.pc, vcpu->arch.last_inst);
833#endif
834 if ((vcpu->arch.last_inst & 0xff0007ff) !=
835 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100836 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000837 r = RESUME_GUEST;
838 break;
839 }
840 }
841
842 vcpu->stat.emulated_inst_exits++;
843 er = kvmppc_emulate_instruction(run, vcpu);
844 switch (er) {
845 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100846 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000847 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100848 case EMULATE_AGAIN:
849 r = RESUME_GUEST;
850 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000851 case EMULATE_FAIL:
852 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
853 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100854 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000855 r = RESUME_GUEST;
856 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +0100857 case EMULATE_DO_MMIO:
858 run->exit_reason = KVM_EXIT_MMIO;
859 r = RESUME_HOST_NV;
860 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000861 default:
862 BUG();
863 }
864 break;
865 }
866 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafad0a0482010-03-24 21:48:30 +0100867 // XXX make user settable
868 if (vcpu->arch.osi_enabled &&
869 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
870 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
871 u64 *gprs = run->osi.gprs;
872 int i;
873
874 run->exit_reason = KVM_EXIT_OSI;
875 for (i = 0; i < 32; i++)
876 gprs[i] = kvmppc_get_gpr(vcpu, i);
877 vcpu->arch.osi_needed = 1;
878 r = RESUME_HOST_NV;
879
880 } else {
881 vcpu->stat.syscall_exits++;
882 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
883 r = RESUME_GUEST;
884 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000885 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000886 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000887 case BOOK3S_INTERRUPT_ALTIVEC:
888 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100889 {
890 int ext_msr = 0;
891
892 switch (exit_nr) {
893 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
894 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
895 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
896 }
897
898 switch (kvmppc_check_ext(vcpu, exit_nr)) {
899 case EMULATE_DONE:
900 /* everything ok - let's enable the ext */
901 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
902 break;
903 case EMULATE_FAIL:
904 /* we need to emulate this instruction */
905 goto program_interrupt;
906 break;
907 default:
908 /* nothing to worry about - go again */
909 break;
910 }
Alexander Graf180a34d2010-01-15 14:49:11 +0100911 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100912 }
Alexander Grafca7f4202010-03-24 21:48:28 +0100913 case BOOK3S_INTERRUPT_ALIGNMENT:
914 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
915 to_book3s(vcpu)->dsisr = kvmppc_alignment_dsisr(vcpu,
916 vcpu->arch.last_inst);
917 vcpu->arch.dear = kvmppc_alignment_dar(vcpu,
918 vcpu->arch.last_inst);
919 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
920 }
921 r = RESUME_GUEST;
922 break;
Alexander Graf180a34d2010-01-15 14:49:11 +0100923 case BOOK3S_INTERRUPT_MACHINE_CHECK:
924 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000925 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
926 r = RESUME_GUEST;
927 break;
928 default:
929 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +0100930 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
931 exit_nr, vcpu->arch.pc, vcpu->arch.shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000932 r = RESUME_HOST;
933 BUG();
934 break;
935 }
936
937
938 if (!(r & RESUME_HOST)) {
939 /* To avoid clobbering exit_reason, only check for signals if
940 * we aren't already exiting to userspace for some other
941 * reason. */
942 if (signal_pending(current)) {
943#ifdef EXIT_DEBUG
944 printk(KERN_EMERG "KVM: Going back to host\n");
945#endif
946 vcpu->stat.signal_exits++;
947 run->exit_reason = KVM_EXIT_INTR;
948 r = -EINTR;
949 } else {
950 /* In case an interrupt came in that was triggered
951 * from userspace (like DEC), we need to check what
952 * to inject now! */
953 kvmppc_core_deliver_interrupts(vcpu);
954 }
955 }
956
957#ifdef EXIT_DEBUG
958 printk(KERN_EMERG "KVM exit: vcpu=0x%p pc=0x%lx r=0x%x\n", vcpu, vcpu->arch.pc, r);
959#endif
960
961 return r;
962}
963
964int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
965{
966 return 0;
967}
968
969int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
970{
971 int i;
972
Alexander Grafa56cf342010-03-24 21:48:23 +0100973 vcpu_load(vcpu);
974
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000975 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100976 regs->cr = kvmppc_get_cr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000977 regs->ctr = vcpu->arch.ctr;
978 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100979 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000980 regs->msr = vcpu->arch.msr;
981 regs->srr0 = vcpu->arch.srr0;
982 regs->srr1 = vcpu->arch.srr1;
983 regs->pid = vcpu->arch.pid;
984 regs->sprg0 = vcpu->arch.sprg0;
985 regs->sprg1 = vcpu->arch.sprg1;
986 regs->sprg2 = vcpu->arch.sprg2;
987 regs->sprg3 = vcpu->arch.sprg3;
988 regs->sprg5 = vcpu->arch.sprg4;
989 regs->sprg6 = vcpu->arch.sprg5;
990 regs->sprg7 = vcpu->arch.sprg6;
991
992 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100993 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000994
Alexander Grafa56cf342010-03-24 21:48:23 +0100995 vcpu_put(vcpu);
996
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000997 return 0;
998}
999
1000int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1001{
1002 int i;
1003
Alexander Grafa56cf342010-03-24 21:48:23 +01001004 vcpu_load(vcpu);
1005
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001006 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001007 kvmppc_set_cr(vcpu, regs->cr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001008 vcpu->arch.ctr = regs->ctr;
1009 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001010 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001011 kvmppc_set_msr(vcpu, regs->msr);
1012 vcpu->arch.srr0 = regs->srr0;
1013 vcpu->arch.srr1 = regs->srr1;
1014 vcpu->arch.sprg0 = regs->sprg0;
1015 vcpu->arch.sprg1 = regs->sprg1;
1016 vcpu->arch.sprg2 = regs->sprg2;
1017 vcpu->arch.sprg3 = regs->sprg3;
1018 vcpu->arch.sprg5 = regs->sprg4;
1019 vcpu->arch.sprg6 = regs->sprg5;
1020 vcpu->arch.sprg7 = regs->sprg6;
1021
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001022 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1023 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001024
Alexander Grafa56cf342010-03-24 21:48:23 +01001025 vcpu_put(vcpu);
1026
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001027 return 0;
1028}
1029
1030int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1031 struct kvm_sregs *sregs)
1032{
Alexander Grafe15a1132009-11-30 03:02:02 +00001033 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1034 int i;
1035
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001036 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +00001037
1038 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1039 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1040 for (i = 0; i < 64; i++) {
1041 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
1042 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1043 }
1044 } else {
1045 for (i = 0; i < 16; i++) {
1046 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1047 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1048 }
1049 for (i = 0; i < 8; i++) {
1050 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1051 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1052 }
1053 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001054 return 0;
1055}
1056
1057int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1058 struct kvm_sregs *sregs)
1059{
Alexander Grafe15a1132009-11-30 03:02:02 +00001060 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1061 int i;
1062
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001063 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001064
1065 vcpu3s->sdr1 = sregs->u.s.sdr1;
1066 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1067 for (i = 0; i < 64; i++) {
1068 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1069 sregs->u.s.ppc64.slb[i].slbe);
1070 }
1071 } else {
1072 for (i = 0; i < 16; i++) {
1073 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1074 }
1075 for (i = 0; i < 8; i++) {
1076 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1077 (u32)sregs->u.s.ppc32.ibat[i]);
1078 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1079 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1080 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1081 (u32)sregs->u.s.ppc32.dbat[i]);
1082 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1083 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1084 }
1085 }
1086
1087 /* Flush the MMU after messing with the segments */
1088 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001089 return 0;
1090}
1091
1092int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1093{
1094 return -ENOTSUPP;
1095}
1096
1097int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1098{
1099 return -ENOTSUPP;
1100}
1101
1102int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1103 struct kvm_translation *tr)
1104{
1105 return 0;
1106}
1107
1108/*
1109 * Get (and clear) the dirty memory log for a memory slot.
1110 */
1111int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1112 struct kvm_dirty_log *log)
1113{
1114 struct kvm_memory_slot *memslot;
1115 struct kvm_vcpu *vcpu;
1116 ulong ga, ga_end;
1117 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001118 int r;
1119 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001120
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001121 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001122
1123 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1124 if (r)
1125 goto out;
1126
1127 /* If nothing is dirty, don't bother messing with page tables. */
1128 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001129 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001130
1131 ga = memslot->base_gfn << PAGE_SHIFT;
1132 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1133
1134 kvm_for_each_vcpu(n, vcpu, kvm)
1135 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1136
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001137 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001138 memset(memslot->dirty_bitmap, 0, n);
1139 }
1140
1141 r = 0;
1142out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001143 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001144 return r;
1145}
1146
1147int kvmppc_core_check_processor_compat(void)
1148{
1149 return 0;
1150}
1151
1152struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1153{
1154 struct kvmppc_vcpu_book3s *vcpu_book3s;
1155 struct kvm_vcpu *vcpu;
1156 int err;
1157
Alexander Graf032c3402010-02-19 12:24:33 +01001158 vcpu_book3s = vmalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001159 if (!vcpu_book3s) {
1160 err = -ENOMEM;
1161 goto out;
1162 }
Alexander Graf7e821d32010-02-22 16:52:08 +01001163 memset(vcpu_book3s, 0, sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001164
1165 vcpu = &vcpu_book3s->vcpu;
1166 err = kvm_vcpu_init(vcpu, kvm, id);
1167 if (err)
1168 goto free_vcpu;
1169
1170 vcpu->arch.host_retip = kvm_return_point;
1171 vcpu->arch.host_msr = mfmsr();
1172 /* default to book3s_64 (970fx) */
1173 vcpu->arch.pvr = 0x3C0301;
1174 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1175 vcpu_book3s->slb_nr = 64;
1176
1177 /* remember where some real-mode handlers are */
1178 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1179 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1180 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf021ec9c2010-01-08 02:58:06 +01001181 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001182
1183 vcpu->arch.shadow_msr = MSR_USER64;
1184
1185 err = __init_new_context();
1186 if (err < 0)
1187 goto free_vcpu;
1188 vcpu_book3s->context_id = err;
1189
1190 vcpu_book3s->vsid_max = ((vcpu_book3s->context_id + 1) << USER_ESID_BITS) - 1;
1191 vcpu_book3s->vsid_first = vcpu_book3s->context_id << USER_ESID_BITS;
1192 vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
1193
1194 return vcpu;
1195
1196free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001197 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001198out:
1199 return ERR_PTR(err);
1200}
1201
1202void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1203{
1204 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1205
1206 __destroy_context(vcpu_book3s->context_id);
1207 kvm_vcpu_uninit(vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001208 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001209}
1210
1211extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1212int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1213{
1214 int ret;
Alexander Graf180a34d2010-01-15 14:49:11 +01001215 struct thread_struct ext_bkp;
Alexander Grafa2b07662010-03-24 21:48:31 +01001216#ifdef CONFIG_ALTIVEC
Alexander Graf180a34d2010-01-15 14:49:11 +01001217 bool save_vec = current->thread.used_vr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001218#endif
1219#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +01001220 bool save_vsx = current->thread.used_vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001221#endif
Alexander Graf180a34d2010-01-15 14:49:11 +01001222 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001223
1224 /* No need to go into the guest when all we do is going out */
1225 if (signal_pending(current)) {
1226 kvm_run->exit_reason = KVM_EXIT_INTR;
1227 return -EINTR;
1228 }
1229
Alexander Graf180a34d2010-01-15 14:49:11 +01001230 /* Save FPU state in stack */
1231 if (current->thread.regs->msr & MSR_FP)
1232 giveup_fpu(current);
1233 memcpy(ext_bkp.fpr, current->thread.fpr, sizeof(current->thread.fpr));
1234 ext_bkp.fpscr = current->thread.fpscr;
1235 ext_bkp.fpexc_mode = current->thread.fpexc_mode;
1236
1237#ifdef CONFIG_ALTIVEC
1238 /* Save Altivec state in stack */
1239 if (save_vec) {
1240 if (current->thread.regs->msr & MSR_VEC)
1241 giveup_altivec(current);
1242 memcpy(ext_bkp.vr, current->thread.vr, sizeof(ext_bkp.vr));
1243 ext_bkp.vscr = current->thread.vscr;
1244 ext_bkp.vrsave = current->thread.vrsave;
1245 }
1246 ext_bkp.used_vr = current->thread.used_vr;
1247#endif
1248
1249#ifdef CONFIG_VSX
1250 /* Save VSX state in stack */
1251 if (save_vsx && (current->thread.regs->msr & MSR_VSX))
1252 __giveup_vsx(current);
1253 ext_bkp.used_vsr = current->thread.used_vsr;
1254#endif
1255
1256 /* Remember the MSR with disabled extensions */
1257 ext_msr = current->thread.regs->msr;
1258
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001259 /* XXX we get called with irq disabled - change that! */
1260 local_irq_enable();
1261
Alexander Grafd1bab742010-02-19 11:00:35 +01001262 /* Preload FPU if it's enabled */
1263 if (vcpu->arch.msr & MSR_FP)
1264 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1265
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001266 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1267
1268 local_irq_disable();
1269
Alexander Graf180a34d2010-01-15 14:49:11 +01001270 current->thread.regs->msr = ext_msr;
1271
1272 /* Make sure we save the guest FPU/Altivec/VSX state */
1273 kvmppc_giveup_ext(vcpu, MSR_FP);
1274 kvmppc_giveup_ext(vcpu, MSR_VEC);
1275 kvmppc_giveup_ext(vcpu, MSR_VSX);
1276
1277 /* Restore FPU state from stack */
1278 memcpy(current->thread.fpr, ext_bkp.fpr, sizeof(ext_bkp.fpr));
1279 current->thread.fpscr = ext_bkp.fpscr;
1280 current->thread.fpexc_mode = ext_bkp.fpexc_mode;
1281
1282#ifdef CONFIG_ALTIVEC
1283 /* Restore Altivec state from stack */
1284 if (save_vec && current->thread.used_vr) {
1285 memcpy(current->thread.vr, ext_bkp.vr, sizeof(ext_bkp.vr));
1286 current->thread.vscr = ext_bkp.vscr;
1287 current->thread.vrsave= ext_bkp.vrsave;
1288 }
1289 current->thread.used_vr = ext_bkp.used_vr;
1290#endif
1291
1292#ifdef CONFIG_VSX
1293 current->thread.used_vsr = ext_bkp.used_vsr;
1294#endif
1295
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001296 return ret;
1297}
1298
1299static int kvmppc_book3s_init(void)
1300{
1301 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), THIS_MODULE);
1302}
1303
1304static void kvmppc_book3s_exit(void)
1305{
1306 kvm_exit();
1307}
1308
1309module_init(kvmppc_book3s_init);
1310module_exit(kvmppc_book3s_exit);