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