blob: a73b3959e41c57fe10208cfc30a941c01e4d5a87 [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>
24#include <linux/module.h>
25#include <linux/vmalloc.h>
26#include <linux/fs.h>
Hollis Blanchard7924bd42008-12-02 15:51:55 -060027
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050028#include <asm/cputable.h>
29#include <asm/uaccess.h>
30#include <asm/kvm_ppc.h>
Hollis Blanchard73e75b412008-12-02 15:51:57 -060031#include "timing.h"
Hollis Blanchardd9fbd032008-11-05 09:36:13 -060032#include <asm/cacheflush.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050033
Hollis Blanchard75f74f02008-11-05 09:36:16 -060034#include "booke.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050035
Hollis Blanchardd9fbd032008-11-05 09:36:13 -060036unsigned long kvmppc_booke_handlers;
37
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050038#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
39#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
40
41struct kvm_stats_debugfs_item debugfs_entries[] = {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050042 { "mmio", VCPU_STAT(mmio_exits) },
43 { "dcr", VCPU_STAT(dcr_exits) },
44 { "sig", VCPU_STAT(signal_exits) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050045 { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
46 { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
47 { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
48 { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
49 { "sysc", VCPU_STAT(syscall_exits) },
50 { "isi", VCPU_STAT(isi_exits) },
51 { "dsi", VCPU_STAT(dsi_exits) },
52 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
53 { "dec", VCPU_STAT(dec_exits) },
54 { "ext_intr", VCPU_STAT(ext_intr_exits) },
Hollis Blanchard45c5eb62008-04-25 17:55:49 -050055 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050056 { NULL }
57};
58
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050059/* TODO: use vcpu_printf() */
60void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
61{
62 int i;
63
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060064 printk("pc: %08lx msr: %08lx\n", vcpu->arch.pc, vcpu->arch.msr);
65 printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
66 printk("srr0: %08lx srr1: %08lx\n", vcpu->arch.srr0, vcpu->arch.srr1);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050067
68 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
69
70 for (i = 0; i < 32; i += 4) {
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -060071 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050072 vcpu->arch.gpr[i],
73 vcpu->arch.gpr[i+1],
74 vcpu->arch.gpr[i+2],
75 vcpu->arch.gpr[i+3]);
76 }
77}
78
Hollis Blanchardd4cf3892008-11-05 09:36:23 -060079static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
80 unsigned int priority)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060081{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060082 set_bit(priority, &vcpu->arch.pending_exceptions);
83}
84
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060085void kvmppc_core_queue_program(struct kvm_vcpu *vcpu)
86{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -060087 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060088}
89
90void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
91{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -060092 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060093}
94
95int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
96{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -060097 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060098}
99
100void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
101 struct kvm_interrupt *irq)
102{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600103 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_EXTERNAL);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600104}
105
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600106/* Deliver the interrupt of the corresponding priority, if possible. */
107static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
108 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500109{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600110 int allowed = 0;
111 ulong msr_mask;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500112
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600113 switch (priority) {
114 case BOOKE_IRQPRIO_PROGRAM:
115 case BOOKE_IRQPRIO_DTLB_MISS:
116 case BOOKE_IRQPRIO_ITLB_MISS:
117 case BOOKE_IRQPRIO_SYSCALL:
118 case BOOKE_IRQPRIO_DATA_STORAGE:
119 case BOOKE_IRQPRIO_INST_STORAGE:
120 case BOOKE_IRQPRIO_FP_UNAVAIL:
121 case BOOKE_IRQPRIO_AP_UNAVAIL:
122 case BOOKE_IRQPRIO_ALIGNMENT:
123 allowed = 1;
124 msr_mask = MSR_CE|MSR_ME|MSR_DE;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500125 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600126 case BOOKE_IRQPRIO_CRITICAL:
127 case BOOKE_IRQPRIO_WATCHDOG:
128 allowed = vcpu->arch.msr & MSR_CE;
129 msr_mask = MSR_ME;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500130 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600131 case BOOKE_IRQPRIO_MACHINE_CHECK:
132 allowed = vcpu->arch.msr & MSR_ME;
133 msr_mask = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500134 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600135 case BOOKE_IRQPRIO_EXTERNAL:
136 case BOOKE_IRQPRIO_DECREMENTER:
137 case BOOKE_IRQPRIO_FIT:
138 allowed = vcpu->arch.msr & MSR_EE;
139 msr_mask = MSR_CE|MSR_ME|MSR_DE;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500140 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600141 case BOOKE_IRQPRIO_DEBUG:
142 allowed = vcpu->arch.msr & MSR_DE;
143 msr_mask = MSR_ME;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500144 break;
145 }
146
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600147 if (allowed) {
148 vcpu->arch.srr0 = vcpu->arch.pc;
149 vcpu->arch.srr1 = vcpu->arch.msr;
150 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
151 kvmppc_set_msr(vcpu, vcpu->arch.msr & msr_mask);
152
153 clear_bit(priority, &vcpu->arch.pending_exceptions);
154 }
155
156 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500157}
158
159/* Check pending exceptions and deliver one, if possible. */
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600160void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500161{
162 unsigned long *pending = &vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500163 unsigned int priority;
164
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600165 priority = __ffs(*pending);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500166 while (priority <= BOOKE_MAX_INTERRUPT) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600167 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500168 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500169
170 priority = find_next_bit(pending,
171 BITS_PER_BYTE * sizeof(*pending),
172 priority + 1);
173 }
174}
175
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500176/**
177 * kvmppc_handle_exit
178 *
179 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
180 */
181int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
182 unsigned int exit_nr)
183{
184 enum emulation_result er;
185 int r = RESUME_HOST;
186
Hollis Blanchard73e75b412008-12-02 15:51:57 -0600187 /* update before a new last_exit_type is rewritten */
188 kvmppc_update_timing_stats(vcpu);
189
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500190 local_irq_enable();
191
192 run->exit_reason = KVM_EXIT_UNKNOWN;
193 run->ready_for_interrupt_injection = 1;
194
195 switch (exit_nr) {
196 case BOOKE_INTERRUPT_MACHINE_CHECK:
197 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
198 kvmppc_dump_vcpu(vcpu);
199 r = RESUME_HOST;
200 break;
201
202 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600203 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -0600204 if (need_resched())
205 cond_resched();
206 r = RESUME_GUEST;
207 break;
208
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500209 case BOOKE_INTERRUPT_DECREMENTER:
210 /* Since we switched IVPR back to the host's value, the host
211 * handled this interrupt the moment we enabled interrupts.
212 * Now we just offer it a chance to reschedule the guest. */
Hollis Blanchard7b701592008-12-02 15:51:58 -0600213 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500214 if (need_resched())
215 cond_resched();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500216 r = RESUME_GUEST;
217 break;
218
219 case BOOKE_INTERRUPT_PROGRAM:
220 if (vcpu->arch.msr & MSR_PR) {
221 /* Program traps generated by user-level software must be handled
222 * by the guest kernel. */
223 vcpu->arch.esr = vcpu->arch.fault_esr;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600224 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500225 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600226 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500227 break;
228 }
229
230 er = kvmppc_emulate_instruction(run, vcpu);
231 switch (er) {
232 case EMULATE_DONE:
Hollis Blanchard73e75b412008-12-02 15:51:57 -0600233 /* don't overwrite subtypes, just account kvm_stats */
Hollis Blanchard7b701592008-12-02 15:51:58 -0600234 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500235 /* Future optimization: only reload non-volatiles if
236 * they were actually modified by emulation. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500237 r = RESUME_GUEST_NV;
238 break;
239 case EMULATE_DO_DCR:
240 run->exit_reason = KVM_EXIT_DCR;
241 r = RESUME_HOST;
242 break;
243 case EMULATE_FAIL:
244 /* XXX Deliver Program interrupt to guest. */
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -0600245 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500246 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
247 /* For debugging, encode the failing instruction and
248 * report it to userspace. */
249 run->hw.hardware_exit_reason = ~0ULL << 32;
250 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
251 r = RESUME_HOST;
252 break;
253 default:
254 BUG();
255 }
256 break;
257
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200258 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600259 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600260 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200261 r = RESUME_GUEST;
262 break;
263
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500264 case BOOKE_INTERRUPT_DATA_STORAGE:
265 vcpu->arch.dear = vcpu->arch.fault_dear;
266 vcpu->arch.esr = vcpu->arch.fault_esr;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600267 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600268 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500269 r = RESUME_GUEST;
270 break;
271
272 case BOOKE_INTERRUPT_INST_STORAGE:
273 vcpu->arch.esr = vcpu->arch.fault_esr;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600274 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600275 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500276 r = RESUME_GUEST;
277 break;
278
279 case BOOKE_INTERRUPT_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600280 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600281 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500282 r = RESUME_GUEST;
283 break;
284
285 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500286 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600287 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600288 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500289 gfn_t gfn;
290
291 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600292 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600293 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500294 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600295 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500296 vcpu->arch.dear = vcpu->arch.fault_dear;
297 vcpu->arch.esr = vcpu->arch.fault_esr;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600298 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500299 r = RESUME_GUEST;
300 break;
301 }
302
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600303 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600304 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500305
306 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
307 /* The guest TLB had a mapping, but the shadow TLB
308 * didn't, and it is RAM. This could be because:
309 * a) the entry is mapping the host kernel, or
310 * b) the guest used a large mapping which we're faking
311 * Either way, we need to satisfy the fault without
312 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600313 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600314 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500315 r = RESUME_GUEST;
316 } else {
317 /* Guest has mapped and accessed a page which is not
318 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600319 vcpu->arch.paddr_accessed = gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500320 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600321 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500322 }
323
324 break;
325 }
326
327 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500328 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -0600329 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500330 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600331 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500332
333 r = RESUME_GUEST;
334
335 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600336 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600337 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500338 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600339 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600340 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500341 break;
342 }
343
Hollis Blanchard7b701592008-12-02 15:51:58 -0600344 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500345
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600346 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -0600347 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500348
349 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
350 /* The guest TLB had a mapping, but the shadow TLB
351 * didn't. This could be because:
352 * a) the entry is mapping the host kernel, or
353 * b) the guest used a large mapping which we're faking
354 * Either way, we need to satisfy the fault without
355 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600356 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500357 } else {
358 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600359 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500360 }
361
362 break;
363 }
364
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500365 case BOOKE_INTERRUPT_DEBUG: {
366 u32 dbsr;
367
368 vcpu->arch.pc = mfspr(SPRN_CSRR0);
369
370 /* clear IAC events in DBSR register */
371 dbsr = mfspr(SPRN_DBSR);
372 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
373 mtspr(SPRN_DBSR, dbsr);
374
375 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600376 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500377 r = RESUME_HOST;
378 break;
379 }
380
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500381 default:
382 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
383 BUG();
384 }
385
386 local_irq_disable();
387
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600388 kvmppc_core_deliver_interrupts(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500389
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500390 if (!(r & RESUME_HOST)) {
391 /* To avoid clobbering exit_reason, only check for signals if
392 * we aren't already exiting to userspace for some other
393 * reason. */
394 if (signal_pending(current)) {
395 run->exit_reason = KVM_EXIT_INTR;
396 r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600397 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500398 }
399 }
400
401 return r;
402}
403
404/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
405int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
406{
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500407 vcpu->arch.pc = 0;
408 vcpu->arch.msr = 0;
409 vcpu->arch.gpr[1] = (16<<20) - 8; /* -8 for the callee-save LR slot */
410
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500411 vcpu->arch.shadow_pid = 1;
412
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500413 /* Eye-catching number so we know if the guest takes an interrupt
414 * before it's programmed its own IVPR. */
415 vcpu->arch.ivpr = 0x55550000;
416
Hollis Blanchard73e75b412008-12-02 15:51:57 -0600417 kvmppc_init_timing_stats(vcpu);
418
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600419 return kvmppc_core_vcpu_setup(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500420}
421
422int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
423{
424 int i;
425
426 regs->pc = vcpu->arch.pc;
427 regs->cr = vcpu->arch.cr;
428 regs->ctr = vcpu->arch.ctr;
429 regs->lr = vcpu->arch.lr;
430 regs->xer = vcpu->arch.xer;
431 regs->msr = vcpu->arch.msr;
432 regs->srr0 = vcpu->arch.srr0;
433 regs->srr1 = vcpu->arch.srr1;
434 regs->pid = vcpu->arch.pid;
435 regs->sprg0 = vcpu->arch.sprg0;
436 regs->sprg1 = vcpu->arch.sprg1;
437 regs->sprg2 = vcpu->arch.sprg2;
438 regs->sprg3 = vcpu->arch.sprg3;
439 regs->sprg5 = vcpu->arch.sprg4;
440 regs->sprg6 = vcpu->arch.sprg5;
441 regs->sprg7 = vcpu->arch.sprg6;
442
443 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
444 regs->gpr[i] = vcpu->arch.gpr[i];
445
446 return 0;
447}
448
449int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
450{
451 int i;
452
453 vcpu->arch.pc = regs->pc;
454 vcpu->arch.cr = regs->cr;
455 vcpu->arch.ctr = regs->ctr;
456 vcpu->arch.lr = regs->lr;
457 vcpu->arch.xer = regs->xer;
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -0600458 kvmppc_set_msr(vcpu, regs->msr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500459 vcpu->arch.srr0 = regs->srr0;
460 vcpu->arch.srr1 = regs->srr1;
461 vcpu->arch.sprg0 = regs->sprg0;
462 vcpu->arch.sprg1 = regs->sprg1;
463 vcpu->arch.sprg2 = regs->sprg2;
464 vcpu->arch.sprg3 = regs->sprg3;
465 vcpu->arch.sprg5 = regs->sprg4;
466 vcpu->arch.sprg6 = regs->sprg5;
467 vcpu->arch.sprg7 = regs->sprg6;
468
469 for (i = 0; i < ARRAY_SIZE(vcpu->arch.gpr); i++)
470 vcpu->arch.gpr[i] = regs->gpr[i];
471
472 return 0;
473}
474
475int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
476 struct kvm_sregs *sregs)
477{
478 return -ENOTSUPP;
479}
480
481int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
482 struct kvm_sregs *sregs)
483{
484 return -ENOTSUPP;
485}
486
487int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
488{
489 return -ENOTSUPP;
490}
491
492int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
493{
494 return -ENOTSUPP;
495}
496
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500497int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
498 struct kvm_translation *tr)
499{
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600500 return kvmppc_core_vcpu_translate(vcpu, tr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500501}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600502
Hollis Blancharddb93f572008-11-05 09:36:18 -0600503int kvmppc_booke_init(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600504{
505 unsigned long ivor[16];
506 unsigned long max_ivor = 0;
507 int i;
508
509 /* We install our own exception handlers by hijacking IVPR. IVPR must
510 * be 16-bit aligned, so we need a 64KB allocation. */
511 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
512 VCPU_SIZE_ORDER);
513 if (!kvmppc_booke_handlers)
514 return -ENOMEM;
515
516 /* XXX make sure our handlers are smaller than Linux's */
517
518 /* Copy our interrupt handlers to match host IVORs. That way we don't
519 * have to swap the IVORs on every guest/host transition. */
520 ivor[0] = mfspr(SPRN_IVOR0);
521 ivor[1] = mfspr(SPRN_IVOR1);
522 ivor[2] = mfspr(SPRN_IVOR2);
523 ivor[3] = mfspr(SPRN_IVOR3);
524 ivor[4] = mfspr(SPRN_IVOR4);
525 ivor[5] = mfspr(SPRN_IVOR5);
526 ivor[6] = mfspr(SPRN_IVOR6);
527 ivor[7] = mfspr(SPRN_IVOR7);
528 ivor[8] = mfspr(SPRN_IVOR8);
529 ivor[9] = mfspr(SPRN_IVOR9);
530 ivor[10] = mfspr(SPRN_IVOR10);
531 ivor[11] = mfspr(SPRN_IVOR11);
532 ivor[12] = mfspr(SPRN_IVOR12);
533 ivor[13] = mfspr(SPRN_IVOR13);
534 ivor[14] = mfspr(SPRN_IVOR14);
535 ivor[15] = mfspr(SPRN_IVOR15);
536
537 for (i = 0; i < 16; i++) {
538 if (ivor[i] > max_ivor)
539 max_ivor = ivor[i];
540
541 memcpy((void *)kvmppc_booke_handlers + ivor[i],
542 kvmppc_handlers_start + i * kvmppc_handler_len,
543 kvmppc_handler_len);
544 }
545 flush_icache_range(kvmppc_booke_handlers,
546 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
547
Hollis Blancharddb93f572008-11-05 09:36:18 -0600548 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600549}
550
Hollis Blancharddb93f572008-11-05 09:36:18 -0600551void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600552{
553 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
554 kvm_exit();
555}