]>
git.proxmox.com Git - mirror_qemu.git/blob - target-cris/op_helper.c
0ce5f09fa9a1044eaf4a8a40c44cdf4f6e0590be
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/>.
23 #include "exec/helper-proto.h"
24 #include "qemu/host-utils.h"
26 //#define CRIS_OP_HELPER_DEBUG
29 #ifdef CRIS_OP_HELPER_DEBUG
31 #define D_LOG(...) qemu_log(__VA_ARGS__)
34 #define D_LOG(...) do { } while (0)
37 #if !defined(CONFIG_USER_ONLY)
38 #include "exec/softmmu_exec.h"
40 /* Try to fill the TLB and return an exception if error. If retaddr is
41 NULL, it means that the function was called in C code (i.e. not
42 from generated code or from helper.c) */
43 void tlb_fill(CPUState
*cs
, target_ulong addr
, int is_write
, int mmu_idx
,
46 CRISCPU
*cpu
= CRIS_CPU(cs
);
47 CPUCRISState
*env
= &cpu
->env
;
50 D_LOG("%s pc=%x tpc=%x ra=%p\n", __func__
,
51 env
->pc
, env
->pregs
[PR_EDA
], (void *)retaddr
);
52 ret
= cris_cpu_handle_mmu_fault(cs
, addr
, is_write
, mmu_idx
);
55 /* now we have a real cpu fault */
56 if (cpu_restore_state(cs
, retaddr
)) {
57 /* Evaluate flags after retranslation. */
58 helper_top_evaluate_flags(env
);
67 void helper_raise_exception(CPUCRISState
*env
, uint32_t index
)
69 CPUState
*cs
= CPU(cris_env_get_cpu(env
));
71 cs
->exception_index
= index
;
75 void helper_tlb_flush_pid(CPUCRISState
*env
, uint32_t pid
)
77 #if !defined(CONFIG_USER_ONLY)
79 if (pid
!= (env
->pregs
[PR_PID
] & 0xff))
80 cris_mmu_flush_pid(env
, env
->pregs
[PR_PID
]);
84 void helper_spc_write(CPUCRISState
*env
, uint32_t new_spc
)
86 #if !defined(CONFIG_USER_ONLY)
87 CRISCPU
*cpu
= cris_env_get_cpu(env
);
88 CPUState
*cs
= CPU(cpu
);
90 tlb_flush_page(cs
, env
->pregs
[PR_SPC
]);
91 tlb_flush_page(cs
, new_spc
);
95 void helper_dump(uint32_t a0
, uint32_t a1
, uint32_t a2
)
97 qemu_log("%s: a0=%x a1=%x\n", __func__
, a0
, a1
);
100 /* Used by the tlb decoder. */
101 #define EXTRACT_FIELD(src, start, end) \
102 (((src) >> start) & ((1 << (end - start + 1)) - 1))
104 void helper_movl_sreg_reg(CPUCRISState
*env
, uint32_t sreg
, uint32_t reg
)
106 #if !defined(CONFIG_USER_ONLY)
107 CRISCPU
*cpu
= cris_env_get_cpu(env
);
110 srs
= env
->pregs
[PR_SRS
];
112 env
->sregs
[srs
][sreg
] = env
->regs
[reg
];
114 #if !defined(CONFIG_USER_ONLY)
115 if (srs
== 1 || srs
== 2) {
117 /* Writes to tlb-hi write to mm_cause as a side
119 env
->sregs
[SFR_RW_MM_TLB_HI
] = env
->regs
[reg
];
120 env
->sregs
[SFR_R_MM_CAUSE
] = env
->regs
[reg
];
122 else if (sreg
== 5) {
129 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
134 /* We've just made a write to tlb_lo. */
135 lo
= env
->sregs
[SFR_RW_MM_TLB_LO
];
136 /* Writes are done via r_mm_cause. */
137 hi
= env
->sregs
[SFR_R_MM_CAUSE
];
139 vaddr
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].hi
,
141 vaddr
<<= TARGET_PAGE_BITS
;
142 tlb_v
= EXTRACT_FIELD(env
->tlbsets
[srs
-1][set
][idx
].lo
,
144 env
->tlbsets
[srs
- 1][set
][idx
].lo
= lo
;
145 env
->tlbsets
[srs
- 1][set
][idx
].hi
= hi
;
147 D_LOG("tlb flush vaddr=%x v=%d pc=%x\n",
148 vaddr
, tlb_v
, env
->pc
);
150 tlb_flush_page(CPU(cpu
), vaddr
);
157 void helper_movl_reg_sreg(CPUCRISState
*env
, uint32_t reg
, uint32_t sreg
)
160 env
->pregs
[PR_SRS
] &= 3;
161 srs
= env
->pregs
[PR_SRS
];
163 #if !defined(CONFIG_USER_ONLY)
164 if (srs
== 1 || srs
== 2)
170 idx
= set
= env
->sregs
[SFR_RW_MM_TLB_SEL
];
175 /* Update the mirror regs. */
176 hi
= env
->tlbsets
[srs
- 1][set
][idx
].hi
;
177 lo
= env
->tlbsets
[srs
- 1][set
][idx
].lo
;
178 env
->sregs
[SFR_RW_MM_TLB_HI
] = hi
;
179 env
->sregs
[SFR_RW_MM_TLB_LO
] = lo
;
182 env
->regs
[reg
] = env
->sregs
[srs
][sreg
];
185 static void cris_ccs_rshift(CPUCRISState
*env
)
189 /* Apply the ccs shift. */
190 ccs
= env
->pregs
[PR_CCS
];
191 ccs
= (ccs
& 0xc0000000) | ((ccs
& 0x0fffffff) >> 10);
194 /* Enter user mode. */
195 env
->ksp
= env
->regs
[R_SP
];
196 env
->regs
[R_SP
] = env
->pregs
[PR_USP
];
199 env
->pregs
[PR_CCS
] = ccs
;
202 void helper_rfe(CPUCRISState
*env
)
204 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
206 D_LOG("rfe: erp=%x pid=%x ccs=%x btarget=%x\n",
207 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
211 cris_ccs_rshift(env
);
213 /* RFE sets the P_FLAG only if the R_FLAG is not set. */
215 env
->pregs
[PR_CCS
] |= P_FLAG
;
218 void helper_rfn(CPUCRISState
*env
)
220 int rflag
= env
->pregs
[PR_CCS
] & R_FLAG
;
222 D_LOG("rfn: erp=%x pid=%x ccs=%x btarget=%x\n",
223 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
227 cris_ccs_rshift(env
);
229 /* Set the P_FLAG only if the R_FLAG is not set. */
231 env
->pregs
[PR_CCS
] |= P_FLAG
;
233 /* Always set the M flag. */
234 env
->pregs
[PR_CCS
] |= M_FLAG_V32
;
237 uint32_t helper_lz(uint32_t t0
)
242 uint32_t helper_btst(CPUCRISState
*env
, uint32_t t0
, uint32_t t1
, uint32_t ccs
)
244 /* FIXME: clean this up. */
247 The N flag is set according to the selected bit in the dest reg.
248 The Z flag is set if the selected bit and all bits to the right are
250 The X flag is cleared.
251 Other flags are left untouched.
252 The destination reg is not affected.*/
253 unsigned int fz
, sbit
, bset
, mask
, masked_t0
;
256 bset
= !!(t0
& (1 << sbit
));
257 mask
= sbit
== 31 ? -1 : (1 << (sbit
+ 1)) - 1;
258 masked_t0
= t0
& mask
;
259 fz
= !(masked_t0
| bset
);
261 /* Clear the X, N and Z flags. */
262 ccs
= ccs
& ~(X_FLAG
| N_FLAG
| Z_FLAG
);
263 if (env
->pregs
[PR_VR
] < 32)
264 ccs
&= ~(V_FLAG
| C_FLAG
);
265 /* Set the N and Z flags accordingly. */
266 ccs
|= (bset
<< 3) | (fz
<< 2);
270 static inline uint32_t evaluate_flags_writeback(CPUCRISState
*env
,
271 uint32_t flags
, uint32_t ccs
)
273 unsigned int x
, z
, mask
;
275 /* Extended arithmetics, leave the z flag alone. */
277 mask
= env
->cc_mask
| X_FLAG
;
284 /* all insn clear the x-flag except setf or clrf. */
290 uint32_t helper_evaluate_flags_muls(CPUCRISState
*env
,
291 uint32_t ccs
, uint32_t res
, uint32_t mof
)
297 dneg
= ((int32_t)res
) < 0;
306 if ((dneg
&& mof
!= -1)
307 || (!dneg
&& mof
!= 0))
309 return evaluate_flags_writeback(env
, flags
, ccs
);
312 uint32_t helper_evaluate_flags_mulu(CPUCRISState
*env
,
313 uint32_t ccs
, uint32_t res
, uint32_t mof
)
328 return evaluate_flags_writeback(env
, flags
, ccs
);
331 uint32_t helper_evaluate_flags_mcp(CPUCRISState
*env
, uint32_t ccs
,
332 uint32_t src
, uint32_t dst
, uint32_t res
)
336 src
= src
& 0x80000000;
337 dst
= dst
& 0x80000000;
339 if ((res
& 0x80000000L
) != 0L)
357 return evaluate_flags_writeback(env
, flags
, ccs
);
360 uint32_t helper_evaluate_flags_alu_4(CPUCRISState
*env
, uint32_t ccs
,
361 uint32_t src
, uint32_t dst
, uint32_t res
)
365 src
= src
& 0x80000000;
366 dst
= dst
& 0x80000000;
368 if ((res
& 0x80000000L
) != 0L)
386 return evaluate_flags_writeback(env
, flags
, ccs
);
389 uint32_t helper_evaluate_flags_sub_4(CPUCRISState
*env
, uint32_t ccs
,
390 uint32_t src
, uint32_t dst
, uint32_t res
)
394 src
= (~src
) & 0x80000000;
395 dst
= dst
& 0x80000000;
397 if ((res
& 0x80000000L
) != 0L)
416 return evaluate_flags_writeback(env
, flags
, ccs
);
419 uint32_t helper_evaluate_flags_move_4(CPUCRISState
*env
,
420 uint32_t ccs
, uint32_t res
)
424 if ((int32_t)res
< 0)
429 return evaluate_flags_writeback(env
, flags
, ccs
);
431 uint32_t helper_evaluate_flags_move_2(CPUCRISState
*env
,
432 uint32_t ccs
, uint32_t res
)
436 if ((int16_t)res
< 0L)
441 return evaluate_flags_writeback(env
, flags
, ccs
);
444 /* TODO: This is expensive. We could split things up and only evaluate part of
445 CCR on a need to know basis. For now, we simply re-evaluate everything. */
446 void helper_evaluate_flags(CPUCRISState
*env
)
448 uint32_t src
, dst
, res
;
453 res
= env
->cc_result
;
455 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
458 /* Now, evaluate the flags. This stuff is based on
459 Per Zander's CRISv10 simulator. */
460 switch (env
->cc_size
)
463 if ((res
& 0x80L
) != 0L)
466 if (((src
& 0x80L
) == 0L)
467 && ((dst
& 0x80L
) == 0L))
471 else if (((src
& 0x80L
) != 0L)
472 && ((dst
& 0x80L
) != 0L))
479 if ((res
& 0xFFL
) == 0L)
483 if (((src
& 0x80L
) != 0L)
484 && ((dst
& 0x80L
) != 0L))
488 if ((dst
& 0x80L
) != 0L
489 || (src
& 0x80L
) != 0L)
496 if ((res
& 0x8000L
) != 0L)
499 if (((src
& 0x8000L
) == 0L)
500 && ((dst
& 0x8000L
) == 0L))
504 else if (((src
& 0x8000L
) != 0L)
505 && ((dst
& 0x8000L
) != 0L))
512 if ((res
& 0xFFFFL
) == 0L)
516 if (((src
& 0x8000L
) != 0L)
517 && ((dst
& 0x8000L
) != 0L))
521 if ((dst
& 0x8000L
) != 0L
522 || (src
& 0x8000L
) != 0L)
529 if ((res
& 0x80000000L
) != 0L)
532 if (((src
& 0x80000000L
) == 0L)
533 && ((dst
& 0x80000000L
) == 0L))
537 else if (((src
& 0x80000000L
) != 0L) &&
538 ((dst
& 0x80000000L
) != 0L))
547 if (((src
& 0x80000000L
) != 0L)
548 && ((dst
& 0x80000000L
) != 0L))
550 if ((dst
& 0x80000000L
) != 0L
551 || (src
& 0x80000000L
) != 0L)
559 if (env
->cc_op
== CC_OP_SUB
|| env
->cc_op
== CC_OP_CMP
)
562 env
->pregs
[PR_CCS
] = evaluate_flags_writeback(env
, flags
,
566 void helper_top_evaluate_flags(CPUCRISState
*env
)
571 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mcp(env
,
572 env
->pregs
[PR_CCS
], env
->cc_src
,
573 env
->cc_dest
, env
->cc_result
);
576 env
->pregs
[PR_CCS
] = helper_evaluate_flags_muls(env
,
577 env
->pregs
[PR_CCS
], env
->cc_result
,
581 env
->pregs
[PR_CCS
] = helper_evaluate_flags_mulu(env
,
582 env
->pregs
[PR_CCS
], env
->cc_result
,
592 switch (env
->cc_size
)
596 helper_evaluate_flags_move_4(env
,
602 helper_evaluate_flags_move_2(env
,
607 helper_evaluate_flags(env
);
616 if (env
->cc_size
== 4)
618 helper_evaluate_flags_sub_4(env
,
620 env
->cc_src
, env
->cc_dest
,
623 helper_evaluate_flags(env
);
627 switch (env
->cc_size
)
631 helper_evaluate_flags_alu_4(env
,
633 env
->cc_src
, env
->cc_dest
,
637 helper_evaluate_flags(env
);