blob: e283e44e9f16b408a28c2b5b2b7aeb4197693584 [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 Blanchard73e75b42008-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,
Alexander Graf8e5b26b2010-01-08 02:58:01 +010072 kvmppc_get_gpr(vcpu, i),
73 kvmppc_get_gpr(vcpu, i+1),
74 kvmppc_get_gpr(vcpu, i+2),
75 kvmppc_get_gpr(vcpu, i+3));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050076 }
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
Alexander Graf25a8a022010-01-08 02:58:07 +010085void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong flags)
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060086{
Alexander Graf25a8a022010-01-08 02:58:07 +010087 /* BookE does flags in ESR, so ignore those we get here */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -060088 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060089}
90
91void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
92{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -060093 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060094}
95
96int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
97{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -060098 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -060099}
100
Alexander Graf7706664d2009-12-21 20:21:24 +0100101void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
102{
103 clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
104}
105
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600106void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
107 struct kvm_interrupt *irq)
108{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600109 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_EXTERNAL);
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600110}
111
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600112/* Deliver the interrupt of the corresponding priority, if possible. */
113static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
114 unsigned int priority)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500115{
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600116 int allowed = 0;
117 ulong msr_mask;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500118
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600119 switch (priority) {
120 case BOOKE_IRQPRIO_PROGRAM:
121 case BOOKE_IRQPRIO_DTLB_MISS:
122 case BOOKE_IRQPRIO_ITLB_MISS:
123 case BOOKE_IRQPRIO_SYSCALL:
124 case BOOKE_IRQPRIO_DATA_STORAGE:
125 case BOOKE_IRQPRIO_INST_STORAGE:
126 case BOOKE_IRQPRIO_FP_UNAVAIL:
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600127 case BOOKE_IRQPRIO_SPE_UNAVAIL:
128 case BOOKE_IRQPRIO_SPE_FP_DATA:
129 case BOOKE_IRQPRIO_SPE_FP_ROUND:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600130 case BOOKE_IRQPRIO_AP_UNAVAIL:
131 case BOOKE_IRQPRIO_ALIGNMENT:
132 allowed = 1;
133 msr_mask = MSR_CE|MSR_ME|MSR_DE;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500134 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600135 case BOOKE_IRQPRIO_CRITICAL:
136 case BOOKE_IRQPRIO_WATCHDOG:
137 allowed = vcpu->arch.msr & MSR_CE;
138 msr_mask = MSR_ME;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500139 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600140 case BOOKE_IRQPRIO_MACHINE_CHECK:
141 allowed = vcpu->arch.msr & MSR_ME;
142 msr_mask = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500143 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600144 case BOOKE_IRQPRIO_EXTERNAL:
145 case BOOKE_IRQPRIO_DECREMENTER:
146 case BOOKE_IRQPRIO_FIT:
147 allowed = vcpu->arch.msr & MSR_EE;
148 msr_mask = MSR_CE|MSR_ME|MSR_DE;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500149 break;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600150 case BOOKE_IRQPRIO_DEBUG:
151 allowed = vcpu->arch.msr & MSR_DE;
152 msr_mask = MSR_ME;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500153 break;
154 }
155
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600156 if (allowed) {
157 vcpu->arch.srr0 = vcpu->arch.pc;
158 vcpu->arch.srr1 = vcpu->arch.msr;
159 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
160 kvmppc_set_msr(vcpu, vcpu->arch.msr & msr_mask);
161
162 clear_bit(priority, &vcpu->arch.pending_exceptions);
163 }
164
165 return allowed;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500166}
167
168/* Check pending exceptions and deliver one, if possible. */
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600169void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500170{
171 unsigned long *pending = &vcpu->arch.pending_exceptions;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500172 unsigned int priority;
173
Hollis Blanchard9ab80842008-11-05 09:36:22 -0600174 priority = __ffs(*pending);
Hollis Blanchardbdc89f12009-01-03 16:23:12 -0600175 while (priority <= BOOKE_IRQPRIO_MAX) {
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600176 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500177 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500178
179 priority = find_next_bit(pending,
180 BITS_PER_BYTE * sizeof(*pending),
181 priority + 1);
182 }
183}
184
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500185/**
186 * kvmppc_handle_exit
187 *
188 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
189 */
190int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
191 unsigned int exit_nr)
192{
193 enum emulation_result er;
194 int r = RESUME_HOST;
195
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600196 /* update before a new last_exit_type is rewritten */
197 kvmppc_update_timing_stats(vcpu);
198
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500199 local_irq_enable();
200
201 run->exit_reason = KVM_EXIT_UNKNOWN;
202 run->ready_for_interrupt_injection = 1;
203
204 switch (exit_nr) {
205 case BOOKE_INTERRUPT_MACHINE_CHECK:
206 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
207 kvmppc_dump_vcpu(vcpu);
208 r = RESUME_HOST;
209 break;
210
211 case BOOKE_INTERRUPT_EXTERNAL:
Hollis Blanchard7b701592008-12-02 15:51:58 -0600212 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
Hollis Blanchard1b6766c2008-11-05 09:36:21 -0600213 if (need_resched())
214 cond_resched();
215 r = RESUME_GUEST;
216 break;
217
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500218 case BOOKE_INTERRUPT_DECREMENTER:
219 /* Since we switched IVPR back to the host's value, the host
220 * handled this interrupt the moment we enabled interrupts.
221 * Now we just offer it a chance to reschedule the guest. */
Hollis Blanchard7b701592008-12-02 15:51:58 -0600222 kvmppc_account_exit(vcpu, DEC_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500223 if (need_resched())
224 cond_resched();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500225 r = RESUME_GUEST;
226 break;
227
228 case BOOKE_INTERRUPT_PROGRAM:
229 if (vcpu->arch.msr & MSR_PR) {
230 /* Program traps generated by user-level software must be handled
231 * by the guest kernel. */
232 vcpu->arch.esr = vcpu->arch.fault_esr;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600233 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500234 r = RESUME_GUEST;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600235 kvmppc_account_exit(vcpu, USR_PR_INST);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500236 break;
237 }
238
239 er = kvmppc_emulate_instruction(run, vcpu);
240 switch (er) {
241 case EMULATE_DONE:
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600242 /* don't overwrite subtypes, just account kvm_stats */
Hollis Blanchard7b701592008-12-02 15:51:58 -0600243 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500244 /* Future optimization: only reload non-volatiles if
245 * they were actually modified by emulation. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500246 r = RESUME_GUEST_NV;
247 break;
248 case EMULATE_DO_DCR:
249 run->exit_reason = KVM_EXIT_DCR;
250 r = RESUME_HOST;
251 break;
252 case EMULATE_FAIL:
253 /* XXX Deliver Program interrupt to guest. */
Hollis Blanchard5cf8ca22008-11-05 09:36:19 -0600254 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500255 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
256 /* For debugging, encode the failing instruction and
257 * report it to userspace. */
258 run->hw.hardware_exit_reason = ~0ULL << 32;
259 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
260 r = RESUME_HOST;
261 break;
262 default:
263 BUG();
264 }
265 break;
266
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200267 case BOOKE_INTERRUPT_FP_UNAVAIL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600268 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600269 kvmppc_account_exit(vcpu, FP_UNAVAIL);
Christian Ehrhardtde368dc2008-04-29 18:18:23 +0200270 r = RESUME_GUEST;
271 break;
272
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600273 case BOOKE_INTERRUPT_SPE_UNAVAIL:
274 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_UNAVAIL);
275 r = RESUME_GUEST;
276 break;
277
278 case BOOKE_INTERRUPT_SPE_FP_DATA:
279 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
280 r = RESUME_GUEST;
281 break;
282
283 case BOOKE_INTERRUPT_SPE_FP_ROUND:
284 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
285 r = RESUME_GUEST;
286 break;
287
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500288 case BOOKE_INTERRUPT_DATA_STORAGE:
289 vcpu->arch.dear = vcpu->arch.fault_dear;
290 vcpu->arch.esr = vcpu->arch.fault_esr;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600291 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600292 kvmppc_account_exit(vcpu, DSI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500293 r = RESUME_GUEST;
294 break;
295
296 case BOOKE_INTERRUPT_INST_STORAGE:
297 vcpu->arch.esr = vcpu->arch.fault_esr;
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600298 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600299 kvmppc_account_exit(vcpu, ISI_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500300 r = RESUME_GUEST;
301 break;
302
303 case BOOKE_INTERRUPT_SYSCALL:
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600304 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600305 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500306 r = RESUME_GUEST;
307 break;
308
309 case BOOKE_INTERRUPT_DTLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500310 unsigned long eaddr = vcpu->arch.fault_dear;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600311 int gtlb_index;
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600312 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500313 gfn_t gfn;
314
315 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600316 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600317 if (gtlb_index < 0) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500318 /* The guest didn't have a mapping for it. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600319 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500320 vcpu->arch.dear = vcpu->arch.fault_dear;
321 vcpu->arch.esr = vcpu->arch.fault_esr;
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600322 kvmppc_mmu_dtlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600323 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500324 r = RESUME_GUEST;
325 break;
326 }
327
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600328 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600329 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500330
331 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
332 /* The guest TLB had a mapping, but the shadow TLB
333 * didn't, and it is RAM. This could be because:
334 * a) the entry is mapping the host kernel, or
335 * b) the guest used a large mapping which we're faking
336 * Either way, we need to satisfy the fault without
337 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600338 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600339 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500340 r = RESUME_GUEST;
341 } else {
342 /* Guest has mapped and accessed a page which is not
343 * actually RAM. */
Hollis Blanchard475e7cd2009-01-03 16:23:00 -0600344 vcpu->arch.paddr_accessed = gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500345 r = kvmppc_emulate_mmio(run, vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600346 kvmppc_account_exit(vcpu, MMIO_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500347 }
348
349 break;
350 }
351
352 case BOOKE_INTERRUPT_ITLB_MISS: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500353 unsigned long eaddr = vcpu->arch.pc;
Hollis Blanchard89168612008-12-02 15:51:53 -0600354 gpa_t gpaddr;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500355 gfn_t gfn;
Hollis Blanchard7924bd42008-12-02 15:51:55 -0600356 int gtlb_index;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500357
358 r = RESUME_GUEST;
359
360 /* Check the guest TLB. */
Hollis Blanchardfa86b8d2009-01-03 16:23:03 -0600361 gtlb_index = kvmppc_mmu_itlb_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. */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600364 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
Hollis Blanchardb52a6382009-01-03 16:23:11 -0600365 kvmppc_mmu_itlb_miss(vcpu);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600366 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500367 break;
368 }
369
Hollis Blanchard7b701592008-12-02 15:51:58 -0600370 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500371
Hollis Blanchardbe8d1ca2009-01-03 16:23:02 -0600372 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
Hollis Blanchard89168612008-12-02 15:51:53 -0600373 gfn = gpaddr >> PAGE_SHIFT;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500374
375 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
376 /* The guest TLB had a mapping, but the shadow TLB
377 * didn't. This could be because:
378 * a) the entry is mapping the host kernel, or
379 * b) the guest used a large mapping which we're faking
380 * Either way, we need to satisfy the fault without
381 * invoking the guest. */
Hollis Blanchard58a96212009-01-03 16:23:01 -0600382 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500383 } else {
384 /* Guest mapped and leaped at non-RAM! */
Hollis Blanchardd4cf3892008-11-05 09:36:23 -0600385 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500386 }
387
388 break;
389 }
390
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500391 case BOOKE_INTERRUPT_DEBUG: {
392 u32 dbsr;
393
394 vcpu->arch.pc = mfspr(SPRN_CSRR0);
395
396 /* clear IAC events in DBSR register */
397 dbsr = mfspr(SPRN_DBSR);
398 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
399 mtspr(SPRN_DBSR, dbsr);
400
401 run->exit_reason = KVM_EXIT_DEBUG;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600402 kvmppc_account_exit(vcpu, DEBUG_EXITS);
Hollis Blanchard6a0ab732008-07-25 13:54:49 -0500403 r = RESUME_HOST;
404 break;
405 }
406
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500407 default:
408 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
409 BUG();
410 }
411
412 local_irq_disable();
413
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600414 kvmppc_core_deliver_interrupts(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500415
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500416 if (!(r & RESUME_HOST)) {
417 /* To avoid clobbering exit_reason, only check for signals if
418 * we aren't already exiting to userspace for some other
419 * reason. */
420 if (signal_pending(current)) {
421 run->exit_reason = KVM_EXIT_INTR;
422 r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
Hollis Blanchard7b701592008-12-02 15:51:58 -0600423 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500424 }
425 }
426
427 return r;
428}
429
430/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
431int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
432{
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500433 vcpu->arch.pc = 0;
434 vcpu->arch.msr = 0;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100435 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500436
Hollis Blanchard49dd2c42008-07-25 13:54:53 -0500437 vcpu->arch.shadow_pid = 1;
438
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500439 /* Eye-catching number so we know if the guest takes an interrupt
440 * before it's programmed its own IVPR. */
441 vcpu->arch.ivpr = 0x55550000;
442
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600443 kvmppc_init_timing_stats(vcpu);
444
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600445 return kvmppc_core_vcpu_setup(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500446}
447
448int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
449{
450 int i;
451
452 regs->pc = vcpu->arch.pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100453 regs->cr = kvmppc_get_cr(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500454 regs->ctr = vcpu->arch.ctr;
455 regs->lr = vcpu->arch.lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100456 regs->xer = kvmppc_get_xer(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500457 regs->msr = vcpu->arch.msr;
458 regs->srr0 = vcpu->arch.srr0;
459 regs->srr1 = vcpu->arch.srr1;
460 regs->pid = vcpu->arch.pid;
461 regs->sprg0 = vcpu->arch.sprg0;
462 regs->sprg1 = vcpu->arch.sprg1;
463 regs->sprg2 = vcpu->arch.sprg2;
464 regs->sprg3 = vcpu->arch.sprg3;
465 regs->sprg5 = vcpu->arch.sprg4;
466 regs->sprg6 = vcpu->arch.sprg5;
467 regs->sprg7 = vcpu->arch.sprg6;
468
469 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100470 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500471
472 return 0;
473}
474
475int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
476{
477 int i;
478
479 vcpu->arch.pc = regs->pc;
Alexander Graf992b5b22010-01-08 02:58:02 +0100480 kvmppc_set_cr(vcpu, regs->cr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500481 vcpu->arch.ctr = regs->ctr;
482 vcpu->arch.lr = regs->lr;
Alexander Graf992b5b22010-01-08 02:58:02 +0100483 kvmppc_set_xer(vcpu, regs->xer);
Hollis Blanchardb8fd68a2008-11-05 09:36:20 -0600484 kvmppc_set_msr(vcpu, regs->msr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500485 vcpu->arch.srr0 = regs->srr0;
486 vcpu->arch.srr1 = regs->srr1;
487 vcpu->arch.sprg0 = regs->sprg0;
488 vcpu->arch.sprg1 = regs->sprg1;
489 vcpu->arch.sprg2 = regs->sprg2;
490 vcpu->arch.sprg3 = regs->sprg3;
491 vcpu->arch.sprg5 = regs->sprg4;
492 vcpu->arch.sprg6 = regs->sprg5;
493 vcpu->arch.sprg7 = regs->sprg6;
494
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100495 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
496 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500497
498 return 0;
499}
500
501int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
502 struct kvm_sregs *sregs)
503{
504 return -ENOTSUPP;
505}
506
507int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
508 struct kvm_sregs *sregs)
509{
510 return -ENOTSUPP;
511}
512
513int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
514{
515 return -ENOTSUPP;
516}
517
518int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
519{
520 return -ENOTSUPP;
521}
522
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500523int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
524 struct kvm_translation *tr)
525{
Hollis Blanchard5cbb5102008-11-05 09:36:17 -0600526 return kvmppc_core_vcpu_translate(vcpu, tr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500527}
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600528
Alexander Graf4e755752009-10-30 05:47:01 +0000529int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
530{
531 return -ENOTSUPP;
532}
533
Stephen Rothwell2986b8c2009-06-02 11:46:14 +1000534int __init kvmppc_booke_init(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600535{
536 unsigned long ivor[16];
537 unsigned long max_ivor = 0;
538 int i;
539
540 /* We install our own exception handlers by hijacking IVPR. IVPR must
541 * be 16-bit aligned, so we need a 64KB allocation. */
542 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
543 VCPU_SIZE_ORDER);
544 if (!kvmppc_booke_handlers)
545 return -ENOMEM;
546
547 /* XXX make sure our handlers are smaller than Linux's */
548
549 /* Copy our interrupt handlers to match host IVORs. That way we don't
550 * have to swap the IVORs on every guest/host transition. */
551 ivor[0] = mfspr(SPRN_IVOR0);
552 ivor[1] = mfspr(SPRN_IVOR1);
553 ivor[2] = mfspr(SPRN_IVOR2);
554 ivor[3] = mfspr(SPRN_IVOR3);
555 ivor[4] = mfspr(SPRN_IVOR4);
556 ivor[5] = mfspr(SPRN_IVOR5);
557 ivor[6] = mfspr(SPRN_IVOR6);
558 ivor[7] = mfspr(SPRN_IVOR7);
559 ivor[8] = mfspr(SPRN_IVOR8);
560 ivor[9] = mfspr(SPRN_IVOR9);
561 ivor[10] = mfspr(SPRN_IVOR10);
562 ivor[11] = mfspr(SPRN_IVOR11);
563 ivor[12] = mfspr(SPRN_IVOR12);
564 ivor[13] = mfspr(SPRN_IVOR13);
565 ivor[14] = mfspr(SPRN_IVOR14);
566 ivor[15] = mfspr(SPRN_IVOR15);
567
568 for (i = 0; i < 16; i++) {
569 if (ivor[i] > max_ivor)
570 max_ivor = ivor[i];
571
572 memcpy((void *)kvmppc_booke_handlers + ivor[i],
573 kvmppc_handlers_start + i * kvmppc_handler_len,
574 kvmppc_handler_len);
575 }
576 flush_icache_range(kvmppc_booke_handlers,
577 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
578
Hollis Blancharddb93f572008-11-05 09:36:18 -0600579 return 0;
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600580}
581
Hollis Blancharddb93f572008-11-05 09:36:18 -0600582void __exit kvmppc_booke_exit(void)
Hollis Blanchardd9fbd032008-11-05 09:36:13 -0600583{
584 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
585 kvm_exit();
586}