Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (17.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 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
        if (env->hflags & MIPS_HFLAG_ERL)
223 90b37806 bellard
            T0 |= (1 << CP0St_ERL);
224 6af0bf9c bellard
        if (env->hflags & MIPS_HFLAG_EXL)
225 90b37806 bellard
            T0 |= (1 << CP0St_EXL);
226 6af0bf9c bellard
        rn = "Status";
227 6af0bf9c bellard
        break;
228 6af0bf9c bellard
    case 13:
229 6af0bf9c bellard
        T0 = env->CP0_Cause;
230 6af0bf9c bellard
        rn = "Cause";
231 6af0bf9c bellard
        break;
232 6af0bf9c bellard
    case 14:
233 6af0bf9c bellard
        T0 = env->CP0_EPC;
234 6af0bf9c bellard
        rn = "EPC";
235 6af0bf9c bellard
        break;
236 6af0bf9c bellard
    case 15:
237 6af0bf9c bellard
        T0 = env->CP0_PRid;
238 6af0bf9c bellard
        rn = "PRid";
239 6af0bf9c bellard
        break;
240 6af0bf9c bellard
    case 16:
241 6af0bf9c bellard
        switch (sel) {
242 6af0bf9c bellard
        case 0:
243 6af0bf9c bellard
            T0 = env->CP0_Config0;
244 6af0bf9c bellard
            rn = "Config";
245 6af0bf9c bellard
            break;
246 6af0bf9c bellard
        case 1:
247 6af0bf9c bellard
            T0 = env->CP0_Config1;
248 6af0bf9c bellard
            rn = "Config1";
249 6af0bf9c bellard
            break;
250 6af0bf9c bellard
        default:
251 6af0bf9c bellard
            rn = "Unknown config register";
252 6af0bf9c bellard
            break;
253 6af0bf9c bellard
        }
254 6af0bf9c bellard
        break;
255 6af0bf9c bellard
    case 17:
256 6af0bf9c bellard
        T0 = env->CP0_LLAddr >> 4;
257 6af0bf9c bellard
        rn = "LLAddr";
258 6af0bf9c bellard
        break;
259 6af0bf9c bellard
    case 18:
260 6af0bf9c bellard
        T0 = env->CP0_WatchLo;
261 6af0bf9c bellard
        rn = "WatchLo";
262 6af0bf9c bellard
        break;
263 6af0bf9c bellard
    case 19:
264 6af0bf9c bellard
        T0 = env->CP0_WatchHi;
265 6af0bf9c bellard
        rn = "WatchHi";
266 6af0bf9c bellard
        break;
267 6af0bf9c bellard
    case 23:
268 6af0bf9c bellard
        T0 = env->CP0_Debug;
269 6af0bf9c bellard
        if (env->hflags & MIPS_HFLAG_DM)
270 6af0bf9c bellard
            T0 |= 1 << CP0DB_DM;
271 6af0bf9c bellard
        rn = "Debug";
272 6af0bf9c bellard
        break;
273 6af0bf9c bellard
    case 24:
274 6af0bf9c bellard
        T0 = env->CP0_DEPC;
275 6af0bf9c bellard
        rn = "DEPC";
276 6af0bf9c bellard
        break;
277 6af0bf9c bellard
    case 28:
278 6af0bf9c bellard
        switch (sel) {
279 6af0bf9c bellard
        case 0:
280 6af0bf9c bellard
            T0 = env->CP0_TagLo;
281 6af0bf9c bellard
            rn = "TagLo";
282 6af0bf9c bellard
            break;
283 6af0bf9c bellard
        case 1:
284 6af0bf9c bellard
            T0 = env->CP0_DataLo;
285 6af0bf9c bellard
            rn = "DataLo";
286 6af0bf9c bellard
            break;
287 6af0bf9c bellard
        default:
288 6af0bf9c bellard
            rn = "unknown sel";
289 6af0bf9c bellard
            break;
290 6af0bf9c bellard
        }
291 6af0bf9c bellard
        break;
292 6af0bf9c bellard
    case 30:
293 6af0bf9c bellard
        T0 = env->CP0_ErrorEPC;
294 6af0bf9c bellard
        rn = "ErrorEPC";
295 6af0bf9c bellard
        break;
296 6af0bf9c bellard
    case 31:
297 6af0bf9c bellard
        T0 = env->CP0_DESAVE;
298 6af0bf9c bellard
        rn = "DESAVE";
299 6af0bf9c bellard
        break;
300 6af0bf9c bellard
    default:
301 6af0bf9c bellard
        rn = "unknown";
302 6af0bf9c bellard
        break;
303 6af0bf9c bellard
    }
304 6af0bf9c bellard
 print:
305 6af0bf9c bellard
#if defined MIPS_DEBUG_DISAS
306 6af0bf9c bellard
    if (loglevel & CPU_LOG_TB_IN_ASM) {
307 6af0bf9c bellard
        fprintf(logfile, "%08x mfc0 %s => %08x (%d %d)\n",
308 6af0bf9c bellard
                env->PC, rn, T0, reg, sel);
309 6af0bf9c bellard
    }
310 6af0bf9c bellard
#endif
311 6af0bf9c bellard
    return;
312 6af0bf9c bellard
}
313 6af0bf9c bellard
314 6af0bf9c bellard
void do_mtc0 (int reg, int sel)
315 6af0bf9c bellard
{
316 6af0bf9c bellard
    const unsigned char *rn;
317 6af0bf9c bellard
    uint32_t val, old, mask;
318 6af0bf9c bellard
319 6af0bf9c bellard
    if (sel != 0 && reg != 16 && reg != 28) {
320 6af0bf9c bellard
        val = -1;
321 6af0bf9c bellard
        old = -1;
322 6af0bf9c bellard
        rn = "invalid";
323 6af0bf9c bellard
        goto print;
324 6af0bf9c bellard
    }
325 6af0bf9c bellard
    switch (reg) {
326 6af0bf9c bellard
    case 0:
327 6af0bf9c bellard
        val = (env->CP0_index & 0x80000000) | (T0 & 0x0000000F);
328 6af0bf9c bellard
        old = env->CP0_index;
329 6af0bf9c bellard
        env->CP0_index = val;
330 6af0bf9c bellard
        rn = "Index";
331 6af0bf9c bellard
        break;
332 6af0bf9c bellard
    case 2:
333 3d9fb9fe bellard
        val = T0 & 0x3FFFFFFF;
334 6af0bf9c bellard
        old = env->CP0_EntryLo0;
335 6af0bf9c bellard
        env->CP0_EntryLo0 = val;
336 6af0bf9c bellard
        rn = "EntryLo0";
337 6af0bf9c bellard
        break;
338 6af0bf9c bellard
    case 3:
339 3d9fb9fe bellard
        val = T0 & 0x3FFFFFFF;
340 6af0bf9c bellard
        old = env->CP0_EntryLo1;
341 6af0bf9c bellard
        env->CP0_EntryLo1 = val;
342 6af0bf9c bellard
        rn = "EntryLo1";
343 6af0bf9c bellard
        break;
344 6af0bf9c bellard
    case 4:
345 6af0bf9c bellard
        val = (env->CP0_Context & 0xFF000000) | (T0 & 0x00FFFFF0);
346 6af0bf9c bellard
        old = env->CP0_Context;
347 6af0bf9c bellard
        env->CP0_Context = val;
348 6af0bf9c bellard
        rn = "Context";
349 6af0bf9c bellard
        break;
350 6af0bf9c bellard
    case 5:
351 6af0bf9c bellard
        val = T0 & 0x01FFE000;
352 6af0bf9c bellard
        old = env->CP0_PageMask;
353 6af0bf9c bellard
        env->CP0_PageMask = val;
354 6af0bf9c bellard
        rn = "PageMask";
355 6af0bf9c bellard
        break;
356 6af0bf9c bellard
    case 6:
357 6af0bf9c bellard
        val = T0 & 0x0000000F;
358 6af0bf9c bellard
        old = env->CP0_Wired;
359 6af0bf9c bellard
        env->CP0_Wired = val;
360 6af0bf9c bellard
        rn = "Wired";
361 6af0bf9c bellard
        break;
362 6af0bf9c bellard
    case 9:
363 6af0bf9c bellard
        val = T0;
364 6af0bf9c bellard
        old = cpu_mips_get_count(env);
365 6af0bf9c bellard
        cpu_mips_store_count(env, val);
366 6af0bf9c bellard
        rn = "Count";
367 6af0bf9c bellard
        break;
368 6af0bf9c bellard
    case 10:
369 6af0bf9c bellard
        val = T0 & 0xFFFFF0FF;
370 6af0bf9c bellard
        old = env->CP0_EntryHi;
371 6af0bf9c bellard
        env->CP0_EntryHi = val;
372 4ad40f36 bellard
        /* If the ASID changes, flush qemu's TLB.  */
373 4ad40f36 bellard
        if ((old & 0xFF) != (val & 0xFF))
374 4ad40f36 bellard
          tlb_flush (env, 1);
375 6af0bf9c bellard
        rn = "EntryHi";
376 6af0bf9c bellard
        break;
377 6af0bf9c bellard
    case 11:
378 6af0bf9c bellard
        val = T0;
379 6af0bf9c bellard
        old = env->CP0_Compare;
380 6af0bf9c bellard
        cpu_mips_store_compare(env, val);
381 6af0bf9c bellard
        rn = "Compare";
382 6af0bf9c bellard
        break;
383 6af0bf9c bellard
    case 12:
384 6af0bf9c bellard
        val = T0 & 0xFA78FF01;
385 6af0bf9c bellard
        if (T0 & (1 << CP0St_UM))
386 6af0bf9c bellard
            env->hflags |= MIPS_HFLAG_UM;
387 6af0bf9c bellard
        else
388 6af0bf9c bellard
            env->hflags &= ~MIPS_HFLAG_UM;
389 6af0bf9c bellard
        if (T0 & (1 << CP0St_ERL))
390 6af0bf9c bellard
            env->hflags |= MIPS_HFLAG_ERL;
391 6af0bf9c bellard
        else
392 6af0bf9c bellard
            env->hflags &= ~MIPS_HFLAG_ERL;
393 6af0bf9c bellard
        if (T0 & (1 << CP0St_EXL))
394 6af0bf9c bellard
            env->hflags |= MIPS_HFLAG_EXL;
395 6af0bf9c bellard
        else
396 6af0bf9c bellard
            env->hflags &= ~MIPS_HFLAG_EXL;
397 6af0bf9c bellard
        old = env->CP0_Status;
398 6af0bf9c bellard
        env->CP0_Status = val;
399 6af0bf9c bellard
        /* If we unmasked an asserted IRQ, raise it */
400 ae022501 bellard
        mask = 0x0000FF00;
401 6af0bf9c bellard
        if (loglevel & CPU_LOG_TB_IN_ASM) {
402 6af0bf9c bellard
            fprintf(logfile, "Status %08x => %08x Cause %08x (%08x %08x %08x)\n",
403 6af0bf9c bellard
                    old, val, env->CP0_Cause, old & mask, val & mask,
404 6af0bf9c bellard
                    env->CP0_Cause & mask);
405 6af0bf9c bellard
        }
406 6af0bf9c bellard
        if ((val & (1 << CP0St_IE)) && !(old & (1 << CP0St_IE)) &&
407 6af0bf9c bellard
            !(env->hflags & MIPS_HFLAG_EXL) &&
408 6af0bf9c bellard
            !(env->hflags & MIPS_HFLAG_ERL) &&
409 3d9fb9fe bellard
            !(env->hflags & MIPS_HFLAG_DM) &&
410 e1d9a508 bellard
            (env->CP0_Status & env->CP0_Cause & mask)) {
411 6af0bf9c bellard
            if (logfile)
412 6af0bf9c bellard
                fprintf(logfile, "Raise pending IRQs\n");
413 6af0bf9c bellard
            env->interrupt_request |= CPU_INTERRUPT_HARD;
414 3d9fb9fe bellard
        } else if (!(val & (1 << CP0St_IE)) && (old & (1 << CP0St_IE))) {
415 6af0bf9c bellard
            env->interrupt_request &= ~CPU_INTERRUPT_HARD;
416 6af0bf9c bellard
        }
417 6af0bf9c bellard
        rn = "Status";
418 6af0bf9c bellard
        break;
419 6af0bf9c bellard
    case 13:
420 6af0bf9c bellard
        val = (env->CP0_Cause & 0xB000F87C) | (T0 & 0x000C00300);
421 6af0bf9c bellard
        old = env->CP0_Cause;
422 6af0bf9c bellard
        env->CP0_Cause = val;
423 6af0bf9c bellard
#if 0
424 e37e863f bellard
        {
425 e37e863f bellard
            int i;
426 e37e863f bellard
            /* Check if we ever asserted a software IRQ */
427 e37e863f bellard
            for (i = 0; i < 2; i++) {
428 e37e863f bellard
                mask = 0x100 << i;
429 e37e863f bellard
                if ((val & mask) & !(old & mask))
430 e37e863f bellard
                    mips_set_irq(i);
431 e37e863f bellard
            }
432 6af0bf9c bellard
        }
433 6af0bf9c bellard
#endif
434 6af0bf9c bellard
        rn = "Cause";
435 6af0bf9c bellard
        break;
436 6af0bf9c bellard
    case 14:
437 6af0bf9c bellard
        val = T0;
438 6af0bf9c bellard
        old = env->CP0_EPC;
439 6af0bf9c bellard
        env->CP0_EPC = val;
440 6af0bf9c bellard
        rn = "EPC";
441 6af0bf9c bellard
        break;
442 6af0bf9c bellard
    case 16:
443 6af0bf9c bellard
        switch (sel) {
444 6af0bf9c bellard
        case 0:
445 6af0bf9c bellard
#if defined(MIPS_USES_R4K_TLB)
446 6af0bf9c bellard
            val = (env->CP0_Config0 & 0x8017FF80) | (T0 & 0x7E000001);
447 6af0bf9c bellard
#else
448 6af0bf9c bellard
            val = (env->CP0_Config0 & 0xFE17FF80) | (T0 & 0x00000001);
449 6af0bf9c bellard
#endif
450 6af0bf9c bellard
            old = env->CP0_Config0;
451 6af0bf9c bellard
            env->CP0_Config0 = val;
452 6af0bf9c bellard
            rn = "Config0";
453 6af0bf9c bellard
            break;
454 6af0bf9c bellard
        default:
455 6af0bf9c bellard
            val = -1;
456 6af0bf9c bellard
            old = -1;
457 6af0bf9c bellard
            rn = "bad config selector";
458 6af0bf9c bellard
            break;
459 6af0bf9c bellard
        }
460 6af0bf9c bellard
        break;
461 6af0bf9c bellard
    case 18:
462 6af0bf9c bellard
        val = T0;
463 6af0bf9c bellard
        old = env->CP0_WatchLo;
464 6af0bf9c bellard
        env->CP0_WatchLo = val;
465 6af0bf9c bellard
        rn = "WatchLo";
466 6af0bf9c bellard
        break;
467 6af0bf9c bellard
    case 19:
468 6af0bf9c bellard
        val = T0 & 0x40FF0FF8;
469 6af0bf9c bellard
        old = env->CP0_WatchHi;
470 6af0bf9c bellard
        env->CP0_WatchHi = val;
471 6af0bf9c bellard
        rn = "WatchHi";
472 6af0bf9c bellard
        break;
473 6af0bf9c bellard
    case 23:
474 6af0bf9c bellard
        val = (env->CP0_Debug & 0x8C03FC1F) | (T0 & 0x13300120);
475 6af0bf9c bellard
        if (T0 & (1 << CP0DB_DM))
476 6af0bf9c bellard
            env->hflags |= MIPS_HFLAG_DM;
477 6af0bf9c bellard
        else
478 6af0bf9c bellard
            env->hflags &= ~MIPS_HFLAG_DM;
479 6af0bf9c bellard
        old = env->CP0_Debug;
480 6af0bf9c bellard
        env->CP0_Debug = val;
481 6af0bf9c bellard
        rn = "Debug";
482 6af0bf9c bellard
        break;
483 6af0bf9c bellard
    case 24:
484 6af0bf9c bellard
        val = T0;
485 6af0bf9c bellard
        old = env->CP0_DEPC;
486 6af0bf9c bellard
        env->CP0_DEPC = val;
487 6af0bf9c bellard
        rn = "DEPC";
488 6af0bf9c bellard
        break;
489 6af0bf9c bellard
    case 28:
490 6af0bf9c bellard
        switch (sel) {
491 6af0bf9c bellard
        case 0:
492 6af0bf9c bellard
            val = T0 & 0xFFFFFCF6;
493 6af0bf9c bellard
            old = env->CP0_TagLo;
494 6af0bf9c bellard
            env->CP0_TagLo = val;
495 6af0bf9c bellard
            rn = "TagLo";
496 6af0bf9c bellard
            break;
497 6af0bf9c bellard
        default:
498 6af0bf9c bellard
            val = -1;
499 6af0bf9c bellard
            old = -1;
500 6af0bf9c bellard
            rn = "invalid sel";
501 6af0bf9c bellard
            break;
502 6af0bf9c bellard
        }
503 6af0bf9c bellard
        break;
504 6af0bf9c bellard
    case 30:
505 6af0bf9c bellard
        val = T0;
506 6af0bf9c bellard
        old = env->CP0_ErrorEPC;
507 6af0bf9c bellard
        env->CP0_ErrorEPC = val;
508 6af0bf9c bellard
        rn = "EPC";
509 6af0bf9c bellard
        break;
510 6af0bf9c bellard
    case 31:
511 6af0bf9c bellard
        val = T0;
512 6af0bf9c bellard
        old = env->CP0_DESAVE;
513 6af0bf9c bellard
        env->CP0_DESAVE = val;
514 6af0bf9c bellard
        rn = "DESAVE";
515 6af0bf9c bellard
        break;
516 6af0bf9c bellard
    default:
517 6af0bf9c bellard
        val = -1;
518 6af0bf9c bellard
        old = -1;
519 6af0bf9c bellard
        rn = "unknown";
520 6af0bf9c bellard
        break;
521 6af0bf9c bellard
    }
522 6af0bf9c bellard
 print:
523 6af0bf9c bellard
#if defined MIPS_DEBUG_DISAS
524 6af0bf9c bellard
    if (loglevel & CPU_LOG_TB_IN_ASM) {
525 6af0bf9c bellard
        fprintf(logfile, "%08x mtc0 %s %08x => %08x (%d %d %08x)\n",
526 6af0bf9c bellard
                env->PC, rn, T0, val, reg, sel, old);
527 6af0bf9c bellard
    }
528 6af0bf9c bellard
#endif
529 6af0bf9c bellard
    return;
530 6af0bf9c bellard
}
531 6af0bf9c bellard
532 6af0bf9c bellard
/* TLB management */
533 6af0bf9c bellard
#if defined(MIPS_USES_R4K_TLB)
534 98c1b82b pbrook
static void invalidate_tlb (int idx)
535 6af0bf9c bellard
{
536 6af0bf9c bellard
    tlb_t *tlb;
537 98c1b82b pbrook
    target_ulong addr;
538 6af0bf9c bellard
539 6af0bf9c bellard
    tlb = &env->tlb[idx];
540 98c1b82b pbrook
    if (tlb->V0) {
541 98c1b82b pbrook
        tb_invalidate_page_range(tlb->PFN[0], tlb->end - tlb->VPN);
542 4ad40f36 bellard
        addr = tlb->VPN;
543 4ad40f36 bellard
        while (addr < tlb->end) {
544 4ad40f36 bellard
            tlb_flush_page (env, addr);
545 4ad40f36 bellard
            addr += TARGET_PAGE_SIZE;
546 4ad40f36 bellard
        }
547 6af0bf9c bellard
    }
548 98c1b82b pbrook
    if (tlb->V1) {
549 98c1b82b pbrook
        tb_invalidate_page_range(tlb->PFN[1], tlb->end2 - tlb->end);
550 4ad40f36 bellard
        addr = tlb->end;
551 4ad40f36 bellard
        while (addr < tlb->end2) {
552 4ad40f36 bellard
            tlb_flush_page (env, addr);
553 4ad40f36 bellard
            addr += TARGET_PAGE_SIZE;
554 4ad40f36 bellard
        }
555 6af0bf9c bellard
    }
556 6af0bf9c bellard
}
557 6af0bf9c bellard
558 98c1b82b pbrook
static void fill_tlb (int idx)
559 6af0bf9c bellard
{
560 6af0bf9c bellard
    tlb_t *tlb;
561 6af0bf9c bellard
    int size;
562 6af0bf9c bellard
563 6af0bf9c bellard
    /* XXX: detect conflicting TLBs and raise a MCHECK exception when needed */
564 6af0bf9c bellard
    tlb = &env->tlb[idx];
565 6af0bf9c bellard
    tlb->VPN = env->CP0_EntryHi & 0xFFFFE000;
566 98c1b82b pbrook
    tlb->ASID = env->CP0_EntryHi & 0xFF;
567 6af0bf9c bellard
    size = env->CP0_PageMask >> 13;
568 6af0bf9c bellard
    size = 4 * (size + 1);
569 6af0bf9c bellard
    tlb->end = tlb->VPN + (1 << (8 + size));
570 4ad40f36 bellard
    tlb->end2 = tlb->end + (1 << (8 + size));
571 6af0bf9c bellard
    tlb->G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
572 98c1b82b pbrook
    tlb->V0 = (env->CP0_EntryLo0 & 2) != 0;
573 98c1b82b pbrook
    tlb->D0 = (env->CP0_EntryLo0 & 4) != 0;
574 98c1b82b pbrook
    tlb->C0 = (env->CP0_EntryLo0 >> 3) & 0x7;
575 6af0bf9c bellard
    tlb->PFN[0] = (env->CP0_EntryLo0 >> 6) << 12;
576 98c1b82b pbrook
    tlb->V1 = (env->CP0_EntryLo1 & 2) != 0;
577 98c1b82b pbrook
    tlb->D1 = (env->CP0_EntryLo1 & 4) != 0;
578 98c1b82b pbrook
    tlb->C1 = (env->CP0_EntryLo1 >> 3) & 0x7;
579 6af0bf9c bellard
    tlb->PFN[1] = (env->CP0_EntryLo1 >> 6) << 12;
580 6af0bf9c bellard
}
581 6af0bf9c bellard
582 6af0bf9c bellard
void do_tlbwi (void)
583 6af0bf9c bellard
{
584 7a962d30 bellard
    /* Wildly undefined effects for CP0_index containing a too high value and
585 7a962d30 bellard
       MIPS_TLB_NB not being a power of two.  But so does real silicon.  */
586 98c1b82b pbrook
    invalidate_tlb(env->CP0_index & (MIPS_TLB_NB - 1));
587 98c1b82b pbrook
    fill_tlb(env->CP0_index & (MIPS_TLB_NB - 1));
588 6af0bf9c bellard
}
589 6af0bf9c bellard
590 6af0bf9c bellard
void do_tlbwr (void)
591 6af0bf9c bellard
{
592 6af0bf9c bellard
    int r = cpu_mips_get_random(env);
593 6af0bf9c bellard
594 98c1b82b pbrook
    invalidate_tlb(r);
595 98c1b82b pbrook
    fill_tlb(r);
596 6af0bf9c bellard
}
597 6af0bf9c bellard
598 6af0bf9c bellard
void do_tlbp (void)
599 6af0bf9c bellard
{
600 6af0bf9c bellard
    tlb_t *tlb;
601 6af0bf9c bellard
    target_ulong tag;
602 6af0bf9c bellard
    uint8_t ASID;
603 6af0bf9c bellard
    int i;
604 6af0bf9c bellard
605 3d9fb9fe bellard
    tag = env->CP0_EntryHi & 0xFFFFE000;
606 3d9fb9fe bellard
    ASID = env->CP0_EntryHi & 0xFF;
607 3d9fb9fe bellard
    for (i = 0; i < MIPS_TLB_NB; i++) {
608 6af0bf9c bellard
        tlb = &env->tlb[i];
609 6af0bf9c bellard
        /* Check ASID, virtual page number & size */
610 6af0bf9c bellard
        if ((tlb->G == 1 || tlb->ASID == ASID) && tlb->VPN == tag) {
611 6af0bf9c bellard
            /* TLB match */
612 6af0bf9c bellard
            env->CP0_index = i;
613 6af0bf9c bellard
            break;
614 6af0bf9c bellard
        }
615 6af0bf9c bellard
    }
616 7a962d30 bellard
    if (i == MIPS_TLB_NB) {
617 6af0bf9c bellard
        env->CP0_index |= 0x80000000;
618 6af0bf9c bellard
    }
619 6af0bf9c bellard
}
620 6af0bf9c bellard
621 6af0bf9c bellard
void do_tlbr (void)
622 6af0bf9c bellard
{
623 6af0bf9c bellard
    tlb_t *tlb;
624 09c56b84 pbrook
    uint8_t ASID;
625 6af0bf9c bellard
    int size;
626 6af0bf9c bellard
627 09c56b84 pbrook
    ASID = env->CP0_EntryHi & 0xFF;
628 7a962d30 bellard
    tlb = &env->tlb[env->CP0_index & (MIPS_TLB_NB - 1)];
629 4ad40f36 bellard
630 4ad40f36 bellard
    /* If this will change the current ASID, flush qemu's TLB.  */
631 09c56b84 pbrook
    if (ASID != tlb->ASID && tlb->G != 1)
632 4ad40f36 bellard
      tlb_flush (env, 1);
633 4ad40f36 bellard
634 6af0bf9c bellard
    env->CP0_EntryHi = tlb->VPN | tlb->ASID;
635 6af0bf9c bellard
    size = (tlb->end - tlb->VPN) >> 12;
636 6af0bf9c bellard
    env->CP0_PageMask = (size - 1) << 13;
637 98c1b82b pbrook
    env->CP0_EntryLo0 = tlb->G | (tlb->V0 << 1) | (tlb->D0 << 2)
638 98c1b82b pbrook
                | (tlb->C0 << 3) | (tlb->PFN[0] >> 6);
639 98c1b82b pbrook
    env->CP0_EntryLo1 = tlb->G | (tlb->V1 << 1) | (tlb->D1 << 2)
640 98c1b82b pbrook
                | (tlb->C1 << 3) | (tlb->PFN[1] >> 6);
641 6af0bf9c bellard
}
642 6af0bf9c bellard
#endif
643 6af0bf9c bellard
644 048f6b4d bellard
#endif /* !CONFIG_USER_ONLY */
645 048f6b4d bellard
646 6af0bf9c bellard
void op_dump_ldst (const unsigned char *func)
647 6af0bf9c bellard
{
648 6af0bf9c bellard
    if (loglevel)
649 6af0bf9c bellard
        fprintf(logfile, "%s => %08x %08x\n", __func__, T0, T1);
650 6af0bf9c bellard
}
651 6af0bf9c bellard
652 6af0bf9c bellard
void dump_sc (void)
653 6af0bf9c bellard
{
654 6af0bf9c bellard
    if (loglevel) {
655 6af0bf9c bellard
        fprintf(logfile, "%s %08x at %08x (%08x)\n", __func__,
656 6af0bf9c bellard
                T1, T0, env->CP0_LLAddr);
657 6af0bf9c bellard
    }
658 6af0bf9c bellard
}
659 6af0bf9c bellard
660 6af0bf9c bellard
void debug_eret (void)
661 6af0bf9c bellard
{
662 6af0bf9c bellard
    if (loglevel) {
663 6af0bf9c bellard
        fprintf(logfile, "ERET: pc %08x EPC %08x ErrorEPC %08x (%d)\n",
664 6af0bf9c bellard
                env->PC, env->CP0_EPC, env->CP0_ErrorEPC,
665 6af0bf9c bellard
                env->hflags & MIPS_HFLAG_ERL ? 1 : 0);
666 6af0bf9c bellard
    }
667 6af0bf9c bellard
}
668 6af0bf9c bellard
669 6af0bf9c bellard
void do_pmon (int function)
670 6af0bf9c bellard
{
671 6af0bf9c bellard
    function /= 2;
672 6af0bf9c bellard
    switch (function) {
673 6af0bf9c bellard
    case 2: /* TODO: char inbyte(int waitflag); */
674 6af0bf9c bellard
        if (env->gpr[4] == 0)
675 6af0bf9c bellard
            env->gpr[2] = -1;
676 6af0bf9c bellard
        /* Fall through */
677 6af0bf9c bellard
    case 11: /* TODO: char inbyte (void); */
678 6af0bf9c bellard
        env->gpr[2] = -1;
679 6af0bf9c bellard
        break;
680 6af0bf9c bellard
    case 3:
681 6af0bf9c bellard
    case 12:
682 6af0bf9c bellard
        printf("%c", env->gpr[4] & 0xFF);
683 6af0bf9c bellard
        break;
684 6af0bf9c bellard
    case 17:
685 6af0bf9c bellard
        break;
686 6af0bf9c bellard
    case 158:
687 6af0bf9c bellard
        {
688 6af0bf9c bellard
            unsigned char *fmt = (void *)env->gpr[4];
689 6af0bf9c bellard
            printf("%s", fmt);
690 6af0bf9c bellard
        }
691 6af0bf9c bellard
        break;
692 6af0bf9c bellard
    }
693 6af0bf9c bellard
}
694 e37e863f bellard
695 e37e863f bellard
#if !defined(CONFIG_USER_ONLY) 
696 e37e863f bellard
697 4ad40f36 bellard
static void do_unaligned_access (target_ulong addr, int is_write, int is_user, void *retaddr);
698 4ad40f36 bellard
699 e37e863f bellard
#define MMUSUFFIX _mmu
700 4ad40f36 bellard
#define ALIGNED_ONLY
701 e37e863f bellard
702 e37e863f bellard
#define SHIFT 0
703 e37e863f bellard
#include "softmmu_template.h"
704 e37e863f bellard
705 e37e863f bellard
#define SHIFT 1
706 e37e863f bellard
#include "softmmu_template.h"
707 e37e863f bellard
708 e37e863f bellard
#define SHIFT 2
709 e37e863f bellard
#include "softmmu_template.h"
710 e37e863f bellard
711 e37e863f bellard
#define SHIFT 3
712 e37e863f bellard
#include "softmmu_template.h"
713 e37e863f bellard
714 4ad40f36 bellard
static void do_unaligned_access (target_ulong addr, int is_write, int is_user, void *retaddr)
715 4ad40f36 bellard
{
716 4ad40f36 bellard
    env->CP0_BadVAddr = addr;
717 4ad40f36 bellard
    do_restore_state (retaddr);
718 4ad40f36 bellard
    do_raise_exception ((is_write == 1) ? EXCP_AdES : EXCP_AdEL);
719 4ad40f36 bellard
}
720 4ad40f36 bellard
721 e37e863f bellard
void tlb_fill (target_ulong addr, int is_write, int is_user, void *retaddr)
722 e37e863f bellard
{
723 e37e863f bellard
    TranslationBlock *tb;
724 e37e863f bellard
    CPUState *saved_env;
725 e37e863f bellard
    unsigned long pc;
726 e37e863f bellard
    int ret;
727 e37e863f bellard
728 e37e863f bellard
    /* XXX: hack to restore env in all cases, even if not called from
729 e37e863f bellard
       generated code */
730 e37e863f bellard
    saved_env = env;
731 e37e863f bellard
    env = cpu_single_env;
732 e37e863f bellard
    ret = cpu_mips_handle_mmu_fault(env, addr, is_write, is_user, 1);
733 e37e863f bellard
    if (ret) {
734 e37e863f bellard
        if (retaddr) {
735 e37e863f bellard
            /* now we have a real cpu fault */
736 e37e863f bellard
            pc = (unsigned long)retaddr;
737 e37e863f bellard
            tb = tb_find_pc(pc);
738 e37e863f bellard
            if (tb) {
739 e37e863f bellard
                /* the PC is inside the translated code. It means that we have
740 e37e863f bellard
                   a virtual CPU fault */
741 e37e863f bellard
                cpu_restore_state(tb, env, pc, NULL);
742 e37e863f bellard
            }
743 e37e863f bellard
        }
744 e37e863f bellard
        do_raise_exception_err(env->exception_index, env->error_code);
745 e37e863f bellard
    }
746 e37e863f bellard
    env = saved_env;
747 e37e863f bellard
}
748 e37e863f bellard
749 e37e863f bellard
#endif