blob: 683cbd686d01456cc9b8259734e2f1bdb2300624 [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 Graf4ffc6352012-08-08 20:31:13 +0200458static void kvmppc_check_requests(struct kvm_vcpu *vcpu)
459{
Alexander Graf2d8185d2012-08-10 12:31:12 +0200460 trace_kvm_check_requests(vcpu);
Alexander Graf63460462012-08-08 00:44:52 +0200461
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 Graf4ffc6352012-08-08 20:31:13 +0200468}
469
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000470/*
471 * Common checks before entering the guest world. Call with interrupts
472 * disabled.
473 *
474 * returns !0 if a signal is pending and check_signal is true
475 */
Alexander Graf03660ba2012-02-28 12:00:41 +0100476static int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000477{
478 int r = 0;
479
480 WARN_ON_ONCE(!irqs_disabled());
481 while (true) {
482 if (need_resched()) {
483 local_irq_enable();
484 cond_resched();
485 local_irq_disable();
486 continue;
487 }
488
Alexander Graf03660ba2012-02-28 12:00:41 +0100489 if (signal_pending(current)) {
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000490 r = 1;
491 break;
492 }
493
Alexander Graf4ffc6352012-08-08 20:31:13 +0200494 smp_mb();
495 if (vcpu->requests) {
496 /* Make sure we process requests preemptable */
497 local_irq_enable();
498 kvmppc_check_requests(vcpu);
499 local_irq_disable();
500 continue;
501 }
502
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000503 if (kvmppc_core_prepare_to_enter(vcpu)) {
504 /* interrupts got enabled in between, so we
505 are back at square 1 */
506 continue;
507 }
508
Alexander Grafd69c6432012-08-08 20:44:20 +0200509 if (vcpu->mode == EXITING_GUEST_MODE) {
510 r = 1;
511 break;
512 }
513
514 /* Going into guest context! Yay! */
515 vcpu->mode = IN_GUEST_MODE;
516 smp_wmb();
517
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000518 break;
519 }
520
521 return r;
Scott Woodc59a6a32011-11-08 18:23:25 -0600522}
523
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000524int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
525{
526 int ret;
Scott Wood8fae8452011-12-20 15:34:45 +0000527#ifdef CONFIG_PPC_FPU
528 unsigned int fpscr;
529 int fpexc_mode;
530 u64 fpr[32];
531#endif
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000532
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200533 if (!vcpu->arch.sane) {
534 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
535 return -EINVAL;
536 }
537
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000538 local_irq_disable();
Alexander Graf03660ba2012-02-28 12:00:41 +0100539 if (kvmppc_prepare_to_enter(vcpu)) {
Scott Wood1d1ef222011-11-08 16:11:59 -0600540 kvm_run->exit_reason = KVM_EXIT_INTR;
541 ret = -EINTR;
542 goto out;
543 }
544
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000545 kvm_guest_enter();
Scott Wood8fae8452011-12-20 15:34:45 +0000546
547#ifdef CONFIG_PPC_FPU
548 /* Save userspace FPU state in stack */
549 enable_kernel_fp();
550 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
551 fpscr = current->thread.fpscr.val;
552 fpexc_mode = current->thread.fpexc_mode;
553
554 /* Restore guest FPU state to thread */
555 memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr));
556 current->thread.fpscr.val = vcpu->arch.fpscr;
557
558 /*
559 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
560 * as always using the FPU. Kernel usage of FP (via
561 * enable_kernel_fp()) in this thread must not occur while
562 * vcpu->fpu_active is set.
563 */
564 vcpu->fpu_active = 1;
565
566 kvmppc_load_guest_fp(vcpu);
567#endif
568
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000569 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
Scott Wood8fae8452011-12-20 15:34:45 +0000570
571#ifdef CONFIG_PPC_FPU
572 kvmppc_save_guest_fp(vcpu);
573
574 vcpu->fpu_active = 0;
575
576 /* Save guest FPU state from thread */
577 memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr));
578 vcpu->arch.fpscr = current->thread.fpscr.val;
579
580 /* Restore userspace FPU state from stack */
581 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
582 current->thread.fpscr.val = fpscr;
583 current->thread.fpexc_mode = fpexc_mode;
584#endif
585
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000586 kvm_guest_exit();
Alexander Graf862d31f2012-07-31 00:19:50 +0200587 vcpu->mode = OUTSIDE_GUEST_MODE;
588 smp_wmb();
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000589
Scott Wood1d1ef222011-11-08 16:11:59 -0600590out:
Alexander Grafd69c6432012-08-08 20:44:20 +0200591 vcpu->mode = OUTSIDE_GUEST_MODE;
592 smp_wmb();
Scott Wood1d1ef222011-11-08 16:11:59 -0600593 local_irq_enable();
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000594 return ret;
595}
596
Scott Woodd30f6e42011-12-20 15:34:43 +0000597static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
598{
599 enum emulation_result er;
600
601 er = kvmppc_emulate_instruction(run, vcpu);
602 switch (er) {
603 case EMULATE_DONE:
604 /* don't overwrite subtypes, just account kvm_stats */
605 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
606 /* Future optimization: only reload non-volatiles if
607 * they were actually modified by emulation. */
608 return RESUME_GUEST_NV;
609
610 case EMULATE_DO_DCR:
611 run->exit_reason = KVM_EXIT_DCR;
612 return RESUME_HOST;
613
614 case EMULATE_FAIL:
Scott Woodd30f6e42011-12-20 15:34:43 +0000615 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
616 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
617 /* For debugging, encode the failing instruction and
618 * report it to userspace. */
619 run->hw.hardware_exit_reason = ~0ULL << 32;
620 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
Alexander Grafd1ff5492012-02-16 13:24:03 +0000621 kvmppc_core_queue_program(vcpu, ESR_PIL);
Scott Woodd30f6e42011-12-20 15:34:43 +0000622 return RESUME_HOST;
623
624 default:
625 BUG();
626 }
627}
628
Alexander Graf4e642cc2012-02-20 23:57:26 +0100629static void kvmppc_fill_pt_regs(struct pt_regs *regs)
630{
631 ulong r1, ip, msr, lr;
632
633 asm("mr %0, 1" : "=r"(r1));
634 asm("mflr %0" : "=r"(lr));
635 asm("mfmsr %0" : "=r"(msr));
636 asm("bl 1f; 1: mflr %0" : "=r"(ip));
637
638 memset(regs, 0, sizeof(*regs));
639 regs->gpr[1] = r1;
640 regs->nip = ip;
641 regs->msr = msr;
642 regs->link = lr;
643}
644
Bharat Bhushan6328e592012-06-20 05:56:53 +0000645/*
646 * For interrupts needed to be handled by host interrupt handlers,
647 * corresponding host handler are called from here in similar way
648 * (but not exact) as they are called from low level handler
649 * (such as from arch/powerpc/kernel/head_fsl_booke.S).
650 */
Alexander Graf4e642cc2012-02-20 23:57:26 +0100651static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
652 unsigned int exit_nr)
653{
654 struct pt_regs regs;
655
656 switch (exit_nr) {
657 case BOOKE_INTERRUPT_EXTERNAL:
658 kvmppc_fill_pt_regs(&regs);
659 do_IRQ(&regs);
660 break;
661 case BOOKE_INTERRUPT_DECREMENTER:
662 kvmppc_fill_pt_regs(&regs);
663 timer_interrupt(&regs);
664 break;
665#if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64)
666 case BOOKE_INTERRUPT_DOORBELL:
667 kvmppc_fill_pt_regs(&regs);
668 doorbell_exception(&regs);
669 break;
670#endif
671 case BOOKE_INTERRUPT_MACHINE_CHECK:
672 /* FIXME */
673 break;
Alexander Graf7cc1e8e2012-02-22 16:26:34 +0100674 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
675 kvmppc_fill_pt_regs(&regs);
676 performance_monitor_exception(&regs);
677 break;
Bharat Bhushan6328e592012-06-20 05:56:53 +0000678 case BOOKE_INTERRUPT_WATCHDOG:
679 kvmppc_fill_pt_regs(&regs);
680#ifdef CONFIG_BOOKE_WDT
681 WatchdogException(&regs);
682#else
683 unknown_exception(&regs);
684#endif
685 break;
686 case BOOKE_INTERRUPT_CRITICAL:
687 unknown_exception(&regs);
688 break;
Alexander Graf4e642cc2012-02-20 23:57:26 +0100689 }
690}
691
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500692/**
693 * kvmppc_handle_exit
694 *
695 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
696 */
697int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
698 unsigned int exit_nr)
699{
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500700 int r = RESUME_HOST;
701
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600702 /* update before a new last_exit_type is rewritten */
703 kvmppc_update_timing_stats(vcpu);
704
Alexander Graf4e642cc2012-02-20 23:57:26 +0100705 /* restart interrupts if they were meant for the host */
706 kvmppc_restart_interrupt(vcpu, exit_nr);
Scott Woodd30f6e42011-12-20 15:34:43 +0000707
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500708 local_irq_enable();
709
Alexander Graf97c95052012-08-02 15:10:00 +0200710 trace_kvm_exit(exit_nr, vcpu);
711
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500712 run->exit_reason = KVM_EXIT_UNKNOWN;
713 run->ready_for_interrupt_injection = 1;
714
715 switch (exit_nr) {
716 case BOOKE_INTERRUPT_MACHINE_CHECK:
Alexander Grafc35c9d82012-02-20 12:21:18 +0100717 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
718 kvmppc_dump_vcpu(vcpu);
719 /* For debugging, send invalid exit reason to user space */
720 run->hw.hardware_exit_reason = ~1ULL << 32;
721 run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
722 r = RESUME_HOST;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500723 break;
724
725 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600726 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -0600727 r = RESUME_GUEST;
728 break;
729
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500730 case BOOKE_INTERRUPT_DECREMENTER:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600731 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500732 r = RESUME_GUEST;
733 break;
734
Bharat Bhushan6328e592012-06-20 05:56:53 +0000735 case BOOKE_INTERRUPT_WATCHDOG:
736 r = RESUME_GUEST;
737 break;
738
Scott Woodd30f6e42011-12-20 15:34:43 +0000739 case BOOKE_INTERRUPT_DOORBELL:
740 kvmppc_account_exit(vcpu, DBELL_EXITS);
Scott Woodd30f6e42011-12-20 15:34:43 +0000741 r = RESUME_GUEST;
742 break;
743
744 case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
745 kvmppc_account_exit(vcpu, GDBELL_EXITS);
746
747 /*
748 * We are here because there is a pending guest interrupt
749 * which could not be delivered as MSR_CE or MSR_ME was not
750 * set. Once we break from here we will retry delivery.
751 */
752 r = RESUME_GUEST;
753 break;
754
755 case BOOKE_INTERRUPT_GUEST_DBELL:
756 kvmppc_account_exit(vcpu, GDBELL_EXITS);
757
758 /*
759 * We are here because there is a pending guest interrupt
760 * which could not be delivered as MSR_EE was not set. Once
761 * we break from here we will retry delivery.
762 */
763 r = RESUME_GUEST;
764 break;
765
Alexander Graf95f2e922012-02-20 22:45:12 +0100766 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
767 r = RESUME_GUEST;
768 break;
769
Scott Woodd30f6e42011-12-20 15:34:43 +0000770 case BOOKE_INTERRUPT_HV_PRIV:
771 r = emulation_exit(run, vcpu);
772 break;
773
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500774 case BOOKE_INTERRUPT_PROGRAM:
Scott Woodd30f6e42011-12-20 15:34:43 +0000775 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
Alexander Graf02685972012-02-20 12:33:22 +0100776 /*
777 * Program traps generated by user-level software must
778 * be handled by the guest kernel.
779 *
780 * In GS mode, hypervisor privileged instructions trap
781 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
782 * actual program interrupts, handled by the guest.
783 */
Liu Yudaf5e272010-02-02 19:44:35 +0800784 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500785 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600786 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500787 break;
788 }
789
Scott Woodd30f6e42011-12-20 15:34:43 +0000790 r = emulation_exit(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500791 break;
792
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200793 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600794 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600795 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200796 r = RESUME_GUEST;
797 break;
798
Scott Wood4cd35f62011-06-14 18:34:31 -0500799#ifdef CONFIG_SPE
800 case BOOKE_INTERRUPT_SPE_UNAVAIL: {
801 if (vcpu->arch.shared->msr & MSR_SPE)
802 kvmppc_vcpu_enable_spe(vcpu);
803 else
804 kvmppc_booke_queue_irqprio(vcpu,
805 BOOKE_IRQPRIO_SPE_UNAVAIL);
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600806 r = RESUME_GUEST;
807 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500808 }
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600809
810 case BOOKE_INTERRUPT_SPE_FP_DATA:
811 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
812 r = RESUME_GUEST;
813 break;
814
815 case BOOKE_INTERRUPT_SPE_FP_ROUND:
816 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
817 r = RESUME_GUEST;
818 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500819#else
820 case BOOKE_INTERRUPT_SPE_UNAVAIL:
821 /*
822 * Guest wants SPE, but host kernel doesn't support it. Send
823 * an "unimplemented operation" program check to the guest.
824 */
825 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
826 r = RESUME_GUEST;
827 break;
828
829 /*
830 * These really should never happen without CONFIG_SPE,
831 * as we should never enable the real MSR[SPE] in the guest.
832 */
833 case BOOKE_INTERRUPT_SPE_FP_DATA:
834 case BOOKE_INTERRUPT_SPE_FP_ROUND:
835 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
836 __func__, exit_nr, vcpu->arch.pc);
837 run->hw.hardware_exit_reason = exit_nr;
838 r = RESUME_HOST;
839 break;
840#endif
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600841
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500842 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800843 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
844 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600845 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500846 r = RESUME_GUEST;
847 break;
848
849 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800850 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600851 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500852 r = RESUME_GUEST;
853 break;
854
Scott Woodd30f6e42011-12-20 15:34:43 +0000855#ifdef CONFIG_KVM_BOOKE_HV
856 case BOOKE_INTERRUPT_HV_SYSCALL:
857 if (!(vcpu->arch.shared->msr & MSR_PR)) {
858 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
859 } else {
860 /*
861 * hcall from guest userspace -- send privileged
862 * instruction program check.
863 */
864 kvmppc_core_queue_program(vcpu, ESR_PPR);
865 }
866
867 r = RESUME_GUEST;
868 break;
869#else
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500870 case BOOKE_INTERRUPT_SYSCALL:
Alexander Graf2a342ed2010-07-29 14:47:48 +0200871 if (!(vcpu->arch.shared->msr & MSR_PR) &&
872 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
873 /* KVM PV hypercalls */
874 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
875 r = RESUME_GUEST;
876 } else {
877 /* Guest syscalls */
878 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
879 }
Hollis Blanchard7b701592008-12-02 15:51:58 -0600880 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500881 r = RESUME_GUEST;
882 break;
Scott Woodd30f6e42011-12-20 15:34:43 +0000883#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500884
885 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500886 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600887 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600888 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500889 gfn_t gfn;
890
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000891#ifdef CONFIG_KVM_E500V2
Scott Wooda4cd8b22011-06-14 18:34:41 -0500892 if (!(vcpu->arch.shared->msr & MSR_PR) &&
893 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
894 kvmppc_map_magic(vcpu);
895 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
896 r = RESUME_GUEST;
897
898 break;
899 }
900#endif
901
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500902 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600903 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600904 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500905 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +0800906 kvmppc_core_queue_dtlb_miss(vcpu,
907 vcpu->arch.fault_dear,
908 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600909 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600910 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500911 r = RESUME_GUEST;
912 break;
913 }
914
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600915 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600916 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500917
918 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
919 /* The guest TLB had a mapping, but the shadow TLB
920 * didn't, and it is RAM. This could be because:
921 * a) the entry is mapping the host kernel, or
922 * b) the guest used a large mapping which we're faking
923 * Either way, we need to satisfy the fault without
924 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600925 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600926 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500927 r = RESUME_GUEST;
928 } else {
929 /* Guest has mapped and accessed a page which is not
930 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600931 vcpu->arch.paddr_accessed = gpaddr;
Alexander Graf6020c0f2012-03-12 02:26:30 +0100932 vcpu->arch.vaddr_accessed = eaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500933 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600934 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500935 }
936
937 break;
938 }
939
940 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500941 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -0600942 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500943 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600944 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500945
946 r = RESUME_GUEST;
947
948 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600949 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600950 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500951 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600952 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600953 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600954 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500955 break;
956 }
957
Hollis Blanchard7b701592008-12-02 15:51:58 -0600958 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500959
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600960 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -0600961 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500962
963 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
964 /* The guest TLB had a mapping, but the shadow TLB
965 * didn't. This could be because:
966 * a) the entry is mapping the host kernel, or
967 * b) the guest used a large mapping which we're faking
968 * Either way, we need to satisfy the fault without
969 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600970 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500971 } else {
972 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600973 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500974 }
975
976 break;
977 }
978
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500979 case BOOKE_INTERRUPT_DEBUG: {
980 u32 dbsr;
981
982 vcpu->arch.pc = mfspr(SPRN_CSRR0);
983
984 /* clear IAC events in DBSR register */
985 dbsr = mfspr(SPRN_DBSR);
986 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
987 mtspr(SPRN_DBSR, dbsr);
988
989 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600990 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500991 r = RESUME_HOST;
992 break;
993 }
994
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500995 default:
996 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
997 BUG();
998 }
999
Alexander Grafa8e4ef82012-02-16 14:07:37 +00001000 /*
1001 * To avoid clobbering exit_reason, only check for signals if we
1002 * aren't already exiting to userspace for some other reason.
1003 */
Alexander Graf03660ba2012-02-28 12:00:41 +01001004 if (!(r & RESUME_HOST)) {
1005 local_irq_disable();
1006 if (kvmppc_prepare_to_enter(vcpu)) {
1007 run->exit_reason = KVM_EXIT_INTR;
1008 r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
1009 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
1010 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001011 }
1012
1013 return r;
1014}
1015
1016/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
1017int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1018{
Hollis Blanchard082decf2010-08-07 10:33:56 -07001019 int i;
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001020 int r;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001021
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001022 vcpu->arch.pc = 0;
Scott Woodb5904972011-11-08 18:23:30 -06001023 vcpu->arch.shared->pir = vcpu->vcpu_id;
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001024 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Scott Woodd30f6e42011-12-20 15:34:43 +00001025 kvmppc_set_msr(vcpu, 0);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001026
Scott Woodd30f6e42011-12-20 15:34:43 +00001027#ifndef CONFIG_KVM_BOOKE_HV
1028 vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001029 vcpu->arch.shadow_pid = 1;
Scott Woodd30f6e42011-12-20 15:34:43 +00001030 vcpu->arch.shared->msr = 0;
1031#endif
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001032
Hollis Blanchard082decf2010-08-07 10:33:56 -07001033 /* Eye-catching numbers so we know if the guest takes an interrupt
1034 * before it's programmed its own IVPR/IVORs. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001035 vcpu->arch.ivpr = 0x55550000;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001036 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
1037 vcpu->arch.ivor[i] = 0x7700 | i * 4;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001038
Hollis Blanchard73e75b42008-12-02 15:51:57 -06001039 kvmppc_init_timing_stats(vcpu);
1040
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001041 r = kvmppc_core_vcpu_setup(vcpu);
1042 kvmppc_sanity_check(vcpu);
1043 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001044}
1045
1046int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1047{
1048 int i;
1049
1050 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001051 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001052 regs->ctr = vcpu->arch.ctr;
1053 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001054 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001055 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +02001056 regs->srr0 = vcpu->arch.shared->srr0;
1057 regs->srr1 = vcpu->arch.shared->srr1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001058 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001059 regs->sprg0 = vcpu->arch.shared->sprg0;
1060 regs->sprg1 = vcpu->arch.shared->sprg1;
1061 regs->sprg2 = vcpu->arch.shared->sprg2;
1062 regs->sprg3 = vcpu->arch.shared->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -06001063 regs->sprg4 = vcpu->arch.shared->sprg4;
1064 regs->sprg5 = vcpu->arch.shared->sprg5;
1065 regs->sprg6 = vcpu->arch.shared->sprg6;
1066 regs->sprg7 = vcpu->arch.shared->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001067
1068 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001069 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001070
1071 return 0;
1072}
1073
1074int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1075{
1076 int i;
1077
1078 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001079 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001080 vcpu->arch.ctr = regs->ctr;
1081 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001082 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -06001083 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +02001084 vcpu->arch.shared->srr0 = regs->srr0;
1085 vcpu->arch.shared->srr1 = regs->srr1;
Scott Wood5ce941e2011-04-27 17:24:21 -05001086 kvmppc_set_pid(vcpu, regs->pid);
Alexander Grafa73a9592010-07-29 14:47:47 +02001087 vcpu->arch.shared->sprg0 = regs->sprg0;
1088 vcpu->arch.shared->sprg1 = regs->sprg1;
1089 vcpu->arch.shared->sprg2 = regs->sprg2;
1090 vcpu->arch.shared->sprg3 = regs->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -06001091 vcpu->arch.shared->sprg4 = regs->sprg4;
1092 vcpu->arch.shared->sprg5 = regs->sprg5;
1093 vcpu->arch.shared->sprg6 = regs->sprg6;
1094 vcpu->arch.shared->sprg7 = regs->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001095
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001096 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1097 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001098
1099 return 0;
1100}
1101
Scott Wood5ce941e2011-04-27 17:24:21 -05001102static void get_sregs_base(struct kvm_vcpu *vcpu,
1103 struct kvm_sregs *sregs)
1104{
1105 u64 tb = get_tb();
1106
1107 sregs->u.e.features |= KVM_SREGS_E_BASE;
1108
1109 sregs->u.e.csrr0 = vcpu->arch.csrr0;
1110 sregs->u.e.csrr1 = vcpu->arch.csrr1;
1111 sregs->u.e.mcsr = vcpu->arch.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +00001112 sregs->u.e.esr = get_guest_esr(vcpu);
1113 sregs->u.e.dear = get_guest_dear(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001114 sregs->u.e.tsr = vcpu->arch.tsr;
1115 sregs->u.e.tcr = vcpu->arch.tcr;
1116 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
1117 sregs->u.e.tb = tb;
1118 sregs->u.e.vrsave = vcpu->arch.vrsave;
1119}
1120
1121static int set_sregs_base(struct kvm_vcpu *vcpu,
1122 struct kvm_sregs *sregs)
1123{
1124 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
1125 return 0;
1126
1127 vcpu->arch.csrr0 = sregs->u.e.csrr0;
1128 vcpu->arch.csrr1 = sregs->u.e.csrr1;
1129 vcpu->arch.mcsr = sregs->u.e.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +00001130 set_guest_esr(vcpu, sregs->u.e.esr);
1131 set_guest_dear(vcpu, sregs->u.e.dear);
Scott Wood5ce941e2011-04-27 17:24:21 -05001132 vcpu->arch.vrsave = sregs->u.e.vrsave;
Scott Wooddfd4d472011-11-17 12:39:59 +00001133 kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001134
Scott Wooddfd4d472011-11-17 12:39:59 +00001135 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
Scott Wood5ce941e2011-04-27 17:24:21 -05001136 vcpu->arch.dec = sregs->u.e.dec;
Scott Wooddfd4d472011-11-17 12:39:59 +00001137 kvmppc_emulate_dec(vcpu);
1138 }
Scott Wood5ce941e2011-04-27 17:24:21 -05001139
1140 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
Scott Wooddfd4d472011-11-17 12:39:59 +00001141 vcpu->arch.tsr = sregs->u.e.tsr;
1142 update_timer_ints(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001143 }
1144
1145 return 0;
1146}
1147
1148static void get_sregs_arch206(struct kvm_vcpu *vcpu,
1149 struct kvm_sregs *sregs)
1150{
1151 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
1152
Scott Wood841741f2011-09-02 17:39:37 -05001153 sregs->u.e.pir = vcpu->vcpu_id;
Scott Wood5ce941e2011-04-27 17:24:21 -05001154 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
1155 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
1156 sregs->u.e.decar = vcpu->arch.decar;
1157 sregs->u.e.ivpr = vcpu->arch.ivpr;
1158}
1159
1160static int set_sregs_arch206(struct kvm_vcpu *vcpu,
1161 struct kvm_sregs *sregs)
1162{
1163 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
1164 return 0;
1165
Scott Wood841741f2011-09-02 17:39:37 -05001166 if (sregs->u.e.pir != vcpu->vcpu_id)
Scott Wood5ce941e2011-04-27 17:24:21 -05001167 return -EINVAL;
1168
1169 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
1170 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
1171 vcpu->arch.decar = sregs->u.e.decar;
1172 vcpu->arch.ivpr = sregs->u.e.ivpr;
1173
1174 return 0;
1175}
1176
1177void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1178{
1179 sregs->u.e.features |= KVM_SREGS_E_IVOR;
1180
1181 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
1182 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
1183 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
1184 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
1185 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
1186 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
1187 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
1188 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
1189 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
1190 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
1191 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
1192 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
1193 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
1194 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
1195 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
1196 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
1197}
1198
1199int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1200{
1201 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
1202 return 0;
1203
1204 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
1205 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
1206 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
1207 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
1208 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
1209 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
1210 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
1211 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
1212 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
1213 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
1214 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
1215 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
1216 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
1217 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
1218 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
1219 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
1220
1221 return 0;
1222}
1223
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001224int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1225 struct kvm_sregs *sregs)
1226{
Scott Wood5ce941e2011-04-27 17:24:21 -05001227 sregs->pvr = vcpu->arch.pvr;
1228
1229 get_sregs_base(vcpu, sregs);
1230 get_sregs_arch206(vcpu, sregs);
1231 kvmppc_core_get_sregs(vcpu, sregs);
1232 return 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001233}
1234
1235int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1236 struct kvm_sregs *sregs)
1237{
Scott Wood5ce941e2011-04-27 17:24:21 -05001238 int ret;
1239
1240 if (vcpu->arch.pvr != sregs->pvr)
1241 return -EINVAL;
1242
1243 ret = set_sregs_base(vcpu, sregs);
1244 if (ret < 0)
1245 return ret;
1246
1247 ret = set_sregs_arch206(vcpu, sregs);
1248 if (ret < 0)
1249 return ret;
1250
1251 return kvmppc_core_set_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001252}
1253
Paul Mackerras31f34382011-12-12 12:26:50 +00001254int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1255{
1256 return -EINVAL;
1257}
1258
1259int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1260{
1261 return -EINVAL;
1262}
1263
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001264int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1265{
1266 return -ENOTSUPP;
1267}
1268
1269int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1270{
1271 return -ENOTSUPP;
1272}
1273
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001274int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1275 struct kvm_translation *tr)
1276{
Avi Kivity98001d82010-05-13 11:05:49 +03001277 int r;
1278
Avi Kivity98001d82010-05-13 11:05:49 +03001279 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +03001280 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001281}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001282
Alexander Graf4e755752009-10-30 05:47:01 +00001283int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1284{
1285 return -ENOTSUPP;
1286}
1287
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001288int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1289 struct kvm_userspace_memory_region *mem)
1290{
1291 return 0;
1292}
1293
1294void kvmppc_core_commit_memory_region(struct kvm *kvm,
1295 struct kvm_userspace_memory_region *mem)
1296{
1297}
1298
Scott Wooddfd4d472011-11-17 12:39:59 +00001299void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1300{
1301 vcpu->arch.tcr = new_tcr;
1302 update_timer_ints(vcpu);
1303}
1304
1305void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1306{
1307 set_bits(tsr_bits, &vcpu->arch.tsr);
1308 smp_wmb();
1309 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1310 kvm_vcpu_kick(vcpu);
1311}
1312
1313void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1314{
1315 clear_bits(tsr_bits, &vcpu->arch.tsr);
1316 update_timer_ints(vcpu);
1317}
1318
1319void kvmppc_decrementer_func(unsigned long data)
1320{
1321 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1322
Bharat Bhushan21bd0002012-05-20 23:21:23 +00001323 if (vcpu->arch.tcr & TCR_ARE) {
1324 vcpu->arch.dec = vcpu->arch.decar;
1325 kvmppc_emulate_dec(vcpu);
1326 }
1327
Scott Wooddfd4d472011-11-17 12:39:59 +00001328 kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1329}
1330
Scott Wood94fa9d92011-12-20 15:34:22 +00001331void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1332{
Scott Woodd30f6e42011-12-20 15:34:43 +00001333 current->thread.kvm_vcpu = vcpu;
Scott Wood94fa9d92011-12-20 15:34:22 +00001334}
1335
1336void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
1337{
Scott Woodd30f6e42011-12-20 15:34:43 +00001338 current->thread.kvm_vcpu = NULL;
Scott Wood94fa9d92011-12-20 15:34:22 +00001339}
1340
Stephen Rothwell2986b8c2009-06-02 11:46:14 +10001341int __init kvmppc_booke_init(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001342{
Scott Woodd30f6e42011-12-20 15:34:43 +00001343#ifndef CONFIG_KVM_BOOKE_HV
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001344 unsigned long ivor[16];
1345 unsigned long max_ivor = 0;
1346 int i;
1347
1348 /* We install our own exception handlers by hijacking IVPR. IVPR must
1349 * be 16-bit aligned, so we need a 64KB allocation. */
1350 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1351 VCPU_SIZE_ORDER);
1352 if (!kvmppc_booke_handlers)
1353 return -ENOMEM;
1354
1355 /* XXX make sure our handlers are smaller than Linux's */
1356
1357 /* Copy our interrupt handlers to match host IVORs. That way we don't
1358 * have to swap the IVORs on every guest/host transition. */
1359 ivor[0] = mfspr(SPRN_IVOR0);
1360 ivor[1] = mfspr(SPRN_IVOR1);
1361 ivor[2] = mfspr(SPRN_IVOR2);
1362 ivor[3] = mfspr(SPRN_IVOR3);
1363 ivor[4] = mfspr(SPRN_IVOR4);
1364 ivor[5] = mfspr(SPRN_IVOR5);
1365 ivor[6] = mfspr(SPRN_IVOR6);
1366 ivor[7] = mfspr(SPRN_IVOR7);
1367 ivor[8] = mfspr(SPRN_IVOR8);
1368 ivor[9] = mfspr(SPRN_IVOR9);
1369 ivor[10] = mfspr(SPRN_IVOR10);
1370 ivor[11] = mfspr(SPRN_IVOR11);
1371 ivor[12] = mfspr(SPRN_IVOR12);
1372 ivor[13] = mfspr(SPRN_IVOR13);
1373 ivor[14] = mfspr(SPRN_IVOR14);
1374 ivor[15] = mfspr(SPRN_IVOR15);
1375
1376 for (i = 0; i < 16; i++) {
1377 if (ivor[i] > max_ivor)
1378 max_ivor = ivor[i];
1379
1380 memcpy((void *)kvmppc_booke_handlers + ivor[i],
1381 kvmppc_handlers_start + i * kvmppc_handler_len,
1382 kvmppc_handler_len);
1383 }
1384 flush_icache_range(kvmppc_booke_handlers,
1385 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
Scott Woodd30f6e42011-12-20 15:34:43 +00001386#endif /* !BOOKE_HV */
Hollis Blancharddb93f572008-11-05 09:36:18 -06001387 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001388}
1389
Hollis Blancharddb93f572008-11-05 09:36:18 -06001390void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001391{
1392 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
1393 kvm_exit();
1394}