xref: /openbmc/linux/arch/powerpc/kvm/booke.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1d94d71cbSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d9fbd03dSHollis Blanchard /*
3d9fbd03dSHollis Blanchard  *
4d9fbd03dSHollis Blanchard  * Copyright IBM Corp. 2007
54cd35f67SScott Wood  * Copyright 2010-2011 Freescale Semiconductor, Inc.
6d9fbd03dSHollis Blanchard  *
7d9fbd03dSHollis Blanchard  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
8d9fbd03dSHollis Blanchard  *          Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
9d30f6e48SScott Wood  *          Scott Wood <scottwood@freescale.com>
10d30f6e48SScott Wood  *          Varun Sethi <varun.sethi@freescale.com>
11d9fbd03dSHollis Blanchard  */
12d9fbd03dSHollis Blanchard 
13d9fbd03dSHollis Blanchard #include <linux/errno.h>
14d9fbd03dSHollis Blanchard #include <linux/err.h>
15d9fbd03dSHollis Blanchard #include <linux/kvm_host.h>
165a0e3ad6STejun Heo #include <linux/gfp.h>
17d9fbd03dSHollis Blanchard #include <linux/module.h>
18d9fbd03dSHollis Blanchard #include <linux/vmalloc.h>
19d9fbd03dSHollis Blanchard #include <linux/fs.h>
207924bd41SHollis Blanchard 
21d9fbd03dSHollis Blanchard #include <asm/cputable.h>
227c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
233a96570fSNicholas Piggin #include <asm/interrupt.h>
24d9fbd03dSHollis Blanchard #include <asm/kvm_ppc.h>
25d9fbd03dSHollis Blanchard #include <asm/cacheflush.h>
26d30f6e48SScott Wood #include <asm/dbell.h>
27d30f6e48SScott Wood #include <asm/hw_irq.h>
28d30f6e48SScott Wood #include <asm/irq.h>
29b50df19cSMihai Caraman #include <asm/time.h>
30d9fbd03dSHollis Blanchard 
31d30f6e48SScott Wood #include "timing.h"
3275f74f0dSHollis Blanchard #include "booke.h"
33dba291f2SAneesh Kumar K.V 
34dba291f2SAneesh Kumar K.V #define CREATE_TRACE_POINTS
35dba291f2SAneesh Kumar K.V #include "trace_booke.h"
36d9fbd03dSHollis Blanchard 
37d9fbd03dSHollis Blanchard unsigned long kvmppc_booke_handlers;
38d9fbd03dSHollis Blanchard 
39fcfe1baeSJing Zhang const struct _kvm_stats_desc kvm_vm_stats_desc[] = {
40fcfe1baeSJing Zhang 	KVM_GENERIC_VM_STATS(),
41fcfe1baeSJing Zhang 	STATS_DESC_ICOUNTER(VM, num_2M_pages),
42fcfe1baeSJing Zhang 	STATS_DESC_ICOUNTER(VM, num_1G_pages)
43fcfe1baeSJing Zhang };
44fcfe1baeSJing Zhang 
45fcfe1baeSJing Zhang const struct kvm_stats_header kvm_vm_stats_header = {
46fcfe1baeSJing Zhang 	.name_size = KVM_STATS_NAME_SIZE,
47fcfe1baeSJing Zhang 	.num_desc = ARRAY_SIZE(kvm_vm_stats_desc),
48fcfe1baeSJing Zhang 	.id_offset = sizeof(struct kvm_stats_header),
49fcfe1baeSJing Zhang 	.desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
50fcfe1baeSJing Zhang 	.data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
51fcfe1baeSJing Zhang 		       sizeof(kvm_vm_stats_desc),
52fcfe1baeSJing Zhang };
53fcfe1baeSJing Zhang 
54ce55c049SJing Zhang const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
55ce55c049SJing Zhang 	KVM_GENERIC_VCPU_STATS(),
56ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, sum_exits),
57ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, mmio_exits),
58ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, signal_exits),
59ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, light_exits),
60ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, itlb_real_miss_exits),
61ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, itlb_virt_miss_exits),
62ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, dtlb_real_miss_exits),
63ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, dtlb_virt_miss_exits),
64ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, syscall_exits),
65ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, isi_exits),
66ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, dsi_exits),
67ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, emulated_inst_exits),
68ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, dec_exits),
69ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, ext_intr_exits),
70ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, halt_successful_wait),
71ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, dbell_exits),
72ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, gdbell_exits),
73ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, ld),
74ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, st),
75ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, pthru_all),
76ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, pthru_host),
77ce55c049SJing Zhang 	STATS_DESC_COUNTER(VCPU, pthru_bad_aff)
78ce55c049SJing Zhang };
79ce55c049SJing Zhang 
80ce55c049SJing Zhang const struct kvm_stats_header kvm_vcpu_stats_header = {
81ce55c049SJing Zhang 	.name_size = KVM_STATS_NAME_SIZE,
82ce55c049SJing Zhang 	.num_desc = ARRAY_SIZE(kvm_vcpu_stats_desc),
83ce55c049SJing Zhang 	.id_offset = sizeof(struct kvm_stats_header),
84ce55c049SJing Zhang 	.desc_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE,
85ce55c049SJing Zhang 	.data_offset = sizeof(struct kvm_stats_header) + KVM_STATS_NAME_SIZE +
86ce55c049SJing Zhang 		       sizeof(kvm_vcpu_stats_desc),
87ce55c049SJing Zhang };
88ce55c049SJing Zhang 
89d9fbd03dSHollis Blanchard /* TODO: use vcpu_printf() */
kvmppc_dump_vcpu(struct kvm_vcpu * vcpu)90d9fbd03dSHollis Blanchard void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
91d9fbd03dSHollis Blanchard {
92d9fbd03dSHollis Blanchard 	int i;
93d9fbd03dSHollis Blanchard 
94173c520aSSimon Guo 	printk("pc:   %08lx msr:  %08llx\n", vcpu->arch.regs.nip,
95173c520aSSimon Guo 			vcpu->arch.shared->msr);
96173c520aSSimon Guo 	printk("lr:   %08lx ctr:  %08lx\n", vcpu->arch.regs.link,
97173c520aSSimon Guo 			vcpu->arch.regs.ctr);
98de7906c3SAlexander Graf 	printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
99de7906c3SAlexander Graf 					    vcpu->arch.shared->srr1);
100d9fbd03dSHollis Blanchard 
101d9fbd03dSHollis Blanchard 	printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
102d9fbd03dSHollis Blanchard 
103d9fbd03dSHollis Blanchard 	for (i = 0; i < 32; i += 4) {
1045cf8ca22SHollis Blanchard 		printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
1058e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i),
1068e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i+1),
1078e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i+2),
1088e5b26b5SAlexander Graf 		       kvmppc_get_gpr(vcpu, i+3));
109d9fbd03dSHollis Blanchard 	}
110d9fbd03dSHollis Blanchard }
111d9fbd03dSHollis Blanchard 
1124cd35f67SScott Wood #ifdef CONFIG_SPE
kvmppc_vcpu_disable_spe(struct kvm_vcpu * vcpu)1134cd35f67SScott Wood void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
1144cd35f67SScott Wood {
1154cd35f67SScott Wood 	preempt_disable();
1164cd35f67SScott Wood 	enable_kernel_spe();
1174cd35f67SScott Wood 	kvmppc_save_guest_spe(vcpu);
118dc4fbba1SAnton Blanchard 	disable_kernel_spe();
1194cd35f67SScott Wood 	vcpu->arch.shadow_msr &= ~MSR_SPE;
1204cd35f67SScott Wood 	preempt_enable();
1214cd35f67SScott Wood }
1224cd35f67SScott Wood 
kvmppc_vcpu_enable_spe(struct kvm_vcpu * vcpu)1234cd35f67SScott Wood static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
1244cd35f67SScott Wood {
1254cd35f67SScott Wood 	preempt_disable();
1264cd35f67SScott Wood 	enable_kernel_spe();
1274cd35f67SScott Wood 	kvmppc_load_guest_spe(vcpu);
128dc4fbba1SAnton Blanchard 	disable_kernel_spe();
1294cd35f67SScott Wood 	vcpu->arch.shadow_msr |= MSR_SPE;
1304cd35f67SScott Wood 	preempt_enable();
1314cd35f67SScott Wood }
1324cd35f67SScott Wood 
kvmppc_vcpu_sync_spe(struct kvm_vcpu * vcpu)1334cd35f67SScott Wood static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
1344cd35f67SScott Wood {
1354cd35f67SScott Wood 	if (vcpu->arch.shared->msr & MSR_SPE) {
1364cd35f67SScott Wood 		if (!(vcpu->arch.shadow_msr & MSR_SPE))
1374cd35f67SScott Wood 			kvmppc_vcpu_enable_spe(vcpu);
1384cd35f67SScott Wood 	} else if (vcpu->arch.shadow_msr & MSR_SPE) {
1394cd35f67SScott Wood 		kvmppc_vcpu_disable_spe(vcpu);
1404cd35f67SScott Wood 	}
1414cd35f67SScott Wood }
1424cd35f67SScott Wood #else
kvmppc_vcpu_sync_spe(struct kvm_vcpu * vcpu)1434cd35f67SScott Wood static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
1444cd35f67SScott Wood {
1454cd35f67SScott Wood }
1464cd35f67SScott Wood #endif
1474cd35f67SScott Wood 
1483efc7da6SMihai Caraman /*
1493efc7da6SMihai Caraman  * Load up guest vcpu FP state if it's needed.
1503efc7da6SMihai Caraman  * It also set the MSR_FP in thread so that host know
1513efc7da6SMihai Caraman  * we're holding FPU, and then host can help to save
1523efc7da6SMihai Caraman  * guest vcpu FP state if other threads require to use FPU.
1533efc7da6SMihai Caraman  * This simulates an FP unavailable fault.
1543efc7da6SMihai Caraman  *
1553efc7da6SMihai Caraman  * It requires to be called with preemption disabled.
1563efc7da6SMihai Caraman  */
kvmppc_load_guest_fp(struct kvm_vcpu * vcpu)1573efc7da6SMihai Caraman static inline void kvmppc_load_guest_fp(struct kvm_vcpu *vcpu)
1583efc7da6SMihai Caraman {
1593efc7da6SMihai Caraman #ifdef CONFIG_PPC_FPU
1603efc7da6SMihai Caraman 	if (!(current->thread.regs->msr & MSR_FP)) {
1613efc7da6SMihai Caraman 		enable_kernel_fp();
1623efc7da6SMihai Caraman 		load_fp_state(&vcpu->arch.fp);
163dc4fbba1SAnton Blanchard 		disable_kernel_fp();
1643efc7da6SMihai Caraman 		current->thread.fp_save_area = &vcpu->arch.fp;
1653efc7da6SMihai Caraman 		current->thread.regs->msr |= MSR_FP;
1663efc7da6SMihai Caraman 	}
1673efc7da6SMihai Caraman #endif
1683efc7da6SMihai Caraman }
1693efc7da6SMihai Caraman 
1703efc7da6SMihai Caraman /*
1713efc7da6SMihai Caraman  * Save guest vcpu FP state into thread.
1723efc7da6SMihai Caraman  * It requires to be called with preemption disabled.
1733efc7da6SMihai Caraman  */
kvmppc_save_guest_fp(struct kvm_vcpu * vcpu)1743efc7da6SMihai Caraman static inline void kvmppc_save_guest_fp(struct kvm_vcpu *vcpu)
1753efc7da6SMihai Caraman {
1763efc7da6SMihai Caraman #ifdef CONFIG_PPC_FPU
1773efc7da6SMihai Caraman 	if (current->thread.regs->msr & MSR_FP)
1783efc7da6SMihai Caraman 		giveup_fpu(current);
1793efc7da6SMihai Caraman 	current->thread.fp_save_area = NULL;
1803efc7da6SMihai Caraman #endif
1813efc7da6SMihai Caraman }
1823efc7da6SMihai Caraman 
kvmppc_vcpu_sync_fpu(struct kvm_vcpu * vcpu)1837a08c274SAlexander Graf static void kvmppc_vcpu_sync_fpu(struct kvm_vcpu *vcpu)
1847a08c274SAlexander Graf {
1857a08c274SAlexander Graf #if defined(CONFIG_PPC_FPU) && !defined(CONFIG_KVM_BOOKE_HV)
1867a08c274SAlexander Graf 	/* We always treat the FP bit as enabled from the host
1877a08c274SAlexander Graf 	   perspective, so only need to adjust the shadow MSR */
1887a08c274SAlexander Graf 	vcpu->arch.shadow_msr &= ~MSR_FP;
1897a08c274SAlexander Graf 	vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_FP;
1907a08c274SAlexander Graf #endif
1917a08c274SAlexander Graf }
1927a08c274SAlexander Graf 
19395d80a29SMihai Caraman /*
19495d80a29SMihai Caraman  * Simulate AltiVec unavailable fault to load guest state
19595d80a29SMihai Caraman  * from thread to AltiVec unit.
19695d80a29SMihai Caraman  * It requires to be called with preemption disabled.
19795d80a29SMihai Caraman  */
kvmppc_load_guest_altivec(struct kvm_vcpu * vcpu)19895d80a29SMihai Caraman static inline void kvmppc_load_guest_altivec(struct kvm_vcpu *vcpu)
19995d80a29SMihai Caraman {
20095d80a29SMihai Caraman #ifdef CONFIG_ALTIVEC
20195d80a29SMihai Caraman 	if (cpu_has_feature(CPU_FTR_ALTIVEC)) {
20295d80a29SMihai Caraman 		if (!(current->thread.regs->msr & MSR_VEC)) {
20395d80a29SMihai Caraman 			enable_kernel_altivec();
20495d80a29SMihai Caraman 			load_vr_state(&vcpu->arch.vr);
205dc4fbba1SAnton Blanchard 			disable_kernel_altivec();
20695d80a29SMihai Caraman 			current->thread.vr_save_area = &vcpu->arch.vr;
20795d80a29SMihai Caraman 			current->thread.regs->msr |= MSR_VEC;
20895d80a29SMihai Caraman 		}
20995d80a29SMihai Caraman 	}
21095d80a29SMihai Caraman #endif
21195d80a29SMihai Caraman }
21295d80a29SMihai Caraman 
21395d80a29SMihai Caraman /*
21495d80a29SMihai Caraman  * Save guest vcpu AltiVec state into thread.
21595d80a29SMihai Caraman  * It requires to be called with preemption disabled.
21695d80a29SMihai Caraman  */
kvmppc_save_guest_altivec(struct kvm_vcpu * vcpu)21795d80a29SMihai Caraman static inline void kvmppc_save_guest_altivec(struct kvm_vcpu *vcpu)
21895d80a29SMihai Caraman {
21995d80a29SMihai Caraman #ifdef CONFIG_ALTIVEC
22095d80a29SMihai Caraman 	if (cpu_has_feature(CPU_FTR_ALTIVEC)) {
22195d80a29SMihai Caraman 		if (current->thread.regs->msr & MSR_VEC)
22295d80a29SMihai Caraman 			giveup_altivec(current);
22395d80a29SMihai Caraman 		current->thread.vr_save_area = NULL;
22495d80a29SMihai Caraman 	}
22595d80a29SMihai Caraman #endif
22695d80a29SMihai Caraman }
22795d80a29SMihai Caraman 
kvmppc_vcpu_sync_debug(struct kvm_vcpu * vcpu)228ce11e48bSBharat Bhushan static void kvmppc_vcpu_sync_debug(struct kvm_vcpu *vcpu)
229ce11e48bSBharat Bhushan {
230ce11e48bSBharat Bhushan 	/* Synchronize guest's desire to get debug interrupts into shadow MSR */
231ce11e48bSBharat Bhushan #ifndef CONFIG_KVM_BOOKE_HV
232ce11e48bSBharat Bhushan 	vcpu->arch.shadow_msr &= ~MSR_DE;
233ce11e48bSBharat Bhushan 	vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_DE;
234ce11e48bSBharat Bhushan #endif
235ce11e48bSBharat Bhushan 
236ce11e48bSBharat Bhushan 	/* Force enable debug interrupts when user space wants to debug */
237ce11e48bSBharat Bhushan 	if (vcpu->guest_debug) {
238ce11e48bSBharat Bhushan #ifdef CONFIG_KVM_BOOKE_HV
239ce11e48bSBharat Bhushan 		/*
240ce11e48bSBharat Bhushan 		 * Since there is no shadow MSR, sync MSR_DE into the guest
241ce11e48bSBharat Bhushan 		 * visible MSR.
242ce11e48bSBharat Bhushan 		 */
243ce11e48bSBharat Bhushan 		vcpu->arch.shared->msr |= MSR_DE;
244ce11e48bSBharat Bhushan #else
245ce11e48bSBharat Bhushan 		vcpu->arch.shadow_msr |= MSR_DE;
246ce11e48bSBharat Bhushan 		vcpu->arch.shared->msr &= ~MSR_DE;
247ce11e48bSBharat Bhushan #endif
248ce11e48bSBharat Bhushan 	}
249ce11e48bSBharat Bhushan }
250ce11e48bSBharat Bhushan 
251dd9ebf1fSLiu Yu /*
252dd9ebf1fSLiu Yu  * Helper function for "full" MSR writes.  No need to call this if only
253dd9ebf1fSLiu Yu  * EE/CE/ME/DE/RI are changing.
254dd9ebf1fSLiu Yu  */
kvmppc_set_msr(struct kvm_vcpu * vcpu,u32 new_msr)2554cd35f67SScott Wood void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
2564cd35f67SScott Wood {
257dd9ebf1fSLiu Yu 	u32 old_msr = vcpu->arch.shared->msr;
2584cd35f67SScott Wood 
259d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
260d30f6e48SScott Wood 	new_msr |= MSR_GS;
261d30f6e48SScott Wood #endif
262d30f6e48SScott Wood 
2634cd35f67SScott Wood 	vcpu->arch.shared->msr = new_msr;
2644cd35f67SScott Wood 
265dd9ebf1fSLiu Yu 	kvmppc_mmu_msr_notify(vcpu, old_msr);
2664cd35f67SScott Wood 	kvmppc_vcpu_sync_spe(vcpu);
2677a08c274SAlexander Graf 	kvmppc_vcpu_sync_fpu(vcpu);
268ce11e48bSBharat Bhushan 	kvmppc_vcpu_sync_debug(vcpu);
2694cd35f67SScott Wood }
2704cd35f67SScott Wood 
kvmppc_booke_queue_irqprio(struct kvm_vcpu * vcpu,unsigned int priority)271d4cf3892SHollis Blanchard static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
272d4cf3892SHollis Blanchard                                        unsigned int priority)
2739dd921cfSHollis Blanchard {
2746346046cSAlexander Graf 	trace_kvm_booke_queue_irqprio(vcpu, priority);
2759dd921cfSHollis Blanchard 	set_bit(priority, &vcpu->arch.pending_exceptions);
2769dd921cfSHollis Blanchard }
2779dd921cfSHollis Blanchard 
kvmppc_core_queue_dtlb_miss(struct kvm_vcpu * vcpu,ulong dear_flags,ulong esr_flags)2788de12015SAlexander Graf void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
279daf5e271SLiu Yu 				 ulong dear_flags, ulong esr_flags)
2809dd921cfSHollis Blanchard {
281daf5e271SLiu Yu 	vcpu->arch.queued_dear = dear_flags;
282daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
283daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
284daf5e271SLiu Yu }
285daf5e271SLiu Yu 
kvmppc_core_queue_data_storage(struct kvm_vcpu * vcpu,ulong srr1_flags,ulong dear_flags,ulong esr_flags)286460ba21dSNicholas Piggin void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu, ulong srr1_flags,
287daf5e271SLiu Yu 				    ulong dear_flags, ulong esr_flags)
288daf5e271SLiu Yu {
289460ba21dSNicholas Piggin 	WARN_ON_ONCE(srr1_flags);
290daf5e271SLiu Yu 	vcpu->arch.queued_dear = dear_flags;
291daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
292daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
293daf5e271SLiu Yu }
294daf5e271SLiu Yu 
kvmppc_core_queue_itlb_miss(struct kvm_vcpu * vcpu)2958de12015SAlexander Graf void kvmppc_core_queue_itlb_miss(struct kvm_vcpu *vcpu)
2968de12015SAlexander Graf {
2978de12015SAlexander Graf 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
2988de12015SAlexander Graf }
2998de12015SAlexander Graf 
kvmppc_core_queue_inst_storage(struct kvm_vcpu * vcpu,ulong esr_flags)3008de12015SAlexander Graf void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu, ulong esr_flags)
301daf5e271SLiu Yu {
302daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
303daf5e271SLiu Yu 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
304daf5e271SLiu Yu }
305daf5e271SLiu Yu 
kvmppc_core_queue_alignment(struct kvm_vcpu * vcpu,ulong dear_flags,ulong esr_flags)306011da899SAlexander Graf static void kvmppc_core_queue_alignment(struct kvm_vcpu *vcpu, ulong dear_flags,
307011da899SAlexander Graf 					ulong esr_flags)
308011da899SAlexander Graf {
309011da899SAlexander Graf 	vcpu->arch.queued_dear = dear_flags;
310011da899SAlexander Graf 	vcpu->arch.queued_esr = esr_flags;
311011da899SAlexander Graf 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALIGNMENT);
312011da899SAlexander Graf }
313011da899SAlexander Graf 
kvmppc_core_queue_program(struct kvm_vcpu * vcpu,ulong esr_flags)314daf5e271SLiu Yu void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
315daf5e271SLiu Yu {
316daf5e271SLiu Yu 	vcpu->arch.queued_esr = esr_flags;
317d4cf3892SHollis Blanchard 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
3189dd921cfSHollis Blanchard }
3199dd921cfSHollis Blanchard 
kvmppc_core_queue_fpunavail(struct kvm_vcpu * vcpu,ulong srr1_flags)320460ba21dSNicholas Piggin void kvmppc_core_queue_fpunavail(struct kvm_vcpu *vcpu, ulong srr1_flags)
321307d9279SPaul Mackerras {
322460ba21dSNicholas Piggin 	WARN_ON_ONCE(srr1_flags);
323307d9279SPaul Mackerras 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
324307d9279SPaul Mackerras }
325307d9279SPaul Mackerras 
326b2d7ecbeSLaurentiu Tudor #ifdef CONFIG_ALTIVEC
kvmppc_core_queue_vec_unavail(struct kvm_vcpu * vcpu,ulong srr1_flags)327460ba21dSNicholas Piggin void kvmppc_core_queue_vec_unavail(struct kvm_vcpu *vcpu, ulong srr1_flags)
328b2d7ecbeSLaurentiu Tudor {
329460ba21dSNicholas Piggin 	WARN_ON_ONCE(srr1_flags);
330b2d7ecbeSLaurentiu Tudor 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALTIVEC_UNAVAIL);
331b2d7ecbeSLaurentiu Tudor }
332b2d7ecbeSLaurentiu Tudor #endif
333b2d7ecbeSLaurentiu Tudor 
kvmppc_core_queue_dec(struct kvm_vcpu * vcpu)3349dd921cfSHollis Blanchard void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
3359dd921cfSHollis Blanchard {
336d4cf3892SHollis Blanchard 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
3379dd921cfSHollis Blanchard }
3389dd921cfSHollis Blanchard 
kvmppc_core_pending_dec(struct kvm_vcpu * vcpu)3399dd921cfSHollis Blanchard int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
3409dd921cfSHollis Blanchard {
341d4cf3892SHollis Blanchard 	return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
3429dd921cfSHollis Blanchard }
3439dd921cfSHollis Blanchard 
kvmppc_core_dequeue_dec(struct kvm_vcpu * vcpu)3447706664dSAlexander Graf void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
3457706664dSAlexander Graf {
3467706664dSAlexander Graf 	clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
3477706664dSAlexander Graf }
3487706664dSAlexander Graf 
kvmppc_core_queue_external(struct kvm_vcpu * vcpu,struct kvm_interrupt * irq)3499dd921cfSHollis Blanchard void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
3509dd921cfSHollis Blanchard                                 struct kvm_interrupt *irq)
3519dd921cfSHollis Blanchard {
352c5335f17SAlexander Graf 	unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
353c5335f17SAlexander Graf 
354c5335f17SAlexander Graf 	if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
355c5335f17SAlexander Graf 		prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
356c5335f17SAlexander Graf 
357c5335f17SAlexander Graf 	kvmppc_booke_queue_irqprio(vcpu, prio);
3589dd921cfSHollis Blanchard }
3599dd921cfSHollis Blanchard 
kvmppc_core_dequeue_external(struct kvm_vcpu * vcpu)3604fe27d2aSPaul Mackerras void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu)
3614496f974SAlexander Graf {
3624496f974SAlexander Graf 	clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
363c5335f17SAlexander Graf 	clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
3644496f974SAlexander Graf }
3654496f974SAlexander Graf 
kvmppc_core_queue_watchdog(struct kvm_vcpu * vcpu)366f61c94bbSBharat Bhushan static void kvmppc_core_queue_watchdog(struct kvm_vcpu *vcpu)
367f61c94bbSBharat Bhushan {
368f61c94bbSBharat Bhushan 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_WATCHDOG);
369f61c94bbSBharat Bhushan }
370f61c94bbSBharat Bhushan 
kvmppc_core_dequeue_watchdog(struct kvm_vcpu * vcpu)371f61c94bbSBharat Bhushan static void kvmppc_core_dequeue_watchdog(struct kvm_vcpu *vcpu)
372f61c94bbSBharat Bhushan {
373f61c94bbSBharat Bhushan 	clear_bit(BOOKE_IRQPRIO_WATCHDOG, &vcpu->arch.pending_exceptions);
374f61c94bbSBharat Bhushan }
375f61c94bbSBharat Bhushan 
kvmppc_core_queue_debug(struct kvm_vcpu * vcpu)3762f699a59SBharat Bhushan void kvmppc_core_queue_debug(struct kvm_vcpu *vcpu)
3772f699a59SBharat Bhushan {
3782f699a59SBharat Bhushan 	kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DEBUG);
3792f699a59SBharat Bhushan }
3802f699a59SBharat Bhushan 
kvmppc_core_dequeue_debug(struct kvm_vcpu * vcpu)3812f699a59SBharat Bhushan void kvmppc_core_dequeue_debug(struct kvm_vcpu *vcpu)
3822f699a59SBharat Bhushan {
3832f699a59SBharat Bhushan 	clear_bit(BOOKE_IRQPRIO_DEBUG, &vcpu->arch.pending_exceptions);
3842f699a59SBharat Bhushan }
3852f699a59SBharat Bhushan 
set_guest_srr(struct kvm_vcpu * vcpu,unsigned long srr0,u32 srr1)386d30f6e48SScott Wood static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
387d30f6e48SScott Wood {
38831579eeaSBharat Bhushan 	kvmppc_set_srr0(vcpu, srr0);
38931579eeaSBharat Bhushan 	kvmppc_set_srr1(vcpu, srr1);
390d30f6e48SScott Wood }
391d30f6e48SScott Wood 
set_guest_csrr(struct kvm_vcpu * vcpu,unsigned long srr0,u32 srr1)392d30f6e48SScott Wood static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
393d30f6e48SScott Wood {
394d30f6e48SScott Wood 	vcpu->arch.csrr0 = srr0;
395d30f6e48SScott Wood 	vcpu->arch.csrr1 = srr1;
396d30f6e48SScott Wood }
397d30f6e48SScott Wood 
set_guest_dsrr(struct kvm_vcpu * vcpu,unsigned long srr0,u32 srr1)398d30f6e48SScott Wood static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
399d30f6e48SScott Wood {
400d30f6e48SScott Wood 	if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
401d30f6e48SScott Wood 		vcpu->arch.dsrr0 = srr0;
402d30f6e48SScott Wood 		vcpu->arch.dsrr1 = srr1;
403d30f6e48SScott Wood 	} else {
404d30f6e48SScott Wood 		set_guest_csrr(vcpu, srr0, srr1);
405d30f6e48SScott Wood 	}
406d30f6e48SScott Wood }
407d30f6e48SScott Wood 
set_guest_mcsrr(struct kvm_vcpu * vcpu,unsigned long srr0,u32 srr1)408d30f6e48SScott Wood static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
409d30f6e48SScott Wood {
410d30f6e48SScott Wood 	vcpu->arch.mcsrr0 = srr0;
411d30f6e48SScott Wood 	vcpu->arch.mcsrr1 = srr1;
412d30f6e48SScott Wood }
413d30f6e48SScott Wood 
414d4cf3892SHollis Blanchard /* Deliver the interrupt of the corresponding priority, if possible. */
kvmppc_booke_irqprio_deliver(struct kvm_vcpu * vcpu,unsigned int priority)415d4cf3892SHollis Blanchard static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
416d4cf3892SHollis Blanchard                                         unsigned int priority)
417d9fbd03dSHollis Blanchard {
418d4cf3892SHollis Blanchard 	int allowed = 0;
41979300f8cSAlexander Graf 	ulong msr_mask = 0;
4201c810636SAlexander Graf 	bool update_esr = false, update_dear = false, update_epr = false;
4215c6cedf4SAlexander Graf 	ulong crit_raw = vcpu->arch.shared->critical;
4225c6cedf4SAlexander Graf 	ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
4235c6cedf4SAlexander Graf 	bool crit;
424c5335f17SAlexander Graf 	bool keep_irq = false;
425d30f6e48SScott Wood 	enum int_class int_class;
42695e90b43SMihai Caraman 	ulong new_msr = vcpu->arch.shared->msr;
4275c6cedf4SAlexander Graf 
4285c6cedf4SAlexander Graf 	/* Truncate crit indicators in 32 bit mode */
4295c6cedf4SAlexander Graf 	if (!(vcpu->arch.shared->msr & MSR_SF)) {
4305c6cedf4SAlexander Graf 		crit_raw &= 0xffffffff;
4315c6cedf4SAlexander Graf 		crit_r1 &= 0xffffffff;
4325c6cedf4SAlexander Graf 	}
4335c6cedf4SAlexander Graf 
4345c6cedf4SAlexander Graf 	/* Critical section when crit == r1 */
4355c6cedf4SAlexander Graf 	crit = (crit_raw == crit_r1);
4365c6cedf4SAlexander Graf 	/* ... and we're in supervisor mode */
4375c6cedf4SAlexander Graf 	crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
438d9fbd03dSHollis Blanchard 
439c5335f17SAlexander Graf 	if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
440c5335f17SAlexander Graf 		priority = BOOKE_IRQPRIO_EXTERNAL;
441c5335f17SAlexander Graf 		keep_irq = true;
442c5335f17SAlexander Graf 	}
443c5335f17SAlexander Graf 
4445df554adSScott Wood 	if ((priority == BOOKE_IRQPRIO_EXTERNAL) && vcpu->arch.epr_flags)
4451c810636SAlexander Graf 		update_epr = true;
4461c810636SAlexander Graf 
447d4cf3892SHollis Blanchard 	switch (priority) {
448d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DTLB_MISS:
449daf5e271SLiu Yu 	case BOOKE_IRQPRIO_DATA_STORAGE:
450011da899SAlexander Graf 	case BOOKE_IRQPRIO_ALIGNMENT:
451daf5e271SLiu Yu 		update_dear = true;
4528fc6ba0aSJoe Perches 		fallthrough;
453daf5e271SLiu Yu 	case BOOKE_IRQPRIO_INST_STORAGE:
454daf5e271SLiu Yu 	case BOOKE_IRQPRIO_PROGRAM:
455daf5e271SLiu Yu 		update_esr = true;
4568fc6ba0aSJoe Perches 		fallthrough;
457d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_ITLB_MISS:
458d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_SYSCALL:
459d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_FP_UNAVAIL:
46095d80a29SMihai Caraman #ifdef CONFIG_SPE_POSSIBLE
461bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_UNAVAIL:
462bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_FP_DATA:
463bb3a8a17SHollis Blanchard 	case BOOKE_IRQPRIO_SPE_FP_ROUND:
46495d80a29SMihai Caraman #endif
46595d80a29SMihai Caraman #ifdef CONFIG_ALTIVEC
46695d80a29SMihai Caraman 	case BOOKE_IRQPRIO_ALTIVEC_UNAVAIL:
46795d80a29SMihai Caraman 	case BOOKE_IRQPRIO_ALTIVEC_ASSIST:
46895d80a29SMihai Caraman #endif
469d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_AP_UNAVAIL:
470d4cf3892SHollis Blanchard 		allowed = 1;
47179300f8cSAlexander Graf 		msr_mask = MSR_CE | MSR_ME | MSR_DE;
472d30f6e48SScott Wood 		int_class = INT_CLASS_NONCRIT;
473d9fbd03dSHollis Blanchard 		break;
474f61c94bbSBharat Bhushan 	case BOOKE_IRQPRIO_WATCHDOG:
475d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_CRITICAL:
4764ab96919SAlexander Graf 	case BOOKE_IRQPRIO_DBELL_CRIT:
477666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_CE;
478d30f6e48SScott Wood 		allowed = allowed && !crit;
47979300f8cSAlexander Graf 		msr_mask = MSR_ME;
480d30f6e48SScott Wood 		int_class = INT_CLASS_CRIT;
481d9fbd03dSHollis Blanchard 		break;
482d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_MACHINE_CHECK:
483666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_ME;
484d30f6e48SScott Wood 		allowed = allowed && !crit;
485d30f6e48SScott Wood 		int_class = INT_CLASS_MC;
486d9fbd03dSHollis Blanchard 		break;
487d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DECREMENTER:
488d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_FIT:
489dfd4d47eSScott Wood 		keep_irq = true;
4908fc6ba0aSJoe Perches 		fallthrough;
491dfd4d47eSScott Wood 	case BOOKE_IRQPRIO_EXTERNAL:
4924ab96919SAlexander Graf 	case BOOKE_IRQPRIO_DBELL:
493666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_EE;
4945c6cedf4SAlexander Graf 		allowed = allowed && !crit;
49579300f8cSAlexander Graf 		msr_mask = MSR_CE | MSR_ME | MSR_DE;
496d30f6e48SScott Wood 		int_class = INT_CLASS_NONCRIT;
497d9fbd03dSHollis Blanchard 		break;
498d4cf3892SHollis Blanchard 	case BOOKE_IRQPRIO_DEBUG:
499666e7252SAlexander Graf 		allowed = vcpu->arch.shared->msr & MSR_DE;
500d30f6e48SScott Wood 		allowed = allowed && !crit;
50179300f8cSAlexander Graf 		msr_mask = MSR_ME;
5029fee7563SBharat Bhushan 		if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC))
5039fee7563SBharat Bhushan 			int_class = INT_CLASS_DBG;
5049fee7563SBharat Bhushan 		else
505d30f6e48SScott Wood 			int_class = INT_CLASS_CRIT;
5069fee7563SBharat Bhushan 
507d9fbd03dSHollis Blanchard 		break;
508d9fbd03dSHollis Blanchard 	}
509d9fbd03dSHollis Blanchard 
510d4cf3892SHollis Blanchard 	if (allowed) {
511d30f6e48SScott Wood 		switch (int_class) {
512d30f6e48SScott Wood 		case INT_CLASS_NONCRIT:
513173c520aSSimon Guo 			set_guest_srr(vcpu, vcpu->arch.regs.nip,
514d30f6e48SScott Wood 				      vcpu->arch.shared->msr);
515d30f6e48SScott Wood 			break;
516d30f6e48SScott Wood 		case INT_CLASS_CRIT:
517173c520aSSimon Guo 			set_guest_csrr(vcpu, vcpu->arch.regs.nip,
518d30f6e48SScott Wood 				       vcpu->arch.shared->msr);
519d30f6e48SScott Wood 			break;
520d30f6e48SScott Wood 		case INT_CLASS_DBG:
521173c520aSSimon Guo 			set_guest_dsrr(vcpu, vcpu->arch.regs.nip,
522d30f6e48SScott Wood 				       vcpu->arch.shared->msr);
523d30f6e48SScott Wood 			break;
524d30f6e48SScott Wood 		case INT_CLASS_MC:
525173c520aSSimon Guo 			set_guest_mcsrr(vcpu, vcpu->arch.regs.nip,
526d30f6e48SScott Wood 					vcpu->arch.shared->msr);
527d30f6e48SScott Wood 			break;
528d30f6e48SScott Wood 		}
529d30f6e48SScott Wood 
530173c520aSSimon Guo 		vcpu->arch.regs.nip = vcpu->arch.ivpr |
531173c520aSSimon Guo 					vcpu->arch.ivor[priority];
532a300bf8cSKaixu Xia 		if (update_esr)
533dc168549SBharat Bhushan 			kvmppc_set_esr(vcpu, vcpu->arch.queued_esr);
534a300bf8cSKaixu Xia 		if (update_dear)
535a5414d4bSBharat Bhushan 			kvmppc_set_dar(vcpu, vcpu->arch.queued_dear);
536a300bf8cSKaixu Xia 		if (update_epr) {
5375df554adSScott Wood 			if (vcpu->arch.epr_flags & KVMPPC_EPR_USER)
5381c810636SAlexander Graf 				kvm_make_request(KVM_REQ_EPR_EXIT, vcpu);
539eb1e4f43SScott Wood 			else if (vcpu->arch.epr_flags & KVMPPC_EPR_KERNEL) {
540eb1e4f43SScott Wood 				BUG_ON(vcpu->arch.irq_type != KVMPPC_IRQ_MPIC);
541eb1e4f43SScott Wood 				kvmppc_mpic_set_epr(vcpu);
542eb1e4f43SScott Wood 			}
5435df554adSScott Wood 		}
54495e90b43SMihai Caraman 
54595e90b43SMihai Caraman 		new_msr &= msr_mask;
54695e90b43SMihai Caraman #if defined(CONFIG_64BIT)
54795e90b43SMihai Caraman 		if (vcpu->arch.epcr & SPRN_EPCR_ICM)
54895e90b43SMihai Caraman 			new_msr |= MSR_CM;
54995e90b43SMihai Caraman #endif
55095e90b43SMihai Caraman 		kvmppc_set_msr(vcpu, new_msr);
551d4cf3892SHollis Blanchard 
552c5335f17SAlexander Graf 		if (!keep_irq)
553d4cf3892SHollis Blanchard 			clear_bit(priority, &vcpu->arch.pending_exceptions);
554d4cf3892SHollis Blanchard 	}
555d4cf3892SHollis Blanchard 
556d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
557d30f6e48SScott Wood 	/*
558d30f6e48SScott Wood 	 * If an interrupt is pending but masked, raise a guest doorbell
559d30f6e48SScott Wood 	 * so that we are notified when the guest enables the relevant
560d30f6e48SScott Wood 	 * MSR bit.
561d30f6e48SScott Wood 	 */
562d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
563d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
564d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
565d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
566d30f6e48SScott Wood 	if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
567d30f6e48SScott Wood 		kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
568d30f6e48SScott Wood #endif
569d30f6e48SScott Wood 
570d4cf3892SHollis Blanchard 	return allowed;
571d9fbd03dSHollis Blanchard }
572d9fbd03dSHollis Blanchard 
573f61c94bbSBharat Bhushan /*
574f61c94bbSBharat Bhushan  * Return the number of jiffies until the next timeout.  If the timeout is
575f61c94bbSBharat Bhushan  * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA
576f61c94bbSBharat Bhushan  * because the larger value can break the timer APIs.
577f61c94bbSBharat Bhushan  */
watchdog_next_timeout(struct kvm_vcpu * vcpu)578f61c94bbSBharat Bhushan static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu)
579f61c94bbSBharat Bhushan {
580f61c94bbSBharat Bhushan 	u64 tb, wdt_tb, wdt_ticks = 0;
581f61c94bbSBharat Bhushan 	u64 nr_jiffies = 0;
582f61c94bbSBharat Bhushan 	u32 period = TCR_GET_WP(vcpu->arch.tcr);
583f61c94bbSBharat Bhushan 
584f61c94bbSBharat Bhushan 	wdt_tb = 1ULL << (63 - period);
585f61c94bbSBharat Bhushan 	tb = get_tb();
586f61c94bbSBharat Bhushan 	/*
587f61c94bbSBharat Bhushan 	 * The watchdog timeout will hapeen when TB bit corresponding
588f61c94bbSBharat Bhushan 	 * to watchdog will toggle from 0 to 1.
589f61c94bbSBharat Bhushan 	 */
590f61c94bbSBharat Bhushan 	if (tb & wdt_tb)
591f61c94bbSBharat Bhushan 		wdt_ticks = wdt_tb;
592f61c94bbSBharat Bhushan 
593f61c94bbSBharat Bhushan 	wdt_ticks += wdt_tb - (tb & (wdt_tb - 1));
594f61c94bbSBharat Bhushan 
595f61c94bbSBharat Bhushan 	/* Convert timebase ticks to jiffies */
596f61c94bbSBharat Bhushan 	nr_jiffies = wdt_ticks;
597f61c94bbSBharat Bhushan 
598f61c94bbSBharat Bhushan 	if (do_div(nr_jiffies, tb_ticks_per_jiffy))
599f61c94bbSBharat Bhushan 		nr_jiffies++;
600f61c94bbSBharat Bhushan 
601f61c94bbSBharat Bhushan 	return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA);
602f61c94bbSBharat Bhushan }
603f61c94bbSBharat Bhushan 
arm_next_watchdog(struct kvm_vcpu * vcpu)604f61c94bbSBharat Bhushan static void arm_next_watchdog(struct kvm_vcpu *vcpu)
605f61c94bbSBharat Bhushan {
606f61c94bbSBharat Bhushan 	unsigned long nr_jiffies;
607f61c94bbSBharat Bhushan 	unsigned long flags;
608f61c94bbSBharat Bhushan 
609f61c94bbSBharat Bhushan 	/*
610f61c94bbSBharat Bhushan 	 * If TSR_ENW and TSR_WIS are not set then no need to exit to
611f61c94bbSBharat Bhushan 	 * userspace, so clear the KVM_REQ_WATCHDOG request.
612f61c94bbSBharat Bhushan 	 */
613f61c94bbSBharat Bhushan 	if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS))
61472875d8aSRadim Krčmář 		kvm_clear_request(KVM_REQ_WATCHDOG, vcpu);
615f61c94bbSBharat Bhushan 
616f61c94bbSBharat Bhushan 	spin_lock_irqsave(&vcpu->arch.wdt_lock, flags);
617f61c94bbSBharat Bhushan 	nr_jiffies = watchdog_next_timeout(vcpu);
618f61c94bbSBharat Bhushan 	/*
619f61c94bbSBharat Bhushan 	 * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA
620f61c94bbSBharat Bhushan 	 * then do not run the watchdog timer as this can break timer APIs.
621f61c94bbSBharat Bhushan 	 */
622f61c94bbSBharat Bhushan 	if (nr_jiffies < NEXT_TIMER_MAX_DELTA)
623f61c94bbSBharat Bhushan 		mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies);
624f61c94bbSBharat Bhushan 	else
625f61c94bbSBharat Bhushan 		del_timer(&vcpu->arch.wdt_timer);
626f61c94bbSBharat Bhushan 	spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags);
627f61c94bbSBharat Bhushan }
628f61c94bbSBharat Bhushan 
kvmppc_watchdog_func(struct timer_list * t)629e83ca8cfSSean Christopherson static void kvmppc_watchdog_func(struct timer_list *t)
630f61c94bbSBharat Bhushan {
63186cb30ecSKees Cook 	struct kvm_vcpu *vcpu = from_timer(vcpu, t, arch.wdt_timer);
632f61c94bbSBharat Bhushan 	u32 tsr, new_tsr;
633f61c94bbSBharat Bhushan 	int final;
634f61c94bbSBharat Bhushan 
635f61c94bbSBharat Bhushan 	do {
636f61c94bbSBharat Bhushan 		new_tsr = tsr = vcpu->arch.tsr;
637f61c94bbSBharat Bhushan 		final = 0;
638f61c94bbSBharat Bhushan 
639f61c94bbSBharat Bhushan 		/* Time out event */
640f61c94bbSBharat Bhushan 		if (tsr & TSR_ENW) {
641f61c94bbSBharat Bhushan 			if (tsr & TSR_WIS)
642f61c94bbSBharat Bhushan 				final = 1;
643f61c94bbSBharat Bhushan 			else
644f61c94bbSBharat Bhushan 				new_tsr = tsr | TSR_WIS;
645f61c94bbSBharat Bhushan 		} else {
646f61c94bbSBharat Bhushan 			new_tsr = tsr | TSR_ENW;
647f61c94bbSBharat Bhushan 		}
648f61c94bbSBharat Bhushan 	} while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr);
649f61c94bbSBharat Bhushan 
650f61c94bbSBharat Bhushan 	if (new_tsr & TSR_WIS) {
651f61c94bbSBharat Bhushan 		smp_wmb();
652f61c94bbSBharat Bhushan 		kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
653f61c94bbSBharat Bhushan 		kvm_vcpu_kick(vcpu);
654f61c94bbSBharat Bhushan 	}
655f61c94bbSBharat Bhushan 
656f61c94bbSBharat Bhushan 	/*
657f61c94bbSBharat Bhushan 	 * If this is final watchdog expiry and some action is required
658f61c94bbSBharat Bhushan 	 * then exit to userspace.
659f61c94bbSBharat Bhushan 	 */
660f61c94bbSBharat Bhushan 	if (final && (vcpu->arch.tcr & TCR_WRC_MASK) &&
661f61c94bbSBharat Bhushan 	    vcpu->arch.watchdog_enabled) {
662f61c94bbSBharat Bhushan 		smp_wmb();
663f61c94bbSBharat Bhushan 		kvm_make_request(KVM_REQ_WATCHDOG, vcpu);
664f61c94bbSBharat Bhushan 		kvm_vcpu_kick(vcpu);
665f61c94bbSBharat Bhushan 	}
666f61c94bbSBharat Bhushan 
667f61c94bbSBharat Bhushan 	/*
668f61c94bbSBharat Bhushan 	 * Stop running the watchdog timer after final expiration to
669f61c94bbSBharat Bhushan 	 * prevent the host from being flooded with timers if the
670f61c94bbSBharat Bhushan 	 * guest sets a short period.
671f61c94bbSBharat Bhushan 	 * Timers will resume when TSR/TCR is updated next time.
672f61c94bbSBharat Bhushan 	 */
673f61c94bbSBharat Bhushan 	if (!final)
674f61c94bbSBharat Bhushan 		arm_next_watchdog(vcpu);
675f61c94bbSBharat Bhushan }
676f61c94bbSBharat Bhushan 
update_timer_ints(struct kvm_vcpu * vcpu)677dfd4d47eSScott Wood static void update_timer_ints(struct kvm_vcpu *vcpu)
678dfd4d47eSScott Wood {
679dfd4d47eSScott Wood 	if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
680dfd4d47eSScott Wood 		kvmppc_core_queue_dec(vcpu);
681dfd4d47eSScott Wood 	else
682dfd4d47eSScott Wood 		kvmppc_core_dequeue_dec(vcpu);
683f61c94bbSBharat Bhushan 
684f61c94bbSBharat Bhushan 	if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS))
685f61c94bbSBharat Bhushan 		kvmppc_core_queue_watchdog(vcpu);
686f61c94bbSBharat Bhushan 	else
687f61c94bbSBharat Bhushan 		kvmppc_core_dequeue_watchdog(vcpu);
688dfd4d47eSScott Wood }
689dfd4d47eSScott Wood 
kvmppc_core_check_exceptions(struct kvm_vcpu * vcpu)690c59a6a3eSScott Wood static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
691d9fbd03dSHollis Blanchard {
692d9fbd03dSHollis Blanchard 	unsigned long *pending = &vcpu->arch.pending_exceptions;
693d9fbd03dSHollis Blanchard 	unsigned int priority;
694d9fbd03dSHollis Blanchard 
6959ab80843SHollis Blanchard 	priority = __ffs(*pending);
6968b3a00fcSAlexander Graf 	while (priority < BOOKE_IRQPRIO_MAX) {
697d4cf3892SHollis Blanchard 		if (kvmppc_booke_irqprio_deliver(vcpu, priority))
698d9fbd03dSHollis Blanchard 			break;
699d9fbd03dSHollis Blanchard 
700d9fbd03dSHollis Blanchard 		priority = find_next_bit(pending,
701d9fbd03dSHollis Blanchard 		                         BITS_PER_BYTE * sizeof(*pending),
702d9fbd03dSHollis Blanchard 		                         priority + 1);
703d9fbd03dSHollis Blanchard 	}
70490bba358SAlexander Graf 
70590bba358SAlexander Graf 	/* Tell the guest about our interrupt status */
70629ac26efSScott Wood 	vcpu->arch.shared->int_pending = !!*pending;
707d9fbd03dSHollis Blanchard }
708d9fbd03dSHollis Blanchard 
709c59a6a3eSScott Wood /* Check pending exceptions and deliver one, if possible. */
kvmppc_core_prepare_to_enter(struct kvm_vcpu * vcpu)710a8e4ef84SAlexander Graf int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
711c59a6a3eSScott Wood {
712a8e4ef84SAlexander Graf 	int r = 0;
713c59a6a3eSScott Wood 	WARN_ON_ONCE(!irqs_disabled());
714c59a6a3eSScott Wood 
715c59a6a3eSScott Wood 	kvmppc_core_check_exceptions(vcpu);
716c59a6a3eSScott Wood 
7172fa6e1e1SRadim Krčmář 	if (kvm_request_pending(vcpu)) {
718b8c649a9SAlexander Graf 		/* Exception delivery raised request; start over */
719b8c649a9SAlexander Graf 		return 1;
720b8c649a9SAlexander Graf 	}
721b8c649a9SAlexander Graf 
722c59a6a3eSScott Wood 	if (vcpu->arch.shared->msr & MSR_WE) {
723c59a6a3eSScott Wood 		local_irq_enable();
72491b99ea7SSean Christopherson 		kvm_vcpu_halt(vcpu);
7256c85f52bSScott Wood 		hard_irq_disable();
726c59a6a3eSScott Wood 
727c59a6a3eSScott Wood 		kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
728a8e4ef84SAlexander Graf 		r = 1;
72963e9f235SYang Li 	}
730a8e4ef84SAlexander Graf 
731a8e4ef84SAlexander Graf 	return r;
732a8e4ef84SAlexander Graf }
733a8e4ef84SAlexander Graf 
kvmppc_core_check_requests(struct kvm_vcpu * vcpu)7347c973a2eSAlexander Graf int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
7354ffc6356SAlexander Graf {
7367c973a2eSAlexander Graf 	int r = 1; /* Indicate we want to get back into the guest */
7377c973a2eSAlexander Graf 
7384ffc6356SAlexander Graf 	if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
7394ffc6356SAlexander Graf 		update_timer_ints(vcpu);
740862d31f7SAlexander Graf #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
741862d31f7SAlexander Graf 	if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
742862d31f7SAlexander Graf 		kvmppc_core_flush_tlb(vcpu);
743862d31f7SAlexander Graf #endif
7447c973a2eSAlexander Graf 
745f61c94bbSBharat Bhushan 	if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) {
746f61c94bbSBharat Bhushan 		vcpu->run->exit_reason = KVM_EXIT_WATCHDOG;
747f61c94bbSBharat Bhushan 		r = 0;
748f61c94bbSBharat Bhushan 	}
749f61c94bbSBharat Bhushan 
7501c810636SAlexander Graf 	if (kvm_check_request(KVM_REQ_EPR_EXIT, vcpu)) {
7511c810636SAlexander Graf 		vcpu->run->epr.epr = 0;
7521c810636SAlexander Graf 		vcpu->arch.epr_needed = true;
7531c810636SAlexander Graf 		vcpu->run->exit_reason = KVM_EXIT_EPR;
7541c810636SAlexander Graf 		r = 0;
7551c810636SAlexander Graf 	}
7561c810636SAlexander Graf 
7577c973a2eSAlexander Graf 	return r;
7584ffc6356SAlexander Graf }
7594ffc6356SAlexander Graf 
kvmppc_vcpu_run(struct kvm_vcpu * vcpu)7608c99d345STianjia Zhang int kvmppc_vcpu_run(struct kvm_vcpu *vcpu)
761df6909e5SPaul Mackerras {
7627ee78855SAlexander Graf 	int ret, s;
763f5f97210SScott Wood 	struct debug_reg debug;
764df6909e5SPaul Mackerras 
765af8f38b3SAlexander Graf 	if (!vcpu->arch.sane) {
7667ec21d9dSTianjia Zhang 		vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
767af8f38b3SAlexander Graf 		return -EINVAL;
768af8f38b3SAlexander Graf 	}
769af8f38b3SAlexander Graf 
7707ee78855SAlexander Graf 	s = kvmppc_prepare_to_enter(vcpu);
7717ee78855SAlexander Graf 	if (s <= 0) {
7727ee78855SAlexander Graf 		ret = s;
7731d1ef222SScott Wood 		goto out;
7741d1ef222SScott Wood 	}
7756c85f52bSScott Wood 	/* interrupts now hard-disabled */
7761d1ef222SScott Wood 
7778fae845fSScott Wood #ifdef CONFIG_PPC_FPU
7788fae845fSScott Wood 	/* Save userspace FPU state in stack */
7798fae845fSScott Wood 	enable_kernel_fp();
7808fae845fSScott Wood 
7818fae845fSScott Wood 	/*
7828fae845fSScott Wood 	 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
7833efc7da6SMihai Caraman 	 * as always using the FPU.
7848fae845fSScott Wood 	 */
7858fae845fSScott Wood 	kvmppc_load_guest_fp(vcpu);
7868fae845fSScott Wood #endif
7878fae845fSScott Wood 
78895d80a29SMihai Caraman #ifdef CONFIG_ALTIVEC
78995d80a29SMihai Caraman 	/* Save userspace AltiVec state in stack */
79095d80a29SMihai Caraman 	if (cpu_has_feature(CPU_FTR_ALTIVEC))
79195d80a29SMihai Caraman 		enable_kernel_altivec();
79295d80a29SMihai Caraman 	/*
79395d80a29SMihai Caraman 	 * Since we can't trap on MSR_VEC in GS-mode, we consider the guest
79495d80a29SMihai Caraman 	 * as always using the AltiVec.
79595d80a29SMihai Caraman 	 */
79695d80a29SMihai Caraman 	kvmppc_load_guest_altivec(vcpu);
79795d80a29SMihai Caraman #endif
79895d80a29SMihai Caraman 
799ce11e48bSBharat Bhushan 	/* Switch to guest debug context */
800348ba710SBharat Bhushan 	debug = vcpu->arch.dbg_reg;
801f5f97210SScott Wood 	switch_booke_debug_regs(&debug);
802f5f97210SScott Wood 	debug = current->thread.debug;
803348ba710SBharat Bhushan 	current->thread.debug = vcpu->arch.dbg_reg;
804ce11e48bSBharat Bhushan 
805e1bd0a7eSLeonardo Bras 	vcpu->arch.pgdir = vcpu->kvm->mm->pgd;
8065f1c248fSScott Wood 	kvmppc_fix_ee_before_entry();
807f8941fbeSScott Wood 
8087ec21d9dSTianjia Zhang 	ret = __kvmppc_vcpu_run(vcpu);
8098fae845fSScott Wood 
8106edaa530SPaolo Bonzini 	/* No need for guest_exit. It's done in handle_exit.
81124afa37bSAlexander Graf 	   We also get here with interrupts enabled. */
81224afa37bSAlexander Graf 
813ce11e48bSBharat Bhushan 	/* Switch back to user space debug context */
814f5f97210SScott Wood 	switch_booke_debug_regs(&debug);
815f5f97210SScott Wood 	current->thread.debug = debug;
816ce11e48bSBharat Bhushan 
8178fae845fSScott Wood #ifdef CONFIG_PPC_FPU
8188fae845fSScott Wood 	kvmppc_save_guest_fp(vcpu);
8198fae845fSScott Wood #endif
8208fae845fSScott Wood 
82195d80a29SMihai Caraman #ifdef CONFIG_ALTIVEC
82295d80a29SMihai Caraman 	kvmppc_save_guest_altivec(vcpu);
82395d80a29SMihai Caraman #endif
82495d80a29SMihai Caraman 
8251d1ef222SScott Wood out:
826d69c6436SAlexander Graf 	vcpu->mode = OUTSIDE_GUEST_MODE;
827df6909e5SPaul Mackerras 	return ret;
828df6909e5SPaul Mackerras }
829df6909e5SPaul Mackerras 
emulation_exit(struct kvm_vcpu * vcpu)8308c99d345STianjia Zhang static int emulation_exit(struct kvm_vcpu *vcpu)
831d9fbd03dSHollis Blanchard {
832d9fbd03dSHollis Blanchard 	enum emulation_result er;
833d9fbd03dSHollis Blanchard 
8348c99d345STianjia Zhang 	er = kvmppc_emulate_instruction(vcpu);
835d9fbd03dSHollis Blanchard 	switch (er) {
836d9fbd03dSHollis Blanchard 	case EMULATE_DONE:
83773e75b41SHollis Blanchard 		/* don't overwrite subtypes, just account kvm_stats */
8387b701591SHollis Blanchard 		kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
839d9fbd03dSHollis Blanchard 		/* Future optimization: only reload non-volatiles if
840d9fbd03dSHollis Blanchard 		 * they were actually modified by emulation. */
841d30f6e48SScott Wood 		return RESUME_GUEST_NV;
842d30f6e48SScott Wood 
84351f04726SMihai Caraman 	case EMULATE_AGAIN:
84451f04726SMihai Caraman 		return RESUME_GUEST;
84551f04726SMihai Caraman 
846d9fbd03dSHollis Blanchard 	case EMULATE_FAIL:
847*953e3739SPaul Mackerras 		printk(KERN_CRIT "%s: emulation at %lx failed (%08lx)\n",
848173c520aSSimon Guo 		       __func__, vcpu->arch.regs.nip, vcpu->arch.last_inst);
849d9fbd03dSHollis Blanchard 		/* For debugging, encode the failing instruction and
850d9fbd03dSHollis Blanchard 		 * report it to userspace. */
8518c99d345STianjia Zhang 		vcpu->run->hw.hardware_exit_reason = ~0ULL << 32;
8528c99d345STianjia Zhang 		vcpu->run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
853d1ff5499SAlexander Graf 		kvmppc_core_queue_program(vcpu, ESR_PIL);
854d30f6e48SScott Wood 		return RESUME_HOST;
855d30f6e48SScott Wood 
8569b4f5308SBharat Bhushan 	case EMULATE_EXIT_USER:
8579b4f5308SBharat Bhushan 		return RESUME_HOST;
8589b4f5308SBharat Bhushan 
859d9fbd03dSHollis Blanchard 	default:
860d9fbd03dSHollis Blanchard 		BUG();
861d9fbd03dSHollis Blanchard 	}
862d30f6e48SScott Wood }
863d30f6e48SScott Wood 
kvmppc_handle_debug(struct kvm_vcpu * vcpu)8648c99d345STianjia Zhang static int kvmppc_handle_debug(struct kvm_vcpu *vcpu)
865ce11e48bSBharat Bhushan {
8668c99d345STianjia Zhang 	struct kvm_run *run = vcpu->run;
867348ba710SBharat Bhushan 	struct debug_reg *dbg_reg = &(vcpu->arch.dbg_reg);
868ce11e48bSBharat Bhushan 	u32 dbsr = vcpu->arch.dbsr;
869ce11e48bSBharat Bhushan 
8702f699a59SBharat Bhushan 	if (vcpu->guest_debug == 0) {
8712f699a59SBharat Bhushan 		/*
8722f699a59SBharat Bhushan 		 * Debug resources belong to Guest.
8732f699a59SBharat Bhushan 		 * Imprecise debug event is not injected
8742f699a59SBharat Bhushan 		 */
8752f699a59SBharat Bhushan 		if (dbsr & DBSR_IDE) {
8762f699a59SBharat Bhushan 			dbsr &= ~DBSR_IDE;
8772f699a59SBharat Bhushan 			if (!dbsr)
8782f699a59SBharat Bhushan 				return RESUME_GUEST;
8792f699a59SBharat Bhushan 		}
8802f699a59SBharat Bhushan 
8812f699a59SBharat Bhushan 		if (dbsr && (vcpu->arch.shared->msr & MSR_DE) &&
8822f699a59SBharat Bhushan 			    (vcpu->arch.dbg_reg.dbcr0 & DBCR0_IDM))
8832f699a59SBharat Bhushan 			kvmppc_core_queue_debug(vcpu);
8842f699a59SBharat Bhushan 
8852f699a59SBharat Bhushan 		/* Inject a program interrupt if trap debug is not allowed */
8862f699a59SBharat Bhushan 		if ((dbsr & DBSR_TIE) && !(vcpu->arch.shared->msr & MSR_DE))
8872f699a59SBharat Bhushan 			kvmppc_core_queue_program(vcpu, ESR_PTR);
8882f699a59SBharat Bhushan 
8892f699a59SBharat Bhushan 		return RESUME_GUEST;
8902f699a59SBharat Bhushan 	}
8912f699a59SBharat Bhushan 
8922f699a59SBharat Bhushan 	/*
8932f699a59SBharat Bhushan 	 * Debug resource owned by userspace.
8942f699a59SBharat Bhushan 	 * Clear guest dbsr (vcpu->arch.dbsr)
8952f699a59SBharat Bhushan 	 */
8962190991eSBharat Bhushan 	vcpu->arch.dbsr = 0;
897ce11e48bSBharat Bhushan 	run->debug.arch.status = 0;
898173c520aSSimon Guo 	run->debug.arch.address = vcpu->arch.regs.nip;
899ce11e48bSBharat Bhushan 
900ce11e48bSBharat Bhushan 	if (dbsr & (DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4)) {
901ce11e48bSBharat Bhushan 		run->debug.arch.status |= KVMPPC_DEBUG_BREAKPOINT;
902ce11e48bSBharat Bhushan 	} else {
903ce11e48bSBharat Bhushan 		if (dbsr & (DBSR_DAC1W | DBSR_DAC2W))
904ce11e48bSBharat Bhushan 			run->debug.arch.status |= KVMPPC_DEBUG_WATCH_WRITE;
905ce11e48bSBharat Bhushan 		else if (dbsr & (DBSR_DAC1R | DBSR_DAC2R))
906ce11e48bSBharat Bhushan 			run->debug.arch.status |= KVMPPC_DEBUG_WATCH_READ;
907ce11e48bSBharat Bhushan 		if (dbsr & (DBSR_DAC1R | DBSR_DAC1W))
908ce11e48bSBharat Bhushan 			run->debug.arch.address = dbg_reg->dac1;
909ce11e48bSBharat Bhushan 		else if (dbsr & (DBSR_DAC2R | DBSR_DAC2W))
910ce11e48bSBharat Bhushan 			run->debug.arch.address = dbg_reg->dac2;
911ce11e48bSBharat Bhushan 	}
912ce11e48bSBharat Bhushan 
913ce11e48bSBharat Bhushan 	return RESUME_HOST;
914ce11e48bSBharat Bhushan }
915ce11e48bSBharat Bhushan 
kvmppc_fill_pt_regs(struct pt_regs * regs)9164e642ccbSAlexander Graf static void kvmppc_fill_pt_regs(struct pt_regs *regs)
9174e642ccbSAlexander Graf {
918fe6de81bSSathvika Vasireddy 	ulong r1, msr, lr;
9194e642ccbSAlexander Graf 
9204e642ccbSAlexander Graf 	asm("mr %0, 1" : "=r"(r1));
9214e642ccbSAlexander Graf 	asm("mflr %0" : "=r"(lr));
9224e642ccbSAlexander Graf 	asm("mfmsr %0" : "=r"(msr));
9234e642ccbSAlexander Graf 
9244e642ccbSAlexander Graf 	memset(regs, 0, sizeof(*regs));
9254e642ccbSAlexander Graf 	regs->gpr[1] = r1;
926fe6de81bSSathvika Vasireddy 	regs->nip = _THIS_IP_;
9274e642ccbSAlexander Graf 	regs->msr = msr;
9284e642ccbSAlexander Graf 	regs->link = lr;
9294e642ccbSAlexander Graf }
9304e642ccbSAlexander Graf 
9316328e593SBharat Bhushan /*
9326328e593SBharat Bhushan  * For interrupts needed to be handled by host interrupt handlers,
9336328e593SBharat Bhushan  * corresponding host handler are called from here in similar way
9346328e593SBharat Bhushan  * (but not exact) as they are called from low level handler
9356328e593SBharat Bhushan  * (such as from arch/powerpc/kernel/head_fsl_booke.S).
9366328e593SBharat Bhushan  */
kvmppc_restart_interrupt(struct kvm_vcpu * vcpu,unsigned int exit_nr)9374e642ccbSAlexander Graf static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
9384e642ccbSAlexander Graf 				     unsigned int exit_nr)
9394e642ccbSAlexander Graf {
9404e642ccbSAlexander Graf 	struct pt_regs regs;
9414e642ccbSAlexander Graf 
9424e642ccbSAlexander Graf 	switch (exit_nr) {
9434e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_EXTERNAL:
9444e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
9454e642ccbSAlexander Graf 		do_IRQ(&regs);
9464e642ccbSAlexander Graf 		break;
9474e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_DECREMENTER:
9484e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
9494e642ccbSAlexander Graf 		timer_interrupt(&regs);
9504e642ccbSAlexander Graf 		break;
9515f17ce8bSTiejun Chen #if defined(CONFIG_PPC_DOORBELL)
9524e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_DOORBELL:
9534e642ccbSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
9544e642ccbSAlexander Graf 		doorbell_exception(&regs);
9554e642ccbSAlexander Graf 		break;
9564e642ccbSAlexander Graf #endif
9574e642ccbSAlexander Graf 	case BOOKE_INTERRUPT_MACHINE_CHECK:
9584e642ccbSAlexander Graf 		/* FIXME */
9594e642ccbSAlexander Graf 		break;
9607cc1e8eeSAlexander Graf 	case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
9617cc1e8eeSAlexander Graf 		kvmppc_fill_pt_regs(&regs);
9627cc1e8eeSAlexander Graf 		performance_monitor_exception(&regs);
9637cc1e8eeSAlexander Graf 		break;
9646328e593SBharat Bhushan 	case BOOKE_INTERRUPT_WATCHDOG:
9656328e593SBharat Bhushan 		kvmppc_fill_pt_regs(&regs);
9666328e593SBharat Bhushan #ifdef CONFIG_BOOKE_WDT
9676328e593SBharat Bhushan 		WatchdogException(&regs);
9686328e593SBharat Bhushan #else
9696328e593SBharat Bhushan 		unknown_exception(&regs);
9706328e593SBharat Bhushan #endif
9716328e593SBharat Bhushan 		break;
9726328e593SBharat Bhushan 	case BOOKE_INTERRUPT_CRITICAL:
973845ac985STudor Laurentiu 		kvmppc_fill_pt_regs(&regs);
9746328e593SBharat Bhushan 		unknown_exception(&regs);
9756328e593SBharat Bhushan 		break;
976ce11e48bSBharat Bhushan 	case BOOKE_INTERRUPT_DEBUG:
977ce11e48bSBharat Bhushan 		/* Save DBSR before preemption is enabled */
978ce11e48bSBharat Bhushan 		vcpu->arch.dbsr = mfspr(SPRN_DBSR);
979ce11e48bSBharat Bhushan 		kvmppc_clear_dbsr();
980ce11e48bSBharat Bhushan 		break;
9814e642ccbSAlexander Graf 	}
9824e642ccbSAlexander Graf }
9834e642ccbSAlexander Graf 
kvmppc_resume_inst_load(struct kvm_vcpu * vcpu,enum emulation_result emulated,u32 last_inst)9848c99d345STianjia Zhang static int kvmppc_resume_inst_load(struct kvm_vcpu *vcpu,
985f5250471SMihai Caraman 				  enum emulation_result emulated, u32 last_inst)
986f5250471SMihai Caraman {
987f5250471SMihai Caraman 	switch (emulated) {
988f5250471SMihai Caraman 	case EMULATE_AGAIN:
989f5250471SMihai Caraman 		return RESUME_GUEST;
990f5250471SMihai Caraman 
991f5250471SMihai Caraman 	case EMULATE_FAIL:
992f5250471SMihai Caraman 		pr_debug("%s: load instruction from guest address %lx failed\n",
993173c520aSSimon Guo 		       __func__, vcpu->arch.regs.nip);
994f5250471SMihai Caraman 		/* For debugging, encode the failing instruction and
995f5250471SMihai Caraman 		 * report it to userspace. */
9968c99d345STianjia Zhang 		vcpu->run->hw.hardware_exit_reason = ~0ULL << 32;
9978c99d345STianjia Zhang 		vcpu->run->hw.hardware_exit_reason |= last_inst;
998f5250471SMihai Caraman 		kvmppc_core_queue_program(vcpu, ESR_PIL);
999f5250471SMihai Caraman 		return RESUME_HOST;
1000f5250471SMihai Caraman 
1001f5250471SMihai Caraman 	default:
1002f5250471SMihai Caraman 		BUG();
1003f5250471SMihai Caraman 	}
1004f5250471SMihai Caraman }
1005f5250471SMihai Caraman 
100643d05c61SMichael Ellerman /*
1007d30f6e48SScott Wood  * kvmppc_handle_exit
1008d30f6e48SScott Wood  *
1009d30f6e48SScott Wood  * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
1010d30f6e48SScott Wood  */
kvmppc_handle_exit(struct kvm_vcpu * vcpu,unsigned int exit_nr)10117ec21d9dSTianjia Zhang int kvmppc_handle_exit(struct kvm_vcpu *vcpu, unsigned int exit_nr)
1012d30f6e48SScott Wood {
10137ec21d9dSTianjia Zhang 	struct kvm_run *run = vcpu->run;
1014d30f6e48SScott Wood 	int r = RESUME_HOST;
10157ee78855SAlexander Graf 	int s;
1016f1e89028SScott Wood 	int idx;
1017f5250471SMihai Caraman 	u32 last_inst = KVM_INST_FETCH_FAILED;
1018acf17878SPaul Mackerras 	ppc_inst_t pinst;
1019f5250471SMihai Caraman 	enum emulation_result emulated = EMULATE_DONE;
1020d30f6e48SScott Wood 
10216c645b01SNicholas Piggin 	/* Fix irq state (pairs with kvmppc_fix_ee_before_entry()) */
10226c645b01SNicholas Piggin 	kvmppc_fix_ee_after_exit();
10236c645b01SNicholas Piggin 
1024d30f6e48SScott Wood 	/* update before a new last_exit_type is rewritten */
1025d30f6e48SScott Wood 	kvmppc_update_timing_stats(vcpu);
1026d30f6e48SScott Wood 
10274e642ccbSAlexander Graf 	/* restart interrupts if they were meant for the host */
10284e642ccbSAlexander Graf 	kvmppc_restart_interrupt(vcpu, exit_nr);
1029d30f6e48SScott Wood 
1030f5250471SMihai Caraman 	/*
1031446957baSAdam Buchbinder 	 * get last instruction before being preempted
1032f5250471SMihai Caraman 	 * TODO: for e6500 check also BOOKE_INTERRUPT_LRAT_ERROR & ESR_DATA
1033f5250471SMihai Caraman 	 */
1034f5250471SMihai Caraman 	switch (exit_nr) {
1035f5250471SMihai Caraman 	case BOOKE_INTERRUPT_DATA_STORAGE:
1036f5250471SMihai Caraman 	case BOOKE_INTERRUPT_DTLB_MISS:
1037f5250471SMihai Caraman 	case BOOKE_INTERRUPT_HV_PRIV:
1038acf17878SPaul Mackerras 		emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &pinst);
1039acf17878SPaul Mackerras 		last_inst = ppc_inst_val(pinst);
1040f5250471SMihai Caraman 		break;
1041033aaa14SMadhavan Srinivasan 	case BOOKE_INTERRUPT_PROGRAM:
1042033aaa14SMadhavan Srinivasan 		/* SW breakpoints arrive as illegal instructions on HV */
1043acf17878SPaul Mackerras 		if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
1044acf17878SPaul Mackerras 			emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &pinst);
1045acf17878SPaul Mackerras 			last_inst = ppc_inst_val(pinst);
1046acf17878SPaul Mackerras 		}
1047033aaa14SMadhavan Srinivasan 		break;
1048f5250471SMihai Caraman 	default:
1049f5250471SMihai Caraman 		break;
1050f5250471SMihai Caraman 	}
1051f5250471SMihai Caraman 
105297c95059SAlexander Graf 	trace_kvm_exit(exit_nr, vcpu);
1053235cee16SLaurent Vivier 
1054235cee16SLaurent Vivier 	context_tracking_guest_exit();
1055235cee16SLaurent Vivier 	if (!vtime_accounting_enabled_this_cpu()) {
1056235cee16SLaurent Vivier 		local_irq_enable();
1057235cee16SLaurent Vivier 		/*
1058235cee16SLaurent Vivier 		 * Service IRQs here before vtime_account_guest_exit() so any
1059235cee16SLaurent Vivier 		 * ticks that occurred while running the guest are accounted to
1060235cee16SLaurent Vivier 		 * the guest. If vtime accounting is enabled, accounting uses
1061235cee16SLaurent Vivier 		 * TB rather than ticks, so it can be done without enabling
1062235cee16SLaurent Vivier 		 * interrupts here, which has the problem that it accounts
1063235cee16SLaurent Vivier 		 * interrupt processing overhead to the host.
1064235cee16SLaurent Vivier 		 */
1065235cee16SLaurent Vivier 		local_irq_disable();
1066235cee16SLaurent Vivier 	}
1067235cee16SLaurent Vivier 	vtime_account_guest_exit();
1068e233d54dSPaolo Bonzini 
1069e233d54dSPaolo Bonzini 	local_irq_enable();
107097c95059SAlexander Graf 
1071d30f6e48SScott Wood 	run->exit_reason = KVM_EXIT_UNKNOWN;
1072d30f6e48SScott Wood 	run->ready_for_interrupt_injection = 1;
1073d30f6e48SScott Wood 
1074f5250471SMihai Caraman 	if (emulated != EMULATE_DONE) {
10758c99d345STianjia Zhang 		r = kvmppc_resume_inst_load(vcpu, emulated, last_inst);
1076f5250471SMihai Caraman 		goto out;
1077f5250471SMihai Caraman 	}
1078f5250471SMihai Caraman 
1079d30f6e48SScott Wood 	switch (exit_nr) {
1080d30f6e48SScott Wood 	case BOOKE_INTERRUPT_MACHINE_CHECK:
1081c35c9d84SAlexander Graf 		printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
1082c35c9d84SAlexander Graf 		kvmppc_dump_vcpu(vcpu);
1083c35c9d84SAlexander Graf 		/* For debugging, send invalid exit reason to user space */
1084c35c9d84SAlexander Graf 		run->hw.hardware_exit_reason = ~1ULL << 32;
1085c35c9d84SAlexander Graf 		run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
1086c35c9d84SAlexander Graf 		r = RESUME_HOST;
1087d30f6e48SScott Wood 		break;
1088d30f6e48SScott Wood 
1089d30f6e48SScott Wood 	case BOOKE_INTERRUPT_EXTERNAL:
1090d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
1091d30f6e48SScott Wood 		r = RESUME_GUEST;
1092d30f6e48SScott Wood 		break;
1093d30f6e48SScott Wood 
1094d30f6e48SScott Wood 	case BOOKE_INTERRUPT_DECREMENTER:
1095d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, DEC_EXITS);
1096d30f6e48SScott Wood 		r = RESUME_GUEST;
1097d30f6e48SScott Wood 		break;
1098d30f6e48SScott Wood 
10996328e593SBharat Bhushan 	case BOOKE_INTERRUPT_WATCHDOG:
11006328e593SBharat Bhushan 		r = RESUME_GUEST;
11016328e593SBharat Bhushan 		break;
11026328e593SBharat Bhushan 
1103d30f6e48SScott Wood 	case BOOKE_INTERRUPT_DOORBELL:
1104d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, DBELL_EXITS);
1105d30f6e48SScott Wood 		r = RESUME_GUEST;
1106d30f6e48SScott Wood 		break;
1107d30f6e48SScott Wood 
1108d30f6e48SScott Wood 	case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
1109d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, GDBELL_EXITS);
1110d30f6e48SScott Wood 
1111d30f6e48SScott Wood 		/*
1112d30f6e48SScott Wood 		 * We are here because there is a pending guest interrupt
1113d30f6e48SScott Wood 		 * which could not be delivered as MSR_CE or MSR_ME was not
1114d30f6e48SScott Wood 		 * set.  Once we break from here we will retry delivery.
1115d30f6e48SScott Wood 		 */
1116d30f6e48SScott Wood 		r = RESUME_GUEST;
1117d30f6e48SScott Wood 		break;
1118d30f6e48SScott Wood 
1119d30f6e48SScott Wood 	case BOOKE_INTERRUPT_GUEST_DBELL:
1120d30f6e48SScott Wood 		kvmppc_account_exit(vcpu, GDBELL_EXITS);
1121d30f6e48SScott Wood 
1122d30f6e48SScott Wood 		/*
1123d30f6e48SScott Wood 		 * We are here because there is a pending guest interrupt
1124d30f6e48SScott Wood 		 * which could not be delivered as MSR_EE was not set.  Once
1125d30f6e48SScott Wood 		 * we break from here we will retry delivery.
1126d30f6e48SScott Wood 		 */
1127d30f6e48SScott Wood 		r = RESUME_GUEST;
1128d30f6e48SScott Wood 		break;
1129d30f6e48SScott Wood 
113095f2e921SAlexander Graf 	case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
113195f2e921SAlexander Graf 		r = RESUME_GUEST;
113295f2e921SAlexander Graf 		break;
113395f2e921SAlexander Graf 
1134d30f6e48SScott Wood 	case BOOKE_INTERRUPT_HV_PRIV:
11358c99d345STianjia Zhang 		r = emulation_exit(vcpu);
1136d30f6e48SScott Wood 		break;
1137d30f6e48SScott Wood 
1138d30f6e48SScott Wood 	case BOOKE_INTERRUPT_PROGRAM:
1139033aaa14SMadhavan Srinivasan 		if ((vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) &&
1140033aaa14SMadhavan Srinivasan 			(last_inst == KVMPPC_INST_SW_BREAKPOINT)) {
1141033aaa14SMadhavan Srinivasan 			/*
1142033aaa14SMadhavan Srinivasan 			 * We are here because of an SW breakpoint instr,
1143033aaa14SMadhavan Srinivasan 			 * so lets return to host to handle.
1144033aaa14SMadhavan Srinivasan 			 */
11458c99d345STianjia Zhang 			r = kvmppc_handle_debug(vcpu);
1146033aaa14SMadhavan Srinivasan 			run->exit_reason = KVM_EXIT_DEBUG;
1147033aaa14SMadhavan Srinivasan 			kvmppc_account_exit(vcpu, DEBUG_EXITS);
1148033aaa14SMadhavan Srinivasan 			break;
1149033aaa14SMadhavan Srinivasan 		}
1150033aaa14SMadhavan Srinivasan 
1151d30f6e48SScott Wood 		if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
11520268597cSAlexander Graf 			/*
11530268597cSAlexander Graf 			 * Program traps generated by user-level software must
11540268597cSAlexander Graf 			 * be handled by the guest kernel.
11550268597cSAlexander Graf 			 *
11560268597cSAlexander Graf 			 * In GS mode, hypervisor privileged instructions trap
11570268597cSAlexander Graf 			 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
11580268597cSAlexander Graf 			 * actual program interrupts, handled by the guest.
11590268597cSAlexander Graf 			 */
1160d30f6e48SScott Wood 			kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
1161d30f6e48SScott Wood 			r = RESUME_GUEST;
1162d30f6e48SScott Wood 			kvmppc_account_exit(vcpu, USR_PR_INST);
1163d30f6e48SScott Wood 			break;
1164d30f6e48SScott Wood 		}
1165d30f6e48SScott Wood 
11668c99d345STianjia Zhang 		r = emulation_exit(vcpu);
1167d9fbd03dSHollis Blanchard 		break;
1168d9fbd03dSHollis Blanchard 
1169d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_FP_UNAVAIL:
1170d4cf3892SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
11717b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, FP_UNAVAIL);
1172d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
1173d9fbd03dSHollis Blanchard 		break;
1174d9fbd03dSHollis Blanchard 
11754cd35f67SScott Wood #ifdef CONFIG_SPE
11764cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_UNAVAIL: {
11774cd35f67SScott Wood 		if (vcpu->arch.shared->msr & MSR_SPE)
11784cd35f67SScott Wood 			kvmppc_vcpu_enable_spe(vcpu);
11794cd35f67SScott Wood 		else
11804cd35f67SScott Wood 			kvmppc_booke_queue_irqprio(vcpu,
11814cd35f67SScott Wood 						   BOOKE_IRQPRIO_SPE_UNAVAIL);
1182bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
1183bb3a8a17SHollis Blanchard 		break;
11844cd35f67SScott Wood 	}
1185bb3a8a17SHollis Blanchard 
1186bb3a8a17SHollis Blanchard 	case BOOKE_INTERRUPT_SPE_FP_DATA:
1187bb3a8a17SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
1188bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
1189bb3a8a17SHollis Blanchard 		break;
1190bb3a8a17SHollis Blanchard 
1191bb3a8a17SHollis Blanchard 	case BOOKE_INTERRUPT_SPE_FP_ROUND:
1192bb3a8a17SHollis Blanchard 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
1193bb3a8a17SHollis Blanchard 		r = RESUME_GUEST;
1194bb3a8a17SHollis Blanchard 		break;
119595d80a29SMihai Caraman #elif defined(CONFIG_SPE_POSSIBLE)
11964cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_UNAVAIL:
11974cd35f67SScott Wood 		/*
11984cd35f67SScott Wood 		 * Guest wants SPE, but host kernel doesn't support it.  Send
11994cd35f67SScott Wood 		 * an "unimplemented operation" program check to the guest.
12004cd35f67SScott Wood 		 */
12014cd35f67SScott Wood 		kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
12024cd35f67SScott Wood 		r = RESUME_GUEST;
12034cd35f67SScott Wood 		break;
12044cd35f67SScott Wood 
12054cd35f67SScott Wood 	/*
12064cd35f67SScott Wood 	 * These really should never happen without CONFIG_SPE,
12074cd35f67SScott Wood 	 * as we should never enable the real MSR[SPE] in the guest.
12084cd35f67SScott Wood 	 */
12094cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_FP_DATA:
12104cd35f67SScott Wood 	case BOOKE_INTERRUPT_SPE_FP_ROUND:
12114cd35f67SScott Wood 		printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
1212173c520aSSimon Guo 		       __func__, exit_nr, vcpu->arch.regs.nip);
12134cd35f67SScott Wood 		run->hw.hardware_exit_reason = exit_nr;
12144cd35f67SScott Wood 		r = RESUME_HOST;
12154cd35f67SScott Wood 		break;
121695d80a29SMihai Caraman #endif /* CONFIG_SPE_POSSIBLE */
121795d80a29SMihai Caraman 
121895d80a29SMihai Caraman /*
121995d80a29SMihai Caraman  * On cores with Vector category, KVM is loaded only if CONFIG_ALTIVEC,
12207cb79f43SSean Christopherson  * see kvmppc_e500mc_check_processor_compat().
122195d80a29SMihai Caraman  */
122295d80a29SMihai Caraman #ifdef CONFIG_ALTIVEC
122395d80a29SMihai Caraman 	case BOOKE_INTERRUPT_ALTIVEC_UNAVAIL:
122495d80a29SMihai Caraman 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALTIVEC_UNAVAIL);
122595d80a29SMihai Caraman 		r = RESUME_GUEST;
122695d80a29SMihai Caraman 		break;
122795d80a29SMihai Caraman 
122895d80a29SMihai Caraman 	case BOOKE_INTERRUPT_ALTIVEC_ASSIST:
122995d80a29SMihai Caraman 		kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALTIVEC_ASSIST);
123095d80a29SMihai Caraman 		r = RESUME_GUEST;
123195d80a29SMihai Caraman 		break;
12324cd35f67SScott Wood #endif
1233bb3a8a17SHollis Blanchard 
1234d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DATA_STORAGE:
1235460ba21dSNicholas Piggin 		kvmppc_core_queue_data_storage(vcpu, 0, vcpu->arch.fault_dear,
1236daf5e271SLiu Yu 		                               vcpu->arch.fault_esr);
12377b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, DSI_EXITS);
1238d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
1239d9fbd03dSHollis Blanchard 		break;
1240d9fbd03dSHollis Blanchard 
1241d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_INST_STORAGE:
1242daf5e271SLiu Yu 		kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
12437b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, ISI_EXITS);
1244d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
1245d9fbd03dSHollis Blanchard 		break;
1246d9fbd03dSHollis Blanchard 
1247011da899SAlexander Graf 	case BOOKE_INTERRUPT_ALIGNMENT:
1248011da899SAlexander Graf 		kvmppc_core_queue_alignment(vcpu, vcpu->arch.fault_dear,
1249011da899SAlexander Graf 		                            vcpu->arch.fault_esr);
1250011da899SAlexander Graf 		r = RESUME_GUEST;
1251011da899SAlexander Graf 		break;
1252011da899SAlexander Graf 
1253d30f6e48SScott Wood #ifdef CONFIG_KVM_BOOKE_HV
1254d30f6e48SScott Wood 	case BOOKE_INTERRUPT_HV_SYSCALL:
1255d30f6e48SScott Wood 		if (!(vcpu->arch.shared->msr & MSR_PR)) {
1256d30f6e48SScott Wood 			kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1257d30f6e48SScott Wood 		} else {
1258d30f6e48SScott Wood 			/*
1259d30f6e48SScott Wood 			 * hcall from guest userspace -- send privileged
1260d30f6e48SScott Wood 			 * instruction program check.
1261d30f6e48SScott Wood 			 */
1262d30f6e48SScott Wood 			kvmppc_core_queue_program(vcpu, ESR_PPR);
1263d30f6e48SScott Wood 		}
1264d30f6e48SScott Wood 
1265d30f6e48SScott Wood 		r = RESUME_GUEST;
1266d30f6e48SScott Wood 		break;
1267d30f6e48SScott Wood #else
1268d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_SYSCALL:
12692a342ed5SAlexander Graf 		if (!(vcpu->arch.shared->msr & MSR_PR) &&
12702a342ed5SAlexander Graf 		    (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
12712a342ed5SAlexander Graf 			/* KVM PV hypercalls */
12722a342ed5SAlexander Graf 			kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
12732a342ed5SAlexander Graf 			r = RESUME_GUEST;
12742a342ed5SAlexander Graf 		} else {
12752a342ed5SAlexander Graf 			/* Guest syscalls */
1276d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
12772a342ed5SAlexander Graf 		}
12787b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, SYSCALL_EXITS);
1279d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
1280d9fbd03dSHollis Blanchard 		break;
1281d30f6e48SScott Wood #endif
1282d9fbd03dSHollis Blanchard 
1283d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DTLB_MISS: {
1284d9fbd03dSHollis Blanchard 		unsigned long eaddr = vcpu->arch.fault_dear;
12857924bd41SHollis Blanchard 		int gtlb_index;
1286475e7cddSHollis Blanchard 		gpa_t gpaddr;
1287d9fbd03dSHollis Blanchard 		gfn_t gfn;
1288d9fbd03dSHollis Blanchard 
1289bf7ca4bdSAlexander Graf #ifdef CONFIG_KVM_E500V2
1290a4cd8b23SScott Wood 		if (!(vcpu->arch.shared->msr & MSR_PR) &&
1291a4cd8b23SScott Wood 		    (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
1292a4cd8b23SScott Wood 			kvmppc_map_magic(vcpu);
1293a4cd8b23SScott Wood 			kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
1294a4cd8b23SScott Wood 			r = RESUME_GUEST;
1295a4cd8b23SScott Wood 
1296a4cd8b23SScott Wood 			break;
1297a4cd8b23SScott Wood 		}
1298a4cd8b23SScott Wood #endif
1299a4cd8b23SScott Wood 
1300d9fbd03dSHollis Blanchard 		/* Check the guest TLB. */
1301fa86b8ddSHollis Blanchard 		gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
13027924bd41SHollis Blanchard 		if (gtlb_index < 0) {
1303d9fbd03dSHollis Blanchard 			/* The guest didn't have a mapping for it. */
1304daf5e271SLiu Yu 			kvmppc_core_queue_dtlb_miss(vcpu,
1305daf5e271SLiu Yu 			                            vcpu->arch.fault_dear,
1306daf5e271SLiu Yu 			                            vcpu->arch.fault_esr);
1307b52a638cSHollis Blanchard 			kvmppc_mmu_dtlb_miss(vcpu);
13087b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
1309d9fbd03dSHollis Blanchard 			r = RESUME_GUEST;
1310d9fbd03dSHollis Blanchard 			break;
1311d9fbd03dSHollis Blanchard 		}
1312d9fbd03dSHollis Blanchard 
1313f1e89028SScott Wood 		idx = srcu_read_lock(&vcpu->kvm->srcu);
1314f1e89028SScott Wood 
1315be8d1caeSHollis Blanchard 		gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
1316475e7cddSHollis Blanchard 		gfn = gpaddr >> PAGE_SHIFT;
1317d9fbd03dSHollis Blanchard 
1318d9fbd03dSHollis Blanchard 		if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1319d9fbd03dSHollis Blanchard 			/* The guest TLB had a mapping, but the shadow TLB
1320d9fbd03dSHollis Blanchard 			 * didn't, and it is RAM. This could be because:
1321d9fbd03dSHollis Blanchard 			 * a) the entry is mapping the host kernel, or
1322d9fbd03dSHollis Blanchard 			 * b) the guest used a large mapping which we're faking
1323d9fbd03dSHollis Blanchard 			 * Either way, we need to satisfy the fault without
1324d9fbd03dSHollis Blanchard 			 * invoking the guest. */
132558a96214SHollis Blanchard 			kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
13267b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
1327d9fbd03dSHollis Blanchard 			r = RESUME_GUEST;
1328d9fbd03dSHollis Blanchard 		} else {
1329d9fbd03dSHollis Blanchard 			/* Guest has mapped and accessed a page which is not
1330d9fbd03dSHollis Blanchard 			 * actually RAM. */
1331475e7cddSHollis Blanchard 			vcpu->arch.paddr_accessed = gpaddr;
13326020c0f6SAlexander Graf 			vcpu->arch.vaddr_accessed = eaddr;
13338c99d345STianjia Zhang 			r = kvmppc_emulate_mmio(vcpu);
13347b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, MMIO_EXITS);
1335d9fbd03dSHollis Blanchard 		}
1336d9fbd03dSHollis Blanchard 
1337f1e89028SScott Wood 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
1338d9fbd03dSHollis Blanchard 		break;
1339d9fbd03dSHollis Blanchard 	}
1340d9fbd03dSHollis Blanchard 
1341d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_ITLB_MISS: {
1342173c520aSSimon Guo 		unsigned long eaddr = vcpu->arch.regs.nip;
134389168618SHollis Blanchard 		gpa_t gpaddr;
1344d9fbd03dSHollis Blanchard 		gfn_t gfn;
13457924bd41SHollis Blanchard 		int gtlb_index;
1346d9fbd03dSHollis Blanchard 
1347d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
1348d9fbd03dSHollis Blanchard 
1349d9fbd03dSHollis Blanchard 		/* Check the guest TLB. */
1350fa86b8ddSHollis Blanchard 		gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
13517924bd41SHollis Blanchard 		if (gtlb_index < 0) {
1352d9fbd03dSHollis Blanchard 			/* The guest didn't have a mapping for it. */
1353d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
1354b52a638cSHollis Blanchard 			kvmppc_mmu_itlb_miss(vcpu);
13557b701591SHollis Blanchard 			kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
1356d9fbd03dSHollis Blanchard 			break;
1357d9fbd03dSHollis Blanchard 		}
1358d9fbd03dSHollis Blanchard 
13597b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
1360d9fbd03dSHollis Blanchard 
1361f1e89028SScott Wood 		idx = srcu_read_lock(&vcpu->kvm->srcu);
1362f1e89028SScott Wood 
1363be8d1caeSHollis Blanchard 		gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
136489168618SHollis Blanchard 		gfn = gpaddr >> PAGE_SHIFT;
1365d9fbd03dSHollis Blanchard 
1366d9fbd03dSHollis Blanchard 		if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1367d9fbd03dSHollis Blanchard 			/* The guest TLB had a mapping, but the shadow TLB
1368d9fbd03dSHollis Blanchard 			 * didn't. This could be because:
1369d9fbd03dSHollis Blanchard 			 * a) the entry is mapping the host kernel, or
1370d9fbd03dSHollis Blanchard 			 * b) the guest used a large mapping which we're faking
1371d9fbd03dSHollis Blanchard 			 * Either way, we need to satisfy the fault without
1372d9fbd03dSHollis Blanchard 			 * invoking the guest. */
137358a96214SHollis Blanchard 			kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
1374d9fbd03dSHollis Blanchard 		} else {
1375d9fbd03dSHollis Blanchard 			/* Guest mapped and leaped at non-RAM! */
1376d4cf3892SHollis Blanchard 			kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
1377d9fbd03dSHollis Blanchard 		}
1378d9fbd03dSHollis Blanchard 
1379f1e89028SScott Wood 		srcu_read_unlock(&vcpu->kvm->srcu, idx);
1380d9fbd03dSHollis Blanchard 		break;
1381d9fbd03dSHollis Blanchard 	}
1382d9fbd03dSHollis Blanchard 
1383d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DEBUG: {
13848c99d345STianjia Zhang 		r = kvmppc_handle_debug(vcpu);
1385ce11e48bSBharat Bhushan 		if (r == RESUME_HOST)
1386d9fbd03dSHollis Blanchard 			run->exit_reason = KVM_EXIT_DEBUG;
13877b701591SHollis Blanchard 		kvmppc_account_exit(vcpu, DEBUG_EXITS);
1388d9fbd03dSHollis Blanchard 		break;
1389d9fbd03dSHollis Blanchard 	}
1390d9fbd03dSHollis Blanchard 
1391d9fbd03dSHollis Blanchard 	default:
1392d9fbd03dSHollis Blanchard 		printk(KERN_EMERG "exit_nr %d\n", exit_nr);
1393d9fbd03dSHollis Blanchard 		BUG();
1394d9fbd03dSHollis Blanchard 	}
1395d9fbd03dSHollis Blanchard 
1396f5250471SMihai Caraman out:
1397a8e4ef84SAlexander Graf 	/*
1398a8e4ef84SAlexander Graf 	 * To avoid clobbering exit_reason, only check for signals if we
1399a8e4ef84SAlexander Graf 	 * aren't already exiting to userspace for some other reason.
1400a8e4ef84SAlexander Graf 	 */
140103660ba2SAlexander Graf 	if (!(r & RESUME_HOST)) {
14027ee78855SAlexander Graf 		s = kvmppc_prepare_to_enter(vcpu);
14036c85f52bSScott Wood 		if (s <= 0)
14047ee78855SAlexander Graf 			r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
14056c85f52bSScott Wood 		else {
14066c85f52bSScott Wood 			/* interrupts now hard-disabled */
14075f1c248fSScott Wood 			kvmppc_fix_ee_before_entry();
14083efc7da6SMihai Caraman 			kvmppc_load_guest_fp(vcpu);
140995d80a29SMihai Caraman 			kvmppc_load_guest_altivec(vcpu);
141024afa37bSAlexander Graf 		}
141124afa37bSAlexander Graf 	}
1412706fb730SAlexander Graf 
1413d9fbd03dSHollis Blanchard 	return r;
1414d9fbd03dSHollis Blanchard }
1415d9fbd03dSHollis Blanchard 
kvmppc_set_tsr(struct kvm_vcpu * vcpu,u32 new_tsr)1416d26f22c9SBharat Bhushan static void kvmppc_set_tsr(struct kvm_vcpu *vcpu, u32 new_tsr)
1417d26f22c9SBharat Bhushan {
1418d26f22c9SBharat Bhushan 	u32 old_tsr = vcpu->arch.tsr;
1419d26f22c9SBharat Bhushan 
1420d26f22c9SBharat Bhushan 	vcpu->arch.tsr = new_tsr;
1421d26f22c9SBharat Bhushan 
1422d26f22c9SBharat Bhushan 	if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS))
1423d26f22c9SBharat Bhushan 		arm_next_watchdog(vcpu);
1424d26f22c9SBharat Bhushan 
1425d26f22c9SBharat Bhushan 	update_timer_ints(vcpu);
1426d26f22c9SBharat Bhushan }
1427d26f22c9SBharat Bhushan 
kvmppc_subarch_vcpu_init(struct kvm_vcpu * vcpu)1428f61c94bbSBharat Bhushan int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
1429f61c94bbSBharat Bhushan {
1430f61c94bbSBharat Bhushan 	/* setup watchdog timer once */
1431f61c94bbSBharat Bhushan 	spin_lock_init(&vcpu->arch.wdt_lock);
143286cb30ecSKees Cook 	timer_setup(&vcpu->arch.wdt_timer, kvmppc_watchdog_func, 0);
1433f61c94bbSBharat Bhushan 
14342f699a59SBharat Bhushan 	/*
14352f699a59SBharat Bhushan 	 * Clear DBSR.MRR to avoid guest debug interrupt as
14362f699a59SBharat Bhushan 	 * this is of host interest
14372f699a59SBharat Bhushan 	 */
14382f699a59SBharat Bhushan 	mtspr(SPRN_DBSR, DBSR_MRR);
1439f61c94bbSBharat Bhushan 	return 0;
1440f61c94bbSBharat Bhushan }
1441f61c94bbSBharat Bhushan 
kvmppc_subarch_vcpu_uninit(struct kvm_vcpu * vcpu)1442f61c94bbSBharat Bhushan void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
1443f61c94bbSBharat Bhushan {
1444f61c94bbSBharat Bhushan 	del_timer_sync(&vcpu->arch.wdt_timer);
1445f61c94bbSBharat Bhushan }
1446f61c94bbSBharat Bhushan 
kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu * vcpu,struct kvm_regs * regs)1447d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1448d9fbd03dSHollis Blanchard {
1449d9fbd03dSHollis Blanchard 	int i;
1450d9fbd03dSHollis Blanchard 
14511fc9b76bSChristoffer Dall 	vcpu_load(vcpu);
14521fc9b76bSChristoffer Dall 
1453173c520aSSimon Guo 	regs->pc = vcpu->arch.regs.nip;
1454992b5b29SAlexander Graf 	regs->cr = kvmppc_get_cr(vcpu);
1455173c520aSSimon Guo 	regs->ctr = vcpu->arch.regs.ctr;
1456173c520aSSimon Guo 	regs->lr = vcpu->arch.regs.link;
1457992b5b29SAlexander Graf 	regs->xer = kvmppc_get_xer(vcpu);
1458666e7252SAlexander Graf 	regs->msr = vcpu->arch.shared->msr;
145931579eeaSBharat Bhushan 	regs->srr0 = kvmppc_get_srr0(vcpu);
146031579eeaSBharat Bhushan 	regs->srr1 = kvmppc_get_srr1(vcpu);
1461d9fbd03dSHollis Blanchard 	regs->pid = vcpu->arch.pid;
1462c1b8a01bSBharat Bhushan 	regs->sprg0 = kvmppc_get_sprg0(vcpu);
1463c1b8a01bSBharat Bhushan 	regs->sprg1 = kvmppc_get_sprg1(vcpu);
1464c1b8a01bSBharat Bhushan 	regs->sprg2 = kvmppc_get_sprg2(vcpu);
1465c1b8a01bSBharat Bhushan 	regs->sprg3 = kvmppc_get_sprg3(vcpu);
1466c1b8a01bSBharat Bhushan 	regs->sprg4 = kvmppc_get_sprg4(vcpu);
1467c1b8a01bSBharat Bhushan 	regs->sprg5 = kvmppc_get_sprg5(vcpu);
1468c1b8a01bSBharat Bhushan 	regs->sprg6 = kvmppc_get_sprg6(vcpu);
1469c1b8a01bSBharat Bhushan 	regs->sprg7 = kvmppc_get_sprg7(vcpu);
1470d9fbd03dSHollis Blanchard 
1471d9fbd03dSHollis Blanchard 	for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
14728e5b26b5SAlexander Graf 		regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
1473d9fbd03dSHollis Blanchard 
14741fc9b76bSChristoffer Dall 	vcpu_put(vcpu);
1475d9fbd03dSHollis Blanchard 	return 0;
1476d9fbd03dSHollis Blanchard }
1477d9fbd03dSHollis Blanchard 
kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu * vcpu,struct kvm_regs * regs)1478d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1479d9fbd03dSHollis Blanchard {
1480d9fbd03dSHollis Blanchard 	int i;
1481d9fbd03dSHollis Blanchard 
1482875656feSChristoffer Dall 	vcpu_load(vcpu);
1483875656feSChristoffer Dall 
1484173c520aSSimon Guo 	vcpu->arch.regs.nip = regs->pc;
1485992b5b29SAlexander Graf 	kvmppc_set_cr(vcpu, regs->cr);
1486173c520aSSimon Guo 	vcpu->arch.regs.ctr = regs->ctr;
1487173c520aSSimon Guo 	vcpu->arch.regs.link = regs->lr;
1488992b5b29SAlexander Graf 	kvmppc_set_xer(vcpu, regs->xer);
1489b8fd68acSHollis Blanchard 	kvmppc_set_msr(vcpu, regs->msr);
149031579eeaSBharat Bhushan 	kvmppc_set_srr0(vcpu, regs->srr0);
149131579eeaSBharat Bhushan 	kvmppc_set_srr1(vcpu, regs->srr1);
14925ce941eeSScott Wood 	kvmppc_set_pid(vcpu, regs->pid);
1493c1b8a01bSBharat Bhushan 	kvmppc_set_sprg0(vcpu, regs->sprg0);
1494c1b8a01bSBharat Bhushan 	kvmppc_set_sprg1(vcpu, regs->sprg1);
1495c1b8a01bSBharat Bhushan 	kvmppc_set_sprg2(vcpu, regs->sprg2);
1496c1b8a01bSBharat Bhushan 	kvmppc_set_sprg3(vcpu, regs->sprg3);
1497c1b8a01bSBharat Bhushan 	kvmppc_set_sprg4(vcpu, regs->sprg4);
1498c1b8a01bSBharat Bhushan 	kvmppc_set_sprg5(vcpu, regs->sprg5);
1499c1b8a01bSBharat Bhushan 	kvmppc_set_sprg6(vcpu, regs->sprg6);
1500c1b8a01bSBharat Bhushan 	kvmppc_set_sprg7(vcpu, regs->sprg7);
1501d9fbd03dSHollis Blanchard 
15028e5b26b5SAlexander Graf 	for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
15038e5b26b5SAlexander Graf 		kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
1504d9fbd03dSHollis Blanchard 
1505875656feSChristoffer Dall 	vcpu_put(vcpu);
1506d9fbd03dSHollis Blanchard 	return 0;
1507d9fbd03dSHollis Blanchard }
1508d9fbd03dSHollis Blanchard 
get_sregs_base(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)15095ce941eeSScott Wood static void get_sregs_base(struct kvm_vcpu *vcpu,
15105ce941eeSScott Wood                            struct kvm_sregs *sregs)
15115ce941eeSScott Wood {
15125ce941eeSScott Wood 	u64 tb = get_tb();
15135ce941eeSScott Wood 
15145ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_BASE;
15155ce941eeSScott Wood 
15165ce941eeSScott Wood 	sregs->u.e.csrr0 = vcpu->arch.csrr0;
15175ce941eeSScott Wood 	sregs->u.e.csrr1 = vcpu->arch.csrr1;
15185ce941eeSScott Wood 	sregs->u.e.mcsr = vcpu->arch.mcsr;
1519dc168549SBharat Bhushan 	sregs->u.e.esr = kvmppc_get_esr(vcpu);
1520a5414d4bSBharat Bhushan 	sregs->u.e.dear = kvmppc_get_dar(vcpu);
15215ce941eeSScott Wood 	sregs->u.e.tsr = vcpu->arch.tsr;
15225ce941eeSScott Wood 	sregs->u.e.tcr = vcpu->arch.tcr;
15235ce941eeSScott Wood 	sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
15245ce941eeSScott Wood 	sregs->u.e.tb = tb;
15255ce941eeSScott Wood 	sregs->u.e.vrsave = vcpu->arch.vrsave;
15265ce941eeSScott Wood }
15275ce941eeSScott Wood 
set_sregs_base(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)15285ce941eeSScott Wood static int set_sregs_base(struct kvm_vcpu *vcpu,
15295ce941eeSScott Wood                           struct kvm_sregs *sregs)
15305ce941eeSScott Wood {
15315ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
15325ce941eeSScott Wood 		return 0;
15335ce941eeSScott Wood 
15345ce941eeSScott Wood 	vcpu->arch.csrr0 = sregs->u.e.csrr0;
15355ce941eeSScott Wood 	vcpu->arch.csrr1 = sregs->u.e.csrr1;
15365ce941eeSScott Wood 	vcpu->arch.mcsr = sregs->u.e.mcsr;
1537dc168549SBharat Bhushan 	kvmppc_set_esr(vcpu, sregs->u.e.esr);
1538a5414d4bSBharat Bhushan 	kvmppc_set_dar(vcpu, sregs->u.e.dear);
15395ce941eeSScott Wood 	vcpu->arch.vrsave = sregs->u.e.vrsave;
1540dfd4d47eSScott Wood 	kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
15415ce941eeSScott Wood 
1542dfd4d47eSScott Wood 	if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
15435ce941eeSScott Wood 		vcpu->arch.dec = sregs->u.e.dec;
15445ce941eeSScott Wood 		kvmppc_emulate_dec(vcpu);
1545dfd4d47eSScott Wood 	}
15465ce941eeSScott Wood 
1547d26f22c9SBharat Bhushan 	if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR)
1548d26f22c9SBharat Bhushan 		kvmppc_set_tsr(vcpu, sregs->u.e.tsr);
15495ce941eeSScott Wood 
15505ce941eeSScott Wood 	return 0;
15515ce941eeSScott Wood }
15525ce941eeSScott Wood 
get_sregs_arch206(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)15535ce941eeSScott Wood static void get_sregs_arch206(struct kvm_vcpu *vcpu,
15545ce941eeSScott Wood                               struct kvm_sregs *sregs)
15555ce941eeSScott Wood {
15565ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_ARCH206;
15575ce941eeSScott Wood 
1558841741f2SScott Wood 	sregs->u.e.pir = vcpu->vcpu_id;
15595ce941eeSScott Wood 	sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
15605ce941eeSScott Wood 	sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
15615ce941eeSScott Wood 	sregs->u.e.decar = vcpu->arch.decar;
15625ce941eeSScott Wood 	sregs->u.e.ivpr = vcpu->arch.ivpr;
15635ce941eeSScott Wood }
15645ce941eeSScott Wood 
set_sregs_arch206(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)15655ce941eeSScott Wood static int set_sregs_arch206(struct kvm_vcpu *vcpu,
15665ce941eeSScott Wood                              struct kvm_sregs *sregs)
15675ce941eeSScott Wood {
15685ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
15695ce941eeSScott Wood 		return 0;
15705ce941eeSScott Wood 
1571841741f2SScott Wood 	if (sregs->u.e.pir != vcpu->vcpu_id)
15725ce941eeSScott Wood 		return -EINVAL;
15735ce941eeSScott Wood 
15745ce941eeSScott Wood 	vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
15755ce941eeSScott Wood 	vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
15765ce941eeSScott Wood 	vcpu->arch.decar = sregs->u.e.decar;
15775ce941eeSScott Wood 	vcpu->arch.ivpr = sregs->u.e.ivpr;
15785ce941eeSScott Wood 
15795ce941eeSScott Wood 	return 0;
15805ce941eeSScott Wood }
15815ce941eeSScott Wood 
kvmppc_get_sregs_ivor(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)15823a167beaSAneesh Kumar K.V int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
15835ce941eeSScott Wood {
15845ce941eeSScott Wood 	sregs->u.e.features |= KVM_SREGS_E_IVOR;
15855ce941eeSScott Wood 
15865ce941eeSScott Wood 	sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
15875ce941eeSScott Wood 	sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
15885ce941eeSScott Wood 	sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
15895ce941eeSScott Wood 	sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
15905ce941eeSScott Wood 	sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
15915ce941eeSScott Wood 	sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
15925ce941eeSScott Wood 	sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
15935ce941eeSScott Wood 	sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
15945ce941eeSScott Wood 	sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
15955ce941eeSScott Wood 	sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
15965ce941eeSScott Wood 	sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
15975ce941eeSScott Wood 	sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
15985ce941eeSScott Wood 	sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
15995ce941eeSScott Wood 	sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
16005ce941eeSScott Wood 	sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
16015ce941eeSScott Wood 	sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
16023a167beaSAneesh Kumar K.V 	return 0;
16035ce941eeSScott Wood }
16045ce941eeSScott Wood 
kvmppc_set_sregs_ivor(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)16055ce941eeSScott Wood int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
16065ce941eeSScott Wood {
16075ce941eeSScott Wood 	if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
16085ce941eeSScott Wood 		return 0;
16095ce941eeSScott Wood 
16105ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
16115ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
16125ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
16135ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
16145ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
16155ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
16165ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
16175ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
16185ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
16195ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
16205ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
16215ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
16225ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
16235ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
16245ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
16255ce941eeSScott Wood 	vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
16265ce941eeSScott Wood 
16275ce941eeSScott Wood 	return 0;
16285ce941eeSScott Wood }
16295ce941eeSScott Wood 
kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)1630d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1631d9fbd03dSHollis Blanchard                                   struct kvm_sregs *sregs)
1632d9fbd03dSHollis Blanchard {
1633bcdec41cSChristoffer Dall 	int ret;
1634bcdec41cSChristoffer Dall 
1635bcdec41cSChristoffer Dall 	vcpu_load(vcpu);
1636bcdec41cSChristoffer Dall 
16375ce941eeSScott Wood 	sregs->pvr = vcpu->arch.pvr;
16385ce941eeSScott Wood 
16395ce941eeSScott Wood 	get_sregs_base(vcpu, sregs);
16405ce941eeSScott Wood 	get_sregs_arch206(vcpu, sregs);
1641bcdec41cSChristoffer Dall 	ret = vcpu->kvm->arch.kvm_ops->get_sregs(vcpu, sregs);
1642bcdec41cSChristoffer Dall 
1643bcdec41cSChristoffer Dall 	vcpu_put(vcpu);
1644bcdec41cSChristoffer Dall 	return ret;
1645d9fbd03dSHollis Blanchard }
1646d9fbd03dSHollis Blanchard 
kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)1647d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1648d9fbd03dSHollis Blanchard                                   struct kvm_sregs *sregs)
1649d9fbd03dSHollis Blanchard {
1650b4ef9d4eSChristoffer Dall 	int ret = -EINVAL;
16515ce941eeSScott Wood 
1652b4ef9d4eSChristoffer Dall 	vcpu_load(vcpu);
16535ce941eeSScott Wood 	if (vcpu->arch.pvr != sregs->pvr)
1654b4ef9d4eSChristoffer Dall 		goto out;
16555ce941eeSScott Wood 
16565ce941eeSScott Wood 	ret = set_sregs_base(vcpu, sregs);
16575ce941eeSScott Wood 	if (ret < 0)
1658b4ef9d4eSChristoffer Dall 		goto out;
16595ce941eeSScott Wood 
16605ce941eeSScott Wood 	ret = set_sregs_arch206(vcpu, sregs);
16615ce941eeSScott Wood 	if (ret < 0)
1662b4ef9d4eSChristoffer Dall 		goto out;
16635ce941eeSScott Wood 
1664b4ef9d4eSChristoffer Dall 	ret = vcpu->kvm->arch.kvm_ops->set_sregs(vcpu, sregs);
1665b4ef9d4eSChristoffer Dall 
1666b4ef9d4eSChristoffer Dall out:
1667b4ef9d4eSChristoffer Dall 	vcpu_put(vcpu);
1668b4ef9d4eSChristoffer Dall 	return ret;
1669d9fbd03dSHollis Blanchard }
1670d9fbd03dSHollis Blanchard 
kvmppc_get_one_reg(struct kvm_vcpu * vcpu,u64 id,union kvmppc_one_reg * val)16718a41ea53SMihai Caraman int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id,
16728a41ea53SMihai Caraman 			union kvmppc_one_reg *val)
167331f3438eSPaul Mackerras {
167435b299e2SMihai Caraman 	int r = 0;
167535b299e2SMihai Caraman 
16768a41ea53SMihai Caraman 	switch (id) {
16776df8d3fcSBharat Bhushan 	case KVM_REG_PPC_IAC1:
16788a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.dbg_reg.iac1);
16796df8d3fcSBharat Bhushan 		break;
1680547465efSBharat Bhushan 	case KVM_REG_PPC_IAC2:
16818a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.dbg_reg.iac2);
1682547465efSBharat Bhushan 		break;
1683547465efSBharat Bhushan #if CONFIG_PPC_ADV_DEBUG_IACS > 2
1684547465efSBharat Bhushan 	case KVM_REG_PPC_IAC3:
16858a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.dbg_reg.iac3);
1686547465efSBharat Bhushan 		break;
1687547465efSBharat Bhushan 	case KVM_REG_PPC_IAC4:
16888a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.dbg_reg.iac4);
1689547465efSBharat Bhushan 		break;
1690547465efSBharat Bhushan #endif
16916df8d3fcSBharat Bhushan 	case KVM_REG_PPC_DAC1:
16928a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.dbg_reg.dac1);
1693547465efSBharat Bhushan 		break;
169435b299e2SMihai Caraman 	case KVM_REG_PPC_DAC2:
16958a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.dbg_reg.dac2);
16962c509672SBharat Bhushan 		break;
1697324b3e63SAlexander Graf 	case KVM_REG_PPC_EPR: {
169834f754b9SBharat Bhushan 		u32 epr = kvmppc_get_epr(vcpu);
16998a41ea53SMihai Caraman 		*val = get_reg_val(id, epr);
1700324b3e63SAlexander Graf 		break;
1701324b3e63SAlexander Graf 	}
1702352df1deSMihai Caraman #if defined(CONFIG_64BIT)
1703352df1deSMihai Caraman 	case KVM_REG_PPC_EPCR:
17048a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.epcr);
1705352df1deSMihai Caraman 		break;
1706352df1deSMihai Caraman #endif
170778accda4SBharat Bhushan 	case KVM_REG_PPC_TCR:
17088a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.tcr);
170978accda4SBharat Bhushan 		break;
171078accda4SBharat Bhushan 	case KVM_REG_PPC_TSR:
17118a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.tsr);
171278accda4SBharat Bhushan 		break;
171335b299e2SMihai Caraman 	case KVM_REG_PPC_DEBUG_INST:
1714033aaa14SMadhavan Srinivasan 		*val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
17158c32a2eaSBharat Bhushan 		break;
17168b75cbbeSPaul Mackerras 	case KVM_REG_PPC_VRSAVE:
17178a41ea53SMihai Caraman 		*val = get_reg_val(id, vcpu->arch.vrsave);
17188c32a2eaSBharat Bhushan 		break;
17196df8d3fcSBharat Bhushan 	default:
17208a41ea53SMihai Caraman 		r = vcpu->kvm->arch.kvm_ops->get_one_reg(vcpu, id, val);
17216df8d3fcSBharat Bhushan 		break;
17226df8d3fcSBharat Bhushan 	}
172335b299e2SMihai Caraman 
17246df8d3fcSBharat Bhushan 	return r;
172531f3438eSPaul Mackerras }
172631f3438eSPaul Mackerras 
kvmppc_set_one_reg(struct kvm_vcpu * vcpu,u64 id,union kvmppc_one_reg * val)17278a41ea53SMihai Caraman int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id,
17288a41ea53SMihai Caraman 			union kvmppc_one_reg *val)
172931f3438eSPaul Mackerras {
173035b299e2SMihai Caraman 	int r = 0;
173135b299e2SMihai Caraman 
17328a41ea53SMihai Caraman 	switch (id) {
17336df8d3fcSBharat Bhushan 	case KVM_REG_PPC_IAC1:
17348a41ea53SMihai Caraman 		vcpu->arch.dbg_reg.iac1 = set_reg_val(id, *val);
17356df8d3fcSBharat Bhushan 		break;
1736547465efSBharat Bhushan 	case KVM_REG_PPC_IAC2:
17378a41ea53SMihai Caraman 		vcpu->arch.dbg_reg.iac2 = set_reg_val(id, *val);
1738547465efSBharat Bhushan 		break;
1739547465efSBharat Bhushan #if CONFIG_PPC_ADV_DEBUG_IACS > 2
1740547465efSBharat Bhushan 	case KVM_REG_PPC_IAC3:
17418a41ea53SMihai Caraman 		vcpu->arch.dbg_reg.iac3 = set_reg_val(id, *val);
1742547465efSBharat Bhushan 		break;
1743547465efSBharat Bhushan 	case KVM_REG_PPC_IAC4:
17448a41ea53SMihai Caraman 		vcpu->arch.dbg_reg.iac4 = set_reg_val(id, *val);
1745547465efSBharat Bhushan 		break;
1746547465efSBharat Bhushan #endif
17476df8d3fcSBharat Bhushan 	case KVM_REG_PPC_DAC1:
17488a41ea53SMihai Caraman 		vcpu->arch.dbg_reg.dac1 = set_reg_val(id, *val);
1749547465efSBharat Bhushan 		break;
175035b299e2SMihai Caraman 	case KVM_REG_PPC_DAC2:
17518a41ea53SMihai Caraman 		vcpu->arch.dbg_reg.dac2 = set_reg_val(id, *val);
17522c509672SBharat Bhushan 		break;
1753324b3e63SAlexander Graf 	case KVM_REG_PPC_EPR: {
17548a41ea53SMihai Caraman 		u32 new_epr = set_reg_val(id, *val);
1755324b3e63SAlexander Graf 		kvmppc_set_epr(vcpu, new_epr);
1756324b3e63SAlexander Graf 		break;
1757324b3e63SAlexander Graf 	}
1758352df1deSMihai Caraman #if defined(CONFIG_64BIT)
1759352df1deSMihai Caraman 	case KVM_REG_PPC_EPCR: {
17608a41ea53SMihai Caraman 		u32 new_epcr = set_reg_val(id, *val);
1761352df1deSMihai Caraman 		kvmppc_set_epcr(vcpu, new_epcr);
1762352df1deSMihai Caraman 		break;
1763352df1deSMihai Caraman 	}
1764352df1deSMihai Caraman #endif
176578accda4SBharat Bhushan 	case KVM_REG_PPC_OR_TSR: {
17668a41ea53SMihai Caraman 		u32 tsr_bits = set_reg_val(id, *val);
176778accda4SBharat Bhushan 		kvmppc_set_tsr_bits(vcpu, tsr_bits);
176878accda4SBharat Bhushan 		break;
176978accda4SBharat Bhushan 	}
177078accda4SBharat Bhushan 	case KVM_REG_PPC_CLEAR_TSR: {
17718a41ea53SMihai Caraman 		u32 tsr_bits = set_reg_val(id, *val);
177278accda4SBharat Bhushan 		kvmppc_clr_tsr_bits(vcpu, tsr_bits);
177378accda4SBharat Bhushan 		break;
177478accda4SBharat Bhushan 	}
177578accda4SBharat Bhushan 	case KVM_REG_PPC_TSR: {
17768a41ea53SMihai Caraman 		u32 tsr = set_reg_val(id, *val);
177778accda4SBharat Bhushan 		kvmppc_set_tsr(vcpu, tsr);
177878accda4SBharat Bhushan 		break;
177978accda4SBharat Bhushan 	}
178078accda4SBharat Bhushan 	case KVM_REG_PPC_TCR: {
17818a41ea53SMihai Caraman 		u32 tcr = set_reg_val(id, *val);
178278accda4SBharat Bhushan 		kvmppc_set_tcr(vcpu, tcr);
178378accda4SBharat Bhushan 		break;
178478accda4SBharat Bhushan 	}
17858b75cbbeSPaul Mackerras 	case KVM_REG_PPC_VRSAVE:
17868a41ea53SMihai Caraman 		vcpu->arch.vrsave = set_reg_val(id, *val);
17878b75cbbeSPaul Mackerras 		break;
17886df8d3fcSBharat Bhushan 	default:
17898a41ea53SMihai Caraman 		r = vcpu->kvm->arch.kvm_ops->set_one_reg(vcpu, id, val);
17906df8d3fcSBharat Bhushan 		break;
17916df8d3fcSBharat Bhushan 	}
179235b299e2SMihai Caraman 
17936df8d3fcSBharat Bhushan 	return r;
179431f3438eSPaul Mackerras }
179531f3438eSPaul Mackerras 
kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu * vcpu,struct kvm_fpu * fpu)1796d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1797d9fbd03dSHollis Blanchard {
17984e1b2ab7SGreg Kurz 	return -EOPNOTSUPP;
1799d9fbd03dSHollis Blanchard }
1800d9fbd03dSHollis Blanchard 
kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu * vcpu,struct kvm_fpu * fpu)1801d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1802d9fbd03dSHollis Blanchard {
18034e1b2ab7SGreg Kurz 	return -EOPNOTSUPP;
1804d9fbd03dSHollis Blanchard }
1805d9fbd03dSHollis Blanchard 
kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu * vcpu,struct kvm_translation * tr)1806d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1807d9fbd03dSHollis Blanchard                                   struct kvm_translation *tr)
1808d9fbd03dSHollis Blanchard {
180998001d8dSAvi Kivity 	int r;
181098001d8dSAvi Kivity 
18111da5b61dSChristoffer Dall 	vcpu_load(vcpu);
181298001d8dSAvi Kivity 	r = kvmppc_core_vcpu_translate(vcpu, tr);
18131da5b61dSChristoffer Dall 	vcpu_put(vcpu);
181498001d8dSAvi Kivity 	return r;
1815d9fbd03dSHollis Blanchard }
1816d9fbd03dSHollis Blanchard 
kvm_arch_sync_dirty_log(struct kvm * kvm,struct kvm_memory_slot * memslot)18170dff0846SSean Christopherson void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
18180dff0846SSean Christopherson {
18190dff0846SSean Christopherson 
18200dff0846SSean Christopherson }
18210dff0846SSean Christopherson 
kvm_vm_ioctl_get_dirty_log(struct kvm * kvm,struct kvm_dirty_log * log)18224e755758SAlexander Graf int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
18234e755758SAlexander Graf {
18244e1b2ab7SGreg Kurz 	return -EOPNOTSUPP;
18254e755758SAlexander Graf }
18264e755758SAlexander Graf 
kvmppc_core_free_memslot(struct kvm * kvm,struct kvm_memory_slot * slot)1827e96c81eeSSean Christopherson void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
1828a66b48c3SPaul Mackerras {
1829a66b48c3SPaul Mackerras }
1830a66b48c3SPaul Mackerras 
kvmppc_core_prepare_memory_region(struct kvm * kvm,const struct kvm_memory_slot * old,struct kvm_memory_slot * new,enum kvm_mr_change change)1831f9e0554dSPaul Mackerras int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1832537a17b3SSean Christopherson 				      const struct kvm_memory_slot *old,
1833537a17b3SSean Christopherson 				      struct kvm_memory_slot *new,
183482307e67SSean Christopherson 				      enum kvm_mr_change change)
1835f9e0554dSPaul Mackerras {
1836f9e0554dSPaul Mackerras 	return 0;
1837f9e0554dSPaul Mackerras }
1838f9e0554dSPaul Mackerras 
kvmppc_core_commit_memory_region(struct kvm * kvm,struct kvm_memory_slot * old,const struct kvm_memory_slot * new,enum kvm_mr_change change)1839f9e0554dSPaul Mackerras void kvmppc_core_commit_memory_region(struct kvm *kvm,
1840537a17b3SSean Christopherson 				struct kvm_memory_slot *old,
1841f032b734SBharata B Rao 				const struct kvm_memory_slot *new,
1842f032b734SBharata B Rao 				enum kvm_mr_change change)
1843dfe49dbdSPaul Mackerras {
1844dfe49dbdSPaul Mackerras }
1845dfe49dbdSPaul Mackerras 
kvmppc_core_flush_memslot(struct kvm * kvm,struct kvm_memory_slot * memslot)1846dfe49dbdSPaul Mackerras void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
1847f9e0554dSPaul Mackerras {
1848f9e0554dSPaul Mackerras }
1849f9e0554dSPaul Mackerras 
kvmppc_set_epcr(struct kvm_vcpu * vcpu,u32 new_epcr)185038f98824SMihai Caraman void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr)
185138f98824SMihai Caraman {
185238f98824SMihai Caraman #if defined(CONFIG_64BIT)
185338f98824SMihai Caraman 	vcpu->arch.epcr = new_epcr;
185438f98824SMihai Caraman #ifdef CONFIG_KVM_BOOKE_HV
185538f98824SMihai Caraman 	vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM;
185638f98824SMihai Caraman 	if (vcpu->arch.epcr  & SPRN_EPCR_ICM)
185738f98824SMihai Caraman 		vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM;
185838f98824SMihai Caraman #endif
185938f98824SMihai Caraman #endif
186038f98824SMihai Caraman }
186138f98824SMihai Caraman 
kvmppc_set_tcr(struct kvm_vcpu * vcpu,u32 new_tcr)1862dfd4d47eSScott Wood void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1863dfd4d47eSScott Wood {
1864dfd4d47eSScott Wood 	vcpu->arch.tcr = new_tcr;
1865f61c94bbSBharat Bhushan 	arm_next_watchdog(vcpu);
1866dfd4d47eSScott Wood 	update_timer_ints(vcpu);
1867dfd4d47eSScott Wood }
1868dfd4d47eSScott Wood 
kvmppc_set_tsr_bits(struct kvm_vcpu * vcpu,u32 tsr_bits)1869dfd4d47eSScott Wood void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1870dfd4d47eSScott Wood {
1871dfd4d47eSScott Wood 	set_bits(tsr_bits, &vcpu->arch.tsr);
1872dfd4d47eSScott Wood 	smp_wmb();
1873dfd4d47eSScott Wood 	kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1874dfd4d47eSScott Wood 	kvm_vcpu_kick(vcpu);
1875dfd4d47eSScott Wood }
1876dfd4d47eSScott Wood 
kvmppc_clr_tsr_bits(struct kvm_vcpu * vcpu,u32 tsr_bits)1877dfd4d47eSScott Wood void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1878dfd4d47eSScott Wood {
1879dfd4d47eSScott Wood 	clear_bits(tsr_bits, &vcpu->arch.tsr);
1880f61c94bbSBharat Bhushan 
1881f61c94bbSBharat Bhushan 	/*
1882f61c94bbSBharat Bhushan 	 * We may have stopped the watchdog due to
1883f61c94bbSBharat Bhushan 	 * being stuck on final expiration.
1884f61c94bbSBharat Bhushan 	 */
1885f61c94bbSBharat Bhushan 	if (tsr_bits & (TSR_ENW | TSR_WIS))
1886f61c94bbSBharat Bhushan 		arm_next_watchdog(vcpu);
1887f61c94bbSBharat Bhushan 
1888dfd4d47eSScott Wood 	update_timer_ints(vcpu);
1889dfd4d47eSScott Wood }
1890dfd4d47eSScott Wood 
kvmppc_decrementer_func(struct kvm_vcpu * vcpu)1891d02d4d15SMihai Caraman void kvmppc_decrementer_func(struct kvm_vcpu *vcpu)
1892dfd4d47eSScott Wood {
189321bd000aSBharat Bhushan 	if (vcpu->arch.tcr & TCR_ARE) {
189421bd000aSBharat Bhushan 		vcpu->arch.dec = vcpu->arch.decar;
189521bd000aSBharat Bhushan 		kvmppc_emulate_dec(vcpu);
189621bd000aSBharat Bhushan 	}
189721bd000aSBharat Bhushan 
1898dfd4d47eSScott Wood 	kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1899dfd4d47eSScott Wood }
1900dfd4d47eSScott Wood 
kvmppc_booke_add_breakpoint(struct debug_reg * dbg_reg,uint64_t addr,int index)1901ce11e48bSBharat Bhushan static int kvmppc_booke_add_breakpoint(struct debug_reg *dbg_reg,
1902ce11e48bSBharat Bhushan 				       uint64_t addr, int index)
1903ce11e48bSBharat Bhushan {
1904ce11e48bSBharat Bhushan 	switch (index) {
1905ce11e48bSBharat Bhushan 	case 0:
1906ce11e48bSBharat Bhushan 		dbg_reg->dbcr0 |= DBCR0_IAC1;
1907ce11e48bSBharat Bhushan 		dbg_reg->iac1 = addr;
1908ce11e48bSBharat Bhushan 		break;
1909ce11e48bSBharat Bhushan 	case 1:
1910ce11e48bSBharat Bhushan 		dbg_reg->dbcr0 |= DBCR0_IAC2;
1911ce11e48bSBharat Bhushan 		dbg_reg->iac2 = addr;
1912ce11e48bSBharat Bhushan 		break;
1913ce11e48bSBharat Bhushan #if CONFIG_PPC_ADV_DEBUG_IACS > 2
1914ce11e48bSBharat Bhushan 	case 2:
1915ce11e48bSBharat Bhushan 		dbg_reg->dbcr0 |= DBCR0_IAC3;
1916ce11e48bSBharat Bhushan 		dbg_reg->iac3 = addr;
1917ce11e48bSBharat Bhushan 		break;
1918ce11e48bSBharat Bhushan 	case 3:
1919ce11e48bSBharat Bhushan 		dbg_reg->dbcr0 |= DBCR0_IAC4;
1920ce11e48bSBharat Bhushan 		dbg_reg->iac4 = addr;
1921ce11e48bSBharat Bhushan 		break;
1922ce11e48bSBharat Bhushan #endif
1923ce11e48bSBharat Bhushan 	default:
1924ce11e48bSBharat Bhushan 		return -EINVAL;
1925ce11e48bSBharat Bhushan 	}
1926ce11e48bSBharat Bhushan 
1927ce11e48bSBharat Bhushan 	dbg_reg->dbcr0 |= DBCR0_IDM;
1928ce11e48bSBharat Bhushan 	return 0;
1929ce11e48bSBharat Bhushan }
1930ce11e48bSBharat Bhushan 
kvmppc_booke_add_watchpoint(struct debug_reg * dbg_reg,uint64_t addr,int type,int index)1931ce11e48bSBharat Bhushan static int kvmppc_booke_add_watchpoint(struct debug_reg *dbg_reg, uint64_t addr,
1932ce11e48bSBharat Bhushan 				       int type, int index)
1933ce11e48bSBharat Bhushan {
1934ce11e48bSBharat Bhushan 	switch (index) {
1935ce11e48bSBharat Bhushan 	case 0:
1936ce11e48bSBharat Bhushan 		if (type & KVMPPC_DEBUG_WATCH_READ)
1937ce11e48bSBharat Bhushan 			dbg_reg->dbcr0 |= DBCR0_DAC1R;
1938ce11e48bSBharat Bhushan 		if (type & KVMPPC_DEBUG_WATCH_WRITE)
1939ce11e48bSBharat Bhushan 			dbg_reg->dbcr0 |= DBCR0_DAC1W;
1940ce11e48bSBharat Bhushan 		dbg_reg->dac1 = addr;
1941ce11e48bSBharat Bhushan 		break;
1942ce11e48bSBharat Bhushan 	case 1:
1943ce11e48bSBharat Bhushan 		if (type & KVMPPC_DEBUG_WATCH_READ)
1944ce11e48bSBharat Bhushan 			dbg_reg->dbcr0 |= DBCR0_DAC2R;
1945ce11e48bSBharat Bhushan 		if (type & KVMPPC_DEBUG_WATCH_WRITE)
1946ce11e48bSBharat Bhushan 			dbg_reg->dbcr0 |= DBCR0_DAC2W;
1947ce11e48bSBharat Bhushan 		dbg_reg->dac2 = addr;
1948ce11e48bSBharat Bhushan 		break;
1949ce11e48bSBharat Bhushan 	default:
1950ce11e48bSBharat Bhushan 		return -EINVAL;
1951ce11e48bSBharat Bhushan 	}
1952ce11e48bSBharat Bhushan 
1953ce11e48bSBharat Bhushan 	dbg_reg->dbcr0 |= DBCR0_IDM;
1954ce11e48bSBharat Bhushan 	return 0;
1955ce11e48bSBharat Bhushan }
kvm_guest_protect_msr(struct kvm_vcpu * vcpu,ulong prot_bitmap,bool set)1956e83ca8cfSSean Christopherson static void kvm_guest_protect_msr(struct kvm_vcpu *vcpu, ulong prot_bitmap,
1957e83ca8cfSSean Christopherson 				  bool set)
1958ce11e48bSBharat Bhushan {
1959ce11e48bSBharat Bhushan 	/* XXX: Add similar MSR protection for BookE-PR */
1960ce11e48bSBharat Bhushan #ifdef CONFIG_KVM_BOOKE_HV
1961ce11e48bSBharat Bhushan 	BUG_ON(prot_bitmap & ~(MSRP_UCLEP | MSRP_DEP | MSRP_PMMP));
1962ce11e48bSBharat Bhushan 	if (set) {
1963ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_UCLE)
1964ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp |= MSRP_UCLEP;
1965ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_DE)
1966ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp |= MSRP_DEP;
1967ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_PMM)
1968ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp |= MSRP_PMMP;
1969ce11e48bSBharat Bhushan 	} else {
1970ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_UCLE)
1971ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp &= ~MSRP_UCLEP;
1972ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_DE)
1973ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp &= ~MSRP_DEP;
1974ce11e48bSBharat Bhushan 		if (prot_bitmap & MSR_PMM)
1975ce11e48bSBharat Bhushan 			vcpu->arch.shadow_msrp &= ~MSRP_PMMP;
1976ce11e48bSBharat Bhushan 	}
1977ce11e48bSBharat Bhushan #endif
1978ce11e48bSBharat Bhushan }
1979ce11e48bSBharat Bhushan 
kvmppc_xlate(struct kvm_vcpu * vcpu,ulong eaddr,enum xlate_instdata xlid,enum xlate_readwrite xlrw,struct kvmppc_pte * pte)19807d15c06fSAlexander Graf int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, enum xlate_instdata xlid,
19817d15c06fSAlexander Graf 		 enum xlate_readwrite xlrw, struct kvmppc_pte *pte)
19827d15c06fSAlexander Graf {
19837d15c06fSAlexander Graf 	int gtlb_index;
19847d15c06fSAlexander Graf 	gpa_t gpaddr;
19857d15c06fSAlexander Graf 
19867d15c06fSAlexander Graf #ifdef CONFIG_KVM_E500V2
19877d15c06fSAlexander Graf 	if (!(vcpu->arch.shared->msr & MSR_PR) &&
19887d15c06fSAlexander Graf 	    (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
19897d15c06fSAlexander Graf 		pte->eaddr = eaddr;
19907d15c06fSAlexander Graf 		pte->raddr = (vcpu->arch.magic_page_pa & PAGE_MASK) |
19917d15c06fSAlexander Graf 			     (eaddr & ~PAGE_MASK);
19927d15c06fSAlexander Graf 		pte->vpage = eaddr >> PAGE_SHIFT;
19937d15c06fSAlexander Graf 		pte->may_read = true;
19947d15c06fSAlexander Graf 		pte->may_write = true;
19957d15c06fSAlexander Graf 		pte->may_execute = true;
19967d15c06fSAlexander Graf 
19977d15c06fSAlexander Graf 		return 0;
19987d15c06fSAlexander Graf 	}
19997d15c06fSAlexander Graf #endif
20007d15c06fSAlexander Graf 
20017d15c06fSAlexander Graf 	/* Check the guest TLB. */
20027d15c06fSAlexander Graf 	switch (xlid) {
20037d15c06fSAlexander Graf 	case XLATE_INST:
20047d15c06fSAlexander Graf 		gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
20057d15c06fSAlexander Graf 		break;
20067d15c06fSAlexander Graf 	case XLATE_DATA:
20077d15c06fSAlexander Graf 		gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
20087d15c06fSAlexander Graf 		break;
20097d15c06fSAlexander Graf 	default:
20107d15c06fSAlexander Graf 		BUG();
20117d15c06fSAlexander Graf 	}
20127d15c06fSAlexander Graf 
20137d15c06fSAlexander Graf 	/* Do we have a TLB entry at all? */
20147d15c06fSAlexander Graf 	if (gtlb_index < 0)
20157d15c06fSAlexander Graf 		return -ENOENT;
20167d15c06fSAlexander Graf 
20177d15c06fSAlexander Graf 	gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
20187d15c06fSAlexander Graf 
20197d15c06fSAlexander Graf 	pte->eaddr = eaddr;
20207d15c06fSAlexander Graf 	pte->raddr = (gpaddr & PAGE_MASK) | (eaddr & ~PAGE_MASK);
20217d15c06fSAlexander Graf 	pte->vpage = eaddr >> PAGE_SHIFT;
20227d15c06fSAlexander Graf 
20237d15c06fSAlexander Graf 	/* XXX read permissions from the guest TLB */
20247d15c06fSAlexander Graf 	pte->may_read = true;
20257d15c06fSAlexander Graf 	pte->may_write = true;
20267d15c06fSAlexander Graf 	pte->may_execute = true;
20277d15c06fSAlexander Graf 
20287d15c06fSAlexander Graf 	return 0;
20297d15c06fSAlexander Graf }
20307d15c06fSAlexander Graf 
kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu * vcpu,struct kvm_guest_debug * dbg)2031ce11e48bSBharat Bhushan int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
2032ce11e48bSBharat Bhushan 					 struct kvm_guest_debug *dbg)
2033ce11e48bSBharat Bhushan {
2034ce11e48bSBharat Bhushan 	struct debug_reg *dbg_reg;
2035ce11e48bSBharat Bhushan 	int n, b = 0, w = 0;
203666b56562SChristoffer Dall 	int ret = 0;
203766b56562SChristoffer Dall 
203866b56562SChristoffer Dall 	vcpu_load(vcpu);
2039ce11e48bSBharat Bhushan 
2040ce11e48bSBharat Bhushan 	if (!(dbg->control & KVM_GUESTDBG_ENABLE)) {
2041348ba710SBharat Bhushan 		vcpu->arch.dbg_reg.dbcr0 = 0;
2042ce11e48bSBharat Bhushan 		vcpu->guest_debug = 0;
2043ce11e48bSBharat Bhushan 		kvm_guest_protect_msr(vcpu, MSR_DE, false);
204466b56562SChristoffer Dall 		goto out;
2045ce11e48bSBharat Bhushan 	}
2046ce11e48bSBharat Bhushan 
2047ce11e48bSBharat Bhushan 	kvm_guest_protect_msr(vcpu, MSR_DE, true);
2048ce11e48bSBharat Bhushan 	vcpu->guest_debug = dbg->control;
2049348ba710SBharat Bhushan 	vcpu->arch.dbg_reg.dbcr0 = 0;
2050ce11e48bSBharat Bhushan 
2051ce11e48bSBharat Bhushan 	if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
2052348ba710SBharat Bhushan 		vcpu->arch.dbg_reg.dbcr0 |= DBCR0_IDM | DBCR0_IC;
2053ce11e48bSBharat Bhushan 
2054ce11e48bSBharat Bhushan 	/* Code below handles only HW breakpoints */
2055348ba710SBharat Bhushan 	dbg_reg = &(vcpu->arch.dbg_reg);
2056ce11e48bSBharat Bhushan 
2057ce11e48bSBharat Bhushan #ifdef CONFIG_KVM_BOOKE_HV
2058ce11e48bSBharat Bhushan 	/*
2059ce11e48bSBharat Bhushan 	 * On BookE-HV (e500mc) the guest is always executed with MSR.GS=1
2060ce11e48bSBharat Bhushan 	 * DBCR1 and DBCR2 are set to trigger debug events when MSR.PR is 0
2061ce11e48bSBharat Bhushan 	 */
2062ce11e48bSBharat Bhushan 	dbg_reg->dbcr1 = 0;
2063ce11e48bSBharat Bhushan 	dbg_reg->dbcr2 = 0;
2064ce11e48bSBharat Bhushan #else
2065ce11e48bSBharat Bhushan 	/*
2066ce11e48bSBharat Bhushan 	 * On BookE-PR (e500v2) the guest is always executed with MSR.PR=1
2067ce11e48bSBharat Bhushan 	 * We set DBCR1 and DBCR2 to only trigger debug events when MSR.PR
2068ce11e48bSBharat Bhushan 	 * is set.
2069ce11e48bSBharat Bhushan 	 */
2070ce11e48bSBharat Bhushan 	dbg_reg->dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US | DBCR1_IAC3US |
2071ce11e48bSBharat Bhushan 			  DBCR1_IAC4US;
2072ce11e48bSBharat Bhushan 	dbg_reg->dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
2073ce11e48bSBharat Bhushan #endif
2074ce11e48bSBharat Bhushan 
2075ce11e48bSBharat Bhushan 	if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
207666b56562SChristoffer Dall 		goto out;
2077ce11e48bSBharat Bhushan 
207866b56562SChristoffer Dall 	ret = -EINVAL;
2079ce11e48bSBharat Bhushan 	for (n = 0; n < (KVMPPC_BOOKE_IAC_NUM + KVMPPC_BOOKE_DAC_NUM); n++) {
2080ce11e48bSBharat Bhushan 		uint64_t addr = dbg->arch.bp[n].addr;
2081ce11e48bSBharat Bhushan 		uint32_t type = dbg->arch.bp[n].type;
2082ce11e48bSBharat Bhushan 
2083ce11e48bSBharat Bhushan 		if (type == KVMPPC_DEBUG_NONE)
2084ce11e48bSBharat Bhushan 			continue;
2085ce11e48bSBharat Bhushan 
2086ac0e89bbSDan Carpenter 		if (type & ~(KVMPPC_DEBUG_WATCH_READ |
2087ce11e48bSBharat Bhushan 			     KVMPPC_DEBUG_WATCH_WRITE |
2088ce11e48bSBharat Bhushan 			     KVMPPC_DEBUG_BREAKPOINT))
208966b56562SChristoffer Dall 			goto out;
2090ce11e48bSBharat Bhushan 
2091ce11e48bSBharat Bhushan 		if (type & KVMPPC_DEBUG_BREAKPOINT) {
2092ce11e48bSBharat Bhushan 			/* Setting H/W breakpoint */
2093ce11e48bSBharat Bhushan 			if (kvmppc_booke_add_breakpoint(dbg_reg, addr, b++))
209466b56562SChristoffer Dall 				goto out;
2095ce11e48bSBharat Bhushan 		} else {
2096ce11e48bSBharat Bhushan 			/* Setting H/W watchpoint */
2097ce11e48bSBharat Bhushan 			if (kvmppc_booke_add_watchpoint(dbg_reg, addr,
2098ce11e48bSBharat Bhushan 							type, w++))
209966b56562SChristoffer Dall 				goto out;
2100ce11e48bSBharat Bhushan 		}
2101ce11e48bSBharat Bhushan 	}
2102ce11e48bSBharat Bhushan 
210366b56562SChristoffer Dall 	ret = 0;
210466b56562SChristoffer Dall out:
210566b56562SChristoffer Dall 	vcpu_put(vcpu);
210666b56562SChristoffer Dall 	return ret;
2107ce11e48bSBharat Bhushan }
2108ce11e48bSBharat Bhushan 
kvmppc_booke_vcpu_load(struct kvm_vcpu * vcpu,int cpu)210994fa9d99SScott Wood void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
211094fa9d99SScott Wood {
2111a47d72f3SPaul Mackerras 	vcpu->cpu = smp_processor_id();
2112d30f6e48SScott Wood 	current->thread.kvm_vcpu = vcpu;
211394fa9d99SScott Wood }
211494fa9d99SScott Wood 
kvmppc_booke_vcpu_put(struct kvm_vcpu * vcpu)211594fa9d99SScott Wood void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
211694fa9d99SScott Wood {
2117d30f6e48SScott Wood 	current->thread.kvm_vcpu = NULL;
2118a47d72f3SPaul Mackerras 	vcpu->cpu = -1;
2119ce11e48bSBharat Bhushan 
2120ce11e48bSBharat Bhushan 	/* Clear pending debug event in DBSR */
2121ce11e48bSBharat Bhushan 	kvmppc_clear_dbsr();
212294fa9d99SScott Wood }
212394fa9d99SScott Wood 
kvmppc_core_init_vm(struct kvm * kvm)21243a167beaSAneesh Kumar K.V int kvmppc_core_init_vm(struct kvm *kvm)
21253a167beaSAneesh Kumar K.V {
2126cbbc58d4SAneesh Kumar K.V 	return kvm->arch.kvm_ops->init_vm(kvm);
21273a167beaSAneesh Kumar K.V }
21283a167beaSAneesh Kumar K.V 
kvmppc_core_vcpu_create(struct kvm_vcpu * vcpu)2129ff030fdfSSean Christopherson int kvmppc_core_vcpu_create(struct kvm_vcpu *vcpu)
21303a167beaSAneesh Kumar K.V {
2131b3d42c98SSean Christopherson 	int i;
2132b3d42c98SSean Christopherson 	int r;
2133b3d42c98SSean Christopherson 
2134b3d42c98SSean Christopherson 	r = vcpu->kvm->arch.kvm_ops->vcpu_create(vcpu);
2135b3d42c98SSean Christopherson 	if (r)
2136b3d42c98SSean Christopherson 		return r;
2137b3d42c98SSean Christopherson 
2138b3d42c98SSean Christopherson 	/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
2139b3d42c98SSean Christopherson 	vcpu->arch.regs.nip = 0;
2140b3d42c98SSean Christopherson 	vcpu->arch.shared->pir = vcpu->vcpu_id;
2141b3d42c98SSean Christopherson 	kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
2142b3d42c98SSean Christopherson 	kvmppc_set_msr(vcpu, 0);
2143b3d42c98SSean Christopherson 
2144b3d42c98SSean Christopherson #ifndef CONFIG_KVM_BOOKE_HV
2145b3d42c98SSean Christopherson 	vcpu->arch.shadow_msr = MSR_USER | MSR_IS | MSR_DS;
2146b3d42c98SSean Christopherson 	vcpu->arch.shadow_pid = 1;
2147b3d42c98SSean Christopherson 	vcpu->arch.shared->msr = 0;
2148b3d42c98SSean Christopherson #endif
2149b3d42c98SSean Christopherson 
2150b3d42c98SSean Christopherson 	/* Eye-catching numbers so we know if the guest takes an interrupt
2151b3d42c98SSean Christopherson 	 * before it's programmed its own IVPR/IVORs. */
2152b3d42c98SSean Christopherson 	vcpu->arch.ivpr = 0x55550000;
2153b3d42c98SSean Christopherson 	for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
2154b3d42c98SSean Christopherson 		vcpu->arch.ivor[i] = 0x7700 | i * 4;
2155b3d42c98SSean Christopherson 
2156b3d42c98SSean Christopherson 	kvmppc_init_timing_stats(vcpu);
2157b3d42c98SSean Christopherson 
2158b3d42c98SSean Christopherson 	r = kvmppc_core_vcpu_setup(vcpu);
2159b3d42c98SSean Christopherson 	if (r)
2160b3d42c98SSean Christopherson 		vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
2161b3d42c98SSean Christopherson 	kvmppc_sanity_check(vcpu);
2162b3d42c98SSean Christopherson 	return r;
21633a167beaSAneesh Kumar K.V }
21643a167beaSAneesh Kumar K.V 
kvmppc_core_vcpu_free(struct kvm_vcpu * vcpu)21653a167beaSAneesh Kumar K.V void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
21663a167beaSAneesh Kumar K.V {
2167cbbc58d4SAneesh Kumar K.V 	vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
21683a167beaSAneesh Kumar K.V }
21693a167beaSAneesh Kumar K.V 
kvmppc_core_destroy_vm(struct kvm * kvm)21703a167beaSAneesh Kumar K.V void kvmppc_core_destroy_vm(struct kvm *kvm)
21713a167beaSAneesh Kumar K.V {
2172cbbc58d4SAneesh Kumar K.V 	kvm->arch.kvm_ops->destroy_vm(kvm);
21733a167beaSAneesh Kumar K.V }
21743a167beaSAneesh Kumar K.V 
kvmppc_core_vcpu_load(struct kvm_vcpu * vcpu,int cpu)21753a167beaSAneesh Kumar K.V void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
21763a167beaSAneesh Kumar K.V {
2177cbbc58d4SAneesh Kumar K.V 	vcpu->kvm->arch.kvm_ops->vcpu_load(vcpu, cpu);
21783a167beaSAneesh Kumar K.V }
21793a167beaSAneesh Kumar K.V 
kvmppc_core_vcpu_put(struct kvm_vcpu * vcpu)21803a167beaSAneesh Kumar K.V void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
21813a167beaSAneesh Kumar K.V {
2182cbbc58d4SAneesh Kumar K.V 	vcpu->kvm->arch.kvm_ops->vcpu_put(vcpu);
2183d9fbd03dSHollis Blanchard }
2184d9fbd03dSHollis Blanchard 
kvmppc_booke_init(void)2185d9fbd03dSHollis Blanchard int __init kvmppc_booke_init(void)
2186d9fbd03dSHollis Blanchard {
2187d30f6e48SScott Wood #ifndef CONFIG_KVM_BOOKE_HV
2188d9fbd03dSHollis Blanchard 	unsigned long ivor[16];
21891d542d9cSBharat Bhushan 	unsigned long *handler = kvmppc_booke_handler_addr;
2190d9fbd03dSHollis Blanchard 	unsigned long max_ivor = 0;
21911d542d9cSBharat Bhushan 	unsigned long handler_len;
2192d9fbd03dSHollis Blanchard 	int i;
2193d9fbd03dSHollis Blanchard 
2194d9fbd03dSHollis Blanchard 	/* We install our own exception handlers by hijacking IVPR. IVPR must
2195d9fbd03dSHollis Blanchard 	 * be 16-bit aligned, so we need a 64KB allocation. */
2196d9fbd03dSHollis Blanchard 	kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
2197d9fbd03dSHollis Blanchard 	                                         VCPU_SIZE_ORDER);
2198d9fbd03dSHollis Blanchard 	if (!kvmppc_booke_handlers)
2199d9fbd03dSHollis Blanchard 		return -ENOMEM;
2200d9fbd03dSHollis Blanchard 
2201d9fbd03dSHollis Blanchard 	/* XXX make sure our handlers are smaller than Linux's */
2202d9fbd03dSHollis Blanchard 
2203d9fbd03dSHollis Blanchard 	/* Copy our interrupt handlers to match host IVORs. That way we don't
2204d9fbd03dSHollis Blanchard 	 * have to swap the IVORs on every guest/host transition. */
2205d9fbd03dSHollis Blanchard 	ivor[0] = mfspr(SPRN_IVOR0);
2206d9fbd03dSHollis Blanchard 	ivor[1] = mfspr(SPRN_IVOR1);
2207d9fbd03dSHollis Blanchard 	ivor[2] = mfspr(SPRN_IVOR2);
2208d9fbd03dSHollis Blanchard 	ivor[3] = mfspr(SPRN_IVOR3);
2209d9fbd03dSHollis Blanchard 	ivor[4] = mfspr(SPRN_IVOR4);
2210d9fbd03dSHollis Blanchard 	ivor[5] = mfspr(SPRN_IVOR5);
2211d9fbd03dSHollis Blanchard 	ivor[6] = mfspr(SPRN_IVOR6);
2212d9fbd03dSHollis Blanchard 	ivor[7] = mfspr(SPRN_IVOR7);
2213d9fbd03dSHollis Blanchard 	ivor[8] = mfspr(SPRN_IVOR8);
2214d9fbd03dSHollis Blanchard 	ivor[9] = mfspr(SPRN_IVOR9);
2215d9fbd03dSHollis Blanchard 	ivor[10] = mfspr(SPRN_IVOR10);
2216d9fbd03dSHollis Blanchard 	ivor[11] = mfspr(SPRN_IVOR11);
2217d9fbd03dSHollis Blanchard 	ivor[12] = mfspr(SPRN_IVOR12);
2218d9fbd03dSHollis Blanchard 	ivor[13] = mfspr(SPRN_IVOR13);
2219d9fbd03dSHollis Blanchard 	ivor[14] = mfspr(SPRN_IVOR14);
2220d9fbd03dSHollis Blanchard 	ivor[15] = mfspr(SPRN_IVOR15);
2221d9fbd03dSHollis Blanchard 
2222d9fbd03dSHollis Blanchard 	for (i = 0; i < 16; i++) {
2223d9fbd03dSHollis Blanchard 		if (ivor[i] > max_ivor)
22241d542d9cSBharat Bhushan 			max_ivor = i;
2225d9fbd03dSHollis Blanchard 
22261d542d9cSBharat Bhushan 		handler_len = handler[i + 1] - handler[i];
2227d9fbd03dSHollis Blanchard 		memcpy((void *)kvmppc_booke_handlers + ivor[i],
22281d542d9cSBharat Bhushan 		       (void *)handler[i], handler_len);
2229d9fbd03dSHollis Blanchard 	}
22301d542d9cSBharat Bhushan 
22311d542d9cSBharat Bhushan 	handler_len = handler[max_ivor + 1] - handler[max_ivor];
22321d542d9cSBharat Bhushan 	flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers +
22331d542d9cSBharat Bhushan 			   ivor[max_ivor] + handler_len);
2234d30f6e48SScott Wood #endif /* !BOOKE_HV */
2235db93f574SHollis Blanchard 	return 0;
2236d9fbd03dSHollis Blanchard }
2237d9fbd03dSHollis Blanchard 
kvmppc_booke_exit(void)2238db93f574SHollis Blanchard void __exit kvmppc_booke_exit(void)
2239d9fbd03dSHollis Blanchard {
2240d9fbd03dSHollis Blanchard 	free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
2241d9fbd03dSHollis Blanchard 	kvm_exit();
2242d9fbd03dSHollis Blanchard }
2243