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(void)
93 void helper_dump(uint32_t a0
, uint32_t a1
)
95 (fprintf(logfile
, "%s: a0=%x a1=%x\n", __func__
, a0
, a1
));
98 void helper_dummy(void)
103 void helper_movl_sreg_reg (uint32_t sreg
, uint32_t reg
)
106 srs
= env
->pregs
[PR_SRS
];
108 env
->sregs
[srs
][sreg
] = env
->regs
[reg
];
110 #if !defined(CONFIG_USER_ONLY)
111 if (srs
== 1 || srs
== 2) {
113 /* Writes to tlb-hi write to mm_cause as a side
115 env
->sregs
[SFR_RW_MM_TLB_HI
] = T0
;
116 env
->sregs
[SFR_R_MM_CAUSE
] = T0
;
118 else if (sreg
== 5) {
124 vaddr
= cris_mmu_tlb_latest_update(env
);
125 D(fprintf(logfile
, "tlb flush vaddr=%x\n", vaddr
));
126 tlb_flush_page(env
, vaddr
);
128 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
133 /* We've just made a write to tlb_lo. */
134 lo
= env
->sregs
[SFR_RW_MM_TLB_LO
];
135 /* Writes are done via r_mm_cause. */
136 hi
= env
->sregs
[SFR_R_MM_CAUSE
];
137 env
->tlbsets
[srs
- 1][set
][idx
].lo
= lo
;
138 env
->tlbsets
[srs
- 1][set
][idx
].hi
= hi
;
144 void helper_movl_reg_sreg (uint32_t reg
, uint32_t sreg
)
147 env
->pregs
[PR_SRS
] &= 3;
148 srs
= env
->pregs
[PR_SRS
];
150 #if !defined(CONFIG_USER_ONLY)
151 if (srs
== 1 || srs
== 2)
157 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
162 /* Update the mirror regs. */
163 hi
= env
->tlbsets
[srs
- 1][set
][idx
].hi
;
164 lo
= env
->tlbsets
[srs
- 1][set
][idx
].lo
;
165 env
->sregs
[SFR_RW_MM_TLB_HI
] = hi
;
166 env
->sregs
[SFR_RW_MM_TLB_LO
] = lo
;
169 env
->regs
[reg
] = env
->sregs
[srs
][sreg
];
173 static void cris_ccs_rshift(CPUState
*env
)
177 /* Apply the ccs shift. */
178 ccs
= env
->pregs
[PR_CCS
];
179 ccs
= (ccs
& 0xc0000000) | ((ccs
& 0x0fffffff) >> 10);
182 /* Enter user mode. */
183 env
->ksp
= env
->regs
[R_SP
];
184 env
->regs
[R_SP
] = env
->pregs
[PR_USP
];
187 env
->pregs
[PR_CCS
] = ccs
;
190 void helper_rfe(void)
192 D(fprintf(logfile
, "rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
193 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
197 cris_ccs_rshift(env
);
199 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
200 if (!(env
->pregs
[PR_CCS
] & R_FLAG
))
201 env
->pregs
[PR_CCS
] |= P_FLAG
;
204 void helper_store(uint32_t a0
)
206 if (env
->pregs
[PR_CCS
] & P_FLAG
)
208 cpu_abort(env
, "cond_store_failed! pc=%x a0=%x\n",
213 void do_unassigned_access(target_phys_addr_t addr
, int is_write
, int is_exec
,
216 D(printf("%s addr=%x w=%d ex=%d asi=%d\n",
217 __func__
, addr
, is_write
, is_exec
, is_asi
));
220 static void evaluate_flags_writeback(uint32_t flags
)
224 /* Extended arithmetics, leave the z flag alone. */
225 env
->debug3
= env
->pregs
[PR_CCS
];
230 x
= env
->pregs
[PR_CCS
] & X_FLAG
;
232 if ((x
|| env
->cc_op
== CC_OP_ADDC
)
234 env
->cc_mask
&= ~Z_FLAG
;
236 /* all insn clear the x-flag except setf or clrf. */
237 env
->pregs
[PR_CCS
] &= ~(env
->cc_mask
| X_FLAG
);
238 flags
&= env
->cc_mask
;
239 env
->pregs
[PR_CCS
] |= flags
;
242 void helper_evaluate_flags_muls(void)
254 res
= env
->cc_result
;
256 dneg
= ((int32_t)res
) < 0;
258 mof
= env
->pregs
[PR_MOF
];
266 if ((dneg
&& mof
!= -1)
267 || (!dneg
&& mof
!= 0))
269 evaluate_flags_writeback(flags
);
272 void helper_evaluate_flags_mulu(void)
283 res
= env
->cc_result
;
285 mof
= env
->pregs
[PR_MOF
];
296 evaluate_flags_writeback(flags
);
299 void helper_evaluate_flags_mcp(void)
308 res
= env
->cc_result
;
310 if ((res
& 0x80000000L
) != 0L)
313 if (((src
& 0x80000000L
) == 0L)
314 && ((dst
& 0x80000000L
) == 0L))
318 else if (((src
& 0x80000000L
) != 0L) &&
319 ((dst
& 0x80000000L
) != 0L))
328 if (((src
& 0x80000000L
) != 0L)
329 && ((dst
& 0x80000000L
) != 0L))
331 if ((dst
& 0x80000000L
) != 0L
332 || (src
& 0x80000000L
) != 0L)
336 evaluate_flags_writeback(flags
);
339 void helper_evaluate_flags_alu_4(void)
348 res
= env
->cc_result
;
350 if ((res
& 0x80000000L
) != 0L)
353 if (((src
& 0x80000000L
) == 0L)
354 && ((dst
& 0x80000000L
) == 0L))
358 else if (((src
& 0x80000000L
) != 0L) &&
359 ((dst
& 0x80000000L
) != 0L))
368 if (((src
& 0x80000000L
) != 0L)
369 && ((dst
& 0x80000000L
) != 0L))
371 if ((dst
& 0x80000000L
) != 0L
372 || (src
& 0x80000000L
) != 0L)
376 if (env
->cc_op
== CC_OP_SUB
377 || env
->cc_op
== CC_OP_CMP
) {
380 evaluate_flags_writeback(flags
);
383 void helper_evaluate_flags_move_4 (void)
390 res
= env
->cc_result
;
392 if ((int32_t)res
< 0)
397 evaluate_flags_writeback(flags
);
399 void helper_evaluate_flags_move_2 (void)
406 res
= env
->cc_result
;
408 if ((int16_t)res
< 0L)
413 evaluate_flags_writeback(flags
);
416 /* TODO: This is expensive. We could split things up and only evaluate part of
417 CCR on a need to know basis. For now, we simply re-evaluate everything. */
418 void helper_evaluate_flags (void)
427 res
= env
->cc_result
;
430 /* Now, evaluate the flags. This stuff is based on
431 Per Zander's CRISv10 simulator. */
432 switch (env
->cc_size
)
435 if ((res
& 0x80L
) != 0L)
438 if (((src
& 0x80L
) == 0L)
439 && ((dst
& 0x80L
) == 0L))
443 else if (((src
& 0x80L
) != 0L)
444 && ((dst
& 0x80L
) != 0L))
451 if ((res
& 0xFFL
) == 0L)
455 if (((src
& 0x80L
) != 0L)
456 && ((dst
& 0x80L
) != 0L))
460 if ((dst
& 0x80L
) != 0L
461 || (src
& 0x80L
) != 0L)
468 if ((res
& 0x8000L
) != 0L)
471 if (((src
& 0x8000L
) == 0L)
472 && ((dst
& 0x8000L
) == 0L))
476 else if (((src
& 0x8000L
) != 0L)
477 && ((dst
& 0x8000L
) != 0L))
484 if ((res
& 0xFFFFL
) == 0L)
488 if (((src
& 0x8000L
) != 0L)
489 && ((dst
& 0x8000L
) != 0L))
493 if ((dst
& 0x8000L
) != 0L
494 || (src
& 0x8000L
) != 0L)
501 if ((res
& 0x80000000L
) != 0L)
504 if (((src
& 0x80000000L
) == 0L)
505 && ((dst
& 0x80000000L
) == 0L))
509 else if (((src
& 0x80000000L
) != 0L) &&
510 ((dst
& 0x80000000L
) != 0L))
519 if (((src
& 0x80000000L
) != 0L)
520 && ((dst
& 0x80000000L
) != 0L))
522 if ((dst
& 0x80000000L
) != 0L
523 || (src
& 0x80000000L
) != 0L)
531 if (env
->cc_op
== CC_OP_SUB
532 || env
->cc_op
== CC_OP_CMP
) {
535 evaluate_flags_writeback(flags
);