blob: 8e8aa4cb5968eac71c79d0a66e40f7d3af897452 [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;
Alexander Graf79300f82012-02-15 19:12:29 +0000283 ulong msr_mask = 0;
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;
Alexander Graf79300f82012-02-15 19:12:29 +0000325 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000326 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;
Alexander Graf79300f82012-02-15 19:12:29 +0000332 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000333 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;
Scott Woodd30f6e42011-12-20 15:34:43 +0000338 int_class = INT_CLASS_MC;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500339 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600340 case BOOKE_IRQPRIO_DECREMENTER:
341 case BOOKE_IRQPRIO_FIT:
Scott Wooddfd4d472011-11-17 12:39:59 +0000342 keep_irq = true;
343 /* fall through */
344 case BOOKE_IRQPRIO_EXTERNAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000345 case BOOKE_IRQPRIO_DBELL:
Alexander Graf666e7252010-07-29 14:47:43 +0200346 allowed = vcpu->arch.shared->msr & MSR_EE;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200347 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000348 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000349 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500350 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600351 case BOOKE_IRQPRIO_DEBUG:
Alexander Graf666e7252010-07-29 14:47:43 +0200352 allowed = vcpu->arch.shared->msr & MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000353 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000354 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000355 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500356 break;
357 }
358
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600359 if (allowed) {
Scott Woodd30f6e42011-12-20 15:34:43 +0000360 switch (int_class) {
361 case INT_CLASS_NONCRIT:
362 set_guest_srr(vcpu, vcpu->arch.pc,
363 vcpu->arch.shared->msr);
364 break;
365 case INT_CLASS_CRIT:
366 set_guest_csrr(vcpu, vcpu->arch.pc,
367 vcpu->arch.shared->msr);
368 break;
369 case INT_CLASS_DBG:
370 set_guest_dsrr(vcpu, vcpu->arch.pc,
371 vcpu->arch.shared->msr);
372 break;
373 case INT_CLASS_MC:
374 set_guest_mcsrr(vcpu, vcpu->arch.pc,
375 vcpu->arch.shared->msr);
376 break;
377 }
378
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600379 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
Liu Yudaf5e272010-02-02 19:44:35 +0800380 if (update_esr == true)
Scott Woodd30f6e42011-12-20 15:34:43 +0000381 set_guest_esr(vcpu, vcpu->arch.queued_esr);
Liu Yudaf5e272010-02-02 19:44:35 +0800382 if (update_dear == true)
Scott Woodd30f6e42011-12-20 15:34:43 +0000383 set_guest_dear(vcpu, vcpu->arch.queued_dear);
Alexander Graf666e7252010-07-29 14:47:43 +0200384 kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600385
Alexander Grafc5335f12010-08-30 14:03:24 +0200386 if (!keep_irq)
387 clear_bit(priority, &vcpu->arch.pending_exceptions);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600388 }
389
Scott Woodd30f6e42011-12-20 15:34:43 +0000390#ifdef CONFIG_KVM_BOOKE_HV
391 /*
392 * If an interrupt is pending but masked, raise a guest doorbell
393 * so that we are notified when the guest enables the relevant
394 * MSR bit.
395 */
396 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
397 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
398 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
399 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
400 if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
401 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
402#endif
403
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600404 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500405}
406
Scott Wooddfd4d472011-11-17 12:39:59 +0000407static void update_timer_ints(struct kvm_vcpu *vcpu)
408{
409 if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
410 kvmppc_core_queue_dec(vcpu);
411 else
412 kvmppc_core_dequeue_dec(vcpu);
413}
414
Scott Woodc59a6a32011-11-08 18:23:25 -0600415static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500416{
417 unsigned long *pending = &vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500418 unsigned int priority;
419
Scott Wooddfd4d472011-11-17 12:39:59 +0000420 if (vcpu->requests) {
421 if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu)) {
422 smp_mb();
423 update_timer_ints(vcpu);
424 }
425 }
426
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600427 priority = __ffs(*pending);
Alexander Graf8b3a00f2012-02-16 14:12:46 +0000428 while (priority < BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600429 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500430 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500431
432 priority = find_next_bit(pending,
433 BITS_PER_BYTE * sizeof(*pending),
434 priority + 1);
435 }
Alexander Graf90bba352010-07-29 14:47:51 +0200436
437 /* Tell the guest about our interrupt status */
Scott Wood29ac26e2011-11-08 18:23:27 -0600438 vcpu->arch.shared->int_pending = !!*pending;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500439}
440
Scott Woodc59a6a32011-11-08 18:23:25 -0600441/* Check pending exceptions and deliver one, if possible. */
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000442int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
Scott Woodc59a6a32011-11-08 18:23:25 -0600443{
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000444 int r = 0;
Scott Woodc59a6a32011-11-08 18:23:25 -0600445 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);
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000455 r = 1;
Scott Woodc59a6a32011-11-08 18:23:25 -0600456 };
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000457
458 return r;
459}
460
461/*
462 * Common checks before entering the guest world. Call with interrupts
463 * disabled.
464 *
465 * returns !0 if a signal is pending and check_signal is true
466 */
467static int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu, bool check_signal)
468{
469 int r = 0;
470
471 WARN_ON_ONCE(!irqs_disabled());
472 while (true) {
473 if (need_resched()) {
474 local_irq_enable();
475 cond_resched();
476 local_irq_disable();
477 continue;
478 }
479
480 if (check_signal && signal_pending(current)) {
481 r = 1;
482 break;
483 }
484
485 if (kvmppc_core_prepare_to_enter(vcpu)) {
486 /* interrupts got enabled in between, so we
487 are back at square 1 */
488 continue;
489 }
490
491 break;
492 }
493
494 return r;
Scott Woodc59a6a32011-11-08 18:23:25 -0600495}
496
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000497int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
498{
499 int ret;
Scott Wood8fae8452011-12-20 15:34:45 +0000500#ifdef CONFIG_PPC_FPU
501 unsigned int fpscr;
502 int fpexc_mode;
503 u64 fpr[32];
504#endif
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000505
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200506 if (!vcpu->arch.sane) {
507 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
508 return -EINVAL;
509 }
510
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000511 local_irq_disable();
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000512 if (kvmppc_prepare_to_enter(vcpu, true)) {
Scott Wood1d1ef222011-11-08 16:11:59 -0600513 kvm_run->exit_reason = KVM_EXIT_INTR;
514 ret = -EINTR;
515 goto out;
516 }
517
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000518 kvm_guest_enter();
Scott Wood8fae8452011-12-20 15:34:45 +0000519
520#ifdef CONFIG_PPC_FPU
521 /* Save userspace FPU state in stack */
522 enable_kernel_fp();
523 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
524 fpscr = current->thread.fpscr.val;
525 fpexc_mode = current->thread.fpexc_mode;
526
527 /* Restore guest FPU state to thread */
528 memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr));
529 current->thread.fpscr.val = vcpu->arch.fpscr;
530
531 /*
532 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
533 * as always using the FPU. Kernel usage of FP (via
534 * enable_kernel_fp()) in this thread must not occur while
535 * vcpu->fpu_active is set.
536 */
537 vcpu->fpu_active = 1;
538
539 kvmppc_load_guest_fp(vcpu);
540#endif
541
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000542 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
Scott Wood8fae8452011-12-20 15:34:45 +0000543
544#ifdef CONFIG_PPC_FPU
545 kvmppc_save_guest_fp(vcpu);
546
547 vcpu->fpu_active = 0;
548
549 /* Save guest FPU state from thread */
550 memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr));
551 vcpu->arch.fpscr = current->thread.fpscr.val;
552
553 /* Restore userspace FPU state from stack */
554 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
555 current->thread.fpscr.val = fpscr;
556 current->thread.fpexc_mode = fpexc_mode;
557#endif
558
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000559 kvm_guest_exit();
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000560
Scott Wood1d1ef222011-11-08 16:11:59 -0600561out:
562 local_irq_enable();
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000563 return ret;
564}
565
Scott Woodd30f6e42011-12-20 15:34:43 +0000566static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
567{
568 enum emulation_result er;
569
570 er = kvmppc_emulate_instruction(run, vcpu);
571 switch (er) {
572 case EMULATE_DONE:
573 /* don't overwrite subtypes, just account kvm_stats */
574 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
575 /* Future optimization: only reload non-volatiles if
576 * they were actually modified by emulation. */
577 return RESUME_GUEST_NV;
578
579 case EMULATE_DO_DCR:
580 run->exit_reason = KVM_EXIT_DCR;
581 return RESUME_HOST;
582
583 case EMULATE_FAIL:
Scott Woodd30f6e42011-12-20 15:34:43 +0000584 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
585 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
586 /* For debugging, encode the failing instruction and
587 * report it to userspace. */
588 run->hw.hardware_exit_reason = ~0ULL << 32;
589 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
Alexander Grafd1ff5492012-02-16 13:24:03 +0000590 kvmppc_core_queue_program(vcpu, ESR_PIL);
Scott Woodd30f6e42011-12-20 15:34:43 +0000591 return RESUME_HOST;
592
593 default:
594 BUG();
595 }
596}
597
Alexander Graf4e642cc2012-02-20 23:57:26 +0100598static void kvmppc_fill_pt_regs(struct pt_regs *regs)
599{
600 ulong r1, ip, msr, lr;
601
602 asm("mr %0, 1" : "=r"(r1));
603 asm("mflr %0" : "=r"(lr));
604 asm("mfmsr %0" : "=r"(msr));
605 asm("bl 1f; 1: mflr %0" : "=r"(ip));
606
607 memset(regs, 0, sizeof(*regs));
608 regs->gpr[1] = r1;
609 regs->nip = ip;
610 regs->msr = msr;
611 regs->link = lr;
612}
613
614static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
615 unsigned int exit_nr)
616{
617 struct pt_regs regs;
618
619 switch (exit_nr) {
620 case BOOKE_INTERRUPT_EXTERNAL:
621 kvmppc_fill_pt_regs(&regs);
622 do_IRQ(&regs);
623 break;
624 case BOOKE_INTERRUPT_DECREMENTER:
625 kvmppc_fill_pt_regs(&regs);
626 timer_interrupt(&regs);
627 break;
628#if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64)
629 case BOOKE_INTERRUPT_DOORBELL:
630 kvmppc_fill_pt_regs(&regs);
631 doorbell_exception(&regs);
632 break;
633#endif
634 case BOOKE_INTERRUPT_MACHINE_CHECK:
635 /* FIXME */
636 break;
Alexander Graf7cc1e8e2012-02-22 16:26:34 +0100637 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
638 kvmppc_fill_pt_regs(&regs);
639 performance_monitor_exception(&regs);
640 break;
Alexander Graf4e642cc2012-02-20 23:57:26 +0100641 }
642}
643
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500644/**
645 * kvmppc_handle_exit
646 *
647 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
648 */
649int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
650 unsigned int exit_nr)
651{
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500652 int r = RESUME_HOST;
653
Hollis Blanchard73e75b412008-12-02 15:51:57 -0600654 /* update before a new last_exit_type is rewritten */
655 kvmppc_update_timing_stats(vcpu);
656
Alexander Graf4e642cc2012-02-20 23:57:26 +0100657 /* restart interrupts if they were meant for the host */
658 kvmppc_restart_interrupt(vcpu, exit_nr);
Scott Woodd30f6e42011-12-20 15:34:43 +0000659
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500660 local_irq_enable();
661
662 run->exit_reason = KVM_EXIT_UNKNOWN;
663 run->ready_for_interrupt_injection = 1;
664
665 switch (exit_nr) {
666 case BOOKE_INTERRUPT_MACHINE_CHECK:
Alexander Grafc35c9d82012-02-20 12:21:18 +0100667 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
668 kvmppc_dump_vcpu(vcpu);
669 /* For debugging, send invalid exit reason to user space */
670 run->hw.hardware_exit_reason = ~1ULL << 32;
671 run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
672 r = RESUME_HOST;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500673 break;
674
675 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600676 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -0600677 r = RESUME_GUEST;
678 break;
679
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500680 case BOOKE_INTERRUPT_DECREMENTER:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600681 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500682 r = RESUME_GUEST;
683 break;
684
Scott Woodd30f6e42011-12-20 15:34:43 +0000685 case BOOKE_INTERRUPT_DOORBELL:
686 kvmppc_account_exit(vcpu, DBELL_EXITS);
Scott Woodd30f6e42011-12-20 15:34:43 +0000687 r = RESUME_GUEST;
688 break;
689
690 case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
691 kvmppc_account_exit(vcpu, GDBELL_EXITS);
692
693 /*
694 * We are here because there is a pending guest interrupt
695 * which could not be delivered as MSR_CE or MSR_ME was not
696 * set. Once we break from here we will retry delivery.
697 */
698 r = RESUME_GUEST;
699 break;
700
701 case BOOKE_INTERRUPT_GUEST_DBELL:
702 kvmppc_account_exit(vcpu, GDBELL_EXITS);
703
704 /*
705 * We are here because there is a pending guest interrupt
706 * which could not be delivered as MSR_EE was not set. Once
707 * we break from here we will retry delivery.
708 */
709 r = RESUME_GUEST;
710 break;
711
Alexander Graf95f2e922012-02-20 22:45:12 +0100712 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
713 r = RESUME_GUEST;
714 break;
715
Scott Woodd30f6e42011-12-20 15:34:43 +0000716 case BOOKE_INTERRUPT_HV_PRIV:
717 r = emulation_exit(run, vcpu);
718 break;
719
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500720 case BOOKE_INTERRUPT_PROGRAM:
Scott Woodd30f6e42011-12-20 15:34:43 +0000721 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
Alexander Graf02685972012-02-20 12:33:22 +0100722 /*
723 * Program traps generated by user-level software must
724 * be handled by the guest kernel.
725 *
726 * In GS mode, hypervisor privileged instructions trap
727 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
728 * actual program interrupts, handled by the guest.
729 */
Liu Yudaf5e272010-02-02 19:44:35 +0800730 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500731 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600732 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500733 break;
734 }
735
Scott Woodd30f6e42011-12-20 15:34:43 +0000736 r = emulation_exit(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500737 break;
738
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200739 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600740 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600741 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200742 r = RESUME_GUEST;
743 break;
744
Scott Wood4cd35f62011-06-14 18:34:31 -0500745#ifdef CONFIG_SPE
746 case BOOKE_INTERRUPT_SPE_UNAVAIL: {
747 if (vcpu->arch.shared->msr & MSR_SPE)
748 kvmppc_vcpu_enable_spe(vcpu);
749 else
750 kvmppc_booke_queue_irqprio(vcpu,
751 BOOKE_IRQPRIO_SPE_UNAVAIL);
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600752 r = RESUME_GUEST;
753 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500754 }
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600755
756 case BOOKE_INTERRUPT_SPE_FP_DATA:
757 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
758 r = RESUME_GUEST;
759 break;
760
761 case BOOKE_INTERRUPT_SPE_FP_ROUND:
762 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
763 r = RESUME_GUEST;
764 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500765#else
766 case BOOKE_INTERRUPT_SPE_UNAVAIL:
767 /*
768 * Guest wants SPE, but host kernel doesn't support it. Send
769 * an "unimplemented operation" program check to the guest.
770 */
771 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
772 r = RESUME_GUEST;
773 break;
774
775 /*
776 * These really should never happen without CONFIG_SPE,
777 * as we should never enable the real MSR[SPE] in the guest.
778 */
779 case BOOKE_INTERRUPT_SPE_FP_DATA:
780 case BOOKE_INTERRUPT_SPE_FP_ROUND:
781 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
782 __func__, exit_nr, vcpu->arch.pc);
783 run->hw.hardware_exit_reason = exit_nr;
784 r = RESUME_HOST;
785 break;
786#endif
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600787
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500788 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800789 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
790 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600791 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500792 r = RESUME_GUEST;
793 break;
794
795 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800796 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600797 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500798 r = RESUME_GUEST;
799 break;
800
Scott Woodd30f6e42011-12-20 15:34:43 +0000801#ifdef CONFIG_KVM_BOOKE_HV
802 case BOOKE_INTERRUPT_HV_SYSCALL:
803 if (!(vcpu->arch.shared->msr & MSR_PR)) {
804 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
805 } else {
806 /*
807 * hcall from guest userspace -- send privileged
808 * instruction program check.
809 */
810 kvmppc_core_queue_program(vcpu, ESR_PPR);
811 }
812
813 r = RESUME_GUEST;
814 break;
815#else
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500816 case BOOKE_INTERRUPT_SYSCALL:
Alexander Graf2a342ed2010-07-29 14:47:48 +0200817 if (!(vcpu->arch.shared->msr & MSR_PR) &&
818 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
819 /* KVM PV hypercalls */
820 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
821 r = RESUME_GUEST;
822 } else {
823 /* Guest syscalls */
824 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
825 }
Hollis Blanchard7b701592008-12-02 15:51:58 -0600826 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500827 r = RESUME_GUEST;
828 break;
Scott Woodd30f6e42011-12-20 15:34:43 +0000829#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500830
831 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500832 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600833 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600834 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500835 gfn_t gfn;
836
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000837#ifdef CONFIG_KVM_E500V2
Scott Wooda4cd8b22011-06-14 18:34:41 -0500838 if (!(vcpu->arch.shared->msr & MSR_PR) &&
839 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
840 kvmppc_map_magic(vcpu);
841 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
842 r = RESUME_GUEST;
843
844 break;
845 }
846#endif
847
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500848 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600849 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600850 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500851 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +0800852 kvmppc_core_queue_dtlb_miss(vcpu,
853 vcpu->arch.fault_dear,
854 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600855 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600856 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500857 r = RESUME_GUEST;
858 break;
859 }
860
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600861 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600862 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500863
864 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
865 /* The guest TLB had a mapping, but the shadow TLB
866 * didn't, and it is RAM. This could be because:
867 * a) the entry is mapping the host kernel, or
868 * b) the guest used a large mapping which we're faking
869 * Either way, we need to satisfy the fault without
870 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600871 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600872 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500873 r = RESUME_GUEST;
874 } else {
875 /* Guest has mapped and accessed a page which is not
876 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600877 vcpu->arch.paddr_accessed = gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500878 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600879 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500880 }
881
882 break;
883 }
884
885 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500886 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -0600887 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500888 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600889 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500890
891 r = RESUME_GUEST;
892
893 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600894 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600895 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500896 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600897 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600898 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600899 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500900 break;
901 }
902
Hollis Blanchard7b701592008-12-02 15:51:58 -0600903 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500904
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600905 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -0600906 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500907
908 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
909 /* The guest TLB had a mapping, but the shadow TLB
910 * didn't. This could be because:
911 * a) the entry is mapping the host kernel, or
912 * b) the guest used a large mapping which we're faking
913 * Either way, we need to satisfy the fault without
914 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600915 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500916 } else {
917 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600918 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500919 }
920
921 break;
922 }
923
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500924 case BOOKE_INTERRUPT_DEBUG: {
925 u32 dbsr;
926
927 vcpu->arch.pc = mfspr(SPRN_CSRR0);
928
929 /* clear IAC events in DBSR register */
930 dbsr = mfspr(SPRN_DBSR);
931 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
932 mtspr(SPRN_DBSR, dbsr);
933
934 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600935 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500936 r = RESUME_HOST;
937 break;
938 }
939
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500940 default:
941 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
942 BUG();
943 }
944
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000945 /*
946 * To avoid clobbering exit_reason, only check for signals if we
947 * aren't already exiting to userspace for some other reason.
948 */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500949 local_irq_disable();
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000950 if (kvmppc_prepare_to_enter(vcpu, !(r & RESUME_HOST))) {
951 run->exit_reason = KVM_EXIT_INTR;
952 r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
953 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500954 }
955
956 return r;
957}
958
959/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
960int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
961{
Hollis Blanchard082decf2010-08-07 10:33:56 -0700962 int i;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200963 int r;
Hollis Blanchard082decf2010-08-07 10:33:56 -0700964
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500965 vcpu->arch.pc = 0;
Scott Woodb5904972011-11-08 18:23:30 -0600966 vcpu->arch.shared->pir = vcpu->vcpu_id;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100967 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Scott Woodd30f6e42011-12-20 15:34:43 +0000968 kvmppc_set_msr(vcpu, 0);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500969
Scott Woodd30f6e42011-12-20 15:34:43 +0000970#ifndef CONFIG_KVM_BOOKE_HV
971 vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500972 vcpu->arch.shadow_pid = 1;
Scott Woodd30f6e42011-12-20 15:34:43 +0000973 vcpu->arch.shared->msr = 0;
974#endif
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500975
Hollis Blanchard082decf2010-08-07 10:33:56 -0700976 /* Eye-catching numbers so we know if the guest takes an interrupt
977 * before it's programmed its own IVPR/IVORs. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500978 vcpu->arch.ivpr = 0x55550000;
Hollis Blanchard082decf2010-08-07 10:33:56 -0700979 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
980 vcpu->arch.ivor[i] = 0x7700 | i * 4;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500981
Hollis Blanchard73e75b412008-12-02 15:51:57 -0600982 kvmppc_init_timing_stats(vcpu);
983
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200984 r = kvmppc_core_vcpu_setup(vcpu);
985 kvmppc_sanity_check(vcpu);
986 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500987}
988
989int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
990{
991 int i;
992
993 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100994 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500995 regs->ctr = vcpu->arch.ctr;
996 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100997 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +0200998 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +0200999 regs->srr0 = vcpu->arch.shared->srr0;
1000 regs->srr1 = vcpu->arch.shared->srr1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001001 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001002 regs->sprg0 = vcpu->arch.shared->sprg0;
1003 regs->sprg1 = vcpu->arch.shared->sprg1;
1004 regs->sprg2 = vcpu->arch.shared->sprg2;
1005 regs->sprg3 = vcpu->arch.shared->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -06001006 regs->sprg4 = vcpu->arch.shared->sprg4;
1007 regs->sprg5 = vcpu->arch.shared->sprg5;
1008 regs->sprg6 = vcpu->arch.shared->sprg6;
1009 regs->sprg7 = vcpu->arch.shared->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001010
1011 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001012 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001013
1014 return 0;
1015}
1016
1017int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1018{
1019 int i;
1020
1021 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001022 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001023 vcpu->arch.ctr = regs->ctr;
1024 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001025 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -06001026 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +02001027 vcpu->arch.shared->srr0 = regs->srr0;
1028 vcpu->arch.shared->srr1 = regs->srr1;
Scott Wood5ce941e2011-04-27 17:24:21 -05001029 kvmppc_set_pid(vcpu, regs->pid);
Alexander Grafa73a9592010-07-29 14:47:47 +02001030 vcpu->arch.shared->sprg0 = regs->sprg0;
1031 vcpu->arch.shared->sprg1 = regs->sprg1;
1032 vcpu->arch.shared->sprg2 = regs->sprg2;
1033 vcpu->arch.shared->sprg3 = regs->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -06001034 vcpu->arch.shared->sprg4 = regs->sprg4;
1035 vcpu->arch.shared->sprg5 = regs->sprg5;
1036 vcpu->arch.shared->sprg6 = regs->sprg6;
1037 vcpu->arch.shared->sprg7 = regs->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001038
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001039 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1040 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001041
1042 return 0;
1043}
1044
Scott Wood5ce941e2011-04-27 17:24:21 -05001045static void get_sregs_base(struct kvm_vcpu *vcpu,
1046 struct kvm_sregs *sregs)
1047{
1048 u64 tb = get_tb();
1049
1050 sregs->u.e.features |= KVM_SREGS_E_BASE;
1051
1052 sregs->u.e.csrr0 = vcpu->arch.csrr0;
1053 sregs->u.e.csrr1 = vcpu->arch.csrr1;
1054 sregs->u.e.mcsr = vcpu->arch.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +00001055 sregs->u.e.esr = get_guest_esr(vcpu);
1056 sregs->u.e.dear = get_guest_dear(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001057 sregs->u.e.tsr = vcpu->arch.tsr;
1058 sregs->u.e.tcr = vcpu->arch.tcr;
1059 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
1060 sregs->u.e.tb = tb;
1061 sregs->u.e.vrsave = vcpu->arch.vrsave;
1062}
1063
1064static int set_sregs_base(struct kvm_vcpu *vcpu,
1065 struct kvm_sregs *sregs)
1066{
1067 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
1068 return 0;
1069
1070 vcpu->arch.csrr0 = sregs->u.e.csrr0;
1071 vcpu->arch.csrr1 = sregs->u.e.csrr1;
1072 vcpu->arch.mcsr = sregs->u.e.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +00001073 set_guest_esr(vcpu, sregs->u.e.esr);
1074 set_guest_dear(vcpu, sregs->u.e.dear);
Scott Wood5ce941e2011-04-27 17:24:21 -05001075 vcpu->arch.vrsave = sregs->u.e.vrsave;
Scott Wooddfd4d472011-11-17 12:39:59 +00001076 kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001077
Scott Wooddfd4d472011-11-17 12:39:59 +00001078 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
Scott Wood5ce941e2011-04-27 17:24:21 -05001079 vcpu->arch.dec = sregs->u.e.dec;
Scott Wooddfd4d472011-11-17 12:39:59 +00001080 kvmppc_emulate_dec(vcpu);
1081 }
Scott Wood5ce941e2011-04-27 17:24:21 -05001082
1083 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
Scott Wooddfd4d472011-11-17 12:39:59 +00001084 vcpu->arch.tsr = sregs->u.e.tsr;
1085 update_timer_ints(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001086 }
1087
1088 return 0;
1089}
1090
1091static void get_sregs_arch206(struct kvm_vcpu *vcpu,
1092 struct kvm_sregs *sregs)
1093{
1094 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
1095
Scott Wood841741f22011-09-02 17:39:37 -05001096 sregs->u.e.pir = vcpu->vcpu_id;
Scott Wood5ce941e2011-04-27 17:24:21 -05001097 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
1098 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
1099 sregs->u.e.decar = vcpu->arch.decar;
1100 sregs->u.e.ivpr = vcpu->arch.ivpr;
1101}
1102
1103static int set_sregs_arch206(struct kvm_vcpu *vcpu,
1104 struct kvm_sregs *sregs)
1105{
1106 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
1107 return 0;
1108
Scott Wood841741f22011-09-02 17:39:37 -05001109 if (sregs->u.e.pir != vcpu->vcpu_id)
Scott Wood5ce941e2011-04-27 17:24:21 -05001110 return -EINVAL;
1111
1112 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
1113 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
1114 vcpu->arch.decar = sregs->u.e.decar;
1115 vcpu->arch.ivpr = sregs->u.e.ivpr;
1116
1117 return 0;
1118}
1119
1120void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1121{
1122 sregs->u.e.features |= KVM_SREGS_E_IVOR;
1123
1124 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
1125 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
1126 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
1127 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
1128 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
1129 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
1130 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
1131 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
1132 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
1133 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
1134 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
1135 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
1136 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
1137 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
1138 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
1139 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
1140}
1141
1142int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1143{
1144 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
1145 return 0;
1146
1147 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
1148 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
1149 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
1150 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
1151 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
1152 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
1153 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
1154 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
1155 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
1156 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
1157 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
1158 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
1159 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
1160 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
1161 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
1162 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
1163
1164 return 0;
1165}
1166
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001167int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1168 struct kvm_sregs *sregs)
1169{
Scott Wood5ce941e2011-04-27 17:24:21 -05001170 sregs->pvr = vcpu->arch.pvr;
1171
1172 get_sregs_base(vcpu, sregs);
1173 get_sregs_arch206(vcpu, sregs);
1174 kvmppc_core_get_sregs(vcpu, sregs);
1175 return 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001176}
1177
1178int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1179 struct kvm_sregs *sregs)
1180{
Scott Wood5ce941e2011-04-27 17:24:21 -05001181 int ret;
1182
1183 if (vcpu->arch.pvr != sregs->pvr)
1184 return -EINVAL;
1185
1186 ret = set_sregs_base(vcpu, sregs);
1187 if (ret < 0)
1188 return ret;
1189
1190 ret = set_sregs_arch206(vcpu, sregs);
1191 if (ret < 0)
1192 return ret;
1193
1194 return kvmppc_core_set_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001195}
1196
Paul Mackerras31f34382011-12-12 12:26:50 +00001197int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1198{
1199 return -EINVAL;
1200}
1201
1202int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1203{
1204 return -EINVAL;
1205}
1206
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001207int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1208{
1209 return -ENOTSUPP;
1210}
1211
1212int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1213{
1214 return -ENOTSUPP;
1215}
1216
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001217int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1218 struct kvm_translation *tr)
1219{
Avi Kivity98001d82010-05-13 11:05:49 +03001220 int r;
1221
Avi Kivity98001d82010-05-13 11:05:49 +03001222 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +03001223 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001224}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001225
Alexander Graf4e755752009-10-30 05:47:01 +00001226int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1227{
1228 return -ENOTSUPP;
1229}
1230
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001231int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1232 struct kvm_userspace_memory_region *mem)
1233{
1234 return 0;
1235}
1236
1237void kvmppc_core_commit_memory_region(struct kvm *kvm,
1238 struct kvm_userspace_memory_region *mem)
1239{
1240}
1241
Scott Wooddfd4d472011-11-17 12:39:59 +00001242void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1243{
1244 vcpu->arch.tcr = new_tcr;
1245 update_timer_ints(vcpu);
1246}
1247
1248void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1249{
1250 set_bits(tsr_bits, &vcpu->arch.tsr);
1251 smp_wmb();
1252 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1253 kvm_vcpu_kick(vcpu);
1254}
1255
1256void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1257{
1258 clear_bits(tsr_bits, &vcpu->arch.tsr);
1259 update_timer_ints(vcpu);
1260}
1261
1262void kvmppc_decrementer_func(unsigned long data)
1263{
1264 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1265
1266 kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1267}
1268
Scott Wood94fa9d92011-12-20 15:34:22 +00001269void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1270{
Scott Woodd30f6e42011-12-20 15:34:43 +00001271 current->thread.kvm_vcpu = vcpu;
Scott Wood94fa9d92011-12-20 15:34:22 +00001272}
1273
1274void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
1275{
Scott Woodd30f6e42011-12-20 15:34:43 +00001276 current->thread.kvm_vcpu = NULL;
Scott Wood94fa9d92011-12-20 15:34:22 +00001277}
1278
Stephen Rothwell2986b8c2009-06-02 11:46:14 +10001279int __init kvmppc_booke_init(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001280{
Scott Woodd30f6e42011-12-20 15:34:43 +00001281#ifndef CONFIG_KVM_BOOKE_HV
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001282 unsigned long ivor[16];
1283 unsigned long max_ivor = 0;
1284 int i;
1285
1286 /* We install our own exception handlers by hijacking IVPR. IVPR must
1287 * be 16-bit aligned, so we need a 64KB allocation. */
1288 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1289 VCPU_SIZE_ORDER);
1290 if (!kvmppc_booke_handlers)
1291 return -ENOMEM;
1292
1293 /* XXX make sure our handlers are smaller than Linux's */
1294
1295 /* Copy our interrupt handlers to match host IVORs. That way we don't
1296 * have to swap the IVORs on every guest/host transition. */
1297 ivor[0] = mfspr(SPRN_IVOR0);
1298 ivor[1] = mfspr(SPRN_IVOR1);
1299 ivor[2] = mfspr(SPRN_IVOR2);
1300 ivor[3] = mfspr(SPRN_IVOR3);
1301 ivor[4] = mfspr(SPRN_IVOR4);
1302 ivor[5] = mfspr(SPRN_IVOR5);
1303 ivor[6] = mfspr(SPRN_IVOR6);
1304 ivor[7] = mfspr(SPRN_IVOR7);
1305 ivor[8] = mfspr(SPRN_IVOR8);
1306 ivor[9] = mfspr(SPRN_IVOR9);
1307 ivor[10] = mfspr(SPRN_IVOR10);
1308 ivor[11] = mfspr(SPRN_IVOR11);
1309 ivor[12] = mfspr(SPRN_IVOR12);
1310 ivor[13] = mfspr(SPRN_IVOR13);
1311 ivor[14] = mfspr(SPRN_IVOR14);
1312 ivor[15] = mfspr(SPRN_IVOR15);
1313
1314 for (i = 0; i < 16; i++) {
1315 if (ivor[i] > max_ivor)
1316 max_ivor = ivor[i];
1317
1318 memcpy((void *)kvmppc_booke_handlers + ivor[i],
1319 kvmppc_handlers_start + i * kvmppc_handler_len,
1320 kvmppc_handler_len);
1321 }
1322 flush_icache_range(kvmppc_booke_handlers,
1323 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
Scott Woodd30f6e42011-12-20 15:34:43 +00001324#endif /* !BOOKE_HV */
Hollis Blancharddb93f572008-11-05 09:36:18 -06001325 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001326}
1327
Hollis Blancharddb93f572008-11-05 09:36:18 -06001328void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001329{
1330 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
1331 kvm_exit();
1332}