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)
39 #define MMUSUFFIX _mmu
42 #include "softmmu_template.h"
45 #include "softmmu_template.h"
48 #include "softmmu_template.h"
51 #include "softmmu_template.h"
53 /* Try to fill the TLB and return an exception if error. If retaddr is
54 NULL, it means that the function was called in C code (i.e. not
55 from generated code or from helper.c) */
56 /* XXX: fix it to restore all registers */
57 void tlb_fill (target_ulong addr
, int is_write
, int mmu_idx
, void *retaddr
)
64 /* XXX: hack to restore env in all cases, even if not called from
69 D_LOG("%s pc=%x tpc=%x ra=%x\n", __func__
,
70 env
->pc
, env
->debug1
, retaddr
);
71 ret
= cpu_cris_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
, 1);
74 /* now we have a real cpu fault */
75 pc
= (unsigned long)retaddr
;
78 /* the PC is inside the translated code. It means that we have
79 a virtual CPU fault */
80 cpu_restore_state(tb
, env
, pc
, NULL
);
82 /* Evaluate flags after retranslation. */
83 helper_top_evaluate_flags();
93 void helper_raise_exception(uint32_t index
)
95 env
->exception_index
= index
;
99 void helper_tlb_flush_pid(uint32_t pid
)
101 #if !defined(CONFIG_USER_ONLY)
103 if (pid
!= (env
->pregs
[PR_PID
] & 0xff))
104 cris_mmu_flush_pid(env
, env
->pregs
[PR_PID
]);
108 void helper_spc_write(uint32_t new_spc
)
110 #if !defined(CONFIG_USER_ONLY)
111 tlb_flush_page(env
, env
->pregs
[PR_SPC
]);
112 tlb_flush_page(env
, new_spc
);
116 void helper_dump(uint32_t a0
, uint32_t a1
, uint32_t a2
)
118 qemu_log("%s: a0=%x a1=%x\n", __func__
, a0
, a1
);
121 /* Used by the tlb decoder. */
122 #define EXTRACT_FIELD(src, start, end) \
123 (((src) >> start) & ((1 << (end - start + 1)) - 1))
125 void helper_movl_sreg_reg (uint32_t sreg
, uint32_t reg
)
128 srs
= env
->pregs
[PR_SRS
];
130 env
->sregs
[srs
][sreg
] = env
->regs
[reg
];
132 #if !defined(CONFIG_USER_ONLY)
133 if (srs
== 1 || srs
== 2) {
135 /* Writes to tlb-hi write to mm_cause as a side
137 env
->sregs
[SFR_RW_MM_TLB_HI
] = env
->regs
[reg
];
138 env
->sregs
[SFR_R_MM_CAUSE
] = env
->regs
[reg
];
140 else if (sreg
== 5) {
147 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
152 /* We've just made a write to tlb_lo. */
153 lo
= env
->sregs
[SFR_RW_MM_TLB_LO
];
154 /* Writes are done via r_mm_cause. */
155 hi
= env
->sregs
[SFR_R_MM_CAUSE
];
157 vaddr
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].hi
,
159 vaddr
<<= TARGET_PAGE_BITS
;
160 tlb_v
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].lo
,
162 env
->tlbsets
[srs
- 1][set
][idx
].lo
= lo
;
163 env
->tlbsets
[srs
- 1][set
][idx
].hi
= hi
;
165 D_LOG("tlb flush vaddr=%x v=%d pc=%x\n",
166 vaddr
, tlb_v
, env
->pc
);
167 tlb_flush_page(env
, vaddr
);
173 void helper_movl_reg_sreg (uint32_t reg
, uint32_t sreg
)
176 env
->pregs
[PR_SRS
] &= 3;
177 srs
= env
->pregs
[PR_SRS
];
179 #if !defined(CONFIG_USER_ONLY)
180 if (srs
== 1 || srs
== 2)
186 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
191 /* Update the mirror regs. */
192 hi
= env
->tlbsets
[srs
- 1][set
][idx
].hi
;
193 lo
= env
->tlbsets
[srs
- 1][set
][idx
].lo
;
194 env
->sregs
[SFR_RW_MM_TLB_HI
] = hi
;
195 env
->sregs
[SFR_RW_MM_TLB_LO
] = lo
;
198 env
->regs
[reg
] = env
->sregs
[srs
][sreg
];
201 static void cris_ccs_rshift(CPUState
*env
)
205 /* Apply the ccs shift. */
206 ccs
= env
->pregs
[PR_CCS
];
207 ccs
= (ccs
& 0xc0000000) | ((ccs
& 0x0fffffff) >> 10);
210 /* Enter user mode. */
211 env
->ksp
= env
->regs
[R_SP
];
212 env
->regs
[R_SP
] = env
->pregs
[PR_USP
];
215 env
->pregs
[PR_CCS
] = ccs
;
218 void helper_rfe(void)
220 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
222 D_LOG("rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
223 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
227 cris_ccs_rshift(env
);
229 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
231 env
->pregs
[PR_CCS
] |= P_FLAG
;
234 void helper_rfn(void)
236 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
238 D_LOG("rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
239 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
243 cris_ccs_rshift(env
);
245 /* Set the P_FLAG only if the R_FLAG is not set. */
247 env
->pregs
[PR_CCS
] |= P_FLAG
;
249 /* Always set the M flag. */
250 env
->pregs
[PR_CCS
] |= M_FLAG
;
253 uint32_t helper_lz(uint32_t t0
)
258 uint32_t helper_btst(uint32_t t0
, uint32_t t1
, uint32_t ccs
)
260 /* FIXME: clean this up. */
263 The N flag is set according to the selected bit in the dest reg.
264 The Z flag is set if the selected bit and all bits to the right are
266 The X flag is cleared.
267 Other flags are left untouched.
268 The destination reg is not affected.*/
269 unsigned int fz
, sbit
, bset
, mask
, masked_t0
;
272 bset
= !!(t0
& (1 << sbit
));
273 mask
= sbit
== 31 ? -1 : (1 << (sbit
+ 1)) - 1;
274 masked_t0
= t0
& mask
;
275 fz
= !(masked_t0
| bset
);
277 /* Clear the X, N and Z flags. */
278 ccs
= ccs
& ~(X_FLAG
| N_FLAG
| Z_FLAG
);
279 /* Set the N and Z flags accordingly. */
280 ccs
|= (bset
<< 3) | (fz
<< 2);
284 static inline uint32_t evaluate_flags_writeback(uint32_t flags
, uint32_t ccs
)
286 unsigned int x
, z
, mask
;
288 /* Extended arithmetics, leave the z flag alone. */
290 mask
= env
->cc_mask
| X_FLAG
;
297 /* all insn clear the x-flag except setf or clrf. */
303 uint32_t helper_evaluate_flags_muls(uint32_t ccs
, uint32_t res
, uint32_t mof
)
309 dneg
= ((int32_t)res
) < 0;
318 if ((dneg
&& mof
!= -1)
319 || (!dneg
&& mof
!= 0))
321 return evaluate_flags_writeback(flags
, ccs
);
324 uint32_t helper_evaluate_flags_mulu(uint32_t ccs
, uint32_t res
, uint32_t mof
)
339 return evaluate_flags_writeback(flags
, ccs
);
342 uint32_t helper_evaluate_flags_mcp(uint32_t ccs
,
343 uint32_t src
, uint32_t dst
, uint32_t res
)
347 src
= src
& 0x80000000;
348 dst
= dst
& 0x80000000;
350 if ((res
& 0x80000000L
) != 0L)
368 return evaluate_flags_writeback(flags
, ccs
);
371 uint32_t helper_evaluate_flags_alu_4(uint32_t ccs
,
372 uint32_t src
, uint32_t dst
, uint32_t res
)
376 src
= src
& 0x80000000;
377 dst
= dst
& 0x80000000;
379 if ((res
& 0x80000000L
) != 0L)
397 return evaluate_flags_writeback(flags
, ccs
);
400 uint32_t helper_evaluate_flags_sub_4(uint32_t ccs
,
401 uint32_t src
, uint32_t dst
, uint32_t res
)
405 src
= (~src
) & 0x80000000;
406 dst
= dst
& 0x80000000;
408 if ((res
& 0x80000000L
) != 0L)
427 return evaluate_flags_writeback(flags
, ccs
);
430 uint32_t helper_evaluate_flags_move_4(uint32_t ccs
, uint32_t res
)
434 if ((int32_t)res
< 0)
439 return evaluate_flags_writeback(flags
, ccs
);
441 uint32_t helper_evaluate_flags_move_2(uint32_t ccs
, uint32_t res
)
445 if ((int16_t)res
< 0L)
450 return evaluate_flags_writeback(flags
, ccs
);
453 /* TODO: This is expensive. We could split things up and only evaluate part of
454 CCR on a need to know basis. For now, we simply re-evaluate everything. */
455 void helper_evaluate_flags(void)
457 uint32_t src
, dst
, res
;
462 res
= env
->cc_result
;
464 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
467 /* Now, evaluate the flags. This stuff is based on
468 Per Zander's CRISv10 simulator. */
469 switch (env
->cc_size
)
472 if ((res
& 0x80L
) != 0L)
475 if (((src
& 0x80L
) == 0L)
476 && ((dst
& 0x80L
) == 0L))
480 else if (((src
& 0x80L
) != 0L)
481 && ((dst
& 0x80L
) != 0L))
488 if ((res
& 0xFFL
) == 0L)
492 if (((src
& 0x80L
) != 0L)
493 && ((dst
& 0x80L
) != 0L))
497 if ((dst
& 0x80L
) != 0L
498 || (src
& 0x80L
) != 0L)
505 if ((res
& 0x8000L
) != 0L)
508 if (((src
& 0x8000L
) == 0L)
509 && ((dst
& 0x8000L
) == 0L))
513 else if (((src
& 0x8000L
) != 0L)
514 && ((dst
& 0x8000L
) != 0L))
521 if ((res
& 0xFFFFL
) == 0L)
525 if (((src
& 0x8000L
) != 0L)
526 && ((dst
& 0x8000L
) != 0L))
530 if ((dst
& 0x8000L
) != 0L
531 || (src
& 0x8000L
) != 0L)
538 if ((res
& 0x80000000L
) != 0L)
541 if (((src
& 0x80000000L
) == 0L)
542 && ((dst
& 0x80000000L
) == 0L))
546 else if (((src
& 0x80000000L
) != 0L) &&
547 ((dst
& 0x80000000L
) != 0L))
556 if (((src
& 0x80000000L
) != 0L)
557 && ((dst
& 0x80000000L
) != 0L))
559 if ((dst
& 0x80000000L
) != 0L
560 || (src
& 0x80000000L
) != 0L)
568 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
571 env
->pregs
[PR_CCS
] = evaluate_flags_writeback(flags
, env
->pregs
[PR_CCS
]);
574 void helper_top_evaluate_flags(void)
579 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mcp(
580 env
->pregs
[PR_CCS
], env
->cc_src
,
581 env
->cc_dest
, env
->cc_result
);
584 env
->pregs
[PR_CCS
] = helper_evaluate_flags_muls(
585 env
->pregs
[PR_CCS
], env
->cc_result
,
589 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mulu(
590 env
->pregs
[PR_CCS
], env
->cc_result
,
600 switch (env
->cc_size
)
604 helper_evaluate_flags_move_4(
610 helper_evaluate_flags_move_2(
615 helper_evaluate_flags();
624 if (env
->cc_size
== 4)
626 helper_evaluate_flags_sub_4(
628 env
->cc_src
, env
->cc_dest
,
631 helper_evaluate_flags();
635 switch (env
->cc_size
)
639 helper_evaluate_flags_alu_4(
641 env
->cc_src
, env
->cc_dest
,
645 helper_evaluate_flags();