blob: f8ac26599f6dcecc702fde89372ebd44039c1abf [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++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200778
779#ifdef CONFIG_PPC_BOOK3S_32
780 /* We set segments as unused segments when invalidating them. So
781 * treat the respective fault as segment fault. */
782 if (to_svcpu(vcpu)->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT]
783 == SR_INVALID) {
784 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
785 r = RESUME_GUEST;
786 break;
787 }
788#endif
789
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000790 /* only care about PTEG not found errors, but leave NX alone */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200791 if (to_svcpu(vcpu)->shadow_srr1 & 0x40000000) {
792 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000793 vcpu->stat.sp_instruc++;
794 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
795 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
796 /*
797 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
798 * so we can't use the NX bit inside the guest. Let's cross our fingers,
799 * that no guest that needs the dcbz hack does NX.
800 */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200801 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFULL);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100802 r = RESUME_GUEST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000803 } else {
Alexander Grafc7f38f42010-04-16 00:11:40 +0200804 vcpu->arch.msr |= to_svcpu(vcpu)->shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000805 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Grafc7f38f42010-04-16 00:11:40 +0200806 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000807 r = RESUME_GUEST;
808 }
809 break;
810 case BOOK3S_INTERRUPT_DATA_STORAGE:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200811 {
812 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000813 vcpu->stat.pf_storage++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200814
815#ifdef CONFIG_PPC_BOOK3S_32
816 /* We set segments as unused segments when invalidating them. So
817 * treat the respective fault as segment fault. */
818 if ((to_svcpu(vcpu)->sr[dar >> SID_SHIFT]) == SR_INVALID) {
819 kvmppc_mmu_map_segment(vcpu, dar);
820 r = RESUME_GUEST;
821 break;
822 }
823#endif
824
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000825 /* The only case we need to handle is missing shadow PTEs */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200826 if (to_svcpu(vcpu)->fault_dsisr & DSISR_NOHPTE) {
827 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000828 } else {
Alexander Grafc7f38f42010-04-16 00:11:40 +0200829 vcpu->arch.dear = dar;
830 to_book3s(vcpu)->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000831 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
832 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.dear, ~0xFFFULL);
833 r = RESUME_GUEST;
834 }
835 break;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200836 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000837 case BOOK3S_INTERRUPT_DATA_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200838 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
839 vcpu->arch.dear = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000840 kvmppc_book3s_queue_irqprio(vcpu,
841 BOOK3S_INTERRUPT_DATA_SEGMENT);
842 }
843 r = RESUME_GUEST;
844 break;
845 case BOOK3S_INTERRUPT_INST_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200846 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000847 kvmppc_book3s_queue_irqprio(vcpu,
848 BOOK3S_INTERRUPT_INST_SEGMENT);
849 }
850 r = RESUME_GUEST;
851 break;
852 /* We're good on these - the host merely wanted to get our attention */
853 case BOOK3S_INTERRUPT_DECREMENTER:
854 vcpu->stat.dec_exits++;
855 r = RESUME_GUEST;
856 break;
857 case BOOK3S_INTERRUPT_EXTERNAL:
858 vcpu->stat.ext_intr_exits++;
859 r = RESUME_GUEST;
860 break;
861 case BOOK3S_INTERRUPT_PROGRAM:
862 {
863 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100864 ulong flags;
865
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100866program_interrupt:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200867 flags = to_svcpu(vcpu)->shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000868
869 if (vcpu->arch.msr & MSR_PR) {
870#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +0200871 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 +0000872#endif
Alexander Grafc7f38f42010-04-16 00:11:40 +0200873 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000874 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100875 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000876 r = RESUME_GUEST;
877 break;
878 }
879 }
880
881 vcpu->stat.emulated_inst_exits++;
882 er = kvmppc_emulate_instruction(run, vcpu);
883 switch (er) {
884 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100885 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000886 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100887 case EMULATE_AGAIN:
888 r = RESUME_GUEST;
889 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000890 case EMULATE_FAIL:
891 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200892 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
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;
Alexander Grafe5c29e92010-02-19 11:00:43 +0100896 case EMULATE_DO_MMIO:
897 run->exit_reason = KVM_EXIT_MMIO;
898 r = RESUME_HOST_NV;
899 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000900 default:
901 BUG();
902 }
903 break;
904 }
905 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafad0a0482010-03-24 21:48:30 +0100906 // XXX make user settable
907 if (vcpu->arch.osi_enabled &&
908 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
909 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
910 u64 *gprs = run->osi.gprs;
911 int i;
912
913 run->exit_reason = KVM_EXIT_OSI;
914 for (i = 0; i < 32; i++)
915 gprs[i] = kvmppc_get_gpr(vcpu, i);
916 vcpu->arch.osi_needed = 1;
917 r = RESUME_HOST_NV;
918
919 } else {
920 vcpu->stat.syscall_exits++;
921 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
922 r = RESUME_GUEST;
923 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000924 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000925 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000926 case BOOK3S_INTERRUPT_ALTIVEC:
927 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100928 {
929 int ext_msr = 0;
930
931 switch (exit_nr) {
932 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
933 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
934 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
935 }
936
937 switch (kvmppc_check_ext(vcpu, exit_nr)) {
938 case EMULATE_DONE:
939 /* everything ok - let's enable the ext */
940 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
941 break;
942 case EMULATE_FAIL:
943 /* we need to emulate this instruction */
944 goto program_interrupt;
945 break;
946 default:
947 /* nothing to worry about - go again */
948 break;
949 }
Alexander Graf180a34d2010-01-15 14:49:11 +0100950 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100951 }
Alexander Grafca7f4202010-03-24 21:48:28 +0100952 case BOOK3S_INTERRUPT_ALIGNMENT:
953 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
954 to_book3s(vcpu)->dsisr = kvmppc_alignment_dsisr(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +0200955 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +0100956 vcpu->arch.dear = kvmppc_alignment_dar(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +0200957 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +0100958 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
959 }
960 r = RESUME_GUEST;
961 break;
Alexander Graf180a34d2010-01-15 14:49:11 +0100962 case BOOK3S_INTERRUPT_MACHINE_CHECK:
963 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000964 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
965 r = RESUME_GUEST;
966 break;
967 default:
968 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +0100969 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200970 exit_nr, kvmppc_get_pc(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000971 r = RESUME_HOST;
972 BUG();
973 break;
974 }
975
976
977 if (!(r & RESUME_HOST)) {
978 /* To avoid clobbering exit_reason, only check for signals if
979 * we aren't already exiting to userspace for some other
980 * reason. */
981 if (signal_pending(current)) {
982#ifdef EXIT_DEBUG
983 printk(KERN_EMERG "KVM: Going back to host\n");
984#endif
985 vcpu->stat.signal_exits++;
986 run->exit_reason = KVM_EXIT_INTR;
987 r = -EINTR;
988 } else {
989 /* In case an interrupt came in that was triggered
990 * from userspace (like DEC), we need to check what
991 * to inject now! */
992 kvmppc_core_deliver_interrupts(vcpu);
993 }
994 }
995
996#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +0200997 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 +0000998#endif
999
1000 return r;
1001}
1002
1003int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1004{
1005 return 0;
1006}
1007
1008int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1009{
1010 int i;
1011
Alexander Grafa56cf342010-03-24 21:48:23 +01001012 vcpu_load(vcpu);
1013
Alexander Grafc7f38f42010-04-16 00:11:40 +02001014 regs->pc = kvmppc_get_pc(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001015 regs->cr = kvmppc_get_cr(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001016 regs->ctr = kvmppc_get_ctr(vcpu);
1017 regs->lr = kvmppc_get_lr(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001018 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001019 regs->msr = vcpu->arch.msr;
1020 regs->srr0 = vcpu->arch.srr0;
1021 regs->srr1 = vcpu->arch.srr1;
1022 regs->pid = vcpu->arch.pid;
1023 regs->sprg0 = vcpu->arch.sprg0;
1024 regs->sprg1 = vcpu->arch.sprg1;
1025 regs->sprg2 = vcpu->arch.sprg2;
1026 regs->sprg3 = vcpu->arch.sprg3;
1027 regs->sprg5 = vcpu->arch.sprg4;
1028 regs->sprg6 = vcpu->arch.sprg5;
1029 regs->sprg7 = vcpu->arch.sprg6;
1030
1031 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001032 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001033
Alexander Grafa56cf342010-03-24 21:48:23 +01001034 vcpu_put(vcpu);
1035
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001036 return 0;
1037}
1038
1039int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1040{
1041 int i;
1042
Alexander Grafa56cf342010-03-24 21:48:23 +01001043 vcpu_load(vcpu);
1044
Alexander Grafc7f38f42010-04-16 00:11:40 +02001045 kvmppc_set_pc(vcpu, regs->pc);
Alexander Graf992b5b22010-01-08 02:58:02 +01001046 kvmppc_set_cr(vcpu, regs->cr);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001047 kvmppc_set_ctr(vcpu, regs->ctr);
1048 kvmppc_set_lr(vcpu, regs->lr);
Alexander Graf992b5b22010-01-08 02:58:02 +01001049 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001050 kvmppc_set_msr(vcpu, regs->msr);
1051 vcpu->arch.srr0 = regs->srr0;
1052 vcpu->arch.srr1 = regs->srr1;
1053 vcpu->arch.sprg0 = regs->sprg0;
1054 vcpu->arch.sprg1 = regs->sprg1;
1055 vcpu->arch.sprg2 = regs->sprg2;
1056 vcpu->arch.sprg3 = regs->sprg3;
1057 vcpu->arch.sprg5 = regs->sprg4;
1058 vcpu->arch.sprg6 = regs->sprg5;
1059 vcpu->arch.sprg7 = regs->sprg6;
1060
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001061 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1062 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001063
Alexander Grafa56cf342010-03-24 21:48:23 +01001064 vcpu_put(vcpu);
1065
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001066 return 0;
1067}
1068
1069int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1070 struct kvm_sregs *sregs)
1071{
Alexander Grafe15a1132009-11-30 03:02:02 +00001072 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1073 int i;
1074
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001075 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +00001076
1077 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1078 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1079 for (i = 0; i < 64; i++) {
1080 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
1081 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1082 }
1083 } else {
1084 for (i = 0; i < 16; i++) {
1085 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1086 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1087 }
1088 for (i = 0; i < 8; i++) {
1089 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1090 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1091 }
1092 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001093 return 0;
1094}
1095
1096int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1097 struct kvm_sregs *sregs)
1098{
Alexander Grafe15a1132009-11-30 03:02:02 +00001099 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1100 int i;
1101
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001102 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001103
1104 vcpu3s->sdr1 = sregs->u.s.sdr1;
1105 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1106 for (i = 0; i < 64; i++) {
1107 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1108 sregs->u.s.ppc64.slb[i].slbe);
1109 }
1110 } else {
1111 for (i = 0; i < 16; i++) {
1112 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1113 }
1114 for (i = 0; i < 8; i++) {
1115 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1116 (u32)sregs->u.s.ppc32.ibat[i]);
1117 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1118 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1119 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1120 (u32)sregs->u.s.ppc32.dbat[i]);
1121 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1122 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1123 }
1124 }
1125
1126 /* Flush the MMU after messing with the segments */
1127 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001128 return 0;
1129}
1130
1131int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1132{
1133 return -ENOTSUPP;
1134}
1135
1136int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1137{
1138 return -ENOTSUPP;
1139}
1140
1141int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1142 struct kvm_translation *tr)
1143{
1144 return 0;
1145}
1146
1147/*
1148 * Get (and clear) the dirty memory log for a memory slot.
1149 */
1150int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1151 struct kvm_dirty_log *log)
1152{
1153 struct kvm_memory_slot *memslot;
1154 struct kvm_vcpu *vcpu;
1155 ulong ga, ga_end;
1156 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001157 int r;
1158 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001159
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001160 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001161
1162 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1163 if (r)
1164 goto out;
1165
1166 /* If nothing is dirty, don't bother messing with page tables. */
1167 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001168 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001169
1170 ga = memslot->base_gfn << PAGE_SHIFT;
1171 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1172
1173 kvm_for_each_vcpu(n, vcpu, kvm)
1174 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1175
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001176 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001177 memset(memslot->dirty_bitmap, 0, n);
1178 }
1179
1180 r = 0;
1181out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001182 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001183 return r;
1184}
1185
1186int kvmppc_core_check_processor_compat(void)
1187{
1188 return 0;
1189}
1190
1191struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1192{
1193 struct kvmppc_vcpu_book3s *vcpu_book3s;
1194 struct kvm_vcpu *vcpu;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001195 int err = -ENOMEM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001196
Alexander Graf032c3402010-02-19 12:24:33 +01001197 vcpu_book3s = vmalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Grafc7f38f42010-04-16 00:11:40 +02001198 if (!vcpu_book3s)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001199 goto out;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001200
Alexander Graf7e821d32010-02-22 16:52:08 +01001201 memset(vcpu_book3s, 0, sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001202
Alexander Grafc7f38f42010-04-16 00:11:40 +02001203 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
1204 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
1205 if (!vcpu_book3s->shadow_vcpu)
1206 goto free_vcpu;
1207
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001208 vcpu = &vcpu_book3s->vcpu;
1209 err = kvm_vcpu_init(vcpu, kvm, id);
1210 if (err)
Alexander Grafc7f38f42010-04-16 00:11:40 +02001211 goto free_shadow_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001212
1213 vcpu->arch.host_retip = kvm_return_point;
1214 vcpu->arch.host_msr = mfmsr();
1215 /* default to book3s_64 (970fx) */
1216 vcpu->arch.pvr = 0x3C0301;
1217 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1218 vcpu_book3s->slb_nr = 64;
1219
1220 /* remember where some real-mode handlers are */
1221 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1222 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1223 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf021ec9c2010-01-08 02:58:06 +01001224 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001225
1226 vcpu->arch.shadow_msr = MSR_USER64;
1227
Alexander Graf9cc5e952010-04-16 00:11:45 +02001228 err = kvmppc_mmu_init(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001229 if (err < 0)
Alexander Grafc7f38f42010-04-16 00:11:40 +02001230 goto free_shadow_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001231
1232 return vcpu;
1233
Alexander Grafc7f38f42010-04-16 00:11:40 +02001234free_shadow_vcpu:
1235 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001236free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001237 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001238out:
1239 return ERR_PTR(err);
1240}
1241
1242void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1243{
1244 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1245
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001246 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001247 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001248 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001249}
1250
1251extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1252int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1253{
1254 int ret;
Alexander Graf180a34d2010-01-15 14:49:11 +01001255 struct thread_struct ext_bkp;
Alexander Grafa2b07662010-03-24 21:48:31 +01001256#ifdef CONFIG_ALTIVEC
Alexander Graf180a34d2010-01-15 14:49:11 +01001257 bool save_vec = current->thread.used_vr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001258#endif
1259#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +01001260 bool save_vsx = current->thread.used_vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001261#endif
Alexander Graf180a34d2010-01-15 14:49:11 +01001262 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001263
1264 /* No need to go into the guest when all we do is going out */
1265 if (signal_pending(current)) {
1266 kvm_run->exit_reason = KVM_EXIT_INTR;
1267 return -EINTR;
1268 }
1269
Alexander Graf180a34d2010-01-15 14:49:11 +01001270 /* Save FPU state in stack */
1271 if (current->thread.regs->msr & MSR_FP)
1272 giveup_fpu(current);
1273 memcpy(ext_bkp.fpr, current->thread.fpr, sizeof(current->thread.fpr));
1274 ext_bkp.fpscr = current->thread.fpscr;
1275 ext_bkp.fpexc_mode = current->thread.fpexc_mode;
1276
1277#ifdef CONFIG_ALTIVEC
1278 /* Save Altivec state in stack */
1279 if (save_vec) {
1280 if (current->thread.regs->msr & MSR_VEC)
1281 giveup_altivec(current);
1282 memcpy(ext_bkp.vr, current->thread.vr, sizeof(ext_bkp.vr));
1283 ext_bkp.vscr = current->thread.vscr;
1284 ext_bkp.vrsave = current->thread.vrsave;
1285 }
1286 ext_bkp.used_vr = current->thread.used_vr;
1287#endif
1288
1289#ifdef CONFIG_VSX
1290 /* Save VSX state in stack */
1291 if (save_vsx && (current->thread.regs->msr & MSR_VSX))
1292 __giveup_vsx(current);
1293 ext_bkp.used_vsr = current->thread.used_vsr;
1294#endif
1295
1296 /* Remember the MSR with disabled extensions */
1297 ext_msr = current->thread.regs->msr;
1298
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001299 /* XXX we get called with irq disabled - change that! */
1300 local_irq_enable();
1301
Alexander Grafd1bab742010-02-19 11:00:35 +01001302 /* Preload FPU if it's enabled */
1303 if (vcpu->arch.msr & MSR_FP)
1304 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1305
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001306 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1307
1308 local_irq_disable();
1309
Alexander Graf180a34d2010-01-15 14:49:11 +01001310 current->thread.regs->msr = ext_msr;
1311
1312 /* Make sure we save the guest FPU/Altivec/VSX state */
1313 kvmppc_giveup_ext(vcpu, MSR_FP);
1314 kvmppc_giveup_ext(vcpu, MSR_VEC);
1315 kvmppc_giveup_ext(vcpu, MSR_VSX);
1316
1317 /* Restore FPU state from stack */
1318 memcpy(current->thread.fpr, ext_bkp.fpr, sizeof(ext_bkp.fpr));
1319 current->thread.fpscr = ext_bkp.fpscr;
1320 current->thread.fpexc_mode = ext_bkp.fpexc_mode;
1321
1322#ifdef CONFIG_ALTIVEC
1323 /* Restore Altivec state from stack */
1324 if (save_vec && current->thread.used_vr) {
1325 memcpy(current->thread.vr, ext_bkp.vr, sizeof(ext_bkp.vr));
1326 current->thread.vscr = ext_bkp.vscr;
1327 current->thread.vrsave= ext_bkp.vrsave;
1328 }
1329 current->thread.used_vr = ext_bkp.used_vr;
1330#endif
1331
1332#ifdef CONFIG_VSX
1333 current->thread.used_vsr = ext_bkp.used_vsr;
1334#endif
1335
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001336 return ret;
1337}
1338
1339static int kvmppc_book3s_init(void)
1340{
1341 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), THIS_MODULE);
1342}
1343
1344static void kvmppc_book3s_exit(void)
1345{
1346 kvm_exit();
1347}
1348
1349module_init(kvmppc_book3s_init);
1350module_exit(kvmppc_book3s_exit);