CRIS: Remove X flag from tb flags.
[qemu/qemu-JZ.git] / target-cris / op_helper.c
blob197fdcc8844483f3cad2405ac65e3488515532de
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"
26 #define MMUSUFFIX _mmu
27 #ifdef __s390__
28 # define GETPC() ((void*)((unsigned long)__builtin_return_address(0) & 0x7fffffffUL))
29 #else
30 # define GETPC() (__builtin_return_address(0))
31 #endif
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 #define D(x)
47 /* Try to fill the TLB and return an exception if error. If retaddr is
48 NULL, it means that the function was called in C code (i.e. not
49 from generated code or from helper.c) */
50 /* XXX: fix it to restore all registers */
51 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
53 TranslationBlock *tb;
54 CPUState *saved_env;
55 unsigned long pc;
56 int ret;
58 /* XXX: hack to restore env in all cases, even if not called from
59 generated code */
60 saved_env = env;
61 env = cpu_single_env;
63 D(fprintf(logfile, "%s pc=%x tpc=%x ra=%x\n", __func__,
64 env->pc, env->debug1, retaddr));
65 ret = cpu_cris_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
66 if (__builtin_expect(ret, 0)) {
67 if (retaddr) {
68 /* now we have a real cpu fault */
69 pc = (unsigned long)retaddr;
70 tb = tb_find_pc(pc);
71 if (tb) {
72 /* the PC is inside the translated code. It means that we have
73 a virtual CPU fault */
74 cpu_restore_state(tb, env, pc, NULL);
77 cpu_loop_exit();
79 env = saved_env;
82 void helper_raise_exception(uint32_t index)
84 env->exception_index = index;
85 cpu_loop_exit();
88 void helper_tlb_flush(void)
90 tlb_flush(env, 1);
93 void helper_dump(uint32_t a0, uint32_t a1)
95 (fprintf(logfile, "%s: a0=%x a1=%x\n", __func__, a0, a1));
98 void helper_dummy(void)
103 void helper_movl_sreg_reg (uint32_t sreg, uint32_t reg)
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] = T0;
116 env->sregs[SFR_R_MM_CAUSE] = T0;
118 else if (sreg == 5) {
119 uint32_t set;
120 uint32_t idx;
121 uint32_t lo, hi;
122 uint32_t vaddr;
124 vaddr = cris_mmu_tlb_latest_update(env);
125 D(fprintf(logfile, "tlb flush vaddr=%x\n", vaddr));
126 tlb_flush_page(env, vaddr);
128 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
129 set >>= 4;
130 set &= 3;
132 idx &= 15;
133 /* We've just made a write to tlb_lo. */
134 lo = env->sregs[SFR_RW_MM_TLB_LO];
135 /* Writes are done via r_mm_cause. */
136 hi = env->sregs[SFR_R_MM_CAUSE];
137 env->tlbsets[srs - 1][set][idx].lo = lo;
138 env->tlbsets[srs - 1][set][idx].hi = hi;
141 #endif
144 void helper_movl_reg_sreg (uint32_t reg, uint32_t sreg)
146 uint32_t srs;
147 env->pregs[PR_SRS] &= 3;
148 srs = env->pregs[PR_SRS];
150 #if !defined(CONFIG_USER_ONLY)
151 if (srs == 1 || srs == 2)
153 uint32_t set;
154 uint32_t idx;
155 uint32_t lo, hi;
157 idx = set = env->sregs[SFR_RW_MM_TLB_SEL];
158 set >>= 4;
159 set &= 3;
160 idx &= 15;
162 /* Update the mirror regs. */
163 hi = env->tlbsets[srs - 1][set][idx].hi;
164 lo = env->tlbsets[srs - 1][set][idx].lo;
165 env->sregs[SFR_RW_MM_TLB_HI] = hi;
166 env->sregs[SFR_RW_MM_TLB_LO] = lo;
168 #endif
169 env->regs[reg] = env->sregs[srs][sreg];
170 RETURN();
173 static void cris_ccs_rshift(CPUState *env)
175 uint32_t ccs;
177 /* Apply the ccs shift. */
178 ccs = env->pregs[PR_CCS];
179 ccs = (ccs & 0xc0000000) | ((ccs & 0x0fffffff) >> 10);
180 if (ccs & U_FLAG)
182 /* Enter user mode. */
183 env->ksp = env->regs[R_SP];
184 env->regs[R_SP] = env->pregs[PR_USP];
187 env->pregs[PR_CCS] = ccs;
190 void helper_rfe(void)
192 D(fprintf(logfile, "rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
193 env->pregs[PR_ERP], env->pregs[PR_PID],
194 env->pregs[PR_CCS],
195 env->btarget));
197 cris_ccs_rshift(env);
199 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
200 if (!(env->pregs[PR_CCS] & R_FLAG))
201 env->pregs[PR_CCS] |= P_FLAG;
204 void helper_store(uint32_t a0)
206 if (env->pregs[PR_CCS] & P_FLAG )
208 cpu_abort(env, "cond_store_failed! pc=%x a0=%x\n",
209 env->pc, a0);
213 void do_unassigned_access(target_phys_addr_t addr, int is_write, int is_exec,
214 int is_asi)
216 D(printf("%s addr=%x w=%d ex=%d asi=%d\n",
217 __func__, addr, is_write, is_exec, is_asi));
220 static void evaluate_flags_writeback(uint32_t flags)
222 int x;
224 /* Extended arithmetics, leave the z flag alone. */
225 env->debug3 = env->pregs[PR_CCS];
227 if (env->cc_x_live)
228 x = env->cc_x;
229 else
230 x = env->pregs[PR_CCS] & X_FLAG;
232 if ((x || env->cc_op == CC_OP_ADDC)
233 && flags & Z_FLAG)
234 env->cc_mask &= ~Z_FLAG;
236 /* all insn clear the x-flag except setf or clrf. */
237 env->pregs[PR_CCS] &= ~(env->cc_mask | X_FLAG);
238 flags &= env->cc_mask;
239 env->pregs[PR_CCS] |= flags;
242 void helper_evaluate_flags_muls(void)
244 uint32_t src;
245 uint32_t dst;
246 uint32_t res;
247 uint32_t flags = 0;
248 int64_t tmp;
249 int32_t mof;
250 int dneg;
252 src = env->cc_src;
253 dst = env->cc_dest;
254 res = env->cc_result;
256 dneg = ((int32_t)res) < 0;
258 mof = env->pregs[PR_MOF];
259 tmp = mof;
260 tmp <<= 32;
261 tmp |= res;
262 if (tmp == 0)
263 flags |= Z_FLAG;
264 else if (tmp < 0)
265 flags |= N_FLAG;
266 if ((dneg && mof != -1)
267 || (!dneg && mof != 0))
268 flags |= V_FLAG;
269 evaluate_flags_writeback(flags);
272 void helper_evaluate_flags_mulu(void)
274 uint32_t src;
275 uint32_t dst;
276 uint32_t res;
277 uint32_t flags = 0;
278 uint64_t tmp;
279 uint32_t mof;
281 src = env->cc_src;
282 dst = env->cc_dest;
283 res = env->cc_result;
285 mof = env->pregs[PR_MOF];
286 tmp = mof;
287 tmp <<= 32;
288 tmp |= res;
289 if (tmp == 0)
290 flags |= Z_FLAG;
291 else if (tmp >> 63)
292 flags |= N_FLAG;
293 if (mof)
294 flags |= V_FLAG;
296 evaluate_flags_writeback(flags);
299 void helper_evaluate_flags_mcp(void)
301 uint32_t src;
302 uint32_t dst;
303 uint32_t res;
304 uint32_t flags = 0;
306 src = env->cc_src;
307 dst = env->cc_dest;
308 res = env->cc_result;
310 if ((res & 0x80000000L) != 0L)
312 flags |= N_FLAG;
313 if (((src & 0x80000000L) == 0L)
314 && ((dst & 0x80000000L) == 0L))
316 flags |= V_FLAG;
318 else if (((src & 0x80000000L) != 0L) &&
319 ((dst & 0x80000000L) != 0L))
321 flags |= R_FLAG;
324 else
326 if (res == 0L)
327 flags |= Z_FLAG;
328 if (((src & 0x80000000L) != 0L)
329 && ((dst & 0x80000000L) != 0L))
330 flags |= V_FLAG;
331 if ((dst & 0x80000000L) != 0L
332 || (src & 0x80000000L) != 0L)
333 flags |= R_FLAG;
336 evaluate_flags_writeback(flags);
339 void helper_evaluate_flags_alu_4(void)
341 uint32_t src;
342 uint32_t dst;
343 uint32_t res;
344 uint32_t flags = 0;
346 src = env->cc_src;
347 dst = env->cc_dest;
348 res = env->cc_result;
350 if ((res & 0x80000000L) != 0L)
352 flags |= N_FLAG;
353 if (((src & 0x80000000L) == 0L)
354 && ((dst & 0x80000000L) == 0L))
356 flags |= V_FLAG;
358 else if (((src & 0x80000000L) != 0L) &&
359 ((dst & 0x80000000L) != 0L))
361 flags |= C_FLAG;
364 else
366 if (res == 0L)
367 flags |= Z_FLAG;
368 if (((src & 0x80000000L) != 0L)
369 && ((dst & 0x80000000L) != 0L))
370 flags |= V_FLAG;
371 if ((dst & 0x80000000L) != 0L
372 || (src & 0x80000000L) != 0L)
373 flags |= C_FLAG;
376 if (env->cc_op == CC_OP_SUB
377 || env->cc_op == CC_OP_CMP) {
378 flags ^= C_FLAG;
380 evaluate_flags_writeback(flags);
383 void helper_evaluate_flags_move_4 (void)
385 uint32_t src;
386 uint32_t res;
387 uint32_t flags = 0;
389 src = env->cc_src;
390 res = env->cc_result;
392 if ((int32_t)res < 0)
393 flags |= N_FLAG;
394 else if (res == 0L)
395 flags |= Z_FLAG;
397 evaluate_flags_writeback(flags);
399 void helper_evaluate_flags_move_2 (void)
401 uint32_t src;
402 uint32_t flags = 0;
403 uint16_t res;
405 src = env->cc_src;
406 res = env->cc_result;
408 if ((int16_t)res < 0L)
409 flags |= N_FLAG;
410 else if (res == 0)
411 flags |= Z_FLAG;
413 evaluate_flags_writeback(flags);
416 /* TODO: This is expensive. We could split things up and only evaluate part of
417 CCR on a need to know basis. For now, we simply re-evaluate everything. */
418 void helper_evaluate_flags (void)
420 uint32_t src;
421 uint32_t dst;
422 uint32_t res;
423 uint32_t flags = 0;
425 src = env->cc_src;
426 dst = env->cc_dest;
427 res = env->cc_result;
430 /* Now, evaluate the flags. This stuff is based on
431 Per Zander's CRISv10 simulator. */
432 switch (env->cc_size)
434 case 1:
435 if ((res & 0x80L) != 0L)
437 flags |= N_FLAG;
438 if (((src & 0x80L) == 0L)
439 && ((dst & 0x80L) == 0L))
441 flags |= V_FLAG;
443 else if (((src & 0x80L) != 0L)
444 && ((dst & 0x80L) != 0L))
446 flags |= C_FLAG;
449 else
451 if ((res & 0xFFL) == 0L)
453 flags |= Z_FLAG;
455 if (((src & 0x80L) != 0L)
456 && ((dst & 0x80L) != 0L))
458 flags |= V_FLAG;
460 if ((dst & 0x80L) != 0L
461 || (src & 0x80L) != 0L)
463 flags |= C_FLAG;
466 break;
467 case 2:
468 if ((res & 0x8000L) != 0L)
470 flags |= N_FLAG;
471 if (((src & 0x8000L) == 0L)
472 && ((dst & 0x8000L) == 0L))
474 flags |= V_FLAG;
476 else if (((src & 0x8000L) != 0L)
477 && ((dst & 0x8000L) != 0L))
479 flags |= C_FLAG;
482 else
484 if ((res & 0xFFFFL) == 0L)
486 flags |= Z_FLAG;
488 if (((src & 0x8000L) != 0L)
489 && ((dst & 0x8000L) != 0L))
491 flags |= V_FLAG;
493 if ((dst & 0x8000L) != 0L
494 || (src & 0x8000L) != 0L)
496 flags |= C_FLAG;
499 break;
500 case 4:
501 if ((res & 0x80000000L) != 0L)
503 flags |= N_FLAG;
504 if (((src & 0x80000000L) == 0L)
505 && ((dst & 0x80000000L) == 0L))
507 flags |= V_FLAG;
509 else if (((src & 0x80000000L) != 0L) &&
510 ((dst & 0x80000000L) != 0L))
512 flags |= C_FLAG;
515 else
517 if (res == 0L)
518 flags |= Z_FLAG;
519 if (((src & 0x80000000L) != 0L)
520 && ((dst & 0x80000000L) != 0L))
521 flags |= V_FLAG;
522 if ((dst & 0x80000000L) != 0L
523 || (src & 0x80000000L) != 0L)
524 flags |= C_FLAG;
526 break;
527 default:
528 break;
531 if (env->cc_op == CC_OP_SUB
532 || env->cc_op == CC_OP_CMP) {
533 flags ^= C_FLAG;
535 evaluate_flags_writeback(flags);