Merge tag 'v3.1.0-rc0'
[qemu/ar7.git] / target / arm / op_helper.c
blob783d658969f6386f710630a5a17de842ffbe1be7
1 /*
2 * ARM helper routines
4 * Copyright (c) 2005-2007 CodeSourcery, LLC
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
20 #include "qemu/log.h"
21 #include "qemu/main-loop.h"
22 #include "cpu.h"
23 #include "exec/helper-proto.h"
24 #include "internals.h"
25 #include "exec/exec-all.h"
26 #include "exec/cpu_ldst.h"
28 #define SIGNBIT (uint32_t)0x80000000
29 #define SIGNBIT64 ((uint64_t)1 << 63)
31 QEMU_NORETURN
32 void raise_exception(CPUARMState *env, uint32_t excp,
33 uint32_t syndrome, uint32_t target_el)
35 CPUState *cs = CPU(arm_env_get_cpu(env));
37 if ((env->cp15.hcr_el2 & HCR_TGE) &&
38 target_el == 1 && !arm_is_secure(env)) {
40 * Redirect NS EL1 exceptions to NS EL2. These are reported with
41 * their original syndrome register value, with the exception of
42 * SIMD/FP access traps, which are reported as uncategorized
43 * (see DDI0478C.a D1.10.4)
45 target_el = 2;
46 if (syn_get_ec(syndrome) == EC_ADVSIMDFPACCESSTRAP) {
47 syndrome = syn_uncategorized();
51 assert(!excp_is_internal(excp));
52 cs->exception_index = excp;
53 env->exception.syndrome = syndrome;
54 env->exception.target_el = target_el;
55 cpu_loop_exit(cs);
58 static int exception_target_el(CPUARMState *env)
60 int target_el = MAX(1, arm_current_el(env));
62 /* No such thing as secure EL1 if EL3 is aarch32, so update the target EL
63 * to EL3 in this case.
65 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3) && target_el == 1) {
66 target_el = 3;
69 return target_el;
72 uint32_t HELPER(neon_tbl)(uint32_t ireg, uint32_t def, void *vn,
73 uint32_t maxindex)
75 uint32_t val, shift;
76 uint64_t *table = vn;
78 val = 0;
79 for (shift = 0; shift < 32; shift += 8) {
80 uint32_t index = (ireg >> shift) & 0xff;
81 if (index < maxindex) {
82 uint32_t tmp = (table[index >> 3] >> ((index & 7) << 3)) & 0xff;
83 val |= tmp << shift;
84 } else {
85 val |= def & (0xff << shift);
88 return val;
91 #if !defined(CONFIG_USER_ONLY)
93 static inline uint32_t merge_syn_data_abort(uint32_t template_syn,
94 unsigned int target_el,
95 bool same_el, bool ea,
96 bool s1ptw, bool is_write,
97 int fsc)
99 uint32_t syn;
101 /* ISV is only set for data aborts routed to EL2 and
102 * never for stage-1 page table walks faulting on stage 2.
104 * Furthermore, ISV is only set for certain kinds of load/stores.
105 * If the template syndrome does not have ISV set, we should leave
106 * it cleared.
108 * See ARMv8 specs, D7-1974:
109 * ISS encoding for an exception from a Data Abort, the
110 * ISV field.
112 if (!(template_syn & ARM_EL_ISV) || target_el != 2 || s1ptw) {
113 syn = syn_data_abort_no_iss(same_el,
114 ea, 0, s1ptw, is_write, fsc);
115 } else {
116 /* Fields: IL, ISV, SAS, SSE, SRT, SF and AR come from the template
117 * syndrome created at translation time.
118 * Now we create the runtime syndrome with the remaining fields.
120 syn = syn_data_abort_with_iss(same_el,
121 0, 0, 0, 0, 0,
122 ea, 0, s1ptw, is_write, fsc,
123 false);
124 /* Merge the runtime syndrome with the template syndrome. */
125 syn |= template_syn;
127 return syn;
130 static void deliver_fault(ARMCPU *cpu, vaddr addr, MMUAccessType access_type,
131 int mmu_idx, ARMMMUFaultInfo *fi)
133 CPUARMState *env = &cpu->env;
134 int target_el;
135 bool same_el;
136 uint32_t syn, exc, fsr, fsc;
137 ARMMMUIdx arm_mmu_idx = core_to_arm_mmu_idx(env, mmu_idx);
139 target_el = exception_target_el(env);
140 if (fi->stage2) {
141 target_el = 2;
142 env->cp15.hpfar_el2 = extract64(fi->s2addr, 12, 47) << 4;
144 same_el = (arm_current_el(env) == target_el);
146 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
147 arm_s1_regime_using_lpae_format(env, arm_mmu_idx)) {
148 /* LPAE format fault status register : bottom 6 bits are
149 * status code in the same form as needed for syndrome
151 fsr = arm_fi_to_lfsc(fi);
152 fsc = extract32(fsr, 0, 6);
153 } else {
154 fsr = arm_fi_to_sfsc(fi);
155 /* Short format FSR : this fault will never actually be reported
156 * to an EL that uses a syndrome register. Use a (currently)
157 * reserved FSR code in case the constructed syndrome does leak
158 * into the guest somehow.
160 fsc = 0x3f;
163 if (access_type == MMU_INST_FETCH) {
164 syn = syn_insn_abort(same_el, fi->ea, fi->s1ptw, fsc);
165 exc = EXCP_PREFETCH_ABORT;
166 } else {
167 syn = merge_syn_data_abort(env->exception.syndrome, target_el,
168 same_el, fi->ea, fi->s1ptw,
169 access_type == MMU_DATA_STORE,
170 fsc);
171 if (access_type == MMU_DATA_STORE
172 && arm_feature(env, ARM_FEATURE_V6)) {
173 fsr |= (1 << 11);
175 exc = EXCP_DATA_ABORT;
178 env->exception.vaddress = addr;
179 env->exception.fsr = fsr;
180 raise_exception(env, exc, syn, target_el);
183 /* try to fill the TLB and return an exception if error. If retaddr is
184 * NULL, it means that the function was called in C code (i.e. not
185 * from generated code or from helper.c)
187 void tlb_fill(CPUState *cs, target_ulong addr, int size,
188 MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
190 bool ret;
191 ARMMMUFaultInfo fi = {};
193 ret = arm_tlb_fill(cs, addr, access_type, mmu_idx, &fi);
194 if (unlikely(ret)) {
195 ARMCPU *cpu = ARM_CPU(cs);
197 /* now we have a real cpu fault */
198 cpu_restore_state(cs, retaddr, true);
200 deliver_fault(cpu, addr, access_type, mmu_idx, &fi);
204 /* Raise a data fault alignment exception for the specified virtual address */
205 void arm_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
206 MMUAccessType access_type,
207 int mmu_idx, uintptr_t retaddr)
209 ARMCPU *cpu = ARM_CPU(cs);
210 ARMMMUFaultInfo fi = {};
212 /* now we have a real cpu fault */
213 cpu_restore_state(cs, retaddr, true);
215 fi.type = ARMFault_Alignment;
216 deliver_fault(cpu, vaddr, access_type, mmu_idx, &fi);
219 /* arm_cpu_do_transaction_failed: handle a memory system error response
220 * (eg "no device/memory present at address") by raising an external abort
221 * exception
223 void arm_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
224 vaddr addr, unsigned size,
225 MMUAccessType access_type,
226 int mmu_idx, MemTxAttrs attrs,
227 MemTxResult response, uintptr_t retaddr)
229 ARMCPU *cpu = ARM_CPU(cs);
230 ARMMMUFaultInfo fi = {};
232 /* now we have a real cpu fault */
233 cpu_restore_state(cs, retaddr, true);
235 fi.ea = arm_extabort_type(response);
236 fi.type = ARMFault_SyncExternal;
237 deliver_fault(cpu, addr, access_type, mmu_idx, &fi);
240 #endif /* !defined(CONFIG_USER_ONLY) */
242 void HELPER(v8m_stackcheck)(CPUARMState *env, uint32_t newvalue)
245 * Perform the v8M stack limit check for SP updates from translated code,
246 * raising an exception if the limit is breached.
248 if (newvalue < v7m_sp_limit(env)) {
249 CPUState *cs = CPU(arm_env_get_cpu(env));
252 * Stack limit exceptions are a rare case, so rather than syncing
253 * PC/condbits before the call, we use cpu_restore_state() to
254 * get them right before raising the exception.
256 cpu_restore_state(cs, GETPC(), true);
257 raise_exception(env, EXCP_STKOF, 0, 1);
261 uint32_t HELPER(add_setq)(CPUARMState *env, uint32_t a, uint32_t b)
263 uint32_t res = a + b;
264 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT))
265 env->QF = 1;
266 return res;
269 uint32_t HELPER(add_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
271 uint32_t res = a + b;
272 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT)) {
273 env->QF = 1;
274 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
276 return res;
279 uint32_t HELPER(sub_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
281 uint32_t res = a - b;
282 if (((res ^ a) & SIGNBIT) && ((a ^ b) & SIGNBIT)) {
283 env->QF = 1;
284 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
286 return res;
289 uint32_t HELPER(double_saturate)(CPUARMState *env, int32_t val)
291 uint32_t res;
292 if (val >= 0x40000000) {
293 res = ~SIGNBIT;
294 env->QF = 1;
295 } else if (val <= (int32_t)0xc0000000) {
296 res = SIGNBIT;
297 env->QF = 1;
298 } else {
299 res = val << 1;
301 return res;
304 uint32_t HELPER(add_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
306 uint32_t res = a + b;
307 if (res < a) {
308 env->QF = 1;
309 res = ~0;
311 return res;
314 uint32_t HELPER(sub_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
316 uint32_t res = a - b;
317 if (res > a) {
318 env->QF = 1;
319 res = 0;
321 return res;
324 /* Signed saturation. */
325 static inline uint32_t do_ssat(CPUARMState *env, int32_t val, int shift)
327 int32_t top;
328 uint32_t mask;
330 top = val >> shift;
331 mask = (1u << shift) - 1;
332 if (top > 0) {
333 env->QF = 1;
334 return mask;
335 } else if (top < -1) {
336 env->QF = 1;
337 return ~mask;
339 return val;
342 /* Unsigned saturation. */
343 static inline uint32_t do_usat(CPUARMState *env, int32_t val, int shift)
345 uint32_t max;
347 max = (1u << shift) - 1;
348 if (val < 0) {
349 env->QF = 1;
350 return 0;
351 } else if (val > max) {
352 env->QF = 1;
353 return max;
355 return val;
358 /* Signed saturate. */
359 uint32_t HELPER(ssat)(CPUARMState *env, uint32_t x, uint32_t shift)
361 return do_ssat(env, x, shift);
364 /* Dual halfword signed saturate. */
365 uint32_t HELPER(ssat16)(CPUARMState *env, uint32_t x, uint32_t shift)
367 uint32_t res;
369 res = (uint16_t)do_ssat(env, (int16_t)x, shift);
370 res |= do_ssat(env, ((int32_t)x) >> 16, shift) << 16;
371 return res;
374 /* Unsigned saturate. */
375 uint32_t HELPER(usat)(CPUARMState *env, uint32_t x, uint32_t shift)
377 return do_usat(env, x, shift);
380 /* Dual halfword unsigned saturate. */
381 uint32_t HELPER(usat16)(CPUARMState *env, uint32_t x, uint32_t shift)
383 uint32_t res;
385 res = (uint16_t)do_usat(env, (int16_t)x, shift);
386 res |= do_usat(env, ((int32_t)x) >> 16, shift) << 16;
387 return res;
390 void HELPER(setend)(CPUARMState *env)
392 env->uncached_cpsr ^= CPSR_E;
395 /* Function checks whether WFx (WFI/WFE) instructions are set up to be trapped.
396 * The function returns the target EL (1-3) if the instruction is to be trapped;
397 * otherwise it returns 0 indicating it is not trapped.
399 static inline int check_wfx_trap(CPUARMState *env, bool is_wfe)
401 int cur_el = arm_current_el(env);
402 uint64_t mask;
404 if (arm_feature(env, ARM_FEATURE_M)) {
405 /* M profile cores can never trap WFI/WFE. */
406 return 0;
409 /* If we are currently in EL0 then we need to check if SCTLR is set up for
410 * WFx instructions being trapped to EL1. These trap bits don't exist in v7.
412 if (cur_el < 1 && arm_feature(env, ARM_FEATURE_V8)) {
413 int target_el;
415 mask = is_wfe ? SCTLR_nTWE : SCTLR_nTWI;
416 if (arm_is_secure_below_el3(env) && !arm_el_is_aa64(env, 3)) {
417 /* Secure EL0 and Secure PL1 is at EL3 */
418 target_el = 3;
419 } else {
420 target_el = 1;
423 if (!(env->cp15.sctlr_el[target_el] & mask)) {
424 return target_el;
428 /* We are not trapping to EL1; trap to EL2 if HCR_EL2 requires it
429 * No need for ARM_FEATURE check as if HCR_EL2 doesn't exist the
430 * bits will be zero indicating no trap.
432 if (cur_el < 2 && !arm_is_secure(env)) {
433 mask = (is_wfe) ? HCR_TWE : HCR_TWI;
434 if (env->cp15.hcr_el2 & mask) {
435 return 2;
439 /* We are not trapping to EL1 or EL2; trap to EL3 if SCR_EL3 requires it */
440 if (cur_el < 3) {
441 mask = (is_wfe) ? SCR_TWE : SCR_TWI;
442 if (env->cp15.scr_el3 & mask) {
443 return 3;
447 return 0;
450 void HELPER(wfi)(CPUARMState *env, uint32_t insn_len)
452 CPUState *cs = CPU(arm_env_get_cpu(env));
453 int target_el = check_wfx_trap(env, false);
455 if (cpu_has_work(cs)) {
456 /* Don't bother to go into our "low power state" if
457 * we would just wake up immediately.
459 return;
462 if (target_el) {
463 env->pc -= insn_len;
464 raise_exception(env, EXCP_UDEF, syn_wfx(1, 0xe, 0, insn_len == 2),
465 target_el);
468 cs->exception_index = EXCP_HLT;
469 cs->halted = 1;
470 cpu_loop_exit(cs);
473 void QEMU_NORETURN HELPER(wfe)(CPUARMState *env)
475 /* This is a hint instruction that is semantically different
476 * from YIELD even though we currently implement it identically.
477 * Don't actually halt the CPU, just yield back to top
478 * level loop. This is not going into a "low power state"
479 * (ie halting until some event occurs), so we never take
480 * a configurable trap to a different exception level.
482 HELPER(yield)(env);
485 void QEMU_NORETURN HELPER(yield)(CPUARMState *env)
487 ARMCPU *cpu = arm_env_get_cpu(env);
488 CPUState *cs = CPU(cpu);
490 /* This is a non-trappable hint instruction that generally indicates
491 * that the guest is currently busy-looping. Yield control back to the
492 * top level loop so that a more deserving VCPU has a chance to run.
494 cs->exception_index = EXCP_YIELD;
495 cpu_loop_exit(cs);
498 /* Raise an internal-to-QEMU exception. This is limited to only
499 * those EXCP values which are special cases for QEMU to interrupt
500 * execution and not to be used for exceptions which are passed to
501 * the guest (those must all have syndrome information and thus should
502 * use exception_with_syndrome).
504 void QEMU_NORETURN HELPER(exception_internal)(CPUARMState *env, uint32_t excp)
506 CPUState *cs = CPU(arm_env_get_cpu(env));
508 assert(excp_is_internal(excp));
509 cs->exception_index = excp;
510 cpu_loop_exit(cs);
513 /* Raise an exception with the specified syndrome register value */
514 void QEMU_NORETURN
515 HELPER(exception_with_syndrome)(CPUARMState *env, uint32_t excp,
516 uint32_t syndrome, uint32_t target_el)
518 raise_exception(env, excp, syndrome, target_el);
521 /* Raise an EXCP_BKPT with the specified syndrome register value,
522 * targeting the correct exception level for debug exceptions.
524 void HELPER(exception_bkpt_insn)(CPUARMState *env, uint32_t syndrome)
526 /* FSR will only be used if the debug target EL is AArch32. */
527 env->exception.fsr = arm_debug_exception_fsr(env);
528 /* FAR is UNKNOWN: clear vaddress to avoid potentially exposing
529 * values to the guest that it shouldn't be able to see at its
530 * exception/security level.
532 env->exception.vaddress = 0;
533 raise_exception(env, EXCP_BKPT, syndrome, arm_debug_target_el(env));
536 uint32_t HELPER(cpsr_read)(CPUARMState *env)
538 return cpsr_read(env) & ~(CPSR_EXEC | CPSR_RESERVED);
541 void HELPER(cpsr_write)(CPUARMState *env, uint32_t val, uint32_t mask)
543 cpsr_write(env, val, mask, CPSRWriteByInstr);
546 /* Write the CPSR for a 32-bit exception return */
547 void HELPER(cpsr_write_eret)(CPUARMState *env, uint32_t val)
549 qemu_mutex_lock_iothread();
550 arm_call_pre_el_change_hook(arm_env_get_cpu(env));
551 qemu_mutex_unlock_iothread();
553 cpsr_write(env, val, CPSR_ERET_MASK, CPSRWriteExceptionReturn);
555 /* Generated code has already stored the new PC value, but
556 * without masking out its low bits, because which bits need
557 * masking depends on whether we're returning to Thumb or ARM
558 * state. Do the masking now.
560 env->regs[15] &= (env->thumb ? ~1 : ~3);
562 qemu_mutex_lock_iothread();
563 arm_call_el_change_hook(arm_env_get_cpu(env));
564 qemu_mutex_unlock_iothread();
567 /* Access to user mode registers from privileged modes. */
568 uint32_t HELPER(get_user_reg)(CPUARMState *env, uint32_t regno)
570 uint32_t val;
572 if (regno == 13) {
573 val = env->banked_r13[BANK_USRSYS];
574 } else if (regno == 14) {
575 val = env->banked_r14[BANK_USRSYS];
576 } else if (regno >= 8
577 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
578 val = env->usr_regs[regno - 8];
579 } else {
580 val = env->regs[regno];
582 return val;
585 void HELPER(set_user_reg)(CPUARMState *env, uint32_t regno, uint32_t val)
587 if (regno == 13) {
588 env->banked_r13[BANK_USRSYS] = val;
589 } else if (regno == 14) {
590 env->banked_r14[BANK_USRSYS] = val;
591 } else if (regno >= 8
592 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
593 env->usr_regs[regno - 8] = val;
594 } else {
595 env->regs[regno] = val;
599 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
601 if ((env->uncached_cpsr & CPSR_M) == mode) {
602 env->regs[13] = val;
603 } else {
604 env->banked_r13[bank_number(mode)] = val;
608 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
610 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_SYS) {
611 /* SRS instruction is UNPREDICTABLE from System mode; we UNDEF.
612 * Other UNPREDICTABLE and UNDEF cases were caught at translate time.
614 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
615 exception_target_el(env));
618 if ((env->uncached_cpsr & CPSR_M) == mode) {
619 return env->regs[13];
620 } else {
621 return env->banked_r13[bank_number(mode)];
625 static void msr_mrs_banked_exc_checks(CPUARMState *env, uint32_t tgtmode,
626 uint32_t regno)
628 /* Raise an exception if the requested access is one of the UNPREDICTABLE
629 * cases; otherwise return. This broadly corresponds to the pseudocode
630 * BankedRegisterAccessValid() and SPSRAccessValid(),
631 * except that we have already handled some cases at translate time.
633 int curmode = env->uncached_cpsr & CPSR_M;
635 if (regno == 17) {
636 /* ELR_Hyp: a special case because access from tgtmode is OK */
637 if (curmode != ARM_CPU_MODE_HYP && curmode != ARM_CPU_MODE_MON) {
638 goto undef;
640 return;
643 if (curmode == tgtmode) {
644 goto undef;
647 if (tgtmode == ARM_CPU_MODE_USR) {
648 switch (regno) {
649 case 8 ... 12:
650 if (curmode != ARM_CPU_MODE_FIQ) {
651 goto undef;
653 break;
654 case 13:
655 if (curmode == ARM_CPU_MODE_SYS) {
656 goto undef;
658 break;
659 case 14:
660 if (curmode == ARM_CPU_MODE_HYP || curmode == ARM_CPU_MODE_SYS) {
661 goto undef;
663 break;
664 default:
665 break;
669 if (tgtmode == ARM_CPU_MODE_HYP) {
670 /* SPSR_Hyp, r13_hyp: accessible from Monitor mode only */
671 if (curmode != ARM_CPU_MODE_MON) {
672 goto undef;
676 return;
678 undef:
679 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
680 exception_target_el(env));
683 void HELPER(msr_banked)(CPUARMState *env, uint32_t value, uint32_t tgtmode,
684 uint32_t regno)
686 msr_mrs_banked_exc_checks(env, tgtmode, regno);
688 switch (regno) {
689 case 16: /* SPSRs */
690 env->banked_spsr[bank_number(tgtmode)] = value;
691 break;
692 case 17: /* ELR_Hyp */
693 env->elr_el[2] = value;
694 break;
695 case 13:
696 env->banked_r13[bank_number(tgtmode)] = value;
697 break;
698 case 14:
699 env->banked_r14[bank_number(tgtmode)] = value;
700 break;
701 case 8 ... 12:
702 switch (tgtmode) {
703 case ARM_CPU_MODE_USR:
704 env->usr_regs[regno - 8] = value;
705 break;
706 case ARM_CPU_MODE_FIQ:
707 env->fiq_regs[regno - 8] = value;
708 break;
709 default:
710 g_assert_not_reached();
712 break;
713 default:
714 g_assert_not_reached();
718 uint32_t HELPER(mrs_banked)(CPUARMState *env, uint32_t tgtmode, uint32_t regno)
720 msr_mrs_banked_exc_checks(env, tgtmode, regno);
722 switch (regno) {
723 case 16: /* SPSRs */
724 return env->banked_spsr[bank_number(tgtmode)];
725 case 17: /* ELR_Hyp */
726 return env->elr_el[2];
727 case 13:
728 return env->banked_r13[bank_number(tgtmode)];
729 case 14:
730 return env->banked_r14[bank_number(tgtmode)];
731 case 8 ... 12:
732 switch (tgtmode) {
733 case ARM_CPU_MODE_USR:
734 return env->usr_regs[regno - 8];
735 case ARM_CPU_MODE_FIQ:
736 return env->fiq_regs[regno - 8];
737 default:
738 g_assert_not_reached();
740 default:
741 g_assert_not_reached();
745 void HELPER(access_check_cp_reg)(CPUARMState *env, void *rip, uint32_t syndrome,
746 uint32_t isread)
748 const ARMCPRegInfo *ri = rip;
749 int target_el;
751 if (arm_feature(env, ARM_FEATURE_XSCALE) && ri->cp < 14
752 && extract32(env->cp15.c15_cpar, ri->cp, 1) == 0) {
753 raise_exception(env, EXCP_UDEF, syndrome, exception_target_el(env));
756 if (!ri->accessfn) {
757 return;
760 switch (ri->accessfn(env, ri, isread)) {
761 case CP_ACCESS_OK:
762 return;
763 case CP_ACCESS_TRAP:
764 target_el = exception_target_el(env);
765 break;
766 case CP_ACCESS_TRAP_EL2:
767 /* Requesting a trap to EL2 when we're in EL3 or S-EL0/1 is
768 * a bug in the access function.
770 assert(!arm_is_secure(env) && arm_current_el(env) != 3);
771 target_el = 2;
772 break;
773 case CP_ACCESS_TRAP_EL3:
774 target_el = 3;
775 break;
776 case CP_ACCESS_TRAP_UNCATEGORIZED:
777 target_el = exception_target_el(env);
778 syndrome = syn_uncategorized();
779 break;
780 case CP_ACCESS_TRAP_UNCATEGORIZED_EL2:
781 target_el = 2;
782 syndrome = syn_uncategorized();
783 break;
784 case CP_ACCESS_TRAP_UNCATEGORIZED_EL3:
785 target_el = 3;
786 syndrome = syn_uncategorized();
787 break;
788 case CP_ACCESS_TRAP_FP_EL2:
789 target_el = 2;
790 /* Since we are an implementation that takes exceptions on a trapped
791 * conditional insn only if the insn has passed its condition code
792 * check, we take the IMPDEF choice to always report CV=1 COND=0xe
793 * (which is also the required value for AArch64 traps).
795 syndrome = syn_fp_access_trap(1, 0xe, false);
796 break;
797 case CP_ACCESS_TRAP_FP_EL3:
798 target_el = 3;
799 syndrome = syn_fp_access_trap(1, 0xe, false);
800 break;
801 default:
802 g_assert_not_reached();
805 raise_exception(env, EXCP_UDEF, syndrome, target_el);
808 void HELPER(set_cp_reg)(CPUARMState *env, void *rip, uint32_t value)
810 const ARMCPRegInfo *ri = rip;
812 if (ri->type & ARM_CP_IO) {
813 qemu_mutex_lock_iothread();
814 ri->writefn(env, ri, value);
815 qemu_mutex_unlock_iothread();
816 } else {
817 ri->writefn(env, ri, value);
821 uint32_t HELPER(get_cp_reg)(CPUARMState *env, void *rip)
823 const ARMCPRegInfo *ri = rip;
824 uint32_t res;
826 if (ri->type & ARM_CP_IO) {
827 qemu_mutex_lock_iothread();
828 res = ri->readfn(env, ri);
829 qemu_mutex_unlock_iothread();
830 } else {
831 res = ri->readfn(env, ri);
834 return res;
837 void HELPER(set_cp_reg64)(CPUARMState *env, void *rip, uint64_t value)
839 const ARMCPRegInfo *ri = rip;
841 if (ri->type & ARM_CP_IO) {
842 qemu_mutex_lock_iothread();
843 ri->writefn(env, ri, value);
844 qemu_mutex_unlock_iothread();
845 } else {
846 ri->writefn(env, ri, value);
850 uint64_t HELPER(get_cp_reg64)(CPUARMState *env, void *rip)
852 const ARMCPRegInfo *ri = rip;
853 uint64_t res;
855 if (ri->type & ARM_CP_IO) {
856 qemu_mutex_lock_iothread();
857 res = ri->readfn(env, ri);
858 qemu_mutex_unlock_iothread();
859 } else {
860 res = ri->readfn(env, ri);
863 return res;
866 void HELPER(msr_i_pstate)(CPUARMState *env, uint32_t op, uint32_t imm)
868 /* MSR_i to update PSTATE. This is OK from EL0 only if UMA is set.
869 * Note that SPSel is never OK from EL0; we rely on handle_msr_i()
870 * to catch that case at translate time.
872 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
873 uint32_t syndrome = syn_aa64_sysregtrap(0, extract32(op, 0, 3),
874 extract32(op, 3, 3), 4,
875 imm, 0x1f, 0);
876 raise_exception(env, EXCP_UDEF, syndrome, exception_target_el(env));
879 switch (op) {
880 case 0x05: /* SPSel */
881 update_spsel(env, imm);
882 break;
883 case 0x1e: /* DAIFSet */
884 env->daif |= (imm << 6) & PSTATE_DAIF;
885 break;
886 case 0x1f: /* DAIFClear */
887 env->daif &= ~((imm << 6) & PSTATE_DAIF);
888 break;
889 default:
890 g_assert_not_reached();
894 void HELPER(clear_pstate_ss)(CPUARMState *env)
896 env->pstate &= ~PSTATE_SS;
899 void HELPER(pre_hvc)(CPUARMState *env)
901 ARMCPU *cpu = arm_env_get_cpu(env);
902 int cur_el = arm_current_el(env);
903 /* FIXME: Use actual secure state. */
904 bool secure = false;
905 bool undef;
907 if (arm_is_psci_call(cpu, EXCP_HVC)) {
908 /* If PSCI is enabled and this looks like a valid PSCI call then
909 * that overrides the architecturally mandated HVC behaviour.
911 return;
914 if (!arm_feature(env, ARM_FEATURE_EL2)) {
915 /* If EL2 doesn't exist, HVC always UNDEFs */
916 undef = true;
917 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
918 /* EL3.HCE has priority over EL2.HCD. */
919 undef = !(env->cp15.scr_el3 & SCR_HCE);
920 } else {
921 undef = env->cp15.hcr_el2 & HCR_HCD;
924 /* In ARMv7 and ARMv8/AArch32, HVC is undef in secure state.
925 * For ARMv8/AArch64, HVC is allowed in EL3.
926 * Note that we've already trapped HVC from EL0 at translation
927 * time.
929 if (secure && (!is_a64(env) || cur_el == 1)) {
930 undef = true;
933 if (undef) {
934 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
935 exception_target_el(env));
939 void HELPER(pre_smc)(CPUARMState *env, uint32_t syndrome)
941 ARMCPU *cpu = arm_env_get_cpu(env);
942 int cur_el = arm_current_el(env);
943 bool secure = arm_is_secure(env);
944 bool smd = env->cp15.scr_el3 & SCR_SMD;
945 /* On ARMv8 with EL3 AArch64, SMD applies to both S and NS state.
946 * On ARMv8 with EL3 AArch32, or ARMv7 with the Virtualization
947 * extensions, SMD only applies to NS state.
948 * On ARMv7 without the Virtualization extensions, the SMD bit
949 * doesn't exist, but we forbid the guest to set it to 1 in scr_write(),
950 * so we need not special case this here.
952 bool undef = arm_feature(env, ARM_FEATURE_AARCH64) ? smd : smd && !secure;
954 if (!arm_feature(env, ARM_FEATURE_EL3) &&
955 cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
956 /* If we have no EL3 then SMC always UNDEFs and can't be
957 * trapped to EL2. PSCI-via-SMC is a sort of ersatz EL3
958 * firmware within QEMU, and we want an EL2 guest to be able
959 * to forbid its EL1 from making PSCI calls into QEMU's
960 * "firmware" via HCR.TSC, so for these purposes treat
961 * PSCI-via-SMC as implying an EL3.
963 undef = true;
964 } else if (!secure && cur_el == 1 && (env->cp15.hcr_el2 & HCR_TSC)) {
965 /* In NS EL1, HCR controlled routing to EL2 has priority over SMD.
966 * We also want an EL2 guest to be able to forbid its EL1 from
967 * making PSCI calls into QEMU's "firmware" via HCR.TSC.
969 raise_exception(env, EXCP_HYP_TRAP, syndrome, 2);
972 /* If PSCI is enabled and this looks like a valid PSCI call then
973 * suppress the UNDEF -- we'll catch the SMC exception and
974 * implement the PSCI call behaviour there.
976 if (undef && !arm_is_psci_call(cpu, EXCP_SMC)) {
977 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
978 exception_target_el(env));
982 static int el_from_spsr(uint32_t spsr)
984 /* Return the exception level that this SPSR is requesting a return to,
985 * or -1 if it is invalid (an illegal return)
987 if (spsr & PSTATE_nRW) {
988 switch (spsr & CPSR_M) {
989 case ARM_CPU_MODE_USR:
990 return 0;
991 case ARM_CPU_MODE_HYP:
992 return 2;
993 case ARM_CPU_MODE_FIQ:
994 case ARM_CPU_MODE_IRQ:
995 case ARM_CPU_MODE_SVC:
996 case ARM_CPU_MODE_ABT:
997 case ARM_CPU_MODE_UND:
998 case ARM_CPU_MODE_SYS:
999 return 1;
1000 case ARM_CPU_MODE_MON:
1001 /* Returning to Mon from AArch64 is never possible,
1002 * so this is an illegal return.
1004 default:
1005 return -1;
1007 } else {
1008 if (extract32(spsr, 1, 1)) {
1009 /* Return with reserved M[1] bit set */
1010 return -1;
1012 if (extract32(spsr, 0, 4) == 1) {
1013 /* return to EL0 with M[0] bit set */
1014 return -1;
1016 return extract32(spsr, 2, 2);
1020 void HELPER(exception_return)(CPUARMState *env)
1022 int cur_el = arm_current_el(env);
1023 unsigned int spsr_idx = aarch64_banked_spsr_index(cur_el);
1024 uint32_t spsr = env->banked_spsr[spsr_idx];
1025 int new_el;
1026 bool return_to_aa64 = (spsr & PSTATE_nRW) == 0;
1028 aarch64_save_sp(env, cur_el);
1030 arm_clear_exclusive(env);
1032 /* We must squash the PSTATE.SS bit to zero unless both of the
1033 * following hold:
1034 * 1. debug exceptions are currently disabled
1035 * 2. singlestep will be active in the EL we return to
1036 * We check 1 here and 2 after we've done the pstate/cpsr write() to
1037 * transition to the EL we're going to.
1039 if (arm_generate_debug_exceptions(env)) {
1040 spsr &= ~PSTATE_SS;
1043 new_el = el_from_spsr(spsr);
1044 if (new_el == -1) {
1045 goto illegal_return;
1047 if (new_el > cur_el
1048 || (new_el == 2 && !arm_feature(env, ARM_FEATURE_EL2))) {
1049 /* Disallow return to an EL which is unimplemented or higher
1050 * than the current one.
1052 goto illegal_return;
1055 if (new_el != 0 && arm_el_is_aa64(env, new_el) != return_to_aa64) {
1056 /* Return to an EL which is configured for a different register width */
1057 goto illegal_return;
1060 if (new_el == 2 && arm_is_secure_below_el3(env)) {
1061 /* Return to the non-existent secure-EL2 */
1062 goto illegal_return;
1065 if (new_el == 1 && (env->cp15.hcr_el2 & HCR_TGE)
1066 && !arm_is_secure_below_el3(env)) {
1067 goto illegal_return;
1070 qemu_mutex_lock_iothread();
1071 arm_call_pre_el_change_hook(arm_env_get_cpu(env));
1072 qemu_mutex_unlock_iothread();
1074 if (!return_to_aa64) {
1075 env->aarch64 = 0;
1076 /* We do a raw CPSR write because aarch64_sync_64_to_32()
1077 * will sort the register banks out for us, and we've already
1078 * caught all the bad-mode cases in el_from_spsr().
1080 cpsr_write(env, spsr, ~0, CPSRWriteRaw);
1081 if (!arm_singlestep_active(env)) {
1082 env->uncached_cpsr &= ~PSTATE_SS;
1084 aarch64_sync_64_to_32(env);
1086 if (spsr & CPSR_T) {
1087 env->regs[15] = env->elr_el[cur_el] & ~0x1;
1088 } else {
1089 env->regs[15] = env->elr_el[cur_el] & ~0x3;
1091 qemu_log_mask(CPU_LOG_INT, "Exception return from AArch64 EL%d to "
1092 "AArch32 EL%d PC 0x%" PRIx32 "\n",
1093 cur_el, new_el, env->regs[15]);
1094 } else {
1095 env->aarch64 = 1;
1096 pstate_write(env, spsr);
1097 if (!arm_singlestep_active(env)) {
1098 env->pstate &= ~PSTATE_SS;
1100 aarch64_restore_sp(env, new_el);
1101 env->pc = env->elr_el[cur_el];
1102 qemu_log_mask(CPU_LOG_INT, "Exception return from AArch64 EL%d to "
1103 "AArch64 EL%d PC 0x%" PRIx64 "\n",
1104 cur_el, new_el, env->pc);
1107 * Note that cur_el can never be 0. If new_el is 0, then
1108 * el0_a64 is return_to_aa64, else el0_a64 is ignored.
1110 aarch64_sve_change_el(env, cur_el, new_el, return_to_aa64);
1112 qemu_mutex_lock_iothread();
1113 arm_call_el_change_hook(arm_env_get_cpu(env));
1114 qemu_mutex_unlock_iothread();
1116 return;
1118 illegal_return:
1119 /* Illegal return events of various kinds have architecturally
1120 * mandated behaviour:
1121 * restore NZCV and DAIF from SPSR_ELx
1122 * set PSTATE.IL
1123 * restore PC from ELR_ELx
1124 * no change to exception level, execution state or stack pointer
1126 env->pstate |= PSTATE_IL;
1127 env->pc = env->elr_el[cur_el];
1128 spsr &= PSTATE_NZCV | PSTATE_DAIF;
1129 spsr |= pstate_read(env) & ~(PSTATE_NZCV | PSTATE_DAIF);
1130 pstate_write(env, spsr);
1131 if (!arm_singlestep_active(env)) {
1132 env->pstate &= ~PSTATE_SS;
1134 qemu_log_mask(LOG_GUEST_ERROR, "Illegal exception return at EL%d: "
1135 "resuming execution at 0x%" PRIx64 "\n", cur_el, env->pc);
1138 /* Return true if the linked breakpoint entry lbn passes its checks */
1139 static bool linked_bp_matches(ARMCPU *cpu, int lbn)
1141 CPUARMState *env = &cpu->env;
1142 uint64_t bcr = env->cp15.dbgbcr[lbn];
1143 int brps = extract32(cpu->dbgdidr, 24, 4);
1144 int ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
1145 int bt;
1146 uint32_t contextidr;
1148 /* Links to unimplemented or non-context aware breakpoints are
1149 * CONSTRAINED UNPREDICTABLE: either behave as if disabled, or
1150 * as if linked to an UNKNOWN context-aware breakpoint (in which
1151 * case DBGWCR<n>_EL1.LBN must indicate that breakpoint).
1152 * We choose the former.
1154 if (lbn > brps || lbn < (brps - ctx_cmps)) {
1155 return false;
1158 bcr = env->cp15.dbgbcr[lbn];
1160 if (extract64(bcr, 0, 1) == 0) {
1161 /* Linked breakpoint disabled : generate no events */
1162 return false;
1165 bt = extract64(bcr, 20, 4);
1167 /* We match the whole register even if this is AArch32 using the
1168 * short descriptor format (in which case it holds both PROCID and ASID),
1169 * since we don't implement the optional v7 context ID masking.
1171 contextidr = extract64(env->cp15.contextidr_el[1], 0, 32);
1173 switch (bt) {
1174 case 3: /* linked context ID match */
1175 if (arm_current_el(env) > 1) {
1176 /* Context matches never fire in EL2 or (AArch64) EL3 */
1177 return false;
1179 return (contextidr == extract64(env->cp15.dbgbvr[lbn], 0, 32));
1180 case 5: /* linked address mismatch (reserved in AArch64) */
1181 case 9: /* linked VMID match (reserved if no EL2) */
1182 case 11: /* linked context ID and VMID match (reserved if no EL2) */
1183 default:
1184 /* Links to Unlinked context breakpoints must generate no
1185 * events; we choose to do the same for reserved values too.
1187 return false;
1190 return false;
1193 static bool bp_wp_matches(ARMCPU *cpu, int n, bool is_wp)
1195 CPUARMState *env = &cpu->env;
1196 uint64_t cr;
1197 int pac, hmc, ssc, wt, lbn;
1198 /* Note that for watchpoints the check is against the CPU security
1199 * state, not the S/NS attribute on the offending data access.
1201 bool is_secure = arm_is_secure(env);
1202 int access_el = arm_current_el(env);
1204 if (is_wp) {
1205 CPUWatchpoint *wp = env->cpu_watchpoint[n];
1207 if (!wp || !(wp->flags & BP_WATCHPOINT_HIT)) {
1208 return false;
1210 cr = env->cp15.dbgwcr[n];
1211 if (wp->hitattrs.user) {
1212 /* The LDRT/STRT/LDT/STT "unprivileged access" instructions should
1213 * match watchpoints as if they were accesses done at EL0, even if
1214 * the CPU is at EL1 or higher.
1216 access_el = 0;
1218 } else {
1219 uint64_t pc = is_a64(env) ? env->pc : env->regs[15];
1221 if (!env->cpu_breakpoint[n] || env->cpu_breakpoint[n]->pc != pc) {
1222 return false;
1224 cr = env->cp15.dbgbcr[n];
1226 /* The WATCHPOINT_HIT flag guarantees us that the watchpoint is
1227 * enabled and that the address and access type match; for breakpoints
1228 * we know the address matched; check the remaining fields, including
1229 * linked breakpoints. We rely on WCR and BCR having the same layout
1230 * for the LBN, SSC, HMC, PAC/PMC and is-linked fields.
1231 * Note that some combinations of {PAC, HMC, SSC} are reserved and
1232 * must act either like some valid combination or as if the watchpoint
1233 * were disabled. We choose the former, and use this together with
1234 * the fact that EL3 must always be Secure and EL2 must always be
1235 * Non-Secure to simplify the code slightly compared to the full
1236 * table in the ARM ARM.
1238 pac = extract64(cr, 1, 2);
1239 hmc = extract64(cr, 13, 1);
1240 ssc = extract64(cr, 14, 2);
1242 switch (ssc) {
1243 case 0:
1244 break;
1245 case 1:
1246 case 3:
1247 if (is_secure) {
1248 return false;
1250 break;
1251 case 2:
1252 if (!is_secure) {
1253 return false;
1255 break;
1258 switch (access_el) {
1259 case 3:
1260 case 2:
1261 if (!hmc) {
1262 return false;
1264 break;
1265 case 1:
1266 if (extract32(pac, 0, 1) == 0) {
1267 return false;
1269 break;
1270 case 0:
1271 if (extract32(pac, 1, 1) == 0) {
1272 return false;
1274 break;
1275 default:
1276 g_assert_not_reached();
1279 wt = extract64(cr, 20, 1);
1280 lbn = extract64(cr, 16, 4);
1282 if (wt && !linked_bp_matches(cpu, lbn)) {
1283 return false;
1286 return true;
1289 static bool check_watchpoints(ARMCPU *cpu)
1291 CPUARMState *env = &cpu->env;
1292 int n;
1294 /* If watchpoints are disabled globally or we can't take debug
1295 * exceptions here then watchpoint firings are ignored.
1297 if (extract32(env->cp15.mdscr_el1, 15, 1) == 0
1298 || !arm_generate_debug_exceptions(env)) {
1299 return false;
1302 for (n = 0; n < ARRAY_SIZE(env->cpu_watchpoint); n++) {
1303 if (bp_wp_matches(cpu, n, true)) {
1304 return true;
1307 return false;
1310 static bool check_breakpoints(ARMCPU *cpu)
1312 CPUARMState *env = &cpu->env;
1313 int n;
1315 /* If breakpoints are disabled globally or we can't take debug
1316 * exceptions here then breakpoint firings are ignored.
1318 if (extract32(env->cp15.mdscr_el1, 15, 1) == 0
1319 || !arm_generate_debug_exceptions(env)) {
1320 return false;
1323 for (n = 0; n < ARRAY_SIZE(env->cpu_breakpoint); n++) {
1324 if (bp_wp_matches(cpu, n, false)) {
1325 return true;
1328 return false;
1331 void HELPER(check_breakpoints)(CPUARMState *env)
1333 ARMCPU *cpu = arm_env_get_cpu(env);
1335 if (check_breakpoints(cpu)) {
1336 HELPER(exception_internal(env, EXCP_DEBUG));
1340 bool arm_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
1342 /* Called by core code when a CPU watchpoint fires; need to check if this
1343 * is also an architectural watchpoint match.
1345 ARMCPU *cpu = ARM_CPU(cs);
1347 return check_watchpoints(cpu);
1350 vaddr arm_adjust_watchpoint_address(CPUState *cs, vaddr addr, int len)
1352 ARMCPU *cpu = ARM_CPU(cs);
1353 CPUARMState *env = &cpu->env;
1355 /* In BE32 system mode, target memory is stored byteswapped (on a
1356 * little-endian host system), and by the time we reach here (via an
1357 * opcode helper) the addresses of subword accesses have been adjusted
1358 * to account for that, which means that watchpoints will not match.
1359 * Undo the adjustment here.
1361 if (arm_sctlr_b(env)) {
1362 if (len == 1) {
1363 addr ^= 3;
1364 } else if (len == 2) {
1365 addr ^= 2;
1369 return addr;
1372 void arm_debug_excp_handler(CPUState *cs)
1374 /* Called by core code when a watchpoint or breakpoint fires;
1375 * need to check which one and raise the appropriate exception.
1377 ARMCPU *cpu = ARM_CPU(cs);
1378 CPUARMState *env = &cpu->env;
1379 CPUWatchpoint *wp_hit = cs->watchpoint_hit;
1381 if (wp_hit) {
1382 if (wp_hit->flags & BP_CPU) {
1383 bool wnr = (wp_hit->flags & BP_WATCHPOINT_HIT_WRITE) != 0;
1384 bool same_el = arm_debug_target_el(env) == arm_current_el(env);
1386 cs->watchpoint_hit = NULL;
1388 env->exception.fsr = arm_debug_exception_fsr(env);
1389 env->exception.vaddress = wp_hit->hitaddr;
1390 raise_exception(env, EXCP_DATA_ABORT,
1391 syn_watchpoint(same_el, 0, wnr),
1392 arm_debug_target_el(env));
1394 } else {
1395 uint64_t pc = is_a64(env) ? env->pc : env->regs[15];
1396 bool same_el = (arm_debug_target_el(env) == arm_current_el(env));
1398 /* (1) GDB breakpoints should be handled first.
1399 * (2) Do not raise a CPU exception if no CPU breakpoint has fired,
1400 * since singlestep is also done by generating a debug internal
1401 * exception.
1403 if (cpu_breakpoint_test(cs, pc, BP_GDB)
1404 || !cpu_breakpoint_test(cs, pc, BP_CPU)) {
1405 return;
1408 env->exception.fsr = arm_debug_exception_fsr(env);
1409 /* FAR is UNKNOWN: clear vaddress to avoid potentially exposing
1410 * values to the guest that it shouldn't be able to see at its
1411 * exception/security level.
1413 env->exception.vaddress = 0;
1414 raise_exception(env, EXCP_PREFETCH_ABORT,
1415 syn_breakpoint(same_el),
1416 arm_debug_target_el(env));
1420 /* ??? Flag setting arithmetic is awkward because we need to do comparisons.
1421 The only way to do that in TCG is a conditional branch, which clobbers
1422 all our temporaries. For now implement these as helper functions. */
1424 /* Similarly for variable shift instructions. */
1426 uint32_t HELPER(shl_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1428 int shift = i & 0xff;
1429 if (shift >= 32) {
1430 if (shift == 32)
1431 env->CF = x & 1;
1432 else
1433 env->CF = 0;
1434 return 0;
1435 } else if (shift != 0) {
1436 env->CF = (x >> (32 - shift)) & 1;
1437 return x << shift;
1439 return x;
1442 uint32_t HELPER(shr_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1444 int shift = i & 0xff;
1445 if (shift >= 32) {
1446 if (shift == 32)
1447 env->CF = (x >> 31) & 1;
1448 else
1449 env->CF = 0;
1450 return 0;
1451 } else if (shift != 0) {
1452 env->CF = (x >> (shift - 1)) & 1;
1453 return x >> shift;
1455 return x;
1458 uint32_t HELPER(sar_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1460 int shift = i & 0xff;
1461 if (shift >= 32) {
1462 env->CF = (x >> 31) & 1;
1463 return (int32_t)x >> 31;
1464 } else if (shift != 0) {
1465 env->CF = (x >> (shift - 1)) & 1;
1466 return (int32_t)x >> shift;
1468 return x;
1471 uint32_t HELPER(ror_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1473 int shift1, shift;
1474 shift1 = i & 0xff;
1475 shift = shift1 & 0x1f;
1476 if (shift == 0) {
1477 if (shift1 != 0)
1478 env->CF = (x >> 31) & 1;
1479 return x;
1480 } else {
1481 env->CF = (x >> (shift - 1)) & 1;
1482 return ((uint32_t)x >> shift) | (x << (32 - shift));