blob: 322da7d2334ad2a246e7ed3017e24c2cc3c0c622 [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) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050054 { "sig", VCPU_STAT(signal_exits) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050055 { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
56 { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
57 { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
58 { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
59 { "sysc", VCPU_STAT(syscall_exits) },
60 { "isi", VCPU_STAT(isi_exits) },
61 { "dsi", VCPU_STAT(dsi_exits) },
62 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
63 { "dec", VCPU_STAT(dec_exits) },
64 { "ext_intr", VCPU_STAT(ext_intr_exits) },
Hollis Blanchard45c5eb62008-04-25 17:55:49 -050065 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Scott Woodd30f6e42011-12-20 15:34:43 +000066 { "doorbell", VCPU_STAT(dbell_exits) },
67 { "guest doorbell", VCPU_STAT(gdbell_exits) },
Alexander Grafcf1c5ca2012-08-01 12:56:51 +020068 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050069 { NULL }
70};
71
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050072/* TODO: use vcpu_printf() */
73void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
74{
75 int i;
76
Alexander Graf666e7252010-07-29 14:47:43 +020077 printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060078 printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
Alexander Grafde7906c2010-07-29 14:47:46 +020079 printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
80 vcpu->arch.shared->srr1);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050081
82 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
83
84 for (i = 0; i < 32; i += 4) {
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060085 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
Alexander Graf8e5b26b2010-01-08 02:58:01 +010086 kvmppc_get_gpr(vcpu, i),
87 kvmppc_get_gpr(vcpu, i+1),
88 kvmppc_get_gpr(vcpu, i+2),
89 kvmppc_get_gpr(vcpu, i+3));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050090 }
91}
92
Scott Wood4cd35f62011-06-14 18:34:31 -050093#ifdef CONFIG_SPE
94void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
95{
96 preempt_disable();
97 enable_kernel_spe();
98 kvmppc_save_guest_spe(vcpu);
99 vcpu->arch.shadow_msr &= ~MSR_SPE;
100 preempt_enable();
101}
102
103static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
104{
105 preempt_disable();
106 enable_kernel_spe();
107 kvmppc_load_guest_spe(vcpu);
108 vcpu->arch.shadow_msr |= MSR_SPE;
109 preempt_enable();
110}
111
112static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
113{
114 if (vcpu->arch.shared->msr & MSR_SPE) {
115 if (!(vcpu->arch.shadow_msr & MSR_SPE))
116 kvmppc_vcpu_enable_spe(vcpu);
117 } else if (vcpu->arch.shadow_msr & MSR_SPE) {
118 kvmppc_vcpu_disable_spe(vcpu);
119 }
120}
121#else
122static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
123{
124}
125#endif
126
Alexander Graf7a08c272012-08-16 13:10:16 +0200127static void kvmppc_vcpu_sync_fpu(struct kvm_vcpu *vcpu)
128{
129#if defined(CONFIG_PPC_FPU) && !defined(CONFIG_KVM_BOOKE_HV)
130 /* We always treat the FP bit as enabled from the host
131 perspective, so only need to adjust the shadow MSR */
132 vcpu->arch.shadow_msr &= ~MSR_FP;
133 vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_FP;
134#endif
135}
136
Bharat Bhushance11e482013-07-04 12:27:47 +0530137static void kvmppc_vcpu_sync_debug(struct kvm_vcpu *vcpu)
138{
139 /* Synchronize guest's desire to get debug interrupts into shadow MSR */
140#ifndef CONFIG_KVM_BOOKE_HV
141 vcpu->arch.shadow_msr &= ~MSR_DE;
142 vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_DE;
143#endif
144
145 /* Force enable debug interrupts when user space wants to debug */
146 if (vcpu->guest_debug) {
147#ifdef CONFIG_KVM_BOOKE_HV
148 /*
149 * Since there is no shadow MSR, sync MSR_DE into the guest
150 * visible MSR.
151 */
152 vcpu->arch.shared->msr |= MSR_DE;
153#else
154 vcpu->arch.shadow_msr |= MSR_DE;
155 vcpu->arch.shared->msr &= ~MSR_DE;
156#endif
157 }
158}
159
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500160/*
161 * Helper function for "full" MSR writes. No need to call this if only
162 * EE/CE/ME/DE/RI are changing.
163 */
Scott Wood4cd35f62011-06-14 18:34:31 -0500164void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
165{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500166 u32 old_msr = vcpu->arch.shared->msr;
Scott Wood4cd35f62011-06-14 18:34:31 -0500167
Scott Woodd30f6e42011-12-20 15:34:43 +0000168#ifdef CONFIG_KVM_BOOKE_HV
169 new_msr |= MSR_GS;
170#endif
171
Scott Wood4cd35f62011-06-14 18:34:31 -0500172 vcpu->arch.shared->msr = new_msr;
173
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500174 kvmppc_mmu_msr_notify(vcpu, old_msr);
Scott Wood4cd35f62011-06-14 18:34:31 -0500175 kvmppc_vcpu_sync_spe(vcpu);
Alexander Graf7a08c272012-08-16 13:10:16 +0200176 kvmppc_vcpu_sync_fpu(vcpu);
Bharat Bhushance11e482013-07-04 12:27:47 +0530177 kvmppc_vcpu_sync_debug(vcpu);
Scott Wood4cd35f62011-06-14 18:34:31 -0500178}
179
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600180static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
181 unsigned int priority)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600182{
Alexander Graf63460462012-08-08 00:44:52 +0200183 trace_kvm_booke_queue_irqprio(vcpu, priority);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600184 set_bit(priority, &vcpu->arch.pending_exceptions);
185}
186
Alexander Graf8de12012014-06-18 21:56:55 +0200187void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
188 ulong dear_flags, ulong esr_flags)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600189{
Liu Yudaf5e272010-02-02 19:44:35 +0800190 vcpu->arch.queued_dear = dear_flags;
191 vcpu->arch.queued_esr = esr_flags;
192 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
193}
194
Alexander Graf8de12012014-06-18 21:56:55 +0200195void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
196 ulong dear_flags, ulong esr_flags)
Liu Yudaf5e272010-02-02 19:44:35 +0800197{
198 vcpu->arch.queued_dear = dear_flags;
199 vcpu->arch.queued_esr = esr_flags;
200 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
201}
202
Alexander Graf8de12012014-06-18 21:56:55 +0200203void kvmppc_core_queue_itlb_miss(struct kvm_vcpu *vcpu)
204{
205 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
206}
207
208void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu, ulong esr_flags)
Liu Yudaf5e272010-02-02 19:44:35 +0800209{
210 vcpu->arch.queued_esr = esr_flags;
211 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
212}
213
Alexander Graf011da892013-01-31 14:17:38 +0100214static void kvmppc_core_queue_alignment(struct kvm_vcpu *vcpu, ulong dear_flags,
215 ulong esr_flags)
216{
217 vcpu->arch.queued_dear = dear_flags;
218 vcpu->arch.queued_esr = esr_flags;
219 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALIGNMENT);
220}
221
Liu Yudaf5e272010-02-02 19:44:35 +0800222void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
223{
224 vcpu->arch.queued_esr = esr_flags;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600225 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600226}
227
228void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
229{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600230 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600231}
232
233int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
234{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600235 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600236}
237
Alexander Graf7706664d2009-12-21 20:21:24 +0100238void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
239{
240 clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
241}
242
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600243void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
244 struct kvm_interrupt *irq)
245{
Alexander Grafc5335f12010-08-30 14:03:24 +0200246 unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
247
248 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
249 prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
250
251 kvmppc_booke_queue_irqprio(vcpu, prio);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600252}
253
Paul Mackerras4fe27d22013-02-14 14:00:25 +0000254void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu)
Alexander Graf4496f972010-04-07 10:03:25 +0200255{
256 clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
Alexander Grafc5335f12010-08-30 14:03:24 +0200257 clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
Alexander Graf4496f972010-04-07 10:03:25 +0200258}
259
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000260static void kvmppc_core_queue_watchdog(struct kvm_vcpu *vcpu)
261{
262 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_WATCHDOG);
263}
264
265static void kvmppc_core_dequeue_watchdog(struct kvm_vcpu *vcpu)
266{
267 clear_bit(BOOKE_IRQPRIO_WATCHDOG, &vcpu->arch.pending_exceptions);
268}
269
Scott Woodd30f6e42011-12-20 15:34:43 +0000270static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
271{
Bharat Bhushan31579ee2014-07-17 17:01:36 +0530272 kvmppc_set_srr0(vcpu, srr0);
273 kvmppc_set_srr1(vcpu, srr1);
Scott Woodd30f6e42011-12-20 15:34:43 +0000274}
275
276static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
277{
278 vcpu->arch.csrr0 = srr0;
279 vcpu->arch.csrr1 = srr1;
280}
281
282static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
283{
284 if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
285 vcpu->arch.dsrr0 = srr0;
286 vcpu->arch.dsrr1 = srr1;
287 } else {
288 set_guest_csrr(vcpu, srr0, srr1);
289 }
290}
291
292static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
293{
294 vcpu->arch.mcsrr0 = srr0;
295 vcpu->arch.mcsrr1 = srr1;
296}
297
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600298/* Deliver the interrupt of the corresponding priority, if possible. */
299static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
300 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500301{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600302 int allowed = 0;
Alexander Graf79300f82012-02-15 19:12:29 +0000303 ulong msr_mask = 0;
Alexander Graf1c810632013-01-04 18:12:48 +0100304 bool update_esr = false, update_dear = false, update_epr = false;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200305 ulong crit_raw = vcpu->arch.shared->critical;
306 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
307 bool crit;
Alexander Grafc5335f12010-08-30 14:03:24 +0200308 bool keep_irq = false;
Scott Woodd30f6e42011-12-20 15:34:43 +0000309 enum int_class int_class;
Mihai Caraman95e90b42012-10-11 06:13:26 +0000310 ulong new_msr = vcpu->arch.shared->msr;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200311
312 /* Truncate crit indicators in 32 bit mode */
313 if (!(vcpu->arch.shared->msr & MSR_SF)) {
314 crit_raw &= 0xffffffff;
315 crit_r1 &= 0xffffffff;
316 }
317
318 /* Critical section when crit == r1 */
319 crit = (crit_raw == crit_r1);
320 /* ... and we're in supervisor mode */
321 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500322
Alexander Grafc5335f12010-08-30 14:03:24 +0200323 if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
324 priority = BOOKE_IRQPRIO_EXTERNAL;
325 keep_irq = true;
326 }
327
Scott Wood5df554ad2013-04-12 14:08:46 +0000328 if ((priority == BOOKE_IRQPRIO_EXTERNAL) && vcpu->arch.epr_flags)
Alexander Graf1c810632013-01-04 18:12:48 +0100329 update_epr = true;
330
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600331 switch (priority) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600332 case BOOKE_IRQPRIO_DTLB_MISS:
Liu Yudaf5e272010-02-02 19:44:35 +0800333 case BOOKE_IRQPRIO_DATA_STORAGE:
Alexander Graf011da892013-01-31 14:17:38 +0100334 case BOOKE_IRQPRIO_ALIGNMENT:
Liu Yudaf5e272010-02-02 19:44:35 +0800335 update_dear = true;
336 /* fall through */
337 case BOOKE_IRQPRIO_INST_STORAGE:
338 case BOOKE_IRQPRIO_PROGRAM:
339 update_esr = true;
340 /* fall through */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600341 case BOOKE_IRQPRIO_ITLB_MISS:
342 case BOOKE_IRQPRIO_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600343 case BOOKE_IRQPRIO_FP_UNAVAIL:
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600344 case BOOKE_IRQPRIO_SPE_UNAVAIL:
345 case BOOKE_IRQPRIO_SPE_FP_DATA:
346 case BOOKE_IRQPRIO_SPE_FP_ROUND:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600347 case BOOKE_IRQPRIO_AP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600348 allowed = 1;
Alexander Graf79300f82012-02-15 19:12:29 +0000349 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000350 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500351 break;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000352 case BOOKE_IRQPRIO_WATCHDOG:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600353 case BOOKE_IRQPRIO_CRITICAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000354 case BOOKE_IRQPRIO_DBELL_CRIT:
Alexander Graf666e7252010-07-29 14:47:43 +0200355 allowed = vcpu->arch.shared->msr & MSR_CE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000356 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000357 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000358 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500359 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600360 case BOOKE_IRQPRIO_MACHINE_CHECK:
Alexander Graf666e7252010-07-29 14:47:43 +0200361 allowed = vcpu->arch.shared->msr & MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000362 allowed = allowed && !crit;
Scott Woodd30f6e42011-12-20 15:34:43 +0000363 int_class = INT_CLASS_MC;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500364 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600365 case BOOKE_IRQPRIO_DECREMENTER:
366 case BOOKE_IRQPRIO_FIT:
Scott Wooddfd4d472011-11-17 12:39:59 +0000367 keep_irq = true;
368 /* fall through */
369 case BOOKE_IRQPRIO_EXTERNAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000370 case BOOKE_IRQPRIO_DBELL:
Alexander Graf666e7252010-07-29 14:47:43 +0200371 allowed = vcpu->arch.shared->msr & MSR_EE;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200372 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000373 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000374 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500375 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600376 case BOOKE_IRQPRIO_DEBUG:
Alexander Graf666e7252010-07-29 14:47:43 +0200377 allowed = vcpu->arch.shared->msr & MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000378 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000379 msr_mask = MSR_ME;
Bharat Bhushan9fee7562014-08-06 12:08:51 +0530380 if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC))
381 int_class = INT_CLASS_DBG;
382 else
383 int_class = INT_CLASS_CRIT;
384
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500385 break;
386 }
387
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600388 if (allowed) {
Scott Woodd30f6e42011-12-20 15:34:43 +0000389 switch (int_class) {
390 case INT_CLASS_NONCRIT:
391 set_guest_srr(vcpu, vcpu->arch.pc,
392 vcpu->arch.shared->msr);
393 break;
394 case INT_CLASS_CRIT:
395 set_guest_csrr(vcpu, vcpu->arch.pc,
396 vcpu->arch.shared->msr);
397 break;
398 case INT_CLASS_DBG:
399 set_guest_dsrr(vcpu, vcpu->arch.pc,
400 vcpu->arch.shared->msr);
401 break;
402 case INT_CLASS_MC:
403 set_guest_mcsrr(vcpu, vcpu->arch.pc,
404 vcpu->arch.shared->msr);
405 break;
406 }
407
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600408 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
Liu Yudaf5e272010-02-02 19:44:35 +0800409 if (update_esr == true)
Bharat Bhushandc168542014-07-17 17:01:38 +0530410 kvmppc_set_esr(vcpu, vcpu->arch.queued_esr);
Liu Yudaf5e272010-02-02 19:44:35 +0800411 if (update_dear == true)
Bharat Bhushana5414d42014-07-17 17:01:37 +0530412 kvmppc_set_dar(vcpu, vcpu->arch.queued_dear);
Scott Wood5df554ad2013-04-12 14:08:46 +0000413 if (update_epr == true) {
414 if (vcpu->arch.epr_flags & KVMPPC_EPR_USER)
415 kvm_make_request(KVM_REQ_EPR_EXIT, vcpu);
Scott Woodeb1e4f42013-04-12 14:08:47 +0000416 else if (vcpu->arch.epr_flags & KVMPPC_EPR_KERNEL) {
417 BUG_ON(vcpu->arch.irq_type != KVMPPC_IRQ_MPIC);
418 kvmppc_mpic_set_epr(vcpu);
419 }
Scott Wood5df554ad2013-04-12 14:08:46 +0000420 }
Mihai Caraman95e90b42012-10-11 06:13:26 +0000421
422 new_msr &= msr_mask;
423#if defined(CONFIG_64BIT)
424 if (vcpu->arch.epcr & SPRN_EPCR_ICM)
425 new_msr |= MSR_CM;
426#endif
427 kvmppc_set_msr(vcpu, new_msr);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600428
Alexander Grafc5335f12010-08-30 14:03:24 +0200429 if (!keep_irq)
430 clear_bit(priority, &vcpu->arch.pending_exceptions);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600431 }
432
Scott Woodd30f6e42011-12-20 15:34:43 +0000433#ifdef CONFIG_KVM_BOOKE_HV
434 /*
435 * If an interrupt is pending but masked, raise a guest doorbell
436 * so that we are notified when the guest enables the relevant
437 * MSR bit.
438 */
439 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
440 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
441 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
442 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
443 if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
444 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
445#endif
446
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600447 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500448}
449
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000450/*
451 * Return the number of jiffies until the next timeout. If the timeout is
452 * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA
453 * because the larger value can break the timer APIs.
454 */
455static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu)
456{
457 u64 tb, wdt_tb, wdt_ticks = 0;
458 u64 nr_jiffies = 0;
459 u32 period = TCR_GET_WP(vcpu->arch.tcr);
460
461 wdt_tb = 1ULL << (63 - period);
462 tb = get_tb();
463 /*
464 * The watchdog timeout will hapeen when TB bit corresponding
465 * to watchdog will toggle from 0 to 1.
466 */
467 if (tb & wdt_tb)
468 wdt_ticks = wdt_tb;
469
470 wdt_ticks += wdt_tb - (tb & (wdt_tb - 1));
471
472 /* Convert timebase ticks to jiffies */
473 nr_jiffies = wdt_ticks;
474
475 if (do_div(nr_jiffies, tb_ticks_per_jiffy))
476 nr_jiffies++;
477
478 return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA);
479}
480
481static void arm_next_watchdog(struct kvm_vcpu *vcpu)
482{
483 unsigned long nr_jiffies;
484 unsigned long flags;
485
486 /*
487 * If TSR_ENW and TSR_WIS are not set then no need to exit to
488 * userspace, so clear the KVM_REQ_WATCHDOG request.
489 */
490 if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS))
491 clear_bit(KVM_REQ_WATCHDOG, &vcpu->requests);
492
493 spin_lock_irqsave(&vcpu->arch.wdt_lock, flags);
494 nr_jiffies = watchdog_next_timeout(vcpu);
495 /*
496 * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA
497 * then do not run the watchdog timer as this can break timer APIs.
498 */
499 if (nr_jiffies < NEXT_TIMER_MAX_DELTA)
500 mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies);
501 else
502 del_timer(&vcpu->arch.wdt_timer);
503 spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags);
504}
505
506void kvmppc_watchdog_func(unsigned long data)
507{
508 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
509 u32 tsr, new_tsr;
510 int final;
511
512 do {
513 new_tsr = tsr = vcpu->arch.tsr;
514 final = 0;
515
516 /* Time out event */
517 if (tsr & TSR_ENW) {
518 if (tsr & TSR_WIS)
519 final = 1;
520 else
521 new_tsr = tsr | TSR_WIS;
522 } else {
523 new_tsr = tsr | TSR_ENW;
524 }
525 } while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr);
526
527 if (new_tsr & TSR_WIS) {
528 smp_wmb();
529 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
530 kvm_vcpu_kick(vcpu);
531 }
532
533 /*
534 * If this is final watchdog expiry and some action is required
535 * then exit to userspace.
536 */
537 if (final && (vcpu->arch.tcr & TCR_WRC_MASK) &&
538 vcpu->arch.watchdog_enabled) {
539 smp_wmb();
540 kvm_make_request(KVM_REQ_WATCHDOG, vcpu);
541 kvm_vcpu_kick(vcpu);
542 }
543
544 /*
545 * Stop running the watchdog timer after final expiration to
546 * prevent the host from being flooded with timers if the
547 * guest sets a short period.
548 * Timers will resume when TSR/TCR is updated next time.
549 */
550 if (!final)
551 arm_next_watchdog(vcpu);
552}
553
Scott Wooddfd4d472011-11-17 12:39:59 +0000554static void update_timer_ints(struct kvm_vcpu *vcpu)
555{
556 if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
557 kvmppc_core_queue_dec(vcpu);
558 else
559 kvmppc_core_dequeue_dec(vcpu);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000560
561 if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS))
562 kvmppc_core_queue_watchdog(vcpu);
563 else
564 kvmppc_core_dequeue_watchdog(vcpu);
Scott Wooddfd4d472011-11-17 12:39:59 +0000565}
566
Scott Woodc59a6a32011-11-08 18:23:25 -0600567static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500568{
569 unsigned long *pending = &vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500570 unsigned int priority;
571
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600572 priority = __ffs(*pending);
Alexander Graf8b3a00f2012-02-16 14:12:46 +0000573 while (priority < BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600574 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500575 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500576
577 priority = find_next_bit(pending,
578 BITS_PER_BYTE * sizeof(*pending),
579 priority + 1);
580 }
Alexander Graf90bba352010-07-29 14:47:51 +0200581
582 /* Tell the guest about our interrupt status */
Scott Wood29ac26e2011-11-08 18:23:27 -0600583 vcpu->arch.shared->int_pending = !!*pending;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500584}
585
Scott Woodc59a6a32011-11-08 18:23:25 -0600586/* Check pending exceptions and deliver one, if possible. */
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000587int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
Scott Woodc59a6a32011-11-08 18:23:25 -0600588{
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000589 int r = 0;
Scott Woodc59a6a32011-11-08 18:23:25 -0600590 WARN_ON_ONCE(!irqs_disabled());
591
592 kvmppc_core_check_exceptions(vcpu);
593
Alexander Grafb8c649a2012-12-20 04:52:39 +0000594 if (vcpu->requests) {
595 /* Exception delivery raised request; start over */
596 return 1;
597 }
598
Scott Woodc59a6a32011-11-08 18:23:25 -0600599 if (vcpu->arch.shared->msr & MSR_WE) {
600 local_irq_enable();
601 kvm_vcpu_block(vcpu);
Alexander Graf966cd0f2012-03-14 16:55:08 +0100602 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Scott Wood6c85f522014-01-09 19:18:40 -0600603 hard_irq_disable();
Scott Woodc59a6a32011-11-08 18:23:25 -0600604
605 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000606 r = 1;
Scott Woodc59a6a32011-11-08 18:23:25 -0600607 };
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000608
609 return r;
610}
611
Alexander Graf7c973a22012-08-13 12:50:35 +0200612int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
Alexander Graf4ffc6352012-08-08 20:31:13 +0200613{
Alexander Graf7c973a22012-08-13 12:50:35 +0200614 int r = 1; /* Indicate we want to get back into the guest */
615
Alexander Graf2d8185d2012-08-10 12:31:12 +0200616 if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
617 update_timer_ints(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200618#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Alexander Graf2d8185d2012-08-10 12:31:12 +0200619 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
620 kvmppc_core_flush_tlb(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200621#endif
Alexander Graf7c973a22012-08-13 12:50:35 +0200622
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000623 if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) {
624 vcpu->run->exit_reason = KVM_EXIT_WATCHDOG;
625 r = 0;
626 }
627
Alexander Graf1c810632013-01-04 18:12:48 +0100628 if (kvm_check_request(KVM_REQ_EPR_EXIT, vcpu)) {
629 vcpu->run->epr.epr = 0;
630 vcpu->arch.epr_needed = true;
631 vcpu->run->exit_reason = KVM_EXIT_EPR;
632 r = 0;
633 }
634
Alexander Graf7c973a22012-08-13 12:50:35 +0200635 return r;
Alexander Graf4ffc6352012-08-08 20:31:13 +0200636}
637
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000638int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
639{
Alexander Graf7ee78852012-08-13 12:44:41 +0200640 int ret, s;
Scott Woodf5f97212013-11-22 15:52:29 -0600641 struct debug_reg debug;
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000642
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200643 if (!vcpu->arch.sane) {
644 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
645 return -EINVAL;
646 }
647
Alexander Graf7ee78852012-08-13 12:44:41 +0200648 s = kvmppc_prepare_to_enter(vcpu);
649 if (s <= 0) {
Alexander Graf7ee78852012-08-13 12:44:41 +0200650 ret = s;
Scott Wood1d1ef222011-11-08 16:11:59 -0600651 goto out;
652 }
Scott Wood6c85f522014-01-09 19:18:40 -0600653 /* interrupts now hard-disabled */
Scott Wood1d1ef222011-11-08 16:11:59 -0600654
Scott Wood8fae8452011-12-20 15:34:45 +0000655#ifdef CONFIG_PPC_FPU
656 /* Save userspace FPU state in stack */
657 enable_kernel_fp();
Scott Wood8fae8452011-12-20 15:34:45 +0000658
659 /*
660 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
661 * as always using the FPU. Kernel usage of FP (via
662 * enable_kernel_fp()) in this thread must not occur while
663 * vcpu->fpu_active is set.
664 */
665 vcpu->fpu_active = 1;
666
667 kvmppc_load_guest_fp(vcpu);
668#endif
669
Bharat Bhushance11e482013-07-04 12:27:47 +0530670 /* Switch to guest debug context */
Scott Woodf5f97212013-11-22 15:52:29 -0600671 debug = vcpu->arch.shadow_dbg_reg;
672 switch_booke_debug_regs(&debug);
673 debug = current->thread.debug;
Bharat Bhushance11e482013-07-04 12:27:47 +0530674 current->thread.debug = vcpu->arch.shadow_dbg_reg;
675
Bharat Bhushan08c9a182013-11-18 11:18:54 +0530676 vcpu->arch.pgdir = current->mm->pgd;
Scott Wood5f1c2482013-07-10 17:47:39 -0500677 kvmppc_fix_ee_before_entry();
Scott Woodf8941fbe2013-06-11 11:38:31 -0500678
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000679 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
Scott Wood8fae8452011-12-20 15:34:45 +0000680
Alexander Graf24afa372012-08-12 12:42:30 +0200681 /* No need for kvm_guest_exit. It's done in handle_exit.
682 We also get here with interrupts enabled. */
683
Bharat Bhushance11e482013-07-04 12:27:47 +0530684 /* Switch back to user space debug context */
Scott Woodf5f97212013-11-22 15:52:29 -0600685 switch_booke_debug_regs(&debug);
686 current->thread.debug = debug;
Bharat Bhushance11e482013-07-04 12:27:47 +0530687
Scott Wood8fae8452011-12-20 15:34:45 +0000688#ifdef CONFIG_PPC_FPU
689 kvmppc_save_guest_fp(vcpu);
690
691 vcpu->fpu_active = 0;
Scott Wood8fae8452011-12-20 15:34:45 +0000692#endif
693
Scott Wood1d1ef222011-11-08 16:11:59 -0600694out:
Alexander Grafd69c6432012-08-08 20:44:20 +0200695 vcpu->mode = OUTSIDE_GUEST_MODE;
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000696 return ret;
697}
698
Scott Woodd30f6e42011-12-20 15:34:43 +0000699static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
700{
701 enum emulation_result er;
702
703 er = kvmppc_emulate_instruction(run, vcpu);
704 switch (er) {
705 case EMULATE_DONE:
706 /* don't overwrite subtypes, just account kvm_stats */
707 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
708 /* Future optimization: only reload non-volatiles if
709 * they were actually modified by emulation. */
710 return RESUME_GUEST_NV;
711
Mihai Caraman51f04722014-07-23 19:06:21 +0300712 case EMULATE_AGAIN:
713 return RESUME_GUEST;
714
Scott Woodd30f6e42011-12-20 15:34:43 +0000715 case EMULATE_FAIL:
Scott Woodd30f6e42011-12-20 15:34:43 +0000716 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
717 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
718 /* For debugging, encode the failing instruction and
719 * report it to userspace. */
720 run->hw.hardware_exit_reason = ~0ULL << 32;
721 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
Alexander Grafd1ff5492012-02-16 13:24:03 +0000722 kvmppc_core_queue_program(vcpu, ESR_PIL);
Scott Woodd30f6e42011-12-20 15:34:43 +0000723 return RESUME_HOST;
724
Bharat Bhushan9b4f5302013-04-08 00:32:15 +0000725 case EMULATE_EXIT_USER:
726 return RESUME_HOST;
727
Scott Woodd30f6e42011-12-20 15:34:43 +0000728 default:
729 BUG();
730 }
731}
732
Bharat Bhushance11e482013-07-04 12:27:47 +0530733static int kvmppc_handle_debug(struct kvm_run *run, struct kvm_vcpu *vcpu)
734{
735 struct debug_reg *dbg_reg = &(vcpu->arch.shadow_dbg_reg);
736 u32 dbsr = vcpu->arch.dbsr;
737
738 run->debug.arch.status = 0;
739 run->debug.arch.address = vcpu->arch.pc;
740
741 if (dbsr & (DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4)) {
742 run->debug.arch.status |= KVMPPC_DEBUG_BREAKPOINT;
743 } else {
744 if (dbsr & (DBSR_DAC1W | DBSR_DAC2W))
745 run->debug.arch.status |= KVMPPC_DEBUG_WATCH_WRITE;
746 else if (dbsr & (DBSR_DAC1R | DBSR_DAC2R))
747 run->debug.arch.status |= KVMPPC_DEBUG_WATCH_READ;
748 if (dbsr & (DBSR_DAC1R | DBSR_DAC1W))
749 run->debug.arch.address = dbg_reg->dac1;
750 else if (dbsr & (DBSR_DAC2R | DBSR_DAC2W))
751 run->debug.arch.address = dbg_reg->dac2;
752 }
753
754 return RESUME_HOST;
755}
756
Alexander Graf4e642cc2012-02-20 23:57:26 +0100757static void kvmppc_fill_pt_regs(struct pt_regs *regs)
758{
759 ulong r1, ip, msr, lr;
760
761 asm("mr %0, 1" : "=r"(r1));
762 asm("mflr %0" : "=r"(lr));
763 asm("mfmsr %0" : "=r"(msr));
764 asm("bl 1f; 1: mflr %0" : "=r"(ip));
765
766 memset(regs, 0, sizeof(*regs));
767 regs->gpr[1] = r1;
768 regs->nip = ip;
769 regs->msr = msr;
770 regs->link = lr;
771}
772
Bharat Bhushan6328e592012-06-20 05:56:53 +0000773/*
774 * For interrupts needed to be handled by host interrupt handlers,
775 * corresponding host handler are called from here in similar way
776 * (but not exact) as they are called from low level handler
777 * (such as from arch/powerpc/kernel/head_fsl_booke.S).
778 */
Alexander Graf4e642cc2012-02-20 23:57:26 +0100779static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
780 unsigned int exit_nr)
781{
782 struct pt_regs regs;
783
784 switch (exit_nr) {
785 case BOOKE_INTERRUPT_EXTERNAL:
786 kvmppc_fill_pt_regs(&regs);
787 do_IRQ(&regs);
788 break;
789 case BOOKE_INTERRUPT_DECREMENTER:
790 kvmppc_fill_pt_regs(&regs);
791 timer_interrupt(&regs);
792 break;
Tiejun Chen5f17ce82013-05-13 10:00:45 +0800793#if defined(CONFIG_PPC_DOORBELL)
Alexander Graf4e642cc2012-02-20 23:57:26 +0100794 case BOOKE_INTERRUPT_DOORBELL:
795 kvmppc_fill_pt_regs(&regs);
796 doorbell_exception(&regs);
797 break;
798#endif
799 case BOOKE_INTERRUPT_MACHINE_CHECK:
800 /* FIXME */
801 break;
Alexander Graf7cc1e8e2012-02-22 16:26:34 +0100802 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
803 kvmppc_fill_pt_regs(&regs);
804 performance_monitor_exception(&regs);
805 break;
Bharat Bhushan6328e592012-06-20 05:56:53 +0000806 case BOOKE_INTERRUPT_WATCHDOG:
807 kvmppc_fill_pt_regs(&regs);
808#ifdef CONFIG_BOOKE_WDT
809 WatchdogException(&regs);
810#else
811 unknown_exception(&regs);
812#endif
813 break;
814 case BOOKE_INTERRUPT_CRITICAL:
815 unknown_exception(&regs);
816 break;
Bharat Bhushance11e482013-07-04 12:27:47 +0530817 case BOOKE_INTERRUPT_DEBUG:
818 /* Save DBSR before preemption is enabled */
819 vcpu->arch.dbsr = mfspr(SPRN_DBSR);
820 kvmppc_clear_dbsr();
821 break;
Alexander Graf4e642cc2012-02-20 23:57:26 +0100822 }
823}
824
Mihai Caramanf5250472014-07-23 19:06:22 +0300825static int kvmppc_resume_inst_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
826 enum emulation_result emulated, u32 last_inst)
827{
828 switch (emulated) {
829 case EMULATE_AGAIN:
830 return RESUME_GUEST;
831
832 case EMULATE_FAIL:
833 pr_debug("%s: load instruction from guest address %lx failed\n",
834 __func__, vcpu->arch.pc);
835 /* For debugging, encode the failing instruction and
836 * report it to userspace. */
837 run->hw.hardware_exit_reason = ~0ULL << 32;
838 run->hw.hardware_exit_reason |= last_inst;
839 kvmppc_core_queue_program(vcpu, ESR_PIL);
840 return RESUME_HOST;
841
842 default:
843 BUG();
844 }
845}
846
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500847/**
848 * kvmppc_handle_exit
849 *
850 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
851 */
852int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
853 unsigned int exit_nr)
854{
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500855 int r = RESUME_HOST;
Alexander Graf7ee78852012-08-13 12:44:41 +0200856 int s;
Scott Woodf1e89022013-06-06 19:16:31 -0500857 int idx;
Mihai Caramanf5250472014-07-23 19:06:22 +0300858 u32 last_inst = KVM_INST_FETCH_FAILED;
859 enum emulation_result emulated = EMULATE_DONE;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500860
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600861 /* update before a new last_exit_type is rewritten */
862 kvmppc_update_timing_stats(vcpu);
863
Alexander Graf4e642cc2012-02-20 23:57:26 +0100864 /* restart interrupts if they were meant for the host */
865 kvmppc_restart_interrupt(vcpu, exit_nr);
Scott Woodd30f6e42011-12-20 15:34:43 +0000866
Mihai Caramanf5250472014-07-23 19:06:22 +0300867 /*
868 * get last instruction before beeing preempted
869 * TODO: for e6500 check also BOOKE_INTERRUPT_LRAT_ERROR & ESR_DATA
870 */
871 switch (exit_nr) {
872 case BOOKE_INTERRUPT_DATA_STORAGE:
873 case BOOKE_INTERRUPT_DTLB_MISS:
874 case BOOKE_INTERRUPT_HV_PRIV:
875 emulated = kvmppc_get_last_inst(vcpu, false, &last_inst);
876 break;
877 default:
878 break;
879 }
880
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500881 local_irq_enable();
882
Alexander Graf97c95052012-08-02 15:10:00 +0200883 trace_kvm_exit(exit_nr, vcpu);
Alexander Graf706fb732012-08-12 11:29:09 +0200884 kvm_guest_exit();
Alexander Graf97c95052012-08-02 15:10:00 +0200885
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500886 run->exit_reason = KVM_EXIT_UNKNOWN;
887 run->ready_for_interrupt_injection = 1;
888
Mihai Caramanf5250472014-07-23 19:06:22 +0300889 if (emulated != EMULATE_DONE) {
890 r = kvmppc_resume_inst_load(run, vcpu, emulated, last_inst);
891 goto out;
892 }
893
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500894 switch (exit_nr) {
895 case BOOKE_INTERRUPT_MACHINE_CHECK:
Alexander Grafc35c9d82012-02-20 12:21:18 +0100896 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
897 kvmppc_dump_vcpu(vcpu);
898 /* For debugging, send invalid exit reason to user space */
899 run->hw.hardware_exit_reason = ~1ULL << 32;
900 run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
901 r = RESUME_HOST;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500902 break;
903
904 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600905 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -0600906 r = RESUME_GUEST;
907 break;
908
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500909 case BOOKE_INTERRUPT_DECREMENTER:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600910 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500911 r = RESUME_GUEST;
912 break;
913
Bharat Bhushan6328e592012-06-20 05:56:53 +0000914 case BOOKE_INTERRUPT_WATCHDOG:
915 r = RESUME_GUEST;
916 break;
917
Scott Woodd30f6e42011-12-20 15:34:43 +0000918 case BOOKE_INTERRUPT_DOORBELL:
919 kvmppc_account_exit(vcpu, DBELL_EXITS);
Scott Woodd30f6e42011-12-20 15:34:43 +0000920 r = RESUME_GUEST;
921 break;
922
923 case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
924 kvmppc_account_exit(vcpu, GDBELL_EXITS);
925
926 /*
927 * We are here because there is a pending guest interrupt
928 * which could not be delivered as MSR_CE or MSR_ME was not
929 * set. Once we break from here we will retry delivery.
930 */
931 r = RESUME_GUEST;
932 break;
933
934 case BOOKE_INTERRUPT_GUEST_DBELL:
935 kvmppc_account_exit(vcpu, GDBELL_EXITS);
936
937 /*
938 * We are here because there is a pending guest interrupt
939 * which could not be delivered as MSR_EE was not set. Once
940 * we break from here we will retry delivery.
941 */
942 r = RESUME_GUEST;
943 break;
944
Alexander Graf95f2e922012-02-20 22:45:12 +0100945 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
946 r = RESUME_GUEST;
947 break;
948
Scott Woodd30f6e42011-12-20 15:34:43 +0000949 case BOOKE_INTERRUPT_HV_PRIV:
950 r = emulation_exit(run, vcpu);
951 break;
952
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500953 case BOOKE_INTERRUPT_PROGRAM:
Scott Woodd30f6e42011-12-20 15:34:43 +0000954 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
Alexander Graf02685972012-02-20 12:33:22 +0100955 /*
956 * Program traps generated by user-level software must
957 * be handled by the guest kernel.
958 *
959 * In GS mode, hypervisor privileged instructions trap
960 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
961 * actual program interrupts, handled by the guest.
962 */
Liu Yudaf5e272010-02-02 19:44:35 +0800963 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500964 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600965 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500966 break;
967 }
968
Scott Woodd30f6e42011-12-20 15:34:43 +0000969 r = emulation_exit(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500970 break;
971
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200972 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600973 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600974 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200975 r = RESUME_GUEST;
976 break;
977
Scott Wood4cd35f62011-06-14 18:34:31 -0500978#ifdef CONFIG_SPE
979 case BOOKE_INTERRUPT_SPE_UNAVAIL: {
980 if (vcpu->arch.shared->msr & MSR_SPE)
981 kvmppc_vcpu_enable_spe(vcpu);
982 else
983 kvmppc_booke_queue_irqprio(vcpu,
984 BOOKE_IRQPRIO_SPE_UNAVAIL);
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600985 r = RESUME_GUEST;
986 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500987 }
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600988
989 case BOOKE_INTERRUPT_SPE_FP_DATA:
990 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
991 r = RESUME_GUEST;
992 break;
993
994 case BOOKE_INTERRUPT_SPE_FP_ROUND:
995 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
996 r = RESUME_GUEST;
997 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500998#else
999 case BOOKE_INTERRUPT_SPE_UNAVAIL:
1000 /*
1001 * Guest wants SPE, but host kernel doesn't support it. Send
1002 * an "unimplemented operation" program check to the guest.
1003 */
1004 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
1005 r = RESUME_GUEST;
1006 break;
1007
1008 /*
1009 * These really should never happen without CONFIG_SPE,
1010 * as we should never enable the real MSR[SPE] in the guest.
1011 */
1012 case BOOKE_INTERRUPT_SPE_FP_DATA:
1013 case BOOKE_INTERRUPT_SPE_FP_ROUND:
1014 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
1015 __func__, exit_nr, vcpu->arch.pc);
1016 run->hw.hardware_exit_reason = exit_nr;
1017 r = RESUME_HOST;
1018 break;
1019#endif
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -06001020
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001021 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +08001022 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
1023 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001024 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001025 r = RESUME_GUEST;
1026 break;
1027
1028 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +08001029 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001030 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001031 r = RESUME_GUEST;
1032 break;
1033
Alexander Graf011da892013-01-31 14:17:38 +01001034 case BOOKE_INTERRUPT_ALIGNMENT:
1035 kvmppc_core_queue_alignment(vcpu, vcpu->arch.fault_dear,
1036 vcpu->arch.fault_esr);
1037 r = RESUME_GUEST;
1038 break;
1039
Scott Woodd30f6e42011-12-20 15:34:43 +00001040#ifdef CONFIG_KVM_BOOKE_HV
1041 case BOOKE_INTERRUPT_HV_SYSCALL:
1042 if (!(vcpu->arch.shared->msr & MSR_PR)) {
1043 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1044 } else {
1045 /*
1046 * hcall from guest userspace -- send privileged
1047 * instruction program check.
1048 */
1049 kvmppc_core_queue_program(vcpu, ESR_PPR);
1050 }
1051
1052 r = RESUME_GUEST;
1053 break;
1054#else
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001055 case BOOKE_INTERRUPT_SYSCALL:
Alexander Graf2a342ed2010-07-29 14:47:48 +02001056 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1057 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
1058 /* KVM PV hypercalls */
1059 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1060 r = RESUME_GUEST;
1061 } else {
1062 /* Guest syscalls */
1063 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
1064 }
Hollis Blanchard7b701592008-12-02 15:51:58 -06001065 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001066 r = RESUME_GUEST;
1067 break;
Scott Woodd30f6e42011-12-20 15:34:43 +00001068#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001069
1070 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001071 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001072 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001073 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001074 gfn_t gfn;
1075
Alexander Grafbf7ca4b2012-02-15 23:40:00 +00001076#ifdef CONFIG_KVM_E500V2
Scott Wooda4cd8b22011-06-14 18:34:41 -05001077 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1078 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
1079 kvmppc_map_magic(vcpu);
1080 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
1081 r = RESUME_GUEST;
1082
1083 break;
1084 }
1085#endif
1086
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001087 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -06001088 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001089 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001090 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +08001091 kvmppc_core_queue_dtlb_miss(vcpu,
1092 vcpu->arch.fault_dear,
1093 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -06001094 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001095 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001096 r = RESUME_GUEST;
1097 break;
1098 }
1099
Scott Woodf1e89022013-06-06 19:16:31 -05001100 idx = srcu_read_lock(&vcpu->kvm->srcu);
1101
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -06001102 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001103 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001104
1105 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1106 /* The guest TLB had a mapping, but the shadow TLB
1107 * didn't, and it is RAM. This could be because:
1108 * a) the entry is mapping the host kernel, or
1109 * b) the guest used a large mapping which we're faking
1110 * Either way, we need to satisfy the fault without
1111 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -06001112 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001113 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001114 r = RESUME_GUEST;
1115 } else {
1116 /* Guest has mapped and accessed a page which is not
1117 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001118 vcpu->arch.paddr_accessed = gpaddr;
Alexander Graf6020c0f2012-03-12 02:26:30 +01001119 vcpu->arch.vaddr_accessed = eaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001120 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001121 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001122 }
1123
Scott Woodf1e89022013-06-06 19:16:31 -05001124 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001125 break;
1126 }
1127
1128 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001129 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -06001130 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001131 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001132 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001133
1134 r = RESUME_GUEST;
1135
1136 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -06001137 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001138 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001139 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001140 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -06001141 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001142 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001143 break;
1144 }
1145
Hollis Blanchard7b701592008-12-02 15:51:58 -06001146 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001147
Scott Woodf1e89022013-06-06 19:16:31 -05001148 idx = srcu_read_lock(&vcpu->kvm->srcu);
1149
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -06001150 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -06001151 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001152
1153 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1154 /* The guest TLB had a mapping, but the shadow TLB
1155 * didn't. This could be because:
1156 * a) the entry is mapping the host kernel, or
1157 * b) the guest used a large mapping which we're faking
1158 * Either way, we need to satisfy the fault without
1159 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -06001160 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001161 } else {
1162 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001163 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001164 }
1165
Scott Woodf1e89022013-06-06 19:16:31 -05001166 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001167 break;
1168 }
1169
Hollis Blanchard6a0ab732008-07-25 13:54:49 -05001170 case BOOKE_INTERRUPT_DEBUG: {
Bharat Bhushance11e482013-07-04 12:27:47 +05301171 r = kvmppc_handle_debug(run, vcpu);
1172 if (r == RESUME_HOST)
1173 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -06001174 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -05001175 break;
1176 }
1177
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001178 default:
1179 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
1180 BUG();
1181 }
1182
Mihai Caramanf5250472014-07-23 19:06:22 +03001183out:
Alexander Grafa8e4ef82012-02-16 14:07:37 +00001184 /*
1185 * To avoid clobbering exit_reason, only check for signals if we
1186 * aren't already exiting to userspace for some other reason.
1187 */
Alexander Graf03660ba2012-02-28 12:00:41 +01001188 if (!(r & RESUME_HOST)) {
Alexander Graf7ee78852012-08-13 12:44:41 +02001189 s = kvmppc_prepare_to_enter(vcpu);
Scott Wood6c85f522014-01-09 19:18:40 -06001190 if (s <= 0)
Alexander Graf7ee78852012-08-13 12:44:41 +02001191 r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
Scott Wood6c85f522014-01-09 19:18:40 -06001192 else {
1193 /* interrupts now hard-disabled */
Scott Wood5f1c2482013-07-10 17:47:39 -05001194 kvmppc_fix_ee_before_entry();
Alexander Graf03660ba2012-02-28 12:00:41 +01001195 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001196 }
1197
1198 return r;
1199}
1200
Bharat Bhushand26f22c2013-02-24 18:57:11 +00001201static void kvmppc_set_tsr(struct kvm_vcpu *vcpu, u32 new_tsr)
1202{
1203 u32 old_tsr = vcpu->arch.tsr;
1204
1205 vcpu->arch.tsr = new_tsr;
1206
1207 if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS))
1208 arm_next_watchdog(vcpu);
1209
1210 update_timer_ints(vcpu);
1211}
1212
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001213/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
1214int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1215{
Hollis Blanchard082decf2010-08-07 10:33:56 -07001216 int i;
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001217 int r;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001218
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001219 vcpu->arch.pc = 0;
Scott Woodb5904972011-11-08 18:23:30 -06001220 vcpu->arch.shared->pir = vcpu->vcpu_id;
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001221 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Scott Woodd30f6e42011-12-20 15:34:43 +00001222 kvmppc_set_msr(vcpu, 0);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001223
Scott Woodd30f6e42011-12-20 15:34:43 +00001224#ifndef CONFIG_KVM_BOOKE_HV
Bharat Bhushance11e482013-07-04 12:27:47 +05301225 vcpu->arch.shadow_msr = MSR_USER | MSR_IS | MSR_DS;
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001226 vcpu->arch.shadow_pid = 1;
Scott Woodd30f6e42011-12-20 15:34:43 +00001227 vcpu->arch.shared->msr = 0;
1228#endif
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001229
Hollis Blanchard082decf2010-08-07 10:33:56 -07001230 /* Eye-catching numbers so we know if the guest takes an interrupt
1231 * before it's programmed its own IVPR/IVORs. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001232 vcpu->arch.ivpr = 0x55550000;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001233 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
1234 vcpu->arch.ivor[i] = 0x7700 | i * 4;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001235
Hollis Blanchard73e75b42008-12-02 15:51:57 -06001236 kvmppc_init_timing_stats(vcpu);
1237
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001238 r = kvmppc_core_vcpu_setup(vcpu);
1239 kvmppc_sanity_check(vcpu);
1240 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001241}
1242
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001243int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
1244{
1245 /* setup watchdog timer once */
1246 spin_lock_init(&vcpu->arch.wdt_lock);
1247 setup_timer(&vcpu->arch.wdt_timer, kvmppc_watchdog_func,
1248 (unsigned long)vcpu);
1249
1250 return 0;
1251}
1252
1253void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
1254{
1255 del_timer_sync(&vcpu->arch.wdt_timer);
1256}
1257
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001258int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1259{
1260 int i;
1261
1262 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001263 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001264 regs->ctr = vcpu->arch.ctr;
1265 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001266 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001267 regs->msr = vcpu->arch.shared->msr;
Bharat Bhushan31579ee2014-07-17 17:01:36 +05301268 regs->srr0 = kvmppc_get_srr0(vcpu);
1269 regs->srr1 = kvmppc_get_srr1(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001270 regs->pid = vcpu->arch.pid;
Bharat Bhushanc1b8a012014-07-17 17:01:39 +05301271 regs->sprg0 = kvmppc_get_sprg0(vcpu);
1272 regs->sprg1 = kvmppc_get_sprg1(vcpu);
1273 regs->sprg2 = kvmppc_get_sprg2(vcpu);
1274 regs->sprg3 = kvmppc_get_sprg3(vcpu);
1275 regs->sprg4 = kvmppc_get_sprg4(vcpu);
1276 regs->sprg5 = kvmppc_get_sprg5(vcpu);
1277 regs->sprg6 = kvmppc_get_sprg6(vcpu);
1278 regs->sprg7 = kvmppc_get_sprg7(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001279
1280 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001281 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001282
1283 return 0;
1284}
1285
1286int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1287{
1288 int i;
1289
1290 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001291 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001292 vcpu->arch.ctr = regs->ctr;
1293 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001294 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -06001295 kvmppc_set_msr(vcpu, regs->msr);
Bharat Bhushan31579ee2014-07-17 17:01:36 +05301296 kvmppc_set_srr0(vcpu, regs->srr0);
1297 kvmppc_set_srr1(vcpu, regs->srr1);
Scott Wood5ce941e2011-04-27 17:24:21 -05001298 kvmppc_set_pid(vcpu, regs->pid);
Bharat Bhushanc1b8a012014-07-17 17:01:39 +05301299 kvmppc_set_sprg0(vcpu, regs->sprg0);
1300 kvmppc_set_sprg1(vcpu, regs->sprg1);
1301 kvmppc_set_sprg2(vcpu, regs->sprg2);
1302 kvmppc_set_sprg3(vcpu, regs->sprg3);
1303 kvmppc_set_sprg4(vcpu, regs->sprg4);
1304 kvmppc_set_sprg5(vcpu, regs->sprg5);
1305 kvmppc_set_sprg6(vcpu, regs->sprg6);
1306 kvmppc_set_sprg7(vcpu, regs->sprg7);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001307
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001308 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1309 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001310
1311 return 0;
1312}
1313
Scott Wood5ce941e2011-04-27 17:24:21 -05001314static void get_sregs_base(struct kvm_vcpu *vcpu,
1315 struct kvm_sregs *sregs)
1316{
1317 u64 tb = get_tb();
1318
1319 sregs->u.e.features |= KVM_SREGS_E_BASE;
1320
1321 sregs->u.e.csrr0 = vcpu->arch.csrr0;
1322 sregs->u.e.csrr1 = vcpu->arch.csrr1;
1323 sregs->u.e.mcsr = vcpu->arch.mcsr;
Bharat Bhushandc168542014-07-17 17:01:38 +05301324 sregs->u.e.esr = kvmppc_get_esr(vcpu);
Bharat Bhushana5414d42014-07-17 17:01:37 +05301325 sregs->u.e.dear = kvmppc_get_dar(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001326 sregs->u.e.tsr = vcpu->arch.tsr;
1327 sregs->u.e.tcr = vcpu->arch.tcr;
1328 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
1329 sregs->u.e.tb = tb;
1330 sregs->u.e.vrsave = vcpu->arch.vrsave;
1331}
1332
1333static int set_sregs_base(struct kvm_vcpu *vcpu,
1334 struct kvm_sregs *sregs)
1335{
1336 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
1337 return 0;
1338
1339 vcpu->arch.csrr0 = sregs->u.e.csrr0;
1340 vcpu->arch.csrr1 = sregs->u.e.csrr1;
1341 vcpu->arch.mcsr = sregs->u.e.mcsr;
Bharat Bhushandc168542014-07-17 17:01:38 +05301342 kvmppc_set_esr(vcpu, sregs->u.e.esr);
Bharat Bhushana5414d42014-07-17 17:01:37 +05301343 kvmppc_set_dar(vcpu, sregs->u.e.dear);
Scott Wood5ce941e2011-04-27 17:24:21 -05001344 vcpu->arch.vrsave = sregs->u.e.vrsave;
Scott Wooddfd4d472011-11-17 12:39:59 +00001345 kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001346
Scott Wooddfd4d472011-11-17 12:39:59 +00001347 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
Scott Wood5ce941e2011-04-27 17:24:21 -05001348 vcpu->arch.dec = sregs->u.e.dec;
Scott Wooddfd4d472011-11-17 12:39:59 +00001349 kvmppc_emulate_dec(vcpu);
1350 }
Scott Wood5ce941e2011-04-27 17:24:21 -05001351
Bharat Bhushand26f22c2013-02-24 18:57:11 +00001352 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR)
1353 kvmppc_set_tsr(vcpu, sregs->u.e.tsr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001354
1355 return 0;
1356}
1357
1358static void get_sregs_arch206(struct kvm_vcpu *vcpu,
1359 struct kvm_sregs *sregs)
1360{
1361 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
1362
Scott Wood841741f2011-09-02 17:39:37 -05001363 sregs->u.e.pir = vcpu->vcpu_id;
Scott Wood5ce941e2011-04-27 17:24:21 -05001364 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
1365 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
1366 sregs->u.e.decar = vcpu->arch.decar;
1367 sregs->u.e.ivpr = vcpu->arch.ivpr;
1368}
1369
1370static int set_sregs_arch206(struct kvm_vcpu *vcpu,
1371 struct kvm_sregs *sregs)
1372{
1373 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
1374 return 0;
1375
Scott Wood841741f2011-09-02 17:39:37 -05001376 if (sregs->u.e.pir != vcpu->vcpu_id)
Scott Wood5ce941e2011-04-27 17:24:21 -05001377 return -EINVAL;
1378
1379 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
1380 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
1381 vcpu->arch.decar = sregs->u.e.decar;
1382 vcpu->arch.ivpr = sregs->u.e.ivpr;
1383
1384 return 0;
1385}
1386
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301387int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Scott Wood5ce941e2011-04-27 17:24:21 -05001388{
1389 sregs->u.e.features |= KVM_SREGS_E_IVOR;
1390
1391 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
1392 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
1393 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
1394 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
1395 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
1396 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
1397 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
1398 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
1399 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
1400 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
1401 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
1402 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
1403 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
1404 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
1405 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
1406 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301407 return 0;
Scott Wood5ce941e2011-04-27 17:24:21 -05001408}
1409
1410int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1411{
1412 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
1413 return 0;
1414
1415 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
1416 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
1417 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
1418 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
1419 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
1420 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
1421 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
1422 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
1423 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
1424 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
1425 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
1426 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
1427 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
1428 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
1429 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
1430 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
1431
1432 return 0;
1433}
1434
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001435int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1436 struct kvm_sregs *sregs)
1437{
Scott Wood5ce941e2011-04-27 17:24:21 -05001438 sregs->pvr = vcpu->arch.pvr;
1439
1440 get_sregs_base(vcpu, sregs);
1441 get_sregs_arch206(vcpu, sregs);
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301442 return vcpu->kvm->arch.kvm_ops->get_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001443}
1444
1445int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1446 struct kvm_sregs *sregs)
1447{
Scott Wood5ce941e2011-04-27 17:24:21 -05001448 int ret;
1449
1450 if (vcpu->arch.pvr != sregs->pvr)
1451 return -EINVAL;
1452
1453 ret = set_sregs_base(vcpu, sregs);
1454 if (ret < 0)
1455 return ret;
1456
1457 ret = set_sregs_arch206(vcpu, sregs);
1458 if (ret < 0)
1459 return ret;
1460
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301461 return vcpu->kvm->arch.kvm_ops->set_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001462}
1463
Paul Mackerras31f34382011-12-12 12:26:50 +00001464int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1465{
Mihai Caraman35b299e2013-04-11 00:03:07 +00001466 int r = 0;
1467 union kvmppc_one_reg val;
1468 int size;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001469
1470 size = one_reg_size(reg->id);
1471 if (size > sizeof(val))
1472 return -EINVAL;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001473
1474 switch (reg->id) {
1475 case KVM_REG_PPC_IAC1:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301476 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac1);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001477 break;
Bharat Bhushan547465e2013-07-04 12:27:46 +05301478 case KVM_REG_PPC_IAC2:
1479 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac2);
1480 break;
1481#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1482 case KVM_REG_PPC_IAC3:
1483 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac3);
1484 break;
1485 case KVM_REG_PPC_IAC4:
1486 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac4);
1487 break;
1488#endif
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001489 case KVM_REG_PPC_DAC1:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301490 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.dac1);
1491 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001492 case KVM_REG_PPC_DAC2:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301493 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.dac2);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001494 break;
Alexander Graf324b3e62013-01-04 18:28:51 +01001495 case KVM_REG_PPC_EPR: {
Bharat Bhushan34f754b2014-07-17 17:01:40 +05301496 u32 epr = kvmppc_get_epr(vcpu);
Mihai Caraman35b299e2013-04-11 00:03:07 +00001497 val = get_reg_val(reg->id, epr);
Alexander Graf324b3e62013-01-04 18:28:51 +01001498 break;
1499 }
Mihai Caraman352df1d2012-10-11 06:13:29 +00001500#if defined(CONFIG_64BIT)
1501 case KVM_REG_PPC_EPCR:
Mihai Caraman35b299e2013-04-11 00:03:07 +00001502 val = get_reg_val(reg->id, vcpu->arch.epcr);
Mihai Caraman352df1d2012-10-11 06:13:29 +00001503 break;
1504#endif
Bharat Bhushan78accda2013-02-24 18:57:12 +00001505 case KVM_REG_PPC_TCR:
Mihai Caraman35b299e2013-04-11 00:03:07 +00001506 val = get_reg_val(reg->id, vcpu->arch.tcr);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001507 break;
1508 case KVM_REG_PPC_TSR:
Mihai Caraman35b299e2013-04-11 00:03:07 +00001509 val = get_reg_val(reg->id, vcpu->arch.tsr);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001510 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001511 case KVM_REG_PPC_DEBUG_INST:
Bharat Bhushanb12c7842013-07-04 12:27:45 +05301512 val = get_reg_val(reg->id, KVMPPC_INST_EHPRIV_DEBUG);
Bharat Bhushan8c32a2e2013-03-20 20:24:58 +00001513 break;
Paul Mackerras8b75cbb2013-09-20 14:52:37 +10001514 case KVM_REG_PPC_VRSAVE:
1515 val = get_reg_val(reg->id, vcpu->arch.vrsave);
Bharat Bhushan8c32a2e2013-03-20 20:24:58 +00001516 break;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001517 default:
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301518 r = vcpu->kvm->arch.kvm_ops->get_one_reg(vcpu, reg->id, &val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001519 break;
1520 }
Mihai Caraman35b299e2013-04-11 00:03:07 +00001521
1522 if (r)
1523 return r;
1524
1525 if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
1526 r = -EFAULT;
1527
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001528 return r;
Paul Mackerras31f34382011-12-12 12:26:50 +00001529}
1530
1531int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1532{
Mihai Caraman35b299e2013-04-11 00:03:07 +00001533 int r = 0;
1534 union kvmppc_one_reg val;
1535 int size;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001536
1537 size = one_reg_size(reg->id);
1538 if (size > sizeof(val))
1539 return -EINVAL;
1540
1541 if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
1542 return -EFAULT;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001543
1544 switch (reg->id) {
1545 case KVM_REG_PPC_IAC1:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301546 vcpu->arch.dbg_reg.iac1 = set_reg_val(reg->id, val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001547 break;
Bharat Bhushan547465e2013-07-04 12:27:46 +05301548 case KVM_REG_PPC_IAC2:
1549 vcpu->arch.dbg_reg.iac2 = set_reg_val(reg->id, val);
1550 break;
1551#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1552 case KVM_REG_PPC_IAC3:
1553 vcpu->arch.dbg_reg.iac3 = set_reg_val(reg->id, val);
1554 break;
1555 case KVM_REG_PPC_IAC4:
1556 vcpu->arch.dbg_reg.iac4 = set_reg_val(reg->id, val);
1557 break;
1558#endif
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001559 case KVM_REG_PPC_DAC1:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301560 vcpu->arch.dbg_reg.dac1 = set_reg_val(reg->id, val);
1561 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001562 case KVM_REG_PPC_DAC2:
Bharat Bhushan547465e2013-07-04 12:27:46 +05301563 vcpu->arch.dbg_reg.dac2 = set_reg_val(reg->id, val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001564 break;
Alexander Graf324b3e62013-01-04 18:28:51 +01001565 case KVM_REG_PPC_EPR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001566 u32 new_epr = set_reg_val(reg->id, val);
1567 kvmppc_set_epr(vcpu, new_epr);
Alexander Graf324b3e62013-01-04 18:28:51 +01001568 break;
1569 }
Mihai Caraman352df1d2012-10-11 06:13:29 +00001570#if defined(CONFIG_64BIT)
1571 case KVM_REG_PPC_EPCR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001572 u32 new_epcr = set_reg_val(reg->id, val);
1573 kvmppc_set_epcr(vcpu, new_epcr);
Mihai Caraman352df1d2012-10-11 06:13:29 +00001574 break;
1575 }
1576#endif
Bharat Bhushan78accda2013-02-24 18:57:12 +00001577 case KVM_REG_PPC_OR_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_set_tsr_bits(vcpu, tsr_bits);
1580 break;
1581 }
1582 case KVM_REG_PPC_CLEAR_TSR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001583 u32 tsr_bits = set_reg_val(reg->id, val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001584 kvmppc_clr_tsr_bits(vcpu, tsr_bits);
1585 break;
1586 }
1587 case KVM_REG_PPC_TSR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001588 u32 tsr = set_reg_val(reg->id, val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001589 kvmppc_set_tsr(vcpu, tsr);
1590 break;
1591 }
1592 case KVM_REG_PPC_TCR: {
Mihai Caraman35b299e2013-04-11 00:03:07 +00001593 u32 tcr = set_reg_val(reg->id, val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001594 kvmppc_set_tcr(vcpu, tcr);
1595 break;
1596 }
Paul Mackerras8b75cbb2013-09-20 14:52:37 +10001597 case KVM_REG_PPC_VRSAVE:
1598 vcpu->arch.vrsave = set_reg_val(reg->id, val);
1599 break;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001600 default:
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301601 r = vcpu->kvm->arch.kvm_ops->set_one_reg(vcpu, reg->id, &val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001602 break;
1603 }
Mihai Caraman35b299e2013-04-11 00:03:07 +00001604
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001605 return r;
Paul Mackerras31f34382011-12-12 12:26:50 +00001606}
1607
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001608int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1609{
1610 return -ENOTSUPP;
1611}
1612
1613int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1614{
1615 return -ENOTSUPP;
1616}
1617
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001618int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1619 struct kvm_translation *tr)
1620{
Avi Kivity98001d82010-05-13 11:05:49 +03001621 int r;
1622
Avi Kivity98001d82010-05-13 11:05:49 +03001623 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +03001624 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001625}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001626
Alexander Graf4e755752009-10-30 05:47:01 +00001627int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1628{
1629 return -ENOTSUPP;
1630}
1631
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05301632void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001633 struct kvm_memory_slot *dont)
1634{
1635}
1636
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05301637int kvmppc_core_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001638 unsigned long npages)
1639{
1640 return 0;
1641}
1642
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001643int kvmppc_core_prepare_memory_region(struct kvm *kvm,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001644 struct kvm_memory_slot *memslot,
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001645 struct kvm_userspace_memory_region *mem)
1646{
1647 return 0;
1648}
1649
1650void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001651 struct kvm_userspace_memory_region *mem,
Takuya Yoshikawa84826442013-02-27 19:45:25 +09001652 const struct kvm_memory_slot *old)
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001653{
1654}
1655
1656void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001657{
1658}
1659
Mihai Caraman38f98822012-10-11 06:13:27 +00001660void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr)
1661{
1662#if defined(CONFIG_64BIT)
1663 vcpu->arch.epcr = new_epcr;
1664#ifdef CONFIG_KVM_BOOKE_HV
1665 vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM;
1666 if (vcpu->arch.epcr & SPRN_EPCR_ICM)
1667 vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM;
1668#endif
1669#endif
1670}
1671
Scott Wooddfd4d472011-11-17 12:39:59 +00001672void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1673{
1674 vcpu->arch.tcr = new_tcr;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001675 arm_next_watchdog(vcpu);
Scott Wooddfd4d472011-11-17 12:39:59 +00001676 update_timer_ints(vcpu);
1677}
1678
1679void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1680{
1681 set_bits(tsr_bits, &vcpu->arch.tsr);
1682 smp_wmb();
1683 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1684 kvm_vcpu_kick(vcpu);
1685}
1686
1687void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1688{
1689 clear_bits(tsr_bits, &vcpu->arch.tsr);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001690
1691 /*
1692 * We may have stopped the watchdog due to
1693 * being stuck on final expiration.
1694 */
1695 if (tsr_bits & (TSR_ENW | TSR_WIS))
1696 arm_next_watchdog(vcpu);
1697
Scott Wooddfd4d472011-11-17 12:39:59 +00001698 update_timer_ints(vcpu);
1699}
1700
1701void kvmppc_decrementer_func(unsigned long data)
1702{
1703 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1704
Bharat Bhushan21bd0002012-05-20 23:21:23 +00001705 if (vcpu->arch.tcr & TCR_ARE) {
1706 vcpu->arch.dec = vcpu->arch.decar;
1707 kvmppc_emulate_dec(vcpu);
1708 }
1709
Scott Wooddfd4d472011-11-17 12:39:59 +00001710 kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1711}
1712
Bharat Bhushance11e482013-07-04 12:27:47 +05301713static int kvmppc_booke_add_breakpoint(struct debug_reg *dbg_reg,
1714 uint64_t addr, int index)
1715{
1716 switch (index) {
1717 case 0:
1718 dbg_reg->dbcr0 |= DBCR0_IAC1;
1719 dbg_reg->iac1 = addr;
1720 break;
1721 case 1:
1722 dbg_reg->dbcr0 |= DBCR0_IAC2;
1723 dbg_reg->iac2 = addr;
1724 break;
1725#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1726 case 2:
1727 dbg_reg->dbcr0 |= DBCR0_IAC3;
1728 dbg_reg->iac3 = addr;
1729 break;
1730 case 3:
1731 dbg_reg->dbcr0 |= DBCR0_IAC4;
1732 dbg_reg->iac4 = addr;
1733 break;
1734#endif
1735 default:
1736 return -EINVAL;
1737 }
1738
1739 dbg_reg->dbcr0 |= DBCR0_IDM;
1740 return 0;
1741}
1742
1743static int kvmppc_booke_add_watchpoint(struct debug_reg *dbg_reg, uint64_t addr,
1744 int type, int index)
1745{
1746 switch (index) {
1747 case 0:
1748 if (type & KVMPPC_DEBUG_WATCH_READ)
1749 dbg_reg->dbcr0 |= DBCR0_DAC1R;
1750 if (type & KVMPPC_DEBUG_WATCH_WRITE)
1751 dbg_reg->dbcr0 |= DBCR0_DAC1W;
1752 dbg_reg->dac1 = addr;
1753 break;
1754 case 1:
1755 if (type & KVMPPC_DEBUG_WATCH_READ)
1756 dbg_reg->dbcr0 |= DBCR0_DAC2R;
1757 if (type & KVMPPC_DEBUG_WATCH_WRITE)
1758 dbg_reg->dbcr0 |= DBCR0_DAC2W;
1759 dbg_reg->dac2 = addr;
1760 break;
1761 default:
1762 return -EINVAL;
1763 }
1764
1765 dbg_reg->dbcr0 |= DBCR0_IDM;
1766 return 0;
1767}
1768void kvm_guest_protect_msr(struct kvm_vcpu *vcpu, ulong prot_bitmap, bool set)
1769{
1770 /* XXX: Add similar MSR protection for BookE-PR */
1771#ifdef CONFIG_KVM_BOOKE_HV
1772 BUG_ON(prot_bitmap & ~(MSRP_UCLEP | MSRP_DEP | MSRP_PMMP));
1773 if (set) {
1774 if (prot_bitmap & MSR_UCLE)
1775 vcpu->arch.shadow_msrp |= MSRP_UCLEP;
1776 if (prot_bitmap & MSR_DE)
1777 vcpu->arch.shadow_msrp |= MSRP_DEP;
1778 if (prot_bitmap & MSR_PMM)
1779 vcpu->arch.shadow_msrp |= MSRP_PMMP;
1780 } else {
1781 if (prot_bitmap & MSR_UCLE)
1782 vcpu->arch.shadow_msrp &= ~MSRP_UCLEP;
1783 if (prot_bitmap & MSR_DE)
1784 vcpu->arch.shadow_msrp &= ~MSRP_DEP;
1785 if (prot_bitmap & MSR_PMM)
1786 vcpu->arch.shadow_msrp &= ~MSRP_PMMP;
1787 }
1788#endif
1789}
1790
Alexander Graf7d15c06f2014-06-20 13:52:36 +02001791int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, enum xlate_instdata xlid,
1792 enum xlate_readwrite xlrw, struct kvmppc_pte *pte)
1793{
1794 int gtlb_index;
1795 gpa_t gpaddr;
1796
1797#ifdef CONFIG_KVM_E500V2
1798 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1799 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
1800 pte->eaddr = eaddr;
1801 pte->raddr = (vcpu->arch.magic_page_pa & PAGE_MASK) |
1802 (eaddr & ~PAGE_MASK);
1803 pte->vpage = eaddr >> PAGE_SHIFT;
1804 pte->may_read = true;
1805 pte->may_write = true;
1806 pte->may_execute = true;
1807
1808 return 0;
1809 }
1810#endif
1811
1812 /* Check the guest TLB. */
1813 switch (xlid) {
1814 case XLATE_INST:
1815 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
1816 break;
1817 case XLATE_DATA:
1818 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
1819 break;
1820 default:
1821 BUG();
1822 }
1823
1824 /* Do we have a TLB entry at all? */
1825 if (gtlb_index < 0)
1826 return -ENOENT;
1827
1828 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
1829
1830 pte->eaddr = eaddr;
1831 pte->raddr = (gpaddr & PAGE_MASK) | (eaddr & ~PAGE_MASK);
1832 pte->vpage = eaddr >> PAGE_SHIFT;
1833
1834 /* XXX read permissions from the guest TLB */
1835 pte->may_read = true;
1836 pte->may_write = true;
1837 pte->may_execute = true;
1838
1839 return 0;
1840}
1841
Bharat Bhushance11e482013-07-04 12:27:47 +05301842int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1843 struct kvm_guest_debug *dbg)
1844{
1845 struct debug_reg *dbg_reg;
1846 int n, b = 0, w = 0;
1847
1848 if (!(dbg->control & KVM_GUESTDBG_ENABLE)) {
1849 vcpu->arch.shadow_dbg_reg.dbcr0 = 0;
1850 vcpu->guest_debug = 0;
1851 kvm_guest_protect_msr(vcpu, MSR_DE, false);
1852 return 0;
1853 }
1854
1855 kvm_guest_protect_msr(vcpu, MSR_DE, true);
1856 vcpu->guest_debug = dbg->control;
1857 vcpu->arch.shadow_dbg_reg.dbcr0 = 0;
1858 /* Set DBCR0_EDM in guest visible DBCR0 register. */
1859 vcpu->arch.dbg_reg.dbcr0 = DBCR0_EDM;
1860
1861 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1862 vcpu->arch.shadow_dbg_reg.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1863
1864 /* Code below handles only HW breakpoints */
1865 dbg_reg = &(vcpu->arch.shadow_dbg_reg);
1866
1867#ifdef CONFIG_KVM_BOOKE_HV
1868 /*
1869 * On BookE-HV (e500mc) the guest is always executed with MSR.GS=1
1870 * DBCR1 and DBCR2 are set to trigger debug events when MSR.PR is 0
1871 */
1872 dbg_reg->dbcr1 = 0;
1873 dbg_reg->dbcr2 = 0;
1874#else
1875 /*
1876 * On BookE-PR (e500v2) the guest is always executed with MSR.PR=1
1877 * We set DBCR1 and DBCR2 to only trigger debug events when MSR.PR
1878 * is set.
1879 */
1880 dbg_reg->dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US | DBCR1_IAC3US |
1881 DBCR1_IAC4US;
1882 dbg_reg->dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
1883#endif
1884
1885 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1886 return 0;
1887
1888 for (n = 0; n < (KVMPPC_BOOKE_IAC_NUM + KVMPPC_BOOKE_DAC_NUM); n++) {
1889 uint64_t addr = dbg->arch.bp[n].addr;
1890 uint32_t type = dbg->arch.bp[n].type;
1891
1892 if (type == KVMPPC_DEBUG_NONE)
1893 continue;
1894
1895 if (type & !(KVMPPC_DEBUG_WATCH_READ |
1896 KVMPPC_DEBUG_WATCH_WRITE |
1897 KVMPPC_DEBUG_BREAKPOINT))
1898 return -EINVAL;
1899
1900 if (type & KVMPPC_DEBUG_BREAKPOINT) {
1901 /* Setting H/W breakpoint */
1902 if (kvmppc_booke_add_breakpoint(dbg_reg, addr, b++))
1903 return -EINVAL;
1904 } else {
1905 /* Setting H/W watchpoint */
1906 if (kvmppc_booke_add_watchpoint(dbg_reg, addr,
1907 type, w++))
1908 return -EINVAL;
1909 }
1910 }
1911
1912 return 0;
1913}
1914
Scott Wood94fa9d92011-12-20 15:34:22 +00001915void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1916{
Paul Mackerrasa47d72f2012-09-20 19:35:51 +00001917 vcpu->cpu = smp_processor_id();
Scott Woodd30f6e42011-12-20 15:34:43 +00001918 current->thread.kvm_vcpu = vcpu;
Scott Wood94fa9d92011-12-20 15:34:22 +00001919}
1920
1921void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
1922{
Scott Woodd30f6e42011-12-20 15:34:43 +00001923 current->thread.kvm_vcpu = NULL;
Paul Mackerrasa47d72f2012-09-20 19:35:51 +00001924 vcpu->cpu = -1;
Bharat Bhushance11e482013-07-04 12:27:47 +05301925
1926 /* Clear pending debug event in DBSR */
1927 kvmppc_clear_dbsr();
Scott Wood94fa9d92011-12-20 15:34:22 +00001928}
1929
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301930void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
1931{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301932 vcpu->kvm->arch.kvm_ops->mmu_destroy(vcpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301933}
1934
1935int kvmppc_core_init_vm(struct kvm *kvm)
1936{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301937 return kvm->arch.kvm_ops->init_vm(kvm);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301938}
1939
1940struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
1941{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301942 return kvm->arch.kvm_ops->vcpu_create(kvm, id);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301943}
1944
1945void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
1946{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301947 vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301948}
1949
1950void kvmppc_core_destroy_vm(struct kvm *kvm)
1951{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301952 kvm->arch.kvm_ops->destroy_vm(kvm);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301953}
1954
1955void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1956{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301957 vcpu->kvm->arch.kvm_ops->vcpu_load(vcpu, cpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301958}
1959
1960void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
1961{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301962 vcpu->kvm->arch.kvm_ops->vcpu_put(vcpu);
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001963}
1964
1965int __init kvmppc_booke_init(void)
1966{
Scott Woodd30f6e42011-12-20 15:34:43 +00001967#ifndef CONFIG_KVM_BOOKE_HV
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001968 unsigned long ivor[16];
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001969 unsigned long *handler = kvmppc_booke_handler_addr;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001970 unsigned long max_ivor = 0;
Bharat Bhushan1d542d92013-01-15 22:24:39 +00001971 unsigned long handler_len;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001972 int i;
1973
1974 /* We install our own exception handlers by hijacking IVPR. IVPR must
1975 * be 16-bit aligned, so we need a 64KB allocation. */
1976 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1977 VCPU_SIZE_ORDER);
1978 if (!kvmppc_booke_handlers)
1979 return -ENOMEM;
1980
1981 /* XXX make sure our handlers are smaller than Linux's */
1982
1983 /* Copy our interrupt handlers to match host IVORs. That way we don't
1984 * have to swap the IVORs on every guest/host transition. */
1985 ivor[0] = mfspr(SPRN_IVOR0);
1986 ivor[1] = mfspr(SPRN_IVOR1);
1987 ivor[2] = mfspr(SPRN_IVOR2);
1988 ivor[3] = mfspr(SPRN_IVOR3);
1989 ivor[4] = mfspr(SPRN_IVOR4);
1990 ivor[5] = mfspr(SPRN_IVOR5);
1991 ivor[6] = mfspr(SPRN_IVOR6);
1992 ivor[7] = mfspr(SPRN_IVOR7);
1993 ivor[8] = mfspr(SPRN_IVOR8);
1994 ivor[9] = mfspr(SPRN_IVOR9);
1995 ivor[10] = mfspr(SPRN_IVOR10);
1996 ivor[11] = mfspr(SPRN_IVOR11);
1997 ivor[12] = mfspr(SPRN_IVOR12);
1998 ivor[13] = mfspr(SPRN_IVOR13);
1999 ivor[14] = mfspr(SPRN_IVOR14);
2000 ivor[15] = mfspr(SPRN_IVOR15);
2001
2002 for (i = 0; i < 16; i++) {
2003 if (ivor[i] > max_ivor)
Bharat Bhushan1d542d92013-01-15 22:24:39 +00002004 max_ivor = i;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002005
Bharat Bhushan1d542d92013-01-15 22:24:39 +00002006 handler_len = handler[i + 1] - handler[i];
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002007 memcpy((void *)kvmppc_booke_handlers + ivor[i],
Bharat Bhushan1d542d92013-01-15 22:24:39 +00002008 (void *)handler[i], handler_len);
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002009 }
Bharat Bhushan1d542d92013-01-15 22:24:39 +00002010
2011 handler_len = handler[max_ivor + 1] - handler[max_ivor];
2012 flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers +
2013 ivor[max_ivor] + handler_len);
Scott Woodd30f6e42011-12-20 15:34:43 +00002014#endif /* !BOOKE_HV */
Hollis Blancharddb93f572008-11-05 09:36:18 -06002015 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002016}
2017
Hollis Blancharddb93f572008-11-05 09:36:18 -06002018void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002019{
2020 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
2021 kvm_exit();
2022}