2 * PowerPC implementation of KVM hooks
4 * Copyright IBM Corp. 2007
5 * Copyright (C) 2011 Freescale Semiconductor, Inc.
8 * Jerone Young <jyoung5@us.ibm.com>
9 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
10 * Hollis Blanchard <hollisb@us.ibm.com>
12 * This work is licensed under the terms of the GNU GPL, version 2 or later.
13 * See the COPYING file in the top-level directory.
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
21 #include <linux/kvm.h>
23 #include "qemu-common.h"
24 #include "qemu-timer.h"
29 #include "device_tree.h"
34 #define dprintf(fmt, ...) \
35 do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
37 #define dprintf(fmt, ...) \
41 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
45 static int cap_interrupt_unset
= false;
46 static int cap_interrupt_level
= false;
47 static int cap_segstate
;
48 static int cap_booke_sregs
;
50 /* XXX We have a race condition where we actually have a level triggered
51 * interrupt, but the infrastructure can't expose that yet, so the guest
52 * takes but ignores it, goes to sleep and never gets notified that there's
53 * still an interrupt pending.
55 * As a quick workaround, let's just wake up again 20 ms after we injected
56 * an interrupt. That way we can assure that we're always reinjecting
57 * interrupts in case the guest swallowed them.
59 static QEMUTimer
*idle_timer
;
61 static void kvm_kick_env(void *env
)
66 int kvm_arch_init(KVMState
*s
)
68 #ifdef KVM_CAP_PPC_UNSET_IRQ
69 cap_interrupt_unset
= kvm_check_extension(s
, KVM_CAP_PPC_UNSET_IRQ
);
71 #ifdef KVM_CAP_PPC_IRQ_LEVEL
72 cap_interrupt_level
= kvm_check_extension(s
, KVM_CAP_PPC_IRQ_LEVEL
);
74 #ifdef KVM_CAP_PPC_SEGSTATE
75 cap_segstate
= kvm_check_extension(s
, KVM_CAP_PPC_SEGSTATE
);
77 #ifdef KVM_CAP_PPC_BOOKE_SREGS
78 cap_booke_sregs
= kvm_check_extension(s
, KVM_CAP_PPC_BOOKE_SREGS
);
81 if (!cap_interrupt_level
) {
82 fprintf(stderr
, "KVM: Couldn't find level irq capability. Expect the "
83 "VM to stall at times!\n");
89 static int kvm_arch_sync_sregs(CPUState
*cenv
)
91 struct kvm_sregs sregs
;
94 if (cenv
->excp_model
== POWERPC_EXCP_BOOKE
) {
95 /* What we're really trying to say is "if we're on BookE, we use
96 the native PVR for now". This is the only sane way to check
97 it though, so we potentially confuse users that they can run
98 BookE guests on BookS. Let's hope nobody dares enough :) */
102 fprintf(stderr
, "kvm error: missing PVR setting capability\n");
107 ret
= kvm_vcpu_ioctl(cenv
, KVM_GET_SREGS
, &sregs
);
112 sregs
.pvr
= cenv
->spr
[SPR_PVR
];
113 return kvm_vcpu_ioctl(cenv
, KVM_SET_SREGS
, &sregs
);
116 int kvm_arch_init_vcpu(CPUState
*cenv
)
120 ret
= kvm_arch_sync_sregs(cenv
);
125 idle_timer
= qemu_new_timer_ns(vm_clock
, kvm_kick_env
, cenv
);
130 void kvm_arch_reset_vcpu(CPUState
*env
)
134 int kvm_arch_put_registers(CPUState
*env
, int level
)
136 struct kvm_regs regs
;
140 ret
= kvm_vcpu_ioctl(env
, KVM_GET_REGS
, ®s
);
150 regs
.srr0
= env
->spr
[SPR_SRR0
];
151 regs
.srr1
= env
->spr
[SPR_SRR1
];
153 regs
.sprg0
= env
->spr
[SPR_SPRG0
];
154 regs
.sprg1
= env
->spr
[SPR_SPRG1
];
155 regs
.sprg2
= env
->spr
[SPR_SPRG2
];
156 regs
.sprg3
= env
->spr
[SPR_SPRG3
];
157 regs
.sprg4
= env
->spr
[SPR_SPRG4
];
158 regs
.sprg5
= env
->spr
[SPR_SPRG5
];
159 regs
.sprg6
= env
->spr
[SPR_SPRG6
];
160 regs
.sprg7
= env
->spr
[SPR_SPRG7
];
162 regs
.pid
= env
->spr
[SPR_BOOKE_PID
];
164 for (i
= 0;i
< 32; i
++)
165 regs
.gpr
[i
] = env
->gpr
[i
];
167 ret
= kvm_vcpu_ioctl(env
, KVM_SET_REGS
, ®s
);
174 int kvm_arch_get_registers(CPUState
*env
)
176 struct kvm_regs regs
;
177 struct kvm_sregs sregs
;
181 ret
= kvm_vcpu_ioctl(env
, KVM_GET_REGS
, ®s
);
186 for (i
= 7; i
>= 0; i
--) {
187 env
->crf
[i
] = cr
& 15;
197 env
->spr
[SPR_SRR0
] = regs
.srr0
;
198 env
->spr
[SPR_SRR1
] = regs
.srr1
;
200 env
->spr
[SPR_SPRG0
] = regs
.sprg0
;
201 env
->spr
[SPR_SPRG1
] = regs
.sprg1
;
202 env
->spr
[SPR_SPRG2
] = regs
.sprg2
;
203 env
->spr
[SPR_SPRG3
] = regs
.sprg3
;
204 env
->spr
[SPR_SPRG4
] = regs
.sprg4
;
205 env
->spr
[SPR_SPRG5
] = regs
.sprg5
;
206 env
->spr
[SPR_SPRG6
] = regs
.sprg6
;
207 env
->spr
[SPR_SPRG7
] = regs
.sprg7
;
209 env
->spr
[SPR_BOOKE_PID
] = regs
.pid
;
211 for (i
= 0;i
< 32; i
++)
212 env
->gpr
[i
] = regs
.gpr
[i
];
214 if (cap_booke_sregs
) {
215 ret
= kvm_vcpu_ioctl(env
, KVM_GET_SREGS
, &sregs
);
220 #ifdef KVM_CAP_PPC_BOOKE_SREGS
221 if (sregs
.u
.e
.features
& KVM_SREGS_E_BASE
) {
222 env
->spr
[SPR_BOOKE_CSRR0
] = sregs
.u
.e
.csrr0
;
223 env
->spr
[SPR_BOOKE_CSRR1
] = sregs
.u
.e
.csrr1
;
224 env
->spr
[SPR_BOOKE_ESR
] = sregs
.u
.e
.esr
;
225 env
->spr
[SPR_BOOKE_DEAR
] = sregs
.u
.e
.dear
;
226 env
->spr
[SPR_BOOKE_MCSR
] = sregs
.u
.e
.mcsr
;
227 env
->spr
[SPR_BOOKE_TSR
] = sregs
.u
.e
.tsr
;
228 env
->spr
[SPR_BOOKE_TCR
] = sregs
.u
.e
.tcr
;
229 env
->spr
[SPR_DECR
] = sregs
.u
.e
.dec
;
230 env
->spr
[SPR_TBL
] = sregs
.u
.e
.tb
& 0xffffffff;
231 env
->spr
[SPR_TBU
] = sregs
.u
.e
.tb
>> 32;
232 env
->spr
[SPR_VRSAVE
] = sregs
.u
.e
.vrsave
;
235 if (sregs
.u
.e
.features
& KVM_SREGS_E_ARCH206
) {
236 env
->spr
[SPR_BOOKE_PIR
] = sregs
.u
.e
.pir
;
237 env
->spr
[SPR_BOOKE_MCSRR0
] = sregs
.u
.e
.mcsrr0
;
238 env
->spr
[SPR_BOOKE_MCSRR1
] = sregs
.u
.e
.mcsrr1
;
239 env
->spr
[SPR_BOOKE_DECAR
] = sregs
.u
.e
.decar
;
240 env
->spr
[SPR_BOOKE_IVPR
] = sregs
.u
.e
.ivpr
;
243 if (sregs
.u
.e
.features
& KVM_SREGS_E_64
) {
244 env
->spr
[SPR_BOOKE_EPCR
] = sregs
.u
.e
.epcr
;
247 if (sregs
.u
.e
.features
& KVM_SREGS_E_SPRG8
) {
248 env
->spr
[SPR_BOOKE_SPRG8
] = sregs
.u
.e
.sprg8
;
251 if (sregs
.u
.e
.features
& KVM_SREGS_E_IVOR
) {
252 env
->spr
[SPR_BOOKE_IVOR0
] = sregs
.u
.e
.ivor_low
[0];
253 env
->spr
[SPR_BOOKE_IVOR1
] = sregs
.u
.e
.ivor_low
[1];
254 env
->spr
[SPR_BOOKE_IVOR2
] = sregs
.u
.e
.ivor_low
[2];
255 env
->spr
[SPR_BOOKE_IVOR3
] = sregs
.u
.e
.ivor_low
[3];
256 env
->spr
[SPR_BOOKE_IVOR4
] = sregs
.u
.e
.ivor_low
[4];
257 env
->spr
[SPR_BOOKE_IVOR5
] = sregs
.u
.e
.ivor_low
[5];
258 env
->spr
[SPR_BOOKE_IVOR6
] = sregs
.u
.e
.ivor_low
[6];
259 env
->spr
[SPR_BOOKE_IVOR7
] = sregs
.u
.e
.ivor_low
[7];
260 env
->spr
[SPR_BOOKE_IVOR8
] = sregs
.u
.e
.ivor_low
[8];
261 env
->spr
[SPR_BOOKE_IVOR9
] = sregs
.u
.e
.ivor_low
[9];
262 env
->spr
[SPR_BOOKE_IVOR10
] = sregs
.u
.e
.ivor_low
[10];
263 env
->spr
[SPR_BOOKE_IVOR11
] = sregs
.u
.e
.ivor_low
[11];
264 env
->spr
[SPR_BOOKE_IVOR12
] = sregs
.u
.e
.ivor_low
[12];
265 env
->spr
[SPR_BOOKE_IVOR13
] = sregs
.u
.e
.ivor_low
[13];
266 env
->spr
[SPR_BOOKE_IVOR14
] = sregs
.u
.e
.ivor_low
[14];
267 env
->spr
[SPR_BOOKE_IVOR15
] = sregs
.u
.e
.ivor_low
[15];
269 if (sregs
.u
.e
.features
& KVM_SREGS_E_SPE
) {
270 env
->spr
[SPR_BOOKE_IVOR32
] = sregs
.u
.e
.ivor_high
[0];
271 env
->spr
[SPR_BOOKE_IVOR33
] = sregs
.u
.e
.ivor_high
[1];
272 env
->spr
[SPR_BOOKE_IVOR34
] = sregs
.u
.e
.ivor_high
[2];
275 if (sregs
.u
.e
.features
& KVM_SREGS_E_PM
) {
276 env
->spr
[SPR_BOOKE_IVOR35
] = sregs
.u
.e
.ivor_high
[3];
279 if (sregs
.u
.e
.features
& KVM_SREGS_E_PC
) {
280 env
->spr
[SPR_BOOKE_IVOR36
] = sregs
.u
.e
.ivor_high
[4];
281 env
->spr
[SPR_BOOKE_IVOR37
] = sregs
.u
.e
.ivor_high
[5];
285 if (sregs
.u
.e
.features
& KVM_SREGS_E_ARCH206_MMU
) {
286 env
->spr
[SPR_BOOKE_MAS0
] = sregs
.u
.e
.mas0
;
287 env
->spr
[SPR_BOOKE_MAS1
] = sregs
.u
.e
.mas1
;
288 env
->spr
[SPR_BOOKE_MAS2
] = sregs
.u
.e
.mas2
;
289 env
->spr
[SPR_BOOKE_MAS3
] = sregs
.u
.e
.mas7_3
& 0xffffffff;
290 env
->spr
[SPR_BOOKE_MAS4
] = sregs
.u
.e
.mas4
;
291 env
->spr
[SPR_BOOKE_MAS6
] = sregs
.u
.e
.mas6
;
292 env
->spr
[SPR_BOOKE_MAS7
] = sregs
.u
.e
.mas7_3
>> 32;
293 env
->spr
[SPR_MMUCFG
] = sregs
.u
.e
.mmucfg
;
294 env
->spr
[SPR_BOOKE_TLB0CFG
] = sregs
.u
.e
.tlbcfg
[0];
295 env
->spr
[SPR_BOOKE_TLB1CFG
] = sregs
.u
.e
.tlbcfg
[1];
298 if (sregs
.u
.e
.features
& KVM_SREGS_EXP
) {
299 env
->spr
[SPR_BOOKE_EPR
] = sregs
.u
.e
.epr
;
302 if (sregs
.u
.e
.features
& KVM_SREGS_E_PD
) {
303 env
->spr
[SPR_BOOKE_EPLC
] = sregs
.u
.e
.eplc
;
304 env
->spr
[SPR_BOOKE_EPSC
] = sregs
.u
.e
.epsc
;
307 if (sregs
.u
.e
.impl_id
== KVM_SREGS_E_IMPL_FSL
) {
308 env
->spr
[SPR_E500_SVR
] = sregs
.u
.e
.impl
.fsl
.svr
;
309 env
->spr
[SPR_Exxx_MCAR
] = sregs
.u
.e
.impl
.fsl
.mcar
;
310 env
->spr
[SPR_HID0
] = sregs
.u
.e
.impl
.fsl
.hid0
;
312 if (sregs
.u
.e
.impl
.fsl
.features
& KVM_SREGS_E_FSL_PIDn
) {
313 env
->spr
[SPR_BOOKE_PID1
] = sregs
.u
.e
.impl
.fsl
.pid1
;
314 env
->spr
[SPR_BOOKE_PID2
] = sregs
.u
.e
.impl
.fsl
.pid2
;
321 ret
= kvm_vcpu_ioctl(env
, KVM_GET_SREGS
, &sregs
);
326 #ifdef KVM_CAP_PPC_SEGSTATE
327 ppc_store_sdr1(env
, sregs
.u
.s
.sdr1
);
331 for (i
= 0; i
< 64; i
++) {
332 ppc_store_slb(env
, sregs
.u
.s
.ppc64
.slb
[i
].slbe
,
333 sregs
.u
.s
.ppc64
.slb
[i
].slbv
);
338 for (i
= 0; i
< 16; i
++) {
339 env
->sr
[i
] = sregs
.u
.s
.ppc32
.sr
[i
];
343 for (i
= 0; i
< 8; i
++) {
344 env
->DBAT
[0][i
] = sregs
.u
.s
.ppc32
.dbat
[i
] & 0xffffffff;
345 env
->DBAT
[1][i
] = sregs
.u
.s
.ppc32
.dbat
[i
] >> 32;
346 env
->IBAT
[0][i
] = sregs
.u
.s
.ppc32
.ibat
[i
] & 0xffffffff;
347 env
->IBAT
[1][i
] = sregs
.u
.s
.ppc32
.ibat
[i
] >> 32;
355 int kvmppc_set_interrupt(CPUState
*env
, int irq
, int level
)
357 unsigned virq
= level
? KVM_INTERRUPT_SET_LEVEL
: KVM_INTERRUPT_UNSET
;
359 if (irq
!= PPC_INTERRUPT_EXT
) {
363 if (!kvm_enabled() || !cap_interrupt_unset
|| !cap_interrupt_level
) {
367 kvm_vcpu_ioctl(env
, KVM_INTERRUPT
, &virq
);
372 #if defined(TARGET_PPCEMB)
373 #define PPC_INPUT_INT PPC40x_INPUT_INT
374 #elif defined(TARGET_PPC64)
375 #define PPC_INPUT_INT PPC970_INPUT_INT
377 #define PPC_INPUT_INT PPC6xx_INPUT_INT
380 void kvm_arch_pre_run(CPUState
*env
, struct kvm_run
*run
)
385 /* PowerPC Qemu tracks the various core input pins (interrupt, critical
386 * interrupt, reset, etc) in PPC-specific env->irq_input_state. */
387 if (!cap_interrupt_level
&&
388 run
->ready_for_interrupt_injection
&&
389 (env
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
390 (env
->irq_input_state
& (1<<PPC_INPUT_INT
)))
392 /* For now KVM disregards the 'irq' argument. However, in the
393 * future KVM could cache it in-kernel to avoid a heavyweight exit
394 * when reading the UIC.
396 irq
= KVM_INTERRUPT_SET
;
398 dprintf("injected interrupt %d\n", irq
);
399 r
= kvm_vcpu_ioctl(env
, KVM_INTERRUPT
, &irq
);
401 printf("cpu %d fail inject %x\n", env
->cpu_index
, irq
);
403 /* Always wake up soon in case the interrupt was level based */
404 qemu_mod_timer(idle_timer
, qemu_get_clock_ns(vm_clock
) +
405 (get_ticks_per_sec() / 50));
408 /* We don't know if there are more interrupts pending after this. However,
409 * the guest will return to userspace in the course of handling this one
410 * anyways, so we will get a chance to deliver the rest. */
413 void kvm_arch_post_run(CPUState
*env
, struct kvm_run
*run
)
417 int kvm_arch_process_async_events(CPUState
*env
)
422 static int kvmppc_handle_halt(CPUState
*env
)
424 if (!(env
->interrupt_request
& CPU_INTERRUPT_HARD
) && (msr_ee
)) {
426 env
->exception_index
= EXCP_HLT
;
432 /* map dcr access to existing qemu dcr emulation */
433 static int kvmppc_handle_dcr_read(CPUState
*env
, uint32_t dcrn
, uint32_t *data
)
435 if (ppc_dcr_read(env
->dcr_env
, dcrn
, data
) < 0)
436 fprintf(stderr
, "Read to unhandled DCR (0x%x)\n", dcrn
);
441 static int kvmppc_handle_dcr_write(CPUState
*env
, uint32_t dcrn
, uint32_t data
)
443 if (ppc_dcr_write(env
->dcr_env
, dcrn
, data
) < 0)
444 fprintf(stderr
, "Write to unhandled DCR (0x%x)\n", dcrn
);
449 int kvm_arch_handle_exit(CPUState
*env
, struct kvm_run
*run
)
453 switch (run
->exit_reason
) {
455 if (run
->dcr
.is_write
) {
456 dprintf("handle dcr write\n");
457 ret
= kvmppc_handle_dcr_write(env
, run
->dcr
.dcrn
, run
->dcr
.data
);
459 dprintf("handle dcr read\n");
460 ret
= kvmppc_handle_dcr_read(env
, run
->dcr
.dcrn
, &run
->dcr
.data
);
464 dprintf("handle halt\n");
465 ret
= kvmppc_handle_halt(env
);
468 fprintf(stderr
, "KVM: unknown exit reason %d\n", run
->exit_reason
);
476 static int read_cpuinfo(const char *field
, char *value
, int len
)
480 int field_len
= strlen(field
);
483 f
= fopen("/proc/cpuinfo", "r");
489 if(!fgets(line
, sizeof(line
), f
)) {
492 if (!strncmp(line
, field
, field_len
)) {
493 strncpy(value
, line
, len
);
504 uint32_t kvmppc_get_tbfreq(void)
508 uint32_t retval
= get_ticks_per_sec();
510 if (read_cpuinfo("timebase", line
, sizeof(line
))) {
514 if (!(ns
= strchr(line
, ':'))) {
524 int kvmppc_get_hypercall(CPUState
*env
, uint8_t *buf
, int buf_len
)
526 uint32_t *hc
= (uint32_t*)buf
;
528 #ifdef KVM_CAP_PPC_GET_PVINFO
529 struct kvm_ppc_pvinfo pvinfo
;
531 if (kvm_check_extension(env
->kvm_state
, KVM_CAP_PPC_GET_PVINFO
) &&
532 !kvm_vm_ioctl(env
->kvm_state
, KVM_PPC_GET_PVINFO
, &pvinfo
)) {
533 memcpy(buf
, pvinfo
.hcall
, buf_len
);
540 * Fallback to always fail hypercalls:
556 bool kvm_arch_stop_on_emulation_error(CPUState
*env
)
561 int kvm_arch_on_sigbus_vcpu(CPUState
*env
, int code
, void *addr
)
566 int kvm_arch_on_sigbus(int code
, void *addr
)