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/>.
23 #include "exec/helper-proto.h"
24 #include "qemu/host-utils.h"
25 #include "exec/cpu_ldst.h"
27 //#define CRIS_OP_HELPER_DEBUG
30 #ifdef CRIS_OP_HELPER_DEBUG
32 #define D_LOG(...) qemu_log(__VA_ARGS__)
35 #define D_LOG(...) do { } while (0)
38 #if !defined(CONFIG_USER_ONLY)
39 /* Try to fill the TLB and return an exception if error. If retaddr is
40 NULL, it means that the function was called in C code (i.e. not
41 from generated code or from helper.c) */
42 void tlb_fill(CPUState
*cs
, target_ulong addr
, int is_write
, int mmu_idx
,
45 CRISCPU
*cpu
= CRIS_CPU(cs
);
46 CPUCRISState
*env
= &cpu
->env
;
49 D_LOG("%s pc=%x tpc=%x ra=%p\n", __func__
,
50 env
->pc
, env
->pregs
[PR_EDA
], (void *)retaddr
);
51 ret
= cris_cpu_handle_mmu_fault(cs
, addr
, is_write
, mmu_idx
);
54 /* now we have a real cpu fault */
55 if (cpu_restore_state(cs
, retaddr
)) {
56 /* Evaluate flags after retranslation. */
57 helper_top_evaluate_flags(env
);
66 void helper_raise_exception(CPUCRISState
*env
, uint32_t index
)
68 CPUState
*cs
= CPU(cris_env_get_cpu(env
));
70 cs
->exception_index
= index
;
74 void helper_tlb_flush_pid(CPUCRISState
*env
, uint32_t pid
)
76 #if !defined(CONFIG_USER_ONLY)
78 if (pid
!= (env
->pregs
[PR_PID
] & 0xff))
79 cris_mmu_flush_pid(env
, env
->pregs
[PR_PID
]);
83 void helper_spc_write(CPUCRISState
*env
, uint32_t new_spc
)
85 #if !defined(CONFIG_USER_ONLY)
86 CRISCPU
*cpu
= cris_env_get_cpu(env
);
87 CPUState
*cs
= CPU(cpu
);
89 tlb_flush_page(cs
, env
->pregs
[PR_SPC
]);
90 tlb_flush_page(cs
, new_spc
);
94 void helper_dump(uint32_t a0
, uint32_t a1
, uint32_t a2
)
96 qemu_log("%s: a0=%x a1=%x\n", __func__
, a0
, a1
);
99 /* Used by the tlb decoder. */
100 #define EXTRACT_FIELD(src, start, end) \
101 (((src) >> start) & ((1 << (end - start + 1)) - 1))
103 void helper_movl_sreg_reg(CPUCRISState
*env
, uint32_t sreg
, uint32_t reg
)
105 #if !defined(CONFIG_USER_ONLY)
106 CRISCPU
*cpu
= cris_env_get_cpu(env
);
109 srs
= env
->pregs
[PR_SRS
];
111 env
->sregs
[srs
][sreg
] = env
->regs
[reg
];
113 #if !defined(CONFIG_USER_ONLY)
114 if (srs
== 1 || srs
== 2) {
116 /* Writes to tlb-hi write to mm_cause as a side
118 env
->sregs
[SFR_RW_MM_TLB_HI
] = env
->regs
[reg
];
119 env
->sregs
[SFR_R_MM_CAUSE
] = env
->regs
[reg
];
121 else if (sreg
== 5) {
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
];
138 vaddr
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].hi
,
140 vaddr
<<= TARGET_PAGE_BITS
;
141 tlb_v
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].lo
,
143 env
->tlbsets
[srs
- 1][set
][idx
].lo
= lo
;
144 env
->tlbsets
[srs
- 1][set
][idx
].hi
= hi
;
146 D_LOG("tlb flush vaddr=%x v=%d pc=%x\n",
147 vaddr
, tlb_v
, env
->pc
);
149 tlb_flush_page(CPU(cpu
), vaddr
);
156 void helper_movl_reg_sreg(CPUCRISState
*env
, uint32_t reg
, uint32_t sreg
)
159 env
->pregs
[PR_SRS
] &= 3;
160 srs
= env
->pregs
[PR_SRS
];
162 #if !defined(CONFIG_USER_ONLY)
163 if (srs
== 1 || srs
== 2)
169 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
174 /* Update the mirror regs. */
175 hi
= env
->tlbsets
[srs
- 1][set
][idx
].hi
;
176 lo
= env
->tlbsets
[srs
- 1][set
][idx
].lo
;
177 env
->sregs
[SFR_RW_MM_TLB_HI
] = hi
;
178 env
->sregs
[SFR_RW_MM_TLB_LO
] = lo
;
181 env
->regs
[reg
] = env
->sregs
[srs
][sreg
];
184 static void cris_ccs_rshift(CPUCRISState
*env
)
188 /* Apply the ccs shift. */
189 ccs
= env
->pregs
[PR_CCS
];
190 ccs
= (ccs
& 0xc0000000) | ((ccs
& 0x0fffffff) >> 10);
193 /* Enter user mode. */
194 env
->ksp
= env
->regs
[R_SP
];
195 env
->regs
[R_SP
] = env
->pregs
[PR_USP
];
198 env
->pregs
[PR_CCS
] = ccs
;
201 void helper_rfe(CPUCRISState
*env
)
203 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
205 D_LOG("rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
206 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
210 cris_ccs_rshift(env
);
212 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
214 env
->pregs
[PR_CCS
] |= P_FLAG
;
217 void helper_rfn(CPUCRISState
*env
)
219 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
221 D_LOG("rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
222 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
226 cris_ccs_rshift(env
);
228 /* Set the P_FLAG only if the R_FLAG is not set. */
230 env
->pregs
[PR_CCS
] |= P_FLAG
;
232 /* Always set the M flag. */
233 env
->pregs
[PR_CCS
] |= M_FLAG_V32
;
236 uint32_t helper_lz(uint32_t t0
)
241 uint32_t helper_btst(CPUCRISState
*env
, uint32_t t0
, uint32_t t1
, uint32_t ccs
)
243 /* FIXME: clean this up. */
246 The N flag is set according to the selected bit in the dest reg.
247 The Z flag is set if the selected bit and all bits to the right are
249 The X flag is cleared.
250 Other flags are left untouched.
251 The destination reg is not affected.*/
252 unsigned int fz
, sbit
, bset
, mask
, masked_t0
;
255 bset
= !!(t0
& (1 << sbit
));
256 mask
= sbit
== 31 ? -1 : (1 << (sbit
+ 1)) - 1;
257 masked_t0
= t0
& mask
;
258 fz
= !(masked_t0
| bset
);
260 /* Clear the X, N and Z flags. */
261 ccs
= ccs
& ~(X_FLAG
| N_FLAG
| Z_FLAG
);
262 if (env
->pregs
[PR_VR
] < 32)
263 ccs
&= ~(V_FLAG
| C_FLAG
);
264 /* Set the N and Z flags accordingly. */
265 ccs
|= (bset
<< 3) | (fz
<< 2);
269 static inline uint32_t evaluate_flags_writeback(CPUCRISState
*env
,
270 uint32_t flags
, uint32_t ccs
)
272 unsigned int x
, z
, mask
;
274 /* Extended arithmetics, leave the z flag alone. */
276 mask
= env
->cc_mask
| X_FLAG
;
283 /* all insn clear the x-flag except setf or clrf. */
289 uint32_t helper_evaluate_flags_muls(CPUCRISState
*env
,
290 uint32_t ccs
, uint32_t res
, uint32_t mof
)
296 dneg
= ((int32_t)res
) < 0;
305 if ((dneg
&& mof
!= -1)
306 || (!dneg
&& mof
!= 0))
308 return evaluate_flags_writeback(env
, flags
, ccs
);
311 uint32_t helper_evaluate_flags_mulu(CPUCRISState
*env
,
312 uint32_t ccs
, uint32_t res
, uint32_t mof
)
327 return evaluate_flags_writeback(env
, flags
, ccs
);
330 uint32_t helper_evaluate_flags_mcp(CPUCRISState
*env
, uint32_t ccs
,
331 uint32_t src
, uint32_t dst
, uint32_t res
)
335 src
= src
& 0x80000000;
336 dst
= dst
& 0x80000000;
338 if ((res
& 0x80000000L
) != 0L)
356 return evaluate_flags_writeback(env
, flags
, ccs
);
359 uint32_t helper_evaluate_flags_alu_4(CPUCRISState
*env
, uint32_t ccs
,
360 uint32_t src
, uint32_t dst
, uint32_t res
)
364 src
= src
& 0x80000000;
365 dst
= dst
& 0x80000000;
367 if ((res
& 0x80000000L
) != 0L)
385 return evaluate_flags_writeback(env
, flags
, ccs
);
388 uint32_t helper_evaluate_flags_sub_4(CPUCRISState
*env
, uint32_t ccs
,
389 uint32_t src
, uint32_t dst
, uint32_t res
)
393 src
= (~src
) & 0x80000000;
394 dst
= dst
& 0x80000000;
396 if ((res
& 0x80000000L
) != 0L)
415 return evaluate_flags_writeback(env
, flags
, ccs
);
418 uint32_t helper_evaluate_flags_move_4(CPUCRISState
*env
,
419 uint32_t ccs
, uint32_t res
)
423 if ((int32_t)res
< 0)
428 return evaluate_flags_writeback(env
, flags
, ccs
);
430 uint32_t helper_evaluate_flags_move_2(CPUCRISState
*env
,
431 uint32_t ccs
, uint32_t res
)
435 if ((int16_t)res
< 0L)
440 return evaluate_flags_writeback(env
, flags
, ccs
);
443 /* TODO: This is expensive. We could split things up and only evaluate part of
444 CCR on a need to know basis. For now, we simply re-evaluate everything. */
445 void helper_evaluate_flags(CPUCRISState
*env
)
447 uint32_t src
, dst
, res
;
452 res
= env
->cc_result
;
454 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
457 /* Now, evaluate the flags. This stuff is based on
458 Per Zander's CRISv10 simulator. */
459 switch (env
->cc_size
)
462 if ((res
& 0x80L
) != 0L)
465 if (((src
& 0x80L
) == 0L)
466 && ((dst
& 0x80L
) == 0L))
470 else if (((src
& 0x80L
) != 0L)
471 && ((dst
& 0x80L
) != 0L))
478 if ((res
& 0xFFL
) == 0L)
482 if (((src
& 0x80L
) != 0L)
483 && ((dst
& 0x80L
) != 0L))
487 if ((dst
& 0x80L
) != 0L
488 || (src
& 0x80L
) != 0L)
495 if ((res
& 0x8000L
) != 0L)
498 if (((src
& 0x8000L
) == 0L)
499 && ((dst
& 0x8000L
) == 0L))
503 else if (((src
& 0x8000L
) != 0L)
504 && ((dst
& 0x8000L
) != 0L))
511 if ((res
& 0xFFFFL
) == 0L)
515 if (((src
& 0x8000L
) != 0L)
516 && ((dst
& 0x8000L
) != 0L))
520 if ((dst
& 0x8000L
) != 0L
521 || (src
& 0x8000L
) != 0L)
528 if ((res
& 0x80000000L
) != 0L)
531 if (((src
& 0x80000000L
) == 0L)
532 && ((dst
& 0x80000000L
) == 0L))
536 else if (((src
& 0x80000000L
) != 0L) &&
537 ((dst
& 0x80000000L
) != 0L))
546 if (((src
& 0x80000000L
) != 0L)
547 && ((dst
& 0x80000000L
) != 0L))
549 if ((dst
& 0x80000000L
) != 0L
550 || (src
& 0x80000000L
) != 0L)
558 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
561 env
->pregs
[PR_CCS
] = evaluate_flags_writeback(env
, flags
,
565 void helper_top_evaluate_flags(CPUCRISState
*env
)
570 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mcp(env
,
571 env
->pregs
[PR_CCS
], env
->cc_src
,
572 env
->cc_dest
, env
->cc_result
);
575 env
->pregs
[PR_CCS
] = helper_evaluate_flags_muls(env
,
576 env
->pregs
[PR_CCS
], env
->cc_result
,
580 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mulu(env
,
581 env
->pregs
[PR_CCS
], env
->cc_result
,
591 switch (env
->cc_size
)
595 helper_evaluate_flags_move_4(env
,
601 helper_evaluate_flags_move_2(env
,
606 helper_evaluate_flags(env
);
615 if (env
->cc_size
== 4)
617 helper_evaluate_flags_sub_4(env
,
619 env
->cc_src
, env
->cc_dest
,
622 helper_evaluate_flags(env
);
626 switch (env
->cc_size
)
630 helper_evaluate_flags_alu_4(env
,
632 env
->cc_src
, env
->cc_dest
,
636 helper_evaluate_flags(env
);