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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #define MMUSUFFIX _mmu
30 #include "softmmu_template.h"
33 #include "softmmu_template.h"
36 #include "softmmu_template.h"
39 #include "softmmu_template.h"
43 /* Try to fill the TLB and return an exception if error. If retaddr is
44 NULL, it means that the function was called in C code (i.e. not
45 from generated code or from helper.c) */
46 /* XXX: fix it to restore all registers */
47 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
54 /* XXX: hack to restore env in all cases, even if not called from
59 D(fprintf(logfile, "%s pc=%x tpc=%x ra=%x\n", __func__,
60 env->pc, env->debug1, retaddr));
61 ret = cpu_cris_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
62 if (__builtin_expect(ret, 0)) {
64 /* now we have a real cpu fault */
65 pc = (unsigned long)retaddr;
68 /* the PC is inside the translated code. It means that we have
69 a virtual CPU fault */
70 cpu_restore_state(tb, env, pc, NULL);
72 /* Evaluate flags after retranslation. */
73 helper_top_evaluate_flags();
81 void helper_raise_exception(uint32_t index)
83 env->exception_index = index;
87 void helper_tlb_flush_pid(uint32_t pid)
89 #if !defined(CONFIG_USER_ONLY)
90 cris_mmu_flush_pid(env, pid);
94 void helper_dump(uint32_t a0, uint32_t a1, uint32_t a2)
96 (fprintf(logfile, "%s: a0=%x a1=%x\n", __func__, a0, a1));
99 void helper_dummy(void)
104 /* Used by the tlb decoder. */
105 #define EXTRACT_FIELD(src, start, end) \
106 (((src) >> start) & ((1 << (end - start + 1)) - 1))
108 void helper_movl_sreg_reg (uint32_t sreg, uint32_t reg)
111 srs = env->pregs[PR_SRS];
113 env->sregs[srs][sreg] = env->regs[reg];
115 #if !defined(CONFIG_USER_ONLY)
116 if (srs == 1 || srs == 2) {
118 /* Writes to tlb-hi write to mm_cause as a side
120 env->sregs[SFR_RW_MM_TLB_HI] = T0;
121 env->sregs[SFR_R_MM_CAUSE] = T0;
123 else if (sreg == 5) {
130 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
135 /* We've just made a write to tlb_lo. */
136 lo = env->sregs[SFR_RW_MM_TLB_LO];
137 /* Writes are done via r_mm_cause. */
138 hi = env->sregs[SFR_R_MM_CAUSE];
140 vaddr = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].hi,
142 vaddr <<= TARGET_PAGE_BITS;
143 tlb_v = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].lo,
145 env->tlbsets[srs - 1][set][idx].lo = lo;
146 env->tlbsets[srs - 1][set][idx].hi = hi;
149 "tlb flush vaddr=%x v=%d pc=%x\n",
150 vaddr, tlb_v, env->pc));
151 tlb_flush_page(env, vaddr);
157 void helper_movl_reg_sreg (uint32_t reg, uint32_t sreg)
160 env->pregs[PR_SRS] &= 3;
161 srs = env->pregs[PR_SRS];
163 #if !defined(CONFIG_USER_ONLY)
164 if (srs == 1 || srs == 2)
170 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
175 /* Update the mirror regs. */
176 hi = env->tlbsets[srs - 1][set][idx].hi;
177 lo = env->tlbsets[srs - 1][set][idx].lo;
178 env->sregs[SFR_RW_MM_TLB_HI] = hi;
179 env->sregs[SFR_RW_MM_TLB_LO] = lo;
182 env->regs[reg] = env->sregs[srs][sreg];
186 static void cris_ccs_rshift(CPUState *env)
190 /* Apply the ccs shift. */
191 ccs = env->pregs[PR_CCS];
192 ccs = (ccs & 0xc0000000) | ((ccs & 0x0fffffff) >> 10);
195 /* Enter user mode. */
196 env->ksp = env->regs[R_SP];
197 env->regs[R_SP] = env->pregs[PR_USP];
200 env->pregs[PR_CCS] = ccs;
203 void helper_rfe(void)
205 int rflag = env->pregs[PR_CCS] & R_FLAG;
207 D(fprintf(logfile, "rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
208 env->pregs[PR_ERP], env->pregs[PR_PID],
212 cris_ccs_rshift(env);
214 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
216 env->pregs[PR_CCS] |= P_FLAG;
219 void helper_store(uint32_t a0)
221 if (env->pregs[PR_CCS] & P_FLAG )
223 cpu_abort(env, "cond_store_failed! pc=%x a0=%x\n",
228 void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
231 D(printf("%s addr=%x w=%d ex=%d asi=%d\n",
232 __func__, addr, is_write, is_exec, is_asi));
235 static void evaluate_flags_writeback(uint32_t flags)
239 /* Extended arithmetics, leave the z flag alone. */
241 if ((x || env->cc_op == CC_OP_ADDC)
243 env->cc_mask &= ~Z_FLAG;
245 /* all insn clear the x-flag except setf or clrf. */
246 env->pregs[PR_CCS] &= ~(env->cc_mask | X_FLAG);
247 flags &= env->cc_mask;
248 env->pregs[PR_CCS] |= flags;
251 void helper_evaluate_flags_muls(void)
263 res = env->cc_result;
265 dneg = ((int32_t)res) < 0;
267 mof = env->pregs[PR_MOF];
275 if ((dneg && mof != -1)
276 || (!dneg && mof != 0))
278 evaluate_flags_writeback(flags);
281 void helper_evaluate_flags_mulu(void)
292 res = env->cc_result;
294 mof = env->pregs[PR_MOF];
305 evaluate_flags_writeback(flags);
308 void helper_evaluate_flags_mcp(void)
317 res = env->cc_result;
319 if ((res & 0x80000000L) != 0L)
322 if (((src & 0x80000000L) == 0L)
323 && ((dst & 0x80000000L) == 0L))
327 else if (((src & 0x80000000L) != 0L) &&
328 ((dst & 0x80000000L) != 0L))
337 if (((src & 0x80000000L) != 0L)
338 && ((dst & 0x80000000L) != 0L))
340 if ((dst & 0x80000000L) != 0L
341 || (src & 0x80000000L) != 0L)
345 evaluate_flags_writeback(flags);
348 void helper_evaluate_flags_alu_4(void)
358 /* Reconstruct the result. */
368 res = env->cc_result;
372 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
375 if ((res & 0x80000000L) != 0L)
378 if (((src & 0x80000000L) == 0L)
379 && ((dst & 0x80000000L) == 0L))
383 else if (((src & 0x80000000L) != 0L) &&
384 ((dst & 0x80000000L) != 0L))
393 if (((src & 0x80000000L) != 0L)
394 && ((dst & 0x80000000L) != 0L))
396 if ((dst & 0x80000000L) != 0L
397 || (src & 0x80000000L) != 0L)
401 if (env->cc_op == CC_OP_SUB
402 || env->cc_op == CC_OP_CMP) {
405 evaluate_flags_writeback(flags);
408 void helper_evaluate_flags_move_4 (void)
413 res = env->cc_result;
415 if ((int32_t)res < 0)
420 evaluate_flags_writeback(flags);
422 void helper_evaluate_flags_move_2 (void)
429 res = env->cc_result;
431 if ((int16_t)res < 0L)
436 evaluate_flags_writeback(flags);
439 /* TODO: This is expensive. We could split things up and only evaluate part of
440 CCR on a need to know basis. For now, we simply re-evaluate everything. */
441 void helper_evaluate_flags (void)
450 res = env->cc_result;
452 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
455 /* Now, evaluate the flags. This stuff is based on
456 Per Zander's CRISv10 simulator. */
457 switch (env->cc_size)
460 if ((res & 0x80L) != 0L)
463 if (((src & 0x80L) == 0L)
464 && ((dst & 0x80L) == 0L))
468 else if (((src & 0x80L) != 0L)
469 && ((dst & 0x80L) != 0L))
476 if ((res & 0xFFL) == 0L)
480 if (((src & 0x80L) != 0L)
481 && ((dst & 0x80L) != 0L))
485 if ((dst & 0x80L) != 0L
486 || (src & 0x80L) != 0L)
493 if ((res & 0x8000L) != 0L)
496 if (((src & 0x8000L) == 0L)
497 && ((dst & 0x8000L) == 0L))
501 else if (((src & 0x8000L) != 0L)
502 && ((dst & 0x8000L) != 0L))
509 if ((res & 0xFFFFL) == 0L)
513 if (((src & 0x8000L) != 0L)
514 && ((dst & 0x8000L) != 0L))
518 if ((dst & 0x8000L) != 0L
519 || (src & 0x8000L) != 0L)
526 if ((res & 0x80000000L) != 0L)
529 if (((src & 0x80000000L) == 0L)
530 && ((dst & 0x80000000L) == 0L))
534 else if (((src & 0x80000000L) != 0L) &&
535 ((dst & 0x80000000L) != 0L))
544 if (((src & 0x80000000L) != 0L)
545 && ((dst & 0x80000000L) != 0L))
547 if ((dst & 0x80000000L) != 0L
548 || (src & 0x80000000L) != 0L)
556 if (env->cc_op == CC_OP_SUB
557 || env->cc_op == CC_OP_CMP) {
560 evaluate_flags_writeback(flags);
563 void helper_top_evaluate_flags(void)
568 helper_evaluate_flags_mcp();
571 helper_evaluate_flags_muls();
574 helper_evaluate_flags_mulu();
583 switch (env->cc_size)
586 helper_evaluate_flags_move_4();
589 helper_evaluate_flags_move_2();
592 helper_evaluate_flags();
601 switch (env->cc_size)
604 helper_evaluate_flags_alu_4();
607 helper_evaluate_flags();