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