blob: 34562d4a3a12edea11644b36563799a4237f91d5 [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
Alexander Graf324b3e62013-01-04 18:28:51 +0100295static unsigned long get_guest_epr(struct kvm_vcpu *vcpu)
296{
297#ifdef CONFIG_KVM_BOOKE_HV
298 return mfspr(SPRN_GEPR);
299#else
300 return vcpu->arch.epr;
301#endif
302}
303
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600304/* Deliver the interrupt of the corresponding priority, if possible. */
305static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
306 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500307{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600308 int allowed = 0;
Alexander Graf79300f82012-02-15 19:12:29 +0000309 ulong msr_mask = 0;
Alexander Graf1c810632013-01-04 18:12:48 +0100310 bool update_esr = false, update_dear = false, update_epr = false;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200311 ulong crit_raw = vcpu->arch.shared->critical;
312 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
313 bool crit;
Alexander Grafc5335f12010-08-30 14:03:24 +0200314 bool keep_irq = false;
Scott Woodd30f6e42011-12-20 15:34:43 +0000315 enum int_class int_class;
Mihai Caraman95e90b42012-10-11 06:13:26 +0000316 ulong new_msr = vcpu->arch.shared->msr;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200317
318 /* Truncate crit indicators in 32 bit mode */
319 if (!(vcpu->arch.shared->msr & MSR_SF)) {
320 crit_raw &= 0xffffffff;
321 crit_r1 &= 0xffffffff;
322 }
323
324 /* Critical section when crit == r1 */
325 crit = (crit_raw == crit_r1);
326 /* ... and we're in supervisor mode */
327 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500328
Alexander Grafc5335f12010-08-30 14:03:24 +0200329 if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
330 priority = BOOKE_IRQPRIO_EXTERNAL;
331 keep_irq = true;
332 }
333
Scott Wood5df554ad2013-04-12 14:08:46 +0000334 if ((priority == BOOKE_IRQPRIO_EXTERNAL) && vcpu->arch.epr_flags)
Alexander Graf1c810632013-01-04 18:12:48 +0100335 update_epr = true;
336
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600337 switch (priority) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600338 case BOOKE_IRQPRIO_DTLB_MISS:
Liu Yudaf5e272010-02-02 19:44:35 +0800339 case BOOKE_IRQPRIO_DATA_STORAGE:
Alexander Graf011da892013-01-31 14:17:38 +0100340 case BOOKE_IRQPRIO_ALIGNMENT:
Liu Yudaf5e272010-02-02 19:44:35 +0800341 update_dear = true;
342 /* fall through */
343 case BOOKE_IRQPRIO_INST_STORAGE:
344 case BOOKE_IRQPRIO_PROGRAM:
345 update_esr = true;
346 /* fall through */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600347 case BOOKE_IRQPRIO_ITLB_MISS:
348 case BOOKE_IRQPRIO_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600349 case BOOKE_IRQPRIO_FP_UNAVAIL:
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600350 case BOOKE_IRQPRIO_SPE_UNAVAIL:
351 case BOOKE_IRQPRIO_SPE_FP_DATA:
352 case BOOKE_IRQPRIO_SPE_FP_ROUND:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600353 case BOOKE_IRQPRIO_AP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600354 allowed = 1;
Alexander Graf79300f82012-02-15 19:12:29 +0000355 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000356 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500357 break;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000358 case BOOKE_IRQPRIO_WATCHDOG:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600359 case BOOKE_IRQPRIO_CRITICAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000360 case BOOKE_IRQPRIO_DBELL_CRIT:
Alexander Graf666e7252010-07-29 14:47:43 +0200361 allowed = vcpu->arch.shared->msr & MSR_CE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000362 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000363 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000364 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500365 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600366 case BOOKE_IRQPRIO_MACHINE_CHECK:
Alexander Graf666e7252010-07-29 14:47:43 +0200367 allowed = vcpu->arch.shared->msr & MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000368 allowed = allowed && !crit;
Scott Woodd30f6e42011-12-20 15:34:43 +0000369 int_class = INT_CLASS_MC;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500370 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600371 case BOOKE_IRQPRIO_DECREMENTER:
372 case BOOKE_IRQPRIO_FIT:
Scott Wooddfd4d472011-11-17 12:39:59 +0000373 keep_irq = true;
374 /* fall through */
375 case BOOKE_IRQPRIO_EXTERNAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000376 case BOOKE_IRQPRIO_DBELL:
Alexander Graf666e7252010-07-29 14:47:43 +0200377 allowed = vcpu->arch.shared->msr & MSR_EE;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200378 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000379 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000380 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500381 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600382 case BOOKE_IRQPRIO_DEBUG:
Alexander Graf666e7252010-07-29 14:47:43 +0200383 allowed = vcpu->arch.shared->msr & MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000384 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000385 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000386 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500387 break;
388 }
389
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600390 if (allowed) {
Scott Woodd30f6e42011-12-20 15:34:43 +0000391 switch (int_class) {
392 case INT_CLASS_NONCRIT:
393 set_guest_srr(vcpu, vcpu->arch.pc,
394 vcpu->arch.shared->msr);
395 break;
396 case INT_CLASS_CRIT:
397 set_guest_csrr(vcpu, vcpu->arch.pc,
398 vcpu->arch.shared->msr);
399 break;
400 case INT_CLASS_DBG:
401 set_guest_dsrr(vcpu, vcpu->arch.pc,
402 vcpu->arch.shared->msr);
403 break;
404 case INT_CLASS_MC:
405 set_guest_mcsrr(vcpu, vcpu->arch.pc,
406 vcpu->arch.shared->msr);
407 break;
408 }
409
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600410 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
Liu Yudaf5e272010-02-02 19:44:35 +0800411 if (update_esr == true)
Bharat Bhushandc168542014-07-17 17:01:38 +0530412 kvmppc_set_esr(vcpu, vcpu->arch.queued_esr);
Liu Yudaf5e272010-02-02 19:44:35 +0800413 if (update_dear == true)
Bharat Bhushana5414d42014-07-17 17:01:37 +0530414 kvmppc_set_dar(vcpu, vcpu->arch.queued_dear);
Scott Wood5df554ad2013-04-12 14:08:46 +0000415 if (update_epr == true) {
416 if (vcpu->arch.epr_flags & KVMPPC_EPR_USER)
417 kvm_make_request(KVM_REQ_EPR_EXIT, vcpu);
Scott Woodeb1e4f42013-04-12 14:08:47 +0000418 else if (vcpu->arch.epr_flags & KVMPPC_EPR_KERNEL) {
419 BUG_ON(vcpu->arch.irq_type != KVMPPC_IRQ_MPIC);
420 kvmppc_mpic_set_epr(vcpu);
421 }
Scott Wood5df554ad2013-04-12 14:08:46 +0000422 }
Mihai Caraman95e90b42012-10-11 06:13:26 +0000423
424 new_msr &= msr_mask;
425#if defined(CONFIG_64BIT)
426 if (vcpu->arch.epcr & SPRN_EPCR_ICM)
427 new_msr |= MSR_CM;
428#endif
429 kvmppc_set_msr(vcpu, new_msr);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600430
Alexander Grafc5335f12010-08-30 14:03:24 +0200431 if (!keep_irq)
432 clear_bit(priority, &vcpu->arch.pending_exceptions);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600433 }
434
Scott Woodd30f6e42011-12-20 15:34:43 +0000435#ifdef CONFIG_KVM_BOOKE_HV
436 /*
437 * If an interrupt is pending but masked, raise a guest doorbell
438 * so that we are notified when the guest enables the relevant
439 * MSR bit.
440 */
441 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
442 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
443 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
444 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
445 if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
446 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
447#endif
448
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600449 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500450}
451
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000452/*
453 * Return the number of jiffies until the next timeout. If the timeout is
454 * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA
455 * because the larger value can break the timer APIs.
456 */
457static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu)
458{
459 u64 tb, wdt_tb, wdt_ticks = 0;
460 u64 nr_jiffies = 0;
461 u32 period = TCR_GET_WP(vcpu->arch.tcr);
462
463 wdt_tb = 1ULL << (63 - period);
464 tb = get_tb();
465 /*
466 * The watchdog timeout will hapeen when TB bit corresponding
467 * to watchdog will toggle from 0 to 1.
468 */
469 if (tb & wdt_tb)
470 wdt_ticks = wdt_tb;
471
472 wdt_ticks += wdt_tb - (tb & (wdt_tb - 1));
473
474 /* Convert timebase ticks to jiffies */
475 nr_jiffies = wdt_ticks;
476
477 if (do_div(nr_jiffies, tb_ticks_per_jiffy))
478 nr_jiffies++;
479
480 return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA);
481}
482
483static void arm_next_watchdog(struct kvm_vcpu *vcpu)
484{
485 unsigned long nr_jiffies;
486 unsigned long flags;
487
488 /*
489 * If TSR_ENW and TSR_WIS are not set then no need to exit to
490 * userspace, so clear the KVM_REQ_WATCHDOG request.
491 */
492 if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS))
493 clear_bit(KVM_REQ_WATCHDOG, &vcpu->requests);
494
495 spin_lock_irqsave(&vcpu->arch.wdt_lock, flags);
496 nr_jiffies = watchdog_next_timeout(vcpu);
497 /*
498 * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA
499 * then do not run the watchdog timer as this can break timer APIs.
500 */
501 if (nr_jiffies < NEXT_TIMER_MAX_DELTA)
502 mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies);
503 else
504 del_timer(&vcpu->arch.wdt_timer);
505 spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags);
506}
507
508void kvmppc_watchdog_func(unsigned long data)
509{
510 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
511 u32 tsr, new_tsr;
512 int final;
513
514 do {
515 new_tsr = tsr = vcpu->arch.tsr;
516 final = 0;
517
518 /* Time out event */
519 if (tsr & TSR_ENW) {
520 if (tsr & TSR_WIS)
521 final = 1;
522 else
523 new_tsr = tsr | TSR_WIS;
524 } else {
525 new_tsr = tsr | TSR_ENW;
526 }
527 } while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr);
528
529 if (new_tsr & TSR_WIS) {
530 smp_wmb();
531 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
532 kvm_vcpu_kick(vcpu);
533 }
534
535 /*
536 * If this is final watchdog expiry and some action is required
537 * then exit to userspace.
538 */
539 if (final && (vcpu->arch.tcr & TCR_WRC_MASK) &&
540 vcpu->arch.watchdog_enabled) {
541 smp_wmb();
542 kvm_make_request(KVM_REQ_WATCHDOG, vcpu);
543 kvm_vcpu_kick(vcpu);
544 }
545
546 /*
547 * Stop running the watchdog timer after final expiration to
548 * prevent the host from being flooded with timers if the
549 * guest sets a short period.
550 * Timers will resume when TSR/TCR is updated next time.
551 */
552 if (!final)
553 arm_next_watchdog(vcpu);
554}
555
Scott Wooddfd4d472011-11-17 12:39:59 +0000556static void update_timer_ints(struct kvm_vcpu *vcpu)
557{
558 if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
559 kvmppc_core_queue_dec(vcpu);
560 else
561 kvmppc_core_dequeue_dec(vcpu);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000562
563 if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS))
564 kvmppc_core_queue_watchdog(vcpu);
565 else
566 kvmppc_core_dequeue_watchdog(vcpu);
Scott Wooddfd4d472011-11-17 12:39:59 +0000567}
568
Scott Woodc59a6a32011-11-08 18:23:25 -0600569static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500570{
571 unsigned long *pending = &vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500572 unsigned int priority;
573
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600574 priority = __ffs(*pending);
Alexander Graf8b3a00f2012-02-16 14:12:46 +0000575 while (priority < BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600576 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500577 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500578
579 priority = find_next_bit(pending,
580 BITS_PER_BYTE * sizeof(*pending),
581 priority + 1);
582 }
Alexander Graf90bba352010-07-29 14:47:51 +0200583
584 /* Tell the guest about our interrupt status */
Scott Wood29ac26e2011-11-08 18:23:27 -0600585 vcpu->arch.shared->int_pending = !!*pending;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500586}
587
Scott Woodc59a6a32011-11-08 18:23:25 -0600588/* Check pending exceptions and deliver one, if possible. */
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000589int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
Scott Woodc59a6a32011-11-08 18:23:25 -0600590{
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000591 int r = 0;
Scott Woodc59a6a32011-11-08 18:23:25 -0600592 WARN_ON_ONCE(!irqs_disabled());
593
594 kvmppc_core_check_exceptions(vcpu);
595
Alexander Grafb8c649a2012-12-20 04:52:39 +0000596 if (vcpu->requests) {
597 /* Exception delivery raised request; start over */
598 return 1;
599 }
600
Scott Woodc59a6a32011-11-08 18:23:25 -0600601 if (vcpu->arch.shared->msr & MSR_WE) {
602 local_irq_enable();
603 kvm_vcpu_block(vcpu);
Alexander Graf966cd0f2012-03-14 16:55:08 +0100604 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Scott Wood6c85f522014-01-09 19:18:40 -0600605 hard_irq_disable();
Scott Woodc59a6a32011-11-08 18:23:25 -0600606
607 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000608 r = 1;
Scott Woodc59a6a32011-11-08 18:23:25 -0600609 };
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000610
611 return r;
612}
613
Alexander Graf7c973a22012-08-13 12:50:35 +0200614int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
Alexander Graf4ffc6352012-08-08 20:31:13 +0200615{
Alexander Graf7c973a22012-08-13 12:50:35 +0200616 int r = 1; /* Indicate we want to get back into the guest */
617
Alexander Graf2d8185d2012-08-10 12:31:12 +0200618 if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
619 update_timer_ints(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200620#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Alexander Graf2d8185d2012-08-10 12:31:12 +0200621 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
622 kvmppc_core_flush_tlb(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200623#endif
Alexander Graf7c973a22012-08-13 12:50:35 +0200624
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000625 if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) {
626 vcpu->run->exit_reason = KVM_EXIT_WATCHDOG;
627 r = 0;
628 }
629
Alexander Graf1c810632013-01-04 18:12:48 +0100630 if (kvm_check_request(KVM_REQ_EPR_EXIT, vcpu)) {
631 vcpu->run->epr.epr = 0;
632 vcpu->arch.epr_needed = true;
633 vcpu->run->exit_reason = KVM_EXIT_EPR;
634 r = 0;
635 }
636
Alexander Graf7c973a22012-08-13 12:50:35 +0200637 return r;
Alexander Graf4ffc6352012-08-08 20:31:13 +0200638}
639
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000640int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
641{
Alexander Graf7ee78852012-08-13 12:44:41 +0200642 int ret, s;
Scott Woodf5f97212013-11-22 15:52:29 -0600643 struct debug_reg debug;
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000644
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200645 if (!vcpu->arch.sane) {
646 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
647 return -EINVAL;
648 }
649
Alexander Graf7ee78852012-08-13 12:44:41 +0200650 s = kvmppc_prepare_to_enter(vcpu);
651 if (s <= 0) {
Alexander Graf7ee78852012-08-13 12:44:41 +0200652 ret = s;
Scott Wood1d1ef222011-11-08 16:11:59 -0600653 goto out;
654 }
Scott Wood6c85f522014-01-09 19:18:40 -0600655 /* interrupts now hard-disabled */
Scott Wood1d1ef222011-11-08 16:11:59 -0600656
Scott Wood8fae8452011-12-20 15:34:45 +0000657#ifdef CONFIG_PPC_FPU
658 /* Save userspace FPU state in stack */
659 enable_kernel_fp();
Scott Wood8fae8452011-12-20 15:34:45 +0000660
661 /*
662 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
663 * as always using the FPU. Kernel usage of FP (via
664 * enable_kernel_fp()) in this thread must not occur while
665 * vcpu->fpu_active is set.
666 */
667 vcpu->fpu_active = 1;
668
669 kvmppc_load_guest_fp(vcpu);
670#endif
671
Bharat Bhushance11e482013-07-04 12:27:47 +0530672 /* Switch to guest debug context */
Scott Woodf5f97212013-11-22 15:52:29 -0600673 debug = vcpu->arch.shadow_dbg_reg;
674 switch_booke_debug_regs(&debug);
675 debug = current->thread.debug;
Bharat Bhushance11e482013-07-04 12:27:47 +0530676 current->thread.debug = vcpu->arch.shadow_dbg_reg;
677
Bharat Bhushan08c9a182013-11-18 11:18:54 +0530678 vcpu->arch.pgdir = current->mm->pgd;
Scott Wood5f1c2482013-07-10 17:47:39 -0500679 kvmppc_fix_ee_before_entry();
Scott Woodf8941fbe2013-06-11 11:38:31 -0500680
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000681 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
Scott Wood8fae8452011-12-20 15:34:45 +0000682
Alexander Graf24afa37b2012-08-12 12:42:30 +0200683 /* No need for kvm_guest_exit. It's done in handle_exit.
684 We also get here with interrupts enabled. */
685
Bharat Bhushance11e482013-07-04 12:27:47 +0530686 /* Switch back to user space debug context */
Scott Woodf5f97212013-11-22 15:52:29 -0600687 switch_booke_debug_regs(&debug);
688 current->thread.debug = debug;
Bharat Bhushance11e482013-07-04 12:27:47 +0530689
Scott Wood8fae8452011-12-20 15:34:45 +0000690#ifdef CONFIG_PPC_FPU
691 kvmppc_save_guest_fp(vcpu);
692
693 vcpu->fpu_active = 0;
Scott Wood8fae8452011-12-20 15:34:45 +0000694#endif
695
Scott Wood1d1ef222011-11-08 16:11:59 -0600696out:
Alexander Grafd69c6432012-08-08 20:44:20 +0200697 vcpu->mode = OUTSIDE_GUEST_MODE;
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000698 return ret;
699}
700
Scott Woodd30f6e42011-12-20 15:34:43 +0000701static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
702{
703 enum emulation_result er;
704
705 er = kvmppc_emulate_instruction(run, vcpu);
706 switch (er) {
707 case EMULATE_DONE:
708 /* don't overwrite subtypes, just account kvm_stats */
709 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
710 /* Future optimization: only reload non-volatiles if
711 * they were actually modified by emulation. */
712 return RESUME_GUEST_NV;
713
714 case EMULATE_DO_DCR:
715 run->exit_reason = KVM_EXIT_DCR;
716 return RESUME_HOST;
717
718 case EMULATE_FAIL:
Scott Woodd30f6e42011-12-20 15:34:43 +0000719 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
720 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
721 /* For debugging, encode the failing instruction and
722 * report it to userspace. */
723 run->hw.hardware_exit_reason = ~0ULL << 32;
724 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
Alexander Grafd1ff5492012-02-16 13:24:03 +0000725 kvmppc_core_queue_program(vcpu, ESR_PIL);
Scott Woodd30f6e42011-12-20 15:34:43 +0000726 return RESUME_HOST;
727
Bharat Bhushan9b4f5302013-04-08 00:32:15 +0000728 case EMULATE_EXIT_USER:
729 return RESUME_HOST;
730
Scott Woodd30f6e42011-12-20 15:34:43 +0000731 default:
732 BUG();
733 }
734}
735
Bharat Bhushance11e482013-07-04 12:27:47 +0530736static int kvmppc_handle_debug(struct kvm_run *run, struct kvm_vcpu *vcpu)
737{
738 struct debug_reg *dbg_reg = &(vcpu->arch.shadow_dbg_reg);
739 u32 dbsr = vcpu->arch.dbsr;
740
741 run->debug.arch.status = 0;
742 run->debug.arch.address = vcpu->arch.pc;
743
744 if (dbsr & (DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4)) {
745 run->debug.arch.status |= KVMPPC_DEBUG_BREAKPOINT;
746 } else {
747 if (dbsr & (DBSR_DAC1W | DBSR_DAC2W))
748 run->debug.arch.status |= KVMPPC_DEBUG_WATCH_WRITE;
749 else if (dbsr & (DBSR_DAC1R | DBSR_DAC2R))
750 run->debug.arch.status |= KVMPPC_DEBUG_WATCH_READ;
751 if (dbsr & (DBSR_DAC1R | DBSR_DAC1W))
752 run->debug.arch.address = dbg_reg->dac1;
753 else if (dbsr & (DBSR_DAC2R | DBSR_DAC2W))
754 run->debug.arch.address = dbg_reg->dac2;
755 }
756
757 return RESUME_HOST;
758}
759
Alexander Graf4e642cc2012-02-20 23:57:26 +0100760static void kvmppc_fill_pt_regs(struct pt_regs *regs)
761{
762 ulong r1, ip, msr, lr;
763
764 asm("mr %0, 1" : "=r"(r1));
765 asm("mflr %0" : "=r"(lr));
766 asm("mfmsr %0" : "=r"(msr));
767 asm("bl 1f; 1: mflr %0" : "=r"(ip));
768
769 memset(regs, 0, sizeof(*regs));
770 regs->gpr[1] = r1;
771 regs->nip = ip;
772 regs->msr = msr;
773 regs->link = lr;
774}
775
Bharat Bhushan6328e592012-06-20 05:56:53 +0000776/*
777 * For interrupts needed to be handled by host interrupt handlers,
778 * corresponding host handler are called from here in similar way
779 * (but not exact) as they are called from low level handler
780 * (such as from arch/powerpc/kernel/head_fsl_booke.S).
781 */
Alexander Graf4e642cc2012-02-20 23:57:26 +0100782static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
783 unsigned int exit_nr)
784{
785 struct pt_regs regs;
786
787 switch (exit_nr) {
788 case BOOKE_INTERRUPT_EXTERNAL:
789 kvmppc_fill_pt_regs(&regs);
790 do_IRQ(&regs);
791 break;
792 case BOOKE_INTERRUPT_DECREMENTER:
793 kvmppc_fill_pt_regs(&regs);
794 timer_interrupt(&regs);
795 break;
Tiejun Chen5f17ce82013-05-13 10:00:45 +0800796#if defined(CONFIG_PPC_DOORBELL)
Alexander Graf4e642cc2012-02-20 23:57:26 +0100797 case BOOKE_INTERRUPT_DOORBELL:
798 kvmppc_fill_pt_regs(&regs);
799 doorbell_exception(&regs);
800 break;
801#endif
802 case BOOKE_INTERRUPT_MACHINE_CHECK:
803 /* FIXME */
804 break;
Alexander Graf7cc1e8e2012-02-22 16:26:34 +0100805 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
806 kvmppc_fill_pt_regs(&regs);
807 performance_monitor_exception(&regs);
808 break;
Bharat Bhushan6328e592012-06-20 05:56:53 +0000809 case BOOKE_INTERRUPT_WATCHDOG:
810 kvmppc_fill_pt_regs(&regs);
811#ifdef CONFIG_BOOKE_WDT
812 WatchdogException(&regs);
813#else
814 unknown_exception(&regs);
815#endif
816 break;
817 case BOOKE_INTERRUPT_CRITICAL:
818 unknown_exception(&regs);
819 break;
Bharat Bhushance11e482013-07-04 12:27:47 +0530820 case BOOKE_INTERRUPT_DEBUG:
821 /* Save DBSR before preemption is enabled */
822 vcpu->arch.dbsr = mfspr(SPRN_DBSR);
823 kvmppc_clear_dbsr();
824 break;
Alexander Graf4e642cc2012-02-20 23:57:26 +0100825 }
826}
827
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500828/**
829 * kvmppc_handle_exit
830 *
831 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
832 */
833int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
834 unsigned int exit_nr)
835{
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500836 int r = RESUME_HOST;
Alexander Graf7ee78852012-08-13 12:44:41 +0200837 int s;
Scott Woodf1e89022013-06-06 19:16:31 -0500838 int idx;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500839
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600840 /* update before a new last_exit_type is rewritten */
841 kvmppc_update_timing_stats(vcpu);
842
Alexander Graf4e642cc2012-02-20 23:57:26 +0100843 /* restart interrupts if they were meant for the host */
844 kvmppc_restart_interrupt(vcpu, exit_nr);
Scott Woodd30f6e42011-12-20 15:34:43 +0000845
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500846 local_irq_enable();
847
Alexander Graf97c95052012-08-02 15:10:00 +0200848 trace_kvm_exit(exit_nr, vcpu);
Alexander Graf706fb732012-08-12 11:29:09 +0200849 kvm_guest_exit();
Alexander Graf97c95052012-08-02 15:10:00 +0200850
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500851 run->exit_reason = KVM_EXIT_UNKNOWN;
852 run->ready_for_interrupt_injection = 1;
853
854 switch (exit_nr) {
855 case BOOKE_INTERRUPT_MACHINE_CHECK:
Alexander Grafc35c9d82012-02-20 12:21:18 +0100856 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
857 kvmppc_dump_vcpu(vcpu);
858 /* For debugging, send invalid exit reason to user space */
859 run->hw.hardware_exit_reason = ~1ULL << 32;
860 run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
861 r = RESUME_HOST;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500862 break;
863
864 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600865 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -0600866 r = RESUME_GUEST;
867 break;
868
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500869 case BOOKE_INTERRUPT_DECREMENTER:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600870 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500871 r = RESUME_GUEST;
872 break;
873
Bharat Bhushan6328e592012-06-20 05:56:53 +0000874 case BOOKE_INTERRUPT_WATCHDOG:
875 r = RESUME_GUEST;
876 break;
877
Scott Woodd30f6e42011-12-20 15:34:43 +0000878 case BOOKE_INTERRUPT_DOORBELL:
879 kvmppc_account_exit(vcpu, DBELL_EXITS);
Scott Woodd30f6e42011-12-20 15:34:43 +0000880 r = RESUME_GUEST;
881 break;
882
883 case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
884 kvmppc_account_exit(vcpu, GDBELL_EXITS);
885
886 /*
887 * We are here because there is a pending guest interrupt
888 * which could not be delivered as MSR_CE or MSR_ME was not
889 * set. Once we break from here we will retry delivery.
890 */
891 r = RESUME_GUEST;
892 break;
893
894 case BOOKE_INTERRUPT_GUEST_DBELL:
895 kvmppc_account_exit(vcpu, GDBELL_EXITS);
896
897 /*
898 * We are here because there is a pending guest interrupt
899 * which could not be delivered as MSR_EE was not set. Once
900 * we break from here we will retry delivery.
901 */
902 r = RESUME_GUEST;
903 break;
904
Alexander Graf95f2e922012-02-20 22:45:12 +0100905 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
906 r = RESUME_GUEST;
907 break;
908
Scott Woodd30f6e42011-12-20 15:34:43 +0000909 case BOOKE_INTERRUPT_HV_PRIV:
910 r = emulation_exit(run, vcpu);
911 break;
912
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500913 case BOOKE_INTERRUPT_PROGRAM:
Scott Woodd30f6e42011-12-20 15:34:43 +0000914 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
Alexander Graf0268597c2012-02-20 12:33:22 +0100915 /*
916 * Program traps generated by user-level software must
917 * be handled by the guest kernel.
918 *
919 * In GS mode, hypervisor privileged instructions trap
920 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
921 * actual program interrupts, handled by the guest.
922 */
Liu Yudaf5e272010-02-02 19:44:35 +0800923 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500924 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600925 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500926 break;
927 }
928
Scott Woodd30f6e42011-12-20 15:34:43 +0000929 r = emulation_exit(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500930 break;
931
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200932 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600933 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600934 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200935 r = RESUME_GUEST;
936 break;
937
Scott Wood4cd35f62011-06-14 18:34:31 -0500938#ifdef CONFIG_SPE
939 case BOOKE_INTERRUPT_SPE_UNAVAIL: {
940 if (vcpu->arch.shared->msr & MSR_SPE)
941 kvmppc_vcpu_enable_spe(vcpu);
942 else
943 kvmppc_booke_queue_irqprio(vcpu,
944 BOOKE_IRQPRIO_SPE_UNAVAIL);
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600945 r = RESUME_GUEST;
946 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500947 }
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600948
949 case BOOKE_INTERRUPT_SPE_FP_DATA:
950 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
951 r = RESUME_GUEST;
952 break;
953
954 case BOOKE_INTERRUPT_SPE_FP_ROUND:
955 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
956 r = RESUME_GUEST;
957 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500958#else
959 case BOOKE_INTERRUPT_SPE_UNAVAIL:
960 /*
961 * Guest wants SPE, but host kernel doesn't support it. Send
962 * an "unimplemented operation" program check to the guest.
963 */
964 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
965 r = RESUME_GUEST;
966 break;
967
968 /*
969 * These really should never happen without CONFIG_SPE,
970 * as we should never enable the real MSR[SPE] in the guest.
971 */
972 case BOOKE_INTERRUPT_SPE_FP_DATA:
973 case BOOKE_INTERRUPT_SPE_FP_ROUND:
974 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
975 __func__, exit_nr, vcpu->arch.pc);
976 run->hw.hardware_exit_reason = exit_nr;
977 r = RESUME_HOST;
978 break;
979#endif
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600980
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500981 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800982 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
983 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600984 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500985 r = RESUME_GUEST;
986 break;
987
988 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800989 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600990 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500991 r = RESUME_GUEST;
992 break;
993
Alexander Graf011da892013-01-31 14:17:38 +0100994 case BOOKE_INTERRUPT_ALIGNMENT:
995 kvmppc_core_queue_alignment(vcpu, vcpu->arch.fault_dear,
996 vcpu->arch.fault_esr);
997 r = RESUME_GUEST;
998 break;
999
Scott Woodd30f6e42011-12-20 15:34:43 +00001000#ifdef CONFIG_KVM_BOOKE_HV
1001 case BOOKE_INTERRUPT_HV_SYSCALL:
1002 if (!(vcpu->arch.shared->msr & MSR_PR)) {
1003 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1004 } else {
1005 /*
1006 * hcall from guest userspace -- send privileged
1007 * instruction program check.
1008 */
1009 kvmppc_core_queue_program(vcpu, ESR_PPR);
1010 }
1011
1012 r = RESUME_GUEST;
1013 break;
1014#else
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001015 case BOOKE_INTERRUPT_SYSCALL:
Alexander Graf2a342ed2010-07-29 14:47:48 +02001016 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1017 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
1018 /* KVM PV hypercalls */
1019 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1020 r = RESUME_GUEST;
1021 } else {
1022 /* Guest syscalls */
1023 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
1024 }
Hollis Blanchard7b701592008-12-02 15:51:58 -06001025 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001026 r = RESUME_GUEST;
1027 break;
Scott Woodd30f6e42011-12-20 15:34:43 +00001028#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001029
1030 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001031 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001032 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001033 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001034 gfn_t gfn;
1035
Alexander Grafbf7ca4b2012-02-15 23:40:00 +00001036#ifdef CONFIG_KVM_E500V2
Scott Wooda4cd8b22011-06-14 18:34:41 -05001037 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1038 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
1039 kvmppc_map_magic(vcpu);
1040 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
1041 r = RESUME_GUEST;
1042
1043 break;
1044 }
1045#endif
1046
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001047 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -06001048 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001049 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001050 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +08001051 kvmppc_core_queue_dtlb_miss(vcpu,
1052 vcpu->arch.fault_dear,
1053 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -06001054 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001055 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001056 r = RESUME_GUEST;
1057 break;
1058 }
1059
Scott Woodf1e89022013-06-06 19:16:31 -05001060 idx = srcu_read_lock(&vcpu->kvm->srcu);
1061
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -06001062 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001063 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001064
1065 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1066 /* The guest TLB had a mapping, but the shadow TLB
1067 * didn't, and it is RAM. This could be because:
1068 * a) the entry is mapping the host kernel, or
1069 * b) the guest used a large mapping which we're faking
1070 * Either way, we need to satisfy the fault without
1071 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -06001072 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001073 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001074 r = RESUME_GUEST;
1075 } else {
1076 /* Guest has mapped and accessed a page which is not
1077 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001078 vcpu->arch.paddr_accessed = gpaddr;
Alexander Graf6020c0f2012-03-12 02:26:30 +01001079 vcpu->arch.vaddr_accessed = eaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001080 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001081 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001082 }
1083
Scott Woodf1e89022013-06-06 19:16:31 -05001084 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001085 break;
1086 }
1087
1088 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001089 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -06001090 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001091 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001092 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001093
1094 r = RESUME_GUEST;
1095
1096 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -06001097 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001098 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001099 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001100 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -06001101 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001102 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001103 break;
1104 }
1105
Hollis Blanchard7b701592008-12-02 15:51:58 -06001106 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001107
Scott Woodf1e89022013-06-06 19:16:31 -05001108 idx = srcu_read_lock(&vcpu->kvm->srcu);
1109
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -06001110 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -06001111 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001112
1113 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1114 /* The guest TLB had a mapping, but the shadow TLB
1115 * didn't. This could be because:
1116 * a) the entry is mapping the host kernel, or
1117 * b) the guest used a large mapping which we're faking
1118 * Either way, we need to satisfy the fault without
1119 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -06001120 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001121 } else {
1122 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001123 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001124 }
1125
Scott Woodf1e89022013-06-06 19:16:31 -05001126 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001127 break;
1128 }
1129
Hollis Blanchard6a0ab732008-07-25 13:54:49 -05001130 case BOOKE_INTERRUPT_DEBUG: {
Bharat Bhushance11e482013-07-04 12:27:47 +05301131 r = kvmppc_handle_debug(run, vcpu);
1132 if (r == RESUME_HOST)
1133 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -06001134 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -05001135 break;
1136 }
1137
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001138 default:
1139 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
1140 BUG();
1141 }
1142
Alexander Grafa8e4ef82012-02-16 14:07:37 +00001143 /*
1144 * To avoid clobbering exit_reason, only check for signals if we
1145 * aren't already exiting to userspace for some other reason.
1146 */
Alexander Graf03660ba2012-02-28 12:00:41 +01001147 if (!(r & RESUME_HOST)) {
Alexander Graf7ee78852012-08-13 12:44:41 +02001148 s = kvmppc_prepare_to_enter(vcpu);
Scott Wood6c85f522014-01-09 19:18:40 -06001149 if (s <= 0)
Alexander Graf7ee78852012-08-13 12:44:41 +02001150 r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
Scott Wood6c85f522014-01-09 19:18:40 -06001151 else {
1152 /* interrupts now hard-disabled */
Scott Wood5f1c2482013-07-10 17:47:39 -05001153 kvmppc_fix_ee_before_entry();
Alexander Graf03660ba2012-02-28 12:00:41 +01001154 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001155 }
1156
1157 return r;
1158}
1159
Bharat Bhushand26f22c2013-02-24 18:57:11 +00001160static void kvmppc_set_tsr(struct kvm_vcpu *vcpu, u32 new_tsr)
1161{
1162 u32 old_tsr = vcpu->arch.tsr;
1163
1164 vcpu->arch.tsr = new_tsr;
1165
1166 if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS))
1167 arm_next_watchdog(vcpu);
1168
1169 update_timer_ints(vcpu);
1170}
1171
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001172/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
1173int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1174{
Hollis Blanchard082decf2010-08-07 10:33:56 -07001175 int i;
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001176 int r;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001177
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001178 vcpu->arch.pc = 0;
Scott Woodb5904972011-11-08 18:23:30 -06001179 vcpu->arch.shared->pir = vcpu->vcpu_id;
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001180 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Scott Woodd30f6e42011-12-20 15:34:43 +00001181 kvmppc_set_msr(vcpu, 0);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001182
Scott Woodd30f6e42011-12-20 15:34:43 +00001183#ifndef CONFIG_KVM_BOOKE_HV
Bharat Bhushance11e482013-07-04 12:27:47 +05301184 vcpu->arch.shadow_msr = MSR_USER | MSR_IS | MSR_DS;
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001185 vcpu->arch.shadow_pid = 1;
Scott Woodd30f6e42011-12-20 15:34:43 +00001186 vcpu->arch.shared->msr = 0;
1187#endif
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001188
Hollis Blanchard082decf2010-08-07 10:33:56 -07001189 /* Eye-catching numbers so we know if the guest takes an interrupt
1190 * before it's programmed its own IVPR/IVORs. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001191 vcpu->arch.ivpr = 0x55550000;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001192 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
1193 vcpu->arch.ivor[i] = 0x7700 | i * 4;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001194
Hollis Blanchard73e75b42008-12-02 15:51:57 -06001195 kvmppc_init_timing_stats(vcpu);
1196
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001197 r = kvmppc_core_vcpu_setup(vcpu);
1198 kvmppc_sanity_check(vcpu);
1199 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001200}
1201
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001202int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
1203{
1204 /* setup watchdog timer once */
1205 spin_lock_init(&vcpu->arch.wdt_lock);
1206 setup_timer(&vcpu->arch.wdt_timer, kvmppc_watchdog_func,
1207 (unsigned long)vcpu);
1208
1209 return 0;
1210}
1211
1212void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
1213{
1214 del_timer_sync(&vcpu->arch.wdt_timer);
1215}
1216
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001217int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1218{
1219 int i;
1220
1221 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001222 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001223 regs->ctr = vcpu->arch.ctr;
1224 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001225 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001226 regs->msr = vcpu->arch.shared->msr;
Bharat Bhushan31579ee2014-07-17 17:01:36 +05301227 regs->srr0 = kvmppc_get_srr0(vcpu);
1228 regs->srr1 = kvmppc_get_srr1(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001229 regs->pid = vcpu->arch.pid;
Bharat Bhushanc1b8a012014-07-17 17:01:39 +05301230 regs->sprg0 = kvmppc_get_sprg0(vcpu);
1231 regs->sprg1 = kvmppc_get_sprg1(vcpu);
1232 regs->sprg2 = kvmppc_get_sprg2(vcpu);
1233 regs->sprg3 = kvmppc_get_sprg3(vcpu);
1234 regs->sprg4 = kvmppc_get_sprg4(vcpu);
1235 regs->sprg5 = kvmppc_get_sprg5(vcpu);
1236 regs->sprg6 = kvmppc_get_sprg6(vcpu);
1237 regs->sprg7 = kvmppc_get_sprg7(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001238
1239 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001240 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001241
1242 return 0;
1243}
1244
1245int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1246{
1247 int i;
1248
1249 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001250 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001251 vcpu->arch.ctr = regs->ctr;
1252 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001253 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -06001254 kvmppc_set_msr(vcpu, regs->msr);
Bharat Bhushan31579ee2014-07-17 17:01:36 +05301255 kvmppc_set_srr0(vcpu, regs->srr0);
1256 kvmppc_set_srr1(vcpu, regs->srr1);
Scott Wood5ce941e2011-04-27 17:24:21 -05001257 kvmppc_set_pid(vcpu, regs->pid);
Bharat Bhushanc1b8a012014-07-17 17:01:39 +05301258 kvmppc_set_sprg0(vcpu, regs->sprg0);
1259 kvmppc_set_sprg1(vcpu, regs->sprg1);
1260 kvmppc_set_sprg2(vcpu, regs->sprg2);
1261 kvmppc_set_sprg3(vcpu, regs->sprg3);
1262 kvmppc_set_sprg4(vcpu, regs->sprg4);
1263 kvmppc_set_sprg5(vcpu, regs->sprg5);
1264 kvmppc_set_sprg6(vcpu, regs->sprg6);
1265 kvmppc_set_sprg7(vcpu, regs->sprg7);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001266
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001267 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1268 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001269
1270 return 0;
1271}
1272
Scott Wood5ce941e2011-04-27 17:24:21 -05001273static void get_sregs_base(struct kvm_vcpu *vcpu,
1274 struct kvm_sregs *sregs)
1275{
1276 u64 tb = get_tb();
1277
1278 sregs->u.e.features |= KVM_SREGS_E_BASE;
1279
1280 sregs->u.e.csrr0 = vcpu->arch.csrr0;
1281 sregs->u.e.csrr1 = vcpu->arch.csrr1;
1282 sregs->u.e.mcsr = vcpu->arch.mcsr;
Bharat Bhushandc168542014-07-17 17:01:38 +05301283 sregs->u.e.esr = kvmppc_get_esr(vcpu);
Bharat Bhushana5414d42014-07-17 17:01:37 +05301284 sregs->u.e.dear = kvmppc_get_dar(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001285 sregs->u.e.tsr = vcpu->arch.tsr;
1286 sregs->u.e.tcr = vcpu->arch.tcr;
1287 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
1288 sregs->u.e.tb = tb;
1289 sregs->u.e.vrsave = vcpu->arch.vrsave;
1290}
1291
1292static int set_sregs_base(struct kvm_vcpu *vcpu,
1293 struct kvm_sregs *sregs)
1294{
1295 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
1296 return 0;
1297
1298 vcpu->arch.csrr0 = sregs->u.e.csrr0;
1299 vcpu->arch.csrr1 = sregs->u.e.csrr1;
1300 vcpu->arch.mcsr = sregs->u.e.mcsr;
Bharat Bhushandc168542014-07-17 17:01:38 +05301301 kvmppc_set_esr(vcpu, sregs->u.e.esr);
Bharat Bhushana5414d42014-07-17 17:01:37 +05301302 kvmppc_set_dar(vcpu, sregs->u.e.dear);
Scott Wood5ce941e2011-04-27 17:24:21 -05001303 vcpu->arch.vrsave = sregs->u.e.vrsave;
Scott Wooddfd4d472011-11-17 12:39:59 +00001304 kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001305
Scott Wooddfd4d472011-11-17 12:39:59 +00001306 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
Scott Wood5ce941e2011-04-27 17:24:21 -05001307 vcpu->arch.dec = sregs->u.e.dec;
Scott Wooddfd4d472011-11-17 12:39:59 +00001308 kvmppc_emulate_dec(vcpu);
1309 }
Scott Wood5ce941e2011-04-27 17:24:21 -05001310
Bharat Bhushand26f22c2013-02-24 18:57:11 +00001311 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR)
1312 kvmppc_set_tsr(vcpu, sregs->u.e.tsr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001313
1314 return 0;
1315}
1316
1317static void get_sregs_arch206(struct kvm_vcpu *vcpu,
1318 struct kvm_sregs *sregs)
1319{
1320 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
1321
Scott Wood841741f2011-09-02 17:39:37 -05001322 sregs->u.e.pir = vcpu->vcpu_id;
Scott Wood5ce941e2011-04-27 17:24:21 -05001323 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
1324 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
1325 sregs->u.e.decar = vcpu->arch.decar;
1326 sregs->u.e.ivpr = vcpu->arch.ivpr;
1327}
1328
1329static int set_sregs_arch206(struct kvm_vcpu *vcpu,
1330 struct kvm_sregs *sregs)
1331{
1332 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
1333 return 0;
1334
Scott Wood841741f2011-09-02 17:39:37 -05001335 if (sregs->u.e.pir != vcpu->vcpu_id)
Scott Wood5ce941e2011-04-27 17:24:21 -05001336 return -EINVAL;
1337
1338 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
1339 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
1340 vcpu->arch.decar = sregs->u.e.decar;
1341 vcpu->arch.ivpr = sregs->u.e.ivpr;
1342
1343 return 0;
1344}
1345
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301346int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Scott Wood5ce941e2011-04-27 17:24:21 -05001347{
1348 sregs->u.e.features |= KVM_SREGS_E_IVOR;
1349
1350 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
1351 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
1352 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
1353 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
1354 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
1355 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
1356 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
1357 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
1358 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
1359 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
1360 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
1361 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
1362 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
1363 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
1364 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
1365 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301366 return 0;
Scott Wood5ce941e2011-04-27 17:24:21 -05001367}
1368
1369int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1370{
1371 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
1372 return 0;
1373
1374 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
1375 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
1376 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
1377 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
1378 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
1379 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
1380 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
1381 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
1382 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
1383 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
1384 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
1385 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
1386 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
1387 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
1388 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
1389 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
1390
1391 return 0;
1392}
1393
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001394int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1395 struct kvm_sregs *sregs)
1396{
Scott Wood5ce941e2011-04-27 17:24:21 -05001397 sregs->pvr = vcpu->arch.pvr;
1398
1399 get_sregs_base(vcpu, sregs);
1400 get_sregs_arch206(vcpu, sregs);
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301401 return vcpu->kvm->arch.kvm_ops->get_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001402}
1403
1404int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1405 struct kvm_sregs *sregs)
1406{
Scott Wood5ce941e2011-04-27 17:24:21 -05001407 int ret;
1408
1409 if (vcpu->arch.pvr != sregs->pvr)
1410 return -EINVAL;
1411
1412 ret = set_sregs_base(vcpu, sregs);
1413 if (ret < 0)
1414 return ret;
1415
1416 ret = set_sregs_arch206(vcpu, sregs);
1417 if (ret < 0)
1418 return ret;
1419
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301420 return vcpu->kvm->arch.kvm_ops->set_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001421}
1422
Paul Mackerras31f34382011-12-12 12:26:50 +00001423int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1424{
Mihai Caraman35b299e2013-04-11 00:03:07 +00001425 int r = 0;
1426 union kvmppc_one_reg val;
1427 int size;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001428
1429 size = one_reg_size(reg->id);
1430 if (size > sizeof(val))
1431 return -EINVAL;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001432
1433 switch (reg->id) {
1434 case KVM_REG_PPC_IAC1:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301435 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac1);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001436 break;
Bharat Bhushan547465e2013-07-04 12:27:46 +05301437 case KVM_REG_PPC_IAC2:
1438 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac2);
1439 break;
1440#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1441 case KVM_REG_PPC_IAC3:
1442 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac3);
1443 break;
1444 case KVM_REG_PPC_IAC4:
1445 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac4);
1446 break;
1447#endif
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001448 case KVM_REG_PPC_DAC1:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301449 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.dac1);
1450 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001451 case KVM_REG_PPC_DAC2:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301452 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.dac2);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001453 break;
Alexander Graf324b3e62013-01-04 18:28:51 +01001454 case KVM_REG_PPC_EPR: {
1455 u32 epr = get_guest_epr(vcpu);
Mihai Caraman35b299e2013-04-11 00:03:07 +00001456 val = get_reg_val(reg->id, epr);
Alexander Graf324b3e62013-01-04 18:28:51 +01001457 break;
1458 }
Mihai Caraman352df1d2012-10-11 06:13:29 +00001459#if defined(CONFIG_64BIT)
1460 case KVM_REG_PPC_EPCR:
Mihai Caraman35b299e2013-04-11 00:03:07 +00001461 val = get_reg_val(reg->id, vcpu->arch.epcr);
Mihai Caraman352df1d2012-10-11 06:13:29 +00001462 break;
1463#endif
Bharat Bhushan78accda2013-02-24 18:57:12 +00001464 case KVM_REG_PPC_TCR:
Mihai Caraman35b299e2013-04-11 00:03:07 +00001465 val = get_reg_val(reg->id, vcpu->arch.tcr);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001466 break;
1467 case KVM_REG_PPC_TSR:
Mihai Caraman35b299e2013-04-11 00:03:07 +00001468 val = get_reg_val(reg->id, vcpu->arch.tsr);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001469 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001470 case KVM_REG_PPC_DEBUG_INST:
Bharat Bhushanb12c7842013-07-04 12:27:45 +05301471 val = get_reg_val(reg->id, KVMPPC_INST_EHPRIV_DEBUG);
Bharat Bhushan8c32a2e2013-03-20 20:24:58 +00001472 break;
Paul Mackerras8b75cbb2013-09-20 14:52:37 +10001473 case KVM_REG_PPC_VRSAVE:
1474 val = get_reg_val(reg->id, vcpu->arch.vrsave);
Bharat Bhushan8c32a2e2013-03-20 20:24:58 +00001475 break;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001476 default:
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301477 r = vcpu->kvm->arch.kvm_ops->get_one_reg(vcpu, reg->id, &val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001478 break;
1479 }
Mihai Caraman35b299e2013-04-11 00:03:07 +00001480
1481 if (r)
1482 return r;
1483
1484 if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
1485 r = -EFAULT;
1486
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001487 return r;
Paul Mackerras31f34382011-12-12 12:26:50 +00001488}
1489
1490int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1491{
Mihai Caraman35b299e2013-04-11 00:03:07 +00001492 int r = 0;
1493 union kvmppc_one_reg val;
1494 int size;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001495
1496 size = one_reg_size(reg->id);
1497 if (size > sizeof(val))
1498 return -EINVAL;
1499
1500 if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
1501 return -EFAULT;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001502
1503 switch (reg->id) {
1504 case KVM_REG_PPC_IAC1:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301505 vcpu->arch.dbg_reg.iac1 = set_reg_val(reg->id, val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001506 break;
Bharat Bhushan547465e2013-07-04 12:27:46 +05301507 case KVM_REG_PPC_IAC2:
1508 vcpu->arch.dbg_reg.iac2 = set_reg_val(reg->id, val);
1509 break;
1510#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1511 case KVM_REG_PPC_IAC3:
1512 vcpu->arch.dbg_reg.iac3 = set_reg_val(reg->id, val);
1513 break;
1514 case KVM_REG_PPC_IAC4:
1515 vcpu->arch.dbg_reg.iac4 = set_reg_val(reg->id, val);
1516 break;
1517#endif
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001518 case KVM_REG_PPC_DAC1:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301519 vcpu->arch.dbg_reg.dac1 = set_reg_val(reg->id, val);
1520 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001521 case KVM_REG_PPC_DAC2:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301522 vcpu->arch.dbg_reg.dac2 = set_reg_val(reg->id, val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001523 break;
Alexander Graf324b3e62013-01-04 18:28:51 +01001524 case KVM_REG_PPC_EPR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001525 u32 new_epr = set_reg_val(reg->id, val);
1526 kvmppc_set_epr(vcpu, new_epr);
Alexander Graf324b3e62013-01-04 18:28:51 +01001527 break;
1528 }
Mihai Caraman352df1d2012-10-11 06:13:29 +00001529#if defined(CONFIG_64BIT)
1530 case KVM_REG_PPC_EPCR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001531 u32 new_epcr = set_reg_val(reg->id, val);
1532 kvmppc_set_epcr(vcpu, new_epcr);
Mihai Caraman352df1d2012-10-11 06:13:29 +00001533 break;
1534 }
1535#endif
Bharat Bhushan78accda2013-02-24 18:57:12 +00001536 case KVM_REG_PPC_OR_TSR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001537 u32 tsr_bits = set_reg_val(reg->id, val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001538 kvmppc_set_tsr_bits(vcpu, tsr_bits);
1539 break;
1540 }
1541 case KVM_REG_PPC_CLEAR_TSR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001542 u32 tsr_bits = set_reg_val(reg->id, val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001543 kvmppc_clr_tsr_bits(vcpu, tsr_bits);
1544 break;
1545 }
1546 case KVM_REG_PPC_TSR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001547 u32 tsr = set_reg_val(reg->id, val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001548 kvmppc_set_tsr(vcpu, tsr);
1549 break;
1550 }
1551 case KVM_REG_PPC_TCR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001552 u32 tcr = set_reg_val(reg->id, val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001553 kvmppc_set_tcr(vcpu, tcr);
1554 break;
1555 }
Paul Mackerras8b75cbb2013-09-20 14:52:37 +10001556 case KVM_REG_PPC_VRSAVE:
1557 vcpu->arch.vrsave = set_reg_val(reg->id, val);
1558 break;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001559 default:
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301560 r = vcpu->kvm->arch.kvm_ops->set_one_reg(vcpu, reg->id, &val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001561 break;
1562 }
Mihai Caraman35b299e2013-04-11 00:03:07 +00001563
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001564 return r;
Paul Mackerras31f34382011-12-12 12:26:50 +00001565}
1566
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001567int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1568{
1569 return -ENOTSUPP;
1570}
1571
1572int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1573{
1574 return -ENOTSUPP;
1575}
1576
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001577int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1578 struct kvm_translation *tr)
1579{
Avi Kivity98001d82010-05-13 11:05:49 +03001580 int r;
1581
Avi Kivity98001d82010-05-13 11:05:49 +03001582 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +03001583 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001584}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001585
Alexander Graf4e755752009-10-30 05:47:01 +00001586int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1587{
1588 return -ENOTSUPP;
1589}
1590
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05301591void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001592 struct kvm_memory_slot *dont)
1593{
1594}
1595
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05301596int kvmppc_core_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001597 unsigned long npages)
1598{
1599 return 0;
1600}
1601
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001602int kvmppc_core_prepare_memory_region(struct kvm *kvm,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001603 struct kvm_memory_slot *memslot,
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001604 struct kvm_userspace_memory_region *mem)
1605{
1606 return 0;
1607}
1608
1609void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001610 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001611 const struct kvm_memory_slot *old)
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001612{
1613}
1614
1615void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001616{
1617}
1618
Mihai Caraman38f98822012-10-11 06:13:27 +00001619void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr)
1620{
1621#if defined(CONFIG_64BIT)
1622 vcpu->arch.epcr = new_epcr;
1623#ifdef CONFIG_KVM_BOOKE_HV
1624 vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM;
1625 if (vcpu->arch.epcr & SPRN_EPCR_ICM)
1626 vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM;
1627#endif
1628#endif
1629}
1630
Scott Wooddfd4d472011-11-17 12:39:59 +00001631void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1632{
1633 vcpu->arch.tcr = new_tcr;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001634 arm_next_watchdog(vcpu);
Scott Wooddfd4d472011-11-17 12:39:59 +00001635 update_timer_ints(vcpu);
1636}
1637
1638void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1639{
1640 set_bits(tsr_bits, &vcpu->arch.tsr);
1641 smp_wmb();
1642 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1643 kvm_vcpu_kick(vcpu);
1644}
1645
1646void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1647{
1648 clear_bits(tsr_bits, &vcpu->arch.tsr);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001649
1650 /*
1651 * We may have stopped the watchdog due to
1652 * being stuck on final expiration.
1653 */
1654 if (tsr_bits & (TSR_ENW | TSR_WIS))
1655 arm_next_watchdog(vcpu);
1656
Scott Wooddfd4d472011-11-17 12:39:59 +00001657 update_timer_ints(vcpu);
1658}
1659
1660void kvmppc_decrementer_func(unsigned long data)
1661{
1662 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1663
Bharat Bhushan21bd0002012-05-20 23:21:23 +00001664 if (vcpu->arch.tcr & TCR_ARE) {
1665 vcpu->arch.dec = vcpu->arch.decar;
1666 kvmppc_emulate_dec(vcpu);
1667 }
1668
Scott Wooddfd4d472011-11-17 12:39:59 +00001669 kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1670}
1671
Bharat Bhushance11e482013-07-04 12:27:47 +05301672static int kvmppc_booke_add_breakpoint(struct debug_reg *dbg_reg,
1673 uint64_t addr, int index)
1674{
1675 switch (index) {
1676 case 0:
1677 dbg_reg->dbcr0 |= DBCR0_IAC1;
1678 dbg_reg->iac1 = addr;
1679 break;
1680 case 1:
1681 dbg_reg->dbcr0 |= DBCR0_IAC2;
1682 dbg_reg->iac2 = addr;
1683 break;
1684#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1685 case 2:
1686 dbg_reg->dbcr0 |= DBCR0_IAC3;
1687 dbg_reg->iac3 = addr;
1688 break;
1689 case 3:
1690 dbg_reg->dbcr0 |= DBCR0_IAC4;
1691 dbg_reg->iac4 = addr;
1692 break;
1693#endif
1694 default:
1695 return -EINVAL;
1696 }
1697
1698 dbg_reg->dbcr0 |= DBCR0_IDM;
1699 return 0;
1700}
1701
1702static int kvmppc_booke_add_watchpoint(struct debug_reg *dbg_reg, uint64_t addr,
1703 int type, int index)
1704{
1705 switch (index) {
1706 case 0:
1707 if (type & KVMPPC_DEBUG_WATCH_READ)
1708 dbg_reg->dbcr0 |= DBCR0_DAC1R;
1709 if (type & KVMPPC_DEBUG_WATCH_WRITE)
1710 dbg_reg->dbcr0 |= DBCR0_DAC1W;
1711 dbg_reg->dac1 = addr;
1712 break;
1713 case 1:
1714 if (type & KVMPPC_DEBUG_WATCH_READ)
1715 dbg_reg->dbcr0 |= DBCR0_DAC2R;
1716 if (type & KVMPPC_DEBUG_WATCH_WRITE)
1717 dbg_reg->dbcr0 |= DBCR0_DAC2W;
1718 dbg_reg->dac2 = addr;
1719 break;
1720 default:
1721 return -EINVAL;
1722 }
1723
1724 dbg_reg->dbcr0 |= DBCR0_IDM;
1725 return 0;
1726}
1727void kvm_guest_protect_msr(struct kvm_vcpu *vcpu, ulong prot_bitmap, bool set)
1728{
1729 /* XXX: Add similar MSR protection for BookE-PR */
1730#ifdef CONFIG_KVM_BOOKE_HV
1731 BUG_ON(prot_bitmap & ~(MSRP_UCLEP | MSRP_DEP | MSRP_PMMP));
1732 if (set) {
1733 if (prot_bitmap & MSR_UCLE)
1734 vcpu->arch.shadow_msrp |= MSRP_UCLEP;
1735 if (prot_bitmap & MSR_DE)
1736 vcpu->arch.shadow_msrp |= MSRP_DEP;
1737 if (prot_bitmap & MSR_PMM)
1738 vcpu->arch.shadow_msrp |= MSRP_PMMP;
1739 } else {
1740 if (prot_bitmap & MSR_UCLE)
1741 vcpu->arch.shadow_msrp &= ~MSRP_UCLEP;
1742 if (prot_bitmap & MSR_DE)
1743 vcpu->arch.shadow_msrp &= ~MSRP_DEP;
1744 if (prot_bitmap & MSR_PMM)
1745 vcpu->arch.shadow_msrp &= ~MSRP_PMMP;
1746 }
1747#endif
1748}
1749
1750int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1751 struct kvm_guest_debug *dbg)
1752{
1753 struct debug_reg *dbg_reg;
1754 int n, b = 0, w = 0;
1755
1756 if (!(dbg->control & KVM_GUESTDBG_ENABLE)) {
1757 vcpu->arch.shadow_dbg_reg.dbcr0 = 0;
1758 vcpu->guest_debug = 0;
1759 kvm_guest_protect_msr(vcpu, MSR_DE, false);
1760 return 0;
1761 }
1762
1763 kvm_guest_protect_msr(vcpu, MSR_DE, true);
1764 vcpu->guest_debug = dbg->control;
1765 vcpu->arch.shadow_dbg_reg.dbcr0 = 0;
1766 /* Set DBCR0_EDM in guest visible DBCR0 register. */
1767 vcpu->arch.dbg_reg.dbcr0 = DBCR0_EDM;
1768
1769 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1770 vcpu->arch.shadow_dbg_reg.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1771
1772 /* Code below handles only HW breakpoints */
1773 dbg_reg = &(vcpu->arch.shadow_dbg_reg);
1774
1775#ifdef CONFIG_KVM_BOOKE_HV
1776 /*
1777 * On BookE-HV (e500mc) the guest is always executed with MSR.GS=1
1778 * DBCR1 and DBCR2 are set to trigger debug events when MSR.PR is 0
1779 */
1780 dbg_reg->dbcr1 = 0;
1781 dbg_reg->dbcr2 = 0;
1782#else
1783 /*
1784 * On BookE-PR (e500v2) the guest is always executed with MSR.PR=1
1785 * We set DBCR1 and DBCR2 to only trigger debug events when MSR.PR
1786 * is set.
1787 */
1788 dbg_reg->dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US | DBCR1_IAC3US |
1789 DBCR1_IAC4US;
1790 dbg_reg->dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
1791#endif
1792
1793 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1794 return 0;
1795
1796 for (n = 0; n < (KVMPPC_BOOKE_IAC_NUM + KVMPPC_BOOKE_DAC_NUM); n++) {
1797 uint64_t addr = dbg->arch.bp[n].addr;
1798 uint32_t type = dbg->arch.bp[n].type;
1799
1800 if (type == KVMPPC_DEBUG_NONE)
1801 continue;
1802
1803 if (type & !(KVMPPC_DEBUG_WATCH_READ |
1804 KVMPPC_DEBUG_WATCH_WRITE |
1805 KVMPPC_DEBUG_BREAKPOINT))
1806 return -EINVAL;
1807
1808 if (type & KVMPPC_DEBUG_BREAKPOINT) {
1809 /* Setting H/W breakpoint */
1810 if (kvmppc_booke_add_breakpoint(dbg_reg, addr, b++))
1811 return -EINVAL;
1812 } else {
1813 /* Setting H/W watchpoint */
1814 if (kvmppc_booke_add_watchpoint(dbg_reg, addr,
1815 type, w++))
1816 return -EINVAL;
1817 }
1818 }
1819
1820 return 0;
1821}
1822
Scott Wood94fa9d92011-12-20 15:34:22 +00001823void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1824{
Paul Mackerrasa47d72f2012-09-20 19:35:51 +00001825 vcpu->cpu = smp_processor_id();
Scott Woodd30f6e42011-12-20 15:34:43 +00001826 current->thread.kvm_vcpu = vcpu;
Scott Wood94fa9d92011-12-20 15:34:22 +00001827}
1828
1829void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
1830{
Scott Woodd30f6e42011-12-20 15:34:43 +00001831 current->thread.kvm_vcpu = NULL;
Paul Mackerrasa47d72f2012-09-20 19:35:51 +00001832 vcpu->cpu = -1;
Bharat Bhushance11e482013-07-04 12:27:47 +05301833
1834 /* Clear pending debug event in DBSR */
1835 kvmppc_clear_dbsr();
Scott Wood94fa9d92011-12-20 15:34:22 +00001836}
1837
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301838void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
1839{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301840 vcpu->kvm->arch.kvm_ops->mmu_destroy(vcpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301841}
1842
1843int kvmppc_core_init_vm(struct kvm *kvm)
1844{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301845 return kvm->arch.kvm_ops->init_vm(kvm);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301846}
1847
1848struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1849{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301850 return kvm->arch.kvm_ops->vcpu_create(kvm, id);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301851}
1852
1853void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1854{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301855 vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301856}
1857
1858void kvmppc_core_destroy_vm(struct kvm *kvm)
1859{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301860 kvm->arch.kvm_ops->destroy_vm(kvm);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301861}
1862
1863void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1864{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301865 vcpu->kvm->arch.kvm_ops->vcpu_load(vcpu, cpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301866}
1867
1868void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
1869{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301870 vcpu->kvm->arch.kvm_ops->vcpu_put(vcpu);
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001871}
1872
1873int __init kvmppc_booke_init(void)
1874{
Scott Woodd30f6e42011-12-20 15:34:43 +00001875#ifndef CONFIG_KVM_BOOKE_HV
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001876 unsigned long ivor[16];
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001877 unsigned long *handler = kvmppc_booke_handler_addr;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001878 unsigned long max_ivor = 0;
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001879 unsigned long handler_len;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001880 int i;
1881
1882 /* We install our own exception handlers by hijacking IVPR. IVPR must
1883 * be 16-bit aligned, so we need a 64KB allocation. */
1884 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1885 VCPU_SIZE_ORDER);
1886 if (!kvmppc_booke_handlers)
1887 return -ENOMEM;
1888
1889 /* XXX make sure our handlers are smaller than Linux's */
1890
1891 /* Copy our interrupt handlers to match host IVORs. That way we don't
1892 * have to swap the IVORs on every guest/host transition. */
1893 ivor[0] = mfspr(SPRN_IVOR0);
1894 ivor[1] = mfspr(SPRN_IVOR1);
1895 ivor[2] = mfspr(SPRN_IVOR2);
1896 ivor[3] = mfspr(SPRN_IVOR3);
1897 ivor[4] = mfspr(SPRN_IVOR4);
1898 ivor[5] = mfspr(SPRN_IVOR5);
1899 ivor[6] = mfspr(SPRN_IVOR6);
1900 ivor[7] = mfspr(SPRN_IVOR7);
1901 ivor[8] = mfspr(SPRN_IVOR8);
1902 ivor[9] = mfspr(SPRN_IVOR9);
1903 ivor[10] = mfspr(SPRN_IVOR10);
1904 ivor[11] = mfspr(SPRN_IVOR11);
1905 ivor[12] = mfspr(SPRN_IVOR12);
1906 ivor[13] = mfspr(SPRN_IVOR13);
1907 ivor[14] = mfspr(SPRN_IVOR14);
1908 ivor[15] = mfspr(SPRN_IVOR15);
1909
1910 for (i = 0; i < 16; i++) {
1911 if (ivor[i] > max_ivor)
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001912 max_ivor = i;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001913
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001914 handler_len = handler[i + 1] - handler[i];
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001915 memcpy((void *)kvmppc_booke_handlers + ivor[i],
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001916 (void *)handler[i], handler_len);
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001917 }
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001918
1919 handler_len = handler[max_ivor + 1] - handler[max_ivor];
1920 flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers +
1921 ivor[max_ivor] + handler_len);
Scott Woodd30f6e42011-12-20 15:34:43 +00001922#endif /* !BOOKE_HV */
Hollis Blancharddb93f572008-11-05 09:36:18 -06001923 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001924}
1925
Hollis Blancharddb93f572008-11-05 09:36:18 -06001926void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001927{
1928 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
1929 kvm_exit();
1930}