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