qapi: Improve reporting of invalid flags
[qemu/ar7.git] / target / cris / op_helper.c
blob6b1e7ae4a8dfcc9b8bbad446dba4e6980f4858af
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 void helper_raise_exception(CPUCRISState *env, uint32_t index)
42 CPUState *cs = env_cpu(env);
44 cs->exception_index = index;
45 cpu_loop_exit(cs);
48 void helper_tlb_flush_pid(CPUCRISState *env, uint32_t pid)
50 #if !defined(CONFIG_USER_ONLY)
51 pid &= 0xff;
52 if (pid != (env->pregs[PR_PID] & 0xff)) {
53 cris_mmu_flush_pid(env, env->pregs[PR_PID]);
55 #endif
58 void helper_spc_write(CPUCRISState *env, uint32_t new_spc)
60 #if !defined(CONFIG_USER_ONLY)
61 CPUState *cs = env_cpu(env);
63 tlb_flush_page(cs, env->pregs[PR_SPC]);
64 tlb_flush_page(cs, new_spc);
65 #endif
68 /* Used by the tlb decoder. */
69 #define EXTRACT_FIELD(src, start, end) \
70 (((src) >> start) & ((1 << (end - start + 1)) - 1))
72 void helper_movl_sreg_reg(CPUCRISState *env, uint32_t sreg, uint32_t reg)
74 uint32_t srs;
75 srs = env->pregs[PR_SRS];
76 srs &= 3;
77 env->sregs[srs][sreg] = env->regs[reg];
79 #if !defined(CONFIG_USER_ONLY)
80 if (srs == 1 || srs == 2) {
81 if (sreg == 6) {
82 /* Writes to tlb-hi write to mm_cause as a side effect. */
83 env->sregs[SFR_RW_MM_TLB_HI] = env->regs[reg];
84 env->sregs[SFR_R_MM_CAUSE] = env->regs[reg];
85 } else if (sreg == 5) {
86 uint32_t set;
87 uint32_t idx;
88 uint32_t lo, hi;
89 uint32_t vaddr;
90 int tlb_v;
92 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
93 set >>= 4;
94 set &= 3;
96 idx &= 15;
97 /* We've just made a write to tlb_lo. */
98 lo = env->sregs[SFR_RW_MM_TLB_LO];
99 /* Writes are done via r_mm_cause. */
100 hi = env->sregs[SFR_R_MM_CAUSE];
102 vaddr = EXTRACT_FIELD(env->tlbsets[srs - 1][set][idx].hi, 13, 31);
103 vaddr <<= TARGET_PAGE_BITS;
104 tlb_v = EXTRACT_FIELD(env->tlbsets[srs - 1][set][idx].lo, 3, 3);
105 env->tlbsets[srs - 1][set][idx].lo = lo;
106 env->tlbsets[srs - 1][set][idx].hi = hi;
108 D_LOG("tlb flush vaddr=%x v=%d pc=%x\n",
109 vaddr, tlb_v, env->pc);
110 if (tlb_v) {
111 tlb_flush_page(env_cpu(env), vaddr);
115 #endif
118 void helper_movl_reg_sreg(CPUCRISState *env, uint32_t reg, uint32_t sreg)
120 uint32_t srs;
121 env->pregs[PR_SRS] &= 3;
122 srs = env->pregs[PR_SRS];
124 #if !defined(CONFIG_USER_ONLY)
125 if (srs == 1 || srs == 2) {
126 uint32_t set;
127 uint32_t idx;
128 uint32_t lo, hi;
130 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
131 set >>= 4;
132 set &= 3;
133 idx &= 15;
135 /* Update the mirror regs. */
136 hi = env->tlbsets[srs - 1][set][idx].hi;
137 lo = env->tlbsets[srs - 1][set][idx].lo;
138 env->sregs[SFR_RW_MM_TLB_HI] = hi;
139 env->sregs[SFR_RW_MM_TLB_LO] = lo;
141 #endif
142 env->regs[reg] = env->sregs[srs][sreg];
145 static void cris_ccs_rshift(CPUCRISState *env)
147 uint32_t ccs;
149 /* Apply the ccs shift. */
150 ccs = env->pregs[PR_CCS];
151 ccs = (ccs & 0xc0000000) | ((ccs & 0x0fffffff) >> 10);
152 if (ccs & U_FLAG) {
153 /* Enter user mode. */
154 env->ksp = env->regs[R_SP];
155 env->regs[R_SP] = env->pregs[PR_USP];
158 env->pregs[PR_CCS] = ccs;
161 void helper_rfe(CPUCRISState *env)
163 int rflag = env->pregs[PR_CCS] & R_FLAG;
165 D_LOG("rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
166 env->pregs[PR_ERP], env->pregs[PR_PID],
167 env->pregs[PR_CCS],
168 env->btarget);
170 cris_ccs_rshift(env);
172 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
173 if (!rflag) {
174 env->pregs[PR_CCS] |= P_FLAG;
178 void helper_rfn(CPUCRISState *env)
180 int rflag = env->pregs[PR_CCS] & R_FLAG;
182 D_LOG("rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
183 env->pregs[PR_ERP], env->pregs[PR_PID],
184 env->pregs[PR_CCS],
185 env->btarget);
187 cris_ccs_rshift(env);
189 /* Set the P_FLAG only if the R_FLAG is not set. */
190 if (!rflag) {
191 env->pregs[PR_CCS] |= P_FLAG;
194 /* Always set the M flag. */
195 env->pregs[PR_CCS] |= M_FLAG_V32;
198 uint32_t helper_btst(CPUCRISState *env, uint32_t t0, uint32_t t1, uint32_t ccs)
200 /* FIXME: clean this up. */
203 * des ref:
204 * The N flag is set according to the selected bit in the dest reg.
205 * The Z flag is set if the selected bit and all bits to the right are
206 * zero.
207 * The X flag is cleared.
208 * Other flags are left untouched.
209 * The destination reg is not affected.
211 unsigned int fz, sbit, bset, mask, masked_t0;
213 sbit = t1 & 31;
214 bset = !!(t0 & (1 << sbit));
215 mask = sbit == 31 ? -1 : (1 << (sbit + 1)) - 1;
216 masked_t0 = t0 & mask;
217 fz = !(masked_t0 | bset);
219 /* Clear the X, N and Z flags. */
220 ccs = ccs & ~(X_FLAG | N_FLAG | Z_FLAG);
221 if (env->pregs[PR_VR] < 32) {
222 ccs &= ~(V_FLAG | C_FLAG);
224 /* Set the N and Z flags accordingly. */
225 ccs |= (bset << 3) | (fz << 2);
226 return ccs;
229 static inline uint32_t evaluate_flags_writeback(CPUCRISState *env,
230 uint32_t flags, uint32_t ccs)
232 unsigned int x, z, mask;
234 /* Extended arithmetics, leave the z flag alone. */
235 x = env->cc_x;
236 mask = env->cc_mask | X_FLAG;
237 if (x) {
238 z = flags & Z_FLAG;
239 mask = mask & ~z;
241 flags &= mask;
243 /* all insn clear the x-flag except setf or clrf. */
244 ccs &= ~mask;
245 ccs |= flags;
246 return ccs;
249 uint32_t helper_evaluate_flags_muls(CPUCRISState *env,
250 uint32_t ccs, uint32_t res, uint32_t mof)
252 uint32_t flags = 0;
253 int64_t tmp;
254 int dneg;
256 dneg = ((int32_t)res) < 0;
258 tmp = mof;
259 tmp <<= 32;
260 tmp |= res;
261 if (tmp == 0) {
262 flags |= Z_FLAG;
263 } else if (tmp < 0) {
264 flags |= N_FLAG;
266 if ((dneg && mof != -1) || (!dneg && mof != 0)) {
267 flags |= V_FLAG;
269 return evaluate_flags_writeback(env, flags, ccs);
272 uint32_t helper_evaluate_flags_mulu(CPUCRISState *env,
273 uint32_t ccs, uint32_t res, uint32_t mof)
275 uint32_t flags = 0;
276 uint64_t tmp;
278 tmp = mof;
279 tmp <<= 32;
280 tmp |= res;
281 if (tmp == 0) {
282 flags |= Z_FLAG;
283 } else if (tmp >> 63) {
284 flags |= N_FLAG;
286 if (mof) {
287 flags |= V_FLAG;
290 return evaluate_flags_writeback(env, flags, ccs);
293 uint32_t helper_evaluate_flags_mcp(CPUCRISState *env, uint32_t ccs,
294 uint32_t src, uint32_t dst, uint32_t res)
296 uint32_t flags = 0;
298 src = src & 0x80000000;
299 dst = dst & 0x80000000;
301 if ((res & 0x80000000L) != 0L) {
302 flags |= N_FLAG;
303 if (!src && !dst) {
304 flags |= V_FLAG;
305 } else if (src & dst) {
306 flags |= R_FLAG;
308 } else {
309 if (res == 0L) {
310 flags |= Z_FLAG;
312 if (src & dst) {
313 flags |= V_FLAG;
315 if (dst | src) {
316 flags |= R_FLAG;
320 return evaluate_flags_writeback(env, flags, ccs);
323 uint32_t helper_evaluate_flags_alu_4(CPUCRISState *env, uint32_t ccs,
324 uint32_t src, uint32_t dst, uint32_t res)
326 uint32_t flags = 0;
328 src = src & 0x80000000;
329 dst = dst & 0x80000000;
331 if ((res & 0x80000000L) != 0L) {
332 flags |= N_FLAG;
333 if (!src && !dst) {
334 flags |= V_FLAG;
335 } else if (src & dst) {
336 flags |= C_FLAG;
338 } else {
339 if (res == 0L) {
340 flags |= Z_FLAG;
342 if (src & dst) {
343 flags |= V_FLAG;
345 if (dst | src) {
346 flags |= C_FLAG;
350 return evaluate_flags_writeback(env, flags, ccs);
353 uint32_t helper_evaluate_flags_sub_4(CPUCRISState *env, uint32_t ccs,
354 uint32_t src, uint32_t dst, uint32_t res)
356 uint32_t flags = 0;
358 src = (~src) & 0x80000000;
359 dst = dst & 0x80000000;
361 if ((res & 0x80000000L) != 0L) {
362 flags |= N_FLAG;
363 if (!src && !dst) {
364 flags |= V_FLAG;
365 } else if (src & dst) {
366 flags |= C_FLAG;
368 } else {
369 if (res == 0L) {
370 flags |= Z_FLAG;
372 if (src & dst) {
373 flags |= V_FLAG;
375 if (dst | src) {
376 flags |= C_FLAG;
380 flags ^= C_FLAG;
381 return evaluate_flags_writeback(env, flags, ccs);
384 uint32_t helper_evaluate_flags_move_4(CPUCRISState *env,
385 uint32_t ccs, uint32_t res)
387 uint32_t flags = 0;
389 if ((int32_t)res < 0) {
390 flags |= N_FLAG;
391 } else if (res == 0L) {
392 flags |= Z_FLAG;
395 return evaluate_flags_writeback(env, flags, ccs);
398 uint32_t helper_evaluate_flags_move_2(CPUCRISState *env,
399 uint32_t ccs, uint32_t res)
401 uint32_t flags = 0;
403 if ((int16_t)res < 0L) {
404 flags |= N_FLAG;
405 } else if (res == 0) {
406 flags |= Z_FLAG;
409 return evaluate_flags_writeback(env, flags, ccs);
413 * TODO: This is expensive. We could split things up and only evaluate part of
414 * CCR on a need to know basis. For now, we simply re-evaluate everything.
416 void helper_evaluate_flags(CPUCRISState *env)
418 uint32_t src, dst, res;
419 uint32_t flags = 0;
421 src = env->cc_src;
422 dst = env->cc_dest;
423 res = env->cc_result;
425 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP) {
426 src = ~src;
430 * Now, evaluate the flags. This stuff is based on
431 * Per Zander's CRISv10 simulator.
433 switch (env->cc_size) {
434 case 1:
435 if ((res & 0x80L) != 0L) {
436 flags |= N_FLAG;
437 if (((src & 0x80L) == 0L) && ((dst & 0x80L) == 0L)) {
438 flags |= V_FLAG;
439 } else if (((src & 0x80L) != 0L) && ((dst & 0x80L) != 0L)) {
440 flags |= C_FLAG;
442 } else {
443 if ((res & 0xFFL) == 0L) {
444 flags |= Z_FLAG;
446 if (((src & 0x80L) != 0L) && ((dst & 0x80L) != 0L)) {
447 flags |= V_FLAG;
449 if ((dst & 0x80L) != 0L || (src & 0x80L) != 0L) {
450 flags |= C_FLAG;
453 break;
454 case 2:
455 if ((res & 0x8000L) != 0L) {
456 flags |= N_FLAG;
457 if (((src & 0x8000L) == 0L) && ((dst & 0x8000L) == 0L)) {
458 flags |= V_FLAG;
459 } else if (((src & 0x8000L) != 0L) && ((dst & 0x8000L) != 0L)) {
460 flags |= C_FLAG;
462 } else {
463 if ((res & 0xFFFFL) == 0L) {
464 flags |= Z_FLAG;
466 if (((src & 0x8000L) != 0L) && ((dst & 0x8000L) != 0L)) {
467 flags |= V_FLAG;
469 if ((dst & 0x8000L) != 0L || (src & 0x8000L) != 0L) {
470 flags |= C_FLAG;
473 break;
474 case 4:
475 if ((res & 0x80000000L) != 0L) {
476 flags |= N_FLAG;
477 if (((src & 0x80000000L) == 0L) && ((dst & 0x80000000L) == 0L)) {
478 flags |= V_FLAG;
479 } else if (((src & 0x80000000L) != 0L) &&
480 ((dst & 0x80000000L) != 0L)) {
481 flags |= C_FLAG;
483 } else {
484 if (res == 0L) {
485 flags |= Z_FLAG;
487 if (((src & 0x80000000L) != 0L) && ((dst & 0x80000000L) != 0L)) {
488 flags |= V_FLAG;
490 if ((dst & 0x80000000L) != 0L || (src & 0x80000000L) != 0L) {
491 flags |= C_FLAG;
494 break;
495 default:
496 break;
499 if (env->cc_op == CC_OP_SUB || env->cc_op == CC_OP_CMP) {
500 flags ^= C_FLAG;
503 env->pregs[PR_CCS] = evaluate_flags_writeback(env, flags,
504 env->pregs[PR_CCS]);
507 void helper_top_evaluate_flags(CPUCRISState *env)
509 switch (env->cc_op) {
510 case CC_OP_MCP:
511 env->pregs[PR_CCS]
512 = helper_evaluate_flags_mcp(env, env->pregs[PR_CCS],
513 env->cc_src, env->cc_dest,
514 env->cc_result);
515 break;
516 case CC_OP_MULS:
517 env->pregs[PR_CCS]
518 = helper_evaluate_flags_muls(env, env->pregs[PR_CCS],
519 env->cc_result, env->pregs[PR_MOF]);
520 break;
521 case CC_OP_MULU:
522 env->pregs[PR_CCS]
523 = helper_evaluate_flags_mulu(env, env->pregs[PR_CCS],
524 env->cc_result, env->pregs[PR_MOF]);
525 break;
526 case CC_OP_MOVE:
527 case CC_OP_AND:
528 case CC_OP_OR:
529 case CC_OP_XOR:
530 case CC_OP_ASR:
531 case CC_OP_LSR:
532 case CC_OP_LSL:
533 switch (env->cc_size) {
534 case 4:
535 env->pregs[PR_CCS] =
536 helper_evaluate_flags_move_4(env,
537 env->pregs[PR_CCS],
538 env->cc_result);
539 break;
540 case 2:
541 env->pregs[PR_CCS] =
542 helper_evaluate_flags_move_2(env,
543 env->pregs[PR_CCS],
544 env->cc_result);
545 break;
546 default:
547 helper_evaluate_flags(env);
548 break;
550 break;
551 case CC_OP_FLAGS:
552 /* live. */
553 break;
554 case CC_OP_SUB:
555 case CC_OP_CMP:
556 if (env->cc_size == 4) {
557 env->pregs[PR_CCS] =
558 helper_evaluate_flags_sub_4(env,
559 env->pregs[PR_CCS],
560 env->cc_src, env->cc_dest,
561 env->cc_result);
562 } else {
563 helper_evaluate_flags(env);
565 break;
566 default:
567 switch (env->cc_size) {
568 case 4:
569 env->pregs[PR_CCS] =
570 helper_evaluate_flags_alu_4(env,
571 env->pregs[PR_CCS],
572 env->cc_src, env->cc_dest,
573 env->cc_result);
574 break;
575 default:
576 helper_evaluate_flags(env);
577 break;
579 break;