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
29 #include "softmmu_template.h"
32 #include "softmmu_template.h"
35 #include "softmmu_template.h"
38 #include "softmmu_template.h"
42 /* Try to fill the TLB and return an exception if error. If retaddr is
43 NULL, it means that the function was called in C code (i.e. not
44 from generated code or from helper.c) */
45 /* XXX: fix it to restore all registers */
46 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
53 /* XXX: hack to restore env in all cases, even if not called from
58 D(fprintf(logfile, "%s pc=%x tpc=%x ra=%x\n", __func__,
59 env->pc, env->debug1, retaddr));
60 ret = cpu_cris_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
61 if (__builtin_expect(ret, 0)) {
63 /* now we have a real cpu fault */
64 pc = (unsigned long)retaddr;
67 /* the PC is inside the translated code. It means that we have
68 a virtual CPU fault */
69 cpu_restore_state(tb, env, pc, NULL);
77 void helper_raise_exception(uint32_t index)
79 env->exception_index = index;
83 void helper_tlb_flush_pid(uint32_t pid)
85 #if !defined(CONFIG_USER_ONLY)
86 cris_mmu_flush_pid(env, pid);
90 void helper_tlb_flush(void)
95 void helper_dump(uint32_t a0, uint32_t a1)
97 (fprintf(logfile, "%s: a0=%x a1=%x\n", __func__, a0, a1));
100 void helper_dummy(void)
105 /* Used by the tlb decoder. */
106 #define EXTRACT_FIELD(src, start, end) \
107 (((src) >> start) & ((1 << (end - start + 1)) - 1))
109 void helper_movl_sreg_reg (uint32_t sreg, uint32_t reg)
112 srs = env->pregs[PR_SRS];
114 env->sregs[srs][sreg] = env->regs[reg];
116 #if !defined(CONFIG_USER_ONLY)
117 if (srs == 1 || srs == 2) {
119 /* Writes to tlb-hi write to mm_cause as a side
121 env->sregs[SFR_RW_MM_TLB_HI] = T0;
122 env->sregs[SFR_R_MM_CAUSE] = T0;
124 else if (sreg == 5) {
131 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
136 /* We've just made a write to tlb_lo. */
137 lo = env->sregs[SFR_RW_MM_TLB_LO];
138 /* Writes are done via r_mm_cause. */
139 hi = env->sregs[SFR_R_MM_CAUSE];
141 vaddr = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].hi,
143 vaddr <<= TARGET_PAGE_BITS;
144 tlb_v = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].lo,
146 env->tlbsets[srs - 1][set][idx].lo = lo;
147 env->tlbsets[srs - 1][set][idx].hi = hi;
150 "tlb flush vaddr=%x v=%d pc=%x\n",
151 vaddr, tlb_v, env->pc));
152 tlb_flush_page(env, vaddr);
158 void helper_movl_reg_sreg (uint32_t reg, uint32_t sreg)
161 env->pregs[PR_SRS] &= 3;
162 srs = env->pregs[PR_SRS];
164 #if !defined(CONFIG_USER_ONLY)
165 if (srs == 1 || srs == 2)
171 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
176 /* Update the mirror regs. */
177 hi = env->tlbsets[srs - 1][set][idx].hi;
178 lo = env->tlbsets[srs - 1][set][idx].lo;
179 env->sregs[SFR_RW_MM_TLB_HI] = hi;
180 env->sregs[SFR_RW_MM_TLB_LO] = lo;
183 env->regs[reg] = env->sregs[srs][sreg];
187 static void cris_ccs_rshift(CPUState *env)
191 /* Apply the ccs shift. */
192 ccs = env->pregs[PR_CCS];
193 ccs = (ccs & 0xc0000000) | ((ccs & 0x0fffffff) >> 10);
196 /* Enter user mode. */
197 env->ksp = env->regs[R_SP];
198 env->regs[R_SP] = env->pregs[PR_USP];
201 env->pregs[PR_CCS] = ccs;
204 void helper_rfe(void)
206 D(fprintf(logfile, "rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
207 env->pregs[PR_ERP], env->pregs[PR_PID],
211 cris_ccs_rshift(env);
213 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
214 if (!(env->pregs[PR_CCS] & R_FLAG))
215 env->pregs[PR_CCS] |= P_FLAG;
218 void helper_store(uint32_t a0)
220 if (env->pregs[PR_CCS] & P_FLAG )
222 cpu_abort(env, "cond_store_failed! pc=%x a0=%x\n",
227 void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
230 D(printf("%s addr=%x w=%d ex=%d asi=%d\n",
231 __func__, addr, is_write, is_exec, is_asi));
234 static void evaluate_flags_writeback(uint32_t flags)
238 /* Extended arithmetics, leave the z flag alone. */
239 env->debug3 = env->pregs[PR_CCS];
244 x = env->pregs[PR_CCS] & X_FLAG;
246 if ((x || env->cc_op == CC_OP_ADDC)
248 env->cc_mask &= ~Z_FLAG;
250 /* all insn clear the x-flag except setf or clrf. */
251 env->pregs[PR_CCS] &= ~(env->cc_mask | X_FLAG);
252 flags &= env->cc_mask;
253 env->pregs[PR_CCS] |= flags;
256 void helper_evaluate_flags_muls(void)
268 res = env->cc_result;
270 dneg = ((int32_t)res) < 0;
272 mof = env->pregs[PR_MOF];
280 if ((dneg && mof != -1)
281 || (!dneg && mof != 0))
283 evaluate_flags_writeback(flags);
286 void helper_evaluate_flags_mulu(void)
297 res = env->cc_result;
299 mof = env->pregs[PR_MOF];
310 evaluate_flags_writeback(flags);
313 void helper_evaluate_flags_mcp(void)
322 res = env->cc_result;
324 if ((res & 0x80000000L) != 0L)
327 if (((src & 0x80000000L) == 0L)
328 && ((dst & 0x80000000L) == 0L))
332 else if (((src & 0x80000000L) != 0L) &&
333 ((dst & 0x80000000L) != 0L))
342 if (((src & 0x80000000L) != 0L)
343 && ((dst & 0x80000000L) != 0L))
345 if ((dst & 0x80000000L) != 0L
346 || (src & 0x80000000L) != 0L)
350 evaluate_flags_writeback(flags);
353 void helper_evaluate_flags_alu_4(void)
362 res = env->cc_result;
364 if ((res & 0x80000000L) != 0L)
367 if (((src & 0x80000000L) == 0L)
368 && ((dst & 0x80000000L) == 0L))
372 else if (((src & 0x80000000L) != 0L) &&
373 ((dst & 0x80000000L) != 0L))
382 if (((src & 0x80000000L) != 0L)
383 && ((dst & 0x80000000L) != 0L))
385 if ((dst & 0x80000000L) != 0L
386 || (src & 0x80000000L) != 0L)
390 if (env->cc_op == CC_OP_SUB
391 || env->cc_op == CC_OP_CMP) {
394 evaluate_flags_writeback(flags);
397 void helper_evaluate_flags_move_4 (void)
404 res = env->cc_result;
406 if ((int32_t)res < 0)
411 evaluate_flags_writeback(flags);
413 void helper_evaluate_flags_move_2 (void)
420 res = env->cc_result;
422 if ((int16_t)res < 0L)
427 evaluate_flags_writeback(flags);
430 /* TODO: This is expensive. We could split things up and only evaluate part of
431 CCR on a need to know basis. For now, we simply re-evaluate everything. */
432 void helper_evaluate_flags (void)
441 res = env->cc_result;
444 /* Now, evaluate the flags. This stuff is based on
445 Per Zander's CRISv10 simulator. */
446 switch (env->cc_size)
449 if ((res & 0x80L) != 0L)
452 if (((src & 0x80L) == 0L)
453 && ((dst & 0x80L) == 0L))
457 else if (((src & 0x80L) != 0L)
458 && ((dst & 0x80L) != 0L))
465 if ((res & 0xFFL) == 0L)
469 if (((src & 0x80L) != 0L)
470 && ((dst & 0x80L) != 0L))
474 if ((dst & 0x80L) != 0L
475 || (src & 0x80L) != 0L)
482 if ((res & 0x8000L) != 0L)
485 if (((src & 0x8000L) == 0L)
486 && ((dst & 0x8000L) == 0L))
490 else if (((src & 0x8000L) != 0L)
491 && ((dst & 0x8000L) != 0L))
498 if ((res & 0xFFFFL) == 0L)
502 if (((src & 0x8000L) != 0L)
503 && ((dst & 0x8000L) != 0L))
507 if ((dst & 0x8000L) != 0L
508 || (src & 0x8000L) != 0L)
515 if ((res & 0x80000000L) != 0L)
518 if (((src & 0x80000000L) == 0L)
519 && ((dst & 0x80000000L) == 0L))
523 else if (((src & 0x80000000L) != 0L) &&
524 ((dst & 0x80000000L) != 0L))
533 if (((src & 0x80000000L) != 0L)
534 && ((dst & 0x80000000L) != 0L))
536 if ((dst & 0x80000000L) != 0L
537 || (src & 0x80000000L) != 0L)
545 if (env->cc_op == CC_OP_SUB
546 || env->cc_op == CC_OP_CMP) {
549 evaluate_flags_writeback(flags);