2 * MIPS emulation helpers for qemu.
4 * Copyright (c) 2004-2005 Jocelyn Mayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #define MIPS_DEBUG_DISAS
24 #define GETPC() (__builtin_return_address(0))
26 /*****************************************************************************/
27 /* Exceptions processing helpers */
28 void cpu_loop_exit(void)
30 longjmp(env->jmp_env, 1);
33 void do_raise_exception_err (uint32_t exception, int error_code)
36 if (logfile && exception < 0x100)
37 fprintf(logfile, "%s: %d %d\n", __func__, exception, error_code);
39 env->exception_index = exception;
40 env->error_code = error_code;
45 void do_raise_exception (uint32_t exception)
47 do_raise_exception_err(exception, 0);
50 void do_restore_state (void *pc_ptr)
53 unsigned long pc = (unsigned long) pc_ptr;
56 cpu_restore_state (tb, env, pc, NULL);
59 void do_raise_exception_direct_err (uint32_t exception, int error_code)
61 do_restore_state (GETPC ());
62 do_raise_exception_err (exception, error_code);
65 void do_raise_exception_direct (uint32_t exception)
67 do_raise_exception_direct_err (exception, 0);
70 #define MEMSUFFIX _raw
71 #include "op_helper_mem.c"
73 #if !defined(CONFIG_USER_ONLY)
74 #define MEMSUFFIX _user
75 #include "op_helper_mem.c"
77 #define MEMSUFFIX _kernel
78 #include "op_helper_mem.c"
83 #if TARGET_LONG_BITS > HOST_LONG_BITS
84 /* Those might call libgcc functions. */
97 T0 = (int64_t)T0 >> T1;
100 void do_dsra32 (void)
102 T0 = (int64_t)T0 >> (T1 + 32);
110 void do_dsrl32 (void)
112 T0 = T0 >> (T1 + 32);
120 tmp = T0 << (0x40 - T1);
121 T0 = (T0 >> T1) | tmp;
126 void do_drotr32 (void)
131 tmp = T0 << (0x40 - (32 + T1));
132 T0 = (T0 >> (32 + T1)) | tmp;
139 T0 = T1 << (T0 & 0x3F);
144 T0 = (int64_t)T1 >> (T0 & 0x3F);
149 T0 = T1 >> (T0 & 0x3F);
152 void do_drotrv (void)
158 tmp = T1 << (0x40 - T0);
159 T0 = (T1 >> T0) | tmp;
163 #endif /* TARGET_LONG_BITS > HOST_LONG_BITS */
164 #endif /* TARGET_MIPS64 */
166 /* 64 bits arithmetic for 32 bits hosts */
167 #if TARGET_LONG_BITS > HOST_LONG_BITS
168 static inline uint64_t get_HILO (void)
170 return (env->HI << 32) | (uint32_t)env->LO;
173 static inline void set_HILO (uint64_t HILO)
175 env->LO = (int32_t)HILO;
176 env->HI = (int32_t)(HILO >> 32);
181 set_HILO((int64_t)(int32_t)T0 * (int64_t)(int32_t)T1);
186 set_HILO((uint64_t)(uint32_t)T0 * (uint64_t)(uint32_t)T1);
193 tmp = ((int64_t)(int32_t)T0 * (int64_t)(int32_t)T1);
194 set_HILO((int64_t)get_HILO() + tmp);
201 tmp = ((uint64_t)(uint32_t)T0 * (uint64_t)(uint32_t)T1);
202 set_HILO(get_HILO() + tmp);
209 tmp = ((int64_t)(int32_t)T0 * (int64_t)(int32_t)T1);
210 set_HILO((int64_t)get_HILO() - tmp);
217 tmp = ((uint64_t)(uint32_t)T0 * (uint64_t)(uint32_t)T1);
218 set_HILO(get_HILO() - tmp);
226 set_HILO((int64_t)T0 * (int64_t)T1);
229 void do_dmultu (void)
232 set_HILO((uint64_t)T0 * (uint64_t)T1);
238 env->LO = (int64_t)T0 / (int64_t)T1;
239 env->HI = (int64_t)T0 % (int64_t)T1;
252 #if defined(CONFIG_USER_ONLY)
253 void do_mfc0_random (void)
255 cpu_abort(env, "mfc0 random\n");
258 void do_mfc0_count (void)
260 cpu_abort(env, "mfc0 count\n");
263 void cpu_mips_store_count(CPUState *env, uint32_t value)
265 cpu_abort(env, "mtc0 count\n");
268 void cpu_mips_store_compare(CPUState *env, uint32_t value)
270 cpu_abort(env, "mtc0 compare\n");
273 void cpu_mips_update_irq(CPUState *env)
275 cpu_abort(env, "mtc0 status / mtc0 cause\n");
278 void do_mtc0_status_debug(uint32_t old, uint32_t val)
280 cpu_abort(env, "mtc0 status debug\n");
283 void do_mtc0_status_irqraise_debug (void)
285 cpu_abort(env, "mtc0 status irqraise debug\n");
290 cpu_abort(env, "tlbwi\n");
295 cpu_abort(env, "tlbwr\n");
300 cpu_abort(env, "tlbp\n");
305 cpu_abort(env, "tlbr\n");
308 void cpu_mips_tlb_flush (CPUState *env, int flush_global)
310 cpu_abort(env, "mips_tlb_flush\n");
316 void do_mfc0_random (void)
318 T0 = (int32_t)cpu_mips_get_random(env);
321 void do_mfc0_count (void)
323 T0 = (int32_t)cpu_mips_get_count(env);
326 void do_mtc0_status_debug(uint32_t old, uint32_t val)
328 const uint32_t mask = 0x0000FF00;
329 fprintf(logfile, "Status %08x => %08x Cause %08x (%08x %08x %08x)\n",
330 old, val, env->CP0_Cause, old & mask, val & mask,
331 env->CP0_Cause & mask);
334 void do_mtc0_status_irqraise_debug(void)
336 fprintf(logfile, "Raise pending IRQs\n");
339 void fpu_handle_exception(void)
341 #ifdef CONFIG_SOFTFLOAT
342 int flags = get_float_exception_flags(&env->fp_status);
343 unsigned int cpuflags = 0, enable, cause = 0;
345 enable = GET_FP_ENABLE(env->fcr31);
347 /* determine current flags */
348 if (flags & float_flag_invalid) {
349 cpuflags |= FP_INVALID;
350 cause |= FP_INVALID & enable;
352 if (flags & float_flag_divbyzero) {
354 cause |= FP_DIV0 & enable;
356 if (flags & float_flag_overflow) {
357 cpuflags |= FP_OVERFLOW;
358 cause |= FP_OVERFLOW & enable;
360 if (flags & float_flag_underflow) {
361 cpuflags |= FP_UNDERFLOW;
362 cause |= FP_UNDERFLOW & enable;
364 if (flags & float_flag_inexact) {
365 cpuflags |= FP_INEXACT;
366 cause |= FP_INEXACT & enable;
368 SET_FP_FLAGS(env->fcr31, cpuflags);
369 SET_FP_CAUSE(env->fcr31, cause);
371 SET_FP_FLAGS(env->fcr31, 0);
372 SET_FP_CAUSE(env->fcr31, 0);
377 #if defined(MIPS_USES_R4K_TLB)
378 void cpu_mips_tlb_flush (CPUState *env, int flush_global)
380 /* Flush qemu's TLB and discard all shadowed entries. */
381 tlb_flush (env, flush_global);
382 env->tlb_in_use = MIPS_TLB_NB;
385 static void mips_tlb_flush_extra (CPUState *env, int first)
387 /* Discard entries from env->tlb[first] onwards. */
388 while (env->tlb_in_use > first) {
389 invalidate_tlb(env, --env->tlb_in_use, 0);
393 static void fill_tlb (int idx)
397 /* XXX: detect conflicting TLBs and raise a MCHECK exception when needed */
398 tlb = &env->tlb[idx];
399 tlb->VPN = env->CP0_EntryHi & ~(target_ulong)0x1FFF;
400 tlb->ASID = env->CP0_EntryHi & 0xFF;
401 tlb->PageMask = env->CP0_PageMask;
402 tlb->G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
403 tlb->V0 = (env->CP0_EntryLo0 & 2) != 0;
404 tlb->D0 = (env->CP0_EntryLo0 & 4) != 0;
405 tlb->C0 = (env->CP0_EntryLo0 >> 3) & 0x7;
406 tlb->PFN[0] = (env->CP0_EntryLo0 >> 6) << 12;
407 tlb->V1 = (env->CP0_EntryLo1 & 2) != 0;
408 tlb->D1 = (env->CP0_EntryLo1 & 4) != 0;
409 tlb->C1 = (env->CP0_EntryLo1 >> 3) & 0x7;
410 tlb->PFN[1] = (env->CP0_EntryLo1 >> 6) << 12;
415 /* Discard cached TLB entries. We could avoid doing this if the
416 tlbwi is just upgrading access permissions on the current entry;
417 that might be a further win. */
418 mips_tlb_flush_extra (env, MIPS_TLB_NB);
420 /* Wildly undefined effects for CP0_Index containing a too high value and
421 MIPS_TLB_NB not being a power of two. But so does real silicon. */
422 invalidate_tlb(env, env->CP0_Index & (MIPS_TLB_NB - 1), 0);
423 fill_tlb(env->CP0_Index & (MIPS_TLB_NB - 1));
428 int r = cpu_mips_get_random(env);
430 invalidate_tlb(env, r, 1);
441 tag = env->CP0_EntryHi & (int32_t)0xFFFFE000;
442 ASID = env->CP0_EntryHi & 0xFF;
443 for (i = 0; i < MIPS_TLB_NB; i++) {
445 /* Check ASID, virtual page number & size */
446 if ((tlb->G == 1 || tlb->ASID == ASID) && tlb->VPN == tag) {
452 if (i == MIPS_TLB_NB) {
453 /* No match. Discard any shadow entries, if any of them match. */
454 for (i = MIPS_TLB_NB; i < env->tlb_in_use; i++) {
457 /* Check ASID, virtual page number & size */
458 if ((tlb->G == 1 || tlb->ASID == ASID) && tlb->VPN == tag) {
459 mips_tlb_flush_extra (env, i);
464 env->CP0_Index |= 0x80000000;
473 ASID = env->CP0_EntryHi & 0xFF;
474 tlb = &env->tlb[env->CP0_Index & (MIPS_TLB_NB - 1)];
476 /* If this will change the current ASID, flush qemu's TLB. */
477 if (ASID != tlb->ASID)
478 cpu_mips_tlb_flush (env, 1);
480 mips_tlb_flush_extra(env, MIPS_TLB_NB);
482 env->CP0_EntryHi = tlb->VPN | tlb->ASID;
483 env->CP0_PageMask = tlb->PageMask;
484 env->CP0_EntryLo0 = tlb->G | (tlb->V0 << 1) | (tlb->D0 << 2) |
485 (tlb->C0 << 3) | (tlb->PFN[0] >> 6);
486 env->CP0_EntryLo1 = tlb->G | (tlb->V1 << 1) | (tlb->D1 << 2) |
487 (tlb->C1 << 3) | (tlb->PFN[1] >> 6);
491 #endif /* !CONFIG_USER_ONLY */
493 void dump_ldst (const unsigned char *func)
496 fprintf(logfile, "%s => " TARGET_FMT_lx " " TARGET_FMT_lx "\n", __func__, T0, T1);
502 fprintf(logfile, "%s " TARGET_FMT_lx " at " TARGET_FMT_lx " (" TARGET_FMT_lx ")\n", __func__,
503 T1, T0, env->CP0_LLAddr);
507 void debug_eret (void)
510 fprintf(logfile, "ERET: pc " TARGET_FMT_lx " EPC " TARGET_FMT_lx,
511 env->PC, env->CP0_EPC);
512 if (env->CP0_Status & (1 << CP0St_ERL))
513 fprintf(logfile, " ErrorEPC " TARGET_FMT_lx, env->CP0_ErrorEPC);
514 fputs("\n", logfile);
518 void do_pmon (int function)
522 case 2: /* TODO: char inbyte(int waitflag); */
523 if (env->gpr[4] == 0)
526 case 11: /* TODO: char inbyte (void); */
531 printf("%c", (char)(env->gpr[4] & 0xFF));
537 unsigned char *fmt = (void *)(unsigned long)env->gpr[4];
544 #if !defined(CONFIG_USER_ONLY)
546 static void do_unaligned_access (target_ulong addr, int is_write, int is_user, void *retaddr);
548 #define MMUSUFFIX _mmu
552 #include "softmmu_template.h"
555 #include "softmmu_template.h"
558 #include "softmmu_template.h"
561 #include "softmmu_template.h"
563 static void do_unaligned_access (target_ulong addr, int is_write, int is_user, void *retaddr)
565 env->CP0_BadVAddr = addr;
566 do_restore_state (retaddr);
567 do_raise_exception ((is_write == 1) ? EXCP_AdES : EXCP_AdEL);
570 void tlb_fill (target_ulong addr, int is_write, int is_user, void *retaddr)
572 TranslationBlock *tb;
577 /* XXX: hack to restore env in all cases, even if not called from
580 env = cpu_single_env;
581 ret = cpu_mips_handle_mmu_fault(env, addr, is_write, is_user, 1);
584 /* now we have a real cpu fault */
585 pc = (unsigned long)retaddr;
588 /* the PC is inside the translated code. It means that we have
589 a virtual CPU fault */
590 cpu_restore_state(tb, env, pc, NULL);
593 do_raise_exception_err(env->exception_index, env->error_code);