Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / target / arm / op_helper.c
blobdbf1b2a044175ae945c31c970011cd2c2a1e154d
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 void QEMU_NORETURN
32 raise_exception(CPUARMState *env, uint32_t excp,
33 uint32_t syndrome, uint32_t target_el)
35 CPUState *cs = CPU(arm_env_get_cpu(env));
37 assert(!excp_is_internal(excp));
38 cs->exception_index = excp;
39 env->exception.syndrome = syndrome;
40 env->exception.target_el = target_el;
41 cpu_loop_exit(cs);
44 static int exception_target_el(CPUARMState *env)
46 int target_el = MAX(1, arm_current_el(env));
48 /* No such thing as secure EL1 if EL3 is aarch32, so update the target EL
49 * to EL3 in this case.
51 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3) && target_el == 1) {
52 target_el = 3;
55 return target_el;
58 uint32_t HELPER(neon_tbl)(uint32_t ireg, uint32_t def, void *vn,
59 uint32_t maxindex)
61 uint32_t val, shift;
62 uint64_t *table = vn;
64 val = 0;
65 for (shift = 0; shift < 32; shift += 8) {
66 uint32_t index = (ireg >> shift) & 0xff;
67 if (index < maxindex) {
68 uint32_t tmp = (table[index >> 3] >> ((index & 7) << 3)) & 0xff;
69 val |= tmp << shift;
70 } else {
71 val |= def & (0xff << shift);
74 return val;
77 #if !defined(CONFIG_USER_ONLY)
79 static inline uint32_t merge_syn_data_abort(uint32_t template_syn,
80 unsigned int target_el,
81 bool same_el, bool ea,
82 bool s1ptw, bool is_write,
83 int fsc)
85 uint32_t syn;
87 /* ISV is only set for data aborts routed to EL2 and
88 * never for stage-1 page table walks faulting on stage 2.
90 * Furthermore, ISV is only set for certain kinds of load/stores.
91 * If the template syndrome does not have ISV set, we should leave
92 * it cleared.
94 * See ARMv8 specs, D7-1974:
95 * ISS encoding for an exception from a Data Abort, the
96 * ISV field.
98 if (!(template_syn & ARM_EL_ISV) || target_el != 2 || s1ptw) {
99 syn = syn_data_abort_no_iss(same_el,
100 ea, 0, s1ptw, is_write, fsc);
101 } else {
102 /* Fields: IL, ISV, SAS, SSE, SRT, SF and AR come from the template
103 * syndrome created at translation time.
104 * Now we create the runtime syndrome with the remaining fields.
106 syn = syn_data_abort_with_iss(same_el,
107 0, 0, 0, 0, 0,
108 ea, 0, s1ptw, is_write, fsc,
109 false);
110 /* Merge the runtime syndrome with the template syndrome. */
111 syn |= template_syn;
113 return syn;
116 static void deliver_fault(ARMCPU *cpu, vaddr addr, MMUAccessType access_type,
117 int mmu_idx, ARMMMUFaultInfo *fi)
119 CPUARMState *env = &cpu->env;
120 int target_el;
121 bool same_el;
122 uint32_t syn, exc, fsr, fsc;
123 ARMMMUIdx arm_mmu_idx = core_to_arm_mmu_idx(env, mmu_idx);
125 target_el = exception_target_el(env);
126 if (fi->stage2) {
127 target_el = 2;
128 env->cp15.hpfar_el2 = extract64(fi->s2addr, 12, 47) << 4;
130 same_el = (arm_current_el(env) == target_el);
132 if (target_el == 2 || arm_el_is_aa64(env, target_el) ||
133 arm_s1_regime_using_lpae_format(env, arm_mmu_idx)) {
134 /* LPAE format fault status register : bottom 6 bits are
135 * status code in the same form as needed for syndrome
137 fsr = arm_fi_to_lfsc(fi);
138 fsc = extract32(fsr, 0, 6);
139 } else {
140 fsr = arm_fi_to_sfsc(fi);
141 /* Short format FSR : this fault will never actually be reported
142 * to an EL that uses a syndrome register. Use a (currently)
143 * reserved FSR code in case the constructed syndrome does leak
144 * into the guest somehow.
146 fsc = 0x3f;
149 if (access_type == MMU_INST_FETCH) {
150 syn = syn_insn_abort(same_el, fi->ea, fi->s1ptw, fsc);
151 exc = EXCP_PREFETCH_ABORT;
152 } else {
153 syn = merge_syn_data_abort(env->exception.syndrome, target_el,
154 same_el, fi->ea, fi->s1ptw,
155 access_type == MMU_DATA_STORE,
156 fsc);
157 if (access_type == MMU_DATA_STORE
158 && arm_feature(env, ARM_FEATURE_V6)) {
159 fsr |= (1 << 11);
161 exc = EXCP_DATA_ABORT;
164 env->exception.vaddress = addr;
165 env->exception.fsr = fsr;
166 raise_exception(env, exc, syn, target_el);
169 /* try to fill the TLB and return an exception if error. If retaddr is
170 * NULL, it means that the function was called in C code (i.e. not
171 * from generated code or from helper.c)
173 void tlb_fill(CPUState *cs, target_ulong addr, int size,
174 MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
176 bool ret;
177 ARMMMUFaultInfo fi = {};
179 ret = arm_tlb_fill(cs, addr, access_type, mmu_idx, &fi);
180 if (unlikely(ret)) {
181 ARMCPU *cpu = ARM_CPU(cs);
183 /* now we have a real cpu fault */
184 cpu_restore_state(cs, retaddr);
186 deliver_fault(cpu, addr, access_type, mmu_idx, &fi);
190 /* Raise a data fault alignment exception for the specified virtual address */
191 void arm_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
192 MMUAccessType access_type,
193 int mmu_idx, uintptr_t retaddr)
195 ARMCPU *cpu = ARM_CPU(cs);
196 ARMMMUFaultInfo fi = {};
198 /* now we have a real cpu fault */
199 cpu_restore_state(cs, retaddr);
201 fi.type = ARMFault_Alignment;
202 deliver_fault(cpu, vaddr, access_type, mmu_idx, &fi);
205 /* arm_cpu_do_transaction_failed: handle a memory system error response
206 * (eg "no device/memory present at address") by raising an external abort
207 * exception
209 void arm_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
210 vaddr addr, unsigned size,
211 MMUAccessType access_type,
212 int mmu_idx, MemTxAttrs attrs,
213 MemTxResult response, uintptr_t retaddr)
215 ARMCPU *cpu = ARM_CPU(cs);
216 ARMMMUFaultInfo fi = {};
218 /* now we have a real cpu fault */
219 cpu_restore_state(cs, retaddr);
221 fi.ea = arm_extabort_type(response);
222 fi.type = ARMFault_SyncExternal;
223 deliver_fault(cpu, addr, access_type, mmu_idx, &fi);
226 #endif /* !defined(CONFIG_USER_ONLY) */
228 uint32_t HELPER(add_setq)(CPUARMState *env, uint32_t a, uint32_t b)
230 uint32_t res = a + b;
231 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT))
232 env->QF = 1;
233 return res;
236 uint32_t HELPER(add_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
238 uint32_t res = a + b;
239 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT)) {
240 env->QF = 1;
241 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
243 return res;
246 uint32_t HELPER(sub_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
248 uint32_t res = a - b;
249 if (((res ^ a) & SIGNBIT) && ((a ^ b) & SIGNBIT)) {
250 env->QF = 1;
251 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
253 return res;
256 uint32_t HELPER(double_saturate)(CPUARMState *env, int32_t val)
258 uint32_t res;
259 if (val >= 0x40000000) {
260 res = ~SIGNBIT;
261 env->QF = 1;
262 } else if (val <= (int32_t)0xc0000000) {
263 res = SIGNBIT;
264 env->QF = 1;
265 } else {
266 res = val << 1;
268 return res;
271 uint32_t HELPER(add_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
273 uint32_t res = a + b;
274 if (res < a) {
275 env->QF = 1;
276 res = ~0;
278 return res;
281 uint32_t HELPER(sub_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
283 uint32_t res = a - b;
284 if (res > a) {
285 env->QF = 1;
286 res = 0;
288 return res;
291 /* Signed saturation. */
292 static inline uint32_t do_ssat(CPUARMState *env, int32_t val, int shift)
294 int32_t top;
295 uint32_t mask;
297 top = val >> shift;
298 mask = (1u << shift) - 1;
299 if (top > 0) {
300 env->QF = 1;
301 return mask;
302 } else if (top < -1) {
303 env->QF = 1;
304 return ~mask;
306 return val;
309 /* Unsigned saturation. */
310 static inline uint32_t do_usat(CPUARMState *env, int32_t val, int shift)
312 uint32_t max;
314 max = (1u << shift) - 1;
315 if (val < 0) {
316 env->QF = 1;
317 return 0;
318 } else if (val > max) {
319 env->QF = 1;
320 return max;
322 return val;
325 /* Signed saturate. */
326 uint32_t HELPER(ssat)(CPUARMState *env, uint32_t x, uint32_t shift)
328 return do_ssat(env, x, shift);
331 /* Dual halfword signed saturate. */
332 uint32_t HELPER(ssat16)(CPUARMState *env, uint32_t x, uint32_t shift)
334 uint32_t res;
336 res = (uint16_t)do_ssat(env, (int16_t)x, shift);
337 res |= do_ssat(env, ((int32_t)x) >> 16, shift) << 16;
338 return res;
341 /* Unsigned saturate. */
342 uint32_t HELPER(usat)(CPUARMState *env, uint32_t x, uint32_t shift)
344 return do_usat(env, x, shift);
347 /* Dual halfword unsigned saturate. */
348 uint32_t HELPER(usat16)(CPUARMState *env, uint32_t x, uint32_t shift)
350 uint32_t res;
352 res = (uint16_t)do_usat(env, (int16_t)x, shift);
353 res |= do_usat(env, ((int32_t)x) >> 16, shift) << 16;
354 return res;
357 void HELPER(setend)(CPUARMState *env)
359 env->uncached_cpsr ^= CPSR_E;
362 /* Function checks whether WFx (WFI/WFE) instructions are set up to be trapped.
363 * The function returns the target EL (1-3) if the instruction is to be trapped;
364 * otherwise it returns 0 indicating it is not trapped.
366 static inline int check_wfx_trap(CPUARMState *env, bool is_wfe)
368 int cur_el = arm_current_el(env);
369 uint64_t mask;
371 if (arm_feature(env, ARM_FEATURE_M)) {
372 /* M profile cores can never trap WFI/WFE. */
373 return 0;
376 /* If we are currently in EL0 then we need to check if SCTLR is set up for
377 * WFx instructions being trapped to EL1. These trap bits don't exist in v7.
379 if (cur_el < 1 && arm_feature(env, ARM_FEATURE_V8)) {
380 int target_el;
382 mask = is_wfe ? SCTLR_nTWE : SCTLR_nTWI;
383 if (arm_is_secure_below_el3(env) && !arm_el_is_aa64(env, 3)) {
384 /* Secure EL0 and Secure PL1 is at EL3 */
385 target_el = 3;
386 } else {
387 target_el = 1;
390 if (!(env->cp15.sctlr_el[target_el] & mask)) {
391 return target_el;
395 /* We are not trapping to EL1; trap to EL2 if HCR_EL2 requires it
396 * No need for ARM_FEATURE check as if HCR_EL2 doesn't exist the
397 * bits will be zero indicating no trap.
399 if (cur_el < 2 && !arm_is_secure(env)) {
400 mask = (is_wfe) ? HCR_TWE : HCR_TWI;
401 if (env->cp15.hcr_el2 & mask) {
402 return 2;
406 /* We are not trapping to EL1 or EL2; trap to EL3 if SCR_EL3 requires it */
407 if (cur_el < 3) {
408 mask = (is_wfe) ? SCR_TWE : SCR_TWI;
409 if (env->cp15.scr_el3 & mask) {
410 return 3;
414 return 0;
417 void HELPER(wfi)(CPUARMState *env, uint32_t insn_len)
419 CPUState *cs = CPU(arm_env_get_cpu(env));
420 int target_el = check_wfx_trap(env, false);
422 if (cpu_has_work(cs)) {
423 /* Don't bother to go into our "low power state" if
424 * we would just wake up immediately.
426 return;
429 if (target_el) {
430 env->pc -= insn_len;
431 raise_exception(env, EXCP_UDEF, syn_wfx(1, 0xe, 0, insn_len == 2),
432 target_el);
435 cs->exception_index = EXCP_HLT;
436 cs->halted = 1;
437 cpu_loop_exit(cs);
440 void QEMU_NORETURN HELPER(wfe)(CPUARMState *env)
442 /* This is a hint instruction that is semantically different
443 * from YIELD even though we currently implement it identically.
444 * Don't actually halt the CPU, just yield back to top
445 * level loop. This is not going into a "low power state"
446 * (ie halting until some event occurs), so we never take
447 * a configurable trap to a different exception level.
449 HELPER(yield)(env);
452 void QEMU_NORETURN HELPER(yield)(CPUARMState *env)
454 ARMCPU *cpu = arm_env_get_cpu(env);
455 CPUState *cs = CPU(cpu);
457 /* This is a non-trappable hint instruction that generally indicates
458 * that the guest is currently busy-looping. Yield control back to the
459 * top level loop so that a more deserving VCPU has a chance to run.
461 cs->exception_index = EXCP_YIELD;
462 cpu_loop_exit(cs);
465 /* Raise an internal-to-QEMU exception. This is limited to only
466 * those EXCP values which are special cases for QEMU to interrupt
467 * execution and not to be used for exceptions which are passed to
468 * the guest (those must all have syndrome information and thus should
469 * use exception_with_syndrome).
471 void QEMU_NORETURN HELPER(exception_internal)(CPUARMState *env, uint32_t excp)
473 CPUState *cs = CPU(arm_env_get_cpu(env));
475 assert(excp_is_internal(excp));
476 cs->exception_index = excp;
477 cpu_loop_exit(cs);
480 /* Raise an exception with the specified syndrome register value */
481 void QEMU_NORETURN
482 HELPER(exception_with_syndrome)(CPUARMState *env, uint32_t excp,
483 uint32_t syndrome, uint32_t target_el)
485 raise_exception(env, excp, syndrome, target_el);
488 uint32_t HELPER(cpsr_read)(CPUARMState *env)
490 return cpsr_read(env) & ~(CPSR_EXEC | CPSR_RESERVED);
493 void HELPER(cpsr_write)(CPUARMState *env, uint32_t val, uint32_t mask)
495 cpsr_write(env, val, mask, CPSRWriteByInstr);
498 /* Write the CPSR for a 32-bit exception return */
499 void HELPER(cpsr_write_eret)(CPUARMState *env, uint32_t val)
501 cpsr_write(env, val, CPSR_ERET_MASK, CPSRWriteExceptionReturn);
503 /* Generated code has already stored the new PC value, but
504 * without masking out its low bits, because which bits need
505 * masking depends on whether we're returning to Thumb or ARM
506 * state. Do the masking now.
508 env->regs[15] &= (env->thumb ? ~1 : ~3);
510 qemu_mutex_lock_iothread();
511 arm_call_el_change_hook(arm_env_get_cpu(env));
512 qemu_mutex_unlock_iothread();
515 /* Access to user mode registers from privileged modes. */
516 uint32_t HELPER(get_user_reg)(CPUARMState *env, uint32_t regno)
518 uint32_t val;
520 if (regno == 13) {
521 val = env->banked_r13[BANK_USRSYS];
522 } else if (regno == 14) {
523 val = env->banked_r14[BANK_USRSYS];
524 } else if (regno >= 8
525 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
526 val = env->usr_regs[regno - 8];
527 } else {
528 val = env->regs[regno];
530 return val;
533 void HELPER(set_user_reg)(CPUARMState *env, uint32_t regno, uint32_t val)
535 if (regno == 13) {
536 env->banked_r13[BANK_USRSYS] = val;
537 } else if (regno == 14) {
538 env->banked_r14[BANK_USRSYS] = val;
539 } else if (regno >= 8
540 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
541 env->usr_regs[regno - 8] = val;
542 } else {
543 env->regs[regno] = val;
547 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
549 if ((env->uncached_cpsr & CPSR_M) == mode) {
550 env->regs[13] = val;
551 } else {
552 env->banked_r13[bank_number(mode)] = val;
556 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
558 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_SYS) {
559 /* SRS instruction is UNPREDICTABLE from System mode; we UNDEF.
560 * Other UNPREDICTABLE and UNDEF cases were caught at translate time.
562 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
563 exception_target_el(env));
566 if ((env->uncached_cpsr & CPSR_M) == mode) {
567 return env->regs[13];
568 } else {
569 return env->banked_r13[bank_number(mode)];
573 static void msr_mrs_banked_exc_checks(CPUARMState *env, uint32_t tgtmode,
574 uint32_t regno)
576 /* Raise an exception if the requested access is one of the UNPREDICTABLE
577 * cases; otherwise return. This broadly corresponds to the pseudocode
578 * BankedRegisterAccessValid() and SPSRAccessValid(),
579 * except that we have already handled some cases at translate time.
581 int curmode = env->uncached_cpsr & CPSR_M;
583 if (curmode == tgtmode) {
584 goto undef;
587 if (tgtmode == ARM_CPU_MODE_USR) {
588 switch (regno) {
589 case 8 ... 12:
590 if (curmode != ARM_CPU_MODE_FIQ) {
591 goto undef;
593 break;
594 case 13:
595 if (curmode == ARM_CPU_MODE_SYS) {
596 goto undef;
598 break;
599 case 14:
600 if (curmode == ARM_CPU_MODE_HYP || curmode == ARM_CPU_MODE_SYS) {
601 goto undef;
603 break;
604 default:
605 break;
609 if (tgtmode == ARM_CPU_MODE_HYP) {
610 switch (regno) {
611 case 17: /* ELR_Hyp */
612 if (curmode != ARM_CPU_MODE_HYP && curmode != ARM_CPU_MODE_MON) {
613 goto undef;
615 break;
616 default:
617 if (curmode != ARM_CPU_MODE_MON) {
618 goto undef;
620 break;
624 return;
626 undef:
627 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
628 exception_target_el(env));
631 void HELPER(msr_banked)(CPUARMState *env, uint32_t value, uint32_t tgtmode,
632 uint32_t regno)
634 msr_mrs_banked_exc_checks(env, tgtmode, regno);
636 switch (regno) {
637 case 16: /* SPSRs */
638 env->banked_spsr[bank_number(tgtmode)] = value;
639 break;
640 case 17: /* ELR_Hyp */
641 env->elr_el[2] = value;
642 break;
643 case 13:
644 env->banked_r13[bank_number(tgtmode)] = value;
645 break;
646 case 14:
647 env->banked_r14[bank_number(tgtmode)] = value;
648 break;
649 case 8 ... 12:
650 switch (tgtmode) {
651 case ARM_CPU_MODE_USR:
652 env->usr_regs[regno - 8] = value;
653 break;
654 case ARM_CPU_MODE_FIQ:
655 env->fiq_regs[regno - 8] = value;
656 break;
657 default:
658 g_assert_not_reached();
660 break;
661 default:
662 g_assert_not_reached();
666 uint32_t HELPER(mrs_banked)(CPUARMState *env, uint32_t tgtmode, uint32_t regno)
668 msr_mrs_banked_exc_checks(env, tgtmode, regno);
670 switch (regno) {
671 case 16: /* SPSRs */
672 return env->banked_spsr[bank_number(tgtmode)];
673 case 17: /* ELR_Hyp */
674 return env->elr_el[2];
675 case 13:
676 return env->banked_r13[bank_number(tgtmode)];
677 case 14:
678 return env->banked_r14[bank_number(tgtmode)];
679 case 8 ... 12:
680 switch (tgtmode) {
681 case ARM_CPU_MODE_USR:
682 return env->usr_regs[regno - 8];
683 case ARM_CPU_MODE_FIQ:
684 return env->fiq_regs[regno - 8];
685 default:
686 g_assert_not_reached();
688 default:
689 g_assert_not_reached();
693 void HELPER(access_check_cp_reg)(CPUARMState *env, void *rip, uint32_t syndrome,
694 uint32_t isread)
696 const ARMCPRegInfo *ri = rip;
697 int target_el;
699 if (arm_feature(env, ARM_FEATURE_XSCALE) && ri->cp < 14
700 && extract32(env->cp15.c15_cpar, ri->cp, 1) == 0) {
701 raise_exception(env, EXCP_UDEF, syndrome, exception_target_el(env));
704 if (!ri->accessfn) {
705 return;
708 switch (ri->accessfn(env, ri, isread)) {
709 case CP_ACCESS_OK:
710 return;
711 case CP_ACCESS_TRAP:
712 target_el = exception_target_el(env);
713 break;
714 case CP_ACCESS_TRAP_EL2:
715 /* Requesting a trap to EL2 when we're in EL3 or S-EL0/1 is
716 * a bug in the access function.
718 assert(!arm_is_secure(env) && arm_current_el(env) != 3);
719 target_el = 2;
720 break;
721 case CP_ACCESS_TRAP_EL3:
722 target_el = 3;
723 break;
724 case CP_ACCESS_TRAP_UNCATEGORIZED:
725 target_el = exception_target_el(env);
726 syndrome = syn_uncategorized();
727 break;
728 case CP_ACCESS_TRAP_UNCATEGORIZED_EL2:
729 target_el = 2;
730 syndrome = syn_uncategorized();
731 break;
732 case CP_ACCESS_TRAP_UNCATEGORIZED_EL3:
733 target_el = 3;
734 syndrome = syn_uncategorized();
735 break;
736 case CP_ACCESS_TRAP_FP_EL2:
737 target_el = 2;
738 /* Since we are an implementation that takes exceptions on a trapped
739 * conditional insn only if the insn has passed its condition code
740 * check, we take the IMPDEF choice to always report CV=1 COND=0xe
741 * (which is also the required value for AArch64 traps).
743 syndrome = syn_fp_access_trap(1, 0xe, false);
744 break;
745 case CP_ACCESS_TRAP_FP_EL3:
746 target_el = 3;
747 syndrome = syn_fp_access_trap(1, 0xe, false);
748 break;
749 default:
750 g_assert_not_reached();
753 raise_exception(env, EXCP_UDEF, syndrome, target_el);
756 void HELPER(set_cp_reg)(CPUARMState *env, void *rip, uint32_t value)
758 const ARMCPRegInfo *ri = rip;
760 if (ri->type & ARM_CP_IO) {
761 qemu_mutex_lock_iothread();
762 ri->writefn(env, ri, value);
763 qemu_mutex_unlock_iothread();
764 } else {
765 ri->writefn(env, ri, value);
769 uint32_t HELPER(get_cp_reg)(CPUARMState *env, void *rip)
771 const ARMCPRegInfo *ri = rip;
772 uint32_t res;
774 if (ri->type & ARM_CP_IO) {
775 qemu_mutex_lock_iothread();
776 res = ri->readfn(env, ri);
777 qemu_mutex_unlock_iothread();
778 } else {
779 res = ri->readfn(env, ri);
782 return res;
785 void HELPER(set_cp_reg64)(CPUARMState *env, void *rip, uint64_t value)
787 const ARMCPRegInfo *ri = rip;
789 if (ri->type & ARM_CP_IO) {
790 qemu_mutex_lock_iothread();
791 ri->writefn(env, ri, value);
792 qemu_mutex_unlock_iothread();
793 } else {
794 ri->writefn(env, ri, value);
798 uint64_t HELPER(get_cp_reg64)(CPUARMState *env, void *rip)
800 const ARMCPRegInfo *ri = rip;
801 uint64_t res;
803 if (ri->type & ARM_CP_IO) {
804 qemu_mutex_lock_iothread();
805 res = ri->readfn(env, ri);
806 qemu_mutex_unlock_iothread();
807 } else {
808 res = ri->readfn(env, ri);
811 return res;
814 void HELPER(msr_i_pstate)(CPUARMState *env, uint32_t op, uint32_t imm)
816 /* MSR_i to update PSTATE. This is OK from EL0 only if UMA is set.
817 * Note that SPSel is never OK from EL0; we rely on handle_msr_i()
818 * to catch that case at translate time.
820 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
821 uint32_t syndrome = syn_aa64_sysregtrap(0, extract32(op, 0, 3),
822 extract32(op, 3, 3), 4,
823 imm, 0x1f, 0);
824 raise_exception(env, EXCP_UDEF, syndrome, exception_target_el(env));
827 switch (op) {
828 case 0x05: /* SPSel */
829 update_spsel(env, imm);
830 break;
831 case 0x1e: /* DAIFSet */
832 env->daif |= (imm << 6) & PSTATE_DAIF;
833 break;
834 case 0x1f: /* DAIFClear */
835 env->daif &= ~((imm << 6) & PSTATE_DAIF);
836 break;
837 default:
838 g_assert_not_reached();
842 void HELPER(clear_pstate_ss)(CPUARMState *env)
844 env->pstate &= ~PSTATE_SS;
847 void HELPER(pre_hvc)(CPUARMState *env)
849 ARMCPU *cpu = arm_env_get_cpu(env);
850 int cur_el = arm_current_el(env);
851 /* FIXME: Use actual secure state. */
852 bool secure = false;
853 bool undef;
855 if (arm_is_psci_call(cpu, EXCP_HVC)) {
856 /* If PSCI is enabled and this looks like a valid PSCI call then
857 * that overrides the architecturally mandated HVC behaviour.
859 return;
862 if (!arm_feature(env, ARM_FEATURE_EL2)) {
863 /* If EL2 doesn't exist, HVC always UNDEFs */
864 undef = true;
865 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
866 /* EL3.HCE has priority over EL2.HCD. */
867 undef = !(env->cp15.scr_el3 & SCR_HCE);
868 } else {
869 undef = env->cp15.hcr_el2 & HCR_HCD;
872 /* In ARMv7 and ARMv8/AArch32, HVC is undef in secure state.
873 * For ARMv8/AArch64, HVC is allowed in EL3.
874 * Note that we've already trapped HVC from EL0 at translation
875 * time.
877 if (secure && (!is_a64(env) || cur_el == 1)) {
878 undef = true;
881 if (undef) {
882 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
883 exception_target_el(env));
887 void HELPER(pre_smc)(CPUARMState *env, uint32_t syndrome)
889 ARMCPU *cpu = arm_env_get_cpu(env);
890 int cur_el = arm_current_el(env);
891 bool secure = arm_is_secure(env);
892 bool smd = env->cp15.scr_el3 & SCR_SMD;
893 /* On ARMv8 with EL3 AArch64, SMD applies to both S and NS state.
894 * On ARMv8 with EL3 AArch32, or ARMv7 with the Virtualization
895 * extensions, SMD only applies to NS state.
896 * On ARMv7 without the Virtualization extensions, the SMD bit
897 * doesn't exist, but we forbid the guest to set it to 1 in scr_write(),
898 * so we need not special case this here.
900 bool undef = arm_feature(env, ARM_FEATURE_AARCH64) ? smd : smd && !secure;
902 if (!arm_feature(env, ARM_FEATURE_EL3) &&
903 cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) {
904 /* If we have no EL3 then SMC always UNDEFs and can't be
905 * trapped to EL2. PSCI-via-SMC is a sort of ersatz EL3
906 * firmware within QEMU, and we want an EL2 guest to be able
907 * to forbid its EL1 from making PSCI calls into QEMU's
908 * "firmware" via HCR.TSC, so for these purposes treat
909 * PSCI-via-SMC as implying an EL3.
911 undef = true;
912 } else if (!secure && cur_el == 1 && (env->cp15.hcr_el2 & HCR_TSC)) {
913 /* In NS EL1, HCR controlled routing to EL2 has priority over SMD.
914 * We also want an EL2 guest to be able to forbid its EL1 from
915 * making PSCI calls into QEMU's "firmware" via HCR.TSC.
917 raise_exception(env, EXCP_HYP_TRAP, syndrome, 2);
920 /* If PSCI is enabled and this looks like a valid PSCI call then
921 * suppress the UNDEF -- we'll catch the SMC exception and
922 * implement the PSCI call behaviour there.
924 if (undef && !arm_is_psci_call(cpu, EXCP_SMC)) {
925 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
926 exception_target_el(env));
930 static int el_from_spsr(uint32_t spsr)
932 /* Return the exception level that this SPSR is requesting a return to,
933 * or -1 if it is invalid (an illegal return)
935 if (spsr & PSTATE_nRW) {
936 switch (spsr & CPSR_M) {
937 case ARM_CPU_MODE_USR:
938 return 0;
939 case ARM_CPU_MODE_HYP:
940 return 2;
941 case ARM_CPU_MODE_FIQ:
942 case ARM_CPU_MODE_IRQ:
943 case ARM_CPU_MODE_SVC:
944 case ARM_CPU_MODE_ABT:
945 case ARM_CPU_MODE_UND:
946 case ARM_CPU_MODE_SYS:
947 return 1;
948 case ARM_CPU_MODE_MON:
949 /* Returning to Mon from AArch64 is never possible,
950 * so this is an illegal return.
952 default:
953 return -1;
955 } else {
956 if (extract32(spsr, 1, 1)) {
957 /* Return with reserved M[1] bit set */
958 return -1;
960 if (extract32(spsr, 0, 4) == 1) {
961 /* return to EL0 with M[0] bit set */
962 return -1;
964 return extract32(spsr, 2, 2);
968 void HELPER(exception_return)(CPUARMState *env)
970 int cur_el = arm_current_el(env);
971 unsigned int spsr_idx = aarch64_banked_spsr_index(cur_el);
972 uint32_t spsr = env->banked_spsr[spsr_idx];
973 int new_el;
974 bool return_to_aa64 = (spsr & PSTATE_nRW) == 0;
976 aarch64_save_sp(env, cur_el);
978 arm_clear_exclusive(env);
980 /* We must squash the PSTATE.SS bit to zero unless both of the
981 * following hold:
982 * 1. debug exceptions are currently disabled
983 * 2. singlestep will be active in the EL we return to
984 * We check 1 here and 2 after we've done the pstate/cpsr write() to
985 * transition to the EL we're going to.
987 if (arm_generate_debug_exceptions(env)) {
988 spsr &= ~PSTATE_SS;
991 new_el = el_from_spsr(spsr);
992 if (new_el == -1) {
993 goto illegal_return;
995 if (new_el > cur_el
996 || (new_el == 2 && !arm_feature(env, ARM_FEATURE_EL2))) {
997 /* Disallow return to an EL which is unimplemented or higher
998 * than the current one.
1000 goto illegal_return;
1003 if (new_el != 0 && arm_el_is_aa64(env, new_el) != return_to_aa64) {
1004 /* Return to an EL which is configured for a different register width */
1005 goto illegal_return;
1008 if (new_el == 2 && arm_is_secure_below_el3(env)) {
1009 /* Return to the non-existent secure-EL2 */
1010 goto illegal_return;
1013 if (new_el == 1 && (env->cp15.hcr_el2 & HCR_TGE)
1014 && !arm_is_secure_below_el3(env)) {
1015 goto illegal_return;
1018 if (!return_to_aa64) {
1019 env->aarch64 = 0;
1020 /* We do a raw CPSR write because aarch64_sync_64_to_32()
1021 * will sort the register banks out for us, and we've already
1022 * caught all the bad-mode cases in el_from_spsr().
1024 cpsr_write(env, spsr, ~0, CPSRWriteRaw);
1025 if (!arm_singlestep_active(env)) {
1026 env->uncached_cpsr &= ~PSTATE_SS;
1028 aarch64_sync_64_to_32(env);
1030 if (spsr & CPSR_T) {
1031 env->regs[15] = env->elr_el[cur_el] & ~0x1;
1032 } else {
1033 env->regs[15] = env->elr_el[cur_el] & ~0x3;
1035 qemu_log_mask(CPU_LOG_INT, "Exception return from AArch64 EL%d to "
1036 "AArch32 EL%d PC 0x%" PRIx32 "\n",
1037 cur_el, new_el, env->regs[15]);
1038 } else {
1039 env->aarch64 = 1;
1040 pstate_write(env, spsr);
1041 if (!arm_singlestep_active(env)) {
1042 env->pstate &= ~PSTATE_SS;
1044 aarch64_restore_sp(env, new_el);
1045 env->pc = env->elr_el[cur_el];
1046 qemu_log_mask(CPU_LOG_INT, "Exception return from AArch64 EL%d to "
1047 "AArch64 EL%d PC 0x%" PRIx64 "\n",
1048 cur_el, new_el, env->pc);
1051 qemu_mutex_lock_iothread();
1052 arm_call_el_change_hook(arm_env_get_cpu(env));
1053 qemu_mutex_unlock_iothread();
1055 return;
1057 illegal_return:
1058 /* Illegal return events of various kinds have architecturally
1059 * mandated behaviour:
1060 * restore NZCV and DAIF from SPSR_ELx
1061 * set PSTATE.IL
1062 * restore PC from ELR_ELx
1063 * no change to exception level, execution state or stack pointer
1065 env->pstate |= PSTATE_IL;
1066 env->pc = env->elr_el[cur_el];
1067 spsr &= PSTATE_NZCV | PSTATE_DAIF;
1068 spsr |= pstate_read(env) & ~(PSTATE_NZCV | PSTATE_DAIF);
1069 pstate_write(env, spsr);
1070 if (!arm_singlestep_active(env)) {
1071 env->pstate &= ~PSTATE_SS;
1073 qemu_log_mask(LOG_GUEST_ERROR, "Illegal exception return at EL%d: "
1074 "resuming execution at 0x%" PRIx64 "\n", cur_el, env->pc);
1077 /* Return true if the linked breakpoint entry lbn passes its checks */
1078 static bool linked_bp_matches(ARMCPU *cpu, int lbn)
1080 CPUARMState *env = &cpu->env;
1081 uint64_t bcr = env->cp15.dbgbcr[lbn];
1082 int brps = extract32(cpu->dbgdidr, 24, 4);
1083 int ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
1084 int bt;
1085 uint32_t contextidr;
1087 /* Links to unimplemented or non-context aware breakpoints are
1088 * CONSTRAINED UNPREDICTABLE: either behave as if disabled, or
1089 * as if linked to an UNKNOWN context-aware breakpoint (in which
1090 * case DBGWCR<n>_EL1.LBN must indicate that breakpoint).
1091 * We choose the former.
1093 if (lbn > brps || lbn < (brps - ctx_cmps)) {
1094 return false;
1097 bcr = env->cp15.dbgbcr[lbn];
1099 if (extract64(bcr, 0, 1) == 0) {
1100 /* Linked breakpoint disabled : generate no events */
1101 return false;
1104 bt = extract64(bcr, 20, 4);
1106 /* We match the whole register even if this is AArch32 using the
1107 * short descriptor format (in which case it holds both PROCID and ASID),
1108 * since we don't implement the optional v7 context ID masking.
1110 contextidr = extract64(env->cp15.contextidr_el[1], 0, 32);
1112 switch (bt) {
1113 case 3: /* linked context ID match */
1114 if (arm_current_el(env) > 1) {
1115 /* Context matches never fire in EL2 or (AArch64) EL3 */
1116 return false;
1118 return (contextidr == extract64(env->cp15.dbgbvr[lbn], 0, 32));
1119 case 5: /* linked address mismatch (reserved in AArch64) */
1120 case 9: /* linked VMID match (reserved if no EL2) */
1121 case 11: /* linked context ID and VMID match (reserved if no EL2) */
1122 default:
1123 /* Links to Unlinked context breakpoints must generate no
1124 * events; we choose to do the same for reserved values too.
1126 return false;
1129 return false;
1132 static bool bp_wp_matches(ARMCPU *cpu, int n, bool is_wp)
1134 CPUARMState *env = &cpu->env;
1135 uint64_t cr;
1136 int pac, hmc, ssc, wt, lbn;
1137 /* Note that for watchpoints the check is against the CPU security
1138 * state, not the S/NS attribute on the offending data access.
1140 bool is_secure = arm_is_secure(env);
1141 int access_el = arm_current_el(env);
1143 if (is_wp) {
1144 CPUWatchpoint *wp = env->cpu_watchpoint[n];
1146 if (!wp || !(wp->flags & BP_WATCHPOINT_HIT)) {
1147 return false;
1149 cr = env->cp15.dbgwcr[n];
1150 if (wp->hitattrs.user) {
1151 /* The LDRT/STRT/LDT/STT "unprivileged access" instructions should
1152 * match watchpoints as if they were accesses done at EL0, even if
1153 * the CPU is at EL1 or higher.
1155 access_el = 0;
1157 } else {
1158 uint64_t pc = is_a64(env) ? env->pc : env->regs[15];
1160 if (!env->cpu_breakpoint[n] || env->cpu_breakpoint[n]->pc != pc) {
1161 return false;
1163 cr = env->cp15.dbgbcr[n];
1165 /* The WATCHPOINT_HIT flag guarantees us that the watchpoint is
1166 * enabled and that the address and access type match; for breakpoints
1167 * we know the address matched; check the remaining fields, including
1168 * linked breakpoints. We rely on WCR and BCR having the same layout
1169 * for the LBN, SSC, HMC, PAC/PMC and is-linked fields.
1170 * Note that some combinations of {PAC, HMC, SSC} are reserved and
1171 * must act either like some valid combination or as if the watchpoint
1172 * were disabled. We choose the former, and use this together with
1173 * the fact that EL3 must always be Secure and EL2 must always be
1174 * Non-Secure to simplify the code slightly compared to the full
1175 * table in the ARM ARM.
1177 pac = extract64(cr, 1, 2);
1178 hmc = extract64(cr, 13, 1);
1179 ssc = extract64(cr, 14, 2);
1181 switch (ssc) {
1182 case 0:
1183 break;
1184 case 1:
1185 case 3:
1186 if (is_secure) {
1187 return false;
1189 break;
1190 case 2:
1191 if (!is_secure) {
1192 return false;
1194 break;
1197 switch (access_el) {
1198 case 3:
1199 case 2:
1200 if (!hmc) {
1201 return false;
1203 break;
1204 case 1:
1205 if (extract32(pac, 0, 1) == 0) {
1206 return false;
1208 break;
1209 case 0:
1210 if (extract32(pac, 1, 1) == 0) {
1211 return false;
1213 break;
1214 default:
1215 g_assert_not_reached();
1218 wt = extract64(cr, 20, 1);
1219 lbn = extract64(cr, 16, 4);
1221 if (wt && !linked_bp_matches(cpu, lbn)) {
1222 return false;
1225 return true;
1228 static bool check_watchpoints(ARMCPU *cpu)
1230 CPUARMState *env = &cpu->env;
1231 int n;
1233 /* If watchpoints are disabled globally or we can't take debug
1234 * exceptions here then watchpoint firings are ignored.
1236 if (extract32(env->cp15.mdscr_el1, 15, 1) == 0
1237 || !arm_generate_debug_exceptions(env)) {
1238 return false;
1241 for (n = 0; n < ARRAY_SIZE(env->cpu_watchpoint); n++) {
1242 if (bp_wp_matches(cpu, n, true)) {
1243 return true;
1246 return false;
1249 static bool check_breakpoints(ARMCPU *cpu)
1251 CPUARMState *env = &cpu->env;
1252 int n;
1254 /* If breakpoints are disabled globally or we can't take debug
1255 * exceptions here then breakpoint firings are ignored.
1257 if (extract32(env->cp15.mdscr_el1, 15, 1) == 0
1258 || !arm_generate_debug_exceptions(env)) {
1259 return false;
1262 for (n = 0; n < ARRAY_SIZE(env->cpu_breakpoint); n++) {
1263 if (bp_wp_matches(cpu, n, false)) {
1264 return true;
1267 return false;
1270 void HELPER(check_breakpoints)(CPUARMState *env)
1272 ARMCPU *cpu = arm_env_get_cpu(env);
1274 if (check_breakpoints(cpu)) {
1275 HELPER(exception_internal(env, EXCP_DEBUG));
1279 bool arm_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
1281 /* Called by core code when a CPU watchpoint fires; need to check if this
1282 * is also an architectural watchpoint match.
1284 ARMCPU *cpu = ARM_CPU(cs);
1286 return check_watchpoints(cpu);
1289 vaddr arm_adjust_watchpoint_address(CPUState *cs, vaddr addr, int len)
1291 ARMCPU *cpu = ARM_CPU(cs);
1292 CPUARMState *env = &cpu->env;
1294 /* In BE32 system mode, target memory is stored byteswapped (on a
1295 * little-endian host system), and by the time we reach here (via an
1296 * opcode helper) the addresses of subword accesses have been adjusted
1297 * to account for that, which means that watchpoints will not match.
1298 * Undo the adjustment here.
1300 if (arm_sctlr_b(env)) {
1301 if (len == 1) {
1302 addr ^= 3;
1303 } else if (len == 2) {
1304 addr ^= 2;
1308 return addr;
1311 void arm_debug_excp_handler(CPUState *cs)
1313 /* Called by core code when a watchpoint or breakpoint fires;
1314 * need to check which one and raise the appropriate exception.
1316 ARMCPU *cpu = ARM_CPU(cs);
1317 CPUARMState *env = &cpu->env;
1318 CPUWatchpoint *wp_hit = cs->watchpoint_hit;
1320 if (wp_hit) {
1321 if (wp_hit->flags & BP_CPU) {
1322 bool wnr = (wp_hit->flags & BP_WATCHPOINT_HIT_WRITE) != 0;
1323 bool same_el = arm_debug_target_el(env) == arm_current_el(env);
1325 cs->watchpoint_hit = NULL;
1327 if (extended_addresses_enabled(env)) {
1328 env->exception.fsr = (1 << 9) | 0x22;
1329 } else {
1330 env->exception.fsr = 0x2;
1332 env->exception.vaddress = wp_hit->hitaddr;
1333 raise_exception(env, EXCP_DATA_ABORT,
1334 syn_watchpoint(same_el, 0, wnr),
1335 arm_debug_target_el(env));
1337 } else {
1338 uint64_t pc = is_a64(env) ? env->pc : env->regs[15];
1339 bool same_el = (arm_debug_target_el(env) == arm_current_el(env));
1341 /* (1) GDB breakpoints should be handled first.
1342 * (2) Do not raise a CPU exception if no CPU breakpoint has fired,
1343 * since singlestep is also done by generating a debug internal
1344 * exception.
1346 if (cpu_breakpoint_test(cs, pc, BP_GDB)
1347 || !cpu_breakpoint_test(cs, pc, BP_CPU)) {
1348 return;
1351 if (extended_addresses_enabled(env)) {
1352 env->exception.fsr = (1 << 9) | 0x22;
1353 } else {
1354 env->exception.fsr = 0x2;
1356 /* FAR is UNKNOWN, so doesn't need setting */
1357 raise_exception(env, EXCP_PREFETCH_ABORT,
1358 syn_breakpoint(same_el),
1359 arm_debug_target_el(env));
1363 /* ??? Flag setting arithmetic is awkward because we need to do comparisons.
1364 The only way to do that in TCG is a conditional branch, which clobbers
1365 all our temporaries. For now implement these as helper functions. */
1367 /* Similarly for variable shift instructions. */
1369 uint32_t HELPER(shl_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1371 int shift = i & 0xff;
1372 if (shift >= 32) {
1373 if (shift == 32)
1374 env->CF = x & 1;
1375 else
1376 env->CF = 0;
1377 return 0;
1378 } else if (shift != 0) {
1379 env->CF = (x >> (32 - shift)) & 1;
1380 return x << shift;
1382 return x;
1385 uint32_t HELPER(shr_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1387 int shift = i & 0xff;
1388 if (shift >= 32) {
1389 if (shift == 32)
1390 env->CF = (x >> 31) & 1;
1391 else
1392 env->CF = 0;
1393 return 0;
1394 } else if (shift != 0) {
1395 env->CF = (x >> (shift - 1)) & 1;
1396 return x >> shift;
1398 return x;
1401 uint32_t HELPER(sar_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1403 int shift = i & 0xff;
1404 if (shift >= 32) {
1405 env->CF = (x >> 31) & 1;
1406 return (int32_t)x >> 31;
1407 } else if (shift != 0) {
1408 env->CF = (x >> (shift - 1)) & 1;
1409 return (int32_t)x >> shift;
1411 return x;
1414 uint32_t HELPER(ror_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1416 int shift1, shift;
1417 shift1 = i & 0xff;
1418 shift = shift1 & 0x1f;
1419 if (shift == 0) {
1420 if (shift1 != 0)
1421 env->CF = (x >> 31) & 1;
1422 return x;
1423 } else {
1424 env->CF = (x >> (shift - 1)) & 1;
1425 return ((uint32_t)x >> shift) | (x << (32 - shift));