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