blob: f66de7e518f7b8ec53b1b040f39a51b2fc835cf8 [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 Graf07b09072010-04-16 00:11:53 +020043/* Some compatibility defines */
44#ifdef CONFIG_PPC_BOOK3S_32
45#define MSR_USER32 MSR_USER
46#define MSR_USER64 MSR_USER
47#define HW_PAGE_SIZE PAGE_SIZE
48#endif
49
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000050struct kvm_stats_debugfs_item debugfs_entries[] = {
51 { "exits", VCPU_STAT(sum_exits) },
52 { "mmio", VCPU_STAT(mmio_exits) },
53 { "sig", VCPU_STAT(signal_exits) },
54 { "sysc", VCPU_STAT(syscall_exits) },
55 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
56 { "dec", VCPU_STAT(dec_exits) },
57 { "ext_intr", VCPU_STAT(ext_intr_exits) },
58 { "queue_intr", VCPU_STAT(queue_intr) },
59 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
60 { "pf_storage", VCPU_STAT(pf_storage) },
61 { "sp_storage", VCPU_STAT(sp_storage) },
62 { "pf_instruc", VCPU_STAT(pf_instruc) },
63 { "sp_instruc", VCPU_STAT(sp_instruc) },
64 { "ld", VCPU_STAT(ld) },
65 { "ld_slow", VCPU_STAT(ld_slow) },
66 { "st", VCPU_STAT(st) },
67 { "st_slow", VCPU_STAT(st_slow) },
68 { NULL }
69};
70
71void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
72{
73}
74
75void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
76{
77}
78
79void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
80{
Alexander Grafc7f38f42010-04-16 00:11:40 +020081#ifdef CONFIG_PPC_BOOK3S_64
82 memcpy(to_svcpu(vcpu)->slb, to_book3s(vcpu)->slb_shadow, sizeof(to_svcpu(vcpu)->slb));
83 memcpy(&get_paca()->shadow_vcpu, to_book3s(vcpu)->shadow_vcpu,
Alexander Graf7e57cba2010-01-08 02:58:03 +010084 sizeof(get_paca()->shadow_vcpu));
Alexander Grafc7f38f42010-04-16 00:11:40 +020085 to_svcpu(vcpu)->slb_max = to_book3s(vcpu)->slb_shadow_max;
86#endif
87
88#ifdef CONFIG_PPC_BOOK3S_32
89 current->thread.kvm_shadow_vcpu = to_book3s(vcpu)->shadow_vcpu;
90#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000091}
92
93void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
94{
Alexander Grafc7f38f42010-04-16 00:11:40 +020095#ifdef CONFIG_PPC_BOOK3S_64
96 memcpy(to_book3s(vcpu)->slb_shadow, to_svcpu(vcpu)->slb, sizeof(to_svcpu(vcpu)->slb));
97 memcpy(to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
Alexander Graf7e57cba2010-01-08 02:58:03 +010098 sizeof(get_paca()->shadow_vcpu));
Alexander Grafc7f38f42010-04-16 00:11:40 +020099 to_book3s(vcpu)->slb_shadow_max = to_svcpu(vcpu)->slb_max;
100#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100101
102 kvmppc_giveup_ext(vcpu, MSR_FP);
103 kvmppc_giveup_ext(vcpu, MSR_VEC);
104 kvmppc_giveup_ext(vcpu, MSR_VSX);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000105}
106
Alexander Graf0bb1fb72009-12-21 20:21:25 +0100107#if defined(EXIT_DEBUG)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000108static u32 kvmppc_get_dec(struct kvm_vcpu *vcpu)
109{
110 u64 jd = mftb() - vcpu->arch.dec_jiffies;
111 return vcpu->arch.dec - jd;
112}
113#endif
114
Alexander Grafa76f8492010-01-15 14:49:14 +0100115static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
116{
117 vcpu->arch.shadow_msr = vcpu->arch.msr;
118 /* Guest MSR values */
119 vcpu->arch.shadow_msr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE |
120 MSR_BE | MSR_DE;
121 /* Process MSR values */
122 vcpu->arch.shadow_msr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR |
123 MSR_EE;
124 /* External providers the guest reserved */
125 vcpu->arch.shadow_msr |= (vcpu->arch.msr & vcpu->arch.guest_owned_ext);
126 /* 64-bit Process MSR values */
127#ifdef CONFIG_PPC_BOOK3S_64
128 vcpu->arch.shadow_msr |= MSR_ISF | MSR_HV;
129#endif
130}
131
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000132void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
133{
134 ulong old_msr = vcpu->arch.msr;
135
136#ifdef EXIT_DEBUG
137 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
138#endif
Alexander Grafa76f8492010-01-15 14:49:14 +0100139
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000140 msr &= to_book3s(vcpu)->msr_mask;
141 vcpu->arch.msr = msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100142 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000143
144 if (msr & (MSR_WE|MSR_POW)) {
145 if (!vcpu->arch.pending_exceptions) {
146 kvm_vcpu_block(vcpu);
147 vcpu->stat.halt_wakeup++;
148 }
149 }
150
151 if (((vcpu->arch.msr & (MSR_IR|MSR_DR)) != (old_msr & (MSR_IR|MSR_DR))) ||
152 (vcpu->arch.msr & MSR_PR) != (old_msr & MSR_PR)) {
Alexander Graf3eeafd72010-03-24 21:48:17 +0100153 bool dr = (vcpu->arch.msr & MSR_DR) ? true : false;
154 bool ir = (vcpu->arch.msr & MSR_IR) ? true : false;
155
156 /* Flush split mode PTEs */
157 if (dr != ir)
158 kvmppc_mmu_pte_vflush(vcpu, VSID_SPLIT_MASK,
159 VSID_SPLIT_MASK);
160
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000161 kvmppc_mmu_flush_segments(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +0200162 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000163 }
Alexander Grafd1bab742010-02-19 11:00:35 +0100164
165 /* Preload FPU if it's enabled */
166 if (vcpu->arch.msr & MSR_FP)
167 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000168}
169
170void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
171{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200172 vcpu->arch.srr0 = kvmppc_get_pc(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000173 vcpu->arch.srr1 = vcpu->arch.msr | flags;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200174 kvmppc_set_pc(vcpu, to_book3s(vcpu)->hior + vec);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000175 vcpu->arch.mmu.reset_msr(vcpu);
176}
177
Alexander Graf583617b2009-12-21 20:21:23 +0100178static int kvmppc_book3s_vec2irqprio(unsigned int vec)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000179{
180 unsigned int prio;
181
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000182 switch (vec) {
183 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
184 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
185 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
186 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
187 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
188 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
189 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
190 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
191 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
192 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
193 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
194 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
195 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
196 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
197 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
198 default: prio = BOOK3S_IRQPRIO_MAX; break;
199 }
200
Alexander Graf583617b2009-12-21 20:21:23 +0100201 return prio;
202}
203
Alexander Graf7706664d2009-12-21 20:21:24 +0100204static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
205 unsigned int vec)
206{
207 clear_bit(kvmppc_book3s_vec2irqprio(vec),
208 &vcpu->arch.pending_exceptions);
209}
210
Alexander Graf583617b2009-12-21 20:21:23 +0100211void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
212{
213 vcpu->stat.queue_intr++;
214
215 set_bit(kvmppc_book3s_vec2irqprio(vec),
216 &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000217#ifdef EXIT_DEBUG
218 printk(KERN_INFO "Queueing interrupt %x\n", vec);
219#endif
220}
221
222
Alexander Graf25a8a022010-01-08 02:58:07 +0100223void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000224{
Alexander Graf25a8a022010-01-08 02:58:07 +0100225 to_book3s(vcpu)->prog_flags = flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000226 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM);
227}
228
229void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
230{
231 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
232}
233
234int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
235{
236 return test_bit(BOOK3S_INTERRUPT_DECREMENTER >> 7, &vcpu->arch.pending_exceptions);
237}
238
Alexander Graf7706664d2009-12-21 20:21:24 +0100239void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
240{
241 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
242}
243
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000244void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
245 struct kvm_interrupt *irq)
246{
247 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
248}
249
Alexander Graf18978762010-03-24 21:48:18 +0100250void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
251 struct kvm_interrupt *irq)
252{
253 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
254}
255
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000256int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
257{
258 int deliver = 1;
259 int vec = 0;
Alexander Graf25a8a022010-01-08 02:58:07 +0100260 ulong flags = 0ULL;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000261
262 switch (priority) {
263 case BOOK3S_IRQPRIO_DECREMENTER:
264 deliver = vcpu->arch.msr & MSR_EE;
265 vec = BOOK3S_INTERRUPT_DECREMENTER;
266 break;
267 case BOOK3S_IRQPRIO_EXTERNAL:
268 deliver = vcpu->arch.msr & MSR_EE;
269 vec = BOOK3S_INTERRUPT_EXTERNAL;
270 break;
271 case BOOK3S_IRQPRIO_SYSTEM_RESET:
272 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
273 break;
274 case BOOK3S_IRQPRIO_MACHINE_CHECK:
275 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
276 break;
277 case BOOK3S_IRQPRIO_DATA_STORAGE:
278 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
279 break;
280 case BOOK3S_IRQPRIO_INST_STORAGE:
281 vec = BOOK3S_INTERRUPT_INST_STORAGE;
282 break;
283 case BOOK3S_IRQPRIO_DATA_SEGMENT:
284 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
285 break;
286 case BOOK3S_IRQPRIO_INST_SEGMENT:
287 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
288 break;
289 case BOOK3S_IRQPRIO_ALIGNMENT:
290 vec = BOOK3S_INTERRUPT_ALIGNMENT;
291 break;
292 case BOOK3S_IRQPRIO_PROGRAM:
293 vec = BOOK3S_INTERRUPT_PROGRAM;
Alexander Graf25a8a022010-01-08 02:58:07 +0100294 flags = to_book3s(vcpu)->prog_flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000295 break;
296 case BOOK3S_IRQPRIO_VSX:
297 vec = BOOK3S_INTERRUPT_VSX;
298 break;
299 case BOOK3S_IRQPRIO_ALTIVEC:
300 vec = BOOK3S_INTERRUPT_ALTIVEC;
301 break;
302 case BOOK3S_IRQPRIO_FP_UNAVAIL:
303 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
304 break;
305 case BOOK3S_IRQPRIO_SYSCALL:
306 vec = BOOK3S_INTERRUPT_SYSCALL;
307 break;
308 case BOOK3S_IRQPRIO_DEBUG:
309 vec = BOOK3S_INTERRUPT_TRACE;
310 break;
311 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
312 vec = BOOK3S_INTERRUPT_PERFMON;
313 break;
314 default:
315 deliver = 0;
316 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
317 break;
318 }
319
320#if 0
321 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
322#endif
323
324 if (deliver)
Alexander Graf25a8a022010-01-08 02:58:07 +0100325 kvmppc_inject_interrupt(vcpu, vec, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000326
327 return deliver;
328}
329
330void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
331{
332 unsigned long *pending = &vcpu->arch.pending_exceptions;
333 unsigned int priority;
334
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000335#ifdef EXIT_DEBUG
336 if (vcpu->arch.pending_exceptions)
337 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
338#endif
339 priority = __ffs(*pending);
Alexander Grafada7ba12010-04-16 00:11:56 +0200340 while (priority < BOOK3S_IRQPRIO_MAX) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100341 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
342 (priority != BOOK3S_IRQPRIO_DECREMENTER)) {
343 /* DEC interrupts get cleared by mtdec */
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000344 clear_bit(priority, &vcpu->arch.pending_exceptions);
345 break;
346 }
347
348 priority = find_next_bit(pending,
349 BITS_PER_BYTE * sizeof(*pending),
350 priority + 1);
351 }
352}
353
354void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
355{
Alexander Grafe15a1132009-11-30 03:02:02 +0000356 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000357 vcpu->arch.pvr = pvr;
Alexander Graf07b09072010-04-16 00:11:53 +0200358#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000359 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
360 kvmppc_mmu_book3s_64_init(vcpu);
361 to_book3s(vcpu)->hior = 0xfff00000;
362 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
Alexander Graf07b09072010-04-16 00:11:53 +0200363 } else
364#endif
365 {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000366 kvmppc_mmu_book3s_32_init(vcpu);
367 to_book3s(vcpu)->hior = 0;
368 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
369 }
370
371 /* If we are in hypervisor level on 970, we can tell the CPU to
372 * treat DCBZ as 32 bytes store */
373 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
374 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
375 !strcmp(cur_cpu_spec->platform, "ppc970"))
376 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
377
Alexander Graf05b0ab12010-03-24 21:48:37 +0100378 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
379 really needs them in a VM on Cell and force disable them. */
380 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
381 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
Alexander Graf07b09072010-04-16 00:11:53 +0200382
383#ifdef CONFIG_PPC_BOOK3S_32
384 /* 32 bit Book3S always has 32 byte dcbz */
385 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
386#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000387}
388
389/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
390 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
391 * emulate 32 bytes dcbz length.
392 *
393 * The Book3s_64 inventors also realized this case and implemented a special bit
394 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
395 *
396 * My approach here is to patch the dcbz instruction on executing pages.
397 */
398static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
399{
Alexander Graf9fb244a2010-03-24 21:48:32 +0100400 struct page *hpage;
401 u64 hpage_offset;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000402 u32 *page;
403 int i;
404
Alexander Graf9fb244a2010-03-24 21:48:32 +0100405 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
406 if (is_error_page(hpage))
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000407 return;
408
Alexander Graf9fb244a2010-03-24 21:48:32 +0100409 hpage_offset = pte->raddr & ~PAGE_MASK;
410 hpage_offset &= ~0xFFFULL;
411 hpage_offset /= 4;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000412
Alexander Graf9fb244a2010-03-24 21:48:32 +0100413 get_page(hpage);
414 page = kmap_atomic(hpage, KM_USER0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000415
Alexander Graf9fb244a2010-03-24 21:48:32 +0100416 /* patch dcbz into reserved instruction, so we trap */
417 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
418 if ((page[i] & 0xff0007ff) == INS_DCBZ)
419 page[i] &= 0xfffffff7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000420
Alexander Graf9fb244a2010-03-24 21:48:32 +0100421 kunmap_atomic(page, KM_USER0);
422 put_page(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000423}
424
425static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
426 struct kvmppc_pte *pte)
427{
428 int relocated = (vcpu->arch.msr & (data ? MSR_DR : MSR_IR));
429 int r;
430
431 if (relocated) {
432 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
433 } else {
434 pte->eaddr = eaddr;
435 pte->raddr = eaddr & 0xffffffff;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100436 pte->vpage = VSID_REAL | eaddr >> 12;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000437 pte->may_read = true;
438 pte->may_write = true;
439 pte->may_execute = true;
440 r = 0;
441 }
442
443 return r;
444}
445
446static hva_t kvmppc_bad_hva(void)
447{
448 return PAGE_OFFSET;
449}
450
451static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
452 bool read)
453{
454 hva_t hpage;
455
456 if (read && !pte->may_read)
457 goto err;
458
459 if (!read && !pte->may_write)
460 goto err;
461
462 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
463 if (kvm_is_error_hva(hpage))
464 goto err;
465
466 return hpage | (pte->raddr & ~PAGE_MASK);
467err:
468 return kvmppc_bad_hva();
469}
470
Alexander Graf5467a972010-02-19 11:00:38 +0100471int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
472 bool data)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000473{
474 struct kvmppc_pte pte;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000475
476 vcpu->stat.st++;
477
Alexander Graf5467a972010-02-19 11:00:38 +0100478 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
Alexander Graf9fb244a2010-03-24 21:48:32 +0100479 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100480
481 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000482
Alexander Graf9fb244a2010-03-24 21:48:32 +0100483 if (!pte.may_write)
484 return -EPERM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000485
Alexander Graf9fb244a2010-03-24 21:48:32 +0100486 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
487 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000488
Alexander Graf5467a972010-02-19 11:00:38 +0100489 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000490}
491
Alexander Graf5467a972010-02-19 11:00:38 +0100492int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000493 bool data)
494{
495 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100496 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000497
498 vcpu->stat.ld++;
499
Alexander Graf5467a972010-02-19 11:00:38 +0100500 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
501 goto nopte;
502
503 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000504
505 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
506 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100507 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000508
509 if (copy_from_user(ptr, (void __user *)hva, size)) {
510 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100511 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000512 }
513
Alexander Graf5467a972010-02-19 11:00:38 +0100514 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000515
Alexander Graf5467a972010-02-19 11:00:38 +0100516nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000517 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100518mmio:
519 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000520}
521
522static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
523{
524 return kvm_is_visible_gfn(vcpu->kvm, gfn);
525}
526
527int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
528 ulong eaddr, int vec)
529{
530 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
531 int r = RESUME_GUEST;
532 int relocated;
533 int page_found = 0;
534 struct kvmppc_pte pte;
535 bool is_mmio = false;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100536 bool dr = (vcpu->arch.msr & MSR_DR) ? true : false;
537 bool ir = (vcpu->arch.msr & MSR_IR) ? true : false;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000538
Alexander Graf3eeafd72010-03-24 21:48:17 +0100539 relocated = data ? dr : ir;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000540
541 /* Resolve real address if translation turned on */
542 if (relocated) {
543 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
544 } else {
545 pte.may_execute = true;
546 pte.may_read = true;
547 pte.may_write = true;
548 pte.raddr = eaddr & 0xffffffff;
549 pte.eaddr = eaddr;
550 pte.vpage = eaddr >> 12;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100551 }
552
553 switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
554 case 0:
555 pte.vpage |= VSID_REAL;
556 break;
557 case MSR_DR:
558 pte.vpage |= VSID_REAL_DR;
559 break;
560 case MSR_IR:
561 pte.vpage |= VSID_REAL_IR;
562 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000563 }
564
565 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
566 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
567 /*
568 * If we do the dcbz hack, we have to NX on every execution,
569 * so we can patch the executing code. This renders our guest
570 * NX-less.
571 */
572 pte.may_execute = !data;
573 }
574
575 if (page_found == -ENOENT) {
576 /* Page not found in guest PTE entries */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200577 vcpu->arch.dear = kvmppc_get_fault_dar(vcpu);
578 to_book3s(vcpu)->dsisr = to_svcpu(vcpu)->fault_dsisr;
579 vcpu->arch.msr |= (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000580 kvmppc_book3s_queue_irqprio(vcpu, vec);
581 } else if (page_found == -EPERM) {
582 /* Storage protection */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200583 vcpu->arch.dear = kvmppc_get_fault_dar(vcpu);
584 to_book3s(vcpu)->dsisr = to_svcpu(vcpu)->fault_dsisr & ~DSISR_NOHPTE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000585 to_book3s(vcpu)->dsisr |= DSISR_PROTFAULT;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200586 vcpu->arch.msr |= (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000587 kvmppc_book3s_queue_irqprio(vcpu, vec);
588 } else if (page_found == -EINVAL) {
589 /* Page not found in guest SLB */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200590 vcpu->arch.dear = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000591 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
592 } else if (!is_mmio &&
593 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
594 /* The guest's PTE is not mapped yet. Map on the host */
595 kvmppc_mmu_map_page(vcpu, &pte);
596 if (data)
597 vcpu->stat.sp_storage++;
598 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
599 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
600 kvmppc_patch_dcbz(vcpu, &pte);
601 } else {
602 /* MMIO */
603 vcpu->stat.mmio_exits++;
604 vcpu->arch.paddr_accessed = pte.raddr;
605 r = kvmppc_emulate_mmio(run, vcpu);
606 if ( r == RESUME_HOST_NV )
607 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000608 }
609
610 return r;
611}
612
Alexander Graf180a34d2010-01-15 14:49:11 +0100613static inline int get_fpr_index(int i)
614{
615#ifdef CONFIG_VSX
616 i *= 2;
617#endif
618 return i;
619}
620
621/* Give up external provider (FPU, Altivec, VSX) */
Alexander Grafaba3bd72010-02-19 11:00:39 +0100622void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
Alexander Graf180a34d2010-01-15 14:49:11 +0100623{
624 struct thread_struct *t = &current->thread;
625 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100626#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100627 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100628#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100629 u64 *thread_fpr = (u64*)t->fpr;
630 int i;
631
632 if (!(vcpu->arch.guest_owned_ext & msr))
633 return;
634
635#ifdef DEBUG_EXT
636 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
637#endif
638
639 switch (msr) {
640 case MSR_FP:
641 giveup_fpu(current);
642 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
643 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
644
645 vcpu->arch.fpscr = t->fpscr.val;
646 break;
647 case MSR_VEC:
648#ifdef CONFIG_ALTIVEC
649 giveup_altivec(current);
650 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
651 vcpu->arch.vscr = t->vscr;
652#endif
653 break;
654 case MSR_VSX:
655#ifdef CONFIG_VSX
656 __giveup_vsx(current);
657 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
658 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
659#endif
660 break;
661 default:
662 BUG();
663 }
664
665 vcpu->arch.guest_owned_ext &= ~msr;
666 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100667 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100668}
669
Alexander Graf89632212010-03-24 21:48:21 +0100670static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100671{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200672 ulong srr0 = kvmppc_get_pc(vcpu);
673 u32 last_inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100674 int ret;
675
Alexander Grafc7f38f42010-04-16 00:11:40 +0200676 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100677 if (ret == -ENOENT) {
678 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 33, 33, 1);
679 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 34, 36, 0);
680 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 42, 47, 0);
681 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
Alexander Graf89632212010-03-24 21:48:21 +0100682 return EMULATE_AGAIN;
683 }
684
685 return EMULATE_DONE;
686}
687
688static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
689{
690
691 /* Need to do paired single emulation? */
692 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
693 return EMULATE_DONE;
694
695 /* Read out the instruction */
696 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100697 /* Need to emulate */
698 return EMULATE_FAIL;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100699
700 return EMULATE_AGAIN;
701}
702
Alexander Graf180a34d2010-01-15 14:49:11 +0100703/* Handle external providers (FPU, Altivec, VSX) */
704static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
705 ulong msr)
706{
707 struct thread_struct *t = &current->thread;
708 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100709#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100710 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100711#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100712 u64 *thread_fpr = (u64*)t->fpr;
713 int i;
714
Alexander Graf3c402a72010-02-19 11:00:32 +0100715 /* When we have paired singles, we emulate in software */
716 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
717 return RESUME_GUEST;
718
Alexander Graf180a34d2010-01-15 14:49:11 +0100719 if (!(vcpu->arch.msr & msr)) {
720 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
721 return RESUME_GUEST;
722 }
723
Alexander Grafc2453692010-03-24 21:48:22 +0100724 /* We already own the ext */
725 if (vcpu->arch.guest_owned_ext & msr) {
726 return RESUME_GUEST;
727 }
728
Alexander Graf180a34d2010-01-15 14:49:11 +0100729#ifdef DEBUG_EXT
730 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
731#endif
732
733 current->thread.regs->msr |= msr;
734
735 switch (msr) {
736 case MSR_FP:
737 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
738 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
739
740 t->fpscr.val = vcpu->arch.fpscr;
741 t->fpexc_mode = 0;
742 kvmppc_load_up_fpu();
743 break;
744 case MSR_VEC:
745#ifdef CONFIG_ALTIVEC
746 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
747 t->vscr = vcpu->arch.vscr;
748 t->vrsave = -1;
749 kvmppc_load_up_altivec();
750#endif
751 break;
752 case MSR_VSX:
753#ifdef CONFIG_VSX
754 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
755 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
756 kvmppc_load_up_vsx();
757#endif
758 break;
759 default:
760 BUG();
761 }
762
763 vcpu->arch.guest_owned_ext |= msr;
764
Alexander Grafa76f8492010-01-15 14:49:14 +0100765 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100766
767 return RESUME_GUEST;
768}
769
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000770int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
771 unsigned int exit_nr)
772{
773 int r = RESUME_HOST;
774
775 vcpu->stat.sum_exits++;
776
777 run->exit_reason = KVM_EXIT_UNKNOWN;
778 run->ready_for_interrupt_injection = 1;
779#ifdef EXIT_DEBUG
780 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | dec=0x%x | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200781 exit_nr, kvmppc_get_pc(vcpu), kvmppc_get_fault_dar(vcpu),
782 kvmppc_get_dec(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000783#elif defined (EXIT_DEBUG_SIMPLE)
784 if ((exit_nr != 0x900) && (exit_nr != 0x500))
785 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200786 exit_nr, kvmppc_get_pc(vcpu), kvmppc_get_fault_dar(vcpu),
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000787 vcpu->arch.msr);
788#endif
789 kvm_resched(vcpu);
790 switch (exit_nr) {
791 case BOOK3S_INTERRUPT_INST_STORAGE:
792 vcpu->stat.pf_instruc++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200793
794#ifdef CONFIG_PPC_BOOK3S_32
795 /* We set segments as unused segments when invalidating them. So
796 * treat the respective fault as segment fault. */
797 if (to_svcpu(vcpu)->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT]
798 == SR_INVALID) {
799 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
800 r = RESUME_GUEST;
801 break;
802 }
803#endif
804
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000805 /* only care about PTEG not found errors, but leave NX alone */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200806 if (to_svcpu(vcpu)->shadow_srr1 & 0x40000000) {
807 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000808 vcpu->stat.sp_instruc++;
809 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
810 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
811 /*
812 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
813 * so we can't use the NX bit inside the guest. Let's cross our fingers,
814 * that no guest that needs the dcbz hack does NX.
815 */
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200816 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100817 r = RESUME_GUEST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000818 } else {
Alexander Grafc7f38f42010-04-16 00:11:40 +0200819 vcpu->arch.msr |= to_svcpu(vcpu)->shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000820 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200821 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000822 r = RESUME_GUEST;
823 }
824 break;
825 case BOOK3S_INTERRUPT_DATA_STORAGE:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200826 {
827 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000828 vcpu->stat.pf_storage++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200829
830#ifdef CONFIG_PPC_BOOK3S_32
831 /* We set segments as unused segments when invalidating them. So
832 * treat the respective fault as segment fault. */
833 if ((to_svcpu(vcpu)->sr[dar >> SID_SHIFT]) == SR_INVALID) {
834 kvmppc_mmu_map_segment(vcpu, dar);
835 r = RESUME_GUEST;
836 break;
837 }
838#endif
839
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000840 /* The only case we need to handle is missing shadow PTEs */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200841 if (to_svcpu(vcpu)->fault_dsisr & DSISR_NOHPTE) {
842 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000843 } else {
Alexander Grafc7f38f42010-04-16 00:11:40 +0200844 vcpu->arch.dear = dar;
845 to_book3s(vcpu)->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000846 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200847 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.dear, ~0xFFFUL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000848 r = RESUME_GUEST;
849 }
850 break;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200851 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000852 case BOOK3S_INTERRUPT_DATA_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200853 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
854 vcpu->arch.dear = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000855 kvmppc_book3s_queue_irqprio(vcpu,
856 BOOK3S_INTERRUPT_DATA_SEGMENT);
857 }
858 r = RESUME_GUEST;
859 break;
860 case BOOK3S_INTERRUPT_INST_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200861 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000862 kvmppc_book3s_queue_irqprio(vcpu,
863 BOOK3S_INTERRUPT_INST_SEGMENT);
864 }
865 r = RESUME_GUEST;
866 break;
867 /* We're good on these - the host merely wanted to get our attention */
868 case BOOK3S_INTERRUPT_DECREMENTER:
869 vcpu->stat.dec_exits++;
870 r = RESUME_GUEST;
871 break;
872 case BOOK3S_INTERRUPT_EXTERNAL:
873 vcpu->stat.ext_intr_exits++;
874 r = RESUME_GUEST;
875 break;
Alexander Graf7fdaec92010-04-20 02:49:47 +0200876 case BOOK3S_INTERRUPT_PERFMON:
877 r = RESUME_GUEST;
878 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000879 case BOOK3S_INTERRUPT_PROGRAM:
880 {
881 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100882 ulong flags;
883
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100884program_interrupt:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200885 flags = to_svcpu(vcpu)->shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000886
887 if (vcpu->arch.msr & MSR_PR) {
888#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +0200889 printk(KERN_INFO "Userspace triggered 0x700 exception at 0x%lx (0x%x)\n", kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000890#endif
Alexander Grafc7f38f42010-04-16 00:11:40 +0200891 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000892 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100893 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000894 r = RESUME_GUEST;
895 break;
896 }
897 }
898
899 vcpu->stat.emulated_inst_exits++;
900 er = kvmppc_emulate_instruction(run, vcpu);
901 switch (er) {
902 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100903 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000904 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100905 case EMULATE_AGAIN:
906 r = RESUME_GUEST;
907 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000908 case EMULATE_FAIL:
909 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200910 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100911 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000912 r = RESUME_GUEST;
913 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +0100914 case EMULATE_DO_MMIO:
915 run->exit_reason = KVM_EXIT_MMIO;
916 r = RESUME_HOST_NV;
917 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000918 default:
919 BUG();
920 }
921 break;
922 }
923 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafad0a0482010-03-24 21:48:30 +0100924 // XXX make user settable
925 if (vcpu->arch.osi_enabled &&
926 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
927 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
928 u64 *gprs = run->osi.gprs;
929 int i;
930
931 run->exit_reason = KVM_EXIT_OSI;
932 for (i = 0; i < 32; i++)
933 gprs[i] = kvmppc_get_gpr(vcpu, i);
934 vcpu->arch.osi_needed = 1;
935 r = RESUME_HOST_NV;
936
937 } else {
938 vcpu->stat.syscall_exits++;
939 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
940 r = RESUME_GUEST;
941 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000942 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000943 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000944 case BOOK3S_INTERRUPT_ALTIVEC:
945 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100946 {
947 int ext_msr = 0;
948
949 switch (exit_nr) {
950 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
951 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
952 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
953 }
954
955 switch (kvmppc_check_ext(vcpu, exit_nr)) {
956 case EMULATE_DONE:
957 /* everything ok - let's enable the ext */
958 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
959 break;
960 case EMULATE_FAIL:
961 /* we need to emulate this instruction */
962 goto program_interrupt;
963 break;
964 default:
965 /* nothing to worry about - go again */
966 break;
967 }
Alexander Graf180a34d2010-01-15 14:49:11 +0100968 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100969 }
Alexander Grafca7f4202010-03-24 21:48:28 +0100970 case BOOK3S_INTERRUPT_ALIGNMENT:
971 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
972 to_book3s(vcpu)->dsisr = kvmppc_alignment_dsisr(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +0200973 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +0100974 vcpu->arch.dear = kvmppc_alignment_dar(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +0200975 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +0100976 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
977 }
978 r = RESUME_GUEST;
979 break;
Alexander Graf180a34d2010-01-15 14:49:11 +0100980 case BOOK3S_INTERRUPT_MACHINE_CHECK:
981 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000982 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
983 r = RESUME_GUEST;
984 break;
985 default:
986 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +0100987 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200988 exit_nr, kvmppc_get_pc(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000989 r = RESUME_HOST;
990 BUG();
991 break;
992 }
993
994
995 if (!(r & RESUME_HOST)) {
996 /* To avoid clobbering exit_reason, only check for signals if
997 * we aren't already exiting to userspace for some other
998 * reason. */
999 if (signal_pending(current)) {
1000#ifdef EXIT_DEBUG
1001 printk(KERN_EMERG "KVM: Going back to host\n");
1002#endif
1003 vcpu->stat.signal_exits++;
1004 run->exit_reason = KVM_EXIT_INTR;
1005 r = -EINTR;
1006 } else {
1007 /* In case an interrupt came in that was triggered
1008 * from userspace (like DEC), we need to check what
1009 * to inject now! */
1010 kvmppc_core_deliver_interrupts(vcpu);
1011 }
1012 }
1013
1014#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +02001015 printk(KERN_EMERG "KVM exit: vcpu=0x%p pc=0x%lx r=0x%x\n", vcpu, kvmppc_get_pc(vcpu), r);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001016#endif
1017
1018 return r;
1019}
1020
1021int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1022{
1023 return 0;
1024}
1025
1026int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1027{
1028 int i;
1029
Alexander Grafa56cf342010-03-24 21:48:23 +01001030 vcpu_load(vcpu);
1031
Alexander Grafc7f38f42010-04-16 00:11:40 +02001032 regs->pc = kvmppc_get_pc(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001033 regs->cr = kvmppc_get_cr(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001034 regs->ctr = kvmppc_get_ctr(vcpu);
1035 regs->lr = kvmppc_get_lr(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001036 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001037 regs->msr = vcpu->arch.msr;
1038 regs->srr0 = vcpu->arch.srr0;
1039 regs->srr1 = vcpu->arch.srr1;
1040 regs->pid = vcpu->arch.pid;
1041 regs->sprg0 = vcpu->arch.sprg0;
1042 regs->sprg1 = vcpu->arch.sprg1;
1043 regs->sprg2 = vcpu->arch.sprg2;
1044 regs->sprg3 = vcpu->arch.sprg3;
1045 regs->sprg5 = vcpu->arch.sprg4;
1046 regs->sprg6 = vcpu->arch.sprg5;
1047 regs->sprg7 = vcpu->arch.sprg6;
1048
1049 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001050 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001051
Alexander Grafa56cf342010-03-24 21:48:23 +01001052 vcpu_put(vcpu);
1053
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001054 return 0;
1055}
1056
1057int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1058{
1059 int i;
1060
Alexander Grafa56cf342010-03-24 21:48:23 +01001061 vcpu_load(vcpu);
1062
Alexander Grafc7f38f42010-04-16 00:11:40 +02001063 kvmppc_set_pc(vcpu, regs->pc);
Alexander Graf992b5b22010-01-08 02:58:02 +01001064 kvmppc_set_cr(vcpu, regs->cr);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001065 kvmppc_set_ctr(vcpu, regs->ctr);
1066 kvmppc_set_lr(vcpu, regs->lr);
Alexander Graf992b5b22010-01-08 02:58:02 +01001067 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001068 kvmppc_set_msr(vcpu, regs->msr);
1069 vcpu->arch.srr0 = regs->srr0;
1070 vcpu->arch.srr1 = regs->srr1;
1071 vcpu->arch.sprg0 = regs->sprg0;
1072 vcpu->arch.sprg1 = regs->sprg1;
1073 vcpu->arch.sprg2 = regs->sprg2;
1074 vcpu->arch.sprg3 = regs->sprg3;
1075 vcpu->arch.sprg5 = regs->sprg4;
1076 vcpu->arch.sprg6 = regs->sprg5;
1077 vcpu->arch.sprg7 = regs->sprg6;
1078
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001079 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1080 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001081
Alexander Grafa56cf342010-03-24 21:48:23 +01001082 vcpu_put(vcpu);
1083
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001084 return 0;
1085}
1086
1087int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1088 struct kvm_sregs *sregs)
1089{
Alexander Grafe15a1132009-11-30 03:02:02 +00001090 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1091 int i;
1092
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001093 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +00001094
1095 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1096 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1097 for (i = 0; i < 64; i++) {
1098 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
1099 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1100 }
1101 } else {
1102 for (i = 0; i < 16; i++) {
1103 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1104 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1105 }
1106 for (i = 0; i < 8; i++) {
1107 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1108 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1109 }
1110 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001111 return 0;
1112}
1113
1114int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1115 struct kvm_sregs *sregs)
1116{
Alexander Grafe15a1132009-11-30 03:02:02 +00001117 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1118 int i;
1119
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001120 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001121
1122 vcpu3s->sdr1 = sregs->u.s.sdr1;
1123 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1124 for (i = 0; i < 64; i++) {
1125 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1126 sregs->u.s.ppc64.slb[i].slbe);
1127 }
1128 } else {
1129 for (i = 0; i < 16; i++) {
1130 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1131 }
1132 for (i = 0; i < 8; i++) {
1133 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1134 (u32)sregs->u.s.ppc32.ibat[i]);
1135 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1136 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1137 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1138 (u32)sregs->u.s.ppc32.dbat[i]);
1139 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1140 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1141 }
1142 }
1143
1144 /* Flush the MMU after messing with the segments */
1145 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001146 return 0;
1147}
1148
1149int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1150{
1151 return -ENOTSUPP;
1152}
1153
1154int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1155{
1156 return -ENOTSUPP;
1157}
1158
1159int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1160 struct kvm_translation *tr)
1161{
1162 return 0;
1163}
1164
1165/*
1166 * Get (and clear) the dirty memory log for a memory slot.
1167 */
1168int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1169 struct kvm_dirty_log *log)
1170{
1171 struct kvm_memory_slot *memslot;
1172 struct kvm_vcpu *vcpu;
1173 ulong ga, ga_end;
1174 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001175 int r;
1176 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001177
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001178 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001179
1180 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1181 if (r)
1182 goto out;
1183
1184 /* If nothing is dirty, don't bother messing with page tables. */
1185 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001186 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001187
1188 ga = memslot->base_gfn << PAGE_SHIFT;
1189 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1190
1191 kvm_for_each_vcpu(n, vcpu, kvm)
1192 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1193
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001194 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001195 memset(memslot->dirty_bitmap, 0, n);
1196 }
1197
1198 r = 0;
1199out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001200 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001201 return r;
1202}
1203
1204int kvmppc_core_check_processor_compat(void)
1205{
1206 return 0;
1207}
1208
1209struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1210{
1211 struct kvmppc_vcpu_book3s *vcpu_book3s;
1212 struct kvm_vcpu *vcpu;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001213 int err = -ENOMEM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001214
Alexander Graf032c3402010-02-19 12:24:33 +01001215 vcpu_book3s = vmalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Grafc7f38f42010-04-16 00:11:40 +02001216 if (!vcpu_book3s)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001217 goto out;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001218
Alexander Graf7e821d32010-02-22 16:52:08 +01001219 memset(vcpu_book3s, 0, sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001220
Alexander Grafc7f38f42010-04-16 00:11:40 +02001221 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
1222 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
1223 if (!vcpu_book3s->shadow_vcpu)
1224 goto free_vcpu;
1225
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001226 vcpu = &vcpu_book3s->vcpu;
1227 err = kvm_vcpu_init(vcpu, kvm, id);
1228 if (err)
Alexander Grafc7f38f42010-04-16 00:11:40 +02001229 goto free_shadow_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001230
1231 vcpu->arch.host_retip = kvm_return_point;
1232 vcpu->arch.host_msr = mfmsr();
Alexander Graf07b09072010-04-16 00:11:53 +02001233#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001234 /* default to book3s_64 (970fx) */
1235 vcpu->arch.pvr = 0x3C0301;
Alexander Graf07b09072010-04-16 00:11:53 +02001236#else
1237 /* default to book3s_32 (750) */
1238 vcpu->arch.pvr = 0x84202;
1239#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001240 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1241 vcpu_book3s->slb_nr = 64;
1242
1243 /* remember where some real-mode handlers are */
1244 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1245 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1246 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf07b09072010-04-16 00:11:53 +02001247#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf021ec9c2010-01-08 02:58:06 +01001248 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf07b09072010-04-16 00:11:53 +02001249#else
1250 vcpu->arch.rmcall = (ulong)kvmppc_rmcall;
1251#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001252
1253 vcpu->arch.shadow_msr = MSR_USER64;
1254
Alexander Graf9cc5e952010-04-16 00:11:45 +02001255 err = kvmppc_mmu_init(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001256 if (err < 0)
Alexander Grafc7f38f42010-04-16 00:11:40 +02001257 goto free_shadow_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001258
1259 return vcpu;
1260
Alexander Grafc7f38f42010-04-16 00:11:40 +02001261free_shadow_vcpu:
1262 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001263free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001264 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001265out:
1266 return ERR_PTR(err);
1267}
1268
1269void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1270{
1271 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1272
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001273 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001274 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001275 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001276}
1277
1278extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1279int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1280{
1281 int ret;
Alexander Graf180a34d2010-01-15 14:49:11 +01001282 struct thread_struct ext_bkp;
Alexander Grafa2b07662010-03-24 21:48:31 +01001283#ifdef CONFIG_ALTIVEC
Alexander Graf180a34d2010-01-15 14:49:11 +01001284 bool save_vec = current->thread.used_vr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001285#endif
1286#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +01001287 bool save_vsx = current->thread.used_vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001288#endif
Alexander Graf180a34d2010-01-15 14:49:11 +01001289 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001290
1291 /* No need to go into the guest when all we do is going out */
1292 if (signal_pending(current)) {
1293 kvm_run->exit_reason = KVM_EXIT_INTR;
1294 return -EINTR;
1295 }
1296
Alexander Graf180a34d2010-01-15 14:49:11 +01001297 /* Save FPU state in stack */
1298 if (current->thread.regs->msr & MSR_FP)
1299 giveup_fpu(current);
1300 memcpy(ext_bkp.fpr, current->thread.fpr, sizeof(current->thread.fpr));
1301 ext_bkp.fpscr = current->thread.fpscr;
1302 ext_bkp.fpexc_mode = current->thread.fpexc_mode;
1303
1304#ifdef CONFIG_ALTIVEC
1305 /* Save Altivec state in stack */
1306 if (save_vec) {
1307 if (current->thread.regs->msr & MSR_VEC)
1308 giveup_altivec(current);
1309 memcpy(ext_bkp.vr, current->thread.vr, sizeof(ext_bkp.vr));
1310 ext_bkp.vscr = current->thread.vscr;
1311 ext_bkp.vrsave = current->thread.vrsave;
1312 }
1313 ext_bkp.used_vr = current->thread.used_vr;
1314#endif
1315
1316#ifdef CONFIG_VSX
1317 /* Save VSX state in stack */
1318 if (save_vsx && (current->thread.regs->msr & MSR_VSX))
1319 __giveup_vsx(current);
1320 ext_bkp.used_vsr = current->thread.used_vsr;
1321#endif
1322
1323 /* Remember the MSR with disabled extensions */
1324 ext_msr = current->thread.regs->msr;
1325
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001326 /* XXX we get called with irq disabled - change that! */
1327 local_irq_enable();
1328
Alexander Grafd1bab742010-02-19 11:00:35 +01001329 /* Preload FPU if it's enabled */
1330 if (vcpu->arch.msr & MSR_FP)
1331 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1332
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001333 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1334
1335 local_irq_disable();
1336
Alexander Graf180a34d2010-01-15 14:49:11 +01001337 current->thread.regs->msr = ext_msr;
1338
1339 /* Make sure we save the guest FPU/Altivec/VSX state */
1340 kvmppc_giveup_ext(vcpu, MSR_FP);
1341 kvmppc_giveup_ext(vcpu, MSR_VEC);
1342 kvmppc_giveup_ext(vcpu, MSR_VSX);
1343
1344 /* Restore FPU state from stack */
1345 memcpy(current->thread.fpr, ext_bkp.fpr, sizeof(ext_bkp.fpr));
1346 current->thread.fpscr = ext_bkp.fpscr;
1347 current->thread.fpexc_mode = ext_bkp.fpexc_mode;
1348
1349#ifdef CONFIG_ALTIVEC
1350 /* Restore Altivec state from stack */
1351 if (save_vec && current->thread.used_vr) {
1352 memcpy(current->thread.vr, ext_bkp.vr, sizeof(ext_bkp.vr));
1353 current->thread.vscr = ext_bkp.vscr;
1354 current->thread.vrsave= ext_bkp.vrsave;
1355 }
1356 current->thread.used_vr = ext_bkp.used_vr;
1357#endif
1358
1359#ifdef CONFIG_VSX
1360 current->thread.used_vsr = ext_bkp.used_vsr;
1361#endif
1362
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001363 return ret;
1364}
1365
1366static int kvmppc_book3s_init(void)
1367{
1368 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), THIS_MODULE);
1369}
1370
1371static void kvmppc_book3s_exit(void)
1372{
1373 kvm_exit();
1374}
1375
1376module_init(kvmppc_book3s_init);
1377module_exit(kvmppc_book3s_exit);