target-ppc: expose cpu capability flags
[qemu.git] / target-cris / op_helper.c
blobedfaf41a8c74bba01874065490251cab42f51fb8
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 "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)
40 #define MMUSUFFIX _mmu
42 #define SHIFT 0
43 #include "softmmu_template.h"
45 #define SHIFT 1
46 #include "softmmu_template.h"
48 #define SHIFT 2
49 #include "softmmu_template.h"
51 #define SHIFT 3
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 /* XXX: fix it to restore all registers */
58 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
60 TranslationBlock *tb;
61 CPUState *saved_env;
62 unsigned long pc;
63 int ret;
65 /* XXX: hack to restore env in all cases, even if not called from
66 generated code */
67 saved_env = env;
68 env = cpu_single_env;
70 D_LOG("%s pc=%x tpc=%x ra=%x\n", __func__,
71 env->pc, env->debug1, retaddr);
72 ret = cpu_cris_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
73 if (unlikely(ret)) {
74 if (retaddr) {
75 /* now we have a real cpu fault */
76 pc = (unsigned long)retaddr;
77 tb = tb_find_pc(pc);
78 if (tb) {
79 /* the PC is inside the translated code. It means that we have
80 a virtual CPU fault */
81 cpu_restore_state(tb, env, pc, NULL);
83 /* Evaluate flags after retranslation. */
84 helper_top_evaluate_flags();
87 cpu_loop_exit();
89 env = saved_env;
92 #endif
94 void helper_raise_exception(uint32_t index)
96 env->exception_index = index;
97 cpu_loop_exit();
100 void helper_tlb_flush_pid(uint32_t pid)
102 #if !defined(CONFIG_USER_ONLY)
103 pid &= 0xff;
104 if (pid != (env->pregs[PR_PID] & 0xff))
105 cris_mmu_flush_pid(env, env->pregs[PR_PID]);
106 #endif
109 void helper_spc_write(uint32_t new_spc)
111 #if !defined(CONFIG_USER_ONLY)
112 tlb_flush_page(env, env->pregs[PR_SPC]);
113 tlb_flush_page(env, new_spc);
114 #endif
117 void helper_dump(uint32_t a0, uint32_t a1, uint32_t a2)
119 qemu_log("%s: a0=%x a1=%x\n", __func__, a0, a1);
122 /* Used by the tlb decoder. */
123 #define EXTRACT_FIELD(src, start, end) \
124 (((src) >> start) & ((1 << (end - start + 1)) - 1))
126 void helper_movl_sreg_reg (uint32_t sreg, uint32_t reg)
128 uint32_t srs;
129 srs = env->pregs[PR_SRS];
130 srs &= 3;
131 env->sregs[srs][sreg] = env->regs[reg];
133 #if !defined(CONFIG_USER_ONLY)
134 if (srs == 1 || srs == 2) {
135 if (sreg == 6) {
136 /* Writes to tlb-hi write to mm_cause as a side
137 effect. */
138 env->sregs[SFR_RW_MM_TLB_HI] = env->regs[reg];
139 env->sregs[SFR_R_MM_CAUSE] = env->regs[reg];
141 else if (sreg == 5) {
142 uint32_t set;
143 uint32_t idx;
144 uint32_t lo, hi;
145 uint32_t vaddr;
146 int tlb_v;
148 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
149 set >>= 4;
150 set &= 3;
152 idx &= 15;
153 /* We've just made a write to tlb_lo. */
154 lo = env->sregs[SFR_RW_MM_TLB_LO];
155 /* Writes are done via r_mm_cause. */
156 hi = env->sregs[SFR_R_MM_CAUSE];
158 vaddr = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].hi,
159 13, 31);
160 vaddr <<= TARGET_PAGE_BITS;
161 tlb_v = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].lo,
162 3, 3);
163 env->tlbsets[srs - 1][set][idx].lo = lo;
164 env->tlbsets[srs - 1][set][idx].hi = hi;
166 D_LOG("tlb flush vaddr=%x v=%d pc=%x\n",
167 vaddr, tlb_v, env->pc);
168 tlb_flush_page(env, vaddr);
171 #endif
174 void helper_movl_reg_sreg (uint32_t reg, uint32_t sreg)
176 uint32_t srs;
177 env->pregs[PR_SRS] &= 3;
178 srs = env->pregs[PR_SRS];
180 #if !defined(CONFIG_USER_ONLY)
181 if (srs == 1 || srs == 2)
183 uint32_t set;
184 uint32_t idx;
185 uint32_t lo, hi;
187 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
188 set >>= 4;
189 set &= 3;
190 idx &= 15;
192 /* Update the mirror regs. */
193 hi = env->tlbsets[srs - 1][set][idx].hi;
194 lo = env->tlbsets[srs - 1][set][idx].lo;
195 env->sregs[SFR_RW_MM_TLB_HI] = hi;
196 env->sregs[SFR_RW_MM_TLB_LO] = lo;
198 #endif
199 env->regs[reg] = env->sregs[srs][sreg];
202 static void cris_ccs_rshift(CPUState *env)
204 uint32_t ccs;
206 /* Apply the ccs shift. */
207 ccs = env->pregs[PR_CCS];
208 ccs = (ccs & 0xc0000000) | ((ccs & 0x0fffffff) >> 10);
209 if (ccs & U_FLAG)
211 /* Enter user mode. */
212 env->ksp = env->regs[R_SP];
213 env->regs[R_SP] = env->pregs[PR_USP];
216 env->pregs[PR_CCS] = ccs;
219 void helper_rfe(void)
221 int rflag = env->pregs[PR_CCS] & R_FLAG;
223 D_LOG("rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
224 env->pregs[PR_ERP], env->pregs[PR_PID],
225 env->pregs[PR_CCS],
226 env->btarget);
228 cris_ccs_rshift(env);
230 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
231 if (!rflag)
232 env->pregs[PR_CCS] |= P_FLAG;
235 void helper_rfn(void)
237 int rflag = env->pregs[PR_CCS] & R_FLAG;
239 D_LOG("rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
240 env->pregs[PR_ERP], env->pregs[PR_PID],
241 env->pregs[PR_CCS],
242 env->btarget);
244 cris_ccs_rshift(env);
246 /* Set the P_FLAG only if the R_FLAG is not set. */
247 if (!rflag)
248 env->pregs[PR_CCS] |= P_FLAG;
250 /* Always set the M flag. */
251 env->pregs[PR_CCS] |= M_FLAG;
254 uint32_t helper_lz(uint32_t t0)
256 return clz32(t0);
259 uint32_t helper_btst(uint32_t t0, uint32_t t1, uint32_t ccs)
261 /* FIXME: clean this up. */
263 /* des ref:
264 The N flag is set according to the selected bit in the dest reg.
265 The Z flag is set if the selected bit and all bits to the right are
266 zero.
267 The X flag is cleared.
268 Other flags are left untouched.
269 The destination reg is not affected.*/
270 unsigned int fz, sbit, bset, mask, masked_t0;
272 sbit = t1 & 31;
273 bset = !!(t0 & (1 << sbit));
274 mask = sbit == 31 ? -1 : (1 << (sbit + 1)) - 1;
275 masked_t0 = t0 & mask;
276 fz = !(masked_t0 | bset);
278 /* Clear the X, N and Z flags. */
279 ccs = ccs & ~(X_FLAG | N_FLAG | Z_FLAG);
280 /* Set the N and Z flags accordingly. */
281 ccs |= (bset << 3) | (fz << 2);
282 return ccs;
285 static inline uint32_t evaluate_flags_writeback(uint32_t flags, uint32_t ccs)
287 unsigned int x, z, mask;
289 /* Extended arithmetics, leave the z flag alone. */
290 x = env->cc_x;
291 mask = env->cc_mask | X_FLAG;
292 if (x) {
293 z = flags & Z_FLAG;
294 mask = mask & ~z;
296 flags &= mask;
298 /* all insn clear the x-flag except setf or clrf. */
299 ccs &= ~mask;
300 ccs |= flags;
301 return ccs;
304 uint32_t helper_evaluate_flags_muls(uint32_t ccs, uint32_t res, uint32_t mof)
306 uint32_t flags = 0;
307 int64_t tmp;
308 int dneg;
310 dneg = ((int32_t)res) < 0;
312 tmp = mof;
313 tmp <<= 32;
314 tmp |= res;
315 if (tmp == 0)
316 flags |= Z_FLAG;
317 else if (tmp < 0)
318 flags |= N_FLAG;
319 if ((dneg && mof != -1)
320 || (!dneg && mof != 0))
321 flags |= V_FLAG;
322 return evaluate_flags_writeback(flags, ccs);
325 uint32_t helper_evaluate_flags_mulu(uint32_t ccs, uint32_t res, uint32_t mof)
327 uint32_t flags = 0;
328 uint64_t tmp;
330 tmp = mof;
331 tmp <<= 32;
332 tmp |= res;
333 if (tmp == 0)
334 flags |= Z_FLAG;
335 else if (tmp >> 63)
336 flags |= N_FLAG;
337 if (mof)
338 flags |= V_FLAG;
340 return evaluate_flags_writeback(flags, ccs);
343 uint32_t helper_evaluate_flags_mcp(uint32_t ccs,
344 uint32_t src, uint32_t dst, uint32_t res)
346 uint32_t flags = 0;
348 src = src & 0x80000000;
349 dst = dst & 0x80000000;
351 if ((res & 0x80000000L) != 0L)
353 flags |= N_FLAG;
354 if (!src && !dst)
355 flags |= V_FLAG;
356 else if (src & dst)
357 flags |= R_FLAG;
359 else
361 if (res == 0L)
362 flags |= Z_FLAG;
363 if (src & dst)
364 flags |= V_FLAG;
365 if (dst | src)
366 flags |= R_FLAG;
369 return evaluate_flags_writeback(flags, ccs);
372 uint32_t helper_evaluate_flags_alu_4(uint32_t ccs,
373 uint32_t src, uint32_t dst, uint32_t res)
375 uint32_t flags = 0;
377 src = src & 0x80000000;
378 dst = dst & 0x80000000;
380 if ((res & 0x80000000L) != 0L)
382 flags |= N_FLAG;
383 if (!src && !dst)
384 flags |= V_FLAG;
385 else if (src & dst)
386 flags |= C_FLAG;
388 else
390 if (res == 0L)
391 flags |= Z_FLAG;
392 if (src & dst)
393 flags |= V_FLAG;
394 if (dst | src)
395 flags |= C_FLAG;
398 return evaluate_flags_writeback(flags, ccs);
401 uint32_t helper_evaluate_flags_sub_4(uint32_t ccs,
402 uint32_t src, uint32_t dst, uint32_t res)
404 uint32_t flags = 0;
406 src = (~src) & 0x80000000;
407 dst = dst & 0x80000000;
409 if ((res & 0x80000000L) != 0L)
411 flags |= N_FLAG;
412 if (!src && !dst)
413 flags |= V_FLAG;
414 else if (src & dst)
415 flags |= C_FLAG;
417 else
419 if (res == 0L)
420 flags |= Z_FLAG;
421 if (src & dst)
422 flags |= V_FLAG;
423 if (dst | src)
424 flags |= C_FLAG;
427 flags ^= C_FLAG;
428 return evaluate_flags_writeback(flags, ccs);
431 uint32_t helper_evaluate_flags_move_4(uint32_t ccs, uint32_t res)
433 uint32_t flags = 0;
435 if ((int32_t)res < 0)
436 flags |= N_FLAG;
437 else if (res == 0L)
438 flags |= Z_FLAG;
440 return evaluate_flags_writeback(flags, ccs);
442 uint32_t helper_evaluate_flags_move_2(uint32_t ccs, uint32_t res)
444 uint32_t flags = 0;
446 if ((int16_t)res < 0L)
447 flags |= N_FLAG;
448 else if (res == 0)
449 flags |= Z_FLAG;
451 return evaluate_flags_writeback(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(void)
458 uint32_t src, dst, res;
459 uint32_t flags = 0;
461 src = env->cc_src;
462 dst = env->cc_dest;
463 res = env->cc_result;
465 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
466 src = ~src;
468 /* Now, evaluate the flags. This stuff is based on
469 Per Zander's CRISv10 simulator. */
470 switch (env->cc_size)
472 case 1:
473 if ((res & 0x80L) != 0L)
475 flags |= N_FLAG;
476 if (((src & 0x80L) == 0L)
477 && ((dst & 0x80L) == 0L))
479 flags |= V_FLAG;
481 else if (((src & 0x80L) != 0L)
482 && ((dst & 0x80L) != 0L))
484 flags |= C_FLAG;
487 else
489 if ((res & 0xFFL) == 0L)
491 flags |= Z_FLAG;
493 if (((src & 0x80L) != 0L)
494 && ((dst & 0x80L) != 0L))
496 flags |= V_FLAG;
498 if ((dst & 0x80L) != 0L
499 || (src & 0x80L) != 0L)
501 flags |= C_FLAG;
504 break;
505 case 2:
506 if ((res & 0x8000L) != 0L)
508 flags |= N_FLAG;
509 if (((src & 0x8000L) == 0L)
510 && ((dst & 0x8000L) == 0L))
512 flags |= V_FLAG;
514 else if (((src & 0x8000L) != 0L)
515 && ((dst & 0x8000L) != 0L))
517 flags |= C_FLAG;
520 else
522 if ((res & 0xFFFFL) == 0L)
524 flags |= Z_FLAG;
526 if (((src & 0x8000L) != 0L)
527 && ((dst & 0x8000L) != 0L))
529 flags |= V_FLAG;
531 if ((dst & 0x8000L) != 0L
532 || (src & 0x8000L) != 0L)
534 flags |= C_FLAG;
537 break;
538 case 4:
539 if ((res & 0x80000000L) != 0L)
541 flags |= N_FLAG;
542 if (((src & 0x80000000L) == 0L)
543 && ((dst & 0x80000000L) == 0L))
545 flags |= V_FLAG;
547 else if (((src & 0x80000000L) != 0L) &&
548 ((dst & 0x80000000L) != 0L))
550 flags |= C_FLAG;
553 else
555 if (res == 0L)
556 flags |= Z_FLAG;
557 if (((src & 0x80000000L) != 0L)
558 && ((dst & 0x80000000L) != 0L))
559 flags |= V_FLAG;
560 if ((dst & 0x80000000L) != 0L
561 || (src & 0x80000000L) != 0L)
562 flags |= C_FLAG;
564 break;
565 default:
566 break;
569 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
570 flags ^= C_FLAG;
572 env->pregs[PR_CCS] = evaluate_flags_writeback(flags, env->pregs[PR_CCS]);
575 void helper_top_evaluate_flags(void)
577 switch (env->cc_op)
579 case CC_OP_MCP:
580 env->pregs[PR_CCS] = helper_evaluate_flags_mcp(
581 env->pregs[PR_CCS], env->cc_src,
582 env->cc_dest, env->cc_result);
583 break;
584 case CC_OP_MULS:
585 env->pregs[PR_CCS] = helper_evaluate_flags_muls(
586 env->pregs[PR_CCS], env->cc_result,
587 env->pregs[PR_MOF]);
588 break;
589 case CC_OP_MULU:
590 env->pregs[PR_CCS] = helper_evaluate_flags_mulu(
591 env->pregs[PR_CCS], env->cc_result,
592 env->pregs[PR_MOF]);
593 break;
594 case CC_OP_MOVE:
595 case CC_OP_AND:
596 case CC_OP_OR:
597 case CC_OP_XOR:
598 case CC_OP_ASR:
599 case CC_OP_LSR:
600 case CC_OP_LSL:
601 switch (env->cc_size)
603 case 4:
604 env->pregs[PR_CCS] =
605 helper_evaluate_flags_move_4(
606 env->pregs[PR_CCS],
607 env->cc_result);
608 break;
609 case 2:
610 env->pregs[PR_CCS] =
611 helper_evaluate_flags_move_2(
612 env->pregs[PR_CCS],
613 env->cc_result);
614 break;
615 default:
616 helper_evaluate_flags();
617 break;
619 break;
620 case CC_OP_FLAGS:
621 /* live. */
622 break;
623 case CC_OP_SUB:
624 case CC_OP_CMP:
625 if (env->cc_size == 4)
626 env->pregs[PR_CCS] =
627 helper_evaluate_flags_sub_4(
628 env->pregs[PR_CCS],
629 env->cc_src, env->cc_dest,
630 env->cc_result);
631 else
632 helper_evaluate_flags();
633 break;
634 default:
636 switch (env->cc_size)
638 case 4:
639 env->pregs[PR_CCS] =
640 helper_evaluate_flags_alu_4(
641 env->pregs[PR_CCS],
642 env->cc_src, env->cc_dest,
643 env->cc_result);
644 break;
645 default:
646 helper_evaluate_flags();
647 break;
650 break;