blob: a983ccaf3cce23ea1edc583b7b3a1e5aa4813a16 [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>
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"
Aneesh Kumar K.Vdba291f2013-10-07 22:17:58 +053043
44#define CREATE_TRACE_POINTS
45#include "trace_booke.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050046
Hollis Blanchardd9fbd032008-11-05 09:36:13 -060047unsigned long kvmppc_booke_handlers;
48
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050049#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
50#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
51
52struct kvm_stats_debugfs_item debugfs_entries[] = {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050053 { "mmio", VCPU_STAT(mmio_exits) },
54 { "dcr", VCPU_STAT(dcr_exits) },
55 { "sig", VCPU_STAT(signal_exits) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050056 { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
57 { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
58 { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
59 { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
60 { "sysc", VCPU_STAT(syscall_exits) },
61 { "isi", VCPU_STAT(isi_exits) },
62 { "dsi", VCPU_STAT(dsi_exits) },
63 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
64 { "dec", VCPU_STAT(dec_exits) },
65 { "ext_intr", VCPU_STAT(ext_intr_exits) },
Hollis Blanchard45c5eb62008-04-25 17:55:49 -050066 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Scott Woodd30f6e42011-12-20 15:34:43 +000067 { "doorbell", VCPU_STAT(dbell_exits) },
68 { "guest doorbell", VCPU_STAT(gdbell_exits) },
Alexander Grafcf1c5ca2012-08-01 12:56:51 +020069 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050070 { NULL }
71};
72
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050073/* TODO: use vcpu_printf() */
74void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
75{
76 int i;
77
Alexander Graf666e7252010-07-29 14:47:43 +020078 printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060079 printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
Alexander Grafde7906c2010-07-29 14:47:46 +020080 printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
81 vcpu->arch.shared->srr1);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050082
83 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
84
85 for (i = 0; i < 32; i += 4) {
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060086 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
Alexander Graf8e5b26b2010-01-08 02:58:01 +010087 kvmppc_get_gpr(vcpu, i),
88 kvmppc_get_gpr(vcpu, i+1),
89 kvmppc_get_gpr(vcpu, i+2),
90 kvmppc_get_gpr(vcpu, i+3));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050091 }
92}
93
Scott Wood4cd35f62011-06-14 18:34:31 -050094#ifdef CONFIG_SPE
95void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
96{
97 preempt_disable();
98 enable_kernel_spe();
99 kvmppc_save_guest_spe(vcpu);
100 vcpu->arch.shadow_msr &= ~MSR_SPE;
101 preempt_enable();
102}
103
104static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
105{
106 preempt_disable();
107 enable_kernel_spe();
108 kvmppc_load_guest_spe(vcpu);
109 vcpu->arch.shadow_msr |= MSR_SPE;
110 preempt_enable();
111}
112
113static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
114{
115 if (vcpu->arch.shared->msr & MSR_SPE) {
116 if (!(vcpu->arch.shadow_msr & MSR_SPE))
117 kvmppc_vcpu_enable_spe(vcpu);
118 } else if (vcpu->arch.shadow_msr & MSR_SPE) {
119 kvmppc_vcpu_disable_spe(vcpu);
120 }
121}
122#else
123static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
124{
125}
126#endif
127
Alexander Graf7a08c272012-08-16 13:10:16 +0200128static void kvmppc_vcpu_sync_fpu(struct kvm_vcpu *vcpu)
129{
130#if defined(CONFIG_PPC_FPU) && !defined(CONFIG_KVM_BOOKE_HV)
131 /* We always treat the FP bit as enabled from the host
132 perspective, so only need to adjust the shadow MSR */
133 vcpu->arch.shadow_msr &= ~MSR_FP;
134 vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_FP;
135#endif
136}
137
Bharat Bhushance11e482013-07-04 12:27:47 +0530138static void kvmppc_vcpu_sync_debug(struct kvm_vcpu *vcpu)
139{
140 /* Synchronize guest's desire to get debug interrupts into shadow MSR */
141#ifndef CONFIG_KVM_BOOKE_HV
142 vcpu->arch.shadow_msr &= ~MSR_DE;
143 vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_DE;
144#endif
145
146 /* Force enable debug interrupts when user space wants to debug */
147 if (vcpu->guest_debug) {
148#ifdef CONFIG_KVM_BOOKE_HV
149 /*
150 * Since there is no shadow MSR, sync MSR_DE into the guest
151 * visible MSR.
152 */
153 vcpu->arch.shared->msr |= MSR_DE;
154#else
155 vcpu->arch.shadow_msr |= MSR_DE;
156 vcpu->arch.shared->msr &= ~MSR_DE;
157#endif
158 }
159}
160
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500161/*
162 * Helper function for "full" MSR writes. No need to call this if only
163 * EE/CE/ME/DE/RI are changing.
164 */
Scott Wood4cd35f62011-06-14 18:34:31 -0500165void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
166{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500167 u32 old_msr = vcpu->arch.shared->msr;
Scott Wood4cd35f62011-06-14 18:34:31 -0500168
Scott Woodd30f6e42011-12-20 15:34:43 +0000169#ifdef CONFIG_KVM_BOOKE_HV
170 new_msr |= MSR_GS;
171#endif
172
Scott Wood4cd35f62011-06-14 18:34:31 -0500173 vcpu->arch.shared->msr = new_msr;
174
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500175 kvmppc_mmu_msr_notify(vcpu, old_msr);
Scott Wood4cd35f62011-06-14 18:34:31 -0500176 kvmppc_vcpu_sync_spe(vcpu);
Alexander Graf7a08c272012-08-16 13:10:16 +0200177 kvmppc_vcpu_sync_fpu(vcpu);
Bharat Bhushance11e482013-07-04 12:27:47 +0530178 kvmppc_vcpu_sync_debug(vcpu);
Scott Wood4cd35f62011-06-14 18:34:31 -0500179}
180
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600181static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
182 unsigned int priority)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600183{
Alexander Graf63460462012-08-08 00:44:52 +0200184 trace_kvm_booke_queue_irqprio(vcpu, priority);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600185 set_bit(priority, &vcpu->arch.pending_exceptions);
186}
187
Liu Yudaf5e272010-02-02 19:44:35 +0800188static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
189 ulong dear_flags, ulong esr_flags)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600190{
Liu Yudaf5e272010-02-02 19:44:35 +0800191 vcpu->arch.queued_dear = dear_flags;
192 vcpu->arch.queued_esr = esr_flags;
193 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
194}
195
196static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
197 ulong dear_flags, ulong esr_flags)
198{
199 vcpu->arch.queued_dear = dear_flags;
200 vcpu->arch.queued_esr = esr_flags;
201 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
202}
203
204static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
205 ulong esr_flags)
206{
207 vcpu->arch.queued_esr = esr_flags;
208 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
209}
210
Alexander Graf011da892013-01-31 14:17:38 +0100211static void kvmppc_core_queue_alignment(struct kvm_vcpu *vcpu, ulong dear_flags,
212 ulong esr_flags)
213{
214 vcpu->arch.queued_dear = dear_flags;
215 vcpu->arch.queued_esr = esr_flags;
216 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALIGNMENT);
217}
218
Liu Yudaf5e272010-02-02 19:44:35 +0800219void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
220{
221 vcpu->arch.queued_esr = esr_flags;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600222 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600223}
224
225void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
226{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600227 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600228}
229
230int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
231{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600232 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600233}
234
Alexander Graf7706664d2009-12-21 20:21:24 +0100235void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
236{
237 clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
238}
239
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600240void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
241 struct kvm_interrupt *irq)
242{
Alexander Grafc5335f12010-08-30 14:03:24 +0200243 unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
244
245 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
246 prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
247
248 kvmppc_booke_queue_irqprio(vcpu, prio);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600249}
250
Paul Mackerras4fe27d22013-02-14 14:00:25 +0000251void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu)
Alexander Graf4496f972010-04-07 10:03:25 +0200252{
253 clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
Alexander Grafc5335f12010-08-30 14:03:24 +0200254 clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
Alexander Graf4496f972010-04-07 10:03:25 +0200255}
256
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000257static void kvmppc_core_queue_watchdog(struct kvm_vcpu *vcpu)
258{
259 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_WATCHDOG);
260}
261
262static void kvmppc_core_dequeue_watchdog(struct kvm_vcpu *vcpu)
263{
264 clear_bit(BOOKE_IRQPRIO_WATCHDOG, &vcpu->arch.pending_exceptions);
265}
266
Scott Woodd30f6e42011-12-20 15:34:43 +0000267static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
268{
269#ifdef CONFIG_KVM_BOOKE_HV
270 mtspr(SPRN_GSRR0, srr0);
271 mtspr(SPRN_GSRR1, srr1);
272#else
273 vcpu->arch.shared->srr0 = srr0;
274 vcpu->arch.shared->srr1 = srr1;
275#endif
276}
277
278static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
279{
280 vcpu->arch.csrr0 = srr0;
281 vcpu->arch.csrr1 = srr1;
282}
283
284static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
285{
286 if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
287 vcpu->arch.dsrr0 = srr0;
288 vcpu->arch.dsrr1 = srr1;
289 } else {
290 set_guest_csrr(vcpu, srr0, srr1);
291 }
292}
293
294static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
295{
296 vcpu->arch.mcsrr0 = srr0;
297 vcpu->arch.mcsrr1 = srr1;
298}
299
300static unsigned long get_guest_dear(struct kvm_vcpu *vcpu)
301{
302#ifdef CONFIG_KVM_BOOKE_HV
303 return mfspr(SPRN_GDEAR);
304#else
305 return vcpu->arch.shared->dar;
306#endif
307}
308
309static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear)
310{
311#ifdef CONFIG_KVM_BOOKE_HV
312 mtspr(SPRN_GDEAR, dear);
313#else
314 vcpu->arch.shared->dar = dear;
315#endif
316}
317
318static unsigned long get_guest_esr(struct kvm_vcpu *vcpu)
319{
320#ifdef CONFIG_KVM_BOOKE_HV
321 return mfspr(SPRN_GESR);
322#else
323 return vcpu->arch.shared->esr;
324#endif
325}
326
327static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr)
328{
329#ifdef CONFIG_KVM_BOOKE_HV
330 mtspr(SPRN_GESR, esr);
331#else
332 vcpu->arch.shared->esr = esr;
333#endif
334}
335
Alexander Graf324b3e62013-01-04 18:28:51 +0100336static unsigned long get_guest_epr(struct kvm_vcpu *vcpu)
337{
338#ifdef CONFIG_KVM_BOOKE_HV
339 return mfspr(SPRN_GEPR);
340#else
341 return vcpu->arch.epr;
342#endif
343}
344
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600345/* Deliver the interrupt of the corresponding priority, if possible. */
346static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
347 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500348{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600349 int allowed = 0;
Alexander Graf79300f82012-02-15 19:12:29 +0000350 ulong msr_mask = 0;
Alexander Graf1c810632013-01-04 18:12:48 +0100351 bool update_esr = false, update_dear = false, update_epr = false;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200352 ulong crit_raw = vcpu->arch.shared->critical;
353 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
354 bool crit;
Alexander Grafc5335f12010-08-30 14:03:24 +0200355 bool keep_irq = false;
Scott Woodd30f6e42011-12-20 15:34:43 +0000356 enum int_class int_class;
Mihai Caraman95e90b42012-10-11 06:13:26 +0000357 ulong new_msr = vcpu->arch.shared->msr;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200358
359 /* Truncate crit indicators in 32 bit mode */
360 if (!(vcpu->arch.shared->msr & MSR_SF)) {
361 crit_raw &= 0xffffffff;
362 crit_r1 &= 0xffffffff;
363 }
364
365 /* Critical section when crit == r1 */
366 crit = (crit_raw == crit_r1);
367 /* ... and we're in supervisor mode */
368 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500369
Alexander Grafc5335f12010-08-30 14:03:24 +0200370 if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
371 priority = BOOKE_IRQPRIO_EXTERNAL;
372 keep_irq = true;
373 }
374
Scott Wood5df554ad2013-04-12 14:08:46 +0000375 if ((priority == BOOKE_IRQPRIO_EXTERNAL) && vcpu->arch.epr_flags)
Alexander Graf1c810632013-01-04 18:12:48 +0100376 update_epr = true;
377
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600378 switch (priority) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600379 case BOOKE_IRQPRIO_DTLB_MISS:
Liu Yudaf5e272010-02-02 19:44:35 +0800380 case BOOKE_IRQPRIO_DATA_STORAGE:
Alexander Graf011da892013-01-31 14:17:38 +0100381 case BOOKE_IRQPRIO_ALIGNMENT:
Liu Yudaf5e272010-02-02 19:44:35 +0800382 update_dear = true;
383 /* fall through */
384 case BOOKE_IRQPRIO_INST_STORAGE:
385 case BOOKE_IRQPRIO_PROGRAM:
386 update_esr = true;
387 /* fall through */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600388 case BOOKE_IRQPRIO_ITLB_MISS:
389 case BOOKE_IRQPRIO_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600390 case BOOKE_IRQPRIO_FP_UNAVAIL:
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600391 case BOOKE_IRQPRIO_SPE_UNAVAIL:
392 case BOOKE_IRQPRIO_SPE_FP_DATA:
393 case BOOKE_IRQPRIO_SPE_FP_ROUND:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600394 case BOOKE_IRQPRIO_AP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600395 allowed = 1;
Alexander Graf79300f82012-02-15 19:12:29 +0000396 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000397 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500398 break;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000399 case BOOKE_IRQPRIO_WATCHDOG:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600400 case BOOKE_IRQPRIO_CRITICAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000401 case BOOKE_IRQPRIO_DBELL_CRIT:
Alexander Graf666e7252010-07-29 14:47:43 +0200402 allowed = vcpu->arch.shared->msr & MSR_CE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000403 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000404 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000405 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500406 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600407 case BOOKE_IRQPRIO_MACHINE_CHECK:
Alexander Graf666e7252010-07-29 14:47:43 +0200408 allowed = vcpu->arch.shared->msr & MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000409 allowed = allowed && !crit;
Scott Woodd30f6e42011-12-20 15:34:43 +0000410 int_class = INT_CLASS_MC;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500411 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600412 case BOOKE_IRQPRIO_DECREMENTER:
413 case BOOKE_IRQPRIO_FIT:
Scott Wooddfd4d472011-11-17 12:39:59 +0000414 keep_irq = true;
415 /* fall through */
416 case BOOKE_IRQPRIO_EXTERNAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000417 case BOOKE_IRQPRIO_DBELL:
Alexander Graf666e7252010-07-29 14:47:43 +0200418 allowed = vcpu->arch.shared->msr & MSR_EE;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200419 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000420 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000421 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500422 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600423 case BOOKE_IRQPRIO_DEBUG:
Alexander Graf666e7252010-07-29 14:47:43 +0200424 allowed = vcpu->arch.shared->msr & MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000425 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000426 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000427 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500428 break;
429 }
430
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600431 if (allowed) {
Scott Woodd30f6e42011-12-20 15:34:43 +0000432 switch (int_class) {
433 case INT_CLASS_NONCRIT:
434 set_guest_srr(vcpu, vcpu->arch.pc,
435 vcpu->arch.shared->msr);
436 break;
437 case INT_CLASS_CRIT:
438 set_guest_csrr(vcpu, vcpu->arch.pc,
439 vcpu->arch.shared->msr);
440 break;
441 case INT_CLASS_DBG:
442 set_guest_dsrr(vcpu, vcpu->arch.pc,
443 vcpu->arch.shared->msr);
444 break;
445 case INT_CLASS_MC:
446 set_guest_mcsrr(vcpu, vcpu->arch.pc,
447 vcpu->arch.shared->msr);
448 break;
449 }
450
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600451 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
Liu Yudaf5e272010-02-02 19:44:35 +0800452 if (update_esr == true)
Scott Woodd30f6e42011-12-20 15:34:43 +0000453 set_guest_esr(vcpu, vcpu->arch.queued_esr);
Liu Yudaf5e272010-02-02 19:44:35 +0800454 if (update_dear == true)
Scott Woodd30f6e42011-12-20 15:34:43 +0000455 set_guest_dear(vcpu, vcpu->arch.queued_dear);
Scott Wood5df554ad2013-04-12 14:08:46 +0000456 if (update_epr == true) {
457 if (vcpu->arch.epr_flags & KVMPPC_EPR_USER)
458 kvm_make_request(KVM_REQ_EPR_EXIT, vcpu);
Scott Woodeb1e4f42013-04-12 14:08:47 +0000459 else if (vcpu->arch.epr_flags & KVMPPC_EPR_KERNEL) {
460 BUG_ON(vcpu->arch.irq_type != KVMPPC_IRQ_MPIC);
461 kvmppc_mpic_set_epr(vcpu);
462 }
Scott Wood5df554ad2013-04-12 14:08:46 +0000463 }
Mihai Caraman95e90b42012-10-11 06:13:26 +0000464
465 new_msr &= msr_mask;
466#if defined(CONFIG_64BIT)
467 if (vcpu->arch.epcr & SPRN_EPCR_ICM)
468 new_msr |= MSR_CM;
469#endif
470 kvmppc_set_msr(vcpu, new_msr);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600471
Alexander Grafc5335f12010-08-30 14:03:24 +0200472 if (!keep_irq)
473 clear_bit(priority, &vcpu->arch.pending_exceptions);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600474 }
475
Scott Woodd30f6e42011-12-20 15:34:43 +0000476#ifdef CONFIG_KVM_BOOKE_HV
477 /*
478 * If an interrupt is pending but masked, raise a guest doorbell
479 * so that we are notified when the guest enables the relevant
480 * MSR bit.
481 */
482 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
483 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
484 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
485 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
486 if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
487 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
488#endif
489
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600490 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500491}
492
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000493/*
494 * Return the number of jiffies until the next timeout. If the timeout is
495 * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA
496 * because the larger value can break the timer APIs.
497 */
498static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu)
499{
500 u64 tb, wdt_tb, wdt_ticks = 0;
501 u64 nr_jiffies = 0;
502 u32 period = TCR_GET_WP(vcpu->arch.tcr);
503
504 wdt_tb = 1ULL << (63 - period);
505 tb = get_tb();
506 /*
507 * The watchdog timeout will hapeen when TB bit corresponding
508 * to watchdog will toggle from 0 to 1.
509 */
510 if (tb & wdt_tb)
511 wdt_ticks = wdt_tb;
512
513 wdt_ticks += wdt_tb - (tb & (wdt_tb - 1));
514
515 /* Convert timebase ticks to jiffies */
516 nr_jiffies = wdt_ticks;
517
518 if (do_div(nr_jiffies, tb_ticks_per_jiffy))
519 nr_jiffies++;
520
521 return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA);
522}
523
524static void arm_next_watchdog(struct kvm_vcpu *vcpu)
525{
526 unsigned long nr_jiffies;
527 unsigned long flags;
528
529 /*
530 * If TSR_ENW and TSR_WIS are not set then no need to exit to
531 * userspace, so clear the KVM_REQ_WATCHDOG request.
532 */
533 if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS))
534 clear_bit(KVM_REQ_WATCHDOG, &vcpu->requests);
535
536 spin_lock_irqsave(&vcpu->arch.wdt_lock, flags);
537 nr_jiffies = watchdog_next_timeout(vcpu);
538 /*
539 * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA
540 * then do not run the watchdog timer as this can break timer APIs.
541 */
542 if (nr_jiffies < NEXT_TIMER_MAX_DELTA)
543 mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies);
544 else
545 del_timer(&vcpu->arch.wdt_timer);
546 spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags);
547}
548
549void kvmppc_watchdog_func(unsigned long data)
550{
551 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
552 u32 tsr, new_tsr;
553 int final;
554
555 do {
556 new_tsr = tsr = vcpu->arch.tsr;
557 final = 0;
558
559 /* Time out event */
560 if (tsr & TSR_ENW) {
561 if (tsr & TSR_WIS)
562 final = 1;
563 else
564 new_tsr = tsr | TSR_WIS;
565 } else {
566 new_tsr = tsr | TSR_ENW;
567 }
568 } while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr);
569
570 if (new_tsr & TSR_WIS) {
571 smp_wmb();
572 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
573 kvm_vcpu_kick(vcpu);
574 }
575
576 /*
577 * If this is final watchdog expiry and some action is required
578 * then exit to userspace.
579 */
580 if (final && (vcpu->arch.tcr & TCR_WRC_MASK) &&
581 vcpu->arch.watchdog_enabled) {
582 smp_wmb();
583 kvm_make_request(KVM_REQ_WATCHDOG, vcpu);
584 kvm_vcpu_kick(vcpu);
585 }
586
587 /*
588 * Stop running the watchdog timer after final expiration to
589 * prevent the host from being flooded with timers if the
590 * guest sets a short period.
591 * Timers will resume when TSR/TCR is updated next time.
592 */
593 if (!final)
594 arm_next_watchdog(vcpu);
595}
596
Scott Wooddfd4d472011-11-17 12:39:59 +0000597static void update_timer_ints(struct kvm_vcpu *vcpu)
598{
599 if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
600 kvmppc_core_queue_dec(vcpu);
601 else
602 kvmppc_core_dequeue_dec(vcpu);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000603
604 if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS))
605 kvmppc_core_queue_watchdog(vcpu);
606 else
607 kvmppc_core_dequeue_watchdog(vcpu);
Scott Wooddfd4d472011-11-17 12:39:59 +0000608}
609
Scott Woodc59a6a32011-11-08 18:23:25 -0600610static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500611{
612 unsigned long *pending = &vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500613 unsigned int priority;
614
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600615 priority = __ffs(*pending);
Alexander Graf8b3a00f2012-02-16 14:12:46 +0000616 while (priority < BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600617 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500618 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500619
620 priority = find_next_bit(pending,
621 BITS_PER_BYTE * sizeof(*pending),
622 priority + 1);
623 }
Alexander Graf90bba352010-07-29 14:47:51 +0200624
625 /* Tell the guest about our interrupt status */
Scott Wood29ac26e2011-11-08 18:23:27 -0600626 vcpu->arch.shared->int_pending = !!*pending;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500627}
628
Scott Woodc59a6a32011-11-08 18:23:25 -0600629/* Check pending exceptions and deliver one, if possible. */
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000630int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
Scott Woodc59a6a32011-11-08 18:23:25 -0600631{
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000632 int r = 0;
Scott Woodc59a6a32011-11-08 18:23:25 -0600633 WARN_ON_ONCE(!irqs_disabled());
634
635 kvmppc_core_check_exceptions(vcpu);
636
Alexander Grafb8c649a2012-12-20 04:52:39 +0000637 if (vcpu->requests) {
638 /* Exception delivery raised request; start over */
639 return 1;
640 }
641
Scott Woodc59a6a32011-11-08 18:23:25 -0600642 if (vcpu->arch.shared->msr & MSR_WE) {
643 local_irq_enable();
644 kvm_vcpu_block(vcpu);
Alexander Graf966cd0f2012-03-14 16:55:08 +0100645 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Scott Woodc59a6a32011-11-08 18:23:25 -0600646 local_irq_disable();
647
648 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000649 r = 1;
Scott Woodc59a6a32011-11-08 18:23:25 -0600650 };
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000651
652 return r;
653}
654
Alexander Graf7c973a22012-08-13 12:50:35 +0200655int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
Alexander Graf4ffc6352012-08-08 20:31:13 +0200656{
Alexander Graf7c973a22012-08-13 12:50:35 +0200657 int r = 1; /* Indicate we want to get back into the guest */
658
Alexander Graf2d8185d2012-08-10 12:31:12 +0200659 if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
660 update_timer_ints(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200661#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Alexander Graf2d8185d2012-08-10 12:31:12 +0200662 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
663 kvmppc_core_flush_tlb(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200664#endif
Alexander Graf7c973a22012-08-13 12:50:35 +0200665
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000666 if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) {
667 vcpu->run->exit_reason = KVM_EXIT_WATCHDOG;
668 r = 0;
669 }
670
Alexander Graf1c810632013-01-04 18:12:48 +0100671 if (kvm_check_request(KVM_REQ_EPR_EXIT, vcpu)) {
672 vcpu->run->epr.epr = 0;
673 vcpu->arch.epr_needed = true;
674 vcpu->run->exit_reason = KVM_EXIT_EPR;
675 r = 0;
676 }
677
Alexander Graf7c973a22012-08-13 12:50:35 +0200678 return r;
Alexander Graf4ffc6352012-08-08 20:31:13 +0200679}
680
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000681int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
682{
Alexander Graf7ee78852012-08-13 12:44:41 +0200683 int ret, s;
Bharat Bhushance11e482013-07-04 12:27:47 +0530684 struct thread_struct thread;
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000685
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200686 if (!vcpu->arch.sane) {
687 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
688 return -EINVAL;
689 }
690
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000691 local_irq_disable();
Alexander Graf7ee78852012-08-13 12:44:41 +0200692 s = kvmppc_prepare_to_enter(vcpu);
693 if (s <= 0) {
Alexander Graf24afa372012-08-12 12:42:30 +0200694 local_irq_enable();
Alexander Graf7ee78852012-08-13 12:44:41 +0200695 ret = s;
Scott Wood1d1ef222011-11-08 16:11:59 -0600696 goto out;
697 }
698
Scott Wood8fae8452011-12-20 15:34:45 +0000699#ifdef CONFIG_PPC_FPU
700 /* Save userspace FPU state in stack */
701 enable_kernel_fp();
Scott Wood8fae8452011-12-20 15:34:45 +0000702
703 /*
704 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
705 * as always using the FPU. Kernel usage of FP (via
706 * enable_kernel_fp()) in this thread must not occur while
707 * vcpu->fpu_active is set.
708 */
709 vcpu->fpu_active = 1;
710
711 kvmppc_load_guest_fp(vcpu);
712#endif
713
Bharat Bhushance11e482013-07-04 12:27:47 +0530714 /* Switch to guest debug context */
715 thread.debug = vcpu->arch.shadow_dbg_reg;
716 switch_booke_debug_regs(&thread);
717 thread.debug = current->thread.debug;
718 current->thread.debug = vcpu->arch.shadow_dbg_reg;
719
Scott Wood5f1c2482013-07-10 17:47:39 -0500720 kvmppc_fix_ee_before_entry();
Scott Woodf8941fbe2013-06-11 11:38:31 -0500721
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000722 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
Scott Wood8fae8452011-12-20 15:34:45 +0000723
Alexander Graf24afa372012-08-12 12:42:30 +0200724 /* No need for kvm_guest_exit. It's done in handle_exit.
725 We also get here with interrupts enabled. */
726
Bharat Bhushance11e482013-07-04 12:27:47 +0530727 /* Switch back to user space debug context */
728 switch_booke_debug_regs(&thread);
729 current->thread.debug = thread.debug;
730
Scott Wood8fae8452011-12-20 15:34:45 +0000731#ifdef CONFIG_PPC_FPU
732 kvmppc_save_guest_fp(vcpu);
733
734 vcpu->fpu_active = 0;
Scott Wood8fae8452011-12-20 15:34:45 +0000735#endif
736
Scott Wood1d1ef222011-11-08 16:11:59 -0600737out:
Alexander Grafd69c6432012-08-08 20:44:20 +0200738 vcpu->mode = OUTSIDE_GUEST_MODE;
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000739 return ret;
740}
741
Scott Woodd30f6e42011-12-20 15:34:43 +0000742static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
743{
744 enum emulation_result er;
745
746 er = kvmppc_emulate_instruction(run, vcpu);
747 switch (er) {
748 case EMULATE_DONE:
749 /* don't overwrite subtypes, just account kvm_stats */
750 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
751 /* Future optimization: only reload non-volatiles if
752 * they were actually modified by emulation. */
753 return RESUME_GUEST_NV;
754
755 case EMULATE_DO_DCR:
756 run->exit_reason = KVM_EXIT_DCR;
757 return RESUME_HOST;
758
759 case EMULATE_FAIL:
Scott Woodd30f6e42011-12-20 15:34:43 +0000760 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
761 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
762 /* For debugging, encode the failing instruction and
763 * report it to userspace. */
764 run->hw.hardware_exit_reason = ~0ULL << 32;
765 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
Alexander Grafd1ff5492012-02-16 13:24:03 +0000766 kvmppc_core_queue_program(vcpu, ESR_PIL);
Scott Woodd30f6e42011-12-20 15:34:43 +0000767 return RESUME_HOST;
768
Bharat Bhushan9b4f5302013-04-08 00:32:15 +0000769 case EMULATE_EXIT_USER:
770 return RESUME_HOST;
771
Scott Woodd30f6e42011-12-20 15:34:43 +0000772 default:
773 BUG();
774 }
775}
776
Bharat Bhushance11e482013-07-04 12:27:47 +0530777static int kvmppc_handle_debug(struct kvm_run *run, struct kvm_vcpu *vcpu)
778{
779 struct debug_reg *dbg_reg = &(vcpu->arch.shadow_dbg_reg);
780 u32 dbsr = vcpu->arch.dbsr;
781
782 run->debug.arch.status = 0;
783 run->debug.arch.address = vcpu->arch.pc;
784
785 if (dbsr & (DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4)) {
786 run->debug.arch.status |= KVMPPC_DEBUG_BREAKPOINT;
787 } else {
788 if (dbsr & (DBSR_DAC1W | DBSR_DAC2W))
789 run->debug.arch.status |= KVMPPC_DEBUG_WATCH_WRITE;
790 else if (dbsr & (DBSR_DAC1R | DBSR_DAC2R))
791 run->debug.arch.status |= KVMPPC_DEBUG_WATCH_READ;
792 if (dbsr & (DBSR_DAC1R | DBSR_DAC1W))
793 run->debug.arch.address = dbg_reg->dac1;
794 else if (dbsr & (DBSR_DAC2R | DBSR_DAC2W))
795 run->debug.arch.address = dbg_reg->dac2;
796 }
797
798 return RESUME_HOST;
799}
800
Alexander Graf4e642cc2012-02-20 23:57:26 +0100801static void kvmppc_fill_pt_regs(struct pt_regs *regs)
802{
803 ulong r1, ip, msr, lr;
804
805 asm("mr %0, 1" : "=r"(r1));
806 asm("mflr %0" : "=r"(lr));
807 asm("mfmsr %0" : "=r"(msr));
808 asm("bl 1f; 1: mflr %0" : "=r"(ip));
809
810 memset(regs, 0, sizeof(*regs));
811 regs->gpr[1] = r1;
812 regs->nip = ip;
813 regs->msr = msr;
814 regs->link = lr;
815}
816
Bharat Bhushan6328e592012-06-20 05:56:53 +0000817/*
818 * For interrupts needed to be handled by host interrupt handlers,
819 * corresponding host handler are called from here in similar way
820 * (but not exact) as they are called from low level handler
821 * (such as from arch/powerpc/kernel/head_fsl_booke.S).
822 */
Alexander Graf4e642cc2012-02-20 23:57:26 +0100823static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
824 unsigned int exit_nr)
825{
826 struct pt_regs regs;
827
828 switch (exit_nr) {
829 case BOOKE_INTERRUPT_EXTERNAL:
830 kvmppc_fill_pt_regs(&regs);
831 do_IRQ(&regs);
832 break;
833 case BOOKE_INTERRUPT_DECREMENTER:
834 kvmppc_fill_pt_regs(&regs);
835 timer_interrupt(&regs);
836 break;
Tiejun Chen5f17ce82013-05-13 10:00:45 +0800837#if defined(CONFIG_PPC_DOORBELL)
Alexander Graf4e642cc2012-02-20 23:57:26 +0100838 case BOOKE_INTERRUPT_DOORBELL:
839 kvmppc_fill_pt_regs(&regs);
840 doorbell_exception(&regs);
841 break;
842#endif
843 case BOOKE_INTERRUPT_MACHINE_CHECK:
844 /* FIXME */
845 break;
Alexander Graf7cc1e8e2012-02-22 16:26:34 +0100846 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
847 kvmppc_fill_pt_regs(&regs);
848 performance_monitor_exception(&regs);
849 break;
Bharat Bhushan6328e592012-06-20 05:56:53 +0000850 case BOOKE_INTERRUPT_WATCHDOG:
851 kvmppc_fill_pt_regs(&regs);
852#ifdef CONFIG_BOOKE_WDT
853 WatchdogException(&regs);
854#else
855 unknown_exception(&regs);
856#endif
857 break;
858 case BOOKE_INTERRUPT_CRITICAL:
859 unknown_exception(&regs);
860 break;
Bharat Bhushance11e482013-07-04 12:27:47 +0530861 case BOOKE_INTERRUPT_DEBUG:
862 /* Save DBSR before preemption is enabled */
863 vcpu->arch.dbsr = mfspr(SPRN_DBSR);
864 kvmppc_clear_dbsr();
865 break;
Alexander Graf4e642cc2012-02-20 23:57:26 +0100866 }
867}
868
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500869/**
870 * kvmppc_handle_exit
871 *
872 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
873 */
874int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
875 unsigned int exit_nr)
876{
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500877 int r = RESUME_HOST;
Alexander Graf7ee78852012-08-13 12:44:41 +0200878 int s;
Scott Woodf1e89022013-06-06 19:16:31 -0500879 int idx;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500880
Scott Wood7c11c0c2013-06-06 19:16:32 -0500881#ifdef CONFIG_PPC64
882 WARN_ON(local_paca->irq_happened != 0);
883#endif
884
885 /*
886 * We enter with interrupts disabled in hardware, but
887 * we need to call hard_irq_disable anyway to ensure that
888 * the software state is kept in sync.
889 */
890 hard_irq_disable();
891
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600892 /* update before a new last_exit_type is rewritten */
893 kvmppc_update_timing_stats(vcpu);
894
Alexander Graf4e642cc2012-02-20 23:57:26 +0100895 /* restart interrupts if they were meant for the host */
896 kvmppc_restart_interrupt(vcpu, exit_nr);
Scott Woodd30f6e42011-12-20 15:34:43 +0000897
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500898 local_irq_enable();
899
Alexander Graf97c95052012-08-02 15:10:00 +0200900 trace_kvm_exit(exit_nr, vcpu);
Alexander Graf706fb732012-08-12 11:29:09 +0200901 kvm_guest_exit();
Alexander Graf97c95052012-08-02 15:10:00 +0200902
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500903 run->exit_reason = KVM_EXIT_UNKNOWN;
904 run->ready_for_interrupt_injection = 1;
905
906 switch (exit_nr) {
907 case BOOKE_INTERRUPT_MACHINE_CHECK:
Alexander Grafc35c9d82012-02-20 12:21:18 +0100908 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
909 kvmppc_dump_vcpu(vcpu);
910 /* For debugging, send invalid exit reason to user space */
911 run->hw.hardware_exit_reason = ~1ULL << 32;
912 run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
913 r = RESUME_HOST;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500914 break;
915
916 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600917 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -0600918 r = RESUME_GUEST;
919 break;
920
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500921 case BOOKE_INTERRUPT_DECREMENTER:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600922 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500923 r = RESUME_GUEST;
924 break;
925
Bharat Bhushan6328e592012-06-20 05:56:53 +0000926 case BOOKE_INTERRUPT_WATCHDOG:
927 r = RESUME_GUEST;
928 break;
929
Scott Woodd30f6e42011-12-20 15:34:43 +0000930 case BOOKE_INTERRUPT_DOORBELL:
931 kvmppc_account_exit(vcpu, DBELL_EXITS);
Scott Woodd30f6e42011-12-20 15:34:43 +0000932 r = RESUME_GUEST;
933 break;
934
935 case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
936 kvmppc_account_exit(vcpu, GDBELL_EXITS);
937
938 /*
939 * We are here because there is a pending guest interrupt
940 * which could not be delivered as MSR_CE or MSR_ME was not
941 * set. Once we break from here we will retry delivery.
942 */
943 r = RESUME_GUEST;
944 break;
945
946 case BOOKE_INTERRUPT_GUEST_DBELL:
947 kvmppc_account_exit(vcpu, GDBELL_EXITS);
948
949 /*
950 * We are here because there is a pending guest interrupt
951 * which could not be delivered as MSR_EE was not set. Once
952 * we break from here we will retry delivery.
953 */
954 r = RESUME_GUEST;
955 break;
956
Alexander Graf95f2e922012-02-20 22:45:12 +0100957 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
958 r = RESUME_GUEST;
959 break;
960
Scott Woodd30f6e42011-12-20 15:34:43 +0000961 case BOOKE_INTERRUPT_HV_PRIV:
962 r = emulation_exit(run, vcpu);
963 break;
964
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500965 case BOOKE_INTERRUPT_PROGRAM:
Scott Woodd30f6e42011-12-20 15:34:43 +0000966 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
Alexander Graf02685972012-02-20 12:33:22 +0100967 /*
968 * Program traps generated by user-level software must
969 * be handled by the guest kernel.
970 *
971 * In GS mode, hypervisor privileged instructions trap
972 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
973 * actual program interrupts, handled by the guest.
974 */
Liu Yudaf5e272010-02-02 19:44:35 +0800975 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500976 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600977 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500978 break;
979 }
980
Scott Woodd30f6e42011-12-20 15:34:43 +0000981 r = emulation_exit(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500982 break;
983
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200984 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600985 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600986 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200987 r = RESUME_GUEST;
988 break;
989
Scott Wood4cd35f62011-06-14 18:34:31 -0500990#ifdef CONFIG_SPE
991 case BOOKE_INTERRUPT_SPE_UNAVAIL: {
992 if (vcpu->arch.shared->msr & MSR_SPE)
993 kvmppc_vcpu_enable_spe(vcpu);
994 else
995 kvmppc_booke_queue_irqprio(vcpu,
996 BOOKE_IRQPRIO_SPE_UNAVAIL);
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600997 r = RESUME_GUEST;
998 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500999 }
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -06001000
1001 case BOOKE_INTERRUPT_SPE_FP_DATA:
1002 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
1003 r = RESUME_GUEST;
1004 break;
1005
1006 case BOOKE_INTERRUPT_SPE_FP_ROUND:
1007 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
1008 r = RESUME_GUEST;
1009 break;
Scott Wood4cd35f62011-06-14 18:34:31 -05001010#else
1011 case BOOKE_INTERRUPT_SPE_UNAVAIL:
1012 /*
1013 * Guest wants SPE, but host kernel doesn't support it. Send
1014 * an "unimplemented operation" program check to the guest.
1015 */
1016 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
1017 r = RESUME_GUEST;
1018 break;
1019
1020 /*
1021 * These really should never happen without CONFIG_SPE,
1022 * as we should never enable the real MSR[SPE] in the guest.
1023 */
1024 case BOOKE_INTERRUPT_SPE_FP_DATA:
1025 case BOOKE_INTERRUPT_SPE_FP_ROUND:
1026 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
1027 __func__, exit_nr, vcpu->arch.pc);
1028 run->hw.hardware_exit_reason = exit_nr;
1029 r = RESUME_HOST;
1030 break;
1031#endif
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -06001032
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001033 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +08001034 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
1035 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001036 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001037 r = RESUME_GUEST;
1038 break;
1039
1040 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +08001041 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001042 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001043 r = RESUME_GUEST;
1044 break;
1045
Alexander Graf011da892013-01-31 14:17:38 +01001046 case BOOKE_INTERRUPT_ALIGNMENT:
1047 kvmppc_core_queue_alignment(vcpu, vcpu->arch.fault_dear,
1048 vcpu->arch.fault_esr);
1049 r = RESUME_GUEST;
1050 break;
1051
Scott Woodd30f6e42011-12-20 15:34:43 +00001052#ifdef CONFIG_KVM_BOOKE_HV
1053 case BOOKE_INTERRUPT_HV_SYSCALL:
1054 if (!(vcpu->arch.shared->msr & MSR_PR)) {
1055 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1056 } else {
1057 /*
1058 * hcall from guest userspace -- send privileged
1059 * instruction program check.
1060 */
1061 kvmppc_core_queue_program(vcpu, ESR_PPR);
1062 }
1063
1064 r = RESUME_GUEST;
1065 break;
1066#else
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001067 case BOOKE_INTERRUPT_SYSCALL:
Alexander Graf2a342ed2010-07-29 14:47:48 +02001068 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1069 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
1070 /* KVM PV hypercalls */
1071 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1072 r = RESUME_GUEST;
1073 } else {
1074 /* Guest syscalls */
1075 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
1076 }
Hollis Blanchard7b701592008-12-02 15:51:58 -06001077 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001078 r = RESUME_GUEST;
1079 break;
Scott Woodd30f6e42011-12-20 15:34:43 +00001080#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001081
1082 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001083 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001084 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001085 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001086 gfn_t gfn;
1087
Alexander Grafbf7ca4b2012-02-15 23:40:00 +00001088#ifdef CONFIG_KVM_E500V2
Scott Wooda4cd8b22011-06-14 18:34:41 -05001089 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1090 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
1091 kvmppc_map_magic(vcpu);
1092 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
1093 r = RESUME_GUEST;
1094
1095 break;
1096 }
1097#endif
1098
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001099 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -06001100 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001101 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001102 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +08001103 kvmppc_core_queue_dtlb_miss(vcpu,
1104 vcpu->arch.fault_dear,
1105 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -06001106 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001107 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001108 r = RESUME_GUEST;
1109 break;
1110 }
1111
Scott Woodf1e89022013-06-06 19:16:31 -05001112 idx = srcu_read_lock(&vcpu->kvm->srcu);
1113
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -06001114 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001115 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001116
1117 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1118 /* The guest TLB had a mapping, but the shadow TLB
1119 * didn't, and it is RAM. This could be because:
1120 * a) the entry is mapping the host kernel, or
1121 * b) the guest used a large mapping which we're faking
1122 * Either way, we need to satisfy the fault without
1123 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -06001124 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001125 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001126 r = RESUME_GUEST;
1127 } else {
1128 /* Guest has mapped and accessed a page which is not
1129 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001130 vcpu->arch.paddr_accessed = gpaddr;
Alexander Graf6020c0f2012-03-12 02:26:30 +01001131 vcpu->arch.vaddr_accessed = eaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001132 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001133 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001134 }
1135
Scott Woodf1e89022013-06-06 19:16:31 -05001136 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001137 break;
1138 }
1139
1140 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001141 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -06001142 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001143 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001144 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001145
1146 r = RESUME_GUEST;
1147
1148 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -06001149 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001150 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001151 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001152 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -06001153 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001154 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001155 break;
1156 }
1157
Hollis Blanchard7b701592008-12-02 15:51:58 -06001158 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001159
Scott Woodf1e89022013-06-06 19:16:31 -05001160 idx = srcu_read_lock(&vcpu->kvm->srcu);
1161
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -06001162 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -06001163 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001164
1165 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1166 /* The guest TLB had a mapping, but the shadow TLB
1167 * didn't. This could be because:
1168 * a) the entry is mapping the host kernel, or
1169 * b) the guest used a large mapping which we're faking
1170 * Either way, we need to satisfy the fault without
1171 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -06001172 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001173 } else {
1174 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001175 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001176 }
1177
Scott Woodf1e89022013-06-06 19:16:31 -05001178 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001179 break;
1180 }
1181
Hollis Blanchard6a0ab732008-07-25 13:54:49 -05001182 case BOOKE_INTERRUPT_DEBUG: {
Bharat Bhushance11e482013-07-04 12:27:47 +05301183 r = kvmppc_handle_debug(run, vcpu);
1184 if (r == RESUME_HOST)
1185 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -06001186 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -05001187 break;
1188 }
1189
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001190 default:
1191 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
1192 BUG();
1193 }
1194
Alexander Grafa8e4ef82012-02-16 14:07:37 +00001195 /*
1196 * To avoid clobbering exit_reason, only check for signals if we
1197 * aren't already exiting to userspace for some other reason.
1198 */
Alexander Graf03660ba2012-02-28 12:00:41 +01001199 if (!(r & RESUME_HOST)) {
1200 local_irq_disable();
Alexander Graf7ee78852012-08-13 12:44:41 +02001201 s = kvmppc_prepare_to_enter(vcpu);
1202 if (s <= 0) {
Alexander Graf24afa372012-08-12 12:42:30 +02001203 local_irq_enable();
Alexander Graf7ee78852012-08-13 12:44:41 +02001204 r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
Alexander Graf24afa372012-08-12 12:42:30 +02001205 } else {
Scott Wood5f1c2482013-07-10 17:47:39 -05001206 kvmppc_fix_ee_before_entry();
Alexander Graf03660ba2012-02-28 12:00:41 +01001207 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001208 }
1209
1210 return r;
1211}
1212
Bharat Bhushand26f22c2013-02-24 18:57:11 +00001213static void kvmppc_set_tsr(struct kvm_vcpu *vcpu, u32 new_tsr)
1214{
1215 u32 old_tsr = vcpu->arch.tsr;
1216
1217 vcpu->arch.tsr = new_tsr;
1218
1219 if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS))
1220 arm_next_watchdog(vcpu);
1221
1222 update_timer_ints(vcpu);
1223}
1224
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001225/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
1226int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1227{
Hollis Blanchard082decf2010-08-07 10:33:56 -07001228 int i;
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001229 int r;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001230
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001231 vcpu->arch.pc = 0;
Scott Woodb5904972011-11-08 18:23:30 -06001232 vcpu->arch.shared->pir = vcpu->vcpu_id;
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001233 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Scott Woodd30f6e42011-12-20 15:34:43 +00001234 kvmppc_set_msr(vcpu, 0);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001235
Scott Woodd30f6e42011-12-20 15:34:43 +00001236#ifndef CONFIG_KVM_BOOKE_HV
Bharat Bhushance11e482013-07-04 12:27:47 +05301237 vcpu->arch.shadow_msr = MSR_USER | MSR_IS | MSR_DS;
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001238 vcpu->arch.shadow_pid = 1;
Scott Woodd30f6e42011-12-20 15:34:43 +00001239 vcpu->arch.shared->msr = 0;
1240#endif
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001241
Hollis Blanchard082decf2010-08-07 10:33:56 -07001242 /* Eye-catching numbers so we know if the guest takes an interrupt
1243 * before it's programmed its own IVPR/IVORs. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001244 vcpu->arch.ivpr = 0x55550000;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001245 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
1246 vcpu->arch.ivor[i] = 0x7700 | i * 4;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001247
Hollis Blanchard73e75b42008-12-02 15:51:57 -06001248 kvmppc_init_timing_stats(vcpu);
1249
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001250 r = kvmppc_core_vcpu_setup(vcpu);
1251 kvmppc_sanity_check(vcpu);
1252 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001253}
1254
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001255int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
1256{
1257 /* setup watchdog timer once */
1258 spin_lock_init(&vcpu->arch.wdt_lock);
1259 setup_timer(&vcpu->arch.wdt_timer, kvmppc_watchdog_func,
1260 (unsigned long)vcpu);
1261
1262 return 0;
1263}
1264
1265void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
1266{
1267 del_timer_sync(&vcpu->arch.wdt_timer);
1268}
1269
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001270int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1271{
1272 int i;
1273
1274 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001275 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001276 regs->ctr = vcpu->arch.ctr;
1277 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001278 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001279 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +02001280 regs->srr0 = vcpu->arch.shared->srr0;
1281 regs->srr1 = vcpu->arch.shared->srr1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001282 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001283 regs->sprg0 = vcpu->arch.shared->sprg0;
1284 regs->sprg1 = vcpu->arch.shared->sprg1;
1285 regs->sprg2 = vcpu->arch.shared->sprg2;
1286 regs->sprg3 = vcpu->arch.shared->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -06001287 regs->sprg4 = vcpu->arch.shared->sprg4;
1288 regs->sprg5 = vcpu->arch.shared->sprg5;
1289 regs->sprg6 = vcpu->arch.shared->sprg6;
1290 regs->sprg7 = vcpu->arch.shared->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001291
1292 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001293 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001294
1295 return 0;
1296}
1297
1298int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1299{
1300 int i;
1301
1302 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001303 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001304 vcpu->arch.ctr = regs->ctr;
1305 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001306 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -06001307 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +02001308 vcpu->arch.shared->srr0 = regs->srr0;
1309 vcpu->arch.shared->srr1 = regs->srr1;
Scott Wood5ce941e2011-04-27 17:24:21 -05001310 kvmppc_set_pid(vcpu, regs->pid);
Alexander Grafa73a9592010-07-29 14:47:47 +02001311 vcpu->arch.shared->sprg0 = regs->sprg0;
1312 vcpu->arch.shared->sprg1 = regs->sprg1;
1313 vcpu->arch.shared->sprg2 = regs->sprg2;
1314 vcpu->arch.shared->sprg3 = regs->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -06001315 vcpu->arch.shared->sprg4 = regs->sprg4;
1316 vcpu->arch.shared->sprg5 = regs->sprg5;
1317 vcpu->arch.shared->sprg6 = regs->sprg6;
1318 vcpu->arch.shared->sprg7 = regs->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001319
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001320 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1321 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001322
1323 return 0;
1324}
1325
Scott Wood5ce941e2011-04-27 17:24:21 -05001326static void get_sregs_base(struct kvm_vcpu *vcpu,
1327 struct kvm_sregs *sregs)
1328{
1329 u64 tb = get_tb();
1330
1331 sregs->u.e.features |= KVM_SREGS_E_BASE;
1332
1333 sregs->u.e.csrr0 = vcpu->arch.csrr0;
1334 sregs->u.e.csrr1 = vcpu->arch.csrr1;
1335 sregs->u.e.mcsr = vcpu->arch.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +00001336 sregs->u.e.esr = get_guest_esr(vcpu);
1337 sregs->u.e.dear = get_guest_dear(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001338 sregs->u.e.tsr = vcpu->arch.tsr;
1339 sregs->u.e.tcr = vcpu->arch.tcr;
1340 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
1341 sregs->u.e.tb = tb;
1342 sregs->u.e.vrsave = vcpu->arch.vrsave;
1343}
1344
1345static int set_sregs_base(struct kvm_vcpu *vcpu,
1346 struct kvm_sregs *sregs)
1347{
1348 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
1349 return 0;
1350
1351 vcpu->arch.csrr0 = sregs->u.e.csrr0;
1352 vcpu->arch.csrr1 = sregs->u.e.csrr1;
1353 vcpu->arch.mcsr = sregs->u.e.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +00001354 set_guest_esr(vcpu, sregs->u.e.esr);
1355 set_guest_dear(vcpu, sregs->u.e.dear);
Scott Wood5ce941e2011-04-27 17:24:21 -05001356 vcpu->arch.vrsave = sregs->u.e.vrsave;
Scott Wooddfd4d472011-11-17 12:39:59 +00001357 kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001358
Scott Wooddfd4d472011-11-17 12:39:59 +00001359 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
Scott Wood5ce941e2011-04-27 17:24:21 -05001360 vcpu->arch.dec = sregs->u.e.dec;
Scott Wooddfd4d472011-11-17 12:39:59 +00001361 kvmppc_emulate_dec(vcpu);
1362 }
Scott Wood5ce941e2011-04-27 17:24:21 -05001363
Bharat Bhushand26f22c2013-02-24 18:57:11 +00001364 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR)
1365 kvmppc_set_tsr(vcpu, sregs->u.e.tsr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001366
1367 return 0;
1368}
1369
1370static void get_sregs_arch206(struct kvm_vcpu *vcpu,
1371 struct kvm_sregs *sregs)
1372{
1373 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
1374
Scott Wood841741f2011-09-02 17:39:37 -05001375 sregs->u.e.pir = vcpu->vcpu_id;
Scott Wood5ce941e2011-04-27 17:24:21 -05001376 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
1377 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
1378 sregs->u.e.decar = vcpu->arch.decar;
1379 sregs->u.e.ivpr = vcpu->arch.ivpr;
1380}
1381
1382static int set_sregs_arch206(struct kvm_vcpu *vcpu,
1383 struct kvm_sregs *sregs)
1384{
1385 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
1386 return 0;
1387
Scott Wood841741f2011-09-02 17:39:37 -05001388 if (sregs->u.e.pir != vcpu->vcpu_id)
Scott Wood5ce941e2011-04-27 17:24:21 -05001389 return -EINVAL;
1390
1391 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
1392 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
1393 vcpu->arch.decar = sregs->u.e.decar;
1394 vcpu->arch.ivpr = sregs->u.e.ivpr;
1395
1396 return 0;
1397}
1398
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301399int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Scott Wood5ce941e2011-04-27 17:24:21 -05001400{
1401 sregs->u.e.features |= KVM_SREGS_E_IVOR;
1402
1403 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
1404 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
1405 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
1406 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
1407 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
1408 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
1409 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
1410 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
1411 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
1412 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
1413 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
1414 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
1415 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
1416 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
1417 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
1418 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301419 return 0;
Scott Wood5ce941e2011-04-27 17:24:21 -05001420}
1421
1422int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1423{
1424 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
1425 return 0;
1426
1427 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
1428 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
1429 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
1430 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
1431 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
1432 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
1433 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
1434 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
1435 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
1436 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
1437 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
1438 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
1439 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
1440 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
1441 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
1442 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
1443
1444 return 0;
1445}
1446
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001447int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1448 struct kvm_sregs *sregs)
1449{
Scott Wood5ce941e2011-04-27 17:24:21 -05001450 sregs->pvr = vcpu->arch.pvr;
1451
1452 get_sregs_base(vcpu, sregs);
1453 get_sregs_arch206(vcpu, sregs);
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301454 return vcpu->kvm->arch.kvm_ops->get_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001455}
1456
1457int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1458 struct kvm_sregs *sregs)
1459{
Scott Wood5ce941e2011-04-27 17:24:21 -05001460 int ret;
1461
1462 if (vcpu->arch.pvr != sregs->pvr)
1463 return -EINVAL;
1464
1465 ret = set_sregs_base(vcpu, sregs);
1466 if (ret < 0)
1467 return ret;
1468
1469 ret = set_sregs_arch206(vcpu, sregs);
1470 if (ret < 0)
1471 return ret;
1472
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301473 return vcpu->kvm->arch.kvm_ops->set_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001474}
1475
Paul Mackerras31f34382011-12-12 12:26:50 +00001476int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1477{
Mihai Caraman35b299e2013-04-11 00:03:07 +00001478 int r = 0;
1479 union kvmppc_one_reg val;
1480 int size;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001481
1482 size = one_reg_size(reg->id);
1483 if (size > sizeof(val))
1484 return -EINVAL;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001485
1486 switch (reg->id) {
1487 case KVM_REG_PPC_IAC1:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301488 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac1);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001489 break;
Bharat Bhushan547465e2013-07-04 12:27:46 +05301490 case KVM_REG_PPC_IAC2:
1491 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac2);
1492 break;
1493#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1494 case KVM_REG_PPC_IAC3:
1495 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac3);
1496 break;
1497 case KVM_REG_PPC_IAC4:
1498 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac4);
1499 break;
1500#endif
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001501 case KVM_REG_PPC_DAC1:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301502 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.dac1);
1503 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001504 case KVM_REG_PPC_DAC2:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301505 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.dac2);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001506 break;
Alexander Graf324b3e62013-01-04 18:28:51 +01001507 case KVM_REG_PPC_EPR: {
1508 u32 epr = get_guest_epr(vcpu);
Mihai Caraman35b299e2013-04-11 00:03:07 +00001509 val = get_reg_val(reg->id, epr);
Alexander Graf324b3e62013-01-04 18:28:51 +01001510 break;
1511 }
Mihai Caraman352df1d2012-10-11 06:13:29 +00001512#if defined(CONFIG_64BIT)
1513 case KVM_REG_PPC_EPCR:
Mihai Caraman35b299e2013-04-11 00:03:07 +00001514 val = get_reg_val(reg->id, vcpu->arch.epcr);
Mihai Caraman352df1d2012-10-11 06:13:29 +00001515 break;
1516#endif
Bharat Bhushan78accda2013-02-24 18:57:12 +00001517 case KVM_REG_PPC_TCR:
Mihai Caraman35b299e2013-04-11 00:03:07 +00001518 val = get_reg_val(reg->id, vcpu->arch.tcr);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001519 break;
1520 case KVM_REG_PPC_TSR:
Mihai Caraman35b299e2013-04-11 00:03:07 +00001521 val = get_reg_val(reg->id, vcpu->arch.tsr);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001522 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001523 case KVM_REG_PPC_DEBUG_INST:
Bharat Bhushanb12c7842013-07-04 12:27:45 +05301524 val = get_reg_val(reg->id, KVMPPC_INST_EHPRIV_DEBUG);
Bharat Bhushan8c32a2e2013-03-20 20:24:58 +00001525 break;
Paul Mackerras8b75cbb2013-09-20 14:52:37 +10001526 case KVM_REG_PPC_VRSAVE:
1527 val = get_reg_val(reg->id, vcpu->arch.vrsave);
Bharat Bhushan8c32a2e2013-03-20 20:24:58 +00001528 break;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001529 default:
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301530 r = vcpu->kvm->arch.kvm_ops->get_one_reg(vcpu, reg->id, &val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001531 break;
1532 }
Mihai Caraman35b299e2013-04-11 00:03:07 +00001533
1534 if (r)
1535 return r;
1536
1537 if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
1538 r = -EFAULT;
1539
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001540 return r;
Paul Mackerras31f34382011-12-12 12:26:50 +00001541}
1542
1543int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1544{
Mihai Caraman35b299e2013-04-11 00:03:07 +00001545 int r = 0;
1546 union kvmppc_one_reg val;
1547 int size;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001548
1549 size = one_reg_size(reg->id);
1550 if (size > sizeof(val))
1551 return -EINVAL;
1552
1553 if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
1554 return -EFAULT;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001555
1556 switch (reg->id) {
1557 case KVM_REG_PPC_IAC1:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301558 vcpu->arch.dbg_reg.iac1 = set_reg_val(reg->id, val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001559 break;
Bharat Bhushan547465e2013-07-04 12:27:46 +05301560 case KVM_REG_PPC_IAC2:
1561 vcpu->arch.dbg_reg.iac2 = set_reg_val(reg->id, val);
1562 break;
1563#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1564 case KVM_REG_PPC_IAC3:
1565 vcpu->arch.dbg_reg.iac3 = set_reg_val(reg->id, val);
1566 break;
1567 case KVM_REG_PPC_IAC4:
1568 vcpu->arch.dbg_reg.iac4 = set_reg_val(reg->id, val);
1569 break;
1570#endif
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001571 case KVM_REG_PPC_DAC1:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301572 vcpu->arch.dbg_reg.dac1 = set_reg_val(reg->id, val);
1573 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001574 case KVM_REG_PPC_DAC2:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301575 vcpu->arch.dbg_reg.dac2 = set_reg_val(reg->id, val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001576 break;
Alexander Graf324b3e62013-01-04 18:28:51 +01001577 case KVM_REG_PPC_EPR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001578 u32 new_epr = set_reg_val(reg->id, val);
1579 kvmppc_set_epr(vcpu, new_epr);
Alexander Graf324b3e62013-01-04 18:28:51 +01001580 break;
1581 }
Mihai Caraman352df1d2012-10-11 06:13:29 +00001582#if defined(CONFIG_64BIT)
1583 case KVM_REG_PPC_EPCR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001584 u32 new_epcr = set_reg_val(reg->id, val);
1585 kvmppc_set_epcr(vcpu, new_epcr);
Mihai Caraman352df1d2012-10-11 06:13:29 +00001586 break;
1587 }
1588#endif
Bharat Bhushan78accda2013-02-24 18:57:12 +00001589 case KVM_REG_PPC_OR_TSR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001590 u32 tsr_bits = set_reg_val(reg->id, val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001591 kvmppc_set_tsr_bits(vcpu, tsr_bits);
1592 break;
1593 }
1594 case KVM_REG_PPC_CLEAR_TSR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001595 u32 tsr_bits = set_reg_val(reg->id, val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001596 kvmppc_clr_tsr_bits(vcpu, tsr_bits);
1597 break;
1598 }
1599 case KVM_REG_PPC_TSR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001600 u32 tsr = set_reg_val(reg->id, val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001601 kvmppc_set_tsr(vcpu, tsr);
1602 break;
1603 }
1604 case KVM_REG_PPC_TCR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001605 u32 tcr = set_reg_val(reg->id, val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001606 kvmppc_set_tcr(vcpu, tcr);
1607 break;
1608 }
Paul Mackerras8b75cbb2013-09-20 14:52:37 +10001609 case KVM_REG_PPC_VRSAVE:
1610 vcpu->arch.vrsave = set_reg_val(reg->id, val);
1611 break;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001612 default:
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301613 r = vcpu->kvm->arch.kvm_ops->set_one_reg(vcpu, reg->id, &val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001614 break;
1615 }
Mihai Caraman35b299e2013-04-11 00:03:07 +00001616
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001617 return r;
Paul Mackerras31f34382011-12-12 12:26:50 +00001618}
1619
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001620int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1621{
1622 return -ENOTSUPP;
1623}
1624
1625int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1626{
1627 return -ENOTSUPP;
1628}
1629
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001630int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1631 struct kvm_translation *tr)
1632{
Avi Kivity98001d82010-05-13 11:05:49 +03001633 int r;
1634
Avi Kivity98001d82010-05-13 11:05:49 +03001635 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +03001636 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001637}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001638
Alexander Graf4e755752009-10-30 05:47:01 +00001639int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1640{
1641 return -ENOTSUPP;
1642}
1643
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05301644void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001645 struct kvm_memory_slot *dont)
1646{
1647}
1648
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05301649int kvmppc_core_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001650 unsigned long npages)
1651{
1652 return 0;
1653}
1654
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001655int kvmppc_core_prepare_memory_region(struct kvm *kvm,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001656 struct kvm_memory_slot *memslot,
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001657 struct kvm_userspace_memory_region *mem)
1658{
1659 return 0;
1660}
1661
1662void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001663 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001664 const struct kvm_memory_slot *old)
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001665{
1666}
1667
1668void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001669{
1670}
1671
Mihai Caraman38f98822012-10-11 06:13:27 +00001672void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr)
1673{
1674#if defined(CONFIG_64BIT)
1675 vcpu->arch.epcr = new_epcr;
1676#ifdef CONFIG_KVM_BOOKE_HV
1677 vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM;
1678 if (vcpu->arch.epcr & SPRN_EPCR_ICM)
1679 vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM;
1680#endif
1681#endif
1682}
1683
Scott Wooddfd4d472011-11-17 12:39:59 +00001684void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1685{
1686 vcpu->arch.tcr = new_tcr;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001687 arm_next_watchdog(vcpu);
Scott Wooddfd4d472011-11-17 12:39:59 +00001688 update_timer_ints(vcpu);
1689}
1690
1691void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1692{
1693 set_bits(tsr_bits, &vcpu->arch.tsr);
1694 smp_wmb();
1695 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1696 kvm_vcpu_kick(vcpu);
1697}
1698
1699void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1700{
1701 clear_bits(tsr_bits, &vcpu->arch.tsr);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001702
1703 /*
1704 * We may have stopped the watchdog due to
1705 * being stuck on final expiration.
1706 */
1707 if (tsr_bits & (TSR_ENW | TSR_WIS))
1708 arm_next_watchdog(vcpu);
1709
Scott Wooddfd4d472011-11-17 12:39:59 +00001710 update_timer_ints(vcpu);
1711}
1712
1713void kvmppc_decrementer_func(unsigned long data)
1714{
1715 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1716
Bharat Bhushan21bd0002012-05-20 23:21:23 +00001717 if (vcpu->arch.tcr & TCR_ARE) {
1718 vcpu->arch.dec = vcpu->arch.decar;
1719 kvmppc_emulate_dec(vcpu);
1720 }
1721
Scott Wooddfd4d472011-11-17 12:39:59 +00001722 kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1723}
1724
Bharat Bhushance11e482013-07-04 12:27:47 +05301725static int kvmppc_booke_add_breakpoint(struct debug_reg *dbg_reg,
1726 uint64_t addr, int index)
1727{
1728 switch (index) {
1729 case 0:
1730 dbg_reg->dbcr0 |= DBCR0_IAC1;
1731 dbg_reg->iac1 = addr;
1732 break;
1733 case 1:
1734 dbg_reg->dbcr0 |= DBCR0_IAC2;
1735 dbg_reg->iac2 = addr;
1736 break;
1737#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1738 case 2:
1739 dbg_reg->dbcr0 |= DBCR0_IAC3;
1740 dbg_reg->iac3 = addr;
1741 break;
1742 case 3:
1743 dbg_reg->dbcr0 |= DBCR0_IAC4;
1744 dbg_reg->iac4 = addr;
1745 break;
1746#endif
1747 default:
1748 return -EINVAL;
1749 }
1750
1751 dbg_reg->dbcr0 |= DBCR0_IDM;
1752 return 0;
1753}
1754
1755static int kvmppc_booke_add_watchpoint(struct debug_reg *dbg_reg, uint64_t addr,
1756 int type, int index)
1757{
1758 switch (index) {
1759 case 0:
1760 if (type & KVMPPC_DEBUG_WATCH_READ)
1761 dbg_reg->dbcr0 |= DBCR0_DAC1R;
1762 if (type & KVMPPC_DEBUG_WATCH_WRITE)
1763 dbg_reg->dbcr0 |= DBCR0_DAC1W;
1764 dbg_reg->dac1 = addr;
1765 break;
1766 case 1:
1767 if (type & KVMPPC_DEBUG_WATCH_READ)
1768 dbg_reg->dbcr0 |= DBCR0_DAC2R;
1769 if (type & KVMPPC_DEBUG_WATCH_WRITE)
1770 dbg_reg->dbcr0 |= DBCR0_DAC2W;
1771 dbg_reg->dac2 = addr;
1772 break;
1773 default:
1774 return -EINVAL;
1775 }
1776
1777 dbg_reg->dbcr0 |= DBCR0_IDM;
1778 return 0;
1779}
1780void kvm_guest_protect_msr(struct kvm_vcpu *vcpu, ulong prot_bitmap, bool set)
1781{
1782 /* XXX: Add similar MSR protection for BookE-PR */
1783#ifdef CONFIG_KVM_BOOKE_HV
1784 BUG_ON(prot_bitmap & ~(MSRP_UCLEP | MSRP_DEP | MSRP_PMMP));
1785 if (set) {
1786 if (prot_bitmap & MSR_UCLE)
1787 vcpu->arch.shadow_msrp |= MSRP_UCLEP;
1788 if (prot_bitmap & MSR_DE)
1789 vcpu->arch.shadow_msrp |= MSRP_DEP;
1790 if (prot_bitmap & MSR_PMM)
1791 vcpu->arch.shadow_msrp |= MSRP_PMMP;
1792 } else {
1793 if (prot_bitmap & MSR_UCLE)
1794 vcpu->arch.shadow_msrp &= ~MSRP_UCLEP;
1795 if (prot_bitmap & MSR_DE)
1796 vcpu->arch.shadow_msrp &= ~MSRP_DEP;
1797 if (prot_bitmap & MSR_PMM)
1798 vcpu->arch.shadow_msrp &= ~MSRP_PMMP;
1799 }
1800#endif
1801}
1802
1803int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1804 struct kvm_guest_debug *dbg)
1805{
1806 struct debug_reg *dbg_reg;
1807 int n, b = 0, w = 0;
1808
1809 if (!(dbg->control & KVM_GUESTDBG_ENABLE)) {
1810 vcpu->arch.shadow_dbg_reg.dbcr0 = 0;
1811 vcpu->guest_debug = 0;
1812 kvm_guest_protect_msr(vcpu, MSR_DE, false);
1813 return 0;
1814 }
1815
1816 kvm_guest_protect_msr(vcpu, MSR_DE, true);
1817 vcpu->guest_debug = dbg->control;
1818 vcpu->arch.shadow_dbg_reg.dbcr0 = 0;
1819 /* Set DBCR0_EDM in guest visible DBCR0 register. */
1820 vcpu->arch.dbg_reg.dbcr0 = DBCR0_EDM;
1821
1822 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1823 vcpu->arch.shadow_dbg_reg.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1824
1825 /* Code below handles only HW breakpoints */
1826 dbg_reg = &(vcpu->arch.shadow_dbg_reg);
1827
1828#ifdef CONFIG_KVM_BOOKE_HV
1829 /*
1830 * On BookE-HV (e500mc) the guest is always executed with MSR.GS=1
1831 * DBCR1 and DBCR2 are set to trigger debug events when MSR.PR is 0
1832 */
1833 dbg_reg->dbcr1 = 0;
1834 dbg_reg->dbcr2 = 0;
1835#else
1836 /*
1837 * On BookE-PR (e500v2) the guest is always executed with MSR.PR=1
1838 * We set DBCR1 and DBCR2 to only trigger debug events when MSR.PR
1839 * is set.
1840 */
1841 dbg_reg->dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US | DBCR1_IAC3US |
1842 DBCR1_IAC4US;
1843 dbg_reg->dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
1844#endif
1845
1846 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1847 return 0;
1848
1849 for (n = 0; n < (KVMPPC_BOOKE_IAC_NUM + KVMPPC_BOOKE_DAC_NUM); n++) {
1850 uint64_t addr = dbg->arch.bp[n].addr;
1851 uint32_t type = dbg->arch.bp[n].type;
1852
1853 if (type == KVMPPC_DEBUG_NONE)
1854 continue;
1855
1856 if (type & !(KVMPPC_DEBUG_WATCH_READ |
1857 KVMPPC_DEBUG_WATCH_WRITE |
1858 KVMPPC_DEBUG_BREAKPOINT))
1859 return -EINVAL;
1860
1861 if (type & KVMPPC_DEBUG_BREAKPOINT) {
1862 /* Setting H/W breakpoint */
1863 if (kvmppc_booke_add_breakpoint(dbg_reg, addr, b++))
1864 return -EINVAL;
1865 } else {
1866 /* Setting H/W watchpoint */
1867 if (kvmppc_booke_add_watchpoint(dbg_reg, addr,
1868 type, w++))
1869 return -EINVAL;
1870 }
1871 }
1872
1873 return 0;
1874}
1875
Scott Wood94fa9d92011-12-20 15:34:22 +00001876void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1877{
Paul Mackerrasa47d72f2012-09-20 19:35:51 +00001878 vcpu->cpu = smp_processor_id();
Scott Woodd30f6e42011-12-20 15:34:43 +00001879 current->thread.kvm_vcpu = vcpu;
Scott Wood94fa9d92011-12-20 15:34:22 +00001880}
1881
1882void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
1883{
Scott Woodd30f6e42011-12-20 15:34:43 +00001884 current->thread.kvm_vcpu = NULL;
Paul Mackerrasa47d72f2012-09-20 19:35:51 +00001885 vcpu->cpu = -1;
Bharat Bhushance11e482013-07-04 12:27:47 +05301886
1887 /* Clear pending debug event in DBSR */
1888 kvmppc_clear_dbsr();
Scott Wood94fa9d92011-12-20 15:34:22 +00001889}
1890
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301891void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
1892{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301893 vcpu->kvm->arch.kvm_ops->mmu_destroy(vcpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301894}
1895
1896int kvmppc_core_init_vm(struct kvm *kvm)
1897{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301898 return kvm->arch.kvm_ops->init_vm(kvm);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301899}
1900
1901struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1902{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301903 return kvm->arch.kvm_ops->vcpu_create(kvm, id);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301904}
1905
1906void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1907{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301908 vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301909}
1910
1911void kvmppc_core_destroy_vm(struct kvm *kvm)
1912{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301913 kvm->arch.kvm_ops->destroy_vm(kvm);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301914}
1915
1916void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1917{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301918 vcpu->kvm->arch.kvm_ops->vcpu_load(vcpu, cpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301919}
1920
1921void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
1922{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301923 vcpu->kvm->arch.kvm_ops->vcpu_put(vcpu);
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001924}
1925
1926int __init kvmppc_booke_init(void)
1927{
Scott Woodd30f6e42011-12-20 15:34:43 +00001928#ifndef CONFIG_KVM_BOOKE_HV
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001929 unsigned long ivor[16];
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001930 unsigned long *handler = kvmppc_booke_handler_addr;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001931 unsigned long max_ivor = 0;
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001932 unsigned long handler_len;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001933 int i;
1934
1935 /* We install our own exception handlers by hijacking IVPR. IVPR must
1936 * be 16-bit aligned, so we need a 64KB allocation. */
1937 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1938 VCPU_SIZE_ORDER);
1939 if (!kvmppc_booke_handlers)
1940 return -ENOMEM;
1941
1942 /* XXX make sure our handlers are smaller than Linux's */
1943
1944 /* Copy our interrupt handlers to match host IVORs. That way we don't
1945 * have to swap the IVORs on every guest/host transition. */
1946 ivor[0] = mfspr(SPRN_IVOR0);
1947 ivor[1] = mfspr(SPRN_IVOR1);
1948 ivor[2] = mfspr(SPRN_IVOR2);
1949 ivor[3] = mfspr(SPRN_IVOR3);
1950 ivor[4] = mfspr(SPRN_IVOR4);
1951 ivor[5] = mfspr(SPRN_IVOR5);
1952 ivor[6] = mfspr(SPRN_IVOR6);
1953 ivor[7] = mfspr(SPRN_IVOR7);
1954 ivor[8] = mfspr(SPRN_IVOR8);
1955 ivor[9] = mfspr(SPRN_IVOR9);
1956 ivor[10] = mfspr(SPRN_IVOR10);
1957 ivor[11] = mfspr(SPRN_IVOR11);
1958 ivor[12] = mfspr(SPRN_IVOR12);
1959 ivor[13] = mfspr(SPRN_IVOR13);
1960 ivor[14] = mfspr(SPRN_IVOR14);
1961 ivor[15] = mfspr(SPRN_IVOR15);
1962
1963 for (i = 0; i < 16; i++) {
1964 if (ivor[i] > max_ivor)
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001965 max_ivor = i;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001966
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001967 handler_len = handler[i + 1] - handler[i];
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001968 memcpy((void *)kvmppc_booke_handlers + ivor[i],
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001969 (void *)handler[i], handler_len);
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001970 }
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001971
1972 handler_len = handler[max_ivor + 1] - handler[max_ivor];
1973 flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers +
1974 ivor[max_ivor] + handler_len);
Scott Woodd30f6e42011-12-20 15:34:43 +00001975#endif /* !BOOKE_HV */
Hollis Blancharddb93f572008-11-05 09:36:18 -06001976 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001977}
1978
Hollis Blancharddb93f572008-11-05 09:36:18 -06001979void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001980{
1981 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
1982 kvm_exit();
1983}