blob: feaefc433276cb78863604622084fd83818708d7 [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>
20 */
21
22#include <linux/errno.h>
23#include <linux/err.h>
24#include <linux/kvm_host.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/gfp.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050026#include <linux/module.h>
27#include <linux/vmalloc.h>
28#include <linux/fs.h>
Hollis Blanchard7924bd42008-12-02 15:51:55 -060029
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050030#include <asm/cputable.h>
31#include <asm/uaccess.h>
32#include <asm/kvm_ppc.h>
Hollis Blanchard73e75b42008-12-02 15:51:57 -060033#include "timing.h"
Hollis Blanchardd9fbd032008-11-05 09:36:13 -060034#include <asm/cacheflush.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050035
Hollis Blanchard75f74f02008-11-05 09:36:16 -060036#include "booke.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050037
Hollis Blanchardd9fbd032008-11-05 09:36:13 -060038unsigned long kvmppc_booke_handlers;
39
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050040#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
41#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
42
43struct kvm_stats_debugfs_item debugfs_entries[] = {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050044 { "mmio", VCPU_STAT(mmio_exits) },
45 { "dcr", VCPU_STAT(dcr_exits) },
46 { "sig", VCPU_STAT(signal_exits) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050047 { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
48 { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
49 { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
50 { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
51 { "sysc", VCPU_STAT(syscall_exits) },
52 { "isi", VCPU_STAT(isi_exits) },
53 { "dsi", VCPU_STAT(dsi_exits) },
54 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
55 { "dec", VCPU_STAT(dec_exits) },
56 { "ext_intr", VCPU_STAT(ext_intr_exits) },
Hollis Blanchard45c5eb62008-04-25 17:55:49 -050057 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050058 { NULL }
59};
60
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050061/* TODO: use vcpu_printf() */
62void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
63{
64 int i;
65
Alexander Graf666e7252010-07-29 14:47:43 +020066 printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060067 printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
Alexander Grafde7906c2010-07-29 14:47:46 +020068 printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
69 vcpu->arch.shared->srr1);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050070
71 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
72
73 for (i = 0; i < 32; i += 4) {
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060074 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
Alexander Graf8e5b26b2010-01-08 02:58:01 +010075 kvmppc_get_gpr(vcpu, i),
76 kvmppc_get_gpr(vcpu, i+1),
77 kvmppc_get_gpr(vcpu, i+2),
78 kvmppc_get_gpr(vcpu, i+3));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050079 }
80}
81
Scott Wood4cd35f62011-06-14 18:34:31 -050082#ifdef CONFIG_SPE
83void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
84{
85 preempt_disable();
86 enable_kernel_spe();
87 kvmppc_save_guest_spe(vcpu);
88 vcpu->arch.shadow_msr &= ~MSR_SPE;
89 preempt_enable();
90}
91
92static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
93{
94 preempt_disable();
95 enable_kernel_spe();
96 kvmppc_load_guest_spe(vcpu);
97 vcpu->arch.shadow_msr |= MSR_SPE;
98 preempt_enable();
99}
100
101static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
102{
103 if (vcpu->arch.shared->msr & MSR_SPE) {
104 if (!(vcpu->arch.shadow_msr & MSR_SPE))
105 kvmppc_vcpu_enable_spe(vcpu);
106 } else if (vcpu->arch.shadow_msr & MSR_SPE) {
107 kvmppc_vcpu_disable_spe(vcpu);
108 }
109}
110#else
111static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
112{
113}
114#endif
115
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500116/*
117 * Helper function for "full" MSR writes. No need to call this if only
118 * EE/CE/ME/DE/RI are changing.
119 */
Scott Wood4cd35f62011-06-14 18:34:31 -0500120void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
121{
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500122 u32 old_msr = vcpu->arch.shared->msr;
Scott Wood4cd35f62011-06-14 18:34:31 -0500123
124 vcpu->arch.shared->msr = new_msr;
125
Liu Yudd9ebf1f2011-06-14 18:35:14 -0500126 kvmppc_mmu_msr_notify(vcpu, old_msr);
127
Scott Wood4cd35f62011-06-14 18:34:31 -0500128 if (vcpu->arch.shared->msr & MSR_WE) {
129 kvm_vcpu_block(vcpu);
130 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
131 };
132
133 kvmppc_vcpu_sync_spe(vcpu);
134}
135
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600136static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
137 unsigned int priority)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600138{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600139 set_bit(priority, &vcpu->arch.pending_exceptions);
140}
141
Liu Yudaf5e272010-02-02 19:44:35 +0800142static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
143 ulong dear_flags, ulong esr_flags)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600144{
Liu Yudaf5e272010-02-02 19:44:35 +0800145 vcpu->arch.queued_dear = dear_flags;
146 vcpu->arch.queued_esr = esr_flags;
147 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
148}
149
150static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
151 ulong dear_flags, ulong esr_flags)
152{
153 vcpu->arch.queued_dear = dear_flags;
154 vcpu->arch.queued_esr = esr_flags;
155 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
156}
157
158static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
159 ulong esr_flags)
160{
161 vcpu->arch.queued_esr = esr_flags;
162 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
163}
164
165void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
166{
167 vcpu->arch.queued_esr = esr_flags;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600168 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600169}
170
171void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
172{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600173 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600174}
175
176int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
177{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600178 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600179}
180
Alexander Graf7706664d2009-12-21 20:21:24 +0100181void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
182{
183 clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
184}
185
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600186void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
187 struct kvm_interrupt *irq)
188{
Alexander Grafc5335f12010-08-30 14:03:24 +0200189 unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
190
191 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
192 prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
193
194 kvmppc_booke_queue_irqprio(vcpu, prio);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600195}
196
Alexander Graf4496f972010-04-07 10:03:25 +0200197void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
198 struct kvm_interrupt *irq)
199{
200 clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
Alexander Grafc5335f12010-08-30 14:03:24 +0200201 clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
Alexander Graf4496f972010-04-07 10:03:25 +0200202}
203
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600204/* Deliver the interrupt of the corresponding priority, if possible. */
205static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
206 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500207{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600208 int allowed = 0;
Asias He6045be52010-06-19 16:52:12 +0800209 ulong uninitialized_var(msr_mask);
Liu Yudaf5e272010-02-02 19:44:35 +0800210 bool update_esr = false, update_dear = false;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200211 ulong crit_raw = vcpu->arch.shared->critical;
212 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
213 bool crit;
Alexander Grafc5335f12010-08-30 14:03:24 +0200214 bool keep_irq = false;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200215
216 /* Truncate crit indicators in 32 bit mode */
217 if (!(vcpu->arch.shared->msr & MSR_SF)) {
218 crit_raw &= 0xffffffff;
219 crit_r1 &= 0xffffffff;
220 }
221
222 /* Critical section when crit == r1 */
223 crit = (crit_raw == crit_r1);
224 /* ... and we're in supervisor mode */
225 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500226
Alexander Grafc5335f12010-08-30 14:03:24 +0200227 if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
228 priority = BOOKE_IRQPRIO_EXTERNAL;
229 keep_irq = true;
230 }
231
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600232 switch (priority) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600233 case BOOKE_IRQPRIO_DTLB_MISS:
Liu Yudaf5e272010-02-02 19:44:35 +0800234 case BOOKE_IRQPRIO_DATA_STORAGE:
235 update_dear = true;
236 /* fall through */
237 case BOOKE_IRQPRIO_INST_STORAGE:
238 case BOOKE_IRQPRIO_PROGRAM:
239 update_esr = true;
240 /* fall through */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600241 case BOOKE_IRQPRIO_ITLB_MISS:
242 case BOOKE_IRQPRIO_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600243 case BOOKE_IRQPRIO_FP_UNAVAIL:
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600244 case BOOKE_IRQPRIO_SPE_UNAVAIL:
245 case BOOKE_IRQPRIO_SPE_FP_DATA:
246 case BOOKE_IRQPRIO_SPE_FP_ROUND:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600247 case BOOKE_IRQPRIO_AP_UNAVAIL:
248 case BOOKE_IRQPRIO_ALIGNMENT:
249 allowed = 1;
250 msr_mask = MSR_CE|MSR_ME|MSR_DE;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500251 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600252 case BOOKE_IRQPRIO_CRITICAL:
253 case BOOKE_IRQPRIO_WATCHDOG:
Alexander Graf666e7252010-07-29 14:47:43 +0200254 allowed = vcpu->arch.shared->msr & MSR_CE;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600255 msr_mask = MSR_ME;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500256 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600257 case BOOKE_IRQPRIO_MACHINE_CHECK:
Alexander Graf666e7252010-07-29 14:47:43 +0200258 allowed = vcpu->arch.shared->msr & MSR_ME;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600259 msr_mask = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500260 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600261 case BOOKE_IRQPRIO_EXTERNAL:
262 case BOOKE_IRQPRIO_DECREMENTER:
263 case BOOKE_IRQPRIO_FIT:
Alexander Graf666e7252010-07-29 14:47:43 +0200264 allowed = vcpu->arch.shared->msr & MSR_EE;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200265 allowed = allowed && !crit;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600266 msr_mask = MSR_CE|MSR_ME|MSR_DE;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500267 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600268 case BOOKE_IRQPRIO_DEBUG:
Alexander Graf666e7252010-07-29 14:47:43 +0200269 allowed = vcpu->arch.shared->msr & MSR_DE;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600270 msr_mask = MSR_ME;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500271 break;
272 }
273
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600274 if (allowed) {
Alexander Grafde7906c2010-07-29 14:47:46 +0200275 vcpu->arch.shared->srr0 = vcpu->arch.pc;
276 vcpu->arch.shared->srr1 = vcpu->arch.shared->msr;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600277 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
Liu Yudaf5e272010-02-02 19:44:35 +0800278 if (update_esr == true)
279 vcpu->arch.esr = vcpu->arch.queued_esr;
280 if (update_dear == true)
Alexander Graf5e030182010-07-29 14:47:45 +0200281 vcpu->arch.shared->dar = vcpu->arch.queued_dear;
Alexander Graf666e7252010-07-29 14:47:43 +0200282 kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600283
Alexander Grafc5335f12010-08-30 14:03:24 +0200284 if (!keep_irq)
285 clear_bit(priority, &vcpu->arch.pending_exceptions);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600286 }
287
288 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500289}
290
291/* Check pending exceptions and deliver one, if possible. */
Scott Wood7e28e60e2011-11-08 18:23:20 -0600292void kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500293{
294 unsigned long *pending = &vcpu->arch.pending_exceptions;
Alexander Graf90bba352010-07-29 14:47:51 +0200295 unsigned long old_pending = vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500296 unsigned int priority;
297
Scott Wood25051b52011-11-08 18:23:23 -0600298 WARN_ON_ONCE(!irqs_disabled());
299
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600300 priority = __ffs(*pending);
Hollis Blanchardbdc89f12009-01-03 16:23:12 -0600301 while (priority <= BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600302 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500303 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500304
305 priority = find_next_bit(pending,
306 BITS_PER_BYTE * sizeof(*pending),
307 priority + 1);
308 }
Alexander Graf90bba352010-07-29 14:47:51 +0200309
310 /* Tell the guest about our interrupt status */
311 if (*pending)
312 vcpu->arch.shared->int_pending = 1;
313 else if (old_pending)
314 vcpu->arch.shared->int_pending = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500315}
316
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000317int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
318{
319 int ret;
320
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200321 if (!vcpu->arch.sane) {
322 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
323 return -EINVAL;
324 }
325
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000326 local_irq_disable();
Scott Wood1d1ef222011-11-08 16:11:59 -0600327
Scott Wood25051b52011-11-08 18:23:23 -0600328 kvmppc_core_prepare_to_enter(vcpu);
329
Scott Wood1d1ef222011-11-08 16:11:59 -0600330 if (signal_pending(current)) {
331 kvm_run->exit_reason = KVM_EXIT_INTR;
332 ret = -EINTR;
333 goto out;
334 }
335
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000336 kvm_guest_enter();
337 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
338 kvm_guest_exit();
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000339
Scott Wood1d1ef222011-11-08 16:11:59 -0600340out:
341 local_irq_enable();
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000342 return ret;
343}
344
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500345/**
346 * kvmppc_handle_exit
347 *
348 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
349 */
350int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
351 unsigned int exit_nr)
352{
353 enum emulation_result er;
354 int r = RESUME_HOST;
355
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600356 /* update before a new last_exit_type is rewritten */
357 kvmppc_update_timing_stats(vcpu);
358
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500359 local_irq_enable();
360
361 run->exit_reason = KVM_EXIT_UNKNOWN;
362 run->ready_for_interrupt_injection = 1;
363
364 switch (exit_nr) {
365 case BOOKE_INTERRUPT_MACHINE_CHECK:
366 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
367 kvmppc_dump_vcpu(vcpu);
368 r = RESUME_HOST;
369 break;
370
371 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600372 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -0600373 if (need_resched())
374 cond_resched();
375 r = RESUME_GUEST;
376 break;
377
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500378 case BOOKE_INTERRUPT_DECREMENTER:
379 /* Since we switched IVPR back to the host's value, the host
380 * handled this interrupt the moment we enabled interrupts.
381 * Now we just offer it a chance to reschedule the guest. */
Hollis Blanchard7b701592008-12-02 15:51:58 -0600382 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500383 if (need_resched())
384 cond_resched();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500385 r = RESUME_GUEST;
386 break;
387
388 case BOOKE_INTERRUPT_PROGRAM:
Alexander Graf666e7252010-07-29 14:47:43 +0200389 if (vcpu->arch.shared->msr & MSR_PR) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500390 /* Program traps generated by user-level software must be handled
391 * by the guest kernel. */
Liu Yudaf5e272010-02-02 19:44:35 +0800392 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500393 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600394 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500395 break;
396 }
397
398 er = kvmppc_emulate_instruction(run, vcpu);
399 switch (er) {
400 case EMULATE_DONE:
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600401 /* don't overwrite subtypes, just account kvm_stats */
Hollis Blanchard7b701592008-12-02 15:51:58 -0600402 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500403 /* Future optimization: only reload non-volatiles if
404 * they were actually modified by emulation. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500405 r = RESUME_GUEST_NV;
406 break;
407 case EMULATE_DO_DCR:
408 run->exit_reason = KVM_EXIT_DCR;
409 r = RESUME_HOST;
410 break;
411 case EMULATE_FAIL:
412 /* XXX Deliver Program interrupt to guest. */
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -0600413 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500414 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
415 /* For debugging, encode the failing instruction and
416 * report it to userspace. */
417 run->hw.hardware_exit_reason = ~0ULL << 32;
418 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
419 r = RESUME_HOST;
420 break;
421 default:
422 BUG();
423 }
424 break;
425
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200426 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600427 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600428 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200429 r = RESUME_GUEST;
430 break;
431
Scott Wood4cd35f62011-06-14 18:34:31 -0500432#ifdef CONFIG_SPE
433 case BOOKE_INTERRUPT_SPE_UNAVAIL: {
434 if (vcpu->arch.shared->msr & MSR_SPE)
435 kvmppc_vcpu_enable_spe(vcpu);
436 else
437 kvmppc_booke_queue_irqprio(vcpu,
438 BOOKE_IRQPRIO_SPE_UNAVAIL);
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600439 r = RESUME_GUEST;
440 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500441 }
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600442
443 case BOOKE_INTERRUPT_SPE_FP_DATA:
444 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
445 r = RESUME_GUEST;
446 break;
447
448 case BOOKE_INTERRUPT_SPE_FP_ROUND:
449 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
450 r = RESUME_GUEST;
451 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500452#else
453 case BOOKE_INTERRUPT_SPE_UNAVAIL:
454 /*
455 * Guest wants SPE, but host kernel doesn't support it. Send
456 * an "unimplemented operation" program check to the guest.
457 */
458 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
459 r = RESUME_GUEST;
460 break;
461
462 /*
463 * These really should never happen without CONFIG_SPE,
464 * as we should never enable the real MSR[SPE] in the guest.
465 */
466 case BOOKE_INTERRUPT_SPE_FP_DATA:
467 case BOOKE_INTERRUPT_SPE_FP_ROUND:
468 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
469 __func__, exit_nr, vcpu->arch.pc);
470 run->hw.hardware_exit_reason = exit_nr;
471 r = RESUME_HOST;
472 break;
473#endif
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600474
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500475 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800476 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
477 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600478 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500479 r = RESUME_GUEST;
480 break;
481
482 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800483 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600484 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500485 r = RESUME_GUEST;
486 break;
487
488 case BOOKE_INTERRUPT_SYSCALL:
Alexander Graf2a342ed2010-07-29 14:47:48 +0200489 if (!(vcpu->arch.shared->msr & MSR_PR) &&
490 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
491 /* KVM PV hypercalls */
492 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
493 r = RESUME_GUEST;
494 } else {
495 /* Guest syscalls */
496 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
497 }
Hollis Blanchard7b701592008-12-02 15:51:58 -0600498 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500499 r = RESUME_GUEST;
500 break;
501
502 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500503 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600504 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600505 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500506 gfn_t gfn;
507
Scott Wooda4cd8b22011-06-14 18:34:41 -0500508#ifdef CONFIG_KVM_E500
509 if (!(vcpu->arch.shared->msr & MSR_PR) &&
510 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
511 kvmppc_map_magic(vcpu);
512 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
513 r = RESUME_GUEST;
514
515 break;
516 }
517#endif
518
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500519 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600520 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600521 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500522 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +0800523 kvmppc_core_queue_dtlb_miss(vcpu,
524 vcpu->arch.fault_dear,
525 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600526 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600527 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500528 r = RESUME_GUEST;
529 break;
530 }
531
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600532 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600533 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500534
535 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
536 /* The guest TLB had a mapping, but the shadow TLB
537 * didn't, and it is RAM. This could be because:
538 * a) the entry is mapping the host kernel, or
539 * b) the guest used a large mapping which we're faking
540 * Either way, we need to satisfy the fault without
541 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600542 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600543 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500544 r = RESUME_GUEST;
545 } else {
546 /* Guest has mapped and accessed a page which is not
547 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600548 vcpu->arch.paddr_accessed = gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500549 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600550 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500551 }
552
553 break;
554 }
555
556 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500557 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -0600558 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500559 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600560 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500561
562 r = RESUME_GUEST;
563
564 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600565 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600566 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500567 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600568 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600569 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600570 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500571 break;
572 }
573
Hollis Blanchard7b701592008-12-02 15:51:58 -0600574 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500575
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600576 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -0600577 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500578
579 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
580 /* The guest TLB had a mapping, but the shadow TLB
581 * didn't. This could be because:
582 * a) the entry is mapping the host kernel, or
583 * b) the guest used a large mapping which we're faking
584 * Either way, we need to satisfy the fault without
585 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600586 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500587 } else {
588 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600589 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500590 }
591
592 break;
593 }
594
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500595 case BOOKE_INTERRUPT_DEBUG: {
596 u32 dbsr;
597
598 vcpu->arch.pc = mfspr(SPRN_CSRR0);
599
600 /* clear IAC events in DBSR register */
601 dbsr = mfspr(SPRN_DBSR);
602 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
603 mtspr(SPRN_DBSR, dbsr);
604
605 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600606 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500607 r = RESUME_HOST;
608 break;
609 }
610
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500611 default:
612 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
613 BUG();
614 }
615
616 local_irq_disable();
617
Scott Wood7e28e60e2011-11-08 18:23:20 -0600618 kvmppc_core_prepare_to_enter(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500619
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500620 if (!(r & RESUME_HOST)) {
621 /* To avoid clobbering exit_reason, only check for signals if
622 * we aren't already exiting to userspace for some other
623 * reason. */
624 if (signal_pending(current)) {
625 run->exit_reason = KVM_EXIT_INTR;
626 r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600627 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500628 }
629 }
630
631 return r;
632}
633
634/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
635int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
636{
Hollis Blanchard082decf2010-08-07 10:33:56 -0700637 int i;
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200638 int r;
Hollis Blanchard082decf2010-08-07 10:33:56 -0700639
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500640 vcpu->arch.pc = 0;
Alexander Graf666e7252010-07-29 14:47:43 +0200641 vcpu->arch.shared->msr = 0;
Scott Woodecee2732011-06-14 18:34:29 -0500642 vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100643 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500644
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500645 vcpu->arch.shadow_pid = 1;
646
Hollis Blanchard082decf2010-08-07 10:33:56 -0700647 /* Eye-catching numbers so we know if the guest takes an interrupt
648 * before it's programmed its own IVPR/IVORs. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500649 vcpu->arch.ivpr = 0x55550000;
Hollis Blanchard082decf2010-08-07 10:33:56 -0700650 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
651 vcpu->arch.ivor[i] = 0x7700 | i * 4;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500652
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600653 kvmppc_init_timing_stats(vcpu);
654
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200655 r = kvmppc_core_vcpu_setup(vcpu);
656 kvmppc_sanity_check(vcpu);
657 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500658}
659
660int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
661{
662 int i;
663
664 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100665 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500666 regs->ctr = vcpu->arch.ctr;
667 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100668 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +0200669 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +0200670 regs->srr0 = vcpu->arch.shared->srr0;
671 regs->srr1 = vcpu->arch.shared->srr1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500672 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +0200673 regs->sprg0 = vcpu->arch.shared->sprg0;
674 regs->sprg1 = vcpu->arch.shared->sprg1;
675 regs->sprg2 = vcpu->arch.shared->sprg2;
676 regs->sprg3 = vcpu->arch.shared->sprg3;
Peter Tyserbc9c1932010-12-29 13:51:25 -0600677 regs->sprg4 = vcpu->arch.sprg4;
678 regs->sprg5 = vcpu->arch.sprg5;
679 regs->sprg6 = vcpu->arch.sprg6;
680 regs->sprg7 = vcpu->arch.sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500681
682 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100683 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500684
685 return 0;
686}
687
688int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
689{
690 int i;
691
692 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100693 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500694 vcpu->arch.ctr = regs->ctr;
695 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100696 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -0600697 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +0200698 vcpu->arch.shared->srr0 = regs->srr0;
699 vcpu->arch.shared->srr1 = regs->srr1;
Scott Wood5ce941e2011-04-27 17:24:21 -0500700 kvmppc_set_pid(vcpu, regs->pid);
Alexander Grafa73a9592010-07-29 14:47:47 +0200701 vcpu->arch.shared->sprg0 = regs->sprg0;
702 vcpu->arch.shared->sprg1 = regs->sprg1;
703 vcpu->arch.shared->sprg2 = regs->sprg2;
704 vcpu->arch.shared->sprg3 = regs->sprg3;
Peter Tyserbc9c1932010-12-29 13:51:25 -0600705 vcpu->arch.sprg4 = regs->sprg4;
706 vcpu->arch.sprg5 = regs->sprg5;
707 vcpu->arch.sprg6 = regs->sprg6;
708 vcpu->arch.sprg7 = regs->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500709
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100710 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
711 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500712
713 return 0;
714}
715
Scott Wood5ce941e2011-04-27 17:24:21 -0500716static void get_sregs_base(struct kvm_vcpu *vcpu,
717 struct kvm_sregs *sregs)
718{
719 u64 tb = get_tb();
720
721 sregs->u.e.features |= KVM_SREGS_E_BASE;
722
723 sregs->u.e.csrr0 = vcpu->arch.csrr0;
724 sregs->u.e.csrr1 = vcpu->arch.csrr1;
725 sregs->u.e.mcsr = vcpu->arch.mcsr;
726 sregs->u.e.esr = vcpu->arch.esr;
727 sregs->u.e.dear = vcpu->arch.shared->dar;
728 sregs->u.e.tsr = vcpu->arch.tsr;
729 sregs->u.e.tcr = vcpu->arch.tcr;
730 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
731 sregs->u.e.tb = tb;
732 sregs->u.e.vrsave = vcpu->arch.vrsave;
733}
734
735static int set_sregs_base(struct kvm_vcpu *vcpu,
736 struct kvm_sregs *sregs)
737{
738 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
739 return 0;
740
741 vcpu->arch.csrr0 = sregs->u.e.csrr0;
742 vcpu->arch.csrr1 = sregs->u.e.csrr1;
743 vcpu->arch.mcsr = sregs->u.e.mcsr;
744 vcpu->arch.esr = sregs->u.e.esr;
745 vcpu->arch.shared->dar = sregs->u.e.dear;
746 vcpu->arch.vrsave = sregs->u.e.vrsave;
747 vcpu->arch.tcr = sregs->u.e.tcr;
748
749 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC)
750 vcpu->arch.dec = sregs->u.e.dec;
751
752 kvmppc_emulate_dec(vcpu);
753
754 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
755 /*
756 * FIXME: existing KVM timer handling is incomplete.
757 * TSR cannot be read by the guest, and its value in
758 * vcpu->arch is always zero. For now, just handle
759 * the case where the caller is trying to inject a
760 * decrementer interrupt.
761 */
762
763 if ((sregs->u.e.tsr & TSR_DIS) &&
764 (vcpu->arch.tcr & TCR_DIE))
765 kvmppc_core_queue_dec(vcpu);
766 }
767
768 return 0;
769}
770
771static void get_sregs_arch206(struct kvm_vcpu *vcpu,
772 struct kvm_sregs *sregs)
773{
774 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
775
Scott Wood841741f2011-09-02 17:39:37 -0500776 sregs->u.e.pir = vcpu->vcpu_id;
Scott Wood5ce941e2011-04-27 17:24:21 -0500777 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
778 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
779 sregs->u.e.decar = vcpu->arch.decar;
780 sregs->u.e.ivpr = vcpu->arch.ivpr;
781}
782
783static int set_sregs_arch206(struct kvm_vcpu *vcpu,
784 struct kvm_sregs *sregs)
785{
786 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
787 return 0;
788
Scott Wood841741f2011-09-02 17:39:37 -0500789 if (sregs->u.e.pir != vcpu->vcpu_id)
Scott Wood5ce941e2011-04-27 17:24:21 -0500790 return -EINVAL;
791
792 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
793 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
794 vcpu->arch.decar = sregs->u.e.decar;
795 vcpu->arch.ivpr = sregs->u.e.ivpr;
796
797 return 0;
798}
799
800void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
801{
802 sregs->u.e.features |= KVM_SREGS_E_IVOR;
803
804 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
805 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
806 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
807 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
808 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
809 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
810 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
811 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
812 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
813 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
814 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
815 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
816 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
817 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
818 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
819 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
820}
821
822int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
823{
824 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
825 return 0;
826
827 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
828 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
829 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
830 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
831 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
832 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
833 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
834 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
835 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
836 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
837 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
838 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
839 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
840 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
841 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
842 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
843
844 return 0;
845}
846
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500847int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
848 struct kvm_sregs *sregs)
849{
Scott Wood5ce941e2011-04-27 17:24:21 -0500850 sregs->pvr = vcpu->arch.pvr;
851
852 get_sregs_base(vcpu, sregs);
853 get_sregs_arch206(vcpu, sregs);
854 kvmppc_core_get_sregs(vcpu, sregs);
855 return 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500856}
857
858int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
859 struct kvm_sregs *sregs)
860{
Scott Wood5ce941e2011-04-27 17:24:21 -0500861 int ret;
862
863 if (vcpu->arch.pvr != sregs->pvr)
864 return -EINVAL;
865
866 ret = set_sregs_base(vcpu, sregs);
867 if (ret < 0)
868 return ret;
869
870 ret = set_sregs_arch206(vcpu, sregs);
871 if (ret < 0)
872 return ret;
873
874 return kvmppc_core_set_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500875}
876
877int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
878{
879 return -ENOTSUPP;
880}
881
882int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
883{
884 return -ENOTSUPP;
885}
886
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500887int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
888 struct kvm_translation *tr)
889{
Avi Kivity98001d82010-05-13 11:05:49 +0300890 int r;
891
Avi Kivity98001d82010-05-13 11:05:49 +0300892 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +0300893 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500894}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600895
Alexander Graf4e755752009-10-30 05:47:01 +0000896int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
897{
898 return -ENOTSUPP;
899}
900
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000901int kvmppc_core_prepare_memory_region(struct kvm *kvm,
902 struct kvm_userspace_memory_region *mem)
903{
904 return 0;
905}
906
907void kvmppc_core_commit_memory_region(struct kvm *kvm,
908 struct kvm_userspace_memory_region *mem)
909{
910}
911
912int kvmppc_core_init_vm(struct kvm *kvm)
913{
914 return 0;
915}
916
917void kvmppc_core_destroy_vm(struct kvm *kvm)
918{
919}
920
Stephen Rothwell2986b8c2009-06-02 11:46:14 +1000921int __init kvmppc_booke_init(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600922{
923 unsigned long ivor[16];
924 unsigned long max_ivor = 0;
925 int i;
926
927 /* We install our own exception handlers by hijacking IVPR. IVPR must
928 * be 16-bit aligned, so we need a 64KB allocation. */
929 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
930 VCPU_SIZE_ORDER);
931 if (!kvmppc_booke_handlers)
932 return -ENOMEM;
933
934 /* XXX make sure our handlers are smaller than Linux's */
935
936 /* Copy our interrupt handlers to match host IVORs. That way we don't
937 * have to swap the IVORs on every guest/host transition. */
938 ivor[0] = mfspr(SPRN_IVOR0);
939 ivor[1] = mfspr(SPRN_IVOR1);
940 ivor[2] = mfspr(SPRN_IVOR2);
941 ivor[3] = mfspr(SPRN_IVOR3);
942 ivor[4] = mfspr(SPRN_IVOR4);
943 ivor[5] = mfspr(SPRN_IVOR5);
944 ivor[6] = mfspr(SPRN_IVOR6);
945 ivor[7] = mfspr(SPRN_IVOR7);
946 ivor[8] = mfspr(SPRN_IVOR8);
947 ivor[9] = mfspr(SPRN_IVOR9);
948 ivor[10] = mfspr(SPRN_IVOR10);
949 ivor[11] = mfspr(SPRN_IVOR11);
950 ivor[12] = mfspr(SPRN_IVOR12);
951 ivor[13] = mfspr(SPRN_IVOR13);
952 ivor[14] = mfspr(SPRN_IVOR14);
953 ivor[15] = mfspr(SPRN_IVOR15);
954
955 for (i = 0; i < 16; i++) {
956 if (ivor[i] > max_ivor)
957 max_ivor = ivor[i];
958
959 memcpy((void *)kvmppc_booke_handlers + ivor[i],
960 kvmppc_handlers_start + i * kvmppc_handler_len,
961 kvmppc_handler_len);
962 }
963 flush_icache_range(kvmppc_booke_handlers,
964 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
965
Hollis Blancharddb93f572008-11-05 09:36:18 -0600966 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600967}
968
Hollis Blancharddb93f572008-11-05 09:36:18 -0600969void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600970{
971 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
972 kvm_exit();
973}