blob: fd5875179e5c0e6738a1e7867a75f05cbe1fa0c8 [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 Wood4cd35f672011-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>
33#include <asm/uaccess.h>
34#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) },
Hollis Blanchard45c5eb62008-04-25 17:55:49 -050067 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Scott Woodd30f6e42011-12-20 15:34:43 +000068 { "doorbell", VCPU_STAT(dbell_exits) },
69 { "guest doorbell", VCPU_STAT(gdbell_exits) },
Alexander Grafcf1c5ca2012-08-01 12:56:51 +020070 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050071 { NULL }
72};
73
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050074/* TODO: use vcpu_printf() */
75void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
76{
77 int i;
78
Alexander Graf666e7252010-07-29 14:47:43 +020079 printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060080 printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
Alexander Grafde7906c2010-07-29 14:47:46 +020081 printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
82 vcpu->arch.shared->srr1);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050083
84 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
85
86 for (i = 0; i < 32; i += 4) {
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060087 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
Alexander Graf8e5b26b2010-01-08 02:58:01 +010088 kvmppc_get_gpr(vcpu, i),
89 kvmppc_get_gpr(vcpu, i+1),
90 kvmppc_get_gpr(vcpu, i+2),
91 kvmppc_get_gpr(vcpu, i+3));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050092 }
93}
94
Scott Wood4cd35f672011-06-14 18:34:31 -050095#ifdef CONFIG_SPE
96void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
97{
98 preempt_disable();
99 enable_kernel_spe();
100 kvmppc_save_guest_spe(vcpu);
101 vcpu->arch.shadow_msr &= ~MSR_SPE;
102 preempt_enable();
103}
104
105static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
106{
107 preempt_disable();
108 enable_kernel_spe();
109 kvmppc_load_guest_spe(vcpu);
110 vcpu->arch.shadow_msr |= MSR_SPE;
111 preempt_enable();
112}
113
114static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
115{
116 if (vcpu->arch.shared->msr & MSR_SPE) {
117 if (!(vcpu->arch.shadow_msr & MSR_SPE))
118 kvmppc_vcpu_enable_spe(vcpu);
119 } else if (vcpu->arch.shadow_msr & MSR_SPE) {
120 kvmppc_vcpu_disable_spe(vcpu);
121 }
122}
123#else
124static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
125{
126}
127#endif
128
Mihai Caraman3efc7da2014-08-20 16:36:22 +0300129/*
130 * Load up guest vcpu FP state if it's needed.
131 * It also set the MSR_FP in thread so that host know
132 * we're holding FPU, and then host can help to save
133 * guest vcpu FP state if other threads require to use FPU.
134 * This simulates an FP unavailable fault.
135 *
136 * It requires to be called with preemption disabled.
137 */
138static inline void kvmppc_load_guest_fp(struct kvm_vcpu *vcpu)
139{
140#ifdef CONFIG_PPC_FPU
141 if (!(current->thread.regs->msr & MSR_FP)) {
142 enable_kernel_fp();
143 load_fp_state(&vcpu->arch.fp);
144 current->thread.fp_save_area = &vcpu->arch.fp;
145 current->thread.regs->msr |= MSR_FP;
146 }
147#endif
148}
149
150/*
151 * Save guest vcpu FP state into thread.
152 * It requires to be called with preemption disabled.
153 */
154static inline void kvmppc_save_guest_fp(struct kvm_vcpu *vcpu)
155{
156#ifdef CONFIG_PPC_FPU
157 if (current->thread.regs->msr & MSR_FP)
158 giveup_fpu(current);
159 current->thread.fp_save_area = NULL;
160#endif
161}
162
Alexander Graf7a08c272012-08-16 13:10:16 +0200163static void kvmppc_vcpu_sync_fpu(struct kvm_vcpu *vcpu)
164{
165#if defined(CONFIG_PPC_FPU) && !defined(CONFIG_KVM_BOOKE_HV)
166 /* We always treat the FP bit as enabled from the host
167 perspective, so only need to adjust the shadow MSR */
168 vcpu->arch.shadow_msr &= ~MSR_FP;
169 vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_FP;
170#endif
171}
172
Mihai Caraman95d80a22014-08-20 16:36:23 +0300173/*
174 * Simulate AltiVec unavailable fault to load guest state
175 * from thread to AltiVec unit.
176 * It requires to be called with preemption disabled.
177 */
178static inline void kvmppc_load_guest_altivec(struct kvm_vcpu *vcpu)
179{
180#ifdef CONFIG_ALTIVEC
181 if (cpu_has_feature(CPU_FTR_ALTIVEC)) {
182 if (!(current->thread.regs->msr & MSR_VEC)) {
183 enable_kernel_altivec();
184 load_vr_state(&vcpu->arch.vr);
185 current->thread.vr_save_area = &vcpu->arch.vr;
186 current->thread.regs->msr |= MSR_VEC;
187 }
188 }
189#endif
190}
191
192/*
193 * Save guest vcpu AltiVec state into thread.
194 * It requires to be called with preemption disabled.
195 */
196static inline void kvmppc_save_guest_altivec(struct kvm_vcpu *vcpu)
197{
198#ifdef CONFIG_ALTIVEC
199 if (cpu_has_feature(CPU_FTR_ALTIVEC)) {
200 if (current->thread.regs->msr & MSR_VEC)
201 giveup_altivec(current);
202 current->thread.vr_save_area = NULL;
203 }
204#endif
205}
206
Bharat Bhushance11e482013-07-04 12:27:47 +0530207static void kvmppc_vcpu_sync_debug(struct kvm_vcpu *vcpu)
208{
209 /* Synchronize guest's desire to get debug interrupts into shadow MSR */
210#ifndef CONFIG_KVM_BOOKE_HV
211 vcpu->arch.shadow_msr &= ~MSR_DE;
212 vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_DE;
213#endif
214
215 /* Force enable debug interrupts when user space wants to debug */
216 if (vcpu->guest_debug) {
217#ifdef CONFIG_KVM_BOOKE_HV
218 /*
219 * Since there is no shadow MSR, sync MSR_DE into the guest
220 * visible MSR.
221 */
222 vcpu->arch.shared->msr |= MSR_DE;
223#else
224 vcpu->arch.shadow_msr |= MSR_DE;
225 vcpu->arch.shared->msr &= ~MSR_DE;
226#endif
227 }
228}
229
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500230/*
231 * Helper function for "full" MSR writes. No need to call this if only
232 * EE/CE/ME/DE/RI are changing.
233 */
Scott Wood4cd35f672011-06-14 18:34:31 -0500234void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
235{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500236 u32 old_msr = vcpu->arch.shared->msr;
Scott Wood4cd35f672011-06-14 18:34:31 -0500237
Scott Woodd30f6e42011-12-20 15:34:43 +0000238#ifdef CONFIG_KVM_BOOKE_HV
239 new_msr |= MSR_GS;
240#endif
241
Scott Wood4cd35f672011-06-14 18:34:31 -0500242 vcpu->arch.shared->msr = new_msr;
243
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500244 kvmppc_mmu_msr_notify(vcpu, old_msr);
Scott Wood4cd35f672011-06-14 18:34:31 -0500245 kvmppc_vcpu_sync_spe(vcpu);
Alexander Graf7a08c272012-08-16 13:10:16 +0200246 kvmppc_vcpu_sync_fpu(vcpu);
Bharat Bhushance11e482013-07-04 12:27:47 +0530247 kvmppc_vcpu_sync_debug(vcpu);
Scott Wood4cd35f672011-06-14 18:34:31 -0500248}
249
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600250static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
251 unsigned int priority)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600252{
Alexander Graf63460462012-08-08 00:44:52 +0200253 trace_kvm_booke_queue_irqprio(vcpu, priority);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600254 set_bit(priority, &vcpu->arch.pending_exceptions);
255}
256
Alexander Graf8de12012014-06-18 21:56:55 +0200257void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
258 ulong dear_flags, ulong esr_flags)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600259{
Liu Yudaf5e272010-02-02 19:44:35 +0800260 vcpu->arch.queued_dear = dear_flags;
261 vcpu->arch.queued_esr = esr_flags;
262 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
263}
264
Alexander Graf8de12012014-06-18 21:56:55 +0200265void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
266 ulong dear_flags, ulong esr_flags)
Liu Yudaf5e272010-02-02 19:44:35 +0800267{
268 vcpu->arch.queued_dear = dear_flags;
269 vcpu->arch.queued_esr = esr_flags;
270 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
271}
272
Alexander Graf8de12012014-06-18 21:56:55 +0200273void kvmppc_core_queue_itlb_miss(struct kvm_vcpu *vcpu)
274{
275 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
276}
277
278void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu, ulong esr_flags)
Liu Yudaf5e272010-02-02 19:44:35 +0800279{
280 vcpu->arch.queued_esr = esr_flags;
281 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
282}
283
Alexander Graf011da892013-01-31 14:17:38 +0100284static void kvmppc_core_queue_alignment(struct kvm_vcpu *vcpu, ulong dear_flags,
285 ulong esr_flags)
286{
287 vcpu->arch.queued_dear = dear_flags;
288 vcpu->arch.queued_esr = esr_flags;
289 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALIGNMENT);
290}
291
Liu Yudaf5e272010-02-02 19:44:35 +0800292void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
293{
294 vcpu->arch.queued_esr = esr_flags;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600295 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600296}
297
298void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
299{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600300 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600301}
302
303int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
304{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600305 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600306}
307
Alexander Graf7706664d2009-12-21 20:21:24 +0100308void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
309{
310 clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
311}
312
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600313void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
314 struct kvm_interrupt *irq)
315{
Alexander Grafc5335f12010-08-30 14:03:24 +0200316 unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
317
318 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
319 prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
320
321 kvmppc_booke_queue_irqprio(vcpu, prio);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600322}
323
Paul Mackerras4fe27d22013-02-14 14:00:25 +0000324void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu)
Alexander Graf4496f972010-04-07 10:03:25 +0200325{
326 clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
Alexander Grafc5335f12010-08-30 14:03:24 +0200327 clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
Alexander Graf4496f972010-04-07 10:03:25 +0200328}
329
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000330static void kvmppc_core_queue_watchdog(struct kvm_vcpu *vcpu)
331{
332 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_WATCHDOG);
333}
334
335static void kvmppc_core_dequeue_watchdog(struct kvm_vcpu *vcpu)
336{
337 clear_bit(BOOKE_IRQPRIO_WATCHDOG, &vcpu->arch.pending_exceptions);
338}
339
Bharat Bhushan2f699a52014-08-13 14:39:44 +0530340void kvmppc_core_queue_debug(struct kvm_vcpu *vcpu)
341{
342 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DEBUG);
343}
344
345void kvmppc_core_dequeue_debug(struct kvm_vcpu *vcpu)
346{
347 clear_bit(BOOKE_IRQPRIO_DEBUG, &vcpu->arch.pending_exceptions);
348}
349
Scott Woodd30f6e42011-12-20 15:34:43 +0000350static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
351{
Bharat Bhushan31579ee2014-07-17 17:01:36 +0530352 kvmppc_set_srr0(vcpu, srr0);
353 kvmppc_set_srr1(vcpu, srr1);
Scott Woodd30f6e42011-12-20 15:34:43 +0000354}
355
356static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
357{
358 vcpu->arch.csrr0 = srr0;
359 vcpu->arch.csrr1 = srr1;
360}
361
362static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
363{
364 if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
365 vcpu->arch.dsrr0 = srr0;
366 vcpu->arch.dsrr1 = srr1;
367 } else {
368 set_guest_csrr(vcpu, srr0, srr1);
369 }
370}
371
372static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
373{
374 vcpu->arch.mcsrr0 = srr0;
375 vcpu->arch.mcsrr1 = srr1;
376}
377
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600378/* Deliver the interrupt of the corresponding priority, if possible. */
379static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
380 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500381{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600382 int allowed = 0;
Alexander Graf79300f82012-02-15 19:12:29 +0000383 ulong msr_mask = 0;
Alexander Graf1c810632013-01-04 18:12:48 +0100384 bool update_esr = false, update_dear = false, update_epr = false;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200385 ulong crit_raw = vcpu->arch.shared->critical;
386 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
387 bool crit;
Alexander Grafc5335f12010-08-30 14:03:24 +0200388 bool keep_irq = false;
Scott Woodd30f6e42011-12-20 15:34:43 +0000389 enum int_class int_class;
Mihai Caraman95e90b42012-10-11 06:13:26 +0000390 ulong new_msr = vcpu->arch.shared->msr;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200391
392 /* Truncate crit indicators in 32 bit mode */
393 if (!(vcpu->arch.shared->msr & MSR_SF)) {
394 crit_raw &= 0xffffffff;
395 crit_r1 &= 0xffffffff;
396 }
397
398 /* Critical section when crit == r1 */
399 crit = (crit_raw == crit_r1);
400 /* ... and we're in supervisor mode */
401 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500402
Alexander Grafc5335f12010-08-30 14:03:24 +0200403 if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
404 priority = BOOKE_IRQPRIO_EXTERNAL;
405 keep_irq = true;
406 }
407
Scott Wood5df554a2013-04-12 14:08:46 +0000408 if ((priority == BOOKE_IRQPRIO_EXTERNAL) && vcpu->arch.epr_flags)
Alexander Graf1c810632013-01-04 18:12:48 +0100409 update_epr = true;
410
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600411 switch (priority) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600412 case BOOKE_IRQPRIO_DTLB_MISS:
Liu Yudaf5e272010-02-02 19:44:35 +0800413 case BOOKE_IRQPRIO_DATA_STORAGE:
Alexander Graf011da892013-01-31 14:17:38 +0100414 case BOOKE_IRQPRIO_ALIGNMENT:
Liu Yudaf5e272010-02-02 19:44:35 +0800415 update_dear = true;
416 /* fall through */
417 case BOOKE_IRQPRIO_INST_STORAGE:
418 case BOOKE_IRQPRIO_PROGRAM:
419 update_esr = true;
420 /* fall through */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600421 case BOOKE_IRQPRIO_ITLB_MISS:
422 case BOOKE_IRQPRIO_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600423 case BOOKE_IRQPRIO_FP_UNAVAIL:
Mihai Caraman95d80a22014-08-20 16:36:23 +0300424#ifdef CONFIG_SPE_POSSIBLE
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600425 case BOOKE_IRQPRIO_SPE_UNAVAIL:
426 case BOOKE_IRQPRIO_SPE_FP_DATA:
427 case BOOKE_IRQPRIO_SPE_FP_ROUND:
Mihai Caraman95d80a22014-08-20 16:36:23 +0300428#endif
429#ifdef CONFIG_ALTIVEC
430 case BOOKE_IRQPRIO_ALTIVEC_UNAVAIL:
431 case BOOKE_IRQPRIO_ALTIVEC_ASSIST:
432#endif
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600433 case BOOKE_IRQPRIO_AP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600434 allowed = 1;
Alexander Graf79300f82012-02-15 19:12:29 +0000435 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000436 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500437 break;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000438 case BOOKE_IRQPRIO_WATCHDOG:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600439 case BOOKE_IRQPRIO_CRITICAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000440 case BOOKE_IRQPRIO_DBELL_CRIT:
Alexander Graf666e7252010-07-29 14:47:43 +0200441 allowed = vcpu->arch.shared->msr & MSR_CE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000442 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000443 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000444 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500445 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600446 case BOOKE_IRQPRIO_MACHINE_CHECK:
Alexander Graf666e7252010-07-29 14:47:43 +0200447 allowed = vcpu->arch.shared->msr & MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000448 allowed = allowed && !crit;
Scott Woodd30f6e42011-12-20 15:34:43 +0000449 int_class = INT_CLASS_MC;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500450 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600451 case BOOKE_IRQPRIO_DECREMENTER:
452 case BOOKE_IRQPRIO_FIT:
Scott Wooddfd4d472011-11-17 12:39:59 +0000453 keep_irq = true;
454 /* fall through */
455 case BOOKE_IRQPRIO_EXTERNAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000456 case BOOKE_IRQPRIO_DBELL:
Alexander Graf666e7252010-07-29 14:47:43 +0200457 allowed = vcpu->arch.shared->msr & MSR_EE;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200458 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000459 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000460 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500461 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600462 case BOOKE_IRQPRIO_DEBUG:
Alexander Graf666e7252010-07-29 14:47:43 +0200463 allowed = vcpu->arch.shared->msr & MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000464 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000465 msr_mask = MSR_ME;
Bharat Bhushan9fee7562014-08-06 12:08:51 +0530466 if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC))
467 int_class = INT_CLASS_DBG;
468 else
469 int_class = INT_CLASS_CRIT;
470
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500471 break;
472 }
473
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600474 if (allowed) {
Scott Woodd30f6e42011-12-20 15:34:43 +0000475 switch (int_class) {
476 case INT_CLASS_NONCRIT:
477 set_guest_srr(vcpu, vcpu->arch.pc,
478 vcpu->arch.shared->msr);
479 break;
480 case INT_CLASS_CRIT:
481 set_guest_csrr(vcpu, vcpu->arch.pc,
482 vcpu->arch.shared->msr);
483 break;
484 case INT_CLASS_DBG:
485 set_guest_dsrr(vcpu, vcpu->arch.pc,
486 vcpu->arch.shared->msr);
487 break;
488 case INT_CLASS_MC:
489 set_guest_mcsrr(vcpu, vcpu->arch.pc,
490 vcpu->arch.shared->msr);
491 break;
492 }
493
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600494 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
Liu Yudaf5e272010-02-02 19:44:35 +0800495 if (update_esr == true)
Bharat Bhushandc168542014-07-17 17:01:38 +0530496 kvmppc_set_esr(vcpu, vcpu->arch.queued_esr);
Liu Yudaf5e272010-02-02 19:44:35 +0800497 if (update_dear == true)
Bharat Bhushana5414d42014-07-17 17:01:37 +0530498 kvmppc_set_dar(vcpu, vcpu->arch.queued_dear);
Scott Wood5df554a2013-04-12 14:08:46 +0000499 if (update_epr == true) {
500 if (vcpu->arch.epr_flags & KVMPPC_EPR_USER)
501 kvm_make_request(KVM_REQ_EPR_EXIT, vcpu);
Scott Woodeb1e4f42013-04-12 14:08:47 +0000502 else if (vcpu->arch.epr_flags & KVMPPC_EPR_KERNEL) {
503 BUG_ON(vcpu->arch.irq_type != KVMPPC_IRQ_MPIC);
504 kvmppc_mpic_set_epr(vcpu);
505 }
Scott Wood5df554a2013-04-12 14:08:46 +0000506 }
Mihai Caraman95e90b42012-10-11 06:13:26 +0000507
508 new_msr &= msr_mask;
509#if defined(CONFIG_64BIT)
510 if (vcpu->arch.epcr & SPRN_EPCR_ICM)
511 new_msr |= MSR_CM;
512#endif
513 kvmppc_set_msr(vcpu, new_msr);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600514
Alexander Grafc5335f12010-08-30 14:03:24 +0200515 if (!keep_irq)
516 clear_bit(priority, &vcpu->arch.pending_exceptions);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600517 }
518
Scott Woodd30f6e42011-12-20 15:34:43 +0000519#ifdef CONFIG_KVM_BOOKE_HV
520 /*
521 * If an interrupt is pending but masked, raise a guest doorbell
522 * so that we are notified when the guest enables the relevant
523 * MSR bit.
524 */
525 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
526 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
527 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
528 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
529 if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
530 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
531#endif
532
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600533 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500534}
535
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000536/*
537 * Return the number of jiffies until the next timeout. If the timeout is
538 * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA
539 * because the larger value can break the timer APIs.
540 */
541static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu)
542{
543 u64 tb, wdt_tb, wdt_ticks = 0;
544 u64 nr_jiffies = 0;
545 u32 period = TCR_GET_WP(vcpu->arch.tcr);
546
547 wdt_tb = 1ULL << (63 - period);
548 tb = get_tb();
549 /*
550 * The watchdog timeout will hapeen when TB bit corresponding
551 * to watchdog will toggle from 0 to 1.
552 */
553 if (tb & wdt_tb)
554 wdt_ticks = wdt_tb;
555
556 wdt_ticks += wdt_tb - (tb & (wdt_tb - 1));
557
558 /* Convert timebase ticks to jiffies */
559 nr_jiffies = wdt_ticks;
560
561 if (do_div(nr_jiffies, tb_ticks_per_jiffy))
562 nr_jiffies++;
563
564 return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA);
565}
566
567static void arm_next_watchdog(struct kvm_vcpu *vcpu)
568{
569 unsigned long nr_jiffies;
570 unsigned long flags;
571
572 /*
573 * If TSR_ENW and TSR_WIS are not set then no need to exit to
574 * userspace, so clear the KVM_REQ_WATCHDOG request.
575 */
576 if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS))
577 clear_bit(KVM_REQ_WATCHDOG, &vcpu->requests);
578
579 spin_lock_irqsave(&vcpu->arch.wdt_lock, flags);
580 nr_jiffies = watchdog_next_timeout(vcpu);
581 /*
582 * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA
583 * then do not run the watchdog timer as this can break timer APIs.
584 */
585 if (nr_jiffies < NEXT_TIMER_MAX_DELTA)
586 mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies);
587 else
588 del_timer(&vcpu->arch.wdt_timer);
589 spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags);
590}
591
592void kvmppc_watchdog_func(unsigned long data)
593{
594 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
595 u32 tsr, new_tsr;
596 int final;
597
598 do {
599 new_tsr = tsr = vcpu->arch.tsr;
600 final = 0;
601
602 /* Time out event */
603 if (tsr & TSR_ENW) {
604 if (tsr & TSR_WIS)
605 final = 1;
606 else
607 new_tsr = tsr | TSR_WIS;
608 } else {
609 new_tsr = tsr | TSR_ENW;
610 }
611 } while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr);
612
613 if (new_tsr & TSR_WIS) {
614 smp_wmb();
615 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
616 kvm_vcpu_kick(vcpu);
617 }
618
619 /*
620 * If this is final watchdog expiry and some action is required
621 * then exit to userspace.
622 */
623 if (final && (vcpu->arch.tcr & TCR_WRC_MASK) &&
624 vcpu->arch.watchdog_enabled) {
625 smp_wmb();
626 kvm_make_request(KVM_REQ_WATCHDOG, vcpu);
627 kvm_vcpu_kick(vcpu);
628 }
629
630 /*
631 * Stop running the watchdog timer after final expiration to
632 * prevent the host from being flooded with timers if the
633 * guest sets a short period.
634 * Timers will resume when TSR/TCR is updated next time.
635 */
636 if (!final)
637 arm_next_watchdog(vcpu);
638}
639
Scott Wooddfd4d472011-11-17 12:39:59 +0000640static void update_timer_ints(struct kvm_vcpu *vcpu)
641{
642 if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
643 kvmppc_core_queue_dec(vcpu);
644 else
645 kvmppc_core_dequeue_dec(vcpu);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000646
647 if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS))
648 kvmppc_core_queue_watchdog(vcpu);
649 else
650 kvmppc_core_dequeue_watchdog(vcpu);
Scott Wooddfd4d472011-11-17 12:39:59 +0000651}
652
Scott Woodc59a6a32011-11-08 18:23:25 -0600653static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500654{
655 unsigned long *pending = &vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500656 unsigned int priority;
657
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600658 priority = __ffs(*pending);
Alexander Graf8b3a00f2012-02-16 14:12:46 +0000659 while (priority < BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600660 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500661 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500662
663 priority = find_next_bit(pending,
664 BITS_PER_BYTE * sizeof(*pending),
665 priority + 1);
666 }
Alexander Graf90bba352010-07-29 14:47:51 +0200667
668 /* Tell the guest about our interrupt status */
Scott Wood29ac26e2011-11-08 18:23:27 -0600669 vcpu->arch.shared->int_pending = !!*pending;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500670}
671
Scott Woodc59a6a32011-11-08 18:23:25 -0600672/* Check pending exceptions and deliver one, if possible. */
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000673int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
Scott Woodc59a6a32011-11-08 18:23:25 -0600674{
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000675 int r = 0;
Scott Woodc59a6a32011-11-08 18:23:25 -0600676 WARN_ON_ONCE(!irqs_disabled());
677
678 kvmppc_core_check_exceptions(vcpu);
679
Alexander Grafb8c649a2012-12-20 04:52:39 +0000680 if (vcpu->requests) {
681 /* Exception delivery raised request; start over */
682 return 1;
683 }
684
Scott Woodc59a6a32011-11-08 18:23:25 -0600685 if (vcpu->arch.shared->msr & MSR_WE) {
686 local_irq_enable();
687 kvm_vcpu_block(vcpu);
Alexander Graf966cd0f2012-03-14 16:55:08 +0100688 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Scott Wood6c85f522014-01-09 19:18:40 -0600689 hard_irq_disable();
Scott Woodc59a6a32011-11-08 18:23:25 -0600690
691 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000692 r = 1;
Scott Woodc59a6a32011-11-08 18:23:25 -0600693 };
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000694
695 return r;
696}
697
Alexander Graf7c973a22012-08-13 12:50:35 +0200698int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
Alexander Graf4ffc6352012-08-08 20:31:13 +0200699{
Alexander Graf7c973a22012-08-13 12:50:35 +0200700 int r = 1; /* Indicate we want to get back into the guest */
701
Alexander Graf2d8185d2012-08-10 12:31:12 +0200702 if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
703 update_timer_ints(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200704#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Alexander Graf2d8185d2012-08-10 12:31:12 +0200705 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
706 kvmppc_core_flush_tlb(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200707#endif
Alexander Graf7c973a22012-08-13 12:50:35 +0200708
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000709 if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) {
710 vcpu->run->exit_reason = KVM_EXIT_WATCHDOG;
711 r = 0;
712 }
713
Alexander Graf1c810632013-01-04 18:12:48 +0100714 if (kvm_check_request(KVM_REQ_EPR_EXIT, vcpu)) {
715 vcpu->run->epr.epr = 0;
716 vcpu->arch.epr_needed = true;
717 vcpu->run->exit_reason = KVM_EXIT_EPR;
718 r = 0;
719 }
720
Alexander Graf7c973a22012-08-13 12:50:35 +0200721 return r;
Alexander Graf4ffc6352012-08-08 20:31:13 +0200722}
723
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000724int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
725{
Alexander Graf7ee78852012-08-13 12:44:41 +0200726 int ret, s;
Scott Woodf5f97212013-11-22 15:52:29 -0600727 struct debug_reg debug;
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000728
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200729 if (!vcpu->arch.sane) {
730 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
731 return -EINVAL;
732 }
733
Alexander Graf7ee78852012-08-13 12:44:41 +0200734 s = kvmppc_prepare_to_enter(vcpu);
735 if (s <= 0) {
Alexander Graf7ee78852012-08-13 12:44:41 +0200736 ret = s;
Scott Wood1d1ef222011-11-08 16:11:59 -0600737 goto out;
738 }
Scott Wood6c85f522014-01-09 19:18:40 -0600739 /* interrupts now hard-disabled */
Scott Wood1d1ef222011-11-08 16:11:59 -0600740
Scott Wood8fae8452011-12-20 15:34:45 +0000741#ifdef CONFIG_PPC_FPU
742 /* Save userspace FPU state in stack */
743 enable_kernel_fp();
Scott Wood8fae8452011-12-20 15:34:45 +0000744
745 /*
746 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
Mihai Caraman3efc7da2014-08-20 16:36:22 +0300747 * as always using the FPU.
Scott Wood8fae8452011-12-20 15:34:45 +0000748 */
Scott Wood8fae8452011-12-20 15:34:45 +0000749 kvmppc_load_guest_fp(vcpu);
750#endif
751
Mihai Caraman95d80a22014-08-20 16:36:23 +0300752#ifdef CONFIG_ALTIVEC
753 /* Save userspace AltiVec state in stack */
754 if (cpu_has_feature(CPU_FTR_ALTIVEC))
755 enable_kernel_altivec();
756 /*
757 * Since we can't trap on MSR_VEC in GS-mode, we consider the guest
758 * as always using the AltiVec.
759 */
760 kvmppc_load_guest_altivec(vcpu);
761#endif
762
Bharat Bhushance11e482013-07-04 12:27:47 +0530763 /* Switch to guest debug context */
Bharat Bhushan348ba712014-08-06 12:08:55 +0530764 debug = vcpu->arch.dbg_reg;
Scott Woodf5f97212013-11-22 15:52:29 -0600765 switch_booke_debug_regs(&debug);
766 debug = current->thread.debug;
Bharat Bhushan348ba712014-08-06 12:08:55 +0530767 current->thread.debug = vcpu->arch.dbg_reg;
Bharat Bhushance11e482013-07-04 12:27:47 +0530768
Bharat Bhushan08c9a182013-11-18 11:18:54 +0530769 vcpu->arch.pgdir = current->mm->pgd;
Scott Wood5f1c2482013-07-10 17:47:39 -0500770 kvmppc_fix_ee_before_entry();
Scott Woodf8941fbe2013-06-11 11:38:31 -0500771
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000772 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
Scott Wood8fae8452011-12-20 15:34:45 +0000773
Alexander Graf24afa372012-08-12 12:42:30 +0200774 /* No need for kvm_guest_exit. It's done in handle_exit.
775 We also get here with interrupts enabled. */
776
Bharat Bhushance11e482013-07-04 12:27:47 +0530777 /* Switch back to user space debug context */
Scott Woodf5f97212013-11-22 15:52:29 -0600778 switch_booke_debug_regs(&debug);
779 current->thread.debug = debug;
Bharat Bhushance11e482013-07-04 12:27:47 +0530780
Scott Wood8fae8452011-12-20 15:34:45 +0000781#ifdef CONFIG_PPC_FPU
782 kvmppc_save_guest_fp(vcpu);
Scott Wood8fae8452011-12-20 15:34:45 +0000783#endif
784
Mihai Caraman95d80a22014-08-20 16:36:23 +0300785#ifdef CONFIG_ALTIVEC
786 kvmppc_save_guest_altivec(vcpu);
787#endif
788
Scott Wood1d1ef222011-11-08 16:11:59 -0600789out:
Alexander Grafd69c6432012-08-08 20:44:20 +0200790 vcpu->mode = OUTSIDE_GUEST_MODE;
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000791 return ret;
792}
793
Scott Woodd30f6e42011-12-20 15:34:43 +0000794static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
795{
796 enum emulation_result er;
797
798 er = kvmppc_emulate_instruction(run, vcpu);
799 switch (er) {
800 case EMULATE_DONE:
801 /* don't overwrite subtypes, just account kvm_stats */
802 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
803 /* Future optimization: only reload non-volatiles if
804 * they were actually modified by emulation. */
805 return RESUME_GUEST_NV;
806
Mihai Caraman51f04722014-07-23 19:06:21 +0300807 case EMULATE_AGAIN:
808 return RESUME_GUEST;
809
Scott Woodd30f6e42011-12-20 15:34:43 +0000810 case EMULATE_FAIL:
Scott Woodd30f6e42011-12-20 15:34:43 +0000811 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
812 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
813 /* For debugging, encode the failing instruction and
814 * report it to userspace. */
815 run->hw.hardware_exit_reason = ~0ULL << 32;
816 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
Alexander Grafd1ff5492012-02-16 13:24:03 +0000817 kvmppc_core_queue_program(vcpu, ESR_PIL);
Scott Woodd30f6e42011-12-20 15:34:43 +0000818 return RESUME_HOST;
819
Bharat Bhushan9b4f5302013-04-08 00:32:15 +0000820 case EMULATE_EXIT_USER:
821 return RESUME_HOST;
822
Scott Woodd30f6e42011-12-20 15:34:43 +0000823 default:
824 BUG();
825 }
826}
827
Bharat Bhushance11e482013-07-04 12:27:47 +0530828static int kvmppc_handle_debug(struct kvm_run *run, struct kvm_vcpu *vcpu)
829{
Bharat Bhushan348ba712014-08-06 12:08:55 +0530830 struct debug_reg *dbg_reg = &(vcpu->arch.dbg_reg);
Bharat Bhushance11e482013-07-04 12:27:47 +0530831 u32 dbsr = vcpu->arch.dbsr;
832
Bharat Bhushan2f699a52014-08-13 14:39:44 +0530833 if (vcpu->guest_debug == 0) {
834 /*
835 * Debug resources belong to Guest.
836 * Imprecise debug event is not injected
837 */
838 if (dbsr & DBSR_IDE) {
839 dbsr &= ~DBSR_IDE;
840 if (!dbsr)
841 return RESUME_GUEST;
842 }
843
844 if (dbsr && (vcpu->arch.shared->msr & MSR_DE) &&
845 (vcpu->arch.dbg_reg.dbcr0 & DBCR0_IDM))
846 kvmppc_core_queue_debug(vcpu);
847
848 /* Inject a program interrupt if trap debug is not allowed */
849 if ((dbsr & DBSR_TIE) && !(vcpu->arch.shared->msr & MSR_DE))
850 kvmppc_core_queue_program(vcpu, ESR_PTR);
851
852 return RESUME_GUEST;
853 }
854
855 /*
856 * Debug resource owned by userspace.
857 * Clear guest dbsr (vcpu->arch.dbsr)
858 */
Bharat Bhushan21909912014-08-06 12:08:54 +0530859 vcpu->arch.dbsr = 0;
Bharat Bhushance11e482013-07-04 12:27:47 +0530860 run->debug.arch.status = 0;
861 run->debug.arch.address = vcpu->arch.pc;
862
863 if (dbsr & (DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4)) {
864 run->debug.arch.status |= KVMPPC_DEBUG_BREAKPOINT;
865 } else {
866 if (dbsr & (DBSR_DAC1W | DBSR_DAC2W))
867 run->debug.arch.status |= KVMPPC_DEBUG_WATCH_WRITE;
868 else if (dbsr & (DBSR_DAC1R | DBSR_DAC2R))
869 run->debug.arch.status |= KVMPPC_DEBUG_WATCH_READ;
870 if (dbsr & (DBSR_DAC1R | DBSR_DAC1W))
871 run->debug.arch.address = dbg_reg->dac1;
872 else if (dbsr & (DBSR_DAC2R | DBSR_DAC2W))
873 run->debug.arch.address = dbg_reg->dac2;
874 }
875
876 return RESUME_HOST;
877}
878
Alexander Graf4e642cc2012-02-20 23:57:26 +0100879static void kvmppc_fill_pt_regs(struct pt_regs *regs)
880{
881 ulong r1, ip, msr, lr;
882
883 asm("mr %0, 1" : "=r"(r1));
884 asm("mflr %0" : "=r"(lr));
885 asm("mfmsr %0" : "=r"(msr));
886 asm("bl 1f; 1: mflr %0" : "=r"(ip));
887
888 memset(regs, 0, sizeof(*regs));
889 regs->gpr[1] = r1;
890 regs->nip = ip;
891 regs->msr = msr;
892 regs->link = lr;
893}
894
Bharat Bhushan6328e592012-06-20 05:56:53 +0000895/*
896 * For interrupts needed to be handled by host interrupt handlers,
897 * corresponding host handler are called from here in similar way
898 * (but not exact) as they are called from low level handler
899 * (such as from arch/powerpc/kernel/head_fsl_booke.S).
900 */
Alexander Graf4e642cc2012-02-20 23:57:26 +0100901static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
902 unsigned int exit_nr)
903{
904 struct pt_regs regs;
905
906 switch (exit_nr) {
907 case BOOKE_INTERRUPT_EXTERNAL:
908 kvmppc_fill_pt_regs(&regs);
909 do_IRQ(&regs);
910 break;
911 case BOOKE_INTERRUPT_DECREMENTER:
912 kvmppc_fill_pt_regs(&regs);
913 timer_interrupt(&regs);
914 break;
Tiejun Chen5f17ce82013-05-13 10:00:45 +0800915#if defined(CONFIG_PPC_DOORBELL)
Alexander Graf4e642cc2012-02-20 23:57:26 +0100916 case BOOKE_INTERRUPT_DOORBELL:
917 kvmppc_fill_pt_regs(&regs);
918 doorbell_exception(&regs);
919 break;
920#endif
921 case BOOKE_INTERRUPT_MACHINE_CHECK:
922 /* FIXME */
923 break;
Alexander Graf7cc1e8e2012-02-22 16:26:34 +0100924 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
925 kvmppc_fill_pt_regs(&regs);
926 performance_monitor_exception(&regs);
927 break;
Bharat Bhushan6328e592012-06-20 05:56:53 +0000928 case BOOKE_INTERRUPT_WATCHDOG:
929 kvmppc_fill_pt_regs(&regs);
930#ifdef CONFIG_BOOKE_WDT
931 WatchdogException(&regs);
932#else
933 unknown_exception(&regs);
934#endif
935 break;
936 case BOOKE_INTERRUPT_CRITICAL:
Tudor Laurentiu845ac982015-05-18 15:44:27 +0300937 kvmppc_fill_pt_regs(&regs);
Bharat Bhushan6328e592012-06-20 05:56:53 +0000938 unknown_exception(&regs);
939 break;
Bharat Bhushance11e482013-07-04 12:27:47 +0530940 case BOOKE_INTERRUPT_DEBUG:
941 /* Save DBSR before preemption is enabled */
942 vcpu->arch.dbsr = mfspr(SPRN_DBSR);
943 kvmppc_clear_dbsr();
944 break;
Alexander Graf4e642cc2012-02-20 23:57:26 +0100945 }
946}
947
Mihai Caramanf5250472014-07-23 19:06:22 +0300948static int kvmppc_resume_inst_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
949 enum emulation_result emulated, u32 last_inst)
950{
951 switch (emulated) {
952 case EMULATE_AGAIN:
953 return RESUME_GUEST;
954
955 case EMULATE_FAIL:
956 pr_debug("%s: load instruction from guest address %lx failed\n",
957 __func__, vcpu->arch.pc);
958 /* For debugging, encode the failing instruction and
959 * report it to userspace. */
960 run->hw.hardware_exit_reason = ~0ULL << 32;
961 run->hw.hardware_exit_reason |= last_inst;
962 kvmppc_core_queue_program(vcpu, ESR_PIL);
963 return RESUME_HOST;
964
965 default:
966 BUG();
967 }
968}
969
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500970/**
971 * kvmppc_handle_exit
972 *
973 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
974 */
975int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
976 unsigned int exit_nr)
977{
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500978 int r = RESUME_HOST;
Alexander Graf7ee78852012-08-13 12:44:41 +0200979 int s;
Scott Woodf1e89022013-06-06 19:16:31 -0500980 int idx;
Mihai Caramanf5250472014-07-23 19:06:22 +0300981 u32 last_inst = KVM_INST_FETCH_FAILED;
982 enum emulation_result emulated = EMULATE_DONE;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500983
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600984 /* update before a new last_exit_type is rewritten */
985 kvmppc_update_timing_stats(vcpu);
986
Alexander Graf4e642cc2012-02-20 23:57:26 +0100987 /* restart interrupts if they were meant for the host */
988 kvmppc_restart_interrupt(vcpu, exit_nr);
Scott Woodd30f6e42011-12-20 15:34:43 +0000989
Mihai Caramanf5250472014-07-23 19:06:22 +0300990 /*
991 * get last instruction before beeing preempted
992 * TODO: for e6500 check also BOOKE_INTERRUPT_LRAT_ERROR & ESR_DATA
993 */
994 switch (exit_nr) {
995 case BOOKE_INTERRUPT_DATA_STORAGE:
996 case BOOKE_INTERRUPT_DTLB_MISS:
997 case BOOKE_INTERRUPT_HV_PRIV:
Alexander Graf8d0eff62014-09-10 14:37:29 +0200998 emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
Mihai Caramanf5250472014-07-23 19:06:22 +0300999 break;
Madhavan Srinivasan033aaa12014-09-09 22:37:36 +05301000 case BOOKE_INTERRUPT_PROGRAM:
1001 /* SW breakpoints arrive as illegal instructions on HV */
1002 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Alexander Graf8d0eff62014-09-10 14:37:29 +02001003 emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst);
Madhavan Srinivasan033aaa12014-09-09 22:37:36 +05301004 break;
Mihai Caramanf5250472014-07-23 19:06:22 +03001005 default:
1006 break;
1007 }
1008
Alexander Graf97c95052012-08-02 15:10:00 +02001009 trace_kvm_exit(exit_nr, vcpu);
Paolo Bonzinie233d542015-04-30 14:39:40 +02001010 __kvm_guest_exit();
1011
1012 local_irq_enable();
Alexander Graf97c95052012-08-02 15:10:00 +02001013
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001014 run->exit_reason = KVM_EXIT_UNKNOWN;
1015 run->ready_for_interrupt_injection = 1;
1016
Mihai Caramanf5250472014-07-23 19:06:22 +03001017 if (emulated != EMULATE_DONE) {
1018 r = kvmppc_resume_inst_load(run, vcpu, emulated, last_inst);
1019 goto out;
1020 }
1021
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001022 switch (exit_nr) {
1023 case BOOKE_INTERRUPT_MACHINE_CHECK:
Alexander Grafc35c9d82012-02-20 12:21:18 +01001024 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
1025 kvmppc_dump_vcpu(vcpu);
1026 /* For debugging, send invalid exit reason to user space */
1027 run->hw.hardware_exit_reason = ~1ULL << 32;
1028 run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
1029 r = RESUME_HOST;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001030 break;
1031
1032 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -06001033 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -06001034 r = RESUME_GUEST;
1035 break;
1036
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001037 case BOOKE_INTERRUPT_DECREMENTER:
Hollis Blanchard7b701592008-12-02 15:51:58 -06001038 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001039 r = RESUME_GUEST;
1040 break;
1041
Bharat Bhushan6328e592012-06-20 05:56:53 +00001042 case BOOKE_INTERRUPT_WATCHDOG:
1043 r = RESUME_GUEST;
1044 break;
1045
Scott Woodd30f6e42011-12-20 15:34:43 +00001046 case BOOKE_INTERRUPT_DOORBELL:
1047 kvmppc_account_exit(vcpu, DBELL_EXITS);
Scott Woodd30f6e42011-12-20 15:34:43 +00001048 r = RESUME_GUEST;
1049 break;
1050
1051 case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
1052 kvmppc_account_exit(vcpu, GDBELL_EXITS);
1053
1054 /*
1055 * We are here because there is a pending guest interrupt
1056 * which could not be delivered as MSR_CE or MSR_ME was not
1057 * set. Once we break from here we will retry delivery.
1058 */
1059 r = RESUME_GUEST;
1060 break;
1061
1062 case BOOKE_INTERRUPT_GUEST_DBELL:
1063 kvmppc_account_exit(vcpu, GDBELL_EXITS);
1064
1065 /*
1066 * We are here because there is a pending guest interrupt
1067 * which could not be delivered as MSR_EE was not set. Once
1068 * we break from here we will retry delivery.
1069 */
1070 r = RESUME_GUEST;
1071 break;
1072
Alexander Graf95f2e922012-02-20 22:45:12 +01001073 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
1074 r = RESUME_GUEST;
1075 break;
1076
Scott Woodd30f6e42011-12-20 15:34:43 +00001077 case BOOKE_INTERRUPT_HV_PRIV:
1078 r = emulation_exit(run, vcpu);
1079 break;
1080
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001081 case BOOKE_INTERRUPT_PROGRAM:
Madhavan Srinivasan033aaa12014-09-09 22:37:36 +05301082 if ((vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) &&
1083 (last_inst == KVMPPC_INST_SW_BREAKPOINT)) {
1084 /*
1085 * We are here because of an SW breakpoint instr,
1086 * so lets return to host to handle.
1087 */
1088 r = kvmppc_handle_debug(run, vcpu);
1089 run->exit_reason = KVM_EXIT_DEBUG;
1090 kvmppc_account_exit(vcpu, DEBUG_EXITS);
1091 break;
1092 }
1093
Scott Woodd30f6e42011-12-20 15:34:43 +00001094 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
Alexander Graf02685972012-02-20 12:33:22 +01001095 /*
1096 * Program traps generated by user-level software must
1097 * be handled by the guest kernel.
1098 *
1099 * In GS mode, hypervisor privileged instructions trap
1100 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
1101 * actual program interrupts, handled by the guest.
1102 */
Liu Yudaf5e272010-02-02 19:44:35 +08001103 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001104 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -06001105 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001106 break;
1107 }
1108
Scott Woodd30f6e42011-12-20 15:34:43 +00001109 r = emulation_exit(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001110 break;
1111
Christian Ehrhardtde368dc2008-04-29 18:18:23 +02001112 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001113 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001114 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +02001115 r = RESUME_GUEST;
1116 break;
1117
Scott Wood4cd35f672011-06-14 18:34:31 -05001118#ifdef CONFIG_SPE
1119 case BOOKE_INTERRUPT_SPE_UNAVAIL: {
1120 if (vcpu->arch.shared->msr & MSR_SPE)
1121 kvmppc_vcpu_enable_spe(vcpu);
1122 else
1123 kvmppc_booke_queue_irqprio(vcpu,
1124 BOOKE_IRQPRIO_SPE_UNAVAIL);
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -06001125 r = RESUME_GUEST;
1126 break;
Scott Wood4cd35f672011-06-14 18:34:31 -05001127 }
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -06001128
1129 case BOOKE_INTERRUPT_SPE_FP_DATA:
1130 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
1131 r = RESUME_GUEST;
1132 break;
1133
1134 case BOOKE_INTERRUPT_SPE_FP_ROUND:
1135 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
1136 r = RESUME_GUEST;
1137 break;
Mihai Caraman95d80a22014-08-20 16:36:23 +03001138#elif defined(CONFIG_SPE_POSSIBLE)
Scott Wood4cd35f672011-06-14 18:34:31 -05001139 case BOOKE_INTERRUPT_SPE_UNAVAIL:
1140 /*
1141 * Guest wants SPE, but host kernel doesn't support it. Send
1142 * an "unimplemented operation" program check to the guest.
1143 */
1144 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
1145 r = RESUME_GUEST;
1146 break;
1147
1148 /*
1149 * These really should never happen without CONFIG_SPE,
1150 * as we should never enable the real MSR[SPE] in the guest.
1151 */
1152 case BOOKE_INTERRUPT_SPE_FP_DATA:
1153 case BOOKE_INTERRUPT_SPE_FP_ROUND:
1154 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
1155 __func__, exit_nr, vcpu->arch.pc);
1156 run->hw.hardware_exit_reason = exit_nr;
1157 r = RESUME_HOST;
1158 break;
Mihai Caraman95d80a22014-08-20 16:36:23 +03001159#endif /* CONFIG_SPE_POSSIBLE */
1160
1161/*
1162 * On cores with Vector category, KVM is loaded only if CONFIG_ALTIVEC,
1163 * see kvmppc_core_check_processor_compat().
1164 */
1165#ifdef CONFIG_ALTIVEC
1166 case BOOKE_INTERRUPT_ALTIVEC_UNAVAIL:
1167 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALTIVEC_UNAVAIL);
1168 r = RESUME_GUEST;
1169 break;
1170
1171 case BOOKE_INTERRUPT_ALTIVEC_ASSIST:
1172 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALTIVEC_ASSIST);
1173 r = RESUME_GUEST;
1174 break;
Scott Wood4cd35f672011-06-14 18:34:31 -05001175#endif
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -06001176
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001177 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +08001178 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
1179 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001180 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001181 r = RESUME_GUEST;
1182 break;
1183
1184 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +08001185 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001186 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001187 r = RESUME_GUEST;
1188 break;
1189
Alexander Graf011da892013-01-31 14:17:38 +01001190 case BOOKE_INTERRUPT_ALIGNMENT:
1191 kvmppc_core_queue_alignment(vcpu, vcpu->arch.fault_dear,
1192 vcpu->arch.fault_esr);
1193 r = RESUME_GUEST;
1194 break;
1195
Scott Woodd30f6e42011-12-20 15:34:43 +00001196#ifdef CONFIG_KVM_BOOKE_HV
1197 case BOOKE_INTERRUPT_HV_SYSCALL:
1198 if (!(vcpu->arch.shared->msr & MSR_PR)) {
1199 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1200 } else {
1201 /*
1202 * hcall from guest userspace -- send privileged
1203 * instruction program check.
1204 */
1205 kvmppc_core_queue_program(vcpu, ESR_PPR);
1206 }
1207
1208 r = RESUME_GUEST;
1209 break;
1210#else
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001211 case BOOKE_INTERRUPT_SYSCALL:
Alexander Graf2a342ed2010-07-29 14:47:48 +02001212 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1213 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
1214 /* KVM PV hypercalls */
1215 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1216 r = RESUME_GUEST;
1217 } else {
1218 /* Guest syscalls */
1219 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
1220 }
Hollis Blanchard7b701592008-12-02 15:51:58 -06001221 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001222 r = RESUME_GUEST;
1223 break;
Scott Woodd30f6e42011-12-20 15:34:43 +00001224#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001225
1226 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001227 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001228 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001229 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001230 gfn_t gfn;
1231
Alexander Grafbf7ca4b2012-02-15 23:40:00 +00001232#ifdef CONFIG_KVM_E500V2
Scott Wooda4cd8b22011-06-14 18:34:41 -05001233 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1234 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
1235 kvmppc_map_magic(vcpu);
1236 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
1237 r = RESUME_GUEST;
1238
1239 break;
1240 }
1241#endif
1242
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001243 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -06001244 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001245 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001246 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +08001247 kvmppc_core_queue_dtlb_miss(vcpu,
1248 vcpu->arch.fault_dear,
1249 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -06001250 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001251 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001252 r = RESUME_GUEST;
1253 break;
1254 }
1255
Scott Woodf1e89022013-06-06 19:16:31 -05001256 idx = srcu_read_lock(&vcpu->kvm->srcu);
1257
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -06001258 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001259 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001260
1261 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1262 /* The guest TLB had a mapping, but the shadow TLB
1263 * didn't, and it is RAM. This could be because:
1264 * a) the entry is mapping the host kernel, or
1265 * b) the guest used a large mapping which we're faking
1266 * Either way, we need to satisfy the fault without
1267 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -06001268 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001269 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001270 r = RESUME_GUEST;
1271 } else {
1272 /* Guest has mapped and accessed a page which is not
1273 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001274 vcpu->arch.paddr_accessed = gpaddr;
Alexander Graf6020c0f2012-03-12 02:26:30 +01001275 vcpu->arch.vaddr_accessed = eaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001276 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001277 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001278 }
1279
Scott Woodf1e89022013-06-06 19:16:31 -05001280 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001281 break;
1282 }
1283
1284 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001285 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -06001286 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001287 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001288 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001289
1290 r = RESUME_GUEST;
1291
1292 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -06001293 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001294 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001295 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001296 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -06001297 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001298 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001299 break;
1300 }
1301
Hollis Blanchard7b701592008-12-02 15:51:58 -06001302 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001303
Scott Woodf1e89022013-06-06 19:16:31 -05001304 idx = srcu_read_lock(&vcpu->kvm->srcu);
1305
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -06001306 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -06001307 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001308
1309 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1310 /* The guest TLB had a mapping, but the shadow TLB
1311 * didn't. This could be because:
1312 * a) the entry is mapping the host kernel, or
1313 * b) the guest used a large mapping which we're faking
1314 * Either way, we need to satisfy the fault without
1315 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -06001316 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001317 } else {
1318 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001319 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001320 }
1321
Scott Woodf1e89022013-06-06 19:16:31 -05001322 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001323 break;
1324 }
1325
Hollis Blanchard6a0ab732008-07-25 13:54:49 -05001326 case BOOKE_INTERRUPT_DEBUG: {
Bharat Bhushance11e482013-07-04 12:27:47 +05301327 r = kvmppc_handle_debug(run, vcpu);
1328 if (r == RESUME_HOST)
1329 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -06001330 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -05001331 break;
1332 }
1333
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001334 default:
1335 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
1336 BUG();
1337 }
1338
Mihai Caramanf5250472014-07-23 19:06:22 +03001339out:
Alexander Grafa8e4ef82012-02-16 14:07:37 +00001340 /*
1341 * To avoid clobbering exit_reason, only check for signals if we
1342 * aren't already exiting to userspace for some other reason.
1343 */
Alexander Graf03660ba2012-02-28 12:00:41 +01001344 if (!(r & RESUME_HOST)) {
Alexander Graf7ee78852012-08-13 12:44:41 +02001345 s = kvmppc_prepare_to_enter(vcpu);
Scott Wood6c85f522014-01-09 19:18:40 -06001346 if (s <= 0)
Alexander Graf7ee78852012-08-13 12:44:41 +02001347 r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
Scott Wood6c85f522014-01-09 19:18:40 -06001348 else {
1349 /* interrupts now hard-disabled */
Scott Wood5f1c2482013-07-10 17:47:39 -05001350 kvmppc_fix_ee_before_entry();
Mihai Caraman3efc7da2014-08-20 16:36:22 +03001351 kvmppc_load_guest_fp(vcpu);
Mihai Caraman95d80a22014-08-20 16:36:23 +03001352 kvmppc_load_guest_altivec(vcpu);
Alexander Graf03660ba2012-02-28 12:00:41 +01001353 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001354 }
1355
1356 return r;
1357}
1358
Bharat Bhushand26f22c2013-02-24 18:57:11 +00001359static void kvmppc_set_tsr(struct kvm_vcpu *vcpu, u32 new_tsr)
1360{
1361 u32 old_tsr = vcpu->arch.tsr;
1362
1363 vcpu->arch.tsr = new_tsr;
1364
1365 if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS))
1366 arm_next_watchdog(vcpu);
1367
1368 update_timer_ints(vcpu);
1369}
1370
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001371/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
1372int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1373{
Hollis Blanchard082decf2010-08-07 10:33:56 -07001374 int i;
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001375 int r;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001376
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001377 vcpu->arch.pc = 0;
Scott Woodb5904972011-11-08 18:23:30 -06001378 vcpu->arch.shared->pir = vcpu->vcpu_id;
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001379 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Scott Woodd30f6e42011-12-20 15:34:43 +00001380 kvmppc_set_msr(vcpu, 0);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001381
Scott Woodd30f6e42011-12-20 15:34:43 +00001382#ifndef CONFIG_KVM_BOOKE_HV
Bharat Bhushance11e482013-07-04 12:27:47 +05301383 vcpu->arch.shadow_msr = MSR_USER | MSR_IS | MSR_DS;
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001384 vcpu->arch.shadow_pid = 1;
Scott Woodd30f6e42011-12-20 15:34:43 +00001385 vcpu->arch.shared->msr = 0;
1386#endif
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001387
Hollis Blanchard082decf2010-08-07 10:33:56 -07001388 /* Eye-catching numbers so we know if the guest takes an interrupt
1389 * before it's programmed its own IVPR/IVORs. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001390 vcpu->arch.ivpr = 0x55550000;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001391 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
1392 vcpu->arch.ivor[i] = 0x7700 | i * 4;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001393
Hollis Blanchard73e75b42008-12-02 15:51:57 -06001394 kvmppc_init_timing_stats(vcpu);
1395
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001396 r = kvmppc_core_vcpu_setup(vcpu);
1397 kvmppc_sanity_check(vcpu);
1398 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001399}
1400
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001401int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
1402{
1403 /* setup watchdog timer once */
1404 spin_lock_init(&vcpu->arch.wdt_lock);
1405 setup_timer(&vcpu->arch.wdt_timer, kvmppc_watchdog_func,
1406 (unsigned long)vcpu);
1407
Bharat Bhushan2f699a52014-08-13 14:39:44 +05301408 /*
1409 * Clear DBSR.MRR to avoid guest debug interrupt as
1410 * this is of host interest
1411 */
1412 mtspr(SPRN_DBSR, DBSR_MRR);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001413 return 0;
1414}
1415
1416void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
1417{
1418 del_timer_sync(&vcpu->arch.wdt_timer);
1419}
1420
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001421int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1422{
1423 int i;
1424
1425 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001426 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001427 regs->ctr = vcpu->arch.ctr;
1428 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001429 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001430 regs->msr = vcpu->arch.shared->msr;
Bharat Bhushan31579ee2014-07-17 17:01:36 +05301431 regs->srr0 = kvmppc_get_srr0(vcpu);
1432 regs->srr1 = kvmppc_get_srr1(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001433 regs->pid = vcpu->arch.pid;
Bharat Bhushanc1b8a012014-07-17 17:01:39 +05301434 regs->sprg0 = kvmppc_get_sprg0(vcpu);
1435 regs->sprg1 = kvmppc_get_sprg1(vcpu);
1436 regs->sprg2 = kvmppc_get_sprg2(vcpu);
1437 regs->sprg3 = kvmppc_get_sprg3(vcpu);
1438 regs->sprg4 = kvmppc_get_sprg4(vcpu);
1439 regs->sprg5 = kvmppc_get_sprg5(vcpu);
1440 regs->sprg6 = kvmppc_get_sprg6(vcpu);
1441 regs->sprg7 = kvmppc_get_sprg7(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001442
1443 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001444 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001445
1446 return 0;
1447}
1448
1449int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1450{
1451 int i;
1452
1453 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001454 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001455 vcpu->arch.ctr = regs->ctr;
1456 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001457 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -06001458 kvmppc_set_msr(vcpu, regs->msr);
Bharat Bhushan31579ee2014-07-17 17:01:36 +05301459 kvmppc_set_srr0(vcpu, regs->srr0);
1460 kvmppc_set_srr1(vcpu, regs->srr1);
Scott Wood5ce941e2011-04-27 17:24:21 -05001461 kvmppc_set_pid(vcpu, regs->pid);
Bharat Bhushanc1b8a012014-07-17 17:01:39 +05301462 kvmppc_set_sprg0(vcpu, regs->sprg0);
1463 kvmppc_set_sprg1(vcpu, regs->sprg1);
1464 kvmppc_set_sprg2(vcpu, regs->sprg2);
1465 kvmppc_set_sprg3(vcpu, regs->sprg3);
1466 kvmppc_set_sprg4(vcpu, regs->sprg4);
1467 kvmppc_set_sprg5(vcpu, regs->sprg5);
1468 kvmppc_set_sprg6(vcpu, regs->sprg6);
1469 kvmppc_set_sprg7(vcpu, regs->sprg7);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001470
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001471 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1472 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001473
1474 return 0;
1475}
1476
Scott Wood5ce941e2011-04-27 17:24:21 -05001477static void get_sregs_base(struct kvm_vcpu *vcpu,
1478 struct kvm_sregs *sregs)
1479{
1480 u64 tb = get_tb();
1481
1482 sregs->u.e.features |= KVM_SREGS_E_BASE;
1483
1484 sregs->u.e.csrr0 = vcpu->arch.csrr0;
1485 sregs->u.e.csrr1 = vcpu->arch.csrr1;
1486 sregs->u.e.mcsr = vcpu->arch.mcsr;
Bharat Bhushandc168542014-07-17 17:01:38 +05301487 sregs->u.e.esr = kvmppc_get_esr(vcpu);
Bharat Bhushana5414d42014-07-17 17:01:37 +05301488 sregs->u.e.dear = kvmppc_get_dar(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001489 sregs->u.e.tsr = vcpu->arch.tsr;
1490 sregs->u.e.tcr = vcpu->arch.tcr;
1491 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
1492 sregs->u.e.tb = tb;
1493 sregs->u.e.vrsave = vcpu->arch.vrsave;
1494}
1495
1496static int set_sregs_base(struct kvm_vcpu *vcpu,
1497 struct kvm_sregs *sregs)
1498{
1499 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
1500 return 0;
1501
1502 vcpu->arch.csrr0 = sregs->u.e.csrr0;
1503 vcpu->arch.csrr1 = sregs->u.e.csrr1;
1504 vcpu->arch.mcsr = sregs->u.e.mcsr;
Bharat Bhushandc168542014-07-17 17:01:38 +05301505 kvmppc_set_esr(vcpu, sregs->u.e.esr);
Bharat Bhushana5414d42014-07-17 17:01:37 +05301506 kvmppc_set_dar(vcpu, sregs->u.e.dear);
Scott Wood5ce941e2011-04-27 17:24:21 -05001507 vcpu->arch.vrsave = sregs->u.e.vrsave;
Scott Wooddfd4d472011-11-17 12:39:59 +00001508 kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001509
Scott Wooddfd4d472011-11-17 12:39:59 +00001510 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
Scott Wood5ce941e2011-04-27 17:24:21 -05001511 vcpu->arch.dec = sregs->u.e.dec;
Scott Wooddfd4d472011-11-17 12:39:59 +00001512 kvmppc_emulate_dec(vcpu);
1513 }
Scott Wood5ce941e2011-04-27 17:24:21 -05001514
Bharat Bhushand26f22c2013-02-24 18:57:11 +00001515 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR)
1516 kvmppc_set_tsr(vcpu, sregs->u.e.tsr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001517
1518 return 0;
1519}
1520
1521static void get_sregs_arch206(struct kvm_vcpu *vcpu,
1522 struct kvm_sregs *sregs)
1523{
1524 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
1525
Scott Wood841741f2011-09-02 17:39:37 -05001526 sregs->u.e.pir = vcpu->vcpu_id;
Scott Wood5ce941e2011-04-27 17:24:21 -05001527 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
1528 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
1529 sregs->u.e.decar = vcpu->arch.decar;
1530 sregs->u.e.ivpr = vcpu->arch.ivpr;
1531}
1532
1533static int set_sregs_arch206(struct kvm_vcpu *vcpu,
1534 struct kvm_sregs *sregs)
1535{
1536 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
1537 return 0;
1538
Scott Wood841741f2011-09-02 17:39:37 -05001539 if (sregs->u.e.pir != vcpu->vcpu_id)
Scott Wood5ce941e2011-04-27 17:24:21 -05001540 return -EINVAL;
1541
1542 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
1543 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
1544 vcpu->arch.decar = sregs->u.e.decar;
1545 vcpu->arch.ivpr = sregs->u.e.ivpr;
1546
1547 return 0;
1548}
1549
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301550int kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
Scott Wood5ce941e2011-04-27 17:24:21 -05001551{
1552 sregs->u.e.features |= KVM_SREGS_E_IVOR;
1553
1554 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
1555 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
1556 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
1557 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
1558 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
1559 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
1560 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
1561 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
1562 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
1563 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
1564 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
1565 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
1566 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
1567 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
1568 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
1569 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301570 return 0;
Scott Wood5ce941e2011-04-27 17:24:21 -05001571}
1572
1573int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1574{
1575 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
1576 return 0;
1577
1578 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
1579 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
1580 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
1581 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
1582 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
1583 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
1584 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
1585 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
1586 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
1587 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
1588 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
1589 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
1590 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
1591 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
1592 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
1593 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
1594
1595 return 0;
1596}
1597
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001598int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1599 struct kvm_sregs *sregs)
1600{
Scott Wood5ce941e2011-04-27 17:24:21 -05001601 sregs->pvr = vcpu->arch.pvr;
1602
1603 get_sregs_base(vcpu, sregs);
1604 get_sregs_arch206(vcpu, sregs);
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301605 return vcpu->kvm->arch.kvm_ops->get_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001606}
1607
1608int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1609 struct kvm_sregs *sregs)
1610{
Scott Wood5ce941e2011-04-27 17:24:21 -05001611 int ret;
1612
1613 if (vcpu->arch.pvr != sregs->pvr)
1614 return -EINVAL;
1615
1616 ret = set_sregs_base(vcpu, sregs);
1617 if (ret < 0)
1618 return ret;
1619
1620 ret = set_sregs_arch206(vcpu, sregs);
1621 if (ret < 0)
1622 return ret;
1623
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05301624 return vcpu->kvm->arch.kvm_ops->set_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001625}
1626
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001627int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id,
1628 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001629{
Mihai Caraman35b299e2013-04-11 00:03:07 +00001630 int r = 0;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001631
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001632 switch (id) {
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001633 case KVM_REG_PPC_IAC1:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001634 *val = get_reg_val(id, vcpu->arch.dbg_reg.iac1);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001635 break;
Bharat Bhushan547465e2013-07-04 12:27:46 +05301636 case KVM_REG_PPC_IAC2:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001637 *val = get_reg_val(id, vcpu->arch.dbg_reg.iac2);
Bharat Bhushan547465e2013-07-04 12:27:46 +05301638 break;
1639#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1640 case KVM_REG_PPC_IAC3:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001641 *val = get_reg_val(id, vcpu->arch.dbg_reg.iac3);
Bharat Bhushan547465e2013-07-04 12:27:46 +05301642 break;
1643 case KVM_REG_PPC_IAC4:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001644 *val = get_reg_val(id, vcpu->arch.dbg_reg.iac4);
Bharat Bhushan547465e2013-07-04 12:27:46 +05301645 break;
1646#endif
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001647 case KVM_REG_PPC_DAC1:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001648 *val = get_reg_val(id, vcpu->arch.dbg_reg.dac1);
Bharat Bhushan547465e2013-07-04 12:27:46 +05301649 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001650 case KVM_REG_PPC_DAC2:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001651 *val = get_reg_val(id, vcpu->arch.dbg_reg.dac2);
Bharat Bhushan2c509672014-08-06 12:08:56 +05301652 break;
Alexander Graf324b3e62013-01-04 18:28:51 +01001653 case KVM_REG_PPC_EPR: {
Bharat Bhushan34f754b2014-07-17 17:01:40 +05301654 u32 epr = kvmppc_get_epr(vcpu);
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001655 *val = get_reg_val(id, epr);
Alexander Graf324b3e62013-01-04 18:28:51 +01001656 break;
1657 }
Mihai Caraman352df1d2012-10-11 06:13:29 +00001658#if defined(CONFIG_64BIT)
1659 case KVM_REG_PPC_EPCR:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001660 *val = get_reg_val(id, vcpu->arch.epcr);
Mihai Caraman352df1d2012-10-11 06:13:29 +00001661 break;
1662#endif
Bharat Bhushan78accda2013-02-24 18:57:12 +00001663 case KVM_REG_PPC_TCR:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001664 *val = get_reg_val(id, vcpu->arch.tcr);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001665 break;
1666 case KVM_REG_PPC_TSR:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001667 *val = get_reg_val(id, vcpu->arch.tsr);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001668 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001669 case KVM_REG_PPC_DEBUG_INST:
Madhavan Srinivasan033aaa12014-09-09 22:37:36 +05301670 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
Bharat Bhushan8c32a2e2013-03-20 20:24:58 +00001671 break;
Paul Mackerras8b75cbb2013-09-20 14:52:37 +10001672 case KVM_REG_PPC_VRSAVE:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001673 *val = get_reg_val(id, vcpu->arch.vrsave);
Bharat Bhushan8c32a2e2013-03-20 20:24:58 +00001674 break;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001675 default:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001676 r = vcpu->kvm->arch.kvm_ops->get_one_reg(vcpu, id, val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001677 break;
1678 }
Mihai Caraman35b299e2013-04-11 00:03:07 +00001679
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001680 return r;
Paul Mackerras31f34382011-12-12 12:26:50 +00001681}
1682
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001683int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id,
1684 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001685{
Mihai Caraman35b299e2013-04-11 00:03:07 +00001686 int r = 0;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001687
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001688 switch (id) {
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001689 case KVM_REG_PPC_IAC1:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001690 vcpu->arch.dbg_reg.iac1 = set_reg_val(id, *val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001691 break;
Bharat Bhushan547465e2013-07-04 12:27:46 +05301692 case KVM_REG_PPC_IAC2:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001693 vcpu->arch.dbg_reg.iac2 = set_reg_val(id, *val);
Bharat Bhushan547465e2013-07-04 12:27:46 +05301694 break;
1695#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1696 case KVM_REG_PPC_IAC3:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001697 vcpu->arch.dbg_reg.iac3 = set_reg_val(id, *val);
Bharat Bhushan547465e2013-07-04 12:27:46 +05301698 break;
1699 case KVM_REG_PPC_IAC4:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001700 vcpu->arch.dbg_reg.iac4 = set_reg_val(id, *val);
Bharat Bhushan547465e2013-07-04 12:27:46 +05301701 break;
1702#endif
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001703 case KVM_REG_PPC_DAC1:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001704 vcpu->arch.dbg_reg.dac1 = set_reg_val(id, *val);
Bharat Bhushan547465e2013-07-04 12:27:46 +05301705 break;
Mihai Caraman35b299e2013-04-11 00:03:07 +00001706 case KVM_REG_PPC_DAC2:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001707 vcpu->arch.dbg_reg.dac2 = set_reg_val(id, *val);
Bharat Bhushan2c509672014-08-06 12:08:56 +05301708 break;
Alexander Graf324b3e62013-01-04 18:28:51 +01001709 case KVM_REG_PPC_EPR: {
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001710 u32 new_epr = set_reg_val(id, *val);
Mihai Caraman35b299e2013-04-11 00:03:07 +00001711 kvmppc_set_epr(vcpu, new_epr);
Alexander Graf324b3e62013-01-04 18:28:51 +01001712 break;
1713 }
Mihai Caraman352df1d2012-10-11 06:13:29 +00001714#if defined(CONFIG_64BIT)
1715 case KVM_REG_PPC_EPCR: {
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001716 u32 new_epcr = set_reg_val(id, *val);
Mihai Caraman35b299e2013-04-11 00:03:07 +00001717 kvmppc_set_epcr(vcpu, new_epcr);
Mihai Caraman352df1d2012-10-11 06:13:29 +00001718 break;
1719 }
1720#endif
Bharat Bhushan78accda2013-02-24 18:57:12 +00001721 case KVM_REG_PPC_OR_TSR: {
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001722 u32 tsr_bits = set_reg_val(id, *val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001723 kvmppc_set_tsr_bits(vcpu, tsr_bits);
1724 break;
1725 }
1726 case KVM_REG_PPC_CLEAR_TSR: {
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001727 u32 tsr_bits = set_reg_val(id, *val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001728 kvmppc_clr_tsr_bits(vcpu, tsr_bits);
1729 break;
1730 }
1731 case KVM_REG_PPC_TSR: {
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001732 u32 tsr = set_reg_val(id, *val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001733 kvmppc_set_tsr(vcpu, tsr);
1734 break;
1735 }
1736 case KVM_REG_PPC_TCR: {
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001737 u32 tcr = set_reg_val(id, *val);
Bharat Bhushan78accda2013-02-24 18:57:12 +00001738 kvmppc_set_tcr(vcpu, tcr);
1739 break;
1740 }
Paul Mackerras8b75cbb2013-09-20 14:52:37 +10001741 case KVM_REG_PPC_VRSAVE:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001742 vcpu->arch.vrsave = set_reg_val(id, *val);
Paul Mackerras8b75cbb2013-09-20 14:52:37 +10001743 break;
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001744 default:
Mihai Caraman8a41ea52014-08-20 16:36:24 +03001745 r = vcpu->kvm->arch.kvm_ops->set_one_reg(vcpu, id, val);
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001746 break;
1747 }
Mihai Caraman35b299e2013-04-11 00:03:07 +00001748
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001749 return r;
Paul Mackerras31f34382011-12-12 12:26:50 +00001750}
1751
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001752int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1753{
1754 return -ENOTSUPP;
1755}
1756
1757int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1758{
1759 return -ENOTSUPP;
1760}
1761
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001762int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1763 struct kvm_translation *tr)
1764{
Avi Kivity98001d82010-05-13 11:05:49 +03001765 int r;
1766
Avi Kivity98001d82010-05-13 11:05:49 +03001767 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +03001768 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001769}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001770
Alexander Graf4e755752009-10-30 05:47:01 +00001771int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1772{
1773 return -ENOTSUPP;
1774}
1775
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05301776void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001777 struct kvm_memory_slot *dont)
1778{
1779}
1780
Aneesh Kumar K.V55870272013-10-07 22:18:00 +05301781int kvmppc_core_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001782 unsigned long npages)
1783{
1784 return 0;
1785}
1786
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001787int kvmppc_core_prepare_memory_region(struct kvm *kvm,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001788 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02001789 const struct kvm_userspace_memory_region *mem)
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001790{
1791 return 0;
1792}
1793
1794void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02001795 const struct kvm_userspace_memory_region *mem,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02001796 const struct kvm_memory_slot *old,
1797 const struct kvm_memory_slot *new)
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001798{
1799}
1800
1801void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001802{
1803}
1804
Mihai Caraman38f98822012-10-11 06:13:27 +00001805void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr)
1806{
1807#if defined(CONFIG_64BIT)
1808 vcpu->arch.epcr = new_epcr;
1809#ifdef CONFIG_KVM_BOOKE_HV
1810 vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM;
1811 if (vcpu->arch.epcr & SPRN_EPCR_ICM)
1812 vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM;
1813#endif
1814#endif
1815}
1816
Scott Wooddfd4d472011-11-17 12:39:59 +00001817void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1818{
1819 vcpu->arch.tcr = new_tcr;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001820 arm_next_watchdog(vcpu);
Scott Wooddfd4d472011-11-17 12:39:59 +00001821 update_timer_ints(vcpu);
1822}
1823
1824void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1825{
1826 set_bits(tsr_bits, &vcpu->arch.tsr);
1827 smp_wmb();
1828 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1829 kvm_vcpu_kick(vcpu);
1830}
1831
1832void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1833{
1834 clear_bits(tsr_bits, &vcpu->arch.tsr);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001835
1836 /*
1837 * We may have stopped the watchdog due to
1838 * being stuck on final expiration.
1839 */
1840 if (tsr_bits & (TSR_ENW | TSR_WIS))
1841 arm_next_watchdog(vcpu);
1842
Scott Wooddfd4d472011-11-17 12:39:59 +00001843 update_timer_ints(vcpu);
1844}
1845
Mihai Caramand02d4d12014-09-01 17:19:56 +03001846void kvmppc_decrementer_func(struct kvm_vcpu *vcpu)
Scott Wooddfd4d472011-11-17 12:39:59 +00001847{
Bharat Bhushan21bd0002012-05-20 23:21:23 +00001848 if (vcpu->arch.tcr & TCR_ARE) {
1849 vcpu->arch.dec = vcpu->arch.decar;
1850 kvmppc_emulate_dec(vcpu);
1851 }
1852
Scott Wooddfd4d472011-11-17 12:39:59 +00001853 kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1854}
1855
Bharat Bhushance11e482013-07-04 12:27:47 +05301856static int kvmppc_booke_add_breakpoint(struct debug_reg *dbg_reg,
1857 uint64_t addr, int index)
1858{
1859 switch (index) {
1860 case 0:
1861 dbg_reg->dbcr0 |= DBCR0_IAC1;
1862 dbg_reg->iac1 = addr;
1863 break;
1864 case 1:
1865 dbg_reg->dbcr0 |= DBCR0_IAC2;
1866 dbg_reg->iac2 = addr;
1867 break;
1868#if CONFIG_PPC_ADV_DEBUG_IACS > 2
1869 case 2:
1870 dbg_reg->dbcr0 |= DBCR0_IAC3;
1871 dbg_reg->iac3 = addr;
1872 break;
1873 case 3:
1874 dbg_reg->dbcr0 |= DBCR0_IAC4;
1875 dbg_reg->iac4 = addr;
1876 break;
1877#endif
1878 default:
1879 return -EINVAL;
1880 }
1881
1882 dbg_reg->dbcr0 |= DBCR0_IDM;
1883 return 0;
1884}
1885
1886static int kvmppc_booke_add_watchpoint(struct debug_reg *dbg_reg, uint64_t addr,
1887 int type, int index)
1888{
1889 switch (index) {
1890 case 0:
1891 if (type & KVMPPC_DEBUG_WATCH_READ)
1892 dbg_reg->dbcr0 |= DBCR0_DAC1R;
1893 if (type & KVMPPC_DEBUG_WATCH_WRITE)
1894 dbg_reg->dbcr0 |= DBCR0_DAC1W;
1895 dbg_reg->dac1 = addr;
1896 break;
1897 case 1:
1898 if (type & KVMPPC_DEBUG_WATCH_READ)
1899 dbg_reg->dbcr0 |= DBCR0_DAC2R;
1900 if (type & KVMPPC_DEBUG_WATCH_WRITE)
1901 dbg_reg->dbcr0 |= DBCR0_DAC2W;
1902 dbg_reg->dac2 = addr;
1903 break;
1904 default:
1905 return -EINVAL;
1906 }
1907
1908 dbg_reg->dbcr0 |= DBCR0_IDM;
1909 return 0;
1910}
1911void kvm_guest_protect_msr(struct kvm_vcpu *vcpu, ulong prot_bitmap, bool set)
1912{
1913 /* XXX: Add similar MSR protection for BookE-PR */
1914#ifdef CONFIG_KVM_BOOKE_HV
1915 BUG_ON(prot_bitmap & ~(MSRP_UCLEP | MSRP_DEP | MSRP_PMMP));
1916 if (set) {
1917 if (prot_bitmap & MSR_UCLE)
1918 vcpu->arch.shadow_msrp |= MSRP_UCLEP;
1919 if (prot_bitmap & MSR_DE)
1920 vcpu->arch.shadow_msrp |= MSRP_DEP;
1921 if (prot_bitmap & MSR_PMM)
1922 vcpu->arch.shadow_msrp |= MSRP_PMMP;
1923 } else {
1924 if (prot_bitmap & MSR_UCLE)
1925 vcpu->arch.shadow_msrp &= ~MSRP_UCLEP;
1926 if (prot_bitmap & MSR_DE)
1927 vcpu->arch.shadow_msrp &= ~MSRP_DEP;
1928 if (prot_bitmap & MSR_PMM)
1929 vcpu->arch.shadow_msrp &= ~MSRP_PMMP;
1930 }
1931#endif
1932}
1933
Alexander Graf7d15c062014-06-20 13:52:36 +02001934int kvmppc_xlate(struct kvm_vcpu *vcpu, ulong eaddr, enum xlate_instdata xlid,
1935 enum xlate_readwrite xlrw, struct kvmppc_pte *pte)
1936{
1937 int gtlb_index;
1938 gpa_t gpaddr;
1939
1940#ifdef CONFIG_KVM_E500V2
1941 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1942 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
1943 pte->eaddr = eaddr;
1944 pte->raddr = (vcpu->arch.magic_page_pa & PAGE_MASK) |
1945 (eaddr & ~PAGE_MASK);
1946 pte->vpage = eaddr >> PAGE_SHIFT;
1947 pte->may_read = true;
1948 pte->may_write = true;
1949 pte->may_execute = true;
1950
1951 return 0;
1952 }
1953#endif
1954
1955 /* Check the guest TLB. */
1956 switch (xlid) {
1957 case XLATE_INST:
1958 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
1959 break;
1960 case XLATE_DATA:
1961 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
1962 break;
1963 default:
1964 BUG();
1965 }
1966
1967 /* Do we have a TLB entry at all? */
1968 if (gtlb_index < 0)
1969 return -ENOENT;
1970
1971 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
1972
1973 pte->eaddr = eaddr;
1974 pte->raddr = (gpaddr & PAGE_MASK) | (eaddr & ~PAGE_MASK);
1975 pte->vpage = eaddr >> PAGE_SHIFT;
1976
1977 /* XXX read permissions from the guest TLB */
1978 pte->may_read = true;
1979 pte->may_write = true;
1980 pte->may_execute = true;
1981
1982 return 0;
1983}
1984
Bharat Bhushance11e482013-07-04 12:27:47 +05301985int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1986 struct kvm_guest_debug *dbg)
1987{
1988 struct debug_reg *dbg_reg;
1989 int n, b = 0, w = 0;
1990
1991 if (!(dbg->control & KVM_GUESTDBG_ENABLE)) {
Bharat Bhushan348ba712014-08-06 12:08:55 +05301992 vcpu->arch.dbg_reg.dbcr0 = 0;
Bharat Bhushance11e482013-07-04 12:27:47 +05301993 vcpu->guest_debug = 0;
1994 kvm_guest_protect_msr(vcpu, MSR_DE, false);
1995 return 0;
1996 }
1997
1998 kvm_guest_protect_msr(vcpu, MSR_DE, true);
1999 vcpu->guest_debug = dbg->control;
Bharat Bhushan348ba712014-08-06 12:08:55 +05302000 vcpu->arch.dbg_reg.dbcr0 = 0;
Bharat Bhushance11e482013-07-04 12:27:47 +05302001
2002 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
Bharat Bhushan348ba712014-08-06 12:08:55 +05302003 vcpu->arch.dbg_reg.dbcr0 |= DBCR0_IDM | DBCR0_IC;
Bharat Bhushance11e482013-07-04 12:27:47 +05302004
2005 /* Code below handles only HW breakpoints */
Bharat Bhushan348ba712014-08-06 12:08:55 +05302006 dbg_reg = &(vcpu->arch.dbg_reg);
Bharat Bhushance11e482013-07-04 12:27:47 +05302007
2008#ifdef CONFIG_KVM_BOOKE_HV
2009 /*
2010 * On BookE-HV (e500mc) the guest is always executed with MSR.GS=1
2011 * DBCR1 and DBCR2 are set to trigger debug events when MSR.PR is 0
2012 */
2013 dbg_reg->dbcr1 = 0;
2014 dbg_reg->dbcr2 = 0;
2015#else
2016 /*
2017 * On BookE-PR (e500v2) the guest is always executed with MSR.PR=1
2018 * We set DBCR1 and DBCR2 to only trigger debug events when MSR.PR
2019 * is set.
2020 */
2021 dbg_reg->dbcr1 = DBCR1_IAC1US | DBCR1_IAC2US | DBCR1_IAC3US |
2022 DBCR1_IAC4US;
2023 dbg_reg->dbcr2 = DBCR2_DAC1US | DBCR2_DAC2US;
2024#endif
2025
2026 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
2027 return 0;
2028
2029 for (n = 0; n < (KVMPPC_BOOKE_IAC_NUM + KVMPPC_BOOKE_DAC_NUM); n++) {
2030 uint64_t addr = dbg->arch.bp[n].addr;
2031 uint32_t type = dbg->arch.bp[n].type;
2032
2033 if (type == KVMPPC_DEBUG_NONE)
2034 continue;
2035
2036 if (type & !(KVMPPC_DEBUG_WATCH_READ |
2037 KVMPPC_DEBUG_WATCH_WRITE |
2038 KVMPPC_DEBUG_BREAKPOINT))
2039 return -EINVAL;
2040
2041 if (type & KVMPPC_DEBUG_BREAKPOINT) {
2042 /* Setting H/W breakpoint */
2043 if (kvmppc_booke_add_breakpoint(dbg_reg, addr, b++))
2044 return -EINVAL;
2045 } else {
2046 /* Setting H/W watchpoint */
2047 if (kvmppc_booke_add_watchpoint(dbg_reg, addr,
2048 type, w++))
2049 return -EINVAL;
2050 }
2051 }
2052
2053 return 0;
2054}
2055
Scott Wood94fa9d92011-12-20 15:34:22 +00002056void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2057{
Paul Mackerrasa47d72f2012-09-20 19:35:51 +00002058 vcpu->cpu = smp_processor_id();
Scott Woodd30f6e42011-12-20 15:34:43 +00002059 current->thread.kvm_vcpu = vcpu;
Scott Wood94fa9d92011-12-20 15:34:22 +00002060}
2061
2062void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
2063{
Scott Woodd30f6e42011-12-20 15:34:43 +00002064 current->thread.kvm_vcpu = NULL;
Paul Mackerrasa47d72f2012-09-20 19:35:51 +00002065 vcpu->cpu = -1;
Bharat Bhushance11e482013-07-04 12:27:47 +05302066
2067 /* Clear pending debug event in DBSR */
2068 kvmppc_clear_dbsr();
Scott Wood94fa9d92011-12-20 15:34:22 +00002069}
2070
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302071void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
2072{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302073 vcpu->kvm->arch.kvm_ops->mmu_destroy(vcpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302074}
2075
2076int kvmppc_core_init_vm(struct kvm *kvm)
2077{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302078 return kvm->arch.kvm_ops->init_vm(kvm);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302079}
2080
2081struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
2082{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302083 return kvm->arch.kvm_ops->vcpu_create(kvm, id);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302084}
2085
2086void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
2087{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302088 vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302089}
2090
2091void kvmppc_core_destroy_vm(struct kvm *kvm)
2092{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302093 kvm->arch.kvm_ops->destroy_vm(kvm);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302094}
2095
2096void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2097{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302098 vcpu->kvm->arch.kvm_ops->vcpu_load(vcpu, cpu);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302099}
2100
2101void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
2102{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302103 vcpu->kvm->arch.kvm_ops->vcpu_put(vcpu);
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002104}
2105
2106int __init kvmppc_booke_init(void)
2107{
Scott Woodd30f6e42011-12-20 15:34:43 +00002108#ifndef CONFIG_KVM_BOOKE_HV
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002109 unsigned long ivor[16];
Bharat Bhushan1d542d92013-01-15 22:24:39 +00002110 unsigned long *handler = kvmppc_booke_handler_addr;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002111 unsigned long max_ivor = 0;
Bharat Bhushan1d542d92013-01-15 22:24:39 +00002112 unsigned long handler_len;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002113 int i;
2114
2115 /* We install our own exception handlers by hijacking IVPR. IVPR must
2116 * be 16-bit aligned, so we need a 64KB allocation. */
2117 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
2118 VCPU_SIZE_ORDER);
2119 if (!kvmppc_booke_handlers)
2120 return -ENOMEM;
2121
2122 /* XXX make sure our handlers are smaller than Linux's */
2123
2124 /* Copy our interrupt handlers to match host IVORs. That way we don't
2125 * have to swap the IVORs on every guest/host transition. */
2126 ivor[0] = mfspr(SPRN_IVOR0);
2127 ivor[1] = mfspr(SPRN_IVOR1);
2128 ivor[2] = mfspr(SPRN_IVOR2);
2129 ivor[3] = mfspr(SPRN_IVOR3);
2130 ivor[4] = mfspr(SPRN_IVOR4);
2131 ivor[5] = mfspr(SPRN_IVOR5);
2132 ivor[6] = mfspr(SPRN_IVOR6);
2133 ivor[7] = mfspr(SPRN_IVOR7);
2134 ivor[8] = mfspr(SPRN_IVOR8);
2135 ivor[9] = mfspr(SPRN_IVOR9);
2136 ivor[10] = mfspr(SPRN_IVOR10);
2137 ivor[11] = mfspr(SPRN_IVOR11);
2138 ivor[12] = mfspr(SPRN_IVOR12);
2139 ivor[13] = mfspr(SPRN_IVOR13);
2140 ivor[14] = mfspr(SPRN_IVOR14);
2141 ivor[15] = mfspr(SPRN_IVOR15);
2142
2143 for (i = 0; i < 16; i++) {
2144 if (ivor[i] > max_ivor)
Bharat Bhushan1d542d92013-01-15 22:24:39 +00002145 max_ivor = i;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002146
Bharat Bhushan1d542d92013-01-15 22:24:39 +00002147 handler_len = handler[i + 1] - handler[i];
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002148 memcpy((void *)kvmppc_booke_handlers + ivor[i],
Bharat Bhushan1d542d92013-01-15 22:24:39 +00002149 (void *)handler[i], handler_len);
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002150 }
Bharat Bhushan1d542d92013-01-15 22:24:39 +00002151
2152 handler_len = handler[max_ivor + 1] - handler[max_ivor];
2153 flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers +
2154 ivor[max_ivor] + handler_len);
Scott Woodd30f6e42011-12-20 15:34:43 +00002155#endif /* !BOOKE_HV */
Hollis Blancharddb93f572008-11-05 09:36:18 -06002156 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002157}
2158
Hollis Blancharddb93f572008-11-05 09:36:18 -06002159void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06002160{
2161 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
2162 kvm_exit();
2163}