xref: /openbmc/linux/arch/powerpc/kvm/booke.c (revision b8fd68ac8db1f926fdb2c7f196598a279461de53)
1d9fbd03dSHollis Blanchard /*
2d9fbd03dSHollis Blanchard  * This program is free software; you can redistribute it and/or modify
3d9fbd03dSHollis Blanchard  * it under the terms of the GNU General Public License, version 2, as
4d9fbd03dSHollis Blanchard  * published by the Free Software Foundation.
5d9fbd03dSHollis Blanchard  *
6d9fbd03dSHollis Blanchard  * This program is distributed in the hope that it will be useful,
7d9fbd03dSHollis Blanchard  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8d9fbd03dSHollis Blanchard  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9d9fbd03dSHollis Blanchard  * GNU General Public License for more details.
10d9fbd03dSHollis Blanchard  *
11d9fbd03dSHollis Blanchard  * You should have received a copy of the GNU General Public License
12d9fbd03dSHollis Blanchard  * along with this program; if not, write to the Free Software
13d9fbd03dSHollis Blanchard  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
14d9fbd03dSHollis Blanchard  *
15d9fbd03dSHollis Blanchard  * Copyright IBM Corp. 2007
16d9fbd03dSHollis Blanchard  *
17d9fbd03dSHollis Blanchard  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18d9fbd03dSHollis Blanchard  *          Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
19d9fbd03dSHollis Blanchard  */
20d9fbd03dSHollis Blanchard 
21d9fbd03dSHollis Blanchard #include <linux/errno.h>
22d9fbd03dSHollis Blanchard #include <linux/err.h>
23d9fbd03dSHollis Blanchard #include <linux/kvm_host.h>
24d9fbd03dSHollis Blanchard #include <linux/module.h>
25d9fbd03dSHollis Blanchard #include <linux/vmalloc.h>
26d9fbd03dSHollis Blanchard #include <linux/fs.h>
27d9fbd03dSHollis Blanchard #include <asm/cputable.h>
28d9fbd03dSHollis Blanchard #include <asm/uaccess.h>
29d9fbd03dSHollis Blanchard #include <asm/kvm_ppc.h>
30d9fbd03dSHollis Blanchard #include <asm/cacheflush.h>
31d9fbd03dSHollis Blanchard 
3275f74f0dSHollis Blanchard #include "booke.h"
33d9fbd03dSHollis Blanchard #include "44x_tlb.h"
34d9fbd03dSHollis Blanchard 
35d9fbd03dSHollis Blanchard unsigned long kvmppc_booke_handlers;
36d9fbd03dSHollis Blanchard 
37d9fbd03dSHollis Blanchard #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
38d9fbd03dSHollis Blanchard #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
39d9fbd03dSHollis Blanchard 
40d9fbd03dSHollis Blanchard struct kvm_stats_debugfs_item debugfs_entries[] = {
41d9fbd03dSHollis Blanchard 	{ "exits",      VCPU_STAT(sum_exits) },
42d9fbd03dSHollis Blanchard 	{ "mmio",       VCPU_STAT(mmio_exits) },
43d9fbd03dSHollis Blanchard 	{ "dcr",        VCPU_STAT(dcr_exits) },
44d9fbd03dSHollis Blanchard 	{ "sig",        VCPU_STAT(signal_exits) },
45d9fbd03dSHollis Blanchard 	{ "light",      VCPU_STAT(light_exits) },
46d9fbd03dSHollis Blanchard 	{ "itlb_r",     VCPU_STAT(itlb_real_miss_exits) },
47d9fbd03dSHollis Blanchard 	{ "itlb_v",     VCPU_STAT(itlb_virt_miss_exits) },
48d9fbd03dSHollis Blanchard 	{ "dtlb_r",     VCPU_STAT(dtlb_real_miss_exits) },
49d9fbd03dSHollis Blanchard 	{ "dtlb_v",     VCPU_STAT(dtlb_virt_miss_exits) },
50d9fbd03dSHollis Blanchard 	{ "sysc",       VCPU_STAT(syscall_exits) },
51d9fbd03dSHollis Blanchard 	{ "isi",        VCPU_STAT(isi_exits) },
52d9fbd03dSHollis Blanchard 	{ "dsi",        VCPU_STAT(dsi_exits) },
53d9fbd03dSHollis Blanchard 	{ "inst_emu",   VCPU_STAT(emulated_inst_exits) },
54d9fbd03dSHollis Blanchard 	{ "dec",        VCPU_STAT(dec_exits) },
55d9fbd03dSHollis Blanchard 	{ "ext_intr",   VCPU_STAT(ext_intr_exits) },
56d9fbd03dSHollis Blanchard 	{ "halt_wakeup", VCPU_STAT(halt_wakeup) },
57d9fbd03dSHollis Blanchard 	{ NULL }
58d9fbd03dSHollis Blanchard };
59d9fbd03dSHollis Blanchard 
60d9fbd03dSHollis Blanchard static const u32 interrupt_msr_mask[16] = {
61d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_CRITICAL]      = MSR_ME,
62d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_MACHINE_CHECK] = 0,
63d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_DATA_STORAGE]  = MSR_CE|MSR_ME|MSR_DE,
64d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_INST_STORAGE]  = MSR_CE|MSR_ME|MSR_DE,
65d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_EXTERNAL]      = MSR_CE|MSR_ME|MSR_DE,
66d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_ALIGNMENT]     = MSR_CE|MSR_ME|MSR_DE,
67d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_PROGRAM]       = MSR_CE|MSR_ME|MSR_DE,
68d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_FP_UNAVAIL]    = MSR_CE|MSR_ME|MSR_DE,
69d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_SYSCALL]       = MSR_CE|MSR_ME|MSR_DE,
70d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_AP_UNAVAIL]    = MSR_CE|MSR_ME|MSR_DE,
71d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_DECREMENTER]   = MSR_CE|MSR_ME|MSR_DE,
72d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_FIT]           = MSR_CE|MSR_ME|MSR_DE,
73d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_WATCHDOG]      = MSR_ME,
74d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_DTLB_MISS]     = MSR_CE|MSR_ME|MSR_DE,
75d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_ITLB_MISS]     = MSR_CE|MSR_ME|MSR_DE,
76d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_DEBUG]         = MSR_ME,
77d9fbd03dSHollis Blanchard };
78d9fbd03dSHollis Blanchard 
79d9fbd03dSHollis Blanchard const unsigned char exception_priority[] = {
80d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_DATA_STORAGE] = 0,
81d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_INST_STORAGE] = 1,
82d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_ALIGNMENT] = 2,
83d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_PROGRAM] = 3,
84d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_FP_UNAVAIL] = 4,
85d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_SYSCALL] = 5,
86d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_AP_UNAVAIL] = 6,
87d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_DTLB_MISS] = 7,
88d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_ITLB_MISS] = 8,
89d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_MACHINE_CHECK] = 9,
90d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_DEBUG] = 10,
91d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_CRITICAL] = 11,
92d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_WATCHDOG] = 12,
93d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_EXTERNAL] = 13,
94d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_FIT] = 14,
95d9fbd03dSHollis Blanchard 	[BOOKE_INTERRUPT_DECREMENTER] = 15,
96d9fbd03dSHollis Blanchard };
97d9fbd03dSHollis Blanchard 
98d9fbd03dSHollis Blanchard const unsigned char priority_exception[] = {
99d9fbd03dSHollis Blanchard 	BOOKE_INTERRUPT_DATA_STORAGE,
100d9fbd03dSHollis Blanchard 	BOOKE_INTERRUPT_INST_STORAGE,
101d9fbd03dSHollis Blanchard 	BOOKE_INTERRUPT_ALIGNMENT,
102d9fbd03dSHollis Blanchard 	BOOKE_INTERRUPT_PROGRAM,
103d9fbd03dSHollis Blanchard 	BOOKE_INTERRUPT_FP_UNAVAIL,
104d9fbd03dSHollis Blanchard 	BOOKE_INTERRUPT_SYSCALL,
105d9fbd03dSHollis Blanchard 	BOOKE_INTERRUPT_AP_UNAVAIL,
106d9fbd03dSHollis Blanchard 	BOOKE_INTERRUPT_DTLB_MISS,
107d9fbd03dSHollis Blanchard 	BOOKE_INTERRUPT_ITLB_MISS,
108d9fbd03dSHollis Blanchard 	BOOKE_INTERRUPT_MACHINE_CHECK,
109d9fbd03dSHollis Blanchard 	BOOKE_INTERRUPT_DEBUG,
110d9fbd03dSHollis Blanchard 	BOOKE_INTERRUPT_CRITICAL,
111d9fbd03dSHollis Blanchard 	BOOKE_INTERRUPT_WATCHDOG,
112d9fbd03dSHollis Blanchard 	BOOKE_INTERRUPT_EXTERNAL,
113d9fbd03dSHollis Blanchard 	BOOKE_INTERRUPT_FIT,
114d9fbd03dSHollis Blanchard 	BOOKE_INTERRUPT_DECREMENTER,
115d9fbd03dSHollis Blanchard };
116d9fbd03dSHollis Blanchard 
117d9fbd03dSHollis Blanchard 
118d9fbd03dSHollis Blanchard /* TODO: use vcpu_printf() */
119d9fbd03dSHollis Blanchard void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu)
120d9fbd03dSHollis Blanchard {
121d9fbd03dSHollis Blanchard 	int i;
122d9fbd03dSHollis Blanchard 
1235cf8ca22SHollis Blanchard 	printk("pc:   %08lx msr:  %08lx\n", vcpu->arch.pc, vcpu->arch.msr);
1245cf8ca22SHollis Blanchard 	printk("lr:   %08lx ctr:  %08lx\n", vcpu->arch.lr, vcpu->arch.ctr);
1255cf8ca22SHollis Blanchard 	printk("srr0: %08lx srr1: %08lx\n", vcpu->arch.srr0, vcpu->arch.srr1);
126d9fbd03dSHollis Blanchard 
127d9fbd03dSHollis Blanchard 	printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions);
128d9fbd03dSHollis Blanchard 
129d9fbd03dSHollis Blanchard 	for (i = 0; i < 32; i += 4) {
1305cf8ca22SHollis Blanchard 		printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i,
131d9fbd03dSHollis Blanchard 		       vcpu->arch.gpr[i],
132d9fbd03dSHollis Blanchard 		       vcpu->arch.gpr[i+1],
133d9fbd03dSHollis Blanchard 		       vcpu->arch.gpr[i+2],
134d9fbd03dSHollis Blanchard 		       vcpu->arch.gpr[i+3]);
135d9fbd03dSHollis Blanchard 	}
136d9fbd03dSHollis Blanchard }
137d9fbd03dSHollis Blanchard 
1389dd921cfSHollis Blanchard static void kvmppc_booke_queue_exception(struct kvm_vcpu *vcpu, int exception)
1399dd921cfSHollis Blanchard {
1409dd921cfSHollis Blanchard 	unsigned int priority = exception_priority[exception];
1419dd921cfSHollis Blanchard 	set_bit(priority, &vcpu->arch.pending_exceptions);
1429dd921cfSHollis Blanchard }
1439dd921cfSHollis Blanchard 
1449dd921cfSHollis Blanchard static void kvmppc_booke_clear_exception(struct kvm_vcpu *vcpu, int exception)
1459dd921cfSHollis Blanchard {
1469dd921cfSHollis Blanchard 	unsigned int priority = exception_priority[exception];
1479dd921cfSHollis Blanchard 	clear_bit(priority, &vcpu->arch.pending_exceptions);
1489dd921cfSHollis Blanchard }
1499dd921cfSHollis Blanchard 
1509dd921cfSHollis Blanchard void kvmppc_core_queue_program(struct kvm_vcpu *vcpu)
1519dd921cfSHollis Blanchard {
1529dd921cfSHollis Blanchard 	kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_PROGRAM);
1539dd921cfSHollis Blanchard }
1549dd921cfSHollis Blanchard 
1559dd921cfSHollis Blanchard void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu)
1569dd921cfSHollis Blanchard {
1579dd921cfSHollis Blanchard 	kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_DECREMENTER);
1589dd921cfSHollis Blanchard }
1599dd921cfSHollis Blanchard 
1609dd921cfSHollis Blanchard int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu)
1619dd921cfSHollis Blanchard {
1629dd921cfSHollis Blanchard 	unsigned int priority = exception_priority[BOOKE_INTERRUPT_DECREMENTER];
1639dd921cfSHollis Blanchard 	return test_bit(priority, &vcpu->arch.pending_exceptions);
1649dd921cfSHollis Blanchard }
1659dd921cfSHollis Blanchard 
1669dd921cfSHollis Blanchard void kvmppc_core_queue_external(struct kvm_vcpu *vcpu,
1679dd921cfSHollis Blanchard                                 struct kvm_interrupt *irq)
1689dd921cfSHollis Blanchard {
1699dd921cfSHollis Blanchard 	kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_EXTERNAL);
1709dd921cfSHollis Blanchard }
1719dd921cfSHollis Blanchard 
172d9fbd03dSHollis Blanchard /* Check if we are ready to deliver the interrupt */
173d9fbd03dSHollis Blanchard static int kvmppc_can_deliver_interrupt(struct kvm_vcpu *vcpu, int interrupt)
174d9fbd03dSHollis Blanchard {
175d9fbd03dSHollis Blanchard 	int r;
176d9fbd03dSHollis Blanchard 
177d9fbd03dSHollis Blanchard 	switch (interrupt) {
178d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_CRITICAL:
179d9fbd03dSHollis Blanchard 		r = vcpu->arch.msr & MSR_CE;
180d9fbd03dSHollis Blanchard 		break;
181d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_MACHINE_CHECK:
182d9fbd03dSHollis Blanchard 		r = vcpu->arch.msr & MSR_ME;
183d9fbd03dSHollis Blanchard 		break;
184d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_EXTERNAL:
185d9fbd03dSHollis Blanchard 		r = vcpu->arch.msr & MSR_EE;
186d9fbd03dSHollis Blanchard 		break;
187d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DECREMENTER:
188d9fbd03dSHollis Blanchard 		r = vcpu->arch.msr & MSR_EE;
189d9fbd03dSHollis Blanchard 		break;
190d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_FIT:
191d9fbd03dSHollis Blanchard 		r = vcpu->arch.msr & MSR_EE;
192d9fbd03dSHollis Blanchard 		break;
193d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_WATCHDOG:
194d9fbd03dSHollis Blanchard 		r = vcpu->arch.msr & MSR_CE;
195d9fbd03dSHollis Blanchard 		break;
196d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DEBUG:
197d9fbd03dSHollis Blanchard 		r = vcpu->arch.msr & MSR_DE;
198d9fbd03dSHollis Blanchard 		break;
199d9fbd03dSHollis Blanchard 	default:
200d9fbd03dSHollis Blanchard 		r = 1;
201d9fbd03dSHollis Blanchard 	}
202d9fbd03dSHollis Blanchard 
203d9fbd03dSHollis Blanchard 	return r;
204d9fbd03dSHollis Blanchard }
205d9fbd03dSHollis Blanchard 
2069dd921cfSHollis Blanchard static void kvmppc_booke_deliver_interrupt(struct kvm_vcpu *vcpu, int interrupt)
207d9fbd03dSHollis Blanchard {
208d9fbd03dSHollis Blanchard 	switch (interrupt) {
209d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DECREMENTER:
210d9fbd03dSHollis Blanchard 		vcpu->arch.tsr |= TSR_DIS;
211d9fbd03dSHollis Blanchard 		break;
212d9fbd03dSHollis Blanchard 	}
213d9fbd03dSHollis Blanchard 
214d9fbd03dSHollis Blanchard 	vcpu->arch.srr0 = vcpu->arch.pc;
215d9fbd03dSHollis Blanchard 	vcpu->arch.srr1 = vcpu->arch.msr;
216d9fbd03dSHollis Blanchard 	vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[interrupt];
217d9fbd03dSHollis Blanchard 	kvmppc_set_msr(vcpu, vcpu->arch.msr & interrupt_msr_mask[interrupt]);
218d9fbd03dSHollis Blanchard }
219d9fbd03dSHollis Blanchard 
220d9fbd03dSHollis Blanchard /* Check pending exceptions and deliver one, if possible. */
2219dd921cfSHollis Blanchard void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu)
222d9fbd03dSHollis Blanchard {
223d9fbd03dSHollis Blanchard 	unsigned long *pending = &vcpu->arch.pending_exceptions;
224d9fbd03dSHollis Blanchard 	unsigned int exception;
225d9fbd03dSHollis Blanchard 	unsigned int priority;
226d9fbd03dSHollis Blanchard 
227d9fbd03dSHollis Blanchard 	priority = find_first_bit(pending, BITS_PER_BYTE * sizeof(*pending));
228d9fbd03dSHollis Blanchard 	while (priority <= BOOKE_MAX_INTERRUPT) {
229d9fbd03dSHollis Blanchard 		exception = priority_exception[priority];
230d9fbd03dSHollis Blanchard 		if (kvmppc_can_deliver_interrupt(vcpu, exception)) {
2319dd921cfSHollis Blanchard 			kvmppc_booke_clear_exception(vcpu, exception);
2329dd921cfSHollis Blanchard 			kvmppc_booke_deliver_interrupt(vcpu, exception);
233d9fbd03dSHollis Blanchard 			break;
234d9fbd03dSHollis Blanchard 		}
235d9fbd03dSHollis Blanchard 
236d9fbd03dSHollis Blanchard 		priority = find_next_bit(pending,
237d9fbd03dSHollis Blanchard 		                         BITS_PER_BYTE * sizeof(*pending),
238d9fbd03dSHollis Blanchard 		                         priority + 1);
239d9fbd03dSHollis Blanchard 	}
240d9fbd03dSHollis Blanchard }
241d9fbd03dSHollis Blanchard 
242d9fbd03dSHollis Blanchard /**
243d9fbd03dSHollis Blanchard  * kvmppc_handle_exit
244d9fbd03dSHollis Blanchard  *
245d9fbd03dSHollis Blanchard  * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
246d9fbd03dSHollis Blanchard  */
247d9fbd03dSHollis Blanchard int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
248d9fbd03dSHollis Blanchard                        unsigned int exit_nr)
249d9fbd03dSHollis Blanchard {
250d9fbd03dSHollis Blanchard 	enum emulation_result er;
251d9fbd03dSHollis Blanchard 	int r = RESUME_HOST;
252d9fbd03dSHollis Blanchard 
253d9fbd03dSHollis Blanchard 	local_irq_enable();
254d9fbd03dSHollis Blanchard 
255d9fbd03dSHollis Blanchard 	run->exit_reason = KVM_EXIT_UNKNOWN;
256d9fbd03dSHollis Blanchard 	run->ready_for_interrupt_injection = 1;
257d9fbd03dSHollis Blanchard 
258d9fbd03dSHollis Blanchard 	switch (exit_nr) {
259d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_MACHINE_CHECK:
260d9fbd03dSHollis Blanchard 		printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR));
261d9fbd03dSHollis Blanchard 		kvmppc_dump_vcpu(vcpu);
262d9fbd03dSHollis Blanchard 		r = RESUME_HOST;
263d9fbd03dSHollis Blanchard 		break;
264d9fbd03dSHollis Blanchard 
265d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_EXTERNAL:
266d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DECREMENTER:
267d9fbd03dSHollis Blanchard 		/* Since we switched IVPR back to the host's value, the host
268d9fbd03dSHollis Blanchard 		 * handled this interrupt the moment we enabled interrupts.
269d9fbd03dSHollis Blanchard 		 * Now we just offer it a chance to reschedule the guest. */
270d9fbd03dSHollis Blanchard 
271d9fbd03dSHollis Blanchard 		/* XXX At this point the TLB still holds our shadow TLB, so if
272d9fbd03dSHollis Blanchard 		 * we do reschedule the host will fault over it. Perhaps we
273d9fbd03dSHollis Blanchard 		 * should politely restore the host's entries to minimize
274d9fbd03dSHollis Blanchard 		 * misses before ceding control. */
275d9fbd03dSHollis Blanchard 		if (need_resched())
276d9fbd03dSHollis Blanchard 			cond_resched();
277d9fbd03dSHollis Blanchard 		if (exit_nr == BOOKE_INTERRUPT_DECREMENTER)
278d9fbd03dSHollis Blanchard 			vcpu->stat.dec_exits++;
279d9fbd03dSHollis Blanchard 		else
280d9fbd03dSHollis Blanchard 			vcpu->stat.ext_intr_exits++;
281d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
282d9fbd03dSHollis Blanchard 		break;
283d9fbd03dSHollis Blanchard 
284d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_PROGRAM:
285d9fbd03dSHollis Blanchard 		if (vcpu->arch.msr & MSR_PR) {
286d9fbd03dSHollis Blanchard 			/* Program traps generated by user-level software must be handled
287d9fbd03dSHollis Blanchard 			 * by the guest kernel. */
288d9fbd03dSHollis Blanchard 			vcpu->arch.esr = vcpu->arch.fault_esr;
2899dd921cfSHollis Blanchard 			kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_PROGRAM);
290d9fbd03dSHollis Blanchard 			r = RESUME_GUEST;
291d9fbd03dSHollis Blanchard 			break;
292d9fbd03dSHollis Blanchard 		}
293d9fbd03dSHollis Blanchard 
294d9fbd03dSHollis Blanchard 		er = kvmppc_emulate_instruction(run, vcpu);
295d9fbd03dSHollis Blanchard 		switch (er) {
296d9fbd03dSHollis Blanchard 		case EMULATE_DONE:
297d9fbd03dSHollis Blanchard 			/* Future optimization: only reload non-volatiles if
298d9fbd03dSHollis Blanchard 			 * they were actually modified by emulation. */
299d9fbd03dSHollis Blanchard 			vcpu->stat.emulated_inst_exits++;
300d9fbd03dSHollis Blanchard 			r = RESUME_GUEST_NV;
301d9fbd03dSHollis Blanchard 			break;
302d9fbd03dSHollis Blanchard 		case EMULATE_DO_DCR:
303d9fbd03dSHollis Blanchard 			run->exit_reason = KVM_EXIT_DCR;
304d9fbd03dSHollis Blanchard 			r = RESUME_HOST;
305d9fbd03dSHollis Blanchard 			break;
306d9fbd03dSHollis Blanchard 		case EMULATE_FAIL:
307d9fbd03dSHollis Blanchard 			/* XXX Deliver Program interrupt to guest. */
3085cf8ca22SHollis Blanchard 			printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n",
309d9fbd03dSHollis Blanchard 			       __func__, vcpu->arch.pc, vcpu->arch.last_inst);
310d9fbd03dSHollis Blanchard 			/* For debugging, encode the failing instruction and
311d9fbd03dSHollis Blanchard 			 * report it to userspace. */
312d9fbd03dSHollis Blanchard 			run->hw.hardware_exit_reason = ~0ULL << 32;
313d9fbd03dSHollis Blanchard 			run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
314d9fbd03dSHollis Blanchard 			r = RESUME_HOST;
315d9fbd03dSHollis Blanchard 			break;
316d9fbd03dSHollis Blanchard 		default:
317d9fbd03dSHollis Blanchard 			BUG();
318d9fbd03dSHollis Blanchard 		}
319d9fbd03dSHollis Blanchard 		break;
320d9fbd03dSHollis Blanchard 
321d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_FP_UNAVAIL:
3229dd921cfSHollis Blanchard 		kvmppc_booke_queue_exception(vcpu, exit_nr);
323d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
324d9fbd03dSHollis Blanchard 		break;
325d9fbd03dSHollis Blanchard 
326d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DATA_STORAGE:
327d9fbd03dSHollis Blanchard 		vcpu->arch.dear = vcpu->arch.fault_dear;
328d9fbd03dSHollis Blanchard 		vcpu->arch.esr = vcpu->arch.fault_esr;
3299dd921cfSHollis Blanchard 		kvmppc_booke_queue_exception(vcpu, exit_nr);
330d9fbd03dSHollis Blanchard 		vcpu->stat.dsi_exits++;
331d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
332d9fbd03dSHollis Blanchard 		break;
333d9fbd03dSHollis Blanchard 
334d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_INST_STORAGE:
335d9fbd03dSHollis Blanchard 		vcpu->arch.esr = vcpu->arch.fault_esr;
3369dd921cfSHollis Blanchard 		kvmppc_booke_queue_exception(vcpu, exit_nr);
337d9fbd03dSHollis Blanchard 		vcpu->stat.isi_exits++;
338d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
339d9fbd03dSHollis Blanchard 		break;
340d9fbd03dSHollis Blanchard 
341d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_SYSCALL:
3429dd921cfSHollis Blanchard 		kvmppc_booke_queue_exception(vcpu, exit_nr);
343d9fbd03dSHollis Blanchard 		vcpu->stat.syscall_exits++;
344d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
345d9fbd03dSHollis Blanchard 		break;
346d9fbd03dSHollis Blanchard 
347d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DTLB_MISS: {
348d9fbd03dSHollis Blanchard 		struct kvmppc_44x_tlbe *gtlbe;
349d9fbd03dSHollis Blanchard 		unsigned long eaddr = vcpu->arch.fault_dear;
350d9fbd03dSHollis Blanchard 		gfn_t gfn;
351d9fbd03dSHollis Blanchard 
352d9fbd03dSHollis Blanchard 		/* Check the guest TLB. */
353d9fbd03dSHollis Blanchard 		gtlbe = kvmppc_44x_dtlb_search(vcpu, eaddr);
354d9fbd03dSHollis Blanchard 		if (!gtlbe) {
355d9fbd03dSHollis Blanchard 			/* The guest didn't have a mapping for it. */
3569dd921cfSHollis Blanchard 			kvmppc_booke_queue_exception(vcpu, exit_nr);
357d9fbd03dSHollis Blanchard 			vcpu->arch.dear = vcpu->arch.fault_dear;
358d9fbd03dSHollis Blanchard 			vcpu->arch.esr = vcpu->arch.fault_esr;
359d9fbd03dSHollis Blanchard 			vcpu->stat.dtlb_real_miss_exits++;
360d9fbd03dSHollis Blanchard 			r = RESUME_GUEST;
361d9fbd03dSHollis Blanchard 			break;
362d9fbd03dSHollis Blanchard 		}
363d9fbd03dSHollis Blanchard 
364d9fbd03dSHollis Blanchard 		vcpu->arch.paddr_accessed = tlb_xlate(gtlbe, eaddr);
365d9fbd03dSHollis Blanchard 		gfn = vcpu->arch.paddr_accessed >> PAGE_SHIFT;
366d9fbd03dSHollis Blanchard 
367d9fbd03dSHollis Blanchard 		if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
368d9fbd03dSHollis Blanchard 			/* The guest TLB had a mapping, but the shadow TLB
369d9fbd03dSHollis Blanchard 			 * didn't, and it is RAM. This could be because:
370d9fbd03dSHollis Blanchard 			 * a) the entry is mapping the host kernel, or
371d9fbd03dSHollis Blanchard 			 * b) the guest used a large mapping which we're faking
372d9fbd03dSHollis Blanchard 			 * Either way, we need to satisfy the fault without
373d9fbd03dSHollis Blanchard 			 * invoking the guest. */
374d9fbd03dSHollis Blanchard 			kvmppc_mmu_map(vcpu, eaddr, gfn, gtlbe->tid,
375d9fbd03dSHollis Blanchard 			               gtlbe->word2);
376d9fbd03dSHollis Blanchard 			vcpu->stat.dtlb_virt_miss_exits++;
377d9fbd03dSHollis Blanchard 			r = RESUME_GUEST;
378d9fbd03dSHollis Blanchard 		} else {
379d9fbd03dSHollis Blanchard 			/* Guest has mapped and accessed a page which is not
380d9fbd03dSHollis Blanchard 			 * actually RAM. */
381d9fbd03dSHollis Blanchard 			r = kvmppc_emulate_mmio(run, vcpu);
382d9fbd03dSHollis Blanchard 		}
383d9fbd03dSHollis Blanchard 
384d9fbd03dSHollis Blanchard 		break;
385d9fbd03dSHollis Blanchard 	}
386d9fbd03dSHollis Blanchard 
387d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_ITLB_MISS: {
388d9fbd03dSHollis Blanchard 		struct kvmppc_44x_tlbe *gtlbe;
389d9fbd03dSHollis Blanchard 		unsigned long eaddr = vcpu->arch.pc;
390d9fbd03dSHollis Blanchard 		gfn_t gfn;
391d9fbd03dSHollis Blanchard 
392d9fbd03dSHollis Blanchard 		r = RESUME_GUEST;
393d9fbd03dSHollis Blanchard 
394d9fbd03dSHollis Blanchard 		/* Check the guest TLB. */
395d9fbd03dSHollis Blanchard 		gtlbe = kvmppc_44x_itlb_search(vcpu, eaddr);
396d9fbd03dSHollis Blanchard 		if (!gtlbe) {
397d9fbd03dSHollis Blanchard 			/* The guest didn't have a mapping for it. */
3989dd921cfSHollis Blanchard 			kvmppc_booke_queue_exception(vcpu, exit_nr);
399d9fbd03dSHollis Blanchard 			vcpu->stat.itlb_real_miss_exits++;
400d9fbd03dSHollis Blanchard 			break;
401d9fbd03dSHollis Blanchard 		}
402d9fbd03dSHollis Blanchard 
403d9fbd03dSHollis Blanchard 		vcpu->stat.itlb_virt_miss_exits++;
404d9fbd03dSHollis Blanchard 
405d9fbd03dSHollis Blanchard 		gfn = tlb_xlate(gtlbe, eaddr) >> PAGE_SHIFT;
406d9fbd03dSHollis Blanchard 
407d9fbd03dSHollis Blanchard 		if (kvm_is_visible_gfn(vcpu->kvm, gfn)) {
408d9fbd03dSHollis Blanchard 			/* The guest TLB had a mapping, but the shadow TLB
409d9fbd03dSHollis Blanchard 			 * didn't. This could be because:
410d9fbd03dSHollis Blanchard 			 * a) the entry is mapping the host kernel, or
411d9fbd03dSHollis Blanchard 			 * b) the guest used a large mapping which we're faking
412d9fbd03dSHollis Blanchard 			 * Either way, we need to satisfy the fault without
413d9fbd03dSHollis Blanchard 			 * invoking the guest. */
414d9fbd03dSHollis Blanchard 			kvmppc_mmu_map(vcpu, eaddr, gfn, gtlbe->tid,
415d9fbd03dSHollis Blanchard 			               gtlbe->word2);
416d9fbd03dSHollis Blanchard 		} else {
417d9fbd03dSHollis Blanchard 			/* Guest mapped and leaped at non-RAM! */
4189dd921cfSHollis Blanchard 			kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_MACHINE_CHECK);
419d9fbd03dSHollis Blanchard 		}
420d9fbd03dSHollis Blanchard 
421d9fbd03dSHollis Blanchard 		break;
422d9fbd03dSHollis Blanchard 	}
423d9fbd03dSHollis Blanchard 
424d9fbd03dSHollis Blanchard 	case BOOKE_INTERRUPT_DEBUG: {
425d9fbd03dSHollis Blanchard 		u32 dbsr;
426d9fbd03dSHollis Blanchard 
427d9fbd03dSHollis Blanchard 		vcpu->arch.pc = mfspr(SPRN_CSRR0);
428d9fbd03dSHollis Blanchard 
429d9fbd03dSHollis Blanchard 		/* clear IAC events in DBSR register */
430d9fbd03dSHollis Blanchard 		dbsr = mfspr(SPRN_DBSR);
431d9fbd03dSHollis Blanchard 		dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4;
432d9fbd03dSHollis Blanchard 		mtspr(SPRN_DBSR, dbsr);
433d9fbd03dSHollis Blanchard 
434d9fbd03dSHollis Blanchard 		run->exit_reason = KVM_EXIT_DEBUG;
435d9fbd03dSHollis Blanchard 		r = RESUME_HOST;
436d9fbd03dSHollis Blanchard 		break;
437d9fbd03dSHollis Blanchard 	}
438d9fbd03dSHollis Blanchard 
439d9fbd03dSHollis Blanchard 	default:
440d9fbd03dSHollis Blanchard 		printk(KERN_EMERG "exit_nr %d\n", exit_nr);
441d9fbd03dSHollis Blanchard 		BUG();
442d9fbd03dSHollis Blanchard 	}
443d9fbd03dSHollis Blanchard 
444d9fbd03dSHollis Blanchard 	local_irq_disable();
445d9fbd03dSHollis Blanchard 
4469dd921cfSHollis Blanchard 	kvmppc_core_deliver_interrupts(vcpu);
447d9fbd03dSHollis Blanchard 
448d9fbd03dSHollis Blanchard 	/* Do some exit accounting. */
449d9fbd03dSHollis Blanchard 	vcpu->stat.sum_exits++;
450d9fbd03dSHollis Blanchard 	if (!(r & RESUME_HOST)) {
451d9fbd03dSHollis Blanchard 		/* To avoid clobbering exit_reason, only check for signals if
452d9fbd03dSHollis Blanchard 		 * we aren't already exiting to userspace for some other
453d9fbd03dSHollis Blanchard 		 * reason. */
454d9fbd03dSHollis Blanchard 		if (signal_pending(current)) {
455d9fbd03dSHollis Blanchard 			run->exit_reason = KVM_EXIT_INTR;
456d9fbd03dSHollis Blanchard 			r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV);
457d9fbd03dSHollis Blanchard 
458d9fbd03dSHollis Blanchard 			vcpu->stat.signal_exits++;
459d9fbd03dSHollis Blanchard 		} else {
460d9fbd03dSHollis Blanchard 			vcpu->stat.light_exits++;
461d9fbd03dSHollis Blanchard 		}
462d9fbd03dSHollis Blanchard 	} else {
463d9fbd03dSHollis Blanchard 		switch (run->exit_reason) {
464d9fbd03dSHollis Blanchard 		case KVM_EXIT_MMIO:
465d9fbd03dSHollis Blanchard 			vcpu->stat.mmio_exits++;
466d9fbd03dSHollis Blanchard 			break;
467d9fbd03dSHollis Blanchard 		case KVM_EXIT_DCR:
468d9fbd03dSHollis Blanchard 			vcpu->stat.dcr_exits++;
469d9fbd03dSHollis Blanchard 			break;
470d9fbd03dSHollis Blanchard 		case KVM_EXIT_INTR:
471d9fbd03dSHollis Blanchard 			vcpu->stat.signal_exits++;
472d9fbd03dSHollis Blanchard 			break;
473d9fbd03dSHollis Blanchard 		}
474d9fbd03dSHollis Blanchard 	}
475d9fbd03dSHollis Blanchard 
476d9fbd03dSHollis Blanchard 	return r;
477d9fbd03dSHollis Blanchard }
478d9fbd03dSHollis Blanchard 
479d9fbd03dSHollis Blanchard /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
480d9fbd03dSHollis Blanchard int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
481d9fbd03dSHollis Blanchard {
482d9fbd03dSHollis Blanchard 	vcpu->arch.pc = 0;
483d9fbd03dSHollis Blanchard 	vcpu->arch.msr = 0;
484d9fbd03dSHollis Blanchard 	vcpu->arch.gpr[1] = (16<<20) - 8; /* -8 for the callee-save LR slot */
485d9fbd03dSHollis Blanchard 
486d9fbd03dSHollis Blanchard 	vcpu->arch.shadow_pid = 1;
487d9fbd03dSHollis Blanchard 
488d9fbd03dSHollis Blanchard 	/* Eye-catching number so we know if the guest takes an interrupt
489d9fbd03dSHollis Blanchard 	 * before it's programmed its own IVPR. */
490d9fbd03dSHollis Blanchard 	vcpu->arch.ivpr = 0x55550000;
491d9fbd03dSHollis Blanchard 
4925cbb5106SHollis Blanchard 	return kvmppc_core_vcpu_setup(vcpu);
493d9fbd03dSHollis Blanchard }
494d9fbd03dSHollis Blanchard 
495d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
496d9fbd03dSHollis Blanchard {
497d9fbd03dSHollis Blanchard 	int i;
498d9fbd03dSHollis Blanchard 
499d9fbd03dSHollis Blanchard 	regs->pc = vcpu->arch.pc;
500d9fbd03dSHollis Blanchard 	regs->cr = vcpu->arch.cr;
501d9fbd03dSHollis Blanchard 	regs->ctr = vcpu->arch.ctr;
502d9fbd03dSHollis Blanchard 	regs->lr = vcpu->arch.lr;
503d9fbd03dSHollis Blanchard 	regs->xer = vcpu->arch.xer;
504d9fbd03dSHollis Blanchard 	regs->msr = vcpu->arch.msr;
505d9fbd03dSHollis Blanchard 	regs->srr0 = vcpu->arch.srr0;
506d9fbd03dSHollis Blanchard 	regs->srr1 = vcpu->arch.srr1;
507d9fbd03dSHollis Blanchard 	regs->pid = vcpu->arch.pid;
508d9fbd03dSHollis Blanchard 	regs->sprg0 = vcpu->arch.sprg0;
509d9fbd03dSHollis Blanchard 	regs->sprg1 = vcpu->arch.sprg1;
510d9fbd03dSHollis Blanchard 	regs->sprg2 = vcpu->arch.sprg2;
511d9fbd03dSHollis Blanchard 	regs->sprg3 = vcpu->arch.sprg3;
512d9fbd03dSHollis Blanchard 	regs->sprg5 = vcpu->arch.sprg4;
513d9fbd03dSHollis Blanchard 	regs->sprg6 = vcpu->arch.sprg5;
514d9fbd03dSHollis Blanchard 	regs->sprg7 = vcpu->arch.sprg6;
515d9fbd03dSHollis Blanchard 
516d9fbd03dSHollis Blanchard 	for (i = 0; i < ARRAY_SIZE(regs->gpr); i++)
517d9fbd03dSHollis Blanchard 		regs->gpr[i] = vcpu->arch.gpr[i];
518d9fbd03dSHollis Blanchard 
519d9fbd03dSHollis Blanchard 	return 0;
520d9fbd03dSHollis Blanchard }
521d9fbd03dSHollis Blanchard 
522d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
523d9fbd03dSHollis Blanchard {
524d9fbd03dSHollis Blanchard 	int i;
525d9fbd03dSHollis Blanchard 
526d9fbd03dSHollis Blanchard 	vcpu->arch.pc = regs->pc;
527d9fbd03dSHollis Blanchard 	vcpu->arch.cr = regs->cr;
528d9fbd03dSHollis Blanchard 	vcpu->arch.ctr = regs->ctr;
529d9fbd03dSHollis Blanchard 	vcpu->arch.lr = regs->lr;
530d9fbd03dSHollis Blanchard 	vcpu->arch.xer = regs->xer;
531*b8fd68acSHollis Blanchard 	kvmppc_set_msr(vcpu, regs->msr);
532d9fbd03dSHollis Blanchard 	vcpu->arch.srr0 = regs->srr0;
533d9fbd03dSHollis Blanchard 	vcpu->arch.srr1 = regs->srr1;
534d9fbd03dSHollis Blanchard 	vcpu->arch.sprg0 = regs->sprg0;
535d9fbd03dSHollis Blanchard 	vcpu->arch.sprg1 = regs->sprg1;
536d9fbd03dSHollis Blanchard 	vcpu->arch.sprg2 = regs->sprg2;
537d9fbd03dSHollis Blanchard 	vcpu->arch.sprg3 = regs->sprg3;
538d9fbd03dSHollis Blanchard 	vcpu->arch.sprg5 = regs->sprg4;
539d9fbd03dSHollis Blanchard 	vcpu->arch.sprg6 = regs->sprg5;
540d9fbd03dSHollis Blanchard 	vcpu->arch.sprg7 = regs->sprg6;
541d9fbd03dSHollis Blanchard 
542d9fbd03dSHollis Blanchard 	for (i = 0; i < ARRAY_SIZE(vcpu->arch.gpr); i++)
543d9fbd03dSHollis Blanchard 		vcpu->arch.gpr[i] = regs->gpr[i];
544d9fbd03dSHollis Blanchard 
545d9fbd03dSHollis Blanchard 	return 0;
546d9fbd03dSHollis Blanchard }
547d9fbd03dSHollis Blanchard 
548d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
549d9fbd03dSHollis Blanchard                                   struct kvm_sregs *sregs)
550d9fbd03dSHollis Blanchard {
551d9fbd03dSHollis Blanchard 	return -ENOTSUPP;
552d9fbd03dSHollis Blanchard }
553d9fbd03dSHollis Blanchard 
554d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
555d9fbd03dSHollis Blanchard                                   struct kvm_sregs *sregs)
556d9fbd03dSHollis Blanchard {
557d9fbd03dSHollis Blanchard 	return -ENOTSUPP;
558d9fbd03dSHollis Blanchard }
559d9fbd03dSHollis Blanchard 
560d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
561d9fbd03dSHollis Blanchard {
562d9fbd03dSHollis Blanchard 	return -ENOTSUPP;
563d9fbd03dSHollis Blanchard }
564d9fbd03dSHollis Blanchard 
565d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
566d9fbd03dSHollis Blanchard {
567d9fbd03dSHollis Blanchard 	return -ENOTSUPP;
568d9fbd03dSHollis Blanchard }
569d9fbd03dSHollis Blanchard 
570d9fbd03dSHollis Blanchard int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
571d9fbd03dSHollis Blanchard                                   struct kvm_translation *tr)
572d9fbd03dSHollis Blanchard {
5735cbb5106SHollis Blanchard 	return kvmppc_core_vcpu_translate(vcpu, tr);
574d9fbd03dSHollis Blanchard }
575d9fbd03dSHollis Blanchard 
576db93f574SHollis Blanchard int kvmppc_booke_init(void)
577d9fbd03dSHollis Blanchard {
578d9fbd03dSHollis Blanchard 	unsigned long ivor[16];
579d9fbd03dSHollis Blanchard 	unsigned long max_ivor = 0;
580d9fbd03dSHollis Blanchard 	int i;
581d9fbd03dSHollis Blanchard 
582d9fbd03dSHollis Blanchard 	/* We install our own exception handlers by hijacking IVPR. IVPR must
583d9fbd03dSHollis Blanchard 	 * be 16-bit aligned, so we need a 64KB allocation. */
584d9fbd03dSHollis Blanchard 	kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO,
585d9fbd03dSHollis Blanchard 	                                         VCPU_SIZE_ORDER);
586d9fbd03dSHollis Blanchard 	if (!kvmppc_booke_handlers)
587d9fbd03dSHollis Blanchard 		return -ENOMEM;
588d9fbd03dSHollis Blanchard 
589d9fbd03dSHollis Blanchard 	/* XXX make sure our handlers are smaller than Linux's */
590d9fbd03dSHollis Blanchard 
591d9fbd03dSHollis Blanchard 	/* Copy our interrupt handlers to match host IVORs. That way we don't
592d9fbd03dSHollis Blanchard 	 * have to swap the IVORs on every guest/host transition. */
593d9fbd03dSHollis Blanchard 	ivor[0] = mfspr(SPRN_IVOR0);
594d9fbd03dSHollis Blanchard 	ivor[1] = mfspr(SPRN_IVOR1);
595d9fbd03dSHollis Blanchard 	ivor[2] = mfspr(SPRN_IVOR2);
596d9fbd03dSHollis Blanchard 	ivor[3] = mfspr(SPRN_IVOR3);
597d9fbd03dSHollis Blanchard 	ivor[4] = mfspr(SPRN_IVOR4);
598d9fbd03dSHollis Blanchard 	ivor[5] = mfspr(SPRN_IVOR5);
599d9fbd03dSHollis Blanchard 	ivor[6] = mfspr(SPRN_IVOR6);
600d9fbd03dSHollis Blanchard 	ivor[7] = mfspr(SPRN_IVOR7);
601d9fbd03dSHollis Blanchard 	ivor[8] = mfspr(SPRN_IVOR8);
602d9fbd03dSHollis Blanchard 	ivor[9] = mfspr(SPRN_IVOR9);
603d9fbd03dSHollis Blanchard 	ivor[10] = mfspr(SPRN_IVOR10);
604d9fbd03dSHollis Blanchard 	ivor[11] = mfspr(SPRN_IVOR11);
605d9fbd03dSHollis Blanchard 	ivor[12] = mfspr(SPRN_IVOR12);
606d9fbd03dSHollis Blanchard 	ivor[13] = mfspr(SPRN_IVOR13);
607d9fbd03dSHollis Blanchard 	ivor[14] = mfspr(SPRN_IVOR14);
608d9fbd03dSHollis Blanchard 	ivor[15] = mfspr(SPRN_IVOR15);
609d9fbd03dSHollis Blanchard 
610d9fbd03dSHollis Blanchard 	for (i = 0; i < 16; i++) {
611d9fbd03dSHollis Blanchard 		if (ivor[i] > max_ivor)
612d9fbd03dSHollis Blanchard 			max_ivor = ivor[i];
613d9fbd03dSHollis Blanchard 
614d9fbd03dSHollis Blanchard 		memcpy((void *)kvmppc_booke_handlers + ivor[i],
615d9fbd03dSHollis Blanchard 		       kvmppc_handlers_start + i * kvmppc_handler_len,
616d9fbd03dSHollis Blanchard 		       kvmppc_handler_len);
617d9fbd03dSHollis Blanchard 	}
618d9fbd03dSHollis Blanchard 	flush_icache_range(kvmppc_booke_handlers,
619d9fbd03dSHollis Blanchard 	                   kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
620d9fbd03dSHollis Blanchard 
621db93f574SHollis Blanchard 	return 0;
622d9fbd03dSHollis Blanchard }
623d9fbd03dSHollis Blanchard 
624db93f574SHollis Blanchard void __exit kvmppc_booke_exit(void)
625d9fbd03dSHollis Blanchard {
626d9fbd03dSHollis Blanchard 	free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER);
627d9fbd03dSHollis Blanchard 	kvm_exit();
628d9fbd03dSHollis Blanchard }
629