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
, int size
,
45 MMUAccessType access_type
, 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
, size
, access_type
, mmu_idx
);
56 /* now we have a real cpu fault */
57 if (cpu_restore_state(cs
, retaddr
, true)) {
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_btst(CPUCRISState
*env
, uint32_t t0
, uint32_t t1
, uint32_t ccs
)
235 /* FIXME: clean this up. */
238 The N flag is set according to the selected bit in the dest reg.
239 The Z flag is set if the selected bit and all bits to the right are
241 The X flag is cleared.
242 Other flags are left untouched.
243 The destination reg is not affected.*/
244 unsigned int fz
, sbit
, bset
, mask
, masked_t0
;
247 bset
= !!(t0
& (1 << sbit
));
248 mask
= sbit
== 31 ? -1 : (1 << (sbit
+ 1)) - 1;
249 masked_t0
= t0
& mask
;
250 fz
= !(masked_t0
| bset
);
252 /* Clear the X, N and Z flags. */
253 ccs
= ccs
& ~(X_FLAG
| N_FLAG
| Z_FLAG
);
254 if (env
->pregs
[PR_VR
] < 32)
255 ccs
&= ~(V_FLAG
| C_FLAG
);
256 /* Set the N and Z flags accordingly. */
257 ccs
|= (bset
<< 3) | (fz
<< 2);
261 static inline uint32_t evaluate_flags_writeback(CPUCRISState
*env
,
262 uint32_t flags
, uint32_t ccs
)
264 unsigned int x
, z
, mask
;
266 /* Extended arithmetics, leave the z flag alone. */
268 mask
= env
->cc_mask
| X_FLAG
;
275 /* all insn clear the x-flag except setf or clrf. */
281 uint32_t helper_evaluate_flags_muls(CPUCRISState
*env
,
282 uint32_t ccs
, uint32_t res
, uint32_t mof
)
288 dneg
= ((int32_t)res
) < 0;
297 if ((dneg
&& mof
!= -1)
298 || (!dneg
&& mof
!= 0))
300 return evaluate_flags_writeback(env
, flags
, ccs
);
303 uint32_t helper_evaluate_flags_mulu(CPUCRISState
*env
,
304 uint32_t ccs
, uint32_t res
, uint32_t mof
)
319 return evaluate_flags_writeback(env
, flags
, ccs
);
322 uint32_t helper_evaluate_flags_mcp(CPUCRISState
*env
, uint32_t ccs
,
323 uint32_t src
, uint32_t dst
, uint32_t res
)
327 src
= src
& 0x80000000;
328 dst
= dst
& 0x80000000;
330 if ((res
& 0x80000000L
) != 0L)
348 return evaluate_flags_writeback(env
, flags
, ccs
);
351 uint32_t helper_evaluate_flags_alu_4(CPUCRISState
*env
, uint32_t ccs
,
352 uint32_t src
, uint32_t dst
, uint32_t res
)
356 src
= src
& 0x80000000;
357 dst
= dst
& 0x80000000;
359 if ((res
& 0x80000000L
) != 0L)
377 return evaluate_flags_writeback(env
, flags
, ccs
);
380 uint32_t helper_evaluate_flags_sub_4(CPUCRISState
*env
, uint32_t ccs
,
381 uint32_t src
, uint32_t dst
, uint32_t res
)
385 src
= (~src
) & 0x80000000;
386 dst
= dst
& 0x80000000;
388 if ((res
& 0x80000000L
) != 0L)
407 return evaluate_flags_writeback(env
, flags
, ccs
);
410 uint32_t helper_evaluate_flags_move_4(CPUCRISState
*env
,
411 uint32_t ccs
, uint32_t res
)
415 if ((int32_t)res
< 0)
420 return evaluate_flags_writeback(env
, flags
, ccs
);
422 uint32_t helper_evaluate_flags_move_2(CPUCRISState
*env
,
423 uint32_t ccs
, uint32_t res
)
427 if ((int16_t)res
< 0L)
432 return evaluate_flags_writeback(env
, flags
, ccs
);
435 /* TODO: This is expensive. We could split things up and only evaluate part of
436 CCR on a need to know basis. For now, we simply re-evaluate everything. */
437 void helper_evaluate_flags(CPUCRISState
*env
)
439 uint32_t src
, dst
, res
;
444 res
= env
->cc_result
;
446 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
449 /* Now, evaluate the flags. This stuff is based on
450 Per Zander's CRISv10 simulator. */
451 switch (env
->cc_size
)
454 if ((res
& 0x80L
) != 0L)
457 if (((src
& 0x80L
) == 0L)
458 && ((dst
& 0x80L
) == 0L))
462 else if (((src
& 0x80L
) != 0L)
463 && ((dst
& 0x80L
) != 0L))
470 if ((res
& 0xFFL
) == 0L)
474 if (((src
& 0x80L
) != 0L)
475 && ((dst
& 0x80L
) != 0L))
479 if ((dst
& 0x80L
) != 0L
480 || (src
& 0x80L
) != 0L)
487 if ((res
& 0x8000L
) != 0L)
490 if (((src
& 0x8000L
) == 0L)
491 && ((dst
& 0x8000L
) == 0L))
495 else if (((src
& 0x8000L
) != 0L)
496 && ((dst
& 0x8000L
) != 0L))
503 if ((res
& 0xFFFFL
) == 0L)
507 if (((src
& 0x8000L
) != 0L)
508 && ((dst
& 0x8000L
) != 0L))
512 if ((dst
& 0x8000L
) != 0L
513 || (src
& 0x8000L
) != 0L)
520 if ((res
& 0x80000000L
) != 0L)
523 if (((src
& 0x80000000L
) == 0L)
524 && ((dst
& 0x80000000L
) == 0L))
528 else if (((src
& 0x80000000L
) != 0L) &&
529 ((dst
& 0x80000000L
) != 0L))
538 if (((src
& 0x80000000L
) != 0L)
539 && ((dst
& 0x80000000L
) != 0L))
541 if ((dst
& 0x80000000L
) != 0L
542 || (src
& 0x80000000L
) != 0L)
550 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
553 env
->pregs
[PR_CCS
] = evaluate_flags_writeback(env
, flags
,
557 void helper_top_evaluate_flags(CPUCRISState
*env
)
562 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mcp(env
,
563 env
->pregs
[PR_CCS
], env
->cc_src
,
564 env
->cc_dest
, env
->cc_result
);
567 env
->pregs
[PR_CCS
] = helper_evaluate_flags_muls(env
,
568 env
->pregs
[PR_CCS
], env
->cc_result
,
572 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mulu(env
,
573 env
->pregs
[PR_CCS
], env
->cc_result
,
583 switch (env
->cc_size
)
587 helper_evaluate_flags_move_4(env
,
593 helper_evaluate_flags_move_2(env
,
598 helper_evaluate_flags(env
);
607 if (env
->cc_size
== 4)
609 helper_evaluate_flags_sub_4(env
,
611 env
->cc_src
, env
->cc_dest
,
614 helper_evaluate_flags(env
);
618 switch (env
->cc_size
)
622 helper_evaluate_flags_alu_4(env
,
624 env
->cc_src
, env
->cc_dest
,
628 helper_evaluate_flags(env
);