xref: /openbmc/qemu/target/mips/kvm.c (revision d525ffab)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  * This file is subject to the terms and conditions of the GNU General Public
3fcf5ef2aSThomas Huth  * License.  See the file "COPYING" in the main directory of this archive
4fcf5ef2aSThomas Huth  * for more details.
5fcf5ef2aSThomas Huth  *
6fcf5ef2aSThomas Huth  * KVM/MIPS: MIPS specific KVM APIs
7fcf5ef2aSThomas Huth  *
8fcf5ef2aSThomas Huth  * Copyright (C) 2012-2014 Imagination Technologies Ltd.
9fcf5ef2aSThomas Huth  * Authors: Sanjay Lal <sanjayl@kymasys.com>
10fcf5ef2aSThomas Huth */
11fcf5ef2aSThomas Huth 
12fcf5ef2aSThomas Huth #include "qemu/osdep.h"
13fcf5ef2aSThomas Huth #include <sys/ioctl.h>
14fcf5ef2aSThomas Huth 
15fcf5ef2aSThomas Huth #include <linux/kvm.h>
16fcf5ef2aSThomas Huth 
17fcf5ef2aSThomas Huth #include "qemu-common.h"
18fcf5ef2aSThomas Huth #include "cpu.h"
19fcf5ef2aSThomas Huth #include "qemu/error-report.h"
20fcf5ef2aSThomas Huth #include "qemu/timer.h"
21fcf5ef2aSThomas Huth #include "sysemu/sysemu.h"
22fcf5ef2aSThomas Huth #include "sysemu/kvm.h"
23fcf5ef2aSThomas Huth #include "sysemu/cpus.h"
24fcf5ef2aSThomas Huth #include "kvm_mips.h"
25fcf5ef2aSThomas Huth #include "exec/memattrs.h"
26fcf5ef2aSThomas Huth 
27fcf5ef2aSThomas Huth #define DEBUG_KVM 0
28fcf5ef2aSThomas Huth 
29fcf5ef2aSThomas Huth #define DPRINTF(fmt, ...) \
30fcf5ef2aSThomas Huth     do { if (DEBUG_KVM) { fprintf(stderr, fmt, ## __VA_ARGS__); } } while (0)
31fcf5ef2aSThomas Huth 
32fcf5ef2aSThomas Huth static int kvm_mips_fpu_cap;
33fcf5ef2aSThomas Huth static int kvm_mips_msa_cap;
34fcf5ef2aSThomas Huth 
35fcf5ef2aSThomas Huth const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
36fcf5ef2aSThomas Huth     KVM_CAP_LAST_INFO
37fcf5ef2aSThomas Huth };
38fcf5ef2aSThomas Huth 
39fcf5ef2aSThomas Huth static void kvm_mips_update_state(void *opaque, int running, RunState state);
40fcf5ef2aSThomas Huth 
41fcf5ef2aSThomas Huth unsigned long kvm_arch_vcpu_id(CPUState *cs)
42fcf5ef2aSThomas Huth {
43fcf5ef2aSThomas Huth     return cs->cpu_index;
44fcf5ef2aSThomas Huth }
45fcf5ef2aSThomas Huth 
46fcf5ef2aSThomas Huth int kvm_arch_init(MachineState *ms, KVMState *s)
47fcf5ef2aSThomas Huth {
48fcf5ef2aSThomas Huth     /* MIPS has 128 signals */
49fcf5ef2aSThomas Huth     kvm_set_sigmask_len(s, 16);
50fcf5ef2aSThomas Huth 
51fcf5ef2aSThomas Huth     kvm_mips_fpu_cap = kvm_check_extension(s, KVM_CAP_MIPS_FPU);
52fcf5ef2aSThomas Huth     kvm_mips_msa_cap = kvm_check_extension(s, KVM_CAP_MIPS_MSA);
53fcf5ef2aSThomas Huth 
54fcf5ef2aSThomas Huth     DPRINTF("%s\n", __func__);
55fcf5ef2aSThomas Huth     return 0;
56fcf5ef2aSThomas Huth }
57fcf5ef2aSThomas Huth 
58*d525ffabSPaolo Bonzini int kvm_arch_irqchip_create(MachineState *ms, KVMState *s)
59*d525ffabSPaolo Bonzini {
60*d525ffabSPaolo Bonzini     return 0;
61*d525ffabSPaolo Bonzini }
62*d525ffabSPaolo Bonzini 
63fcf5ef2aSThomas Huth int kvm_arch_init_vcpu(CPUState *cs)
64fcf5ef2aSThomas Huth {
65fcf5ef2aSThomas Huth     MIPSCPU *cpu = MIPS_CPU(cs);
66fcf5ef2aSThomas Huth     CPUMIPSState *env = &cpu->env;
67fcf5ef2aSThomas Huth     int ret = 0;
68fcf5ef2aSThomas Huth 
69fcf5ef2aSThomas Huth     qemu_add_vm_change_state_handler(kvm_mips_update_state, cs);
70fcf5ef2aSThomas Huth 
71fcf5ef2aSThomas Huth     if (kvm_mips_fpu_cap && env->CP0_Config1 & (1 << CP0C1_FP)) {
72fcf5ef2aSThomas Huth         ret = kvm_vcpu_enable_cap(cs, KVM_CAP_MIPS_FPU, 0, 0);
73fcf5ef2aSThomas Huth         if (ret < 0) {
74fcf5ef2aSThomas Huth             /* mark unsupported so it gets disabled on reset */
75fcf5ef2aSThomas Huth             kvm_mips_fpu_cap = 0;
76fcf5ef2aSThomas Huth             ret = 0;
77fcf5ef2aSThomas Huth         }
78fcf5ef2aSThomas Huth     }
79fcf5ef2aSThomas Huth 
80fcf5ef2aSThomas Huth     if (kvm_mips_msa_cap && env->CP0_Config3 & (1 << CP0C3_MSAP)) {
81fcf5ef2aSThomas Huth         ret = kvm_vcpu_enable_cap(cs, KVM_CAP_MIPS_MSA, 0, 0);
82fcf5ef2aSThomas Huth         if (ret < 0) {
83fcf5ef2aSThomas Huth             /* mark unsupported so it gets disabled on reset */
84fcf5ef2aSThomas Huth             kvm_mips_msa_cap = 0;
85fcf5ef2aSThomas Huth             ret = 0;
86fcf5ef2aSThomas Huth         }
87fcf5ef2aSThomas Huth     }
88fcf5ef2aSThomas Huth 
89fcf5ef2aSThomas Huth     DPRINTF("%s\n", __func__);
90fcf5ef2aSThomas Huth     return ret;
91fcf5ef2aSThomas Huth }
92fcf5ef2aSThomas Huth 
93fcf5ef2aSThomas Huth void kvm_mips_reset_vcpu(MIPSCPU *cpu)
94fcf5ef2aSThomas Huth {
95fcf5ef2aSThomas Huth     CPUMIPSState *env = &cpu->env;
96fcf5ef2aSThomas Huth 
97fcf5ef2aSThomas Huth     if (!kvm_mips_fpu_cap && env->CP0_Config1 & (1 << CP0C1_FP)) {
98fcf5ef2aSThomas Huth         fprintf(stderr, "Warning: KVM does not support FPU, disabling\n");
99fcf5ef2aSThomas Huth         env->CP0_Config1 &= ~(1 << CP0C1_FP);
100fcf5ef2aSThomas Huth     }
101fcf5ef2aSThomas Huth     if (!kvm_mips_msa_cap && env->CP0_Config3 & (1 << CP0C3_MSAP)) {
102fcf5ef2aSThomas Huth         fprintf(stderr, "Warning: KVM does not support MSA, disabling\n");
103fcf5ef2aSThomas Huth         env->CP0_Config3 &= ~(1 << CP0C3_MSAP);
104fcf5ef2aSThomas Huth     }
105fcf5ef2aSThomas Huth 
106fcf5ef2aSThomas Huth     DPRINTF("%s\n", __func__);
107fcf5ef2aSThomas Huth }
108fcf5ef2aSThomas Huth 
109fcf5ef2aSThomas Huth int kvm_arch_insert_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
110fcf5ef2aSThomas Huth {
111fcf5ef2aSThomas Huth     DPRINTF("%s\n", __func__);
112fcf5ef2aSThomas Huth     return 0;
113fcf5ef2aSThomas Huth }
114fcf5ef2aSThomas Huth 
115fcf5ef2aSThomas Huth int kvm_arch_remove_sw_breakpoint(CPUState *cs, struct kvm_sw_breakpoint *bp)
116fcf5ef2aSThomas Huth {
117fcf5ef2aSThomas Huth     DPRINTF("%s\n", __func__);
118fcf5ef2aSThomas Huth     return 0;
119fcf5ef2aSThomas Huth }
120fcf5ef2aSThomas Huth 
121fcf5ef2aSThomas Huth static inline int cpu_mips_io_interrupts_pending(MIPSCPU *cpu)
122fcf5ef2aSThomas Huth {
123fcf5ef2aSThomas Huth     CPUMIPSState *env = &cpu->env;
124fcf5ef2aSThomas Huth 
125fcf5ef2aSThomas Huth     return env->CP0_Cause & (0x1 << (2 + CP0Ca_IP));
126fcf5ef2aSThomas Huth }
127fcf5ef2aSThomas Huth 
128fcf5ef2aSThomas Huth 
129fcf5ef2aSThomas Huth void kvm_arch_pre_run(CPUState *cs, struct kvm_run *run)
130fcf5ef2aSThomas Huth {
131fcf5ef2aSThomas Huth     MIPSCPU *cpu = MIPS_CPU(cs);
132fcf5ef2aSThomas Huth     int r;
133fcf5ef2aSThomas Huth     struct kvm_mips_interrupt intr;
134fcf5ef2aSThomas Huth 
135fcf5ef2aSThomas Huth     qemu_mutex_lock_iothread();
136fcf5ef2aSThomas Huth 
137fcf5ef2aSThomas Huth     if ((cs->interrupt_request & CPU_INTERRUPT_HARD) &&
138fcf5ef2aSThomas Huth             cpu_mips_io_interrupts_pending(cpu)) {
139fcf5ef2aSThomas Huth         intr.cpu = -1;
140fcf5ef2aSThomas Huth         intr.irq = 2;
141fcf5ef2aSThomas Huth         r = kvm_vcpu_ioctl(cs, KVM_INTERRUPT, &intr);
142fcf5ef2aSThomas Huth         if (r < 0) {
143fcf5ef2aSThomas Huth             error_report("%s: cpu %d: failed to inject IRQ %x",
144fcf5ef2aSThomas Huth                          __func__, cs->cpu_index, intr.irq);
145fcf5ef2aSThomas Huth         }
146fcf5ef2aSThomas Huth     }
147fcf5ef2aSThomas Huth 
148fcf5ef2aSThomas Huth     qemu_mutex_unlock_iothread();
149fcf5ef2aSThomas Huth }
150fcf5ef2aSThomas Huth 
151fcf5ef2aSThomas Huth MemTxAttrs kvm_arch_post_run(CPUState *cs, struct kvm_run *run)
152fcf5ef2aSThomas Huth {
153fcf5ef2aSThomas Huth     return MEMTXATTRS_UNSPECIFIED;
154fcf5ef2aSThomas Huth }
155fcf5ef2aSThomas Huth 
156fcf5ef2aSThomas Huth int kvm_arch_process_async_events(CPUState *cs)
157fcf5ef2aSThomas Huth {
158fcf5ef2aSThomas Huth     return cs->halted;
159fcf5ef2aSThomas Huth }
160fcf5ef2aSThomas Huth 
161fcf5ef2aSThomas Huth int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
162fcf5ef2aSThomas Huth {
163fcf5ef2aSThomas Huth     int ret;
164fcf5ef2aSThomas Huth 
165fcf5ef2aSThomas Huth     DPRINTF("%s\n", __func__);
166fcf5ef2aSThomas Huth     switch (run->exit_reason) {
167fcf5ef2aSThomas Huth     default:
168fcf5ef2aSThomas Huth         error_report("%s: unknown exit reason %d",
169fcf5ef2aSThomas Huth                      __func__, run->exit_reason);
170fcf5ef2aSThomas Huth         ret = -1;
171fcf5ef2aSThomas Huth         break;
172fcf5ef2aSThomas Huth     }
173fcf5ef2aSThomas Huth 
174fcf5ef2aSThomas Huth     return ret;
175fcf5ef2aSThomas Huth }
176fcf5ef2aSThomas Huth 
177fcf5ef2aSThomas Huth bool kvm_arch_stop_on_emulation_error(CPUState *cs)
178fcf5ef2aSThomas Huth {
179fcf5ef2aSThomas Huth     DPRINTF("%s\n", __func__);
180fcf5ef2aSThomas Huth     return true;
181fcf5ef2aSThomas Huth }
182fcf5ef2aSThomas Huth 
183fcf5ef2aSThomas Huth int kvm_arch_on_sigbus_vcpu(CPUState *cs, int code, void *addr)
184fcf5ef2aSThomas Huth {
185fcf5ef2aSThomas Huth     DPRINTF("%s\n", __func__);
186fcf5ef2aSThomas Huth     return 1;
187fcf5ef2aSThomas Huth }
188fcf5ef2aSThomas Huth 
189fcf5ef2aSThomas Huth int kvm_arch_on_sigbus(int code, void *addr)
190fcf5ef2aSThomas Huth {
191fcf5ef2aSThomas Huth     DPRINTF("%s\n", __func__);
192fcf5ef2aSThomas Huth     return 1;
193fcf5ef2aSThomas Huth }
194fcf5ef2aSThomas Huth 
195fcf5ef2aSThomas Huth void kvm_arch_init_irq_routing(KVMState *s)
196fcf5ef2aSThomas Huth {
197fcf5ef2aSThomas Huth }
198fcf5ef2aSThomas Huth 
199fcf5ef2aSThomas Huth int kvm_mips_set_interrupt(MIPSCPU *cpu, int irq, int level)
200fcf5ef2aSThomas Huth {
201fcf5ef2aSThomas Huth     CPUState *cs = CPU(cpu);
202fcf5ef2aSThomas Huth     struct kvm_mips_interrupt intr;
203fcf5ef2aSThomas Huth 
204fcf5ef2aSThomas Huth     if (!kvm_enabled()) {
205fcf5ef2aSThomas Huth         return 0;
206fcf5ef2aSThomas Huth     }
207fcf5ef2aSThomas Huth 
208fcf5ef2aSThomas Huth     intr.cpu = -1;
209fcf5ef2aSThomas Huth 
210fcf5ef2aSThomas Huth     if (level) {
211fcf5ef2aSThomas Huth         intr.irq = irq;
212fcf5ef2aSThomas Huth     } else {
213fcf5ef2aSThomas Huth         intr.irq = -irq;
214fcf5ef2aSThomas Huth     }
215fcf5ef2aSThomas Huth 
216fcf5ef2aSThomas Huth     kvm_vcpu_ioctl(cs, KVM_INTERRUPT, &intr);
217fcf5ef2aSThomas Huth 
218fcf5ef2aSThomas Huth     return 0;
219fcf5ef2aSThomas Huth }
220fcf5ef2aSThomas Huth 
221fcf5ef2aSThomas Huth int kvm_mips_set_ipi_interrupt(MIPSCPU *cpu, int irq, int level)
222fcf5ef2aSThomas Huth {
223fcf5ef2aSThomas Huth     CPUState *cs = current_cpu;
224fcf5ef2aSThomas Huth     CPUState *dest_cs = CPU(cpu);
225fcf5ef2aSThomas Huth     struct kvm_mips_interrupt intr;
226fcf5ef2aSThomas Huth 
227fcf5ef2aSThomas Huth     if (!kvm_enabled()) {
228fcf5ef2aSThomas Huth         return 0;
229fcf5ef2aSThomas Huth     }
230fcf5ef2aSThomas Huth 
231fcf5ef2aSThomas Huth     intr.cpu = dest_cs->cpu_index;
232fcf5ef2aSThomas Huth 
233fcf5ef2aSThomas Huth     if (level) {
234fcf5ef2aSThomas Huth         intr.irq = irq;
235fcf5ef2aSThomas Huth     } else {
236fcf5ef2aSThomas Huth         intr.irq = -irq;
237fcf5ef2aSThomas Huth     }
238fcf5ef2aSThomas Huth 
239fcf5ef2aSThomas Huth     DPRINTF("%s: CPU %d, IRQ: %d\n", __func__, intr.cpu, intr.irq);
240fcf5ef2aSThomas Huth 
241fcf5ef2aSThomas Huth     kvm_vcpu_ioctl(cs, KVM_INTERRUPT, &intr);
242fcf5ef2aSThomas Huth 
243fcf5ef2aSThomas Huth     return 0;
244fcf5ef2aSThomas Huth }
245fcf5ef2aSThomas Huth 
246fcf5ef2aSThomas Huth #define MIPS_CP0_32(_R, _S)                                     \
247fcf5ef2aSThomas Huth     (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U32 | (8 * (_R) + (_S)))
248fcf5ef2aSThomas Huth 
249fcf5ef2aSThomas Huth #define MIPS_CP0_64(_R, _S)                                     \
250fcf5ef2aSThomas Huth     (KVM_REG_MIPS_CP0 | KVM_REG_SIZE_U64 | (8 * (_R) + (_S)))
251fcf5ef2aSThomas Huth 
252fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_INDEX          MIPS_CP0_32(0, 0)
253fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_CONTEXT        MIPS_CP0_64(4, 0)
254fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_USERLOCAL      MIPS_CP0_64(4, 2)
255fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_PAGEMASK       MIPS_CP0_32(5, 0)
256fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_WIRED          MIPS_CP0_32(6, 0)
257fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_HWRENA         MIPS_CP0_32(7, 0)
258fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_BADVADDR       MIPS_CP0_64(8, 0)
259fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_COUNT          MIPS_CP0_32(9, 0)
260fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_ENTRYHI        MIPS_CP0_64(10, 0)
261fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_COMPARE        MIPS_CP0_32(11, 0)
262fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_STATUS         MIPS_CP0_32(12, 0)
263fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_CAUSE          MIPS_CP0_32(13, 0)
264fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_EPC            MIPS_CP0_64(14, 0)
265fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_PRID           MIPS_CP0_32(15, 0)
266fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_CONFIG         MIPS_CP0_32(16, 0)
267fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_CONFIG1        MIPS_CP0_32(16, 1)
268fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_CONFIG2        MIPS_CP0_32(16, 2)
269fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_CONFIG3        MIPS_CP0_32(16, 3)
270fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_CONFIG4        MIPS_CP0_32(16, 4)
271fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_CONFIG5        MIPS_CP0_32(16, 5)
272fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_ERROREPC       MIPS_CP0_64(30, 0)
273fcf5ef2aSThomas Huth 
274fcf5ef2aSThomas Huth static inline int kvm_mips_put_one_reg(CPUState *cs, uint64_t reg_id,
275fcf5ef2aSThomas Huth                                        int32_t *addr)
276fcf5ef2aSThomas Huth {
277fcf5ef2aSThomas Huth     struct kvm_one_reg cp0reg = {
278fcf5ef2aSThomas Huth         .id = reg_id,
279fcf5ef2aSThomas Huth         .addr = (uintptr_t)addr
280fcf5ef2aSThomas Huth     };
281fcf5ef2aSThomas Huth 
282fcf5ef2aSThomas Huth     return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &cp0reg);
283fcf5ef2aSThomas Huth }
284fcf5ef2aSThomas Huth 
285fcf5ef2aSThomas Huth static inline int kvm_mips_put_one_ureg(CPUState *cs, uint64_t reg_id,
286fcf5ef2aSThomas Huth                                         uint32_t *addr)
287fcf5ef2aSThomas Huth {
288fcf5ef2aSThomas Huth     struct kvm_one_reg cp0reg = {
289fcf5ef2aSThomas Huth         .id = reg_id,
290fcf5ef2aSThomas Huth         .addr = (uintptr_t)addr
291fcf5ef2aSThomas Huth     };
292fcf5ef2aSThomas Huth 
293fcf5ef2aSThomas Huth     return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &cp0reg);
294fcf5ef2aSThomas Huth }
295fcf5ef2aSThomas Huth 
296fcf5ef2aSThomas Huth static inline int kvm_mips_put_one_ulreg(CPUState *cs, uint64_t reg_id,
297fcf5ef2aSThomas Huth                                          target_ulong *addr)
298fcf5ef2aSThomas Huth {
299fcf5ef2aSThomas Huth     uint64_t val64 = *addr;
300fcf5ef2aSThomas Huth     struct kvm_one_reg cp0reg = {
301fcf5ef2aSThomas Huth         .id = reg_id,
302fcf5ef2aSThomas Huth         .addr = (uintptr_t)&val64
303fcf5ef2aSThomas Huth     };
304fcf5ef2aSThomas Huth 
305fcf5ef2aSThomas Huth     return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &cp0reg);
306fcf5ef2aSThomas Huth }
307fcf5ef2aSThomas Huth 
308fcf5ef2aSThomas Huth static inline int kvm_mips_put_one_reg64(CPUState *cs, uint64_t reg_id,
309fcf5ef2aSThomas Huth                                          int64_t *addr)
310fcf5ef2aSThomas Huth {
311fcf5ef2aSThomas Huth     struct kvm_one_reg cp0reg = {
312fcf5ef2aSThomas Huth         .id = reg_id,
313fcf5ef2aSThomas Huth         .addr = (uintptr_t)addr
314fcf5ef2aSThomas Huth     };
315fcf5ef2aSThomas Huth 
316fcf5ef2aSThomas Huth     return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &cp0reg);
317fcf5ef2aSThomas Huth }
318fcf5ef2aSThomas Huth 
319fcf5ef2aSThomas Huth static inline int kvm_mips_put_one_ureg64(CPUState *cs, uint64_t reg_id,
320fcf5ef2aSThomas Huth                                           uint64_t *addr)
321fcf5ef2aSThomas Huth {
322fcf5ef2aSThomas Huth     struct kvm_one_reg cp0reg = {
323fcf5ef2aSThomas Huth         .id = reg_id,
324fcf5ef2aSThomas Huth         .addr = (uintptr_t)addr
325fcf5ef2aSThomas Huth     };
326fcf5ef2aSThomas Huth 
327fcf5ef2aSThomas Huth     return kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &cp0reg);
328fcf5ef2aSThomas Huth }
329fcf5ef2aSThomas Huth 
330fcf5ef2aSThomas Huth static inline int kvm_mips_get_one_reg(CPUState *cs, uint64_t reg_id,
331fcf5ef2aSThomas Huth                                        int32_t *addr)
332fcf5ef2aSThomas Huth {
333fcf5ef2aSThomas Huth     struct kvm_one_reg cp0reg = {
334fcf5ef2aSThomas Huth         .id = reg_id,
335fcf5ef2aSThomas Huth         .addr = (uintptr_t)addr
336fcf5ef2aSThomas Huth     };
337fcf5ef2aSThomas Huth 
338fcf5ef2aSThomas Huth     return kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &cp0reg);
339fcf5ef2aSThomas Huth }
340fcf5ef2aSThomas Huth 
341fcf5ef2aSThomas Huth static inline int kvm_mips_get_one_ureg(CPUState *cs, uint64_t reg_id,
342fcf5ef2aSThomas Huth                                         uint32_t *addr)
343fcf5ef2aSThomas Huth {
344fcf5ef2aSThomas Huth     struct kvm_one_reg cp0reg = {
345fcf5ef2aSThomas Huth         .id = reg_id,
346fcf5ef2aSThomas Huth         .addr = (uintptr_t)addr
347fcf5ef2aSThomas Huth     };
348fcf5ef2aSThomas Huth 
349fcf5ef2aSThomas Huth     return kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &cp0reg);
350fcf5ef2aSThomas Huth }
351fcf5ef2aSThomas Huth 
352fcf5ef2aSThomas Huth static inline int kvm_mips_get_one_ulreg(CPUState *cs, uint64_t reg_id,
353fcf5ef2aSThomas Huth                                          target_ulong *addr)
354fcf5ef2aSThomas Huth {
355fcf5ef2aSThomas Huth     int ret;
356fcf5ef2aSThomas Huth     uint64_t val64 = 0;
357fcf5ef2aSThomas Huth     struct kvm_one_reg cp0reg = {
358fcf5ef2aSThomas Huth         .id = reg_id,
359fcf5ef2aSThomas Huth         .addr = (uintptr_t)&val64
360fcf5ef2aSThomas Huth     };
361fcf5ef2aSThomas Huth 
362fcf5ef2aSThomas Huth     ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &cp0reg);
363fcf5ef2aSThomas Huth     if (ret >= 0) {
364fcf5ef2aSThomas Huth         *addr = val64;
365fcf5ef2aSThomas Huth     }
366fcf5ef2aSThomas Huth     return ret;
367fcf5ef2aSThomas Huth }
368fcf5ef2aSThomas Huth 
369fcf5ef2aSThomas Huth static inline int kvm_mips_get_one_reg64(CPUState *cs, uint64_t reg_id,
370fcf5ef2aSThomas Huth                                          int64_t *addr)
371fcf5ef2aSThomas Huth {
372fcf5ef2aSThomas Huth     struct kvm_one_reg cp0reg = {
373fcf5ef2aSThomas Huth         .id = reg_id,
374fcf5ef2aSThomas Huth         .addr = (uintptr_t)addr
375fcf5ef2aSThomas Huth     };
376fcf5ef2aSThomas Huth 
377fcf5ef2aSThomas Huth     return kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &cp0reg);
378fcf5ef2aSThomas Huth }
379fcf5ef2aSThomas Huth 
380fcf5ef2aSThomas Huth static inline int kvm_mips_get_one_ureg64(CPUState *cs, uint64_t reg_id,
381fcf5ef2aSThomas Huth                                           uint64_t *addr)
382fcf5ef2aSThomas Huth {
383fcf5ef2aSThomas Huth     struct kvm_one_reg cp0reg = {
384fcf5ef2aSThomas Huth         .id = reg_id,
385fcf5ef2aSThomas Huth         .addr = (uintptr_t)addr
386fcf5ef2aSThomas Huth     };
387fcf5ef2aSThomas Huth 
388fcf5ef2aSThomas Huth     return kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &cp0reg);
389fcf5ef2aSThomas Huth }
390fcf5ef2aSThomas Huth 
391fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_CONFIG_MASK    (1U << CP0C0_M)
392fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_CONFIG1_MASK   ((1U << CP0C1_M) | \
393fcf5ef2aSThomas Huth                                          (1U << CP0C1_FP))
394fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_CONFIG2_MASK   (1U << CP0C2_M)
395fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_CONFIG3_MASK   ((1U << CP0C3_M) | \
396fcf5ef2aSThomas Huth                                          (1U << CP0C3_MSAP))
397fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_CONFIG4_MASK   (1U << CP0C4_M)
398fcf5ef2aSThomas Huth #define KVM_REG_MIPS_CP0_CONFIG5_MASK   ((1U << CP0C5_MSAEn) | \
399fcf5ef2aSThomas Huth                                          (1U << CP0C5_UFE) | \
400fcf5ef2aSThomas Huth                                          (1U << CP0C5_FRE) | \
401fcf5ef2aSThomas Huth                                          (1U << CP0C5_UFR))
402fcf5ef2aSThomas Huth 
403fcf5ef2aSThomas Huth static inline int kvm_mips_change_one_reg(CPUState *cs, uint64_t reg_id,
404fcf5ef2aSThomas Huth                                           int32_t *addr, int32_t mask)
405fcf5ef2aSThomas Huth {
406fcf5ef2aSThomas Huth     int err;
407fcf5ef2aSThomas Huth     int32_t tmp, change;
408fcf5ef2aSThomas Huth 
409fcf5ef2aSThomas Huth     err = kvm_mips_get_one_reg(cs, reg_id, &tmp);
410fcf5ef2aSThomas Huth     if (err < 0) {
411fcf5ef2aSThomas Huth         return err;
412fcf5ef2aSThomas Huth     }
413fcf5ef2aSThomas Huth 
414fcf5ef2aSThomas Huth     /* only change bits in mask */
415fcf5ef2aSThomas Huth     change = (*addr ^ tmp) & mask;
416fcf5ef2aSThomas Huth     if (!change) {
417fcf5ef2aSThomas Huth         return 0;
418fcf5ef2aSThomas Huth     }
419fcf5ef2aSThomas Huth 
420fcf5ef2aSThomas Huth     tmp = tmp ^ change;
421fcf5ef2aSThomas Huth     return kvm_mips_put_one_reg(cs, reg_id, &tmp);
422fcf5ef2aSThomas Huth }
423fcf5ef2aSThomas Huth 
424fcf5ef2aSThomas Huth /*
425fcf5ef2aSThomas Huth  * We freeze the KVM timer when either the VM clock is stopped or the state is
426fcf5ef2aSThomas Huth  * saved (the state is dirty).
427fcf5ef2aSThomas Huth  */
428fcf5ef2aSThomas Huth 
429fcf5ef2aSThomas Huth /*
430fcf5ef2aSThomas Huth  * Save the state of the KVM timer when VM clock is stopped or state is synced
431fcf5ef2aSThomas Huth  * to QEMU.
432fcf5ef2aSThomas Huth  */
433fcf5ef2aSThomas Huth static int kvm_mips_save_count(CPUState *cs)
434fcf5ef2aSThomas Huth {
435fcf5ef2aSThomas Huth     MIPSCPU *cpu = MIPS_CPU(cs);
436fcf5ef2aSThomas Huth     CPUMIPSState *env = &cpu->env;
437fcf5ef2aSThomas Huth     uint64_t count_ctl;
438fcf5ef2aSThomas Huth     int err, ret = 0;
439fcf5ef2aSThomas Huth 
440fcf5ef2aSThomas Huth     /* freeze KVM timer */
441fcf5ef2aSThomas Huth     err = kvm_mips_get_one_ureg64(cs, KVM_REG_MIPS_COUNT_CTL, &count_ctl);
442fcf5ef2aSThomas Huth     if (err < 0) {
443fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__, err);
444fcf5ef2aSThomas Huth         ret = err;
445fcf5ef2aSThomas Huth     } else if (!(count_ctl & KVM_REG_MIPS_COUNT_CTL_DC)) {
446fcf5ef2aSThomas Huth         count_ctl |= KVM_REG_MIPS_COUNT_CTL_DC;
447fcf5ef2aSThomas Huth         err = kvm_mips_put_one_ureg64(cs, KVM_REG_MIPS_COUNT_CTL, &count_ctl);
448fcf5ef2aSThomas Huth         if (err < 0) {
449fcf5ef2aSThomas Huth             DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__, err);
450fcf5ef2aSThomas Huth             ret = err;
451fcf5ef2aSThomas Huth         }
452fcf5ef2aSThomas Huth     }
453fcf5ef2aSThomas Huth 
454fcf5ef2aSThomas Huth     /* read CP0_Cause */
455fcf5ef2aSThomas Huth     err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_CAUSE, &env->CP0_Cause);
456fcf5ef2aSThomas Huth     if (err < 0) {
457fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_CAUSE (%d)\n", __func__, err);
458fcf5ef2aSThomas Huth         ret = err;
459fcf5ef2aSThomas Huth     }
460fcf5ef2aSThomas Huth 
461fcf5ef2aSThomas Huth     /* read CP0_Count */
462fcf5ef2aSThomas Huth     err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_COUNT, &env->CP0_Count);
463fcf5ef2aSThomas Huth     if (err < 0) {
464fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_COUNT (%d)\n", __func__, err);
465fcf5ef2aSThomas Huth         ret = err;
466fcf5ef2aSThomas Huth     }
467fcf5ef2aSThomas Huth 
468fcf5ef2aSThomas Huth     return ret;
469fcf5ef2aSThomas Huth }
470fcf5ef2aSThomas Huth 
471fcf5ef2aSThomas Huth /*
472fcf5ef2aSThomas Huth  * Restore the state of the KVM timer when VM clock is restarted or state is
473fcf5ef2aSThomas Huth  * synced to KVM.
474fcf5ef2aSThomas Huth  */
475fcf5ef2aSThomas Huth static int kvm_mips_restore_count(CPUState *cs)
476fcf5ef2aSThomas Huth {
477fcf5ef2aSThomas Huth     MIPSCPU *cpu = MIPS_CPU(cs);
478fcf5ef2aSThomas Huth     CPUMIPSState *env = &cpu->env;
479fcf5ef2aSThomas Huth     uint64_t count_ctl;
480fcf5ef2aSThomas Huth     int err_dc, err, ret = 0;
481fcf5ef2aSThomas Huth 
482fcf5ef2aSThomas Huth     /* check the timer is frozen */
483fcf5ef2aSThomas Huth     err_dc = kvm_mips_get_one_ureg64(cs, KVM_REG_MIPS_COUNT_CTL, &count_ctl);
484fcf5ef2aSThomas Huth     if (err_dc < 0) {
485fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get COUNT_CTL (%d)\n", __func__, err_dc);
486fcf5ef2aSThomas Huth         ret = err_dc;
487fcf5ef2aSThomas Huth     } else if (!(count_ctl & KVM_REG_MIPS_COUNT_CTL_DC)) {
488fcf5ef2aSThomas Huth         /* freeze timer (sets COUNT_RESUME for us) */
489fcf5ef2aSThomas Huth         count_ctl |= KVM_REG_MIPS_COUNT_CTL_DC;
490fcf5ef2aSThomas Huth         err = kvm_mips_put_one_ureg64(cs, KVM_REG_MIPS_COUNT_CTL, &count_ctl);
491fcf5ef2aSThomas Huth         if (err < 0) {
492fcf5ef2aSThomas Huth             DPRINTF("%s: Failed to set COUNT_CTL.DC=1 (%d)\n", __func__, err);
493fcf5ef2aSThomas Huth             ret = err;
494fcf5ef2aSThomas Huth         }
495fcf5ef2aSThomas Huth     }
496fcf5ef2aSThomas Huth 
497fcf5ef2aSThomas Huth     /* load CP0_Cause */
498fcf5ef2aSThomas Huth     err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_CAUSE, &env->CP0_Cause);
499fcf5ef2aSThomas Huth     if (err < 0) {
500fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to put CP0_CAUSE (%d)\n", __func__, err);
501fcf5ef2aSThomas Huth         ret = err;
502fcf5ef2aSThomas Huth     }
503fcf5ef2aSThomas Huth 
504fcf5ef2aSThomas Huth     /* load CP0_Count */
505fcf5ef2aSThomas Huth     err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_COUNT, &env->CP0_Count);
506fcf5ef2aSThomas Huth     if (err < 0) {
507fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to put CP0_COUNT (%d)\n", __func__, err);
508fcf5ef2aSThomas Huth         ret = err;
509fcf5ef2aSThomas Huth     }
510fcf5ef2aSThomas Huth 
511fcf5ef2aSThomas Huth     /* resume KVM timer */
512fcf5ef2aSThomas Huth     if (err_dc >= 0) {
513fcf5ef2aSThomas Huth         count_ctl &= ~KVM_REG_MIPS_COUNT_CTL_DC;
514fcf5ef2aSThomas Huth         err = kvm_mips_put_one_ureg64(cs, KVM_REG_MIPS_COUNT_CTL, &count_ctl);
515fcf5ef2aSThomas Huth         if (err < 0) {
516fcf5ef2aSThomas Huth             DPRINTF("%s: Failed to set COUNT_CTL.DC=0 (%d)\n", __func__, err);
517fcf5ef2aSThomas Huth             ret = err;
518fcf5ef2aSThomas Huth         }
519fcf5ef2aSThomas Huth     }
520fcf5ef2aSThomas Huth 
521fcf5ef2aSThomas Huth     return ret;
522fcf5ef2aSThomas Huth }
523fcf5ef2aSThomas Huth 
524fcf5ef2aSThomas Huth /*
525fcf5ef2aSThomas Huth  * Handle the VM clock being started or stopped
526fcf5ef2aSThomas Huth  */
527fcf5ef2aSThomas Huth static void kvm_mips_update_state(void *opaque, int running, RunState state)
528fcf5ef2aSThomas Huth {
529fcf5ef2aSThomas Huth     CPUState *cs = opaque;
530fcf5ef2aSThomas Huth     int ret;
531fcf5ef2aSThomas Huth     uint64_t count_resume;
532fcf5ef2aSThomas Huth 
533fcf5ef2aSThomas Huth     /*
534fcf5ef2aSThomas Huth      * If state is already dirty (synced to QEMU) then the KVM timer state is
535fcf5ef2aSThomas Huth      * already saved and can be restored when it is synced back to KVM.
536fcf5ef2aSThomas Huth      */
537fcf5ef2aSThomas Huth     if (!running) {
538fcf5ef2aSThomas Huth         if (!cs->kvm_vcpu_dirty) {
539fcf5ef2aSThomas Huth             ret = kvm_mips_save_count(cs);
540fcf5ef2aSThomas Huth             if (ret < 0) {
541fcf5ef2aSThomas Huth                 fprintf(stderr, "Failed saving count\n");
542fcf5ef2aSThomas Huth             }
543fcf5ef2aSThomas Huth         }
544fcf5ef2aSThomas Huth     } else {
545fcf5ef2aSThomas Huth         /* Set clock restore time to now */
546fcf5ef2aSThomas Huth         count_resume = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
547fcf5ef2aSThomas Huth         ret = kvm_mips_put_one_ureg64(cs, KVM_REG_MIPS_COUNT_RESUME,
548fcf5ef2aSThomas Huth                                       &count_resume);
549fcf5ef2aSThomas Huth         if (ret < 0) {
550fcf5ef2aSThomas Huth             fprintf(stderr, "Failed setting COUNT_RESUME\n");
551fcf5ef2aSThomas Huth             return;
552fcf5ef2aSThomas Huth         }
553fcf5ef2aSThomas Huth 
554fcf5ef2aSThomas Huth         if (!cs->kvm_vcpu_dirty) {
555fcf5ef2aSThomas Huth             ret = kvm_mips_restore_count(cs);
556fcf5ef2aSThomas Huth             if (ret < 0) {
557fcf5ef2aSThomas Huth                 fprintf(stderr, "Failed restoring count\n");
558fcf5ef2aSThomas Huth             }
559fcf5ef2aSThomas Huth         }
560fcf5ef2aSThomas Huth     }
561fcf5ef2aSThomas Huth }
562fcf5ef2aSThomas Huth 
563fcf5ef2aSThomas Huth static int kvm_mips_put_fpu_registers(CPUState *cs, int level)
564fcf5ef2aSThomas Huth {
565fcf5ef2aSThomas Huth     MIPSCPU *cpu = MIPS_CPU(cs);
566fcf5ef2aSThomas Huth     CPUMIPSState *env = &cpu->env;
567fcf5ef2aSThomas Huth     int err, ret = 0;
568fcf5ef2aSThomas Huth     unsigned int i;
569fcf5ef2aSThomas Huth 
570fcf5ef2aSThomas Huth     /* Only put FPU state if we're emulating a CPU with an FPU */
571fcf5ef2aSThomas Huth     if (env->CP0_Config1 & (1 << CP0C1_FP)) {
572fcf5ef2aSThomas Huth         /* FPU Control Registers */
573fcf5ef2aSThomas Huth         if (level == KVM_PUT_FULL_STATE) {
574fcf5ef2aSThomas Huth             err = kvm_mips_put_one_ureg(cs, KVM_REG_MIPS_FCR_IR,
575fcf5ef2aSThomas Huth                                         &env->active_fpu.fcr0);
576fcf5ef2aSThomas Huth             if (err < 0) {
577fcf5ef2aSThomas Huth                 DPRINTF("%s: Failed to put FCR_IR (%d)\n", __func__, err);
578fcf5ef2aSThomas Huth                 ret = err;
579fcf5ef2aSThomas Huth             }
580fcf5ef2aSThomas Huth         }
581fcf5ef2aSThomas Huth         err = kvm_mips_put_one_ureg(cs, KVM_REG_MIPS_FCR_CSR,
582fcf5ef2aSThomas Huth                                     &env->active_fpu.fcr31);
583fcf5ef2aSThomas Huth         if (err < 0) {
584fcf5ef2aSThomas Huth             DPRINTF("%s: Failed to put FCR_CSR (%d)\n", __func__, err);
585fcf5ef2aSThomas Huth             ret = err;
586fcf5ef2aSThomas Huth         }
587fcf5ef2aSThomas Huth 
588fcf5ef2aSThomas Huth         /*
589fcf5ef2aSThomas Huth          * FPU register state is a subset of MSA vector state, so don't put FPU
590fcf5ef2aSThomas Huth          * registers if we're emulating a CPU with MSA.
591fcf5ef2aSThomas Huth          */
592fcf5ef2aSThomas Huth         if (!(env->CP0_Config3 & (1 << CP0C3_MSAP))) {
593fcf5ef2aSThomas Huth             /* Floating point registers */
594fcf5ef2aSThomas Huth             for (i = 0; i < 32; ++i) {
595fcf5ef2aSThomas Huth                 if (env->CP0_Status & (1 << CP0St_FR)) {
596fcf5ef2aSThomas Huth                     err = kvm_mips_put_one_ureg64(cs, KVM_REG_MIPS_FPR_64(i),
597fcf5ef2aSThomas Huth                                                   &env->active_fpu.fpr[i].d);
598fcf5ef2aSThomas Huth                 } else {
599fcf5ef2aSThomas Huth                     err = kvm_mips_get_one_ureg(cs, KVM_REG_MIPS_FPR_32(i),
600fcf5ef2aSThomas Huth                                     &env->active_fpu.fpr[i].w[FP_ENDIAN_IDX]);
601fcf5ef2aSThomas Huth                 }
602fcf5ef2aSThomas Huth                 if (err < 0) {
603fcf5ef2aSThomas Huth                     DPRINTF("%s: Failed to put FPR%u (%d)\n", __func__, i, err);
604fcf5ef2aSThomas Huth                     ret = err;
605fcf5ef2aSThomas Huth                 }
606fcf5ef2aSThomas Huth             }
607fcf5ef2aSThomas Huth         }
608fcf5ef2aSThomas Huth     }
609fcf5ef2aSThomas Huth 
610fcf5ef2aSThomas Huth     /* Only put MSA state if we're emulating a CPU with MSA */
611fcf5ef2aSThomas Huth     if (env->CP0_Config3 & (1 << CP0C3_MSAP)) {
612fcf5ef2aSThomas Huth         /* MSA Control Registers */
613fcf5ef2aSThomas Huth         if (level == KVM_PUT_FULL_STATE) {
614fcf5ef2aSThomas Huth             err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_MSA_IR,
615fcf5ef2aSThomas Huth                                        &env->msair);
616fcf5ef2aSThomas Huth             if (err < 0) {
617fcf5ef2aSThomas Huth                 DPRINTF("%s: Failed to put MSA_IR (%d)\n", __func__, err);
618fcf5ef2aSThomas Huth                 ret = err;
619fcf5ef2aSThomas Huth             }
620fcf5ef2aSThomas Huth         }
621fcf5ef2aSThomas Huth         err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_MSA_CSR,
622fcf5ef2aSThomas Huth                                    &env->active_tc.msacsr);
623fcf5ef2aSThomas Huth         if (err < 0) {
624fcf5ef2aSThomas Huth             DPRINTF("%s: Failed to put MSA_CSR (%d)\n", __func__, err);
625fcf5ef2aSThomas Huth             ret = err;
626fcf5ef2aSThomas Huth         }
627fcf5ef2aSThomas Huth 
628fcf5ef2aSThomas Huth         /* Vector registers (includes FP registers) */
629fcf5ef2aSThomas Huth         for (i = 0; i < 32; ++i) {
630fcf5ef2aSThomas Huth             /* Big endian MSA not supported by QEMU yet anyway */
631fcf5ef2aSThomas Huth             err = kvm_mips_put_one_reg64(cs, KVM_REG_MIPS_VEC_128(i),
632fcf5ef2aSThomas Huth                                          env->active_fpu.fpr[i].wr.d);
633fcf5ef2aSThomas Huth             if (err < 0) {
634fcf5ef2aSThomas Huth                 DPRINTF("%s: Failed to put VEC%u (%d)\n", __func__, i, err);
635fcf5ef2aSThomas Huth                 ret = err;
636fcf5ef2aSThomas Huth             }
637fcf5ef2aSThomas Huth         }
638fcf5ef2aSThomas Huth     }
639fcf5ef2aSThomas Huth 
640fcf5ef2aSThomas Huth     return ret;
641fcf5ef2aSThomas Huth }
642fcf5ef2aSThomas Huth 
643fcf5ef2aSThomas Huth static int kvm_mips_get_fpu_registers(CPUState *cs)
644fcf5ef2aSThomas Huth {
645fcf5ef2aSThomas Huth     MIPSCPU *cpu = MIPS_CPU(cs);
646fcf5ef2aSThomas Huth     CPUMIPSState *env = &cpu->env;
647fcf5ef2aSThomas Huth     int err, ret = 0;
648fcf5ef2aSThomas Huth     unsigned int i;
649fcf5ef2aSThomas Huth 
650fcf5ef2aSThomas Huth     /* Only get FPU state if we're emulating a CPU with an FPU */
651fcf5ef2aSThomas Huth     if (env->CP0_Config1 & (1 << CP0C1_FP)) {
652fcf5ef2aSThomas Huth         /* FPU Control Registers */
653fcf5ef2aSThomas Huth         err = kvm_mips_get_one_ureg(cs, KVM_REG_MIPS_FCR_IR,
654fcf5ef2aSThomas Huth                                     &env->active_fpu.fcr0);
655fcf5ef2aSThomas Huth         if (err < 0) {
656fcf5ef2aSThomas Huth             DPRINTF("%s: Failed to get FCR_IR (%d)\n", __func__, err);
657fcf5ef2aSThomas Huth             ret = err;
658fcf5ef2aSThomas Huth         }
659fcf5ef2aSThomas Huth         err = kvm_mips_get_one_ureg(cs, KVM_REG_MIPS_FCR_CSR,
660fcf5ef2aSThomas Huth                                     &env->active_fpu.fcr31);
661fcf5ef2aSThomas Huth         if (err < 0) {
662fcf5ef2aSThomas Huth             DPRINTF("%s: Failed to get FCR_CSR (%d)\n", __func__, err);
663fcf5ef2aSThomas Huth             ret = err;
664fcf5ef2aSThomas Huth         } else {
665fcf5ef2aSThomas Huth             restore_fp_status(env);
666fcf5ef2aSThomas Huth         }
667fcf5ef2aSThomas Huth 
668fcf5ef2aSThomas Huth         /*
669fcf5ef2aSThomas Huth          * FPU register state is a subset of MSA vector state, so don't save FPU
670fcf5ef2aSThomas Huth          * registers if we're emulating a CPU with MSA.
671fcf5ef2aSThomas Huth          */
672fcf5ef2aSThomas Huth         if (!(env->CP0_Config3 & (1 << CP0C3_MSAP))) {
673fcf5ef2aSThomas Huth             /* Floating point registers */
674fcf5ef2aSThomas Huth             for (i = 0; i < 32; ++i) {
675fcf5ef2aSThomas Huth                 if (env->CP0_Status & (1 << CP0St_FR)) {
676fcf5ef2aSThomas Huth                     err = kvm_mips_get_one_ureg64(cs, KVM_REG_MIPS_FPR_64(i),
677fcf5ef2aSThomas Huth                                                   &env->active_fpu.fpr[i].d);
678fcf5ef2aSThomas Huth                 } else {
679fcf5ef2aSThomas Huth                     err = kvm_mips_get_one_ureg(cs, KVM_REG_MIPS_FPR_32(i),
680fcf5ef2aSThomas Huth                                     &env->active_fpu.fpr[i].w[FP_ENDIAN_IDX]);
681fcf5ef2aSThomas Huth                 }
682fcf5ef2aSThomas Huth                 if (err < 0) {
683fcf5ef2aSThomas Huth                     DPRINTF("%s: Failed to get FPR%u (%d)\n", __func__, i, err);
684fcf5ef2aSThomas Huth                     ret = err;
685fcf5ef2aSThomas Huth                 }
686fcf5ef2aSThomas Huth             }
687fcf5ef2aSThomas Huth         }
688fcf5ef2aSThomas Huth     }
689fcf5ef2aSThomas Huth 
690fcf5ef2aSThomas Huth     /* Only get MSA state if we're emulating a CPU with MSA */
691fcf5ef2aSThomas Huth     if (env->CP0_Config3 & (1 << CP0C3_MSAP)) {
692fcf5ef2aSThomas Huth         /* MSA Control Registers */
693fcf5ef2aSThomas Huth         err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_MSA_IR,
694fcf5ef2aSThomas Huth                                    &env->msair);
695fcf5ef2aSThomas Huth         if (err < 0) {
696fcf5ef2aSThomas Huth             DPRINTF("%s: Failed to get MSA_IR (%d)\n", __func__, err);
697fcf5ef2aSThomas Huth             ret = err;
698fcf5ef2aSThomas Huth         }
699fcf5ef2aSThomas Huth         err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_MSA_CSR,
700fcf5ef2aSThomas Huth                                    &env->active_tc.msacsr);
701fcf5ef2aSThomas Huth         if (err < 0) {
702fcf5ef2aSThomas Huth             DPRINTF("%s: Failed to get MSA_CSR (%d)\n", __func__, err);
703fcf5ef2aSThomas Huth             ret = err;
704fcf5ef2aSThomas Huth         } else {
705fcf5ef2aSThomas Huth             restore_msa_fp_status(env);
706fcf5ef2aSThomas Huth         }
707fcf5ef2aSThomas Huth 
708fcf5ef2aSThomas Huth         /* Vector registers (includes FP registers) */
709fcf5ef2aSThomas Huth         for (i = 0; i < 32; ++i) {
710fcf5ef2aSThomas Huth             /* Big endian MSA not supported by QEMU yet anyway */
711fcf5ef2aSThomas Huth             err = kvm_mips_get_one_reg64(cs, KVM_REG_MIPS_VEC_128(i),
712fcf5ef2aSThomas Huth                                          env->active_fpu.fpr[i].wr.d);
713fcf5ef2aSThomas Huth             if (err < 0) {
714fcf5ef2aSThomas Huth                 DPRINTF("%s: Failed to get VEC%u (%d)\n", __func__, i, err);
715fcf5ef2aSThomas Huth                 ret = err;
716fcf5ef2aSThomas Huth             }
717fcf5ef2aSThomas Huth         }
718fcf5ef2aSThomas Huth     }
719fcf5ef2aSThomas Huth 
720fcf5ef2aSThomas Huth     return ret;
721fcf5ef2aSThomas Huth }
722fcf5ef2aSThomas Huth 
723fcf5ef2aSThomas Huth 
724fcf5ef2aSThomas Huth static int kvm_mips_put_cp0_registers(CPUState *cs, int level)
725fcf5ef2aSThomas Huth {
726fcf5ef2aSThomas Huth     MIPSCPU *cpu = MIPS_CPU(cs);
727fcf5ef2aSThomas Huth     CPUMIPSState *env = &cpu->env;
728fcf5ef2aSThomas Huth     int err, ret = 0;
729fcf5ef2aSThomas Huth 
730fcf5ef2aSThomas Huth     (void)level;
731fcf5ef2aSThomas Huth 
732fcf5ef2aSThomas Huth     err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_INDEX, &env->CP0_Index);
733fcf5ef2aSThomas Huth     if (err < 0) {
734fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to put CP0_INDEX (%d)\n", __func__, err);
735fcf5ef2aSThomas Huth         ret = err;
736fcf5ef2aSThomas Huth     }
737fcf5ef2aSThomas Huth     err = kvm_mips_put_one_ulreg(cs, KVM_REG_MIPS_CP0_CONTEXT,
738fcf5ef2aSThomas Huth                                  &env->CP0_Context);
739fcf5ef2aSThomas Huth     if (err < 0) {
740fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to put CP0_CONTEXT (%d)\n", __func__, err);
741fcf5ef2aSThomas Huth         ret = err;
742fcf5ef2aSThomas Huth     }
743fcf5ef2aSThomas Huth     err = kvm_mips_put_one_ulreg(cs, KVM_REG_MIPS_CP0_USERLOCAL,
744fcf5ef2aSThomas Huth                                  &env->active_tc.CP0_UserLocal);
745fcf5ef2aSThomas Huth     if (err < 0) {
746fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to put CP0_USERLOCAL (%d)\n", __func__, err);
747fcf5ef2aSThomas Huth         ret = err;
748fcf5ef2aSThomas Huth     }
749fcf5ef2aSThomas Huth     err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_PAGEMASK,
750fcf5ef2aSThomas Huth                                &env->CP0_PageMask);
751fcf5ef2aSThomas Huth     if (err < 0) {
752fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to put CP0_PAGEMASK (%d)\n", __func__, err);
753fcf5ef2aSThomas Huth         ret = err;
754fcf5ef2aSThomas Huth     }
755fcf5ef2aSThomas Huth     err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_WIRED, &env->CP0_Wired);
756fcf5ef2aSThomas Huth     if (err < 0) {
757fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to put CP0_WIRED (%d)\n", __func__, err);
758fcf5ef2aSThomas Huth         ret = err;
759fcf5ef2aSThomas Huth     }
760fcf5ef2aSThomas Huth     err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_HWRENA, &env->CP0_HWREna);
761fcf5ef2aSThomas Huth     if (err < 0) {
762fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to put CP0_HWRENA (%d)\n", __func__, err);
763fcf5ef2aSThomas Huth         ret = err;
764fcf5ef2aSThomas Huth     }
765fcf5ef2aSThomas Huth     err = kvm_mips_put_one_ulreg(cs, KVM_REG_MIPS_CP0_BADVADDR,
766fcf5ef2aSThomas Huth                                  &env->CP0_BadVAddr);
767fcf5ef2aSThomas Huth     if (err < 0) {
768fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to put CP0_BADVADDR (%d)\n", __func__, err);
769fcf5ef2aSThomas Huth         ret = err;
770fcf5ef2aSThomas Huth     }
771fcf5ef2aSThomas Huth 
772fcf5ef2aSThomas Huth     /* If VM clock stopped then state will be restored when it is restarted */
773fcf5ef2aSThomas Huth     if (runstate_is_running()) {
774fcf5ef2aSThomas Huth         err = kvm_mips_restore_count(cs);
775fcf5ef2aSThomas Huth         if (err < 0) {
776fcf5ef2aSThomas Huth             ret = err;
777fcf5ef2aSThomas Huth         }
778fcf5ef2aSThomas Huth     }
779fcf5ef2aSThomas Huth 
780fcf5ef2aSThomas Huth     err = kvm_mips_put_one_ulreg(cs, KVM_REG_MIPS_CP0_ENTRYHI,
781fcf5ef2aSThomas Huth                                  &env->CP0_EntryHi);
782fcf5ef2aSThomas Huth     if (err < 0) {
783fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to put CP0_ENTRYHI (%d)\n", __func__, err);
784fcf5ef2aSThomas Huth         ret = err;
785fcf5ef2aSThomas Huth     }
786fcf5ef2aSThomas Huth     err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_COMPARE,
787fcf5ef2aSThomas Huth                                &env->CP0_Compare);
788fcf5ef2aSThomas Huth     if (err < 0) {
789fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to put CP0_COMPARE (%d)\n", __func__, err);
790fcf5ef2aSThomas Huth         ret = err;
791fcf5ef2aSThomas Huth     }
792fcf5ef2aSThomas Huth     err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_STATUS, &env->CP0_Status);
793fcf5ef2aSThomas Huth     if (err < 0) {
794fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to put CP0_STATUS (%d)\n", __func__, err);
795fcf5ef2aSThomas Huth         ret = err;
796fcf5ef2aSThomas Huth     }
797fcf5ef2aSThomas Huth     err = kvm_mips_put_one_ulreg(cs, KVM_REG_MIPS_CP0_EPC, &env->CP0_EPC);
798fcf5ef2aSThomas Huth     if (err < 0) {
799fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to put CP0_EPC (%d)\n", __func__, err);
800fcf5ef2aSThomas Huth         ret = err;
801fcf5ef2aSThomas Huth     }
802fcf5ef2aSThomas Huth     err = kvm_mips_put_one_reg(cs, KVM_REG_MIPS_CP0_PRID, &env->CP0_PRid);
803fcf5ef2aSThomas Huth     if (err < 0) {
804fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to put CP0_PRID (%d)\n", __func__, err);
805fcf5ef2aSThomas Huth         ret = err;
806fcf5ef2aSThomas Huth     }
807fcf5ef2aSThomas Huth     err = kvm_mips_change_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG,
808fcf5ef2aSThomas Huth                                   &env->CP0_Config0,
809fcf5ef2aSThomas Huth                                   KVM_REG_MIPS_CP0_CONFIG_MASK);
810fcf5ef2aSThomas Huth     if (err < 0) {
811fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to change CP0_CONFIG (%d)\n", __func__, err);
812fcf5ef2aSThomas Huth         ret = err;
813fcf5ef2aSThomas Huth     }
814fcf5ef2aSThomas Huth     err = kvm_mips_change_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG1,
815fcf5ef2aSThomas Huth                                   &env->CP0_Config1,
816fcf5ef2aSThomas Huth                                   KVM_REG_MIPS_CP0_CONFIG1_MASK);
817fcf5ef2aSThomas Huth     if (err < 0) {
818fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to change CP0_CONFIG1 (%d)\n", __func__, err);
819fcf5ef2aSThomas Huth         ret = err;
820fcf5ef2aSThomas Huth     }
821fcf5ef2aSThomas Huth     err = kvm_mips_change_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG2,
822fcf5ef2aSThomas Huth                                   &env->CP0_Config2,
823fcf5ef2aSThomas Huth                                   KVM_REG_MIPS_CP0_CONFIG2_MASK);
824fcf5ef2aSThomas Huth     if (err < 0) {
825fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to change CP0_CONFIG2 (%d)\n", __func__, err);
826fcf5ef2aSThomas Huth         ret = err;
827fcf5ef2aSThomas Huth     }
828fcf5ef2aSThomas Huth     err = kvm_mips_change_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG3,
829fcf5ef2aSThomas Huth                                   &env->CP0_Config3,
830fcf5ef2aSThomas Huth                                   KVM_REG_MIPS_CP0_CONFIG3_MASK);
831fcf5ef2aSThomas Huth     if (err < 0) {
832fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to change CP0_CONFIG3 (%d)\n", __func__, err);
833fcf5ef2aSThomas Huth         ret = err;
834fcf5ef2aSThomas Huth     }
835fcf5ef2aSThomas Huth     err = kvm_mips_change_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG4,
836fcf5ef2aSThomas Huth                                   &env->CP0_Config4,
837fcf5ef2aSThomas Huth                                   KVM_REG_MIPS_CP0_CONFIG4_MASK);
838fcf5ef2aSThomas Huth     if (err < 0) {
839fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to change CP0_CONFIG4 (%d)\n", __func__, err);
840fcf5ef2aSThomas Huth         ret = err;
841fcf5ef2aSThomas Huth     }
842fcf5ef2aSThomas Huth     err = kvm_mips_change_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG5,
843fcf5ef2aSThomas Huth                                   &env->CP0_Config5,
844fcf5ef2aSThomas Huth                                   KVM_REG_MIPS_CP0_CONFIG5_MASK);
845fcf5ef2aSThomas Huth     if (err < 0) {
846fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to change CP0_CONFIG5 (%d)\n", __func__, err);
847fcf5ef2aSThomas Huth         ret = err;
848fcf5ef2aSThomas Huth     }
849fcf5ef2aSThomas Huth     err = kvm_mips_put_one_ulreg(cs, KVM_REG_MIPS_CP0_ERROREPC,
850fcf5ef2aSThomas Huth                                  &env->CP0_ErrorEPC);
851fcf5ef2aSThomas Huth     if (err < 0) {
852fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to put CP0_ERROREPC (%d)\n", __func__, err);
853fcf5ef2aSThomas Huth         ret = err;
854fcf5ef2aSThomas Huth     }
855fcf5ef2aSThomas Huth 
856fcf5ef2aSThomas Huth     return ret;
857fcf5ef2aSThomas Huth }
858fcf5ef2aSThomas Huth 
859fcf5ef2aSThomas Huth static int kvm_mips_get_cp0_registers(CPUState *cs)
860fcf5ef2aSThomas Huth {
861fcf5ef2aSThomas Huth     MIPSCPU *cpu = MIPS_CPU(cs);
862fcf5ef2aSThomas Huth     CPUMIPSState *env = &cpu->env;
863fcf5ef2aSThomas Huth     int err, ret = 0;
864fcf5ef2aSThomas Huth 
865fcf5ef2aSThomas Huth     err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_INDEX, &env->CP0_Index);
866fcf5ef2aSThomas Huth     if (err < 0) {
867fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_INDEX (%d)\n", __func__, err);
868fcf5ef2aSThomas Huth         ret = err;
869fcf5ef2aSThomas Huth     }
870fcf5ef2aSThomas Huth     err = kvm_mips_get_one_ulreg(cs, KVM_REG_MIPS_CP0_CONTEXT,
871fcf5ef2aSThomas Huth                                  &env->CP0_Context);
872fcf5ef2aSThomas Huth     if (err < 0) {
873fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_CONTEXT (%d)\n", __func__, err);
874fcf5ef2aSThomas Huth         ret = err;
875fcf5ef2aSThomas Huth     }
876fcf5ef2aSThomas Huth     err = kvm_mips_get_one_ulreg(cs, KVM_REG_MIPS_CP0_USERLOCAL,
877fcf5ef2aSThomas Huth                                  &env->active_tc.CP0_UserLocal);
878fcf5ef2aSThomas Huth     if (err < 0) {
879fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_USERLOCAL (%d)\n", __func__, err);
880fcf5ef2aSThomas Huth         ret = err;
881fcf5ef2aSThomas Huth     }
882fcf5ef2aSThomas Huth     err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_PAGEMASK,
883fcf5ef2aSThomas Huth                                &env->CP0_PageMask);
884fcf5ef2aSThomas Huth     if (err < 0) {
885fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_PAGEMASK (%d)\n", __func__, err);
886fcf5ef2aSThomas Huth         ret = err;
887fcf5ef2aSThomas Huth     }
888fcf5ef2aSThomas Huth     err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_WIRED, &env->CP0_Wired);
889fcf5ef2aSThomas Huth     if (err < 0) {
890fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_WIRED (%d)\n", __func__, err);
891fcf5ef2aSThomas Huth         ret = err;
892fcf5ef2aSThomas Huth     }
893fcf5ef2aSThomas Huth     err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_HWRENA, &env->CP0_HWREna);
894fcf5ef2aSThomas Huth     if (err < 0) {
895fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_HWRENA (%d)\n", __func__, err);
896fcf5ef2aSThomas Huth         ret = err;
897fcf5ef2aSThomas Huth     }
898fcf5ef2aSThomas Huth     err = kvm_mips_get_one_ulreg(cs, KVM_REG_MIPS_CP0_BADVADDR,
899fcf5ef2aSThomas Huth                                  &env->CP0_BadVAddr);
900fcf5ef2aSThomas Huth     if (err < 0) {
901fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_BADVADDR (%d)\n", __func__, err);
902fcf5ef2aSThomas Huth         ret = err;
903fcf5ef2aSThomas Huth     }
904fcf5ef2aSThomas Huth     err = kvm_mips_get_one_ulreg(cs, KVM_REG_MIPS_CP0_ENTRYHI,
905fcf5ef2aSThomas Huth                                  &env->CP0_EntryHi);
906fcf5ef2aSThomas Huth     if (err < 0) {
907fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_ENTRYHI (%d)\n", __func__, err);
908fcf5ef2aSThomas Huth         ret = err;
909fcf5ef2aSThomas Huth     }
910fcf5ef2aSThomas Huth     err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_COMPARE,
911fcf5ef2aSThomas Huth                                &env->CP0_Compare);
912fcf5ef2aSThomas Huth     if (err < 0) {
913fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_COMPARE (%d)\n", __func__, err);
914fcf5ef2aSThomas Huth         ret = err;
915fcf5ef2aSThomas Huth     }
916fcf5ef2aSThomas Huth     err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_STATUS, &env->CP0_Status);
917fcf5ef2aSThomas Huth     if (err < 0) {
918fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_STATUS (%d)\n", __func__, err);
919fcf5ef2aSThomas Huth         ret = err;
920fcf5ef2aSThomas Huth     }
921fcf5ef2aSThomas Huth 
922fcf5ef2aSThomas Huth     /* If VM clock stopped then state was already saved when it was stopped */
923fcf5ef2aSThomas Huth     if (runstate_is_running()) {
924fcf5ef2aSThomas Huth         err = kvm_mips_save_count(cs);
925fcf5ef2aSThomas Huth         if (err < 0) {
926fcf5ef2aSThomas Huth             ret = err;
927fcf5ef2aSThomas Huth         }
928fcf5ef2aSThomas Huth     }
929fcf5ef2aSThomas Huth 
930fcf5ef2aSThomas Huth     err = kvm_mips_get_one_ulreg(cs, KVM_REG_MIPS_CP0_EPC, &env->CP0_EPC);
931fcf5ef2aSThomas Huth     if (err < 0) {
932fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_EPC (%d)\n", __func__, err);
933fcf5ef2aSThomas Huth         ret = err;
934fcf5ef2aSThomas Huth     }
935fcf5ef2aSThomas Huth     err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_PRID, &env->CP0_PRid);
936fcf5ef2aSThomas Huth     if (err < 0) {
937fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_PRID (%d)\n", __func__, err);
938fcf5ef2aSThomas Huth         ret = err;
939fcf5ef2aSThomas Huth     }
940fcf5ef2aSThomas Huth     err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG, &env->CP0_Config0);
941fcf5ef2aSThomas Huth     if (err < 0) {
942fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_CONFIG (%d)\n", __func__, err);
943fcf5ef2aSThomas Huth         ret = err;
944fcf5ef2aSThomas Huth     }
945fcf5ef2aSThomas Huth     err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG1, &env->CP0_Config1);
946fcf5ef2aSThomas Huth     if (err < 0) {
947fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_CONFIG1 (%d)\n", __func__, err);
948fcf5ef2aSThomas Huth         ret = err;
949fcf5ef2aSThomas Huth     }
950fcf5ef2aSThomas Huth     err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG2, &env->CP0_Config2);
951fcf5ef2aSThomas Huth     if (err < 0) {
952fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_CONFIG2 (%d)\n", __func__, err);
953fcf5ef2aSThomas Huth         ret = err;
954fcf5ef2aSThomas Huth     }
955fcf5ef2aSThomas Huth     err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG3, &env->CP0_Config3);
956fcf5ef2aSThomas Huth     if (err < 0) {
957fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_CONFIG3 (%d)\n", __func__, err);
958fcf5ef2aSThomas Huth         ret = err;
959fcf5ef2aSThomas Huth     }
960fcf5ef2aSThomas Huth     err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG4, &env->CP0_Config4);
961fcf5ef2aSThomas Huth     if (err < 0) {
962fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_CONFIG4 (%d)\n", __func__, err);
963fcf5ef2aSThomas Huth         ret = err;
964fcf5ef2aSThomas Huth     }
965fcf5ef2aSThomas Huth     err = kvm_mips_get_one_reg(cs, KVM_REG_MIPS_CP0_CONFIG5, &env->CP0_Config5);
966fcf5ef2aSThomas Huth     if (err < 0) {
967fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_CONFIG5 (%d)\n", __func__, err);
968fcf5ef2aSThomas Huth         ret = err;
969fcf5ef2aSThomas Huth     }
970fcf5ef2aSThomas Huth     err = kvm_mips_get_one_ulreg(cs, KVM_REG_MIPS_CP0_ERROREPC,
971fcf5ef2aSThomas Huth                                  &env->CP0_ErrorEPC);
972fcf5ef2aSThomas Huth     if (err < 0) {
973fcf5ef2aSThomas Huth         DPRINTF("%s: Failed to get CP0_ERROREPC (%d)\n", __func__, err);
974fcf5ef2aSThomas Huth         ret = err;
975fcf5ef2aSThomas Huth     }
976fcf5ef2aSThomas Huth 
977fcf5ef2aSThomas Huth     return ret;
978fcf5ef2aSThomas Huth }
979fcf5ef2aSThomas Huth 
980fcf5ef2aSThomas Huth int kvm_arch_put_registers(CPUState *cs, int level)
981fcf5ef2aSThomas Huth {
982fcf5ef2aSThomas Huth     MIPSCPU *cpu = MIPS_CPU(cs);
983fcf5ef2aSThomas Huth     CPUMIPSState *env = &cpu->env;
984fcf5ef2aSThomas Huth     struct kvm_regs regs;
985fcf5ef2aSThomas Huth     int ret;
986fcf5ef2aSThomas Huth     int i;
987fcf5ef2aSThomas Huth 
988fcf5ef2aSThomas Huth     /* Set the registers based on QEMU's view of things */
989fcf5ef2aSThomas Huth     for (i = 0; i < 32; i++) {
990fcf5ef2aSThomas Huth         regs.gpr[i] = (int64_t)(target_long)env->active_tc.gpr[i];
991fcf5ef2aSThomas Huth     }
992fcf5ef2aSThomas Huth 
993fcf5ef2aSThomas Huth     regs.hi = (int64_t)(target_long)env->active_tc.HI[0];
994fcf5ef2aSThomas Huth     regs.lo = (int64_t)(target_long)env->active_tc.LO[0];
995fcf5ef2aSThomas Huth     regs.pc = (int64_t)(target_long)env->active_tc.PC;
996fcf5ef2aSThomas Huth 
997fcf5ef2aSThomas Huth     ret = kvm_vcpu_ioctl(cs, KVM_SET_REGS, &regs);
998fcf5ef2aSThomas Huth 
999fcf5ef2aSThomas Huth     if (ret < 0) {
1000fcf5ef2aSThomas Huth         return ret;
1001fcf5ef2aSThomas Huth     }
1002fcf5ef2aSThomas Huth 
1003fcf5ef2aSThomas Huth     ret = kvm_mips_put_cp0_registers(cs, level);
1004fcf5ef2aSThomas Huth     if (ret < 0) {
1005fcf5ef2aSThomas Huth         return ret;
1006fcf5ef2aSThomas Huth     }
1007fcf5ef2aSThomas Huth 
1008fcf5ef2aSThomas Huth     ret = kvm_mips_put_fpu_registers(cs, level);
1009fcf5ef2aSThomas Huth     if (ret < 0) {
1010fcf5ef2aSThomas Huth         return ret;
1011fcf5ef2aSThomas Huth     }
1012fcf5ef2aSThomas Huth 
1013fcf5ef2aSThomas Huth     return ret;
1014fcf5ef2aSThomas Huth }
1015fcf5ef2aSThomas Huth 
1016fcf5ef2aSThomas Huth int kvm_arch_get_registers(CPUState *cs)
1017fcf5ef2aSThomas Huth {
1018fcf5ef2aSThomas Huth     MIPSCPU *cpu = MIPS_CPU(cs);
1019fcf5ef2aSThomas Huth     CPUMIPSState *env = &cpu->env;
1020fcf5ef2aSThomas Huth     int ret = 0;
1021fcf5ef2aSThomas Huth     struct kvm_regs regs;
1022fcf5ef2aSThomas Huth     int i;
1023fcf5ef2aSThomas Huth 
1024fcf5ef2aSThomas Huth     /* Get the current register set as KVM seems it */
1025fcf5ef2aSThomas Huth     ret = kvm_vcpu_ioctl(cs, KVM_GET_REGS, &regs);
1026fcf5ef2aSThomas Huth 
1027fcf5ef2aSThomas Huth     if (ret < 0) {
1028fcf5ef2aSThomas Huth         return ret;
1029fcf5ef2aSThomas Huth     }
1030fcf5ef2aSThomas Huth 
1031fcf5ef2aSThomas Huth     for (i = 0; i < 32; i++) {
1032fcf5ef2aSThomas Huth         env->active_tc.gpr[i] = regs.gpr[i];
1033fcf5ef2aSThomas Huth     }
1034fcf5ef2aSThomas Huth 
1035fcf5ef2aSThomas Huth     env->active_tc.HI[0] = regs.hi;
1036fcf5ef2aSThomas Huth     env->active_tc.LO[0] = regs.lo;
1037fcf5ef2aSThomas Huth     env->active_tc.PC = regs.pc;
1038fcf5ef2aSThomas Huth 
1039fcf5ef2aSThomas Huth     kvm_mips_get_cp0_registers(cs);
1040fcf5ef2aSThomas Huth     kvm_mips_get_fpu_registers(cs);
1041fcf5ef2aSThomas Huth 
1042fcf5ef2aSThomas Huth     return ret;
1043fcf5ef2aSThomas Huth }
1044fcf5ef2aSThomas Huth 
1045fcf5ef2aSThomas Huth int kvm_arch_fixup_msi_route(struct kvm_irq_routing_entry *route,
1046fcf5ef2aSThomas Huth                              uint64_t address, uint32_t data, PCIDevice *dev)
1047fcf5ef2aSThomas Huth {
1048fcf5ef2aSThomas Huth     return 0;
1049fcf5ef2aSThomas Huth }
1050fcf5ef2aSThomas Huth 
1051fcf5ef2aSThomas Huth int kvm_arch_add_msi_route_post(struct kvm_irq_routing_entry *route,
1052fcf5ef2aSThomas Huth                                 int vector, PCIDevice *dev)
1053fcf5ef2aSThomas Huth {
1054fcf5ef2aSThomas Huth     return 0;
1055fcf5ef2aSThomas Huth }
1056fcf5ef2aSThomas Huth 
1057fcf5ef2aSThomas Huth int kvm_arch_release_virq_post(int virq)
1058fcf5ef2aSThomas Huth {
1059fcf5ef2aSThomas Huth     return 0;
1060fcf5ef2aSThomas Huth }
1061fcf5ef2aSThomas Huth 
1062fcf5ef2aSThomas Huth int kvm_arch_msi_data_to_gsi(uint32_t data)
1063fcf5ef2aSThomas Huth {
1064fcf5ef2aSThomas Huth     abort();
1065fcf5ef2aSThomas Huth }
1066