blob: 4aab6d2ce133fef325c90774674da0c6e644548b [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);
67 printk("srr0: %08lx srr1: %08lx\n", vcpu->arch.srr0, vcpu->arch.srr1);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050068
69 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
70
71 for (i = 0; i < 32; i += 4) {
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060072 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
Alexander Graf8e5b26b2010-01-08 02:58:01 +010073 kvmppc_get_gpr(vcpu, i),
74 kvmppc_get_gpr(vcpu, i+1),
75 kvmppc_get_gpr(vcpu, i+2),
76 kvmppc_get_gpr(vcpu, i+3));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050077 }
78}
79
Hollis Blanchardd4cf3892008-11-05 09:36:23 -060080static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
81 unsigned int priority)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060082{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060083 set_bit(priority, &vcpu->arch.pending_exceptions);
84}
85
Liu Yudaf5e272010-02-02 19:44:35 +080086static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
87 ulong dear_flags, ulong esr_flags)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060088{
Liu Yudaf5e272010-02-02 19:44:35 +080089 vcpu->arch.queued_dear = dear_flags;
90 vcpu->arch.queued_esr = esr_flags;
91 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
92}
93
94static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
95 ulong dear_flags, ulong esr_flags)
96{
97 vcpu->arch.queued_dear = dear_flags;
98 vcpu->arch.queued_esr = esr_flags;
99 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
100}
101
102static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
103 ulong esr_flags)
104{
105 vcpu->arch.queued_esr = esr_flags;
106 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
107}
108
109void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
110{
111 vcpu->arch.queued_esr = esr_flags;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600112 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600113}
114
115void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
116{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600117 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600118}
119
120int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
121{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600122 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600123}
124
Alexander Graf7706664d2009-12-21 20:21:24 +0100125void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
126{
127 clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
128}
129
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600130void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
131 struct kvm_interrupt *irq)
132{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600133 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_EXTERNAL);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600134}
135
Alexander Graf4496f972010-04-07 10:03:25 +0200136void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
137 struct kvm_interrupt *irq)
138{
139 clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
140}
141
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600142/* Deliver the interrupt of the corresponding priority, if possible. */
143static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
144 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500145{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600146 int allowed = 0;
Asias He6045be52010-06-19 16:52:12 +0800147 ulong uninitialized_var(msr_mask);
Liu Yudaf5e272010-02-02 19:44:35 +0800148 bool update_esr = false, update_dear = false;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500149
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600150 switch (priority) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600151 case BOOKE_IRQPRIO_DTLB_MISS:
Liu Yudaf5e272010-02-02 19:44:35 +0800152 case BOOKE_IRQPRIO_DATA_STORAGE:
153 update_dear = true;
154 /* fall through */
155 case BOOKE_IRQPRIO_INST_STORAGE:
156 case BOOKE_IRQPRIO_PROGRAM:
157 update_esr = true;
158 /* fall through */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600159 case BOOKE_IRQPRIO_ITLB_MISS:
160 case BOOKE_IRQPRIO_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600161 case BOOKE_IRQPRIO_FP_UNAVAIL:
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600162 case BOOKE_IRQPRIO_SPE_UNAVAIL:
163 case BOOKE_IRQPRIO_SPE_FP_DATA:
164 case BOOKE_IRQPRIO_SPE_FP_ROUND:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600165 case BOOKE_IRQPRIO_AP_UNAVAIL:
166 case BOOKE_IRQPRIO_ALIGNMENT:
167 allowed = 1;
168 msr_mask = MSR_CE|MSR_ME|MSR_DE;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500169 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600170 case BOOKE_IRQPRIO_CRITICAL:
171 case BOOKE_IRQPRIO_WATCHDOG:
Alexander Graf666e7252010-07-29 14:47:43 +0200172 allowed = vcpu->arch.shared->msr & MSR_CE;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600173 msr_mask = MSR_ME;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500174 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600175 case BOOKE_IRQPRIO_MACHINE_CHECK:
Alexander Graf666e7252010-07-29 14:47:43 +0200176 allowed = vcpu->arch.shared->msr & MSR_ME;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600177 msr_mask = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500178 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600179 case BOOKE_IRQPRIO_EXTERNAL:
180 case BOOKE_IRQPRIO_DECREMENTER:
181 case BOOKE_IRQPRIO_FIT:
Alexander Graf666e7252010-07-29 14:47:43 +0200182 allowed = vcpu->arch.shared->msr & MSR_EE;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600183 msr_mask = MSR_CE|MSR_ME|MSR_DE;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500184 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600185 case BOOKE_IRQPRIO_DEBUG:
Alexander Graf666e7252010-07-29 14:47:43 +0200186 allowed = vcpu->arch.shared->msr & MSR_DE;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600187 msr_mask = MSR_ME;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500188 break;
189 }
190
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600191 if (allowed) {
192 vcpu->arch.srr0 = vcpu->arch.pc;
Alexander Graf666e7252010-07-29 14:47:43 +0200193 vcpu->arch.srr1 = vcpu->arch.shared->msr;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600194 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
Liu Yudaf5e272010-02-02 19:44:35 +0800195 if (update_esr == true)
196 vcpu->arch.esr = vcpu->arch.queued_esr;
197 if (update_dear == true)
Alexander Graf5e030182010-07-29 14:47:45 +0200198 vcpu->arch.shared->dar = vcpu->arch.queued_dear;
Alexander Graf666e7252010-07-29 14:47:43 +0200199 kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600200
201 clear_bit(priority, &vcpu->arch.pending_exceptions);
202 }
203
204 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500205}
206
207/* Check pending exceptions and deliver one, if possible. */
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600208void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500209{
210 unsigned long *pending = &vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500211 unsigned int priority;
212
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600213 priority = __ffs(*pending);
Hollis Blanchardbdc89f12009-01-03 16:23:12 -0600214 while (priority <= BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600215 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500216 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500217
218 priority = find_next_bit(pending,
219 BITS_PER_BYTE * sizeof(*pending),
220 priority + 1);
221 }
222}
223
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500224/**
225 * kvmppc_handle_exit
226 *
227 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
228 */
229int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
230 unsigned int exit_nr)
231{
232 enum emulation_result er;
233 int r = RESUME_HOST;
234
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600235 /* update before a new last_exit_type is rewritten */
236 kvmppc_update_timing_stats(vcpu);
237
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500238 local_irq_enable();
239
240 run->exit_reason = KVM_EXIT_UNKNOWN;
241 run->ready_for_interrupt_injection = 1;
242
243 switch (exit_nr) {
244 case BOOKE_INTERRUPT_MACHINE_CHECK:
245 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
246 kvmppc_dump_vcpu(vcpu);
247 r = RESUME_HOST;
248 break;
249
250 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600251 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -0600252 if (need_resched())
253 cond_resched();
254 r = RESUME_GUEST;
255 break;
256
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500257 case BOOKE_INTERRUPT_DECREMENTER:
258 /* Since we switched IVPR back to the host's value, the host
259 * handled this interrupt the moment we enabled interrupts.
260 * Now we just offer it a chance to reschedule the guest. */
Hollis Blanchard7b701592008-12-02 15:51:58 -0600261 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500262 if (need_resched())
263 cond_resched();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500264 r = RESUME_GUEST;
265 break;
266
267 case BOOKE_INTERRUPT_PROGRAM:
Alexander Graf666e7252010-07-29 14:47:43 +0200268 if (vcpu->arch.shared->msr & MSR_PR) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500269 /* Program traps generated by user-level software must be handled
270 * by the guest kernel. */
Liu Yudaf5e272010-02-02 19:44:35 +0800271 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500272 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600273 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500274 break;
275 }
276
277 er = kvmppc_emulate_instruction(run, vcpu);
278 switch (er) {
279 case EMULATE_DONE:
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600280 /* don't overwrite subtypes, just account kvm_stats */
Hollis Blanchard7b701592008-12-02 15:51:58 -0600281 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500282 /* Future optimization: only reload non-volatiles if
283 * they were actually modified by emulation. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500284 r = RESUME_GUEST_NV;
285 break;
286 case EMULATE_DO_DCR:
287 run->exit_reason = KVM_EXIT_DCR;
288 r = RESUME_HOST;
289 break;
290 case EMULATE_FAIL:
291 /* XXX Deliver Program interrupt to guest. */
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -0600292 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500293 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
294 /* For debugging, encode the failing instruction and
295 * report it to userspace. */
296 run->hw.hardware_exit_reason = ~0ULL << 32;
297 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
298 r = RESUME_HOST;
299 break;
300 default:
301 BUG();
302 }
303 break;
304
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200305 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600306 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600307 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200308 r = RESUME_GUEST;
309 break;
310
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600311 case BOOKE_INTERRUPT_SPE_UNAVAIL:
312 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_UNAVAIL);
313 r = RESUME_GUEST;
314 break;
315
316 case BOOKE_INTERRUPT_SPE_FP_DATA:
317 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
318 r = RESUME_GUEST;
319 break;
320
321 case BOOKE_INTERRUPT_SPE_FP_ROUND:
322 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
323 r = RESUME_GUEST;
324 break;
325
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500326 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800327 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
328 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600329 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500330 r = RESUME_GUEST;
331 break;
332
333 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800334 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600335 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500336 r = RESUME_GUEST;
337 break;
338
339 case BOOKE_INTERRUPT_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600340 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600341 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500342 r = RESUME_GUEST;
343 break;
344
345 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500346 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600347 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600348 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500349 gfn_t gfn;
350
351 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600352 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600353 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500354 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +0800355 kvmppc_core_queue_dtlb_miss(vcpu,
356 vcpu->arch.fault_dear,
357 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600358 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600359 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500360 r = RESUME_GUEST;
361 break;
362 }
363
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600364 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600365 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500366
367 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
368 /* The guest TLB had a mapping, but the shadow TLB
369 * didn't, and it is RAM. This could be because:
370 * a) the entry is mapping the host kernel, or
371 * b) the guest used a large mapping which we're faking
372 * Either way, we need to satisfy the fault without
373 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600374 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600375 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500376 r = RESUME_GUEST;
377 } else {
378 /* Guest has mapped and accessed a page which is not
379 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600380 vcpu->arch.paddr_accessed = gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500381 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600382 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500383 }
384
385 break;
386 }
387
388 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500389 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -0600390 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500391 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600392 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500393
394 r = RESUME_GUEST;
395
396 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600397 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600398 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500399 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600400 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600401 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600402 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500403 break;
404 }
405
Hollis Blanchard7b701592008-12-02 15:51:58 -0600406 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500407
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600408 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -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. 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 Blanchardbbf45ba2008-04-16 23:28:09 -0500419 } else {
420 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600421 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500422 }
423
424 break;
425 }
426
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500427 case BOOKE_INTERRUPT_DEBUG: {
428 u32 dbsr;
429
430 vcpu->arch.pc = mfspr(SPRN_CSRR0);
431
432 /* clear IAC events in DBSR register */
433 dbsr = mfspr(SPRN_DBSR);
434 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
435 mtspr(SPRN_DBSR, dbsr);
436
437 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600438 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500439 r = RESUME_HOST;
440 break;
441 }
442
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500443 default:
444 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
445 BUG();
446 }
447
448 local_irq_disable();
449
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600450 kvmppc_core_deliver_interrupts(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500451
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500452 if (!(r & RESUME_HOST)) {
453 /* To avoid clobbering exit_reason, only check for signals if
454 * we aren't already exiting to userspace for some other
455 * reason. */
456 if (signal_pending(current)) {
457 run->exit_reason = KVM_EXIT_INTR;
458 r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600459 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500460 }
461 }
462
463 return r;
464}
465
466/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
467int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
468{
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500469 vcpu->arch.pc = 0;
Alexander Graf666e7252010-07-29 14:47:43 +0200470 vcpu->arch.shared->msr = 0;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100471 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500472
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500473 vcpu->arch.shadow_pid = 1;
474
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500475 /* Eye-catching number so we know if the guest takes an interrupt
476 * before it's programmed its own IVPR. */
477 vcpu->arch.ivpr = 0x55550000;
478
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600479 kvmppc_init_timing_stats(vcpu);
480
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600481 return kvmppc_core_vcpu_setup(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500482}
483
484int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
485{
486 int i;
487
488 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100489 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500490 regs->ctr = vcpu->arch.ctr;
491 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100492 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +0200493 regs->msr = vcpu->arch.shared->msr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500494 regs->srr0 = vcpu->arch.srr0;
495 regs->srr1 = vcpu->arch.srr1;
496 regs->pid = vcpu->arch.pid;
497 regs->sprg0 = vcpu->arch.sprg0;
498 regs->sprg1 = vcpu->arch.sprg1;
499 regs->sprg2 = vcpu->arch.sprg2;
500 regs->sprg3 = vcpu->arch.sprg3;
501 regs->sprg5 = vcpu->arch.sprg4;
502 regs->sprg6 = vcpu->arch.sprg5;
503 regs->sprg7 = vcpu->arch.sprg6;
504
505 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100506 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500507
508 return 0;
509}
510
511int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
512{
513 int i;
514
515 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100516 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500517 vcpu->arch.ctr = regs->ctr;
518 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100519 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -0600520 kvmppc_set_msr(vcpu, regs->msr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500521 vcpu->arch.srr0 = regs->srr0;
522 vcpu->arch.srr1 = regs->srr1;
523 vcpu->arch.sprg0 = regs->sprg0;
524 vcpu->arch.sprg1 = regs->sprg1;
525 vcpu->arch.sprg2 = regs->sprg2;
526 vcpu->arch.sprg3 = regs->sprg3;
527 vcpu->arch.sprg5 = regs->sprg4;
528 vcpu->arch.sprg6 = regs->sprg5;
529 vcpu->arch.sprg7 = regs->sprg6;
530
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100531 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
532 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500533
534 return 0;
535}
536
537int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
538 struct kvm_sregs *sregs)
539{
540 return -ENOTSUPP;
541}
542
543int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
544 struct kvm_sregs *sregs)
545{
546 return -ENOTSUPP;
547}
548
549int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
550{
551 return -ENOTSUPP;
552}
553
554int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
555{
556 return -ENOTSUPP;
557}
558
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500559int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
560 struct kvm_translation *tr)
561{
Avi Kivity98001d82010-05-13 11:05:49 +0300562 int r;
563
Avi Kivity98001d82010-05-13 11:05:49 +0300564 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +0300565 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500566}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600567
Alexander Graf4e755752009-10-30 05:47:01 +0000568int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
569{
570 return -ENOTSUPP;
571}
572
Stephen Rothwell2986b8c2009-06-02 11:46:14 +1000573int __init kvmppc_booke_init(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600574{
575 unsigned long ivor[16];
576 unsigned long max_ivor = 0;
577 int i;
578
579 /* We install our own exception handlers by hijacking IVPR. IVPR must
580 * be 16-bit aligned, so we need a 64KB allocation. */
581 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
582 VCPU_SIZE_ORDER);
583 if (!kvmppc_booke_handlers)
584 return -ENOMEM;
585
586 /* XXX make sure our handlers are smaller than Linux's */
587
588 /* Copy our interrupt handlers to match host IVORs. That way we don't
589 * have to swap the IVORs on every guest/host transition. */
590 ivor[0] = mfspr(SPRN_IVOR0);
591 ivor[1] = mfspr(SPRN_IVOR1);
592 ivor[2] = mfspr(SPRN_IVOR2);
593 ivor[3] = mfspr(SPRN_IVOR3);
594 ivor[4] = mfspr(SPRN_IVOR4);
595 ivor[5] = mfspr(SPRN_IVOR5);
596 ivor[6] = mfspr(SPRN_IVOR6);
597 ivor[7] = mfspr(SPRN_IVOR7);
598 ivor[8] = mfspr(SPRN_IVOR8);
599 ivor[9] = mfspr(SPRN_IVOR9);
600 ivor[10] = mfspr(SPRN_IVOR10);
601 ivor[11] = mfspr(SPRN_IVOR11);
602 ivor[12] = mfspr(SPRN_IVOR12);
603 ivor[13] = mfspr(SPRN_IVOR13);
604 ivor[14] = mfspr(SPRN_IVOR14);
605 ivor[15] = mfspr(SPRN_IVOR15);
606
607 for (i = 0; i < 16; i++) {
608 if (ivor[i] > max_ivor)
609 max_ivor = ivor[i];
610
611 memcpy((void *)kvmppc_booke_handlers + ivor[i],
612 kvmppc_handlers_start + i * kvmppc_handler_len,
613 kvmppc_handler_len);
614 }
615 flush_icache_range(kvmppc_booke_handlers,
616 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
617
Hollis Blancharddb93f572008-11-05 09:36:18 -0600618 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600619}
620
Hollis Blancharddb93f572008-11-05 09:36:18 -0600621void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600622{
623 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
624 kvm_exit();
625}