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, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
24 #include "exec/helper-proto.h"
25 #include "qemu/host-utils.h"
26 #include "exec/exec-all.h"
27 #include "exec/cpu_ldst.h"
29 //#define CRIS_OP_HELPER_DEBUG
32 #ifdef CRIS_OP_HELPER_DEBUG
34 #define D_LOG(...) qemu_log(__VA_ARGS__)
37 #define D_LOG(...) do { } while (0)
40 #if !defined(CONFIG_USER_ONLY)
41 /* Try to fill the TLB and return an exception if error. If retaddr is
42 NULL, it means that the function was called in C code (i.e. not
43 from generated code or from helper.c) */
44 void tlb_fill(CPUState
*cs
, target_ulong addr
, MMUAccessType access_type
,
45 int mmu_idx
, uintptr_t retaddr
)
47 CRISCPU
*cpu
= CRIS_CPU(cs
);
48 CPUCRISState
*env
= &cpu
->env
;
51 D_LOG("%s pc=%x tpc=%x ra=%p\n", __func__
,
52 env
->pc
, env
->pregs
[PR_EDA
], (void *)retaddr
);
53 ret
= cris_cpu_handle_mmu_fault(cs
, addr
, access_type
, mmu_idx
);
56 /* now we have a real cpu fault */
57 if (cpu_restore_state(cs
, retaddr
)) {
58 /* Evaluate flags after retranslation. */
59 helper_top_evaluate_flags(env
);
68 void helper_raise_exception(CPUCRISState
*env
, uint32_t index
)
70 CPUState
*cs
= CPU(cris_env_get_cpu(env
));
72 cs
->exception_index
= index
;
76 void helper_tlb_flush_pid(CPUCRISState
*env
, uint32_t pid
)
78 #if !defined(CONFIG_USER_ONLY)
80 if (pid
!= (env
->pregs
[PR_PID
] & 0xff))
81 cris_mmu_flush_pid(env
, env
->pregs
[PR_PID
]);
85 void helper_spc_write(CPUCRISState
*env
, uint32_t new_spc
)
87 #if !defined(CONFIG_USER_ONLY)
88 CRISCPU
*cpu
= cris_env_get_cpu(env
);
89 CPUState
*cs
= CPU(cpu
);
91 tlb_flush_page(cs
, env
->pregs
[PR_SPC
]);
92 tlb_flush_page(cs
, new_spc
);
96 /* Used by the tlb decoder. */
97 #define EXTRACT_FIELD(src, start, end) \
98 (((src) >> start) & ((1 << (end - start + 1)) - 1))
100 void helper_movl_sreg_reg(CPUCRISState
*env
, uint32_t sreg
, uint32_t reg
)
102 #if !defined(CONFIG_USER_ONLY)
103 CRISCPU
*cpu
= cris_env_get_cpu(env
);
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
] = env
->regs
[reg
];
116 env
->sregs
[SFR_R_MM_CAUSE
] = env
->regs
[reg
];
118 else if (sreg
== 5) {
125 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
130 /* We've just made a write to tlb_lo. */
131 lo
= env
->sregs
[SFR_RW_MM_TLB_LO
];
132 /* Writes are done via r_mm_cause. */
133 hi
= env
->sregs
[SFR_R_MM_CAUSE
];
135 vaddr
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].hi
,
137 vaddr
<<= TARGET_PAGE_BITS
;
138 tlb_v
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].lo
,
140 env
->tlbsets
[srs
- 1][set
][idx
].lo
= lo
;
141 env
->tlbsets
[srs
- 1][set
][idx
].hi
= hi
;
143 D_LOG("tlb flush vaddr=%x v=%d pc=%x\n",
144 vaddr
, tlb_v
, env
->pc
);
146 tlb_flush_page(CPU(cpu
), vaddr
);
153 void helper_movl_reg_sreg(CPUCRISState
*env
, uint32_t reg
, uint32_t sreg
)
156 env
->pregs
[PR_SRS
] &= 3;
157 srs
= env
->pregs
[PR_SRS
];
159 #if !defined(CONFIG_USER_ONLY)
160 if (srs
== 1 || srs
== 2)
166 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
171 /* Update the mirror regs. */
172 hi
= env
->tlbsets
[srs
- 1][set
][idx
].hi
;
173 lo
= env
->tlbsets
[srs
- 1][set
][idx
].lo
;
174 env
->sregs
[SFR_RW_MM_TLB_HI
] = hi
;
175 env
->sregs
[SFR_RW_MM_TLB_LO
] = lo
;
178 env
->regs
[reg
] = env
->sregs
[srs
][sreg
];
181 static void cris_ccs_rshift(CPUCRISState
*env
)
185 /* Apply the ccs shift. */
186 ccs
= env
->pregs
[PR_CCS
];
187 ccs
= (ccs
& 0xc0000000) | ((ccs
& 0x0fffffff) >> 10);
190 /* Enter user mode. */
191 env
->ksp
= env
->regs
[R_SP
];
192 env
->regs
[R_SP
] = env
->pregs
[PR_USP
];
195 env
->pregs
[PR_CCS
] = ccs
;
198 void helper_rfe(CPUCRISState
*env
)
200 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
202 D_LOG("rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
203 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
207 cris_ccs_rshift(env
);
209 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
211 env
->pregs
[PR_CCS
] |= P_FLAG
;
214 void helper_rfn(CPUCRISState
*env
)
216 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
218 D_LOG("rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
219 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
223 cris_ccs_rshift(env
);
225 /* Set the P_FLAG only if the R_FLAG is not set. */
227 env
->pregs
[PR_CCS
] |= P_FLAG
;
229 /* Always set the M flag. */
230 env
->pregs
[PR_CCS
] |= M_FLAG_V32
;
233 uint32_t helper_lz(uint32_t t0
)
238 uint32_t helper_btst(CPUCRISState
*env
, uint32_t t0
, uint32_t t1
, uint32_t ccs
)
240 /* FIXME: clean this up. */
243 The N flag is set according to the selected bit in the dest reg.
244 The Z flag is set if the selected bit and all bits to the right are
246 The X flag is cleared.
247 Other flags are left untouched.
248 The destination reg is not affected.*/
249 unsigned int fz
, sbit
, bset
, mask
, masked_t0
;
252 bset
= !!(t0
& (1 << sbit
));
253 mask
= sbit
== 31 ? -1 : (1 << (sbit
+ 1)) - 1;
254 masked_t0
= t0
& mask
;
255 fz
= !(masked_t0
| bset
);
257 /* Clear the X, N and Z flags. */
258 ccs
= ccs
& ~(X_FLAG
| N_FLAG
| Z_FLAG
);
259 if (env
->pregs
[PR_VR
] < 32)
260 ccs
&= ~(V_FLAG
| C_FLAG
);
261 /* Set the N and Z flags accordingly. */
262 ccs
|= (bset
<< 3) | (fz
<< 2);
266 static inline uint32_t evaluate_flags_writeback(CPUCRISState
*env
,
267 uint32_t flags
, uint32_t ccs
)
269 unsigned int x
, z
, mask
;
271 /* Extended arithmetics, leave the z flag alone. */
273 mask
= env
->cc_mask
| X_FLAG
;
280 /* all insn clear the x-flag except setf or clrf. */
286 uint32_t helper_evaluate_flags_muls(CPUCRISState
*env
,
287 uint32_t ccs
, uint32_t res
, uint32_t mof
)
293 dneg
= ((int32_t)res
) < 0;
302 if ((dneg
&& mof
!= -1)
303 || (!dneg
&& mof
!= 0))
305 return evaluate_flags_writeback(env
, flags
, ccs
);
308 uint32_t helper_evaluate_flags_mulu(CPUCRISState
*env
,
309 uint32_t ccs
, uint32_t res
, uint32_t mof
)
324 return evaluate_flags_writeback(env
, flags
, ccs
);
327 uint32_t helper_evaluate_flags_mcp(CPUCRISState
*env
, uint32_t ccs
,
328 uint32_t src
, uint32_t dst
, uint32_t res
)
332 src
= src
& 0x80000000;
333 dst
= dst
& 0x80000000;
335 if ((res
& 0x80000000L
) != 0L)
353 return evaluate_flags_writeback(env
, flags
, ccs
);
356 uint32_t helper_evaluate_flags_alu_4(CPUCRISState
*env
, uint32_t ccs
,
357 uint32_t src
, uint32_t dst
, uint32_t res
)
361 src
= src
& 0x80000000;
362 dst
= dst
& 0x80000000;
364 if ((res
& 0x80000000L
) != 0L)
382 return evaluate_flags_writeback(env
, flags
, ccs
);
385 uint32_t helper_evaluate_flags_sub_4(CPUCRISState
*env
, uint32_t ccs
,
386 uint32_t src
, uint32_t dst
, uint32_t res
)
390 src
= (~src
) & 0x80000000;
391 dst
= dst
& 0x80000000;
393 if ((res
& 0x80000000L
) != 0L)
412 return evaluate_flags_writeback(env
, flags
, ccs
);
415 uint32_t helper_evaluate_flags_move_4(CPUCRISState
*env
,
416 uint32_t ccs
, uint32_t res
)
420 if ((int32_t)res
< 0)
425 return evaluate_flags_writeback(env
, flags
, ccs
);
427 uint32_t helper_evaluate_flags_move_2(CPUCRISState
*env
,
428 uint32_t ccs
, uint32_t res
)
432 if ((int16_t)res
< 0L)
437 return evaluate_flags_writeback(env
, flags
, ccs
);
440 /* TODO: This is expensive. We could split things up and only evaluate part of
441 CCR on a need to know basis. For now, we simply re-evaluate everything. */
442 void helper_evaluate_flags(CPUCRISState
*env
)
444 uint32_t src
, dst
, res
;
449 res
= env
->cc_result
;
451 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
454 /* Now, evaluate the flags. This stuff is based on
455 Per Zander's CRISv10 simulator. */
456 switch (env
->cc_size
)
459 if ((res
& 0x80L
) != 0L)
462 if (((src
& 0x80L
) == 0L)
463 && ((dst
& 0x80L
) == 0L))
467 else if (((src
& 0x80L
) != 0L)
468 && ((dst
& 0x80L
) != 0L))
475 if ((res
& 0xFFL
) == 0L)
479 if (((src
& 0x80L
) != 0L)
480 && ((dst
& 0x80L
) != 0L))
484 if ((dst
& 0x80L
) != 0L
485 || (src
& 0x80L
) != 0L)
492 if ((res
& 0x8000L
) != 0L)
495 if (((src
& 0x8000L
) == 0L)
496 && ((dst
& 0x8000L
) == 0L))
500 else if (((src
& 0x8000L
) != 0L)
501 && ((dst
& 0x8000L
) != 0L))
508 if ((res
& 0xFFFFL
) == 0L)
512 if (((src
& 0x8000L
) != 0L)
513 && ((dst
& 0x8000L
) != 0L))
517 if ((dst
& 0x8000L
) != 0L
518 || (src
& 0x8000L
) != 0L)
525 if ((res
& 0x80000000L
) != 0L)
528 if (((src
& 0x80000000L
) == 0L)
529 && ((dst
& 0x80000000L
) == 0L))
533 else if (((src
& 0x80000000L
) != 0L) &&
534 ((dst
& 0x80000000L
) != 0L))
543 if (((src
& 0x80000000L
) != 0L)
544 && ((dst
& 0x80000000L
) != 0L))
546 if ((dst
& 0x80000000L
) != 0L
547 || (src
& 0x80000000L
) != 0L)
555 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
558 env
->pregs
[PR_CCS
] = evaluate_flags_writeback(env
, flags
,
562 void helper_top_evaluate_flags(CPUCRISState
*env
)
567 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mcp(env
,
568 env
->pregs
[PR_CCS
], env
->cc_src
,
569 env
->cc_dest
, env
->cc_result
);
572 env
->pregs
[PR_CCS
] = helper_evaluate_flags_muls(env
,
573 env
->pregs
[PR_CCS
], env
->cc_result
,
577 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mulu(env
,
578 env
->pregs
[PR_CCS
], env
->cc_result
,
588 switch (env
->cc_size
)
592 helper_evaluate_flags_move_4(env
,
598 helper_evaluate_flags_move_2(env
,
603 helper_evaluate_flags(env
);
612 if (env
->cc_size
== 4)
614 helper_evaluate_flags_sub_4(env
,
616 env
->cc_src
, env
->cc_dest
,
619 helper_evaluate_flags(env
);
623 switch (env
->cc_size
)
627 helper_evaluate_flags_alu_4(env
,
629 env
->cc_src
, env
->cc_dest
,
633 helper_evaluate_flags(env
);