PPC: 440: Emulate DCBR0
[qemu/agraf.git] / target-cris / op_helper.c
bloba7468d41c675a5a7f8ffe3e0f2e60ba8eec43c39
1 /*
2 * CRIS helper routines
4 * Copyright (c) 2007 AXIS Communications
5 * Written by Edgar E. Iglesias
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "cpu.h"
22 #include "mmu.h"
23 #include "helper.h"
24 #include "host-utils.h"
26 //#define CRIS_OP_HELPER_DEBUG
29 #ifdef CRIS_OP_HELPER_DEBUG
30 #define D(x) x
31 #define D_LOG(...) qemu_log(__VA__ARGS__)
32 #else
33 #define D(x)
34 #define D_LOG(...) do { } while (0)
35 #endif
37 #if !defined(CONFIG_USER_ONLY)
38 #include "softmmu_exec.h"
40 #define MMUSUFFIX _mmu
42 #define SHIFT 0
43 #include "softmmu_template.h"
45 #define SHIFT 1
46 #include "softmmu_template.h"
48 #define SHIFT 2
49 #include "softmmu_template.h"
51 #define SHIFT 3
52 #include "softmmu_template.h"
54 /* Try to fill the TLB and return an exception if error. If retaddr is
55 NULL, it means that the function was called in C code (i.e. not
56 from generated code or from helper.c) */
57 void tlb_fill(CPUCRISState *env, target_ulong addr, int is_write, int mmu_idx,
58 uintptr_t retaddr)
60 TranslationBlock *tb;
61 int ret;
63 D_LOG("%s pc=%x tpc=%x ra=%p\n", __func__,
64 env->pc, env->debug1, (void *)retaddr);
65 ret = cpu_cris_handle_mmu_fault(env, addr, is_write, mmu_idx);
66 if (unlikely(ret)) {
67 if (retaddr) {
68 /* now we have a real cpu fault */
69 tb = tb_find_pc(retaddr);
70 if (tb) {
71 /* the PC is inside the translated code. It means that we have
72 a virtual CPU fault */
73 cpu_restore_state(tb, env, retaddr);
75 /* Evaluate flags after retranslation. */
76 helper_top_evaluate_flags(env);
79 cpu_loop_exit(env);
83 #endif
85 void helper_raise_exception(CPUCRISState *env, uint32_t index)
87 env->exception_index = index;
88 cpu_loop_exit(env);
91 void helper_tlb_flush_pid(CPUCRISState *env, uint32_t pid)
93 #if !defined(CONFIG_USER_ONLY)
94 pid &= 0xff;
95 if (pid != (env->pregs[PR_PID] & 0xff))
96 cris_mmu_flush_pid(env, env->pregs[PR_PID]);
97 #endif
100 void helper_spc_write(CPUCRISState *env, uint32_t new_spc)
102 #if !defined(CONFIG_USER_ONLY)
103 tlb_flush_page(env, env->pregs[PR_SPC]);
104 tlb_flush_page(env, new_spc);
105 #endif
108 void helper_dump(uint32_t a0, uint32_t a1, uint32_t a2)
110 qemu_log("%s: a0=%x a1=%x\n", __func__, a0, a1);
113 /* Used by the tlb decoder. */
114 #define EXTRACT_FIELD(src, start, end) \
115 (((src) >> start) & ((1 << (end - start + 1)) - 1))
117 void helper_movl_sreg_reg(CPUCRISState *env, uint32_t sreg, uint32_t reg)
119 uint32_t srs;
120 srs = env->pregs[PR_SRS];
121 srs &= 3;
122 env->sregs[srs][sreg] = env->regs[reg];
124 #if !defined(CONFIG_USER_ONLY)
125 if (srs == 1 || srs == 2) {
126 if (sreg == 6) {
127 /* Writes to tlb-hi write to mm_cause as a side
128 effect. */
129 env->sregs[SFR_RW_MM_TLB_HI] = env->regs[reg];
130 env->sregs[SFR_R_MM_CAUSE] = env->regs[reg];
132 else if (sreg == 5) {
133 uint32_t set;
134 uint32_t idx;
135 uint32_t lo, hi;
136 uint32_t vaddr;
137 int tlb_v;
139 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
140 set >>= 4;
141 set &= 3;
143 idx &= 15;
144 /* We've just made a write to tlb_lo. */
145 lo = env->sregs[SFR_RW_MM_TLB_LO];
146 /* Writes are done via r_mm_cause. */
147 hi = env->sregs[SFR_R_MM_CAUSE];
149 vaddr = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].hi,
150 13, 31);
151 vaddr <<= TARGET_PAGE_BITS;
152 tlb_v = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].lo,
153 3, 3);
154 env->tlbsets[srs - 1][set][idx].lo = lo;
155 env->tlbsets[srs - 1][set][idx].hi = hi;
157 D_LOG("tlb flush vaddr=%x v=%d pc=%x\n",
158 vaddr, tlb_v, env->pc);
159 if (tlb_v) {
160 tlb_flush_page(env, vaddr);
164 #endif
167 void helper_movl_reg_sreg(CPUCRISState *env, uint32_t reg, uint32_t sreg)
169 uint32_t srs;
170 env->pregs[PR_SRS] &= 3;
171 srs = env->pregs[PR_SRS];
173 #if !defined(CONFIG_USER_ONLY)
174 if (srs == 1 || srs == 2)
176 uint32_t set;
177 uint32_t idx;
178 uint32_t lo, hi;
180 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
181 set >>= 4;
182 set &= 3;
183 idx &= 15;
185 /* Update the mirror regs. */
186 hi = env->tlbsets[srs - 1][set][idx].hi;
187 lo = env->tlbsets[srs - 1][set][idx].lo;
188 env->sregs[SFR_RW_MM_TLB_HI] = hi;
189 env->sregs[SFR_RW_MM_TLB_LO] = lo;
191 #endif
192 env->regs[reg] = env->sregs[srs][sreg];
195 static void cris_ccs_rshift(CPUCRISState *env)
197 uint32_t ccs;
199 /* Apply the ccs shift. */
200 ccs = env->pregs[PR_CCS];
201 ccs = (ccs & 0xc0000000) | ((ccs & 0x0fffffff) >> 10);
202 if (ccs & U_FLAG)
204 /* Enter user mode. */
205 env->ksp = env->regs[R_SP];
206 env->regs[R_SP] = env->pregs[PR_USP];
209 env->pregs[PR_CCS] = ccs;
212 void helper_rfe(CPUCRISState *env)
214 int rflag = env->pregs[PR_CCS] & R_FLAG;
216 D_LOG("rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
217 env->pregs[PR_ERP], env->pregs[PR_PID],
218 env->pregs[PR_CCS],
219 env->btarget);
221 cris_ccs_rshift(env);
223 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
224 if (!rflag)
225 env->pregs[PR_CCS] |= P_FLAG;
228 void helper_rfn(CPUCRISState *env)
230 int rflag = env->pregs[PR_CCS] & R_FLAG;
232 D_LOG("rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
233 env->pregs[PR_ERP], env->pregs[PR_PID],
234 env->pregs[PR_CCS],
235 env->btarget);
237 cris_ccs_rshift(env);
239 /* Set the P_FLAG only if the R_FLAG is not set. */
240 if (!rflag)
241 env->pregs[PR_CCS] |= P_FLAG;
243 /* Always set the M flag. */
244 env->pregs[PR_CCS] |= M_FLAG_V32;
247 uint32_t helper_lz(uint32_t t0)
249 return clz32(t0);
252 uint32_t helper_btst(CPUCRISState *env, uint32_t t0, uint32_t t1, uint32_t ccs)
254 /* FIXME: clean this up. */
256 /* des ref:
257 The N flag is set according to the selected bit in the dest reg.
258 The Z flag is set if the selected bit and all bits to the right are
259 zero.
260 The X flag is cleared.
261 Other flags are left untouched.
262 The destination reg is not affected.*/
263 unsigned int fz, sbit, bset, mask, masked_t0;
265 sbit = t1 & 31;
266 bset = !!(t0 & (1 << sbit));
267 mask = sbit == 31 ? -1 : (1 << (sbit + 1)) - 1;
268 masked_t0 = t0 & mask;
269 fz = !(masked_t0 | bset);
271 /* Clear the X, N and Z flags. */
272 ccs = ccs & ~(X_FLAG | N_FLAG | Z_FLAG);
273 if (env->pregs[PR_VR] < 32)
274 ccs &= ~(V_FLAG | C_FLAG);
275 /* Set the N and Z flags accordingly. */
276 ccs |= (bset << 3) | (fz << 2);
277 return ccs;
280 static inline uint32_t evaluate_flags_writeback(CPUCRISState *env,
281 uint32_t flags, uint32_t ccs)
283 unsigned int x, z, mask;
285 /* Extended arithmetics, leave the z flag alone. */
286 x = env->cc_x;
287 mask = env->cc_mask | X_FLAG;
288 if (x) {
289 z = flags & Z_FLAG;
290 mask = mask & ~z;
292 flags &= mask;
294 /* all insn clear the x-flag except setf or clrf. */
295 ccs &= ~mask;
296 ccs |= flags;
297 return ccs;
300 uint32_t helper_evaluate_flags_muls(CPUCRISState *env,
301 uint32_t ccs, uint32_t res, uint32_t mof)
303 uint32_t flags = 0;
304 int64_t tmp;
305 int dneg;
307 dneg = ((int32_t)res) < 0;
309 tmp = mof;
310 tmp <<= 32;
311 tmp |= res;
312 if (tmp == 0)
313 flags |= Z_FLAG;
314 else if (tmp < 0)
315 flags |= N_FLAG;
316 if ((dneg && mof != -1)
317 || (!dneg && mof != 0))
318 flags |= V_FLAG;
319 return evaluate_flags_writeback(env, flags, ccs);
322 uint32_t helper_evaluate_flags_mulu(CPUCRISState *env,
323 uint32_t ccs, uint32_t res, uint32_t mof)
325 uint32_t flags = 0;
326 uint64_t tmp;
328 tmp = mof;
329 tmp <<= 32;
330 tmp |= res;
331 if (tmp == 0)
332 flags |= Z_FLAG;
333 else if (tmp >> 63)
334 flags |= N_FLAG;
335 if (mof)
336 flags |= V_FLAG;
338 return evaluate_flags_writeback(env, flags, ccs);
341 uint32_t helper_evaluate_flags_mcp(CPUCRISState *env, uint32_t ccs,
342 uint32_t src, uint32_t dst, uint32_t res)
344 uint32_t flags = 0;
346 src = src & 0x80000000;
347 dst = dst & 0x80000000;
349 if ((res & 0x80000000L) != 0L)
351 flags |= N_FLAG;
352 if (!src && !dst)
353 flags |= V_FLAG;
354 else if (src & dst)
355 flags |= R_FLAG;
357 else
359 if (res == 0L)
360 flags |= Z_FLAG;
361 if (src & dst)
362 flags |= V_FLAG;
363 if (dst | src)
364 flags |= R_FLAG;
367 return evaluate_flags_writeback(env, flags, ccs);
370 uint32_t helper_evaluate_flags_alu_4(CPUCRISState *env, uint32_t ccs,
371 uint32_t src, uint32_t dst, uint32_t res)
373 uint32_t flags = 0;
375 src = src & 0x80000000;
376 dst = dst & 0x80000000;
378 if ((res & 0x80000000L) != 0L)
380 flags |= N_FLAG;
381 if (!src && !dst)
382 flags |= V_FLAG;
383 else if (src & dst)
384 flags |= C_FLAG;
386 else
388 if (res == 0L)
389 flags |= Z_FLAG;
390 if (src & dst)
391 flags |= V_FLAG;
392 if (dst | src)
393 flags |= C_FLAG;
396 return evaluate_flags_writeback(env, flags, ccs);
399 uint32_t helper_evaluate_flags_sub_4(CPUCRISState *env, uint32_t ccs,
400 uint32_t src, uint32_t dst, uint32_t res)
402 uint32_t flags = 0;
404 src = (~src) & 0x80000000;
405 dst = dst & 0x80000000;
407 if ((res & 0x80000000L) != 0L)
409 flags |= N_FLAG;
410 if (!src && !dst)
411 flags |= V_FLAG;
412 else if (src & dst)
413 flags |= C_FLAG;
415 else
417 if (res == 0L)
418 flags |= Z_FLAG;
419 if (src & dst)
420 flags |= V_FLAG;
421 if (dst | src)
422 flags |= C_FLAG;
425 flags ^= C_FLAG;
426 return evaluate_flags_writeback(env, flags, ccs);
429 uint32_t helper_evaluate_flags_move_4(CPUCRISState *env,
430 uint32_t ccs, uint32_t res)
432 uint32_t flags = 0;
434 if ((int32_t)res < 0)
435 flags |= N_FLAG;
436 else if (res == 0L)
437 flags |= Z_FLAG;
439 return evaluate_flags_writeback(env, flags, ccs);
441 uint32_t helper_evaluate_flags_move_2(CPUCRISState *env,
442 uint32_t ccs, uint32_t res)
444 uint32_t flags = 0;
446 if ((int16_t)res < 0L)
447 flags |= N_FLAG;
448 else if (res == 0)
449 flags |= Z_FLAG;
451 return evaluate_flags_writeback(env, flags, ccs);
454 /* TODO: This is expensive. We could split things up and only evaluate part of
455 CCR on a need to know basis. For now, we simply re-evaluate everything. */
456 void helper_evaluate_flags(CPUCRISState *env)
458 uint32_t src, dst, res;
459 uint32_t flags = 0;
461 src = env->cc_src;
462 dst = env->cc_dest;
463 res = env->cc_result;
465 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
466 src = ~src;
468 /* Now, evaluate the flags. This stuff is based on
469 Per Zander's CRISv10 simulator. */
470 switch (env->cc_size)
472 case 1:
473 if ((res & 0x80L) != 0L)
475 flags |= N_FLAG;
476 if (((src & 0x80L) == 0L)
477 && ((dst & 0x80L) == 0L))
479 flags |= V_FLAG;
481 else if (((src & 0x80L) != 0L)
482 && ((dst & 0x80L) != 0L))
484 flags |= C_FLAG;
487 else
489 if ((res & 0xFFL) == 0L)
491 flags |= Z_FLAG;
493 if (((src & 0x80L) != 0L)
494 && ((dst & 0x80L) != 0L))
496 flags |= V_FLAG;
498 if ((dst & 0x80L) != 0L
499 || (src & 0x80L) != 0L)
501 flags |= C_FLAG;
504 break;
505 case 2:
506 if ((res & 0x8000L) != 0L)
508 flags |= N_FLAG;
509 if (((src & 0x8000L) == 0L)
510 && ((dst & 0x8000L) == 0L))
512 flags |= V_FLAG;
514 else if (((src & 0x8000L) != 0L)
515 && ((dst & 0x8000L) != 0L))
517 flags |= C_FLAG;
520 else
522 if ((res & 0xFFFFL) == 0L)
524 flags |= Z_FLAG;
526 if (((src & 0x8000L) != 0L)
527 && ((dst & 0x8000L) != 0L))
529 flags |= V_FLAG;
531 if ((dst & 0x8000L) != 0L
532 || (src & 0x8000L) != 0L)
534 flags |= C_FLAG;
537 break;
538 case 4:
539 if ((res & 0x80000000L) != 0L)
541 flags |= N_FLAG;
542 if (((src & 0x80000000L) == 0L)
543 && ((dst & 0x80000000L) == 0L))
545 flags |= V_FLAG;
547 else if (((src & 0x80000000L) != 0L) &&
548 ((dst & 0x80000000L) != 0L))
550 flags |= C_FLAG;
553 else
555 if (res == 0L)
556 flags |= Z_FLAG;
557 if (((src & 0x80000000L) != 0L)
558 && ((dst & 0x80000000L) != 0L))
559 flags |= V_FLAG;
560 if ((dst & 0x80000000L) != 0L
561 || (src & 0x80000000L) != 0L)
562 flags |= C_FLAG;
564 break;
565 default:
566 break;
569 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
570 flags ^= C_FLAG;
572 env->pregs[PR_CCS] = evaluate_flags_writeback(env, flags,
573 env->pregs[PR_CCS]);
576 void helper_top_evaluate_flags(CPUCRISState *env)
578 switch (env->cc_op)
580 case CC_OP_MCP:
581 env->pregs[PR_CCS] = helper_evaluate_flags_mcp(env,
582 env->pregs[PR_CCS], env->cc_src,
583 env->cc_dest, env->cc_result);
584 break;
585 case CC_OP_MULS:
586 env->pregs[PR_CCS] = helper_evaluate_flags_muls(env,
587 env->pregs[PR_CCS], env->cc_result,
588 env->pregs[PR_MOF]);
589 break;
590 case CC_OP_MULU:
591 env->pregs[PR_CCS] = helper_evaluate_flags_mulu(env,
592 env->pregs[PR_CCS], env->cc_result,
593 env->pregs[PR_MOF]);
594 break;
595 case CC_OP_MOVE:
596 case CC_OP_AND:
597 case CC_OP_OR:
598 case CC_OP_XOR:
599 case CC_OP_ASR:
600 case CC_OP_LSR:
601 case CC_OP_LSL:
602 switch (env->cc_size)
604 case 4:
605 env->pregs[PR_CCS] =
606 helper_evaluate_flags_move_4(env,
607 env->pregs[PR_CCS],
608 env->cc_result);
609 break;
610 case 2:
611 env->pregs[PR_CCS] =
612 helper_evaluate_flags_move_2(env,
613 env->pregs[PR_CCS],
614 env->cc_result);
615 break;
616 default:
617 helper_evaluate_flags(env);
618 break;
620 break;
621 case CC_OP_FLAGS:
622 /* live. */
623 break;
624 case CC_OP_SUB:
625 case CC_OP_CMP:
626 if (env->cc_size == 4)
627 env->pregs[PR_CCS] =
628 helper_evaluate_flags_sub_4(env,
629 env->pregs[PR_CCS],
630 env->cc_src, env->cc_dest,
631 env->cc_result);
632 else
633 helper_evaluate_flags(env);
634 break;
635 default:
637 switch (env->cc_size)
639 case 4:
640 env->pregs[PR_CCS] =
641 helper_evaluate_flags_alu_4(env,
642 env->pregs[PR_CCS],
643 env->cc_src, env->cc_dest,
644 env->cc_result);
645 break;
646 default:
647 helper_evaluate_flags(env);
648 break;
651 break;