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
29 #if !defined(CONFIG_USER_ONLY)
31 #define MMUSUFFIX _mmu
34 #include "softmmu_template.h"
37 #include "softmmu_template.h"
40 #include "softmmu_template.h"
43 #include "softmmu_template.h"
45 /* Try to fill the TLB and return an exception if error. If retaddr is
46 NULL, it means that the function was called in C code (i.e. not
47 from generated code or from helper.c) */
48 /* XXX: fix it to restore all registers */
49 void tlb_fill (target_ulong addr
, int is_write
, int mmu_idx
, void *retaddr
)
56 /* XXX: hack to restore env in all cases, even if not called from
61 D(fprintf(logfile
, "%s pc=%x tpc=%x ra=%x\n", __func__
,
62 env
->pc
, env
->debug1
, retaddr
));
63 ret
= cpu_cris_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
, 1);
64 if (__builtin_expect(ret
, 0)) {
66 /* now we have a real cpu fault */
67 pc
= (unsigned long)retaddr
;
70 /* the PC is inside the translated code. It means that we have
71 a virtual CPU fault */
72 cpu_restore_state(tb
, env
, pc
, NULL
);
74 /* Evaluate flags after retranslation. */
75 helper_top_evaluate_flags();
85 void helper_raise_exception(uint32_t index
)
87 env
->exception_index
= index
;
91 void helper_tlb_flush_pid(uint32_t pid
)
93 #if !defined(CONFIG_USER_ONLY)
94 cris_mmu_flush_pid(env
, pid
);
98 void helper_dump(uint32_t a0
, uint32_t a1
, uint32_t a2
)
100 (fprintf(logfile
, "%s: a0=%x a1=%x\n", __func__
, a0
, a1
));
103 void helper_dummy(void)
108 /* Used by the tlb decoder. */
109 #define EXTRACT_FIELD(src, start, end) \
110 (((src) >> start) & ((1 << (end - start + 1)) - 1))
112 void helper_movl_sreg_reg (uint32_t sreg
, uint32_t reg
)
115 srs
= env
->pregs
[PR_SRS
];
117 env
->sregs
[srs
][sreg
] = env
->regs
[reg
];
119 #if !defined(CONFIG_USER_ONLY)
120 if (srs
== 1 || srs
== 2) {
122 /* Writes to tlb-hi write to mm_cause as a side
124 env
->sregs
[SFR_RW_MM_TLB_HI
] = env
->regs
[reg
];
125 env
->sregs
[SFR_R_MM_CAUSE
] = env
->regs
[reg
];
127 else if (sreg
== 5) {
134 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
139 /* We've just made a write to tlb_lo. */
140 lo
= env
->sregs
[SFR_RW_MM_TLB_LO
];
141 /* Writes are done via r_mm_cause. */
142 hi
= env
->sregs
[SFR_R_MM_CAUSE
];
144 vaddr
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].hi
,
146 vaddr
<<= TARGET_PAGE_BITS
;
147 tlb_v
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].lo
,
149 env
->tlbsets
[srs
- 1][set
][idx
].lo
= lo
;
150 env
->tlbsets
[srs
- 1][set
][idx
].hi
= hi
;
153 "tlb flush vaddr=%x v=%d pc=%x\n",
154 vaddr
, tlb_v
, env
->pc
));
155 tlb_flush_page(env
, vaddr
);
161 void helper_movl_reg_sreg (uint32_t reg
, uint32_t sreg
)
164 env
->pregs
[PR_SRS
] &= 3;
165 srs
= env
->pregs
[PR_SRS
];
167 #if !defined(CONFIG_USER_ONLY)
168 if (srs
== 1 || srs
== 2)
174 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
179 /* Update the mirror regs. */
180 hi
= env
->tlbsets
[srs
- 1][set
][idx
].hi
;
181 lo
= env
->tlbsets
[srs
- 1][set
][idx
].lo
;
182 env
->sregs
[SFR_RW_MM_TLB_HI
] = hi
;
183 env
->sregs
[SFR_RW_MM_TLB_LO
] = lo
;
186 env
->regs
[reg
] = env
->sregs
[srs
][sreg
];
190 static void cris_ccs_rshift(CPUState
*env
)
194 /* Apply the ccs shift. */
195 ccs
= env
->pregs
[PR_CCS
];
196 ccs
= (ccs
& 0xc0000000) | ((ccs
& 0x0fffffff) >> 10);
199 /* Enter user mode. */
200 env
->ksp
= env
->regs
[R_SP
];
201 env
->regs
[R_SP
] = env
->pregs
[PR_USP
];
204 env
->pregs
[PR_CCS
] = ccs
;
207 void helper_rfe(void)
209 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
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. */
220 env
->pregs
[PR_CCS
] |= P_FLAG
;
223 void helper_rfn(void)
225 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
227 D(fprintf(logfile
, "rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
228 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
232 cris_ccs_rshift(env
);
234 /* Set the P_FLAG only if the R_FLAG is not set. */
236 env
->pregs
[PR_CCS
] |= P_FLAG
;
238 /* Always set the M flag. */
239 env
->pregs
[PR_CCS
] |= M_FLAG
;
242 void helper_store(uint32_t a0
)
244 if (env
->pregs
[PR_CCS
] & P_FLAG
)
246 cpu_abort(env
, "cond_store_failed! pc=%x a0=%x\n",
251 void do_unassigned_access(target_phys_addr_t addr
, int is_write
, int is_exec
,
254 D(printf("%s addr=%x w=%d ex=%d asi=%d\n",
255 __func__
, addr
, is_write
, is_exec
, is_asi
));
258 static void evaluate_flags_writeback(uint32_t flags
)
262 /* Extended arithmetics, leave the z flag alone. */
264 if ((x
|| env
->cc_op
== CC_OP_ADDC
)
266 env
->cc_mask
&= ~Z_FLAG
;
268 /* all insn clear the x-flag except setf or clrf. */
269 env
->pregs
[PR_CCS
] &= ~(env
->cc_mask
| X_FLAG
);
270 flags
&= env
->cc_mask
;
271 env
->pregs
[PR_CCS
] |= flags
;
274 void helper_evaluate_flags_muls(void)
286 res
= env
->cc_result
;
288 dneg
= ((int32_t)res
) < 0;
290 mof
= env
->pregs
[PR_MOF
];
298 if ((dneg
&& mof
!= -1)
299 || (!dneg
&& mof
!= 0))
301 evaluate_flags_writeback(flags
);
304 void helper_evaluate_flags_mulu(void)
315 res
= env
->cc_result
;
317 mof
= env
->pregs
[PR_MOF
];
328 evaluate_flags_writeback(flags
);
331 void helper_evaluate_flags_mcp(void)
340 res
= env
->cc_result
;
342 if ((res
& 0x80000000L
) != 0L)
345 if (((src
& 0x80000000L
) == 0L)
346 && ((dst
& 0x80000000L
) == 0L))
350 else if (((src
& 0x80000000L
) != 0L) &&
351 ((dst
& 0x80000000L
) != 0L))
360 if (((src
& 0x80000000L
) != 0L)
361 && ((dst
& 0x80000000L
) != 0L))
363 if ((dst
& 0x80000000L
) != 0L
364 || (src
& 0x80000000L
) != 0L)
368 evaluate_flags_writeback(flags
);
371 void helper_evaluate_flags_alu_4(void)
381 /* Reconstruct the result. */
391 res
= env
->cc_result
;
395 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
398 if ((res
& 0x80000000L
) != 0L)
401 if (((src
& 0x80000000L
) == 0L)
402 && ((dst
& 0x80000000L
) == 0L))
406 else if (((src
& 0x80000000L
) != 0L) &&
407 ((dst
& 0x80000000L
) != 0L))
416 if (((src
& 0x80000000L
) != 0L)
417 && ((dst
& 0x80000000L
) != 0L))
419 if ((dst
& 0x80000000L
) != 0L
420 || (src
& 0x80000000L
) != 0L)
424 if (env
->cc_op
== CC_OP_SUB
425 || env
->cc_op
== CC_OP_CMP
) {
428 evaluate_flags_writeback(flags
);
431 void helper_evaluate_flags_move_4 (void)
436 res
= env
->cc_result
;
438 if ((int32_t)res
< 0)
443 evaluate_flags_writeback(flags
);
445 void helper_evaluate_flags_move_2 (void)
452 res
= env
->cc_result
;
454 if ((int16_t)res
< 0L)
459 evaluate_flags_writeback(flags
);
462 /* TODO: This is expensive. We could split things up and only evaluate part of
463 CCR on a need to know basis. For now, we simply re-evaluate everything. */
464 void helper_evaluate_flags (void)
473 res
= env
->cc_result
;
475 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
478 /* Now, evaluate the flags. This stuff is based on
479 Per Zander's CRISv10 simulator. */
480 switch (env
->cc_size
)
483 if ((res
& 0x80L
) != 0L)
486 if (((src
& 0x80L
) == 0L)
487 && ((dst
& 0x80L
) == 0L))
491 else if (((src
& 0x80L
) != 0L)
492 && ((dst
& 0x80L
) != 0L))
499 if ((res
& 0xFFL
) == 0L)
503 if (((src
& 0x80L
) != 0L)
504 && ((dst
& 0x80L
) != 0L))
508 if ((dst
& 0x80L
) != 0L
509 || (src
& 0x80L
) != 0L)
516 if ((res
& 0x8000L
) != 0L)
519 if (((src
& 0x8000L
) == 0L)
520 && ((dst
& 0x8000L
) == 0L))
524 else if (((src
& 0x8000L
) != 0L)
525 && ((dst
& 0x8000L
) != 0L))
532 if ((res
& 0xFFFFL
) == 0L)
536 if (((src
& 0x8000L
) != 0L)
537 && ((dst
& 0x8000L
) != 0L))
541 if ((dst
& 0x8000L
) != 0L
542 || (src
& 0x8000L
) != 0L)
549 if ((res
& 0x80000000L
) != 0L)
552 if (((src
& 0x80000000L
) == 0L)
553 && ((dst
& 0x80000000L
) == 0L))
557 else if (((src
& 0x80000000L
) != 0L) &&
558 ((dst
& 0x80000000L
) != 0L))
567 if (((src
& 0x80000000L
) != 0L)
568 && ((dst
& 0x80000000L
) != 0L))
570 if ((dst
& 0x80000000L
) != 0L
571 || (src
& 0x80000000L
) != 0L)
579 if (env
->cc_op
== CC_OP_SUB
580 || env
->cc_op
== CC_OP_CMP
) {
583 evaluate_flags_writeback(flags
);
586 void helper_top_evaluate_flags(void)
591 helper_evaluate_flags_mcp();
594 helper_evaluate_flags_muls();
597 helper_evaluate_flags_mulu();
606 switch (env
->cc_size
)
609 helper_evaluate_flags_move_4();
612 helper_evaluate_flags_move_2();
615 helper_evaluate_flags();
624 switch (env
->cc_size
)
627 helper_evaluate_flags_alu_4();
630 helper_evaluate_flags();