aspeed: Implement write-1-{set, clear} for AST2500 strapping
[qemu/ar7.git] / target / cris / op_helper.c
blob0ee3a3117b9b88159ceec39986040661681810e2
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 "qemu/osdep.h"
22 #include "cpu.h"
23 #include "mmu.h"
24 #include "exec/helper-proto.h"
25 #include "qemu/host-utils.h"
26 #include "exec/exec-all.h"
27 #include "exec/cpu_ldst.h"
29 //#define CRIS_OP_HELPER_DEBUG
32 #ifdef CRIS_OP_HELPER_DEBUG
33 #define D(x) x
34 #define D_LOG(...) qemu_log(__VA_ARGS__)
35 #else
36 #define D(x)
37 #define D_LOG(...) do { } while (0)
38 #endif
40 #if !defined(CONFIG_USER_ONLY)
41 /* Try to fill the TLB and return an exception if error. If retaddr is
42 NULL, it means that the function was called in C code (i.e. not
43 from generated code or from helper.c) */
44 void tlb_fill(CPUState *cs, target_ulong addr, int size,
45 MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
47 CRISCPU *cpu = CRIS_CPU(cs);
48 CPUCRISState *env = &cpu->env;
49 int ret;
51 D_LOG("%s pc=%x tpc=%x ra=%p\n", __func__,
52 env->pc, env->pregs[PR_EDA], (void *)retaddr);
53 ret = cris_cpu_handle_mmu_fault(cs, addr, size, access_type, mmu_idx);
54 if (unlikely(ret)) {
55 if (retaddr) {
56 /* now we have a real cpu fault */
57 if (cpu_restore_state(cs, retaddr, true)) {
58 /* Evaluate flags after retranslation. */
59 helper_top_evaluate_flags(env);
62 cpu_loop_exit(cs);
66 #endif
68 void helper_raise_exception(CPUCRISState *env, uint32_t index)
70 CPUState *cs = CPU(cris_env_get_cpu(env));
72 cs->exception_index = index;
73 cpu_loop_exit(cs);
76 void helper_tlb_flush_pid(CPUCRISState *env, uint32_t pid)
78 #if !defined(CONFIG_USER_ONLY)
79 pid &= 0xff;
80 if (pid != (env->pregs[PR_PID] & 0xff))
81 cris_mmu_flush_pid(env, env->pregs[PR_PID]);
82 #endif
85 void helper_spc_write(CPUCRISState *env, uint32_t new_spc)
87 #if !defined(CONFIG_USER_ONLY)
88 CRISCPU *cpu = cris_env_get_cpu(env);
89 CPUState *cs = CPU(cpu);
91 tlb_flush_page(cs, env->pregs[PR_SPC]);
92 tlb_flush_page(cs, new_spc);
93 #endif
96 /* Used by the tlb decoder. */
97 #define EXTRACT_FIELD(src, start, end) \
98 (((src) >> start) & ((1 << (end - start + 1)) - 1))
100 void helper_movl_sreg_reg(CPUCRISState *env, uint32_t sreg, uint32_t reg)
102 #if !defined(CONFIG_USER_ONLY)
103 CRISCPU *cpu = cris_env_get_cpu(env);
104 #endif
105 uint32_t srs;
106 srs = env->pregs[PR_SRS];
107 srs &= 3;
108 env->sregs[srs][sreg] = env->regs[reg];
110 #if !defined(CONFIG_USER_ONLY)
111 if (srs == 1 || srs == 2) {
112 if (sreg == 6) {
113 /* Writes to tlb-hi write to mm_cause as a side
114 effect. */
115 env->sregs[SFR_RW_MM_TLB_HI] = env->regs[reg];
116 env->sregs[SFR_R_MM_CAUSE] = env->regs[reg];
118 else if (sreg == 5) {
119 uint32_t set;
120 uint32_t idx;
121 uint32_t lo, hi;
122 uint32_t vaddr;
123 int tlb_v;
125 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
126 set >>= 4;
127 set &= 3;
129 idx &= 15;
130 /* We've just made a write to tlb_lo. */
131 lo = env->sregs[SFR_RW_MM_TLB_LO];
132 /* Writes are done via r_mm_cause. */
133 hi = env->sregs[SFR_R_MM_CAUSE];
135 vaddr = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].hi,
136 13, 31);
137 vaddr <<= TARGET_PAGE_BITS;
138 tlb_v = EXTRACT_FIELD(env->tlbsets[srs-1][set][idx].lo,
139 3, 3);
140 env->tlbsets[srs - 1][set][idx].lo = lo;
141 env->tlbsets[srs - 1][set][idx].hi = hi;
143 D_LOG("tlb flush vaddr=%x v=%d pc=%x\n",
144 vaddr, tlb_v, env->pc);
145 if (tlb_v) {
146 tlb_flush_page(CPU(cpu), vaddr);
150 #endif
153 void helper_movl_reg_sreg(CPUCRISState *env, uint32_t reg, uint32_t sreg)
155 uint32_t srs;
156 env->pregs[PR_SRS] &= 3;
157 srs = env->pregs[PR_SRS];
159 #if !defined(CONFIG_USER_ONLY)
160 if (srs == 1 || srs == 2)
162 uint32_t set;
163 uint32_t idx;
164 uint32_t lo, hi;
166 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
167 set >>= 4;
168 set &= 3;
169 idx &= 15;
171 /* Update the mirror regs. */
172 hi = env->tlbsets[srs - 1][set][idx].hi;
173 lo = env->tlbsets[srs - 1][set][idx].lo;
174 env->sregs[SFR_RW_MM_TLB_HI] = hi;
175 env->sregs[SFR_RW_MM_TLB_LO] = lo;
177 #endif
178 env->regs[reg] = env->sregs[srs][sreg];
181 static void cris_ccs_rshift(CPUCRISState *env)
183 uint32_t ccs;
185 /* Apply the ccs shift. */
186 ccs = env->pregs[PR_CCS];
187 ccs = (ccs & 0xc0000000) | ((ccs & 0x0fffffff) >> 10);
188 if (ccs & U_FLAG)
190 /* Enter user mode. */
191 env->ksp = env->regs[R_SP];
192 env->regs[R_SP] = env->pregs[PR_USP];
195 env->pregs[PR_CCS] = ccs;
198 void helper_rfe(CPUCRISState *env)
200 int rflag = env->pregs[PR_CCS] & R_FLAG;
202 D_LOG("rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
203 env->pregs[PR_ERP], env->pregs[PR_PID],
204 env->pregs[PR_CCS],
205 env->btarget);
207 cris_ccs_rshift(env);
209 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
210 if (!rflag)
211 env->pregs[PR_CCS] |= P_FLAG;
214 void helper_rfn(CPUCRISState *env)
216 int rflag = env->pregs[PR_CCS] & R_FLAG;
218 D_LOG("rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
219 env->pregs[PR_ERP], env->pregs[PR_PID],
220 env->pregs[PR_CCS],
221 env->btarget);
223 cris_ccs_rshift(env);
225 /* Set the P_FLAG only if the R_FLAG is not set. */
226 if (!rflag)
227 env->pregs[PR_CCS] |= P_FLAG;
229 /* Always set the M flag. */
230 env->pregs[PR_CCS] |= M_FLAG_V32;
233 uint32_t helper_btst(CPUCRISState *env, uint32_t t0, uint32_t t1, uint32_t ccs)
235 /* FIXME: clean this up. */
237 /* des ref:
238 The N flag is set according to the selected bit in the dest reg.
239 The Z flag is set if the selected bit and all bits to the right are
240 zero.
241 The X flag is cleared.
242 Other flags are left untouched.
243 The destination reg is not affected.*/
244 unsigned int fz, sbit, bset, mask, masked_t0;
246 sbit = t1 & 31;
247 bset = !!(t0 & (1 << sbit));
248 mask = sbit == 31 ? -1 : (1 << (sbit + 1)) - 1;
249 masked_t0 = t0 & mask;
250 fz = !(masked_t0 | bset);
252 /* Clear the X, N and Z flags. */
253 ccs = ccs & ~(X_FLAG | N_FLAG | Z_FLAG);
254 if (env->pregs[PR_VR] < 32)
255 ccs &= ~(V_FLAG | C_FLAG);
256 /* Set the N and Z flags accordingly. */
257 ccs |= (bset << 3) | (fz << 2);
258 return ccs;
261 static inline uint32_t evaluate_flags_writeback(CPUCRISState *env,
262 uint32_t flags, uint32_t ccs)
264 unsigned int x, z, mask;
266 /* Extended arithmetics, leave the z flag alone. */
267 x = env->cc_x;
268 mask = env->cc_mask | X_FLAG;
269 if (x) {
270 z = flags & Z_FLAG;
271 mask = mask & ~z;
273 flags &= mask;
275 /* all insn clear the x-flag except setf or clrf. */
276 ccs &= ~mask;
277 ccs |= flags;
278 return ccs;
281 uint32_t helper_evaluate_flags_muls(CPUCRISState *env,
282 uint32_t ccs, uint32_t res, uint32_t mof)
284 uint32_t flags = 0;
285 int64_t tmp;
286 int dneg;
288 dneg = ((int32_t)res) < 0;
290 tmp = mof;
291 tmp <<= 32;
292 tmp |= res;
293 if (tmp == 0)
294 flags |= Z_FLAG;
295 else if (tmp < 0)
296 flags |= N_FLAG;
297 if ((dneg && mof != -1)
298 || (!dneg && mof != 0))
299 flags |= V_FLAG;
300 return evaluate_flags_writeback(env, flags, ccs);
303 uint32_t helper_evaluate_flags_mulu(CPUCRISState *env,
304 uint32_t ccs, uint32_t res, uint32_t mof)
306 uint32_t flags = 0;
307 uint64_t tmp;
309 tmp = mof;
310 tmp <<= 32;
311 tmp |= res;
312 if (tmp == 0)
313 flags |= Z_FLAG;
314 else if (tmp >> 63)
315 flags |= N_FLAG;
316 if (mof)
317 flags |= V_FLAG;
319 return evaluate_flags_writeback(env, flags, ccs);
322 uint32_t helper_evaluate_flags_mcp(CPUCRISState *env, uint32_t ccs,
323 uint32_t src, uint32_t dst, uint32_t res)
325 uint32_t flags = 0;
327 src = src & 0x80000000;
328 dst = dst & 0x80000000;
330 if ((res & 0x80000000L) != 0L)
332 flags |= N_FLAG;
333 if (!src && !dst)
334 flags |= V_FLAG;
335 else if (src & dst)
336 flags |= R_FLAG;
338 else
340 if (res == 0L)
341 flags |= Z_FLAG;
342 if (src & dst)
343 flags |= V_FLAG;
344 if (dst | src)
345 flags |= R_FLAG;
348 return evaluate_flags_writeback(env, flags, ccs);
351 uint32_t helper_evaluate_flags_alu_4(CPUCRISState *env, uint32_t ccs,
352 uint32_t src, uint32_t dst, uint32_t res)
354 uint32_t flags = 0;
356 src = src & 0x80000000;
357 dst = dst & 0x80000000;
359 if ((res & 0x80000000L) != 0L)
361 flags |= N_FLAG;
362 if (!src && !dst)
363 flags |= V_FLAG;
364 else if (src & dst)
365 flags |= C_FLAG;
367 else
369 if (res == 0L)
370 flags |= Z_FLAG;
371 if (src & dst)
372 flags |= V_FLAG;
373 if (dst | src)
374 flags |= C_FLAG;
377 return evaluate_flags_writeback(env, flags, ccs);
380 uint32_t helper_evaluate_flags_sub_4(CPUCRISState *env, uint32_t ccs,
381 uint32_t src, uint32_t dst, uint32_t res)
383 uint32_t flags = 0;
385 src = (~src) & 0x80000000;
386 dst = dst & 0x80000000;
388 if ((res & 0x80000000L) != 0L)
390 flags |= N_FLAG;
391 if (!src && !dst)
392 flags |= V_FLAG;
393 else if (src & dst)
394 flags |= C_FLAG;
396 else
398 if (res == 0L)
399 flags |= Z_FLAG;
400 if (src & dst)
401 flags |= V_FLAG;
402 if (dst | src)
403 flags |= C_FLAG;
406 flags ^= C_FLAG;
407 return evaluate_flags_writeback(env, flags, ccs);
410 uint32_t helper_evaluate_flags_move_4(CPUCRISState *env,
411 uint32_t ccs, uint32_t res)
413 uint32_t flags = 0;
415 if ((int32_t)res < 0)
416 flags |= N_FLAG;
417 else if (res == 0L)
418 flags |= Z_FLAG;
420 return evaluate_flags_writeback(env, flags, ccs);
422 uint32_t helper_evaluate_flags_move_2(CPUCRISState *env,
423 uint32_t ccs, uint32_t res)
425 uint32_t flags = 0;
427 if ((int16_t)res < 0L)
428 flags |= N_FLAG;
429 else if (res == 0)
430 flags |= Z_FLAG;
432 return evaluate_flags_writeback(env, flags, ccs);
435 /* TODO: This is expensive. We could split things up and only evaluate part of
436 CCR on a need to know basis. For now, we simply re-evaluate everything. */
437 void helper_evaluate_flags(CPUCRISState *env)
439 uint32_t src, dst, res;
440 uint32_t flags = 0;
442 src = env->cc_src;
443 dst = env->cc_dest;
444 res = env->cc_result;
446 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
447 src = ~src;
449 /* Now, evaluate the flags. This stuff is based on
450 Per Zander's CRISv10 simulator. */
451 switch (env->cc_size)
453 case 1:
454 if ((res & 0x80L) != 0L)
456 flags |= N_FLAG;
457 if (((src & 0x80L) == 0L)
458 && ((dst & 0x80L) == 0L))
460 flags |= V_FLAG;
462 else if (((src & 0x80L) != 0L)
463 && ((dst & 0x80L) != 0L))
465 flags |= C_FLAG;
468 else
470 if ((res & 0xFFL) == 0L)
472 flags |= Z_FLAG;
474 if (((src & 0x80L) != 0L)
475 && ((dst & 0x80L) != 0L))
477 flags |= V_FLAG;
479 if ((dst & 0x80L) != 0L
480 || (src & 0x80L) != 0L)
482 flags |= C_FLAG;
485 break;
486 case 2:
487 if ((res & 0x8000L) != 0L)
489 flags |= N_FLAG;
490 if (((src & 0x8000L) == 0L)
491 && ((dst & 0x8000L) == 0L))
493 flags |= V_FLAG;
495 else if (((src & 0x8000L) != 0L)
496 && ((dst & 0x8000L) != 0L))
498 flags |= C_FLAG;
501 else
503 if ((res & 0xFFFFL) == 0L)
505 flags |= Z_FLAG;
507 if (((src & 0x8000L) != 0L)
508 && ((dst & 0x8000L) != 0L))
510 flags |= V_FLAG;
512 if ((dst & 0x8000L) != 0L
513 || (src & 0x8000L) != 0L)
515 flags |= C_FLAG;
518 break;
519 case 4:
520 if ((res & 0x80000000L) != 0L)
522 flags |= N_FLAG;
523 if (((src & 0x80000000L) == 0L)
524 && ((dst & 0x80000000L) == 0L))
526 flags |= V_FLAG;
528 else if (((src & 0x80000000L) != 0L) &&
529 ((dst & 0x80000000L) != 0L))
531 flags |= C_FLAG;
534 else
536 if (res == 0L)
537 flags |= Z_FLAG;
538 if (((src & 0x80000000L) != 0L)
539 && ((dst & 0x80000000L) != 0L))
540 flags |= V_FLAG;
541 if ((dst & 0x80000000L) != 0L
542 || (src & 0x80000000L) != 0L)
543 flags |= C_FLAG;
545 break;
546 default:
547 break;
550 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP)
551 flags ^= C_FLAG;
553 env->pregs[PR_CCS] = evaluate_flags_writeback(env, flags,
554 env->pregs[PR_CCS]);
557 void helper_top_evaluate_flags(CPUCRISState *env)
559 switch (env->cc_op)
561 case CC_OP_MCP:
562 env->pregs[PR_CCS] = helper_evaluate_flags_mcp(env,
563 env->pregs[PR_CCS], env->cc_src,
564 env->cc_dest, env->cc_result);
565 break;
566 case CC_OP_MULS:
567 env->pregs[PR_CCS] = helper_evaluate_flags_muls(env,
568 env->pregs[PR_CCS], env->cc_result,
569 env->pregs[PR_MOF]);
570 break;
571 case CC_OP_MULU:
572 env->pregs[PR_CCS] = helper_evaluate_flags_mulu(env,
573 env->pregs[PR_CCS], env->cc_result,
574 env->pregs[PR_MOF]);
575 break;
576 case CC_OP_MOVE:
577 case CC_OP_AND:
578 case CC_OP_OR:
579 case CC_OP_XOR:
580 case CC_OP_ASR:
581 case CC_OP_LSR:
582 case CC_OP_LSL:
583 switch (env->cc_size)
585 case 4:
586 env->pregs[PR_CCS] =
587 helper_evaluate_flags_move_4(env,
588 env->pregs[PR_CCS],
589 env->cc_result);
590 break;
591 case 2:
592 env->pregs[PR_CCS] =
593 helper_evaluate_flags_move_2(env,
594 env->pregs[PR_CCS],
595 env->cc_result);
596 break;
597 default:
598 helper_evaluate_flags(env);
599 break;
601 break;
602 case CC_OP_FLAGS:
603 /* live. */
604 break;
605 case CC_OP_SUB:
606 case CC_OP_CMP:
607 if (env->cc_size == 4)
608 env->pregs[PR_CCS] =
609 helper_evaluate_flags_sub_4(env,
610 env->pregs[PR_CCS],
611 env->cc_src, env->cc_dest,
612 env->cc_result);
613 else
614 helper_evaluate_flags(env);
615 break;
616 default:
618 switch (env->cc_size)
620 case 4:
621 env->pregs[PR_CCS] =
622 helper_evaluate_flags_alu_4(env,
623 env->pregs[PR_CCS],
624 env->cc_src, env->cc_dest,
625 env->cc_result);
626 break;
627 default:
628 helper_evaluate_flags(env);
629 break;
632 break;