Merge remote-tracking branch 'qemu-project/master'
[qemu/ar7.git] / target / arm / tcg / op_helper.c
blobd64b6002fa0db328a0592be04aad05ca78715ca3
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.1 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/main-loop.h"
21 #include "cpu.h"
22 #include "exec/helper-proto.h"
23 #include "internals.h"
24 #include "cpu-features.h"
25 #include "exec/exec-all.h"
26 #include "exec/cpu_ldst.h"
27 #include "cpregs.h"
29 #define SIGNBIT (uint32_t)0x80000000
30 #define SIGNBIT64 ((uint64_t)1 << 63)
32 int exception_target_el(CPUARMState *env)
34 int target_el = MAX(1, arm_current_el(env));
37 * No such thing as secure EL1 if EL3 is aarch32,
38 * so update the target EL to EL3 in this case.
40 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3) && target_el == 1) {
41 target_el = 3;
44 return target_el;
47 void raise_exception(CPUARMState *env, uint32_t excp,
48 uint32_t syndrome, uint32_t target_el)
50 CPUState *cs = env_cpu(env);
52 if (target_el == 1 && (arm_hcr_el2_eff(env) & HCR_TGE)) {
54 * Redirect NS EL1 exceptions to NS EL2. These are reported with
55 * their original syndrome register value, with the exception of
56 * SIMD/FP access traps, which are reported as uncategorized
57 * (see DDI0478C.a D1.10.4)
59 target_el = 2;
60 if (syn_get_ec(syndrome) == EC_ADVSIMDFPACCESSTRAP) {
61 syndrome = syn_uncategorized();
65 assert(!excp_is_internal(excp));
66 cs->exception_index = excp;
67 env->exception.syndrome = syndrome;
68 env->exception.target_el = target_el;
69 cpu_loop_exit(cs);
72 void raise_exception_ra(CPUARMState *env, uint32_t excp, uint32_t syndrome,
73 uint32_t target_el, uintptr_t ra)
75 CPUState *cs = env_cpu(env);
78 * restore_state_to_opc() will set env->exception.syndrome, so
79 * we must restore CPU state here before setting the syndrome
80 * the caller passed us, and cannot use cpu_loop_exit_restore().
82 cpu_restore_state(cs, ra);
83 raise_exception(env, excp, syndrome, target_el);
86 uint64_t HELPER(neon_tbl)(CPUARMState *env, uint32_t desc,
87 uint64_t ireg, uint64_t def)
89 uint64_t tmp, val = 0;
90 uint32_t maxindex = ((desc & 3) + 1) * 8;
91 uint32_t base_reg = desc >> 2;
92 uint32_t shift, index, reg;
94 for (shift = 0; shift < 64; shift += 8) {
95 index = (ireg >> shift) & 0xff;
96 if (index < maxindex) {
97 reg = base_reg + (index >> 3);
98 tmp = *aa32_vfp_dreg(env, reg);
99 tmp = ((tmp >> ((index & 7) << 3)) & 0xff) << shift;
100 } else {
101 tmp = def & (0xffull << shift);
103 val |= tmp;
105 return val;
108 void HELPER(v8m_stackcheck)(CPUARMState *env, uint32_t newvalue)
111 * Perform the v8M stack limit check for SP updates from translated code,
112 * raising an exception if the limit is breached.
114 if (newvalue < v7m_sp_limit(env)) {
116 * Stack limit exceptions are a rare case, so rather than syncing
117 * PC/condbits before the call, we use raise_exception_ra() so
118 * that cpu_restore_state() will sort them out.
120 raise_exception_ra(env, EXCP_STKOF, 0, 1, GETPC());
124 /* Sign/zero extend */
125 uint32_t HELPER(sxtb16)(uint32_t x)
127 uint32_t res;
128 res = (uint16_t)(int8_t)x;
129 res |= (uint32_t)(int8_t)(x >> 16) << 16;
130 return res;
133 static void handle_possible_div0_trap(CPUARMState *env, uintptr_t ra)
136 * Take a division-by-zero exception if necessary; otherwise return
137 * to get the usual non-trapping division behaviour (result of 0)
139 if (arm_feature(env, ARM_FEATURE_M)
140 && (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_DIV_0_TRP_MASK)) {
141 raise_exception_ra(env, EXCP_DIVBYZERO, 0, 1, ra);
145 uint32_t HELPER(uxtb16)(uint32_t x)
147 uint32_t res;
148 res = (uint16_t)(uint8_t)x;
149 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
150 return res;
153 int32_t HELPER(sdiv)(CPUARMState *env, int32_t num, int32_t den)
155 if (den == 0) {
156 handle_possible_div0_trap(env, GETPC());
157 return 0;
159 if (num == INT_MIN && den == -1) {
160 return INT_MIN;
162 return num / den;
165 uint32_t HELPER(udiv)(CPUARMState *env, uint32_t num, uint32_t den)
167 if (den == 0) {
168 handle_possible_div0_trap(env, GETPC());
169 return 0;
171 return num / den;
174 uint32_t HELPER(rbit)(uint32_t x)
176 return revbit32(x);
179 uint32_t HELPER(add_setq)(CPUARMState *env, uint32_t a, uint32_t b)
181 uint32_t res = a + b;
182 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT))
183 env->QF = 1;
184 return res;
187 uint32_t HELPER(add_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
189 uint32_t res = a + b;
190 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT)) {
191 env->QF = 1;
192 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
194 return res;
197 uint32_t HELPER(sub_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
199 uint32_t res = a - b;
200 if (((res ^ a) & SIGNBIT) && ((a ^ b) & SIGNBIT)) {
201 env->QF = 1;
202 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
204 return res;
207 uint32_t HELPER(add_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
209 uint32_t res = a + b;
210 if (res < a) {
211 env->QF = 1;
212 res = ~0;
214 return res;
217 uint32_t HELPER(sub_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
219 uint32_t res = a - b;
220 if (res > a) {
221 env->QF = 1;
222 res = 0;
224 return res;
227 /* Signed saturation. */
228 static inline uint32_t do_ssat(CPUARMState *env, int32_t val, int shift)
230 int32_t top;
231 uint32_t mask;
233 top = val >> shift;
234 mask = (1u << shift) - 1;
235 if (top > 0) {
236 env->QF = 1;
237 return mask;
238 } else if (top < -1) {
239 env->QF = 1;
240 return ~mask;
242 return val;
245 /* Unsigned saturation. */
246 static inline uint32_t do_usat(CPUARMState *env, int32_t val, int shift)
248 uint32_t max;
250 max = (1u << shift) - 1;
251 if (val < 0) {
252 env->QF = 1;
253 return 0;
254 } else if (val > max) {
255 env->QF = 1;
256 return max;
258 return val;
261 /* Signed saturate. */
262 uint32_t HELPER(ssat)(CPUARMState *env, uint32_t x, uint32_t shift)
264 return do_ssat(env, x, shift);
267 /* Dual halfword signed saturate. */
268 uint32_t HELPER(ssat16)(CPUARMState *env, uint32_t x, uint32_t shift)
270 uint32_t res;
272 res = (uint16_t)do_ssat(env, (int16_t)x, shift);
273 res |= do_ssat(env, ((int32_t)x) >> 16, shift) << 16;
274 return res;
277 /* Unsigned saturate. */
278 uint32_t HELPER(usat)(CPUARMState *env, uint32_t x, uint32_t shift)
280 return do_usat(env, x, shift);
283 /* Dual halfword unsigned saturate. */
284 uint32_t HELPER(usat16)(CPUARMState *env, uint32_t x, uint32_t shift)
286 uint32_t res;
288 res = (uint16_t)do_usat(env, (int16_t)x, shift);
289 res |= do_usat(env, ((int32_t)x) >> 16, shift) << 16;
290 return res;
293 void HELPER(setend)(CPUARMState *env)
295 env->uncached_cpsr ^= CPSR_E;
296 arm_rebuild_hflags(env);
299 void HELPER(check_bxj_trap)(CPUARMState *env, uint32_t rm)
302 * Only called if in NS EL0 or EL1 for a BXJ for a v7A CPU;
303 * check if HSTR.TJDBX means we need to trap to EL2.
305 if (env->cp15.hstr_el2 & HSTR_TJDBX) {
307 * We know the condition code check passed, so take the IMPDEF
308 * choice to always report CV=1 COND 0xe
310 uint32_t syn = syn_bxjtrap(1, 0xe, rm);
311 raise_exception_ra(env, EXCP_HYP_TRAP, syn, 2, GETPC());
315 #ifndef CONFIG_USER_ONLY
316 /* Function checks whether WFx (WFI/WFE) instructions are set up to be trapped.
317 * The function returns the target EL (1-3) if the instruction is to be trapped;
318 * otherwise it returns 0 indicating it is not trapped.
320 static inline int check_wfx_trap(CPUARMState *env, bool is_wfe)
322 int cur_el = arm_current_el(env);
323 uint64_t mask;
325 if (arm_feature(env, ARM_FEATURE_M)) {
326 /* M profile cores can never trap WFI/WFE. */
327 return 0;
330 /* If we are currently in EL0 then we need to check if SCTLR is set up for
331 * WFx instructions being trapped to EL1. These trap bits don't exist in v7.
333 if (cur_el < 1 && arm_feature(env, ARM_FEATURE_V8)) {
334 int target_el;
336 mask = is_wfe ? SCTLR_nTWE : SCTLR_nTWI;
337 if (arm_is_secure_below_el3(env) && !arm_el_is_aa64(env, 3)) {
338 /* Secure EL0 and Secure PL1 is at EL3 */
339 target_el = 3;
340 } else {
341 target_el = 1;
344 if (!(env->cp15.sctlr_el[target_el] & mask)) {
345 return target_el;
349 /* We are not trapping to EL1; trap to EL2 if HCR_EL2 requires it
350 * No need for ARM_FEATURE check as if HCR_EL2 doesn't exist the
351 * bits will be zero indicating no trap.
353 if (cur_el < 2) {
354 mask = is_wfe ? HCR_TWE : HCR_TWI;
355 if (arm_hcr_el2_eff(env) & mask) {
356 return 2;
360 /* We are not trapping to EL1 or EL2; trap to EL3 if SCR_EL3 requires it */
361 if (cur_el < 3) {
362 mask = (is_wfe) ? SCR_TWE : SCR_TWI;
363 if (env->cp15.scr_el3 & mask) {
364 return 3;
368 return 0;
370 #endif
372 void HELPER(wfi)(CPUARMState *env, uint32_t insn_len)
374 #ifdef CONFIG_USER_ONLY
376 * WFI in the user-mode emulator is technically permitted but not
377 * something any real-world code would do. AArch64 Linux kernels
378 * trap it via SCTRL_EL1.nTWI and make it an (expensive) NOP;
379 * AArch32 kernels don't trap it so it will delay a bit.
380 * For QEMU, make it NOP here, because trying to raise EXCP_HLT
381 * would trigger an abort.
383 return;
384 #else
385 CPUState *cs = env_cpu(env);
386 int target_el = check_wfx_trap(env, false);
388 if (cpu_has_work(cs)) {
389 /* Don't bother to go into our "low power state" if
390 * we would just wake up immediately.
392 return;
395 if (target_el) {
396 if (env->aarch64) {
397 env->pc -= insn_len;
398 } else {
399 env->regs[15] -= insn_len;
402 raise_exception(env, EXCP_UDEF, syn_wfx(1, 0xe, 0, insn_len == 2),
403 target_el);
406 cs->exception_index = EXCP_HLT;
407 cs->halted = 1;
408 cpu_loop_exit(cs);
409 #endif
412 void HELPER(wfit)(CPUARMState *env, uint64_t timeout)
414 #ifdef CONFIG_USER_ONLY
416 * WFI in the user-mode emulator is technically permitted but not
417 * something any real-world code would do. AArch64 Linux kernels
418 * trap it via SCTRL_EL1.nTWI and make it an (expensive) NOP;
419 * AArch32 kernels don't trap it so it will delay a bit.
420 * For QEMU, make it NOP here, because trying to raise EXCP_HLT
421 * would trigger an abort.
423 return;
424 #else
425 ARMCPU *cpu = env_archcpu(env);
426 CPUState *cs = env_cpu(env);
427 int target_el = check_wfx_trap(env, false);
428 /* The WFIT should time out when CNTVCT_EL0 >= the specified value. */
429 uint64_t cntval = gt_get_countervalue(env);
430 uint64_t offset = gt_virt_cnt_offset(env);
431 uint64_t cntvct = cntval - offset;
432 uint64_t nexttick;
434 if (cpu_has_work(cs) || cntvct >= timeout) {
436 * Don't bother to go into our "low power state" if
437 * we would just wake up immediately.
439 return;
442 if (target_el) {
443 env->pc -= 4;
444 raise_exception(env, EXCP_UDEF, syn_wfx(1, 0xe, 0, false),
445 target_el);
448 if (uadd64_overflow(timeout, offset, &nexttick)) {
449 nexttick = UINT64_MAX;
451 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) {
453 * If the timeout is too long for the signed 64-bit range
454 * of a QEMUTimer, let it expire early.
456 timer_mod_ns(cpu->wfxt_timer, INT64_MAX);
457 } else {
458 timer_mod(cpu->wfxt_timer, nexttick);
460 cs->exception_index = EXCP_HLT;
461 cs->halted = 1;
462 cpu_loop_exit(cs);
463 #endif
466 void HELPER(wfe)(CPUARMState *env)
468 /* This is a hint instruction that is semantically different
469 * from YIELD even though we currently implement it identically.
470 * Don't actually halt the CPU, just yield back to top
471 * level loop. This is not going into a "low power state"
472 * (ie halting until some event occurs), so we never take
473 * a configurable trap to a different exception level.
475 HELPER(yield)(env);
478 void G_NORETURN HELPER(yield)(CPUARMState *env)
480 CPUState *cs = env_cpu(env);
482 /* This is a non-trappable hint instruction that generally indicates
483 * that the guest is currently busy-looping. Yield control back to the
484 * top level loop so that a more deserving VCPU has a chance to run.
486 cs->exception_index = EXCP_YIELD;
487 cpu_loop_exit(cs);
490 /* Raise an internal-to-QEMU exception. This is limited to only
491 * those EXCP values which are special cases for QEMU to interrupt
492 * execution and not to be used for exceptions which are passed to
493 * the guest (those must all have syndrome information and thus should
494 * use exception_with_syndrome*).
496 void G_NORETURN HELPER(exception_internal)(CPUARMState *env, uint32_t excp)
498 CPUState *cs = env_cpu(env);
500 assert(excp_is_internal(excp));
501 cs->exception_index = excp;
502 cpu_loop_exit(cs);
505 /* Raise an exception with the specified syndrome register value */
506 void G_NORETURN
507 HELPER(exception_with_syndrome_el)(CPUARMState *env, uint32_t excp,
508 uint32_t syndrome, uint32_t target_el)
510 raise_exception(env, excp, syndrome, target_el);
514 * Raise an exception with the specified syndrome register value
515 * to the default target el.
517 void HELPER(exception_with_syndrome)(CPUARMState *env, uint32_t excp,
518 uint32_t syndrome)
520 raise_exception(env, excp, syndrome, exception_target_el(env));
523 uint32_t HELPER(cpsr_read)(CPUARMState *env)
525 return cpsr_read(env) & ~CPSR_EXEC;
528 void HELPER(cpsr_write)(CPUARMState *env, uint32_t val, uint32_t mask)
530 cpsr_write(env, val, mask, CPSRWriteByInstr);
531 /* TODO: Not all cpsr bits are relevant to hflags. */
532 arm_rebuild_hflags(env);
535 /* Write the CPSR for a 32-bit exception return */
536 void HELPER(cpsr_write_eret)(CPUARMState *env, uint32_t val)
538 uint32_t mask;
540 bql_lock();
541 arm_call_pre_el_change_hook(env_archcpu(env));
542 bql_unlock();
544 mask = aarch32_cpsr_valid_mask(env->features, &env_archcpu(env)->isar);
545 cpsr_write(env, val, mask, CPSRWriteExceptionReturn);
547 /* Generated code has already stored the new PC value, but
548 * without masking out its low bits, because which bits need
549 * masking depends on whether we're returning to Thumb or ARM
550 * state. Do the masking now.
552 env->regs[15] &= (env->thumb ? ~1 : ~3);
553 arm_rebuild_hflags(env);
555 bql_lock();
556 arm_call_el_change_hook(env_archcpu(env));
557 bql_unlock();
560 /* Access to user mode registers from privileged modes. */
561 uint32_t HELPER(get_user_reg)(CPUARMState *env, uint32_t regno)
563 uint32_t val;
565 if (regno == 13) {
566 val = env->banked_r13[BANK_USRSYS];
567 } else if (regno == 14) {
568 val = env->banked_r14[BANK_USRSYS];
569 } else if (regno >= 8
570 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
571 val = env->usr_regs[regno - 8];
572 } else {
573 val = env->regs[regno];
575 return val;
578 void HELPER(set_user_reg)(CPUARMState *env, uint32_t regno, uint32_t val)
580 if (regno == 13) {
581 env->banked_r13[BANK_USRSYS] = val;
582 } else if (regno == 14) {
583 env->banked_r14[BANK_USRSYS] = val;
584 } else if (regno >= 8
585 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
586 env->usr_regs[regno - 8] = val;
587 } else {
588 env->regs[regno] = val;
592 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
594 if ((env->uncached_cpsr & CPSR_M) == mode) {
595 env->regs[13] = val;
596 } else {
597 env->banked_r13[bank_number(mode)] = val;
601 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
603 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_SYS) {
604 /* SRS instruction is UNPREDICTABLE from System mode; we UNDEF.
605 * Other UNPREDICTABLE and UNDEF cases were caught at translate time.
607 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
608 exception_target_el(env));
611 if ((env->uncached_cpsr & CPSR_M) == mode) {
612 return env->regs[13];
613 } else {
614 return env->banked_r13[bank_number(mode)];
618 static void msr_mrs_banked_exc_checks(CPUARMState *env, uint32_t tgtmode,
619 uint32_t regno)
621 /* Raise an exception if the requested access is one of the UNPREDICTABLE
622 * cases; otherwise return. This broadly corresponds to the pseudocode
623 * BankedRegisterAccessValid() and SPSRAccessValid(),
624 * except that we have already handled some cases at translate time.
626 int curmode = env->uncached_cpsr & CPSR_M;
628 if (tgtmode == ARM_CPU_MODE_HYP) {
630 * Handle Hyp target regs first because some are special cases
631 * which don't want the usual "not accessible from tgtmode" check.
633 switch (regno) {
634 case 16 ... 17: /* ELR_Hyp, SPSR_Hyp */
635 if (curmode != ARM_CPU_MODE_HYP && curmode != ARM_CPU_MODE_MON) {
636 goto undef;
638 break;
639 case 13:
640 if (curmode != ARM_CPU_MODE_MON) {
641 goto undef;
643 break;
644 default:
645 g_assert_not_reached();
647 return;
650 if (curmode == tgtmode) {
651 goto undef;
654 if (tgtmode == ARM_CPU_MODE_USR) {
655 switch (regno) {
656 case 8 ... 12:
657 if (curmode != ARM_CPU_MODE_FIQ) {
658 goto undef;
660 break;
661 case 13:
662 if (curmode == ARM_CPU_MODE_SYS) {
663 goto undef;
665 break;
666 case 14:
667 if (curmode == ARM_CPU_MODE_HYP || curmode == ARM_CPU_MODE_SYS) {
668 goto undef;
670 break;
671 default:
672 break;
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 if (tgtmode == (env->uncached_cpsr & CPSR_M)) {
691 /* Only happens for SPSR_Hyp access in Hyp mode */
692 env->spsr = value;
693 } else {
694 env->banked_spsr[bank_number(tgtmode)] = value;
696 break;
697 case 17: /* ELR_Hyp */
698 env->elr_el[2] = value;
699 break;
700 case 13:
701 env->banked_r13[bank_number(tgtmode)] = value;
702 break;
703 case 14:
704 env->banked_r14[r14_bank_number(tgtmode)] = value;
705 break;
706 case 8 ... 12:
707 switch (tgtmode) {
708 case ARM_CPU_MODE_USR:
709 env->usr_regs[regno - 8] = value;
710 break;
711 case ARM_CPU_MODE_FIQ:
712 env->fiq_regs[regno - 8] = value;
713 break;
714 default:
715 g_assert_not_reached();
717 break;
718 default:
719 g_assert_not_reached();
723 uint32_t HELPER(mrs_banked)(CPUARMState *env, uint32_t tgtmode, uint32_t regno)
725 msr_mrs_banked_exc_checks(env, tgtmode, regno);
727 switch (regno) {
728 case 16: /* SPSRs */
729 if (tgtmode == (env->uncached_cpsr & CPSR_M)) {
730 /* Only happens for SPSR_Hyp access in Hyp mode */
731 return env->spsr;
732 } else {
733 return env->banked_spsr[bank_number(tgtmode)];
735 case 17: /* ELR_Hyp */
736 return env->elr_el[2];
737 case 13:
738 return env->banked_r13[bank_number(tgtmode)];
739 case 14:
740 return env->banked_r14[r14_bank_number(tgtmode)];
741 case 8 ... 12:
742 switch (tgtmode) {
743 case ARM_CPU_MODE_USR:
744 return env->usr_regs[regno - 8];
745 case ARM_CPU_MODE_FIQ:
746 return env->fiq_regs[regno - 8];
747 default:
748 g_assert_not_reached();
750 default:
751 g_assert_not_reached();
755 const void *HELPER(access_check_cp_reg)(CPUARMState *env, uint32_t key,
756 uint32_t syndrome, uint32_t isread)
758 ARMCPU *cpu = env_archcpu(env);
759 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, key);
760 CPAccessResult res = CP_ACCESS_OK;
761 int target_el;
763 assert(ri != NULL);
765 if (arm_feature(env, ARM_FEATURE_XSCALE) && ri->cp < 14
766 && extract32(env->cp15.c15_cpar, ri->cp, 1) == 0) {
767 res = CP_ACCESS_TRAP;
768 goto fail;
771 if (ri->accessfn) {
772 res = ri->accessfn(env, ri, isread);
776 * If the access function indicates a trap from EL0 to EL1 then
777 * that always takes priority over the HSTR_EL2 trap. (If it indicates
778 * a trap to EL3, then the HSTR_EL2 trap takes priority; if it indicates
779 * a trap to EL2, then the syndrome is the same either way so we don't
780 * care whether technically the architecture says that HSTR_EL2 trap or
781 * the other trap takes priority. So we take the "check HSTR_EL2" path
782 * for all of those cases.)
784 if (res != CP_ACCESS_OK && ((res & CP_ACCESS_EL_MASK) == 0) &&
785 arm_current_el(env) == 0) {
786 goto fail;
790 * HSTR_EL2 traps from EL1 are checked earlier, in generated code;
791 * we only need to check here for traps from EL0.
793 if (!is_a64(env) && arm_current_el(env) == 0 && ri->cp == 15 &&
794 arm_is_el2_enabled(env) &&
795 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) {
796 uint32_t mask = 1 << ri->crn;
798 if (ri->type & ARM_CP_64BIT) {
799 mask = 1 << ri->crm;
802 /* T4 and T14 are RES0 */
803 mask &= ~((1 << 4) | (1 << 14));
805 if (env->cp15.hstr_el2 & mask) {
806 res = CP_ACCESS_TRAP_EL2;
807 goto fail;
812 * Fine-grained traps also are lower priority than undef-to-EL1,
813 * higher priority than trap-to-EL3, and we don't care about priority
814 * order with other EL2 traps because the syndrome value is the same.
816 if (arm_fgt_active(env, arm_current_el(env))) {
817 uint64_t trapword = 0;
818 unsigned int idx = FIELD_EX32(ri->fgt, FGT, IDX);
819 unsigned int bitpos = FIELD_EX32(ri->fgt, FGT, BITPOS);
820 bool rev = FIELD_EX32(ri->fgt, FGT, REV);
821 bool trapbit;
823 if (ri->fgt & FGT_EXEC) {
824 assert(idx < ARRAY_SIZE(env->cp15.fgt_exec));
825 trapword = env->cp15.fgt_exec[idx];
826 } else if (isread && (ri->fgt & FGT_R)) {
827 assert(idx < ARRAY_SIZE(env->cp15.fgt_read));
828 trapword = env->cp15.fgt_read[idx];
829 } else if (!isread && (ri->fgt & FGT_W)) {
830 assert(idx < ARRAY_SIZE(env->cp15.fgt_write));
831 trapword = env->cp15.fgt_write[idx];
834 trapbit = extract64(trapword, bitpos, 1);
835 if (trapbit != rev) {
836 res = CP_ACCESS_TRAP_EL2;
837 goto fail;
841 if (likely(res == CP_ACCESS_OK)) {
842 return ri;
845 fail:
846 switch (res & ~CP_ACCESS_EL_MASK) {
847 case CP_ACCESS_TRAP:
848 break;
849 case CP_ACCESS_TRAP_UNCATEGORIZED:
850 /* Only CP_ACCESS_TRAP traps are direct to a specified EL */
851 assert((res & CP_ACCESS_EL_MASK) == 0);
852 if (cpu_isar_feature(aa64_ids, cpu) && isread &&
853 arm_cpreg_in_idspace(ri)) {
855 * FEAT_IDST says this should be reported as EC_SYSTEMREGISTERTRAP,
856 * not EC_UNCATEGORIZED
858 break;
860 syndrome = syn_uncategorized();
861 break;
862 default:
863 g_assert_not_reached();
866 target_el = res & CP_ACCESS_EL_MASK;
867 switch (target_el) {
868 case 0:
869 target_el = exception_target_el(env);
870 break;
871 case 2:
872 assert(arm_current_el(env) != 3);
873 assert(arm_is_el2_enabled(env));
874 break;
875 case 3:
876 assert(arm_feature(env, ARM_FEATURE_EL3));
877 break;
878 default:
879 /* No "direct" traps to EL1 */
880 g_assert_not_reached();
883 raise_exception(env, EXCP_UDEF, syndrome, target_el);
886 const void *HELPER(lookup_cp_reg)(CPUARMState *env, uint32_t key)
888 ARMCPU *cpu = env_archcpu(env);
889 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, key);
891 assert(ri != NULL);
892 return ri;
896 * Test for HCR_EL2.TIDCP at EL1.
897 * Since implementation defined registers are rare, and within QEMU
898 * most of them are no-op, do not waste HFLAGS space for this and
899 * always use a helper.
901 void HELPER(tidcp_el1)(CPUARMState *env, uint32_t syndrome)
903 if (arm_hcr_el2_eff(env) & HCR_TIDCP) {
904 raise_exception_ra(env, EXCP_UDEF, syndrome, 2, GETPC());
909 * Similarly, for FEAT_TIDCP1 at EL0.
910 * We have already checked for the presence of the feature.
912 void HELPER(tidcp_el0)(CPUARMState *env, uint32_t syndrome)
914 /* See arm_sctlr(), but we also need the sctlr el. */
915 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0);
916 int target_el = mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1;
919 * The bit is not valid unless the target el is aa64, but since the
920 * bit test is simpler perform that first and check validity after.
922 if ((env->cp15.sctlr_el[target_el] & SCTLR_TIDCP)
923 && arm_el_is_aa64(env, target_el)) {
924 raise_exception_ra(env, EXCP_UDEF, syndrome, target_el, GETPC());
928 void HELPER(set_cp_reg)(CPUARMState *env, const void *rip, uint32_t value)
930 const ARMCPRegInfo *ri = rip;
932 if (ri->type & ARM_CP_IO) {
933 bql_lock();
934 ri->writefn(env, ri, value);
935 bql_unlock();
936 } else {
937 ri->writefn(env, ri, value);
941 uint32_t HELPER(get_cp_reg)(CPUARMState *env, const void *rip)
943 const ARMCPRegInfo *ri = rip;
944 uint32_t res;
946 if (ri->type & ARM_CP_IO) {
947 bql_lock();
948 res = ri->readfn(env, ri);
949 bql_unlock();
950 } else {
951 res = ri->readfn(env, ri);
954 return res;
957 void HELPER(set_cp_reg64)(CPUARMState *env, const void *rip, uint64_t value)
959 const ARMCPRegInfo *ri = rip;
961 if (ri->type & ARM_CP_IO) {
962 bql_lock();
963 ri->writefn(env, ri, value);
964 bql_unlock();
965 } else {
966 ri->writefn(env, ri, value);
970 uint64_t HELPER(get_cp_reg64)(CPUARMState *env, const void *rip)
972 const ARMCPRegInfo *ri = rip;
973 uint64_t res;
975 if (ri->type & ARM_CP_IO) {
976 bql_lock();
977 res = ri->readfn(env, ri);
978 bql_unlock();
979 } else {
980 res = ri->readfn(env, ri);
983 return res;
986 void HELPER(pre_hvc)(CPUARMState *env)
988 ARMCPU *cpu = env_archcpu(env);
989 int cur_el = arm_current_el(env);
990 /* FIXME: Use actual secure state. */
991 bool secure = false;
992 bool undef;
994 if (arm_is_psci_call(cpu, EXCP_HVC)) {
995 /* If PSCI is enabled and this looks like a valid PSCI call then
996 * that overrides the architecturally mandated HVC behaviour.
998 return;
1001 if (!arm_feature(env, ARM_FEATURE_EL2)) {
1002 /* If EL2 doesn't exist, HVC always UNDEFs */
1003 undef = true;
1004 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
1005 /* EL3.HCE has priority over EL2.HCD. */
1006 undef = !(env->cp15.scr_el3 & SCR_HCE);
1007 } else {
1008 undef = env->cp15.hcr_el2 & HCR_HCD;
1011 /* In ARMv7 and ARMv8/AArch32, HVC is undef in secure state.
1012 * For ARMv8/AArch64, HVC is allowed in EL3.
1013 * Note that we've already trapped HVC from EL0 at translation
1014 * time.
1016 if (secure && (!is_a64(env) || cur_el == 1)) {
1017 undef = true;
1020 if (undef) {
1021 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
1022 exception_target_el(env));
1026 void HELPER(pre_smc)(CPUARMState *env, uint32_t syndrome)
1028 ARMCPU *cpu = env_archcpu(env);
1029 int cur_el = arm_current_el(env);
1030 bool secure = arm_is_secure(env);
1031 bool smd_flag = env->cp15.scr_el3 & SCR_SMD;
1034 * SMC behaviour is summarized in the following table.
1035 * This helper handles the "Trap to EL2" and "Undef insn" cases.
1036 * The "Trap to EL3" and "PSCI call" cases are handled in the exception
1037 * helper.
1039 * -> ARM_FEATURE_EL3 and !SMD
1040 * HCR_TSC && NS EL1 !HCR_TSC || !NS EL1
1042 * Conduit SMC, valid call Trap to EL2 PSCI Call
1043 * Conduit SMC, inval call Trap to EL2 Trap to EL3
1044 * Conduit not SMC Trap to EL2 Trap to EL3
1047 * -> ARM_FEATURE_EL3 and SMD
1048 * HCR_TSC && NS EL1 !HCR_TSC || !NS EL1
1050 * Conduit SMC, valid call Trap to EL2 PSCI Call
1051 * Conduit SMC, inval call Trap to EL2 Undef insn
1052 * Conduit not SMC Trap to EL2 Undef insn
1055 * -> !ARM_FEATURE_EL3
1056 * HCR_TSC && NS EL1 !HCR_TSC || !NS EL1
1058 * Conduit SMC, valid call Trap to EL2 PSCI Call
1059 * Conduit SMC, inval call Trap to EL2 Undef insn
1060 * Conduit not SMC Undef or trap[1] Undef insn
1062 * [1] In this case:
1063 * - if HCR_EL2.NV == 1 we must trap to EL2
1064 * - if HCR_EL2.NV == 0 then newer architecture revisions permit
1065 * AArch64 (but not AArch32) to trap to EL2 as an IMPDEF choice
1066 * - otherwise we must UNDEF
1067 * We take the IMPDEF choice to always UNDEF if HCR_EL2.NV == 0.
1070 /* On ARMv8 with EL3 AArch64, SMD applies to both S and NS state.
1071 * On ARMv8 with EL3 AArch32, or ARMv7 with the Virtualization
1072 * extensions, SMD only applies to NS state.
1073 * On ARMv7 without the Virtualization extensions, the SMD bit
1074 * doesn't exist, but we forbid the guest to set it to 1 in scr_write(),
1075 * so we need not special case this here.
1077 bool smd = arm_feature(env, ARM_FEATURE_AARCH64) ? smd_flag
1078 : smd_flag && !secure;
1080 if (!arm_feature(env, ARM_FEATURE_EL3) &&
1081 !(arm_hcr_el2_eff(env) & HCR_NV) &&
1082 cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
1084 * If we have no EL3 then traditionally SMC always UNDEFs and can't be
1085 * trapped to EL2. For nested virtualization, SMC can be trapped to
1086 * the outer hypervisor. PSCI-via-SMC is a sort of ersatz EL3
1087 * firmware within QEMU, and we want an EL2 guest to be able
1088 * to forbid its EL1 from making PSCI calls into QEMU's
1089 * "firmware" via HCR.TSC, so for these purposes treat
1090 * PSCI-via-SMC as implying an EL3.
1091 * This handles the very last line of the previous table.
1093 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
1094 exception_target_el(env));
1097 if (cur_el == 1 && (arm_hcr_el2_eff(env) & HCR_TSC)) {
1098 /* In NS EL1, HCR controlled routing to EL2 has priority over SMD.
1099 * We also want an EL2 guest to be able to forbid its EL1 from
1100 * making PSCI calls into QEMU's "firmware" via HCR.TSC.
1101 * This handles all the "Trap to EL2" cases of the previous table.
1103 raise_exception(env, EXCP_HYP_TRAP, syndrome, 2);
1106 /* Catch the two remaining "Undef insn" cases of the previous table:
1107 * - PSCI conduit is SMC but we don't have a valid PCSI call,
1108 * - We don't have EL3 or SMD is set.
1110 if (!arm_is_psci_call(cpu, EXCP_SMC) &&
1111 (smd || !arm_feature(env, ARM_FEATURE_EL3))) {
1112 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
1113 exception_target_el(env));
1117 /* ??? Flag setting arithmetic is awkward because we need to do comparisons.
1118 The only way to do that in TCG is a conditional branch, which clobbers
1119 all our temporaries. For now implement these as helper functions. */
1121 /* Similarly for variable shift instructions. */
1123 uint32_t HELPER(shl_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1125 int shift = i & 0xff;
1126 if (shift >= 32) {
1127 if (shift == 32)
1128 env->CF = x & 1;
1129 else
1130 env->CF = 0;
1131 return 0;
1132 } else if (shift != 0) {
1133 env->CF = (x >> (32 - shift)) & 1;
1134 return x << shift;
1136 return x;
1139 uint32_t HELPER(shr_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1141 int shift = i & 0xff;
1142 if (shift >= 32) {
1143 if (shift == 32)
1144 env->CF = (x >> 31) & 1;
1145 else
1146 env->CF = 0;
1147 return 0;
1148 } else if (shift != 0) {
1149 env->CF = (x >> (shift - 1)) & 1;
1150 return x >> shift;
1152 return x;
1155 uint32_t HELPER(sar_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1157 int shift = i & 0xff;
1158 if (shift >= 32) {
1159 env->CF = (x >> 31) & 1;
1160 return (int32_t)x >> 31;
1161 } else if (shift != 0) {
1162 env->CF = (x >> (shift - 1)) & 1;
1163 return (int32_t)x >> shift;
1165 return x;
1168 uint32_t HELPER(ror_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1170 int shift1, shift;
1171 shift1 = i & 0xff;
1172 shift = shift1 & 0x1f;
1173 if (shift == 0) {
1174 if (shift1 != 0)
1175 env->CF = (x >> 31) & 1;
1176 return x;
1177 } else {
1178 env->CF = (x >> (shift - 1)) & 1;
1179 return ((uint32_t)x >> shift) | (x << (32 - shift));
1183 void HELPER(probe_access)(CPUARMState *env, target_ulong ptr,
1184 uint32_t access_type, uint32_t mmu_idx,
1185 uint32_t size)
1187 uint32_t in_page = -((uint32_t)ptr | TARGET_PAGE_SIZE);
1188 uintptr_t ra = GETPC();
1190 if (likely(size <= in_page)) {
1191 probe_access(env, ptr, size, access_type, mmu_idx, ra);
1192 } else {
1193 probe_access(env, ptr, in_page, access_type, mmu_idx, ra);
1194 probe_access(env, ptr + in_page, size - in_page,
1195 access_type, mmu_idx, ra);
1200 * This function corresponds to AArch64.vESBOperation().
1201 * Note that the AArch32 version is not functionally different.
1203 void HELPER(vesb)(CPUARMState *env)
1206 * The EL2Enabled() check is done inside arm_hcr_el2_eff,
1207 * and will return HCR_EL2.VSE == 0, so nothing happens.
1209 uint64_t hcr = arm_hcr_el2_eff(env);
1210 bool enabled = !(hcr & HCR_TGE) && (hcr & HCR_AMO);
1211 bool pending = enabled && (hcr & HCR_VSE);
1212 bool masked = (env->daif & PSTATE_A);
1214 /* If VSE pending and masked, defer the exception. */
1215 if (pending && masked) {
1216 uint32_t syndrome;
1218 if (arm_el_is_aa64(env, 1)) {
1219 /* Copy across IDS and ISS from VSESR. */
1220 syndrome = env->cp15.vsesr_el2 & 0x1ffffff;
1221 } else {
1222 ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal };
1224 if (extended_addresses_enabled(env)) {
1225 syndrome = arm_fi_to_lfsc(&fi);
1226 } else {
1227 syndrome = arm_fi_to_sfsc(&fi);
1229 /* Copy across AET and ExT from VSESR. */
1230 syndrome |= env->cp15.vsesr_el2 & 0xd000;
1233 /* Set VDISR_EL2.A along with the syndrome. */
1234 env->cp15.vdisr_el2 = syndrome | (1u << 31);
1236 /* Clear pending virtual SError */
1237 env->cp15.hcr_el2 &= ~HCR_VSE;
1238 cpu_reset_interrupt(env_cpu(env), CPU_INTERRUPT_VSERR);