xref: /openbmc/linux/arch/powerpc/kvm/booke.c (revision 7c973a2ebb8fb9c8ee2ae9647f9ad7b0ad58a3e6)
1d9fbd03dSHollis Blanchard /*
2d9fbd03dSHollis Blanchard  * This program is free software; you can redistribute it and/or modify
3d9fbd03dSHollis Blanchard  * it under the terms of the GNU General Public License, version 2, as
4d9fbd03dSHollis Blanchard  * published by the Free Software Foundation.
5d9fbd03dSHollis Blanchard  *
6d9fbd03dSHollis Blanchard  * This program is distributed in the hope that it will be useful,
7d9fbd03dSHollis Blanchard  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8d9fbd03dSHollis Blanchard  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9d9fbd03dSHollis Blanchard  * GNU General Public License for more details.
10d9fbd03dSHollis Blanchard  *
11d9fbd03dSHollis Blanchard  * You should have received a copy of the GNU General Public License
12d9fbd03dSHollis Blanchard  * along with this program; if not, write to the Free Software
13d9fbd03dSHollis Blanchard  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
14d9fbd03dSHollis Blanchard  *
15d9fbd03dSHollis Blanchard  * Copyright IBM Corp. 2007
164cd35f67SScott Wood  * Copyright 2010-2011 Freescale Semiconductor, Inc.
17d9fbd03dSHollis Blanchard  *
18d9fbd03dSHollis Blanchard  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
19d9fbd03dSHollis Blanchard  *          Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
20d30f6e48SScott Wood  *          Scott Wood <scottwood@freescale.com>
21d30f6e48SScott Wood  *          Varun Sethi <varun.sethi@freescale.com>
22d9fbd03dSHollis Blanchard  */
23d9fbd03dSHollis Blanchard 
24d9fbd03dSHollis Blanchard #include <linux/errno.h>
25d9fbd03dSHollis Blanchard #include <linux/err.h>
26d9fbd03dSHollis Blanchard #include <linux/kvm_host.h>
275a0e3ad6STejun Heo #include <linux/gfp.h>
28d9fbd03dSHollis Blanchard #include <linux/module.h>
29d9fbd03dSHollis Blanchard #include <linux/vmalloc.h>
30d9fbd03dSHollis Blanchard #include <linux/fs.h>
317924bd41SHollis Blanchard 
32d9fbd03dSHollis Blanchard #include <asm/cputable.h>
33d9fbd03dSHollis Blanchard #include <asm/uaccess.h>
34d9fbd03dSHollis Blanchard #include <asm/kvm_ppc.h>
35d9fbd03dSHollis Blanchard #include <asm/cacheflush.h>
36d30f6e48SScott Wood #include <asm/dbell.h>
37d30f6e48SScott Wood #include <asm/hw_irq.h>
38d30f6e48SScott Wood #include <asm/irq.h>
39d9fbd03dSHollis Blanchard 
40d30f6e48SScott Wood #include "timing.h"
4175f74f0dSHollis Blanchard #include "booke.h"
4297c95059SAlexander Graf #include "trace.h"
43d9fbd03dSHollis Blanchard 
44d9fbd03dSHollis Blanchard unsigned long kvmppc_booke_handlers;
45d9fbd03dSHollis Blanchard 
46d9fbd03dSHollis Blanchard #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
47d9fbd03dSHollis Blanchard #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
48d9fbd03dSHollis Blanchard 
49d9fbd03dSHollis Blanchard struct kvm_stats_debugfs_item debugfs_entries[] = {
50d9fbd03dSHollis Blanchard 	{ "mmio",       VCPU_STAT(mmio_exits) },
51d9fbd03dSHollis Blanchard 	{ "dcr",        VCPU_STAT(dcr_exits) },
52d9fbd03dSHollis Blanchard 	{ "sig",        VCPU_STAT(signal_exits) },
53d9fbd03dSHollis Blanchard 	{ "itlb_r",     VCPU_STAT(itlb_real_miss_exits) },
54d9fbd03dSHollis Blanchard 	{ "itlb_v",     VCPU_STAT(itlb_virt_miss_exits) },
55d9fbd03dSHollis Blanchard 	{ "dtlb_r",     VCPU_STAT(dtlb_real_miss_exits) },
56d9fbd03dSHollis Blanchard 	{ "dtlb_v",     VCPU_STAT(dtlb_virt_miss_exits) },
57d9fbd03dSHollis Blanchard 	{ "sysc",       VCPU_STAT(syscall_exits) },
58d9fbd03dSHollis Blanchard 	{ "isi",        VCPU_STAT(isi_exits) },
59d9fbd03dSHollis Blanchard 	{ "dsi",        VCPU_STAT(dsi_exits) },
60d9fbd03dSHollis Blanchard 	{ "inst_emu",   VCPU_STAT(emulated_inst_exits) },
61d9fbd03dSHollis Blanchard 	{ "dec",        VCPU_STAT(dec_exits) },
62d9fbd03dSHollis Blanchard 	{ "ext_intr",   VCPU_STAT(ext_intr_exits) },
63d9fbd03dSHollis Blanchard 	{ "halt_wakeup", VCPU_STAT(halt_wakeup) },
64d30f6e48SScott Wood 	{ "doorbell", VCPU_STAT(dbell_exits) },
65d30f6e48SScott Wood 	{ "guest doorbell", VCPU_STAT(gdbell_exits) },
66cf1c5ca4SAlexander Graf 	{ "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
67d9fbd03dSHollis Blanchard 	{ NULL }
68d9fbd03dSHollis Blanchard };
69d9fbd03dSHollis Blanchard 
70d9fbd03dSHollis Blanchard /* TODO: use vcpu_printf() */
71d9fbd03dSHollis Blanchard void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
72d9fbd03dSHollis Blanchard {
73d9fbd03dSHollis Blanchard 	int i;
74d9fbd03dSHollis Blanchard 
75666e7252SAlexander Graf 	printk("pc:   %08lx msr:  %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
765cf8ca22SHollis Blanchard 	printk("lr:   %08lx ctr:  %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
77de7906c3SAlexander Graf 	printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
78de7906c3SAlexander Graf 					    vcpu->arch.shared->srr1);
79d9fbd03dSHollis Blanchard 
80d9fbd03dSHollis Blanchard 	printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
81d9fbd03dSHollis Blanchard 
82d9fbd03dSHollis Blanchard 	for (i = 0; i < 32; i += 4) {
835cf8ca22SHollis Blanchard 		printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
848e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i),
858e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i+1),
868e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i+2),
878e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i+3));
88d9fbd03dSHollis Blanchard 	}
89d9fbd03dSHollis Blanchard }
90d9fbd03dSHollis Blanchard 
914cd35f67SScott Wood #ifdef CONFIG_SPE
924cd35f67SScott Wood void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
934cd35f67SScott Wood {
944cd35f67SScott Wood 	preempt_disable();
954cd35f67SScott Wood 	enable_kernel_spe();
964cd35f67SScott Wood 	kvmppc_save_guest_spe(vcpu);
974cd35f67SScott Wood 	vcpu->arch.shadow_msr &= ~MSR_SPE;
984cd35f67SScott Wood 	preempt_enable();
994cd35f67SScott Wood }
1004cd35f67SScott Wood 
1014cd35f67SScott Wood static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
1024cd35f67SScott Wood {
1034cd35f67SScott Wood 	preempt_disable();
1044cd35f67SScott Wood 	enable_kernel_spe();
1054cd35f67SScott Wood 	kvmppc_load_guest_spe(vcpu);
1064cd35f67SScott Wood 	vcpu->arch.shadow_msr |= MSR_SPE;
1074cd35f67SScott Wood 	preempt_enable();
1084cd35f67SScott Wood }
1094cd35f67SScott Wood 
1104cd35f67SScott Wood static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
1114cd35f67SScott Wood {
1124cd35f67SScott Wood 	if (vcpu->arch.shared->msr & MSR_SPE) {
1134cd35f67SScott Wood 		if (!(vcpu->arch.shadow_msr & MSR_SPE))
1144cd35f67SScott Wood 			kvmppc_vcpu_enable_spe(vcpu);
1154cd35f67SScott Wood 	} else if (vcpu->arch.shadow_msr & MSR_SPE) {
1164cd35f67SScott Wood 		kvmppc_vcpu_disable_spe(vcpu);
1174cd35f67SScott Wood 	}
1184cd35f67SScott Wood }
1194cd35f67SScott Wood #else
1204cd35f67SScott Wood static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
1214cd35f67SScott Wood {
1224cd35f67SScott Wood }
1234cd35f67SScott Wood #endif
1244cd35f67SScott Wood 
125dd9ebf1fSLiu Yu /*
126dd9ebf1fSLiu Yu  * Helper function for "full" MSR writes.  No need to call this if only
127dd9ebf1fSLiu Yu  * EE/CE/ME/DE/RI are changing.
128dd9ebf1fSLiu Yu  */
1294cd35f67SScott Wood void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
1304cd35f67SScott Wood {
131dd9ebf1fSLiu Yu 	u32 old_msr = vcpu->arch.shared->msr;
1324cd35f67SScott Wood 
133d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
134d30f6e48SScott Wood 	new_msr |= MSR_GS;
135d30f6e48SScott Wood #endif
136d30f6e48SScott Wood 
1374cd35f67SScott Wood 	vcpu->arch.shared->msr = new_msr;
1384cd35f67SScott Wood 
139dd9ebf1fSLiu Yu 	kvmppc_mmu_msr_notify(vcpu, old_msr);
1404cd35f67SScott Wood 	kvmppc_vcpu_sync_spe(vcpu);
1414cd35f67SScott Wood }
1424cd35f67SScott Wood 
143d4cf3892SHollis Blanchard static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
144d4cf3892SHollis Blanchard                                        unsigned int priority)
1459dd921cfSHollis Blanchard {
1466346046cSAlexander Graf 	trace_kvm_booke_queue_irqprio(vcpu, priority);
1479dd921cfSHollis Blanchard 	set_bit(priority, &vcpu->arch.pending_exceptions);
1489dd921cfSHollis Blanchard }
1499dd921cfSHollis Blanchard 
150daf5e271SLiu Yu static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
151daf5e271SLiu Yu                                         ulong dear_flags, ulong esr_flags)
1529dd921cfSHollis Blanchard {
153daf5e271SLiu Yu 	vcpu->arch.queued_dear = dear_flags;
154daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
155daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
156daf5e271SLiu Yu }
157daf5e271SLiu Yu 
158daf5e271SLiu Yu static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
159daf5e271SLiu Yu                                            ulong dear_flags, ulong esr_flags)
160daf5e271SLiu Yu {
161daf5e271SLiu Yu 	vcpu->arch.queued_dear = dear_flags;
162daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
163daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
164daf5e271SLiu Yu }
165daf5e271SLiu Yu 
166daf5e271SLiu Yu static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
167daf5e271SLiu Yu                                            ulong esr_flags)
168daf5e271SLiu Yu {
169daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
170daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
171daf5e271SLiu Yu }
172daf5e271SLiu Yu 
173daf5e271SLiu Yu void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
174daf5e271SLiu Yu {
175daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
176d4cf3892SHollis Blanchard 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
1779dd921cfSHollis Blanchard }
1789dd921cfSHollis Blanchard 
1799dd921cfSHollis Blanchard void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
1809dd921cfSHollis Blanchard {
181d4cf3892SHollis Blanchard 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
1829dd921cfSHollis Blanchard }
1839dd921cfSHollis Blanchard 
1849dd921cfSHollis Blanchard int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
1859dd921cfSHollis Blanchard {
186d4cf3892SHollis Blanchard 	return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
1879dd921cfSHollis Blanchard }
1889dd921cfSHollis Blanchard 
1897706664dSAlexander Graf void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
1907706664dSAlexander Graf {
1917706664dSAlexander Graf 	clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
1927706664dSAlexander Graf }
1937706664dSAlexander Graf 
1949dd921cfSHollis Blanchard void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
1959dd921cfSHollis Blanchard                                 struct kvm_interrupt *irq)
1969dd921cfSHollis Blanchard {
197c5335f17SAlexander Graf 	unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
198c5335f17SAlexander Graf 
199c5335f17SAlexander Graf 	if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
200c5335f17SAlexander Graf 		prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
201c5335f17SAlexander Graf 
202c5335f17SAlexander Graf 	kvmppc_booke_queue_irqprio(vcpu, prio);
2039dd921cfSHollis Blanchard }
2049dd921cfSHollis Blanchard 
2054496f974SAlexander Graf void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
2064496f974SAlexander Graf                                   struct kvm_interrupt *irq)
2074496f974SAlexander Graf {
2084496f974SAlexander Graf 	clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
209c5335f17SAlexander Graf 	clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
2104496f974SAlexander Graf }
2114496f974SAlexander Graf 
212d30f6e48SScott Wood static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
213d30f6e48SScott Wood {
214d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
215d30f6e48SScott Wood 	mtspr(SPRN_GSRR0, srr0);
216d30f6e48SScott Wood 	mtspr(SPRN_GSRR1, srr1);
217d30f6e48SScott Wood #else
218d30f6e48SScott Wood 	vcpu->arch.shared->srr0 = srr0;
219d30f6e48SScott Wood 	vcpu->arch.shared->srr1 = srr1;
220d30f6e48SScott Wood #endif
221d30f6e48SScott Wood }
222d30f6e48SScott Wood 
223d30f6e48SScott Wood static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
224d30f6e48SScott Wood {
225d30f6e48SScott Wood 	vcpu->arch.csrr0 = srr0;
226d30f6e48SScott Wood 	vcpu->arch.csrr1 = srr1;
227d30f6e48SScott Wood }
228d30f6e48SScott Wood 
229d30f6e48SScott Wood static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
230d30f6e48SScott Wood {
231d30f6e48SScott Wood 	if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
232d30f6e48SScott Wood 		vcpu->arch.dsrr0 = srr0;
233d30f6e48SScott Wood 		vcpu->arch.dsrr1 = srr1;
234d30f6e48SScott Wood 	} else {
235d30f6e48SScott Wood 		set_guest_csrr(vcpu, srr0, srr1);
236d30f6e48SScott Wood 	}
237d30f6e48SScott Wood }
238d30f6e48SScott Wood 
239d30f6e48SScott Wood static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
240d30f6e48SScott Wood {
241d30f6e48SScott Wood 	vcpu->arch.mcsrr0 = srr0;
242d30f6e48SScott Wood 	vcpu->arch.mcsrr1 = srr1;
243d30f6e48SScott Wood }
244d30f6e48SScott Wood 
245d30f6e48SScott Wood static unsigned long get_guest_dear(struct kvm_vcpu *vcpu)
246d30f6e48SScott Wood {
247d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
248d30f6e48SScott Wood 	return mfspr(SPRN_GDEAR);
249d30f6e48SScott Wood #else
250d30f6e48SScott Wood 	return vcpu->arch.shared->dar;
251d30f6e48SScott Wood #endif
252d30f6e48SScott Wood }
253d30f6e48SScott Wood 
254d30f6e48SScott Wood static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear)
255d30f6e48SScott Wood {
256d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
257d30f6e48SScott Wood 	mtspr(SPRN_GDEAR, dear);
258d30f6e48SScott Wood #else
259d30f6e48SScott Wood 	vcpu->arch.shared->dar = dear;
260d30f6e48SScott Wood #endif
261d30f6e48SScott Wood }
262d30f6e48SScott Wood 
263d30f6e48SScott Wood static unsigned long get_guest_esr(struct kvm_vcpu *vcpu)
264d30f6e48SScott Wood {
265d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
266d30f6e48SScott Wood 	return mfspr(SPRN_GESR);
267d30f6e48SScott Wood #else
268d30f6e48SScott Wood 	return vcpu->arch.shared->esr;
269d30f6e48SScott Wood #endif
270d30f6e48SScott Wood }
271d30f6e48SScott Wood 
272d30f6e48SScott Wood static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr)
273d30f6e48SScott Wood {
274d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
275d30f6e48SScott Wood 	mtspr(SPRN_GESR, esr);
276d30f6e48SScott Wood #else
277d30f6e48SScott Wood 	vcpu->arch.shared->esr = esr;
278d30f6e48SScott Wood #endif
279d30f6e48SScott Wood }
280d30f6e48SScott Wood 
281d4cf3892SHollis Blanchard /* Deliver the interrupt of the corresponding priority, if possible. */
282d4cf3892SHollis Blanchard static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
283d4cf3892SHollis Blanchard                                         unsigned int priority)
284d9fbd03dSHollis Blanchard {
285d4cf3892SHollis Blanchard 	int allowed = 0;
28679300f8cSAlexander Graf 	ulong msr_mask = 0;
287daf5e271SLiu Yu 	bool update_esr = false, update_dear = false;
2885c6cedf4SAlexander Graf 	ulong crit_raw = vcpu->arch.shared->critical;
2895c6cedf4SAlexander Graf 	ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
2905c6cedf4SAlexander Graf 	bool crit;
291c5335f17SAlexander Graf 	bool keep_irq = false;
292d30f6e48SScott Wood 	enum int_class int_class;
2935c6cedf4SAlexander Graf 
2945c6cedf4SAlexander Graf 	/* Truncate crit indicators in 32 bit mode */
2955c6cedf4SAlexander Graf 	if (!(vcpu->arch.shared->msr & MSR_SF)) {
2965c6cedf4SAlexander Graf 		crit_raw &= 0xffffffff;
2975c6cedf4SAlexander Graf 		crit_r1 &= 0xffffffff;
2985c6cedf4SAlexander Graf 	}
2995c6cedf4SAlexander Graf 
3005c6cedf4SAlexander Graf 	/* Critical section when crit == r1 */
3015c6cedf4SAlexander Graf 	crit = (crit_raw == crit_r1);
3025c6cedf4SAlexander Graf 	/* ... and we're in supervisor mode */
3035c6cedf4SAlexander Graf 	crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
304d9fbd03dSHollis Blanchard 
305c5335f17SAlexander Graf 	if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
306c5335f17SAlexander Graf 		priority = BOOKE_IRQPRIO_EXTERNAL;
307c5335f17SAlexander Graf 		keep_irq = true;
308c5335f17SAlexander Graf 	}
309c5335f17SAlexander Graf 
310d4cf3892SHollis Blanchard 	switch (priority) {
311d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DTLB_MISS:
312daf5e271SLiu Yu 	case BOOKE_IRQPRIO_DATA_STORAGE:
313daf5e271SLiu Yu 		update_dear = true;
314daf5e271SLiu Yu 		/* fall through */
315daf5e271SLiu Yu 	case BOOKE_IRQPRIO_INST_STORAGE:
316daf5e271SLiu Yu 	case BOOKE_IRQPRIO_PROGRAM:
317daf5e271SLiu Yu 		update_esr = true;
318daf5e271SLiu Yu 		/* fall through */
319d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_ITLB_MISS:
320d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_SYSCALL:
321d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_FP_UNAVAIL:
322bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_UNAVAIL:
323bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_FP_DATA:
324bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_FP_ROUND:
325d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_AP_UNAVAIL:
326d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_ALIGNMENT:
327d4cf3892SHollis Blanchard 		allowed = 1;
32879300f8cSAlexander Graf 		msr_mask = MSR_CE | MSR_ME | MSR_DE;
329d30f6e48SScott Wood 		int_class = INT_CLASS_NONCRIT;
330d9fbd03dSHollis Blanchard 		break;
331d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_CRITICAL:
3324ab96919SAlexander Graf 	case BOOKE_IRQPRIO_DBELL_CRIT:
333666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_CE;
334d30f6e48SScott Wood 		allowed = allowed && !crit;
33579300f8cSAlexander Graf 		msr_mask = MSR_ME;
336d30f6e48SScott Wood 		int_class = INT_CLASS_CRIT;
337d9fbd03dSHollis Blanchard 		break;
338d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_MACHINE_CHECK:
339666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_ME;
340d30f6e48SScott Wood 		allowed = allowed && !crit;
341d30f6e48SScott Wood 		int_class = INT_CLASS_MC;
342d9fbd03dSHollis Blanchard 		break;
343d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DECREMENTER:
344d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_FIT:
345dfd4d47eSScott Wood 		keep_irq = true;
346dfd4d47eSScott Wood 		/* fall through */
347dfd4d47eSScott Wood 	case BOOKE_IRQPRIO_EXTERNAL:
3484ab96919SAlexander Graf 	case BOOKE_IRQPRIO_DBELL:
349666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_EE;
3505c6cedf4SAlexander Graf 		allowed = allowed && !crit;
35179300f8cSAlexander Graf 		msr_mask = MSR_CE | MSR_ME | MSR_DE;
352d30f6e48SScott Wood 		int_class = INT_CLASS_NONCRIT;
353d9fbd03dSHollis Blanchard 		break;
354d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DEBUG:
355666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_DE;
356d30f6e48SScott Wood 		allowed = allowed && !crit;
35779300f8cSAlexander Graf 		msr_mask = MSR_ME;
358d30f6e48SScott Wood 		int_class = INT_CLASS_CRIT;
359d9fbd03dSHollis Blanchard 		break;
360d9fbd03dSHollis Blanchard 	}
361d9fbd03dSHollis Blanchard 
362d4cf3892SHollis Blanchard 	if (allowed) {
363d30f6e48SScott Wood 		switch (int_class) {
364d30f6e48SScott Wood 		case INT_CLASS_NONCRIT:
365d30f6e48SScott Wood 			set_guest_srr(vcpu, vcpu->arch.pc,
366d30f6e48SScott Wood 				      vcpu->arch.shared->msr);
367d30f6e48SScott Wood 			break;
368d30f6e48SScott Wood 		case INT_CLASS_CRIT:
369d30f6e48SScott Wood 			set_guest_csrr(vcpu, vcpu->arch.pc,
370d30f6e48SScott Wood 				       vcpu->arch.shared->msr);
371d30f6e48SScott Wood 			break;
372d30f6e48SScott Wood 		case INT_CLASS_DBG:
373d30f6e48SScott Wood 			set_guest_dsrr(vcpu, vcpu->arch.pc,
374d30f6e48SScott Wood 				       vcpu->arch.shared->msr);
375d30f6e48SScott Wood 			break;
376d30f6e48SScott Wood 		case INT_CLASS_MC:
377d30f6e48SScott Wood 			set_guest_mcsrr(vcpu, vcpu->arch.pc,
378d30f6e48SScott Wood 					vcpu->arch.shared->msr);
379d30f6e48SScott Wood 			break;
380d30f6e48SScott Wood 		}
381d30f6e48SScott Wood 
382d4cf3892SHollis Blanchard 		vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
383daf5e271SLiu Yu 		if (update_esr == true)
384d30f6e48SScott Wood 			set_guest_esr(vcpu, vcpu->arch.queued_esr);
385daf5e271SLiu Yu 		if (update_dear == true)
386d30f6e48SScott Wood 			set_guest_dear(vcpu, vcpu->arch.queued_dear);
387666e7252SAlexander Graf 		kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
388d4cf3892SHollis Blanchard 
389c5335f17SAlexander Graf 		if (!keep_irq)
390d4cf3892SHollis Blanchard 			clear_bit(priority, &vcpu->arch.pending_exceptions);
391d4cf3892SHollis Blanchard 	}
392d4cf3892SHollis Blanchard 
393d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
394d30f6e48SScott Wood 	/*
395d30f6e48SScott Wood 	 * If an interrupt is pending but masked, raise a guest doorbell
396d30f6e48SScott Wood 	 * so that we are notified when the guest enables the relevant
397d30f6e48SScott Wood 	 * MSR bit.
398d30f6e48SScott Wood 	 */
399d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
400d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
401d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
402d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
403d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
404d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
405d30f6e48SScott Wood #endif
406d30f6e48SScott Wood 
407d4cf3892SHollis Blanchard 	return allowed;
408d9fbd03dSHollis Blanchard }
409d9fbd03dSHollis Blanchard 
410dfd4d47eSScott Wood static void update_timer_ints(struct kvm_vcpu *vcpu)
411dfd4d47eSScott Wood {
412dfd4d47eSScott Wood 	if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
413dfd4d47eSScott Wood 		kvmppc_core_queue_dec(vcpu);
414dfd4d47eSScott Wood 	else
415dfd4d47eSScott Wood 		kvmppc_core_dequeue_dec(vcpu);
416dfd4d47eSScott Wood }
417dfd4d47eSScott Wood 
418c59a6a3eSScott Wood static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
419d9fbd03dSHollis Blanchard {
420d9fbd03dSHollis Blanchard 	unsigned long *pending = &vcpu->arch.pending_exceptions;
421d9fbd03dSHollis Blanchard 	unsigned int priority;
422d9fbd03dSHollis Blanchard 
4239ab80843SHollis Blanchard 	priority = __ffs(*pending);
4248b3a00fcSAlexander Graf 	while (priority < BOOKE_IRQPRIO_MAX) {
425d4cf3892SHollis Blanchard 		if (kvmppc_booke_irqprio_deliver(vcpu, priority))
426d9fbd03dSHollis Blanchard 			break;
427d9fbd03dSHollis Blanchard 
428d9fbd03dSHollis Blanchard 		priority = find_next_bit(pending,
429d9fbd03dSHollis Blanchard 		                         BITS_PER_BYTE * sizeof(*pending),
430d9fbd03dSHollis Blanchard 		                         priority + 1);
431d9fbd03dSHollis Blanchard 	}
43290bba358SAlexander Graf 
43390bba358SAlexander Graf 	/* Tell the guest about our interrupt status */
43429ac26efSScott Wood 	vcpu->arch.shared->int_pending = !!*pending;
435d9fbd03dSHollis Blanchard }
436d9fbd03dSHollis Blanchard 
437c59a6a3eSScott Wood /* Check pending exceptions and deliver one, if possible. */
438a8e4ef84SAlexander Graf int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
439c59a6a3eSScott Wood {
440a8e4ef84SAlexander Graf 	int r = 0;
441c59a6a3eSScott Wood 	WARN_ON_ONCE(!irqs_disabled());
442c59a6a3eSScott Wood 
443c59a6a3eSScott Wood 	kvmppc_core_check_exceptions(vcpu);
444c59a6a3eSScott Wood 
445c59a6a3eSScott Wood 	if (vcpu->arch.shared->msr & MSR_WE) {
446c59a6a3eSScott Wood 		local_irq_enable();
447c59a6a3eSScott Wood 		kvm_vcpu_block(vcpu);
448966cd0f3SAlexander Graf 		clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
449c59a6a3eSScott Wood 		local_irq_disable();
450c59a6a3eSScott Wood 
451c59a6a3eSScott Wood 		kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
452a8e4ef84SAlexander Graf 		r = 1;
453c59a6a3eSScott Wood 	};
454a8e4ef84SAlexander Graf 
455a8e4ef84SAlexander Graf 	return r;
456a8e4ef84SAlexander Graf }
457a8e4ef84SAlexander Graf 
458*7c973a2eSAlexander Graf int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
4594ffc6356SAlexander Graf {
460*7c973a2eSAlexander Graf 	int r = 1; /* Indicate we want to get back into the guest */
461*7c973a2eSAlexander Graf 
4624ffc6356SAlexander Graf 	if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
4634ffc6356SAlexander Graf 		update_timer_ints(vcpu);
464862d31f7SAlexander Graf #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
465862d31f7SAlexander Graf 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
466862d31f7SAlexander Graf 		kvmppc_core_flush_tlb(vcpu);
467862d31f7SAlexander Graf #endif
468*7c973a2eSAlexander Graf 
469*7c973a2eSAlexander Graf 	return r;
4704ffc6356SAlexander Graf }
4714ffc6356SAlexander Graf 
472df6909e5SPaul Mackerras int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
473df6909e5SPaul Mackerras {
4747ee78855SAlexander Graf 	int ret, s;
4758fae845fSScott Wood #ifdef CONFIG_PPC_FPU
4768fae845fSScott Wood 	unsigned int fpscr;
4778fae845fSScott Wood 	int fpexc_mode;
4788fae845fSScott Wood 	u64 fpr[32];
4798fae845fSScott Wood #endif
480df6909e5SPaul Mackerras 
481af8f38b3SAlexander Graf 	if (!vcpu->arch.sane) {
482af8f38b3SAlexander Graf 		kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
483af8f38b3SAlexander Graf 		return -EINVAL;
484af8f38b3SAlexander Graf 	}
485af8f38b3SAlexander Graf 
486df6909e5SPaul Mackerras 	local_irq_disable();
4877ee78855SAlexander Graf 	s = kvmppc_prepare_to_enter(vcpu);
4887ee78855SAlexander Graf 	if (s <= 0) {
48924afa37bSAlexander Graf 		local_irq_enable();
4907ee78855SAlexander Graf 		ret = s;
4911d1ef222SScott Wood 		goto out;
4921d1ef222SScott Wood 	}
493bd2be683SAlexander Graf 	kvmppc_lazy_ee_enable();
4941d1ef222SScott Wood 
495df6909e5SPaul Mackerras 	kvm_guest_enter();
4968fae845fSScott Wood 
4978fae845fSScott Wood #ifdef CONFIG_PPC_FPU
4988fae845fSScott Wood 	/* Save userspace FPU state in stack */
4998fae845fSScott Wood 	enable_kernel_fp();
5008fae845fSScott Wood 	memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
5018fae845fSScott Wood 	fpscr = current->thread.fpscr.val;
5028fae845fSScott Wood 	fpexc_mode = current->thread.fpexc_mode;
5038fae845fSScott Wood 
5048fae845fSScott Wood 	/* Restore guest FPU state to thread */
5058fae845fSScott Wood 	memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr));
5068fae845fSScott Wood 	current->thread.fpscr.val = vcpu->arch.fpscr;
5078fae845fSScott Wood 
5088fae845fSScott Wood 	/*
5098fae845fSScott Wood 	 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
5108fae845fSScott Wood 	 * as always using the FPU.  Kernel usage of FP (via
5118fae845fSScott Wood 	 * enable_kernel_fp()) in this thread must not occur while
5128fae845fSScott Wood 	 * vcpu->fpu_active is set.
5138fae845fSScott Wood 	 */
5148fae845fSScott Wood 	vcpu->fpu_active = 1;
5158fae845fSScott Wood 
5168fae845fSScott Wood 	kvmppc_load_guest_fp(vcpu);
5178fae845fSScott Wood #endif
5188fae845fSScott Wood 
519df6909e5SPaul Mackerras 	ret = __kvmppc_vcpu_run(kvm_run, vcpu);
5208fae845fSScott Wood 
52124afa37bSAlexander Graf 	/* No need for kvm_guest_exit. It's done in handle_exit.
52224afa37bSAlexander Graf 	   We also get here with interrupts enabled. */
52324afa37bSAlexander Graf 
5248fae845fSScott Wood #ifdef CONFIG_PPC_FPU
5258fae845fSScott Wood 	kvmppc_save_guest_fp(vcpu);
5268fae845fSScott Wood 
5278fae845fSScott Wood 	vcpu->fpu_active = 0;
5288fae845fSScott Wood 
5298fae845fSScott Wood 	/* Save guest FPU state from thread */
5308fae845fSScott Wood 	memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr));
5318fae845fSScott Wood 	vcpu->arch.fpscr = current->thread.fpscr.val;
5328fae845fSScott Wood 
5338fae845fSScott Wood 	/* Restore userspace FPU state from stack */
5348fae845fSScott Wood 	memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
5358fae845fSScott Wood 	current->thread.fpscr.val = fpscr;
5368fae845fSScott Wood 	current->thread.fpexc_mode = fpexc_mode;
5378fae845fSScott Wood #endif
5388fae845fSScott Wood 
5391d1ef222SScott Wood out:
540d69c6436SAlexander Graf 	vcpu->mode = OUTSIDE_GUEST_MODE;
541d69c6436SAlexander Graf 	smp_wmb();
542df6909e5SPaul Mackerras 	return ret;
543df6909e5SPaul Mackerras }
544df6909e5SPaul Mackerras 
545d30f6e48SScott Wood static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
546d9fbd03dSHollis Blanchard {
547d9fbd03dSHollis Blanchard 	enum emulation_result er;
548d9fbd03dSHollis Blanchard 
549d9fbd03dSHollis Blanchard 	er = kvmppc_emulate_instruction(run, vcpu);
550d9fbd03dSHollis Blanchard 	switch (er) {
551d9fbd03dSHollis Blanchard 	case EMULATE_DONE:
55273e75b41SHollis Blanchard 		/* don't overwrite subtypes, just account kvm_stats */
5537b701591SHollis Blanchard 		kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
554d9fbd03dSHollis Blanchard 		/* Future optimization: only reload non-volatiles if
555d9fbd03dSHollis Blanchard 		 * they were actually modified by emulation. */
556d30f6e48SScott Wood 		return RESUME_GUEST_NV;
557d30f6e48SScott Wood 
558d9fbd03dSHollis Blanchard 	case EMULATE_DO_DCR:
559d9fbd03dSHollis Blanchard 		run->exit_reason = KVM_EXIT_DCR;
560d30f6e48SScott Wood 		return RESUME_HOST;
561d30f6e48SScott Wood 
562d9fbd03dSHollis Blanchard 	case EMULATE_FAIL:
5635cf8ca22SHollis Blanchard 		printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
564d9fbd03dSHollis Blanchard 		       __func__, vcpu->arch.pc, vcpu->arch.last_inst);
565d9fbd03dSHollis Blanchard 		/* For debugging, encode the failing instruction and
566d9fbd03dSHollis Blanchard 		 * report it to userspace. */
567d9fbd03dSHollis Blanchard 		run->hw.hardware_exit_reason = ~0ULL << 32;
568d9fbd03dSHollis Blanchard 		run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
569d1ff5499SAlexander Graf 		kvmppc_core_queue_program(vcpu, ESR_PIL);
570d30f6e48SScott Wood 		return RESUME_HOST;
571d30f6e48SScott Wood 
572d9fbd03dSHollis Blanchard 	default:
573d9fbd03dSHollis Blanchard 		BUG();
574d9fbd03dSHollis Blanchard 	}
575d30f6e48SScott Wood }
576d30f6e48SScott Wood 
5774e642ccbSAlexander Graf static void kvmppc_fill_pt_regs(struct pt_regs *regs)
5784e642ccbSAlexander Graf {
5794e642ccbSAlexander Graf 	ulong r1, ip, msr, lr;
5804e642ccbSAlexander Graf 
5814e642ccbSAlexander Graf 	asm("mr %0, 1" : "=r"(r1));
5824e642ccbSAlexander Graf 	asm("mflr %0" : "=r"(lr));
5834e642ccbSAlexander Graf 	asm("mfmsr %0" : "=r"(msr));
5844e642ccbSAlexander Graf 	asm("bl 1f; 1: mflr %0" : "=r"(ip));
5854e642ccbSAlexander Graf 
5864e642ccbSAlexander Graf 	memset(regs, 0, sizeof(*regs));
5874e642ccbSAlexander Graf 	regs->gpr[1] = r1;
5884e642ccbSAlexander Graf 	regs->nip = ip;
5894e642ccbSAlexander Graf 	regs->msr = msr;
5904e642ccbSAlexander Graf 	regs->link = lr;
5914e642ccbSAlexander Graf }
5924e642ccbSAlexander Graf 
5936328e593SBharat Bhushan /*
5946328e593SBharat Bhushan  * For interrupts needed to be handled by host interrupt handlers,
5956328e593SBharat Bhushan  * corresponding host handler are called from here in similar way
5966328e593SBharat Bhushan  * (but not exact) as they are called from low level handler
5976328e593SBharat Bhushan  * (such as from arch/powerpc/kernel/head_fsl_booke.S).
5986328e593SBharat Bhushan  */
5994e642ccbSAlexander Graf static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
6004e642ccbSAlexander Graf 				     unsigned int exit_nr)
6014e642ccbSAlexander Graf {
6024e642ccbSAlexander Graf 	struct pt_regs regs;
6034e642ccbSAlexander Graf 
6044e642ccbSAlexander Graf 	switch (exit_nr) {
6054e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_EXTERNAL:
6064e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
6074e642ccbSAlexander Graf 		do_IRQ(&regs);
6084e642ccbSAlexander Graf 		break;
6094e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_DECREMENTER:
6104e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
6114e642ccbSAlexander Graf 		timer_interrupt(&regs);
6124e642ccbSAlexander Graf 		break;
6134e642ccbSAlexander Graf #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64)
6144e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_DOORBELL:
6154e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
6164e642ccbSAlexander Graf 		doorbell_exception(&regs);
6174e642ccbSAlexander Graf 		break;
6184e642ccbSAlexander Graf #endif
6194e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_MACHINE_CHECK:
6204e642ccbSAlexander Graf 		/* FIXME */
6214e642ccbSAlexander Graf 		break;
6227cc1e8eeSAlexander Graf 	case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
6237cc1e8eeSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
6247cc1e8eeSAlexander Graf 		performance_monitor_exception(&regs);
6257cc1e8eeSAlexander Graf 		break;
6266328e593SBharat Bhushan 	case BOOKE_INTERRUPT_WATCHDOG:
6276328e593SBharat Bhushan 		kvmppc_fill_pt_regs(&regs);
6286328e593SBharat Bhushan #ifdef CONFIG_BOOKE_WDT
6296328e593SBharat Bhushan 		WatchdogException(&regs);
6306328e593SBharat Bhushan #else
6316328e593SBharat Bhushan 		unknown_exception(&regs);
6326328e593SBharat Bhushan #endif
6336328e593SBharat Bhushan 		break;
6346328e593SBharat Bhushan 	case BOOKE_INTERRUPT_CRITICAL:
6356328e593SBharat Bhushan 		unknown_exception(&regs);
6366328e593SBharat Bhushan 		break;
6374e642ccbSAlexander Graf 	}
6384e642ccbSAlexander Graf }
6394e642ccbSAlexander Graf 
640d30f6e48SScott Wood /**
641d30f6e48SScott Wood  * kvmppc_handle_exit
642d30f6e48SScott Wood  *
643d30f6e48SScott Wood  * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
644d30f6e48SScott Wood  */
645d30f6e48SScott Wood int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
646d30f6e48SScott Wood                        unsigned int exit_nr)
647d30f6e48SScott Wood {
648d30f6e48SScott Wood 	int r = RESUME_HOST;
6497ee78855SAlexander Graf 	int s;
650d30f6e48SScott Wood 
651d30f6e48SScott Wood 	/* update before a new last_exit_type is rewritten */
652d30f6e48SScott Wood 	kvmppc_update_timing_stats(vcpu);
653d30f6e48SScott Wood 
6544e642ccbSAlexander Graf 	/* restart interrupts if they were meant for the host */
6554e642ccbSAlexander Graf 	kvmppc_restart_interrupt(vcpu, exit_nr);
656d30f6e48SScott Wood 
657d30f6e48SScott Wood 	local_irq_enable();
658d30f6e48SScott Wood 
65997c95059SAlexander Graf 	trace_kvm_exit(exit_nr, vcpu);
660706fb730SAlexander Graf 	kvm_guest_exit();
66197c95059SAlexander Graf 
662d30f6e48SScott Wood 	run->exit_reason = KVM_EXIT_UNKNOWN;
663d30f6e48SScott Wood 	run->ready_for_interrupt_injection = 1;
664d30f6e48SScott Wood 
665d30f6e48SScott Wood 	switch (exit_nr) {
666d30f6e48SScott Wood 	case BOOKE_INTERRUPT_MACHINE_CHECK:
667c35c9d84SAlexander Graf 		printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
668c35c9d84SAlexander Graf 		kvmppc_dump_vcpu(vcpu);
669c35c9d84SAlexander Graf 		/* For debugging, send invalid exit reason to user space */
670c35c9d84SAlexander Graf 		run->hw.hardware_exit_reason = ~1ULL << 32;
671c35c9d84SAlexander Graf 		run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
672c35c9d84SAlexander Graf 		r = RESUME_HOST;
673d30f6e48SScott Wood 		break;
674d30f6e48SScott Wood 
675d30f6e48SScott Wood 	case BOOKE_INTERRUPT_EXTERNAL:
676d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
677d30f6e48SScott Wood 		r = RESUME_GUEST;
678d30f6e48SScott Wood 		break;
679d30f6e48SScott Wood 
680d30f6e48SScott Wood 	case BOOKE_INTERRUPT_DECREMENTER:
681d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, DEC_EXITS);
682d30f6e48SScott Wood 		r = RESUME_GUEST;
683d30f6e48SScott Wood 		break;
684d30f6e48SScott Wood 
6856328e593SBharat Bhushan 	case BOOKE_INTERRUPT_WATCHDOG:
6866328e593SBharat Bhushan 		r = RESUME_GUEST;
6876328e593SBharat Bhushan 		break;
6886328e593SBharat Bhushan 
689d30f6e48SScott Wood 	case BOOKE_INTERRUPT_DOORBELL:
690d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, DBELL_EXITS);
691d30f6e48SScott Wood 		r = RESUME_GUEST;
692d30f6e48SScott Wood 		break;
693d30f6e48SScott Wood 
694d30f6e48SScott Wood 	case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
695d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, GDBELL_EXITS);
696d30f6e48SScott Wood 
697d30f6e48SScott Wood 		/*
698d30f6e48SScott Wood 		 * We are here because there is a pending guest interrupt
699d30f6e48SScott Wood 		 * which could not be delivered as MSR_CE or MSR_ME was not
700d30f6e48SScott Wood 		 * set.  Once we break from here we will retry delivery.
701d30f6e48SScott Wood 		 */
702d30f6e48SScott Wood 		r = RESUME_GUEST;
703d30f6e48SScott Wood 		break;
704d30f6e48SScott Wood 
705d30f6e48SScott Wood 	case BOOKE_INTERRUPT_GUEST_DBELL:
706d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, GDBELL_EXITS);
707d30f6e48SScott Wood 
708d30f6e48SScott Wood 		/*
709d30f6e48SScott Wood 		 * We are here because there is a pending guest interrupt
710d30f6e48SScott Wood 		 * which could not be delivered as MSR_EE was not set.  Once
711d30f6e48SScott Wood 		 * we break from here we will retry delivery.
712d30f6e48SScott Wood 		 */
713d30f6e48SScott Wood 		r = RESUME_GUEST;
714d30f6e48SScott Wood 		break;
715d30f6e48SScott Wood 
71695f2e921SAlexander Graf 	case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
71795f2e921SAlexander Graf 		r = RESUME_GUEST;
71895f2e921SAlexander Graf 		break;
71995f2e921SAlexander Graf 
720d30f6e48SScott Wood 	case BOOKE_INTERRUPT_HV_PRIV:
721d30f6e48SScott Wood 		r = emulation_exit(run, vcpu);
722d30f6e48SScott Wood 		break;
723d30f6e48SScott Wood 
724d30f6e48SScott Wood 	case BOOKE_INTERRUPT_PROGRAM:
725d30f6e48SScott Wood 		if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
7260268597cSAlexander Graf 			/*
7270268597cSAlexander Graf 			 * Program traps generated by user-level software must
7280268597cSAlexander Graf 			 * be handled by the guest kernel.
7290268597cSAlexander Graf 			 *
7300268597cSAlexander Graf 			 * In GS mode, hypervisor privileged instructions trap
7310268597cSAlexander Graf 			 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
7320268597cSAlexander Graf 			 * actual program interrupts, handled by the guest.
7330268597cSAlexander Graf 			 */
734d30f6e48SScott Wood 			kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
735d30f6e48SScott Wood 			r = RESUME_GUEST;
736d30f6e48SScott Wood 			kvmppc_account_exit(vcpu, USR_PR_INST);
737d30f6e48SScott Wood 			break;
738d30f6e48SScott Wood 		}
739d30f6e48SScott Wood 
740d30f6e48SScott Wood 		r = emulation_exit(run, vcpu);
741d9fbd03dSHollis Blanchard 		break;
742d9fbd03dSHollis Blanchard 
743d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_FP_UNAVAIL:
744d4cf3892SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
7457b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, FP_UNAVAIL);
746d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
747d9fbd03dSHollis Blanchard 		break;
748d9fbd03dSHollis Blanchard 
7494cd35f67SScott Wood #ifdef CONFIG_SPE
7504cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_UNAVAIL: {
7514cd35f67SScott Wood 		if (vcpu->arch.shared->msr & MSR_SPE)
7524cd35f67SScott Wood 			kvmppc_vcpu_enable_spe(vcpu);
7534cd35f67SScott Wood 		else
7544cd35f67SScott Wood 			kvmppc_booke_queue_irqprio(vcpu,
7554cd35f67SScott Wood 						   BOOKE_IRQPRIO_SPE_UNAVAIL);
756bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
757bb3a8a17SHollis Blanchard 		break;
7584cd35f67SScott Wood 	}
759bb3a8a17SHollis Blanchard 
760bb3a8a17SHollis Blanchard 	case BOOKE_INTERRUPT_SPE_FP_DATA:
761bb3a8a17SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
762bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
763bb3a8a17SHollis Blanchard 		break;
764bb3a8a17SHollis Blanchard 
765bb3a8a17SHollis Blanchard 	case BOOKE_INTERRUPT_SPE_FP_ROUND:
766bb3a8a17SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
767bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
768bb3a8a17SHollis Blanchard 		break;
7694cd35f67SScott Wood #else
7704cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_UNAVAIL:
7714cd35f67SScott Wood 		/*
7724cd35f67SScott Wood 		 * Guest wants SPE, but host kernel doesn't support it.  Send
7734cd35f67SScott Wood 		 * an "unimplemented operation" program check to the guest.
7744cd35f67SScott Wood 		 */
7754cd35f67SScott Wood 		kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
7764cd35f67SScott Wood 		r = RESUME_GUEST;
7774cd35f67SScott Wood 		break;
7784cd35f67SScott Wood 
7794cd35f67SScott Wood 	/*
7804cd35f67SScott Wood 	 * These really should never happen without CONFIG_SPE,
7814cd35f67SScott Wood 	 * as we should never enable the real MSR[SPE] in the guest.
7824cd35f67SScott Wood 	 */
7834cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_FP_DATA:
7844cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_FP_ROUND:
7854cd35f67SScott Wood 		printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
7864cd35f67SScott Wood 		       __func__, exit_nr, vcpu->arch.pc);
7874cd35f67SScott Wood 		run->hw.hardware_exit_reason = exit_nr;
7884cd35f67SScott Wood 		r = RESUME_HOST;
7894cd35f67SScott Wood 		break;
7904cd35f67SScott Wood #endif
791bb3a8a17SHollis Blanchard 
792d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DATA_STORAGE:
793daf5e271SLiu Yu 		kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
794daf5e271SLiu Yu 		                               vcpu->arch.fault_esr);
7957b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, DSI_EXITS);
796d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
797d9fbd03dSHollis Blanchard 		break;
798d9fbd03dSHollis Blanchard 
799d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_INST_STORAGE:
800daf5e271SLiu Yu 		kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
8017b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, ISI_EXITS);
802d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
803d9fbd03dSHollis Blanchard 		break;
804d9fbd03dSHollis Blanchard 
805d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
806d30f6e48SScott Wood 	case BOOKE_INTERRUPT_HV_SYSCALL:
807d30f6e48SScott Wood 		if (!(vcpu->arch.shared->msr & MSR_PR)) {
808d30f6e48SScott Wood 			kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
809d30f6e48SScott Wood 		} else {
810d30f6e48SScott Wood 			/*
811d30f6e48SScott Wood 			 * hcall from guest userspace -- send privileged
812d30f6e48SScott Wood 			 * instruction program check.
813d30f6e48SScott Wood 			 */
814d30f6e48SScott Wood 			kvmppc_core_queue_program(vcpu, ESR_PPR);
815d30f6e48SScott Wood 		}
816d30f6e48SScott Wood 
817d30f6e48SScott Wood 		r = RESUME_GUEST;
818d30f6e48SScott Wood 		break;
819d30f6e48SScott Wood #else
820d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_SYSCALL:
8212a342ed5SAlexander Graf 		if (!(vcpu->arch.shared->msr & MSR_PR) &&
8222a342ed5SAlexander Graf 		    (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
8232a342ed5SAlexander Graf 			/* KVM PV hypercalls */
8242a342ed5SAlexander Graf 			kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
8252a342ed5SAlexander Graf 			r = RESUME_GUEST;
8262a342ed5SAlexander Graf 		} else {
8272a342ed5SAlexander Graf 			/* Guest syscalls */
828d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
8292a342ed5SAlexander Graf 		}
8307b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, SYSCALL_EXITS);
831d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
832d9fbd03dSHollis Blanchard 		break;
833d30f6e48SScott Wood #endif
834d9fbd03dSHollis Blanchard 
835d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DTLB_MISS: {
836d9fbd03dSHollis Blanchard 		unsigned long eaddr = vcpu->arch.fault_dear;
8377924bd41SHollis Blanchard 		int gtlb_index;
838475e7cddSHollis Blanchard 		gpa_t gpaddr;
839d9fbd03dSHollis Blanchard 		gfn_t gfn;
840d9fbd03dSHollis Blanchard 
841bf7ca4bdSAlexander Graf #ifdef CONFIG_KVM_E500V2
842a4cd8b23SScott Wood 		if (!(vcpu->arch.shared->msr & MSR_PR) &&
843a4cd8b23SScott Wood 		    (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
844a4cd8b23SScott Wood 			kvmppc_map_magic(vcpu);
845a4cd8b23SScott Wood 			kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
846a4cd8b23SScott Wood 			r = RESUME_GUEST;
847a4cd8b23SScott Wood 
848a4cd8b23SScott Wood 			break;
849a4cd8b23SScott Wood 		}
850a4cd8b23SScott Wood #endif
851a4cd8b23SScott Wood 
852d9fbd03dSHollis Blanchard 		/* Check the guest TLB. */
853fa86b8ddSHollis Blanchard 		gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
8547924bd41SHollis Blanchard 		if (gtlb_index < 0) {
855d9fbd03dSHollis Blanchard 			/* The guest didn't have a mapping for it. */
856daf5e271SLiu Yu 			kvmppc_core_queue_dtlb_miss(vcpu,
857daf5e271SLiu Yu 			                            vcpu->arch.fault_dear,
858daf5e271SLiu Yu 			                            vcpu->arch.fault_esr);
859b52a638cSHollis Blanchard 			kvmppc_mmu_dtlb_miss(vcpu);
8607b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
861d9fbd03dSHollis Blanchard 			r = RESUME_GUEST;
862d9fbd03dSHollis Blanchard 			break;
863d9fbd03dSHollis Blanchard 		}
864d9fbd03dSHollis Blanchard 
865be8d1caeSHollis Blanchard 		gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
866475e7cddSHollis Blanchard 		gfn = gpaddr >> PAGE_SHIFT;
867d9fbd03dSHollis Blanchard 
868d9fbd03dSHollis Blanchard 		if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
869d9fbd03dSHollis Blanchard 			/* The guest TLB had a mapping, but the shadow TLB
870d9fbd03dSHollis Blanchard 			 * didn't, and it is RAM. This could be because:
871d9fbd03dSHollis Blanchard 			 * a) the entry is mapping the host kernel, or
872d9fbd03dSHollis Blanchard 			 * b) the guest used a large mapping which we're faking
873d9fbd03dSHollis Blanchard 			 * Either way, we need to satisfy the fault without
874d9fbd03dSHollis Blanchard 			 * invoking the guest. */
87558a96214SHollis Blanchard 			kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
8767b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
877d9fbd03dSHollis Blanchard 			r = RESUME_GUEST;
878d9fbd03dSHollis Blanchard 		} else {
879d9fbd03dSHollis Blanchard 			/* Guest has mapped and accessed a page which is not
880d9fbd03dSHollis Blanchard 			 * actually RAM. */
881475e7cddSHollis Blanchard 			vcpu->arch.paddr_accessed = gpaddr;
8826020c0f6SAlexander Graf 			vcpu->arch.vaddr_accessed = eaddr;
883d9fbd03dSHollis Blanchard 			r = kvmppc_emulate_mmio(run, vcpu);
8847b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, MMIO_EXITS);
885d9fbd03dSHollis Blanchard 		}
886d9fbd03dSHollis Blanchard 
887d9fbd03dSHollis Blanchard 		break;
888d9fbd03dSHollis Blanchard 	}
889d9fbd03dSHollis Blanchard 
890d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_ITLB_MISS: {
891d9fbd03dSHollis Blanchard 		unsigned long eaddr = vcpu->arch.pc;
89289168618SHollis Blanchard 		gpa_t gpaddr;
893d9fbd03dSHollis Blanchard 		gfn_t gfn;
8947924bd41SHollis Blanchard 		int gtlb_index;
895d9fbd03dSHollis Blanchard 
896d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
897d9fbd03dSHollis Blanchard 
898d9fbd03dSHollis Blanchard 		/* Check the guest TLB. */
899fa86b8ddSHollis Blanchard 		gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
9007924bd41SHollis Blanchard 		if (gtlb_index < 0) {
901d9fbd03dSHollis Blanchard 			/* The guest didn't have a mapping for it. */
902d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
903b52a638cSHollis Blanchard 			kvmppc_mmu_itlb_miss(vcpu);
9047b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
905d9fbd03dSHollis Blanchard 			break;
906d9fbd03dSHollis Blanchard 		}
907d9fbd03dSHollis Blanchard 
9087b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
909d9fbd03dSHollis Blanchard 
910be8d1caeSHollis Blanchard 		gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
91189168618SHollis Blanchard 		gfn = gpaddr >> PAGE_SHIFT;
912d9fbd03dSHollis Blanchard 
913d9fbd03dSHollis Blanchard 		if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
914d9fbd03dSHollis Blanchard 			/* The guest TLB had a mapping, but the shadow TLB
915d9fbd03dSHollis Blanchard 			 * didn't. This could be because:
916d9fbd03dSHollis Blanchard 			 * a) the entry is mapping the host kernel, or
917d9fbd03dSHollis Blanchard 			 * b) the guest used a large mapping which we're faking
918d9fbd03dSHollis Blanchard 			 * Either way, we need to satisfy the fault without
919d9fbd03dSHollis Blanchard 			 * invoking the guest. */
92058a96214SHollis Blanchard 			kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
921d9fbd03dSHollis Blanchard 		} else {
922d9fbd03dSHollis Blanchard 			/* Guest mapped and leaped at non-RAM! */
923d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
924d9fbd03dSHollis Blanchard 		}
925d9fbd03dSHollis Blanchard 
926d9fbd03dSHollis Blanchard 		break;
927d9fbd03dSHollis Blanchard 	}
928d9fbd03dSHollis Blanchard 
929d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DEBUG: {
930d9fbd03dSHollis Blanchard 		u32 dbsr;
931d9fbd03dSHollis Blanchard 
932d9fbd03dSHollis Blanchard 		vcpu->arch.pc = mfspr(SPRN_CSRR0);
933d9fbd03dSHollis Blanchard 
934d9fbd03dSHollis Blanchard 		/* clear IAC events in DBSR register */
935d9fbd03dSHollis Blanchard 		dbsr = mfspr(SPRN_DBSR);
936d9fbd03dSHollis Blanchard 		dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
937d9fbd03dSHollis Blanchard 		mtspr(SPRN_DBSR, dbsr);
938d9fbd03dSHollis Blanchard 
939d9fbd03dSHollis Blanchard 		run->exit_reason = KVM_EXIT_DEBUG;
9407b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, DEBUG_EXITS);
941d9fbd03dSHollis Blanchard 		r = RESUME_HOST;
942d9fbd03dSHollis Blanchard 		break;
943d9fbd03dSHollis Blanchard 	}
944d9fbd03dSHollis Blanchard 
945d9fbd03dSHollis Blanchard 	default:
946d9fbd03dSHollis Blanchard 		printk(KERN_EMERG "exit_nr %d\n", exit_nr);
947d9fbd03dSHollis Blanchard 		BUG();
948d9fbd03dSHollis Blanchard 	}
949d9fbd03dSHollis Blanchard 
950a8e4ef84SAlexander Graf 	/*
951a8e4ef84SAlexander Graf 	 * To avoid clobbering exit_reason, only check for signals if we
952a8e4ef84SAlexander Graf 	 * aren't already exiting to userspace for some other reason.
953a8e4ef84SAlexander Graf 	 */
95403660ba2SAlexander Graf 	if (!(r & RESUME_HOST)) {
955d9fbd03dSHollis Blanchard 		local_irq_disable();
9567ee78855SAlexander Graf 		s = kvmppc_prepare_to_enter(vcpu);
9577ee78855SAlexander Graf 		if (s <= 0) {
95824afa37bSAlexander Graf 			local_irq_enable();
9597ee78855SAlexander Graf 			r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
96024afa37bSAlexander Graf 		} else {
961bd2be683SAlexander Graf 			kvmppc_lazy_ee_enable();
96224afa37bSAlexander Graf 		}
96324afa37bSAlexander Graf 	}
964706fb730SAlexander Graf 
965d9fbd03dSHollis Blanchard 	return r;
966d9fbd03dSHollis Blanchard }
967d9fbd03dSHollis Blanchard 
968d9fbd03dSHollis Blanchard /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
969d9fbd03dSHollis Blanchard int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
970d9fbd03dSHollis Blanchard {
971082decf2SHollis Blanchard 	int i;
972af8f38b3SAlexander Graf 	int r;
973082decf2SHollis Blanchard 
974d9fbd03dSHollis Blanchard 	vcpu->arch.pc = 0;
975b5904972SScott Wood 	vcpu->arch.shared->pir = vcpu->vcpu_id;
9768e5b26b5SAlexander Graf 	kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
977d30f6e48SScott Wood 	kvmppc_set_msr(vcpu, 0);
978d9fbd03dSHollis Blanchard 
979d30f6e48SScott Wood #ifndef CONFIG_KVM_BOOKE_HV
980d30f6e48SScott Wood 	vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
981d9fbd03dSHollis Blanchard 	vcpu->arch.shadow_pid = 1;
982d30f6e48SScott Wood 	vcpu->arch.shared->msr = 0;
983d30f6e48SScott Wood #endif
984d9fbd03dSHollis Blanchard 
985082decf2SHollis Blanchard 	/* Eye-catching numbers so we know if the guest takes an interrupt
986082decf2SHollis Blanchard 	 * before it's programmed its own IVPR/IVORs. */
987d9fbd03dSHollis Blanchard 	vcpu->arch.ivpr = 0x55550000;
988082decf2SHollis Blanchard 	for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
989082decf2SHollis Blanchard 		vcpu->arch.ivor[i] = 0x7700 | i * 4;
990d9fbd03dSHollis Blanchard 
99173e75b41SHollis Blanchard 	kvmppc_init_timing_stats(vcpu);
99273e75b41SHollis Blanchard 
993af8f38b3SAlexander Graf 	r = kvmppc_core_vcpu_setup(vcpu);
994af8f38b3SAlexander Graf 	kvmppc_sanity_check(vcpu);
995af8f38b3SAlexander Graf 	return r;
996d9fbd03dSHollis Blanchard }
997d9fbd03dSHollis Blanchard 
998d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
999d9fbd03dSHollis Blanchard {
1000d9fbd03dSHollis Blanchard 	int i;
1001d9fbd03dSHollis Blanchard 
1002d9fbd03dSHollis Blanchard 	regs->pc = vcpu->arch.pc;
1003992b5b29SAlexander Graf 	regs->cr = kvmppc_get_cr(vcpu);
1004d9fbd03dSHollis Blanchard 	regs->ctr = vcpu->arch.ctr;
1005d9fbd03dSHollis Blanchard 	regs->lr = vcpu->arch.lr;
1006992b5b29SAlexander Graf 	regs->xer = kvmppc_get_xer(vcpu);
1007666e7252SAlexander Graf 	regs->msr = vcpu->arch.shared->msr;
1008de7906c3SAlexander Graf 	regs->srr0 = vcpu->arch.shared->srr0;
1009de7906c3SAlexander Graf 	regs->srr1 = vcpu->arch.shared->srr1;
1010d9fbd03dSHollis Blanchard 	regs->pid = vcpu->arch.pid;
1011a73a9599SAlexander Graf 	regs->sprg0 = vcpu->arch.shared->sprg0;
1012a73a9599SAlexander Graf 	regs->sprg1 = vcpu->arch.shared->sprg1;
1013a73a9599SAlexander Graf 	regs->sprg2 = vcpu->arch.shared->sprg2;
1014a73a9599SAlexander Graf 	regs->sprg3 = vcpu->arch.shared->sprg3;
1015b5904972SScott Wood 	regs->sprg4 = vcpu->arch.shared->sprg4;
1016b5904972SScott Wood 	regs->sprg5 = vcpu->arch.shared->sprg5;
1017b5904972SScott Wood 	regs->sprg6 = vcpu->arch.shared->sprg6;
1018b5904972SScott Wood 	regs->sprg7 = vcpu->arch.shared->sprg7;
1019d9fbd03dSHollis Blanchard 
1020d9fbd03dSHollis Blanchard 	for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
10218e5b26b5SAlexander Graf 		regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
1022d9fbd03dSHollis Blanchard 
1023d9fbd03dSHollis Blanchard 	return 0;
1024d9fbd03dSHollis Blanchard }
1025d9fbd03dSHollis Blanchard 
1026d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1027d9fbd03dSHollis Blanchard {
1028d9fbd03dSHollis Blanchard 	int i;
1029d9fbd03dSHollis Blanchard 
1030d9fbd03dSHollis Blanchard 	vcpu->arch.pc = regs->pc;
1031992b5b29SAlexander Graf 	kvmppc_set_cr(vcpu, regs->cr);
1032d9fbd03dSHollis Blanchard 	vcpu->arch.ctr = regs->ctr;
1033d9fbd03dSHollis Blanchard 	vcpu->arch.lr = regs->lr;
1034992b5b29SAlexander Graf 	kvmppc_set_xer(vcpu, regs->xer);
1035b8fd68acSHollis Blanchard 	kvmppc_set_msr(vcpu, regs->msr);
1036de7906c3SAlexander Graf 	vcpu->arch.shared->srr0 = regs->srr0;
1037de7906c3SAlexander Graf 	vcpu->arch.shared->srr1 = regs->srr1;
10385ce941eeSScott Wood 	kvmppc_set_pid(vcpu, regs->pid);
1039a73a9599SAlexander Graf 	vcpu->arch.shared->sprg0 = regs->sprg0;
1040a73a9599SAlexander Graf 	vcpu->arch.shared->sprg1 = regs->sprg1;
1041a73a9599SAlexander Graf 	vcpu->arch.shared->sprg2 = regs->sprg2;
1042a73a9599SAlexander Graf 	vcpu->arch.shared->sprg3 = regs->sprg3;
1043b5904972SScott Wood 	vcpu->arch.shared->sprg4 = regs->sprg4;
1044b5904972SScott Wood 	vcpu->arch.shared->sprg5 = regs->sprg5;
1045b5904972SScott Wood 	vcpu->arch.shared->sprg6 = regs->sprg6;
1046b5904972SScott Wood 	vcpu->arch.shared->sprg7 = regs->sprg7;
1047d9fbd03dSHollis Blanchard 
10488e5b26b5SAlexander Graf 	for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
10498e5b26b5SAlexander Graf 		kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
1050d9fbd03dSHollis Blanchard 
1051d9fbd03dSHollis Blanchard 	return 0;
1052d9fbd03dSHollis Blanchard }
1053d9fbd03dSHollis Blanchard 
10545ce941eeSScott Wood static void get_sregs_base(struct kvm_vcpu *vcpu,
10555ce941eeSScott Wood                            struct kvm_sregs *sregs)
10565ce941eeSScott Wood {
10575ce941eeSScott Wood 	u64 tb = get_tb();
10585ce941eeSScott Wood 
10595ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_BASE;
10605ce941eeSScott Wood 
10615ce941eeSScott Wood 	sregs->u.e.csrr0 = vcpu->arch.csrr0;
10625ce941eeSScott Wood 	sregs->u.e.csrr1 = vcpu->arch.csrr1;
10635ce941eeSScott Wood 	sregs->u.e.mcsr = vcpu->arch.mcsr;
1064d30f6e48SScott Wood 	sregs->u.e.esr = get_guest_esr(vcpu);
1065d30f6e48SScott Wood 	sregs->u.e.dear = get_guest_dear(vcpu);
10665ce941eeSScott Wood 	sregs->u.e.tsr = vcpu->arch.tsr;
10675ce941eeSScott Wood 	sregs->u.e.tcr = vcpu->arch.tcr;
10685ce941eeSScott Wood 	sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
10695ce941eeSScott Wood 	sregs->u.e.tb = tb;
10705ce941eeSScott Wood 	sregs->u.e.vrsave = vcpu->arch.vrsave;
10715ce941eeSScott Wood }
10725ce941eeSScott Wood 
10735ce941eeSScott Wood static int set_sregs_base(struct kvm_vcpu *vcpu,
10745ce941eeSScott Wood                           struct kvm_sregs *sregs)
10755ce941eeSScott Wood {
10765ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
10775ce941eeSScott Wood 		return 0;
10785ce941eeSScott Wood 
10795ce941eeSScott Wood 	vcpu->arch.csrr0 = sregs->u.e.csrr0;
10805ce941eeSScott Wood 	vcpu->arch.csrr1 = sregs->u.e.csrr1;
10815ce941eeSScott Wood 	vcpu->arch.mcsr = sregs->u.e.mcsr;
1082d30f6e48SScott Wood 	set_guest_esr(vcpu, sregs->u.e.esr);
1083d30f6e48SScott Wood 	set_guest_dear(vcpu, sregs->u.e.dear);
10845ce941eeSScott Wood 	vcpu->arch.vrsave = sregs->u.e.vrsave;
1085dfd4d47eSScott Wood 	kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
10865ce941eeSScott Wood 
1087dfd4d47eSScott Wood 	if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
10885ce941eeSScott Wood 		vcpu->arch.dec = sregs->u.e.dec;
10895ce941eeSScott Wood 		kvmppc_emulate_dec(vcpu);
1090dfd4d47eSScott Wood 	}
10915ce941eeSScott Wood 
10925ce941eeSScott Wood 	if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
1093dfd4d47eSScott Wood 		vcpu->arch.tsr = sregs->u.e.tsr;
1094dfd4d47eSScott Wood 		update_timer_ints(vcpu);
10955ce941eeSScott Wood 	}
10965ce941eeSScott Wood 
10975ce941eeSScott Wood 	return 0;
10985ce941eeSScott Wood }
10995ce941eeSScott Wood 
11005ce941eeSScott Wood static void get_sregs_arch206(struct kvm_vcpu *vcpu,
11015ce941eeSScott Wood                               struct kvm_sregs *sregs)
11025ce941eeSScott Wood {
11035ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_ARCH206;
11045ce941eeSScott Wood 
1105841741f2SScott Wood 	sregs->u.e.pir = vcpu->vcpu_id;
11065ce941eeSScott Wood 	sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
11075ce941eeSScott Wood 	sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
11085ce941eeSScott Wood 	sregs->u.e.decar = vcpu->arch.decar;
11095ce941eeSScott Wood 	sregs->u.e.ivpr = vcpu->arch.ivpr;
11105ce941eeSScott Wood }
11115ce941eeSScott Wood 
11125ce941eeSScott Wood static int set_sregs_arch206(struct kvm_vcpu *vcpu,
11135ce941eeSScott Wood                              struct kvm_sregs *sregs)
11145ce941eeSScott Wood {
11155ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
11165ce941eeSScott Wood 		return 0;
11175ce941eeSScott Wood 
1118841741f2SScott Wood 	if (sregs->u.e.pir != vcpu->vcpu_id)
11195ce941eeSScott Wood 		return -EINVAL;
11205ce941eeSScott Wood 
11215ce941eeSScott Wood 	vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
11225ce941eeSScott Wood 	vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
11235ce941eeSScott Wood 	vcpu->arch.decar = sregs->u.e.decar;
11245ce941eeSScott Wood 	vcpu->arch.ivpr = sregs->u.e.ivpr;
11255ce941eeSScott Wood 
11265ce941eeSScott Wood 	return 0;
11275ce941eeSScott Wood }
11285ce941eeSScott Wood 
11295ce941eeSScott Wood void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
11305ce941eeSScott Wood {
11315ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_IVOR;
11325ce941eeSScott Wood 
11335ce941eeSScott Wood 	sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
11345ce941eeSScott Wood 	sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
11355ce941eeSScott Wood 	sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
11365ce941eeSScott Wood 	sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
11375ce941eeSScott Wood 	sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
11385ce941eeSScott Wood 	sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
11395ce941eeSScott Wood 	sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
11405ce941eeSScott Wood 	sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
11415ce941eeSScott Wood 	sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
11425ce941eeSScott Wood 	sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
11435ce941eeSScott Wood 	sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
11445ce941eeSScott Wood 	sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
11455ce941eeSScott Wood 	sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
11465ce941eeSScott Wood 	sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
11475ce941eeSScott Wood 	sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
11485ce941eeSScott Wood 	sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
11495ce941eeSScott Wood }
11505ce941eeSScott Wood 
11515ce941eeSScott Wood int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
11525ce941eeSScott Wood {
11535ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
11545ce941eeSScott Wood 		return 0;
11555ce941eeSScott Wood 
11565ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
11575ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
11585ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
11595ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
11605ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
11615ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
11625ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
11635ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
11645ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
11655ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
11665ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
11675ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
11685ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
11695ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
11705ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
11715ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
11725ce941eeSScott Wood 
11735ce941eeSScott Wood 	return 0;
11745ce941eeSScott Wood }
11755ce941eeSScott Wood 
1176d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1177d9fbd03dSHollis Blanchard                                   struct kvm_sregs *sregs)
1178d9fbd03dSHollis Blanchard {
11795ce941eeSScott Wood 	sregs->pvr = vcpu->arch.pvr;
11805ce941eeSScott Wood 
11815ce941eeSScott Wood 	get_sregs_base(vcpu, sregs);
11825ce941eeSScott Wood 	get_sregs_arch206(vcpu, sregs);
11835ce941eeSScott Wood 	kvmppc_core_get_sregs(vcpu, sregs);
11845ce941eeSScott Wood 	return 0;
1185d9fbd03dSHollis Blanchard }
1186d9fbd03dSHollis Blanchard 
1187d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1188d9fbd03dSHollis Blanchard                                   struct kvm_sregs *sregs)
1189d9fbd03dSHollis Blanchard {
11905ce941eeSScott Wood 	int ret;
11915ce941eeSScott Wood 
11925ce941eeSScott Wood 	if (vcpu->arch.pvr != sregs->pvr)
11935ce941eeSScott Wood 		return -EINVAL;
11945ce941eeSScott Wood 
11955ce941eeSScott Wood 	ret = set_sregs_base(vcpu, sregs);
11965ce941eeSScott Wood 	if (ret < 0)
11975ce941eeSScott Wood 		return ret;
11985ce941eeSScott Wood 
11995ce941eeSScott Wood 	ret = set_sregs_arch206(vcpu, sregs);
12005ce941eeSScott Wood 	if (ret < 0)
12015ce941eeSScott Wood 		return ret;
12025ce941eeSScott Wood 
12035ce941eeSScott Wood 	return kvmppc_core_set_sregs(vcpu, sregs);
1204d9fbd03dSHollis Blanchard }
1205d9fbd03dSHollis Blanchard 
120631f3438eSPaul Mackerras int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
120731f3438eSPaul Mackerras {
120831f3438eSPaul Mackerras 	return -EINVAL;
120931f3438eSPaul Mackerras }
121031f3438eSPaul Mackerras 
121131f3438eSPaul Mackerras int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
121231f3438eSPaul Mackerras {
121331f3438eSPaul Mackerras 	return -EINVAL;
121431f3438eSPaul Mackerras }
121531f3438eSPaul Mackerras 
1216d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1217d9fbd03dSHollis Blanchard {
1218d9fbd03dSHollis Blanchard 	return -ENOTSUPP;
1219d9fbd03dSHollis Blanchard }
1220d9fbd03dSHollis Blanchard 
1221d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1222d9fbd03dSHollis Blanchard {
1223d9fbd03dSHollis Blanchard 	return -ENOTSUPP;
1224d9fbd03dSHollis Blanchard }
1225d9fbd03dSHollis Blanchard 
1226d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1227d9fbd03dSHollis Blanchard                                   struct kvm_translation *tr)
1228d9fbd03dSHollis Blanchard {
122998001d8dSAvi Kivity 	int r;
123098001d8dSAvi Kivity 
123198001d8dSAvi Kivity 	r = kvmppc_core_vcpu_translate(vcpu, tr);
123298001d8dSAvi Kivity 	return r;
1233d9fbd03dSHollis Blanchard }
1234d9fbd03dSHollis Blanchard 
12354e755758SAlexander Graf int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
12364e755758SAlexander Graf {
12374e755758SAlexander Graf 	return -ENOTSUPP;
12384e755758SAlexander Graf }
12394e755758SAlexander Graf 
1240f9e0554dSPaul Mackerras int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1241f9e0554dSPaul Mackerras 				      struct kvm_userspace_memory_region *mem)
1242f9e0554dSPaul Mackerras {
1243f9e0554dSPaul Mackerras 	return 0;
1244f9e0554dSPaul Mackerras }
1245f9e0554dSPaul Mackerras 
1246f9e0554dSPaul Mackerras void kvmppc_core_commit_memory_region(struct kvm *kvm,
1247f9e0554dSPaul Mackerras 				struct kvm_userspace_memory_region *mem)
1248f9e0554dSPaul Mackerras {
1249f9e0554dSPaul Mackerras }
1250f9e0554dSPaul Mackerras 
1251dfd4d47eSScott Wood void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1252dfd4d47eSScott Wood {
1253dfd4d47eSScott Wood 	vcpu->arch.tcr = new_tcr;
1254dfd4d47eSScott Wood 	update_timer_ints(vcpu);
1255dfd4d47eSScott Wood }
1256dfd4d47eSScott Wood 
1257dfd4d47eSScott Wood void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1258dfd4d47eSScott Wood {
1259dfd4d47eSScott Wood 	set_bits(tsr_bits, &vcpu->arch.tsr);
1260dfd4d47eSScott Wood 	smp_wmb();
1261dfd4d47eSScott Wood 	kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1262dfd4d47eSScott Wood 	kvm_vcpu_kick(vcpu);
1263dfd4d47eSScott Wood }
1264dfd4d47eSScott Wood 
1265dfd4d47eSScott Wood void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1266dfd4d47eSScott Wood {
1267dfd4d47eSScott Wood 	clear_bits(tsr_bits, &vcpu->arch.tsr);
1268dfd4d47eSScott Wood 	update_timer_ints(vcpu);
1269dfd4d47eSScott Wood }
1270dfd4d47eSScott Wood 
1271dfd4d47eSScott Wood void kvmppc_decrementer_func(unsigned long data)
1272dfd4d47eSScott Wood {
1273dfd4d47eSScott Wood 	struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1274dfd4d47eSScott Wood 
127521bd000aSBharat Bhushan 	if (vcpu->arch.tcr & TCR_ARE) {
127621bd000aSBharat Bhushan 		vcpu->arch.dec = vcpu->arch.decar;
127721bd000aSBharat Bhushan 		kvmppc_emulate_dec(vcpu);
127821bd000aSBharat Bhushan 	}
127921bd000aSBharat Bhushan 
1280dfd4d47eSScott Wood 	kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1281dfd4d47eSScott Wood }
1282dfd4d47eSScott Wood 
128394fa9d99SScott Wood void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
128494fa9d99SScott Wood {
1285d30f6e48SScott Wood 	current->thread.kvm_vcpu = vcpu;
128694fa9d99SScott Wood }
128794fa9d99SScott Wood 
128894fa9d99SScott Wood void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
128994fa9d99SScott Wood {
1290d30f6e48SScott Wood 	current->thread.kvm_vcpu = NULL;
129194fa9d99SScott Wood }
129294fa9d99SScott Wood 
12932986b8c7SStephen Rothwell int __init kvmppc_booke_init(void)
1294d9fbd03dSHollis Blanchard {
1295d30f6e48SScott Wood #ifndef CONFIG_KVM_BOOKE_HV
1296d9fbd03dSHollis Blanchard 	unsigned long ivor[16];
1297d9fbd03dSHollis Blanchard 	unsigned long max_ivor = 0;
1298d9fbd03dSHollis Blanchard 	int i;
1299d9fbd03dSHollis Blanchard 
1300d9fbd03dSHollis Blanchard 	/* We install our own exception handlers by hijacking IVPR. IVPR must
1301d9fbd03dSHollis Blanchard 	 * be 16-bit aligned, so we need a 64KB allocation. */
1302d9fbd03dSHollis Blanchard 	kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1303d9fbd03dSHollis Blanchard 	                                         VCPU_SIZE_ORDER);
1304d9fbd03dSHollis Blanchard 	if (!kvmppc_booke_handlers)
1305d9fbd03dSHollis Blanchard 		return -ENOMEM;
1306d9fbd03dSHollis Blanchard 
1307d9fbd03dSHollis Blanchard 	/* XXX make sure our handlers are smaller than Linux's */
1308d9fbd03dSHollis Blanchard 
1309d9fbd03dSHollis Blanchard 	/* Copy our interrupt handlers to match host IVORs. That way we don't
1310d9fbd03dSHollis Blanchard 	 * have to swap the IVORs on every guest/host transition. */
1311d9fbd03dSHollis Blanchard 	ivor[0] = mfspr(SPRN_IVOR0);
1312d9fbd03dSHollis Blanchard 	ivor[1] = mfspr(SPRN_IVOR1);
1313d9fbd03dSHollis Blanchard 	ivor[2] = mfspr(SPRN_IVOR2);
1314d9fbd03dSHollis Blanchard 	ivor[3] = mfspr(SPRN_IVOR3);
1315d9fbd03dSHollis Blanchard 	ivor[4] = mfspr(SPRN_IVOR4);
1316d9fbd03dSHollis Blanchard 	ivor[5] = mfspr(SPRN_IVOR5);
1317d9fbd03dSHollis Blanchard 	ivor[6] = mfspr(SPRN_IVOR6);
1318d9fbd03dSHollis Blanchard 	ivor[7] = mfspr(SPRN_IVOR7);
1319d9fbd03dSHollis Blanchard 	ivor[8] = mfspr(SPRN_IVOR8);
1320d9fbd03dSHollis Blanchard 	ivor[9] = mfspr(SPRN_IVOR9);
1321d9fbd03dSHollis Blanchard 	ivor[10] = mfspr(SPRN_IVOR10);
1322d9fbd03dSHollis Blanchard 	ivor[11] = mfspr(SPRN_IVOR11);
1323d9fbd03dSHollis Blanchard 	ivor[12] = mfspr(SPRN_IVOR12);
1324d9fbd03dSHollis Blanchard 	ivor[13] = mfspr(SPRN_IVOR13);
1325d9fbd03dSHollis Blanchard 	ivor[14] = mfspr(SPRN_IVOR14);
1326d9fbd03dSHollis Blanchard 	ivor[15] = mfspr(SPRN_IVOR15);
1327d9fbd03dSHollis Blanchard 
1328d9fbd03dSHollis Blanchard 	for (i = 0; i < 16; i++) {
1329d9fbd03dSHollis Blanchard 		if (ivor[i] > max_ivor)
1330d9fbd03dSHollis Blanchard 			max_ivor = ivor[i];
1331d9fbd03dSHollis Blanchard 
1332d9fbd03dSHollis Blanchard 		memcpy((void *)kvmppc_booke_handlers + ivor[i],
1333d9fbd03dSHollis Blanchard 		       kvmppc_handlers_start + i * kvmppc_handler_len,
1334d9fbd03dSHollis Blanchard 		       kvmppc_handler_len);
1335d9fbd03dSHollis Blanchard 	}
1336d9fbd03dSHollis Blanchard 	flush_icache_range(kvmppc_booke_handlers,
1337d9fbd03dSHollis Blanchard 	                   kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
1338d30f6e48SScott Wood #endif /* !BOOKE_HV */
1339db93f574SHollis Blanchard 	return 0;
1340d9fbd03dSHollis Blanchard }
1341d9fbd03dSHollis Blanchard 
1342db93f574SHollis Blanchard void __exit kvmppc_booke_exit(void)
1343d9fbd03dSHollis Blanchard {
1344d9fbd03dSHollis Blanchard 	free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
1345d9fbd03dSHollis Blanchard 	kvm_exit();
1346d9fbd03dSHollis Blanchard }
1347