4 * Copyright (c) 2012 SUSE LINUX Products GmbH
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.
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.
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>
21 #include "qemu/osdep.h"
22 #include "qemu/cutils.h"
23 #include "qemu/qemu-print.h"
24 #include "qapi/error.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 "qapi/qapi-commands-machine-target.h"
36 #include "fpu_helper.h"
38 const char regnames
[32][4] = {
39 "r0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
40 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
41 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
42 "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra",
45 #if !defined(CONFIG_USER_ONLY)
47 /* Called for updates to CP0_Status. */
48 void sync_c0_status(CPUMIPSState
*env
, CPUMIPSState
*cpu
, int tc
)
50 int32_t tcstatus
, *tcst
;
51 uint32_t v
= cpu
->CP0_Status
;
52 uint32_t cu
, mx
, asid
, ksu
;
53 uint32_t mask
= ((1 << CP0TCSt_TCU3
)
59 | (0xff << CP0TCSt_TASID
));
61 cu
= (v
>> CP0St_CU0
) & 0xf;
62 mx
= (v
>> CP0St_MX
) & 0x1;
63 ksu
= (v
>> CP0St_KSU
) & 0x3;
64 asid
= env
->CP0_EntryHi
& env
->CP0_EntryHi_ASID_mask
;
66 tcstatus
= cu
<< CP0TCSt_TCU0
;
67 tcstatus
|= mx
<< CP0TCSt_TMX
;
68 tcstatus
|= ksu
<< CP0TCSt_TKSU
;
71 if (tc
== cpu
->current_tc
) {
72 tcst
= &cpu
->active_tc
.CP0_TCStatus
;
74 tcst
= &cpu
->tcs
[tc
].CP0_TCStatus
;
82 void cpu_mips_store_status(CPUMIPSState
*env
, target_ulong val
)
84 uint32_t mask
= env
->CP0_Status_rw_bitmask
;
85 target_ulong old
= env
->CP0_Status
;
87 if (env
->insn_flags
& ISA_MIPS_R6
) {
88 bool has_supervisor
= extract32(mask
, CP0St_KSU
, 2) == 0x3;
89 #if defined(TARGET_MIPS64)
90 uint32_t ksux
= (1 << CP0St_KX
) & val
;
91 ksux
|= (ksux
>> 1) & val
; /* KX = 0 forces SX to be 0 */
92 ksux
|= (ksux
>> 1) & val
; /* SX = 0 forces UX to be 0 */
93 val
= (val
& ~(7 << CP0St_UX
)) | ksux
;
95 if (has_supervisor
&& extract32(val
, CP0St_KSU
, 2) == 0x3) {
96 mask
&= ~(3 << CP0St_KSU
);
98 mask
&= ~(((1 << CP0St_SR
) | (1 << CP0St_NMI
)) & val
);
101 env
->CP0_Status
= (old
& ~mask
) | (val
& mask
);
102 #if defined(TARGET_MIPS64)
103 if ((env
->CP0_Status
^ old
) & (old
& (7 << CP0St_UX
))) {
104 /* Access to at least one of the 64-bit segments has been disabled */
105 tlb_flush(env_cpu(env
));
108 if (ase_mt_available(env
)) {
109 sync_c0_status(env
, env
, env
->current_tc
);
115 void cpu_mips_store_cause(CPUMIPSState
*env
, target_ulong val
)
117 uint32_t mask
= 0x00C00300;
118 uint32_t old
= env
->CP0_Cause
;
121 if (env
->insn_flags
& ISA_MIPS_R2
) {
122 mask
|= 1 << CP0Ca_DC
;
124 if (env
->insn_flags
& ISA_MIPS_R6
) {
125 mask
&= ~((1 << CP0Ca_WP
) & val
);
128 env
->CP0_Cause
= (env
->CP0_Cause
& ~mask
) | (val
& mask
);
130 if ((old
^ env
->CP0_Cause
) & (1 << CP0Ca_DC
)) {
131 if (env
->CP0_Cause
& (1 << CP0Ca_DC
)) {
132 cpu_mips_stop_count(env
);
134 cpu_mips_start_count(env
);
138 /* Set/reset software interrupts */
139 for (i
= 0 ; i
< 2 ; i
++) {
140 if ((old
^ env
->CP0_Cause
) & (1 << (CP0Ca_IP
+ i
))) {
141 cpu_mips_soft_irq(env
, i
, env
->CP0_Cause
& (1 << (CP0Ca_IP
+ i
)));
146 #endif /* !CONFIG_USER_ONLY */
148 static void fpu_dump_state(CPUMIPSState
*env
, FILE *f
, int flags
)
151 int is_fpu64
= !!(env
->hflags
& MIPS_HFLAG_F64
);
153 #define printfpr(fp) \
156 qemu_fprintf(f, "w:%08x d:%016" PRIx64 \
157 " fd:%13g fs:%13g psu: %13g\n", \
158 (fp)->w[FP_ENDIAN_IDX], (fp)->d, \
160 (double)(fp)->fs[FP_ENDIAN_IDX], \
161 (double)(fp)->fs[!FP_ENDIAN_IDX]); \
164 tmp.w[FP_ENDIAN_IDX] = (fp)->w[FP_ENDIAN_IDX]; \
165 tmp.w[!FP_ENDIAN_IDX] = ((fp) + 1)->w[FP_ENDIAN_IDX]; \
166 qemu_fprintf(f, "w:%08x d:%016" PRIx64 \
167 " fd:%13g fs:%13g psu:%13g\n", \
168 tmp.w[FP_ENDIAN_IDX], tmp.d, \
170 (double)tmp.fs[FP_ENDIAN_IDX], \
171 (double)tmp.fs[!FP_ENDIAN_IDX]); \
177 "CP1 FCR0 0x%08x FCR31 0x%08x SR.FR %d fp_status 0x%02x\n",
178 env
->active_fpu
.fcr0
, env
->active_fpu
.fcr31
, is_fpu64
,
179 get_float_exception_flags(&env
->active_fpu
.fp_status
));
180 for (i
= 0; i
< 32; (is_fpu64
) ? i
++ : (i
+= 2)) {
181 qemu_fprintf(f
, "%3s: ", fregnames
[i
]);
182 printfpr(&env
->active_fpu
.fpr
[i
]);
188 static void mips_cpu_dump_state(CPUState
*cs
, FILE *f
, int flags
)
190 MIPSCPU
*cpu
= MIPS_CPU(cs
);
191 CPUMIPSState
*env
= &cpu
->env
;
194 qemu_fprintf(f
, "pc=0x" TARGET_FMT_lx
" HI=0x" TARGET_FMT_lx
195 " LO=0x" TARGET_FMT_lx
" ds %04x "
196 TARGET_FMT_lx
" " TARGET_FMT_ld
"\n",
197 env
->active_tc
.PC
, env
->active_tc
.HI
[0], env
->active_tc
.LO
[0],
198 env
->hflags
, env
->btarget
, env
->bcond
);
199 for (i
= 0; i
< 32; i
++) {
201 qemu_fprintf(f
, "GPR%02d:", i
);
203 qemu_fprintf(f
, " %s " TARGET_FMT_lx
,
204 regnames
[i
], env
->active_tc
.gpr
[i
]);
206 qemu_fprintf(f
, "\n");
210 qemu_fprintf(f
, "CP0 Status 0x%08x Cause 0x%08x EPC 0x"
212 env
->CP0_Status
, env
->CP0_Cause
, env
->CP0_EPC
);
213 qemu_fprintf(f
, " Config0 0x%08x Config1 0x%08x LLAddr 0x%016"
215 env
->CP0_Config0
, env
->CP0_Config1
, env
->CP0_LLAddr
);
216 qemu_fprintf(f
, " Config2 0x%08x Config3 0x%08x\n",
217 env
->CP0_Config2
, env
->CP0_Config3
);
218 qemu_fprintf(f
, " Config4 0x%08x Config5 0x%08x\n",
219 env
->CP0_Config4
, env
->CP0_Config5
);
220 if ((flags
& CPU_DUMP_FPU
) && (env
->hflags
& MIPS_HFLAG_FPU
)) {
221 fpu_dump_state(env
, f
, flags
);
225 static const char * const excp_names
[EXCP_LAST
+ 1] = {
226 [EXCP_RESET
] = "reset",
227 [EXCP_SRESET
] = "soft reset",
228 [EXCP_DSS
] = "debug single step",
229 [EXCP_DINT
] = "debug interrupt",
230 [EXCP_NMI
] = "non-maskable interrupt",
231 [EXCP_MCHECK
] = "machine check",
232 [EXCP_EXT_INTERRUPT
] = "interrupt",
233 [EXCP_DFWATCH
] = "deferred watchpoint",
234 [EXCP_DIB
] = "debug instruction breakpoint",
235 [EXCP_IWATCH
] = "instruction fetch watchpoint",
236 [EXCP_AdEL
] = "address error load",
237 [EXCP_AdES
] = "address error store",
238 [EXCP_TLBF
] = "TLB refill",
239 [EXCP_IBE
] = "instruction bus error",
240 [EXCP_DBp
] = "debug breakpoint",
241 [EXCP_SYSCALL
] = "syscall",
242 [EXCP_BREAK
] = "break",
243 [EXCP_CpU
] = "coprocessor unusable",
244 [EXCP_RI
] = "reserved instruction",
245 [EXCP_OVERFLOW
] = "arithmetic overflow",
246 [EXCP_TRAP
] = "trap",
247 [EXCP_FPE
] = "floating point",
248 [EXCP_DDBS
] = "debug data break store",
249 [EXCP_DWATCH
] = "data watchpoint",
250 [EXCP_LTLBL
] = "TLB modify",
251 [EXCP_TLBL
] = "TLB load",
252 [EXCP_TLBS
] = "TLB store",
253 [EXCP_DBE
] = "data bus error",
254 [EXCP_DDBL
] = "debug data break load",
255 [EXCP_THREAD
] = "thread",
256 [EXCP_MDMX
] = "MDMX",
257 [EXCP_C2E
] = "precise coprocessor 2",
258 [EXCP_CACHE
] = "cache error",
259 [EXCP_TLBXI
] = "TLB execute-inhibit",
260 [EXCP_TLBRI
] = "TLB read-inhibit",
261 [EXCP_MSADIS
] = "MSA disabled",
262 [EXCP_MSAFPE
] = "MSA floating point",
265 const char *mips_exception_name(int32_t exception
)
267 if (exception
< 0 || exception
> EXCP_LAST
) {
270 return excp_names
[exception
];
273 void cpu_set_exception_base(int vp_index
, target_ulong address
)
275 MIPSCPU
*vp
= MIPS_CPU(qemu_get_cpu(vp_index
));
276 vp
->env
.exception_base
= address
;
279 target_ulong
exception_resume_pc(CPUMIPSState
*env
)
282 target_ulong isa_mode
;
284 isa_mode
= !!(env
->hflags
& MIPS_HFLAG_M16
);
285 bad_pc
= env
->active_tc
.PC
| isa_mode
;
286 if (env
->hflags
& MIPS_HFLAG_BMASK
) {
288 * If the exception was raised from a delay slot, come back to
291 bad_pc
-= (env
->hflags
& MIPS_HFLAG_B16
? 2 : 4);
297 bool mips_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
)
299 if (interrupt_request
& CPU_INTERRUPT_HARD
) {
300 MIPSCPU
*cpu
= MIPS_CPU(cs
);
301 CPUMIPSState
*env
= &cpu
->env
;
303 if (cpu_mips_hw_interrupts_enabled(env
) &&
304 cpu_mips_hw_interrupts_pending(env
)) {
306 cs
->exception_index
= EXCP_EXT_INTERRUPT
;
308 mips_cpu_do_interrupt(cs
);
315 void QEMU_NORETURN
do_raise_exception_err(CPUMIPSState
*env
,
320 CPUState
*cs
= env_cpu(env
);
322 qemu_log_mask(CPU_LOG_INT
, "%s: %d (%s) %d\n",
323 __func__
, exception
, mips_exception_name(exception
),
325 cs
->exception_index
= exception
;
326 env
->error_code
= error_code
;
328 cpu_loop_exit_restore(cs
, pc
);
331 static void mips_cpu_set_pc(CPUState
*cs
, vaddr value
)
333 MIPSCPU
*cpu
= MIPS_CPU(cs
);
334 CPUMIPSState
*env
= &cpu
->env
;
336 env
->active_tc
.PC
= value
& ~(target_ulong
)1;
338 env
->hflags
|= MIPS_HFLAG_M16
;
340 env
->hflags
&= ~(MIPS_HFLAG_M16
);
345 static void mips_cpu_synchronize_from_tb(CPUState
*cs
,
346 const TranslationBlock
*tb
)
348 MIPSCPU
*cpu
= MIPS_CPU(cs
);
349 CPUMIPSState
*env
= &cpu
->env
;
351 env
->active_tc
.PC
= tb
->pc
;
352 env
->hflags
&= ~MIPS_HFLAG_BMASK
;
353 env
->hflags
|= tb
->flags
& MIPS_HFLAG_BMASK
;
356 # ifndef CONFIG_USER_ONLY
357 static bool mips_io_recompile_replay_branch(CPUState
*cs
,
358 const TranslationBlock
*tb
)
360 MIPSCPU
*cpu
= MIPS_CPU(cs
);
361 CPUMIPSState
*env
= &cpu
->env
;
363 if ((env
->hflags
& MIPS_HFLAG_BMASK
) != 0
364 && env
->active_tc
.PC
!= tb
->pc
) {
365 env
->active_tc
.PC
-= (env
->hflags
& MIPS_HFLAG_B16
? 2 : 4);
366 env
->hflags
&= ~MIPS_HFLAG_BMASK
;
371 # endif /* !CONFIG_USER_ONLY */
372 #endif /* CONFIG_TCG */
374 static bool mips_cpu_has_work(CPUState
*cs
)
376 MIPSCPU
*cpu
= MIPS_CPU(cs
);
377 CPUMIPSState
*env
= &cpu
->env
;
378 bool has_work
= false;
381 * Prior to MIPS Release 6 it is implementation dependent if non-enabled
382 * interrupts wake-up the CPU, however most of the implementations only
383 * check for interrupts that can be taken.
385 if ((cs
->interrupt_request
& CPU_INTERRUPT_HARD
) &&
386 cpu_mips_hw_interrupts_pending(env
)) {
387 if (cpu_mips_hw_interrupts_enabled(env
) ||
388 (env
->insn_flags
& ISA_MIPS_R6
)) {
393 /* MIPS-MT has the ability to halt the CPU. */
394 if (ase_mt_available(env
)) {
396 * The QEMU model will issue an _WAKE request whenever the CPUs
397 * should be woken up.
399 if (cs
->interrupt_request
& CPU_INTERRUPT_WAKE
) {
403 if (!mips_vpe_active(env
)) {
407 /* MIPS Release 6 has the ability to halt the CPU. */
408 if (env
->CP0_Config5
& (1 << CP0C5_VP
)) {
409 if (cs
->interrupt_request
& CPU_INTERRUPT_WAKE
) {
412 if (!mips_vp_active(env
)) {
419 #include "cpu-defs.c.inc"
421 static void mips_cpu_reset(DeviceState
*dev
)
423 CPUState
*cs
= CPU(dev
);
424 MIPSCPU
*cpu
= MIPS_CPU(cs
);
425 MIPSCPUClass
*mcc
= MIPS_CPU_GET_CLASS(cpu
);
426 CPUMIPSState
*env
= &cpu
->env
;
428 mcc
->parent_reset(dev
);
430 memset(env
, 0, offsetof(CPUMIPSState
, end_reset_fields
));
432 /* Reset registers to their default values */
433 env
->CP0_PRid
= env
->cpu_model
->CP0_PRid
;
434 env
->CP0_Config0
= env
->cpu_model
->CP0_Config0
;
435 #ifdef TARGET_WORDS_BIGENDIAN
436 env
->CP0_Config0
|= (1 << CP0C0_BE
);
438 env
->CP0_Config1
= env
->cpu_model
->CP0_Config1
;
439 env
->CP0_Config2
= env
->cpu_model
->CP0_Config2
;
440 env
->CP0_Config3
= env
->cpu_model
->CP0_Config3
;
441 env
->CP0_Config4
= env
->cpu_model
->CP0_Config4
;
442 env
->CP0_Config4_rw_bitmask
= env
->cpu_model
->CP0_Config4_rw_bitmask
;
443 env
->CP0_Config5
= env
->cpu_model
->CP0_Config5
;
444 env
->CP0_Config5_rw_bitmask
= env
->cpu_model
->CP0_Config5_rw_bitmask
;
445 env
->CP0_Config6
= env
->cpu_model
->CP0_Config6
;
446 env
->CP0_Config6_rw_bitmask
= env
->cpu_model
->CP0_Config6_rw_bitmask
;
447 env
->CP0_Config7
= env
->cpu_model
->CP0_Config7
;
448 env
->CP0_Config7_rw_bitmask
= env
->cpu_model
->CP0_Config7_rw_bitmask
;
449 env
->CP0_LLAddr_rw_bitmask
= env
->cpu_model
->CP0_LLAddr_rw_bitmask
450 << env
->cpu_model
->CP0_LLAddr_shift
;
451 env
->CP0_LLAddr_shift
= env
->cpu_model
->CP0_LLAddr_shift
;
452 env
->SYNCI_Step
= env
->cpu_model
->SYNCI_Step
;
453 env
->CCRes
= env
->cpu_model
->CCRes
;
454 env
->CP0_Status_rw_bitmask
= env
->cpu_model
->CP0_Status_rw_bitmask
;
455 env
->CP0_TCStatus_rw_bitmask
= env
->cpu_model
->CP0_TCStatus_rw_bitmask
;
456 env
->CP0_SRSCtl
= env
->cpu_model
->CP0_SRSCtl
;
458 env
->SEGBITS
= env
->cpu_model
->SEGBITS
;
459 env
->SEGMask
= (target_ulong
)((1ULL << env
->cpu_model
->SEGBITS
) - 1);
460 #if defined(TARGET_MIPS64)
461 if (env
->cpu_model
->insn_flags
& ISA_MIPS3
) {
462 env
->SEGMask
|= 3ULL << 62;
465 env
->PABITS
= env
->cpu_model
->PABITS
;
466 env
->CP0_SRSConf0_rw_bitmask
= env
->cpu_model
->CP0_SRSConf0_rw_bitmask
;
467 env
->CP0_SRSConf0
= env
->cpu_model
->CP0_SRSConf0
;
468 env
->CP0_SRSConf1_rw_bitmask
= env
->cpu_model
->CP0_SRSConf1_rw_bitmask
;
469 env
->CP0_SRSConf1
= env
->cpu_model
->CP0_SRSConf1
;
470 env
->CP0_SRSConf2_rw_bitmask
= env
->cpu_model
->CP0_SRSConf2_rw_bitmask
;
471 env
->CP0_SRSConf2
= env
->cpu_model
->CP0_SRSConf2
;
472 env
->CP0_SRSConf3_rw_bitmask
= env
->cpu_model
->CP0_SRSConf3_rw_bitmask
;
473 env
->CP0_SRSConf3
= env
->cpu_model
->CP0_SRSConf3
;
474 env
->CP0_SRSConf4_rw_bitmask
= env
->cpu_model
->CP0_SRSConf4_rw_bitmask
;
475 env
->CP0_SRSConf4
= env
->cpu_model
->CP0_SRSConf4
;
476 env
->CP0_PageGrain_rw_bitmask
= env
->cpu_model
->CP0_PageGrain_rw_bitmask
;
477 env
->CP0_PageGrain
= env
->cpu_model
->CP0_PageGrain
;
478 env
->CP0_EBaseWG_rw_bitmask
= env
->cpu_model
->CP0_EBaseWG_rw_bitmask
;
479 env
->active_fpu
.fcr0
= env
->cpu_model
->CP1_fcr0
;
480 env
->active_fpu
.fcr31_rw_bitmask
= env
->cpu_model
->CP1_fcr31_rw_bitmask
;
481 env
->active_fpu
.fcr31
= env
->cpu_model
->CP1_fcr31
;
482 env
->msair
= env
->cpu_model
->MSAIR
;
483 env
->insn_flags
= env
->cpu_model
->insn_flags
;
485 #if defined(CONFIG_USER_ONLY)
486 env
->CP0_Status
= (MIPS_HFLAG_UM
<< CP0St_KSU
);
487 # ifdef TARGET_MIPS64
488 /* Enable 64-bit register mode. */
489 env
->CP0_Status
|= (1 << CP0St_PX
);
491 # ifdef TARGET_ABI_MIPSN64
492 /* Enable 64-bit address mode. */
493 env
->CP0_Status
|= (1 << CP0St_UX
);
496 * Enable access to the CPUNum, SYNCI_Step, CC, and CCRes RDHWR
497 * hardware registers.
499 env
->CP0_HWREna
|= 0x0000000F;
500 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
501 env
->CP0_Status
|= (1 << CP0St_CU1
);
503 if (env
->CP0_Config3
& (1 << CP0C3_DSPP
)) {
504 env
->CP0_Status
|= (1 << CP0St_MX
);
506 # if defined(TARGET_MIPS64)
507 /* For MIPS64, init FR bit to 1 if FPU unit is there and bit is writable. */
508 if ((env
->CP0_Config1
& (1 << CP0C1_FP
)) &&
509 (env
->CP0_Status_rw_bitmask
& (1 << CP0St_FR
))) {
510 env
->CP0_Status
|= (1 << CP0St_FR
);
513 #else /* !CONFIG_USER_ONLY */
514 if (env
->hflags
& MIPS_HFLAG_BMASK
) {
516 * If the exception was raised from a delay slot,
517 * come back to the jump.
519 env
->CP0_ErrorEPC
= (env
->active_tc
.PC
520 - (env
->hflags
& MIPS_HFLAG_B16
? 2 : 4));
522 env
->CP0_ErrorEPC
= env
->active_tc
.PC
;
524 env
->active_tc
.PC
= env
->exception_base
;
525 env
->CP0_Random
= env
->tlb
->nb_tlb
- 1;
526 env
->tlb
->tlb_in_use
= env
->tlb
->nb_tlb
;
528 env
->CP0_GlobalNumber
= (cs
->cpu_index
& 0xFF) << CP0GN_VPId
;
529 env
->CP0_EBase
= (cs
->cpu_index
& 0x3FF);
530 if (mips_um_ksegs_enabled()) {
531 env
->CP0_EBase
|= 0x40000000;
533 env
->CP0_EBase
|= (int32_t)0x80000000;
535 if (env
->CP0_Config3
& (1 << CP0C3_CMGCR
)) {
536 env
->CP0_CMGCRBase
= 0x1fbf8000 >> 4;
538 env
->CP0_EntryHi_ASID_mask
= (env
->CP0_Config5
& (1 << CP0C5_MI
)) ?
539 0x0 : (env
->CP0_Config4
& (1 << CP0C4_AE
)) ? 0x3ff : 0xff;
540 env
->CP0_Status
= (1 << CP0St_BEV
) | (1 << CP0St_ERL
);
542 * Vectored interrupts not implemented, timer on int 7,
543 * no performance counters.
545 env
->CP0_IntCtl
= 0xe0000000;
549 for (i
= 0; i
< 7; i
++) {
550 env
->CP0_WatchLo
[i
] = 0;
551 env
->CP0_WatchHi
[i
] = 0x80000000;
553 env
->CP0_WatchLo
[7] = 0;
554 env
->CP0_WatchHi
[7] = 0;
556 /* Count register increments in debug mode, EJTAG version 1 */
557 env
->CP0_Debug
= (1 << CP0DB_CNT
) | (0x1 << CP0DB_VER
);
559 cpu_mips_store_count(env
, 1);
561 if (ase_mt_available(env
)) {
564 /* Only TC0 on VPE 0 starts as active. */
565 for (i
= 0; i
< ARRAY_SIZE(env
->tcs
); i
++) {
566 env
->tcs
[i
].CP0_TCBind
= cs
->cpu_index
<< CP0TCBd_CurVPE
;
567 env
->tcs
[i
].CP0_TCHalt
= 1;
569 env
->active_tc
.CP0_TCHalt
= 1;
572 if (cs
->cpu_index
== 0) {
573 /* VPE0 starts up enabled. */
574 env
->mvp
->CP0_MVPControl
|= (1 << CP0MVPCo_EVP
);
575 env
->CP0_VPEConf0
|= (1 << CP0VPEC0_MVP
) | (1 << CP0VPEC0_VPA
);
577 /* TC0 starts up unhalted. */
579 env
->active_tc
.CP0_TCHalt
= 0;
580 env
->tcs
[0].CP0_TCHalt
= 0;
581 /* With thread 0 active. */
582 env
->active_tc
.CP0_TCStatus
= (1 << CP0TCSt_A
);
583 env
->tcs
[0].CP0_TCStatus
= (1 << CP0TCSt_A
);
588 * Configure default legacy segmentation control. We use this regardless of
589 * whether segmentation control is presented to the guest.
591 /* KSeg3 (seg0 0xE0000000..0xFFFFFFFF) */
592 env
->CP0_SegCtl0
= (CP0SC_AM_MK
<< CP0SC_AM
);
593 /* KSeg2 (seg1 0xC0000000..0xDFFFFFFF) */
594 env
->CP0_SegCtl0
|= ((CP0SC_AM_MSK
<< CP0SC_AM
)) << 16;
595 /* KSeg1 (seg2 0xA0000000..0x9FFFFFFF) */
596 env
->CP0_SegCtl1
= (0 << CP0SC_PA
) | (CP0SC_AM_UK
<< CP0SC_AM
) |
598 /* KSeg0 (seg3 0x80000000..0x9FFFFFFF) */
599 env
->CP0_SegCtl1
|= ((0 << CP0SC_PA
) | (CP0SC_AM_UK
<< CP0SC_AM
) |
600 (3 << CP0SC_C
)) << 16;
601 /* USeg (seg4 0x40000000..0x7FFFFFFF) */
602 env
->CP0_SegCtl2
= (2 << CP0SC_PA
) | (CP0SC_AM_MUSK
<< CP0SC_AM
) |
603 (1 << CP0SC_EU
) | (2 << CP0SC_C
);
604 /* USeg (seg5 0x00000000..0x3FFFFFFF) */
605 env
->CP0_SegCtl2
|= ((0 << CP0SC_PA
) | (CP0SC_AM_MUSK
<< CP0SC_AM
) |
606 (1 << CP0SC_EU
) | (2 << CP0SC_C
)) << 16;
607 /* XKPhys (note, SegCtl2.XR = 0, so XAM won't be used) */
608 env
->CP0_SegCtl1
|= (CP0SC_AM_UK
<< CP0SC1_XAM
);
609 #endif /* !CONFIG_USER_ONLY */
610 if ((env
->insn_flags
& ISA_MIPS_R6
) &&
611 (env
->active_fpu
.fcr0
& (1 << FCR0_F64
))) {
612 /* Status.FR = 0 mode in 64-bit FPU not allowed in R6 */
613 env
->CP0_Status
|= (1 << CP0St_FR
);
616 if (env
->insn_flags
& ISA_MIPS_R6
) {
618 env
->CP0_PWSize
= 0x40;
624 env
->CP0_PWField
= 0x0C30C302;
631 env
->CP0_PWField
= 0x02;
634 if (env
->CP0_Config3
& (1 << CP0C3_ISA
) & (1 << (CP0C3_ISA
+ 1))) {
635 /* microMIPS on reset when Config3.ISA is 3 */
636 env
->hflags
|= MIPS_HFLAG_M16
;
642 restore_fp_status(env
);
644 cs
->exception_index
= EXCP_NONE
;
646 if (semihosting_get_argc()) {
647 /* UHI interface can be used to obtain argc and argv */
648 env
->active_tc
.gpr
[4] = -1;
651 #ifndef CONFIG_USER_ONLY
653 kvm_mips_reset_vcpu(cpu
);
658 static void mips_cpu_disas_set_info(CPUState
*s
, disassemble_info
*info
)
660 MIPSCPU
*cpu
= MIPS_CPU(s
);
661 CPUMIPSState
*env
= &cpu
->env
;
663 if (!(env
->insn_flags
& ISA_NANOMIPS32
)) {
664 #ifdef TARGET_WORDS_BIGENDIAN
665 info
->print_insn
= print_insn_big_mips
;
667 info
->print_insn
= print_insn_little_mips
;
670 #if defined(CONFIG_NANOMIPS_DIS)
671 info
->print_insn
= print_insn_nanomips
;
677 * Since commit 6af0bf9c7c3 this model assumes a CPU clocked at 200MHz.
679 #define CPU_FREQ_HZ_DEFAULT 200000000
680 #define CP0_COUNT_RATE_DEFAULT 2
682 static void mips_cp0_period_set(MIPSCPU
*cpu
)
684 CPUMIPSState
*env
= &cpu
->env
;
686 env
->cp0_count_ns
= clock_ticks_to_ns(MIPS_CPU(cpu
)->clock
,
687 cpu
->cp0_count_rate
);
688 assert(env
->cp0_count_ns
);
691 static void mips_cpu_realizefn(DeviceState
*dev
, Error
**errp
)
693 CPUState
*cs
= CPU(dev
);
694 MIPSCPU
*cpu
= MIPS_CPU(dev
);
695 CPUMIPSState
*env
= &cpu
->env
;
696 MIPSCPUClass
*mcc
= MIPS_CPU_GET_CLASS(dev
);
697 Error
*local_err
= NULL
;
699 if (!clock_get(cpu
->clock
)) {
700 #ifndef CONFIG_USER_ONLY
701 if (!qtest_enabled()) {
702 g_autofree
char *cpu_freq_str
= freq_to_str(CPU_FREQ_HZ_DEFAULT
);
704 warn_report("CPU input clock is not connected to any output clock, "
705 "using default frequency of %s.", cpu_freq_str
);
708 /* Initialize the frequency in case the clock remains unconnected. */
709 clock_set_hz(cpu
->clock
, CPU_FREQ_HZ_DEFAULT
);
711 mips_cp0_period_set(cpu
);
713 cpu_exec_realizefn(cs
, &local_err
);
714 if (local_err
!= NULL
) {
715 error_propagate(errp
, local_err
);
719 env
->exception_base
= (int32_t)0xBFC00000;
721 #ifndef CONFIG_USER_ONLY
722 mmu_init(env
, env
->cpu_model
);
724 fpu_init(env
, env
->cpu_model
);
730 mcc
->parent_realize(dev
, errp
);
733 static void mips_cpu_initfn(Object
*obj
)
735 MIPSCPU
*cpu
= MIPS_CPU(obj
);
736 CPUMIPSState
*env
= &cpu
->env
;
737 MIPSCPUClass
*mcc
= MIPS_CPU_GET_CLASS(obj
);
739 cpu_set_cpustate_pointers(cpu
);
740 cpu
->clock
= qdev_init_clock_in(DEVICE(obj
), "clk-in", NULL
, cpu
, 0);
741 env
->cpu_model
= mcc
->cpu_def
;
744 static char *mips_cpu_type_name(const char *cpu_model
)
746 return g_strdup_printf(MIPS_CPU_TYPE_NAME("%s"), cpu_model
);
749 static ObjectClass
*mips_cpu_class_by_name(const char *cpu_model
)
754 typename
= mips_cpu_type_name(cpu_model
);
755 oc
= object_class_by_name(typename
);
760 static Property mips_cpu_properties
[] = {
761 /* CP0 timer running at half the clock of the CPU */
762 DEFINE_PROP_UINT32("cp0-count-rate", MIPSCPU
, cp0_count_rate
,
763 CP0_COUNT_RATE_DEFAULT
),
764 DEFINE_PROP_END_OF_LIST()
768 #include "hw/core/tcg-cpu-ops.h"
770 * NB: cannot be const, as some elements are changed for specific
771 * mips hardware (see hw/mips/jazz.c).
773 static struct TCGCPUOps mips_tcg_ops
= {
774 .initialize
= mips_tcg_init
,
775 .synchronize_from_tb
= mips_cpu_synchronize_from_tb
,
776 .cpu_exec_interrupt
= mips_cpu_exec_interrupt
,
777 .tlb_fill
= mips_cpu_tlb_fill
,
779 #if !defined(CONFIG_USER_ONLY)
780 .do_interrupt
= mips_cpu_do_interrupt
,
781 .do_transaction_failed
= mips_cpu_do_transaction_failed
,
782 .do_unaligned_access
= mips_cpu_do_unaligned_access
,
783 .io_recompile_replay_branch
= mips_io_recompile_replay_branch
,
784 #endif /* !CONFIG_USER_ONLY */
786 #endif /* CONFIG_TCG */
788 static void mips_cpu_class_init(ObjectClass
*c
, void *data
)
790 MIPSCPUClass
*mcc
= MIPS_CPU_CLASS(c
);
791 CPUClass
*cc
= CPU_CLASS(c
);
792 DeviceClass
*dc
= DEVICE_CLASS(c
);
794 device_class_set_parent_realize(dc
, mips_cpu_realizefn
,
795 &mcc
->parent_realize
);
796 device_class_set_parent_reset(dc
, mips_cpu_reset
, &mcc
->parent_reset
);
797 device_class_set_props(dc
, mips_cpu_properties
);
799 cc
->class_by_name
= mips_cpu_class_by_name
;
800 cc
->has_work
= mips_cpu_has_work
;
801 cc
->dump_state
= mips_cpu_dump_state
;
802 cc
->set_pc
= mips_cpu_set_pc
;
803 cc
->gdb_read_register
= mips_cpu_gdb_read_register
;
804 cc
->gdb_write_register
= mips_cpu_gdb_write_register
;
805 #ifndef CONFIG_USER_ONLY
806 cc
->get_phys_page_debug
= mips_cpu_get_phys_page_debug
;
807 cc
->vmsd
= &vmstate_mips_cpu
;
809 cc
->disas_set_info
= mips_cpu_disas_set_info
;
810 cc
->gdb_num_core_regs
= 73;
811 cc
->gdb_stop_before_watchpoint
= true;
813 cc
->tcg_ops
= &mips_tcg_ops
;
814 #endif /* CONFIG_TCG */
817 static const TypeInfo mips_cpu_type_info
= {
818 .name
= TYPE_MIPS_CPU
,
820 .instance_size
= sizeof(MIPSCPU
),
821 .instance_init
= mips_cpu_initfn
,
823 .class_size
= sizeof(MIPSCPUClass
),
824 .class_init
= mips_cpu_class_init
,
827 static void mips_cpu_cpudef_class_init(ObjectClass
*oc
, void *data
)
829 MIPSCPUClass
*mcc
= MIPS_CPU_CLASS(oc
);
833 static void mips_register_cpudef_type(const struct mips_def_t
*def
)
835 char *typename
= mips_cpu_type_name(def
->name
);
838 .parent
= TYPE_MIPS_CPU
,
839 .class_init
= mips_cpu_cpudef_class_init
,
840 .class_data
= (void *)def
,
847 static void mips_cpu_register_types(void)
851 type_register_static(&mips_cpu_type_info
);
852 for (i
= 0; i
< mips_defs_number
; i
++) {
853 mips_register_cpudef_type(&mips_defs
[i
]);
857 type_init(mips_cpu_register_types
)
859 static void mips_cpu_add_definition(gpointer data
, gpointer user_data
)
861 ObjectClass
*oc
= data
;
862 CpuDefinitionInfoList
**cpu_list
= user_data
;
863 CpuDefinitionInfo
*info
;
864 const char *typename
;
866 typename
= object_class_get_name(oc
);
867 info
= g_malloc0(sizeof(*info
));
868 info
->name
= g_strndup(typename
,
869 strlen(typename
) - strlen("-" TYPE_MIPS_CPU
));
870 info
->q_typename
= g_strdup(typename
);
872 QAPI_LIST_PREPEND(*cpu_list
, info
);
875 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
877 CpuDefinitionInfoList
*cpu_list
= NULL
;
880 list
= object_class_get_list(TYPE_MIPS_CPU
, false);
881 g_slist_foreach(list
, mips_cpu_add_definition
, &cpu_list
);
887 /* Could be used by generic CPU object */
888 MIPSCPU
*mips_cpu_create_with_clock(const char *cpu_type
, Clock
*cpu_refclk
)
892 cpu
= DEVICE(object_new(cpu_type
));
893 qdev_connect_clock_in(cpu
, "clk-in", cpu_refclk
);
894 qdev_realize(cpu
, NULL
, &error_abort
);
896 return MIPS_CPU(cpu
);
899 bool cpu_supports_isa(const CPUMIPSState
*env
, uint64_t isa_mask
)
901 return (env
->cpu_model
->insn_flags
& isa_mask
) != 0;
904 bool cpu_type_supports_isa(const char *cpu_type
, uint64_t isa
)
906 const MIPSCPUClass
*mcc
= MIPS_CPU_CLASS(object_class_by_name(cpu_type
));
907 return (mcc
->cpu_def
->insn_flags
& isa
) != 0;
910 bool cpu_type_supports_cps_smp(const char *cpu_type
)
912 const MIPSCPUClass
*mcc
= MIPS_CPU_CLASS(object_class_by_name(cpu_type
));
913 return (mcc
->cpu_def
->CP0_Config3
& (1 << CP0C3_CMGCR
)) != 0;