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
31 #if defined(CONFIG_USER_ONLY)
33 void do_interrupt (CPUState
*env
)
35 env
->exception_index
= -1;
38 int cpu_sh4_handle_mmu_fault(CPUState
* env
, target_ulong address
, int rw
,
39 int is_user
, int is_softmmu
)
44 env
->exception_index
= 0x0a0;
47 env
->exception_index
= 0x0c0;
50 env
->exception_index
= 0x0a0;
56 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
* env
, target_ulong addr
)
61 #else /* !CONFIG_USER_ONLY */
64 #define MMU_ITLB_MISS (-1)
65 #define MMU_ITLB_MULTIPLE (-2)
66 #define MMU_ITLB_VIOLATION (-3)
67 #define MMU_DTLB_MISS_READ (-4)
68 #define MMU_DTLB_MISS_WRITE (-5)
69 #define MMU_DTLB_INITIAL_WRITE (-6)
70 #define MMU_DTLB_VIOLATION_READ (-7)
71 #define MMU_DTLB_VIOLATION_WRITE (-8)
72 #define MMU_DTLB_MULTIPLE (-9)
73 #define MMU_DTLB_MISS (-10)
75 void do_interrupt(CPUState
* env
)
77 if (loglevel
& CPU_LOG_INT
) {
79 switch (env
->exception_index
) {
81 expname
= "addr_error";
87 expname
= "tlb_violation";
90 expname
= "illegal_instruction";
93 expname
= "slot_illegal_instruction";
96 expname
= "fpu_disable";
102 expname
= "data_write";
105 expname
= "dtlb_miss_write";
108 expname
= "dtlb_violation_write";
111 expname
= "fpu_exception";
114 expname
= "initial_page_write";
123 fprintf(logfile
, "exception 0x%03x [%s] raised\n",
124 env
->exception_index
, expname
);
125 cpu_dump_state(env
, logfile
, fprintf
, 0);
130 env
->sgr
= env
->gregs
[15];
131 env
->sr
|= SR_BL
| SR_MD
| SR_RB
;
133 env
->expevt
= env
->exception_index
& 0x7ff;
134 switch (env
->exception_index
) {
138 env
->pc
= env
->vbr
+ 0x400;
141 env
->pc
= 0xa0000000;
144 env
->pc
= env
->vbr
+ 0x100;
149 static void update_itlb_use(CPUState
* env
, int itlbnb
)
151 uint8_t or_mask
= 0, and_mask
= (uint8_t) - 1;
170 env
->mmucr
&= (and_mask
<< 24);
171 env
->mmucr
|= (or_mask
<< 24);
174 static int itlb_replacement(CPUState
* env
)
176 if ((env
->mmucr
& 0xe0000000) == 0xe0000000)
178 if ((env
->mmucr
& 0x98000000) == 0x08000000)
180 if ((env
->mmucr
& 0x54000000) == 0x04000000)
182 if ((env
->mmucr
& 0x2c000000) == 0x00000000)
187 /* Find the corresponding entry in the right TLB
188 Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE
190 static int find_tlb_entry(CPUState
* env
, target_ulong address
,
191 tlb_t
* entries
, uint8_t nbtlb
, int use_asid
)
193 int match
= MMU_DTLB_MISS
;
198 asid
= env
->pteh
& 0xff;
200 for (i
= 0; i
< nbtlb
; i
++) {
202 continue; /* Invalid entry */
203 if (use_asid
&& entries
[i
].asid
!= asid
&& !entries
[i
].sh
)
204 continue; /* Bad ASID */
206 switch (entries
[i
].sz
) {
208 size
= 1024; /* 1kB */
211 size
= 4 * 1024; /* 4kB */
214 size
= 64 * 1024; /* 64kB */
217 size
= 1024 * 1024; /* 1MB */
223 start
= (entries
[i
].vpn
<< 10) & ~(entries
[i
].size
- 1);
224 end
= start
+ entries
[i
].size
- 1;
225 if (address
>= start
&& address
<= end
) { /* Match */
227 return MMU_DTLB_MULTIPLE
; /* Multiple match */
234 /* Find itlb entry - update itlb from utlb if necessary and asked for
235 Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
236 Update the itlb from utlb if update is not 0
238 int find_itlb_entry(CPUState
* env
, target_ulong address
,
239 int use_asid
, int update
)
243 e
= find_tlb_entry(env
, address
, env
->itlb
, ITLB_SIZE
, use_asid
);
244 if (e
== MMU_DTLB_MULTIPLE
)
245 e
= MMU_ITLB_MULTIPLE
;
246 else if (e
== MMU_DTLB_MISS
&& update
) {
247 e
= find_tlb_entry(env
, address
, env
->utlb
, UTLB_SIZE
, use_asid
);
249 n
= itlb_replacement(env
);
250 env
->itlb
[n
] = env
->utlb
[e
];
255 update_itlb_use(env
, e
);
260 Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
261 int find_utlb_entry(CPUState
* env
, target_ulong address
, int use_asid
)
266 urb
= ((env
->mmucr
) >> 18) & 0x3f;
267 urc
= ((env
->mmucr
) >> 10) & 0x3f;
269 if (urc
== urb
|| urc
== UTLB_SIZE
- 1)
271 env
->mmucr
= (env
->mmucr
& 0xffff03ff) | (urc
<< 10);
274 return find_tlb_entry(env
, address
, env
->utlb
, UTLB_SIZE
, use_asid
);
277 /* Match address against MMU
278 Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
279 MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
280 MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
281 MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION
283 static int get_mmu_address(CPUState
* env
, target_ulong
* physical
,
284 int *prot
, target_ulong address
,
285 int rw
, int access_type
)
287 int use_asid
, is_code
, n
;
288 tlb_t
*matching
= NULL
;
290 use_asid
= (env
->mmucr
& MMUCR_SV
) == 0 && (env
->sr
& SR_MD
) == 0;
291 is_code
= env
->pc
== address
; /* Hack */
293 /* Use a hack to find if this is an instruction or data access */
294 if (env
->pc
== address
&& !(rw
& PAGE_WRITE
)) {
295 n
= find_itlb_entry(env
, address
, use_asid
, 1);
297 matching
= &env
->itlb
[n
];
298 if ((env
->sr
& SR_MD
) & !(matching
->pr
& 2))
299 n
= MMU_ITLB_VIOLATION
;
304 n
= find_utlb_entry(env
, address
, use_asid
);
306 matching
= &env
->utlb
[n
];
307 switch ((matching
->pr
<< 1) | ((env
->sr
& SR_MD
) ? 1 : 0)) {
310 n
= (rw
& PAGE_WRITE
) ? MMU_DTLB_VIOLATION_WRITE
:
311 MMU_DTLB_VIOLATION_READ
;
317 n
= MMU_DTLB_VIOLATION_WRITE
;
324 *prot
= rw
& (PAGE_READ
| PAGE_WRITE
);
327 } else if (n
== MMU_DTLB_MISS
) {
328 n
= (rw
& PAGE_WRITE
) ? MMU_DTLB_MISS_WRITE
:
333 *physical
= ((matching
->ppn
<< 10) & ~(matching
->size
- 1)) |
334 (address
& (matching
->size
- 1));
335 if ((rw
& PAGE_WRITE
) & !matching
->d
)
336 n
= MMU_DTLB_INITIAL_WRITE
;
343 int get_physical_address(CPUState
* env
, target_ulong
* physical
,
344 int *prot
, target_ulong address
,
345 int rw
, int access_type
)
347 /* P1, P2 and P4 areas do not use translation */
348 if ((address
>= 0x80000000 && address
< 0xc0000000) ||
349 address
>= 0xe0000000) {
350 if (!(env
->sr
& SR_MD
)
351 && (address
< 0xe0000000 || address
> 0xe4000000)) {
352 /* Unauthorized access in user mode (only store queues are available) */
353 fprintf(stderr
, "Unauthorized access\n");
354 return (rw
& PAGE_WRITE
) ? MMU_DTLB_MISS_WRITE
:
357 /* Mask upper 3 bits */
358 *physical
= address
& 0x1FFFFFFF;
359 *prot
= PAGE_READ
| PAGE_WRITE
;
363 /* If MMU is disabled, return the corresponding physical page */
364 if (!env
->mmucr
& MMUCR_AT
) {
365 *physical
= address
& 0x1FFFFFFF;
366 *prot
= PAGE_READ
| PAGE_WRITE
;
370 /* We need to resort to the MMU */
371 return get_mmu_address(env
, physical
, prot
, address
, rw
, access_type
);
374 int cpu_sh4_handle_mmu_fault(CPUState
* env
, target_ulong address
, int rw
,
375 int is_user
, int is_softmmu
)
377 target_ulong physical
, page_offset
, page_size
;
378 int prot
, ret
, access_type
;
382 fprintf(stderr
, "%s pc %08x ad %08x rw %d is_user %d smmu %d\n",
383 __func__
, env
->pc
, address
, rw
, is_user
, is_softmmu
);
386 access_type
= ACCESS_INT
;
388 get_physical_address(env
, &physical
, &prot
, address
, rw
,
395 case MMU_DTLB_MISS_READ
:
396 env
->exception_index
= 0x040;
398 case MMU_DTLB_MULTIPLE
:
399 case MMU_ITLB_MULTIPLE
:
400 env
->exception_index
= 0x140;
402 case MMU_ITLB_VIOLATION
:
403 env
->exception_index
= 0x0a0;
405 case MMU_DTLB_MISS_WRITE
:
406 env
->exception_index
= 0x060;
408 case MMU_DTLB_INITIAL_WRITE
:
409 env
->exception_index
= 0x080;
411 case MMU_DTLB_VIOLATION_READ
:
412 env
->exception_index
= 0x0a0;
414 case MMU_DTLB_VIOLATION_WRITE
:
415 env
->exception_index
= 0x0c0;
423 page_size
= TARGET_PAGE_SIZE
;
425 (address
- (address
& TARGET_PAGE_MASK
)) & ~(page_size
- 1);
426 address
= (address
& TARGET_PAGE_MASK
) + page_offset
;
427 physical
= (physical
& TARGET_PAGE_MASK
) + page_offset
;
429 return tlb_set_page(env
, address
, physical
, prot
, is_user
, is_softmmu
);
432 target_phys_addr_t
cpu_get_phys_page_debug(CPUState
* env
, target_ulong addr
)
434 target_ulong physical
;
437 get_physical_address(env
, &physical
, &prot
, addr
, PAGE_READ
, 0);