blob: 3b43adb9689feed9e64991d55b86f2ebed553cc3 [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{
Bharat Bhushan31579ee2014-07-17 17:01:36 +0530269 kvmppc_set_srr0(vcpu, srr0);
270 kvmppc_set_srr1(vcpu, srr1);
Scott Woodd30f6e42011-12-20 15:34:43 +0000271}
272
273static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
274{
275 vcpu->arch.csrr0 = srr0;
276 vcpu->arch.csrr1 = srr1;
277}
278
279static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
280{
281 if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
282 vcpu->arch.dsrr0 = srr0;
283 vcpu->arch.dsrr1 = srr1;
284 } else {
285 set_guest_csrr(vcpu, srr0, srr1);
286 }
287}
288
289static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
290{
291 vcpu->arch.mcsrr0 = srr0;
292 vcpu->arch.mcsrr1 = srr1;
293}
294
295static unsigned long get_guest_dear(struct kvm_vcpu *vcpu)
296{
297#ifdef CONFIG_KVM_BOOKE_HV
298 return mfspr(SPRN_GDEAR);
299#else
300 return vcpu->arch.shared->dar;
301#endif
302}
303
304static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear)
305{
306#ifdef CONFIG_KVM_BOOKE_HV
307 mtspr(SPRN_GDEAR, dear);
308#else
309 vcpu->arch.shared->dar = dear;
310#endif
311}
312
313static unsigned long get_guest_esr(struct kvm_vcpu *vcpu)
314{
315#ifdef CONFIG_KVM_BOOKE_HV
316 return mfspr(SPRN_GESR);
317#else
318 return vcpu->arch.shared->esr;
319#endif
320}
321
322static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr)
323{
324#ifdef CONFIG_KVM_BOOKE_HV
325 mtspr(SPRN_GESR, esr);
326#else
327 vcpu->arch.shared->esr = esr;
328#endif
329}
330
Alexander Graf324b3e62013-01-04 18:28:51 +0100331static unsigned long get_guest_epr(struct kvm_vcpu *vcpu)
332{
333#ifdef CONFIG_KVM_BOOKE_HV
334 return mfspr(SPRN_GEPR);
335#else
336 return vcpu->arch.epr;
337#endif
338}
339
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600340/* Deliver the interrupt of the corresponding priority, if possible. */
341static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
342 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500343{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600344 int allowed = 0;
Alexander Graf79300f82012-02-15 19:12:29 +0000345 ulong msr_mask = 0;
Alexander Graf1c810632013-01-04 18:12:48 +0100346 bool update_esr = false, update_dear = false, update_epr = false;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200347 ulong crit_raw = vcpu->arch.shared->critical;
348 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
349 bool crit;
Alexander Grafc5335f12010-08-30 14:03:24 +0200350 bool keep_irq = false;
Scott Woodd30f6e42011-12-20 15:34:43 +0000351 enum int_class int_class;
Mihai Caraman95e90b42012-10-11 06:13:26 +0000352 ulong new_msr = vcpu->arch.shared->msr;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200353
354 /* Truncate crit indicators in 32 bit mode */
355 if (!(vcpu->arch.shared->msr & MSR_SF)) {
356 crit_raw &= 0xffffffff;
357 crit_r1 &= 0xffffffff;
358 }
359
360 /* Critical section when crit == r1 */
361 crit = (crit_raw == crit_r1);
362 /* ... and we're in supervisor mode */
363 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500364
Alexander Grafc5335f12010-08-30 14:03:24 +0200365 if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
366 priority = BOOKE_IRQPRIO_EXTERNAL;
367 keep_irq = true;
368 }
369
Scott Wood5df554ad2013-04-12 14:08:46 +0000370 if ((priority == BOOKE_IRQPRIO_EXTERNAL) && vcpu->arch.epr_flags)
Alexander Graf1c810632013-01-04 18:12:48 +0100371 update_epr = true;
372
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600373 switch (priority) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600374 case BOOKE_IRQPRIO_DTLB_MISS:
Liu Yudaf5e272010-02-02 19:44:35 +0800375 case BOOKE_IRQPRIO_DATA_STORAGE:
Alexander Graf011da892013-01-31 14:17:38 +0100376 case BOOKE_IRQPRIO_ALIGNMENT:
Liu Yudaf5e272010-02-02 19:44:35 +0800377 update_dear = true;
378 /* fall through */
379 case BOOKE_IRQPRIO_INST_STORAGE:
380 case BOOKE_IRQPRIO_PROGRAM:
381 update_esr = true;
382 /* fall through */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600383 case BOOKE_IRQPRIO_ITLB_MISS:
384 case BOOKE_IRQPRIO_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600385 case BOOKE_IRQPRIO_FP_UNAVAIL:
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600386 case BOOKE_IRQPRIO_SPE_UNAVAIL:
387 case BOOKE_IRQPRIO_SPE_FP_DATA:
388 case BOOKE_IRQPRIO_SPE_FP_ROUND:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600389 case BOOKE_IRQPRIO_AP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600390 allowed = 1;
Alexander Graf79300f82012-02-15 19:12:29 +0000391 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000392 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500393 break;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000394 case BOOKE_IRQPRIO_WATCHDOG:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600395 case BOOKE_IRQPRIO_CRITICAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000396 case BOOKE_IRQPRIO_DBELL_CRIT:
Alexander Graf666e7252010-07-29 14:47:43 +0200397 allowed = vcpu->arch.shared->msr & MSR_CE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000398 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000399 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000400 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500401 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600402 case BOOKE_IRQPRIO_MACHINE_CHECK:
Alexander Graf666e7252010-07-29 14:47:43 +0200403 allowed = vcpu->arch.shared->msr & MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000404 allowed = allowed && !crit;
Scott Woodd30f6e42011-12-20 15:34:43 +0000405 int_class = INT_CLASS_MC;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500406 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600407 case BOOKE_IRQPRIO_DECREMENTER:
408 case BOOKE_IRQPRIO_FIT:
Scott Wooddfd4d472011-11-17 12:39:59 +0000409 keep_irq = true;
410 /* fall through */
411 case BOOKE_IRQPRIO_EXTERNAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000412 case BOOKE_IRQPRIO_DBELL:
Alexander Graf666e7252010-07-29 14:47:43 +0200413 allowed = vcpu->arch.shared->msr & MSR_EE;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200414 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000415 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000416 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500417 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600418 case BOOKE_IRQPRIO_DEBUG:
Alexander Graf666e7252010-07-29 14:47:43 +0200419 allowed = vcpu->arch.shared->msr & MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000420 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000421 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000422 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500423 break;
424 }
425
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600426 if (allowed) {
Scott Woodd30f6e42011-12-20 15:34:43 +0000427 switch (int_class) {
428 case INT_CLASS_NONCRIT:
429 set_guest_srr(vcpu, vcpu->arch.pc,
430 vcpu->arch.shared->msr);
431 break;
432 case INT_CLASS_CRIT:
433 set_guest_csrr(vcpu, vcpu->arch.pc,
434 vcpu->arch.shared->msr);
435 break;
436 case INT_CLASS_DBG:
437 set_guest_dsrr(vcpu, vcpu->arch.pc,
438 vcpu->arch.shared->msr);
439 break;
440 case INT_CLASS_MC:
441 set_guest_mcsrr(vcpu, vcpu->arch.pc,
442 vcpu->arch.shared->msr);
443 break;
444 }
445
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600446 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
Liu Yudaf5e272010-02-02 19:44:35 +0800447 if (update_esr == true)
Scott Woodd30f6e42011-12-20 15:34:43 +0000448 set_guest_esr(vcpu, vcpu->arch.queued_esr);
Liu Yudaf5e272010-02-02 19:44:35 +0800449 if (update_dear == true)
Scott Woodd30f6e42011-12-20 15:34:43 +0000450 set_guest_dear(vcpu, vcpu->arch.queued_dear);
Scott Wood5df554ad2013-04-12 14:08:46 +0000451 if (update_epr == true) {
452 if (vcpu->arch.epr_flags & KVMPPC_EPR_USER)
453 kvm_make_request(KVM_REQ_EPR_EXIT, vcpu);
Scott Woodeb1e4f42013-04-12 14:08:47 +0000454 else if (vcpu->arch.epr_flags & KVMPPC_EPR_KERNEL) {
455 BUG_ON(vcpu->arch.irq_type != KVMPPC_IRQ_MPIC);
456 kvmppc_mpic_set_epr(vcpu);
457 }
Scott Wood5df554ad2013-04-12 14:08:46 +0000458 }
Mihai Caraman95e90b42012-10-11 06:13:26 +0000459
460 new_msr &= msr_mask;
461#if defined(CONFIG_64BIT)
462 if (vcpu->arch.epcr & SPRN_EPCR_ICM)
463 new_msr |= MSR_CM;
464#endif
465 kvmppc_set_msr(vcpu, new_msr);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600466
Alexander Grafc5335f12010-08-30 14:03:24 +0200467 if (!keep_irq)
468 clear_bit(priority, &vcpu->arch.pending_exceptions);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600469 }
470
Scott Woodd30f6e42011-12-20 15:34:43 +0000471#ifdef CONFIG_KVM_BOOKE_HV
472 /*
473 * If an interrupt is pending but masked, raise a guest doorbell
474 * so that we are notified when the guest enables the relevant
475 * MSR bit.
476 */
477 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
478 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
479 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
480 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
481 if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
482 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
483#endif
484
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600485 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500486}
487
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000488/*
489 * Return the number of jiffies until the next timeout. If the timeout is
490 * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA
491 * because the larger value can break the timer APIs.
492 */
493static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu)
494{
495 u64 tb, wdt_tb, wdt_ticks = 0;
496 u64 nr_jiffies = 0;
497 u32 period = TCR_GET_WP(vcpu->arch.tcr);
498
499 wdt_tb = 1ULL << (63 - period);
500 tb = get_tb();
501 /*
502 * The watchdog timeout will hapeen when TB bit corresponding
503 * to watchdog will toggle from 0 to 1.
504 */
505 if (tb & wdt_tb)
506 wdt_ticks = wdt_tb;
507
508 wdt_ticks += wdt_tb - (tb & (wdt_tb - 1));
509
510 /* Convert timebase ticks to jiffies */
511 nr_jiffies = wdt_ticks;
512
513 if (do_div(nr_jiffies, tb_ticks_per_jiffy))
514 nr_jiffies++;
515
516 return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA);
517}
518
519static void arm_next_watchdog(struct kvm_vcpu *vcpu)
520{
521 unsigned long nr_jiffies;
522 unsigned long flags;
523
524 /*
525 * If TSR_ENW and TSR_WIS are not set then no need to exit to
526 * userspace, so clear the KVM_REQ_WATCHDOG request.
527 */
528 if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS))
529 clear_bit(KVM_REQ_WATCHDOG, &vcpu->requests);
530
531 spin_lock_irqsave(&vcpu->arch.wdt_lock, flags);
532 nr_jiffies = watchdog_next_timeout(vcpu);
533 /*
534 * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA
535 * then do not run the watchdog timer as this can break timer APIs.
536 */
537 if (nr_jiffies < NEXT_TIMER_MAX_DELTA)
538 mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies);
539 else
540 del_timer(&vcpu->arch.wdt_timer);
541 spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags);
542}
543
544void kvmppc_watchdog_func(unsigned long data)
545{
546 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
547 u32 tsr, new_tsr;
548 int final;
549
550 do {
551 new_tsr = tsr = vcpu->arch.tsr;
552 final = 0;
553
554 /* Time out event */
555 if (tsr & TSR_ENW) {
556 if (tsr & TSR_WIS)
557 final = 1;
558 else
559 new_tsr = tsr | TSR_WIS;
560 } else {
561 new_tsr = tsr | TSR_ENW;
562 }
563 } while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr);
564
565 if (new_tsr & TSR_WIS) {
566 smp_wmb();
567 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
568 kvm_vcpu_kick(vcpu);
569 }
570
571 /*
572 * If this is final watchdog expiry and some action is required
573 * then exit to userspace.
574 */
575 if (final && (vcpu->arch.tcr & TCR_WRC_MASK) &&
576 vcpu->arch.watchdog_enabled) {
577 smp_wmb();
578 kvm_make_request(KVM_REQ_WATCHDOG, vcpu);
579 kvm_vcpu_kick(vcpu);
580 }
581
582 /*
583 * Stop running the watchdog timer after final expiration to
584 * prevent the host from being flooded with timers if the
585 * guest sets a short period.
586 * Timers will resume when TSR/TCR is updated next time.
587 */
588 if (!final)
589 arm_next_watchdog(vcpu);
590}
591
Scott Wooddfd4d472011-11-17 12:39:59 +0000592static void update_timer_ints(struct kvm_vcpu *vcpu)
593{
594 if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
595 kvmppc_core_queue_dec(vcpu);
596 else
597 kvmppc_core_dequeue_dec(vcpu);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000598
599 if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS))
600 kvmppc_core_queue_watchdog(vcpu);
601 else
602 kvmppc_core_dequeue_watchdog(vcpu);
Scott Wooddfd4d472011-11-17 12:39:59 +0000603}
604
Scott Woodc59a6a32011-11-08 18:23:25 -0600605static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500606{
607 unsigned long *pending = &vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500608 unsigned int priority;
609
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600610 priority = __ffs(*pending);
Alexander Graf8b3a00f2012-02-16 14:12:46 +0000611 while (priority < BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600612 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500613 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500614
615 priority = find_next_bit(pending,
616 BITS_PER_BYTE * sizeof(*pending),
617 priority + 1);
618 }
Alexander Graf90bba352010-07-29 14:47:51 +0200619
620 /* Tell the guest about our interrupt status */
Scott Wood29ac26e2011-11-08 18:23:27 -0600621 vcpu->arch.shared->int_pending = !!*pending;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500622}
623
Scott Woodc59a6a32011-11-08 18:23:25 -0600624/* Check pending exceptions and deliver one, if possible. */
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000625int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
Scott Woodc59a6a32011-11-08 18:23:25 -0600626{
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000627 int r = 0;
Scott Woodc59a6a32011-11-08 18:23:25 -0600628 WARN_ON_ONCE(!irqs_disabled());
629
630 kvmppc_core_check_exceptions(vcpu);
631
Alexander Grafb8c649a2012-12-20 04:52:39 +0000632 if (vcpu->requests) {
633 /* Exception delivery raised request; start over */
634 return 1;
635 }
636
Scott Woodc59a6a32011-11-08 18:23:25 -0600637 if (vcpu->arch.shared->msr & MSR_WE) {
638 local_irq_enable();
639 kvm_vcpu_block(vcpu);
Alexander Graf966cd0f2012-03-14 16:55:08 +0100640 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Scott Wood6c85f522014-01-09 19:18:40 -0600641 hard_irq_disable();
Scott Woodc59a6a32011-11-08 18:23:25 -0600642
643 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000644 r = 1;
Scott Woodc59a6a32011-11-08 18:23:25 -0600645 };
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000646
647 return r;
648}
649
Alexander Graf7c973a22012-08-13 12:50:35 +0200650int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
Alexander Graf4ffc6352012-08-08 20:31:13 +0200651{
Alexander Graf7c973a22012-08-13 12:50:35 +0200652 int r = 1; /* Indicate we want to get back into the guest */
653
Alexander Graf2d8185d2012-08-10 12:31:12 +0200654 if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
655 update_timer_ints(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200656#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Alexander Graf2d8185d2012-08-10 12:31:12 +0200657 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
658 kvmppc_core_flush_tlb(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200659#endif
Alexander Graf7c973a22012-08-13 12:50:35 +0200660
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000661 if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) {
662 vcpu->run->exit_reason = KVM_EXIT_WATCHDOG;
663 r = 0;
664 }
665
Alexander Graf1c810632013-01-04 18:12:48 +0100666 if (kvm_check_request(KVM_REQ_EPR_EXIT, vcpu)) {
667 vcpu->run->epr.epr = 0;
668 vcpu->arch.epr_needed = true;
669 vcpu->run->exit_reason = KVM_EXIT_EPR;
670 r = 0;
671 }
672
Alexander Graf7c973a22012-08-13 12:50:35 +0200673 return r;
Alexander Graf4ffc6352012-08-08 20:31:13 +0200674}
675
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000676int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
677{
Alexander Graf7ee78852012-08-13 12:44:41 +0200678 int ret, s;
Scott Woodf5f97212013-11-22 15:52:29 -0600679 struct debug_reg debug;
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000680
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200681 if (!vcpu->arch.sane) {
682 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
683 return -EINVAL;
684 }
685
Alexander Graf7ee78852012-08-13 12:44:41 +0200686 s = kvmppc_prepare_to_enter(vcpu);
687 if (s <= 0) {
Alexander Graf7ee78852012-08-13 12:44:41 +0200688 ret = s;
Scott Wood1d1ef222011-11-08 16:11:59 -0600689 goto out;
690 }
Scott Wood6c85f522014-01-09 19:18:40 -0600691 /* interrupts now hard-disabled */
Scott Wood1d1ef222011-11-08 16:11:59 -0600692
Scott Wood8fae8452011-12-20 15:34:45 +0000693#ifdef CONFIG_PPC_FPU
694 /* Save userspace FPU state in stack */
695 enable_kernel_fp();
Scott Wood8fae8452011-12-20 15:34:45 +0000696
697 /*
698 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
699 * as always using the FPU. Kernel usage of FP (via
700 * enable_kernel_fp()) in this thread must not occur while
701 * vcpu->fpu_active is set.
702 */
703 vcpu->fpu_active = 1;
704
705 kvmppc_load_guest_fp(vcpu);
706#endif
707
Bharat Bhushance11e482013-07-04 12:27:47 +0530708 /* Switch to guest debug context */
Scott Woodf5f97212013-11-22 15:52:29 -0600709 debug = vcpu->arch.shadow_dbg_reg;
710 switch_booke_debug_regs(&debug);
711 debug = current->thread.debug;
Bharat Bhushance11e482013-07-04 12:27:47 +0530712 current->thread.debug = vcpu->arch.shadow_dbg_reg;
713
Bharat Bhushan08c9a182013-11-18 11:18:54 +0530714 vcpu->arch.pgdir = current->mm->pgd;
Scott Wood5f1c2482013-07-10 17:47:39 -0500715 kvmppc_fix_ee_before_entry();
Scott Woodf8941fbe2013-06-11 11:38:31 -0500716
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000717 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
Scott Wood8fae8452011-12-20 15:34:45 +0000718
Alexander Graf24afa37b2012-08-12 12:42:30 +0200719 /* No need for kvm_guest_exit. It's done in handle_exit.
720 We also get here with interrupts enabled. */
721
Bharat Bhushance11e482013-07-04 12:27:47 +0530722 /* Switch back to user space debug context */
Scott Woodf5f97212013-11-22 15:52:29 -0600723 switch_booke_debug_regs(&debug);
724 current->thread.debug = debug;
Bharat Bhushance11e482013-07-04 12:27:47 +0530725
Scott Wood8fae8452011-12-20 15:34:45 +0000726#ifdef CONFIG_PPC_FPU
727 kvmppc_save_guest_fp(vcpu);
728
729 vcpu->fpu_active = 0;
Scott Wood8fae8452011-12-20 15:34:45 +0000730#endif
731
Scott Wood1d1ef222011-11-08 16:11:59 -0600732out:
Alexander Grafd69c6432012-08-08 20:44:20 +0200733 vcpu->mode = OUTSIDE_GUEST_MODE;
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000734 return ret;
735}
736
Scott Woodd30f6e42011-12-20 15:34:43 +0000737static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
738{
739 enum emulation_result er;
740
741 er = kvmppc_emulate_instruction(run, vcpu);
742 switch (er) {
743 case EMULATE_DONE:
744 /* don't overwrite subtypes, just account kvm_stats */
745 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
746 /* Future optimization: only reload non-volatiles if
747 * they were actually modified by emulation. */
748 return RESUME_GUEST_NV;
749
750 case EMULATE_DO_DCR:
751 run->exit_reason = KVM_EXIT_DCR;
752 return RESUME_HOST;
753
754 case EMULATE_FAIL:
Scott Woodd30f6e42011-12-20 15:34:43 +0000755 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
756 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
757 /* For debugging, encode the failing instruction and
758 * report it to userspace. */
759 run->hw.hardware_exit_reason = ~0ULL << 32;
760 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
Alexander Grafd1ff5492012-02-16 13:24:03 +0000761 kvmppc_core_queue_program(vcpu, ESR_PIL);
Scott Woodd30f6e42011-12-20 15:34:43 +0000762 return RESUME_HOST;
763
Bharat Bhushan9b4f5302013-04-08 00:32:15 +0000764 case EMULATE_EXIT_USER:
765 return RESUME_HOST;
766
Scott Woodd30f6e42011-12-20 15:34:43 +0000767 default:
768 BUG();
769 }
770}
771
Bharat Bhushance11e482013-07-04 12:27:47 +0530772static int kvmppc_handle_debug(struct kvm_run *run, struct kvm_vcpu *vcpu)
773{
774 struct debug_reg *dbg_reg = &(vcpu->arch.shadow_dbg_reg);
775 u32 dbsr = vcpu->arch.dbsr;
776
777 run->debug.arch.status = 0;
778 run->debug.arch.address = vcpu->arch.pc;
779
780 if (dbsr & (DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4)) {
781 run->debug.arch.status |= KVMPPC_DEBUG_BREAKPOINT;
782 } else {
783 if (dbsr & (DBSR_DAC1W | DBSR_DAC2W))
784 run->debug.arch.status |= KVMPPC_DEBUG_WATCH_WRITE;
785 else if (dbsr & (DBSR_DAC1R | DBSR_DAC2R))
786 run->debug.arch.status |= KVMPPC_DEBUG_WATCH_READ;
787 if (dbsr & (DBSR_DAC1R | DBSR_DAC1W))
788 run->debug.arch.address = dbg_reg->dac1;
789 else if (dbsr & (DBSR_DAC2R | DBSR_DAC2W))
790 run->debug.arch.address = dbg_reg->dac2;
791 }
792
793 return RESUME_HOST;
794}
795
Alexander Graf4e642cc2012-02-20 23:57:26 +0100796static void kvmppc_fill_pt_regs(struct pt_regs *regs)
797{
798 ulong r1, ip, msr, lr;
799
800 asm("mr %0, 1" : "=r"(r1));
801 asm("mflr %0" : "=r"(lr));
802 asm("mfmsr %0" : "=r"(msr));
803 asm("bl 1f; 1: mflr %0" : "=r"(ip));
804
805 memset(regs, 0, sizeof(*regs));
806 regs->gpr[1] = r1;
807 regs->nip = ip;
808 regs->msr = msr;
809 regs->link = lr;
810}
811
Bharat Bhushan6328e592012-06-20 05:56:53 +0000812/*
813 * For interrupts needed to be handled by host interrupt handlers,
814 * corresponding host handler are called from here in similar way
815 * (but not exact) as they are called from low level handler
816 * (such as from arch/powerpc/kernel/head_fsl_booke.S).
817 */
Alexander Graf4e642cc2012-02-20 23:57:26 +0100818static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
819 unsigned int exit_nr)
820{
821 struct pt_regs regs;
822
823 switch (exit_nr) {
824 case BOOKE_INTERRUPT_EXTERNAL:
825 kvmppc_fill_pt_regs(&regs);
826 do_IRQ(&regs);
827 break;
828 case BOOKE_INTERRUPT_DECREMENTER:
829 kvmppc_fill_pt_regs(&regs);
830 timer_interrupt(&regs);
831 break;
Tiejun Chen5f17ce82013-05-13 10:00:45 +0800832#if defined(CONFIG_PPC_DOORBELL)
Alexander Graf4e642cc2012-02-20 23:57:26 +0100833 case BOOKE_INTERRUPT_DOORBELL:
834 kvmppc_fill_pt_regs(&regs);
835 doorbell_exception(&regs);
836 break;
837#endif
838 case BOOKE_INTERRUPT_MACHINE_CHECK:
839 /* FIXME */
840 break;
Alexander Graf7cc1e8e2012-02-22 16:26:34 +0100841 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
842 kvmppc_fill_pt_regs(&regs);
843 performance_monitor_exception(&regs);
844 break;
Bharat Bhushan6328e592012-06-20 05:56:53 +0000845 case BOOKE_INTERRUPT_WATCHDOG:
846 kvmppc_fill_pt_regs(&regs);
847#ifdef CONFIG_BOOKE_WDT
848 WatchdogException(&regs);
849#else
850 unknown_exception(&regs);
851#endif
852 break;
853 case BOOKE_INTERRUPT_CRITICAL:
854 unknown_exception(&regs);
855 break;
Bharat Bhushance11e482013-07-04 12:27:47 +0530856 case BOOKE_INTERRUPT_DEBUG:
857 /* Save DBSR before preemption is enabled */
858 vcpu->arch.dbsr = mfspr(SPRN_DBSR);
859 kvmppc_clear_dbsr();
860 break;
Alexander Graf4e642cc2012-02-20 23:57:26 +0100861 }
862}
863
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500864/**
865 * kvmppc_handle_exit
866 *
867 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
868 */
869int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
870 unsigned int exit_nr)
871{
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500872 int r = RESUME_HOST;
Alexander Graf7ee78852012-08-13 12:44:41 +0200873 int s;
Scott Woodf1e89022013-06-06 19:16:31 -0500874 int idx;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500875
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600876 /* update before a new last_exit_type is rewritten */
877 kvmppc_update_timing_stats(vcpu);
878
Alexander Graf4e642cc2012-02-20 23:57:26 +0100879 /* restart interrupts if they were meant for the host */
880 kvmppc_restart_interrupt(vcpu, exit_nr);
Scott Woodd30f6e42011-12-20 15:34:43 +0000881
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500882 local_irq_enable();
883
Alexander Graf97c95052012-08-02 15:10:00 +0200884 trace_kvm_exit(exit_nr, vcpu);
Alexander Graf706fb732012-08-12 11:29:09 +0200885 kvm_guest_exit();
Alexander Graf97c95052012-08-02 15:10:00 +0200886
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500887 run->exit_reason = KVM_EXIT_UNKNOWN;
888 run->ready_for_interrupt_injection = 1;
889
890 switch (exit_nr) {
891 case BOOKE_INTERRUPT_MACHINE_CHECK:
Alexander Grafc35c9d82012-02-20 12:21:18 +0100892 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
893 kvmppc_dump_vcpu(vcpu);
894 /* For debugging, send invalid exit reason to user space */
895 run->hw.hardware_exit_reason = ~1ULL << 32;
896 run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
897 r = RESUME_HOST;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500898 break;
899
900 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600901 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -0600902 r = RESUME_GUEST;
903 break;
904
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500905 case BOOKE_INTERRUPT_DECREMENTER:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600906 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500907 r = RESUME_GUEST;
908 break;
909
Bharat Bhushan6328e592012-06-20 05:56:53 +0000910 case BOOKE_INTERRUPT_WATCHDOG:
911 r = RESUME_GUEST;
912 break;
913
Scott Woodd30f6e42011-12-20 15:34:43 +0000914 case BOOKE_INTERRUPT_DOORBELL:
915 kvmppc_account_exit(vcpu, DBELL_EXITS);
Scott Woodd30f6e42011-12-20 15:34:43 +0000916 r = RESUME_GUEST;
917 break;
918
919 case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
920 kvmppc_account_exit(vcpu, GDBELL_EXITS);
921
922 /*
923 * We are here because there is a pending guest interrupt
924 * which could not be delivered as MSR_CE or MSR_ME was not
925 * set. Once we break from here we will retry delivery.
926 */
927 r = RESUME_GUEST;
928 break;
929
930 case BOOKE_INTERRUPT_GUEST_DBELL:
931 kvmppc_account_exit(vcpu, GDBELL_EXITS);
932
933 /*
934 * We are here because there is a pending guest interrupt
935 * which could not be delivered as MSR_EE was not set. Once
936 * we break from here we will retry delivery.
937 */
938 r = RESUME_GUEST;
939 break;
940
Alexander Graf95f2e922012-02-20 22:45:12 +0100941 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
942 r = RESUME_GUEST;
943 break;
944
Scott Woodd30f6e42011-12-20 15:34:43 +0000945 case BOOKE_INTERRUPT_HV_PRIV:
946 r = emulation_exit(run, vcpu);
947 break;
948
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500949 case BOOKE_INTERRUPT_PROGRAM:
Scott Woodd30f6e42011-12-20 15:34:43 +0000950 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
Alexander Graf0268597c2012-02-20 12:33:22 +0100951 /*
952 * Program traps generated by user-level software must
953 * be handled by the guest kernel.
954 *
955 * In GS mode, hypervisor privileged instructions trap
956 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
957 * actual program interrupts, handled by the guest.
958 */
Liu Yudaf5e272010-02-02 19:44:35 +0800959 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500960 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600961 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500962 break;
963 }
964
Scott Woodd30f6e42011-12-20 15:34:43 +0000965 r = emulation_exit(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500966 break;
967
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200968 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600969 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600970 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200971 r = RESUME_GUEST;
972 break;
973
Scott Wood4cd35f62011-06-14 18:34:31 -0500974#ifdef CONFIG_SPE
975 case BOOKE_INTERRUPT_SPE_UNAVAIL: {
976 if (vcpu->arch.shared->msr & MSR_SPE)
977 kvmppc_vcpu_enable_spe(vcpu);
978 else
979 kvmppc_booke_queue_irqprio(vcpu,
980 BOOKE_IRQPRIO_SPE_UNAVAIL);
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600981 r = RESUME_GUEST;
982 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500983 }
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600984
985 case BOOKE_INTERRUPT_SPE_FP_DATA:
986 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
987 r = RESUME_GUEST;
988 break;
989
990 case BOOKE_INTERRUPT_SPE_FP_ROUND:
991 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
992 r = RESUME_GUEST;
993 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500994#else
995 case BOOKE_INTERRUPT_SPE_UNAVAIL:
996 /*
997 * Guest wants SPE, but host kernel doesn't support it. Send
998 * an "unimplemented operation" program check to the guest.
999 */
1000 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
1001 r = RESUME_GUEST;
1002 break;
1003
1004 /*
1005 * These really should never happen without CONFIG_SPE,
1006 * as we should never enable the real MSR[SPE] in the guest.
1007 */
1008 case BOOKE_INTERRUPT_SPE_FP_DATA:
1009 case BOOKE_INTERRUPT_SPE_FP_ROUND:
1010 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
1011 __func__, exit_nr, vcpu->arch.pc);
1012 run->hw.hardware_exit_reason = exit_nr;
1013 r = RESUME_HOST;
1014 break;
1015#endif
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -06001016
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001017 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +08001018 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
1019 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001020 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001021 r = RESUME_GUEST;
1022 break;
1023
1024 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +08001025 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001026 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001027 r = RESUME_GUEST;
1028 break;
1029
Alexander Graf011da892013-01-31 14:17:38 +01001030 case BOOKE_INTERRUPT_ALIGNMENT:
1031 kvmppc_core_queue_alignment(vcpu, vcpu->arch.fault_dear,
1032 vcpu->arch.fault_esr);
1033 r = RESUME_GUEST;
1034 break;
1035
Scott Woodd30f6e42011-12-20 15:34:43 +00001036#ifdef CONFIG_KVM_BOOKE_HV
1037 case BOOKE_INTERRUPT_HV_SYSCALL:
1038 if (!(vcpu->arch.shared->msr & MSR_PR)) {
1039 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1040 } else {
1041 /*
1042 * hcall from guest userspace -- send privileged
1043 * instruction program check.
1044 */
1045 kvmppc_core_queue_program(vcpu, ESR_PPR);
1046 }
1047
1048 r = RESUME_GUEST;
1049 break;
1050#else
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001051 case BOOKE_INTERRUPT_SYSCALL:
Alexander Graf2a342ed2010-07-29 14:47:48 +02001052 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1053 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
1054 /* KVM PV hypercalls */
1055 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1056 r = RESUME_GUEST;
1057 } else {
1058 /* Guest syscalls */
1059 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
1060 }
Hollis Blanchard7b701592008-12-02 15:51:58 -06001061 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001062 r = RESUME_GUEST;
1063 break;
Scott Woodd30f6e42011-12-20 15:34:43 +00001064#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001065
1066 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001067 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001068 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001069 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001070 gfn_t gfn;
1071
Alexander Grafbf7ca4b2012-02-15 23:40:00 +00001072#ifdef CONFIG_KVM_E500V2
Scott Wooda4cd8b22011-06-14 18:34:41 -05001073 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1074 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
1075 kvmppc_map_magic(vcpu);
1076 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
1077 r = RESUME_GUEST;
1078
1079 break;
1080 }
1081#endif
1082
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001083 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -06001084 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001085 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001086 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +08001087 kvmppc_core_queue_dtlb_miss(vcpu,
1088 vcpu->arch.fault_dear,
1089 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -06001090 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001091 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001092 r = RESUME_GUEST;
1093 break;
1094 }
1095
Scott Woodf1e89022013-06-06 19:16:31 -05001096 idx = srcu_read_lock(&vcpu->kvm->srcu);
1097
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -06001098 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001099 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001100
1101 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1102 /* The guest TLB had a mapping, but the shadow TLB
1103 * didn't, and it is RAM. This could be because:
1104 * a) the entry is mapping the host kernel, or
1105 * b) the guest used a large mapping which we're faking
1106 * Either way, we need to satisfy the fault without
1107 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -06001108 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001109 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001110 r = RESUME_GUEST;
1111 } else {
1112 /* Guest has mapped and accessed a page which is not
1113 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001114 vcpu->arch.paddr_accessed = gpaddr;
Alexander Graf6020c0f2012-03-12 02:26:30 +01001115 vcpu->arch.vaddr_accessed = eaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001116 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001117 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001118 }
1119
Scott Woodf1e89022013-06-06 19:16:31 -05001120 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001121 break;
1122 }
1123
1124 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001125 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -06001126 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001127 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001128 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001129
1130 r = RESUME_GUEST;
1131
1132 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -06001133 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001134 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001135 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001136 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -06001137 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001138 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001139 break;
1140 }
1141
Hollis Blanchard7b701592008-12-02 15:51:58 -06001142 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001143
Scott Woodf1e89022013-06-06 19:16:31 -05001144 idx = srcu_read_lock(&vcpu->kvm->srcu);
1145
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -06001146 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -06001147 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001148
1149 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1150 /* The guest TLB had a mapping, but the shadow TLB
1151 * didn't. This could be because:
1152 * a) the entry is mapping the host kernel, or
1153 * b) the guest used a large mapping which we're faking
1154 * Either way, we need to satisfy the fault without
1155 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -06001156 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001157 } else {
1158 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001159 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001160 }
1161
Scott Woodf1e89022013-06-06 19:16:31 -05001162 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001163 break;
1164 }
1165
Hollis Blanchard6a0ab732008-07-25 13:54:49 -05001166 case BOOKE_INTERRUPT_DEBUG: {
Bharat Bhushance11e482013-07-04 12:27:47 +05301167 r = kvmppc_handle_debug(run, vcpu);
1168 if (r == RESUME_HOST)
1169 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -06001170 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -05001171 break;
1172 }
1173
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001174 default:
1175 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
1176 BUG();
1177 }
1178
Alexander Grafa8e4ef82012-02-16 14:07:37 +00001179 /*
1180 * To avoid clobbering exit_reason, only check for signals if we
1181 * aren't already exiting to userspace for some other reason.
1182 */
Alexander Graf03660ba2012-02-28 12:00:41 +01001183 if (!(r & RESUME_HOST)) {
Alexander Graf7ee78852012-08-13 12:44:41 +02001184 s = kvmppc_prepare_to_enter(vcpu);
Scott Wood6c85f522014-01-09 19:18:40 -06001185 if (s <= 0)
Alexander Graf7ee78852012-08-13 12:44:41 +02001186 r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
Scott Wood6c85f522014-01-09 19:18:40 -06001187 else {
1188 /* interrupts now hard-disabled */
Scott Wood5f1c2482013-07-10 17:47:39 -05001189 kvmppc_fix_ee_before_entry();
Alexander Graf03660ba2012-02-28 12:00:41 +01001190 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001191 }
1192
1193 return r;
1194}
1195
Bharat Bhushand26f22c2013-02-24 18:57:11 +00001196static void kvmppc_set_tsr(struct kvm_vcpu *vcpu, u32 new_tsr)
1197{
1198 u32 old_tsr = vcpu->arch.tsr;
1199
1200 vcpu->arch.tsr = new_tsr;
1201
1202 if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS))
1203 arm_next_watchdog(vcpu);
1204
1205 update_timer_ints(vcpu);
1206}
1207
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001208/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
1209int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1210{
Hollis Blanchard082decf2010-08-07 10:33:56 -07001211 int i;
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001212 int r;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001213
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001214 vcpu->arch.pc = 0;
Scott Woodb5904972011-11-08 18:23:30 -06001215 vcpu->arch.shared->pir = vcpu->vcpu_id;
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001216 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Scott Woodd30f6e42011-12-20 15:34:43 +00001217 kvmppc_set_msr(vcpu, 0);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001218
Scott Woodd30f6e42011-12-20 15:34:43 +00001219#ifndef CONFIG_KVM_BOOKE_HV
Bharat Bhushance11e482013-07-04 12:27:47 +05301220 vcpu->arch.shadow_msr = MSR_USER | MSR_IS | MSR_DS;
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001221 vcpu->arch.shadow_pid = 1;
Scott Woodd30f6e42011-12-20 15:34:43 +00001222 vcpu->arch.shared->msr = 0;
1223#endif
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001224
Hollis Blanchard082decf2010-08-07 10:33:56 -07001225 /* Eye-catching numbers so we know if the guest takes an interrupt
1226 * before it's programmed its own IVPR/IVORs. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001227 vcpu->arch.ivpr = 0x55550000;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001228 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
1229 vcpu->arch.ivor[i] = 0x7700 | i * 4;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001230
Hollis Blanchard73e75b42008-12-02 15:51:57 -06001231 kvmppc_init_timing_stats(vcpu);
1232
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001233 r = kvmppc_core_vcpu_setup(vcpu);
1234 kvmppc_sanity_check(vcpu);
1235 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001236}
1237
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001238int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
1239{
1240 /* setup watchdog timer once */
1241 spin_lock_init(&vcpu->arch.wdt_lock);
1242 setup_timer(&vcpu->arch.wdt_timer, kvmppc_watchdog_func,
1243 (unsigned long)vcpu);
1244
1245 return 0;
1246}
1247
1248void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
1249{
1250 del_timer_sync(&vcpu->arch.wdt_timer);
1251}
1252
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001253int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1254{
1255 int i;
1256
1257 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001258 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001259 regs->ctr = vcpu->arch.ctr;
1260 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001261 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001262 regs->msr = vcpu->arch.shared->msr;
Bharat Bhushan31579ee2014-07-17 17:01:36 +05301263 regs->srr0 = kvmppc_get_srr0(vcpu);
1264 regs->srr1 = kvmppc_get_srr1(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001265 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001266 regs->sprg0 = vcpu->arch.shared->sprg0;
1267 regs->sprg1 = vcpu->arch.shared->sprg1;
1268 regs->sprg2 = vcpu->arch.shared->sprg2;
1269 regs->sprg3 = vcpu->arch.shared->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -06001270 regs->sprg4 = vcpu->arch.shared->sprg4;
1271 regs->sprg5 = vcpu->arch.shared->sprg5;
1272 regs->sprg6 = vcpu->arch.shared->sprg6;
1273 regs->sprg7 = vcpu->arch.shared->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001274
1275 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001276 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001277
1278 return 0;
1279}
1280
1281int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1282{
1283 int i;
1284
1285 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001286 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001287 vcpu->arch.ctr = regs->ctr;
1288 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001289 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -06001290 kvmppc_set_msr(vcpu, regs->msr);
Bharat Bhushan31579ee2014-07-17 17:01:36 +05301291 kvmppc_set_srr0(vcpu, regs->srr0);
1292 kvmppc_set_srr1(vcpu, regs->srr1);
Scott Wood5ce941e2011-04-27 17:24:21 -05001293 kvmppc_set_pid(vcpu, regs->pid);
Alexander Grafa73a9592010-07-29 14:47:47 +02001294 vcpu->arch.shared->sprg0 = regs->sprg0;
1295 vcpu->arch.shared->sprg1 = regs->sprg1;
1296 vcpu->arch.shared->sprg2 = regs->sprg2;
1297 vcpu->arch.shared->sprg3 = regs->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -06001298 vcpu->arch.shared->sprg4 = regs->sprg4;
1299 vcpu->arch.shared->sprg5 = regs->sprg5;
1300 vcpu->arch.shared->sprg6 = regs->sprg6;
1301 vcpu->arch.shared->sprg7 = regs->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001302
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001303 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1304 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001305
1306 return 0;
1307}
1308
Scott Wood5ce941e2011-04-27 17:24:21 -05001309static void get_sregs_base(struct kvm_vcpu *vcpu,
1310 struct kvm_sregs *sregs)
1311{
1312 u64 tb = get_tb();
1313
1314 sregs->u.e.features |= KVM_SREGS_E_BASE;
1315
1316 sregs->u.e.csrr0 = vcpu->arch.csrr0;
1317 sregs->u.e.csrr1 = vcpu->arch.csrr1;
1318 sregs->u.e.mcsr = vcpu->arch.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +00001319 sregs->u.e.esr = get_guest_esr(vcpu);
1320 sregs->u.e.dear = get_guest_dear(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001321 sregs->u.e.tsr = vcpu->arch.tsr;
1322 sregs->u.e.tcr = vcpu->arch.tcr;
1323 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
1324 sregs->u.e.tb = tb;
1325 sregs->u.e.vrsave = vcpu->arch.vrsave;
1326}
1327
1328static int set_sregs_base(struct kvm_vcpu *vcpu,
1329 struct kvm_sregs *sregs)
1330{
1331 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
1332 return 0;
1333
1334 vcpu->arch.csrr0 = sregs->u.e.csrr0;
1335 vcpu->arch.csrr1 = sregs->u.e.csrr1;
1336 vcpu->arch.mcsr = sregs->u.e.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +00001337 set_guest_esr(vcpu, sregs->u.e.esr);
1338 set_guest_dear(vcpu, sregs->u.e.dear);
Scott Wood5ce941e2011-04-27 17:24:21 -05001339 vcpu->arch.vrsave = sregs->u.e.vrsave;
Scott Wooddfd4d472011-11-17 12:39:59 +00001340 kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001341
Scott Wooddfd4d472011-11-17 12:39:59 +00001342 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
Scott Wood5ce941e2011-04-27 17:24:21 -05001343 vcpu->arch.dec = sregs->u.e.dec;
Scott Wooddfd4d472011-11-17 12:39:59 +00001344 kvmppc_emulate_dec(vcpu);
1345 }
Scott Wood5ce941e2011-04-27 17:24:21 -05001346
Bharat Bhushand26f22c2013-02-24 18:57:11 +00001347 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR)
1348 kvmppc_set_tsr(vcpu, sregs->u.e.tsr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001349
1350 return 0;
1351}
1352
1353static void get_sregs_arch206(struct kvm_vcpu *vcpu,
1354 struct kvm_sregs *sregs)
1355{
1356 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
1357
Scott Wood841741f2011-09-02 17:39:37 -05001358 sregs->u.e.pir = vcpu->vcpu_id;
Scott Wood5ce941e2011-04-27 17:24:21 -05001359 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
1360 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
1361 sregs->u.e.decar = vcpu->arch.decar;
1362 sregs->u.e.ivpr = vcpu->arch.ivpr;
1363}
1364
1365static int set_sregs_arch206(struct kvm_vcpu *vcpu,
1366 struct kvm_sregs *sregs)
1367{
1368 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
1369 return 0;
1370
Scott Wood841741f2011-09-02 17:39:37 -05001371 if (sregs->u.e.pir != vcpu->vcpu_id)
Scott Wood5ce941e2011-04-27 17:24:21 -05001372 return -EINVAL;
1373
1374 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
1375 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
1376 vcpu->arch.decar = sregs->u.e.decar;
1377 vcpu->arch.ivpr = sregs->u.e.ivpr;
1378
1379 return 0;
1380}
1381
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301382int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Scott Wood5ce941e2011-04-27 17:24:21 -05001383{
1384 sregs->u.e.features |= KVM_SREGS_E_IVOR;
1385
1386 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
1387 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
1388 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
1389 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
1390 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
1391 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
1392 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
1393 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
1394 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
1395 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
1396 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
1397 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
1398 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
1399 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
1400 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
1401 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301402 return 0;
Scott Wood5ce941e2011-04-27 17:24:21 -05001403}
1404
1405int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1406{
1407 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
1408 return 0;
1409
1410 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
1411 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
1412 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
1413 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
1414 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
1415 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
1416 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
1417 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
1418 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
1419 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
1420 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
1421 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
1422 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
1423 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
1424 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
1425 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
1426
1427 return 0;
1428}
1429
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001430int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1431 struct kvm_sregs *sregs)
1432{
Scott Wood5ce941e2011-04-27 17:24:21 -05001433 sregs->pvr = vcpu->arch.pvr;
1434
1435 get_sregs_base(vcpu, sregs);
1436 get_sregs_arch206(vcpu, sregs);
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301437 return vcpu->kvm->arch.kvm_ops->get_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001438}
1439
1440int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1441 struct kvm_sregs *sregs)
1442{
Scott Wood5ce941e2011-04-27 17:24:21 -05001443 int ret;
1444
1445 if (vcpu->arch.pvr != sregs->pvr)
1446 return -EINVAL;
1447
1448 ret = set_sregs_base(vcpu, sregs);
1449 if (ret < 0)
1450 return ret;
1451
1452 ret = set_sregs_arch206(vcpu, sregs);
1453 if (ret < 0)
1454 return ret;
1455
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301456 return vcpu->kvm->arch.kvm_ops->set_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001457}
1458
Paul Mackerras31f34382011-12-12 12:26:50 +00001459int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1460{
Mihai Caraman35b299e2013-04-11 00:03:07 +00001461 int r = 0;
1462 union kvmppc_one_reg val;
1463 int size;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001464
1465 size = one_reg_size(reg->id);
1466 if (size > sizeof(val))
1467 return -EINVAL;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001468
1469 switch (reg->id) {
1470 case KVM_REG_PPC_IAC1:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301471 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac1);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001472 break;
Bharat Bhushan547465e2013-07-04 12:27:46 +05301473 case KVM_REG_PPC_IAC2:
1474 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac2);
1475 break;
1476#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1477 case KVM_REG_PPC_IAC3:
1478 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac3);
1479 break;
1480 case KVM_REG_PPC_IAC4:
1481 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac4);
1482 break;
1483#endif
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001484 case KVM_REG_PPC_DAC1:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301485 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.dac1);
1486 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001487 case KVM_REG_PPC_DAC2:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301488 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.dac2);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001489 break;
Alexander Graf324b3e62013-01-04 18:28:51 +01001490 case KVM_REG_PPC_EPR: {
1491 u32 epr = get_guest_epr(vcpu);
Mihai Caraman35b299e2013-04-11 00:03:07 +00001492 val = get_reg_val(reg->id, epr);
Alexander Graf324b3e62013-01-04 18:28:51 +01001493 break;
1494 }
Mihai Caraman352df1d2012-10-11 06:13:29 +00001495#if defined(CONFIG_64BIT)
1496 case KVM_REG_PPC_EPCR:
Mihai Caraman35b299e2013-04-11 00:03:07 +00001497 val = get_reg_val(reg->id, vcpu->arch.epcr);
Mihai Caraman352df1d2012-10-11 06:13:29 +00001498 break;
1499#endif
Bharat Bhushan78accda2013-02-24 18:57:12 +00001500 case KVM_REG_PPC_TCR:
Mihai Caraman35b299e2013-04-11 00:03:07 +00001501 val = get_reg_val(reg->id, vcpu->arch.tcr);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001502 break;
1503 case KVM_REG_PPC_TSR:
Mihai Caraman35b299e2013-04-11 00:03:07 +00001504 val = get_reg_val(reg->id, vcpu->arch.tsr);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001505 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001506 case KVM_REG_PPC_DEBUG_INST:
Bharat Bhushanb12c7842013-07-04 12:27:45 +05301507 val = get_reg_val(reg->id, KVMPPC_INST_EHPRIV_DEBUG);
Bharat Bhushan8c32a2e2013-03-20 20:24:58 +00001508 break;
Paul Mackerras8b75cbb2013-09-20 14:52:37 +10001509 case KVM_REG_PPC_VRSAVE:
1510 val = get_reg_val(reg->id, vcpu->arch.vrsave);
Bharat Bhushan8c32a2e2013-03-20 20:24:58 +00001511 break;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001512 default:
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301513 r = vcpu->kvm->arch.kvm_ops->get_one_reg(vcpu, reg->id, &val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001514 break;
1515 }
Mihai Caraman35b299e2013-04-11 00:03:07 +00001516
1517 if (r)
1518 return r;
1519
1520 if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
1521 r = -EFAULT;
1522
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001523 return r;
Paul Mackerras31f34382011-12-12 12:26:50 +00001524}
1525
1526int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1527{
Mihai Caraman35b299e2013-04-11 00:03:07 +00001528 int r = 0;
1529 union kvmppc_one_reg val;
1530 int size;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001531
1532 size = one_reg_size(reg->id);
1533 if (size > sizeof(val))
1534 return -EINVAL;
1535
1536 if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
1537 return -EFAULT;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001538
1539 switch (reg->id) {
1540 case KVM_REG_PPC_IAC1:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301541 vcpu->arch.dbg_reg.iac1 = set_reg_val(reg->id, val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001542 break;
Bharat Bhushan547465e2013-07-04 12:27:46 +05301543 case KVM_REG_PPC_IAC2:
1544 vcpu->arch.dbg_reg.iac2 = set_reg_val(reg->id, val);
1545 break;
1546#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1547 case KVM_REG_PPC_IAC3:
1548 vcpu->arch.dbg_reg.iac3 = set_reg_val(reg->id, val);
1549 break;
1550 case KVM_REG_PPC_IAC4:
1551 vcpu->arch.dbg_reg.iac4 = set_reg_val(reg->id, val);
1552 break;
1553#endif
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001554 case KVM_REG_PPC_DAC1:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301555 vcpu->arch.dbg_reg.dac1 = set_reg_val(reg->id, val);
1556 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001557 case KVM_REG_PPC_DAC2:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301558 vcpu->arch.dbg_reg.dac2 = set_reg_val(reg->id, val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001559 break;
Alexander Graf324b3e62013-01-04 18:28:51 +01001560 case KVM_REG_PPC_EPR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001561 u32 new_epr = set_reg_val(reg->id, val);
1562 kvmppc_set_epr(vcpu, new_epr);
Alexander Graf324b3e62013-01-04 18:28:51 +01001563 break;
1564 }
Mihai Caraman352df1d2012-10-11 06:13:29 +00001565#if defined(CONFIG_64BIT)
1566 case KVM_REG_PPC_EPCR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001567 u32 new_epcr = set_reg_val(reg->id, val);
1568 kvmppc_set_epcr(vcpu, new_epcr);
Mihai Caraman352df1d2012-10-11 06:13:29 +00001569 break;
1570 }
1571#endif
Bharat Bhushan78accda2013-02-24 18:57:12 +00001572 case KVM_REG_PPC_OR_TSR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001573 u32 tsr_bits = set_reg_val(reg->id, val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001574 kvmppc_set_tsr_bits(vcpu, tsr_bits);
1575 break;
1576 }
1577 case KVM_REG_PPC_CLEAR_TSR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001578 u32 tsr_bits = set_reg_val(reg->id, val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001579 kvmppc_clr_tsr_bits(vcpu, tsr_bits);
1580 break;
1581 }
1582 case KVM_REG_PPC_TSR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001583 u32 tsr = set_reg_val(reg->id, val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001584 kvmppc_set_tsr(vcpu, tsr);
1585 break;
1586 }
1587 case KVM_REG_PPC_TCR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001588 u32 tcr = set_reg_val(reg->id, val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001589 kvmppc_set_tcr(vcpu, tcr);
1590 break;
1591 }
Paul Mackerras8b75cbb2013-09-20 14:52:37 +10001592 case KVM_REG_PPC_VRSAVE:
1593 vcpu->arch.vrsave = set_reg_val(reg->id, val);
1594 break;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001595 default:
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301596 r = vcpu->kvm->arch.kvm_ops->set_one_reg(vcpu, reg->id, &val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001597 break;
1598 }
Mihai Caraman35b299e2013-04-11 00:03:07 +00001599
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001600 return r;
Paul Mackerras31f34382011-12-12 12:26:50 +00001601}
1602
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001603int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1604{
1605 return -ENOTSUPP;
1606}
1607
1608int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1609{
1610 return -ENOTSUPP;
1611}
1612
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001613int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1614 struct kvm_translation *tr)
1615{
Avi Kivity98001d82010-05-13 11:05:49 +03001616 int r;
1617
Avi Kivity98001d82010-05-13 11:05:49 +03001618 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +03001619 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001620}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001621
Alexander Graf4e755752009-10-30 05:47:01 +00001622int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1623{
1624 return -ENOTSUPP;
1625}
1626
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05301627void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001628 struct kvm_memory_slot *dont)
1629{
1630}
1631
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05301632int kvmppc_core_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001633 unsigned long npages)
1634{
1635 return 0;
1636}
1637
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001638int kvmppc_core_prepare_memory_region(struct kvm *kvm,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001639 struct kvm_memory_slot *memslot,
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001640 struct kvm_userspace_memory_region *mem)
1641{
1642 return 0;
1643}
1644
1645void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001646 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001647 const struct kvm_memory_slot *old)
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001648{
1649}
1650
1651void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001652{
1653}
1654
Mihai Caraman38f98822012-10-11 06:13:27 +00001655void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr)
1656{
1657#if defined(CONFIG_64BIT)
1658 vcpu->arch.epcr = new_epcr;
1659#ifdef CONFIG_KVM_BOOKE_HV
1660 vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM;
1661 if (vcpu->arch.epcr & SPRN_EPCR_ICM)
1662 vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM;
1663#endif
1664#endif
1665}
1666
Scott Wooddfd4d472011-11-17 12:39:59 +00001667void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1668{
1669 vcpu->arch.tcr = new_tcr;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001670 arm_next_watchdog(vcpu);
Scott Wooddfd4d472011-11-17 12:39:59 +00001671 update_timer_ints(vcpu);
1672}
1673
1674void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1675{
1676 set_bits(tsr_bits, &vcpu->arch.tsr);
1677 smp_wmb();
1678 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1679 kvm_vcpu_kick(vcpu);
1680}
1681
1682void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1683{
1684 clear_bits(tsr_bits, &vcpu->arch.tsr);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001685
1686 /*
1687 * We may have stopped the watchdog due to
1688 * being stuck on final expiration.
1689 */
1690 if (tsr_bits & (TSR_ENW | TSR_WIS))
1691 arm_next_watchdog(vcpu);
1692
Scott Wooddfd4d472011-11-17 12:39:59 +00001693 update_timer_ints(vcpu);
1694}
1695
1696void kvmppc_decrementer_func(unsigned long data)
1697{
1698 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1699
Bharat Bhushan21bd0002012-05-20 23:21:23 +00001700 if (vcpu->arch.tcr & TCR_ARE) {
1701 vcpu->arch.dec = vcpu->arch.decar;
1702 kvmppc_emulate_dec(vcpu);
1703 }
1704
Scott Wooddfd4d472011-11-17 12:39:59 +00001705 kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1706}
1707
Bharat Bhushance11e482013-07-04 12:27:47 +05301708static int kvmppc_booke_add_breakpoint(struct debug_reg *dbg_reg,
1709 uint64_t addr, int index)
1710{
1711 switch (index) {
1712 case 0:
1713 dbg_reg->dbcr0 |= DBCR0_IAC1;
1714 dbg_reg->iac1 = addr;
1715 break;
1716 case 1:
1717 dbg_reg->dbcr0 |= DBCR0_IAC2;
1718 dbg_reg->iac2 = addr;
1719 break;
1720#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1721 case 2:
1722 dbg_reg->dbcr0 |= DBCR0_IAC3;
1723 dbg_reg->iac3 = addr;
1724 break;
1725 case 3:
1726 dbg_reg->dbcr0 |= DBCR0_IAC4;
1727 dbg_reg->iac4 = addr;
1728 break;
1729#endif
1730 default:
1731 return -EINVAL;
1732 }
1733
1734 dbg_reg->dbcr0 |= DBCR0_IDM;
1735 return 0;
1736}
1737
1738static int kvmppc_booke_add_watchpoint(struct debug_reg *dbg_reg, uint64_t addr,
1739 int type, int index)
1740{
1741 switch (index) {
1742 case 0:
1743 if (type & KVMPPC_DEBUG_WATCH_READ)
1744 dbg_reg->dbcr0 |= DBCR0_DAC1R;
1745 if (type & KVMPPC_DEBUG_WATCH_WRITE)
1746 dbg_reg->dbcr0 |= DBCR0_DAC1W;
1747 dbg_reg->dac1 = addr;
1748 break;
1749 case 1:
1750 if (type & KVMPPC_DEBUG_WATCH_READ)
1751 dbg_reg->dbcr0 |= DBCR0_DAC2R;
1752 if (type & KVMPPC_DEBUG_WATCH_WRITE)
1753 dbg_reg->dbcr0 |= DBCR0_DAC2W;
1754 dbg_reg->dac2 = addr;
1755 break;
1756 default:
1757 return -EINVAL;
1758 }
1759
1760 dbg_reg->dbcr0 |= DBCR0_IDM;
1761 return 0;
1762}
1763void kvm_guest_protect_msr(struct kvm_vcpu *vcpu, ulong prot_bitmap, bool set)
1764{
1765 /* XXX: Add similar MSR protection for BookE-PR */
1766#ifdef CONFIG_KVM_BOOKE_HV
1767 BUG_ON(prot_bitmap & ~(MSRP_UCLEP | MSRP_DEP | MSRP_PMMP));
1768 if (set) {
1769 if (prot_bitmap & MSR_UCLE)
1770 vcpu->arch.shadow_msrp |= MSRP_UCLEP;
1771 if (prot_bitmap & MSR_DE)
1772 vcpu->arch.shadow_msrp |= MSRP_DEP;
1773 if (prot_bitmap & MSR_PMM)
1774 vcpu->arch.shadow_msrp |= MSRP_PMMP;
1775 } else {
1776 if (prot_bitmap & MSR_UCLE)
1777 vcpu->arch.shadow_msrp &= ~MSRP_UCLEP;
1778 if (prot_bitmap & MSR_DE)
1779 vcpu->arch.shadow_msrp &= ~MSRP_DEP;
1780 if (prot_bitmap & MSR_PMM)
1781 vcpu->arch.shadow_msrp &= ~MSRP_PMMP;
1782 }
1783#endif
1784}
1785
1786int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1787 struct kvm_guest_debug *dbg)
1788{
1789 struct debug_reg *dbg_reg;
1790 int n, b = 0, w = 0;
1791
1792 if (!(dbg->control & KVM_GUESTDBG_ENABLE)) {
1793 vcpu->arch.shadow_dbg_reg.dbcr0 = 0;
1794 vcpu->guest_debug = 0;
1795 kvm_guest_protect_msr(vcpu, MSR_DE, false);
1796 return 0;
1797 }
1798
1799 kvm_guest_protect_msr(vcpu, MSR_DE, true);
1800 vcpu->guest_debug = dbg->control;
1801 vcpu->arch.shadow_dbg_reg.dbcr0 = 0;
1802 /* Set DBCR0_EDM in guest visible DBCR0 register. */
1803 vcpu->arch.dbg_reg.dbcr0 = DBCR0_EDM;
1804
1805 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1806 vcpu->arch.shadow_dbg_reg.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1807
1808 /* Code below handles only HW breakpoints */
1809 dbg_reg = &(vcpu->arch.shadow_dbg_reg);
1810
1811#ifdef CONFIG_KVM_BOOKE_HV
1812 /*
1813 * On BookE-HV (e500mc) the guest is always executed with MSR.GS=1
1814 * DBCR1 and DBCR2 are set to trigger debug events when MSR.PR is 0
1815 */
1816 dbg_reg->dbcr1 = 0;
1817 dbg_reg->dbcr2 = 0;
1818#else
1819 /*
1820 * On BookE-PR (e500v2) the guest is always executed with MSR.PR=1
1821 * We set DBCR1 and DBCR2 to only trigger debug events when MSR.PR
1822 * is set.
1823 */
1824 dbg_reg->dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US | DBCR1_IAC3US |
1825 DBCR1_IAC4US;
1826 dbg_reg->dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
1827#endif
1828
1829 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1830 return 0;
1831
1832 for (n = 0; n < (KVMPPC_BOOKE_IAC_NUM + KVMPPC_BOOKE_DAC_NUM); n++) {
1833 uint64_t addr = dbg->arch.bp[n].addr;
1834 uint32_t type = dbg->arch.bp[n].type;
1835
1836 if (type == KVMPPC_DEBUG_NONE)
1837 continue;
1838
1839 if (type & !(KVMPPC_DEBUG_WATCH_READ |
1840 KVMPPC_DEBUG_WATCH_WRITE |
1841 KVMPPC_DEBUG_BREAKPOINT))
1842 return -EINVAL;
1843
1844 if (type & KVMPPC_DEBUG_BREAKPOINT) {
1845 /* Setting H/W breakpoint */
1846 if (kvmppc_booke_add_breakpoint(dbg_reg, addr, b++))
1847 return -EINVAL;
1848 } else {
1849 /* Setting H/W watchpoint */
1850 if (kvmppc_booke_add_watchpoint(dbg_reg, addr,
1851 type, w++))
1852 return -EINVAL;
1853 }
1854 }
1855
1856 return 0;
1857}
1858
Scott Wood94fa9d92011-12-20 15:34:22 +00001859void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1860{
Paul Mackerrasa47d72f2012-09-20 19:35:51 +00001861 vcpu->cpu = smp_processor_id();
Scott Woodd30f6e42011-12-20 15:34:43 +00001862 current->thread.kvm_vcpu = vcpu;
Scott Wood94fa9d92011-12-20 15:34:22 +00001863}
1864
1865void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
1866{
Scott Woodd30f6e42011-12-20 15:34:43 +00001867 current->thread.kvm_vcpu = NULL;
Paul Mackerrasa47d72f2012-09-20 19:35:51 +00001868 vcpu->cpu = -1;
Bharat Bhushance11e482013-07-04 12:27:47 +05301869
1870 /* Clear pending debug event in DBSR */
1871 kvmppc_clear_dbsr();
Scott Wood94fa9d92011-12-20 15:34:22 +00001872}
1873
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301874void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
1875{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301876 vcpu->kvm->arch.kvm_ops->mmu_destroy(vcpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301877}
1878
1879int kvmppc_core_init_vm(struct kvm *kvm)
1880{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301881 return kvm->arch.kvm_ops->init_vm(kvm);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301882}
1883
1884struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1885{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301886 return kvm->arch.kvm_ops->vcpu_create(kvm, id);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301887}
1888
1889void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1890{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301891 vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301892}
1893
1894void kvmppc_core_destroy_vm(struct kvm *kvm)
1895{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301896 kvm->arch.kvm_ops->destroy_vm(kvm);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301897}
1898
1899void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1900{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301901 vcpu->kvm->arch.kvm_ops->vcpu_load(vcpu, cpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301902}
1903
1904void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
1905{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301906 vcpu->kvm->arch.kvm_ops->vcpu_put(vcpu);
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001907}
1908
1909int __init kvmppc_booke_init(void)
1910{
Scott Woodd30f6e42011-12-20 15:34:43 +00001911#ifndef CONFIG_KVM_BOOKE_HV
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001912 unsigned long ivor[16];
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001913 unsigned long *handler = kvmppc_booke_handler_addr;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001914 unsigned long max_ivor = 0;
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001915 unsigned long handler_len;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001916 int i;
1917
1918 /* We install our own exception handlers by hijacking IVPR. IVPR must
1919 * be 16-bit aligned, so we need a 64KB allocation. */
1920 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1921 VCPU_SIZE_ORDER);
1922 if (!kvmppc_booke_handlers)
1923 return -ENOMEM;
1924
1925 /* XXX make sure our handlers are smaller than Linux's */
1926
1927 /* Copy our interrupt handlers to match host IVORs. That way we don't
1928 * have to swap the IVORs on every guest/host transition. */
1929 ivor[0] = mfspr(SPRN_IVOR0);
1930 ivor[1] = mfspr(SPRN_IVOR1);
1931 ivor[2] = mfspr(SPRN_IVOR2);
1932 ivor[3] = mfspr(SPRN_IVOR3);
1933 ivor[4] = mfspr(SPRN_IVOR4);
1934 ivor[5] = mfspr(SPRN_IVOR5);
1935 ivor[6] = mfspr(SPRN_IVOR6);
1936 ivor[7] = mfspr(SPRN_IVOR7);
1937 ivor[8] = mfspr(SPRN_IVOR8);
1938 ivor[9] = mfspr(SPRN_IVOR9);
1939 ivor[10] = mfspr(SPRN_IVOR10);
1940 ivor[11] = mfspr(SPRN_IVOR11);
1941 ivor[12] = mfspr(SPRN_IVOR12);
1942 ivor[13] = mfspr(SPRN_IVOR13);
1943 ivor[14] = mfspr(SPRN_IVOR14);
1944 ivor[15] = mfspr(SPRN_IVOR15);
1945
1946 for (i = 0; i < 16; i++) {
1947 if (ivor[i] > max_ivor)
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001948 max_ivor = i;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001949
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001950 handler_len = handler[i + 1] - handler[i];
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001951 memcpy((void *)kvmppc_booke_handlers + ivor[i],
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001952 (void *)handler[i], handler_len);
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001953 }
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001954
1955 handler_len = handler[max_ivor + 1] - handler[max_ivor];
1956 flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers +
1957 ivor[max_ivor] + handler_len);
Scott Woodd30f6e42011-12-20 15:34:43 +00001958#endif /* !BOOKE_HV */
Hollis Blancharddb93f572008-11-05 09:36:18 -06001959 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001960}
1961
Hollis Blancharddb93f572008-11-05 09:36:18 -06001962void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001963{
1964 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
1965 kvm_exit();
1966}