blob: c36493087dbf896b6672ea4016431dfe6593a972 [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"
Alexander Graf97c95052012-08-02 15:10:00 +020042#include "trace.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050043
Hollis Blanchardd9fbd032008-11-05 09:36:13 -060044unsigned long kvmppc_booke_handlers;
45
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050046#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
47#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
48
49struct kvm_stats_debugfs_item debugfs_entries[] = {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050050 { "mmio", VCPU_STAT(mmio_exits) },
51 { "dcr", VCPU_STAT(dcr_exits) },
52 { "sig", VCPU_STAT(signal_exits) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050053 { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
54 { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
55 { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
56 { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
57 { "sysc", VCPU_STAT(syscall_exits) },
58 { "isi", VCPU_STAT(isi_exits) },
59 { "dsi", VCPU_STAT(dsi_exits) },
60 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
61 { "dec", VCPU_STAT(dec_exits) },
62 { "ext_intr", VCPU_STAT(ext_intr_exits) },
Hollis Blanchard45c5eb62008-04-25 17:55:49 -050063 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Scott Woodd30f6e42011-12-20 15:34:43 +000064 { "doorbell", VCPU_STAT(dbell_exits) },
65 { "guest doorbell", VCPU_STAT(gdbell_exits) },
Alexander Grafcf1c5ca2012-08-01 12:56:51 +020066 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050067 { NULL }
68};
69
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050070/* TODO: use vcpu_printf() */
71void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
72{
73 int i;
74
Alexander Graf666e7252010-07-29 14:47:43 +020075 printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060076 printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
Alexander Grafde7906c2010-07-29 14:47:46 +020077 printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
78 vcpu->arch.shared->srr1);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050079
80 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
81
82 for (i = 0; i < 32; i += 4) {
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060083 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
Alexander Graf8e5b26b2010-01-08 02:58:01 +010084 kvmppc_get_gpr(vcpu, i),
85 kvmppc_get_gpr(vcpu, i+1),
86 kvmppc_get_gpr(vcpu, i+2),
87 kvmppc_get_gpr(vcpu, i+3));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050088 }
89}
90
Scott Wood4cd35f62011-06-14 18:34:31 -050091#ifdef CONFIG_SPE
92void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
93{
94 preempt_disable();
95 enable_kernel_spe();
96 kvmppc_save_guest_spe(vcpu);
97 vcpu->arch.shadow_msr &= ~MSR_SPE;
98 preempt_enable();
99}
100
101static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
102{
103 preempt_disable();
104 enable_kernel_spe();
105 kvmppc_load_guest_spe(vcpu);
106 vcpu->arch.shadow_msr |= MSR_SPE;
107 preempt_enable();
108}
109
110static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
111{
112 if (vcpu->arch.shared->msr & MSR_SPE) {
113 if (!(vcpu->arch.shadow_msr & MSR_SPE))
114 kvmppc_vcpu_enable_spe(vcpu);
115 } else if (vcpu->arch.shadow_msr & MSR_SPE) {
116 kvmppc_vcpu_disable_spe(vcpu);
117 }
118}
119#else
120static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
121{
122}
123#endif
124
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500125/*
126 * Helper function for "full" MSR writes. No need to call this if only
127 * EE/CE/ME/DE/RI are changing.
128 */
Scott Wood4cd35f62011-06-14 18:34:31 -0500129void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
130{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500131 u32 old_msr = vcpu->arch.shared->msr;
Scott Wood4cd35f62011-06-14 18:34:31 -0500132
Scott Woodd30f6e42011-12-20 15:34:43 +0000133#ifdef CONFIG_KVM_BOOKE_HV
134 new_msr |= MSR_GS;
135#endif
136
Scott Wood4cd35f62011-06-14 18:34:31 -0500137 vcpu->arch.shared->msr = new_msr;
138
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500139 kvmppc_mmu_msr_notify(vcpu, old_msr);
Scott Wood4cd35f62011-06-14 18:34:31 -0500140 kvmppc_vcpu_sync_spe(vcpu);
141}
142
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600143static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
144 unsigned int priority)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600145{
Alexander Graf63460462012-08-08 00:44:52 +0200146 trace_kvm_booke_queue_irqprio(vcpu, priority);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600147 set_bit(priority, &vcpu->arch.pending_exceptions);
148}
149
Liu Yudaf5e272010-02-02 19:44:35 +0800150static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
151 ulong dear_flags, ulong esr_flags)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600152{
Liu Yudaf5e272010-02-02 19:44:35 +0800153 vcpu->arch.queued_dear = dear_flags;
154 vcpu->arch.queued_esr = esr_flags;
155 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
156}
157
158static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
159 ulong dear_flags, ulong esr_flags)
160{
161 vcpu->arch.queued_dear = dear_flags;
162 vcpu->arch.queued_esr = esr_flags;
163 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
164}
165
166static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
167 ulong esr_flags)
168{
169 vcpu->arch.queued_esr = esr_flags;
170 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
171}
172
173void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
174{
175 vcpu->arch.queued_esr = esr_flags;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600176 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600177}
178
179void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
180{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600181 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600182}
183
184int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
185{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600186 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600187}
188
Alexander Graf7706664d2009-12-21 20:21:24 +0100189void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
190{
191 clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
192}
193
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600194void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
195 struct kvm_interrupt *irq)
196{
Alexander Grafc5335f12010-08-30 14:03:24 +0200197 unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
198
199 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
200 prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
201
202 kvmppc_booke_queue_irqprio(vcpu, prio);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600203}
204
Alexander Graf4496f972010-04-07 10:03:25 +0200205void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
206 struct kvm_interrupt *irq)
207{
208 clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
Alexander Grafc5335f12010-08-30 14:03:24 +0200209 clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
Alexander Graf4496f972010-04-07 10:03:25 +0200210}
211
Scott Woodd30f6e42011-12-20 15:34:43 +0000212static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
213{
214#ifdef CONFIG_KVM_BOOKE_HV
215 mtspr(SPRN_GSRR0, srr0);
216 mtspr(SPRN_GSRR1, srr1);
217#else
218 vcpu->arch.shared->srr0 = srr0;
219 vcpu->arch.shared->srr1 = srr1;
220#endif
221}
222
223static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
224{
225 vcpu->arch.csrr0 = srr0;
226 vcpu->arch.csrr1 = srr1;
227}
228
229static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
230{
231 if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
232 vcpu->arch.dsrr0 = srr0;
233 vcpu->arch.dsrr1 = srr1;
234 } else {
235 set_guest_csrr(vcpu, srr0, srr1);
236 }
237}
238
239static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
240{
241 vcpu->arch.mcsrr0 = srr0;
242 vcpu->arch.mcsrr1 = srr1;
243}
244
245static unsigned long get_guest_dear(struct kvm_vcpu *vcpu)
246{
247#ifdef CONFIG_KVM_BOOKE_HV
248 return mfspr(SPRN_GDEAR);
249#else
250 return vcpu->arch.shared->dar;
251#endif
252}
253
254static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear)
255{
256#ifdef CONFIG_KVM_BOOKE_HV
257 mtspr(SPRN_GDEAR, dear);
258#else
259 vcpu->arch.shared->dar = dear;
260#endif
261}
262
263static unsigned long get_guest_esr(struct kvm_vcpu *vcpu)
264{
265#ifdef CONFIG_KVM_BOOKE_HV
266 return mfspr(SPRN_GESR);
267#else
268 return vcpu->arch.shared->esr;
269#endif
270}
271
272static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr)
273{
274#ifdef CONFIG_KVM_BOOKE_HV
275 mtspr(SPRN_GESR, esr);
276#else
277 vcpu->arch.shared->esr = esr;
278#endif
279}
280
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600281/* Deliver the interrupt of the corresponding priority, if possible. */
282static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
283 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500284{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600285 int allowed = 0;
Alexander Graf79300f82012-02-15 19:12:29 +0000286 ulong msr_mask = 0;
Liu Yudaf5e272010-02-02 19:44:35 +0800287 bool update_esr = false, update_dear = false;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200288 ulong crit_raw = vcpu->arch.shared->critical;
289 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
290 bool crit;
Alexander Grafc5335f12010-08-30 14:03:24 +0200291 bool keep_irq = false;
Scott Woodd30f6e42011-12-20 15:34:43 +0000292 enum int_class int_class;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200293
294 /* Truncate crit indicators in 32 bit mode */
295 if (!(vcpu->arch.shared->msr & MSR_SF)) {
296 crit_raw &= 0xffffffff;
297 crit_r1 &= 0xffffffff;
298 }
299
300 /* Critical section when crit == r1 */
301 crit = (crit_raw == crit_r1);
302 /* ... and we're in supervisor mode */
303 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500304
Alexander Grafc5335f12010-08-30 14:03:24 +0200305 if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
306 priority = BOOKE_IRQPRIO_EXTERNAL;
307 keep_irq = true;
308 }
309
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600310 switch (priority) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600311 case BOOKE_IRQPRIO_DTLB_MISS:
Liu Yudaf5e272010-02-02 19:44:35 +0800312 case BOOKE_IRQPRIO_DATA_STORAGE:
313 update_dear = true;
314 /* fall through */
315 case BOOKE_IRQPRIO_INST_STORAGE:
316 case BOOKE_IRQPRIO_PROGRAM:
317 update_esr = true;
318 /* fall through */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600319 case BOOKE_IRQPRIO_ITLB_MISS:
320 case BOOKE_IRQPRIO_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600321 case BOOKE_IRQPRIO_FP_UNAVAIL:
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600322 case BOOKE_IRQPRIO_SPE_UNAVAIL:
323 case BOOKE_IRQPRIO_SPE_FP_DATA:
324 case BOOKE_IRQPRIO_SPE_FP_ROUND:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600325 case BOOKE_IRQPRIO_AP_UNAVAIL:
326 case BOOKE_IRQPRIO_ALIGNMENT:
327 allowed = 1;
Alexander Graf79300f82012-02-15 19:12:29 +0000328 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000329 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500330 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600331 case BOOKE_IRQPRIO_CRITICAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000332 case BOOKE_IRQPRIO_DBELL_CRIT:
Alexander Graf666e7252010-07-29 14:47:43 +0200333 allowed = vcpu->arch.shared->msr & MSR_CE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000334 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000335 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000336 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500337 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600338 case BOOKE_IRQPRIO_MACHINE_CHECK:
Alexander Graf666e7252010-07-29 14:47:43 +0200339 allowed = vcpu->arch.shared->msr & MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000340 allowed = allowed && !crit;
Scott Woodd30f6e42011-12-20 15:34:43 +0000341 int_class = INT_CLASS_MC;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500342 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600343 case BOOKE_IRQPRIO_DECREMENTER:
344 case BOOKE_IRQPRIO_FIT:
Scott Wooddfd4d472011-11-17 12:39:59 +0000345 keep_irq = true;
346 /* fall through */
347 case BOOKE_IRQPRIO_EXTERNAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000348 case BOOKE_IRQPRIO_DBELL:
Alexander Graf666e7252010-07-29 14:47:43 +0200349 allowed = vcpu->arch.shared->msr & MSR_EE;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200350 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000351 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000352 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500353 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600354 case BOOKE_IRQPRIO_DEBUG:
Alexander Graf666e7252010-07-29 14:47:43 +0200355 allowed = vcpu->arch.shared->msr & MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000356 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000357 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000358 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500359 break;
360 }
361
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600362 if (allowed) {
Scott Woodd30f6e42011-12-20 15:34:43 +0000363 switch (int_class) {
364 case INT_CLASS_NONCRIT:
365 set_guest_srr(vcpu, vcpu->arch.pc,
366 vcpu->arch.shared->msr);
367 break;
368 case INT_CLASS_CRIT:
369 set_guest_csrr(vcpu, vcpu->arch.pc,
370 vcpu->arch.shared->msr);
371 break;
372 case INT_CLASS_DBG:
373 set_guest_dsrr(vcpu, vcpu->arch.pc,
374 vcpu->arch.shared->msr);
375 break;
376 case INT_CLASS_MC:
377 set_guest_mcsrr(vcpu, vcpu->arch.pc,
378 vcpu->arch.shared->msr);
379 break;
380 }
381
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600382 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
Liu Yudaf5e272010-02-02 19:44:35 +0800383 if (update_esr == true)
Scott Woodd30f6e42011-12-20 15:34:43 +0000384 set_guest_esr(vcpu, vcpu->arch.queued_esr);
Liu Yudaf5e272010-02-02 19:44:35 +0800385 if (update_dear == true)
Scott Woodd30f6e42011-12-20 15:34:43 +0000386 set_guest_dear(vcpu, vcpu->arch.queued_dear);
Alexander Graf666e7252010-07-29 14:47:43 +0200387 kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600388
Alexander Grafc5335f12010-08-30 14:03:24 +0200389 if (!keep_irq)
390 clear_bit(priority, &vcpu->arch.pending_exceptions);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600391 }
392
Scott Woodd30f6e42011-12-20 15:34:43 +0000393#ifdef CONFIG_KVM_BOOKE_HV
394 /*
395 * If an interrupt is pending but masked, raise a guest doorbell
396 * so that we are notified when the guest enables the relevant
397 * MSR bit.
398 */
399 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
400 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
401 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
402 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
403 if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
404 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
405#endif
406
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600407 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500408}
409
Scott Wooddfd4d472011-11-17 12:39:59 +0000410static void update_timer_ints(struct kvm_vcpu *vcpu)
411{
412 if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
413 kvmppc_core_queue_dec(vcpu);
414 else
415 kvmppc_core_dequeue_dec(vcpu);
416}
417
Scott Woodc59a6a32011-11-08 18:23:25 -0600418static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500419{
420 unsigned long *pending = &vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500421 unsigned int priority;
422
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600423 priority = __ffs(*pending);
Alexander Graf8b3a00f2012-02-16 14:12:46 +0000424 while (priority < BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600425 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500426 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500427
428 priority = find_next_bit(pending,
429 BITS_PER_BYTE * sizeof(*pending),
430 priority + 1);
431 }
Alexander Graf90bba352010-07-29 14:47:51 +0200432
433 /* Tell the guest about our interrupt status */
Scott Wood29ac26e2011-11-08 18:23:27 -0600434 vcpu->arch.shared->int_pending = !!*pending;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500435}
436
Scott Woodc59a6a32011-11-08 18:23:25 -0600437/* Check pending exceptions and deliver one, if possible. */
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000438int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
Scott Woodc59a6a32011-11-08 18:23:25 -0600439{
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000440 int r = 0;
Scott Woodc59a6a32011-11-08 18:23:25 -0600441 WARN_ON_ONCE(!irqs_disabled());
442
443 kvmppc_core_check_exceptions(vcpu);
444
445 if (vcpu->arch.shared->msr & MSR_WE) {
446 local_irq_enable();
447 kvm_vcpu_block(vcpu);
Alexander Graf966cd0f2012-03-14 16:55:08 +0100448 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Scott Woodc59a6a32011-11-08 18:23:25 -0600449 local_irq_disable();
450
451 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000452 r = 1;
Scott Woodc59a6a32011-11-08 18:23:25 -0600453 };
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000454
455 return r;
456}
457
Alexander Graf7c973a22012-08-13 12:50:35 +0200458int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
Alexander Graf4ffc6352012-08-08 20:31:13 +0200459{
Alexander Graf7c973a22012-08-13 12:50:35 +0200460 int r = 1; /* Indicate we want to get back into the guest */
461
Alexander Graf2d8185d2012-08-10 12:31:12 +0200462 if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
463 update_timer_ints(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200464#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Alexander Graf2d8185d2012-08-10 12:31:12 +0200465 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
466 kvmppc_core_flush_tlb(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200467#endif
Alexander Graf7c973a22012-08-13 12:50:35 +0200468
469 return r;
Alexander Graf4ffc6352012-08-08 20:31:13 +0200470}
471
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000472int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
473{
Alexander Graf7ee78852012-08-13 12:44:41 +0200474 int ret, s;
Scott Wood8fae8452011-12-20 15:34:45 +0000475#ifdef CONFIG_PPC_FPU
476 unsigned int fpscr;
477 int fpexc_mode;
478 u64 fpr[32];
479#endif
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000480
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200481 if (!vcpu->arch.sane) {
482 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
483 return -EINVAL;
484 }
485
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000486 local_irq_disable();
Alexander Graf7ee78852012-08-13 12:44:41 +0200487 s = kvmppc_prepare_to_enter(vcpu);
488 if (s <= 0) {
Alexander Graf24afa372012-08-12 12:42:30 +0200489 local_irq_enable();
Alexander Graf7ee78852012-08-13 12:44:41 +0200490 ret = s;
Scott Wood1d1ef222011-11-08 16:11:59 -0600491 goto out;
492 }
Alexander Grafbd2be682012-08-13 01:04:19 +0200493 kvmppc_lazy_ee_enable();
Scott Wood1d1ef222011-11-08 16:11:59 -0600494
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000495 kvm_guest_enter();
Scott Wood8fae8452011-12-20 15:34:45 +0000496
497#ifdef CONFIG_PPC_FPU
498 /* Save userspace FPU state in stack */
499 enable_kernel_fp();
500 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
501 fpscr = current->thread.fpscr.val;
502 fpexc_mode = current->thread.fpexc_mode;
503
504 /* Restore guest FPU state to thread */
505 memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr));
506 current->thread.fpscr.val = vcpu->arch.fpscr;
507
508 /*
509 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
510 * as always using the FPU. Kernel usage of FP (via
511 * enable_kernel_fp()) in this thread must not occur while
512 * vcpu->fpu_active is set.
513 */
514 vcpu->fpu_active = 1;
515
516 kvmppc_load_guest_fp(vcpu);
517#endif
518
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000519 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
Scott Wood8fae8452011-12-20 15:34:45 +0000520
Alexander Graf24afa372012-08-12 12:42:30 +0200521 /* No need for kvm_guest_exit. It's done in handle_exit.
522 We also get here with interrupts enabled. */
523
Scott Wood8fae8452011-12-20 15:34:45 +0000524#ifdef CONFIG_PPC_FPU
525 kvmppc_save_guest_fp(vcpu);
526
527 vcpu->fpu_active = 0;
528
529 /* Save guest FPU state from thread */
530 memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr));
531 vcpu->arch.fpscr = current->thread.fpscr.val;
532
533 /* Restore userspace FPU state from stack */
534 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
535 current->thread.fpscr.val = fpscr;
536 current->thread.fpexc_mode = fpexc_mode;
537#endif
538
Scott Wood1d1ef222011-11-08 16:11:59 -0600539out:
Alexander Grafd69c6432012-08-08 20:44:20 +0200540 vcpu->mode = OUTSIDE_GUEST_MODE;
541 smp_wmb();
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000542 return ret;
543}
544
Scott Woodd30f6e42011-12-20 15:34:43 +0000545static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
546{
547 enum emulation_result er;
548
549 er = kvmppc_emulate_instruction(run, vcpu);
550 switch (er) {
551 case EMULATE_DONE:
552 /* don't overwrite subtypes, just account kvm_stats */
553 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
554 /* Future optimization: only reload non-volatiles if
555 * they were actually modified by emulation. */
556 return RESUME_GUEST_NV;
557
558 case EMULATE_DO_DCR:
559 run->exit_reason = KVM_EXIT_DCR;
560 return RESUME_HOST;
561
562 case EMULATE_FAIL:
Scott Woodd30f6e42011-12-20 15:34:43 +0000563 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
564 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
565 /* For debugging, encode the failing instruction and
566 * report it to userspace. */
567 run->hw.hardware_exit_reason = ~0ULL << 32;
568 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
Alexander Grafd1ff5492012-02-16 13:24:03 +0000569 kvmppc_core_queue_program(vcpu, ESR_PIL);
Scott Woodd30f6e42011-12-20 15:34:43 +0000570 return RESUME_HOST;
571
572 default:
573 BUG();
574 }
575}
576
Alexander Graf4e642cc2012-02-20 23:57:26 +0100577static void kvmppc_fill_pt_regs(struct pt_regs *regs)
578{
579 ulong r1, ip, msr, lr;
580
581 asm("mr %0, 1" : "=r"(r1));
582 asm("mflr %0" : "=r"(lr));
583 asm("mfmsr %0" : "=r"(msr));
584 asm("bl 1f; 1: mflr %0" : "=r"(ip));
585
586 memset(regs, 0, sizeof(*regs));
587 regs->gpr[1] = r1;
588 regs->nip = ip;
589 regs->msr = msr;
590 regs->link = lr;
591}
592
Bharat Bhushan6328e592012-06-20 05:56:53 +0000593/*
594 * For interrupts needed to be handled by host interrupt handlers,
595 * corresponding host handler are called from here in similar way
596 * (but not exact) as they are called from low level handler
597 * (such as from arch/powerpc/kernel/head_fsl_booke.S).
598 */
Alexander Graf4e642cc2012-02-20 23:57:26 +0100599static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
600 unsigned int exit_nr)
601{
602 struct pt_regs regs;
603
604 switch (exit_nr) {
605 case BOOKE_INTERRUPT_EXTERNAL:
606 kvmppc_fill_pt_regs(&regs);
607 do_IRQ(&regs);
608 break;
609 case BOOKE_INTERRUPT_DECREMENTER:
610 kvmppc_fill_pt_regs(&regs);
611 timer_interrupt(&regs);
612 break;
613#if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64)
614 case BOOKE_INTERRUPT_DOORBELL:
615 kvmppc_fill_pt_regs(&regs);
616 doorbell_exception(&regs);
617 break;
618#endif
619 case BOOKE_INTERRUPT_MACHINE_CHECK:
620 /* FIXME */
621 break;
Alexander Graf7cc1e8e2012-02-22 16:26:34 +0100622 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
623 kvmppc_fill_pt_regs(&regs);
624 performance_monitor_exception(&regs);
625 break;
Bharat Bhushan6328e592012-06-20 05:56:53 +0000626 case BOOKE_INTERRUPT_WATCHDOG:
627 kvmppc_fill_pt_regs(&regs);
628#ifdef CONFIG_BOOKE_WDT
629 WatchdogException(&regs);
630#else
631 unknown_exception(&regs);
632#endif
633 break;
634 case BOOKE_INTERRUPT_CRITICAL:
635 unknown_exception(&regs);
636 break;
Alexander Graf4e642cc2012-02-20 23:57:26 +0100637 }
638}
639
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500640/**
641 * kvmppc_handle_exit
642 *
643 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
644 */
645int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
646 unsigned int exit_nr)
647{
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500648 int r = RESUME_HOST;
Alexander Graf7ee78852012-08-13 12:44:41 +0200649 int s;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500650
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600651 /* update before a new last_exit_type is rewritten */
652 kvmppc_update_timing_stats(vcpu);
653
Alexander Graf4e642cc2012-02-20 23:57:26 +0100654 /* restart interrupts if they were meant for the host */
655 kvmppc_restart_interrupt(vcpu, exit_nr);
Scott Woodd30f6e42011-12-20 15:34:43 +0000656
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500657 local_irq_enable();
658
Alexander Graf97c95052012-08-02 15:10:00 +0200659 trace_kvm_exit(exit_nr, vcpu);
Alexander Graf706fb732012-08-12 11:29:09 +0200660 kvm_guest_exit();
Alexander Graf97c95052012-08-02 15:10:00 +0200661
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500662 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
Bharat Bhushan6328e592012-06-20 05:56:53 +0000685 case BOOKE_INTERRUPT_WATCHDOG:
686 r = RESUME_GUEST;
687 break;
688
Scott Woodd30f6e42011-12-20 15:34:43 +0000689 case BOOKE_INTERRUPT_DOORBELL:
690 kvmppc_account_exit(vcpu, DBELL_EXITS);
Scott Woodd30f6e42011-12-20 15:34:43 +0000691 r = RESUME_GUEST;
692 break;
693
694 case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
695 kvmppc_account_exit(vcpu, GDBELL_EXITS);
696
697 /*
698 * We are here because there is a pending guest interrupt
699 * which could not be delivered as MSR_CE or MSR_ME was not
700 * set. Once we break from here we will retry delivery.
701 */
702 r = RESUME_GUEST;
703 break;
704
705 case BOOKE_INTERRUPT_GUEST_DBELL:
706 kvmppc_account_exit(vcpu, GDBELL_EXITS);
707
708 /*
709 * We are here because there is a pending guest interrupt
710 * which could not be delivered as MSR_EE was not set. Once
711 * we break from here we will retry delivery.
712 */
713 r = RESUME_GUEST;
714 break;
715
Alexander Graf95f2e922012-02-20 22:45:12 +0100716 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
717 r = RESUME_GUEST;
718 break;
719
Scott Woodd30f6e42011-12-20 15:34:43 +0000720 case BOOKE_INTERRUPT_HV_PRIV:
721 r = emulation_exit(run, vcpu);
722 break;
723
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500724 case BOOKE_INTERRUPT_PROGRAM:
Scott Woodd30f6e42011-12-20 15:34:43 +0000725 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
Alexander Graf02685972012-02-20 12:33:22 +0100726 /*
727 * Program traps generated by user-level software must
728 * be handled by the guest kernel.
729 *
730 * In GS mode, hypervisor privileged instructions trap
731 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
732 * actual program interrupts, handled by the guest.
733 */
Liu Yudaf5e272010-02-02 19:44:35 +0800734 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500735 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600736 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500737 break;
738 }
739
Scott Woodd30f6e42011-12-20 15:34:43 +0000740 r = emulation_exit(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500741 break;
742
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200743 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600744 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600745 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200746 r = RESUME_GUEST;
747 break;
748
Scott Wood4cd35f62011-06-14 18:34:31 -0500749#ifdef CONFIG_SPE
750 case BOOKE_INTERRUPT_SPE_UNAVAIL: {
751 if (vcpu->arch.shared->msr & MSR_SPE)
752 kvmppc_vcpu_enable_spe(vcpu);
753 else
754 kvmppc_booke_queue_irqprio(vcpu,
755 BOOKE_IRQPRIO_SPE_UNAVAIL);
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600756 r = RESUME_GUEST;
757 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500758 }
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600759
760 case BOOKE_INTERRUPT_SPE_FP_DATA:
761 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
762 r = RESUME_GUEST;
763 break;
764
765 case BOOKE_INTERRUPT_SPE_FP_ROUND:
766 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
767 r = RESUME_GUEST;
768 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500769#else
770 case BOOKE_INTERRUPT_SPE_UNAVAIL:
771 /*
772 * Guest wants SPE, but host kernel doesn't support it. Send
773 * an "unimplemented operation" program check to the guest.
774 */
775 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
776 r = RESUME_GUEST;
777 break;
778
779 /*
780 * These really should never happen without CONFIG_SPE,
781 * as we should never enable the real MSR[SPE] in the guest.
782 */
783 case BOOKE_INTERRUPT_SPE_FP_DATA:
784 case BOOKE_INTERRUPT_SPE_FP_ROUND:
785 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
786 __func__, exit_nr, vcpu->arch.pc);
787 run->hw.hardware_exit_reason = exit_nr;
788 r = RESUME_HOST;
789 break;
790#endif
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600791
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500792 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800793 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
794 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600795 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500796 r = RESUME_GUEST;
797 break;
798
799 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800800 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600801 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500802 r = RESUME_GUEST;
803 break;
804
Scott Woodd30f6e42011-12-20 15:34:43 +0000805#ifdef CONFIG_KVM_BOOKE_HV
806 case BOOKE_INTERRUPT_HV_SYSCALL:
807 if (!(vcpu->arch.shared->msr & MSR_PR)) {
808 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
809 } else {
810 /*
811 * hcall from guest userspace -- send privileged
812 * instruction program check.
813 */
814 kvmppc_core_queue_program(vcpu, ESR_PPR);
815 }
816
817 r = RESUME_GUEST;
818 break;
819#else
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500820 case BOOKE_INTERRUPT_SYSCALL:
Alexander Graf2a342ed2010-07-29 14:47:48 +0200821 if (!(vcpu->arch.shared->msr & MSR_PR) &&
822 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
823 /* KVM PV hypercalls */
824 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
825 r = RESUME_GUEST;
826 } else {
827 /* Guest syscalls */
828 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
829 }
Hollis Blanchard7b701592008-12-02 15:51:58 -0600830 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500831 r = RESUME_GUEST;
832 break;
Scott Woodd30f6e42011-12-20 15:34:43 +0000833#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500834
835 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500836 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600837 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600838 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500839 gfn_t gfn;
840
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000841#ifdef CONFIG_KVM_E500V2
Scott Wooda4cd8b22011-06-14 18:34:41 -0500842 if (!(vcpu->arch.shared->msr & MSR_PR) &&
843 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
844 kvmppc_map_magic(vcpu);
845 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
846 r = RESUME_GUEST;
847
848 break;
849 }
850#endif
851
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500852 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600853 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600854 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500855 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +0800856 kvmppc_core_queue_dtlb_miss(vcpu,
857 vcpu->arch.fault_dear,
858 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600859 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600860 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500861 r = RESUME_GUEST;
862 break;
863 }
864
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600865 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600866 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500867
868 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
869 /* The guest TLB had a mapping, but the shadow TLB
870 * didn't, and it is RAM. This could be because:
871 * a) the entry is mapping the host kernel, or
872 * b) the guest used a large mapping which we're faking
873 * Either way, we need to satisfy the fault without
874 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600875 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600876 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500877 r = RESUME_GUEST;
878 } else {
879 /* Guest has mapped and accessed a page which is not
880 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600881 vcpu->arch.paddr_accessed = gpaddr;
Alexander Graf6020c0f2012-03-12 02:26:30 +0100882 vcpu->arch.vaddr_accessed = eaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500883 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600884 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500885 }
886
887 break;
888 }
889
890 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500891 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -0600892 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500893 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600894 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500895
896 r = RESUME_GUEST;
897
898 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600899 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600900 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500901 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600902 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600903 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600904 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500905 break;
906 }
907
Hollis Blanchard7b701592008-12-02 15:51:58 -0600908 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500909
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600910 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -0600911 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500912
913 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
914 /* The guest TLB had a mapping, but the shadow TLB
915 * didn't. This could be because:
916 * a) the entry is mapping the host kernel, or
917 * b) the guest used a large mapping which we're faking
918 * Either way, we need to satisfy the fault without
919 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600920 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500921 } else {
922 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600923 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500924 }
925
926 break;
927 }
928
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500929 case BOOKE_INTERRUPT_DEBUG: {
930 u32 dbsr;
931
932 vcpu->arch.pc = mfspr(SPRN_CSRR0);
933
934 /* clear IAC events in DBSR register */
935 dbsr = mfspr(SPRN_DBSR);
936 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
937 mtspr(SPRN_DBSR, dbsr);
938
939 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600940 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500941 r = RESUME_HOST;
942 break;
943 }
944
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500945 default:
946 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
947 BUG();
948 }
949
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000950 /*
951 * To avoid clobbering exit_reason, only check for signals if we
952 * aren't already exiting to userspace for some other reason.
953 */
Alexander Graf03660ba2012-02-28 12:00:41 +0100954 if (!(r & RESUME_HOST)) {
955 local_irq_disable();
Alexander Graf7ee78852012-08-13 12:44:41 +0200956 s = kvmppc_prepare_to_enter(vcpu);
957 if (s <= 0) {
Alexander Graf24afa372012-08-12 12:42:30 +0200958 local_irq_enable();
Alexander Graf7ee78852012-08-13 12:44:41 +0200959 r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
Alexander Graf24afa372012-08-12 12:42:30 +0200960 } else {
Alexander Grafbd2be682012-08-13 01:04:19 +0200961 kvmppc_lazy_ee_enable();
Alexander Graf03660ba2012-02-28 12:00:41 +0100962 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500963 }
964
965 return r;
966}
967
968/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
969int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
970{
Hollis Blanchard082decf2010-08-07 10:33:56 -0700971 int i;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200972 int r;
Hollis Blanchard082decf2010-08-07 10:33:56 -0700973
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500974 vcpu->arch.pc = 0;
Scott Woodb5904972011-11-08 18:23:30 -0600975 vcpu->arch.shared->pir = vcpu->vcpu_id;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100976 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Scott Woodd30f6e42011-12-20 15:34:43 +0000977 kvmppc_set_msr(vcpu, 0);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500978
Scott Woodd30f6e42011-12-20 15:34:43 +0000979#ifndef CONFIG_KVM_BOOKE_HV
980 vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500981 vcpu->arch.shadow_pid = 1;
Scott Woodd30f6e42011-12-20 15:34:43 +0000982 vcpu->arch.shared->msr = 0;
983#endif
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500984
Hollis Blanchard082decf2010-08-07 10:33:56 -0700985 /* Eye-catching numbers so we know if the guest takes an interrupt
986 * before it's programmed its own IVPR/IVORs. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500987 vcpu->arch.ivpr = 0x55550000;
Hollis Blanchard082decf2010-08-07 10:33:56 -0700988 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
989 vcpu->arch.ivor[i] = 0x7700 | i * 4;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500990
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600991 kvmppc_init_timing_stats(vcpu);
992
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200993 r = kvmppc_core_vcpu_setup(vcpu);
994 kvmppc_sanity_check(vcpu);
995 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500996}
997
998int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
999{
1000 int i;
1001
1002 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001003 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001004 regs->ctr = vcpu->arch.ctr;
1005 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001006 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001007 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +02001008 regs->srr0 = vcpu->arch.shared->srr0;
1009 regs->srr1 = vcpu->arch.shared->srr1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001010 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001011 regs->sprg0 = vcpu->arch.shared->sprg0;
1012 regs->sprg1 = vcpu->arch.shared->sprg1;
1013 regs->sprg2 = vcpu->arch.shared->sprg2;
1014 regs->sprg3 = vcpu->arch.shared->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -06001015 regs->sprg4 = vcpu->arch.shared->sprg4;
1016 regs->sprg5 = vcpu->arch.shared->sprg5;
1017 regs->sprg6 = vcpu->arch.shared->sprg6;
1018 regs->sprg7 = vcpu->arch.shared->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001019
1020 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001021 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001022
1023 return 0;
1024}
1025
1026int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1027{
1028 int i;
1029
1030 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001031 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001032 vcpu->arch.ctr = regs->ctr;
1033 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001034 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -06001035 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +02001036 vcpu->arch.shared->srr0 = regs->srr0;
1037 vcpu->arch.shared->srr1 = regs->srr1;
Scott Wood5ce941e2011-04-27 17:24:21 -05001038 kvmppc_set_pid(vcpu, regs->pid);
Alexander Grafa73a9592010-07-29 14:47:47 +02001039 vcpu->arch.shared->sprg0 = regs->sprg0;
1040 vcpu->arch.shared->sprg1 = regs->sprg1;
1041 vcpu->arch.shared->sprg2 = regs->sprg2;
1042 vcpu->arch.shared->sprg3 = regs->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -06001043 vcpu->arch.shared->sprg4 = regs->sprg4;
1044 vcpu->arch.shared->sprg5 = regs->sprg5;
1045 vcpu->arch.shared->sprg6 = regs->sprg6;
1046 vcpu->arch.shared->sprg7 = regs->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001047
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001048 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1049 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001050
1051 return 0;
1052}
1053
Scott Wood5ce941e2011-04-27 17:24:21 -05001054static void get_sregs_base(struct kvm_vcpu *vcpu,
1055 struct kvm_sregs *sregs)
1056{
1057 u64 tb = get_tb();
1058
1059 sregs->u.e.features |= KVM_SREGS_E_BASE;
1060
1061 sregs->u.e.csrr0 = vcpu->arch.csrr0;
1062 sregs->u.e.csrr1 = vcpu->arch.csrr1;
1063 sregs->u.e.mcsr = vcpu->arch.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +00001064 sregs->u.e.esr = get_guest_esr(vcpu);
1065 sregs->u.e.dear = get_guest_dear(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001066 sregs->u.e.tsr = vcpu->arch.tsr;
1067 sregs->u.e.tcr = vcpu->arch.tcr;
1068 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
1069 sregs->u.e.tb = tb;
1070 sregs->u.e.vrsave = vcpu->arch.vrsave;
1071}
1072
1073static int set_sregs_base(struct kvm_vcpu *vcpu,
1074 struct kvm_sregs *sregs)
1075{
1076 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
1077 return 0;
1078
1079 vcpu->arch.csrr0 = sregs->u.e.csrr0;
1080 vcpu->arch.csrr1 = sregs->u.e.csrr1;
1081 vcpu->arch.mcsr = sregs->u.e.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +00001082 set_guest_esr(vcpu, sregs->u.e.esr);
1083 set_guest_dear(vcpu, sregs->u.e.dear);
Scott Wood5ce941e2011-04-27 17:24:21 -05001084 vcpu->arch.vrsave = sregs->u.e.vrsave;
Scott Wooddfd4d472011-11-17 12:39:59 +00001085 kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001086
Scott Wooddfd4d472011-11-17 12:39:59 +00001087 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
Scott Wood5ce941e2011-04-27 17:24:21 -05001088 vcpu->arch.dec = sregs->u.e.dec;
Scott Wooddfd4d472011-11-17 12:39:59 +00001089 kvmppc_emulate_dec(vcpu);
1090 }
Scott Wood5ce941e2011-04-27 17:24:21 -05001091
1092 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
Scott Wooddfd4d472011-11-17 12:39:59 +00001093 vcpu->arch.tsr = sregs->u.e.tsr;
1094 update_timer_ints(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001095 }
1096
1097 return 0;
1098}
1099
1100static void get_sregs_arch206(struct kvm_vcpu *vcpu,
1101 struct kvm_sregs *sregs)
1102{
1103 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
1104
Scott Wood841741f2011-09-02 17:39:37 -05001105 sregs->u.e.pir = vcpu->vcpu_id;
Scott Wood5ce941e2011-04-27 17:24:21 -05001106 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
1107 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
1108 sregs->u.e.decar = vcpu->arch.decar;
1109 sregs->u.e.ivpr = vcpu->arch.ivpr;
1110}
1111
1112static int set_sregs_arch206(struct kvm_vcpu *vcpu,
1113 struct kvm_sregs *sregs)
1114{
1115 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
1116 return 0;
1117
Scott Wood841741f2011-09-02 17:39:37 -05001118 if (sregs->u.e.pir != vcpu->vcpu_id)
Scott Wood5ce941e2011-04-27 17:24:21 -05001119 return -EINVAL;
1120
1121 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
1122 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
1123 vcpu->arch.decar = sregs->u.e.decar;
1124 vcpu->arch.ivpr = sregs->u.e.ivpr;
1125
1126 return 0;
1127}
1128
1129void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1130{
1131 sregs->u.e.features |= KVM_SREGS_E_IVOR;
1132
1133 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
1134 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
1135 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
1136 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
1137 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
1138 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
1139 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
1140 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
1141 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
1142 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
1143 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
1144 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
1145 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
1146 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
1147 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
1148 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
1149}
1150
1151int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1152{
1153 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
1154 return 0;
1155
1156 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
1157 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
1158 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
1159 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
1160 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
1161 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
1162 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
1163 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
1164 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
1165 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
1166 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
1167 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
1168 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
1169 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
1170 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
1171 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
1172
1173 return 0;
1174}
1175
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001176int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1177 struct kvm_sregs *sregs)
1178{
Scott Wood5ce941e2011-04-27 17:24:21 -05001179 sregs->pvr = vcpu->arch.pvr;
1180
1181 get_sregs_base(vcpu, sregs);
1182 get_sregs_arch206(vcpu, sregs);
1183 kvmppc_core_get_sregs(vcpu, sregs);
1184 return 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001185}
1186
1187int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1188 struct kvm_sregs *sregs)
1189{
Scott Wood5ce941e2011-04-27 17:24:21 -05001190 int ret;
1191
1192 if (vcpu->arch.pvr != sregs->pvr)
1193 return -EINVAL;
1194
1195 ret = set_sregs_base(vcpu, sregs);
1196 if (ret < 0)
1197 return ret;
1198
1199 ret = set_sregs_arch206(vcpu, sregs);
1200 if (ret < 0)
1201 return ret;
1202
1203 return kvmppc_core_set_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001204}
1205
Paul Mackerras31f34382011-12-12 12:26:50 +00001206int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1207{
1208 return -EINVAL;
1209}
1210
1211int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1212{
1213 return -EINVAL;
1214}
1215
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001216int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1217{
1218 return -ENOTSUPP;
1219}
1220
1221int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1222{
1223 return -ENOTSUPP;
1224}
1225
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001226int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1227 struct kvm_translation *tr)
1228{
Avi Kivity98001d82010-05-13 11:05:49 +03001229 int r;
1230
Avi Kivity98001d82010-05-13 11:05:49 +03001231 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +03001232 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001233}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001234
Alexander Graf4e755752009-10-30 05:47:01 +00001235int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1236{
1237 return -ENOTSUPP;
1238}
1239
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001240int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1241 struct kvm_userspace_memory_region *mem)
1242{
1243 return 0;
1244}
1245
1246void kvmppc_core_commit_memory_region(struct kvm *kvm,
1247 struct kvm_userspace_memory_region *mem)
1248{
1249}
1250
Scott Wooddfd4d472011-11-17 12:39:59 +00001251void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1252{
1253 vcpu->arch.tcr = new_tcr;
1254 update_timer_ints(vcpu);
1255}
1256
1257void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1258{
1259 set_bits(tsr_bits, &vcpu->arch.tsr);
1260 smp_wmb();
1261 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1262 kvm_vcpu_kick(vcpu);
1263}
1264
1265void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1266{
1267 clear_bits(tsr_bits, &vcpu->arch.tsr);
1268 update_timer_ints(vcpu);
1269}
1270
1271void kvmppc_decrementer_func(unsigned long data)
1272{
1273 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1274
Bharat Bhushan21bd0002012-05-20 23:21:23 +00001275 if (vcpu->arch.tcr & TCR_ARE) {
1276 vcpu->arch.dec = vcpu->arch.decar;
1277 kvmppc_emulate_dec(vcpu);
1278 }
1279
Scott Wooddfd4d472011-11-17 12:39:59 +00001280 kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1281}
1282
Scott Wood94fa9d92011-12-20 15:34:22 +00001283void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1284{
Scott Woodd30f6e42011-12-20 15:34:43 +00001285 current->thread.kvm_vcpu = vcpu;
Scott Wood94fa9d92011-12-20 15:34:22 +00001286}
1287
1288void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
1289{
Scott Woodd30f6e42011-12-20 15:34:43 +00001290 current->thread.kvm_vcpu = NULL;
Scott Wood94fa9d92011-12-20 15:34:22 +00001291}
1292
Stephen Rothwell2986b8c2009-06-02 11:46:14 +10001293int __init kvmppc_booke_init(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001294{
Scott Woodd30f6e42011-12-20 15:34:43 +00001295#ifndef CONFIG_KVM_BOOKE_HV
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001296 unsigned long ivor[16];
1297 unsigned long max_ivor = 0;
1298 int i;
1299
1300 /* We install our own exception handlers by hijacking IVPR. IVPR must
1301 * be 16-bit aligned, so we need a 64KB allocation. */
1302 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1303 VCPU_SIZE_ORDER);
1304 if (!kvmppc_booke_handlers)
1305 return -ENOMEM;
1306
1307 /* XXX make sure our handlers are smaller than Linux's */
1308
1309 /* Copy our interrupt handlers to match host IVORs. That way we don't
1310 * have to swap the IVORs on every guest/host transition. */
1311 ivor[0] = mfspr(SPRN_IVOR0);
1312 ivor[1] = mfspr(SPRN_IVOR1);
1313 ivor[2] = mfspr(SPRN_IVOR2);
1314 ivor[3] = mfspr(SPRN_IVOR3);
1315 ivor[4] = mfspr(SPRN_IVOR4);
1316 ivor[5] = mfspr(SPRN_IVOR5);
1317 ivor[6] = mfspr(SPRN_IVOR6);
1318 ivor[7] = mfspr(SPRN_IVOR7);
1319 ivor[8] = mfspr(SPRN_IVOR8);
1320 ivor[9] = mfspr(SPRN_IVOR9);
1321 ivor[10] = mfspr(SPRN_IVOR10);
1322 ivor[11] = mfspr(SPRN_IVOR11);
1323 ivor[12] = mfspr(SPRN_IVOR12);
1324 ivor[13] = mfspr(SPRN_IVOR13);
1325 ivor[14] = mfspr(SPRN_IVOR14);
1326 ivor[15] = mfspr(SPRN_IVOR15);
1327
1328 for (i = 0; i < 16; i++) {
1329 if (ivor[i] > max_ivor)
1330 max_ivor = ivor[i];
1331
1332 memcpy((void *)kvmppc_booke_handlers + ivor[i],
1333 kvmppc_handlers_start + i * kvmppc_handler_len,
1334 kvmppc_handler_len);
1335 }
1336 flush_icache_range(kvmppc_booke_handlers,
1337 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
Scott Woodd30f6e42011-12-20 15:34:43 +00001338#endif /* !BOOKE_HV */
Hollis Blancharddb93f572008-11-05 09:36:18 -06001339 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001340}
1341
Hollis Blancharddb93f572008-11-05 09:36:18 -06001342void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001343{
1344 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
1345 kvm_exit();
1346}