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>
19 #include "qemu/error-report.h"
20 #include "qemu/main-loop.h"
21 #include "sysemu/kvm.h"
22 #include "sysemu/kvm_int.h"
23 #include "sysemu/runstate.h"
25 #include "hw/boards.h"
26 #include "fpu_helper.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
, bool 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(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
&& ase_msa_available(env
)) {
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 int kvm_arch_destroy_vcpu(CPUState
*cs
)
99 void kvm_mips_reset_vcpu(MIPSCPU
*cpu
)
101 CPUMIPSState
*env
= &cpu
->env
;
103 if (!kvm_mips_fpu_cap
&& env
->CP0_Config1
& (1 << CP0C1_FP
)) {
104 warn_report("KVM does not support FPU, disabling");
105 env
->CP0_Config1
&= ~(1 << CP0C1_FP
);
107 if (!kvm_mips_msa_cap
&& ase_msa_available(env
)) {
108 warn_report("KVM does not support MSA, disabling");
109 env
->CP0_Config3
&= ~(1 << CP0C3_MSAP
);
112 DPRINTF("%s\n", __func__
);
115 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
117 DPRINTF("%s\n", __func__
);
121 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
123 DPRINTF("%s\n", __func__
);
127 static inline int cpu_mips_io_interrupts_pending(MIPSCPU
*cpu
)
129 CPUMIPSState
*env
= &cpu
->env
;
131 return env
->CP0_Cause
& (0x1 << (2 + CP0Ca_IP
));
135 void kvm_arch_pre_run(CPUState
*cs
, struct kvm_run
*run
)
137 MIPSCPU
*cpu
= MIPS_CPU(cs
);
139 struct kvm_mips_interrupt intr
;
141 qemu_mutex_lock_iothread();
143 if ((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
144 cpu_mips_io_interrupts_pending(cpu
)) {
147 r
= kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
149 error_report("%s: cpu %d: failed to inject IRQ %x",
150 __func__
, cs
->cpu_index
, intr
.irq
);
154 qemu_mutex_unlock_iothread();
157 MemTxAttrs
kvm_arch_post_run(CPUState
*cs
, struct kvm_run
*run
)
159 return MEMTXATTRS_UNSPECIFIED
;
162 int kvm_arch_process_async_events(CPUState
*cs
)
167 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
171 DPRINTF("%s\n", __func__
);
172 switch (run
->exit_reason
) {
174 error_report("%s: unknown exit reason %d",
175 __func__
, run
->exit_reason
);
183 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
185 DPRINTF("%s\n", __func__
);
189 void kvm_arch_init_irq_routing(KVMState
*s
)
193 int kvm_mips_set_interrupt(MIPSCPU
*cpu
, int irq
, int level
)
195 CPUState
*cs
= CPU(cpu
);
196 struct kvm_mips_interrupt intr
;
198 assert(kvm_enabled());
208 kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
213 int kvm_mips_set_ipi_interrupt(MIPSCPU
*cpu
, int irq
, int level
)
215 CPUState
*cs
= current_cpu
;
216 CPUState
*dest_cs
= CPU(cpu
);
217 struct kvm_mips_interrupt intr
;
219 assert(kvm_enabled());
221 intr
.cpu
= dest_cs
->cpu_index
;
229 DPRINTF("%s: CPU %d, IRQ: %d\n", __func__
, intr
.cpu
, intr
.irq
);
231 kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
236 #define MIPS_CP0_32(_R, _S) \
237 (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U32 | (8 * (_R) + (_S)))
239 #define MIPS_CP0_64(_R, _S) \
240 (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U64 | (8 * (_R) + (_S)))
242 #define KVM_REG_MIPS_CP0_INDEX MIPS_CP0_32(0, 0)
243 #define KVM_REG_MIPS_CP0_RANDOM MIPS_CP0_32(1, 0)
244 #define KVM_REG_MIPS_CP0_CONTEXT MIPS_CP0_64(4, 0)
245 #define KVM_REG_MIPS_CP0_USERLOCAL MIPS_CP0_64(4, 2)
246 #define KVM_REG_MIPS_CP0_PAGEMASK MIPS_CP0_32(5, 0)
247 #define KVM_REG_MIPS_CP0_PAGEGRAIN MIPS_CP0_32(5, 1)
248 #define KVM_REG_MIPS_CP0_PWBASE MIPS_CP0_64(5, 5)
249 #define KVM_REG_MIPS_CP0_PWFIELD MIPS_CP0_64(5, 6)
250 #define KVM_REG_MIPS_CP0_PWSIZE MIPS_CP0_64(5, 7)
251 #define KVM_REG_MIPS_CP0_WIRED MIPS_CP0_32(6, 0)
252 #define KVM_REG_MIPS_CP0_PWCTL MIPS_CP0_32(6, 6)
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_EBASE MIPS_CP0_64(15, 1)
263 #define KVM_REG_MIPS_CP0_CONFIG MIPS_CP0_32(16, 0)
264 #define KVM_REG_MIPS_CP0_CONFIG1 MIPS_CP0_32(16, 1)
265 #define KVM_REG_MIPS_CP0_CONFIG2 MIPS_CP0_32(16, 2)
266 #define KVM_REG_MIPS_CP0_CONFIG3 MIPS_CP0_32(16, 3)
267 #define KVM_REG_MIPS_CP0_CONFIG4 MIPS_CP0_32(16, 4)
268 #define KVM_REG_MIPS_CP0_CONFIG5 MIPS_CP0_32(16, 5)
269 #define KVM_REG_MIPS_CP0_CONFIG6 MIPS_CP0_32(16, 6)
270 #define KVM_REG_MIPS_CP0_XCONTEXT MIPS_CP0_64(20, 0)
271 #define KVM_REG_MIPS_CP0_ERROREPC MIPS_CP0_64(30, 0)
272 #define KVM_REG_MIPS_CP0_KSCRATCH1 MIPS_CP0_64(31, 2)
273 #define KVM_REG_MIPS_CP0_KSCRATCH2 MIPS_CP0_64(31, 3)
274 #define KVM_REG_MIPS_CP0_KSCRATCH3 MIPS_CP0_64(31, 4)
275 #define KVM_REG_MIPS_CP0_KSCRATCH4 MIPS_CP0_64(31, 5)
276 #define KVM_REG_MIPS_CP0_KSCRATCH5 MIPS_CP0_64(31, 6)
277 #define KVM_REG_MIPS_CP0_KSCRATCH6 MIPS_CP0_64(31, 7)
279 static inline int kvm_mips_put_one_reg(CPUState
*cs
, uint64_t reg_id
,
282 struct kvm_one_reg cp0reg
= {
284 .addr
= (uintptr_t)addr
287 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
290 static inline int kvm_mips_put_one_ureg(CPUState
*cs
, uint64_t reg_id
,
293 struct kvm_one_reg cp0reg
= {
295 .addr
= (uintptr_t)addr
298 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
301 static inline int kvm_mips_put_one_ulreg(CPUState
*cs
, uint64_t reg_id
,
304 uint64_t val64
= *addr
;
305 struct kvm_one_reg cp0reg
= {
307 .addr
= (uintptr_t)&val64
310 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
313 static inline int kvm_mips_put_one_reg64(CPUState
*cs
, uint64_t reg_id
,
316 struct kvm_one_reg cp0reg
= {
318 .addr
= (uintptr_t)addr
321 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
324 static inline int kvm_mips_put_one_ureg64(CPUState
*cs
, uint64_t reg_id
,
327 struct kvm_one_reg cp0reg
= {
329 .addr
= (uintptr_t)addr
332 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
335 static inline int kvm_mips_get_one_reg(CPUState
*cs
, uint64_t reg_id
,
338 struct kvm_one_reg cp0reg
= {
340 .addr
= (uintptr_t)addr
343 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
346 static inline int kvm_mips_get_one_ureg(CPUState
*cs
, uint64_t reg_id
,
349 struct kvm_one_reg cp0reg
= {
351 .addr
= (uintptr_t)addr
354 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
357 static inline int kvm_mips_get_one_ulreg(CPUState
*cs
, uint64_t reg_id
,
362 struct kvm_one_reg cp0reg
= {
364 .addr
= (uintptr_t)&val64
367 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
374 static inline int kvm_mips_get_one_reg64(CPUState
*cs
, uint64_t reg_id
,
377 struct kvm_one_reg cp0reg
= {
379 .addr
= (uintptr_t)addr
382 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
385 static inline int kvm_mips_get_one_ureg64(CPUState
*cs
, uint64_t reg_id
,
388 struct kvm_one_reg cp0reg
= {
390 .addr
= (uintptr_t)addr
393 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
396 #define KVM_REG_MIPS_CP0_CONFIG_MASK (1U << CP0C0_M)
397 #define KVM_REG_MIPS_CP0_CONFIG1_MASK ((1U << CP0C1_M) | \
399 #define KVM_REG_MIPS_CP0_CONFIG2_MASK (1U << CP0C2_M)
400 #define KVM_REG_MIPS_CP0_CONFIG3_MASK ((1U << CP0C3_M) | \
402 #define KVM_REG_MIPS_CP0_CONFIG4_MASK (1U << CP0C4_M)
403 #define KVM_REG_MIPS_CP0_CONFIG5_MASK ((1U << CP0C5_MSAEn) | \
404 (1U << CP0C5_UFE) | \
405 (1U << CP0C5_FRE) | \
407 #define KVM_REG_MIPS_CP0_CONFIG6_MASK ((1U << CP0C6_BPPASS) | \
408 (0x3fU << CP0C6_KPOS) | \
410 (1U << CP0C6_VTLBONLY) | \
411 (1U << CP0C6_LASX) | \
412 (1U << CP0C6_SSEN) | \
413 (1U << CP0C6_DISDRTIME) | \
414 (1U << CP0C6_PIXNUEN) | \
415 (1U << CP0C6_SCRAND) | \
416 (1U << CP0C6_LLEXCEN) | \
417 (1U << CP0C6_DISVC) | \
418 (1U << CP0C6_VCLRU) | \
419 (1U << CP0C6_DCLRU) | \
420 (1U << CP0C6_PIXUEN) | \
421 (1U << CP0C6_DISBLKLYEN) | \
422 (1U << CP0C6_UMEMUALEN) | \
423 (1U << CP0C6_SFBEN) | \
424 (1U << CP0C6_FLTINT) | \
425 (1U << CP0C6_VLTINT) | \
426 (1U << CP0C6_DISBTB) | \
427 (3U << CP0C6_STPREFCTL) | \
428 (1U << CP0C6_INSTPREF) | \
429 (1U << CP0C6_DATAPREF))
431 static inline int kvm_mips_change_one_reg(CPUState
*cs
, uint64_t reg_id
,
432 int32_t *addr
, int32_t mask
)
437 err
= kvm_mips_get_one_reg(cs
, reg_id
, &tmp
);
442 /* only change bits in mask */
443 change
= (*addr
^ tmp
) & mask
;
449 return kvm_mips_put_one_reg(cs
, reg_id
, &tmp
);
453 * We freeze the KVM timer when either the VM clock is stopped or the state is
454 * saved (the state is dirty).
458 * Save the state of the KVM timer when VM clock is stopped or state is synced
461 static int kvm_mips_save_count(CPUState
*cs
)
463 MIPSCPU
*cpu
= MIPS_CPU(cs
);
464 CPUMIPSState
*env
= &cpu
->env
;
468 /* freeze KVM timer */
469 err
= kvm_mips_get_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
471 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__
, err
);
473 } else if (!(count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
)) {
474 count_ctl
|= KVM_REG_MIPS_COUNT_CTL_DC
;
475 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
477 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__
, err
);
483 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CAUSE
, &env
->CP0_Cause
);
485 DPRINTF("%s: Failed to get CP0_CAUSE (%d)\n", __func__
, err
);
490 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_COUNT
, &env
->CP0_Count
);
492 DPRINTF("%s: Failed to get CP0_COUNT (%d)\n", __func__
, err
);
500 * Restore the state of the KVM timer when VM clock is restarted or state is
503 static int kvm_mips_restore_count(CPUState
*cs
)
505 MIPSCPU
*cpu
= MIPS_CPU(cs
);
506 CPUMIPSState
*env
= &cpu
->env
;
508 int err_dc
, err
, ret
= 0;
510 /* check the timer is frozen */
511 err_dc
= kvm_mips_get_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
513 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__
, err_dc
);
515 } else if (!(count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
)) {
516 /* freeze timer (sets COUNT_RESUME for us) */
517 count_ctl
|= KVM_REG_MIPS_COUNT_CTL_DC
;
518 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
520 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__
, err
);
526 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_CAUSE
, &env
->CP0_Cause
);
528 DPRINTF("%s: Failed to put CP0_CAUSE (%d)\n", __func__
, err
);
533 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_COUNT
, &env
->CP0_Count
);
535 DPRINTF("%s: Failed to put CP0_COUNT (%d)\n", __func__
, err
);
539 /* resume KVM timer */
541 count_ctl
&= ~KVM_REG_MIPS_COUNT_CTL_DC
;
542 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
544 DPRINTF("%s: Failed to set COUNT_CTL.DC=0 (%d)\n", __func__
, err
);
553 * Handle the VM clock being started or stopped
555 static void kvm_mips_update_state(void *opaque
, bool running
, RunState state
)
557 CPUState
*cs
= opaque
;
559 uint64_t count_resume
;
562 * If state is already dirty (synced to QEMU) then the KVM timer state is
563 * already saved and can be restored when it is synced back to KVM.
566 if (!cs
->vcpu_dirty
) {
567 ret
= kvm_mips_save_count(cs
);
569 warn_report("Failed saving count");
573 /* Set clock restore time to now */
574 count_resume
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
575 ret
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_COUNT_RESUME
,
578 warn_report("Failed setting COUNT_RESUME");
582 if (!cs
->vcpu_dirty
) {
583 ret
= kvm_mips_restore_count(cs
);
585 warn_report("Failed restoring count");
591 static int kvm_mips_put_fpu_registers(CPUState
*cs
, int level
)
593 MIPSCPU
*cpu
= MIPS_CPU(cs
);
594 CPUMIPSState
*env
= &cpu
->env
;
598 /* Only put FPU state if we're emulating a CPU with an FPU */
599 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
600 /* FPU Control Registers */
601 if (level
== KVM_PUT_FULL_STATE
) {
602 err
= kvm_mips_put_one_ureg(cs
, KVM_REG_MIPS_FCR_IR
,
603 &env
->active_fpu
.fcr0
);
605 DPRINTF("%s: Failed to put FCR_IR (%d)\n", __func__
, err
);
609 err
= kvm_mips_put_one_ureg(cs
, KVM_REG_MIPS_FCR_CSR
,
610 &env
->active_fpu
.fcr31
);
612 DPRINTF("%s: Failed to put FCR_CSR (%d)\n", __func__
, err
);
617 * FPU register state is a subset of MSA vector state, so don't put FPU
618 * registers if we're emulating a CPU with MSA.
620 if (!ase_msa_available(env
)) {
621 /* Floating point registers */
622 for (i
= 0; i
< 32; ++i
) {
623 if (env
->CP0_Status
& (1 << CP0St_FR
)) {
624 err
= kvm_mips_put_one_ureg64(cs
, KVM_REG_MIPS_FPR_64(i
),
625 &env
->active_fpu
.fpr
[i
].d
);
627 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FPR_32(i
),
628 &env
->active_fpu
.fpr
[i
].w
[FP_ENDIAN_IDX
]);
631 DPRINTF("%s: Failed to put FPR%u (%d)\n", __func__
, i
, err
);
638 /* Only put MSA state if we're emulating a CPU with MSA */
639 if (ase_msa_available(env
)) {
640 /* MSA Control Registers */
641 if (level
== KVM_PUT_FULL_STATE
) {
642 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_MSA_IR
,
645 DPRINTF("%s: Failed to put MSA_IR (%d)\n", __func__
, err
);
649 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_MSA_CSR
,
650 &env
->active_tc
.msacsr
);
652 DPRINTF("%s: Failed to put MSA_CSR (%d)\n", __func__
, err
);
656 /* Vector registers (includes FP registers) */
657 for (i
= 0; i
< 32; ++i
) {
658 /* Big endian MSA not supported by QEMU yet anyway */
659 err
= kvm_mips_put_one_reg64(cs
, KVM_REG_MIPS_VEC_128(i
),
660 env
->active_fpu
.fpr
[i
].wr
.d
);
662 DPRINTF("%s: Failed to put VEC%u (%d)\n", __func__
, i
, err
);
671 static int kvm_mips_get_fpu_registers(CPUState
*cs
)
673 MIPSCPU
*cpu
= MIPS_CPU(cs
);
674 CPUMIPSState
*env
= &cpu
->env
;
678 /* Only get FPU state if we're emulating a CPU with an FPU */
679 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
680 /* FPU Control Registers */
681 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FCR_IR
,
682 &env
->active_fpu
.fcr0
);
684 DPRINTF("%s: Failed to get FCR_IR (%d)\n", __func__
, err
);
687 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FCR_CSR
,
688 &env
->active_fpu
.fcr31
);
690 DPRINTF("%s: Failed to get FCR_CSR (%d)\n", __func__
, err
);
693 restore_fp_status(env
);
697 * FPU register state is a subset of MSA vector state, so don't save FPU
698 * registers if we're emulating a CPU with MSA.
700 if (!ase_msa_available(env
)) {
701 /* Floating point registers */
702 for (i
= 0; i
< 32; ++i
) {
703 if (env
->CP0_Status
& (1 << CP0St_FR
)) {
704 err
= kvm_mips_get_one_ureg64(cs
, KVM_REG_MIPS_FPR_64(i
),
705 &env
->active_fpu
.fpr
[i
].d
);
707 err
= kvm_mips_get_one_ureg(cs
, KVM_REG_MIPS_FPR_32(i
),
708 &env
->active_fpu
.fpr
[i
].w
[FP_ENDIAN_IDX
]);
711 DPRINTF("%s: Failed to get FPR%u (%d)\n", __func__
, i
, err
);
718 /* Only get MSA state if we're emulating a CPU with MSA */
719 if (ase_msa_available(env
)) {
720 /* MSA Control Registers */
721 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_MSA_IR
,
724 DPRINTF("%s: Failed to get MSA_IR (%d)\n", __func__
, err
);
727 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_MSA_CSR
,
728 &env
->active_tc
.msacsr
);
730 DPRINTF("%s: Failed to get MSA_CSR (%d)\n", __func__
, err
);
733 restore_msa_fp_status(env
);
736 /* Vector registers (includes FP registers) */
737 for (i
= 0; i
< 32; ++i
) {
738 /* Big endian MSA not supported by QEMU yet anyway */
739 err
= kvm_mips_get_one_reg64(cs
, KVM_REG_MIPS_VEC_128(i
),
740 env
->active_fpu
.fpr
[i
].wr
.d
);
742 DPRINTF("%s: Failed to get VEC%u (%d)\n", __func__
, i
, err
);
752 static int kvm_mips_put_cp0_registers(CPUState
*cs
, int level
)
754 MIPSCPU
*cpu
= MIPS_CPU(cs
);
755 CPUMIPSState
*env
= &cpu
->env
;
760 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_INDEX
, &env
->CP0_Index
);
762 DPRINTF("%s: Failed to put CP0_INDEX (%d)\n", __func__
, err
);
765 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_RANDOM
, &env
->CP0_Random
);
767 DPRINTF("%s: Failed to put CP0_RANDOM (%d)\n", __func__
, err
);
770 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_CONTEXT
,
773 DPRINTF("%s: Failed to put CP0_CONTEXT (%d)\n", __func__
, err
);
776 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_USERLOCAL
,
777 &env
->active_tc
.CP0_UserLocal
);
779 DPRINTF("%s: Failed to put CP0_USERLOCAL (%d)\n", __func__
, err
);
782 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEMASK
,
785 DPRINTF("%s: Failed to put CP0_PAGEMASK (%d)\n", __func__
, err
);
788 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEGRAIN
,
789 &env
->CP0_PageGrain
);
791 DPRINTF("%s: Failed to put CP0_PAGEGRAIN (%d)\n", __func__
, err
);
794 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_PWBASE
,
797 DPRINTF("%s: Failed to put CP0_PWBASE (%d)\n", __func__
, err
);
800 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_PWFIELD
,
803 DPRINTF("%s: Failed to put CP0_PWField (%d)\n", __func__
, err
);
806 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_PWSIZE
,
809 DPRINTF("%s: Failed to put CP0_PWSIZE (%d)\n", __func__
, err
);
812 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_WIRED
, &env
->CP0_Wired
);
814 DPRINTF("%s: Failed to put CP0_WIRED (%d)\n", __func__
, err
);
817 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_PWCTL
, &env
->CP0_PWCtl
);
819 DPRINTF("%s: Failed to put CP0_PWCTL (%d)\n", __func__
, err
);
822 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_HWRENA
, &env
->CP0_HWREna
);
824 DPRINTF("%s: Failed to put CP0_HWRENA (%d)\n", __func__
, err
);
827 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_BADVADDR
,
830 DPRINTF("%s: Failed to put CP0_BADVADDR (%d)\n", __func__
, err
);
834 /* If VM clock stopped then state will be restored when it is restarted */
835 if (runstate_is_running()) {
836 err
= kvm_mips_restore_count(cs
);
842 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_ENTRYHI
,
845 DPRINTF("%s: Failed to put CP0_ENTRYHI (%d)\n", __func__
, err
);
848 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_COMPARE
,
851 DPRINTF("%s: Failed to put CP0_COMPARE (%d)\n", __func__
, err
);
854 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_STATUS
, &env
->CP0_Status
);
856 DPRINTF("%s: Failed to put CP0_STATUS (%d)\n", __func__
, err
);
859 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_EPC
, &env
->CP0_EPC
);
861 DPRINTF("%s: Failed to put CP0_EPC (%d)\n", __func__
, err
);
864 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_PRID
, &env
->CP0_PRid
);
866 DPRINTF("%s: Failed to put CP0_PRID (%d)\n", __func__
, err
);
869 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_EBASE
, &env
->CP0_EBase
);
871 DPRINTF("%s: Failed to put CP0_EBASE (%d)\n", __func__
, err
);
874 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG
,
876 KVM_REG_MIPS_CP0_CONFIG_MASK
);
878 DPRINTF("%s: Failed to change CP0_CONFIG (%d)\n", __func__
, err
);
881 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG1
,
883 KVM_REG_MIPS_CP0_CONFIG1_MASK
);
885 DPRINTF("%s: Failed to change CP0_CONFIG1 (%d)\n", __func__
, err
);
888 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG2
,
890 KVM_REG_MIPS_CP0_CONFIG2_MASK
);
892 DPRINTF("%s: Failed to change CP0_CONFIG2 (%d)\n", __func__
, err
);
895 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG3
,
897 KVM_REG_MIPS_CP0_CONFIG3_MASK
);
899 DPRINTF("%s: Failed to change CP0_CONFIG3 (%d)\n", __func__
, err
);
902 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG4
,
904 KVM_REG_MIPS_CP0_CONFIG4_MASK
);
906 DPRINTF("%s: Failed to change CP0_CONFIG4 (%d)\n", __func__
, err
);
909 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG5
,
911 KVM_REG_MIPS_CP0_CONFIG5_MASK
);
913 DPRINTF("%s: Failed to change CP0_CONFIG5 (%d)\n", __func__
, err
);
916 err
= kvm_mips_change_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG6
,
918 KVM_REG_MIPS_CP0_CONFIG6_MASK
);
920 DPRINTF("%s: Failed to change CP0_CONFIG6 (%d)\n", __func__
, err
);
923 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_XCONTEXT
,
926 DPRINTF("%s: Failed to put CP0_XCONTEXT (%d)\n", __func__
, err
);
929 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_ERROREPC
,
932 DPRINTF("%s: Failed to put CP0_ERROREPC (%d)\n", __func__
, err
);
935 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH1
,
936 &env
->CP0_KScratch
[0]);
938 DPRINTF("%s: Failed to put CP0_KSCRATCH1 (%d)\n", __func__
, err
);
941 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH2
,
942 &env
->CP0_KScratch
[1]);
944 DPRINTF("%s: Failed to put CP0_KSCRATCH2 (%d)\n", __func__
, err
);
947 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH3
,
948 &env
->CP0_KScratch
[2]);
950 DPRINTF("%s: Failed to put CP0_KSCRATCH3 (%d)\n", __func__
, err
);
953 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH4
,
954 &env
->CP0_KScratch
[3]);
956 DPRINTF("%s: Failed to put CP0_KSCRATCH4 (%d)\n", __func__
, err
);
959 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH5
,
960 &env
->CP0_KScratch
[4]);
962 DPRINTF("%s: Failed to put CP0_KSCRATCH5 (%d)\n", __func__
, err
);
965 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH6
,
966 &env
->CP0_KScratch
[5]);
968 DPRINTF("%s: Failed to put CP0_KSCRATCH6 (%d)\n", __func__
, err
);
975 static int kvm_mips_get_cp0_registers(CPUState
*cs
)
977 MIPSCPU
*cpu
= MIPS_CPU(cs
);
978 CPUMIPSState
*env
= &cpu
->env
;
981 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_INDEX
, &env
->CP0_Index
);
983 DPRINTF("%s: Failed to get CP0_INDEX (%d)\n", __func__
, err
);
986 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_RANDOM
, &env
->CP0_Random
);
988 DPRINTF("%s: Failed to get CP0_RANDOM (%d)\n", __func__
, err
);
991 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_CONTEXT
,
994 DPRINTF("%s: Failed to get CP0_CONTEXT (%d)\n", __func__
, err
);
997 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_USERLOCAL
,
998 &env
->active_tc
.CP0_UserLocal
);
1000 DPRINTF("%s: Failed to get CP0_USERLOCAL (%d)\n", __func__
, err
);
1003 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEMASK
,
1004 &env
->CP0_PageMask
);
1006 DPRINTF("%s: Failed to get CP0_PAGEMASK (%d)\n", __func__
, err
);
1009 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEGRAIN
,
1010 &env
->CP0_PageGrain
);
1012 DPRINTF("%s: Failed to get CP0_PAGEGRAIN (%d)\n", __func__
, err
);
1015 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_PWBASE
,
1018 DPRINTF("%s: Failed to get CP0_PWBASE (%d)\n", __func__
, err
);
1021 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_PWFIELD
,
1024 DPRINTF("%s: Failed to get CP0_PWFIELD (%d)\n", __func__
, err
);
1027 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_PWSIZE
,
1030 DPRINTF("%s: Failed to get CP0_PWSIZE (%d)\n", __func__
, err
);
1033 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_WIRED
, &env
->CP0_Wired
);
1035 DPRINTF("%s: Failed to get CP0_WIRED (%d)\n", __func__
, err
);
1038 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_PWCTL
, &env
->CP0_PWCtl
);
1040 DPRINTF("%s: Failed to get CP0_PWCtl (%d)\n", __func__
, err
);
1043 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_HWRENA
, &env
->CP0_HWREna
);
1045 DPRINTF("%s: Failed to get CP0_HWRENA (%d)\n", __func__
, err
);
1048 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_BADVADDR
,
1049 &env
->CP0_BadVAddr
);
1051 DPRINTF("%s: Failed to get CP0_BADVADDR (%d)\n", __func__
, err
);
1054 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_ENTRYHI
,
1057 DPRINTF("%s: Failed to get CP0_ENTRYHI (%d)\n", __func__
, err
);
1060 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_COMPARE
,
1063 DPRINTF("%s: Failed to get CP0_COMPARE (%d)\n", __func__
, err
);
1066 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_STATUS
, &env
->CP0_Status
);
1068 DPRINTF("%s: Failed to get CP0_STATUS (%d)\n", __func__
, err
);
1072 /* If VM clock stopped then state was already saved when it was stopped */
1073 if (runstate_is_running()) {
1074 err
= kvm_mips_save_count(cs
);
1080 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_EPC
, &env
->CP0_EPC
);
1082 DPRINTF("%s: Failed to get CP0_EPC (%d)\n", __func__
, err
);
1085 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_PRID
, &env
->CP0_PRid
);
1087 DPRINTF("%s: Failed to get CP0_PRID (%d)\n", __func__
, err
);
1090 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_EBASE
, &env
->CP0_EBase
);
1092 DPRINTF("%s: Failed to get CP0_EBASE (%d)\n", __func__
, err
);
1095 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG
, &env
->CP0_Config0
);
1097 DPRINTF("%s: Failed to get CP0_CONFIG (%d)\n", __func__
, err
);
1100 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG1
, &env
->CP0_Config1
);
1102 DPRINTF("%s: Failed to get CP0_CONFIG1 (%d)\n", __func__
, err
);
1105 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG2
, &env
->CP0_Config2
);
1107 DPRINTF("%s: Failed to get CP0_CONFIG2 (%d)\n", __func__
, err
);
1110 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG3
, &env
->CP0_Config3
);
1112 DPRINTF("%s: Failed to get CP0_CONFIG3 (%d)\n", __func__
, err
);
1115 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG4
, &env
->CP0_Config4
);
1117 DPRINTF("%s: Failed to get CP0_CONFIG4 (%d)\n", __func__
, err
);
1120 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG5
, &env
->CP0_Config5
);
1122 DPRINTF("%s: Failed to get CP0_CONFIG5 (%d)\n", __func__
, err
);
1125 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CONFIG6
, &env
->CP0_Config6
);
1127 DPRINTF("%s: Failed to get CP0_CONFIG6 (%d)\n", __func__
, err
);
1130 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_XCONTEXT
,
1131 &env
->CP0_XContext
);
1133 DPRINTF("%s: Failed to get CP0_XCONTEXT (%d)\n", __func__
, err
);
1136 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_ERROREPC
,
1137 &env
->CP0_ErrorEPC
);
1139 DPRINTF("%s: Failed to get CP0_ERROREPC (%d)\n", __func__
, err
);
1142 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH1
,
1143 &env
->CP0_KScratch
[0]);
1145 DPRINTF("%s: Failed to get CP0_KSCRATCH1 (%d)\n", __func__
, err
);
1148 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH2
,
1149 &env
->CP0_KScratch
[1]);
1151 DPRINTF("%s: Failed to get CP0_KSCRATCH2 (%d)\n", __func__
, err
);
1154 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH3
,
1155 &env
->CP0_KScratch
[2]);
1157 DPRINTF("%s: Failed to get CP0_KSCRATCH3 (%d)\n", __func__
, err
);
1160 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH4
,
1161 &env
->CP0_KScratch
[3]);
1163 DPRINTF("%s: Failed to get CP0_KSCRATCH4 (%d)\n", __func__
, err
);
1166 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH5
,
1167 &env
->CP0_KScratch
[4]);
1169 DPRINTF("%s: Failed to get CP0_KSCRATCH5 (%d)\n", __func__
, err
);
1172 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_KSCRATCH6
,
1173 &env
->CP0_KScratch
[5]);
1175 DPRINTF("%s: Failed to get CP0_KSCRATCH6 (%d)\n", __func__
, err
);
1182 int kvm_arch_put_registers(CPUState
*cs
, int level
)
1184 MIPSCPU
*cpu
= MIPS_CPU(cs
);
1185 CPUMIPSState
*env
= &cpu
->env
;
1186 struct kvm_regs regs
;
1190 /* Set the registers based on QEMU's view of things */
1191 for (i
= 0; i
< 32; i
++) {
1192 regs
.gpr
[i
] = (int64_t)(target_long
)env
->active_tc
.gpr
[i
];
1195 regs
.hi
= (int64_t)(target_long
)env
->active_tc
.HI
[0];
1196 regs
.lo
= (int64_t)(target_long
)env
->active_tc
.LO
[0];
1197 regs
.pc
= (int64_t)(target_long
)env
->active_tc
.PC
;
1199 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_REGS
, ®s
);
1205 ret
= kvm_mips_put_cp0_registers(cs
, level
);
1210 ret
= kvm_mips_put_fpu_registers(cs
, level
);
1218 int kvm_arch_get_registers(CPUState
*cs
)
1220 MIPSCPU
*cpu
= MIPS_CPU(cs
);
1221 CPUMIPSState
*env
= &cpu
->env
;
1223 struct kvm_regs regs
;
1226 /* Get the current register set as KVM seems it */
1227 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_REGS
, ®s
);
1233 for (i
= 0; i
< 32; i
++) {
1234 env
->active_tc
.gpr
[i
] = regs
.gpr
[i
];
1237 env
->active_tc
.HI
[0] = regs
.hi
;
1238 env
->active_tc
.LO
[0] = regs
.lo
;
1239 env
->active_tc
.PC
= regs
.pc
;
1241 kvm_mips_get_cp0_registers(cs
);
1242 kvm_mips_get_fpu_registers(cs
);
1247 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
1248 uint64_t address
, uint32_t data
, PCIDevice
*dev
)
1253 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry
*route
,
1254 int vector
, PCIDevice
*dev
)
1259 int kvm_arch_release_virq_post(int virq
)
1264 int kvm_arch_msi_data_to_gsi(uint32_t data
)
1269 int kvm_arch_get_default_type(MachineState
*machine
)
1271 #if defined(KVM_CAP_MIPS_VZ)
1273 KVMState
*s
= KVM_STATE(machine
->accelerator
);
1275 r
= kvm_check_extension(s
, KVM_CAP_MIPS_VZ
);
1277 return KVM_VM_MIPS_VZ
;
1281 error_report("KVM_VM_MIPS_VZ type is not available");
1285 bool kvm_arch_cpu_check_are_resettable(void)
1290 void kvm_arch_accel_class_init(ObjectClass
*oc
)