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/cpu_ldst.h"
28 //#define CRIS_OP_HELPER_DEBUG
31 #ifdef CRIS_OP_HELPER_DEBUG
33 #define D_LOG(...) qemu_log(__VA_ARGS__)
36 #define D_LOG(...) do { } while (0)
39 #if !defined(CONFIG_USER_ONLY)
40 /* Try to fill the TLB and return an exception if error. If retaddr is
41 NULL, it means that the function was called in C code (i.e. not
42 from generated code or from helper.c) */
43 void tlb_fill(CPUState
*cs
, target_ulong addr
, int is_write
, int mmu_idx
,
46 CRISCPU
*cpu
= CRIS_CPU(cs
);
47 CPUCRISState
*env
= &cpu
->env
;
50 D_LOG("%s pc=%x tpc=%x ra=%p\n", __func__
,
51 env
->pc
, env
->pregs
[PR_EDA
], (void *)retaddr
);
52 ret
= cris_cpu_handle_mmu_fault(cs
, addr
, is_write
, mmu_idx
);
55 /* now we have a real cpu fault */
56 if (cpu_restore_state(cs
, retaddr
)) {
57 /* Evaluate flags after retranslation. */
58 helper_top_evaluate_flags(env
);
67 void helper_raise_exception(CPUCRISState
*env
, uint32_t index
)
69 CPUState
*cs
= CPU(cris_env_get_cpu(env
));
71 cs
->exception_index
= index
;
75 void helper_tlb_flush_pid(CPUCRISState
*env
, uint32_t pid
)
77 #if !defined(CONFIG_USER_ONLY)
79 if (pid
!= (env
->pregs
[PR_PID
] & 0xff))
80 cris_mmu_flush_pid(env
, env
->pregs
[PR_PID
]);
84 void helper_spc_write(CPUCRISState
*env
, uint32_t new_spc
)
86 #if !defined(CONFIG_USER_ONLY)
87 CRISCPU
*cpu
= cris_env_get_cpu(env
);
88 CPUState
*cs
= CPU(cpu
);
90 tlb_flush_page(cs
, env
->pregs
[PR_SPC
]);
91 tlb_flush_page(cs
, new_spc
);
95 /* Used by the tlb decoder. */
96 #define EXTRACT_FIELD(src, start, end) \
97 (((src) >> start) & ((1 << (end - start + 1)) - 1))
99 void helper_movl_sreg_reg(CPUCRISState
*env
, uint32_t sreg
, uint32_t reg
)
101 #if !defined(CONFIG_USER_ONLY)
102 CRISCPU
*cpu
= cris_env_get_cpu(env
);
105 srs
= env
->pregs
[PR_SRS
];
107 env
->sregs
[srs
][sreg
] = env
->regs
[reg
];
109 #if !defined(CONFIG_USER_ONLY)
110 if (srs
== 1 || srs
== 2) {
112 /* Writes to tlb-hi write to mm_cause as a side
114 env
->sregs
[SFR_RW_MM_TLB_HI
] = env
->regs
[reg
];
115 env
->sregs
[SFR_R_MM_CAUSE
] = env
->regs
[reg
];
117 else if (sreg
== 5) {
124 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
129 /* We've just made a write to tlb_lo. */
130 lo
= env
->sregs
[SFR_RW_MM_TLB_LO
];
131 /* Writes are done via r_mm_cause. */
132 hi
= env
->sregs
[SFR_R_MM_CAUSE
];
134 vaddr
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].hi
,
136 vaddr
<<= TARGET_PAGE_BITS
;
137 tlb_v
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].lo
,
139 env
->tlbsets
[srs
- 1][set
][idx
].lo
= lo
;
140 env
->tlbsets
[srs
- 1][set
][idx
].hi
= hi
;
142 D_LOG("tlb flush vaddr=%x v=%d pc=%x\n",
143 vaddr
, tlb_v
, env
->pc
);
145 tlb_flush_page(CPU(cpu
), vaddr
);
152 void helper_movl_reg_sreg(CPUCRISState
*env
, uint32_t reg
, uint32_t sreg
)
155 env
->pregs
[PR_SRS
] &= 3;
156 srs
= env
->pregs
[PR_SRS
];
158 #if !defined(CONFIG_USER_ONLY)
159 if (srs
== 1 || srs
== 2)
165 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
170 /* Update the mirror regs. */
171 hi
= env
->tlbsets
[srs
- 1][set
][idx
].hi
;
172 lo
= env
->tlbsets
[srs
- 1][set
][idx
].lo
;
173 env
->sregs
[SFR_RW_MM_TLB_HI
] = hi
;
174 env
->sregs
[SFR_RW_MM_TLB_LO
] = lo
;
177 env
->regs
[reg
] = env
->sregs
[srs
][sreg
];
180 static void cris_ccs_rshift(CPUCRISState
*env
)
184 /* Apply the ccs shift. */
185 ccs
= env
->pregs
[PR_CCS
];
186 ccs
= (ccs
& 0xc0000000) | ((ccs
& 0x0fffffff) >> 10);
189 /* Enter user mode. */
190 env
->ksp
= env
->regs
[R_SP
];
191 env
->regs
[R_SP
] = env
->pregs
[PR_USP
];
194 env
->pregs
[PR_CCS
] = ccs
;
197 void helper_rfe(CPUCRISState
*env
)
199 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
201 D_LOG("rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
202 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
206 cris_ccs_rshift(env
);
208 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
210 env
->pregs
[PR_CCS
] |= P_FLAG
;
213 void helper_rfn(CPUCRISState
*env
)
215 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
217 D_LOG("rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
218 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
222 cris_ccs_rshift(env
);
224 /* Set the P_FLAG only if the R_FLAG is not set. */
226 env
->pregs
[PR_CCS
] |= P_FLAG
;
228 /* Always set the M flag. */
229 env
->pregs
[PR_CCS
] |= M_FLAG_V32
;
232 uint32_t helper_lz(uint32_t t0
)
237 uint32_t helper_btst(CPUCRISState
*env
, uint32_t t0
, uint32_t t1
, uint32_t ccs
)
239 /* FIXME: clean this up. */
242 The N flag is set according to the selected bit in the dest reg.
243 The Z flag is set if the selected bit and all bits to the right are
245 The X flag is cleared.
246 Other flags are left untouched.
247 The destination reg is not affected.*/
248 unsigned int fz
, sbit
, bset
, mask
, masked_t0
;
251 bset
= !!(t0
& (1 << sbit
));
252 mask
= sbit
== 31 ? -1 : (1 << (sbit
+ 1)) - 1;
253 masked_t0
= t0
& mask
;
254 fz
= !(masked_t0
| bset
);
256 /* Clear the X, N and Z flags. */
257 ccs
= ccs
& ~(X_FLAG
| N_FLAG
| Z_FLAG
);
258 if (env
->pregs
[PR_VR
] < 32)
259 ccs
&= ~(V_FLAG
| C_FLAG
);
260 /* Set the N and Z flags accordingly. */
261 ccs
|= (bset
<< 3) | (fz
<< 2);
265 static inline uint32_t evaluate_flags_writeback(CPUCRISState
*env
,
266 uint32_t flags
, uint32_t ccs
)
268 unsigned int x
, z
, mask
;
270 /* Extended arithmetics, leave the z flag alone. */
272 mask
= env
->cc_mask
| X_FLAG
;
279 /* all insn clear the x-flag except setf or clrf. */
285 uint32_t helper_evaluate_flags_muls(CPUCRISState
*env
,
286 uint32_t ccs
, uint32_t res
, uint32_t mof
)
292 dneg
= ((int32_t)res
) < 0;
301 if ((dneg
&& mof
!= -1)
302 || (!dneg
&& mof
!= 0))
304 return evaluate_flags_writeback(env
, flags
, ccs
);
307 uint32_t helper_evaluate_flags_mulu(CPUCRISState
*env
,
308 uint32_t ccs
, uint32_t res
, uint32_t mof
)
323 return evaluate_flags_writeback(env
, flags
, ccs
);
326 uint32_t helper_evaluate_flags_mcp(CPUCRISState
*env
, uint32_t ccs
,
327 uint32_t src
, uint32_t dst
, uint32_t res
)
331 src
= src
& 0x80000000;
332 dst
= dst
& 0x80000000;
334 if ((res
& 0x80000000L
) != 0L)
352 return evaluate_flags_writeback(env
, flags
, ccs
);
355 uint32_t helper_evaluate_flags_alu_4(CPUCRISState
*env
, uint32_t ccs
,
356 uint32_t src
, uint32_t dst
, uint32_t res
)
360 src
= src
& 0x80000000;
361 dst
= dst
& 0x80000000;
363 if ((res
& 0x80000000L
) != 0L)
381 return evaluate_flags_writeback(env
, flags
, ccs
);
384 uint32_t helper_evaluate_flags_sub_4(CPUCRISState
*env
, uint32_t ccs
,
385 uint32_t src
, uint32_t dst
, uint32_t res
)
389 src
= (~src
) & 0x80000000;
390 dst
= dst
& 0x80000000;
392 if ((res
& 0x80000000L
) != 0L)
411 return evaluate_flags_writeback(env
, flags
, ccs
);
414 uint32_t helper_evaluate_flags_move_4(CPUCRISState
*env
,
415 uint32_t ccs
, uint32_t res
)
419 if ((int32_t)res
< 0)
424 return evaluate_flags_writeback(env
, flags
, ccs
);
426 uint32_t helper_evaluate_flags_move_2(CPUCRISState
*env
,
427 uint32_t ccs
, uint32_t res
)
431 if ((int16_t)res
< 0L)
436 return evaluate_flags_writeback(env
, flags
, ccs
);
439 /* TODO: This is expensive. We could split things up and only evaluate part of
440 CCR on a need to know basis. For now, we simply re-evaluate everything. */
441 void helper_evaluate_flags(CPUCRISState
*env
)
443 uint32_t src
, dst
, res
;
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
|| env
->cc_op
== CC_OP_CMP
)
557 env
->pregs
[PR_CCS
] = evaluate_flags_writeback(env
, flags
,
561 void helper_top_evaluate_flags(CPUCRISState
*env
)
566 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mcp(env
,
567 env
->pregs
[PR_CCS
], env
->cc_src
,
568 env
->cc_dest
, env
->cc_result
);
571 env
->pregs
[PR_CCS
] = helper_evaluate_flags_muls(env
,
572 env
->pregs
[PR_CCS
], env
->cc_result
,
576 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mulu(env
,
577 env
->pregs
[PR_CCS
], env
->cc_result
,
587 switch (env
->cc_size
)
591 helper_evaluate_flags_move_4(env
,
597 helper_evaluate_flags_move_2(env
,
602 helper_evaluate_flags(env
);
611 if (env
->cc_size
== 4)
613 helper_evaluate_flags_sub_4(env
,
615 env
->cc_src
, env
->cc_dest
,
618 helper_evaluate_flags(env
);
622 switch (env
->cc_size
)
626 helper_evaluate_flags_alu_4(env
,
628 env
->cc_src
, env
->cc_dest
,
632 helper_evaluate_flags(env
);