blob: 41c23b636f53f993a14655927cdc7be8528eec71 [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
Alexander Graf05b0ab12010-03-24 21:48:37 +0100360 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
361 really needs them in a VM on Cell and force disable them. */
362 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
363 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000364}
365
366/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
367 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
368 * emulate 32 bytes dcbz length.
369 *
370 * The Book3s_64 inventors also realized this case and implemented a special bit
371 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
372 *
373 * My approach here is to patch the dcbz instruction on executing pages.
374 */
375static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
376{
Alexander Graf9fb244a2010-03-24 21:48:32 +0100377 struct page *hpage;
378 u64 hpage_offset;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000379 u32 *page;
380 int i;
381
Alexander Graf9fb244a2010-03-24 21:48:32 +0100382 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
383 if (is_error_page(hpage))
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000384 return;
385
Alexander Graf9fb244a2010-03-24 21:48:32 +0100386 hpage_offset = pte->raddr & ~PAGE_MASK;
387 hpage_offset &= ~0xFFFULL;
388 hpage_offset /= 4;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000389
Alexander Graf9fb244a2010-03-24 21:48:32 +0100390 get_page(hpage);
391 page = kmap_atomic(hpage, KM_USER0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000392
Alexander Graf9fb244a2010-03-24 21:48:32 +0100393 /* patch dcbz into reserved instruction, so we trap */
394 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
395 if ((page[i] & 0xff0007ff) == INS_DCBZ)
396 page[i] &= 0xfffffff7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000397
Alexander Graf9fb244a2010-03-24 21:48:32 +0100398 kunmap_atomic(page, KM_USER0);
399 put_page(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000400}
401
402static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
403 struct kvmppc_pte *pte)
404{
405 int relocated = (vcpu->arch.msr & (data ? MSR_DR : MSR_IR));
406 int r;
407
408 if (relocated) {
409 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
410 } else {
411 pte->eaddr = eaddr;
412 pte->raddr = eaddr & 0xffffffff;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100413 pte->vpage = VSID_REAL | eaddr >> 12;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000414 pte->may_read = true;
415 pte->may_write = true;
416 pte->may_execute = true;
417 r = 0;
418 }
419
420 return r;
421}
422
423static hva_t kvmppc_bad_hva(void)
424{
425 return PAGE_OFFSET;
426}
427
428static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
429 bool read)
430{
431 hva_t hpage;
432
433 if (read && !pte->may_read)
434 goto err;
435
436 if (!read && !pte->may_write)
437 goto err;
438
439 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
440 if (kvm_is_error_hva(hpage))
441 goto err;
442
443 return hpage | (pte->raddr & ~PAGE_MASK);
444err:
445 return kvmppc_bad_hva();
446}
447
Alexander Graf5467a972010-02-19 11:00:38 +0100448int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
449 bool data)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000450{
451 struct kvmppc_pte pte;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000452
453 vcpu->stat.st++;
454
Alexander Graf5467a972010-02-19 11:00:38 +0100455 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
Alexander Graf9fb244a2010-03-24 21:48:32 +0100456 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100457
458 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000459
Alexander Graf9fb244a2010-03-24 21:48:32 +0100460 if (!pte.may_write)
461 return -EPERM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000462
Alexander Graf9fb244a2010-03-24 21:48:32 +0100463 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
464 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000465
Alexander Graf5467a972010-02-19 11:00:38 +0100466 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000467}
468
Alexander Graf5467a972010-02-19 11:00:38 +0100469int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000470 bool data)
471{
472 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100473 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000474
475 vcpu->stat.ld++;
476
Alexander Graf5467a972010-02-19 11:00:38 +0100477 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
478 goto nopte;
479
480 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000481
482 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
483 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100484 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000485
486 if (copy_from_user(ptr, (void __user *)hva, size)) {
487 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100488 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000489 }
490
Alexander Graf5467a972010-02-19 11:00:38 +0100491 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000492
Alexander Graf5467a972010-02-19 11:00:38 +0100493nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000494 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100495mmio:
496 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000497}
498
499static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
500{
501 return kvm_is_visible_gfn(vcpu->kvm, gfn);
502}
503
504int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
505 ulong eaddr, int vec)
506{
507 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
508 int r = RESUME_GUEST;
509 int relocated;
510 int page_found = 0;
511 struct kvmppc_pte pte;
512 bool is_mmio = false;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100513 bool dr = (vcpu->arch.msr & MSR_DR) ? true : false;
514 bool ir = (vcpu->arch.msr & MSR_IR) ? true : false;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000515
Alexander Graf3eeafd72010-03-24 21:48:17 +0100516 relocated = data ? dr : ir;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000517
518 /* Resolve real address if translation turned on */
519 if (relocated) {
520 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
521 } else {
522 pte.may_execute = true;
523 pte.may_read = true;
524 pte.may_write = true;
525 pte.raddr = eaddr & 0xffffffff;
526 pte.eaddr = eaddr;
527 pte.vpage = eaddr >> 12;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100528 }
529
530 switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
531 case 0:
532 pte.vpage |= VSID_REAL;
533 break;
534 case MSR_DR:
535 pte.vpage |= VSID_REAL_DR;
536 break;
537 case MSR_IR:
538 pte.vpage |= VSID_REAL_IR;
539 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000540 }
541
542 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
543 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
544 /*
545 * If we do the dcbz hack, we have to NX on every execution,
546 * so we can patch the executing code. This renders our guest
547 * NX-less.
548 */
549 pte.may_execute = !data;
550 }
551
552 if (page_found == -ENOENT) {
553 /* Page not found in guest PTE entries */
554 vcpu->arch.dear = vcpu->arch.fault_dear;
555 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr;
Alexander Graff7adbba2010-01-15 14:49:13 +0100556 vcpu->arch.msr |= (vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000557 kvmppc_book3s_queue_irqprio(vcpu, vec);
558 } else if (page_found == -EPERM) {
559 /* Storage protection */
560 vcpu->arch.dear = vcpu->arch.fault_dear;
561 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr & ~DSISR_NOHPTE;
562 to_book3s(vcpu)->dsisr |= DSISR_PROTFAULT;
Alexander Graff7adbba2010-01-15 14:49:13 +0100563 vcpu->arch.msr |= (vcpu->arch.shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000564 kvmppc_book3s_queue_irqprio(vcpu, vec);
565 } else if (page_found == -EINVAL) {
566 /* Page not found in guest SLB */
567 vcpu->arch.dear = vcpu->arch.fault_dear;
568 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
569 } else if (!is_mmio &&
570 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
571 /* The guest's PTE is not mapped yet. Map on the host */
572 kvmppc_mmu_map_page(vcpu, &pte);
573 if (data)
574 vcpu->stat.sp_storage++;
575 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
576 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
577 kvmppc_patch_dcbz(vcpu, &pte);
578 } else {
579 /* MMIO */
580 vcpu->stat.mmio_exits++;
581 vcpu->arch.paddr_accessed = pte.raddr;
582 r = kvmppc_emulate_mmio(run, vcpu);
583 if ( r == RESUME_HOST_NV )
584 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000585 }
586
587 return r;
588}
589
Alexander Graf180a34d2010-01-15 14:49:11 +0100590static inline int get_fpr_index(int i)
591{
592#ifdef CONFIG_VSX
593 i *= 2;
594#endif
595 return i;
596}
597
598/* Give up external provider (FPU, Altivec, VSX) */
Alexander Grafaba3bd72010-02-19 11:00:39 +0100599void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
Alexander Graf180a34d2010-01-15 14:49:11 +0100600{
601 struct thread_struct *t = &current->thread;
602 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100603#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100604 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100605#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100606 u64 *thread_fpr = (u64*)t->fpr;
607 int i;
608
609 if (!(vcpu->arch.guest_owned_ext & msr))
610 return;
611
612#ifdef DEBUG_EXT
613 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
614#endif
615
616 switch (msr) {
617 case MSR_FP:
618 giveup_fpu(current);
619 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
620 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
621
622 vcpu->arch.fpscr = t->fpscr.val;
623 break;
624 case MSR_VEC:
625#ifdef CONFIG_ALTIVEC
626 giveup_altivec(current);
627 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
628 vcpu->arch.vscr = t->vscr;
629#endif
630 break;
631 case MSR_VSX:
632#ifdef CONFIG_VSX
633 __giveup_vsx(current);
634 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
635 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
636#endif
637 break;
638 default:
639 BUG();
640 }
641
642 vcpu->arch.guest_owned_ext &= ~msr;
643 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100644 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100645}
646
Alexander Graf89632212010-03-24 21:48:21 +0100647static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100648{
649 ulong srr0 = vcpu->arch.pc;
650 int ret;
651
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100652 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &vcpu->arch.last_inst, false);
653 if (ret == -ENOENT) {
654 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 33, 33, 1);
655 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 34, 36, 0);
656 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 42, 47, 0);
657 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
Alexander Graf89632212010-03-24 21:48:21 +0100658 return EMULATE_AGAIN;
659 }
660
661 return EMULATE_DONE;
662}
663
664static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
665{
666
667 /* Need to do paired single emulation? */
668 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
669 return EMULATE_DONE;
670
671 /* Read out the instruction */
672 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100673 /* Need to emulate */
674 return EMULATE_FAIL;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100675
676 return EMULATE_AGAIN;
677}
678
Alexander Graf180a34d2010-01-15 14:49:11 +0100679/* Handle external providers (FPU, Altivec, VSX) */
680static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
681 ulong msr)
682{
683 struct thread_struct *t = &current->thread;
684 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100685#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100686 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100687#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100688 u64 *thread_fpr = (u64*)t->fpr;
689 int i;
690
Alexander Graf3c402a72010-02-19 11:00:32 +0100691 /* When we have paired singles, we emulate in software */
692 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
693 return RESUME_GUEST;
694
Alexander Graf180a34d2010-01-15 14:49:11 +0100695 if (!(vcpu->arch.msr & msr)) {
696 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
697 return RESUME_GUEST;
698 }
699
Alexander Grafc2453692010-03-24 21:48:22 +0100700 /* We already own the ext */
701 if (vcpu->arch.guest_owned_ext & msr) {
702 return RESUME_GUEST;
703 }
704
Alexander Graf180a34d2010-01-15 14:49:11 +0100705#ifdef DEBUG_EXT
706 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
707#endif
708
709 current->thread.regs->msr |= msr;
710
711 switch (msr) {
712 case MSR_FP:
713 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
714 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
715
716 t->fpscr.val = vcpu->arch.fpscr;
717 t->fpexc_mode = 0;
718 kvmppc_load_up_fpu();
719 break;
720 case MSR_VEC:
721#ifdef CONFIG_ALTIVEC
722 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
723 t->vscr = vcpu->arch.vscr;
724 t->vrsave = -1;
725 kvmppc_load_up_altivec();
726#endif
727 break;
728 case MSR_VSX:
729#ifdef CONFIG_VSX
730 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
731 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
732 kvmppc_load_up_vsx();
733#endif
734 break;
735 default:
736 BUG();
737 }
738
739 vcpu->arch.guest_owned_ext |= msr;
740
Alexander Grafa76f8492010-01-15 14:49:14 +0100741 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100742
743 return RESUME_GUEST;
744}
745
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000746int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
747 unsigned int exit_nr)
748{
749 int r = RESUME_HOST;
750
751 vcpu->stat.sum_exits++;
752
753 run->exit_reason = KVM_EXIT_UNKNOWN;
754 run->ready_for_interrupt_injection = 1;
755#ifdef EXIT_DEBUG
756 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | dec=0x%x | msr=0x%lx\n",
757 exit_nr, vcpu->arch.pc, vcpu->arch.fault_dear,
758 kvmppc_get_dec(vcpu), vcpu->arch.msr);
759#elif defined (EXIT_DEBUG_SIMPLE)
760 if ((exit_nr != 0x900) && (exit_nr != 0x500))
761 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | msr=0x%lx\n",
762 exit_nr, vcpu->arch.pc, vcpu->arch.fault_dear,
763 vcpu->arch.msr);
764#endif
765 kvm_resched(vcpu);
766 switch (exit_nr) {
767 case BOOK3S_INTERRUPT_INST_STORAGE:
768 vcpu->stat.pf_instruc++;
769 /* only care about PTEG not found errors, but leave NX alone */
Alexander Graff7adbba2010-01-15 14:49:13 +0100770 if (vcpu->arch.shadow_srr1 & 0x40000000) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000771 r = kvmppc_handle_pagefault(run, vcpu, vcpu->arch.pc, exit_nr);
772 vcpu->stat.sp_instruc++;
773 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
774 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
775 /*
776 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
777 * so we can't use the NX bit inside the guest. Let's cross our fingers,
778 * that no guest that needs the dcbz hack does NX.
779 */
780 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.pc, ~0xFFFULL);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100781 r = RESUME_GUEST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000782 } else {
Alexander Graff7adbba2010-01-15 14:49:13 +0100783 vcpu->arch.msr |= vcpu->arch.shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000784 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
785 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.pc, ~0xFFFULL);
786 r = RESUME_GUEST;
787 }
788 break;
789 case BOOK3S_INTERRUPT_DATA_STORAGE:
790 vcpu->stat.pf_storage++;
791 /* The only case we need to handle is missing shadow PTEs */
792 if (vcpu->arch.fault_dsisr & DSISR_NOHPTE) {
793 r = kvmppc_handle_pagefault(run, vcpu, vcpu->arch.fault_dear, exit_nr);
794 } else {
795 vcpu->arch.dear = vcpu->arch.fault_dear;
796 to_book3s(vcpu)->dsisr = vcpu->arch.fault_dsisr;
797 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
798 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.dear, ~0xFFFULL);
799 r = RESUME_GUEST;
800 }
801 break;
802 case BOOK3S_INTERRUPT_DATA_SEGMENT:
803 if (kvmppc_mmu_map_segment(vcpu, vcpu->arch.fault_dear) < 0) {
804 vcpu->arch.dear = vcpu->arch.fault_dear;
805 kvmppc_book3s_queue_irqprio(vcpu,
806 BOOK3S_INTERRUPT_DATA_SEGMENT);
807 }
808 r = RESUME_GUEST;
809 break;
810 case BOOK3S_INTERRUPT_INST_SEGMENT:
811 if (kvmppc_mmu_map_segment(vcpu, vcpu->arch.pc) < 0) {
812 kvmppc_book3s_queue_irqprio(vcpu,
813 BOOK3S_INTERRUPT_INST_SEGMENT);
814 }
815 r = RESUME_GUEST;
816 break;
817 /* We're good on these - the host merely wanted to get our attention */
818 case BOOK3S_INTERRUPT_DECREMENTER:
819 vcpu->stat.dec_exits++;
820 r = RESUME_GUEST;
821 break;
822 case BOOK3S_INTERRUPT_EXTERNAL:
823 vcpu->stat.ext_intr_exits++;
824 r = RESUME_GUEST;
825 break;
826 case BOOK3S_INTERRUPT_PROGRAM:
827 {
828 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100829 ulong flags;
830
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100831program_interrupt:
Alexander Graff7adbba2010-01-15 14:49:13 +0100832 flags = vcpu->arch.shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000833
834 if (vcpu->arch.msr & MSR_PR) {
835#ifdef EXIT_DEBUG
836 printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", vcpu->arch.pc, vcpu->arch.last_inst);
837#endif
838 if ((vcpu->arch.last_inst & 0xff0007ff) !=
839 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100840 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000841 r = RESUME_GUEST;
842 break;
843 }
844 }
845
846 vcpu->stat.emulated_inst_exits++;
847 er = kvmppc_emulate_instruction(run, vcpu);
848 switch (er) {
849 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100850 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000851 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100852 case EMULATE_AGAIN:
853 r = RESUME_GUEST;
854 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000855 case EMULATE_FAIL:
856 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
857 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100858 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000859 r = RESUME_GUEST;
860 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +0100861 case EMULATE_DO_MMIO:
862 run->exit_reason = KVM_EXIT_MMIO;
863 r = RESUME_HOST_NV;
864 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000865 default:
866 BUG();
867 }
868 break;
869 }
870 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafad0a0482010-03-24 21:48:30 +0100871 // XXX make user settable
872 if (vcpu->arch.osi_enabled &&
873 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
874 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
875 u64 *gprs = run->osi.gprs;
876 int i;
877
878 run->exit_reason = KVM_EXIT_OSI;
879 for (i = 0; i < 32; i++)
880 gprs[i] = kvmppc_get_gpr(vcpu, i);
881 vcpu->arch.osi_needed = 1;
882 r = RESUME_HOST_NV;
883
884 } else {
885 vcpu->stat.syscall_exits++;
886 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
887 r = RESUME_GUEST;
888 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000889 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000890 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000891 case BOOK3S_INTERRUPT_ALTIVEC:
892 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100893 {
894 int ext_msr = 0;
895
896 switch (exit_nr) {
897 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
898 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
899 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
900 }
901
902 switch (kvmppc_check_ext(vcpu, exit_nr)) {
903 case EMULATE_DONE:
904 /* everything ok - let's enable the ext */
905 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
906 break;
907 case EMULATE_FAIL:
908 /* we need to emulate this instruction */
909 goto program_interrupt;
910 break;
911 default:
912 /* nothing to worry about - go again */
913 break;
914 }
Alexander Graf180a34d2010-01-15 14:49:11 +0100915 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100916 }
Alexander Grafca7f4202010-03-24 21:48:28 +0100917 case BOOK3S_INTERRUPT_ALIGNMENT:
918 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
919 to_book3s(vcpu)->dsisr = kvmppc_alignment_dsisr(vcpu,
920 vcpu->arch.last_inst);
921 vcpu->arch.dear = kvmppc_alignment_dar(vcpu,
922 vcpu->arch.last_inst);
923 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
924 }
925 r = RESUME_GUEST;
926 break;
Alexander Graf180a34d2010-01-15 14:49:11 +0100927 case BOOK3S_INTERRUPT_MACHINE_CHECK:
928 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000929 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
930 r = RESUME_GUEST;
931 break;
932 default:
933 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +0100934 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
935 exit_nr, vcpu->arch.pc, vcpu->arch.shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000936 r = RESUME_HOST;
937 BUG();
938 break;
939 }
940
941
942 if (!(r & RESUME_HOST)) {
943 /* To avoid clobbering exit_reason, only check for signals if
944 * we aren't already exiting to userspace for some other
945 * reason. */
946 if (signal_pending(current)) {
947#ifdef EXIT_DEBUG
948 printk(KERN_EMERG "KVM: Going back to host\n");
949#endif
950 vcpu->stat.signal_exits++;
951 run->exit_reason = KVM_EXIT_INTR;
952 r = -EINTR;
953 } else {
954 /* In case an interrupt came in that was triggered
955 * from userspace (like DEC), we need to check what
956 * to inject now! */
957 kvmppc_core_deliver_interrupts(vcpu);
958 }
959 }
960
961#ifdef EXIT_DEBUG
962 printk(KERN_EMERG "KVM exit: vcpu=0x%p pc=0x%lx r=0x%x\n", vcpu, vcpu->arch.pc, r);
963#endif
964
965 return r;
966}
967
968int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
969{
970 return 0;
971}
972
973int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
974{
975 int i;
976
Alexander Grafa56cf342010-03-24 21:48:23 +0100977 vcpu_load(vcpu);
978
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000979 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100980 regs->cr = kvmppc_get_cr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000981 regs->ctr = vcpu->arch.ctr;
982 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100983 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000984 regs->msr = vcpu->arch.msr;
985 regs->srr0 = vcpu->arch.srr0;
986 regs->srr1 = vcpu->arch.srr1;
987 regs->pid = vcpu->arch.pid;
988 regs->sprg0 = vcpu->arch.sprg0;
989 regs->sprg1 = vcpu->arch.sprg1;
990 regs->sprg2 = vcpu->arch.sprg2;
991 regs->sprg3 = vcpu->arch.sprg3;
992 regs->sprg5 = vcpu->arch.sprg4;
993 regs->sprg6 = vcpu->arch.sprg5;
994 regs->sprg7 = vcpu->arch.sprg6;
995
996 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100997 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000998
Alexander Grafa56cf342010-03-24 21:48:23 +0100999 vcpu_put(vcpu);
1000
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001001 return 0;
1002}
1003
1004int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1005{
1006 int i;
1007
Alexander Grafa56cf342010-03-24 21:48:23 +01001008 vcpu_load(vcpu);
1009
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001010 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001011 kvmppc_set_cr(vcpu, regs->cr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001012 vcpu->arch.ctr = regs->ctr;
1013 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001014 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001015 kvmppc_set_msr(vcpu, regs->msr);
1016 vcpu->arch.srr0 = regs->srr0;
1017 vcpu->arch.srr1 = regs->srr1;
1018 vcpu->arch.sprg0 = regs->sprg0;
1019 vcpu->arch.sprg1 = regs->sprg1;
1020 vcpu->arch.sprg2 = regs->sprg2;
1021 vcpu->arch.sprg3 = regs->sprg3;
1022 vcpu->arch.sprg5 = regs->sprg4;
1023 vcpu->arch.sprg6 = regs->sprg5;
1024 vcpu->arch.sprg7 = regs->sprg6;
1025
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001026 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1027 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001028
Alexander Grafa56cf342010-03-24 21:48:23 +01001029 vcpu_put(vcpu);
1030
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001031 return 0;
1032}
1033
1034int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1035 struct kvm_sregs *sregs)
1036{
Alexander Grafe15a1132009-11-30 03:02:02 +00001037 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1038 int i;
1039
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001040 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +00001041
1042 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1043 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1044 for (i = 0; i < 64; i++) {
1045 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
1046 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1047 }
1048 } else {
1049 for (i = 0; i < 16; i++) {
1050 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1051 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1052 }
1053 for (i = 0; i < 8; i++) {
1054 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1055 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1056 }
1057 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001058 return 0;
1059}
1060
1061int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1062 struct kvm_sregs *sregs)
1063{
Alexander Grafe15a1132009-11-30 03:02:02 +00001064 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1065 int i;
1066
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001067 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001068
1069 vcpu3s->sdr1 = sregs->u.s.sdr1;
1070 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1071 for (i = 0; i < 64; i++) {
1072 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1073 sregs->u.s.ppc64.slb[i].slbe);
1074 }
1075 } else {
1076 for (i = 0; i < 16; i++) {
1077 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1078 }
1079 for (i = 0; i < 8; i++) {
1080 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1081 (u32)sregs->u.s.ppc32.ibat[i]);
1082 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1083 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1084 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1085 (u32)sregs->u.s.ppc32.dbat[i]);
1086 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1087 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1088 }
1089 }
1090
1091 /* Flush the MMU after messing with the segments */
1092 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001093 return 0;
1094}
1095
1096int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1097{
1098 return -ENOTSUPP;
1099}
1100
1101int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1102{
1103 return -ENOTSUPP;
1104}
1105
1106int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1107 struct kvm_translation *tr)
1108{
1109 return 0;
1110}
1111
1112/*
1113 * Get (and clear) the dirty memory log for a memory slot.
1114 */
1115int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1116 struct kvm_dirty_log *log)
1117{
1118 struct kvm_memory_slot *memslot;
1119 struct kvm_vcpu *vcpu;
1120 ulong ga, ga_end;
1121 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001122 int r;
1123 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001124
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001125 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001126
1127 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1128 if (r)
1129 goto out;
1130
1131 /* If nothing is dirty, don't bother messing with page tables. */
1132 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001133 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001134
1135 ga = memslot->base_gfn << PAGE_SHIFT;
1136 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1137
1138 kvm_for_each_vcpu(n, vcpu, kvm)
1139 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1140
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001141 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001142 memset(memslot->dirty_bitmap, 0, n);
1143 }
1144
1145 r = 0;
1146out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001147 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001148 return r;
1149}
1150
1151int kvmppc_core_check_processor_compat(void)
1152{
1153 return 0;
1154}
1155
1156struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1157{
1158 struct kvmppc_vcpu_book3s *vcpu_book3s;
1159 struct kvm_vcpu *vcpu;
1160 int err;
1161
Alexander Graf032c3402010-02-19 12:24:33 +01001162 vcpu_book3s = vmalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001163 if (!vcpu_book3s) {
1164 err = -ENOMEM;
1165 goto out;
1166 }
Alexander Graf7e821d32010-02-22 16:52:08 +01001167 memset(vcpu_book3s, 0, sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001168
1169 vcpu = &vcpu_book3s->vcpu;
1170 err = kvm_vcpu_init(vcpu, kvm, id);
1171 if (err)
1172 goto free_vcpu;
1173
1174 vcpu->arch.host_retip = kvm_return_point;
1175 vcpu->arch.host_msr = mfmsr();
1176 /* default to book3s_64 (970fx) */
1177 vcpu->arch.pvr = 0x3C0301;
1178 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1179 vcpu_book3s->slb_nr = 64;
1180
1181 /* remember where some real-mode handlers are */
1182 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1183 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1184 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf021ec9c2010-01-08 02:58:06 +01001185 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001186
1187 vcpu->arch.shadow_msr = MSR_USER64;
1188
1189 err = __init_new_context();
1190 if (err < 0)
1191 goto free_vcpu;
1192 vcpu_book3s->context_id = err;
1193
1194 vcpu_book3s->vsid_max = ((vcpu_book3s->context_id + 1) << USER_ESID_BITS) - 1;
1195 vcpu_book3s->vsid_first = vcpu_book3s->context_id << USER_ESID_BITS;
1196 vcpu_book3s->vsid_next = vcpu_book3s->vsid_first;
1197
1198 return vcpu;
1199
1200free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001201 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001202out:
1203 return ERR_PTR(err);
1204}
1205
1206void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1207{
1208 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1209
1210 __destroy_context(vcpu_book3s->context_id);
1211 kvm_vcpu_uninit(vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001212 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001213}
1214
1215extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1216int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1217{
1218 int ret;
Alexander Graf180a34d2010-01-15 14:49:11 +01001219 struct thread_struct ext_bkp;
Alexander Grafa2b07662010-03-24 21:48:31 +01001220#ifdef CONFIG_ALTIVEC
Alexander Graf180a34d2010-01-15 14:49:11 +01001221 bool save_vec = current->thread.used_vr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001222#endif
1223#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +01001224 bool save_vsx = current->thread.used_vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001225#endif
Alexander Graf180a34d2010-01-15 14:49:11 +01001226 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001227
1228 /* No need to go into the guest when all we do is going out */
1229 if (signal_pending(current)) {
1230 kvm_run->exit_reason = KVM_EXIT_INTR;
1231 return -EINTR;
1232 }
1233
Alexander Graf180a34d2010-01-15 14:49:11 +01001234 /* Save FPU state in stack */
1235 if (current->thread.regs->msr & MSR_FP)
1236 giveup_fpu(current);
1237 memcpy(ext_bkp.fpr, current->thread.fpr, sizeof(current->thread.fpr));
1238 ext_bkp.fpscr = current->thread.fpscr;
1239 ext_bkp.fpexc_mode = current->thread.fpexc_mode;
1240
1241#ifdef CONFIG_ALTIVEC
1242 /* Save Altivec state in stack */
1243 if (save_vec) {
1244 if (current->thread.regs->msr & MSR_VEC)
1245 giveup_altivec(current);
1246 memcpy(ext_bkp.vr, current->thread.vr, sizeof(ext_bkp.vr));
1247 ext_bkp.vscr = current->thread.vscr;
1248 ext_bkp.vrsave = current->thread.vrsave;
1249 }
1250 ext_bkp.used_vr = current->thread.used_vr;
1251#endif
1252
1253#ifdef CONFIG_VSX
1254 /* Save VSX state in stack */
1255 if (save_vsx && (current->thread.regs->msr & MSR_VSX))
1256 __giveup_vsx(current);
1257 ext_bkp.used_vsr = current->thread.used_vsr;
1258#endif
1259
1260 /* Remember the MSR with disabled extensions */
1261 ext_msr = current->thread.regs->msr;
1262
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001263 /* XXX we get called with irq disabled - change that! */
1264 local_irq_enable();
1265
Alexander Grafd1bab742010-02-19 11:00:35 +01001266 /* Preload FPU if it's enabled */
1267 if (vcpu->arch.msr & MSR_FP)
1268 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1269
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001270 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1271
1272 local_irq_disable();
1273
Alexander Graf180a34d2010-01-15 14:49:11 +01001274 current->thread.regs->msr = ext_msr;
1275
1276 /* Make sure we save the guest FPU/Altivec/VSX state */
1277 kvmppc_giveup_ext(vcpu, MSR_FP);
1278 kvmppc_giveup_ext(vcpu, MSR_VEC);
1279 kvmppc_giveup_ext(vcpu, MSR_VSX);
1280
1281 /* Restore FPU state from stack */
1282 memcpy(current->thread.fpr, ext_bkp.fpr, sizeof(ext_bkp.fpr));
1283 current->thread.fpscr = ext_bkp.fpscr;
1284 current->thread.fpexc_mode = ext_bkp.fpexc_mode;
1285
1286#ifdef CONFIG_ALTIVEC
1287 /* Restore Altivec state from stack */
1288 if (save_vec && current->thread.used_vr) {
1289 memcpy(current->thread.vr, ext_bkp.vr, sizeof(ext_bkp.vr));
1290 current->thread.vscr = ext_bkp.vscr;
1291 current->thread.vrsave= ext_bkp.vrsave;
1292 }
1293 current->thread.used_vr = ext_bkp.used_vr;
1294#endif
1295
1296#ifdef CONFIG_VSX
1297 current->thread.used_vsr = ext_bkp.used_vsr;
1298#endif
1299
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001300 return ret;
1301}
1302
1303static int kvmppc_book3s_init(void)
1304{
1305 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), THIS_MODULE);
1306}
1307
1308static void kvmppc_book3s_exit(void)
1309{
1310 kvm_exit();
1311}
1312
1313module_init(kvmppc_book3s_init);
1314module_exit(kvmppc_book3s_exit);