[sh4] delay slot bug fix
[qemu] / target-sh4 / helper.c
1 /*
2  *  SH4 emulation
3  *
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
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
19  */
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <signal.h>
26 #include <assert.h>
27
28 #include "cpu.h"
29 #include "exec-all.h"
30 #include "hw/sh_intc.h"
31
32 #if defined(CONFIG_USER_ONLY)
33
34 void do_interrupt (CPUState *env)
35 {
36   env->exception_index = -1;
37 }
38
39 int cpu_sh4_handle_mmu_fault(CPUState * env, target_ulong address, int rw,
40                              int mmu_idx, int is_softmmu)
41 {
42     env->tea = address;
43     env->exception_index = 0;
44     switch (rw) {
45     case 0:
46         env->tea = address;
47         env->exception_index = 0x0a0;
48         break;
49     case 1:
50         env->tea = address;
51         env->exception_index = 0x0c0;
52         break;
53     }
54     return 1;
55 }
56
57 target_phys_addr_t cpu_get_phys_page_debug(CPUState * env, target_ulong addr)
58 {
59     return addr;
60 }
61
62 #else /* !CONFIG_USER_ONLY */
63
64 #define MMU_OK                   0
65 #define MMU_ITLB_MISS            (-1)
66 #define MMU_ITLB_MULTIPLE        (-2)
67 #define MMU_ITLB_VIOLATION       (-3)
68 #define MMU_DTLB_MISS_READ       (-4)
69 #define MMU_DTLB_MISS_WRITE      (-5)
70 #define MMU_DTLB_INITIAL_WRITE   (-6)
71 #define MMU_DTLB_VIOLATION_READ  (-7)
72 #define MMU_DTLB_VIOLATION_WRITE (-8)
73 #define MMU_DTLB_MULTIPLE        (-9)
74 #define MMU_DTLB_MISS            (-10)
75
76 void do_interrupt(CPUState * env)
77 {
78     int do_irq = env->interrupt_request & CPU_INTERRUPT_HARD;
79     int do_exp, irq_vector = env->exception_index;
80
81     /* prioritize exceptions over interrupts */
82
83     do_exp = env->exception_index != -1;
84     do_irq = do_irq && (env->exception_index == -1);
85
86     if (env->sr & SR_BL) {
87         if (do_exp && env->exception_index != 0x1e0) {
88             env->exception_index = 0x000; /* masked exception -> reset */
89         }
90         if (do_irq && !env->intr_at_halt) {
91             return; /* masked */
92         }
93         env->intr_at_halt = 0;
94     }
95
96     if (do_irq) {
97         irq_vector = sh_intc_get_pending_vector(env->intc_handle,
98                                                 (env->sr >> 4) & 0xf);
99         if (irq_vector == -1) {
100             return; /* masked */
101         }
102     }
103
104     if (loglevel & CPU_LOG_INT) {
105         const char *expname;
106         switch (env->exception_index) {
107         case 0x0e0:
108             expname = "addr_error";
109             break;
110         case 0x040:
111             expname = "tlb_miss";
112             break;
113         case 0x0a0:
114             expname = "tlb_violation";
115             break;
116         case 0x180:
117             expname = "illegal_instruction";
118             break;
119         case 0x1a0:
120             expname = "slot_illegal_instruction";
121             break;
122         case 0x800:
123             expname = "fpu_disable";
124             break;
125         case 0x820:
126             expname = "slot_fpu";
127             break;
128         case 0x100:
129             expname = "data_write";
130             break;
131         case 0x060:
132             expname = "dtlb_miss_write";
133             break;
134         case 0x0c0:
135             expname = "dtlb_violation_write";
136             break;
137         case 0x120:
138             expname = "fpu_exception";
139             break;
140         case 0x080:
141             expname = "initial_page_write";
142             break;
143         case 0x160:
144             expname = "trapa";
145             break;
146         default:
147             expname = do_irq ? "interrupt" : "???";
148             break;
149         }
150         fprintf(logfile, "exception 0x%03x [%s] raised\n",
151                 irq_vector, expname);
152         cpu_dump_state(env, logfile, fprintf, 0);
153     }
154
155     env->ssr = env->sr;
156     env->spc = env->pc;
157     env->sgr = env->gregs[15];
158     env->sr |= SR_BL | SR_MD | SR_RB;
159
160     if (env->flags & (DELAY_SLOT | DELAY_SLOT_CONDITIONAL)) {
161         /* Branch instruction should be executed again before delay slot. */
162         env->spc -= 2;
163         /* Clear flags for exception/interrupt routine. */
164         env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL | DELAY_SLOT_TRUE);
165     }
166     if (env->flags & DELAY_SLOT_CLEARME)
167         env->flags = 0;
168
169     if (do_exp) {
170         env->expevt = env->exception_index;
171         switch (env->exception_index) {
172         case 0x000:
173         case 0x020:
174         case 0x140:
175             env->sr &= ~SR_FD;
176             env->sr |= 0xf << 4; /* IMASK */
177             env->pc = 0xa0000000;
178             break;
179         case 0x040:
180         case 0x060:
181             env->pc = env->vbr + 0x400;
182             break;
183         case 0x160:
184             env->spc += 2; /* special case for TRAPA */
185             /* fall through */
186         default:
187             env->pc = env->vbr + 0x100;
188             break;
189         }
190         return;
191     }
192
193     if (do_irq) {
194         env->intevt = irq_vector;
195         env->pc = env->vbr + 0x600;
196         return;
197     }
198 }
199
200 static void update_itlb_use(CPUState * env, int itlbnb)
201 {
202     uint8_t or_mask = 0, and_mask = (uint8_t) - 1;
203
204     switch (itlbnb) {
205     case 0:
206         and_mask = 0x1f;
207         break;
208     case 1:
209         and_mask = 0xe7;
210         or_mask = 0x80;
211         break;
212     case 2:
213         and_mask = 0xfb;
214         or_mask = 0x50;
215         break;
216     case 3:
217         or_mask = 0x2c;
218         break;
219     }
220
221     env->mmucr &= (and_mask << 24) | 0x00ffffff;
222     env->mmucr |= (or_mask << 24);
223 }
224
225 static int itlb_replacement(CPUState * env)
226 {
227     if ((env->mmucr & 0xe0000000) == 0xe0000000)
228         return 0;
229     if ((env->mmucr & 0x98000000) == 0x18000000)
230         return 1;
231     if ((env->mmucr & 0x54000000) == 0x04000000)
232         return 2;
233     if ((env->mmucr & 0x2c000000) == 0x00000000)
234         return 3;
235     assert(0);
236 }
237
238 /* Find the corresponding entry in the right TLB
239    Return entry, MMU_DTLB_MISS or MMU_DTLB_MULTIPLE
240 */
241 static int find_tlb_entry(CPUState * env, target_ulong address,
242                           tlb_t * entries, uint8_t nbtlb, int use_asid)
243 {
244     int match = MMU_DTLB_MISS;
245     uint32_t start, end;
246     uint8_t asid;
247     int i;
248
249     asid = env->pteh & 0xff;
250
251     for (i = 0; i < nbtlb; i++) {
252         if (!entries[i].v)
253             continue;           /* Invalid entry */
254         if (use_asid && entries[i].asid != asid && !entries[i].sh)
255             continue;           /* Bad ASID */
256 #if 0
257         switch (entries[i].sz) {
258         case 0:
259             size = 1024;        /* 1kB */
260             break;
261         case 1:
262             size = 4 * 1024;    /* 4kB */
263             break;
264         case 2:
265             size = 64 * 1024;   /* 64kB */
266             break;
267         case 3:
268             size = 1024 * 1024; /* 1MB */
269             break;
270         default:
271             assert(0);
272         }
273 #endif
274         start = (entries[i].vpn << 10) & ~(entries[i].size - 1);
275         end = start + entries[i].size - 1;
276         if (address >= start && address <= end) {       /* Match */
277             if (match != MMU_DTLB_MISS)
278                 return MMU_DTLB_MULTIPLE;       /* Multiple match */
279             match = i;
280         }
281     }
282     return match;
283 }
284
285 /* Find itlb entry - update itlb from utlb if necessary and asked for
286    Return entry, MMU_ITLB_MISS, MMU_ITLB_MULTIPLE or MMU_DTLB_MULTIPLE
287    Update the itlb from utlb if update is not 0
288 */
289 int find_itlb_entry(CPUState * env, target_ulong address,
290                     int use_asid, int update)
291 {
292     int e, n;
293
294     e = find_tlb_entry(env, address, env->itlb, ITLB_SIZE, use_asid);
295     if (e == MMU_DTLB_MULTIPLE)
296         e = MMU_ITLB_MULTIPLE;
297     else if (e == MMU_DTLB_MISS && update) {
298         e = find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid);
299         if (e >= 0) {
300             n = itlb_replacement(env);
301             env->itlb[n] = env->utlb[e];
302             e = n;
303         } else if (e == MMU_DTLB_MISS)
304             e = MMU_ITLB_MISS;
305     } else if (e == MMU_DTLB_MISS)
306         e = MMU_ITLB_MISS;
307     if (e >= 0)
308         update_itlb_use(env, e);
309     return e;
310 }
311
312 /* Find utlb entry
313    Return entry, MMU_DTLB_MISS, MMU_DTLB_MULTIPLE */
314 int find_utlb_entry(CPUState * env, target_ulong address, int use_asid)
315 {
316     uint8_t urb, urc;
317
318     /* Increment URC */
319     urb = ((env->mmucr) >> 18) & 0x3f;
320     urc = ((env->mmucr) >> 10) & 0x3f;
321     urc++;
322     if (urc == urb || urc == UTLB_SIZE - 1)
323         urc = 0;
324     env->mmucr = (env->mmucr & 0xffff03ff) | (urc << 10);
325
326     /* Return entry */
327     return find_tlb_entry(env, address, env->utlb, UTLB_SIZE, use_asid);
328 }
329
330 /* Match address against MMU
331    Return MMU_OK, MMU_DTLB_MISS_READ, MMU_DTLB_MISS_WRITE,
332    MMU_DTLB_INITIAL_WRITE, MMU_DTLB_VIOLATION_READ,
333    MMU_DTLB_VIOLATION_WRITE, MMU_ITLB_MISS,
334    MMU_ITLB_MULTIPLE, MMU_ITLB_VIOLATION
335 */
336 static int get_mmu_address(CPUState * env, target_ulong * physical,
337                            int *prot, target_ulong address,
338                            int rw, int access_type)
339 {
340     int use_asid, is_code, n;
341     tlb_t *matching = NULL;
342
343     use_asid = (env->mmucr & MMUCR_SV) == 0 && (env->sr & SR_MD) == 0;
344     is_code = env->pc == address;       /* Hack */
345
346     /* Use a hack to find if this is an instruction or data access */
347     if (env->pc == address && !(rw & PAGE_WRITE)) {
348         n = find_itlb_entry(env, address, use_asid, 1);
349         if (n >= 0) {
350             matching = &env->itlb[n];
351             if ((env->sr & SR_MD) & !(matching->pr & 2))
352                 n = MMU_ITLB_VIOLATION;
353             else
354                 *prot = PAGE_READ;
355         }
356     } else {
357         n = find_utlb_entry(env, address, use_asid);
358         if (n >= 0) {
359             matching = &env->utlb[n];
360             switch ((matching->pr << 1) | ((env->sr & SR_MD) ? 1 : 0)) {
361             case 0:             /* 000 */
362             case 2:             /* 010 */
363                 n = (rw & PAGE_WRITE) ? MMU_DTLB_VIOLATION_WRITE :
364                     MMU_DTLB_VIOLATION_READ;
365                 break;
366             case 1:             /* 001 */
367             case 4:             /* 100 */
368             case 5:             /* 101 */
369                 if (rw & PAGE_WRITE)
370                     n = MMU_DTLB_VIOLATION_WRITE;
371                 else
372                     *prot = PAGE_READ;
373                 break;
374             case 3:             /* 011 */
375             case 6:             /* 110 */
376             case 7:             /* 111 */
377                 *prot = rw & (PAGE_READ | PAGE_WRITE);
378                 break;
379             }
380         } else if (n == MMU_DTLB_MISS) {
381             n = (rw & PAGE_WRITE) ? MMU_DTLB_MISS_WRITE :
382                 MMU_DTLB_MISS_READ;
383         }
384     }
385     if (n >= 0) {
386         *physical = ((matching->ppn << 10) & ~(matching->size - 1)) |
387             (address & (matching->size - 1));
388         if ((rw & PAGE_WRITE) & !matching->d)
389             n = MMU_DTLB_INITIAL_WRITE;
390         else
391             n = MMU_OK;
392     }
393     return n;
394 }
395
396 int get_physical_address(CPUState * env, target_ulong * physical,
397                          int *prot, target_ulong address,
398                          int rw, int access_type)
399 {
400     /* P1, P2 and P4 areas do not use translation */
401     if ((address >= 0x80000000 && address < 0xc0000000) ||
402         address >= 0xe0000000) {
403         if (!(env->sr & SR_MD)
404             && (address < 0xe0000000 || address > 0xe4000000)) {
405             /* Unauthorized access in user mode (only store queues are available) */
406             fprintf(stderr, "Unauthorized access\n");
407             return (rw & PAGE_WRITE) ? MMU_DTLB_MISS_WRITE :
408                 MMU_DTLB_MISS_READ;
409         }
410         /* Mask upper 3 bits */
411         *physical = address & 0x1FFFFFFF;
412         *prot = PAGE_READ | PAGE_WRITE;
413         return MMU_OK;
414     }
415
416     /* If MMU is disabled, return the corresponding physical page */
417     if (!env->mmucr & MMUCR_AT) {
418         *physical = address & 0x1FFFFFFF;
419         *prot = PAGE_READ | PAGE_WRITE;
420         return MMU_OK;
421     }
422
423     /* We need to resort to the MMU */
424     return get_mmu_address(env, physical, prot, address, rw, access_type);
425 }
426
427 int cpu_sh4_handle_mmu_fault(CPUState * env, target_ulong address, int rw,
428                              int mmu_idx, int is_softmmu)
429 {
430     target_ulong physical, page_offset, page_size;
431     int prot, ret, access_type;
432
433     switch (rw) {
434     case 0:
435         rw = PAGE_READ;
436         break;
437     case 1:
438         rw = PAGE_WRITE;
439         break;
440     case 2: /* READ_ACCESS_TYPE == 2 defined in softmmu_template.h */
441         rw = PAGE_READ;
442         break;
443     default:
444         /* fatal error */
445         assert(0);
446     }
447
448     /* XXXXX */
449 #if 0
450     fprintf(stderr, "%s pc %08x ad %08x rw %d mmu_idx %d smmu %d\n",
451             __func__, env->pc, address, rw, mmu_idx, is_softmmu);
452 #endif
453
454     access_type = ACCESS_INT;
455     ret =
456         get_physical_address(env, &physical, &prot, address, rw,
457                              access_type);
458
459     if (ret != MMU_OK) {
460         env->tea = address;
461         switch (ret) {
462         case MMU_ITLB_MISS:
463         case MMU_DTLB_MISS_READ:
464             env->exception_index = 0x040;
465             break;
466         case MMU_DTLB_MULTIPLE:
467         case MMU_ITLB_MULTIPLE:
468             env->exception_index = 0x140;
469             break;
470         case MMU_ITLB_VIOLATION:
471             env->exception_index = 0x0a0;
472             break;
473         case MMU_DTLB_MISS_WRITE:
474             env->exception_index = 0x060;
475             break;
476         case MMU_DTLB_INITIAL_WRITE:
477             env->exception_index = 0x080;
478             break;
479         case MMU_DTLB_VIOLATION_READ:
480             env->exception_index = 0x0a0;
481             break;
482         case MMU_DTLB_VIOLATION_WRITE:
483             env->exception_index = 0x0c0;
484             break;
485         default:
486             assert(0);
487         }
488         return 1;
489     }
490
491     page_size = TARGET_PAGE_SIZE;
492     page_offset =
493         (address - (address & TARGET_PAGE_MASK)) & ~(page_size - 1);
494     address = (address & TARGET_PAGE_MASK) + page_offset;
495     physical = (physical & TARGET_PAGE_MASK) + page_offset;
496
497     return tlb_set_page(env, address, physical, prot, mmu_idx, is_softmmu);
498 }
499
500 target_phys_addr_t cpu_get_phys_page_debug(CPUState * env, target_ulong addr)
501 {
502     target_ulong physical;
503     int prot;
504
505     get_physical_address(env, &physical, &prot, addr, PAGE_READ, 0);
506     return physical;
507 }
508
509 void cpu_load_tlb(CPUState * env)
510 {
511     int n = cpu_mmucr_urc(env->mmucr);
512     tlb_t * entry = &env->utlb[n];
513
514     /* Take values into cpu status from registers. */
515     entry->asid = (uint8_t)cpu_pteh_asid(env->pteh);
516     entry->vpn  = cpu_pteh_vpn(env->pteh);
517     entry->v    = (uint8_t)cpu_ptel_v(env->ptel);
518     entry->ppn  = cpu_ptel_ppn(env->ptel);
519     entry->sz   = (uint8_t)cpu_ptel_sz(env->ptel);
520     switch (entry->sz) {
521     case 0: /* 00 */
522         entry->size = 1024; /* 1K */
523         break;
524     case 1: /* 01 */
525         entry->size = 1024 * 4; /* 4K */
526         break;
527     case 2: /* 10 */
528         entry->size = 1024 * 64; /* 64K */
529         break;
530     case 3: /* 11 */
531         entry->size = 1024 * 1024; /* 1M */
532         break;
533     default:
534         assert(0);
535         break;
536     }
537     entry->sh   = (uint8_t)cpu_ptel_sh(env->ptel);
538     entry->c    = (uint8_t)cpu_ptel_c(env->ptel);
539     entry->pr   = (uint8_t)cpu_ptel_pr(env->ptel);
540     entry->d    = (uint8_t)cpu_ptel_d(env->ptel);
541     entry->wt   = (uint8_t)cpu_ptel_wt(env->ptel);
542     entry->sa   = (uint8_t)cpu_ptea_sa(env->ptea);
543     entry->tc   = (uint8_t)cpu_ptea_tc(env->ptea);
544 }
545
546 #endif