blob: 7ce2ed07831faff60c663a6cb37c0f5c8512d869 [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) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050066 { NULL }
67};
68
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050069/* TODO: use vcpu_printf() */
70void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
71{
72 int i;
73
Alexander Graf666e7252010-07-29 14:47:43 +020074 printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060075 printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
Alexander Grafde7906c2010-07-29 14:47:46 +020076 printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
77 vcpu->arch.shared->srr1);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050078
79 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
80
81 for (i = 0; i < 32; i += 4) {
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060082 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
Alexander Graf8e5b26b2010-01-08 02:58:01 +010083 kvmppc_get_gpr(vcpu, i),
84 kvmppc_get_gpr(vcpu, i+1),
85 kvmppc_get_gpr(vcpu, i+2),
86 kvmppc_get_gpr(vcpu, i+3));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050087 }
88}
89
Scott Wood4cd35f62011-06-14 18:34:31 -050090#ifdef CONFIG_SPE
91void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
92{
93 preempt_disable();
94 enable_kernel_spe();
95 kvmppc_save_guest_spe(vcpu);
96 vcpu->arch.shadow_msr &= ~MSR_SPE;
97 preempt_enable();
98}
99
100static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
101{
102 preempt_disable();
103 enable_kernel_spe();
104 kvmppc_load_guest_spe(vcpu);
105 vcpu->arch.shadow_msr |= MSR_SPE;
106 preempt_enable();
107}
108
109static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
110{
111 if (vcpu->arch.shared->msr & MSR_SPE) {
112 if (!(vcpu->arch.shadow_msr & MSR_SPE))
113 kvmppc_vcpu_enable_spe(vcpu);
114 } else if (vcpu->arch.shadow_msr & MSR_SPE) {
115 kvmppc_vcpu_disable_spe(vcpu);
116 }
117}
118#else
119static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
120{
121}
122#endif
123
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500124/*
125 * Helper function for "full" MSR writes. No need to call this if only
126 * EE/CE/ME/DE/RI are changing.
127 */
Scott Wood4cd35f62011-06-14 18:34:31 -0500128void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
129{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500130 u32 old_msr = vcpu->arch.shared->msr;
Scott Wood4cd35f62011-06-14 18:34:31 -0500131
Scott Woodd30f6e42011-12-20 15:34:43 +0000132#ifdef CONFIG_KVM_BOOKE_HV
133 new_msr |= MSR_GS;
134#endif
135
Scott Wood4cd35f62011-06-14 18:34:31 -0500136 vcpu->arch.shared->msr = new_msr;
137
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500138 kvmppc_mmu_msr_notify(vcpu, old_msr);
Scott Wood4cd35f62011-06-14 18:34:31 -0500139 kvmppc_vcpu_sync_spe(vcpu);
140}
141
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600142static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
143 unsigned int priority)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600144{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600145 set_bit(priority, &vcpu->arch.pending_exceptions);
146}
147
Liu Yudaf5e272010-02-02 19:44:35 +0800148static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
149 ulong dear_flags, ulong esr_flags)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600150{
Liu Yudaf5e272010-02-02 19:44:35 +0800151 vcpu->arch.queued_dear = dear_flags;
152 vcpu->arch.queued_esr = esr_flags;
153 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
154}
155
156static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
157 ulong dear_flags, ulong esr_flags)
158{
159 vcpu->arch.queued_dear = dear_flags;
160 vcpu->arch.queued_esr = esr_flags;
161 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
162}
163
164static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
165 ulong esr_flags)
166{
167 vcpu->arch.queued_esr = esr_flags;
168 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
169}
170
171void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
172{
173 vcpu->arch.queued_esr = esr_flags;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600174 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600175}
176
177void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
178{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600179 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600180}
181
182int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
183{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600184 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600185}
186
Alexander Graf7706664d2009-12-21 20:21:24 +0100187void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
188{
189 clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
190}
191
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600192void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
193 struct kvm_interrupt *irq)
194{
Alexander Grafc5335f12010-08-30 14:03:24 +0200195 unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
196
197 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
198 prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
199
200 kvmppc_booke_queue_irqprio(vcpu, prio);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600201}
202
Alexander Graf4496f972010-04-07 10:03:25 +0200203void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
204 struct kvm_interrupt *irq)
205{
206 clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
Alexander Grafc5335f12010-08-30 14:03:24 +0200207 clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
Alexander Graf4496f972010-04-07 10:03:25 +0200208}
209
Scott Woodd30f6e42011-12-20 15:34:43 +0000210static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
211{
212#ifdef CONFIG_KVM_BOOKE_HV
213 mtspr(SPRN_GSRR0, srr0);
214 mtspr(SPRN_GSRR1, srr1);
215#else
216 vcpu->arch.shared->srr0 = srr0;
217 vcpu->arch.shared->srr1 = srr1;
218#endif
219}
220
221static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
222{
223 vcpu->arch.csrr0 = srr0;
224 vcpu->arch.csrr1 = srr1;
225}
226
227static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
228{
229 if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
230 vcpu->arch.dsrr0 = srr0;
231 vcpu->arch.dsrr1 = srr1;
232 } else {
233 set_guest_csrr(vcpu, srr0, srr1);
234 }
235}
236
237static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
238{
239 vcpu->arch.mcsrr0 = srr0;
240 vcpu->arch.mcsrr1 = srr1;
241}
242
243static unsigned long get_guest_dear(struct kvm_vcpu *vcpu)
244{
245#ifdef CONFIG_KVM_BOOKE_HV
246 return mfspr(SPRN_GDEAR);
247#else
248 return vcpu->arch.shared->dar;
249#endif
250}
251
252static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear)
253{
254#ifdef CONFIG_KVM_BOOKE_HV
255 mtspr(SPRN_GDEAR, dear);
256#else
257 vcpu->arch.shared->dar = dear;
258#endif
259}
260
261static unsigned long get_guest_esr(struct kvm_vcpu *vcpu)
262{
263#ifdef CONFIG_KVM_BOOKE_HV
264 return mfspr(SPRN_GESR);
265#else
266 return vcpu->arch.shared->esr;
267#endif
268}
269
270static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr)
271{
272#ifdef CONFIG_KVM_BOOKE_HV
273 mtspr(SPRN_GESR, esr);
274#else
275 vcpu->arch.shared->esr = esr;
276#endif
277}
278
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600279/* Deliver the interrupt of the corresponding priority, if possible. */
280static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
281 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500282{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600283 int allowed = 0;
Alexander Graf79300f82012-02-15 19:12:29 +0000284 ulong msr_mask = 0;
Liu Yudaf5e272010-02-02 19:44:35 +0800285 bool update_esr = false, update_dear = false;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200286 ulong crit_raw = vcpu->arch.shared->critical;
287 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
288 bool crit;
Alexander Grafc5335f12010-08-30 14:03:24 +0200289 bool keep_irq = false;
Scott Woodd30f6e42011-12-20 15:34:43 +0000290 enum int_class int_class;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200291
292 /* Truncate crit indicators in 32 bit mode */
293 if (!(vcpu->arch.shared->msr & MSR_SF)) {
294 crit_raw &= 0xffffffff;
295 crit_r1 &= 0xffffffff;
296 }
297
298 /* Critical section when crit == r1 */
299 crit = (crit_raw == crit_r1);
300 /* ... and we're in supervisor mode */
301 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500302
Alexander Grafc5335f12010-08-30 14:03:24 +0200303 if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
304 priority = BOOKE_IRQPRIO_EXTERNAL;
305 keep_irq = true;
306 }
307
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600308 switch (priority) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600309 case BOOKE_IRQPRIO_DTLB_MISS:
Liu Yudaf5e272010-02-02 19:44:35 +0800310 case BOOKE_IRQPRIO_DATA_STORAGE:
311 update_dear = true;
312 /* fall through */
313 case BOOKE_IRQPRIO_INST_STORAGE:
314 case BOOKE_IRQPRIO_PROGRAM:
315 update_esr = true;
316 /* fall through */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600317 case BOOKE_IRQPRIO_ITLB_MISS:
318 case BOOKE_IRQPRIO_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600319 case BOOKE_IRQPRIO_FP_UNAVAIL:
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600320 case BOOKE_IRQPRIO_SPE_UNAVAIL:
321 case BOOKE_IRQPRIO_SPE_FP_DATA:
322 case BOOKE_IRQPRIO_SPE_FP_ROUND:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600323 case BOOKE_IRQPRIO_AP_UNAVAIL:
324 case BOOKE_IRQPRIO_ALIGNMENT:
325 allowed = 1;
Alexander Graf79300f82012-02-15 19:12:29 +0000326 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000327 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500328 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600329 case BOOKE_IRQPRIO_CRITICAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000330 case BOOKE_IRQPRIO_DBELL_CRIT:
Alexander Graf666e7252010-07-29 14:47:43 +0200331 allowed = vcpu->arch.shared->msr & MSR_CE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000332 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000333 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000334 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500335 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600336 case BOOKE_IRQPRIO_MACHINE_CHECK:
Alexander Graf666e7252010-07-29 14:47:43 +0200337 allowed = vcpu->arch.shared->msr & MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000338 allowed = allowed && !crit;
Scott Woodd30f6e42011-12-20 15:34:43 +0000339 int_class = INT_CLASS_MC;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500340 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600341 case BOOKE_IRQPRIO_DECREMENTER:
342 case BOOKE_IRQPRIO_FIT:
Scott Wooddfd4d472011-11-17 12:39:59 +0000343 keep_irq = true;
344 /* fall through */
345 case BOOKE_IRQPRIO_EXTERNAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000346 case BOOKE_IRQPRIO_DBELL:
Alexander Graf666e7252010-07-29 14:47:43 +0200347 allowed = vcpu->arch.shared->msr & MSR_EE;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200348 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000349 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000350 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500351 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600352 case BOOKE_IRQPRIO_DEBUG:
Alexander Graf666e7252010-07-29 14:47:43 +0200353 allowed = vcpu->arch.shared->msr & MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000354 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000355 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000356 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500357 break;
358 }
359
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600360 if (allowed) {
Scott Woodd30f6e42011-12-20 15:34:43 +0000361 switch (int_class) {
362 case INT_CLASS_NONCRIT:
363 set_guest_srr(vcpu, vcpu->arch.pc,
364 vcpu->arch.shared->msr);
365 break;
366 case INT_CLASS_CRIT:
367 set_guest_csrr(vcpu, vcpu->arch.pc,
368 vcpu->arch.shared->msr);
369 break;
370 case INT_CLASS_DBG:
371 set_guest_dsrr(vcpu, vcpu->arch.pc,
372 vcpu->arch.shared->msr);
373 break;
374 case INT_CLASS_MC:
375 set_guest_mcsrr(vcpu, vcpu->arch.pc,
376 vcpu->arch.shared->msr);
377 break;
378 }
379
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600380 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
Liu Yudaf5e272010-02-02 19:44:35 +0800381 if (update_esr == true)
Scott Woodd30f6e42011-12-20 15:34:43 +0000382 set_guest_esr(vcpu, vcpu->arch.queued_esr);
Liu Yudaf5e272010-02-02 19:44:35 +0800383 if (update_dear == true)
Scott Woodd30f6e42011-12-20 15:34:43 +0000384 set_guest_dear(vcpu, vcpu->arch.queued_dear);
Alexander Graf666e7252010-07-29 14:47:43 +0200385 kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600386
Alexander Grafc5335f12010-08-30 14:03:24 +0200387 if (!keep_irq)
388 clear_bit(priority, &vcpu->arch.pending_exceptions);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600389 }
390
Scott Woodd30f6e42011-12-20 15:34:43 +0000391#ifdef CONFIG_KVM_BOOKE_HV
392 /*
393 * If an interrupt is pending but masked, raise a guest doorbell
394 * so that we are notified when the guest enables the relevant
395 * MSR bit.
396 */
397 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
398 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
399 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
400 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
401 if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
402 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
403#endif
404
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600405 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500406}
407
Scott Wooddfd4d472011-11-17 12:39:59 +0000408static void update_timer_ints(struct kvm_vcpu *vcpu)
409{
410 if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
411 kvmppc_core_queue_dec(vcpu);
412 else
413 kvmppc_core_dequeue_dec(vcpu);
414}
415
Scott Woodc59a6a32011-11-08 18:23:25 -0600416static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500417{
418 unsigned long *pending = &vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500419 unsigned int priority;
420
Scott Wooddfd4d472011-11-17 12:39:59 +0000421 if (vcpu->requests) {
422 if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu)) {
423 smp_mb();
424 update_timer_ints(vcpu);
425 }
426 }
427
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600428 priority = __ffs(*pending);
Alexander Graf8b3a00f2012-02-16 14:12:46 +0000429 while (priority < BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600430 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500431 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500432
433 priority = find_next_bit(pending,
434 BITS_PER_BYTE * sizeof(*pending),
435 priority + 1);
436 }
Alexander Graf90bba352010-07-29 14:47:51 +0200437
438 /* Tell the guest about our interrupt status */
Scott Wood29ac26e2011-11-08 18:23:27 -0600439 vcpu->arch.shared->int_pending = !!*pending;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500440}
441
Scott Woodc59a6a32011-11-08 18:23:25 -0600442/* Check pending exceptions and deliver one, if possible. */
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000443int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
Scott Woodc59a6a32011-11-08 18:23:25 -0600444{
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000445 int r = 0;
Scott Woodc59a6a32011-11-08 18:23:25 -0600446 WARN_ON_ONCE(!irqs_disabled());
447
448 kvmppc_core_check_exceptions(vcpu);
449
450 if (vcpu->arch.shared->msr & MSR_WE) {
451 local_irq_enable();
452 kvm_vcpu_block(vcpu);
Alexander Graf966cd0f2012-03-14 16:55:08 +0100453 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Scott Woodc59a6a32011-11-08 18:23:25 -0600454 local_irq_disable();
455
456 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000457 r = 1;
Scott Woodc59a6a32011-11-08 18:23:25 -0600458 };
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000459
460 return r;
461}
462
463/*
464 * Common checks before entering the guest world. Call with interrupts
465 * disabled.
466 *
467 * returns !0 if a signal is pending and check_signal is true
468 */
Alexander Graf03660ba2012-02-28 12:00:41 +0100469static int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000470{
471 int r = 0;
472
473 WARN_ON_ONCE(!irqs_disabled());
474 while (true) {
475 if (need_resched()) {
476 local_irq_enable();
477 cond_resched();
478 local_irq_disable();
479 continue;
480 }
481
Alexander Graf03660ba2012-02-28 12:00:41 +0100482 if (signal_pending(current)) {
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000483 r = 1;
484 break;
485 }
486
487 if (kvmppc_core_prepare_to_enter(vcpu)) {
488 /* interrupts got enabled in between, so we
489 are back at square 1 */
490 continue;
491 }
492
493 break;
494 }
495
496 return r;
Scott Woodc59a6a32011-11-08 18:23:25 -0600497}
498
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000499int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
500{
501 int ret;
Scott Wood8fae8452011-12-20 15:34:45 +0000502#ifdef CONFIG_PPC_FPU
503 unsigned int fpscr;
504 int fpexc_mode;
505 u64 fpr[32];
506#endif
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000507
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200508 if (!vcpu->arch.sane) {
509 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
510 return -EINVAL;
511 }
512
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000513 local_irq_disable();
Alexander Graf03660ba2012-02-28 12:00:41 +0100514 if (kvmppc_prepare_to_enter(vcpu)) {
Scott Wood1d1ef222011-11-08 16:11:59 -0600515 kvm_run->exit_reason = KVM_EXIT_INTR;
516 ret = -EINTR;
517 goto out;
518 }
519
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000520 kvm_guest_enter();
Scott Wood8fae8452011-12-20 15:34:45 +0000521
522#ifdef CONFIG_PPC_FPU
523 /* Save userspace FPU state in stack */
524 enable_kernel_fp();
525 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
526 fpscr = current->thread.fpscr.val;
527 fpexc_mode = current->thread.fpexc_mode;
528
529 /* Restore guest FPU state to thread */
530 memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr));
531 current->thread.fpscr.val = vcpu->arch.fpscr;
532
533 /*
534 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
535 * as always using the FPU. Kernel usage of FP (via
536 * enable_kernel_fp()) in this thread must not occur while
537 * vcpu->fpu_active is set.
538 */
539 vcpu->fpu_active = 1;
540
541 kvmppc_load_guest_fp(vcpu);
542#endif
543
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000544 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
Scott Wood8fae8452011-12-20 15:34:45 +0000545
546#ifdef CONFIG_PPC_FPU
547 kvmppc_save_guest_fp(vcpu);
548
549 vcpu->fpu_active = 0;
550
551 /* Save guest FPU state from thread */
552 memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr));
553 vcpu->arch.fpscr = current->thread.fpscr.val;
554
555 /* Restore userspace FPU state from stack */
556 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
557 current->thread.fpscr.val = fpscr;
558 current->thread.fpexc_mode = fpexc_mode;
559#endif
560
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000561 kvm_guest_exit();
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000562
Scott Wood1d1ef222011-11-08 16:11:59 -0600563out:
564 local_irq_enable();
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000565 return ret;
566}
567
Scott Woodd30f6e42011-12-20 15:34:43 +0000568static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
569{
570 enum emulation_result er;
571
572 er = kvmppc_emulate_instruction(run, vcpu);
573 switch (er) {
574 case EMULATE_DONE:
575 /* don't overwrite subtypes, just account kvm_stats */
576 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
577 /* Future optimization: only reload non-volatiles if
578 * they were actually modified by emulation. */
579 return RESUME_GUEST_NV;
580
581 case EMULATE_DO_DCR:
582 run->exit_reason = KVM_EXIT_DCR;
583 return RESUME_HOST;
584
585 case EMULATE_FAIL:
Scott Woodd30f6e42011-12-20 15:34:43 +0000586 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
587 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
588 /* For debugging, encode the failing instruction and
589 * report it to userspace. */
590 run->hw.hardware_exit_reason = ~0ULL << 32;
591 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
Alexander Grafd1ff5492012-02-16 13:24:03 +0000592 kvmppc_core_queue_program(vcpu, ESR_PIL);
Scott Woodd30f6e42011-12-20 15:34:43 +0000593 return RESUME_HOST;
594
595 default:
596 BUG();
597 }
598}
599
Alexander Graf4e642cc2012-02-20 23:57:26 +0100600static void kvmppc_fill_pt_regs(struct pt_regs *regs)
601{
602 ulong r1, ip, msr, lr;
603
604 asm("mr %0, 1" : "=r"(r1));
605 asm("mflr %0" : "=r"(lr));
606 asm("mfmsr %0" : "=r"(msr));
607 asm("bl 1f; 1: mflr %0" : "=r"(ip));
608
609 memset(regs, 0, sizeof(*regs));
610 regs->gpr[1] = r1;
611 regs->nip = ip;
612 regs->msr = msr;
613 regs->link = lr;
614}
615
Bharat Bhushan6328e592012-06-20 05:56:53 +0000616/*
617 * For interrupts needed to be handled by host interrupt handlers,
618 * corresponding host handler are called from here in similar way
619 * (but not exact) as they are called from low level handler
620 * (such as from arch/powerpc/kernel/head_fsl_booke.S).
621 */
Alexander Graf4e642cc2012-02-20 23:57:26 +0100622static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
623 unsigned int exit_nr)
624{
625 struct pt_regs regs;
626
627 switch (exit_nr) {
628 case BOOKE_INTERRUPT_EXTERNAL:
629 kvmppc_fill_pt_regs(&regs);
630 do_IRQ(&regs);
631 break;
632 case BOOKE_INTERRUPT_DECREMENTER:
633 kvmppc_fill_pt_regs(&regs);
634 timer_interrupt(&regs);
635 break;
636#if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64)
637 case BOOKE_INTERRUPT_DOORBELL:
638 kvmppc_fill_pt_regs(&regs);
639 doorbell_exception(&regs);
640 break;
641#endif
642 case BOOKE_INTERRUPT_MACHINE_CHECK:
643 /* FIXME */
644 break;
Alexander Graf7cc1e8e2012-02-22 16:26:34 +0100645 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
646 kvmppc_fill_pt_regs(&regs);
647 performance_monitor_exception(&regs);
648 break;
Bharat Bhushan6328e592012-06-20 05:56:53 +0000649 case BOOKE_INTERRUPT_WATCHDOG:
650 kvmppc_fill_pt_regs(&regs);
651#ifdef CONFIG_BOOKE_WDT
652 WatchdogException(&regs);
653#else
654 unknown_exception(&regs);
655#endif
656 break;
657 case BOOKE_INTERRUPT_CRITICAL:
658 unknown_exception(&regs);
659 break;
Alexander Graf4e642cc2012-02-20 23:57:26 +0100660 }
661}
662
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500663/**
664 * kvmppc_handle_exit
665 *
666 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
667 */
668int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
669 unsigned int exit_nr)
670{
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500671 int r = RESUME_HOST;
672
Hollis Blanchard73e75b412008-12-02 15:51:57 -0600673 /* update before a new last_exit_type is rewritten */
674 kvmppc_update_timing_stats(vcpu);
675
Alexander Graf4e642cc2012-02-20 23:57:26 +0100676 /* restart interrupts if they were meant for the host */
677 kvmppc_restart_interrupt(vcpu, exit_nr);
Scott Woodd30f6e42011-12-20 15:34:43 +0000678
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500679 local_irq_enable();
680
Alexander Graf97c95052012-08-02 15:10:00 +0200681 trace_kvm_exit(exit_nr, vcpu);
682
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500683 run->exit_reason = KVM_EXIT_UNKNOWN;
684 run->ready_for_interrupt_injection = 1;
685
686 switch (exit_nr) {
687 case BOOKE_INTERRUPT_MACHINE_CHECK:
Alexander Grafc35c9d82012-02-20 12:21:18 +0100688 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
689 kvmppc_dump_vcpu(vcpu);
690 /* For debugging, send invalid exit reason to user space */
691 run->hw.hardware_exit_reason = ~1ULL << 32;
692 run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
693 r = RESUME_HOST;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500694 break;
695
696 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600697 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -0600698 r = RESUME_GUEST;
699 break;
700
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500701 case BOOKE_INTERRUPT_DECREMENTER:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600702 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500703 r = RESUME_GUEST;
704 break;
705
Bharat Bhushan6328e592012-06-20 05:56:53 +0000706 case BOOKE_INTERRUPT_WATCHDOG:
707 r = RESUME_GUEST;
708 break;
709
Scott Woodd30f6e42011-12-20 15:34:43 +0000710 case BOOKE_INTERRUPT_DOORBELL:
711 kvmppc_account_exit(vcpu, DBELL_EXITS);
Scott Woodd30f6e42011-12-20 15:34:43 +0000712 r = RESUME_GUEST;
713 break;
714
715 case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
716 kvmppc_account_exit(vcpu, GDBELL_EXITS);
717
718 /*
719 * We are here because there is a pending guest interrupt
720 * which could not be delivered as MSR_CE or MSR_ME was not
721 * set. Once we break from here we will retry delivery.
722 */
723 r = RESUME_GUEST;
724 break;
725
726 case BOOKE_INTERRUPT_GUEST_DBELL:
727 kvmppc_account_exit(vcpu, GDBELL_EXITS);
728
729 /*
730 * We are here because there is a pending guest interrupt
731 * which could not be delivered as MSR_EE was not set. Once
732 * we break from here we will retry delivery.
733 */
734 r = RESUME_GUEST;
735 break;
736
Alexander Graf95f2e922012-02-20 22:45:12 +0100737 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
738 r = RESUME_GUEST;
739 break;
740
Scott Woodd30f6e42011-12-20 15:34:43 +0000741 case BOOKE_INTERRUPT_HV_PRIV:
742 r = emulation_exit(run, vcpu);
743 break;
744
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500745 case BOOKE_INTERRUPT_PROGRAM:
Scott Woodd30f6e42011-12-20 15:34:43 +0000746 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
Alexander Graf02685972012-02-20 12:33:22 +0100747 /*
748 * Program traps generated by user-level software must
749 * be handled by the guest kernel.
750 *
751 * In GS mode, hypervisor privileged instructions trap
752 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
753 * actual program interrupts, handled by the guest.
754 */
Liu Yudaf5e272010-02-02 19:44:35 +0800755 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500756 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600757 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500758 break;
759 }
760
Scott Woodd30f6e42011-12-20 15:34:43 +0000761 r = emulation_exit(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500762 break;
763
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200764 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600765 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600766 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200767 r = RESUME_GUEST;
768 break;
769
Scott Wood4cd35f62011-06-14 18:34:31 -0500770#ifdef CONFIG_SPE
771 case BOOKE_INTERRUPT_SPE_UNAVAIL: {
772 if (vcpu->arch.shared->msr & MSR_SPE)
773 kvmppc_vcpu_enable_spe(vcpu);
774 else
775 kvmppc_booke_queue_irqprio(vcpu,
776 BOOKE_IRQPRIO_SPE_UNAVAIL);
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600777 r = RESUME_GUEST;
778 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500779 }
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600780
781 case BOOKE_INTERRUPT_SPE_FP_DATA:
782 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
783 r = RESUME_GUEST;
784 break;
785
786 case BOOKE_INTERRUPT_SPE_FP_ROUND:
787 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
788 r = RESUME_GUEST;
789 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500790#else
791 case BOOKE_INTERRUPT_SPE_UNAVAIL:
792 /*
793 * Guest wants SPE, but host kernel doesn't support it. Send
794 * an "unimplemented operation" program check to the guest.
795 */
796 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
797 r = RESUME_GUEST;
798 break;
799
800 /*
801 * These really should never happen without CONFIG_SPE,
802 * as we should never enable the real MSR[SPE] in the guest.
803 */
804 case BOOKE_INTERRUPT_SPE_FP_DATA:
805 case BOOKE_INTERRUPT_SPE_FP_ROUND:
806 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
807 __func__, exit_nr, vcpu->arch.pc);
808 run->hw.hardware_exit_reason = exit_nr;
809 r = RESUME_HOST;
810 break;
811#endif
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600812
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500813 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800814 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
815 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600816 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500817 r = RESUME_GUEST;
818 break;
819
820 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800821 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600822 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500823 r = RESUME_GUEST;
824 break;
825
Scott Woodd30f6e42011-12-20 15:34:43 +0000826#ifdef CONFIG_KVM_BOOKE_HV
827 case BOOKE_INTERRUPT_HV_SYSCALL:
828 if (!(vcpu->arch.shared->msr & MSR_PR)) {
829 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
830 } else {
831 /*
832 * hcall from guest userspace -- send privileged
833 * instruction program check.
834 */
835 kvmppc_core_queue_program(vcpu, ESR_PPR);
836 }
837
838 r = RESUME_GUEST;
839 break;
840#else
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500841 case BOOKE_INTERRUPT_SYSCALL:
Alexander Graf2a342ed2010-07-29 14:47:48 +0200842 if (!(vcpu->arch.shared->msr & MSR_PR) &&
843 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
844 /* KVM PV hypercalls */
845 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
846 r = RESUME_GUEST;
847 } else {
848 /* Guest syscalls */
849 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
850 }
Hollis Blanchard7b701592008-12-02 15:51:58 -0600851 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500852 r = RESUME_GUEST;
853 break;
Scott Woodd30f6e42011-12-20 15:34:43 +0000854#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500855
856 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500857 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600858 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600859 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500860 gfn_t gfn;
861
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000862#ifdef CONFIG_KVM_E500V2
Scott Wooda4cd8b22011-06-14 18:34:41 -0500863 if (!(vcpu->arch.shared->msr & MSR_PR) &&
864 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
865 kvmppc_map_magic(vcpu);
866 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
867 r = RESUME_GUEST;
868
869 break;
870 }
871#endif
872
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500873 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600874 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600875 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500876 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +0800877 kvmppc_core_queue_dtlb_miss(vcpu,
878 vcpu->arch.fault_dear,
879 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600880 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600881 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500882 r = RESUME_GUEST;
883 break;
884 }
885
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600886 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600887 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500888
889 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
890 /* The guest TLB had a mapping, but the shadow TLB
891 * didn't, and it is RAM. This could be because:
892 * a) the entry is mapping the host kernel, or
893 * b) the guest used a large mapping which we're faking
894 * Either way, we need to satisfy the fault without
895 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600896 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600897 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500898 r = RESUME_GUEST;
899 } else {
900 /* Guest has mapped and accessed a page which is not
901 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600902 vcpu->arch.paddr_accessed = gpaddr;
Alexander Graf6020c0f2012-03-12 02:26:30 +0100903 vcpu->arch.vaddr_accessed = eaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500904 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600905 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500906 }
907
908 break;
909 }
910
911 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500912 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -0600913 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500914 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600915 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500916
917 r = RESUME_GUEST;
918
919 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600920 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600921 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500922 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600923 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600924 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600925 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500926 break;
927 }
928
Hollis Blanchard7b701592008-12-02 15:51:58 -0600929 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500930
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600931 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -0600932 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500933
934 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
935 /* The guest TLB had a mapping, but the shadow TLB
936 * didn't. This could be because:
937 * a) the entry is mapping the host kernel, or
938 * b) the guest used a large mapping which we're faking
939 * Either way, we need to satisfy the fault without
940 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600941 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500942 } else {
943 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600944 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500945 }
946
947 break;
948 }
949
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500950 case BOOKE_INTERRUPT_DEBUG: {
951 u32 dbsr;
952
953 vcpu->arch.pc = mfspr(SPRN_CSRR0);
954
955 /* clear IAC events in DBSR register */
956 dbsr = mfspr(SPRN_DBSR);
957 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
958 mtspr(SPRN_DBSR, dbsr);
959
960 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600961 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500962 r = RESUME_HOST;
963 break;
964 }
965
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500966 default:
967 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
968 BUG();
969 }
970
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000971 /*
972 * To avoid clobbering exit_reason, only check for signals if we
973 * aren't already exiting to userspace for some other reason.
974 */
Alexander Graf03660ba2012-02-28 12:00:41 +0100975 if (!(r & RESUME_HOST)) {
976 local_irq_disable();
977 if (kvmppc_prepare_to_enter(vcpu)) {
978 run->exit_reason = KVM_EXIT_INTR;
979 r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
980 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
981 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500982 }
983
984 return r;
985}
986
987/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
988int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
989{
Hollis Blanchard082decf2010-08-07 10:33:56 -0700990 int i;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200991 int r;
Hollis Blanchard082decf2010-08-07 10:33:56 -0700992
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500993 vcpu->arch.pc = 0;
Scott Woodb5904972011-11-08 18:23:30 -0600994 vcpu->arch.shared->pir = vcpu->vcpu_id;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100995 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Scott Woodd30f6e42011-12-20 15:34:43 +0000996 kvmppc_set_msr(vcpu, 0);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500997
Scott Woodd30f6e42011-12-20 15:34:43 +0000998#ifndef CONFIG_KVM_BOOKE_HV
999 vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001000 vcpu->arch.shadow_pid = 1;
Scott Woodd30f6e42011-12-20 15:34:43 +00001001 vcpu->arch.shared->msr = 0;
1002#endif
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001003
Hollis Blanchard082decf2010-08-07 10:33:56 -07001004 /* Eye-catching numbers so we know if the guest takes an interrupt
1005 * before it's programmed its own IVPR/IVORs. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001006 vcpu->arch.ivpr = 0x55550000;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001007 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
1008 vcpu->arch.ivor[i] = 0x7700 | i * 4;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001009
Hollis Blanchard73e75b412008-12-02 15:51:57 -06001010 kvmppc_init_timing_stats(vcpu);
1011
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001012 r = kvmppc_core_vcpu_setup(vcpu);
1013 kvmppc_sanity_check(vcpu);
1014 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001015}
1016
1017int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1018{
1019 int i;
1020
1021 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001022 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001023 regs->ctr = vcpu->arch.ctr;
1024 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001025 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001026 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +02001027 regs->srr0 = vcpu->arch.shared->srr0;
1028 regs->srr1 = vcpu->arch.shared->srr1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001029 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001030 regs->sprg0 = vcpu->arch.shared->sprg0;
1031 regs->sprg1 = vcpu->arch.shared->sprg1;
1032 regs->sprg2 = vcpu->arch.shared->sprg2;
1033 regs->sprg3 = vcpu->arch.shared->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -06001034 regs->sprg4 = vcpu->arch.shared->sprg4;
1035 regs->sprg5 = vcpu->arch.shared->sprg5;
1036 regs->sprg6 = vcpu->arch.shared->sprg6;
1037 regs->sprg7 = vcpu->arch.shared->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001038
1039 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001040 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001041
1042 return 0;
1043}
1044
1045int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1046{
1047 int i;
1048
1049 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001050 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001051 vcpu->arch.ctr = regs->ctr;
1052 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001053 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -06001054 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +02001055 vcpu->arch.shared->srr0 = regs->srr0;
1056 vcpu->arch.shared->srr1 = regs->srr1;
Scott Wood5ce941e2011-04-27 17:24:21 -05001057 kvmppc_set_pid(vcpu, regs->pid);
Alexander Grafa73a9592010-07-29 14:47:47 +02001058 vcpu->arch.shared->sprg0 = regs->sprg0;
1059 vcpu->arch.shared->sprg1 = regs->sprg1;
1060 vcpu->arch.shared->sprg2 = regs->sprg2;
1061 vcpu->arch.shared->sprg3 = regs->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -06001062 vcpu->arch.shared->sprg4 = regs->sprg4;
1063 vcpu->arch.shared->sprg5 = regs->sprg5;
1064 vcpu->arch.shared->sprg6 = regs->sprg6;
1065 vcpu->arch.shared->sprg7 = regs->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001066
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001067 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1068 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001069
1070 return 0;
1071}
1072
Scott Wood5ce941e2011-04-27 17:24:21 -05001073static void get_sregs_base(struct kvm_vcpu *vcpu,
1074 struct kvm_sregs *sregs)
1075{
1076 u64 tb = get_tb();
1077
1078 sregs->u.e.features |= KVM_SREGS_E_BASE;
1079
1080 sregs->u.e.csrr0 = vcpu->arch.csrr0;
1081 sregs->u.e.csrr1 = vcpu->arch.csrr1;
1082 sregs->u.e.mcsr = vcpu->arch.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +00001083 sregs->u.e.esr = get_guest_esr(vcpu);
1084 sregs->u.e.dear = get_guest_dear(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001085 sregs->u.e.tsr = vcpu->arch.tsr;
1086 sregs->u.e.tcr = vcpu->arch.tcr;
1087 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
1088 sregs->u.e.tb = tb;
1089 sregs->u.e.vrsave = vcpu->arch.vrsave;
1090}
1091
1092static int set_sregs_base(struct kvm_vcpu *vcpu,
1093 struct kvm_sregs *sregs)
1094{
1095 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
1096 return 0;
1097
1098 vcpu->arch.csrr0 = sregs->u.e.csrr0;
1099 vcpu->arch.csrr1 = sregs->u.e.csrr1;
1100 vcpu->arch.mcsr = sregs->u.e.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +00001101 set_guest_esr(vcpu, sregs->u.e.esr);
1102 set_guest_dear(vcpu, sregs->u.e.dear);
Scott Wood5ce941e2011-04-27 17:24:21 -05001103 vcpu->arch.vrsave = sregs->u.e.vrsave;
Scott Wooddfd4d472011-11-17 12:39:59 +00001104 kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001105
Scott Wooddfd4d472011-11-17 12:39:59 +00001106 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
Scott Wood5ce941e2011-04-27 17:24:21 -05001107 vcpu->arch.dec = sregs->u.e.dec;
Scott Wooddfd4d472011-11-17 12:39:59 +00001108 kvmppc_emulate_dec(vcpu);
1109 }
Scott Wood5ce941e2011-04-27 17:24:21 -05001110
1111 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
Scott Wooddfd4d472011-11-17 12:39:59 +00001112 vcpu->arch.tsr = sregs->u.e.tsr;
1113 update_timer_ints(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001114 }
1115
1116 return 0;
1117}
1118
1119static void get_sregs_arch206(struct kvm_vcpu *vcpu,
1120 struct kvm_sregs *sregs)
1121{
1122 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
1123
Scott Wood841741f22011-09-02 17:39:37 -05001124 sregs->u.e.pir = vcpu->vcpu_id;
Scott Wood5ce941e2011-04-27 17:24:21 -05001125 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
1126 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
1127 sregs->u.e.decar = vcpu->arch.decar;
1128 sregs->u.e.ivpr = vcpu->arch.ivpr;
1129}
1130
1131static int set_sregs_arch206(struct kvm_vcpu *vcpu,
1132 struct kvm_sregs *sregs)
1133{
1134 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
1135 return 0;
1136
Scott Wood841741f22011-09-02 17:39:37 -05001137 if (sregs->u.e.pir != vcpu->vcpu_id)
Scott Wood5ce941e2011-04-27 17:24:21 -05001138 return -EINVAL;
1139
1140 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
1141 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
1142 vcpu->arch.decar = sregs->u.e.decar;
1143 vcpu->arch.ivpr = sregs->u.e.ivpr;
1144
1145 return 0;
1146}
1147
1148void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1149{
1150 sregs->u.e.features |= KVM_SREGS_E_IVOR;
1151
1152 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
1153 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
1154 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
1155 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
1156 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
1157 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
1158 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
1159 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
1160 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
1161 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
1162 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
1163 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
1164 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
1165 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
1166 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
1167 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
1168}
1169
1170int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1171{
1172 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
1173 return 0;
1174
1175 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
1176 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
1177 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
1178 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
1179 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
1180 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
1181 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
1182 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
1183 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
1184 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
1185 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
1186 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
1187 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
1188 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
1189 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
1190 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
1191
1192 return 0;
1193}
1194
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001195int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1196 struct kvm_sregs *sregs)
1197{
Scott Wood5ce941e2011-04-27 17:24:21 -05001198 sregs->pvr = vcpu->arch.pvr;
1199
1200 get_sregs_base(vcpu, sregs);
1201 get_sregs_arch206(vcpu, sregs);
1202 kvmppc_core_get_sregs(vcpu, sregs);
1203 return 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001204}
1205
1206int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1207 struct kvm_sregs *sregs)
1208{
Scott Wood5ce941e2011-04-27 17:24:21 -05001209 int ret;
1210
1211 if (vcpu->arch.pvr != sregs->pvr)
1212 return -EINVAL;
1213
1214 ret = set_sregs_base(vcpu, sregs);
1215 if (ret < 0)
1216 return ret;
1217
1218 ret = set_sregs_arch206(vcpu, sregs);
1219 if (ret < 0)
1220 return ret;
1221
1222 return kvmppc_core_set_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001223}
1224
Paul Mackerras31f34382011-12-12 12:26:50 +00001225int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1226{
1227 return -EINVAL;
1228}
1229
1230int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1231{
1232 return -EINVAL;
1233}
1234
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001235int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1236{
1237 return -ENOTSUPP;
1238}
1239
1240int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1241{
1242 return -ENOTSUPP;
1243}
1244
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001245int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1246 struct kvm_translation *tr)
1247{
Avi Kivity98001d82010-05-13 11:05:49 +03001248 int r;
1249
Avi Kivity98001d82010-05-13 11:05:49 +03001250 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +03001251 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001252}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001253
Alexander Graf4e755752009-10-30 05:47:01 +00001254int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1255{
1256 return -ENOTSUPP;
1257}
1258
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001259int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1260 struct kvm_userspace_memory_region *mem)
1261{
1262 return 0;
1263}
1264
1265void kvmppc_core_commit_memory_region(struct kvm *kvm,
1266 struct kvm_userspace_memory_region *mem)
1267{
1268}
1269
Scott Wooddfd4d472011-11-17 12:39:59 +00001270void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1271{
1272 vcpu->arch.tcr = new_tcr;
1273 update_timer_ints(vcpu);
1274}
1275
1276void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1277{
1278 set_bits(tsr_bits, &vcpu->arch.tsr);
1279 smp_wmb();
1280 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1281 kvm_vcpu_kick(vcpu);
1282}
1283
1284void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1285{
1286 clear_bits(tsr_bits, &vcpu->arch.tsr);
1287 update_timer_ints(vcpu);
1288}
1289
1290void kvmppc_decrementer_func(unsigned long data)
1291{
1292 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1293
Bharat Bhushan21bd0002012-05-20 23:21:23 +00001294 if (vcpu->arch.tcr & TCR_ARE) {
1295 vcpu->arch.dec = vcpu->arch.decar;
1296 kvmppc_emulate_dec(vcpu);
1297 }
1298
Scott Wooddfd4d472011-11-17 12:39:59 +00001299 kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1300}
1301
Scott Wood94fa9d92011-12-20 15:34:22 +00001302void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1303{
Scott Woodd30f6e42011-12-20 15:34:43 +00001304 current->thread.kvm_vcpu = vcpu;
Scott Wood94fa9d92011-12-20 15:34:22 +00001305}
1306
1307void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
1308{
Scott Woodd30f6e42011-12-20 15:34:43 +00001309 current->thread.kvm_vcpu = NULL;
Scott Wood94fa9d92011-12-20 15:34:22 +00001310}
1311
Stephen Rothwell2986b8c2009-06-02 11:46:14 +10001312int __init kvmppc_booke_init(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001313{
Scott Woodd30f6e42011-12-20 15:34:43 +00001314#ifndef CONFIG_KVM_BOOKE_HV
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001315 unsigned long ivor[16];
1316 unsigned long max_ivor = 0;
1317 int i;
1318
1319 /* We install our own exception handlers by hijacking IVPR. IVPR must
1320 * be 16-bit aligned, so we need a 64KB allocation. */
1321 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1322 VCPU_SIZE_ORDER);
1323 if (!kvmppc_booke_handlers)
1324 return -ENOMEM;
1325
1326 /* XXX make sure our handlers are smaller than Linux's */
1327
1328 /* Copy our interrupt handlers to match host IVORs. That way we don't
1329 * have to swap the IVORs on every guest/host transition. */
1330 ivor[0] = mfspr(SPRN_IVOR0);
1331 ivor[1] = mfspr(SPRN_IVOR1);
1332 ivor[2] = mfspr(SPRN_IVOR2);
1333 ivor[3] = mfspr(SPRN_IVOR3);
1334 ivor[4] = mfspr(SPRN_IVOR4);
1335 ivor[5] = mfspr(SPRN_IVOR5);
1336 ivor[6] = mfspr(SPRN_IVOR6);
1337 ivor[7] = mfspr(SPRN_IVOR7);
1338 ivor[8] = mfspr(SPRN_IVOR8);
1339 ivor[9] = mfspr(SPRN_IVOR9);
1340 ivor[10] = mfspr(SPRN_IVOR10);
1341 ivor[11] = mfspr(SPRN_IVOR11);
1342 ivor[12] = mfspr(SPRN_IVOR12);
1343 ivor[13] = mfspr(SPRN_IVOR13);
1344 ivor[14] = mfspr(SPRN_IVOR14);
1345 ivor[15] = mfspr(SPRN_IVOR15);
1346
1347 for (i = 0; i < 16; i++) {
1348 if (ivor[i] > max_ivor)
1349 max_ivor = ivor[i];
1350
1351 memcpy((void *)kvmppc_booke_handlers + ivor[i],
1352 kvmppc_handlers_start + i * kvmppc_handler_len,
1353 kvmppc_handler_len);
1354 }
1355 flush_icache_range(kvmppc_booke_handlers,
1356 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
Scott Woodd30f6e42011-12-20 15:34:43 +00001357#endif /* !BOOKE_HV */
Hollis Blancharddb93f572008-11-05 09:36:18 -06001358 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001359}
1360
Hollis Blancharddb93f572008-11-05 09:36:18 -06001361void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001362{
1363 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
1364 kvm_exit();
1365}