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/>.
24 #include "qemu/host-utils.h"
26 //#define CRIS_OP_HELPER_DEBUG
29 #ifdef CRIS_OP_HELPER_DEBUG
31 #define D_LOG(...) qemu_log(__VA_ARGS__)
34 #define D_LOG(...) do { } while (0)
37 #if !defined(CONFIG_USER_ONLY)
38 #include "exec/softmmu_exec.h"
40 #define MMUSUFFIX _mmu
43 #include "exec/softmmu_template.h"
46 #include "exec/softmmu_template.h"
49 #include "exec/softmmu_template.h"
52 #include "exec/softmmu_template.h"
54 /* Try to fill the TLB and return an exception if error. If retaddr is
55 NULL, it means that the function was called in C code (i.e. not
56 from generated code or from helper.c) */
57 void tlb_fill(CPUState
*cs
, target_ulong addr
, int is_write
, int mmu_idx
,
60 CRISCPU
*cpu
= CRIS_CPU(cs
);
61 CPUCRISState
*env
= &cpu
->env
;
64 D_LOG("%s pc=%x tpc=%x ra=%p\n", __func__
,
65 env
->pc
, env
->pregs
[PR_EDA
], (void *)retaddr
);
66 ret
= cris_cpu_handle_mmu_fault(cs
, addr
, is_write
, mmu_idx
);
69 /* now we have a real cpu fault */
70 if (cpu_restore_state(cs
, retaddr
)) {
71 /* Evaluate flags after retranslation. */
72 helper_top_evaluate_flags(env
);
81 void helper_raise_exception(CPUCRISState
*env
, uint32_t index
)
83 CPUState
*cs
= CPU(cris_env_get_cpu(env
));
85 cs
->exception_index
= index
;
89 void helper_tlb_flush_pid(CPUCRISState
*env
, uint32_t pid
)
91 #if !defined(CONFIG_USER_ONLY)
93 if (pid
!= (env
->pregs
[PR_PID
] & 0xff))
94 cris_mmu_flush_pid(env
, env
->pregs
[PR_PID
]);
98 void helper_spc_write(CPUCRISState
*env
, uint32_t new_spc
)
100 #if !defined(CONFIG_USER_ONLY)
101 CRISCPU
*cpu
= cris_env_get_cpu(env
);
102 CPUState
*cs
= CPU(cpu
);
104 tlb_flush_page(cs
, env
->pregs
[PR_SPC
]);
105 tlb_flush_page(cs
, new_spc
);
109 void helper_dump(uint32_t a0
, uint32_t a1
, uint32_t a2
)
111 qemu_log("%s: a0=%x a1=%x\n", __func__
, a0
, a1
);
114 /* Used by the tlb decoder. */
115 #define EXTRACT_FIELD(src, start, end) \
116 (((src) >> start) & ((1 << (end - start + 1)) - 1))
118 void helper_movl_sreg_reg(CPUCRISState
*env
, uint32_t sreg
, uint32_t reg
)
120 #if !defined(CONFIG_USER_ONLY)
121 CRISCPU
*cpu
= cris_env_get_cpu(env
);
124 srs
= env
->pregs
[PR_SRS
];
126 env
->sregs
[srs
][sreg
] = env
->regs
[reg
];
128 #if !defined(CONFIG_USER_ONLY)
129 if (srs
== 1 || srs
== 2) {
131 /* Writes to tlb-hi write to mm_cause as a side
133 env
->sregs
[SFR_RW_MM_TLB_HI
] = env
->regs
[reg
];
134 env
->sregs
[SFR_R_MM_CAUSE
] = env
->regs
[reg
];
136 else if (sreg
== 5) {
143 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
148 /* We've just made a write to tlb_lo. */
149 lo
= env
->sregs
[SFR_RW_MM_TLB_LO
];
150 /* Writes are done via r_mm_cause. */
151 hi
= env
->sregs
[SFR_R_MM_CAUSE
];
153 vaddr
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].hi
,
155 vaddr
<<= TARGET_PAGE_BITS
;
156 tlb_v
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].lo
,
158 env
->tlbsets
[srs
- 1][set
][idx
].lo
= lo
;
159 env
->tlbsets
[srs
- 1][set
][idx
].hi
= hi
;
161 D_LOG("tlb flush vaddr=%x v=%d pc=%x\n",
162 vaddr
, tlb_v
, env
->pc
);
164 tlb_flush_page(CPU(cpu
), vaddr
);
171 void helper_movl_reg_sreg(CPUCRISState
*env
, uint32_t reg
, uint32_t sreg
)
174 env
->pregs
[PR_SRS
] &= 3;
175 srs
= env
->pregs
[PR_SRS
];
177 #if !defined(CONFIG_USER_ONLY)
178 if (srs
== 1 || srs
== 2)
184 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
189 /* Update the mirror regs. */
190 hi
= env
->tlbsets
[srs
- 1][set
][idx
].hi
;
191 lo
= env
->tlbsets
[srs
- 1][set
][idx
].lo
;
192 env
->sregs
[SFR_RW_MM_TLB_HI
] = hi
;
193 env
->sregs
[SFR_RW_MM_TLB_LO
] = lo
;
196 env
->regs
[reg
] = env
->sregs
[srs
][sreg
];
199 static void cris_ccs_rshift(CPUCRISState
*env
)
203 /* Apply the ccs shift. */
204 ccs
= env
->pregs
[PR_CCS
];
205 ccs
= (ccs
& 0xc0000000) | ((ccs
& 0x0fffffff) >> 10);
208 /* Enter user mode. */
209 env
->ksp
= env
->regs
[R_SP
];
210 env
->regs
[R_SP
] = env
->pregs
[PR_USP
];
213 env
->pregs
[PR_CCS
] = ccs
;
216 void helper_rfe(CPUCRISState
*env
)
218 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
220 D_LOG("rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
221 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
225 cris_ccs_rshift(env
);
227 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
229 env
->pregs
[PR_CCS
] |= P_FLAG
;
232 void helper_rfn(CPUCRISState
*env
)
234 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
236 D_LOG("rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
237 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
241 cris_ccs_rshift(env
);
243 /* Set the P_FLAG only if the R_FLAG is not set. */
245 env
->pregs
[PR_CCS
] |= P_FLAG
;
247 /* Always set the M flag. */
248 env
->pregs
[PR_CCS
] |= M_FLAG_V32
;
251 uint32_t helper_lz(uint32_t t0
)
256 uint32_t helper_btst(CPUCRISState
*env
, uint32_t t0
, uint32_t t1
, uint32_t ccs
)
258 /* FIXME: clean this up. */
261 The N flag is set according to the selected bit in the dest reg.
262 The Z flag is set if the selected bit and all bits to the right are
264 The X flag is cleared.
265 Other flags are left untouched.
266 The destination reg is not affected.*/
267 unsigned int fz
, sbit
, bset
, mask
, masked_t0
;
270 bset
= !!(t0
& (1 << sbit
));
271 mask
= sbit
== 31 ? -1 : (1 << (sbit
+ 1)) - 1;
272 masked_t0
= t0
& mask
;
273 fz
= !(masked_t0
| bset
);
275 /* Clear the X, N and Z flags. */
276 ccs
= ccs
& ~(X_FLAG
| N_FLAG
| Z_FLAG
);
277 if (env
->pregs
[PR_VR
] < 32)
278 ccs
&= ~(V_FLAG
| C_FLAG
);
279 /* Set the N and Z flags accordingly. */
280 ccs
|= (bset
<< 3) | (fz
<< 2);
284 static inline uint32_t evaluate_flags_writeback(CPUCRISState
*env
,
285 uint32_t flags
, uint32_t ccs
)
287 unsigned int x
, z
, mask
;
289 /* Extended arithmetics, leave the z flag alone. */
291 mask
= env
->cc_mask
| X_FLAG
;
298 /* all insn clear the x-flag except setf or clrf. */
304 uint32_t helper_evaluate_flags_muls(CPUCRISState
*env
,
305 uint32_t ccs
, uint32_t res
, uint32_t mof
)
311 dneg
= ((int32_t)res
) < 0;
320 if ((dneg
&& mof
!= -1)
321 || (!dneg
&& mof
!= 0))
323 return evaluate_flags_writeback(env
, flags
, ccs
);
326 uint32_t helper_evaluate_flags_mulu(CPUCRISState
*env
,
327 uint32_t ccs
, uint32_t res
, uint32_t mof
)
342 return evaluate_flags_writeback(env
, flags
, ccs
);
345 uint32_t helper_evaluate_flags_mcp(CPUCRISState
*env
, uint32_t ccs
,
346 uint32_t src
, uint32_t dst
, uint32_t res
)
350 src
= src
& 0x80000000;
351 dst
= dst
& 0x80000000;
353 if ((res
& 0x80000000L
) != 0L)
371 return evaluate_flags_writeback(env
, flags
, ccs
);
374 uint32_t helper_evaluate_flags_alu_4(CPUCRISState
*env
, uint32_t ccs
,
375 uint32_t src
, uint32_t dst
, uint32_t res
)
379 src
= src
& 0x80000000;
380 dst
= dst
& 0x80000000;
382 if ((res
& 0x80000000L
) != 0L)
400 return evaluate_flags_writeback(env
, flags
, ccs
);
403 uint32_t helper_evaluate_flags_sub_4(CPUCRISState
*env
, uint32_t ccs
,
404 uint32_t src
, uint32_t dst
, uint32_t res
)
408 src
= (~src
) & 0x80000000;
409 dst
= dst
& 0x80000000;
411 if ((res
& 0x80000000L
) != 0L)
430 return evaluate_flags_writeback(env
, flags
, ccs
);
433 uint32_t helper_evaluate_flags_move_4(CPUCRISState
*env
,
434 uint32_t ccs
, uint32_t res
)
438 if ((int32_t)res
< 0)
443 return evaluate_flags_writeback(env
, flags
, ccs
);
445 uint32_t helper_evaluate_flags_move_2(CPUCRISState
*env
,
446 uint32_t ccs
, uint32_t res
)
450 if ((int16_t)res
< 0L)
455 return evaluate_flags_writeback(env
, flags
, ccs
);
458 /* TODO: This is expensive. We could split things up and only evaluate part of
459 CCR on a need to know basis. For now, we simply re-evaluate everything. */
460 void helper_evaluate_flags(CPUCRISState
*env
)
462 uint32_t src
, dst
, res
;
467 res
= env
->cc_result
;
469 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
472 /* Now, evaluate the flags. This stuff is based on
473 Per Zander's CRISv10 simulator. */
474 switch (env
->cc_size
)
477 if ((res
& 0x80L
) != 0L)
480 if (((src
& 0x80L
) == 0L)
481 && ((dst
& 0x80L
) == 0L))
485 else if (((src
& 0x80L
) != 0L)
486 && ((dst
& 0x80L
) != 0L))
493 if ((res
& 0xFFL
) == 0L)
497 if (((src
& 0x80L
) != 0L)
498 && ((dst
& 0x80L
) != 0L))
502 if ((dst
& 0x80L
) != 0L
503 || (src
& 0x80L
) != 0L)
510 if ((res
& 0x8000L
) != 0L)
513 if (((src
& 0x8000L
) == 0L)
514 && ((dst
& 0x8000L
) == 0L))
518 else if (((src
& 0x8000L
) != 0L)
519 && ((dst
& 0x8000L
) != 0L))
526 if ((res
& 0xFFFFL
) == 0L)
530 if (((src
& 0x8000L
) != 0L)
531 && ((dst
& 0x8000L
) != 0L))
535 if ((dst
& 0x8000L
) != 0L
536 || (src
& 0x8000L
) != 0L)
543 if ((res
& 0x80000000L
) != 0L)
546 if (((src
& 0x80000000L
) == 0L)
547 && ((dst
& 0x80000000L
) == 0L))
551 else if (((src
& 0x80000000L
) != 0L) &&
552 ((dst
& 0x80000000L
) != 0L))
561 if (((src
& 0x80000000L
) != 0L)
562 && ((dst
& 0x80000000L
) != 0L))
564 if ((dst
& 0x80000000L
) != 0L
565 || (src
& 0x80000000L
) != 0L)
573 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
576 env
->pregs
[PR_CCS
] = evaluate_flags_writeback(env
, flags
,
580 void helper_top_evaluate_flags(CPUCRISState
*env
)
585 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mcp(env
,
586 env
->pregs
[PR_CCS
], env
->cc_src
,
587 env
->cc_dest
, env
->cc_result
);
590 env
->pregs
[PR_CCS
] = helper_evaluate_flags_muls(env
,
591 env
->pregs
[PR_CCS
], env
->cc_result
,
595 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mulu(env
,
596 env
->pregs
[PR_CCS
], env
->cc_result
,
606 switch (env
->cc_size
)
610 helper_evaluate_flags_move_4(env
,
616 helper_evaluate_flags_move_2(env
,
621 helper_evaluate_flags(env
);
630 if (env
->cc_size
== 4)
632 helper_evaluate_flags_sub_4(env
,
634 env
->cc_src
, env
->cc_dest
,
637 helper_evaluate_flags(env
);
641 switch (env
->cc_size
)
645 helper_evaluate_flags_alu_4(env
,
647 env
->cc_src
, env
->cc_dest
,
651 helper_evaluate_flags(env
);