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>
16 #include <linux/kvm.h>
18 #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_init_vcpu(CPUState
*cs
)
61 MIPSCPU
*cpu
= MIPS_CPU(cs
);
62 CPUMIPSState
*env
= &cpu
->env
;
65 qemu_add_vm_change_state_handler(kvm_mips_update_state
, cs
);
67 if (kvm_mips_fpu_cap
&& env
->CP0_Config1
& (1 << CP0C1_FP
)) {
68 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_MIPS_FPU
, 0, 0);
70 /* mark unsupported so it gets disabled on reset */
76 if (kvm_mips_msa_cap
&& env
->CP0_Config3
& (1 << CP0C3_MSAP
)) {
77 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_MIPS_MSA
, 0, 0);
79 /* mark unsupported so it gets disabled on reset */
85 DPRINTF("%s\n", __func__
);
89 void kvm_mips_reset_vcpu(MIPSCPU
*cpu
)
91 CPUMIPSState
*env
= &cpu
->env
;
93 if (!kvm_mips_fpu_cap
&& env
->CP0_Config1
& (1 << CP0C1_FP
)) {
94 fprintf(stderr
, "Warning: KVM does not support FPU, disabling\n");
95 env
->CP0_Config1
&= ~(1 << CP0C1_FP
);
97 if (!kvm_mips_msa_cap
&& env
->CP0_Config3
& (1 << CP0C3_MSAP
)) {
98 fprintf(stderr
, "Warning: KVM does not support MSA, disabling\n");
99 env
->CP0_Config3
&= ~(1 << CP0C3_MSAP
);
102 DPRINTF("%s\n", __func__
);
105 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
107 DPRINTF("%s\n", __func__
);
111 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
113 DPRINTF("%s\n", __func__
);
117 static inline int cpu_mips_io_interrupts_pending(MIPSCPU
*cpu
)
119 CPUMIPSState
*env
= &cpu
->env
;
121 return env
->CP0_Cause
& (0x1 << (2 + CP0Ca_IP
));
125 void kvm_arch_pre_run(CPUState
*cs
, struct kvm_run
*run
)
127 MIPSCPU
*cpu
= MIPS_CPU(cs
);
129 struct kvm_mips_interrupt intr
;
131 qemu_mutex_lock_iothread();
133 if ((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
134 cpu_mips_io_interrupts_pending(cpu
)) {
137 r
= kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
139 error_report("%s: cpu %d: failed to inject IRQ %x",
140 __func__
, cs
->cpu_index
, intr
.irq
);
144 qemu_mutex_unlock_iothread();
147 MemTxAttrs
kvm_arch_post_run(CPUState
*cs
, struct kvm_run
*run
)
149 return MEMTXATTRS_UNSPECIFIED
;
152 int kvm_arch_process_async_events(CPUState
*cs
)
157 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
161 DPRINTF("%s\n", __func__
);
162 switch (run
->exit_reason
) {
164 error_report("%s: unknown exit reason %d",
165 __func__
, run
->exit_reason
);
173 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
175 DPRINTF("%s\n", __func__
);
179 int kvm_arch_on_sigbus_vcpu(CPUState
*cs
, int code
, void *addr
)
181 DPRINTF("%s\n", __func__
);
185 int kvm_arch_on_sigbus(int code
, void *addr
)
187 DPRINTF("%s\n", __func__
);
191 void kvm_arch_init_irq_routing(KVMState
*s
)
195 int kvm_mips_set_interrupt(MIPSCPU
*cpu
, int irq
, int level
)
197 CPUState
*cs
= CPU(cpu
);
198 struct kvm_mips_interrupt intr
;
200 if (!kvm_enabled()) {
212 kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
217 int kvm_mips_set_ipi_interrupt(MIPSCPU
*cpu
, int irq
, int level
)
219 CPUState
*cs
= current_cpu
;
220 CPUState
*dest_cs
= CPU(cpu
);
221 struct kvm_mips_interrupt intr
;
223 if (!kvm_enabled()) {
227 intr
.cpu
= dest_cs
->cpu_index
;
235 DPRINTF("%s: CPU %d, IRQ: %d\n", __func__
, intr
.cpu
, intr
.irq
);
237 kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
242 #define MIPS_CP0_32(_R, _S) \
243 (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U32 | (8 * (_R) + (_S)))
245 #define MIPS_CP0_64(_R, _S) \
246 (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U64 | (8 * (_R) + (_S)))
248 #define KVM_REG_MIPS_CP0_INDEX MIPS_CP0_32(0, 0)
249 #define KVM_REG_MIPS_CP0_CONTEXT MIPS_CP0_64(4, 0)
250 #define KVM_REG_MIPS_CP0_USERLOCAL MIPS_CP0_64(4, 2)
251 #define KVM_REG_MIPS_CP0_PAGEMASK MIPS_CP0_32(5, 0)
252 #define KVM_REG_MIPS_CP0_WIRED MIPS_CP0_32(6, 0)
253 #define KVM_REG_MIPS_CP0_HWRENA MIPS_CP0_32(7, 0)
254 #define KVM_REG_MIPS_CP0_BADVADDR MIPS_CP0_64(8, 0)
255 #define KVM_REG_MIPS_CP0_COUNT MIPS_CP0_32(9, 0)
256 #define KVM_REG_MIPS_CP0_ENTRYHI MIPS_CP0_64(10, 0)
257 #define KVM_REG_MIPS_CP0_COMPARE MIPS_CP0_32(11, 0)
258 #define KVM_REG_MIPS_CP0_STATUS MIPS_CP0_32(12, 0)
259 #define KVM_REG_MIPS_CP0_CAUSE MIPS_CP0_32(13, 0)
260 #define KVM_REG_MIPS_CP0_EPC MIPS_CP0_64(14, 0)
261 #define KVM_REG_MIPS_CP0_PRID MIPS_CP0_32(15, 0)
262 #define KVM_REG_MIPS_CP0_CONFIG MIPS_CP0_32(16, 0)
263 #define KVM_REG_MIPS_CP0_CONFIG1 MIPS_CP0_32(16, 1)
264 #define KVM_REG_MIPS_CP0_CONFIG2 MIPS_CP0_32(16, 2)
265 #define KVM_REG_MIPS_CP0_CONFIG3 MIPS_CP0_32(16, 3)
266 #define KVM_REG_MIPS_CP0_CONFIG4 MIPS_CP0_32(16, 4)
267 #define KVM_REG_MIPS_CP0_CONFIG5 MIPS_CP0_32(16, 5)
268 #define KVM_REG_MIPS_CP0_ERROREPC MIPS_CP0_64(30, 0)
270 static inline int kvm_mips_put_one_reg(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_put_one_ureg(CPUState
*cs
, uint64_t reg_id
,
284 struct kvm_one_reg cp0reg
= {
286 .addr
= (uintptr_t)addr
289 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
292 static inline int kvm_mips_put_one_ulreg(CPUState
*cs
, uint64_t reg_id
,
295 uint64_t val64
= *addr
;
296 struct kvm_one_reg cp0reg
= {
298 .addr
= (uintptr_t)&val64
301 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
304 static inline int kvm_mips_put_one_reg64(CPUState
*cs
, uint64_t reg_id
,
307 struct kvm_one_reg cp0reg
= {
309 .addr
= (uintptr_t)addr
312 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
315 static inline int kvm_mips_put_one_ureg64(CPUState
*cs
, uint64_t reg_id
,
318 struct kvm_one_reg cp0reg
= {
320 .addr
= (uintptr_t)addr
323 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
326 static inline int kvm_mips_get_one_reg(CPUState
*cs
, uint64_t reg_id
,
329 struct kvm_one_reg cp0reg
= {
331 .addr
= (uintptr_t)addr
334 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
337 static inline int kvm_mips_get_one_ureg(CPUState
*cs
, uint64_t reg_id
,
340 struct kvm_one_reg cp0reg
= {
342 .addr
= (uintptr_t)addr
345 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
348 static inline int kvm_mips_get_one_ulreg(CPUState
*cs
, uint64_t reg_id
,
353 struct kvm_one_reg cp0reg
= {
355 .addr
= (uintptr_t)&val64
358 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
365 static inline int kvm_mips_get_one_reg64(CPUState
*cs
, uint64_t reg_id
,
368 struct kvm_one_reg cp0reg
= {
370 .addr
= (uintptr_t)addr
373 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
376 static inline int kvm_mips_get_one_ureg64(CPUState
*cs
, uint64_t reg_id
,
379 struct kvm_one_reg cp0reg
= {
381 .addr
= (uintptr_t)addr
384 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
387 #define KVM_REG_MIPS_CP0_CONFIG_MASK (1U << CP0C0_M)
388 #define KVM_REG_MIPS_CP0_CONFIG1_MASK ((1U << CP0C1_M) | \
390 #define KVM_REG_MIPS_CP0_CONFIG2_MASK (1U << CP0C2_M)
391 #define KVM_REG_MIPS_CP0_CONFIG3_MASK ((1U << CP0C3_M) | \
393 #define KVM_REG_MIPS_CP0_CONFIG4_MASK (1U << CP0C4_M)
394 #define KVM_REG_MIPS_CP0_CONFIG5_MASK ((1U << CP0C5_MSAEn) | \
395 (1U << CP0C5_UFE) | \
396 (1U << CP0C5_FRE) | \
399 static inline int kvm_mips_change_one_reg(CPUState
*cs
, uint64_t reg_id
,
400 int32_t *addr
, int32_t mask
)
405 err
= kvm_mips_get_one_reg(cs
, reg_id
, &tmp
);
410 /* only change bits in mask */
411 change
= (*addr
^ tmp
) & mask
;
417 return kvm_mips_put_one_reg(cs
, reg_id
, &tmp
);
421 * We freeze the KVM timer when either the VM clock is stopped or the state is
422 * saved (the state is dirty).
426 * Save the state of the KVM timer when VM clock is stopped or state is synced
429 static int kvm_mips_save_count(CPUState
*cs
)
431 MIPSCPU
*cpu
= MIPS_CPU(cs
);
432 CPUMIPSState
*env
= &cpu
->env
;
436 /* freeze KVM timer */
437 err
= kvm_mips_get_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
439 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__
, err
);
441 } else if (!(count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
)) {
442 count_ctl
|= KVM_REG_MIPS_COUNT_CTL_DC
;
443 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
445 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__
, err
);
451 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CAUSE
, &env
->CP0_Cause
);
453 DPRINTF("%s: Failed to get CP0_CAUSE (%d)\n", __func__
, err
);
458 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_COUNT
, &env
->CP0_Count
);
460 DPRINTF("%s: Failed to get CP0_COUNT (%d)\n", __func__
, err
);
468 * Restore the state of the KVM timer when VM clock is restarted or state is
471 static int kvm_mips_restore_count(CPUState
*cs
)
473 MIPSCPU
*cpu
= MIPS_CPU(cs
);
474 CPUMIPSState
*env
= &cpu
->env
;
476 int err_dc
, err
, ret
= 0;
478 /* check the timer is frozen */
479 err_dc
= kvm_mips_get_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
481 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__
, err_dc
);
483 } else if (!(count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
)) {
484 /* freeze timer (sets COUNT_RESUME for us) */
485 count_ctl
|= KVM_REG_MIPS_COUNT_CTL_DC
;
486 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
488 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__
, err
);
494 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_CAUSE
, &env
->CP0_Cause
);
496 DPRINTF("%s: Failed to put CP0_CAUSE (%d)\n", __func__
, err
);
501 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_COUNT
, &env
->CP0_Count
);
503 DPRINTF("%s: Failed to put CP0_COUNT (%d)\n", __func__
, err
);
507 /* resume KVM timer */
509 count_ctl
&= ~KVM_REG_MIPS_COUNT_CTL_DC
;
510 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
512 DPRINTF("%s: Failed to set COUNT_CTL.DC=0 (%d)\n", __func__
, err
);
521 * Handle the VM clock being started or stopped
523 static void kvm_mips_update_state(void *opaque
, int running
, RunState state
)
525 CPUState
*cs
= opaque
;
527 uint64_t count_resume
;
530 * If state is already dirty (synced to QEMU) then the KVM timer state is
531 * already saved and can be restored when it is synced back to KVM.
534 if (!cs
->kvm_vcpu_dirty
) {
535 ret
= kvm_mips_save_count(cs
);
537 fprintf(stderr
, "Failed saving count\n");
541 /* Set clock restore time to now */
542 count_resume
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
543 ret
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_RESUME
,
546 fprintf(stderr
, "Failed setting COUNT_RESUME\n");
550 if (!cs
->kvm_vcpu_dirty
) {
551 ret
= kvm_mips_restore_count(cs
);
553 fprintf(stderr
, "Failed restoring count\n");
559 static int kvm_mips_put_fpu_registers(CPUState
*cs
, int level
)
561 MIPSCPU
*cpu
= MIPS_CPU(cs
);
562 CPUMIPSState
*env
= &cpu
->env
;
566 /* Only put FPU state if we're emulating a CPU with an FPU */
567 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
568 /* FPU Control Registers */
569 if (level
== KVM_PUT_FULL_STATE
) {
570 err
= kvm_mips_put_one_ureg(cs
, KVM_REG_MIPS_FCR_IR
,
571 &env
->active_fpu
.fcr0
);
573 DPRINTF("%s: Failed to put FCR_IR (%d)\n", __func__
, err
);
577 err
= kvm_mips_put_one_ureg(cs
, KVM_REG_MIPS_FCR_CSR
,
578 &env
->active_fpu
.fcr31
);
580 DPRINTF("%s: Failed to put FCR_CSR (%d)\n", __func__
, err
);
585 * FPU register state is a subset of MSA vector state, so don't put FPU
586 * registers if we're emulating a CPU with MSA.
588 if (!(env
->CP0_Config3
& (1 << CP0C3_MSAP
))) {
589 /* Floating point registers */
590 for (i
= 0; i
< 32; ++i
) {
591 if (env
->CP0_Status
& (1 << CP0St_FR
)) {
592 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_FPR_64(i
),
593 &env
->active_fpu
.fpr
[i
].d
);
595 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FPR_32(i
),
596 &env
->active_fpu
.fpr
[i
].w
[FP_ENDIAN_IDX
]);
599 DPRINTF("%s: Failed to put FPR%u (%d)\n", __func__
, i
, err
);
606 /* Only put MSA state if we're emulating a CPU with MSA */
607 if (env
->CP0_Config3
& (1 << CP0C3_MSAP
)) {
608 /* MSA Control Registers */
609 if (level
== KVM_PUT_FULL_STATE
) {
610 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_MSA_IR
,
613 DPRINTF("%s: Failed to put MSA_IR (%d)\n", __func__
, err
);
617 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_MSA_CSR
,
618 &env
->active_tc
.msacsr
);
620 DPRINTF("%s: Failed to put MSA_CSR (%d)\n", __func__
, err
);
624 /* Vector registers (includes FP registers) */
625 for (i
= 0; i
< 32; ++i
) {
626 /* Big endian MSA not supported by QEMU yet anyway */
627 err
= kvm_mips_put_one_reg64(cs
, KVM_REG_MIPS_VEC_128(i
),
628 env
->active_fpu
.fpr
[i
].wr
.d
);
630 DPRINTF("%s: Failed to put VEC%u (%d)\n", __func__
, i
, err
);
639 static int kvm_mips_get_fpu_registers(CPUState
*cs
)
641 MIPSCPU
*cpu
= MIPS_CPU(cs
);
642 CPUMIPSState
*env
= &cpu
->env
;
646 /* Only get FPU state if we're emulating a CPU with an FPU */
647 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
648 /* FPU Control Registers */
649 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FCR_IR
,
650 &env
->active_fpu
.fcr0
);
652 DPRINTF("%s: Failed to get FCR_IR (%d)\n", __func__
, err
);
655 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FCR_CSR
,
656 &env
->active_fpu
.fcr31
);
658 DPRINTF("%s: Failed to get FCR_CSR (%d)\n", __func__
, err
);
661 restore_fp_status(env
);
665 * FPU register state is a subset of MSA vector state, so don't save FPU
666 * registers if we're emulating a CPU with MSA.
668 if (!(env
->CP0_Config3
& (1 << CP0C3_MSAP
))) {
669 /* Floating point registers */
670 for (i
= 0; i
< 32; ++i
) {
671 if (env
->CP0_Status
& (1 << CP0St_FR
)) {
672 err
= kvm_mips_get_one_ureg64(cs
, KVM_REG_MIPS_FPR_64(i
),
673 &env
->active_fpu
.fpr
[i
].d
);
675 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FPR_32(i
),
676 &env
->active_fpu
.fpr
[i
].w
[FP_ENDIAN_IDX
]);
679 DPRINTF("%s: Failed to get FPR%u (%d)\n", __func__
, i
, err
);
686 /* Only get MSA state if we're emulating a CPU with MSA */
687 if (env
->CP0_Config3
& (1 << CP0C3_MSAP
)) {
688 /* MSA Control Registers */
689 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_MSA_IR
,
692 DPRINTF("%s: Failed to get MSA_IR (%d)\n", __func__
, err
);
695 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_MSA_CSR
,
696 &env
->active_tc
.msacsr
);
698 DPRINTF("%s: Failed to get MSA_CSR (%d)\n", __func__
, err
);
701 restore_msa_fp_status(env
);
704 /* Vector registers (includes FP registers) */
705 for (i
= 0; i
< 32; ++i
) {
706 /* Big endian MSA not supported by QEMU yet anyway */
707 err
= kvm_mips_get_one_reg64(cs
, KVM_REG_MIPS_VEC_128(i
),
708 env
->active_fpu
.fpr
[i
].wr
.d
);
710 DPRINTF("%s: Failed to get VEC%u (%d)\n", __func__
, i
, err
);
720 static int kvm_mips_put_cp0_registers(CPUState
*cs
, int level
)
722 MIPSCPU
*cpu
= MIPS_CPU(cs
);
723 CPUMIPSState
*env
= &cpu
->env
;
728 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_INDEX
, &env
->CP0_Index
);
730 DPRINTF("%s: Failed to put CP0_INDEX (%d)\n", __func__
, err
);
733 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_CONTEXT
,
736 DPRINTF("%s: Failed to put CP0_CONTEXT (%d)\n", __func__
, err
);
739 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_USERLOCAL
,
740 &env
->active_tc
.CP0_UserLocal
);
742 DPRINTF("%s: Failed to put CP0_USERLOCAL (%d)\n", __func__
, err
);
745 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEMASK
,
748 DPRINTF("%s: Failed to put CP0_PAGEMASK (%d)\n", __func__
, err
);
751 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_WIRED
, &env
->CP0_Wired
);
753 DPRINTF("%s: Failed to put CP0_WIRED (%d)\n", __func__
, err
);
756 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_HWRENA
, &env
->CP0_HWREna
);
758 DPRINTF("%s: Failed to put CP0_HWRENA (%d)\n", __func__
, err
);
761 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_BADVADDR
,
764 DPRINTF("%s: Failed to put CP0_BADVADDR (%d)\n", __func__
, err
);
768 /* If VM clock stopped then state will be restored when it is restarted */
769 if (runstate_is_running()) {
770 err
= kvm_mips_restore_count(cs
);
776 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_ENTRYHI
,
779 DPRINTF("%s: Failed to put CP0_ENTRYHI (%d)\n", __func__
, err
);
782 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_COMPARE
,
785 DPRINTF("%s: Failed to put CP0_COMPARE (%d)\n", __func__
, err
);
788 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_STATUS
, &env
->CP0_Status
);
790 DPRINTF("%s: Failed to put CP0_STATUS (%d)\n", __func__
, err
);
793 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_EPC
, &env
->CP0_EPC
);
795 DPRINTF("%s: Failed to put CP0_EPC (%d)\n", __func__
, err
);
798 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_PRID
, &env
->CP0_PRid
);
800 DPRINTF("%s: Failed to put CP0_PRID (%d)\n", __func__
, err
);
803 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG
,
805 KVM_REG_MIPS_CP0_CONFIG_MASK
);
807 DPRINTF("%s: Failed to change CP0_CONFIG (%d)\n", __func__
, err
);
810 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG1
,
812 KVM_REG_MIPS_CP0_CONFIG1_MASK
);
814 DPRINTF("%s: Failed to change CP0_CONFIG1 (%d)\n", __func__
, err
);
817 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG2
,
819 KVM_REG_MIPS_CP0_CONFIG2_MASK
);
821 DPRINTF("%s: Failed to change CP0_CONFIG2 (%d)\n", __func__
, err
);
824 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG3
,
826 KVM_REG_MIPS_CP0_CONFIG3_MASK
);
828 DPRINTF("%s: Failed to change CP0_CONFIG3 (%d)\n", __func__
, err
);
831 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG4
,
833 KVM_REG_MIPS_CP0_CONFIG4_MASK
);
835 DPRINTF("%s: Failed to change CP0_CONFIG4 (%d)\n", __func__
, err
);
838 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG5
,
840 KVM_REG_MIPS_CP0_CONFIG5_MASK
);
842 DPRINTF("%s: Failed to change CP0_CONFIG5 (%d)\n", __func__
, err
);
845 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_ERROREPC
,
848 DPRINTF("%s: Failed to put CP0_ERROREPC (%d)\n", __func__
, err
);
855 static int kvm_mips_get_cp0_registers(CPUState
*cs
)
857 MIPSCPU
*cpu
= MIPS_CPU(cs
);
858 CPUMIPSState
*env
= &cpu
->env
;
861 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_INDEX
, &env
->CP0_Index
);
863 DPRINTF("%s: Failed to get CP0_INDEX (%d)\n", __func__
, err
);
866 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_CONTEXT
,
869 DPRINTF("%s: Failed to get CP0_CONTEXT (%d)\n", __func__
, err
);
872 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_USERLOCAL
,
873 &env
->active_tc
.CP0_UserLocal
);
875 DPRINTF("%s: Failed to get CP0_USERLOCAL (%d)\n", __func__
, err
);
878 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEMASK
,
881 DPRINTF("%s: Failed to get CP0_PAGEMASK (%d)\n", __func__
, err
);
884 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_WIRED
, &env
->CP0_Wired
);
886 DPRINTF("%s: Failed to get CP0_WIRED (%d)\n", __func__
, err
);
889 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_HWRENA
, &env
->CP0_HWREna
);
891 DPRINTF("%s: Failed to get CP0_HWRENA (%d)\n", __func__
, err
);
894 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_BADVADDR
,
897 DPRINTF("%s: Failed to get CP0_BADVADDR (%d)\n", __func__
, err
);
900 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_ENTRYHI
,
903 DPRINTF("%s: Failed to get CP0_ENTRYHI (%d)\n", __func__
, err
);
906 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_COMPARE
,
909 DPRINTF("%s: Failed to get CP0_COMPARE (%d)\n", __func__
, err
);
912 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_STATUS
, &env
->CP0_Status
);
914 DPRINTF("%s: Failed to get CP0_STATUS (%d)\n", __func__
, err
);
918 /* If VM clock stopped then state was already saved when it was stopped */
919 if (runstate_is_running()) {
920 err
= kvm_mips_save_count(cs
);
926 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_EPC
, &env
->CP0_EPC
);
928 DPRINTF("%s: Failed to get CP0_EPC (%d)\n", __func__
, err
);
931 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_PRID
, &env
->CP0_PRid
);
933 DPRINTF("%s: Failed to get CP0_PRID (%d)\n", __func__
, err
);
936 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG
, &env
->CP0_Config0
);
938 DPRINTF("%s: Failed to get CP0_CONFIG (%d)\n", __func__
, err
);
941 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG1
, &env
->CP0_Config1
);
943 DPRINTF("%s: Failed to get CP0_CONFIG1 (%d)\n", __func__
, err
);
946 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG2
, &env
->CP0_Config2
);
948 DPRINTF("%s: Failed to get CP0_CONFIG2 (%d)\n", __func__
, err
);
951 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG3
, &env
->CP0_Config3
);
953 DPRINTF("%s: Failed to get CP0_CONFIG3 (%d)\n", __func__
, err
);
956 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG4
, &env
->CP0_Config4
);
958 DPRINTF("%s: Failed to get CP0_CONFIG4 (%d)\n", __func__
, err
);
961 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG5
, &env
->CP0_Config5
);
963 DPRINTF("%s: Failed to get CP0_CONFIG5 (%d)\n", __func__
, err
);
966 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_ERROREPC
,
969 DPRINTF("%s: Failed to get CP0_ERROREPC (%d)\n", __func__
, err
);
976 int kvm_arch_put_registers(CPUState
*cs
, int level
)
978 MIPSCPU
*cpu
= MIPS_CPU(cs
);
979 CPUMIPSState
*env
= &cpu
->env
;
980 struct kvm_regs regs
;
984 /* Set the registers based on QEMU's view of things */
985 for (i
= 0; i
< 32; i
++) {
986 regs
.gpr
[i
] = (int64_t)(target_long
)env
->active_tc
.gpr
[i
];
989 regs
.hi
= (int64_t)(target_long
)env
->active_tc
.HI
[0];
990 regs
.lo
= (int64_t)(target_long
)env
->active_tc
.LO
[0];
991 regs
.pc
= (int64_t)(target_long
)env
->active_tc
.PC
;
993 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_REGS
, ®s
);
999 ret
= kvm_mips_put_cp0_registers(cs
, level
);
1004 ret
= kvm_mips_put_fpu_registers(cs
, level
);
1012 int kvm_arch_get_registers(CPUState
*cs
)
1014 MIPSCPU
*cpu
= MIPS_CPU(cs
);
1015 CPUMIPSState
*env
= &cpu
->env
;
1017 struct kvm_regs regs
;
1020 /* Get the current register set as KVM seems it */
1021 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_REGS
, ®s
);
1027 for (i
= 0; i
< 32; i
++) {
1028 env
->active_tc
.gpr
[i
] = regs
.gpr
[i
];
1031 env
->active_tc
.HI
[0] = regs
.hi
;
1032 env
->active_tc
.LO
[0] = regs
.lo
;
1033 env
->active_tc
.PC
= regs
.pc
;
1035 kvm_mips_get_cp0_registers(cs
);
1036 kvm_mips_get_fpu_registers(cs
);
1041 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
1042 uint64_t address
, uint32_t data
, PCIDevice
*dev
)
1047 int kvm_arch_msi_data_to_gsi(uint32_t data
)