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