Statistics
| Branch: | Revision:

root / target-mips / op_helper.c @ fcb4a419

History | View | Annotate | Download (13.9 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 2d0e944d ths
#include <stdlib.h>
21 6af0bf9c bellard
#include "exec.h"
22 6af0bf9c bellard
23 4ad40f36 bellard
#define GETPC() (__builtin_return_address(0))
24 4ad40f36 bellard
25 6af0bf9c bellard
/*****************************************************************************/
26 6af0bf9c bellard
/* Exceptions processing helpers */
27 6af0bf9c bellard
void cpu_loop_exit(void)
28 6af0bf9c bellard
{
29 6af0bf9c bellard
    longjmp(env->jmp_env, 1);
30 6af0bf9c bellard
}
31 6af0bf9c bellard
32 6af0bf9c bellard
void do_raise_exception_err (uint32_t exception, int error_code)
33 6af0bf9c bellard
{
34 6af0bf9c bellard
#if 1
35 6af0bf9c bellard
    if (logfile && exception < 0x100)
36 6af0bf9c bellard
        fprintf(logfile, "%s: %d %d\n", __func__, exception, error_code);
37 6af0bf9c bellard
#endif
38 6af0bf9c bellard
    env->exception_index = exception;
39 6af0bf9c bellard
    env->error_code = error_code;
40 6af0bf9c bellard
    T0 = 0;
41 6af0bf9c bellard
    cpu_loop_exit();
42 6af0bf9c bellard
}
43 6af0bf9c bellard
44 6af0bf9c bellard
void do_raise_exception (uint32_t exception)
45 6af0bf9c bellard
{
46 6af0bf9c bellard
    do_raise_exception_err(exception, 0);
47 6af0bf9c bellard
}
48 6af0bf9c bellard
49 4ad40f36 bellard
void do_restore_state (void *pc_ptr)
50 4ad40f36 bellard
{
51 4ad40f36 bellard
  TranslationBlock *tb;
52 4ad40f36 bellard
  unsigned long pc = (unsigned long) pc_ptr;
53 4ad40f36 bellard
54 4ad40f36 bellard
  tb = tb_find_pc (pc);
55 4ad40f36 bellard
  cpu_restore_state (tb, env, pc, NULL);
56 4ad40f36 bellard
}
57 4ad40f36 bellard
58 e397ee33 ths
void do_raise_exception_direct_err (uint32_t exception, int error_code)
59 4ad40f36 bellard
{
60 4ad40f36 bellard
    do_restore_state (GETPC ());
61 e397ee33 ths
    do_raise_exception_err (exception, error_code);
62 e397ee33 ths
}
63 e397ee33 ths
64 e397ee33 ths
void do_raise_exception_direct (uint32_t exception)
65 e397ee33 ths
{
66 e397ee33 ths
    do_raise_exception_direct_err (exception, 0);
67 4ad40f36 bellard
}
68 4ad40f36 bellard
69 6af0bf9c bellard
#define MEMSUFFIX _raw
70 6af0bf9c bellard
#include "op_helper_mem.c"
71 6af0bf9c bellard
#undef MEMSUFFIX
72 6af0bf9c bellard
#if !defined(CONFIG_USER_ONLY)
73 6af0bf9c bellard
#define MEMSUFFIX _user
74 6af0bf9c bellard
#include "op_helper_mem.c"
75 6af0bf9c bellard
#undef MEMSUFFIX
76 6af0bf9c bellard
#define MEMSUFFIX _kernel
77 6af0bf9c bellard
#include "op_helper_mem.c"
78 6af0bf9c bellard
#undef MEMSUFFIX
79 6af0bf9c bellard
#endif
80 6af0bf9c bellard
81 60aa19ab ths
#ifdef TARGET_MIPS64
82 c570fd16 ths
#if TARGET_LONG_BITS > HOST_LONG_BITS
83 c570fd16 ths
/* Those might call libgcc functions.  */
84 c570fd16 ths
void do_dsll (void)
85 c570fd16 ths
{
86 c570fd16 ths
    T0 = T0 << T1;
87 c570fd16 ths
}
88 c570fd16 ths
89 c570fd16 ths
void do_dsll32 (void)
90 c570fd16 ths
{
91 c570fd16 ths
    T0 = T0 << (T1 + 32);
92 c570fd16 ths
}
93 c570fd16 ths
94 c570fd16 ths
void do_dsra (void)
95 c570fd16 ths
{
96 c570fd16 ths
    T0 = (int64_t)T0 >> T1;
97 c570fd16 ths
}
98 c570fd16 ths
99 c570fd16 ths
void do_dsra32 (void)
100 c570fd16 ths
{
101 c570fd16 ths
    T0 = (int64_t)T0 >> (T1 + 32);
102 c570fd16 ths
}
103 c570fd16 ths
104 c570fd16 ths
void do_dsrl (void)
105 c570fd16 ths
{
106 c570fd16 ths
    T0 = T0 >> T1;
107 c570fd16 ths
}
108 c570fd16 ths
109 c570fd16 ths
void do_dsrl32 (void)
110 c570fd16 ths
{
111 c570fd16 ths
    T0 = T0 >> (T1 + 32);
112 c570fd16 ths
}
113 c570fd16 ths
114 c570fd16 ths
void do_drotr (void)
115 c570fd16 ths
{
116 c570fd16 ths
    target_ulong tmp;
117 c570fd16 ths
118 c570fd16 ths
    if (T1) {
119 c570fd16 ths
       tmp = T0 << (0x40 - T1);
120 c570fd16 ths
       T0 = (T0 >> T1) | tmp;
121 5a63bcb2 ths
    }
122 c570fd16 ths
}
123 c570fd16 ths
124 c570fd16 ths
void do_drotr32 (void)
125 c570fd16 ths
{
126 c570fd16 ths
    target_ulong tmp;
127 c570fd16 ths
128 c570fd16 ths
    if (T1) {
129 c570fd16 ths
       tmp = T0 << (0x40 - (32 + T1));
130 c570fd16 ths
       T0 = (T0 >> (32 + T1)) | tmp;
131 5a63bcb2 ths
    }
132 c570fd16 ths
}
133 c570fd16 ths
134 c570fd16 ths
void do_dsllv (void)
135 c570fd16 ths
{
136 c570fd16 ths
    T0 = T1 << (T0 & 0x3F);
137 c570fd16 ths
}
138 c570fd16 ths
139 c570fd16 ths
void do_dsrav (void)
140 c570fd16 ths
{
141 c570fd16 ths
    T0 = (int64_t)T1 >> (T0 & 0x3F);
142 c570fd16 ths
}
143 c570fd16 ths
144 c570fd16 ths
void do_dsrlv (void)
145 c570fd16 ths
{
146 c570fd16 ths
    T0 = T1 >> (T0 & 0x3F);
147 c570fd16 ths
}
148 c570fd16 ths
149 c570fd16 ths
void do_drotrv (void)
150 c570fd16 ths
{
151 c570fd16 ths
    target_ulong tmp;
152 c570fd16 ths
153 c570fd16 ths
    T0 &= 0x3F;
154 c570fd16 ths
    if (T0) {
155 c570fd16 ths
       tmp = T1 << (0x40 - T0);
156 c570fd16 ths
       T0 = (T1 >> T0) | tmp;
157 c570fd16 ths
    } else
158 c570fd16 ths
       T0 = T1;
159 c570fd16 ths
}
160 c570fd16 ths
#endif /* TARGET_LONG_BITS > HOST_LONG_BITS */
161 60aa19ab ths
#endif /* TARGET_MIPS64 */
162 c570fd16 ths
163 6af0bf9c bellard
/* 64 bits arithmetic for 32 bits hosts */
164 c570fd16 ths
#if TARGET_LONG_BITS > HOST_LONG_BITS
165 6af0bf9c bellard
static inline uint64_t get_HILO (void)
166 6af0bf9c bellard
{
167 7495fd0f ths
    return (env->HI << 32) | (uint32_t)env->LO;
168 6af0bf9c bellard
}
169 6af0bf9c bellard
170 6af0bf9c bellard
static inline void set_HILO (uint64_t HILO)
171 6af0bf9c bellard
{
172 7495fd0f ths
    env->LO = (int32_t)HILO;
173 5dc4b744 ths
    env->HI = (int32_t)(HILO >> 32);
174 6af0bf9c bellard
}
175 6af0bf9c bellard
176 6af0bf9c bellard
void do_mult (void)
177 6af0bf9c bellard
{
178 4ad40f36 bellard
    set_HILO((int64_t)(int32_t)T0 * (int64_t)(int32_t)T1);
179 6af0bf9c bellard
}
180 6af0bf9c bellard
181 6af0bf9c bellard
void do_multu (void)
182 6af0bf9c bellard
{
183 c570fd16 ths
    set_HILO((uint64_t)(uint32_t)T0 * (uint64_t)(uint32_t)T1);
184 6af0bf9c bellard
}
185 6af0bf9c bellard
186 6af0bf9c bellard
void do_madd (void)
187 6af0bf9c bellard
{
188 6af0bf9c bellard
    int64_t tmp;
189 6af0bf9c bellard
190 4ad40f36 bellard
    tmp = ((int64_t)(int32_t)T0 * (int64_t)(int32_t)T1);
191 6af0bf9c bellard
    set_HILO((int64_t)get_HILO() + tmp);
192 6af0bf9c bellard
}
193 6af0bf9c bellard
194 6af0bf9c bellard
void do_maddu (void)
195 6af0bf9c bellard
{
196 6af0bf9c bellard
    uint64_t tmp;
197 6af0bf9c bellard
198 c570fd16 ths
    tmp = ((uint64_t)(uint32_t)T0 * (uint64_t)(uint32_t)T1);
199 6af0bf9c bellard
    set_HILO(get_HILO() + tmp);
200 6af0bf9c bellard
}
201 6af0bf9c bellard
202 6af0bf9c bellard
void do_msub (void)
203 6af0bf9c bellard
{
204 6af0bf9c bellard
    int64_t tmp;
205 6af0bf9c bellard
206 4ad40f36 bellard
    tmp = ((int64_t)(int32_t)T0 * (int64_t)(int32_t)T1);
207 6af0bf9c bellard
    set_HILO((int64_t)get_HILO() - tmp);
208 6af0bf9c bellard
}
209 6af0bf9c bellard
210 6af0bf9c bellard
void do_msubu (void)
211 6af0bf9c bellard
{
212 6af0bf9c bellard
    uint64_t tmp;
213 6af0bf9c bellard
214 c570fd16 ths
    tmp = ((uint64_t)(uint32_t)T0 * (uint64_t)(uint32_t)T1);
215 6af0bf9c bellard
    set_HILO(get_HILO() - tmp);
216 6af0bf9c bellard
}
217 6af0bf9c bellard
#endif
218 6af0bf9c bellard
219 80c27194 ths
#if HOST_LONG_BITS < 64
220 80c27194 ths
void do_div (void)
221 80c27194 ths
{
222 80c27194 ths
    /* 64bit datatypes because we may see overflow/underflow. */
223 80c27194 ths
    if (T1 != 0) {
224 80c27194 ths
        env->LO = (int32_t)((int64_t)(int32_t)T0 / (int32_t)T1);
225 80c27194 ths
        env->HI = (int32_t)((int64_t)(int32_t)T0 % (int32_t)T1);
226 80c27194 ths
    }
227 80c27194 ths
}
228 80c27194 ths
#endif
229 80c27194 ths
230 60aa19ab ths
#ifdef TARGET_MIPS64
231 c570fd16 ths
void do_dmult (void)
232 c570fd16 ths
{
233 2d0e944d ths
    env->LO = (int64_t)T0 * (int64_t)T1;
234 c570fd16 ths
    /* XXX */
235 2d0e944d ths
    env->HI = (env->LO | (1ULL << 63)) ? ~0ULL : 0ULL;
236 c570fd16 ths
}
237 c570fd16 ths
238 c570fd16 ths
void do_dmultu (void)
239 c570fd16 ths
{
240 2d0e944d ths
    env->LO = T0 * T1;
241 c570fd16 ths
    /* XXX */
242 2d0e944d ths
    env->HI = 0;
243 c570fd16 ths
}
244 c570fd16 ths
245 c570fd16 ths
void do_ddiv (void)
246 c570fd16 ths
{
247 c570fd16 ths
    if (T1 != 0) {
248 2d0e944d ths
        lldiv_t res = lldiv((int64_t)T0, (int64_t)T1);
249 2d0e944d ths
        env->LO = res.quot;
250 2d0e944d ths
        env->HI = res.rem;
251 c570fd16 ths
    }
252 c570fd16 ths
}
253 c570fd16 ths
254 c570fd16 ths
void do_ddivu (void)
255 c570fd16 ths
{
256 c570fd16 ths
    if (T1 != 0) {
257 2d0e944d ths
        /* XXX: lldivu? */
258 2d0e944d ths
        lldiv_t res = lldiv(T0, T1);
259 2d0e944d ths
        env->LO = (uint64_t)res.quot;
260 2d0e944d ths
        env->HI = (uint64_t)res.rem;
261 c570fd16 ths
    }
262 c570fd16 ths
}
263 c570fd16 ths
#endif
264 c570fd16 ths
265 048f6b4d bellard
#if defined(CONFIG_USER_ONLY) 
266 873eb012 ths
void do_mfc0_random (void)
267 048f6b4d bellard
{
268 873eb012 ths
    cpu_abort(env, "mfc0 random\n");
269 048f6b4d bellard
}
270 873eb012 ths
271 873eb012 ths
void do_mfc0_count (void)
272 873eb012 ths
{
273 873eb012 ths
    cpu_abort(env, "mfc0 count\n");
274 873eb012 ths
}
275 873eb012 ths
276 8c0fdd85 ths
void cpu_mips_store_count(CPUState *env, uint32_t value)
277 048f6b4d bellard
{
278 8c0fdd85 ths
    cpu_abort(env, "mtc0 count\n");
279 8c0fdd85 ths
}
280 8c0fdd85 ths
281 8c0fdd85 ths
void cpu_mips_store_compare(CPUState *env, uint32_t value)
282 8c0fdd85 ths
{
283 8c0fdd85 ths
    cpu_abort(env, "mtc0 compare\n");
284 8c0fdd85 ths
}
285 8c0fdd85 ths
286 4de9b249 ths
void cpu_mips_update_irq(CPUState *env)
287 4de9b249 ths
{
288 4de9b249 ths
    cpu_abort(env, "mtc0 status / mtc0 cause\n");
289 4de9b249 ths
}
290 4de9b249 ths
291 8c0fdd85 ths
void do_mtc0_status_debug(uint32_t old, uint32_t val)
292 8c0fdd85 ths
{
293 7a387fff ths
    cpu_abort(env, "mtc0 status debug\n");
294 8c0fdd85 ths
}
295 8c0fdd85 ths
296 7a387fff ths
void do_mtc0_status_irqraise_debug (void)
297 8c0fdd85 ths
{
298 7a387fff ths
    cpu_abort(env, "mtc0 status irqraise debug\n");
299 048f6b4d bellard
}
300 048f6b4d bellard
301 048f6b4d bellard
void do_tlbwi (void)
302 048f6b4d bellard
{
303 048f6b4d bellard
    cpu_abort(env, "tlbwi\n");
304 048f6b4d bellard
}
305 048f6b4d bellard
306 048f6b4d bellard
void do_tlbwr (void)
307 048f6b4d bellard
{
308 048f6b4d bellard
    cpu_abort(env, "tlbwr\n");
309 048f6b4d bellard
}
310 048f6b4d bellard
311 048f6b4d bellard
void do_tlbp (void)
312 048f6b4d bellard
{
313 048f6b4d bellard
    cpu_abort(env, "tlbp\n");
314 048f6b4d bellard
}
315 048f6b4d bellard
316 048f6b4d bellard
void do_tlbr (void)
317 048f6b4d bellard
{
318 048f6b4d bellard
    cpu_abort(env, "tlbr\n");
319 048f6b4d bellard
}
320 873eb012 ths
321 8c0fdd85 ths
void cpu_mips_tlb_flush (CPUState *env, int flush_global)
322 8c0fdd85 ths
{
323 8c0fdd85 ths
    cpu_abort(env, "mips_tlb_flush\n");
324 8c0fdd85 ths
}
325 8c0fdd85 ths
326 048f6b4d bellard
#else
327 048f6b4d bellard
328 6af0bf9c bellard
/* CP0 helpers */
329 873eb012 ths
void do_mfc0_random (void)
330 6af0bf9c bellard
{
331 5dc4b744 ths
    T0 = (int32_t)cpu_mips_get_random(env);
332 873eb012 ths
}
333 6af0bf9c bellard
334 873eb012 ths
void do_mfc0_count (void)
335 873eb012 ths
{
336 5dc4b744 ths
    T0 = (int32_t)cpu_mips_get_count(env);
337 6af0bf9c bellard
}
338 6af0bf9c bellard
339 8c0fdd85 ths
void do_mtc0_status_debug(uint32_t old, uint32_t val)
340 6af0bf9c bellard
{
341 f41c52f1 ths
    fprintf(logfile, "Status %08x (%08x) => %08x (%08x) Cause %08x",
342 f41c52f1 ths
            old, old & env->CP0_Cause & CP0Ca_IP_mask,
343 f41c52f1 ths
            val, val & env->CP0_Cause & CP0Ca_IP_mask,
344 f41c52f1 ths
            env->CP0_Cause);
345 f41c52f1 ths
    (env->hflags & MIPS_HFLAG_UM) ? fputs(", UM\n", logfile)
346 f41c52f1 ths
                                  : fputs("\n", logfile);
347 8c0fdd85 ths
}
348 8c0fdd85 ths
349 8c0fdd85 ths
void do_mtc0_status_irqraise_debug(void)
350 8c0fdd85 ths
{
351 8c0fdd85 ths
    fprintf(logfile, "Raise pending IRQs\n");
352 6af0bf9c bellard
}
353 6af0bf9c bellard
354 6ea83fed bellard
void fpu_handle_exception(void)
355 6ea83fed bellard
{
356 6ea83fed bellard
#ifdef CONFIG_SOFTFLOAT
357 6ea83fed bellard
    int flags = get_float_exception_flags(&env->fp_status);
358 6ea83fed bellard
    unsigned int cpuflags = 0, enable, cause = 0;
359 6ea83fed bellard
360 6ea83fed bellard
    enable = GET_FP_ENABLE(env->fcr31);
361 6ea83fed bellard
362 6ea83fed bellard
    /* determine current flags */   
363 6ea83fed bellard
    if (flags & float_flag_invalid) {
364 6ea83fed bellard
        cpuflags |= FP_INVALID;
365 6ea83fed bellard
        cause |= FP_INVALID & enable;
366 6ea83fed bellard
    }
367 6ea83fed bellard
    if (flags & float_flag_divbyzero) {
368 6ea83fed bellard
        cpuflags |= FP_DIV0;    
369 6ea83fed bellard
        cause |= FP_DIV0 & enable;
370 6ea83fed bellard
    }
371 6ea83fed bellard
    if (flags & float_flag_overflow) {
372 6ea83fed bellard
        cpuflags |= FP_OVERFLOW;    
373 6ea83fed bellard
        cause |= FP_OVERFLOW & enable;
374 6ea83fed bellard
    }
375 6ea83fed bellard
    if (flags & float_flag_underflow) {
376 6ea83fed bellard
        cpuflags |= FP_UNDERFLOW;   
377 6ea83fed bellard
        cause |= FP_UNDERFLOW & enable;
378 6ea83fed bellard
    }
379 6ea83fed bellard
    if (flags & float_flag_inexact) {
380 6ea83fed bellard
        cpuflags |= FP_INEXACT; 
381 6ea83fed bellard
        cause |= FP_INEXACT & enable;
382 6ea83fed bellard
    }
383 6ea83fed bellard
    SET_FP_FLAGS(env->fcr31, cpuflags);
384 6ea83fed bellard
    SET_FP_CAUSE(env->fcr31, cause);
385 6ea83fed bellard
#else
386 6ea83fed bellard
    SET_FP_FLAGS(env->fcr31, 0);
387 6ea83fed bellard
    SET_FP_CAUSE(env->fcr31, 0);
388 6ea83fed bellard
#endif
389 6ea83fed bellard
}
390 6ea83fed bellard
391 6af0bf9c bellard
/* TLB management */
392 6af0bf9c bellard
#if defined(MIPS_USES_R4K_TLB)
393 814b9a47 ths
void cpu_mips_tlb_flush (CPUState *env, int flush_global)
394 814b9a47 ths
{
395 814b9a47 ths
    /* Flush qemu's TLB and discard all shadowed entries.  */
396 814b9a47 ths
    tlb_flush (env, flush_global);
397 fcb4a419 ths
    env->tlb_in_use = env->nb_tlb;
398 814b9a47 ths
}
399 814b9a47 ths
400 814b9a47 ths
static void mips_tlb_flush_extra (CPUState *env, int first)
401 814b9a47 ths
{
402 814b9a47 ths
    /* Discard entries from env->tlb[first] onwards.  */
403 814b9a47 ths
    while (env->tlb_in_use > first) {
404 2ee4aed8 bellard
        invalidate_tlb(env, --env->tlb_in_use, 0);
405 814b9a47 ths
    }
406 814b9a47 ths
}
407 814b9a47 ths
408 98c1b82b pbrook
static void fill_tlb (int idx)
409 6af0bf9c bellard
{
410 6af0bf9c bellard
    tlb_t *tlb;
411 6af0bf9c bellard
412 6af0bf9c bellard
    /* XXX: detect conflicting TLBs and raise a MCHECK exception when needed */
413 6af0bf9c bellard
    tlb = &env->tlb[idx];
414 925fd0f2 ths
    tlb->VPN = env->CP0_EntryHi & ~(target_ulong)0x1FFF;
415 98c1b82b pbrook
    tlb->ASID = env->CP0_EntryHi & 0xFF;
416 3b1c8be4 ths
    tlb->PageMask = env->CP0_PageMask;
417 6af0bf9c bellard
    tlb->G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
418 98c1b82b pbrook
    tlb->V0 = (env->CP0_EntryLo0 & 2) != 0;
419 98c1b82b pbrook
    tlb->D0 = (env->CP0_EntryLo0 & 4) != 0;
420 98c1b82b pbrook
    tlb->C0 = (env->CP0_EntryLo0 >> 3) & 0x7;
421 6af0bf9c bellard
    tlb->PFN[0] = (env->CP0_EntryLo0 >> 6) << 12;
422 98c1b82b pbrook
    tlb->V1 = (env->CP0_EntryLo1 & 2) != 0;
423 98c1b82b pbrook
    tlb->D1 = (env->CP0_EntryLo1 & 4) != 0;
424 98c1b82b pbrook
    tlb->C1 = (env->CP0_EntryLo1 >> 3) & 0x7;
425 6af0bf9c bellard
    tlb->PFN[1] = (env->CP0_EntryLo1 >> 6) << 12;
426 6af0bf9c bellard
}
427 6af0bf9c bellard
428 6af0bf9c bellard
void do_tlbwi (void)
429 6af0bf9c bellard
{
430 814b9a47 ths
    /* Discard cached TLB entries.  We could avoid doing this if the
431 814b9a47 ths
       tlbwi is just upgrading access permissions on the current entry;
432 814b9a47 ths
       that might be a further win.  */
433 fcb4a419 ths
    mips_tlb_flush_extra (env, env->nb_tlb);
434 814b9a47 ths
435 fcb4a419 ths
    invalidate_tlb(env, env->CP0_Index % env->nb_tlb, 0);
436 fcb4a419 ths
    fill_tlb(env->CP0_Index % env->nb_tlb);
437 6af0bf9c bellard
}
438 6af0bf9c bellard
439 6af0bf9c bellard
void do_tlbwr (void)
440 6af0bf9c bellard
{
441 6af0bf9c bellard
    int r = cpu_mips_get_random(env);
442 6af0bf9c bellard
443 2ee4aed8 bellard
    invalidate_tlb(env, r, 1);
444 98c1b82b pbrook
    fill_tlb(r);
445 6af0bf9c bellard
}
446 6af0bf9c bellard
447 6af0bf9c bellard
void do_tlbp (void)
448 6af0bf9c bellard
{
449 6af0bf9c bellard
    tlb_t *tlb;
450 6af0bf9c bellard
    target_ulong tag;
451 6af0bf9c bellard
    uint8_t ASID;
452 6af0bf9c bellard
    int i;
453 6af0bf9c bellard
454 5dc4b744 ths
    tag = env->CP0_EntryHi & (int32_t)0xFFFFE000;
455 3d9fb9fe bellard
    ASID = env->CP0_EntryHi & 0xFF;
456 fcb4a419 ths
    for (i = 0; i < env->nb_tlb; i++) {
457 6af0bf9c bellard
        tlb = &env->tlb[i];
458 6af0bf9c bellard
        /* Check ASID, virtual page number & size */
459 6af0bf9c bellard
        if ((tlb->G == 1 || tlb->ASID == ASID) && tlb->VPN == tag) {
460 6af0bf9c bellard
            /* TLB match */
461 9c2149c8 ths
            env->CP0_Index = i;
462 6af0bf9c bellard
            break;
463 6af0bf9c bellard
        }
464 6af0bf9c bellard
    }
465 fcb4a419 ths
    if (i == env->nb_tlb) {
466 814b9a47 ths
        /* No match.  Discard any shadow entries, if any of them match.  */
467 fcb4a419 ths
        for (i = env->nb_tlb; i < env->tlb_in_use; i++) {
468 814b9a47 ths
            tlb = &env->tlb[i];
469 814b9a47 ths
470 814b9a47 ths
            /* Check ASID, virtual page number & size */
471 814b9a47 ths
            if ((tlb->G == 1 || tlb->ASID == ASID) && tlb->VPN == tag) {
472 814b9a47 ths
                mips_tlb_flush_extra (env, i);
473 814b9a47 ths
                break;
474 814b9a47 ths
            }
475 814b9a47 ths
        }
476 814b9a47 ths
477 9c2149c8 ths
        env->CP0_Index |= 0x80000000;
478 6af0bf9c bellard
    }
479 6af0bf9c bellard
}
480 6af0bf9c bellard
481 6af0bf9c bellard
void do_tlbr (void)
482 6af0bf9c bellard
{
483 6af0bf9c bellard
    tlb_t *tlb;
484 09c56b84 pbrook
    uint8_t ASID;
485 6af0bf9c bellard
486 09c56b84 pbrook
    ASID = env->CP0_EntryHi & 0xFF;
487 fcb4a419 ths
    tlb = &env->tlb[env->CP0_Index % env->nb_tlb];
488 4ad40f36 bellard
489 4ad40f36 bellard
    /* If this will change the current ASID, flush qemu's TLB.  */
490 814b9a47 ths
    if (ASID != tlb->ASID)
491 814b9a47 ths
        cpu_mips_tlb_flush (env, 1);
492 814b9a47 ths
493 fcb4a419 ths
    mips_tlb_flush_extra(env, env->nb_tlb);
494 4ad40f36 bellard
495 6af0bf9c bellard
    env->CP0_EntryHi = tlb->VPN | tlb->ASID;
496 3b1c8be4 ths
    env->CP0_PageMask = tlb->PageMask;
497 7495fd0f ths
    env->CP0_EntryLo0 = tlb->G | (tlb->V0 << 1) | (tlb->D0 << 2) |
498 7495fd0f ths
                        (tlb->C0 << 3) | (tlb->PFN[0] >> 6);
499 7495fd0f ths
    env->CP0_EntryLo1 = tlb->G | (tlb->V1 << 1) | (tlb->D1 << 2) |
500 7495fd0f ths
                        (tlb->C1 << 3) | (tlb->PFN[1] >> 6);
501 6af0bf9c bellard
}
502 6af0bf9c bellard
#endif
503 6af0bf9c bellard
504 048f6b4d bellard
#endif /* !CONFIG_USER_ONLY */
505 048f6b4d bellard
506 c570fd16 ths
void dump_ldst (const unsigned char *func)
507 6af0bf9c bellard
{
508 6af0bf9c bellard
    if (loglevel)
509 3594c774 ths
        fprintf(logfile, "%s => " TARGET_FMT_lx " " TARGET_FMT_lx "\n", __func__, T0, T1);
510 6af0bf9c bellard
}
511 6af0bf9c bellard
512 6af0bf9c bellard
void dump_sc (void)
513 6af0bf9c bellard
{
514 6af0bf9c bellard
    if (loglevel) {
515 3594c774 ths
        fprintf(logfile, "%s " TARGET_FMT_lx " at " TARGET_FMT_lx " (" TARGET_FMT_lx ")\n", __func__,
516 6af0bf9c bellard
                T1, T0, env->CP0_LLAddr);
517 6af0bf9c bellard
    }
518 6af0bf9c bellard
}
519 6af0bf9c bellard
520 f41c52f1 ths
void debug_pre_eret (void)
521 6af0bf9c bellard
{
522 f41c52f1 ths
    fprintf(logfile, "ERET: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
523 f41c52f1 ths
            env->PC, env->CP0_EPC);
524 f41c52f1 ths
    if (env->CP0_Status & (1 << CP0St_ERL))
525 f41c52f1 ths
        fprintf(logfile, " ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
526 f41c52f1 ths
    if (env->hflags & MIPS_HFLAG_DM)
527 f41c52f1 ths
        fprintf(logfile, " DEPC " TARGET_FMT_lx, env->CP0_DEPC);
528 f41c52f1 ths
    fputs("\n", logfile);
529 f41c52f1 ths
}
530 f41c52f1 ths
531 f41c52f1 ths
void debug_post_eret (void)
532 f41c52f1 ths
{
533 744e0915 ths
    fprintf(logfile, "  =>  PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
534 f41c52f1 ths
            env->PC, env->CP0_EPC);
535 f41c52f1 ths
    if (env->CP0_Status & (1 << CP0St_ERL))
536 f41c52f1 ths
        fprintf(logfile, " ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
537 f41c52f1 ths
    if (env->hflags & MIPS_HFLAG_DM)
538 f41c52f1 ths
        fprintf(logfile, " DEPC " TARGET_FMT_lx, env->CP0_DEPC);
539 f41c52f1 ths
    if (env->hflags & MIPS_HFLAG_UM)
540 f41c52f1 ths
        fputs(", UM\n", logfile);
541 f41c52f1 ths
    else
542 24c7b0e3 ths
        fputs("\n", logfile);
543 6af0bf9c bellard
}
544 6af0bf9c bellard
545 6af0bf9c bellard
void do_pmon (int function)
546 6af0bf9c bellard
{
547 6af0bf9c bellard
    function /= 2;
548 6af0bf9c bellard
    switch (function) {
549 6af0bf9c bellard
    case 2: /* TODO: char inbyte(int waitflag); */
550 6af0bf9c bellard
        if (env->gpr[4] == 0)
551 6af0bf9c bellard
            env->gpr[2] = -1;
552 6af0bf9c bellard
        /* Fall through */
553 6af0bf9c bellard
    case 11: /* TODO: char inbyte (void); */
554 6af0bf9c bellard
        env->gpr[2] = -1;
555 6af0bf9c bellard
        break;
556 6af0bf9c bellard
    case 3:
557 6af0bf9c bellard
    case 12:
558 c570fd16 ths
        printf("%c", (char)(env->gpr[4] & 0xFF));
559 6af0bf9c bellard
        break;
560 6af0bf9c bellard
    case 17:
561 6af0bf9c bellard
        break;
562 6af0bf9c bellard
    case 158:
563 6af0bf9c bellard
        {
564 c570fd16 ths
            unsigned char *fmt = (void *)(unsigned long)env->gpr[4];
565 6af0bf9c bellard
            printf("%s", fmt);
566 6af0bf9c bellard
        }
567 6af0bf9c bellard
        break;
568 6af0bf9c bellard
    }
569 6af0bf9c bellard
}
570 e37e863f bellard
571 e37e863f bellard
#if !defined(CONFIG_USER_ONLY) 
572 e37e863f bellard
573 4ad40f36 bellard
static void do_unaligned_access (target_ulong addr, int is_write, int is_user, void *retaddr);
574 4ad40f36 bellard
575 e37e863f bellard
#define MMUSUFFIX _mmu
576 4ad40f36 bellard
#define ALIGNED_ONLY
577 e37e863f bellard
578 e37e863f bellard
#define SHIFT 0
579 e37e863f bellard
#include "softmmu_template.h"
580 e37e863f bellard
581 e37e863f bellard
#define SHIFT 1
582 e37e863f bellard
#include "softmmu_template.h"
583 e37e863f bellard
584 e37e863f bellard
#define SHIFT 2
585 e37e863f bellard
#include "softmmu_template.h"
586 e37e863f bellard
587 e37e863f bellard
#define SHIFT 3
588 e37e863f bellard
#include "softmmu_template.h"
589 e37e863f bellard
590 4ad40f36 bellard
static void do_unaligned_access (target_ulong addr, int is_write, int is_user, void *retaddr)
591 4ad40f36 bellard
{
592 4ad40f36 bellard
    env->CP0_BadVAddr = addr;
593 4ad40f36 bellard
    do_restore_state (retaddr);
594 4ad40f36 bellard
    do_raise_exception ((is_write == 1) ? EXCP_AdES : EXCP_AdEL);
595 4ad40f36 bellard
}
596 4ad40f36 bellard
597 e37e863f bellard
void tlb_fill (target_ulong addr, int is_write, int is_user, void *retaddr)
598 e37e863f bellard
{
599 e37e863f bellard
    TranslationBlock *tb;
600 e37e863f bellard
    CPUState *saved_env;
601 e37e863f bellard
    unsigned long pc;
602 e37e863f bellard
    int ret;
603 e37e863f bellard
604 e37e863f bellard
    /* XXX: hack to restore env in all cases, even if not called from
605 e37e863f bellard
       generated code */
606 e37e863f bellard
    saved_env = env;
607 e37e863f bellard
    env = cpu_single_env;
608 e37e863f bellard
    ret = cpu_mips_handle_mmu_fault(env, addr, is_write, is_user, 1);
609 e37e863f bellard
    if (ret) {
610 e37e863f bellard
        if (retaddr) {
611 e37e863f bellard
            /* now we have a real cpu fault */
612 e37e863f bellard
            pc = (unsigned long)retaddr;
613 e37e863f bellard
            tb = tb_find_pc(pc);
614 e37e863f bellard
            if (tb) {
615 e37e863f bellard
                /* the PC is inside the translated code. It means that we have
616 e37e863f bellard
                   a virtual CPU fault */
617 e37e863f bellard
                cpu_restore_state(tb, env, pc, NULL);
618 e37e863f bellard
            }
619 e37e863f bellard
        }
620 e37e863f bellard
        do_raise_exception_err(env->exception_index, env->error_code);
621 e37e863f bellard
    }
622 e37e863f bellard
    env = saved_env;
623 e37e863f bellard
}
624 e37e863f bellard
625 e37e863f bellard
#endif