blob: 13e0747178e3377e1b554824f8b261df045e2243 [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{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600134 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_EXTERNAL);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600135}
136
Alexander Graf4496f972010-04-07 10:03:25 +0200137void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
138 struct kvm_interrupt *irq)
139{
140 clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
141}
142
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600143/* Deliver the interrupt of the corresponding priority, if possible. */
144static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
145 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500146{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600147 int allowed = 0;
Asias He6045be52010-06-19 16:52:12 +0800148 ulong uninitialized_var(msr_mask);
Liu Yudaf5e272010-02-02 19:44:35 +0800149 bool update_esr = false, update_dear = false;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500150
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600151 switch (priority) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600152 case BOOKE_IRQPRIO_DTLB_MISS:
Liu Yudaf5e272010-02-02 19:44:35 +0800153 case BOOKE_IRQPRIO_DATA_STORAGE:
154 update_dear = true;
155 /* fall through */
156 case BOOKE_IRQPRIO_INST_STORAGE:
157 case BOOKE_IRQPRIO_PROGRAM:
158 update_esr = true;
159 /* fall through */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600160 case BOOKE_IRQPRIO_ITLB_MISS:
161 case BOOKE_IRQPRIO_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600162 case BOOKE_IRQPRIO_FP_UNAVAIL:
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600163 case BOOKE_IRQPRIO_SPE_UNAVAIL:
164 case BOOKE_IRQPRIO_SPE_FP_DATA:
165 case BOOKE_IRQPRIO_SPE_FP_ROUND:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600166 case BOOKE_IRQPRIO_AP_UNAVAIL:
167 case BOOKE_IRQPRIO_ALIGNMENT:
168 allowed = 1;
169 msr_mask = MSR_CE|MSR_ME|MSR_DE;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500170 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600171 case BOOKE_IRQPRIO_CRITICAL:
172 case BOOKE_IRQPRIO_WATCHDOG:
Alexander Graf666e7252010-07-29 14:47:43 +0200173 allowed = vcpu->arch.shared->msr & MSR_CE;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600174 msr_mask = MSR_ME;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500175 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600176 case BOOKE_IRQPRIO_MACHINE_CHECK:
Alexander Graf666e7252010-07-29 14:47:43 +0200177 allowed = vcpu->arch.shared->msr & MSR_ME;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600178 msr_mask = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500179 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600180 case BOOKE_IRQPRIO_EXTERNAL:
181 case BOOKE_IRQPRIO_DECREMENTER:
182 case BOOKE_IRQPRIO_FIT:
Alexander Graf666e7252010-07-29 14:47:43 +0200183 allowed = vcpu->arch.shared->msr & MSR_EE;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600184 msr_mask = MSR_CE|MSR_ME|MSR_DE;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500185 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600186 case BOOKE_IRQPRIO_DEBUG:
Alexander Graf666e7252010-07-29 14:47:43 +0200187 allowed = vcpu->arch.shared->msr & MSR_DE;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600188 msr_mask = MSR_ME;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500189 break;
190 }
191
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600192 if (allowed) {
Alexander Grafde7906c2010-07-29 14:47:46 +0200193 vcpu->arch.shared->srr0 = vcpu->arch.pc;
194 vcpu->arch.shared->srr1 = vcpu->arch.shared->msr;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600195 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
Liu Yudaf5e272010-02-02 19:44:35 +0800196 if (update_esr == true)
197 vcpu->arch.esr = vcpu->arch.queued_esr;
198 if (update_dear == true)
Alexander Graf5e030182010-07-29 14:47:45 +0200199 vcpu->arch.shared->dar = vcpu->arch.queued_dear;
Alexander Graf666e7252010-07-29 14:47:43 +0200200 kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600201
202 clear_bit(priority, &vcpu->arch.pending_exceptions);
203 }
204
205 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500206}
207
208/* Check pending exceptions and deliver one, if possible. */
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600209void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500210{
211 unsigned long *pending = &vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500212 unsigned int priority;
213
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600214 priority = __ffs(*pending);
Hollis Blanchardbdc89f12009-01-03 16:23:12 -0600215 while (priority <= BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600216 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500217 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500218
219 priority = find_next_bit(pending,
220 BITS_PER_BYTE * sizeof(*pending),
221 priority + 1);
222 }
223}
224
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500225/**
226 * kvmppc_handle_exit
227 *
228 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
229 */
230int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
231 unsigned int exit_nr)
232{
233 enum emulation_result er;
234 int r = RESUME_HOST;
235
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600236 /* update before a new last_exit_type is rewritten */
237 kvmppc_update_timing_stats(vcpu);
238
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500239 local_irq_enable();
240
241 run->exit_reason = KVM_EXIT_UNKNOWN;
242 run->ready_for_interrupt_injection = 1;
243
244 switch (exit_nr) {
245 case BOOKE_INTERRUPT_MACHINE_CHECK:
246 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
247 kvmppc_dump_vcpu(vcpu);
248 r = RESUME_HOST;
249 break;
250
251 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600252 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -0600253 if (need_resched())
254 cond_resched();
255 r = RESUME_GUEST;
256 break;
257
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500258 case BOOKE_INTERRUPT_DECREMENTER:
259 /* Since we switched IVPR back to the host's value, the host
260 * handled this interrupt the moment we enabled interrupts.
261 * Now we just offer it a chance to reschedule the guest. */
Hollis Blanchard7b701592008-12-02 15:51:58 -0600262 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500263 if (need_resched())
264 cond_resched();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500265 r = RESUME_GUEST;
266 break;
267
268 case BOOKE_INTERRUPT_PROGRAM:
Alexander Graf666e7252010-07-29 14:47:43 +0200269 if (vcpu->arch.shared->msr & MSR_PR) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500270 /* Program traps generated by user-level software must be handled
271 * by the guest kernel. */
Liu Yudaf5e272010-02-02 19:44:35 +0800272 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500273 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600274 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500275 break;
276 }
277
278 er = kvmppc_emulate_instruction(run, vcpu);
279 switch (er) {
280 case EMULATE_DONE:
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600281 /* don't overwrite subtypes, just account kvm_stats */
Hollis Blanchard7b701592008-12-02 15:51:58 -0600282 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500283 /* Future optimization: only reload non-volatiles if
284 * they were actually modified by emulation. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500285 r = RESUME_GUEST_NV;
286 break;
287 case EMULATE_DO_DCR:
288 run->exit_reason = KVM_EXIT_DCR;
289 r = RESUME_HOST;
290 break;
291 case EMULATE_FAIL:
292 /* XXX Deliver Program interrupt to guest. */
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -0600293 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500294 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
295 /* For debugging, encode the failing instruction and
296 * report it to userspace. */
297 run->hw.hardware_exit_reason = ~0ULL << 32;
298 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
299 r = RESUME_HOST;
300 break;
301 default:
302 BUG();
303 }
304 break;
305
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200306 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600307 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600308 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200309 r = RESUME_GUEST;
310 break;
311
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600312 case BOOKE_INTERRUPT_SPE_UNAVAIL:
313 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_UNAVAIL);
314 r = RESUME_GUEST;
315 break;
316
317 case BOOKE_INTERRUPT_SPE_FP_DATA:
318 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
319 r = RESUME_GUEST;
320 break;
321
322 case BOOKE_INTERRUPT_SPE_FP_ROUND:
323 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
324 r = RESUME_GUEST;
325 break;
326
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500327 case BOOKE_INTERRUPT_DATA_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800328 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
329 vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600330 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500331 r = RESUME_GUEST;
332 break;
333
334 case BOOKE_INTERRUPT_INST_STORAGE:
Liu Yudaf5e272010-02-02 19:44:35 +0800335 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600336 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500337 r = RESUME_GUEST;
338 break;
339
340 case BOOKE_INTERRUPT_SYSCALL:
Alexander Graf2a342ed2010-07-29 14:47:48 +0200341 if (!(vcpu->arch.shared->msr & MSR_PR) &&
342 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
343 /* KVM PV hypercalls */
344 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
345 r = RESUME_GUEST;
346 } else {
347 /* Guest syscalls */
348 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
349 }
Hollis Blanchard7b701592008-12-02 15:51:58 -0600350 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500351 r = RESUME_GUEST;
352 break;
353
354 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500355 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600356 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600357 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500358 gfn_t gfn;
359
360 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600361 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600362 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500363 /* The guest didn't have a mapping for it. */
Liu Yudaf5e272010-02-02 19:44:35 +0800364 kvmppc_core_queue_dtlb_miss(vcpu,
365 vcpu->arch.fault_dear,
366 vcpu->arch.fault_esr);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600367 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600368 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500369 r = RESUME_GUEST;
370 break;
371 }
372
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600373 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600374 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500375
376 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
377 /* The guest TLB had a mapping, but the shadow TLB
378 * didn't, and it is RAM. This could be because:
379 * a) the entry is mapping the host kernel, or
380 * b) the guest used a large mapping which we're faking
381 * Either way, we need to satisfy the fault without
382 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600383 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600384 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500385 r = RESUME_GUEST;
386 } else {
387 /* Guest has mapped and accessed a page which is not
388 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600389 vcpu->arch.paddr_accessed = gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500390 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600391 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500392 }
393
394 break;
395 }
396
397 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500398 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -0600399 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500400 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600401 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500402
403 r = RESUME_GUEST;
404
405 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600406 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600407 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500408 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600409 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600410 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600411 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500412 break;
413 }
414
Hollis Blanchard7b701592008-12-02 15:51:58 -0600415 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500416
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600417 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -0600418 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500419
420 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
421 /* The guest TLB had a mapping, but the shadow TLB
422 * didn't. This could be because:
423 * a) the entry is mapping the host kernel, or
424 * b) the guest used a large mapping which we're faking
425 * Either way, we need to satisfy the fault without
426 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600427 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500428 } else {
429 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600430 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500431 }
432
433 break;
434 }
435
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500436 case BOOKE_INTERRUPT_DEBUG: {
437 u32 dbsr;
438
439 vcpu->arch.pc = mfspr(SPRN_CSRR0);
440
441 /* clear IAC events in DBSR register */
442 dbsr = mfspr(SPRN_DBSR);
443 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
444 mtspr(SPRN_DBSR, dbsr);
445
446 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600447 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500448 r = RESUME_HOST;
449 break;
450 }
451
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500452 default:
453 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
454 BUG();
455 }
456
457 local_irq_disable();
458
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600459 kvmppc_core_deliver_interrupts(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500460
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500461 if (!(r & RESUME_HOST)) {
462 /* To avoid clobbering exit_reason, only check for signals if
463 * we aren't already exiting to userspace for some other
464 * reason. */
465 if (signal_pending(current)) {
466 run->exit_reason = KVM_EXIT_INTR;
467 r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600468 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500469 }
470 }
471
472 return r;
473}
474
475/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
476int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
477{
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500478 vcpu->arch.pc = 0;
Alexander Graf666e7252010-07-29 14:47:43 +0200479 vcpu->arch.shared->msr = 0;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100480 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500481
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500482 vcpu->arch.shadow_pid = 1;
483
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500484 /* Eye-catching number so we know if the guest takes an interrupt
485 * before it's programmed its own IVPR. */
486 vcpu->arch.ivpr = 0x55550000;
487
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600488 kvmppc_init_timing_stats(vcpu);
489
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600490 return kvmppc_core_vcpu_setup(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500491}
492
493int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
494{
495 int i;
496
497 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100498 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500499 regs->ctr = vcpu->arch.ctr;
500 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100501 regs->xer = kvmppc_get_xer(vcpu);
Alexander Graf666e7252010-07-29 14:47:43 +0200502 regs->msr = vcpu->arch.shared->msr;
Alexander Grafde7906c2010-07-29 14:47:46 +0200503 regs->srr0 = vcpu->arch.shared->srr0;
504 regs->srr1 = vcpu->arch.shared->srr1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500505 regs->pid = vcpu->arch.pid;
Alexander Grafa73a9592010-07-29 14:47:47 +0200506 regs->sprg0 = vcpu->arch.shared->sprg0;
507 regs->sprg1 = vcpu->arch.shared->sprg1;
508 regs->sprg2 = vcpu->arch.shared->sprg2;
509 regs->sprg3 = vcpu->arch.shared->sprg3;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500510 regs->sprg5 = vcpu->arch.sprg4;
511 regs->sprg6 = vcpu->arch.sprg5;
512 regs->sprg7 = vcpu->arch.sprg6;
513
514 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100515 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500516
517 return 0;
518}
519
520int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
521{
522 int i;
523
524 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100525 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500526 vcpu->arch.ctr = regs->ctr;
527 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100528 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -0600529 kvmppc_set_msr(vcpu, regs->msr);
Alexander Grafde7906c2010-07-29 14:47:46 +0200530 vcpu->arch.shared->srr0 = regs->srr0;
531 vcpu->arch.shared->srr1 = regs->srr1;
Alexander Grafa73a9592010-07-29 14:47:47 +0200532 vcpu->arch.shared->sprg0 = regs->sprg0;
533 vcpu->arch.shared->sprg1 = regs->sprg1;
534 vcpu->arch.shared->sprg2 = regs->sprg2;
535 vcpu->arch.shared->sprg3 = regs->sprg3;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500536 vcpu->arch.sprg5 = regs->sprg4;
537 vcpu->arch.sprg6 = regs->sprg5;
538 vcpu->arch.sprg7 = regs->sprg6;
539
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100540 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
541 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500542
543 return 0;
544}
545
546int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
547 struct kvm_sregs *sregs)
548{
549 return -ENOTSUPP;
550}
551
552int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
553 struct kvm_sregs *sregs)
554{
555 return -ENOTSUPP;
556}
557
558int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
559{
560 return -ENOTSUPP;
561}
562
563int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
564{
565 return -ENOTSUPP;
566}
567
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500568int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
569 struct kvm_translation *tr)
570{
Avi Kivity98001d82010-05-13 11:05:49 +0300571 int r;
572
Avi Kivity98001d82010-05-13 11:05:49 +0300573 r = kvmppc_core_vcpu_translate(vcpu, tr);
Avi Kivity98001d82010-05-13 11:05:49 +0300574 return r;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500575}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600576
Alexander Graf4e755752009-10-30 05:47:01 +0000577int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
578{
579 return -ENOTSUPP;
580}
581
Stephen Rothwell2986b8c2009-06-02 11:46:14 +1000582int __init kvmppc_booke_init(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600583{
584 unsigned long ivor[16];
585 unsigned long max_ivor = 0;
586 int i;
587
588 /* We install our own exception handlers by hijacking IVPR. IVPR must
589 * be 16-bit aligned, so we need a 64KB allocation. */
590 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
591 VCPU_SIZE_ORDER);
592 if (!kvmppc_booke_handlers)
593 return -ENOMEM;
594
595 /* XXX make sure our handlers are smaller than Linux's */
596
597 /* Copy our interrupt handlers to match host IVORs. That way we don't
598 * have to swap the IVORs on every guest/host transition. */
599 ivor[0] = mfspr(SPRN_IVOR0);
600 ivor[1] = mfspr(SPRN_IVOR1);
601 ivor[2] = mfspr(SPRN_IVOR2);
602 ivor[3] = mfspr(SPRN_IVOR3);
603 ivor[4] = mfspr(SPRN_IVOR4);
604 ivor[5] = mfspr(SPRN_IVOR5);
605 ivor[6] = mfspr(SPRN_IVOR6);
606 ivor[7] = mfspr(SPRN_IVOR7);
607 ivor[8] = mfspr(SPRN_IVOR8);
608 ivor[9] = mfspr(SPRN_IVOR9);
609 ivor[10] = mfspr(SPRN_IVOR10);
610 ivor[11] = mfspr(SPRN_IVOR11);
611 ivor[12] = mfspr(SPRN_IVOR12);
612 ivor[13] = mfspr(SPRN_IVOR13);
613 ivor[14] = mfspr(SPRN_IVOR14);
614 ivor[15] = mfspr(SPRN_IVOR15);
615
616 for (i = 0; i < 16; i++) {
617 if (ivor[i] > max_ivor)
618 max_ivor = ivor[i];
619
620 memcpy((void *)kvmppc_booke_handlers + ivor[i],
621 kvmppc_handlers_start + i * kvmppc_handler_len,
622 kvmppc_handler_len);
623 }
624 flush_icache_range(kvmppc_booke_handlers,
625 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
626
Hollis Blancharddb93f572008-11-05 09:36:18 -0600627 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600628}
629
Hollis Blancharddb93f572008-11-05 09:36:18 -0600630void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600631{
632 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
633 kvm_exit();
634}