]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/powerpc/kvm/booke.c
Merge remote-tracking branch 'hid/for-next'
[karo-tx-linux.git] / arch / powerpc / kvm / booke.c
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
16  * Copyright 2010-2011 Freescale Semiconductor, Inc.
17  *
18  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
19  *          Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
20  *          Scott Wood <scottwood@freescale.com>
21  *          Varun Sethi <varun.sethi@freescale.com>
22  */
23
24 #include <linux/errno.h>
25 #include <linux/err.h>
26 #include <linux/kvm_host.h>
27 #include <linux/gfp.h>
28 #include <linux/module.h>
29 #include <linux/vmalloc.h>
30 #include <linux/fs.h>
31
32 #include <asm/cputable.h>
33 #include <asm/uaccess.h>
34 #include <asm/kvm_ppc.h>
35 #include <asm/cacheflush.h>
36 #include <asm/dbell.h>
37 #include <asm/hw_irq.h>
38 #include <asm/irq.h>
39 #include <asm/time.h>
40
41 #include "timing.h"
42 #include "booke.h"
43 #include "trace.h"
44
45 unsigned long kvmppc_booke_handlers;
46
47 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
48 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
49
50 struct kvm_stats_debugfs_item debugfs_entries[] = {
51         { "mmio",       VCPU_STAT(mmio_exits) },
52         { "dcr",        VCPU_STAT(dcr_exits) },
53         { "sig",        VCPU_STAT(signal_exits) },
54         { "itlb_r",     VCPU_STAT(itlb_real_miss_exits) },
55         { "itlb_v",     VCPU_STAT(itlb_virt_miss_exits) },
56         { "dtlb_r",     VCPU_STAT(dtlb_real_miss_exits) },
57         { "dtlb_v",     VCPU_STAT(dtlb_virt_miss_exits) },
58         { "sysc",       VCPU_STAT(syscall_exits) },
59         { "isi",        VCPU_STAT(isi_exits) },
60         { "dsi",        VCPU_STAT(dsi_exits) },
61         { "inst_emu",   VCPU_STAT(emulated_inst_exits) },
62         { "dec",        VCPU_STAT(dec_exits) },
63         { "ext_intr",   VCPU_STAT(ext_intr_exits) },
64         { "halt_wakeup", VCPU_STAT(halt_wakeup) },
65         { "doorbell", VCPU_STAT(dbell_exits) },
66         { "guest doorbell", VCPU_STAT(gdbell_exits) },
67         { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
68         { NULL }
69 };
70
71 /* TODO: use vcpu_printf() */
72 void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
73 {
74         int i;
75
76         printk("pc:   %08lx msr:  %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr);
77         printk("lr:   %08lx ctr:  %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
78         printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0,
79                                             vcpu->arch.shared->srr1);
80
81         printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
82
83         for (i = 0; i < 32; i += 4) {
84                 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
85                        kvmppc_get_gpr(vcpu, i),
86                        kvmppc_get_gpr(vcpu, i+1),
87                        kvmppc_get_gpr(vcpu, i+2),
88                        kvmppc_get_gpr(vcpu, i+3));
89         }
90 }
91
92 #ifdef CONFIG_SPE
93 void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu)
94 {
95         preempt_disable();
96         enable_kernel_spe();
97         kvmppc_save_guest_spe(vcpu);
98         vcpu->arch.shadow_msr &= ~MSR_SPE;
99         preempt_enable();
100 }
101
102 static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu)
103 {
104         preempt_disable();
105         enable_kernel_spe();
106         kvmppc_load_guest_spe(vcpu);
107         vcpu->arch.shadow_msr |= MSR_SPE;
108         preempt_enable();
109 }
110
111 static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
112 {
113         if (vcpu->arch.shared->msr & MSR_SPE) {
114                 if (!(vcpu->arch.shadow_msr & MSR_SPE))
115                         kvmppc_vcpu_enable_spe(vcpu);
116         } else if (vcpu->arch.shadow_msr & MSR_SPE) {
117                 kvmppc_vcpu_disable_spe(vcpu);
118         }
119 }
120 #else
121 static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu)
122 {
123 }
124 #endif
125
126 static void kvmppc_vcpu_sync_fpu(struct kvm_vcpu *vcpu)
127 {
128 #if defined(CONFIG_PPC_FPU) && !defined(CONFIG_KVM_BOOKE_HV)
129         /* We always treat the FP bit as enabled from the host
130            perspective, so only need to adjust the shadow MSR */
131         vcpu->arch.shadow_msr &= ~MSR_FP;
132         vcpu->arch.shadow_msr |= vcpu->arch.shared->msr & MSR_FP;
133 #endif
134 }
135
136 /*
137  * Helper function for "full" MSR writes.  No need to call this if only
138  * EE/CE/ME/DE/RI are changing.
139  */
140 void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr)
141 {
142         u32 old_msr = vcpu->arch.shared->msr;
143
144 #ifdef CONFIG_KVM_BOOKE_HV
145         new_msr |= MSR_GS;
146 #endif
147
148         vcpu->arch.shared->msr = new_msr;
149
150         kvmppc_mmu_msr_notify(vcpu, old_msr);
151         kvmppc_vcpu_sync_spe(vcpu);
152         kvmppc_vcpu_sync_fpu(vcpu);
153 }
154
155 static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
156                                        unsigned int priority)
157 {
158         trace_kvm_booke_queue_irqprio(vcpu, priority);
159         set_bit(priority, &vcpu->arch.pending_exceptions);
160 }
161
162 static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
163                                         ulong dear_flags, ulong esr_flags)
164 {
165         vcpu->arch.queued_dear = dear_flags;
166         vcpu->arch.queued_esr = esr_flags;
167         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
168 }
169
170 static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
171                                            ulong dear_flags, ulong esr_flags)
172 {
173         vcpu->arch.queued_dear = dear_flags;
174         vcpu->arch.queued_esr = esr_flags;
175         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
176 }
177
178 static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
179                                            ulong esr_flags)
180 {
181         vcpu->arch.queued_esr = esr_flags;
182         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
183 }
184
185 static void kvmppc_core_queue_alignment(struct kvm_vcpu *vcpu, ulong dear_flags,
186                                         ulong esr_flags)
187 {
188         vcpu->arch.queued_dear = dear_flags;
189         vcpu->arch.queued_esr = esr_flags;
190         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ALIGNMENT);
191 }
192
193 void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
194 {
195         vcpu->arch.queued_esr = esr_flags;
196         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
197 }
198
199 void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
200 {
201         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
202 }
203
204 int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
205 {
206         return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
207 }
208
209 void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
210 {
211         clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
212 }
213
214 void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
215                                 struct kvm_interrupt *irq)
216 {
217         unsigned int prio = BOOKE_IRQPRIO_EXTERNAL;
218
219         if (irq->irq == KVM_INTERRUPT_SET_LEVEL)
220                 prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL;
221
222         kvmppc_booke_queue_irqprio(vcpu, prio);
223 }
224
225 void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu)
226 {
227         clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions);
228         clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions);
229 }
230
231 static void kvmppc_core_queue_watchdog(struct kvm_vcpu *vcpu)
232 {
233         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_WATCHDOG);
234 }
235
236 static void kvmppc_core_dequeue_watchdog(struct kvm_vcpu *vcpu)
237 {
238         clear_bit(BOOKE_IRQPRIO_WATCHDOG, &vcpu->arch.pending_exceptions);
239 }
240
241 static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
242 {
243 #ifdef CONFIG_KVM_BOOKE_HV
244         mtspr(SPRN_GSRR0, srr0);
245         mtspr(SPRN_GSRR1, srr1);
246 #else
247         vcpu->arch.shared->srr0 = srr0;
248         vcpu->arch.shared->srr1 = srr1;
249 #endif
250 }
251
252 static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
253 {
254         vcpu->arch.csrr0 = srr0;
255         vcpu->arch.csrr1 = srr1;
256 }
257
258 static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
259 {
260         if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) {
261                 vcpu->arch.dsrr0 = srr0;
262                 vcpu->arch.dsrr1 = srr1;
263         } else {
264                 set_guest_csrr(vcpu, srr0, srr1);
265         }
266 }
267
268 static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1)
269 {
270         vcpu->arch.mcsrr0 = srr0;
271         vcpu->arch.mcsrr1 = srr1;
272 }
273
274 static unsigned long get_guest_dear(struct kvm_vcpu *vcpu)
275 {
276 #ifdef CONFIG_KVM_BOOKE_HV
277         return mfspr(SPRN_GDEAR);
278 #else
279         return vcpu->arch.shared->dar;
280 #endif
281 }
282
283 static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear)
284 {
285 #ifdef CONFIG_KVM_BOOKE_HV
286         mtspr(SPRN_GDEAR, dear);
287 #else
288         vcpu->arch.shared->dar = dear;
289 #endif
290 }
291
292 static unsigned long get_guest_esr(struct kvm_vcpu *vcpu)
293 {
294 #ifdef CONFIG_KVM_BOOKE_HV
295         return mfspr(SPRN_GESR);
296 #else
297         return vcpu->arch.shared->esr;
298 #endif
299 }
300
301 static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr)
302 {
303 #ifdef CONFIG_KVM_BOOKE_HV
304         mtspr(SPRN_GESR, esr);
305 #else
306         vcpu->arch.shared->esr = esr;
307 #endif
308 }
309
310 static unsigned long get_guest_epr(struct kvm_vcpu *vcpu)
311 {
312 #ifdef CONFIG_KVM_BOOKE_HV
313         return mfspr(SPRN_GEPR);
314 #else
315         return vcpu->arch.epr;
316 #endif
317 }
318
319 /* Deliver the interrupt of the corresponding priority, if possible. */
320 static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
321                                         unsigned int priority)
322 {
323         int allowed = 0;
324         ulong msr_mask = 0;
325         bool update_esr = false, update_dear = false, update_epr = false;
326         ulong crit_raw = vcpu->arch.shared->critical;
327         ulong crit_r1 = kvmppc_get_gpr(vcpu, 1);
328         bool crit;
329         bool keep_irq = false;
330         enum int_class int_class;
331         ulong new_msr = vcpu->arch.shared->msr;
332
333         /* Truncate crit indicators in 32 bit mode */
334         if (!(vcpu->arch.shared->msr & MSR_SF)) {
335                 crit_raw &= 0xffffffff;
336                 crit_r1 &= 0xffffffff;
337         }
338
339         /* Critical section when crit == r1 */
340         crit = (crit_raw == crit_r1);
341         /* ... and we're in supervisor mode */
342         crit = crit && !(vcpu->arch.shared->msr & MSR_PR);
343
344         if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) {
345                 priority = BOOKE_IRQPRIO_EXTERNAL;
346                 keep_irq = true;
347         }
348
349         if ((priority == BOOKE_IRQPRIO_EXTERNAL) && vcpu->arch.epr_flags)
350                 update_epr = true;
351
352         switch (priority) {
353         case BOOKE_IRQPRIO_DTLB_MISS:
354         case BOOKE_IRQPRIO_DATA_STORAGE:
355         case BOOKE_IRQPRIO_ALIGNMENT:
356                 update_dear = true;
357                 /* fall through */
358         case BOOKE_IRQPRIO_INST_STORAGE:
359         case BOOKE_IRQPRIO_PROGRAM:
360                 update_esr = true;
361                 /* fall through */
362         case BOOKE_IRQPRIO_ITLB_MISS:
363         case BOOKE_IRQPRIO_SYSCALL:
364         case BOOKE_IRQPRIO_FP_UNAVAIL:
365         case BOOKE_IRQPRIO_SPE_UNAVAIL:
366         case BOOKE_IRQPRIO_SPE_FP_DATA:
367         case BOOKE_IRQPRIO_SPE_FP_ROUND:
368         case BOOKE_IRQPRIO_AP_UNAVAIL:
369                 allowed = 1;
370                 msr_mask = MSR_CE | MSR_ME | MSR_DE;
371                 int_class = INT_CLASS_NONCRIT;
372                 break;
373         case BOOKE_IRQPRIO_WATCHDOG:
374         case BOOKE_IRQPRIO_CRITICAL:
375         case BOOKE_IRQPRIO_DBELL_CRIT:
376                 allowed = vcpu->arch.shared->msr & MSR_CE;
377                 allowed = allowed && !crit;
378                 msr_mask = MSR_ME;
379                 int_class = INT_CLASS_CRIT;
380                 break;
381         case BOOKE_IRQPRIO_MACHINE_CHECK:
382                 allowed = vcpu->arch.shared->msr & MSR_ME;
383                 allowed = allowed && !crit;
384                 int_class = INT_CLASS_MC;
385                 break;
386         case BOOKE_IRQPRIO_DECREMENTER:
387         case BOOKE_IRQPRIO_FIT:
388                 keep_irq = true;
389                 /* fall through */
390         case BOOKE_IRQPRIO_EXTERNAL:
391         case BOOKE_IRQPRIO_DBELL:
392                 allowed = vcpu->arch.shared->msr & MSR_EE;
393                 allowed = allowed && !crit;
394                 msr_mask = MSR_CE | MSR_ME | MSR_DE;
395                 int_class = INT_CLASS_NONCRIT;
396                 break;
397         case BOOKE_IRQPRIO_DEBUG:
398                 allowed = vcpu->arch.shared->msr & MSR_DE;
399                 allowed = allowed && !crit;
400                 msr_mask = MSR_ME;
401                 int_class = INT_CLASS_CRIT;
402                 break;
403         }
404
405         if (allowed) {
406                 switch (int_class) {
407                 case INT_CLASS_NONCRIT:
408                         set_guest_srr(vcpu, vcpu->arch.pc,
409                                       vcpu->arch.shared->msr);
410                         break;
411                 case INT_CLASS_CRIT:
412                         set_guest_csrr(vcpu, vcpu->arch.pc,
413                                        vcpu->arch.shared->msr);
414                         break;
415                 case INT_CLASS_DBG:
416                         set_guest_dsrr(vcpu, vcpu->arch.pc,
417                                        vcpu->arch.shared->msr);
418                         break;
419                 case INT_CLASS_MC:
420                         set_guest_mcsrr(vcpu, vcpu->arch.pc,
421                                         vcpu->arch.shared->msr);
422                         break;
423                 }
424
425                 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
426                 if (update_esr == true)
427                         set_guest_esr(vcpu, vcpu->arch.queued_esr);
428                 if (update_dear == true)
429                         set_guest_dear(vcpu, vcpu->arch.queued_dear);
430                 if (update_epr == true) {
431                         if (vcpu->arch.epr_flags & KVMPPC_EPR_USER)
432                                 kvm_make_request(KVM_REQ_EPR_EXIT, vcpu);
433                         else if (vcpu->arch.epr_flags & KVMPPC_EPR_KERNEL) {
434                                 BUG_ON(vcpu->arch.irq_type != KVMPPC_IRQ_MPIC);
435                                 kvmppc_mpic_set_epr(vcpu);
436                         }
437                 }
438
439                 new_msr &= msr_mask;
440 #if defined(CONFIG_64BIT)
441                 if (vcpu->arch.epcr & SPRN_EPCR_ICM)
442                         new_msr |= MSR_CM;
443 #endif
444                 kvmppc_set_msr(vcpu, new_msr);
445
446                 if (!keep_irq)
447                         clear_bit(priority, &vcpu->arch.pending_exceptions);
448         }
449
450 #ifdef CONFIG_KVM_BOOKE_HV
451         /*
452          * If an interrupt is pending but masked, raise a guest doorbell
453          * so that we are notified when the guest enables the relevant
454          * MSR bit.
455          */
456         if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
457                 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
458         if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
459                 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
460         if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
461                 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
462 #endif
463
464         return allowed;
465 }
466
467 /*
468  * Return the number of jiffies until the next timeout.  If the timeout is
469  * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA
470  * because the larger value can break the timer APIs.
471  */
472 static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu)
473 {
474         u64 tb, wdt_tb, wdt_ticks = 0;
475         u64 nr_jiffies = 0;
476         u32 period = TCR_GET_WP(vcpu->arch.tcr);
477
478         wdt_tb = 1ULL << (63 - period);
479         tb = get_tb();
480         /*
481          * The watchdog timeout will hapeen when TB bit corresponding
482          * to watchdog will toggle from 0 to 1.
483          */
484         if (tb & wdt_tb)
485                 wdt_ticks = wdt_tb;
486
487         wdt_ticks += wdt_tb - (tb & (wdt_tb - 1));
488
489         /* Convert timebase ticks to jiffies */
490         nr_jiffies = wdt_ticks;
491
492         if (do_div(nr_jiffies, tb_ticks_per_jiffy))
493                 nr_jiffies++;
494
495         return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA);
496 }
497
498 static void arm_next_watchdog(struct kvm_vcpu *vcpu)
499 {
500         unsigned long nr_jiffies;
501         unsigned long flags;
502
503         /*
504          * If TSR_ENW and TSR_WIS are not set then no need to exit to
505          * userspace, so clear the KVM_REQ_WATCHDOG request.
506          */
507         if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS))
508                 clear_bit(KVM_REQ_WATCHDOG, &vcpu->requests);
509
510         spin_lock_irqsave(&vcpu->arch.wdt_lock, flags);
511         nr_jiffies = watchdog_next_timeout(vcpu);
512         /*
513          * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA
514          * then do not run the watchdog timer as this can break timer APIs.
515          */
516         if (nr_jiffies < NEXT_TIMER_MAX_DELTA)
517                 mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies);
518         else
519                 del_timer(&vcpu->arch.wdt_timer);
520         spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags);
521 }
522
523 void kvmppc_watchdog_func(unsigned long data)
524 {
525         struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
526         u32 tsr, new_tsr;
527         int final;
528
529         do {
530                 new_tsr = tsr = vcpu->arch.tsr;
531                 final = 0;
532
533                 /* Time out event */
534                 if (tsr & TSR_ENW) {
535                         if (tsr & TSR_WIS)
536                                 final = 1;
537                         else
538                                 new_tsr = tsr | TSR_WIS;
539                 } else {
540                         new_tsr = tsr | TSR_ENW;
541                 }
542         } while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr);
543
544         if (new_tsr & TSR_WIS) {
545                 smp_wmb();
546                 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
547                 kvm_vcpu_kick(vcpu);
548         }
549
550         /*
551          * If this is final watchdog expiry and some action is required
552          * then exit to userspace.
553          */
554         if (final && (vcpu->arch.tcr & TCR_WRC_MASK) &&
555             vcpu->arch.watchdog_enabled) {
556                 smp_wmb();
557                 kvm_make_request(KVM_REQ_WATCHDOG, vcpu);
558                 kvm_vcpu_kick(vcpu);
559         }
560
561         /*
562          * Stop running the watchdog timer after final expiration to
563          * prevent the host from being flooded with timers if the
564          * guest sets a short period.
565          * Timers will resume when TSR/TCR is updated next time.
566          */
567         if (!final)
568                 arm_next_watchdog(vcpu);
569 }
570
571 static void update_timer_ints(struct kvm_vcpu *vcpu)
572 {
573         if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
574                 kvmppc_core_queue_dec(vcpu);
575         else
576                 kvmppc_core_dequeue_dec(vcpu);
577
578         if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS))
579                 kvmppc_core_queue_watchdog(vcpu);
580         else
581                 kvmppc_core_dequeue_watchdog(vcpu);
582 }
583
584 static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
585 {
586         unsigned long *pending = &vcpu->arch.pending_exceptions;
587         unsigned int priority;
588
589         priority = __ffs(*pending);
590         while (priority < BOOKE_IRQPRIO_MAX) {
591                 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
592                         break;
593
594                 priority = find_next_bit(pending,
595                                          BITS_PER_BYTE * sizeof(*pending),
596                                          priority + 1);
597         }
598
599         /* Tell the guest about our interrupt status */
600         vcpu->arch.shared->int_pending = !!*pending;
601 }
602
603 /* Check pending exceptions and deliver one, if possible. */
604 int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
605 {
606         int r = 0;
607         WARN_ON_ONCE(!irqs_disabled());
608
609         kvmppc_core_check_exceptions(vcpu);
610
611         if (vcpu->requests) {
612                 /* Exception delivery raised request; start over */
613                 return 1;
614         }
615
616         if (vcpu->arch.shared->msr & MSR_WE) {
617                 local_irq_enable();
618                 kvm_vcpu_block(vcpu);
619                 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
620                 local_irq_disable();
621
622                 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
623                 r = 1;
624         };
625
626         return r;
627 }
628
629 int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
630 {
631         int r = 1; /* Indicate we want to get back into the guest */
632
633         if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
634                 update_timer_ints(vcpu);
635 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
636         if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
637                 kvmppc_core_flush_tlb(vcpu);
638 #endif
639
640         if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) {
641                 vcpu->run->exit_reason = KVM_EXIT_WATCHDOG;
642                 r = 0;
643         }
644
645         if (kvm_check_request(KVM_REQ_EPR_EXIT, vcpu)) {
646                 vcpu->run->epr.epr = 0;
647                 vcpu->arch.epr_needed = true;
648                 vcpu->run->exit_reason = KVM_EXIT_EPR;
649                 r = 0;
650         }
651
652         return r;
653 }
654
655 int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
656 {
657         int ret, s;
658 #ifdef CONFIG_PPC_FPU
659         struct thread_fp_state fp;
660         int fpexc_mode;
661 #endif
662
663         if (!vcpu->arch.sane) {
664                 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
665                 return -EINVAL;
666         }
667
668         local_irq_disable();
669         s = kvmppc_prepare_to_enter(vcpu);
670         if (s <= 0) {
671                 local_irq_enable();
672                 ret = s;
673                 goto out;
674         }
675
676 #ifdef CONFIG_PPC_FPU
677         /* Save userspace FPU state in stack */
678         enable_kernel_fp();
679         fp = current->thread.fp_state;
680         fpexc_mode = current->thread.fpexc_mode;
681
682         /* Restore guest FPU state to thread */
683         memcpy(current->thread.fp_state.fpr, vcpu->arch.fpr,
684                sizeof(vcpu->arch.fpr));
685         current->thread.fp_state.fpscr = vcpu->arch.fpscr;
686
687         /*
688          * Since we can't trap on MSR_FP in GS-mode, we consider the guest
689          * as always using the FPU.  Kernel usage of FP (via
690          * enable_kernel_fp()) in this thread must not occur while
691          * vcpu->fpu_active is set.
692          */
693         vcpu->fpu_active = 1;
694
695         kvmppc_load_guest_fp(vcpu);
696 #endif
697
698         kvmppc_fix_ee_before_entry();
699
700         ret = __kvmppc_vcpu_run(kvm_run, vcpu);
701
702         /* No need for kvm_guest_exit. It's done in handle_exit.
703            We also get here with interrupts enabled. */
704
705 #ifdef CONFIG_PPC_FPU
706         kvmppc_save_guest_fp(vcpu);
707
708         vcpu->fpu_active = 0;
709
710         /* Save guest FPU state from thread */
711         memcpy(vcpu->arch.fpr, current->thread.fp_state.fpr,
712                sizeof(vcpu->arch.fpr));
713         vcpu->arch.fpscr = current->thread.fp_state.fpscr;
714
715         /* Restore userspace FPU state from stack */
716         current->thread.fp_state = fp;
717         current->thread.fpexc_mode = fpexc_mode;
718 #endif
719
720 out:
721         vcpu->mode = OUTSIDE_GUEST_MODE;
722         return ret;
723 }
724
725 static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
726 {
727         enum emulation_result er;
728
729         er = kvmppc_emulate_instruction(run, vcpu);
730         switch (er) {
731         case EMULATE_DONE:
732                 /* don't overwrite subtypes, just account kvm_stats */
733                 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
734                 /* Future optimization: only reload non-volatiles if
735                  * they were actually modified by emulation. */
736                 return RESUME_GUEST_NV;
737
738         case EMULATE_DO_DCR:
739                 run->exit_reason = KVM_EXIT_DCR;
740                 return RESUME_HOST;
741
742         case EMULATE_FAIL:
743                 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
744                        __func__, vcpu->arch.pc, vcpu->arch.last_inst);
745                 /* For debugging, encode the failing instruction and
746                  * report it to userspace. */
747                 run->hw.hardware_exit_reason = ~0ULL << 32;
748                 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
749                 kvmppc_core_queue_program(vcpu, ESR_PIL);
750                 return RESUME_HOST;
751
752         case EMULATE_EXIT_USER:
753                 return RESUME_HOST;
754
755         default:
756                 BUG();
757         }
758 }
759
760 static void kvmppc_fill_pt_regs(struct pt_regs *regs)
761 {
762         ulong r1, ip, msr, lr;
763
764         asm("mr %0, 1" : "=r"(r1));
765         asm("mflr %0" : "=r"(lr));
766         asm("mfmsr %0" : "=r"(msr));
767         asm("bl 1f; 1: mflr %0" : "=r"(ip));
768
769         memset(regs, 0, sizeof(*regs));
770         regs->gpr[1] = r1;
771         regs->nip = ip;
772         regs->msr = msr;
773         regs->link = lr;
774 }
775
776 /*
777  * For interrupts needed to be handled by host interrupt handlers,
778  * corresponding host handler are called from here in similar way
779  * (but not exact) as they are called from low level handler
780  * (such as from arch/powerpc/kernel/head_fsl_booke.S).
781  */
782 static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
783                                      unsigned int exit_nr)
784 {
785         struct pt_regs regs;
786
787         switch (exit_nr) {
788         case BOOKE_INTERRUPT_EXTERNAL:
789                 kvmppc_fill_pt_regs(&regs);
790                 do_IRQ(&regs);
791                 break;
792         case BOOKE_INTERRUPT_DECREMENTER:
793                 kvmppc_fill_pt_regs(&regs);
794                 timer_interrupt(&regs);
795                 break;
796 #if defined(CONFIG_PPC_DOORBELL)
797         case BOOKE_INTERRUPT_DOORBELL:
798                 kvmppc_fill_pt_regs(&regs);
799                 doorbell_exception(&regs);
800                 break;
801 #endif
802         case BOOKE_INTERRUPT_MACHINE_CHECK:
803                 /* FIXME */
804                 break;
805         case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
806                 kvmppc_fill_pt_regs(&regs);
807                 performance_monitor_exception(&regs);
808                 break;
809         case BOOKE_INTERRUPT_WATCHDOG:
810                 kvmppc_fill_pt_regs(&regs);
811 #ifdef CONFIG_BOOKE_WDT
812                 WatchdogException(&regs);
813 #else
814                 unknown_exception(&regs);
815 #endif
816                 break;
817         case BOOKE_INTERRUPT_CRITICAL:
818                 unknown_exception(&regs);
819                 break;
820         }
821 }
822
823 /**
824  * kvmppc_handle_exit
825  *
826  * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
827  */
828 int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
829                        unsigned int exit_nr)
830 {
831         int r = RESUME_HOST;
832         int s;
833         int idx;
834
835 #ifdef CONFIG_PPC64
836         WARN_ON(local_paca->irq_happened != 0);
837 #endif
838
839         /*
840          * We enter with interrupts disabled in hardware, but
841          * we need to call hard_irq_disable anyway to ensure that
842          * the software state is kept in sync.
843          */
844         hard_irq_disable();
845
846         /* update before a new last_exit_type is rewritten */
847         kvmppc_update_timing_stats(vcpu);
848
849         /* restart interrupts if they were meant for the host */
850         kvmppc_restart_interrupt(vcpu, exit_nr);
851
852         local_irq_enable();
853
854         trace_kvm_exit(exit_nr, vcpu);
855         kvm_guest_exit();
856
857         run->exit_reason = KVM_EXIT_UNKNOWN;
858         run->ready_for_interrupt_injection = 1;
859
860         switch (exit_nr) {
861         case BOOKE_INTERRUPT_MACHINE_CHECK:
862                 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
863                 kvmppc_dump_vcpu(vcpu);
864                 /* For debugging, send invalid exit reason to user space */
865                 run->hw.hardware_exit_reason = ~1ULL << 32;
866                 run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
867                 r = RESUME_HOST;
868                 break;
869
870         case BOOKE_INTERRUPT_EXTERNAL:
871                 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
872                 r = RESUME_GUEST;
873                 break;
874
875         case BOOKE_INTERRUPT_DECREMENTER:
876                 kvmppc_account_exit(vcpu, DEC_EXITS);
877                 r = RESUME_GUEST;
878                 break;
879
880         case BOOKE_INTERRUPT_WATCHDOG:
881                 r = RESUME_GUEST;
882                 break;
883
884         case BOOKE_INTERRUPT_DOORBELL:
885                 kvmppc_account_exit(vcpu, DBELL_EXITS);
886                 r = RESUME_GUEST;
887                 break;
888
889         case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
890                 kvmppc_account_exit(vcpu, GDBELL_EXITS);
891
892                 /*
893                  * We are here because there is a pending guest interrupt
894                  * which could not be delivered as MSR_CE or MSR_ME was not
895                  * set.  Once we break from here we will retry delivery.
896                  */
897                 r = RESUME_GUEST;
898                 break;
899
900         case BOOKE_INTERRUPT_GUEST_DBELL:
901                 kvmppc_account_exit(vcpu, GDBELL_EXITS);
902
903                 /*
904                  * We are here because there is a pending guest interrupt
905                  * which could not be delivered as MSR_EE was not set.  Once
906                  * we break from here we will retry delivery.
907                  */
908                 r = RESUME_GUEST;
909                 break;
910
911         case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
912                 r = RESUME_GUEST;
913                 break;
914
915         case BOOKE_INTERRUPT_HV_PRIV:
916                 r = emulation_exit(run, vcpu);
917                 break;
918
919         case BOOKE_INTERRUPT_PROGRAM:
920                 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
921                         /*
922                          * Program traps generated by user-level software must
923                          * be handled by the guest kernel.
924                          *
925                          * In GS mode, hypervisor privileged instructions trap
926                          * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
927                          * actual program interrupts, handled by the guest.
928                          */
929                         kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
930                         r = RESUME_GUEST;
931                         kvmppc_account_exit(vcpu, USR_PR_INST);
932                         break;
933                 }
934
935                 r = emulation_exit(run, vcpu);
936                 break;
937
938         case BOOKE_INTERRUPT_FP_UNAVAIL:
939                 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
940                 kvmppc_account_exit(vcpu, FP_UNAVAIL);
941                 r = RESUME_GUEST;
942                 break;
943
944 #ifdef CONFIG_SPE
945         case BOOKE_INTERRUPT_SPE_UNAVAIL: {
946                 if (vcpu->arch.shared->msr & MSR_SPE)
947                         kvmppc_vcpu_enable_spe(vcpu);
948                 else
949                         kvmppc_booke_queue_irqprio(vcpu,
950                                                    BOOKE_IRQPRIO_SPE_UNAVAIL);
951                 r = RESUME_GUEST;
952                 break;
953         }
954
955         case BOOKE_INTERRUPT_SPE_FP_DATA:
956                 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
957                 r = RESUME_GUEST;
958                 break;
959
960         case BOOKE_INTERRUPT_SPE_FP_ROUND:
961                 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
962                 r = RESUME_GUEST;
963                 break;
964 #else
965         case BOOKE_INTERRUPT_SPE_UNAVAIL:
966                 /*
967                  * Guest wants SPE, but host kernel doesn't support it.  Send
968                  * an "unimplemented operation" program check to the guest.
969                  */
970                 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
971                 r = RESUME_GUEST;
972                 break;
973
974         /*
975          * These really should never happen without CONFIG_SPE,
976          * as we should never enable the real MSR[SPE] in the guest.
977          */
978         case BOOKE_INTERRUPT_SPE_FP_DATA:
979         case BOOKE_INTERRUPT_SPE_FP_ROUND:
980                 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
981                        __func__, exit_nr, vcpu->arch.pc);
982                 run->hw.hardware_exit_reason = exit_nr;
983                 r = RESUME_HOST;
984                 break;
985 #endif
986
987         case BOOKE_INTERRUPT_DATA_STORAGE:
988                 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
989                                                vcpu->arch.fault_esr);
990                 kvmppc_account_exit(vcpu, DSI_EXITS);
991                 r = RESUME_GUEST;
992                 break;
993
994         case BOOKE_INTERRUPT_INST_STORAGE:
995                 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
996                 kvmppc_account_exit(vcpu, ISI_EXITS);
997                 r = RESUME_GUEST;
998                 break;
999
1000         case BOOKE_INTERRUPT_ALIGNMENT:
1001                 kvmppc_core_queue_alignment(vcpu, vcpu->arch.fault_dear,
1002                                             vcpu->arch.fault_esr);
1003                 r = RESUME_GUEST;
1004                 break;
1005
1006 #ifdef CONFIG_KVM_BOOKE_HV
1007         case BOOKE_INTERRUPT_HV_SYSCALL:
1008                 if (!(vcpu->arch.shared->msr & MSR_PR)) {
1009                         kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1010                 } else {
1011                         /*
1012                          * hcall from guest userspace -- send privileged
1013                          * instruction program check.
1014                          */
1015                         kvmppc_core_queue_program(vcpu, ESR_PPR);
1016                 }
1017
1018                 r = RESUME_GUEST;
1019                 break;
1020 #else
1021         case BOOKE_INTERRUPT_SYSCALL:
1022                 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1023                     (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
1024                         /* KVM PV hypercalls */
1025                         kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1026                         r = RESUME_GUEST;
1027                 } else {
1028                         /* Guest syscalls */
1029                         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
1030                 }
1031                 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
1032                 r = RESUME_GUEST;
1033                 break;
1034 #endif
1035
1036         case BOOKE_INTERRUPT_DTLB_MISS: {
1037                 unsigned long eaddr = vcpu->arch.fault_dear;
1038                 int gtlb_index;
1039                 gpa_t gpaddr;
1040                 gfn_t gfn;
1041
1042 #ifdef CONFIG_KVM_E500V2
1043                 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1044                     (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
1045                         kvmppc_map_magic(vcpu);
1046                         kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
1047                         r = RESUME_GUEST;
1048
1049                         break;
1050                 }
1051 #endif
1052
1053                 /* Check the guest TLB. */
1054                 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
1055                 if (gtlb_index < 0) {
1056                         /* The guest didn't have a mapping for it. */
1057                         kvmppc_core_queue_dtlb_miss(vcpu,
1058                                                     vcpu->arch.fault_dear,
1059                                                     vcpu->arch.fault_esr);
1060                         kvmppc_mmu_dtlb_miss(vcpu);
1061                         kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
1062                         r = RESUME_GUEST;
1063                         break;
1064                 }
1065
1066                 idx = srcu_read_lock(&vcpu->kvm->srcu);
1067
1068                 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
1069                 gfn = gpaddr >> PAGE_SHIFT;
1070
1071                 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1072                         /* The guest TLB had a mapping, but the shadow TLB
1073                          * didn't, and it is RAM. This could be because:
1074                          * a) the entry is mapping the host kernel, or
1075                          * b) the guest used a large mapping which we're faking
1076                          * Either way, we need to satisfy the fault without
1077                          * invoking the guest. */
1078                         kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
1079                         kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
1080                         r = RESUME_GUEST;
1081                 } else {
1082                         /* Guest has mapped and accessed a page which is not
1083                          * actually RAM. */
1084                         vcpu->arch.paddr_accessed = gpaddr;
1085                         vcpu->arch.vaddr_accessed = eaddr;
1086                         r = kvmppc_emulate_mmio(run, vcpu);
1087                         kvmppc_account_exit(vcpu, MMIO_EXITS);
1088                 }
1089
1090                 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1091                 break;
1092         }
1093
1094         case BOOKE_INTERRUPT_ITLB_MISS: {
1095                 unsigned long eaddr = vcpu->arch.pc;
1096                 gpa_t gpaddr;
1097                 gfn_t gfn;
1098                 int gtlb_index;
1099
1100                 r = RESUME_GUEST;
1101
1102                 /* Check the guest TLB. */
1103                 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
1104                 if (gtlb_index < 0) {
1105                         /* The guest didn't have a mapping for it. */
1106                         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
1107                         kvmppc_mmu_itlb_miss(vcpu);
1108                         kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
1109                         break;
1110                 }
1111
1112                 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
1113
1114                 idx = srcu_read_lock(&vcpu->kvm->srcu);
1115
1116                 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
1117                 gfn = gpaddr >> PAGE_SHIFT;
1118
1119                 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1120                         /* The guest TLB had a mapping, but the shadow TLB
1121                          * didn't. This could be because:
1122                          * a) the entry is mapping the host kernel, or
1123                          * b) the guest used a large mapping which we're faking
1124                          * Either way, we need to satisfy the fault without
1125                          * invoking the guest. */
1126                         kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
1127                 } else {
1128                         /* Guest mapped and leaped at non-RAM! */
1129                         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
1130                 }
1131
1132                 srcu_read_unlock(&vcpu->kvm->srcu, idx);
1133                 break;
1134         }
1135
1136         case BOOKE_INTERRUPT_DEBUG: {
1137                 u32 dbsr;
1138
1139                 vcpu->arch.pc = mfspr(SPRN_CSRR0);
1140
1141                 /* clear IAC events in DBSR register */
1142                 dbsr = mfspr(SPRN_DBSR);
1143                 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
1144                 mtspr(SPRN_DBSR, dbsr);
1145
1146                 run->exit_reason = KVM_EXIT_DEBUG;
1147                 kvmppc_account_exit(vcpu, DEBUG_EXITS);
1148                 r = RESUME_HOST;
1149                 break;
1150         }
1151
1152         default:
1153                 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
1154                 BUG();
1155         }
1156
1157         /*
1158          * To avoid clobbering exit_reason, only check for signals if we
1159          * aren't already exiting to userspace for some other reason.
1160          */
1161         if (!(r & RESUME_HOST)) {
1162                 local_irq_disable();
1163                 s = kvmppc_prepare_to_enter(vcpu);
1164                 if (s <= 0) {
1165                         local_irq_enable();
1166                         r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
1167                 } else {
1168                         kvmppc_fix_ee_before_entry();
1169                 }
1170         }
1171
1172         return r;
1173 }
1174
1175 static void kvmppc_set_tsr(struct kvm_vcpu *vcpu, u32 new_tsr)
1176 {
1177         u32 old_tsr = vcpu->arch.tsr;
1178
1179         vcpu->arch.tsr = new_tsr;
1180
1181         if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS))
1182                 arm_next_watchdog(vcpu);
1183
1184         update_timer_ints(vcpu);
1185 }
1186
1187 /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
1188 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1189 {
1190         int i;
1191         int r;
1192
1193         vcpu->arch.pc = 0;
1194         vcpu->arch.shared->pir = vcpu->vcpu_id;
1195         kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
1196         kvmppc_set_msr(vcpu, 0);
1197
1198 #ifndef CONFIG_KVM_BOOKE_HV
1199         vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
1200         vcpu->arch.shadow_pid = 1;
1201         vcpu->arch.shared->msr = 0;
1202 #endif
1203
1204         /* Eye-catching numbers so we know if the guest takes an interrupt
1205          * before it's programmed its own IVPR/IVORs. */
1206         vcpu->arch.ivpr = 0x55550000;
1207         for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
1208                 vcpu->arch.ivor[i] = 0x7700 | i * 4;
1209
1210         kvmppc_init_timing_stats(vcpu);
1211
1212         r = kvmppc_core_vcpu_setup(vcpu);
1213         kvmppc_sanity_check(vcpu);
1214         return r;
1215 }
1216
1217 int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
1218 {
1219         /* setup watchdog timer once */
1220         spin_lock_init(&vcpu->arch.wdt_lock);
1221         setup_timer(&vcpu->arch.wdt_timer, kvmppc_watchdog_func,
1222                     (unsigned long)vcpu);
1223
1224         return 0;
1225 }
1226
1227 void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
1228 {
1229         del_timer_sync(&vcpu->arch.wdt_timer);
1230 }
1231
1232 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1233 {
1234         int i;
1235
1236         regs->pc = vcpu->arch.pc;
1237         regs->cr = kvmppc_get_cr(vcpu);
1238         regs->ctr = vcpu->arch.ctr;
1239         regs->lr = vcpu->arch.lr;
1240         regs->xer = kvmppc_get_xer(vcpu);
1241         regs->msr = vcpu->arch.shared->msr;
1242         regs->srr0 = vcpu->arch.shared->srr0;
1243         regs->srr1 = vcpu->arch.shared->srr1;
1244         regs->pid = vcpu->arch.pid;
1245         regs->sprg0 = vcpu->arch.shared->sprg0;
1246         regs->sprg1 = vcpu->arch.shared->sprg1;
1247         regs->sprg2 = vcpu->arch.shared->sprg2;
1248         regs->sprg3 = vcpu->arch.shared->sprg3;
1249         regs->sprg4 = vcpu->arch.shared->sprg4;
1250         regs->sprg5 = vcpu->arch.shared->sprg5;
1251         regs->sprg6 = vcpu->arch.shared->sprg6;
1252         regs->sprg7 = vcpu->arch.shared->sprg7;
1253
1254         for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1255                 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
1256
1257         return 0;
1258 }
1259
1260 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1261 {
1262         int i;
1263
1264         vcpu->arch.pc = regs->pc;
1265         kvmppc_set_cr(vcpu, regs->cr);
1266         vcpu->arch.ctr = regs->ctr;
1267         vcpu->arch.lr = regs->lr;
1268         kvmppc_set_xer(vcpu, regs->xer);
1269         kvmppc_set_msr(vcpu, regs->msr);
1270         vcpu->arch.shared->srr0 = regs->srr0;
1271         vcpu->arch.shared->srr1 = regs->srr1;
1272         kvmppc_set_pid(vcpu, regs->pid);
1273         vcpu->arch.shared->sprg0 = regs->sprg0;
1274         vcpu->arch.shared->sprg1 = regs->sprg1;
1275         vcpu->arch.shared->sprg2 = regs->sprg2;
1276         vcpu->arch.shared->sprg3 = regs->sprg3;
1277         vcpu->arch.shared->sprg4 = regs->sprg4;
1278         vcpu->arch.shared->sprg5 = regs->sprg5;
1279         vcpu->arch.shared->sprg6 = regs->sprg6;
1280         vcpu->arch.shared->sprg7 = regs->sprg7;
1281
1282         for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1283                 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
1284
1285         return 0;
1286 }
1287
1288 static void get_sregs_base(struct kvm_vcpu *vcpu,
1289                            struct kvm_sregs *sregs)
1290 {
1291         u64 tb = get_tb();
1292
1293         sregs->u.e.features |= KVM_SREGS_E_BASE;
1294
1295         sregs->u.e.csrr0 = vcpu->arch.csrr0;
1296         sregs->u.e.csrr1 = vcpu->arch.csrr1;
1297         sregs->u.e.mcsr = vcpu->arch.mcsr;
1298         sregs->u.e.esr = get_guest_esr(vcpu);
1299         sregs->u.e.dear = get_guest_dear(vcpu);
1300         sregs->u.e.tsr = vcpu->arch.tsr;
1301         sregs->u.e.tcr = vcpu->arch.tcr;
1302         sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
1303         sregs->u.e.tb = tb;
1304         sregs->u.e.vrsave = vcpu->arch.vrsave;
1305 }
1306
1307 static int set_sregs_base(struct kvm_vcpu *vcpu,
1308                           struct kvm_sregs *sregs)
1309 {
1310         if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
1311                 return 0;
1312
1313         vcpu->arch.csrr0 = sregs->u.e.csrr0;
1314         vcpu->arch.csrr1 = sregs->u.e.csrr1;
1315         vcpu->arch.mcsr = sregs->u.e.mcsr;
1316         set_guest_esr(vcpu, sregs->u.e.esr);
1317         set_guest_dear(vcpu, sregs->u.e.dear);
1318         vcpu->arch.vrsave = sregs->u.e.vrsave;
1319         kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
1320
1321         if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
1322                 vcpu->arch.dec = sregs->u.e.dec;
1323                 kvmppc_emulate_dec(vcpu);
1324         }
1325
1326         if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR)
1327                 kvmppc_set_tsr(vcpu, sregs->u.e.tsr);
1328
1329         return 0;
1330 }
1331
1332 static void get_sregs_arch206(struct kvm_vcpu *vcpu,
1333                               struct kvm_sregs *sregs)
1334 {
1335         sregs->u.e.features |= KVM_SREGS_E_ARCH206;
1336
1337         sregs->u.e.pir = vcpu->vcpu_id;
1338         sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
1339         sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
1340         sregs->u.e.decar = vcpu->arch.decar;
1341         sregs->u.e.ivpr = vcpu->arch.ivpr;
1342 }
1343
1344 static int set_sregs_arch206(struct kvm_vcpu *vcpu,
1345                              struct kvm_sregs *sregs)
1346 {
1347         if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
1348                 return 0;
1349
1350         if (sregs->u.e.pir != vcpu->vcpu_id)
1351                 return -EINVAL;
1352
1353         vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
1354         vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
1355         vcpu->arch.decar = sregs->u.e.decar;
1356         vcpu->arch.ivpr = sregs->u.e.ivpr;
1357
1358         return 0;
1359 }
1360
1361 void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1362 {
1363         sregs->u.e.features |= KVM_SREGS_E_IVOR;
1364
1365         sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
1366         sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
1367         sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
1368         sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
1369         sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
1370         sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
1371         sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
1372         sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
1373         sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
1374         sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
1375         sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
1376         sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
1377         sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
1378         sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
1379         sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
1380         sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
1381 }
1382
1383 int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1384 {
1385         if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
1386                 return 0;
1387
1388         vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
1389         vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
1390         vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
1391         vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
1392         vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
1393         vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
1394         vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
1395         vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
1396         vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
1397         vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
1398         vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
1399         vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
1400         vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
1401         vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
1402         vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
1403         vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
1404
1405         return 0;
1406 }
1407
1408 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1409                                   struct kvm_sregs *sregs)
1410 {
1411         sregs->pvr = vcpu->arch.pvr;
1412
1413         get_sregs_base(vcpu, sregs);
1414         get_sregs_arch206(vcpu, sregs);
1415         kvmppc_core_get_sregs(vcpu, sregs);
1416         return 0;
1417 }
1418
1419 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1420                                   struct kvm_sregs *sregs)
1421 {
1422         int ret;
1423
1424         if (vcpu->arch.pvr != sregs->pvr)
1425                 return -EINVAL;
1426
1427         ret = set_sregs_base(vcpu, sregs);
1428         if (ret < 0)
1429                 return ret;
1430
1431         ret = set_sregs_arch206(vcpu, sregs);
1432         if (ret < 0)
1433                 return ret;
1434
1435         return kvmppc_core_set_sregs(vcpu, sregs);
1436 }
1437
1438 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1439 {
1440         int r = 0;
1441         union kvmppc_one_reg val;
1442         int size;
1443         long int i;
1444
1445         size = one_reg_size(reg->id);
1446         if (size > sizeof(val))
1447                 return -EINVAL;
1448
1449         switch (reg->id) {
1450         case KVM_REG_PPC_IAC1:
1451         case KVM_REG_PPC_IAC2:
1452         case KVM_REG_PPC_IAC3:
1453         case KVM_REG_PPC_IAC4:
1454                 i = reg->id - KVM_REG_PPC_IAC1;
1455                 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac[i]);
1456                 break;
1457         case KVM_REG_PPC_DAC1:
1458         case KVM_REG_PPC_DAC2:
1459                 i = reg->id - KVM_REG_PPC_DAC1;
1460                 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.dac[i]);
1461                 break;
1462         case KVM_REG_PPC_EPR: {
1463                 u32 epr = get_guest_epr(vcpu);
1464                 val = get_reg_val(reg->id, epr);
1465                 break;
1466         }
1467 #if defined(CONFIG_64BIT)
1468         case KVM_REG_PPC_EPCR:
1469                 val = get_reg_val(reg->id, vcpu->arch.epcr);
1470                 break;
1471 #endif
1472         case KVM_REG_PPC_TCR:
1473                 val = get_reg_val(reg->id, vcpu->arch.tcr);
1474                 break;
1475         case KVM_REG_PPC_TSR:
1476                 val = get_reg_val(reg->id, vcpu->arch.tsr);
1477                 break;
1478         case KVM_REG_PPC_DEBUG_INST:
1479                 val = get_reg_val(reg->id, KVMPPC_INST_EHPRIV);
1480                 break;
1481         default:
1482                 r = kvmppc_get_one_reg(vcpu, reg->id, &val);
1483                 break;
1484         }
1485
1486         if (r)
1487                 return r;
1488
1489         if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
1490                 r = -EFAULT;
1491
1492         return r;
1493 }
1494
1495 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1496 {
1497         int r = 0;
1498         union kvmppc_one_reg val;
1499         int size;
1500         long int i;
1501
1502         size = one_reg_size(reg->id);
1503         if (size > sizeof(val))
1504                 return -EINVAL;
1505
1506         if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
1507                 return -EFAULT;
1508
1509         switch (reg->id) {
1510         case KVM_REG_PPC_IAC1:
1511         case KVM_REG_PPC_IAC2:
1512         case KVM_REG_PPC_IAC3:
1513         case KVM_REG_PPC_IAC4:
1514                 i = reg->id - KVM_REG_PPC_IAC1;
1515                 vcpu->arch.dbg_reg.iac[i] = set_reg_val(reg->id, val);
1516                 break;
1517         case KVM_REG_PPC_DAC1:
1518         case KVM_REG_PPC_DAC2:
1519                 i = reg->id - KVM_REG_PPC_DAC1;
1520                 vcpu->arch.dbg_reg.dac[i] = set_reg_val(reg->id, val);
1521                 break;
1522         case KVM_REG_PPC_EPR: {
1523                 u32 new_epr = set_reg_val(reg->id, val);
1524                 kvmppc_set_epr(vcpu, new_epr);
1525                 break;
1526         }
1527 #if defined(CONFIG_64BIT)
1528         case KVM_REG_PPC_EPCR: {
1529                 u32 new_epcr = set_reg_val(reg->id, val);
1530                 kvmppc_set_epcr(vcpu, new_epcr);
1531                 break;
1532         }
1533 #endif
1534         case KVM_REG_PPC_OR_TSR: {
1535                 u32 tsr_bits = set_reg_val(reg->id, val);
1536                 kvmppc_set_tsr_bits(vcpu, tsr_bits);
1537                 break;
1538         }
1539         case KVM_REG_PPC_CLEAR_TSR: {
1540                 u32 tsr_bits = set_reg_val(reg->id, val);
1541                 kvmppc_clr_tsr_bits(vcpu, tsr_bits);
1542                 break;
1543         }
1544         case KVM_REG_PPC_TSR: {
1545                 u32 tsr = set_reg_val(reg->id, val);
1546                 kvmppc_set_tsr(vcpu, tsr);
1547                 break;
1548         }
1549         case KVM_REG_PPC_TCR: {
1550                 u32 tcr = set_reg_val(reg->id, val);
1551                 kvmppc_set_tcr(vcpu, tcr);
1552                 break;
1553         }
1554         default:
1555                 r = kvmppc_set_one_reg(vcpu, reg->id, &val);
1556                 break;
1557         }
1558
1559         return r;
1560 }
1561
1562 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1563                                          struct kvm_guest_debug *dbg)
1564 {
1565         return -EINVAL;
1566 }
1567
1568 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1569 {
1570         return -ENOTSUPP;
1571 }
1572
1573 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1574 {
1575         return -ENOTSUPP;
1576 }
1577
1578 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1579                                   struct kvm_translation *tr)
1580 {
1581         int r;
1582
1583         r = kvmppc_core_vcpu_translate(vcpu, tr);
1584         return r;
1585 }
1586
1587 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1588 {
1589         return -ENOTSUPP;
1590 }
1591
1592 void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1593                               struct kvm_memory_slot *dont)
1594 {
1595 }
1596
1597 int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1598                                unsigned long npages)
1599 {
1600         return 0;
1601 }
1602
1603 int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1604                                       struct kvm_memory_slot *memslot,
1605                                       struct kvm_userspace_memory_region *mem)
1606 {
1607         return 0;
1608 }
1609
1610 void kvmppc_core_commit_memory_region(struct kvm *kvm,
1611                                 struct kvm_userspace_memory_region *mem,
1612                                 const struct kvm_memory_slot *old)
1613 {
1614 }
1615
1616 void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
1617 {
1618 }
1619
1620 void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr)
1621 {
1622 #if defined(CONFIG_64BIT)
1623         vcpu->arch.epcr = new_epcr;
1624 #ifdef CONFIG_KVM_BOOKE_HV
1625         vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM;
1626         if (vcpu->arch.epcr  & SPRN_EPCR_ICM)
1627                 vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM;
1628 #endif
1629 #endif
1630 }
1631
1632 void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1633 {
1634         vcpu->arch.tcr = new_tcr;
1635         arm_next_watchdog(vcpu);
1636         update_timer_ints(vcpu);
1637 }
1638
1639 void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1640 {
1641         set_bits(tsr_bits, &vcpu->arch.tsr);
1642         smp_wmb();
1643         kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1644         kvm_vcpu_kick(vcpu);
1645 }
1646
1647 void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1648 {
1649         clear_bits(tsr_bits, &vcpu->arch.tsr);
1650
1651         /*
1652          * We may have stopped the watchdog due to
1653          * being stuck on final expiration.
1654          */
1655         if (tsr_bits & (TSR_ENW | TSR_WIS))
1656                 arm_next_watchdog(vcpu);
1657
1658         update_timer_ints(vcpu);
1659 }
1660
1661 void kvmppc_decrementer_func(unsigned long data)
1662 {
1663         struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1664
1665         if (vcpu->arch.tcr & TCR_ARE) {
1666                 vcpu->arch.dec = vcpu->arch.decar;
1667                 kvmppc_emulate_dec(vcpu);
1668         }
1669
1670         kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1671 }
1672
1673 void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1674 {
1675         vcpu->cpu = smp_processor_id();
1676         current->thread.kvm_vcpu = vcpu;
1677 }
1678
1679 void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
1680 {
1681         current->thread.kvm_vcpu = NULL;
1682         vcpu->cpu = -1;
1683 }
1684
1685 int __init kvmppc_booke_init(void)
1686 {
1687 #ifndef CONFIG_KVM_BOOKE_HV
1688         unsigned long ivor[16];
1689         unsigned long *handler = kvmppc_booke_handler_addr;
1690         unsigned long max_ivor = 0;
1691         unsigned long handler_len;
1692         int i;
1693
1694         /* We install our own exception handlers by hijacking IVPR. IVPR must
1695          * be 16-bit aligned, so we need a 64KB allocation. */
1696         kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1697                                                  VCPU_SIZE_ORDER);
1698         if (!kvmppc_booke_handlers)
1699                 return -ENOMEM;
1700
1701         /* XXX make sure our handlers are smaller than Linux's */
1702
1703         /* Copy our interrupt handlers to match host IVORs. That way we don't
1704          * have to swap the IVORs on every guest/host transition. */
1705         ivor[0] = mfspr(SPRN_IVOR0);
1706         ivor[1] = mfspr(SPRN_IVOR1);
1707         ivor[2] = mfspr(SPRN_IVOR2);
1708         ivor[3] = mfspr(SPRN_IVOR3);
1709         ivor[4] = mfspr(SPRN_IVOR4);
1710         ivor[5] = mfspr(SPRN_IVOR5);
1711         ivor[6] = mfspr(SPRN_IVOR6);
1712         ivor[7] = mfspr(SPRN_IVOR7);
1713         ivor[8] = mfspr(SPRN_IVOR8);
1714         ivor[9] = mfspr(SPRN_IVOR9);
1715         ivor[10] = mfspr(SPRN_IVOR10);
1716         ivor[11] = mfspr(SPRN_IVOR11);
1717         ivor[12] = mfspr(SPRN_IVOR12);
1718         ivor[13] = mfspr(SPRN_IVOR13);
1719         ivor[14] = mfspr(SPRN_IVOR14);
1720         ivor[15] = mfspr(SPRN_IVOR15);
1721
1722         for (i = 0; i < 16; i++) {
1723                 if (ivor[i] > max_ivor)
1724                         max_ivor = i;
1725
1726                 handler_len = handler[i + 1] - handler[i];
1727                 memcpy((void *)kvmppc_booke_handlers + ivor[i],
1728                        (void *)handler[i], handler_len);
1729         }
1730
1731         handler_len = handler[max_ivor + 1] - handler[max_ivor];
1732         flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers +
1733                            ivor[max_ivor] + handler_len);
1734 #endif /* !BOOKE_HV */
1735         return 0;
1736 }
1737
1738 void __exit kvmppc_booke_exit(void)
1739 {
1740         free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
1741         kvm_exit();
1742 }