target/m68k: Merge disas_m68k_insn into m68k_tr_translate_insn
[qemu/ar7.git] / target / mips / kvm.c
blob8e72850962e1c9e186bccca10e64c425f3592b62
1 /*
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
4 * for more details.
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"
18 #include "cpu.h"
19 #include "internal.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"
25 #include "kvm_mips.h"
26 #include "exec/memattrs.h"
28 #define DEBUG_KVM 0
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[] = {
37 KVM_CAP_LAST_INFO
40 static void kvm_mips_update_state(void *opaque, int running, RunState state);
42 unsigned long kvm_arch_vcpu_id(CPUState *cs)
44 return cs->cpu_index;
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__);
56 return 0;
59 int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
61 return 0;
64 int kvm_arch_init_vcpu(CPUState *cs)
66 MIPSCPU *cpu = MIPS_CPU(cs);
67 CPUMIPSState *env = &cpu->env;
68 int ret = 0;
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);
74 if (ret < 0) {
75 /* mark unsupported so it gets disabled on reset */
76 kvm_mips_fpu_cap = 0;
77 ret = 0;
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);
83 if (ret < 0) {
84 /* mark unsupported so it gets disabled on reset */
85 kvm_mips_msa_cap = 0;
86 ret = 0;
90 DPRINTF("%s\n", __func__);
91 return ret;
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__);
113 return 0;
116 int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
118 DPRINTF("%s\n", __func__);
119 return 0;
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);
133 int r;
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)) {
140 intr.cpu = -1;
141 intr.irq = 2;
142 r = kvm_vcpu_ioctl(cs, KVM_INTERRUPT, &intr);
143 if (r < 0) {
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)
159 return cs->halted;
162 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
164 int ret;
166 DPRINTF("%s\n", __func__);
167 switch (run->exit_reason) {
168 default:
169 error_report("%s: unknown exit reason %d",
170 __func__, run->exit_reason);
171 ret = -1;
172 break;
175 return ret;
178 bool kvm_arch_stop_on_emulation_error(CPUState *cs)
180 DPRINTF("%s\n", __func__);
181 return true;
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()) {
194 return 0;
197 intr.cpu = -1;
199 if (level) {
200 intr.irq = irq;
201 } else {
202 intr.irq = -irq;
205 kvm_vcpu_ioctl(cs, KVM_INTERRUPT, &intr);
207 return 0;
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()) {
217 return 0;
220 intr.cpu = dest_cs->cpu_index;
222 if (level) {
223 intr.irq = irq;
224 } else {
225 intr.irq = -irq;
228 DPRINTF("%s: CPU %d, IRQ: %d\n", __func__, intr.cpu, intr.irq);
230 kvm_vcpu_ioctl(cs, KVM_INTERRUPT, &intr);
232 return 0;
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,
264 int32_t *addr)
266 struct kvm_one_reg cp0reg = {
267 .id = reg_id,
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,
275 uint32_t *addr)
277 struct kvm_one_reg cp0reg = {
278 .id = reg_id,
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,
286 target_ulong *addr)
288 uint64_t val64 = *addr;
289 struct kvm_one_reg cp0reg = {
290 .id = reg_id,
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,
298 int64_t *addr)
300 struct kvm_one_reg cp0reg = {
301 .id = reg_id,
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,
309 uint64_t *addr)
311 struct kvm_one_reg cp0reg = {
312 .id = reg_id,
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,
320 int32_t *addr)
322 struct kvm_one_reg cp0reg = {
323 .id = reg_id,
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,
331 uint32_t *addr)
333 struct kvm_one_reg cp0reg = {
334 .id = reg_id,
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,
342 target_ulong *addr)
344 int ret;
345 uint64_t val64 = 0;
346 struct kvm_one_reg cp0reg = {
347 .id = reg_id,
348 .addr = (uintptr_t)&val64
351 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &cp0reg);
352 if (ret >= 0) {
353 *addr = val64;
355 return ret;
358 static inline int kvm_mips_get_one_reg64(CPUState *cs, uint64_t reg_id,
359 int64_t *addr)
361 struct kvm_one_reg cp0reg = {
362 .id = reg_id,
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,
370 uint64_t *addr)
372 struct kvm_one_reg cp0reg = {
373 .id = reg_id,
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) | \
382 (1U << CP0C1_FP))
383 #define KVM_REG_MIPS_CP0_CONFIG2_MASK (1U << CP0C2_M)
384 #define KVM_REG_MIPS_CP0_CONFIG3_MASK ((1U << CP0C3_M) | \
385 (1U << CP0C3_MSAP))
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) | \
390 (1U << CP0C5_UFR))
392 static inline int kvm_mips_change_one_reg(CPUState *cs, uint64_t reg_id,
393 int32_t *addr, int32_t mask)
395 int err;
396 int32_t tmp, change;
398 err = kvm_mips_get_one_reg(cs, reg_id, &tmp);
399 if (err < 0) {
400 return err;
403 /* only change bits in mask */
404 change = (*addr ^ tmp) & mask;
405 if (!change) {
406 return 0;
409 tmp = tmp ^ change;
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
420 * to QEMU.
422 static int kvm_mips_save_count(CPUState *cs)
424 MIPSCPU *cpu = MIPS_CPU(cs);
425 CPUMIPSState *env = &cpu->env;
426 uint64_t count_ctl;
427 int err, ret = 0;
429 /* freeze KVM timer */
430 err = kvm_mips_get_one_ureg64(cs, KVM_REG_MIPS_COUNT_CTL, &count_ctl);
431 if (err < 0) {
432 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__, err);
433 ret = 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);
437 if (err < 0) {
438 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__, err);
439 ret = err;
443 /* read CP0_Cause */
444 err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_CAUSE, &env->CP0_Cause);
445 if (err < 0) {
446 DPRINTF("%s: Failed to get CP0_CAUSE (%d)\n", __func__, err);
447 ret = err;
450 /* read CP0_Count */
451 err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_COUNT, &env->CP0_Count);
452 if (err < 0) {
453 DPRINTF("%s: Failed to get CP0_COUNT (%d)\n", __func__, err);
454 ret = err;
457 return ret;
461 * Restore the state of the KVM timer when VM clock is restarted or state is
462 * synced to KVM.
464 static int kvm_mips_restore_count(CPUState *cs)
466 MIPSCPU *cpu = MIPS_CPU(cs);
467 CPUMIPSState *env = &cpu->env;
468 uint64_t count_ctl;
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);
473 if (err_dc < 0) {
474 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__, err_dc);
475 ret = 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);
480 if (err < 0) {
481 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__, err);
482 ret = err;
486 /* load CP0_Cause */
487 err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_CAUSE, &env->CP0_Cause);
488 if (err < 0) {
489 DPRINTF("%s: Failed to put CP0_CAUSE (%d)\n", __func__, err);
490 ret = err;
493 /* load CP0_Count */
494 err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_COUNT, &env->CP0_Count);
495 if (err < 0) {
496 DPRINTF("%s: Failed to put CP0_COUNT (%d)\n", __func__, err);
497 ret = err;
500 /* resume KVM timer */
501 if (err_dc >= 0) {
502 count_ctl &= ~KVM_REG_MIPS_COUNT_CTL_DC;
503 err = kvm_mips_put_one_ureg64(cs, KVM_REG_MIPS_COUNT_CTL, &count_ctl);
504 if (err < 0) {
505 DPRINTF("%s: Failed to set COUNT_CTL.DC=0 (%d)\n", __func__, err);
506 ret = err;
510 return ret;
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;
519 int ret;
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.
526 if (!running) {
527 if (!cs->vcpu_dirty) {
528 ret = kvm_mips_save_count(cs);
529 if (ret < 0) {
530 warn_report("Failed saving count");
533 } else {
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,
537 &count_resume);
538 if (ret < 0) {
539 warn_report("Failed setting COUNT_RESUME");
540 return;
543 if (!cs->vcpu_dirty) {
544 ret = kvm_mips_restore_count(cs);
545 if (ret < 0) {
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;
556 int err, ret = 0;
557 unsigned int i;
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);
565 if (err < 0) {
566 DPRINTF("%s: Failed to put FCR_IR (%d)\n", __func__, err);
567 ret = err;
570 err = kvm_mips_put_one_ureg(cs, KVM_REG_MIPS_FCR_CSR,
571 &env->active_fpu.fcr31);
572 if (err < 0) {
573 DPRINTF("%s: Failed to put FCR_CSR (%d)\n", __func__, err);
574 ret = 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);
587 } else {
588 err = kvm_mips_get_one_ureg(cs, KVM_REG_MIPS_FPR_32(i),
589 &env->active_fpu.fpr[i].w[FP_ENDIAN_IDX]);
591 if (err < 0) {
592 DPRINTF("%s: Failed to put FPR%u (%d)\n", __func__, i, err);
593 ret = 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,
604 &env->msair);
605 if (err < 0) {
606 DPRINTF("%s: Failed to put MSA_IR (%d)\n", __func__, err);
607 ret = err;
610 err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_MSA_CSR,
611 &env->active_tc.msacsr);
612 if (err < 0) {
613 DPRINTF("%s: Failed to put MSA_CSR (%d)\n", __func__, err);
614 ret = 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);
622 if (err < 0) {
623 DPRINTF("%s: Failed to put VEC%u (%d)\n", __func__, i, err);
624 ret = err;
629 return ret;
632 static int kvm_mips_get_fpu_registers(CPUState *cs)
634 MIPSCPU *cpu = MIPS_CPU(cs);
635 CPUMIPSState *env = &cpu->env;
636 int err, ret = 0;
637 unsigned int i;
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);
644 if (err < 0) {
645 DPRINTF("%s: Failed to get FCR_IR (%d)\n", __func__, err);
646 ret = err;
648 err = kvm_mips_get_one_ureg(cs, KVM_REG_MIPS_FCR_CSR,
649 &env->active_fpu.fcr31);
650 if (err < 0) {
651 DPRINTF("%s: Failed to get FCR_CSR (%d)\n", __func__, err);
652 ret = err;
653 } else {
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);
667 } else {
668 err = kvm_mips_get_one_ureg(cs, KVM_REG_MIPS_FPR_32(i),
669 &env->active_fpu.fpr[i].w[FP_ENDIAN_IDX]);
671 if (err < 0) {
672 DPRINTF("%s: Failed to get FPR%u (%d)\n", __func__, i, err);
673 ret = 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,
683 &env->msair);
684 if (err < 0) {
685 DPRINTF("%s: Failed to get MSA_IR (%d)\n", __func__, err);
686 ret = err;
688 err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_MSA_CSR,
689 &env->active_tc.msacsr);
690 if (err < 0) {
691 DPRINTF("%s: Failed to get MSA_CSR (%d)\n", __func__, err);
692 ret = err;
693 } else {
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);
702 if (err < 0) {
703 DPRINTF("%s: Failed to get VEC%u (%d)\n", __func__, i, err);
704 ret = err;
709 return ret;
713 static int kvm_mips_put_cp0_registers(CPUState *cs, int level)
715 MIPSCPU *cpu = MIPS_CPU(cs);
716 CPUMIPSState *env = &cpu->env;
717 int err, ret = 0;
719 (void)level;
721 err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_INDEX, &env->CP0_Index);
722 if (err < 0) {
723 DPRINTF("%s: Failed to put CP0_INDEX (%d)\n", __func__, err);
724 ret = err;
726 err = kvm_mips_put_one_ulreg(cs, KVM_REG_MIPS_CP0_CONTEXT,
727 &env->CP0_Context);
728 if (err < 0) {
729 DPRINTF("%s: Failed to put CP0_CONTEXT (%d)\n", __func__, err);
730 ret = err;
732 err = kvm_mips_put_one_ulreg(cs, KVM_REG_MIPS_CP0_USERLOCAL,
733 &env->active_tc.CP0_UserLocal);
734 if (err < 0) {
735 DPRINTF("%s: Failed to put CP0_USERLOCAL (%d)\n", __func__, err);
736 ret = err;
738 err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_PAGEMASK,
739 &env->CP0_PageMask);
740 if (err < 0) {
741 DPRINTF("%s: Failed to put CP0_PAGEMASK (%d)\n", __func__, err);
742 ret = err;
744 err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_WIRED, &env->CP0_Wired);
745 if (err < 0) {
746 DPRINTF("%s: Failed to put CP0_WIRED (%d)\n", __func__, err);
747 ret = err;
749 err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_HWRENA, &env->CP0_HWREna);
750 if (err < 0) {
751 DPRINTF("%s: Failed to put CP0_HWRENA (%d)\n", __func__, err);
752 ret = err;
754 err = kvm_mips_put_one_ulreg(cs, KVM_REG_MIPS_CP0_BADVADDR,
755 &env->CP0_BadVAddr);
756 if (err < 0) {
757 DPRINTF("%s: Failed to put CP0_BADVADDR (%d)\n", __func__, err);
758 ret = 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);
764 if (err < 0) {
765 ret = err;
769 err = kvm_mips_put_one_ulreg(cs, KVM_REG_MIPS_CP0_ENTRYHI,
770 &env->CP0_EntryHi);
771 if (err < 0) {
772 DPRINTF("%s: Failed to put CP0_ENTRYHI (%d)\n", __func__, err);
773 ret = err;
775 err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_COMPARE,
776 &env->CP0_Compare);
777 if (err < 0) {
778 DPRINTF("%s: Failed to put CP0_COMPARE (%d)\n", __func__, err);
779 ret = err;
781 err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_STATUS, &env->CP0_Status);
782 if (err < 0) {
783 DPRINTF("%s: Failed to put CP0_STATUS (%d)\n", __func__, err);
784 ret = err;
786 err = kvm_mips_put_one_ulreg(cs, KVM_REG_MIPS_CP0_EPC, &env->CP0_EPC);
787 if (err < 0) {
788 DPRINTF("%s: Failed to put CP0_EPC (%d)\n", __func__, err);
789 ret = err;
791 err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_PRID, &env->CP0_PRid);
792 if (err < 0) {
793 DPRINTF("%s: Failed to put CP0_PRID (%d)\n", __func__, err);
794 ret = err;
796 err = kvm_mips_change_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG,
797 &env->CP0_Config0,
798 KVM_REG_MIPS_CP0_CONFIG_MASK);
799 if (err < 0) {
800 DPRINTF("%s: Failed to change CP0_CONFIG (%d)\n", __func__, err);
801 ret = err;
803 err = kvm_mips_change_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG1,
804 &env->CP0_Config1,
805 KVM_REG_MIPS_CP0_CONFIG1_MASK);
806 if (err < 0) {
807 DPRINTF("%s: Failed to change CP0_CONFIG1 (%d)\n", __func__, err);
808 ret = err;
810 err = kvm_mips_change_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG2,
811 &env->CP0_Config2,
812 KVM_REG_MIPS_CP0_CONFIG2_MASK);
813 if (err < 0) {
814 DPRINTF("%s: Failed to change CP0_CONFIG2 (%d)\n", __func__, err);
815 ret = err;
817 err = kvm_mips_change_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG3,
818 &env->CP0_Config3,
819 KVM_REG_MIPS_CP0_CONFIG3_MASK);
820 if (err < 0) {
821 DPRINTF("%s: Failed to change CP0_CONFIG3 (%d)\n", __func__, err);
822 ret = err;
824 err = kvm_mips_change_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG4,
825 &env->CP0_Config4,
826 KVM_REG_MIPS_CP0_CONFIG4_MASK);
827 if (err < 0) {
828 DPRINTF("%s: Failed to change CP0_CONFIG4 (%d)\n", __func__, err);
829 ret = err;
831 err = kvm_mips_change_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG5,
832 &env->CP0_Config5,
833 KVM_REG_MIPS_CP0_CONFIG5_MASK);
834 if (err < 0) {
835 DPRINTF("%s: Failed to change CP0_CONFIG5 (%d)\n", __func__, err);
836 ret = err;
838 err = kvm_mips_put_one_ulreg(cs, KVM_REG_MIPS_CP0_ERROREPC,
839 &env->CP0_ErrorEPC);
840 if (err < 0) {
841 DPRINTF("%s: Failed to put CP0_ERROREPC (%d)\n", __func__, err);
842 ret = err;
845 return ret;
848 static int kvm_mips_get_cp0_registers(CPUState *cs)
850 MIPSCPU *cpu = MIPS_CPU(cs);
851 CPUMIPSState *env = &cpu->env;
852 int err, ret = 0;
854 err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_INDEX, &env->CP0_Index);
855 if (err < 0) {
856 DPRINTF("%s: Failed to get CP0_INDEX (%d)\n", __func__, err);
857 ret = err;
859 err = kvm_mips_get_one_ulreg(cs, KVM_REG_MIPS_CP0_CONTEXT,
860 &env->CP0_Context);
861 if (err < 0) {
862 DPRINTF("%s: Failed to get CP0_CONTEXT (%d)\n", __func__, err);
863 ret = err;
865 err = kvm_mips_get_one_ulreg(cs, KVM_REG_MIPS_CP0_USERLOCAL,
866 &env->active_tc.CP0_UserLocal);
867 if (err < 0) {
868 DPRINTF("%s: Failed to get CP0_USERLOCAL (%d)\n", __func__, err);
869 ret = err;
871 err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_PAGEMASK,
872 &env->CP0_PageMask);
873 if (err < 0) {
874 DPRINTF("%s: Failed to get CP0_PAGEMASK (%d)\n", __func__, err);
875 ret = err;
877 err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_WIRED, &env->CP0_Wired);
878 if (err < 0) {
879 DPRINTF("%s: Failed to get CP0_WIRED (%d)\n", __func__, err);
880 ret = err;
882 err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_HWRENA, &env->CP0_HWREna);
883 if (err < 0) {
884 DPRINTF("%s: Failed to get CP0_HWRENA (%d)\n", __func__, err);
885 ret = err;
887 err = kvm_mips_get_one_ulreg(cs, KVM_REG_MIPS_CP0_BADVADDR,
888 &env->CP0_BadVAddr);
889 if (err < 0) {
890 DPRINTF("%s: Failed to get CP0_BADVADDR (%d)\n", __func__, err);
891 ret = err;
893 err = kvm_mips_get_one_ulreg(cs, KVM_REG_MIPS_CP0_ENTRYHI,
894 &env->CP0_EntryHi);
895 if (err < 0) {
896 DPRINTF("%s: Failed to get CP0_ENTRYHI (%d)\n", __func__, err);
897 ret = err;
899 err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_COMPARE,
900 &env->CP0_Compare);
901 if (err < 0) {
902 DPRINTF("%s: Failed to get CP0_COMPARE (%d)\n", __func__, err);
903 ret = err;
905 err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_STATUS, &env->CP0_Status);
906 if (err < 0) {
907 DPRINTF("%s: Failed to get CP0_STATUS (%d)\n", __func__, err);
908 ret = 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);
914 if (err < 0) {
915 ret = err;
919 err = kvm_mips_get_one_ulreg(cs, KVM_REG_MIPS_CP0_EPC, &env->CP0_EPC);
920 if (err < 0) {
921 DPRINTF("%s: Failed to get CP0_EPC (%d)\n", __func__, err);
922 ret = err;
924 err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_PRID, &env->CP0_PRid);
925 if (err < 0) {
926 DPRINTF("%s: Failed to get CP0_PRID (%d)\n", __func__, err);
927 ret = err;
929 err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG, &env->CP0_Config0);
930 if (err < 0) {
931 DPRINTF("%s: Failed to get CP0_CONFIG (%d)\n", __func__, err);
932 ret = err;
934 err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG1, &env->CP0_Config1);
935 if (err < 0) {
936 DPRINTF("%s: Failed to get CP0_CONFIG1 (%d)\n", __func__, err);
937 ret = err;
939 err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG2, &env->CP0_Config2);
940 if (err < 0) {
941 DPRINTF("%s: Failed to get CP0_CONFIG2 (%d)\n", __func__, err);
942 ret = err;
944 err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG3, &env->CP0_Config3);
945 if (err < 0) {
946 DPRINTF("%s: Failed to get CP0_CONFIG3 (%d)\n", __func__, err);
947 ret = err;
949 err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG4, &env->CP0_Config4);
950 if (err < 0) {
951 DPRINTF("%s: Failed to get CP0_CONFIG4 (%d)\n", __func__, err);
952 ret = err;
954 err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG5, &env->CP0_Config5);
955 if (err < 0) {
956 DPRINTF("%s: Failed to get CP0_CONFIG5 (%d)\n", __func__, err);
957 ret = err;
959 err = kvm_mips_get_one_ulreg(cs, KVM_REG_MIPS_CP0_ERROREPC,
960 &env->CP0_ErrorEPC);
961 if (err < 0) {
962 DPRINTF("%s: Failed to get CP0_ERROREPC (%d)\n", __func__, err);
963 ret = err;
966 return ret;
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;
974 int ret;
975 int i;
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, &regs);
988 if (ret < 0) {
989 return ret;
992 ret = kvm_mips_put_cp0_registers(cs, level);
993 if (ret < 0) {
994 return ret;
997 ret = kvm_mips_put_fpu_registers(cs, level);
998 if (ret < 0) {
999 return ret;
1002 return ret;
1005 int kvm_arch_get_registers(CPUState *cs)
1007 MIPSCPU *cpu = MIPS_CPU(cs);
1008 CPUMIPSState *env = &cpu->env;
1009 int ret = 0;
1010 struct kvm_regs regs;
1011 int i;
1013 /* Get the current register set as KVM seems it */
1014 ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
1016 if (ret < 0) {
1017 return ret;
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);
1031 return ret;
1034 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
1035 uint64_t address, uint32_t data, PCIDevice *dev)
1037 return 0;
1040 int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
1041 int vector, PCIDevice *dev)
1043 return 0;
1046 int kvm_arch_release_virq_post(int virq)
1048 return 0;
1051 int kvm_arch_msi_data_to_gsi(uint32_t data)
1053 abort();