KVM: PPC: BookE: Add some more trace points
[linux-2.6-block.git] / arch / powerpc / kvm / booke.c
CommitLineData
bbf45ba5
HB
1/*
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
4cd35f67 16 * Copyright 2010-2011 Freescale Semiconductor, Inc.
bbf45ba5
HB
17 *
18 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
19 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
d30f6e48
SW
20 * Scott Wood <scottwood@freescale.com>
21 * Varun Sethi <varun.sethi@freescale.com>
bbf45ba5
HB
22 */
23
24#include <linux/errno.h>
25#include <linux/err.h>
26#include <linux/kvm_host.h>
5a0e3ad6 27#include <linux/gfp.h>
bbf45ba5
HB
28#include <linux/module.h>
29#include <linux/vmalloc.h>
30#include <linux/fs.h>
7924bd41 31
bbf45ba5
HB
32#include <asm/cputable.h>
33#include <asm/uaccess.h>
34#include <asm/kvm_ppc.h>
d9fbd03d 35#include <asm/cacheflush.h>
d30f6e48
SW
36#include <asm/dbell.h>
37#include <asm/hw_irq.h>
38#include <asm/irq.h>
bbf45ba5 39
d30f6e48 40#include "timing.h"
75f74f0d 41#include "booke.h"
97c95059 42#include "trace.h"
bbf45ba5 43
d9fbd03d
HB
44unsigned long kvmppc_booke_handlers;
45
bbf45ba5
HB
46#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
47#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
48
49struct kvm_stats_debugfs_item debugfs_entries[] = {
bbf45ba5
HB
50 { "mmio", VCPU_STAT(mmio_exits) },
51 { "dcr", VCPU_STAT(dcr_exits) },
52 { "sig", VCPU_STAT(signal_exits) },
bbf45ba5
HB
53 { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
54 { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
55 { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
56 { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
57 { "sysc", VCPU_STAT(syscall_exits) },
58 { "isi", VCPU_STAT(isi_exits) },
59 { "dsi", VCPU_STAT(dsi_exits) },
60 { "inst_emu", VCPU_STAT(emulated_inst_exits) },
61 { "dec", VCPU_STAT(dec_exits) },
62 { "ext_intr", VCPU_STAT(ext_intr_exits) },
45c5eb67 63 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
d30f6e48
SW
64 { "doorbell", VCPU_STAT(dbell_exits) },
65 { "guest doorbell", VCPU_STAT(gdbell_exits) },
cf1c5ca4 66 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
bbf45ba5
HB
67 { NULL }
68};
69
bbf45ba5
HB
70/* TODO: use vcpu_printf() */
71void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
72{
73 int i;
74
666e7252 75 printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
5cf8ca22 76 printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
de7906c3
AG
77 printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
78 vcpu->arch.shared->srr1);
bbf45ba5
HB
79
80 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
81
82 for (i = 0; i < 32; i += 4) {
5cf8ca22 83 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
8e5b26b5
AG
84 kvmppc_get_gpr(vcpu, i),
85 kvmppc_get_gpr(vcpu, i+1),
86 kvmppc_get_gpr(vcpu, i+2),
87 kvmppc_get_gpr(vcpu, i+3));
bbf45ba5
HB
88 }
89}
90
4cd35f67
SW
91#ifdef CONFIG_SPE
92void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
93{
94 preempt_disable();
95 enable_kernel_spe();
96 kvmppc_save_guest_spe(vcpu);
97 vcpu->arch.shadow_msr &= ~MSR_SPE;
98 preempt_enable();
99}
100
101static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
102{
103 preempt_disable();
104 enable_kernel_spe();
105 kvmppc_load_guest_spe(vcpu);
106 vcpu->arch.shadow_msr |= MSR_SPE;
107 preempt_enable();
108}
109
110static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
111{
112 if (vcpu->arch.shared->msr & MSR_SPE) {
113 if (!(vcpu->arch.shadow_msr & MSR_SPE))
114 kvmppc_vcpu_enable_spe(vcpu);
115 } else if (vcpu->arch.shadow_msr & MSR_SPE) {
116 kvmppc_vcpu_disable_spe(vcpu);
117 }
118}
119#else
120static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
121{
122}
123#endif
124
dd9ebf1f
LY
125/*
126 * Helper function for "full" MSR writes. No need to call this if only
127 * EE/CE/ME/DE/RI are changing.
128 */
4cd35f67
SW
129void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
130{
dd9ebf1f 131 u32 old_msr = vcpu->arch.shared->msr;
4cd35f67 132
d30f6e48
SW
133#ifdef CONFIG_KVM_BOOKE_HV
134 new_msr |= MSR_GS;
135#endif
136
4cd35f67
SW
137 vcpu->arch.shared->msr = new_msr;
138
dd9ebf1f 139 kvmppc_mmu_msr_notify(vcpu, old_msr);
4cd35f67
SW
140 kvmppc_vcpu_sync_spe(vcpu);
141}
142
d4cf3892
HB
143static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
144 unsigned int priority)
9dd921cf 145{
6346046c 146 trace_kvm_booke_queue_irqprio(vcpu, priority);
9dd921cf
HB
147 set_bit(priority, &vcpu->arch.pending_exceptions);
148}
149
daf5e271
LY
150static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
151 ulong dear_flags, ulong esr_flags)
9dd921cf 152{
daf5e271
LY
153 vcpu->arch.queued_dear = dear_flags;
154 vcpu->arch.queued_esr = esr_flags;
155 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
156}
157
158static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
159 ulong dear_flags, ulong esr_flags)
160{
161 vcpu->arch.queued_dear = dear_flags;
162 vcpu->arch.queued_esr = esr_flags;
163 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
164}
165
166static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
167 ulong esr_flags)
168{
169 vcpu->arch.queued_esr = esr_flags;
170 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
171}
172
173void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
174{
175 vcpu->arch.queued_esr = esr_flags;
d4cf3892 176 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
9dd921cf
HB
177}
178
179void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
180{
d4cf3892 181 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
9dd921cf
HB
182}
183
184int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
185{
d4cf3892 186 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
9dd921cf
HB
187}
188
7706664d
AG
189void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
190{
191 clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
192}
193
9dd921cf
HB
194void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
195 struct kvm_interrupt *irq)
196{
c5335f17
AG
197 unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
198
199 if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
200 prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
201
202 kvmppc_booke_queue_irqprio(vcpu, prio);
9dd921cf
HB
203}
204
4496f974
AG
205void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu,
206 struct kvm_interrupt *irq)
207{
208 clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
c5335f17 209 clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
4496f974
AG
210}
211
d30f6e48
SW
212static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
213{
214#ifdef CONFIG_KVM_BOOKE_HV
215 mtspr(SPRN_GSRR0, srr0);
216 mtspr(SPRN_GSRR1, srr1);
217#else
218 vcpu->arch.shared->srr0 = srr0;
219 vcpu->arch.shared->srr1 = srr1;
220#endif
221}
222
223static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
224{
225 vcpu->arch.csrr0 = srr0;
226 vcpu->arch.csrr1 = srr1;
227}
228
229static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
230{
231 if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
232 vcpu->arch.dsrr0 = srr0;
233 vcpu->arch.dsrr1 = srr1;
234 } else {
235 set_guest_csrr(vcpu, srr0, srr1);
236 }
237}
238
239static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
240{
241 vcpu->arch.mcsrr0 = srr0;
242 vcpu->arch.mcsrr1 = srr1;
243}
244
245static unsigned long get_guest_dear(struct kvm_vcpu *vcpu)
246{
247#ifdef CONFIG_KVM_BOOKE_HV
248 return mfspr(SPRN_GDEAR);
249#else
250 return vcpu->arch.shared->dar;
251#endif
252}
253
254static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear)
255{
256#ifdef CONFIG_KVM_BOOKE_HV
257 mtspr(SPRN_GDEAR, dear);
258#else
259 vcpu->arch.shared->dar = dear;
260#endif
261}
262
263static unsigned long get_guest_esr(struct kvm_vcpu *vcpu)
264{
265#ifdef CONFIG_KVM_BOOKE_HV
266 return mfspr(SPRN_GESR);
267#else
268 return vcpu->arch.shared->esr;
269#endif
270}
271
272static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr)
273{
274#ifdef CONFIG_KVM_BOOKE_HV
275 mtspr(SPRN_GESR, esr);
276#else
277 vcpu->arch.shared->esr = esr;
278#endif
279}
280
d4cf3892
HB
281/* Deliver the interrupt of the corresponding priority, if possible. */
282static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
283 unsigned int priority)
bbf45ba5 284{
d4cf3892 285 int allowed = 0;
79300f8c 286 ulong msr_mask = 0;
daf5e271 287 bool update_esr = false, update_dear = false;
5c6cedf4
AG
288 ulong crit_raw = vcpu->arch.shared->critical;
289 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
290 bool crit;
c5335f17 291 bool keep_irq = false;
d30f6e48 292 enum int_class int_class;
5c6cedf4
AG
293
294 /* Truncate crit indicators in 32 bit mode */
295 if (!(vcpu->arch.shared->msr & MSR_SF)) {
296 crit_raw &= 0xffffffff;
297 crit_r1 &= 0xffffffff;
298 }
299
300 /* Critical section when crit == r1 */
301 crit = (crit_raw == crit_r1);
302 /* ... and we're in supervisor mode */
303 crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
d4cf3892 304
c5335f17
AG
305 if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
306 priority = BOOKE_IRQPRIO_EXTERNAL;
307 keep_irq = true;
308 }
309
d4cf3892 310 switch (priority) {
d4cf3892 311 case BOOKE_IRQPRIO_DTLB_MISS:
d4cf3892 312 case BOOKE_IRQPRIO_DATA_STORAGE:
daf5e271
LY
313 update_dear = true;
314 /* fall through */
d4cf3892 315 case BOOKE_IRQPRIO_INST_STORAGE:
daf5e271
LY
316 case BOOKE_IRQPRIO_PROGRAM:
317 update_esr = true;
318 /* fall through */
319 case BOOKE_IRQPRIO_ITLB_MISS:
320 case BOOKE_IRQPRIO_SYSCALL:
d4cf3892 321 case BOOKE_IRQPRIO_FP_UNAVAIL:
bb3a8a17
HB
322 case BOOKE_IRQPRIO_SPE_UNAVAIL:
323 case BOOKE_IRQPRIO_SPE_FP_DATA:
324 case BOOKE_IRQPRIO_SPE_FP_ROUND:
d4cf3892
HB
325 case BOOKE_IRQPRIO_AP_UNAVAIL:
326 case BOOKE_IRQPRIO_ALIGNMENT:
327 allowed = 1;
79300f8c 328 msr_mask = MSR_CE | MSR_ME | MSR_DE;
d30f6e48 329 int_class = INT_CLASS_NONCRIT;
bbf45ba5 330 break;
d4cf3892 331 case BOOKE_IRQPRIO_CRITICAL:
4ab96919 332 case BOOKE_IRQPRIO_DBELL_CRIT:
666e7252 333 allowed = vcpu->arch.shared->msr & MSR_CE;
d30f6e48 334 allowed = allowed && !crit;
79300f8c 335 msr_mask = MSR_ME;
d30f6e48 336 int_class = INT_CLASS_CRIT;
bbf45ba5 337 break;
d4cf3892 338 case BOOKE_IRQPRIO_MACHINE_CHECK:
666e7252 339 allowed = vcpu->arch.shared->msr & MSR_ME;
d30f6e48 340 allowed = allowed && !crit;
d30f6e48 341 int_class = INT_CLASS_MC;
bbf45ba5 342 break;
d4cf3892
HB
343 case BOOKE_IRQPRIO_DECREMENTER:
344 case BOOKE_IRQPRIO_FIT:
dfd4d47e
SW
345 keep_irq = true;
346 /* fall through */
347 case BOOKE_IRQPRIO_EXTERNAL:
4ab96919 348 case BOOKE_IRQPRIO_DBELL:
666e7252 349 allowed = vcpu->arch.shared->msr & MSR_EE;
5c6cedf4 350 allowed = allowed && !crit;
79300f8c 351 msr_mask = MSR_CE | MSR_ME | MSR_DE;
d30f6e48 352 int_class = INT_CLASS_NONCRIT;
bbf45ba5 353 break;
d4cf3892 354 case BOOKE_IRQPRIO_DEBUG:
666e7252 355 allowed = vcpu->arch.shared->msr & MSR_DE;
d30f6e48 356 allowed = allowed && !crit;
79300f8c 357 msr_mask = MSR_ME;
d30f6e48 358 int_class = INT_CLASS_CRIT;
bbf45ba5 359 break;
bbf45ba5
HB
360 }
361
d4cf3892 362 if (allowed) {
d30f6e48
SW
363 switch (int_class) {
364 case INT_CLASS_NONCRIT:
365 set_guest_srr(vcpu, vcpu->arch.pc,
366 vcpu->arch.shared->msr);
367 break;
368 case INT_CLASS_CRIT:
369 set_guest_csrr(vcpu, vcpu->arch.pc,
370 vcpu->arch.shared->msr);
371 break;
372 case INT_CLASS_DBG:
373 set_guest_dsrr(vcpu, vcpu->arch.pc,
374 vcpu->arch.shared->msr);
375 break;
376 case INT_CLASS_MC:
377 set_guest_mcsrr(vcpu, vcpu->arch.pc,
378 vcpu->arch.shared->msr);
379 break;
380 }
381
d4cf3892 382 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
daf5e271 383 if (update_esr == true)
d30f6e48 384 set_guest_esr(vcpu, vcpu->arch.queued_esr);
daf5e271 385 if (update_dear == true)
d30f6e48 386 set_guest_dear(vcpu, vcpu->arch.queued_dear);
666e7252 387 kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask);
bbf45ba5 388
c5335f17
AG
389 if (!keep_irq)
390 clear_bit(priority, &vcpu->arch.pending_exceptions);
bbf45ba5
HB
391 }
392
d30f6e48
SW
393#ifdef CONFIG_KVM_BOOKE_HV
394 /*
395 * If an interrupt is pending but masked, raise a guest doorbell
396 * so that we are notified when the guest enables the relevant
397 * MSR bit.
398 */
399 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
400 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
401 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
402 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
403 if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
404 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
405#endif
406
d4cf3892 407 return allowed;
bbf45ba5
HB
408}
409
dfd4d47e
SW
410static void update_timer_ints(struct kvm_vcpu *vcpu)
411{
412 if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
413 kvmppc_core_queue_dec(vcpu);
414 else
415 kvmppc_core_dequeue_dec(vcpu);
416}
417
c59a6a3e 418static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
bbf45ba5
HB
419{
420 unsigned long *pending = &vcpu->arch.pending_exceptions;
bbf45ba5
HB
421 unsigned int priority;
422
9ab80843 423 priority = __ffs(*pending);
8b3a00fc 424 while (priority < BOOKE_IRQPRIO_MAX) {
d4cf3892 425 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
bbf45ba5 426 break;
bbf45ba5
HB
427
428 priority = find_next_bit(pending,
429 BITS_PER_BYTE * sizeof(*pending),
430 priority + 1);
431 }
90bba358
AG
432
433 /* Tell the guest about our interrupt status */
29ac26ef 434 vcpu->arch.shared->int_pending = !!*pending;
bbf45ba5
HB
435}
436
c59a6a3e 437/* Check pending exceptions and deliver one, if possible. */
a8e4ef84 438int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
c59a6a3e 439{
a8e4ef84 440 int r = 0;
c59a6a3e
SW
441 WARN_ON_ONCE(!irqs_disabled());
442
443 kvmppc_core_check_exceptions(vcpu);
444
445 if (vcpu->arch.shared->msr & MSR_WE) {
446 local_irq_enable();
447 kvm_vcpu_block(vcpu);
966cd0f3 448 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
c59a6a3e
SW
449 local_irq_disable();
450
451 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
a8e4ef84 452 r = 1;
c59a6a3e 453 };
a8e4ef84
AG
454
455 return r;
456}
457
4ffc6356
AG
458static void kvmppc_check_requests(struct kvm_vcpu *vcpu)
459{
460 if (vcpu->requests) {
6346046c
AG
461 trace_kvm_check_requests(vcpu);
462
4ffc6356
AG
463 if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
464 update_timer_ints(vcpu);
862d31f7
AG
465#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
466 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
467 kvmppc_core_flush_tlb(vcpu);
468#endif
4ffc6356
AG
469 }
470}
471
a8e4ef84
AG
472/*
473 * Common checks before entering the guest world. Call with interrupts
474 * disabled.
475 *
476 * returns !0 if a signal is pending and check_signal is true
477 */
03660ba2 478static int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
a8e4ef84
AG
479{
480 int r = 0;
481
482 WARN_ON_ONCE(!irqs_disabled());
483 while (true) {
484 if (need_resched()) {
485 local_irq_enable();
486 cond_resched();
487 local_irq_disable();
488 continue;
489 }
490
03660ba2 491 if (signal_pending(current)) {
a8e4ef84
AG
492 r = 1;
493 break;
494 }
495
4ffc6356
AG
496 smp_mb();
497 if (vcpu->requests) {
498 /* Make sure we process requests preemptable */
499 local_irq_enable();
500 kvmppc_check_requests(vcpu);
501 local_irq_disable();
502 continue;
503 }
504
a8e4ef84
AG
505 if (kvmppc_core_prepare_to_enter(vcpu)) {
506 /* interrupts got enabled in between, so we
507 are back at square 1 */
508 continue;
509 }
510
d69c6436
AG
511 if (vcpu->mode == EXITING_GUEST_MODE) {
512 r = 1;
513 break;
514 }
515
516 /* Going into guest context! Yay! */
517 vcpu->mode = IN_GUEST_MODE;
518 smp_wmb();
519
a8e4ef84
AG
520 break;
521 }
522
523 return r;
c59a6a3e
SW
524}
525
df6909e5
PM
526int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
527{
528 int ret;
8fae845f
SW
529#ifdef CONFIG_PPC_FPU
530 unsigned int fpscr;
531 int fpexc_mode;
532 u64 fpr[32];
533#endif
df6909e5 534
af8f38b3
AG
535 if (!vcpu->arch.sane) {
536 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
537 return -EINVAL;
538 }
539
df6909e5 540 local_irq_disable();
03660ba2 541 if (kvmppc_prepare_to_enter(vcpu)) {
1d1ef222
SW
542 kvm_run->exit_reason = KVM_EXIT_INTR;
543 ret = -EINTR;
544 goto out;
545 }
546
df6909e5 547 kvm_guest_enter();
8fae845f
SW
548
549#ifdef CONFIG_PPC_FPU
550 /* Save userspace FPU state in stack */
551 enable_kernel_fp();
552 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
553 fpscr = current->thread.fpscr.val;
554 fpexc_mode = current->thread.fpexc_mode;
555
556 /* Restore guest FPU state to thread */
557 memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr));
558 current->thread.fpscr.val = vcpu->arch.fpscr;
559
560 /*
561 * Since we can't trap on MSR_FP in GS-mode, we consider the guest
562 * as always using the FPU. Kernel usage of FP (via
563 * enable_kernel_fp()) in this thread must not occur while
564 * vcpu->fpu_active is set.
565 */
566 vcpu->fpu_active = 1;
567
568 kvmppc_load_guest_fp(vcpu);
569#endif
570
df6909e5 571 ret = __kvmppc_vcpu_run(kvm_run, vcpu);
8fae845f
SW
572
573#ifdef CONFIG_PPC_FPU
574 kvmppc_save_guest_fp(vcpu);
575
576 vcpu->fpu_active = 0;
577
578 /* Save guest FPU state from thread */
579 memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr));
580 vcpu->arch.fpscr = current->thread.fpscr.val;
581
582 /* Restore userspace FPU state from stack */
583 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
584 current->thread.fpscr.val = fpscr;
585 current->thread.fpexc_mode = fpexc_mode;
586#endif
587
df6909e5 588 kvm_guest_exit();
862d31f7
AG
589 vcpu->mode = OUTSIDE_GUEST_MODE;
590 smp_wmb();
df6909e5 591
1d1ef222 592out:
d69c6436
AG
593 vcpu->mode = OUTSIDE_GUEST_MODE;
594 smp_wmb();
1d1ef222 595 local_irq_enable();
df6909e5
PM
596 return ret;
597}
598
d30f6e48
SW
599static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
600{
601 enum emulation_result er;
602
603 er = kvmppc_emulate_instruction(run, vcpu);
604 switch (er) {
605 case EMULATE_DONE:
606 /* don't overwrite subtypes, just account kvm_stats */
607 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
608 /* Future optimization: only reload non-volatiles if
609 * they were actually modified by emulation. */
610 return RESUME_GUEST_NV;
611
612 case EMULATE_DO_DCR:
613 run->exit_reason = KVM_EXIT_DCR;
614 return RESUME_HOST;
615
616 case EMULATE_FAIL:
d30f6e48
SW
617 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
618 __func__, vcpu->arch.pc, vcpu->arch.last_inst);
619 /* For debugging, encode the failing instruction and
620 * report it to userspace. */
621 run->hw.hardware_exit_reason = ~0ULL << 32;
622 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
d1ff5499 623 kvmppc_core_queue_program(vcpu, ESR_PIL);
d30f6e48
SW
624 return RESUME_HOST;
625
626 default:
627 BUG();
628 }
629}
630
4e642ccb 631static void kvmppc_fill_pt_regs(struct pt_regs *regs)
bbf45ba5 632{
4e642ccb 633 ulong r1, ip, msr, lr;
bbf45ba5 634
4e642ccb
AG
635 asm("mr %0, 1" : "=r"(r1));
636 asm("mflr %0" : "=r"(lr));
637 asm("mfmsr %0" : "=r"(msr));
638 asm("bl 1f; 1: mflr %0" : "=r"(ip));
639
640 memset(regs, 0, sizeof(*regs));
641 regs->gpr[1] = r1;
642 regs->nip = ip;
643 regs->msr = msr;
644 regs->link = lr;
645}
646
6328e593
BB
647/*
648 * For interrupts needed to be handled by host interrupt handlers,
649 * corresponding host handler are called from here in similar way
650 * (but not exact) as they are called from low level handler
651 * (such as from arch/powerpc/kernel/head_fsl_booke.S).
652 */
4e642ccb
AG
653static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
654 unsigned int exit_nr)
655{
656 struct pt_regs regs;
73e75b41 657
d30f6e48
SW
658 switch (exit_nr) {
659 case BOOKE_INTERRUPT_EXTERNAL:
4e642ccb
AG
660 kvmppc_fill_pt_regs(&regs);
661 do_IRQ(&regs);
d30f6e48 662 break;
d30f6e48 663 case BOOKE_INTERRUPT_DECREMENTER:
4e642ccb
AG
664 kvmppc_fill_pt_regs(&regs);
665 timer_interrupt(&regs);
d30f6e48 666 break;
d30f6e48
SW
667#if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64)
668 case BOOKE_INTERRUPT_DOORBELL:
4e642ccb
AG
669 kvmppc_fill_pt_regs(&regs);
670 doorbell_exception(&regs);
d30f6e48
SW
671 break;
672#endif
673 case BOOKE_INTERRUPT_MACHINE_CHECK:
674 /* FIXME */
675 break;
7cc1e8ee
AG
676 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
677 kvmppc_fill_pt_regs(&regs);
678 performance_monitor_exception(&regs);
679 break;
6328e593
BB
680 case BOOKE_INTERRUPT_WATCHDOG:
681 kvmppc_fill_pt_regs(&regs);
682#ifdef CONFIG_BOOKE_WDT
683 WatchdogException(&regs);
684#else
685 unknown_exception(&regs);
686#endif
687 break;
688 case BOOKE_INTERRUPT_CRITICAL:
689 unknown_exception(&regs);
690 break;
d30f6e48 691 }
4e642ccb
AG
692}
693
694/**
695 * kvmppc_handle_exit
696 *
697 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
698 */
699int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
700 unsigned int exit_nr)
701{
702 int r = RESUME_HOST;
703
704 /* update before a new last_exit_type is rewritten */
705 kvmppc_update_timing_stats(vcpu);
706
707 /* restart interrupts if they were meant for the host */
708 kvmppc_restart_interrupt(vcpu, exit_nr);
d30f6e48 709
bbf45ba5
HB
710 local_irq_enable();
711
97c95059
AG
712 trace_kvm_exit(exit_nr, vcpu);
713
bbf45ba5
HB
714 run->exit_reason = KVM_EXIT_UNKNOWN;
715 run->ready_for_interrupt_injection = 1;
716
717 switch (exit_nr) {
718 case BOOKE_INTERRUPT_MACHINE_CHECK:
c35c9d84
AG
719 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
720 kvmppc_dump_vcpu(vcpu);
721 /* For debugging, send invalid exit reason to user space */
722 run->hw.hardware_exit_reason = ~1ULL << 32;
723 run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
724 r = RESUME_HOST;
bbf45ba5
HB
725 break;
726
727 case BOOKE_INTERRUPT_EXTERNAL:
7b701591 728 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
1b6766c7
HB
729 r = RESUME_GUEST;
730 break;
731
bbf45ba5 732 case BOOKE_INTERRUPT_DECREMENTER:
7b701591 733 kvmppc_account_exit(vcpu, DEC_EXITS);
bbf45ba5
HB
734 r = RESUME_GUEST;
735 break;
736
6328e593
BB
737 case BOOKE_INTERRUPT_WATCHDOG:
738 r = RESUME_GUEST;
739 break;
740
d30f6e48
SW
741 case BOOKE_INTERRUPT_DOORBELL:
742 kvmppc_account_exit(vcpu, DBELL_EXITS);
d30f6e48
SW
743 r = RESUME_GUEST;
744 break;
745
746 case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
747 kvmppc_account_exit(vcpu, GDBELL_EXITS);
748
749 /*
750 * We are here because there is a pending guest interrupt
751 * which could not be delivered as MSR_CE or MSR_ME was not
752 * set. Once we break from here we will retry delivery.
753 */
754 r = RESUME_GUEST;
755 break;
756
757 case BOOKE_INTERRUPT_GUEST_DBELL:
758 kvmppc_account_exit(vcpu, GDBELL_EXITS);
759
760 /*
761 * We are here because there is a pending guest interrupt
762 * which could not be delivered as MSR_EE was not set. Once
763 * we break from here we will retry delivery.
764 */
765 r = RESUME_GUEST;
766 break;
767
95f2e921
AG
768 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
769 r = RESUME_GUEST;
770 break;
771
d30f6e48
SW
772 case BOOKE_INTERRUPT_HV_PRIV:
773 r = emulation_exit(run, vcpu);
774 break;
775
bbf45ba5 776 case BOOKE_INTERRUPT_PROGRAM:
d30f6e48 777 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
0268597c
AG
778 /*
779 * Program traps generated by user-level software must
780 * be handled by the guest kernel.
781 *
782 * In GS mode, hypervisor privileged instructions trap
783 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
784 * actual program interrupts, handled by the guest.
785 */
daf5e271 786 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
bbf45ba5 787 r = RESUME_GUEST;
7b701591 788 kvmppc_account_exit(vcpu, USR_PR_INST);
bbf45ba5
HB
789 break;
790 }
791
d30f6e48 792 r = emulation_exit(run, vcpu);
bbf45ba5
HB
793 break;
794
de368dce 795 case BOOKE_INTERRUPT_FP_UNAVAIL:
d4cf3892 796 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
7b701591 797 kvmppc_account_exit(vcpu, FP_UNAVAIL);
de368dce
CE
798 r = RESUME_GUEST;
799 break;
800
4cd35f67
SW
801#ifdef CONFIG_SPE
802 case BOOKE_INTERRUPT_SPE_UNAVAIL: {
803 if (vcpu->arch.shared->msr & MSR_SPE)
804 kvmppc_vcpu_enable_spe(vcpu);
805 else
806 kvmppc_booke_queue_irqprio(vcpu,
807 BOOKE_IRQPRIO_SPE_UNAVAIL);
bb3a8a17
HB
808 r = RESUME_GUEST;
809 break;
4cd35f67 810 }
bb3a8a17
HB
811
812 case BOOKE_INTERRUPT_SPE_FP_DATA:
813 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
814 r = RESUME_GUEST;
815 break;
816
817 case BOOKE_INTERRUPT_SPE_FP_ROUND:
818 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
819 r = RESUME_GUEST;
820 break;
4cd35f67
SW
821#else
822 case BOOKE_INTERRUPT_SPE_UNAVAIL:
823 /*
824 * Guest wants SPE, but host kernel doesn't support it. Send
825 * an "unimplemented operation" program check to the guest.
826 */
827 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
828 r = RESUME_GUEST;
829 break;
830
831 /*
832 * These really should never happen without CONFIG_SPE,
833 * as we should never enable the real MSR[SPE] in the guest.
834 */
835 case BOOKE_INTERRUPT_SPE_FP_DATA:
836 case BOOKE_INTERRUPT_SPE_FP_ROUND:
837 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
838 __func__, exit_nr, vcpu->arch.pc);
839 run->hw.hardware_exit_reason = exit_nr;
840 r = RESUME_HOST;
841 break;
842#endif
bb3a8a17 843
bbf45ba5 844 case BOOKE_INTERRUPT_DATA_STORAGE:
daf5e271
LY
845 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
846 vcpu->arch.fault_esr);
7b701591 847 kvmppc_account_exit(vcpu, DSI_EXITS);
bbf45ba5
HB
848 r = RESUME_GUEST;
849 break;
850
851 case BOOKE_INTERRUPT_INST_STORAGE:
daf5e271 852 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
7b701591 853 kvmppc_account_exit(vcpu, ISI_EXITS);
bbf45ba5
HB
854 r = RESUME_GUEST;
855 break;
856
d30f6e48
SW
857#ifdef CONFIG_KVM_BOOKE_HV
858 case BOOKE_INTERRUPT_HV_SYSCALL:
859 if (!(vcpu->arch.shared->msr & MSR_PR)) {
860 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
861 } else {
862 /*
863 * hcall from guest userspace -- send privileged
864 * instruction program check.
865 */
866 kvmppc_core_queue_program(vcpu, ESR_PPR);
867 }
868
869 r = RESUME_GUEST;
870 break;
871#else
bbf45ba5 872 case BOOKE_INTERRUPT_SYSCALL:
2a342ed5
AG
873 if (!(vcpu->arch.shared->msr & MSR_PR) &&
874 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
875 /* KVM PV hypercalls */
876 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
877 r = RESUME_GUEST;
878 } else {
879 /* Guest syscalls */
880 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
881 }
7b701591 882 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
bbf45ba5
HB
883 r = RESUME_GUEST;
884 break;
d30f6e48 885#endif
bbf45ba5
HB
886
887 case BOOKE_INTERRUPT_DTLB_MISS: {
bbf45ba5 888 unsigned long eaddr = vcpu->arch.fault_dear;
7924bd41 889 int gtlb_index;
475e7cdd 890 gpa_t gpaddr;
bbf45ba5
HB
891 gfn_t gfn;
892
bf7ca4bd 893#ifdef CONFIG_KVM_E500V2
a4cd8b23
SW
894 if (!(vcpu->arch.shared->msr & MSR_PR) &&
895 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
896 kvmppc_map_magic(vcpu);
897 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
898 r = RESUME_GUEST;
899
900 break;
901 }
902#endif
903
bbf45ba5 904 /* Check the guest TLB. */
fa86b8dd 905 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
7924bd41 906 if (gtlb_index < 0) {
bbf45ba5 907 /* The guest didn't have a mapping for it. */
daf5e271
LY
908 kvmppc_core_queue_dtlb_miss(vcpu,
909 vcpu->arch.fault_dear,
910 vcpu->arch.fault_esr);
b52a638c 911 kvmppc_mmu_dtlb_miss(vcpu);
7b701591 912 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
bbf45ba5
HB
913 r = RESUME_GUEST;
914 break;
915 }
916
be8d1cae 917 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
475e7cdd 918 gfn = gpaddr >> PAGE_SHIFT;
bbf45ba5
HB
919
920 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
921 /* The guest TLB had a mapping, but the shadow TLB
922 * didn't, and it is RAM. This could be because:
923 * a) the entry is mapping the host kernel, or
924 * b) the guest used a large mapping which we're faking
925 * Either way, we need to satisfy the fault without
926 * invoking the guest. */
58a96214 927 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
7b701591 928 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
bbf45ba5
HB
929 r = RESUME_GUEST;
930 } else {
931 /* Guest has mapped and accessed a page which is not
932 * actually RAM. */
475e7cdd 933 vcpu->arch.paddr_accessed = gpaddr;
6020c0f6 934 vcpu->arch.vaddr_accessed = eaddr;
bbf45ba5 935 r = kvmppc_emulate_mmio(run, vcpu);
7b701591 936 kvmppc_account_exit(vcpu, MMIO_EXITS);
bbf45ba5
HB
937 }
938
939 break;
940 }
941
942 case BOOKE_INTERRUPT_ITLB_MISS: {
bbf45ba5 943 unsigned long eaddr = vcpu->arch.pc;
89168618 944 gpa_t gpaddr;
bbf45ba5 945 gfn_t gfn;
7924bd41 946 int gtlb_index;
bbf45ba5
HB
947
948 r = RESUME_GUEST;
949
950 /* Check the guest TLB. */
fa86b8dd 951 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
7924bd41 952 if (gtlb_index < 0) {
bbf45ba5 953 /* The guest didn't have a mapping for it. */
d4cf3892 954 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
b52a638c 955 kvmppc_mmu_itlb_miss(vcpu);
7b701591 956 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
bbf45ba5
HB
957 break;
958 }
959
7b701591 960 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
bbf45ba5 961
be8d1cae 962 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
89168618 963 gfn = gpaddr >> PAGE_SHIFT;
bbf45ba5
HB
964
965 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
966 /* The guest TLB had a mapping, but the shadow TLB
967 * didn't. This could be because:
968 * a) the entry is mapping the host kernel, or
969 * b) the guest used a large mapping which we're faking
970 * Either way, we need to satisfy the fault without
971 * invoking the guest. */
58a96214 972 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
bbf45ba5
HB
973 } else {
974 /* Guest mapped and leaped at non-RAM! */
d4cf3892 975 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
bbf45ba5
HB
976 }
977
978 break;
979 }
980
6a0ab738
HB
981 case BOOKE_INTERRUPT_DEBUG: {
982 u32 dbsr;
983
984 vcpu->arch.pc = mfspr(SPRN_CSRR0);
985
986 /* clear IAC events in DBSR register */
987 dbsr = mfspr(SPRN_DBSR);
988 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
989 mtspr(SPRN_DBSR, dbsr);
990
991 run->exit_reason = KVM_EXIT_DEBUG;
7b701591 992 kvmppc_account_exit(vcpu, DEBUG_EXITS);
6a0ab738
HB
993 r = RESUME_HOST;
994 break;
995 }
996
bbf45ba5
HB
997 default:
998 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
999 BUG();
1000 }
1001
a8e4ef84
AG
1002 /*
1003 * To avoid clobbering exit_reason, only check for signals if we
1004 * aren't already exiting to userspace for some other reason.
1005 */
03660ba2
AG
1006 if (!(r & RESUME_HOST)) {
1007 local_irq_disable();
1008 if (kvmppc_prepare_to_enter(vcpu)) {
1009 run->exit_reason = KVM_EXIT_INTR;
1010 r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
1011 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
1012 }
bbf45ba5
HB
1013 }
1014
1015 return r;
1016}
1017
1018/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
1019int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1020{
082decf2 1021 int i;
af8f38b3 1022 int r;
082decf2 1023
bbf45ba5 1024 vcpu->arch.pc = 0;
b5904972 1025 vcpu->arch.shared->pir = vcpu->vcpu_id;
8e5b26b5 1026 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
d30f6e48 1027 kvmppc_set_msr(vcpu, 0);
bbf45ba5 1028
d30f6e48
SW
1029#ifndef CONFIG_KVM_BOOKE_HV
1030 vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
49dd2c49 1031 vcpu->arch.shadow_pid = 1;
d30f6e48
SW
1032 vcpu->arch.shared->msr = 0;
1033#endif
49dd2c49 1034
082decf2
HB
1035 /* Eye-catching numbers so we know if the guest takes an interrupt
1036 * before it's programmed its own IVPR/IVORs. */
bbf45ba5 1037 vcpu->arch.ivpr = 0x55550000;
082decf2
HB
1038 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
1039 vcpu->arch.ivor[i] = 0x7700 | i * 4;
bbf45ba5 1040
73e75b41
HB
1041 kvmppc_init_timing_stats(vcpu);
1042
af8f38b3
AG
1043 r = kvmppc_core_vcpu_setup(vcpu);
1044 kvmppc_sanity_check(vcpu);
1045 return r;
bbf45ba5
HB
1046}
1047
1048int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1049{
1050 int i;
1051
1052 regs->pc = vcpu->arch.pc;
992b5b29 1053 regs->cr = kvmppc_get_cr(vcpu);
bbf45ba5
HB
1054 regs->ctr = vcpu->arch.ctr;
1055 regs->lr = vcpu->arch.lr;
992b5b29 1056 regs->xer = kvmppc_get_xer(vcpu);
666e7252 1057 regs->msr = vcpu->arch.shared->msr;
de7906c3
AG
1058 regs->srr0 = vcpu->arch.shared->srr0;
1059 regs->srr1 = vcpu->arch.shared->srr1;
bbf45ba5 1060 regs->pid = vcpu->arch.pid;
a73a9599
AG
1061 regs->sprg0 = vcpu->arch.shared->sprg0;
1062 regs->sprg1 = vcpu->arch.shared->sprg1;
1063 regs->sprg2 = vcpu->arch.shared->sprg2;
1064 regs->sprg3 = vcpu->arch.shared->sprg3;
b5904972
SW
1065 regs->sprg4 = vcpu->arch.shared->sprg4;
1066 regs->sprg5 = vcpu->arch.shared->sprg5;
1067 regs->sprg6 = vcpu->arch.shared->sprg6;
1068 regs->sprg7 = vcpu->arch.shared->sprg7;
bbf45ba5
HB
1069
1070 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
8e5b26b5 1071 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
bbf45ba5
HB
1072
1073 return 0;
1074}
1075
1076int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1077{
1078 int i;
1079
1080 vcpu->arch.pc = regs->pc;
992b5b29 1081 kvmppc_set_cr(vcpu, regs->cr);
bbf45ba5
HB
1082 vcpu->arch.ctr = regs->ctr;
1083 vcpu->arch.lr = regs->lr;
992b5b29 1084 kvmppc_set_xer(vcpu, regs->xer);
b8fd68ac 1085 kvmppc_set_msr(vcpu, regs->msr);
de7906c3
AG
1086 vcpu->arch.shared->srr0 = regs->srr0;
1087 vcpu->arch.shared->srr1 = regs->srr1;
5ce941ee 1088 kvmppc_set_pid(vcpu, regs->pid);
a73a9599
AG
1089 vcpu->arch.shared->sprg0 = regs->sprg0;
1090 vcpu->arch.shared->sprg1 = regs->sprg1;
1091 vcpu->arch.shared->sprg2 = regs->sprg2;
1092 vcpu->arch.shared->sprg3 = regs->sprg3;
b5904972
SW
1093 vcpu->arch.shared->sprg4 = regs->sprg4;
1094 vcpu->arch.shared->sprg5 = regs->sprg5;
1095 vcpu->arch.shared->sprg6 = regs->sprg6;
1096 vcpu->arch.shared->sprg7 = regs->sprg7;
bbf45ba5 1097
8e5b26b5
AG
1098 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1099 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
bbf45ba5
HB
1100
1101 return 0;
1102}
1103
5ce941ee
SW
1104static void get_sregs_base(struct kvm_vcpu *vcpu,
1105 struct kvm_sregs *sregs)
1106{
1107 u64 tb = get_tb();
1108
1109 sregs->u.e.features |= KVM_SREGS_E_BASE;
1110
1111 sregs->u.e.csrr0 = vcpu->arch.csrr0;
1112 sregs->u.e.csrr1 = vcpu->arch.csrr1;
1113 sregs->u.e.mcsr = vcpu->arch.mcsr;
d30f6e48
SW
1114 sregs->u.e.esr = get_guest_esr(vcpu);
1115 sregs->u.e.dear = get_guest_dear(vcpu);
5ce941ee
SW
1116 sregs->u.e.tsr = vcpu->arch.tsr;
1117 sregs->u.e.tcr = vcpu->arch.tcr;
1118 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
1119 sregs->u.e.tb = tb;
1120 sregs->u.e.vrsave = vcpu->arch.vrsave;
1121}
1122
1123static int set_sregs_base(struct kvm_vcpu *vcpu,
1124 struct kvm_sregs *sregs)
1125{
1126 if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
1127 return 0;
1128
1129 vcpu->arch.csrr0 = sregs->u.e.csrr0;
1130 vcpu->arch.csrr1 = sregs->u.e.csrr1;
1131 vcpu->arch.mcsr = sregs->u.e.mcsr;
d30f6e48
SW
1132 set_guest_esr(vcpu, sregs->u.e.esr);
1133 set_guest_dear(vcpu, sregs->u.e.dear);
5ce941ee 1134 vcpu->arch.vrsave = sregs->u.e.vrsave;
dfd4d47e 1135 kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
5ce941ee 1136
dfd4d47e 1137 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
5ce941ee 1138 vcpu->arch.dec = sregs->u.e.dec;
dfd4d47e
SW
1139 kvmppc_emulate_dec(vcpu);
1140 }
5ce941ee
SW
1141
1142 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) {
dfd4d47e
SW
1143 vcpu->arch.tsr = sregs->u.e.tsr;
1144 update_timer_ints(vcpu);
5ce941ee
SW
1145 }
1146
1147 return 0;
1148}
1149
1150static void get_sregs_arch206(struct kvm_vcpu *vcpu,
1151 struct kvm_sregs *sregs)
1152{
1153 sregs->u.e.features |= KVM_SREGS_E_ARCH206;
1154
841741f2 1155 sregs->u.e.pir = vcpu->vcpu_id;
5ce941ee
SW
1156 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
1157 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
1158 sregs->u.e.decar = vcpu->arch.decar;
1159 sregs->u.e.ivpr = vcpu->arch.ivpr;
1160}
1161
1162static int set_sregs_arch206(struct kvm_vcpu *vcpu,
1163 struct kvm_sregs *sregs)
1164{
1165 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
1166 return 0;
1167
841741f2 1168 if (sregs->u.e.pir != vcpu->vcpu_id)
5ce941ee
SW
1169 return -EINVAL;
1170
1171 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
1172 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
1173 vcpu->arch.decar = sregs->u.e.decar;
1174 vcpu->arch.ivpr = sregs->u.e.ivpr;
1175
1176 return 0;
1177}
1178
1179void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1180{
1181 sregs->u.e.features |= KVM_SREGS_E_IVOR;
1182
1183 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
1184 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
1185 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
1186 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
1187 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
1188 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
1189 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
1190 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
1191 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
1192 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
1193 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
1194 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
1195 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
1196 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
1197 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
1198 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
1199}
1200
1201int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1202{
1203 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
1204 return 0;
1205
1206 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
1207 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
1208 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
1209 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
1210 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
1211 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
1212 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
1213 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
1214 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
1215 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
1216 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
1217 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
1218 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
1219 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
1220 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
1221 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
1222
1223 return 0;
1224}
1225
bbf45ba5
HB
1226int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1227 struct kvm_sregs *sregs)
1228{
5ce941ee
SW
1229 sregs->pvr = vcpu->arch.pvr;
1230
1231 get_sregs_base(vcpu, sregs);
1232 get_sregs_arch206(vcpu, sregs);
1233 kvmppc_core_get_sregs(vcpu, sregs);
1234 return 0;
bbf45ba5
HB
1235}
1236
1237int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1238 struct kvm_sregs *sregs)
1239{
5ce941ee
SW
1240 int ret;
1241
1242 if (vcpu->arch.pvr != sregs->pvr)
1243 return -EINVAL;
1244
1245 ret = set_sregs_base(vcpu, sregs);
1246 if (ret < 0)
1247 return ret;
1248
1249 ret = set_sregs_arch206(vcpu, sregs);
1250 if (ret < 0)
1251 return ret;
1252
1253 return kvmppc_core_set_sregs(vcpu, sregs);
bbf45ba5
HB
1254}
1255
31f3438e
PM
1256int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1257{
1258 return -EINVAL;
1259}
1260
1261int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1262{
1263 return -EINVAL;
1264}
1265
bbf45ba5
HB
1266int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1267{
1268 return -ENOTSUPP;
1269}
1270
1271int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1272{
1273 return -ENOTSUPP;
1274}
1275
bbf45ba5
HB
1276int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1277 struct kvm_translation *tr)
1278{
98001d8d
AK
1279 int r;
1280
98001d8d 1281 r = kvmppc_core_vcpu_translate(vcpu, tr);
98001d8d 1282 return r;
bbf45ba5 1283}
d9fbd03d 1284
4e755758
AG
1285int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1286{
1287 return -ENOTSUPP;
1288}
1289
f9e0554d
PM
1290int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1291 struct kvm_userspace_memory_region *mem)
1292{
1293 return 0;
1294}
1295
1296void kvmppc_core_commit_memory_region(struct kvm *kvm,
1297 struct kvm_userspace_memory_region *mem)
1298{
1299}
1300
dfd4d47e
SW
1301void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1302{
1303 vcpu->arch.tcr = new_tcr;
1304 update_timer_ints(vcpu);
1305}
1306
1307void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1308{
1309 set_bits(tsr_bits, &vcpu->arch.tsr);
1310 smp_wmb();
1311 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1312 kvm_vcpu_kick(vcpu);
1313}
1314
1315void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1316{
1317 clear_bits(tsr_bits, &vcpu->arch.tsr);
1318 update_timer_ints(vcpu);
1319}
1320
1321void kvmppc_decrementer_func(unsigned long data)
1322{
1323 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1324
21bd000a
BB
1325 if (vcpu->arch.tcr & TCR_ARE) {
1326 vcpu->arch.dec = vcpu->arch.decar;
1327 kvmppc_emulate_dec(vcpu);
1328 }
1329
dfd4d47e
SW
1330 kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1331}
1332
94fa9d99
SW
1333void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1334{
d30f6e48 1335 current->thread.kvm_vcpu = vcpu;
94fa9d99
SW
1336}
1337
1338void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
1339{
d30f6e48 1340 current->thread.kvm_vcpu = NULL;
94fa9d99
SW
1341}
1342
2986b8c7 1343int __init kvmppc_booke_init(void)
d9fbd03d 1344{
d30f6e48 1345#ifndef CONFIG_KVM_BOOKE_HV
d9fbd03d
HB
1346 unsigned long ivor[16];
1347 unsigned long max_ivor = 0;
1348 int i;
1349
1350 /* We install our own exception handlers by hijacking IVPR. IVPR must
1351 * be 16-bit aligned, so we need a 64KB allocation. */
1352 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1353 VCPU_SIZE_ORDER);
1354 if (!kvmppc_booke_handlers)
1355 return -ENOMEM;
1356
1357 /* XXX make sure our handlers are smaller than Linux's */
1358
1359 /* Copy our interrupt handlers to match host IVORs. That way we don't
1360 * have to swap the IVORs on every guest/host transition. */
1361 ivor[0] = mfspr(SPRN_IVOR0);
1362 ivor[1] = mfspr(SPRN_IVOR1);
1363 ivor[2] = mfspr(SPRN_IVOR2);
1364 ivor[3] = mfspr(SPRN_IVOR3);
1365 ivor[4] = mfspr(SPRN_IVOR4);
1366 ivor[5] = mfspr(SPRN_IVOR5);
1367 ivor[6] = mfspr(SPRN_IVOR6);
1368 ivor[7] = mfspr(SPRN_IVOR7);
1369 ivor[8] = mfspr(SPRN_IVOR8);
1370 ivor[9] = mfspr(SPRN_IVOR9);
1371 ivor[10] = mfspr(SPRN_IVOR10);
1372 ivor[11] = mfspr(SPRN_IVOR11);
1373 ivor[12] = mfspr(SPRN_IVOR12);
1374 ivor[13] = mfspr(SPRN_IVOR13);
1375 ivor[14] = mfspr(SPRN_IVOR14);
1376 ivor[15] = mfspr(SPRN_IVOR15);
1377
1378 for (i = 0; i < 16; i++) {
1379 if (ivor[i] > max_ivor)
1380 max_ivor = ivor[i];
1381
1382 memcpy((void *)kvmppc_booke_handlers + ivor[i],
1383 kvmppc_handlers_start + i * kvmppc_handler_len,
1384 kvmppc_handler_len);
1385 }
1386 flush_icache_range(kvmppc_booke_handlers,
1387 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
d30f6e48 1388#endif /* !BOOKE_HV */
db93f574 1389 return 0;
d9fbd03d
HB
1390}
1391
db93f574 1392void __exit kvmppc_booke_exit(void)
d9fbd03d
HB
1393{
1394 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
1395 kvm_exit();
1396}