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/units.h"
22 #include "qemu/main-loop.h"
24 #include "exec/helper-proto.h"
25 #include "internals.h"
26 #include "exec/exec-all.h"
27 #include "exec/cpu_ldst.h"
29 #define SIGNBIT (uint32_t)0x80000000
30 #define SIGNBIT64 ((uint64_t)1 << 63)
32 static CPUState
*do_raise_exception(CPUARMState
*env
, uint32_t excp
,
33 uint32_t syndrome
, uint32_t target_el
)
35 CPUState
*cs
= env_cpu(env
);
37 if (target_el
== 1 && (arm_hcr_el2_eff(env
) & HCR_TGE
)) {
39 * Redirect NS EL1 exceptions to NS EL2. These are reported with
40 * their original syndrome register value, with the exception of
41 * SIMD/FP access traps, which are reported as uncategorized
42 * (see DDI0478C.a D1.10.4)
45 if (syn_get_ec(syndrome
) == EC_ADVSIMDFPACCESSTRAP
) {
46 syndrome
= syn_uncategorized();
50 assert(!excp_is_internal(excp
));
51 cs
->exception_index
= excp
;
52 env
->exception
.syndrome
= syndrome
;
53 env
->exception
.target_el
= target_el
;
58 void raise_exception(CPUARMState
*env
, uint32_t excp
,
59 uint32_t syndrome
, uint32_t target_el
)
61 CPUState
*cs
= do_raise_exception(env
, excp
, syndrome
, target_el
);
65 void raise_exception_ra(CPUARMState
*env
, uint32_t excp
, uint32_t syndrome
,
66 uint32_t target_el
, uintptr_t ra
)
68 CPUState
*cs
= do_raise_exception(env
, excp
, syndrome
, target_el
);
69 cpu_loop_exit_restore(cs
, ra
);
72 uint32_t HELPER(neon_tbl
)(uint32_t ireg
, uint32_t def
, void *vn
,
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;
85 val
|= def
& (0xff << shift
);
91 void HELPER(v8m_stackcheck
)(CPUARMState
*env
, uint32_t newvalue
)
94 * Perform the v8M stack limit check for SP updates from translated code,
95 * raising an exception if the limit is breached.
97 if (newvalue
< v7m_sp_limit(env
)) {
98 CPUState
*cs
= env_cpu(env
);
101 * Stack limit exceptions are a rare case, so rather than syncing
102 * PC/condbits before the call, we use cpu_restore_state() to
103 * get them right before raising the exception.
105 cpu_restore_state(cs
, GETPC(), true);
106 raise_exception(env
, EXCP_STKOF
, 0, 1);
110 uint32_t HELPER(add_setq
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
112 uint32_t res
= a
+ b
;
113 if (((res
^ a
) & SIGNBIT
) && !((a
^ b
) & SIGNBIT
))
118 uint32_t HELPER(add_saturate
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
120 uint32_t res
= a
+ b
;
121 if (((res
^ a
) & SIGNBIT
) && !((a
^ b
) & SIGNBIT
)) {
123 res
= ~(((int32_t)a
>> 31) ^ SIGNBIT
);
128 uint32_t HELPER(sub_saturate
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
130 uint32_t res
= a
- b
;
131 if (((res
^ a
) & SIGNBIT
) && ((a
^ b
) & SIGNBIT
)) {
133 res
= ~(((int32_t)a
>> 31) ^ SIGNBIT
);
138 uint32_t HELPER(add_usaturate
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
140 uint32_t res
= a
+ b
;
148 uint32_t HELPER(sub_usaturate
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
150 uint32_t res
= a
- b
;
158 /* Signed saturation. */
159 static inline uint32_t do_ssat(CPUARMState
*env
, int32_t val
, int shift
)
165 mask
= (1u << shift
) - 1;
169 } else if (top
< -1) {
176 /* Unsigned saturation. */
177 static inline uint32_t do_usat(CPUARMState
*env
, int32_t val
, int shift
)
181 max
= (1u << shift
) - 1;
185 } else if (val
> max
) {
192 /* Signed saturate. */
193 uint32_t HELPER(ssat
)(CPUARMState
*env
, uint32_t x
, uint32_t shift
)
195 return do_ssat(env
, x
, shift
);
198 /* Dual halfword signed saturate. */
199 uint32_t HELPER(ssat16
)(CPUARMState
*env
, uint32_t x
, uint32_t shift
)
203 res
= (uint16_t)do_ssat(env
, (int16_t)x
, shift
);
204 res
|= do_ssat(env
, ((int32_t)x
) >> 16, shift
) << 16;
208 /* Unsigned saturate. */
209 uint32_t HELPER(usat
)(CPUARMState
*env
, uint32_t x
, uint32_t shift
)
211 return do_usat(env
, x
, shift
);
214 /* Dual halfword unsigned saturate. */
215 uint32_t HELPER(usat16
)(CPUARMState
*env
, uint32_t x
, uint32_t shift
)
219 res
= (uint16_t)do_usat(env
, (int16_t)x
, shift
);
220 res
|= do_usat(env
, ((int32_t)x
) >> 16, shift
) << 16;
224 void HELPER(setend
)(CPUARMState
*env
)
226 env
->uncached_cpsr
^= CPSR_E
;
227 arm_rebuild_hflags(env
);
230 /* Function checks whether WFx (WFI/WFE) instructions are set up to be trapped.
231 * The function returns the target EL (1-3) if the instruction is to be trapped;
232 * otherwise it returns 0 indicating it is not trapped.
234 static inline int check_wfx_trap(CPUARMState
*env
, bool is_wfe
)
236 int cur_el
= arm_current_el(env
);
239 if (arm_feature(env
, ARM_FEATURE_M
)) {
240 /* M profile cores can never trap WFI/WFE. */
244 /* If we are currently in EL0 then we need to check if SCTLR is set up for
245 * WFx instructions being trapped to EL1. These trap bits don't exist in v7.
247 if (cur_el
< 1 && arm_feature(env
, ARM_FEATURE_V8
)) {
250 mask
= is_wfe
? SCTLR_nTWE
: SCTLR_nTWI
;
251 if (arm_is_secure_below_el3(env
) && !arm_el_is_aa64(env
, 3)) {
252 /* Secure EL0 and Secure PL1 is at EL3 */
258 if (!(env
->cp15
.sctlr_el
[target_el
] & mask
)) {
263 /* We are not trapping to EL1; trap to EL2 if HCR_EL2 requires it
264 * No need for ARM_FEATURE check as if HCR_EL2 doesn't exist the
265 * bits will be zero indicating no trap.
268 mask
= is_wfe
? HCR_TWE
: HCR_TWI
;
269 if (arm_hcr_el2_eff(env
) & mask
) {
274 /* We are not trapping to EL1 or EL2; trap to EL3 if SCR_EL3 requires it */
276 mask
= (is_wfe
) ? SCR_TWE
: SCR_TWI
;
277 if (env
->cp15
.scr_el3
& mask
) {
285 void HELPER(wfi
)(CPUARMState
*env
, uint32_t insn_len
)
287 CPUState
*cs
= env_cpu(env
);
288 int target_el
= check_wfx_trap(env
, false);
290 if (cpu_has_work(cs
)) {
291 /* Don't bother to go into our "low power state" if
292 * we would just wake up immediately.
301 env
->regs
[15] -= insn_len
;
304 raise_exception(env
, EXCP_UDEF
, syn_wfx(1, 0xe, 0, insn_len
== 2),
308 cs
->exception_index
= EXCP_HLT
;
313 void HELPER(wfe
)(CPUARMState
*env
)
315 /* This is a hint instruction that is semantically different
316 * from YIELD even though we currently implement it identically.
317 * Don't actually halt the CPU, just yield back to top
318 * level loop. This is not going into a "low power state"
319 * (ie halting until some event occurs), so we never take
320 * a configurable trap to a different exception level.
325 void HELPER(yield
)(CPUARMState
*env
)
327 CPUState
*cs
= env_cpu(env
);
329 /* This is a non-trappable hint instruction that generally indicates
330 * that the guest is currently busy-looping. Yield control back to the
331 * top level loop so that a more deserving VCPU has a chance to run.
333 cs
->exception_index
= EXCP_YIELD
;
337 /* Raise an internal-to-QEMU exception. This is limited to only
338 * those EXCP values which are special cases for QEMU to interrupt
339 * execution and not to be used for exceptions which are passed to
340 * the guest (those must all have syndrome information and thus should
341 * use exception_with_syndrome).
343 void HELPER(exception_internal
)(CPUARMState
*env
, uint32_t excp
)
345 CPUState
*cs
= env_cpu(env
);
347 assert(excp_is_internal(excp
));
348 cs
->exception_index
= excp
;
352 /* Raise an exception with the specified syndrome register value */
353 void HELPER(exception_with_syndrome
)(CPUARMState
*env
, uint32_t excp
,
354 uint32_t syndrome
, uint32_t target_el
)
356 raise_exception(env
, excp
, syndrome
, target_el
);
359 /* Raise an EXCP_BKPT with the specified syndrome register value,
360 * targeting the correct exception level for debug exceptions.
362 void HELPER(exception_bkpt_insn
)(CPUARMState
*env
, uint32_t syndrome
)
364 int debug_el
= arm_debug_target_el(env
);
365 int cur_el
= arm_current_el(env
);
367 /* FSR will only be used if the debug target EL is AArch32. */
368 env
->exception
.fsr
= arm_debug_exception_fsr(env
);
369 /* FAR is UNKNOWN: clear vaddress to avoid potentially exposing
370 * values to the guest that it shouldn't be able to see at its
371 * exception/security level.
373 env
->exception
.vaddress
= 0;
375 * Other kinds of architectural debug exception are ignored if
376 * they target an exception level below the current one (in QEMU
377 * this is checked by arm_generate_debug_exceptions()). Breakpoint
378 * instructions are special because they always generate an exception
379 * to somewhere: if they can't go to the configured debug exception
380 * level they are taken to the current exception level.
382 if (debug_el
< cur_el
) {
385 raise_exception(env
, EXCP_BKPT
, syndrome
, debug_el
);
388 uint32_t HELPER(cpsr_read
)(CPUARMState
*env
)
390 return cpsr_read(env
) & ~(CPSR_EXEC
| CPSR_RESERVED
);
393 void HELPER(cpsr_write
)(CPUARMState
*env
, uint32_t val
, uint32_t mask
)
395 cpsr_write(env
, val
, mask
, CPSRWriteByInstr
);
396 /* TODO: Not all cpsr bits are relevant to hflags. */
397 arm_rebuild_hflags(env
);
400 /* Write the CPSR for a 32-bit exception return */
401 void HELPER(cpsr_write_eret
)(CPUARMState
*env
, uint32_t val
)
403 qemu_mutex_lock_iothread();
404 arm_call_pre_el_change_hook(env_archcpu(env
));
405 qemu_mutex_unlock_iothread();
407 cpsr_write(env
, val
, CPSR_ERET_MASK
, CPSRWriteExceptionReturn
);
409 /* Generated code has already stored the new PC value, but
410 * without masking out its low bits, because which bits need
411 * masking depends on whether we're returning to Thumb or ARM
412 * state. Do the masking now.
414 env
->regs
[15] &= (env
->thumb
? ~1 : ~3);
415 arm_rebuild_hflags(env
);
417 qemu_mutex_lock_iothread();
418 arm_call_el_change_hook(env_archcpu(env
));
419 qemu_mutex_unlock_iothread();
422 /* Access to user mode registers from privileged modes. */
423 uint32_t HELPER(get_user_reg
)(CPUARMState
*env
, uint32_t regno
)
428 val
= env
->banked_r13
[BANK_USRSYS
];
429 } else if (regno
== 14) {
430 val
= env
->banked_r14
[BANK_USRSYS
];
431 } else if (regno
>= 8
432 && (env
->uncached_cpsr
& 0x1f) == ARM_CPU_MODE_FIQ
) {
433 val
= env
->usr_regs
[regno
- 8];
435 val
= env
->regs
[regno
];
440 void HELPER(set_user_reg
)(CPUARMState
*env
, uint32_t regno
, uint32_t val
)
443 env
->banked_r13
[BANK_USRSYS
] = val
;
444 } else if (regno
== 14) {
445 env
->banked_r14
[BANK_USRSYS
] = val
;
446 } else if (regno
>= 8
447 && (env
->uncached_cpsr
& 0x1f) == ARM_CPU_MODE_FIQ
) {
448 env
->usr_regs
[regno
- 8] = val
;
450 env
->regs
[regno
] = val
;
454 void HELPER(set_r13_banked
)(CPUARMState
*env
, uint32_t mode
, uint32_t val
)
456 if ((env
->uncached_cpsr
& CPSR_M
) == mode
) {
459 env
->banked_r13
[bank_number(mode
)] = val
;
463 uint32_t HELPER(get_r13_banked
)(CPUARMState
*env
, uint32_t mode
)
465 if ((env
->uncached_cpsr
& CPSR_M
) == ARM_CPU_MODE_SYS
) {
466 /* SRS instruction is UNPREDICTABLE from System mode; we UNDEF.
467 * Other UNPREDICTABLE and UNDEF cases were caught at translate time.
469 raise_exception(env
, EXCP_UDEF
, syn_uncategorized(),
470 exception_target_el(env
));
473 if ((env
->uncached_cpsr
& CPSR_M
) == mode
) {
474 return env
->regs
[13];
476 return env
->banked_r13
[bank_number(mode
)];
480 static void msr_mrs_banked_exc_checks(CPUARMState
*env
, uint32_t tgtmode
,
483 /* Raise an exception if the requested access is one of the UNPREDICTABLE
484 * cases; otherwise return. This broadly corresponds to the pseudocode
485 * BankedRegisterAccessValid() and SPSRAccessValid(),
486 * except that we have already handled some cases at translate time.
488 int curmode
= env
->uncached_cpsr
& CPSR_M
;
491 /* ELR_Hyp: a special case because access from tgtmode is OK */
492 if (curmode
!= ARM_CPU_MODE_HYP
&& curmode
!= ARM_CPU_MODE_MON
) {
498 if (curmode
== tgtmode
) {
502 if (tgtmode
== ARM_CPU_MODE_USR
) {
505 if (curmode
!= ARM_CPU_MODE_FIQ
) {
510 if (curmode
== ARM_CPU_MODE_SYS
) {
515 if (curmode
== ARM_CPU_MODE_HYP
|| curmode
== ARM_CPU_MODE_SYS
) {
524 if (tgtmode
== ARM_CPU_MODE_HYP
) {
525 /* SPSR_Hyp, r13_hyp: accessible from Monitor mode only */
526 if (curmode
!= ARM_CPU_MODE_MON
) {
534 raise_exception(env
, EXCP_UDEF
, syn_uncategorized(),
535 exception_target_el(env
));
538 void HELPER(msr_banked
)(CPUARMState
*env
, uint32_t value
, uint32_t tgtmode
,
541 msr_mrs_banked_exc_checks(env
, tgtmode
, regno
);
545 env
->banked_spsr
[bank_number(tgtmode
)] = value
;
547 case 17: /* ELR_Hyp */
548 env
->elr_el
[2] = value
;
551 env
->banked_r13
[bank_number(tgtmode
)] = value
;
554 env
->banked_r14
[r14_bank_number(tgtmode
)] = value
;
558 case ARM_CPU_MODE_USR
:
559 env
->usr_regs
[regno
- 8] = value
;
561 case ARM_CPU_MODE_FIQ
:
562 env
->fiq_regs
[regno
- 8] = value
;
565 g_assert_not_reached();
569 g_assert_not_reached();
573 uint32_t HELPER(mrs_banked
)(CPUARMState
*env
, uint32_t tgtmode
, uint32_t regno
)
575 msr_mrs_banked_exc_checks(env
, tgtmode
, regno
);
579 return env
->banked_spsr
[bank_number(tgtmode
)];
580 case 17: /* ELR_Hyp */
581 return env
->elr_el
[2];
583 return env
->banked_r13
[bank_number(tgtmode
)];
585 return env
->banked_r14
[r14_bank_number(tgtmode
)];
588 case ARM_CPU_MODE_USR
:
589 return env
->usr_regs
[regno
- 8];
590 case ARM_CPU_MODE_FIQ
:
591 return env
->fiq_regs
[regno
- 8];
593 g_assert_not_reached();
596 g_assert_not_reached();
600 void HELPER(access_check_cp_reg
)(CPUARMState
*env
, void *rip
, uint32_t syndrome
,
603 const ARMCPRegInfo
*ri
= rip
;
606 if (arm_feature(env
, ARM_FEATURE_XSCALE
) && ri
->cp
< 14
607 && extract32(env
->cp15
.c15_cpar
, ri
->cp
, 1) == 0) {
608 raise_exception(env
, EXCP_UDEF
, syndrome
, exception_target_el(env
));
612 * Check for an EL2 trap due to HSTR_EL2. We expect EL0 accesses
613 * to sysregs non accessible at EL0 to have UNDEF-ed already.
615 if (!is_a64(env
) && arm_current_el(env
) < 2 && ri
->cp
== 15 &&
616 (arm_hcr_el2_eff(env
) & (HCR_E2H
| HCR_TGE
)) != (HCR_E2H
| HCR_TGE
)) {
617 uint32_t mask
= 1 << ri
->crn
;
619 if (ri
->type
& ARM_CP_64BIT
) {
623 /* T4 and T14 are RES0 */
624 mask
&= ~((1 << 4) | (1 << 14));
626 if (env
->cp15
.hstr_el2
& mask
) {
636 switch (ri
->accessfn(env
, ri
, isread
)) {
640 target_el
= exception_target_el(env
);
642 case CP_ACCESS_TRAP_EL2
:
643 /* Requesting a trap to EL2 when we're in EL3 or S-EL0/1 is
644 * a bug in the access function.
646 assert(!arm_is_secure(env
) && arm_current_el(env
) != 3);
649 case CP_ACCESS_TRAP_EL3
:
652 case CP_ACCESS_TRAP_UNCATEGORIZED
:
653 target_el
= exception_target_el(env
);
654 syndrome
= syn_uncategorized();
656 case CP_ACCESS_TRAP_UNCATEGORIZED_EL2
:
658 syndrome
= syn_uncategorized();
660 case CP_ACCESS_TRAP_UNCATEGORIZED_EL3
:
662 syndrome
= syn_uncategorized();
664 case CP_ACCESS_TRAP_FP_EL2
:
666 /* Since we are an implementation that takes exceptions on a trapped
667 * conditional insn only if the insn has passed its condition code
668 * check, we take the IMPDEF choice to always report CV=1 COND=0xe
669 * (which is also the required value for AArch64 traps).
671 syndrome
= syn_fp_access_trap(1, 0xe, false);
673 case CP_ACCESS_TRAP_FP_EL3
:
675 syndrome
= syn_fp_access_trap(1, 0xe, false);
678 g_assert_not_reached();
682 raise_exception(env
, EXCP_UDEF
, syndrome
, target_el
);
685 void HELPER(set_cp_reg
)(CPUARMState
*env
, void *rip
, uint32_t value
)
687 const ARMCPRegInfo
*ri
= rip
;
689 if (ri
->type
& ARM_CP_IO
) {
690 qemu_mutex_lock_iothread();
691 ri
->writefn(env
, ri
, value
);
692 qemu_mutex_unlock_iothread();
694 ri
->writefn(env
, ri
, value
);
698 uint32_t HELPER(get_cp_reg
)(CPUARMState
*env
, void *rip
)
700 const ARMCPRegInfo
*ri
= rip
;
703 if (ri
->type
& ARM_CP_IO
) {
704 qemu_mutex_lock_iothread();
705 res
= ri
->readfn(env
, ri
);
706 qemu_mutex_unlock_iothread();
708 res
= ri
->readfn(env
, ri
);
714 void HELPER(set_cp_reg64
)(CPUARMState
*env
, void *rip
, uint64_t value
)
716 const ARMCPRegInfo
*ri
= rip
;
718 if (ri
->type
& ARM_CP_IO
) {
719 qemu_mutex_lock_iothread();
720 ri
->writefn(env
, ri
, value
);
721 qemu_mutex_unlock_iothread();
723 ri
->writefn(env
, ri
, value
);
727 uint64_t HELPER(get_cp_reg64
)(CPUARMState
*env
, void *rip
)
729 const ARMCPRegInfo
*ri
= rip
;
732 if (ri
->type
& ARM_CP_IO
) {
733 qemu_mutex_lock_iothread();
734 res
= ri
->readfn(env
, ri
);
735 qemu_mutex_unlock_iothread();
737 res
= ri
->readfn(env
, ri
);
743 void HELPER(pre_hvc
)(CPUARMState
*env
)
745 ARMCPU
*cpu
= env_archcpu(env
);
746 int cur_el
= arm_current_el(env
);
747 /* FIXME: Use actual secure state. */
751 if (arm_is_psci_call(cpu
, EXCP_HVC
)) {
752 /* If PSCI is enabled and this looks like a valid PSCI call then
753 * that overrides the architecturally mandated HVC behaviour.
758 if (!arm_feature(env
, ARM_FEATURE_EL2
)) {
759 /* If EL2 doesn't exist, HVC always UNDEFs */
761 } else if (arm_feature(env
, ARM_FEATURE_EL3
)) {
762 /* EL3.HCE has priority over EL2.HCD. */
763 undef
= !(env
->cp15
.scr_el3
& SCR_HCE
);
765 undef
= env
->cp15
.hcr_el2
& HCR_HCD
;
768 /* In ARMv7 and ARMv8/AArch32, HVC is undef in secure state.
769 * For ARMv8/AArch64, HVC is allowed in EL3.
770 * Note that we've already trapped HVC from EL0 at translation
773 if (secure
&& (!is_a64(env
) || cur_el
== 1)) {
778 raise_exception(env
, EXCP_UDEF
, syn_uncategorized(),
779 exception_target_el(env
));
783 void HELPER(pre_smc
)(CPUARMState
*env
, uint32_t syndrome
)
785 ARMCPU
*cpu
= env_archcpu(env
);
786 int cur_el
= arm_current_el(env
);
787 bool secure
= arm_is_secure(env
);
788 bool smd_flag
= env
->cp15
.scr_el3
& SCR_SMD
;
791 * SMC behaviour is summarized in the following table.
792 * This helper handles the "Trap to EL2" and "Undef insn" cases.
793 * The "Trap to EL3" and "PSCI call" cases are handled in the exception
796 * -> ARM_FEATURE_EL3 and !SMD
797 * HCR_TSC && NS EL1 !HCR_TSC || !NS EL1
799 * Conduit SMC, valid call Trap to EL2 PSCI Call
800 * Conduit SMC, inval call Trap to EL2 Trap to EL3
801 * Conduit not SMC Trap to EL2 Trap to EL3
804 * -> ARM_FEATURE_EL3 and SMD
805 * HCR_TSC && NS EL1 !HCR_TSC || !NS EL1
807 * Conduit SMC, valid call Trap to EL2 PSCI Call
808 * Conduit SMC, inval call Trap to EL2 Undef insn
809 * Conduit not SMC Trap to EL2 Undef insn
812 * -> !ARM_FEATURE_EL3
813 * HCR_TSC && NS EL1 !HCR_TSC || !NS EL1
815 * Conduit SMC, valid call Trap to EL2 PSCI Call
816 * Conduit SMC, inval call Trap to EL2 Undef insn
817 * Conduit not SMC Undef insn Undef insn
820 /* On ARMv8 with EL3 AArch64, SMD applies to both S and NS state.
821 * On ARMv8 with EL3 AArch32, or ARMv7 with the Virtualization
822 * extensions, SMD only applies to NS state.
823 * On ARMv7 without the Virtualization extensions, the SMD bit
824 * doesn't exist, but we forbid the guest to set it to 1 in scr_write(),
825 * so we need not special case this here.
827 bool smd
= arm_feature(env
, ARM_FEATURE_AARCH64
) ? smd_flag
828 : smd_flag
&& !secure
;
830 if (!arm_feature(env
, ARM_FEATURE_EL3
) &&
831 cpu
->psci_conduit
!= QEMU_PSCI_CONDUIT_SMC
) {
832 /* If we have no EL3 then SMC always UNDEFs and can't be
833 * trapped to EL2. PSCI-via-SMC is a sort of ersatz EL3
834 * firmware within QEMU, and we want an EL2 guest to be able
835 * to forbid its EL1 from making PSCI calls into QEMU's
836 * "firmware" via HCR.TSC, so for these purposes treat
837 * PSCI-via-SMC as implying an EL3.
838 * This handles the very last line of the previous table.
840 raise_exception(env
, EXCP_UDEF
, syn_uncategorized(),
841 exception_target_el(env
));
844 if (cur_el
== 1 && (arm_hcr_el2_eff(env
) & HCR_TSC
)) {
845 /* In NS EL1, HCR controlled routing to EL2 has priority over SMD.
846 * We also want an EL2 guest to be able to forbid its EL1 from
847 * making PSCI calls into QEMU's "firmware" via HCR.TSC.
848 * This handles all the "Trap to EL2" cases of the previous table.
850 raise_exception(env
, EXCP_HYP_TRAP
, syndrome
, 2);
853 /* Catch the two remaining "Undef insn" cases of the previous table:
854 * - PSCI conduit is SMC but we don't have a valid PCSI call,
855 * - We don't have EL3 or SMD is set.
857 if (!arm_is_psci_call(cpu
, EXCP_SMC
) &&
858 (smd
|| !arm_feature(env
, ARM_FEATURE_EL3
))) {
859 raise_exception(env
, EXCP_UDEF
, syn_uncategorized(),
860 exception_target_el(env
));
864 /* ??? Flag setting arithmetic is awkward because we need to do comparisons.
865 The only way to do that in TCG is a conditional branch, which clobbers
866 all our temporaries. For now implement these as helper functions. */
868 /* Similarly for variable shift instructions. */
870 uint32_t HELPER(shl_cc
)(CPUARMState
*env
, uint32_t x
, uint32_t i
)
872 int shift
= i
& 0xff;
879 } else if (shift
!= 0) {
880 env
->CF
= (x
>> (32 - shift
)) & 1;
886 uint32_t HELPER(shr_cc
)(CPUARMState
*env
, uint32_t x
, uint32_t i
)
888 int shift
= i
& 0xff;
891 env
->CF
= (x
>> 31) & 1;
895 } else if (shift
!= 0) {
896 env
->CF
= (x
>> (shift
- 1)) & 1;
902 uint32_t HELPER(sar_cc
)(CPUARMState
*env
, uint32_t x
, uint32_t i
)
904 int shift
= i
& 0xff;
906 env
->CF
= (x
>> 31) & 1;
907 return (int32_t)x
>> 31;
908 } else if (shift
!= 0) {
909 env
->CF
= (x
>> (shift
- 1)) & 1;
910 return (int32_t)x
>> shift
;
915 uint32_t HELPER(ror_cc
)(CPUARMState
*env
, uint32_t x
, uint32_t i
)
919 shift
= shift1
& 0x1f;
922 env
->CF
= (x
>> 31) & 1;
925 env
->CF
= (x
>> (shift
- 1)) & 1;
926 return ((uint32_t)x
>> shift
) | (x
<< (32 - shift
));
930 void HELPER(dc_zva
)(CPUARMState
*env
, uint64_t vaddr_in
)
933 * Implement DC ZVA, which zeroes a fixed-length block of memory.
934 * Note that we do not implement the (architecturally mandated)
935 * alignment fault for attempts to use this on Device memory
936 * (which matches the usual QEMU behaviour of not implementing either
937 * alignment faults or any memory attribute handling).
940 ARMCPU
*cpu
= env_archcpu(env
);
941 uint64_t blocklen
= 4 << cpu
->dcz_blocksize
;
942 uint64_t vaddr
= vaddr_in
& ~(blocklen
- 1);
944 #ifndef CONFIG_USER_ONLY
947 * Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
948 * the block size so we might have to do more than one TLB lookup.
949 * We know that in fact for any v8 CPU the page size is at least 4K
950 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
951 * 1K as an artefact of legacy v5 subpage support being present in the
952 * same QEMU executable. So in practice the hostaddr[] array has
953 * two entries, given the current setting of TARGET_PAGE_BITS_MIN.
955 int maxidx
= DIV_ROUND_UP(blocklen
, TARGET_PAGE_SIZE
);
956 void *hostaddr
[DIV_ROUND_UP(2 * KiB
, 1 << TARGET_PAGE_BITS_MIN
)];
958 unsigned mmu_idx
= cpu_mmu_index(env
, false);
959 TCGMemOpIdx oi
= make_memop_idx(MO_UB
, mmu_idx
);
961 assert(maxidx
<= ARRAY_SIZE(hostaddr
));
963 for (try = 0; try < 2; try++) {
965 for (i
= 0; i
< maxidx
; i
++) {
966 hostaddr
[i
] = tlb_vaddr_to_host(env
,
967 vaddr
+ TARGET_PAGE_SIZE
* i
,
975 * If it's all in the TLB it's fair game for just writing to;
976 * we know we don't need to update dirty status, etc.
978 for (i
= 0; i
< maxidx
- 1; i
++) {
979 memset(hostaddr
[i
], 0, TARGET_PAGE_SIZE
);
981 memset(hostaddr
[i
], 0, blocklen
- (i
* TARGET_PAGE_SIZE
));
985 * OK, try a store and see if we can populate the tlb. This
986 * might cause an exception if the memory isn't writable,
987 * in which case we will longjmp out of here. We must for
988 * this purpose use the actual register value passed to us
989 * so that we get the fault address right.
991 helper_ret_stb_mmu(env
, vaddr_in
, 0, oi
, GETPC());
992 /* Now we can populate the other TLB entries, if any */
993 for (i
= 0; i
< maxidx
; i
++) {
994 uint64_t va
= vaddr
+ TARGET_PAGE_SIZE
* i
;
995 if (va
!= (vaddr_in
& TARGET_PAGE_MASK
)) {
996 helper_ret_stb_mmu(env
, va
, 0, oi
, GETPC());
1002 * Slow path (probably attempt to do this to an I/O device or
1003 * similar, or clearing of a block of code we have translations
1004 * cached for). Just do a series of byte writes as the architecture
1005 * demands. It's not worth trying to use a cpu_physical_memory_map(),
1006 * memset(), unmap() sequence here because:
1007 * + we'd need to account for the blocksize being larger than a page
1008 * + the direct-RAM access case is almost always going to be dealt
1009 * with in the fastpath code above, so there's no speed benefit
1010 * + we would have to deal with the map returning NULL because the
1011 * bounce buffer was in use
1013 for (i
= 0; i
< blocklen
; i
++) {
1014 helper_ret_stb_mmu(env
, vaddr
+ i
, 0, oi
, GETPC());
1018 memset(g2h(vaddr
), 0, blocklen
);