blob: d0986968a6119f550746fec2577e7cda05a10b15 [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{
Alexander Grafc7f38f42010-04-16 00:11:40 +020074#ifdef CONFIG_PPC_BOOK3S_64
75 memcpy(to_svcpu(vcpu)->slb, to_book3s(vcpu)->slb_shadow, sizeof(to_svcpu(vcpu)->slb));
76 memcpy(&get_paca()->shadow_vcpu, to_book3s(vcpu)->shadow_vcpu,
Alexander Graf7e57cba2010-01-08 02:58:03 +010077 sizeof(get_paca()->shadow_vcpu));
Alexander Grafc7f38f42010-04-16 00:11:40 +020078 to_svcpu(vcpu)->slb_max = to_book3s(vcpu)->slb_shadow_max;
79#endif
80
81#ifdef CONFIG_PPC_BOOK3S_32
82 current->thread.kvm_shadow_vcpu = to_book3s(vcpu)->shadow_vcpu;
83#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000084}
85
86void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
87{
Alexander Grafc7f38f42010-04-16 00:11:40 +020088#ifdef CONFIG_PPC_BOOK3S_64
89 memcpy(to_book3s(vcpu)->slb_shadow, to_svcpu(vcpu)->slb, sizeof(to_svcpu(vcpu)->slb));
90 memcpy(to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
Alexander Graf7e57cba2010-01-08 02:58:03 +010091 sizeof(get_paca()->shadow_vcpu));
Alexander Grafc7f38f42010-04-16 00:11:40 +020092 to_book3s(vcpu)->slb_shadow_max = to_svcpu(vcpu)->slb_max;
93#endif
Alexander Graf180a34d2010-01-15 14:49:11 +010094
95 kvmppc_giveup_ext(vcpu, MSR_FP);
96 kvmppc_giveup_ext(vcpu, MSR_VEC);
97 kvmppc_giveup_ext(vcpu, MSR_VSX);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000098}
99
Alexander Graf0bb1fb72009-12-21 20:21:25 +0100100#if defined(EXIT_DEBUG)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000101static u32 kvmppc_get_dec(struct kvm_vcpu *vcpu)
102{
103 u64 jd = mftb() - vcpu->arch.dec_jiffies;
104 return vcpu->arch.dec - jd;
105}
106#endif
107
Alexander Grafa76f8492010-01-15 14:49:14 +0100108static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
109{
110 vcpu->arch.shadow_msr = vcpu->arch.msr;
111 /* Guest MSR values */
112 vcpu->arch.shadow_msr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE |
113 MSR_BE | MSR_DE;
114 /* Process MSR values */
115 vcpu->arch.shadow_msr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR |
116 MSR_EE;
117 /* External providers the guest reserved */
118 vcpu->arch.shadow_msr |= (vcpu->arch.msr & vcpu->arch.guest_owned_ext);
119 /* 64-bit Process MSR values */
120#ifdef CONFIG_PPC_BOOK3S_64
121 vcpu->arch.shadow_msr |= MSR_ISF | MSR_HV;
122#endif
123}
124
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000125void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
126{
127 ulong old_msr = vcpu->arch.msr;
128
129#ifdef EXIT_DEBUG
130 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
131#endif
Alexander Grafa76f8492010-01-15 14:49:14 +0100132
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000133 msr &= to_book3s(vcpu)->msr_mask;
134 vcpu->arch.msr = msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100135 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000136
137 if (msr & (MSR_WE|MSR_POW)) {
138 if (!vcpu->arch.pending_exceptions) {
139 kvm_vcpu_block(vcpu);
140 vcpu->stat.halt_wakeup++;
141 }
142 }
143
144 if (((vcpu->arch.msr & (MSR_IR|MSR_DR)) != (old_msr & (MSR_IR|MSR_DR))) ||
145 (vcpu->arch.msr & MSR_PR) != (old_msr & MSR_PR)) {
Alexander Graf3eeafd72010-03-24 21:48:17 +0100146 bool dr = (vcpu->arch.msr & MSR_DR) ? true : false;
147 bool ir = (vcpu->arch.msr & MSR_IR) ? true : false;
148
149 /* Flush split mode PTEs */
150 if (dr != ir)
151 kvmppc_mmu_pte_vflush(vcpu, VSID_SPLIT_MASK,
152 VSID_SPLIT_MASK);
153
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000154 kvmppc_mmu_flush_segments(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +0200155 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000156 }
Alexander Grafd1bab742010-02-19 11:00:35 +0100157
158 /* Preload FPU if it's enabled */
159 if (vcpu->arch.msr & MSR_FP)
160 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000161}
162
163void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
164{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200165 vcpu->arch.srr0 = kvmppc_get_pc(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000166 vcpu->arch.srr1 = vcpu->arch.msr | flags;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200167 kvmppc_set_pc(vcpu, to_book3s(vcpu)->hior + vec);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000168 vcpu->arch.mmu.reset_msr(vcpu);
169}
170
Alexander Graf583617b2009-12-21 20:21:23 +0100171static int kvmppc_book3s_vec2irqprio(unsigned int vec)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000172{
173 unsigned int prio;
174
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000175 switch (vec) {
176 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
177 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
178 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
179 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
180 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
181 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
182 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
183 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
184 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
185 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
186 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
187 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
188 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
189 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
190 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
191 default: prio = BOOK3S_IRQPRIO_MAX; break;
192 }
193
Alexander Graf583617b2009-12-21 20:21:23 +0100194 return prio;
195}
196
Alexander Graf7706664d2009-12-21 20:21:24 +0100197static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
198 unsigned int vec)
199{
200 clear_bit(kvmppc_book3s_vec2irqprio(vec),
201 &vcpu->arch.pending_exceptions);
202}
203
Alexander Graf583617b2009-12-21 20:21:23 +0100204void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
205{
206 vcpu->stat.queue_intr++;
207
208 set_bit(kvmppc_book3s_vec2irqprio(vec),
209 &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000210#ifdef EXIT_DEBUG
211 printk(KERN_INFO "Queueing interrupt %x\n", vec);
212#endif
213}
214
215
Alexander Graf25a8a022010-01-08 02:58:07 +0100216void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000217{
Alexander Graf25a8a022010-01-08 02:58:07 +0100218 to_book3s(vcpu)->prog_flags = flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000219 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM);
220}
221
222void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
223{
224 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
225}
226
227int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
228{
229 return test_bit(BOOK3S_INTERRUPT_DECREMENTER >> 7, &vcpu->arch.pending_exceptions);
230}
231
Alexander Graf7706664d2009-12-21 20:21:24 +0100232void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
233{
234 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
235}
236
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000237void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
238 struct kvm_interrupt *irq)
239{
240 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
241}
242
Alexander Graf18978762010-03-24 21:48:18 +0100243void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
244 struct kvm_interrupt *irq)
245{
246 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
247}
248
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000249int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
250{
251 int deliver = 1;
252 int vec = 0;
Alexander Graf25a8a022010-01-08 02:58:07 +0100253 ulong flags = 0ULL;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000254
255 switch (priority) {
256 case BOOK3S_IRQPRIO_DECREMENTER:
257 deliver = vcpu->arch.msr & MSR_EE;
258 vec = BOOK3S_INTERRUPT_DECREMENTER;
259 break;
260 case BOOK3S_IRQPRIO_EXTERNAL:
261 deliver = vcpu->arch.msr & MSR_EE;
262 vec = BOOK3S_INTERRUPT_EXTERNAL;
263 break;
264 case BOOK3S_IRQPRIO_SYSTEM_RESET:
265 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
266 break;
267 case BOOK3S_IRQPRIO_MACHINE_CHECK:
268 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
269 break;
270 case BOOK3S_IRQPRIO_DATA_STORAGE:
271 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
272 break;
273 case BOOK3S_IRQPRIO_INST_STORAGE:
274 vec = BOOK3S_INTERRUPT_INST_STORAGE;
275 break;
276 case BOOK3S_IRQPRIO_DATA_SEGMENT:
277 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
278 break;
279 case BOOK3S_IRQPRIO_INST_SEGMENT:
280 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
281 break;
282 case BOOK3S_IRQPRIO_ALIGNMENT:
283 vec = BOOK3S_INTERRUPT_ALIGNMENT;
284 break;
285 case BOOK3S_IRQPRIO_PROGRAM:
286 vec = BOOK3S_INTERRUPT_PROGRAM;
Alexander Graf25a8a022010-01-08 02:58:07 +0100287 flags = to_book3s(vcpu)->prog_flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000288 break;
289 case BOOK3S_IRQPRIO_VSX:
290 vec = BOOK3S_INTERRUPT_VSX;
291 break;
292 case BOOK3S_IRQPRIO_ALTIVEC:
293 vec = BOOK3S_INTERRUPT_ALTIVEC;
294 break;
295 case BOOK3S_IRQPRIO_FP_UNAVAIL:
296 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
297 break;
298 case BOOK3S_IRQPRIO_SYSCALL:
299 vec = BOOK3S_INTERRUPT_SYSCALL;
300 break;
301 case BOOK3S_IRQPRIO_DEBUG:
302 vec = BOOK3S_INTERRUPT_TRACE;
303 break;
304 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
305 vec = BOOK3S_INTERRUPT_PERFMON;
306 break;
307 default:
308 deliver = 0;
309 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
310 break;
311 }
312
313#if 0
314 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
315#endif
316
317 if (deliver)
Alexander Graf25a8a022010-01-08 02:58:07 +0100318 kvmppc_inject_interrupt(vcpu, vec, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000319
320 return deliver;
321}
322
323void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
324{
325 unsigned long *pending = &vcpu->arch.pending_exceptions;
326 unsigned int priority;
327
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000328#ifdef EXIT_DEBUG
329 if (vcpu->arch.pending_exceptions)
330 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
331#endif
332 priority = __ffs(*pending);
333 while (priority <= (sizeof(unsigned int) * 8)) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100334 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
335 (priority != BOOK3S_IRQPRIO_DECREMENTER)) {
336 /* DEC interrupts get cleared by mtdec */
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000337 clear_bit(priority, &vcpu->arch.pending_exceptions);
338 break;
339 }
340
341 priority = find_next_bit(pending,
342 BITS_PER_BYTE * sizeof(*pending),
343 priority + 1);
344 }
345}
346
347void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
348{
Alexander Grafe15a1132009-11-30 03:02:02 +0000349 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000350 vcpu->arch.pvr = pvr;
351 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
352 kvmppc_mmu_book3s_64_init(vcpu);
353 to_book3s(vcpu)->hior = 0xfff00000;
354 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
355 } else {
356 kvmppc_mmu_book3s_32_init(vcpu);
357 to_book3s(vcpu)->hior = 0;
358 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
359 }
360
361 /* If we are in hypervisor level on 970, we can tell the CPU to
362 * treat DCBZ as 32 bytes store */
363 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
364 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
365 !strcmp(cur_cpu_spec->platform, "ppc970"))
366 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
367
Alexander Graf05b0ab12010-03-24 21:48:37 +0100368 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
369 really needs them in a VM on Cell and force disable them. */
370 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
371 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000372}
373
374/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
375 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
376 * emulate 32 bytes dcbz length.
377 *
378 * The Book3s_64 inventors also realized this case and implemented a special bit
379 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
380 *
381 * My approach here is to patch the dcbz instruction on executing pages.
382 */
383static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
384{
Alexander Graf9fb244a2010-03-24 21:48:32 +0100385 struct page *hpage;
386 u64 hpage_offset;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000387 u32 *page;
388 int i;
389
Alexander Graf9fb244a2010-03-24 21:48:32 +0100390 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
391 if (is_error_page(hpage))
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000392 return;
393
Alexander Graf9fb244a2010-03-24 21:48:32 +0100394 hpage_offset = pte->raddr & ~PAGE_MASK;
395 hpage_offset &= ~0xFFFULL;
396 hpage_offset /= 4;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000397
Alexander Graf9fb244a2010-03-24 21:48:32 +0100398 get_page(hpage);
399 page = kmap_atomic(hpage, KM_USER0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000400
Alexander Graf9fb244a2010-03-24 21:48:32 +0100401 /* patch dcbz into reserved instruction, so we trap */
402 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
403 if ((page[i] & 0xff0007ff) == INS_DCBZ)
404 page[i] &= 0xfffffff7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000405
Alexander Graf9fb244a2010-03-24 21:48:32 +0100406 kunmap_atomic(page, KM_USER0);
407 put_page(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000408}
409
410static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
411 struct kvmppc_pte *pte)
412{
413 int relocated = (vcpu->arch.msr & (data ? MSR_DR : MSR_IR));
414 int r;
415
416 if (relocated) {
417 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
418 } else {
419 pte->eaddr = eaddr;
420 pte->raddr = eaddr & 0xffffffff;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100421 pte->vpage = VSID_REAL | eaddr >> 12;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000422 pte->may_read = true;
423 pte->may_write = true;
424 pte->may_execute = true;
425 r = 0;
426 }
427
428 return r;
429}
430
431static hva_t kvmppc_bad_hva(void)
432{
433 return PAGE_OFFSET;
434}
435
436static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
437 bool read)
438{
439 hva_t hpage;
440
441 if (read && !pte->may_read)
442 goto err;
443
444 if (!read && !pte->may_write)
445 goto err;
446
447 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
448 if (kvm_is_error_hva(hpage))
449 goto err;
450
451 return hpage | (pte->raddr & ~PAGE_MASK);
452err:
453 return kvmppc_bad_hva();
454}
455
Alexander Graf5467a972010-02-19 11:00:38 +0100456int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
457 bool data)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000458{
459 struct kvmppc_pte pte;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000460
461 vcpu->stat.st++;
462
Alexander Graf5467a972010-02-19 11:00:38 +0100463 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
Alexander Graf9fb244a2010-03-24 21:48:32 +0100464 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100465
466 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000467
Alexander Graf9fb244a2010-03-24 21:48:32 +0100468 if (!pte.may_write)
469 return -EPERM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000470
Alexander Graf9fb244a2010-03-24 21:48:32 +0100471 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
472 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000473
Alexander Graf5467a972010-02-19 11:00:38 +0100474 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000475}
476
Alexander Graf5467a972010-02-19 11:00:38 +0100477int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000478 bool data)
479{
480 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100481 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000482
483 vcpu->stat.ld++;
484
Alexander Graf5467a972010-02-19 11:00:38 +0100485 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
486 goto nopte;
487
488 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000489
490 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
491 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100492 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000493
494 if (copy_from_user(ptr, (void __user *)hva, size)) {
495 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100496 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000497 }
498
Alexander Graf5467a972010-02-19 11:00:38 +0100499 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000500
Alexander Graf5467a972010-02-19 11:00:38 +0100501nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000502 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100503mmio:
504 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000505}
506
507static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
508{
509 return kvm_is_visible_gfn(vcpu->kvm, gfn);
510}
511
512int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
513 ulong eaddr, int vec)
514{
515 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
516 int r = RESUME_GUEST;
517 int relocated;
518 int page_found = 0;
519 struct kvmppc_pte pte;
520 bool is_mmio = false;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100521 bool dr = (vcpu->arch.msr & MSR_DR) ? true : false;
522 bool ir = (vcpu->arch.msr & MSR_IR) ? true : false;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000523
Alexander Graf3eeafd72010-03-24 21:48:17 +0100524 relocated = data ? dr : ir;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000525
526 /* Resolve real address if translation turned on */
527 if (relocated) {
528 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
529 } else {
530 pte.may_execute = true;
531 pte.may_read = true;
532 pte.may_write = true;
533 pte.raddr = eaddr & 0xffffffff;
534 pte.eaddr = eaddr;
535 pte.vpage = eaddr >> 12;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100536 }
537
538 switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
539 case 0:
540 pte.vpage |= VSID_REAL;
541 break;
542 case MSR_DR:
543 pte.vpage |= VSID_REAL_DR;
544 break;
545 case MSR_IR:
546 pte.vpage |= VSID_REAL_IR;
547 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000548 }
549
550 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
551 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
552 /*
553 * If we do the dcbz hack, we have to NX on every execution,
554 * so we can patch the executing code. This renders our guest
555 * NX-less.
556 */
557 pte.may_execute = !data;
558 }
559
560 if (page_found == -ENOENT) {
561 /* Page not found in guest PTE entries */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200562 vcpu->arch.dear = kvmppc_get_fault_dar(vcpu);
563 to_book3s(vcpu)->dsisr = to_svcpu(vcpu)->fault_dsisr;
564 vcpu->arch.msr |= (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000565 kvmppc_book3s_queue_irqprio(vcpu, vec);
566 } else if (page_found == -EPERM) {
567 /* Storage protection */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200568 vcpu->arch.dear = kvmppc_get_fault_dar(vcpu);
569 to_book3s(vcpu)->dsisr = to_svcpu(vcpu)->fault_dsisr & ~DSISR_NOHPTE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000570 to_book3s(vcpu)->dsisr |= DSISR_PROTFAULT;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200571 vcpu->arch.msr |= (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000572 kvmppc_book3s_queue_irqprio(vcpu, vec);
573 } else if (page_found == -EINVAL) {
574 /* Page not found in guest SLB */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200575 vcpu->arch.dear = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000576 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
577 } else if (!is_mmio &&
578 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
579 /* The guest's PTE is not mapped yet. Map on the host */
580 kvmppc_mmu_map_page(vcpu, &pte);
581 if (data)
582 vcpu->stat.sp_storage++;
583 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
584 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
585 kvmppc_patch_dcbz(vcpu, &pte);
586 } else {
587 /* MMIO */
588 vcpu->stat.mmio_exits++;
589 vcpu->arch.paddr_accessed = pte.raddr;
590 r = kvmppc_emulate_mmio(run, vcpu);
591 if ( r == RESUME_HOST_NV )
592 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000593 }
594
595 return r;
596}
597
Alexander Graf180a34d2010-01-15 14:49:11 +0100598static inline int get_fpr_index(int i)
599{
600#ifdef CONFIG_VSX
601 i *= 2;
602#endif
603 return i;
604}
605
606/* Give up external provider (FPU, Altivec, VSX) */
Alexander Grafaba3bd72010-02-19 11:00:39 +0100607void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
Alexander Graf180a34d2010-01-15 14:49:11 +0100608{
609 struct thread_struct *t = &current->thread;
610 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100611#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100612 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100613#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100614 u64 *thread_fpr = (u64*)t->fpr;
615 int i;
616
617 if (!(vcpu->arch.guest_owned_ext & msr))
618 return;
619
620#ifdef DEBUG_EXT
621 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
622#endif
623
624 switch (msr) {
625 case MSR_FP:
626 giveup_fpu(current);
627 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
628 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
629
630 vcpu->arch.fpscr = t->fpscr.val;
631 break;
632 case MSR_VEC:
633#ifdef CONFIG_ALTIVEC
634 giveup_altivec(current);
635 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
636 vcpu->arch.vscr = t->vscr;
637#endif
638 break;
639 case MSR_VSX:
640#ifdef CONFIG_VSX
641 __giveup_vsx(current);
642 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
643 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
644#endif
645 break;
646 default:
647 BUG();
648 }
649
650 vcpu->arch.guest_owned_ext &= ~msr;
651 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100652 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100653}
654
Alexander Graf89632212010-03-24 21:48:21 +0100655static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100656{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200657 ulong srr0 = kvmppc_get_pc(vcpu);
658 u32 last_inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100659 int ret;
660
Alexander Grafc7f38f42010-04-16 00:11:40 +0200661 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100662 if (ret == -ENOENT) {
663 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 33, 33, 1);
664 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 34, 36, 0);
665 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 42, 47, 0);
666 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
Alexander Graf89632212010-03-24 21:48:21 +0100667 return EMULATE_AGAIN;
668 }
669
670 return EMULATE_DONE;
671}
672
673static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
674{
675
676 /* Need to do paired single emulation? */
677 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
678 return EMULATE_DONE;
679
680 /* Read out the instruction */
681 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100682 /* Need to emulate */
683 return EMULATE_FAIL;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100684
685 return EMULATE_AGAIN;
686}
687
Alexander Graf180a34d2010-01-15 14:49:11 +0100688/* Handle external providers (FPU, Altivec, VSX) */
689static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
690 ulong msr)
691{
692 struct thread_struct *t = &current->thread;
693 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100694#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100695 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100696#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100697 u64 *thread_fpr = (u64*)t->fpr;
698 int i;
699
Alexander Graf3c402a72010-02-19 11:00:32 +0100700 /* When we have paired singles, we emulate in software */
701 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
702 return RESUME_GUEST;
703
Alexander Graf180a34d2010-01-15 14:49:11 +0100704 if (!(vcpu->arch.msr & msr)) {
705 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
706 return RESUME_GUEST;
707 }
708
Alexander Grafc2453692010-03-24 21:48:22 +0100709 /* We already own the ext */
710 if (vcpu->arch.guest_owned_ext & msr) {
711 return RESUME_GUEST;
712 }
713
Alexander Graf180a34d2010-01-15 14:49:11 +0100714#ifdef DEBUG_EXT
715 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
716#endif
717
718 current->thread.regs->msr |= msr;
719
720 switch (msr) {
721 case MSR_FP:
722 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
723 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
724
725 t->fpscr.val = vcpu->arch.fpscr;
726 t->fpexc_mode = 0;
727 kvmppc_load_up_fpu();
728 break;
729 case MSR_VEC:
730#ifdef CONFIG_ALTIVEC
731 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
732 t->vscr = vcpu->arch.vscr;
733 t->vrsave = -1;
734 kvmppc_load_up_altivec();
735#endif
736 break;
737 case MSR_VSX:
738#ifdef CONFIG_VSX
739 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
740 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
741 kvmppc_load_up_vsx();
742#endif
743 break;
744 default:
745 BUG();
746 }
747
748 vcpu->arch.guest_owned_ext |= msr;
749
Alexander Grafa76f8492010-01-15 14:49:14 +0100750 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100751
752 return RESUME_GUEST;
753}
754
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000755int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
756 unsigned int exit_nr)
757{
758 int r = RESUME_HOST;
759
760 vcpu->stat.sum_exits++;
761
762 run->exit_reason = KVM_EXIT_UNKNOWN;
763 run->ready_for_interrupt_injection = 1;
764#ifdef EXIT_DEBUG
765 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 +0200766 exit_nr, kvmppc_get_pc(vcpu), kvmppc_get_fault_dar(vcpu),
767 kvmppc_get_dec(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000768#elif defined (EXIT_DEBUG_SIMPLE)
769 if ((exit_nr != 0x900) && (exit_nr != 0x500))
770 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200771 exit_nr, kvmppc_get_pc(vcpu), kvmppc_get_fault_dar(vcpu),
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000772 vcpu->arch.msr);
773#endif
774 kvm_resched(vcpu);
775 switch (exit_nr) {
776 case BOOK3S_INTERRUPT_INST_STORAGE:
777 vcpu->stat.pf_instruc++;
778 /* only care about PTEG not found errors, but leave NX alone */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200779 if (to_svcpu(vcpu)->shadow_srr1 & 0x40000000) {
780 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000781 vcpu->stat.sp_instruc++;
782 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
783 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
784 /*
785 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
786 * so we can't use the NX bit inside the guest. Let's cross our fingers,
787 * that no guest that needs the dcbz hack does NX.
788 */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200789 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFULL);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100790 r = RESUME_GUEST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000791 } else {
Alexander Grafc7f38f42010-04-16 00:11:40 +0200792 vcpu->arch.msr |= to_svcpu(vcpu)->shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000793 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Grafc7f38f42010-04-16 00:11:40 +0200794 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000795 r = RESUME_GUEST;
796 }
797 break;
798 case BOOK3S_INTERRUPT_DATA_STORAGE:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200799 {
800 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000801 vcpu->stat.pf_storage++;
802 /* The only case we need to handle is missing shadow PTEs */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200803 if (to_svcpu(vcpu)->fault_dsisr & DSISR_NOHPTE) {
804 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000805 } else {
Alexander Grafc7f38f42010-04-16 00:11:40 +0200806 vcpu->arch.dear = dar;
807 to_book3s(vcpu)->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000808 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
809 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.dear, ~0xFFFULL);
810 r = RESUME_GUEST;
811 }
812 break;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200813 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000814 case BOOK3S_INTERRUPT_DATA_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200815 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
816 vcpu->arch.dear = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000817 kvmppc_book3s_queue_irqprio(vcpu,
818 BOOK3S_INTERRUPT_DATA_SEGMENT);
819 }
820 r = RESUME_GUEST;
821 break;
822 case BOOK3S_INTERRUPT_INST_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200823 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000824 kvmppc_book3s_queue_irqprio(vcpu,
825 BOOK3S_INTERRUPT_INST_SEGMENT);
826 }
827 r = RESUME_GUEST;
828 break;
829 /* We're good on these - the host merely wanted to get our attention */
830 case BOOK3S_INTERRUPT_DECREMENTER:
831 vcpu->stat.dec_exits++;
832 r = RESUME_GUEST;
833 break;
834 case BOOK3S_INTERRUPT_EXTERNAL:
835 vcpu->stat.ext_intr_exits++;
836 r = RESUME_GUEST;
837 break;
838 case BOOK3S_INTERRUPT_PROGRAM:
839 {
840 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100841 ulong flags;
842
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100843program_interrupt:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200844 flags = to_svcpu(vcpu)->shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000845
846 if (vcpu->arch.msr & MSR_PR) {
847#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +0200848 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 +0000849#endif
Alexander Grafc7f38f42010-04-16 00:11:40 +0200850 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000851 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100852 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000853 r = RESUME_GUEST;
854 break;
855 }
856 }
857
858 vcpu->stat.emulated_inst_exits++;
859 er = kvmppc_emulate_instruction(run, vcpu);
860 switch (er) {
861 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100862 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000863 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100864 case EMULATE_AGAIN:
865 r = RESUME_GUEST;
866 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000867 case EMULATE_FAIL:
868 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200869 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100870 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000871 r = RESUME_GUEST;
872 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +0100873 case EMULATE_DO_MMIO:
874 run->exit_reason = KVM_EXIT_MMIO;
875 r = RESUME_HOST_NV;
876 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000877 default:
878 BUG();
879 }
880 break;
881 }
882 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafad0a0482010-03-24 21:48:30 +0100883 // XXX make user settable
884 if (vcpu->arch.osi_enabled &&
885 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
886 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
887 u64 *gprs = run->osi.gprs;
888 int i;
889
890 run->exit_reason = KVM_EXIT_OSI;
891 for (i = 0; i < 32; i++)
892 gprs[i] = kvmppc_get_gpr(vcpu, i);
893 vcpu->arch.osi_needed = 1;
894 r = RESUME_HOST_NV;
895
896 } else {
897 vcpu->stat.syscall_exits++;
898 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
899 r = RESUME_GUEST;
900 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000901 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000902 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000903 case BOOK3S_INTERRUPT_ALTIVEC:
904 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100905 {
906 int ext_msr = 0;
907
908 switch (exit_nr) {
909 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
910 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
911 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
912 }
913
914 switch (kvmppc_check_ext(vcpu, exit_nr)) {
915 case EMULATE_DONE:
916 /* everything ok - let's enable the ext */
917 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
918 break;
919 case EMULATE_FAIL:
920 /* we need to emulate this instruction */
921 goto program_interrupt;
922 break;
923 default:
924 /* nothing to worry about - go again */
925 break;
926 }
Alexander Graf180a34d2010-01-15 14:49:11 +0100927 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100928 }
Alexander Grafca7f4202010-03-24 21:48:28 +0100929 case BOOK3S_INTERRUPT_ALIGNMENT:
930 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
931 to_book3s(vcpu)->dsisr = kvmppc_alignment_dsisr(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +0200932 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +0100933 vcpu->arch.dear = kvmppc_alignment_dar(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +0200934 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +0100935 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
936 }
937 r = RESUME_GUEST;
938 break;
Alexander Graf180a34d2010-01-15 14:49:11 +0100939 case BOOK3S_INTERRUPT_MACHINE_CHECK:
940 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000941 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
942 r = RESUME_GUEST;
943 break;
944 default:
945 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +0100946 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200947 exit_nr, kvmppc_get_pc(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000948 r = RESUME_HOST;
949 BUG();
950 break;
951 }
952
953
954 if (!(r & RESUME_HOST)) {
955 /* To avoid clobbering exit_reason, only check for signals if
956 * we aren't already exiting to userspace for some other
957 * reason. */
958 if (signal_pending(current)) {
959#ifdef EXIT_DEBUG
960 printk(KERN_EMERG "KVM: Going back to host\n");
961#endif
962 vcpu->stat.signal_exits++;
963 run->exit_reason = KVM_EXIT_INTR;
964 r = -EINTR;
965 } else {
966 /* In case an interrupt came in that was triggered
967 * from userspace (like DEC), we need to check what
968 * to inject now! */
969 kvmppc_core_deliver_interrupts(vcpu);
970 }
971 }
972
973#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +0200974 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 +0000975#endif
976
977 return r;
978}
979
980int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
981{
982 return 0;
983}
984
985int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
986{
987 int i;
988
Alexander Grafa56cf342010-03-24 21:48:23 +0100989 vcpu_load(vcpu);
990
Alexander Grafc7f38f42010-04-16 00:11:40 +0200991 regs->pc = kvmppc_get_pc(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +0100992 regs->cr = kvmppc_get_cr(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +0200993 regs->ctr = kvmppc_get_ctr(vcpu);
994 regs->lr = kvmppc_get_lr(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +0100995 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000996 regs->msr = vcpu->arch.msr;
997 regs->srr0 = vcpu->arch.srr0;
998 regs->srr1 = vcpu->arch.srr1;
999 regs->pid = vcpu->arch.pid;
1000 regs->sprg0 = vcpu->arch.sprg0;
1001 regs->sprg1 = vcpu->arch.sprg1;
1002 regs->sprg2 = vcpu->arch.sprg2;
1003 regs->sprg3 = vcpu->arch.sprg3;
1004 regs->sprg5 = vcpu->arch.sprg4;
1005 regs->sprg6 = vcpu->arch.sprg5;
1006 regs->sprg7 = vcpu->arch.sprg6;
1007
1008 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001009 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001010
Alexander Grafa56cf342010-03-24 21:48:23 +01001011 vcpu_put(vcpu);
1012
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001013 return 0;
1014}
1015
1016int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1017{
1018 int i;
1019
Alexander Grafa56cf342010-03-24 21:48:23 +01001020 vcpu_load(vcpu);
1021
Alexander Grafc7f38f42010-04-16 00:11:40 +02001022 kvmppc_set_pc(vcpu, regs->pc);
Alexander Graf992b5b22010-01-08 02:58:02 +01001023 kvmppc_set_cr(vcpu, regs->cr);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001024 kvmppc_set_ctr(vcpu, regs->ctr);
1025 kvmppc_set_lr(vcpu, regs->lr);
Alexander Graf992b5b22010-01-08 02:58:02 +01001026 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001027 kvmppc_set_msr(vcpu, regs->msr);
1028 vcpu->arch.srr0 = regs->srr0;
1029 vcpu->arch.srr1 = regs->srr1;
1030 vcpu->arch.sprg0 = regs->sprg0;
1031 vcpu->arch.sprg1 = regs->sprg1;
1032 vcpu->arch.sprg2 = regs->sprg2;
1033 vcpu->arch.sprg3 = regs->sprg3;
1034 vcpu->arch.sprg5 = regs->sprg4;
1035 vcpu->arch.sprg6 = regs->sprg5;
1036 vcpu->arch.sprg7 = regs->sprg6;
1037
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001038 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1039 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001040
Alexander Grafa56cf342010-03-24 21:48:23 +01001041 vcpu_put(vcpu);
1042
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001043 return 0;
1044}
1045
1046int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1047 struct kvm_sregs *sregs)
1048{
Alexander Grafe15a1132009-11-30 03:02:02 +00001049 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1050 int i;
1051
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001052 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +00001053
1054 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1055 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1056 for (i = 0; i < 64; i++) {
1057 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
1058 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1059 }
1060 } else {
1061 for (i = 0; i < 16; i++) {
1062 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1063 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1064 }
1065 for (i = 0; i < 8; i++) {
1066 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1067 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1068 }
1069 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001070 return 0;
1071}
1072
1073int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1074 struct kvm_sregs *sregs)
1075{
Alexander Grafe15a1132009-11-30 03:02:02 +00001076 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1077 int i;
1078
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001079 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001080
1081 vcpu3s->sdr1 = sregs->u.s.sdr1;
1082 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1083 for (i = 0; i < 64; i++) {
1084 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1085 sregs->u.s.ppc64.slb[i].slbe);
1086 }
1087 } else {
1088 for (i = 0; i < 16; i++) {
1089 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1090 }
1091 for (i = 0; i < 8; i++) {
1092 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1093 (u32)sregs->u.s.ppc32.ibat[i]);
1094 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1095 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1096 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1097 (u32)sregs->u.s.ppc32.dbat[i]);
1098 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1099 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1100 }
1101 }
1102
1103 /* Flush the MMU after messing with the segments */
1104 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001105 return 0;
1106}
1107
1108int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1109{
1110 return -ENOTSUPP;
1111}
1112
1113int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1114{
1115 return -ENOTSUPP;
1116}
1117
1118int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1119 struct kvm_translation *tr)
1120{
1121 return 0;
1122}
1123
1124/*
1125 * Get (and clear) the dirty memory log for a memory slot.
1126 */
1127int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1128 struct kvm_dirty_log *log)
1129{
1130 struct kvm_memory_slot *memslot;
1131 struct kvm_vcpu *vcpu;
1132 ulong ga, ga_end;
1133 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001134 int r;
1135 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001136
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001137 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001138
1139 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1140 if (r)
1141 goto out;
1142
1143 /* If nothing is dirty, don't bother messing with page tables. */
1144 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001145 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001146
1147 ga = memslot->base_gfn << PAGE_SHIFT;
1148 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1149
1150 kvm_for_each_vcpu(n, vcpu, kvm)
1151 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1152
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001153 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001154 memset(memslot->dirty_bitmap, 0, n);
1155 }
1156
1157 r = 0;
1158out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001159 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001160 return r;
1161}
1162
1163int kvmppc_core_check_processor_compat(void)
1164{
1165 return 0;
1166}
1167
1168struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1169{
1170 struct kvmppc_vcpu_book3s *vcpu_book3s;
1171 struct kvm_vcpu *vcpu;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001172 int err = -ENOMEM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001173
Alexander Graf032c3402010-02-19 12:24:33 +01001174 vcpu_book3s = vmalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Grafc7f38f42010-04-16 00:11:40 +02001175 if (!vcpu_book3s)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001176 goto out;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001177
Alexander Graf7e821d32010-02-22 16:52:08 +01001178 memset(vcpu_book3s, 0, sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001179
Alexander Grafc7f38f42010-04-16 00:11:40 +02001180 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
1181 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
1182 if (!vcpu_book3s->shadow_vcpu)
1183 goto free_vcpu;
1184
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001185 vcpu = &vcpu_book3s->vcpu;
1186 err = kvm_vcpu_init(vcpu, kvm, id);
1187 if (err)
Alexander Grafc7f38f42010-04-16 00:11:40 +02001188 goto free_shadow_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001189
1190 vcpu->arch.host_retip = kvm_return_point;
1191 vcpu->arch.host_msr = mfmsr();
1192 /* default to book3s_64 (970fx) */
1193 vcpu->arch.pvr = 0x3C0301;
1194 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1195 vcpu_book3s->slb_nr = 64;
1196
1197 /* remember where some real-mode handlers are */
1198 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1199 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1200 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf021ec9c2010-01-08 02:58:06 +01001201 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001202
1203 vcpu->arch.shadow_msr = MSR_USER64;
1204
Alexander Graf9cc5e952010-04-16 00:11:45 +02001205 err = kvmppc_mmu_init(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001206 if (err < 0)
Alexander Grafc7f38f42010-04-16 00:11:40 +02001207 goto free_shadow_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001208
1209 return vcpu;
1210
Alexander Grafc7f38f42010-04-16 00:11:40 +02001211free_shadow_vcpu:
1212 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001213free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001214 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001215out:
1216 return ERR_PTR(err);
1217}
1218
1219void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1220{
1221 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1222
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001223 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001224 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001225 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001226}
1227
1228extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1229int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1230{
1231 int ret;
Alexander Graf180a34d2010-01-15 14:49:11 +01001232 struct thread_struct ext_bkp;
Alexander Grafa2b07662010-03-24 21:48:31 +01001233#ifdef CONFIG_ALTIVEC
Alexander Graf180a34d2010-01-15 14:49:11 +01001234 bool save_vec = current->thread.used_vr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001235#endif
1236#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +01001237 bool save_vsx = current->thread.used_vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001238#endif
Alexander Graf180a34d2010-01-15 14:49:11 +01001239 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001240
1241 /* No need to go into the guest when all we do is going out */
1242 if (signal_pending(current)) {
1243 kvm_run->exit_reason = KVM_EXIT_INTR;
1244 return -EINTR;
1245 }
1246
Alexander Graf180a34d2010-01-15 14:49:11 +01001247 /* Save FPU state in stack */
1248 if (current->thread.regs->msr & MSR_FP)
1249 giveup_fpu(current);
1250 memcpy(ext_bkp.fpr, current->thread.fpr, sizeof(current->thread.fpr));
1251 ext_bkp.fpscr = current->thread.fpscr;
1252 ext_bkp.fpexc_mode = current->thread.fpexc_mode;
1253
1254#ifdef CONFIG_ALTIVEC
1255 /* Save Altivec state in stack */
1256 if (save_vec) {
1257 if (current->thread.regs->msr & MSR_VEC)
1258 giveup_altivec(current);
1259 memcpy(ext_bkp.vr, current->thread.vr, sizeof(ext_bkp.vr));
1260 ext_bkp.vscr = current->thread.vscr;
1261 ext_bkp.vrsave = current->thread.vrsave;
1262 }
1263 ext_bkp.used_vr = current->thread.used_vr;
1264#endif
1265
1266#ifdef CONFIG_VSX
1267 /* Save VSX state in stack */
1268 if (save_vsx && (current->thread.regs->msr & MSR_VSX))
1269 __giveup_vsx(current);
1270 ext_bkp.used_vsr = current->thread.used_vsr;
1271#endif
1272
1273 /* Remember the MSR with disabled extensions */
1274 ext_msr = current->thread.regs->msr;
1275
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001276 /* XXX we get called with irq disabled - change that! */
1277 local_irq_enable();
1278
Alexander Grafd1bab742010-02-19 11:00:35 +01001279 /* Preload FPU if it's enabled */
1280 if (vcpu->arch.msr & MSR_FP)
1281 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1282
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001283 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1284
1285 local_irq_disable();
1286
Alexander Graf180a34d2010-01-15 14:49:11 +01001287 current->thread.regs->msr = ext_msr;
1288
1289 /* Make sure we save the guest FPU/Altivec/VSX state */
1290 kvmppc_giveup_ext(vcpu, MSR_FP);
1291 kvmppc_giveup_ext(vcpu, MSR_VEC);
1292 kvmppc_giveup_ext(vcpu, MSR_VSX);
1293
1294 /* Restore FPU state from stack */
1295 memcpy(current->thread.fpr, ext_bkp.fpr, sizeof(ext_bkp.fpr));
1296 current->thread.fpscr = ext_bkp.fpscr;
1297 current->thread.fpexc_mode = ext_bkp.fpexc_mode;
1298
1299#ifdef CONFIG_ALTIVEC
1300 /* Restore Altivec state from stack */
1301 if (save_vec && current->thread.used_vr) {
1302 memcpy(current->thread.vr, ext_bkp.vr, sizeof(ext_bkp.vr));
1303 current->thread.vscr = ext_bkp.vscr;
1304 current->thread.vrsave= ext_bkp.vrsave;
1305 }
1306 current->thread.used_vr = ext_bkp.used_vr;
1307#endif
1308
1309#ifdef CONFIG_VSX
1310 current->thread.used_vsr = ext_bkp.used_vsr;
1311#endif
1312
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001313 return ret;
1314}
1315
1316static int kvmppc_book3s_init(void)
1317{
1318 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), THIS_MODULE);
1319}
1320
1321static void kvmppc_book3s_exit(void)
1322{
1323 kvm_exit();
1324}
1325
1326module_init(kvmppc_book3s_init);
1327module_exit(kvmppc_book3s_exit);