Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[pandora-kernel.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  *
17  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18  *          Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
19  */
20
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/kvm_host.h>
24 #include <linux/module.h>
25 #include <linux/vmalloc.h>
26 #include <linux/fs.h>
27
28 #include <asm/cputable.h>
29 #include <asm/uaccess.h>
30 #include <asm/kvm_ppc.h>
31 #include "timing.h"
32 #include <asm/cacheflush.h>
33
34 #include "booke.h"
35
36 unsigned long kvmppc_booke_handlers;
37
38 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
39 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
40
41 struct kvm_stats_debugfs_item debugfs_entries[] = {
42         { "mmio",       VCPU_STAT(mmio_exits) },
43         { "dcr",        VCPU_STAT(dcr_exits) },
44         { "sig",        VCPU_STAT(signal_exits) },
45         { "itlb_r",     VCPU_STAT(itlb_real_miss_exits) },
46         { "itlb_v",     VCPU_STAT(itlb_virt_miss_exits) },
47         { "dtlb_r",     VCPU_STAT(dtlb_real_miss_exits) },
48         { "dtlb_v",     VCPU_STAT(dtlb_virt_miss_exits) },
49         { "sysc",       VCPU_STAT(syscall_exits) },
50         { "isi",        VCPU_STAT(isi_exits) },
51         { "dsi",        VCPU_STAT(dsi_exits) },
52         { "inst_emu",   VCPU_STAT(emulated_inst_exits) },
53         { "dec",        VCPU_STAT(dec_exits) },
54         { "ext_intr",   VCPU_STAT(ext_intr_exits) },
55         { "halt_wakeup", VCPU_STAT(halt_wakeup) },
56         { NULL }
57 };
58
59 /* TODO: use vcpu_printf() */
60 void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
61 {
62         int i;
63
64         printk("pc:   %08lx msr:  %08lx\n", vcpu->arch.pc, vcpu->arch.msr);
65         printk("lr:   %08lx ctr:  %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
66         printk("srr0: %08lx srr1: %08lx\n", vcpu->arch.srr0, vcpu->arch.srr1);
67
68         printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
69
70         for (i = 0; i < 32; i += 4) {
71                 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
72                        kvmppc_get_gpr(vcpu, i),
73                        kvmppc_get_gpr(vcpu, i+1),
74                        kvmppc_get_gpr(vcpu, i+2),
75                        kvmppc_get_gpr(vcpu, i+3));
76         }
77 }
78
79 static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu,
80                                        unsigned int priority)
81 {
82         set_bit(priority, &vcpu->arch.pending_exceptions);
83 }
84
85 static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu,
86                                         ulong dear_flags, ulong esr_flags)
87 {
88         vcpu->arch.queued_dear = dear_flags;
89         vcpu->arch.queued_esr = esr_flags;
90         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS);
91 }
92
93 static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu,
94                                            ulong dear_flags, ulong esr_flags)
95 {
96         vcpu->arch.queued_dear = dear_flags;
97         vcpu->arch.queued_esr = esr_flags;
98         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE);
99 }
100
101 static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu,
102                                            ulong esr_flags)
103 {
104         vcpu->arch.queued_esr = esr_flags;
105         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE);
106 }
107
108 void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags)
109 {
110         vcpu->arch.queued_esr = esr_flags;
111         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM);
112 }
113
114 void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
115 {
116         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER);
117 }
118
119 int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
120 {
121         return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
122 }
123
124 void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu)
125 {
126         clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions);
127 }
128
129 void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
130                                 struct kvm_interrupt *irq)
131 {
132         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_EXTERNAL);
133 }
134
135 /* Deliver the interrupt of the corresponding priority, if possible. */
136 static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu,
137                                         unsigned int priority)
138 {
139         int allowed = 0;
140         ulong msr_mask;
141         bool update_esr = false, update_dear = false;
142
143         switch (priority) {
144         case BOOKE_IRQPRIO_DTLB_MISS:
145         case BOOKE_IRQPRIO_DATA_STORAGE:
146                 update_dear = true;
147                 /* fall through */
148         case BOOKE_IRQPRIO_INST_STORAGE:
149         case BOOKE_IRQPRIO_PROGRAM:
150                 update_esr = true;
151                 /* fall through */
152         case BOOKE_IRQPRIO_ITLB_MISS:
153         case BOOKE_IRQPRIO_SYSCALL:
154         case BOOKE_IRQPRIO_FP_UNAVAIL:
155         case BOOKE_IRQPRIO_SPE_UNAVAIL:
156         case BOOKE_IRQPRIO_SPE_FP_DATA:
157         case BOOKE_IRQPRIO_SPE_FP_ROUND:
158         case BOOKE_IRQPRIO_AP_UNAVAIL:
159         case BOOKE_IRQPRIO_ALIGNMENT:
160                 allowed = 1;
161                 msr_mask = MSR_CE|MSR_ME|MSR_DE;
162                 break;
163         case BOOKE_IRQPRIO_CRITICAL:
164         case BOOKE_IRQPRIO_WATCHDOG:
165                 allowed = vcpu->arch.msr & MSR_CE;
166                 msr_mask = MSR_ME;
167                 break;
168         case BOOKE_IRQPRIO_MACHINE_CHECK:
169                 allowed = vcpu->arch.msr & MSR_ME;
170                 msr_mask = 0;
171                 break;
172         case BOOKE_IRQPRIO_EXTERNAL:
173         case BOOKE_IRQPRIO_DECREMENTER:
174         case BOOKE_IRQPRIO_FIT:
175                 allowed = vcpu->arch.msr & MSR_EE;
176                 msr_mask = MSR_CE|MSR_ME|MSR_DE;
177                 break;
178         case BOOKE_IRQPRIO_DEBUG:
179                 allowed = vcpu->arch.msr & MSR_DE;
180                 msr_mask = MSR_ME;
181                 break;
182         }
183
184         if (allowed) {
185                 vcpu->arch.srr0 = vcpu->arch.pc;
186                 vcpu->arch.srr1 = vcpu->arch.msr;
187                 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority];
188                 if (update_esr == true)
189                         vcpu->arch.esr = vcpu->arch.queued_esr;
190                 if (update_dear == true)
191                         vcpu->arch.dear = vcpu->arch.queued_dear;
192                 kvmppc_set_msr(vcpu, vcpu->arch.msr & msr_mask);
193
194                 clear_bit(priority, &vcpu->arch.pending_exceptions);
195         }
196
197         return allowed;
198 }
199
200 /* Check pending exceptions and deliver one, if possible. */
201 void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
202 {
203         unsigned long *pending = &vcpu->arch.pending_exceptions;
204         unsigned int priority;
205
206         priority = __ffs(*pending);
207         while (priority <= BOOKE_IRQPRIO_MAX) {
208                 if (kvmppc_booke_irqprio_deliver(vcpu, priority))
209                         break;
210
211                 priority = find_next_bit(pending,
212                                          BITS_PER_BYTE * sizeof(*pending),
213                                          priority + 1);
214         }
215 }
216
217 /**
218  * kvmppc_handle_exit
219  *
220  * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
221  */
222 int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
223                        unsigned int exit_nr)
224 {
225         enum emulation_result er;
226         int r = RESUME_HOST;
227
228         /* update before a new last_exit_type is rewritten */
229         kvmppc_update_timing_stats(vcpu);
230
231         local_irq_enable();
232
233         run->exit_reason = KVM_EXIT_UNKNOWN;
234         run->ready_for_interrupt_injection = 1;
235
236         switch (exit_nr) {
237         case BOOKE_INTERRUPT_MACHINE_CHECK:
238                 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
239                 kvmppc_dump_vcpu(vcpu);
240                 r = RESUME_HOST;
241                 break;
242
243         case BOOKE_INTERRUPT_EXTERNAL:
244                 kvmppc_account_exit(vcpu, EXT_INTR_EXITS);
245                 if (need_resched())
246                         cond_resched();
247                 r = RESUME_GUEST;
248                 break;
249
250         case BOOKE_INTERRUPT_DECREMENTER:
251                 /* Since we switched IVPR back to the host's value, the host
252                  * handled this interrupt the moment we enabled interrupts.
253                  * Now we just offer it a chance to reschedule the guest. */
254                 kvmppc_account_exit(vcpu, DEC_EXITS);
255                 if (need_resched())
256                         cond_resched();
257                 r = RESUME_GUEST;
258                 break;
259
260         case BOOKE_INTERRUPT_PROGRAM:
261                 if (vcpu->arch.msr & MSR_PR) {
262                         /* Program traps generated by user-level software must be handled
263                          * by the guest kernel. */
264                         kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr);
265                         r = RESUME_GUEST;
266                         kvmppc_account_exit(vcpu, USR_PR_INST);
267                         break;
268                 }
269
270                 er = kvmppc_emulate_instruction(run, vcpu);
271                 switch (er) {
272                 case EMULATE_DONE:
273                         /* don't overwrite subtypes, just account kvm_stats */
274                         kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS);
275                         /* Future optimization: only reload non-volatiles if
276                          * they were actually modified by emulation. */
277                         r = RESUME_GUEST_NV;
278                         break;
279                 case EMULATE_DO_DCR:
280                         run->exit_reason = KVM_EXIT_DCR;
281                         r = RESUME_HOST;
282                         break;
283                 case EMULATE_FAIL:
284                         /* XXX Deliver Program interrupt to guest. */
285                         printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
286                                __func__, vcpu->arch.pc, vcpu->arch.last_inst);
287                         /* For debugging, encode the failing instruction and
288                          * report it to userspace. */
289                         run->hw.hardware_exit_reason = ~0ULL << 32;
290                         run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
291                         r = RESUME_HOST;
292                         break;
293                 default:
294                         BUG();
295                 }
296                 break;
297
298         case BOOKE_INTERRUPT_FP_UNAVAIL:
299                 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL);
300                 kvmppc_account_exit(vcpu, FP_UNAVAIL);
301                 r = RESUME_GUEST;
302                 break;
303
304         case BOOKE_INTERRUPT_SPE_UNAVAIL:
305                 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_UNAVAIL);
306                 r = RESUME_GUEST;
307                 break;
308
309         case BOOKE_INTERRUPT_SPE_FP_DATA:
310                 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA);
311                 r = RESUME_GUEST;
312                 break;
313
314         case BOOKE_INTERRUPT_SPE_FP_ROUND:
315                 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND);
316                 r = RESUME_GUEST;
317                 break;
318
319         case BOOKE_INTERRUPT_DATA_STORAGE:
320                 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear,
321                                                vcpu->arch.fault_esr);
322                 kvmppc_account_exit(vcpu, DSI_EXITS);
323                 r = RESUME_GUEST;
324                 break;
325
326         case BOOKE_INTERRUPT_INST_STORAGE:
327                 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr);
328                 kvmppc_account_exit(vcpu, ISI_EXITS);
329                 r = RESUME_GUEST;
330                 break;
331
332         case BOOKE_INTERRUPT_SYSCALL:
333                 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL);
334                 kvmppc_account_exit(vcpu, SYSCALL_EXITS);
335                 r = RESUME_GUEST;
336                 break;
337
338         case BOOKE_INTERRUPT_DTLB_MISS: {
339                 unsigned long eaddr = vcpu->arch.fault_dear;
340                 int gtlb_index;
341                 gpa_t gpaddr;
342                 gfn_t gfn;
343
344                 /* Check the guest TLB. */
345                 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr);
346                 if (gtlb_index < 0) {
347                         /* The guest didn't have a mapping for it. */
348                         kvmppc_core_queue_dtlb_miss(vcpu,
349                                                     vcpu->arch.fault_dear,
350                                                     vcpu->arch.fault_esr);
351                         kvmppc_mmu_dtlb_miss(vcpu);
352                         kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS);
353                         r = RESUME_GUEST;
354                         break;
355                 }
356
357                 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
358                 gfn = gpaddr >> PAGE_SHIFT;
359
360                 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
361                         /* The guest TLB had a mapping, but the shadow TLB
362                          * didn't, and it is RAM. This could be because:
363                          * a) the entry is mapping the host kernel, or
364                          * b) the guest used a large mapping which we're faking
365                          * Either way, we need to satisfy the fault without
366                          * invoking the guest. */
367                         kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
368                         kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS);
369                         r = RESUME_GUEST;
370                 } else {
371                         /* Guest has mapped and accessed a page which is not
372                          * actually RAM. */
373                         vcpu->arch.paddr_accessed = gpaddr;
374                         r = kvmppc_emulate_mmio(run, vcpu);
375                         kvmppc_account_exit(vcpu, MMIO_EXITS);
376                 }
377
378                 break;
379         }
380
381         case BOOKE_INTERRUPT_ITLB_MISS: {
382                 unsigned long eaddr = vcpu->arch.pc;
383                 gpa_t gpaddr;
384                 gfn_t gfn;
385                 int gtlb_index;
386
387                 r = RESUME_GUEST;
388
389                 /* Check the guest TLB. */
390                 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr);
391                 if (gtlb_index < 0) {
392                         /* The guest didn't have a mapping for it. */
393                         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS);
394                         kvmppc_mmu_itlb_miss(vcpu);
395                         kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS);
396                         break;
397                 }
398
399                 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS);
400
401                 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr);
402                 gfn = gpaddr >> PAGE_SHIFT;
403
404                 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
405                         /* The guest TLB had a mapping, but the shadow TLB
406                          * didn't. This could be because:
407                          * a) the entry is mapping the host kernel, or
408                          * b) the guest used a large mapping which we're faking
409                          * Either way, we need to satisfy the fault without
410                          * invoking the guest. */
411                         kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index);
412                 } else {
413                         /* Guest mapped and leaped at non-RAM! */
414                         kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK);
415                 }
416
417                 break;
418         }
419
420         case BOOKE_INTERRUPT_DEBUG: {
421                 u32 dbsr;
422
423                 vcpu->arch.pc = mfspr(SPRN_CSRR0);
424
425                 /* clear IAC events in DBSR register */
426                 dbsr = mfspr(SPRN_DBSR);
427                 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
428                 mtspr(SPRN_DBSR, dbsr);
429
430                 run->exit_reason = KVM_EXIT_DEBUG;
431                 kvmppc_account_exit(vcpu, DEBUG_EXITS);
432                 r = RESUME_HOST;
433                 break;
434         }
435
436         default:
437                 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
438                 BUG();
439         }
440
441         local_irq_disable();
442
443         kvmppc_core_deliver_interrupts(vcpu);
444
445         if (!(r & RESUME_HOST)) {
446                 /* To avoid clobbering exit_reason, only check for signals if
447                  * we aren't already exiting to userspace for some other
448                  * reason. */
449                 if (signal_pending(current)) {
450                         run->exit_reason = KVM_EXIT_INTR;
451                         r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
452                         kvmppc_account_exit(vcpu, SIGNAL_EXITS);
453                 }
454         }
455
456         return r;
457 }
458
459 /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
460 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
461 {
462         vcpu->arch.pc = 0;
463         vcpu->arch.msr = 0;
464         kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
465
466         vcpu->arch.shadow_pid = 1;
467
468         /* Eye-catching number so we know if the guest takes an interrupt
469          * before it's programmed its own IVPR. */
470         vcpu->arch.ivpr = 0x55550000;
471
472         kvmppc_init_timing_stats(vcpu);
473
474         return kvmppc_core_vcpu_setup(vcpu);
475 }
476
477 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
478 {
479         int i;
480
481         regs->pc = vcpu->arch.pc;
482         regs->cr = kvmppc_get_cr(vcpu);
483         regs->ctr = vcpu->arch.ctr;
484         regs->lr = vcpu->arch.lr;
485         regs->xer = kvmppc_get_xer(vcpu);
486         regs->msr = vcpu->arch.msr;
487         regs->srr0 = vcpu->arch.srr0;
488         regs->srr1 = vcpu->arch.srr1;
489         regs->pid = vcpu->arch.pid;
490         regs->sprg0 = vcpu->arch.sprg0;
491         regs->sprg1 = vcpu->arch.sprg1;
492         regs->sprg2 = vcpu->arch.sprg2;
493         regs->sprg3 = vcpu->arch.sprg3;
494         regs->sprg5 = vcpu->arch.sprg4;
495         regs->sprg6 = vcpu->arch.sprg5;
496         regs->sprg7 = vcpu->arch.sprg6;
497
498         for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
499                 regs->gpr[i] = kvmppc_get_gpr(vcpu, i);
500
501         return 0;
502 }
503
504 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
505 {
506         int i;
507
508         vcpu->arch.pc = regs->pc;
509         kvmppc_set_cr(vcpu, regs->cr);
510         vcpu->arch.ctr = regs->ctr;
511         vcpu->arch.lr = regs->lr;
512         kvmppc_set_xer(vcpu, regs->xer);
513         kvmppc_set_msr(vcpu, regs->msr);
514         vcpu->arch.srr0 = regs->srr0;
515         vcpu->arch.srr1 = regs->srr1;
516         vcpu->arch.sprg0 = regs->sprg0;
517         vcpu->arch.sprg1 = regs->sprg1;
518         vcpu->arch.sprg2 = regs->sprg2;
519         vcpu->arch.sprg3 = regs->sprg3;
520         vcpu->arch.sprg5 = regs->sprg4;
521         vcpu->arch.sprg6 = regs->sprg5;
522         vcpu->arch.sprg7 = regs->sprg6;
523
524         for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
525                 kvmppc_set_gpr(vcpu, i, regs->gpr[i]);
526
527         return 0;
528 }
529
530 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
531                                   struct kvm_sregs *sregs)
532 {
533         return -ENOTSUPP;
534 }
535
536 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
537                                   struct kvm_sregs *sregs)
538 {
539         return -ENOTSUPP;
540 }
541
542 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
543 {
544         return -ENOTSUPP;
545 }
546
547 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
548 {
549         return -ENOTSUPP;
550 }
551
552 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
553                                   struct kvm_translation *tr)
554 {
555         return kvmppc_core_vcpu_translate(vcpu, tr);
556 }
557
558 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
559 {
560         return -ENOTSUPP;
561 }
562
563 int __init kvmppc_booke_init(void)
564 {
565         unsigned long ivor[16];
566         unsigned long max_ivor = 0;
567         int i;
568
569         /* We install our own exception handlers by hijacking IVPR. IVPR must
570          * be 16-bit aligned, so we need a 64KB allocation. */
571         kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
572                                                  VCPU_SIZE_ORDER);
573         if (!kvmppc_booke_handlers)
574                 return -ENOMEM;
575
576         /* XXX make sure our handlers are smaller than Linux's */
577
578         /* Copy our interrupt handlers to match host IVORs. That way we don't
579          * have to swap the IVORs on every guest/host transition. */
580         ivor[0] = mfspr(SPRN_IVOR0);
581         ivor[1] = mfspr(SPRN_IVOR1);
582         ivor[2] = mfspr(SPRN_IVOR2);
583         ivor[3] = mfspr(SPRN_IVOR3);
584         ivor[4] = mfspr(SPRN_IVOR4);
585         ivor[5] = mfspr(SPRN_IVOR5);
586         ivor[6] = mfspr(SPRN_IVOR6);
587         ivor[7] = mfspr(SPRN_IVOR7);
588         ivor[8] = mfspr(SPRN_IVOR8);
589         ivor[9] = mfspr(SPRN_IVOR9);
590         ivor[10] = mfspr(SPRN_IVOR10);
591         ivor[11] = mfspr(SPRN_IVOR11);
592         ivor[12] = mfspr(SPRN_IVOR12);
593         ivor[13] = mfspr(SPRN_IVOR13);
594         ivor[14] = mfspr(SPRN_IVOR14);
595         ivor[15] = mfspr(SPRN_IVOR15);
596
597         for (i = 0; i < 16; i++) {
598                 if (ivor[i] > max_ivor)
599                         max_ivor = ivor[i];
600
601                 memcpy((void *)kvmppc_booke_handlers + ivor[i],
602                        kvmppc_handlers_start + i * kvmppc_handler_len,
603                        kvmppc_handler_len);
604         }
605         flush_icache_range(kvmppc_booke_handlers,
606                            kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
607
608         return 0;
609 }
610
611 void __exit kvmppc_booke_exit(void)
612 {
613         free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
614         kvm_exit();
615 }