block: Use children list in bdrv_refresh_filename
[qemu/ar7.git] / target / arm / op_helper.c
blobc998eadfaa804370206c475ba253fb82c69a83a1
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 static CPUState *do_raise_exception(CPUARMState *env, uint32_t excp,
32 uint32_t syndrome, uint32_t target_el)
34 CPUState *cs = CPU(arm_env_get_cpu(env));
36 if (target_el == 1 && (arm_hcr_el2_eff(env) & HCR_TGE)) {
38 * Redirect NS EL1 exceptions to NS EL2. These are reported with
39 * their original syndrome register value, with the exception of
40 * SIMD/FP access traps, which are reported as uncategorized
41 * (see DDI0478C.a D1.10.4)
43 target_el = 2;
44 if (syn_get_ec(syndrome) == EC_ADVSIMDFPACCESSTRAP) {
45 syndrome = syn_uncategorized();
49 assert(!excp_is_internal(excp));
50 cs->exception_index = excp;
51 env->exception.syndrome = syndrome;
52 env->exception.target_el = target_el;
54 return cs;
57 void raise_exception(CPUARMState *env, uint32_t excp,
58 uint32_t syndrome, uint32_t target_el)
60 CPUState *cs = do_raise_exception(env, excp, syndrome, target_el);
61 cpu_loop_exit(cs);
64 void raise_exception_ra(CPUARMState *env, uint32_t excp, uint32_t syndrome,
65 uint32_t target_el, uintptr_t ra)
67 CPUState *cs = do_raise_exception(env, excp, syndrome, target_el);
68 cpu_loop_exit_restore(cs, ra);
71 static int exception_target_el(CPUARMState *env)
73 int target_el = MAX(1, arm_current_el(env));
75 /* No such thing as secure EL1 if EL3 is aarch32, so update the target EL
76 * to EL3 in this case.
78 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3) && target_el == 1) {
79 target_el = 3;
82 return target_el;
85 uint32_t HELPER(neon_tbl)(uint32_t ireg, uint32_t def, void *vn,
86 uint32_t maxindex)
88 uint32_t val, shift;
89 uint64_t *table = vn;
91 val = 0;
92 for (shift = 0; shift < 32; shift += 8) {
93 uint32_t index = (ireg >> shift) & 0xff;
94 if (index < maxindex) {
95 uint32_t tmp = (table[index >> 3] >> ((index & 7) << 3)) & 0xff;
96 val |= tmp << shift;
97 } else {
98 val |= def & (0xff << shift);
101 return val;
104 #if !defined(CONFIG_USER_ONLY)
106 static inline uint32_t merge_syn_data_abort(uint32_t template_syn,
107 unsigned int target_el,
108 bool same_el, bool ea,
109 bool s1ptw, bool is_write,
110 int fsc)
112 uint32_t syn;
114 /* ISV is only set for data aborts routed to EL2 and
115 * never for stage-1 page table walks faulting on stage 2.
117 * Furthermore, ISV is only set for certain kinds of load/stores.
118 * If the template syndrome does not have ISV set, we should leave
119 * it cleared.
121 * See ARMv8 specs, D7-1974:
122 * ISS encoding for an exception from a Data Abort, the
123 * ISV field.
125 if (!(template_syn & ARM_EL_ISV) || target_el != 2 || s1ptw) {
126 syn = syn_data_abort_no_iss(same_el,
127 ea, 0, s1ptw, is_write, fsc);
128 } else {
129 /* Fields: IL, ISV, SAS, SSE, SRT, SF and AR come from the template
130 * syndrome created at translation time.
131 * Now we create the runtime syndrome with the remaining fields.
133 syn = syn_data_abort_with_iss(same_el,
134 0, 0, 0, 0, 0,
135 ea, 0, s1ptw, is_write, fsc,
136 false);
137 /* Merge the runtime syndrome with the template syndrome. */
138 syn |= template_syn;
140 return syn;
143 static void deliver_fault(ARMCPU *cpu, vaddr addr, MMUAccessType access_type,
144 int mmu_idx, ARMMMUFaultInfo *fi)
146 CPUARMState *env = &cpu->env;
147 int target_el;
148 bool same_el;
149 uint32_t syn, exc, fsr, fsc;
150 ARMMMUIdx arm_mmu_idx = core_to_arm_mmu_idx(env, mmu_idx);
152 target_el = exception_target_el(env);
153 if (fi->stage2) {
154 target_el = 2;
155 env->cp15.hpfar_el2 = extract64(fi->s2addr, 12, 47) << 4;
157 same_el = (arm_current_el(env) == target_el);
159 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
160 arm_s1_regime_using_lpae_format(env, arm_mmu_idx)) {
161 /* LPAE format fault status register : bottom 6 bits are
162 * status code in the same form as needed for syndrome
164 fsr = arm_fi_to_lfsc(fi);
165 fsc = extract32(fsr, 0, 6);
166 } else {
167 fsr = arm_fi_to_sfsc(fi);
168 /* Short format FSR : this fault will never actually be reported
169 * to an EL that uses a syndrome register. Use a (currently)
170 * reserved FSR code in case the constructed syndrome does leak
171 * into the guest somehow.
173 fsc = 0x3f;
176 if (access_type == MMU_INST_FETCH) {
177 syn = syn_insn_abort(same_el, fi->ea, fi->s1ptw, fsc);
178 exc = EXCP_PREFETCH_ABORT;
179 } else {
180 syn = merge_syn_data_abort(env->exception.syndrome, target_el,
181 same_el, fi->ea, fi->s1ptw,
182 access_type == MMU_DATA_STORE,
183 fsc);
184 if (access_type == MMU_DATA_STORE
185 && arm_feature(env, ARM_FEATURE_V6)) {
186 fsr |= (1 << 11);
188 exc = EXCP_DATA_ABORT;
191 env->exception.vaddress = addr;
192 env->exception.fsr = fsr;
193 raise_exception(env, exc, syn, target_el);
196 /* try to fill the TLB and return an exception if error. If retaddr is
197 * NULL, it means that the function was called in C code (i.e. not
198 * from generated code or from helper.c)
200 void tlb_fill(CPUState *cs, target_ulong addr, int size,
201 MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
203 bool ret;
204 ARMMMUFaultInfo fi = {};
206 ret = arm_tlb_fill(cs, addr, access_type, mmu_idx, &fi);
207 if (unlikely(ret)) {
208 ARMCPU *cpu = ARM_CPU(cs);
210 /* now we have a real cpu fault */
211 cpu_restore_state(cs, retaddr, true);
213 deliver_fault(cpu, addr, access_type, mmu_idx, &fi);
217 /* Raise a data fault alignment exception for the specified virtual address */
218 void arm_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
219 MMUAccessType access_type,
220 int mmu_idx, uintptr_t retaddr)
222 ARMCPU *cpu = ARM_CPU(cs);
223 ARMMMUFaultInfo fi = {};
225 /* now we have a real cpu fault */
226 cpu_restore_state(cs, retaddr, true);
228 fi.type = ARMFault_Alignment;
229 deliver_fault(cpu, vaddr, access_type, mmu_idx, &fi);
232 /* arm_cpu_do_transaction_failed: handle a memory system error response
233 * (eg "no device/memory present at address") by raising an external abort
234 * exception
236 void arm_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
237 vaddr addr, unsigned size,
238 MMUAccessType access_type,
239 int mmu_idx, MemTxAttrs attrs,
240 MemTxResult response, uintptr_t retaddr)
242 ARMCPU *cpu = ARM_CPU(cs);
243 ARMMMUFaultInfo fi = {};
245 /* now we have a real cpu fault */
246 cpu_restore_state(cs, retaddr, true);
248 fi.ea = arm_extabort_type(response);
249 fi.type = ARMFault_SyncExternal;
250 deliver_fault(cpu, addr, access_type, mmu_idx, &fi);
253 #endif /* !defined(CONFIG_USER_ONLY) */
255 void HELPER(v8m_stackcheck)(CPUARMState *env, uint32_t newvalue)
258 * Perform the v8M stack limit check for SP updates from translated code,
259 * raising an exception if the limit is breached.
261 if (newvalue < v7m_sp_limit(env)) {
262 CPUState *cs = CPU(arm_env_get_cpu(env));
265 * Stack limit exceptions are a rare case, so rather than syncing
266 * PC/condbits before the call, we use cpu_restore_state() to
267 * get them right before raising the exception.
269 cpu_restore_state(cs, GETPC(), true);
270 raise_exception(env, EXCP_STKOF, 0, 1);
274 uint32_t HELPER(add_setq)(CPUARMState *env, uint32_t a, uint32_t b)
276 uint32_t res = a + b;
277 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT))
278 env->QF = 1;
279 return res;
282 uint32_t HELPER(add_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
284 uint32_t res = a + b;
285 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT)) {
286 env->QF = 1;
287 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
289 return res;
292 uint32_t HELPER(sub_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
294 uint32_t res = a - b;
295 if (((res ^ a) & SIGNBIT) && ((a ^ b) & SIGNBIT)) {
296 env->QF = 1;
297 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
299 return res;
302 uint32_t HELPER(double_saturate)(CPUARMState *env, int32_t val)
304 uint32_t res;
305 if (val >= 0x40000000) {
306 res = ~SIGNBIT;
307 env->QF = 1;
308 } else if (val <= (int32_t)0xc0000000) {
309 res = SIGNBIT;
310 env->QF = 1;
311 } else {
312 res = val << 1;
314 return res;
317 uint32_t HELPER(add_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
319 uint32_t res = a + b;
320 if (res < a) {
321 env->QF = 1;
322 res = ~0;
324 return res;
327 uint32_t HELPER(sub_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
329 uint32_t res = a - b;
330 if (res > a) {
331 env->QF = 1;
332 res = 0;
334 return res;
337 /* Signed saturation. */
338 static inline uint32_t do_ssat(CPUARMState *env, int32_t val, int shift)
340 int32_t top;
341 uint32_t mask;
343 top = val >> shift;
344 mask = (1u << shift) - 1;
345 if (top > 0) {
346 env->QF = 1;
347 return mask;
348 } else if (top < -1) {
349 env->QF = 1;
350 return ~mask;
352 return val;
355 /* Unsigned saturation. */
356 static inline uint32_t do_usat(CPUARMState *env, int32_t val, int shift)
358 uint32_t max;
360 max = (1u << shift) - 1;
361 if (val < 0) {
362 env->QF = 1;
363 return 0;
364 } else if (val > max) {
365 env->QF = 1;
366 return max;
368 return val;
371 /* Signed saturate. */
372 uint32_t HELPER(ssat)(CPUARMState *env, uint32_t x, uint32_t shift)
374 return do_ssat(env, x, shift);
377 /* Dual halfword signed saturate. */
378 uint32_t HELPER(ssat16)(CPUARMState *env, uint32_t x, uint32_t shift)
380 uint32_t res;
382 res = (uint16_t)do_ssat(env, (int16_t)x, shift);
383 res |= do_ssat(env, ((int32_t)x) >> 16, shift) << 16;
384 return res;
387 /* Unsigned saturate. */
388 uint32_t HELPER(usat)(CPUARMState *env, uint32_t x, uint32_t shift)
390 return do_usat(env, x, shift);
393 /* Dual halfword unsigned saturate. */
394 uint32_t HELPER(usat16)(CPUARMState *env, uint32_t x, uint32_t shift)
396 uint32_t res;
398 res = (uint16_t)do_usat(env, (int16_t)x, shift);
399 res |= do_usat(env, ((int32_t)x) >> 16, shift) << 16;
400 return res;
403 void HELPER(setend)(CPUARMState *env)
405 env->uncached_cpsr ^= CPSR_E;
408 /* Function checks whether WFx (WFI/WFE) instructions are set up to be trapped.
409 * The function returns the target EL (1-3) if the instruction is to be trapped;
410 * otherwise it returns 0 indicating it is not trapped.
412 static inline int check_wfx_trap(CPUARMState *env, bool is_wfe)
414 int cur_el = arm_current_el(env);
415 uint64_t mask;
417 if (arm_feature(env, ARM_FEATURE_M)) {
418 /* M profile cores can never trap WFI/WFE. */
419 return 0;
422 /* If we are currently in EL0 then we need to check if SCTLR is set up for
423 * WFx instructions being trapped to EL1. These trap bits don't exist in v7.
425 if (cur_el < 1 && arm_feature(env, ARM_FEATURE_V8)) {
426 int target_el;
428 mask = is_wfe ? SCTLR_nTWE : SCTLR_nTWI;
429 if (arm_is_secure_below_el3(env) && !arm_el_is_aa64(env, 3)) {
430 /* Secure EL0 and Secure PL1 is at EL3 */
431 target_el = 3;
432 } else {
433 target_el = 1;
436 if (!(env->cp15.sctlr_el[target_el] & mask)) {
437 return target_el;
441 /* We are not trapping to EL1; trap to EL2 if HCR_EL2 requires it
442 * No need for ARM_FEATURE check as if HCR_EL2 doesn't exist the
443 * bits will be zero indicating no trap.
445 if (cur_el < 2) {
446 mask = is_wfe ? HCR_TWE : HCR_TWI;
447 if (arm_hcr_el2_eff(env) & mask) {
448 return 2;
452 /* We are not trapping to EL1 or EL2; trap to EL3 if SCR_EL3 requires it */
453 if (cur_el < 3) {
454 mask = (is_wfe) ? SCR_TWE : SCR_TWI;
455 if (env->cp15.scr_el3 & mask) {
456 return 3;
460 return 0;
463 void HELPER(wfi)(CPUARMState *env, uint32_t insn_len)
465 CPUState *cs = CPU(arm_env_get_cpu(env));
466 int target_el = check_wfx_trap(env, false);
468 if (cpu_has_work(cs)) {
469 /* Don't bother to go into our "low power state" if
470 * we would just wake up immediately.
472 return;
475 if (target_el) {
476 env->pc -= insn_len;
477 raise_exception(env, EXCP_UDEF, syn_wfx(1, 0xe, 0, insn_len == 2),
478 target_el);
481 cs->exception_index = EXCP_HLT;
482 cs->halted = 1;
483 cpu_loop_exit(cs);
486 void HELPER(wfe)(CPUARMState *env)
488 /* This is a hint instruction that is semantically different
489 * from YIELD even though we currently implement it identically.
490 * Don't actually halt the CPU, just yield back to top
491 * level loop. This is not going into a "low power state"
492 * (ie halting until some event occurs), so we never take
493 * a configurable trap to a different exception level.
495 HELPER(yield)(env);
498 void HELPER(yield)(CPUARMState *env)
500 ARMCPU *cpu = arm_env_get_cpu(env);
501 CPUState *cs = CPU(cpu);
503 /* This is a non-trappable hint instruction that generally indicates
504 * that the guest is currently busy-looping. Yield control back to the
505 * top level loop so that a more deserving VCPU has a chance to run.
507 cs->exception_index = EXCP_YIELD;
508 cpu_loop_exit(cs);
511 /* Raise an internal-to-QEMU exception. This is limited to only
512 * those EXCP values which are special cases for QEMU to interrupt
513 * execution and not to be used for exceptions which are passed to
514 * the guest (those must all have syndrome information and thus should
515 * use exception_with_syndrome).
517 void HELPER(exception_internal)(CPUARMState *env, uint32_t excp)
519 CPUState *cs = CPU(arm_env_get_cpu(env));
521 assert(excp_is_internal(excp));
522 cs->exception_index = excp;
523 cpu_loop_exit(cs);
526 /* Raise an exception with the specified syndrome register value */
527 void HELPER(exception_with_syndrome)(CPUARMState *env, uint32_t excp,
528 uint32_t syndrome, uint32_t target_el)
530 raise_exception(env, excp, syndrome, target_el);
533 /* Raise an EXCP_BKPT with the specified syndrome register value,
534 * targeting the correct exception level for debug exceptions.
536 void HELPER(exception_bkpt_insn)(CPUARMState *env, uint32_t syndrome)
538 /* FSR will only be used if the debug target EL is AArch32. */
539 env->exception.fsr = arm_debug_exception_fsr(env);
540 /* FAR is UNKNOWN: clear vaddress to avoid potentially exposing
541 * values to the guest that it shouldn't be able to see at its
542 * exception/security level.
544 env->exception.vaddress = 0;
545 raise_exception(env, EXCP_BKPT, syndrome, arm_debug_target_el(env));
548 uint32_t HELPER(cpsr_read)(CPUARMState *env)
550 return cpsr_read(env) & ~(CPSR_EXEC | CPSR_RESERVED);
553 void HELPER(cpsr_write)(CPUARMState *env, uint32_t val, uint32_t mask)
555 cpsr_write(env, val, mask, CPSRWriteByInstr);
558 /* Write the CPSR for a 32-bit exception return */
559 void HELPER(cpsr_write_eret)(CPUARMState *env, uint32_t val)
561 qemu_mutex_lock_iothread();
562 arm_call_pre_el_change_hook(arm_env_get_cpu(env));
563 qemu_mutex_unlock_iothread();
565 cpsr_write(env, val, CPSR_ERET_MASK, CPSRWriteExceptionReturn);
567 /* Generated code has already stored the new PC value, but
568 * without masking out its low bits, because which bits need
569 * masking depends on whether we're returning to Thumb or ARM
570 * state. Do the masking now.
572 env->regs[15] &= (env->thumb ? ~1 : ~3);
574 qemu_mutex_lock_iothread();
575 arm_call_el_change_hook(arm_env_get_cpu(env));
576 qemu_mutex_unlock_iothread();
579 /* Access to user mode registers from privileged modes. */
580 uint32_t HELPER(get_user_reg)(CPUARMState *env, uint32_t regno)
582 uint32_t val;
584 if (regno == 13) {
585 val = env->banked_r13[BANK_USRSYS];
586 } else if (regno == 14) {
587 val = env->banked_r14[BANK_USRSYS];
588 } else if (regno >= 8
589 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
590 val = env->usr_regs[regno - 8];
591 } else {
592 val = env->regs[regno];
594 return val;
597 void HELPER(set_user_reg)(CPUARMState *env, uint32_t regno, uint32_t val)
599 if (regno == 13) {
600 env->banked_r13[BANK_USRSYS] = val;
601 } else if (regno == 14) {
602 env->banked_r14[BANK_USRSYS] = val;
603 } else if (regno >= 8
604 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
605 env->usr_regs[regno - 8] = val;
606 } else {
607 env->regs[regno] = val;
611 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
613 if ((env->uncached_cpsr & CPSR_M) == mode) {
614 env->regs[13] = val;
615 } else {
616 env->banked_r13[bank_number(mode)] = val;
620 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
622 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_SYS) {
623 /* SRS instruction is UNPREDICTABLE from System mode; we UNDEF.
624 * Other UNPREDICTABLE and UNDEF cases were caught at translate time.
626 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
627 exception_target_el(env));
630 if ((env->uncached_cpsr & CPSR_M) == mode) {
631 return env->regs[13];
632 } else {
633 return env->banked_r13[bank_number(mode)];
637 static void msr_mrs_banked_exc_checks(CPUARMState *env, uint32_t tgtmode,
638 uint32_t regno)
640 /* Raise an exception if the requested access is one of the UNPREDICTABLE
641 * cases; otherwise return. This broadly corresponds to the pseudocode
642 * BankedRegisterAccessValid() and SPSRAccessValid(),
643 * except that we have already handled some cases at translate time.
645 int curmode = env->uncached_cpsr & CPSR_M;
647 if (regno == 17) {
648 /* ELR_Hyp: a special case because access from tgtmode is OK */
649 if (curmode != ARM_CPU_MODE_HYP && curmode != ARM_CPU_MODE_MON) {
650 goto undef;
652 return;
655 if (curmode == tgtmode) {
656 goto undef;
659 if (tgtmode == ARM_CPU_MODE_USR) {
660 switch (regno) {
661 case 8 ... 12:
662 if (curmode != ARM_CPU_MODE_FIQ) {
663 goto undef;
665 break;
666 case 13:
667 if (curmode == ARM_CPU_MODE_SYS) {
668 goto undef;
670 break;
671 case 14:
672 if (curmode == ARM_CPU_MODE_HYP || curmode == ARM_CPU_MODE_SYS) {
673 goto undef;
675 break;
676 default:
677 break;
681 if (tgtmode == ARM_CPU_MODE_HYP) {
682 /* SPSR_Hyp, r13_hyp: accessible from Monitor mode only */
683 if (curmode != ARM_CPU_MODE_MON) {
684 goto undef;
688 return;
690 undef:
691 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
692 exception_target_el(env));
695 void HELPER(msr_banked)(CPUARMState *env, uint32_t value, uint32_t tgtmode,
696 uint32_t regno)
698 msr_mrs_banked_exc_checks(env, tgtmode, regno);
700 switch (regno) {
701 case 16: /* SPSRs */
702 env->banked_spsr[bank_number(tgtmode)] = value;
703 break;
704 case 17: /* ELR_Hyp */
705 env->elr_el[2] = value;
706 break;
707 case 13:
708 env->banked_r13[bank_number(tgtmode)] = value;
709 break;
710 case 14:
711 env->banked_r14[r14_bank_number(tgtmode)] = value;
712 break;
713 case 8 ... 12:
714 switch (tgtmode) {
715 case ARM_CPU_MODE_USR:
716 env->usr_regs[regno - 8] = value;
717 break;
718 case ARM_CPU_MODE_FIQ:
719 env->fiq_regs[regno - 8] = value;
720 break;
721 default:
722 g_assert_not_reached();
724 break;
725 default:
726 g_assert_not_reached();
730 uint32_t HELPER(mrs_banked)(CPUARMState *env, uint32_t tgtmode, uint32_t regno)
732 msr_mrs_banked_exc_checks(env, tgtmode, regno);
734 switch (regno) {
735 case 16: /* SPSRs */
736 return env->banked_spsr[bank_number(tgtmode)];
737 case 17: /* ELR_Hyp */
738 return env->elr_el[2];
739 case 13:
740 return env->banked_r13[bank_number(tgtmode)];
741 case 14:
742 return env->banked_r14[r14_bank_number(tgtmode)];
743 case 8 ... 12:
744 switch (tgtmode) {
745 case ARM_CPU_MODE_USR:
746 return env->usr_regs[regno - 8];
747 case ARM_CPU_MODE_FIQ:
748 return env->fiq_regs[regno - 8];
749 default:
750 g_assert_not_reached();
752 default:
753 g_assert_not_reached();
757 void HELPER(access_check_cp_reg)(CPUARMState *env, void *rip, uint32_t syndrome,
758 uint32_t isread)
760 const ARMCPRegInfo *ri = rip;
761 int target_el;
763 if (arm_feature(env, ARM_FEATURE_XSCALE) && ri->cp < 14
764 && extract32(env->cp15.c15_cpar, ri->cp, 1) == 0) {
765 raise_exception(env, EXCP_UDEF, syndrome, exception_target_el(env));
768 if (!ri->accessfn) {
769 return;
772 switch (ri->accessfn(env, ri, isread)) {
773 case CP_ACCESS_OK:
774 return;
775 case CP_ACCESS_TRAP:
776 target_el = exception_target_el(env);
777 break;
778 case CP_ACCESS_TRAP_EL2:
779 /* Requesting a trap to EL2 when we're in EL3 or S-EL0/1 is
780 * a bug in the access function.
782 assert(!arm_is_secure(env) && arm_current_el(env) != 3);
783 target_el = 2;
784 break;
785 case CP_ACCESS_TRAP_EL3:
786 target_el = 3;
787 break;
788 case CP_ACCESS_TRAP_UNCATEGORIZED:
789 target_el = exception_target_el(env);
790 syndrome = syn_uncategorized();
791 break;
792 case CP_ACCESS_TRAP_UNCATEGORIZED_EL2:
793 target_el = 2;
794 syndrome = syn_uncategorized();
795 break;
796 case CP_ACCESS_TRAP_UNCATEGORIZED_EL3:
797 target_el = 3;
798 syndrome = syn_uncategorized();
799 break;
800 case CP_ACCESS_TRAP_FP_EL2:
801 target_el = 2;
802 /* Since we are an implementation that takes exceptions on a trapped
803 * conditional insn only if the insn has passed its condition code
804 * check, we take the IMPDEF choice to always report CV=1 COND=0xe
805 * (which is also the required value for AArch64 traps).
807 syndrome = syn_fp_access_trap(1, 0xe, false);
808 break;
809 case CP_ACCESS_TRAP_FP_EL3:
810 target_el = 3;
811 syndrome = syn_fp_access_trap(1, 0xe, false);
812 break;
813 default:
814 g_assert_not_reached();
817 raise_exception(env, EXCP_UDEF, syndrome, target_el);
820 void HELPER(set_cp_reg)(CPUARMState *env, void *rip, uint32_t value)
822 const ARMCPRegInfo *ri = rip;
824 if (ri->type & ARM_CP_IO) {
825 qemu_mutex_lock_iothread();
826 ri->writefn(env, ri, value);
827 qemu_mutex_unlock_iothread();
828 } else {
829 ri->writefn(env, ri, value);
833 uint32_t HELPER(get_cp_reg)(CPUARMState *env, void *rip)
835 const ARMCPRegInfo *ri = rip;
836 uint32_t res;
838 if (ri->type & ARM_CP_IO) {
839 qemu_mutex_lock_iothread();
840 res = ri->readfn(env, ri);
841 qemu_mutex_unlock_iothread();
842 } else {
843 res = ri->readfn(env, ri);
846 return res;
849 void HELPER(set_cp_reg64)(CPUARMState *env, void *rip, uint64_t value)
851 const ARMCPRegInfo *ri = rip;
853 if (ri->type & ARM_CP_IO) {
854 qemu_mutex_lock_iothread();
855 ri->writefn(env, ri, value);
856 qemu_mutex_unlock_iothread();
857 } else {
858 ri->writefn(env, ri, value);
862 uint64_t HELPER(get_cp_reg64)(CPUARMState *env, void *rip)
864 const ARMCPRegInfo *ri = rip;
865 uint64_t res;
867 if (ri->type & ARM_CP_IO) {
868 qemu_mutex_lock_iothread();
869 res = ri->readfn(env, ri);
870 qemu_mutex_unlock_iothread();
871 } else {
872 res = ri->readfn(env, ri);
875 return res;
878 void HELPER(msr_i_pstate)(CPUARMState *env, uint32_t op, uint32_t imm)
880 /* MSR_i to update PSTATE. This is OK from EL0 only if UMA is set.
881 * Note that SPSel is never OK from EL0; we rely on handle_msr_i()
882 * to catch that case at translate time.
884 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
885 uint32_t syndrome = syn_aa64_sysregtrap(0, extract32(op, 0, 3),
886 extract32(op, 3, 3), 4,
887 imm, 0x1f, 0);
888 raise_exception(env, EXCP_UDEF, syndrome, exception_target_el(env));
891 switch (op) {
892 case 0x05: /* SPSel */
893 update_spsel(env, imm);
894 break;
895 case 0x1e: /* DAIFSet */
896 env->daif |= (imm << 6) & PSTATE_DAIF;
897 break;
898 case 0x1f: /* DAIFClear */
899 env->daif &= ~((imm << 6) & PSTATE_DAIF);
900 break;
901 default:
902 g_assert_not_reached();
906 void HELPER(clear_pstate_ss)(CPUARMState *env)
908 env->pstate &= ~PSTATE_SS;
911 void HELPER(pre_hvc)(CPUARMState *env)
913 ARMCPU *cpu = arm_env_get_cpu(env);
914 int cur_el = arm_current_el(env);
915 /* FIXME: Use actual secure state. */
916 bool secure = false;
917 bool undef;
919 if (arm_is_psci_call(cpu, EXCP_HVC)) {
920 /* If PSCI is enabled and this looks like a valid PSCI call then
921 * that overrides the architecturally mandated HVC behaviour.
923 return;
926 if (!arm_feature(env, ARM_FEATURE_EL2)) {
927 /* If EL2 doesn't exist, HVC always UNDEFs */
928 undef = true;
929 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
930 /* EL3.HCE has priority over EL2.HCD. */
931 undef = !(env->cp15.scr_el3 & SCR_HCE);
932 } else {
933 undef = env->cp15.hcr_el2 & HCR_HCD;
936 /* In ARMv7 and ARMv8/AArch32, HVC is undef in secure state.
937 * For ARMv8/AArch64, HVC is allowed in EL3.
938 * Note that we've already trapped HVC from EL0 at translation
939 * time.
941 if (secure && (!is_a64(env) || cur_el == 1)) {
942 undef = true;
945 if (undef) {
946 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
947 exception_target_el(env));
951 void HELPER(pre_smc)(CPUARMState *env, uint32_t syndrome)
953 ARMCPU *cpu = arm_env_get_cpu(env);
954 int cur_el = arm_current_el(env);
955 bool secure = arm_is_secure(env);
956 bool smd_flag = env->cp15.scr_el3 & SCR_SMD;
959 * SMC behaviour is summarized in the following table.
960 * This helper handles the "Trap to EL2" and "Undef insn" cases.
961 * The "Trap to EL3" and "PSCI call" cases are handled in the exception
962 * helper.
964 * -> ARM_FEATURE_EL3 and !SMD
965 * HCR_TSC && NS EL1 !HCR_TSC || !NS EL1
967 * Conduit SMC, valid call Trap to EL2 PSCI Call
968 * Conduit SMC, inval call Trap to EL2 Trap to EL3
969 * Conduit not SMC Trap to EL2 Trap to EL3
972 * -> ARM_FEATURE_EL3 and SMD
973 * HCR_TSC && NS EL1 !HCR_TSC || !NS EL1
975 * Conduit SMC, valid call Trap to EL2 PSCI Call
976 * Conduit SMC, inval call Trap to EL2 Undef insn
977 * Conduit not SMC Trap to EL2 Undef insn
980 * -> !ARM_FEATURE_EL3
981 * HCR_TSC && NS EL1 !HCR_TSC || !NS EL1
983 * Conduit SMC, valid call Trap to EL2 PSCI Call
984 * Conduit SMC, inval call Trap to EL2 Undef insn
985 * Conduit not SMC Undef insn Undef insn
988 /* On ARMv8 with EL3 AArch64, SMD applies to both S and NS state.
989 * On ARMv8 with EL3 AArch32, or ARMv7 with the Virtualization
990 * extensions, SMD only applies to NS state.
991 * On ARMv7 without the Virtualization extensions, the SMD bit
992 * doesn't exist, but we forbid the guest to set it to 1 in scr_write(),
993 * so we need not special case this here.
995 bool smd = arm_feature(env, ARM_FEATURE_AARCH64) ? smd_flag
996 : smd_flag && !secure;
998 if (!arm_feature(env, ARM_FEATURE_EL3) &&
999 cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
1000 /* If we have no EL3 then SMC always UNDEFs and can't be
1001 * trapped to EL2. PSCI-via-SMC is a sort of ersatz EL3
1002 * firmware within QEMU, and we want an EL2 guest to be able
1003 * to forbid its EL1 from making PSCI calls into QEMU's
1004 * "firmware" via HCR.TSC, so for these purposes treat
1005 * PSCI-via-SMC as implying an EL3.
1006 * This handles the very last line of the previous table.
1008 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
1009 exception_target_el(env));
1012 if (cur_el == 1 && (arm_hcr_el2_eff(env) & HCR_TSC)) {
1013 /* In NS EL1, HCR controlled routing to EL2 has priority over SMD.
1014 * We also want an EL2 guest to be able to forbid its EL1 from
1015 * making PSCI calls into QEMU's "firmware" via HCR.TSC.
1016 * This handles all the "Trap to EL2" cases of the previous table.
1018 raise_exception(env, EXCP_HYP_TRAP, syndrome, 2);
1021 /* Catch the two remaining "Undef insn" cases of the previous table:
1022 * - PSCI conduit is SMC but we don't have a valid PCSI call,
1023 * - We don't have EL3 or SMD is set.
1025 if (!arm_is_psci_call(cpu, EXCP_SMC) &&
1026 (smd || !arm_feature(env, ARM_FEATURE_EL3))) {
1027 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
1028 exception_target_el(env));
1032 /* Return true if the linked breakpoint entry lbn passes its checks */
1033 static bool linked_bp_matches(ARMCPU *cpu, int lbn)
1035 CPUARMState *env = &cpu->env;
1036 uint64_t bcr = env->cp15.dbgbcr[lbn];
1037 int brps = extract32(cpu->dbgdidr, 24, 4);
1038 int ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
1039 int bt;
1040 uint32_t contextidr;
1042 /* Links to unimplemented or non-context aware breakpoints are
1043 * CONSTRAINED UNPREDICTABLE: either behave as if disabled, or
1044 * as if linked to an UNKNOWN context-aware breakpoint (in which
1045 * case DBGWCR<n>_EL1.LBN must indicate that breakpoint).
1046 * We choose the former.
1048 if (lbn > brps || lbn < (brps - ctx_cmps)) {
1049 return false;
1052 bcr = env->cp15.dbgbcr[lbn];
1054 if (extract64(bcr, 0, 1) == 0) {
1055 /* Linked breakpoint disabled : generate no events */
1056 return false;
1059 bt = extract64(bcr, 20, 4);
1061 /* We match the whole register even if this is AArch32 using the
1062 * short descriptor format (in which case it holds both PROCID and ASID),
1063 * since we don't implement the optional v7 context ID masking.
1065 contextidr = extract64(env->cp15.contextidr_el[1], 0, 32);
1067 switch (bt) {
1068 case 3: /* linked context ID match */
1069 if (arm_current_el(env) > 1) {
1070 /* Context matches never fire in EL2 or (AArch64) EL3 */
1071 return false;
1073 return (contextidr == extract64(env->cp15.dbgbvr[lbn], 0, 32));
1074 case 5: /* linked address mismatch (reserved in AArch64) */
1075 case 9: /* linked VMID match (reserved if no EL2) */
1076 case 11: /* linked context ID and VMID match (reserved if no EL2) */
1077 default:
1078 /* Links to Unlinked context breakpoints must generate no
1079 * events; we choose to do the same for reserved values too.
1081 return false;
1084 return false;
1087 static bool bp_wp_matches(ARMCPU *cpu, int n, bool is_wp)
1089 CPUARMState *env = &cpu->env;
1090 uint64_t cr;
1091 int pac, hmc, ssc, wt, lbn;
1092 /* Note that for watchpoints the check is against the CPU security
1093 * state, not the S/NS attribute on the offending data access.
1095 bool is_secure = arm_is_secure(env);
1096 int access_el = arm_current_el(env);
1098 if (is_wp) {
1099 CPUWatchpoint *wp = env->cpu_watchpoint[n];
1101 if (!wp || !(wp->flags & BP_WATCHPOINT_HIT)) {
1102 return false;
1104 cr = env->cp15.dbgwcr[n];
1105 if (wp->hitattrs.user) {
1106 /* The LDRT/STRT/LDT/STT "unprivileged access" instructions should
1107 * match watchpoints as if they were accesses done at EL0, even if
1108 * the CPU is at EL1 or higher.
1110 access_el = 0;
1112 } else {
1113 uint64_t pc = is_a64(env) ? env->pc : env->regs[15];
1115 if (!env->cpu_breakpoint[n] || env->cpu_breakpoint[n]->pc != pc) {
1116 return false;
1118 cr = env->cp15.dbgbcr[n];
1120 /* The WATCHPOINT_HIT flag guarantees us that the watchpoint is
1121 * enabled and that the address and access type match; for breakpoints
1122 * we know the address matched; check the remaining fields, including
1123 * linked breakpoints. We rely on WCR and BCR having the same layout
1124 * for the LBN, SSC, HMC, PAC/PMC and is-linked fields.
1125 * Note that some combinations of {PAC, HMC, SSC} are reserved and
1126 * must act either like some valid combination or as if the watchpoint
1127 * were disabled. We choose the former, and use this together with
1128 * the fact that EL3 must always be Secure and EL2 must always be
1129 * Non-Secure to simplify the code slightly compared to the full
1130 * table in the ARM ARM.
1132 pac = extract64(cr, 1, 2);
1133 hmc = extract64(cr, 13, 1);
1134 ssc = extract64(cr, 14, 2);
1136 switch (ssc) {
1137 case 0:
1138 break;
1139 case 1:
1140 case 3:
1141 if (is_secure) {
1142 return false;
1144 break;
1145 case 2:
1146 if (!is_secure) {
1147 return false;
1149 break;
1152 switch (access_el) {
1153 case 3:
1154 case 2:
1155 if (!hmc) {
1156 return false;
1158 break;
1159 case 1:
1160 if (extract32(pac, 0, 1) == 0) {
1161 return false;
1163 break;
1164 case 0:
1165 if (extract32(pac, 1, 1) == 0) {
1166 return false;
1168 break;
1169 default:
1170 g_assert_not_reached();
1173 wt = extract64(cr, 20, 1);
1174 lbn = extract64(cr, 16, 4);
1176 if (wt && !linked_bp_matches(cpu, lbn)) {
1177 return false;
1180 return true;
1183 static bool check_watchpoints(ARMCPU *cpu)
1185 CPUARMState *env = &cpu->env;
1186 int n;
1188 /* If watchpoints are disabled globally or we can't take debug
1189 * exceptions here then watchpoint firings are ignored.
1191 if (extract32(env->cp15.mdscr_el1, 15, 1) == 0
1192 || !arm_generate_debug_exceptions(env)) {
1193 return false;
1196 for (n = 0; n < ARRAY_SIZE(env->cpu_watchpoint); n++) {
1197 if (bp_wp_matches(cpu, n, true)) {
1198 return true;
1201 return false;
1204 static bool check_breakpoints(ARMCPU *cpu)
1206 CPUARMState *env = &cpu->env;
1207 int n;
1209 /* If breakpoints are disabled globally or we can't take debug
1210 * exceptions here then breakpoint firings are ignored.
1212 if (extract32(env->cp15.mdscr_el1, 15, 1) == 0
1213 || !arm_generate_debug_exceptions(env)) {
1214 return false;
1217 for (n = 0; n < ARRAY_SIZE(env->cpu_breakpoint); n++) {
1218 if (bp_wp_matches(cpu, n, false)) {
1219 return true;
1222 return false;
1225 void HELPER(check_breakpoints)(CPUARMState *env)
1227 ARMCPU *cpu = arm_env_get_cpu(env);
1229 if (check_breakpoints(cpu)) {
1230 HELPER(exception_internal(env, EXCP_DEBUG));
1234 bool arm_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
1236 /* Called by core code when a CPU watchpoint fires; need to check if this
1237 * is also an architectural watchpoint match.
1239 ARMCPU *cpu = ARM_CPU(cs);
1241 return check_watchpoints(cpu);
1244 vaddr arm_adjust_watchpoint_address(CPUState *cs, vaddr addr, int len)
1246 ARMCPU *cpu = ARM_CPU(cs);
1247 CPUARMState *env = &cpu->env;
1249 /* In BE32 system mode, target memory is stored byteswapped (on a
1250 * little-endian host system), and by the time we reach here (via an
1251 * opcode helper) the addresses of subword accesses have been adjusted
1252 * to account for that, which means that watchpoints will not match.
1253 * Undo the adjustment here.
1255 if (arm_sctlr_b(env)) {
1256 if (len == 1) {
1257 addr ^= 3;
1258 } else if (len == 2) {
1259 addr ^= 2;
1263 return addr;
1266 void arm_debug_excp_handler(CPUState *cs)
1268 /* Called by core code when a watchpoint or breakpoint fires;
1269 * need to check which one and raise the appropriate exception.
1271 ARMCPU *cpu = ARM_CPU(cs);
1272 CPUARMState *env = &cpu->env;
1273 CPUWatchpoint *wp_hit = cs->watchpoint_hit;
1275 if (wp_hit) {
1276 if (wp_hit->flags & BP_CPU) {
1277 bool wnr = (wp_hit->flags & BP_WATCHPOINT_HIT_WRITE) != 0;
1278 bool same_el = arm_debug_target_el(env) == arm_current_el(env);
1280 cs->watchpoint_hit = NULL;
1282 env->exception.fsr = arm_debug_exception_fsr(env);
1283 env->exception.vaddress = wp_hit->hitaddr;
1284 raise_exception(env, EXCP_DATA_ABORT,
1285 syn_watchpoint(same_el, 0, wnr),
1286 arm_debug_target_el(env));
1288 } else {
1289 uint64_t pc = is_a64(env) ? env->pc : env->regs[15];
1290 bool same_el = (arm_debug_target_el(env) == arm_current_el(env));
1292 /* (1) GDB breakpoints should be handled first.
1293 * (2) Do not raise a CPU exception if no CPU breakpoint has fired,
1294 * since singlestep is also done by generating a debug internal
1295 * exception.
1297 if (cpu_breakpoint_test(cs, pc, BP_GDB)
1298 || !cpu_breakpoint_test(cs, pc, BP_CPU)) {
1299 return;
1302 env->exception.fsr = arm_debug_exception_fsr(env);
1303 /* FAR is UNKNOWN: clear vaddress to avoid potentially exposing
1304 * values to the guest that it shouldn't be able to see at its
1305 * exception/security level.
1307 env->exception.vaddress = 0;
1308 raise_exception(env, EXCP_PREFETCH_ABORT,
1309 syn_breakpoint(same_el),
1310 arm_debug_target_el(env));
1314 /* ??? Flag setting arithmetic is awkward because we need to do comparisons.
1315 The only way to do that in TCG is a conditional branch, which clobbers
1316 all our temporaries. For now implement these as helper functions. */
1318 /* Similarly for variable shift instructions. */
1320 uint32_t HELPER(shl_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1322 int shift = i & 0xff;
1323 if (shift >= 32) {
1324 if (shift == 32)
1325 env->CF = x & 1;
1326 else
1327 env->CF = 0;
1328 return 0;
1329 } else if (shift != 0) {
1330 env->CF = (x >> (32 - shift)) & 1;
1331 return x << shift;
1333 return x;
1336 uint32_t HELPER(shr_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1338 int shift = i & 0xff;
1339 if (shift >= 32) {
1340 if (shift == 32)
1341 env->CF = (x >> 31) & 1;
1342 else
1343 env->CF = 0;
1344 return 0;
1345 } else if (shift != 0) {
1346 env->CF = (x >> (shift - 1)) & 1;
1347 return x >> shift;
1349 return x;
1352 uint32_t HELPER(sar_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1354 int shift = i & 0xff;
1355 if (shift >= 32) {
1356 env->CF = (x >> 31) & 1;
1357 return (int32_t)x >> 31;
1358 } else if (shift != 0) {
1359 env->CF = (x >> (shift - 1)) & 1;
1360 return (int32_t)x >> shift;
1362 return x;
1365 uint32_t HELPER(ror_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1367 int shift1, shift;
1368 shift1 = i & 0xff;
1369 shift = shift1 & 0x1f;
1370 if (shift == 0) {
1371 if (shift1 != 0)
1372 env->CF = (x >> 31) & 1;
1373 return x;
1374 } else {
1375 env->CF = (x >> (shift - 1)) & 1;
1376 return ((uint32_t)x >> shift) | (x << (32 - shift));