2 * MIPS emulation helpers for qemu.
4 * Copyright (c) 2004-2005 Jocelyn Mayer
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 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 <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
23 #include "exec/exec-all.h"
24 #include "exec/cpu_ldst.h"
26 #include "hw/mips/cpudevs.h"
27 #include "qapi/qapi-commands-target.h"
39 #if !defined(CONFIG_USER_ONLY)
41 /* no MMU emulation */
42 int no_mmu_map_address (CPUMIPSState
*env
, hwaddr
*physical
, int *prot
,
43 target_ulong address
, int rw
, int access_type
)
53 /* fixed mapping MMU emulation */
54 int fixed_mmu_map_address (CPUMIPSState
*env
, hwaddr
*physical
, int *prot
,
55 target_ulong address
, int rw
, int access_type
)
57 if (address
<= (int32_t)0x7FFFFFFFUL
) {
58 if (!(env
->CP0_Status
& (1 << CP0St_ERL
)))
59 *physical
= address
+ 0x40000000UL
;
62 } else if (address
<= (int32_t)0xBFFFFFFFUL
)
63 *physical
= address
& 0x1FFFFFFF;
74 /* MIPS32/MIPS64 R4000-style MMU emulation */
75 int r4k_map_address (CPUMIPSState
*env
, hwaddr
*physical
, int *prot
,
76 target_ulong address
, int rw
, int access_type
)
78 uint16_t ASID
= env
->CP0_EntryHi
& env
->CP0_EntryHi_ASID_mask
;
81 for (i
= 0; i
< env
->tlb
->tlb_in_use
; i
++) {
82 r4k_tlb_t
*tlb
= &env
->tlb
->mmu
.r4k
.tlb
[i
];
83 /* 1k pages are not supported. */
84 target_ulong mask
= tlb
->PageMask
| ~(TARGET_PAGE_MASK
<< 1);
85 target_ulong tag
= address
& ~mask
;
86 target_ulong VPN
= tlb
->VPN
& ~mask
;
87 #if defined(TARGET_MIPS64)
91 /* Check ASID, virtual page number & size */
92 if ((tlb
->G
== 1 || tlb
->ASID
== ASID
) && VPN
== tag
&& !tlb
->EHINV
) {
94 int n
= !!(address
& mask
& ~(mask
>> 1));
95 /* Check access rights */
96 if (!(n
? tlb
->V1
: tlb
->V0
)) {
97 return TLBRET_INVALID
;
99 if (rw
== MMU_INST_FETCH
&& (n
? tlb
->XI1
: tlb
->XI0
)) {
102 if (rw
== MMU_DATA_LOAD
&& (n
? tlb
->RI1
: tlb
->RI0
)) {
105 if (rw
!= MMU_DATA_STORE
|| (n
? tlb
->D1
: tlb
->D0
)) {
106 *physical
= tlb
->PFN
[n
] | (address
& (mask
>> 1));
108 if (n
? tlb
->D1
: tlb
->D0
)
115 return TLBRET_NOMATCH
;
118 static int is_seg_am_mapped(unsigned int am
, bool eu
, int mmu_idx
)
121 * Interpret access control mode and mmu_idx.
124 * UK 0 0 1 1 0 0 - - 0
125 * MK 1 0 1 1 0 1 - - !eu
126 * MSK 2 0 0 1 0 1 1 - !eu
127 * MUSK 3 0 0 0 0 1 1 1 !eu
128 * MUSUK 4 0 0 0 0 0 1 1 0
129 * USK 5 0 0 1 0 0 0 - 0
130 * - 6 - - - - - - - -
131 * UUSK 7 0 0 0 0 0 0 0 0
137 /* If EU is set, always unmapped */
143 /* Never AdE, TLB mapped if AM={1,2,3} */
144 adetlb_mask
= 0x70000000;
148 /* AdE if AM={0,1}, TLB mapped if AM={2,3,4} */
149 adetlb_mask
= 0xc0380000;
153 /* AdE if AM={0,1,2,5}, TLB mapped if AM={3,4} */
154 adetlb_mask
= 0xe4180000;
157 /* does this AM cause AdE in current execution mode */
158 if ((adetlb_mask
<< am
) < 0) {
159 return TLBRET_BADADDR
;
164 /* is this AM mapped in current execution mode */
165 return ((adetlb_mask
<< am
) < 0);
168 return TLBRET_BADADDR
;
172 static int get_seg_physical_address(CPUMIPSState
*env
, hwaddr
*physical
,
173 int *prot
, target_ulong real_address
,
174 int rw
, int access_type
, int mmu_idx
,
175 unsigned int am
, bool eu
,
176 target_ulong segmask
,
177 hwaddr physical_base
)
179 int mapped
= is_seg_am_mapped(am
, eu
, mmu_idx
);
182 /* is_seg_am_mapped can report TLBRET_BADADDR */
185 /* The segment is TLB mapped */
186 return env
->tlb
->map_address(env
, physical
, prot
, real_address
, rw
,
189 /* The segment is unmapped */
190 *physical
= physical_base
| (real_address
& segmask
);
191 *prot
= PAGE_READ
| PAGE_WRITE
;
196 static int get_segctl_physical_address(CPUMIPSState
*env
, hwaddr
*physical
,
197 int *prot
, target_ulong real_address
,
198 int rw
, int access_type
, int mmu_idx
,
199 uint16_t segctl
, target_ulong segmask
)
201 unsigned int am
= (segctl
& CP0SC_AM_MASK
) >> CP0SC_AM
;
202 bool eu
= (segctl
>> CP0SC_EU
) & 1;
203 hwaddr pa
= ((hwaddr
)segctl
& CP0SC_PA_MASK
) << 20;
205 return get_seg_physical_address(env
, physical
, prot
, real_address
, rw
,
206 access_type
, mmu_idx
, am
, eu
, segmask
,
207 pa
& ~(hwaddr
)segmask
);
210 static int get_physical_address (CPUMIPSState
*env
, hwaddr
*physical
,
211 int *prot
, target_ulong real_address
,
212 int rw
, int access_type
, int mmu_idx
)
214 /* User mode can only access useg/xuseg */
215 #if defined(TARGET_MIPS64)
216 int user_mode
= mmu_idx
== MIPS_HFLAG_UM
;
217 int supervisor_mode
= mmu_idx
== MIPS_HFLAG_SM
;
218 int kernel_mode
= !user_mode
&& !supervisor_mode
;
219 int UX
= (env
->CP0_Status
& (1 << CP0St_UX
)) != 0;
220 int SX
= (env
->CP0_Status
& (1 << CP0St_SX
)) != 0;
221 int KX
= (env
->CP0_Status
& (1 << CP0St_KX
)) != 0;
223 int ret
= TLBRET_MATCH
;
224 /* effective address (modified for KVM T&E kernel segments) */
225 target_ulong address
= real_address
;
227 #define USEG_LIMIT ((target_ulong)(int32_t)0x7FFFFFFFUL)
228 #define KSEG0_BASE ((target_ulong)(int32_t)0x80000000UL)
229 #define KSEG1_BASE ((target_ulong)(int32_t)0xA0000000UL)
230 #define KSEG2_BASE ((target_ulong)(int32_t)0xC0000000UL)
231 #define KSEG3_BASE ((target_ulong)(int32_t)0xE0000000UL)
233 #define KVM_KSEG0_BASE ((target_ulong)(int32_t)0x40000000UL)
234 #define KVM_KSEG2_BASE ((target_ulong)(int32_t)0x60000000UL)
236 if (mips_um_ksegs_enabled()) {
237 /* KVM T&E adds guest kernel segments in useg */
238 if (real_address
>= KVM_KSEG0_BASE
) {
239 if (real_address
< KVM_KSEG2_BASE
) {
241 address
+= KSEG0_BASE
- KVM_KSEG0_BASE
;
242 } else if (real_address
<= USEG_LIMIT
) {
244 address
+= KSEG2_BASE
- KVM_KSEG2_BASE
;
249 if (address
<= USEG_LIMIT
) {
253 if (address
>= 0x40000000UL
) {
254 segctl
= env
->CP0_SegCtl2
;
256 segctl
= env
->CP0_SegCtl2
>> 16;
258 ret
= get_segctl_physical_address(env
, physical
, prot
, real_address
, rw
,
259 access_type
, mmu_idx
, segctl
,
261 #if defined(TARGET_MIPS64)
262 } else if (address
< 0x4000000000000000ULL
) {
264 if (UX
&& address
<= (0x3FFFFFFFFFFFFFFFULL
& env
->SEGMask
)) {
265 ret
= env
->tlb
->map_address(env
, physical
, prot
, real_address
, rw
, access_type
);
267 ret
= TLBRET_BADADDR
;
269 } else if (address
< 0x8000000000000000ULL
) {
271 if ((supervisor_mode
|| kernel_mode
) &&
272 SX
&& address
<= (0x7FFFFFFFFFFFFFFFULL
& env
->SEGMask
)) {
273 ret
= env
->tlb
->map_address(env
, physical
, prot
, real_address
, rw
, access_type
);
275 ret
= TLBRET_BADADDR
;
277 } else if (address
< 0xC000000000000000ULL
) {
279 if ((address
& 0x07FFFFFFFFFFFFFFULL
) <= env
->PAMask
) {
280 /* KX/SX/UX bit to check for each xkphys EVA access mode */
281 static const uint8_t am_ksux
[8] = {
282 [CP0SC_AM_UK
] = (1u << CP0St_KX
),
283 [CP0SC_AM_MK
] = (1u << CP0St_KX
),
284 [CP0SC_AM_MSK
] = (1u << CP0St_SX
),
285 [CP0SC_AM_MUSK
] = (1u << CP0St_UX
),
286 [CP0SC_AM_MUSUK
] = (1u << CP0St_UX
),
287 [CP0SC_AM_USK
] = (1u << CP0St_SX
),
288 [6] = (1u << CP0St_KX
),
289 [CP0SC_AM_UUSK
] = (1u << CP0St_UX
),
291 unsigned int am
= CP0SC_AM_UK
;
292 unsigned int xr
= (env
->CP0_SegCtl2
& CP0SC2_XR_MASK
) >> CP0SC2_XR
;
294 if (xr
& (1 << ((address
>> 59) & 0x7))) {
295 am
= (env
->CP0_SegCtl1
& CP0SC1_XAM_MASK
) >> CP0SC1_XAM
;
297 /* Does CP0_Status.KX/SX/UX permit the access mode (am) */
298 if (env
->CP0_Status
& am_ksux
[am
]) {
299 ret
= get_seg_physical_address(env
, physical
, prot
,
300 real_address
, rw
, access_type
,
301 mmu_idx
, am
, false, env
->PAMask
,
304 ret
= TLBRET_BADADDR
;
307 ret
= TLBRET_BADADDR
;
309 } else if (address
< 0xFFFFFFFF80000000ULL
) {
311 if (kernel_mode
&& KX
&&
312 address
<= (0xFFFFFFFF7FFFFFFFULL
& env
->SEGMask
)) {
313 ret
= env
->tlb
->map_address(env
, physical
, prot
, real_address
, rw
, access_type
);
315 ret
= TLBRET_BADADDR
;
318 } else if (address
< KSEG1_BASE
) {
320 ret
= get_segctl_physical_address(env
, physical
, prot
, real_address
, rw
,
321 access_type
, mmu_idx
,
322 env
->CP0_SegCtl1
>> 16, 0x1FFFFFFF);
323 } else if (address
< KSEG2_BASE
) {
325 ret
= get_segctl_physical_address(env
, physical
, prot
, real_address
, rw
,
326 access_type
, mmu_idx
,
327 env
->CP0_SegCtl1
, 0x1FFFFFFF);
328 } else if (address
< KSEG3_BASE
) {
330 ret
= get_segctl_physical_address(env
, physical
, prot
, real_address
, rw
,
331 access_type
, mmu_idx
,
332 env
->CP0_SegCtl0
>> 16, 0x1FFFFFFF);
335 /* XXX: debug segment is not emulated */
336 ret
= get_segctl_physical_address(env
, physical
, prot
, real_address
, rw
,
337 access_type
, mmu_idx
,
338 env
->CP0_SegCtl0
, 0x1FFFFFFF);
343 void cpu_mips_tlb_flush(CPUMIPSState
*env
)
345 MIPSCPU
*cpu
= mips_env_get_cpu(env
);
347 /* Flush qemu's TLB and discard all shadowed entries. */
349 env
->tlb
->tlb_in_use
= env
->tlb
->nb_tlb
;
352 /* Called for updates to CP0_Status. */
353 void sync_c0_status(CPUMIPSState
*env
, CPUMIPSState
*cpu
, int tc
)
355 int32_t tcstatus
, *tcst
;
356 uint32_t v
= cpu
->CP0_Status
;
357 uint32_t cu
, mx
, asid
, ksu
;
358 uint32_t mask
= ((1 << CP0TCSt_TCU3
)
359 | (1 << CP0TCSt_TCU2
)
360 | (1 << CP0TCSt_TCU1
)
361 | (1 << CP0TCSt_TCU0
)
363 | (3 << CP0TCSt_TKSU
)
364 | (0xff << CP0TCSt_TASID
));
366 cu
= (v
>> CP0St_CU0
) & 0xf;
367 mx
= (v
>> CP0St_MX
) & 0x1;
368 ksu
= (v
>> CP0St_KSU
) & 0x3;
369 asid
= env
->CP0_EntryHi
& env
->CP0_EntryHi_ASID_mask
;
371 tcstatus
= cu
<< CP0TCSt_TCU0
;
372 tcstatus
|= mx
<< CP0TCSt_TMX
;
373 tcstatus
|= ksu
<< CP0TCSt_TKSU
;
376 if (tc
== cpu
->current_tc
) {
377 tcst
= &cpu
->active_tc
.CP0_TCStatus
;
379 tcst
= &cpu
->tcs
[tc
].CP0_TCStatus
;
387 void cpu_mips_store_status(CPUMIPSState
*env
, target_ulong val
)
389 uint32_t mask
= env
->CP0_Status_rw_bitmask
;
390 target_ulong old
= env
->CP0_Status
;
392 if (env
->insn_flags
& ISA_MIPS32R6
) {
393 bool has_supervisor
= extract32(mask
, CP0St_KSU
, 2) == 0x3;
394 #if defined(TARGET_MIPS64)
395 uint32_t ksux
= (1 << CP0St_KX
) & val
;
396 ksux
|= (ksux
>> 1) & val
; /* KX = 0 forces SX to be 0 */
397 ksux
|= (ksux
>> 1) & val
; /* SX = 0 forces UX to be 0 */
398 val
= (val
& ~(7 << CP0St_UX
)) | ksux
;
400 if (has_supervisor
&& extract32(val
, CP0St_KSU
, 2) == 0x3) {
401 mask
&= ~(3 << CP0St_KSU
);
403 mask
&= ~(((1 << CP0St_SR
) | (1 << CP0St_NMI
)) & val
);
406 env
->CP0_Status
= (old
& ~mask
) | (val
& mask
);
407 #if defined(TARGET_MIPS64)
408 if ((env
->CP0_Status
^ old
) & (old
& (7 << CP0St_UX
))) {
409 /* Access to at least one of the 64-bit segments has been disabled */
410 tlb_flush(CPU(mips_env_get_cpu(env
)));
413 if (env
->CP0_Config3
& (1 << CP0C3_MT
)) {
414 sync_c0_status(env
, env
, env
->current_tc
);
420 void cpu_mips_store_cause(CPUMIPSState
*env
, target_ulong val
)
422 uint32_t mask
= 0x00C00300;
423 uint32_t old
= env
->CP0_Cause
;
426 if (env
->insn_flags
& ISA_MIPS32R2
) {
427 mask
|= 1 << CP0Ca_DC
;
429 if (env
->insn_flags
& ISA_MIPS32R6
) {
430 mask
&= ~((1 << CP0Ca_WP
) & val
);
433 env
->CP0_Cause
= (env
->CP0_Cause
& ~mask
) | (val
& mask
);
435 if ((old
^ env
->CP0_Cause
) & (1 << CP0Ca_DC
)) {
436 if (env
->CP0_Cause
& (1 << CP0Ca_DC
)) {
437 cpu_mips_stop_count(env
);
439 cpu_mips_start_count(env
);
443 /* Set/reset software interrupts */
444 for (i
= 0 ; i
< 2 ; i
++) {
445 if ((old
^ env
->CP0_Cause
) & (1 << (CP0Ca_IP
+ i
))) {
446 cpu_mips_soft_irq(env
, i
, env
->CP0_Cause
& (1 << (CP0Ca_IP
+ i
)));
452 static void raise_mmu_exception(CPUMIPSState
*env
, target_ulong address
,
453 int rw
, int tlb_error
)
455 CPUState
*cs
= CPU(mips_env_get_cpu(env
));
456 int exception
= 0, error_code
= 0;
458 if (rw
== MMU_INST_FETCH
) {
459 error_code
|= EXCP_INST_NOTAVAIL
;
465 /* Reference to kernel address from user mode or supervisor mode */
466 /* Reference to supervisor address from user mode */
467 if (rw
== MMU_DATA_STORE
) {
468 exception
= EXCP_AdES
;
470 exception
= EXCP_AdEL
;
474 /* No TLB match for a mapped address */
475 if (rw
== MMU_DATA_STORE
) {
476 exception
= EXCP_TLBS
;
478 exception
= EXCP_TLBL
;
480 error_code
|= EXCP_TLB_NOMATCH
;
483 /* TLB match with no valid bit */
484 if (rw
== MMU_DATA_STORE
) {
485 exception
= EXCP_TLBS
;
487 exception
= EXCP_TLBL
;
491 /* TLB match but 'D' bit is cleared */
492 exception
= EXCP_LTLBL
;
495 /* Execute-Inhibit Exception */
496 if (env
->CP0_PageGrain
& (1 << CP0PG_IEC
)) {
497 exception
= EXCP_TLBXI
;
499 exception
= EXCP_TLBL
;
503 /* Read-Inhibit Exception */
504 if (env
->CP0_PageGrain
& (1 << CP0PG_IEC
)) {
505 exception
= EXCP_TLBRI
;
507 exception
= EXCP_TLBL
;
511 /* Raise exception */
512 if (!(env
->hflags
& MIPS_HFLAG_DM
)) {
513 env
->CP0_BadVAddr
= address
;
515 env
->CP0_Context
= (env
->CP0_Context
& ~0x007fffff) |
516 ((address
>> 9) & 0x007ffff0);
517 env
->CP0_EntryHi
= (env
->CP0_EntryHi
& env
->CP0_EntryHi_ASID_mask
) |
518 (env
->CP0_EntryHi
& (1 << CP0EnHi_EHINV
)) |
519 (address
& (TARGET_PAGE_MASK
<< 1));
520 #if defined(TARGET_MIPS64)
521 env
->CP0_EntryHi
&= env
->SEGMask
;
523 /* PTEBase */ (env
->CP0_XContext
& ((~0ULL) << (env
->SEGBITS
- 7))) |
524 /* R */ (extract64(address
, 62, 2) << (env
->SEGBITS
- 9)) |
525 /* BadVPN2 */ (extract64(address
, 13, env
->SEGBITS
- 13) << 4);
527 cs
->exception_index
= exception
;
528 env
->error_code
= error_code
;
531 #if !defined(CONFIG_USER_ONLY)
532 hwaddr
mips_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
534 MIPSCPU
*cpu
= MIPS_CPU(cs
);
535 CPUMIPSState
*env
= &cpu
->env
;
539 if (get_physical_address(env
, &phys_addr
, &prot
, addr
, 0, ACCESS_INT
,
540 cpu_mmu_index(env
, false)) != 0) {
547 #if !defined(CONFIG_USER_ONLY)
548 #if !defined(TARGET_MIPS64)
551 * Perform hardware page table walk
553 * Memory accesses are performed using the KERNEL privilege level.
554 * Synchronous exceptions detected on memory accesses cause a silent exit
555 * from page table walking, resulting in a TLB or XTLB Refill exception.
557 * Implementations are not required to support page table walk memory
558 * accesses from mapped memory regions. When an unsupported access is
559 * attempted, a silent exit is taken, resulting in a TLB or XTLB Refill
562 * Note that if an exception is caused by AddressTranslation or LoadMemory
563 * functions, the exception is not taken, a silent exit is taken,
564 * resulting in a TLB or XTLB Refill exception.
567 static bool get_pte(CPUMIPSState
*env
, uint64_t vaddr
, int entry_size
,
570 if ((vaddr
& ((entry_size
>> 3) - 1)) != 0) {
573 if (entry_size
== 64) {
574 *pte
= cpu_ldq_code(env
, vaddr
);
576 *pte
= cpu_ldl_code(env
, vaddr
);
581 static uint64_t get_tlb_entry_layout(CPUMIPSState
*env
, uint64_t entry
,
582 int entry_size
, int ptei
)
584 uint64_t result
= entry
;
586 if (ptei
> entry_size
) {
589 result
>>= (ptei
- 2);
592 result
|= rixi
<< CP0EnLo_XI
;
596 static int walk_directory(CPUMIPSState
*env
, uint64_t *vaddr
,
597 int directory_index
, bool *huge_page
, bool *hgpg_directory_hit
,
598 uint64_t *pw_entrylo0
, uint64_t *pw_entrylo1
)
600 int dph
= (env
->CP0_PWCtl
>> CP0PC_DPH
) & 0x1;
601 int psn
= (env
->CP0_PWCtl
>> CP0PC_PSN
) & 0x3F;
602 int hugepg
= (env
->CP0_PWCtl
>> CP0PC_HUGEPG
) & 0x1;
603 int pf_ptew
= (env
->CP0_PWField
>> CP0PF_PTEW
) & 0x3F;
604 int ptew
= (env
->CP0_PWSize
>> CP0PS_PTEW
) & 0x3F;
605 int native_shift
= (((env
->CP0_PWSize
>> CP0PS_PS
) & 1) == 0) ? 2 : 3;
606 int directory_shift
= (ptew
> 1) ? -1 :
607 (hugepg
&& (ptew
== 1)) ? native_shift
+ 1 : native_shift
;
608 int leaf_shift
= (ptew
> 1) ? -1 :
609 (ptew
== 1) ? native_shift
+ 1 : native_shift
;
610 uint32_t direntry_size
= 1 << (directory_shift
+ 3);
611 uint32_t leafentry_size
= 1 << (leaf_shift
+ 3);
618 if (get_physical_address(env
, &paddr
, &prot
, *vaddr
, MMU_DATA_LOAD
,
619 ACCESS_INT
, cpu_mmu_index(env
, false)) !=
621 /* wrong base address */
624 if (!get_pte(env
, *vaddr
, direntry_size
, &entry
)) {
628 if ((entry
& (1 << psn
)) && hugepg
) {
630 *hgpg_directory_hit
= true;
631 entry
= get_tlb_entry_layout(env
, entry
, leafentry_size
, pf_ptew
);
632 w
= directory_index
- 1;
633 if (directory_index
& 0x1) {
634 /* Generate adjacent page from same PTE for odd TLB page */
636 *pw_entrylo0
= entry
& ~lsb
; /* even page */
637 *pw_entrylo1
= entry
| lsb
; /* odd page */
639 int oddpagebit
= 1 << leaf_shift
;
640 uint64_t vaddr2
= *vaddr
^ oddpagebit
;
641 if (*vaddr
& oddpagebit
) {
642 *pw_entrylo1
= entry
;
644 *pw_entrylo0
= entry
;
646 if (get_physical_address(env
, &paddr
, &prot
, vaddr2
, MMU_DATA_LOAD
,
647 ACCESS_INT
, cpu_mmu_index(env
, false)) !=
651 if (!get_pte(env
, vaddr2
, leafentry_size
, &entry
)) {
654 entry
= get_tlb_entry_layout(env
, entry
, leafentry_size
, pf_ptew
);
655 if (*vaddr
& oddpagebit
) {
656 *pw_entrylo0
= entry
;
658 *pw_entrylo1
= entry
;
670 static bool page_table_walk_refill(CPUMIPSState
*env
, vaddr address
, int rw
,
673 int gdw
= (env
->CP0_PWSize
>> CP0PS_GDW
) & 0x3F;
674 int udw
= (env
->CP0_PWSize
>> CP0PS_UDW
) & 0x3F;
675 int mdw
= (env
->CP0_PWSize
>> CP0PS_MDW
) & 0x3F;
676 int ptw
= (env
->CP0_PWSize
>> CP0PS_PTW
) & 0x3F;
677 int ptew
= (env
->CP0_PWSize
>> CP0PS_PTEW
) & 0x3F;
680 bool huge_page
= false;
681 bool hgpg_bdhit
= false;
682 bool hgpg_gdhit
= false;
683 bool hgpg_udhit
= false;
684 bool hgpg_mdhit
= false;
686 int32_t pw_pagemask
= 0;
687 target_ulong pw_entryhi
= 0;
688 uint64_t pw_entrylo0
= 0;
689 uint64_t pw_entrylo1
= 0;
691 /* Native pointer size */
692 /*For the 32-bit architectures, this bit is fixed to 0.*/
693 int native_shift
= (((env
->CP0_PWSize
>> CP0PS_PS
) & 1) == 0) ? 2 : 3;
695 /* Indices from PWField */
696 int pf_gdw
= (env
->CP0_PWField
>> CP0PF_GDW
) & 0x3F;
697 int pf_udw
= (env
->CP0_PWField
>> CP0PF_UDW
) & 0x3F;
698 int pf_mdw
= (env
->CP0_PWField
>> CP0PF_MDW
) & 0x3F;
699 int pf_ptw
= (env
->CP0_PWField
>> CP0PF_PTW
) & 0x3F;
700 int pf_ptew
= (env
->CP0_PWField
>> CP0PF_PTEW
) & 0x3F;
702 /* Indices computed from faulting address */
703 int gindex
= (address
>> pf_gdw
) & ((1 << gdw
) - 1);
704 int uindex
= (address
>> pf_udw
) & ((1 << udw
) - 1);
705 int mindex
= (address
>> pf_mdw
) & ((1 << mdw
) - 1);
706 int ptindex
= (address
>> pf_ptw
) & ((1 << ptw
) - 1);
708 /* Other HTW configs */
709 int hugepg
= (env
->CP0_PWCtl
>> CP0PC_HUGEPG
) & 0x1;
711 /* HTW Shift values (depend on entry size) */
712 int directory_shift
= (ptew
> 1) ? -1 :
713 (hugepg
&& (ptew
== 1)) ? native_shift
+ 1 : native_shift
;
714 int leaf_shift
= (ptew
> 1) ? -1 :
715 (ptew
== 1) ? native_shift
+ 1 : native_shift
;
717 /* Offsets into tables */
718 int goffset
= gindex
<< directory_shift
;
719 int uoffset
= uindex
<< directory_shift
;
720 int moffset
= mindex
<< directory_shift
;
721 int ptoffset0
= (ptindex
>> 1) << (leaf_shift
+ 1);
722 int ptoffset1
= ptoffset0
| (1 << (leaf_shift
));
724 uint32_t leafentry_size
= 1 << (leaf_shift
+ 3);
726 /* Starting address - Page Table Base */
727 uint64_t vaddr
= env
->CP0_PWBase
;
734 if (!(env
->CP0_Config3
& (1 << CP0C3_PW
))) {
735 /* walker is unimplemented */
738 if (!(env
->CP0_PWCtl
& (1 << CP0PC_PWEN
))) {
739 /* walker is disabled */
742 if (!(gdw
> 0 || udw
> 0 || mdw
> 0)) {
743 /* no structure to walk */
746 if ((directory_shift
== -1) || (leaf_shift
== -1)) {
750 /* Global Directory */
753 switch (walk_directory(env
, &vaddr
, pf_gdw
, &huge_page
, &hgpg_gdhit
,
754 &pw_entrylo0
, &pw_entrylo1
))
766 /* Upper directory */
769 switch (walk_directory(env
, &vaddr
, pf_udw
, &huge_page
, &hgpg_udhit
,
770 &pw_entrylo0
, &pw_entrylo1
))
782 /* Middle directory */
785 switch (walk_directory(env
, &vaddr
, pf_mdw
, &huge_page
, &hgpg_mdhit
,
786 &pw_entrylo0
, &pw_entrylo1
))
798 /* Leaf Level Page Table - First half of PTE pair */
800 if (get_physical_address(env
, &paddr
, &prot
, vaddr
, MMU_DATA_LOAD
,
801 ACCESS_INT
, cpu_mmu_index(env
, false)) !=
805 if (!get_pte(env
, vaddr
, leafentry_size
, &dir_entry
)) {
808 dir_entry
= get_tlb_entry_layout(env
, dir_entry
, leafentry_size
, pf_ptew
);
809 pw_entrylo0
= dir_entry
;
811 /* Leaf Level Page Table - Second half of PTE pair */
813 if (get_physical_address(env
, &paddr
, &prot
, vaddr
, MMU_DATA_LOAD
,
814 ACCESS_INT
, cpu_mmu_index(env
, false)) !=
818 if (!get_pte(env
, vaddr
, leafentry_size
, &dir_entry
)) {
821 dir_entry
= get_tlb_entry_layout(env
, dir_entry
, leafentry_size
, pf_ptew
);
822 pw_entrylo1
= dir_entry
;
826 m
= (1 << pf_ptw
) - 1;
829 switch (hgpg_bdhit
<< 3 | hgpg_gdhit
<< 2 | hgpg_udhit
<< 1 |
833 m
= (1 << pf_gdw
) - 1;
839 m
= (1 << pf_udw
) - 1;
845 m
= (1 << pf_mdw
) - 1;
852 pw_pagemask
= m
>> 12;
853 update_pagemask(env
, pw_pagemask
<< 13, &pw_pagemask
);
854 pw_entryhi
= (address
& ~0x1fff) | (env
->CP0_EntryHi
& 0xFF);
856 target_ulong tmp_entryhi
= env
->CP0_EntryHi
;
857 int32_t tmp_pagemask
= env
->CP0_PageMask
;
858 uint64_t tmp_entrylo0
= env
->CP0_EntryLo0
;
859 uint64_t tmp_entrylo1
= env
->CP0_EntryLo1
;
861 env
->CP0_EntryHi
= pw_entryhi
;
862 env
->CP0_PageMask
= pw_pagemask
;
863 env
->CP0_EntryLo0
= pw_entrylo0
;
864 env
->CP0_EntryLo1
= pw_entrylo1
;
867 * The hardware page walker inserts a page into the TLB in a manner
868 * identical to a TLBWR instruction as executed by the software refill
871 r4k_helper_tlbwr(env
);
873 env
->CP0_EntryHi
= tmp_entryhi
;
874 env
->CP0_PageMask
= tmp_pagemask
;
875 env
->CP0_EntryLo0
= tmp_entrylo0
;
876 env
->CP0_EntryLo1
= tmp_entrylo1
;
883 int mips_cpu_handle_mmu_fault(CPUState
*cs
, vaddr address
, int size
, int rw
,
886 MIPSCPU
*cpu
= MIPS_CPU(cs
);
887 CPUMIPSState
*env
= &cpu
->env
;
888 #if !defined(CONFIG_USER_ONLY)
896 log_cpu_state(cs
, 0);
898 qemu_log_mask(CPU_LOG_MMU
,
899 "%s pc " TARGET_FMT_lx
" ad %" VADDR_PRIx
" rw %d mmu_idx %d\n",
900 __func__
, env
->active_tc
.PC
, address
, rw
, mmu_idx
);
903 #if !defined(CONFIG_USER_ONLY)
904 /* XXX: put correct access by using cpu_restore_state() correctly */
905 access_type
= ACCESS_INT
;
906 ret
= get_physical_address(env
, &physical
, &prot
,
907 address
, rw
, access_type
, mmu_idx
);
910 qemu_log_mask(CPU_LOG_MMU
,
911 "%s address=%" VADDR_PRIx
" physical " TARGET_FMT_plx
912 " prot %d\n", __func__
, address
, physical
, prot
);
915 qemu_log_mask(CPU_LOG_MMU
,
916 "%s address=%" VADDR_PRIx
" ret %d\n", __func__
, address
,
920 if (ret
== TLBRET_MATCH
) {
921 tlb_set_page(cs
, address
& TARGET_PAGE_MASK
,
922 physical
& TARGET_PAGE_MASK
, prot
| PAGE_EXEC
,
923 mmu_idx
, TARGET_PAGE_SIZE
);
928 #if !defined(CONFIG_USER_ONLY)
929 #if !defined(TARGET_MIPS64)
930 if ((ret
== TLBRET_NOMATCH
) && (env
->tlb
->nb_tlb
> 1)) {
932 * Memory reads during hardware page table walking are performed
933 * as if they were kernel-mode load instructions.
935 int mode
= (env
->hflags
& MIPS_HFLAG_KSU
);
937 env
->hflags
&= ~MIPS_HFLAG_KSU
;
938 ret_walker
= page_table_walk_refill(env
, address
, rw
, mmu_idx
);
941 ret
= get_physical_address(env
, &physical
, &prot
,
942 address
, rw
, access_type
, mmu_idx
);
943 if (ret
== TLBRET_MATCH
) {
944 tlb_set_page(cs
, address
& TARGET_PAGE_MASK
,
945 physical
& TARGET_PAGE_MASK
, prot
| PAGE_EXEC
,
946 mmu_idx
, TARGET_PAGE_SIZE
);
954 raise_mmu_exception(env
, address
, rw
, ret
);
961 #if !defined(CONFIG_USER_ONLY)
962 hwaddr
cpu_mips_translate_address(CPUMIPSState
*env
, target_ulong address
, int rw
)
970 access_type
= ACCESS_INT
;
971 ret
= get_physical_address(env
, &physical
, &prot
, address
, rw
, access_type
,
972 cpu_mmu_index(env
, false));
973 if (ret
!= TLBRET_MATCH
) {
974 raise_mmu_exception(env
, address
, rw
, ret
);
981 static const char * const excp_names
[EXCP_LAST
+ 1] = {
982 [EXCP_RESET
] = "reset",
983 [EXCP_SRESET
] = "soft reset",
984 [EXCP_DSS
] = "debug single step",
985 [EXCP_DINT
] = "debug interrupt",
986 [EXCP_NMI
] = "non-maskable interrupt",
987 [EXCP_MCHECK
] = "machine check",
988 [EXCP_EXT_INTERRUPT
] = "interrupt",
989 [EXCP_DFWATCH
] = "deferred watchpoint",
990 [EXCP_DIB
] = "debug instruction breakpoint",
991 [EXCP_IWATCH
] = "instruction fetch watchpoint",
992 [EXCP_AdEL
] = "address error load",
993 [EXCP_AdES
] = "address error store",
994 [EXCP_TLBF
] = "TLB refill",
995 [EXCP_IBE
] = "instruction bus error",
996 [EXCP_DBp
] = "debug breakpoint",
997 [EXCP_SYSCALL
] = "syscall",
998 [EXCP_BREAK
] = "break",
999 [EXCP_CpU
] = "coprocessor unusable",
1000 [EXCP_RI
] = "reserved instruction",
1001 [EXCP_OVERFLOW
] = "arithmetic overflow",
1002 [EXCP_TRAP
] = "trap",
1003 [EXCP_FPE
] = "floating point",
1004 [EXCP_DDBS
] = "debug data break store",
1005 [EXCP_DWATCH
] = "data watchpoint",
1006 [EXCP_LTLBL
] = "TLB modify",
1007 [EXCP_TLBL
] = "TLB load",
1008 [EXCP_TLBS
] = "TLB store",
1009 [EXCP_DBE
] = "data bus error",
1010 [EXCP_DDBL
] = "debug data break load",
1011 [EXCP_THREAD
] = "thread",
1012 [EXCP_MDMX
] = "MDMX",
1013 [EXCP_C2E
] = "precise coprocessor 2",
1014 [EXCP_CACHE
] = "cache error",
1015 [EXCP_TLBXI
] = "TLB execute-inhibit",
1016 [EXCP_TLBRI
] = "TLB read-inhibit",
1017 [EXCP_MSADIS
] = "MSA disabled",
1018 [EXCP_MSAFPE
] = "MSA floating point",
1022 target_ulong
exception_resume_pc (CPUMIPSState
*env
)
1024 target_ulong bad_pc
;
1025 target_ulong isa_mode
;
1027 isa_mode
= !!(env
->hflags
& MIPS_HFLAG_M16
);
1028 bad_pc
= env
->active_tc
.PC
| isa_mode
;
1029 if (env
->hflags
& MIPS_HFLAG_BMASK
) {
1030 /* If the exception was raised from a delay slot, come back to
1032 bad_pc
-= (env
->hflags
& MIPS_HFLAG_B16
? 2 : 4);
1038 #if !defined(CONFIG_USER_ONLY)
1039 static void set_hflags_for_handler (CPUMIPSState
*env
)
1041 /* Exception handlers are entered in 32-bit mode. */
1042 env
->hflags
&= ~(MIPS_HFLAG_M16
);
1043 /* ...except that microMIPS lets you choose. */
1044 if (env
->insn_flags
& ASE_MICROMIPS
) {
1045 env
->hflags
|= (!!(env
->CP0_Config3
1046 & (1 << CP0C3_ISA_ON_EXC
))
1047 << MIPS_HFLAG_M16_SHIFT
);
1051 static inline void set_badinstr_registers(CPUMIPSState
*env
)
1053 if (env
->insn_flags
& ISA_NANOMIPS32
) {
1054 if (env
->CP0_Config3
& (1 << CP0C3_BI
)) {
1055 uint32_t instr
= (cpu_lduw_code(env
, env
->active_tc
.PC
)) << 16;
1056 if ((instr
& 0x10000000) == 0) {
1057 instr
|= cpu_lduw_code(env
, env
->active_tc
.PC
+ 2);
1059 env
->CP0_BadInstr
= instr
;
1061 if ((instr
& 0xFC000000) == 0x60000000) {
1062 instr
= cpu_lduw_code(env
, env
->active_tc
.PC
+ 4) << 16;
1063 env
->CP0_BadInstrX
= instr
;
1069 if (env
->hflags
& MIPS_HFLAG_M16
) {
1070 /* TODO: add BadInstr support for microMIPS */
1073 if (env
->CP0_Config3
& (1 << CP0C3_BI
)) {
1074 env
->CP0_BadInstr
= cpu_ldl_code(env
, env
->active_tc
.PC
);
1076 if ((env
->CP0_Config3
& (1 << CP0C3_BP
)) &&
1077 (env
->hflags
& MIPS_HFLAG_BMASK
)) {
1078 env
->CP0_BadInstrP
= cpu_ldl_code(env
, env
->active_tc
.PC
- 4);
1083 void mips_cpu_do_interrupt(CPUState
*cs
)
1085 #if !defined(CONFIG_USER_ONLY)
1086 MIPSCPU
*cpu
= MIPS_CPU(cs
);
1087 CPUMIPSState
*env
= &cpu
->env
;
1088 bool update_badinstr
= 0;
1089 target_ulong offset
;
1093 if (qemu_loglevel_mask(CPU_LOG_INT
)
1094 && cs
->exception_index
!= EXCP_EXT_INTERRUPT
) {
1095 if (cs
->exception_index
< 0 || cs
->exception_index
> EXCP_LAST
) {
1098 name
= excp_names
[cs
->exception_index
];
1101 qemu_log("%s enter: PC " TARGET_FMT_lx
" EPC " TARGET_FMT_lx
1103 __func__
, env
->active_tc
.PC
, env
->CP0_EPC
, name
);
1105 if (cs
->exception_index
== EXCP_EXT_INTERRUPT
&&
1106 (env
->hflags
& MIPS_HFLAG_DM
)) {
1107 cs
->exception_index
= EXCP_DINT
;
1110 switch (cs
->exception_index
) {
1112 env
->CP0_Debug
|= 1 << CP0DB_DSS
;
1113 /* Debug single step cannot be raised inside a delay slot and
1114 resume will always occur on the next instruction
1115 (but we assume the pc has always been updated during
1116 code translation). */
1117 env
->CP0_DEPC
= env
->active_tc
.PC
| !!(env
->hflags
& MIPS_HFLAG_M16
);
1118 goto enter_debug_mode
;
1120 env
->CP0_Debug
|= 1 << CP0DB_DINT
;
1123 env
->CP0_Debug
|= 1 << CP0DB_DIB
;
1126 env
->CP0_Debug
|= 1 << CP0DB_DBp
;
1127 /* Setup DExcCode - SDBBP instruction */
1128 env
->CP0_Debug
= (env
->CP0_Debug
& ~(0x1fULL
<< CP0DB_DEC
)) | 9 << CP0DB_DEC
;
1131 env
->CP0_Debug
|= 1 << CP0DB_DDBS
;
1134 env
->CP0_Debug
|= 1 << CP0DB_DDBL
;
1136 env
->CP0_DEPC
= exception_resume_pc(env
);
1137 env
->hflags
&= ~MIPS_HFLAG_BMASK
;
1139 if (env
->insn_flags
& ISA_MIPS3
) {
1140 env
->hflags
|= MIPS_HFLAG_64
;
1141 if (!(env
->insn_flags
& ISA_MIPS64R6
) ||
1142 env
->CP0_Status
& (1 << CP0St_KX
)) {
1143 env
->hflags
&= ~MIPS_HFLAG_AWRAP
;
1146 env
->hflags
|= MIPS_HFLAG_DM
| MIPS_HFLAG_CP0
;
1147 env
->hflags
&= ~(MIPS_HFLAG_KSU
);
1148 /* EJTAG probe trap enable is not implemented... */
1149 if (!(env
->CP0_Status
& (1 << CP0St_EXL
)))
1150 env
->CP0_Cause
&= ~(1U << CP0Ca_BD
);
1151 env
->active_tc
.PC
= env
->exception_base
+ 0x480;
1152 set_hflags_for_handler(env
);
1155 cpu_reset(CPU(cpu
));
1158 env
->CP0_Status
|= (1 << CP0St_SR
);
1159 memset(env
->CP0_WatchLo
, 0, sizeof(env
->CP0_WatchLo
));
1162 env
->CP0_Status
|= (1 << CP0St_NMI
);
1164 env
->CP0_ErrorEPC
= exception_resume_pc(env
);
1165 env
->hflags
&= ~MIPS_HFLAG_BMASK
;
1166 env
->CP0_Status
|= (1 << CP0St_ERL
) | (1 << CP0St_BEV
);
1167 if (env
->insn_flags
& ISA_MIPS3
) {
1168 env
->hflags
|= MIPS_HFLAG_64
;
1169 if (!(env
->insn_flags
& ISA_MIPS64R6
) ||
1170 env
->CP0_Status
& (1 << CP0St_KX
)) {
1171 env
->hflags
&= ~MIPS_HFLAG_AWRAP
;
1174 env
->hflags
|= MIPS_HFLAG_CP0
;
1175 env
->hflags
&= ~(MIPS_HFLAG_KSU
);
1176 if (!(env
->CP0_Status
& (1 << CP0St_EXL
)))
1177 env
->CP0_Cause
&= ~(1U << CP0Ca_BD
);
1178 env
->active_tc
.PC
= env
->exception_base
;
1179 set_hflags_for_handler(env
);
1181 case EXCP_EXT_INTERRUPT
:
1183 if (env
->CP0_Cause
& (1 << CP0Ca_IV
)) {
1184 uint32_t spacing
= (env
->CP0_IntCtl
>> CP0IntCtl_VS
) & 0x1f;
1186 if ((env
->CP0_Status
& (1 << CP0St_BEV
)) || spacing
== 0) {
1189 uint32_t vector
= 0;
1190 uint32_t pending
= (env
->CP0_Cause
& CP0Ca_IP_mask
) >> CP0Ca_IP
;
1192 if (env
->CP0_Config3
& (1 << CP0C3_VEIC
)) {
1193 /* For VEIC mode, the external interrupt controller feeds
1194 * the vector through the CP0Cause IP lines. */
1197 /* Vectored Interrupts
1198 * Mask with Status.IM7-IM0 to get enabled interrupts. */
1199 pending
&= (env
->CP0_Status
>> CP0St_IM
) & 0xff;
1200 /* Find the highest-priority interrupt. */
1201 while (pending
>>= 1) {
1205 offset
= 0x200 + (vector
* (spacing
<< 5));
1211 update_badinstr
= !(env
->error_code
& EXCP_INST_NOTAVAIL
);
1215 update_badinstr
= !(env
->error_code
& EXCP_INST_NOTAVAIL
);
1216 if ((env
->error_code
& EXCP_TLB_NOMATCH
) &&
1217 !(env
->CP0_Status
& (1 << CP0St_EXL
))) {
1218 #if defined(TARGET_MIPS64)
1219 int R
= env
->CP0_BadVAddr
>> 62;
1220 int UX
= (env
->CP0_Status
& (1 << CP0St_UX
)) != 0;
1221 int KX
= (env
->CP0_Status
& (1 << CP0St_KX
)) != 0;
1223 if ((R
!= 0 || UX
) && (R
!= 3 || KX
) &&
1224 (!(env
->insn_flags
& (INSN_LOONGSON2E
| INSN_LOONGSON2F
)))) {
1229 #if defined(TARGET_MIPS64)
1236 update_badinstr
= 1;
1237 if ((env
->error_code
& EXCP_TLB_NOMATCH
) &&
1238 !(env
->CP0_Status
& (1 << CP0St_EXL
))) {
1239 #if defined(TARGET_MIPS64)
1240 int R
= env
->CP0_BadVAddr
>> 62;
1241 int UX
= (env
->CP0_Status
& (1 << CP0St_UX
)) != 0;
1242 int KX
= (env
->CP0_Status
& (1 << CP0St_KX
)) != 0;
1244 if ((R
!= 0 || UX
) && (R
!= 3 || KX
) &&
1245 (!(env
->insn_flags
& (INSN_LOONGSON2E
| INSN_LOONGSON2F
)))) {
1250 #if defined(TARGET_MIPS64)
1257 update_badinstr
= !(env
->error_code
& EXCP_INST_NOTAVAIL
);
1261 update_badinstr
= 1;
1271 update_badinstr
= 1;
1275 update_badinstr
= 1;
1279 update_badinstr
= 1;
1283 update_badinstr
= 1;
1284 env
->CP0_Cause
= (env
->CP0_Cause
& ~(0x3 << CP0Ca_CE
)) |
1285 (env
->error_code
<< CP0Ca_CE
);
1289 update_badinstr
= 1;
1293 update_badinstr
= 1;
1297 update_badinstr
= 1;
1301 update_badinstr
= 1;
1308 update_badinstr
= 1;
1315 update_badinstr
= 1;
1322 /* XXX: TODO: manage deferred watch exceptions */
1337 if (!(env
->CP0_Status
& (1 << CP0St_EXL
))) {
1338 env
->CP0_EPC
= exception_resume_pc(env
);
1339 if (update_badinstr
) {
1340 set_badinstr_registers(env
);
1342 if (env
->hflags
& MIPS_HFLAG_BMASK
) {
1343 env
->CP0_Cause
|= (1U << CP0Ca_BD
);
1345 env
->CP0_Cause
&= ~(1U << CP0Ca_BD
);
1347 env
->CP0_Status
|= (1 << CP0St_EXL
);
1348 if (env
->insn_flags
& ISA_MIPS3
) {
1349 env
->hflags
|= MIPS_HFLAG_64
;
1350 if (!(env
->insn_flags
& ISA_MIPS64R6
) ||
1351 env
->CP0_Status
& (1 << CP0St_KX
)) {
1352 env
->hflags
&= ~MIPS_HFLAG_AWRAP
;
1355 env
->hflags
|= MIPS_HFLAG_CP0
;
1356 env
->hflags
&= ~(MIPS_HFLAG_KSU
);
1358 env
->hflags
&= ~MIPS_HFLAG_BMASK
;
1359 if (env
->CP0_Status
& (1 << CP0St_BEV
)) {
1360 env
->active_tc
.PC
= env
->exception_base
+ 0x200;
1361 } else if (cause
== 30 && !(env
->CP0_Config3
& (1 << CP0C3_SC
) &&
1362 env
->CP0_Config5
& (1 << CP0C5_CV
))) {
1363 /* Force KSeg1 for cache errors */
1364 env
->active_tc
.PC
= KSEG1_BASE
| (env
->CP0_EBase
& 0x1FFFF000);
1366 env
->active_tc
.PC
= env
->CP0_EBase
& ~0xfff;
1369 env
->active_tc
.PC
+= offset
;
1370 set_hflags_for_handler(env
);
1371 env
->CP0_Cause
= (env
->CP0_Cause
& ~(0x1f << CP0Ca_EC
)) | (cause
<< CP0Ca_EC
);
1376 if (qemu_loglevel_mask(CPU_LOG_INT
)
1377 && cs
->exception_index
!= EXCP_EXT_INTERRUPT
) {
1378 qemu_log("%s: PC " TARGET_FMT_lx
" EPC " TARGET_FMT_lx
" cause %d\n"
1379 " S %08x C %08x A " TARGET_FMT_lx
" D " TARGET_FMT_lx
"\n",
1380 __func__
, env
->active_tc
.PC
, env
->CP0_EPC
, cause
,
1381 env
->CP0_Status
, env
->CP0_Cause
, env
->CP0_BadVAddr
,
1385 cs
->exception_index
= EXCP_NONE
;
1388 bool mips_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
)
1390 if (interrupt_request
& CPU_INTERRUPT_HARD
) {
1391 MIPSCPU
*cpu
= MIPS_CPU(cs
);
1392 CPUMIPSState
*env
= &cpu
->env
;
1394 if (cpu_mips_hw_interrupts_enabled(env
) &&
1395 cpu_mips_hw_interrupts_pending(env
)) {
1397 cs
->exception_index
= EXCP_EXT_INTERRUPT
;
1398 env
->error_code
= 0;
1399 mips_cpu_do_interrupt(cs
);
1406 #if !defined(CONFIG_USER_ONLY)
1407 void r4k_invalidate_tlb (CPUMIPSState
*env
, int idx
, int use_extra
)
1409 MIPSCPU
*cpu
= mips_env_get_cpu(env
);
1414 uint16_t ASID
= env
->CP0_EntryHi
& env
->CP0_EntryHi_ASID_mask
;
1417 tlb
= &env
->tlb
->mmu
.r4k
.tlb
[idx
];
1418 /* The qemu TLB is flushed when the ASID changes, so no need to
1419 flush these entries again. */
1420 if (tlb
->G
== 0 && tlb
->ASID
!= ASID
) {
1424 if (use_extra
&& env
->tlb
->tlb_in_use
< MIPS_TLB_MAX
) {
1425 /* For tlbwr, we can shadow the discarded entry into
1426 a new (fake) TLB entry, as long as the guest can not
1427 tell that it's there. */
1428 env
->tlb
->mmu
.r4k
.tlb
[env
->tlb
->tlb_in_use
] = *tlb
;
1429 env
->tlb
->tlb_in_use
++;
1433 /* 1k pages are not supported. */
1434 mask
= tlb
->PageMask
| ~(TARGET_PAGE_MASK
<< 1);
1437 addr
= tlb
->VPN
& ~mask
;
1438 #if defined(TARGET_MIPS64)
1439 if (addr
>= (0xFFFFFFFF80000000ULL
& env
->SEGMask
)) {
1440 addr
|= 0x3FFFFF0000000000ULL
;
1443 end
= addr
| (mask
>> 1);
1444 while (addr
< end
) {
1445 // optimize memset in tlb_flush_page!!!
1446 tlb_flush_page(cs
, addr
);
1447 addr
+= TARGET_PAGE_SIZE
;
1452 addr
= (tlb
->VPN
& ~mask
) | ((mask
>> 1) + 1);
1453 #if defined(TARGET_MIPS64)
1454 if (addr
>= (0xFFFFFFFF80000000ULL
& env
->SEGMask
)) {
1455 addr
|= 0x3FFFFF0000000000ULL
;
1459 while (addr
- 1 < end
) {
1460 // optimize memset in tlb_flush_page!!!
1461 tlb_flush_page(cs
, addr
);
1462 addr
+= TARGET_PAGE_SIZE
;
1468 void QEMU_NORETURN
do_raise_exception_err(CPUMIPSState
*env
,
1473 CPUState
*cs
= CPU(mips_env_get_cpu(env
));
1475 qemu_log_mask(CPU_LOG_INT
, "%s: %d %d\n",
1476 __func__
, exception
, error_code
);
1477 cs
->exception_index
= exception
;
1478 env
->error_code
= error_code
;
1480 cpu_loop_exit_restore(cs
, pc
);
1483 static void mips_cpu_add_definition(gpointer data
, gpointer user_data
)
1485 ObjectClass
*oc
= data
;
1486 CpuDefinitionInfoList
**cpu_list
= user_data
;
1487 CpuDefinitionInfoList
*entry
;
1488 CpuDefinitionInfo
*info
;
1489 const char *typename
;
1491 typename
= object_class_get_name(oc
);
1492 info
= g_malloc0(sizeof(*info
));
1493 info
->name
= g_strndup(typename
,
1494 strlen(typename
) - strlen("-" TYPE_MIPS_CPU
));
1495 info
->q_typename
= g_strdup(typename
);
1497 entry
= g_malloc0(sizeof(*entry
));
1498 entry
->value
= info
;
1499 entry
->next
= *cpu_list
;
1503 CpuDefinitionInfoList
*qmp_query_cpu_definitions(Error
**errp
)
1505 CpuDefinitionInfoList
*cpu_list
= NULL
;
1508 list
= object_class_get_list(TYPE_MIPS_CPU
, false);
1509 g_slist_foreach(list
, mips_cpu_add_definition
, &cpu_list
);