xref: /openbmc/linux/arch/powerpc/kvm/booke.c (revision 862d31f788f9a249f7656d02d8d4006e306108ce)
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 {
1469dd921cfSHollis Blanchard 	set_bit(priority, &vcpu->arch.pending_exceptions);
1479dd921cfSHollis Blanchard }
1489dd921cfSHollis Blanchard 
149daf5e271SLiu Yu static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
150daf5e271SLiu Yu                                         ulong dear_flags, ulong esr_flags)
1519dd921cfSHollis Blanchard {
152daf5e271SLiu Yu 	vcpu->arch.queued_dear = dear_flags;
153daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
154daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
155daf5e271SLiu Yu }
156daf5e271SLiu Yu 
157daf5e271SLiu Yu static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
158daf5e271SLiu Yu                                            ulong dear_flags, ulong esr_flags)
159daf5e271SLiu Yu {
160daf5e271SLiu Yu 	vcpu->arch.queued_dear = dear_flags;
161daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
162daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
163daf5e271SLiu Yu }
164daf5e271SLiu Yu 
165daf5e271SLiu Yu static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
166daf5e271SLiu Yu                                            ulong esr_flags)
167daf5e271SLiu Yu {
168daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
169daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
170daf5e271SLiu Yu }
171daf5e271SLiu Yu 
172daf5e271SLiu Yu void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
173daf5e271SLiu Yu {
174daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
175d4cf3892SHollis Blanchard 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
1769dd921cfSHollis Blanchard }
1779dd921cfSHollis Blanchard 
1789dd921cfSHollis Blanchard void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
1799dd921cfSHollis Blanchard {
180d4cf3892SHollis Blanchard 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
1819dd921cfSHollis Blanchard }
1829dd921cfSHollis Blanchard 
1839dd921cfSHollis Blanchard int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
1849dd921cfSHollis Blanchard {
185d4cf3892SHollis Blanchard 	return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
1869dd921cfSHollis Blanchard }
1879dd921cfSHollis Blanchard 
1887706664dSAlexander Graf void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
1897706664dSAlexander Graf {
1907706664dSAlexander Graf 	clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
1917706664dSAlexander Graf }
1927706664dSAlexander Graf 
1939dd921cfSHollis Blanchard void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
1949dd921cfSHollis Blanchard                                 struct kvm_interrupt *irq)
1959dd921cfSHollis Blanchard {
196c5335f17SAlexander Graf 	unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
197c5335f17SAlexander Graf 
198c5335f17SAlexander Graf 	if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
199c5335f17SAlexander Graf 		prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
200c5335f17SAlexander Graf 
201c5335f17SAlexander Graf 	kvmppc_booke_queue_irqprio(vcpu, prio);
2029dd921cfSHollis Blanchard }
2039dd921cfSHollis Blanchard 
2044496f974SAlexander Graf void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
2054496f974SAlexander Graf                                   struct kvm_interrupt *irq)
2064496f974SAlexander Graf {
2074496f974SAlexander Graf 	clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
208c5335f17SAlexander Graf 	clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
2094496f974SAlexander Graf }
2104496f974SAlexander Graf 
211d30f6e48SScott Wood static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
212d30f6e48SScott Wood {
213d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
214d30f6e48SScott Wood 	mtspr(SPRN_GSRR0, srr0);
215d30f6e48SScott Wood 	mtspr(SPRN_GSRR1, srr1);
216d30f6e48SScott Wood #else
217d30f6e48SScott Wood 	vcpu->arch.shared->srr0 = srr0;
218d30f6e48SScott Wood 	vcpu->arch.shared->srr1 = srr1;
219d30f6e48SScott Wood #endif
220d30f6e48SScott Wood }
221d30f6e48SScott Wood 
222d30f6e48SScott Wood static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
223d30f6e48SScott Wood {
224d30f6e48SScott Wood 	vcpu->arch.csrr0 = srr0;
225d30f6e48SScott Wood 	vcpu->arch.csrr1 = srr1;
226d30f6e48SScott Wood }
227d30f6e48SScott Wood 
228d30f6e48SScott Wood static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
229d30f6e48SScott Wood {
230d30f6e48SScott Wood 	if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
231d30f6e48SScott Wood 		vcpu->arch.dsrr0 = srr0;
232d30f6e48SScott Wood 		vcpu->arch.dsrr1 = srr1;
233d30f6e48SScott Wood 	} else {
234d30f6e48SScott Wood 		set_guest_csrr(vcpu, srr0, srr1);
235d30f6e48SScott Wood 	}
236d30f6e48SScott Wood }
237d30f6e48SScott Wood 
238d30f6e48SScott Wood static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
239d30f6e48SScott Wood {
240d30f6e48SScott Wood 	vcpu->arch.mcsrr0 = srr0;
241d30f6e48SScott Wood 	vcpu->arch.mcsrr1 = srr1;
242d30f6e48SScott Wood }
243d30f6e48SScott Wood 
244d30f6e48SScott Wood static unsigned long get_guest_dear(struct kvm_vcpu *vcpu)
245d30f6e48SScott Wood {
246d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
247d30f6e48SScott Wood 	return mfspr(SPRN_GDEAR);
248d30f6e48SScott Wood #else
249d30f6e48SScott Wood 	return vcpu->arch.shared->dar;
250d30f6e48SScott Wood #endif
251d30f6e48SScott Wood }
252d30f6e48SScott Wood 
253d30f6e48SScott Wood static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear)
254d30f6e48SScott Wood {
255d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
256d30f6e48SScott Wood 	mtspr(SPRN_GDEAR, dear);
257d30f6e48SScott Wood #else
258d30f6e48SScott Wood 	vcpu->arch.shared->dar = dear;
259d30f6e48SScott Wood #endif
260d30f6e48SScott Wood }
261d30f6e48SScott Wood 
262d30f6e48SScott Wood static unsigned long get_guest_esr(struct kvm_vcpu *vcpu)
263d30f6e48SScott Wood {
264d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
265d30f6e48SScott Wood 	return mfspr(SPRN_GESR);
266d30f6e48SScott Wood #else
267d30f6e48SScott Wood 	return vcpu->arch.shared->esr;
268d30f6e48SScott Wood #endif
269d30f6e48SScott Wood }
270d30f6e48SScott Wood 
271d30f6e48SScott Wood static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr)
272d30f6e48SScott Wood {
273d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
274d30f6e48SScott Wood 	mtspr(SPRN_GESR, esr);
275d30f6e48SScott Wood #else
276d30f6e48SScott Wood 	vcpu->arch.shared->esr = esr;
277d30f6e48SScott Wood #endif
278d30f6e48SScott Wood }
279d30f6e48SScott Wood 
280d4cf3892SHollis Blanchard /* Deliver the interrupt of the corresponding priority, if possible. */
281d4cf3892SHollis Blanchard static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
282d4cf3892SHollis Blanchard                                         unsigned int priority)
283d9fbd03dSHollis Blanchard {
284d4cf3892SHollis Blanchard 	int allowed = 0;
28579300f8cSAlexander Graf 	ulong msr_mask = 0;
286daf5e271SLiu Yu 	bool update_esr = false, update_dear = false;
2875c6cedf4SAlexander Graf 	ulong crit_raw = vcpu->arch.shared->critical;
2885c6cedf4SAlexander Graf 	ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
2895c6cedf4SAlexander Graf 	bool crit;
290c5335f17SAlexander Graf 	bool keep_irq = false;
291d30f6e48SScott Wood 	enum int_class int_class;
2925c6cedf4SAlexander Graf 
2935c6cedf4SAlexander Graf 	/* Truncate crit indicators in 32 bit mode */
2945c6cedf4SAlexander Graf 	if (!(vcpu->arch.shared->msr & MSR_SF)) {
2955c6cedf4SAlexander Graf 		crit_raw &= 0xffffffff;
2965c6cedf4SAlexander Graf 		crit_r1 &= 0xffffffff;
2975c6cedf4SAlexander Graf 	}
2985c6cedf4SAlexander Graf 
2995c6cedf4SAlexander Graf 	/* Critical section when crit == r1 */
3005c6cedf4SAlexander Graf 	crit = (crit_raw == crit_r1);
3015c6cedf4SAlexander Graf 	/* ... and we're in supervisor mode */
3025c6cedf4SAlexander Graf 	crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
303d9fbd03dSHollis Blanchard 
304c5335f17SAlexander Graf 	if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
305c5335f17SAlexander Graf 		priority = BOOKE_IRQPRIO_EXTERNAL;
306c5335f17SAlexander Graf 		keep_irq = true;
307c5335f17SAlexander Graf 	}
308c5335f17SAlexander Graf 
309d4cf3892SHollis Blanchard 	switch (priority) {
310d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DTLB_MISS:
311daf5e271SLiu Yu 	case BOOKE_IRQPRIO_DATA_STORAGE:
312daf5e271SLiu Yu 		update_dear = true;
313daf5e271SLiu Yu 		/* fall through */
314daf5e271SLiu Yu 	case BOOKE_IRQPRIO_INST_STORAGE:
315daf5e271SLiu Yu 	case BOOKE_IRQPRIO_PROGRAM:
316daf5e271SLiu Yu 		update_esr = true;
317daf5e271SLiu Yu 		/* fall through */
318d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_ITLB_MISS:
319d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_SYSCALL:
320d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_FP_UNAVAIL:
321bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_UNAVAIL:
322bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_FP_DATA:
323bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_FP_ROUND:
324d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_AP_UNAVAIL:
325d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_ALIGNMENT:
326d4cf3892SHollis Blanchard 		allowed = 1;
32779300f8cSAlexander Graf 		msr_mask = MSR_CE | MSR_ME | MSR_DE;
328d30f6e48SScott Wood 		int_class = INT_CLASS_NONCRIT;
329d9fbd03dSHollis Blanchard 		break;
330d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_CRITICAL:
3314ab96919SAlexander Graf 	case BOOKE_IRQPRIO_DBELL_CRIT:
332666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_CE;
333d30f6e48SScott Wood 		allowed = allowed && !crit;
33479300f8cSAlexander Graf 		msr_mask = MSR_ME;
335d30f6e48SScott Wood 		int_class = INT_CLASS_CRIT;
336d9fbd03dSHollis Blanchard 		break;
337d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_MACHINE_CHECK:
338666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_ME;
339d30f6e48SScott Wood 		allowed = allowed && !crit;
340d30f6e48SScott Wood 		int_class = INT_CLASS_MC;
341d9fbd03dSHollis Blanchard 		break;
342d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DECREMENTER:
343d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_FIT:
344dfd4d47eSScott Wood 		keep_irq = true;
345dfd4d47eSScott Wood 		/* fall through */
346dfd4d47eSScott Wood 	case BOOKE_IRQPRIO_EXTERNAL:
3474ab96919SAlexander Graf 	case BOOKE_IRQPRIO_DBELL:
348666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_EE;
3495c6cedf4SAlexander Graf 		allowed = allowed && !crit;
35079300f8cSAlexander Graf 		msr_mask = MSR_CE | MSR_ME | MSR_DE;
351d30f6e48SScott Wood 		int_class = INT_CLASS_NONCRIT;
352d9fbd03dSHollis Blanchard 		break;
353d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DEBUG:
354666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_DE;
355d30f6e48SScott Wood 		allowed = allowed && !crit;
35679300f8cSAlexander Graf 		msr_mask = MSR_ME;
357d30f6e48SScott Wood 		int_class = INT_CLASS_CRIT;
358d9fbd03dSHollis Blanchard 		break;
359d9fbd03dSHollis Blanchard 	}
360d9fbd03dSHollis Blanchard 
361d4cf3892SHollis Blanchard 	if (allowed) {
362d30f6e48SScott Wood 		switch (int_class) {
363d30f6e48SScott Wood 		case INT_CLASS_NONCRIT:
364d30f6e48SScott Wood 			set_guest_srr(vcpu, vcpu->arch.pc,
365d30f6e48SScott Wood 				      vcpu->arch.shared->msr);
366d30f6e48SScott Wood 			break;
367d30f6e48SScott Wood 		case INT_CLASS_CRIT:
368d30f6e48SScott Wood 			set_guest_csrr(vcpu, vcpu->arch.pc,
369d30f6e48SScott Wood 				       vcpu->arch.shared->msr);
370d30f6e48SScott Wood 			break;
371d30f6e48SScott Wood 		case INT_CLASS_DBG:
372d30f6e48SScott Wood 			set_guest_dsrr(vcpu, vcpu->arch.pc,
373d30f6e48SScott Wood 				       vcpu->arch.shared->msr);
374d30f6e48SScott Wood 			break;
375d30f6e48SScott Wood 		case INT_CLASS_MC:
376d30f6e48SScott Wood 			set_guest_mcsrr(vcpu, vcpu->arch.pc,
377d30f6e48SScott Wood 					vcpu->arch.shared->msr);
378d30f6e48SScott Wood 			break;
379d30f6e48SScott Wood 		}
380d30f6e48SScott Wood 
381d4cf3892SHollis Blanchard 		vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
382daf5e271SLiu Yu 		if (update_esr == true)
383d30f6e48SScott Wood 			set_guest_esr(vcpu, vcpu->arch.queued_esr);
384daf5e271SLiu Yu 		if (update_dear == true)
385d30f6e48SScott Wood 			set_guest_dear(vcpu, vcpu->arch.queued_dear);
386666e7252SAlexander Graf 		kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
387d4cf3892SHollis Blanchard 
388c5335f17SAlexander Graf 		if (!keep_irq)
389d4cf3892SHollis Blanchard 			clear_bit(priority, &vcpu->arch.pending_exceptions);
390d4cf3892SHollis Blanchard 	}
391d4cf3892SHollis Blanchard 
392d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
393d30f6e48SScott Wood 	/*
394d30f6e48SScott Wood 	 * If an interrupt is pending but masked, raise a guest doorbell
395d30f6e48SScott Wood 	 * so that we are notified when the guest enables the relevant
396d30f6e48SScott Wood 	 * MSR bit.
397d30f6e48SScott Wood 	 */
398d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
399d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
400d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
401d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
402d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
403d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
404d30f6e48SScott Wood #endif
405d30f6e48SScott Wood 
406d4cf3892SHollis Blanchard 	return allowed;
407d9fbd03dSHollis Blanchard }
408d9fbd03dSHollis Blanchard 
409dfd4d47eSScott Wood static void update_timer_ints(struct kvm_vcpu *vcpu)
410dfd4d47eSScott Wood {
411dfd4d47eSScott Wood 	if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
412dfd4d47eSScott Wood 		kvmppc_core_queue_dec(vcpu);
413dfd4d47eSScott Wood 	else
414dfd4d47eSScott Wood 		kvmppc_core_dequeue_dec(vcpu);
415dfd4d47eSScott Wood }
416dfd4d47eSScott Wood 
417c59a6a3eSScott Wood static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
418d9fbd03dSHollis Blanchard {
419d9fbd03dSHollis Blanchard 	unsigned long *pending = &vcpu->arch.pending_exceptions;
420d9fbd03dSHollis Blanchard 	unsigned int priority;
421d9fbd03dSHollis Blanchard 
4229ab80843SHollis Blanchard 	priority = __ffs(*pending);
4238b3a00fcSAlexander Graf 	while (priority < BOOKE_IRQPRIO_MAX) {
424d4cf3892SHollis Blanchard 		if (kvmppc_booke_irqprio_deliver(vcpu, priority))
425d9fbd03dSHollis Blanchard 			break;
426d9fbd03dSHollis Blanchard 
427d9fbd03dSHollis Blanchard 		priority = find_next_bit(pending,
428d9fbd03dSHollis Blanchard 		                         BITS_PER_BYTE * sizeof(*pending),
429d9fbd03dSHollis Blanchard 		                         priority + 1);
430d9fbd03dSHollis Blanchard 	}
43190bba358SAlexander Graf 
43290bba358SAlexander Graf 	/* Tell the guest about our interrupt status */
43329ac26efSScott Wood 	vcpu->arch.shared->int_pending = !!*pending;
434d9fbd03dSHollis Blanchard }
435d9fbd03dSHollis Blanchard 
436c59a6a3eSScott Wood /* Check pending exceptions and deliver one, if possible. */
437a8e4ef84SAlexander Graf int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
438c59a6a3eSScott Wood {
439a8e4ef84SAlexander Graf 	int r = 0;
440c59a6a3eSScott Wood 	WARN_ON_ONCE(!irqs_disabled());
441c59a6a3eSScott Wood 
442c59a6a3eSScott Wood 	kvmppc_core_check_exceptions(vcpu);
443c59a6a3eSScott Wood 
444c59a6a3eSScott Wood 	if (vcpu->arch.shared->msr & MSR_WE) {
445c59a6a3eSScott Wood 		local_irq_enable();
446c59a6a3eSScott Wood 		kvm_vcpu_block(vcpu);
447966cd0f3SAlexander Graf 		clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
448c59a6a3eSScott Wood 		local_irq_disable();
449c59a6a3eSScott Wood 
450c59a6a3eSScott Wood 		kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
451a8e4ef84SAlexander Graf 		r = 1;
452c59a6a3eSScott Wood 	};
453a8e4ef84SAlexander Graf 
454a8e4ef84SAlexander Graf 	return r;
455a8e4ef84SAlexander Graf }
456a8e4ef84SAlexander Graf 
4574ffc6356SAlexander Graf static void kvmppc_check_requests(struct kvm_vcpu *vcpu)
4584ffc6356SAlexander Graf {
4594ffc6356SAlexander Graf 	if (vcpu->requests) {
4604ffc6356SAlexander Graf 		if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
4614ffc6356SAlexander Graf 			update_timer_ints(vcpu);
462*862d31f7SAlexander Graf #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
463*862d31f7SAlexander Graf 		if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
464*862d31f7SAlexander Graf 			kvmppc_core_flush_tlb(vcpu);
465*862d31f7SAlexander Graf #endif
4664ffc6356SAlexander Graf 	}
4674ffc6356SAlexander Graf }
4684ffc6356SAlexander Graf 
469a8e4ef84SAlexander Graf /*
470a8e4ef84SAlexander Graf  * Common checks before entering the guest world.  Call with interrupts
471a8e4ef84SAlexander Graf  * disabled.
472a8e4ef84SAlexander Graf  *
473a8e4ef84SAlexander Graf  * returns !0 if a signal is pending and check_signal is true
474a8e4ef84SAlexander Graf  */
47503660ba2SAlexander Graf static int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
476a8e4ef84SAlexander Graf {
477a8e4ef84SAlexander Graf 	int r = 0;
478a8e4ef84SAlexander Graf 
479a8e4ef84SAlexander Graf 	WARN_ON_ONCE(!irqs_disabled());
480a8e4ef84SAlexander Graf 	while (true) {
481a8e4ef84SAlexander Graf 		if (need_resched()) {
482a8e4ef84SAlexander Graf 			local_irq_enable();
483a8e4ef84SAlexander Graf 			cond_resched();
484a8e4ef84SAlexander Graf 			local_irq_disable();
485a8e4ef84SAlexander Graf 			continue;
486a8e4ef84SAlexander Graf 		}
487a8e4ef84SAlexander Graf 
48803660ba2SAlexander Graf 		if (signal_pending(current)) {
489a8e4ef84SAlexander Graf 			r = 1;
490a8e4ef84SAlexander Graf 			break;
491a8e4ef84SAlexander Graf 		}
492a8e4ef84SAlexander Graf 
4934ffc6356SAlexander Graf 		smp_mb();
4944ffc6356SAlexander Graf 		if (vcpu->requests) {
4954ffc6356SAlexander Graf 			/* Make sure we process requests preemptable */
4964ffc6356SAlexander Graf 			local_irq_enable();
4974ffc6356SAlexander Graf 			kvmppc_check_requests(vcpu);
4984ffc6356SAlexander Graf 			local_irq_disable();
4994ffc6356SAlexander Graf 			continue;
5004ffc6356SAlexander Graf 		}
5014ffc6356SAlexander Graf 
502a8e4ef84SAlexander Graf 		if (kvmppc_core_prepare_to_enter(vcpu)) {
503a8e4ef84SAlexander Graf 			/* interrupts got enabled in between, so we
504a8e4ef84SAlexander Graf 			   are back at square 1 */
505a8e4ef84SAlexander Graf 			continue;
506a8e4ef84SAlexander Graf 		}
507a8e4ef84SAlexander Graf 
508d69c6436SAlexander Graf 		if (vcpu->mode == EXITING_GUEST_MODE) {
509d69c6436SAlexander Graf 			r = 1;
510d69c6436SAlexander Graf 			break;
511d69c6436SAlexander Graf 		}
512d69c6436SAlexander Graf 
513d69c6436SAlexander Graf 		/* Going into guest context! Yay! */
514d69c6436SAlexander Graf 		vcpu->mode = IN_GUEST_MODE;
515d69c6436SAlexander Graf 		smp_wmb();
516d69c6436SAlexander Graf 
517a8e4ef84SAlexander Graf 		break;
518a8e4ef84SAlexander Graf 	}
519a8e4ef84SAlexander Graf 
520a8e4ef84SAlexander Graf 	return r;
521c59a6a3eSScott Wood }
522c59a6a3eSScott Wood 
523df6909e5SPaul Mackerras int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
524df6909e5SPaul Mackerras {
525df6909e5SPaul Mackerras 	int ret;
5268fae845fSScott Wood #ifdef CONFIG_PPC_FPU
5278fae845fSScott Wood 	unsigned int fpscr;
5288fae845fSScott Wood 	int fpexc_mode;
5298fae845fSScott Wood 	u64 fpr[32];
5308fae845fSScott Wood #endif
531df6909e5SPaul Mackerras 
532af8f38b3SAlexander Graf 	if (!vcpu->arch.sane) {
533af8f38b3SAlexander Graf 		kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
534af8f38b3SAlexander Graf 		return -EINVAL;
535af8f38b3SAlexander Graf 	}
536af8f38b3SAlexander Graf 
537df6909e5SPaul Mackerras 	local_irq_disable();
53803660ba2SAlexander Graf 	if (kvmppc_prepare_to_enter(vcpu)) {
5391d1ef222SScott Wood 		kvm_run->exit_reason = KVM_EXIT_INTR;
5401d1ef222SScott Wood 		ret = -EINTR;
5411d1ef222SScott Wood 		goto out;
5421d1ef222SScott Wood 	}
5431d1ef222SScott Wood 
544df6909e5SPaul Mackerras 	kvm_guest_enter();
5458fae845fSScott Wood 
5468fae845fSScott Wood #ifdef CONFIG_PPC_FPU
5478fae845fSScott Wood 	/* Save userspace FPU state in stack */
5488fae845fSScott Wood 	enable_kernel_fp();
5498fae845fSScott Wood 	memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
5508fae845fSScott Wood 	fpscr = current->thread.fpscr.val;
5518fae845fSScott Wood 	fpexc_mode = current->thread.fpexc_mode;
5528fae845fSScott Wood 
5538fae845fSScott Wood 	/* Restore guest FPU state to thread */
5548fae845fSScott Wood 	memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr));
5558fae845fSScott Wood 	current->thread.fpscr.val = vcpu->arch.fpscr;
5568fae845fSScott Wood 
5578fae845fSScott Wood 	/*
5588fae845fSScott Wood 	 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
5598fae845fSScott Wood 	 * as always using the FPU.  Kernel usage of FP (via
5608fae845fSScott Wood 	 * enable_kernel_fp()) in this thread must not occur while
5618fae845fSScott Wood 	 * vcpu->fpu_active is set.
5628fae845fSScott Wood 	 */
5638fae845fSScott Wood 	vcpu->fpu_active = 1;
5648fae845fSScott Wood 
5658fae845fSScott Wood 	kvmppc_load_guest_fp(vcpu);
5668fae845fSScott Wood #endif
5678fae845fSScott Wood 
568df6909e5SPaul Mackerras 	ret = __kvmppc_vcpu_run(kvm_run, vcpu);
5698fae845fSScott Wood 
5708fae845fSScott Wood #ifdef CONFIG_PPC_FPU
5718fae845fSScott Wood 	kvmppc_save_guest_fp(vcpu);
5728fae845fSScott Wood 
5738fae845fSScott Wood 	vcpu->fpu_active = 0;
5748fae845fSScott Wood 
5758fae845fSScott Wood 	/* Save guest FPU state from thread */
5768fae845fSScott Wood 	memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr));
5778fae845fSScott Wood 	vcpu->arch.fpscr = current->thread.fpscr.val;
5788fae845fSScott Wood 
5798fae845fSScott Wood 	/* Restore userspace FPU state from stack */
5808fae845fSScott Wood 	memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
5818fae845fSScott Wood 	current->thread.fpscr.val = fpscr;
5828fae845fSScott Wood 	current->thread.fpexc_mode = fpexc_mode;
5838fae845fSScott Wood #endif
5848fae845fSScott Wood 
585df6909e5SPaul Mackerras 	kvm_guest_exit();
586*862d31f7SAlexander Graf 	vcpu->mode = OUTSIDE_GUEST_MODE;
587*862d31f7SAlexander Graf 	smp_wmb();
588df6909e5SPaul Mackerras 
5891d1ef222SScott Wood out:
590d69c6436SAlexander Graf 	vcpu->mode = OUTSIDE_GUEST_MODE;
591d69c6436SAlexander Graf 	smp_wmb();
5921d1ef222SScott Wood 	local_irq_enable();
593df6909e5SPaul Mackerras 	return ret;
594df6909e5SPaul Mackerras }
595df6909e5SPaul Mackerras 
596d30f6e48SScott Wood static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
597d9fbd03dSHollis Blanchard {
598d9fbd03dSHollis Blanchard 	enum emulation_result er;
599d9fbd03dSHollis Blanchard 
600d9fbd03dSHollis Blanchard 	er = kvmppc_emulate_instruction(run, vcpu);
601d9fbd03dSHollis Blanchard 	switch (er) {
602d9fbd03dSHollis Blanchard 	case EMULATE_DONE:
60373e75b41SHollis Blanchard 		/* don't overwrite subtypes, just account kvm_stats */
6047b701591SHollis Blanchard 		kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
605d9fbd03dSHollis Blanchard 		/* Future optimization: only reload non-volatiles if
606d9fbd03dSHollis Blanchard 		 * they were actually modified by emulation. */
607d30f6e48SScott Wood 		return RESUME_GUEST_NV;
608d30f6e48SScott Wood 
609d9fbd03dSHollis Blanchard 	case EMULATE_DO_DCR:
610d9fbd03dSHollis Blanchard 		run->exit_reason = KVM_EXIT_DCR;
611d30f6e48SScott Wood 		return RESUME_HOST;
612d30f6e48SScott Wood 
613d9fbd03dSHollis Blanchard 	case EMULATE_FAIL:
6145cf8ca22SHollis Blanchard 		printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
615d9fbd03dSHollis Blanchard 		       __func__, vcpu->arch.pc, vcpu->arch.last_inst);
616d9fbd03dSHollis Blanchard 		/* For debugging, encode the failing instruction and
617d9fbd03dSHollis Blanchard 		 * report it to userspace. */
618d9fbd03dSHollis Blanchard 		run->hw.hardware_exit_reason = ~0ULL << 32;
619d9fbd03dSHollis Blanchard 		run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
620d1ff5499SAlexander Graf 		kvmppc_core_queue_program(vcpu, ESR_PIL);
621d30f6e48SScott Wood 		return RESUME_HOST;
622d30f6e48SScott Wood 
623d9fbd03dSHollis Blanchard 	default:
624d9fbd03dSHollis Blanchard 		BUG();
625d9fbd03dSHollis Blanchard 	}
626d30f6e48SScott Wood }
627d30f6e48SScott Wood 
6284e642ccbSAlexander Graf static void kvmppc_fill_pt_regs(struct pt_regs *regs)
6294e642ccbSAlexander Graf {
6304e642ccbSAlexander Graf 	ulong r1, ip, msr, lr;
6314e642ccbSAlexander Graf 
6324e642ccbSAlexander Graf 	asm("mr %0, 1" : "=r"(r1));
6334e642ccbSAlexander Graf 	asm("mflr %0" : "=r"(lr));
6344e642ccbSAlexander Graf 	asm("mfmsr %0" : "=r"(msr));
6354e642ccbSAlexander Graf 	asm("bl 1f; 1: mflr %0" : "=r"(ip));
6364e642ccbSAlexander Graf 
6374e642ccbSAlexander Graf 	memset(regs, 0, sizeof(*regs));
6384e642ccbSAlexander Graf 	regs->gpr[1] = r1;
6394e642ccbSAlexander Graf 	regs->nip = ip;
6404e642ccbSAlexander Graf 	regs->msr = msr;
6414e642ccbSAlexander Graf 	regs->link = lr;
6424e642ccbSAlexander Graf }
6434e642ccbSAlexander Graf 
6446328e593SBharat Bhushan /*
6456328e593SBharat Bhushan  * For interrupts needed to be handled by host interrupt handlers,
6466328e593SBharat Bhushan  * corresponding host handler are called from here in similar way
6476328e593SBharat Bhushan  * (but not exact) as they are called from low level handler
6486328e593SBharat Bhushan  * (such as from arch/powerpc/kernel/head_fsl_booke.S).
6496328e593SBharat Bhushan  */
6504e642ccbSAlexander Graf static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
6514e642ccbSAlexander Graf 				     unsigned int exit_nr)
6524e642ccbSAlexander Graf {
6534e642ccbSAlexander Graf 	struct pt_regs regs;
6544e642ccbSAlexander Graf 
6554e642ccbSAlexander Graf 	switch (exit_nr) {
6564e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_EXTERNAL:
6574e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
6584e642ccbSAlexander Graf 		do_IRQ(&regs);
6594e642ccbSAlexander Graf 		break;
6604e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_DECREMENTER:
6614e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
6624e642ccbSAlexander Graf 		timer_interrupt(&regs);
6634e642ccbSAlexander Graf 		break;
6644e642ccbSAlexander Graf #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64)
6654e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_DOORBELL:
6664e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
6674e642ccbSAlexander Graf 		doorbell_exception(&regs);
6684e642ccbSAlexander Graf 		break;
6694e642ccbSAlexander Graf #endif
6704e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_MACHINE_CHECK:
6714e642ccbSAlexander Graf 		/* FIXME */
6724e642ccbSAlexander Graf 		break;
6737cc1e8eeSAlexander Graf 	case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
6747cc1e8eeSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
6757cc1e8eeSAlexander Graf 		performance_monitor_exception(&regs);
6767cc1e8eeSAlexander Graf 		break;
6776328e593SBharat Bhushan 	case BOOKE_INTERRUPT_WATCHDOG:
6786328e593SBharat Bhushan 		kvmppc_fill_pt_regs(&regs);
6796328e593SBharat Bhushan #ifdef CONFIG_BOOKE_WDT
6806328e593SBharat Bhushan 		WatchdogException(&regs);
6816328e593SBharat Bhushan #else
6826328e593SBharat Bhushan 		unknown_exception(&regs);
6836328e593SBharat Bhushan #endif
6846328e593SBharat Bhushan 		break;
6856328e593SBharat Bhushan 	case BOOKE_INTERRUPT_CRITICAL:
6866328e593SBharat Bhushan 		unknown_exception(&regs);
6876328e593SBharat Bhushan 		break;
6884e642ccbSAlexander Graf 	}
6894e642ccbSAlexander Graf }
6904e642ccbSAlexander Graf 
691d30f6e48SScott Wood /**
692d30f6e48SScott Wood  * kvmppc_handle_exit
693d30f6e48SScott Wood  *
694d30f6e48SScott Wood  * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
695d30f6e48SScott Wood  */
696d30f6e48SScott Wood int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
697d30f6e48SScott Wood                        unsigned int exit_nr)
698d30f6e48SScott Wood {
699d30f6e48SScott Wood 	int r = RESUME_HOST;
700d30f6e48SScott Wood 
701d30f6e48SScott Wood 	/* update before a new last_exit_type is rewritten */
702d30f6e48SScott Wood 	kvmppc_update_timing_stats(vcpu);
703d30f6e48SScott Wood 
7044e642ccbSAlexander Graf 	/* restart interrupts if they were meant for the host */
7054e642ccbSAlexander Graf 	kvmppc_restart_interrupt(vcpu, exit_nr);
706d30f6e48SScott Wood 
707d30f6e48SScott Wood 	local_irq_enable();
708d30f6e48SScott Wood 
70997c95059SAlexander Graf 	trace_kvm_exit(exit_nr, vcpu);
71097c95059SAlexander Graf 
711d30f6e48SScott Wood 	run->exit_reason = KVM_EXIT_UNKNOWN;
712d30f6e48SScott Wood 	run->ready_for_interrupt_injection = 1;
713d30f6e48SScott Wood 
714d30f6e48SScott Wood 	switch (exit_nr) {
715d30f6e48SScott Wood 	case BOOKE_INTERRUPT_MACHINE_CHECK:
716c35c9d84SAlexander Graf 		printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
717c35c9d84SAlexander Graf 		kvmppc_dump_vcpu(vcpu);
718c35c9d84SAlexander Graf 		/* For debugging, send invalid exit reason to user space */
719c35c9d84SAlexander Graf 		run->hw.hardware_exit_reason = ~1ULL << 32;
720c35c9d84SAlexander Graf 		run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
721c35c9d84SAlexander Graf 		r = RESUME_HOST;
722d30f6e48SScott Wood 		break;
723d30f6e48SScott Wood 
724d30f6e48SScott Wood 	case BOOKE_INTERRUPT_EXTERNAL:
725d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
726d30f6e48SScott Wood 		r = RESUME_GUEST;
727d30f6e48SScott Wood 		break;
728d30f6e48SScott Wood 
729d30f6e48SScott Wood 	case BOOKE_INTERRUPT_DECREMENTER:
730d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, DEC_EXITS);
731d30f6e48SScott Wood 		r = RESUME_GUEST;
732d30f6e48SScott Wood 		break;
733d30f6e48SScott Wood 
7346328e593SBharat Bhushan 	case BOOKE_INTERRUPT_WATCHDOG:
7356328e593SBharat Bhushan 		r = RESUME_GUEST;
7366328e593SBharat Bhushan 		break;
7376328e593SBharat Bhushan 
738d30f6e48SScott Wood 	case BOOKE_INTERRUPT_DOORBELL:
739d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, DBELL_EXITS);
740d30f6e48SScott Wood 		r = RESUME_GUEST;
741d30f6e48SScott Wood 		break;
742d30f6e48SScott Wood 
743d30f6e48SScott Wood 	case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
744d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, GDBELL_EXITS);
745d30f6e48SScott Wood 
746d30f6e48SScott Wood 		/*
747d30f6e48SScott Wood 		 * We are here because there is a pending guest interrupt
748d30f6e48SScott Wood 		 * which could not be delivered as MSR_CE or MSR_ME was not
749d30f6e48SScott Wood 		 * set.  Once we break from here we will retry delivery.
750d30f6e48SScott Wood 		 */
751d30f6e48SScott Wood 		r = RESUME_GUEST;
752d30f6e48SScott Wood 		break;
753d30f6e48SScott Wood 
754d30f6e48SScott Wood 	case BOOKE_INTERRUPT_GUEST_DBELL:
755d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, GDBELL_EXITS);
756d30f6e48SScott Wood 
757d30f6e48SScott Wood 		/*
758d30f6e48SScott Wood 		 * We are here because there is a pending guest interrupt
759d30f6e48SScott Wood 		 * which could not be delivered as MSR_EE was not set.  Once
760d30f6e48SScott Wood 		 * we break from here we will retry delivery.
761d30f6e48SScott Wood 		 */
762d30f6e48SScott Wood 		r = RESUME_GUEST;
763d30f6e48SScott Wood 		break;
764d30f6e48SScott Wood 
76595f2e921SAlexander Graf 	case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
76695f2e921SAlexander Graf 		r = RESUME_GUEST;
76795f2e921SAlexander Graf 		break;
76895f2e921SAlexander Graf 
769d30f6e48SScott Wood 	case BOOKE_INTERRUPT_HV_PRIV:
770d30f6e48SScott Wood 		r = emulation_exit(run, vcpu);
771d30f6e48SScott Wood 		break;
772d30f6e48SScott Wood 
773d30f6e48SScott Wood 	case BOOKE_INTERRUPT_PROGRAM:
774d30f6e48SScott Wood 		if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
7750268597cSAlexander Graf 			/*
7760268597cSAlexander Graf 			 * Program traps generated by user-level software must
7770268597cSAlexander Graf 			 * be handled by the guest kernel.
7780268597cSAlexander Graf 			 *
7790268597cSAlexander Graf 			 * In GS mode, hypervisor privileged instructions trap
7800268597cSAlexander Graf 			 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
7810268597cSAlexander Graf 			 * actual program interrupts, handled by the guest.
7820268597cSAlexander Graf 			 */
783d30f6e48SScott Wood 			kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
784d30f6e48SScott Wood 			r = RESUME_GUEST;
785d30f6e48SScott Wood 			kvmppc_account_exit(vcpu, USR_PR_INST);
786d30f6e48SScott Wood 			break;
787d30f6e48SScott Wood 		}
788d30f6e48SScott Wood 
789d30f6e48SScott Wood 		r = emulation_exit(run, vcpu);
790d9fbd03dSHollis Blanchard 		break;
791d9fbd03dSHollis Blanchard 
792d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_FP_UNAVAIL:
793d4cf3892SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
7947b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, FP_UNAVAIL);
795d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
796d9fbd03dSHollis Blanchard 		break;
797d9fbd03dSHollis Blanchard 
7984cd35f67SScott Wood #ifdef CONFIG_SPE
7994cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_UNAVAIL: {
8004cd35f67SScott Wood 		if (vcpu->arch.shared->msr & MSR_SPE)
8014cd35f67SScott Wood 			kvmppc_vcpu_enable_spe(vcpu);
8024cd35f67SScott Wood 		else
8034cd35f67SScott Wood 			kvmppc_booke_queue_irqprio(vcpu,
8044cd35f67SScott Wood 						   BOOKE_IRQPRIO_SPE_UNAVAIL);
805bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
806bb3a8a17SHollis Blanchard 		break;
8074cd35f67SScott Wood 	}
808bb3a8a17SHollis Blanchard 
809bb3a8a17SHollis Blanchard 	case BOOKE_INTERRUPT_SPE_FP_DATA:
810bb3a8a17SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
811bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
812bb3a8a17SHollis Blanchard 		break;
813bb3a8a17SHollis Blanchard 
814bb3a8a17SHollis Blanchard 	case BOOKE_INTERRUPT_SPE_FP_ROUND:
815bb3a8a17SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
816bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
817bb3a8a17SHollis Blanchard 		break;
8184cd35f67SScott Wood #else
8194cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_UNAVAIL:
8204cd35f67SScott Wood 		/*
8214cd35f67SScott Wood 		 * Guest wants SPE, but host kernel doesn't support it.  Send
8224cd35f67SScott Wood 		 * an "unimplemented operation" program check to the guest.
8234cd35f67SScott Wood 		 */
8244cd35f67SScott Wood 		kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
8254cd35f67SScott Wood 		r = RESUME_GUEST;
8264cd35f67SScott Wood 		break;
8274cd35f67SScott Wood 
8284cd35f67SScott Wood 	/*
8294cd35f67SScott Wood 	 * These really should never happen without CONFIG_SPE,
8304cd35f67SScott Wood 	 * as we should never enable the real MSR[SPE] in the guest.
8314cd35f67SScott Wood 	 */
8324cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_FP_DATA:
8334cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_FP_ROUND:
8344cd35f67SScott Wood 		printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
8354cd35f67SScott Wood 		       __func__, exit_nr, vcpu->arch.pc);
8364cd35f67SScott Wood 		run->hw.hardware_exit_reason = exit_nr;
8374cd35f67SScott Wood 		r = RESUME_HOST;
8384cd35f67SScott Wood 		break;
8394cd35f67SScott Wood #endif
840bb3a8a17SHollis Blanchard 
841d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DATA_STORAGE:
842daf5e271SLiu Yu 		kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
843daf5e271SLiu Yu 		                               vcpu->arch.fault_esr);
8447b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, DSI_EXITS);
845d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
846d9fbd03dSHollis Blanchard 		break;
847d9fbd03dSHollis Blanchard 
848d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_INST_STORAGE:
849daf5e271SLiu Yu 		kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
8507b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, ISI_EXITS);
851d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
852d9fbd03dSHollis Blanchard 		break;
853d9fbd03dSHollis Blanchard 
854d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
855d30f6e48SScott Wood 	case BOOKE_INTERRUPT_HV_SYSCALL:
856d30f6e48SScott Wood 		if (!(vcpu->arch.shared->msr & MSR_PR)) {
857d30f6e48SScott Wood 			kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
858d30f6e48SScott Wood 		} else {
859d30f6e48SScott Wood 			/*
860d30f6e48SScott Wood 			 * hcall from guest userspace -- send privileged
861d30f6e48SScott Wood 			 * instruction program check.
862d30f6e48SScott Wood 			 */
863d30f6e48SScott Wood 			kvmppc_core_queue_program(vcpu, ESR_PPR);
864d30f6e48SScott Wood 		}
865d30f6e48SScott Wood 
866d30f6e48SScott Wood 		r = RESUME_GUEST;
867d30f6e48SScott Wood 		break;
868d30f6e48SScott Wood #else
869d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_SYSCALL:
8702a342ed5SAlexander Graf 		if (!(vcpu->arch.shared->msr & MSR_PR) &&
8712a342ed5SAlexander Graf 		    (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
8722a342ed5SAlexander Graf 			/* KVM PV hypercalls */
8732a342ed5SAlexander Graf 			kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
8742a342ed5SAlexander Graf 			r = RESUME_GUEST;
8752a342ed5SAlexander Graf 		} else {
8762a342ed5SAlexander Graf 			/* Guest syscalls */
877d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
8782a342ed5SAlexander Graf 		}
8797b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, SYSCALL_EXITS);
880d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
881d9fbd03dSHollis Blanchard 		break;
882d30f6e48SScott Wood #endif
883d9fbd03dSHollis Blanchard 
884d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DTLB_MISS: {
885d9fbd03dSHollis Blanchard 		unsigned long eaddr = vcpu->arch.fault_dear;
8867924bd41SHollis Blanchard 		int gtlb_index;
887475e7cddSHollis Blanchard 		gpa_t gpaddr;
888d9fbd03dSHollis Blanchard 		gfn_t gfn;
889d9fbd03dSHollis Blanchard 
890bf7ca4bdSAlexander Graf #ifdef CONFIG_KVM_E500V2
891a4cd8b23SScott Wood 		if (!(vcpu->arch.shared->msr & MSR_PR) &&
892a4cd8b23SScott Wood 		    (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
893a4cd8b23SScott Wood 			kvmppc_map_magic(vcpu);
894a4cd8b23SScott Wood 			kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
895a4cd8b23SScott Wood 			r = RESUME_GUEST;
896a4cd8b23SScott Wood 
897a4cd8b23SScott Wood 			break;
898a4cd8b23SScott Wood 		}
899a4cd8b23SScott Wood #endif
900a4cd8b23SScott Wood 
901d9fbd03dSHollis Blanchard 		/* Check the guest TLB. */
902fa86b8ddSHollis Blanchard 		gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
9037924bd41SHollis Blanchard 		if (gtlb_index < 0) {
904d9fbd03dSHollis Blanchard 			/* The guest didn't have a mapping for it. */
905daf5e271SLiu Yu 			kvmppc_core_queue_dtlb_miss(vcpu,
906daf5e271SLiu Yu 			                            vcpu->arch.fault_dear,
907daf5e271SLiu Yu 			                            vcpu->arch.fault_esr);
908b52a638cSHollis Blanchard 			kvmppc_mmu_dtlb_miss(vcpu);
9097b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
910d9fbd03dSHollis Blanchard 			r = RESUME_GUEST;
911d9fbd03dSHollis Blanchard 			break;
912d9fbd03dSHollis Blanchard 		}
913d9fbd03dSHollis Blanchard 
914be8d1caeSHollis Blanchard 		gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
915475e7cddSHollis Blanchard 		gfn = gpaddr >> PAGE_SHIFT;
916d9fbd03dSHollis Blanchard 
917d9fbd03dSHollis Blanchard 		if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
918d9fbd03dSHollis Blanchard 			/* The guest TLB had a mapping, but the shadow TLB
919d9fbd03dSHollis Blanchard 			 * didn't, and it is RAM. This could be because:
920d9fbd03dSHollis Blanchard 			 * a) the entry is mapping the host kernel, or
921d9fbd03dSHollis Blanchard 			 * b) the guest used a large mapping which we're faking
922d9fbd03dSHollis Blanchard 			 * Either way, we need to satisfy the fault without
923d9fbd03dSHollis Blanchard 			 * invoking the guest. */
92458a96214SHollis Blanchard 			kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
9257b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
926d9fbd03dSHollis Blanchard 			r = RESUME_GUEST;
927d9fbd03dSHollis Blanchard 		} else {
928d9fbd03dSHollis Blanchard 			/* Guest has mapped and accessed a page which is not
929d9fbd03dSHollis Blanchard 			 * actually RAM. */
930475e7cddSHollis Blanchard 			vcpu->arch.paddr_accessed = gpaddr;
9316020c0f6SAlexander Graf 			vcpu->arch.vaddr_accessed = eaddr;
932d9fbd03dSHollis Blanchard 			r = kvmppc_emulate_mmio(run, vcpu);
9337b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, MMIO_EXITS);
934d9fbd03dSHollis Blanchard 		}
935d9fbd03dSHollis Blanchard 
936d9fbd03dSHollis Blanchard 		break;
937d9fbd03dSHollis Blanchard 	}
938d9fbd03dSHollis Blanchard 
939d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_ITLB_MISS: {
940d9fbd03dSHollis Blanchard 		unsigned long eaddr = vcpu->arch.pc;
94189168618SHollis Blanchard 		gpa_t gpaddr;
942d9fbd03dSHollis Blanchard 		gfn_t gfn;
9437924bd41SHollis Blanchard 		int gtlb_index;
944d9fbd03dSHollis Blanchard 
945d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
946d9fbd03dSHollis Blanchard 
947d9fbd03dSHollis Blanchard 		/* Check the guest TLB. */
948fa86b8ddSHollis Blanchard 		gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
9497924bd41SHollis Blanchard 		if (gtlb_index < 0) {
950d9fbd03dSHollis Blanchard 			/* The guest didn't have a mapping for it. */
951d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
952b52a638cSHollis Blanchard 			kvmppc_mmu_itlb_miss(vcpu);
9537b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
954d9fbd03dSHollis Blanchard 			break;
955d9fbd03dSHollis Blanchard 		}
956d9fbd03dSHollis Blanchard 
9577b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
958d9fbd03dSHollis Blanchard 
959be8d1caeSHollis Blanchard 		gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
96089168618SHollis Blanchard 		gfn = gpaddr >> PAGE_SHIFT;
961d9fbd03dSHollis Blanchard 
962d9fbd03dSHollis Blanchard 		if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
963d9fbd03dSHollis Blanchard 			/* The guest TLB had a mapping, but the shadow TLB
964d9fbd03dSHollis Blanchard 			 * didn't. This could be because:
965d9fbd03dSHollis Blanchard 			 * a) the entry is mapping the host kernel, or
966d9fbd03dSHollis Blanchard 			 * b) the guest used a large mapping which we're faking
967d9fbd03dSHollis Blanchard 			 * Either way, we need to satisfy the fault without
968d9fbd03dSHollis Blanchard 			 * invoking the guest. */
96958a96214SHollis Blanchard 			kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
970d9fbd03dSHollis Blanchard 		} else {
971d9fbd03dSHollis Blanchard 			/* Guest mapped and leaped at non-RAM! */
972d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
973d9fbd03dSHollis Blanchard 		}
974d9fbd03dSHollis Blanchard 
975d9fbd03dSHollis Blanchard 		break;
976d9fbd03dSHollis Blanchard 	}
977d9fbd03dSHollis Blanchard 
978d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DEBUG: {
979d9fbd03dSHollis Blanchard 		u32 dbsr;
980d9fbd03dSHollis Blanchard 
981d9fbd03dSHollis Blanchard 		vcpu->arch.pc = mfspr(SPRN_CSRR0);
982d9fbd03dSHollis Blanchard 
983d9fbd03dSHollis Blanchard 		/* clear IAC events in DBSR register */
984d9fbd03dSHollis Blanchard 		dbsr = mfspr(SPRN_DBSR);
985d9fbd03dSHollis Blanchard 		dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
986d9fbd03dSHollis Blanchard 		mtspr(SPRN_DBSR, dbsr);
987d9fbd03dSHollis Blanchard 
988d9fbd03dSHollis Blanchard 		run->exit_reason = KVM_EXIT_DEBUG;
9897b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, DEBUG_EXITS);
990d9fbd03dSHollis Blanchard 		r = RESUME_HOST;
991d9fbd03dSHollis Blanchard 		break;
992d9fbd03dSHollis Blanchard 	}
993d9fbd03dSHollis Blanchard 
994d9fbd03dSHollis Blanchard 	default:
995d9fbd03dSHollis Blanchard 		printk(KERN_EMERG "exit_nr %d\n", exit_nr);
996d9fbd03dSHollis Blanchard 		BUG();
997d9fbd03dSHollis Blanchard 	}
998d9fbd03dSHollis Blanchard 
999a8e4ef84SAlexander Graf 	/*
1000a8e4ef84SAlexander Graf 	 * To avoid clobbering exit_reason, only check for signals if we
1001a8e4ef84SAlexander Graf 	 * aren't already exiting to userspace for some other reason.
1002a8e4ef84SAlexander Graf 	 */
100303660ba2SAlexander Graf 	if (!(r & RESUME_HOST)) {
1004d9fbd03dSHollis Blanchard 		local_irq_disable();
100503660ba2SAlexander Graf 		if (kvmppc_prepare_to_enter(vcpu)) {
1006d9fbd03dSHollis Blanchard 			run->exit_reason = KVM_EXIT_INTR;
1007d9fbd03dSHollis Blanchard 			r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
10087b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, SIGNAL_EXITS);
1009d9fbd03dSHollis Blanchard 		}
101003660ba2SAlexander Graf 	}
1011d9fbd03dSHollis Blanchard 
1012d9fbd03dSHollis Blanchard 	return r;
1013d9fbd03dSHollis Blanchard }
1014d9fbd03dSHollis Blanchard 
1015d9fbd03dSHollis Blanchard /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
1016d9fbd03dSHollis Blanchard int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1017d9fbd03dSHollis Blanchard {
1018082decf2SHollis Blanchard 	int i;
1019af8f38b3SAlexander Graf 	int r;
1020082decf2SHollis Blanchard 
1021d9fbd03dSHollis Blanchard 	vcpu->arch.pc = 0;
1022b5904972SScott Wood 	vcpu->arch.shared->pir = vcpu->vcpu_id;
10238e5b26b5SAlexander Graf 	kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
1024d30f6e48SScott Wood 	kvmppc_set_msr(vcpu, 0);
1025d9fbd03dSHollis Blanchard 
1026d30f6e48SScott Wood #ifndef CONFIG_KVM_BOOKE_HV
1027d30f6e48SScott Wood 	vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
1028d9fbd03dSHollis Blanchard 	vcpu->arch.shadow_pid = 1;
1029d30f6e48SScott Wood 	vcpu->arch.shared->msr = 0;
1030d30f6e48SScott Wood #endif
1031d9fbd03dSHollis Blanchard 
1032082decf2SHollis Blanchard 	/* Eye-catching numbers so we know if the guest takes an interrupt
1033082decf2SHollis Blanchard 	 * before it's programmed its own IVPR/IVORs. */
1034d9fbd03dSHollis Blanchard 	vcpu->arch.ivpr = 0x55550000;
1035082decf2SHollis Blanchard 	for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
1036082decf2SHollis Blanchard 		vcpu->arch.ivor[i] = 0x7700 | i * 4;
1037d9fbd03dSHollis Blanchard 
103873e75b41SHollis Blanchard 	kvmppc_init_timing_stats(vcpu);
103973e75b41SHollis Blanchard 
1040af8f38b3SAlexander Graf 	r = kvmppc_core_vcpu_setup(vcpu);
1041af8f38b3SAlexander Graf 	kvmppc_sanity_check(vcpu);
1042af8f38b3SAlexander Graf 	return r;
1043d9fbd03dSHollis Blanchard }
1044d9fbd03dSHollis Blanchard 
1045d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1046d9fbd03dSHollis Blanchard {
1047d9fbd03dSHollis Blanchard 	int i;
1048d9fbd03dSHollis Blanchard 
1049d9fbd03dSHollis Blanchard 	regs->pc = vcpu->arch.pc;
1050992b5b29SAlexander Graf 	regs->cr = kvmppc_get_cr(vcpu);
1051d9fbd03dSHollis Blanchard 	regs->ctr = vcpu->arch.ctr;
1052d9fbd03dSHollis Blanchard 	regs->lr = vcpu->arch.lr;
1053992b5b29SAlexander Graf 	regs->xer = kvmppc_get_xer(vcpu);
1054666e7252SAlexander Graf 	regs->msr = vcpu->arch.shared->msr;
1055de7906c3SAlexander Graf 	regs->srr0 = vcpu->arch.shared->srr0;
1056de7906c3SAlexander Graf 	regs->srr1 = vcpu->arch.shared->srr1;
1057d9fbd03dSHollis Blanchard 	regs->pid = vcpu->arch.pid;
1058a73a9599SAlexander Graf 	regs->sprg0 = vcpu->arch.shared->sprg0;
1059a73a9599SAlexander Graf 	regs->sprg1 = vcpu->arch.shared->sprg1;
1060a73a9599SAlexander Graf 	regs->sprg2 = vcpu->arch.shared->sprg2;
1061a73a9599SAlexander Graf 	regs->sprg3 = vcpu->arch.shared->sprg3;
1062b5904972SScott Wood 	regs->sprg4 = vcpu->arch.shared->sprg4;
1063b5904972SScott Wood 	regs->sprg5 = vcpu->arch.shared->sprg5;
1064b5904972SScott Wood 	regs->sprg6 = vcpu->arch.shared->sprg6;
1065b5904972SScott Wood 	regs->sprg7 = vcpu->arch.shared->sprg7;
1066d9fbd03dSHollis Blanchard 
1067d9fbd03dSHollis Blanchard 	for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
10688e5b26b5SAlexander Graf 		regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
1069d9fbd03dSHollis Blanchard 
1070d9fbd03dSHollis Blanchard 	return 0;
1071d9fbd03dSHollis Blanchard }
1072d9fbd03dSHollis Blanchard 
1073d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1074d9fbd03dSHollis Blanchard {
1075d9fbd03dSHollis Blanchard 	int i;
1076d9fbd03dSHollis Blanchard 
1077d9fbd03dSHollis Blanchard 	vcpu->arch.pc = regs->pc;
1078992b5b29SAlexander Graf 	kvmppc_set_cr(vcpu, regs->cr);
1079d9fbd03dSHollis Blanchard 	vcpu->arch.ctr = regs->ctr;
1080d9fbd03dSHollis Blanchard 	vcpu->arch.lr = regs->lr;
1081992b5b29SAlexander Graf 	kvmppc_set_xer(vcpu, regs->xer);
1082b8fd68acSHollis Blanchard 	kvmppc_set_msr(vcpu, regs->msr);
1083de7906c3SAlexander Graf 	vcpu->arch.shared->srr0 = regs->srr0;
1084de7906c3SAlexander Graf 	vcpu->arch.shared->srr1 = regs->srr1;
10855ce941eeSScott Wood 	kvmppc_set_pid(vcpu, regs->pid);
1086a73a9599SAlexander Graf 	vcpu->arch.shared->sprg0 = regs->sprg0;
1087a73a9599SAlexander Graf 	vcpu->arch.shared->sprg1 = regs->sprg1;
1088a73a9599SAlexander Graf 	vcpu->arch.shared->sprg2 = regs->sprg2;
1089a73a9599SAlexander Graf 	vcpu->arch.shared->sprg3 = regs->sprg3;
1090b5904972SScott Wood 	vcpu->arch.shared->sprg4 = regs->sprg4;
1091b5904972SScott Wood 	vcpu->arch.shared->sprg5 = regs->sprg5;
1092b5904972SScott Wood 	vcpu->arch.shared->sprg6 = regs->sprg6;
1093b5904972SScott Wood 	vcpu->arch.shared->sprg7 = regs->sprg7;
1094d9fbd03dSHollis Blanchard 
10958e5b26b5SAlexander Graf 	for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
10968e5b26b5SAlexander Graf 		kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
1097d9fbd03dSHollis Blanchard 
1098d9fbd03dSHollis Blanchard 	return 0;
1099d9fbd03dSHollis Blanchard }
1100d9fbd03dSHollis Blanchard 
11015ce941eeSScott Wood static void get_sregs_base(struct kvm_vcpu *vcpu,
11025ce941eeSScott Wood                            struct kvm_sregs *sregs)
11035ce941eeSScott Wood {
11045ce941eeSScott Wood 	u64 tb = get_tb();
11055ce941eeSScott Wood 
11065ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_BASE;
11075ce941eeSScott Wood 
11085ce941eeSScott Wood 	sregs->u.e.csrr0 = vcpu->arch.csrr0;
11095ce941eeSScott Wood 	sregs->u.e.csrr1 = vcpu->arch.csrr1;
11105ce941eeSScott Wood 	sregs->u.e.mcsr = vcpu->arch.mcsr;
1111d30f6e48SScott Wood 	sregs->u.e.esr = get_guest_esr(vcpu);
1112d30f6e48SScott Wood 	sregs->u.e.dear = get_guest_dear(vcpu);
11135ce941eeSScott Wood 	sregs->u.e.tsr = vcpu->arch.tsr;
11145ce941eeSScott Wood 	sregs->u.e.tcr = vcpu->arch.tcr;
11155ce941eeSScott Wood 	sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
11165ce941eeSScott Wood 	sregs->u.e.tb = tb;
11175ce941eeSScott Wood 	sregs->u.e.vrsave = vcpu->arch.vrsave;
11185ce941eeSScott Wood }
11195ce941eeSScott Wood 
11205ce941eeSScott Wood static int set_sregs_base(struct kvm_vcpu *vcpu,
11215ce941eeSScott Wood                           struct kvm_sregs *sregs)
11225ce941eeSScott Wood {
11235ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
11245ce941eeSScott Wood 		return 0;
11255ce941eeSScott Wood 
11265ce941eeSScott Wood 	vcpu->arch.csrr0 = sregs->u.e.csrr0;
11275ce941eeSScott Wood 	vcpu->arch.csrr1 = sregs->u.e.csrr1;
11285ce941eeSScott Wood 	vcpu->arch.mcsr = sregs->u.e.mcsr;
1129d30f6e48SScott Wood 	set_guest_esr(vcpu, sregs->u.e.esr);
1130d30f6e48SScott Wood 	set_guest_dear(vcpu, sregs->u.e.dear);
11315ce941eeSScott Wood 	vcpu->arch.vrsave = sregs->u.e.vrsave;
1132dfd4d47eSScott Wood 	kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
11335ce941eeSScott Wood 
1134dfd4d47eSScott Wood 	if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
11355ce941eeSScott Wood 		vcpu->arch.dec = sregs->u.e.dec;
11365ce941eeSScott Wood 		kvmppc_emulate_dec(vcpu);
1137dfd4d47eSScott Wood 	}
11385ce941eeSScott Wood 
11395ce941eeSScott Wood 	if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
1140dfd4d47eSScott Wood 		vcpu->arch.tsr = sregs->u.e.tsr;
1141dfd4d47eSScott Wood 		update_timer_ints(vcpu);
11425ce941eeSScott Wood 	}
11435ce941eeSScott Wood 
11445ce941eeSScott Wood 	return 0;
11455ce941eeSScott Wood }
11465ce941eeSScott Wood 
11475ce941eeSScott Wood static void get_sregs_arch206(struct kvm_vcpu *vcpu,
11485ce941eeSScott Wood                               struct kvm_sregs *sregs)
11495ce941eeSScott Wood {
11505ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_ARCH206;
11515ce941eeSScott Wood 
1152841741f2SScott Wood 	sregs->u.e.pir = vcpu->vcpu_id;
11535ce941eeSScott Wood 	sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
11545ce941eeSScott Wood 	sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
11555ce941eeSScott Wood 	sregs->u.e.decar = vcpu->arch.decar;
11565ce941eeSScott Wood 	sregs->u.e.ivpr = vcpu->arch.ivpr;
11575ce941eeSScott Wood }
11585ce941eeSScott Wood 
11595ce941eeSScott Wood static int set_sregs_arch206(struct kvm_vcpu *vcpu,
11605ce941eeSScott Wood                              struct kvm_sregs *sregs)
11615ce941eeSScott Wood {
11625ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
11635ce941eeSScott Wood 		return 0;
11645ce941eeSScott Wood 
1165841741f2SScott Wood 	if (sregs->u.e.pir != vcpu->vcpu_id)
11665ce941eeSScott Wood 		return -EINVAL;
11675ce941eeSScott Wood 
11685ce941eeSScott Wood 	vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
11695ce941eeSScott Wood 	vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
11705ce941eeSScott Wood 	vcpu->arch.decar = sregs->u.e.decar;
11715ce941eeSScott Wood 	vcpu->arch.ivpr = sregs->u.e.ivpr;
11725ce941eeSScott Wood 
11735ce941eeSScott Wood 	return 0;
11745ce941eeSScott Wood }
11755ce941eeSScott Wood 
11765ce941eeSScott Wood void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
11775ce941eeSScott Wood {
11785ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_IVOR;
11795ce941eeSScott Wood 
11805ce941eeSScott Wood 	sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
11815ce941eeSScott Wood 	sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
11825ce941eeSScott Wood 	sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
11835ce941eeSScott Wood 	sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
11845ce941eeSScott Wood 	sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
11855ce941eeSScott Wood 	sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
11865ce941eeSScott Wood 	sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
11875ce941eeSScott Wood 	sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
11885ce941eeSScott Wood 	sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
11895ce941eeSScott Wood 	sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
11905ce941eeSScott Wood 	sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
11915ce941eeSScott Wood 	sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
11925ce941eeSScott Wood 	sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
11935ce941eeSScott Wood 	sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
11945ce941eeSScott Wood 	sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
11955ce941eeSScott Wood 	sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
11965ce941eeSScott Wood }
11975ce941eeSScott Wood 
11985ce941eeSScott Wood int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
11995ce941eeSScott Wood {
12005ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
12015ce941eeSScott Wood 		return 0;
12025ce941eeSScott Wood 
12035ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
12045ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
12055ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
12065ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
12075ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
12085ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
12095ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
12105ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
12115ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
12125ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
12135ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
12145ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
12155ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
12165ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
12175ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
12185ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
12195ce941eeSScott Wood 
12205ce941eeSScott Wood 	return 0;
12215ce941eeSScott Wood }
12225ce941eeSScott Wood 
1223d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1224d9fbd03dSHollis Blanchard                                   struct kvm_sregs *sregs)
1225d9fbd03dSHollis Blanchard {
12265ce941eeSScott Wood 	sregs->pvr = vcpu->arch.pvr;
12275ce941eeSScott Wood 
12285ce941eeSScott Wood 	get_sregs_base(vcpu, sregs);
12295ce941eeSScott Wood 	get_sregs_arch206(vcpu, sregs);
12305ce941eeSScott Wood 	kvmppc_core_get_sregs(vcpu, sregs);
12315ce941eeSScott Wood 	return 0;
1232d9fbd03dSHollis Blanchard }
1233d9fbd03dSHollis Blanchard 
1234d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1235d9fbd03dSHollis Blanchard                                   struct kvm_sregs *sregs)
1236d9fbd03dSHollis Blanchard {
12375ce941eeSScott Wood 	int ret;
12385ce941eeSScott Wood 
12395ce941eeSScott Wood 	if (vcpu->arch.pvr != sregs->pvr)
12405ce941eeSScott Wood 		return -EINVAL;
12415ce941eeSScott Wood 
12425ce941eeSScott Wood 	ret = set_sregs_base(vcpu, sregs);
12435ce941eeSScott Wood 	if (ret < 0)
12445ce941eeSScott Wood 		return ret;
12455ce941eeSScott Wood 
12465ce941eeSScott Wood 	ret = set_sregs_arch206(vcpu, sregs);
12475ce941eeSScott Wood 	if (ret < 0)
12485ce941eeSScott Wood 		return ret;
12495ce941eeSScott Wood 
12505ce941eeSScott Wood 	return kvmppc_core_set_sregs(vcpu, sregs);
1251d9fbd03dSHollis Blanchard }
1252d9fbd03dSHollis Blanchard 
125331f3438eSPaul Mackerras int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
125431f3438eSPaul Mackerras {
125531f3438eSPaul Mackerras 	return -EINVAL;
125631f3438eSPaul Mackerras }
125731f3438eSPaul Mackerras 
125831f3438eSPaul Mackerras int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
125931f3438eSPaul Mackerras {
126031f3438eSPaul Mackerras 	return -EINVAL;
126131f3438eSPaul Mackerras }
126231f3438eSPaul Mackerras 
1263d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1264d9fbd03dSHollis Blanchard {
1265d9fbd03dSHollis Blanchard 	return -ENOTSUPP;
1266d9fbd03dSHollis Blanchard }
1267d9fbd03dSHollis Blanchard 
1268d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1269d9fbd03dSHollis Blanchard {
1270d9fbd03dSHollis Blanchard 	return -ENOTSUPP;
1271d9fbd03dSHollis Blanchard }
1272d9fbd03dSHollis Blanchard 
1273d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1274d9fbd03dSHollis Blanchard                                   struct kvm_translation *tr)
1275d9fbd03dSHollis Blanchard {
127698001d8dSAvi Kivity 	int r;
127798001d8dSAvi Kivity 
127898001d8dSAvi Kivity 	r = kvmppc_core_vcpu_translate(vcpu, tr);
127998001d8dSAvi Kivity 	return r;
1280d9fbd03dSHollis Blanchard }
1281d9fbd03dSHollis Blanchard 
12824e755758SAlexander Graf int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
12834e755758SAlexander Graf {
12844e755758SAlexander Graf 	return -ENOTSUPP;
12854e755758SAlexander Graf }
12864e755758SAlexander Graf 
1287f9e0554dSPaul Mackerras int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1288f9e0554dSPaul Mackerras 				      struct kvm_userspace_memory_region *mem)
1289f9e0554dSPaul Mackerras {
1290f9e0554dSPaul Mackerras 	return 0;
1291f9e0554dSPaul Mackerras }
1292f9e0554dSPaul Mackerras 
1293f9e0554dSPaul Mackerras void kvmppc_core_commit_memory_region(struct kvm *kvm,
1294f9e0554dSPaul Mackerras 				struct kvm_userspace_memory_region *mem)
1295f9e0554dSPaul Mackerras {
1296f9e0554dSPaul Mackerras }
1297f9e0554dSPaul Mackerras 
1298dfd4d47eSScott Wood void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1299dfd4d47eSScott Wood {
1300dfd4d47eSScott Wood 	vcpu->arch.tcr = new_tcr;
1301dfd4d47eSScott Wood 	update_timer_ints(vcpu);
1302dfd4d47eSScott Wood }
1303dfd4d47eSScott Wood 
1304dfd4d47eSScott Wood void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1305dfd4d47eSScott Wood {
1306dfd4d47eSScott Wood 	set_bits(tsr_bits, &vcpu->arch.tsr);
1307dfd4d47eSScott Wood 	smp_wmb();
1308dfd4d47eSScott Wood 	kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1309dfd4d47eSScott Wood 	kvm_vcpu_kick(vcpu);
1310dfd4d47eSScott Wood }
1311dfd4d47eSScott Wood 
1312dfd4d47eSScott Wood void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1313dfd4d47eSScott Wood {
1314dfd4d47eSScott Wood 	clear_bits(tsr_bits, &vcpu->arch.tsr);
1315dfd4d47eSScott Wood 	update_timer_ints(vcpu);
1316dfd4d47eSScott Wood }
1317dfd4d47eSScott Wood 
1318dfd4d47eSScott Wood void kvmppc_decrementer_func(unsigned long data)
1319dfd4d47eSScott Wood {
1320dfd4d47eSScott Wood 	struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1321dfd4d47eSScott Wood 
132221bd000aSBharat Bhushan 	if (vcpu->arch.tcr & TCR_ARE) {
132321bd000aSBharat Bhushan 		vcpu->arch.dec = vcpu->arch.decar;
132421bd000aSBharat Bhushan 		kvmppc_emulate_dec(vcpu);
132521bd000aSBharat Bhushan 	}
132621bd000aSBharat Bhushan 
1327dfd4d47eSScott Wood 	kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1328dfd4d47eSScott Wood }
1329dfd4d47eSScott Wood 
133094fa9d99SScott Wood void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
133194fa9d99SScott Wood {
1332d30f6e48SScott Wood 	current->thread.kvm_vcpu = vcpu;
133394fa9d99SScott Wood }
133494fa9d99SScott Wood 
133594fa9d99SScott Wood void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
133694fa9d99SScott Wood {
1337d30f6e48SScott Wood 	current->thread.kvm_vcpu = NULL;
133894fa9d99SScott Wood }
133994fa9d99SScott Wood 
13402986b8c7SStephen Rothwell int __init kvmppc_booke_init(void)
1341d9fbd03dSHollis Blanchard {
1342d30f6e48SScott Wood #ifndef CONFIG_KVM_BOOKE_HV
1343d9fbd03dSHollis Blanchard 	unsigned long ivor[16];
1344d9fbd03dSHollis Blanchard 	unsigned long max_ivor = 0;
1345d9fbd03dSHollis Blanchard 	int i;
1346d9fbd03dSHollis Blanchard 
1347d9fbd03dSHollis Blanchard 	/* We install our own exception handlers by hijacking IVPR. IVPR must
1348d9fbd03dSHollis Blanchard 	 * be 16-bit aligned, so we need a 64KB allocation. */
1349d9fbd03dSHollis Blanchard 	kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1350d9fbd03dSHollis Blanchard 	                                         VCPU_SIZE_ORDER);
1351d9fbd03dSHollis Blanchard 	if (!kvmppc_booke_handlers)
1352d9fbd03dSHollis Blanchard 		return -ENOMEM;
1353d9fbd03dSHollis Blanchard 
1354d9fbd03dSHollis Blanchard 	/* XXX make sure our handlers are smaller than Linux's */
1355d9fbd03dSHollis Blanchard 
1356d9fbd03dSHollis Blanchard 	/* Copy our interrupt handlers to match host IVORs. That way we don't
1357d9fbd03dSHollis Blanchard 	 * have to swap the IVORs on every guest/host transition. */
1358d9fbd03dSHollis Blanchard 	ivor[0] = mfspr(SPRN_IVOR0);
1359d9fbd03dSHollis Blanchard 	ivor[1] = mfspr(SPRN_IVOR1);
1360d9fbd03dSHollis Blanchard 	ivor[2] = mfspr(SPRN_IVOR2);
1361d9fbd03dSHollis Blanchard 	ivor[3] = mfspr(SPRN_IVOR3);
1362d9fbd03dSHollis Blanchard 	ivor[4] = mfspr(SPRN_IVOR4);
1363d9fbd03dSHollis Blanchard 	ivor[5] = mfspr(SPRN_IVOR5);
1364d9fbd03dSHollis Blanchard 	ivor[6] = mfspr(SPRN_IVOR6);
1365d9fbd03dSHollis Blanchard 	ivor[7] = mfspr(SPRN_IVOR7);
1366d9fbd03dSHollis Blanchard 	ivor[8] = mfspr(SPRN_IVOR8);
1367d9fbd03dSHollis Blanchard 	ivor[9] = mfspr(SPRN_IVOR9);
1368d9fbd03dSHollis Blanchard 	ivor[10] = mfspr(SPRN_IVOR10);
1369d9fbd03dSHollis Blanchard 	ivor[11] = mfspr(SPRN_IVOR11);
1370d9fbd03dSHollis Blanchard 	ivor[12] = mfspr(SPRN_IVOR12);
1371d9fbd03dSHollis Blanchard 	ivor[13] = mfspr(SPRN_IVOR13);
1372d9fbd03dSHollis Blanchard 	ivor[14] = mfspr(SPRN_IVOR14);
1373d9fbd03dSHollis Blanchard 	ivor[15] = mfspr(SPRN_IVOR15);
1374d9fbd03dSHollis Blanchard 
1375d9fbd03dSHollis Blanchard 	for (i = 0; i < 16; i++) {
1376d9fbd03dSHollis Blanchard 		if (ivor[i] > max_ivor)
1377d9fbd03dSHollis Blanchard 			max_ivor = ivor[i];
1378d9fbd03dSHollis Blanchard 
1379d9fbd03dSHollis Blanchard 		memcpy((void *)kvmppc_booke_handlers + ivor[i],
1380d9fbd03dSHollis Blanchard 		       kvmppc_handlers_start + i * kvmppc_handler_len,
1381d9fbd03dSHollis Blanchard 		       kvmppc_handler_len);
1382d9fbd03dSHollis Blanchard 	}
1383d9fbd03dSHollis Blanchard 	flush_icache_range(kvmppc_booke_handlers,
1384d9fbd03dSHollis Blanchard 	                   kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
1385d30f6e48SScott Wood #endif /* !BOOKE_HV */
1386db93f574SHollis Blanchard 	return 0;
1387d9fbd03dSHollis Blanchard }
1388d9fbd03dSHollis Blanchard 
1389db93f574SHollis Blanchard void __exit kvmppc_booke_exit(void)
1390d9fbd03dSHollis Blanchard {
1391d9fbd03dSHollis Blanchard 	free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
1392d9fbd03dSHollis Blanchard 	kvm_exit();
1393d9fbd03dSHollis Blanchard }
1394