Statistics
| Branch: | Revision:

root / target-mips / helper.c @ 1b2b0af5

History | View | Annotate | Download (12.5 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 e37e863f bellard
#include <stdarg.h>
21 e37e863f bellard
#include <stdlib.h>
22 e37e863f bellard
#include <stdio.h>
23 e37e863f bellard
#include <string.h>
24 e37e863f bellard
#include <inttypes.h>
25 e37e863f bellard
#include <signal.h>
26 e37e863f bellard
#include <assert.h>
27 e37e863f bellard
28 e37e863f bellard
#include "cpu.h"
29 e37e863f bellard
#include "exec-all.h"
30 6af0bf9c bellard
31 6af0bf9c bellard
/* MIPS32 4K MMU emulation */
32 9fb63ac2 bellard
#ifdef MIPS_USES_R4K_TLB
33 6af0bf9c bellard
static int map_address (CPUState *env, target_ulong *physical, int *prot,
34 6af0bf9c bellard
                        target_ulong address, int rw, int access_type)
35 6af0bf9c bellard
{
36 6af0bf9c bellard
    tlb_t *tlb;
37 6af0bf9c bellard
    target_ulong tag;
38 6af0bf9c bellard
    uint8_t ASID;
39 6af0bf9c bellard
    int i, n;
40 6af0bf9c bellard
    int ret;
41 6af0bf9c bellard
42 6af0bf9c bellard
    ret = -2;
43 6af0bf9c bellard
    tag = (address & 0xFFFFE000);
44 6af0bf9c bellard
    ASID = env->CP0_EntryHi & 0x000000FF;
45 7a962d30 bellard
    for (i = 0; i < MIPS_TLB_NB; i++) {
46 6af0bf9c bellard
        tlb = &env->tlb[i];
47 6af0bf9c bellard
        /* Check ASID, virtual page number & size */
48 6af0bf9c bellard
        if ((tlb->G == 1 || tlb->ASID == ASID) &&
49 4ad40f36 bellard
            tlb->VPN == tag && address < tlb->end2) {
50 6af0bf9c bellard
            /* TLB match */
51 6af0bf9c bellard
            n = (address >> 12) & 1;
52 6af0bf9c bellard
            /* Check access rights */
53 98c1b82b pbrook
            if (!(n ? tlb->V1 : tlb->V0))
54 98c1b82b pbrook
                return -3;
55 98c1b82b pbrook
            if (rw == 0 || (n ? tlb->D1 : tlb->D0)) {
56 6af0bf9c bellard
                *physical = tlb->PFN[n] | (address & 0xFFF);
57 9fb63ac2 bellard
                *prot = PAGE_READ;
58 98c1b82b pbrook
                if (n ? tlb->D1 : tlb->D0)
59 9fb63ac2 bellard
                    *prot |= PAGE_WRITE;
60 6af0bf9c bellard
                return 0;
61 6af0bf9c bellard
            }
62 98c1b82b pbrook
            return -4;
63 6af0bf9c bellard
        }
64 6af0bf9c bellard
    }
65 6af0bf9c bellard
66 6af0bf9c bellard
    return ret;
67 6af0bf9c bellard
}
68 6af0bf9c bellard
#endif
69 6af0bf9c bellard
70 6af0bf9c bellard
int get_physical_address (CPUState *env, target_ulong *physical, int *prot,
71 6af0bf9c bellard
                          target_ulong address, int rw, int access_type)
72 6af0bf9c bellard
{
73 6af0bf9c bellard
    int user_mode;
74 6af0bf9c bellard
    int ret;
75 6af0bf9c bellard
76 6af0bf9c bellard
    /* User mode can only access useg */
77 6af0bf9c bellard
    user_mode = ((env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_UM) ? 1 : 0;
78 6af0bf9c bellard
#if 0
79 6af0bf9c bellard
    if (logfile) {
80 6af0bf9c bellard
        fprintf(logfile, "user mode %d h %08x\n",
81 6af0bf9c bellard
                user_mode, env->hflags);
82 6af0bf9c bellard
    }
83 6af0bf9c bellard
#endif
84 6af0bf9c bellard
    if (user_mode && address > 0x7FFFFFFFUL)
85 6af0bf9c bellard
        return -1;
86 6af0bf9c bellard
    ret = 0;
87 6af0bf9c bellard
    if (address < 0x80000000UL) {
88 9fb63ac2 bellard
        if (!(env->hflags & MIPS_HFLAG_ERL)) {
89 9fb63ac2 bellard
#ifdef MIPS_USES_R4K_TLB
90 9fb63ac2 bellard
            ret = map_address(env, physical, prot, address, rw, access_type);
91 6af0bf9c bellard
#else
92 6af0bf9c bellard
            *physical = address + 0x40000000UL;
93 6af0bf9c bellard
            *prot = PAGE_READ | PAGE_WRITE;
94 6af0bf9c bellard
#endif
95 6af0bf9c bellard
        } else {
96 6af0bf9c bellard
            *physical = address;
97 6af0bf9c bellard
            *prot = PAGE_READ | PAGE_WRITE;
98 6af0bf9c bellard
        }
99 6af0bf9c bellard
    } else if (address < 0xA0000000UL) {
100 6af0bf9c bellard
        /* kseg0 */
101 6af0bf9c bellard
        /* XXX: check supervisor mode */
102 6af0bf9c bellard
        *physical = address - 0x80000000UL;
103 6af0bf9c bellard
        *prot = PAGE_READ | PAGE_WRITE;
104 6af0bf9c bellard
    } else if (address < 0xC0000000UL) {
105 6af0bf9c bellard
        /* kseg1 */
106 6af0bf9c bellard
        /* XXX: check supervisor mode */
107 6af0bf9c bellard
        *physical = address - 0xA0000000UL;
108 6af0bf9c bellard
        *prot = PAGE_READ | PAGE_WRITE;
109 6af0bf9c bellard
    } else if (address < 0xE0000000UL) {
110 6af0bf9c bellard
        /* kseg2 */
111 9fb63ac2 bellard
#ifdef MIPS_USES_R4K_TLB
112 9fb63ac2 bellard
        ret = map_address(env, physical, prot, address, rw, access_type);
113 6af0bf9c bellard
#else
114 6af0bf9c bellard
        *physical = address;
115 6af0bf9c bellard
        *prot = PAGE_READ | PAGE_WRITE;
116 6af0bf9c bellard
#endif
117 6af0bf9c bellard
    } else {
118 6af0bf9c bellard
        /* kseg3 */
119 6af0bf9c bellard
        /* XXX: check supervisor mode */
120 6af0bf9c bellard
        /* XXX: debug segment is not emulated */
121 9fb63ac2 bellard
#ifdef MIPS_USES_R4K_TLB
122 9fb63ac2 bellard
        ret = map_address(env, physical, prot, address, rw, access_type);
123 6af0bf9c bellard
#else
124 6af0bf9c bellard
        *physical = address;
125 6af0bf9c bellard
        *prot = PAGE_READ | PAGE_WRITE;
126 6af0bf9c bellard
#endif
127 6af0bf9c bellard
    }
128 6af0bf9c bellard
#if 0
129 6af0bf9c bellard
    if (logfile) {
130 6af0bf9c bellard
        fprintf(logfile, "%08x %d %d => %08x %d (%d)\n", address, rw,
131 6af0bf9c bellard
                access_type, *physical, *prot, ret);
132 6af0bf9c bellard
    }
133 6af0bf9c bellard
#endif
134 6af0bf9c bellard
135 6af0bf9c bellard
    return ret;
136 6af0bf9c bellard
}
137 6af0bf9c bellard
138 6af0bf9c bellard
#if defined(CONFIG_USER_ONLY) 
139 6af0bf9c bellard
target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
140 6af0bf9c bellard
{
141 6af0bf9c bellard
    return addr;
142 6af0bf9c bellard
}
143 6af0bf9c bellard
#else
144 6af0bf9c bellard
target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
145 6af0bf9c bellard
{
146 6af0bf9c bellard
    target_ulong phys_addr;
147 6af0bf9c bellard
    int prot;
148 6af0bf9c bellard
149 6af0bf9c bellard
    if (get_physical_address(env, &phys_addr, &prot, addr, 0, ACCESS_INT) != 0)
150 6af0bf9c bellard
        return -1;
151 6af0bf9c bellard
    return phys_addr;
152 6af0bf9c bellard
}
153 6af0bf9c bellard
154 6af0bf9c bellard
void cpu_mips_init_mmu (CPUState *env)
155 6af0bf9c bellard
{
156 6af0bf9c bellard
}
157 6af0bf9c bellard
#endif /* !defined(CONFIG_USER_ONLY) */
158 6af0bf9c bellard
159 6af0bf9c bellard
int cpu_mips_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
160 6af0bf9c bellard
                               int is_user, int is_softmmu)
161 6af0bf9c bellard
{
162 6af0bf9c bellard
    target_ulong physical;
163 6af0bf9c bellard
    int prot;
164 6af0bf9c bellard
    int exception = 0, error_code = 0;
165 6af0bf9c bellard
    int access_type;
166 6af0bf9c bellard
    int ret = 0;
167 6af0bf9c bellard
168 6af0bf9c bellard
    if (logfile) {
169 4ad40f36 bellard
#if 0
170 6af0bf9c bellard
        cpu_dump_state(env, logfile, fprintf, 0);
171 4ad40f36 bellard
#endif
172 6af0bf9c bellard
        fprintf(logfile, "%s pc %08x ad %08x rw %d is_user %d smmu %d\n",
173 6af0bf9c bellard
                __func__, env->PC, address, rw, is_user, is_softmmu);
174 6af0bf9c bellard
    }
175 4ad40f36 bellard
176 4ad40f36 bellard
    rw &= 1;
177 4ad40f36 bellard
178 6af0bf9c bellard
    /* data access */
179 6af0bf9c bellard
    /* XXX: put correct access by using cpu_restore_state()
180 6af0bf9c bellard
       correctly */
181 6af0bf9c bellard
    access_type = ACCESS_INT;
182 6af0bf9c bellard
    if (env->user_mode_only) {
183 6af0bf9c bellard
        /* user mode only emulation */
184 6af0bf9c bellard
        ret = -2;
185 6af0bf9c bellard
        goto do_fault;
186 6af0bf9c bellard
    }
187 6af0bf9c bellard
    ret = get_physical_address(env, &physical, &prot,
188 6af0bf9c bellard
                               address, rw, access_type);
189 6af0bf9c bellard
    if (logfile) {
190 6af0bf9c bellard
        fprintf(logfile, "%s address=%08x ret %d physical %08x prot %d\n",
191 6af0bf9c bellard
                __func__, address, ret, physical, prot);
192 6af0bf9c bellard
    }
193 6af0bf9c bellard
    if (ret == 0) {
194 6af0bf9c bellard
        ret = tlb_set_page(env, address & ~0xFFF, physical & ~0xFFF, prot,
195 6af0bf9c bellard
                           is_user, is_softmmu);
196 6af0bf9c bellard
    } else if (ret < 0) {
197 6af0bf9c bellard
    do_fault:
198 6af0bf9c bellard
        switch (ret) {
199 6af0bf9c bellard
        default:
200 6af0bf9c bellard
        case -1:
201 6af0bf9c bellard
            /* Reference to kernel address from user mode or supervisor mode */
202 6af0bf9c bellard
            /* Reference to supervisor address from user mode */
203 6af0bf9c bellard
            if (rw)
204 6af0bf9c bellard
                exception = EXCP_AdES;
205 6af0bf9c bellard
            else
206 6af0bf9c bellard
                exception = EXCP_AdEL;
207 6af0bf9c bellard
            break;
208 6af0bf9c bellard
        case -2:
209 6af0bf9c bellard
            /* No TLB match for a mapped address */
210 6af0bf9c bellard
            if (rw)
211 6af0bf9c bellard
                exception = EXCP_TLBS;
212 6af0bf9c bellard
            else
213 6af0bf9c bellard
                exception = EXCP_TLBL;
214 6af0bf9c bellard
            error_code = 1;
215 6af0bf9c bellard
            break;
216 6af0bf9c bellard
        case -3:
217 6af0bf9c bellard
            /* TLB match with no valid bit */
218 6af0bf9c bellard
            if (rw)
219 6af0bf9c bellard
                exception = EXCP_TLBS;
220 6af0bf9c bellard
            else
221 6af0bf9c bellard
                exception = EXCP_TLBL;
222 6af0bf9c bellard
            error_code = 0;
223 6af0bf9c bellard
            break;
224 6af0bf9c bellard
        case -4:
225 6af0bf9c bellard
            /* TLB match but 'D' bit is cleared */
226 6af0bf9c bellard
            exception = EXCP_LTLBL;
227 6af0bf9c bellard
            break;
228 6af0bf9c bellard
                
229 6af0bf9c bellard
        }
230 6af0bf9c bellard
        /* Raise exception */
231 6af0bf9c bellard
        env->CP0_BadVAddr = address;
232 85498508 bellard
        env->CP0_Context = (env->CP0_Context & 0xff800000) |
233 4ad40f36 bellard
                           ((address >> 9) &   0x007ffff0);
234 6af0bf9c bellard
        env->CP0_EntryHi =
235 85498508 bellard
            (env->CP0_EntryHi & 0x000000FF) | (address & 0xFFFFF000);
236 6af0bf9c bellard
        env->exception_index = exception;
237 6af0bf9c bellard
        env->error_code = error_code;
238 6af0bf9c bellard
        ret = 1;
239 6af0bf9c bellard
    }
240 6af0bf9c bellard
241 6af0bf9c bellard
    return ret;
242 6af0bf9c bellard
}
243 6af0bf9c bellard
244 6af0bf9c bellard
void do_interrupt (CPUState *env)
245 6af0bf9c bellard
{
246 6af0bf9c bellard
    target_ulong pc, offset;
247 6af0bf9c bellard
    int cause = -1;
248 6af0bf9c bellard
249 6af0bf9c bellard
    if (logfile && env->exception_index != EXCP_EXT_INTERRUPT) {
250 6af0bf9c bellard
        fprintf(logfile, "%s enter: PC %08x EPC %08x cause %d excp %d\n",
251 6af0bf9c bellard
                __func__, env->PC, env->CP0_EPC, cause, env->exception_index);
252 6af0bf9c bellard
    }
253 6af0bf9c bellard
    if (env->exception_index == EXCP_EXT_INTERRUPT &&
254 6af0bf9c bellard
        (env->hflags & MIPS_HFLAG_DM))
255 6af0bf9c bellard
        env->exception_index = EXCP_DINT;
256 6af0bf9c bellard
    offset = 0x180;
257 6af0bf9c bellard
    switch (env->exception_index) {
258 6af0bf9c bellard
    case EXCP_DSS:
259 6af0bf9c bellard
        env->CP0_Debug |= 1 << CP0DB_DSS;
260 6af0bf9c bellard
        /* Debug single step cannot be raised inside a delay slot and
261 6af0bf9c bellard
         * resume will always occur on the next instruction
262 6af0bf9c bellard
         * (but we assume the pc has always been updated during
263 6af0bf9c bellard
         *  code translation).
264 6af0bf9c bellard
         */
265 6af0bf9c bellard
        env->CP0_DEPC = env->PC;
266 6af0bf9c bellard
        goto enter_debug_mode;
267 6af0bf9c bellard
    case EXCP_DINT:
268 6af0bf9c bellard
        env->CP0_Debug |= 1 << CP0DB_DINT;
269 6af0bf9c bellard
        goto set_DEPC;
270 6af0bf9c bellard
    case EXCP_DIB:
271 6af0bf9c bellard
        env->CP0_Debug |= 1 << CP0DB_DIB;
272 6af0bf9c bellard
        goto set_DEPC;
273 6af0bf9c bellard
    case EXCP_DBp:
274 6af0bf9c bellard
        env->CP0_Debug |= 1 << CP0DB_DBp;
275 6af0bf9c bellard
        goto set_DEPC;
276 6af0bf9c bellard
    case EXCP_DDBS:
277 6af0bf9c bellard
        env->CP0_Debug |= 1 << CP0DB_DDBS;
278 6af0bf9c bellard
        goto set_DEPC;
279 6af0bf9c bellard
    case EXCP_DDBL:
280 6af0bf9c bellard
        env->CP0_Debug |= 1 << CP0DB_DDBL;
281 6af0bf9c bellard
        goto set_DEPC;
282 6af0bf9c bellard
    set_DEPC:
283 4ad40f36 bellard
        if (env->hflags & MIPS_HFLAG_BMASK) {
284 6af0bf9c bellard
            /* If the exception was raised from a delay slot,
285 6af0bf9c bellard
             * come back to the jump
286 6af0bf9c bellard
             */
287 6af0bf9c bellard
            env->CP0_DEPC = env->PC - 4;
288 4ad40f36 bellard
            env->hflags &= ~MIPS_HFLAG_BMASK;
289 6af0bf9c bellard
        } else {
290 6af0bf9c bellard
            env->CP0_DEPC = env->PC;
291 6af0bf9c bellard
        }
292 6af0bf9c bellard
    enter_debug_mode:
293 6af0bf9c bellard
        env->hflags |= MIPS_HFLAG_DM;
294 6af0bf9c bellard
        /* EJTAG probe trap enable is not implemented... */
295 6af0bf9c bellard
        pc = 0xBFC00480;
296 6af0bf9c bellard
        break;
297 6af0bf9c bellard
    case EXCP_RESET:
298 9fb63ac2 bellard
#ifdef MIPS_USES_R4K_TLB
299 6af0bf9c bellard
        env->CP0_random = MIPS_TLB_NB - 1;
300 6af0bf9c bellard
#endif
301 6af0bf9c bellard
        env->CP0_Wired = 0;
302 6af0bf9c bellard
        env->CP0_Config0 = MIPS_CONFIG0;
303 6af0bf9c bellard
#if defined (MIPS_CONFIG1)
304 6af0bf9c bellard
        env->CP0_Config1 = MIPS_CONFIG1;
305 6af0bf9c bellard
#endif
306 6af0bf9c bellard
#if defined (MIPS_CONFIG2)
307 6af0bf9c bellard
        env->CP0_Config2 = MIPS_CONFIG2;
308 6af0bf9c bellard
#endif
309 6af0bf9c bellard
#if defined (MIPS_CONFIG3)
310 6af0bf9c bellard
        env->CP0_Config3 = MIPS_CONFIG3;
311 6af0bf9c bellard
#endif
312 6af0bf9c bellard
        env->CP0_WatchLo = 0;
313 6af0bf9c bellard
        env->CP0_Status = (1 << CP0St_CU0) | (1 << CP0St_BEV);
314 6af0bf9c bellard
        goto set_error_EPC;
315 6af0bf9c bellard
    case EXCP_SRESET:
316 6af0bf9c bellard
        env->CP0_Status = (1 << CP0St_CU0) | (1 << CP0St_BEV) |
317 6af0bf9c bellard
            (1 << CP0St_SR);
318 6af0bf9c bellard
        env->CP0_WatchLo = 0;
319 6af0bf9c bellard
        goto set_error_EPC;
320 6af0bf9c bellard
    case EXCP_NMI:
321 6af0bf9c bellard
        env->CP0_Status = (1 << CP0St_CU0) | (1 << CP0St_BEV) |
322 6af0bf9c bellard
            (1 << CP0St_NMI);
323 6af0bf9c bellard
    set_error_EPC:
324 4ad40f36 bellard
        if (env->hflags & MIPS_HFLAG_BMASK) {
325 6af0bf9c bellard
            /* If the exception was raised from a delay slot,
326 6af0bf9c bellard
             * come back to the jump
327 6af0bf9c bellard
             */
328 6af0bf9c bellard
            env->CP0_ErrorEPC = env->PC - 4;
329 ecd78a0a pbrook
            env->hflags &= ~MIPS_HFLAG_BMASK;
330 6af0bf9c bellard
        } else {
331 6af0bf9c bellard
            env->CP0_ErrorEPC = env->PC;
332 6af0bf9c bellard
        }
333 4ad40f36 bellard
        env->hflags = MIPS_HFLAG_ERL;
334 6af0bf9c bellard
        pc = 0xBFC00000;
335 6af0bf9c bellard
        break;
336 6af0bf9c bellard
    case EXCP_MCHECK:
337 6af0bf9c bellard
        cause = 24;
338 6af0bf9c bellard
        goto set_EPC;
339 6af0bf9c bellard
    case EXCP_EXT_INTERRUPT:
340 6af0bf9c bellard
        cause = 0;
341 6af0bf9c bellard
        if (env->CP0_Cause & (1 << CP0Ca_IV))
342 6af0bf9c bellard
            offset = 0x200;
343 6af0bf9c bellard
        goto set_EPC;
344 6af0bf9c bellard
    case EXCP_DWATCH:
345 6af0bf9c bellard
        cause = 23;
346 6af0bf9c bellard
        /* XXX: TODO: manage defered watch exceptions */
347 6af0bf9c bellard
        goto set_EPC;
348 6af0bf9c bellard
    case EXCP_AdEL:
349 6af0bf9c bellard
    case EXCP_AdES:
350 6af0bf9c bellard
        cause = 4;
351 6af0bf9c bellard
        goto set_EPC;
352 6af0bf9c bellard
    case EXCP_TLBL:
353 6af0bf9c bellard
    case EXCP_TLBF:
354 6af0bf9c bellard
        cause = 2;
355 6af0bf9c bellard
        if (env->error_code == 1 && !(env->hflags & MIPS_HFLAG_EXL))
356 6af0bf9c bellard
            offset = 0x000;
357 6af0bf9c bellard
        goto set_EPC;
358 6af0bf9c bellard
    case EXCP_IBE:
359 6af0bf9c bellard
        cause = 6;
360 6af0bf9c bellard
        goto set_EPC;
361 6af0bf9c bellard
    case EXCP_DBE:
362 6af0bf9c bellard
        cause = 7;
363 6af0bf9c bellard
        goto set_EPC;
364 6af0bf9c bellard
    case EXCP_SYSCALL:
365 6af0bf9c bellard
        cause = 8;
366 6af0bf9c bellard
        goto set_EPC;
367 6af0bf9c bellard
    case EXCP_BREAK:
368 6af0bf9c bellard
        cause = 9;
369 6af0bf9c bellard
        goto set_EPC;
370 6af0bf9c bellard
    case EXCP_RI:
371 6af0bf9c bellard
        cause = 10;
372 6af0bf9c bellard
        goto set_EPC;
373 6af0bf9c bellard
    case EXCP_CpU:
374 6af0bf9c bellard
        cause = 11;
375 4ad40f36 bellard
        env->CP0_Cause = (env->CP0_Cause & ~0x03000000) | (env->error_code << 28);
376 6af0bf9c bellard
        goto set_EPC;
377 6af0bf9c bellard
    case EXCP_OVERFLOW:
378 6af0bf9c bellard
        cause = 12;
379 6af0bf9c bellard
        goto set_EPC;
380 6af0bf9c bellard
    case EXCP_TRAP:
381 6af0bf9c bellard
        cause = 13;
382 6af0bf9c bellard
        goto set_EPC;
383 6af0bf9c bellard
    case EXCP_LTLBL:
384 6af0bf9c bellard
        cause = 1;
385 6af0bf9c bellard
        goto set_EPC;
386 6af0bf9c bellard
    case EXCP_TLBS:
387 6af0bf9c bellard
        cause = 3;
388 0d8aca8c bellard
        if (env->error_code == 1 && !(env->hflags & MIPS_HFLAG_EXL))
389 0d8aca8c bellard
            offset = 0x000;
390 0d8aca8c bellard
        goto set_EPC;
391 6af0bf9c bellard
    set_EPC:
392 6af0bf9c bellard
        if (env->CP0_Status & (1 << CP0St_BEV)) {
393 6af0bf9c bellard
            pc = 0xBFC00200;
394 6af0bf9c bellard
        } else {
395 6af0bf9c bellard
            pc = 0x80000000;
396 6af0bf9c bellard
        }
397 6af0bf9c bellard
        env->hflags |= MIPS_HFLAG_EXL;
398 6af0bf9c bellard
        pc += offset;
399 6af0bf9c bellard
        env->CP0_Cause = (env->CP0_Cause & ~0x7C) | (cause << 2);
400 4ad40f36 bellard
        if (env->hflags & MIPS_HFLAG_BMASK) {
401 6af0bf9c bellard
            /* If the exception was raised from a delay slot,
402 6af0bf9c bellard
             * come back to the jump
403 6af0bf9c bellard
             */
404 6af0bf9c bellard
            env->CP0_EPC = env->PC - 4;
405 6af0bf9c bellard
            env->CP0_Cause |= 0x80000000;
406 4ad40f36 bellard
            env->hflags &= ~MIPS_HFLAG_BMASK;
407 6af0bf9c bellard
        } else {
408 6af0bf9c bellard
            env->CP0_EPC = env->PC;
409 6af0bf9c bellard
            env->CP0_Cause &= ~0x80000000;
410 6af0bf9c bellard
        }
411 6af0bf9c bellard
        break;
412 6af0bf9c bellard
    default:
413 6af0bf9c bellard
        if (logfile) {
414 6af0bf9c bellard
            fprintf(logfile, "Invalid MIPS exception %d. Exiting\n",
415 6af0bf9c bellard
                    env->exception_index);
416 6af0bf9c bellard
        }
417 6af0bf9c bellard
        printf("Invalid MIPS exception %d. Exiting\n", env->exception_index);
418 6af0bf9c bellard
        exit(1);
419 6af0bf9c bellard
    }
420 6af0bf9c bellard
    env->PC = pc;
421 6af0bf9c bellard
    if (logfile && env->exception_index != EXCP_EXT_INTERRUPT) {
422 6af0bf9c bellard
        fprintf(logfile, "%s: PC %08x EPC %08x cause %d excp %d\n"
423 6af0bf9c bellard
                "    S %08x C %08x A %08x D %08x\n",
424 6af0bf9c bellard
                __func__, env->PC, env->CP0_EPC, cause, env->exception_index,
425 6af0bf9c bellard
                env->CP0_Status, env->CP0_Cause, env->CP0_BadVAddr,
426 6af0bf9c bellard
                env->CP0_DEPC);
427 6af0bf9c bellard
    }
428 6af0bf9c bellard
    env->exception_index = EXCP_NONE;
429 6af0bf9c bellard
}