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.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 <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"
38 #if !defined(CONFIG_USER_ONLY)
40 /* no MMU emulation */
41 int no_mmu_map_address(CPUMIPSState
*env
, hwaddr
*physical
, int *prot
,
42 target_ulong address
, int rw
, int access_type
)
45 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
49 /* fixed mapping MMU emulation */
50 int fixed_mmu_map_address(CPUMIPSState
*env
, hwaddr
*physical
, int *prot
,
51 target_ulong address
, int rw
, int access_type
)
53 if (address
<= (int32_t)0x7FFFFFFFUL
) {
54 if (!(env
->CP0_Status
& (1 << CP0St_ERL
))) {
55 *physical
= address
+ 0x40000000UL
;
59 } else if (address
<= (int32_t)0xBFFFFFFFUL
) {
60 *physical
= address
& 0x1FFFFFFF;
65 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
69 /* MIPS32/MIPS64 R4000-style MMU emulation */
70 int r4k_map_address(CPUMIPSState
*env
, hwaddr
*physical
, int *prot
,
71 target_ulong address
, int rw
, int access_type
)
73 uint16_t ASID
= env
->CP0_EntryHi
& env
->CP0_EntryHi_ASID_mask
;
74 uint32_t MMID
= env
->CP0_MemoryMapID
;
75 bool mi
= !!((env
->CP0_Config5
>> CP0C5_MI
) & 1);
79 MMID
= mi
? MMID
: (uint32_t) ASID
;
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/MMID, virtual page number & size */
92 tlb_mmid
= mi
? tlb
->MMID
: (uint32_t) tlb
->ASID
;
93 if ((tlb
->G
== 1 || tlb_mmid
== MMID
) && VPN
== tag
&& !tlb
->EHINV
) {
95 int n
= !!(address
& mask
& ~(mask
>> 1));
96 /* Check access rights */
97 if (!(n
? tlb
->V1
: tlb
->V0
)) {
98 return TLBRET_INVALID
;
100 if (rw
== MMU_INST_FETCH
&& (n
? tlb
->XI1
: tlb
->XI0
)) {
103 if (rw
== MMU_DATA_LOAD
&& (n
? tlb
->RI1
: tlb
->RI0
)) {
106 if (rw
!= MMU_DATA_STORE
|| (n
? tlb
->D1
: tlb
->D0
)) {
107 *physical
= tlb
->PFN
[n
] | (address
& (mask
>> 1));
109 if (n
? tlb
->D1
: tlb
->D0
) {
112 if (!(n
? tlb
->XI1
: tlb
->XI0
)) {
120 return TLBRET_NOMATCH
;
123 static int is_seg_am_mapped(unsigned int am
, bool eu
, int mmu_idx
)
126 * Interpret access control mode and mmu_idx.
129 * UK 0 0 1 1 0 0 - - 0
130 * MK 1 0 1 1 0 1 - - !eu
131 * MSK 2 0 0 1 0 1 1 - !eu
132 * MUSK 3 0 0 0 0 1 1 1 !eu
133 * MUSUK 4 0 0 0 0 0 1 1 0
134 * USK 5 0 0 1 0 0 0 - 0
135 * - 6 - - - - - - - -
136 * UUSK 7 0 0 0 0 0 0 0 0
142 /* If EU is set, always unmapped */
148 /* Never AdE, TLB mapped if AM={1,2,3} */
149 adetlb_mask
= 0x70000000;
153 /* AdE if AM={0,1}, TLB mapped if AM={2,3,4} */
154 adetlb_mask
= 0xc0380000;
158 /* AdE if AM={0,1,2,5}, TLB mapped if AM={3,4} */
159 adetlb_mask
= 0xe4180000;
162 /* does this AM cause AdE in current execution mode */
163 if ((adetlb_mask
<< am
) < 0) {
164 return TLBRET_BADADDR
;
169 /* is this AM mapped in current execution mode */
170 return ((adetlb_mask
<< am
) < 0);
173 return TLBRET_BADADDR
;
177 static int get_seg_physical_address(CPUMIPSState
*env
, hwaddr
*physical
,
178 int *prot
, target_ulong real_address
,
179 int rw
, int access_type
, int mmu_idx
,
180 unsigned int am
, bool eu
,
181 target_ulong segmask
,
182 hwaddr physical_base
)
184 int mapped
= is_seg_am_mapped(am
, eu
, mmu_idx
);
187 /* is_seg_am_mapped can report TLBRET_BADADDR */
190 /* The segment is TLB mapped */
191 return env
->tlb
->map_address(env
, physical
, prot
, real_address
, rw
,
194 /* The segment is unmapped */
195 *physical
= physical_base
| (real_address
& segmask
);
196 *prot
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
201 static int get_segctl_physical_address(CPUMIPSState
*env
, hwaddr
*physical
,
202 int *prot
, target_ulong real_address
,
203 int rw
, int access_type
, int mmu_idx
,
204 uint16_t segctl
, target_ulong segmask
)
206 unsigned int am
= (segctl
& CP0SC_AM_MASK
) >> CP0SC_AM
;
207 bool eu
= (segctl
>> CP0SC_EU
) & 1;
208 hwaddr pa
= ((hwaddr
)segctl
& CP0SC_PA_MASK
) << 20;
210 return get_seg_physical_address(env
, physical
, prot
, real_address
, rw
,
211 access_type
, mmu_idx
, am
, eu
, segmask
,
212 pa
& ~(hwaddr
)segmask
);
215 static int get_physical_address(CPUMIPSState
*env
, hwaddr
*physical
,
216 int *prot
, target_ulong real_address
,
217 int rw
, int access_type
, int mmu_idx
)
219 /* User mode can only access useg/xuseg */
220 #if defined(TARGET_MIPS64)
221 int user_mode
= mmu_idx
== MIPS_HFLAG_UM
;
222 int supervisor_mode
= mmu_idx
== MIPS_HFLAG_SM
;
223 int kernel_mode
= !user_mode
&& !supervisor_mode
;
224 int UX
= (env
->CP0_Status
& (1 << CP0St_UX
)) != 0;
225 int SX
= (env
->CP0_Status
& (1 << CP0St_SX
)) != 0;
226 int KX
= (env
->CP0_Status
& (1 << CP0St_KX
)) != 0;
228 int ret
= TLBRET_MATCH
;
229 /* effective address (modified for KVM T&E kernel segments) */
230 target_ulong address
= real_address
;
232 #define USEG_LIMIT ((target_ulong)(int32_t)0x7FFFFFFFUL)
233 #define KSEG0_BASE ((target_ulong)(int32_t)0x80000000UL)
234 #define KSEG1_BASE ((target_ulong)(int32_t)0xA0000000UL)
235 #define KSEG2_BASE ((target_ulong)(int32_t)0xC0000000UL)
236 #define KSEG3_BASE ((target_ulong)(int32_t)0xE0000000UL)
238 #define KVM_KSEG0_BASE ((target_ulong)(int32_t)0x40000000UL)
239 #define KVM_KSEG2_BASE ((target_ulong)(int32_t)0x60000000UL)
241 if (mips_um_ksegs_enabled()) {
242 /* KVM T&E adds guest kernel segments in useg */
243 if (real_address
>= KVM_KSEG0_BASE
) {
244 if (real_address
< KVM_KSEG2_BASE
) {
246 address
+= KSEG0_BASE
- KVM_KSEG0_BASE
;
247 } else if (real_address
<= USEG_LIMIT
) {
249 address
+= KSEG2_BASE
- KVM_KSEG2_BASE
;
254 if (address
<= USEG_LIMIT
) {
258 if (address
>= 0x40000000UL
) {
259 segctl
= env
->CP0_SegCtl2
;
261 segctl
= env
->CP0_SegCtl2
>> 16;
263 ret
= get_segctl_physical_address(env
, physical
, prot
,
264 real_address
, rw
, access_type
,
265 mmu_idx
, segctl
, 0x3FFFFFFF);
266 #if defined(TARGET_MIPS64)
267 } else if (address
< 0x4000000000000000ULL
) {
269 if (UX
&& address
<= (0x3FFFFFFFFFFFFFFFULL
& env
->SEGMask
)) {
270 ret
= env
->tlb
->map_address(env
, physical
, prot
,
271 real_address
, rw
, access_type
);
273 ret
= TLBRET_BADADDR
;
275 } else if (address
< 0x8000000000000000ULL
) {
277 if ((supervisor_mode
|| kernel_mode
) &&
278 SX
&& address
<= (0x7FFFFFFFFFFFFFFFULL
& env
->SEGMask
)) {
279 ret
= env
->tlb
->map_address(env
, physical
, prot
,
280 real_address
, rw
, access_type
);
282 ret
= TLBRET_BADADDR
;
284 } else if (address
< 0xC000000000000000ULL
) {
286 if ((address
& 0x07FFFFFFFFFFFFFFULL
) <= env
->PAMask
) {
287 /* KX/SX/UX bit to check for each xkphys EVA access mode */
288 static const uint8_t am_ksux
[8] = {
289 [CP0SC_AM_UK
] = (1u << CP0St_KX
),
290 [CP0SC_AM_MK
] = (1u << CP0St_KX
),
291 [CP0SC_AM_MSK
] = (1u << CP0St_SX
),
292 [CP0SC_AM_MUSK
] = (1u << CP0St_UX
),
293 [CP0SC_AM_MUSUK
] = (1u << CP0St_UX
),
294 [CP0SC_AM_USK
] = (1u << CP0St_SX
),
295 [6] = (1u << CP0St_KX
),
296 [CP0SC_AM_UUSK
] = (1u << CP0St_UX
),
298 unsigned int am
= CP0SC_AM_UK
;
299 unsigned int xr
= (env
->CP0_SegCtl2
& CP0SC2_XR_MASK
) >> CP0SC2_XR
;
301 if (xr
& (1 << ((address
>> 59) & 0x7))) {
302 am
= (env
->CP0_SegCtl1
& CP0SC1_XAM_MASK
) >> CP0SC1_XAM
;
304 /* Does CP0_Status.KX/SX/UX permit the access mode (am) */
305 if (env
->CP0_Status
& am_ksux
[am
]) {
306 ret
= get_seg_physical_address(env
, physical
, prot
,
307 real_address
, rw
, access_type
,
308 mmu_idx
, am
, false, env
->PAMask
,
311 ret
= TLBRET_BADADDR
;
314 ret
= TLBRET_BADADDR
;
316 } else if (address
< 0xFFFFFFFF80000000ULL
) {
318 if (kernel_mode
&& KX
&&
319 address
<= (0xFFFFFFFF7FFFFFFFULL
& env
->SEGMask
)) {
320 ret
= env
->tlb
->map_address(env
, physical
, prot
,
321 real_address
, rw
, access_type
);
323 ret
= TLBRET_BADADDR
;
326 } else if (address
< KSEG1_BASE
) {
328 ret
= get_segctl_physical_address(env
, physical
, prot
, real_address
, rw
,
329 access_type
, mmu_idx
,
330 env
->CP0_SegCtl1
>> 16, 0x1FFFFFFF);
331 } else if (address
< KSEG2_BASE
) {
333 ret
= get_segctl_physical_address(env
, physical
, prot
, real_address
, rw
,
334 access_type
, mmu_idx
,
335 env
->CP0_SegCtl1
, 0x1FFFFFFF);
336 } else if (address
< KSEG3_BASE
) {
338 ret
= get_segctl_physical_address(env
, physical
, prot
, real_address
, rw
,
339 access_type
, mmu_idx
,
340 env
->CP0_SegCtl0
>> 16, 0x1FFFFFFF);
344 * XXX: debug segment is not emulated
346 ret
= get_segctl_physical_address(env
, physical
, prot
, real_address
, rw
,
347 access_type
, mmu_idx
,
348 env
->CP0_SegCtl0
, 0x1FFFFFFF);
353 void cpu_mips_tlb_flush(CPUMIPSState
*env
)
355 /* Flush qemu's TLB and discard all shadowed entries. */
356 tlb_flush(env_cpu(env
));
357 env
->tlb
->tlb_in_use
= env
->tlb
->nb_tlb
;
360 /* Called for updates to CP0_Status. */
361 void sync_c0_status(CPUMIPSState
*env
, CPUMIPSState
*cpu
, int tc
)
363 int32_t tcstatus
, *tcst
;
364 uint32_t v
= cpu
->CP0_Status
;
365 uint32_t cu
, mx
, asid
, ksu
;
366 uint32_t mask
= ((1 << CP0TCSt_TCU3
)
367 | (1 << CP0TCSt_TCU2
)
368 | (1 << CP0TCSt_TCU1
)
369 | (1 << CP0TCSt_TCU0
)
371 | (3 << CP0TCSt_TKSU
)
372 | (0xff << CP0TCSt_TASID
));
374 cu
= (v
>> CP0St_CU0
) & 0xf;
375 mx
= (v
>> CP0St_MX
) & 0x1;
376 ksu
= (v
>> CP0St_KSU
) & 0x3;
377 asid
= env
->CP0_EntryHi
& env
->CP0_EntryHi_ASID_mask
;
379 tcstatus
= cu
<< CP0TCSt_TCU0
;
380 tcstatus
|= mx
<< CP0TCSt_TMX
;
381 tcstatus
|= ksu
<< CP0TCSt_TKSU
;
384 if (tc
== cpu
->current_tc
) {
385 tcst
= &cpu
->active_tc
.CP0_TCStatus
;
387 tcst
= &cpu
->tcs
[tc
].CP0_TCStatus
;
395 void cpu_mips_store_status(CPUMIPSState
*env
, target_ulong val
)
397 uint32_t mask
= env
->CP0_Status_rw_bitmask
;
398 target_ulong old
= env
->CP0_Status
;
400 if (env
->insn_flags
& ISA_MIPS32R6
) {
401 bool has_supervisor
= extract32(mask
, CP0St_KSU
, 2) == 0x3;
402 #if defined(TARGET_MIPS64)
403 uint32_t ksux
= (1 << CP0St_KX
) & val
;
404 ksux
|= (ksux
>> 1) & val
; /* KX = 0 forces SX to be 0 */
405 ksux
|= (ksux
>> 1) & val
; /* SX = 0 forces UX to be 0 */
406 val
= (val
& ~(7 << CP0St_UX
)) | ksux
;
408 if (has_supervisor
&& extract32(val
, CP0St_KSU
, 2) == 0x3) {
409 mask
&= ~(3 << CP0St_KSU
);
411 mask
&= ~(((1 << CP0St_SR
) | (1 << CP0St_NMI
)) & val
);
414 env
->CP0_Status
= (old
& ~mask
) | (val
& mask
);
415 #if defined(TARGET_MIPS64)
416 if ((env
->CP0_Status
^ old
) & (old
& (7 << CP0St_UX
))) {
417 /* Access to at least one of the 64-bit segments has been disabled */
418 tlb_flush(env_cpu(env
));
421 if (ase_mt_available(env
)) {
422 sync_c0_status(env
, env
, env
->current_tc
);
428 void cpu_mips_store_cause(CPUMIPSState
*env
, target_ulong val
)
430 uint32_t mask
= 0x00C00300;
431 uint32_t old
= env
->CP0_Cause
;
434 if (env
->insn_flags
& ISA_MIPS32R2
) {
435 mask
|= 1 << CP0Ca_DC
;
437 if (env
->insn_flags
& ISA_MIPS32R6
) {
438 mask
&= ~((1 << CP0Ca_WP
) & val
);
441 env
->CP0_Cause
= (env
->CP0_Cause
& ~mask
) | (val
& mask
);
443 if ((old
^ env
->CP0_Cause
) & (1 << CP0Ca_DC
)) {
444 if (env
->CP0_Cause
& (1 << CP0Ca_DC
)) {
445 cpu_mips_stop_count(env
);
447 cpu_mips_start_count(env
);
451 /* Set/reset software interrupts */
452 for (i
= 0 ; i
< 2 ; i
++) {
453 if ((old
^ env
->CP0_Cause
) & (1 << (CP0Ca_IP
+ i
))) {
454 cpu_mips_soft_irq(env
, i
, env
->CP0_Cause
& (1 << (CP0Ca_IP
+ i
)));
460 static void raise_mmu_exception(CPUMIPSState
*env
, target_ulong address
,
461 int rw
, int tlb_error
)
463 CPUState
*cs
= env_cpu(env
);
464 int exception
= 0, error_code
= 0;
466 if (rw
== MMU_INST_FETCH
) {
467 error_code
|= EXCP_INST_NOTAVAIL
;
473 /* Reference to kernel address from user mode or supervisor mode */
474 /* Reference to supervisor address from user mode */
475 if (rw
== MMU_DATA_STORE
) {
476 exception
= EXCP_AdES
;
478 exception
= EXCP_AdEL
;
482 /* No TLB match for a mapped address */
483 if (rw
== MMU_DATA_STORE
) {
484 exception
= EXCP_TLBS
;
486 exception
= EXCP_TLBL
;
488 error_code
|= EXCP_TLB_NOMATCH
;
491 /* TLB match with no valid bit */
492 if (rw
== MMU_DATA_STORE
) {
493 exception
= EXCP_TLBS
;
495 exception
= EXCP_TLBL
;
499 /* TLB match but 'D' bit is cleared */
500 exception
= EXCP_LTLBL
;
503 /* Execute-Inhibit Exception */
504 if (env
->CP0_PageGrain
& (1 << CP0PG_IEC
)) {
505 exception
= EXCP_TLBXI
;
507 exception
= EXCP_TLBL
;
511 /* Read-Inhibit Exception */
512 if (env
->CP0_PageGrain
& (1 << CP0PG_IEC
)) {
513 exception
= EXCP_TLBRI
;
515 exception
= EXCP_TLBL
;
519 /* Raise exception */
520 if (!(env
->hflags
& MIPS_HFLAG_DM
)) {
521 env
->CP0_BadVAddr
= address
;
523 env
->CP0_Context
= (env
->CP0_Context
& ~0x007fffff) |
524 ((address
>> 9) & 0x007ffff0);
525 env
->CP0_EntryHi
= (env
->CP0_EntryHi
& env
->CP0_EntryHi_ASID_mask
) |
526 (env
->CP0_EntryHi
& (1 << CP0EnHi_EHINV
)) |
527 (address
& (TARGET_PAGE_MASK
<< 1));
528 #if defined(TARGET_MIPS64)
529 env
->CP0_EntryHi
&= env
->SEGMask
;
531 (env
->CP0_XContext
& ((~0ULL) << (env
->SEGBITS
- 7))) | /* PTEBase */
532 (extract64(address
, 62, 2) << (env
->SEGBITS
- 9)) | /* R */
533 (extract64(address
, 13, env
->SEGBITS
- 13) << 4); /* BadVPN2 */
535 cs
->exception_index
= exception
;
536 env
->error_code
= error_code
;
539 #if !defined(CONFIG_USER_ONLY)
540 hwaddr
mips_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
542 MIPSCPU
*cpu
= MIPS_CPU(cs
);
543 CPUMIPSState
*env
= &cpu
->env
;
547 if (get_physical_address(env
, &phys_addr
, &prot
, addr
, 0, ACCESS_INT
,
548 cpu_mmu_index(env
, false)) != 0) {
555 #if !defined(CONFIG_USER_ONLY)
556 #if !defined(TARGET_MIPS64)
559 * Perform hardware page table walk
561 * Memory accesses are performed using the KERNEL privilege level.
562 * Synchronous exceptions detected on memory accesses cause a silent exit
563 * from page table walking, resulting in a TLB or XTLB Refill exception.
565 * Implementations are not required to support page table walk memory
566 * accesses from mapped memory regions. When an unsupported access is
567 * attempted, a silent exit is taken, resulting in a TLB or XTLB Refill
570 * Note that if an exception is caused by AddressTranslation or LoadMemory
571 * functions, the exception is not taken, a silent exit is taken,
572 * resulting in a TLB or XTLB Refill exception.
575 static bool get_pte(CPUMIPSState
*env
, uint64_t vaddr
, int entry_size
,
578 if ((vaddr
& ((entry_size
>> 3) - 1)) != 0) {
581 if (entry_size
== 64) {
582 *pte
= cpu_ldq_code(env
, vaddr
);
584 *pte
= cpu_ldl_code(env
, vaddr
);
589 static uint64_t get_tlb_entry_layout(CPUMIPSState
*env
, uint64_t entry
,
590 int entry_size
, int ptei
)
592 uint64_t result
= entry
;
594 if (ptei
> entry_size
) {
597 result
>>= (ptei
- 2);
600 result
|= rixi
<< CP0EnLo_XI
;
604 static int walk_directory(CPUMIPSState
*env
, uint64_t *vaddr
,
605 int directory_index
, bool *huge_page
, bool *hgpg_directory_hit
,
606 uint64_t *pw_entrylo0
, uint64_t *pw_entrylo1
)
608 int dph
= (env
->CP0_PWCtl
>> CP0PC_DPH
) & 0x1;
609 int psn
= (env
->CP0_PWCtl
>> CP0PC_PSN
) & 0x3F;
610 int hugepg
= (env
->CP0_PWCtl
>> CP0PC_HUGEPG
) & 0x1;
611 int pf_ptew
= (env
->CP0_PWField
>> CP0PF_PTEW
) & 0x3F;
612 int ptew
= (env
->CP0_PWSize
>> CP0PS_PTEW
) & 0x3F;
613 int native_shift
= (((env
->CP0_PWSize
>> CP0PS_PS
) & 1) == 0) ? 2 : 3;
614 int directory_shift
= (ptew
> 1) ? -1 :
615 (hugepg
&& (ptew
== 1)) ? native_shift
+ 1 : native_shift
;
616 int leaf_shift
= (ptew
> 1) ? -1 :
617 (ptew
== 1) ? native_shift
+ 1 : native_shift
;
618 uint32_t direntry_size
= 1 << (directory_shift
+ 3);
619 uint32_t leafentry_size
= 1 << (leaf_shift
+ 3);
626 if (get_physical_address(env
, &paddr
, &prot
, *vaddr
, MMU_DATA_LOAD
,
627 ACCESS_INT
, cpu_mmu_index(env
, false)) !=
629 /* wrong base address */
632 if (!get_pte(env
, *vaddr
, direntry_size
, &entry
)) {
636 if ((entry
& (1 << psn
)) && hugepg
) {
638 *hgpg_directory_hit
= true;
639 entry
= get_tlb_entry_layout(env
, entry
, leafentry_size
, pf_ptew
);
640 w
= directory_index
- 1;
641 if (directory_index
& 0x1) {
642 /* Generate adjacent page from same PTE for odd TLB page */
644 *pw_entrylo0
= entry
& ~lsb
; /* even page */
645 *pw_entrylo1
= entry
| lsb
; /* odd page */
647 int oddpagebit
= 1 << leaf_shift
;
648 uint64_t vaddr2
= *vaddr
^ oddpagebit
;
649 if (*vaddr
& oddpagebit
) {
650 *pw_entrylo1
= entry
;
652 *pw_entrylo0
= entry
;
654 if (get_physical_address(env
, &paddr
, &prot
, vaddr2
, MMU_DATA_LOAD
,
655 ACCESS_INT
, cpu_mmu_index(env
, false)) !=
659 if (!get_pte(env
, vaddr2
, leafentry_size
, &entry
)) {
662 entry
= get_tlb_entry_layout(env
, entry
, leafentry_size
, pf_ptew
);
663 if (*vaddr
& oddpagebit
) {
664 *pw_entrylo0
= entry
;
666 *pw_entrylo1
= entry
;
678 static bool page_table_walk_refill(CPUMIPSState
*env
, vaddr address
, int rw
,
681 int gdw
= (env
->CP0_PWSize
>> CP0PS_GDW
) & 0x3F;
682 int udw
= (env
->CP0_PWSize
>> CP0PS_UDW
) & 0x3F;
683 int mdw
= (env
->CP0_PWSize
>> CP0PS_MDW
) & 0x3F;
684 int ptw
= (env
->CP0_PWSize
>> CP0PS_PTW
) & 0x3F;
685 int ptew
= (env
->CP0_PWSize
>> CP0PS_PTEW
) & 0x3F;
688 bool huge_page
= false;
689 bool hgpg_bdhit
= false;
690 bool hgpg_gdhit
= false;
691 bool hgpg_udhit
= false;
692 bool hgpg_mdhit
= false;
694 int32_t pw_pagemask
= 0;
695 target_ulong pw_entryhi
= 0;
696 uint64_t pw_entrylo0
= 0;
697 uint64_t pw_entrylo1
= 0;
699 /* Native pointer size */
700 /*For the 32-bit architectures, this bit is fixed to 0.*/
701 int native_shift
= (((env
->CP0_PWSize
>> CP0PS_PS
) & 1) == 0) ? 2 : 3;
703 /* Indices from PWField */
704 int pf_gdw
= (env
->CP0_PWField
>> CP0PF_GDW
) & 0x3F;
705 int pf_udw
= (env
->CP0_PWField
>> CP0PF_UDW
) & 0x3F;
706 int pf_mdw
= (env
->CP0_PWField
>> CP0PF_MDW
) & 0x3F;
707 int pf_ptw
= (env
->CP0_PWField
>> CP0PF_PTW
) & 0x3F;
708 int pf_ptew
= (env
->CP0_PWField
>> CP0PF_PTEW
) & 0x3F;
710 /* Indices computed from faulting address */
711 int gindex
= (address
>> pf_gdw
) & ((1 << gdw
) - 1);
712 int uindex
= (address
>> pf_udw
) & ((1 << udw
) - 1);
713 int mindex
= (address
>> pf_mdw
) & ((1 << mdw
) - 1);
714 int ptindex
= (address
>> pf_ptw
) & ((1 << ptw
) - 1);
716 /* Other HTW configs */
717 int hugepg
= (env
->CP0_PWCtl
>> CP0PC_HUGEPG
) & 0x1;
719 /* HTW Shift values (depend on entry size) */
720 int directory_shift
= (ptew
> 1) ? -1 :
721 (hugepg
&& (ptew
== 1)) ? native_shift
+ 1 : native_shift
;
722 int leaf_shift
= (ptew
> 1) ? -1 :
723 (ptew
== 1) ? native_shift
+ 1 : native_shift
;
725 /* Offsets into tables */
726 int goffset
= gindex
<< directory_shift
;
727 int uoffset
= uindex
<< directory_shift
;
728 int moffset
= mindex
<< directory_shift
;
729 int ptoffset0
= (ptindex
>> 1) << (leaf_shift
+ 1);
730 int ptoffset1
= ptoffset0
| (1 << (leaf_shift
));
732 uint32_t leafentry_size
= 1 << (leaf_shift
+ 3);
734 /* Starting address - Page Table Base */
735 uint64_t vaddr
= env
->CP0_PWBase
;
742 if (!(env
->CP0_Config3
& (1 << CP0C3_PW
))) {
743 /* walker is unimplemented */
746 if (!(env
->CP0_PWCtl
& (1 << CP0PC_PWEN
))) {
747 /* walker is disabled */
750 if (!(gdw
> 0 || udw
> 0 || mdw
> 0)) {
751 /* no structure to walk */
754 if ((directory_shift
== -1) || (leaf_shift
== -1)) {
758 /* Global Directory */
761 switch (walk_directory(env
, &vaddr
, pf_gdw
, &huge_page
, &hgpg_gdhit
,
762 &pw_entrylo0
, &pw_entrylo1
))
774 /* Upper directory */
777 switch (walk_directory(env
, &vaddr
, pf_udw
, &huge_page
, &hgpg_udhit
,
778 &pw_entrylo0
, &pw_entrylo1
))
790 /* Middle directory */
793 switch (walk_directory(env
, &vaddr
, pf_mdw
, &huge_page
, &hgpg_mdhit
,
794 &pw_entrylo0
, &pw_entrylo1
))
806 /* Leaf Level Page Table - First half of PTE pair */
808 if (get_physical_address(env
, &paddr
, &prot
, vaddr
, MMU_DATA_LOAD
,
809 ACCESS_INT
, cpu_mmu_index(env
, false)) !=
813 if (!get_pte(env
, vaddr
, leafentry_size
, &dir_entry
)) {
816 dir_entry
= get_tlb_entry_layout(env
, dir_entry
, leafentry_size
, pf_ptew
);
817 pw_entrylo0
= dir_entry
;
819 /* Leaf Level Page Table - Second half of PTE pair */
821 if (get_physical_address(env
, &paddr
, &prot
, vaddr
, MMU_DATA_LOAD
,
822 ACCESS_INT
, cpu_mmu_index(env
, false)) !=
826 if (!get_pte(env
, vaddr
, leafentry_size
, &dir_entry
)) {
829 dir_entry
= get_tlb_entry_layout(env
, dir_entry
, leafentry_size
, pf_ptew
);
830 pw_entrylo1
= dir_entry
;
834 m
= (1 << pf_ptw
) - 1;
837 switch (hgpg_bdhit
<< 3 | hgpg_gdhit
<< 2 | hgpg_udhit
<< 1 |
841 m
= (1 << pf_gdw
) - 1;
847 m
= (1 << pf_udw
) - 1;
853 m
= (1 << pf_mdw
) - 1;
860 pw_pagemask
= m
>> TARGET_PAGE_BITS_MIN
;
861 update_pagemask(env
, pw_pagemask
<< CP0PM_MASK
, &pw_pagemask
);
862 pw_entryhi
= (address
& ~0x1fff) | (env
->CP0_EntryHi
& 0xFF);
864 target_ulong tmp_entryhi
= env
->CP0_EntryHi
;
865 int32_t tmp_pagemask
= env
->CP0_PageMask
;
866 uint64_t tmp_entrylo0
= env
->CP0_EntryLo0
;
867 uint64_t tmp_entrylo1
= env
->CP0_EntryLo1
;
869 env
->CP0_EntryHi
= pw_entryhi
;
870 env
->CP0_PageMask
= pw_pagemask
;
871 env
->CP0_EntryLo0
= pw_entrylo0
;
872 env
->CP0_EntryLo1
= pw_entrylo1
;
875 * The hardware page walker inserts a page into the TLB in a manner
876 * identical to a TLBWR instruction as executed by the software refill
879 r4k_helper_tlbwr(env
);
881 env
->CP0_EntryHi
= tmp_entryhi
;
882 env
->CP0_PageMask
= tmp_pagemask
;
883 env
->CP0_EntryLo0
= tmp_entrylo0
;
884 env
->CP0_EntryLo1
= tmp_entrylo1
;
891 bool mips_cpu_tlb_fill(CPUState
*cs
, vaddr address
, int size
,
892 MMUAccessType access_type
, int mmu_idx
,
893 bool probe
, uintptr_t retaddr
)
895 MIPSCPU
*cpu
= MIPS_CPU(cs
);
896 CPUMIPSState
*env
= &cpu
->env
;
897 #if !defined(CONFIG_USER_ONLY)
900 int mips_access_type
;
902 int ret
= TLBRET_BADADDR
;
905 #if !defined(CONFIG_USER_ONLY)
906 /* XXX: put correct access by using cpu_restore_state() correctly */
907 mips_access_type
= ACCESS_INT
;
908 ret
= get_physical_address(env
, &physical
, &prot
, address
,
909 access_type
, mips_access_type
, mmu_idx
);
912 qemu_log_mask(CPU_LOG_MMU
,
913 "%s address=%" VADDR_PRIx
" physical " TARGET_FMT_plx
914 " prot %d\n", __func__
, address
, physical
, prot
);
917 qemu_log_mask(CPU_LOG_MMU
,
918 "%s address=%" VADDR_PRIx
" ret %d\n", __func__
, address
,
922 if (ret
== TLBRET_MATCH
) {
923 tlb_set_page(cs
, address
& TARGET_PAGE_MASK
,
924 physical
& TARGET_PAGE_MASK
, prot
,
925 mmu_idx
, TARGET_PAGE_SIZE
);
928 #if !defined(TARGET_MIPS64)
929 if ((ret
== TLBRET_NOMATCH
) && (env
->tlb
->nb_tlb
> 1)) {
931 * Memory reads during hardware page table walking are performed
932 * as if they were kernel-mode load instructions.
934 int mode
= (env
->hflags
& MIPS_HFLAG_KSU
);
936 env
->hflags
&= ~MIPS_HFLAG_KSU
;
937 ret_walker
= page_table_walk_refill(env
, address
, access_type
, mmu_idx
);
940 ret
= get_physical_address(env
, &physical
, &prot
, address
,
941 access_type
, mips_access_type
, mmu_idx
);
942 if (ret
== TLBRET_MATCH
) {
943 tlb_set_page(cs
, address
& TARGET_PAGE_MASK
,
944 physical
& TARGET_PAGE_MASK
, prot
,
945 mmu_idx
, TARGET_PAGE_SIZE
);
956 raise_mmu_exception(env
, address
, access_type
, ret
);
957 do_raise_exception_err(env
, cs
->exception_index
, env
->error_code
, retaddr
);
960 #ifndef CONFIG_USER_ONLY
961 hwaddr
cpu_mips_translate_address(CPUMIPSState
*env
, target_ulong address
,
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
);
980 #endif /* !CONFIG_USER_ONLY */
982 static const char * const excp_names
[EXCP_LAST
+ 1] = {
983 [EXCP_RESET
] = "reset",
984 [EXCP_SRESET
] = "soft reset",
985 [EXCP_DSS
] = "debug single step",
986 [EXCP_DINT
] = "debug interrupt",
987 [EXCP_NMI
] = "non-maskable interrupt",
988 [EXCP_MCHECK
] = "machine check",
989 [EXCP_EXT_INTERRUPT
] = "interrupt",
990 [EXCP_DFWATCH
] = "deferred watchpoint",
991 [EXCP_DIB
] = "debug instruction breakpoint",
992 [EXCP_IWATCH
] = "instruction fetch watchpoint",
993 [EXCP_AdEL
] = "address error load",
994 [EXCP_AdES
] = "address error store",
995 [EXCP_TLBF
] = "TLB refill",
996 [EXCP_IBE
] = "instruction bus error",
997 [EXCP_DBp
] = "debug breakpoint",
998 [EXCP_SYSCALL
] = "syscall",
999 [EXCP_BREAK
] = "break",
1000 [EXCP_CpU
] = "coprocessor unusable",
1001 [EXCP_RI
] = "reserved instruction",
1002 [EXCP_OVERFLOW
] = "arithmetic overflow",
1003 [EXCP_TRAP
] = "trap",
1004 [EXCP_FPE
] = "floating point",
1005 [EXCP_DDBS
] = "debug data break store",
1006 [EXCP_DWATCH
] = "data watchpoint",
1007 [EXCP_LTLBL
] = "TLB modify",
1008 [EXCP_TLBL
] = "TLB load",
1009 [EXCP_TLBS
] = "TLB store",
1010 [EXCP_DBE
] = "data bus error",
1011 [EXCP_DDBL
] = "debug data break load",
1012 [EXCP_THREAD
] = "thread",
1013 [EXCP_MDMX
] = "MDMX",
1014 [EXCP_C2E
] = "precise coprocessor 2",
1015 [EXCP_CACHE
] = "cache error",
1016 [EXCP_TLBXI
] = "TLB execute-inhibit",
1017 [EXCP_TLBRI
] = "TLB read-inhibit",
1018 [EXCP_MSADIS
] = "MSA disabled",
1019 [EXCP_MSAFPE
] = "MSA floating point",
1022 static const char *mips_exception_name(int32_t exception
)
1024 if (exception
< 0 || exception
> EXCP_LAST
) {
1027 return excp_names
[exception
];
1030 target_ulong
exception_resume_pc(CPUMIPSState
*env
)
1032 target_ulong bad_pc
;
1033 target_ulong isa_mode
;
1035 isa_mode
= !!(env
->hflags
& MIPS_HFLAG_M16
);
1036 bad_pc
= env
->active_tc
.PC
| isa_mode
;
1037 if (env
->hflags
& MIPS_HFLAG_BMASK
) {
1039 * If the exception was raised from a delay slot, come back to
1042 bad_pc
-= (env
->hflags
& MIPS_HFLAG_B16
? 2 : 4);
1048 #if !defined(CONFIG_USER_ONLY)
1049 static void set_hflags_for_handler(CPUMIPSState
*env
)
1051 /* Exception handlers are entered in 32-bit mode. */
1052 env
->hflags
&= ~(MIPS_HFLAG_M16
);
1053 /* ...except that microMIPS lets you choose. */
1054 if (env
->insn_flags
& ASE_MICROMIPS
) {
1055 env
->hflags
|= (!!(env
->CP0_Config3
&
1056 (1 << CP0C3_ISA_ON_EXC
))
1057 << MIPS_HFLAG_M16_SHIFT
);
1061 static inline void set_badinstr_registers(CPUMIPSState
*env
)
1063 if (env
->insn_flags
& ISA_NANOMIPS32
) {
1064 if (env
->CP0_Config3
& (1 << CP0C3_BI
)) {
1065 uint32_t instr
= (cpu_lduw_code(env
, env
->active_tc
.PC
)) << 16;
1066 if ((instr
& 0x10000000) == 0) {
1067 instr
|= cpu_lduw_code(env
, env
->active_tc
.PC
+ 2);
1069 env
->CP0_BadInstr
= instr
;
1071 if ((instr
& 0xFC000000) == 0x60000000) {
1072 instr
= cpu_lduw_code(env
, env
->active_tc
.PC
+ 4) << 16;
1073 env
->CP0_BadInstrX
= instr
;
1079 if (env
->hflags
& MIPS_HFLAG_M16
) {
1080 /* TODO: add BadInstr support for microMIPS */
1083 if (env
->CP0_Config3
& (1 << CP0C3_BI
)) {
1084 env
->CP0_BadInstr
= cpu_ldl_code(env
, env
->active_tc
.PC
);
1086 if ((env
->CP0_Config3
& (1 << CP0C3_BP
)) &&
1087 (env
->hflags
& MIPS_HFLAG_BMASK
)) {
1088 env
->CP0_BadInstrP
= cpu_ldl_code(env
, env
->active_tc
.PC
- 4);
1093 void mips_cpu_do_interrupt(CPUState
*cs
)
1095 #if !defined(CONFIG_USER_ONLY)
1096 MIPSCPU
*cpu
= MIPS_CPU(cs
);
1097 CPUMIPSState
*env
= &cpu
->env
;
1098 bool update_badinstr
= 0;
1099 target_ulong offset
;
1102 if (qemu_loglevel_mask(CPU_LOG_INT
)
1103 && cs
->exception_index
!= EXCP_EXT_INTERRUPT
) {
1104 qemu_log("%s enter: PC " TARGET_FMT_lx
" EPC " TARGET_FMT_lx
1106 __func__
, env
->active_tc
.PC
, env
->CP0_EPC
,
1107 mips_exception_name(cs
->exception_index
));
1109 if (cs
->exception_index
== EXCP_EXT_INTERRUPT
&&
1110 (env
->hflags
& MIPS_HFLAG_DM
)) {
1111 cs
->exception_index
= EXCP_DINT
;
1114 switch (cs
->exception_index
) {
1116 env
->CP0_Debug
|= 1 << CP0DB_DSS
;
1118 * Debug single step cannot be raised inside a delay slot and
1119 * resume will always occur on the next instruction
1120 * (but we assume the pc has always been updated during
1121 * code translation).
1123 env
->CP0_DEPC
= env
->active_tc
.PC
| !!(env
->hflags
& MIPS_HFLAG_M16
);
1124 goto enter_debug_mode
;
1126 env
->CP0_Debug
|= 1 << CP0DB_DINT
;
1129 env
->CP0_Debug
|= 1 << CP0DB_DIB
;
1132 env
->CP0_Debug
|= 1 << CP0DB_DBp
;
1133 /* Setup DExcCode - SDBBP instruction */
1134 env
->CP0_Debug
= (env
->CP0_Debug
& ~(0x1fULL
<< CP0DB_DEC
)) |
1138 env
->CP0_Debug
|= 1 << CP0DB_DDBS
;
1141 env
->CP0_Debug
|= 1 << CP0DB_DDBL
;
1143 env
->CP0_DEPC
= exception_resume_pc(env
);
1144 env
->hflags
&= ~MIPS_HFLAG_BMASK
;
1146 if (env
->insn_flags
& ISA_MIPS3
) {
1147 env
->hflags
|= MIPS_HFLAG_64
;
1148 if (!(env
->insn_flags
& ISA_MIPS64R6
) ||
1149 env
->CP0_Status
& (1 << CP0St_KX
)) {
1150 env
->hflags
&= ~MIPS_HFLAG_AWRAP
;
1153 env
->hflags
|= MIPS_HFLAG_DM
| MIPS_HFLAG_CP0
;
1154 env
->hflags
&= ~(MIPS_HFLAG_KSU
);
1155 /* EJTAG probe trap enable is not implemented... */
1156 if (!(env
->CP0_Status
& (1 << CP0St_EXL
))) {
1157 env
->CP0_Cause
&= ~(1U << CP0Ca_BD
);
1159 env
->active_tc
.PC
= env
->exception_base
+ 0x480;
1160 set_hflags_for_handler(env
);
1163 cpu_reset(CPU(cpu
));
1166 env
->CP0_Status
|= (1 << CP0St_SR
);
1167 memset(env
->CP0_WatchLo
, 0, sizeof(env
->CP0_WatchLo
));
1170 env
->CP0_Status
|= (1 << CP0St_NMI
);
1172 env
->CP0_ErrorEPC
= exception_resume_pc(env
);
1173 env
->hflags
&= ~MIPS_HFLAG_BMASK
;
1174 env
->CP0_Status
|= (1 << CP0St_ERL
) | (1 << CP0St_BEV
);
1175 if (env
->insn_flags
& ISA_MIPS3
) {
1176 env
->hflags
|= MIPS_HFLAG_64
;
1177 if (!(env
->insn_flags
& ISA_MIPS64R6
) ||
1178 env
->CP0_Status
& (1 << CP0St_KX
)) {
1179 env
->hflags
&= ~MIPS_HFLAG_AWRAP
;
1182 env
->hflags
|= MIPS_HFLAG_CP0
;
1183 env
->hflags
&= ~(MIPS_HFLAG_KSU
);
1184 if (!(env
->CP0_Status
& (1 << CP0St_EXL
))) {
1185 env
->CP0_Cause
&= ~(1U << CP0Ca_BD
);
1187 env
->active_tc
.PC
= env
->exception_base
;
1188 set_hflags_for_handler(env
);
1190 case EXCP_EXT_INTERRUPT
:
1192 if (env
->CP0_Cause
& (1 << CP0Ca_IV
)) {
1193 uint32_t spacing
= (env
->CP0_IntCtl
>> CP0IntCtl_VS
) & 0x1f;
1195 if ((env
->CP0_Status
& (1 << CP0St_BEV
)) || spacing
== 0) {
1198 uint32_t vector
= 0;
1199 uint32_t pending
= (env
->CP0_Cause
& CP0Ca_IP_mask
) >> CP0Ca_IP
;
1201 if (env
->CP0_Config3
& (1 << CP0C3_VEIC
)) {
1203 * For VEIC mode, the external interrupt controller feeds
1204 * the vector through the CP0Cause IP lines.
1209 * Vectored Interrupts
1210 * Mask with Status.IM7-IM0 to get enabled interrupts.
1212 pending
&= (env
->CP0_Status
>> CP0St_IM
) & 0xff;
1213 /* Find the highest-priority interrupt. */
1214 while (pending
>>= 1) {
1218 offset
= 0x200 + (vector
* (spacing
<< 5));
1224 update_badinstr
= !(env
->error_code
& EXCP_INST_NOTAVAIL
);
1228 update_badinstr
= !(env
->error_code
& EXCP_INST_NOTAVAIL
);
1229 if ((env
->error_code
& EXCP_TLB_NOMATCH
) &&
1230 !(env
->CP0_Status
& (1 << CP0St_EXL
))) {
1231 #if defined(TARGET_MIPS64)
1232 int R
= env
->CP0_BadVAddr
>> 62;
1233 int UX
= (env
->CP0_Status
& (1 << CP0St_UX
)) != 0;
1234 int KX
= (env
->CP0_Status
& (1 << CP0St_KX
)) != 0;
1236 if ((R
!= 0 || UX
) && (R
!= 3 || KX
) &&
1237 (!(env
->insn_flags
& (INSN_LOONGSON2E
| INSN_LOONGSON2F
)))) {
1242 #if defined(TARGET_MIPS64)
1249 update_badinstr
= 1;
1250 if ((env
->error_code
& EXCP_TLB_NOMATCH
) &&
1251 !(env
->CP0_Status
& (1 << CP0St_EXL
))) {
1252 #if defined(TARGET_MIPS64)
1253 int R
= env
->CP0_BadVAddr
>> 62;
1254 int UX
= (env
->CP0_Status
& (1 << CP0St_UX
)) != 0;
1255 int KX
= (env
->CP0_Status
& (1 << CP0St_KX
)) != 0;
1257 if ((R
!= 0 || UX
) && (R
!= 3 || KX
) &&
1258 (!(env
->insn_flags
& (INSN_LOONGSON2E
| INSN_LOONGSON2F
)))) {
1263 #if defined(TARGET_MIPS64)
1270 update_badinstr
= !(env
->error_code
& EXCP_INST_NOTAVAIL
);
1274 update_badinstr
= 1;
1284 update_badinstr
= 1;
1288 update_badinstr
= 1;
1292 update_badinstr
= 1;
1296 update_badinstr
= 1;
1297 env
->CP0_Cause
= (env
->CP0_Cause
& ~(0x3 << CP0Ca_CE
)) |
1298 (env
->error_code
<< CP0Ca_CE
);
1302 update_badinstr
= 1;
1306 update_badinstr
= 1;
1310 update_badinstr
= 1;
1314 update_badinstr
= 1;
1321 update_badinstr
= 1;
1328 update_badinstr
= 1;
1335 /* XXX: TODO: manage deferred watch exceptions */
1350 if (!(env
->CP0_Status
& (1 << CP0St_EXL
))) {
1351 env
->CP0_EPC
= exception_resume_pc(env
);
1352 if (update_badinstr
) {
1353 set_badinstr_registers(env
);
1355 if (env
->hflags
& MIPS_HFLAG_BMASK
) {
1356 env
->CP0_Cause
|= (1U << CP0Ca_BD
);
1358 env
->CP0_Cause
&= ~(1U << CP0Ca_BD
);
1360 env
->CP0_Status
|= (1 << CP0St_EXL
);
1361 if (env
->insn_flags
& ISA_MIPS3
) {
1362 env
->hflags
|= MIPS_HFLAG_64
;
1363 if (!(env
->insn_flags
& ISA_MIPS64R6
) ||
1364 env
->CP0_Status
& (1 << CP0St_KX
)) {
1365 env
->hflags
&= ~MIPS_HFLAG_AWRAP
;
1368 env
->hflags
|= MIPS_HFLAG_CP0
;
1369 env
->hflags
&= ~(MIPS_HFLAG_KSU
);
1371 env
->hflags
&= ~MIPS_HFLAG_BMASK
;
1372 if (env
->CP0_Status
& (1 << CP0St_BEV
)) {
1373 env
->active_tc
.PC
= env
->exception_base
+ 0x200;
1374 } else if (cause
== 30 && !(env
->CP0_Config3
& (1 << CP0C3_SC
) &&
1375 env
->CP0_Config5
& (1 << CP0C5_CV
))) {
1376 /* Force KSeg1 for cache errors */
1377 env
->active_tc
.PC
= KSEG1_BASE
| (env
->CP0_EBase
& 0x1FFFF000);
1379 env
->active_tc
.PC
= env
->CP0_EBase
& ~0xfff;
1382 env
->active_tc
.PC
+= offset
;
1383 set_hflags_for_handler(env
);
1384 env
->CP0_Cause
= (env
->CP0_Cause
& ~(0x1f << CP0Ca_EC
)) |
1385 (cause
<< CP0Ca_EC
);
1390 if (qemu_loglevel_mask(CPU_LOG_INT
)
1391 && cs
->exception_index
!= EXCP_EXT_INTERRUPT
) {
1392 qemu_log("%s: PC " TARGET_FMT_lx
" EPC " TARGET_FMT_lx
" cause %d\n"
1393 " S %08x C %08x A " TARGET_FMT_lx
" D " TARGET_FMT_lx
"\n",
1394 __func__
, env
->active_tc
.PC
, env
->CP0_EPC
, cause
,
1395 env
->CP0_Status
, env
->CP0_Cause
, env
->CP0_BadVAddr
,
1399 cs
->exception_index
= EXCP_NONE
;
1402 bool mips_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
)
1404 if (interrupt_request
& CPU_INTERRUPT_HARD
) {
1405 MIPSCPU
*cpu
= MIPS_CPU(cs
);
1406 CPUMIPSState
*env
= &cpu
->env
;
1408 if (cpu_mips_hw_interrupts_enabled(env
) &&
1409 cpu_mips_hw_interrupts_pending(env
)) {
1411 cs
->exception_index
= EXCP_EXT_INTERRUPT
;
1412 env
->error_code
= 0;
1413 mips_cpu_do_interrupt(cs
);
1420 #if !defined(CONFIG_USER_ONLY)
1421 void r4k_invalidate_tlb(CPUMIPSState
*env
, int idx
, int use_extra
)
1423 CPUState
*cs
= env_cpu(env
);
1427 uint16_t ASID
= env
->CP0_EntryHi
& env
->CP0_EntryHi_ASID_mask
;
1428 uint32_t MMID
= env
->CP0_MemoryMapID
;
1429 bool mi
= !!((env
->CP0_Config5
>> CP0C5_MI
) & 1);
1433 MMID
= mi
? MMID
: (uint32_t) ASID
;
1435 tlb
= &env
->tlb
->mmu
.r4k
.tlb
[idx
];
1437 * The qemu TLB is flushed when the ASID/MMID changes, so no need to
1438 * flush these entries again.
1440 tlb_mmid
= mi
? tlb
->MMID
: (uint32_t) tlb
->ASID
;
1441 if (tlb
->G
== 0 && tlb_mmid
!= MMID
) {
1445 if (use_extra
&& env
->tlb
->tlb_in_use
< MIPS_TLB_MAX
) {
1447 * For tlbwr, we can shadow the discarded entry into
1448 * a new (fake) TLB entry, as long as the guest can not
1449 * tell that it's there.
1451 env
->tlb
->mmu
.r4k
.tlb
[env
->tlb
->tlb_in_use
] = *tlb
;
1452 env
->tlb
->tlb_in_use
++;
1456 /* 1k pages are not supported. */
1457 mask
= tlb
->PageMask
| ~(TARGET_PAGE_MASK
<< 1);
1459 addr
= tlb
->VPN
& ~mask
;
1460 #if defined(TARGET_MIPS64)
1461 if (addr
>= (0xFFFFFFFF80000000ULL
& env
->SEGMask
)) {
1462 addr
|= 0x3FFFFF0000000000ULL
;
1465 end
= addr
| (mask
>> 1);
1466 while (addr
< end
) {
1467 tlb_flush_page(cs
, addr
);
1468 addr
+= TARGET_PAGE_SIZE
;
1472 addr
= (tlb
->VPN
& ~mask
) | ((mask
>> 1) + 1);
1473 #if defined(TARGET_MIPS64)
1474 if (addr
>= (0xFFFFFFFF80000000ULL
& env
->SEGMask
)) {
1475 addr
|= 0x3FFFFF0000000000ULL
;
1479 while (addr
- 1 < end
) {
1480 tlb_flush_page(cs
, addr
);
1481 addr
+= TARGET_PAGE_SIZE
;
1487 void QEMU_NORETURN
do_raise_exception_err(CPUMIPSState
*env
,
1492 CPUState
*cs
= env_cpu(env
);
1494 qemu_log_mask(CPU_LOG_INT
, "%s: %d (%s) %d\n",
1495 __func__
, exception
, mips_exception_name(exception
),
1497 cs
->exception_index
= exception
;
1498 env
->error_code
= error_code
;
1500 cpu_loop_exit_restore(cs
, pc
);