blob: 9f97dbe25e4eec866dd11e8c2e36dd4b6f107732 [file] [log] [blame]
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001/*
2 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
3 *
4 * Authors:
5 * Alexander Graf <agraf@suse.de>
6 * Kevin Wolf <mail@kevin-wolf.de>
7 *
8 * Description:
9 * This file is derived from arch/powerpc/kvm/44x.c,
10 * by Hollis Blanchard <hollisb@us.ibm.com>.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License, version 2, as
14 * published by the Free Software Foundation.
15 */
16
17#include <linux/kvm_host.h>
18#include <linux/err.h>
19
20#include <asm/reg.h>
21#include <asm/cputable.h>
22#include <asm/cacheflush.h>
23#include <asm/tlbflush.h>
24#include <asm/uaccess.h>
25#include <asm/io.h>
26#include <asm/kvm_ppc.h>
27#include <asm/kvm_book3s.h>
28#include <asm/mmu_context.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/gfp.h>
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000030#include <linux/sched.h>
31#include <linux/vmalloc.h>
Alexander Graf9fb244a2010-03-24 21:48:32 +010032#include <linux/highmem.h>
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000033
34#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
35
36/* #define EXIT_DEBUG */
37/* #define EXIT_DEBUG_SIMPLE */
Alexander Graf180a34d2010-01-15 14:49:11 +010038/* #define DEBUG_EXT */
39
Alexander Grafc8c0b6f2010-02-19 11:00:34 +010040static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
41 ulong msr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000042
Alexander Graf07b09072010-04-16 00:11:53 +020043/* Some compatibility defines */
44#ifdef CONFIG_PPC_BOOK3S_32
45#define MSR_USER32 MSR_USER
46#define MSR_USER64 MSR_USER
47#define HW_PAGE_SIZE PAGE_SIZE
48#endif
49
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000050struct kvm_stats_debugfs_item debugfs_entries[] = {
51 { "exits", VCPU_STAT(sum_exits) },
52 { "mmio", VCPU_STAT(mmio_exits) },
53 { "sig", VCPU_STAT(signal_exits) },
54 { "sysc", VCPU_STAT(syscall_exits) },
55 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
56 { "dec", VCPU_STAT(dec_exits) },
57 { "ext_intr", VCPU_STAT(ext_intr_exits) },
58 { "queue_intr", VCPU_STAT(queue_intr) },
59 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
60 { "pf_storage", VCPU_STAT(pf_storage) },
61 { "sp_storage", VCPU_STAT(sp_storage) },
62 { "pf_instruc", VCPU_STAT(pf_instruc) },
63 { "sp_instruc", VCPU_STAT(sp_instruc) },
64 { "ld", VCPU_STAT(ld) },
65 { "ld_slow", VCPU_STAT(ld_slow) },
66 { "st", VCPU_STAT(st) },
67 { "st_slow", VCPU_STAT(st_slow) },
68 { NULL }
69};
70
71void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
72{
73}
74
75void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
76{
77}
78
79void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
80{
Alexander Grafc7f38f42010-04-16 00:11:40 +020081#ifdef CONFIG_PPC_BOOK3S_64
82 memcpy(to_svcpu(vcpu)->slb, to_book3s(vcpu)->slb_shadow, sizeof(to_svcpu(vcpu)->slb));
83 memcpy(&get_paca()->shadow_vcpu, to_book3s(vcpu)->shadow_vcpu,
Alexander Graf7e57cba2010-01-08 02:58:03 +010084 sizeof(get_paca()->shadow_vcpu));
Alexander Grafc7f38f42010-04-16 00:11:40 +020085 to_svcpu(vcpu)->slb_max = to_book3s(vcpu)->slb_shadow_max;
86#endif
87
88#ifdef CONFIG_PPC_BOOK3S_32
89 current->thread.kvm_shadow_vcpu = to_book3s(vcpu)->shadow_vcpu;
90#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +000091}
92
93void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
94{
Alexander Grafc7f38f42010-04-16 00:11:40 +020095#ifdef CONFIG_PPC_BOOK3S_64
96 memcpy(to_book3s(vcpu)->slb_shadow, to_svcpu(vcpu)->slb, sizeof(to_svcpu(vcpu)->slb));
97 memcpy(to_book3s(vcpu)->shadow_vcpu, &get_paca()->shadow_vcpu,
Alexander Graf7e57cba2010-01-08 02:58:03 +010098 sizeof(get_paca()->shadow_vcpu));
Alexander Grafc7f38f42010-04-16 00:11:40 +020099 to_book3s(vcpu)->slb_shadow_max = to_svcpu(vcpu)->slb_max;
100#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100101
102 kvmppc_giveup_ext(vcpu, MSR_FP);
103 kvmppc_giveup_ext(vcpu, MSR_VEC);
104 kvmppc_giveup_ext(vcpu, MSR_VSX);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000105}
106
Alexander Graf0bb1fb72009-12-21 20:21:25 +0100107#if defined(EXIT_DEBUG)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000108static u32 kvmppc_get_dec(struct kvm_vcpu *vcpu)
109{
110 u64 jd = mftb() - vcpu->arch.dec_jiffies;
111 return vcpu->arch.dec - jd;
112}
113#endif
114
Alexander Grafa76f8492010-01-15 14:49:14 +0100115static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu)
116{
117 vcpu->arch.shadow_msr = vcpu->arch.msr;
118 /* Guest MSR values */
119 vcpu->arch.shadow_msr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE |
120 MSR_BE | MSR_DE;
121 /* Process MSR values */
122 vcpu->arch.shadow_msr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR |
123 MSR_EE;
124 /* External providers the guest reserved */
125 vcpu->arch.shadow_msr |= (vcpu->arch.msr & vcpu->arch.guest_owned_ext);
126 /* 64-bit Process MSR values */
127#ifdef CONFIG_PPC_BOOK3S_64
128 vcpu->arch.shadow_msr |= MSR_ISF | MSR_HV;
129#endif
130}
131
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000132void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
133{
134 ulong old_msr = vcpu->arch.msr;
135
136#ifdef EXIT_DEBUG
137 printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr);
138#endif
Alexander Grafa76f8492010-01-15 14:49:14 +0100139
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000140 msr &= to_book3s(vcpu)->msr_mask;
141 vcpu->arch.msr = msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100142 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000143
144 if (msr & (MSR_WE|MSR_POW)) {
145 if (!vcpu->arch.pending_exceptions) {
146 kvm_vcpu_block(vcpu);
147 vcpu->stat.halt_wakeup++;
148 }
149 }
150
Alexander Graff7bc74e2010-04-20 02:49:48 +0200151 if ((vcpu->arch.msr & (MSR_PR|MSR_IR|MSR_DR)) !=
152 (old_msr & (MSR_PR|MSR_IR|MSR_DR))) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000153 kvmppc_mmu_flush_segments(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +0200154 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000155 }
Alexander Grafd1bab742010-02-19 11:00:35 +0100156
157 /* Preload FPU if it's enabled */
158 if (vcpu->arch.msr & MSR_FP)
159 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000160}
161
162void kvmppc_inject_interrupt(struct kvm_vcpu *vcpu, int vec, u64 flags)
163{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200164 vcpu->arch.srr0 = kvmppc_get_pc(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000165 vcpu->arch.srr1 = vcpu->arch.msr | flags;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200166 kvmppc_set_pc(vcpu, to_book3s(vcpu)->hior + vec);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000167 vcpu->arch.mmu.reset_msr(vcpu);
168}
169
Alexander Graf583617b2009-12-21 20:21:23 +0100170static int kvmppc_book3s_vec2irqprio(unsigned int vec)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000171{
172 unsigned int prio;
173
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000174 switch (vec) {
175 case 0x100: prio = BOOK3S_IRQPRIO_SYSTEM_RESET; break;
176 case 0x200: prio = BOOK3S_IRQPRIO_MACHINE_CHECK; break;
177 case 0x300: prio = BOOK3S_IRQPRIO_DATA_STORAGE; break;
178 case 0x380: prio = BOOK3S_IRQPRIO_DATA_SEGMENT; break;
179 case 0x400: prio = BOOK3S_IRQPRIO_INST_STORAGE; break;
180 case 0x480: prio = BOOK3S_IRQPRIO_INST_SEGMENT; break;
181 case 0x500: prio = BOOK3S_IRQPRIO_EXTERNAL; break;
182 case 0x600: prio = BOOK3S_IRQPRIO_ALIGNMENT; break;
183 case 0x700: prio = BOOK3S_IRQPRIO_PROGRAM; break;
184 case 0x800: prio = BOOK3S_IRQPRIO_FP_UNAVAIL; break;
185 case 0x900: prio = BOOK3S_IRQPRIO_DECREMENTER; break;
186 case 0xc00: prio = BOOK3S_IRQPRIO_SYSCALL; break;
187 case 0xd00: prio = BOOK3S_IRQPRIO_DEBUG; break;
188 case 0xf20: prio = BOOK3S_IRQPRIO_ALTIVEC; break;
189 case 0xf40: prio = BOOK3S_IRQPRIO_VSX; break;
190 default: prio = BOOK3S_IRQPRIO_MAX; break;
191 }
192
Alexander Graf583617b2009-12-21 20:21:23 +0100193 return prio;
194}
195
Alexander Graf7706664d2009-12-21 20:21:24 +0100196static void kvmppc_book3s_dequeue_irqprio(struct kvm_vcpu *vcpu,
197 unsigned int vec)
198{
199 clear_bit(kvmppc_book3s_vec2irqprio(vec),
200 &vcpu->arch.pending_exceptions);
201}
202
Alexander Graf583617b2009-12-21 20:21:23 +0100203void kvmppc_book3s_queue_irqprio(struct kvm_vcpu *vcpu, unsigned int vec)
204{
205 vcpu->stat.queue_intr++;
206
207 set_bit(kvmppc_book3s_vec2irqprio(vec),
208 &vcpu->arch.pending_exceptions);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000209#ifdef EXIT_DEBUG
210 printk(KERN_INFO "Queueing interrupt %x\n", vec);
211#endif
212}
213
214
Alexander Graf25a8a022010-01-08 02:58:07 +0100215void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000216{
Alexander Graf25a8a022010-01-08 02:58:07 +0100217 to_book3s(vcpu)->prog_flags = flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000218 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_PROGRAM);
219}
220
221void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
222{
223 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
224}
225
226int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
227{
228 return test_bit(BOOK3S_INTERRUPT_DECREMENTER >> 7, &vcpu->arch.pending_exceptions);
229}
230
Alexander Graf7706664d2009-12-21 20:21:24 +0100231void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
232{
233 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_DECREMENTER);
234}
235
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000236void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
237 struct kvm_interrupt *irq)
238{
239 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
240}
241
Alexander Graf18978762010-03-24 21:48:18 +0100242void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
243 struct kvm_interrupt *irq)
244{
245 kvmppc_book3s_dequeue_irqprio(vcpu, BOOK3S_INTERRUPT_EXTERNAL);
246}
247
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000248int kvmppc_book3s_irqprio_deliver(struct kvm_vcpu *vcpu, unsigned int priority)
249{
250 int deliver = 1;
251 int vec = 0;
Alexander Graf25a8a022010-01-08 02:58:07 +0100252 ulong flags = 0ULL;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000253
254 switch (priority) {
255 case BOOK3S_IRQPRIO_DECREMENTER:
256 deliver = vcpu->arch.msr & MSR_EE;
257 vec = BOOK3S_INTERRUPT_DECREMENTER;
258 break;
259 case BOOK3S_IRQPRIO_EXTERNAL:
260 deliver = vcpu->arch.msr & MSR_EE;
261 vec = BOOK3S_INTERRUPT_EXTERNAL;
262 break;
263 case BOOK3S_IRQPRIO_SYSTEM_RESET:
264 vec = BOOK3S_INTERRUPT_SYSTEM_RESET;
265 break;
266 case BOOK3S_IRQPRIO_MACHINE_CHECK:
267 vec = BOOK3S_INTERRUPT_MACHINE_CHECK;
268 break;
269 case BOOK3S_IRQPRIO_DATA_STORAGE:
270 vec = BOOK3S_INTERRUPT_DATA_STORAGE;
271 break;
272 case BOOK3S_IRQPRIO_INST_STORAGE:
273 vec = BOOK3S_INTERRUPT_INST_STORAGE;
274 break;
275 case BOOK3S_IRQPRIO_DATA_SEGMENT:
276 vec = BOOK3S_INTERRUPT_DATA_SEGMENT;
277 break;
278 case BOOK3S_IRQPRIO_INST_SEGMENT:
279 vec = BOOK3S_INTERRUPT_INST_SEGMENT;
280 break;
281 case BOOK3S_IRQPRIO_ALIGNMENT:
282 vec = BOOK3S_INTERRUPT_ALIGNMENT;
283 break;
284 case BOOK3S_IRQPRIO_PROGRAM:
285 vec = BOOK3S_INTERRUPT_PROGRAM;
Alexander Graf25a8a022010-01-08 02:58:07 +0100286 flags = to_book3s(vcpu)->prog_flags;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000287 break;
288 case BOOK3S_IRQPRIO_VSX:
289 vec = BOOK3S_INTERRUPT_VSX;
290 break;
291 case BOOK3S_IRQPRIO_ALTIVEC:
292 vec = BOOK3S_INTERRUPT_ALTIVEC;
293 break;
294 case BOOK3S_IRQPRIO_FP_UNAVAIL:
295 vec = BOOK3S_INTERRUPT_FP_UNAVAIL;
296 break;
297 case BOOK3S_IRQPRIO_SYSCALL:
298 vec = BOOK3S_INTERRUPT_SYSCALL;
299 break;
300 case BOOK3S_IRQPRIO_DEBUG:
301 vec = BOOK3S_INTERRUPT_TRACE;
302 break;
303 case BOOK3S_IRQPRIO_PERFORMANCE_MONITOR:
304 vec = BOOK3S_INTERRUPT_PERFMON;
305 break;
306 default:
307 deliver = 0;
308 printk(KERN_ERR "KVM: Unknown interrupt: 0x%x\n", priority);
309 break;
310 }
311
312#if 0
313 printk(KERN_INFO "Deliver interrupt 0x%x? %x\n", vec, deliver);
314#endif
315
316 if (deliver)
Alexander Graf25a8a022010-01-08 02:58:07 +0100317 kvmppc_inject_interrupt(vcpu, vec, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000318
319 return deliver;
320}
321
322void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
323{
324 unsigned long *pending = &vcpu->arch.pending_exceptions;
325 unsigned int priority;
326
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000327#ifdef EXIT_DEBUG
328 if (vcpu->arch.pending_exceptions)
329 printk(KERN_EMERG "KVM: Check pending: %lx\n", vcpu->arch.pending_exceptions);
330#endif
331 priority = __ffs(*pending);
Alexander Grafada7ba12010-04-16 00:11:56 +0200332 while (priority < BOOK3S_IRQPRIO_MAX) {
Alexander Graf7706664d2009-12-21 20:21:24 +0100333 if (kvmppc_book3s_irqprio_deliver(vcpu, priority) &&
334 (priority != BOOK3S_IRQPRIO_DECREMENTER)) {
335 /* DEC interrupts get cleared by mtdec */
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000336 clear_bit(priority, &vcpu->arch.pending_exceptions);
337 break;
338 }
339
340 priority = find_next_bit(pending,
341 BITS_PER_BYTE * sizeof(*pending),
342 priority + 1);
343 }
344}
345
346void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
347{
Alexander Grafb83d4a92010-04-20 02:49:54 +0200348 u32 host_pvr;
349
Alexander Grafe15a1132009-11-30 03:02:02 +0000350 vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000351 vcpu->arch.pvr = pvr;
Alexander Graf07b09072010-04-16 00:11:53 +0200352#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000353 if ((pvr >= 0x330000) && (pvr < 0x70330000)) {
354 kvmppc_mmu_book3s_64_init(vcpu);
355 to_book3s(vcpu)->hior = 0xfff00000;
356 to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL;
Alexander Graf07b09072010-04-16 00:11:53 +0200357 } else
358#endif
359 {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000360 kvmppc_mmu_book3s_32_init(vcpu);
361 to_book3s(vcpu)->hior = 0;
362 to_book3s(vcpu)->msr_mask = 0xffffffffULL;
363 }
364
365 /* If we are in hypervisor level on 970, we can tell the CPU to
366 * treat DCBZ as 32 bytes store */
367 vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32;
368 if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) &&
369 !strcmp(cur_cpu_spec->platform, "ppc970"))
370 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
371
Alexander Graf05b0ab12010-03-24 21:48:37 +0100372 /* Cell performs badly if MSR_FEx are set. So let's hope nobody
373 really needs them in a VM on Cell and force disable them. */
374 if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be"))
375 to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1);
Alexander Graf07b09072010-04-16 00:11:53 +0200376
377#ifdef CONFIG_PPC_BOOK3S_32
378 /* 32 bit Book3S always has 32 byte dcbz */
379 vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32;
380#endif
Alexander Grafb83d4a92010-04-20 02:49:54 +0200381
382 /* On some CPUs we can execute paired single operations natively */
383 asm ( "mfpvr %0" : "=r"(host_pvr));
384 switch (host_pvr) {
385 case 0x00080200: /* lonestar 2.0 */
386 case 0x00088202: /* lonestar 2.2 */
387 case 0x70000100: /* gekko 1.0 */
388 case 0x00080100: /* gekko 2.0 */
389 case 0x00083203: /* gekko 2.3a */
390 case 0x00083213: /* gekko 2.3b */
391 case 0x00083204: /* gekko 2.4 */
392 case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */
393 case 0x00087200: /* broadway */
394 vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS;
395 /* Enable HID2.PSE - in case we need it later */
396 mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29));
397 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000398}
399
400/* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To
401 * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to
402 * emulate 32 bytes dcbz length.
403 *
404 * The Book3s_64 inventors also realized this case and implemented a special bit
405 * in the HID5 register, which is a hypervisor ressource. Thus we can't use it.
406 *
407 * My approach here is to patch the dcbz instruction on executing pages.
408 */
409static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte)
410{
Alexander Graf9fb244a2010-03-24 21:48:32 +0100411 struct page *hpage;
412 u64 hpage_offset;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000413 u32 *page;
414 int i;
415
Alexander Graf9fb244a2010-03-24 21:48:32 +0100416 hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
417 if (is_error_page(hpage))
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000418 return;
419
Alexander Graf9fb244a2010-03-24 21:48:32 +0100420 hpage_offset = pte->raddr & ~PAGE_MASK;
421 hpage_offset &= ~0xFFFULL;
422 hpage_offset /= 4;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000423
Alexander Graf9fb244a2010-03-24 21:48:32 +0100424 get_page(hpage);
425 page = kmap_atomic(hpage, KM_USER0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000426
Alexander Graf9fb244a2010-03-24 21:48:32 +0100427 /* patch dcbz into reserved instruction, so we trap */
428 for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++)
429 if ((page[i] & 0xff0007ff) == INS_DCBZ)
430 page[i] &= 0xfffffff7;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000431
Alexander Graf9fb244a2010-03-24 21:48:32 +0100432 kunmap_atomic(page, KM_USER0);
433 put_page(hpage);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000434}
435
436static int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, bool data,
437 struct kvmppc_pte *pte)
438{
439 int relocated = (vcpu->arch.msr & (data ? MSR_DR : MSR_IR));
440 int r;
441
442 if (relocated) {
443 r = vcpu->arch.mmu.xlate(vcpu, eaddr, pte, data);
444 } else {
445 pte->eaddr = eaddr;
446 pte->raddr = eaddr & 0xffffffff;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100447 pte->vpage = VSID_REAL | eaddr >> 12;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000448 pte->may_read = true;
449 pte->may_write = true;
450 pte->may_execute = true;
451 r = 0;
452 }
453
454 return r;
455}
456
457static hva_t kvmppc_bad_hva(void)
458{
459 return PAGE_OFFSET;
460}
461
462static hva_t kvmppc_pte_to_hva(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte,
463 bool read)
464{
465 hva_t hpage;
466
467 if (read && !pte->may_read)
468 goto err;
469
470 if (!read && !pte->may_write)
471 goto err;
472
473 hpage = gfn_to_hva(vcpu->kvm, pte->raddr >> PAGE_SHIFT);
474 if (kvm_is_error_hva(hpage))
475 goto err;
476
477 return hpage | (pte->raddr & ~PAGE_MASK);
478err:
479 return kvmppc_bad_hva();
480}
481
Alexander Graf5467a972010-02-19 11:00:38 +0100482int kvmppc_st(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
483 bool data)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000484{
485 struct kvmppc_pte pte;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000486
487 vcpu->stat.st++;
488
Alexander Graf5467a972010-02-19 11:00:38 +0100489 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
Alexander Graf9fb244a2010-03-24 21:48:32 +0100490 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100491
492 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000493
Alexander Graf9fb244a2010-03-24 21:48:32 +0100494 if (!pte.may_write)
495 return -EPERM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000496
Alexander Graf9fb244a2010-03-24 21:48:32 +0100497 if (kvm_write_guest(vcpu->kvm, pte.raddr, ptr, size))
498 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000499
Alexander Graf5467a972010-02-19 11:00:38 +0100500 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000501}
502
Alexander Graf5467a972010-02-19 11:00:38 +0100503int kvmppc_ld(struct kvm_vcpu *vcpu, ulong *eaddr, int size, void *ptr,
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000504 bool data)
505{
506 struct kvmppc_pte pte;
Alexander Graf5467a972010-02-19 11:00:38 +0100507 hva_t hva = *eaddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000508
509 vcpu->stat.ld++;
510
Alexander Graf5467a972010-02-19 11:00:38 +0100511 if (kvmppc_xlate(vcpu, *eaddr, data, &pte))
512 goto nopte;
513
514 *eaddr = pte.raddr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000515
516 hva = kvmppc_pte_to_hva(vcpu, &pte, true);
517 if (kvm_is_error_hva(hva))
Alexander Graf5467a972010-02-19 11:00:38 +0100518 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000519
520 if (copy_from_user(ptr, (void __user *)hva, size)) {
521 printk(KERN_INFO "kvmppc_ld at 0x%lx failed\n", hva);
Alexander Graf5467a972010-02-19 11:00:38 +0100522 goto mmio;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000523 }
524
Alexander Graf5467a972010-02-19 11:00:38 +0100525 return EMULATE_DONE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000526
Alexander Graf5467a972010-02-19 11:00:38 +0100527nopte:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000528 return -ENOENT;
Alexander Graf5467a972010-02-19 11:00:38 +0100529mmio:
530 return EMULATE_DO_MMIO;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000531}
532
533static int kvmppc_visible_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
534{
535 return kvm_is_visible_gfn(vcpu->kvm, gfn);
536}
537
538int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu,
539 ulong eaddr, int vec)
540{
541 bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE);
542 int r = RESUME_GUEST;
543 int relocated;
544 int page_found = 0;
545 struct kvmppc_pte pte;
546 bool is_mmio = false;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100547 bool dr = (vcpu->arch.msr & MSR_DR) ? true : false;
548 bool ir = (vcpu->arch.msr & MSR_IR) ? true : false;
Alexander Graff7bc74e2010-04-20 02:49:48 +0200549 u64 vsid;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000550
Alexander Graf3eeafd72010-03-24 21:48:17 +0100551 relocated = data ? dr : ir;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000552
553 /* Resolve real address if translation turned on */
554 if (relocated) {
555 page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data);
556 } else {
557 pte.may_execute = true;
558 pte.may_read = true;
559 pte.may_write = true;
560 pte.raddr = eaddr & 0xffffffff;
561 pte.eaddr = eaddr;
562 pte.vpage = eaddr >> 12;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100563 }
564
565 switch (vcpu->arch.msr & (MSR_DR|MSR_IR)) {
566 case 0:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200567 pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12));
Alexander Graf3eeafd72010-03-24 21:48:17 +0100568 break;
569 case MSR_DR:
Alexander Graf3eeafd72010-03-24 21:48:17 +0100570 case MSR_IR:
Alexander Graff7bc74e2010-04-20 02:49:48 +0200571 vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid);
572
573 if ((vcpu->arch.msr & (MSR_DR|MSR_IR)) == MSR_DR)
574 pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12));
575 else
576 pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12));
577 pte.vpage |= vsid;
578
579 if (vsid == -1)
580 page_found = -EINVAL;
Alexander Graf3eeafd72010-03-24 21:48:17 +0100581 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000582 }
583
584 if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
585 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
586 /*
587 * If we do the dcbz hack, we have to NX on every execution,
588 * so we can patch the executing code. This renders our guest
589 * NX-less.
590 */
591 pte.may_execute = !data;
592 }
593
594 if (page_found == -ENOENT) {
595 /* Page not found in guest PTE entries */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200596 vcpu->arch.dear = kvmppc_get_fault_dar(vcpu);
597 to_book3s(vcpu)->dsisr = to_svcpu(vcpu)->fault_dsisr;
598 vcpu->arch.msr |= (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000599 kvmppc_book3s_queue_irqprio(vcpu, vec);
600 } else if (page_found == -EPERM) {
601 /* Storage protection */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200602 vcpu->arch.dear = kvmppc_get_fault_dar(vcpu);
603 to_book3s(vcpu)->dsisr = to_svcpu(vcpu)->fault_dsisr & ~DSISR_NOHPTE;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000604 to_book3s(vcpu)->dsisr |= DSISR_PROTFAULT;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200605 vcpu->arch.msr |= (to_svcpu(vcpu)->shadow_srr1 & 0x00000000f8000000ULL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000606 kvmppc_book3s_queue_irqprio(vcpu, vec);
607 } else if (page_found == -EINVAL) {
608 /* Page not found in guest SLB */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200609 vcpu->arch.dear = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000610 kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80);
611 } else if (!is_mmio &&
612 kvmppc_visible_gfn(vcpu, pte.raddr >> PAGE_SHIFT)) {
613 /* The guest's PTE is not mapped yet. Map on the host */
614 kvmppc_mmu_map_page(vcpu, &pte);
615 if (data)
616 vcpu->stat.sp_storage++;
617 else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
618 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32)))
619 kvmppc_patch_dcbz(vcpu, &pte);
620 } else {
621 /* MMIO */
622 vcpu->stat.mmio_exits++;
623 vcpu->arch.paddr_accessed = pte.raddr;
624 r = kvmppc_emulate_mmio(run, vcpu);
625 if ( r == RESUME_HOST_NV )
626 r = RESUME_HOST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000627 }
628
629 return r;
630}
631
Alexander Graf180a34d2010-01-15 14:49:11 +0100632static inline int get_fpr_index(int i)
633{
634#ifdef CONFIG_VSX
635 i *= 2;
636#endif
637 return i;
638}
639
640/* Give up external provider (FPU, Altivec, VSX) */
Alexander Grafaba3bd72010-02-19 11:00:39 +0100641void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr)
Alexander Graf180a34d2010-01-15 14:49:11 +0100642{
643 struct thread_struct *t = &current->thread;
644 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100645#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100646 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100647#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100648 u64 *thread_fpr = (u64*)t->fpr;
649 int i;
650
651 if (!(vcpu->arch.guest_owned_ext & msr))
652 return;
653
654#ifdef DEBUG_EXT
655 printk(KERN_INFO "Giving up ext 0x%lx\n", msr);
656#endif
657
658 switch (msr) {
659 case MSR_FP:
660 giveup_fpu(current);
661 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
662 vcpu_fpr[i] = thread_fpr[get_fpr_index(i)];
663
664 vcpu->arch.fpscr = t->fpscr.val;
665 break;
666 case MSR_VEC:
667#ifdef CONFIG_ALTIVEC
668 giveup_altivec(current);
669 memcpy(vcpu->arch.vr, t->vr, sizeof(vcpu->arch.vr));
670 vcpu->arch.vscr = t->vscr;
671#endif
672 break;
673 case MSR_VSX:
674#ifdef CONFIG_VSX
675 __giveup_vsx(current);
676 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
677 vcpu_vsx[i] = thread_fpr[get_fpr_index(i) + 1];
678#endif
679 break;
680 default:
681 BUG();
682 }
683
684 vcpu->arch.guest_owned_ext &= ~msr;
685 current->thread.regs->msr &= ~msr;
Alexander Grafa76f8492010-01-15 14:49:14 +0100686 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100687}
688
Alexander Graf89632212010-03-24 21:48:21 +0100689static int kvmppc_read_inst(struct kvm_vcpu *vcpu)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100690{
Alexander Grafc7f38f42010-04-16 00:11:40 +0200691 ulong srr0 = kvmppc_get_pc(vcpu);
692 u32 last_inst = kvmppc_get_last_inst(vcpu);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100693 int ret;
694
Alexander Grafc7f38f42010-04-16 00:11:40 +0200695 ret = kvmppc_ld(vcpu, &srr0, sizeof(u32), &last_inst, false);
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100696 if (ret == -ENOENT) {
697 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 33, 33, 1);
698 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 34, 36, 0);
699 vcpu->arch.msr = kvmppc_set_field(vcpu->arch.msr, 42, 47, 0);
700 kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_INST_STORAGE);
Alexander Graf89632212010-03-24 21:48:21 +0100701 return EMULATE_AGAIN;
702 }
703
704 return EMULATE_DONE;
705}
706
707static int kvmppc_check_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr)
708{
709
710 /* Need to do paired single emulation? */
711 if (!(vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE))
712 return EMULATE_DONE;
713
714 /* Read out the instruction */
715 if (kvmppc_read_inst(vcpu) == EMULATE_DONE)
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100716 /* Need to emulate */
717 return EMULATE_FAIL;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100718
719 return EMULATE_AGAIN;
720}
721
Alexander Graf180a34d2010-01-15 14:49:11 +0100722/* Handle external providers (FPU, Altivec, VSX) */
723static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr,
724 ulong msr)
725{
726 struct thread_struct *t = &current->thread;
727 u64 *vcpu_fpr = vcpu->arch.fpr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100728#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +0100729 u64 *vcpu_vsx = vcpu->arch.vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +0100730#endif
Alexander Graf180a34d2010-01-15 14:49:11 +0100731 u64 *thread_fpr = (u64*)t->fpr;
732 int i;
733
Alexander Graf3c402a72010-02-19 11:00:32 +0100734 /* When we have paired singles, we emulate in software */
735 if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE)
736 return RESUME_GUEST;
737
Alexander Graf180a34d2010-01-15 14:49:11 +0100738 if (!(vcpu->arch.msr & msr)) {
739 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
740 return RESUME_GUEST;
741 }
742
Alexander Grafc2453692010-03-24 21:48:22 +0100743 /* We already own the ext */
744 if (vcpu->arch.guest_owned_ext & msr) {
745 return RESUME_GUEST;
746 }
747
Alexander Graf180a34d2010-01-15 14:49:11 +0100748#ifdef DEBUG_EXT
749 printk(KERN_INFO "Loading up ext 0x%lx\n", msr);
750#endif
751
752 current->thread.regs->msr |= msr;
753
754 switch (msr) {
755 case MSR_FP:
756 for (i = 0; i < ARRAY_SIZE(vcpu->arch.fpr); i++)
757 thread_fpr[get_fpr_index(i)] = vcpu_fpr[i];
758
759 t->fpscr.val = vcpu->arch.fpscr;
760 t->fpexc_mode = 0;
761 kvmppc_load_up_fpu();
762 break;
763 case MSR_VEC:
764#ifdef CONFIG_ALTIVEC
765 memcpy(t->vr, vcpu->arch.vr, sizeof(vcpu->arch.vr));
766 t->vscr = vcpu->arch.vscr;
767 t->vrsave = -1;
768 kvmppc_load_up_altivec();
769#endif
770 break;
771 case MSR_VSX:
772#ifdef CONFIG_VSX
773 for (i = 0; i < ARRAY_SIZE(vcpu->arch.vsr); i++)
774 thread_fpr[get_fpr_index(i) + 1] = vcpu_vsx[i];
775 kvmppc_load_up_vsx();
776#endif
777 break;
778 default:
779 BUG();
780 }
781
782 vcpu->arch.guest_owned_ext |= msr;
783
Alexander Grafa76f8492010-01-15 14:49:14 +0100784 kvmppc_recalc_shadow_msr(vcpu);
Alexander Graf180a34d2010-01-15 14:49:11 +0100785
786 return RESUME_GUEST;
787}
788
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000789int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
790 unsigned int exit_nr)
791{
792 int r = RESUME_HOST;
793
794 vcpu->stat.sum_exits++;
795
796 run->exit_reason = KVM_EXIT_UNKNOWN;
797 run->ready_for_interrupt_injection = 1;
798#ifdef EXIT_DEBUG
799 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 +0200800 exit_nr, kvmppc_get_pc(vcpu), kvmppc_get_fault_dar(vcpu),
801 kvmppc_get_dec(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000802#elif defined (EXIT_DEBUG_SIMPLE)
803 if ((exit_nr != 0x900) && (exit_nr != 0x500))
804 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | dar=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200805 exit_nr, kvmppc_get_pc(vcpu), kvmppc_get_fault_dar(vcpu),
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000806 vcpu->arch.msr);
807#endif
808 kvm_resched(vcpu);
809 switch (exit_nr) {
810 case BOOK3S_INTERRUPT_INST_STORAGE:
811 vcpu->stat.pf_instruc++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200812
813#ifdef CONFIG_PPC_BOOK3S_32
814 /* We set segments as unused segments when invalidating them. So
815 * treat the respective fault as segment fault. */
816 if (to_svcpu(vcpu)->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT]
817 == SR_INVALID) {
818 kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu));
819 r = RESUME_GUEST;
820 break;
821 }
822#endif
823
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000824 /* only care about PTEG not found errors, but leave NX alone */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200825 if (to_svcpu(vcpu)->shadow_srr1 & 0x40000000) {
826 r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000827 vcpu->stat.sp_instruc++;
828 } else if (vcpu->arch.mmu.is_dcbz32(vcpu) &&
829 (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) {
830 /*
831 * XXX If we do the dcbz hack we use the NX bit to flush&patch the page,
832 * so we can't use the NX bit inside the guest. Let's cross our fingers,
833 * that no guest that needs the dcbz hack does NX.
834 */
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200835 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
Alexander Graf9fb244a2010-03-24 21:48:32 +0100836 r = RESUME_GUEST;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000837 } else {
Alexander Grafc7f38f42010-04-16 00:11:40 +0200838 vcpu->arch.msr |= to_svcpu(vcpu)->shadow_srr1 & 0x58000000;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000839 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200840 kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000841 r = RESUME_GUEST;
842 }
843 break;
844 case BOOK3S_INTERRUPT_DATA_STORAGE:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200845 {
846 ulong dar = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000847 vcpu->stat.pf_storage++;
Alexander Graf61db97c2010-04-16 00:11:52 +0200848
849#ifdef CONFIG_PPC_BOOK3S_32
850 /* We set segments as unused segments when invalidating them. So
851 * treat the respective fault as segment fault. */
852 if ((to_svcpu(vcpu)->sr[dar >> SID_SHIFT]) == SR_INVALID) {
853 kvmppc_mmu_map_segment(vcpu, dar);
854 r = RESUME_GUEST;
855 break;
856 }
857#endif
858
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000859 /* The only case we need to handle is missing shadow PTEs */
Alexander Grafc7f38f42010-04-16 00:11:40 +0200860 if (to_svcpu(vcpu)->fault_dsisr & DSISR_NOHPTE) {
861 r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000862 } else {
Alexander Grafc7f38f42010-04-16 00:11:40 +0200863 vcpu->arch.dear = dar;
864 to_book3s(vcpu)->dsisr = to_svcpu(vcpu)->fault_dsisr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000865 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
Alexander Grafaf7b4d12010-04-20 02:49:46 +0200866 kvmppc_mmu_pte_flush(vcpu, vcpu->arch.dear, ~0xFFFUL);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000867 r = RESUME_GUEST;
868 }
869 break;
Alexander Grafc7f38f42010-04-16 00:11:40 +0200870 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000871 case BOOK3S_INTERRUPT_DATA_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200872 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) {
873 vcpu->arch.dear = kvmppc_get_fault_dar(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000874 kvmppc_book3s_queue_irqprio(vcpu,
875 BOOK3S_INTERRUPT_DATA_SEGMENT);
876 }
877 r = RESUME_GUEST;
878 break;
879 case BOOK3S_INTERRUPT_INST_SEGMENT:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200880 if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) {
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000881 kvmppc_book3s_queue_irqprio(vcpu,
882 BOOK3S_INTERRUPT_INST_SEGMENT);
883 }
884 r = RESUME_GUEST;
885 break;
886 /* We're good on these - the host merely wanted to get our attention */
887 case BOOK3S_INTERRUPT_DECREMENTER:
888 vcpu->stat.dec_exits++;
889 r = RESUME_GUEST;
890 break;
891 case BOOK3S_INTERRUPT_EXTERNAL:
892 vcpu->stat.ext_intr_exits++;
893 r = RESUME_GUEST;
894 break;
Alexander Graf7fdaec92010-04-20 02:49:47 +0200895 case BOOK3S_INTERRUPT_PERFMON:
896 r = RESUME_GUEST;
897 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000898 case BOOK3S_INTERRUPT_PROGRAM:
899 {
900 enum emulation_result er;
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100901 ulong flags;
902
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100903program_interrupt:
Alexander Grafc7f38f42010-04-16 00:11:40 +0200904 flags = to_svcpu(vcpu)->shadow_srr1 & 0x1f0000ull;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000905
906 if (vcpu->arch.msr & MSR_PR) {
907#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +0200908 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 +0000909#endif
Alexander Grafc7f38f42010-04-16 00:11:40 +0200910 if ((kvmppc_get_last_inst(vcpu) & 0xff0007ff) !=
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000911 (INS_DCBZ & 0xfffffff7)) {
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100912 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000913 r = RESUME_GUEST;
914 break;
915 }
916 }
917
918 vcpu->stat.emulated_inst_exits++;
919 er = kvmppc_emulate_instruction(run, vcpu);
920 switch (er) {
921 case EMULATE_DONE:
Alexander Graf97c4cfb2010-01-04 22:19:25 +0100922 r = RESUME_GUEST_NV;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000923 break;
Alexander Graf37f5bca2010-02-19 11:00:31 +0100924 case EMULATE_AGAIN:
925 r = RESUME_GUEST;
926 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000927 case EMULATE_FAIL:
928 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +0200929 __func__, kvmppc_get_pc(vcpu), kvmppc_get_last_inst(vcpu));
Alexander Grafff1ca3f2010-01-08 02:58:09 +0100930 kvmppc_core_queue_program(vcpu, flags);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000931 r = RESUME_GUEST;
932 break;
Alexander Grafe5c29e92010-02-19 11:00:43 +0100933 case EMULATE_DO_MMIO:
934 run->exit_reason = KVM_EXIT_MMIO;
935 r = RESUME_HOST_NV;
936 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000937 default:
938 BUG();
939 }
940 break;
941 }
942 case BOOK3S_INTERRUPT_SYSCALL:
Alexander Grafad0a0482010-03-24 21:48:30 +0100943 // XXX make user settable
944 if (vcpu->arch.osi_enabled &&
945 (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) &&
946 (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) {
947 u64 *gprs = run->osi.gprs;
948 int i;
949
950 run->exit_reason = KVM_EXIT_OSI;
951 for (i = 0; i < 32; i++)
952 gprs[i] = kvmppc_get_gpr(vcpu, i);
953 vcpu->arch.osi_needed = 1;
954 r = RESUME_HOST_NV;
955
956 } else {
957 vcpu->stat.syscall_exits++;
958 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
959 r = RESUME_GUEST;
960 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000961 break;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000962 case BOOK3S_INTERRUPT_FP_UNAVAIL:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +0000963 case BOOK3S_INTERRUPT_ALTIVEC:
964 case BOOK3S_INTERRUPT_VSX:
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100965 {
966 int ext_msr = 0;
967
968 switch (exit_nr) {
969 case BOOK3S_INTERRUPT_FP_UNAVAIL: ext_msr = MSR_FP; break;
970 case BOOK3S_INTERRUPT_ALTIVEC: ext_msr = MSR_VEC; break;
971 case BOOK3S_INTERRUPT_VSX: ext_msr = MSR_VSX; break;
972 }
973
974 switch (kvmppc_check_ext(vcpu, exit_nr)) {
975 case EMULATE_DONE:
976 /* everything ok - let's enable the ext */
977 r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr);
978 break;
979 case EMULATE_FAIL:
980 /* we need to emulate this instruction */
981 goto program_interrupt;
982 break;
983 default:
984 /* nothing to worry about - go again */
985 break;
986 }
Alexander Graf180a34d2010-01-15 14:49:11 +0100987 break;
Alexander Grafc8c0b6f2010-02-19 11:00:34 +0100988 }
Alexander Grafca7f4202010-03-24 21:48:28 +0100989 case BOOK3S_INTERRUPT_ALIGNMENT:
990 if (kvmppc_read_inst(vcpu) == EMULATE_DONE) {
991 to_book3s(vcpu)->dsisr = kvmppc_alignment_dsisr(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +0200992 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +0100993 vcpu->arch.dear = kvmppc_alignment_dar(vcpu,
Alexander Grafc7f38f42010-04-16 00:11:40 +0200994 kvmppc_get_last_inst(vcpu));
Alexander Grafca7f4202010-03-24 21:48:28 +0100995 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
996 }
997 r = RESUME_GUEST;
998 break;
Alexander Graf180a34d2010-01-15 14:49:11 +0100999 case BOOK3S_INTERRUPT_MACHINE_CHECK:
1000 case BOOK3S_INTERRUPT_TRACE:
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001001 kvmppc_book3s_queue_irqprio(vcpu, exit_nr);
1002 r = RESUME_GUEST;
1003 break;
1004 default:
1005 /* Ugh - bork here! What did we get? */
Alexander Graff7adbba2010-01-15 14:49:13 +01001006 printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n",
Alexander Grafc7f38f42010-04-16 00:11:40 +02001007 exit_nr, kvmppc_get_pc(vcpu), to_svcpu(vcpu)->shadow_srr1);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001008 r = RESUME_HOST;
1009 BUG();
1010 break;
1011 }
1012
1013
1014 if (!(r & RESUME_HOST)) {
1015 /* To avoid clobbering exit_reason, only check for signals if
1016 * we aren't already exiting to userspace for some other
1017 * reason. */
1018 if (signal_pending(current)) {
1019#ifdef EXIT_DEBUG
1020 printk(KERN_EMERG "KVM: Going back to host\n");
1021#endif
1022 vcpu->stat.signal_exits++;
1023 run->exit_reason = KVM_EXIT_INTR;
1024 r = -EINTR;
1025 } else {
1026 /* In case an interrupt came in that was triggered
1027 * from userspace (like DEC), we need to check what
1028 * to inject now! */
1029 kvmppc_core_deliver_interrupts(vcpu);
1030 }
1031 }
1032
1033#ifdef EXIT_DEBUG
Alexander Grafc7f38f42010-04-16 00:11:40 +02001034 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 +00001035#endif
1036
1037 return r;
1038}
1039
1040int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1041{
1042 return 0;
1043}
1044
1045int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1046{
1047 int i;
1048
Alexander Grafa56cf342010-03-24 21:48:23 +01001049 vcpu_load(vcpu);
1050
Alexander Grafc7f38f42010-04-16 00:11:40 +02001051 regs->pc = kvmppc_get_pc(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001052 regs->cr = kvmppc_get_cr(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001053 regs->ctr = kvmppc_get_ctr(vcpu);
1054 regs->lr = kvmppc_get_lr(vcpu);
Alexander Graf992b5b22010-01-08 02:58:02 +01001055 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001056 regs->msr = vcpu->arch.msr;
1057 regs->srr0 = vcpu->arch.srr0;
1058 regs->srr1 = vcpu->arch.srr1;
1059 regs->pid = vcpu->arch.pid;
1060 regs->sprg0 = vcpu->arch.sprg0;
1061 regs->sprg1 = vcpu->arch.sprg1;
1062 regs->sprg2 = vcpu->arch.sprg2;
1063 regs->sprg3 = vcpu->arch.sprg3;
1064 regs->sprg5 = vcpu->arch.sprg4;
1065 regs->sprg6 = vcpu->arch.sprg5;
1066 regs->sprg7 = vcpu->arch.sprg6;
1067
1068 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001069 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001070
Alexander Grafa56cf342010-03-24 21:48:23 +01001071 vcpu_put(vcpu);
1072
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001073 return 0;
1074}
1075
1076int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1077{
1078 int i;
1079
Alexander Grafa56cf342010-03-24 21:48:23 +01001080 vcpu_load(vcpu);
1081
Alexander Grafc7f38f42010-04-16 00:11:40 +02001082 kvmppc_set_pc(vcpu, regs->pc);
Alexander Graf992b5b22010-01-08 02:58:02 +01001083 kvmppc_set_cr(vcpu, regs->cr);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001084 kvmppc_set_ctr(vcpu, regs->ctr);
1085 kvmppc_set_lr(vcpu, regs->lr);
Alexander Graf992b5b22010-01-08 02:58:02 +01001086 kvmppc_set_xer(vcpu, regs->xer);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001087 kvmppc_set_msr(vcpu, regs->msr);
1088 vcpu->arch.srr0 = regs->srr0;
1089 vcpu->arch.srr1 = regs->srr1;
1090 vcpu->arch.sprg0 = regs->sprg0;
1091 vcpu->arch.sprg1 = regs->sprg1;
1092 vcpu->arch.sprg2 = regs->sprg2;
1093 vcpu->arch.sprg3 = regs->sprg3;
1094 vcpu->arch.sprg5 = regs->sprg4;
1095 vcpu->arch.sprg6 = regs->sprg5;
1096 vcpu->arch.sprg7 = regs->sprg6;
1097
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001098 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1099 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001100
Alexander Grafa56cf342010-03-24 21:48:23 +01001101 vcpu_put(vcpu);
1102
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001103 return 0;
1104}
1105
1106int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1107 struct kvm_sregs *sregs)
1108{
Alexander Grafe15a1132009-11-30 03:02:02 +00001109 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1110 int i;
1111
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001112 sregs->pvr = vcpu->arch.pvr;
Alexander Grafe15a1132009-11-30 03:02:02 +00001113
1114 sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1;
1115 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1116 for (i = 0; i < 64; i++) {
1117 sregs->u.s.ppc64.slb[i].slbe = vcpu3s->slb[i].orige | i;
1118 sregs->u.s.ppc64.slb[i].slbv = vcpu3s->slb[i].origv;
1119 }
1120 } else {
1121 for (i = 0; i < 16; i++) {
1122 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1123 sregs->u.s.ppc32.sr[i] = vcpu3s->sr[i].raw;
1124 }
1125 for (i = 0; i < 8; i++) {
1126 sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw;
1127 sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw;
1128 }
1129 }
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001130 return 0;
1131}
1132
1133int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1134 struct kvm_sregs *sregs)
1135{
Alexander Grafe15a1132009-11-30 03:02:02 +00001136 struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu);
1137 int i;
1138
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001139 kvmppc_set_pvr(vcpu, sregs->pvr);
Alexander Grafe15a1132009-11-30 03:02:02 +00001140
1141 vcpu3s->sdr1 = sregs->u.s.sdr1;
1142 if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) {
1143 for (i = 0; i < 64; i++) {
1144 vcpu->arch.mmu.slbmte(vcpu, sregs->u.s.ppc64.slb[i].slbv,
1145 sregs->u.s.ppc64.slb[i].slbe);
1146 }
1147 } else {
1148 for (i = 0; i < 16; i++) {
1149 vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]);
1150 }
1151 for (i = 0; i < 8; i++) {
1152 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false,
1153 (u32)sregs->u.s.ppc32.ibat[i]);
1154 kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true,
1155 (u32)(sregs->u.s.ppc32.ibat[i] >> 32));
1156 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false,
1157 (u32)sregs->u.s.ppc32.dbat[i]);
1158 kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true,
1159 (u32)(sregs->u.s.ppc32.dbat[i] >> 32));
1160 }
1161 }
1162
1163 /* Flush the MMU after messing with the segments */
1164 kvmppc_mmu_pte_flush(vcpu, 0, 0);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001165 return 0;
1166}
1167
1168int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1169{
1170 return -ENOTSUPP;
1171}
1172
1173int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1174{
1175 return -ENOTSUPP;
1176}
1177
1178int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1179 struct kvm_translation *tr)
1180{
1181 return 0;
1182}
1183
1184/*
1185 * Get (and clear) the dirty memory log for a memory slot.
1186 */
1187int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1188 struct kvm_dirty_log *log)
1189{
1190 struct kvm_memory_slot *memslot;
1191 struct kvm_vcpu *vcpu;
1192 ulong ga, ga_end;
1193 int is_dirty = 0;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001194 int r;
1195 unsigned long n;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001196
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001197 mutex_lock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001198
1199 r = kvm_get_dirty_log(kvm, log, &is_dirty);
1200 if (r)
1201 goto out;
1202
1203 /* If nothing is dirty, don't bother messing with page tables. */
1204 if (is_dirty) {
Marcelo Tosatti46a26bf2009-12-23 14:35:16 -02001205 memslot = &kvm->memslots->memslots[log->slot];
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001206
1207 ga = memslot->base_gfn << PAGE_SHIFT;
1208 ga_end = ga + (memslot->npages << PAGE_SHIFT);
1209
1210 kvm_for_each_vcpu(n, vcpu, kvm)
1211 kvmppc_mmu_pte_pflush(vcpu, ga, ga_end);
1212
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001213 n = kvm_dirty_bitmap_bytes(memslot);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001214 memset(memslot->dirty_bitmap, 0, n);
1215 }
1216
1217 r = 0;
1218out:
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001219 mutex_unlock(&kvm->slots_lock);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001220 return r;
1221}
1222
1223int kvmppc_core_check_processor_compat(void)
1224{
1225 return 0;
1226}
1227
1228struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1229{
1230 struct kvmppc_vcpu_book3s *vcpu_book3s;
1231 struct kvm_vcpu *vcpu;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001232 int err = -ENOMEM;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001233
Alexander Graf032c3402010-02-19 12:24:33 +01001234 vcpu_book3s = vmalloc(sizeof(struct kvmppc_vcpu_book3s));
Alexander Grafc7f38f42010-04-16 00:11:40 +02001235 if (!vcpu_book3s)
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001236 goto out;
Alexander Grafc7f38f42010-04-16 00:11:40 +02001237
Alexander Graf7e821d32010-02-22 16:52:08 +01001238 memset(vcpu_book3s, 0, sizeof(struct kvmppc_vcpu_book3s));
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001239
Alexander Grafc7f38f42010-04-16 00:11:40 +02001240 vcpu_book3s->shadow_vcpu = (struct kvmppc_book3s_shadow_vcpu *)
1241 kzalloc(sizeof(*vcpu_book3s->shadow_vcpu), GFP_KERNEL);
1242 if (!vcpu_book3s->shadow_vcpu)
1243 goto free_vcpu;
1244
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001245 vcpu = &vcpu_book3s->vcpu;
1246 err = kvm_vcpu_init(vcpu, kvm, id);
1247 if (err)
Alexander Grafc7f38f42010-04-16 00:11:40 +02001248 goto free_shadow_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001249
1250 vcpu->arch.host_retip = kvm_return_point;
1251 vcpu->arch.host_msr = mfmsr();
Alexander Graf07b09072010-04-16 00:11:53 +02001252#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001253 /* default to book3s_64 (970fx) */
1254 vcpu->arch.pvr = 0x3C0301;
Alexander Graf07b09072010-04-16 00:11:53 +02001255#else
1256 /* default to book3s_32 (750) */
1257 vcpu->arch.pvr = 0x84202;
1258#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001259 kvmppc_set_pvr(vcpu, vcpu->arch.pvr);
1260 vcpu_book3s->slb_nr = 64;
1261
1262 /* remember where some real-mode handlers are */
1263 vcpu->arch.trampoline_lowmem = kvmppc_trampoline_lowmem;
1264 vcpu->arch.trampoline_enter = kvmppc_trampoline_enter;
1265 vcpu->arch.highmem_handler = (ulong)kvmppc_handler_highmem;
Alexander Graf07b09072010-04-16 00:11:53 +02001266#ifdef CONFIG_PPC_BOOK3S_64
Alexander Graf021ec9c2010-01-08 02:58:06 +01001267 vcpu->arch.rmcall = *(ulong*)kvmppc_rmcall;
Alexander Graf07b09072010-04-16 00:11:53 +02001268#else
1269 vcpu->arch.rmcall = (ulong)kvmppc_rmcall;
1270#endif
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001271
1272 vcpu->arch.shadow_msr = MSR_USER64;
1273
Alexander Graf9cc5e952010-04-16 00:11:45 +02001274 err = kvmppc_mmu_init(vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001275 if (err < 0)
Alexander Grafc7f38f42010-04-16 00:11:40 +02001276 goto free_shadow_vcpu;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001277
1278 return vcpu;
1279
Alexander Grafc7f38f42010-04-16 00:11:40 +02001280free_shadow_vcpu:
1281 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001282free_vcpu:
Alexander Graf032c3402010-02-19 12:24:33 +01001283 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001284out:
1285 return ERR_PTR(err);
1286}
1287
1288void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1289{
1290 struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu);
1291
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001292 kvm_vcpu_uninit(vcpu);
Alexander Grafc7f38f42010-04-16 00:11:40 +02001293 kfree(vcpu_book3s->shadow_vcpu);
Alexander Graf032c3402010-02-19 12:24:33 +01001294 vfree(vcpu_book3s);
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001295}
1296
1297extern int __kvmppc_vcpu_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1298int __kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1299{
1300 int ret;
Alexander Graf180a34d2010-01-15 14:49:11 +01001301 struct thread_struct ext_bkp;
Alexander Grafa2b07662010-03-24 21:48:31 +01001302#ifdef CONFIG_ALTIVEC
Alexander Graf180a34d2010-01-15 14:49:11 +01001303 bool save_vec = current->thread.used_vr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001304#endif
1305#ifdef CONFIG_VSX
Alexander Graf180a34d2010-01-15 14:49:11 +01001306 bool save_vsx = current->thread.used_vsr;
Alexander Grafa2b07662010-03-24 21:48:31 +01001307#endif
Alexander Graf180a34d2010-01-15 14:49:11 +01001308 ulong ext_msr;
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001309
1310 /* No need to go into the guest when all we do is going out */
1311 if (signal_pending(current)) {
1312 kvm_run->exit_reason = KVM_EXIT_INTR;
1313 return -EINTR;
1314 }
1315
Alexander Graf180a34d2010-01-15 14:49:11 +01001316 /* Save FPU state in stack */
1317 if (current->thread.regs->msr & MSR_FP)
1318 giveup_fpu(current);
1319 memcpy(ext_bkp.fpr, current->thread.fpr, sizeof(current->thread.fpr));
1320 ext_bkp.fpscr = current->thread.fpscr;
1321 ext_bkp.fpexc_mode = current->thread.fpexc_mode;
1322
1323#ifdef CONFIG_ALTIVEC
1324 /* Save Altivec state in stack */
1325 if (save_vec) {
1326 if (current->thread.regs->msr & MSR_VEC)
1327 giveup_altivec(current);
1328 memcpy(ext_bkp.vr, current->thread.vr, sizeof(ext_bkp.vr));
1329 ext_bkp.vscr = current->thread.vscr;
1330 ext_bkp.vrsave = current->thread.vrsave;
1331 }
1332 ext_bkp.used_vr = current->thread.used_vr;
1333#endif
1334
1335#ifdef CONFIG_VSX
1336 /* Save VSX state in stack */
1337 if (save_vsx && (current->thread.regs->msr & MSR_VSX))
1338 __giveup_vsx(current);
1339 ext_bkp.used_vsr = current->thread.used_vsr;
1340#endif
1341
1342 /* Remember the MSR with disabled extensions */
1343 ext_msr = current->thread.regs->msr;
1344
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001345 /* XXX we get called with irq disabled - change that! */
1346 local_irq_enable();
1347
Alexander Grafd1bab742010-02-19 11:00:35 +01001348 /* Preload FPU if it's enabled */
1349 if (vcpu->arch.msr & MSR_FP)
1350 kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP);
1351
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001352 ret = __kvmppc_vcpu_entry(kvm_run, vcpu);
1353
1354 local_irq_disable();
1355
Alexander Graf180a34d2010-01-15 14:49:11 +01001356 current->thread.regs->msr = ext_msr;
1357
1358 /* Make sure we save the guest FPU/Altivec/VSX state */
1359 kvmppc_giveup_ext(vcpu, MSR_FP);
1360 kvmppc_giveup_ext(vcpu, MSR_VEC);
1361 kvmppc_giveup_ext(vcpu, MSR_VSX);
1362
1363 /* Restore FPU state from stack */
1364 memcpy(current->thread.fpr, ext_bkp.fpr, sizeof(ext_bkp.fpr));
1365 current->thread.fpscr = ext_bkp.fpscr;
1366 current->thread.fpexc_mode = ext_bkp.fpexc_mode;
1367
1368#ifdef CONFIG_ALTIVEC
1369 /* Restore Altivec state from stack */
1370 if (save_vec && current->thread.used_vr) {
1371 memcpy(current->thread.vr, ext_bkp.vr, sizeof(ext_bkp.vr));
1372 current->thread.vscr = ext_bkp.vscr;
1373 current->thread.vrsave= ext_bkp.vrsave;
1374 }
1375 current->thread.used_vr = ext_bkp.used_vr;
1376#endif
1377
1378#ifdef CONFIG_VSX
1379 current->thread.used_vsr = ext_bkp.used_vsr;
1380#endif
1381
Alexander Graf2f4cf5e2009-10-30 05:47:10 +00001382 return ret;
1383}
1384
1385static int kvmppc_book3s_init(void)
1386{
1387 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_book3s), THIS_MODULE);
1388}
1389
1390static void kvmppc_book3s_exit(void)
1391{
1392 kvm_exit();
1393}
1394
1395module_init(kvmppc_book3s_init);
1396module_exit(kvmppc_book3s_exit);