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 "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 "softmmu_exec.h"
40 #define MMUSUFFIX _mmu
43 #include "softmmu_template.h"
46 #include "softmmu_template.h"
49 #include "softmmu_template.h"
52 #include "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(CPUCRISState
*env
, target_ulong addr
, int is_write
, int mmu_idx
,
63 D_LOG("%s pc=%x tpc=%x ra=%p\n", __func__
,
64 env
->pc
, env
->debug1
, (void *)retaddr
);
65 ret
= cpu_cris_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
);
68 /* now we have a real cpu fault */
69 tb
= tb_find_pc(retaddr
);
71 /* the PC is inside the translated code. It means that we have
72 a virtual CPU fault */
73 cpu_restore_state(tb
, env
, retaddr
);
75 /* Evaluate flags after retranslation. */
76 helper_top_evaluate_flags(env
);
85 void helper_raise_exception(CPUCRISState
*env
, uint32_t index
)
87 env
->exception_index
= index
;
91 void helper_tlb_flush_pid(CPUCRISState
*env
, uint32_t pid
)
93 #if !defined(CONFIG_USER_ONLY)
95 if (pid
!= (env
->pregs
[PR_PID
] & 0xff))
96 cris_mmu_flush_pid(env
, env
->pregs
[PR_PID
]);
100 void helper_spc_write(CPUCRISState
*env
, uint32_t new_spc
)
102 #if !defined(CONFIG_USER_ONLY)
103 tlb_flush_page(env
, env
->pregs
[PR_SPC
]);
104 tlb_flush_page(env
, new_spc
);
108 void helper_dump(uint32_t a0
, uint32_t a1
, uint32_t a2
)
110 qemu_log("%s: a0=%x a1=%x\n", __func__
, a0
, a1
);
113 /* Used by the tlb decoder. */
114 #define EXTRACT_FIELD(src, start, end) \
115 (((src) >> start) & ((1 << (end - start + 1)) - 1))
117 void helper_movl_sreg_reg(CPUCRISState
*env
, uint32_t sreg
, uint32_t reg
)
120 srs
= env
->pregs
[PR_SRS
];
122 env
->sregs
[srs
][sreg
] = env
->regs
[reg
];
124 #if !defined(CONFIG_USER_ONLY)
125 if (srs
== 1 || srs
== 2) {
127 /* Writes to tlb-hi write to mm_cause as a side
129 env
->sregs
[SFR_RW_MM_TLB_HI
] = env
->regs
[reg
];
130 env
->sregs
[SFR_R_MM_CAUSE
] = env
->regs
[reg
];
132 else if (sreg
== 5) {
139 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
144 /* We've just made a write to tlb_lo. */
145 lo
= env
->sregs
[SFR_RW_MM_TLB_LO
];
146 /* Writes are done via r_mm_cause. */
147 hi
= env
->sregs
[SFR_R_MM_CAUSE
];
149 vaddr
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].hi
,
151 vaddr
<<= TARGET_PAGE_BITS
;
152 tlb_v
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].lo
,
154 env
->tlbsets
[srs
- 1][set
][idx
].lo
= lo
;
155 env
->tlbsets
[srs
- 1][set
][idx
].hi
= hi
;
157 D_LOG("tlb flush vaddr=%x v=%d pc=%x\n",
158 vaddr
, tlb_v
, env
->pc
);
160 tlb_flush_page(env
, vaddr
);
167 void helper_movl_reg_sreg(CPUCRISState
*env
, uint32_t reg
, uint32_t sreg
)
170 env
->pregs
[PR_SRS
] &= 3;
171 srs
= env
->pregs
[PR_SRS
];
173 #if !defined(CONFIG_USER_ONLY)
174 if (srs
== 1 || srs
== 2)
180 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
185 /* Update the mirror regs. */
186 hi
= env
->tlbsets
[srs
- 1][set
][idx
].hi
;
187 lo
= env
->tlbsets
[srs
- 1][set
][idx
].lo
;
188 env
->sregs
[SFR_RW_MM_TLB_HI
] = hi
;
189 env
->sregs
[SFR_RW_MM_TLB_LO
] = lo
;
192 env
->regs
[reg
] = env
->sregs
[srs
][sreg
];
195 static void cris_ccs_rshift(CPUCRISState
*env
)
199 /* Apply the ccs shift. */
200 ccs
= env
->pregs
[PR_CCS
];
201 ccs
= (ccs
& 0xc0000000) | ((ccs
& 0x0fffffff) >> 10);
204 /* Enter user mode. */
205 env
->ksp
= env
->regs
[R_SP
];
206 env
->regs
[R_SP
] = env
->pregs
[PR_USP
];
209 env
->pregs
[PR_CCS
] = ccs
;
212 void helper_rfe(CPUCRISState
*env
)
214 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
216 D_LOG("rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
217 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
221 cris_ccs_rshift(env
);
223 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
225 env
->pregs
[PR_CCS
] |= P_FLAG
;
228 void helper_rfn(CPUCRISState
*env
)
230 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
232 D_LOG("rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
233 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
237 cris_ccs_rshift(env
);
239 /* Set the P_FLAG only if the R_FLAG is not set. */
241 env
->pregs
[PR_CCS
] |= P_FLAG
;
243 /* Always set the M flag. */
244 env
->pregs
[PR_CCS
] |= M_FLAG_V32
;
247 uint32_t helper_lz(uint32_t t0
)
252 uint32_t helper_btst(CPUCRISState
*env
, uint32_t t0
, uint32_t t1
, uint32_t ccs
)
254 /* FIXME: clean this up. */
257 The N flag is set according to the selected bit in the dest reg.
258 The Z flag is set if the selected bit and all bits to the right are
260 The X flag is cleared.
261 Other flags are left untouched.
262 The destination reg is not affected.*/
263 unsigned int fz
, sbit
, bset
, mask
, masked_t0
;
266 bset
= !!(t0
& (1 << sbit
));
267 mask
= sbit
== 31 ? -1 : (1 << (sbit
+ 1)) - 1;
268 masked_t0
= t0
& mask
;
269 fz
= !(masked_t0
| bset
);
271 /* Clear the X, N and Z flags. */
272 ccs
= ccs
& ~(X_FLAG
| N_FLAG
| Z_FLAG
);
273 if (env
->pregs
[PR_VR
] < 32)
274 ccs
&= ~(V_FLAG
| C_FLAG
);
275 /* Set the N and Z flags accordingly. */
276 ccs
|= (bset
<< 3) | (fz
<< 2);
280 static inline uint32_t evaluate_flags_writeback(CPUCRISState
*env
,
281 uint32_t flags
, uint32_t ccs
)
283 unsigned int x
, z
, mask
;
285 /* Extended arithmetics, leave the z flag alone. */
287 mask
= env
->cc_mask
| X_FLAG
;
294 /* all insn clear the x-flag except setf or clrf. */
300 uint32_t helper_evaluate_flags_muls(CPUCRISState
*env
,
301 uint32_t ccs
, uint32_t res
, uint32_t mof
)
307 dneg
= ((int32_t)res
) < 0;
316 if ((dneg
&& mof
!= -1)
317 || (!dneg
&& mof
!= 0))
319 return evaluate_flags_writeback(env
, flags
, ccs
);
322 uint32_t helper_evaluate_flags_mulu(CPUCRISState
*env
,
323 uint32_t ccs
, uint32_t res
, uint32_t mof
)
338 return evaluate_flags_writeback(env
, flags
, ccs
);
341 uint32_t helper_evaluate_flags_mcp(CPUCRISState
*env
, uint32_t ccs
,
342 uint32_t src
, uint32_t dst
, uint32_t res
)
346 src
= src
& 0x80000000;
347 dst
= dst
& 0x80000000;
349 if ((res
& 0x80000000L
) != 0L)
367 return evaluate_flags_writeback(env
, flags
, ccs
);
370 uint32_t helper_evaluate_flags_alu_4(CPUCRISState
*env
, uint32_t ccs
,
371 uint32_t src
, uint32_t dst
, uint32_t res
)
375 src
= src
& 0x80000000;
376 dst
= dst
& 0x80000000;
378 if ((res
& 0x80000000L
) != 0L)
396 return evaluate_flags_writeback(env
, flags
, ccs
);
399 uint32_t helper_evaluate_flags_sub_4(CPUCRISState
*env
, uint32_t ccs
,
400 uint32_t src
, uint32_t dst
, uint32_t res
)
404 src
= (~src
) & 0x80000000;
405 dst
= dst
& 0x80000000;
407 if ((res
& 0x80000000L
) != 0L)
426 return evaluate_flags_writeback(env
, flags
, ccs
);
429 uint32_t helper_evaluate_flags_move_4(CPUCRISState
*env
,
430 uint32_t ccs
, uint32_t res
)
434 if ((int32_t)res
< 0)
439 return evaluate_flags_writeback(env
, flags
, ccs
);
441 uint32_t helper_evaluate_flags_move_2(CPUCRISState
*env
,
442 uint32_t ccs
, uint32_t res
)
446 if ((int16_t)res
< 0L)
451 return evaluate_flags_writeback(env
, flags
, ccs
);
454 /* TODO: This is expensive. We could split things up and only evaluate part of
455 CCR on a need to know basis. For now, we simply re-evaluate everything. */
456 void helper_evaluate_flags(CPUCRISState
*env
)
458 uint32_t src
, dst
, res
;
463 res
= env
->cc_result
;
465 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
468 /* Now, evaluate the flags. This stuff is based on
469 Per Zander's CRISv10 simulator. */
470 switch (env
->cc_size
)
473 if ((res
& 0x80L
) != 0L)
476 if (((src
& 0x80L
) == 0L)
477 && ((dst
& 0x80L
) == 0L))
481 else if (((src
& 0x80L
) != 0L)
482 && ((dst
& 0x80L
) != 0L))
489 if ((res
& 0xFFL
) == 0L)
493 if (((src
& 0x80L
) != 0L)
494 && ((dst
& 0x80L
) != 0L))
498 if ((dst
& 0x80L
) != 0L
499 || (src
& 0x80L
) != 0L)
506 if ((res
& 0x8000L
) != 0L)
509 if (((src
& 0x8000L
) == 0L)
510 && ((dst
& 0x8000L
) == 0L))
514 else if (((src
& 0x8000L
) != 0L)
515 && ((dst
& 0x8000L
) != 0L))
522 if ((res
& 0xFFFFL
) == 0L)
526 if (((src
& 0x8000L
) != 0L)
527 && ((dst
& 0x8000L
) != 0L))
531 if ((dst
& 0x8000L
) != 0L
532 || (src
& 0x8000L
) != 0L)
539 if ((res
& 0x80000000L
) != 0L)
542 if (((src
& 0x80000000L
) == 0L)
543 && ((dst
& 0x80000000L
) == 0L))
547 else if (((src
& 0x80000000L
) != 0L) &&
548 ((dst
& 0x80000000L
) != 0L))
557 if (((src
& 0x80000000L
) != 0L)
558 && ((dst
& 0x80000000L
) != 0L))
560 if ((dst
& 0x80000000L
) != 0L
561 || (src
& 0x80000000L
) != 0L)
569 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
572 env
->pregs
[PR_CCS
] = evaluate_flags_writeback(env
, flags
,
576 void helper_top_evaluate_flags(CPUCRISState
*env
)
581 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mcp(env
,
582 env
->pregs
[PR_CCS
], env
->cc_src
,
583 env
->cc_dest
, env
->cc_result
);
586 env
->pregs
[PR_CCS
] = helper_evaluate_flags_muls(env
,
587 env
->pregs
[PR_CCS
], env
->cc_result
,
591 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mulu(env
,
592 env
->pregs
[PR_CCS
], env
->cc_result
,
602 switch (env
->cc_size
)
606 helper_evaluate_flags_move_4(env
,
612 helper_evaluate_flags_move_2(env
,
617 helper_evaluate_flags(env
);
626 if (env
->cc_size
== 4)
628 helper_evaluate_flags_sub_4(env
,
630 env
->cc_src
, env
->cc_dest
,
633 helper_evaluate_flags(env
);
637 switch (env
->cc_size
)
641 helper_evaluate_flags_alu_4(env
,
643 env
->cc_src
, env
->cc_dest
,
647 helper_evaluate_flags(env
);