Statistics
| Branch: | Revision:

root / target-mips / helper.c @ 24c7b0e3

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