xref: /openbmc/linux/arch/powerpc/kvm/booke.c (revision 97c95059848358f1577f471ec47cf68690f996e4)
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"
42*97c95059SAlexander 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) },
66d9fbd03dSHollis Blanchard 	{ NULL }
67d9fbd03dSHollis Blanchard };
68d9fbd03dSHollis Blanchard 
69d9fbd03dSHollis Blanchard /* TODO: use vcpu_printf() */
70d9fbd03dSHollis Blanchard void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
71d9fbd03dSHollis Blanchard {
72d9fbd03dSHollis Blanchard 	int i;
73d9fbd03dSHollis Blanchard 
74666e7252SAlexander Graf 	printk("pc:   %08lx msr:  %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
755cf8ca22SHollis Blanchard 	printk("lr:   %08lx ctr:  %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
76de7906c3SAlexander Graf 	printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
77de7906c3SAlexander Graf 					    vcpu->arch.shared->srr1);
78d9fbd03dSHollis Blanchard 
79d9fbd03dSHollis Blanchard 	printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
80d9fbd03dSHollis Blanchard 
81d9fbd03dSHollis Blanchard 	for (i = 0; i < 32; i += 4) {
825cf8ca22SHollis Blanchard 		printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
838e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i),
848e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i+1),
858e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i+2),
868e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i+3));
87d9fbd03dSHollis Blanchard 	}
88d9fbd03dSHollis Blanchard }
89d9fbd03dSHollis Blanchard 
904cd35f67SScott Wood #ifdef CONFIG_SPE
914cd35f67SScott Wood void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
924cd35f67SScott Wood {
934cd35f67SScott Wood 	preempt_disable();
944cd35f67SScott Wood 	enable_kernel_spe();
954cd35f67SScott Wood 	kvmppc_save_guest_spe(vcpu);
964cd35f67SScott Wood 	vcpu->arch.shadow_msr &= ~MSR_SPE;
974cd35f67SScott Wood 	preempt_enable();
984cd35f67SScott Wood }
994cd35f67SScott Wood 
1004cd35f67SScott Wood static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
1014cd35f67SScott Wood {
1024cd35f67SScott Wood 	preempt_disable();
1034cd35f67SScott Wood 	enable_kernel_spe();
1044cd35f67SScott Wood 	kvmppc_load_guest_spe(vcpu);
1054cd35f67SScott Wood 	vcpu->arch.shadow_msr |= MSR_SPE;
1064cd35f67SScott Wood 	preempt_enable();
1074cd35f67SScott Wood }
1084cd35f67SScott Wood 
1094cd35f67SScott Wood static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
1104cd35f67SScott Wood {
1114cd35f67SScott Wood 	if (vcpu->arch.shared->msr & MSR_SPE) {
1124cd35f67SScott Wood 		if (!(vcpu->arch.shadow_msr & MSR_SPE))
1134cd35f67SScott Wood 			kvmppc_vcpu_enable_spe(vcpu);
1144cd35f67SScott Wood 	} else if (vcpu->arch.shadow_msr & MSR_SPE) {
1154cd35f67SScott Wood 		kvmppc_vcpu_disable_spe(vcpu);
1164cd35f67SScott Wood 	}
1174cd35f67SScott Wood }
1184cd35f67SScott Wood #else
1194cd35f67SScott Wood static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
1204cd35f67SScott Wood {
1214cd35f67SScott Wood }
1224cd35f67SScott Wood #endif
1234cd35f67SScott Wood 
124dd9ebf1fSLiu Yu /*
125dd9ebf1fSLiu Yu  * Helper function for "full" MSR writes.  No need to call this if only
126dd9ebf1fSLiu Yu  * EE/CE/ME/DE/RI are changing.
127dd9ebf1fSLiu Yu  */
1284cd35f67SScott Wood void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
1294cd35f67SScott Wood {
130dd9ebf1fSLiu Yu 	u32 old_msr = vcpu->arch.shared->msr;
1314cd35f67SScott Wood 
132d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
133d30f6e48SScott Wood 	new_msr |= MSR_GS;
134d30f6e48SScott Wood #endif
135d30f6e48SScott Wood 
1364cd35f67SScott Wood 	vcpu->arch.shared->msr = new_msr;
1374cd35f67SScott Wood 
138dd9ebf1fSLiu Yu 	kvmppc_mmu_msr_notify(vcpu, old_msr);
1394cd35f67SScott Wood 	kvmppc_vcpu_sync_spe(vcpu);
1404cd35f67SScott Wood }
1414cd35f67SScott Wood 
142d4cf3892SHollis Blanchard static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
143d4cf3892SHollis Blanchard                                        unsigned int priority)
1449dd921cfSHollis Blanchard {
1459dd921cfSHollis Blanchard 	set_bit(priority, &vcpu->arch.pending_exceptions);
1469dd921cfSHollis Blanchard }
1479dd921cfSHollis Blanchard 
148daf5e271SLiu Yu static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
149daf5e271SLiu Yu                                         ulong dear_flags, ulong esr_flags)
1509dd921cfSHollis Blanchard {
151daf5e271SLiu Yu 	vcpu->arch.queued_dear = dear_flags;
152daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
153daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
154daf5e271SLiu Yu }
155daf5e271SLiu Yu 
156daf5e271SLiu Yu static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
157daf5e271SLiu Yu                                            ulong dear_flags, ulong esr_flags)
158daf5e271SLiu Yu {
159daf5e271SLiu Yu 	vcpu->arch.queued_dear = dear_flags;
160daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
161daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
162daf5e271SLiu Yu }
163daf5e271SLiu Yu 
164daf5e271SLiu Yu static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
165daf5e271SLiu Yu                                            ulong esr_flags)
166daf5e271SLiu Yu {
167daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
168daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
169daf5e271SLiu Yu }
170daf5e271SLiu Yu 
171daf5e271SLiu Yu void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
172daf5e271SLiu Yu {
173daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
174d4cf3892SHollis Blanchard 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
1759dd921cfSHollis Blanchard }
1769dd921cfSHollis Blanchard 
1779dd921cfSHollis Blanchard void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
1789dd921cfSHollis Blanchard {
179d4cf3892SHollis Blanchard 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
1809dd921cfSHollis Blanchard }
1819dd921cfSHollis Blanchard 
1829dd921cfSHollis Blanchard int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
1839dd921cfSHollis Blanchard {
184d4cf3892SHollis Blanchard 	return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
1859dd921cfSHollis Blanchard }
1869dd921cfSHollis Blanchard 
1877706664dSAlexander Graf void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
1887706664dSAlexander Graf {
1897706664dSAlexander Graf 	clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
1907706664dSAlexander Graf }
1917706664dSAlexander Graf 
1929dd921cfSHollis Blanchard void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
1939dd921cfSHollis Blanchard                                 struct kvm_interrupt *irq)
1949dd921cfSHollis Blanchard {
195c5335f17SAlexander Graf 	unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
196c5335f17SAlexander Graf 
197c5335f17SAlexander Graf 	if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
198c5335f17SAlexander Graf 		prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
199c5335f17SAlexander Graf 
200c5335f17SAlexander Graf 	kvmppc_booke_queue_irqprio(vcpu, prio);
2019dd921cfSHollis Blanchard }
2029dd921cfSHollis Blanchard 
2034496f974SAlexander Graf void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
2044496f974SAlexander Graf                                   struct kvm_interrupt *irq)
2054496f974SAlexander Graf {
2064496f974SAlexander Graf 	clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
207c5335f17SAlexander Graf 	clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
2084496f974SAlexander Graf }
2094496f974SAlexander Graf 
210d30f6e48SScott Wood static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
211d30f6e48SScott Wood {
212d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
213d30f6e48SScott Wood 	mtspr(SPRN_GSRR0, srr0);
214d30f6e48SScott Wood 	mtspr(SPRN_GSRR1, srr1);
215d30f6e48SScott Wood #else
216d30f6e48SScott Wood 	vcpu->arch.shared->srr0 = srr0;
217d30f6e48SScott Wood 	vcpu->arch.shared->srr1 = srr1;
218d30f6e48SScott Wood #endif
219d30f6e48SScott Wood }
220d30f6e48SScott Wood 
221d30f6e48SScott Wood static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
222d30f6e48SScott Wood {
223d30f6e48SScott Wood 	vcpu->arch.csrr0 = srr0;
224d30f6e48SScott Wood 	vcpu->arch.csrr1 = srr1;
225d30f6e48SScott Wood }
226d30f6e48SScott Wood 
227d30f6e48SScott Wood static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
228d30f6e48SScott Wood {
229d30f6e48SScott Wood 	if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
230d30f6e48SScott Wood 		vcpu->arch.dsrr0 = srr0;
231d30f6e48SScott Wood 		vcpu->arch.dsrr1 = srr1;
232d30f6e48SScott Wood 	} else {
233d30f6e48SScott Wood 		set_guest_csrr(vcpu, srr0, srr1);
234d30f6e48SScott Wood 	}
235d30f6e48SScott Wood }
236d30f6e48SScott Wood 
237d30f6e48SScott Wood static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
238d30f6e48SScott Wood {
239d30f6e48SScott Wood 	vcpu->arch.mcsrr0 = srr0;
240d30f6e48SScott Wood 	vcpu->arch.mcsrr1 = srr1;
241d30f6e48SScott Wood }
242d30f6e48SScott Wood 
243d30f6e48SScott Wood static unsigned long get_guest_dear(struct kvm_vcpu *vcpu)
244d30f6e48SScott Wood {
245d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
246d30f6e48SScott Wood 	return mfspr(SPRN_GDEAR);
247d30f6e48SScott Wood #else
248d30f6e48SScott Wood 	return vcpu->arch.shared->dar;
249d30f6e48SScott Wood #endif
250d30f6e48SScott Wood }
251d30f6e48SScott Wood 
252d30f6e48SScott Wood static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear)
253d30f6e48SScott Wood {
254d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
255d30f6e48SScott Wood 	mtspr(SPRN_GDEAR, dear);
256d30f6e48SScott Wood #else
257d30f6e48SScott Wood 	vcpu->arch.shared->dar = dear;
258d30f6e48SScott Wood #endif
259d30f6e48SScott Wood }
260d30f6e48SScott Wood 
261d30f6e48SScott Wood static unsigned long get_guest_esr(struct kvm_vcpu *vcpu)
262d30f6e48SScott Wood {
263d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
264d30f6e48SScott Wood 	return mfspr(SPRN_GESR);
265d30f6e48SScott Wood #else
266d30f6e48SScott Wood 	return vcpu->arch.shared->esr;
267d30f6e48SScott Wood #endif
268d30f6e48SScott Wood }
269d30f6e48SScott Wood 
270d30f6e48SScott Wood static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr)
271d30f6e48SScott Wood {
272d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
273d30f6e48SScott Wood 	mtspr(SPRN_GESR, esr);
274d30f6e48SScott Wood #else
275d30f6e48SScott Wood 	vcpu->arch.shared->esr = esr;
276d30f6e48SScott Wood #endif
277d30f6e48SScott Wood }
278d30f6e48SScott Wood 
279d4cf3892SHollis Blanchard /* Deliver the interrupt of the corresponding priority, if possible. */
280d4cf3892SHollis Blanchard static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
281d4cf3892SHollis Blanchard                                         unsigned int priority)
282d9fbd03dSHollis Blanchard {
283d4cf3892SHollis Blanchard 	int allowed = 0;
28479300f8cSAlexander Graf 	ulong msr_mask = 0;
285daf5e271SLiu Yu 	bool update_esr = false, update_dear = false;
2865c6cedf4SAlexander Graf 	ulong crit_raw = vcpu->arch.shared->critical;
2875c6cedf4SAlexander Graf 	ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
2885c6cedf4SAlexander Graf 	bool crit;
289c5335f17SAlexander Graf 	bool keep_irq = false;
290d30f6e48SScott Wood 	enum int_class int_class;
2915c6cedf4SAlexander Graf 
2925c6cedf4SAlexander Graf 	/* Truncate crit indicators in 32 bit mode */
2935c6cedf4SAlexander Graf 	if (!(vcpu->arch.shared->msr & MSR_SF)) {
2945c6cedf4SAlexander Graf 		crit_raw &= 0xffffffff;
2955c6cedf4SAlexander Graf 		crit_r1 &= 0xffffffff;
2965c6cedf4SAlexander Graf 	}
2975c6cedf4SAlexander Graf 
2985c6cedf4SAlexander Graf 	/* Critical section when crit == r1 */
2995c6cedf4SAlexander Graf 	crit = (crit_raw == crit_r1);
3005c6cedf4SAlexander Graf 	/* ... and we're in supervisor mode */
3015c6cedf4SAlexander Graf 	crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
302d9fbd03dSHollis Blanchard 
303c5335f17SAlexander Graf 	if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
304c5335f17SAlexander Graf 		priority = BOOKE_IRQPRIO_EXTERNAL;
305c5335f17SAlexander Graf 		keep_irq = true;
306c5335f17SAlexander Graf 	}
307c5335f17SAlexander Graf 
308d4cf3892SHollis Blanchard 	switch (priority) {
309d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DTLB_MISS:
310daf5e271SLiu Yu 	case BOOKE_IRQPRIO_DATA_STORAGE:
311daf5e271SLiu Yu 		update_dear = true;
312daf5e271SLiu Yu 		/* fall through */
313daf5e271SLiu Yu 	case BOOKE_IRQPRIO_INST_STORAGE:
314daf5e271SLiu Yu 	case BOOKE_IRQPRIO_PROGRAM:
315daf5e271SLiu Yu 		update_esr = true;
316daf5e271SLiu Yu 		/* fall through */
317d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_ITLB_MISS:
318d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_SYSCALL:
319d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_FP_UNAVAIL:
320bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_UNAVAIL:
321bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_FP_DATA:
322bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_FP_ROUND:
323d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_AP_UNAVAIL:
324d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_ALIGNMENT:
325d4cf3892SHollis Blanchard 		allowed = 1;
32679300f8cSAlexander Graf 		msr_mask = MSR_CE | MSR_ME | MSR_DE;
327d30f6e48SScott Wood 		int_class = INT_CLASS_NONCRIT;
328d9fbd03dSHollis Blanchard 		break;
329d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_CRITICAL:
3304ab96919SAlexander Graf 	case BOOKE_IRQPRIO_DBELL_CRIT:
331666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_CE;
332d30f6e48SScott Wood 		allowed = allowed && !crit;
33379300f8cSAlexander Graf 		msr_mask = MSR_ME;
334d30f6e48SScott Wood 		int_class = INT_CLASS_CRIT;
335d9fbd03dSHollis Blanchard 		break;
336d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_MACHINE_CHECK:
337666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_ME;
338d30f6e48SScott Wood 		allowed = allowed && !crit;
339d30f6e48SScott Wood 		int_class = INT_CLASS_MC;
340d9fbd03dSHollis Blanchard 		break;
341d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DECREMENTER:
342d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_FIT:
343dfd4d47eSScott Wood 		keep_irq = true;
344dfd4d47eSScott Wood 		/* fall through */
345dfd4d47eSScott Wood 	case BOOKE_IRQPRIO_EXTERNAL:
3464ab96919SAlexander Graf 	case BOOKE_IRQPRIO_DBELL:
347666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_EE;
3485c6cedf4SAlexander Graf 		allowed = allowed && !crit;
34979300f8cSAlexander Graf 		msr_mask = MSR_CE | MSR_ME | MSR_DE;
350d30f6e48SScott Wood 		int_class = INT_CLASS_NONCRIT;
351d9fbd03dSHollis Blanchard 		break;
352d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DEBUG:
353666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_DE;
354d30f6e48SScott Wood 		allowed = allowed && !crit;
35579300f8cSAlexander Graf 		msr_mask = MSR_ME;
356d30f6e48SScott Wood 		int_class = INT_CLASS_CRIT;
357d9fbd03dSHollis Blanchard 		break;
358d9fbd03dSHollis Blanchard 	}
359d9fbd03dSHollis Blanchard 
360d4cf3892SHollis Blanchard 	if (allowed) {
361d30f6e48SScott Wood 		switch (int_class) {
362d30f6e48SScott Wood 		case INT_CLASS_NONCRIT:
363d30f6e48SScott Wood 			set_guest_srr(vcpu, vcpu->arch.pc,
364d30f6e48SScott Wood 				      vcpu->arch.shared->msr);
365d30f6e48SScott Wood 			break;
366d30f6e48SScott Wood 		case INT_CLASS_CRIT:
367d30f6e48SScott Wood 			set_guest_csrr(vcpu, vcpu->arch.pc,
368d30f6e48SScott Wood 				       vcpu->arch.shared->msr);
369d30f6e48SScott Wood 			break;
370d30f6e48SScott Wood 		case INT_CLASS_DBG:
371d30f6e48SScott Wood 			set_guest_dsrr(vcpu, vcpu->arch.pc,
372d30f6e48SScott Wood 				       vcpu->arch.shared->msr);
373d30f6e48SScott Wood 			break;
374d30f6e48SScott Wood 		case INT_CLASS_MC:
375d30f6e48SScott Wood 			set_guest_mcsrr(vcpu, vcpu->arch.pc,
376d30f6e48SScott Wood 					vcpu->arch.shared->msr);
377d30f6e48SScott Wood 			break;
378d30f6e48SScott Wood 		}
379d30f6e48SScott Wood 
380d4cf3892SHollis Blanchard 		vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
381daf5e271SLiu Yu 		if (update_esr == true)
382d30f6e48SScott Wood 			set_guest_esr(vcpu, vcpu->arch.queued_esr);
383daf5e271SLiu Yu 		if (update_dear == true)
384d30f6e48SScott Wood 			set_guest_dear(vcpu, vcpu->arch.queued_dear);
385666e7252SAlexander Graf 		kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
386d4cf3892SHollis Blanchard 
387c5335f17SAlexander Graf 		if (!keep_irq)
388d4cf3892SHollis Blanchard 			clear_bit(priority, &vcpu->arch.pending_exceptions);
389d4cf3892SHollis Blanchard 	}
390d4cf3892SHollis Blanchard 
391d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
392d30f6e48SScott Wood 	/*
393d30f6e48SScott Wood 	 * If an interrupt is pending but masked, raise a guest doorbell
394d30f6e48SScott Wood 	 * so that we are notified when the guest enables the relevant
395d30f6e48SScott Wood 	 * MSR bit.
396d30f6e48SScott Wood 	 */
397d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
398d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
399d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
400d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
401d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
402d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
403d30f6e48SScott Wood #endif
404d30f6e48SScott Wood 
405d4cf3892SHollis Blanchard 	return allowed;
406d9fbd03dSHollis Blanchard }
407d9fbd03dSHollis Blanchard 
408dfd4d47eSScott Wood static void update_timer_ints(struct kvm_vcpu *vcpu)
409dfd4d47eSScott Wood {
410dfd4d47eSScott Wood 	if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
411dfd4d47eSScott Wood 		kvmppc_core_queue_dec(vcpu);
412dfd4d47eSScott Wood 	else
413dfd4d47eSScott Wood 		kvmppc_core_dequeue_dec(vcpu);
414dfd4d47eSScott Wood }
415dfd4d47eSScott Wood 
416c59a6a3eSScott Wood static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
417d9fbd03dSHollis Blanchard {
418d9fbd03dSHollis Blanchard 	unsigned long *pending = &vcpu->arch.pending_exceptions;
419d9fbd03dSHollis Blanchard 	unsigned int priority;
420d9fbd03dSHollis Blanchard 
421dfd4d47eSScott Wood 	if (vcpu->requests) {
422dfd4d47eSScott Wood 		if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu)) {
423dfd4d47eSScott Wood 			smp_mb();
424dfd4d47eSScott Wood 			update_timer_ints(vcpu);
425dfd4d47eSScott Wood 		}
426dfd4d47eSScott Wood 	}
427dfd4d47eSScott Wood 
4289ab80843SHollis Blanchard 	priority = __ffs(*pending);
4298b3a00fcSAlexander Graf 	while (priority < BOOKE_IRQPRIO_MAX) {
430d4cf3892SHollis Blanchard 		if (kvmppc_booke_irqprio_deliver(vcpu, priority))
431d9fbd03dSHollis Blanchard 			break;
432d9fbd03dSHollis Blanchard 
433d9fbd03dSHollis Blanchard 		priority = find_next_bit(pending,
434d9fbd03dSHollis Blanchard 		                         BITS_PER_BYTE * sizeof(*pending),
435d9fbd03dSHollis Blanchard 		                         priority + 1);
436d9fbd03dSHollis Blanchard 	}
43790bba358SAlexander Graf 
43890bba358SAlexander Graf 	/* Tell the guest about our interrupt status */
43929ac26efSScott Wood 	vcpu->arch.shared->int_pending = !!*pending;
440d9fbd03dSHollis Blanchard }
441d9fbd03dSHollis Blanchard 
442c59a6a3eSScott Wood /* Check pending exceptions and deliver one, if possible. */
443a8e4ef84SAlexander Graf int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
444c59a6a3eSScott Wood {
445a8e4ef84SAlexander Graf 	int r = 0;
446c59a6a3eSScott Wood 	WARN_ON_ONCE(!irqs_disabled());
447c59a6a3eSScott Wood 
448c59a6a3eSScott Wood 	kvmppc_core_check_exceptions(vcpu);
449c59a6a3eSScott Wood 
450c59a6a3eSScott Wood 	if (vcpu->arch.shared->msr & MSR_WE) {
451c59a6a3eSScott Wood 		local_irq_enable();
452c59a6a3eSScott Wood 		kvm_vcpu_block(vcpu);
453966cd0f3SAlexander Graf 		clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
454c59a6a3eSScott Wood 		local_irq_disable();
455c59a6a3eSScott Wood 
456c59a6a3eSScott Wood 		kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
457a8e4ef84SAlexander Graf 		r = 1;
458c59a6a3eSScott Wood 	};
459a8e4ef84SAlexander Graf 
460a8e4ef84SAlexander Graf 	return r;
461a8e4ef84SAlexander Graf }
462a8e4ef84SAlexander Graf 
463a8e4ef84SAlexander Graf /*
464a8e4ef84SAlexander Graf  * Common checks before entering the guest world.  Call with interrupts
465a8e4ef84SAlexander Graf  * disabled.
466a8e4ef84SAlexander Graf  *
467a8e4ef84SAlexander Graf  * returns !0 if a signal is pending and check_signal is true
468a8e4ef84SAlexander Graf  */
46903660ba2SAlexander Graf static int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
470a8e4ef84SAlexander Graf {
471a8e4ef84SAlexander Graf 	int r = 0;
472a8e4ef84SAlexander Graf 
473a8e4ef84SAlexander Graf 	WARN_ON_ONCE(!irqs_disabled());
474a8e4ef84SAlexander Graf 	while (true) {
475a8e4ef84SAlexander Graf 		if (need_resched()) {
476a8e4ef84SAlexander Graf 			local_irq_enable();
477a8e4ef84SAlexander Graf 			cond_resched();
478a8e4ef84SAlexander Graf 			local_irq_disable();
479a8e4ef84SAlexander Graf 			continue;
480a8e4ef84SAlexander Graf 		}
481a8e4ef84SAlexander Graf 
48203660ba2SAlexander Graf 		if (signal_pending(current)) {
483a8e4ef84SAlexander Graf 			r = 1;
484a8e4ef84SAlexander Graf 			break;
485a8e4ef84SAlexander Graf 		}
486a8e4ef84SAlexander Graf 
487a8e4ef84SAlexander Graf 		if (kvmppc_core_prepare_to_enter(vcpu)) {
488a8e4ef84SAlexander Graf 			/* interrupts got enabled in between, so we
489a8e4ef84SAlexander Graf 			   are back at square 1 */
490a8e4ef84SAlexander Graf 			continue;
491a8e4ef84SAlexander Graf 		}
492a8e4ef84SAlexander Graf 
493a8e4ef84SAlexander Graf 		break;
494a8e4ef84SAlexander Graf 	}
495a8e4ef84SAlexander Graf 
496a8e4ef84SAlexander Graf 	return r;
497c59a6a3eSScott Wood }
498c59a6a3eSScott Wood 
499df6909e5SPaul Mackerras int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
500df6909e5SPaul Mackerras {
501df6909e5SPaul Mackerras 	int ret;
5028fae845fSScott Wood #ifdef CONFIG_PPC_FPU
5038fae845fSScott Wood 	unsigned int fpscr;
5048fae845fSScott Wood 	int fpexc_mode;
5058fae845fSScott Wood 	u64 fpr[32];
5068fae845fSScott Wood #endif
507df6909e5SPaul Mackerras 
508af8f38b3SAlexander Graf 	if (!vcpu->arch.sane) {
509af8f38b3SAlexander Graf 		kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
510af8f38b3SAlexander Graf 		return -EINVAL;
511af8f38b3SAlexander Graf 	}
512af8f38b3SAlexander Graf 
513df6909e5SPaul Mackerras 	local_irq_disable();
51403660ba2SAlexander Graf 	if (kvmppc_prepare_to_enter(vcpu)) {
5151d1ef222SScott Wood 		kvm_run->exit_reason = KVM_EXIT_INTR;
5161d1ef222SScott Wood 		ret = -EINTR;
5171d1ef222SScott Wood 		goto out;
5181d1ef222SScott Wood 	}
5191d1ef222SScott Wood 
520df6909e5SPaul Mackerras 	kvm_guest_enter();
5218fae845fSScott Wood 
5228fae845fSScott Wood #ifdef CONFIG_PPC_FPU
5238fae845fSScott Wood 	/* Save userspace FPU state in stack */
5248fae845fSScott Wood 	enable_kernel_fp();
5258fae845fSScott Wood 	memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
5268fae845fSScott Wood 	fpscr = current->thread.fpscr.val;
5278fae845fSScott Wood 	fpexc_mode = current->thread.fpexc_mode;
5288fae845fSScott Wood 
5298fae845fSScott Wood 	/* Restore guest FPU state to thread */
5308fae845fSScott Wood 	memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr));
5318fae845fSScott Wood 	current->thread.fpscr.val = vcpu->arch.fpscr;
5328fae845fSScott Wood 
5338fae845fSScott Wood 	/*
5348fae845fSScott Wood 	 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
5358fae845fSScott Wood 	 * as always using the FPU.  Kernel usage of FP (via
5368fae845fSScott Wood 	 * enable_kernel_fp()) in this thread must not occur while
5378fae845fSScott Wood 	 * vcpu->fpu_active is set.
5388fae845fSScott Wood 	 */
5398fae845fSScott Wood 	vcpu->fpu_active = 1;
5408fae845fSScott Wood 
5418fae845fSScott Wood 	kvmppc_load_guest_fp(vcpu);
5428fae845fSScott Wood #endif
5438fae845fSScott Wood 
544df6909e5SPaul Mackerras 	ret = __kvmppc_vcpu_run(kvm_run, vcpu);
5458fae845fSScott Wood 
5468fae845fSScott Wood #ifdef CONFIG_PPC_FPU
5478fae845fSScott Wood 	kvmppc_save_guest_fp(vcpu);
5488fae845fSScott Wood 
5498fae845fSScott Wood 	vcpu->fpu_active = 0;
5508fae845fSScott Wood 
5518fae845fSScott Wood 	/* Save guest FPU state from thread */
5528fae845fSScott Wood 	memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr));
5538fae845fSScott Wood 	vcpu->arch.fpscr = current->thread.fpscr.val;
5548fae845fSScott Wood 
5558fae845fSScott Wood 	/* Restore userspace FPU state from stack */
5568fae845fSScott Wood 	memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
5578fae845fSScott Wood 	current->thread.fpscr.val = fpscr;
5588fae845fSScott Wood 	current->thread.fpexc_mode = fpexc_mode;
5598fae845fSScott Wood #endif
5608fae845fSScott Wood 
561df6909e5SPaul Mackerras 	kvm_guest_exit();
562df6909e5SPaul Mackerras 
5631d1ef222SScott Wood out:
5641d1ef222SScott Wood 	local_irq_enable();
565df6909e5SPaul Mackerras 	return ret;
566df6909e5SPaul Mackerras }
567df6909e5SPaul Mackerras 
568d30f6e48SScott Wood static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
569d9fbd03dSHollis Blanchard {
570d9fbd03dSHollis Blanchard 	enum emulation_result er;
571d9fbd03dSHollis Blanchard 
572d9fbd03dSHollis Blanchard 	er = kvmppc_emulate_instruction(run, vcpu);
573d9fbd03dSHollis Blanchard 	switch (er) {
574d9fbd03dSHollis Blanchard 	case EMULATE_DONE:
57573e75b41SHollis Blanchard 		/* don't overwrite subtypes, just account kvm_stats */
5767b701591SHollis Blanchard 		kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
577d9fbd03dSHollis Blanchard 		/* Future optimization: only reload non-volatiles if
578d9fbd03dSHollis Blanchard 		 * they were actually modified by emulation. */
579d30f6e48SScott Wood 		return RESUME_GUEST_NV;
580d30f6e48SScott Wood 
581d9fbd03dSHollis Blanchard 	case EMULATE_DO_DCR:
582d9fbd03dSHollis Blanchard 		run->exit_reason = KVM_EXIT_DCR;
583d30f6e48SScott Wood 		return RESUME_HOST;
584d30f6e48SScott Wood 
585d9fbd03dSHollis Blanchard 	case EMULATE_FAIL:
5865cf8ca22SHollis Blanchard 		printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
587d9fbd03dSHollis Blanchard 		       __func__, vcpu->arch.pc, vcpu->arch.last_inst);
588d9fbd03dSHollis Blanchard 		/* For debugging, encode the failing instruction and
589d9fbd03dSHollis Blanchard 		 * report it to userspace. */
590d9fbd03dSHollis Blanchard 		run->hw.hardware_exit_reason = ~0ULL << 32;
591d9fbd03dSHollis Blanchard 		run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
592d1ff5499SAlexander Graf 		kvmppc_core_queue_program(vcpu, ESR_PIL);
593d30f6e48SScott Wood 		return RESUME_HOST;
594d30f6e48SScott Wood 
595d9fbd03dSHollis Blanchard 	default:
596d9fbd03dSHollis Blanchard 		BUG();
597d9fbd03dSHollis Blanchard 	}
598d30f6e48SScott Wood }
599d30f6e48SScott Wood 
6004e642ccbSAlexander Graf static void kvmppc_fill_pt_regs(struct pt_regs *regs)
6014e642ccbSAlexander Graf {
6024e642ccbSAlexander Graf 	ulong r1, ip, msr, lr;
6034e642ccbSAlexander Graf 
6044e642ccbSAlexander Graf 	asm("mr %0, 1" : "=r"(r1));
6054e642ccbSAlexander Graf 	asm("mflr %0" : "=r"(lr));
6064e642ccbSAlexander Graf 	asm("mfmsr %0" : "=r"(msr));
6074e642ccbSAlexander Graf 	asm("bl 1f; 1: mflr %0" : "=r"(ip));
6084e642ccbSAlexander Graf 
6094e642ccbSAlexander Graf 	memset(regs, 0, sizeof(*regs));
6104e642ccbSAlexander Graf 	regs->gpr[1] = r1;
6114e642ccbSAlexander Graf 	regs->nip = ip;
6124e642ccbSAlexander Graf 	regs->msr = msr;
6134e642ccbSAlexander Graf 	regs->link = lr;
6144e642ccbSAlexander Graf }
6154e642ccbSAlexander Graf 
6166328e593SBharat Bhushan /*
6176328e593SBharat Bhushan  * For interrupts needed to be handled by host interrupt handlers,
6186328e593SBharat Bhushan  * corresponding host handler are called from here in similar way
6196328e593SBharat Bhushan  * (but not exact) as they are called from low level handler
6206328e593SBharat Bhushan  * (such as from arch/powerpc/kernel/head_fsl_booke.S).
6216328e593SBharat Bhushan  */
6224e642ccbSAlexander Graf static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
6234e642ccbSAlexander Graf 				     unsigned int exit_nr)
6244e642ccbSAlexander Graf {
6254e642ccbSAlexander Graf 	struct pt_regs regs;
6264e642ccbSAlexander Graf 
6274e642ccbSAlexander Graf 	switch (exit_nr) {
6284e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_EXTERNAL:
6294e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
6304e642ccbSAlexander Graf 		do_IRQ(&regs);
6314e642ccbSAlexander Graf 		break;
6324e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_DECREMENTER:
6334e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
6344e642ccbSAlexander Graf 		timer_interrupt(&regs);
6354e642ccbSAlexander Graf 		break;
6364e642ccbSAlexander Graf #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64)
6374e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_DOORBELL:
6384e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
6394e642ccbSAlexander Graf 		doorbell_exception(&regs);
6404e642ccbSAlexander Graf 		break;
6414e642ccbSAlexander Graf #endif
6424e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_MACHINE_CHECK:
6434e642ccbSAlexander Graf 		/* FIXME */
6444e642ccbSAlexander Graf 		break;
6457cc1e8eeSAlexander Graf 	case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
6467cc1e8eeSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
6477cc1e8eeSAlexander Graf 		performance_monitor_exception(&regs);
6487cc1e8eeSAlexander Graf 		break;
6496328e593SBharat Bhushan 	case BOOKE_INTERRUPT_WATCHDOG:
6506328e593SBharat Bhushan 		kvmppc_fill_pt_regs(&regs);
6516328e593SBharat Bhushan #ifdef CONFIG_BOOKE_WDT
6526328e593SBharat Bhushan 		WatchdogException(&regs);
6536328e593SBharat Bhushan #else
6546328e593SBharat Bhushan 		unknown_exception(&regs);
6556328e593SBharat Bhushan #endif
6566328e593SBharat Bhushan 		break;
6576328e593SBharat Bhushan 	case BOOKE_INTERRUPT_CRITICAL:
6586328e593SBharat Bhushan 		unknown_exception(&regs);
6596328e593SBharat Bhushan 		break;
6604e642ccbSAlexander Graf 	}
6614e642ccbSAlexander Graf }
6624e642ccbSAlexander Graf 
663d30f6e48SScott Wood /**
664d30f6e48SScott Wood  * kvmppc_handle_exit
665d30f6e48SScott Wood  *
666d30f6e48SScott Wood  * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
667d30f6e48SScott Wood  */
668d30f6e48SScott Wood int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
669d30f6e48SScott Wood                        unsigned int exit_nr)
670d30f6e48SScott Wood {
671d30f6e48SScott Wood 	int r = RESUME_HOST;
672d30f6e48SScott Wood 
673d30f6e48SScott Wood 	/* update before a new last_exit_type is rewritten */
674d30f6e48SScott Wood 	kvmppc_update_timing_stats(vcpu);
675d30f6e48SScott Wood 
6764e642ccbSAlexander Graf 	/* restart interrupts if they were meant for the host */
6774e642ccbSAlexander Graf 	kvmppc_restart_interrupt(vcpu, exit_nr);
678d30f6e48SScott Wood 
679d30f6e48SScott Wood 	local_irq_enable();
680d30f6e48SScott Wood 
681*97c95059SAlexander Graf 	trace_kvm_exit(exit_nr, vcpu);
682*97c95059SAlexander Graf 
683d30f6e48SScott Wood 	run->exit_reason = KVM_EXIT_UNKNOWN;
684d30f6e48SScott Wood 	run->ready_for_interrupt_injection = 1;
685d30f6e48SScott Wood 
686d30f6e48SScott Wood 	switch (exit_nr) {
687d30f6e48SScott Wood 	case BOOKE_INTERRUPT_MACHINE_CHECK:
688c35c9d84SAlexander Graf 		printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
689c35c9d84SAlexander Graf 		kvmppc_dump_vcpu(vcpu);
690c35c9d84SAlexander Graf 		/* For debugging, send invalid exit reason to user space */
691c35c9d84SAlexander Graf 		run->hw.hardware_exit_reason = ~1ULL << 32;
692c35c9d84SAlexander Graf 		run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
693c35c9d84SAlexander Graf 		r = RESUME_HOST;
694d30f6e48SScott Wood 		break;
695d30f6e48SScott Wood 
696d30f6e48SScott Wood 	case BOOKE_INTERRUPT_EXTERNAL:
697d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
698d30f6e48SScott Wood 		r = RESUME_GUEST;
699d30f6e48SScott Wood 		break;
700d30f6e48SScott Wood 
701d30f6e48SScott Wood 	case BOOKE_INTERRUPT_DECREMENTER:
702d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, DEC_EXITS);
703d30f6e48SScott Wood 		r = RESUME_GUEST;
704d30f6e48SScott Wood 		break;
705d30f6e48SScott Wood 
7066328e593SBharat Bhushan 	case BOOKE_INTERRUPT_WATCHDOG:
7076328e593SBharat Bhushan 		r = RESUME_GUEST;
7086328e593SBharat Bhushan 		break;
7096328e593SBharat Bhushan 
710d30f6e48SScott Wood 	case BOOKE_INTERRUPT_DOORBELL:
711d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, DBELL_EXITS);
712d30f6e48SScott Wood 		r = RESUME_GUEST;
713d30f6e48SScott Wood 		break;
714d30f6e48SScott Wood 
715d30f6e48SScott Wood 	case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
716d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, GDBELL_EXITS);
717d30f6e48SScott Wood 
718d30f6e48SScott Wood 		/*
719d30f6e48SScott Wood 		 * We are here because there is a pending guest interrupt
720d30f6e48SScott Wood 		 * which could not be delivered as MSR_CE or MSR_ME was not
721d30f6e48SScott Wood 		 * set.  Once we break from here we will retry delivery.
722d30f6e48SScott Wood 		 */
723d30f6e48SScott Wood 		r = RESUME_GUEST;
724d30f6e48SScott Wood 		break;
725d30f6e48SScott Wood 
726d30f6e48SScott Wood 	case BOOKE_INTERRUPT_GUEST_DBELL:
727d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, GDBELL_EXITS);
728d30f6e48SScott Wood 
729d30f6e48SScott Wood 		/*
730d30f6e48SScott Wood 		 * We are here because there is a pending guest interrupt
731d30f6e48SScott Wood 		 * which could not be delivered as MSR_EE was not set.  Once
732d30f6e48SScott Wood 		 * we break from here we will retry delivery.
733d30f6e48SScott Wood 		 */
734d30f6e48SScott Wood 		r = RESUME_GUEST;
735d30f6e48SScott Wood 		break;
736d30f6e48SScott Wood 
73795f2e921SAlexander Graf 	case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
73895f2e921SAlexander Graf 		r = RESUME_GUEST;
73995f2e921SAlexander Graf 		break;
74095f2e921SAlexander Graf 
741d30f6e48SScott Wood 	case BOOKE_INTERRUPT_HV_PRIV:
742d30f6e48SScott Wood 		r = emulation_exit(run, vcpu);
743d30f6e48SScott Wood 		break;
744d30f6e48SScott Wood 
745d30f6e48SScott Wood 	case BOOKE_INTERRUPT_PROGRAM:
746d30f6e48SScott Wood 		if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
7470268597cSAlexander Graf 			/*
7480268597cSAlexander Graf 			 * Program traps generated by user-level software must
7490268597cSAlexander Graf 			 * be handled by the guest kernel.
7500268597cSAlexander Graf 			 *
7510268597cSAlexander Graf 			 * In GS mode, hypervisor privileged instructions trap
7520268597cSAlexander Graf 			 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
7530268597cSAlexander Graf 			 * actual program interrupts, handled by the guest.
7540268597cSAlexander Graf 			 */
755d30f6e48SScott Wood 			kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
756d30f6e48SScott Wood 			r = RESUME_GUEST;
757d30f6e48SScott Wood 			kvmppc_account_exit(vcpu, USR_PR_INST);
758d30f6e48SScott Wood 			break;
759d30f6e48SScott Wood 		}
760d30f6e48SScott Wood 
761d30f6e48SScott Wood 		r = emulation_exit(run, vcpu);
762d9fbd03dSHollis Blanchard 		break;
763d9fbd03dSHollis Blanchard 
764d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_FP_UNAVAIL:
765d4cf3892SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
7667b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, FP_UNAVAIL);
767d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
768d9fbd03dSHollis Blanchard 		break;
769d9fbd03dSHollis Blanchard 
7704cd35f67SScott Wood #ifdef CONFIG_SPE
7714cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_UNAVAIL: {
7724cd35f67SScott Wood 		if (vcpu->arch.shared->msr & MSR_SPE)
7734cd35f67SScott Wood 			kvmppc_vcpu_enable_spe(vcpu);
7744cd35f67SScott Wood 		else
7754cd35f67SScott Wood 			kvmppc_booke_queue_irqprio(vcpu,
7764cd35f67SScott Wood 						   BOOKE_IRQPRIO_SPE_UNAVAIL);
777bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
778bb3a8a17SHollis Blanchard 		break;
7794cd35f67SScott Wood 	}
780bb3a8a17SHollis Blanchard 
781bb3a8a17SHollis Blanchard 	case BOOKE_INTERRUPT_SPE_FP_DATA:
782bb3a8a17SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
783bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
784bb3a8a17SHollis Blanchard 		break;
785bb3a8a17SHollis Blanchard 
786bb3a8a17SHollis Blanchard 	case BOOKE_INTERRUPT_SPE_FP_ROUND:
787bb3a8a17SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
788bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
789bb3a8a17SHollis Blanchard 		break;
7904cd35f67SScott Wood #else
7914cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_UNAVAIL:
7924cd35f67SScott Wood 		/*
7934cd35f67SScott Wood 		 * Guest wants SPE, but host kernel doesn't support it.  Send
7944cd35f67SScott Wood 		 * an "unimplemented operation" program check to the guest.
7954cd35f67SScott Wood 		 */
7964cd35f67SScott Wood 		kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
7974cd35f67SScott Wood 		r = RESUME_GUEST;
7984cd35f67SScott Wood 		break;
7994cd35f67SScott Wood 
8004cd35f67SScott Wood 	/*
8014cd35f67SScott Wood 	 * These really should never happen without CONFIG_SPE,
8024cd35f67SScott Wood 	 * as we should never enable the real MSR[SPE] in the guest.
8034cd35f67SScott Wood 	 */
8044cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_FP_DATA:
8054cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_FP_ROUND:
8064cd35f67SScott Wood 		printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
8074cd35f67SScott Wood 		       __func__, exit_nr, vcpu->arch.pc);
8084cd35f67SScott Wood 		run->hw.hardware_exit_reason = exit_nr;
8094cd35f67SScott Wood 		r = RESUME_HOST;
8104cd35f67SScott Wood 		break;
8114cd35f67SScott Wood #endif
812bb3a8a17SHollis Blanchard 
813d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DATA_STORAGE:
814daf5e271SLiu Yu 		kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
815daf5e271SLiu Yu 		                               vcpu->arch.fault_esr);
8167b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, DSI_EXITS);
817d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
818d9fbd03dSHollis Blanchard 		break;
819d9fbd03dSHollis Blanchard 
820d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_INST_STORAGE:
821daf5e271SLiu Yu 		kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
8227b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, ISI_EXITS);
823d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
824d9fbd03dSHollis Blanchard 		break;
825d9fbd03dSHollis Blanchard 
826d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
827d30f6e48SScott Wood 	case BOOKE_INTERRUPT_HV_SYSCALL:
828d30f6e48SScott Wood 		if (!(vcpu->arch.shared->msr & MSR_PR)) {
829d30f6e48SScott Wood 			kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
830d30f6e48SScott Wood 		} else {
831d30f6e48SScott Wood 			/*
832d30f6e48SScott Wood 			 * hcall from guest userspace -- send privileged
833d30f6e48SScott Wood 			 * instruction program check.
834d30f6e48SScott Wood 			 */
835d30f6e48SScott Wood 			kvmppc_core_queue_program(vcpu, ESR_PPR);
836d30f6e48SScott Wood 		}
837d30f6e48SScott Wood 
838d30f6e48SScott Wood 		r = RESUME_GUEST;
839d30f6e48SScott Wood 		break;
840d30f6e48SScott Wood #else
841d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_SYSCALL:
8422a342ed5SAlexander Graf 		if (!(vcpu->arch.shared->msr & MSR_PR) &&
8432a342ed5SAlexander Graf 		    (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
8442a342ed5SAlexander Graf 			/* KVM PV hypercalls */
8452a342ed5SAlexander Graf 			kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
8462a342ed5SAlexander Graf 			r = RESUME_GUEST;
8472a342ed5SAlexander Graf 		} else {
8482a342ed5SAlexander Graf 			/* Guest syscalls */
849d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
8502a342ed5SAlexander Graf 		}
8517b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, SYSCALL_EXITS);
852d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
853d9fbd03dSHollis Blanchard 		break;
854d30f6e48SScott Wood #endif
855d9fbd03dSHollis Blanchard 
856d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DTLB_MISS: {
857d9fbd03dSHollis Blanchard 		unsigned long eaddr = vcpu->arch.fault_dear;
8587924bd41SHollis Blanchard 		int gtlb_index;
859475e7cddSHollis Blanchard 		gpa_t gpaddr;
860d9fbd03dSHollis Blanchard 		gfn_t gfn;
861d9fbd03dSHollis Blanchard 
862bf7ca4bdSAlexander Graf #ifdef CONFIG_KVM_E500V2
863a4cd8b23SScott Wood 		if (!(vcpu->arch.shared->msr & MSR_PR) &&
864a4cd8b23SScott Wood 		    (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
865a4cd8b23SScott Wood 			kvmppc_map_magic(vcpu);
866a4cd8b23SScott Wood 			kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
867a4cd8b23SScott Wood 			r = RESUME_GUEST;
868a4cd8b23SScott Wood 
869a4cd8b23SScott Wood 			break;
870a4cd8b23SScott Wood 		}
871a4cd8b23SScott Wood #endif
872a4cd8b23SScott Wood 
873d9fbd03dSHollis Blanchard 		/* Check the guest TLB. */
874fa86b8ddSHollis Blanchard 		gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
8757924bd41SHollis Blanchard 		if (gtlb_index < 0) {
876d9fbd03dSHollis Blanchard 			/* The guest didn't have a mapping for it. */
877daf5e271SLiu Yu 			kvmppc_core_queue_dtlb_miss(vcpu,
878daf5e271SLiu Yu 			                            vcpu->arch.fault_dear,
879daf5e271SLiu Yu 			                            vcpu->arch.fault_esr);
880b52a638cSHollis Blanchard 			kvmppc_mmu_dtlb_miss(vcpu);
8817b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
882d9fbd03dSHollis Blanchard 			r = RESUME_GUEST;
883d9fbd03dSHollis Blanchard 			break;
884d9fbd03dSHollis Blanchard 		}
885d9fbd03dSHollis Blanchard 
886be8d1caeSHollis Blanchard 		gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
887475e7cddSHollis Blanchard 		gfn = gpaddr >> PAGE_SHIFT;
888d9fbd03dSHollis Blanchard 
889d9fbd03dSHollis Blanchard 		if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
890d9fbd03dSHollis Blanchard 			/* The guest TLB had a mapping, but the shadow TLB
891d9fbd03dSHollis Blanchard 			 * didn't, and it is RAM. This could be because:
892d9fbd03dSHollis Blanchard 			 * a) the entry is mapping the host kernel, or
893d9fbd03dSHollis Blanchard 			 * b) the guest used a large mapping which we're faking
894d9fbd03dSHollis Blanchard 			 * Either way, we need to satisfy the fault without
895d9fbd03dSHollis Blanchard 			 * invoking the guest. */
89658a96214SHollis Blanchard 			kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
8977b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
898d9fbd03dSHollis Blanchard 			r = RESUME_GUEST;
899d9fbd03dSHollis Blanchard 		} else {
900d9fbd03dSHollis Blanchard 			/* Guest has mapped and accessed a page which is not
901d9fbd03dSHollis Blanchard 			 * actually RAM. */
902475e7cddSHollis Blanchard 			vcpu->arch.paddr_accessed = gpaddr;
9036020c0f6SAlexander Graf 			vcpu->arch.vaddr_accessed = eaddr;
904d9fbd03dSHollis Blanchard 			r = kvmppc_emulate_mmio(run, vcpu);
9057b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, MMIO_EXITS);
906d9fbd03dSHollis Blanchard 		}
907d9fbd03dSHollis Blanchard 
908d9fbd03dSHollis Blanchard 		break;
909d9fbd03dSHollis Blanchard 	}
910d9fbd03dSHollis Blanchard 
911d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_ITLB_MISS: {
912d9fbd03dSHollis Blanchard 		unsigned long eaddr = vcpu->arch.pc;
91389168618SHollis Blanchard 		gpa_t gpaddr;
914d9fbd03dSHollis Blanchard 		gfn_t gfn;
9157924bd41SHollis Blanchard 		int gtlb_index;
916d9fbd03dSHollis Blanchard 
917d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
918d9fbd03dSHollis Blanchard 
919d9fbd03dSHollis Blanchard 		/* Check the guest TLB. */
920fa86b8ddSHollis Blanchard 		gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
9217924bd41SHollis Blanchard 		if (gtlb_index < 0) {
922d9fbd03dSHollis Blanchard 			/* The guest didn't have a mapping for it. */
923d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
924b52a638cSHollis Blanchard 			kvmppc_mmu_itlb_miss(vcpu);
9257b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
926d9fbd03dSHollis Blanchard 			break;
927d9fbd03dSHollis Blanchard 		}
928d9fbd03dSHollis Blanchard 
9297b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
930d9fbd03dSHollis Blanchard 
931be8d1caeSHollis Blanchard 		gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
93289168618SHollis Blanchard 		gfn = gpaddr >> PAGE_SHIFT;
933d9fbd03dSHollis Blanchard 
934d9fbd03dSHollis Blanchard 		if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
935d9fbd03dSHollis Blanchard 			/* The guest TLB had a mapping, but the shadow TLB
936d9fbd03dSHollis Blanchard 			 * didn't. This could be because:
937d9fbd03dSHollis Blanchard 			 * a) the entry is mapping the host kernel, or
938d9fbd03dSHollis Blanchard 			 * b) the guest used a large mapping which we're faking
939d9fbd03dSHollis Blanchard 			 * Either way, we need to satisfy the fault without
940d9fbd03dSHollis Blanchard 			 * invoking the guest. */
94158a96214SHollis Blanchard 			kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
942d9fbd03dSHollis Blanchard 		} else {
943d9fbd03dSHollis Blanchard 			/* Guest mapped and leaped at non-RAM! */
944d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
945d9fbd03dSHollis Blanchard 		}
946d9fbd03dSHollis Blanchard 
947d9fbd03dSHollis Blanchard 		break;
948d9fbd03dSHollis Blanchard 	}
949d9fbd03dSHollis Blanchard 
950d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DEBUG: {
951d9fbd03dSHollis Blanchard 		u32 dbsr;
952d9fbd03dSHollis Blanchard 
953d9fbd03dSHollis Blanchard 		vcpu->arch.pc = mfspr(SPRN_CSRR0);
954d9fbd03dSHollis Blanchard 
955d9fbd03dSHollis Blanchard 		/* clear IAC events in DBSR register */
956d9fbd03dSHollis Blanchard 		dbsr = mfspr(SPRN_DBSR);
957d9fbd03dSHollis Blanchard 		dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
958d9fbd03dSHollis Blanchard 		mtspr(SPRN_DBSR, dbsr);
959d9fbd03dSHollis Blanchard 
960d9fbd03dSHollis Blanchard 		run->exit_reason = KVM_EXIT_DEBUG;
9617b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, DEBUG_EXITS);
962d9fbd03dSHollis Blanchard 		r = RESUME_HOST;
963d9fbd03dSHollis Blanchard 		break;
964d9fbd03dSHollis Blanchard 	}
965d9fbd03dSHollis Blanchard 
966d9fbd03dSHollis Blanchard 	default:
967d9fbd03dSHollis Blanchard 		printk(KERN_EMERG "exit_nr %d\n", exit_nr);
968d9fbd03dSHollis Blanchard 		BUG();
969d9fbd03dSHollis Blanchard 	}
970d9fbd03dSHollis Blanchard 
971a8e4ef84SAlexander Graf 	/*
972a8e4ef84SAlexander Graf 	 * To avoid clobbering exit_reason, only check for signals if we
973a8e4ef84SAlexander Graf 	 * aren't already exiting to userspace for some other reason.
974a8e4ef84SAlexander Graf 	 */
97503660ba2SAlexander Graf 	if (!(r & RESUME_HOST)) {
976d9fbd03dSHollis Blanchard 		local_irq_disable();
97703660ba2SAlexander Graf 		if (kvmppc_prepare_to_enter(vcpu)) {
978d9fbd03dSHollis Blanchard 			run->exit_reason = KVM_EXIT_INTR;
979d9fbd03dSHollis Blanchard 			r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
9807b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, SIGNAL_EXITS);
981d9fbd03dSHollis Blanchard 		}
98203660ba2SAlexander Graf 	}
983d9fbd03dSHollis Blanchard 
984d9fbd03dSHollis Blanchard 	return r;
985d9fbd03dSHollis Blanchard }
986d9fbd03dSHollis Blanchard 
987d9fbd03dSHollis Blanchard /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
988d9fbd03dSHollis Blanchard int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
989d9fbd03dSHollis Blanchard {
990082decf2SHollis Blanchard 	int i;
991af8f38b3SAlexander Graf 	int r;
992082decf2SHollis Blanchard 
993d9fbd03dSHollis Blanchard 	vcpu->arch.pc = 0;
994b5904972SScott Wood 	vcpu->arch.shared->pir = vcpu->vcpu_id;
9958e5b26b5SAlexander Graf 	kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
996d30f6e48SScott Wood 	kvmppc_set_msr(vcpu, 0);
997d9fbd03dSHollis Blanchard 
998d30f6e48SScott Wood #ifndef CONFIG_KVM_BOOKE_HV
999d30f6e48SScott Wood 	vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
1000d9fbd03dSHollis Blanchard 	vcpu->arch.shadow_pid = 1;
1001d30f6e48SScott Wood 	vcpu->arch.shared->msr = 0;
1002d30f6e48SScott Wood #endif
1003d9fbd03dSHollis Blanchard 
1004082decf2SHollis Blanchard 	/* Eye-catching numbers so we know if the guest takes an interrupt
1005082decf2SHollis Blanchard 	 * before it's programmed its own IVPR/IVORs. */
1006d9fbd03dSHollis Blanchard 	vcpu->arch.ivpr = 0x55550000;
1007082decf2SHollis Blanchard 	for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
1008082decf2SHollis Blanchard 		vcpu->arch.ivor[i] = 0x7700 | i * 4;
1009d9fbd03dSHollis Blanchard 
101073e75b41SHollis Blanchard 	kvmppc_init_timing_stats(vcpu);
101173e75b41SHollis Blanchard 
1012af8f38b3SAlexander Graf 	r = kvmppc_core_vcpu_setup(vcpu);
1013af8f38b3SAlexander Graf 	kvmppc_sanity_check(vcpu);
1014af8f38b3SAlexander Graf 	return r;
1015d9fbd03dSHollis Blanchard }
1016d9fbd03dSHollis Blanchard 
1017d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1018d9fbd03dSHollis Blanchard {
1019d9fbd03dSHollis Blanchard 	int i;
1020d9fbd03dSHollis Blanchard 
1021d9fbd03dSHollis Blanchard 	regs->pc = vcpu->arch.pc;
1022992b5b29SAlexander Graf 	regs->cr = kvmppc_get_cr(vcpu);
1023d9fbd03dSHollis Blanchard 	regs->ctr = vcpu->arch.ctr;
1024d9fbd03dSHollis Blanchard 	regs->lr = vcpu->arch.lr;
1025992b5b29SAlexander Graf 	regs->xer = kvmppc_get_xer(vcpu);
1026666e7252SAlexander Graf 	regs->msr = vcpu->arch.shared->msr;
1027de7906c3SAlexander Graf 	regs->srr0 = vcpu->arch.shared->srr0;
1028de7906c3SAlexander Graf 	regs->srr1 = vcpu->arch.shared->srr1;
1029d9fbd03dSHollis Blanchard 	regs->pid = vcpu->arch.pid;
1030a73a9599SAlexander Graf 	regs->sprg0 = vcpu->arch.shared->sprg0;
1031a73a9599SAlexander Graf 	regs->sprg1 = vcpu->arch.shared->sprg1;
1032a73a9599SAlexander Graf 	regs->sprg2 = vcpu->arch.shared->sprg2;
1033a73a9599SAlexander Graf 	regs->sprg3 = vcpu->arch.shared->sprg3;
1034b5904972SScott Wood 	regs->sprg4 = vcpu->arch.shared->sprg4;
1035b5904972SScott Wood 	regs->sprg5 = vcpu->arch.shared->sprg5;
1036b5904972SScott Wood 	regs->sprg6 = vcpu->arch.shared->sprg6;
1037b5904972SScott Wood 	regs->sprg7 = vcpu->arch.shared->sprg7;
1038d9fbd03dSHollis Blanchard 
1039d9fbd03dSHollis Blanchard 	for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
10408e5b26b5SAlexander Graf 		regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
1041d9fbd03dSHollis Blanchard 
1042d9fbd03dSHollis Blanchard 	return 0;
1043d9fbd03dSHollis Blanchard }
1044d9fbd03dSHollis Blanchard 
1045d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1046d9fbd03dSHollis Blanchard {
1047d9fbd03dSHollis Blanchard 	int i;
1048d9fbd03dSHollis Blanchard 
1049d9fbd03dSHollis Blanchard 	vcpu->arch.pc = regs->pc;
1050992b5b29SAlexander Graf 	kvmppc_set_cr(vcpu, regs->cr);
1051d9fbd03dSHollis Blanchard 	vcpu->arch.ctr = regs->ctr;
1052d9fbd03dSHollis Blanchard 	vcpu->arch.lr = regs->lr;
1053992b5b29SAlexander Graf 	kvmppc_set_xer(vcpu, regs->xer);
1054b8fd68acSHollis Blanchard 	kvmppc_set_msr(vcpu, regs->msr);
1055de7906c3SAlexander Graf 	vcpu->arch.shared->srr0 = regs->srr0;
1056de7906c3SAlexander Graf 	vcpu->arch.shared->srr1 = regs->srr1;
10575ce941eeSScott Wood 	kvmppc_set_pid(vcpu, regs->pid);
1058a73a9599SAlexander Graf 	vcpu->arch.shared->sprg0 = regs->sprg0;
1059a73a9599SAlexander Graf 	vcpu->arch.shared->sprg1 = regs->sprg1;
1060a73a9599SAlexander Graf 	vcpu->arch.shared->sprg2 = regs->sprg2;
1061a73a9599SAlexander Graf 	vcpu->arch.shared->sprg3 = regs->sprg3;
1062b5904972SScott Wood 	vcpu->arch.shared->sprg4 = regs->sprg4;
1063b5904972SScott Wood 	vcpu->arch.shared->sprg5 = regs->sprg5;
1064b5904972SScott Wood 	vcpu->arch.shared->sprg6 = regs->sprg6;
1065b5904972SScott Wood 	vcpu->arch.shared->sprg7 = regs->sprg7;
1066d9fbd03dSHollis Blanchard 
10678e5b26b5SAlexander Graf 	for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
10688e5b26b5SAlexander Graf 		kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
1069d9fbd03dSHollis Blanchard 
1070d9fbd03dSHollis Blanchard 	return 0;
1071d9fbd03dSHollis Blanchard }
1072d9fbd03dSHollis Blanchard 
10735ce941eeSScott Wood static void get_sregs_base(struct kvm_vcpu *vcpu,
10745ce941eeSScott Wood                            struct kvm_sregs *sregs)
10755ce941eeSScott Wood {
10765ce941eeSScott Wood 	u64 tb = get_tb();
10775ce941eeSScott Wood 
10785ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_BASE;
10795ce941eeSScott Wood 
10805ce941eeSScott Wood 	sregs->u.e.csrr0 = vcpu->arch.csrr0;
10815ce941eeSScott Wood 	sregs->u.e.csrr1 = vcpu->arch.csrr1;
10825ce941eeSScott Wood 	sregs->u.e.mcsr = vcpu->arch.mcsr;
1083d30f6e48SScott Wood 	sregs->u.e.esr = get_guest_esr(vcpu);
1084d30f6e48SScott Wood 	sregs->u.e.dear = get_guest_dear(vcpu);
10855ce941eeSScott Wood 	sregs->u.e.tsr = vcpu->arch.tsr;
10865ce941eeSScott Wood 	sregs->u.e.tcr = vcpu->arch.tcr;
10875ce941eeSScott Wood 	sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
10885ce941eeSScott Wood 	sregs->u.e.tb = tb;
10895ce941eeSScott Wood 	sregs->u.e.vrsave = vcpu->arch.vrsave;
10905ce941eeSScott Wood }
10915ce941eeSScott Wood 
10925ce941eeSScott Wood static int set_sregs_base(struct kvm_vcpu *vcpu,
10935ce941eeSScott Wood                           struct kvm_sregs *sregs)
10945ce941eeSScott Wood {
10955ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
10965ce941eeSScott Wood 		return 0;
10975ce941eeSScott Wood 
10985ce941eeSScott Wood 	vcpu->arch.csrr0 = sregs->u.e.csrr0;
10995ce941eeSScott Wood 	vcpu->arch.csrr1 = sregs->u.e.csrr1;
11005ce941eeSScott Wood 	vcpu->arch.mcsr = sregs->u.e.mcsr;
1101d30f6e48SScott Wood 	set_guest_esr(vcpu, sregs->u.e.esr);
1102d30f6e48SScott Wood 	set_guest_dear(vcpu, sregs->u.e.dear);
11035ce941eeSScott Wood 	vcpu->arch.vrsave = sregs->u.e.vrsave;
1104dfd4d47eSScott Wood 	kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
11055ce941eeSScott Wood 
1106dfd4d47eSScott Wood 	if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
11075ce941eeSScott Wood 		vcpu->arch.dec = sregs->u.e.dec;
11085ce941eeSScott Wood 		kvmppc_emulate_dec(vcpu);
1109dfd4d47eSScott Wood 	}
11105ce941eeSScott Wood 
11115ce941eeSScott Wood 	if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
1112dfd4d47eSScott Wood 		vcpu->arch.tsr = sregs->u.e.tsr;
1113dfd4d47eSScott Wood 		update_timer_ints(vcpu);
11145ce941eeSScott Wood 	}
11155ce941eeSScott Wood 
11165ce941eeSScott Wood 	return 0;
11175ce941eeSScott Wood }
11185ce941eeSScott Wood 
11195ce941eeSScott Wood static void get_sregs_arch206(struct kvm_vcpu *vcpu,
11205ce941eeSScott Wood                               struct kvm_sregs *sregs)
11215ce941eeSScott Wood {
11225ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_ARCH206;
11235ce941eeSScott Wood 
1124841741f2SScott Wood 	sregs->u.e.pir = vcpu->vcpu_id;
11255ce941eeSScott Wood 	sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
11265ce941eeSScott Wood 	sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
11275ce941eeSScott Wood 	sregs->u.e.decar = vcpu->arch.decar;
11285ce941eeSScott Wood 	sregs->u.e.ivpr = vcpu->arch.ivpr;
11295ce941eeSScott Wood }
11305ce941eeSScott Wood 
11315ce941eeSScott Wood static int set_sregs_arch206(struct kvm_vcpu *vcpu,
11325ce941eeSScott Wood                              struct kvm_sregs *sregs)
11335ce941eeSScott Wood {
11345ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
11355ce941eeSScott Wood 		return 0;
11365ce941eeSScott Wood 
1137841741f2SScott Wood 	if (sregs->u.e.pir != vcpu->vcpu_id)
11385ce941eeSScott Wood 		return -EINVAL;
11395ce941eeSScott Wood 
11405ce941eeSScott Wood 	vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
11415ce941eeSScott Wood 	vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
11425ce941eeSScott Wood 	vcpu->arch.decar = sregs->u.e.decar;
11435ce941eeSScott Wood 	vcpu->arch.ivpr = sregs->u.e.ivpr;
11445ce941eeSScott Wood 
11455ce941eeSScott Wood 	return 0;
11465ce941eeSScott Wood }
11475ce941eeSScott Wood 
11485ce941eeSScott Wood void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
11495ce941eeSScott Wood {
11505ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_IVOR;
11515ce941eeSScott Wood 
11525ce941eeSScott Wood 	sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
11535ce941eeSScott Wood 	sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
11545ce941eeSScott Wood 	sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
11555ce941eeSScott Wood 	sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
11565ce941eeSScott Wood 	sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
11575ce941eeSScott Wood 	sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
11585ce941eeSScott Wood 	sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
11595ce941eeSScott Wood 	sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
11605ce941eeSScott Wood 	sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
11615ce941eeSScott Wood 	sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
11625ce941eeSScott Wood 	sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
11635ce941eeSScott Wood 	sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
11645ce941eeSScott Wood 	sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
11655ce941eeSScott Wood 	sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
11665ce941eeSScott Wood 	sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
11675ce941eeSScott Wood 	sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
11685ce941eeSScott Wood }
11695ce941eeSScott Wood 
11705ce941eeSScott Wood int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
11715ce941eeSScott Wood {
11725ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
11735ce941eeSScott Wood 		return 0;
11745ce941eeSScott Wood 
11755ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
11765ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
11775ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
11785ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
11795ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
11805ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
11815ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
11825ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
11835ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
11845ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
11855ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
11865ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
11875ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
11885ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
11895ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
11905ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
11915ce941eeSScott Wood 
11925ce941eeSScott Wood 	return 0;
11935ce941eeSScott Wood }
11945ce941eeSScott Wood 
1195d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1196d9fbd03dSHollis Blanchard                                   struct kvm_sregs *sregs)
1197d9fbd03dSHollis Blanchard {
11985ce941eeSScott Wood 	sregs->pvr = vcpu->arch.pvr;
11995ce941eeSScott Wood 
12005ce941eeSScott Wood 	get_sregs_base(vcpu, sregs);
12015ce941eeSScott Wood 	get_sregs_arch206(vcpu, sregs);
12025ce941eeSScott Wood 	kvmppc_core_get_sregs(vcpu, sregs);
12035ce941eeSScott Wood 	return 0;
1204d9fbd03dSHollis Blanchard }
1205d9fbd03dSHollis Blanchard 
1206d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1207d9fbd03dSHollis Blanchard                                   struct kvm_sregs *sregs)
1208d9fbd03dSHollis Blanchard {
12095ce941eeSScott Wood 	int ret;
12105ce941eeSScott Wood 
12115ce941eeSScott Wood 	if (vcpu->arch.pvr != sregs->pvr)
12125ce941eeSScott Wood 		return -EINVAL;
12135ce941eeSScott Wood 
12145ce941eeSScott Wood 	ret = set_sregs_base(vcpu, sregs);
12155ce941eeSScott Wood 	if (ret < 0)
12165ce941eeSScott Wood 		return ret;
12175ce941eeSScott Wood 
12185ce941eeSScott Wood 	ret = set_sregs_arch206(vcpu, sregs);
12195ce941eeSScott Wood 	if (ret < 0)
12205ce941eeSScott Wood 		return ret;
12215ce941eeSScott Wood 
12225ce941eeSScott Wood 	return kvmppc_core_set_sregs(vcpu, sregs);
1223d9fbd03dSHollis Blanchard }
1224d9fbd03dSHollis Blanchard 
122531f3438eSPaul Mackerras int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
122631f3438eSPaul Mackerras {
122731f3438eSPaul Mackerras 	return -EINVAL;
122831f3438eSPaul Mackerras }
122931f3438eSPaul Mackerras 
123031f3438eSPaul Mackerras int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
123131f3438eSPaul Mackerras {
123231f3438eSPaul Mackerras 	return -EINVAL;
123331f3438eSPaul Mackerras }
123431f3438eSPaul Mackerras 
1235d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1236d9fbd03dSHollis Blanchard {
1237d9fbd03dSHollis Blanchard 	return -ENOTSUPP;
1238d9fbd03dSHollis Blanchard }
1239d9fbd03dSHollis Blanchard 
1240d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1241d9fbd03dSHollis Blanchard {
1242d9fbd03dSHollis Blanchard 	return -ENOTSUPP;
1243d9fbd03dSHollis Blanchard }
1244d9fbd03dSHollis Blanchard 
1245d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1246d9fbd03dSHollis Blanchard                                   struct kvm_translation *tr)
1247d9fbd03dSHollis Blanchard {
124898001d8dSAvi Kivity 	int r;
124998001d8dSAvi Kivity 
125098001d8dSAvi Kivity 	r = kvmppc_core_vcpu_translate(vcpu, tr);
125198001d8dSAvi Kivity 	return r;
1252d9fbd03dSHollis Blanchard }
1253d9fbd03dSHollis Blanchard 
12544e755758SAlexander Graf int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
12554e755758SAlexander Graf {
12564e755758SAlexander Graf 	return -ENOTSUPP;
12574e755758SAlexander Graf }
12584e755758SAlexander Graf 
1259f9e0554dSPaul Mackerras int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1260f9e0554dSPaul Mackerras 				      struct kvm_userspace_memory_region *mem)
1261f9e0554dSPaul Mackerras {
1262f9e0554dSPaul Mackerras 	return 0;
1263f9e0554dSPaul Mackerras }
1264f9e0554dSPaul Mackerras 
1265f9e0554dSPaul Mackerras void kvmppc_core_commit_memory_region(struct kvm *kvm,
1266f9e0554dSPaul Mackerras 				struct kvm_userspace_memory_region *mem)
1267f9e0554dSPaul Mackerras {
1268f9e0554dSPaul Mackerras }
1269f9e0554dSPaul Mackerras 
1270dfd4d47eSScott Wood void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1271dfd4d47eSScott Wood {
1272dfd4d47eSScott Wood 	vcpu->arch.tcr = new_tcr;
1273dfd4d47eSScott Wood 	update_timer_ints(vcpu);
1274dfd4d47eSScott Wood }
1275dfd4d47eSScott Wood 
1276dfd4d47eSScott Wood void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1277dfd4d47eSScott Wood {
1278dfd4d47eSScott Wood 	set_bits(tsr_bits, &vcpu->arch.tsr);
1279dfd4d47eSScott Wood 	smp_wmb();
1280dfd4d47eSScott Wood 	kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1281dfd4d47eSScott Wood 	kvm_vcpu_kick(vcpu);
1282dfd4d47eSScott Wood }
1283dfd4d47eSScott Wood 
1284dfd4d47eSScott Wood void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1285dfd4d47eSScott Wood {
1286dfd4d47eSScott Wood 	clear_bits(tsr_bits, &vcpu->arch.tsr);
1287dfd4d47eSScott Wood 	update_timer_ints(vcpu);
1288dfd4d47eSScott Wood }
1289dfd4d47eSScott Wood 
1290dfd4d47eSScott Wood void kvmppc_decrementer_func(unsigned long data)
1291dfd4d47eSScott Wood {
1292dfd4d47eSScott Wood 	struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1293dfd4d47eSScott Wood 
129421bd000aSBharat Bhushan 	if (vcpu->arch.tcr & TCR_ARE) {
129521bd000aSBharat Bhushan 		vcpu->arch.dec = vcpu->arch.decar;
129621bd000aSBharat Bhushan 		kvmppc_emulate_dec(vcpu);
129721bd000aSBharat Bhushan 	}
129821bd000aSBharat Bhushan 
1299dfd4d47eSScott Wood 	kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1300dfd4d47eSScott Wood }
1301dfd4d47eSScott Wood 
130294fa9d99SScott Wood void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
130394fa9d99SScott Wood {
1304d30f6e48SScott Wood 	current->thread.kvm_vcpu = vcpu;
130594fa9d99SScott Wood }
130694fa9d99SScott Wood 
130794fa9d99SScott Wood void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
130894fa9d99SScott Wood {
1309d30f6e48SScott Wood 	current->thread.kvm_vcpu = NULL;
131094fa9d99SScott Wood }
131194fa9d99SScott Wood 
13122986b8c7SStephen Rothwell int __init kvmppc_booke_init(void)
1313d9fbd03dSHollis Blanchard {
1314d30f6e48SScott Wood #ifndef CONFIG_KVM_BOOKE_HV
1315d9fbd03dSHollis Blanchard 	unsigned long ivor[16];
1316d9fbd03dSHollis Blanchard 	unsigned long max_ivor = 0;
1317d9fbd03dSHollis Blanchard 	int i;
1318d9fbd03dSHollis Blanchard 
1319d9fbd03dSHollis Blanchard 	/* We install our own exception handlers by hijacking IVPR. IVPR must
1320d9fbd03dSHollis Blanchard 	 * be 16-bit aligned, so we need a 64KB allocation. */
1321d9fbd03dSHollis Blanchard 	kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1322d9fbd03dSHollis Blanchard 	                                         VCPU_SIZE_ORDER);
1323d9fbd03dSHollis Blanchard 	if (!kvmppc_booke_handlers)
1324d9fbd03dSHollis Blanchard 		return -ENOMEM;
1325d9fbd03dSHollis Blanchard 
1326d9fbd03dSHollis Blanchard 	/* XXX make sure our handlers are smaller than Linux's */
1327d9fbd03dSHollis Blanchard 
1328d9fbd03dSHollis Blanchard 	/* Copy our interrupt handlers to match host IVORs. That way we don't
1329d9fbd03dSHollis Blanchard 	 * have to swap the IVORs on every guest/host transition. */
1330d9fbd03dSHollis Blanchard 	ivor[0] = mfspr(SPRN_IVOR0);
1331d9fbd03dSHollis Blanchard 	ivor[1] = mfspr(SPRN_IVOR1);
1332d9fbd03dSHollis Blanchard 	ivor[2] = mfspr(SPRN_IVOR2);
1333d9fbd03dSHollis Blanchard 	ivor[3] = mfspr(SPRN_IVOR3);
1334d9fbd03dSHollis Blanchard 	ivor[4] = mfspr(SPRN_IVOR4);
1335d9fbd03dSHollis Blanchard 	ivor[5] = mfspr(SPRN_IVOR5);
1336d9fbd03dSHollis Blanchard 	ivor[6] = mfspr(SPRN_IVOR6);
1337d9fbd03dSHollis Blanchard 	ivor[7] = mfspr(SPRN_IVOR7);
1338d9fbd03dSHollis Blanchard 	ivor[8] = mfspr(SPRN_IVOR8);
1339d9fbd03dSHollis Blanchard 	ivor[9] = mfspr(SPRN_IVOR9);
1340d9fbd03dSHollis Blanchard 	ivor[10] = mfspr(SPRN_IVOR10);
1341d9fbd03dSHollis Blanchard 	ivor[11] = mfspr(SPRN_IVOR11);
1342d9fbd03dSHollis Blanchard 	ivor[12] = mfspr(SPRN_IVOR12);
1343d9fbd03dSHollis Blanchard 	ivor[13] = mfspr(SPRN_IVOR13);
1344d9fbd03dSHollis Blanchard 	ivor[14] = mfspr(SPRN_IVOR14);
1345d9fbd03dSHollis Blanchard 	ivor[15] = mfspr(SPRN_IVOR15);
1346d9fbd03dSHollis Blanchard 
1347d9fbd03dSHollis Blanchard 	for (i = 0; i < 16; i++) {
1348d9fbd03dSHollis Blanchard 		if (ivor[i] > max_ivor)
1349d9fbd03dSHollis Blanchard 			max_ivor = ivor[i];
1350d9fbd03dSHollis Blanchard 
1351d9fbd03dSHollis Blanchard 		memcpy((void *)kvmppc_booke_handlers + ivor[i],
1352d9fbd03dSHollis Blanchard 		       kvmppc_handlers_start + i * kvmppc_handler_len,
1353d9fbd03dSHollis Blanchard 		       kvmppc_handler_len);
1354d9fbd03dSHollis Blanchard 	}
1355d9fbd03dSHollis Blanchard 	flush_icache_range(kvmppc_booke_handlers,
1356d9fbd03dSHollis Blanchard 	                   kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
1357d30f6e48SScott Wood #endif /* !BOOKE_HV */
1358db93f574SHollis Blanchard 	return 0;
1359d9fbd03dSHollis Blanchard }
1360d9fbd03dSHollis Blanchard 
1361db93f574SHollis Blanchard void __exit kvmppc_booke_exit(void)
1362d9fbd03dSHollis Blanchard {
1363d9fbd03dSHollis Blanchard 	free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
1364d9fbd03dSHollis Blanchard 	kvm_exit();
1365d9fbd03dSHollis Blanchard }
1366