xref: /qemu/target/mips/cpu.c (revision b282b859cf3442d922644e2cd2bee68272baafd5)
1 /*
2  * QEMU MIPS CPU
3  *
4  * Copyright (c) 2012 SUSE LINUX Products GmbH
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see
18  * <http://www.gnu.org/licenses/lgpl-2.1.html>
19  */
20 
21 #include "qemu/osdep.h"
22 #include "qemu/cutils.h"
23 #include "qemu/qemu-print.h"
24 #include "qemu/error-report.h"
25 #include "qapi/error.h"
26 #include "cpu.h"
27 #include "internal.h"
28 #include "kvm_mips.h"
29 #include "qemu/module.h"
30 #include "system/kvm.h"
31 #include "system/qtest.h"
32 #include "exec/exec-all.h"
33 #include "hw/qdev-properties.h"
34 #include "hw/qdev-clock.h"
35 #include "fpu_helper.h"
36 #ifndef CONFIG_USER_ONLY
37 #include "semihosting/semihost.h"
38 #endif
39 
40 const char regnames[32][3] = {
41     "r0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
42     "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
43     "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
44     "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
45 };
46 
47 static void fpu_dump_fpr(fpr_t *fpr, FILE *f, bool is_fpu64)
48 {
49     if (is_fpu64) {
50         qemu_fprintf(f, "w:%08x d:%016" PRIx64 " fd:%13g fs:%13g psu: %13g\n",
51                      fpr->w[FP_ENDIAN_IDX], fpr->d,
52                      (double)fpr->fd,
53                      (double)fpr->fs[FP_ENDIAN_IDX],
54                      (double)fpr->fs[!FP_ENDIAN_IDX]);
55     } else {
56         fpr_t tmp;
57 
58         tmp.w[FP_ENDIAN_IDX] = fpr->w[FP_ENDIAN_IDX];
59         tmp.w[!FP_ENDIAN_IDX] = (fpr + 1)->w[FP_ENDIAN_IDX];
60         qemu_fprintf(f, "w:%08x d:%016" PRIx64 " fd:%13g fs:%13g psu:%13g\n",
61                      tmp.w[FP_ENDIAN_IDX], tmp.d,
62                      (double)tmp.fd,
63                      (double)tmp.fs[FP_ENDIAN_IDX],
64                      (double)tmp.fs[!FP_ENDIAN_IDX]);
65     }
66 }
67 
68 static void fpu_dump_state(CPUMIPSState *env, FILE *f, int flags)
69 {
70     int i;
71     bool is_fpu64 = !!(env->hflags & MIPS_HFLAG_F64);
72 
73     qemu_fprintf(f,
74                  "CP1 FCR0 0x%08x  FCR31 0x%08x  SR.FR %d  fp_status 0x%02x\n",
75                  env->active_fpu.fcr0, env->active_fpu.fcr31, is_fpu64,
76                  get_float_exception_flags(&env->active_fpu.fp_status));
77     for (i = 0; i < 32; (is_fpu64) ? i++ : (i += 2)) {
78         qemu_fprintf(f, "%3s: ", fregnames[i]);
79         fpu_dump_fpr(&env->active_fpu.fpr[i], f, is_fpu64);
80     }
81 }
82 
83 static void mips_cpu_dump_state(CPUState *cs, FILE *f, int flags)
84 {
85     CPUMIPSState *env = cpu_env(cs);
86     int i;
87 
88     qemu_fprintf(f, "pc=0x" TARGET_FMT_lx " HI=0x" TARGET_FMT_lx
89                  " LO=0x" TARGET_FMT_lx " ds %04x "
90                  TARGET_FMT_lx " " TARGET_FMT_ld "\n",
91                  env->active_tc.PC, env->active_tc.HI[0], env->active_tc.LO[0],
92                  env->hflags, env->btarget, env->bcond);
93     for (i = 0; i < 32; i++) {
94         if ((i & 3) == 0) {
95             qemu_fprintf(f, "GPR%02d:", i);
96         }
97         qemu_fprintf(f, " %s " TARGET_FMT_lx,
98                      regnames[i], env->active_tc.gpr[i]);
99         if ((i & 3) == 3) {
100             qemu_fprintf(f, "\n");
101         }
102     }
103 
104     qemu_fprintf(f, "CP0 Status  0x%08x Cause   0x%08x EPC    0x"
105                  TARGET_FMT_lx "\n",
106                  env->CP0_Status, env->CP0_Cause, env->CP0_EPC);
107     qemu_fprintf(f, "    Config0 0x%08x Config1 0x%08x LLAddr 0x%016"
108                  PRIx64 "\n",
109                  env->CP0_Config0, env->CP0_Config1, env->CP0_LLAddr);
110     qemu_fprintf(f, "    Config2 0x%08x Config3 0x%08x\n",
111                  env->CP0_Config2, env->CP0_Config3);
112     qemu_fprintf(f, "    Config4 0x%08x Config5 0x%08x\n",
113                  env->CP0_Config4, env->CP0_Config5);
114     if ((flags & CPU_DUMP_FPU) && (env->hflags & MIPS_HFLAG_FPU)) {
115         fpu_dump_state(env, f, flags);
116     }
117 }
118 
119 void cpu_set_exception_base(int vp_index, target_ulong address)
120 {
121     MIPSCPU *vp = MIPS_CPU(qemu_get_cpu(vp_index));
122     vp->env.exception_base = address;
123 }
124 
125 static void mips_cpu_set_pc(CPUState *cs, vaddr value)
126 {
127     mips_env_set_pc(cpu_env(cs), value);
128 }
129 
130 static vaddr mips_cpu_get_pc(CPUState *cs)
131 {
132     MIPSCPU *cpu = MIPS_CPU(cs);
133 
134     return cpu->env.active_tc.PC;
135 }
136 
137 #if !defined(CONFIG_USER_ONLY)
138 static bool mips_cpu_has_work(CPUState *cs)
139 {
140     CPUMIPSState *env = cpu_env(cs);
141     bool has_work = false;
142 
143     /*
144      * Prior to MIPS Release 6 it is implementation dependent if non-enabled
145      * interrupts wake-up the CPU, however most of the implementations only
146      * check for interrupts that can be taken. For pre-release 6 CPUs,
147      * check for CP0 Config7 'Wait IE ignore' bit.
148      */
149     if ((cs->interrupt_request & CPU_INTERRUPT_HARD) &&
150         cpu_mips_hw_interrupts_pending(env)) {
151         if (cpu_mips_hw_interrupts_enabled(env) ||
152             (env->CP0_Config7 & (1 << CP0C7_WII)) ||
153             (env->insn_flags & ISA_MIPS_R6)) {
154             has_work = true;
155         }
156     }
157 
158     /* MIPS-MT has the ability to halt the CPU.  */
159     if (ase_mt_available(env)) {
160         /*
161          * The QEMU model will issue an _WAKE request whenever the CPUs
162          * should be woken up.
163          */
164         if (cs->interrupt_request & CPU_INTERRUPT_WAKE) {
165             has_work = true;
166         }
167 
168         if (!mips_vpe_active(env)) {
169             has_work = false;
170         }
171     }
172     /* MIPS Release 6 has the ability to halt the CPU.  */
173     if (env->CP0_Config5 & (1 << CP0C5_VP)) {
174         if (cs->interrupt_request & CPU_INTERRUPT_WAKE) {
175             has_work = true;
176         }
177         if (!mips_vp_active(env)) {
178             has_work = false;
179         }
180     }
181     return has_work;
182 }
183 #endif /* !CONFIG_USER_ONLY */
184 
185 #include "cpu-defs.c.inc"
186 
187 static void mips_cpu_reset_hold(Object *obj, ResetType type)
188 {
189     CPUState *cs = CPU(obj);
190     MIPSCPU *cpu = MIPS_CPU(cs);
191     MIPSCPUClass *mcc = MIPS_CPU_GET_CLASS(obj);
192     CPUMIPSState *env = &cpu->env;
193 
194     if (mcc->parent_phases.hold) {
195         mcc->parent_phases.hold(obj, type);
196     }
197 
198     memset(env, 0, offsetof(CPUMIPSState, end_reset_fields));
199 
200     /* Reset registers to their default values */
201     env->CP0_PRid = env->cpu_model->CP0_PRid;
202     env->CP0_Config0 = deposit32(env->cpu_model->CP0_Config0,
203                                  CP0C0_BE, 1, cpu->is_big_endian);
204     env->CP0_Config1 = env->cpu_model->CP0_Config1;
205     env->CP0_Config2 = env->cpu_model->CP0_Config2;
206     env->CP0_Config3 = env->cpu_model->CP0_Config3;
207     env->CP0_Config4 = env->cpu_model->CP0_Config4;
208     env->CP0_Config4_rw_bitmask = env->cpu_model->CP0_Config4_rw_bitmask;
209     env->CP0_Config5 = env->cpu_model->CP0_Config5;
210     env->CP0_Config5_rw_bitmask = env->cpu_model->CP0_Config5_rw_bitmask;
211     env->CP0_Config6 = env->cpu_model->CP0_Config6;
212     env->CP0_Config6_rw_bitmask = env->cpu_model->CP0_Config6_rw_bitmask;
213     env->CP0_Config7 = env->cpu_model->CP0_Config7;
214     env->CP0_Config7_rw_bitmask = env->cpu_model->CP0_Config7_rw_bitmask;
215     env->CP0_LLAddr_rw_bitmask = env->cpu_model->CP0_LLAddr_rw_bitmask
216                                  << env->cpu_model->CP0_LLAddr_shift;
217     env->CP0_LLAddr_shift = env->cpu_model->CP0_LLAddr_shift;
218     env->SYNCI_Step = env->cpu_model->SYNCI_Step;
219     env->CCRes = env->cpu_model->CCRes;
220     env->CP0_Status_rw_bitmask = env->cpu_model->CP0_Status_rw_bitmask;
221     env->CP0_TCStatus_rw_bitmask = env->cpu_model->CP0_TCStatus_rw_bitmask;
222     env->CP0_SRSCtl = env->cpu_model->CP0_SRSCtl;
223     env->current_tc = 0;
224     env->SEGBITS = env->cpu_model->SEGBITS;
225     env->SEGMask = (target_ulong)((1ULL << env->cpu_model->SEGBITS) - 1);
226 #if defined(TARGET_MIPS64)
227     if (env->cpu_model->insn_flags & ISA_MIPS3) {
228         env->SEGMask |= 3ULL << 62;
229     }
230 #endif
231     env->PABITS = env->cpu_model->PABITS;
232     env->CP0_SRSConf0_rw_bitmask = env->cpu_model->CP0_SRSConf0_rw_bitmask;
233     env->CP0_SRSConf0 = env->cpu_model->CP0_SRSConf0;
234     env->CP0_SRSConf1_rw_bitmask = env->cpu_model->CP0_SRSConf1_rw_bitmask;
235     env->CP0_SRSConf1 = env->cpu_model->CP0_SRSConf1;
236     env->CP0_SRSConf2_rw_bitmask = env->cpu_model->CP0_SRSConf2_rw_bitmask;
237     env->CP0_SRSConf2 = env->cpu_model->CP0_SRSConf2;
238     env->CP0_SRSConf3_rw_bitmask = env->cpu_model->CP0_SRSConf3_rw_bitmask;
239     env->CP0_SRSConf3 = env->cpu_model->CP0_SRSConf3;
240     env->CP0_SRSConf4_rw_bitmask = env->cpu_model->CP0_SRSConf4_rw_bitmask;
241     env->CP0_SRSConf4 = env->cpu_model->CP0_SRSConf4;
242     env->CP0_PageGrain_rw_bitmask = env->cpu_model->CP0_PageGrain_rw_bitmask;
243     env->CP0_PageGrain = env->cpu_model->CP0_PageGrain;
244     env->CP0_EBaseWG_rw_bitmask = env->cpu_model->CP0_EBaseWG_rw_bitmask;
245     env->lcsr_cpucfg1 = env->cpu_model->lcsr_cpucfg1;
246     env->lcsr_cpucfg2 = env->cpu_model->lcsr_cpucfg2;
247     env->active_fpu.fcr0 = env->cpu_model->CP1_fcr0;
248     env->active_fpu.fcr31_rw_bitmask = env->cpu_model->CP1_fcr31_rw_bitmask;
249     env->active_fpu.fcr31 = env->cpu_model->CP1_fcr31;
250     env->msair = env->cpu_model->MSAIR;
251     env->insn_flags = env->cpu_model->insn_flags;
252 
253 #if defined(CONFIG_USER_ONLY)
254     env->CP0_Status = (MIPS_HFLAG_UM << CP0St_KSU);
255 # ifdef TARGET_MIPS64
256     /* Enable 64-bit register mode.  */
257     env->CP0_Status |= (1 << CP0St_PX);
258 # endif
259 # ifdef TARGET_ABI_MIPSN64
260     /* Enable 64-bit address mode.  */
261     env->CP0_Status |= (1 << CP0St_UX);
262 # endif
263     /*
264      * Enable access to the CPUNum, SYNCI_Step, CC, and CCRes RDHWR
265      * hardware registers.
266      */
267     env->CP0_HWREna |= 0x0000000F;
268     if (env->CP0_Config1 & (1 << CP0C1_FP)) {
269         env->CP0_Status |= (1 << CP0St_CU1);
270     }
271     if (env->CP0_Config3 & (1 << CP0C3_DSPP)) {
272         env->CP0_Status |= (1 << CP0St_MX);
273     }
274 # if defined(TARGET_MIPS64)
275     /* For MIPS64, init FR bit to 1 if FPU unit is there and bit is writable. */
276     if ((env->CP0_Config1 & (1 << CP0C1_FP)) &&
277         (env->CP0_Status_rw_bitmask & (1 << CP0St_FR))) {
278         env->CP0_Status |= (1 << CP0St_FR);
279     }
280 # endif
281 #else /* !CONFIG_USER_ONLY */
282     if (env->hflags & MIPS_HFLAG_BMASK) {
283         /*
284          * If the exception was raised from a delay slot,
285          * come back to the jump.
286          */
287         env->CP0_ErrorEPC = (env->active_tc.PC
288                              - (env->hflags & MIPS_HFLAG_B16 ? 2 : 4));
289     } else {
290         env->CP0_ErrorEPC = env->active_tc.PC;
291     }
292     env->active_tc.PC = env->exception_base;
293     env->CP0_Random = env->tlb->nb_tlb - 1;
294     env->tlb->tlb_in_use = env->tlb->nb_tlb;
295     env->CP0_Wired = 0;
296     env->CP0_GlobalNumber = (cs->cpu_index & 0xFF) << CP0GN_VPId;
297     env->CP0_EBase = KSEG0_BASE | (cs->cpu_index & 0x3FF);
298     if (env->CP0_Config3 & (1 << CP0C3_CMGCR)) {
299         env->CP0_CMGCRBase = 0x1fbf8000 >> 4;
300     }
301     env->CP0_EntryHi_ASID_mask = (env->CP0_Config5 & (1 << CP0C5_MI)) ?
302             0x0 : (env->CP0_Config4 & (1 << CP0C4_AE)) ? 0x3ff : 0xff;
303     env->CP0_Status = (1 << CP0St_BEV) | (1 << CP0St_ERL);
304     if (env->insn_flags & INSN_LOONGSON2F) {
305         /* Loongson-2F has those bits hardcoded to 1 */
306         env->CP0_Status |= (1 << CP0St_KX) | (1 << CP0St_SX) |
307                             (1 << CP0St_UX);
308     }
309 
310     /*
311      * Vectored interrupts not implemented, timer on int 7,
312      * no performance counters.
313      */
314     env->CP0_IntCtl = 0xe0000000;
315     {
316         int i;
317 
318         for (i = 0; i < 7; i++) {
319             env->CP0_WatchLo[i] = 0;
320             env->CP0_WatchHi[i] = 1 << CP0WH_M;
321         }
322         env->CP0_WatchLo[7] = 0;
323         env->CP0_WatchHi[7] = 0;
324     }
325     /* Count register increments in debug mode, EJTAG version 1 */
326     env->CP0_Debug = (1 << CP0DB_CNT) | (0x1 << CP0DB_VER);
327 
328     cpu_mips_store_count(env, 1);
329 
330     if (ase_mt_available(env)) {
331         int i;
332 
333         /* Only TC0 on VPE 0 starts as active.  */
334         for (i = 0; i < ARRAY_SIZE(env->tcs); i++) {
335             env->tcs[i].CP0_TCBind = cs->cpu_index << CP0TCBd_CurVPE;
336             env->tcs[i].CP0_TCHalt = 1;
337         }
338         env->active_tc.CP0_TCHalt = 1;
339         cs->halted = 1;
340 
341         if (cs->cpu_index == 0) {
342             /* VPE0 starts up enabled.  */
343             env->mvp->CP0_MVPControl |= (1 << CP0MVPCo_EVP);
344             env->CP0_VPEConf0 |= (1 << CP0VPEC0_MVP) | (1 << CP0VPEC0_VPA);
345 
346             /* TC0 starts up unhalted.  */
347             cs->halted = 0;
348             env->active_tc.CP0_TCHalt = 0;
349             env->tcs[0].CP0_TCHalt = 0;
350             /* With thread 0 active.  */
351             env->active_tc.CP0_TCStatus = (1 << CP0TCSt_A);
352             env->tcs[0].CP0_TCStatus = (1 << CP0TCSt_A);
353         }
354     }
355 
356     /*
357      * Configure default legacy segmentation control. We use this regardless of
358      * whether segmentation control is presented to the guest.
359      */
360     /* KSeg3 (seg0 0xE0000000..0xFFFFFFFF) */
361     env->CP0_SegCtl0 =   (CP0SC_AM_MK << CP0SC_AM);
362     /* KSeg2 (seg1 0xC0000000..0xDFFFFFFF) */
363     env->CP0_SegCtl0 |= ((CP0SC_AM_MSK << CP0SC_AM)) << 16;
364     /* KSeg1 (seg2 0xA0000000..0x9FFFFFFF) */
365     env->CP0_SegCtl1 =   (0 << CP0SC_PA) | (CP0SC_AM_UK << CP0SC_AM) |
366                          (2 << CP0SC_C);
367     /* KSeg0 (seg3 0x80000000..0x9FFFFFFF) */
368     env->CP0_SegCtl1 |= ((0 << CP0SC_PA) | (CP0SC_AM_UK << CP0SC_AM) |
369                          (3 << CP0SC_C)) << 16;
370     /* USeg (seg4 0x40000000..0x7FFFFFFF) */
371     env->CP0_SegCtl2 =   (2 << CP0SC_PA) | (CP0SC_AM_MUSK << CP0SC_AM) |
372                          (1 << CP0SC_EU) | (2 << CP0SC_C);
373     /* USeg (seg5 0x00000000..0x3FFFFFFF) */
374     env->CP0_SegCtl2 |= ((0 << CP0SC_PA) | (CP0SC_AM_MUSK << CP0SC_AM) |
375                          (1 << CP0SC_EU) | (2 << CP0SC_C)) << 16;
376     /* XKPhys (note, SegCtl2.XR = 0, so XAM won't be used) */
377     env->CP0_SegCtl1 |= (CP0SC_AM_UK << CP0SC1_XAM);
378 #endif /* !CONFIG_USER_ONLY */
379     if ((env->insn_flags & ISA_MIPS_R6) &&
380         (env->active_fpu.fcr0 & (1 << FCR0_F64))) {
381         /* Status.FR = 0 mode in 64-bit FPU not allowed in R6 */
382         env->CP0_Status |= (1 << CP0St_FR);
383     }
384 
385     if (env->insn_flags & ISA_MIPS_R6) {
386         /* PTW  =  1 */
387         env->CP0_PWSize = 0x40;
388         /* GDI  = 12 */
389         /* UDI  = 12 */
390         /* MDI  = 12 */
391         /* PRI  = 12 */
392         /* PTEI =  2 */
393         env->CP0_PWField = 0x0C30C302;
394     } else {
395         /* GDI  =  0 */
396         /* UDI  =  0 */
397         /* MDI  =  0 */
398         /* PRI  =  0 */
399         /* PTEI =  2 */
400         env->CP0_PWField = 0x02;
401     }
402 
403     if (env->CP0_Config3 & (1 << CP0C3_ISA) & (1 << (CP0C3_ISA + 1))) {
404         /*  microMIPS on reset when Config3.ISA is 3 */
405         env->hflags |= MIPS_HFLAG_M16;
406     }
407 
408     msa_reset(env);
409     fp_reset(env);
410 
411     compute_hflags(env);
412     restore_pamask(env);
413     cs->exception_index = EXCP_NONE;
414 
415 #ifndef CONFIG_USER_ONLY
416     if (semihosting_get_argc()) {
417         /* UHI interface can be used to obtain argc and argv */
418         env->active_tc.gpr[4] = -1;
419     }
420     if (kvm_enabled()) {
421         kvm_mips_reset_vcpu(cpu);
422     }
423 #endif
424 }
425 
426 static void mips_cpu_disas_set_info(CPUState *s, disassemble_info *info)
427 {
428     if (!(cpu_env(s)->insn_flags & ISA_NANOMIPS32)) {
429         info->endian = TARGET_BIG_ENDIAN ? BFD_ENDIAN_BIG
430                                          : BFD_ENDIAN_LITTLE;
431         info->print_insn = TARGET_BIG_ENDIAN ? print_insn_big_mips
432                                              : print_insn_little_mips;
433     } else {
434         info->print_insn = print_insn_nanomips;
435         info->endian = BFD_ENDIAN_LITTLE;
436     }
437 }
438 
439 /*
440  * Since commit 6af0bf9c7c3 this model assumes a CPU clocked at 200MHz.
441  */
442 #define CPU_FREQ_HZ_DEFAULT     200000000
443 
444 static void mips_cp0_period_set(MIPSCPU *cpu)
445 {
446     CPUMIPSState *env = &cpu->env;
447 
448     clock_set_mul_div(cpu->count_div, env->cpu_model->CCRes, 1);
449     clock_set_source(cpu->count_div, cpu->clock);
450     clock_set_source(env->count_clock, cpu->count_div);
451 }
452 
453 static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
454 {
455     CPUState *cs = CPU(dev);
456     MIPSCPU *cpu = MIPS_CPU(dev);
457     CPUMIPSState *env = &cpu->env;
458     MIPSCPUClass *mcc = MIPS_CPU_GET_CLASS(dev);
459     Error *local_err = NULL;
460 
461     if (!clock_get(cpu->clock)) {
462 #ifndef CONFIG_USER_ONLY
463         if (!qtest_enabled()) {
464             g_autofree char *cpu_freq_str = freq_to_str(CPU_FREQ_HZ_DEFAULT);
465 
466             warn_report("CPU input clock is not connected to any output clock, "
467                         "using default frequency of %s.", cpu_freq_str);
468         }
469 #endif
470         /* Initialize the frequency in case the clock remains unconnected. */
471         clock_set_hz(cpu->clock, CPU_FREQ_HZ_DEFAULT);
472     }
473     mips_cp0_period_set(cpu);
474 
475     cpu_exec_realizefn(cs, &local_err);
476     if (local_err != NULL) {
477         error_propagate(errp, local_err);
478         return;
479     }
480 
481     env->exception_base = (int32_t)0xBFC00000;
482 
483 #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY)
484     mmu_init(env, env->cpu_model);
485 #endif
486     fpu_init(env, env->cpu_model);
487     mvp_init(env);
488 
489     cpu_reset(cs);
490     qemu_init_vcpu(cs);
491 
492     mcc->parent_realize(dev, errp);
493 }
494 
495 static void mips_cpu_initfn(Object *obj)
496 {
497     MIPSCPU *cpu = MIPS_CPU(obj);
498     CPUMIPSState *env = &cpu->env;
499     MIPSCPUClass *mcc = MIPS_CPU_GET_CLASS(obj);
500 
501     cpu->clock = qdev_init_clock_in(DEVICE(obj), "clk-in", NULL, cpu, 0);
502     cpu->count_div = clock_new(OBJECT(obj), "clk-div-count");
503     env->count_clock = clock_new(OBJECT(obj), "clk-count");
504     env->cpu_model = mcc->cpu_def;
505 #ifndef CONFIG_USER_ONLY
506     if (mcc->cpu_def->lcsr_cpucfg2 & (1 << CPUCFG2_LCSRP)) {
507         memory_region_init_io(&env->iocsr.mr, OBJECT(cpu), NULL,
508                                 env, "iocsr", UINT64_MAX);
509         address_space_init(&env->iocsr.as,
510                             &env->iocsr.mr, "IOCSR");
511     }
512 #endif
513 }
514 
515 static char *mips_cpu_type_name(const char *cpu_model)
516 {
517     return g_strdup_printf(MIPS_CPU_TYPE_NAME("%s"), cpu_model);
518 }
519 
520 static ObjectClass *mips_cpu_class_by_name(const char *cpu_model)
521 {
522     ObjectClass *oc;
523     char *typename;
524 
525     typename = mips_cpu_type_name(cpu_model);
526     oc = object_class_by_name(typename);
527     g_free(typename);
528     return oc;
529 }
530 
531 #ifndef CONFIG_USER_ONLY
532 #include "hw/core/sysemu-cpu-ops.h"
533 
534 static const struct SysemuCPUOps mips_sysemu_ops = {
535     .has_work = mips_cpu_has_work,
536     .get_phys_page_debug = mips_cpu_get_phys_page_debug,
537     .legacy_vmsd = &vmstate_mips_cpu,
538 };
539 #endif
540 
541 static const Property mips_cpu_properties[] = {
542     DEFINE_PROP_BOOL("big-endian", MIPSCPU, is_big_endian, TARGET_BIG_ENDIAN),
543 };
544 
545 #ifdef CONFIG_TCG
546 #include "accel/tcg/cpu-ops.h"
547 
548 static int mips_cpu_mmu_index(CPUState *cs, bool ifunc)
549 {
550     return mips_env_mmu_index(cpu_env(cs));
551 }
552 
553 static const TCGCPUOps mips_tcg_ops = {
554     .mttcg_supported = TARGET_LONG_BITS == 32,
555     .guest_default_memory_order = 0,
556 
557     .initialize = mips_tcg_init,
558     .translate_code = mips_translate_code,
559     .synchronize_from_tb = mips_cpu_synchronize_from_tb,
560     .restore_state_to_opc = mips_restore_state_to_opc,
561     .mmu_index = mips_cpu_mmu_index,
562 
563 #if !defined(CONFIG_USER_ONLY)
564     .tlb_fill = mips_cpu_tlb_fill,
565     .cpu_exec_interrupt = mips_cpu_exec_interrupt,
566     .cpu_exec_halt = mips_cpu_has_work,
567     .do_interrupt = mips_cpu_do_interrupt,
568     .do_transaction_failed = mips_cpu_do_transaction_failed,
569     .do_unaligned_access = mips_cpu_do_unaligned_access,
570     .io_recompile_replay_branch = mips_io_recompile_replay_branch,
571 #endif /* !CONFIG_USER_ONLY */
572 };
573 #endif /* CONFIG_TCG */
574 
575 static void mips_cpu_class_init(ObjectClass *c, const void *data)
576 {
577     MIPSCPUClass *mcc = MIPS_CPU_CLASS(c);
578     CPUClass *cc = CPU_CLASS(c);
579     DeviceClass *dc = DEVICE_CLASS(c);
580     ResettableClass *rc = RESETTABLE_CLASS(c);
581 
582     device_class_set_props(dc, mips_cpu_properties);
583     device_class_set_parent_realize(dc, mips_cpu_realizefn,
584                                     &mcc->parent_realize);
585     resettable_class_set_parent_phases(rc, NULL, mips_cpu_reset_hold, NULL,
586                                        &mcc->parent_phases);
587 
588     cc->class_by_name = mips_cpu_class_by_name;
589     cc->dump_state = mips_cpu_dump_state;
590     cc->set_pc = mips_cpu_set_pc;
591     cc->get_pc = mips_cpu_get_pc;
592     cc->gdb_read_register = mips_cpu_gdb_read_register;
593     cc->gdb_write_register = mips_cpu_gdb_write_register;
594 #ifndef CONFIG_USER_ONLY
595     cc->sysemu_ops = &mips_sysemu_ops;
596 #endif
597     cc->disas_set_info = mips_cpu_disas_set_info;
598     cc->gdb_num_core_regs = 73;
599     cc->gdb_stop_before_watchpoint = true;
600 #ifdef CONFIG_TCG
601     cc->tcg_ops = &mips_tcg_ops;
602 #endif /* CONFIG_TCG */
603 }
604 
605 static const TypeInfo mips_cpu_type_info = {
606     .name = TYPE_MIPS_CPU,
607     .parent = TYPE_CPU,
608     .instance_size = sizeof(MIPSCPU),
609     .instance_align = __alignof(MIPSCPU),
610     .instance_init = mips_cpu_initfn,
611     .abstract = true,
612     .class_size = sizeof(MIPSCPUClass),
613     .class_init = mips_cpu_class_init,
614 };
615 
616 static void mips_cpu_cpudef_class_init(ObjectClass *oc, const void *data)
617 {
618     MIPSCPUClass *mcc = MIPS_CPU_CLASS(oc);
619     mcc->cpu_def = data;
620 }
621 
622 static void mips_register_cpudef_type(const struct mips_def_t *def)
623 {
624     char *typename = mips_cpu_type_name(def->name);
625     TypeInfo ti = {
626         .name = typename,
627         .parent = TYPE_MIPS_CPU,
628         .class_init = mips_cpu_cpudef_class_init,
629         .class_data = def,
630     };
631 
632     type_register_static(&ti);
633     g_free(typename);
634 }
635 
636 static void mips_cpu_register_types(void)
637 {
638     int i;
639 
640     type_register_static(&mips_cpu_type_info);
641     for (i = 0; i < mips_defs_number; i++) {
642         mips_register_cpudef_type(&mips_defs[i]);
643     }
644 }
645 
646 type_init(mips_cpu_register_types)
647 
648 /* Could be used by generic CPU object */
649 MIPSCPU *mips_cpu_create_with_clock(const char *cpu_type, Clock *cpu_refclk,
650                                     bool is_big_endian)
651 {
652     DeviceState *cpu;
653 
654     cpu = qdev_new(cpu_type);
655     qdev_connect_clock_in(cpu, "clk-in", cpu_refclk);
656     object_property_set_bool(OBJECT(cpu), "big-endian", is_big_endian,
657                              &error_abort);
658     qdev_realize(cpu, NULL, &error_abort);
659 
660     return MIPS_CPU(cpu);
661 }
662 
663 bool cpu_supports_isa(const CPUMIPSState *env, uint64_t isa_mask)
664 {
665     return (env->cpu_model->insn_flags & isa_mask) != 0;
666 }
667 
668 bool cpu_type_supports_isa(const char *cpu_type, uint64_t isa)
669 {
670     const MIPSCPUClass *mcc = MIPS_CPU_CLASS(object_class_by_name(cpu_type));
671     return (mcc->cpu_def->insn_flags & isa) != 0;
672 }
673 
674 bool cpu_type_supports_cps_smp(const char *cpu_type)
675 {
676     const MIPSCPUClass *mcc = MIPS_CPU_CLASS(object_class_by_name(cpu_type));
677     return (mcc->cpu_def->CP0_Config3 & (1 << CP0C3_CMGCR)) != 0;
678 }
679