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 "qemu/osdep.h"
13 #include <sys/ioctl.h>
15 #include <linux/kvm.h>
17 #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"
23 #include "sysemu/cpus.h"
25 #include "exec/memattrs.h"
29 #define DPRINTF(fmt, ...) \
30 do { if (DEBUG_KVM) { fprintf(stderr, fmt, ## __VA_ARGS__); } } while (0)
32 static int kvm_mips_fpu_cap
;
33 static int kvm_mips_msa_cap
;
35 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
39 static void kvm_mips_update_state(void *opaque
, int running
, RunState state
);
41 unsigned long kvm_arch_vcpu_id(CPUState
*cs
)
46 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
48 /* MIPS has 128 signals */
49 kvm_set_sigmask_len(s
, 16);
51 kvm_mips_fpu_cap
= kvm_check_extension(s
, KVM_CAP_MIPS_FPU
);
52 kvm_mips_msa_cap
= kvm_check_extension(s
, KVM_CAP_MIPS_MSA
);
54 DPRINTF("%s\n", __func__
);
58 int kvm_arch_init_vcpu(CPUState
*cs
)
60 MIPSCPU
*cpu
= MIPS_CPU(cs
);
61 CPUMIPSState
*env
= &cpu
->env
;
64 qemu_add_vm_change_state_handler(kvm_mips_update_state
, cs
);
66 if (kvm_mips_fpu_cap
&& env
->CP0_Config1
& (1 << CP0C1_FP
)) {
67 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_MIPS_FPU
, 0, 0);
69 /* mark unsupported so it gets disabled on reset */
75 if (kvm_mips_msa_cap
&& env
->CP0_Config3
& (1 << CP0C3_MSAP
)) {
76 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_MIPS_MSA
, 0, 0);
78 /* mark unsupported so it gets disabled on reset */
84 DPRINTF("%s\n", __func__
);
88 void kvm_mips_reset_vcpu(MIPSCPU
*cpu
)
90 CPUMIPSState
*env
= &cpu
->env
;
92 if (!kvm_mips_fpu_cap
&& env
->CP0_Config1
& (1 << CP0C1_FP
)) {
93 fprintf(stderr
, "Warning: KVM does not support FPU, disabling\n");
94 env
->CP0_Config1
&= ~(1 << CP0C1_FP
);
96 if (!kvm_mips_msa_cap
&& env
->CP0_Config3
& (1 << CP0C3_MSAP
)) {
97 fprintf(stderr
, "Warning: KVM does not support MSA, disabling\n");
98 env
->CP0_Config3
&= ~(1 << CP0C3_MSAP
);
101 DPRINTF("%s\n", __func__
);
104 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
106 DPRINTF("%s\n", __func__
);
110 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
112 DPRINTF("%s\n", __func__
);
116 static inline int cpu_mips_io_interrupts_pending(MIPSCPU
*cpu
)
118 CPUMIPSState
*env
= &cpu
->env
;
120 return env
->CP0_Cause
& (0x1 << (2 + CP0Ca_IP
));
124 void kvm_arch_pre_run(CPUState
*cs
, struct kvm_run
*run
)
126 MIPSCPU
*cpu
= MIPS_CPU(cs
);
128 struct kvm_mips_interrupt intr
;
130 qemu_mutex_lock_iothread();
132 if ((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
133 cpu_mips_io_interrupts_pending(cpu
)) {
136 r
= kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
138 error_report("%s: cpu %d: failed to inject IRQ %x",
139 __func__
, cs
->cpu_index
, intr
.irq
);
143 qemu_mutex_unlock_iothread();
146 MemTxAttrs
kvm_arch_post_run(CPUState
*cs
, struct kvm_run
*run
)
148 return MEMTXATTRS_UNSPECIFIED
;
151 int kvm_arch_process_async_events(CPUState
*cs
)
156 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
160 DPRINTF("%s\n", __func__
);
161 switch (run
->exit_reason
) {
163 error_report("%s: unknown exit reason %d",
164 __func__
, run
->exit_reason
);
172 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
174 DPRINTF("%s\n", __func__
);
178 int kvm_arch_on_sigbus_vcpu(CPUState
*cs
, int code
, void *addr
)
180 DPRINTF("%s\n", __func__
);
184 int kvm_arch_on_sigbus(int code
, void *addr
)
186 DPRINTF("%s\n", __func__
);
190 void kvm_arch_init_irq_routing(KVMState
*s
)
194 int kvm_mips_set_interrupt(MIPSCPU
*cpu
, int irq
, int level
)
196 CPUState
*cs
= CPU(cpu
);
197 struct kvm_mips_interrupt intr
;
199 if (!kvm_enabled()) {
211 kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
216 int kvm_mips_set_ipi_interrupt(MIPSCPU
*cpu
, int irq
, int level
)
218 CPUState
*cs
= current_cpu
;
219 CPUState
*dest_cs
= CPU(cpu
);
220 struct kvm_mips_interrupt intr
;
222 if (!kvm_enabled()) {
226 intr
.cpu
= dest_cs
->cpu_index
;
234 DPRINTF("%s: CPU %d, IRQ: %d\n", __func__
, intr
.cpu
, intr
.irq
);
236 kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
241 #define MIPS_CP0_32(_R, _S) \
242 (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U32 | (8 * (_R) + (_S)))
244 #define MIPS_CP0_64(_R, _S) \
245 (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U64 | (8 * (_R) + (_S)))
247 #define KVM_REG_MIPS_CP0_INDEX MIPS_CP0_32(0, 0)
248 #define KVM_REG_MIPS_CP0_CONTEXT MIPS_CP0_64(4, 0)
249 #define KVM_REG_MIPS_CP0_USERLOCAL MIPS_CP0_64(4, 2)
250 #define KVM_REG_MIPS_CP0_PAGEMASK MIPS_CP0_32(5, 0)
251 #define KVM_REG_MIPS_CP0_WIRED MIPS_CP0_32(6, 0)
252 #define KVM_REG_MIPS_CP0_HWRENA MIPS_CP0_32(7, 0)
253 #define KVM_REG_MIPS_CP0_BADVADDR MIPS_CP0_64(8, 0)
254 #define KVM_REG_MIPS_CP0_COUNT MIPS_CP0_32(9, 0)
255 #define KVM_REG_MIPS_CP0_ENTRYHI MIPS_CP0_64(10, 0)
256 #define KVM_REG_MIPS_CP0_COMPARE MIPS_CP0_32(11, 0)
257 #define KVM_REG_MIPS_CP0_STATUS MIPS_CP0_32(12, 0)
258 #define KVM_REG_MIPS_CP0_CAUSE MIPS_CP0_32(13, 0)
259 #define KVM_REG_MIPS_CP0_EPC MIPS_CP0_64(14, 0)
260 #define KVM_REG_MIPS_CP0_PRID MIPS_CP0_32(15, 0)
261 #define KVM_REG_MIPS_CP0_CONFIG MIPS_CP0_32(16, 0)
262 #define KVM_REG_MIPS_CP0_CONFIG1 MIPS_CP0_32(16, 1)
263 #define KVM_REG_MIPS_CP0_CONFIG2 MIPS_CP0_32(16, 2)
264 #define KVM_REG_MIPS_CP0_CONFIG3 MIPS_CP0_32(16, 3)
265 #define KVM_REG_MIPS_CP0_CONFIG4 MIPS_CP0_32(16, 4)
266 #define KVM_REG_MIPS_CP0_CONFIG5 MIPS_CP0_32(16, 5)
267 #define KVM_REG_MIPS_CP0_ERROREPC MIPS_CP0_64(30, 0)
269 static inline int kvm_mips_put_one_reg(CPUState
*cs
, uint64_t reg_id
,
272 struct kvm_one_reg cp0reg
= {
274 .addr
= (uintptr_t)addr
277 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
280 static inline int kvm_mips_put_one_ureg(CPUState
*cs
, uint64_t reg_id
,
283 struct kvm_one_reg cp0reg
= {
285 .addr
= (uintptr_t)addr
288 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
291 static inline int kvm_mips_put_one_ulreg(CPUState
*cs
, uint64_t reg_id
,
294 uint64_t val64
= *addr
;
295 struct kvm_one_reg cp0reg
= {
297 .addr
= (uintptr_t)&val64
300 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
303 static inline int kvm_mips_put_one_reg64(CPUState
*cs
, uint64_t reg_id
,
306 struct kvm_one_reg cp0reg
= {
308 .addr
= (uintptr_t)addr
311 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
314 static inline int kvm_mips_put_one_ureg64(CPUState
*cs
, uint64_t reg_id
,
317 struct kvm_one_reg cp0reg
= {
319 .addr
= (uintptr_t)addr
322 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
325 static inline int kvm_mips_get_one_reg(CPUState
*cs
, uint64_t reg_id
,
328 struct kvm_one_reg cp0reg
= {
330 .addr
= (uintptr_t)addr
333 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
336 static inline int kvm_mips_get_one_ureg(CPUState
*cs
, uint64_t reg_id
,
339 struct kvm_one_reg cp0reg
= {
341 .addr
= (uintptr_t)addr
344 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
347 static inline int kvm_mips_get_one_ulreg(CPUState
*cs
, uint64_t reg_id
,
352 struct kvm_one_reg cp0reg
= {
354 .addr
= (uintptr_t)&val64
357 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
364 static inline int kvm_mips_get_one_reg64(CPUState
*cs
, uint64_t reg_id
,
367 struct kvm_one_reg cp0reg
= {
369 .addr
= (uintptr_t)addr
372 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
375 static inline int kvm_mips_get_one_ureg64(CPUState
*cs
, uint64_t reg_id
,
378 struct kvm_one_reg cp0reg
= {
380 .addr
= (uintptr_t)addr
383 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
386 #define KVM_REG_MIPS_CP0_CONFIG_MASK (1U << CP0C0_M)
387 #define KVM_REG_MIPS_CP0_CONFIG1_MASK ((1U << CP0C1_M) | \
389 #define KVM_REG_MIPS_CP0_CONFIG2_MASK (1U << CP0C2_M)
390 #define KVM_REG_MIPS_CP0_CONFIG3_MASK ((1U << CP0C3_M) | \
392 #define KVM_REG_MIPS_CP0_CONFIG4_MASK (1U << CP0C4_M)
393 #define KVM_REG_MIPS_CP0_CONFIG5_MASK ((1U << CP0C5_MSAEn) | \
394 (1U << CP0C5_UFE) | \
395 (1U << CP0C5_FRE) | \
398 static inline int kvm_mips_change_one_reg(CPUState
*cs
, uint64_t reg_id
,
399 int32_t *addr
, int32_t mask
)
404 err
= kvm_mips_get_one_reg(cs
, reg_id
, &tmp
);
409 /* only change bits in mask */
410 change
= (*addr
^ tmp
) & mask
;
416 return kvm_mips_put_one_reg(cs
, reg_id
, &tmp
);
420 * We freeze the KVM timer when either the VM clock is stopped or the state is
421 * saved (the state is dirty).
425 * Save the state of the KVM timer when VM clock is stopped or state is synced
428 static int kvm_mips_save_count(CPUState
*cs
)
430 MIPSCPU
*cpu
= MIPS_CPU(cs
);
431 CPUMIPSState
*env
= &cpu
->env
;
435 /* freeze KVM timer */
436 err
= kvm_mips_get_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
438 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__
, err
);
440 } else if (!(count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
)) {
441 count_ctl
|= KVM_REG_MIPS_COUNT_CTL_DC
;
442 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
444 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__
, err
);
450 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CAUSE
, &env
->CP0_Cause
);
452 DPRINTF("%s: Failed to get CP0_CAUSE (%d)\n", __func__
, err
);
457 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_COUNT
, &env
->CP0_Count
);
459 DPRINTF("%s: Failed to get CP0_COUNT (%d)\n", __func__
, err
);
467 * Restore the state of the KVM timer when VM clock is restarted or state is
470 static int kvm_mips_restore_count(CPUState
*cs
)
472 MIPSCPU
*cpu
= MIPS_CPU(cs
);
473 CPUMIPSState
*env
= &cpu
->env
;
475 int err_dc
, err
, ret
= 0;
477 /* check the timer is frozen */
478 err_dc
= kvm_mips_get_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
480 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__
, err_dc
);
482 } else if (!(count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
)) {
483 /* freeze timer (sets COUNT_RESUME for us) */
484 count_ctl
|= KVM_REG_MIPS_COUNT_CTL_DC
;
485 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
487 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__
, err
);
493 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_CAUSE
, &env
->CP0_Cause
);
495 DPRINTF("%s: Failed to put CP0_CAUSE (%d)\n", __func__
, err
);
500 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_COUNT
, &env
->CP0_Count
);
502 DPRINTF("%s: Failed to put CP0_COUNT (%d)\n", __func__
, err
);
506 /* resume KVM timer */
508 count_ctl
&= ~KVM_REG_MIPS_COUNT_CTL_DC
;
509 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
511 DPRINTF("%s: Failed to set COUNT_CTL.DC=0 (%d)\n", __func__
, err
);
520 * Handle the VM clock being started or stopped
522 static void kvm_mips_update_state(void *opaque
, int running
, RunState state
)
524 CPUState
*cs
= opaque
;
526 uint64_t count_resume
;
529 * If state is already dirty (synced to QEMU) then the KVM timer state is
530 * already saved and can be restored when it is synced back to KVM.
533 if (!cs
->kvm_vcpu_dirty
) {
534 ret
= kvm_mips_save_count(cs
);
536 fprintf(stderr
, "Failed saving count\n");
540 /* Set clock restore time to now */
541 count_resume
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
542 ret
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_RESUME
,
545 fprintf(stderr
, "Failed setting COUNT_RESUME\n");
549 if (!cs
->kvm_vcpu_dirty
) {
550 ret
= kvm_mips_restore_count(cs
);
552 fprintf(stderr
, "Failed restoring count\n");
558 static int kvm_mips_put_fpu_registers(CPUState
*cs
, int level
)
560 MIPSCPU
*cpu
= MIPS_CPU(cs
);
561 CPUMIPSState
*env
= &cpu
->env
;
565 /* Only put FPU state if we're emulating a CPU with an FPU */
566 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
567 /* FPU Control Registers */
568 if (level
== KVM_PUT_FULL_STATE
) {
569 err
= kvm_mips_put_one_ureg(cs
, KVM_REG_MIPS_FCR_IR
,
570 &env
->active_fpu
.fcr0
);
572 DPRINTF("%s: Failed to put FCR_IR (%d)\n", __func__
, err
);
576 err
= kvm_mips_put_one_ureg(cs
, KVM_REG_MIPS_FCR_CSR
,
577 &env
->active_fpu
.fcr31
);
579 DPRINTF("%s: Failed to put FCR_CSR (%d)\n", __func__
, err
);
584 * FPU register state is a subset of MSA vector state, so don't put FPU
585 * registers if we're emulating a CPU with MSA.
587 if (!(env
->CP0_Config3
& (1 << CP0C3_MSAP
))) {
588 /* Floating point registers */
589 for (i
= 0; i
< 32; ++i
) {
590 if (env
->CP0_Status
& (1 << CP0St_FR
)) {
591 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_FPR_64(i
),
592 &env
->active_fpu
.fpr
[i
].d
);
594 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FPR_32(i
),
595 &env
->active_fpu
.fpr
[i
].w
[FP_ENDIAN_IDX
]);
598 DPRINTF("%s: Failed to put FPR%u (%d)\n", __func__
, i
, err
);
605 /* Only put MSA state if we're emulating a CPU with MSA */
606 if (env
->CP0_Config3
& (1 << CP0C3_MSAP
)) {
607 /* MSA Control Registers */
608 if (level
== KVM_PUT_FULL_STATE
) {
609 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_MSA_IR
,
612 DPRINTF("%s: Failed to put MSA_IR (%d)\n", __func__
, err
);
616 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_MSA_CSR
,
617 &env
->active_tc
.msacsr
);
619 DPRINTF("%s: Failed to put MSA_CSR (%d)\n", __func__
, err
);
623 /* Vector registers (includes FP registers) */
624 for (i
= 0; i
< 32; ++i
) {
625 /* Big endian MSA not supported by QEMU yet anyway */
626 err
= kvm_mips_put_one_reg64(cs
, KVM_REG_MIPS_VEC_128(i
),
627 env
->active_fpu
.fpr
[i
].wr
.d
);
629 DPRINTF("%s: Failed to put VEC%u (%d)\n", __func__
, i
, err
);
638 static int kvm_mips_get_fpu_registers(CPUState
*cs
)
640 MIPSCPU
*cpu
= MIPS_CPU(cs
);
641 CPUMIPSState
*env
= &cpu
->env
;
645 /* Only get FPU state if we're emulating a CPU with an FPU */
646 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
647 /* FPU Control Registers */
648 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FCR_IR
,
649 &env
->active_fpu
.fcr0
);
651 DPRINTF("%s: Failed to get FCR_IR (%d)\n", __func__
, err
);
654 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FCR_CSR
,
655 &env
->active_fpu
.fcr31
);
657 DPRINTF("%s: Failed to get FCR_CSR (%d)\n", __func__
, err
);
660 restore_fp_status(env
);
664 * FPU register state is a subset of MSA vector state, so don't save FPU
665 * registers if we're emulating a CPU with MSA.
667 if (!(env
->CP0_Config3
& (1 << CP0C3_MSAP
))) {
668 /* Floating point registers */
669 for (i
= 0; i
< 32; ++i
) {
670 if (env
->CP0_Status
& (1 << CP0St_FR
)) {
671 err
= kvm_mips_get_one_ureg64(cs
, KVM_REG_MIPS_FPR_64(i
),
672 &env
->active_fpu
.fpr
[i
].d
);
674 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FPR_32(i
),
675 &env
->active_fpu
.fpr
[i
].w
[FP_ENDIAN_IDX
]);
678 DPRINTF("%s: Failed to get FPR%u (%d)\n", __func__
, i
, err
);
685 /* Only get MSA state if we're emulating a CPU with MSA */
686 if (env
->CP0_Config3
& (1 << CP0C3_MSAP
)) {
687 /* MSA Control Registers */
688 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_MSA_IR
,
691 DPRINTF("%s: Failed to get MSA_IR (%d)\n", __func__
, err
);
694 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_MSA_CSR
,
695 &env
->active_tc
.msacsr
);
697 DPRINTF("%s: Failed to get MSA_CSR (%d)\n", __func__
, err
);
700 restore_msa_fp_status(env
);
703 /* Vector registers (includes FP registers) */
704 for (i
= 0; i
< 32; ++i
) {
705 /* Big endian MSA not supported by QEMU yet anyway */
706 err
= kvm_mips_get_one_reg64(cs
, KVM_REG_MIPS_VEC_128(i
),
707 env
->active_fpu
.fpr
[i
].wr
.d
);
709 DPRINTF("%s: Failed to get VEC%u (%d)\n", __func__
, i
, err
);
719 static int kvm_mips_put_cp0_registers(CPUState
*cs
, int level
)
721 MIPSCPU
*cpu
= MIPS_CPU(cs
);
722 CPUMIPSState
*env
= &cpu
->env
;
727 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_INDEX
, &env
->CP0_Index
);
729 DPRINTF("%s: Failed to put CP0_INDEX (%d)\n", __func__
, err
);
732 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_CONTEXT
,
735 DPRINTF("%s: Failed to put CP0_CONTEXT (%d)\n", __func__
, err
);
738 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_USERLOCAL
,
739 &env
->active_tc
.CP0_UserLocal
);
741 DPRINTF("%s: Failed to put CP0_USERLOCAL (%d)\n", __func__
, err
);
744 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEMASK
,
747 DPRINTF("%s: Failed to put CP0_PAGEMASK (%d)\n", __func__
, err
);
750 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_WIRED
, &env
->CP0_Wired
);
752 DPRINTF("%s: Failed to put CP0_WIRED (%d)\n", __func__
, err
);
755 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_HWRENA
, &env
->CP0_HWREna
);
757 DPRINTF("%s: Failed to put CP0_HWRENA (%d)\n", __func__
, err
);
760 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_BADVADDR
,
763 DPRINTF("%s: Failed to put CP0_BADVADDR (%d)\n", __func__
, err
);
767 /* If VM clock stopped then state will be restored when it is restarted */
768 if (runstate_is_running()) {
769 err
= kvm_mips_restore_count(cs
);
775 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_ENTRYHI
,
778 DPRINTF("%s: Failed to put CP0_ENTRYHI (%d)\n", __func__
, err
);
781 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_COMPARE
,
784 DPRINTF("%s: Failed to put CP0_COMPARE (%d)\n", __func__
, err
);
787 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_STATUS
, &env
->CP0_Status
);
789 DPRINTF("%s: Failed to put CP0_STATUS (%d)\n", __func__
, err
);
792 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_EPC
, &env
->CP0_EPC
);
794 DPRINTF("%s: Failed to put CP0_EPC (%d)\n", __func__
, err
);
797 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_PRID
, &env
->CP0_PRid
);
799 DPRINTF("%s: Failed to put CP0_PRID (%d)\n", __func__
, err
);
802 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG
,
804 KVM_REG_MIPS_CP0_CONFIG_MASK
);
806 DPRINTF("%s: Failed to change CP0_CONFIG (%d)\n", __func__
, err
);
809 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG1
,
811 KVM_REG_MIPS_CP0_CONFIG1_MASK
);
813 DPRINTF("%s: Failed to change CP0_CONFIG1 (%d)\n", __func__
, err
);
816 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG2
,
818 KVM_REG_MIPS_CP0_CONFIG2_MASK
);
820 DPRINTF("%s: Failed to change CP0_CONFIG2 (%d)\n", __func__
, err
);
823 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG3
,
825 KVM_REG_MIPS_CP0_CONFIG3_MASK
);
827 DPRINTF("%s: Failed to change CP0_CONFIG3 (%d)\n", __func__
, err
);
830 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG4
,
832 KVM_REG_MIPS_CP0_CONFIG4_MASK
);
834 DPRINTF("%s: Failed to change CP0_CONFIG4 (%d)\n", __func__
, err
);
837 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG5
,
839 KVM_REG_MIPS_CP0_CONFIG5_MASK
);
841 DPRINTF("%s: Failed to change CP0_CONFIG5 (%d)\n", __func__
, err
);
844 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_ERROREPC
,
847 DPRINTF("%s: Failed to put CP0_ERROREPC (%d)\n", __func__
, err
);
854 static int kvm_mips_get_cp0_registers(CPUState
*cs
)
856 MIPSCPU
*cpu
= MIPS_CPU(cs
);
857 CPUMIPSState
*env
= &cpu
->env
;
860 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_INDEX
, &env
->CP0_Index
);
862 DPRINTF("%s: Failed to get CP0_INDEX (%d)\n", __func__
, err
);
865 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_CONTEXT
,
868 DPRINTF("%s: Failed to get CP0_CONTEXT (%d)\n", __func__
, err
);
871 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_USERLOCAL
,
872 &env
->active_tc
.CP0_UserLocal
);
874 DPRINTF("%s: Failed to get CP0_USERLOCAL (%d)\n", __func__
, err
);
877 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEMASK
,
880 DPRINTF("%s: Failed to get CP0_PAGEMASK (%d)\n", __func__
, err
);
883 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_WIRED
, &env
->CP0_Wired
);
885 DPRINTF("%s: Failed to get CP0_WIRED (%d)\n", __func__
, err
);
888 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_HWRENA
, &env
->CP0_HWREna
);
890 DPRINTF("%s: Failed to get CP0_HWRENA (%d)\n", __func__
, err
);
893 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_BADVADDR
,
896 DPRINTF("%s: Failed to get CP0_BADVADDR (%d)\n", __func__
, err
);
899 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_ENTRYHI
,
902 DPRINTF("%s: Failed to get CP0_ENTRYHI (%d)\n", __func__
, err
);
905 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_COMPARE
,
908 DPRINTF("%s: Failed to get CP0_COMPARE (%d)\n", __func__
, err
);
911 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_STATUS
, &env
->CP0_Status
);
913 DPRINTF("%s: Failed to get CP0_STATUS (%d)\n", __func__
, err
);
917 /* If VM clock stopped then state was already saved when it was stopped */
918 if (runstate_is_running()) {
919 err
= kvm_mips_save_count(cs
);
925 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_EPC
, &env
->CP0_EPC
);
927 DPRINTF("%s: Failed to get CP0_EPC (%d)\n", __func__
, err
);
930 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_PRID
, &env
->CP0_PRid
);
932 DPRINTF("%s: Failed to get CP0_PRID (%d)\n", __func__
, err
);
935 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG
, &env
->CP0_Config0
);
937 DPRINTF("%s: Failed to get CP0_CONFIG (%d)\n", __func__
, err
);
940 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG1
, &env
->CP0_Config1
);
942 DPRINTF("%s: Failed to get CP0_CONFIG1 (%d)\n", __func__
, err
);
945 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG2
, &env
->CP0_Config2
);
947 DPRINTF("%s: Failed to get CP0_CONFIG2 (%d)\n", __func__
, err
);
950 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG3
, &env
->CP0_Config3
);
952 DPRINTF("%s: Failed to get CP0_CONFIG3 (%d)\n", __func__
, err
);
955 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG4
, &env
->CP0_Config4
);
957 DPRINTF("%s: Failed to get CP0_CONFIG4 (%d)\n", __func__
, err
);
960 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG5
, &env
->CP0_Config5
);
962 DPRINTF("%s: Failed to get CP0_CONFIG5 (%d)\n", __func__
, err
);
965 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_ERROREPC
,
968 DPRINTF("%s: Failed to get CP0_ERROREPC (%d)\n", __func__
, err
);
975 int kvm_arch_put_registers(CPUState
*cs
, int level
)
977 MIPSCPU
*cpu
= MIPS_CPU(cs
);
978 CPUMIPSState
*env
= &cpu
->env
;
979 struct kvm_regs regs
;
983 /* Set the registers based on QEMU's view of things */
984 for (i
= 0; i
< 32; i
++) {
985 regs
.gpr
[i
] = (int64_t)(target_long
)env
->active_tc
.gpr
[i
];
988 regs
.hi
= (int64_t)(target_long
)env
->active_tc
.HI
[0];
989 regs
.lo
= (int64_t)(target_long
)env
->active_tc
.LO
[0];
990 regs
.pc
= (int64_t)(target_long
)env
->active_tc
.PC
;
992 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_REGS
, ®s
);
998 ret
= kvm_mips_put_cp0_registers(cs
, level
);
1003 ret
= kvm_mips_put_fpu_registers(cs
, level
);
1011 int kvm_arch_get_registers(CPUState
*cs
)
1013 MIPSCPU
*cpu
= MIPS_CPU(cs
);
1014 CPUMIPSState
*env
= &cpu
->env
;
1016 struct kvm_regs regs
;
1019 /* Get the current register set as KVM seems it */
1020 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_REGS
, ®s
);
1026 for (i
= 0; i
< 32; i
++) {
1027 env
->active_tc
.gpr
[i
] = regs
.gpr
[i
];
1030 env
->active_tc
.HI
[0] = regs
.hi
;
1031 env
->active_tc
.LO
[0] = regs
.lo
;
1032 env
->active_tc
.PC
= regs
.pc
;
1034 kvm_mips_get_cp0_registers(cs
);
1035 kvm_mips_get_fpu_registers(cs
);
1040 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
1041 uint64_t address
, uint32_t data
, PCIDevice
*dev
)
1046 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry
*route
,
1047 int vector
, PCIDevice
*dev
)
1052 int kvm_arch_release_virq_post(int virq
)
1057 int kvm_arch_msi_data_to_gsi(uint32_t data
)