2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * KVM/MIPS: MIPS specific KVM APIs
8 * Copyright (C) 2012-2014 Imagination Technologies Ltd.
9 * Authors: Sanjay Lal <sanjayl@kymasys.com>
12 #include <sys/types.h>
13 #include <sys/ioctl.h>
16 #include <linux/kvm.h>
18 #include "qemu-common.h"
19 #include "qemu/error-report.h"
20 #include "qemu/timer.h"
21 #include "sysemu/sysemu.h"
22 #include "sysemu/kvm.h"
24 #include "sysemu/cpus.h"
26 #include "exec/memattrs.h"
30 #define DPRINTF(fmt, ...) \
31 do { if (DEBUG_KVM) { fprintf(stderr, fmt, ## __VA_ARGS__); } } while (0)
33 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
37 static void kvm_mips_update_state(void *opaque
, int running
, RunState state
);
39 unsigned long kvm_arch_vcpu_id(CPUState
*cs
)
44 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
46 /* MIPS has 128 signals */
47 kvm_set_sigmask_len(s
, 16);
49 DPRINTF("%s\n", __func__
);
53 int kvm_arch_init_vcpu(CPUState
*cs
)
57 qemu_add_vm_change_state_handler(kvm_mips_update_state
, cs
);
59 DPRINTF("%s\n", __func__
);
63 void kvm_mips_reset_vcpu(MIPSCPU
*cpu
)
65 CPUMIPSState
*env
= &cpu
->env
;
67 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
68 fprintf(stderr
, "Warning: FPU not supported with KVM, disabling\n");
69 env
->CP0_Config1
&= ~(1 << CP0C1_FP
);
72 DPRINTF("%s\n", __func__
);
75 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
77 DPRINTF("%s\n", __func__
);
81 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
83 DPRINTF("%s\n", __func__
);
87 static inline int cpu_mips_io_interrupts_pending(MIPSCPU
*cpu
)
89 CPUMIPSState
*env
= &cpu
->env
;
91 DPRINTF("%s: %#x\n", __func__
, env
->CP0_Cause
& (1 << (2 + CP0Ca_IP
)));
92 return env
->CP0_Cause
& (0x1 << (2 + CP0Ca_IP
));
96 void kvm_arch_pre_run(CPUState
*cs
, struct kvm_run
*run
)
98 MIPSCPU
*cpu
= MIPS_CPU(cs
);
100 struct kvm_mips_interrupt intr
;
102 qemu_mutex_lock_iothread();
104 if ((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
105 cpu_mips_io_interrupts_pending(cpu
)) {
108 r
= kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
110 error_report("%s: cpu %d: failed to inject IRQ %x",
111 __func__
, cs
->cpu_index
, intr
.irq
);
115 qemu_mutex_unlock_iothread();
118 MemTxAttrs
kvm_arch_post_run(CPUState
*cs
, struct kvm_run
*run
)
120 DPRINTF("%s\n", __func__
);
121 return MEMTXATTRS_UNSPECIFIED
;
124 int kvm_arch_process_async_events(CPUState
*cs
)
129 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
133 DPRINTF("%s\n", __func__
);
134 switch (run
->exit_reason
) {
136 error_report("%s: unknown exit reason %d",
137 __func__
, run
->exit_reason
);
145 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
147 DPRINTF("%s\n", __func__
);
151 int kvm_arch_on_sigbus_vcpu(CPUState
*cs
, int code
, void *addr
)
153 DPRINTF("%s\n", __func__
);
157 int kvm_arch_on_sigbus(int code
, void *addr
)
159 DPRINTF("%s\n", __func__
);
163 void kvm_arch_init_irq_routing(KVMState
*s
)
167 int kvm_mips_set_interrupt(MIPSCPU
*cpu
, int irq
, int level
)
169 CPUState
*cs
= CPU(cpu
);
170 struct kvm_mips_interrupt intr
;
172 if (!kvm_enabled()) {
184 kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
189 int kvm_mips_set_ipi_interrupt(MIPSCPU
*cpu
, int irq
, int level
)
191 CPUState
*cs
= current_cpu
;
192 CPUState
*dest_cs
= CPU(cpu
);
193 struct kvm_mips_interrupt intr
;
195 if (!kvm_enabled()) {
199 intr
.cpu
= dest_cs
->cpu_index
;
207 DPRINTF("%s: CPU %d, IRQ: %d\n", __func__
, intr
.cpu
, intr
.irq
);
209 kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
214 #define MIPS_CP0_32(_R, _S) \
215 (KVM_REG_MIPS | KVM_REG_SIZE_U32 | 0x10000 | (8 * (_R) + (_S)))
217 #define MIPS_CP0_64(_R, _S) \
218 (KVM_REG_MIPS | KVM_REG_SIZE_U64 | 0x10000 | (8 * (_R) + (_S)))
220 #define KVM_REG_MIPS_CP0_INDEX MIPS_CP0_32(0, 0)
221 #define KVM_REG_MIPS_CP0_CONTEXT MIPS_CP0_64(4, 0)
222 #define KVM_REG_MIPS_CP0_USERLOCAL MIPS_CP0_64(4, 2)
223 #define KVM_REG_MIPS_CP0_PAGEMASK MIPS_CP0_32(5, 0)
224 #define KVM_REG_MIPS_CP0_WIRED MIPS_CP0_32(6, 0)
225 #define KVM_REG_MIPS_CP0_HWRENA MIPS_CP0_32(7, 0)
226 #define KVM_REG_MIPS_CP0_BADVADDR MIPS_CP0_64(8, 0)
227 #define KVM_REG_MIPS_CP0_COUNT MIPS_CP0_32(9, 0)
228 #define KVM_REG_MIPS_CP0_ENTRYHI MIPS_CP0_64(10, 0)
229 #define KVM_REG_MIPS_CP0_COMPARE MIPS_CP0_32(11, 0)
230 #define KVM_REG_MIPS_CP0_STATUS MIPS_CP0_32(12, 0)
231 #define KVM_REG_MIPS_CP0_CAUSE MIPS_CP0_32(13, 0)
232 #define KVM_REG_MIPS_CP0_EPC MIPS_CP0_64(14, 0)
233 #define KVM_REG_MIPS_CP0_ERROREPC MIPS_CP0_64(30, 0)
235 /* CP0_Count control */
236 #define KVM_REG_MIPS_COUNT_CTL (KVM_REG_MIPS | KVM_REG_SIZE_U64 | \
238 #define KVM_REG_MIPS_COUNT_CTL_DC 0x00000001 /* master disable */
239 /* CP0_Count resume monotonic nanoseconds */
240 #define KVM_REG_MIPS_COUNT_RESUME (KVM_REG_MIPS | KVM_REG_SIZE_U64 | \
242 /* CP0_Count rate in Hz */
243 #define KVM_REG_MIPS_COUNT_HZ (KVM_REG_MIPS | KVM_REG_SIZE_U64 | \
246 static inline int kvm_mips_put_one_reg(CPUState
*cs
, uint64_t reg_id
,
249 uint64_t val64
= *addr
;
250 struct kvm_one_reg cp0reg
= {
252 .addr
= (uintptr_t)&val64
255 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
258 static inline int kvm_mips_put_one_ulreg(CPUState
*cs
, uint64_t reg_id
,
261 uint64_t val64
= *addr
;
262 struct kvm_one_reg cp0reg
= {
264 .addr
= (uintptr_t)&val64
267 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
270 static inline int kvm_mips_put_one_reg64(CPUState
*cs
, uint64_t reg_id
,
273 struct kvm_one_reg cp0reg
= {
275 .addr
= (uintptr_t)addr
278 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
281 static inline int kvm_mips_get_one_reg(CPUState
*cs
, uint64_t reg_id
,
286 struct kvm_one_reg cp0reg
= {
288 .addr
= (uintptr_t)&val64
291 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
298 static inline int kvm_mips_get_one_ulreg(CPUState
*cs
, uint64 reg_id
,
303 struct kvm_one_reg cp0reg
= {
305 .addr
= (uintptr_t)&val64
308 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
315 static inline int kvm_mips_get_one_reg64(CPUState
*cs
, uint64 reg_id
,
318 struct kvm_one_reg cp0reg
= {
320 .addr
= (uintptr_t)addr
323 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
327 * We freeze the KVM timer when either the VM clock is stopped or the state is
328 * saved (the state is dirty).
332 * Save the state of the KVM timer when VM clock is stopped or state is synced
335 static int kvm_mips_save_count(CPUState
*cs
)
337 MIPSCPU
*cpu
= MIPS_CPU(cs
);
338 CPUMIPSState
*env
= &cpu
->env
;
342 /* freeze KVM timer */
343 err
= kvm_mips_get_one_reg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
345 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__
, err
);
347 } else if (!(count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
)) {
348 count_ctl
|= KVM_REG_MIPS_COUNT_CTL_DC
;
349 err
= kvm_mips_put_one_reg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
351 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__
, err
);
357 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CAUSE
, &env
->CP0_Cause
);
359 DPRINTF("%s: Failed to get CP0_CAUSE (%d)\n", __func__
, err
);
364 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_COUNT
, &env
->CP0_Count
);
366 DPRINTF("%s: Failed to get CP0_COUNT (%d)\n", __func__
, err
);
374 * Restore the state of the KVM timer when VM clock is restarted or state is
377 static int kvm_mips_restore_count(CPUState
*cs
)
379 MIPSCPU
*cpu
= MIPS_CPU(cs
);
380 CPUMIPSState
*env
= &cpu
->env
;
382 int err_dc
, err
, ret
= 0;
384 /* check the timer is frozen */
385 err_dc
= kvm_mips_get_one_reg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
387 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__
, err_dc
);
389 } else if (!(count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
)) {
390 /* freeze timer (sets COUNT_RESUME for us) */
391 count_ctl
|= KVM_REG_MIPS_COUNT_CTL_DC
;
392 err
= kvm_mips_put_one_reg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
394 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__
, err
);
400 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_CAUSE
, &env
->CP0_Cause
);
402 DPRINTF("%s: Failed to put CP0_CAUSE (%d)\n", __func__
, err
);
407 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_COUNT
, &env
->CP0_Count
);
409 DPRINTF("%s: Failed to put CP0_COUNT (%d)\n", __func__
, err
);
413 /* resume KVM timer */
415 count_ctl
&= ~KVM_REG_MIPS_COUNT_CTL_DC
;
416 err
= kvm_mips_put_one_reg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
418 DPRINTF("%s: Failed to set COUNT_CTL.DC=0 (%d)\n", __func__
, err
);
427 * Handle the VM clock being started or stopped
429 static void kvm_mips_update_state(void *opaque
, int running
, RunState state
)
431 CPUState
*cs
= opaque
;
433 uint64_t count_resume
;
436 * If state is already dirty (synced to QEMU) then the KVM timer state is
437 * already saved and can be restored when it is synced back to KVM.
440 if (!cs
->kvm_vcpu_dirty
) {
441 ret
= kvm_mips_save_count(cs
);
443 fprintf(stderr
, "Failed saving count\n");
447 /* Set clock restore time to now */
448 count_resume
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
449 ret
= kvm_mips_put_one_reg64(cs
, KVM_REG_MIPS_COUNT_RESUME
,
452 fprintf(stderr
, "Failed setting COUNT_RESUME\n");
456 if (!cs
->kvm_vcpu_dirty
) {
457 ret
= kvm_mips_restore_count(cs
);
459 fprintf(stderr
, "Failed restoring count\n");
465 static int kvm_mips_put_cp0_registers(CPUState
*cs
, int level
)
467 MIPSCPU
*cpu
= MIPS_CPU(cs
);
468 CPUMIPSState
*env
= &cpu
->env
;
473 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_INDEX
, &env
->CP0_Index
);
475 DPRINTF("%s: Failed to put CP0_INDEX (%d)\n", __func__
, err
);
478 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_CONTEXT
,
481 DPRINTF("%s: Failed to put CP0_CONTEXT (%d)\n", __func__
, err
);
484 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_USERLOCAL
,
485 &env
->active_tc
.CP0_UserLocal
);
487 DPRINTF("%s: Failed to put CP0_USERLOCAL (%d)\n", __func__
, err
);
490 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEMASK
,
493 DPRINTF("%s: Failed to put CP0_PAGEMASK (%d)\n", __func__
, err
);
496 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_WIRED
, &env
->CP0_Wired
);
498 DPRINTF("%s: Failed to put CP0_WIRED (%d)\n", __func__
, err
);
501 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_HWRENA
, &env
->CP0_HWREna
);
503 DPRINTF("%s: Failed to put CP0_HWRENA (%d)\n", __func__
, err
);
506 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_BADVADDR
,
509 DPRINTF("%s: Failed to put CP0_BADVADDR (%d)\n", __func__
, err
);
513 /* If VM clock stopped then state will be restored when it is restarted */
514 if (runstate_is_running()) {
515 err
= kvm_mips_restore_count(cs
);
521 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_ENTRYHI
,
524 DPRINTF("%s: Failed to put CP0_ENTRYHI (%d)\n", __func__
, err
);
527 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_COMPARE
,
530 DPRINTF("%s: Failed to put CP0_COMPARE (%d)\n", __func__
, err
);
533 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_STATUS
, &env
->CP0_Status
);
535 DPRINTF("%s: Failed to put CP0_STATUS (%d)\n", __func__
, err
);
538 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_EPC
, &env
->CP0_EPC
);
540 DPRINTF("%s: Failed to put CP0_EPC (%d)\n", __func__
, err
);
543 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_ERROREPC
,
546 DPRINTF("%s: Failed to put CP0_ERROREPC (%d)\n", __func__
, err
);
553 static int kvm_mips_get_cp0_registers(CPUState
*cs
)
555 MIPSCPU
*cpu
= MIPS_CPU(cs
);
556 CPUMIPSState
*env
= &cpu
->env
;
559 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_INDEX
, &env
->CP0_Index
);
561 DPRINTF("%s: Failed to get CP0_INDEX (%d)\n", __func__
, err
);
564 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_CONTEXT
,
567 DPRINTF("%s: Failed to get CP0_CONTEXT (%d)\n", __func__
, err
);
570 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_USERLOCAL
,
571 &env
->active_tc
.CP0_UserLocal
);
573 DPRINTF("%s: Failed to get CP0_USERLOCAL (%d)\n", __func__
, err
);
576 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEMASK
,
579 DPRINTF("%s: Failed to get CP0_PAGEMASK (%d)\n", __func__
, err
);
582 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_WIRED
, &env
->CP0_Wired
);
584 DPRINTF("%s: Failed to get CP0_WIRED (%d)\n", __func__
, err
);
587 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_HWRENA
, &env
->CP0_HWREna
);
589 DPRINTF("%s: Failed to get CP0_HWRENA (%d)\n", __func__
, err
);
592 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_BADVADDR
,
595 DPRINTF("%s: Failed to get CP0_BADVADDR (%d)\n", __func__
, err
);
598 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_ENTRYHI
,
601 DPRINTF("%s: Failed to get CP0_ENTRYHI (%d)\n", __func__
, err
);
604 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_COMPARE
,
607 DPRINTF("%s: Failed to get CP0_COMPARE (%d)\n", __func__
, err
);
610 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_STATUS
, &env
->CP0_Status
);
612 DPRINTF("%s: Failed to get CP0_STATUS (%d)\n", __func__
, err
);
616 /* If VM clock stopped then state was already saved when it was stopped */
617 if (runstate_is_running()) {
618 err
= kvm_mips_save_count(cs
);
624 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_EPC
, &env
->CP0_EPC
);
626 DPRINTF("%s: Failed to get CP0_EPC (%d)\n", __func__
, err
);
629 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_ERROREPC
,
632 DPRINTF("%s: Failed to get CP0_ERROREPC (%d)\n", __func__
, err
);
639 int kvm_arch_put_registers(CPUState
*cs
, int level
)
641 MIPSCPU
*cpu
= MIPS_CPU(cs
);
642 CPUMIPSState
*env
= &cpu
->env
;
643 struct kvm_regs regs
;
647 /* Set the registers based on QEMU's view of things */
648 for (i
= 0; i
< 32; i
++) {
649 regs
.gpr
[i
] = env
->active_tc
.gpr
[i
];
652 regs
.hi
= env
->active_tc
.HI
[0];
653 regs
.lo
= env
->active_tc
.LO
[0];
654 regs
.pc
= env
->active_tc
.PC
;
656 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_REGS
, ®s
);
662 ret
= kvm_mips_put_cp0_registers(cs
, level
);
670 int kvm_arch_get_registers(CPUState
*cs
)
672 MIPSCPU
*cpu
= MIPS_CPU(cs
);
673 CPUMIPSState
*env
= &cpu
->env
;
675 struct kvm_regs regs
;
678 /* Get the current register set as KVM seems it */
679 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_REGS
, ®s
);
685 for (i
= 0; i
< 32; i
++) {
686 env
->active_tc
.gpr
[i
] = regs
.gpr
[i
];
689 env
->active_tc
.HI
[0] = regs
.hi
;
690 env
->active_tc
.LO
[0] = regs
.lo
;
691 env
->active_tc
.PC
= regs
.pc
;
693 kvm_mips_get_cp0_registers(cs
);
698 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
699 uint64_t address
, uint32_t data
)
704 int kvm_arch_msi_data_to_gsi(uint32_t data
)