Kill redundant declarion of perror()
[qemu-kvm/fedora.git] / target-cris / op_helper.c
blobc96e48d837a7b8ba33758ff4027a633ba487dc2e
1 /*
2 * CRIS helper routines
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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
22 #include <assert.h>
23 #include "exec.h"
24 #include "mmu.h"
25 #include "helper.h"
26 #include "host-utils.h"
28 #define D(x)
30 #if !defined(CONFIG_USER_ONLY)
32 #define MMUSUFFIX _mmu
34 #define SHIFT 0
35 #include "softmmu_template.h"
37 #define SHIFT 1
38 #include "softmmu_template.h"
40 #define SHIFT 2
41 #include "softmmu_template.h"
43 #define SHIFT 3
44 #include "softmmu_template.h"
46 /* Try to fill the TLB and return an exception if error. If retaddr is
47 NULL, it means that the function was called in C code (i.e. not
48 from generated code or from helper.c) */
49 /* XXX: fix it to restore all registers */
50 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
52 TranslationBlock *tb;
53 CPUState *saved_env;
54 unsigned long pc;
55 int ret;
57 /* XXX: hack to restore env in all cases, even if not called from
58 generated code */
59 saved_env = env;
60 env = cpu_single_env;
62 D(fprintf(logfile, "%s pc=%x tpc=%x ra=%x\n", __func__,
63 env->pc, env->debug1, retaddr));
64 ret = cpu_cris_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
65 if (unlikely(ret)) {
66 if (retaddr) {
67 /* now we have a real cpu fault */
68 pc = (unsigned long)retaddr;
69 tb = tb_find_pc(pc);
70 if (tb) {
71 /* the PC is inside the translated code. It means that we have
72 a virtual CPU fault */
73 cpu_restore_state(tb, env, pc, NULL);
75 /* Evaluate flags after retranslation. */
76 helper_top_evaluate_flags();
79 cpu_loop_exit();
81 env = saved_env;
84 #endif
86 void helper_raise_exception(uint32_t index)
88 env->exception_index = index;
89 cpu_loop_exit();
92 void helper_tlb_flush_pid(uint32_t pid)
94 #if !defined(CONFIG_USER_ONLY)
95 pid &= 0xff;
96 if (pid != (env->pregs[PR_PID] & 0xff))
97 cris_mmu_flush_pid(env, env->pregs[PR_PID]);
98 #endif
101 void helper_spc_write(uint32_t new_spc)
103 #if !defined(CONFIG_USER_ONLY)
104 tlb_flush_page(env, env->pregs[PR_SPC]);
105 tlb_flush_page(env, new_spc);
106 #endif
109 void helper_dump(uint32_t a0, uint32_t a1, uint32_t a2)
111 (fprintf(logfile, "%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 (uint32_t sreg, uint32_t reg)
120 uint32_t srs;
121 srs = env->pregs[PR_SRS];
122 srs &= 3;
123 env->sregs[srs][sreg] = env->regs[reg];
125 #if !defined(CONFIG_USER_ONLY)
126 if (srs == 1 || srs == 2) {
127 if (sreg == 6) {
128 /* Writes to tlb-hi write to mm_cause as a side
129 effect. */
130 env->sregs[SFR_RW_MM_TLB_HI] = env->regs[reg];
131 env->sregs[SFR_R_MM_CAUSE] = env->regs[reg];
133 else if (sreg == 5) {
134 uint32_t set;
135 uint32_t idx;
136 uint32_t lo, hi;
137 uint32_t vaddr;
138 int tlb_v;
140 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
141 set >>= 4;
142 set &= 3;
144 idx &= 15;
145 /* We've just made a write to tlb_lo. */
146 lo = env->sregs[SFR_RW_MM_TLB_LO];
147 /* Writes are done via r_mm_cause. */
148 hi = env->sregs[SFR_R_MM_CAUSE];
150 vaddr = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].hi,
151 13, 31);
152 vaddr <<= TARGET_PAGE_BITS;
153 tlb_v = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].lo,
154 3, 3);
155 env->tlbsets[srs - 1][set][idx].lo = lo;
156 env->tlbsets[srs - 1][set][idx].hi = hi;
158 D(fprintf(logfile,
159 "tlb flush vaddr=%x v=%d pc=%x\n",
160 vaddr, tlb_v, env->pc));
161 tlb_flush_page(env, vaddr);
164 #endif
167 void helper_movl_reg_sreg (uint32_t reg, uint32_t sreg)
169 uint32_t srs;
170 env->pregs[PR_SRS] &= 3;
171 srs = env->pregs[PR_SRS];
173 #if !defined(CONFIG_USER_ONLY)
174 if (srs == 1 || srs == 2)
176 uint32_t set;
177 uint32_t idx;
178 uint32_t lo, hi;
180 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
181 set >>= 4;
182 set &= 3;
183 idx &= 15;
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;
191 #endif
192 env->regs[reg] = env->sregs[srs][sreg];
195 static void cris_ccs_rshift(CPUState *env)
197 uint32_t ccs;
199 /* Apply the ccs shift. */
200 ccs = env->pregs[PR_CCS];
201 ccs = (ccs & 0xc0000000) | ((ccs & 0x0fffffff) >> 10);
202 if (ccs & U_FLAG)
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(void)
214 int rflag = env->pregs[PR_CCS] & R_FLAG;
216 D(fprintf(logfile, "rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
217 env->pregs[PR_ERP], env->pregs[PR_PID],
218 env->pregs[PR_CCS],
219 env->btarget));
221 cris_ccs_rshift(env);
223 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
224 if (!rflag)
225 env->pregs[PR_CCS] |= P_FLAG;
228 void helper_rfn(void)
230 int rflag = env->pregs[PR_CCS] & R_FLAG;
232 D(fprintf(logfile, "rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
233 env->pregs[PR_ERP], env->pregs[PR_PID],
234 env->pregs[PR_CCS],
235 env->btarget));
237 cris_ccs_rshift(env);
239 /* Set the P_FLAG only if the R_FLAG is not set. */
240 if (!rflag)
241 env->pregs[PR_CCS] |= P_FLAG;
243 /* Always set the M flag. */
244 env->pregs[PR_CCS] |= M_FLAG;
247 uint32_t helper_lz(uint32_t t0)
249 return clz32(t0);
252 uint32_t helper_btst(uint32_t t0, uint32_t t1, uint32_t ccs)
254 /* FIXME: clean this up. */
256 /* des ref:
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
259 zero.
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;
265 sbit = t1 & 31;
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 /* Set the N and Z flags accordingly. */
274 ccs |= (bset << 3) | (fz << 2);
275 return ccs;
278 static inline uint32_t evaluate_flags_writeback(uint32_t flags, uint32_t ccs)
280 unsigned int x, z, mask;
282 /* Extended arithmetics, leave the z flag alone. */
283 x = env->cc_x;
284 mask = env->cc_mask | X_FLAG;
285 if (x) {
286 z = flags & Z_FLAG;
287 mask = mask & ~z;
289 flags &= mask;
291 /* all insn clear the x-flag except setf or clrf. */
292 ccs &= ~mask;
293 ccs |= flags;
294 return ccs;
297 uint32_t helper_evaluate_flags_muls(uint32_t ccs, uint32_t res, uint32_t mof)
299 uint32_t flags = 0;
300 int64_t tmp;
301 int dneg;
303 dneg = ((int32_t)res) < 0;
305 tmp = mof;
306 tmp <<= 32;
307 tmp |= res;
308 if (tmp == 0)
309 flags |= Z_FLAG;
310 else if (tmp < 0)
311 flags |= N_FLAG;
312 if ((dneg && mof != -1)
313 || (!dneg && mof != 0))
314 flags |= V_FLAG;
315 return evaluate_flags_writeback(flags, ccs);
318 uint32_t helper_evaluate_flags_mulu(uint32_t ccs, uint32_t res, uint32_t mof)
320 uint32_t flags = 0;
321 uint64_t tmp;
323 tmp = mof;
324 tmp <<= 32;
325 tmp |= res;
326 if (tmp == 0)
327 flags |= Z_FLAG;
328 else if (tmp >> 63)
329 flags |= N_FLAG;
330 if (mof)
331 flags |= V_FLAG;
333 return evaluate_flags_writeback(flags, ccs);
336 uint32_t helper_evaluate_flags_mcp(uint32_t ccs,
337 uint32_t src, uint32_t dst, uint32_t res)
339 uint32_t flags = 0;
341 src = src & 0x80000000;
342 dst = dst & 0x80000000;
344 if ((res & 0x80000000L) != 0L)
346 flags |= N_FLAG;
347 if (!src && !dst)
348 flags |= V_FLAG;
349 else if (src & dst)
350 flags |= R_FLAG;
352 else
354 if (res == 0L)
355 flags |= Z_FLAG;
356 if (src & dst)
357 flags |= V_FLAG;
358 if (dst | src)
359 flags |= R_FLAG;
362 return evaluate_flags_writeback(flags, ccs);
365 uint32_t helper_evaluate_flags_alu_4(uint32_t ccs,
366 uint32_t src, uint32_t dst, uint32_t res)
368 uint32_t flags = 0;
370 src = src & 0x80000000;
371 dst = dst & 0x80000000;
373 if ((res & 0x80000000L) != 0L)
375 flags |= N_FLAG;
376 if (!src && !dst)
377 flags |= V_FLAG;
378 else if (src & dst)
379 flags |= C_FLAG;
381 else
383 if (res == 0L)
384 flags |= Z_FLAG;
385 if (src & dst)
386 flags |= V_FLAG;
387 if (dst | src)
388 flags |= C_FLAG;
391 return evaluate_flags_writeback(flags, ccs);
394 uint32_t helper_evaluate_flags_sub_4(uint32_t ccs,
395 uint32_t src, uint32_t dst, uint32_t res)
397 uint32_t flags = 0;
399 src = (~src) & 0x80000000;
400 dst = dst & 0x80000000;
402 if ((res & 0x80000000L) != 0L)
404 flags |= N_FLAG;
405 if (!src && !dst)
406 flags |= V_FLAG;
407 else if (src & dst)
408 flags |= C_FLAG;
410 else
412 if (res == 0L)
413 flags |= Z_FLAG;
414 if (src & dst)
415 flags |= V_FLAG;
416 if (dst | src)
417 flags |= C_FLAG;
420 flags ^= C_FLAG;
421 return evaluate_flags_writeback(flags, ccs);
424 uint32_t helper_evaluate_flags_move_4(uint32_t ccs, uint32_t res)
426 uint32_t flags = 0;
428 if ((int32_t)res < 0)
429 flags |= N_FLAG;
430 else if (res == 0L)
431 flags |= Z_FLAG;
433 return evaluate_flags_writeback(flags, ccs);
435 uint32_t helper_evaluate_flags_move_2(uint32_t ccs, uint32_t res)
437 uint32_t flags = 0;
439 if ((int16_t)res < 0L)
440 flags |= N_FLAG;
441 else if (res == 0)
442 flags |= Z_FLAG;
444 return evaluate_flags_writeback(flags, ccs);
447 /* TODO: This is expensive. We could split things up and only evaluate part of
448 CCR on a need to know basis. For now, we simply re-evaluate everything. */
449 void helper_evaluate_flags(void)
451 uint32_t src, dst, res;
452 uint32_t flags = 0;
454 src = env->cc_src;
455 dst = env->cc_dest;
456 res = env->cc_result;
458 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
459 src = ~src;
461 /* Now, evaluate the flags. This stuff is based on
462 Per Zander's CRISv10 simulator. */
463 switch (env->cc_size)
465 case 1:
466 if ((res & 0x80L) != 0L)
468 flags |= N_FLAG;
469 if (((src & 0x80L) == 0L)
470 && ((dst & 0x80L) == 0L))
472 flags |= V_FLAG;
474 else if (((src & 0x80L) != 0L)
475 && ((dst & 0x80L) != 0L))
477 flags |= C_FLAG;
480 else
482 if ((res & 0xFFL) == 0L)
484 flags |= Z_FLAG;
486 if (((src & 0x80L) != 0L)
487 && ((dst & 0x80L) != 0L))
489 flags |= V_FLAG;
491 if ((dst & 0x80L) != 0L
492 || (src & 0x80L) != 0L)
494 flags |= C_FLAG;
497 break;
498 case 2:
499 if ((res & 0x8000L) != 0L)
501 flags |= N_FLAG;
502 if (((src & 0x8000L) == 0L)
503 && ((dst & 0x8000L) == 0L))
505 flags |= V_FLAG;
507 else if (((src & 0x8000L) != 0L)
508 && ((dst & 0x8000L) != 0L))
510 flags |= C_FLAG;
513 else
515 if ((res & 0xFFFFL) == 0L)
517 flags |= Z_FLAG;
519 if (((src & 0x8000L) != 0L)
520 && ((dst & 0x8000L) != 0L))
522 flags |= V_FLAG;
524 if ((dst & 0x8000L) != 0L
525 || (src & 0x8000L) != 0L)
527 flags |= C_FLAG;
530 break;
531 case 4:
532 if ((res & 0x80000000L) != 0L)
534 flags |= N_FLAG;
535 if (((src & 0x80000000L) == 0L)
536 && ((dst & 0x80000000L) == 0L))
538 flags |= V_FLAG;
540 else if (((src & 0x80000000L) != 0L) &&
541 ((dst & 0x80000000L) != 0L))
543 flags |= C_FLAG;
546 else
548 if (res == 0L)
549 flags |= Z_FLAG;
550 if (((src & 0x80000000L) != 0L)
551 && ((dst & 0x80000000L) != 0L))
552 flags |= V_FLAG;
553 if ((dst & 0x80000000L) != 0L
554 || (src & 0x80000000L) != 0L)
555 flags |= C_FLAG;
557 break;
558 default:
559 break;
562 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
563 flags ^= C_FLAG;
565 env->pregs[PR_CCS] = evaluate_flags_writeback(flags, env->pregs[PR_CCS]);
568 void helper_top_evaluate_flags(void)
570 switch (env->cc_op)
572 case CC_OP_MCP:
573 env->pregs[PR_CCS] = helper_evaluate_flags_mcp(
574 env->pregs[PR_CCS], env->cc_src,
575 env->cc_dest, env->cc_result);
576 break;
577 case CC_OP_MULS:
578 env->pregs[PR_CCS] = helper_evaluate_flags_muls(
579 env->pregs[PR_CCS], env->cc_result,
580 env->pregs[PR_MOF]);
581 break;
582 case CC_OP_MULU:
583 env->pregs[PR_CCS] = helper_evaluate_flags_mulu(
584 env->pregs[PR_CCS], env->cc_result,
585 env->pregs[PR_MOF]);
586 break;
587 case CC_OP_MOVE:
588 case CC_OP_AND:
589 case CC_OP_OR:
590 case CC_OP_XOR:
591 case CC_OP_ASR:
592 case CC_OP_LSR:
593 case CC_OP_LSL:
594 switch (env->cc_size)
596 case 4:
597 env->pregs[PR_CCS] =
598 helper_evaluate_flags_move_4(
599 env->pregs[PR_CCS],
600 env->cc_result);
601 break;
602 case 2:
603 env->pregs[PR_CCS] =
604 helper_evaluate_flags_move_2(
605 env->pregs[PR_CCS],
606 env->cc_result);
607 break;
608 default:
609 helper_evaluate_flags();
610 break;
612 break;
613 case CC_OP_FLAGS:
614 /* live. */
615 break;
616 case CC_OP_SUB:
617 case CC_OP_CMP:
618 if (env->cc_size == 4)
619 env->pregs[PR_CCS] =
620 helper_evaluate_flags_sub_4(
621 env->pregs[PR_CCS],
622 env->cc_src, env->cc_dest,
623 env->cc_result);
624 else
625 helper_evaluate_flags();
626 break;
627 default:
629 switch (env->cc_size)
631 case 4:
632 env->pregs[PR_CCS] =
633 helper_evaluate_flags_alu_4(
634 env->pregs[PR_CCS],
635 env->cc_src, env->cc_dest,
636 env->cc_result);
637 break;
638 default:
639 helper_evaluate_flags();
640 break;
643 break;