Statistics
| Branch: | Revision:

root / target-mips / helper.c @ bc9ed47b

History | View | Annotate | Download (13.3 kB)

1 6af0bf9c bellard
/*
2 6af0bf9c bellard
 *  MIPS emulation helpers for qemu.
3 6af0bf9c bellard
 * 
4 6af0bf9c bellard
 *  Copyright (c) 2004-2005 Jocelyn Mayer
5 6af0bf9c bellard
 *
6 6af0bf9c bellard
 * This library is free software; you can redistribute it and/or
7 6af0bf9c bellard
 * modify it under the terms of the GNU Lesser General Public
8 6af0bf9c bellard
 * License as published by the Free Software Foundation; either
9 6af0bf9c bellard
 * version 2 of the License, or (at your option) any later version.
10 6af0bf9c bellard
 *
11 6af0bf9c bellard
 * This library is distributed in the hope that it will be useful,
12 6af0bf9c bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 6af0bf9c bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 6af0bf9c bellard
 * Lesser General Public License for more details.
15 6af0bf9c bellard
 *
16 6af0bf9c bellard
 * You should have received a copy of the GNU Lesser General Public
17 6af0bf9c bellard
 * License along with this library; if not, write to the Free Software
18 6af0bf9c bellard
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 6af0bf9c bellard
 */
20 6af0bf9c bellard
#include "exec.h"
21 6af0bf9c bellard
22 6af0bf9c bellard
/* MIPS32 4K MMU emulation */
23 9fb63ac2 bellard
#ifdef MIPS_USES_R4K_TLB
24 6af0bf9c bellard
static int map_address (CPUState *env, target_ulong *physical, int *prot,
25 6af0bf9c bellard
                        target_ulong address, int rw, int access_type)
26 6af0bf9c bellard
{
27 6af0bf9c bellard
    tlb_t *tlb;
28 6af0bf9c bellard
    target_ulong tag;
29 6af0bf9c bellard
    uint8_t ASID;
30 6af0bf9c bellard
    int i, n;
31 6af0bf9c bellard
    int ret;
32 6af0bf9c bellard
33 6af0bf9c bellard
    ret = -2;
34 6af0bf9c bellard
    tag = (address & 0xFFFFE000);
35 6af0bf9c bellard
    ASID = env->CP0_EntryHi & 0x000000FF;
36 6af0bf9c bellard
    for (i = 0; i < 16; i++) {
37 6af0bf9c bellard
        tlb = &env->tlb[i];
38 6af0bf9c bellard
        /* Check ASID, virtual page number & size */
39 6af0bf9c bellard
        if ((tlb->G == 1 || tlb->ASID == ASID) &&
40 6af0bf9c bellard
            tlb->VPN == tag && address < tlb->end) {
41 6af0bf9c bellard
            /* TLB match */
42 6af0bf9c bellard
            n = (address >> 12) & 1;
43 6af0bf9c bellard
            /* Check access rights */
44 6af0bf9c bellard
            if ((tlb->V[n] & 2) && (rw == 0 || (tlb->D[n] & 4))) {
45 6af0bf9c bellard
                *physical = tlb->PFN[n] | (address & 0xFFF);
46 9fb63ac2 bellard
                *prot = PAGE_READ;
47 6af0bf9c bellard
                if (tlb->D[n])
48 9fb63ac2 bellard
                    *prot |= PAGE_WRITE;
49 6af0bf9c bellard
                return 0;
50 6af0bf9c bellard
            } else if (!(tlb->V[n] & 2)) {
51 6af0bf9c bellard
                return -3;
52 6af0bf9c bellard
            } else {
53 6af0bf9c bellard
                return -4;
54 6af0bf9c bellard
            }
55 6af0bf9c bellard
        }
56 6af0bf9c bellard
    }
57 6af0bf9c bellard
58 6af0bf9c bellard
    return ret;
59 6af0bf9c bellard
}
60 6af0bf9c bellard
#endif
61 6af0bf9c bellard
62 6af0bf9c bellard
int get_physical_address (CPUState *env, target_ulong *physical, int *prot,
63 6af0bf9c bellard
                          target_ulong address, int rw, int access_type)
64 6af0bf9c bellard
{
65 6af0bf9c bellard
    int user_mode;
66 6af0bf9c bellard
    int ret;
67 6af0bf9c bellard
68 6af0bf9c bellard
    /* User mode can only access useg */
69 6af0bf9c bellard
    user_mode = ((env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_UM) ? 1 : 0;
70 6af0bf9c bellard
#if 0
71 6af0bf9c bellard
    if (logfile) {
72 6af0bf9c bellard
        fprintf(logfile, "user mode %d h %08x\n",
73 6af0bf9c bellard
                user_mode, env->hflags);
74 6af0bf9c bellard
    }
75 6af0bf9c bellard
#endif
76 6af0bf9c bellard
    if (user_mode && address > 0x7FFFFFFFUL)
77 6af0bf9c bellard
        return -1;
78 6af0bf9c bellard
    ret = 0;
79 6af0bf9c bellard
    if (address < 0x80000000UL) {
80 9fb63ac2 bellard
        if (!(env->hflags & MIPS_HFLAG_ERL)) {
81 9fb63ac2 bellard
#ifdef MIPS_USES_R4K_TLB
82 9fb63ac2 bellard
            ret = map_address(env, physical, prot, address, rw, access_type);
83 6af0bf9c bellard
#else
84 6af0bf9c bellard
            *physical = address + 0x40000000UL;
85 6af0bf9c bellard
            *prot = PAGE_READ | PAGE_WRITE;
86 6af0bf9c bellard
#endif
87 6af0bf9c bellard
        } else {
88 6af0bf9c bellard
            *physical = address;
89 6af0bf9c bellard
            *prot = PAGE_READ | PAGE_WRITE;
90 6af0bf9c bellard
        }
91 6af0bf9c bellard
    } else if (address < 0xA0000000UL) {
92 6af0bf9c bellard
        /* kseg0 */
93 6af0bf9c bellard
        /* XXX: check supervisor mode */
94 6af0bf9c bellard
        *physical = address - 0x80000000UL;
95 6af0bf9c bellard
        *prot = PAGE_READ | PAGE_WRITE;
96 6af0bf9c bellard
    } else if (address < 0xC0000000UL) {
97 6af0bf9c bellard
        /* kseg1 */
98 6af0bf9c bellard
        /* XXX: check supervisor mode */
99 6af0bf9c bellard
        *physical = address - 0xA0000000UL;
100 6af0bf9c bellard
        *prot = PAGE_READ | PAGE_WRITE;
101 6af0bf9c bellard
    } else if (address < 0xE0000000UL) {
102 6af0bf9c bellard
        /* kseg2 */
103 9fb63ac2 bellard
#ifdef MIPS_USES_R4K_TLB
104 9fb63ac2 bellard
        ret = map_address(env, physical, prot, address, rw, access_type);
105 6af0bf9c bellard
#else
106 6af0bf9c bellard
        *physical = address;
107 6af0bf9c bellard
        *prot = PAGE_READ | PAGE_WRITE;
108 6af0bf9c bellard
#endif
109 6af0bf9c bellard
    } else {
110 6af0bf9c bellard
        /* kseg3 */
111 6af0bf9c bellard
        /* XXX: check supervisor mode */
112 6af0bf9c bellard
        /* XXX: debug segment is not emulated */
113 9fb63ac2 bellard
#ifdef MIPS_USES_R4K_TLB
114 9fb63ac2 bellard
        ret = map_address(env, physical, prot, address, rw, access_type);
115 6af0bf9c bellard
#else
116 6af0bf9c bellard
        *physical = address;
117 6af0bf9c bellard
        *prot = PAGE_READ | PAGE_WRITE;
118 6af0bf9c bellard
#endif
119 6af0bf9c bellard
    }
120 6af0bf9c bellard
#if 0
121 6af0bf9c bellard
    if (logfile) {
122 6af0bf9c bellard
        fprintf(logfile, "%08x %d %d => %08x %d (%d)\n", address, rw,
123 6af0bf9c bellard
                access_type, *physical, *prot, ret);
124 6af0bf9c bellard
    }
125 6af0bf9c bellard
#endif
126 6af0bf9c bellard
127 6af0bf9c bellard
    return ret;
128 6af0bf9c bellard
}
129 6af0bf9c bellard
130 6af0bf9c bellard
#if defined(CONFIG_USER_ONLY) 
131 6af0bf9c bellard
target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
132 6af0bf9c bellard
{
133 6af0bf9c bellard
    return addr;
134 6af0bf9c bellard
}
135 6af0bf9c bellard
#else
136 6af0bf9c bellard
target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
137 6af0bf9c bellard
{
138 6af0bf9c bellard
    target_ulong phys_addr;
139 6af0bf9c bellard
    int prot;
140 6af0bf9c bellard
141 6af0bf9c bellard
    if (get_physical_address(env, &phys_addr, &prot, addr, 0, ACCESS_INT) != 0)
142 6af0bf9c bellard
        return -1;
143 6af0bf9c bellard
    return phys_addr;
144 6af0bf9c bellard
}
145 6af0bf9c bellard
#endif
146 6af0bf9c bellard
147 6af0bf9c bellard
#if !defined(CONFIG_USER_ONLY) 
148 6af0bf9c bellard
149 6af0bf9c bellard
#define MMUSUFFIX _mmu
150 6af0bf9c bellard
#define GETPC() (__builtin_return_address(0))
151 6af0bf9c bellard
152 6af0bf9c bellard
#define SHIFT 0
153 6af0bf9c bellard
#include "softmmu_template.h"
154 6af0bf9c bellard
155 6af0bf9c bellard
#define SHIFT 1
156 6af0bf9c bellard
#include "softmmu_template.h"
157 6af0bf9c bellard
158 6af0bf9c bellard
#define SHIFT 2
159 6af0bf9c bellard
#include "softmmu_template.h"
160 6af0bf9c bellard
161 6af0bf9c bellard
#define SHIFT 3
162 6af0bf9c bellard
#include "softmmu_template.h"
163 6af0bf9c bellard
164 6af0bf9c bellard
void tlb_fill (target_ulong addr, int is_write, int is_user, void *retaddr)
165 6af0bf9c bellard
{
166 6af0bf9c bellard
    TranslationBlock *tb;
167 6af0bf9c bellard
    CPUState *saved_env;
168 6af0bf9c bellard
    unsigned long pc;
169 6af0bf9c bellard
    int ret;
170 6af0bf9c bellard
171 6af0bf9c bellard
    /* XXX: hack to restore env in all cases, even if not called from
172 6af0bf9c bellard
       generated code */
173 6af0bf9c bellard
    saved_env = env;
174 6af0bf9c bellard
    env = cpu_single_env;
175 6af0bf9c bellard
    ret = cpu_mips_handle_mmu_fault(env, addr, is_write, is_user, 1);
176 6af0bf9c bellard
    if (ret) {
177 6af0bf9c bellard
        if (retaddr) {
178 6af0bf9c bellard
            /* now we have a real cpu fault */
179 6af0bf9c bellard
            pc = (unsigned long)retaddr;
180 6af0bf9c bellard
            tb = tb_find_pc(pc);
181 6af0bf9c bellard
            if (tb) {
182 6af0bf9c bellard
                /* the PC is inside the translated code. It means that we have
183 6af0bf9c bellard
                   a virtual CPU fault */
184 6af0bf9c bellard
                cpu_restore_state(tb, env, pc, NULL);
185 6af0bf9c bellard
            }
186 6af0bf9c bellard
        }
187 6af0bf9c bellard
        do_raise_exception_err(env->exception_index, env->error_code);
188 6af0bf9c bellard
    }
189 6af0bf9c bellard
    env = saved_env;
190 6af0bf9c bellard
}
191 6af0bf9c bellard
192 6af0bf9c bellard
void cpu_mips_init_mmu (CPUState *env)
193 6af0bf9c bellard
{
194 6af0bf9c bellard
}
195 6af0bf9c bellard
196 6af0bf9c bellard
#endif /* !defined(CONFIG_USER_ONLY) */
197 6af0bf9c bellard
198 6af0bf9c bellard
int cpu_mips_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
199 6af0bf9c bellard
                               int is_user, int is_softmmu)
200 6af0bf9c bellard
{
201 6af0bf9c bellard
    target_ulong physical;
202 6af0bf9c bellard
    int prot;
203 6af0bf9c bellard
    int exception = 0, error_code = 0;
204 6af0bf9c bellard
    int access_type;
205 6af0bf9c bellard
    int ret = 0;
206 6af0bf9c bellard
207 6af0bf9c bellard
    if (logfile) {
208 6af0bf9c bellard
        cpu_dump_state(env, logfile, fprintf, 0);
209 6af0bf9c bellard
        fprintf(logfile, "%s pc %08x ad %08x rw %d is_user %d smmu %d\n",
210 6af0bf9c bellard
                __func__, env->PC, address, rw, is_user, is_softmmu);
211 6af0bf9c bellard
    }
212 6af0bf9c bellard
    /* data access */
213 6af0bf9c bellard
    /* XXX: put correct access by using cpu_restore_state()
214 6af0bf9c bellard
       correctly */
215 6af0bf9c bellard
    access_type = ACCESS_INT;
216 6af0bf9c bellard
    if (env->user_mode_only) {
217 6af0bf9c bellard
        /* user mode only emulation */
218 6af0bf9c bellard
        ret = -2;
219 6af0bf9c bellard
        goto do_fault;
220 6af0bf9c bellard
    }
221 6af0bf9c bellard
    ret = get_physical_address(env, &physical, &prot,
222 6af0bf9c bellard
                               address, rw, access_type);
223 6af0bf9c bellard
    if (logfile) {
224 6af0bf9c bellard
        fprintf(logfile, "%s address=%08x ret %d physical %08x prot %d\n",
225 6af0bf9c bellard
                __func__, address, ret, physical, prot);
226 6af0bf9c bellard
    }
227 6af0bf9c bellard
    if (ret == 0) {
228 6af0bf9c bellard
        ret = tlb_set_page(env, address & ~0xFFF, physical & ~0xFFF, prot,
229 6af0bf9c bellard
                           is_user, is_softmmu);
230 6af0bf9c bellard
    } else if (ret < 0) {
231 6af0bf9c bellard
    do_fault:
232 6af0bf9c bellard
        switch (ret) {
233 6af0bf9c bellard
        default:
234 6af0bf9c bellard
        case -1:
235 6af0bf9c bellard
            /* Reference to kernel address from user mode or supervisor mode */
236 6af0bf9c bellard
            /* Reference to supervisor address from user mode */
237 6af0bf9c bellard
            if (rw)
238 6af0bf9c bellard
                exception = EXCP_AdES;
239 6af0bf9c bellard
            else
240 6af0bf9c bellard
                exception = EXCP_AdEL;
241 6af0bf9c bellard
            break;
242 6af0bf9c bellard
        case -2:
243 6af0bf9c bellard
            /* No TLB match for a mapped address */
244 6af0bf9c bellard
            if (rw)
245 6af0bf9c bellard
                exception = EXCP_TLBS;
246 6af0bf9c bellard
            else
247 6af0bf9c bellard
                exception = EXCP_TLBL;
248 6af0bf9c bellard
            error_code = 1;
249 6af0bf9c bellard
            break;
250 6af0bf9c bellard
        case -3:
251 6af0bf9c bellard
            /* TLB match with no valid bit */
252 6af0bf9c bellard
            if (rw)
253 6af0bf9c bellard
                exception = EXCP_TLBS;
254 6af0bf9c bellard
            else
255 6af0bf9c bellard
                exception = EXCP_TLBL;
256 6af0bf9c bellard
            error_code = 0;
257 6af0bf9c bellard
            break;
258 6af0bf9c bellard
        case -4:
259 6af0bf9c bellard
            /* TLB match but 'D' bit is cleared */
260 6af0bf9c bellard
            exception = EXCP_LTLBL;
261 6af0bf9c bellard
            break;
262 6af0bf9c bellard
                
263 6af0bf9c bellard
        }
264 6af0bf9c bellard
        if (ret == -2) {
265 6af0bf9c bellard
            exception = EXCP_AdEL;
266 6af0bf9c bellard
        }
267 6af0bf9c bellard
        /* Raise exception */
268 6af0bf9c bellard
        env->CP0_BadVAddr = address;
269 6af0bf9c bellard
        env->CP0_Context =
270 6af0bf9c bellard
            (env->CP0_Context & 0x00000FFF) | (address & 0xFFFFF000);
271 6af0bf9c bellard
        env->CP0_EntryHi =
272 6af0bf9c bellard
            (env->CP0_EntryHi & 0x00000FFF) | (address & 0xFFFFF000);
273 6af0bf9c bellard
        env->exception_index = exception;
274 6af0bf9c bellard
        env->error_code = error_code;
275 6af0bf9c bellard
        ret = 1;
276 6af0bf9c bellard
    }
277 6af0bf9c bellard
278 6af0bf9c bellard
    return ret;
279 6af0bf9c bellard
}
280 6af0bf9c bellard
281 6af0bf9c bellard
void do_interrupt (CPUState *env)
282 6af0bf9c bellard
{
283 6af0bf9c bellard
    target_ulong pc, offset;
284 6af0bf9c bellard
    int cause = -1;
285 6af0bf9c bellard
286 6af0bf9c bellard
    if (logfile && env->exception_index != EXCP_EXT_INTERRUPT) {
287 6af0bf9c bellard
        fprintf(logfile, "%s enter: PC %08x EPC %08x cause %d excp %d\n",
288 6af0bf9c bellard
                __func__, env->PC, env->CP0_EPC, cause, env->exception_index);
289 6af0bf9c bellard
    }
290 6af0bf9c bellard
    if (env->exception_index == EXCP_EXT_INTERRUPT &&
291 6af0bf9c bellard
        (env->hflags & MIPS_HFLAG_DM))
292 6af0bf9c bellard
        env->exception_index = EXCP_DINT;
293 6af0bf9c bellard
    offset = 0x180;
294 6af0bf9c bellard
    switch (env->exception_index) {
295 6af0bf9c bellard
    case EXCP_DSS:
296 6af0bf9c bellard
        env->CP0_Debug |= 1 << CP0DB_DSS;
297 6af0bf9c bellard
        /* Debug single step cannot be raised inside a delay slot and
298 6af0bf9c bellard
         * resume will always occur on the next instruction
299 6af0bf9c bellard
         * (but we assume the pc has always been updated during
300 6af0bf9c bellard
         *  code translation).
301 6af0bf9c bellard
         */
302 6af0bf9c bellard
        env->CP0_DEPC = env->PC;
303 6af0bf9c bellard
        goto enter_debug_mode;
304 6af0bf9c bellard
    case EXCP_DINT:
305 6af0bf9c bellard
        env->CP0_Debug |= 1 << CP0DB_DINT;
306 6af0bf9c bellard
        goto set_DEPC;
307 6af0bf9c bellard
    case EXCP_DIB:
308 6af0bf9c bellard
        env->CP0_Debug |= 1 << CP0DB_DIB;
309 6af0bf9c bellard
        goto set_DEPC;
310 6af0bf9c bellard
    case EXCP_DBp:
311 6af0bf9c bellard
        env->CP0_Debug |= 1 << CP0DB_DBp;
312 6af0bf9c bellard
        goto set_DEPC;
313 6af0bf9c bellard
    case EXCP_DDBS:
314 6af0bf9c bellard
        env->CP0_Debug |= 1 << CP0DB_DDBS;
315 6af0bf9c bellard
        goto set_DEPC;
316 6af0bf9c bellard
    case EXCP_DDBL:
317 6af0bf9c bellard
        env->CP0_Debug |= 1 << CP0DB_DDBL;
318 6af0bf9c bellard
        goto set_DEPC;
319 6af0bf9c bellard
    set_DEPC:
320 6af0bf9c bellard
        if (env->hflags & MIPS_HFLAG_DS) {
321 6af0bf9c bellard
            /* If the exception was raised from a delay slot,
322 6af0bf9c bellard
             * come back to the jump
323 6af0bf9c bellard
             */
324 6af0bf9c bellard
            env->CP0_DEPC = env->PC - 4;
325 6af0bf9c bellard
        } else {
326 6af0bf9c bellard
            env->CP0_DEPC = env->PC;
327 6af0bf9c bellard
        }
328 6af0bf9c bellard
    enter_debug_mode:
329 6af0bf9c bellard
        env->hflags |= MIPS_HFLAG_DM;
330 6af0bf9c bellard
        /* EJTAG probe trap enable is not implemented... */
331 6af0bf9c bellard
        pc = 0xBFC00480;
332 6af0bf9c bellard
        break;
333 6af0bf9c bellard
    case EXCP_RESET:
334 9fb63ac2 bellard
#ifdef MIPS_USES_R4K_TLB
335 6af0bf9c bellard
        env->CP0_random = MIPS_TLB_NB - 1;
336 6af0bf9c bellard
#endif
337 6af0bf9c bellard
        env->CP0_Wired = 0;
338 6af0bf9c bellard
        env->CP0_Config0 = MIPS_CONFIG0;
339 6af0bf9c bellard
#if defined (MIPS_CONFIG1)
340 6af0bf9c bellard
        env->CP0_Config1 = MIPS_CONFIG1;
341 6af0bf9c bellard
#endif
342 6af0bf9c bellard
#if defined (MIPS_CONFIG2)
343 6af0bf9c bellard
        env->CP0_Config2 = MIPS_CONFIG2;
344 6af0bf9c bellard
#endif
345 6af0bf9c bellard
#if defined (MIPS_CONFIG3)
346 6af0bf9c bellard
        env->CP0_Config3 = MIPS_CONFIG3;
347 6af0bf9c bellard
#endif
348 6af0bf9c bellard
        env->CP0_WatchLo = 0;
349 6af0bf9c bellard
        env->CP0_Status = (1 << CP0St_CU0) | (1 << CP0St_BEV);
350 6af0bf9c bellard
        goto set_error_EPC;
351 6af0bf9c bellard
    case EXCP_SRESET:
352 6af0bf9c bellard
        env->CP0_Status = (1 << CP0St_CU0) | (1 << CP0St_BEV) |
353 6af0bf9c bellard
            (1 << CP0St_SR);
354 6af0bf9c bellard
        env->CP0_WatchLo = 0;
355 6af0bf9c bellard
        goto set_error_EPC;
356 6af0bf9c bellard
    case EXCP_NMI:
357 6af0bf9c bellard
        env->CP0_Status = (1 << CP0St_CU0) | (1 << CP0St_BEV) |
358 6af0bf9c bellard
            (1 << CP0St_NMI);
359 6af0bf9c bellard
    set_error_EPC:
360 6af0bf9c bellard
        env->hflags = MIPS_HFLAG_ERL;
361 6af0bf9c bellard
        if (env->hflags & MIPS_HFLAG_DS) {
362 6af0bf9c bellard
            /* If the exception was raised from a delay slot,
363 6af0bf9c bellard
             * come back to the jump
364 6af0bf9c bellard
             */
365 6af0bf9c bellard
            env->CP0_ErrorEPC = env->PC - 4;
366 6af0bf9c bellard
        } else {
367 6af0bf9c bellard
            env->CP0_ErrorEPC = env->PC;
368 6af0bf9c bellard
        }
369 6af0bf9c bellard
        pc = 0xBFC00000;
370 6af0bf9c bellard
        break;
371 6af0bf9c bellard
    case EXCP_MCHECK:
372 6af0bf9c bellard
        cause = 24;
373 6af0bf9c bellard
        goto set_EPC;
374 6af0bf9c bellard
    case EXCP_EXT_INTERRUPT:
375 6af0bf9c bellard
        cause = 0;
376 6af0bf9c bellard
        if (env->CP0_Cause & (1 << CP0Ca_IV))
377 6af0bf9c bellard
            offset = 0x200;
378 6af0bf9c bellard
        goto set_EPC;
379 6af0bf9c bellard
    case EXCP_DWATCH:
380 6af0bf9c bellard
        cause = 23;
381 6af0bf9c bellard
        /* XXX: TODO: manage defered watch exceptions */
382 6af0bf9c bellard
        goto set_EPC;
383 6af0bf9c bellard
    case EXCP_AdEL:
384 6af0bf9c bellard
    case EXCP_AdES:
385 6af0bf9c bellard
        cause = 4;
386 6af0bf9c bellard
        goto set_EPC;
387 6af0bf9c bellard
    case EXCP_TLBL:
388 6af0bf9c bellard
    case EXCP_TLBF:
389 6af0bf9c bellard
        cause = 2;
390 6af0bf9c bellard
        if (env->error_code == 1 && !(env->hflags & MIPS_HFLAG_EXL))
391 6af0bf9c bellard
            offset = 0x000;
392 6af0bf9c bellard
        goto set_EPC;
393 6af0bf9c bellard
    case EXCP_IBE:
394 6af0bf9c bellard
        cause = 6;
395 6af0bf9c bellard
        goto set_EPC;
396 6af0bf9c bellard
    case EXCP_DBE:
397 6af0bf9c bellard
        cause = 7;
398 6af0bf9c bellard
        goto set_EPC;
399 6af0bf9c bellard
    case EXCP_SYSCALL:
400 6af0bf9c bellard
        cause = 8;
401 6af0bf9c bellard
        goto set_EPC;
402 6af0bf9c bellard
    case EXCP_BREAK:
403 6af0bf9c bellard
        cause = 9;
404 6af0bf9c bellard
        goto set_EPC;
405 6af0bf9c bellard
    case EXCP_RI:
406 6af0bf9c bellard
        cause = 10;
407 6af0bf9c bellard
        goto set_EPC;
408 6af0bf9c bellard
    case EXCP_CpU:
409 6af0bf9c bellard
        cause = 11;
410 6af0bf9c bellard
        /* XXX: fill in the faulty unit number */
411 6af0bf9c bellard
        goto set_EPC;
412 6af0bf9c bellard
    case EXCP_OVERFLOW:
413 6af0bf9c bellard
        cause = 12;
414 6af0bf9c bellard
        goto set_EPC;
415 6af0bf9c bellard
    case EXCP_TRAP:
416 6af0bf9c bellard
        cause = 13;
417 6af0bf9c bellard
        goto set_EPC;
418 6af0bf9c bellard
    case EXCP_LTLBL:
419 6af0bf9c bellard
        cause = 1;
420 6af0bf9c bellard
        goto set_EPC;
421 6af0bf9c bellard
    case EXCP_TLBS:
422 6af0bf9c bellard
        cause = 3;
423 6af0bf9c bellard
    set_EPC:
424 6af0bf9c bellard
        if (env->CP0_Status & (1 << CP0St_BEV)) {
425 6af0bf9c bellard
            pc = 0xBFC00200;
426 6af0bf9c bellard
        } else {
427 6af0bf9c bellard
            pc = 0x80000000;
428 6af0bf9c bellard
        }
429 6af0bf9c bellard
        env->hflags |= MIPS_HFLAG_EXL;
430 6af0bf9c bellard
        pc += offset;
431 6af0bf9c bellard
        env->CP0_Cause = (env->CP0_Cause & ~0x7C) | (cause << 2);
432 6af0bf9c bellard
        if (env->hflags & MIPS_HFLAG_DS) {
433 6af0bf9c bellard
            /* If the exception was raised from a delay slot,
434 6af0bf9c bellard
             * come back to the jump
435 6af0bf9c bellard
             */
436 6af0bf9c bellard
            env->CP0_EPC = env->PC - 4;
437 6af0bf9c bellard
            env->CP0_Cause |= 0x80000000;
438 6af0bf9c bellard
        } else {
439 6af0bf9c bellard
            env->CP0_EPC = env->PC;
440 6af0bf9c bellard
            env->CP0_Cause &= ~0x80000000;
441 6af0bf9c bellard
        }
442 6af0bf9c bellard
        break;
443 6af0bf9c bellard
    default:
444 6af0bf9c bellard
        if (logfile) {
445 6af0bf9c bellard
            fprintf(logfile, "Invalid MIPS exception %d. Exiting\n",
446 6af0bf9c bellard
                    env->exception_index);
447 6af0bf9c bellard
        }
448 6af0bf9c bellard
        printf("Invalid MIPS exception %d. Exiting\n", env->exception_index);
449 6af0bf9c bellard
        exit(1);
450 6af0bf9c bellard
    }
451 6af0bf9c bellard
    env->PC = pc;
452 6af0bf9c bellard
    if (logfile && env->exception_index != EXCP_EXT_INTERRUPT) {
453 6af0bf9c bellard
        fprintf(logfile, "%s: PC %08x EPC %08x cause %d excp %d\n"
454 6af0bf9c bellard
                "    S %08x C %08x A %08x D %08x\n",
455 6af0bf9c bellard
                __func__, env->PC, env->CP0_EPC, cause, env->exception_index,
456 6af0bf9c bellard
                env->CP0_Status, env->CP0_Cause, env->CP0_BadVAddr,
457 6af0bf9c bellard
                env->CP0_DEPC);
458 6af0bf9c bellard
    }
459 6af0bf9c bellard
    env->exception_index = EXCP_NONE;
460 6af0bf9c bellard
}