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 <sys/types.h>
13 #include <sys/ioctl.h>
16 #include <linux/kvm.h>
18 #include "qemu-common.h"
19 #include "qemu/error-report.h"
20 #include "qemu/timer.h"
21 #include "sysemu/sysemu.h"
22 #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 const KVMCapabilityInfo kvm_arch_required_capabilities
[] = {
37 static void kvm_mips_update_state(void *opaque
, int running
, RunState state
);
39 unsigned long kvm_arch_vcpu_id(CPUState
*cs
)
44 int kvm_arch_init(MachineState
*ms
, KVMState
*s
)
46 /* MIPS has 128 signals */
47 kvm_set_sigmask_len(s
, 16);
49 DPRINTF("%s\n", __func__
);
53 int kvm_arch_init_vcpu(CPUState
*cs
)
57 qemu_add_vm_change_state_handler(kvm_mips_update_state
, cs
);
59 DPRINTF("%s\n", __func__
);
63 void kvm_mips_reset_vcpu(MIPSCPU
*cpu
)
65 CPUMIPSState
*env
= &cpu
->env
;
67 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
68 fprintf(stderr
, "Warning: FPU not supported with KVM, disabling\n");
69 env
->CP0_Config1
&= ~(1 << CP0C1_FP
);
72 DPRINTF("%s\n", __func__
);
75 int kvm_arch_insert_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
77 DPRINTF("%s\n", __func__
);
81 int kvm_arch_remove_sw_breakpoint(CPUState
*cs
, struct kvm_sw_breakpoint
*bp
)
83 DPRINTF("%s\n", __func__
);
87 static inline int cpu_mips_io_interrupts_pending(MIPSCPU
*cpu
)
89 CPUMIPSState
*env
= &cpu
->env
;
91 DPRINTF("%s: %#x\n", __func__
, env
->CP0_Cause
& (1 << (2 + CP0Ca_IP
)));
92 return env
->CP0_Cause
& (0x1 << (2 + CP0Ca_IP
));
96 void kvm_arch_pre_run(CPUState
*cs
, struct kvm_run
*run
)
98 MIPSCPU
*cpu
= MIPS_CPU(cs
);
100 struct kvm_mips_interrupt intr
;
102 if ((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
103 cpu_mips_io_interrupts_pending(cpu
)) {
106 r
= kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
108 error_report("%s: cpu %d: failed to inject IRQ %x",
109 __func__
, cs
->cpu_index
, intr
.irq
);
114 MemTxAttrs
kvm_arch_post_run(CPUState
*cs
, struct kvm_run
*run
)
116 DPRINTF("%s\n", __func__
);
117 return MEMTXATTRS_UNSPECIFIED
;
120 int kvm_arch_process_async_events(CPUState
*cs
)
125 int kvm_arch_handle_exit(CPUState
*cs
, struct kvm_run
*run
)
129 DPRINTF("%s\n", __func__
);
130 switch (run
->exit_reason
) {
132 error_report("%s: unknown exit reason %d",
133 __func__
, run
->exit_reason
);
141 bool kvm_arch_stop_on_emulation_error(CPUState
*cs
)
143 DPRINTF("%s\n", __func__
);
147 int kvm_arch_on_sigbus_vcpu(CPUState
*cs
, int code
, void *addr
)
149 DPRINTF("%s\n", __func__
);
153 int kvm_arch_on_sigbus(int code
, void *addr
)
155 DPRINTF("%s\n", __func__
);
159 void kvm_arch_init_irq_routing(KVMState
*s
)
163 int kvm_mips_set_interrupt(MIPSCPU
*cpu
, int irq
, int level
)
165 CPUState
*cs
= CPU(cpu
);
166 struct kvm_mips_interrupt intr
;
168 if (!kvm_enabled()) {
180 kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
185 int kvm_mips_set_ipi_interrupt(MIPSCPU
*cpu
, int irq
, int level
)
187 CPUState
*cs
= current_cpu
;
188 CPUState
*dest_cs
= CPU(cpu
);
189 struct kvm_mips_interrupt intr
;
191 if (!kvm_enabled()) {
195 intr
.cpu
= dest_cs
->cpu_index
;
203 DPRINTF("%s: CPU %d, IRQ: %d\n", __func__
, intr
.cpu
, intr
.irq
);
205 kvm_vcpu_ioctl(cs
, KVM_INTERRUPT
, &intr
);
210 #define MIPS_CP0_32(_R, _S) \
211 (KVM_REG_MIPS | KVM_REG_SIZE_U32 | 0x10000 | (8 * (_R) + (_S)))
213 #define MIPS_CP0_64(_R, _S) \
214 (KVM_REG_MIPS | KVM_REG_SIZE_U64 | 0x10000 | (8 * (_R) + (_S)))
216 #define KVM_REG_MIPS_CP0_INDEX MIPS_CP0_32(0, 0)
217 #define KVM_REG_MIPS_CP0_CONTEXT MIPS_CP0_64(4, 0)
218 #define KVM_REG_MIPS_CP0_USERLOCAL MIPS_CP0_64(4, 2)
219 #define KVM_REG_MIPS_CP0_PAGEMASK MIPS_CP0_32(5, 0)
220 #define KVM_REG_MIPS_CP0_WIRED MIPS_CP0_32(6, 0)
221 #define KVM_REG_MIPS_CP0_HWRENA MIPS_CP0_32(7, 0)
222 #define KVM_REG_MIPS_CP0_BADVADDR MIPS_CP0_64(8, 0)
223 #define KVM_REG_MIPS_CP0_COUNT MIPS_CP0_32(9, 0)
224 #define KVM_REG_MIPS_CP0_ENTRYHI MIPS_CP0_64(10, 0)
225 #define KVM_REG_MIPS_CP0_COMPARE MIPS_CP0_32(11, 0)
226 #define KVM_REG_MIPS_CP0_STATUS MIPS_CP0_32(12, 0)
227 #define KVM_REG_MIPS_CP0_CAUSE MIPS_CP0_32(13, 0)
228 #define KVM_REG_MIPS_CP0_EPC MIPS_CP0_64(14, 0)
229 #define KVM_REG_MIPS_CP0_ERROREPC MIPS_CP0_64(30, 0)
231 /* CP0_Count control */
232 #define KVM_REG_MIPS_COUNT_CTL (KVM_REG_MIPS | KVM_REG_SIZE_U64 | \
234 #define KVM_REG_MIPS_COUNT_CTL_DC 0x00000001 /* master disable */
235 /* CP0_Count resume monotonic nanoseconds */
236 #define KVM_REG_MIPS_COUNT_RESUME (KVM_REG_MIPS | KVM_REG_SIZE_U64 | \
238 /* CP0_Count rate in Hz */
239 #define KVM_REG_MIPS_COUNT_HZ (KVM_REG_MIPS | KVM_REG_SIZE_U64 | \
242 static inline int kvm_mips_put_one_reg(CPUState
*cs
, uint64_t reg_id
,
245 uint64_t val64
= *addr
;
246 struct kvm_one_reg cp0reg
= {
248 .addr
= (uintptr_t)&val64
251 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
254 static inline int kvm_mips_put_one_ulreg(CPUState
*cs
, uint64_t reg_id
,
257 uint64_t val64
= *addr
;
258 struct kvm_one_reg cp0reg
= {
260 .addr
= (uintptr_t)&val64
263 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
266 static inline int kvm_mips_put_one_reg64(CPUState
*cs
, uint64_t reg_id
,
269 struct kvm_one_reg cp0reg
= {
271 .addr
= (uintptr_t)addr
274 return kvm_vcpu_ioctl(cs
, KVM_SET_ONE_REG
, &cp0reg
);
277 static inline int kvm_mips_get_one_reg(CPUState
*cs
, uint64_t reg_id
,
282 struct kvm_one_reg cp0reg
= {
284 .addr
= (uintptr_t)&val64
287 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
294 static inline int kvm_mips_get_one_ulreg(CPUState
*cs
, uint64 reg_id
,
299 struct kvm_one_reg cp0reg
= {
301 .addr
= (uintptr_t)&val64
304 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
311 static inline int kvm_mips_get_one_reg64(CPUState
*cs
, uint64 reg_id
,
314 struct kvm_one_reg cp0reg
= {
316 .addr
= (uintptr_t)addr
319 return kvm_vcpu_ioctl(cs
, KVM_GET_ONE_REG
, &cp0reg
);
323 * We freeze the KVM timer when either the VM clock is stopped or the state is
324 * saved (the state is dirty).
328 * Save the state of the KVM timer when VM clock is stopped or state is synced
331 static int kvm_mips_save_count(CPUState
*cs
)
333 MIPSCPU
*cpu
= MIPS_CPU(cs
);
334 CPUMIPSState
*env
= &cpu
->env
;
338 /* freeze KVM timer */
339 err
= kvm_mips_get_one_reg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
341 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__
, err
);
343 } else if (!(count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
)) {
344 count_ctl
|= KVM_REG_MIPS_COUNT_CTL_DC
;
345 err
= kvm_mips_put_one_reg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
347 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__
, err
);
353 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_CAUSE
, &env
->CP0_Cause
);
355 DPRINTF("%s: Failed to get CP0_CAUSE (%d)\n", __func__
, err
);
360 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_COUNT
, &env
->CP0_Count
);
362 DPRINTF("%s: Failed to get CP0_COUNT (%d)\n", __func__
, err
);
370 * Restore the state of the KVM timer when VM clock is restarted or state is
373 static int kvm_mips_restore_count(CPUState
*cs
)
375 MIPSCPU
*cpu
= MIPS_CPU(cs
);
376 CPUMIPSState
*env
= &cpu
->env
;
378 int err_dc
, err
, ret
= 0;
380 /* check the timer is frozen */
381 err_dc
= kvm_mips_get_one_reg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
383 DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__
, err_dc
);
385 } else if (!(count_ctl
& KVM_REG_MIPS_COUNT_CTL_DC
)) {
386 /* freeze timer (sets COUNT_RESUME for us) */
387 count_ctl
|= KVM_REG_MIPS_COUNT_CTL_DC
;
388 err
= kvm_mips_put_one_reg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
390 DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__
, err
);
396 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_CAUSE
, &env
->CP0_Cause
);
398 DPRINTF("%s: Failed to put CP0_CAUSE (%d)\n", __func__
, err
);
403 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_COUNT
, &env
->CP0_Count
);
405 DPRINTF("%s: Failed to put CP0_COUNT (%d)\n", __func__
, err
);
409 /* resume KVM timer */
411 count_ctl
&= ~KVM_REG_MIPS_COUNT_CTL_DC
;
412 err
= kvm_mips_put_one_reg64(cs
, KVM_REG_MIPS_COUNT_CTL
, &count_ctl
);
414 DPRINTF("%s: Failed to set COUNT_CTL.DC=0 (%d)\n", __func__
, err
);
423 * Handle the VM clock being started or stopped
425 static void kvm_mips_update_state(void *opaque
, int running
, RunState state
)
427 CPUState
*cs
= opaque
;
429 uint64_t count_resume
;
432 * If state is already dirty (synced to QEMU) then the KVM timer state is
433 * already saved and can be restored when it is synced back to KVM.
436 if (!cs
->kvm_vcpu_dirty
) {
437 ret
= kvm_mips_save_count(cs
);
439 fprintf(stderr
, "Failed saving count\n");
443 /* Set clock restore time to now */
444 count_resume
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
445 ret
= kvm_mips_put_one_reg64(cs
, KVM_REG_MIPS_COUNT_RESUME
,
448 fprintf(stderr
, "Failed setting COUNT_RESUME\n");
452 if (!cs
->kvm_vcpu_dirty
) {
453 ret
= kvm_mips_restore_count(cs
);
455 fprintf(stderr
, "Failed restoring count\n");
461 static int kvm_mips_put_cp0_registers(CPUState
*cs
, int level
)
463 MIPSCPU
*cpu
= MIPS_CPU(cs
);
464 CPUMIPSState
*env
= &cpu
->env
;
469 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_INDEX
, &env
->CP0_Index
);
471 DPRINTF("%s: Failed to put CP0_INDEX (%d)\n", __func__
, err
);
474 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_CONTEXT
,
477 DPRINTF("%s: Failed to put CP0_CONTEXT (%d)\n", __func__
, err
);
480 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_USERLOCAL
,
481 &env
->active_tc
.CP0_UserLocal
);
483 DPRINTF("%s: Failed to put CP0_USERLOCAL (%d)\n", __func__
, err
);
486 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEMASK
,
489 DPRINTF("%s: Failed to put CP0_PAGEMASK (%d)\n", __func__
, err
);
492 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_WIRED
, &env
->CP0_Wired
);
494 DPRINTF("%s: Failed to put CP0_WIRED (%d)\n", __func__
, err
);
497 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_HWRENA
, &env
->CP0_HWREna
);
499 DPRINTF("%s: Failed to put CP0_HWRENA (%d)\n", __func__
, err
);
502 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_BADVADDR
,
505 DPRINTF("%s: Failed to put CP0_BADVADDR (%d)\n", __func__
, err
);
509 /* If VM clock stopped then state will be restored when it is restarted */
510 if (runstate_is_running()) {
511 err
= kvm_mips_restore_count(cs
);
517 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_ENTRYHI
,
520 DPRINTF("%s: Failed to put CP0_ENTRYHI (%d)\n", __func__
, err
);
523 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_COMPARE
,
526 DPRINTF("%s: Failed to put CP0_COMPARE (%d)\n", __func__
, err
);
529 err
= kvm_mips_put_one_reg(cs
, KVM_REG_MIPS_CP0_STATUS
, &env
->CP0_Status
);
531 DPRINTF("%s: Failed to put CP0_STATUS (%d)\n", __func__
, err
);
534 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_EPC
, &env
->CP0_EPC
);
536 DPRINTF("%s: Failed to put CP0_EPC (%d)\n", __func__
, err
);
539 err
= kvm_mips_put_one_ulreg(cs
, KVM_REG_MIPS_CP0_ERROREPC
,
542 DPRINTF("%s: Failed to put CP0_ERROREPC (%d)\n", __func__
, err
);
549 static int kvm_mips_get_cp0_registers(CPUState
*cs
)
551 MIPSCPU
*cpu
= MIPS_CPU(cs
);
552 CPUMIPSState
*env
= &cpu
->env
;
555 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_INDEX
, &env
->CP0_Index
);
557 DPRINTF("%s: Failed to get CP0_INDEX (%d)\n", __func__
, err
);
560 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_CONTEXT
,
563 DPRINTF("%s: Failed to get CP0_CONTEXT (%d)\n", __func__
, err
);
566 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_USERLOCAL
,
567 &env
->active_tc
.CP0_UserLocal
);
569 DPRINTF("%s: Failed to get CP0_USERLOCAL (%d)\n", __func__
, err
);
572 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_PAGEMASK
,
575 DPRINTF("%s: Failed to get CP0_PAGEMASK (%d)\n", __func__
, err
);
578 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_WIRED
, &env
->CP0_Wired
);
580 DPRINTF("%s: Failed to get CP0_WIRED (%d)\n", __func__
, err
);
583 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_HWRENA
, &env
->CP0_HWREna
);
585 DPRINTF("%s: Failed to get CP0_HWRENA (%d)\n", __func__
, err
);
588 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_BADVADDR
,
591 DPRINTF("%s: Failed to get CP0_BADVADDR (%d)\n", __func__
, err
);
594 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_ENTRYHI
,
597 DPRINTF("%s: Failed to get CP0_ENTRYHI (%d)\n", __func__
, err
);
600 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_COMPARE
,
603 DPRINTF("%s: Failed to get CP0_COMPARE (%d)\n", __func__
, err
);
606 err
= kvm_mips_get_one_reg(cs
, KVM_REG_MIPS_CP0_STATUS
, &env
->CP0_Status
);
608 DPRINTF("%s: Failed to get CP0_STATUS (%d)\n", __func__
, err
);
612 /* If VM clock stopped then state was already saved when it was stopped */
613 if (runstate_is_running()) {
614 err
= kvm_mips_save_count(cs
);
620 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_EPC
, &env
->CP0_EPC
);
622 DPRINTF("%s: Failed to get CP0_EPC (%d)\n", __func__
, err
);
625 err
= kvm_mips_get_one_ulreg(cs
, KVM_REG_MIPS_CP0_ERROREPC
,
628 DPRINTF("%s: Failed to get CP0_ERROREPC (%d)\n", __func__
, err
);
635 int kvm_arch_put_registers(CPUState
*cs
, int level
)
637 MIPSCPU
*cpu
= MIPS_CPU(cs
);
638 CPUMIPSState
*env
= &cpu
->env
;
639 struct kvm_regs regs
;
643 /* Set the registers based on QEMU's view of things */
644 for (i
= 0; i
< 32; i
++) {
645 regs
.gpr
[i
] = env
->active_tc
.gpr
[i
];
648 regs
.hi
= env
->active_tc
.HI
[0];
649 regs
.lo
= env
->active_tc
.LO
[0];
650 regs
.pc
= env
->active_tc
.PC
;
652 ret
= kvm_vcpu_ioctl(cs
, KVM_SET_REGS
, ®s
);
658 ret
= kvm_mips_put_cp0_registers(cs
, level
);
666 int kvm_arch_get_registers(CPUState
*cs
)
668 MIPSCPU
*cpu
= MIPS_CPU(cs
);
669 CPUMIPSState
*env
= &cpu
->env
;
671 struct kvm_regs regs
;
674 /* Get the current register set as KVM seems it */
675 ret
= kvm_vcpu_ioctl(cs
, KVM_GET_REGS
, ®s
);
681 for (i
= 0; i
< 32; i
++) {
682 env
->active_tc
.gpr
[i
] = regs
.gpr
[i
];
685 env
->active_tc
.HI
[0] = regs
.hi
;
686 env
->active_tc
.LO
[0] = regs
.lo
;
687 env
->active_tc
.PC
= regs
.pc
;
689 kvm_mips_get_cp0_registers(cs
);
694 int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry
*route
,
695 uint64_t address
, uint32_t data
)
700 int kvm_arch_msi_data_to_gsi(uint32_t data
)