]>
Commit | Line | Data |
---|---|---|
fdf9b3e8 FB |
1 | /* |
2 | * SH4 emulation | |
5fafdf24 | 3 | * |
fdf9b3e8 FB |
4 | * Copyright (c) 2005 Samuel Tardieu |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
8167ee88 | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
fdf9b3e8 | 18 | */ |
9d4c9946 | 19 | #include "qemu/osdep.h" |
fdf9b3e8 FB |
20 | |
21 | #include "cpu.h" | |
63c91552 | 22 | #include "exec/exec-all.h" |
508127e2 | 23 | #include "exec/log.h" |
73479c5c | 24 | #include "sysemu/sysemu.h" |
b279e5ef BC |
25 | |
26 | #if !defined(CONFIG_USER_ONLY) | |
0d09e41a | 27 | #include "hw/sh4/sh_intc.h" |
b279e5ef | 28 | #endif |
fdf9b3e8 | 29 | |
355fb23d PB |
30 | #if defined(CONFIG_USER_ONLY) |
31 | ||
97a8ea5a | 32 | void superh_cpu_do_interrupt(CPUState *cs) |
355fb23d | 33 | { |
27103424 | 34 | cs->exception_index = -1; |
355fb23d PB |
35 | } |
36 | ||
98670d47 | 37 | int superh_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int size, int rw, |
7510454e | 38 | int mmu_idx) |
355fb23d | 39 | { |
7510454e AF |
40 | SuperHCPU *cpu = SUPERH_CPU(cs); |
41 | CPUSH4State *env = &cpu->env; | |
42 | ||
355fb23d | 43 | env->tea = address; |
27103424 | 44 | cs->exception_index = -1; |
355fb23d PB |
45 | switch (rw) { |
46 | case 0: | |
27103424 | 47 | cs->exception_index = 0x0a0; |
355fb23d PB |
48 | break; |
49 | case 1: | |
27103424 | 50 | cs->exception_index = 0x0c0; |
355fb23d | 51 | break; |
cf7055bd | 52 | case 2: |
27103424 | 53 | cs->exception_index = 0x0a0; |
cf7055bd | 54 | break; |
355fb23d PB |
55 | } |
56 | return 1; | |
57 | } | |
58 | ||
3c1adf12 EI |
59 | int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr) |
60 | { | |
67cc32eb | 61 | /* For user mode, only U0 area is cacheable. */ |
679dee3c | 62 | return !(addr & 0x80000000); |
3c1adf12 EI |
63 | } |
64 | ||
355fb23d PB |
65 | #else /* !CONFIG_USER_ONLY */ |
66 | ||
fdf9b3e8 FB |
67 | #define MMU_OK 0 |
68 | #define MMU_ITLB_MISS (-1) | |
69 | #define MMU_ITLB_MULTIPLE (-2) | |
70 | #define MMU_ITLB_VIOLATION (-3) | |
71 | #define MMU_DTLB_MISS_READ (-4) | |
72 | #define MMU_DTLB_MISS_WRITE (-5) | |
73 | #define MMU_DTLB_INITIAL_WRITE (-6) | |
74 | #define MMU_DTLB_VIOLATION_READ (-7) | |
75 | #define MMU_DTLB_VIOLATION_WRITE (-8) | |
76 | #define MMU_DTLB_MULTIPLE (-9) | |
77 | #define MMU_DTLB_MISS (-10) | |
cf7055bd AJ |
78 | #define MMU_IADDR_ERROR (-11) |
79 | #define MMU_DADDR_ERROR_READ (-12) | |
80 | #define MMU_DADDR_ERROR_WRITE (-13) | |
fdf9b3e8 | 81 | |
97a8ea5a | 82 | void superh_cpu_do_interrupt(CPUState *cs) |
fdf9b3e8 | 83 | { |
97a8ea5a AF |
84 | SuperHCPU *cpu = SUPERH_CPU(cs); |
85 | CPUSH4State *env = &cpu->env; | |
259186a7 | 86 | int do_irq = cs->interrupt_request & CPU_INTERRUPT_HARD; |
27103424 | 87 | int do_exp, irq_vector = cs->exception_index; |
e96e2044 TS |
88 | |
89 | /* prioritize exceptions over interrupts */ | |
90 | ||
27103424 AF |
91 | do_exp = cs->exception_index != -1; |
92 | do_irq = do_irq && (cs->exception_index == -1); | |
e96e2044 | 93 | |
5ed9a259 | 94 | if (env->sr & (1u << SR_BL)) { |
27103424 | 95 | if (do_exp && cs->exception_index != 0x1e0) { |
73479c5c AJ |
96 | /* In theory a masked exception generates a reset exception, |
97 | which in turn jumps to the reset vector. However this only | |
98 | works when using a bootloader. When using a kernel and an | |
99 | initrd, they need to be reloaded and the program counter | |
100 | should be loaded with the kernel entry point. | |
101 | qemu_system_reset_request takes care of that. */ | |
102 | qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); | |
103 | return; | |
e96e2044 | 104 | } |
efac4154 | 105 | if (do_irq && !env->in_sleep) { |
e96e2044 TS |
106 | return; /* masked */ |
107 | } | |
108 | } | |
efac4154 | 109 | env->in_sleep = 0; |
e96e2044 TS |
110 | |
111 | if (do_irq) { | |
112 | irq_vector = sh_intc_get_pending_vector(env->intc_handle, | |
113 | (env->sr >> 4) & 0xf); | |
114 | if (irq_vector == -1) { | |
115 | return; /* masked */ | |
116 | } | |
117 | } | |
118 | ||
8fec2b8c | 119 | if (qemu_loglevel_mask(CPU_LOG_INT)) { |
fdf9b3e8 | 120 | const char *expname; |
27103424 | 121 | switch (cs->exception_index) { |
fdf9b3e8 FB |
122 | case 0x0e0: |
123 | expname = "addr_error"; | |
124 | break; | |
125 | case 0x040: | |
126 | expname = "tlb_miss"; | |
127 | break; | |
128 | case 0x0a0: | |
129 | expname = "tlb_violation"; | |
130 | break; | |
131 | case 0x180: | |
132 | expname = "illegal_instruction"; | |
133 | break; | |
134 | case 0x1a0: | |
135 | expname = "slot_illegal_instruction"; | |
136 | break; | |
137 | case 0x800: | |
138 | expname = "fpu_disable"; | |
139 | break; | |
140 | case 0x820: | |
141 | expname = "slot_fpu"; | |
142 | break; | |
143 | case 0x100: | |
144 | expname = "data_write"; | |
145 | break; | |
146 | case 0x060: | |
147 | expname = "dtlb_miss_write"; | |
148 | break; | |
149 | case 0x0c0: | |
150 | expname = "dtlb_violation_write"; | |
151 | break; | |
152 | case 0x120: | |
153 | expname = "fpu_exception"; | |
154 | break; | |
155 | case 0x080: | |
156 | expname = "initial_page_write"; | |
157 | break; | |
158 | case 0x160: | |
159 | expname = "trapa"; | |
160 | break; | |
161 | default: | |
e96e2044 TS |
162 | expname = do_irq ? "interrupt" : "???"; |
163 | break; | |
fdf9b3e8 | 164 | } |
93fcfe39 AL |
165 | qemu_log("exception 0x%03x [%s] raised\n", |
166 | irq_vector, expname); | |
a0762859 | 167 | log_cpu_state(cs, 0); |
fdf9b3e8 FB |
168 | } |
169 | ||
34086945 | 170 | env->ssr = cpu_read_sr(env); |
e96e2044 | 171 | env->spc = env->pc; |
fdf9b3e8 | 172 | env->sgr = env->gregs[15]; |
5ed9a259 | 173 | env->sr |= (1u << SR_BL) | (1u << SR_MD) | (1u << SR_RB); |
f85da308 | 174 | env->lock_addr = -1; |
fdf9b3e8 | 175 | |
9a562ae7 | 176 | if (env->flags & DELAY_SLOT_MASK) { |
274a9e70 AJ |
177 | /* Branch instruction should be executed again before delay slot. */ |
178 | env->spc -= 2; | |
179 | /* Clear flags for exception/interrupt routine. */ | |
9a562ae7 | 180 | env->flags &= ~DELAY_SLOT_MASK; |
274a9e70 | 181 | } |
274a9e70 | 182 | |
e96e2044 | 183 | if (do_exp) { |
27103424 AF |
184 | env->expevt = cs->exception_index; |
185 | switch (cs->exception_index) { | |
e96e2044 TS |
186 | case 0x000: |
187 | case 0x020: | |
188 | case 0x140: | |
5ed9a259 | 189 | env->sr &= ~(1u << SR_FD); |
e96e2044 TS |
190 | env->sr |= 0xf << 4; /* IMASK */ |
191 | env->pc = 0xa0000000; | |
192 | break; | |
193 | case 0x040: | |
194 | case 0x060: | |
195 | env->pc = env->vbr + 0x400; | |
196 | break; | |
197 | case 0x160: | |
198 | env->spc += 2; /* special case for TRAPA */ | |
199 | /* fall through */ | |
200 | default: | |
201 | env->pc = env->vbr + 0x100; | |
202 | break; | |
203 | } | |
204 | return; | |
205 | } | |
206 | ||
207 | if (do_irq) { | |
208 | env->intevt = irq_vector; | |
209 | env->pc = env->vbr + 0x600; | |
210 | return; | |
fdf9b3e8 FB |
211 | } |
212 | } | |
213 | ||
73e5716c | 214 | static void update_itlb_use(CPUSH4State * env, int itlbnb) |
fdf9b3e8 FB |
215 | { |
216 | uint8_t or_mask = 0, and_mask = (uint8_t) - 1; | |
217 | ||
218 | switch (itlbnb) { | |
219 | case 0: | |
ea2b542a | 220 | and_mask = 0x1f; |
fdf9b3e8 FB |
221 | break; |
222 | case 1: | |
223 | and_mask = 0xe7; | |
224 | or_mask = 0x80; | |
225 | break; | |
226 | case 2: | |
227 | and_mask = 0xfb; | |
228 | or_mask = 0x50; | |
229 | break; | |
230 | case 3: | |
231 | or_mask = 0x2c; | |
232 | break; | |
233 | } | |
234 | ||
ea2b542a | 235 | env->mmucr &= (and_mask << 24) | 0x00ffffff; |
fdf9b3e8 FB |
236 | env->mmucr |= (or_mask << 24); |
237 | } | |
238 | ||
73e5716c | 239 | static int itlb_replacement(CPUSH4State * env) |
fdf9b3e8 | 240 | { |
a47dddd7 AF |
241 | SuperHCPU *cpu = sh_env_get_cpu(env); |
242 | ||
243 | if ((env->mmucr & 0xe0000000) == 0xe0000000) { | |
fdf9b3e8 | 244 | return 0; |
a47dddd7 AF |
245 | } |
246 | if ((env->mmucr & 0x98000000) == 0x18000000) { | |
fdf9b3e8 | 247 | return 1; |
a47dddd7 AF |
248 | } |
249 | if ((env->mmucr & 0x54000000) == 0x04000000) { | |
fdf9b3e8 | 250 | return 2; |
a47dddd7 AF |
251 | } |
252 | if ((env->mmucr & 0x2c000000) == 0x00000000) { | |
fdf9b3e8 | 253 | return 3; |
a47dddd7 AF |
254 | } |
255 | cpu_abort(CPU(cpu), "Unhandled itlb_replacement"); | |
fdf9b3e8 FB |
256 | } |
257 | ||
258 | /* Find the corresponding entry in the right TLB | |
259 | Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE | |
260 | */ | |
73e5716c | 261 | static int find_tlb_entry(CPUSH4State * env, target_ulong address, |
fdf9b3e8 FB |
262 | tlb_t * entries, uint8_t nbtlb, int use_asid) |
263 | { | |
264 | int match = MMU_DTLB_MISS; | |
265 | uint32_t start, end; | |
266 | uint8_t asid; | |
267 | int i; | |
268 | ||
269 | asid = env->pteh & 0xff; | |
270 | ||
271 | for (i = 0; i < nbtlb; i++) { | |
272 | if (!entries[i].v) | |
273 | continue; /* Invalid entry */ | |
eeda6778 | 274 | if (!entries[i].sh && use_asid && entries[i].asid != asid) |
fdf9b3e8 | 275 | continue; /* Bad ASID */ |
fdf9b3e8 FB |
276 | start = (entries[i].vpn << 10) & ~(entries[i].size - 1); |
277 | end = start + entries[i].size - 1; | |
278 | if (address >= start && address <= end) { /* Match */ | |
ea2b542a | 279 | if (match != MMU_DTLB_MISS) |
fdf9b3e8 FB |
280 | return MMU_DTLB_MULTIPLE; /* Multiple match */ |
281 | match = i; | |
282 | } | |
283 | } | |
284 | return match; | |
285 | } | |
286 | ||
73e5716c | 287 | static void increment_urc(CPUSH4State * env) |
29e179bc AJ |
288 | { |
289 | uint8_t urb, urc; | |
290 | ||
291 | /* Increment URC */ | |
292 | urb = ((env->mmucr) >> 18) & 0x3f; | |
293 | urc = ((env->mmucr) >> 10) & 0x3f; | |
294 | urc++; | |
927e3a4e | 295 | if ((urb > 0 && urc > urb) || urc > (UTLB_SIZE - 1)) |
29e179bc AJ |
296 | urc = 0; |
297 | env->mmucr = (env->mmucr & 0xffff03ff) | (urc << 10); | |
298 | } | |
299 | ||
829a4927 AJ |
300 | /* Copy and utlb entry into itlb |
301 | Return entry | |
302 | */ | |
73e5716c | 303 | static int copy_utlb_entry_itlb(CPUSH4State *env, int utlb) |
829a4927 AJ |
304 | { |
305 | int itlb; | |
306 | ||
307 | tlb_t * ientry; | |
308 | itlb = itlb_replacement(env); | |
309 | ientry = &env->itlb[itlb]; | |
310 | if (ientry->v) { | |
31b030d4 | 311 | tlb_flush_page(CPU(sh_env_get_cpu(env)), ientry->vpn << 10); |
829a4927 AJ |
312 | } |
313 | *ientry = env->utlb[utlb]; | |
314 | update_itlb_use(env, itlb); | |
315 | return itlb; | |
316 | } | |
317 | ||
318 | /* Find itlb entry | |
fdf9b3e8 | 319 | Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE |
fdf9b3e8 | 320 | */ |
73e5716c | 321 | static int find_itlb_entry(CPUSH4State * env, target_ulong address, |
829a4927 | 322 | int use_asid) |
fdf9b3e8 | 323 | { |
829a4927 | 324 | int e; |
fdf9b3e8 FB |
325 | |
326 | e = find_tlb_entry(env, address, env->itlb, ITLB_SIZE, use_asid); | |
829a4927 | 327 | if (e == MMU_DTLB_MULTIPLE) { |
fdf9b3e8 | 328 | e = MMU_ITLB_MULTIPLE; |
829a4927 | 329 | } else if (e == MMU_DTLB_MISS) { |
ea2b542a | 330 | e = MMU_ITLB_MISS; |
829a4927 | 331 | } else if (e >= 0) { |
fdf9b3e8 | 332 | update_itlb_use(env, e); |
829a4927 | 333 | } |
fdf9b3e8 FB |
334 | return e; |
335 | } | |
336 | ||
337 | /* Find utlb entry | |
338 | Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */ | |
73e5716c | 339 | static int find_utlb_entry(CPUSH4State * env, target_ulong address, int use_asid) |
fdf9b3e8 | 340 | { |
29e179bc AJ |
341 | /* per utlb access */ |
342 | increment_urc(env); | |
fdf9b3e8 FB |
343 | |
344 | /* Return entry */ | |
345 | return find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid); | |
346 | } | |
347 | ||
348 | /* Match address against MMU | |
349 | Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE, | |
350 | MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ, | |
351 | MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS, | |
cf7055bd AJ |
352 | MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION, |
353 | MMU_IADDR_ERROR, MMU_DADDR_ERROR_READ, MMU_DADDR_ERROR_WRITE. | |
fdf9b3e8 | 354 | */ |
73e5716c | 355 | static int get_mmu_address(CPUSH4State * env, target_ulong * physical, |
fdf9b3e8 FB |
356 | int *prot, target_ulong address, |
357 | int rw, int access_type) | |
358 | { | |
cf7055bd | 359 | int use_asid, n; |
fdf9b3e8 FB |
360 | tlb_t *matching = NULL; |
361 | ||
5ed9a259 | 362 | use_asid = !(env->mmucr & MMUCR_SV) || !(env->sr & (1u << SR_MD)); |
fdf9b3e8 | 363 | |
cf7055bd | 364 | if (rw == 2) { |
829a4927 | 365 | n = find_itlb_entry(env, address, use_asid); |
fdf9b3e8 FB |
366 | if (n >= 0) { |
367 | matching = &env->itlb[n]; | |
5ed9a259 | 368 | if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) { |
fdf9b3e8 | 369 | n = MMU_ITLB_VIOLATION; |
5ed9a259 | 370 | } else { |
5a25cc2b | 371 | *prot = PAGE_EXEC; |
5ed9a259 | 372 | } |
829a4927 AJ |
373 | } else { |
374 | n = find_utlb_entry(env, address, use_asid); | |
375 | if (n >= 0) { | |
376 | n = copy_utlb_entry_itlb(env, n); | |
377 | matching = &env->itlb[n]; | |
5ed9a259 AJ |
378 | if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) { |
379 | n = MMU_ITLB_VIOLATION; | |
829a4927 AJ |
380 | } else { |
381 | *prot = PAGE_READ | PAGE_EXEC; | |
382 | if ((matching->pr & 1) && matching->d) { | |
383 | *prot |= PAGE_WRITE; | |
384 | } | |
385 | } | |
386 | } else if (n == MMU_DTLB_MULTIPLE) { | |
387 | n = MMU_ITLB_MULTIPLE; | |
388 | } else if (n == MMU_DTLB_MISS) { | |
389 | n = MMU_ITLB_MISS; | |
390 | } | |
fdf9b3e8 FB |
391 | } |
392 | } else { | |
393 | n = find_utlb_entry(env, address, use_asid); | |
394 | if (n >= 0) { | |
395 | matching = &env->utlb[n]; | |
5ed9a259 | 396 | if (!(env->sr & (1u << SR_MD)) && !(matching->pr & 2)) { |
628b61a0 AJ |
397 | n = (rw == 1) ? MMU_DTLB_VIOLATION_WRITE : |
398 | MMU_DTLB_VIOLATION_READ; | |
399 | } else if ((rw == 1) && !(matching->pr & 1)) { | |
400 | n = MMU_DTLB_VIOLATION_WRITE; | |
0c16e71e | 401 | } else if ((rw == 1) && !matching->d) { |
628b61a0 AJ |
402 | n = MMU_DTLB_INITIAL_WRITE; |
403 | } else { | |
404 | *prot = PAGE_READ; | |
405 | if ((matching->pr & 1) && matching->d) { | |
406 | *prot |= PAGE_WRITE; | |
407 | } | |
408 | } | |
fdf9b3e8 | 409 | } else if (n == MMU_DTLB_MISS) { |
cf7055bd | 410 | n = (rw == 1) ? MMU_DTLB_MISS_WRITE : |
fdf9b3e8 FB |
411 | MMU_DTLB_MISS_READ; |
412 | } | |
413 | } | |
414 | if (n >= 0) { | |
628b61a0 | 415 | n = MMU_OK; |
fdf9b3e8 FB |
416 | *physical = ((matching->ppn << 10) & ~(matching->size - 1)) | |
417 | (address & (matching->size - 1)); | |
fdf9b3e8 FB |
418 | } |
419 | return n; | |
420 | } | |
421 | ||
73e5716c | 422 | static int get_physical_address(CPUSH4State * env, target_ulong * physical, |
ef7ec1c1 AJ |
423 | int *prot, target_ulong address, |
424 | int rw, int access_type) | |
fdf9b3e8 FB |
425 | { |
426 | /* P1, P2 and P4 areas do not use translation */ | |
427 | if ((address >= 0x80000000 && address < 0xc0000000) || | |
428 | address >= 0xe0000000) { | |
5ed9a259 | 429 | if (!(env->sr & (1u << SR_MD)) |
03e3b61e | 430 | && (address < 0xe0000000 || address >= 0xe4000000)) { |
fdf9b3e8 | 431 | /* Unauthorized access in user mode (only store queues are available) */ |
324189ba | 432 | qemu_log_mask(LOG_GUEST_ERROR, "Unauthorized access\n"); |
cf7055bd AJ |
433 | if (rw == 0) |
434 | return MMU_DADDR_ERROR_READ; | |
435 | else if (rw == 1) | |
436 | return MMU_DADDR_ERROR_WRITE; | |
437 | else | |
438 | return MMU_IADDR_ERROR; | |
fdf9b3e8 | 439 | } |
29e179bc AJ |
440 | if (address >= 0x80000000 && address < 0xc0000000) { |
441 | /* Mask upper 3 bits for P1 and P2 areas */ | |
442 | *physical = address & 0x1fffffff; | |
29e179bc | 443 | } else { |
29e179bc AJ |
444 | *physical = address; |
445 | } | |
5a25cc2b | 446 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; |
fdf9b3e8 FB |
447 | return MMU_OK; |
448 | } | |
449 | ||
450 | /* If MMU is disabled, return the corresponding physical page */ | |
0c16e71e | 451 | if (!(env->mmucr & MMUCR_AT)) { |
fdf9b3e8 | 452 | *physical = address & 0x1FFFFFFF; |
5a25cc2b | 453 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; |
fdf9b3e8 FB |
454 | return MMU_OK; |
455 | } | |
456 | ||
457 | /* We need to resort to the MMU */ | |
458 | return get_mmu_address(env, physical, prot, address, rw, access_type); | |
459 | } | |
460 | ||
98670d47 | 461 | int superh_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int size, int rw, |
7510454e | 462 | int mmu_idx) |
fdf9b3e8 | 463 | { |
7510454e AF |
464 | SuperHCPU *cpu = SUPERH_CPU(cs); |
465 | CPUSH4State *env = &cpu->env; | |
0f3f1ec7 | 466 | target_ulong physical; |
fdf9b3e8 FB |
467 | int prot, ret, access_type; |
468 | ||
fdf9b3e8 FB |
469 | access_type = ACCESS_INT; |
470 | ret = | |
471 | get_physical_address(env, &physical, &prot, address, rw, | |
472 | access_type); | |
473 | ||
474 | if (ret != MMU_OK) { | |
475 | env->tea = address; | |
e3f114f7 AC |
476 | if (ret != MMU_DTLB_MULTIPLE && ret != MMU_ITLB_MULTIPLE) { |
477 | env->pteh = (env->pteh & PTEH_ASID_MASK) | | |
478 | (address & PTEH_VPN_MASK); | |
479 | } | |
fdf9b3e8 FB |
480 | switch (ret) { |
481 | case MMU_ITLB_MISS: | |
482 | case MMU_DTLB_MISS_READ: | |
27103424 | 483 | cs->exception_index = 0x040; |
fdf9b3e8 FB |
484 | break; |
485 | case MMU_DTLB_MULTIPLE: | |
486 | case MMU_ITLB_MULTIPLE: | |
27103424 | 487 | cs->exception_index = 0x140; |
fdf9b3e8 FB |
488 | break; |
489 | case MMU_ITLB_VIOLATION: | |
27103424 | 490 | cs->exception_index = 0x0a0; |
fdf9b3e8 FB |
491 | break; |
492 | case MMU_DTLB_MISS_WRITE: | |
27103424 | 493 | cs->exception_index = 0x060; |
fdf9b3e8 FB |
494 | break; |
495 | case MMU_DTLB_INITIAL_WRITE: | |
27103424 | 496 | cs->exception_index = 0x080; |
fdf9b3e8 FB |
497 | break; |
498 | case MMU_DTLB_VIOLATION_READ: | |
27103424 | 499 | cs->exception_index = 0x0a0; |
fdf9b3e8 FB |
500 | break; |
501 | case MMU_DTLB_VIOLATION_WRITE: | |
27103424 | 502 | cs->exception_index = 0x0c0; |
fdf9b3e8 | 503 | break; |
cf7055bd AJ |
504 | case MMU_IADDR_ERROR: |
505 | case MMU_DADDR_ERROR_READ: | |
27103424 | 506 | cs->exception_index = 0x0e0; |
cf7055bd AJ |
507 | break; |
508 | case MMU_DADDR_ERROR_WRITE: | |
27103424 | 509 | cs->exception_index = 0x100; |
cf7055bd | 510 | break; |
fdf9b3e8 | 511 | default: |
a47dddd7 | 512 | cpu_abort(cs, "Unhandled MMU fault"); |
fdf9b3e8 FB |
513 | } |
514 | return 1; | |
515 | } | |
516 | ||
0f3f1ec7 AJ |
517 | address &= TARGET_PAGE_MASK; |
518 | physical &= TARGET_PAGE_MASK; | |
fdf9b3e8 | 519 | |
0c591eb0 | 520 | tlb_set_page(cs, address, physical, prot, mmu_idx, TARGET_PAGE_SIZE); |
d4c430a8 | 521 | return 0; |
fdf9b3e8 | 522 | } |
355fb23d | 523 | |
00b941e5 | 524 | hwaddr superh_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) |
355fb23d | 525 | { |
00b941e5 | 526 | SuperHCPU *cpu = SUPERH_CPU(cs); |
355fb23d PB |
527 | target_ulong physical; |
528 | int prot; | |
529 | ||
00b941e5 | 530 | get_physical_address(&cpu->env, &physical, &prot, addr, 0, 0); |
355fb23d PB |
531 | return physical; |
532 | } | |
533 | ||
ef7ec1c1 | 534 | void cpu_load_tlb(CPUSH4State * env) |
ea2b542a | 535 | { |
a47dddd7 | 536 | SuperHCPU *cpu = sh_env_get_cpu(env); |
ea2b542a AJ |
537 | int n = cpu_mmucr_urc(env->mmucr); |
538 | tlb_t * entry = &env->utlb[n]; | |
539 | ||
06afe2c8 AJ |
540 | if (entry->v) { |
541 | /* Overwriting valid entry in utlb. */ | |
542 | target_ulong address = entry->vpn << 10; | |
31b030d4 | 543 | tlb_flush_page(CPU(cpu), address); |
06afe2c8 AJ |
544 | } |
545 | ||
ea2b542a AJ |
546 | /* Take values into cpu status from registers. */ |
547 | entry->asid = (uint8_t)cpu_pteh_asid(env->pteh); | |
548 | entry->vpn = cpu_pteh_vpn(env->pteh); | |
549 | entry->v = (uint8_t)cpu_ptel_v(env->ptel); | |
550 | entry->ppn = cpu_ptel_ppn(env->ptel); | |
551 | entry->sz = (uint8_t)cpu_ptel_sz(env->ptel); | |
552 | switch (entry->sz) { | |
553 | case 0: /* 00 */ | |
554 | entry->size = 1024; /* 1K */ | |
555 | break; | |
556 | case 1: /* 01 */ | |
557 | entry->size = 1024 * 4; /* 4K */ | |
558 | break; | |
559 | case 2: /* 10 */ | |
560 | entry->size = 1024 * 64; /* 64K */ | |
561 | break; | |
562 | case 3: /* 11 */ | |
563 | entry->size = 1024 * 1024; /* 1M */ | |
564 | break; | |
565 | default: | |
a47dddd7 | 566 | cpu_abort(CPU(cpu), "Unhandled load_tlb"); |
ea2b542a AJ |
567 | break; |
568 | } | |
569 | entry->sh = (uint8_t)cpu_ptel_sh(env->ptel); | |
570 | entry->c = (uint8_t)cpu_ptel_c(env->ptel); | |
571 | entry->pr = (uint8_t)cpu_ptel_pr(env->ptel); | |
572 | entry->d = (uint8_t)cpu_ptel_d(env->ptel); | |
573 | entry->wt = (uint8_t)cpu_ptel_wt(env->ptel); | |
574 | entry->sa = (uint8_t)cpu_ptea_sa(env->ptea); | |
575 | entry->tc = (uint8_t)cpu_ptea_tc(env->ptea); | |
576 | } | |
577 | ||
e0bcb9ca AJ |
578 | void cpu_sh4_invalidate_tlb(CPUSH4State *s) |
579 | { | |
580 | int i; | |
581 | ||
582 | /* UTLB */ | |
583 | for (i = 0; i < UTLB_SIZE; i++) { | |
584 | tlb_t * entry = &s->utlb[i]; | |
585 | entry->v = 0; | |
586 | } | |
587 | /* ITLB */ | |
e40a67be AC |
588 | for (i = 0; i < ITLB_SIZE; i++) { |
589 | tlb_t * entry = &s->itlb[i]; | |
e0bcb9ca AJ |
590 | entry->v = 0; |
591 | } | |
592 | ||
d10eb08f | 593 | tlb_flush(CPU(sh_env_get_cpu(s))); |
e0bcb9ca AJ |
594 | } |
595 | ||
bc656a29 | 596 | uint32_t cpu_sh4_read_mmaped_itlb_addr(CPUSH4State *s, |
a8170e5e | 597 | hwaddr addr) |
bc656a29 AJ |
598 | { |
599 | int index = (addr & 0x00000300) >> 8; | |
600 | tlb_t * entry = &s->itlb[index]; | |
601 | ||
602 | return (entry->vpn << 10) | | |
603 | (entry->v << 8) | | |
604 | (entry->asid); | |
605 | } | |
606 | ||
a8170e5e | 607 | void cpu_sh4_write_mmaped_itlb_addr(CPUSH4State *s, hwaddr addr, |
c0f809c4 AJ |
608 | uint32_t mem_value) |
609 | { | |
610 | uint32_t vpn = (mem_value & 0xfffffc00) >> 10; | |
611 | uint8_t v = (uint8_t)((mem_value & 0x00000100) >> 8); | |
612 | uint8_t asid = (uint8_t)(mem_value & 0x000000ff); | |
613 | ||
9f97309a | 614 | int index = (addr & 0x00000300) >> 8; |
c0f809c4 AJ |
615 | tlb_t * entry = &s->itlb[index]; |
616 | if (entry->v) { | |
617 | /* Overwriting valid entry in itlb. */ | |
618 | target_ulong address = entry->vpn << 10; | |
31b030d4 | 619 | tlb_flush_page(CPU(sh_env_get_cpu(s)), address); |
c0f809c4 AJ |
620 | } |
621 | entry->asid = asid; | |
622 | entry->vpn = vpn; | |
623 | entry->v = v; | |
624 | } | |
625 | ||
bc656a29 | 626 | uint32_t cpu_sh4_read_mmaped_itlb_data(CPUSH4State *s, |
a8170e5e | 627 | hwaddr addr) |
bc656a29 AJ |
628 | { |
629 | int array = (addr & 0x00800000) >> 23; | |
630 | int index = (addr & 0x00000300) >> 8; | |
631 | tlb_t * entry = &s->itlb[index]; | |
632 | ||
633 | if (array == 0) { | |
634 | /* ITLB Data Array 1 */ | |
635 | return (entry->ppn << 10) | | |
636 | (entry->v << 8) | | |
637 | (entry->pr << 5) | | |
638 | ((entry->sz & 1) << 6) | | |
639 | ((entry->sz & 2) << 4) | | |
640 | (entry->c << 3) | | |
641 | (entry->sh << 1); | |
642 | } else { | |
643 | /* ITLB Data Array 2 */ | |
644 | return (entry->tc << 1) | | |
645 | (entry->sa); | |
646 | } | |
647 | } | |
648 | ||
a8170e5e | 649 | void cpu_sh4_write_mmaped_itlb_data(CPUSH4State *s, hwaddr addr, |
9f97309a AJ |
650 | uint32_t mem_value) |
651 | { | |
652 | int array = (addr & 0x00800000) >> 23; | |
653 | int index = (addr & 0x00000300) >> 8; | |
654 | tlb_t * entry = &s->itlb[index]; | |
655 | ||
656 | if (array == 0) { | |
657 | /* ITLB Data Array 1 */ | |
658 | if (entry->v) { | |
659 | /* Overwriting valid entry in utlb. */ | |
660 | target_ulong address = entry->vpn << 10; | |
31b030d4 | 661 | tlb_flush_page(CPU(sh_env_get_cpu(s)), address); |
9f97309a AJ |
662 | } |
663 | entry->ppn = (mem_value & 0x1ffffc00) >> 10; | |
664 | entry->v = (mem_value & 0x00000100) >> 8; | |
665 | entry->sz = (mem_value & 0x00000080) >> 6 | | |
666 | (mem_value & 0x00000010) >> 4; | |
667 | entry->pr = (mem_value & 0x00000040) >> 5; | |
668 | entry->c = (mem_value & 0x00000008) >> 3; | |
669 | entry->sh = (mem_value & 0x00000002) >> 1; | |
670 | } else { | |
671 | /* ITLB Data Array 2 */ | |
672 | entry->tc = (mem_value & 0x00000008) >> 3; | |
673 | entry->sa = (mem_value & 0x00000007); | |
674 | } | |
675 | } | |
676 | ||
bc656a29 | 677 | uint32_t cpu_sh4_read_mmaped_utlb_addr(CPUSH4State *s, |
a8170e5e | 678 | hwaddr addr) |
bc656a29 AJ |
679 | { |
680 | int index = (addr & 0x00003f00) >> 8; | |
681 | tlb_t * entry = &s->utlb[index]; | |
682 | ||
683 | increment_urc(s); /* per utlb access */ | |
684 | ||
685 | return (entry->vpn << 10) | | |
686 | (entry->v << 8) | | |
687 | (entry->asid); | |
688 | } | |
689 | ||
a8170e5e | 690 | void cpu_sh4_write_mmaped_utlb_addr(CPUSH4State *s, hwaddr addr, |
29e179bc AJ |
691 | uint32_t mem_value) |
692 | { | |
693 | int associate = addr & 0x0000080; | |
694 | uint32_t vpn = (mem_value & 0xfffffc00) >> 10; | |
695 | uint8_t d = (uint8_t)((mem_value & 0x00000200) >> 9); | |
696 | uint8_t v = (uint8_t)((mem_value & 0x00000100) >> 8); | |
697 | uint8_t asid = (uint8_t)(mem_value & 0x000000ff); | |
5ed9a259 | 698 | int use_asid = !(s->mmucr & MMUCR_SV) || !(s->sr & (1u << SR_MD)); |
29e179bc AJ |
699 | |
700 | if (associate) { | |
701 | int i; | |
702 | tlb_t * utlb_match_entry = NULL; | |
703 | int needs_tlb_flush = 0; | |
704 | ||
705 | /* search UTLB */ | |
706 | for (i = 0; i < UTLB_SIZE; i++) { | |
707 | tlb_t * entry = &s->utlb[i]; | |
708 | if (!entry->v) | |
709 | continue; | |
710 | ||
eeda6778 AJ |
711 | if (entry->vpn == vpn |
712 | && (!use_asid || entry->asid == asid || entry->sh)) { | |
29e179bc | 713 | if (utlb_match_entry) { |
27103424 AF |
714 | CPUState *cs = CPU(sh_env_get_cpu(s)); |
715 | ||
29e179bc | 716 | /* Multiple TLB Exception */ |
27103424 | 717 | cs->exception_index = 0x140; |
29e179bc AJ |
718 | s->tea = addr; |
719 | break; | |
720 | } | |
721 | if (entry->v && !v) | |
722 | needs_tlb_flush = 1; | |
723 | entry->v = v; | |
724 | entry->d = d; | |
725 | utlb_match_entry = entry; | |
726 | } | |
727 | increment_urc(s); /* per utlb access */ | |
728 | } | |
729 | ||
730 | /* search ITLB */ | |
731 | for (i = 0; i < ITLB_SIZE; i++) { | |
732 | tlb_t * entry = &s->itlb[i]; | |
eeda6778 AJ |
733 | if (entry->vpn == vpn |
734 | && (!use_asid || entry->asid == asid || entry->sh)) { | |
29e179bc AJ |
735 | if (entry->v && !v) |
736 | needs_tlb_flush = 1; | |
737 | if (utlb_match_entry) | |
738 | *entry = *utlb_match_entry; | |
739 | else | |
740 | entry->v = v; | |
741 | break; | |
742 | } | |
743 | } | |
744 | ||
31b030d4 AF |
745 | if (needs_tlb_flush) { |
746 | tlb_flush_page(CPU(sh_env_get_cpu(s)), vpn << 10); | |
747 | } | |
29e179bc AJ |
748 | |
749 | } else { | |
750 | int index = (addr & 0x00003f00) >> 8; | |
751 | tlb_t * entry = &s->utlb[index]; | |
752 | if (entry->v) { | |
31b030d4 AF |
753 | CPUState *cs = CPU(sh_env_get_cpu(s)); |
754 | ||
29e179bc AJ |
755 | /* Overwriting valid entry in utlb. */ |
756 | target_ulong address = entry->vpn << 10; | |
31b030d4 | 757 | tlb_flush_page(cs, address); |
29e179bc AJ |
758 | } |
759 | entry->asid = asid; | |
760 | entry->vpn = vpn; | |
761 | entry->d = d; | |
762 | entry->v = v; | |
763 | increment_urc(s); | |
764 | } | |
765 | } | |
766 | ||
bc656a29 | 767 | uint32_t cpu_sh4_read_mmaped_utlb_data(CPUSH4State *s, |
a8170e5e | 768 | hwaddr addr) |
bc656a29 AJ |
769 | { |
770 | int array = (addr & 0x00800000) >> 23; | |
771 | int index = (addr & 0x00003f00) >> 8; | |
772 | tlb_t * entry = &s->utlb[index]; | |
773 | ||
774 | increment_urc(s); /* per utlb access */ | |
775 | ||
776 | if (array == 0) { | |
777 | /* ITLB Data Array 1 */ | |
778 | return (entry->ppn << 10) | | |
779 | (entry->v << 8) | | |
780 | (entry->pr << 5) | | |
781 | ((entry->sz & 1) << 6) | | |
782 | ((entry->sz & 2) << 4) | | |
783 | (entry->c << 3) | | |
784 | (entry->d << 2) | | |
785 | (entry->sh << 1) | | |
786 | (entry->wt); | |
787 | } else { | |
788 | /* ITLB Data Array 2 */ | |
789 | return (entry->tc << 1) | | |
790 | (entry->sa); | |
791 | } | |
792 | } | |
793 | ||
a8170e5e | 794 | void cpu_sh4_write_mmaped_utlb_data(CPUSH4State *s, hwaddr addr, |
9f97309a AJ |
795 | uint32_t mem_value) |
796 | { | |
797 | int array = (addr & 0x00800000) >> 23; | |
798 | int index = (addr & 0x00003f00) >> 8; | |
799 | tlb_t * entry = &s->utlb[index]; | |
800 | ||
801 | increment_urc(s); /* per utlb access */ | |
802 | ||
803 | if (array == 0) { | |
804 | /* UTLB Data Array 1 */ | |
805 | if (entry->v) { | |
806 | /* Overwriting valid entry in utlb. */ | |
807 | target_ulong address = entry->vpn << 10; | |
31b030d4 | 808 | tlb_flush_page(CPU(sh_env_get_cpu(s)), address); |
9f97309a AJ |
809 | } |
810 | entry->ppn = (mem_value & 0x1ffffc00) >> 10; | |
811 | entry->v = (mem_value & 0x00000100) >> 8; | |
812 | entry->sz = (mem_value & 0x00000080) >> 6 | | |
813 | (mem_value & 0x00000010) >> 4; | |
814 | entry->pr = (mem_value & 0x00000060) >> 5; | |
815 | entry->c = (mem_value & 0x00000008) >> 3; | |
816 | entry->d = (mem_value & 0x00000004) >> 2; | |
817 | entry->sh = (mem_value & 0x00000002) >> 1; | |
818 | entry->wt = (mem_value & 0x00000001); | |
819 | } else { | |
820 | /* UTLB Data Array 2 */ | |
821 | entry->tc = (mem_value & 0x00000008) >> 3; | |
822 | entry->sa = (mem_value & 0x00000007); | |
823 | } | |
824 | } | |
825 | ||
852d481f EI |
826 | int cpu_sh4_is_cached(CPUSH4State * env, target_ulong addr) |
827 | { | |
828 | int n; | |
5ed9a259 | 829 | int use_asid = !(env->mmucr & MMUCR_SV) || !(env->sr & (1u << SR_MD)); |
852d481f EI |
830 | |
831 | /* check area */ | |
5ed9a259 | 832 | if (env->sr & (1u << SR_MD)) { |
67cc32eb | 833 | /* For privileged mode, P2 and P4 area is not cacheable. */ |
852d481f EI |
834 | if ((0xA0000000 <= addr && addr < 0xC0000000) || 0xE0000000 <= addr) |
835 | return 0; | |
836 | } else { | |
67cc32eb | 837 | /* For user mode, only U0 area is cacheable. */ |
852d481f EI |
838 | if (0x80000000 <= addr) |
839 | return 0; | |
840 | } | |
841 | ||
842 | /* | |
843 | * TODO : Evaluate CCR and check if the cache is on or off. | |
844 | * Now CCR is not in CPUSH4State, but in SH7750State. | |
4abf79a4 | 845 | * When you move the ccr into CPUSH4State, the code will be |
852d481f EI |
846 | * as follows. |
847 | */ | |
848 | #if 0 | |
849 | /* check if operand cache is enabled or not. */ | |
850 | if (!(env->ccr & 1)) | |
851 | return 0; | |
852 | #endif | |
853 | ||
854 | /* if MMU is off, no check for TLB. */ | |
855 | if (env->mmucr & MMUCR_AT) | |
856 | return 1; | |
857 | ||
858 | /* check TLB */ | |
859 | n = find_tlb_entry(env, addr, env->itlb, ITLB_SIZE, use_asid); | |
860 | if (n >= 0) | |
861 | return env->itlb[n].c; | |
862 | ||
863 | n = find_tlb_entry(env, addr, env->utlb, UTLB_SIZE, use_asid); | |
864 | if (n >= 0) | |
865 | return env->utlb[n].c; | |
866 | ||
867 | return 0; | |
868 | } | |
869 | ||
355fb23d | 870 | #endif |
f47ede19 RH |
871 | |
872 | bool superh_cpu_exec_interrupt(CPUState *cs, int interrupt_request) | |
873 | { | |
874 | if (interrupt_request & CPU_INTERRUPT_HARD) { | |
5c6f3eb7 AJ |
875 | SuperHCPU *cpu = SUPERH_CPU(cs); |
876 | CPUSH4State *env = &cpu->env; | |
877 | ||
878 | /* Delay slots are indivisible, ignore interrupts */ | |
879 | if (env->flags & DELAY_SLOT_MASK) { | |
880 | return false; | |
881 | } else { | |
882 | superh_cpu_do_interrupt(cs); | |
883 | return true; | |
884 | } | |
f47ede19 RH |
885 | } |
886 | return false; | |
887 | } |