blob: 0ecbecb2f7cc83baba524ae782efd833c664cf84 [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
116/* Helper function for "full" MSR writes. No need to call this if only EE is
117 * changing. */
118void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
119{
120 if ((new_msr & MSR_PR) != (vcpu->arch.shared->msr & MSR_PR))
121 kvmppc_mmu_priv_switch(vcpu, new_msr & MSR_PR);
122
123 vcpu->arch.shared->msr = new_msr;
124
125 if (vcpu->arch.shared->msr & MSR_WE) {
126 kvm_vcpu_block(vcpu);
127 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
128 };
129
130 kvmppc_vcpu_sync_spe(vcpu);
131}
132
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600133static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
134 unsigned int priority)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600135{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600136 set_bit(priority, &vcpu->arch.pending_exceptions);
137}
138
Liu Yudaf5e272010-02-02 19:44:35 +0800139static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
140 ulong dear_flags, ulong esr_flags)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600141{
Liu Yudaf5e272010-02-02 19:44:35 +0800142 vcpu->arch.queued_dear = dear_flags;
143 vcpu->arch.queued_esr = esr_flags;
144 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
145}
146
147static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
148 ulong dear_flags, ulong esr_flags)
149{
150 vcpu->arch.queued_dear = dear_flags;
151 vcpu->arch.queued_esr = esr_flags;
152 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
153}
154
155static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
156 ulong esr_flags)
157{
158 vcpu->arch.queued_esr = esr_flags;
159 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
160}
161
162void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
163{
164 vcpu->arch.queued_esr = esr_flags;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600165 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600166}
167
168void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
169{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600170 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600171}
172
173int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
174{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600175 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600176}
177
Alexander Graf7706664d2009-12-21 20:21:24 +0100178void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
179{
180 clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
181}
182
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600183void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
184 struct kvm_interrupt *irq)
185{
Alexander Grafc5335f12010-08-30 14:03:24 +0200186 unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
187
188 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
189 prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
190
191 kvmppc_booke_queue_irqprio(vcpu, prio);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600192}
193
Alexander Graf4496f972010-04-07 10:03:25 +0200194void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
195 struct kvm_interrupt *irq)
196{
197 clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
Alexander Grafc5335f12010-08-30 14:03:24 +0200198 clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
Alexander Graf4496f972010-04-07 10:03:25 +0200199}
200
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600201/* Deliver the interrupt of the corresponding priority, if possible. */
202static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
203 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500204{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600205 int allowed = 0;
Asias He6045be52010-06-19 16:52:12 +0800206 ulong uninitialized_var(msr_mask);
Liu Yudaf5e272010-02-02 19:44:35 +0800207 bool update_esr = false, update_dear = false;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200208 ulong crit_raw = vcpu->arch.shared->critical;
209 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
210 bool crit;
Alexander Grafc5335f12010-08-30 14:03:24 +0200211 bool keep_irq = false;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200212
213 /* Truncate crit indicators in 32 bit mode */
214 if (!(vcpu->arch.shared->msr & MSR_SF)) {
215 crit_raw &= 0xffffffff;
216 crit_r1 &= 0xffffffff;
217 }
218
219 /* Critical section when crit == r1 */
220 crit = (crit_raw == crit_r1);
221 /* ... and we're in supervisor mode */
222 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500223
Alexander Grafc5335f12010-08-30 14:03:24 +0200224 if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
225 priority = BOOKE_IRQPRIO_EXTERNAL;
226 keep_irq = true;
227 }
228
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600229 switch (priority) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600230 case BOOKE_IRQPRIO_DTLB_MISS:
Liu Yudaf5e272010-02-02 19:44:35 +0800231 case BOOKE_IRQPRIO_DATA_STORAGE:
232 update_dear = true;
233 /* fall through */
234 case BOOKE_IRQPRIO_INST_STORAGE:
235 case BOOKE_IRQPRIO_PROGRAM:
236 update_esr = true;
237 /* fall through */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600238 case BOOKE_IRQPRIO_ITLB_MISS:
239 case BOOKE_IRQPRIO_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600240 case BOOKE_IRQPRIO_FP_UNAVAIL:
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600241 case BOOKE_IRQPRIO_SPE_UNAVAIL:
242 case BOOKE_IRQPRIO_SPE_FP_DATA:
243 case BOOKE_IRQPRIO_SPE_FP_ROUND:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600244 case BOOKE_IRQPRIO_AP_UNAVAIL:
245 case BOOKE_IRQPRIO_ALIGNMENT:
246 allowed = 1;
247 msr_mask = MSR_CE|MSR_ME|MSR_DE;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500248 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600249 case BOOKE_IRQPRIO_CRITICAL:
250 case BOOKE_IRQPRIO_WATCHDOG:
Alexander Graf666e7252010-07-29 14:47:43 +0200251 allowed = vcpu->arch.shared->msr & MSR_CE;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600252 msr_mask = MSR_ME;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500253 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600254 case BOOKE_IRQPRIO_MACHINE_CHECK:
Alexander Graf666e7252010-07-29 14:47:43 +0200255 allowed = vcpu->arch.shared->msr & MSR_ME;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600256 msr_mask = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500257 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600258 case BOOKE_IRQPRIO_EXTERNAL:
259 case BOOKE_IRQPRIO_DECREMENTER:
260 case BOOKE_IRQPRIO_FIT:
Alexander Graf666e7252010-07-29 14:47:43 +0200261 allowed = vcpu->arch.shared->msr & MSR_EE;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200262 allowed = allowed && !crit;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600263 msr_mask = MSR_CE|MSR_ME|MSR_DE;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500264 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600265 case BOOKE_IRQPRIO_DEBUG:
Alexander Graf666e7252010-07-29 14:47:43 +0200266 allowed = vcpu->arch.shared->msr & MSR_DE;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600267 msr_mask = MSR_ME;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500268 break;
269 }
270
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600271 if (allowed) {
Alexander Grafde7906c2010-07-29 14:47:46 +0200272 vcpu->arch.shared->srr0 = vcpu->arch.pc;
273 vcpu->arch.shared->srr1 = vcpu->arch.shared->msr;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600274 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
Liu Yudaf5e272010-02-02 19:44:35 +0800275 if (update_esr == true)
276 vcpu->arch.esr = vcpu->arch.queued_esr;
277 if (update_dear == true)
Alexander Graf5e030182010-07-29 14:47:45 +0200278 vcpu->arch.shared->dar = vcpu->arch.queued_dear;
Alexander Graf666e7252010-07-29 14:47:43 +0200279 kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600280
Alexander Grafc5335f12010-08-30 14:03:24 +0200281 if (!keep_irq)
282 clear_bit(priority, &vcpu->arch.pending_exceptions);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600283 }
284
285 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500286}
287
288/* Check pending exceptions and deliver one, if possible. */
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600289void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500290{
291 unsigned long *pending = &vcpu->arch.pending_exceptions;
Alexander Graf90bba352010-07-29 14:47:51 +0200292 unsigned long old_pending = vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500293 unsigned int priority;
294
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600295 priority = __ffs(*pending);
Hollis Blanchardbdc89f12009-01-03 16:23:12 -0600296 while (priority <= BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600297 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500298 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500299
300 priority = find_next_bit(pending,
301 BITS_PER_BYTE * sizeof(*pending),
302 priority + 1);
303 }
Alexander Graf90bba352010-07-29 14:47:51 +0200304
305 /* Tell the guest about our interrupt status */
306 if (*pending)
307 vcpu->arch.shared->int_pending = 1;
308 else if (old_pending)
309 vcpu->arch.shared->int_pending = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500310}
311
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500312/**
313 * kvmppc_handle_exit
314 *
315 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
316 */
317int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
318 unsigned int exit_nr)
319{
320 enum emulation_result er;
321 int r = RESUME_HOST;
322
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600323 /* update before a new last_exit_type is rewritten */
324 kvmppc_update_timing_stats(vcpu);
325
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500326 local_irq_enable();
327
328 run->exit_reason = KVM_EXIT_UNKNOWN;
329 run->ready_for_interrupt_injection = 1;
330
331 switch (exit_nr) {
332 case BOOKE_INTERRUPT_MACHINE_CHECK:
333 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
334 kvmppc_dump_vcpu(vcpu);
335 r = RESUME_HOST;
336 break;
337
338 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600339 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -0600340 if (need_resched())
341 cond_resched();
342 r = RESUME_GUEST;
343 break;
344
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500345 case BOOKE_INTERRUPT_DECREMENTER:
346 /* Since we switched IVPR back to the host's value, the host
347 * handled this interrupt the moment we enabled interrupts.
348 * Now we just offer it a chance to reschedule the guest. */
Hollis Blanchard7b701592008-12-02 15:51:58 -0600349 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500350 if (need_resched())
351 cond_resched();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500352 r = RESUME_GUEST;
353 break;
354
355 case BOOKE_INTERRUPT_PROGRAM:
Alexander Graf666e7252010-07-29 14:47:43 +0200356 if (vcpu->arch.shared->msr & MSR_PR) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500357 /* Program traps generated by user-level software must be handled
358 * by the guest kernel. */
Liu Yudaf5e272010-02-02 19:44:35 +0800359 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500360 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600361 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500362 break;
363 }
364
365 er = kvmppc_emulate_instruction(run, vcpu);
366 switch (er) {
367 case EMULATE_DONE:
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600368 /* don't overwrite subtypes, just account kvm_stats */
Hollis Blanchard7b701592008-12-02 15:51:58 -0600369 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500370 /* Future optimization: only reload non-volatiles if
371 * they were actually modified by emulation. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500372 r = RESUME_GUEST_NV;
373 break;
374 case EMULATE_DO_DCR:
375 run->exit_reason = KVM_EXIT_DCR;
376 r = RESUME_HOST;
377 break;
378 case EMULATE_FAIL:
379 /* XXX Deliver Program interrupt to guest. */
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -0600380 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500381 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
382 /* For debugging, encode the failing instruction and
383 * report it to userspace. */
384 run->hw.hardware_exit_reason = ~0ULL << 32;
385 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
386 r = RESUME_HOST;
387 break;
388 default:
389 BUG();
390 }
391 break;
392
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200393 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600394 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600395 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200396 r = RESUME_GUEST;
397 break;
398
Scott Wood4cd35f62011-06-14 18:34:31 -0500399#ifdef CONFIG_SPE
400 case BOOKE_INTERRUPT_SPE_UNAVAIL: {
401 if (vcpu->arch.shared->msr & MSR_SPE)
402 kvmppc_vcpu_enable_spe(vcpu);
403 else
404 kvmppc_booke_queue_irqprio(vcpu,
405 BOOKE_IRQPRIO_SPE_UNAVAIL);
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600406 r = RESUME_GUEST;
407 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500408 }
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600409
410 case BOOKE_INTERRUPT_SPE_FP_DATA:
411 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
412 r = RESUME_GUEST;
413 break;
414
415 case BOOKE_INTERRUPT_SPE_FP_ROUND:
416 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
417 r = RESUME_GUEST;
418 break;
Scott Wood4cd35f62011-06-14 18:34:31 -0500419#else
420 case BOOKE_INTERRUPT_SPE_UNAVAIL:
421 /*
422 * Guest wants SPE, but host kernel doesn't support it. Send
423 * an "unimplemented operation" program check to the guest.
424 */
425 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
426 r = RESUME_GUEST;
427 break;
428
429 /*
430 * These really should never happen without CONFIG_SPE,
431 * as we should never enable the real MSR[SPE] in the guest.
432 */
433 case BOOKE_INTERRUPT_SPE_FP_DATA:
434 case BOOKE_INTERRUPT_SPE_FP_ROUND:
435 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
436 __func__, exit_nr, vcpu->arch.pc);
437 run->hw.hardware_exit_reason = exit_nr;
438 r = RESUME_HOST;
439 break;
440#endif
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600441
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500442 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800443 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
444 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600445 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500446 r = RESUME_GUEST;
447 break;
448
449 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800450 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600451 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500452 r = RESUME_GUEST;
453 break;
454
455 case BOOKE_INTERRUPT_SYSCALL:
Alexander Graf2a342ed2010-07-29 14:47:48 +0200456 if (!(vcpu->arch.shared->msr & MSR_PR) &&
457 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
458 /* KVM PV hypercalls */
459 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
460 r = RESUME_GUEST;
461 } else {
462 /* Guest syscalls */
463 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
464 }
Hollis Blanchard7b701592008-12-02 15:51:58 -0600465 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500466 r = RESUME_GUEST;
467 break;
468
469 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500470 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600471 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600472 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500473 gfn_t gfn;
474
475 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600476 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600477 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500478 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +0800479 kvmppc_core_queue_dtlb_miss(vcpu,
480 vcpu->arch.fault_dear,
481 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600482 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600483 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500484 r = RESUME_GUEST;
485 break;
486 }
487
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600488 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600489 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500490
491 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
492 /* The guest TLB had a mapping, but the shadow TLB
493 * didn't, and it is RAM. This could be because:
494 * a) the entry is mapping the host kernel, or
495 * b) the guest used a large mapping which we're faking
496 * Either way, we need to satisfy the fault without
497 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600498 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600499 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500500 r = RESUME_GUEST;
501 } else {
502 /* Guest has mapped and accessed a page which is not
503 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600504 vcpu->arch.paddr_accessed = gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500505 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600506 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500507 }
508
509 break;
510 }
511
512 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500513 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -0600514 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500515 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600516 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500517
518 r = RESUME_GUEST;
519
520 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600521 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600522 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500523 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600524 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600525 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600526 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500527 break;
528 }
529
Hollis Blanchard7b701592008-12-02 15:51:58 -0600530 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500531
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600532 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -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. 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 Blanchardbbf45ba2008-04-16 23:28:09 -0500543 } else {
544 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600545 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500546 }
547
548 break;
549 }
550
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500551 case BOOKE_INTERRUPT_DEBUG: {
552 u32 dbsr;
553
554 vcpu->arch.pc = mfspr(SPRN_CSRR0);
555
556 /* clear IAC events in DBSR register */
557 dbsr = mfspr(SPRN_DBSR);
558 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
559 mtspr(SPRN_DBSR, dbsr);
560
561 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600562 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500563 r = RESUME_HOST;
564 break;
565 }
566
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500567 default:
568 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
569 BUG();
570 }
571
572 local_irq_disable();
573
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600574 kvmppc_core_deliver_interrupts(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500575
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500576 if (!(r & RESUME_HOST)) {
577 /* To avoid clobbering exit_reason, only check for signals if
578 * we aren't already exiting to userspace for some other
579 * reason. */
580 if (signal_pending(current)) {
581 run->exit_reason = KVM_EXIT_INTR;
582 r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600583 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500584 }
585 }
586
587 return r;
588}
589
590/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
591int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
592{
Hollis Blanchard082decf2010-08-07 10:33:56 -0700593 int i;
594
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500595 vcpu->arch.pc = 0;
Alexander Graf666e7252010-07-29 14:47:43 +0200596 vcpu->arch.shared->msr = 0;
Scott Woodecee2732011-06-14 18:34:29 -0500597 vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100598 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500599
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500600 vcpu->arch.shadow_pid = 1;
601
Hollis Blanchard082decf2010-08-07 10:33:56 -0700602 /* Eye-catching numbers so we know if the guest takes an interrupt
603 * before it's programmed its own IVPR/IVORs. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500604 vcpu->arch.ivpr = 0x55550000;
Hollis Blanchard082decf2010-08-07 10:33:56 -0700605 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
606 vcpu->arch.ivor[i] = 0x7700 | i * 4;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500607
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600608 kvmppc_init_timing_stats(vcpu);
609
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600610 return kvmppc_core_vcpu_setup(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500611}
612
613int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
614{
615 int i;
616
617 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100618 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500619 regs->ctr = vcpu->arch.ctr;
620 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100621 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +0200622 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +0200623 regs->srr0 = vcpu->arch.shared->srr0;
624 regs->srr1 = vcpu->arch.shared->srr1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500625 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +0200626 regs->sprg0 = vcpu->arch.shared->sprg0;
627 regs->sprg1 = vcpu->arch.shared->sprg1;
628 regs->sprg2 = vcpu->arch.shared->sprg2;
629 regs->sprg3 = vcpu->arch.shared->sprg3;
Peter Tyserbc9c1932010-12-29 13:51:25 -0600630 regs->sprg4 = vcpu->arch.sprg4;
631 regs->sprg5 = vcpu->arch.sprg5;
632 regs->sprg6 = vcpu->arch.sprg6;
633 regs->sprg7 = vcpu->arch.sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500634
635 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100636 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500637
638 return 0;
639}
640
641int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
642{
643 int i;
644
645 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100646 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500647 vcpu->arch.ctr = regs->ctr;
648 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100649 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -0600650 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +0200651 vcpu->arch.shared->srr0 = regs->srr0;
652 vcpu->arch.shared->srr1 = regs->srr1;
Scott Wood5ce941e2011-04-27 17:24:21 -0500653 kvmppc_set_pid(vcpu, regs->pid);
Alexander Grafa73a9592010-07-29 14:47:47 +0200654 vcpu->arch.shared->sprg0 = regs->sprg0;
655 vcpu->arch.shared->sprg1 = regs->sprg1;
656 vcpu->arch.shared->sprg2 = regs->sprg2;
657 vcpu->arch.shared->sprg3 = regs->sprg3;
Peter Tyserbc9c1932010-12-29 13:51:25 -0600658 vcpu->arch.sprg4 = regs->sprg4;
659 vcpu->arch.sprg5 = regs->sprg5;
660 vcpu->arch.sprg6 = regs->sprg6;
661 vcpu->arch.sprg7 = regs->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500662
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100663 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
664 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500665
666 return 0;
667}
668
Scott Wood5ce941e2011-04-27 17:24:21 -0500669static void get_sregs_base(struct kvm_vcpu *vcpu,
670 struct kvm_sregs *sregs)
671{
672 u64 tb = get_tb();
673
674 sregs->u.e.features |= KVM_SREGS_E_BASE;
675
676 sregs->u.e.csrr0 = vcpu->arch.csrr0;
677 sregs->u.e.csrr1 = vcpu->arch.csrr1;
678 sregs->u.e.mcsr = vcpu->arch.mcsr;
679 sregs->u.e.esr = vcpu->arch.esr;
680 sregs->u.e.dear = vcpu->arch.shared->dar;
681 sregs->u.e.tsr = vcpu->arch.tsr;
682 sregs->u.e.tcr = vcpu->arch.tcr;
683 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
684 sregs->u.e.tb = tb;
685 sregs->u.e.vrsave = vcpu->arch.vrsave;
686}
687
688static int set_sregs_base(struct kvm_vcpu *vcpu,
689 struct kvm_sregs *sregs)
690{
691 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
692 return 0;
693
694 vcpu->arch.csrr0 = sregs->u.e.csrr0;
695 vcpu->arch.csrr1 = sregs->u.e.csrr1;
696 vcpu->arch.mcsr = sregs->u.e.mcsr;
697 vcpu->arch.esr = sregs->u.e.esr;
698 vcpu->arch.shared->dar = sregs->u.e.dear;
699 vcpu->arch.vrsave = sregs->u.e.vrsave;
700 vcpu->arch.tcr = sregs->u.e.tcr;
701
702 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC)
703 vcpu->arch.dec = sregs->u.e.dec;
704
705 kvmppc_emulate_dec(vcpu);
706
707 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
708 /*
709 * FIXME: existing KVM timer handling is incomplete.
710 * TSR cannot be read by the guest, and its value in
711 * vcpu->arch is always zero. For now, just handle
712 * the case where the caller is trying to inject a
713 * decrementer interrupt.
714 */
715
716 if ((sregs->u.e.tsr & TSR_DIS) &&
717 (vcpu->arch.tcr & TCR_DIE))
718 kvmppc_core_queue_dec(vcpu);
719 }
720
721 return 0;
722}
723
724static void get_sregs_arch206(struct kvm_vcpu *vcpu,
725 struct kvm_sregs *sregs)
726{
727 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
728
729 sregs->u.e.pir = 0;
730 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
731 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
732 sregs->u.e.decar = vcpu->arch.decar;
733 sregs->u.e.ivpr = vcpu->arch.ivpr;
734}
735
736static int set_sregs_arch206(struct kvm_vcpu *vcpu,
737 struct kvm_sregs *sregs)
738{
739 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
740 return 0;
741
742 if (sregs->u.e.pir != 0)
743 return -EINVAL;
744
745 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
746 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
747 vcpu->arch.decar = sregs->u.e.decar;
748 vcpu->arch.ivpr = sregs->u.e.ivpr;
749
750 return 0;
751}
752
753void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
754{
755 sregs->u.e.features |= KVM_SREGS_E_IVOR;
756
757 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
758 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
759 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
760 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
761 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
762 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
763 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
764 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
765 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
766 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
767 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
768 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
769 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
770 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
771 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
772 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
773}
774
775int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
776{
777 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
778 return 0;
779
780 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
781 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
782 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
783 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
784 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
785 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
786 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
787 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
788 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
789 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
790 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
791 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
792 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
793 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
794 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
795 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
796
797 return 0;
798}
799
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500800int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
801 struct kvm_sregs *sregs)
802{
Scott Wood5ce941e2011-04-27 17:24:21 -0500803 sregs->pvr = vcpu->arch.pvr;
804
805 get_sregs_base(vcpu, sregs);
806 get_sregs_arch206(vcpu, sregs);
807 kvmppc_core_get_sregs(vcpu, sregs);
808 return 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500809}
810
811int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
812 struct kvm_sregs *sregs)
813{
Scott Wood5ce941e2011-04-27 17:24:21 -0500814 int ret;
815
816 if (vcpu->arch.pvr != sregs->pvr)
817 return -EINVAL;
818
819 ret = set_sregs_base(vcpu, sregs);
820 if (ret < 0)
821 return ret;
822
823 ret = set_sregs_arch206(vcpu, sregs);
824 if (ret < 0)
825 return ret;
826
827 return kvmppc_core_set_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500828}
829
830int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
831{
832 return -ENOTSUPP;
833}
834
835int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
836{
837 return -ENOTSUPP;
838}
839
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500840int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
841 struct kvm_translation *tr)
842{
Avi Kivity98001d82010-05-13 11:05:49 +0300843 int r;
844
Avi Kivity98001d82010-05-13 11:05:49 +0300845 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +0300846 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500847}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600848
Alexander Graf4e755752009-10-30 05:47:01 +0000849int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
850{
851 return -ENOTSUPP;
852}
853
Stephen Rothwell2986b8c2009-06-02 11:46:14 +1000854int __init kvmppc_booke_init(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600855{
856 unsigned long ivor[16];
857 unsigned long max_ivor = 0;
858 int i;
859
860 /* We install our own exception handlers by hijacking IVPR. IVPR must
861 * be 16-bit aligned, so we need a 64KB allocation. */
862 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
863 VCPU_SIZE_ORDER);
864 if (!kvmppc_booke_handlers)
865 return -ENOMEM;
866
867 /* XXX make sure our handlers are smaller than Linux's */
868
869 /* Copy our interrupt handlers to match host IVORs. That way we don't
870 * have to swap the IVORs on every guest/host transition. */
871 ivor[0] = mfspr(SPRN_IVOR0);
872 ivor[1] = mfspr(SPRN_IVOR1);
873 ivor[2] = mfspr(SPRN_IVOR2);
874 ivor[3] = mfspr(SPRN_IVOR3);
875 ivor[4] = mfspr(SPRN_IVOR4);
876 ivor[5] = mfspr(SPRN_IVOR5);
877 ivor[6] = mfspr(SPRN_IVOR6);
878 ivor[7] = mfspr(SPRN_IVOR7);
879 ivor[8] = mfspr(SPRN_IVOR8);
880 ivor[9] = mfspr(SPRN_IVOR9);
881 ivor[10] = mfspr(SPRN_IVOR10);
882 ivor[11] = mfspr(SPRN_IVOR11);
883 ivor[12] = mfspr(SPRN_IVOR12);
884 ivor[13] = mfspr(SPRN_IVOR13);
885 ivor[14] = mfspr(SPRN_IVOR14);
886 ivor[15] = mfspr(SPRN_IVOR15);
887
888 for (i = 0; i < 16; i++) {
889 if (ivor[i] > max_ivor)
890 max_ivor = ivor[i];
891
892 memcpy((void *)kvmppc_booke_handlers + ivor[i],
893 kvmppc_handlers_start + i * kvmppc_handler_len,
894 kvmppc_handler_len);
895 }
896 flush_icache_range(kvmppc_booke_handlers,
897 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
898
Hollis Blancharddb93f572008-11-05 09:36:18 -0600899 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600900}
901
Hollis Blancharddb93f572008-11-05 09:36:18 -0600902void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600903{
904 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
905 kvm_exit();
906}