blob: 964f4475f55ceaaf08b5d94e352830b6b000269a [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 Wood4cd35f672011-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>
Mihai Caramanb50df192012-10-11 06:13:19 +000039#include <asm/time.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050040
Scott Woodd30f6e42011-12-20 15:34:43 +000041#include "timing.h"
Hollis Blanchard75f74f02008-11-05 09:36:16 -060042#include "booke.h"
Alexander Graf97c95052012-08-02 15:10:00 +020043#include "trace.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050044
Hollis Blanchardd9fbd032008-11-05 09:36:13 -060045unsigned long kvmppc_booke_handlers;
46
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050047#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
48#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
49
50struct kvm_stats_debugfs_item debugfs_entries[] = {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050051 { "mmio", VCPU_STAT(mmio_exits) },
52 { "dcr", VCPU_STAT(dcr_exits) },
53 { "sig", VCPU_STAT(signal_exits) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050054 { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
55 { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
56 { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
57 { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
58 { "sysc", VCPU_STAT(syscall_exits) },
59 { "isi", VCPU_STAT(isi_exits) },
60 { "dsi", VCPU_STAT(dsi_exits) },
61 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
62 { "dec", VCPU_STAT(dec_exits) },
63 { "ext_intr", VCPU_STAT(ext_intr_exits) },
Hollis Blanchard45c5eb62008-04-25 17:55:49 -050064 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Scott Woodd30f6e42011-12-20 15:34:43 +000065 { "doorbell", VCPU_STAT(dbell_exits) },
66 { "guest doorbell", VCPU_STAT(gdbell_exits) },
Alexander Grafcf1c5ca2012-08-01 12:56:51 +020067 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050068 { NULL }
69};
70
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050071/* TODO: use vcpu_printf() */
72void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
73{
74 int i;
75
Alexander Graf666e7252010-07-29 14:47:43 +020076 printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060077 printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
Alexander Grafde7906c2010-07-29 14:47:46 +020078 printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
79 vcpu->arch.shared->srr1);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050080
81 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
82
83 for (i = 0; i < 32; i += 4) {
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060084 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
Alexander Graf8e5b26b2010-01-08 02:58:01 +010085 kvmppc_get_gpr(vcpu, i),
86 kvmppc_get_gpr(vcpu, i+1),
87 kvmppc_get_gpr(vcpu, i+2),
88 kvmppc_get_gpr(vcpu, i+3));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050089 }
90}
91
Scott Wood4cd35f672011-06-14 18:34:31 -050092#ifdef CONFIG_SPE
93void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
94{
95 preempt_disable();
96 enable_kernel_spe();
97 kvmppc_save_guest_spe(vcpu);
98 vcpu->arch.shadow_msr &= ~MSR_SPE;
99 preempt_enable();
100}
101
102static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
103{
104 preempt_disable();
105 enable_kernel_spe();
106 kvmppc_load_guest_spe(vcpu);
107 vcpu->arch.shadow_msr |= MSR_SPE;
108 preempt_enable();
109}
110
111static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
112{
113 if (vcpu->arch.shared->msr & MSR_SPE) {
114 if (!(vcpu->arch.shadow_msr & MSR_SPE))
115 kvmppc_vcpu_enable_spe(vcpu);
116 } else if (vcpu->arch.shadow_msr & MSR_SPE) {
117 kvmppc_vcpu_disable_spe(vcpu);
118 }
119}
120#else
121static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
122{
123}
124#endif
125
Alexander Graf7a08c272012-08-16 13:10:16 +0200126static void kvmppc_vcpu_sync_fpu(struct kvm_vcpu *vcpu)
127{
128#if defined(CONFIG_PPC_FPU) && !defined(CONFIG_KVM_BOOKE_HV)
129 /* We always treat the FP bit as enabled from the host
130 perspective, so only need to adjust the shadow MSR */
131 vcpu->arch.shadow_msr &= ~MSR_FP;
132 vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_FP;
133#endif
134}
135
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500136/*
137 * Helper function for "full" MSR writes. No need to call this if only
138 * EE/CE/ME/DE/RI are changing.
139 */
Scott Wood4cd35f672011-06-14 18:34:31 -0500140void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
141{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500142 u32 old_msr = vcpu->arch.shared->msr;
Scott Wood4cd35f672011-06-14 18:34:31 -0500143
Scott Woodd30f6e42011-12-20 15:34:43 +0000144#ifdef CONFIG_KVM_BOOKE_HV
145 new_msr |= MSR_GS;
146#endif
147
Scott Wood4cd35f672011-06-14 18:34:31 -0500148 vcpu->arch.shared->msr = new_msr;
149
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500150 kvmppc_mmu_msr_notify(vcpu, old_msr);
Scott Wood4cd35f672011-06-14 18:34:31 -0500151 kvmppc_vcpu_sync_spe(vcpu);
Alexander Graf7a08c272012-08-16 13:10:16 +0200152 kvmppc_vcpu_sync_fpu(vcpu);
Scott Wood4cd35f672011-06-14 18:34:31 -0500153}
154
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600155static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
156 unsigned int priority)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600157{
Alexander Graf63460462012-08-08 00:44:52 +0200158 trace_kvm_booke_queue_irqprio(vcpu, priority);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600159 set_bit(priority, &vcpu->arch.pending_exceptions);
160}
161
Liu Yudaf5e272010-02-02 19:44:35 +0800162static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
163 ulong dear_flags, ulong esr_flags)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600164{
Liu Yudaf5e272010-02-02 19:44:35 +0800165 vcpu->arch.queued_dear = dear_flags;
166 vcpu->arch.queued_esr = esr_flags;
167 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
168}
169
170static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
171 ulong dear_flags, ulong esr_flags)
172{
173 vcpu->arch.queued_dear = dear_flags;
174 vcpu->arch.queued_esr = esr_flags;
175 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
176}
177
178static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
179 ulong esr_flags)
180{
181 vcpu->arch.queued_esr = esr_flags;
182 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
183}
184
185void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
186{
187 vcpu->arch.queued_esr = esr_flags;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600188 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600189}
190
191void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
192{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600193 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600194}
195
196int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
197{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600198 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600199}
200
Alexander Graf7706664d2009-12-21 20:21:24 +0100201void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
202{
203 clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
204}
205
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600206void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
207 struct kvm_interrupt *irq)
208{
Alexander Grafc5335f12010-08-30 14:03:24 +0200209 unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
210
211 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
212 prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
213
214 kvmppc_booke_queue_irqprio(vcpu, prio);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600215}
216
Alexander Graf4496f972010-04-07 10:03:25 +0200217void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
218 struct kvm_interrupt *irq)
219{
220 clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
Alexander Grafc5335f12010-08-30 14:03:24 +0200221 clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
Alexander Graf4496f972010-04-07 10:03:25 +0200222}
223
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000224static void kvmppc_core_queue_watchdog(struct kvm_vcpu *vcpu)
225{
226 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_WATCHDOG);
227}
228
229static void kvmppc_core_dequeue_watchdog(struct kvm_vcpu *vcpu)
230{
231 clear_bit(BOOKE_IRQPRIO_WATCHDOG, &vcpu->arch.pending_exceptions);
232}
233
Scott Woodd30f6e42011-12-20 15:34:43 +0000234static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
235{
236#ifdef CONFIG_KVM_BOOKE_HV
237 mtspr(SPRN_GSRR0, srr0);
238 mtspr(SPRN_GSRR1, srr1);
239#else
240 vcpu->arch.shared->srr0 = srr0;
241 vcpu->arch.shared->srr1 = srr1;
242#endif
243}
244
245static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
246{
247 vcpu->arch.csrr0 = srr0;
248 vcpu->arch.csrr1 = srr1;
249}
250
251static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
252{
253 if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
254 vcpu->arch.dsrr0 = srr0;
255 vcpu->arch.dsrr1 = srr1;
256 } else {
257 set_guest_csrr(vcpu, srr0, srr1);
258 }
259}
260
261static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
262{
263 vcpu->arch.mcsrr0 = srr0;
264 vcpu->arch.mcsrr1 = srr1;
265}
266
267static unsigned long get_guest_dear(struct kvm_vcpu *vcpu)
268{
269#ifdef CONFIG_KVM_BOOKE_HV
270 return mfspr(SPRN_GDEAR);
271#else
272 return vcpu->arch.shared->dar;
273#endif
274}
275
276static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear)
277{
278#ifdef CONFIG_KVM_BOOKE_HV
279 mtspr(SPRN_GDEAR, dear);
280#else
281 vcpu->arch.shared->dar = dear;
282#endif
283}
284
285static unsigned long get_guest_esr(struct kvm_vcpu *vcpu)
286{
287#ifdef CONFIG_KVM_BOOKE_HV
288 return mfspr(SPRN_GESR);
289#else
290 return vcpu->arch.shared->esr;
291#endif
292}
293
294static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr)
295{
296#ifdef CONFIG_KVM_BOOKE_HV
297 mtspr(SPRN_GESR, esr);
298#else
299 vcpu->arch.shared->esr = esr;
300#endif
301}
302
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600303/* Deliver the interrupt of the corresponding priority, if possible. */
304static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
305 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500306{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600307 int allowed = 0;
Alexander Graf79300f82012-02-15 19:12:29 +0000308 ulong msr_mask = 0;
Liu Yudaf5e272010-02-02 19:44:35 +0800309 bool update_esr = false, update_dear = false;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200310 ulong crit_raw = vcpu->arch.shared->critical;
311 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
312 bool crit;
Alexander Grafc5335f12010-08-30 14:03:24 +0200313 bool keep_irq = false;
Scott Woodd30f6e42011-12-20 15:34:43 +0000314 enum int_class int_class;
Mihai Caraman95e90b42012-10-11 06:13:26 +0000315 ulong new_msr = vcpu->arch.shared->msr;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200316
317 /* Truncate crit indicators in 32 bit mode */
318 if (!(vcpu->arch.shared->msr & MSR_SF)) {
319 crit_raw &= 0xffffffff;
320 crit_r1 &= 0xffffffff;
321 }
322
323 /* Critical section when crit == r1 */
324 crit = (crit_raw == crit_r1);
325 /* ... and we're in supervisor mode */
326 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500327
Alexander Grafc5335f12010-08-30 14:03:24 +0200328 if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
329 priority = BOOKE_IRQPRIO_EXTERNAL;
330 keep_irq = true;
331 }
332
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600333 switch (priority) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600334 case BOOKE_IRQPRIO_DTLB_MISS:
Liu Yudaf5e272010-02-02 19:44:35 +0800335 case BOOKE_IRQPRIO_DATA_STORAGE:
336 update_dear = true;
337 /* fall through */
338 case BOOKE_IRQPRIO_INST_STORAGE:
339 case BOOKE_IRQPRIO_PROGRAM:
340 update_esr = true;
341 /* fall through */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600342 case BOOKE_IRQPRIO_ITLB_MISS:
343 case BOOKE_IRQPRIO_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600344 case BOOKE_IRQPRIO_FP_UNAVAIL:
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600345 case BOOKE_IRQPRIO_SPE_UNAVAIL:
346 case BOOKE_IRQPRIO_SPE_FP_DATA:
347 case BOOKE_IRQPRIO_SPE_FP_ROUND:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600348 case BOOKE_IRQPRIO_AP_UNAVAIL:
349 case BOOKE_IRQPRIO_ALIGNMENT:
350 allowed = 1;
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;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000354 case BOOKE_IRQPRIO_WATCHDOG:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600355 case BOOKE_IRQPRIO_CRITICAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000356 case BOOKE_IRQPRIO_DBELL_CRIT:
Alexander Graf666e7252010-07-29 14:47:43 +0200357 allowed = vcpu->arch.shared->msr & MSR_CE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000358 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000359 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000360 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500361 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600362 case BOOKE_IRQPRIO_MACHINE_CHECK:
Alexander Graf666e7252010-07-29 14:47:43 +0200363 allowed = vcpu->arch.shared->msr & MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000364 allowed = allowed && !crit;
Scott Woodd30f6e42011-12-20 15:34:43 +0000365 int_class = INT_CLASS_MC;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500366 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600367 case BOOKE_IRQPRIO_DECREMENTER:
368 case BOOKE_IRQPRIO_FIT:
Scott Wooddfd4d472011-11-17 12:39:59 +0000369 keep_irq = true;
370 /* fall through */
371 case BOOKE_IRQPRIO_EXTERNAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000372 case BOOKE_IRQPRIO_DBELL:
Alexander Graf666e7252010-07-29 14:47:43 +0200373 allowed = vcpu->arch.shared->msr & MSR_EE;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200374 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000375 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000376 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500377 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600378 case BOOKE_IRQPRIO_DEBUG:
Alexander Graf666e7252010-07-29 14:47:43 +0200379 allowed = vcpu->arch.shared->msr & MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000380 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000381 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000382 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500383 break;
384 }
385
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600386 if (allowed) {
Scott Woodd30f6e42011-12-20 15:34:43 +0000387 switch (int_class) {
388 case INT_CLASS_NONCRIT:
389 set_guest_srr(vcpu, vcpu->arch.pc,
390 vcpu->arch.shared->msr);
391 break;
392 case INT_CLASS_CRIT:
393 set_guest_csrr(vcpu, vcpu->arch.pc,
394 vcpu->arch.shared->msr);
395 break;
396 case INT_CLASS_DBG:
397 set_guest_dsrr(vcpu, vcpu->arch.pc,
398 vcpu->arch.shared->msr);
399 break;
400 case INT_CLASS_MC:
401 set_guest_mcsrr(vcpu, vcpu->arch.pc,
402 vcpu->arch.shared->msr);
403 break;
404 }
405
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600406 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
Liu Yudaf5e272010-02-02 19:44:35 +0800407 if (update_esr == true)
Scott Woodd30f6e42011-12-20 15:34:43 +0000408 set_guest_esr(vcpu, vcpu->arch.queued_esr);
Liu Yudaf5e272010-02-02 19:44:35 +0800409 if (update_dear == true)
Scott Woodd30f6e42011-12-20 15:34:43 +0000410 set_guest_dear(vcpu, vcpu->arch.queued_dear);
Mihai Caraman95e90b42012-10-11 06:13:26 +0000411
412 new_msr &= msr_mask;
413#if defined(CONFIG_64BIT)
414 if (vcpu->arch.epcr & SPRN_EPCR_ICM)
415 new_msr |= MSR_CM;
416#endif
417 kvmppc_set_msr(vcpu, new_msr);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600418
Alexander Grafc5335f12010-08-30 14:03:24 +0200419 if (!keep_irq)
420 clear_bit(priority, &vcpu->arch.pending_exceptions);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600421 }
422
Scott Woodd30f6e42011-12-20 15:34:43 +0000423#ifdef CONFIG_KVM_BOOKE_HV
424 /*
425 * If an interrupt is pending but masked, raise a guest doorbell
426 * so that we are notified when the guest enables the relevant
427 * MSR bit.
428 */
429 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
430 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
431 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
432 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
433 if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
434 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
435#endif
436
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600437 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500438}
439
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000440/*
441 * Return the number of jiffies until the next timeout. If the timeout is
442 * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA
443 * because the larger value can break the timer APIs.
444 */
445static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu)
446{
447 u64 tb, wdt_tb, wdt_ticks = 0;
448 u64 nr_jiffies = 0;
449 u32 period = TCR_GET_WP(vcpu->arch.tcr);
450
451 wdt_tb = 1ULL << (63 - period);
452 tb = get_tb();
453 /*
454 * The watchdog timeout will hapeen when TB bit corresponding
455 * to watchdog will toggle from 0 to 1.
456 */
457 if (tb & wdt_tb)
458 wdt_ticks = wdt_tb;
459
460 wdt_ticks += wdt_tb - (tb & (wdt_tb - 1));
461
462 /* Convert timebase ticks to jiffies */
463 nr_jiffies = wdt_ticks;
464
465 if (do_div(nr_jiffies, tb_ticks_per_jiffy))
466 nr_jiffies++;
467
468 return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA);
469}
470
471static void arm_next_watchdog(struct kvm_vcpu *vcpu)
472{
473 unsigned long nr_jiffies;
474 unsigned long flags;
475
476 /*
477 * If TSR_ENW and TSR_WIS are not set then no need to exit to
478 * userspace, so clear the KVM_REQ_WATCHDOG request.
479 */
480 if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS))
481 clear_bit(KVM_REQ_WATCHDOG, &vcpu->requests);
482
483 spin_lock_irqsave(&vcpu->arch.wdt_lock, flags);
484 nr_jiffies = watchdog_next_timeout(vcpu);
485 /*
486 * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA
487 * then do not run the watchdog timer as this can break timer APIs.
488 */
489 if (nr_jiffies < NEXT_TIMER_MAX_DELTA)
490 mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies);
491 else
492 del_timer(&vcpu->arch.wdt_timer);
493 spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags);
494}
495
496void kvmppc_watchdog_func(unsigned long data)
497{
498 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
499 u32 tsr, new_tsr;
500 int final;
501
502 do {
503 new_tsr = tsr = vcpu->arch.tsr;
504 final = 0;
505
506 /* Time out event */
507 if (tsr & TSR_ENW) {
508 if (tsr & TSR_WIS)
509 final = 1;
510 else
511 new_tsr = tsr | TSR_WIS;
512 } else {
513 new_tsr = tsr | TSR_ENW;
514 }
515 } while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr);
516
517 if (new_tsr & TSR_WIS) {
518 smp_wmb();
519 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
520 kvm_vcpu_kick(vcpu);
521 }
522
523 /*
524 * If this is final watchdog expiry and some action is required
525 * then exit to userspace.
526 */
527 if (final && (vcpu->arch.tcr & TCR_WRC_MASK) &&
528 vcpu->arch.watchdog_enabled) {
529 smp_wmb();
530 kvm_make_request(KVM_REQ_WATCHDOG, vcpu);
531 kvm_vcpu_kick(vcpu);
532 }
533
534 /*
535 * Stop running the watchdog timer after final expiration to
536 * prevent the host from being flooded with timers if the
537 * guest sets a short period.
538 * Timers will resume when TSR/TCR is updated next time.
539 */
540 if (!final)
541 arm_next_watchdog(vcpu);
542}
543
Scott Wooddfd4d472011-11-17 12:39:59 +0000544static void update_timer_ints(struct kvm_vcpu *vcpu)
545{
546 if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
547 kvmppc_core_queue_dec(vcpu);
548 else
549 kvmppc_core_dequeue_dec(vcpu);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000550
551 if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS))
552 kvmppc_core_queue_watchdog(vcpu);
553 else
554 kvmppc_core_dequeue_watchdog(vcpu);
Scott Wooddfd4d472011-11-17 12:39:59 +0000555}
556
Scott Woodc59a6a32011-11-08 18:23:25 -0600557static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500558{
559 unsigned long *pending = &vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500560 unsigned int priority;
561
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600562 priority = __ffs(*pending);
Alexander Graf8b3a00f2012-02-16 14:12:46 +0000563 while (priority < BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600564 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500565 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500566
567 priority = find_next_bit(pending,
568 BITS_PER_BYTE * sizeof(*pending),
569 priority + 1);
570 }
Alexander Graf90bba352010-07-29 14:47:51 +0200571
572 /* Tell the guest about our interrupt status */
Scott Wood29ac26e2011-11-08 18:23:27 -0600573 vcpu->arch.shared->int_pending = !!*pending;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500574}
575
Scott Woodc59a6a32011-11-08 18:23:25 -0600576/* Check pending exceptions and deliver one, if possible. */
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000577int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
Scott Woodc59a6a32011-11-08 18:23:25 -0600578{
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000579 int r = 0;
Scott Woodc59a6a32011-11-08 18:23:25 -0600580 WARN_ON_ONCE(!irqs_disabled());
581
582 kvmppc_core_check_exceptions(vcpu);
583
Alexander Grafb8c649a2012-12-20 04:52:39 +0000584 if (vcpu->requests) {
585 /* Exception delivery raised request; start over */
586 return 1;
587 }
588
Scott Woodc59a6a32011-11-08 18:23:25 -0600589 if (vcpu->arch.shared->msr & MSR_WE) {
590 local_irq_enable();
591 kvm_vcpu_block(vcpu);
Alexander Graf966cd0f2012-03-14 16:55:08 +0100592 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Scott Woodc59a6a32011-11-08 18:23:25 -0600593 local_irq_disable();
594
595 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000596 r = 1;
Scott Woodc59a6a32011-11-08 18:23:25 -0600597 };
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000598
599 return r;
600}
601
Alexander Graf7c973a22012-08-13 12:50:35 +0200602int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
Alexander Graf4ffc6352012-08-08 20:31:13 +0200603{
Alexander Graf7c973a22012-08-13 12:50:35 +0200604 int r = 1; /* Indicate we want to get back into the guest */
605
Alexander Graf2d8185d2012-08-10 12:31:12 +0200606 if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
607 update_timer_ints(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200608#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Alexander Graf2d8185d2012-08-10 12:31:12 +0200609 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
610 kvmppc_core_flush_tlb(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200611#endif
Alexander Graf7c973a22012-08-13 12:50:35 +0200612
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000613 if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) {
614 vcpu->run->exit_reason = KVM_EXIT_WATCHDOG;
615 r = 0;
616 }
617
Alexander Graf7c973a22012-08-13 12:50:35 +0200618 return r;
Alexander Graf4ffc6352012-08-08 20:31:13 +0200619}
620
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000621int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
622{
Alexander Graf7ee78852012-08-13 12:44:41 +0200623 int ret, s;
Scott Wood8fae8452011-12-20 15:34:45 +0000624#ifdef CONFIG_PPC_FPU
625 unsigned int fpscr;
626 int fpexc_mode;
627 u64 fpr[32];
628#endif
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000629
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200630 if (!vcpu->arch.sane) {
631 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
632 return -EINVAL;
633 }
634
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000635 local_irq_disable();
Alexander Graf7ee78852012-08-13 12:44:41 +0200636 s = kvmppc_prepare_to_enter(vcpu);
637 if (s <= 0) {
Alexander Graf24afa372012-08-12 12:42:30 +0200638 local_irq_enable();
Alexander Graf7ee78852012-08-13 12:44:41 +0200639 ret = s;
Scott Wood1d1ef222011-11-08 16:11:59 -0600640 goto out;
641 }
Alexander Grafbd2be682012-08-13 01:04:19 +0200642 kvmppc_lazy_ee_enable();
Scott Wood1d1ef222011-11-08 16:11:59 -0600643
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000644 kvm_guest_enter();
Scott Wood8fae8452011-12-20 15:34:45 +0000645
646#ifdef CONFIG_PPC_FPU
647 /* Save userspace FPU state in stack */
648 enable_kernel_fp();
649 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
650 fpscr = current->thread.fpscr.val;
651 fpexc_mode = current->thread.fpexc_mode;
652
653 /* Restore guest FPU state to thread */
654 memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr));
655 current->thread.fpscr.val = vcpu->arch.fpscr;
656
657 /*
658 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
659 * as always using the FPU. Kernel usage of FP (via
660 * enable_kernel_fp()) in this thread must not occur while
661 * vcpu->fpu_active is set.
662 */
663 vcpu->fpu_active = 1;
664
665 kvmppc_load_guest_fp(vcpu);
666#endif
667
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000668 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
Scott Wood8fae8452011-12-20 15:34:45 +0000669
Alexander Graf24afa372012-08-12 12:42:30 +0200670 /* No need for kvm_guest_exit. It's done in handle_exit.
671 We also get here with interrupts enabled. */
672
Scott Wood8fae8452011-12-20 15:34:45 +0000673#ifdef CONFIG_PPC_FPU
674 kvmppc_save_guest_fp(vcpu);
675
676 vcpu->fpu_active = 0;
677
678 /* Save guest FPU state from thread */
679 memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr));
680 vcpu->arch.fpscr = current->thread.fpscr.val;
681
682 /* Restore userspace FPU state from stack */
683 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
684 current->thread.fpscr.val = fpscr;
685 current->thread.fpexc_mode = fpexc_mode;
686#endif
687
Scott Wood1d1ef222011-11-08 16:11:59 -0600688out:
Alexander Grafd69c6432012-08-08 20:44:20 +0200689 vcpu->mode = OUTSIDE_GUEST_MODE;
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000690 return ret;
691}
692
Scott Woodd30f6e42011-12-20 15:34:43 +0000693static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
694{
695 enum emulation_result er;
696
697 er = kvmppc_emulate_instruction(run, vcpu);
698 switch (er) {
699 case EMULATE_DONE:
700 /* don't overwrite subtypes, just account kvm_stats */
701 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
702 /* Future optimization: only reload non-volatiles if
703 * they were actually modified by emulation. */
704 return RESUME_GUEST_NV;
705
706 case EMULATE_DO_DCR:
707 run->exit_reason = KVM_EXIT_DCR;
708 return RESUME_HOST;
709
710 case EMULATE_FAIL:
Scott Woodd30f6e42011-12-20 15:34:43 +0000711 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
712 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
713 /* For debugging, encode the failing instruction and
714 * report it to userspace. */
715 run->hw.hardware_exit_reason = ~0ULL << 32;
716 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
Alexander Grafd1ff5492012-02-16 13:24:03 +0000717 kvmppc_core_queue_program(vcpu, ESR_PIL);
Scott Woodd30f6e42011-12-20 15:34:43 +0000718 return RESUME_HOST;
719
720 default:
721 BUG();
722 }
723}
724
Alexander Graf4e642cc2012-02-20 23:57:26 +0100725static void kvmppc_fill_pt_regs(struct pt_regs *regs)
726{
727 ulong r1, ip, msr, lr;
728
729 asm("mr %0, 1" : "=r"(r1));
730 asm("mflr %0" : "=r"(lr));
731 asm("mfmsr %0" : "=r"(msr));
732 asm("bl 1f; 1: mflr %0" : "=r"(ip));
733
734 memset(regs, 0, sizeof(*regs));
735 regs->gpr[1] = r1;
736 regs->nip = ip;
737 regs->msr = msr;
738 regs->link = lr;
739}
740
Bharat Bhushan6328e592012-06-20 05:56:53 +0000741/*
742 * For interrupts needed to be handled by host interrupt handlers,
743 * corresponding host handler are called from here in similar way
744 * (but not exact) as they are called from low level handler
745 * (such as from arch/powerpc/kernel/head_fsl_booke.S).
746 */
Alexander Graf4e642cc2012-02-20 23:57:26 +0100747static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
748 unsigned int exit_nr)
749{
750 struct pt_regs regs;
751
752 switch (exit_nr) {
753 case BOOKE_INTERRUPT_EXTERNAL:
754 kvmppc_fill_pt_regs(&regs);
755 do_IRQ(&regs);
756 break;
757 case BOOKE_INTERRUPT_DECREMENTER:
758 kvmppc_fill_pt_regs(&regs);
759 timer_interrupt(&regs);
760 break;
761#if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64)
762 case BOOKE_INTERRUPT_DOORBELL:
763 kvmppc_fill_pt_regs(&regs);
764 doorbell_exception(&regs);
765 break;
766#endif
767 case BOOKE_INTERRUPT_MACHINE_CHECK:
768 /* FIXME */
769 break;
Alexander Graf7cc1e8e2012-02-22 16:26:34 +0100770 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
771 kvmppc_fill_pt_regs(&regs);
772 performance_monitor_exception(&regs);
773 break;
Bharat Bhushan6328e592012-06-20 05:56:53 +0000774 case BOOKE_INTERRUPT_WATCHDOG:
775 kvmppc_fill_pt_regs(&regs);
776#ifdef CONFIG_BOOKE_WDT
777 WatchdogException(&regs);
778#else
779 unknown_exception(&regs);
780#endif
781 break;
782 case BOOKE_INTERRUPT_CRITICAL:
783 unknown_exception(&regs);
784 break;
Alexander Graf4e642cc2012-02-20 23:57:26 +0100785 }
786}
787
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500788/**
789 * kvmppc_handle_exit
790 *
791 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
792 */
793int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
794 unsigned int exit_nr)
795{
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500796 int r = RESUME_HOST;
Alexander Graf7ee78852012-08-13 12:44:41 +0200797 int s;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500798
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600799 /* update before a new last_exit_type is rewritten */
800 kvmppc_update_timing_stats(vcpu);
801
Alexander Graf4e642cc2012-02-20 23:57:26 +0100802 /* restart interrupts if they were meant for the host */
803 kvmppc_restart_interrupt(vcpu, exit_nr);
Scott Woodd30f6e42011-12-20 15:34:43 +0000804
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500805 local_irq_enable();
806
Alexander Graf97c95052012-08-02 15:10:00 +0200807 trace_kvm_exit(exit_nr, vcpu);
Alexander Graf706fb732012-08-12 11:29:09 +0200808 kvm_guest_exit();
Alexander Graf97c95052012-08-02 15:10:00 +0200809
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500810 run->exit_reason = KVM_EXIT_UNKNOWN;
811 run->ready_for_interrupt_injection = 1;
812
813 switch (exit_nr) {
814 case BOOKE_INTERRUPT_MACHINE_CHECK:
Alexander Grafc35c9d82012-02-20 12:21:18 +0100815 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
816 kvmppc_dump_vcpu(vcpu);
817 /* For debugging, send invalid exit reason to user space */
818 run->hw.hardware_exit_reason = ~1ULL << 32;
819 run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
820 r = RESUME_HOST;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500821 break;
822
823 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600824 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -0600825 r = RESUME_GUEST;
826 break;
827
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500828 case BOOKE_INTERRUPT_DECREMENTER:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600829 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500830 r = RESUME_GUEST;
831 break;
832
Bharat Bhushan6328e592012-06-20 05:56:53 +0000833 case BOOKE_INTERRUPT_WATCHDOG:
834 r = RESUME_GUEST;
835 break;
836
Scott Woodd30f6e42011-12-20 15:34:43 +0000837 case BOOKE_INTERRUPT_DOORBELL:
838 kvmppc_account_exit(vcpu, DBELL_EXITS);
Scott Woodd30f6e42011-12-20 15:34:43 +0000839 r = RESUME_GUEST;
840 break;
841
842 case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
843 kvmppc_account_exit(vcpu, GDBELL_EXITS);
844
845 /*
846 * We are here because there is a pending guest interrupt
847 * which could not be delivered as MSR_CE or MSR_ME was not
848 * set. Once we break from here we will retry delivery.
849 */
850 r = RESUME_GUEST;
851 break;
852
853 case BOOKE_INTERRUPT_GUEST_DBELL:
854 kvmppc_account_exit(vcpu, GDBELL_EXITS);
855
856 /*
857 * We are here because there is a pending guest interrupt
858 * which could not be delivered as MSR_EE was not set. Once
859 * we break from here we will retry delivery.
860 */
861 r = RESUME_GUEST;
862 break;
863
Alexander Graf95f2e922012-02-20 22:45:12 +0100864 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
865 r = RESUME_GUEST;
866 break;
867
Scott Woodd30f6e42011-12-20 15:34:43 +0000868 case BOOKE_INTERRUPT_HV_PRIV:
869 r = emulation_exit(run, vcpu);
870 break;
871
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500872 case BOOKE_INTERRUPT_PROGRAM:
Scott Woodd30f6e42011-12-20 15:34:43 +0000873 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
Alexander Graf02685972012-02-20 12:33:22 +0100874 /*
875 * Program traps generated by user-level software must
876 * be handled by the guest kernel.
877 *
878 * In GS mode, hypervisor privileged instructions trap
879 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
880 * actual program interrupts, handled by the guest.
881 */
Liu Yudaf5e272010-02-02 19:44:35 +0800882 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500883 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600884 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500885 break;
886 }
887
Scott Woodd30f6e42011-12-20 15:34:43 +0000888 r = emulation_exit(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500889 break;
890
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200891 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600892 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600893 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200894 r = RESUME_GUEST;
895 break;
896
Scott Wood4cd35f672011-06-14 18:34:31 -0500897#ifdef CONFIG_SPE
898 case BOOKE_INTERRUPT_SPE_UNAVAIL: {
899 if (vcpu->arch.shared->msr & MSR_SPE)
900 kvmppc_vcpu_enable_spe(vcpu);
901 else
902 kvmppc_booke_queue_irqprio(vcpu,
903 BOOKE_IRQPRIO_SPE_UNAVAIL);
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600904 r = RESUME_GUEST;
905 break;
Scott Wood4cd35f672011-06-14 18:34:31 -0500906 }
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600907
908 case BOOKE_INTERRUPT_SPE_FP_DATA:
909 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
910 r = RESUME_GUEST;
911 break;
912
913 case BOOKE_INTERRUPT_SPE_FP_ROUND:
914 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
915 r = RESUME_GUEST;
916 break;
Scott Wood4cd35f672011-06-14 18:34:31 -0500917#else
918 case BOOKE_INTERRUPT_SPE_UNAVAIL:
919 /*
920 * Guest wants SPE, but host kernel doesn't support it. Send
921 * an "unimplemented operation" program check to the guest.
922 */
923 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
924 r = RESUME_GUEST;
925 break;
926
927 /*
928 * These really should never happen without CONFIG_SPE,
929 * as we should never enable the real MSR[SPE] in the guest.
930 */
931 case BOOKE_INTERRUPT_SPE_FP_DATA:
932 case BOOKE_INTERRUPT_SPE_FP_ROUND:
933 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
934 __func__, exit_nr, vcpu->arch.pc);
935 run->hw.hardware_exit_reason = exit_nr;
936 r = RESUME_HOST;
937 break;
938#endif
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600939
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500940 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800941 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
942 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600943 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500944 r = RESUME_GUEST;
945 break;
946
947 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800948 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600949 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500950 r = RESUME_GUEST;
951 break;
952
Scott Woodd30f6e42011-12-20 15:34:43 +0000953#ifdef CONFIG_KVM_BOOKE_HV
954 case BOOKE_INTERRUPT_HV_SYSCALL:
955 if (!(vcpu->arch.shared->msr & MSR_PR)) {
956 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
957 } else {
958 /*
959 * hcall from guest userspace -- send privileged
960 * instruction program check.
961 */
962 kvmppc_core_queue_program(vcpu, ESR_PPR);
963 }
964
965 r = RESUME_GUEST;
966 break;
967#else
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500968 case BOOKE_INTERRUPT_SYSCALL:
Alexander Graf2a342ed2010-07-29 14:47:48 +0200969 if (!(vcpu->arch.shared->msr & MSR_PR) &&
970 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
971 /* KVM PV hypercalls */
972 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
973 r = RESUME_GUEST;
974 } else {
975 /* Guest syscalls */
976 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
977 }
Hollis Blanchard7b701592008-12-02 15:51:58 -0600978 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500979 r = RESUME_GUEST;
980 break;
Scott Woodd30f6e42011-12-20 15:34:43 +0000981#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500982
983 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500984 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600985 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600986 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500987 gfn_t gfn;
988
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000989#ifdef CONFIG_KVM_E500V2
Scott Wooda4cd8b22011-06-14 18:34:41 -0500990 if (!(vcpu->arch.shared->msr & MSR_PR) &&
991 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
992 kvmppc_map_magic(vcpu);
993 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
994 r = RESUME_GUEST;
995
996 break;
997 }
998#endif
999
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001000 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -06001001 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001002 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001003 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +08001004 kvmppc_core_queue_dtlb_miss(vcpu,
1005 vcpu->arch.fault_dear,
1006 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -06001007 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001008 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001009 r = RESUME_GUEST;
1010 break;
1011 }
1012
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -06001013 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001014 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001015
1016 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1017 /* The guest TLB had a mapping, but the shadow TLB
1018 * didn't, and it is RAM. This could be because:
1019 * a) the entry is mapping the host kernel, or
1020 * b) the guest used a large mapping which we're faking
1021 * Either way, we need to satisfy the fault without
1022 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -06001023 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001024 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001025 r = RESUME_GUEST;
1026 } else {
1027 /* Guest has mapped and accessed a page which is not
1028 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001029 vcpu->arch.paddr_accessed = gpaddr;
Alexander Graf6020c0f2012-03-12 02:26:30 +01001030 vcpu->arch.vaddr_accessed = eaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001031 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001032 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001033 }
1034
1035 break;
1036 }
1037
1038 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001039 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -06001040 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001041 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001042 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001043
1044 r = RESUME_GUEST;
1045
1046 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -06001047 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001048 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001049 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001050 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -06001051 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001052 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001053 break;
1054 }
1055
Hollis Blanchard7b701592008-12-02 15:51:58 -06001056 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001057
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -06001058 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -06001059 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001060
1061 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1062 /* The guest TLB had a mapping, but the shadow TLB
1063 * didn't. This could be because:
1064 * a) the entry is mapping the host kernel, or
1065 * b) the guest used a large mapping which we're faking
1066 * Either way, we need to satisfy the fault without
1067 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -06001068 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001069 } else {
1070 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001071 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001072 }
1073
1074 break;
1075 }
1076
Hollis Blanchard6a0ab732008-07-25 13:54:49 -05001077 case BOOKE_INTERRUPT_DEBUG: {
1078 u32 dbsr;
1079
1080 vcpu->arch.pc = mfspr(SPRN_CSRR0);
1081
1082 /* clear IAC events in DBSR register */
1083 dbsr = mfspr(SPRN_DBSR);
1084 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
1085 mtspr(SPRN_DBSR, dbsr);
1086
1087 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -06001088 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -05001089 r = RESUME_HOST;
1090 break;
1091 }
1092
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001093 default:
1094 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
1095 BUG();
1096 }
1097
Alexander Grafa8e4ef82012-02-16 14:07:37 +00001098 /*
1099 * To avoid clobbering exit_reason, only check for signals if we
1100 * aren't already exiting to userspace for some other reason.
1101 */
Alexander Graf03660ba2012-02-28 12:00:41 +01001102 if (!(r & RESUME_HOST)) {
1103 local_irq_disable();
Alexander Graf7ee78852012-08-13 12:44:41 +02001104 s = kvmppc_prepare_to_enter(vcpu);
1105 if (s <= 0) {
Alexander Graf24afa372012-08-12 12:42:30 +02001106 local_irq_enable();
Alexander Graf7ee78852012-08-13 12:44:41 +02001107 r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
Alexander Graf24afa372012-08-12 12:42:30 +02001108 } else {
Alexander Grafbd2be682012-08-13 01:04:19 +02001109 kvmppc_lazy_ee_enable();
Alexander Graf03660ba2012-02-28 12:00:41 +01001110 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001111 }
1112
1113 return r;
1114}
1115
1116/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
1117int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1118{
Hollis Blanchard082decf2010-08-07 10:33:56 -07001119 int i;
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001120 int r;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001121
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001122 vcpu->arch.pc = 0;
Scott Woodb5904972011-11-08 18:23:30 -06001123 vcpu->arch.shared->pir = vcpu->vcpu_id;
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001124 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Scott Woodd30f6e42011-12-20 15:34:43 +00001125 kvmppc_set_msr(vcpu, 0);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001126
Scott Woodd30f6e42011-12-20 15:34:43 +00001127#ifndef CONFIG_KVM_BOOKE_HV
1128 vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001129 vcpu->arch.shadow_pid = 1;
Scott Woodd30f6e42011-12-20 15:34:43 +00001130 vcpu->arch.shared->msr = 0;
1131#endif
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001132
Hollis Blanchard082decf2010-08-07 10:33:56 -07001133 /* Eye-catching numbers so we know if the guest takes an interrupt
1134 * before it's programmed its own IVPR/IVORs. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001135 vcpu->arch.ivpr = 0x55550000;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001136 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
1137 vcpu->arch.ivor[i] = 0x7700 | i * 4;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001138
Hollis Blanchard73e75b42008-12-02 15:51:57 -06001139 kvmppc_init_timing_stats(vcpu);
1140
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001141 r = kvmppc_core_vcpu_setup(vcpu);
1142 kvmppc_sanity_check(vcpu);
1143 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001144}
1145
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001146int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
1147{
1148 /* setup watchdog timer once */
1149 spin_lock_init(&vcpu->arch.wdt_lock);
1150 setup_timer(&vcpu->arch.wdt_timer, kvmppc_watchdog_func,
1151 (unsigned long)vcpu);
1152
1153 return 0;
1154}
1155
1156void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
1157{
1158 del_timer_sync(&vcpu->arch.wdt_timer);
1159}
1160
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001161int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1162{
1163 int i;
1164
1165 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001166 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001167 regs->ctr = vcpu->arch.ctr;
1168 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001169 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001170 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +02001171 regs->srr0 = vcpu->arch.shared->srr0;
1172 regs->srr1 = vcpu->arch.shared->srr1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001173 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001174 regs->sprg0 = vcpu->arch.shared->sprg0;
1175 regs->sprg1 = vcpu->arch.shared->sprg1;
1176 regs->sprg2 = vcpu->arch.shared->sprg2;
1177 regs->sprg3 = vcpu->arch.shared->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -06001178 regs->sprg4 = vcpu->arch.shared->sprg4;
1179 regs->sprg5 = vcpu->arch.shared->sprg5;
1180 regs->sprg6 = vcpu->arch.shared->sprg6;
1181 regs->sprg7 = vcpu->arch.shared->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001182
1183 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001184 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001185
1186 return 0;
1187}
1188
1189int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1190{
1191 int i;
1192
1193 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001194 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001195 vcpu->arch.ctr = regs->ctr;
1196 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001197 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -06001198 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +02001199 vcpu->arch.shared->srr0 = regs->srr0;
1200 vcpu->arch.shared->srr1 = regs->srr1;
Scott Wood5ce941e2011-04-27 17:24:21 -05001201 kvmppc_set_pid(vcpu, regs->pid);
Alexander Grafa73a9592010-07-29 14:47:47 +02001202 vcpu->arch.shared->sprg0 = regs->sprg0;
1203 vcpu->arch.shared->sprg1 = regs->sprg1;
1204 vcpu->arch.shared->sprg2 = regs->sprg2;
1205 vcpu->arch.shared->sprg3 = regs->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -06001206 vcpu->arch.shared->sprg4 = regs->sprg4;
1207 vcpu->arch.shared->sprg5 = regs->sprg5;
1208 vcpu->arch.shared->sprg6 = regs->sprg6;
1209 vcpu->arch.shared->sprg7 = regs->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001210
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001211 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1212 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001213
1214 return 0;
1215}
1216
Scott Wood5ce941e2011-04-27 17:24:21 -05001217static void get_sregs_base(struct kvm_vcpu *vcpu,
1218 struct kvm_sregs *sregs)
1219{
1220 u64 tb = get_tb();
1221
1222 sregs->u.e.features |= KVM_SREGS_E_BASE;
1223
1224 sregs->u.e.csrr0 = vcpu->arch.csrr0;
1225 sregs->u.e.csrr1 = vcpu->arch.csrr1;
1226 sregs->u.e.mcsr = vcpu->arch.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +00001227 sregs->u.e.esr = get_guest_esr(vcpu);
1228 sregs->u.e.dear = get_guest_dear(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001229 sregs->u.e.tsr = vcpu->arch.tsr;
1230 sregs->u.e.tcr = vcpu->arch.tcr;
1231 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
1232 sregs->u.e.tb = tb;
1233 sregs->u.e.vrsave = vcpu->arch.vrsave;
1234}
1235
1236static int set_sregs_base(struct kvm_vcpu *vcpu,
1237 struct kvm_sregs *sregs)
1238{
1239 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
1240 return 0;
1241
1242 vcpu->arch.csrr0 = sregs->u.e.csrr0;
1243 vcpu->arch.csrr1 = sregs->u.e.csrr1;
1244 vcpu->arch.mcsr = sregs->u.e.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +00001245 set_guest_esr(vcpu, sregs->u.e.esr);
1246 set_guest_dear(vcpu, sregs->u.e.dear);
Scott Wood5ce941e2011-04-27 17:24:21 -05001247 vcpu->arch.vrsave = sregs->u.e.vrsave;
Scott Wooddfd4d472011-11-17 12:39:59 +00001248 kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001249
Scott Wooddfd4d472011-11-17 12:39:59 +00001250 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
Scott Wood5ce941e2011-04-27 17:24:21 -05001251 vcpu->arch.dec = sregs->u.e.dec;
Scott Wooddfd4d472011-11-17 12:39:59 +00001252 kvmppc_emulate_dec(vcpu);
1253 }
Scott Wood5ce941e2011-04-27 17:24:21 -05001254
1255 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001256 u32 old_tsr = vcpu->arch.tsr;
1257
Scott Wooddfd4d472011-11-17 12:39:59 +00001258 vcpu->arch.tsr = sregs->u.e.tsr;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001259
1260 if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS))
1261 arm_next_watchdog(vcpu);
1262
Scott Wooddfd4d472011-11-17 12:39:59 +00001263 update_timer_ints(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001264 }
1265
1266 return 0;
1267}
1268
1269static void get_sregs_arch206(struct kvm_vcpu *vcpu,
1270 struct kvm_sregs *sregs)
1271{
1272 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
1273
Scott Wood841741f2011-09-02 17:39:37 -05001274 sregs->u.e.pir = vcpu->vcpu_id;
Scott Wood5ce941e2011-04-27 17:24:21 -05001275 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
1276 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
1277 sregs->u.e.decar = vcpu->arch.decar;
1278 sregs->u.e.ivpr = vcpu->arch.ivpr;
1279}
1280
1281static int set_sregs_arch206(struct kvm_vcpu *vcpu,
1282 struct kvm_sregs *sregs)
1283{
1284 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
1285 return 0;
1286
Scott Wood841741f2011-09-02 17:39:37 -05001287 if (sregs->u.e.pir != vcpu->vcpu_id)
Scott Wood5ce941e2011-04-27 17:24:21 -05001288 return -EINVAL;
1289
1290 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
1291 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
1292 vcpu->arch.decar = sregs->u.e.decar;
1293 vcpu->arch.ivpr = sregs->u.e.ivpr;
1294
1295 return 0;
1296}
1297
1298void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1299{
1300 sregs->u.e.features |= KVM_SREGS_E_IVOR;
1301
1302 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
1303 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
1304 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
1305 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
1306 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
1307 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
1308 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
1309 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
1310 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
1311 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
1312 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
1313 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
1314 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
1315 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
1316 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
1317 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
1318}
1319
1320int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1321{
1322 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
1323 return 0;
1324
1325 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
1326 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
1327 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
1328 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
1329 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
1330 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
1331 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
1332 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
1333 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
1334 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
1335 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
1336 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
1337 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
1338 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
1339 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
1340 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
1341
1342 return 0;
1343}
1344
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001345int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1346 struct kvm_sregs *sregs)
1347{
Scott Wood5ce941e2011-04-27 17:24:21 -05001348 sregs->pvr = vcpu->arch.pvr;
1349
1350 get_sregs_base(vcpu, sregs);
1351 get_sregs_arch206(vcpu, sregs);
1352 kvmppc_core_get_sregs(vcpu, sregs);
1353 return 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001354}
1355
1356int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1357 struct kvm_sregs *sregs)
1358{
Scott Wood5ce941e2011-04-27 17:24:21 -05001359 int ret;
1360
1361 if (vcpu->arch.pvr != sregs->pvr)
1362 return -EINVAL;
1363
1364 ret = set_sregs_base(vcpu, sregs);
1365 if (ret < 0)
1366 return ret;
1367
1368 ret = set_sregs_arch206(vcpu, sregs);
1369 if (ret < 0)
1370 return ret;
1371
1372 return kvmppc_core_set_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001373}
1374
Paul Mackerras31f34382011-12-12 12:26:50 +00001375int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1376{
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001377 int r = -EINVAL;
1378
1379 switch (reg->id) {
1380 case KVM_REG_PPC_IAC1:
1381 case KVM_REG_PPC_IAC2:
1382 case KVM_REG_PPC_IAC3:
1383 case KVM_REG_PPC_IAC4: {
1384 int iac = reg->id - KVM_REG_PPC_IAC1;
1385 r = copy_to_user((u64 __user *)(long)reg->addr,
1386 &vcpu->arch.dbg_reg.iac[iac], sizeof(u64));
1387 break;
1388 }
1389 case KVM_REG_PPC_DAC1:
1390 case KVM_REG_PPC_DAC2: {
1391 int dac = reg->id - KVM_REG_PPC_DAC1;
1392 r = copy_to_user((u64 __user *)(long)reg->addr,
1393 &vcpu->arch.dbg_reg.dac[dac], sizeof(u64));
1394 break;
1395 }
Mihai Caraman352df1d2012-10-11 06:13:29 +00001396#if defined(CONFIG_64BIT)
1397 case KVM_REG_PPC_EPCR:
1398 r = put_user(vcpu->arch.epcr, (u32 __user *)(long)reg->addr);
1399 break;
1400#endif
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001401 default:
1402 break;
1403 }
1404 return r;
Paul Mackerras31f34382011-12-12 12:26:50 +00001405}
1406
1407int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1408{
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001409 int r = -EINVAL;
1410
1411 switch (reg->id) {
1412 case KVM_REG_PPC_IAC1:
1413 case KVM_REG_PPC_IAC2:
1414 case KVM_REG_PPC_IAC3:
1415 case KVM_REG_PPC_IAC4: {
1416 int iac = reg->id - KVM_REG_PPC_IAC1;
1417 r = copy_from_user(&vcpu->arch.dbg_reg.iac[iac],
1418 (u64 __user *)(long)reg->addr, sizeof(u64));
1419 break;
1420 }
1421 case KVM_REG_PPC_DAC1:
1422 case KVM_REG_PPC_DAC2: {
1423 int dac = reg->id - KVM_REG_PPC_DAC1;
1424 r = copy_from_user(&vcpu->arch.dbg_reg.dac[dac],
1425 (u64 __user *)(long)reg->addr, sizeof(u64));
1426 break;
1427 }
Mihai Caraman352df1d2012-10-11 06:13:29 +00001428#if defined(CONFIG_64BIT)
1429 case KVM_REG_PPC_EPCR: {
1430 u32 new_epcr;
1431 r = get_user(new_epcr, (u32 __user *)(long)reg->addr);
1432 if (r == 0)
1433 kvmppc_set_epcr(vcpu, new_epcr);
1434 break;
1435 }
1436#endif
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001437 default:
1438 break;
1439 }
1440 return r;
Paul Mackerras31f34382011-12-12 12:26:50 +00001441}
1442
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001443int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1444{
1445 return -ENOTSUPP;
1446}
1447
1448int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1449{
1450 return -ENOTSUPP;
1451}
1452
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001453int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1454 struct kvm_translation *tr)
1455{
Avi Kivity98001d82010-05-13 11:05:49 +03001456 int r;
1457
Avi Kivity98001d82010-05-13 11:05:49 +03001458 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +03001459 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001460}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001461
Alexander Graf4e755752009-10-30 05:47:01 +00001462int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1463{
1464 return -ENOTSUPP;
1465}
1466
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001467void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1468 struct kvm_memory_slot *dont)
1469{
1470}
1471
1472int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1473 unsigned long npages)
1474{
1475 return 0;
1476}
1477
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001478int kvmppc_core_prepare_memory_region(struct kvm *kvm,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001479 struct kvm_memory_slot *memslot,
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001480 struct kvm_userspace_memory_region *mem)
1481{
1482 return 0;
1483}
1484
1485void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001486 struct kvm_userspace_memory_region *mem,
1487 struct kvm_memory_slot old)
1488{
1489}
1490
1491void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001492{
1493}
1494
Mihai Caraman38f98822012-10-11 06:13:27 +00001495void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr)
1496{
1497#if defined(CONFIG_64BIT)
1498 vcpu->arch.epcr = new_epcr;
1499#ifdef CONFIG_KVM_BOOKE_HV
1500 vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM;
1501 if (vcpu->arch.epcr & SPRN_EPCR_ICM)
1502 vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM;
1503#endif
1504#endif
1505}
1506
Scott Wooddfd4d472011-11-17 12:39:59 +00001507void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1508{
1509 vcpu->arch.tcr = new_tcr;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001510 arm_next_watchdog(vcpu);
Scott Wooddfd4d472011-11-17 12:39:59 +00001511 update_timer_ints(vcpu);
1512}
1513
1514void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1515{
1516 set_bits(tsr_bits, &vcpu->arch.tsr);
1517 smp_wmb();
1518 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1519 kvm_vcpu_kick(vcpu);
1520}
1521
1522void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1523{
1524 clear_bits(tsr_bits, &vcpu->arch.tsr);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001525
1526 /*
1527 * We may have stopped the watchdog due to
1528 * being stuck on final expiration.
1529 */
1530 if (tsr_bits & (TSR_ENW | TSR_WIS))
1531 arm_next_watchdog(vcpu);
1532
Scott Wooddfd4d472011-11-17 12:39:59 +00001533 update_timer_ints(vcpu);
1534}
1535
1536void kvmppc_decrementer_func(unsigned long data)
1537{
1538 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1539
Bharat Bhushan21bd0002012-05-20 23:21:23 +00001540 if (vcpu->arch.tcr & TCR_ARE) {
1541 vcpu->arch.dec = vcpu->arch.decar;
1542 kvmppc_emulate_dec(vcpu);
1543 }
1544
Scott Wooddfd4d472011-11-17 12:39:59 +00001545 kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1546}
1547
Scott Wood94fa9d92011-12-20 15:34:22 +00001548void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1549{
Paul Mackerrasa47d72f2012-09-20 19:35:51 +00001550 vcpu->cpu = smp_processor_id();
Scott Woodd30f6e42011-12-20 15:34:43 +00001551 current->thread.kvm_vcpu = vcpu;
Scott Wood94fa9d92011-12-20 15:34:22 +00001552}
1553
1554void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
1555{
Scott Woodd30f6e42011-12-20 15:34:43 +00001556 current->thread.kvm_vcpu = NULL;
Paul Mackerrasa47d72f2012-09-20 19:35:51 +00001557 vcpu->cpu = -1;
Scott Wood94fa9d92011-12-20 15:34:22 +00001558}
1559
Stephen Rothwell2986b8c2009-06-02 11:46:14 +10001560int __init kvmppc_booke_init(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001561{
Scott Woodd30f6e42011-12-20 15:34:43 +00001562#ifndef CONFIG_KVM_BOOKE_HV
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001563 unsigned long ivor[16];
1564 unsigned long max_ivor = 0;
1565 int i;
1566
1567 /* We install our own exception handlers by hijacking IVPR. IVPR must
1568 * be 16-bit aligned, so we need a 64KB allocation. */
1569 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1570 VCPU_SIZE_ORDER);
1571 if (!kvmppc_booke_handlers)
1572 return -ENOMEM;
1573
1574 /* XXX make sure our handlers are smaller than Linux's */
1575
1576 /* Copy our interrupt handlers to match host IVORs. That way we don't
1577 * have to swap the IVORs on every guest/host transition. */
1578 ivor[0] = mfspr(SPRN_IVOR0);
1579 ivor[1] = mfspr(SPRN_IVOR1);
1580 ivor[2] = mfspr(SPRN_IVOR2);
1581 ivor[3] = mfspr(SPRN_IVOR3);
1582 ivor[4] = mfspr(SPRN_IVOR4);
1583 ivor[5] = mfspr(SPRN_IVOR5);
1584 ivor[6] = mfspr(SPRN_IVOR6);
1585 ivor[7] = mfspr(SPRN_IVOR7);
1586 ivor[8] = mfspr(SPRN_IVOR8);
1587 ivor[9] = mfspr(SPRN_IVOR9);
1588 ivor[10] = mfspr(SPRN_IVOR10);
1589 ivor[11] = mfspr(SPRN_IVOR11);
1590 ivor[12] = mfspr(SPRN_IVOR12);
1591 ivor[13] = mfspr(SPRN_IVOR13);
1592 ivor[14] = mfspr(SPRN_IVOR14);
1593 ivor[15] = mfspr(SPRN_IVOR15);
1594
1595 for (i = 0; i < 16; i++) {
1596 if (ivor[i] > max_ivor)
1597 max_ivor = ivor[i];
1598
1599 memcpy((void *)kvmppc_booke_handlers + ivor[i],
1600 kvmppc_handlers_start + i * kvmppc_handler_len,
1601 kvmppc_handler_len);
1602 }
1603 flush_icache_range(kvmppc_booke_handlers,
1604 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
Scott Woodd30f6e42011-12-20 15:34:43 +00001605#endif /* !BOOKE_HV */
Hollis Blancharddb93f572008-11-05 09:36:18 -06001606 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001607}
1608
Hollis Blancharddb93f572008-11-05 09:36:18 -06001609void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001610{
1611 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
1612 kvm_exit();
1613}