Statistics
| Branch: | Revision:

root / target-mips / helper.c @ 5a1e8ffb

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