blob: a9ca016da67021691de512d0a8a2f9f0039d56e5 [file] [log] [blame]
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 *
15 * Copyright IBM Corp. 2007
Scott Wood4cd35f62011-06-14 18:34:31 -050016 * Copyright 2010-2011 Freescale Semiconductor, Inc.
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050017 *
18 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
19 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
Scott Woodd30f6e42011-12-20 15:34:43 +000020 * Scott Wood <scottwood@freescale.com>
21 * Varun Sethi <varun.sethi@freescale.com>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050022 */
23
24#include <linux/errno.h>
25#include <linux/err.h>
26#include <linux/kvm_host.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/gfp.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050028#include <linux/module.h>
29#include <linux/vmalloc.h>
30#include <linux/fs.h>
Hollis Blanchard7924bd42008-12-02 15:51:55 -060031
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050032#include <asm/cputable.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080033#include <linux/uaccess.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050034#include <asm/kvm_ppc.h>
Hollis Blanchardd9fbd032008-11-05 09:36:13 -060035#include <asm/cacheflush.h>
Scott Woodd30f6e42011-12-20 15:34:43 +000036#include <asm/dbell.h>
37#include <asm/hw_irq.h>
38#include <asm/irq.h>
Mihai Caramanb50df192012-10-11 06:13:19 +000039#include <asm/time.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050040
Scott Woodd30f6e42011-12-20 15:34:43 +000041#include "timing.h"
Hollis Blanchard75f74f02008-11-05 09:36:16 -060042#include "booke.h"
Aneesh Kumar K.Vdba291f2013-10-07 22:17:58 +053043
44#define CREATE_TRACE_POINTS
45#include "trace_booke.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050046
Hollis Blanchardd9fbd032008-11-05 09:36:13 -060047unsigned long kvmppc_booke_handlers;
48
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050049#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
50#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
51
52struct kvm_stats_debugfs_item debugfs_entries[] = {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050053 { "mmio", VCPU_STAT(mmio_exits) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050054 { "sig", VCPU_STAT(signal_exits) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050055 { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
56 { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
57 { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
58 { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
59 { "sysc", VCPU_STAT(syscall_exits) },
60 { "isi", VCPU_STAT(isi_exits) },
61 { "dsi", VCPU_STAT(dsi_exits) },
62 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
63 { "dec", VCPU_STAT(dec_exits) },
64 { "ext_intr", VCPU_STAT(ext_intr_exits) },
Paolo Bonzinif7819512015-02-04 18:20:58 +010065 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
Paolo Bonzini62bea5b2015-09-15 18:27:57 +020066 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
Christian Borntraeger3491caf2016-05-13 12:16:35 +020067 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
Hollis Blanchard45c5eb62008-04-25 17:55:49 -050068 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Scott Woodd30f6e42011-12-20 15:34:43 +000069 { "doorbell", VCPU_STAT(dbell_exits) },
70 { "guest doorbell", VCPU_STAT(gdbell_exits) },
Alexander Grafcf1c5ca2012-08-01 12:56:51 +020071 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050072 { NULL }
73};
74
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050075/* TODO: use vcpu_printf() */
76void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
77{
78 int i;
79
Simon Guo173c5202018-05-07 14:20:08 +080080 printk("pc: %08lx msr: %08llx\n", vcpu->arch.regs.nip,
81 vcpu->arch.shared->msr);
82 printk("lr: %08lx ctr: %08lx\n", vcpu->arch.regs.link,
83 vcpu->arch.regs.ctr);
Alexander Grafde7906c2010-07-29 14:47:46 +020084 printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
85 vcpu->arch.shared->srr1);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050086
87 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
88
89 for (i = 0; i < 32; i += 4) {
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060090 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
Alexander Graf8e5b26b2010-01-08 02:58:01 +010091 kvmppc_get_gpr(vcpu, i),
92 kvmppc_get_gpr(vcpu, i+1),
93 kvmppc_get_gpr(vcpu, i+2),
94 kvmppc_get_gpr(vcpu, i+3));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050095 }
96}
97
Scott Wood4cd35f62011-06-14 18:34:31 -050098#ifdef CONFIG_SPE
99void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
100{
101 preempt_disable();
102 enable_kernel_spe();
103 kvmppc_save_guest_spe(vcpu);
Anton Blancharddc4fbba2015-10-29 11:44:05 +1100104 disable_kernel_spe();
Scott Wood4cd35f62011-06-14 18:34:31 -0500105 vcpu->arch.shadow_msr &= ~MSR_SPE;
106 preempt_enable();
107}
108
109static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
110{
111 preempt_disable();
112 enable_kernel_spe();
113 kvmppc_load_guest_spe(vcpu);
Anton Blancharddc4fbba2015-10-29 11:44:05 +1100114 disable_kernel_spe();
Scott Wood4cd35f62011-06-14 18:34:31 -0500115 vcpu->arch.shadow_msr |= MSR_SPE;
116 preempt_enable();
117}
118
119static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
120{
121 if (vcpu->arch.shared->msr & MSR_SPE) {
122 if (!(vcpu->arch.shadow_msr & MSR_SPE))
123 kvmppc_vcpu_enable_spe(vcpu);
124 } else if (vcpu->arch.shadow_msr & MSR_SPE) {
125 kvmppc_vcpu_disable_spe(vcpu);
126 }
127}
128#else
129static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
130{
131}
132#endif
133
Mihai Caraman3efc7da2014-08-20 16:36:22 +0300134/*
135 * Load up guest vcpu FP state if it's needed.
136 * It also set the MSR_FP in thread so that host know
137 * we're holding FPU, and then host can help to save
138 * guest vcpu FP state if other threads require to use FPU.
139 * This simulates an FP unavailable fault.
140 *
141 * It requires to be called with preemption disabled.
142 */
143static inline void kvmppc_load_guest_fp(struct kvm_vcpu *vcpu)
144{
145#ifdef CONFIG_PPC_FPU
146 if (!(current->thread.regs->msr & MSR_FP)) {
147 enable_kernel_fp();
148 load_fp_state(&vcpu->arch.fp);
Anton Blancharddc4fbba2015-10-29 11:44:05 +1100149 disable_kernel_fp();
Mihai Caraman3efc7da2014-08-20 16:36:22 +0300150 current->thread.fp_save_area = &vcpu->arch.fp;
151 current->thread.regs->msr |= MSR_FP;
152 }
153#endif
154}
155
156/*
157 * Save guest vcpu FP state into thread.
158 * It requires to be called with preemption disabled.
159 */
160static inline void kvmppc_save_guest_fp(struct kvm_vcpu *vcpu)
161{
162#ifdef CONFIG_PPC_FPU
163 if (current->thread.regs->msr & MSR_FP)
164 giveup_fpu(current);
165 current->thread.fp_save_area = NULL;
166#endif
167}
168
Alexander Graf7a08c272012-08-16 13:10:16 +0200169static void kvmppc_vcpu_sync_fpu(struct kvm_vcpu *vcpu)
170{
171#if defined(CONFIG_PPC_FPU) && !defined(CONFIG_KVM_BOOKE_HV)
172 /* We always treat the FP bit as enabled from the host
173 perspective, so only need to adjust the shadow MSR */
174 vcpu->arch.shadow_msr &= ~MSR_FP;
175 vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_FP;
176#endif
177}
178
Mihai Caraman95d80a22014-08-20 16:36:23 +0300179/*
180 * Simulate AltiVec unavailable fault to load guest state
181 * from thread to AltiVec unit.
182 * It requires to be called with preemption disabled.
183 */
184static inline void kvmppc_load_guest_altivec(struct kvm_vcpu *vcpu)
185{
186#ifdef CONFIG_ALTIVEC
187 if (cpu_has_feature(CPU_FTR_ALTIVEC)) {
188 if (!(current->thread.regs->msr & MSR_VEC)) {
189 enable_kernel_altivec();
190 load_vr_state(&vcpu->arch.vr);
Anton Blancharddc4fbba2015-10-29 11:44:05 +1100191 disable_kernel_altivec();
Mihai Caraman95d80a22014-08-20 16:36:23 +0300192 current->thread.vr_save_area = &vcpu->arch.vr;
193 current->thread.regs->msr |= MSR_VEC;
194 }
195 }
196#endif
197}
198
199/*
200 * Save guest vcpu AltiVec state into thread.
201 * It requires to be called with preemption disabled.
202 */
203static inline void kvmppc_save_guest_altivec(struct kvm_vcpu *vcpu)
204{
205#ifdef CONFIG_ALTIVEC
206 if (cpu_has_feature(CPU_FTR_ALTIVEC)) {
207 if (current->thread.regs->msr & MSR_VEC)
208 giveup_altivec(current);
209 current->thread.vr_save_area = NULL;
210 }
211#endif
212}
213
Bharat Bhushance11e482013-07-04 12:27:47 +0530214static void kvmppc_vcpu_sync_debug(struct kvm_vcpu *vcpu)
215{
216 /* Synchronize guest's desire to get debug interrupts into shadow MSR */
217#ifndef CONFIG_KVM_BOOKE_HV
218 vcpu->arch.shadow_msr &= ~MSR_DE;
219 vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_DE;
220#endif
221
222 /* Force enable debug interrupts when user space wants to debug */
223 if (vcpu->guest_debug) {
224#ifdef CONFIG_KVM_BOOKE_HV
225 /*
226 * Since there is no shadow MSR, sync MSR_DE into the guest
227 * visible MSR.
228 */
229 vcpu->arch.shared->msr |= MSR_DE;
230#else
231 vcpu->arch.shadow_msr |= MSR_DE;
232 vcpu->arch.shared->msr &= ~MSR_DE;
233#endif
234 }
235}
236
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500237/*
238 * Helper function for "full" MSR writes. No need to call this if only
239 * EE/CE/ME/DE/RI are changing.
240 */
Scott Wood4cd35f62011-06-14 18:34:31 -0500241void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
242{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500243 u32 old_msr = vcpu->arch.shared->msr;
Scott Wood4cd35f62011-06-14 18:34:31 -0500244
Scott Woodd30f6e42011-12-20 15:34:43 +0000245#ifdef CONFIG_KVM_BOOKE_HV
246 new_msr |= MSR_GS;
247#endif
248
Scott Wood4cd35f62011-06-14 18:34:31 -0500249 vcpu->arch.shared->msr = new_msr;
250
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500251 kvmppc_mmu_msr_notify(vcpu, old_msr);
Scott Wood4cd35f62011-06-14 18:34:31 -0500252 kvmppc_vcpu_sync_spe(vcpu);
Alexander Graf7a08c272012-08-16 13:10:16 +0200253 kvmppc_vcpu_sync_fpu(vcpu);
Bharat Bhushance11e482013-07-04 12:27:47 +0530254 kvmppc_vcpu_sync_debug(vcpu);
Scott Wood4cd35f62011-06-14 18:34:31 -0500255}
256
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600257static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
258 unsigned int priority)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600259{
Alexander Graf63460462012-08-08 00:44:52 +0200260 trace_kvm_booke_queue_irqprio(vcpu, priority);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600261 set_bit(priority, &vcpu->arch.pending_exceptions);
262}
263
Alexander Graf8de12012014-06-18 21:56:55 +0200264void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
265 ulong dear_flags, ulong esr_flags)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600266{
Liu Yudaf5e272010-02-02 19:44:35 +0800267 vcpu->arch.queued_dear = dear_flags;
268 vcpu->arch.queued_esr = esr_flags;
269 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
270}
271
Alexander Graf8de12012014-06-18 21:56:55 +0200272void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
273 ulong dear_flags, ulong esr_flags)
Liu Yudaf5e272010-02-02 19:44:35 +0800274{
275 vcpu->arch.queued_dear = dear_flags;
276 vcpu->arch.queued_esr = esr_flags;
277 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
278}
279
Alexander Graf8de12012014-06-18 21:56:55 +0200280void kvmppc_core_queue_itlb_miss(struct kvm_vcpu *vcpu)
281{
282 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
283}
284
285void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu, ulong esr_flags)
Liu Yudaf5e272010-02-02 19:44:35 +0800286{
287 vcpu->arch.queued_esr = esr_flags;
288 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
289}
290
Alexander Graf011da892013-01-31 14:17:38 +0100291static void kvmppc_core_queue_alignment(struct kvm_vcpu *vcpu, ulong dear_flags,
292 ulong esr_flags)
293{
294 vcpu->arch.queued_dear = dear_flags;
295 vcpu->arch.queued_esr = esr_flags;
296 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALIGNMENT);
297}
298
Liu Yudaf5e272010-02-02 19:44:35 +0800299void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
300{
301 vcpu->arch.queued_esr = esr_flags;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600302 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600303}
304
Paul Mackerras307d9272017-03-22 21:02:08 +1100305void kvmppc_core_queue_fpunavail(struct kvm_vcpu *vcpu)
306{
307 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
308}
309
Laurentiu Tudorb2d7ecb2018-04-26 15:33:19 +0300310#ifdef CONFIG_ALTIVEC
311void kvmppc_core_queue_vec_unavail(struct kvm_vcpu *vcpu)
312{
313 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALTIVEC_UNAVAIL);
314}
315#endif
316
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600317void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
318{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600319 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600320}
321
322int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
323{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600324 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600325}
326
Alexander Graf7706664d2009-12-21 20:21:24 +0100327void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
328{
329 clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
330}
331
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600332void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
333 struct kvm_interrupt *irq)
334{
Alexander Grafc5335f12010-08-30 14:03:24 +0200335 unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
336
337 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
338 prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
339
340 kvmppc_booke_queue_irqprio(vcpu, prio);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600341}
342
Paul Mackerras4fe27d22013-02-14 14:00:25 +0000343void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu)
Alexander Graf4496f972010-04-07 10:03:25 +0200344{
345 clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
Alexander Grafc5335f12010-08-30 14:03:24 +0200346 clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
Alexander Graf4496f972010-04-07 10:03:25 +0200347}
348
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000349static void kvmppc_core_queue_watchdog(struct kvm_vcpu *vcpu)
350{
351 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_WATCHDOG);
352}
353
354static void kvmppc_core_dequeue_watchdog(struct kvm_vcpu *vcpu)
355{
356 clear_bit(BOOKE_IRQPRIO_WATCHDOG, &vcpu->arch.pending_exceptions);
357}
358
Bharat Bhushan2f699a52014-08-13 14:39:44 +0530359void kvmppc_core_queue_debug(struct kvm_vcpu *vcpu)
360{
361 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DEBUG);
362}
363
364void kvmppc_core_dequeue_debug(struct kvm_vcpu *vcpu)
365{
366 clear_bit(BOOKE_IRQPRIO_DEBUG, &vcpu->arch.pending_exceptions);
367}
368
Scott Woodd30f6e42011-12-20 15:34:43 +0000369static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
370{
Bharat Bhushan31579ee2014-07-17 17:01:36 +0530371 kvmppc_set_srr0(vcpu, srr0);
372 kvmppc_set_srr1(vcpu, srr1);
Scott Woodd30f6e42011-12-20 15:34:43 +0000373}
374
375static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
376{
377 vcpu->arch.csrr0 = srr0;
378 vcpu->arch.csrr1 = srr1;
379}
380
381static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
382{
383 if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
384 vcpu->arch.dsrr0 = srr0;
385 vcpu->arch.dsrr1 = srr1;
386 } else {
387 set_guest_csrr(vcpu, srr0, srr1);
388 }
389}
390
391static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
392{
393 vcpu->arch.mcsrr0 = srr0;
394 vcpu->arch.mcsrr1 = srr1;
395}
396
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600397/* Deliver the interrupt of the corresponding priority, if possible. */
398static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
399 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500400{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600401 int allowed = 0;
Alexander Graf79300f82012-02-15 19:12:29 +0000402 ulong msr_mask = 0;
Alexander Graf1c810632013-01-04 18:12:48 +0100403 bool update_esr = false, update_dear = false, update_epr = false;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200404 ulong crit_raw = vcpu->arch.shared->critical;
405 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
406 bool crit;
Alexander Grafc5335f12010-08-30 14:03:24 +0200407 bool keep_irq = false;
Scott Woodd30f6e42011-12-20 15:34:43 +0000408 enum int_class int_class;
Mihai Caraman95e90b42012-10-11 06:13:26 +0000409 ulong new_msr = vcpu->arch.shared->msr;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200410
411 /* Truncate crit indicators in 32 bit mode */
412 if (!(vcpu->arch.shared->msr & MSR_SF)) {
413 crit_raw &= 0xffffffff;
414 crit_r1 &= 0xffffffff;
415 }
416
417 /* Critical section when crit == r1 */
418 crit = (crit_raw == crit_r1);
419 /* ... and we're in supervisor mode */
420 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500421
Alexander Grafc5335f12010-08-30 14:03:24 +0200422 if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
423 priority = BOOKE_IRQPRIO_EXTERNAL;
424 keep_irq = true;
425 }
426
Scott Wood5df554ad2013-04-12 14:08:46 +0000427 if ((priority == BOOKE_IRQPRIO_EXTERNAL) && vcpu->arch.epr_flags)
Alexander Graf1c810632013-01-04 18:12:48 +0100428 update_epr = true;
429
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600430 switch (priority) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600431 case BOOKE_IRQPRIO_DTLB_MISS:
Liu Yudaf5e272010-02-02 19:44:35 +0800432 case BOOKE_IRQPRIO_DATA_STORAGE:
Alexander Graf011da892013-01-31 14:17:38 +0100433 case BOOKE_IRQPRIO_ALIGNMENT:
Liu Yudaf5e272010-02-02 19:44:35 +0800434 update_dear = true;
435 /* fall through */
436 case BOOKE_IRQPRIO_INST_STORAGE:
437 case BOOKE_IRQPRIO_PROGRAM:
438 update_esr = true;
439 /* fall through */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600440 case BOOKE_IRQPRIO_ITLB_MISS:
441 case BOOKE_IRQPRIO_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600442 case BOOKE_IRQPRIO_FP_UNAVAIL:
Mihai Caraman95d80a22014-08-20 16:36:23 +0300443#ifdef CONFIG_SPE_POSSIBLE
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600444 case BOOKE_IRQPRIO_SPE_UNAVAIL:
445 case BOOKE_IRQPRIO_SPE_FP_DATA:
446 case BOOKE_IRQPRIO_SPE_FP_ROUND:
Mihai Caraman95d80a22014-08-20 16:36:23 +0300447#endif
448#ifdef CONFIG_ALTIVEC
449 case BOOKE_IRQPRIO_ALTIVEC_UNAVAIL:
450 case BOOKE_IRQPRIO_ALTIVEC_ASSIST:
451#endif
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600452 case BOOKE_IRQPRIO_AP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600453 allowed = 1;
Alexander Graf79300f82012-02-15 19:12:29 +0000454 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000455 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500456 break;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000457 case BOOKE_IRQPRIO_WATCHDOG:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600458 case BOOKE_IRQPRIO_CRITICAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000459 case BOOKE_IRQPRIO_DBELL_CRIT:
Alexander Graf666e7252010-07-29 14:47:43 +0200460 allowed = vcpu->arch.shared->msr & MSR_CE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000461 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000462 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000463 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500464 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600465 case BOOKE_IRQPRIO_MACHINE_CHECK:
Alexander Graf666e7252010-07-29 14:47:43 +0200466 allowed = vcpu->arch.shared->msr & MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000467 allowed = allowed && !crit;
Scott Woodd30f6e42011-12-20 15:34:43 +0000468 int_class = INT_CLASS_MC;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500469 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600470 case BOOKE_IRQPRIO_DECREMENTER:
471 case BOOKE_IRQPRIO_FIT:
Scott Wooddfd4d472011-11-17 12:39:59 +0000472 keep_irq = true;
473 /* fall through */
474 case BOOKE_IRQPRIO_EXTERNAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000475 case BOOKE_IRQPRIO_DBELL:
Alexander Graf666e7252010-07-29 14:47:43 +0200476 allowed = vcpu->arch.shared->msr & MSR_EE;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200477 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000478 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000479 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500480 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600481 case BOOKE_IRQPRIO_DEBUG:
Alexander Graf666e7252010-07-29 14:47:43 +0200482 allowed = vcpu->arch.shared->msr & MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000483 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000484 msr_mask = MSR_ME;
Bharat Bhushan9fee7562014-08-06 12:08:51 +0530485 if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC))
486 int_class = INT_CLASS_DBG;
487 else
488 int_class = INT_CLASS_CRIT;
489
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500490 break;
491 }
492
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600493 if (allowed) {
Scott Woodd30f6e42011-12-20 15:34:43 +0000494 switch (int_class) {
495 case INT_CLASS_NONCRIT:
Simon Guo173c5202018-05-07 14:20:08 +0800496 set_guest_srr(vcpu, vcpu->arch.regs.nip,
Scott Woodd30f6e42011-12-20 15:34:43 +0000497 vcpu->arch.shared->msr);
498 break;
499 case INT_CLASS_CRIT:
Simon Guo173c5202018-05-07 14:20:08 +0800500 set_guest_csrr(vcpu, vcpu->arch.regs.nip,
Scott Woodd30f6e42011-12-20 15:34:43 +0000501 vcpu->arch.shared->msr);
502 break;
503 case INT_CLASS_DBG:
Simon Guo173c5202018-05-07 14:20:08 +0800504 set_guest_dsrr(vcpu, vcpu->arch.regs.nip,
Scott Woodd30f6e42011-12-20 15:34:43 +0000505 vcpu->arch.shared->msr);
506 break;
507 case INT_CLASS_MC:
Simon Guo173c5202018-05-07 14:20:08 +0800508 set_guest_mcsrr(vcpu, vcpu->arch.regs.nip,
Scott Woodd30f6e42011-12-20 15:34:43 +0000509 vcpu->arch.shared->msr);
510 break;
511 }
512
Simon Guo173c5202018-05-07 14:20:08 +0800513 vcpu->arch.regs.nip = vcpu->arch.ivpr |
514 vcpu->arch.ivor[priority];
Liu Yudaf5e272010-02-02 19:44:35 +0800515 if (update_esr == true)
Bharat Bhushandc168542014-07-17 17:01:38 +0530516 kvmppc_set_esr(vcpu, vcpu->arch.queued_esr);
Liu Yudaf5e272010-02-02 19:44:35 +0800517 if (update_dear == true)
Bharat Bhushana5414d42014-07-17 17:01:37 +0530518 kvmppc_set_dar(vcpu, vcpu->arch.queued_dear);
Scott Wood5df554ad2013-04-12 14:08:46 +0000519 if (update_epr == true) {
520 if (vcpu->arch.epr_flags & KVMPPC_EPR_USER)
521 kvm_make_request(KVM_REQ_EPR_EXIT, vcpu);
Scott Woodeb1e4f42013-04-12 14:08:47 +0000522 else if (vcpu->arch.epr_flags & KVMPPC_EPR_KERNEL) {
523 BUG_ON(vcpu->arch.irq_type != KVMPPC_IRQ_MPIC);
524 kvmppc_mpic_set_epr(vcpu);
525 }
Scott Wood5df554ad2013-04-12 14:08:46 +0000526 }
Mihai Caraman95e90b42012-10-11 06:13:26 +0000527
528 new_msr &= msr_mask;
529#if defined(CONFIG_64BIT)
530 if (vcpu->arch.epcr & SPRN_EPCR_ICM)
531 new_msr |= MSR_CM;
532#endif
533 kvmppc_set_msr(vcpu, new_msr);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600534
Alexander Grafc5335f12010-08-30 14:03:24 +0200535 if (!keep_irq)
536 clear_bit(priority, &vcpu->arch.pending_exceptions);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600537 }
538
Scott Woodd30f6e42011-12-20 15:34:43 +0000539#ifdef CONFIG_KVM_BOOKE_HV
540 /*
541 * If an interrupt is pending but masked, raise a guest doorbell
542 * so that we are notified when the guest enables the relevant
543 * MSR bit.
544 */
545 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
546 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
547 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
548 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
549 if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
550 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
551#endif
552
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600553 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500554}
555
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000556/*
557 * Return the number of jiffies until the next timeout. If the timeout is
558 * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA
559 * because the larger value can break the timer APIs.
560 */
561static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu)
562{
563 u64 tb, wdt_tb, wdt_ticks = 0;
564 u64 nr_jiffies = 0;
565 u32 period = TCR_GET_WP(vcpu->arch.tcr);
566
567 wdt_tb = 1ULL << (63 - period);
568 tb = get_tb();
569 /*
570 * The watchdog timeout will hapeen when TB bit corresponding
571 * to watchdog will toggle from 0 to 1.
572 */
573 if (tb & wdt_tb)
574 wdt_ticks = wdt_tb;
575
576 wdt_ticks += wdt_tb - (tb & (wdt_tb - 1));
577
578 /* Convert timebase ticks to jiffies */
579 nr_jiffies = wdt_ticks;
580
581 if (do_div(nr_jiffies, tb_ticks_per_jiffy))
582 nr_jiffies++;
583
584 return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA);
585}
586
587static void arm_next_watchdog(struct kvm_vcpu *vcpu)
588{
589 unsigned long nr_jiffies;
590 unsigned long flags;
591
592 /*
593 * If TSR_ENW and TSR_WIS are not set then no need to exit to
594 * userspace, so clear the KVM_REQ_WATCHDOG request.
595 */
596 if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS))
Radim Krčmář72875d82017-04-26 22:32:19 +0200597 kvm_clear_request(KVM_REQ_WATCHDOG, vcpu);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000598
599 spin_lock_irqsave(&vcpu->arch.wdt_lock, flags);
600 nr_jiffies = watchdog_next_timeout(vcpu);
601 /*
602 * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA
603 * then do not run the watchdog timer as this can break timer APIs.
604 */
605 if (nr_jiffies < NEXT_TIMER_MAX_DELTA)
606 mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies);
607 else
608 del_timer(&vcpu->arch.wdt_timer);
609 spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags);
610}
611
Kees Cook86cb30e2017-10-17 20:21:24 -0700612void kvmppc_watchdog_func(struct timer_list *t)
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000613{
Kees Cook86cb30e2017-10-17 20:21:24 -0700614 struct kvm_vcpu *vcpu = from_timer(vcpu, t, arch.wdt_timer);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000615 u32 tsr, new_tsr;
616 int final;
617
618 do {
619 new_tsr = tsr = vcpu->arch.tsr;
620 final = 0;
621
622 /* Time out event */
623 if (tsr & TSR_ENW) {
624 if (tsr & TSR_WIS)
625 final = 1;
626 else
627 new_tsr = tsr | TSR_WIS;
628 } else {
629 new_tsr = tsr | TSR_ENW;
630 }
631 } while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr);
632
633 if (new_tsr & TSR_WIS) {
634 smp_wmb();
635 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
636 kvm_vcpu_kick(vcpu);
637 }
638
639 /*
640 * If this is final watchdog expiry and some action is required
641 * then exit to userspace.
642 */
643 if (final && (vcpu->arch.tcr & TCR_WRC_MASK) &&
644 vcpu->arch.watchdog_enabled) {
645 smp_wmb();
646 kvm_make_request(KVM_REQ_WATCHDOG, vcpu);
647 kvm_vcpu_kick(vcpu);
648 }
649
650 /*
651 * Stop running the watchdog timer after final expiration to
652 * prevent the host from being flooded with timers if the
653 * guest sets a short period.
654 * Timers will resume when TSR/TCR is updated next time.
655 */
656 if (!final)
657 arm_next_watchdog(vcpu);
658}
659
Scott Wooddfd4d472011-11-17 12:39:59 +0000660static void update_timer_ints(struct kvm_vcpu *vcpu)
661{
662 if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
663 kvmppc_core_queue_dec(vcpu);
664 else
665 kvmppc_core_dequeue_dec(vcpu);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000666
667 if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS))
668 kvmppc_core_queue_watchdog(vcpu);
669 else
670 kvmppc_core_dequeue_watchdog(vcpu);
Scott Wooddfd4d472011-11-17 12:39:59 +0000671}
672
Scott Woodc59a6a32011-11-08 18:23:25 -0600673static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500674{
675 unsigned long *pending = &vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500676 unsigned int priority;
677
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600678 priority = __ffs(*pending);
Alexander Graf8b3a00f2012-02-16 14:12:46 +0000679 while (priority < BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600680 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500681 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500682
683 priority = find_next_bit(pending,
684 BITS_PER_BYTE * sizeof(*pending),
685 priority + 1);
686 }
Alexander Graf90bba352010-07-29 14:47:51 +0200687
688 /* Tell the guest about our interrupt status */
Scott Wood29ac26e2011-11-08 18:23:27 -0600689 vcpu->arch.shared->int_pending = !!*pending;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500690}
691
Scott Woodc59a6a32011-11-08 18:23:25 -0600692/* Check pending exceptions and deliver one, if possible. */
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000693int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
Scott Woodc59a6a32011-11-08 18:23:25 -0600694{
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000695 int r = 0;
Scott Woodc59a6a32011-11-08 18:23:25 -0600696 WARN_ON_ONCE(!irqs_disabled());
697
698 kvmppc_core_check_exceptions(vcpu);
699
Radim Krčmář2fa6e1e2017-06-04 14:43:52 +0200700 if (kvm_request_pending(vcpu)) {
Alexander Grafb8c649a2012-12-20 04:52:39 +0000701 /* Exception delivery raised request; start over */
702 return 1;
703 }
704
Scott Woodc59a6a32011-11-08 18:23:25 -0600705 if (vcpu->arch.shared->msr & MSR_WE) {
706 local_irq_enable();
707 kvm_vcpu_block(vcpu);
Radim Krčmář72875d82017-04-26 22:32:19 +0200708 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
Scott Wood6c85f522014-01-09 19:18:40 -0600709 hard_irq_disable();
Scott Woodc59a6a32011-11-08 18:23:25 -0600710
711 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000712 r = 1;
Scott Woodc59a6a32011-11-08 18:23:25 -0600713 };
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000714
715 return r;
716}
717
Alexander Graf7c973a22012-08-13 12:50:35 +0200718int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
Alexander Graf4ffc6352012-08-08 20:31:13 +0200719{
Alexander Graf7c973a22012-08-13 12:50:35 +0200720 int r = 1; /* Indicate we want to get back into the guest */
721
Alexander Graf2d8185d2012-08-10 12:31:12 +0200722 if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
723 update_timer_ints(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200724#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Alexander Graf2d8185d2012-08-10 12:31:12 +0200725 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
726 kvmppc_core_flush_tlb(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200727#endif
Alexander Graf7c973a22012-08-13 12:50:35 +0200728
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000729 if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) {
730 vcpu->run->exit_reason = KVM_EXIT_WATCHDOG;
731 r = 0;
732 }
733
Alexander Graf1c810632013-01-04 18:12:48 +0100734 if (kvm_check_request(KVM_REQ_EPR_EXIT, vcpu)) {
735 vcpu->run->epr.epr = 0;
736 vcpu->arch.epr_needed = true;
737 vcpu->run->exit_reason = KVM_EXIT_EPR;
738 r = 0;
739 }
740
Alexander Graf7c973a22012-08-13 12:50:35 +0200741 return r;
Alexander Graf4ffc6352012-08-08 20:31:13 +0200742}
743
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000744int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
745{
Alexander Graf7ee78852012-08-13 12:44:41 +0200746 int ret, s;
Scott Woodf5f97212013-11-22 15:52:29 -0600747 struct debug_reg debug;
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000748
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200749 if (!vcpu->arch.sane) {
750 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
751 return -EINVAL;
752 }
753
Alexander Graf7ee78852012-08-13 12:44:41 +0200754 s = kvmppc_prepare_to_enter(vcpu);
755 if (s <= 0) {
Alexander Graf7ee78852012-08-13 12:44:41 +0200756 ret = s;
Scott Wood1d1ef222011-11-08 16:11:59 -0600757 goto out;
758 }
Scott Wood6c85f522014-01-09 19:18:40 -0600759 /* interrupts now hard-disabled */
Scott Wood1d1ef222011-11-08 16:11:59 -0600760
Scott Wood8fae8452011-12-20 15:34:45 +0000761#ifdef CONFIG_PPC_FPU
762 /* Save userspace FPU state in stack */
763 enable_kernel_fp();
Scott Wood8fae8452011-12-20 15:34:45 +0000764
765 /*
766 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
Mihai Caraman3efc7da2014-08-20 16:36:22 +0300767 * as always using the FPU.
Scott Wood8fae8452011-12-20 15:34:45 +0000768 */
Scott Wood8fae8452011-12-20 15:34:45 +0000769 kvmppc_load_guest_fp(vcpu);
770#endif
771
Mihai Caraman95d80a22014-08-20 16:36:23 +0300772#ifdef CONFIG_ALTIVEC
773 /* Save userspace AltiVec state in stack */
774 if (cpu_has_feature(CPU_FTR_ALTIVEC))
775 enable_kernel_altivec();
776 /*
777 * Since we can't trap on MSR_VEC in GS-mode, we consider the guest
778 * as always using the AltiVec.
779 */
780 kvmppc_load_guest_altivec(vcpu);
781#endif
782
Bharat Bhushance11e482013-07-04 12:27:47 +0530783 /* Switch to guest debug context */
Bharat Bhushan348ba712014-08-06 12:08:55 +0530784 debug = vcpu->arch.dbg_reg;
Scott Woodf5f97212013-11-22 15:52:29 -0600785 switch_booke_debug_regs(&debug);
786 debug = current->thread.debug;
Bharat Bhushan348ba712014-08-06 12:08:55 +0530787 current->thread.debug = vcpu->arch.dbg_reg;
Bharat Bhushance11e482013-07-04 12:27:47 +0530788
Bharat Bhushan08c9a182013-11-18 11:18:54 +0530789 vcpu->arch.pgdir = current->mm->pgd;
Scott Wood5f1c2482013-07-10 17:47:39 -0500790 kvmppc_fix_ee_before_entry();
Scott Woodf8941fbe2013-06-11 11:38:31 -0500791
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000792 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
Scott Wood8fae8452011-12-20 15:34:45 +0000793
Paolo Bonzini6edaa532016-06-15 15:18:26 +0200794 /* No need for guest_exit. It's done in handle_exit.
Alexander Graf24afa372012-08-12 12:42:30 +0200795 We also get here with interrupts enabled. */
796
Bharat Bhushance11e482013-07-04 12:27:47 +0530797 /* Switch back to user space debug context */
Scott Woodf5f97212013-11-22 15:52:29 -0600798 switch_booke_debug_regs(&debug);
799 current->thread.debug = debug;
Bharat Bhushance11e482013-07-04 12:27:47 +0530800
Scott Wood8fae8452011-12-20 15:34:45 +0000801#ifdef CONFIG_PPC_FPU
802 kvmppc_save_guest_fp(vcpu);
Scott Wood8fae8452011-12-20 15:34:45 +0000803#endif
804
Mihai Caraman95d80a22014-08-20 16:36:23 +0300805#ifdef CONFIG_ALTIVEC
806 kvmppc_save_guest_altivec(vcpu);
807#endif
808
Scott Wood1d1ef222011-11-08 16:11:59 -0600809out:
Alexander Grafd69c6432012-08-08 20:44:20 +0200810 vcpu->mode = OUTSIDE_GUEST_MODE;
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000811 return ret;
812}
813
Scott Woodd30f6e42011-12-20 15:34:43 +0000814static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
815{
816 enum emulation_result er;
817
818 er = kvmppc_emulate_instruction(run, vcpu);
819 switch (er) {
820 case EMULATE_DONE:
821 /* don't overwrite subtypes, just account kvm_stats */
822 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
823 /* Future optimization: only reload non-volatiles if
824 * they were actually modified by emulation. */
825 return RESUME_GUEST_NV;
826
Mihai Caraman51f04722014-07-23 19:06:21 +0300827 case EMULATE_AGAIN:
828 return RESUME_GUEST;
829
Scott Woodd30f6e42011-12-20 15:34:43 +0000830 case EMULATE_FAIL:
Scott Woodd30f6e42011-12-20 15:34:43 +0000831 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Simon Guo173c5202018-05-07 14:20:08 +0800832 __func__, vcpu->arch.regs.nip, vcpu->arch.last_inst);
Scott Woodd30f6e42011-12-20 15:34:43 +0000833 /* For debugging, encode the failing instruction and
834 * report it to userspace. */
835 run->hw.hardware_exit_reason = ~0ULL << 32;
836 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
Alexander Grafd1ff5492012-02-16 13:24:03 +0000837 kvmppc_core_queue_program(vcpu, ESR_PIL);
Scott Woodd30f6e42011-12-20 15:34:43 +0000838 return RESUME_HOST;
839
Bharat Bhushan9b4f5302013-04-08 00:32:15 +0000840 case EMULATE_EXIT_USER:
841 return RESUME_HOST;
842
Scott Woodd30f6e42011-12-20 15:34:43 +0000843 default:
844 BUG();
845 }
846}
847
Bharat Bhushance11e482013-07-04 12:27:47 +0530848static int kvmppc_handle_debug(struct kvm_run *run, struct kvm_vcpu *vcpu)
849{
Bharat Bhushan348ba712014-08-06 12:08:55 +0530850 struct debug_reg *dbg_reg = &(vcpu->arch.dbg_reg);
Bharat Bhushance11e482013-07-04 12:27:47 +0530851 u32 dbsr = vcpu->arch.dbsr;
852
Bharat Bhushan2f699a52014-08-13 14:39:44 +0530853 if (vcpu->guest_debug == 0) {
854 /*
855 * Debug resources belong to Guest.
856 * Imprecise debug event is not injected
857 */
858 if (dbsr & DBSR_IDE) {
859 dbsr &= ~DBSR_IDE;
860 if (!dbsr)
861 return RESUME_GUEST;
862 }
863
864 if (dbsr && (vcpu->arch.shared->msr & MSR_DE) &&
865 (vcpu->arch.dbg_reg.dbcr0 & DBCR0_IDM))
866 kvmppc_core_queue_debug(vcpu);
867
868 /* Inject a program interrupt if trap debug is not allowed */
869 if ((dbsr & DBSR_TIE) && !(vcpu->arch.shared->msr & MSR_DE))
870 kvmppc_core_queue_program(vcpu, ESR_PTR);
871
872 return RESUME_GUEST;
873 }
874
875 /*
876 * Debug resource owned by userspace.
877 * Clear guest dbsr (vcpu->arch.dbsr)
878 */
Bharat Bhushan21909912014-08-06 12:08:54 +0530879 vcpu->arch.dbsr = 0;
Bharat Bhushance11e482013-07-04 12:27:47 +0530880 run->debug.arch.status = 0;
Simon Guo173c5202018-05-07 14:20:08 +0800881 run->debug.arch.address = vcpu->arch.regs.nip;
Bharat Bhushance11e482013-07-04 12:27:47 +0530882
883 if (dbsr & (DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4)) {
884 run->debug.arch.status |= KVMPPC_DEBUG_BREAKPOINT;
885 } else {
886 if (dbsr & (DBSR_DAC1W | DBSR_DAC2W))
887 run->debug.arch.status |= KVMPPC_DEBUG_WATCH_WRITE;
888 else if (dbsr & (DBSR_DAC1R | DBSR_DAC2R))
889 run->debug.arch.status |= KVMPPC_DEBUG_WATCH_READ;
890 if (dbsr & (DBSR_DAC1R | DBSR_DAC1W))
891 run->debug.arch.address = dbg_reg->dac1;
892 else if (dbsr & (DBSR_DAC2R | DBSR_DAC2W))
893 run->debug.arch.address = dbg_reg->dac2;
894 }
895
896 return RESUME_HOST;
897}
898
Alexander Graf4e642cc2012-02-20 23:57:26 +0100899static void kvmppc_fill_pt_regs(struct pt_regs *regs)
900{
901 ulong r1, ip, msr, lr;
902
903 asm("mr %0, 1" : "=r"(r1));
904 asm("mflr %0" : "=r"(lr));
905 asm("mfmsr %0" : "=r"(msr));
906 asm("bl 1f; 1: mflr %0" : "=r"(ip));
907
908 memset(regs, 0, sizeof(*regs));
909 regs->gpr[1] = r1;
910 regs->nip = ip;
911 regs->msr = msr;
912 regs->link = lr;
913}
914
Bharat Bhushan6328e592012-06-20 05:56:53 +0000915/*
916 * For interrupts needed to be handled by host interrupt handlers,
917 * corresponding host handler are called from here in similar way
918 * (but not exact) as they are called from low level handler
919 * (such as from arch/powerpc/kernel/head_fsl_booke.S).
920 */
Alexander Graf4e642cc2012-02-20 23:57:26 +0100921static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
922 unsigned int exit_nr)
923{
924 struct pt_regs regs;
925
926 switch (exit_nr) {
927 case BOOKE_INTERRUPT_EXTERNAL:
928 kvmppc_fill_pt_regs(&regs);
929 do_IRQ(&regs);
930 break;
931 case BOOKE_INTERRUPT_DECREMENTER:
932 kvmppc_fill_pt_regs(&regs);
933 timer_interrupt(&regs);
934 break;
Tiejun Chen5f17ce82013-05-13 10:00:45 +0800935#if defined(CONFIG_PPC_DOORBELL)
Alexander Graf4e642cc2012-02-20 23:57:26 +0100936 case BOOKE_INTERRUPT_DOORBELL:
937 kvmppc_fill_pt_regs(&regs);
938 doorbell_exception(&regs);
939 break;
940#endif
941 case BOOKE_INTERRUPT_MACHINE_CHECK:
942 /* FIXME */
943 break;
Alexander Graf7cc1e8e2012-02-22 16:26:34 +0100944 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
945 kvmppc_fill_pt_regs(&regs);
946 performance_monitor_exception(&regs);
947 break;
Bharat Bhushan6328e592012-06-20 05:56:53 +0000948 case BOOKE_INTERRUPT_WATCHDOG:
949 kvmppc_fill_pt_regs(&regs);
950#ifdef CONFIG_BOOKE_WDT
951 WatchdogException(&regs);
952#else
953 unknown_exception(&regs);
954#endif
955 break;
956 case BOOKE_INTERRUPT_CRITICAL:
Tudor Laurentiu845ac982015-05-18 15:44:27 +0300957 kvmppc_fill_pt_regs(&regs);
Bharat Bhushan6328e592012-06-20 05:56:53 +0000958 unknown_exception(&regs);
959 break;
Bharat Bhushance11e482013-07-04 12:27:47 +0530960 case BOOKE_INTERRUPT_DEBUG:
961 /* Save DBSR before preemption is enabled */
962 vcpu->arch.dbsr = mfspr(SPRN_DBSR);
963 kvmppc_clear_dbsr();
964 break;
Alexander Graf4e642cc2012-02-20 23:57:26 +0100965 }
966}
967
Mihai Caramanf5250472014-07-23 19:06:22 +0300968static int kvmppc_resume_inst_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
969 enum emulation_result emulated, u32 last_inst)
970{
971 switch (emulated) {
972 case EMULATE_AGAIN:
973 return RESUME_GUEST;
974
975 case EMULATE_FAIL:
976 pr_debug("%s: load instruction from guest address %lx failed\n",
Simon Guo173c5202018-05-07 14:20:08 +0800977 __func__, vcpu->arch.regs.nip);
Mihai Caramanf5250472014-07-23 19:06:22 +0300978 /* For debugging, encode the failing instruction and
979 * report it to userspace. */
980 run->hw.hardware_exit_reason = ~0ULL << 32;
981 run->hw.hardware_exit_reason |= last_inst;
982 kvmppc_core_queue_program(vcpu, ESR_PIL);
983 return RESUME_HOST;
984
985 default:
986 BUG();
987 }
988}
989
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500990/**
991 * kvmppc_handle_exit
992 *
993 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
994 */
995int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
996 unsigned int exit_nr)
997{
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500998 int r = RESUME_HOST;
Alexander Graf7ee78852012-08-13 12:44:41 +0200999 int s;
Scott Woodf1e89022013-06-06 19:16:31 -05001000 int idx;
Mihai Caramanf5250472014-07-23 19:06:22 +03001001 u32 last_inst = KVM_INST_FETCH_FAILED;
1002 enum emulation_result emulated = EMULATE_DONE;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001003
Hollis Blanchard73e75b42008-12-02 15:51:57 -06001004 /* update before a new last_exit_type is rewritten */
1005 kvmppc_update_timing_stats(vcpu);
1006
Alexander Graf4e642cc2012-02-20 23:57:26 +01001007 /* restart interrupts if they were meant for the host */
1008 kvmppc_restart_interrupt(vcpu, exit_nr);
Scott Woodd30f6e42011-12-20 15:34:43 +00001009
Mihai Caramanf5250472014-07-23 19:06:22 +03001010 /*
Adam Buchbinder446957b2016-02-24 10:51:11 -08001011 * get last instruction before being preempted
Mihai Caramanf5250472014-07-23 19:06:22 +03001012 * TODO: for e6500 check also BOOKE_INTERRUPT_LRAT_ERROR & ESR_DATA
1013 */
1014 switch (exit_nr) {
1015 case BOOKE_INTERRUPT_DATA_STORAGE:
1016 case BOOKE_INTERRUPT_DTLB_MISS:
1017 case BOOKE_INTERRUPT_HV_PRIV:
Alexander Graf8d0eff62014-09-10 14:37:29 +02001018 emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
Mihai Caramanf5250472014-07-23 19:06:22 +03001019 break;
Madhavan Srinivasan033aaa12014-09-09 22:37:36 +05301020 case BOOKE_INTERRUPT_PROGRAM:
1021 /* SW breakpoints arrive as illegal instructions on HV */
1022 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Alexander Graf8d0eff62014-09-10 14:37:29 +02001023 emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
Madhavan Srinivasan033aaa12014-09-09 22:37:36 +05301024 break;
Mihai Caramanf5250472014-07-23 19:06:22 +03001025 default:
1026 break;
1027 }
1028
Alexander Graf97c95052012-08-02 15:10:00 +02001029 trace_kvm_exit(exit_nr, vcpu);
Paolo Bonzini6edaa532016-06-15 15:18:26 +02001030 guest_exit_irqoff();
Paolo Bonzinie233d542015-04-30 14:39:40 +02001031
1032 local_irq_enable();
Alexander Graf97c95052012-08-02 15:10:00 +02001033
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001034 run->exit_reason = KVM_EXIT_UNKNOWN;
1035 run->ready_for_interrupt_injection = 1;
1036
Mihai Caramanf5250472014-07-23 19:06:22 +03001037 if (emulated != EMULATE_DONE) {
1038 r = kvmppc_resume_inst_load(run, vcpu, emulated, last_inst);
1039 goto out;
1040 }
1041
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001042 switch (exit_nr) {
1043 case BOOKE_INTERRUPT_MACHINE_CHECK:
Alexander Grafc35c9d82012-02-20 12:21:18 +01001044 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
1045 kvmppc_dump_vcpu(vcpu);
1046 /* For debugging, send invalid exit reason to user space */
1047 run->hw.hardware_exit_reason = ~1ULL << 32;
1048 run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
1049 r = RESUME_HOST;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001050 break;
1051
1052 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -06001053 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -06001054 r = RESUME_GUEST;
1055 break;
1056
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001057 case BOOKE_INTERRUPT_DECREMENTER:
Hollis Blanchard7b701592008-12-02 15:51:58 -06001058 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001059 r = RESUME_GUEST;
1060 break;
1061
Bharat Bhushan6328e592012-06-20 05:56:53 +00001062 case BOOKE_INTERRUPT_WATCHDOG:
1063 r = RESUME_GUEST;
1064 break;
1065
Scott Woodd30f6e42011-12-20 15:34:43 +00001066 case BOOKE_INTERRUPT_DOORBELL:
1067 kvmppc_account_exit(vcpu, DBELL_EXITS);
Scott Woodd30f6e42011-12-20 15:34:43 +00001068 r = RESUME_GUEST;
1069 break;
1070
1071 case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
1072 kvmppc_account_exit(vcpu, GDBELL_EXITS);
1073
1074 /*
1075 * We are here because there is a pending guest interrupt
1076 * which could not be delivered as MSR_CE or MSR_ME was not
1077 * set. Once we break from here we will retry delivery.
1078 */
1079 r = RESUME_GUEST;
1080 break;
1081
1082 case BOOKE_INTERRUPT_GUEST_DBELL:
1083 kvmppc_account_exit(vcpu, GDBELL_EXITS);
1084
1085 /*
1086 * We are here because there is a pending guest interrupt
1087 * which could not be delivered as MSR_EE was not set. Once
1088 * we break from here we will retry delivery.
1089 */
1090 r = RESUME_GUEST;
1091 break;
1092
Alexander Graf95f2e922012-02-20 22:45:12 +01001093 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
1094 r = RESUME_GUEST;
1095 break;
1096
Scott Woodd30f6e42011-12-20 15:34:43 +00001097 case BOOKE_INTERRUPT_HV_PRIV:
1098 r = emulation_exit(run, vcpu);
1099 break;
1100
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001101 case BOOKE_INTERRUPT_PROGRAM:
Madhavan Srinivasan033aaa12014-09-09 22:37:36 +05301102 if ((vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) &&
1103 (last_inst == KVMPPC_INST_SW_BREAKPOINT)) {
1104 /*
1105 * We are here because of an SW breakpoint instr,
1106 * so lets return to host to handle.
1107 */
1108 r = kvmppc_handle_debug(run, vcpu);
1109 run->exit_reason = KVM_EXIT_DEBUG;
1110 kvmppc_account_exit(vcpu, DEBUG_EXITS);
1111 break;
1112 }
1113
Scott Woodd30f6e42011-12-20 15:34:43 +00001114 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
Alexander Graf02685972012-02-20 12:33:22 +01001115 /*
1116 * Program traps generated by user-level software must
1117 * be handled by the guest kernel.
1118 *
1119 * In GS mode, hypervisor privileged instructions trap
1120 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
1121 * actual program interrupts, handled by the guest.
1122 */
Liu Yudaf5e272010-02-02 19:44:35 +08001123 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001124 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -06001125 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001126 break;
1127 }
1128
Scott Woodd30f6e42011-12-20 15:34:43 +00001129 r = emulation_exit(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001130 break;
1131
Christian Ehrhardtde368dc2008-04-29 18:18:23 +02001132 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001133 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001134 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +02001135 r = RESUME_GUEST;
1136 break;
1137
Scott Wood4cd35f62011-06-14 18:34:31 -05001138#ifdef CONFIG_SPE
1139 case BOOKE_INTERRUPT_SPE_UNAVAIL: {
1140 if (vcpu->arch.shared->msr & MSR_SPE)
1141 kvmppc_vcpu_enable_spe(vcpu);
1142 else
1143 kvmppc_booke_queue_irqprio(vcpu,
1144 BOOKE_IRQPRIO_SPE_UNAVAIL);
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -06001145 r = RESUME_GUEST;
1146 break;
Scott Wood4cd35f62011-06-14 18:34:31 -05001147 }
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -06001148
1149 case BOOKE_INTERRUPT_SPE_FP_DATA:
1150 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
1151 r = RESUME_GUEST;
1152 break;
1153
1154 case BOOKE_INTERRUPT_SPE_FP_ROUND:
1155 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
1156 r = RESUME_GUEST;
1157 break;
Mihai Caraman95d80a22014-08-20 16:36:23 +03001158#elif defined(CONFIG_SPE_POSSIBLE)
Scott Wood4cd35f62011-06-14 18:34:31 -05001159 case BOOKE_INTERRUPT_SPE_UNAVAIL:
1160 /*
1161 * Guest wants SPE, but host kernel doesn't support it. Send
1162 * an "unimplemented operation" program check to the guest.
1163 */
1164 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
1165 r = RESUME_GUEST;
1166 break;
1167
1168 /*
1169 * These really should never happen without CONFIG_SPE,
1170 * as we should never enable the real MSR[SPE] in the guest.
1171 */
1172 case BOOKE_INTERRUPT_SPE_FP_DATA:
1173 case BOOKE_INTERRUPT_SPE_FP_ROUND:
1174 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
Simon Guo173c5202018-05-07 14:20:08 +08001175 __func__, exit_nr, vcpu->arch.regs.nip);
Scott Wood4cd35f62011-06-14 18:34:31 -05001176 run->hw.hardware_exit_reason = exit_nr;
1177 r = RESUME_HOST;
1178 break;
Mihai Caraman95d80a22014-08-20 16:36:23 +03001179#endif /* CONFIG_SPE_POSSIBLE */
1180
1181/*
1182 * On cores with Vector category, KVM is loaded only if CONFIG_ALTIVEC,
1183 * see kvmppc_core_check_processor_compat().
1184 */
1185#ifdef CONFIG_ALTIVEC
1186 case BOOKE_INTERRUPT_ALTIVEC_UNAVAIL:
1187 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALTIVEC_UNAVAIL);
1188 r = RESUME_GUEST;
1189 break;
1190
1191 case BOOKE_INTERRUPT_ALTIVEC_ASSIST:
1192 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALTIVEC_ASSIST);
1193 r = RESUME_GUEST;
1194 break;
Scott Wood4cd35f62011-06-14 18:34:31 -05001195#endif
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -06001196
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001197 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +08001198 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
1199 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001200 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001201 r = RESUME_GUEST;
1202 break;
1203
1204 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +08001205 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001206 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001207 r = RESUME_GUEST;
1208 break;
1209
Alexander Graf011da892013-01-31 14:17:38 +01001210 case BOOKE_INTERRUPT_ALIGNMENT:
1211 kvmppc_core_queue_alignment(vcpu, vcpu->arch.fault_dear,
1212 vcpu->arch.fault_esr);
1213 r = RESUME_GUEST;
1214 break;
1215
Scott Woodd30f6e42011-12-20 15:34:43 +00001216#ifdef CONFIG_KVM_BOOKE_HV
1217 case BOOKE_INTERRUPT_HV_SYSCALL:
1218 if (!(vcpu->arch.shared->msr & MSR_PR)) {
1219 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1220 } else {
1221 /*
1222 * hcall from guest userspace -- send privileged
1223 * instruction program check.
1224 */
1225 kvmppc_core_queue_program(vcpu, ESR_PPR);
1226 }
1227
1228 r = RESUME_GUEST;
1229 break;
1230#else
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001231 case BOOKE_INTERRUPT_SYSCALL:
Alexander Graf2a342ed2010-07-29 14:47:48 +02001232 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1233 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
1234 /* KVM PV hypercalls */
1235 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1236 r = RESUME_GUEST;
1237 } else {
1238 /* Guest syscalls */
1239 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
1240 }
Hollis Blanchard7b701592008-12-02 15:51:58 -06001241 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001242 r = RESUME_GUEST;
1243 break;
Scott Woodd30f6e42011-12-20 15:34:43 +00001244#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001245
1246 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001247 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001248 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001249 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001250 gfn_t gfn;
1251
Alexander Grafbf7ca4b2012-02-15 23:40:00 +00001252#ifdef CONFIG_KVM_E500V2
Scott Wooda4cd8b22011-06-14 18:34:41 -05001253 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1254 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
1255 kvmppc_map_magic(vcpu);
1256 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
1257 r = RESUME_GUEST;
1258
1259 break;
1260 }
1261#endif
1262
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001263 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -06001264 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001265 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001266 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +08001267 kvmppc_core_queue_dtlb_miss(vcpu,
1268 vcpu->arch.fault_dear,
1269 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -06001270 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001271 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001272 r = RESUME_GUEST;
1273 break;
1274 }
1275
Scott Woodf1e89022013-06-06 19:16:31 -05001276 idx = srcu_read_lock(&vcpu->kvm->srcu);
1277
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -06001278 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001279 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001280
1281 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1282 /* The guest TLB had a mapping, but the shadow TLB
1283 * didn't, and it is RAM. This could be because:
1284 * a) the entry is mapping the host kernel, or
1285 * b) the guest used a large mapping which we're faking
1286 * Either way, we need to satisfy the fault without
1287 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -06001288 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001289 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001290 r = RESUME_GUEST;
1291 } else {
1292 /* Guest has mapped and accessed a page which is not
1293 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001294 vcpu->arch.paddr_accessed = gpaddr;
Alexander Graf6020c0f2012-03-12 02:26:30 +01001295 vcpu->arch.vaddr_accessed = eaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001296 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001297 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001298 }
1299
Scott Woodf1e89022013-06-06 19:16:31 -05001300 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001301 break;
1302 }
1303
1304 case BOOKE_INTERRUPT_ITLB_MISS: {
Simon Guo173c5202018-05-07 14:20:08 +08001305 unsigned long eaddr = vcpu->arch.regs.nip;
Hollis Blanchard89168612008-12-02 15:51:53 -06001306 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001307 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001308 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001309
1310 r = RESUME_GUEST;
1311
1312 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -06001313 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001314 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001315 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001316 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -06001317 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001318 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001319 break;
1320 }
1321
Hollis Blanchard7b701592008-12-02 15:51:58 -06001322 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001323
Scott Woodf1e89022013-06-06 19:16:31 -05001324 idx = srcu_read_lock(&vcpu->kvm->srcu);
1325
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -06001326 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -06001327 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001328
1329 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1330 /* The guest TLB had a mapping, but the shadow TLB
1331 * didn't. This could be because:
1332 * a) the entry is mapping the host kernel, or
1333 * b) the guest used a large mapping which we're faking
1334 * Either way, we need to satisfy the fault without
1335 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -06001336 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001337 } else {
1338 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001339 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001340 }
1341
Scott Woodf1e89022013-06-06 19:16:31 -05001342 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001343 break;
1344 }
1345
Hollis Blanchard6a0ab732008-07-25 13:54:49 -05001346 case BOOKE_INTERRUPT_DEBUG: {
Bharat Bhushance11e482013-07-04 12:27:47 +05301347 r = kvmppc_handle_debug(run, vcpu);
1348 if (r == RESUME_HOST)
1349 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -06001350 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -05001351 break;
1352 }
1353
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001354 default:
1355 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
1356 BUG();
1357 }
1358
Mihai Caramanf5250472014-07-23 19:06:22 +03001359out:
Alexander Grafa8e4ef82012-02-16 14:07:37 +00001360 /*
1361 * To avoid clobbering exit_reason, only check for signals if we
1362 * aren't already exiting to userspace for some other reason.
1363 */
Alexander Graf03660ba2012-02-28 12:00:41 +01001364 if (!(r & RESUME_HOST)) {
Alexander Graf7ee78852012-08-13 12:44:41 +02001365 s = kvmppc_prepare_to_enter(vcpu);
Scott Wood6c85f522014-01-09 19:18:40 -06001366 if (s <= 0)
Alexander Graf7ee78852012-08-13 12:44:41 +02001367 r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
Scott Wood6c85f522014-01-09 19:18:40 -06001368 else {
1369 /* interrupts now hard-disabled */
Scott Wood5f1c2482013-07-10 17:47:39 -05001370 kvmppc_fix_ee_before_entry();
Mihai Caraman3efc7da2014-08-20 16:36:22 +03001371 kvmppc_load_guest_fp(vcpu);
Mihai Caraman95d80a22014-08-20 16:36:23 +03001372 kvmppc_load_guest_altivec(vcpu);
Alexander Graf03660ba2012-02-28 12:00:41 +01001373 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001374 }
1375
1376 return r;
1377}
1378
Bharat Bhushand26f22c2013-02-24 18:57:11 +00001379static void kvmppc_set_tsr(struct kvm_vcpu *vcpu, u32 new_tsr)
1380{
1381 u32 old_tsr = vcpu->arch.tsr;
1382
1383 vcpu->arch.tsr = new_tsr;
1384
1385 if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS))
1386 arm_next_watchdog(vcpu);
1387
1388 update_timer_ints(vcpu);
1389}
1390
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001391/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
1392int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1393{
Hollis Blanchard082decf2010-08-07 10:33:56 -07001394 int i;
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001395 int r;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001396
Simon Guo173c5202018-05-07 14:20:08 +08001397 vcpu->arch.regs.nip = 0;
Scott Woodb5904972011-11-08 18:23:30 -06001398 vcpu->arch.shared->pir = vcpu->vcpu_id;
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001399 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Scott Woodd30f6e42011-12-20 15:34:43 +00001400 kvmppc_set_msr(vcpu, 0);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001401
Scott Woodd30f6e42011-12-20 15:34:43 +00001402#ifndef CONFIG_KVM_BOOKE_HV
Bharat Bhushance11e482013-07-04 12:27:47 +05301403 vcpu->arch.shadow_msr = MSR_USER | MSR_IS | MSR_DS;
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001404 vcpu->arch.shadow_pid = 1;
Scott Woodd30f6e42011-12-20 15:34:43 +00001405 vcpu->arch.shared->msr = 0;
1406#endif
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001407
Hollis Blanchard082decf2010-08-07 10:33:56 -07001408 /* Eye-catching numbers so we know if the guest takes an interrupt
1409 * before it's programmed its own IVPR/IVORs. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001410 vcpu->arch.ivpr = 0x55550000;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001411 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
1412 vcpu->arch.ivor[i] = 0x7700 | i * 4;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001413
Hollis Blanchard73e75b42008-12-02 15:51:57 -06001414 kvmppc_init_timing_stats(vcpu);
1415
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001416 r = kvmppc_core_vcpu_setup(vcpu);
1417 kvmppc_sanity_check(vcpu);
1418 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001419}
1420
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001421int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
1422{
1423 /* setup watchdog timer once */
1424 spin_lock_init(&vcpu->arch.wdt_lock);
Kees Cook86cb30e2017-10-17 20:21:24 -07001425 timer_setup(&vcpu->arch.wdt_timer, kvmppc_watchdog_func, 0);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001426
Bharat Bhushan2f699a52014-08-13 14:39:44 +05301427 /*
1428 * Clear DBSR.MRR to avoid guest debug interrupt as
1429 * this is of host interest
1430 */
1431 mtspr(SPRN_DBSR, DBSR_MRR);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001432 return 0;
1433}
1434
1435void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
1436{
1437 del_timer_sync(&vcpu->arch.wdt_timer);
1438}
1439
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001440int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1441{
1442 int i;
1443
Christoffer Dall1fc9b762017-12-04 21:35:26 +01001444 vcpu_load(vcpu);
1445
Simon Guo173c5202018-05-07 14:20:08 +08001446 regs->pc = vcpu->arch.regs.nip;
Alexander Graf992b5b22010-01-08 02:58:02 +01001447 regs->cr = kvmppc_get_cr(vcpu);
Simon Guo173c5202018-05-07 14:20:08 +08001448 regs->ctr = vcpu->arch.regs.ctr;
1449 regs->lr = vcpu->arch.regs.link;
Alexander Graf992b5b22010-01-08 02:58:02 +01001450 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001451 regs->msr = vcpu->arch.shared->msr;
Bharat Bhushan31579ee2014-07-17 17:01:36 +05301452 regs->srr0 = kvmppc_get_srr0(vcpu);
1453 regs->srr1 = kvmppc_get_srr1(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001454 regs->pid = vcpu->arch.pid;
Bharat Bhushanc1b8a012014-07-17 17:01:39 +05301455 regs->sprg0 = kvmppc_get_sprg0(vcpu);
1456 regs->sprg1 = kvmppc_get_sprg1(vcpu);
1457 regs->sprg2 = kvmppc_get_sprg2(vcpu);
1458 regs->sprg3 = kvmppc_get_sprg3(vcpu);
1459 regs->sprg4 = kvmppc_get_sprg4(vcpu);
1460 regs->sprg5 = kvmppc_get_sprg5(vcpu);
1461 regs->sprg6 = kvmppc_get_sprg6(vcpu);
1462 regs->sprg7 = kvmppc_get_sprg7(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001463
1464 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001465 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001466
Christoffer Dall1fc9b762017-12-04 21:35:26 +01001467 vcpu_put(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001468 return 0;
1469}
1470
1471int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1472{
1473 int i;
1474
Christoffer Dall875656f2017-12-04 21:35:27 +01001475 vcpu_load(vcpu);
1476
Simon Guo173c5202018-05-07 14:20:08 +08001477 vcpu->arch.regs.nip = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001478 kvmppc_set_cr(vcpu, regs->cr);
Simon Guo173c5202018-05-07 14:20:08 +08001479 vcpu->arch.regs.ctr = regs->ctr;
1480 vcpu->arch.regs.link = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001481 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -06001482 kvmppc_set_msr(vcpu, regs->msr);
Bharat Bhushan31579ee2014-07-17 17:01:36 +05301483 kvmppc_set_srr0(vcpu, regs->srr0);
1484 kvmppc_set_srr1(vcpu, regs->srr1);
Scott Wood5ce941e2011-04-27 17:24:21 -05001485 kvmppc_set_pid(vcpu, regs->pid);
Bharat Bhushanc1b8a012014-07-17 17:01:39 +05301486 kvmppc_set_sprg0(vcpu, regs->sprg0);
1487 kvmppc_set_sprg1(vcpu, regs->sprg1);
1488 kvmppc_set_sprg2(vcpu, regs->sprg2);
1489 kvmppc_set_sprg3(vcpu, regs->sprg3);
1490 kvmppc_set_sprg4(vcpu, regs->sprg4);
1491 kvmppc_set_sprg5(vcpu, regs->sprg5);
1492 kvmppc_set_sprg6(vcpu, regs->sprg6);
1493 kvmppc_set_sprg7(vcpu, regs->sprg7);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001494
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001495 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1496 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001497
Christoffer Dall875656f2017-12-04 21:35:27 +01001498 vcpu_put(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001499 return 0;
1500}
1501
Scott Wood5ce941e2011-04-27 17:24:21 -05001502static void get_sregs_base(struct kvm_vcpu *vcpu,
1503 struct kvm_sregs *sregs)
1504{
1505 u64 tb = get_tb();
1506
1507 sregs->u.e.features |= KVM_SREGS_E_BASE;
1508
1509 sregs->u.e.csrr0 = vcpu->arch.csrr0;
1510 sregs->u.e.csrr1 = vcpu->arch.csrr1;
1511 sregs->u.e.mcsr = vcpu->arch.mcsr;
Bharat Bhushandc168542014-07-17 17:01:38 +05301512 sregs->u.e.esr = kvmppc_get_esr(vcpu);
Bharat Bhushana5414d42014-07-17 17:01:37 +05301513 sregs->u.e.dear = kvmppc_get_dar(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001514 sregs->u.e.tsr = vcpu->arch.tsr;
1515 sregs->u.e.tcr = vcpu->arch.tcr;
1516 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
1517 sregs->u.e.tb = tb;
1518 sregs->u.e.vrsave = vcpu->arch.vrsave;
1519}
1520
1521static int set_sregs_base(struct kvm_vcpu *vcpu,
1522 struct kvm_sregs *sregs)
1523{
1524 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
1525 return 0;
1526
1527 vcpu->arch.csrr0 = sregs->u.e.csrr0;
1528 vcpu->arch.csrr1 = sregs->u.e.csrr1;
1529 vcpu->arch.mcsr = sregs->u.e.mcsr;
Bharat Bhushandc168542014-07-17 17:01:38 +05301530 kvmppc_set_esr(vcpu, sregs->u.e.esr);
Bharat Bhushana5414d42014-07-17 17:01:37 +05301531 kvmppc_set_dar(vcpu, sregs->u.e.dear);
Scott Wood5ce941e2011-04-27 17:24:21 -05001532 vcpu->arch.vrsave = sregs->u.e.vrsave;
Scott Wooddfd4d472011-11-17 12:39:59 +00001533 kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001534
Scott Wooddfd4d472011-11-17 12:39:59 +00001535 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
Scott Wood5ce941e2011-04-27 17:24:21 -05001536 vcpu->arch.dec = sregs->u.e.dec;
Scott Wooddfd4d472011-11-17 12:39:59 +00001537 kvmppc_emulate_dec(vcpu);
1538 }
Scott Wood5ce941e2011-04-27 17:24:21 -05001539
Bharat Bhushand26f22c2013-02-24 18:57:11 +00001540 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR)
1541 kvmppc_set_tsr(vcpu, sregs->u.e.tsr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001542
1543 return 0;
1544}
1545
1546static void get_sregs_arch206(struct kvm_vcpu *vcpu,
1547 struct kvm_sregs *sregs)
1548{
1549 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
1550
Scott Wood841741f2011-09-02 17:39:37 -05001551 sregs->u.e.pir = vcpu->vcpu_id;
Scott Wood5ce941e2011-04-27 17:24:21 -05001552 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
1553 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
1554 sregs->u.e.decar = vcpu->arch.decar;
1555 sregs->u.e.ivpr = vcpu->arch.ivpr;
1556}
1557
1558static int set_sregs_arch206(struct kvm_vcpu *vcpu,
1559 struct kvm_sregs *sregs)
1560{
1561 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
1562 return 0;
1563
Scott Wood841741f2011-09-02 17:39:37 -05001564 if (sregs->u.e.pir != vcpu->vcpu_id)
Scott Wood5ce941e2011-04-27 17:24:21 -05001565 return -EINVAL;
1566
1567 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
1568 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
1569 vcpu->arch.decar = sregs->u.e.decar;
1570 vcpu->arch.ivpr = sregs->u.e.ivpr;
1571
1572 return 0;
1573}
1574
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301575int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Scott Wood5ce941e2011-04-27 17:24:21 -05001576{
1577 sregs->u.e.features |= KVM_SREGS_E_IVOR;
1578
1579 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
1580 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
1581 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
1582 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
1583 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
1584 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
1585 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
1586 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
1587 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
1588 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
1589 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
1590 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
1591 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
1592 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
1593 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
1594 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301595 return 0;
Scott Wood5ce941e2011-04-27 17:24:21 -05001596}
1597
1598int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1599{
1600 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
1601 return 0;
1602
1603 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
1604 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
1605 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
1606 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
1607 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
1608 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
1609 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
1610 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
1611 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
1612 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
1613 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
1614 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
1615 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
1616 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
1617 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
1618 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
1619
1620 return 0;
1621}
1622
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001623int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1624 struct kvm_sregs *sregs)
1625{
Christoffer Dallbcdec412017-12-04 21:35:28 +01001626 int ret;
1627
1628 vcpu_load(vcpu);
1629
Scott Wood5ce941e2011-04-27 17:24:21 -05001630 sregs->pvr = vcpu->arch.pvr;
1631
1632 get_sregs_base(vcpu, sregs);
1633 get_sregs_arch206(vcpu, sregs);
Christoffer Dallbcdec412017-12-04 21:35:28 +01001634 ret = vcpu->kvm->arch.kvm_ops->get_sregs(vcpu, sregs);
1635
1636 vcpu_put(vcpu);
1637 return ret;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001638}
1639
1640int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1641 struct kvm_sregs *sregs)
1642{
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01001643 int ret = -EINVAL;
Scott Wood5ce941e2011-04-27 17:24:21 -05001644
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01001645 vcpu_load(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001646 if (vcpu->arch.pvr != sregs->pvr)
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01001647 goto out;
Scott Wood5ce941e2011-04-27 17:24:21 -05001648
1649 ret = set_sregs_base(vcpu, sregs);
1650 if (ret < 0)
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01001651 goto out;
Scott Wood5ce941e2011-04-27 17:24:21 -05001652
1653 ret = set_sregs_arch206(vcpu, sregs);
1654 if (ret < 0)
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01001655 goto out;
Scott Wood5ce941e2011-04-27 17:24:21 -05001656
Christoffer Dallb4ef9d42017-12-04 21:35:29 +01001657 ret = vcpu->kvm->arch.kvm_ops->set_sregs(vcpu, sregs);
1658
1659out:
1660 vcpu_put(vcpu);
1661 return ret;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001662}
1663
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001664int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id,
1665 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001666{
Mihai Caraman35b299e2013-04-11 00:03:07 +00001667 int r = 0;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001668
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001669 switch (id) {
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001670 case KVM_REG_PPC_IAC1:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001671 *val = get_reg_val(id, vcpu->arch.dbg_reg.iac1);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001672 break;
Bharat Bhushan547465e2013-07-04 12:27:46 +05301673 case KVM_REG_PPC_IAC2:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001674 *val = get_reg_val(id, vcpu->arch.dbg_reg.iac2);
Bharat Bhushan547465e2013-07-04 12:27:46 +05301675 break;
1676#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1677 case KVM_REG_PPC_IAC3:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001678 *val = get_reg_val(id, vcpu->arch.dbg_reg.iac3);
Bharat Bhushan547465e2013-07-04 12:27:46 +05301679 break;
1680 case KVM_REG_PPC_IAC4:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001681 *val = get_reg_val(id, vcpu->arch.dbg_reg.iac4);
Bharat Bhushan547465e2013-07-04 12:27:46 +05301682 break;
1683#endif
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001684 case KVM_REG_PPC_DAC1:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001685 *val = get_reg_val(id, vcpu->arch.dbg_reg.dac1);
Bharat Bhushan547465e2013-07-04 12:27:46 +05301686 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001687 case KVM_REG_PPC_DAC2:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001688 *val = get_reg_val(id, vcpu->arch.dbg_reg.dac2);
Bharat Bhushan2c509672014-08-06 12:08:56 +05301689 break;
Alexander Graf324b3e62013-01-04 18:28:51 +01001690 case KVM_REG_PPC_EPR: {
Bharat Bhushan34f754b2014-07-17 17:01:40 +05301691 u32 epr = kvmppc_get_epr(vcpu);
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001692 *val = get_reg_val(id, epr);
Alexander Graf324b3e62013-01-04 18:28:51 +01001693 break;
1694 }
Mihai Caraman352df1d2012-10-11 06:13:29 +00001695#if defined(CONFIG_64BIT)
1696 case KVM_REG_PPC_EPCR:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001697 *val = get_reg_val(id, vcpu->arch.epcr);
Mihai Caraman352df1d2012-10-11 06:13:29 +00001698 break;
1699#endif
Bharat Bhushan78accda2013-02-24 18:57:12 +00001700 case KVM_REG_PPC_TCR:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001701 *val = get_reg_val(id, vcpu->arch.tcr);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001702 break;
1703 case KVM_REG_PPC_TSR:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001704 *val = get_reg_val(id, vcpu->arch.tsr);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001705 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001706 case KVM_REG_PPC_DEBUG_INST:
Madhavan Srinivasan033aaa12014-09-09 22:37:36 +05301707 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
Bharat Bhushan8c32a2e2013-03-20 20:24:58 +00001708 break;
Paul Mackerras8b75cbb2013-09-20 14:52:37 +10001709 case KVM_REG_PPC_VRSAVE:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001710 *val = get_reg_val(id, vcpu->arch.vrsave);
Bharat Bhushan8c32a2e2013-03-20 20:24:58 +00001711 break;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001712 default:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001713 r = vcpu->kvm->arch.kvm_ops->get_one_reg(vcpu, id, val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001714 break;
1715 }
Mihai Caraman35b299e2013-04-11 00:03:07 +00001716
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001717 return r;
Paul Mackerras31f34382011-12-12 12:26:50 +00001718}
1719
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001720int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id,
1721 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001722{
Mihai Caraman35b299e2013-04-11 00:03:07 +00001723 int r = 0;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001724
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001725 switch (id) {
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001726 case KVM_REG_PPC_IAC1:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001727 vcpu->arch.dbg_reg.iac1 = set_reg_val(id, *val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001728 break;
Bharat Bhushan547465e2013-07-04 12:27:46 +05301729 case KVM_REG_PPC_IAC2:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001730 vcpu->arch.dbg_reg.iac2 = set_reg_val(id, *val);
Bharat Bhushan547465e2013-07-04 12:27:46 +05301731 break;
1732#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1733 case KVM_REG_PPC_IAC3:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001734 vcpu->arch.dbg_reg.iac3 = set_reg_val(id, *val);
Bharat Bhushan547465e2013-07-04 12:27:46 +05301735 break;
1736 case KVM_REG_PPC_IAC4:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001737 vcpu->arch.dbg_reg.iac4 = set_reg_val(id, *val);
Bharat Bhushan547465e2013-07-04 12:27:46 +05301738 break;
1739#endif
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001740 case KVM_REG_PPC_DAC1:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001741 vcpu->arch.dbg_reg.dac1 = set_reg_val(id, *val);
Bharat Bhushan547465e2013-07-04 12:27:46 +05301742 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001743 case KVM_REG_PPC_DAC2:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001744 vcpu->arch.dbg_reg.dac2 = set_reg_val(id, *val);
Bharat Bhushan2c509672014-08-06 12:08:56 +05301745 break;
Alexander Graf324b3e62013-01-04 18:28:51 +01001746 case KVM_REG_PPC_EPR: {
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001747 u32 new_epr = set_reg_val(id, *val);
Mihai Caraman35b299e2013-04-11 00:03:07 +00001748 kvmppc_set_epr(vcpu, new_epr);
Alexander Graf324b3e62013-01-04 18:28:51 +01001749 break;
1750 }
Mihai Caraman352df1d2012-10-11 06:13:29 +00001751#if defined(CONFIG_64BIT)
1752 case KVM_REG_PPC_EPCR: {
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001753 u32 new_epcr = set_reg_val(id, *val);
Mihai Caraman35b299e2013-04-11 00:03:07 +00001754 kvmppc_set_epcr(vcpu, new_epcr);
Mihai Caraman352df1d2012-10-11 06:13:29 +00001755 break;
1756 }
1757#endif
Bharat Bhushan78accda2013-02-24 18:57:12 +00001758 case KVM_REG_PPC_OR_TSR: {
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001759 u32 tsr_bits = set_reg_val(id, *val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001760 kvmppc_set_tsr_bits(vcpu, tsr_bits);
1761 break;
1762 }
1763 case KVM_REG_PPC_CLEAR_TSR: {
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001764 u32 tsr_bits = set_reg_val(id, *val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001765 kvmppc_clr_tsr_bits(vcpu, tsr_bits);
1766 break;
1767 }
1768 case KVM_REG_PPC_TSR: {
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001769 u32 tsr = set_reg_val(id, *val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001770 kvmppc_set_tsr(vcpu, tsr);
1771 break;
1772 }
1773 case KVM_REG_PPC_TCR: {
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001774 u32 tcr = set_reg_val(id, *val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001775 kvmppc_set_tcr(vcpu, tcr);
1776 break;
1777 }
Paul Mackerras8b75cbb2013-09-20 14:52:37 +10001778 case KVM_REG_PPC_VRSAVE:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001779 vcpu->arch.vrsave = set_reg_val(id, *val);
Paul Mackerras8b75cbb2013-09-20 14:52:37 +10001780 break;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001781 default:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001782 r = vcpu->kvm->arch.kvm_ops->set_one_reg(vcpu, id, val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001783 break;
1784 }
Mihai Caraman35b299e2013-04-11 00:03:07 +00001785
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001786 return r;
Paul Mackerras31f34382011-12-12 12:26:50 +00001787}
1788
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001789int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1790{
1791 return -ENOTSUPP;
1792}
1793
1794int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1795{
1796 return -ENOTSUPP;
1797}
1798
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001799int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1800 struct kvm_translation *tr)
1801{
Avi Kivity98001d82010-05-13 11:05:49 +03001802 int r;
1803
Christoffer Dall1da5b612017-12-04 21:35:32 +01001804 vcpu_load(vcpu);
Avi Kivity98001d82010-05-13 11:05:49 +03001805 r = kvmppc_core_vcpu_translate(vcpu, tr);
Christoffer Dall1da5b612017-12-04 21:35:32 +01001806 vcpu_put(vcpu);
Avi Kivity98001d82010-05-13 11:05:49 +03001807 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001808}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001809
Alexander Graf4e755752009-10-30 05:47:01 +00001810int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1811{
1812 return -ENOTSUPP;
1813}
1814
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05301815void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001816 struct kvm_memory_slot *dont)
1817{
1818}
1819
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05301820int kvmppc_core_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001821 unsigned long npages)
1822{
1823 return 0;
1824}
1825
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001826int kvmppc_core_prepare_memory_region(struct kvm *kvm,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001827 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02001828 const struct kvm_userspace_memory_region *mem)
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001829{
1830 return 0;
1831}
1832
1833void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02001834 const struct kvm_userspace_memory_region *mem,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02001835 const struct kvm_memory_slot *old,
1836 const struct kvm_memory_slot *new)
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001837{
1838}
1839
1840void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001841{
1842}
1843
Mihai Caraman38f98822012-10-11 06:13:27 +00001844void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr)
1845{
1846#if defined(CONFIG_64BIT)
1847 vcpu->arch.epcr = new_epcr;
1848#ifdef CONFIG_KVM_BOOKE_HV
1849 vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM;
1850 if (vcpu->arch.epcr & SPRN_EPCR_ICM)
1851 vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM;
1852#endif
1853#endif
1854}
1855
Scott Wooddfd4d472011-11-17 12:39:59 +00001856void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1857{
1858 vcpu->arch.tcr = new_tcr;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001859 arm_next_watchdog(vcpu);
Scott Wooddfd4d472011-11-17 12:39:59 +00001860 update_timer_ints(vcpu);
1861}
1862
1863void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1864{
1865 set_bits(tsr_bits, &vcpu->arch.tsr);
1866 smp_wmb();
1867 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1868 kvm_vcpu_kick(vcpu);
1869}
1870
1871void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1872{
1873 clear_bits(tsr_bits, &vcpu->arch.tsr);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001874
1875 /*
1876 * We may have stopped the watchdog due to
1877 * being stuck on final expiration.
1878 */
1879 if (tsr_bits & (TSR_ENW | TSR_WIS))
1880 arm_next_watchdog(vcpu);
1881
Scott Wooddfd4d472011-11-17 12:39:59 +00001882 update_timer_ints(vcpu);
1883}
1884
Mihai Caramand02d4d12014-09-01 17:19:56 +03001885void kvmppc_decrementer_func(struct kvm_vcpu *vcpu)
Scott Wooddfd4d472011-11-17 12:39:59 +00001886{
Bharat Bhushan21bd0002012-05-20 23:21:23 +00001887 if (vcpu->arch.tcr & TCR_ARE) {
1888 vcpu->arch.dec = vcpu->arch.decar;
1889 kvmppc_emulate_dec(vcpu);
1890 }
1891
Scott Wooddfd4d472011-11-17 12:39:59 +00001892 kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1893}
1894
Bharat Bhushance11e482013-07-04 12:27:47 +05301895static int kvmppc_booke_add_breakpoint(struct debug_reg *dbg_reg,
1896 uint64_t addr, int index)
1897{
1898 switch (index) {
1899 case 0:
1900 dbg_reg->dbcr0 |= DBCR0_IAC1;
1901 dbg_reg->iac1 = addr;
1902 break;
1903 case 1:
1904 dbg_reg->dbcr0 |= DBCR0_IAC2;
1905 dbg_reg->iac2 = addr;
1906 break;
1907#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1908 case 2:
1909 dbg_reg->dbcr0 |= DBCR0_IAC3;
1910 dbg_reg->iac3 = addr;
1911 break;
1912 case 3:
1913 dbg_reg->dbcr0 |= DBCR0_IAC4;
1914 dbg_reg->iac4 = addr;
1915 break;
1916#endif
1917 default:
1918 return -EINVAL;
1919 }
1920
1921 dbg_reg->dbcr0 |= DBCR0_IDM;
1922 return 0;
1923}
1924
1925static int kvmppc_booke_add_watchpoint(struct debug_reg *dbg_reg, uint64_t addr,
1926 int type, int index)
1927{
1928 switch (index) {
1929 case 0:
1930 if (type & KVMPPC_DEBUG_WATCH_READ)
1931 dbg_reg->dbcr0 |= DBCR0_DAC1R;
1932 if (type & KVMPPC_DEBUG_WATCH_WRITE)
1933 dbg_reg->dbcr0 |= DBCR0_DAC1W;
1934 dbg_reg->dac1 = addr;
1935 break;
1936 case 1:
1937 if (type & KVMPPC_DEBUG_WATCH_READ)
1938 dbg_reg->dbcr0 |= DBCR0_DAC2R;
1939 if (type & KVMPPC_DEBUG_WATCH_WRITE)
1940 dbg_reg->dbcr0 |= DBCR0_DAC2W;
1941 dbg_reg->dac2 = addr;
1942 break;
1943 default:
1944 return -EINVAL;
1945 }
1946
1947 dbg_reg->dbcr0 |= DBCR0_IDM;
1948 return 0;
1949}
1950void kvm_guest_protect_msr(struct kvm_vcpu *vcpu, ulong prot_bitmap, bool set)
1951{
1952 /* XXX: Add similar MSR protection for BookE-PR */
1953#ifdef CONFIG_KVM_BOOKE_HV
1954 BUG_ON(prot_bitmap & ~(MSRP_UCLEP | MSRP_DEP | MSRP_PMMP));
1955 if (set) {
1956 if (prot_bitmap & MSR_UCLE)
1957 vcpu->arch.shadow_msrp |= MSRP_UCLEP;
1958 if (prot_bitmap & MSR_DE)
1959 vcpu->arch.shadow_msrp |= MSRP_DEP;
1960 if (prot_bitmap & MSR_PMM)
1961 vcpu->arch.shadow_msrp |= MSRP_PMMP;
1962 } else {
1963 if (prot_bitmap & MSR_UCLE)
1964 vcpu->arch.shadow_msrp &= ~MSRP_UCLEP;
1965 if (prot_bitmap & MSR_DE)
1966 vcpu->arch.shadow_msrp &= ~MSRP_DEP;
1967 if (prot_bitmap & MSR_PMM)
1968 vcpu->arch.shadow_msrp &= ~MSRP_PMMP;
1969 }
1970#endif
1971}
1972
Alexander Graf7d15c06f2014-06-20 13:52:36 +02001973int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, enum xlate_instdata xlid,
1974 enum xlate_readwrite xlrw, struct kvmppc_pte *pte)
1975{
1976 int gtlb_index;
1977 gpa_t gpaddr;
1978
1979#ifdef CONFIG_KVM_E500V2
1980 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1981 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
1982 pte->eaddr = eaddr;
1983 pte->raddr = (vcpu->arch.magic_page_pa & PAGE_MASK) |
1984 (eaddr & ~PAGE_MASK);
1985 pte->vpage = eaddr >> PAGE_SHIFT;
1986 pte->may_read = true;
1987 pte->may_write = true;
1988 pte->may_execute = true;
1989
1990 return 0;
1991 }
1992#endif
1993
1994 /* Check the guest TLB. */
1995 switch (xlid) {
1996 case XLATE_INST:
1997 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
1998 break;
1999 case XLATE_DATA:
2000 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
2001 break;
2002 default:
2003 BUG();
2004 }
2005
2006 /* Do we have a TLB entry at all? */
2007 if (gtlb_index < 0)
2008 return -ENOENT;
2009
2010 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
2011
2012 pte->eaddr = eaddr;
2013 pte->raddr = (gpaddr & PAGE_MASK) | (eaddr & ~PAGE_MASK);
2014 pte->vpage = eaddr >> PAGE_SHIFT;
2015
2016 /* XXX read permissions from the guest TLB */
2017 pte->may_read = true;
2018 pte->may_write = true;
2019 pte->may_execute = true;
2020
2021 return 0;
2022}
2023
Bharat Bhushance11e482013-07-04 12:27:47 +05302024int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
2025 struct kvm_guest_debug *dbg)
2026{
2027 struct debug_reg *dbg_reg;
2028 int n, b = 0, w = 0;
Christoffer Dall66b56562017-12-04 21:35:33 +01002029 int ret = 0;
2030
2031 vcpu_load(vcpu);
Bharat Bhushance11e482013-07-04 12:27:47 +05302032
2033 if (!(dbg->control & KVM_GUESTDBG_ENABLE)) {
Bharat Bhushan348ba712014-08-06 12:08:55 +05302034 vcpu->arch.dbg_reg.dbcr0 = 0;
Bharat Bhushance11e482013-07-04 12:27:47 +05302035 vcpu->guest_debug = 0;
2036 kvm_guest_protect_msr(vcpu, MSR_DE, false);
Christoffer Dall66b56562017-12-04 21:35:33 +01002037 goto out;
Bharat Bhushance11e482013-07-04 12:27:47 +05302038 }
2039
2040 kvm_guest_protect_msr(vcpu, MSR_DE, true);
2041 vcpu->guest_debug = dbg->control;
Bharat Bhushan348ba712014-08-06 12:08:55 +05302042 vcpu->arch.dbg_reg.dbcr0 = 0;
Bharat Bhushance11e482013-07-04 12:27:47 +05302043
2044 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Bharat Bhushan348ba712014-08-06 12:08:55 +05302045 vcpu->arch.dbg_reg.dbcr0 |= DBCR0_IDM | DBCR0_IC;
Bharat Bhushance11e482013-07-04 12:27:47 +05302046
2047 /* Code below handles only HW breakpoints */
Bharat Bhushan348ba712014-08-06 12:08:55 +05302048 dbg_reg = &(vcpu->arch.dbg_reg);
Bharat Bhushance11e482013-07-04 12:27:47 +05302049
2050#ifdef CONFIG_KVM_BOOKE_HV
2051 /*
2052 * On BookE-HV (e500mc) the guest is always executed with MSR.GS=1
2053 * DBCR1 and DBCR2 are set to trigger debug events when MSR.PR is 0
2054 */
2055 dbg_reg->dbcr1 = 0;
2056 dbg_reg->dbcr2 = 0;
2057#else
2058 /*
2059 * On BookE-PR (e500v2) the guest is always executed with MSR.PR=1
2060 * We set DBCR1 and DBCR2 to only trigger debug events when MSR.PR
2061 * is set.
2062 */
2063 dbg_reg->dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US | DBCR1_IAC3US |
2064 DBCR1_IAC4US;
2065 dbg_reg->dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
2066#endif
2067
2068 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
Christoffer Dall66b56562017-12-04 21:35:33 +01002069 goto out;
Bharat Bhushance11e482013-07-04 12:27:47 +05302070
Christoffer Dall66b56562017-12-04 21:35:33 +01002071 ret = -EINVAL;
Bharat Bhushance11e482013-07-04 12:27:47 +05302072 for (n = 0; n < (KVMPPC_BOOKE_IAC_NUM + KVMPPC_BOOKE_DAC_NUM); n++) {
2073 uint64_t addr = dbg->arch.bp[n].addr;
2074 uint32_t type = dbg->arch.bp[n].type;
2075
2076 if (type == KVMPPC_DEBUG_NONE)
2077 continue;
2078
Dan Carpenterac0e89b2016-07-14 13:15:46 +03002079 if (type & ~(KVMPPC_DEBUG_WATCH_READ |
Bharat Bhushance11e482013-07-04 12:27:47 +05302080 KVMPPC_DEBUG_WATCH_WRITE |
2081 KVMPPC_DEBUG_BREAKPOINT))
Christoffer Dall66b56562017-12-04 21:35:33 +01002082 goto out;
Bharat Bhushance11e482013-07-04 12:27:47 +05302083
2084 if (type & KVMPPC_DEBUG_BREAKPOINT) {
2085 /* Setting H/W breakpoint */
2086 if (kvmppc_booke_add_breakpoint(dbg_reg, addr, b++))
Christoffer Dall66b56562017-12-04 21:35:33 +01002087 goto out;
Bharat Bhushance11e482013-07-04 12:27:47 +05302088 } else {
2089 /* Setting H/W watchpoint */
2090 if (kvmppc_booke_add_watchpoint(dbg_reg, addr,
2091 type, w++))
Christoffer Dall66b56562017-12-04 21:35:33 +01002092 goto out;
Bharat Bhushance11e482013-07-04 12:27:47 +05302093 }
2094 }
2095
Christoffer Dall66b56562017-12-04 21:35:33 +01002096 ret = 0;
2097out:
2098 vcpu_put(vcpu);
2099 return ret;
Bharat Bhushance11e482013-07-04 12:27:47 +05302100}
2101
Scott Wood94fa9d92011-12-20 15:34:22 +00002102void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2103{
Paul Mackerrasa47d72f2012-09-20 19:35:51 +00002104 vcpu->cpu = smp_processor_id();
Scott Woodd30f6e42011-12-20 15:34:43 +00002105 current->thread.kvm_vcpu = vcpu;
Scott Wood94fa9d92011-12-20 15:34:22 +00002106}
2107
2108void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
2109{
Scott Woodd30f6e42011-12-20 15:34:43 +00002110 current->thread.kvm_vcpu = NULL;
Paul Mackerrasa47d72f2012-09-20 19:35:51 +00002111 vcpu->cpu = -1;
Bharat Bhushance11e482013-07-04 12:27:47 +05302112
2113 /* Clear pending debug event in DBSR */
2114 kvmppc_clear_dbsr();
Scott Wood94fa9d92011-12-20 15:34:22 +00002115}
2116
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302117void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
2118{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302119 vcpu->kvm->arch.kvm_ops->mmu_destroy(vcpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302120}
2121
2122int kvmppc_core_init_vm(struct kvm *kvm)
2123{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302124 return kvm->arch.kvm_ops->init_vm(kvm);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302125}
2126
2127struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
2128{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302129 return kvm->arch.kvm_ops->vcpu_create(kvm, id);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302130}
2131
2132void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
2133{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302134 vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302135}
2136
2137void kvmppc_core_destroy_vm(struct kvm *kvm)
2138{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302139 kvm->arch.kvm_ops->destroy_vm(kvm);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302140}
2141
2142void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2143{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302144 vcpu->kvm->arch.kvm_ops->vcpu_load(vcpu, cpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302145}
2146
2147void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
2148{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302149 vcpu->kvm->arch.kvm_ops->vcpu_put(vcpu);
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002150}
2151
2152int __init kvmppc_booke_init(void)
2153{
Scott Woodd30f6e42011-12-20 15:34:43 +00002154#ifndef CONFIG_KVM_BOOKE_HV
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002155 unsigned long ivor[16];
Bharat Bhushan1d542d92013-01-15 22:24:39 +00002156 unsigned long *handler = kvmppc_booke_handler_addr;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002157 unsigned long max_ivor = 0;
Bharat Bhushan1d542d92013-01-15 22:24:39 +00002158 unsigned long handler_len;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002159 int i;
2160
2161 /* We install our own exception handlers by hijacking IVPR. IVPR must
2162 * be 16-bit aligned, so we need a 64KB allocation. */
2163 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
2164 VCPU_SIZE_ORDER);
2165 if (!kvmppc_booke_handlers)
2166 return -ENOMEM;
2167
2168 /* XXX make sure our handlers are smaller than Linux's */
2169
2170 /* Copy our interrupt handlers to match host IVORs. That way we don't
2171 * have to swap the IVORs on every guest/host transition. */
2172 ivor[0] = mfspr(SPRN_IVOR0);
2173 ivor[1] = mfspr(SPRN_IVOR1);
2174 ivor[2] = mfspr(SPRN_IVOR2);
2175 ivor[3] = mfspr(SPRN_IVOR3);
2176 ivor[4] = mfspr(SPRN_IVOR4);
2177 ivor[5] = mfspr(SPRN_IVOR5);
2178 ivor[6] = mfspr(SPRN_IVOR6);
2179 ivor[7] = mfspr(SPRN_IVOR7);
2180 ivor[8] = mfspr(SPRN_IVOR8);
2181 ivor[9] = mfspr(SPRN_IVOR9);
2182 ivor[10] = mfspr(SPRN_IVOR10);
2183 ivor[11] = mfspr(SPRN_IVOR11);
2184 ivor[12] = mfspr(SPRN_IVOR12);
2185 ivor[13] = mfspr(SPRN_IVOR13);
2186 ivor[14] = mfspr(SPRN_IVOR14);
2187 ivor[15] = mfspr(SPRN_IVOR15);
2188
2189 for (i = 0; i < 16; i++) {
2190 if (ivor[i] > max_ivor)
Bharat Bhushan1d542d92013-01-15 22:24:39 +00002191 max_ivor = i;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002192
Bharat Bhushan1d542d92013-01-15 22:24:39 +00002193 handler_len = handler[i + 1] - handler[i];
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002194 memcpy((void *)kvmppc_booke_handlers + ivor[i],
Bharat Bhushan1d542d92013-01-15 22:24:39 +00002195 (void *)handler[i], handler_len);
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002196 }
Bharat Bhushan1d542d92013-01-15 22:24:39 +00002197
2198 handler_len = handler[max_ivor + 1] - handler[max_ivor];
2199 flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers +
2200 ivor[max_ivor] + handler_len);
Scott Woodd30f6e42011-12-20 15:34:43 +00002201#endif /* !BOOKE_HV */
Hollis Blancharddb93f572008-11-05 09:36:18 -06002202 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002203}
2204
Hollis Blancharddb93f572008-11-05 09:36:18 -06002205void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002206{
2207 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
2208 kvm_exit();
2209}