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/main-loop.h"
22 #include "sysemu/kvm.h"
23 #include "sysemu/kvm_int.h"
24 #include "sysemu/runstate.h"
26 #include "hw/boards.h"
27 #include "fpu_helper.h"
31 #define DPRINTF(fmt, ...) \
32 do { if (DEBUG_KVM) { fprintf(stderr, fmt, ## __VA_ARGS__); } } while (0)
34 static int kvm_mips_fpu_cap
;
35 static int kvm_mips_msa_cap
;
37 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
41 static void kvm_mips_update_state(void *opaque
, bool running
, RunState state
);
43 unsigned long kvm_arch_vcpu_id(CPUState
*cs
)
48 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
50 /* MIPS has 128 signals */
51 kvm_set_sigmask_len(s
, 16);
53 kvm_mips_fpu_cap
= kvm_check_extension(s
, KVM_CAP_MIPS_FPU
);
54 kvm_mips_msa_cap
= kvm_check_extension(s
, KVM_CAP_MIPS_MSA
);
56 DPRINTF("%s\n", __func__
);
60 int kvm_arch_irqchip_create(KVMState
*s
)
65 int kvm_arch_init_vcpu(CPUState
*cs
)
67 MIPSCPU
*cpu
= MIPS_CPU(cs
);
68 CPUMIPSState
*env
= &cpu
->env
;
71 qemu_add_vm_change_state_handler(kvm_mips_update_state
, cs
);
73 if (kvm_mips_fpu_cap
&& env
->CP0_Config1
& (1 << CP0C1_FP
)) {
74 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_MIPS_FPU
, 0, 0);
76 /* mark unsupported so it gets disabled on reset */
82 if (kvm_mips_msa_cap
&& ase_msa_available(env
)) {
83 ret
= kvm_vcpu_enable_cap(cs
, KVM_CAP_MIPS_MSA
, 0, 0);
85 /* mark unsupported so it gets disabled on reset */
91 DPRINTF("%s\n", __func__
);
95 int kvm_arch_destroy_vcpu(CPUState
*cs
)
100 void kvm_mips_reset_vcpu(MIPSCPU
*cpu
)
102 CPUMIPSState
*env
= &cpu
->env
;
104 if (!kvm_mips_fpu_cap
&& env
->CP0_Config1
& (1 << CP0C1_FP
)) {
105 warn_report("KVM does not support FPU, disabling");
106 env
->CP0_Config1
&= ~(1 << CP0C1_FP
);
108 if (!kvm_mips_msa_cap
&& ase_msa_available(env
)) {
109 warn_report("KVM does not support MSA, disabling");
110 env
->CP0_Config3
&= ~(1 << CP0C3_MSAP
);
113 DPRINTF("%s\n", __func__
);
116 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
118 DPRINTF("%s\n", __func__
);
122 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
124 DPRINTF("%s\n", __func__
);
128 static inline int cpu_mips_io_interrupts_pending(MIPSCPU
*cpu
)
130 CPUMIPSState
*env
= &cpu
->env
;
132 return env
->CP0_Cause
& (0x1 << (2 + CP0Ca_IP
));
136 void kvm_arch_pre_run(CPUState
*cs
, struct kvm_run
*run
)
138 MIPSCPU
*cpu
= MIPS_CPU(cs
);
140 struct kvm_mips_interrupt intr
;
142 qemu_mutex_lock_iothread();
144 if ((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
145 cpu_mips_io_interrupts_pending(cpu
)) {
148 r
= kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
150 error_report("%s: cpu %d: failed to inject IRQ %x",
151 __func__
, cs
->cpu_index
, intr
.irq
);
155 qemu_mutex_unlock_iothread();
158 MemTxAttrs
kvm_arch_post_run(CPUState
*cs
, struct kvm_run
*run
)
160 return MEMTXATTRS_UNSPECIFIED
;
163 int kvm_arch_process_async_events(CPUState
*cs
)
168 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
172 DPRINTF("%s\n", __func__
);
173 switch (run
->exit_reason
) {
175 error_report("%s: unknown exit reason %d",
176 __func__
, run
->exit_reason
);
184 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
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 assert(kvm_enabled());
209 kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
214 int kvm_mips_set_ipi_interrupt(MIPSCPU
*cpu
, int irq
, int level
)
216 CPUState
*cs
= current_cpu
;
217 CPUState
*dest_cs
= CPU(cpu
);
218 struct kvm_mips_interrupt intr
;
220 assert(kvm_enabled());
222 intr
.cpu
= dest_cs
->cpu_index
;
230 DPRINTF("%s: CPU %d, IRQ: %d\n", __func__
, intr
.cpu
, intr
.irq
);
232 kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
237 #define MIPS_CP0_32(_R, _S) \
238 (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U32 | (8 * (_R) + (_S)))
240 #define MIPS_CP0_64(_R, _S) \
241 (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U64 | (8 * (_R) + (_S)))
243 #define KVM_REG_MIPS_CP0_INDEX MIPS_CP0_32(0, 0)
244 #define KVM_REG_MIPS_CP0_RANDOM MIPS_CP0_32(1, 0)
245 #define KVM_REG_MIPS_CP0_CONTEXT MIPS_CP0_64(4, 0)
246 #define KVM_REG_MIPS_CP0_USERLOCAL MIPS_CP0_64(4, 2)
247 #define KVM_REG_MIPS_CP0_PAGEMASK MIPS_CP0_32(5, 0)
248 #define KVM_REG_MIPS_CP0_PAGEGRAIN MIPS_CP0_32(5, 1)
249 #define KVM_REG_MIPS_CP0_PWBASE MIPS_CP0_64(5, 5)
250 #define KVM_REG_MIPS_CP0_PWFIELD MIPS_CP0_64(5, 6)
251 #define KVM_REG_MIPS_CP0_PWSIZE MIPS_CP0_64(5, 7)
252 #define KVM_REG_MIPS_CP0_WIRED MIPS_CP0_32(6, 0)
253 #define KVM_REG_MIPS_CP0_PWCTL MIPS_CP0_32(6, 6)
254 #define KVM_REG_MIPS_CP0_HWRENA MIPS_CP0_32(7, 0)
255 #define KVM_REG_MIPS_CP0_BADVADDR MIPS_CP0_64(8, 0)
256 #define KVM_REG_MIPS_CP0_COUNT MIPS_CP0_32(9, 0)
257 #define KVM_REG_MIPS_CP0_ENTRYHI MIPS_CP0_64(10, 0)
258 #define KVM_REG_MIPS_CP0_COMPARE MIPS_CP0_32(11, 0)
259 #define KVM_REG_MIPS_CP0_STATUS MIPS_CP0_32(12, 0)
260 #define KVM_REG_MIPS_CP0_CAUSE MIPS_CP0_32(13, 0)
261 #define KVM_REG_MIPS_CP0_EPC MIPS_CP0_64(14, 0)
262 #define KVM_REG_MIPS_CP0_PRID MIPS_CP0_32(15, 0)
263 #define KVM_REG_MIPS_CP0_EBASE MIPS_CP0_64(15, 1)
264 #define KVM_REG_MIPS_CP0_CONFIG MIPS_CP0_32(16, 0)
265 #define KVM_REG_MIPS_CP0_CONFIG1 MIPS_CP0_32(16, 1)
266 #define KVM_REG_MIPS_CP0_CONFIG2 MIPS_CP0_32(16, 2)
267 #define KVM_REG_MIPS_CP0_CONFIG3 MIPS_CP0_32(16, 3)
268 #define KVM_REG_MIPS_CP0_CONFIG4 MIPS_CP0_32(16, 4)
269 #define KVM_REG_MIPS_CP0_CONFIG5 MIPS_CP0_32(16, 5)
270 #define KVM_REG_MIPS_CP0_CONFIG6 MIPS_CP0_32(16, 6)
271 #define KVM_REG_MIPS_CP0_XCONTEXT MIPS_CP0_64(20, 0)
272 #define KVM_REG_MIPS_CP0_ERROREPC MIPS_CP0_64(30, 0)
273 #define KVM_REG_MIPS_CP0_KSCRATCH1 MIPS_CP0_64(31, 2)
274 #define KVM_REG_MIPS_CP0_KSCRATCH2 MIPS_CP0_64(31, 3)
275 #define KVM_REG_MIPS_CP0_KSCRATCH3 MIPS_CP0_64(31, 4)
276 #define KVM_REG_MIPS_CP0_KSCRATCH4 MIPS_CP0_64(31, 5)
277 #define KVM_REG_MIPS_CP0_KSCRATCH5 MIPS_CP0_64(31, 6)
278 #define KVM_REG_MIPS_CP0_KSCRATCH6 MIPS_CP0_64(31, 7)
280 static inline int kvm_mips_put_one_reg(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_ureg(CPUState
*cs
, uint64_t reg_id
,
294 struct kvm_one_reg cp0reg
= {
296 .addr
= (uintptr_t)addr
299 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
302 static inline int kvm_mips_put_one_ulreg(CPUState
*cs
, uint64_t reg_id
,
305 uint64_t val64
= *addr
;
306 struct kvm_one_reg cp0reg
= {
308 .addr
= (uintptr_t)&val64
311 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
314 static inline int kvm_mips_put_one_reg64(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_put_one_ureg64(CPUState
*cs
, uint64_t reg_id
,
328 struct kvm_one_reg cp0reg
= {
330 .addr
= (uintptr_t)addr
333 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
336 static inline int kvm_mips_get_one_reg(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_ureg(CPUState
*cs
, uint64_t reg_id
,
350 struct kvm_one_reg cp0reg
= {
352 .addr
= (uintptr_t)addr
355 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
358 static inline int kvm_mips_get_one_ulreg(CPUState
*cs
, uint64_t reg_id
,
363 struct kvm_one_reg cp0reg
= {
365 .addr
= (uintptr_t)&val64
368 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
375 static inline int kvm_mips_get_one_reg64(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 static inline int kvm_mips_get_one_ureg64(CPUState
*cs
, uint64_t reg_id
,
389 struct kvm_one_reg cp0reg
= {
391 .addr
= (uintptr_t)addr
394 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
397 #define KVM_REG_MIPS_CP0_CONFIG_MASK (1U << CP0C0_M)
398 #define KVM_REG_MIPS_CP0_CONFIG1_MASK ((1U << CP0C1_M) | \
400 #define KVM_REG_MIPS_CP0_CONFIG2_MASK (1U << CP0C2_M)
401 #define KVM_REG_MIPS_CP0_CONFIG3_MASK ((1U << CP0C3_M) | \
403 #define KVM_REG_MIPS_CP0_CONFIG4_MASK (1U << CP0C4_M)
404 #define KVM_REG_MIPS_CP0_CONFIG5_MASK ((1U << CP0C5_MSAEn) | \
405 (1U << CP0C5_UFE) | \
406 (1U << CP0C5_FRE) | \
408 #define KVM_REG_MIPS_CP0_CONFIG6_MASK ((1U << CP0C6_BPPASS) | \
409 (0x3fU << CP0C6_KPOS) | \
411 (1U << CP0C6_VTLBONLY) | \
412 (1U << CP0C6_LASX) | \
413 (1U << CP0C6_SSEN) | \
414 (1U << CP0C6_DISDRTIME) | \
415 (1U << CP0C6_PIXNUEN) | \
416 (1U << CP0C6_SCRAND) | \
417 (1U << CP0C6_LLEXCEN) | \
418 (1U << CP0C6_DISVC) | \
419 (1U << CP0C6_VCLRU) | \
420 (1U << CP0C6_DCLRU) | \
421 (1U << CP0C6_PIXUEN) | \
422 (1U << CP0C6_DISBLKLYEN) | \
423 (1U << CP0C6_UMEMUALEN) | \
424 (1U << CP0C6_SFBEN) | \
425 (1U << CP0C6_FLTINT) | \
426 (1U << CP0C6_VLTINT) | \
427 (1U << CP0C6_DISBTB) | \
428 (3U << CP0C6_STPREFCTL) | \
429 (1U << CP0C6_INSTPREF) | \
430 (1U << CP0C6_DATAPREF))
432 static inline int kvm_mips_change_one_reg(CPUState
*cs
, uint64_t reg_id
,
433 int32_t *addr
, int32_t mask
)
438 err
= kvm_mips_get_one_reg(cs
, reg_id
, &tmp
);
443 /* only change bits in mask */
444 change
= (*addr
^ tmp
) & mask
;
450 return kvm_mips_put_one_reg(cs
, reg_id
, &tmp
);
454 * We freeze the KVM timer when either the VM clock is stopped or the state is
455 * saved (the state is dirty).
459 * Save the state of the KVM timer when VM clock is stopped or state is synced
462 static int kvm_mips_save_count(CPUState
*cs
)
464 MIPSCPU
*cpu
= MIPS_CPU(cs
);
465 CPUMIPSState
*env
= &cpu
->env
;
469 /* freeze KVM timer */
470 err
= kvm_mips_get_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
472 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__
, err
);
474 } else if (!(count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
)) {
475 count_ctl
|= KVM_REG_MIPS_COUNT_CTL_DC
;
476 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
478 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__
, err
);
484 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CAUSE
, &env
->CP0_Cause
);
486 DPRINTF("%s: Failed to get CP0_CAUSE (%d)\n", __func__
, err
);
491 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_COUNT
, &env
->CP0_Count
);
493 DPRINTF("%s: Failed to get CP0_COUNT (%d)\n", __func__
, err
);
501 * Restore the state of the KVM timer when VM clock is restarted or state is
504 static int kvm_mips_restore_count(CPUState
*cs
)
506 MIPSCPU
*cpu
= MIPS_CPU(cs
);
507 CPUMIPSState
*env
= &cpu
->env
;
509 int err_dc
, err
, ret
= 0;
511 /* check the timer is frozen */
512 err_dc
= kvm_mips_get_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
514 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__
, err_dc
);
516 } else if (!(count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
)) {
517 /* freeze timer (sets COUNT_RESUME for us) */
518 count_ctl
|= KVM_REG_MIPS_COUNT_CTL_DC
;
519 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
521 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__
, err
);
527 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_CAUSE
, &env
->CP0_Cause
);
529 DPRINTF("%s: Failed to put CP0_CAUSE (%d)\n", __func__
, err
);
534 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_COUNT
, &env
->CP0_Count
);
536 DPRINTF("%s: Failed to put CP0_COUNT (%d)\n", __func__
, err
);
540 /* resume KVM timer */
542 count_ctl
&= ~KVM_REG_MIPS_COUNT_CTL_DC
;
543 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
545 DPRINTF("%s: Failed to set COUNT_CTL.DC=0 (%d)\n", __func__
, err
);
554 * Handle the VM clock being started or stopped
556 static void kvm_mips_update_state(void *opaque
, bool running
, RunState state
)
558 CPUState
*cs
= opaque
;
560 uint64_t count_resume
;
563 * If state is already dirty (synced to QEMU) then the KVM timer state is
564 * already saved and can be restored when it is synced back to KVM.
567 if (!cs
->vcpu_dirty
) {
568 ret
= kvm_mips_save_count(cs
);
570 warn_report("Failed saving count");
574 /* Set clock restore time to now */
575 count_resume
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
576 ret
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_RESUME
,
579 warn_report("Failed setting COUNT_RESUME");
583 if (!cs
->vcpu_dirty
) {
584 ret
= kvm_mips_restore_count(cs
);
586 warn_report("Failed restoring count");
592 static int kvm_mips_put_fpu_registers(CPUState
*cs
, int level
)
594 MIPSCPU
*cpu
= MIPS_CPU(cs
);
595 CPUMIPSState
*env
= &cpu
->env
;
599 /* Only put FPU state if we're emulating a CPU with an FPU */
600 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
601 /* FPU Control Registers */
602 if (level
== KVM_PUT_FULL_STATE
) {
603 err
= kvm_mips_put_one_ureg(cs
, KVM_REG_MIPS_FCR_IR
,
604 &env
->active_fpu
.fcr0
);
606 DPRINTF("%s: Failed to put FCR_IR (%d)\n", __func__
, err
);
610 err
= kvm_mips_put_one_ureg(cs
, KVM_REG_MIPS_FCR_CSR
,
611 &env
->active_fpu
.fcr31
);
613 DPRINTF("%s: Failed to put FCR_CSR (%d)\n", __func__
, err
);
618 * FPU register state is a subset of MSA vector state, so don't put FPU
619 * registers if we're emulating a CPU with MSA.
621 if (!ase_msa_available(env
)) {
622 /* Floating point registers */
623 for (i
= 0; i
< 32; ++i
) {
624 if (env
->CP0_Status
& (1 << CP0St_FR
)) {
625 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_FPR_64(i
),
626 &env
->active_fpu
.fpr
[i
].d
);
628 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FPR_32(i
),
629 &env
->active_fpu
.fpr
[i
].w
[FP_ENDIAN_IDX
]);
632 DPRINTF("%s: Failed to put FPR%u (%d)\n", __func__
, i
, err
);
639 /* Only put MSA state if we're emulating a CPU with MSA */
640 if (ase_msa_available(env
)) {
641 /* MSA Control Registers */
642 if (level
== KVM_PUT_FULL_STATE
) {
643 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_MSA_IR
,
646 DPRINTF("%s: Failed to put MSA_IR (%d)\n", __func__
, err
);
650 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_MSA_CSR
,
651 &env
->active_tc
.msacsr
);
653 DPRINTF("%s: Failed to put MSA_CSR (%d)\n", __func__
, err
);
657 /* Vector registers (includes FP registers) */
658 for (i
= 0; i
< 32; ++i
) {
659 /* Big endian MSA not supported by QEMU yet anyway */
660 err
= kvm_mips_put_one_reg64(cs
, KVM_REG_MIPS_VEC_128(i
),
661 env
->active_fpu
.fpr
[i
].wr
.d
);
663 DPRINTF("%s: Failed to put VEC%u (%d)\n", __func__
, i
, err
);
672 static int kvm_mips_get_fpu_registers(CPUState
*cs
)
674 MIPSCPU
*cpu
= MIPS_CPU(cs
);
675 CPUMIPSState
*env
= &cpu
->env
;
679 /* Only get FPU state if we're emulating a CPU with an FPU */
680 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
681 /* FPU Control Registers */
682 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FCR_IR
,
683 &env
->active_fpu
.fcr0
);
685 DPRINTF("%s: Failed to get FCR_IR (%d)\n", __func__
, err
);
688 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FCR_CSR
,
689 &env
->active_fpu
.fcr31
);
691 DPRINTF("%s: Failed to get FCR_CSR (%d)\n", __func__
, err
);
694 restore_fp_status(env
);
698 * FPU register state is a subset of MSA vector state, so don't save FPU
699 * registers if we're emulating a CPU with MSA.
701 if (!ase_msa_available(env
)) {
702 /* Floating point registers */
703 for (i
= 0; i
< 32; ++i
) {
704 if (env
->CP0_Status
& (1 << CP0St_FR
)) {
705 err
= kvm_mips_get_one_ureg64(cs
, KVM_REG_MIPS_FPR_64(i
),
706 &env
->active_fpu
.fpr
[i
].d
);
708 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FPR_32(i
),
709 &env
->active_fpu
.fpr
[i
].w
[FP_ENDIAN_IDX
]);
712 DPRINTF("%s: Failed to get FPR%u (%d)\n", __func__
, i
, err
);
719 /* Only get MSA state if we're emulating a CPU with MSA */
720 if (ase_msa_available(env
)) {
721 /* MSA Control Registers */
722 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_MSA_IR
,
725 DPRINTF("%s: Failed to get MSA_IR (%d)\n", __func__
, err
);
728 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_MSA_CSR
,
729 &env
->active_tc
.msacsr
);
731 DPRINTF("%s: Failed to get MSA_CSR (%d)\n", __func__
, err
);
734 restore_msa_fp_status(env
);
737 /* Vector registers (includes FP registers) */
738 for (i
= 0; i
< 32; ++i
) {
739 /* Big endian MSA not supported by QEMU yet anyway */
740 err
= kvm_mips_get_one_reg64(cs
, KVM_REG_MIPS_VEC_128(i
),
741 env
->active_fpu
.fpr
[i
].wr
.d
);
743 DPRINTF("%s: Failed to get VEC%u (%d)\n", __func__
, i
, err
);
753 static int kvm_mips_put_cp0_registers(CPUState
*cs
, int level
)
755 MIPSCPU
*cpu
= MIPS_CPU(cs
);
756 CPUMIPSState
*env
= &cpu
->env
;
761 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_INDEX
, &env
->CP0_Index
);
763 DPRINTF("%s: Failed to put CP0_INDEX (%d)\n", __func__
, err
);
766 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_RANDOM
, &env
->CP0_Random
);
768 DPRINTF("%s: Failed to put CP0_RANDOM (%d)\n", __func__
, err
);
771 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_CONTEXT
,
774 DPRINTF("%s: Failed to put CP0_CONTEXT (%d)\n", __func__
, err
);
777 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_USERLOCAL
,
778 &env
->active_tc
.CP0_UserLocal
);
780 DPRINTF("%s: Failed to put CP0_USERLOCAL (%d)\n", __func__
, err
);
783 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEMASK
,
786 DPRINTF("%s: Failed to put CP0_PAGEMASK (%d)\n", __func__
, err
);
789 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEGRAIN
,
790 &env
->CP0_PageGrain
);
792 DPRINTF("%s: Failed to put CP0_PAGEGRAIN (%d)\n", __func__
, err
);
795 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_PWBASE
,
798 DPRINTF("%s: Failed to put CP0_PWBASE (%d)\n", __func__
, err
);
801 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_PWFIELD
,
804 DPRINTF("%s: Failed to put CP0_PWField (%d)\n", __func__
, err
);
807 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_PWSIZE
,
810 DPRINTF("%s: Failed to put CP0_PWSIZE (%d)\n", __func__
, err
);
813 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_WIRED
, &env
->CP0_Wired
);
815 DPRINTF("%s: Failed to put CP0_WIRED (%d)\n", __func__
, err
);
818 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_PWCTL
, &env
->CP0_PWCtl
);
820 DPRINTF("%s: Failed to put CP0_PWCTL (%d)\n", __func__
, err
);
823 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_HWRENA
, &env
->CP0_HWREna
);
825 DPRINTF("%s: Failed to put CP0_HWRENA (%d)\n", __func__
, err
);
828 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_BADVADDR
,
831 DPRINTF("%s: Failed to put CP0_BADVADDR (%d)\n", __func__
, err
);
835 /* If VM clock stopped then state will be restored when it is restarted */
836 if (runstate_is_running()) {
837 err
= kvm_mips_restore_count(cs
);
843 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_ENTRYHI
,
846 DPRINTF("%s: Failed to put CP0_ENTRYHI (%d)\n", __func__
, err
);
849 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_COMPARE
,
852 DPRINTF("%s: Failed to put CP0_COMPARE (%d)\n", __func__
, err
);
855 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_STATUS
, &env
->CP0_Status
);
857 DPRINTF("%s: Failed to put CP0_STATUS (%d)\n", __func__
, err
);
860 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_EPC
, &env
->CP0_EPC
);
862 DPRINTF("%s: Failed to put CP0_EPC (%d)\n", __func__
, err
);
865 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_PRID
, &env
->CP0_PRid
);
867 DPRINTF("%s: Failed to put CP0_PRID (%d)\n", __func__
, err
);
870 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_EBASE
, &env
->CP0_EBase
);
872 DPRINTF("%s: Failed to put CP0_EBASE (%d)\n", __func__
, err
);
875 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG
,
877 KVM_REG_MIPS_CP0_CONFIG_MASK
);
879 DPRINTF("%s: Failed to change CP0_CONFIG (%d)\n", __func__
, err
);
882 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG1
,
884 KVM_REG_MIPS_CP0_CONFIG1_MASK
);
886 DPRINTF("%s: Failed to change CP0_CONFIG1 (%d)\n", __func__
, err
);
889 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG2
,
891 KVM_REG_MIPS_CP0_CONFIG2_MASK
);
893 DPRINTF("%s: Failed to change CP0_CONFIG2 (%d)\n", __func__
, err
);
896 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG3
,
898 KVM_REG_MIPS_CP0_CONFIG3_MASK
);
900 DPRINTF("%s: Failed to change CP0_CONFIG3 (%d)\n", __func__
, err
);
903 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG4
,
905 KVM_REG_MIPS_CP0_CONFIG4_MASK
);
907 DPRINTF("%s: Failed to change CP0_CONFIG4 (%d)\n", __func__
, err
);
910 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG5
,
912 KVM_REG_MIPS_CP0_CONFIG5_MASK
);
914 DPRINTF("%s: Failed to change CP0_CONFIG5 (%d)\n", __func__
, err
);
917 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG6
,
919 KVM_REG_MIPS_CP0_CONFIG6_MASK
);
921 DPRINTF("%s: Failed to change CP0_CONFIG6 (%d)\n", __func__
, err
);
924 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_XCONTEXT
,
927 DPRINTF("%s: Failed to put CP0_XCONTEXT (%d)\n", __func__
, err
);
930 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_ERROREPC
,
933 DPRINTF("%s: Failed to put CP0_ERROREPC (%d)\n", __func__
, err
);
936 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH1
,
937 &env
->CP0_KScratch
[0]);
939 DPRINTF("%s: Failed to put CP0_KSCRATCH1 (%d)\n", __func__
, err
);
942 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH2
,
943 &env
->CP0_KScratch
[1]);
945 DPRINTF("%s: Failed to put CP0_KSCRATCH2 (%d)\n", __func__
, err
);
948 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH3
,
949 &env
->CP0_KScratch
[2]);
951 DPRINTF("%s: Failed to put CP0_KSCRATCH3 (%d)\n", __func__
, err
);
954 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH4
,
955 &env
->CP0_KScratch
[3]);
957 DPRINTF("%s: Failed to put CP0_KSCRATCH4 (%d)\n", __func__
, err
);
960 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH5
,
961 &env
->CP0_KScratch
[4]);
963 DPRINTF("%s: Failed to put CP0_KSCRATCH5 (%d)\n", __func__
, err
);
966 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH6
,
967 &env
->CP0_KScratch
[5]);
969 DPRINTF("%s: Failed to put CP0_KSCRATCH6 (%d)\n", __func__
, err
);
976 static int kvm_mips_get_cp0_registers(CPUState
*cs
)
978 MIPSCPU
*cpu
= MIPS_CPU(cs
);
979 CPUMIPSState
*env
= &cpu
->env
;
982 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_INDEX
, &env
->CP0_Index
);
984 DPRINTF("%s: Failed to get CP0_INDEX (%d)\n", __func__
, err
);
987 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_RANDOM
, &env
->CP0_Random
);
989 DPRINTF("%s: Failed to get CP0_RANDOM (%d)\n", __func__
, err
);
992 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_CONTEXT
,
995 DPRINTF("%s: Failed to get CP0_CONTEXT (%d)\n", __func__
, err
);
998 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_USERLOCAL
,
999 &env
->active_tc
.CP0_UserLocal
);
1001 DPRINTF("%s: Failed to get CP0_USERLOCAL (%d)\n", __func__
, err
);
1004 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEMASK
,
1005 &env
->CP0_PageMask
);
1007 DPRINTF("%s: Failed to get CP0_PAGEMASK (%d)\n", __func__
, err
);
1010 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEGRAIN
,
1011 &env
->CP0_PageGrain
);
1013 DPRINTF("%s: Failed to get CP0_PAGEGRAIN (%d)\n", __func__
, err
);
1016 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_PWBASE
,
1019 DPRINTF("%s: Failed to get CP0_PWBASE (%d)\n", __func__
, err
);
1022 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_PWFIELD
,
1025 DPRINTF("%s: Failed to get CP0_PWFIELD (%d)\n", __func__
, err
);
1028 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_PWSIZE
,
1031 DPRINTF("%s: Failed to get CP0_PWSIZE (%d)\n", __func__
, err
);
1034 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_WIRED
, &env
->CP0_Wired
);
1036 DPRINTF("%s: Failed to get CP0_WIRED (%d)\n", __func__
, err
);
1039 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_PWCTL
, &env
->CP0_PWCtl
);
1041 DPRINTF("%s: Failed to get CP0_PWCtl (%d)\n", __func__
, err
);
1044 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_HWRENA
, &env
->CP0_HWREna
);
1046 DPRINTF("%s: Failed to get CP0_HWRENA (%d)\n", __func__
, err
);
1049 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_BADVADDR
,
1050 &env
->CP0_BadVAddr
);
1052 DPRINTF("%s: Failed to get CP0_BADVADDR (%d)\n", __func__
, err
);
1055 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_ENTRYHI
,
1058 DPRINTF("%s: Failed to get CP0_ENTRYHI (%d)\n", __func__
, err
);
1061 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_COMPARE
,
1064 DPRINTF("%s: Failed to get CP0_COMPARE (%d)\n", __func__
, err
);
1067 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_STATUS
, &env
->CP0_Status
);
1069 DPRINTF("%s: Failed to get CP0_STATUS (%d)\n", __func__
, err
);
1073 /* If VM clock stopped then state was already saved when it was stopped */
1074 if (runstate_is_running()) {
1075 err
= kvm_mips_save_count(cs
);
1081 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_EPC
, &env
->CP0_EPC
);
1083 DPRINTF("%s: Failed to get CP0_EPC (%d)\n", __func__
, err
);
1086 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_PRID
, &env
->CP0_PRid
);
1088 DPRINTF("%s: Failed to get CP0_PRID (%d)\n", __func__
, err
);
1091 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_EBASE
, &env
->CP0_EBase
);
1093 DPRINTF("%s: Failed to get CP0_EBASE (%d)\n", __func__
, err
);
1096 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG
, &env
->CP0_Config0
);
1098 DPRINTF("%s: Failed to get CP0_CONFIG (%d)\n", __func__
, err
);
1101 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG1
, &env
->CP0_Config1
);
1103 DPRINTF("%s: Failed to get CP0_CONFIG1 (%d)\n", __func__
, err
);
1106 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG2
, &env
->CP0_Config2
);
1108 DPRINTF("%s: Failed to get CP0_CONFIG2 (%d)\n", __func__
, err
);
1111 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG3
, &env
->CP0_Config3
);
1113 DPRINTF("%s: Failed to get CP0_CONFIG3 (%d)\n", __func__
, err
);
1116 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG4
, &env
->CP0_Config4
);
1118 DPRINTF("%s: Failed to get CP0_CONFIG4 (%d)\n", __func__
, err
);
1121 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG5
, &env
->CP0_Config5
);
1123 DPRINTF("%s: Failed to get CP0_CONFIG5 (%d)\n", __func__
, err
);
1126 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG6
, &env
->CP0_Config6
);
1128 DPRINTF("%s: Failed to get CP0_CONFIG6 (%d)\n", __func__
, err
);
1131 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_XCONTEXT
,
1132 &env
->CP0_XContext
);
1134 DPRINTF("%s: Failed to get CP0_XCONTEXT (%d)\n", __func__
, err
);
1137 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_ERROREPC
,
1138 &env
->CP0_ErrorEPC
);
1140 DPRINTF("%s: Failed to get CP0_ERROREPC (%d)\n", __func__
, err
);
1143 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH1
,
1144 &env
->CP0_KScratch
[0]);
1146 DPRINTF("%s: Failed to get CP0_KSCRATCH1 (%d)\n", __func__
, err
);
1149 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH2
,
1150 &env
->CP0_KScratch
[1]);
1152 DPRINTF("%s: Failed to get CP0_KSCRATCH2 (%d)\n", __func__
, err
);
1155 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH3
,
1156 &env
->CP0_KScratch
[2]);
1158 DPRINTF("%s: Failed to get CP0_KSCRATCH3 (%d)\n", __func__
, err
);
1161 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH4
,
1162 &env
->CP0_KScratch
[3]);
1164 DPRINTF("%s: Failed to get CP0_KSCRATCH4 (%d)\n", __func__
, err
);
1167 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH5
,
1168 &env
->CP0_KScratch
[4]);
1170 DPRINTF("%s: Failed to get CP0_KSCRATCH5 (%d)\n", __func__
, err
);
1173 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH6
,
1174 &env
->CP0_KScratch
[5]);
1176 DPRINTF("%s: Failed to get CP0_KSCRATCH6 (%d)\n", __func__
, err
);
1183 int kvm_arch_put_registers(CPUState
*cs
, int level
)
1185 MIPSCPU
*cpu
= MIPS_CPU(cs
);
1186 CPUMIPSState
*env
= &cpu
->env
;
1187 struct kvm_regs regs
;
1191 /* Set the registers based on QEMU's view of things */
1192 for (i
= 0; i
< 32; i
++) {
1193 regs
.gpr
[i
] = (int64_t)(target_long
)env
->active_tc
.gpr
[i
];
1196 regs
.hi
= (int64_t)(target_long
)env
->active_tc
.HI
[0];
1197 regs
.lo
= (int64_t)(target_long
)env
->active_tc
.LO
[0];
1198 regs
.pc
= (int64_t)(target_long
)env
->active_tc
.PC
;
1200 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_REGS
, ®s
);
1206 ret
= kvm_mips_put_cp0_registers(cs
, level
);
1211 ret
= kvm_mips_put_fpu_registers(cs
, level
);
1219 int kvm_arch_get_registers(CPUState
*cs
)
1221 MIPSCPU
*cpu
= MIPS_CPU(cs
);
1222 CPUMIPSState
*env
= &cpu
->env
;
1224 struct kvm_regs regs
;
1227 /* Get the current register set as KVM seems it */
1228 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_REGS
, ®s
);
1234 for (i
= 0; i
< 32; i
++) {
1235 env
->active_tc
.gpr
[i
] = regs
.gpr
[i
];
1238 env
->active_tc
.HI
[0] = regs
.hi
;
1239 env
->active_tc
.LO
[0] = regs
.lo
;
1240 env
->active_tc
.PC
= regs
.pc
;
1242 kvm_mips_get_cp0_registers(cs
);
1243 kvm_mips_get_fpu_registers(cs
);
1248 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
1249 uint64_t address
, uint32_t data
, PCIDevice
*dev
)
1254 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry
*route
,
1255 int vector
, PCIDevice
*dev
)
1260 int kvm_arch_release_virq_post(int virq
)
1265 int kvm_arch_msi_data_to_gsi(uint32_t data
)
1270 int mips_kvm_type(MachineState
*machine
, const char *vm_type
)
1272 #if defined(KVM_CAP_MIPS_VZ) || defined(KVM_CAP_MIPS_TE)
1274 KVMState
*s
= KVM_STATE(machine
->accelerator
);
1277 #if defined(KVM_CAP_MIPS_VZ)
1278 r
= kvm_check_extension(s
, KVM_CAP_MIPS_VZ
);
1280 return KVM_VM_MIPS_VZ
;
1284 #if defined(KVM_CAP_MIPS_TE)
1285 r
= kvm_check_extension(s
, KVM_CAP_MIPS_TE
);
1287 return KVM_VM_MIPS_TE
;
1294 bool kvm_arch_cpu_check_are_resettable(void)