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
26 #include "host-utils.h"
30 #if !defined(CONFIG_USER_ONLY)
32 #define MMUSUFFIX _mmu
35 #include "softmmu_template.h"
38 #include "softmmu_template.h"
41 #include "softmmu_template.h"
44 #include "softmmu_template.h"
46 /* Try to fill the TLB and return an exception if error. If retaddr is
47 NULL, it means that the function was called in C code (i.e. not
48 from generated code or from helper.c) */
49 /* XXX: fix it to restore all registers */
50 void tlb_fill (target_ulong addr
, int is_write
, int mmu_idx
, void *retaddr
)
57 /* XXX: hack to restore env in all cases, even if not called from
62 D(fprintf(logfile
, "%s pc=%x tpc=%x ra=%x\n", __func__
,
63 env
->pc
, env
->debug1
, retaddr
));
64 ret
= cpu_cris_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
, 1);
67 /* now we have a real cpu fault */
68 pc
= (unsigned long)retaddr
;
71 /* the PC is inside the translated code. It means that we have
72 a virtual CPU fault */
73 cpu_restore_state(tb
, env
, pc
, NULL
);
75 /* Evaluate flags after retranslation. */
76 helper_top_evaluate_flags();
86 void helper_raise_exception(uint32_t index
)
88 env
->exception_index
= index
;
92 void helper_tlb_flush_pid(uint32_t pid
)
94 #if !defined(CONFIG_USER_ONLY)
96 if (pid
!= (env
->pregs
[PR_PID
] & 0xff))
97 cris_mmu_flush_pid(env
, env
->pregs
[PR_PID
]);
101 void helper_spc_write(uint32_t new_spc
)
103 #if !defined(CONFIG_USER_ONLY)
104 tlb_flush_page(env
, env
->pregs
[PR_SPC
]);
105 tlb_flush_page(env
, new_spc
);
109 void helper_dump(uint32_t a0
, uint32_t a1
, uint32_t a2
)
111 (fprintf(logfile
, "%s: a0=%x a1=%x\n", __func__
, a0
, a1
));
114 /* Used by the tlb decoder. */
115 #define EXTRACT_FIELD(src, start, end) \
116 (((src) >> start) & ((1 << (end - start + 1)) - 1))
118 void helper_movl_sreg_reg (uint32_t sreg
, uint32_t reg
)
121 srs
= env
->pregs
[PR_SRS
];
123 env
->sregs
[srs
][sreg
] = env
->regs
[reg
];
125 #if !defined(CONFIG_USER_ONLY)
126 if (srs
== 1 || srs
== 2) {
128 /* Writes to tlb-hi write to mm_cause as a side
130 env
->sregs
[SFR_RW_MM_TLB_HI
] = env
->regs
[reg
];
131 env
->sregs
[SFR_R_MM_CAUSE
] = env
->regs
[reg
];
133 else if (sreg
== 5) {
140 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
145 /* We've just made a write to tlb_lo. */
146 lo
= env
->sregs
[SFR_RW_MM_TLB_LO
];
147 /* Writes are done via r_mm_cause. */
148 hi
= env
->sregs
[SFR_R_MM_CAUSE
];
150 vaddr
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].hi
,
152 vaddr
<<= TARGET_PAGE_BITS
;
153 tlb_v
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].lo
,
155 env
->tlbsets
[srs
- 1][set
][idx
].lo
= lo
;
156 env
->tlbsets
[srs
- 1][set
][idx
].hi
= hi
;
159 "tlb flush vaddr=%x v=%d pc=%x\n",
160 vaddr
, tlb_v
, env
->pc
));
161 tlb_flush_page(env
, vaddr
);
167 void helper_movl_reg_sreg (uint32_t reg
, uint32_t sreg
)
170 env
->pregs
[PR_SRS
] &= 3;
171 srs
= env
->pregs
[PR_SRS
];
173 #if !defined(CONFIG_USER_ONLY)
174 if (srs
== 1 || srs
== 2)
180 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
185 /* Update the mirror regs. */
186 hi
= env
->tlbsets
[srs
- 1][set
][idx
].hi
;
187 lo
= env
->tlbsets
[srs
- 1][set
][idx
].lo
;
188 env
->sregs
[SFR_RW_MM_TLB_HI
] = hi
;
189 env
->sregs
[SFR_RW_MM_TLB_LO
] = lo
;
192 env
->regs
[reg
] = env
->sregs
[srs
][sreg
];
195 static void cris_ccs_rshift(CPUState
*env
)
199 /* Apply the ccs shift. */
200 ccs
= env
->pregs
[PR_CCS
];
201 ccs
= (ccs
& 0xc0000000) | ((ccs
& 0x0fffffff) >> 10);
204 /* Enter user mode. */
205 env
->ksp
= env
->regs
[R_SP
];
206 env
->regs
[R_SP
] = env
->pregs
[PR_USP
];
209 env
->pregs
[PR_CCS
] = ccs
;
212 void helper_rfe(void)
214 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
216 D(fprintf(logfile
, "rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
217 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
221 cris_ccs_rshift(env
);
223 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
225 env
->pregs
[PR_CCS
] |= P_FLAG
;
228 void helper_rfn(void)
230 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
232 D(fprintf(logfile
, "rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
233 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
237 cris_ccs_rshift(env
);
239 /* Set the P_FLAG only if the R_FLAG is not set. */
241 env
->pregs
[PR_CCS
] |= P_FLAG
;
243 /* Always set the M flag. */
244 env
->pregs
[PR_CCS
] |= M_FLAG
;
247 uint32_t helper_lz(uint32_t t0
)
252 uint32_t helper_btst(uint32_t t0
, uint32_t t1
, uint32_t ccs
)
254 /* FIXME: clean this up. */
257 The N flag is set according to the selected bit in the dest reg.
258 The Z flag is set if the selected bit and all bits to the right are
260 The X flag is cleared.
261 Other flags are left untouched.
262 The destination reg is not affected.*/
263 unsigned int fz
, sbit
, bset
, mask
, masked_t0
;
266 bset
= !!(t0
& (1 << sbit
));
267 mask
= sbit
== 31 ? -1 : (1 << (sbit
+ 1)) - 1;
268 masked_t0
= t0
& mask
;
269 fz
= !(masked_t0
| bset
);
271 /* Clear the X, N and Z flags. */
272 ccs
= ccs
& ~(X_FLAG
| N_FLAG
| Z_FLAG
);
273 /* Set the N and Z flags accordingly. */
274 ccs
|= (bset
<< 3) | (fz
<< 2);
278 static inline uint32_t evaluate_flags_writeback(uint32_t flags
, uint32_t ccs
)
280 unsigned int x
, z
, mask
;
282 /* Extended arithmetics, leave the z flag alone. */
284 mask
= env
->cc_mask
| X_FLAG
;
291 /* all insn clear the x-flag except setf or clrf. */
297 uint32_t helper_evaluate_flags_muls(uint32_t ccs
, uint32_t res
, uint32_t mof
)
303 dneg
= ((int32_t)res
) < 0;
312 if ((dneg
&& mof
!= -1)
313 || (!dneg
&& mof
!= 0))
315 return evaluate_flags_writeback(flags
, ccs
);
318 uint32_t helper_evaluate_flags_mulu(uint32_t ccs
, uint32_t res
, uint32_t mof
)
333 return evaluate_flags_writeback(flags
, ccs
);
336 uint32_t helper_evaluate_flags_mcp(uint32_t ccs
,
337 uint32_t src
, uint32_t dst
, uint32_t res
)
341 src
= src
& 0x80000000;
342 dst
= dst
& 0x80000000;
344 if ((res
& 0x80000000L
) != 0L)
362 return evaluate_flags_writeback(flags
, ccs
);
365 uint32_t helper_evaluate_flags_alu_4(uint32_t ccs
,
366 uint32_t src
, uint32_t dst
, uint32_t res
)
370 src
= src
& 0x80000000;
371 dst
= dst
& 0x80000000;
373 if ((res
& 0x80000000L
) != 0L)
391 return evaluate_flags_writeback(flags
, ccs
);
394 uint32_t helper_evaluate_flags_sub_4(uint32_t ccs
,
395 uint32_t src
, uint32_t dst
, uint32_t res
)
399 src
= (~src
) & 0x80000000;
400 dst
= dst
& 0x80000000;
402 if ((res
& 0x80000000L
) != 0L)
421 return evaluate_flags_writeback(flags
, ccs
);
424 uint32_t helper_evaluate_flags_move_4(uint32_t ccs
, uint32_t res
)
428 if ((int32_t)res
< 0)
433 return evaluate_flags_writeback(flags
, ccs
);
435 uint32_t helper_evaluate_flags_move_2(uint32_t ccs
, uint32_t res
)
439 if ((int16_t)res
< 0L)
444 return evaluate_flags_writeback(flags
, ccs
);
447 /* TODO: This is expensive. We could split things up and only evaluate part of
448 CCR on a need to know basis. For now, we simply re-evaluate everything. */
449 void helper_evaluate_flags(void)
451 uint32_t src
, dst
, res
;
456 res
= env
->cc_result
;
458 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
461 /* Now, evaluate the flags. This stuff is based on
462 Per Zander's CRISv10 simulator. */
463 switch (env
->cc_size
)
466 if ((res
& 0x80L
) != 0L)
469 if (((src
& 0x80L
) == 0L)
470 && ((dst
& 0x80L
) == 0L))
474 else if (((src
& 0x80L
) != 0L)
475 && ((dst
& 0x80L
) != 0L))
482 if ((res
& 0xFFL
) == 0L)
486 if (((src
& 0x80L
) != 0L)
487 && ((dst
& 0x80L
) != 0L))
491 if ((dst
& 0x80L
) != 0L
492 || (src
& 0x80L
) != 0L)
499 if ((res
& 0x8000L
) != 0L)
502 if (((src
& 0x8000L
) == 0L)
503 && ((dst
& 0x8000L
) == 0L))
507 else if (((src
& 0x8000L
) != 0L)
508 && ((dst
& 0x8000L
) != 0L))
515 if ((res
& 0xFFFFL
) == 0L)
519 if (((src
& 0x8000L
) != 0L)
520 && ((dst
& 0x8000L
) != 0L))
524 if ((dst
& 0x8000L
) != 0L
525 || (src
& 0x8000L
) != 0L)
532 if ((res
& 0x80000000L
) != 0L)
535 if (((src
& 0x80000000L
) == 0L)
536 && ((dst
& 0x80000000L
) == 0L))
540 else if (((src
& 0x80000000L
) != 0L) &&
541 ((dst
& 0x80000000L
) != 0L))
550 if (((src
& 0x80000000L
) != 0L)
551 && ((dst
& 0x80000000L
) != 0L))
553 if ((dst
& 0x80000000L
) != 0L
554 || (src
& 0x80000000L
) != 0L)
562 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
565 env
->pregs
[PR_CCS
] = evaluate_flags_writeback(flags
, env
->pregs
[PR_CCS
]);
568 void helper_top_evaluate_flags(void)
573 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mcp(
574 env
->pregs
[PR_CCS
], env
->cc_src
,
575 env
->cc_dest
, env
->cc_result
);
578 env
->pregs
[PR_CCS
] = helper_evaluate_flags_muls(
579 env
->pregs
[PR_CCS
], env
->cc_result
,
583 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mulu(
584 env
->pregs
[PR_CCS
], env
->cc_result
,
594 switch (env
->cc_size
)
598 helper_evaluate_flags_move_4(
604 helper_evaluate_flags_move_2(
609 helper_evaluate_flags();
618 if (env
->cc_size
== 4)
620 helper_evaluate_flags_sub_4(
622 env
->cc_src
, env
->cc_dest
,
625 helper_evaluate_flags();
629 switch (env
->cc_size
)
633 helper_evaluate_flags_alu_4(
635 env
->cc_src
, env
->cc_dest
,
639 helper_evaluate_flags();