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 D(fprintf(logfile, "rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
206 env->pregs[PR_ERP], env->pregs[PR_PID],
210 cris_ccs_rshift(env);
212 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
213 if (!(env->pregs[PR_CCS] & R_FLAG))
214 env->pregs[PR_CCS] |= P_FLAG;
217 void helper_store(uint32_t a0)
219 if (env->pregs[PR_CCS] & P_FLAG )
221 cpu_abort(env, "cond_store_failed! pc=%x a0=%x\n",
226 void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
229 D(printf("%s addr=%x w=%d ex=%d asi=%d\n",
230 __func__, addr, is_write, is_exec, is_asi));
233 static void evaluate_flags_writeback(uint32_t flags)
237 /* Extended arithmetics, leave the z flag alone. */
239 if ((x || env->cc_op == CC_OP_ADDC)
241 env->cc_mask &= ~Z_FLAG;
243 /* all insn clear the x-flag except setf or clrf. */
244 env->pregs[PR_CCS] &= ~(env->cc_mask | X_FLAG);
245 flags &= env->cc_mask;
246 env->pregs[PR_CCS] |= flags;
249 void helper_evaluate_flags_muls(void)
261 res = env->cc_result;
263 dneg = ((int32_t)res) < 0;
265 mof = env->pregs[PR_MOF];
273 if ((dneg && mof != -1)
274 || (!dneg && mof != 0))
276 evaluate_flags_writeback(flags);
279 void helper_evaluate_flags_mulu(void)
290 res = env->cc_result;
292 mof = env->pregs[PR_MOF];
303 evaluate_flags_writeback(flags);
306 void helper_evaluate_flags_mcp(void)
315 res = env->cc_result;
317 if ((res & 0x80000000L) != 0L)
320 if (((src & 0x80000000L) == 0L)
321 && ((dst & 0x80000000L) == 0L))
325 else if (((src & 0x80000000L) != 0L) &&
326 ((dst & 0x80000000L) != 0L))
335 if (((src & 0x80000000L) != 0L)
336 && ((dst & 0x80000000L) != 0L))
338 if ((dst & 0x80000000L) != 0L
339 || (src & 0x80000000L) != 0L)
343 evaluate_flags_writeback(flags);
346 void helper_evaluate_flags_alu_4(void)
356 /* Reconstruct the result. */
366 res = env->cc_result;
370 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
373 if ((res & 0x80000000L) != 0L)
376 if (((src & 0x80000000L) == 0L)
377 && ((dst & 0x80000000L) == 0L))
381 else if (((src & 0x80000000L) != 0L) &&
382 ((dst & 0x80000000L) != 0L))
391 if (((src & 0x80000000L) != 0L)
392 && ((dst & 0x80000000L) != 0L))
394 if ((dst & 0x80000000L) != 0L
395 || (src & 0x80000000L) != 0L)
399 if (env->cc_op == CC_OP_SUB
400 || env->cc_op == CC_OP_CMP) {
403 evaluate_flags_writeback(flags);
406 void helper_evaluate_flags_move_4 (void)
411 res = env->cc_result;
413 if ((int32_t)res < 0)
418 evaluate_flags_writeback(flags);
420 void helper_evaluate_flags_move_2 (void)
427 res = env->cc_result;
429 if ((int16_t)res < 0L)
434 evaluate_flags_writeback(flags);
437 /* TODO: This is expensive. We could split things up and only evaluate part of
438 CCR on a need to know basis. For now, we simply re-evaluate everything. */
439 void helper_evaluate_flags (void)
448 res = env->cc_result;
450 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
453 /* Now, evaluate the flags. This stuff is based on
454 Per Zander's CRISv10 simulator. */
455 switch (env->cc_size)
458 if ((res & 0x80L) != 0L)
461 if (((src & 0x80L) == 0L)
462 && ((dst & 0x80L) == 0L))
466 else if (((src & 0x80L) != 0L)
467 && ((dst & 0x80L) != 0L))
474 if ((res & 0xFFL) == 0L)
478 if (((src & 0x80L) != 0L)
479 && ((dst & 0x80L) != 0L))
483 if ((dst & 0x80L) != 0L
484 || (src & 0x80L) != 0L)
491 if ((res & 0x8000L) != 0L)
494 if (((src & 0x8000L) == 0L)
495 && ((dst & 0x8000L) == 0L))
499 else if (((src & 0x8000L) != 0L)
500 && ((dst & 0x8000L) != 0L))
507 if ((res & 0xFFFFL) == 0L)
511 if (((src & 0x8000L) != 0L)
512 && ((dst & 0x8000L) != 0L))
516 if ((dst & 0x8000L) != 0L
517 || (src & 0x8000L) != 0L)
524 if ((res & 0x80000000L) != 0L)
527 if (((src & 0x80000000L) == 0L)
528 && ((dst & 0x80000000L) == 0L))
532 else if (((src & 0x80000000L) != 0L) &&
533 ((dst & 0x80000000L) != 0L))
542 if (((src & 0x80000000L) != 0L)
543 && ((dst & 0x80000000L) != 0L))
545 if ((dst & 0x80000000L) != 0L
546 || (src & 0x80000000L) != 0L)
554 if (env->cc_op == CC_OP_SUB
555 || env->cc_op == CC_OP_CMP) {
558 evaluate_flags_writeback(flags);
561 void helper_top_evaluate_flags(void)
566 helper_evaluate_flags_mcp();
569 helper_evaluate_flags_muls();
572 helper_evaluate_flags_mulu();
581 switch (env->cc_size)
584 helper_evaluate_flags_move_4();
587 helper_evaluate_flags_move_2();
590 helper_evaluate_flags();
599 switch (env->cc_size)
602 helper_evaluate_flags_alu_4();
605 helper_evaluate_flags();