fix event fallout in grlib_apbuart.c
[qemu/aliguori.git] / target-cris / op_helper.c
blob0cfe1b18702004bb1826c92e06e1b78d4ae60e44
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, see <http://www.gnu.org/licenses/>.
21 #include "cpu.h"
22 #include "dyngen-exec.h"
23 #include "mmu.h"
24 #include "helper.h"
25 #include "host-utils.h"
27 //#define CRIS_OP_HELPER_DEBUG
30 #ifdef CRIS_OP_HELPER_DEBUG
31 #define D(x) x
32 #define D_LOG(...) qemu_log(__VA__ARGS__)
33 #else
34 #define D(x)
35 #define D_LOG(...) do { } while (0)
36 #endif
38 #if !defined(CONFIG_USER_ONLY)
39 #include "softmmu_exec.h"
41 #define MMUSUFFIX _mmu
43 #define SHIFT 0
44 #include "softmmu_template.h"
46 #define SHIFT 1
47 #include "softmmu_template.h"
49 #define SHIFT 2
50 #include "softmmu_template.h"
52 #define SHIFT 3
53 #include "softmmu_template.h"
55 /* Try to fill the TLB and return an exception if error. If retaddr is
56 NULL, it means that the function was called in C code (i.e. not
57 from generated code or from helper.c) */
58 /* XXX: fix it to restore all registers */
59 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
61 TranslationBlock *tb;
62 CPUState *saved_env;
63 unsigned long pc;
64 int ret;
66 /* XXX: hack to restore env in all cases, even if not called from
67 generated code */
68 saved_env = env;
69 env = cpu_single_env;
71 D_LOG("%s pc=%x tpc=%x ra=%x\n", __func__,
72 env->pc, env->debug1, retaddr);
73 ret = cpu_cris_handle_mmu_fault(env, addr, is_write, mmu_idx);
74 if (unlikely(ret)) {
75 if (retaddr) {
76 /* now we have a real cpu fault */
77 pc = (unsigned long)retaddr;
78 tb = tb_find_pc(pc);
79 if (tb) {
80 /* the PC is inside the translated code. It means that we have
81 a virtual CPU fault */
82 cpu_restore_state(tb, env, pc);
84 /* Evaluate flags after retranslation. */
85 helper_top_evaluate_flags();
88 cpu_loop_exit(env);
90 env = saved_env;
93 #endif
95 void helper_raise_exception(uint32_t index)
97 env->exception_index = index;
98 cpu_loop_exit(env);
101 void helper_tlb_flush_pid(uint32_t pid)
103 #if !defined(CONFIG_USER_ONLY)
104 pid &= 0xff;
105 if (pid != (env->pregs[PR_PID] & 0xff))
106 cris_mmu_flush_pid(env, env->pregs[PR_PID]);
107 #endif
110 void helper_spc_write(uint32_t new_spc)
112 #if !defined(CONFIG_USER_ONLY)
113 tlb_flush_page(env, env->pregs[PR_SPC]);
114 tlb_flush_page(env, new_spc);
115 #endif
118 void helper_dump(uint32_t a0, uint32_t a1, uint32_t a2)
120 qemu_log("%s: a0=%x a1=%x\n", __func__, a0, a1);
123 /* Used by the tlb decoder. */
124 #define EXTRACT_FIELD(src, start, end) \
125 (((src) >> start) & ((1 << (end - start + 1)) - 1))
127 void helper_movl_sreg_reg (uint32_t sreg, uint32_t reg)
129 uint32_t srs;
130 srs = env->pregs[PR_SRS];
131 srs &= 3;
132 env->sregs[srs][sreg] = env->regs[reg];
134 #if !defined(CONFIG_USER_ONLY)
135 if (srs == 1 || srs == 2) {
136 if (sreg == 6) {
137 /* Writes to tlb-hi write to mm_cause as a side
138 effect. */
139 env->sregs[SFR_RW_MM_TLB_HI] = env->regs[reg];
140 env->sregs[SFR_R_MM_CAUSE] = env->regs[reg];
142 else if (sreg == 5) {
143 uint32_t set;
144 uint32_t idx;
145 uint32_t lo, hi;
146 uint32_t vaddr;
147 int tlb_v;
149 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
150 set >>= 4;
151 set &= 3;
153 idx &= 15;
154 /* We've just made a write to tlb_lo. */
155 lo = env->sregs[SFR_RW_MM_TLB_LO];
156 /* Writes are done via r_mm_cause. */
157 hi = env->sregs[SFR_R_MM_CAUSE];
159 vaddr = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].hi,
160 13, 31);
161 vaddr <<= TARGET_PAGE_BITS;
162 tlb_v = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].lo,
163 3, 3);
164 env->tlbsets[srs - 1][set][idx].lo = lo;
165 env->tlbsets[srs - 1][set][idx].hi = hi;
167 D_LOG("tlb flush vaddr=%x v=%d pc=%x\n",
168 vaddr, tlb_v, env->pc);
169 if (tlb_v) {
170 tlb_flush_page(env, vaddr);
174 #endif
177 void helper_movl_reg_sreg (uint32_t reg, uint32_t sreg)
179 uint32_t srs;
180 env->pregs[PR_SRS] &= 3;
181 srs = env->pregs[PR_SRS];
183 #if !defined(CONFIG_USER_ONLY)
184 if (srs == 1 || srs == 2)
186 uint32_t set;
187 uint32_t idx;
188 uint32_t lo, hi;
190 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
191 set >>= 4;
192 set &= 3;
193 idx &= 15;
195 /* Update the mirror regs. */
196 hi = env->tlbsets[srs - 1][set][idx].hi;
197 lo = env->tlbsets[srs - 1][set][idx].lo;
198 env->sregs[SFR_RW_MM_TLB_HI] = hi;
199 env->sregs[SFR_RW_MM_TLB_LO] = lo;
201 #endif
202 env->regs[reg] = env->sregs[srs][sreg];
205 static void cris_ccs_rshift(CPUState *env)
207 uint32_t ccs;
209 /* Apply the ccs shift. */
210 ccs = env->pregs[PR_CCS];
211 ccs = (ccs & 0xc0000000) | ((ccs & 0x0fffffff) >> 10);
212 if (ccs & U_FLAG)
214 /* Enter user mode. */
215 env->ksp = env->regs[R_SP];
216 env->regs[R_SP] = env->pregs[PR_USP];
219 env->pregs[PR_CCS] = ccs;
222 void helper_rfe(void)
224 int rflag = env->pregs[PR_CCS] & R_FLAG;
226 D_LOG("rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
227 env->pregs[PR_ERP], env->pregs[PR_PID],
228 env->pregs[PR_CCS],
229 env->btarget);
231 cris_ccs_rshift(env);
233 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
234 if (!rflag)
235 env->pregs[PR_CCS] |= P_FLAG;
238 void helper_rfn(void)
240 int rflag = env->pregs[PR_CCS] & R_FLAG;
242 D_LOG("rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
243 env->pregs[PR_ERP], env->pregs[PR_PID],
244 env->pregs[PR_CCS],
245 env->btarget);
247 cris_ccs_rshift(env);
249 /* Set the P_FLAG only if the R_FLAG is not set. */
250 if (!rflag)
251 env->pregs[PR_CCS] |= P_FLAG;
253 /* Always set the M flag. */
254 env->pregs[PR_CCS] |= M_FLAG;
257 uint32_t helper_lz(uint32_t t0)
259 return clz32(t0);
262 uint32_t helper_btst(uint32_t t0, uint32_t t1, uint32_t ccs)
264 /* FIXME: clean this up. */
266 /* des ref:
267 The N flag is set according to the selected bit in the dest reg.
268 The Z flag is set if the selected bit and all bits to the right are
269 zero.
270 The X flag is cleared.
271 Other flags are left untouched.
272 The destination reg is not affected.*/
273 unsigned int fz, sbit, bset, mask, masked_t0;
275 sbit = t1 & 31;
276 bset = !!(t0 & (1 << sbit));
277 mask = sbit == 31 ? -1 : (1 << (sbit + 1)) - 1;
278 masked_t0 = t0 & mask;
279 fz = !(masked_t0 | bset);
281 /* Clear the X, N and Z flags. */
282 ccs = ccs & ~(X_FLAG | N_FLAG | Z_FLAG);
283 if (env->pregs[PR_VR] < 32)
284 ccs &= ~(V_FLAG | C_FLAG);
285 /* Set the N and Z flags accordingly. */
286 ccs |= (bset << 3) | (fz << 2);
287 return ccs;
290 static inline uint32_t evaluate_flags_writeback(uint32_t flags, uint32_t ccs)
292 unsigned int x, z, mask;
294 /* Extended arithmetics, leave the z flag alone. */
295 x = env->cc_x;
296 mask = env->cc_mask | X_FLAG;
297 if (x) {
298 z = flags & Z_FLAG;
299 mask = mask & ~z;
301 flags &= mask;
303 /* all insn clear the x-flag except setf or clrf. */
304 ccs &= ~mask;
305 ccs |= flags;
306 return ccs;
309 uint32_t helper_evaluate_flags_muls(uint32_t ccs, uint32_t res, uint32_t mof)
311 uint32_t flags = 0;
312 int64_t tmp;
313 int dneg;
315 dneg = ((int32_t)res) < 0;
317 tmp = mof;
318 tmp <<= 32;
319 tmp |= res;
320 if (tmp == 0)
321 flags |= Z_FLAG;
322 else if (tmp < 0)
323 flags |= N_FLAG;
324 if ((dneg && mof != -1)
325 || (!dneg && mof != 0))
326 flags |= V_FLAG;
327 return evaluate_flags_writeback(flags, ccs);
330 uint32_t helper_evaluate_flags_mulu(uint32_t ccs, uint32_t res, uint32_t mof)
332 uint32_t flags = 0;
333 uint64_t tmp;
335 tmp = mof;
336 tmp <<= 32;
337 tmp |= res;
338 if (tmp == 0)
339 flags |= Z_FLAG;
340 else if (tmp >> 63)
341 flags |= N_FLAG;
342 if (mof)
343 flags |= V_FLAG;
345 return evaluate_flags_writeback(flags, ccs);
348 uint32_t helper_evaluate_flags_mcp(uint32_t ccs,
349 uint32_t src, uint32_t dst, uint32_t res)
351 uint32_t flags = 0;
353 src = src & 0x80000000;
354 dst = dst & 0x80000000;
356 if ((res & 0x80000000L) != 0L)
358 flags |= N_FLAG;
359 if (!src && !dst)
360 flags |= V_FLAG;
361 else if (src & dst)
362 flags |= R_FLAG;
364 else
366 if (res == 0L)
367 flags |= Z_FLAG;
368 if (src & dst)
369 flags |= V_FLAG;
370 if (dst | src)
371 flags |= R_FLAG;
374 return evaluate_flags_writeback(flags, ccs);
377 uint32_t helper_evaluate_flags_alu_4(uint32_t ccs,
378 uint32_t src, uint32_t dst, uint32_t res)
380 uint32_t flags = 0;
382 src = src & 0x80000000;
383 dst = dst & 0x80000000;
385 if ((res & 0x80000000L) != 0L)
387 flags |= N_FLAG;
388 if (!src && !dst)
389 flags |= V_FLAG;
390 else if (src & dst)
391 flags |= C_FLAG;
393 else
395 if (res == 0L)
396 flags |= Z_FLAG;
397 if (src & dst)
398 flags |= V_FLAG;
399 if (dst | src)
400 flags |= C_FLAG;
403 return evaluate_flags_writeback(flags, ccs);
406 uint32_t helper_evaluate_flags_sub_4(uint32_t ccs,
407 uint32_t src, uint32_t dst, uint32_t res)
409 uint32_t flags = 0;
411 src = (~src) & 0x80000000;
412 dst = dst & 0x80000000;
414 if ((res & 0x80000000L) != 0L)
416 flags |= N_FLAG;
417 if (!src && !dst)
418 flags |= V_FLAG;
419 else if (src & dst)
420 flags |= C_FLAG;
422 else
424 if (res == 0L)
425 flags |= Z_FLAG;
426 if (src & dst)
427 flags |= V_FLAG;
428 if (dst | src)
429 flags |= C_FLAG;
432 flags ^= C_FLAG;
433 return evaluate_flags_writeback(flags, ccs);
436 uint32_t helper_evaluate_flags_move_4(uint32_t ccs, uint32_t res)
438 uint32_t flags = 0;
440 if ((int32_t)res < 0)
441 flags |= N_FLAG;
442 else if (res == 0L)
443 flags |= Z_FLAG;
445 return evaluate_flags_writeback(flags, ccs);
447 uint32_t helper_evaluate_flags_move_2(uint32_t ccs, uint32_t res)
449 uint32_t flags = 0;
451 if ((int16_t)res < 0L)
452 flags |= N_FLAG;
453 else if (res == 0)
454 flags |= Z_FLAG;
456 return evaluate_flags_writeback(flags, ccs);
459 /* TODO: This is expensive. We could split things up and only evaluate part of
460 CCR on a need to know basis. For now, we simply re-evaluate everything. */
461 void helper_evaluate_flags(void)
463 uint32_t src, dst, res;
464 uint32_t flags = 0;
466 src = env->cc_src;
467 dst = env->cc_dest;
468 res = env->cc_result;
470 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
471 src = ~src;
473 /* Now, evaluate the flags. This stuff is based on
474 Per Zander's CRISv10 simulator. */
475 switch (env->cc_size)
477 case 1:
478 if ((res & 0x80L) != 0L)
480 flags |= N_FLAG;
481 if (((src & 0x80L) == 0L)
482 && ((dst & 0x80L) == 0L))
484 flags |= V_FLAG;
486 else if (((src & 0x80L) != 0L)
487 && ((dst & 0x80L) != 0L))
489 flags |= C_FLAG;
492 else
494 if ((res & 0xFFL) == 0L)
496 flags |= Z_FLAG;
498 if (((src & 0x80L) != 0L)
499 && ((dst & 0x80L) != 0L))
501 flags |= V_FLAG;
503 if ((dst & 0x80L) != 0L
504 || (src & 0x80L) != 0L)
506 flags |= C_FLAG;
509 break;
510 case 2:
511 if ((res & 0x8000L) != 0L)
513 flags |= N_FLAG;
514 if (((src & 0x8000L) == 0L)
515 && ((dst & 0x8000L) == 0L))
517 flags |= V_FLAG;
519 else if (((src & 0x8000L) != 0L)
520 && ((dst & 0x8000L) != 0L))
522 flags |= C_FLAG;
525 else
527 if ((res & 0xFFFFL) == 0L)
529 flags |= Z_FLAG;
531 if (((src & 0x8000L) != 0L)
532 && ((dst & 0x8000L) != 0L))
534 flags |= V_FLAG;
536 if ((dst & 0x8000L) != 0L
537 || (src & 0x8000L) != 0L)
539 flags |= C_FLAG;
542 break;
543 case 4:
544 if ((res & 0x80000000L) != 0L)
546 flags |= N_FLAG;
547 if (((src & 0x80000000L) == 0L)
548 && ((dst & 0x80000000L) == 0L))
550 flags |= V_FLAG;
552 else if (((src & 0x80000000L) != 0L) &&
553 ((dst & 0x80000000L) != 0L))
555 flags |= C_FLAG;
558 else
560 if (res == 0L)
561 flags |= Z_FLAG;
562 if (((src & 0x80000000L) != 0L)
563 && ((dst & 0x80000000L) != 0L))
564 flags |= V_FLAG;
565 if ((dst & 0x80000000L) != 0L
566 || (src & 0x80000000L) != 0L)
567 flags |= C_FLAG;
569 break;
570 default:
571 break;
574 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
575 flags ^= C_FLAG;
577 env->pregs[PR_CCS] = evaluate_flags_writeback(flags, env->pregs[PR_CCS]);
580 void helper_top_evaluate_flags(void)
582 switch (env->cc_op)
584 case CC_OP_MCP:
585 env->pregs[PR_CCS] = helper_evaluate_flags_mcp(
586 env->pregs[PR_CCS], env->cc_src,
587 env->cc_dest, env->cc_result);
588 break;
589 case CC_OP_MULS:
590 env->pregs[PR_CCS] = helper_evaluate_flags_muls(
591 env->pregs[PR_CCS], env->cc_result,
592 env->pregs[PR_MOF]);
593 break;
594 case CC_OP_MULU:
595 env->pregs[PR_CCS] = helper_evaluate_flags_mulu(
596 env->pregs[PR_CCS], env->cc_result,
597 env->pregs[PR_MOF]);
598 break;
599 case CC_OP_MOVE:
600 case CC_OP_AND:
601 case CC_OP_OR:
602 case CC_OP_XOR:
603 case CC_OP_ASR:
604 case CC_OP_LSR:
605 case CC_OP_LSL:
606 switch (env->cc_size)
608 case 4:
609 env->pregs[PR_CCS] =
610 helper_evaluate_flags_move_4(
611 env->pregs[PR_CCS],
612 env->cc_result);
613 break;
614 case 2:
615 env->pregs[PR_CCS] =
616 helper_evaluate_flags_move_2(
617 env->pregs[PR_CCS],
618 env->cc_result);
619 break;
620 default:
621 helper_evaluate_flags();
622 break;
624 break;
625 case CC_OP_FLAGS:
626 /* live. */
627 break;
628 case CC_OP_SUB:
629 case CC_OP_CMP:
630 if (env->cc_size == 4)
631 env->pregs[PR_CCS] =
632 helper_evaluate_flags_sub_4(
633 env->pregs[PR_CCS],
634 env->cc_src, env->cc_dest,
635 env->cc_result);
636 else
637 helper_evaluate_flags();
638 break;
639 default:
641 switch (env->cc_size)
643 case 4:
644 env->pregs[PR_CCS] =
645 helper_evaluate_flags_alu_4(
646 env->pregs[PR_CCS],
647 env->cc_src, env->cc_dest,
648 env->cc_result);
649 break;
650 default:
651 helper_evaluate_flags();
652 break;
655 break;