blob: 69d047c22d20b2defdb19ed52003a1387e5abe9f [file] [log] [blame]
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 *
15 * Copyright IBM Corp. 2007
Scott Wood4cd35f62011-06-14 18:34:31 -050016 * Copyright 2010-2011 Freescale Semiconductor, Inc.
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050017 *
18 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
19 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
Scott Woodd30f6e42011-12-20 15:34:43 +000020 * Scott Wood <scottwood@freescale.com>
21 * Varun Sethi <varun.sethi@freescale.com>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050022 */
23
24#include <linux/errno.h>
25#include <linux/err.h>
26#include <linux/kvm_host.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/gfp.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050028#include <linux/module.h>
29#include <linux/vmalloc.h>
30#include <linux/fs.h>
Hollis Blanchard7924bd42008-12-02 15:51:55 -060031
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050032#include <asm/cputable.h>
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>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050039
Scott Woodd30f6e42011-12-20 15:34:43 +000040#include "timing.h"
Hollis Blanchard75f74f02008-11-05 09:36:16 -060041#include "booke.h"
Alexander Graf97c95052012-08-02 15:10:00 +020042#include "trace.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050043
Hollis Blanchardd9fbd032008-11-05 09:36:13 -060044unsigned long kvmppc_booke_handlers;
45
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050046#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
47#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
48
49struct kvm_stats_debugfs_item debugfs_entries[] = {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050050 { "mmio", VCPU_STAT(mmio_exits) },
51 { "dcr", VCPU_STAT(dcr_exits) },
52 { "sig", VCPU_STAT(signal_exits) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050053 { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
54 { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
55 { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
56 { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
57 { "sysc", VCPU_STAT(syscall_exits) },
58 { "isi", VCPU_STAT(isi_exits) },
59 { "dsi", VCPU_STAT(dsi_exits) },
60 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
61 { "dec", VCPU_STAT(dec_exits) },
62 { "ext_intr", VCPU_STAT(ext_intr_exits) },
Hollis Blanchard45c5eb62008-04-25 17:55:49 -050063 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Scott Woodd30f6e42011-12-20 15:34:43 +000064 { "doorbell", VCPU_STAT(dbell_exits) },
65 { "guest doorbell", VCPU_STAT(gdbell_exits) },
Alexander Grafcf1c5ca2012-08-01 12:56:51 +020066 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050067 { NULL }
68};
69
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050070/* TODO: use vcpu_printf() */
71void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
72{
73 int i;
74
Alexander Graf666e7252010-07-29 14:47:43 +020075 printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060076 printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
Alexander Grafde7906c2010-07-29 14:47:46 +020077 printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
78 vcpu->arch.shared->srr1);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050079
80 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
81
82 for (i = 0; i < 32; i += 4) {
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060083 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
Alexander Graf8e5b26b2010-01-08 02:58:01 +010084 kvmppc_get_gpr(vcpu, i),
85 kvmppc_get_gpr(vcpu, i+1),
86 kvmppc_get_gpr(vcpu, i+2),
87 kvmppc_get_gpr(vcpu, i+3));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050088 }
89}
90
Scott Wood4cd35f62011-06-14 18:34:31 -050091#ifdef CONFIG_SPE
92void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
93{
94 preempt_disable();
95 enable_kernel_spe();
96 kvmppc_save_guest_spe(vcpu);
97 vcpu->arch.shadow_msr &= ~MSR_SPE;
98 preempt_enable();
99}
100
101static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
102{
103 preempt_disable();
104 enable_kernel_spe();
105 kvmppc_load_guest_spe(vcpu);
106 vcpu->arch.shadow_msr |= MSR_SPE;
107 preempt_enable();
108}
109
110static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
111{
112 if (vcpu->arch.shared->msr & MSR_SPE) {
113 if (!(vcpu->arch.shadow_msr & MSR_SPE))
114 kvmppc_vcpu_enable_spe(vcpu);
115 } else if (vcpu->arch.shadow_msr & MSR_SPE) {
116 kvmppc_vcpu_disable_spe(vcpu);
117 }
118}
119#else
120static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
121{
122}
123#endif
124
Alexander Graf7a08c272012-08-16 13:10:16 +0200125static void kvmppc_vcpu_sync_fpu(struct kvm_vcpu *vcpu)
126{
127#if defined(CONFIG_PPC_FPU) && !defined(CONFIG_KVM_BOOKE_HV)
128 /* We always treat the FP bit as enabled from the host
129 perspective, so only need to adjust the shadow MSR */
130 vcpu->arch.shadow_msr &= ~MSR_FP;
131 vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_FP;
132#endif
133}
134
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500135/*
136 * Helper function for "full" MSR writes. No need to call this if only
137 * EE/CE/ME/DE/RI are changing.
138 */
Scott Wood4cd35f62011-06-14 18:34:31 -0500139void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
140{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500141 u32 old_msr = vcpu->arch.shared->msr;
Scott Wood4cd35f62011-06-14 18:34:31 -0500142
Scott Woodd30f6e42011-12-20 15:34:43 +0000143#ifdef CONFIG_KVM_BOOKE_HV
144 new_msr |= MSR_GS;
145#endif
146
Scott Wood4cd35f62011-06-14 18:34:31 -0500147 vcpu->arch.shared->msr = new_msr;
148
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500149 kvmppc_mmu_msr_notify(vcpu, old_msr);
Scott Wood4cd35f62011-06-14 18:34:31 -0500150 kvmppc_vcpu_sync_spe(vcpu);
Alexander Graf7a08c272012-08-16 13:10:16 +0200151 kvmppc_vcpu_sync_fpu(vcpu);
Scott Wood4cd35f62011-06-14 18:34:31 -0500152}
153
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600154static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
155 unsigned int priority)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600156{
Alexander Graf63460462012-08-08 00:44:52 +0200157 trace_kvm_booke_queue_irqprio(vcpu, priority);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600158 set_bit(priority, &vcpu->arch.pending_exceptions);
159}
160
Liu Yudaf5e272010-02-02 19:44:35 +0800161static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
162 ulong dear_flags, ulong esr_flags)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600163{
Liu Yudaf5e272010-02-02 19:44:35 +0800164 vcpu->arch.queued_dear = dear_flags;
165 vcpu->arch.queued_esr = esr_flags;
166 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
167}
168
169static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
170 ulong dear_flags, ulong esr_flags)
171{
172 vcpu->arch.queued_dear = dear_flags;
173 vcpu->arch.queued_esr = esr_flags;
174 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
175}
176
177static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
178 ulong esr_flags)
179{
180 vcpu->arch.queued_esr = esr_flags;
181 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
182}
183
184void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
185{
186 vcpu->arch.queued_esr = esr_flags;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600187 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600188}
189
190void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
191{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600192 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600193}
194
195int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
196{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600197 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600198}
199
Alexander Graf7706664d2009-12-21 20:21:24 +0100200void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
201{
202 clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
203}
204
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600205void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
206 struct kvm_interrupt *irq)
207{
Alexander Grafc5335f12010-08-30 14:03:24 +0200208 unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
209
210 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
211 prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
212
213 kvmppc_booke_queue_irqprio(vcpu, prio);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600214}
215
Alexander Graf4496f972010-04-07 10:03:25 +0200216void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
217 struct kvm_interrupt *irq)
218{
219 clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
Alexander Grafc5335f12010-08-30 14:03:24 +0200220 clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
Alexander Graf4496f972010-04-07 10:03:25 +0200221}
222
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000223static void kvmppc_core_queue_watchdog(struct kvm_vcpu *vcpu)
224{
225 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_WATCHDOG);
226}
227
228static void kvmppc_core_dequeue_watchdog(struct kvm_vcpu *vcpu)
229{
230 clear_bit(BOOKE_IRQPRIO_WATCHDOG, &vcpu->arch.pending_exceptions);
231}
232
Scott Woodd30f6e42011-12-20 15:34:43 +0000233static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
234{
235#ifdef CONFIG_KVM_BOOKE_HV
236 mtspr(SPRN_GSRR0, srr0);
237 mtspr(SPRN_GSRR1, srr1);
238#else
239 vcpu->arch.shared->srr0 = srr0;
240 vcpu->arch.shared->srr1 = srr1;
241#endif
242}
243
244static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
245{
246 vcpu->arch.csrr0 = srr0;
247 vcpu->arch.csrr1 = srr1;
248}
249
250static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
251{
252 if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
253 vcpu->arch.dsrr0 = srr0;
254 vcpu->arch.dsrr1 = srr1;
255 } else {
256 set_guest_csrr(vcpu, srr0, srr1);
257 }
258}
259
260static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
261{
262 vcpu->arch.mcsrr0 = srr0;
263 vcpu->arch.mcsrr1 = srr1;
264}
265
266static unsigned long get_guest_dear(struct kvm_vcpu *vcpu)
267{
268#ifdef CONFIG_KVM_BOOKE_HV
269 return mfspr(SPRN_GDEAR);
270#else
271 return vcpu->arch.shared->dar;
272#endif
273}
274
275static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear)
276{
277#ifdef CONFIG_KVM_BOOKE_HV
278 mtspr(SPRN_GDEAR, dear);
279#else
280 vcpu->arch.shared->dar = dear;
281#endif
282}
283
284static unsigned long get_guest_esr(struct kvm_vcpu *vcpu)
285{
286#ifdef CONFIG_KVM_BOOKE_HV
287 return mfspr(SPRN_GESR);
288#else
289 return vcpu->arch.shared->esr;
290#endif
291}
292
293static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr)
294{
295#ifdef CONFIG_KVM_BOOKE_HV
296 mtspr(SPRN_GESR, esr);
297#else
298 vcpu->arch.shared->esr = esr;
299#endif
300}
301
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600302/* Deliver the interrupt of the corresponding priority, if possible. */
303static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
304 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500305{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600306 int allowed = 0;
Alexander Graf79300f82012-02-15 19:12:29 +0000307 ulong msr_mask = 0;
Liu Yudaf5e272010-02-02 19:44:35 +0800308 bool update_esr = false, update_dear = false;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200309 ulong crit_raw = vcpu->arch.shared->critical;
310 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
311 bool crit;
Alexander Grafc5335f12010-08-30 14:03:24 +0200312 bool keep_irq = false;
Scott Woodd30f6e42011-12-20 15:34:43 +0000313 enum int_class int_class;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200314
315 /* Truncate crit indicators in 32 bit mode */
316 if (!(vcpu->arch.shared->msr & MSR_SF)) {
317 crit_raw &= 0xffffffff;
318 crit_r1 &= 0xffffffff;
319 }
320
321 /* Critical section when crit == r1 */
322 crit = (crit_raw == crit_r1);
323 /* ... and we're in supervisor mode */
324 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500325
Alexander Grafc5335f12010-08-30 14:03:24 +0200326 if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
327 priority = BOOKE_IRQPRIO_EXTERNAL;
328 keep_irq = true;
329 }
330
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600331 switch (priority) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600332 case BOOKE_IRQPRIO_DTLB_MISS:
Liu Yudaf5e272010-02-02 19:44:35 +0800333 case BOOKE_IRQPRIO_DATA_STORAGE:
334 update_dear = true;
335 /* fall through */
336 case BOOKE_IRQPRIO_INST_STORAGE:
337 case BOOKE_IRQPRIO_PROGRAM:
338 update_esr = true;
339 /* fall through */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600340 case BOOKE_IRQPRIO_ITLB_MISS:
341 case BOOKE_IRQPRIO_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600342 case BOOKE_IRQPRIO_FP_UNAVAIL:
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600343 case BOOKE_IRQPRIO_SPE_UNAVAIL:
344 case BOOKE_IRQPRIO_SPE_FP_DATA:
345 case BOOKE_IRQPRIO_SPE_FP_ROUND:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600346 case BOOKE_IRQPRIO_AP_UNAVAIL:
347 case BOOKE_IRQPRIO_ALIGNMENT:
348 allowed = 1;
Alexander Graf79300f82012-02-15 19:12:29 +0000349 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000350 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500351 break;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000352 case BOOKE_IRQPRIO_WATCHDOG:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600353 case BOOKE_IRQPRIO_CRITICAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000354 case BOOKE_IRQPRIO_DBELL_CRIT:
Alexander Graf666e7252010-07-29 14:47:43 +0200355 allowed = vcpu->arch.shared->msr & MSR_CE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000356 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000357 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000358 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500359 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600360 case BOOKE_IRQPRIO_MACHINE_CHECK:
Alexander Graf666e7252010-07-29 14:47:43 +0200361 allowed = vcpu->arch.shared->msr & MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000362 allowed = allowed && !crit;
Scott Woodd30f6e42011-12-20 15:34:43 +0000363 int_class = INT_CLASS_MC;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500364 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600365 case BOOKE_IRQPRIO_DECREMENTER:
366 case BOOKE_IRQPRIO_FIT:
Scott Wooddfd4d472011-11-17 12:39:59 +0000367 keep_irq = true;
368 /* fall through */
369 case BOOKE_IRQPRIO_EXTERNAL:
Alexander Graf4ab96912012-02-15 13:28:48 +0000370 case BOOKE_IRQPRIO_DBELL:
Alexander Graf666e7252010-07-29 14:47:43 +0200371 allowed = vcpu->arch.shared->msr & MSR_EE;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200372 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000373 msr_mask = MSR_CE | MSR_ME | MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000374 int_class = INT_CLASS_NONCRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500375 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600376 case BOOKE_IRQPRIO_DEBUG:
Alexander Graf666e7252010-07-29 14:47:43 +0200377 allowed = vcpu->arch.shared->msr & MSR_DE;
Scott Woodd30f6e42011-12-20 15:34:43 +0000378 allowed = allowed && !crit;
Alexander Graf79300f82012-02-15 19:12:29 +0000379 msr_mask = MSR_ME;
Scott Woodd30f6e42011-12-20 15:34:43 +0000380 int_class = INT_CLASS_CRIT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500381 break;
382 }
383
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600384 if (allowed) {
Scott Woodd30f6e42011-12-20 15:34:43 +0000385 switch (int_class) {
386 case INT_CLASS_NONCRIT:
387 set_guest_srr(vcpu, vcpu->arch.pc,
388 vcpu->arch.shared->msr);
389 break;
390 case INT_CLASS_CRIT:
391 set_guest_csrr(vcpu, vcpu->arch.pc,
392 vcpu->arch.shared->msr);
393 break;
394 case INT_CLASS_DBG:
395 set_guest_dsrr(vcpu, vcpu->arch.pc,
396 vcpu->arch.shared->msr);
397 break;
398 case INT_CLASS_MC:
399 set_guest_mcsrr(vcpu, vcpu->arch.pc,
400 vcpu->arch.shared->msr);
401 break;
402 }
403
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600404 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
Liu Yudaf5e272010-02-02 19:44:35 +0800405 if (update_esr == true)
Scott Woodd30f6e42011-12-20 15:34:43 +0000406 set_guest_esr(vcpu, vcpu->arch.queued_esr);
Liu Yudaf5e272010-02-02 19:44:35 +0800407 if (update_dear == true)
Scott Woodd30f6e42011-12-20 15:34:43 +0000408 set_guest_dear(vcpu, vcpu->arch.queued_dear);
Alexander Graf666e7252010-07-29 14:47:43 +0200409 kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600410
Alexander Grafc5335f12010-08-30 14:03:24 +0200411 if (!keep_irq)
412 clear_bit(priority, &vcpu->arch.pending_exceptions);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600413 }
414
Scott Woodd30f6e42011-12-20 15:34:43 +0000415#ifdef CONFIG_KVM_BOOKE_HV
416 /*
417 * If an interrupt is pending but masked, raise a guest doorbell
418 * so that we are notified when the guest enables the relevant
419 * MSR bit.
420 */
421 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
422 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
423 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
424 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
425 if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
426 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
427#endif
428
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600429 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500430}
431
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000432/*
433 * Return the number of jiffies until the next timeout. If the timeout is
434 * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA
435 * because the larger value can break the timer APIs.
436 */
437static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu)
438{
439 u64 tb, wdt_tb, wdt_ticks = 0;
440 u64 nr_jiffies = 0;
441 u32 period = TCR_GET_WP(vcpu->arch.tcr);
442
443 wdt_tb = 1ULL << (63 - period);
444 tb = get_tb();
445 /*
446 * The watchdog timeout will hapeen when TB bit corresponding
447 * to watchdog will toggle from 0 to 1.
448 */
449 if (tb & wdt_tb)
450 wdt_ticks = wdt_tb;
451
452 wdt_ticks += wdt_tb - (tb & (wdt_tb - 1));
453
454 /* Convert timebase ticks to jiffies */
455 nr_jiffies = wdt_ticks;
456
457 if (do_div(nr_jiffies, tb_ticks_per_jiffy))
458 nr_jiffies++;
459
460 return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA);
461}
462
463static void arm_next_watchdog(struct kvm_vcpu *vcpu)
464{
465 unsigned long nr_jiffies;
466 unsigned long flags;
467
468 /*
469 * If TSR_ENW and TSR_WIS are not set then no need to exit to
470 * userspace, so clear the KVM_REQ_WATCHDOG request.
471 */
472 if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS))
473 clear_bit(KVM_REQ_WATCHDOG, &vcpu->requests);
474
475 spin_lock_irqsave(&vcpu->arch.wdt_lock, flags);
476 nr_jiffies = watchdog_next_timeout(vcpu);
477 /*
478 * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA
479 * then do not run the watchdog timer as this can break timer APIs.
480 */
481 if (nr_jiffies < NEXT_TIMER_MAX_DELTA)
482 mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies);
483 else
484 del_timer(&vcpu->arch.wdt_timer);
485 spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags);
486}
487
488void kvmppc_watchdog_func(unsigned long data)
489{
490 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
491 u32 tsr, new_tsr;
492 int final;
493
494 do {
495 new_tsr = tsr = vcpu->arch.tsr;
496 final = 0;
497
498 /* Time out event */
499 if (tsr & TSR_ENW) {
500 if (tsr & TSR_WIS)
501 final = 1;
502 else
503 new_tsr = tsr | TSR_WIS;
504 } else {
505 new_tsr = tsr | TSR_ENW;
506 }
507 } while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr);
508
509 if (new_tsr & TSR_WIS) {
510 smp_wmb();
511 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
512 kvm_vcpu_kick(vcpu);
513 }
514
515 /*
516 * If this is final watchdog expiry and some action is required
517 * then exit to userspace.
518 */
519 if (final && (vcpu->arch.tcr & TCR_WRC_MASK) &&
520 vcpu->arch.watchdog_enabled) {
521 smp_wmb();
522 kvm_make_request(KVM_REQ_WATCHDOG, vcpu);
523 kvm_vcpu_kick(vcpu);
524 }
525
526 /*
527 * Stop running the watchdog timer after final expiration to
528 * prevent the host from being flooded with timers if the
529 * guest sets a short period.
530 * Timers will resume when TSR/TCR is updated next time.
531 */
532 if (!final)
533 arm_next_watchdog(vcpu);
534}
535
Scott Wooddfd4d472011-11-17 12:39:59 +0000536static void update_timer_ints(struct kvm_vcpu *vcpu)
537{
538 if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
539 kvmppc_core_queue_dec(vcpu);
540 else
541 kvmppc_core_dequeue_dec(vcpu);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000542
543 if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS))
544 kvmppc_core_queue_watchdog(vcpu);
545 else
546 kvmppc_core_dequeue_watchdog(vcpu);
Scott Wooddfd4d472011-11-17 12:39:59 +0000547}
548
Scott Woodc59a6a32011-11-08 18:23:25 -0600549static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500550{
551 unsigned long *pending = &vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500552 unsigned int priority;
553
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600554 priority = __ffs(*pending);
Alexander Graf8b3a00f2012-02-16 14:12:46 +0000555 while (priority < BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600556 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500557 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500558
559 priority = find_next_bit(pending,
560 BITS_PER_BYTE * sizeof(*pending),
561 priority + 1);
562 }
Alexander Graf90bba352010-07-29 14:47:51 +0200563
564 /* Tell the guest about our interrupt status */
Scott Wood29ac26e2011-11-08 18:23:27 -0600565 vcpu->arch.shared->int_pending = !!*pending;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500566}
567
Scott Woodc59a6a32011-11-08 18:23:25 -0600568/* Check pending exceptions and deliver one, if possible. */
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000569int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
Scott Woodc59a6a32011-11-08 18:23:25 -0600570{
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000571 int r = 0;
Scott Woodc59a6a32011-11-08 18:23:25 -0600572 WARN_ON_ONCE(!irqs_disabled());
573
574 kvmppc_core_check_exceptions(vcpu);
575
576 if (vcpu->arch.shared->msr & MSR_WE) {
577 local_irq_enable();
578 kvm_vcpu_block(vcpu);
Alexander Graf966cd0f2012-03-14 16:55:08 +0100579 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
Scott Woodc59a6a32011-11-08 18:23:25 -0600580 local_irq_disable();
581
582 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000583 r = 1;
Scott Woodc59a6a32011-11-08 18:23:25 -0600584 };
Alexander Grafa8e4ef82012-02-16 14:07:37 +0000585
586 return r;
587}
588
Alexander Graf7c973a22012-08-13 12:50:35 +0200589int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
Alexander Graf4ffc6352012-08-08 20:31:13 +0200590{
Alexander Graf7c973a22012-08-13 12:50:35 +0200591 int r = 1; /* Indicate we want to get back into the guest */
592
Alexander Graf2d8185d2012-08-10 12:31:12 +0200593 if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
594 update_timer_ints(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200595#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Alexander Graf2d8185d2012-08-10 12:31:12 +0200596 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
597 kvmppc_core_flush_tlb(vcpu);
Alexander Graf862d31f2012-07-31 00:19:50 +0200598#endif
Alexander Graf7c973a22012-08-13 12:50:35 +0200599
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000600 if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) {
601 vcpu->run->exit_reason = KVM_EXIT_WATCHDOG;
602 r = 0;
603 }
604
Alexander Graf7c973a22012-08-13 12:50:35 +0200605 return r;
Alexander Graf4ffc6352012-08-08 20:31:13 +0200606}
607
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000608int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
609{
Alexander Graf7ee78852012-08-13 12:44:41 +0200610 int ret, s;
Scott Wood8fae8452011-12-20 15:34:45 +0000611#ifdef CONFIG_PPC_FPU
612 unsigned int fpscr;
613 int fpexc_mode;
614 u64 fpr[32];
615#endif
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000616
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200617 if (!vcpu->arch.sane) {
618 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
619 return -EINVAL;
620 }
621
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000622 local_irq_disable();
Alexander Graf7ee78852012-08-13 12:44:41 +0200623 s = kvmppc_prepare_to_enter(vcpu);
624 if (s <= 0) {
Alexander Graf24afa372012-08-12 12:42:30 +0200625 local_irq_enable();
Alexander Graf7ee78852012-08-13 12:44:41 +0200626 ret = s;
Scott Wood1d1ef222011-11-08 16:11:59 -0600627 goto out;
628 }
Alexander Grafbd2be682012-08-13 01:04:19 +0200629 kvmppc_lazy_ee_enable();
Scott Wood1d1ef222011-11-08 16:11:59 -0600630
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000631 kvm_guest_enter();
Scott Wood8fae8452011-12-20 15:34:45 +0000632
633#ifdef CONFIG_PPC_FPU
634 /* Save userspace FPU state in stack */
635 enable_kernel_fp();
636 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
637 fpscr = current->thread.fpscr.val;
638 fpexc_mode = current->thread.fpexc_mode;
639
640 /* Restore guest FPU state to thread */
641 memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr));
642 current->thread.fpscr.val = vcpu->arch.fpscr;
643
644 /*
645 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
646 * as always using the FPU. Kernel usage of FP (via
647 * enable_kernel_fp()) in this thread must not occur while
648 * vcpu->fpu_active is set.
649 */
650 vcpu->fpu_active = 1;
651
652 kvmppc_load_guest_fp(vcpu);
653#endif
654
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000655 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
Scott Wood8fae8452011-12-20 15:34:45 +0000656
Alexander Graf24afa372012-08-12 12:42:30 +0200657 /* No need for kvm_guest_exit. It's done in handle_exit.
658 We also get here with interrupts enabled. */
659
Scott Wood8fae8452011-12-20 15:34:45 +0000660#ifdef CONFIG_PPC_FPU
661 kvmppc_save_guest_fp(vcpu);
662
663 vcpu->fpu_active = 0;
664
665 /* Save guest FPU state from thread */
666 memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr));
667 vcpu->arch.fpscr = current->thread.fpscr.val;
668
669 /* Restore userspace FPU state from stack */
670 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
671 current->thread.fpscr.val = fpscr;
672 current->thread.fpexc_mode = fpexc_mode;
673#endif
674
Scott Wood1d1ef222011-11-08 16:11:59 -0600675out:
Alexander Grafd69c6432012-08-08 20:44:20 +0200676 vcpu->mode = OUTSIDE_GUEST_MODE;
677 smp_wmb();
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000678 return ret;
679}
680
Scott Woodd30f6e42011-12-20 15:34:43 +0000681static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
682{
683 enum emulation_result er;
684
685 er = kvmppc_emulate_instruction(run, vcpu);
686 switch (er) {
687 case EMULATE_DONE:
688 /* don't overwrite subtypes, just account kvm_stats */
689 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
690 /* Future optimization: only reload non-volatiles if
691 * they were actually modified by emulation. */
692 return RESUME_GUEST_NV;
693
694 case EMULATE_DO_DCR:
695 run->exit_reason = KVM_EXIT_DCR;
696 return RESUME_HOST;
697
698 case EMULATE_FAIL:
Scott Woodd30f6e42011-12-20 15:34:43 +0000699 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
700 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
701 /* For debugging, encode the failing instruction and
702 * report it to userspace. */
703 run->hw.hardware_exit_reason = ~0ULL << 32;
704 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
Alexander Grafd1ff5492012-02-16 13:24:03 +0000705 kvmppc_core_queue_program(vcpu, ESR_PIL);
Scott Woodd30f6e42011-12-20 15:34:43 +0000706 return RESUME_HOST;
707
708 default:
709 BUG();
710 }
711}
712
Alexander Graf4e642cc2012-02-20 23:57:26 +0100713static void kvmppc_fill_pt_regs(struct pt_regs *regs)
714{
715 ulong r1, ip, msr, lr;
716
717 asm("mr %0, 1" : "=r"(r1));
718 asm("mflr %0" : "=r"(lr));
719 asm("mfmsr %0" : "=r"(msr));
720 asm("bl 1f; 1: mflr %0" : "=r"(ip));
721
722 memset(regs, 0, sizeof(*regs));
723 regs->gpr[1] = r1;
724 regs->nip = ip;
725 regs->msr = msr;
726 regs->link = lr;
727}
728
Bharat Bhushan6328e592012-06-20 05:56:53 +0000729/*
730 * For interrupts needed to be handled by host interrupt handlers,
731 * corresponding host handler are called from here in similar way
732 * (but not exact) as they are called from low level handler
733 * (such as from arch/powerpc/kernel/head_fsl_booke.S).
734 */
Alexander Graf4e642cc2012-02-20 23:57:26 +0100735static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
736 unsigned int exit_nr)
737{
738 struct pt_regs regs;
739
740 switch (exit_nr) {
741 case BOOKE_INTERRUPT_EXTERNAL:
742 kvmppc_fill_pt_regs(&regs);
743 do_IRQ(&regs);
744 break;
745 case BOOKE_INTERRUPT_DECREMENTER:
746 kvmppc_fill_pt_regs(&regs);
747 timer_interrupt(&regs);
748 break;
749#if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64)
750 case BOOKE_INTERRUPT_DOORBELL:
751 kvmppc_fill_pt_regs(&regs);
752 doorbell_exception(&regs);
753 break;
754#endif
755 case BOOKE_INTERRUPT_MACHINE_CHECK:
756 /* FIXME */
757 break;
Alexander Graf7cc1e8e2012-02-22 16:26:34 +0100758 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
759 kvmppc_fill_pt_regs(&regs);
760 performance_monitor_exception(&regs);
761 break;
Bharat Bhushan6328e592012-06-20 05:56:53 +0000762 case BOOKE_INTERRUPT_WATCHDOG:
763 kvmppc_fill_pt_regs(&regs);
764#ifdef CONFIG_BOOKE_WDT
765 WatchdogException(&regs);
766#else
767 unknown_exception(&regs);
768#endif
769 break;
770 case BOOKE_INTERRUPT_CRITICAL:
771 unknown_exception(&regs);
772 break;
Alexander Graf4e642cc2012-02-20 23:57:26 +0100773 }
774}
775
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500776/**
777 * kvmppc_handle_exit
778 *
779 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
780 */
781int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
782 unsigned int exit_nr)
783{
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500784 int r = RESUME_HOST;
Alexander Graf7ee78852012-08-13 12:44:41 +0200785 int s;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500786
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600787 /* update before a new last_exit_type is rewritten */
788 kvmppc_update_timing_stats(vcpu);
789
Alexander Graf4e642cc2012-02-20 23:57:26 +0100790 /* restart interrupts if they were meant for the host */
791 kvmppc_restart_interrupt(vcpu, exit_nr);
Scott Woodd30f6e42011-12-20 15:34:43 +0000792
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500793 local_irq_enable();
794
Alexander Graf97c95052012-08-02 15:10:00 +0200795 trace_kvm_exit(exit_nr, vcpu);
Alexander Graf706fb732012-08-12 11:29:09 +0200796 kvm_guest_exit();
Alexander Graf97c95052012-08-02 15:10:00 +0200797
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500798 run->exit_reason = KVM_EXIT_UNKNOWN;
799 run->ready_for_interrupt_injection = 1;
800
801 switch (exit_nr) {
802 case BOOKE_INTERRUPT_MACHINE_CHECK:
Alexander Grafc35c9d82012-02-20 12:21:18 +0100803 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
804 kvmppc_dump_vcpu(vcpu);
805 /* For debugging, send invalid exit reason to user space */
806 run->hw.hardware_exit_reason = ~1ULL << 32;
807 run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
808 r = RESUME_HOST;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500809 break;
810
811 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600812 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -0600813 r = RESUME_GUEST;
814 break;
815
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500816 case BOOKE_INTERRUPT_DECREMENTER:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600817 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500818 r = RESUME_GUEST;
819 break;
820
Bharat Bhushan6328e592012-06-20 05:56:53 +0000821 case BOOKE_INTERRUPT_WATCHDOG:
822 r = RESUME_GUEST;
823 break;
824
Scott Woodd30f6e42011-12-20 15:34:43 +0000825 case BOOKE_INTERRUPT_DOORBELL:
826 kvmppc_account_exit(vcpu, DBELL_EXITS);
Scott Woodd30f6e42011-12-20 15:34:43 +0000827 r = RESUME_GUEST;
828 break;
829
830 case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
831 kvmppc_account_exit(vcpu, GDBELL_EXITS);
832
833 /*
834 * We are here because there is a pending guest interrupt
835 * which could not be delivered as MSR_CE or MSR_ME was not
836 * set. Once we break from here we will retry delivery.
837 */
838 r = RESUME_GUEST;
839 break;
840
841 case BOOKE_INTERRUPT_GUEST_DBELL:
842 kvmppc_account_exit(vcpu, GDBELL_EXITS);
843
844 /*
845 * We are here because there is a pending guest interrupt
846 * which could not be delivered as MSR_EE was not set. Once
847 * we break from here we will retry delivery.
848 */
849 r = RESUME_GUEST;
850 break;
851
Alexander Graf95f2e922012-02-20 22:45:12 +0100852 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
853 r = RESUME_GUEST;
854 break;
855
Scott Woodd30f6e42011-12-20 15:34:43 +0000856 case BOOKE_INTERRUPT_HV_PRIV:
857 r = emulation_exit(run, vcpu);
858 break;
859
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500860 case BOOKE_INTERRUPT_PROGRAM:
Scott Woodd30f6e42011-12-20 15:34:43 +0000861 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
Alexander Graf02685972012-02-20 12:33:22 +0100862 /*
863 * Program traps generated by user-level software must
864 * be handled by the guest kernel.
865 *
866 * In GS mode, hypervisor privileged instructions trap
867 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
868 * actual program interrupts, handled by the guest.
869 */
Liu Yudaf5e272010-02-02 19:44:35 +0800870 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500871 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600872 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500873 break;
874 }
875
Scott Woodd30f6e42011-12-20 15:34:43 +0000876 r = emulation_exit(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500877 break;
878
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200879 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600880 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600881 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200882 r = RESUME_GUEST;
883 break;
884
Scott Wood4cd35f62011-06-14 18:34:31 -0500885#ifdef CONFIG_SPE
886 case BOOKE_INTERRUPT_SPE_UNAVAIL: {
887 if (vcpu->arch.shared->msr & MSR_SPE)
888 kvmppc_vcpu_enable_spe(vcpu);
889 else
890 kvmppc_booke_queue_irqprio(vcpu,
891 BOOKE_IRQPRIO_SPE_UNAVAIL);
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600892 r = RESUME_GUEST;
893 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500894 }
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600895
896 case BOOKE_INTERRUPT_SPE_FP_DATA:
897 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
898 r = RESUME_GUEST;
899 break;
900
901 case BOOKE_INTERRUPT_SPE_FP_ROUND:
902 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
903 r = RESUME_GUEST;
904 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500905#else
906 case BOOKE_INTERRUPT_SPE_UNAVAIL:
907 /*
908 * Guest wants SPE, but host kernel doesn't support it. Send
909 * an "unimplemented operation" program check to the guest.
910 */
911 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
912 r = RESUME_GUEST;
913 break;
914
915 /*
916 * These really should never happen without CONFIG_SPE,
917 * as we should never enable the real MSR[SPE] in the guest.
918 */
919 case BOOKE_INTERRUPT_SPE_FP_DATA:
920 case BOOKE_INTERRUPT_SPE_FP_ROUND:
921 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
922 __func__, exit_nr, vcpu->arch.pc);
923 run->hw.hardware_exit_reason = exit_nr;
924 r = RESUME_HOST;
925 break;
926#endif
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600927
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500928 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800929 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
930 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600931 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500932 r = RESUME_GUEST;
933 break;
934
935 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800936 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600937 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500938 r = RESUME_GUEST;
939 break;
940
Scott Woodd30f6e42011-12-20 15:34:43 +0000941#ifdef CONFIG_KVM_BOOKE_HV
942 case BOOKE_INTERRUPT_HV_SYSCALL:
943 if (!(vcpu->arch.shared->msr & MSR_PR)) {
944 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
945 } else {
946 /*
947 * hcall from guest userspace -- send privileged
948 * instruction program check.
949 */
950 kvmppc_core_queue_program(vcpu, ESR_PPR);
951 }
952
953 r = RESUME_GUEST;
954 break;
955#else
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500956 case BOOKE_INTERRUPT_SYSCALL:
Alexander Graf2a342ed2010-07-29 14:47:48 +0200957 if (!(vcpu->arch.shared->msr & MSR_PR) &&
958 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
959 /* KVM PV hypercalls */
960 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
961 r = RESUME_GUEST;
962 } else {
963 /* Guest syscalls */
964 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
965 }
Hollis Blanchard7b701592008-12-02 15:51:58 -0600966 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500967 r = RESUME_GUEST;
968 break;
Scott Woodd30f6e42011-12-20 15:34:43 +0000969#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500970
971 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500972 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600973 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600974 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500975 gfn_t gfn;
976
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000977#ifdef CONFIG_KVM_E500V2
Scott Wooda4cd8b22011-06-14 18:34:41 -0500978 if (!(vcpu->arch.shared->msr & MSR_PR) &&
979 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
980 kvmppc_map_magic(vcpu);
981 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
982 r = RESUME_GUEST;
983
984 break;
985 }
986#endif
987
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500988 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600989 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600990 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500991 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +0800992 kvmppc_core_queue_dtlb_miss(vcpu,
993 vcpu->arch.fault_dear,
994 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600995 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600996 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500997 r = RESUME_GUEST;
998 break;
999 }
1000
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -06001001 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001002 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001003
1004 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1005 /* The guest TLB had a mapping, but the shadow TLB
1006 * didn't, and it is RAM. This could be because:
1007 * a) the entry is mapping the host kernel, or
1008 * b) the guest used a large mapping which we're faking
1009 * Either way, we need to satisfy the fault without
1010 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -06001011 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001012 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001013 r = RESUME_GUEST;
1014 } else {
1015 /* Guest has mapped and accessed a page which is not
1016 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -06001017 vcpu->arch.paddr_accessed = gpaddr;
Alexander Graf6020c0f2012-03-12 02:26:30 +01001018 vcpu->arch.vaddr_accessed = eaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001019 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001020 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001021 }
1022
1023 break;
1024 }
1025
1026 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001027 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -06001028 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001029 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001030 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001031
1032 r = RESUME_GUEST;
1033
1034 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -06001035 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -06001036 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001037 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001038 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -06001039 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -06001040 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001041 break;
1042 }
1043
Hollis Blanchard7b701592008-12-02 15:51:58 -06001044 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001045
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -06001046 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -06001047 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001048
1049 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1050 /* The guest TLB had a mapping, but the shadow TLB
1051 * didn't. This could be because:
1052 * a) the entry is mapping the host kernel, or
1053 * b) the guest used a large mapping which we're faking
1054 * Either way, we need to satisfy the fault without
1055 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -06001056 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001057 } else {
1058 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -06001059 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001060 }
1061
1062 break;
1063 }
1064
Hollis Blanchard6a0ab732008-07-25 13:54:49 -05001065 case BOOKE_INTERRUPT_DEBUG: {
1066 u32 dbsr;
1067
1068 vcpu->arch.pc = mfspr(SPRN_CSRR0);
1069
1070 /* clear IAC events in DBSR register */
1071 dbsr = mfspr(SPRN_DBSR);
1072 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
1073 mtspr(SPRN_DBSR, dbsr);
1074
1075 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -06001076 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -05001077 r = RESUME_HOST;
1078 break;
1079 }
1080
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001081 default:
1082 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
1083 BUG();
1084 }
1085
Alexander Grafa8e4ef82012-02-16 14:07:37 +00001086 /*
1087 * To avoid clobbering exit_reason, only check for signals if we
1088 * aren't already exiting to userspace for some other reason.
1089 */
Alexander Graf03660ba2012-02-28 12:00:41 +01001090 if (!(r & RESUME_HOST)) {
1091 local_irq_disable();
Alexander Graf7ee78852012-08-13 12:44:41 +02001092 s = kvmppc_prepare_to_enter(vcpu);
1093 if (s <= 0) {
Alexander Graf24afa372012-08-12 12:42:30 +02001094 local_irq_enable();
Alexander Graf7ee78852012-08-13 12:44:41 +02001095 r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
Alexander Graf24afa372012-08-12 12:42:30 +02001096 } else {
Alexander Grafbd2be682012-08-13 01:04:19 +02001097 kvmppc_lazy_ee_enable();
Alexander Graf03660ba2012-02-28 12:00:41 +01001098 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001099 }
1100
1101 return r;
1102}
1103
1104/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
1105int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1106{
Hollis Blanchard082decf2010-08-07 10:33:56 -07001107 int i;
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001108 int r;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001109
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001110 vcpu->arch.pc = 0;
Scott Woodb5904972011-11-08 18:23:30 -06001111 vcpu->arch.shared->pir = vcpu->vcpu_id;
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001112 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Scott Woodd30f6e42011-12-20 15:34:43 +00001113 kvmppc_set_msr(vcpu, 0);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001114
Scott Woodd30f6e42011-12-20 15:34:43 +00001115#ifndef CONFIG_KVM_BOOKE_HV
1116 vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001117 vcpu->arch.shadow_pid = 1;
Scott Woodd30f6e42011-12-20 15:34:43 +00001118 vcpu->arch.shared->msr = 0;
1119#endif
Hollis Blanchard49dd2c42008-07-25 13:54:53 -05001120
Hollis Blanchard082decf2010-08-07 10:33:56 -07001121 /* Eye-catching numbers so we know if the guest takes an interrupt
1122 * before it's programmed its own IVPR/IVORs. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001123 vcpu->arch.ivpr = 0x55550000;
Hollis Blanchard082decf2010-08-07 10:33:56 -07001124 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
1125 vcpu->arch.ivor[i] = 0x7700 | i * 4;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001126
Hollis Blanchard73e75b42008-12-02 15:51:57 -06001127 kvmppc_init_timing_stats(vcpu);
1128
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001129 r = kvmppc_core_vcpu_setup(vcpu);
1130 kvmppc_sanity_check(vcpu);
1131 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001132}
1133
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001134int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
1135{
1136 /* setup watchdog timer once */
1137 spin_lock_init(&vcpu->arch.wdt_lock);
1138 setup_timer(&vcpu->arch.wdt_timer, kvmppc_watchdog_func,
1139 (unsigned long)vcpu);
1140
1141 return 0;
1142}
1143
1144void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
1145{
1146 del_timer_sync(&vcpu->arch.wdt_timer);
1147}
1148
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001149int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1150{
1151 int i;
1152
1153 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001154 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001155 regs->ctr = vcpu->arch.ctr;
1156 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001157 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +02001158 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +02001159 regs->srr0 = vcpu->arch.shared->srr0;
1160 regs->srr1 = vcpu->arch.shared->srr1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001161 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +02001162 regs->sprg0 = vcpu->arch.shared->sprg0;
1163 regs->sprg1 = vcpu->arch.shared->sprg1;
1164 regs->sprg2 = vcpu->arch.shared->sprg2;
1165 regs->sprg3 = vcpu->arch.shared->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -06001166 regs->sprg4 = vcpu->arch.shared->sprg4;
1167 regs->sprg5 = vcpu->arch.shared->sprg5;
1168 regs->sprg6 = vcpu->arch.shared->sprg6;
1169 regs->sprg7 = vcpu->arch.shared->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001170
1171 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001172 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001173
1174 return 0;
1175}
1176
1177int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1178{
1179 int i;
1180
1181 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +01001182 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001183 vcpu->arch.ctr = regs->ctr;
1184 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +01001185 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -06001186 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +02001187 vcpu->arch.shared->srr0 = regs->srr0;
1188 vcpu->arch.shared->srr1 = regs->srr1;
Scott Wood5ce941e2011-04-27 17:24:21 -05001189 kvmppc_set_pid(vcpu, regs->pid);
Alexander Grafa73a9592010-07-29 14:47:47 +02001190 vcpu->arch.shared->sprg0 = regs->sprg0;
1191 vcpu->arch.shared->sprg1 = regs->sprg1;
1192 vcpu->arch.shared->sprg2 = regs->sprg2;
1193 vcpu->arch.shared->sprg3 = regs->sprg3;
Scott Woodb5904972011-11-08 18:23:30 -06001194 vcpu->arch.shared->sprg4 = regs->sprg4;
1195 vcpu->arch.shared->sprg5 = regs->sprg5;
1196 vcpu->arch.shared->sprg6 = regs->sprg6;
1197 vcpu->arch.shared->sprg7 = regs->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001198
Alexander Graf8e5b26b2010-01-08 02:58:01 +01001199 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1200 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001201
1202 return 0;
1203}
1204
Scott Wood5ce941e2011-04-27 17:24:21 -05001205static void get_sregs_base(struct kvm_vcpu *vcpu,
1206 struct kvm_sregs *sregs)
1207{
1208 u64 tb = get_tb();
1209
1210 sregs->u.e.features |= KVM_SREGS_E_BASE;
1211
1212 sregs->u.e.csrr0 = vcpu->arch.csrr0;
1213 sregs->u.e.csrr1 = vcpu->arch.csrr1;
1214 sregs->u.e.mcsr = vcpu->arch.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +00001215 sregs->u.e.esr = get_guest_esr(vcpu);
1216 sregs->u.e.dear = get_guest_dear(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001217 sregs->u.e.tsr = vcpu->arch.tsr;
1218 sregs->u.e.tcr = vcpu->arch.tcr;
1219 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
1220 sregs->u.e.tb = tb;
1221 sregs->u.e.vrsave = vcpu->arch.vrsave;
1222}
1223
1224static int set_sregs_base(struct kvm_vcpu *vcpu,
1225 struct kvm_sregs *sregs)
1226{
1227 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
1228 return 0;
1229
1230 vcpu->arch.csrr0 = sregs->u.e.csrr0;
1231 vcpu->arch.csrr1 = sregs->u.e.csrr1;
1232 vcpu->arch.mcsr = sregs->u.e.mcsr;
Scott Woodd30f6e42011-12-20 15:34:43 +00001233 set_guest_esr(vcpu, sregs->u.e.esr);
1234 set_guest_dear(vcpu, sregs->u.e.dear);
Scott Wood5ce941e2011-04-27 17:24:21 -05001235 vcpu->arch.vrsave = sregs->u.e.vrsave;
Scott Wooddfd4d472011-11-17 12:39:59 +00001236 kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
Scott Wood5ce941e2011-04-27 17:24:21 -05001237
Scott Wooddfd4d472011-11-17 12:39:59 +00001238 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
Scott Wood5ce941e2011-04-27 17:24:21 -05001239 vcpu->arch.dec = sregs->u.e.dec;
Scott Wooddfd4d472011-11-17 12:39:59 +00001240 kvmppc_emulate_dec(vcpu);
1241 }
Scott Wood5ce941e2011-04-27 17:24:21 -05001242
1243 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001244 u32 old_tsr = vcpu->arch.tsr;
1245
Scott Wooddfd4d472011-11-17 12:39:59 +00001246 vcpu->arch.tsr = sregs->u.e.tsr;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001247
1248 if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS))
1249 arm_next_watchdog(vcpu);
1250
Scott Wooddfd4d472011-11-17 12:39:59 +00001251 update_timer_ints(vcpu);
Scott Wood5ce941e2011-04-27 17:24:21 -05001252 }
1253
1254 return 0;
1255}
1256
1257static void get_sregs_arch206(struct kvm_vcpu *vcpu,
1258 struct kvm_sregs *sregs)
1259{
1260 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
1261
Scott Wood841741f2011-09-02 17:39:37 -05001262 sregs->u.e.pir = vcpu->vcpu_id;
Scott Wood5ce941e2011-04-27 17:24:21 -05001263 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
1264 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
1265 sregs->u.e.decar = vcpu->arch.decar;
1266 sregs->u.e.ivpr = vcpu->arch.ivpr;
1267}
1268
1269static int set_sregs_arch206(struct kvm_vcpu *vcpu,
1270 struct kvm_sregs *sregs)
1271{
1272 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
1273 return 0;
1274
Scott Wood841741f2011-09-02 17:39:37 -05001275 if (sregs->u.e.pir != vcpu->vcpu_id)
Scott Wood5ce941e2011-04-27 17:24:21 -05001276 return -EINVAL;
1277
1278 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
1279 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
1280 vcpu->arch.decar = sregs->u.e.decar;
1281 vcpu->arch.ivpr = sregs->u.e.ivpr;
1282
1283 return 0;
1284}
1285
1286void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1287{
1288 sregs->u.e.features |= KVM_SREGS_E_IVOR;
1289
1290 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
1291 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
1292 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
1293 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
1294 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
1295 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
1296 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
1297 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
1298 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
1299 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
1300 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
1301 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
1302 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
1303 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
1304 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
1305 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
1306}
1307
1308int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1309{
1310 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
1311 return 0;
1312
1313 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
1314 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
1315 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
1316 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
1317 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
1318 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
1319 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
1320 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
1321 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
1322 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
1323 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
1324 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
1325 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
1326 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
1327 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
1328 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
1329
1330 return 0;
1331}
1332
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001333int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1334 struct kvm_sregs *sregs)
1335{
Scott Wood5ce941e2011-04-27 17:24:21 -05001336 sregs->pvr = vcpu->arch.pvr;
1337
1338 get_sregs_base(vcpu, sregs);
1339 get_sregs_arch206(vcpu, sregs);
1340 kvmppc_core_get_sregs(vcpu, sregs);
1341 return 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001342}
1343
1344int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1345 struct kvm_sregs *sregs)
1346{
Scott Wood5ce941e2011-04-27 17:24:21 -05001347 int ret;
1348
1349 if (vcpu->arch.pvr != sregs->pvr)
1350 return -EINVAL;
1351
1352 ret = set_sregs_base(vcpu, sregs);
1353 if (ret < 0)
1354 return ret;
1355
1356 ret = set_sregs_arch206(vcpu, sregs);
1357 if (ret < 0)
1358 return ret;
1359
1360 return kvmppc_core_set_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001361}
1362
Paul Mackerras31f34382011-12-12 12:26:50 +00001363int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1364{
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001365 int r = -EINVAL;
1366
1367 switch (reg->id) {
1368 case KVM_REG_PPC_IAC1:
1369 case KVM_REG_PPC_IAC2:
1370 case KVM_REG_PPC_IAC3:
1371 case KVM_REG_PPC_IAC4: {
1372 int iac = reg->id - KVM_REG_PPC_IAC1;
1373 r = copy_to_user((u64 __user *)(long)reg->addr,
1374 &vcpu->arch.dbg_reg.iac[iac], sizeof(u64));
1375 break;
1376 }
1377 case KVM_REG_PPC_DAC1:
1378 case KVM_REG_PPC_DAC2: {
1379 int dac = reg->id - KVM_REG_PPC_DAC1;
1380 r = copy_to_user((u64 __user *)(long)reg->addr,
1381 &vcpu->arch.dbg_reg.dac[dac], sizeof(u64));
1382 break;
1383 }
1384 default:
1385 break;
1386 }
1387 return r;
Paul Mackerras31f34382011-12-12 12:26:50 +00001388}
1389
1390int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1391{
Bharat Bhushan6df8d3f2012-08-08 21:17:55 +00001392 int r = -EINVAL;
1393
1394 switch (reg->id) {
1395 case KVM_REG_PPC_IAC1:
1396 case KVM_REG_PPC_IAC2:
1397 case KVM_REG_PPC_IAC3:
1398 case KVM_REG_PPC_IAC4: {
1399 int iac = reg->id - KVM_REG_PPC_IAC1;
1400 r = copy_from_user(&vcpu->arch.dbg_reg.iac[iac],
1401 (u64 __user *)(long)reg->addr, sizeof(u64));
1402 break;
1403 }
1404 case KVM_REG_PPC_DAC1:
1405 case KVM_REG_PPC_DAC2: {
1406 int dac = reg->id - KVM_REG_PPC_DAC1;
1407 r = copy_from_user(&vcpu->arch.dbg_reg.dac[dac],
1408 (u64 __user *)(long)reg->addr, sizeof(u64));
1409 break;
1410 }
1411 default:
1412 break;
1413 }
1414 return r;
Paul Mackerras31f34382011-12-12 12:26:50 +00001415}
1416
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001417int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1418{
1419 return -ENOTSUPP;
1420}
1421
1422int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1423{
1424 return -ENOTSUPP;
1425}
1426
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001427int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1428 struct kvm_translation *tr)
1429{
Avi Kivity98001d82010-05-13 11:05:49 +03001430 int r;
1431
Avi Kivity98001d82010-05-13 11:05:49 +03001432 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +03001433 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001434}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001435
Alexander Graf4e755752009-10-30 05:47:01 +00001436int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1437{
1438 return -ENOTSUPP;
1439}
1440
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001441void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1442 struct kvm_memory_slot *dont)
1443{
1444}
1445
1446int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1447 unsigned long npages)
1448{
1449 return 0;
1450}
1451
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001452int kvmppc_core_prepare_memory_region(struct kvm *kvm,
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00001453 struct kvm_memory_slot *memslot,
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001454 struct kvm_userspace_memory_region *mem)
1455{
1456 return 0;
1457}
1458
1459void kvmppc_core_commit_memory_region(struct kvm *kvm,
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00001460 struct kvm_userspace_memory_region *mem,
1461 struct kvm_memory_slot old)
1462{
1463}
1464
1465void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
Paul Mackerrasf9e05542011-06-29 00:19:22 +00001466{
1467}
1468
Scott Wooddfd4d472011-11-17 12:39:59 +00001469void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1470{
1471 vcpu->arch.tcr = new_tcr;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001472 arm_next_watchdog(vcpu);
Scott Wooddfd4d472011-11-17 12:39:59 +00001473 update_timer_ints(vcpu);
1474}
1475
1476void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1477{
1478 set_bits(tsr_bits, &vcpu->arch.tsr);
1479 smp_wmb();
1480 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1481 kvm_vcpu_kick(vcpu);
1482}
1483
1484void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1485{
1486 clear_bits(tsr_bits, &vcpu->arch.tsr);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +00001487
1488 /*
1489 * We may have stopped the watchdog due to
1490 * being stuck on final expiration.
1491 */
1492 if (tsr_bits & (TSR_ENW | TSR_WIS))
1493 arm_next_watchdog(vcpu);
1494
Scott Wooddfd4d472011-11-17 12:39:59 +00001495 update_timer_ints(vcpu);
1496}
1497
1498void kvmppc_decrementer_func(unsigned long data)
1499{
1500 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1501
Bharat Bhushan21bd0002012-05-20 23:21:23 +00001502 if (vcpu->arch.tcr & TCR_ARE) {
1503 vcpu->arch.dec = vcpu->arch.decar;
1504 kvmppc_emulate_dec(vcpu);
1505 }
1506
Scott Wooddfd4d472011-11-17 12:39:59 +00001507 kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1508}
1509
Scott Wood94fa9d92011-12-20 15:34:22 +00001510void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1511{
Paul Mackerrasa47d72f2012-09-20 19:35:51 +00001512 vcpu->cpu = smp_processor_id();
Scott Woodd30f6e42011-12-20 15:34:43 +00001513 current->thread.kvm_vcpu = vcpu;
Scott Wood94fa9d92011-12-20 15:34:22 +00001514}
1515
1516void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
1517{
Scott Woodd30f6e42011-12-20 15:34:43 +00001518 current->thread.kvm_vcpu = NULL;
Paul Mackerrasa47d72f2012-09-20 19:35:51 +00001519 vcpu->cpu = -1;
Scott Wood94fa9d92011-12-20 15:34:22 +00001520}
1521
Stephen Rothwell2986b8c2009-06-02 11:46:14 +10001522int __init kvmppc_booke_init(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001523{
Scott Woodd30f6e42011-12-20 15:34:43 +00001524#ifndef CONFIG_KVM_BOOKE_HV
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001525 unsigned long ivor[16];
1526 unsigned long max_ivor = 0;
1527 int i;
1528
1529 /* We install our own exception handlers by hijacking IVPR. IVPR must
1530 * be 16-bit aligned, so we need a 64KB allocation. */
1531 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1532 VCPU_SIZE_ORDER);
1533 if (!kvmppc_booke_handlers)
1534 return -ENOMEM;
1535
1536 /* XXX make sure our handlers are smaller than Linux's */
1537
1538 /* Copy our interrupt handlers to match host IVORs. That way we don't
1539 * have to swap the IVORs on every guest/host transition. */
1540 ivor[0] = mfspr(SPRN_IVOR0);
1541 ivor[1] = mfspr(SPRN_IVOR1);
1542 ivor[2] = mfspr(SPRN_IVOR2);
1543 ivor[3] = mfspr(SPRN_IVOR3);
1544 ivor[4] = mfspr(SPRN_IVOR4);
1545 ivor[5] = mfspr(SPRN_IVOR5);
1546 ivor[6] = mfspr(SPRN_IVOR6);
1547 ivor[7] = mfspr(SPRN_IVOR7);
1548 ivor[8] = mfspr(SPRN_IVOR8);
1549 ivor[9] = mfspr(SPRN_IVOR9);
1550 ivor[10] = mfspr(SPRN_IVOR10);
1551 ivor[11] = mfspr(SPRN_IVOR11);
1552 ivor[12] = mfspr(SPRN_IVOR12);
1553 ivor[13] = mfspr(SPRN_IVOR13);
1554 ivor[14] = mfspr(SPRN_IVOR14);
1555 ivor[15] = mfspr(SPRN_IVOR15);
1556
1557 for (i = 0; i < 16; i++) {
1558 if (ivor[i] > max_ivor)
1559 max_ivor = ivor[i];
1560
1561 memcpy((void *)kvmppc_booke_handlers + ivor[i],
1562 kvmppc_handlers_start + i * kvmppc_handler_len,
1563 kvmppc_handler_len);
1564 }
1565 flush_icache_range(kvmppc_booke_handlers,
1566 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
Scott Woodd30f6e42011-12-20 15:34:43 +00001567#endif /* !BOOKE_HV */
Hollis Blancharddb93f572008-11-05 09:36:18 -06001568 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001569}
1570
Hollis Blancharddb93f572008-11-05 09:36:18 -06001571void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -06001572{
1573 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
1574 kvm_exit();
1575}