KVM: ppc: refactor instruction emulation into generic and core-specific pieces
[safe/jmp/linux-2.6] / 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 #include <asm/cputable.h>
28 #include <asm/uaccess.h>
29 #include <asm/kvm_ppc.h>
30 #include <asm/cacheflush.h>
31
32 #include "booke.h"
33 #include "44x_tlb.h"
34
35 unsigned long kvmppc_booke_handlers;
36
37 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
38 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
39
40 struct kvm_stats_debugfs_item debugfs_entries[] = {
41         { "exits",      VCPU_STAT(sum_exits) },
42         { "mmio",       VCPU_STAT(mmio_exits) },
43         { "dcr",        VCPU_STAT(dcr_exits) },
44         { "sig",        VCPU_STAT(signal_exits) },
45         { "light",      VCPU_STAT(light_exits) },
46         { "itlb_r",     VCPU_STAT(itlb_real_miss_exits) },
47         { "itlb_v",     VCPU_STAT(itlb_virt_miss_exits) },
48         { "dtlb_r",     VCPU_STAT(dtlb_real_miss_exits) },
49         { "dtlb_v",     VCPU_STAT(dtlb_virt_miss_exits) },
50         { "sysc",       VCPU_STAT(syscall_exits) },
51         { "isi",        VCPU_STAT(isi_exits) },
52         { "dsi",        VCPU_STAT(dsi_exits) },
53         { "inst_emu",   VCPU_STAT(emulated_inst_exits) },
54         { "dec",        VCPU_STAT(dec_exits) },
55         { "ext_intr",   VCPU_STAT(ext_intr_exits) },
56         { "halt_wakeup", VCPU_STAT(halt_wakeup) },
57         { NULL }
58 };
59
60 static const u32 interrupt_msr_mask[16] = {
61         [BOOKE_INTERRUPT_CRITICAL]      = MSR_ME,
62         [BOOKE_INTERRUPT_MACHINE_CHECK] = 0,
63         [BOOKE_INTERRUPT_DATA_STORAGE]  = MSR_CE|MSR_ME|MSR_DE,
64         [BOOKE_INTERRUPT_INST_STORAGE]  = MSR_CE|MSR_ME|MSR_DE,
65         [BOOKE_INTERRUPT_EXTERNAL]      = MSR_CE|MSR_ME|MSR_DE,
66         [BOOKE_INTERRUPT_ALIGNMENT]     = MSR_CE|MSR_ME|MSR_DE,
67         [BOOKE_INTERRUPT_PROGRAM]       = MSR_CE|MSR_ME|MSR_DE,
68         [BOOKE_INTERRUPT_FP_UNAVAIL]    = MSR_CE|MSR_ME|MSR_DE,
69         [BOOKE_INTERRUPT_SYSCALL]       = MSR_CE|MSR_ME|MSR_DE,
70         [BOOKE_INTERRUPT_AP_UNAVAIL]    = MSR_CE|MSR_ME|MSR_DE,
71         [BOOKE_INTERRUPT_DECREMENTER]   = MSR_CE|MSR_ME|MSR_DE,
72         [BOOKE_INTERRUPT_FIT]           = MSR_CE|MSR_ME|MSR_DE,
73         [BOOKE_INTERRUPT_WATCHDOG]      = MSR_ME,
74         [BOOKE_INTERRUPT_DTLB_MISS]     = MSR_CE|MSR_ME|MSR_DE,
75         [BOOKE_INTERRUPT_ITLB_MISS]     = MSR_CE|MSR_ME|MSR_DE,
76         [BOOKE_INTERRUPT_DEBUG]         = MSR_ME,
77 };
78
79 const unsigned char exception_priority[] = {
80         [BOOKE_INTERRUPT_DATA_STORAGE] = 0,
81         [BOOKE_INTERRUPT_INST_STORAGE] = 1,
82         [BOOKE_INTERRUPT_ALIGNMENT] = 2,
83         [BOOKE_INTERRUPT_PROGRAM] = 3,
84         [BOOKE_INTERRUPT_FP_UNAVAIL] = 4,
85         [BOOKE_INTERRUPT_SYSCALL] = 5,
86         [BOOKE_INTERRUPT_AP_UNAVAIL] = 6,
87         [BOOKE_INTERRUPT_DTLB_MISS] = 7,
88         [BOOKE_INTERRUPT_ITLB_MISS] = 8,
89         [BOOKE_INTERRUPT_MACHINE_CHECK] = 9,
90         [BOOKE_INTERRUPT_DEBUG] = 10,
91         [BOOKE_INTERRUPT_CRITICAL] = 11,
92         [BOOKE_INTERRUPT_WATCHDOG] = 12,
93         [BOOKE_INTERRUPT_EXTERNAL] = 13,
94         [BOOKE_INTERRUPT_FIT] = 14,
95         [BOOKE_INTERRUPT_DECREMENTER] = 15,
96 };
97
98 const unsigned char priority_exception[] = {
99         BOOKE_INTERRUPT_DATA_STORAGE,
100         BOOKE_INTERRUPT_INST_STORAGE,
101         BOOKE_INTERRUPT_ALIGNMENT,
102         BOOKE_INTERRUPT_PROGRAM,
103         BOOKE_INTERRUPT_FP_UNAVAIL,
104         BOOKE_INTERRUPT_SYSCALL,
105         BOOKE_INTERRUPT_AP_UNAVAIL,
106         BOOKE_INTERRUPT_DTLB_MISS,
107         BOOKE_INTERRUPT_ITLB_MISS,
108         BOOKE_INTERRUPT_MACHINE_CHECK,
109         BOOKE_INTERRUPT_DEBUG,
110         BOOKE_INTERRUPT_CRITICAL,
111         BOOKE_INTERRUPT_WATCHDOG,
112         BOOKE_INTERRUPT_EXTERNAL,
113         BOOKE_INTERRUPT_FIT,
114         BOOKE_INTERRUPT_DECREMENTER,
115 };
116
117
118 /* TODO: use vcpu_printf() */
119 void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
120 {
121         int i;
122
123         printk("pc:   %08x msr:  %08x\n", vcpu->arch.pc, vcpu->arch.msr);
124         printk("lr:   %08x ctr:  %08x\n", vcpu->arch.lr, vcpu->arch.ctr);
125         printk("srr0: %08x srr1: %08x\n", vcpu->arch.srr0, vcpu->arch.srr1);
126
127         printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
128
129         for (i = 0; i < 32; i += 4) {
130                 printk("gpr%02d: %08x %08x %08x %08x\n", i,
131                        vcpu->arch.gpr[i],
132                        vcpu->arch.gpr[i+1],
133                        vcpu->arch.gpr[i+2],
134                        vcpu->arch.gpr[i+3]);
135         }
136 }
137
138 static void kvmppc_booke_queue_exception(struct kvm_vcpu *vcpu, int exception)
139 {
140         unsigned int priority = exception_priority[exception];
141         set_bit(priority, &vcpu->arch.pending_exceptions);
142 }
143
144 static void kvmppc_booke_clear_exception(struct kvm_vcpu *vcpu, int exception)
145 {
146         unsigned int priority = exception_priority[exception];
147         clear_bit(priority, &vcpu->arch.pending_exceptions);
148 }
149
150 void kvmppc_core_queue_program(struct kvm_vcpu *vcpu)
151 {
152         kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_PROGRAM);
153 }
154
155 void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
156 {
157         kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_DECREMENTER);
158 }
159
160 int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
161 {
162         unsigned int priority = exception_priority[BOOKE_INTERRUPT_DECREMENTER];
163         return test_bit(priority, &vcpu->arch.pending_exceptions);
164 }
165
166 void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
167                                 struct kvm_interrupt *irq)
168 {
169         kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_EXTERNAL);
170 }
171
172 /* Check if we are ready to deliver the interrupt */
173 static int kvmppc_can_deliver_interrupt(struct kvm_vcpu *vcpu, int interrupt)
174 {
175         int r;
176
177         switch (interrupt) {
178         case BOOKE_INTERRUPT_CRITICAL:
179                 r = vcpu->arch.msr & MSR_CE;
180                 break;
181         case BOOKE_INTERRUPT_MACHINE_CHECK:
182                 r = vcpu->arch.msr & MSR_ME;
183                 break;
184         case BOOKE_INTERRUPT_EXTERNAL:
185                 r = vcpu->arch.msr & MSR_EE;
186                 break;
187         case BOOKE_INTERRUPT_DECREMENTER:
188                 r = vcpu->arch.msr & MSR_EE;
189                 break;
190         case BOOKE_INTERRUPT_FIT:
191                 r = vcpu->arch.msr & MSR_EE;
192                 break;
193         case BOOKE_INTERRUPT_WATCHDOG:
194                 r = vcpu->arch.msr & MSR_CE;
195                 break;
196         case BOOKE_INTERRUPT_DEBUG:
197                 r = vcpu->arch.msr & MSR_DE;
198                 break;
199         default:
200                 r = 1;
201         }
202
203         return r;
204 }
205
206 static void kvmppc_booke_deliver_interrupt(struct kvm_vcpu *vcpu, int interrupt)
207 {
208         switch (interrupt) {
209         case BOOKE_INTERRUPT_DECREMENTER:
210                 vcpu->arch.tsr |= TSR_DIS;
211                 break;
212         }
213
214         vcpu->arch.srr0 = vcpu->arch.pc;
215         vcpu->arch.srr1 = vcpu->arch.msr;
216         vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[interrupt];
217         kvmppc_set_msr(vcpu, vcpu->arch.msr & interrupt_msr_mask[interrupt]);
218 }
219
220 /* Check pending exceptions and deliver one, if possible. */
221 void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
222 {
223         unsigned long *pending = &vcpu->arch.pending_exceptions;
224         unsigned int exception;
225         unsigned int priority;
226
227         priority = find_first_bit(pending, BITS_PER_BYTE * sizeof(*pending));
228         while (priority <= BOOKE_MAX_INTERRUPT) {
229                 exception = priority_exception[priority];
230                 if (kvmppc_can_deliver_interrupt(vcpu, exception)) {
231                         kvmppc_booke_clear_exception(vcpu, exception);
232                         kvmppc_booke_deliver_interrupt(vcpu, exception);
233                         break;
234                 }
235
236                 priority = find_next_bit(pending,
237                                          BITS_PER_BYTE * sizeof(*pending),
238                                          priority + 1);
239         }
240 }
241
242 /**
243  * kvmppc_handle_exit
244  *
245  * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
246  */
247 int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
248                        unsigned int exit_nr)
249 {
250         enum emulation_result er;
251         int r = RESUME_HOST;
252
253         local_irq_enable();
254
255         run->exit_reason = KVM_EXIT_UNKNOWN;
256         run->ready_for_interrupt_injection = 1;
257
258         switch (exit_nr) {
259         case BOOKE_INTERRUPT_MACHINE_CHECK:
260                 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
261                 kvmppc_dump_vcpu(vcpu);
262                 r = RESUME_HOST;
263                 break;
264
265         case BOOKE_INTERRUPT_EXTERNAL:
266         case BOOKE_INTERRUPT_DECREMENTER:
267                 /* Since we switched IVPR back to the host's value, the host
268                  * handled this interrupt the moment we enabled interrupts.
269                  * Now we just offer it a chance to reschedule the guest. */
270
271                 /* XXX At this point the TLB still holds our shadow TLB, so if
272                  * we do reschedule the host will fault over it. Perhaps we
273                  * should politely restore the host's entries to minimize
274                  * misses before ceding control. */
275                 if (need_resched())
276                         cond_resched();
277                 if (exit_nr == BOOKE_INTERRUPT_DECREMENTER)
278                         vcpu->stat.dec_exits++;
279                 else
280                         vcpu->stat.ext_intr_exits++;
281                 r = RESUME_GUEST;
282                 break;
283
284         case BOOKE_INTERRUPT_PROGRAM:
285                 if (vcpu->arch.msr & MSR_PR) {
286                         /* Program traps generated by user-level software must be handled
287                          * by the guest kernel. */
288                         vcpu->arch.esr = vcpu->arch.fault_esr;
289                         kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_PROGRAM);
290                         r = RESUME_GUEST;
291                         break;
292                 }
293
294                 er = kvmppc_emulate_instruction(run, vcpu);
295                 switch (er) {
296                 case EMULATE_DONE:
297                         /* Future optimization: only reload non-volatiles if
298                          * they were actually modified by emulation. */
299                         vcpu->stat.emulated_inst_exits++;
300                         r = RESUME_GUEST_NV;
301                         break;
302                 case EMULATE_DO_DCR:
303                         run->exit_reason = KVM_EXIT_DCR;
304                         r = RESUME_HOST;
305                         break;
306                 case EMULATE_FAIL:
307                         /* XXX Deliver Program interrupt to guest. */
308                         printk(KERN_CRIT "%s: emulation at %x failed (%08x)\n",
309                                __func__, vcpu->arch.pc, vcpu->arch.last_inst);
310                         /* For debugging, encode the failing instruction and
311                          * report it to userspace. */
312                         run->hw.hardware_exit_reason = ~0ULL << 32;
313                         run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
314                         r = RESUME_HOST;
315                         break;
316                 default:
317                         BUG();
318                 }
319                 break;
320
321         case BOOKE_INTERRUPT_FP_UNAVAIL:
322                 kvmppc_booke_queue_exception(vcpu, exit_nr);
323                 r = RESUME_GUEST;
324                 break;
325
326         case BOOKE_INTERRUPT_DATA_STORAGE:
327                 vcpu->arch.dear = vcpu->arch.fault_dear;
328                 vcpu->arch.esr = vcpu->arch.fault_esr;
329                 kvmppc_booke_queue_exception(vcpu, exit_nr);
330                 vcpu->stat.dsi_exits++;
331                 r = RESUME_GUEST;
332                 break;
333
334         case BOOKE_INTERRUPT_INST_STORAGE:
335                 vcpu->arch.esr = vcpu->arch.fault_esr;
336                 kvmppc_booke_queue_exception(vcpu, exit_nr);
337                 vcpu->stat.isi_exits++;
338                 r = RESUME_GUEST;
339                 break;
340
341         case BOOKE_INTERRUPT_SYSCALL:
342                 kvmppc_booke_queue_exception(vcpu, exit_nr);
343                 vcpu->stat.syscall_exits++;
344                 r = RESUME_GUEST;
345                 break;
346
347         case BOOKE_INTERRUPT_DTLB_MISS: {
348                 struct kvmppc_44x_tlbe *gtlbe;
349                 unsigned long eaddr = vcpu->arch.fault_dear;
350                 gfn_t gfn;
351
352                 /* Check the guest TLB. */
353                 gtlbe = kvmppc_44x_dtlb_search(vcpu, eaddr);
354                 if (!gtlbe) {
355                         /* The guest didn't have a mapping for it. */
356                         kvmppc_booke_queue_exception(vcpu, exit_nr);
357                         vcpu->arch.dear = vcpu->arch.fault_dear;
358                         vcpu->arch.esr = vcpu->arch.fault_esr;
359                         vcpu->stat.dtlb_real_miss_exits++;
360                         r = RESUME_GUEST;
361                         break;
362                 }
363
364                 vcpu->arch.paddr_accessed = tlb_xlate(gtlbe, eaddr);
365                 gfn = vcpu->arch.paddr_accessed >> PAGE_SHIFT;
366
367                 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
368                         /* The guest TLB had a mapping, but the shadow TLB
369                          * didn't, and it is RAM. This could be because:
370                          * a) the entry is mapping the host kernel, or
371                          * b) the guest used a large mapping which we're faking
372                          * Either way, we need to satisfy the fault without
373                          * invoking the guest. */
374                         kvmppc_mmu_map(vcpu, eaddr, gfn, gtlbe->tid,
375                                        gtlbe->word2);
376                         vcpu->stat.dtlb_virt_miss_exits++;
377                         r = RESUME_GUEST;
378                 } else {
379                         /* Guest has mapped and accessed a page which is not
380                          * actually RAM. */
381                         r = kvmppc_emulate_mmio(run, vcpu);
382                 }
383
384                 break;
385         }
386
387         case BOOKE_INTERRUPT_ITLB_MISS: {
388                 struct kvmppc_44x_tlbe *gtlbe;
389                 unsigned long eaddr = vcpu->arch.pc;
390                 gfn_t gfn;
391
392                 r = RESUME_GUEST;
393
394                 /* Check the guest TLB. */
395                 gtlbe = kvmppc_44x_itlb_search(vcpu, eaddr);
396                 if (!gtlbe) {
397                         /* The guest didn't have a mapping for it. */
398                         kvmppc_booke_queue_exception(vcpu, exit_nr);
399                         vcpu->stat.itlb_real_miss_exits++;
400                         break;
401                 }
402
403                 vcpu->stat.itlb_virt_miss_exits++;
404
405                 gfn = tlb_xlate(gtlbe, eaddr) >> PAGE_SHIFT;
406
407                 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
408                         /* The guest TLB had a mapping, but the shadow TLB
409                          * didn't. This could be because:
410                          * a) the entry is mapping the host kernel, or
411                          * b) the guest used a large mapping which we're faking
412                          * Either way, we need to satisfy the fault without
413                          * invoking the guest. */
414                         kvmppc_mmu_map(vcpu, eaddr, gfn, gtlbe->tid,
415                                        gtlbe->word2);
416                 } else {
417                         /* Guest mapped and leaped at non-RAM! */
418                         kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_MACHINE_CHECK);
419                 }
420
421                 break;
422         }
423
424         case BOOKE_INTERRUPT_DEBUG: {
425                 u32 dbsr;
426
427                 vcpu->arch.pc = mfspr(SPRN_CSRR0);
428
429                 /* clear IAC events in DBSR register */
430                 dbsr = mfspr(SPRN_DBSR);
431                 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
432                 mtspr(SPRN_DBSR, dbsr);
433
434                 run->exit_reason = KVM_EXIT_DEBUG;
435                 r = RESUME_HOST;
436                 break;
437         }
438
439         default:
440                 printk(KERN_EMERG "exit_nr %d\n", exit_nr);
441                 BUG();
442         }
443
444         local_irq_disable();
445
446         kvmppc_core_deliver_interrupts(vcpu);
447
448         /* Do some exit accounting. */
449         vcpu->stat.sum_exits++;
450         if (!(r & RESUME_HOST)) {
451                 /* To avoid clobbering exit_reason, only check for signals if
452                  * we aren't already exiting to userspace for some other
453                  * reason. */
454                 if (signal_pending(current)) {
455                         run->exit_reason = KVM_EXIT_INTR;
456                         r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
457
458                         vcpu->stat.signal_exits++;
459                 } else {
460                         vcpu->stat.light_exits++;
461                 }
462         } else {
463                 switch (run->exit_reason) {
464                 case KVM_EXIT_MMIO:
465                         vcpu->stat.mmio_exits++;
466                         break;
467                 case KVM_EXIT_DCR:
468                         vcpu->stat.dcr_exits++;
469                         break;
470                 case KVM_EXIT_INTR:
471                         vcpu->stat.signal_exits++;
472                         break;
473                 }
474         }
475
476         return r;
477 }
478
479 /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
480 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
481 {
482         struct kvmppc_44x_tlbe *tlbe = &vcpu->arch.guest_tlb[0];
483
484         tlbe->tid = 0;
485         tlbe->word0 = PPC44x_TLB_16M | PPC44x_TLB_VALID;
486         tlbe->word1 = 0;
487         tlbe->word2 = PPC44x_TLB_SX | PPC44x_TLB_SW | PPC44x_TLB_SR;
488
489         tlbe++;
490         tlbe->tid = 0;
491         tlbe->word0 = 0xef600000 | PPC44x_TLB_4K | PPC44x_TLB_VALID;
492         tlbe->word1 = 0xef600000;
493         tlbe->word2 = PPC44x_TLB_SX | PPC44x_TLB_SW | PPC44x_TLB_SR
494                       | PPC44x_TLB_I | PPC44x_TLB_G;
495
496         vcpu->arch.pc = 0;
497         vcpu->arch.msr = 0;
498         vcpu->arch.gpr[1] = (16<<20) - 8; /* -8 for the callee-save LR slot */
499
500         vcpu->arch.shadow_pid = 1;
501
502         /* Eye-catching number so we know if the guest takes an interrupt
503          * before it's programmed its own IVPR. */
504         vcpu->arch.ivpr = 0x55550000;
505
506         /* Since the guest can directly access the timebase, it must know the
507          * real timebase frequency. Accordingly, it must see the state of
508          * CCR1[TCS]. */
509         vcpu->arch.ccr1 = mfspr(SPRN_CCR1);
510
511         return 0;
512 }
513
514 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
515 {
516         int i;
517
518         regs->pc = vcpu->arch.pc;
519         regs->cr = vcpu->arch.cr;
520         regs->ctr = vcpu->arch.ctr;
521         regs->lr = vcpu->arch.lr;
522         regs->xer = vcpu->arch.xer;
523         regs->msr = vcpu->arch.msr;
524         regs->srr0 = vcpu->arch.srr0;
525         regs->srr1 = vcpu->arch.srr1;
526         regs->pid = vcpu->arch.pid;
527         regs->sprg0 = vcpu->arch.sprg0;
528         regs->sprg1 = vcpu->arch.sprg1;
529         regs->sprg2 = vcpu->arch.sprg2;
530         regs->sprg3 = vcpu->arch.sprg3;
531         regs->sprg5 = vcpu->arch.sprg4;
532         regs->sprg6 = vcpu->arch.sprg5;
533         regs->sprg7 = vcpu->arch.sprg6;
534
535         for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
536                 regs->gpr[i] = vcpu->arch.gpr[i];
537
538         return 0;
539 }
540
541 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
542 {
543         int i;
544
545         vcpu->arch.pc = regs->pc;
546         vcpu->arch.cr = regs->cr;
547         vcpu->arch.ctr = regs->ctr;
548         vcpu->arch.lr = regs->lr;
549         vcpu->arch.xer = regs->xer;
550         vcpu->arch.msr = regs->msr;
551         vcpu->arch.srr0 = regs->srr0;
552         vcpu->arch.srr1 = regs->srr1;
553         vcpu->arch.sprg0 = regs->sprg0;
554         vcpu->arch.sprg1 = regs->sprg1;
555         vcpu->arch.sprg2 = regs->sprg2;
556         vcpu->arch.sprg3 = regs->sprg3;
557         vcpu->arch.sprg5 = regs->sprg4;
558         vcpu->arch.sprg6 = regs->sprg5;
559         vcpu->arch.sprg7 = regs->sprg6;
560
561         for (i = 0; i < ARRAY_SIZE(vcpu->arch.gpr); i++)
562                 vcpu->arch.gpr[i] = regs->gpr[i];
563
564         return 0;
565 }
566
567 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
568                                   struct kvm_sregs *sregs)
569 {
570         return -ENOTSUPP;
571 }
572
573 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
574                                   struct kvm_sregs *sregs)
575 {
576         return -ENOTSUPP;
577 }
578
579 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
580 {
581         return -ENOTSUPP;
582 }
583
584 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
585 {
586         return -ENOTSUPP;
587 }
588
589 /* 'linear_address' is actually an encoding of AS|PID|EADDR . */
590 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
591                                   struct kvm_translation *tr)
592 {
593         struct kvmppc_44x_tlbe *gtlbe;
594         int index;
595         gva_t eaddr;
596         u8 pid;
597         u8 as;
598
599         eaddr = tr->linear_address;
600         pid = (tr->linear_address >> 32) & 0xff;
601         as = (tr->linear_address >> 40) & 0x1;
602
603         index = kvmppc_44x_tlb_index(vcpu, eaddr, pid, as);
604         if (index == -1) {
605                 tr->valid = 0;
606                 return 0;
607         }
608
609         gtlbe = &vcpu->arch.guest_tlb[index];
610
611         tr->physical_address = tlb_xlate(gtlbe, eaddr);
612         /* XXX what does "writeable" and "usermode" even mean? */
613         tr->valid = 1;
614
615         return 0;
616 }
617
618 static int kvmppc_booke_init(void)
619 {
620         unsigned long ivor[16];
621         unsigned long max_ivor = 0;
622         int i;
623
624         /* We install our own exception handlers by hijacking IVPR. IVPR must
625          * be 16-bit aligned, so we need a 64KB allocation. */
626         kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
627                                                  VCPU_SIZE_ORDER);
628         if (!kvmppc_booke_handlers)
629                 return -ENOMEM;
630
631         /* XXX make sure our handlers are smaller than Linux's */
632
633         /* Copy our interrupt handlers to match host IVORs. That way we don't
634          * have to swap the IVORs on every guest/host transition. */
635         ivor[0] = mfspr(SPRN_IVOR0);
636         ivor[1] = mfspr(SPRN_IVOR1);
637         ivor[2] = mfspr(SPRN_IVOR2);
638         ivor[3] = mfspr(SPRN_IVOR3);
639         ivor[4] = mfspr(SPRN_IVOR4);
640         ivor[5] = mfspr(SPRN_IVOR5);
641         ivor[6] = mfspr(SPRN_IVOR6);
642         ivor[7] = mfspr(SPRN_IVOR7);
643         ivor[8] = mfspr(SPRN_IVOR8);
644         ivor[9] = mfspr(SPRN_IVOR9);
645         ivor[10] = mfspr(SPRN_IVOR10);
646         ivor[11] = mfspr(SPRN_IVOR11);
647         ivor[12] = mfspr(SPRN_IVOR12);
648         ivor[13] = mfspr(SPRN_IVOR13);
649         ivor[14] = mfspr(SPRN_IVOR14);
650         ivor[15] = mfspr(SPRN_IVOR15);
651
652         for (i = 0; i < 16; i++) {
653                 if (ivor[i] > max_ivor)
654                         max_ivor = ivor[i];
655
656                 memcpy((void *)kvmppc_booke_handlers + ivor[i],
657                        kvmppc_handlers_start + i * kvmppc_handler_len,
658                        kvmppc_handler_len);
659         }
660         flush_icache_range(kvmppc_booke_handlers,
661                            kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
662
663         return kvm_init(NULL, sizeof(struct kvm_vcpu), THIS_MODULE);
664 }
665
666 static void __exit kvmppc_booke_exit(void)
667 {
668         free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
669         kvm_exit();
670 }
671
672 module_init(kvmppc_booke_init)
673 module_exit(kvmppc_booke_exit)