blob: 05cedb5f82100a01351c465d757cb5309ab0edeb [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;
Scott Woodecee2732011-06-14 18:34:29 -0500517 vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100518 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500519
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500520 vcpu->arch.shadow_pid = 1;
521
Hollis Blanchard082decf2010-08-07 10:33:56 -0700522 /* Eye-catching numbers so we know if the guest takes an interrupt
523 * before it's programmed its own IVPR/IVORs. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500524 vcpu->arch.ivpr = 0x55550000;
Hollis Blanchard082decf2010-08-07 10:33:56 -0700525 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
526 vcpu->arch.ivor[i] = 0x7700 | i * 4;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500527
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600528 kvmppc_init_timing_stats(vcpu);
529
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600530 return kvmppc_core_vcpu_setup(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500531}
532
533int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
534{
535 int i;
536
537 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100538 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500539 regs->ctr = vcpu->arch.ctr;
540 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100541 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +0200542 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +0200543 regs->srr0 = vcpu->arch.shared->srr0;
544 regs->srr1 = vcpu->arch.shared->srr1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500545 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +0200546 regs->sprg0 = vcpu->arch.shared->sprg0;
547 regs->sprg1 = vcpu->arch.shared->sprg1;
548 regs->sprg2 = vcpu->arch.shared->sprg2;
549 regs->sprg3 = vcpu->arch.shared->sprg3;
Peter Tyserbc9c1932010-12-29 13:51:25 -0600550 regs->sprg4 = vcpu->arch.sprg4;
551 regs->sprg5 = vcpu->arch.sprg5;
552 regs->sprg6 = vcpu->arch.sprg6;
553 regs->sprg7 = vcpu->arch.sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500554
555 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100556 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500557
558 return 0;
559}
560
561int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
562{
563 int i;
564
565 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100566 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500567 vcpu->arch.ctr = regs->ctr;
568 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100569 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -0600570 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +0200571 vcpu->arch.shared->srr0 = regs->srr0;
572 vcpu->arch.shared->srr1 = regs->srr1;
Scott Wood5ce941e2011-04-27 17:24:21 -0500573 kvmppc_set_pid(vcpu, regs->pid);
Alexander Grafa73a9592010-07-29 14:47:47 +0200574 vcpu->arch.shared->sprg0 = regs->sprg0;
575 vcpu->arch.shared->sprg1 = regs->sprg1;
576 vcpu->arch.shared->sprg2 = regs->sprg2;
577 vcpu->arch.shared->sprg3 = regs->sprg3;
Peter Tyserbc9c1932010-12-29 13:51:25 -0600578 vcpu->arch.sprg4 = regs->sprg4;
579 vcpu->arch.sprg5 = regs->sprg5;
580 vcpu->arch.sprg6 = regs->sprg6;
581 vcpu->arch.sprg7 = regs->sprg7;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500582
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100583 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
584 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500585
586 return 0;
587}
588
Scott Wood5ce941e2011-04-27 17:24:21 -0500589static void get_sregs_base(struct kvm_vcpu *vcpu,
590 struct kvm_sregs *sregs)
591{
592 u64 tb = get_tb();
593
594 sregs->u.e.features |= KVM_SREGS_E_BASE;
595
596 sregs->u.e.csrr0 = vcpu->arch.csrr0;
597 sregs->u.e.csrr1 = vcpu->arch.csrr1;
598 sregs->u.e.mcsr = vcpu->arch.mcsr;
599 sregs->u.e.esr = vcpu->arch.esr;
600 sregs->u.e.dear = vcpu->arch.shared->dar;
601 sregs->u.e.tsr = vcpu->arch.tsr;
602 sregs->u.e.tcr = vcpu->arch.tcr;
603 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
604 sregs->u.e.tb = tb;
605 sregs->u.e.vrsave = vcpu->arch.vrsave;
606}
607
608static int set_sregs_base(struct kvm_vcpu *vcpu,
609 struct kvm_sregs *sregs)
610{
611 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
612 return 0;
613
614 vcpu->arch.csrr0 = sregs->u.e.csrr0;
615 vcpu->arch.csrr1 = sregs->u.e.csrr1;
616 vcpu->arch.mcsr = sregs->u.e.mcsr;
617 vcpu->arch.esr = sregs->u.e.esr;
618 vcpu->arch.shared->dar = sregs->u.e.dear;
619 vcpu->arch.vrsave = sregs->u.e.vrsave;
620 vcpu->arch.tcr = sregs->u.e.tcr;
621
622 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC)
623 vcpu->arch.dec = sregs->u.e.dec;
624
625 kvmppc_emulate_dec(vcpu);
626
627 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
628 /*
629 * FIXME: existing KVM timer handling is incomplete.
630 * TSR cannot be read by the guest, and its value in
631 * vcpu->arch is always zero. For now, just handle
632 * the case where the caller is trying to inject a
633 * decrementer interrupt.
634 */
635
636 if ((sregs->u.e.tsr & TSR_DIS) &&
637 (vcpu->arch.tcr & TCR_DIE))
638 kvmppc_core_queue_dec(vcpu);
639 }
640
641 return 0;
642}
643
644static void get_sregs_arch206(struct kvm_vcpu *vcpu,
645 struct kvm_sregs *sregs)
646{
647 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
648
649 sregs->u.e.pir = 0;
650 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
651 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
652 sregs->u.e.decar = vcpu->arch.decar;
653 sregs->u.e.ivpr = vcpu->arch.ivpr;
654}
655
656static int set_sregs_arch206(struct kvm_vcpu *vcpu,
657 struct kvm_sregs *sregs)
658{
659 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
660 return 0;
661
662 if (sregs->u.e.pir != 0)
663 return -EINVAL;
664
665 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
666 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
667 vcpu->arch.decar = sregs->u.e.decar;
668 vcpu->arch.ivpr = sregs->u.e.ivpr;
669
670 return 0;
671}
672
673void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
674{
675 sregs->u.e.features |= KVM_SREGS_E_IVOR;
676
677 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
678 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
679 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
680 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
681 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
682 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
683 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
684 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
685 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
686 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
687 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
688 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
689 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
690 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
691 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
692 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
693}
694
695int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
696{
697 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
698 return 0;
699
700 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
701 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
702 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
703 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
704 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
705 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
706 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
707 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
708 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
709 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
710 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
711 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
712 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
713 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
714 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
715 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
716
717 return 0;
718}
719
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500720int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
721 struct kvm_sregs *sregs)
722{
Scott Wood5ce941e2011-04-27 17:24:21 -0500723 sregs->pvr = vcpu->arch.pvr;
724
725 get_sregs_base(vcpu, sregs);
726 get_sregs_arch206(vcpu, sregs);
727 kvmppc_core_get_sregs(vcpu, sregs);
728 return 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500729}
730
731int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
732 struct kvm_sregs *sregs)
733{
Scott Wood5ce941e2011-04-27 17:24:21 -0500734 int ret;
735
736 if (vcpu->arch.pvr != sregs->pvr)
737 return -EINVAL;
738
739 ret = set_sregs_base(vcpu, sregs);
740 if (ret < 0)
741 return ret;
742
743 ret = set_sregs_arch206(vcpu, sregs);
744 if (ret < 0)
745 return ret;
746
747 return kvmppc_core_set_sregs(vcpu, sregs);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500748}
749
750int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
751{
752 return -ENOTSUPP;
753}
754
755int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
756{
757 return -ENOTSUPP;
758}
759
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500760int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
761 struct kvm_translation *tr)
762{
Avi Kivity98001d82010-05-13 11:05:49 +0300763 int r;
764
Avi Kivity98001d82010-05-13 11:05:49 +0300765 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +0300766 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500767}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600768
Alexander Graf4e755752009-10-30 05:47:01 +0000769int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
770{
771 return -ENOTSUPP;
772}
773
Stephen Rothwell2986b8c2009-06-02 11:46:14 +1000774int __init kvmppc_booke_init(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600775{
776 unsigned long ivor[16];
777 unsigned long max_ivor = 0;
778 int i;
779
780 /* We install our own exception handlers by hijacking IVPR. IVPR must
781 * be 16-bit aligned, so we need a 64KB allocation. */
782 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
783 VCPU_SIZE_ORDER);
784 if (!kvmppc_booke_handlers)
785 return -ENOMEM;
786
787 /* XXX make sure our handlers are smaller than Linux's */
788
789 /* Copy our interrupt handlers to match host IVORs. That way we don't
790 * have to swap the IVORs on every guest/host transition. */
791 ivor[0] = mfspr(SPRN_IVOR0);
792 ivor[1] = mfspr(SPRN_IVOR1);
793 ivor[2] = mfspr(SPRN_IVOR2);
794 ivor[3] = mfspr(SPRN_IVOR3);
795 ivor[4] = mfspr(SPRN_IVOR4);
796 ivor[5] = mfspr(SPRN_IVOR5);
797 ivor[6] = mfspr(SPRN_IVOR6);
798 ivor[7] = mfspr(SPRN_IVOR7);
799 ivor[8] = mfspr(SPRN_IVOR8);
800 ivor[9] = mfspr(SPRN_IVOR9);
801 ivor[10] = mfspr(SPRN_IVOR10);
802 ivor[11] = mfspr(SPRN_IVOR11);
803 ivor[12] = mfspr(SPRN_IVOR12);
804 ivor[13] = mfspr(SPRN_IVOR13);
805 ivor[14] = mfspr(SPRN_IVOR14);
806 ivor[15] = mfspr(SPRN_IVOR15);
807
808 for (i = 0; i < 16; i++) {
809 if (ivor[i] > max_ivor)
810 max_ivor = ivor[i];
811
812 memcpy((void *)kvmppc_booke_handlers + ivor[i],
813 kvmppc_handlers_start + i * kvmppc_handler_len,
814 kvmppc_handler_len);
815 }
816 flush_icache_range(kvmppc_booke_handlers,
817 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
818
Hollis Blancharddb93f572008-11-05 09:36:18 -0600819 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600820}
821
Hollis Blancharddb93f572008-11-05 09:36:18 -0600822void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600823{
824 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
825 kvm_exit();
826}