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"
29 #define DPRINTF(fmt, ...) \
30 do { if (DEBUG_KVM) { fprintf(stderr, fmt, ## __VA_ARGS__); } } while (0)
32 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
36 static void kvm_mips_update_state(void *opaque
, int running
, RunState state
);
38 unsigned long kvm_arch_vcpu_id(CPUState
*cs
)
43 int kvm_arch_init(KVMState
*s
)
45 /* MIPS has 128 signals */
46 kvm_set_sigmask_len(s
, 16);
48 DPRINTF("%s\n", __func__
);
52 int kvm_arch_init_vcpu(CPUState
*cs
)
56 qemu_add_vm_change_state_handler(kvm_mips_update_state
, cs
);
58 DPRINTF("%s\n", __func__
);
62 void kvm_mips_reset_vcpu(MIPSCPU
*cpu
)
64 CPUMIPSState
*env
= &cpu
->env
;
66 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
67 fprintf(stderr
, "Warning: FPU not supported with KVM, disabling\n");
68 env
->CP0_Config1
&= ~(1 << CP0C1_FP
);
71 DPRINTF("%s\n", __func__
);
74 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
76 DPRINTF("%s\n", __func__
);
80 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
82 DPRINTF("%s\n", __func__
);
86 static inline int cpu_mips_io_interrupts_pending(MIPSCPU
*cpu
)
88 CPUMIPSState
*env
= &cpu
->env
;
90 DPRINTF("%s: %#x\n", __func__
, env
->CP0_Cause
& (1 << (2 + CP0Ca_IP
)));
91 return env
->CP0_Cause
& (0x1 << (2 + CP0Ca_IP
));
95 void kvm_arch_pre_run(CPUState
*cs
, struct kvm_run
*run
)
97 MIPSCPU
*cpu
= MIPS_CPU(cs
);
99 struct kvm_mips_interrupt intr
;
101 if ((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
102 cpu_mips_io_interrupts_pending(cpu
)) {
105 r
= kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
107 error_report("%s: cpu %d: failed to inject IRQ %x",
108 __func__
, cs
->cpu_index
, intr
.irq
);
113 void kvm_arch_post_run(CPUState
*cs
, struct kvm_run
*run
)
115 DPRINTF("%s\n", __func__
);
118 int kvm_arch_process_async_events(CPUState
*cs
)
123 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
127 DPRINTF("%s\n", __func__
);
128 switch (run
->exit_reason
) {
130 error_report("%s: unknown exit reason %d",
131 __func__
, run
->exit_reason
);
139 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
141 DPRINTF("%s\n", __func__
);
145 int kvm_arch_on_sigbus_vcpu(CPUState
*cs
, int code
, void *addr
)
147 DPRINTF("%s\n", __func__
);
151 int kvm_arch_on_sigbus(int code
, void *addr
)
153 DPRINTF("%s\n", __func__
);
157 void kvm_arch_init_irq_routing(KVMState
*s
)
161 int kvm_mips_set_interrupt(MIPSCPU
*cpu
, int irq
, int level
)
163 CPUState
*cs
= CPU(cpu
);
164 struct kvm_mips_interrupt intr
;
166 if (!kvm_enabled()) {
178 kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
183 int kvm_mips_set_ipi_interrupt(MIPSCPU
*cpu
, int irq
, int level
)
185 CPUState
*cs
= current_cpu
;
186 CPUState
*dest_cs
= CPU(cpu
);
187 struct kvm_mips_interrupt intr
;
189 if (!kvm_enabled()) {
193 intr
.cpu
= dest_cs
->cpu_index
;
201 DPRINTF("%s: CPU %d, IRQ: %d\n", __func__
, intr
.cpu
, intr
.irq
);
203 kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
208 #define MIPS_CP0_32(_R, _S) \
209 (KVM_REG_MIPS | KVM_REG_SIZE_U32 | 0x10000 | (8 * (_R) + (_S)))
211 #define MIPS_CP0_64(_R, _S) \
212 (KVM_REG_MIPS | KVM_REG_SIZE_U64 | 0x10000 | (8 * (_R) + (_S)))
214 #define KVM_REG_MIPS_CP0_INDEX MIPS_CP0_32(0, 0)
215 #define KVM_REG_MIPS_CP0_CONTEXT MIPS_CP0_64(4, 0)
216 #define KVM_REG_MIPS_CP0_USERLOCAL MIPS_CP0_64(4, 2)
217 #define KVM_REG_MIPS_CP0_PAGEMASK MIPS_CP0_32(5, 0)
218 #define KVM_REG_MIPS_CP0_WIRED MIPS_CP0_32(6, 0)
219 #define KVM_REG_MIPS_CP0_HWRENA MIPS_CP0_32(7, 0)
220 #define KVM_REG_MIPS_CP0_BADVADDR MIPS_CP0_64(8, 0)
221 #define KVM_REG_MIPS_CP0_COUNT MIPS_CP0_32(9, 0)
222 #define KVM_REG_MIPS_CP0_ENTRYHI MIPS_CP0_64(10, 0)
223 #define KVM_REG_MIPS_CP0_COMPARE MIPS_CP0_32(11, 0)
224 #define KVM_REG_MIPS_CP0_STATUS MIPS_CP0_32(12, 0)
225 #define KVM_REG_MIPS_CP0_CAUSE MIPS_CP0_32(13, 0)
226 #define KVM_REG_MIPS_CP0_EPC MIPS_CP0_64(14, 0)
227 #define KVM_REG_MIPS_CP0_ERROREPC MIPS_CP0_64(30, 0)
229 /* CP0_Count control */
230 #define KVM_REG_MIPS_COUNT_CTL (KVM_REG_MIPS | KVM_REG_SIZE_U64 | \
232 #define KVM_REG_MIPS_COUNT_CTL_DC 0x00000001 /* master disable */
233 /* CP0_Count resume monotonic nanoseconds */
234 #define KVM_REG_MIPS_COUNT_RESUME (KVM_REG_MIPS | KVM_REG_SIZE_U64 | \
236 /* CP0_Count rate in Hz */
237 #define KVM_REG_MIPS_COUNT_HZ (KVM_REG_MIPS | KVM_REG_SIZE_U64 | \
240 static inline int kvm_mips_put_one_reg(CPUState
*cs
, uint64_t reg_id
,
243 uint64_t val64
= *addr
;
244 struct kvm_one_reg cp0reg
= {
246 .addr
= (uintptr_t)&val64
249 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
252 static inline int kvm_mips_put_one_ulreg(CPUState
*cs
, uint64_t reg_id
,
255 uint64_t val64
= *addr
;
256 struct kvm_one_reg cp0reg
= {
258 .addr
= (uintptr_t)&val64
261 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
264 static inline int kvm_mips_put_one_reg64(CPUState
*cs
, uint64_t reg_id
,
267 struct kvm_one_reg cp0reg
= {
269 .addr
= (uintptr_t)addr
272 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
275 static inline int kvm_mips_get_one_reg(CPUState
*cs
, uint64_t reg_id
,
280 struct kvm_one_reg cp0reg
= {
282 .addr
= (uintptr_t)&val64
285 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
292 static inline int kvm_mips_get_one_ulreg(CPUState
*cs
, uint64 reg_id
,
297 struct kvm_one_reg cp0reg
= {
299 .addr
= (uintptr_t)&val64
302 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
309 static inline int kvm_mips_get_one_reg64(CPUState
*cs
, uint64 reg_id
,
312 struct kvm_one_reg cp0reg
= {
314 .addr
= (uintptr_t)addr
317 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
321 * We freeze the KVM timer when either the VM clock is stopped or the state is
322 * saved (the state is dirty).
326 * Save the state of the KVM timer when VM clock is stopped or state is synced
329 static int kvm_mips_save_count(CPUState
*cs
)
331 MIPSCPU
*cpu
= MIPS_CPU(cs
);
332 CPUMIPSState
*env
= &cpu
->env
;
336 /* freeze KVM timer */
337 err
= kvm_mips_get_one_reg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
339 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__
, err
);
341 } else if (!(count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
)) {
342 count_ctl
|= KVM_REG_MIPS_COUNT_CTL_DC
;
343 err
= kvm_mips_put_one_reg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
345 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__
, err
);
351 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CAUSE
, &env
->CP0_Cause
);
353 DPRINTF("%s: Failed to get CP0_CAUSE (%d)\n", __func__
, err
);
358 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_COUNT
, &env
->CP0_Count
);
360 DPRINTF("%s: Failed to get CP0_COUNT (%d)\n", __func__
, err
);
368 * Restore the state of the KVM timer when VM clock is restarted or state is
371 static int kvm_mips_restore_count(CPUState
*cs
)
373 MIPSCPU
*cpu
= MIPS_CPU(cs
);
374 CPUMIPSState
*env
= &cpu
->env
;
376 int err_dc
, err
, ret
= 0;
378 /* check the timer is frozen */
379 err_dc
= kvm_mips_get_one_reg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
381 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__
, err_dc
);
383 } else if (!(count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
)) {
384 /* freeze timer (sets COUNT_RESUME for us) */
385 count_ctl
|= KVM_REG_MIPS_COUNT_CTL_DC
;
386 err
= kvm_mips_put_one_reg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
388 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__
, err
);
394 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_CAUSE
, &env
->CP0_Cause
);
396 DPRINTF("%s: Failed to put CP0_CAUSE (%d)\n", __func__
, err
);
401 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_COUNT
, &env
->CP0_Count
);
403 DPRINTF("%s: Failed to put CP0_COUNT (%d)\n", __func__
, err
);
407 /* resume KVM timer */
409 count_ctl
&= ~KVM_REG_MIPS_COUNT_CTL_DC
;
410 err
= kvm_mips_put_one_reg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
412 DPRINTF("%s: Failed to set COUNT_CTL.DC=0 (%d)\n", __func__
, err
);
421 * Handle the VM clock being started or stopped
423 static void kvm_mips_update_state(void *opaque
, int running
, RunState state
)
425 CPUState
*cs
= opaque
;
427 uint64_t count_resume
;
430 * If state is already dirty (synced to QEMU) then the KVM timer state is
431 * already saved and can be restored when it is synced back to KVM.
434 if (!cs
->kvm_vcpu_dirty
) {
435 ret
= kvm_mips_save_count(cs
);
437 fprintf(stderr
, "Failed saving count\n");
441 /* Set clock restore time to now */
442 count_resume
= get_clock();
443 ret
= kvm_mips_put_one_reg64(cs
, KVM_REG_MIPS_COUNT_RESUME
,
446 fprintf(stderr
, "Failed setting COUNT_RESUME\n");
450 if (!cs
->kvm_vcpu_dirty
) {
451 ret
= kvm_mips_restore_count(cs
);
453 fprintf(stderr
, "Failed restoring count\n");
459 static int kvm_mips_put_cp0_registers(CPUState
*cs
, int level
)
461 MIPSCPU
*cpu
= MIPS_CPU(cs
);
462 CPUMIPSState
*env
= &cpu
->env
;
467 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_INDEX
, &env
->CP0_Index
);
469 DPRINTF("%s: Failed to put CP0_INDEX (%d)\n", __func__
, err
);
472 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_CONTEXT
,
475 DPRINTF("%s: Failed to put CP0_CONTEXT (%d)\n", __func__
, err
);
478 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_USERLOCAL
,
479 &env
->active_tc
.CP0_UserLocal
);
481 DPRINTF("%s: Failed to put CP0_USERLOCAL (%d)\n", __func__
, err
);
484 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEMASK
,
487 DPRINTF("%s: Failed to put CP0_PAGEMASK (%d)\n", __func__
, err
);
490 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_WIRED
, &env
->CP0_Wired
);
492 DPRINTF("%s: Failed to put CP0_WIRED (%d)\n", __func__
, err
);
495 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_HWRENA
, &env
->CP0_HWREna
);
497 DPRINTF("%s: Failed to put CP0_HWRENA (%d)\n", __func__
, err
);
500 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_BADVADDR
,
503 DPRINTF("%s: Failed to put CP0_BADVADDR (%d)\n", __func__
, err
);
507 /* If VM clock stopped then state will be restored when it is restarted */
508 if (runstate_is_running()) {
509 err
= kvm_mips_restore_count(cs
);
515 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_ENTRYHI
,
518 DPRINTF("%s: Failed to put CP0_ENTRYHI (%d)\n", __func__
, err
);
521 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_COMPARE
,
524 DPRINTF("%s: Failed to put CP0_COMPARE (%d)\n", __func__
, err
);
527 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_STATUS
, &env
->CP0_Status
);
529 DPRINTF("%s: Failed to put CP0_STATUS (%d)\n", __func__
, err
);
532 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_EPC
, &env
->CP0_EPC
);
534 DPRINTF("%s: Failed to put CP0_EPC (%d)\n", __func__
, err
);
537 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_ERROREPC
,
540 DPRINTF("%s: Failed to put CP0_ERROREPC (%d)\n", __func__
, err
);
547 static int kvm_mips_get_cp0_registers(CPUState
*cs
)
549 MIPSCPU
*cpu
= MIPS_CPU(cs
);
550 CPUMIPSState
*env
= &cpu
->env
;
553 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_INDEX
, &env
->CP0_Index
);
555 DPRINTF("%s: Failed to get CP0_INDEX (%d)\n", __func__
, err
);
558 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_CONTEXT
,
561 DPRINTF("%s: Failed to get CP0_CONTEXT (%d)\n", __func__
, err
);
564 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_USERLOCAL
,
565 &env
->active_tc
.CP0_UserLocal
);
567 DPRINTF("%s: Failed to get CP0_USERLOCAL (%d)\n", __func__
, err
);
570 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEMASK
,
573 DPRINTF("%s: Failed to get CP0_PAGEMASK (%d)\n", __func__
, err
);
576 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_WIRED
, &env
->CP0_Wired
);
578 DPRINTF("%s: Failed to get CP0_WIRED (%d)\n", __func__
, err
);
581 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_HWRENA
, &env
->CP0_HWREna
);
583 DPRINTF("%s: Failed to get CP0_HWRENA (%d)\n", __func__
, err
);
586 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_BADVADDR
,
589 DPRINTF("%s: Failed to get CP0_BADVADDR (%d)\n", __func__
, err
);
592 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_ENTRYHI
,
595 DPRINTF("%s: Failed to get CP0_ENTRYHI (%d)\n", __func__
, err
);
598 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_COMPARE
,
601 DPRINTF("%s: Failed to get CP0_COMPARE (%d)\n", __func__
, err
);
604 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_STATUS
, &env
->CP0_Status
);
606 DPRINTF("%s: Failed to get CP0_STATUS (%d)\n", __func__
, err
);
610 /* If VM clock stopped then state was already saved when it was stopped */
611 if (runstate_is_running()) {
612 err
= kvm_mips_save_count(cs
);
618 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_EPC
, &env
->CP0_EPC
);
620 DPRINTF("%s: Failed to get CP0_EPC (%d)\n", __func__
, err
);
623 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_ERROREPC
,
626 DPRINTF("%s: Failed to get CP0_ERROREPC (%d)\n", __func__
, err
);
633 int kvm_arch_put_registers(CPUState
*cs
, int level
)
635 MIPSCPU
*cpu
= MIPS_CPU(cs
);
636 CPUMIPSState
*env
= &cpu
->env
;
637 struct kvm_regs regs
;
641 /* Set the registers based on QEMU's view of things */
642 for (i
= 0; i
< 32; i
++) {
643 regs
.gpr
[i
] = env
->active_tc
.gpr
[i
];
646 regs
.hi
= env
->active_tc
.HI
[0];
647 regs
.lo
= env
->active_tc
.LO
[0];
648 regs
.pc
= env
->active_tc
.PC
;
650 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_REGS
, ®s
);
656 ret
= kvm_mips_put_cp0_registers(cs
, level
);
664 int kvm_arch_get_registers(CPUState
*cs
)
666 MIPSCPU
*cpu
= MIPS_CPU(cs
);
667 CPUMIPSState
*env
= &cpu
->env
;
669 struct kvm_regs regs
;
672 /* Get the current register set as KVM seems it */
673 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_REGS
, ®s
);
679 for (i
= 0; i
< 32; i
++) {
680 env
->active_tc
.gpr
[i
] = regs
.gpr
[i
];
683 env
->active_tc
.HI
[0] = regs
.hi
;
684 env
->active_tc
.LO
[0] = regs
.lo
;
685 env
->active_tc
.PC
= regs
.pc
;
687 kvm_mips_get_cp0_registers(cs
);