Statistics
| Branch: | Revision:

root / target-mips / op_helper.c @ 3e382bc8

History | View | Annotate | Download (18.6 kB)

1 6af0bf9c bellard
/*
2 6af0bf9c bellard
 *  MIPS emulation helpers for qemu.
3 6af0bf9c bellard
 * 
4 6af0bf9c bellard
 *  Copyright (c) 2004-2005 Jocelyn Mayer
5 6af0bf9c bellard
 *
6 6af0bf9c bellard
 * This library is free software; you can redistribute it and/or
7 6af0bf9c bellard
 * modify it under the terms of the GNU Lesser General Public
8 6af0bf9c bellard
 * License as published by the Free Software Foundation; either
9 6af0bf9c bellard
 * version 2 of the License, or (at your option) any later version.
10 6af0bf9c bellard
 *
11 6af0bf9c bellard
 * This library is distributed in the hope that it will be useful,
12 6af0bf9c bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 6af0bf9c bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 6af0bf9c bellard
 * Lesser General Public License for more details.
15 6af0bf9c bellard
 *
16 6af0bf9c bellard
 * You should have received a copy of the GNU Lesser General Public
17 6af0bf9c bellard
 * License along with this library; if not, write to the Free Software
18 6af0bf9c bellard
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 6af0bf9c bellard
 */
20 6af0bf9c bellard
#include "exec.h"
21 6af0bf9c bellard
22 6af0bf9c bellard
#define MIPS_DEBUG_DISAS
23 6af0bf9c bellard
24 4ad40f36 bellard
#define GETPC() (__builtin_return_address(0))
25 4ad40f36 bellard
26 6af0bf9c bellard
/*****************************************************************************/
27 6af0bf9c bellard
/* Exceptions processing helpers */
28 6af0bf9c bellard
void cpu_loop_exit(void)
29 6af0bf9c bellard
{
30 6af0bf9c bellard
    longjmp(env->jmp_env, 1);
31 6af0bf9c bellard
}
32 6af0bf9c bellard
33 6af0bf9c bellard
void do_raise_exception_err (uint32_t exception, int error_code)
34 6af0bf9c bellard
{
35 6af0bf9c bellard
#if 1
36 6af0bf9c bellard
    if (logfile && exception < 0x100)
37 6af0bf9c bellard
        fprintf(logfile, "%s: %d %d\n", __func__, exception, error_code);
38 6af0bf9c bellard
#endif
39 6af0bf9c bellard
    env->exception_index = exception;
40 6af0bf9c bellard
    env->error_code = error_code;
41 6af0bf9c bellard
    T0 = 0;
42 6af0bf9c bellard
    cpu_loop_exit();
43 6af0bf9c bellard
}
44 6af0bf9c bellard
45 6af0bf9c bellard
void do_raise_exception (uint32_t exception)
46 6af0bf9c bellard
{
47 6af0bf9c bellard
    do_raise_exception_err(exception, 0);
48 6af0bf9c bellard
}
49 6af0bf9c bellard
50 4ad40f36 bellard
void do_restore_state (void *pc_ptr)
51 4ad40f36 bellard
{
52 4ad40f36 bellard
  TranslationBlock *tb;
53 4ad40f36 bellard
  unsigned long pc = (unsigned long) pc_ptr;
54 4ad40f36 bellard
55 4ad40f36 bellard
  tb = tb_find_pc (pc);
56 4ad40f36 bellard
  cpu_restore_state (tb, env, pc, NULL);
57 4ad40f36 bellard
}
58 4ad40f36 bellard
59 4ad40f36 bellard
void do_raise_exception_direct (uint32_t exception)
60 4ad40f36 bellard
{
61 4ad40f36 bellard
    do_restore_state (GETPC ());
62 4ad40f36 bellard
    do_raise_exception_err (exception, 0);
63 4ad40f36 bellard
}
64 4ad40f36 bellard
65 6af0bf9c bellard
#define MEMSUFFIX _raw
66 6af0bf9c bellard
#include "op_helper_mem.c"
67 6af0bf9c bellard
#undef MEMSUFFIX
68 6af0bf9c bellard
#if !defined(CONFIG_USER_ONLY)
69 6af0bf9c bellard
#define MEMSUFFIX _user
70 6af0bf9c bellard
#include "op_helper_mem.c"
71 6af0bf9c bellard
#undef MEMSUFFIX
72 6af0bf9c bellard
#define MEMSUFFIX _kernel
73 6af0bf9c bellard
#include "op_helper_mem.c"
74 6af0bf9c bellard
#undef MEMSUFFIX
75 6af0bf9c bellard
#endif
76 6af0bf9c bellard
77 6af0bf9c bellard
/* 64 bits arithmetic for 32 bits hosts */
78 6af0bf9c bellard
#if (HOST_LONG_BITS == 32)
79 6af0bf9c bellard
static inline uint64_t get_HILO (void)
80 6af0bf9c bellard
{
81 6af0bf9c bellard
    return ((uint64_t)env->HI << 32) | (uint64_t)env->LO;
82 6af0bf9c bellard
}
83 6af0bf9c bellard
84 6af0bf9c bellard
static inline void set_HILO (uint64_t HILO)
85 6af0bf9c bellard
{
86 6af0bf9c bellard
    env->LO = HILO & 0xFFFFFFFF;
87 6af0bf9c bellard
    env->HI = HILO >> 32;
88 6af0bf9c bellard
}
89 6af0bf9c bellard
90 6af0bf9c bellard
void do_mult (void)
91 6af0bf9c bellard
{
92 4ad40f36 bellard
    set_HILO((int64_t)(int32_t)T0 * (int64_t)(int32_t)T1);
93 6af0bf9c bellard
}
94 6af0bf9c bellard
95 6af0bf9c bellard
void do_multu (void)
96 6af0bf9c bellard
{
97 6af0bf9c bellard
    set_HILO((uint64_t)T0 * (uint64_t)T1);
98 6af0bf9c bellard
}
99 6af0bf9c bellard
100 6af0bf9c bellard
void do_madd (void)
101 6af0bf9c bellard
{
102 6af0bf9c bellard
    int64_t tmp;
103 6af0bf9c bellard
104 4ad40f36 bellard
    tmp = ((int64_t)(int32_t)T0 * (int64_t)(int32_t)T1);
105 6af0bf9c bellard
    set_HILO((int64_t)get_HILO() + tmp);
106 6af0bf9c bellard
}
107 6af0bf9c bellard
108 6af0bf9c bellard
void do_maddu (void)
109 6af0bf9c bellard
{
110 6af0bf9c bellard
    uint64_t tmp;
111 6af0bf9c bellard
112 6af0bf9c bellard
    tmp = ((uint64_t)T0 * (uint64_t)T1);
113 6af0bf9c bellard
    set_HILO(get_HILO() + tmp);
114 6af0bf9c bellard
}
115 6af0bf9c bellard
116 6af0bf9c bellard
void do_msub (void)
117 6af0bf9c bellard
{
118 6af0bf9c bellard
    int64_t tmp;
119 6af0bf9c bellard
120 4ad40f36 bellard
    tmp = ((int64_t)(int32_t)T0 * (int64_t)(int32_t)T1);
121 6af0bf9c bellard
    set_HILO((int64_t)get_HILO() - tmp);
122 6af0bf9c bellard
}
123 6af0bf9c bellard
124 6af0bf9c bellard
void do_msubu (void)
125 6af0bf9c bellard
{
126 6af0bf9c bellard
    uint64_t tmp;
127 6af0bf9c bellard
128 6af0bf9c bellard
    tmp = ((uint64_t)T0 * (uint64_t)T1);
129 6af0bf9c bellard
    set_HILO(get_HILO() - tmp);
130 6af0bf9c bellard
}
131 6af0bf9c bellard
#endif
132 6af0bf9c bellard
133 048f6b4d bellard
#if defined(CONFIG_USER_ONLY) 
134 048f6b4d bellard
void do_mfc0 (int reg, int sel)
135 048f6b4d bellard
{
136 048f6b4d bellard
    cpu_abort(env, "mfc0 reg=%d sel=%d\n", reg, sel);
137 048f6b4d bellard
}
138 048f6b4d bellard
void do_mtc0 (int reg, int sel)
139 048f6b4d bellard
{
140 048f6b4d bellard
    cpu_abort(env, "mtc0 reg=%d sel=%d\n", reg, sel);
141 048f6b4d bellard
}
142 048f6b4d bellard
143 048f6b4d bellard
void do_tlbwi (void)
144 048f6b4d bellard
{
145 048f6b4d bellard
    cpu_abort(env, "tlbwi\n");
146 048f6b4d bellard
}
147 048f6b4d bellard
148 048f6b4d bellard
void do_tlbwr (void)
149 048f6b4d bellard
{
150 048f6b4d bellard
    cpu_abort(env, "tlbwr\n");
151 048f6b4d bellard
}
152 048f6b4d bellard
153 048f6b4d bellard
void do_tlbp (void)
154 048f6b4d bellard
{
155 048f6b4d bellard
    cpu_abort(env, "tlbp\n");
156 048f6b4d bellard
}
157 048f6b4d bellard
158 048f6b4d bellard
void do_tlbr (void)
159 048f6b4d bellard
{
160 048f6b4d bellard
    cpu_abort(env, "tlbr\n");
161 048f6b4d bellard
}
162 048f6b4d bellard
#else
163 048f6b4d bellard
164 6af0bf9c bellard
/* CP0 helpers */
165 6af0bf9c bellard
void do_mfc0 (int reg, int sel)
166 6af0bf9c bellard
{
167 6af0bf9c bellard
    const unsigned char *rn;
168 6af0bf9c bellard
169 6af0bf9c bellard
    if (sel != 0 && reg != 16 && reg != 28) {
170 6af0bf9c bellard
        rn = "invalid";
171 6af0bf9c bellard
        goto print;
172 6af0bf9c bellard
    }
173 6af0bf9c bellard
    switch (reg) {
174 6af0bf9c bellard
    case 0:
175 6af0bf9c bellard
        T0 = env->CP0_index;
176 6af0bf9c bellard
        rn = "Index";
177 6af0bf9c bellard
        break;
178 6af0bf9c bellard
    case 1:
179 6af0bf9c bellard
        T0 = cpu_mips_get_random(env);
180 6af0bf9c bellard
        rn = "Random";
181 6af0bf9c bellard
        break;
182 6af0bf9c bellard
    case 2:
183 6af0bf9c bellard
        T0 = env->CP0_EntryLo0;
184 6af0bf9c bellard
        rn = "EntryLo0";
185 6af0bf9c bellard
        break;
186 6af0bf9c bellard
    case 3:
187 6af0bf9c bellard
        T0 = env->CP0_EntryLo1;
188 6af0bf9c bellard
        rn = "EntryLo1";
189 6af0bf9c bellard
        break;
190 6af0bf9c bellard
    case 4:
191 6af0bf9c bellard
        T0 = env->CP0_Context;
192 6af0bf9c bellard
        rn = "Context";
193 6af0bf9c bellard
        break;
194 6af0bf9c bellard
    case 5:
195 6af0bf9c bellard
        T0 = env->CP0_PageMask;
196 6af0bf9c bellard
        rn = "PageMask";
197 6af0bf9c bellard
        break;
198 6af0bf9c bellard
    case 6:
199 6af0bf9c bellard
        T0 = env->CP0_Wired;
200 6af0bf9c bellard
        rn = "Wired";
201 6af0bf9c bellard
        break;
202 6af0bf9c bellard
    case 8:
203 6af0bf9c bellard
        T0 = env->CP0_BadVAddr;
204 6af0bf9c bellard
        rn = "BadVaddr";
205 6af0bf9c bellard
        break;
206 6af0bf9c bellard
    case 9:
207 6af0bf9c bellard
        T0 = cpu_mips_get_count(env);
208 6af0bf9c bellard
        rn = "Count";
209 6af0bf9c bellard
        break;
210 6af0bf9c bellard
    case 10:
211 6af0bf9c bellard
        T0 = env->CP0_EntryHi;
212 6af0bf9c bellard
        rn = "EntryHi";
213 6af0bf9c bellard
        break;
214 6af0bf9c bellard
    case 11:
215 6af0bf9c bellard
        T0 = env->CP0_Compare;
216 6af0bf9c bellard
        rn = "Compare";
217 6af0bf9c bellard
        break;
218 6af0bf9c bellard
    case 12:
219 6af0bf9c bellard
        T0 = env->CP0_Status;
220 6af0bf9c bellard
        if (env->hflags & MIPS_HFLAG_UM)
221 90b37806 bellard
            T0 |= (1 << CP0St_UM);
222 6af0bf9c bellard
        rn = "Status";
223 6af0bf9c bellard
        break;
224 6af0bf9c bellard
    case 13:
225 6af0bf9c bellard
        T0 = env->CP0_Cause;
226 6af0bf9c bellard
        rn = "Cause";
227 6af0bf9c bellard
        break;
228 6af0bf9c bellard
    case 14:
229 6af0bf9c bellard
        T0 = env->CP0_EPC;
230 6af0bf9c bellard
        rn = "EPC";
231 6af0bf9c bellard
        break;
232 6af0bf9c bellard
    case 15:
233 6af0bf9c bellard
        T0 = env->CP0_PRid;
234 6af0bf9c bellard
        rn = "PRid";
235 6af0bf9c bellard
        break;
236 6af0bf9c bellard
    case 16:
237 6af0bf9c bellard
        switch (sel) {
238 6af0bf9c bellard
        case 0:
239 6af0bf9c bellard
            T0 = env->CP0_Config0;
240 6af0bf9c bellard
            rn = "Config";
241 6af0bf9c bellard
            break;
242 6af0bf9c bellard
        case 1:
243 6af0bf9c bellard
            T0 = env->CP0_Config1;
244 6af0bf9c bellard
            rn = "Config1";
245 6af0bf9c bellard
            break;
246 6af0bf9c bellard
        default:
247 6af0bf9c bellard
            rn = "Unknown config register";
248 6af0bf9c bellard
            break;
249 6af0bf9c bellard
        }
250 6af0bf9c bellard
        break;
251 6af0bf9c bellard
    case 17:
252 6af0bf9c bellard
        T0 = env->CP0_LLAddr >> 4;
253 6af0bf9c bellard
        rn = "LLAddr";
254 6af0bf9c bellard
        break;
255 6af0bf9c bellard
    case 18:
256 6af0bf9c bellard
        T0 = env->CP0_WatchLo;
257 6af0bf9c bellard
        rn = "WatchLo";
258 6af0bf9c bellard
        break;
259 6af0bf9c bellard
    case 19:
260 6af0bf9c bellard
        T0 = env->CP0_WatchHi;
261 6af0bf9c bellard
        rn = "WatchHi";
262 6af0bf9c bellard
        break;
263 6af0bf9c bellard
    case 23:
264 6af0bf9c bellard
        T0 = env->CP0_Debug;
265 6af0bf9c bellard
        if (env->hflags & MIPS_HFLAG_DM)
266 6af0bf9c bellard
            T0 |= 1 << CP0DB_DM;
267 6af0bf9c bellard
        rn = "Debug";
268 6af0bf9c bellard
        break;
269 6af0bf9c bellard
    case 24:
270 6af0bf9c bellard
        T0 = env->CP0_DEPC;
271 6af0bf9c bellard
        rn = "DEPC";
272 6af0bf9c bellard
        break;
273 6af0bf9c bellard
    case 28:
274 6af0bf9c bellard
        switch (sel) {
275 6af0bf9c bellard
        case 0:
276 6af0bf9c bellard
            T0 = env->CP0_TagLo;
277 6af0bf9c bellard
            rn = "TagLo";
278 6af0bf9c bellard
            break;
279 6af0bf9c bellard
        case 1:
280 6af0bf9c bellard
            T0 = env->CP0_DataLo;
281 6af0bf9c bellard
            rn = "DataLo";
282 6af0bf9c bellard
            break;
283 6af0bf9c bellard
        default:
284 6af0bf9c bellard
            rn = "unknown sel";
285 6af0bf9c bellard
            break;
286 6af0bf9c bellard
        }
287 6af0bf9c bellard
        break;
288 6af0bf9c bellard
    case 30:
289 6af0bf9c bellard
        T0 = env->CP0_ErrorEPC;
290 6af0bf9c bellard
        rn = "ErrorEPC";
291 6af0bf9c bellard
        break;
292 6af0bf9c bellard
    case 31:
293 6af0bf9c bellard
        T0 = env->CP0_DESAVE;
294 6af0bf9c bellard
        rn = "DESAVE";
295 6af0bf9c bellard
        break;
296 6af0bf9c bellard
    default:
297 6af0bf9c bellard
        rn = "unknown";
298 6af0bf9c bellard
        break;
299 6af0bf9c bellard
    }
300 6af0bf9c bellard
 print:
301 6af0bf9c bellard
#if defined MIPS_DEBUG_DISAS
302 6af0bf9c bellard
    if (loglevel & CPU_LOG_TB_IN_ASM) {
303 6af0bf9c bellard
        fprintf(logfile, "%08x mfc0 %s => %08x (%d %d)\n",
304 6af0bf9c bellard
                env->PC, rn, T0, reg, sel);
305 6af0bf9c bellard
    }
306 6af0bf9c bellard
#endif
307 6af0bf9c bellard
    return;
308 6af0bf9c bellard
}
309 6af0bf9c bellard
310 6af0bf9c bellard
void do_mtc0 (int reg, int sel)
311 6af0bf9c bellard
{
312 6af0bf9c bellard
    const unsigned char *rn;
313 6af0bf9c bellard
    uint32_t val, old, mask;
314 6af0bf9c bellard
315 6af0bf9c bellard
    if (sel != 0 && reg != 16 && reg != 28) {
316 6af0bf9c bellard
        val = -1;
317 6af0bf9c bellard
        old = -1;
318 6af0bf9c bellard
        rn = "invalid";
319 6af0bf9c bellard
        goto print;
320 6af0bf9c bellard
    }
321 6af0bf9c bellard
    switch (reg) {
322 6af0bf9c bellard
    case 0:
323 6af0bf9c bellard
        val = (env->CP0_index & 0x80000000) | (T0 & 0x0000000F);
324 6af0bf9c bellard
        old = env->CP0_index;
325 6af0bf9c bellard
        env->CP0_index = val;
326 6af0bf9c bellard
        rn = "Index";
327 6af0bf9c bellard
        break;
328 6af0bf9c bellard
    case 2:
329 3d9fb9fe bellard
        val = T0 & 0x3FFFFFFF;
330 6af0bf9c bellard
        old = env->CP0_EntryLo0;
331 6af0bf9c bellard
        env->CP0_EntryLo0 = val;
332 6af0bf9c bellard
        rn = "EntryLo0";
333 6af0bf9c bellard
        break;
334 6af0bf9c bellard
    case 3:
335 3d9fb9fe bellard
        val = T0 & 0x3FFFFFFF;
336 6af0bf9c bellard
        old = env->CP0_EntryLo1;
337 6af0bf9c bellard
        env->CP0_EntryLo1 = val;
338 6af0bf9c bellard
        rn = "EntryLo1";
339 6af0bf9c bellard
        break;
340 6af0bf9c bellard
    case 4:
341 ba9a74da bellard
        val = (env->CP0_Context & 0xFF800000) | (T0 & 0x007FFFF0);
342 6af0bf9c bellard
        old = env->CP0_Context;
343 6af0bf9c bellard
        env->CP0_Context = val;
344 6af0bf9c bellard
        rn = "Context";
345 6af0bf9c bellard
        break;
346 6af0bf9c bellard
    case 5:
347 6af0bf9c bellard
        val = T0 & 0x01FFE000;
348 6af0bf9c bellard
        old = env->CP0_PageMask;
349 6af0bf9c bellard
        env->CP0_PageMask = val;
350 6af0bf9c bellard
        rn = "PageMask";
351 6af0bf9c bellard
        break;
352 6af0bf9c bellard
    case 6:
353 6af0bf9c bellard
        val = T0 & 0x0000000F;
354 6af0bf9c bellard
        old = env->CP0_Wired;
355 6af0bf9c bellard
        env->CP0_Wired = val;
356 6af0bf9c bellard
        rn = "Wired";
357 6af0bf9c bellard
        break;
358 6af0bf9c bellard
    case 9:
359 6af0bf9c bellard
        val = T0;
360 6af0bf9c bellard
        old = cpu_mips_get_count(env);
361 6af0bf9c bellard
        cpu_mips_store_count(env, val);
362 6af0bf9c bellard
        rn = "Count";
363 6af0bf9c bellard
        break;
364 6af0bf9c bellard
    case 10:
365 ba9a74da bellard
        val = T0 & 0xFFFFE0FF;
366 6af0bf9c bellard
        old = env->CP0_EntryHi;
367 6af0bf9c bellard
        env->CP0_EntryHi = val;
368 4ad40f36 bellard
        /* If the ASID changes, flush qemu's TLB.  */
369 4ad40f36 bellard
        if ((old & 0xFF) != (val & 0xFF))
370 4ad40f36 bellard
          tlb_flush (env, 1);
371 6af0bf9c bellard
        rn = "EntryHi";
372 6af0bf9c bellard
        break;
373 6af0bf9c bellard
    case 11:
374 6af0bf9c bellard
        val = T0;
375 6af0bf9c bellard
        old = env->CP0_Compare;
376 6af0bf9c bellard
        cpu_mips_store_compare(env, val);
377 6af0bf9c bellard
        rn = "Compare";
378 6af0bf9c bellard
        break;
379 6af0bf9c bellard
    case 12:
380 6af0bf9c bellard
        val = T0 & 0xFA78FF01;
381 6af0bf9c bellard
        if (T0 & (1 << CP0St_UM))
382 6af0bf9c bellard
            env->hflags |= MIPS_HFLAG_UM;
383 6af0bf9c bellard
        else
384 6af0bf9c bellard
            env->hflags &= ~MIPS_HFLAG_UM;
385 6af0bf9c bellard
        if (T0 & (1 << CP0St_ERL))
386 6af0bf9c bellard
            env->hflags |= MIPS_HFLAG_ERL;
387 6af0bf9c bellard
        else
388 6af0bf9c bellard
            env->hflags &= ~MIPS_HFLAG_ERL;
389 6af0bf9c bellard
        if (T0 & (1 << CP0St_EXL))
390 6af0bf9c bellard
            env->hflags |= MIPS_HFLAG_EXL;
391 6af0bf9c bellard
        else
392 6af0bf9c bellard
            env->hflags &= ~MIPS_HFLAG_EXL;
393 6af0bf9c bellard
        old = env->CP0_Status;
394 6af0bf9c bellard
        env->CP0_Status = val;
395 6af0bf9c bellard
        /* If we unmasked an asserted IRQ, raise it */
396 ae022501 bellard
        mask = 0x0000FF00;
397 6af0bf9c bellard
        if (loglevel & CPU_LOG_TB_IN_ASM) {
398 6af0bf9c bellard
            fprintf(logfile, "Status %08x => %08x Cause %08x (%08x %08x %08x)\n",
399 6af0bf9c bellard
                    old, val, env->CP0_Cause, old & mask, val & mask,
400 6af0bf9c bellard
                    env->CP0_Cause & mask);
401 6af0bf9c bellard
        }
402 6af0bf9c bellard
        if ((val & (1 << CP0St_IE)) && !(old & (1 << CP0St_IE)) &&
403 6af0bf9c bellard
            !(env->hflags & MIPS_HFLAG_EXL) &&
404 6af0bf9c bellard
            !(env->hflags & MIPS_HFLAG_ERL) &&
405 3d9fb9fe bellard
            !(env->hflags & MIPS_HFLAG_DM) &&
406 e1d9a508 bellard
            (env->CP0_Status & env->CP0_Cause & mask)) {
407 6af0bf9c bellard
            if (logfile)
408 6af0bf9c bellard
                fprintf(logfile, "Raise pending IRQs\n");
409 6af0bf9c bellard
            env->interrupt_request |= CPU_INTERRUPT_HARD;
410 3d9fb9fe bellard
        } else if (!(val & (1 << CP0St_IE)) && (old & (1 << CP0St_IE))) {
411 6af0bf9c bellard
            env->interrupt_request &= ~CPU_INTERRUPT_HARD;
412 6af0bf9c bellard
        }
413 6af0bf9c bellard
        rn = "Status";
414 6af0bf9c bellard
        break;
415 6af0bf9c bellard
    case 13:
416 6af0bf9c bellard
        val = (env->CP0_Cause & 0xB000F87C) | (T0 & 0x000C00300);
417 6af0bf9c bellard
        old = env->CP0_Cause;
418 6af0bf9c bellard
        env->CP0_Cause = val;
419 6af0bf9c bellard
#if 0
420 e37e863f bellard
        {
421 e37e863f bellard
            int i;
422 e37e863f bellard
            /* Check if we ever asserted a software IRQ */
423 e37e863f bellard
            for (i = 0; i < 2; i++) {
424 e37e863f bellard
                mask = 0x100 << i;
425 e37e863f bellard
                if ((val & mask) & !(old & mask))
426 e37e863f bellard
                    mips_set_irq(i);
427 e37e863f bellard
            }
428 6af0bf9c bellard
        }
429 6af0bf9c bellard
#endif
430 6af0bf9c bellard
        rn = "Cause";
431 6af0bf9c bellard
        break;
432 6af0bf9c bellard
    case 14:
433 6af0bf9c bellard
        val = T0;
434 6af0bf9c bellard
        old = env->CP0_EPC;
435 6af0bf9c bellard
        env->CP0_EPC = val;
436 6af0bf9c bellard
        rn = "EPC";
437 6af0bf9c bellard
        break;
438 6af0bf9c bellard
    case 16:
439 6af0bf9c bellard
        switch (sel) {
440 6af0bf9c bellard
        case 0:
441 6af0bf9c bellard
#if defined(MIPS_USES_R4K_TLB)
442 6af0bf9c bellard
            val = (env->CP0_Config0 & 0x8017FF80) | (T0 & 0x7E000001);
443 6af0bf9c bellard
#else
444 6af0bf9c bellard
            val = (env->CP0_Config0 & 0xFE17FF80) | (T0 & 0x00000001);
445 6af0bf9c bellard
#endif
446 6af0bf9c bellard
            old = env->CP0_Config0;
447 6af0bf9c bellard
            env->CP0_Config0 = val;
448 6af0bf9c bellard
            rn = "Config0";
449 6af0bf9c bellard
            break;
450 6af0bf9c bellard
        default:
451 6af0bf9c bellard
            val = -1;
452 6af0bf9c bellard
            old = -1;
453 6af0bf9c bellard
            rn = "bad config selector";
454 6af0bf9c bellard
            break;
455 6af0bf9c bellard
        }
456 6af0bf9c bellard
        break;
457 6af0bf9c bellard
    case 18:
458 6af0bf9c bellard
        val = T0;
459 6af0bf9c bellard
        old = env->CP0_WatchLo;
460 6af0bf9c bellard
        env->CP0_WatchLo = val;
461 6af0bf9c bellard
        rn = "WatchLo";
462 6af0bf9c bellard
        break;
463 6af0bf9c bellard
    case 19:
464 6af0bf9c bellard
        val = T0 & 0x40FF0FF8;
465 6af0bf9c bellard
        old = env->CP0_WatchHi;
466 6af0bf9c bellard
        env->CP0_WatchHi = val;
467 6af0bf9c bellard
        rn = "WatchHi";
468 6af0bf9c bellard
        break;
469 6af0bf9c bellard
    case 23:
470 6af0bf9c bellard
        val = (env->CP0_Debug & 0x8C03FC1F) | (T0 & 0x13300120);
471 6af0bf9c bellard
        if (T0 & (1 << CP0DB_DM))
472 6af0bf9c bellard
            env->hflags |= MIPS_HFLAG_DM;
473 6af0bf9c bellard
        else
474 6af0bf9c bellard
            env->hflags &= ~MIPS_HFLAG_DM;
475 6af0bf9c bellard
        old = env->CP0_Debug;
476 6af0bf9c bellard
        env->CP0_Debug = val;
477 6af0bf9c bellard
        rn = "Debug";
478 6af0bf9c bellard
        break;
479 6af0bf9c bellard
    case 24:
480 6af0bf9c bellard
        val = T0;
481 6af0bf9c bellard
        old = env->CP0_DEPC;
482 6af0bf9c bellard
        env->CP0_DEPC = val;
483 6af0bf9c bellard
        rn = "DEPC";
484 6af0bf9c bellard
        break;
485 6af0bf9c bellard
    case 28:
486 6af0bf9c bellard
        switch (sel) {
487 6af0bf9c bellard
        case 0:
488 6af0bf9c bellard
            val = T0 & 0xFFFFFCF6;
489 6af0bf9c bellard
            old = env->CP0_TagLo;
490 6af0bf9c bellard
            env->CP0_TagLo = val;
491 6af0bf9c bellard
            rn = "TagLo";
492 6af0bf9c bellard
            break;
493 6af0bf9c bellard
        default:
494 6af0bf9c bellard
            val = -1;
495 6af0bf9c bellard
            old = -1;
496 6af0bf9c bellard
            rn = "invalid sel";
497 6af0bf9c bellard
            break;
498 6af0bf9c bellard
        }
499 6af0bf9c bellard
        break;
500 6af0bf9c bellard
    case 30:
501 6af0bf9c bellard
        val = T0;
502 6af0bf9c bellard
        old = env->CP0_ErrorEPC;
503 6af0bf9c bellard
        env->CP0_ErrorEPC = val;
504 6af0bf9c bellard
        rn = "EPC";
505 6af0bf9c bellard
        break;
506 6af0bf9c bellard
    case 31:
507 6af0bf9c bellard
        val = T0;
508 6af0bf9c bellard
        old = env->CP0_DESAVE;
509 6af0bf9c bellard
        env->CP0_DESAVE = val;
510 6af0bf9c bellard
        rn = "DESAVE";
511 6af0bf9c bellard
        break;
512 6af0bf9c bellard
    default:
513 6af0bf9c bellard
        val = -1;
514 6af0bf9c bellard
        old = -1;
515 6af0bf9c bellard
        rn = "unknown";
516 6af0bf9c bellard
        break;
517 6af0bf9c bellard
    }
518 6af0bf9c bellard
 print:
519 6af0bf9c bellard
#if defined MIPS_DEBUG_DISAS
520 6af0bf9c bellard
    if (loglevel & CPU_LOG_TB_IN_ASM) {
521 6af0bf9c bellard
        fprintf(logfile, "%08x mtc0 %s %08x => %08x (%d %d %08x)\n",
522 6af0bf9c bellard
                env->PC, rn, T0, val, reg, sel, old);
523 6af0bf9c bellard
    }
524 6af0bf9c bellard
#endif
525 6af0bf9c bellard
    return;
526 6af0bf9c bellard
}
527 6af0bf9c bellard
528 6ea83fed bellard
#ifdef MIPS_USES_FPU
529 6ea83fed bellard
#include "softfloat.h"
530 6ea83fed bellard
531 6ea83fed bellard
void fpu_handle_exception(void)
532 6ea83fed bellard
{
533 6ea83fed bellard
#ifdef CONFIG_SOFTFLOAT
534 6ea83fed bellard
    int flags = get_float_exception_flags(&env->fp_status);
535 6ea83fed bellard
    unsigned int cpuflags = 0, enable, cause = 0;
536 6ea83fed bellard
537 6ea83fed bellard
    enable = GET_FP_ENABLE(env->fcr31);
538 6ea83fed bellard
539 6ea83fed bellard
    /* determine current flags */   
540 6ea83fed bellard
    if (flags & float_flag_invalid) {
541 6ea83fed bellard
        cpuflags |= FP_INVALID;
542 6ea83fed bellard
        cause |= FP_INVALID & enable;
543 6ea83fed bellard
    }
544 6ea83fed bellard
    if (flags & float_flag_divbyzero) {
545 6ea83fed bellard
        cpuflags |= FP_DIV0;    
546 6ea83fed bellard
        cause |= FP_DIV0 & enable;
547 6ea83fed bellard
    }
548 6ea83fed bellard
    if (flags & float_flag_overflow) {
549 6ea83fed bellard
        cpuflags |= FP_OVERFLOW;    
550 6ea83fed bellard
        cause |= FP_OVERFLOW & enable;
551 6ea83fed bellard
    }
552 6ea83fed bellard
    if (flags & float_flag_underflow) {
553 6ea83fed bellard
        cpuflags |= FP_UNDERFLOW;   
554 6ea83fed bellard
        cause |= FP_UNDERFLOW & enable;
555 6ea83fed bellard
    }
556 6ea83fed bellard
    if (flags & float_flag_inexact) {
557 6ea83fed bellard
        cpuflags |= FP_INEXACT; 
558 6ea83fed bellard
        cause |= FP_INEXACT & enable;
559 6ea83fed bellard
    }
560 6ea83fed bellard
    SET_FP_FLAGS(env->fcr31, cpuflags);
561 6ea83fed bellard
    SET_FP_CAUSE(env->fcr31, cause);
562 6ea83fed bellard
#else
563 6ea83fed bellard
    SET_FP_FLAGS(env->fcr31, 0);
564 6ea83fed bellard
    SET_FP_CAUSE(env->fcr31, 0);
565 6ea83fed bellard
#endif
566 6ea83fed bellard
}
567 6ea83fed bellard
#endif /* MIPS_USES_FPU */
568 6ea83fed bellard
569 6af0bf9c bellard
/* TLB management */
570 6af0bf9c bellard
#if defined(MIPS_USES_R4K_TLB)
571 98c1b82b pbrook
static void invalidate_tlb (int idx)
572 6af0bf9c bellard
{
573 6af0bf9c bellard
    tlb_t *tlb;
574 98c1b82b pbrook
    target_ulong addr;
575 6af0bf9c bellard
576 6af0bf9c bellard
    tlb = &env->tlb[idx];
577 98c1b82b pbrook
    if (tlb->V0) {
578 98c1b82b pbrook
        tb_invalidate_page_range(tlb->PFN[0], tlb->end - tlb->VPN);
579 4ad40f36 bellard
        addr = tlb->VPN;
580 4ad40f36 bellard
        while (addr < tlb->end) {
581 4ad40f36 bellard
            tlb_flush_page (env, addr);
582 4ad40f36 bellard
            addr += TARGET_PAGE_SIZE;
583 4ad40f36 bellard
        }
584 6af0bf9c bellard
    }
585 98c1b82b pbrook
    if (tlb->V1) {
586 98c1b82b pbrook
        tb_invalidate_page_range(tlb->PFN[1], tlb->end2 - tlb->end);
587 4ad40f36 bellard
        addr = tlb->end;
588 4ad40f36 bellard
        while (addr < tlb->end2) {
589 4ad40f36 bellard
            tlb_flush_page (env, addr);
590 4ad40f36 bellard
            addr += TARGET_PAGE_SIZE;
591 4ad40f36 bellard
        }
592 6af0bf9c bellard
    }
593 6af0bf9c bellard
}
594 6af0bf9c bellard
595 98c1b82b pbrook
static void fill_tlb (int idx)
596 6af0bf9c bellard
{
597 6af0bf9c bellard
    tlb_t *tlb;
598 6af0bf9c bellard
    int size;
599 6af0bf9c bellard
600 6af0bf9c bellard
    /* XXX: detect conflicting TLBs and raise a MCHECK exception when needed */
601 6af0bf9c bellard
    tlb = &env->tlb[idx];
602 6af0bf9c bellard
    tlb->VPN = env->CP0_EntryHi & 0xFFFFE000;
603 98c1b82b pbrook
    tlb->ASID = env->CP0_EntryHi & 0xFF;
604 6af0bf9c bellard
    size = env->CP0_PageMask >> 13;
605 6af0bf9c bellard
    size = 4 * (size + 1);
606 6af0bf9c bellard
    tlb->end = tlb->VPN + (1 << (8 + size));
607 4ad40f36 bellard
    tlb->end2 = tlb->end + (1 << (8 + size));
608 6af0bf9c bellard
    tlb->G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
609 98c1b82b pbrook
    tlb->V0 = (env->CP0_EntryLo0 & 2) != 0;
610 98c1b82b pbrook
    tlb->D0 = (env->CP0_EntryLo0 & 4) != 0;
611 98c1b82b pbrook
    tlb->C0 = (env->CP0_EntryLo0 >> 3) & 0x7;
612 6af0bf9c bellard
    tlb->PFN[0] = (env->CP0_EntryLo0 >> 6) << 12;
613 98c1b82b pbrook
    tlb->V1 = (env->CP0_EntryLo1 & 2) != 0;
614 98c1b82b pbrook
    tlb->D1 = (env->CP0_EntryLo1 & 4) != 0;
615 98c1b82b pbrook
    tlb->C1 = (env->CP0_EntryLo1 >> 3) & 0x7;
616 6af0bf9c bellard
    tlb->PFN[1] = (env->CP0_EntryLo1 >> 6) << 12;
617 6af0bf9c bellard
}
618 6af0bf9c bellard
619 6af0bf9c bellard
void do_tlbwi (void)
620 6af0bf9c bellard
{
621 7a962d30 bellard
    /* Wildly undefined effects for CP0_index containing a too high value and
622 7a962d30 bellard
       MIPS_TLB_NB not being a power of two.  But so does real silicon.  */
623 98c1b82b pbrook
    invalidate_tlb(env->CP0_index & (MIPS_TLB_NB - 1));
624 98c1b82b pbrook
    fill_tlb(env->CP0_index & (MIPS_TLB_NB - 1));
625 6af0bf9c bellard
}
626 6af0bf9c bellard
627 6af0bf9c bellard
void do_tlbwr (void)
628 6af0bf9c bellard
{
629 6af0bf9c bellard
    int r = cpu_mips_get_random(env);
630 6af0bf9c bellard
631 98c1b82b pbrook
    invalidate_tlb(r);
632 98c1b82b pbrook
    fill_tlb(r);
633 6af0bf9c bellard
}
634 6af0bf9c bellard
635 6af0bf9c bellard
void do_tlbp (void)
636 6af0bf9c bellard
{
637 6af0bf9c bellard
    tlb_t *tlb;
638 6af0bf9c bellard
    target_ulong tag;
639 6af0bf9c bellard
    uint8_t ASID;
640 6af0bf9c bellard
    int i;
641 6af0bf9c bellard
642 3d9fb9fe bellard
    tag = env->CP0_EntryHi & 0xFFFFE000;
643 3d9fb9fe bellard
    ASID = env->CP0_EntryHi & 0xFF;
644 3d9fb9fe bellard
    for (i = 0; i < MIPS_TLB_NB; i++) {
645 6af0bf9c bellard
        tlb = &env->tlb[i];
646 6af0bf9c bellard
        /* Check ASID, virtual page number & size */
647 6af0bf9c bellard
        if ((tlb->G == 1 || tlb->ASID == ASID) && tlb->VPN == tag) {
648 6af0bf9c bellard
            /* TLB match */
649 6af0bf9c bellard
            env->CP0_index = i;
650 6af0bf9c bellard
            break;
651 6af0bf9c bellard
        }
652 6af0bf9c bellard
    }
653 7a962d30 bellard
    if (i == MIPS_TLB_NB) {
654 6af0bf9c bellard
        env->CP0_index |= 0x80000000;
655 6af0bf9c bellard
    }
656 6af0bf9c bellard
}
657 6af0bf9c bellard
658 6af0bf9c bellard
void do_tlbr (void)
659 6af0bf9c bellard
{
660 6af0bf9c bellard
    tlb_t *tlb;
661 09c56b84 pbrook
    uint8_t ASID;
662 6af0bf9c bellard
    int size;
663 6af0bf9c bellard
664 09c56b84 pbrook
    ASID = env->CP0_EntryHi & 0xFF;
665 7a962d30 bellard
    tlb = &env->tlb[env->CP0_index & (MIPS_TLB_NB - 1)];
666 4ad40f36 bellard
667 4ad40f36 bellard
    /* If this will change the current ASID, flush qemu's TLB.  */
668 09c56b84 pbrook
    if (ASID != tlb->ASID && tlb->G != 1)
669 4ad40f36 bellard
      tlb_flush (env, 1);
670 4ad40f36 bellard
671 6af0bf9c bellard
    env->CP0_EntryHi = tlb->VPN | tlb->ASID;
672 6af0bf9c bellard
    size = (tlb->end - tlb->VPN) >> 12;
673 6af0bf9c bellard
    env->CP0_PageMask = (size - 1) << 13;
674 98c1b82b pbrook
    env->CP0_EntryLo0 = tlb->G | (tlb->V0 << 1) | (tlb->D0 << 2)
675 98c1b82b pbrook
                | (tlb->C0 << 3) | (tlb->PFN[0] >> 6);
676 98c1b82b pbrook
    env->CP0_EntryLo1 = tlb->G | (tlb->V1 << 1) | (tlb->D1 << 2)
677 98c1b82b pbrook
                | (tlb->C1 << 3) | (tlb->PFN[1] >> 6);
678 6af0bf9c bellard
}
679 6af0bf9c bellard
#endif
680 6af0bf9c bellard
681 048f6b4d bellard
#endif /* !CONFIG_USER_ONLY */
682 048f6b4d bellard
683 6af0bf9c bellard
void op_dump_ldst (const unsigned char *func)
684 6af0bf9c bellard
{
685 6af0bf9c bellard
    if (loglevel)
686 6af0bf9c bellard
        fprintf(logfile, "%s => %08x %08x\n", __func__, T0, T1);
687 6af0bf9c bellard
}
688 6af0bf9c bellard
689 6af0bf9c bellard
void dump_sc (void)
690 6af0bf9c bellard
{
691 6af0bf9c bellard
    if (loglevel) {
692 6af0bf9c bellard
        fprintf(logfile, "%s %08x at %08x (%08x)\n", __func__,
693 6af0bf9c bellard
                T1, T0, env->CP0_LLAddr);
694 6af0bf9c bellard
    }
695 6af0bf9c bellard
}
696 6af0bf9c bellard
697 6af0bf9c bellard
void debug_eret (void)
698 6af0bf9c bellard
{
699 6af0bf9c bellard
    if (loglevel) {
700 6af0bf9c bellard
        fprintf(logfile, "ERET: pc %08x EPC %08x ErrorEPC %08x (%d)\n",
701 6af0bf9c bellard
                env->PC, env->CP0_EPC, env->CP0_ErrorEPC,
702 6af0bf9c bellard
                env->hflags & MIPS_HFLAG_ERL ? 1 : 0);
703 6af0bf9c bellard
    }
704 6af0bf9c bellard
}
705 6af0bf9c bellard
706 6af0bf9c bellard
void do_pmon (int function)
707 6af0bf9c bellard
{
708 6af0bf9c bellard
    function /= 2;
709 6af0bf9c bellard
    switch (function) {
710 6af0bf9c bellard
    case 2: /* TODO: char inbyte(int waitflag); */
711 6af0bf9c bellard
        if (env->gpr[4] == 0)
712 6af0bf9c bellard
            env->gpr[2] = -1;
713 6af0bf9c bellard
        /* Fall through */
714 6af0bf9c bellard
    case 11: /* TODO: char inbyte (void); */
715 6af0bf9c bellard
        env->gpr[2] = -1;
716 6af0bf9c bellard
        break;
717 6af0bf9c bellard
    case 3:
718 6af0bf9c bellard
    case 12:
719 6af0bf9c bellard
        printf("%c", env->gpr[4] & 0xFF);
720 6af0bf9c bellard
        break;
721 6af0bf9c bellard
    case 17:
722 6af0bf9c bellard
        break;
723 6af0bf9c bellard
    case 158:
724 6af0bf9c bellard
        {
725 6af0bf9c bellard
            unsigned char *fmt = (void *)env->gpr[4];
726 6af0bf9c bellard
            printf("%s", fmt);
727 6af0bf9c bellard
        }
728 6af0bf9c bellard
        break;
729 6af0bf9c bellard
    }
730 6af0bf9c bellard
}
731 e37e863f bellard
732 e37e863f bellard
#if !defined(CONFIG_USER_ONLY) 
733 e37e863f bellard
734 4ad40f36 bellard
static void do_unaligned_access (target_ulong addr, int is_write, int is_user, void *retaddr);
735 4ad40f36 bellard
736 e37e863f bellard
#define MMUSUFFIX _mmu
737 4ad40f36 bellard
#define ALIGNED_ONLY
738 e37e863f bellard
739 e37e863f bellard
#define SHIFT 0
740 e37e863f bellard
#include "softmmu_template.h"
741 e37e863f bellard
742 e37e863f bellard
#define SHIFT 1
743 e37e863f bellard
#include "softmmu_template.h"
744 e37e863f bellard
745 e37e863f bellard
#define SHIFT 2
746 e37e863f bellard
#include "softmmu_template.h"
747 e37e863f bellard
748 e37e863f bellard
#define SHIFT 3
749 e37e863f bellard
#include "softmmu_template.h"
750 e37e863f bellard
751 4ad40f36 bellard
static void do_unaligned_access (target_ulong addr, int is_write, int is_user, void *retaddr)
752 4ad40f36 bellard
{
753 4ad40f36 bellard
    env->CP0_BadVAddr = addr;
754 4ad40f36 bellard
    do_restore_state (retaddr);
755 4ad40f36 bellard
    do_raise_exception ((is_write == 1) ? EXCP_AdES : EXCP_AdEL);
756 4ad40f36 bellard
}
757 4ad40f36 bellard
758 e37e863f bellard
void tlb_fill (target_ulong addr, int is_write, int is_user, void *retaddr)
759 e37e863f bellard
{
760 e37e863f bellard
    TranslationBlock *tb;
761 e37e863f bellard
    CPUState *saved_env;
762 e37e863f bellard
    unsigned long pc;
763 e37e863f bellard
    int ret;
764 e37e863f bellard
765 e37e863f bellard
    /* XXX: hack to restore env in all cases, even if not called from
766 e37e863f bellard
       generated code */
767 e37e863f bellard
    saved_env = env;
768 e37e863f bellard
    env = cpu_single_env;
769 e37e863f bellard
    ret = cpu_mips_handle_mmu_fault(env, addr, is_write, is_user, 1);
770 e37e863f bellard
    if (ret) {
771 e37e863f bellard
        if (retaddr) {
772 e37e863f bellard
            /* now we have a real cpu fault */
773 e37e863f bellard
            pc = (unsigned long)retaddr;
774 e37e863f bellard
            tb = tb_find_pc(pc);
775 e37e863f bellard
            if (tb) {
776 e37e863f bellard
                /* the PC is inside the translated code. It means that we have
777 e37e863f bellard
                   a virtual CPU fault */
778 e37e863f bellard
                cpu_restore_state(tb, env, pc, NULL);
779 e37e863f bellard
            }
780 e37e863f bellard
        }
781 e37e863f bellard
        do_raise_exception_err(env->exception_index, env->error_code);
782 e37e863f bellard
    }
783 e37e863f bellard
    env = saved_env;
784 e37e863f bellard
}
785 e37e863f bellard
786 e37e863f bellard
#endif