blob: 85bd5b8f3fd5d237729ee4c7400aa5e240794cc7 [file] [log] [blame]
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 *
15 * Copyright IBM Corp. 2007
Scott Wood4cd35f62011-06-14 18:34:31 -050016 * Copyright 2010-2011 Freescale Semiconductor, Inc.
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050017 *
18 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
19 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
Scott Woodd30f6e42011-12-20 15:34:43 +000020 * Scott Wood <scottwood@freescale.com>
21 * Varun Sethi <varun.sethi@freescale.com>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050022 */
23
24#include <linux/errno.h>
25#include <linux/err.h>
26#include <linux/kvm_host.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/gfp.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050028#include <linux/module.h>
29#include <linux/vmalloc.h>
30#include <linux/fs.h>
Hollis Blanchard7924bd42008-12-02 15:51:55 -060031
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050032#include <asm/cputable.h>
33#include <asm/uaccess.h>
34#include <asm/kvm_ppc.h>
Hollis Blanchardd9fbd032008-11-05 09:36:13 -060035#include <asm/cacheflush.h>
Scott Woodd30f6e42011-12-20 15:34:43 +000036#include <asm/dbell.h>
37#include <asm/hw_irq.h>
38#include <asm/irq.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050039
Scott Woodd30f6e42011-12-20 15:34:43 +000040#include "timing.h"
Hollis Blanchard75f74f02008-11-05 09:36:16 -060041#include "booke.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050042
Hollis Blanchardd9fbd032008-11-05 09:36:13 -060043unsigned long kvmppc_booke_handlers;
44
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050045#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
46#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
47
48struct kvm_stats_debugfs_item debugfs_entries[] = {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050049 { "mmio", VCPU_STAT(mmio_exits) },
50 { "dcr", VCPU_STAT(dcr_exits) },
51 { "sig", VCPU_STAT(signal_exits) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050052 { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
53 { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
54 { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
55 { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
56 { "sysc", VCPU_STAT(syscall_exits) },
57 { "isi", VCPU_STAT(isi_exits) },
58 { "dsi", VCPU_STAT(dsi_exits) },
59 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
60 { "dec", VCPU_STAT(dec_exits) },
61 { "ext_intr", VCPU_STAT(ext_intr_exits) },
Hollis Blanchard45c5eb62008-04-25 17:55:49 -050062 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Scott Woodd30f6e42011-12-20 15:34:43 +000063 { "doorbell", VCPU_STAT(dbell_exits) },
64 { "guest doorbell", VCPU_STAT(gdbell_exits) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050065 { NULL }
66};
67
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050068/* TODO: use vcpu_printf() */
69void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
70{
71 int i;
72
Alexander Graf666e7252010-07-29 14:47:43 +020073 printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060074 printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
Alexander Grafde7906c2010-07-29 14:47:46 +020075 printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
76 vcpu->arch.shared->srr1);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050077
78 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
79
80 for (i = 0; i < 32; i += 4) {
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060081 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
Alexander Graf8e5b26b2010-01-08 02:58:01 +010082 kvmppc_get_gpr(vcpu, i),
83 kvmppc_get_gpr(vcpu, i+1),
84 kvmppc_get_gpr(vcpu, i+2),
85 kvmppc_get_gpr(vcpu, i+3));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050086 }
87}
88
Scott Wood4cd35f62011-06-14 18:34:31 -050089#ifdef CONFIG_SPE
90void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
91{
92 preempt_disable();
93 enable_kernel_spe();
94 kvmppc_save_guest_spe(vcpu);
95 vcpu->arch.shadow_msr &= ~MSR_SPE;
96 preempt_enable();
97}
98
99static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
100{
101 preempt_disable();
102 enable_kernel_spe();
103 kvmppc_load_guest_spe(vcpu);
104 vcpu->arch.shadow_msr |= MSR_SPE;
105 preempt_enable();
106}
107
108static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
109{
110 if (vcpu->arch.shared->msr & MSR_SPE) {
111 if (!(vcpu->arch.shadow_msr & MSR_SPE))
112 kvmppc_vcpu_enable_spe(vcpu);
113 } else if (vcpu->arch.shadow_msr & MSR_SPE) {
114 kvmppc_vcpu_disable_spe(vcpu);
115 }
116}
117#else
118static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
119{
120}
121#endif
122
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500123/*
124 * Helper function for "full" MSR writes. No need to call this if only
125 * EE/CE/ME/DE/RI are changing.
126 */
Scott Wood4cd35f62011-06-14 18:34:31 -0500127void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
128{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500129 u32 old_msr = vcpu->arch.shared->msr;
Scott Wood4cd35f62011-06-14 18:34:31 -0500130
Scott Woodd30f6e42011-12-20 15:34:43 +0000131#ifdef CONFIG_KVM_BOOKE_HV
132 new_msr |= MSR_GS;
133#endif
134
Scott Wood4cd35f62011-06-14 18:34:31 -0500135 vcpu->arch.shared->msr = new_msr;
136
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500137 kvmppc_mmu_msr_notify(vcpu, old_msr);
Scott Wood4cd35f62011-06-14 18:34:31 -0500138 kvmppc_vcpu_sync_spe(vcpu);
139}
140
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600141static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
142 unsigned int priority)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600143{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600144 set_bit(priority, &vcpu->arch.pending_exceptions);
145}
146
Liu Yudaf5e272010-02-02 19:44:35 +0800147static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
148 ulong dear_flags, ulong esr_flags)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600149{
Liu Yudaf5e272010-02-02 19:44:35 +0800150 vcpu->arch.queued_dear = dear_flags;
151 vcpu->arch.queued_esr = esr_flags;
152 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
153}
154
155static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
156 ulong dear_flags, ulong esr_flags)
157{
158 vcpu->arch.queued_dear = dear_flags;
159 vcpu->arch.queued_esr = esr_flags;
160 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
161}
162
163static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
164 ulong esr_flags)
165{
166 vcpu->arch.queued_esr = esr_flags;
167 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
168}
169
170void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
171{
172 vcpu->arch.queued_esr = esr_flags;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600173 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600174}
175
176void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
177{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600178 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600179}
180
181int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
182{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600183 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600184}
185
Alexander Graf7706664d2009-12-21 20:21:24 +0100186void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
187{
188 clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
189}
190
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600191void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
192 struct kvm_interrupt *irq)
193{
Alexander Grafc5335f12010-08-30 14:03:24 +0200194 unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
195
196 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
197 prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
198
199 kvmppc_booke_queue_irqprio(vcpu, prio);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600200}
201
Alexander Graf4496f972010-04-07 10:03:25 +0200202void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
203 struct kvm_interrupt *irq)
204{
205 clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
Alexander Grafc5335f12010-08-30 14:03:24 +0200206 clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
Alexander Graf4496f972010-04-07 10:03:25 +0200207}
208
Scott Woodd30f6e42011-12-20 15:34:43 +0000209static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
210{
211#ifdef CONFIG_KVM_BOOKE_HV
212 mtspr(SPRN_GSRR0, srr0);
213 mtspr(SPRN_GSRR1, srr1);
214#else
215 vcpu->arch.shared->srr0 = srr0;
216 vcpu->arch.shared->srr1 = srr1;
217#endif
218}
219
220static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
221{
222 vcpu->arch.csrr0 = srr0;
223 vcpu->arch.csrr1 = srr1;
224}
225
226static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
227{
228 if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
229 vcpu->arch.dsrr0 = srr0;
230 vcpu->arch.dsrr1 = srr1;
231 } else {
232 set_guest_csrr(vcpu, srr0, srr1);
233 }
234}
235
236static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
237{
238 vcpu->arch.mcsrr0 = srr0;
239 vcpu->arch.mcsrr1 = srr1;
240}
241
242static unsigned long get_guest_dear(struct kvm_vcpu *vcpu)
243{
244#ifdef CONFIG_KVM_BOOKE_HV
245 return mfspr(SPRN_GDEAR);
246#else
247 return vcpu->arch.shared->dar;
248#endif
249}
250
251static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear)
252{
253#ifdef CONFIG_KVM_BOOKE_HV
254 mtspr(SPRN_GDEAR, dear);
255#else
256 vcpu->arch.shared->dar = dear;
257#endif
258}
259
260static unsigned long get_guest_esr(struct kvm_vcpu *vcpu)
261{
262#ifdef CONFIG_KVM_BOOKE_HV
263 return mfspr(SPRN_GESR);
264#else
265 return vcpu->arch.shared->esr;
266#endif
267}
268
269static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr)
270{
271#ifdef CONFIG_KVM_BOOKE_HV
272 mtspr(SPRN_GESR, esr);
273#else
274 vcpu->arch.shared->esr = esr;
275#endif
276}
277
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600278/* Deliver the interrupt of the corresponding priority, if possible. */
279static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
280 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500281{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600282 int allowed = 0;
Asias He6045be52010-06-19 16:52:12 +0800283 ulong uninitialized_var(msr_mask);
Liu Yudaf5e272010-02-02 19:44:35 +0800284 bool update_esr = false, update_dear = false;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200285 ulong crit_raw = vcpu->arch.shared->critical;
286 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
287 bool crit;
Alexander Grafc5335f12010-08-30 14:03:24 +0200288 bool keep_irq = false;
Scott Woodd30f6e42011-12-20 15:34:43 +0000289 enum int_class int_class;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200290
291 /* Truncate crit indicators in 32 bit mode */
292 if (!(vcpu->arch.shared->msr & MSR_SF)) {
293 crit_raw &= 0xffffffff;
294 crit_r1 &= 0xffffffff;
295 }
296
297 /* Critical section when crit == r1 */
298 crit = (crit_raw == crit_r1);
299 /* ... and we're in supervisor mode */
300 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500301
Alexander Grafc5335f12010-08-30 14:03:24 +0200302 if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
303 priority = BOOKE_IRQPRIO_EXTERNAL;
304 keep_irq = true;
305 }
306
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600307 switch (priority) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600308 case BOOKE_IRQPRIO_DTLB_MISS:
Liu Yudaf5e272010-02-02 19:44:35 +0800309 case BOOKE_IRQPRIO_DATA_STORAGE:
310 update_dear = true;
311 /* fall through */
312 case BOOKE_IRQPRIO_INST_STORAGE:
313 case BOOKE_IRQPRIO_PROGRAM:
314 update_esr = true;
315 /* fall through */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600316 case BOOKE_IRQPRIO_ITLB_MISS:
317 case BOOKE_IRQPRIO_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600318 case BOOKE_IRQPRIO_FP_UNAVAIL:
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600319 case BOOKE_IRQPRIO_SPE_UNAVAIL:
320 case BOOKE_IRQPRIO_SPE_FP_DATA:
321 case BOOKE_IRQPRIO_SPE_FP_ROUND:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600322 case BOOKE_IRQPRIO_AP_UNAVAIL:
323 case BOOKE_IRQPRIO_ALIGNMENT:
324 allowed = 1;
Scott Woodd30f6e42011-12-20 15:34:43 +0000325 msr_mask = MSR_GS | MSR_CE | MSR_ME | MSR_DE;
326 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500327 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600328 case BOOKE_IRQPRIO_CRITICAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000329 case BOOKE_IRQPRIO_DBELL_CRIT:
Alexander Graf666e7252010-07-29 14:47:43 +0200330 allowed = vcpu->arch.shared->msr & MSR_CE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000331 allowed = allowed && !crit;
332 msr_mask = MSR_GS | MSR_ME;
333 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500334 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600335 case BOOKE_IRQPRIO_MACHINE_CHECK:
Alexander Graf666e7252010-07-29 14:47:43 +0200336 allowed = vcpu->arch.shared->msr & MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000337 allowed = allowed && !crit;
338 msr_mask = MSR_GS;
339 int_class = INT_CLASS_MC;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500340 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600341 case BOOKE_IRQPRIO_DECREMENTER:
342 case BOOKE_IRQPRIO_FIT:
Scott Wooddfd4d472011-11-17 12:39:59 +0000343 keep_irq = true;
344 /* fall through */
345 case BOOKE_IRQPRIO_EXTERNAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000346 case BOOKE_IRQPRIO_DBELL:
Alexander Graf666e7252010-07-29 14:47:43 +0200347 allowed = vcpu->arch.shared->msr & MSR_EE;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200348 allowed = allowed && !crit;
Scott Woodd30f6e42011-12-20 15:34:43 +0000349 msr_mask = MSR_GS | MSR_CE | MSR_ME | MSR_DE;
350 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500351 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600352 case BOOKE_IRQPRIO_DEBUG:
Alexander Graf666e7252010-07-29 14:47:43 +0200353 allowed = vcpu->arch.shared->msr & MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000354 allowed = allowed && !crit;
355 msr_mask = MSR_GS | MSR_ME;
356 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500357 break;
358 }
359
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600360 if (allowed) {
Scott Woodd30f6e42011-12-20 15:34:43 +0000361 switch (int_class) {
362 case INT_CLASS_NONCRIT:
363 set_guest_srr(vcpu, vcpu->arch.pc,
364 vcpu->arch.shared->msr);
365 break;
366 case INT_CLASS_CRIT:
367 set_guest_csrr(vcpu, vcpu->arch.pc,
368 vcpu->arch.shared->msr);
369 break;
370 case INT_CLASS_DBG:
371 set_guest_dsrr(vcpu, vcpu->arch.pc,
372 vcpu->arch.shared->msr);
373 break;
374 case INT_CLASS_MC:
375 set_guest_mcsrr(vcpu, vcpu->arch.pc,
376 vcpu->arch.shared->msr);
377 break;
378 }
379
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600380 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
Liu Yudaf5e272010-02-02 19:44:35 +0800381 if (update_esr == true)
Scott Woodd30f6e42011-12-20 15:34:43 +0000382 set_guest_esr(vcpu, vcpu->arch.queued_esr);
Liu Yudaf5e272010-02-02 19:44:35 +0800383 if (update_dear == true)
Scott Woodd30f6e42011-12-20 15:34:43 +0000384 set_guest_dear(vcpu, vcpu->arch.queued_dear);
Alexander Graf666e7252010-07-29 14:47:43 +0200385 kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600386
Alexander Grafc5335f12010-08-30 14:03:24 +0200387 if (!keep_irq)
388 clear_bit(priority, &vcpu->arch.pending_exceptions);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600389 }
390
Scott Woodd30f6e42011-12-20 15:34:43 +0000391#ifdef CONFIG_KVM_BOOKE_HV
392 /*
393 * If an interrupt is pending but masked, raise a guest doorbell
394 * so that we are notified when the guest enables the relevant
395 * MSR bit.
396 */
397 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
398 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
399 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
400 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
401 if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
402 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
403#endif
404
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600405 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500406}
407
Scott Wooddfd4d472011-11-17 12:39:59 +0000408static void update_timer_ints(struct kvm_vcpu *vcpu)
409{
410 if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
411 kvmppc_core_queue_dec(vcpu);
412 else
413 kvmppc_core_dequeue_dec(vcpu);
414}
415
Scott Woodc59a6a32011-11-08 18:23:25 -0600416static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500417{
418 unsigned long *pending = &vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500419 unsigned int priority;
420
Scott Wooddfd4d472011-11-17 12:39:59 +0000421 if (vcpu->requests) {
422 if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu)) {
423 smp_mb();
424 update_timer_ints(vcpu);
425 }
426 }
427
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600428 priority = __ffs(*pending);
Hollis Blanchardbdc89f12009-01-03 16:23:12 -0600429 while (priority <= BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600430 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500431 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500432
433 priority = find_next_bit(pending,
434 BITS_PER_BYTE * sizeof(*pending),
435 priority + 1);
436 }
Alexander Graf90bba352010-07-29 14:47:51 +0200437
438 /* Tell the guest about our interrupt status */
Scott Wood29ac26e2011-11-08 18:23:27 -0600439 vcpu->arch.shared->int_pending = !!*pending;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500440}
441
Scott Woodc59a6a32011-11-08 18:23:25 -0600442/* Check pending exceptions and deliver one, if possible. */
443void kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
444{
445 WARN_ON_ONCE(!irqs_disabled());
446
447 kvmppc_core_check_exceptions(vcpu);
448
449 if (vcpu->arch.shared->msr & MSR_WE) {
450 local_irq_enable();
451 kvm_vcpu_block(vcpu);
452 local_irq_disable();
453
454 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
455 kvmppc_core_check_exceptions(vcpu);
456 };
457}
458
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000459int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
460{
461 int ret;
Scott Wood8fae8452011-12-20 15:34:45 +0000462#ifdef CONFIG_PPC_FPU
463 unsigned int fpscr;
464 int fpexc_mode;
465 u64 fpr[32];
466#endif
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000467
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200468 if (!vcpu->arch.sane) {
469 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
470 return -EINVAL;
471 }
472
Scott Woodd30f6e42011-12-20 15:34:43 +0000473 if (!current->thread.kvm_vcpu) {
474 WARN(1, "no vcpu\n");
475 return -EPERM;
476 }
477
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000478 local_irq_disable();
Scott Wood1d1ef222011-11-08 16:11:59 -0600479
Scott Wood25051b52011-11-08 18:23:23 -0600480 kvmppc_core_prepare_to_enter(vcpu);
481
Scott Wood1d1ef222011-11-08 16:11:59 -0600482 if (signal_pending(current)) {
483 kvm_run->exit_reason = KVM_EXIT_INTR;
484 ret = -EINTR;
485 goto out;
486 }
487
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000488 kvm_guest_enter();
Scott Wood8fae8452011-12-20 15:34:45 +0000489
490#ifdef CONFIG_PPC_FPU
491 /* Save userspace FPU state in stack */
492 enable_kernel_fp();
493 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
494 fpscr = current->thread.fpscr.val;
495 fpexc_mode = current->thread.fpexc_mode;
496
497 /* Restore guest FPU state to thread */
498 memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr));
499 current->thread.fpscr.val = vcpu->arch.fpscr;
500
501 /*
502 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
503 * as always using the FPU. Kernel usage of FP (via
504 * enable_kernel_fp()) in this thread must not occur while
505 * vcpu->fpu_active is set.
506 */
507 vcpu->fpu_active = 1;
508
509 kvmppc_load_guest_fp(vcpu);
510#endif
511
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000512 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
Scott Wood8fae8452011-12-20 15:34:45 +0000513
514#ifdef CONFIG_PPC_FPU
515 kvmppc_save_guest_fp(vcpu);
516
517 vcpu->fpu_active = 0;
518
519 /* Save guest FPU state from thread */
520 memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr));
521 vcpu->arch.fpscr = current->thread.fpscr.val;
522
523 /* Restore userspace FPU state from stack */
524 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
525 current->thread.fpscr.val = fpscr;
526 current->thread.fpexc_mode = fpexc_mode;
527#endif
528
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000529 kvm_guest_exit();
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000530
Scott Wood1d1ef222011-11-08 16:11:59 -0600531out:
532 local_irq_enable();
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000533 return ret;
534}
535
Scott Woodd30f6e42011-12-20 15:34:43 +0000536static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
537{
538 enum emulation_result er;
539
540 er = kvmppc_emulate_instruction(run, vcpu);
541 switch (er) {
542 case EMULATE_DONE:
543 /* don't overwrite subtypes, just account kvm_stats */
544 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
545 /* Future optimization: only reload non-volatiles if
546 * they were actually modified by emulation. */
547 return RESUME_GUEST_NV;
548
549 case EMULATE_DO_DCR:
550 run->exit_reason = KVM_EXIT_DCR;
551 return RESUME_HOST;
552
553 case EMULATE_FAIL:
554 /* XXX Deliver Program interrupt to guest. */
555 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
556 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
557 /* For debugging, encode the failing instruction and
558 * report it to userspace. */
559 run->hw.hardware_exit_reason = ~0ULL << 32;
560 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
561 return RESUME_HOST;
562
563 default:
564 BUG();
565 }
566}
567
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500568/**
569 * kvmppc_handle_exit
570 *
571 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
572 */
573int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
574 unsigned int exit_nr)
575{
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500576 int r = RESUME_HOST;
577
Hollis Blanchard73e75b412008-12-02 15:51:57 -0600578 /* update before a new last_exit_type is rewritten */
579 kvmppc_update_timing_stats(vcpu);
580
Scott Woodd30f6e42011-12-20 15:34:43 +0000581 switch (exit_nr) {
582 case BOOKE_INTERRUPT_EXTERNAL:
583 do_IRQ(current->thread.regs);
584 break;
585
586 case BOOKE_INTERRUPT_DECREMENTER:
587 timer_interrupt(current->thread.regs);
588 break;
589
590#if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64)
591 case BOOKE_INTERRUPT_DOORBELL:
592 doorbell_exception(current->thread.regs);
593 break;
594#endif
595 case BOOKE_INTERRUPT_MACHINE_CHECK:
596 /* FIXME */
597 break;
598 }
599
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500600 local_irq_enable();
601
602 run->exit_reason = KVM_EXIT_UNKNOWN;
603 run->ready_for_interrupt_injection = 1;
604
605 switch (exit_nr) {
606 case BOOKE_INTERRUPT_MACHINE_CHECK:
Scott Woodd30f6e42011-12-20 15:34:43 +0000607 kvm_resched(vcpu);
608 r = RESUME_GUEST;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500609 break;
610
611 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600612 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Scott Woodd30f6e42011-12-20 15:34:43 +0000613 kvm_resched(vcpu);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -0600614 r = RESUME_GUEST;
615 break;
616
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500617 case BOOKE_INTERRUPT_DECREMENTER:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600618 kvmppc_account_exit(vcpu, DEC_EXITS);
Scott Woodd30f6e42011-12-20 15:34:43 +0000619 kvm_resched(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500620 r = RESUME_GUEST;
621 break;
622
Scott Woodd30f6e42011-12-20 15:34:43 +0000623 case BOOKE_INTERRUPT_DOORBELL:
624 kvmppc_account_exit(vcpu, DBELL_EXITS);
625 kvm_resched(vcpu);
626 r = RESUME_GUEST;
627 break;
628
629 case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
630 kvmppc_account_exit(vcpu, GDBELL_EXITS);
631
632 /*
633 * We are here because there is a pending guest interrupt
634 * which could not be delivered as MSR_CE or MSR_ME was not
635 * set. Once we break from here we will retry delivery.
636 */
637 r = RESUME_GUEST;
638 break;
639
640 case BOOKE_INTERRUPT_GUEST_DBELL:
641 kvmppc_account_exit(vcpu, GDBELL_EXITS);
642
643 /*
644 * We are here because there is a pending guest interrupt
645 * which could not be delivered as MSR_EE was not set. Once
646 * we break from here we will retry delivery.
647 */
648 r = RESUME_GUEST;
649 break;
650
651 case BOOKE_INTERRUPT_HV_PRIV:
652 r = emulation_exit(run, vcpu);
653 break;
654
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500655 case BOOKE_INTERRUPT_PROGRAM:
Scott Woodd30f6e42011-12-20 15:34:43 +0000656 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500657 /* Program traps generated by user-level software must be handled
658 * by the guest kernel. */
Liu Yudaf5e272010-02-02 19:44:35 +0800659 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500660 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600661 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500662 break;
663 }
664
Scott Woodd30f6e42011-12-20 15:34:43 +0000665 r = emulation_exit(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500666 break;
667
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200668 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600669 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600670 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200671 r = RESUME_GUEST;
672 break;
673
Scott Wood4cd35f62011-06-14 18:34:31 -0500674#ifdef CONFIG_SPE
675 case BOOKE_INTERRUPT_SPE_UNAVAIL: {
676 if (vcpu->arch.shared->msr & MSR_SPE)
677 kvmppc_vcpu_enable_spe(vcpu);
678 else
679 kvmppc_booke_queue_irqprio(vcpu,
680 BOOKE_IRQPRIO_SPE_UNAVAIL);
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600681 r = RESUME_GUEST;
682 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500683 }
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600684
685 case BOOKE_INTERRUPT_SPE_FP_DATA:
686 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
687 r = RESUME_GUEST;
688 break;
689
690 case BOOKE_INTERRUPT_SPE_FP_ROUND:
691 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
692 r = RESUME_GUEST;
693 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500694#else
695 case BOOKE_INTERRUPT_SPE_UNAVAIL:
696 /*
697 * Guest wants SPE, but host kernel doesn't support it. Send
698 * an "unimplemented operation" program check to the guest.
699 */
700 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
701 r = RESUME_GUEST;
702 break;
703
704 /*
705 * These really should never happen without CONFIG_SPE,
706 * as we should never enable the real MSR[SPE] in the guest.
707 */
708 case BOOKE_INTERRUPT_SPE_FP_DATA:
709 case BOOKE_INTERRUPT_SPE_FP_ROUND:
710 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
711 __func__, exit_nr, vcpu->arch.pc);
712 run->hw.hardware_exit_reason = exit_nr;
713 r = RESUME_HOST;
714 break;
715#endif
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600716
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500717 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800718 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
719 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600720 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500721 r = RESUME_GUEST;
722 break;
723
724 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800725 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600726 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500727 r = RESUME_GUEST;
728 break;
729
Scott Woodd30f6e42011-12-20 15:34:43 +0000730#ifdef CONFIG_KVM_BOOKE_HV
731 case BOOKE_INTERRUPT_HV_SYSCALL:
732 if (!(vcpu->arch.shared->msr & MSR_PR)) {
733 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
734 } else {
735 /*
736 * hcall from guest userspace -- send privileged
737 * instruction program check.
738 */
739 kvmppc_core_queue_program(vcpu, ESR_PPR);
740 }
741
742 r = RESUME_GUEST;
743 break;
744#else
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500745 case BOOKE_INTERRUPT_SYSCALL:
Alexander Graf2a342ed2010-07-29 14:47:48 +0200746 if (!(vcpu->arch.shared->msr & MSR_PR) &&
747 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
748 /* KVM PV hypercalls */
749 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
750 r = RESUME_GUEST;
751 } else {
752 /* Guest syscalls */
753 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
754 }
Hollis Blanchard7b701592008-12-02 15:51:58 -0600755 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500756 r = RESUME_GUEST;
757 break;
Scott Woodd30f6e42011-12-20 15:34:43 +0000758#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500759
760 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500761 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600762 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600763 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500764 gfn_t gfn;
765
Scott Wooda4cd8b22011-06-14 18:34:41 -0500766#ifdef CONFIG_KVM_E500
767 if (!(vcpu->arch.shared->msr & MSR_PR) &&
768 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
769 kvmppc_map_magic(vcpu);
770 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
771 r = RESUME_GUEST;
772
773 break;
774 }
775#endif
776
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500777 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600778 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600779 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500780 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +0800781 kvmppc_core_queue_dtlb_miss(vcpu,
782 vcpu->arch.fault_dear,
783 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600784 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600785 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500786 r = RESUME_GUEST;
787 break;
788 }
789
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600790 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600791 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500792
793 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
794 /* The guest TLB had a mapping, but the shadow TLB
795 * didn't, and it is RAM. This could be because:
796 * a) the entry is mapping the host kernel, or
797 * b) the guest used a large mapping which we're faking
798 * Either way, we need to satisfy the fault without
799 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600800 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600801 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500802 r = RESUME_GUEST;
803 } else {
804 /* Guest has mapped and accessed a page which is not
805 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600806 vcpu->arch.paddr_accessed = gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500807 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600808 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500809 }
810
811 break;
812 }
813
814 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500815 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -0600816 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500817 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600818 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500819
820 r = RESUME_GUEST;
821
822 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600823 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600824 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500825 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600826 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600827 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600828 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500829 break;
830 }
831
Hollis Blanchard7b701592008-12-02 15:51:58 -0600832 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500833
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600834 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -0600835 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500836
837 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
838 /* The guest TLB had a mapping, but the shadow TLB
839 * didn't. This could be because:
840 * a) the entry is mapping the host kernel, or
841 * b) the guest used a large mapping which we're faking
842 * Either way, we need to satisfy the fault without
843 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600844 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500845 } else {
846 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600847 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500848 }
849
850 break;
851 }
852
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500853 case BOOKE_INTERRUPT_DEBUG: {
854 u32 dbsr;
855
856 vcpu->arch.pc = mfspr(SPRN_CSRR0);
857
858 /* clear IAC events in DBSR register */
859 dbsr = mfspr(SPRN_DBSR);
860 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
861 mtspr(SPRN_DBSR, dbsr);
862
863 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600864 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500865 r = RESUME_HOST;
866 break;
867 }
868
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500869 default:
870 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
871 BUG();
872 }
873
874 local_irq_disable();
875
Scott Wood7e28e60e2011-11-08 18:23:20 -0600876 kvmppc_core_prepare_to_enter(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500877
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500878 if (!(r & RESUME_HOST)) {
879 /* To avoid clobbering exit_reason, only check for signals if
880 * we aren't already exiting to userspace for some other
881 * reason. */
882 if (signal_pending(current)) {
883 run->exit_reason = KVM_EXIT_INTR;
884 r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600885 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500886 }
887 }
888
889 return r;
890}
891
892/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
893int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
894{
Hollis Blanchard082decf2010-08-07 10:33:56 -0700895 int i;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200896 int r;
Hollis Blanchard082decf2010-08-07 10:33:56 -0700897
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500898 vcpu->arch.pc = 0;
Scott Woodb5904972011-11-08 18:23:30 -0600899 vcpu->arch.shared->pir = vcpu->vcpu_id;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100900 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Scott Woodd30f6e42011-12-20 15:34:43 +0000901 kvmppc_set_msr(vcpu, 0);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500902
Scott Woodd30f6e42011-12-20 15:34:43 +0000903#ifndef CONFIG_KVM_BOOKE_HV
904 vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500905 vcpu->arch.shadow_pid = 1;
Scott Woodd30f6e42011-12-20 15:34:43 +0000906 vcpu->arch.shared->msr = 0;
907#endif
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500908
Hollis Blanchard082decf2010-08-07 10:33:56 -0700909 /* Eye-catching numbers so we know if the guest takes an interrupt
910 * before it's programmed its own IVPR/IVORs. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500911 vcpu->arch.ivpr = 0x55550000;
Hollis Blanchard082decf2010-08-07 10:33:56 -0700912 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
913 vcpu->arch.ivor[i] = 0x7700 | i * 4;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500914
Hollis Blanchard73e75b412008-12-02 15:51:57 -0600915 kvmppc_init_timing_stats(vcpu);
916
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200917 r = kvmppc_core_vcpu_setup(vcpu);
918 kvmppc_sanity_check(vcpu);
919 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500920}
921
922int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
923{
924 int i;
925
926 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100927 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500928 regs->ctr = vcpu->arch.ctr;
929 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100930 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +0200931 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +0200932 regs->srr0 = vcpu->arch.shared->srr0;
933 regs->srr1 = vcpu->arch.shared->srr1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500934 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +0200935 regs->sprg0 = vcpu->arch.shared->sprg0;
936 regs->sprg1 = vcpu->arch.shared->sprg1;
937 regs->sprg2 = vcpu->arch.shared->sprg2;
938 regs->sprg3 = vcpu->arch.shared->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -0600939 regs->sprg4 = vcpu->arch.shared->sprg4;
940 regs->sprg5 = vcpu->arch.shared->sprg5;
941 regs->sprg6 = vcpu->arch.shared->sprg6;
942 regs->sprg7 = vcpu->arch.shared->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500943
944 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100945 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500946
947 return 0;
948}
949
950int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
951{
952 int i;
953
954 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100955 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500956 vcpu->arch.ctr = regs->ctr;
957 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100958 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -0600959 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +0200960 vcpu->arch.shared->srr0 = regs->srr0;
961 vcpu->arch.shared->srr1 = regs->srr1;
Scott Wood5ce941e2011-04-27 17:24:21 -0500962 kvmppc_set_pid(vcpu, regs->pid);
Alexander Grafa73a9592010-07-29 14:47:47 +0200963 vcpu->arch.shared->sprg0 = regs->sprg0;
964 vcpu->arch.shared->sprg1 = regs->sprg1;
965 vcpu->arch.shared->sprg2 = regs->sprg2;
966 vcpu->arch.shared->sprg3 = regs->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -0600967 vcpu->arch.shared->sprg4 = regs->sprg4;
968 vcpu->arch.shared->sprg5 = regs->sprg5;
969 vcpu->arch.shared->sprg6 = regs->sprg6;
970 vcpu->arch.shared->sprg7 = regs->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500971
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100972 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
973 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500974
975 return 0;
976}
977
Scott Wood5ce941e2011-04-27 17:24:21 -0500978static void get_sregs_base(struct kvm_vcpu *vcpu,
979 struct kvm_sregs *sregs)
980{
981 u64 tb = get_tb();
982
983 sregs->u.e.features |= KVM_SREGS_E_BASE;
984
985 sregs->u.e.csrr0 = vcpu->arch.csrr0;
986 sregs->u.e.csrr1 = vcpu->arch.csrr1;
987 sregs->u.e.mcsr = vcpu->arch.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +0000988 sregs->u.e.esr = get_guest_esr(vcpu);
989 sregs->u.e.dear = get_guest_dear(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -0500990 sregs->u.e.tsr = vcpu->arch.tsr;
991 sregs->u.e.tcr = vcpu->arch.tcr;
992 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
993 sregs->u.e.tb = tb;
994 sregs->u.e.vrsave = vcpu->arch.vrsave;
995}
996
997static int set_sregs_base(struct kvm_vcpu *vcpu,
998 struct kvm_sregs *sregs)
999{
1000 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
1001 return 0;
1002
1003 vcpu->arch.csrr0 = sregs->u.e.csrr0;
1004 vcpu->arch.csrr1 = sregs->u.e.csrr1;
1005 vcpu->arch.mcsr = sregs->u.e.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +00001006 set_guest_esr(vcpu, sregs->u.e.esr);
1007 set_guest_dear(vcpu, sregs->u.e.dear);
Scott Wood5ce941e2011-04-27 17:24:21 -05001008 vcpu->arch.vrsave = sregs->u.e.vrsave;
Scott Wooddfd4d472011-11-17 12:39:59 +00001009 kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001010
Scott Wooddfd4d472011-11-17 12:39:59 +00001011 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
Scott Wood5ce941e2011-04-27 17:24:21 -05001012 vcpu->arch.dec = sregs->u.e.dec;
Scott Wooddfd4d472011-11-17 12:39:59 +00001013 kvmppc_emulate_dec(vcpu);
1014 }
Scott Wood5ce941e2011-04-27 17:24:21 -05001015
1016 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
Scott Wooddfd4d472011-11-17 12:39:59 +00001017 vcpu->arch.tsr = sregs->u.e.tsr;
1018 update_timer_ints(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001019 }
1020
1021 return 0;
1022}
1023
1024static void get_sregs_arch206(struct kvm_vcpu *vcpu,
1025 struct kvm_sregs *sregs)
1026{
1027 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
1028
Scott Wood841741f22011-09-02 17:39:37 -05001029 sregs->u.e.pir = vcpu->vcpu_id;
Scott Wood5ce941e2011-04-27 17:24:21 -05001030 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
1031 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
1032 sregs->u.e.decar = vcpu->arch.decar;
1033 sregs->u.e.ivpr = vcpu->arch.ivpr;
1034}
1035
1036static int set_sregs_arch206(struct kvm_vcpu *vcpu,
1037 struct kvm_sregs *sregs)
1038{
1039 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
1040 return 0;
1041
Scott Wood841741f22011-09-02 17:39:37 -05001042 if (sregs->u.e.pir != vcpu->vcpu_id)
Scott Wood5ce941e2011-04-27 17:24:21 -05001043 return -EINVAL;
1044
1045 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
1046 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
1047 vcpu->arch.decar = sregs->u.e.decar;
1048 vcpu->arch.ivpr = sregs->u.e.ivpr;
1049
1050 return 0;
1051}
1052
1053void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1054{
1055 sregs->u.e.features |= KVM_SREGS_E_IVOR;
1056
1057 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
1058 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
1059 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
1060 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
1061 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
1062 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
1063 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
1064 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
1065 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
1066 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
1067 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
1068 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
1069 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
1070 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
1071 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
1072 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
1073}
1074
1075int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1076{
1077 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
1078 return 0;
1079
1080 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
1081 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
1082 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
1083 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
1084 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
1085 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
1086 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
1087 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
1088 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
1089 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
1090 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
1091 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
1092 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
1093 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
1094 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
1095 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
1096
1097 return 0;
1098}
1099
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001100int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1101 struct kvm_sregs *sregs)
1102{
Scott Wood5ce941e2011-04-27 17:24:21 -05001103 sregs->pvr = vcpu->arch.pvr;
1104
1105 get_sregs_base(vcpu, sregs);
1106 get_sregs_arch206(vcpu, sregs);
1107 kvmppc_core_get_sregs(vcpu, sregs);
1108 return 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001109}
1110
1111int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1112 struct kvm_sregs *sregs)
1113{
Scott Wood5ce941e2011-04-27 17:24:21 -05001114 int ret;
1115
1116 if (vcpu->arch.pvr != sregs->pvr)
1117 return -EINVAL;
1118
1119 ret = set_sregs_base(vcpu, sregs);
1120 if (ret < 0)
1121 return ret;
1122
1123 ret = set_sregs_arch206(vcpu, sregs);
1124 if (ret < 0)
1125 return ret;
1126
1127 return kvmppc_core_set_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001128}
1129
Paul Mackerras31f34382011-12-12 12:26:50 +00001130int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1131{
1132 return -EINVAL;
1133}
1134
1135int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1136{
1137 return -EINVAL;
1138}
1139
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001140int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1141{
1142 return -ENOTSUPP;
1143}
1144
1145int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1146{
1147 return -ENOTSUPP;
1148}
1149
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001150int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1151 struct kvm_translation *tr)
1152{
Avi Kivity98001d82010-05-13 11:05:49 +03001153 int r;
1154
Avi Kivity98001d82010-05-13 11:05:49 +03001155 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +03001156 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001157}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001158
Alexander Graf4e755752009-10-30 05:47:01 +00001159int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1160{
1161 return -ENOTSUPP;
1162}
1163
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001164int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1165 struct kvm_userspace_memory_region *mem)
1166{
1167 return 0;
1168}
1169
1170void kvmppc_core_commit_memory_region(struct kvm *kvm,
1171 struct kvm_userspace_memory_region *mem)
1172{
1173}
1174
Scott Wooddfd4d472011-11-17 12:39:59 +00001175void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1176{
1177 vcpu->arch.tcr = new_tcr;
1178 update_timer_ints(vcpu);
1179}
1180
1181void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1182{
1183 set_bits(tsr_bits, &vcpu->arch.tsr);
1184 smp_wmb();
1185 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1186 kvm_vcpu_kick(vcpu);
1187}
1188
1189void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1190{
1191 clear_bits(tsr_bits, &vcpu->arch.tsr);
1192 update_timer_ints(vcpu);
1193}
1194
1195void kvmppc_decrementer_func(unsigned long data)
1196{
1197 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1198
1199 kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1200}
1201
Scott Wood94fa9d92011-12-20 15:34:22 +00001202void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1203{
Scott Woodd30f6e42011-12-20 15:34:43 +00001204 current->thread.kvm_vcpu = vcpu;
Scott Wood94fa9d92011-12-20 15:34:22 +00001205}
1206
1207void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
1208{
Scott Woodd30f6e42011-12-20 15:34:43 +00001209 current->thread.kvm_vcpu = NULL;
Scott Wood94fa9d92011-12-20 15:34:22 +00001210}
1211
Stephen Rothwell2986b8c2009-06-02 11:46:14 +10001212int __init kvmppc_booke_init(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001213{
Scott Woodd30f6e42011-12-20 15:34:43 +00001214#ifndef CONFIG_KVM_BOOKE_HV
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001215 unsigned long ivor[16];
1216 unsigned long max_ivor = 0;
1217 int i;
1218
1219 /* We install our own exception handlers by hijacking IVPR. IVPR must
1220 * be 16-bit aligned, so we need a 64KB allocation. */
1221 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1222 VCPU_SIZE_ORDER);
1223 if (!kvmppc_booke_handlers)
1224 return -ENOMEM;
1225
1226 /* XXX make sure our handlers are smaller than Linux's */
1227
1228 /* Copy our interrupt handlers to match host IVORs. That way we don't
1229 * have to swap the IVORs on every guest/host transition. */
1230 ivor[0] = mfspr(SPRN_IVOR0);
1231 ivor[1] = mfspr(SPRN_IVOR1);
1232 ivor[2] = mfspr(SPRN_IVOR2);
1233 ivor[3] = mfspr(SPRN_IVOR3);
1234 ivor[4] = mfspr(SPRN_IVOR4);
1235 ivor[5] = mfspr(SPRN_IVOR5);
1236 ivor[6] = mfspr(SPRN_IVOR6);
1237 ivor[7] = mfspr(SPRN_IVOR7);
1238 ivor[8] = mfspr(SPRN_IVOR8);
1239 ivor[9] = mfspr(SPRN_IVOR9);
1240 ivor[10] = mfspr(SPRN_IVOR10);
1241 ivor[11] = mfspr(SPRN_IVOR11);
1242 ivor[12] = mfspr(SPRN_IVOR12);
1243 ivor[13] = mfspr(SPRN_IVOR13);
1244 ivor[14] = mfspr(SPRN_IVOR14);
1245 ivor[15] = mfspr(SPRN_IVOR15);
1246
1247 for (i = 0; i < 16; i++) {
1248 if (ivor[i] > max_ivor)
1249 max_ivor = ivor[i];
1250
1251 memcpy((void *)kvmppc_booke_handlers + ivor[i],
1252 kvmppc_handlers_start + i * kvmppc_handler_len,
1253 kvmppc_handler_len);
1254 }
1255 flush_icache_range(kvmppc_booke_handlers,
1256 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
Scott Woodd30f6e42011-12-20 15:34:43 +00001257#endif /* !BOOKE_HV */
Hollis Blancharddb93f572008-11-05 09:36:18 -06001258 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001259}
1260
Hollis Blancharddb93f572008-11-05 09:36:18 -06001261void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001262{
1263 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
1264 kvm_exit();
1265}