xref: /openbmc/linux/arch/powerpc/kvm/booke.c (revision cbbc58d4fdfab1a39a6ac1b41fcb17885952157a)
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>
39b50df19cSMihai Caraman #include <asm/time.h>
40d9fbd03dSHollis Blanchard 
41d30f6e48SScott Wood #include "timing.h"
4275f74f0dSHollis Blanchard #include "booke.h"
43dba291f2SAneesh Kumar K.V 
44dba291f2SAneesh Kumar K.V #define CREATE_TRACE_POINTS
45dba291f2SAneesh Kumar K.V #include "trace_booke.h"
46d9fbd03dSHollis Blanchard 
47d9fbd03dSHollis Blanchard unsigned long kvmppc_booke_handlers;
48d9fbd03dSHollis Blanchard 
49d9fbd03dSHollis Blanchard #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
50d9fbd03dSHollis Blanchard #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
51d9fbd03dSHollis Blanchard 
52d9fbd03dSHollis Blanchard struct kvm_stats_debugfs_item debugfs_entries[] = {
53d9fbd03dSHollis Blanchard 	{ "mmio",       VCPU_STAT(mmio_exits) },
54d9fbd03dSHollis Blanchard 	{ "dcr",        VCPU_STAT(dcr_exits) },
55d9fbd03dSHollis Blanchard 	{ "sig",        VCPU_STAT(signal_exits) },
56d9fbd03dSHollis Blanchard 	{ "itlb_r",     VCPU_STAT(itlb_real_miss_exits) },
57d9fbd03dSHollis Blanchard 	{ "itlb_v",     VCPU_STAT(itlb_virt_miss_exits) },
58d9fbd03dSHollis Blanchard 	{ "dtlb_r",     VCPU_STAT(dtlb_real_miss_exits) },
59d9fbd03dSHollis Blanchard 	{ "dtlb_v",     VCPU_STAT(dtlb_virt_miss_exits) },
60d9fbd03dSHollis Blanchard 	{ "sysc",       VCPU_STAT(syscall_exits) },
61d9fbd03dSHollis Blanchard 	{ "isi",        VCPU_STAT(isi_exits) },
62d9fbd03dSHollis Blanchard 	{ "dsi",        VCPU_STAT(dsi_exits) },
63d9fbd03dSHollis Blanchard 	{ "inst_emu",   VCPU_STAT(emulated_inst_exits) },
64d9fbd03dSHollis Blanchard 	{ "dec",        VCPU_STAT(dec_exits) },
65d9fbd03dSHollis Blanchard 	{ "ext_intr",   VCPU_STAT(ext_intr_exits) },
66d9fbd03dSHollis Blanchard 	{ "halt_wakeup", VCPU_STAT(halt_wakeup) },
67d30f6e48SScott Wood 	{ "doorbell", VCPU_STAT(dbell_exits) },
68d30f6e48SScott Wood 	{ "guest doorbell", VCPU_STAT(gdbell_exits) },
69cf1c5ca4SAlexander Graf 	{ "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
70d9fbd03dSHollis Blanchard 	{ NULL }
71d9fbd03dSHollis Blanchard };
72d9fbd03dSHollis Blanchard 
73d9fbd03dSHollis Blanchard /* TODO: use vcpu_printf() */
74d9fbd03dSHollis Blanchard void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
75d9fbd03dSHollis Blanchard {
76d9fbd03dSHollis Blanchard 	int i;
77d9fbd03dSHollis Blanchard 
78666e7252SAlexander Graf 	printk("pc:   %08lx msr:  %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
795cf8ca22SHollis Blanchard 	printk("lr:   %08lx ctr:  %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
80de7906c3SAlexander Graf 	printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
81de7906c3SAlexander Graf 					    vcpu->arch.shared->srr1);
82d9fbd03dSHollis Blanchard 
83d9fbd03dSHollis Blanchard 	printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
84d9fbd03dSHollis Blanchard 
85d9fbd03dSHollis Blanchard 	for (i = 0; i < 32; i += 4) {
865cf8ca22SHollis Blanchard 		printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
878e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i),
888e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i+1),
898e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i+2),
908e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i+3));
91d9fbd03dSHollis Blanchard 	}
92d9fbd03dSHollis Blanchard }
93d9fbd03dSHollis Blanchard 
944cd35f67SScott Wood #ifdef CONFIG_SPE
954cd35f67SScott Wood void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
964cd35f67SScott Wood {
974cd35f67SScott Wood 	preempt_disable();
984cd35f67SScott Wood 	enable_kernel_spe();
994cd35f67SScott Wood 	kvmppc_save_guest_spe(vcpu);
1004cd35f67SScott Wood 	vcpu->arch.shadow_msr &= ~MSR_SPE;
1014cd35f67SScott Wood 	preempt_enable();
1024cd35f67SScott Wood }
1034cd35f67SScott Wood 
1044cd35f67SScott Wood static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
1054cd35f67SScott Wood {
1064cd35f67SScott Wood 	preempt_disable();
1074cd35f67SScott Wood 	enable_kernel_spe();
1084cd35f67SScott Wood 	kvmppc_load_guest_spe(vcpu);
1094cd35f67SScott Wood 	vcpu->arch.shadow_msr |= MSR_SPE;
1104cd35f67SScott Wood 	preempt_enable();
1114cd35f67SScott Wood }
1124cd35f67SScott Wood 
1134cd35f67SScott Wood static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
1144cd35f67SScott Wood {
1154cd35f67SScott Wood 	if (vcpu->arch.shared->msr & MSR_SPE) {
1164cd35f67SScott Wood 		if (!(vcpu->arch.shadow_msr & MSR_SPE))
1174cd35f67SScott Wood 			kvmppc_vcpu_enable_spe(vcpu);
1184cd35f67SScott Wood 	} else if (vcpu->arch.shadow_msr & MSR_SPE) {
1194cd35f67SScott Wood 		kvmppc_vcpu_disable_spe(vcpu);
1204cd35f67SScott Wood 	}
1214cd35f67SScott Wood }
1224cd35f67SScott Wood #else
1234cd35f67SScott Wood static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
1244cd35f67SScott Wood {
1254cd35f67SScott Wood }
1264cd35f67SScott Wood #endif
1274cd35f67SScott Wood 
1287a08c274SAlexander Graf static void kvmppc_vcpu_sync_fpu(struct kvm_vcpu *vcpu)
1297a08c274SAlexander Graf {
1307a08c274SAlexander Graf #if defined(CONFIG_PPC_FPU) && !defined(CONFIG_KVM_BOOKE_HV)
1317a08c274SAlexander Graf 	/* We always treat the FP bit as enabled from the host
1327a08c274SAlexander Graf 	   perspective, so only need to adjust the shadow MSR */
1337a08c274SAlexander Graf 	vcpu->arch.shadow_msr &= ~MSR_FP;
1347a08c274SAlexander Graf 	vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_FP;
1357a08c274SAlexander Graf #endif
1367a08c274SAlexander Graf }
1377a08c274SAlexander Graf 
138ce11e48bSBharat Bhushan static void kvmppc_vcpu_sync_debug(struct kvm_vcpu *vcpu)
139ce11e48bSBharat Bhushan {
140ce11e48bSBharat Bhushan 	/* Synchronize guest's desire to get debug interrupts into shadow MSR */
141ce11e48bSBharat Bhushan #ifndef CONFIG_KVM_BOOKE_HV
142ce11e48bSBharat Bhushan 	vcpu->arch.shadow_msr &= ~MSR_DE;
143ce11e48bSBharat Bhushan 	vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_DE;
144ce11e48bSBharat Bhushan #endif
145ce11e48bSBharat Bhushan 
146ce11e48bSBharat Bhushan 	/* Force enable debug interrupts when user space wants to debug */
147ce11e48bSBharat Bhushan 	if (vcpu->guest_debug) {
148ce11e48bSBharat Bhushan #ifdef CONFIG_KVM_BOOKE_HV
149ce11e48bSBharat Bhushan 		/*
150ce11e48bSBharat Bhushan 		 * Since there is no shadow MSR, sync MSR_DE into the guest
151ce11e48bSBharat Bhushan 		 * visible MSR.
152ce11e48bSBharat Bhushan 		 */
153ce11e48bSBharat Bhushan 		vcpu->arch.shared->msr |= MSR_DE;
154ce11e48bSBharat Bhushan #else
155ce11e48bSBharat Bhushan 		vcpu->arch.shadow_msr |= MSR_DE;
156ce11e48bSBharat Bhushan 		vcpu->arch.shared->msr &= ~MSR_DE;
157ce11e48bSBharat Bhushan #endif
158ce11e48bSBharat Bhushan 	}
159ce11e48bSBharat Bhushan }
160ce11e48bSBharat Bhushan 
161dd9ebf1fSLiu Yu /*
162dd9ebf1fSLiu Yu  * Helper function for "full" MSR writes.  No need to call this if only
163dd9ebf1fSLiu Yu  * EE/CE/ME/DE/RI are changing.
164dd9ebf1fSLiu Yu  */
1654cd35f67SScott Wood void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
1664cd35f67SScott Wood {
167dd9ebf1fSLiu Yu 	u32 old_msr = vcpu->arch.shared->msr;
1684cd35f67SScott Wood 
169d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
170d30f6e48SScott Wood 	new_msr |= MSR_GS;
171d30f6e48SScott Wood #endif
172d30f6e48SScott Wood 
1734cd35f67SScott Wood 	vcpu->arch.shared->msr = new_msr;
1744cd35f67SScott Wood 
175dd9ebf1fSLiu Yu 	kvmppc_mmu_msr_notify(vcpu, old_msr);
1764cd35f67SScott Wood 	kvmppc_vcpu_sync_spe(vcpu);
1777a08c274SAlexander Graf 	kvmppc_vcpu_sync_fpu(vcpu);
178ce11e48bSBharat Bhushan 	kvmppc_vcpu_sync_debug(vcpu);
1794cd35f67SScott Wood }
1804cd35f67SScott Wood 
181d4cf3892SHollis Blanchard static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
182d4cf3892SHollis Blanchard                                        unsigned int priority)
1839dd921cfSHollis Blanchard {
1846346046cSAlexander Graf 	trace_kvm_booke_queue_irqprio(vcpu, priority);
1859dd921cfSHollis Blanchard 	set_bit(priority, &vcpu->arch.pending_exceptions);
1869dd921cfSHollis Blanchard }
1879dd921cfSHollis Blanchard 
188daf5e271SLiu Yu static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
189daf5e271SLiu Yu                                         ulong dear_flags, ulong esr_flags)
1909dd921cfSHollis Blanchard {
191daf5e271SLiu Yu 	vcpu->arch.queued_dear = dear_flags;
192daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
193daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
194daf5e271SLiu Yu }
195daf5e271SLiu Yu 
196daf5e271SLiu Yu static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
197daf5e271SLiu Yu                                            ulong dear_flags, ulong esr_flags)
198daf5e271SLiu Yu {
199daf5e271SLiu Yu 	vcpu->arch.queued_dear = dear_flags;
200daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
201daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
202daf5e271SLiu Yu }
203daf5e271SLiu Yu 
204daf5e271SLiu Yu static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
205daf5e271SLiu Yu                                            ulong esr_flags)
206daf5e271SLiu Yu {
207daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
208daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
209daf5e271SLiu Yu }
210daf5e271SLiu Yu 
211011da899SAlexander Graf static void kvmppc_core_queue_alignment(struct kvm_vcpu *vcpu, ulong dear_flags,
212011da899SAlexander Graf 					ulong esr_flags)
213011da899SAlexander Graf {
214011da899SAlexander Graf 	vcpu->arch.queued_dear = dear_flags;
215011da899SAlexander Graf 	vcpu->arch.queued_esr = esr_flags;
216011da899SAlexander Graf 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALIGNMENT);
217011da899SAlexander Graf }
218011da899SAlexander Graf 
219daf5e271SLiu Yu void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
220daf5e271SLiu Yu {
221daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
222d4cf3892SHollis Blanchard 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
2239dd921cfSHollis Blanchard }
2249dd921cfSHollis Blanchard 
2259dd921cfSHollis Blanchard void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
2269dd921cfSHollis Blanchard {
227d4cf3892SHollis Blanchard 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
2289dd921cfSHollis Blanchard }
2299dd921cfSHollis Blanchard 
2309dd921cfSHollis Blanchard int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
2319dd921cfSHollis Blanchard {
232d4cf3892SHollis Blanchard 	return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
2339dd921cfSHollis Blanchard }
2349dd921cfSHollis Blanchard 
2357706664dSAlexander Graf void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
2367706664dSAlexander Graf {
2377706664dSAlexander Graf 	clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
2387706664dSAlexander Graf }
2397706664dSAlexander Graf 
2409dd921cfSHollis Blanchard void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
2419dd921cfSHollis Blanchard                                 struct kvm_interrupt *irq)
2429dd921cfSHollis Blanchard {
243c5335f17SAlexander Graf 	unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
244c5335f17SAlexander Graf 
245c5335f17SAlexander Graf 	if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
246c5335f17SAlexander Graf 		prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
247c5335f17SAlexander Graf 
248c5335f17SAlexander Graf 	kvmppc_booke_queue_irqprio(vcpu, prio);
2499dd921cfSHollis Blanchard }
2509dd921cfSHollis Blanchard 
2514fe27d2aSPaul Mackerras void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu)
2524496f974SAlexander Graf {
2534496f974SAlexander Graf 	clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
254c5335f17SAlexander Graf 	clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
2554496f974SAlexander Graf }
2564496f974SAlexander Graf 
257f61c94bbSBharat Bhushan static void kvmppc_core_queue_watchdog(struct kvm_vcpu *vcpu)
258f61c94bbSBharat Bhushan {
259f61c94bbSBharat Bhushan 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_WATCHDOG);
260f61c94bbSBharat Bhushan }
261f61c94bbSBharat Bhushan 
262f61c94bbSBharat Bhushan static void kvmppc_core_dequeue_watchdog(struct kvm_vcpu *vcpu)
263f61c94bbSBharat Bhushan {
264f61c94bbSBharat Bhushan 	clear_bit(BOOKE_IRQPRIO_WATCHDOG, &vcpu->arch.pending_exceptions);
265f61c94bbSBharat Bhushan }
266f61c94bbSBharat Bhushan 
267d30f6e48SScott Wood static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
268d30f6e48SScott Wood {
269d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
270d30f6e48SScott Wood 	mtspr(SPRN_GSRR0, srr0);
271d30f6e48SScott Wood 	mtspr(SPRN_GSRR1, srr1);
272d30f6e48SScott Wood #else
273d30f6e48SScott Wood 	vcpu->arch.shared->srr0 = srr0;
274d30f6e48SScott Wood 	vcpu->arch.shared->srr1 = srr1;
275d30f6e48SScott Wood #endif
276d30f6e48SScott Wood }
277d30f6e48SScott Wood 
278d30f6e48SScott Wood static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
279d30f6e48SScott Wood {
280d30f6e48SScott Wood 	vcpu->arch.csrr0 = srr0;
281d30f6e48SScott Wood 	vcpu->arch.csrr1 = srr1;
282d30f6e48SScott Wood }
283d30f6e48SScott Wood 
284d30f6e48SScott Wood static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
285d30f6e48SScott Wood {
286d30f6e48SScott Wood 	if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
287d30f6e48SScott Wood 		vcpu->arch.dsrr0 = srr0;
288d30f6e48SScott Wood 		vcpu->arch.dsrr1 = srr1;
289d30f6e48SScott Wood 	} else {
290d30f6e48SScott Wood 		set_guest_csrr(vcpu, srr0, srr1);
291d30f6e48SScott Wood 	}
292d30f6e48SScott Wood }
293d30f6e48SScott Wood 
294d30f6e48SScott Wood static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
295d30f6e48SScott Wood {
296d30f6e48SScott Wood 	vcpu->arch.mcsrr0 = srr0;
297d30f6e48SScott Wood 	vcpu->arch.mcsrr1 = srr1;
298d30f6e48SScott Wood }
299d30f6e48SScott Wood 
300d30f6e48SScott Wood static unsigned long get_guest_dear(struct kvm_vcpu *vcpu)
301d30f6e48SScott Wood {
302d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
303d30f6e48SScott Wood 	return mfspr(SPRN_GDEAR);
304d30f6e48SScott Wood #else
305d30f6e48SScott Wood 	return vcpu->arch.shared->dar;
306d30f6e48SScott Wood #endif
307d30f6e48SScott Wood }
308d30f6e48SScott Wood 
309d30f6e48SScott Wood static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear)
310d30f6e48SScott Wood {
311d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
312d30f6e48SScott Wood 	mtspr(SPRN_GDEAR, dear);
313d30f6e48SScott Wood #else
314d30f6e48SScott Wood 	vcpu->arch.shared->dar = dear;
315d30f6e48SScott Wood #endif
316d30f6e48SScott Wood }
317d30f6e48SScott Wood 
318d30f6e48SScott Wood static unsigned long get_guest_esr(struct kvm_vcpu *vcpu)
319d30f6e48SScott Wood {
320d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
321d30f6e48SScott Wood 	return mfspr(SPRN_GESR);
322d30f6e48SScott Wood #else
323d30f6e48SScott Wood 	return vcpu->arch.shared->esr;
324d30f6e48SScott Wood #endif
325d30f6e48SScott Wood }
326d30f6e48SScott Wood 
327d30f6e48SScott Wood static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr)
328d30f6e48SScott Wood {
329d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
330d30f6e48SScott Wood 	mtspr(SPRN_GESR, esr);
331d30f6e48SScott Wood #else
332d30f6e48SScott Wood 	vcpu->arch.shared->esr = esr;
333d30f6e48SScott Wood #endif
334d30f6e48SScott Wood }
335d30f6e48SScott Wood 
336324b3e63SAlexander Graf static unsigned long get_guest_epr(struct kvm_vcpu *vcpu)
337324b3e63SAlexander Graf {
338324b3e63SAlexander Graf #ifdef CONFIG_KVM_BOOKE_HV
339324b3e63SAlexander Graf 	return mfspr(SPRN_GEPR);
340324b3e63SAlexander Graf #else
341324b3e63SAlexander Graf 	return vcpu->arch.epr;
342324b3e63SAlexander Graf #endif
343324b3e63SAlexander Graf }
344324b3e63SAlexander Graf 
345d4cf3892SHollis Blanchard /* Deliver the interrupt of the corresponding priority, if possible. */
346d4cf3892SHollis Blanchard static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
347d4cf3892SHollis Blanchard                                         unsigned int priority)
348d9fbd03dSHollis Blanchard {
349d4cf3892SHollis Blanchard 	int allowed = 0;
35079300f8cSAlexander Graf 	ulong msr_mask = 0;
3511c810636SAlexander Graf 	bool update_esr = false, update_dear = false, update_epr = false;
3525c6cedf4SAlexander Graf 	ulong crit_raw = vcpu->arch.shared->critical;
3535c6cedf4SAlexander Graf 	ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
3545c6cedf4SAlexander Graf 	bool crit;
355c5335f17SAlexander Graf 	bool keep_irq = false;
356d30f6e48SScott Wood 	enum int_class int_class;
35795e90b43SMihai Caraman 	ulong new_msr = vcpu->arch.shared->msr;
3585c6cedf4SAlexander Graf 
3595c6cedf4SAlexander Graf 	/* Truncate crit indicators in 32 bit mode */
3605c6cedf4SAlexander Graf 	if (!(vcpu->arch.shared->msr & MSR_SF)) {
3615c6cedf4SAlexander Graf 		crit_raw &= 0xffffffff;
3625c6cedf4SAlexander Graf 		crit_r1 &= 0xffffffff;
3635c6cedf4SAlexander Graf 	}
3645c6cedf4SAlexander Graf 
3655c6cedf4SAlexander Graf 	/* Critical section when crit == r1 */
3665c6cedf4SAlexander Graf 	crit = (crit_raw == crit_r1);
3675c6cedf4SAlexander Graf 	/* ... and we're in supervisor mode */
3685c6cedf4SAlexander Graf 	crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
369d9fbd03dSHollis Blanchard 
370c5335f17SAlexander Graf 	if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
371c5335f17SAlexander Graf 		priority = BOOKE_IRQPRIO_EXTERNAL;
372c5335f17SAlexander Graf 		keep_irq = true;
373c5335f17SAlexander Graf 	}
374c5335f17SAlexander Graf 
3755df554adSScott Wood 	if ((priority == BOOKE_IRQPRIO_EXTERNAL) && vcpu->arch.epr_flags)
3761c810636SAlexander Graf 		update_epr = true;
3771c810636SAlexander Graf 
378d4cf3892SHollis Blanchard 	switch (priority) {
379d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DTLB_MISS:
380daf5e271SLiu Yu 	case BOOKE_IRQPRIO_DATA_STORAGE:
381011da899SAlexander Graf 	case BOOKE_IRQPRIO_ALIGNMENT:
382daf5e271SLiu Yu 		update_dear = true;
383daf5e271SLiu Yu 		/* fall through */
384daf5e271SLiu Yu 	case BOOKE_IRQPRIO_INST_STORAGE:
385daf5e271SLiu Yu 	case BOOKE_IRQPRIO_PROGRAM:
386daf5e271SLiu Yu 		update_esr = true;
387daf5e271SLiu Yu 		/* fall through */
388d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_ITLB_MISS:
389d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_SYSCALL:
390d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_FP_UNAVAIL:
391bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_UNAVAIL:
392bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_FP_DATA:
393bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_FP_ROUND:
394d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_AP_UNAVAIL:
395d4cf3892SHollis Blanchard 		allowed = 1;
39679300f8cSAlexander Graf 		msr_mask = MSR_CE | MSR_ME | MSR_DE;
397d30f6e48SScott Wood 		int_class = INT_CLASS_NONCRIT;
398d9fbd03dSHollis Blanchard 		break;
399f61c94bbSBharat Bhushan 	case BOOKE_IRQPRIO_WATCHDOG:
400d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_CRITICAL:
4014ab96919SAlexander Graf 	case BOOKE_IRQPRIO_DBELL_CRIT:
402666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_CE;
403d30f6e48SScott Wood 		allowed = allowed && !crit;
40479300f8cSAlexander Graf 		msr_mask = MSR_ME;
405d30f6e48SScott Wood 		int_class = INT_CLASS_CRIT;
406d9fbd03dSHollis Blanchard 		break;
407d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_MACHINE_CHECK:
408666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_ME;
409d30f6e48SScott Wood 		allowed = allowed && !crit;
410d30f6e48SScott Wood 		int_class = INT_CLASS_MC;
411d9fbd03dSHollis Blanchard 		break;
412d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DECREMENTER:
413d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_FIT:
414dfd4d47eSScott Wood 		keep_irq = true;
415dfd4d47eSScott Wood 		/* fall through */
416dfd4d47eSScott Wood 	case BOOKE_IRQPRIO_EXTERNAL:
4174ab96919SAlexander Graf 	case BOOKE_IRQPRIO_DBELL:
418666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_EE;
4195c6cedf4SAlexander Graf 		allowed = allowed && !crit;
42079300f8cSAlexander Graf 		msr_mask = MSR_CE | MSR_ME | MSR_DE;
421d30f6e48SScott Wood 		int_class = INT_CLASS_NONCRIT;
422d9fbd03dSHollis Blanchard 		break;
423d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DEBUG:
424666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_DE;
425d30f6e48SScott Wood 		allowed = allowed && !crit;
42679300f8cSAlexander Graf 		msr_mask = MSR_ME;
427d30f6e48SScott Wood 		int_class = INT_CLASS_CRIT;
428d9fbd03dSHollis Blanchard 		break;
429d9fbd03dSHollis Blanchard 	}
430d9fbd03dSHollis Blanchard 
431d4cf3892SHollis Blanchard 	if (allowed) {
432d30f6e48SScott Wood 		switch (int_class) {
433d30f6e48SScott Wood 		case INT_CLASS_NONCRIT:
434d30f6e48SScott Wood 			set_guest_srr(vcpu, vcpu->arch.pc,
435d30f6e48SScott Wood 				      vcpu->arch.shared->msr);
436d30f6e48SScott Wood 			break;
437d30f6e48SScott Wood 		case INT_CLASS_CRIT:
438d30f6e48SScott Wood 			set_guest_csrr(vcpu, vcpu->arch.pc,
439d30f6e48SScott Wood 				       vcpu->arch.shared->msr);
440d30f6e48SScott Wood 			break;
441d30f6e48SScott Wood 		case INT_CLASS_DBG:
442d30f6e48SScott Wood 			set_guest_dsrr(vcpu, vcpu->arch.pc,
443d30f6e48SScott Wood 				       vcpu->arch.shared->msr);
444d30f6e48SScott Wood 			break;
445d30f6e48SScott Wood 		case INT_CLASS_MC:
446d30f6e48SScott Wood 			set_guest_mcsrr(vcpu, vcpu->arch.pc,
447d30f6e48SScott Wood 					vcpu->arch.shared->msr);
448d30f6e48SScott Wood 			break;
449d30f6e48SScott Wood 		}
450d30f6e48SScott Wood 
451d4cf3892SHollis Blanchard 		vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
452daf5e271SLiu Yu 		if (update_esr == true)
453d30f6e48SScott Wood 			set_guest_esr(vcpu, vcpu->arch.queued_esr);
454daf5e271SLiu Yu 		if (update_dear == true)
455d30f6e48SScott Wood 			set_guest_dear(vcpu, vcpu->arch.queued_dear);
4565df554adSScott Wood 		if (update_epr == true) {
4575df554adSScott Wood 			if (vcpu->arch.epr_flags & KVMPPC_EPR_USER)
4581c810636SAlexander Graf 				kvm_make_request(KVM_REQ_EPR_EXIT, vcpu);
459eb1e4f43SScott Wood 			else if (vcpu->arch.epr_flags & KVMPPC_EPR_KERNEL) {
460eb1e4f43SScott Wood 				BUG_ON(vcpu->arch.irq_type != KVMPPC_IRQ_MPIC);
461eb1e4f43SScott Wood 				kvmppc_mpic_set_epr(vcpu);
462eb1e4f43SScott Wood 			}
4635df554adSScott Wood 		}
46495e90b43SMihai Caraman 
46595e90b43SMihai Caraman 		new_msr &= msr_mask;
46695e90b43SMihai Caraman #if defined(CONFIG_64BIT)
46795e90b43SMihai Caraman 		if (vcpu->arch.epcr & SPRN_EPCR_ICM)
46895e90b43SMihai Caraman 			new_msr |= MSR_CM;
46995e90b43SMihai Caraman #endif
47095e90b43SMihai Caraman 		kvmppc_set_msr(vcpu, new_msr);
471d4cf3892SHollis Blanchard 
472c5335f17SAlexander Graf 		if (!keep_irq)
473d4cf3892SHollis Blanchard 			clear_bit(priority, &vcpu->arch.pending_exceptions);
474d4cf3892SHollis Blanchard 	}
475d4cf3892SHollis Blanchard 
476d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
477d30f6e48SScott Wood 	/*
478d30f6e48SScott Wood 	 * If an interrupt is pending but masked, raise a guest doorbell
479d30f6e48SScott Wood 	 * so that we are notified when the guest enables the relevant
480d30f6e48SScott Wood 	 * MSR bit.
481d30f6e48SScott Wood 	 */
482d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
483d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
484d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
485d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
486d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
487d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
488d30f6e48SScott Wood #endif
489d30f6e48SScott Wood 
490d4cf3892SHollis Blanchard 	return allowed;
491d9fbd03dSHollis Blanchard }
492d9fbd03dSHollis Blanchard 
493f61c94bbSBharat Bhushan /*
494f61c94bbSBharat Bhushan  * Return the number of jiffies until the next timeout.  If the timeout is
495f61c94bbSBharat Bhushan  * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA
496f61c94bbSBharat Bhushan  * because the larger value can break the timer APIs.
497f61c94bbSBharat Bhushan  */
498f61c94bbSBharat Bhushan static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu)
499f61c94bbSBharat Bhushan {
500f61c94bbSBharat Bhushan 	u64 tb, wdt_tb, wdt_ticks = 0;
501f61c94bbSBharat Bhushan 	u64 nr_jiffies = 0;
502f61c94bbSBharat Bhushan 	u32 period = TCR_GET_WP(vcpu->arch.tcr);
503f61c94bbSBharat Bhushan 
504f61c94bbSBharat Bhushan 	wdt_tb = 1ULL << (63 - period);
505f61c94bbSBharat Bhushan 	tb = get_tb();
506f61c94bbSBharat Bhushan 	/*
507f61c94bbSBharat Bhushan 	 * The watchdog timeout will hapeen when TB bit corresponding
508f61c94bbSBharat Bhushan 	 * to watchdog will toggle from 0 to 1.
509f61c94bbSBharat Bhushan 	 */
510f61c94bbSBharat Bhushan 	if (tb & wdt_tb)
511f61c94bbSBharat Bhushan 		wdt_ticks = wdt_tb;
512f61c94bbSBharat Bhushan 
513f61c94bbSBharat Bhushan 	wdt_ticks += wdt_tb - (tb & (wdt_tb - 1));
514f61c94bbSBharat Bhushan 
515f61c94bbSBharat Bhushan 	/* Convert timebase ticks to jiffies */
516f61c94bbSBharat Bhushan 	nr_jiffies = wdt_ticks;
517f61c94bbSBharat Bhushan 
518f61c94bbSBharat Bhushan 	if (do_div(nr_jiffies, tb_ticks_per_jiffy))
519f61c94bbSBharat Bhushan 		nr_jiffies++;
520f61c94bbSBharat Bhushan 
521f61c94bbSBharat Bhushan 	return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA);
522f61c94bbSBharat Bhushan }
523f61c94bbSBharat Bhushan 
524f61c94bbSBharat Bhushan static void arm_next_watchdog(struct kvm_vcpu *vcpu)
525f61c94bbSBharat Bhushan {
526f61c94bbSBharat Bhushan 	unsigned long nr_jiffies;
527f61c94bbSBharat Bhushan 	unsigned long flags;
528f61c94bbSBharat Bhushan 
529f61c94bbSBharat Bhushan 	/*
530f61c94bbSBharat Bhushan 	 * If TSR_ENW and TSR_WIS are not set then no need to exit to
531f61c94bbSBharat Bhushan 	 * userspace, so clear the KVM_REQ_WATCHDOG request.
532f61c94bbSBharat Bhushan 	 */
533f61c94bbSBharat Bhushan 	if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS))
534f61c94bbSBharat Bhushan 		clear_bit(KVM_REQ_WATCHDOG, &vcpu->requests);
535f61c94bbSBharat Bhushan 
536f61c94bbSBharat Bhushan 	spin_lock_irqsave(&vcpu->arch.wdt_lock, flags);
537f61c94bbSBharat Bhushan 	nr_jiffies = watchdog_next_timeout(vcpu);
538f61c94bbSBharat Bhushan 	/*
539f61c94bbSBharat Bhushan 	 * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA
540f61c94bbSBharat Bhushan 	 * then do not run the watchdog timer as this can break timer APIs.
541f61c94bbSBharat Bhushan 	 */
542f61c94bbSBharat Bhushan 	if (nr_jiffies < NEXT_TIMER_MAX_DELTA)
543f61c94bbSBharat Bhushan 		mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies);
544f61c94bbSBharat Bhushan 	else
545f61c94bbSBharat Bhushan 		del_timer(&vcpu->arch.wdt_timer);
546f61c94bbSBharat Bhushan 	spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags);
547f61c94bbSBharat Bhushan }
548f61c94bbSBharat Bhushan 
549f61c94bbSBharat Bhushan void kvmppc_watchdog_func(unsigned long data)
550f61c94bbSBharat Bhushan {
551f61c94bbSBharat Bhushan 	struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
552f61c94bbSBharat Bhushan 	u32 tsr, new_tsr;
553f61c94bbSBharat Bhushan 	int final;
554f61c94bbSBharat Bhushan 
555f61c94bbSBharat Bhushan 	do {
556f61c94bbSBharat Bhushan 		new_tsr = tsr = vcpu->arch.tsr;
557f61c94bbSBharat Bhushan 		final = 0;
558f61c94bbSBharat Bhushan 
559f61c94bbSBharat Bhushan 		/* Time out event */
560f61c94bbSBharat Bhushan 		if (tsr & TSR_ENW) {
561f61c94bbSBharat Bhushan 			if (tsr & TSR_WIS)
562f61c94bbSBharat Bhushan 				final = 1;
563f61c94bbSBharat Bhushan 			else
564f61c94bbSBharat Bhushan 				new_tsr = tsr | TSR_WIS;
565f61c94bbSBharat Bhushan 		} else {
566f61c94bbSBharat Bhushan 			new_tsr = tsr | TSR_ENW;
567f61c94bbSBharat Bhushan 		}
568f61c94bbSBharat Bhushan 	} while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr);
569f61c94bbSBharat Bhushan 
570f61c94bbSBharat Bhushan 	if (new_tsr & TSR_WIS) {
571f61c94bbSBharat Bhushan 		smp_wmb();
572f61c94bbSBharat Bhushan 		kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
573f61c94bbSBharat Bhushan 		kvm_vcpu_kick(vcpu);
574f61c94bbSBharat Bhushan 	}
575f61c94bbSBharat Bhushan 
576f61c94bbSBharat Bhushan 	/*
577f61c94bbSBharat Bhushan 	 * If this is final watchdog expiry and some action is required
578f61c94bbSBharat Bhushan 	 * then exit to userspace.
579f61c94bbSBharat Bhushan 	 */
580f61c94bbSBharat Bhushan 	if (final && (vcpu->arch.tcr & TCR_WRC_MASK) &&
581f61c94bbSBharat Bhushan 	    vcpu->arch.watchdog_enabled) {
582f61c94bbSBharat Bhushan 		smp_wmb();
583f61c94bbSBharat Bhushan 		kvm_make_request(KVM_REQ_WATCHDOG, vcpu);
584f61c94bbSBharat Bhushan 		kvm_vcpu_kick(vcpu);
585f61c94bbSBharat Bhushan 	}
586f61c94bbSBharat Bhushan 
587f61c94bbSBharat Bhushan 	/*
588f61c94bbSBharat Bhushan 	 * Stop running the watchdog timer after final expiration to
589f61c94bbSBharat Bhushan 	 * prevent the host from being flooded with timers if the
590f61c94bbSBharat Bhushan 	 * guest sets a short period.
591f61c94bbSBharat Bhushan 	 * Timers will resume when TSR/TCR is updated next time.
592f61c94bbSBharat Bhushan 	 */
593f61c94bbSBharat Bhushan 	if (!final)
594f61c94bbSBharat Bhushan 		arm_next_watchdog(vcpu);
595f61c94bbSBharat Bhushan }
596f61c94bbSBharat Bhushan 
597dfd4d47eSScott Wood static void update_timer_ints(struct kvm_vcpu *vcpu)
598dfd4d47eSScott Wood {
599dfd4d47eSScott Wood 	if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
600dfd4d47eSScott Wood 		kvmppc_core_queue_dec(vcpu);
601dfd4d47eSScott Wood 	else
602dfd4d47eSScott Wood 		kvmppc_core_dequeue_dec(vcpu);
603f61c94bbSBharat Bhushan 
604f61c94bbSBharat Bhushan 	if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS))
605f61c94bbSBharat Bhushan 		kvmppc_core_queue_watchdog(vcpu);
606f61c94bbSBharat Bhushan 	else
607f61c94bbSBharat Bhushan 		kvmppc_core_dequeue_watchdog(vcpu);
608dfd4d47eSScott Wood }
609dfd4d47eSScott Wood 
610c59a6a3eSScott Wood static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
611d9fbd03dSHollis Blanchard {
612d9fbd03dSHollis Blanchard 	unsigned long *pending = &vcpu->arch.pending_exceptions;
613d9fbd03dSHollis Blanchard 	unsigned int priority;
614d9fbd03dSHollis Blanchard 
6159ab80843SHollis Blanchard 	priority = __ffs(*pending);
6168b3a00fcSAlexander Graf 	while (priority < BOOKE_IRQPRIO_MAX) {
617d4cf3892SHollis Blanchard 		if (kvmppc_booke_irqprio_deliver(vcpu, priority))
618d9fbd03dSHollis Blanchard 			break;
619d9fbd03dSHollis Blanchard 
620d9fbd03dSHollis Blanchard 		priority = find_next_bit(pending,
621d9fbd03dSHollis Blanchard 		                         BITS_PER_BYTE * sizeof(*pending),
622d9fbd03dSHollis Blanchard 		                         priority + 1);
623d9fbd03dSHollis Blanchard 	}
62490bba358SAlexander Graf 
62590bba358SAlexander Graf 	/* Tell the guest about our interrupt status */
62629ac26efSScott Wood 	vcpu->arch.shared->int_pending = !!*pending;
627d9fbd03dSHollis Blanchard }
628d9fbd03dSHollis Blanchard 
629c59a6a3eSScott Wood /* Check pending exceptions and deliver one, if possible. */
630a8e4ef84SAlexander Graf int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
631c59a6a3eSScott Wood {
632a8e4ef84SAlexander Graf 	int r = 0;
633c59a6a3eSScott Wood 	WARN_ON_ONCE(!irqs_disabled());
634c59a6a3eSScott Wood 
635c59a6a3eSScott Wood 	kvmppc_core_check_exceptions(vcpu);
636c59a6a3eSScott Wood 
637b8c649a9SAlexander Graf 	if (vcpu->requests) {
638b8c649a9SAlexander Graf 		/* Exception delivery raised request; start over */
639b8c649a9SAlexander Graf 		return 1;
640b8c649a9SAlexander Graf 	}
641b8c649a9SAlexander Graf 
642c59a6a3eSScott Wood 	if (vcpu->arch.shared->msr & MSR_WE) {
643c59a6a3eSScott Wood 		local_irq_enable();
644c59a6a3eSScott Wood 		kvm_vcpu_block(vcpu);
645966cd0f3SAlexander Graf 		clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
646c59a6a3eSScott Wood 		local_irq_disable();
647c59a6a3eSScott Wood 
648c59a6a3eSScott Wood 		kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
649a8e4ef84SAlexander Graf 		r = 1;
650c59a6a3eSScott Wood 	};
651a8e4ef84SAlexander Graf 
652a8e4ef84SAlexander Graf 	return r;
653a8e4ef84SAlexander Graf }
654a8e4ef84SAlexander Graf 
6557c973a2eSAlexander Graf int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
6564ffc6356SAlexander Graf {
6577c973a2eSAlexander Graf 	int r = 1; /* Indicate we want to get back into the guest */
6587c973a2eSAlexander Graf 
6594ffc6356SAlexander Graf 	if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
6604ffc6356SAlexander Graf 		update_timer_ints(vcpu);
661862d31f7SAlexander Graf #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
662862d31f7SAlexander Graf 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
663862d31f7SAlexander Graf 		kvmppc_core_flush_tlb(vcpu);
664862d31f7SAlexander Graf #endif
6657c973a2eSAlexander Graf 
666f61c94bbSBharat Bhushan 	if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) {
667f61c94bbSBharat Bhushan 		vcpu->run->exit_reason = KVM_EXIT_WATCHDOG;
668f61c94bbSBharat Bhushan 		r = 0;
669f61c94bbSBharat Bhushan 	}
670f61c94bbSBharat Bhushan 
6711c810636SAlexander Graf 	if (kvm_check_request(KVM_REQ_EPR_EXIT, vcpu)) {
6721c810636SAlexander Graf 		vcpu->run->epr.epr = 0;
6731c810636SAlexander Graf 		vcpu->arch.epr_needed = true;
6741c810636SAlexander Graf 		vcpu->run->exit_reason = KVM_EXIT_EPR;
6751c810636SAlexander Graf 		r = 0;
6761c810636SAlexander Graf 	}
6771c810636SAlexander Graf 
6787c973a2eSAlexander Graf 	return r;
6794ffc6356SAlexander Graf }
6804ffc6356SAlexander Graf 
681df6909e5SPaul Mackerras int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
682df6909e5SPaul Mackerras {
6837ee78855SAlexander Graf 	int ret, s;
684ce11e48bSBharat Bhushan 	struct thread_struct thread;
6858fae845fSScott Wood #ifdef CONFIG_PPC_FPU
6868fae845fSScott Wood 	unsigned int fpscr;
6878fae845fSScott Wood 	int fpexc_mode;
6888fae845fSScott Wood 	u64 fpr[32];
6898fae845fSScott Wood #endif
690df6909e5SPaul Mackerras 
691af8f38b3SAlexander Graf 	if (!vcpu->arch.sane) {
692af8f38b3SAlexander Graf 		kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
693af8f38b3SAlexander Graf 		return -EINVAL;
694af8f38b3SAlexander Graf 	}
695af8f38b3SAlexander Graf 
696df6909e5SPaul Mackerras 	local_irq_disable();
6977ee78855SAlexander Graf 	s = kvmppc_prepare_to_enter(vcpu);
6987ee78855SAlexander Graf 	if (s <= 0) {
69924afa37bSAlexander Graf 		local_irq_enable();
7007ee78855SAlexander Graf 		ret = s;
7011d1ef222SScott Wood 		goto out;
7021d1ef222SScott Wood 	}
7031d1ef222SScott Wood 
7048fae845fSScott Wood #ifdef CONFIG_PPC_FPU
7058fae845fSScott Wood 	/* Save userspace FPU state in stack */
7068fae845fSScott Wood 	enable_kernel_fp();
7078fae845fSScott Wood 	memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
7088fae845fSScott Wood 	fpscr = current->thread.fpscr.val;
7098fae845fSScott Wood 	fpexc_mode = current->thread.fpexc_mode;
7108fae845fSScott Wood 
7118fae845fSScott Wood 	/* Restore guest FPU state to thread */
7128fae845fSScott Wood 	memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr));
7138fae845fSScott Wood 	current->thread.fpscr.val = vcpu->arch.fpscr;
7148fae845fSScott Wood 
7158fae845fSScott Wood 	/*
7168fae845fSScott Wood 	 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
7178fae845fSScott Wood 	 * as always using the FPU.  Kernel usage of FP (via
7188fae845fSScott Wood 	 * enable_kernel_fp()) in this thread must not occur while
7198fae845fSScott Wood 	 * vcpu->fpu_active is set.
7208fae845fSScott Wood 	 */
7218fae845fSScott Wood 	vcpu->fpu_active = 1;
7228fae845fSScott Wood 
7238fae845fSScott Wood 	kvmppc_load_guest_fp(vcpu);
7248fae845fSScott Wood #endif
7258fae845fSScott Wood 
726ce11e48bSBharat Bhushan 	/* Switch to guest debug context */
727ce11e48bSBharat Bhushan 	thread.debug = vcpu->arch.shadow_dbg_reg;
728ce11e48bSBharat Bhushan 	switch_booke_debug_regs(&thread);
729ce11e48bSBharat Bhushan 	thread.debug = current->thread.debug;
730ce11e48bSBharat Bhushan 	current->thread.debug = vcpu->arch.shadow_dbg_reg;
731ce11e48bSBharat Bhushan 
7325f1c248fSScott Wood 	kvmppc_fix_ee_before_entry();
733f8941fbeSScott Wood 
734df6909e5SPaul Mackerras 	ret = __kvmppc_vcpu_run(kvm_run, vcpu);
7358fae845fSScott Wood 
73624afa37bSAlexander Graf 	/* No need for kvm_guest_exit. It's done in handle_exit.
73724afa37bSAlexander Graf 	   We also get here with interrupts enabled. */
73824afa37bSAlexander Graf 
739ce11e48bSBharat Bhushan 	/* Switch back to user space debug context */
740ce11e48bSBharat Bhushan 	switch_booke_debug_regs(&thread);
741ce11e48bSBharat Bhushan 	current->thread.debug = thread.debug;
742ce11e48bSBharat Bhushan 
7438fae845fSScott Wood #ifdef CONFIG_PPC_FPU
7448fae845fSScott Wood 	kvmppc_save_guest_fp(vcpu);
7458fae845fSScott Wood 
7468fae845fSScott Wood 	vcpu->fpu_active = 0;
7478fae845fSScott Wood 
7488fae845fSScott Wood 	/* Save guest FPU state from thread */
7498fae845fSScott Wood 	memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr));
7508fae845fSScott Wood 	vcpu->arch.fpscr = current->thread.fpscr.val;
7518fae845fSScott Wood 
7528fae845fSScott Wood 	/* Restore userspace FPU state from stack */
7538fae845fSScott Wood 	memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
7548fae845fSScott Wood 	current->thread.fpscr.val = fpscr;
7558fae845fSScott Wood 	current->thread.fpexc_mode = fpexc_mode;
7568fae845fSScott Wood #endif
7578fae845fSScott Wood 
7581d1ef222SScott Wood out:
759d69c6436SAlexander Graf 	vcpu->mode = OUTSIDE_GUEST_MODE;
760df6909e5SPaul Mackerras 	return ret;
761df6909e5SPaul Mackerras }
762df6909e5SPaul Mackerras 
763d30f6e48SScott Wood static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
764d9fbd03dSHollis Blanchard {
765d9fbd03dSHollis Blanchard 	enum emulation_result er;
766d9fbd03dSHollis Blanchard 
767d9fbd03dSHollis Blanchard 	er = kvmppc_emulate_instruction(run, vcpu);
768d9fbd03dSHollis Blanchard 	switch (er) {
769d9fbd03dSHollis Blanchard 	case EMULATE_DONE:
77073e75b41SHollis Blanchard 		/* don't overwrite subtypes, just account kvm_stats */
7717b701591SHollis Blanchard 		kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
772d9fbd03dSHollis Blanchard 		/* Future optimization: only reload non-volatiles if
773d9fbd03dSHollis Blanchard 		 * they were actually modified by emulation. */
774d30f6e48SScott Wood 		return RESUME_GUEST_NV;
775d30f6e48SScott Wood 
776d9fbd03dSHollis Blanchard 	case EMULATE_DO_DCR:
777d9fbd03dSHollis Blanchard 		run->exit_reason = KVM_EXIT_DCR;
778d30f6e48SScott Wood 		return RESUME_HOST;
779d30f6e48SScott Wood 
780d9fbd03dSHollis Blanchard 	case EMULATE_FAIL:
7815cf8ca22SHollis Blanchard 		printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
782d9fbd03dSHollis Blanchard 		       __func__, vcpu->arch.pc, vcpu->arch.last_inst);
783d9fbd03dSHollis Blanchard 		/* For debugging, encode the failing instruction and
784d9fbd03dSHollis Blanchard 		 * report it to userspace. */
785d9fbd03dSHollis Blanchard 		run->hw.hardware_exit_reason = ~0ULL << 32;
786d9fbd03dSHollis Blanchard 		run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
787d1ff5499SAlexander Graf 		kvmppc_core_queue_program(vcpu, ESR_PIL);
788d30f6e48SScott Wood 		return RESUME_HOST;
789d30f6e48SScott Wood 
7909b4f5308SBharat Bhushan 	case EMULATE_EXIT_USER:
7919b4f5308SBharat Bhushan 		return RESUME_HOST;
7929b4f5308SBharat Bhushan 
793d9fbd03dSHollis Blanchard 	default:
794d9fbd03dSHollis Blanchard 		BUG();
795d9fbd03dSHollis Blanchard 	}
796d30f6e48SScott Wood }
797d30f6e48SScott Wood 
798ce11e48bSBharat Bhushan static int kvmppc_handle_debug(struct kvm_run *run, struct kvm_vcpu *vcpu)
799ce11e48bSBharat Bhushan {
800ce11e48bSBharat Bhushan 	struct debug_reg *dbg_reg = &(vcpu->arch.shadow_dbg_reg);
801ce11e48bSBharat Bhushan 	u32 dbsr = vcpu->arch.dbsr;
802ce11e48bSBharat Bhushan 
803ce11e48bSBharat Bhushan 	run->debug.arch.status = 0;
804ce11e48bSBharat Bhushan 	run->debug.arch.address = vcpu->arch.pc;
805ce11e48bSBharat Bhushan 
806ce11e48bSBharat Bhushan 	if (dbsr & (DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4)) {
807ce11e48bSBharat Bhushan 		run->debug.arch.status |= KVMPPC_DEBUG_BREAKPOINT;
808ce11e48bSBharat Bhushan 	} else {
809ce11e48bSBharat Bhushan 		if (dbsr & (DBSR_DAC1W | DBSR_DAC2W))
810ce11e48bSBharat Bhushan 			run->debug.arch.status |= KVMPPC_DEBUG_WATCH_WRITE;
811ce11e48bSBharat Bhushan 		else if (dbsr & (DBSR_DAC1R | DBSR_DAC2R))
812ce11e48bSBharat Bhushan 			run->debug.arch.status |= KVMPPC_DEBUG_WATCH_READ;
813ce11e48bSBharat Bhushan 		if (dbsr & (DBSR_DAC1R | DBSR_DAC1W))
814ce11e48bSBharat Bhushan 			run->debug.arch.address = dbg_reg->dac1;
815ce11e48bSBharat Bhushan 		else if (dbsr & (DBSR_DAC2R | DBSR_DAC2W))
816ce11e48bSBharat Bhushan 			run->debug.arch.address = dbg_reg->dac2;
817ce11e48bSBharat Bhushan 	}
818ce11e48bSBharat Bhushan 
819ce11e48bSBharat Bhushan 	return RESUME_HOST;
820ce11e48bSBharat Bhushan }
821ce11e48bSBharat Bhushan 
8224e642ccbSAlexander Graf static void kvmppc_fill_pt_regs(struct pt_regs *regs)
8234e642ccbSAlexander Graf {
8244e642ccbSAlexander Graf 	ulong r1, ip, msr, lr;
8254e642ccbSAlexander Graf 
8264e642ccbSAlexander Graf 	asm("mr %0, 1" : "=r"(r1));
8274e642ccbSAlexander Graf 	asm("mflr %0" : "=r"(lr));
8284e642ccbSAlexander Graf 	asm("mfmsr %0" : "=r"(msr));
8294e642ccbSAlexander Graf 	asm("bl 1f; 1: mflr %0" : "=r"(ip));
8304e642ccbSAlexander Graf 
8314e642ccbSAlexander Graf 	memset(regs, 0, sizeof(*regs));
8324e642ccbSAlexander Graf 	regs->gpr[1] = r1;
8334e642ccbSAlexander Graf 	regs->nip = ip;
8344e642ccbSAlexander Graf 	regs->msr = msr;
8354e642ccbSAlexander Graf 	regs->link = lr;
8364e642ccbSAlexander Graf }
8374e642ccbSAlexander Graf 
8386328e593SBharat Bhushan /*
8396328e593SBharat Bhushan  * For interrupts needed to be handled by host interrupt handlers,
8406328e593SBharat Bhushan  * corresponding host handler are called from here in similar way
8416328e593SBharat Bhushan  * (but not exact) as they are called from low level handler
8426328e593SBharat Bhushan  * (such as from arch/powerpc/kernel/head_fsl_booke.S).
8436328e593SBharat Bhushan  */
8444e642ccbSAlexander Graf static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
8454e642ccbSAlexander Graf 				     unsigned int exit_nr)
8464e642ccbSAlexander Graf {
8474e642ccbSAlexander Graf 	struct pt_regs regs;
8484e642ccbSAlexander Graf 
8494e642ccbSAlexander Graf 	switch (exit_nr) {
8504e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_EXTERNAL:
8514e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
8524e642ccbSAlexander Graf 		do_IRQ(&regs);
8534e642ccbSAlexander Graf 		break;
8544e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_DECREMENTER:
8554e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
8564e642ccbSAlexander Graf 		timer_interrupt(&regs);
8574e642ccbSAlexander Graf 		break;
8585f17ce8bSTiejun Chen #if defined(CONFIG_PPC_DOORBELL)
8594e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_DOORBELL:
8604e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
8614e642ccbSAlexander Graf 		doorbell_exception(&regs);
8624e642ccbSAlexander Graf 		break;
8634e642ccbSAlexander Graf #endif
8644e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_MACHINE_CHECK:
8654e642ccbSAlexander Graf 		/* FIXME */
8664e642ccbSAlexander Graf 		break;
8677cc1e8eeSAlexander Graf 	case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
8687cc1e8eeSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
8697cc1e8eeSAlexander Graf 		performance_monitor_exception(&regs);
8707cc1e8eeSAlexander Graf 		break;
8716328e593SBharat Bhushan 	case BOOKE_INTERRUPT_WATCHDOG:
8726328e593SBharat Bhushan 		kvmppc_fill_pt_regs(&regs);
8736328e593SBharat Bhushan #ifdef CONFIG_BOOKE_WDT
8746328e593SBharat Bhushan 		WatchdogException(&regs);
8756328e593SBharat Bhushan #else
8766328e593SBharat Bhushan 		unknown_exception(&regs);
8776328e593SBharat Bhushan #endif
8786328e593SBharat Bhushan 		break;
8796328e593SBharat Bhushan 	case BOOKE_INTERRUPT_CRITICAL:
8806328e593SBharat Bhushan 		unknown_exception(&regs);
8816328e593SBharat Bhushan 		break;
882ce11e48bSBharat Bhushan 	case BOOKE_INTERRUPT_DEBUG:
883ce11e48bSBharat Bhushan 		/* Save DBSR before preemption is enabled */
884ce11e48bSBharat Bhushan 		vcpu->arch.dbsr = mfspr(SPRN_DBSR);
885ce11e48bSBharat Bhushan 		kvmppc_clear_dbsr();
886ce11e48bSBharat Bhushan 		break;
8874e642ccbSAlexander Graf 	}
8884e642ccbSAlexander Graf }
8894e642ccbSAlexander Graf 
890d30f6e48SScott Wood /**
891d30f6e48SScott Wood  * kvmppc_handle_exit
892d30f6e48SScott Wood  *
893d30f6e48SScott Wood  * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
894d30f6e48SScott Wood  */
895d30f6e48SScott Wood int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
896d30f6e48SScott Wood                        unsigned int exit_nr)
897d30f6e48SScott Wood {
898d30f6e48SScott Wood 	int r = RESUME_HOST;
8997ee78855SAlexander Graf 	int s;
900f1e89028SScott Wood 	int idx;
901d30f6e48SScott Wood 
9027c11c0ccSScott Wood #ifdef CONFIG_PPC64
9037c11c0ccSScott Wood 	WARN_ON(local_paca->irq_happened != 0);
9047c11c0ccSScott Wood #endif
9057c11c0ccSScott Wood 
9067c11c0ccSScott Wood 	/*
9077c11c0ccSScott Wood 	 * We enter with interrupts disabled in hardware, but
9087c11c0ccSScott Wood 	 * we need to call hard_irq_disable anyway to ensure that
9097c11c0ccSScott Wood 	 * the software state is kept in sync.
9107c11c0ccSScott Wood 	 */
9117c11c0ccSScott Wood 	hard_irq_disable();
9127c11c0ccSScott Wood 
913d30f6e48SScott Wood 	/* update before a new last_exit_type is rewritten */
914d30f6e48SScott Wood 	kvmppc_update_timing_stats(vcpu);
915d30f6e48SScott Wood 
9164e642ccbSAlexander Graf 	/* restart interrupts if they were meant for the host */
9174e642ccbSAlexander Graf 	kvmppc_restart_interrupt(vcpu, exit_nr);
918d30f6e48SScott Wood 
919d30f6e48SScott Wood 	local_irq_enable();
920d30f6e48SScott Wood 
92197c95059SAlexander Graf 	trace_kvm_exit(exit_nr, vcpu);
922706fb730SAlexander Graf 	kvm_guest_exit();
92397c95059SAlexander Graf 
924d30f6e48SScott Wood 	run->exit_reason = KVM_EXIT_UNKNOWN;
925d30f6e48SScott Wood 	run->ready_for_interrupt_injection = 1;
926d30f6e48SScott Wood 
927d30f6e48SScott Wood 	switch (exit_nr) {
928d30f6e48SScott Wood 	case BOOKE_INTERRUPT_MACHINE_CHECK:
929c35c9d84SAlexander Graf 		printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
930c35c9d84SAlexander Graf 		kvmppc_dump_vcpu(vcpu);
931c35c9d84SAlexander Graf 		/* For debugging, send invalid exit reason to user space */
932c35c9d84SAlexander Graf 		run->hw.hardware_exit_reason = ~1ULL << 32;
933c35c9d84SAlexander Graf 		run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
934c35c9d84SAlexander Graf 		r = RESUME_HOST;
935d30f6e48SScott Wood 		break;
936d30f6e48SScott Wood 
937d30f6e48SScott Wood 	case BOOKE_INTERRUPT_EXTERNAL:
938d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
939d30f6e48SScott Wood 		r = RESUME_GUEST;
940d30f6e48SScott Wood 		break;
941d30f6e48SScott Wood 
942d30f6e48SScott Wood 	case BOOKE_INTERRUPT_DECREMENTER:
943d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, DEC_EXITS);
944d30f6e48SScott Wood 		r = RESUME_GUEST;
945d30f6e48SScott Wood 		break;
946d30f6e48SScott Wood 
9476328e593SBharat Bhushan 	case BOOKE_INTERRUPT_WATCHDOG:
9486328e593SBharat Bhushan 		r = RESUME_GUEST;
9496328e593SBharat Bhushan 		break;
9506328e593SBharat Bhushan 
951d30f6e48SScott Wood 	case BOOKE_INTERRUPT_DOORBELL:
952d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, DBELL_EXITS);
953d30f6e48SScott Wood 		r = RESUME_GUEST;
954d30f6e48SScott Wood 		break;
955d30f6e48SScott Wood 
956d30f6e48SScott Wood 	case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
957d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, GDBELL_EXITS);
958d30f6e48SScott Wood 
959d30f6e48SScott Wood 		/*
960d30f6e48SScott Wood 		 * We are here because there is a pending guest interrupt
961d30f6e48SScott Wood 		 * which could not be delivered as MSR_CE or MSR_ME was not
962d30f6e48SScott Wood 		 * set.  Once we break from here we will retry delivery.
963d30f6e48SScott Wood 		 */
964d30f6e48SScott Wood 		r = RESUME_GUEST;
965d30f6e48SScott Wood 		break;
966d30f6e48SScott Wood 
967d30f6e48SScott Wood 	case BOOKE_INTERRUPT_GUEST_DBELL:
968d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, GDBELL_EXITS);
969d30f6e48SScott Wood 
970d30f6e48SScott Wood 		/*
971d30f6e48SScott Wood 		 * We are here because there is a pending guest interrupt
972d30f6e48SScott Wood 		 * which could not be delivered as MSR_EE was not set.  Once
973d30f6e48SScott Wood 		 * we break from here we will retry delivery.
974d30f6e48SScott Wood 		 */
975d30f6e48SScott Wood 		r = RESUME_GUEST;
976d30f6e48SScott Wood 		break;
977d30f6e48SScott Wood 
97895f2e921SAlexander Graf 	case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
97995f2e921SAlexander Graf 		r = RESUME_GUEST;
98095f2e921SAlexander Graf 		break;
98195f2e921SAlexander Graf 
982d30f6e48SScott Wood 	case BOOKE_INTERRUPT_HV_PRIV:
983d30f6e48SScott Wood 		r = emulation_exit(run, vcpu);
984d30f6e48SScott Wood 		break;
985d30f6e48SScott Wood 
986d30f6e48SScott Wood 	case BOOKE_INTERRUPT_PROGRAM:
987d30f6e48SScott Wood 		if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
9880268597cSAlexander Graf 			/*
9890268597cSAlexander Graf 			 * Program traps generated by user-level software must
9900268597cSAlexander Graf 			 * be handled by the guest kernel.
9910268597cSAlexander Graf 			 *
9920268597cSAlexander Graf 			 * In GS mode, hypervisor privileged instructions trap
9930268597cSAlexander Graf 			 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
9940268597cSAlexander Graf 			 * actual program interrupts, handled by the guest.
9950268597cSAlexander Graf 			 */
996d30f6e48SScott Wood 			kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
997d30f6e48SScott Wood 			r = RESUME_GUEST;
998d30f6e48SScott Wood 			kvmppc_account_exit(vcpu, USR_PR_INST);
999d30f6e48SScott Wood 			break;
1000d30f6e48SScott Wood 		}
1001d30f6e48SScott Wood 
1002d30f6e48SScott Wood 		r = emulation_exit(run, vcpu);
1003d9fbd03dSHollis Blanchard 		break;
1004d9fbd03dSHollis Blanchard 
1005d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_FP_UNAVAIL:
1006d4cf3892SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
10077b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, FP_UNAVAIL);
1008d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
1009d9fbd03dSHollis Blanchard 		break;
1010d9fbd03dSHollis Blanchard 
10114cd35f67SScott Wood #ifdef CONFIG_SPE
10124cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_UNAVAIL: {
10134cd35f67SScott Wood 		if (vcpu->arch.shared->msr & MSR_SPE)
10144cd35f67SScott Wood 			kvmppc_vcpu_enable_spe(vcpu);
10154cd35f67SScott Wood 		else
10164cd35f67SScott Wood 			kvmppc_booke_queue_irqprio(vcpu,
10174cd35f67SScott Wood 						   BOOKE_IRQPRIO_SPE_UNAVAIL);
1018bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
1019bb3a8a17SHollis Blanchard 		break;
10204cd35f67SScott Wood 	}
1021bb3a8a17SHollis Blanchard 
1022bb3a8a17SHollis Blanchard 	case BOOKE_INTERRUPT_SPE_FP_DATA:
1023bb3a8a17SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
1024bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
1025bb3a8a17SHollis Blanchard 		break;
1026bb3a8a17SHollis Blanchard 
1027bb3a8a17SHollis Blanchard 	case BOOKE_INTERRUPT_SPE_FP_ROUND:
1028bb3a8a17SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
1029bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
1030bb3a8a17SHollis Blanchard 		break;
10314cd35f67SScott Wood #else
10324cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_UNAVAIL:
10334cd35f67SScott Wood 		/*
10344cd35f67SScott Wood 		 * Guest wants SPE, but host kernel doesn't support it.  Send
10354cd35f67SScott Wood 		 * an "unimplemented operation" program check to the guest.
10364cd35f67SScott Wood 		 */
10374cd35f67SScott Wood 		kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
10384cd35f67SScott Wood 		r = RESUME_GUEST;
10394cd35f67SScott Wood 		break;
10404cd35f67SScott Wood 
10414cd35f67SScott Wood 	/*
10424cd35f67SScott Wood 	 * These really should never happen without CONFIG_SPE,
10434cd35f67SScott Wood 	 * as we should never enable the real MSR[SPE] in the guest.
10444cd35f67SScott Wood 	 */
10454cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_FP_DATA:
10464cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_FP_ROUND:
10474cd35f67SScott Wood 		printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
10484cd35f67SScott Wood 		       __func__, exit_nr, vcpu->arch.pc);
10494cd35f67SScott Wood 		run->hw.hardware_exit_reason = exit_nr;
10504cd35f67SScott Wood 		r = RESUME_HOST;
10514cd35f67SScott Wood 		break;
10524cd35f67SScott Wood #endif
1053bb3a8a17SHollis Blanchard 
1054d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DATA_STORAGE:
1055daf5e271SLiu Yu 		kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
1056daf5e271SLiu Yu 		                               vcpu->arch.fault_esr);
10577b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, DSI_EXITS);
1058d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
1059d9fbd03dSHollis Blanchard 		break;
1060d9fbd03dSHollis Blanchard 
1061d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_INST_STORAGE:
1062daf5e271SLiu Yu 		kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
10637b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, ISI_EXITS);
1064d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
1065d9fbd03dSHollis Blanchard 		break;
1066d9fbd03dSHollis Blanchard 
1067011da899SAlexander Graf 	case BOOKE_INTERRUPT_ALIGNMENT:
1068011da899SAlexander Graf 		kvmppc_core_queue_alignment(vcpu, vcpu->arch.fault_dear,
1069011da899SAlexander Graf 		                            vcpu->arch.fault_esr);
1070011da899SAlexander Graf 		r = RESUME_GUEST;
1071011da899SAlexander Graf 		break;
1072011da899SAlexander Graf 
1073d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
1074d30f6e48SScott Wood 	case BOOKE_INTERRUPT_HV_SYSCALL:
1075d30f6e48SScott Wood 		if (!(vcpu->arch.shared->msr & MSR_PR)) {
1076d30f6e48SScott Wood 			kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1077d30f6e48SScott Wood 		} else {
1078d30f6e48SScott Wood 			/*
1079d30f6e48SScott Wood 			 * hcall from guest userspace -- send privileged
1080d30f6e48SScott Wood 			 * instruction program check.
1081d30f6e48SScott Wood 			 */
1082d30f6e48SScott Wood 			kvmppc_core_queue_program(vcpu, ESR_PPR);
1083d30f6e48SScott Wood 		}
1084d30f6e48SScott Wood 
1085d30f6e48SScott Wood 		r = RESUME_GUEST;
1086d30f6e48SScott Wood 		break;
1087d30f6e48SScott Wood #else
1088d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_SYSCALL:
10892a342ed5SAlexander Graf 		if (!(vcpu->arch.shared->msr & MSR_PR) &&
10902a342ed5SAlexander Graf 		    (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
10912a342ed5SAlexander Graf 			/* KVM PV hypercalls */
10922a342ed5SAlexander Graf 			kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
10932a342ed5SAlexander Graf 			r = RESUME_GUEST;
10942a342ed5SAlexander Graf 		} else {
10952a342ed5SAlexander Graf 			/* Guest syscalls */
1096d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
10972a342ed5SAlexander Graf 		}
10987b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, SYSCALL_EXITS);
1099d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
1100d9fbd03dSHollis Blanchard 		break;
1101d30f6e48SScott Wood #endif
1102d9fbd03dSHollis Blanchard 
1103d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DTLB_MISS: {
1104d9fbd03dSHollis Blanchard 		unsigned long eaddr = vcpu->arch.fault_dear;
11057924bd41SHollis Blanchard 		int gtlb_index;
1106475e7cddSHollis Blanchard 		gpa_t gpaddr;
1107d9fbd03dSHollis Blanchard 		gfn_t gfn;
1108d9fbd03dSHollis Blanchard 
1109bf7ca4bdSAlexander Graf #ifdef CONFIG_KVM_E500V2
1110a4cd8b23SScott Wood 		if (!(vcpu->arch.shared->msr & MSR_PR) &&
1111a4cd8b23SScott Wood 		    (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
1112a4cd8b23SScott Wood 			kvmppc_map_magic(vcpu);
1113a4cd8b23SScott Wood 			kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
1114a4cd8b23SScott Wood 			r = RESUME_GUEST;
1115a4cd8b23SScott Wood 
1116a4cd8b23SScott Wood 			break;
1117a4cd8b23SScott Wood 		}
1118a4cd8b23SScott Wood #endif
1119a4cd8b23SScott Wood 
1120d9fbd03dSHollis Blanchard 		/* Check the guest TLB. */
1121fa86b8ddSHollis Blanchard 		gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
11227924bd41SHollis Blanchard 		if (gtlb_index < 0) {
1123d9fbd03dSHollis Blanchard 			/* The guest didn't have a mapping for it. */
1124daf5e271SLiu Yu 			kvmppc_core_queue_dtlb_miss(vcpu,
1125daf5e271SLiu Yu 			                            vcpu->arch.fault_dear,
1126daf5e271SLiu Yu 			                            vcpu->arch.fault_esr);
1127b52a638cSHollis Blanchard 			kvmppc_mmu_dtlb_miss(vcpu);
11287b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
1129d9fbd03dSHollis Blanchard 			r = RESUME_GUEST;
1130d9fbd03dSHollis Blanchard 			break;
1131d9fbd03dSHollis Blanchard 		}
1132d9fbd03dSHollis Blanchard 
1133f1e89028SScott Wood 		idx = srcu_read_lock(&vcpu->kvm->srcu);
1134f1e89028SScott Wood 
1135be8d1caeSHollis Blanchard 		gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
1136475e7cddSHollis Blanchard 		gfn = gpaddr >> PAGE_SHIFT;
1137d9fbd03dSHollis Blanchard 
1138d9fbd03dSHollis Blanchard 		if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1139d9fbd03dSHollis Blanchard 			/* The guest TLB had a mapping, but the shadow TLB
1140d9fbd03dSHollis Blanchard 			 * didn't, and it is RAM. This could be because:
1141d9fbd03dSHollis Blanchard 			 * a) the entry is mapping the host kernel, or
1142d9fbd03dSHollis Blanchard 			 * b) the guest used a large mapping which we're faking
1143d9fbd03dSHollis Blanchard 			 * Either way, we need to satisfy the fault without
1144d9fbd03dSHollis Blanchard 			 * invoking the guest. */
114558a96214SHollis Blanchard 			kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
11467b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
1147d9fbd03dSHollis Blanchard 			r = RESUME_GUEST;
1148d9fbd03dSHollis Blanchard 		} else {
1149d9fbd03dSHollis Blanchard 			/* Guest has mapped and accessed a page which is not
1150d9fbd03dSHollis Blanchard 			 * actually RAM. */
1151475e7cddSHollis Blanchard 			vcpu->arch.paddr_accessed = gpaddr;
11526020c0f6SAlexander Graf 			vcpu->arch.vaddr_accessed = eaddr;
1153d9fbd03dSHollis Blanchard 			r = kvmppc_emulate_mmio(run, vcpu);
11547b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, MMIO_EXITS);
1155d9fbd03dSHollis Blanchard 		}
1156d9fbd03dSHollis Blanchard 
1157f1e89028SScott Wood 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
1158d9fbd03dSHollis Blanchard 		break;
1159d9fbd03dSHollis Blanchard 	}
1160d9fbd03dSHollis Blanchard 
1161d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_ITLB_MISS: {
1162d9fbd03dSHollis Blanchard 		unsigned long eaddr = vcpu->arch.pc;
116389168618SHollis Blanchard 		gpa_t gpaddr;
1164d9fbd03dSHollis Blanchard 		gfn_t gfn;
11657924bd41SHollis Blanchard 		int gtlb_index;
1166d9fbd03dSHollis Blanchard 
1167d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
1168d9fbd03dSHollis Blanchard 
1169d9fbd03dSHollis Blanchard 		/* Check the guest TLB. */
1170fa86b8ddSHollis Blanchard 		gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
11717924bd41SHollis Blanchard 		if (gtlb_index < 0) {
1172d9fbd03dSHollis Blanchard 			/* The guest didn't have a mapping for it. */
1173d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
1174b52a638cSHollis Blanchard 			kvmppc_mmu_itlb_miss(vcpu);
11757b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
1176d9fbd03dSHollis Blanchard 			break;
1177d9fbd03dSHollis Blanchard 		}
1178d9fbd03dSHollis Blanchard 
11797b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
1180d9fbd03dSHollis Blanchard 
1181f1e89028SScott Wood 		idx = srcu_read_lock(&vcpu->kvm->srcu);
1182f1e89028SScott Wood 
1183be8d1caeSHollis Blanchard 		gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
118489168618SHollis Blanchard 		gfn = gpaddr >> PAGE_SHIFT;
1185d9fbd03dSHollis Blanchard 
1186d9fbd03dSHollis Blanchard 		if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1187d9fbd03dSHollis Blanchard 			/* The guest TLB had a mapping, but the shadow TLB
1188d9fbd03dSHollis Blanchard 			 * didn't. This could be because:
1189d9fbd03dSHollis Blanchard 			 * a) the entry is mapping the host kernel, or
1190d9fbd03dSHollis Blanchard 			 * b) the guest used a large mapping which we're faking
1191d9fbd03dSHollis Blanchard 			 * Either way, we need to satisfy the fault without
1192d9fbd03dSHollis Blanchard 			 * invoking the guest. */
119358a96214SHollis Blanchard 			kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
1194d9fbd03dSHollis Blanchard 		} else {
1195d9fbd03dSHollis Blanchard 			/* Guest mapped and leaped at non-RAM! */
1196d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
1197d9fbd03dSHollis Blanchard 		}
1198d9fbd03dSHollis Blanchard 
1199f1e89028SScott Wood 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
1200d9fbd03dSHollis Blanchard 		break;
1201d9fbd03dSHollis Blanchard 	}
1202d9fbd03dSHollis Blanchard 
1203d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DEBUG: {
1204ce11e48bSBharat Bhushan 		r = kvmppc_handle_debug(run, vcpu);
1205ce11e48bSBharat Bhushan 		if (r == RESUME_HOST)
1206d9fbd03dSHollis Blanchard 			run->exit_reason = KVM_EXIT_DEBUG;
12077b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, DEBUG_EXITS);
1208d9fbd03dSHollis Blanchard 		break;
1209d9fbd03dSHollis Blanchard 	}
1210d9fbd03dSHollis Blanchard 
1211d9fbd03dSHollis Blanchard 	default:
1212d9fbd03dSHollis Blanchard 		printk(KERN_EMERG "exit_nr %d\n", exit_nr);
1213d9fbd03dSHollis Blanchard 		BUG();
1214d9fbd03dSHollis Blanchard 	}
1215d9fbd03dSHollis Blanchard 
1216a8e4ef84SAlexander Graf 	/*
1217a8e4ef84SAlexander Graf 	 * To avoid clobbering exit_reason, only check for signals if we
1218a8e4ef84SAlexander Graf 	 * aren't already exiting to userspace for some other reason.
1219a8e4ef84SAlexander Graf 	 */
122003660ba2SAlexander Graf 	if (!(r & RESUME_HOST)) {
1221d9fbd03dSHollis Blanchard 		local_irq_disable();
12227ee78855SAlexander Graf 		s = kvmppc_prepare_to_enter(vcpu);
12237ee78855SAlexander Graf 		if (s <= 0) {
122424afa37bSAlexander Graf 			local_irq_enable();
12257ee78855SAlexander Graf 			r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
122624afa37bSAlexander Graf 		} else {
12275f1c248fSScott Wood 			kvmppc_fix_ee_before_entry();
122824afa37bSAlexander Graf 		}
122924afa37bSAlexander Graf 	}
1230706fb730SAlexander Graf 
1231d9fbd03dSHollis Blanchard 	return r;
1232d9fbd03dSHollis Blanchard }
1233d9fbd03dSHollis Blanchard 
1234d26f22c9SBharat Bhushan static void kvmppc_set_tsr(struct kvm_vcpu *vcpu, u32 new_tsr)
1235d26f22c9SBharat Bhushan {
1236d26f22c9SBharat Bhushan 	u32 old_tsr = vcpu->arch.tsr;
1237d26f22c9SBharat Bhushan 
1238d26f22c9SBharat Bhushan 	vcpu->arch.tsr = new_tsr;
1239d26f22c9SBharat Bhushan 
1240d26f22c9SBharat Bhushan 	if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS))
1241d26f22c9SBharat Bhushan 		arm_next_watchdog(vcpu);
1242d26f22c9SBharat Bhushan 
1243d26f22c9SBharat Bhushan 	update_timer_ints(vcpu);
1244d26f22c9SBharat Bhushan }
1245d26f22c9SBharat Bhushan 
1246d9fbd03dSHollis Blanchard /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
1247d9fbd03dSHollis Blanchard int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1248d9fbd03dSHollis Blanchard {
1249082decf2SHollis Blanchard 	int i;
1250af8f38b3SAlexander Graf 	int r;
1251082decf2SHollis Blanchard 
1252d9fbd03dSHollis Blanchard 	vcpu->arch.pc = 0;
1253b5904972SScott Wood 	vcpu->arch.shared->pir = vcpu->vcpu_id;
12548e5b26b5SAlexander Graf 	kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
1255d30f6e48SScott Wood 	kvmppc_set_msr(vcpu, 0);
1256d9fbd03dSHollis Blanchard 
1257d30f6e48SScott Wood #ifndef CONFIG_KVM_BOOKE_HV
1258ce11e48bSBharat Bhushan 	vcpu->arch.shadow_msr = MSR_USER | MSR_IS | MSR_DS;
1259d9fbd03dSHollis Blanchard 	vcpu->arch.shadow_pid = 1;
1260d30f6e48SScott Wood 	vcpu->arch.shared->msr = 0;
1261d30f6e48SScott Wood #endif
1262d9fbd03dSHollis Blanchard 
1263082decf2SHollis Blanchard 	/* Eye-catching numbers so we know if the guest takes an interrupt
1264082decf2SHollis Blanchard 	 * before it's programmed its own IVPR/IVORs. */
1265d9fbd03dSHollis Blanchard 	vcpu->arch.ivpr = 0x55550000;
1266082decf2SHollis Blanchard 	for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
1267082decf2SHollis Blanchard 		vcpu->arch.ivor[i] = 0x7700 | i * 4;
1268d9fbd03dSHollis Blanchard 
126973e75b41SHollis Blanchard 	kvmppc_init_timing_stats(vcpu);
127073e75b41SHollis Blanchard 
1271af8f38b3SAlexander Graf 	r = kvmppc_core_vcpu_setup(vcpu);
1272af8f38b3SAlexander Graf 	kvmppc_sanity_check(vcpu);
1273af8f38b3SAlexander Graf 	return r;
1274d9fbd03dSHollis Blanchard }
1275d9fbd03dSHollis Blanchard 
1276f61c94bbSBharat Bhushan int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
1277f61c94bbSBharat Bhushan {
1278f61c94bbSBharat Bhushan 	/* setup watchdog timer once */
1279f61c94bbSBharat Bhushan 	spin_lock_init(&vcpu->arch.wdt_lock);
1280f61c94bbSBharat Bhushan 	setup_timer(&vcpu->arch.wdt_timer, kvmppc_watchdog_func,
1281f61c94bbSBharat Bhushan 		    (unsigned long)vcpu);
1282f61c94bbSBharat Bhushan 
1283f61c94bbSBharat Bhushan 	return 0;
1284f61c94bbSBharat Bhushan }
1285f61c94bbSBharat Bhushan 
1286f61c94bbSBharat Bhushan void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
1287f61c94bbSBharat Bhushan {
1288f61c94bbSBharat Bhushan 	del_timer_sync(&vcpu->arch.wdt_timer);
1289f61c94bbSBharat Bhushan }
1290f61c94bbSBharat Bhushan 
1291d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1292d9fbd03dSHollis Blanchard {
1293d9fbd03dSHollis Blanchard 	int i;
1294d9fbd03dSHollis Blanchard 
1295d9fbd03dSHollis Blanchard 	regs->pc = vcpu->arch.pc;
1296992b5b29SAlexander Graf 	regs->cr = kvmppc_get_cr(vcpu);
1297d9fbd03dSHollis Blanchard 	regs->ctr = vcpu->arch.ctr;
1298d9fbd03dSHollis Blanchard 	regs->lr = vcpu->arch.lr;
1299992b5b29SAlexander Graf 	regs->xer = kvmppc_get_xer(vcpu);
1300666e7252SAlexander Graf 	regs->msr = vcpu->arch.shared->msr;
1301de7906c3SAlexander Graf 	regs->srr0 = vcpu->arch.shared->srr0;
1302de7906c3SAlexander Graf 	regs->srr1 = vcpu->arch.shared->srr1;
1303d9fbd03dSHollis Blanchard 	regs->pid = vcpu->arch.pid;
1304a73a9599SAlexander Graf 	regs->sprg0 = vcpu->arch.shared->sprg0;
1305a73a9599SAlexander Graf 	regs->sprg1 = vcpu->arch.shared->sprg1;
1306a73a9599SAlexander Graf 	regs->sprg2 = vcpu->arch.shared->sprg2;
1307a73a9599SAlexander Graf 	regs->sprg3 = vcpu->arch.shared->sprg3;
1308b5904972SScott Wood 	regs->sprg4 = vcpu->arch.shared->sprg4;
1309b5904972SScott Wood 	regs->sprg5 = vcpu->arch.shared->sprg5;
1310b5904972SScott Wood 	regs->sprg6 = vcpu->arch.shared->sprg6;
1311b5904972SScott Wood 	regs->sprg7 = vcpu->arch.shared->sprg7;
1312d9fbd03dSHollis Blanchard 
1313d9fbd03dSHollis Blanchard 	for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
13148e5b26b5SAlexander Graf 		regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
1315d9fbd03dSHollis Blanchard 
1316d9fbd03dSHollis Blanchard 	return 0;
1317d9fbd03dSHollis Blanchard }
1318d9fbd03dSHollis Blanchard 
1319d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1320d9fbd03dSHollis Blanchard {
1321d9fbd03dSHollis Blanchard 	int i;
1322d9fbd03dSHollis Blanchard 
1323d9fbd03dSHollis Blanchard 	vcpu->arch.pc = regs->pc;
1324992b5b29SAlexander Graf 	kvmppc_set_cr(vcpu, regs->cr);
1325d9fbd03dSHollis Blanchard 	vcpu->arch.ctr = regs->ctr;
1326d9fbd03dSHollis Blanchard 	vcpu->arch.lr = regs->lr;
1327992b5b29SAlexander Graf 	kvmppc_set_xer(vcpu, regs->xer);
1328b8fd68acSHollis Blanchard 	kvmppc_set_msr(vcpu, regs->msr);
1329de7906c3SAlexander Graf 	vcpu->arch.shared->srr0 = regs->srr0;
1330de7906c3SAlexander Graf 	vcpu->arch.shared->srr1 = regs->srr1;
13315ce941eeSScott Wood 	kvmppc_set_pid(vcpu, regs->pid);
1332a73a9599SAlexander Graf 	vcpu->arch.shared->sprg0 = regs->sprg0;
1333a73a9599SAlexander Graf 	vcpu->arch.shared->sprg1 = regs->sprg1;
1334a73a9599SAlexander Graf 	vcpu->arch.shared->sprg2 = regs->sprg2;
1335a73a9599SAlexander Graf 	vcpu->arch.shared->sprg3 = regs->sprg3;
1336b5904972SScott Wood 	vcpu->arch.shared->sprg4 = regs->sprg4;
1337b5904972SScott Wood 	vcpu->arch.shared->sprg5 = regs->sprg5;
1338b5904972SScott Wood 	vcpu->arch.shared->sprg6 = regs->sprg6;
1339b5904972SScott Wood 	vcpu->arch.shared->sprg7 = regs->sprg7;
1340d9fbd03dSHollis Blanchard 
13418e5b26b5SAlexander Graf 	for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
13428e5b26b5SAlexander Graf 		kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
1343d9fbd03dSHollis Blanchard 
1344d9fbd03dSHollis Blanchard 	return 0;
1345d9fbd03dSHollis Blanchard }
1346d9fbd03dSHollis Blanchard 
13475ce941eeSScott Wood static void get_sregs_base(struct kvm_vcpu *vcpu,
13485ce941eeSScott Wood                            struct kvm_sregs *sregs)
13495ce941eeSScott Wood {
13505ce941eeSScott Wood 	u64 tb = get_tb();
13515ce941eeSScott Wood 
13525ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_BASE;
13535ce941eeSScott Wood 
13545ce941eeSScott Wood 	sregs->u.e.csrr0 = vcpu->arch.csrr0;
13555ce941eeSScott Wood 	sregs->u.e.csrr1 = vcpu->arch.csrr1;
13565ce941eeSScott Wood 	sregs->u.e.mcsr = vcpu->arch.mcsr;
1357d30f6e48SScott Wood 	sregs->u.e.esr = get_guest_esr(vcpu);
1358d30f6e48SScott Wood 	sregs->u.e.dear = get_guest_dear(vcpu);
13595ce941eeSScott Wood 	sregs->u.e.tsr = vcpu->arch.tsr;
13605ce941eeSScott Wood 	sregs->u.e.tcr = vcpu->arch.tcr;
13615ce941eeSScott Wood 	sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
13625ce941eeSScott Wood 	sregs->u.e.tb = tb;
13635ce941eeSScott Wood 	sregs->u.e.vrsave = vcpu->arch.vrsave;
13645ce941eeSScott Wood }
13655ce941eeSScott Wood 
13665ce941eeSScott Wood static int set_sregs_base(struct kvm_vcpu *vcpu,
13675ce941eeSScott Wood                           struct kvm_sregs *sregs)
13685ce941eeSScott Wood {
13695ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
13705ce941eeSScott Wood 		return 0;
13715ce941eeSScott Wood 
13725ce941eeSScott Wood 	vcpu->arch.csrr0 = sregs->u.e.csrr0;
13735ce941eeSScott Wood 	vcpu->arch.csrr1 = sregs->u.e.csrr1;
13745ce941eeSScott Wood 	vcpu->arch.mcsr = sregs->u.e.mcsr;
1375d30f6e48SScott Wood 	set_guest_esr(vcpu, sregs->u.e.esr);
1376d30f6e48SScott Wood 	set_guest_dear(vcpu, sregs->u.e.dear);
13775ce941eeSScott Wood 	vcpu->arch.vrsave = sregs->u.e.vrsave;
1378dfd4d47eSScott Wood 	kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
13795ce941eeSScott Wood 
1380dfd4d47eSScott Wood 	if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
13815ce941eeSScott Wood 		vcpu->arch.dec = sregs->u.e.dec;
13825ce941eeSScott Wood 		kvmppc_emulate_dec(vcpu);
1383dfd4d47eSScott Wood 	}
13845ce941eeSScott Wood 
1385d26f22c9SBharat Bhushan 	if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR)
1386d26f22c9SBharat Bhushan 		kvmppc_set_tsr(vcpu, sregs->u.e.tsr);
13875ce941eeSScott Wood 
13885ce941eeSScott Wood 	return 0;
13895ce941eeSScott Wood }
13905ce941eeSScott Wood 
13915ce941eeSScott Wood static void get_sregs_arch206(struct kvm_vcpu *vcpu,
13925ce941eeSScott Wood                               struct kvm_sregs *sregs)
13935ce941eeSScott Wood {
13945ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_ARCH206;
13955ce941eeSScott Wood 
1396841741f2SScott Wood 	sregs->u.e.pir = vcpu->vcpu_id;
13975ce941eeSScott Wood 	sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
13985ce941eeSScott Wood 	sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
13995ce941eeSScott Wood 	sregs->u.e.decar = vcpu->arch.decar;
14005ce941eeSScott Wood 	sregs->u.e.ivpr = vcpu->arch.ivpr;
14015ce941eeSScott Wood }
14025ce941eeSScott Wood 
14035ce941eeSScott Wood static int set_sregs_arch206(struct kvm_vcpu *vcpu,
14045ce941eeSScott Wood                              struct kvm_sregs *sregs)
14055ce941eeSScott Wood {
14065ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
14075ce941eeSScott Wood 		return 0;
14085ce941eeSScott Wood 
1409841741f2SScott Wood 	if (sregs->u.e.pir != vcpu->vcpu_id)
14105ce941eeSScott Wood 		return -EINVAL;
14115ce941eeSScott Wood 
14125ce941eeSScott Wood 	vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
14135ce941eeSScott Wood 	vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
14145ce941eeSScott Wood 	vcpu->arch.decar = sregs->u.e.decar;
14155ce941eeSScott Wood 	vcpu->arch.ivpr = sregs->u.e.ivpr;
14165ce941eeSScott Wood 
14175ce941eeSScott Wood 	return 0;
14185ce941eeSScott Wood }
14195ce941eeSScott Wood 
14203a167beaSAneesh Kumar K.V int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
14215ce941eeSScott Wood {
14225ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_IVOR;
14235ce941eeSScott Wood 
14245ce941eeSScott Wood 	sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
14255ce941eeSScott Wood 	sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
14265ce941eeSScott Wood 	sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
14275ce941eeSScott Wood 	sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
14285ce941eeSScott Wood 	sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
14295ce941eeSScott Wood 	sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
14305ce941eeSScott Wood 	sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
14315ce941eeSScott Wood 	sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
14325ce941eeSScott Wood 	sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
14335ce941eeSScott Wood 	sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
14345ce941eeSScott Wood 	sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
14355ce941eeSScott Wood 	sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
14365ce941eeSScott Wood 	sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
14375ce941eeSScott Wood 	sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
14385ce941eeSScott Wood 	sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
14395ce941eeSScott Wood 	sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
14403a167beaSAneesh Kumar K.V 	return 0;
14415ce941eeSScott Wood }
14425ce941eeSScott Wood 
14435ce941eeSScott Wood int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
14445ce941eeSScott Wood {
14455ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
14465ce941eeSScott Wood 		return 0;
14475ce941eeSScott Wood 
14485ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
14495ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
14505ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
14515ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
14525ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
14535ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
14545ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
14555ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
14565ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
14575ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
14585ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
14595ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
14605ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
14615ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
14625ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
14635ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
14645ce941eeSScott Wood 
14655ce941eeSScott Wood 	return 0;
14665ce941eeSScott Wood }
14675ce941eeSScott Wood 
1468d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1469d9fbd03dSHollis Blanchard                                   struct kvm_sregs *sregs)
1470d9fbd03dSHollis Blanchard {
14715ce941eeSScott Wood 	sregs->pvr = vcpu->arch.pvr;
14725ce941eeSScott Wood 
14735ce941eeSScott Wood 	get_sregs_base(vcpu, sregs);
14745ce941eeSScott Wood 	get_sregs_arch206(vcpu, sregs);
1475*cbbc58d4SAneesh Kumar K.V 	return vcpu->kvm->arch.kvm_ops->get_sregs(vcpu, sregs);
1476d9fbd03dSHollis Blanchard }
1477d9fbd03dSHollis Blanchard 
1478d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1479d9fbd03dSHollis Blanchard                                   struct kvm_sregs *sregs)
1480d9fbd03dSHollis Blanchard {
14815ce941eeSScott Wood 	int ret;
14825ce941eeSScott Wood 
14835ce941eeSScott Wood 	if (vcpu->arch.pvr != sregs->pvr)
14845ce941eeSScott Wood 		return -EINVAL;
14855ce941eeSScott Wood 
14865ce941eeSScott Wood 	ret = set_sregs_base(vcpu, sregs);
14875ce941eeSScott Wood 	if (ret < 0)
14885ce941eeSScott Wood 		return ret;
14895ce941eeSScott Wood 
14905ce941eeSScott Wood 	ret = set_sregs_arch206(vcpu, sregs);
14915ce941eeSScott Wood 	if (ret < 0)
14925ce941eeSScott Wood 		return ret;
14935ce941eeSScott Wood 
1494*cbbc58d4SAneesh Kumar K.V 	return vcpu->kvm->arch.kvm_ops->set_sregs(vcpu, sregs);
1495d9fbd03dSHollis Blanchard }
1496d9fbd03dSHollis Blanchard 
149731f3438eSPaul Mackerras int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
149831f3438eSPaul Mackerras {
149935b299e2SMihai Caraman 	int r = 0;
150035b299e2SMihai Caraman 	union kvmppc_one_reg val;
150135b299e2SMihai Caraman 	int size;
150235b299e2SMihai Caraman 
150335b299e2SMihai Caraman 	size = one_reg_size(reg->id);
150435b299e2SMihai Caraman 	if (size > sizeof(val))
150535b299e2SMihai Caraman 		return -EINVAL;
15066df8d3fcSBharat Bhushan 
15076df8d3fcSBharat Bhushan 	switch (reg->id) {
15086df8d3fcSBharat Bhushan 	case KVM_REG_PPC_IAC1:
1509547465efSBharat Bhushan 		val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac1);
15106df8d3fcSBharat Bhushan 		break;
1511547465efSBharat Bhushan 	case KVM_REG_PPC_IAC2:
1512547465efSBharat Bhushan 		val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac2);
1513547465efSBharat Bhushan 		break;
1514547465efSBharat Bhushan #if CONFIG_PPC_ADV_DEBUG_IACS > 2
1515547465efSBharat Bhushan 	case KVM_REG_PPC_IAC3:
1516547465efSBharat Bhushan 		val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac3);
1517547465efSBharat Bhushan 		break;
1518547465efSBharat Bhushan 	case KVM_REG_PPC_IAC4:
1519547465efSBharat Bhushan 		val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac4);
1520547465efSBharat Bhushan 		break;
1521547465efSBharat Bhushan #endif
15226df8d3fcSBharat Bhushan 	case KVM_REG_PPC_DAC1:
1523547465efSBharat Bhushan 		val = get_reg_val(reg->id, vcpu->arch.dbg_reg.dac1);
1524547465efSBharat Bhushan 		break;
152535b299e2SMihai Caraman 	case KVM_REG_PPC_DAC2:
1526547465efSBharat Bhushan 		val = get_reg_val(reg->id, vcpu->arch.dbg_reg.dac2);
15276df8d3fcSBharat Bhushan 		break;
1528324b3e63SAlexander Graf 	case KVM_REG_PPC_EPR: {
1529324b3e63SAlexander Graf 		u32 epr = get_guest_epr(vcpu);
153035b299e2SMihai Caraman 		val = get_reg_val(reg->id, epr);
1531324b3e63SAlexander Graf 		break;
1532324b3e63SAlexander Graf 	}
1533352df1deSMihai Caraman #if defined(CONFIG_64BIT)
1534352df1deSMihai Caraman 	case KVM_REG_PPC_EPCR:
153535b299e2SMihai Caraman 		val = get_reg_val(reg->id, vcpu->arch.epcr);
1536352df1deSMihai Caraman 		break;
1537352df1deSMihai Caraman #endif
153878accda4SBharat Bhushan 	case KVM_REG_PPC_TCR:
153935b299e2SMihai Caraman 		val = get_reg_val(reg->id, vcpu->arch.tcr);
154078accda4SBharat Bhushan 		break;
154178accda4SBharat Bhushan 	case KVM_REG_PPC_TSR:
154235b299e2SMihai Caraman 		val = get_reg_val(reg->id, vcpu->arch.tsr);
154378accda4SBharat Bhushan 		break;
154435b299e2SMihai Caraman 	case KVM_REG_PPC_DEBUG_INST:
1545b12c7841SBharat Bhushan 		val = get_reg_val(reg->id, KVMPPC_INST_EHPRIV_DEBUG);
15468c32a2eaSBharat Bhushan 		break;
15478b75cbbeSPaul Mackerras 	case KVM_REG_PPC_VRSAVE:
15488b75cbbeSPaul Mackerras 		val = get_reg_val(reg->id, vcpu->arch.vrsave);
15498b75cbbeSPaul Mackerras 		break;
15506df8d3fcSBharat Bhushan 	default:
1551*cbbc58d4SAneesh Kumar K.V 		r = vcpu->kvm->arch.kvm_ops->get_one_reg(vcpu, reg->id, &val);
15526df8d3fcSBharat Bhushan 		break;
15536df8d3fcSBharat Bhushan 	}
155435b299e2SMihai Caraman 
155535b299e2SMihai Caraman 	if (r)
155635b299e2SMihai Caraman 		return r;
155735b299e2SMihai Caraman 
155835b299e2SMihai Caraman 	if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
155935b299e2SMihai Caraman 		r = -EFAULT;
156035b299e2SMihai Caraman 
15616df8d3fcSBharat Bhushan 	return r;
156231f3438eSPaul Mackerras }
156331f3438eSPaul Mackerras 
156431f3438eSPaul Mackerras int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
156531f3438eSPaul Mackerras {
156635b299e2SMihai Caraman 	int r = 0;
156735b299e2SMihai Caraman 	union kvmppc_one_reg val;
156835b299e2SMihai Caraman 	int size;
156935b299e2SMihai Caraman 
157035b299e2SMihai Caraman 	size = one_reg_size(reg->id);
157135b299e2SMihai Caraman 	if (size > sizeof(val))
157235b299e2SMihai Caraman 		return -EINVAL;
157335b299e2SMihai Caraman 
157435b299e2SMihai Caraman 	if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
157535b299e2SMihai Caraman 		return -EFAULT;
15766df8d3fcSBharat Bhushan 
15776df8d3fcSBharat Bhushan 	switch (reg->id) {
15786df8d3fcSBharat Bhushan 	case KVM_REG_PPC_IAC1:
1579547465efSBharat Bhushan 		vcpu->arch.dbg_reg.iac1 = set_reg_val(reg->id, val);
15806df8d3fcSBharat Bhushan 		break;
1581547465efSBharat Bhushan 	case KVM_REG_PPC_IAC2:
1582547465efSBharat Bhushan 		vcpu->arch.dbg_reg.iac2 = set_reg_val(reg->id, val);
1583547465efSBharat Bhushan 		break;
1584547465efSBharat Bhushan #if CONFIG_PPC_ADV_DEBUG_IACS > 2
1585547465efSBharat Bhushan 	case KVM_REG_PPC_IAC3:
1586547465efSBharat Bhushan 		vcpu->arch.dbg_reg.iac3 = set_reg_val(reg->id, val);
1587547465efSBharat Bhushan 		break;
1588547465efSBharat Bhushan 	case KVM_REG_PPC_IAC4:
1589547465efSBharat Bhushan 		vcpu->arch.dbg_reg.iac4 = set_reg_val(reg->id, val);
1590547465efSBharat Bhushan 		break;
1591547465efSBharat Bhushan #endif
15926df8d3fcSBharat Bhushan 	case KVM_REG_PPC_DAC1:
1593547465efSBharat Bhushan 		vcpu->arch.dbg_reg.dac1 = set_reg_val(reg->id, val);
1594547465efSBharat Bhushan 		break;
159535b299e2SMihai Caraman 	case KVM_REG_PPC_DAC2:
1596547465efSBharat Bhushan 		vcpu->arch.dbg_reg.dac2 = set_reg_val(reg->id, val);
15976df8d3fcSBharat Bhushan 		break;
1598324b3e63SAlexander Graf 	case KVM_REG_PPC_EPR: {
159935b299e2SMihai Caraman 		u32 new_epr = set_reg_val(reg->id, val);
1600324b3e63SAlexander Graf 		kvmppc_set_epr(vcpu, new_epr);
1601324b3e63SAlexander Graf 		break;
1602324b3e63SAlexander Graf 	}
1603352df1deSMihai Caraman #if defined(CONFIG_64BIT)
1604352df1deSMihai Caraman 	case KVM_REG_PPC_EPCR: {
160535b299e2SMihai Caraman 		u32 new_epcr = set_reg_val(reg->id, val);
1606352df1deSMihai Caraman 		kvmppc_set_epcr(vcpu, new_epcr);
1607352df1deSMihai Caraman 		break;
1608352df1deSMihai Caraman 	}
1609352df1deSMihai Caraman #endif
161078accda4SBharat Bhushan 	case KVM_REG_PPC_OR_TSR: {
161135b299e2SMihai Caraman 		u32 tsr_bits = set_reg_val(reg->id, val);
161278accda4SBharat Bhushan 		kvmppc_set_tsr_bits(vcpu, tsr_bits);
161378accda4SBharat Bhushan 		break;
161478accda4SBharat Bhushan 	}
161578accda4SBharat Bhushan 	case KVM_REG_PPC_CLEAR_TSR: {
161635b299e2SMihai Caraman 		u32 tsr_bits = set_reg_val(reg->id, val);
161778accda4SBharat Bhushan 		kvmppc_clr_tsr_bits(vcpu, tsr_bits);
161878accda4SBharat Bhushan 		break;
161978accda4SBharat Bhushan 	}
162078accda4SBharat Bhushan 	case KVM_REG_PPC_TSR: {
162135b299e2SMihai Caraman 		u32 tsr = set_reg_val(reg->id, val);
162278accda4SBharat Bhushan 		kvmppc_set_tsr(vcpu, tsr);
162378accda4SBharat Bhushan 		break;
162478accda4SBharat Bhushan 	}
162578accda4SBharat Bhushan 	case KVM_REG_PPC_TCR: {
162635b299e2SMihai Caraman 		u32 tcr = set_reg_val(reg->id, val);
162778accda4SBharat Bhushan 		kvmppc_set_tcr(vcpu, tcr);
162878accda4SBharat Bhushan 		break;
162978accda4SBharat Bhushan 	}
16308b75cbbeSPaul Mackerras 	case KVM_REG_PPC_VRSAVE:
16318b75cbbeSPaul Mackerras 		vcpu->arch.vrsave = set_reg_val(reg->id, val);
16328b75cbbeSPaul Mackerras 		break;
16336df8d3fcSBharat Bhushan 	default:
1634*cbbc58d4SAneesh Kumar K.V 		r = vcpu->kvm->arch.kvm_ops->set_one_reg(vcpu, reg->id, &val);
16356df8d3fcSBharat Bhushan 		break;
16366df8d3fcSBharat Bhushan 	}
163735b299e2SMihai Caraman 
16386df8d3fcSBharat Bhushan 	return r;
163931f3438eSPaul Mackerras }
164031f3438eSPaul Mackerras 
1641d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1642d9fbd03dSHollis Blanchard {
1643d9fbd03dSHollis Blanchard 	return -ENOTSUPP;
1644d9fbd03dSHollis Blanchard }
1645d9fbd03dSHollis Blanchard 
1646d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1647d9fbd03dSHollis Blanchard {
1648d9fbd03dSHollis Blanchard 	return -ENOTSUPP;
1649d9fbd03dSHollis Blanchard }
1650d9fbd03dSHollis Blanchard 
1651d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1652d9fbd03dSHollis Blanchard                                   struct kvm_translation *tr)
1653d9fbd03dSHollis Blanchard {
165498001d8dSAvi Kivity 	int r;
165598001d8dSAvi Kivity 
165698001d8dSAvi Kivity 	r = kvmppc_core_vcpu_translate(vcpu, tr);
165798001d8dSAvi Kivity 	return r;
1658d9fbd03dSHollis Blanchard }
1659d9fbd03dSHollis Blanchard 
16604e755758SAlexander Graf int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
16614e755758SAlexander Graf {
16624e755758SAlexander Graf 	return -ENOTSUPP;
16634e755758SAlexander Graf }
16644e755758SAlexander Graf 
16655587027cSAneesh Kumar K.V void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
1666a66b48c3SPaul Mackerras 			      struct kvm_memory_slot *dont)
1667a66b48c3SPaul Mackerras {
1668a66b48c3SPaul Mackerras }
1669a66b48c3SPaul Mackerras 
16705587027cSAneesh Kumar K.V int kvmppc_core_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
1671a66b48c3SPaul Mackerras 			       unsigned long npages)
1672a66b48c3SPaul Mackerras {
1673a66b48c3SPaul Mackerras 	return 0;
1674a66b48c3SPaul Mackerras }
1675a66b48c3SPaul Mackerras 
1676f9e0554dSPaul Mackerras int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1677a66b48c3SPaul Mackerras 				      struct kvm_memory_slot *memslot,
1678f9e0554dSPaul Mackerras 				      struct kvm_userspace_memory_region *mem)
1679f9e0554dSPaul Mackerras {
1680f9e0554dSPaul Mackerras 	return 0;
1681f9e0554dSPaul Mackerras }
1682f9e0554dSPaul Mackerras 
1683f9e0554dSPaul Mackerras void kvmppc_core_commit_memory_region(struct kvm *kvm,
1684dfe49dbdSPaul Mackerras 				struct kvm_userspace_memory_region *mem,
16858482644aSTakuya Yoshikawa 				const struct kvm_memory_slot *old)
1686dfe49dbdSPaul Mackerras {
1687dfe49dbdSPaul Mackerras }
1688dfe49dbdSPaul Mackerras 
1689dfe49dbdSPaul Mackerras void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
1690f9e0554dSPaul Mackerras {
1691f9e0554dSPaul Mackerras }
1692f9e0554dSPaul Mackerras 
169338f98824SMihai Caraman void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr)
169438f98824SMihai Caraman {
169538f98824SMihai Caraman #if defined(CONFIG_64BIT)
169638f98824SMihai Caraman 	vcpu->arch.epcr = new_epcr;
169738f98824SMihai Caraman #ifdef CONFIG_KVM_BOOKE_HV
169838f98824SMihai Caraman 	vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM;
169938f98824SMihai Caraman 	if (vcpu->arch.epcr  & SPRN_EPCR_ICM)
170038f98824SMihai Caraman 		vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM;
170138f98824SMihai Caraman #endif
170238f98824SMihai Caraman #endif
170338f98824SMihai Caraman }
170438f98824SMihai Caraman 
1705dfd4d47eSScott Wood void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1706dfd4d47eSScott Wood {
1707dfd4d47eSScott Wood 	vcpu->arch.tcr = new_tcr;
1708f61c94bbSBharat Bhushan 	arm_next_watchdog(vcpu);
1709dfd4d47eSScott Wood 	update_timer_ints(vcpu);
1710dfd4d47eSScott Wood }
1711dfd4d47eSScott Wood 
1712dfd4d47eSScott Wood void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1713dfd4d47eSScott Wood {
1714dfd4d47eSScott Wood 	set_bits(tsr_bits, &vcpu->arch.tsr);
1715dfd4d47eSScott Wood 	smp_wmb();
1716dfd4d47eSScott Wood 	kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1717dfd4d47eSScott Wood 	kvm_vcpu_kick(vcpu);
1718dfd4d47eSScott Wood }
1719dfd4d47eSScott Wood 
1720dfd4d47eSScott Wood void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1721dfd4d47eSScott Wood {
1722dfd4d47eSScott Wood 	clear_bits(tsr_bits, &vcpu->arch.tsr);
1723f61c94bbSBharat Bhushan 
1724f61c94bbSBharat Bhushan 	/*
1725f61c94bbSBharat Bhushan 	 * We may have stopped the watchdog due to
1726f61c94bbSBharat Bhushan 	 * being stuck on final expiration.
1727f61c94bbSBharat Bhushan 	 */
1728f61c94bbSBharat Bhushan 	if (tsr_bits & (TSR_ENW | TSR_WIS))
1729f61c94bbSBharat Bhushan 		arm_next_watchdog(vcpu);
1730f61c94bbSBharat Bhushan 
1731dfd4d47eSScott Wood 	update_timer_ints(vcpu);
1732dfd4d47eSScott Wood }
1733dfd4d47eSScott Wood 
1734dfd4d47eSScott Wood void kvmppc_decrementer_func(unsigned long data)
1735dfd4d47eSScott Wood {
1736dfd4d47eSScott Wood 	struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1737dfd4d47eSScott Wood 
173821bd000aSBharat Bhushan 	if (vcpu->arch.tcr & TCR_ARE) {
173921bd000aSBharat Bhushan 		vcpu->arch.dec = vcpu->arch.decar;
174021bd000aSBharat Bhushan 		kvmppc_emulate_dec(vcpu);
174121bd000aSBharat Bhushan 	}
174221bd000aSBharat Bhushan 
1743dfd4d47eSScott Wood 	kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1744dfd4d47eSScott Wood }
1745dfd4d47eSScott Wood 
1746ce11e48bSBharat Bhushan static int kvmppc_booke_add_breakpoint(struct debug_reg *dbg_reg,
1747ce11e48bSBharat Bhushan 				       uint64_t addr, int index)
1748ce11e48bSBharat Bhushan {
1749ce11e48bSBharat Bhushan 	switch (index) {
1750ce11e48bSBharat Bhushan 	case 0:
1751ce11e48bSBharat Bhushan 		dbg_reg->dbcr0 |= DBCR0_IAC1;
1752ce11e48bSBharat Bhushan 		dbg_reg->iac1 = addr;
1753ce11e48bSBharat Bhushan 		break;
1754ce11e48bSBharat Bhushan 	case 1:
1755ce11e48bSBharat Bhushan 		dbg_reg->dbcr0 |= DBCR0_IAC2;
1756ce11e48bSBharat Bhushan 		dbg_reg->iac2 = addr;
1757ce11e48bSBharat Bhushan 		break;
1758ce11e48bSBharat Bhushan #if CONFIG_PPC_ADV_DEBUG_IACS > 2
1759ce11e48bSBharat Bhushan 	case 2:
1760ce11e48bSBharat Bhushan 		dbg_reg->dbcr0 |= DBCR0_IAC3;
1761ce11e48bSBharat Bhushan 		dbg_reg->iac3 = addr;
1762ce11e48bSBharat Bhushan 		break;
1763ce11e48bSBharat Bhushan 	case 3:
1764ce11e48bSBharat Bhushan 		dbg_reg->dbcr0 |= DBCR0_IAC4;
1765ce11e48bSBharat Bhushan 		dbg_reg->iac4 = addr;
1766ce11e48bSBharat Bhushan 		break;
1767ce11e48bSBharat Bhushan #endif
1768ce11e48bSBharat Bhushan 	default:
1769ce11e48bSBharat Bhushan 		return -EINVAL;
1770ce11e48bSBharat Bhushan 	}
1771ce11e48bSBharat Bhushan 
1772ce11e48bSBharat Bhushan 	dbg_reg->dbcr0 |= DBCR0_IDM;
1773ce11e48bSBharat Bhushan 	return 0;
1774ce11e48bSBharat Bhushan }
1775ce11e48bSBharat Bhushan 
1776ce11e48bSBharat Bhushan static int kvmppc_booke_add_watchpoint(struct debug_reg *dbg_reg, uint64_t addr,
1777ce11e48bSBharat Bhushan 				       int type, int index)
1778ce11e48bSBharat Bhushan {
1779ce11e48bSBharat Bhushan 	switch (index) {
1780ce11e48bSBharat Bhushan 	case 0:
1781ce11e48bSBharat Bhushan 		if (type & KVMPPC_DEBUG_WATCH_READ)
1782ce11e48bSBharat Bhushan 			dbg_reg->dbcr0 |= DBCR0_DAC1R;
1783ce11e48bSBharat Bhushan 		if (type & KVMPPC_DEBUG_WATCH_WRITE)
1784ce11e48bSBharat Bhushan 			dbg_reg->dbcr0 |= DBCR0_DAC1W;
1785ce11e48bSBharat Bhushan 		dbg_reg->dac1 = addr;
1786ce11e48bSBharat Bhushan 		break;
1787ce11e48bSBharat Bhushan 	case 1:
1788ce11e48bSBharat Bhushan 		if (type & KVMPPC_DEBUG_WATCH_READ)
1789ce11e48bSBharat Bhushan 			dbg_reg->dbcr0 |= DBCR0_DAC2R;
1790ce11e48bSBharat Bhushan 		if (type & KVMPPC_DEBUG_WATCH_WRITE)
1791ce11e48bSBharat Bhushan 			dbg_reg->dbcr0 |= DBCR0_DAC2W;
1792ce11e48bSBharat Bhushan 		dbg_reg->dac2 = addr;
1793ce11e48bSBharat Bhushan 		break;
1794ce11e48bSBharat Bhushan 	default:
1795ce11e48bSBharat Bhushan 		return -EINVAL;
1796ce11e48bSBharat Bhushan 	}
1797ce11e48bSBharat Bhushan 
1798ce11e48bSBharat Bhushan 	dbg_reg->dbcr0 |= DBCR0_IDM;
1799ce11e48bSBharat Bhushan 	return 0;
1800ce11e48bSBharat Bhushan }
1801ce11e48bSBharat Bhushan void kvm_guest_protect_msr(struct kvm_vcpu *vcpu, ulong prot_bitmap, bool set)
1802ce11e48bSBharat Bhushan {
1803ce11e48bSBharat Bhushan 	/* XXX: Add similar MSR protection for BookE-PR */
1804ce11e48bSBharat Bhushan #ifdef CONFIG_KVM_BOOKE_HV
1805ce11e48bSBharat Bhushan 	BUG_ON(prot_bitmap & ~(MSRP_UCLEP | MSRP_DEP | MSRP_PMMP));
1806ce11e48bSBharat Bhushan 	if (set) {
1807ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_UCLE)
1808ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp |= MSRP_UCLEP;
1809ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_DE)
1810ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp |= MSRP_DEP;
1811ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_PMM)
1812ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp |= MSRP_PMMP;
1813ce11e48bSBharat Bhushan 	} else {
1814ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_UCLE)
1815ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp &= ~MSRP_UCLEP;
1816ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_DE)
1817ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp &= ~MSRP_DEP;
1818ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_PMM)
1819ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp &= ~MSRP_PMMP;
1820ce11e48bSBharat Bhushan 	}
1821ce11e48bSBharat Bhushan #endif
1822ce11e48bSBharat Bhushan }
1823ce11e48bSBharat Bhushan 
1824ce11e48bSBharat Bhushan int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1825ce11e48bSBharat Bhushan 					 struct kvm_guest_debug *dbg)
1826ce11e48bSBharat Bhushan {
1827ce11e48bSBharat Bhushan 	struct debug_reg *dbg_reg;
1828ce11e48bSBharat Bhushan 	int n, b = 0, w = 0;
1829ce11e48bSBharat Bhushan 
1830ce11e48bSBharat Bhushan 	if (!(dbg->control & KVM_GUESTDBG_ENABLE)) {
1831ce11e48bSBharat Bhushan 		vcpu->arch.shadow_dbg_reg.dbcr0 = 0;
1832ce11e48bSBharat Bhushan 		vcpu->guest_debug = 0;
1833ce11e48bSBharat Bhushan 		kvm_guest_protect_msr(vcpu, MSR_DE, false);
1834ce11e48bSBharat Bhushan 		return 0;
1835ce11e48bSBharat Bhushan 	}
1836ce11e48bSBharat Bhushan 
1837ce11e48bSBharat Bhushan 	kvm_guest_protect_msr(vcpu, MSR_DE, true);
1838ce11e48bSBharat Bhushan 	vcpu->guest_debug = dbg->control;
1839ce11e48bSBharat Bhushan 	vcpu->arch.shadow_dbg_reg.dbcr0 = 0;
1840ce11e48bSBharat Bhushan 	/* Set DBCR0_EDM in guest visible DBCR0 register. */
1841ce11e48bSBharat Bhushan 	vcpu->arch.dbg_reg.dbcr0 = DBCR0_EDM;
1842ce11e48bSBharat Bhushan 
1843ce11e48bSBharat Bhushan 	if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
1844ce11e48bSBharat Bhushan 		vcpu->arch.shadow_dbg_reg.dbcr0 |= DBCR0_IDM | DBCR0_IC;
1845ce11e48bSBharat Bhushan 
1846ce11e48bSBharat Bhushan 	/* Code below handles only HW breakpoints */
1847ce11e48bSBharat Bhushan 	dbg_reg = &(vcpu->arch.shadow_dbg_reg);
1848ce11e48bSBharat Bhushan 
1849ce11e48bSBharat Bhushan #ifdef CONFIG_KVM_BOOKE_HV
1850ce11e48bSBharat Bhushan 	/*
1851ce11e48bSBharat Bhushan 	 * On BookE-HV (e500mc) the guest is always executed with MSR.GS=1
1852ce11e48bSBharat Bhushan 	 * DBCR1 and DBCR2 are set to trigger debug events when MSR.PR is 0
1853ce11e48bSBharat Bhushan 	 */
1854ce11e48bSBharat Bhushan 	dbg_reg->dbcr1 = 0;
1855ce11e48bSBharat Bhushan 	dbg_reg->dbcr2 = 0;
1856ce11e48bSBharat Bhushan #else
1857ce11e48bSBharat Bhushan 	/*
1858ce11e48bSBharat Bhushan 	 * On BookE-PR (e500v2) the guest is always executed with MSR.PR=1
1859ce11e48bSBharat Bhushan 	 * We set DBCR1 and DBCR2 to only trigger debug events when MSR.PR
1860ce11e48bSBharat Bhushan 	 * is set.
1861ce11e48bSBharat Bhushan 	 */
1862ce11e48bSBharat Bhushan 	dbg_reg->dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US | DBCR1_IAC3US |
1863ce11e48bSBharat Bhushan 			  DBCR1_IAC4US;
1864ce11e48bSBharat Bhushan 	dbg_reg->dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
1865ce11e48bSBharat Bhushan #endif
1866ce11e48bSBharat Bhushan 
1867ce11e48bSBharat Bhushan 	if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1868ce11e48bSBharat Bhushan 		return 0;
1869ce11e48bSBharat Bhushan 
1870ce11e48bSBharat Bhushan 	for (n = 0; n < (KVMPPC_BOOKE_IAC_NUM + KVMPPC_BOOKE_DAC_NUM); n++) {
1871ce11e48bSBharat Bhushan 		uint64_t addr = dbg->arch.bp[n].addr;
1872ce11e48bSBharat Bhushan 		uint32_t type = dbg->arch.bp[n].type;
1873ce11e48bSBharat Bhushan 
1874ce11e48bSBharat Bhushan 		if (type == KVMPPC_DEBUG_NONE)
1875ce11e48bSBharat Bhushan 			continue;
1876ce11e48bSBharat Bhushan 
1877ce11e48bSBharat Bhushan 		if (type & !(KVMPPC_DEBUG_WATCH_READ |
1878ce11e48bSBharat Bhushan 			     KVMPPC_DEBUG_WATCH_WRITE |
1879ce11e48bSBharat Bhushan 			     KVMPPC_DEBUG_BREAKPOINT))
1880ce11e48bSBharat Bhushan 			return -EINVAL;
1881ce11e48bSBharat Bhushan 
1882ce11e48bSBharat Bhushan 		if (type & KVMPPC_DEBUG_BREAKPOINT) {
1883ce11e48bSBharat Bhushan 			/* Setting H/W breakpoint */
1884ce11e48bSBharat Bhushan 			if (kvmppc_booke_add_breakpoint(dbg_reg, addr, b++))
1885ce11e48bSBharat Bhushan 				return -EINVAL;
1886ce11e48bSBharat Bhushan 		} else {
1887ce11e48bSBharat Bhushan 			/* Setting H/W watchpoint */
1888ce11e48bSBharat Bhushan 			if (kvmppc_booke_add_watchpoint(dbg_reg, addr,
1889ce11e48bSBharat Bhushan 							type, w++))
1890ce11e48bSBharat Bhushan 				return -EINVAL;
1891ce11e48bSBharat Bhushan 		}
1892ce11e48bSBharat Bhushan 	}
1893ce11e48bSBharat Bhushan 
1894ce11e48bSBharat Bhushan 	return 0;
1895ce11e48bSBharat Bhushan }
1896ce11e48bSBharat Bhushan 
189794fa9d99SScott Wood void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
189894fa9d99SScott Wood {
1899a47d72f3SPaul Mackerras 	vcpu->cpu = smp_processor_id();
1900d30f6e48SScott Wood 	current->thread.kvm_vcpu = vcpu;
190194fa9d99SScott Wood }
190294fa9d99SScott Wood 
190394fa9d99SScott Wood void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
190494fa9d99SScott Wood {
1905d30f6e48SScott Wood 	current->thread.kvm_vcpu = NULL;
1906a47d72f3SPaul Mackerras 	vcpu->cpu = -1;
1907ce11e48bSBharat Bhushan 
1908ce11e48bSBharat Bhushan 	/* Clear pending debug event in DBSR */
1909ce11e48bSBharat Bhushan 	kvmppc_clear_dbsr();
191094fa9d99SScott Wood }
191194fa9d99SScott Wood 
19123a167beaSAneesh Kumar K.V void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
19133a167beaSAneesh Kumar K.V {
1914*cbbc58d4SAneesh Kumar K.V 	vcpu->kvm->arch.kvm_ops->mmu_destroy(vcpu);
19153a167beaSAneesh Kumar K.V }
19163a167beaSAneesh Kumar K.V 
19173a167beaSAneesh Kumar K.V int kvmppc_core_init_vm(struct kvm *kvm)
19183a167beaSAneesh Kumar K.V {
1919*cbbc58d4SAneesh Kumar K.V 	return kvm->arch.kvm_ops->init_vm(kvm);
19203a167beaSAneesh Kumar K.V }
19213a167beaSAneesh Kumar K.V 
19223a167beaSAneesh Kumar K.V struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
19233a167beaSAneesh Kumar K.V {
1924*cbbc58d4SAneesh Kumar K.V 	return kvm->arch.kvm_ops->vcpu_create(kvm, id);
19253a167beaSAneesh Kumar K.V }
19263a167beaSAneesh Kumar K.V 
19273a167beaSAneesh Kumar K.V void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
19283a167beaSAneesh Kumar K.V {
1929*cbbc58d4SAneesh Kumar K.V 	vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
19303a167beaSAneesh Kumar K.V }
19313a167beaSAneesh Kumar K.V 
19323a167beaSAneesh Kumar K.V void kvmppc_core_destroy_vm(struct kvm *kvm)
19333a167beaSAneesh Kumar K.V {
1934*cbbc58d4SAneesh Kumar K.V 	kvm->arch.kvm_ops->destroy_vm(kvm);
19353a167beaSAneesh Kumar K.V }
19363a167beaSAneesh Kumar K.V 
19373a167beaSAneesh Kumar K.V void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
19383a167beaSAneesh Kumar K.V {
1939*cbbc58d4SAneesh Kumar K.V 	vcpu->kvm->arch.kvm_ops->vcpu_load(vcpu, cpu);
19403a167beaSAneesh Kumar K.V }
19413a167beaSAneesh Kumar K.V 
19423a167beaSAneesh Kumar K.V void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
19433a167beaSAneesh Kumar K.V {
1944*cbbc58d4SAneesh Kumar K.V 	vcpu->kvm->arch.kvm_ops->vcpu_put(vcpu);
19453a167beaSAneesh Kumar K.V }
19463a167beaSAneesh Kumar K.V 
19472986b8c7SStephen Rothwell int __init kvmppc_booke_init(void)
1948d9fbd03dSHollis Blanchard {
1949d30f6e48SScott Wood #ifndef CONFIG_KVM_BOOKE_HV
1950d9fbd03dSHollis Blanchard 	unsigned long ivor[16];
19511d542d9cSBharat Bhushan 	unsigned long *handler = kvmppc_booke_handler_addr;
1952d9fbd03dSHollis Blanchard 	unsigned long max_ivor = 0;
19531d542d9cSBharat Bhushan 	unsigned long handler_len;
1954d9fbd03dSHollis Blanchard 	int i;
1955d9fbd03dSHollis Blanchard 
1956d9fbd03dSHollis Blanchard 	/* We install our own exception handlers by hijacking IVPR. IVPR must
1957d9fbd03dSHollis Blanchard 	 * be 16-bit aligned, so we need a 64KB allocation. */
1958d9fbd03dSHollis Blanchard 	kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1959d9fbd03dSHollis Blanchard 	                                         VCPU_SIZE_ORDER);
1960d9fbd03dSHollis Blanchard 	if (!kvmppc_booke_handlers)
1961d9fbd03dSHollis Blanchard 		return -ENOMEM;
1962d9fbd03dSHollis Blanchard 
1963d9fbd03dSHollis Blanchard 	/* XXX make sure our handlers are smaller than Linux's */
1964d9fbd03dSHollis Blanchard 
1965d9fbd03dSHollis Blanchard 	/* Copy our interrupt handlers to match host IVORs. That way we don't
1966d9fbd03dSHollis Blanchard 	 * have to swap the IVORs on every guest/host transition. */
1967d9fbd03dSHollis Blanchard 	ivor[0] = mfspr(SPRN_IVOR0);
1968d9fbd03dSHollis Blanchard 	ivor[1] = mfspr(SPRN_IVOR1);
1969d9fbd03dSHollis Blanchard 	ivor[2] = mfspr(SPRN_IVOR2);
1970d9fbd03dSHollis Blanchard 	ivor[3] = mfspr(SPRN_IVOR3);
1971d9fbd03dSHollis Blanchard 	ivor[4] = mfspr(SPRN_IVOR4);
1972d9fbd03dSHollis Blanchard 	ivor[5] = mfspr(SPRN_IVOR5);
1973d9fbd03dSHollis Blanchard 	ivor[6] = mfspr(SPRN_IVOR6);
1974d9fbd03dSHollis Blanchard 	ivor[7] = mfspr(SPRN_IVOR7);
1975d9fbd03dSHollis Blanchard 	ivor[8] = mfspr(SPRN_IVOR8);
1976d9fbd03dSHollis Blanchard 	ivor[9] = mfspr(SPRN_IVOR9);
1977d9fbd03dSHollis Blanchard 	ivor[10] = mfspr(SPRN_IVOR10);
1978d9fbd03dSHollis Blanchard 	ivor[11] = mfspr(SPRN_IVOR11);
1979d9fbd03dSHollis Blanchard 	ivor[12] = mfspr(SPRN_IVOR12);
1980d9fbd03dSHollis Blanchard 	ivor[13] = mfspr(SPRN_IVOR13);
1981d9fbd03dSHollis Blanchard 	ivor[14] = mfspr(SPRN_IVOR14);
1982d9fbd03dSHollis Blanchard 	ivor[15] = mfspr(SPRN_IVOR15);
1983d9fbd03dSHollis Blanchard 
1984d9fbd03dSHollis Blanchard 	for (i = 0; i < 16; i++) {
1985d9fbd03dSHollis Blanchard 		if (ivor[i] > max_ivor)
19861d542d9cSBharat Bhushan 			max_ivor = i;
1987d9fbd03dSHollis Blanchard 
19881d542d9cSBharat Bhushan 		handler_len = handler[i + 1] - handler[i];
1989d9fbd03dSHollis Blanchard 		memcpy((void *)kvmppc_booke_handlers + ivor[i],
19901d542d9cSBharat Bhushan 		       (void *)handler[i], handler_len);
1991d9fbd03dSHollis Blanchard 	}
19921d542d9cSBharat Bhushan 
19931d542d9cSBharat Bhushan 	handler_len = handler[max_ivor + 1] - handler[max_ivor];
19941d542d9cSBharat Bhushan 	flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers +
19951d542d9cSBharat Bhushan 			   ivor[max_ivor] + handler_len);
1996d30f6e48SScott Wood #endif /* !BOOKE_HV */
1997db93f574SHollis Blanchard 	return 0;
1998d9fbd03dSHollis Blanchard }
1999d9fbd03dSHollis Blanchard 
2000db93f574SHollis Blanchard void __exit kvmppc_booke_exit(void)
2001d9fbd03dSHollis Blanchard {
2002d9fbd03dSHollis Blanchard 	free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
2003d9fbd03dSHollis Blanchard 	kvm_exit();
2004d9fbd03dSHollis Blanchard }
2005