blob: ef76acb455c310bb441326d59e2cfe2527fe82e6 [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
16 *
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
19 */
20
21#include <linux/errno.h>
22#include <linux/err.h>
23#include <linux/kvm_host.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/gfp.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050025#include <linux/module.h>
26#include <linux/vmalloc.h>
27#include <linux/fs.h>
Hollis Blanchard7924bd42008-12-02 15:51:55 -060028
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050029#include <asm/cputable.h>
30#include <asm/uaccess.h>
31#include <asm/kvm_ppc.h>
Hollis Blanchard73e75b42008-12-02 15:51:57 -060032#include "timing.h"
Hollis Blanchardd9fbd032008-11-05 09:36:13 -060033#include <asm/cacheflush.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050034
Hollis Blanchard75f74f02008-11-05 09:36:16 -060035#include "booke.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050036
Hollis Blanchardd9fbd032008-11-05 09:36:13 -060037unsigned long kvmppc_booke_handlers;
38
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050039#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
40#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
41
42struct kvm_stats_debugfs_item debugfs_entries[] = {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050043 { "mmio", VCPU_STAT(mmio_exits) },
44 { "dcr", VCPU_STAT(dcr_exits) },
45 { "sig", VCPU_STAT(signal_exits) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050046 { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
47 { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
48 { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
49 { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
50 { "sysc", VCPU_STAT(syscall_exits) },
51 { "isi", VCPU_STAT(isi_exits) },
52 { "dsi", VCPU_STAT(dsi_exits) },
53 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
54 { "dec", VCPU_STAT(dec_exits) },
55 { "ext_intr", VCPU_STAT(ext_intr_exits) },
Hollis Blanchard45c5eb62008-04-25 17:55:49 -050056 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050057 { NULL }
58};
59
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050060/* TODO: use vcpu_printf() */
61void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
62{
63 int i;
64
Alexander Graf666e7252010-07-29 14:47:43 +020065 printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060066 printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
Alexander Grafde7906c2010-07-29 14:47:46 +020067 printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
68 vcpu->arch.shared->srr1);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050069
70 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
71
72 for (i = 0; i < 32; i += 4) {
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060073 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
Alexander Graf8e5b26b2010-01-08 02:58:01 +010074 kvmppc_get_gpr(vcpu, i),
75 kvmppc_get_gpr(vcpu, i+1),
76 kvmppc_get_gpr(vcpu, i+2),
77 kvmppc_get_gpr(vcpu, i+3));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050078 }
79}
80
Hollis Blanchardd4cf3892008-11-05 09:36:23 -060081static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
82 unsigned int priority)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060083{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060084 set_bit(priority, &vcpu->arch.pending_exceptions);
85}
86
Liu Yudaf5e272010-02-02 19:44:35 +080087static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
88 ulong dear_flags, ulong esr_flags)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060089{
Liu Yudaf5e272010-02-02 19:44:35 +080090 vcpu->arch.queued_dear = dear_flags;
91 vcpu->arch.queued_esr = esr_flags;
92 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
93}
94
95static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
96 ulong dear_flags, ulong esr_flags)
97{
98 vcpu->arch.queued_dear = dear_flags;
99 vcpu->arch.queued_esr = esr_flags;
100 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
101}
102
103static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
104 ulong esr_flags)
105{
106 vcpu->arch.queued_esr = esr_flags;
107 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
108}
109
110void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
111{
112 vcpu->arch.queued_esr = esr_flags;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600113 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600114}
115
116void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
117{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600118 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600119}
120
121int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
122{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600123 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600124}
125
Alexander Graf7706664d2009-12-21 20:21:24 +0100126void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
127{
128 clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
129}
130
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600131void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
132 struct kvm_interrupt *irq)
133{
Alexander Grafc5335f12010-08-30 14:03:24 +0200134 unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
135
136 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
137 prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
138
139 kvmppc_booke_queue_irqprio(vcpu, prio);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600140}
141
Alexander Graf4496f972010-04-07 10:03:25 +0200142void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
143 struct kvm_interrupt *irq)
144{
145 clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
Alexander Grafc5335f12010-08-30 14:03:24 +0200146 clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
Alexander Graf4496f972010-04-07 10:03:25 +0200147}
148
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600149/* Deliver the interrupt of the corresponding priority, if possible. */
150static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
151 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500152{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600153 int allowed = 0;
Asias He6045be52010-06-19 16:52:12 +0800154 ulong uninitialized_var(msr_mask);
Liu Yudaf5e272010-02-02 19:44:35 +0800155 bool update_esr = false, update_dear = false;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200156 ulong crit_raw = vcpu->arch.shared->critical;
157 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
158 bool crit;
Alexander Grafc5335f12010-08-30 14:03:24 +0200159 bool keep_irq = false;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200160
161 /* Truncate crit indicators in 32 bit mode */
162 if (!(vcpu->arch.shared->msr & MSR_SF)) {
163 crit_raw &= 0xffffffff;
164 crit_r1 &= 0xffffffff;
165 }
166
167 /* Critical section when crit == r1 */
168 crit = (crit_raw == crit_r1);
169 /* ... and we're in supervisor mode */
170 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500171
Alexander Grafc5335f12010-08-30 14:03:24 +0200172 if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
173 priority = BOOKE_IRQPRIO_EXTERNAL;
174 keep_irq = true;
175 }
176
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600177 switch (priority) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600178 case BOOKE_IRQPRIO_DTLB_MISS:
Liu Yudaf5e272010-02-02 19:44:35 +0800179 case BOOKE_IRQPRIO_DATA_STORAGE:
180 update_dear = true;
181 /* fall through */
182 case BOOKE_IRQPRIO_INST_STORAGE:
183 case BOOKE_IRQPRIO_PROGRAM:
184 update_esr = true;
185 /* fall through */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600186 case BOOKE_IRQPRIO_ITLB_MISS:
187 case BOOKE_IRQPRIO_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600188 case BOOKE_IRQPRIO_FP_UNAVAIL:
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600189 case BOOKE_IRQPRIO_SPE_UNAVAIL:
190 case BOOKE_IRQPRIO_SPE_FP_DATA:
191 case BOOKE_IRQPRIO_SPE_FP_ROUND:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600192 case BOOKE_IRQPRIO_AP_UNAVAIL:
193 case BOOKE_IRQPRIO_ALIGNMENT:
194 allowed = 1;
195 msr_mask = MSR_CE|MSR_ME|MSR_DE;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500196 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600197 case BOOKE_IRQPRIO_CRITICAL:
198 case BOOKE_IRQPRIO_WATCHDOG:
Alexander Graf666e7252010-07-29 14:47:43 +0200199 allowed = vcpu->arch.shared->msr & MSR_CE;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600200 msr_mask = MSR_ME;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500201 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600202 case BOOKE_IRQPRIO_MACHINE_CHECK:
Alexander Graf666e7252010-07-29 14:47:43 +0200203 allowed = vcpu->arch.shared->msr & MSR_ME;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600204 msr_mask = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500205 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600206 case BOOKE_IRQPRIO_EXTERNAL:
207 case BOOKE_IRQPRIO_DECREMENTER:
208 case BOOKE_IRQPRIO_FIT:
Alexander Graf666e7252010-07-29 14:47:43 +0200209 allowed = vcpu->arch.shared->msr & MSR_EE;
Alexander Graf5c6cedf2010-07-29 14:47:49 +0200210 allowed = allowed && !crit;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600211 msr_mask = MSR_CE|MSR_ME|MSR_DE;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500212 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600213 case BOOKE_IRQPRIO_DEBUG:
Alexander Graf666e7252010-07-29 14:47:43 +0200214 allowed = vcpu->arch.shared->msr & MSR_DE;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600215 msr_mask = MSR_ME;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500216 break;
217 }
218
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600219 if (allowed) {
Alexander Grafde7906c2010-07-29 14:47:46 +0200220 vcpu->arch.shared->srr0 = vcpu->arch.pc;
221 vcpu->arch.shared->srr1 = vcpu->arch.shared->msr;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600222 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
Liu Yudaf5e272010-02-02 19:44:35 +0800223 if (update_esr == true)
224 vcpu->arch.esr = vcpu->arch.queued_esr;
225 if (update_dear == true)
Alexander Graf5e030182010-07-29 14:47:45 +0200226 vcpu->arch.shared->dar = vcpu->arch.queued_dear;
Alexander Graf666e7252010-07-29 14:47:43 +0200227 kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600228
Alexander Grafc5335f12010-08-30 14:03:24 +0200229 if (!keep_irq)
230 clear_bit(priority, &vcpu->arch.pending_exceptions);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600231 }
232
233 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500234}
235
236/* Check pending exceptions and deliver one, if possible. */
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600237void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500238{
239 unsigned long *pending = &vcpu->arch.pending_exceptions;
Alexander Graf90bba352010-07-29 14:47:51 +0200240 unsigned long old_pending = vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500241 unsigned int priority;
242
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600243 priority = __ffs(*pending);
Hollis Blanchardbdc89f12009-01-03 16:23:12 -0600244 while (priority <= BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600245 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500246 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500247
248 priority = find_next_bit(pending,
249 BITS_PER_BYTE * sizeof(*pending),
250 priority + 1);
251 }
Alexander Graf90bba352010-07-29 14:47:51 +0200252
253 /* Tell the guest about our interrupt status */
254 if (*pending)
255 vcpu->arch.shared->int_pending = 1;
256 else if (old_pending)
257 vcpu->arch.shared->int_pending = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500258}
259
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500260/**
261 * kvmppc_handle_exit
262 *
263 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
264 */
265int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
266 unsigned int exit_nr)
267{
268 enum emulation_result er;
269 int r = RESUME_HOST;
270
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600271 /* update before a new last_exit_type is rewritten */
272 kvmppc_update_timing_stats(vcpu);
273
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500274 local_irq_enable();
275
276 run->exit_reason = KVM_EXIT_UNKNOWN;
277 run->ready_for_interrupt_injection = 1;
278
279 switch (exit_nr) {
280 case BOOKE_INTERRUPT_MACHINE_CHECK:
281 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
282 kvmppc_dump_vcpu(vcpu);
283 r = RESUME_HOST;
284 break;
285
286 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600287 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -0600288 if (need_resched())
289 cond_resched();
290 r = RESUME_GUEST;
291 break;
292
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500293 case BOOKE_INTERRUPT_DECREMENTER:
294 /* Since we switched IVPR back to the host's value, the host
295 * handled this interrupt the moment we enabled interrupts.
296 * Now we just offer it a chance to reschedule the guest. */
Hollis Blanchard7b701592008-12-02 15:51:58 -0600297 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500298 if (need_resched())
299 cond_resched();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500300 r = RESUME_GUEST;
301 break;
302
303 case BOOKE_INTERRUPT_PROGRAM:
Alexander Graf666e7252010-07-29 14:47:43 +0200304 if (vcpu->arch.shared->msr & MSR_PR) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500305 /* Program traps generated by user-level software must be handled
306 * by the guest kernel. */
Liu Yudaf5e272010-02-02 19:44:35 +0800307 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500308 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600309 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500310 break;
311 }
312
313 er = kvmppc_emulate_instruction(run, vcpu);
314 switch (er) {
315 case EMULATE_DONE:
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600316 /* don't overwrite subtypes, just account kvm_stats */
Hollis Blanchard7b701592008-12-02 15:51:58 -0600317 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500318 /* Future optimization: only reload non-volatiles if
319 * they were actually modified by emulation. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500320 r = RESUME_GUEST_NV;
321 break;
322 case EMULATE_DO_DCR:
323 run->exit_reason = KVM_EXIT_DCR;
324 r = RESUME_HOST;
325 break;
326 case EMULATE_FAIL:
327 /* XXX Deliver Program interrupt to guest. */
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -0600328 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500329 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
330 /* For debugging, encode the failing instruction and
331 * report it to userspace. */
332 run->hw.hardware_exit_reason = ~0ULL << 32;
333 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
334 r = RESUME_HOST;
335 break;
336 default:
337 BUG();
338 }
339 break;
340
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200341 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600342 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600343 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200344 r = RESUME_GUEST;
345 break;
346
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600347 case BOOKE_INTERRUPT_SPE_UNAVAIL:
348 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_UNAVAIL);
349 r = RESUME_GUEST;
350 break;
351
352 case BOOKE_INTERRUPT_SPE_FP_DATA:
353 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
354 r = RESUME_GUEST;
355 break;
356
357 case BOOKE_INTERRUPT_SPE_FP_ROUND:
358 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
359 r = RESUME_GUEST;
360 break;
361
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500362 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800363 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
364 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600365 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500366 r = RESUME_GUEST;
367 break;
368
369 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800370 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600371 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500372 r = RESUME_GUEST;
373 break;
374
375 case BOOKE_INTERRUPT_SYSCALL:
Alexander Graf2a342ed2010-07-29 14:47:48 +0200376 if (!(vcpu->arch.shared->msr & MSR_PR) &&
377 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
378 /* KVM PV hypercalls */
379 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
380 r = RESUME_GUEST;
381 } else {
382 /* Guest syscalls */
383 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
384 }
Hollis Blanchard7b701592008-12-02 15:51:58 -0600385 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500386 r = RESUME_GUEST;
387 break;
388
389 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500390 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600391 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600392 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500393 gfn_t gfn;
394
395 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600396 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600397 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500398 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +0800399 kvmppc_core_queue_dtlb_miss(vcpu,
400 vcpu->arch.fault_dear,
401 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600402 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600403 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500404 r = RESUME_GUEST;
405 break;
406 }
407
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600408 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600409 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500410
411 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
412 /* The guest TLB had a mapping, but the shadow TLB
413 * didn't, and it is RAM. This could be because:
414 * a) the entry is mapping the host kernel, or
415 * b) the guest used a large mapping which we're faking
416 * Either way, we need to satisfy the fault without
417 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600418 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600419 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500420 r = RESUME_GUEST;
421 } else {
422 /* Guest has mapped and accessed a page which is not
423 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600424 vcpu->arch.paddr_accessed = gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500425 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600426 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500427 }
428
429 break;
430 }
431
432 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500433 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -0600434 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500435 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600436 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500437
438 r = RESUME_GUEST;
439
440 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600441 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600442 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500443 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600444 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600445 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600446 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500447 break;
448 }
449
Hollis Blanchard7b701592008-12-02 15:51:58 -0600450 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500451
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600452 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -0600453 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500454
455 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
456 /* The guest TLB had a mapping, but the shadow TLB
457 * didn't. This could be because:
458 * a) the entry is mapping the host kernel, or
459 * b) the guest used a large mapping which we're faking
460 * Either way, we need to satisfy the fault without
461 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600462 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500463 } else {
464 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600465 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500466 }
467
468 break;
469 }
470
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500471 case BOOKE_INTERRUPT_DEBUG: {
472 u32 dbsr;
473
474 vcpu->arch.pc = mfspr(SPRN_CSRR0);
475
476 /* clear IAC events in DBSR register */
477 dbsr = mfspr(SPRN_DBSR);
478 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
479 mtspr(SPRN_DBSR, dbsr);
480
481 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600482 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500483 r = RESUME_HOST;
484 break;
485 }
486
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500487 default:
488 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
489 BUG();
490 }
491
492 local_irq_disable();
493
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600494 kvmppc_core_deliver_interrupts(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500495
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500496 if (!(r & RESUME_HOST)) {
497 /* To avoid clobbering exit_reason, only check for signals if
498 * we aren't already exiting to userspace for some other
499 * reason. */
500 if (signal_pending(current)) {
501 run->exit_reason = KVM_EXIT_INTR;
502 r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600503 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500504 }
505 }
506
507 return r;
508}
509
510/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
511int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
512{
Hollis Blanchard082decf2010-08-07 10:33:56 -0700513 int i;
514
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500515 vcpu->arch.pc = 0;
Alexander Graf666e7252010-07-29 14:47:43 +0200516 vcpu->arch.shared->msr = 0;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100517 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500518
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500519 vcpu->arch.shadow_pid = 1;
520
Hollis Blanchard082decf2010-08-07 10:33:56 -0700521 /* Eye-catching numbers so we know if the guest takes an interrupt
522 * before it's programmed its own IVPR/IVORs. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500523 vcpu->arch.ivpr = 0x55550000;
Hollis Blanchard082decf2010-08-07 10:33:56 -0700524 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
525 vcpu->arch.ivor[i] = 0x7700 | i * 4;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500526
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600527 kvmppc_init_timing_stats(vcpu);
528
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600529 return kvmppc_core_vcpu_setup(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500530}
531
532int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
533{
534 int i;
535
536 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100537 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500538 regs->ctr = vcpu->arch.ctr;
539 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100540 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +0200541 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +0200542 regs->srr0 = vcpu->arch.shared->srr0;
543 regs->srr1 = vcpu->arch.shared->srr1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500544 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +0200545 regs->sprg0 = vcpu->arch.shared->sprg0;
546 regs->sprg1 = vcpu->arch.shared->sprg1;
547 regs->sprg2 = vcpu->arch.shared->sprg2;
548 regs->sprg3 = vcpu->arch.shared->sprg3;
Peter Tyserbc9c1932010-12-29 13:51:25 -0600549 regs->sprg4 = vcpu->arch.sprg4;
550 regs->sprg5 = vcpu->arch.sprg5;
551 regs->sprg6 = vcpu->arch.sprg6;
552 regs->sprg7 = vcpu->arch.sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500553
554 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100555 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500556
557 return 0;
558}
559
560int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
561{
562 int i;
563
564 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100565 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500566 vcpu->arch.ctr = regs->ctr;
567 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100568 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -0600569 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +0200570 vcpu->arch.shared->srr0 = regs->srr0;
571 vcpu->arch.shared->srr1 = regs->srr1;
Alexander Grafa73a9592010-07-29 14:47:47 +0200572 vcpu->arch.shared->sprg0 = regs->sprg0;
573 vcpu->arch.shared->sprg1 = regs->sprg1;
574 vcpu->arch.shared->sprg2 = regs->sprg2;
575 vcpu->arch.shared->sprg3 = regs->sprg3;
Peter Tyserbc9c1932010-12-29 13:51:25 -0600576 vcpu->arch.sprg4 = regs->sprg4;
577 vcpu->arch.sprg5 = regs->sprg5;
578 vcpu->arch.sprg6 = regs->sprg6;
579 vcpu->arch.sprg7 = regs->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500580
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100581 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
582 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500583
584 return 0;
585}
586
587int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
588 struct kvm_sregs *sregs)
589{
590 return -ENOTSUPP;
591}
592
593int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
594 struct kvm_sregs *sregs)
595{
596 return -ENOTSUPP;
597}
598
599int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
600{
601 return -ENOTSUPP;
602}
603
604int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
605{
606 return -ENOTSUPP;
607}
608
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500609int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
610 struct kvm_translation *tr)
611{
Avi Kivity98001d82010-05-13 11:05:49 +0300612 int r;
613
Avi Kivity98001d82010-05-13 11:05:49 +0300614 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +0300615 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500616}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600617
Alexander Graf4e755752009-10-30 05:47:01 +0000618int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
619{
620 return -ENOTSUPP;
621}
622
Stephen Rothwell2986b8c2009-06-02 11:46:14 +1000623int __init kvmppc_booke_init(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600624{
625 unsigned long ivor[16];
626 unsigned long max_ivor = 0;
627 int i;
628
629 /* We install our own exception handlers by hijacking IVPR. IVPR must
630 * be 16-bit aligned, so we need a 64KB allocation. */
631 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
632 VCPU_SIZE_ORDER);
633 if (!kvmppc_booke_handlers)
634 return -ENOMEM;
635
636 /* XXX make sure our handlers are smaller than Linux's */
637
638 /* Copy our interrupt handlers to match host IVORs. That way we don't
639 * have to swap the IVORs on every guest/host transition. */
640 ivor[0] = mfspr(SPRN_IVOR0);
641 ivor[1] = mfspr(SPRN_IVOR1);
642 ivor[2] = mfspr(SPRN_IVOR2);
643 ivor[3] = mfspr(SPRN_IVOR3);
644 ivor[4] = mfspr(SPRN_IVOR4);
645 ivor[5] = mfspr(SPRN_IVOR5);
646 ivor[6] = mfspr(SPRN_IVOR6);
647 ivor[7] = mfspr(SPRN_IVOR7);
648 ivor[8] = mfspr(SPRN_IVOR8);
649 ivor[9] = mfspr(SPRN_IVOR9);
650 ivor[10] = mfspr(SPRN_IVOR10);
651 ivor[11] = mfspr(SPRN_IVOR11);
652 ivor[12] = mfspr(SPRN_IVOR12);
653 ivor[13] = mfspr(SPRN_IVOR13);
654 ivor[14] = mfspr(SPRN_IVOR14);
655 ivor[15] = mfspr(SPRN_IVOR15);
656
657 for (i = 0; i < 16; i++) {
658 if (ivor[i] > max_ivor)
659 max_ivor = ivor[i];
660
661 memcpy((void *)kvmppc_booke_handlers + ivor[i],
662 kvmppc_handlers_start + i * kvmppc_handler_len,
663 kvmppc_handler_len);
664 }
665 flush_icache_range(kvmppc_booke_handlers,
666 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
667
Hollis Blancharddb93f572008-11-05 09:36:18 -0600668 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600669}
670
Hollis Blancharddb93f572008-11-05 09:36:18 -0600671void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600672{
673 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
674 kvm_exit();
675}