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/>.
24 #include "host-utils.h"
26 //#define CRIS_OP_HELPER_DEBUG
29 #ifdef CRIS_OP_HELPER_DEBUG
31 #define D_LOG(...) qemu_log(__VA__ARGS__)
34 #define D_LOG(...) do { } while (0)
37 #if !defined(CONFIG_USER_ONLY)
38 #include "exec/softmmu_exec.h"
40 #define MMUSUFFIX _mmu
43 #include "exec/softmmu_template.h"
46 #include "exec/softmmu_template.h"
49 #include "exec/softmmu_template.h"
52 #include "exec/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,
62 D_LOG("%s pc=%x tpc=%x ra=%p\n", __func__,
63 env->pc, env->debug1, (void *)retaddr);
64 ret = cpu_cris_handle_mmu_fault(env, addr, is_write, mmu_idx);
67 /* now we have a real cpu fault */
68 if (cpu_restore_state(env, retaddr)) {
69 /* Evaluate flags after retranslation. */
70 helper_top_evaluate_flags(env);
79 void helper_raise_exception(CPUCRISState *env, uint32_t index)
81 env->exception_index = index;
85 void helper_tlb_flush_pid(CPUCRISState *env, uint32_t pid)
87 #if !defined(CONFIG_USER_ONLY)
89 if (pid != (env->pregs[PR_PID] & 0xff))
90 cris_mmu_flush_pid(env, env->pregs[PR_PID]);
94 void helper_spc_write(CPUCRISState *env, uint32_t new_spc)
96 #if !defined(CONFIG_USER_ONLY)
97 tlb_flush_page(env, env->pregs[PR_SPC]);
98 tlb_flush_page(env, new_spc);
102 void helper_dump(uint32_t a0, uint32_t a1, uint32_t a2)
104 qemu_log("%s: a0=%x a1=%x\n", __func__, a0, a1);
107 /* Used by the tlb decoder. */
108 #define EXTRACT_FIELD(src, start, end) \
109 (((src) >> start) & ((1 << (end - start + 1)) - 1))
111 void helper_movl_sreg_reg(CPUCRISState *env, uint32_t sreg, uint32_t reg)
114 srs = env->pregs[PR_SRS];
116 env->sregs[srs][sreg] = env->regs[reg];
118 #if !defined(CONFIG_USER_ONLY)
119 if (srs == 1 || srs == 2) {
121 /* Writes to tlb-hi write to mm_cause as a side
123 env->sregs[SFR_RW_MM_TLB_HI] = env->regs[reg];
124 env->sregs[SFR_R_MM_CAUSE] = env->regs[reg];
126 else if (sreg == 5) {
133 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
138 /* We've just made a write to tlb_lo. */
139 lo = env->sregs[SFR_RW_MM_TLB_LO];
140 /* Writes are done via r_mm_cause. */
141 hi = env->sregs[SFR_R_MM_CAUSE];
143 vaddr = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].hi,
145 vaddr <<= TARGET_PAGE_BITS;
146 tlb_v = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].lo,
148 env->tlbsets[srs - 1][set][idx].lo = lo;
149 env->tlbsets[srs - 1][set][idx].hi = hi;
151 D_LOG("tlb flush vaddr=%x v=%d pc=%x\n",
152 vaddr, tlb_v, env->pc);
154 tlb_flush_page(env, vaddr);
161 void helper_movl_reg_sreg(CPUCRISState *env, uint32_t reg, uint32_t sreg)
164 env->pregs[PR_SRS] &= 3;
165 srs = env->pregs[PR_SRS];
167 #if !defined(CONFIG_USER_ONLY)
168 if (srs == 1 || srs == 2)
174 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
179 /* Update the mirror regs. */
180 hi = env->tlbsets[srs - 1][set][idx].hi;
181 lo = env->tlbsets[srs - 1][set][idx].lo;
182 env->sregs[SFR_RW_MM_TLB_HI] = hi;
183 env->sregs[SFR_RW_MM_TLB_LO] = lo;
186 env->regs[reg] = env->sregs[srs][sreg];
189 static void cris_ccs_rshift(CPUCRISState *env)
193 /* Apply the ccs shift. */
194 ccs = env->pregs[PR_CCS];
195 ccs = (ccs & 0xc0000000) | ((ccs & 0x0fffffff) >> 10);
198 /* Enter user mode. */
199 env->ksp = env->regs[R_SP];
200 env->regs[R_SP] = env->pregs[PR_USP];
203 env->pregs[PR_CCS] = ccs;
206 void helper_rfe(CPUCRISState *env)
208 int rflag = env->pregs[PR_CCS] & R_FLAG;
210 D_LOG("rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
211 env->pregs[PR_ERP], env->pregs[PR_PID],
215 cris_ccs_rshift(env);
217 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
219 env->pregs[PR_CCS] |= P_FLAG;
222 void helper_rfn(CPUCRISState *env)
224 int rflag = env->pregs[PR_CCS] & R_FLAG;
226 D_LOG("rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
227 env->pregs[PR_ERP], env->pregs[PR_PID],
231 cris_ccs_rshift(env);
233 /* Set the P_FLAG only if the R_FLAG is not set. */
235 env->pregs[PR_CCS] |= P_FLAG;
237 /* Always set the M flag. */
238 env->pregs[PR_CCS] |= M_FLAG_V32;
241 uint32_t helper_lz(uint32_t t0)
246 uint32_t helper_btst(CPUCRISState *env, uint32_t t0, uint32_t t1, uint32_t ccs)
248 /* FIXME: clean this up. */
251 The N flag is set according to the selected bit in the dest reg.
252 The Z flag is set if the selected bit and all bits to the right are
254 The X flag is cleared.
255 Other flags are left untouched.
256 The destination reg is not affected.*/
257 unsigned int fz, sbit, bset, mask, masked_t0;
260 bset = !!(t0 & (1 << sbit));
261 mask = sbit == 31 ? -1 : (1 << (sbit + 1)) - 1;
262 masked_t0 = t0 & mask;
263 fz = !(masked_t0 | bset);
265 /* Clear the X, N and Z flags. */
266 ccs = ccs & ~(X_FLAG | N_FLAG | Z_FLAG);
267 if (env->pregs[PR_VR] < 32)
268 ccs &= ~(V_FLAG | C_FLAG);
269 /* Set the N and Z flags accordingly. */
270 ccs |= (bset << 3) | (fz << 2);
274 static inline uint32_t evaluate_flags_writeback(CPUCRISState *env,
275 uint32_t flags, uint32_t ccs)
277 unsigned int x, z, mask;
279 /* Extended arithmetics, leave the z flag alone. */
281 mask = env->cc_mask | X_FLAG;
288 /* all insn clear the x-flag except setf or clrf. */
294 uint32_t helper_evaluate_flags_muls(CPUCRISState *env,
295 uint32_t ccs, uint32_t res, uint32_t mof)
301 dneg = ((int32_t)res) < 0;
310 if ((dneg && mof != -1)
311 || (!dneg && mof != 0))
313 return evaluate_flags_writeback(env, flags, ccs);
316 uint32_t helper_evaluate_flags_mulu(CPUCRISState *env,
317 uint32_t ccs, uint32_t res, uint32_t mof)
332 return evaluate_flags_writeback(env, flags, ccs);
335 uint32_t helper_evaluate_flags_mcp(CPUCRISState *env, uint32_t ccs,
336 uint32_t src, uint32_t dst, uint32_t res)
340 src = src & 0x80000000;
341 dst = dst & 0x80000000;
343 if ((res & 0x80000000L) != 0L)
361 return evaluate_flags_writeback(env, flags, ccs);
364 uint32_t helper_evaluate_flags_alu_4(CPUCRISState *env, uint32_t ccs,
365 uint32_t src, uint32_t dst, uint32_t res)
369 src = src & 0x80000000;
370 dst = dst & 0x80000000;
372 if ((res & 0x80000000L) != 0L)
390 return evaluate_flags_writeback(env, flags, ccs);
393 uint32_t helper_evaluate_flags_sub_4(CPUCRISState *env, uint32_t ccs,
394 uint32_t src, uint32_t dst, uint32_t res)
398 src = (~src) & 0x80000000;
399 dst = dst & 0x80000000;
401 if ((res & 0x80000000L) != 0L)
420 return evaluate_flags_writeback(env, flags, ccs);
423 uint32_t helper_evaluate_flags_move_4(CPUCRISState *env,
424 uint32_t ccs, uint32_t res)
428 if ((int32_t)res < 0)
433 return evaluate_flags_writeback(env, flags, ccs);
435 uint32_t helper_evaluate_flags_move_2(CPUCRISState *env,
436 uint32_t ccs, uint32_t res)
440 if ((int16_t)res < 0L)
445 return evaluate_flags_writeback(env, flags, ccs);
448 /* TODO: This is expensive. We could split things up and only evaluate part of
449 CCR on a need to know basis. For now, we simply re-evaluate everything. */
450 void helper_evaluate_flags(CPUCRISState *env)
452 uint32_t src, dst, res;
457 res = env->cc_result;
459 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
462 /* Now, evaluate the flags. This stuff is based on
463 Per Zander's CRISv10 simulator. */
464 switch (env->cc_size)
467 if ((res & 0x80L) != 0L)
470 if (((src & 0x80L) == 0L)
471 && ((dst & 0x80L) == 0L))
475 else if (((src & 0x80L) != 0L)
476 && ((dst & 0x80L) != 0L))
483 if ((res & 0xFFL) == 0L)
487 if (((src & 0x80L) != 0L)
488 && ((dst & 0x80L) != 0L))
492 if ((dst & 0x80L) != 0L
493 || (src & 0x80L) != 0L)
500 if ((res & 0x8000L) != 0L)
503 if (((src & 0x8000L) == 0L)
504 && ((dst & 0x8000L) == 0L))
508 else if (((src & 0x8000L) != 0L)
509 && ((dst & 0x8000L) != 0L))
516 if ((res & 0xFFFFL) == 0L)
520 if (((src & 0x8000L) != 0L)
521 && ((dst & 0x8000L) != 0L))
525 if ((dst & 0x8000L) != 0L
526 || (src & 0x8000L) != 0L)
533 if ((res & 0x80000000L) != 0L)
536 if (((src & 0x80000000L) == 0L)
537 && ((dst & 0x80000000L) == 0L))
541 else if (((src & 0x80000000L) != 0L) &&
542 ((dst & 0x80000000L) != 0L))
551 if (((src & 0x80000000L) != 0L)
552 && ((dst & 0x80000000L) != 0L))
554 if ((dst & 0x80000000L) != 0L
555 || (src & 0x80000000L) != 0L)
563 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
566 env->pregs[PR_CCS] = evaluate_flags_writeback(env, flags,
570 void helper_top_evaluate_flags(CPUCRISState *env)
575 env->pregs[PR_CCS] = helper_evaluate_flags_mcp(env,
576 env->pregs[PR_CCS], env->cc_src,
577 env->cc_dest, env->cc_result);
580 env->pregs[PR_CCS] = helper_evaluate_flags_muls(env,
581 env->pregs[PR_CCS], env->cc_result,
585 env->pregs[PR_CCS] = helper_evaluate_flags_mulu(env,
586 env->pregs[PR_CCS], env->cc_result,
596 switch (env->cc_size)
600 helper_evaluate_flags_move_4(env,
606 helper_evaluate_flags_move_2(env,
611 helper_evaluate_flags(env);
620 if (env->cc_size == 4)
622 helper_evaluate_flags_sub_4(env,
624 env->cc_src, env->cc_dest,
627 helper_evaluate_flags(env);
631 switch (env->cc_size)
635 helper_evaluate_flags_alu_4(env,
637 env->cc_src, env->cc_dest,
641 helper_evaluate_flags(env);