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