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
26 #define MMUSUFFIX _mmu
28 # define GETPC() ((void*)((unsigned long)__builtin_return_address(0) & 0x7fffffffUL))
30 # define GETPC() (__builtin_return_address(0))
34 #include "softmmu_template.h"
37 #include "softmmu_template.h"
40 #include "softmmu_template.h"
43 #include "softmmu_template.h"
47 /* Try to fill the TLB and return an exception if error. If retaddr is
48 NULL, it means that the function was called in C code (i.e. not
49 from generated code or from helper.c) */
50 /* XXX: fix it to restore all registers */
51 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
58 /* XXX: hack to restore env in all cases, even if not called from
63 D(fprintf(logfile, "%s pc=%x tpc=%x ra=%x\n", __func__,
64 env->pc, env->debug1, retaddr));
65 ret = cpu_cris_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
66 if (__builtin_expect(ret, 0)) {
68 /* now we have a real cpu fault */
69 pc = (unsigned long)retaddr;
72 /* the PC is inside the translated code. It means that we have
73 a virtual CPU fault */
74 cpu_restore_state(tb, env, pc, NULL);
82 void helper_raise_exception(uint32_t index)
84 env->exception_index = index;
88 void helper_tlb_flush_pid(uint32_t pid)
90 #if !defined(CONFIG_USER_ONLY)
91 cris_mmu_flush_pid(env, pid);
95 void helper_tlb_flush(void)
100 void helper_dump(uint32_t a0, uint32_t a1)
102 (fprintf(logfile, "%s: a0=%x a1=%x\n", __func__, a0, a1));
105 void helper_dummy(void)
110 /* Used by the tlb decoder. */
111 #define EXTRACT_FIELD(src, start, end) \
112 (((src) >> start) & ((1 << (end - start + 1)) - 1))
114 void helper_movl_sreg_reg (uint32_t sreg, uint32_t reg)
117 srs = env->pregs[PR_SRS];
119 env->sregs[srs][sreg] = env->regs[reg];
121 #if !defined(CONFIG_USER_ONLY)
122 if (srs == 1 || srs == 2) {
124 /* Writes to tlb-hi write to mm_cause as a side
126 env->sregs[SFR_RW_MM_TLB_HI] = T0;
127 env->sregs[SFR_R_MM_CAUSE] = T0;
129 else if (sreg == 5) {
136 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
141 /* We've just made a write to tlb_lo. */
142 lo = env->sregs[SFR_RW_MM_TLB_LO];
143 /* Writes are done via r_mm_cause. */
144 hi = env->sregs[SFR_R_MM_CAUSE];
146 vaddr = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].hi,
148 vaddr <<= TARGET_PAGE_BITS;
149 tlb_v = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].lo,
151 env->tlbsets[srs - 1][set][idx].lo = lo;
152 env->tlbsets[srs - 1][set][idx].hi = hi;
155 "tlb flush vaddr=%x v=%d pc=%x\n",
156 vaddr, tlb_v, env->pc));
157 tlb_flush_page(env, vaddr);
163 void helper_movl_reg_sreg (uint32_t reg, uint32_t sreg)
166 env->pregs[PR_SRS] &= 3;
167 srs = env->pregs[PR_SRS];
169 #if !defined(CONFIG_USER_ONLY)
170 if (srs == 1 || srs == 2)
176 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
181 /* Update the mirror regs. */
182 hi = env->tlbsets[srs - 1][set][idx].hi;
183 lo = env->tlbsets[srs - 1][set][idx].lo;
184 env->sregs[SFR_RW_MM_TLB_HI] = hi;
185 env->sregs[SFR_RW_MM_TLB_LO] = lo;
188 env->regs[reg] = env->sregs[srs][sreg];
192 static void cris_ccs_rshift(CPUState *env)
196 /* Apply the ccs shift. */
197 ccs = env->pregs[PR_CCS];
198 ccs = (ccs & 0xc0000000) | ((ccs & 0x0fffffff) >> 10);
201 /* Enter user mode. */
202 env->ksp = env->regs[R_SP];
203 env->regs[R_SP] = env->pregs[PR_USP];
206 env->pregs[PR_CCS] = ccs;
209 void helper_rfe(void)
211 D(fprintf(logfile, "rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
212 env->pregs[PR_ERP], env->pregs[PR_PID],
216 cris_ccs_rshift(env);
218 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
219 if (!(env->pregs[PR_CCS] & R_FLAG))
220 env->pregs[PR_CCS] |= P_FLAG;
223 void helper_store(uint32_t a0)
225 if (env->pregs[PR_CCS] & P_FLAG )
227 cpu_abort(env, "cond_store_failed! pc=%x a0=%x\n",
232 void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
235 D(printf("%s addr=%x w=%d ex=%d asi=%d\n",
236 __func__, addr, is_write, is_exec, is_asi));
239 static void evaluate_flags_writeback(uint32_t flags)
243 /* Extended arithmetics, leave the z flag alone. */
244 env->debug3 = env->pregs[PR_CCS];
249 x = env->pregs[PR_CCS] & X_FLAG;
251 if ((x || env->cc_op == CC_OP_ADDC)
253 env->cc_mask &= ~Z_FLAG;
255 /* all insn clear the x-flag except setf or clrf. */
256 env->pregs[PR_CCS] &= ~(env->cc_mask | X_FLAG);
257 flags &= env->cc_mask;
258 env->pregs[PR_CCS] |= flags;
261 void helper_evaluate_flags_muls(void)
273 res = env->cc_result;
275 dneg = ((int32_t)res) < 0;
277 mof = env->pregs[PR_MOF];
285 if ((dneg && mof != -1)
286 || (!dneg && mof != 0))
288 evaluate_flags_writeback(flags);
291 void helper_evaluate_flags_mulu(void)
302 res = env->cc_result;
304 mof = env->pregs[PR_MOF];
315 evaluate_flags_writeback(flags);
318 void helper_evaluate_flags_mcp(void)
327 res = env->cc_result;
329 if ((res & 0x80000000L) != 0L)
332 if (((src & 0x80000000L) == 0L)
333 && ((dst & 0x80000000L) == 0L))
337 else if (((src & 0x80000000L) != 0L) &&
338 ((dst & 0x80000000L) != 0L))
347 if (((src & 0x80000000L) != 0L)
348 && ((dst & 0x80000000L) != 0L))
350 if ((dst & 0x80000000L) != 0L
351 || (src & 0x80000000L) != 0L)
355 evaluate_flags_writeback(flags);
358 void helper_evaluate_flags_alu_4(void)
367 res = env->cc_result;
369 if ((res & 0x80000000L) != 0L)
372 if (((src & 0x80000000L) == 0L)
373 && ((dst & 0x80000000L) == 0L))
377 else if (((src & 0x80000000L) != 0L) &&
378 ((dst & 0x80000000L) != 0L))
387 if (((src & 0x80000000L) != 0L)
388 && ((dst & 0x80000000L) != 0L))
390 if ((dst & 0x80000000L) != 0L
391 || (src & 0x80000000L) != 0L)
395 if (env->cc_op == CC_OP_SUB
396 || env->cc_op == CC_OP_CMP) {
399 evaluate_flags_writeback(flags);
402 void helper_evaluate_flags_move_4 (void)
409 res = env->cc_result;
411 if ((int32_t)res < 0)
416 evaluate_flags_writeback(flags);
418 void helper_evaluate_flags_move_2 (void)
425 res = env->cc_result;
427 if ((int16_t)res < 0L)
432 evaluate_flags_writeback(flags);
435 /* TODO: This is expensive. We could split things up and only evaluate part of
436 CCR on a need to know basis. For now, we simply re-evaluate everything. */
437 void helper_evaluate_flags (void)
446 res = env->cc_result;
449 /* Now, evaluate the flags. This stuff is based on
450 Per Zander's CRISv10 simulator. */
451 switch (env->cc_size)
454 if ((res & 0x80L) != 0L)
457 if (((src & 0x80L) == 0L)
458 && ((dst & 0x80L) == 0L))
462 else if (((src & 0x80L) != 0L)
463 && ((dst & 0x80L) != 0L))
470 if ((res & 0xFFL) == 0L)
474 if (((src & 0x80L) != 0L)
475 && ((dst & 0x80L) != 0L))
479 if ((dst & 0x80L) != 0L
480 || (src & 0x80L) != 0L)
487 if ((res & 0x8000L) != 0L)
490 if (((src & 0x8000L) == 0L)
491 && ((dst & 0x8000L) == 0L))
495 else if (((src & 0x8000L) != 0L)
496 && ((dst & 0x8000L) != 0L))
503 if ((res & 0xFFFFL) == 0L)
507 if (((src & 0x8000L) != 0L)
508 && ((dst & 0x8000L) != 0L))
512 if ((dst & 0x8000L) != 0L
513 || (src & 0x8000L) != 0L)
520 if ((res & 0x80000000L) != 0L)
523 if (((src & 0x80000000L) == 0L)
524 && ((dst & 0x80000000L) == 0L))
528 else if (((src & 0x80000000L) != 0L) &&
529 ((dst & 0x80000000L) != 0L))
538 if (((src & 0x80000000L) != 0L)
539 && ((dst & 0x80000000L) != 0L))
541 if ((dst & 0x80000000L) != 0L
542 || (src & 0x80000000L) != 0L)
550 if (env->cc_op == CC_OP_SUB
551 || env->cc_op == CC_OP_CMP) {
554 evaluate_flags_writeback(flags);