root / target-mips / op_helper.c @ 8c0fdd85
History | View | Annotate | Download (12.3 kB)
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 |
*/
|
20 |
#include "exec.h" |
21 |
|
22 |
#define MIPS_DEBUG_DISAS
|
23 |
|
24 |
#define GETPC() (__builtin_return_address(0)) |
25 |
|
26 |
/*****************************************************************************/
|
27 |
/* Exceptions processing helpers */
|
28 |
void cpu_loop_exit(void) |
29 |
{ |
30 |
longjmp(env->jmp_env, 1);
|
31 |
} |
32 |
|
33 |
void 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 |
|
45 |
void do_raise_exception (uint32_t exception)
|
46 |
{ |
47 |
do_raise_exception_err(exception, 0);
|
48 |
} |
49 |
|
50 |
void 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 |
|
59 |
void do_raise_exception_direct (uint32_t exception)
|
60 |
{ |
61 |
do_restore_state (GETPC ()); |
62 |
do_raise_exception_err (exception, 0);
|
63 |
} |
64 |
|
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 |
|
77 |
/* 64 bits arithmetic for 32 bits hosts */
|
78 |
#if (HOST_LONG_BITS == 32) |
79 |
static inline uint64_t get_HILO (void) |
80 |
{ |
81 |
return ((uint64_t)env->HI << 32) | (uint64_t)env->LO; |
82 |
} |
83 |
|
84 |
static inline void set_HILO (uint64_t HILO) |
85 |
{ |
86 |
env->LO = HILO & 0xFFFFFFFF;
|
87 |
env->HI = HILO >> 32;
|
88 |
} |
89 |
|
90 |
void do_mult (void) |
91 |
{ |
92 |
set_HILO((int64_t)(int32_t)T0 * (int64_t)(int32_t)T1); |
93 |
} |
94 |
|
95 |
void do_multu (void) |
96 |
{ |
97 |
set_HILO((uint64_t)T0 * (uint64_t)T1); |
98 |
} |
99 |
|
100 |
void do_madd (void) |
101 |
{ |
102 |
int64_t tmp; |
103 |
|
104 |
tmp = ((int64_t)(int32_t)T0 * (int64_t)(int32_t)T1); |
105 |
set_HILO((int64_t)get_HILO() + tmp); |
106 |
} |
107 |
|
108 |
void do_maddu (void) |
109 |
{ |
110 |
uint64_t tmp; |
111 |
|
112 |
tmp = ((uint64_t)T0 * (uint64_t)T1); |
113 |
set_HILO(get_HILO() + tmp); |
114 |
} |
115 |
|
116 |
void do_msub (void) |
117 |
{ |
118 |
int64_t tmp; |
119 |
|
120 |
tmp = ((int64_t)(int32_t)T0 * (int64_t)(int32_t)T1); |
121 |
set_HILO((int64_t)get_HILO() - tmp); |
122 |
} |
123 |
|
124 |
void do_msubu (void) |
125 |
{ |
126 |
uint64_t tmp; |
127 |
|
128 |
tmp = ((uint64_t)T0 * (uint64_t)T1); |
129 |
set_HILO(get_HILO() - tmp); |
130 |
} |
131 |
#endif
|
132 |
|
133 |
#if defined(CONFIG_USER_ONLY)
|
134 |
void do_mfc0_random (void) |
135 |
{ |
136 |
cpu_abort(env, "mfc0 random\n");
|
137 |
} |
138 |
|
139 |
void do_mfc0_count (void) |
140 |
{ |
141 |
cpu_abort(env, "mfc0 count\n");
|
142 |
} |
143 |
|
144 |
void cpu_mips_store_count(CPUState *env, uint32_t value)
|
145 |
{ |
146 |
cpu_abort(env, "mtc0 count\n");
|
147 |
} |
148 |
|
149 |
void cpu_mips_store_compare(CPUState *env, uint32_t value)
|
150 |
{ |
151 |
cpu_abort(env, "mtc0 compare\n");
|
152 |
} |
153 |
|
154 |
void do_mtc0_status_debug(uint32_t old, uint32_t val)
|
155 |
{ |
156 |
cpu_abort(env, "mtc0 status\n");
|
157 |
} |
158 |
|
159 |
void do_mtc0_status_irqraise_debug(void) |
160 |
{ |
161 |
cpu_abort(env, "mtc0 status\n");
|
162 |
} |
163 |
|
164 |
void do_tlbwi (void) |
165 |
{ |
166 |
cpu_abort(env, "tlbwi\n");
|
167 |
} |
168 |
|
169 |
void do_tlbwr (void) |
170 |
{ |
171 |
cpu_abort(env, "tlbwr\n");
|
172 |
} |
173 |
|
174 |
void do_tlbp (void) |
175 |
{ |
176 |
cpu_abort(env, "tlbp\n");
|
177 |
} |
178 |
|
179 |
void do_tlbr (void) |
180 |
{ |
181 |
cpu_abort(env, "tlbr\n");
|
182 |
} |
183 |
|
184 |
void cpu_mips_tlb_flush (CPUState *env, int flush_global) |
185 |
{ |
186 |
cpu_abort(env, "mips_tlb_flush\n");
|
187 |
} |
188 |
|
189 |
#else
|
190 |
|
191 |
/* CP0 helpers */
|
192 |
void do_mfc0_random (void) |
193 |
{ |
194 |
T0 = cpu_mips_get_random(env); |
195 |
} |
196 |
|
197 |
void do_mfc0_count (void) |
198 |
{ |
199 |
T0 = cpu_mips_get_count(env); |
200 |
} |
201 |
|
202 |
void do_mtc0_status_debug(uint32_t old, uint32_t val)
|
203 |
{ |
204 |
const uint32_t mask = 0x0000FF00; |
205 |
fprintf(logfile, "Status %08x => %08x Cause %08x (%08x %08x %08x)\n",
|
206 |
old, val, env->CP0_Cause, old & mask, val & mask, |
207 |
env->CP0_Cause & mask); |
208 |
} |
209 |
|
210 |
void do_mtc0_status_irqraise_debug(void) |
211 |
{ |
212 |
fprintf(logfile, "Raise pending IRQs\n");
|
213 |
} |
214 |
|
215 |
#ifdef MIPS_USES_FPU
|
216 |
#include "softfloat.h" |
217 |
|
218 |
void fpu_handle_exception(void) |
219 |
{ |
220 |
#ifdef CONFIG_SOFTFLOAT
|
221 |
int flags = get_float_exception_flags(&env->fp_status);
|
222 |
unsigned int cpuflags = 0, enable, cause = 0; |
223 |
|
224 |
enable = GET_FP_ENABLE(env->fcr31); |
225 |
|
226 |
/* determine current flags */
|
227 |
if (flags & float_flag_invalid) {
|
228 |
cpuflags |= FP_INVALID; |
229 |
cause |= FP_INVALID & enable; |
230 |
} |
231 |
if (flags & float_flag_divbyzero) {
|
232 |
cpuflags |= FP_DIV0; |
233 |
cause |= FP_DIV0 & enable; |
234 |
} |
235 |
if (flags & float_flag_overflow) {
|
236 |
cpuflags |= FP_OVERFLOW; |
237 |
cause |= FP_OVERFLOW & enable; |
238 |
} |
239 |
if (flags & float_flag_underflow) {
|
240 |
cpuflags |= FP_UNDERFLOW; |
241 |
cause |= FP_UNDERFLOW & enable; |
242 |
} |
243 |
if (flags & float_flag_inexact) {
|
244 |
cpuflags |= FP_INEXACT; |
245 |
cause |= FP_INEXACT & enable; |
246 |
} |
247 |
SET_FP_FLAGS(env->fcr31, cpuflags); |
248 |
SET_FP_CAUSE(env->fcr31, cause); |
249 |
#else
|
250 |
SET_FP_FLAGS(env->fcr31, 0);
|
251 |
SET_FP_CAUSE(env->fcr31, 0);
|
252 |
#endif
|
253 |
} |
254 |
#endif /* MIPS_USES_FPU */ |
255 |
|
256 |
/* TLB management */
|
257 |
#if defined(MIPS_USES_R4K_TLB)
|
258 |
void cpu_mips_tlb_flush (CPUState *env, int flush_global) |
259 |
{ |
260 |
/* Flush qemu's TLB and discard all shadowed entries. */
|
261 |
tlb_flush (env, flush_global); |
262 |
env->tlb_in_use = MIPS_TLB_NB; |
263 |
} |
264 |
|
265 |
static void invalidate_tlb (int idx, int use_extra) |
266 |
{ |
267 |
tlb_t *tlb; |
268 |
target_ulong addr; |
269 |
uint8_t ASID; |
270 |
|
271 |
ASID = env->CP0_EntryHi & 0xFF;
|
272 |
|
273 |
tlb = &env->tlb[idx]; |
274 |
/* The qemu TLB is flushed then the ASID changes, so no need to
|
275 |
flush these entries again. */
|
276 |
if (tlb->G == 0 && tlb->ASID != ASID) { |
277 |
return;
|
278 |
} |
279 |
|
280 |
if (use_extra && env->tlb_in_use < MIPS_TLB_MAX) {
|
281 |
/* For tlbwr, we can shadow the discarded entry into
|
282 |
a new (fake) TLB entry, as long as the guest can not
|
283 |
tell that it's there. */
|
284 |
env->tlb[env->tlb_in_use] = *tlb; |
285 |
env->tlb_in_use++; |
286 |
return;
|
287 |
} |
288 |
|
289 |
if (tlb->V0) {
|
290 |
tb_invalidate_page_range(tlb->PFN[0], tlb->end - tlb->VPN);
|
291 |
addr = tlb->VPN; |
292 |
while (addr < tlb->end) {
|
293 |
tlb_flush_page (env, addr); |
294 |
addr += TARGET_PAGE_SIZE; |
295 |
} |
296 |
} |
297 |
if (tlb->V1) {
|
298 |
tb_invalidate_page_range(tlb->PFN[1], tlb->end2 - tlb->end);
|
299 |
addr = tlb->end; |
300 |
while (addr < tlb->end2) {
|
301 |
tlb_flush_page (env, addr); |
302 |
addr += TARGET_PAGE_SIZE; |
303 |
} |
304 |
} |
305 |
} |
306 |
|
307 |
static void mips_tlb_flush_extra (CPUState *env, int first) |
308 |
{ |
309 |
/* Discard entries from env->tlb[first] onwards. */
|
310 |
while (env->tlb_in_use > first) {
|
311 |
invalidate_tlb(--env->tlb_in_use, 0);
|
312 |
} |
313 |
} |
314 |
|
315 |
static void fill_tlb (int idx) |
316 |
{ |
317 |
tlb_t *tlb; |
318 |
int size;
|
319 |
|
320 |
/* XXX: detect conflicting TLBs and raise a MCHECK exception when needed */
|
321 |
tlb = &env->tlb[idx]; |
322 |
tlb->VPN = env->CP0_EntryHi & 0xFFFFE000;
|
323 |
tlb->ASID = env->CP0_EntryHi & 0xFF;
|
324 |
size = env->CP0_PageMask >> 13;
|
325 |
size = 4 * (size + 1); |
326 |
tlb->end = tlb->VPN + (1 << (8 + size)); |
327 |
tlb->end2 = tlb->end + (1 << (8 + size)); |
328 |
tlb->G = env->CP0_EntryLo0 & env->CP0_EntryLo1 & 1;
|
329 |
tlb->V0 = (env->CP0_EntryLo0 & 2) != 0; |
330 |
tlb->D0 = (env->CP0_EntryLo0 & 4) != 0; |
331 |
tlb->C0 = (env->CP0_EntryLo0 >> 3) & 0x7; |
332 |
tlb->PFN[0] = (env->CP0_EntryLo0 >> 6) << 12; |
333 |
tlb->V1 = (env->CP0_EntryLo1 & 2) != 0; |
334 |
tlb->D1 = (env->CP0_EntryLo1 & 4) != 0; |
335 |
tlb->C1 = (env->CP0_EntryLo1 >> 3) & 0x7; |
336 |
tlb->PFN[1] = (env->CP0_EntryLo1 >> 6) << 12; |
337 |
} |
338 |
|
339 |
void do_tlbwi (void) |
340 |
{ |
341 |
/* Discard cached TLB entries. We could avoid doing this if the
|
342 |
tlbwi is just upgrading access permissions on the current entry;
|
343 |
that might be a further win. */
|
344 |
mips_tlb_flush_extra (env, MIPS_TLB_NB); |
345 |
|
346 |
/* Wildly undefined effects for CP0_index containing a too high value and
|
347 |
MIPS_TLB_NB not being a power of two. But so does real silicon. */
|
348 |
invalidate_tlb(env->CP0_index & (MIPS_TLB_NB - 1), 0); |
349 |
fill_tlb(env->CP0_index & (MIPS_TLB_NB - 1));
|
350 |
} |
351 |
|
352 |
void do_tlbwr (void) |
353 |
{ |
354 |
int r = cpu_mips_get_random(env);
|
355 |
|
356 |
invalidate_tlb(r, 1);
|
357 |
fill_tlb(r); |
358 |
} |
359 |
|
360 |
void do_tlbp (void) |
361 |
{ |
362 |
tlb_t *tlb; |
363 |
target_ulong tag; |
364 |
uint8_t ASID; |
365 |
int i;
|
366 |
|
367 |
tag = env->CP0_EntryHi & 0xFFFFE000;
|
368 |
ASID = env->CP0_EntryHi & 0xFF;
|
369 |
for (i = 0; i < MIPS_TLB_NB; i++) { |
370 |
tlb = &env->tlb[i]; |
371 |
/* Check ASID, virtual page number & size */
|
372 |
if ((tlb->G == 1 || tlb->ASID == ASID) && tlb->VPN == tag) { |
373 |
/* TLB match */
|
374 |
env->CP0_index = i; |
375 |
break;
|
376 |
} |
377 |
} |
378 |
if (i == MIPS_TLB_NB) {
|
379 |
/* No match. Discard any shadow entries, if any of them match. */
|
380 |
for (i = MIPS_TLB_NB; i < env->tlb_in_use; i++) {
|
381 |
tlb = &env->tlb[i]; |
382 |
|
383 |
/* Check ASID, virtual page number & size */
|
384 |
if ((tlb->G == 1 || tlb->ASID == ASID) && tlb->VPN == tag) { |
385 |
mips_tlb_flush_extra (env, i); |
386 |
break;
|
387 |
} |
388 |
} |
389 |
|
390 |
env->CP0_index |= 0x80000000;
|
391 |
} |
392 |
} |
393 |
|
394 |
void do_tlbr (void) |
395 |
{ |
396 |
tlb_t *tlb; |
397 |
uint8_t ASID; |
398 |
int size;
|
399 |
|
400 |
ASID = env->CP0_EntryHi & 0xFF;
|
401 |
tlb = &env->tlb[env->CP0_index & (MIPS_TLB_NB - 1)];
|
402 |
|
403 |
/* If this will change the current ASID, flush qemu's TLB. */
|
404 |
if (ASID != tlb->ASID)
|
405 |
cpu_mips_tlb_flush (env, 1);
|
406 |
|
407 |
mips_tlb_flush_extra(env, MIPS_TLB_NB); |
408 |
|
409 |
env->CP0_EntryHi = tlb->VPN | tlb->ASID; |
410 |
size = (tlb->end - tlb->VPN) >> 12;
|
411 |
env->CP0_PageMask = (size - 1) << 13; |
412 |
env->CP0_EntryLo0 = tlb->G | (tlb->V0 << 1) | (tlb->D0 << 2) |
413 |
| (tlb->C0 << 3) | (tlb->PFN[0] >> 6); |
414 |
env->CP0_EntryLo1 = tlb->G | (tlb->V1 << 1) | (tlb->D1 << 2) |
415 |
| (tlb->C1 << 3) | (tlb->PFN[1] >> 6); |
416 |
} |
417 |
#endif
|
418 |
|
419 |
#endif /* !CONFIG_USER_ONLY */ |
420 |
|
421 |
void op_dump_ldst (const unsigned char *func) |
422 |
{ |
423 |
if (loglevel)
|
424 |
fprintf(logfile, "%s => %08x %08x\n", __func__, T0, T1);
|
425 |
} |
426 |
|
427 |
void dump_sc (void) |
428 |
{ |
429 |
if (loglevel) {
|
430 |
fprintf(logfile, "%s %08x at %08x (%08x)\n", __func__,
|
431 |
T1, T0, env->CP0_LLAddr); |
432 |
} |
433 |
} |
434 |
|
435 |
void debug_eret (void) |
436 |
{ |
437 |
if (loglevel) {
|
438 |
fprintf(logfile, "ERET: pc %08x EPC %08x ErrorEPC %08x (%d)\n",
|
439 |
env->PC, env->CP0_EPC, env->CP0_ErrorEPC, |
440 |
env->hflags & MIPS_HFLAG_ERL ? 1 : 0); |
441 |
} |
442 |
} |
443 |
|
444 |
void do_pmon (int function) |
445 |
{ |
446 |
function /= 2;
|
447 |
switch (function) {
|
448 |
case 2: /* TODO: char inbyte(int waitflag); */ |
449 |
if (env->gpr[4] == 0) |
450 |
env->gpr[2] = -1; |
451 |
/* Fall through */
|
452 |
case 11: /* TODO: char inbyte (void); */ |
453 |
env->gpr[2] = -1; |
454 |
break;
|
455 |
case 3: |
456 |
case 12: |
457 |
printf("%c", env->gpr[4] & 0xFF); |
458 |
break;
|
459 |
case 17: |
460 |
break;
|
461 |
case 158: |
462 |
{ |
463 |
unsigned char *fmt = (void *)env->gpr[4]; |
464 |
printf("%s", fmt);
|
465 |
} |
466 |
break;
|
467 |
} |
468 |
} |
469 |
|
470 |
#if !defined(CONFIG_USER_ONLY)
|
471 |
|
472 |
static void do_unaligned_access (target_ulong addr, int is_write, int is_user, void *retaddr); |
473 |
|
474 |
#define MMUSUFFIX _mmu
|
475 |
#define ALIGNED_ONLY
|
476 |
|
477 |
#define SHIFT 0 |
478 |
#include "softmmu_template.h" |
479 |
|
480 |
#define SHIFT 1 |
481 |
#include "softmmu_template.h" |
482 |
|
483 |
#define SHIFT 2 |
484 |
#include "softmmu_template.h" |
485 |
|
486 |
#define SHIFT 3 |
487 |
#include "softmmu_template.h" |
488 |
|
489 |
static void do_unaligned_access (target_ulong addr, int is_write, int is_user, void *retaddr) |
490 |
{ |
491 |
env->CP0_BadVAddr = addr; |
492 |
do_restore_state (retaddr); |
493 |
do_raise_exception ((is_write == 1) ? EXCP_AdES : EXCP_AdEL);
|
494 |
} |
495 |
|
496 |
void tlb_fill (target_ulong addr, int is_write, int is_user, void *retaddr) |
497 |
{ |
498 |
TranslationBlock *tb; |
499 |
CPUState *saved_env; |
500 |
unsigned long pc; |
501 |
int ret;
|
502 |
|
503 |
/* XXX: hack to restore env in all cases, even if not called from
|
504 |
generated code */
|
505 |
saved_env = env; |
506 |
env = cpu_single_env; |
507 |
ret = cpu_mips_handle_mmu_fault(env, addr, is_write, is_user, 1);
|
508 |
if (ret) {
|
509 |
if (retaddr) {
|
510 |
/* now we have a real cpu fault */
|
511 |
pc = (unsigned long)retaddr; |
512 |
tb = tb_find_pc(pc); |
513 |
if (tb) {
|
514 |
/* the PC is inside the translated code. It means that we have
|
515 |
a virtual CPU fault */
|
516 |
cpu_restore_state(tb, env, pc, NULL);
|
517 |
} |
518 |
} |
519 |
do_raise_exception_err(env->exception_index, env->error_code); |
520 |
} |
521 |
env = saved_env; |
522 |
} |
523 |
|
524 |
#endif
|