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