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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
26 #include "host-utils.h"
30 #if !defined(CONFIG_USER_ONLY)
32 #define MMUSUFFIX _mmu
35 #include "softmmu_template.h"
38 #include "softmmu_template.h"
41 #include "softmmu_template.h"
44 #include "softmmu_template.h"
46 /* Try to fill the TLB and return an exception if error. If retaddr is
47 NULL, it means that the function was called in C code (i.e. not
48 from generated code or from helper.c) */
49 /* XXX: fix it to restore all registers */
50 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
57 /* XXX: hack to restore env in all cases, even if not called from
62 D(fprintf(logfile, "%s pc=%x tpc=%x ra=%x\n", __func__,
63 env->pc, env->debug1, retaddr));
64 ret = cpu_cris_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
67 /* now we have a real cpu fault */
68 pc = (unsigned long)retaddr;
71 /* the PC is inside the translated code. It means that we have
72 a virtual CPU fault */
73 cpu_restore_state(tb, env, pc, NULL);
75 /* Evaluate flags after retranslation. */
76 helper_top_evaluate_flags();
86 void helper_raise_exception(uint32_t index)
88 env->exception_index = index;
92 void helper_tlb_flush_pid(uint32_t pid)
94 #if !defined(CONFIG_USER_ONLY)
96 if (pid != (env->pregs[PR_PID] & 0xff))
97 cris_mmu_flush_pid(env, env->pregs[PR_PID]);
101 void helper_spc_write(uint32_t new_spc)
103 #if !defined(CONFIG_USER_ONLY)
104 tlb_flush_page(env, env->pregs[PR_SPC]);
105 tlb_flush_page(env, new_spc);
109 void helper_dump(uint32_t a0, uint32_t a1, uint32_t a2)
111 (fprintf(logfile, "%s: a0=%x a1=%x\n", __func__, a0, a1));
114 /* Used by the tlb decoder. */
115 #define EXTRACT_FIELD(src, start, end) \
116 (((src) >> start) & ((1 << (end - start + 1)) - 1))
118 void helper_movl_sreg_reg (uint32_t sreg, uint32_t reg)
121 srs = env->pregs[PR_SRS];
123 env->sregs[srs][sreg] = env->regs[reg];
125 #if !defined(CONFIG_USER_ONLY)
126 if (srs == 1 || srs == 2) {
128 /* Writes to tlb-hi write to mm_cause as a side
130 env->sregs[SFR_RW_MM_TLB_HI] = env->regs[reg];
131 env->sregs[SFR_R_MM_CAUSE] = env->regs[reg];
133 else if (sreg == 5) {
140 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
145 /* We've just made a write to tlb_lo. */
146 lo = env->sregs[SFR_RW_MM_TLB_LO];
147 /* Writes are done via r_mm_cause. */
148 hi = env->sregs[SFR_R_MM_CAUSE];
150 vaddr = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].hi,
152 vaddr <<= TARGET_PAGE_BITS;
153 tlb_v = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].lo,
155 env->tlbsets[srs - 1][set][idx].lo = lo;
156 env->tlbsets[srs - 1][set][idx].hi = hi;
159 "tlb flush vaddr=%x v=%d pc=%x\n",
160 vaddr, tlb_v, env->pc));
161 tlb_flush_page(env, vaddr);
167 void helper_movl_reg_sreg (uint32_t reg, uint32_t sreg)
170 env->pregs[PR_SRS] &= 3;
171 srs = env->pregs[PR_SRS];
173 #if !defined(CONFIG_USER_ONLY)
174 if (srs == 1 || srs == 2)
180 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
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;
192 env->regs[reg] = env->sregs[srs][sreg];
195 static void cris_ccs_rshift(CPUState *env)
199 /* Apply the ccs shift. */
200 ccs = env->pregs[PR_CCS];
201 ccs = (ccs & 0xc0000000) | ((ccs & 0x0fffffff) >> 10);
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(void)
214 int rflag = env->pregs[PR_CCS] & R_FLAG;
216 D(fprintf(logfile, "rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
217 env->pregs[PR_ERP], env->pregs[PR_PID],
221 cris_ccs_rshift(env);
223 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
225 env->pregs[PR_CCS] |= P_FLAG;
228 void helper_rfn(void)
230 int rflag = env->pregs[PR_CCS] & R_FLAG;
232 D(fprintf(logfile, "rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
233 env->pregs[PR_ERP], env->pregs[PR_PID],
237 cris_ccs_rshift(env);
239 /* Set the P_FLAG only if the R_FLAG is not set. */
241 env->pregs[PR_CCS] |= P_FLAG;
243 /* Always set the M flag. */
244 env->pregs[PR_CCS] |= M_FLAG;
247 uint32_t helper_lz(uint32_t t0)
252 uint32_t helper_btst(uint32_t t0, uint32_t t1, uint32_t ccs)
254 /* FIXME: clean this up. */
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
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;
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 /* Set the N and Z flags accordingly. */
274 ccs |= (bset << 3) | (fz << 2);
278 static void evaluate_flags_writeback(uint32_t flags)
280 unsigned int x, z, mask;
282 /* Extended arithmetics, leave the z flag alone. */
284 mask = env->cc_mask | X_FLAG;
291 /* all insn clear the x-flag except setf or clrf. */
292 env->pregs[PR_CCS] &= ~mask;
293 env->pregs[PR_CCS] |= flags;
296 void helper_evaluate_flags_muls(void)
308 res = env->cc_result;
310 dneg = ((int32_t)res) < 0;
312 mof = env->pregs[PR_MOF];
320 if ((dneg && mof != -1)
321 || (!dneg && mof != 0))
323 evaluate_flags_writeback(flags);
326 void helper_evaluate_flags_mulu(void)
337 res = env->cc_result;
339 mof = env->pregs[PR_MOF];
350 evaluate_flags_writeback(flags);
353 void helper_evaluate_flags_mcp(void)
360 src = env->cc_src & 0x80000000;
361 dst = env->cc_dest & 0x80000000;
362 res = env->cc_result;
364 if ((res & 0x80000000L) != 0L)
382 evaluate_flags_writeback(flags);
385 void helper_evaluate_flags_alu_4(void)
392 src = env->cc_src & 0x80000000;
393 dst = env->cc_dest & 0x80000000;
394 res = env->cc_result;
396 if ((res & 0x80000000L) != 0L)
414 evaluate_flags_writeback(flags);
417 void helper_evaluate_flags_sub_4(void)
424 src = (~env->cc_src) & 0x80000000;
425 dst = env->cc_dest & 0x80000000;
426 res = env->cc_result;
428 if ((res & 0x80000000L) != 0L)
447 evaluate_flags_writeback(flags);
450 void helper_evaluate_flags_move_4 (void)
455 res = env->cc_result;
457 if ((int32_t)res < 0)
462 evaluate_flags_writeback(flags);
464 void helper_evaluate_flags_move_2 (void)
471 res = env->cc_result;
473 if ((int16_t)res < 0L)
478 evaluate_flags_writeback(flags);
481 /* TODO: This is expensive. We could split things up and only evaluate part of
482 CCR on a need to know basis. For now, we simply re-evaluate everything. */
483 void helper_evaluate_flags (void)
492 res = env->cc_result;
494 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
497 /* Now, evaluate the flags. This stuff is based on
498 Per Zander's CRISv10 simulator. */
499 switch (env->cc_size)
502 if ((res & 0x80L) != 0L)
505 if (((src & 0x80L) == 0L)
506 && ((dst & 0x80L) == 0L))
510 else if (((src & 0x80L) != 0L)
511 && ((dst & 0x80L) != 0L))
518 if ((res & 0xFFL) == 0L)
522 if (((src & 0x80L) != 0L)
523 && ((dst & 0x80L) != 0L))
527 if ((dst & 0x80L) != 0L
528 || (src & 0x80L) != 0L)
535 if ((res & 0x8000L) != 0L)
538 if (((src & 0x8000L) == 0L)
539 && ((dst & 0x8000L) == 0L))
543 else if (((src & 0x8000L) != 0L)
544 && ((dst & 0x8000L) != 0L))
551 if ((res & 0xFFFFL) == 0L)
555 if (((src & 0x8000L) != 0L)
556 && ((dst & 0x8000L) != 0L))
560 if ((dst & 0x8000L) != 0L
561 || (src & 0x8000L) != 0L)
568 if ((res & 0x80000000L) != 0L)
571 if (((src & 0x80000000L) == 0L)
572 && ((dst & 0x80000000L) == 0L))
576 else if (((src & 0x80000000L) != 0L) &&
577 ((dst & 0x80000000L) != 0L))
586 if (((src & 0x80000000L) != 0L)
587 && ((dst & 0x80000000L) != 0L))
589 if ((dst & 0x80000000L) != 0L
590 || (src & 0x80000000L) != 0L)
598 if (env->cc_op == CC_OP_SUB
599 || env->cc_op == CC_OP_CMP) {
602 evaluate_flags_writeback(flags);
605 void helper_top_evaluate_flags(void)
610 helper_evaluate_flags_mcp();
613 helper_evaluate_flags_muls();
616 helper_evaluate_flags_mulu();
625 switch (env->cc_size)
628 helper_evaluate_flags_move_4();
631 helper_evaluate_flags_move_2();
634 helper_evaluate_flags();
643 if (env->cc_size == 4)
644 helper_evaluate_flags_sub_4();
646 helper_evaluate_flags();
650 switch (env->cc_size)
653 helper_evaluate_flags_alu_4();
656 helper_evaluate_flags();