]> Pileus Git - ~andy/linux/blob - arch/powerpc/kvm/booke.c
kvm/ppc/mpic: in-kernel MPIC emulation
[~andy/linux] / 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                 }
434
435                 new_msr &= msr_mask;
436 #if defined(CONFIG_64BIT)
437                 if (vcpu->arch.epcr & SPRN_EPCR_ICM)
438                         new_msr |= MSR_CM;
439 #endif
440                 kvmppc_set_msr(vcpu, new_msr);
441
442                 if (!keep_irq)
443                         clear_bit(priority, &vcpu->arch.pending_exceptions);
444         }
445
446 #ifdef CONFIG_KVM_BOOKE_HV
447         /*
448          * If an interrupt is pending but masked, raise a guest doorbell
449          * so that we are notified when the guest enables the relevant
450          * MSR bit.
451          */
452         if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE)
453                 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT);
454         if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE)
455                 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT);
456         if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK)
457                 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC);
458 #endif
459
460         return allowed;
461 }
462
463 /*
464  * Return the number of jiffies until the next timeout.  If the timeout is
465  * longer than the NEXT_TIMER_MAX_DELTA, then return NEXT_TIMER_MAX_DELTA
466  * because the larger value can break the timer APIs.
467  */
468 static unsigned long watchdog_next_timeout(struct kvm_vcpu *vcpu)
469 {
470         u64 tb, wdt_tb, wdt_ticks = 0;
471         u64 nr_jiffies = 0;
472         u32 period = TCR_GET_WP(vcpu->arch.tcr);
473
474         wdt_tb = 1ULL << (63 - period);
475         tb = get_tb();
476         /*
477          * The watchdog timeout will hapeen when TB bit corresponding
478          * to watchdog will toggle from 0 to 1.
479          */
480         if (tb & wdt_tb)
481                 wdt_ticks = wdt_tb;
482
483         wdt_ticks += wdt_tb - (tb & (wdt_tb - 1));
484
485         /* Convert timebase ticks to jiffies */
486         nr_jiffies = wdt_ticks;
487
488         if (do_div(nr_jiffies, tb_ticks_per_jiffy))
489                 nr_jiffies++;
490
491         return min_t(unsigned long long, nr_jiffies, NEXT_TIMER_MAX_DELTA);
492 }
493
494 static void arm_next_watchdog(struct kvm_vcpu *vcpu)
495 {
496         unsigned long nr_jiffies;
497         unsigned long flags;
498
499         /*
500          * If TSR_ENW and TSR_WIS are not set then no need to exit to
501          * userspace, so clear the KVM_REQ_WATCHDOG request.
502          */
503         if ((vcpu->arch.tsr & (TSR_ENW | TSR_WIS)) != (TSR_ENW | TSR_WIS))
504                 clear_bit(KVM_REQ_WATCHDOG, &vcpu->requests);
505
506         spin_lock_irqsave(&vcpu->arch.wdt_lock, flags);
507         nr_jiffies = watchdog_next_timeout(vcpu);
508         /*
509          * If the number of jiffies of watchdog timer >= NEXT_TIMER_MAX_DELTA
510          * then do not run the watchdog timer as this can break timer APIs.
511          */
512         if (nr_jiffies < NEXT_TIMER_MAX_DELTA)
513                 mod_timer(&vcpu->arch.wdt_timer, jiffies + nr_jiffies);
514         else
515                 del_timer(&vcpu->arch.wdt_timer);
516         spin_unlock_irqrestore(&vcpu->arch.wdt_lock, flags);
517 }
518
519 void kvmppc_watchdog_func(unsigned long data)
520 {
521         struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
522         u32 tsr, new_tsr;
523         int final;
524
525         do {
526                 new_tsr = tsr = vcpu->arch.tsr;
527                 final = 0;
528
529                 /* Time out event */
530                 if (tsr & TSR_ENW) {
531                         if (tsr & TSR_WIS)
532                                 final = 1;
533                         else
534                                 new_tsr = tsr | TSR_WIS;
535                 } else {
536                         new_tsr = tsr | TSR_ENW;
537                 }
538         } while (cmpxchg(&vcpu->arch.tsr, tsr, new_tsr) != tsr);
539
540         if (new_tsr & TSR_WIS) {
541                 smp_wmb();
542                 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
543                 kvm_vcpu_kick(vcpu);
544         }
545
546         /*
547          * If this is final watchdog expiry and some action is required
548          * then exit to userspace.
549          */
550         if (final && (vcpu->arch.tcr & TCR_WRC_MASK) &&
551             vcpu->arch.watchdog_enabled) {
552                 smp_wmb();
553                 kvm_make_request(KVM_REQ_WATCHDOG, vcpu);
554                 kvm_vcpu_kick(vcpu);
555         }
556
557         /*
558          * Stop running the watchdog timer after final expiration to
559          * prevent the host from being flooded with timers if the
560          * guest sets a short period.
561          * Timers will resume when TSR/TCR is updated next time.
562          */
563         if (!final)
564                 arm_next_watchdog(vcpu);
565 }
566
567 static void update_timer_ints(struct kvm_vcpu *vcpu)
568 {
569         if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS))
570                 kvmppc_core_queue_dec(vcpu);
571         else
572                 kvmppc_core_dequeue_dec(vcpu);
573
574         if ((vcpu->arch.tcr & TCR_WIE) && (vcpu->arch.tsr & TSR_WIS))
575                 kvmppc_core_queue_watchdog(vcpu);
576         else
577                 kvmppc_core_dequeue_watchdog(vcpu);
578 }
579
580 static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu)
581 {
582         unsigned long *pending = &vcpu->arch.pending_exceptions;
583         unsigned int priority;
584
585         priority = __ffs(*pending);
586         while (priority < BOOKE_IRQPRIO_MAX) {
587                 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
588                         break;
589
590                 priority = find_next_bit(pending,
591                                          BITS_PER_BYTE * sizeof(*pending),
592                                          priority + 1);
593         }
594
595         /* Tell the guest about our interrupt status */
596         vcpu->arch.shared->int_pending = !!*pending;
597 }
598
599 /* Check pending exceptions and deliver one, if possible. */
600 int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu)
601 {
602         int r = 0;
603         WARN_ON_ONCE(!irqs_disabled());
604
605         kvmppc_core_check_exceptions(vcpu);
606
607         if (vcpu->requests) {
608                 /* Exception delivery raised request; start over */
609                 return 1;
610         }
611
612         if (vcpu->arch.shared->msr & MSR_WE) {
613                 local_irq_enable();
614                 kvm_vcpu_block(vcpu);
615                 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
616                 local_irq_disable();
617
618                 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS);
619                 r = 1;
620         };
621
622         return r;
623 }
624
625 int kvmppc_core_check_requests(struct kvm_vcpu *vcpu)
626 {
627         int r = 1; /* Indicate we want to get back into the guest */
628
629         if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu))
630                 update_timer_ints(vcpu);
631 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
632         if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
633                 kvmppc_core_flush_tlb(vcpu);
634 #endif
635
636         if (kvm_check_request(KVM_REQ_WATCHDOG, vcpu)) {
637                 vcpu->run->exit_reason = KVM_EXIT_WATCHDOG;
638                 r = 0;
639         }
640
641         if (kvm_check_request(KVM_REQ_EPR_EXIT, vcpu)) {
642                 vcpu->run->epr.epr = 0;
643                 vcpu->arch.epr_needed = true;
644                 vcpu->run->exit_reason = KVM_EXIT_EPR;
645                 r = 0;
646         }
647
648         return r;
649 }
650
651 int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
652 {
653         int ret, s;
654 #ifdef CONFIG_PPC_FPU
655         unsigned int fpscr;
656         int fpexc_mode;
657         u64 fpr[32];
658 #endif
659
660         if (!vcpu->arch.sane) {
661                 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
662                 return -EINVAL;
663         }
664
665         local_irq_disable();
666         s = kvmppc_prepare_to_enter(vcpu);
667         if (s <= 0) {
668                 local_irq_enable();
669                 ret = s;
670                 goto out;
671         }
672         kvmppc_lazy_ee_enable();
673
674         kvm_guest_enter();
675
676 #ifdef CONFIG_PPC_FPU
677         /* Save userspace FPU state in stack */
678         enable_kernel_fp();
679         memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr));
680         fpscr = current->thread.fpscr.val;
681         fpexc_mode = current->thread.fpexc_mode;
682
683         /* Restore guest FPU state to thread */
684         memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr));
685         current->thread.fpscr.val = 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         ret = __kvmppc_vcpu_run(kvm_run, vcpu);
699
700         /* No need for kvm_guest_exit. It's done in handle_exit.
701            We also get here with interrupts enabled. */
702
703 #ifdef CONFIG_PPC_FPU
704         kvmppc_save_guest_fp(vcpu);
705
706         vcpu->fpu_active = 0;
707
708         /* Save guest FPU state from thread */
709         memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr));
710         vcpu->arch.fpscr = current->thread.fpscr.val;
711
712         /* Restore userspace FPU state from stack */
713         memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr));
714         current->thread.fpscr.val = fpscr;
715         current->thread.fpexc_mode = fpexc_mode;
716 #endif
717
718 out:
719         vcpu->mode = OUTSIDE_GUEST_MODE;
720         return ret;
721 }
722
723 static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
724 {
725         enum emulation_result er;
726
727         er = kvmppc_emulate_instruction(run, vcpu);
728         switch (er) {
729         case EMULATE_DONE:
730                 /* don't overwrite subtypes, just account kvm_stats */
731                 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
732                 /* Future optimization: only reload non-volatiles if
733                  * they were actually modified by emulation. */
734                 return RESUME_GUEST_NV;
735
736         case EMULATE_DO_DCR:
737                 run->exit_reason = KVM_EXIT_DCR;
738                 return RESUME_HOST;
739
740         case EMULATE_FAIL:
741                 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
742                        __func__, vcpu->arch.pc, vcpu->arch.last_inst);
743                 /* For debugging, encode the failing instruction and
744                  * report it to userspace. */
745                 run->hw.hardware_exit_reason = ~0ULL << 32;
746                 run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
747                 kvmppc_core_queue_program(vcpu, ESR_PIL);
748                 return RESUME_HOST;
749
750         case EMULATE_EXIT_USER:
751                 return RESUME_HOST;
752
753         default:
754                 BUG();
755         }
756 }
757
758 static void kvmppc_fill_pt_regs(struct pt_regs *regs)
759 {
760         ulong r1, ip, msr, lr;
761
762         asm("mr %0, 1" : "=r"(r1));
763         asm("mflr %0" : "=r"(lr));
764         asm("mfmsr %0" : "=r"(msr));
765         asm("bl 1f; 1: mflr %0" : "=r"(ip));
766
767         memset(regs, 0, sizeof(*regs));
768         regs->gpr[1] = r1;
769         regs->nip = ip;
770         regs->msr = msr;
771         regs->link = lr;
772 }
773
774 /*
775  * For interrupts needed to be handled by host interrupt handlers,
776  * corresponding host handler are called from here in similar way
777  * (but not exact) as they are called from low level handler
778  * (such as from arch/powerpc/kernel/head_fsl_booke.S).
779  */
780 static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu,
781                                      unsigned int exit_nr)
782 {
783         struct pt_regs regs;
784
785         switch (exit_nr) {
786         case BOOKE_INTERRUPT_EXTERNAL:
787                 kvmppc_fill_pt_regs(&regs);
788                 do_IRQ(&regs);
789                 break;
790         case BOOKE_INTERRUPT_DECREMENTER:
791                 kvmppc_fill_pt_regs(&regs);
792                 timer_interrupt(&regs);
793                 break;
794 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64)
795         case BOOKE_INTERRUPT_DOORBELL:
796                 kvmppc_fill_pt_regs(&regs);
797                 doorbell_exception(&regs);
798                 break;
799 #endif
800         case BOOKE_INTERRUPT_MACHINE_CHECK:
801                 /* FIXME */
802                 break;
803         case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
804                 kvmppc_fill_pt_regs(&regs);
805                 performance_monitor_exception(&regs);
806                 break;
807         case BOOKE_INTERRUPT_WATCHDOG:
808                 kvmppc_fill_pt_regs(&regs);
809 #ifdef CONFIG_BOOKE_WDT
810                 WatchdogException(&regs);
811 #else
812                 unknown_exception(&regs);
813 #endif
814                 break;
815         case BOOKE_INTERRUPT_CRITICAL:
816                 unknown_exception(&regs);
817                 break;
818         }
819 }
820
821 /**
822  * kvmppc_handle_exit
823  *
824  * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
825  */
826 int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
827                        unsigned int exit_nr)
828 {
829         int r = RESUME_HOST;
830         int s;
831
832         /* update before a new last_exit_type is rewritten */
833         kvmppc_update_timing_stats(vcpu);
834
835         /* restart interrupts if they were meant for the host */
836         kvmppc_restart_interrupt(vcpu, exit_nr);
837
838         local_irq_enable();
839
840         trace_kvm_exit(exit_nr, vcpu);
841         kvm_guest_exit();
842
843         run->exit_reason = KVM_EXIT_UNKNOWN;
844         run->ready_for_interrupt_injection = 1;
845
846         switch (exit_nr) {
847         case BOOKE_INTERRUPT_MACHINE_CHECK:
848                 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
849                 kvmppc_dump_vcpu(vcpu);
850                 /* For debugging, send invalid exit reason to user space */
851                 run->hw.hardware_exit_reason = ~1ULL << 32;
852                 run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR);
853                 r = RESUME_HOST;
854                 break;
855
856         case BOOKE_INTERRUPT_EXTERNAL:
857                 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
858                 r = RESUME_GUEST;
859                 break;
860
861         case BOOKE_INTERRUPT_DECREMENTER:
862                 kvmppc_account_exit(vcpu, DEC_EXITS);
863                 r = RESUME_GUEST;
864                 break;
865
866         case BOOKE_INTERRUPT_WATCHDOG:
867                 r = RESUME_GUEST;
868                 break;
869
870         case BOOKE_INTERRUPT_DOORBELL:
871                 kvmppc_account_exit(vcpu, DBELL_EXITS);
872                 r = RESUME_GUEST;
873                 break;
874
875         case BOOKE_INTERRUPT_GUEST_DBELL_CRIT:
876                 kvmppc_account_exit(vcpu, GDBELL_EXITS);
877
878                 /*
879                  * We are here because there is a pending guest interrupt
880                  * which could not be delivered as MSR_CE or MSR_ME was not
881                  * set.  Once we break from here we will retry delivery.
882                  */
883                 r = RESUME_GUEST;
884                 break;
885
886         case BOOKE_INTERRUPT_GUEST_DBELL:
887                 kvmppc_account_exit(vcpu, GDBELL_EXITS);
888
889                 /*
890                  * We are here because there is a pending guest interrupt
891                  * which could not be delivered as MSR_EE was not set.  Once
892                  * we break from here we will retry delivery.
893                  */
894                 r = RESUME_GUEST;
895                 break;
896
897         case BOOKE_INTERRUPT_PERFORMANCE_MONITOR:
898                 r = RESUME_GUEST;
899                 break;
900
901         case BOOKE_INTERRUPT_HV_PRIV:
902                 r = emulation_exit(run, vcpu);
903                 break;
904
905         case BOOKE_INTERRUPT_PROGRAM:
906                 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) {
907                         /*
908                          * Program traps generated by user-level software must
909                          * be handled by the guest kernel.
910                          *
911                          * In GS mode, hypervisor privileged instructions trap
912                          * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are
913                          * actual program interrupts, handled by the guest.
914                          */
915                         kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
916                         r = RESUME_GUEST;
917                         kvmppc_account_exit(vcpu, USR_PR_INST);
918                         break;
919                 }
920
921                 r = emulation_exit(run, vcpu);
922                 break;
923
924         case BOOKE_INTERRUPT_FP_UNAVAIL:
925                 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
926                 kvmppc_account_exit(vcpu, FP_UNAVAIL);
927                 r = RESUME_GUEST;
928                 break;
929
930 #ifdef CONFIG_SPE
931         case BOOKE_INTERRUPT_SPE_UNAVAIL: {
932                 if (vcpu->arch.shared->msr & MSR_SPE)
933                         kvmppc_vcpu_enable_spe(vcpu);
934                 else
935                         kvmppc_booke_queue_irqprio(vcpu,
936                                                    BOOKE_IRQPRIO_SPE_UNAVAIL);
937                 r = RESUME_GUEST;
938                 break;
939         }
940
941         case BOOKE_INTERRUPT_SPE_FP_DATA:
942                 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
943                 r = RESUME_GUEST;
944                 break;
945
946         case BOOKE_INTERRUPT_SPE_FP_ROUND:
947                 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
948                 r = RESUME_GUEST;
949                 break;
950 #else
951         case BOOKE_INTERRUPT_SPE_UNAVAIL:
952                 /*
953                  * Guest wants SPE, but host kernel doesn't support it.  Send
954                  * an "unimplemented operation" program check to the guest.
955                  */
956                 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV);
957                 r = RESUME_GUEST;
958                 break;
959
960         /*
961          * These really should never happen without CONFIG_SPE,
962          * as we should never enable the real MSR[SPE] in the guest.
963          */
964         case BOOKE_INTERRUPT_SPE_FP_DATA:
965         case BOOKE_INTERRUPT_SPE_FP_ROUND:
966                 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n",
967                        __func__, exit_nr, vcpu->arch.pc);
968                 run->hw.hardware_exit_reason = exit_nr;
969                 r = RESUME_HOST;
970                 break;
971 #endif
972
973         case BOOKE_INTERRUPT_DATA_STORAGE:
974                 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
975                                                vcpu->arch.fault_esr);
976                 kvmppc_account_exit(vcpu, DSI_EXITS);
977                 r = RESUME_GUEST;
978                 break;
979
980         case BOOKE_INTERRUPT_INST_STORAGE:
981                 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
982                 kvmppc_account_exit(vcpu, ISI_EXITS);
983                 r = RESUME_GUEST;
984                 break;
985
986         case BOOKE_INTERRUPT_ALIGNMENT:
987                 kvmppc_core_queue_alignment(vcpu, vcpu->arch.fault_dear,
988                                             vcpu->arch.fault_esr);
989                 r = RESUME_GUEST;
990                 break;
991
992 #ifdef CONFIG_KVM_BOOKE_HV
993         case BOOKE_INTERRUPT_HV_SYSCALL:
994                 if (!(vcpu->arch.shared->msr & MSR_PR)) {
995                         kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
996                 } else {
997                         /*
998                          * hcall from guest userspace -- send privileged
999                          * instruction program check.
1000                          */
1001                         kvmppc_core_queue_program(vcpu, ESR_PPR);
1002                 }
1003
1004                 r = RESUME_GUEST;
1005                 break;
1006 #else
1007         case BOOKE_INTERRUPT_SYSCALL:
1008                 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1009                     (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) {
1010                         /* KVM PV hypercalls */
1011                         kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu));
1012                         r = RESUME_GUEST;
1013                 } else {
1014                         /* Guest syscalls */
1015                         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
1016                 }
1017                 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
1018                 r = RESUME_GUEST;
1019                 break;
1020 #endif
1021
1022         case BOOKE_INTERRUPT_DTLB_MISS: {
1023                 unsigned long eaddr = vcpu->arch.fault_dear;
1024                 int gtlb_index;
1025                 gpa_t gpaddr;
1026                 gfn_t gfn;
1027
1028 #ifdef CONFIG_KVM_E500V2
1029                 if (!(vcpu->arch.shared->msr & MSR_PR) &&
1030                     (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) {
1031                         kvmppc_map_magic(vcpu);
1032                         kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
1033                         r = RESUME_GUEST;
1034
1035                         break;
1036                 }
1037 #endif
1038
1039                 /* Check the guest TLB. */
1040                 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
1041                 if (gtlb_index < 0) {
1042                         /* The guest didn't have a mapping for it. */
1043                         kvmppc_core_queue_dtlb_miss(vcpu,
1044                                                     vcpu->arch.fault_dear,
1045                                                     vcpu->arch.fault_esr);
1046                         kvmppc_mmu_dtlb_miss(vcpu);
1047                         kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
1048                         r = RESUME_GUEST;
1049                         break;
1050                 }
1051
1052                 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
1053                 gfn = gpaddr >> PAGE_SHIFT;
1054
1055                 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1056                         /* The guest TLB had a mapping, but the shadow TLB
1057                          * didn't, and it is RAM. This could be because:
1058                          * a) the entry is mapping the host kernel, or
1059                          * b) the guest used a large mapping which we're faking
1060                          * Either way, we need to satisfy the fault without
1061                          * invoking the guest. */
1062                         kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
1063                         kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
1064                         r = RESUME_GUEST;
1065                 } else {
1066                         /* Guest has mapped and accessed a page which is not
1067                          * actually RAM. */
1068                         vcpu->arch.paddr_accessed = gpaddr;
1069                         vcpu->arch.vaddr_accessed = eaddr;
1070                         r = kvmppc_emulate_mmio(run, vcpu);
1071                         kvmppc_account_exit(vcpu, MMIO_EXITS);
1072                 }
1073
1074                 break;
1075         }
1076
1077         case BOOKE_INTERRUPT_ITLB_MISS: {
1078                 unsigned long eaddr = vcpu->arch.pc;
1079                 gpa_t gpaddr;
1080                 gfn_t gfn;
1081                 int gtlb_index;
1082
1083                 r = RESUME_GUEST;
1084
1085                 /* Check the guest TLB. */
1086                 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
1087                 if (gtlb_index < 0) {
1088                         /* The guest didn't have a mapping for it. */
1089                         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
1090                         kvmppc_mmu_itlb_miss(vcpu);
1091                         kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
1092                         break;
1093                 }
1094
1095                 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
1096
1097                 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
1098                 gfn = gpaddr >> PAGE_SHIFT;
1099
1100                 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
1101                         /* The guest TLB had a mapping, but the shadow TLB
1102                          * didn't. This could be because:
1103                          * a) the entry is mapping the host kernel, or
1104                          * b) the guest used a large mapping which we're faking
1105                          * Either way, we need to satisfy the fault without
1106                          * invoking the guest. */
1107                         kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
1108                 } else {
1109                         /* Guest mapped and leaped at non-RAM! */
1110                         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
1111                 }
1112
1113                 break;
1114         }
1115
1116         case BOOKE_INTERRUPT_DEBUG: {
1117                 u32 dbsr;
1118
1119                 vcpu->arch.pc = mfspr(SPRN_CSRR0);
1120
1121                 /* clear IAC events in DBSR register */
1122                 dbsr = mfspr(SPRN_DBSR);
1123                 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
1124                 mtspr(SPRN_DBSR, dbsr);
1125
1126                 run->exit_reason = KVM_EXIT_DEBUG;
1127                 kvmppc_account_exit(vcpu, DEBUG_EXITS);
1128                 r = RESUME_HOST;
1129                 break;
1130         }
1131
1132         default:
1133                 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
1134                 BUG();
1135         }
1136
1137         /*
1138          * To avoid clobbering exit_reason, only check for signals if we
1139          * aren't already exiting to userspace for some other reason.
1140          */
1141         if (!(r & RESUME_HOST)) {
1142                 local_irq_disable();
1143                 s = kvmppc_prepare_to_enter(vcpu);
1144                 if (s <= 0) {
1145                         local_irq_enable();
1146                         r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
1147                 } else {
1148                         kvmppc_lazy_ee_enable();
1149                 }
1150         }
1151
1152         return r;
1153 }
1154
1155 static void kvmppc_set_tsr(struct kvm_vcpu *vcpu, u32 new_tsr)
1156 {
1157         u32 old_tsr = vcpu->arch.tsr;
1158
1159         vcpu->arch.tsr = new_tsr;
1160
1161         if ((old_tsr ^ vcpu->arch.tsr) & (TSR_ENW | TSR_WIS))
1162                 arm_next_watchdog(vcpu);
1163
1164         update_timer_ints(vcpu);
1165 }
1166
1167 /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
1168 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1169 {
1170         int i;
1171         int r;
1172
1173         vcpu->arch.pc = 0;
1174         vcpu->arch.shared->pir = vcpu->vcpu_id;
1175         kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
1176         kvmppc_set_msr(vcpu, 0);
1177
1178 #ifndef CONFIG_KVM_BOOKE_HV
1179         vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS;
1180         vcpu->arch.shadow_pid = 1;
1181         vcpu->arch.shared->msr = 0;
1182 #endif
1183
1184         /* Eye-catching numbers so we know if the guest takes an interrupt
1185          * before it's programmed its own IVPR/IVORs. */
1186         vcpu->arch.ivpr = 0x55550000;
1187         for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
1188                 vcpu->arch.ivor[i] = 0x7700 | i * 4;
1189
1190         kvmppc_init_timing_stats(vcpu);
1191
1192         r = kvmppc_core_vcpu_setup(vcpu);
1193         kvmppc_sanity_check(vcpu);
1194         return r;
1195 }
1196
1197 int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
1198 {
1199         /* setup watchdog timer once */
1200         spin_lock_init(&vcpu->arch.wdt_lock);
1201         setup_timer(&vcpu->arch.wdt_timer, kvmppc_watchdog_func,
1202                     (unsigned long)vcpu);
1203
1204         return 0;
1205 }
1206
1207 void kvmppc_subarch_vcpu_uninit(struct kvm_vcpu *vcpu)
1208 {
1209         del_timer_sync(&vcpu->arch.wdt_timer);
1210 }
1211
1212 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1213 {
1214         int i;
1215
1216         regs->pc = vcpu->arch.pc;
1217         regs->cr = kvmppc_get_cr(vcpu);
1218         regs->ctr = vcpu->arch.ctr;
1219         regs->lr = vcpu->arch.lr;
1220         regs->xer = kvmppc_get_xer(vcpu);
1221         regs->msr = vcpu->arch.shared->msr;
1222         regs->srr0 = vcpu->arch.shared->srr0;
1223         regs->srr1 = vcpu->arch.shared->srr1;
1224         regs->pid = vcpu->arch.pid;
1225         regs->sprg0 = vcpu->arch.shared->sprg0;
1226         regs->sprg1 = vcpu->arch.shared->sprg1;
1227         regs->sprg2 = vcpu->arch.shared->sprg2;
1228         regs->sprg3 = vcpu->arch.shared->sprg3;
1229         regs->sprg4 = vcpu->arch.shared->sprg4;
1230         regs->sprg5 = vcpu->arch.shared->sprg5;
1231         regs->sprg6 = vcpu->arch.shared->sprg6;
1232         regs->sprg7 = vcpu->arch.shared->sprg7;
1233
1234         for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1235                 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
1236
1237         return 0;
1238 }
1239
1240 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
1241 {
1242         int i;
1243
1244         vcpu->arch.pc = regs->pc;
1245         kvmppc_set_cr(vcpu, regs->cr);
1246         vcpu->arch.ctr = regs->ctr;
1247         vcpu->arch.lr = regs->lr;
1248         kvmppc_set_xer(vcpu, regs->xer);
1249         kvmppc_set_msr(vcpu, regs->msr);
1250         vcpu->arch.shared->srr0 = regs->srr0;
1251         vcpu->arch.shared->srr1 = regs->srr1;
1252         kvmppc_set_pid(vcpu, regs->pid);
1253         vcpu->arch.shared->sprg0 = regs->sprg0;
1254         vcpu->arch.shared->sprg1 = regs->sprg1;
1255         vcpu->arch.shared->sprg2 = regs->sprg2;
1256         vcpu->arch.shared->sprg3 = regs->sprg3;
1257         vcpu->arch.shared->sprg4 = regs->sprg4;
1258         vcpu->arch.shared->sprg5 = regs->sprg5;
1259         vcpu->arch.shared->sprg6 = regs->sprg6;
1260         vcpu->arch.shared->sprg7 = regs->sprg7;
1261
1262         for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
1263                 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
1264
1265         return 0;
1266 }
1267
1268 static void get_sregs_base(struct kvm_vcpu *vcpu,
1269                            struct kvm_sregs *sregs)
1270 {
1271         u64 tb = get_tb();
1272
1273         sregs->u.e.features |= KVM_SREGS_E_BASE;
1274
1275         sregs->u.e.csrr0 = vcpu->arch.csrr0;
1276         sregs->u.e.csrr1 = vcpu->arch.csrr1;
1277         sregs->u.e.mcsr = vcpu->arch.mcsr;
1278         sregs->u.e.esr = get_guest_esr(vcpu);
1279         sregs->u.e.dear = get_guest_dear(vcpu);
1280         sregs->u.e.tsr = vcpu->arch.tsr;
1281         sregs->u.e.tcr = vcpu->arch.tcr;
1282         sregs->u.e.dec = kvmppc_get_dec(vcpu, tb);
1283         sregs->u.e.tb = tb;
1284         sregs->u.e.vrsave = vcpu->arch.vrsave;
1285 }
1286
1287 static int set_sregs_base(struct kvm_vcpu *vcpu,
1288                           struct kvm_sregs *sregs)
1289 {
1290         if (!(sregs->u.e.features & KVM_SREGS_E_BASE))
1291                 return 0;
1292
1293         vcpu->arch.csrr0 = sregs->u.e.csrr0;
1294         vcpu->arch.csrr1 = sregs->u.e.csrr1;
1295         vcpu->arch.mcsr = sregs->u.e.mcsr;
1296         set_guest_esr(vcpu, sregs->u.e.esr);
1297         set_guest_dear(vcpu, sregs->u.e.dear);
1298         vcpu->arch.vrsave = sregs->u.e.vrsave;
1299         kvmppc_set_tcr(vcpu, sregs->u.e.tcr);
1300
1301         if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) {
1302                 vcpu->arch.dec = sregs->u.e.dec;
1303                 kvmppc_emulate_dec(vcpu);
1304         }
1305
1306         if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR)
1307                 kvmppc_set_tsr(vcpu, sregs->u.e.tsr);
1308
1309         return 0;
1310 }
1311
1312 static void get_sregs_arch206(struct kvm_vcpu *vcpu,
1313                               struct kvm_sregs *sregs)
1314 {
1315         sregs->u.e.features |= KVM_SREGS_E_ARCH206;
1316
1317         sregs->u.e.pir = vcpu->vcpu_id;
1318         sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0;
1319         sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1;
1320         sregs->u.e.decar = vcpu->arch.decar;
1321         sregs->u.e.ivpr = vcpu->arch.ivpr;
1322 }
1323
1324 static int set_sregs_arch206(struct kvm_vcpu *vcpu,
1325                              struct kvm_sregs *sregs)
1326 {
1327         if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206))
1328                 return 0;
1329
1330         if (sregs->u.e.pir != vcpu->vcpu_id)
1331                 return -EINVAL;
1332
1333         vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0;
1334         vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1;
1335         vcpu->arch.decar = sregs->u.e.decar;
1336         vcpu->arch.ivpr = sregs->u.e.ivpr;
1337
1338         return 0;
1339 }
1340
1341 void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1342 {
1343         sregs->u.e.features |= KVM_SREGS_E_IVOR;
1344
1345         sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL];
1346         sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK];
1347         sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE];
1348         sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE];
1349         sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL];
1350         sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT];
1351         sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM];
1352         sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL];
1353         sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL];
1354         sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL];
1355         sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER];
1356         sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT];
1357         sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG];
1358         sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS];
1359         sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS];
1360         sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG];
1361 }
1362
1363 int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
1364 {
1365         if (!(sregs->u.e.features & KVM_SREGS_E_IVOR))
1366                 return 0;
1367
1368         vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0];
1369         vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1];
1370         vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2];
1371         vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3];
1372         vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4];
1373         vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5];
1374         vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6];
1375         vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7];
1376         vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8];
1377         vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9];
1378         vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10];
1379         vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11];
1380         vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12];
1381         vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13];
1382         vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14];
1383         vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15];
1384
1385         return 0;
1386 }
1387
1388 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
1389                                   struct kvm_sregs *sregs)
1390 {
1391         sregs->pvr = vcpu->arch.pvr;
1392
1393         get_sregs_base(vcpu, sregs);
1394         get_sregs_arch206(vcpu, sregs);
1395         kvmppc_core_get_sregs(vcpu, sregs);
1396         return 0;
1397 }
1398
1399 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1400                                   struct kvm_sregs *sregs)
1401 {
1402         int ret;
1403
1404         if (vcpu->arch.pvr != sregs->pvr)
1405                 return -EINVAL;
1406
1407         ret = set_sregs_base(vcpu, sregs);
1408         if (ret < 0)
1409                 return ret;
1410
1411         ret = set_sregs_arch206(vcpu, sregs);
1412         if (ret < 0)
1413                 return ret;
1414
1415         return kvmppc_core_set_sregs(vcpu, sregs);
1416 }
1417
1418 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1419 {
1420         int r = 0;
1421         union kvmppc_one_reg val;
1422         int size;
1423         long int i;
1424
1425         size = one_reg_size(reg->id);
1426         if (size > sizeof(val))
1427                 return -EINVAL;
1428
1429         switch (reg->id) {
1430         case KVM_REG_PPC_IAC1:
1431         case KVM_REG_PPC_IAC2:
1432         case KVM_REG_PPC_IAC3:
1433         case KVM_REG_PPC_IAC4:
1434                 i = reg->id - KVM_REG_PPC_IAC1;
1435                 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.iac[i]);
1436                 break;
1437         case KVM_REG_PPC_DAC1:
1438         case KVM_REG_PPC_DAC2:
1439                 i = reg->id - KVM_REG_PPC_DAC1;
1440                 val = get_reg_val(reg->id, vcpu->arch.dbg_reg.dac[i]);
1441                 break;
1442         case KVM_REG_PPC_EPR: {
1443                 u32 epr = get_guest_epr(vcpu);
1444                 val = get_reg_val(reg->id, epr);
1445                 break;
1446         }
1447 #if defined(CONFIG_64BIT)
1448         case KVM_REG_PPC_EPCR:
1449                 val = get_reg_val(reg->id, vcpu->arch.epcr);
1450                 break;
1451 #endif
1452         case KVM_REG_PPC_TCR:
1453                 val = get_reg_val(reg->id, vcpu->arch.tcr);
1454                 break;
1455         case KVM_REG_PPC_TSR:
1456                 val = get_reg_val(reg->id, vcpu->arch.tsr);
1457                 break;
1458         case KVM_REG_PPC_DEBUG_INST:
1459                 val = get_reg_val(reg->id, KVMPPC_INST_EHPRIV);
1460                 break;
1461         default:
1462                 r = kvmppc_get_one_reg(vcpu, reg->id, &val);
1463                 break;
1464         }
1465
1466         if (r)
1467                 return r;
1468
1469         if (copy_to_user((char __user *)(unsigned long)reg->addr, &val, size))
1470                 r = -EFAULT;
1471
1472         return r;
1473 }
1474
1475 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg)
1476 {
1477         int r = 0;
1478         union kvmppc_one_reg val;
1479         int size;
1480         long int i;
1481
1482         size = one_reg_size(reg->id);
1483         if (size > sizeof(val))
1484                 return -EINVAL;
1485
1486         if (copy_from_user(&val, (char __user *)(unsigned long)reg->addr, size))
1487                 return -EFAULT;
1488
1489         switch (reg->id) {
1490         case KVM_REG_PPC_IAC1:
1491         case KVM_REG_PPC_IAC2:
1492         case KVM_REG_PPC_IAC3:
1493         case KVM_REG_PPC_IAC4:
1494                 i = reg->id - KVM_REG_PPC_IAC1;
1495                 vcpu->arch.dbg_reg.iac[i] = set_reg_val(reg->id, val);
1496                 break;
1497         case KVM_REG_PPC_DAC1:
1498         case KVM_REG_PPC_DAC2:
1499                 i = reg->id - KVM_REG_PPC_DAC1;
1500                 vcpu->arch.dbg_reg.dac[i] = set_reg_val(reg->id, val);
1501                 break;
1502         case KVM_REG_PPC_EPR: {
1503                 u32 new_epr = set_reg_val(reg->id, val);
1504                 kvmppc_set_epr(vcpu, new_epr);
1505                 break;
1506         }
1507 #if defined(CONFIG_64BIT)
1508         case KVM_REG_PPC_EPCR: {
1509                 u32 new_epcr = set_reg_val(reg->id, val);
1510                 kvmppc_set_epcr(vcpu, new_epcr);
1511                 break;
1512         }
1513 #endif
1514         case KVM_REG_PPC_OR_TSR: {
1515                 u32 tsr_bits = set_reg_val(reg->id, val);
1516                 kvmppc_set_tsr_bits(vcpu, tsr_bits);
1517                 break;
1518         }
1519         case KVM_REG_PPC_CLEAR_TSR: {
1520                 u32 tsr_bits = set_reg_val(reg->id, val);
1521                 kvmppc_clr_tsr_bits(vcpu, tsr_bits);
1522                 break;
1523         }
1524         case KVM_REG_PPC_TSR: {
1525                 u32 tsr = set_reg_val(reg->id, val);
1526                 kvmppc_set_tsr(vcpu, tsr);
1527                 break;
1528         }
1529         case KVM_REG_PPC_TCR: {
1530                 u32 tcr = set_reg_val(reg->id, val);
1531                 kvmppc_set_tcr(vcpu, tcr);
1532                 break;
1533         }
1534         default:
1535                 r = kvmppc_set_one_reg(vcpu, reg->id, &val);
1536                 break;
1537         }
1538
1539         return r;
1540 }
1541
1542 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
1543                                          struct kvm_guest_debug *dbg)
1544 {
1545         return -EINVAL;
1546 }
1547
1548 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1549 {
1550         return -ENOTSUPP;
1551 }
1552
1553 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
1554 {
1555         return -ENOTSUPP;
1556 }
1557
1558 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
1559                                   struct kvm_translation *tr)
1560 {
1561         int r;
1562
1563         r = kvmppc_core_vcpu_translate(vcpu, tr);
1564         return r;
1565 }
1566
1567 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
1568 {
1569         return -ENOTSUPP;
1570 }
1571
1572 void kvmppc_core_free_memslot(struct kvm_memory_slot *free,
1573                               struct kvm_memory_slot *dont)
1574 {
1575 }
1576
1577 int kvmppc_core_create_memslot(struct kvm_memory_slot *slot,
1578                                unsigned long npages)
1579 {
1580         return 0;
1581 }
1582
1583 int kvmppc_core_prepare_memory_region(struct kvm *kvm,
1584                                       struct kvm_memory_slot *memslot,
1585                                       struct kvm_userspace_memory_region *mem)
1586 {
1587         return 0;
1588 }
1589
1590 void kvmppc_core_commit_memory_region(struct kvm *kvm,
1591                                 struct kvm_userspace_memory_region *mem,
1592                                 const struct kvm_memory_slot *old)
1593 {
1594 }
1595
1596 void kvmppc_core_flush_memslot(struct kvm *kvm, struct kvm_memory_slot *memslot)
1597 {
1598 }
1599
1600 void kvmppc_set_epcr(struct kvm_vcpu *vcpu, u32 new_epcr)
1601 {
1602 #if defined(CONFIG_64BIT)
1603         vcpu->arch.epcr = new_epcr;
1604 #ifdef CONFIG_KVM_BOOKE_HV
1605         vcpu->arch.shadow_epcr &= ~SPRN_EPCR_GICM;
1606         if (vcpu->arch.epcr  & SPRN_EPCR_ICM)
1607                 vcpu->arch.shadow_epcr |= SPRN_EPCR_GICM;
1608 #endif
1609 #endif
1610 }
1611
1612 void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr)
1613 {
1614         vcpu->arch.tcr = new_tcr;
1615         arm_next_watchdog(vcpu);
1616         update_timer_ints(vcpu);
1617 }
1618
1619 void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1620 {
1621         set_bits(tsr_bits, &vcpu->arch.tsr);
1622         smp_wmb();
1623         kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
1624         kvm_vcpu_kick(vcpu);
1625 }
1626
1627 void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits)
1628 {
1629         clear_bits(tsr_bits, &vcpu->arch.tsr);
1630
1631         /*
1632          * We may have stopped the watchdog due to
1633          * being stuck on final expiration.
1634          */
1635         if (tsr_bits & (TSR_ENW | TSR_WIS))
1636                 arm_next_watchdog(vcpu);
1637
1638         update_timer_ints(vcpu);
1639 }
1640
1641 void kvmppc_decrementer_func(unsigned long data)
1642 {
1643         struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data;
1644
1645         if (vcpu->arch.tcr & TCR_ARE) {
1646                 vcpu->arch.dec = vcpu->arch.decar;
1647                 kvmppc_emulate_dec(vcpu);
1648         }
1649
1650         kvmppc_set_tsr_bits(vcpu, TSR_DIS);
1651 }
1652
1653 void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1654 {
1655         vcpu->cpu = smp_processor_id();
1656         current->thread.kvm_vcpu = vcpu;
1657 }
1658
1659 void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu)
1660 {
1661         current->thread.kvm_vcpu = NULL;
1662         vcpu->cpu = -1;
1663 }
1664
1665 int __init kvmppc_booke_init(void)
1666 {
1667 #ifndef CONFIG_KVM_BOOKE_HV
1668         unsigned long ivor[16];
1669         unsigned long *handler = kvmppc_booke_handler_addr;
1670         unsigned long max_ivor = 0;
1671         unsigned long handler_len;
1672         int i;
1673
1674         /* We install our own exception handlers by hijacking IVPR. IVPR must
1675          * be 16-bit aligned, so we need a 64KB allocation. */
1676         kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1677                                                  VCPU_SIZE_ORDER);
1678         if (!kvmppc_booke_handlers)
1679                 return -ENOMEM;
1680
1681         /* XXX make sure our handlers are smaller than Linux's */
1682
1683         /* Copy our interrupt handlers to match host IVORs. That way we don't
1684          * have to swap the IVORs on every guest/host transition. */
1685         ivor[0] = mfspr(SPRN_IVOR0);
1686         ivor[1] = mfspr(SPRN_IVOR1);
1687         ivor[2] = mfspr(SPRN_IVOR2);
1688         ivor[3] = mfspr(SPRN_IVOR3);
1689         ivor[4] = mfspr(SPRN_IVOR4);
1690         ivor[5] = mfspr(SPRN_IVOR5);
1691         ivor[6] = mfspr(SPRN_IVOR6);
1692         ivor[7] = mfspr(SPRN_IVOR7);
1693         ivor[8] = mfspr(SPRN_IVOR8);
1694         ivor[9] = mfspr(SPRN_IVOR9);
1695         ivor[10] = mfspr(SPRN_IVOR10);
1696         ivor[11] = mfspr(SPRN_IVOR11);
1697         ivor[12] = mfspr(SPRN_IVOR12);
1698         ivor[13] = mfspr(SPRN_IVOR13);
1699         ivor[14] = mfspr(SPRN_IVOR14);
1700         ivor[15] = mfspr(SPRN_IVOR15);
1701
1702         for (i = 0; i < 16; i++) {
1703                 if (ivor[i] > max_ivor)
1704                         max_ivor = i;
1705
1706                 handler_len = handler[i + 1] - handler[i];
1707                 memcpy((void *)kvmppc_booke_handlers + ivor[i],
1708                        (void *)handler[i], handler_len);
1709         }
1710
1711         handler_len = handler[max_ivor + 1] - handler[max_ivor];
1712         flush_icache_range(kvmppc_booke_handlers, kvmppc_booke_handlers +
1713                            ivor[max_ivor] + handler_len);
1714 #endif /* !BOOKE_HV */
1715         return 0;
1716 }
1717
1718 void __exit kvmppc_booke_exit(void)
1719 {
1720         free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
1721         kvm_exit();
1722 }