4 * Copyright (c) 2005 Samuel Tardieu
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "hw/sh_intc.h"
32 #if defined(CONFIG_USER_ONLY)
34 void do_interrupt (CPUState
*env
)
36 env
->exception_index
= -1;
39 int cpu_sh4_handle_mmu_fault(CPUState
* env
, target_ulong address
, int rw
,
40 int mmu_idx
, int is_softmmu
)
43 env
->exception_index
= 0;
46 env
->exception_index
= 0x0a0;
49 env
->exception_index
= 0x0c0;
52 env
->exception_index
= 0x0a0;
58 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
* env
, target_ulong addr
)
63 #else /* !CONFIG_USER_ONLY */
66 #define MMU_ITLB_MISS (-1)
67 #define MMU_ITLB_MULTIPLE (-2)
68 #define MMU_ITLB_VIOLATION (-3)
69 #define MMU_DTLB_MISS_READ (-4)
70 #define MMU_DTLB_MISS_WRITE (-5)
71 #define MMU_DTLB_INITIAL_WRITE (-6)
72 #define MMU_DTLB_VIOLATION_READ (-7)
73 #define MMU_DTLB_VIOLATION_WRITE (-8)
74 #define MMU_DTLB_MULTIPLE (-9)
75 #define MMU_DTLB_MISS (-10)
76 #define MMU_IADDR_ERROR (-11)
77 #define MMU_DADDR_ERROR_READ (-12)
78 #define MMU_DADDR_ERROR_WRITE (-13)
80 void do_interrupt(CPUState
* env
)
82 int do_irq
= env
->interrupt_request
& CPU_INTERRUPT_HARD
;
83 int do_exp
, irq_vector
= env
->exception_index
;
85 /* prioritize exceptions over interrupts */
87 do_exp
= env
->exception_index
!= -1;
88 do_irq
= do_irq
&& (env
->exception_index
== -1);
90 if (env
->sr
& SR_BL
) {
91 if (do_exp
&& env
->exception_index
!= 0x1e0) {
92 env
->exception_index
= 0x000; /* masked exception -> reset */
94 if (do_irq
&& !env
->intr_at_halt
) {
97 env
->intr_at_halt
= 0;
101 irq_vector
= sh_intc_get_pending_vector(env
->intc_handle
,
102 (env
->sr
>> 4) & 0xf);
103 if (irq_vector
== -1) {
108 if (loglevel
& CPU_LOG_INT
) {
110 switch (env
->exception_index
) {
112 expname
= "addr_error";
115 expname
= "tlb_miss";
118 expname
= "tlb_violation";
121 expname
= "illegal_instruction";
124 expname
= "slot_illegal_instruction";
127 expname
= "fpu_disable";
130 expname
= "slot_fpu";
133 expname
= "data_write";
136 expname
= "dtlb_miss_write";
139 expname
= "dtlb_violation_write";
142 expname
= "fpu_exception";
145 expname
= "initial_page_write";
151 expname
= do_irq
? "interrupt" : "???";
154 fprintf(logfile
, "exception 0x%03x [%s] raised\n",
155 irq_vector
, expname
);
156 cpu_dump_state(env
, logfile
, fprintf
, 0);
161 env
->sgr
= env
->gregs
[15];
162 env
->sr
|= SR_BL
| SR_MD
| SR_RB
;
164 if (env
->flags
& (DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
)) {
165 /* Branch instruction should be executed again before delay slot. */
167 /* Clear flags for exception/interrupt routine. */
168 env
->flags
&= ~(DELAY_SLOT
| DELAY_SLOT_CONDITIONAL
| DELAY_SLOT_TRUE
);
170 if (env
->flags
& DELAY_SLOT_CLEARME
)
174 env
->expevt
= env
->exception_index
;
175 switch (env
->exception_index
) {
180 env
->sr
|= 0xf << 4; /* IMASK */
181 env
->pc
= 0xa0000000;
185 env
->pc
= env
->vbr
+ 0x400;
188 env
->spc
+= 2; /* special case for TRAPA */
191 env
->pc
= env
->vbr
+ 0x100;
198 env
->intevt
= irq_vector
;
199 env
->pc
= env
->vbr
+ 0x600;
204 static void update_itlb_use(CPUState
* env
, int itlbnb
)
206 uint8_t or_mask
= 0, and_mask
= (uint8_t) - 1;
225 env
->mmucr
&= (and_mask
<< 24) | 0x00ffffff;
226 env
->mmucr
|= (or_mask
<< 24);
229 static int itlb_replacement(CPUState
* env
)
231 if ((env
->mmucr
& 0xe0000000) == 0xe0000000)
233 if ((env
->mmucr
& 0x98000000) == 0x18000000)
235 if ((env
->mmucr
& 0x54000000) == 0x04000000)
237 if ((env
->mmucr
& 0x2c000000) == 0x00000000)
242 /* Find the corresponding entry in the right TLB
243 Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE
245 static int find_tlb_entry(CPUState
* env
, target_ulong address
,
246 tlb_t
* entries
, uint8_t nbtlb
, int use_asid
)
248 int match
= MMU_DTLB_MISS
;
253 asid
= env
->pteh
& 0xff;
255 for (i
= 0; i
< nbtlb
; i
++) {
257 continue; /* Invalid entry */
258 if (use_asid
&& entries
[i
].asid
!= asid
)
259 continue; /* Bad ASID */
261 switch (entries
[i
].sz
) {
263 size
= 1024; /* 1kB */
266 size
= 4 * 1024; /* 4kB */
269 size
= 64 * 1024; /* 64kB */
272 size
= 1024 * 1024; /* 1MB */
278 start
= (entries
[i
].vpn
<< 10) & ~(entries
[i
].size
- 1);
279 end
= start
+ entries
[i
].size
- 1;
280 if (address
>= start
&& address
<= end
) { /* Match */
281 if (match
!= MMU_DTLB_MISS
)
282 return MMU_DTLB_MULTIPLE
; /* Multiple match */
289 static int same_tlb_entry_exists(const tlb_t
* haystack
, uint8_t nbtlb
,
290 const tlb_t
* needle
)
293 for (i
= 0; i
< nbtlb
; i
++)
294 if (!memcmp(&haystack
[i
], needle
, sizeof(tlb_t
)))
299 static void increment_urc(CPUState
* env
)
304 urb
= ((env
->mmucr
) >> 18) & 0x3f;
305 urc
= ((env
->mmucr
) >> 10) & 0x3f;
307 if (urc
== urb
|| urc
== UTLB_SIZE
- 1)
309 env
->mmucr
= (env
->mmucr
& 0xffff03ff) | (urc
<< 10);
312 /* Find itlb entry - update itlb from utlb if necessary and asked for
313 Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
314 Update the itlb from utlb if update is not 0
316 int find_itlb_entry(CPUState
* env
, target_ulong address
,
317 int use_asid
, int update
)
321 e
= find_tlb_entry(env
, address
, env
->itlb
, ITLB_SIZE
, use_asid
);
322 if (e
== MMU_DTLB_MULTIPLE
)
323 e
= MMU_ITLB_MULTIPLE
;
324 else if (e
== MMU_DTLB_MISS
&& update
) {
325 e
= find_tlb_entry(env
, address
, env
->utlb
, UTLB_SIZE
, use_asid
);
328 n
= itlb_replacement(env
);
329 ientry
= &env
->itlb
[n
];
331 if (!same_tlb_entry_exists(env
->utlb
, UTLB_SIZE
, ientry
))
332 tlb_flush_page(env
, ientry
->vpn
<< 10);
334 *ientry
= env
->utlb
[e
];
336 } else if (e
== MMU_DTLB_MISS
)
338 } else if (e
== MMU_DTLB_MISS
)
341 update_itlb_use(env
, e
);
346 Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
347 int find_utlb_entry(CPUState
* env
, target_ulong address
, int use_asid
)
349 /* per utlb access */
353 return find_tlb_entry(env
, address
, env
->utlb
, UTLB_SIZE
, use_asid
);
356 /* Match address against MMU
357 Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
358 MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
359 MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
360 MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION,
361 MMU_IADDR_ERROR, MMU_DADDR_ERROR_READ, MMU_DADDR_ERROR_WRITE.
363 static int get_mmu_address(CPUState
* env
, target_ulong
* physical
,
364 int *prot
, target_ulong address
,
365 int rw
, int access_type
)
368 tlb_t
*matching
= NULL
;
370 use_asid
= (env
->mmucr
& MMUCR_SV
) == 0 || (env
->sr
& SR_MD
) == 0;
373 n
= find_itlb_entry(env
, address
, use_asid
, 1);
375 matching
= &env
->itlb
[n
];
376 if ((env
->sr
& SR_MD
) & !(matching
->pr
& 2))
377 n
= MMU_ITLB_VIOLATION
;
382 n
= find_utlb_entry(env
, address
, use_asid
);
384 matching
= &env
->utlb
[n
];
385 switch ((matching
->pr
<< 1) | ((env
->sr
& SR_MD
) ? 1 : 0)) {
388 n
= (rw
== 1) ? MMU_DTLB_VIOLATION_WRITE
:
389 MMU_DTLB_VIOLATION_READ
;
395 n
= MMU_DTLB_VIOLATION_WRITE
;
402 *prot
= (rw
== 1)? PAGE_WRITE
: PAGE_READ
;
405 } else if (n
== MMU_DTLB_MISS
) {
406 n
= (rw
== 1) ? MMU_DTLB_MISS_WRITE
:
411 *physical
= ((matching
->ppn
<< 10) & ~(matching
->size
- 1)) |
412 (address
& (matching
->size
- 1));
413 if ((rw
== 1) & !matching
->d
)
414 n
= MMU_DTLB_INITIAL_WRITE
;
421 int get_physical_address(CPUState
* env
, target_ulong
* physical
,
422 int *prot
, target_ulong address
,
423 int rw
, int access_type
)
425 /* P1, P2 and P4 areas do not use translation */
426 if ((address
>= 0x80000000 && address
< 0xc0000000) ||
427 address
>= 0xe0000000) {
428 if (!(env
->sr
& SR_MD
)
429 && (address
< 0xe0000000 || address
> 0xe4000000)) {
430 /* Unauthorized access in user mode (only store queues are available) */
431 fprintf(stderr
, "Unauthorized access\n");
433 return MMU_DADDR_ERROR_READ
;
435 return MMU_DADDR_ERROR_WRITE
;
437 return MMU_IADDR_ERROR
;
439 if (address
>= 0x80000000 && address
< 0xc0000000) {
440 /* Mask upper 3 bits for P1 and P2 areas */
441 *physical
= address
& 0x1fffffff;
442 } else if (address
>= 0xfc000000) {
444 * Mask upper 3 bits for control registers in P4 area,
445 * to unify access to control registers via P0-P3 area.
446 * The addresses for cache store queue, TLB address array
449 *physical
= address
& 0x1fffffff;
451 /* access to cache store queue, or TLB address array. */
454 *prot
= PAGE_READ
| PAGE_WRITE
;
458 /* If MMU is disabled, return the corresponding physical page */
459 if (!env
->mmucr
& MMUCR_AT
) {
460 *physical
= address
& 0x1FFFFFFF;
461 *prot
= PAGE_READ
| PAGE_WRITE
;
465 /* We need to resort to the MMU */
466 return get_mmu_address(env
, physical
, prot
, address
, rw
, access_type
);
469 int cpu_sh4_handle_mmu_fault(CPUState
* env
, target_ulong address
, int rw
,
470 int mmu_idx
, int is_softmmu
)
472 target_ulong physical
, page_offset
, page_size
;
473 int prot
, ret
, access_type
;
475 access_type
= ACCESS_INT
;
477 get_physical_address(env
, &physical
, &prot
, address
, rw
,
484 case MMU_DTLB_MISS_READ
:
485 env
->exception_index
= 0x040;
487 case MMU_DTLB_MULTIPLE
:
488 case MMU_ITLB_MULTIPLE
:
489 env
->exception_index
= 0x140;
491 case MMU_ITLB_VIOLATION
:
492 env
->exception_index
= 0x0a0;
494 case MMU_DTLB_MISS_WRITE
:
495 env
->exception_index
= 0x060;
497 case MMU_DTLB_INITIAL_WRITE
:
498 env
->exception_index
= 0x080;
500 case MMU_DTLB_VIOLATION_READ
:
501 env
->exception_index
= 0x0a0;
503 case MMU_DTLB_VIOLATION_WRITE
:
504 env
->exception_index
= 0x0c0;
506 case MMU_IADDR_ERROR
:
507 case MMU_DADDR_ERROR_READ
:
508 env
->exception_index
= 0x0c0;
510 case MMU_DADDR_ERROR_WRITE
:
511 env
->exception_index
= 0x100;
519 page_size
= TARGET_PAGE_SIZE
;
521 (address
- (address
& TARGET_PAGE_MASK
)) & ~(page_size
- 1);
522 address
= (address
& TARGET_PAGE_MASK
) + page_offset
;
523 physical
= (physical
& TARGET_PAGE_MASK
) + page_offset
;
525 return tlb_set_page(env
, address
, physical
, prot
, mmu_idx
, is_softmmu
);
528 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
* env
, target_ulong addr
)
530 target_ulong physical
;
533 get_physical_address(env
, &physical
, &prot
, addr
, 0, 0);
537 void cpu_load_tlb(CPUState
* env
)
539 int n
= cpu_mmucr_urc(env
->mmucr
);
540 tlb_t
* entry
= &env
->utlb
[n
];
543 /* Overwriting valid entry in utlb. */
544 target_ulong address
= entry
->vpn
<< 10;
545 if (!same_tlb_entry_exists(env
->itlb
, ITLB_SIZE
, entry
)) {
546 tlb_flush_page(env
, address
);
550 /* per utlb access cannot implemented. */
553 /* Take values into cpu status from registers. */
554 entry
->asid
= (uint8_t)cpu_pteh_asid(env
->pteh
);
555 entry
->vpn
= cpu_pteh_vpn(env
->pteh
);
556 entry
->v
= (uint8_t)cpu_ptel_v(env
->ptel
);
557 entry
->ppn
= cpu_ptel_ppn(env
->ptel
);
558 entry
->sz
= (uint8_t)cpu_ptel_sz(env
->ptel
);
561 entry
->size
= 1024; /* 1K */
564 entry
->size
= 1024 * 4; /* 4K */
567 entry
->size
= 1024 * 64; /* 64K */
570 entry
->size
= 1024 * 1024; /* 1M */
576 entry
->sh
= (uint8_t)cpu_ptel_sh(env
->ptel
);
577 entry
->c
= (uint8_t)cpu_ptel_c(env
->ptel
);
578 entry
->pr
= (uint8_t)cpu_ptel_pr(env
->ptel
);
579 entry
->d
= (uint8_t)cpu_ptel_d(env
->ptel
);
580 entry
->wt
= (uint8_t)cpu_ptel_wt(env
->ptel
);
581 entry
->sa
= (uint8_t)cpu_ptea_sa(env
->ptea
);
582 entry
->tc
= (uint8_t)cpu_ptea_tc(env
->ptea
);
585 void cpu_sh4_write_mmaped_utlb_addr(CPUSH4State
*s
, target_phys_addr_t addr
,
588 int associate
= addr
& 0x0000080;
589 uint32_t vpn
= (mem_value
& 0xfffffc00) >> 10;
590 uint8_t d
= (uint8_t)((mem_value
& 0x00000200) >> 9);
591 uint8_t v
= (uint8_t)((mem_value
& 0x00000100) >> 8);
592 uint8_t asid
= (uint8_t)(mem_value
& 0x000000ff);
596 tlb_t
* utlb_match_entry
= NULL
;
597 int needs_tlb_flush
= 0;
600 for (i
= 0; i
< UTLB_SIZE
; i
++) {
601 tlb_t
* entry
= &s
->utlb
[i
];
605 if (entry
->vpn
== vpn
&& entry
->asid
== asid
) {
606 if (utlb_match_entry
) {
607 /* Multiple TLB Exception */
608 s
->exception_index
= 0x140;
616 utlb_match_entry
= entry
;
618 increment_urc(s
); /* per utlb access */
622 for (i
= 0; i
< ITLB_SIZE
; i
++) {
623 tlb_t
* entry
= &s
->itlb
[i
];
624 if (entry
->vpn
== vpn
&& entry
->asid
== asid
) {
627 if (utlb_match_entry
)
628 *entry
= *utlb_match_entry
;
636 tlb_flush_page(s
, vpn
<< 10);
639 int index
= (addr
& 0x00003f00) >> 8;
640 tlb_t
* entry
= &s
->utlb
[index
];
642 /* Overwriting valid entry in utlb. */
643 target_ulong address
= entry
->vpn
<< 10;
644 if (!same_tlb_entry_exists(s
->itlb
, ITLB_SIZE
, entry
)) {
645 tlb_flush_page(s
, address
);