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"
20 #include "qemu/error-report.h"
21 #include "qemu/timer.h"
22 #include "sysemu/sysemu.h"
23 #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 static int kvm_mips_fpu_cap
;
34 static int kvm_mips_msa_cap
;
36 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
40 static void kvm_mips_update_state(void *opaque
, int running
, RunState state
);
42 unsigned long kvm_arch_vcpu_id(CPUState
*cs
)
47 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
49 /* MIPS has 128 signals */
50 kvm_set_sigmask_len(s
, 16);
52 kvm_mips_fpu_cap
= kvm_check_extension(s
, KVM_CAP_MIPS_FPU
);
53 kvm_mips_msa_cap
= kvm_check_extension(s
, KVM_CAP_MIPS_MSA
);
55 DPRINTF("%s\n", __func__
);
59 int kvm_arch_irqchip_create(MachineState
*ms
, KVMState
*s
)
64 int kvm_arch_init_vcpu(CPUState
*cs
)
66 MIPSCPU
*cpu
= MIPS_CPU(cs
);
67 CPUMIPSState
*env
= &cpu
->env
;
70 qemu_add_vm_change_state_handler(kvm_mips_update_state
, cs
);
72 if (kvm_mips_fpu_cap
&& env
->CP0_Config1
& (1 << CP0C1_FP
)) {
73 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_MIPS_FPU
, 0, 0);
75 /* mark unsupported so it gets disabled on reset */
81 if (kvm_mips_msa_cap
&& env
->CP0_Config3
& (1 << CP0C3_MSAP
)) {
82 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_MIPS_MSA
, 0, 0);
84 /* mark unsupported so it gets disabled on reset */
90 DPRINTF("%s\n", __func__
);
94 void kvm_mips_reset_vcpu(MIPSCPU
*cpu
)
96 CPUMIPSState
*env
= &cpu
->env
;
98 if (!kvm_mips_fpu_cap
&& env
->CP0_Config1
& (1 << CP0C1_FP
)) {
99 warn_report("KVM does not support FPU, disabling");
100 env
->CP0_Config1
&= ~(1 << CP0C1_FP
);
102 if (!kvm_mips_msa_cap
&& env
->CP0_Config3
& (1 << CP0C3_MSAP
)) {
103 warn_report("KVM does not support MSA, disabling");
104 env
->CP0_Config3
&= ~(1 << CP0C3_MSAP
);
107 DPRINTF("%s\n", __func__
);
110 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
112 DPRINTF("%s\n", __func__
);
116 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
118 DPRINTF("%s\n", __func__
);
122 static inline int cpu_mips_io_interrupts_pending(MIPSCPU
*cpu
)
124 CPUMIPSState
*env
= &cpu
->env
;
126 return env
->CP0_Cause
& (0x1 << (2 + CP0Ca_IP
));
130 void kvm_arch_pre_run(CPUState
*cs
, struct kvm_run
*run
)
132 MIPSCPU
*cpu
= MIPS_CPU(cs
);
134 struct kvm_mips_interrupt intr
;
136 qemu_mutex_lock_iothread();
138 if ((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
139 cpu_mips_io_interrupts_pending(cpu
)) {
142 r
= kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
144 error_report("%s: cpu %d: failed to inject IRQ %x",
145 __func__
, cs
->cpu_index
, intr
.irq
);
149 qemu_mutex_unlock_iothread();
152 MemTxAttrs
kvm_arch_post_run(CPUState
*cs
, struct kvm_run
*run
)
154 return MEMTXATTRS_UNSPECIFIED
;
157 int kvm_arch_process_async_events(CPUState
*cs
)
162 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
166 DPRINTF("%s\n", __func__
);
167 switch (run
->exit_reason
) {
169 error_report("%s: unknown exit reason %d",
170 __func__
, run
->exit_reason
);
178 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
180 DPRINTF("%s\n", __func__
);
184 void kvm_arch_init_irq_routing(KVMState
*s
)
188 int kvm_mips_set_interrupt(MIPSCPU
*cpu
, int irq
, int level
)
190 CPUState
*cs
= CPU(cpu
);
191 struct kvm_mips_interrupt intr
;
193 if (!kvm_enabled()) {
205 kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
210 int kvm_mips_set_ipi_interrupt(MIPSCPU
*cpu
, int irq
, int level
)
212 CPUState
*cs
= current_cpu
;
213 CPUState
*dest_cs
= CPU(cpu
);
214 struct kvm_mips_interrupt intr
;
216 if (!kvm_enabled()) {
220 intr
.cpu
= dest_cs
->cpu_index
;
228 DPRINTF("%s: CPU %d, IRQ: %d\n", __func__
, intr
.cpu
, intr
.irq
);
230 kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
235 #define MIPS_CP0_32(_R, _S) \
236 (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U32 | (8 * (_R) + (_S)))
238 #define MIPS_CP0_64(_R, _S) \
239 (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U64 | (8 * (_R) + (_S)))
241 #define KVM_REG_MIPS_CP0_INDEX MIPS_CP0_32(0, 0)
242 #define KVM_REG_MIPS_CP0_CONTEXT MIPS_CP0_64(4, 0)
243 #define KVM_REG_MIPS_CP0_USERLOCAL MIPS_CP0_64(4, 2)
244 #define KVM_REG_MIPS_CP0_PAGEMASK MIPS_CP0_32(5, 0)
245 #define KVM_REG_MIPS_CP0_WIRED MIPS_CP0_32(6, 0)
246 #define KVM_REG_MIPS_CP0_HWRENA MIPS_CP0_32(7, 0)
247 #define KVM_REG_MIPS_CP0_BADVADDR MIPS_CP0_64(8, 0)
248 #define KVM_REG_MIPS_CP0_COUNT MIPS_CP0_32(9, 0)
249 #define KVM_REG_MIPS_CP0_ENTRYHI MIPS_CP0_64(10, 0)
250 #define KVM_REG_MIPS_CP0_COMPARE MIPS_CP0_32(11, 0)
251 #define KVM_REG_MIPS_CP0_STATUS MIPS_CP0_32(12, 0)
252 #define KVM_REG_MIPS_CP0_CAUSE MIPS_CP0_32(13, 0)
253 #define KVM_REG_MIPS_CP0_EPC MIPS_CP0_64(14, 0)
254 #define KVM_REG_MIPS_CP0_PRID MIPS_CP0_32(15, 0)
255 #define KVM_REG_MIPS_CP0_CONFIG MIPS_CP0_32(16, 0)
256 #define KVM_REG_MIPS_CP0_CONFIG1 MIPS_CP0_32(16, 1)
257 #define KVM_REG_MIPS_CP0_CONFIG2 MIPS_CP0_32(16, 2)
258 #define KVM_REG_MIPS_CP0_CONFIG3 MIPS_CP0_32(16, 3)
259 #define KVM_REG_MIPS_CP0_CONFIG4 MIPS_CP0_32(16, 4)
260 #define KVM_REG_MIPS_CP0_CONFIG5 MIPS_CP0_32(16, 5)
261 #define KVM_REG_MIPS_CP0_ERROREPC MIPS_CP0_64(30, 0)
263 static inline int kvm_mips_put_one_reg(CPUState
*cs
, uint64_t reg_id
,
266 struct kvm_one_reg cp0reg
= {
268 .addr
= (uintptr_t)addr
271 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
274 static inline int kvm_mips_put_one_ureg(CPUState
*cs
, uint64_t reg_id
,
277 struct kvm_one_reg cp0reg
= {
279 .addr
= (uintptr_t)addr
282 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
285 static inline int kvm_mips_put_one_ulreg(CPUState
*cs
, uint64_t reg_id
,
288 uint64_t val64
= *addr
;
289 struct kvm_one_reg cp0reg
= {
291 .addr
= (uintptr_t)&val64
294 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
297 static inline int kvm_mips_put_one_reg64(CPUState
*cs
, uint64_t reg_id
,
300 struct kvm_one_reg cp0reg
= {
302 .addr
= (uintptr_t)addr
305 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
308 static inline int kvm_mips_put_one_ureg64(CPUState
*cs
, uint64_t reg_id
,
311 struct kvm_one_reg cp0reg
= {
313 .addr
= (uintptr_t)addr
316 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
319 static inline int kvm_mips_get_one_reg(CPUState
*cs
, uint64_t reg_id
,
322 struct kvm_one_reg cp0reg
= {
324 .addr
= (uintptr_t)addr
327 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
330 static inline int kvm_mips_get_one_ureg(CPUState
*cs
, uint64_t reg_id
,
333 struct kvm_one_reg cp0reg
= {
335 .addr
= (uintptr_t)addr
338 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
341 static inline int kvm_mips_get_one_ulreg(CPUState
*cs
, uint64_t reg_id
,
346 struct kvm_one_reg cp0reg
= {
348 .addr
= (uintptr_t)&val64
351 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
358 static inline int kvm_mips_get_one_reg64(CPUState
*cs
, uint64_t reg_id
,
361 struct kvm_one_reg cp0reg
= {
363 .addr
= (uintptr_t)addr
366 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
369 static inline int kvm_mips_get_one_ureg64(CPUState
*cs
, uint64_t reg_id
,
372 struct kvm_one_reg cp0reg
= {
374 .addr
= (uintptr_t)addr
377 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
380 #define KVM_REG_MIPS_CP0_CONFIG_MASK (1U << CP0C0_M)
381 #define KVM_REG_MIPS_CP0_CONFIG1_MASK ((1U << CP0C1_M) | \
383 #define KVM_REG_MIPS_CP0_CONFIG2_MASK (1U << CP0C2_M)
384 #define KVM_REG_MIPS_CP0_CONFIG3_MASK ((1U << CP0C3_M) | \
386 #define KVM_REG_MIPS_CP0_CONFIG4_MASK (1U << CP0C4_M)
387 #define KVM_REG_MIPS_CP0_CONFIG5_MASK ((1U << CP0C5_MSAEn) | \
388 (1U << CP0C5_UFE) | \
389 (1U << CP0C5_FRE) | \
392 static inline int kvm_mips_change_one_reg(CPUState
*cs
, uint64_t reg_id
,
393 int32_t *addr
, int32_t mask
)
398 err
= kvm_mips_get_one_reg(cs
, reg_id
, &tmp
);
403 /* only change bits in mask */
404 change
= (*addr
^ tmp
) & mask
;
410 return kvm_mips_put_one_reg(cs
, reg_id
, &tmp
);
414 * We freeze the KVM timer when either the VM clock is stopped or the state is
415 * saved (the state is dirty).
419 * Save the state of the KVM timer when VM clock is stopped or state is synced
422 static int kvm_mips_save_count(CPUState
*cs
)
424 MIPSCPU
*cpu
= MIPS_CPU(cs
);
425 CPUMIPSState
*env
= &cpu
->env
;
429 /* freeze KVM timer */
430 err
= kvm_mips_get_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
432 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__
, err
);
434 } else if (!(count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
)) {
435 count_ctl
|= KVM_REG_MIPS_COUNT_CTL_DC
;
436 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
438 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__
, err
);
444 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CAUSE
, &env
->CP0_Cause
);
446 DPRINTF("%s: Failed to get CP0_CAUSE (%d)\n", __func__
, err
);
451 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_COUNT
, &env
->CP0_Count
);
453 DPRINTF("%s: Failed to get CP0_COUNT (%d)\n", __func__
, err
);
461 * Restore the state of the KVM timer when VM clock is restarted or state is
464 static int kvm_mips_restore_count(CPUState
*cs
)
466 MIPSCPU
*cpu
= MIPS_CPU(cs
);
467 CPUMIPSState
*env
= &cpu
->env
;
469 int err_dc
, err
, ret
= 0;
471 /* check the timer is frozen */
472 err_dc
= kvm_mips_get_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
474 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__
, err_dc
);
476 } else if (!(count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
)) {
477 /* freeze timer (sets COUNT_RESUME for us) */
478 count_ctl
|= KVM_REG_MIPS_COUNT_CTL_DC
;
479 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
481 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__
, err
);
487 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_CAUSE
, &env
->CP0_Cause
);
489 DPRINTF("%s: Failed to put CP0_CAUSE (%d)\n", __func__
, err
);
494 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_COUNT
, &env
->CP0_Count
);
496 DPRINTF("%s: Failed to put CP0_COUNT (%d)\n", __func__
, err
);
500 /* resume KVM timer */
502 count_ctl
&= ~KVM_REG_MIPS_COUNT_CTL_DC
;
503 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
505 DPRINTF("%s: Failed to set COUNT_CTL.DC=0 (%d)\n", __func__
, err
);
514 * Handle the VM clock being started or stopped
516 static void kvm_mips_update_state(void *opaque
, int running
, RunState state
)
518 CPUState
*cs
= opaque
;
520 uint64_t count_resume
;
523 * If state is already dirty (synced to QEMU) then the KVM timer state is
524 * already saved and can be restored when it is synced back to KVM.
527 if (!cs
->vcpu_dirty
) {
528 ret
= kvm_mips_save_count(cs
);
530 warn_report("Failed saving count");
534 /* Set clock restore time to now */
535 count_resume
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
536 ret
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_RESUME
,
539 warn_report("Failed setting COUNT_RESUME");
543 if (!cs
->vcpu_dirty
) {
544 ret
= kvm_mips_restore_count(cs
);
546 warn_report("Failed restoring count");
552 static int kvm_mips_put_fpu_registers(CPUState
*cs
, int level
)
554 MIPSCPU
*cpu
= MIPS_CPU(cs
);
555 CPUMIPSState
*env
= &cpu
->env
;
559 /* Only put FPU state if we're emulating a CPU with an FPU */
560 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
561 /* FPU Control Registers */
562 if (level
== KVM_PUT_FULL_STATE
) {
563 err
= kvm_mips_put_one_ureg(cs
, KVM_REG_MIPS_FCR_IR
,
564 &env
->active_fpu
.fcr0
);
566 DPRINTF("%s: Failed to put FCR_IR (%d)\n", __func__
, err
);
570 err
= kvm_mips_put_one_ureg(cs
, KVM_REG_MIPS_FCR_CSR
,
571 &env
->active_fpu
.fcr31
);
573 DPRINTF("%s: Failed to put FCR_CSR (%d)\n", __func__
, err
);
578 * FPU register state is a subset of MSA vector state, so don't put FPU
579 * registers if we're emulating a CPU with MSA.
581 if (!(env
->CP0_Config3
& (1 << CP0C3_MSAP
))) {
582 /* Floating point registers */
583 for (i
= 0; i
< 32; ++i
) {
584 if (env
->CP0_Status
& (1 << CP0St_FR
)) {
585 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_FPR_64(i
),
586 &env
->active_fpu
.fpr
[i
].d
);
588 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FPR_32(i
),
589 &env
->active_fpu
.fpr
[i
].w
[FP_ENDIAN_IDX
]);
592 DPRINTF("%s: Failed to put FPR%u (%d)\n", __func__
, i
, err
);
599 /* Only put MSA state if we're emulating a CPU with MSA */
600 if (env
->CP0_Config3
& (1 << CP0C3_MSAP
)) {
601 /* MSA Control Registers */
602 if (level
== KVM_PUT_FULL_STATE
) {
603 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_MSA_IR
,
606 DPRINTF("%s: Failed to put MSA_IR (%d)\n", __func__
, err
);
610 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_MSA_CSR
,
611 &env
->active_tc
.msacsr
);
613 DPRINTF("%s: Failed to put MSA_CSR (%d)\n", __func__
, err
);
617 /* Vector registers (includes FP registers) */
618 for (i
= 0; i
< 32; ++i
) {
619 /* Big endian MSA not supported by QEMU yet anyway */
620 err
= kvm_mips_put_one_reg64(cs
, KVM_REG_MIPS_VEC_128(i
),
621 env
->active_fpu
.fpr
[i
].wr
.d
);
623 DPRINTF("%s: Failed to put VEC%u (%d)\n", __func__
, i
, err
);
632 static int kvm_mips_get_fpu_registers(CPUState
*cs
)
634 MIPSCPU
*cpu
= MIPS_CPU(cs
);
635 CPUMIPSState
*env
= &cpu
->env
;
639 /* Only get FPU state if we're emulating a CPU with an FPU */
640 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
641 /* FPU Control Registers */
642 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FCR_IR
,
643 &env
->active_fpu
.fcr0
);
645 DPRINTF("%s: Failed to get FCR_IR (%d)\n", __func__
, err
);
648 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FCR_CSR
,
649 &env
->active_fpu
.fcr31
);
651 DPRINTF("%s: Failed to get FCR_CSR (%d)\n", __func__
, err
);
654 restore_fp_status(env
);
658 * FPU register state is a subset of MSA vector state, so don't save FPU
659 * registers if we're emulating a CPU with MSA.
661 if (!(env
->CP0_Config3
& (1 << CP0C3_MSAP
))) {
662 /* Floating point registers */
663 for (i
= 0; i
< 32; ++i
) {
664 if (env
->CP0_Status
& (1 << CP0St_FR
)) {
665 err
= kvm_mips_get_one_ureg64(cs
, KVM_REG_MIPS_FPR_64(i
),
666 &env
->active_fpu
.fpr
[i
].d
);
668 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FPR_32(i
),
669 &env
->active_fpu
.fpr
[i
].w
[FP_ENDIAN_IDX
]);
672 DPRINTF("%s: Failed to get FPR%u (%d)\n", __func__
, i
, err
);
679 /* Only get MSA state if we're emulating a CPU with MSA */
680 if (env
->CP0_Config3
& (1 << CP0C3_MSAP
)) {
681 /* MSA Control Registers */
682 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_MSA_IR
,
685 DPRINTF("%s: Failed to get MSA_IR (%d)\n", __func__
, err
);
688 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_MSA_CSR
,
689 &env
->active_tc
.msacsr
);
691 DPRINTF("%s: Failed to get MSA_CSR (%d)\n", __func__
, err
);
694 restore_msa_fp_status(env
);
697 /* Vector registers (includes FP registers) */
698 for (i
= 0; i
< 32; ++i
) {
699 /* Big endian MSA not supported by QEMU yet anyway */
700 err
= kvm_mips_get_one_reg64(cs
, KVM_REG_MIPS_VEC_128(i
),
701 env
->active_fpu
.fpr
[i
].wr
.d
);
703 DPRINTF("%s: Failed to get VEC%u (%d)\n", __func__
, i
, err
);
713 static int kvm_mips_put_cp0_registers(CPUState
*cs
, int level
)
715 MIPSCPU
*cpu
= MIPS_CPU(cs
);
716 CPUMIPSState
*env
= &cpu
->env
;
721 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_INDEX
, &env
->CP0_Index
);
723 DPRINTF("%s: Failed to put CP0_INDEX (%d)\n", __func__
, err
);
726 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_CONTEXT
,
729 DPRINTF("%s: Failed to put CP0_CONTEXT (%d)\n", __func__
, err
);
732 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_USERLOCAL
,
733 &env
->active_tc
.CP0_UserLocal
);
735 DPRINTF("%s: Failed to put CP0_USERLOCAL (%d)\n", __func__
, err
);
738 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEMASK
,
741 DPRINTF("%s: Failed to put CP0_PAGEMASK (%d)\n", __func__
, err
);
744 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_WIRED
, &env
->CP0_Wired
);
746 DPRINTF("%s: Failed to put CP0_WIRED (%d)\n", __func__
, err
);
749 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_HWRENA
, &env
->CP0_HWREna
);
751 DPRINTF("%s: Failed to put CP0_HWRENA (%d)\n", __func__
, err
);
754 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_BADVADDR
,
757 DPRINTF("%s: Failed to put CP0_BADVADDR (%d)\n", __func__
, err
);
761 /* If VM clock stopped then state will be restored when it is restarted */
762 if (runstate_is_running()) {
763 err
= kvm_mips_restore_count(cs
);
769 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_ENTRYHI
,
772 DPRINTF("%s: Failed to put CP0_ENTRYHI (%d)\n", __func__
, err
);
775 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_COMPARE
,
778 DPRINTF("%s: Failed to put CP0_COMPARE (%d)\n", __func__
, err
);
781 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_STATUS
, &env
->CP0_Status
);
783 DPRINTF("%s: Failed to put CP0_STATUS (%d)\n", __func__
, err
);
786 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_EPC
, &env
->CP0_EPC
);
788 DPRINTF("%s: Failed to put CP0_EPC (%d)\n", __func__
, err
);
791 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_PRID
, &env
->CP0_PRid
);
793 DPRINTF("%s: Failed to put CP0_PRID (%d)\n", __func__
, err
);
796 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG
,
798 KVM_REG_MIPS_CP0_CONFIG_MASK
);
800 DPRINTF("%s: Failed to change CP0_CONFIG (%d)\n", __func__
, err
);
803 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG1
,
805 KVM_REG_MIPS_CP0_CONFIG1_MASK
);
807 DPRINTF("%s: Failed to change CP0_CONFIG1 (%d)\n", __func__
, err
);
810 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG2
,
812 KVM_REG_MIPS_CP0_CONFIG2_MASK
);
814 DPRINTF("%s: Failed to change CP0_CONFIG2 (%d)\n", __func__
, err
);
817 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG3
,
819 KVM_REG_MIPS_CP0_CONFIG3_MASK
);
821 DPRINTF("%s: Failed to change CP0_CONFIG3 (%d)\n", __func__
, err
);
824 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG4
,
826 KVM_REG_MIPS_CP0_CONFIG4_MASK
);
828 DPRINTF("%s: Failed to change CP0_CONFIG4 (%d)\n", __func__
, err
);
831 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG5
,
833 KVM_REG_MIPS_CP0_CONFIG5_MASK
);
835 DPRINTF("%s: Failed to change CP0_CONFIG5 (%d)\n", __func__
, err
);
838 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_ERROREPC
,
841 DPRINTF("%s: Failed to put CP0_ERROREPC (%d)\n", __func__
, err
);
848 static int kvm_mips_get_cp0_registers(CPUState
*cs
)
850 MIPSCPU
*cpu
= MIPS_CPU(cs
);
851 CPUMIPSState
*env
= &cpu
->env
;
854 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_INDEX
, &env
->CP0_Index
);
856 DPRINTF("%s: Failed to get CP0_INDEX (%d)\n", __func__
, err
);
859 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_CONTEXT
,
862 DPRINTF("%s: Failed to get CP0_CONTEXT (%d)\n", __func__
, err
);
865 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_USERLOCAL
,
866 &env
->active_tc
.CP0_UserLocal
);
868 DPRINTF("%s: Failed to get CP0_USERLOCAL (%d)\n", __func__
, err
);
871 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEMASK
,
874 DPRINTF("%s: Failed to get CP0_PAGEMASK (%d)\n", __func__
, err
);
877 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_WIRED
, &env
->CP0_Wired
);
879 DPRINTF("%s: Failed to get CP0_WIRED (%d)\n", __func__
, err
);
882 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_HWRENA
, &env
->CP0_HWREna
);
884 DPRINTF("%s: Failed to get CP0_HWRENA (%d)\n", __func__
, err
);
887 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_BADVADDR
,
890 DPRINTF("%s: Failed to get CP0_BADVADDR (%d)\n", __func__
, err
);
893 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_ENTRYHI
,
896 DPRINTF("%s: Failed to get CP0_ENTRYHI (%d)\n", __func__
, err
);
899 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_COMPARE
,
902 DPRINTF("%s: Failed to get CP0_COMPARE (%d)\n", __func__
, err
);
905 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_STATUS
, &env
->CP0_Status
);
907 DPRINTF("%s: Failed to get CP0_STATUS (%d)\n", __func__
, err
);
911 /* If VM clock stopped then state was already saved when it was stopped */
912 if (runstate_is_running()) {
913 err
= kvm_mips_save_count(cs
);
919 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_EPC
, &env
->CP0_EPC
);
921 DPRINTF("%s: Failed to get CP0_EPC (%d)\n", __func__
, err
);
924 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_PRID
, &env
->CP0_PRid
);
926 DPRINTF("%s: Failed to get CP0_PRID (%d)\n", __func__
, err
);
929 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG
, &env
->CP0_Config0
);
931 DPRINTF("%s: Failed to get CP0_CONFIG (%d)\n", __func__
, err
);
934 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG1
, &env
->CP0_Config1
);
936 DPRINTF("%s: Failed to get CP0_CONFIG1 (%d)\n", __func__
, err
);
939 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG2
, &env
->CP0_Config2
);
941 DPRINTF("%s: Failed to get CP0_CONFIG2 (%d)\n", __func__
, err
);
944 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG3
, &env
->CP0_Config3
);
946 DPRINTF("%s: Failed to get CP0_CONFIG3 (%d)\n", __func__
, err
);
949 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG4
, &env
->CP0_Config4
);
951 DPRINTF("%s: Failed to get CP0_CONFIG4 (%d)\n", __func__
, err
);
954 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG5
, &env
->CP0_Config5
);
956 DPRINTF("%s: Failed to get CP0_CONFIG5 (%d)\n", __func__
, err
);
959 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_ERROREPC
,
962 DPRINTF("%s: Failed to get CP0_ERROREPC (%d)\n", __func__
, err
);
969 int kvm_arch_put_registers(CPUState
*cs
, int level
)
971 MIPSCPU
*cpu
= MIPS_CPU(cs
);
972 CPUMIPSState
*env
= &cpu
->env
;
973 struct kvm_regs regs
;
977 /* Set the registers based on QEMU's view of things */
978 for (i
= 0; i
< 32; i
++) {
979 regs
.gpr
[i
] = (int64_t)(target_long
)env
->active_tc
.gpr
[i
];
982 regs
.hi
= (int64_t)(target_long
)env
->active_tc
.HI
[0];
983 regs
.lo
= (int64_t)(target_long
)env
->active_tc
.LO
[0];
984 regs
.pc
= (int64_t)(target_long
)env
->active_tc
.PC
;
986 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_REGS
, ®s
);
992 ret
= kvm_mips_put_cp0_registers(cs
, level
);
997 ret
= kvm_mips_put_fpu_registers(cs
, level
);
1005 int kvm_arch_get_registers(CPUState
*cs
)
1007 MIPSCPU
*cpu
= MIPS_CPU(cs
);
1008 CPUMIPSState
*env
= &cpu
->env
;
1010 struct kvm_regs regs
;
1013 /* Get the current register set as KVM seems it */
1014 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_REGS
, ®s
);
1020 for (i
= 0; i
< 32; i
++) {
1021 env
->active_tc
.gpr
[i
] = regs
.gpr
[i
];
1024 env
->active_tc
.HI
[0] = regs
.hi
;
1025 env
->active_tc
.LO
[0] = regs
.lo
;
1026 env
->active_tc
.PC
= regs
.pc
;
1028 kvm_mips_get_cp0_registers(cs
);
1029 kvm_mips_get_fpu_registers(cs
);
1034 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
1035 uint64_t address
, uint32_t data
, PCIDevice
*dev
)
1040 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry
*route
,
1041 int vector
, PCIDevice
*dev
)
1046 int kvm_arch_release_virq_post(int virq
)
1051 int kvm_arch_msi_data_to_gsi(uint32_t data
)