Revert a part of r5087 that didn't need fixing (thanks to malc for spotting)
[qemu/mini2440.git] / target-cris / op_helper.c
blobfcf80705e9c29aac5ad82c305436443bf2159128
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <assert.h>
23 #include "exec.h"
24 #include "mmu.h"
25 #include "helper.h"
27 #define D(x)
29 #if !defined(CONFIG_USER_ONLY)
31 #define MMUSUFFIX _mmu
33 #define SHIFT 0
34 #include "softmmu_template.h"
36 #define SHIFT 1
37 #include "softmmu_template.h"
39 #define SHIFT 2
40 #include "softmmu_template.h"
42 #define SHIFT 3
43 #include "softmmu_template.h"
45 /* Try to fill the TLB and return an exception if error. If retaddr is
46 NULL, it means that the function was called in C code (i.e. not
47 from generated code or from helper.c) */
48 /* XXX: fix it to restore all registers */
49 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
51 TranslationBlock *tb;
52 CPUState *saved_env;
53 unsigned long pc;
54 int ret;
56 /* XXX: hack to restore env in all cases, even if not called from
57 generated code */
58 saved_env = env;
59 env = cpu_single_env;
61 D(fprintf(logfile, "%s pc=%x tpc=%x ra=%x\n", __func__,
62 env->pc, env->debug1, retaddr));
63 ret = cpu_cris_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
64 if (unlikely(ret)) {
65 if (retaddr) {
66 /* now we have a real cpu fault */
67 pc = (unsigned long)retaddr;
68 tb = tb_find_pc(pc);
69 if (tb) {
70 /* the PC is inside the translated code. It means that we have
71 a virtual CPU fault */
72 cpu_restore_state(tb, env, pc, NULL);
74 /* Evaluate flags after retranslation. */
75 helper_top_evaluate_flags();
78 cpu_loop_exit();
80 env = saved_env;
83 #endif
85 void helper_raise_exception(uint32_t index)
87 env->exception_index = index;
88 cpu_loop_exit();
91 void helper_tlb_flush_pid(uint32_t pid)
93 #if !defined(CONFIG_USER_ONLY)
94 cris_mmu_flush_pid(env, pid);
95 #endif
98 void helper_dump(uint32_t a0, uint32_t a1, uint32_t a2)
100 (fprintf(logfile, "%s: a0=%x a1=%x\n", __func__, a0, a1));
103 void helper_dummy(void)
108 /* Used by the tlb decoder. */
109 #define EXTRACT_FIELD(src, start, end) \
110 (((src) >> start) & ((1 << (end - start + 1)) - 1))
112 void helper_movl_sreg_reg (uint32_t sreg, uint32_t reg)
114 uint32_t srs;
115 srs = env->pregs[PR_SRS];
116 srs &= 3;
117 env->sregs[srs][sreg] = env->regs[reg];
119 #if !defined(CONFIG_USER_ONLY)
120 if (srs == 1 || srs == 2) {
121 if (sreg == 6) {
122 /* Writes to tlb-hi write to mm_cause as a side
123 effect. */
124 env->sregs[SFR_RW_MM_TLB_HI] = env->regs[reg];
125 env->sregs[SFR_R_MM_CAUSE] = env->regs[reg];
127 else if (sreg == 5) {
128 uint32_t set;
129 uint32_t idx;
130 uint32_t lo, hi;
131 uint32_t vaddr;
132 int tlb_v;
134 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
135 set >>= 4;
136 set &= 3;
138 idx &= 15;
139 /* We've just made a write to tlb_lo. */
140 lo = env->sregs[SFR_RW_MM_TLB_LO];
141 /* Writes are done via r_mm_cause. */
142 hi = env->sregs[SFR_R_MM_CAUSE];
144 vaddr = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].hi,
145 13, 31);
146 vaddr <<= TARGET_PAGE_BITS;
147 tlb_v = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].lo,
148 3, 3);
149 env->tlbsets[srs - 1][set][idx].lo = lo;
150 env->tlbsets[srs - 1][set][idx].hi = hi;
152 D(fprintf(logfile,
153 "tlb flush vaddr=%x v=%d pc=%x\n",
154 vaddr, tlb_v, env->pc));
155 tlb_flush_page(env, vaddr);
158 #endif
161 void helper_movl_reg_sreg (uint32_t reg, uint32_t sreg)
163 uint32_t srs;
164 env->pregs[PR_SRS] &= 3;
165 srs = env->pregs[PR_SRS];
167 #if !defined(CONFIG_USER_ONLY)
168 if (srs == 1 || srs == 2)
170 uint32_t set;
171 uint32_t idx;
172 uint32_t lo, hi;
174 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
175 set >>= 4;
176 set &= 3;
177 idx &= 15;
179 /* Update the mirror regs. */
180 hi = env->tlbsets[srs - 1][set][idx].hi;
181 lo = env->tlbsets[srs - 1][set][idx].lo;
182 env->sregs[SFR_RW_MM_TLB_HI] = hi;
183 env->sregs[SFR_RW_MM_TLB_LO] = lo;
185 #endif
186 env->regs[reg] = env->sregs[srs][sreg];
187 RETURN();
190 static void cris_ccs_rshift(CPUState *env)
192 uint32_t ccs;
194 /* Apply the ccs shift. */
195 ccs = env->pregs[PR_CCS];
196 ccs = (ccs & 0xc0000000) | ((ccs & 0x0fffffff) >> 10);
197 if (ccs & U_FLAG)
199 /* Enter user mode. */
200 env->ksp = env->regs[R_SP];
201 env->regs[R_SP] = env->pregs[PR_USP];
204 env->pregs[PR_CCS] = ccs;
207 void helper_rfe(void)
209 int rflag = env->pregs[PR_CCS] & R_FLAG;
211 D(fprintf(logfile, "rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
212 env->pregs[PR_ERP], env->pregs[PR_PID],
213 env->pregs[PR_CCS],
214 env->btarget));
216 cris_ccs_rshift(env);
218 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
219 if (!rflag)
220 env->pregs[PR_CCS] |= P_FLAG;
223 void helper_rfn(void)
225 int rflag = env->pregs[PR_CCS] & R_FLAG;
227 D(fprintf(logfile, "rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
228 env->pregs[PR_ERP], env->pregs[PR_PID],
229 env->pregs[PR_CCS],
230 env->btarget));
232 cris_ccs_rshift(env);
234 /* Set the P_FLAG only if the R_FLAG is not set. */
235 if (!rflag)
236 env->pregs[PR_CCS] |= P_FLAG;
238 /* Always set the M flag. */
239 env->pregs[PR_CCS] |= M_FLAG;
242 void helper_store(uint32_t a0)
244 if (env->pregs[PR_CCS] & P_FLAG )
246 cpu_abort(env, "cond_store_failed! pc=%x a0=%x\n",
247 env->pc, a0);
251 void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
252 int is_asi)
254 D(printf("%s addr=%x w=%d ex=%d asi=%d\n",
255 __func__, addr, is_write, is_exec, is_asi));
258 static void evaluate_flags_writeback(uint32_t flags)
260 int x;
262 /* Extended arithmetics, leave the z flag alone. */
263 x = env->cc_x;
264 if ((x || env->cc_op == CC_OP_ADDC)
265 && flags & Z_FLAG)
266 env->cc_mask &= ~Z_FLAG;
268 /* all insn clear the x-flag except setf or clrf. */
269 env->pregs[PR_CCS] &= ~(env->cc_mask | X_FLAG);
270 flags &= env->cc_mask;
271 env->pregs[PR_CCS] |= flags;
274 void helper_evaluate_flags_muls(void)
276 uint32_t src;
277 uint32_t dst;
278 uint32_t res;
279 uint32_t flags = 0;
280 int64_t tmp;
281 int32_t mof;
282 int dneg;
284 src = env->cc_src;
285 dst = env->cc_dest;
286 res = env->cc_result;
288 dneg = ((int32_t)res) < 0;
290 mof = env->pregs[PR_MOF];
291 tmp = mof;
292 tmp <<= 32;
293 tmp |= res;
294 if (tmp == 0)
295 flags |= Z_FLAG;
296 else if (tmp < 0)
297 flags |= N_FLAG;
298 if ((dneg && mof != -1)
299 || (!dneg && mof != 0))
300 flags |= V_FLAG;
301 evaluate_flags_writeback(flags);
304 void helper_evaluate_flags_mulu(void)
306 uint32_t src;
307 uint32_t dst;
308 uint32_t res;
309 uint32_t flags = 0;
310 uint64_t tmp;
311 uint32_t mof;
313 src = env->cc_src;
314 dst = env->cc_dest;
315 res = env->cc_result;
317 mof = env->pregs[PR_MOF];
318 tmp = mof;
319 tmp <<= 32;
320 tmp |= res;
321 if (tmp == 0)
322 flags |= Z_FLAG;
323 else if (tmp >> 63)
324 flags |= N_FLAG;
325 if (mof)
326 flags |= V_FLAG;
328 evaluate_flags_writeback(flags);
331 void helper_evaluate_flags_mcp(void)
333 uint32_t src;
334 uint32_t dst;
335 uint32_t res;
336 uint32_t flags = 0;
338 src = env->cc_src;
339 dst = env->cc_dest;
340 res = env->cc_result;
342 if ((res & 0x80000000L) != 0L)
344 flags |= N_FLAG;
345 if (((src & 0x80000000L) == 0L)
346 && ((dst & 0x80000000L) == 0L))
348 flags |= V_FLAG;
350 else if (((src & 0x80000000L) != 0L) &&
351 ((dst & 0x80000000L) != 0L))
353 flags |= R_FLAG;
356 else
358 if (res == 0L)
359 flags |= Z_FLAG;
360 if (((src & 0x80000000L) != 0L)
361 && ((dst & 0x80000000L) != 0L))
362 flags |= V_FLAG;
363 if ((dst & 0x80000000L) != 0L
364 || (src & 0x80000000L) != 0L)
365 flags |= R_FLAG;
368 evaluate_flags_writeback(flags);
371 void helper_evaluate_flags_alu_4(void)
373 uint32_t src;
374 uint32_t dst;
375 uint32_t res;
376 uint32_t flags = 0;
378 src = env->cc_src;
379 dst = env->cc_dest;
381 /* Reconstruct the result. */
382 switch (env->cc_op)
384 case CC_OP_SUB:
385 res = dst - src;
386 break;
387 case CC_OP_ADD:
388 res = dst + src;
389 break;
390 default:
391 res = env->cc_result;
392 break;
395 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
396 src = ~src;
398 if ((res & 0x80000000L) != 0L)
400 flags |= N_FLAG;
401 if (((src & 0x80000000L) == 0L)
402 && ((dst & 0x80000000L) == 0L))
404 flags |= V_FLAG;
406 else if (((src & 0x80000000L) != 0L) &&
407 ((dst & 0x80000000L) != 0L))
409 flags |= C_FLAG;
412 else
414 if (res == 0L)
415 flags |= Z_FLAG;
416 if (((src & 0x80000000L) != 0L)
417 && ((dst & 0x80000000L) != 0L))
418 flags |= V_FLAG;
419 if ((dst & 0x80000000L) != 0L
420 || (src & 0x80000000L) != 0L)
421 flags |= C_FLAG;
424 if (env->cc_op == CC_OP_SUB
425 || env->cc_op == CC_OP_CMP) {
426 flags ^= C_FLAG;
428 evaluate_flags_writeback(flags);
431 void helper_evaluate_flags_move_4 (void)
433 uint32_t res;
434 uint32_t flags = 0;
436 res = env->cc_result;
438 if ((int32_t)res < 0)
439 flags |= N_FLAG;
440 else if (res == 0L)
441 flags |= Z_FLAG;
443 evaluate_flags_writeback(flags);
445 void helper_evaluate_flags_move_2 (void)
447 uint32_t src;
448 uint32_t flags = 0;
449 uint16_t res;
451 src = env->cc_src;
452 res = env->cc_result;
454 if ((int16_t)res < 0L)
455 flags |= N_FLAG;
456 else if (res == 0)
457 flags |= Z_FLAG;
459 evaluate_flags_writeback(flags);
462 /* TODO: This is expensive. We could split things up and only evaluate part of
463 CCR on a need to know basis. For now, we simply re-evaluate everything. */
464 void helper_evaluate_flags (void)
466 uint32_t src;
467 uint32_t dst;
468 uint32_t res;
469 uint32_t flags = 0;
471 src = env->cc_src;
472 dst = env->cc_dest;
473 res = env->cc_result;
475 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
476 src = ~src;
478 /* Now, evaluate the flags. This stuff is based on
479 Per Zander's CRISv10 simulator. */
480 switch (env->cc_size)
482 case 1:
483 if ((res & 0x80L) != 0L)
485 flags |= N_FLAG;
486 if (((src & 0x80L) == 0L)
487 && ((dst & 0x80L) == 0L))
489 flags |= V_FLAG;
491 else if (((src & 0x80L) != 0L)
492 && ((dst & 0x80L) != 0L))
494 flags |= C_FLAG;
497 else
499 if ((res & 0xFFL) == 0L)
501 flags |= Z_FLAG;
503 if (((src & 0x80L) != 0L)
504 && ((dst & 0x80L) != 0L))
506 flags |= V_FLAG;
508 if ((dst & 0x80L) != 0L
509 || (src & 0x80L) != 0L)
511 flags |= C_FLAG;
514 break;
515 case 2:
516 if ((res & 0x8000L) != 0L)
518 flags |= N_FLAG;
519 if (((src & 0x8000L) == 0L)
520 && ((dst & 0x8000L) == 0L))
522 flags |= V_FLAG;
524 else if (((src & 0x8000L) != 0L)
525 && ((dst & 0x8000L) != 0L))
527 flags |= C_FLAG;
530 else
532 if ((res & 0xFFFFL) == 0L)
534 flags |= Z_FLAG;
536 if (((src & 0x8000L) != 0L)
537 && ((dst & 0x8000L) != 0L))
539 flags |= V_FLAG;
541 if ((dst & 0x8000L) != 0L
542 || (src & 0x8000L) != 0L)
544 flags |= C_FLAG;
547 break;
548 case 4:
549 if ((res & 0x80000000L) != 0L)
551 flags |= N_FLAG;
552 if (((src & 0x80000000L) == 0L)
553 && ((dst & 0x80000000L) == 0L))
555 flags |= V_FLAG;
557 else if (((src & 0x80000000L) != 0L) &&
558 ((dst & 0x80000000L) != 0L))
560 flags |= C_FLAG;
563 else
565 if (res == 0L)
566 flags |= Z_FLAG;
567 if (((src & 0x80000000L) != 0L)
568 && ((dst & 0x80000000L) != 0L))
569 flags |= V_FLAG;
570 if ((dst & 0x80000000L) != 0L
571 || (src & 0x80000000L) != 0L)
572 flags |= C_FLAG;
574 break;
575 default:
576 break;
579 if (env->cc_op == CC_OP_SUB
580 || env->cc_op == CC_OP_CMP) {
581 flags ^= C_FLAG;
583 evaluate_flags_writeback(flags);
586 void helper_top_evaluate_flags(void)
588 switch (env->cc_op)
590 case CC_OP_MCP:
591 helper_evaluate_flags_mcp();
592 break;
593 case CC_OP_MULS:
594 helper_evaluate_flags_muls();
595 break;
596 case CC_OP_MULU:
597 helper_evaluate_flags_mulu();
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 helper_evaluate_flags_move_4();
610 break;
611 case 2:
612 helper_evaluate_flags_move_2();
613 break;
614 default:
615 helper_evaluate_flags();
616 break;
618 break;
619 case CC_OP_FLAGS:
620 /* live. */
621 break;
622 default:
624 switch (env->cc_size)
626 case 4:
627 helper_evaluate_flags_alu_4();
628 break;
629 default:
630 helper_evaluate_flags();
631 break;
634 break;