Statistics
| Branch: | Revision:

root / target-mips / helper.c @ c570fd16

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