s390x: introduce 2.11 compat machine
[qemu.git] / target / arm / op_helper.c
blob2a8566657975924135ebbcc854d1fcd59e9d2402
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 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 assert(!excp_is_internal(excp));
37 cs->exception_index = excp;
38 env->exception.syndrome = syndrome;
39 env->exception.target_el = target_el;
40 cpu_loop_exit(cs);
43 static int exception_target_el(CPUARMState *env)
45 int target_el = MAX(1, arm_current_el(env));
47 /* No such thing as secure EL1 if EL3 is aarch32, so update the target EL
48 * to EL3 in this case.
50 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3) && target_el == 1) {
51 target_el = 3;
54 return target_el;
57 uint32_t HELPER(neon_tbl)(CPUARMState *env, uint32_t ireg, uint32_t def,
58 uint32_t rn, uint32_t maxindex)
60 uint32_t val;
61 uint32_t tmp;
62 int index;
63 int shift;
64 uint64_t *table;
65 table = (uint64_t *)&env->vfp.regs[rn];
66 val = 0;
67 for (shift = 0; shift < 32; shift += 8) {
68 index = (ireg >> shift) & 0xff;
69 if (index < maxindex) {
70 tmp = (table[index >> 3] >> ((index & 7) << 3)) & 0xff;
71 val |= tmp << shift;
72 } else {
73 val |= def & (0xff << shift);
76 return val;
79 #if !defined(CONFIG_USER_ONLY)
81 static inline uint32_t merge_syn_data_abort(uint32_t template_syn,
82 unsigned int target_el,
83 bool same_el,
84 bool s1ptw, bool is_write,
85 int fsc)
87 uint32_t syn;
89 /* ISV is only set for data aborts routed to EL2 and
90 * never for stage-1 page table walks faulting on stage 2.
92 * Furthermore, ISV is only set for certain kinds of load/stores.
93 * If the template syndrome does not have ISV set, we should leave
94 * it cleared.
96 * See ARMv8 specs, D7-1974:
97 * ISS encoding for an exception from a Data Abort, the
98 * ISV field.
100 if (!(template_syn & ARM_EL_ISV) || target_el != 2 || s1ptw) {
101 syn = syn_data_abort_no_iss(same_el,
102 0, 0, s1ptw, is_write, fsc);
103 } else {
104 /* Fields: IL, ISV, SAS, SSE, SRT, SF and AR come from the template
105 * syndrome created at translation time.
106 * Now we create the runtime syndrome with the remaining fields.
108 syn = syn_data_abort_with_iss(same_el,
109 0, 0, 0, 0, 0,
110 0, 0, s1ptw, is_write, fsc,
111 false);
112 /* Merge the runtime syndrome with the template syndrome. */
113 syn |= template_syn;
115 return syn;
118 /* try to fill the TLB and return an exception if error. If retaddr is
119 * NULL, it means that the function was called in C code (i.e. not
120 * from generated code or from helper.c)
122 void tlb_fill(CPUState *cs, target_ulong addr, MMUAccessType access_type,
123 int mmu_idx, uintptr_t retaddr)
125 bool ret;
126 uint32_t fsr = 0;
127 ARMMMUFaultInfo fi = {};
129 ret = arm_tlb_fill(cs, addr, access_type, mmu_idx, &fsr, &fi);
130 if (unlikely(ret)) {
131 ARMCPU *cpu = ARM_CPU(cs);
132 CPUARMState *env = &cpu->env;
133 uint32_t syn, exc, fsc;
134 unsigned int target_el;
135 bool same_el;
137 if (retaddr) {
138 /* now we have a real cpu fault */
139 cpu_restore_state(cs, retaddr);
142 target_el = exception_target_el(env);
143 if (fi.stage2) {
144 target_el = 2;
145 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
147 same_el = arm_current_el(env) == target_el;
149 if (fsr & (1 << 9)) {
150 /* LPAE format fault status register : bottom 6 bits are
151 * status code in the same form as needed for syndrome
153 fsc = extract32(fsr, 0, 6);
154 } else {
155 /* Short format FSR : this fault will never actually be reported
156 * to an EL that uses a syndrome register. Check that here,
157 * and use a (currently) reserved FSR code in case the constructed
158 * syndrome does leak into the guest somehow.
160 assert(target_el != 2 && !arm_el_is_aa64(env, target_el));
161 fsc = 0x3f;
164 /* For insn and data aborts we assume there is no instruction syndrome
165 * information; this is always true for exceptions reported to EL1.
167 if (access_type == MMU_INST_FETCH) {
168 syn = syn_insn_abort(same_el, 0, fi.s1ptw, fsc);
169 exc = EXCP_PREFETCH_ABORT;
170 } else {
171 syn = merge_syn_data_abort(env->exception.syndrome, target_el,
172 same_el, fi.s1ptw,
173 access_type == MMU_DATA_STORE, fsc);
174 if (access_type == MMU_DATA_STORE
175 && arm_feature(env, ARM_FEATURE_V6)) {
176 fsr |= (1 << 11);
178 exc = EXCP_DATA_ABORT;
181 env->exception.vaddress = addr;
182 env->exception.fsr = fsr;
183 raise_exception(env, exc, syn, target_el);
187 /* Raise a data fault alignment exception for the specified virtual address */
188 void arm_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr,
189 MMUAccessType access_type,
190 int mmu_idx, uintptr_t retaddr)
192 ARMCPU *cpu = ARM_CPU(cs);
193 CPUARMState *env = &cpu->env;
194 int target_el;
195 bool same_el;
196 uint32_t syn;
197 ARMMMUIdx arm_mmu_idx = core_to_arm_mmu_idx(env, mmu_idx);
199 if (retaddr) {
200 /* now we have a real cpu fault */
201 cpu_restore_state(cs, retaddr);
204 target_el = exception_target_el(env);
205 same_el = (arm_current_el(env) == target_el);
207 env->exception.vaddress = vaddr;
209 /* the DFSR for an alignment fault depends on whether we're using
210 * the LPAE long descriptor format, or the short descriptor format
212 if (arm_s1_regime_using_lpae_format(env, arm_mmu_idx)) {
213 env->exception.fsr = (1 << 9) | 0x21;
214 } else {
215 env->exception.fsr = 0x1;
218 if (access_type == MMU_DATA_STORE && arm_feature(env, ARM_FEATURE_V6)) {
219 env->exception.fsr |= (1 << 11);
222 syn = merge_syn_data_abort(env->exception.syndrome, target_el,
223 same_el, 0, access_type == MMU_DATA_STORE,
224 0x21);
225 raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
228 #endif /* !defined(CONFIG_USER_ONLY) */
230 uint32_t HELPER(add_setq)(CPUARMState *env, uint32_t a, uint32_t b)
232 uint32_t res = a + b;
233 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT))
234 env->QF = 1;
235 return res;
238 uint32_t HELPER(add_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
240 uint32_t res = a + b;
241 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT)) {
242 env->QF = 1;
243 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
245 return res;
248 uint32_t HELPER(sub_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
250 uint32_t res = a - b;
251 if (((res ^ a) & SIGNBIT) && ((a ^ b) & SIGNBIT)) {
252 env->QF = 1;
253 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
255 return res;
258 uint32_t HELPER(double_saturate)(CPUARMState *env, int32_t val)
260 uint32_t res;
261 if (val >= 0x40000000) {
262 res = ~SIGNBIT;
263 env->QF = 1;
264 } else if (val <= (int32_t)0xc0000000) {
265 res = SIGNBIT;
266 env->QF = 1;
267 } else {
268 res = val << 1;
270 return res;
273 uint32_t HELPER(add_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
275 uint32_t res = a + b;
276 if (res < a) {
277 env->QF = 1;
278 res = ~0;
280 return res;
283 uint32_t HELPER(sub_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
285 uint32_t res = a - b;
286 if (res > a) {
287 env->QF = 1;
288 res = 0;
290 return res;
293 /* Signed saturation. */
294 static inline uint32_t do_ssat(CPUARMState *env, int32_t val, int shift)
296 int32_t top;
297 uint32_t mask;
299 top = val >> shift;
300 mask = (1u << shift) - 1;
301 if (top > 0) {
302 env->QF = 1;
303 return mask;
304 } else if (top < -1) {
305 env->QF = 1;
306 return ~mask;
308 return val;
311 /* Unsigned saturation. */
312 static inline uint32_t do_usat(CPUARMState *env, int32_t val, int shift)
314 uint32_t max;
316 max = (1u << shift) - 1;
317 if (val < 0) {
318 env->QF = 1;
319 return 0;
320 } else if (val > max) {
321 env->QF = 1;
322 return max;
324 return val;
327 /* Signed saturate. */
328 uint32_t HELPER(ssat)(CPUARMState *env, uint32_t x, uint32_t shift)
330 return do_ssat(env, x, shift);
333 /* Dual halfword signed saturate. */
334 uint32_t HELPER(ssat16)(CPUARMState *env, uint32_t x, uint32_t shift)
336 uint32_t res;
338 res = (uint16_t)do_ssat(env, (int16_t)x, shift);
339 res |= do_ssat(env, ((int32_t)x) >> 16, shift) << 16;
340 return res;
343 /* Unsigned saturate. */
344 uint32_t HELPER(usat)(CPUARMState *env, uint32_t x, uint32_t shift)
346 return do_usat(env, x, shift);
349 /* Dual halfword unsigned saturate. */
350 uint32_t HELPER(usat16)(CPUARMState *env, uint32_t x, uint32_t shift)
352 uint32_t res;
354 res = (uint16_t)do_usat(env, (int16_t)x, shift);
355 res |= do_usat(env, ((int32_t)x) >> 16, shift) << 16;
356 return res;
359 void HELPER(setend)(CPUARMState *env)
361 env->uncached_cpsr ^= CPSR_E;
364 /* Function checks whether WFx (WFI/WFE) instructions are set up to be trapped.
365 * The function returns the target EL (1-3) if the instruction is to be trapped;
366 * otherwise it returns 0 indicating it is not trapped.
368 static inline int check_wfx_trap(CPUARMState *env, bool is_wfe)
370 int cur_el = arm_current_el(env);
371 uint64_t mask;
373 /* If we are currently in EL0 then we need to check if SCTLR is set up for
374 * WFx instructions being trapped to EL1. These trap bits don't exist in v7.
376 if (cur_el < 1 && arm_feature(env, ARM_FEATURE_V8)) {
377 int target_el;
379 mask = is_wfe ? SCTLR_nTWE : SCTLR_nTWI;
380 if (arm_is_secure_below_el3(env) && !arm_el_is_aa64(env, 3)) {
381 /* Secure EL0 and Secure PL1 is at EL3 */
382 target_el = 3;
383 } else {
384 target_el = 1;
387 if (!(env->cp15.sctlr_el[target_el] & mask)) {
388 return target_el;
392 /* We are not trapping to EL1; trap to EL2 if HCR_EL2 requires it
393 * No need for ARM_FEATURE check as if HCR_EL2 doesn't exist the
394 * bits will be zero indicating no trap.
396 if (cur_el < 2 && !arm_is_secure(env)) {
397 mask = (is_wfe) ? HCR_TWE : HCR_TWI;
398 if (env->cp15.hcr_el2 & mask) {
399 return 2;
403 /* We are not trapping to EL1 or EL2; trap to EL3 if SCR_EL3 requires it */
404 if (cur_el < 3) {
405 mask = (is_wfe) ? SCR_TWE : SCR_TWI;
406 if (env->cp15.scr_el3 & mask) {
407 return 3;
411 return 0;
414 void HELPER(wfi)(CPUARMState *env)
416 CPUState *cs = CPU(arm_env_get_cpu(env));
417 int target_el = check_wfx_trap(env, false);
419 if (cpu_has_work(cs)) {
420 /* Don't bother to go into our "low power state" if
421 * we would just wake up immediately.
423 return;
426 if (target_el) {
427 env->pc -= 4;
428 raise_exception(env, EXCP_UDEF, syn_wfx(1, 0xe, 0), target_el);
431 cs->exception_index = EXCP_HLT;
432 cs->halted = 1;
433 cpu_loop_exit(cs);
436 void HELPER(wfe)(CPUARMState *env)
438 /* This is a hint instruction that is semantically different
439 * from YIELD even though we currently implement it identically.
440 * Don't actually halt the CPU, just yield back to top
441 * level loop. This is not going into a "low power state"
442 * (ie halting until some event occurs), so we never take
443 * a configurable trap to a different exception level.
445 HELPER(yield)(env);
448 void HELPER(yield)(CPUARMState *env)
450 ARMCPU *cpu = arm_env_get_cpu(env);
451 CPUState *cs = CPU(cpu);
453 /* When running in MTTCG we don't generate jumps to the yield and
454 * WFE helpers as it won't affect the scheduling of other vCPUs.
455 * If we wanted to more completely model WFE/SEV so we don't busy
456 * spin unnecessarily we would need to do something more involved.
458 g_assert(!parallel_cpus);
460 /* This is a non-trappable hint instruction that generally indicates
461 * that the guest is currently busy-looping. Yield control back to the
462 * top level loop so that a more deserving VCPU has a chance to run.
464 cs->exception_index = EXCP_YIELD;
465 cpu_loop_exit(cs);
468 /* Raise an internal-to-QEMU exception. This is limited to only
469 * those EXCP values which are special cases for QEMU to interrupt
470 * execution and not to be used for exceptions which are passed to
471 * the guest (those must all have syndrome information and thus should
472 * use exception_with_syndrome).
474 void HELPER(exception_internal)(CPUARMState *env, uint32_t excp)
476 CPUState *cs = CPU(arm_env_get_cpu(env));
478 assert(excp_is_internal(excp));
479 cs->exception_index = excp;
480 cpu_loop_exit(cs);
483 /* Raise an exception with the specified syndrome register value */
484 void HELPER(exception_with_syndrome)(CPUARMState *env, uint32_t excp,
485 uint32_t syndrome, uint32_t target_el)
487 raise_exception(env, excp, syndrome, target_el);
490 uint32_t HELPER(cpsr_read)(CPUARMState *env)
492 return cpsr_read(env) & ~(CPSR_EXEC | CPSR_RESERVED);
495 void HELPER(cpsr_write)(CPUARMState *env, uint32_t val, uint32_t mask)
497 cpsr_write(env, val, mask, CPSRWriteByInstr);
500 /* Write the CPSR for a 32-bit exception return */
501 void HELPER(cpsr_write_eret)(CPUARMState *env, uint32_t val)
503 cpsr_write(env, val, CPSR_ERET_MASK, CPSRWriteExceptionReturn);
505 /* Generated code has already stored the new PC value, but
506 * without masking out its low bits, because which bits need
507 * masking depends on whether we're returning to Thumb or ARM
508 * state. Do the masking now.
510 env->regs[15] &= (env->thumb ? ~1 : ~3);
512 qemu_mutex_lock_iothread();
513 arm_call_el_change_hook(arm_env_get_cpu(env));
514 qemu_mutex_unlock_iothread();
517 /* Access to user mode registers from privileged modes. */
518 uint32_t HELPER(get_user_reg)(CPUARMState *env, uint32_t regno)
520 uint32_t val;
522 if (regno == 13) {
523 val = env->banked_r13[BANK_USRSYS];
524 } else if (regno == 14) {
525 val = env->banked_r14[BANK_USRSYS];
526 } else if (regno >= 8
527 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
528 val = env->usr_regs[regno - 8];
529 } else {
530 val = env->regs[regno];
532 return val;
535 void HELPER(set_user_reg)(CPUARMState *env, uint32_t regno, uint32_t val)
537 if (regno == 13) {
538 env->banked_r13[BANK_USRSYS] = val;
539 } else if (regno == 14) {
540 env->banked_r14[BANK_USRSYS] = val;
541 } else if (regno >= 8
542 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
543 env->usr_regs[regno - 8] = val;
544 } else {
545 env->regs[regno] = val;
549 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
551 if ((env->uncached_cpsr & CPSR_M) == mode) {
552 env->regs[13] = val;
553 } else {
554 env->banked_r13[bank_number(mode)] = val;
558 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
560 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_SYS) {
561 /* SRS instruction is UNPREDICTABLE from System mode; we UNDEF.
562 * Other UNPREDICTABLE and UNDEF cases were caught at translate time.
564 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
565 exception_target_el(env));
568 if ((env->uncached_cpsr & CPSR_M) == mode) {
569 return env->regs[13];
570 } else {
571 return env->banked_r13[bank_number(mode)];
575 static void msr_mrs_banked_exc_checks(CPUARMState *env, uint32_t tgtmode,
576 uint32_t regno)
578 /* Raise an exception if the requested access is one of the UNPREDICTABLE
579 * cases; otherwise return. This broadly corresponds to the pseudocode
580 * BankedRegisterAccessValid() and SPSRAccessValid(),
581 * except that we have already handled some cases at translate time.
583 int curmode = env->uncached_cpsr & CPSR_M;
585 if (curmode == tgtmode) {
586 goto undef;
589 if (tgtmode == ARM_CPU_MODE_USR) {
590 switch (regno) {
591 case 8 ... 12:
592 if (curmode != ARM_CPU_MODE_FIQ) {
593 goto undef;
595 break;
596 case 13:
597 if (curmode == ARM_CPU_MODE_SYS) {
598 goto undef;
600 break;
601 case 14:
602 if (curmode == ARM_CPU_MODE_HYP || curmode == ARM_CPU_MODE_SYS) {
603 goto undef;
605 break;
606 default:
607 break;
611 if (tgtmode == ARM_CPU_MODE_HYP) {
612 switch (regno) {
613 case 17: /* ELR_Hyp */
614 if (curmode != ARM_CPU_MODE_HYP && curmode != ARM_CPU_MODE_MON) {
615 goto undef;
617 break;
618 default:
619 if (curmode != ARM_CPU_MODE_MON) {
620 goto undef;
622 break;
626 return;
628 undef:
629 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
630 exception_target_el(env));
633 void HELPER(msr_banked)(CPUARMState *env, uint32_t value, uint32_t tgtmode,
634 uint32_t regno)
636 msr_mrs_banked_exc_checks(env, tgtmode, regno);
638 switch (regno) {
639 case 16: /* SPSRs */
640 env->banked_spsr[bank_number(tgtmode)] = value;
641 break;
642 case 17: /* ELR_Hyp */
643 env->elr_el[2] = value;
644 break;
645 case 13:
646 env->banked_r13[bank_number(tgtmode)] = value;
647 break;
648 case 14:
649 env->banked_r14[bank_number(tgtmode)] = value;
650 break;
651 case 8 ... 12:
652 switch (tgtmode) {
653 case ARM_CPU_MODE_USR:
654 env->usr_regs[regno - 8] = value;
655 break;
656 case ARM_CPU_MODE_FIQ:
657 env->fiq_regs[regno - 8] = value;
658 break;
659 default:
660 g_assert_not_reached();
662 break;
663 default:
664 g_assert_not_reached();
668 uint32_t HELPER(mrs_banked)(CPUARMState *env, uint32_t tgtmode, uint32_t regno)
670 msr_mrs_banked_exc_checks(env, tgtmode, regno);
672 switch (regno) {
673 case 16: /* SPSRs */
674 return env->banked_spsr[bank_number(tgtmode)];
675 case 17: /* ELR_Hyp */
676 return env->elr_el[2];
677 case 13:
678 return env->banked_r13[bank_number(tgtmode)];
679 case 14:
680 return env->banked_r14[bank_number(tgtmode)];
681 case 8 ... 12:
682 switch (tgtmode) {
683 case ARM_CPU_MODE_USR:
684 return env->usr_regs[regno - 8];
685 case ARM_CPU_MODE_FIQ:
686 return env->fiq_regs[regno - 8];
687 default:
688 g_assert_not_reached();
690 default:
691 g_assert_not_reached();
695 void HELPER(access_check_cp_reg)(CPUARMState *env, void *rip, uint32_t syndrome,
696 uint32_t isread)
698 const ARMCPRegInfo *ri = rip;
699 int target_el;
701 if (arm_feature(env, ARM_FEATURE_XSCALE) && ri->cp < 14
702 && extract32(env->cp15.c15_cpar, ri->cp, 1) == 0) {
703 raise_exception(env, EXCP_UDEF, syndrome, exception_target_el(env));
706 if (!ri->accessfn) {
707 return;
710 switch (ri->accessfn(env, ri, isread)) {
711 case CP_ACCESS_OK:
712 return;
713 case CP_ACCESS_TRAP:
714 target_el = exception_target_el(env);
715 break;
716 case CP_ACCESS_TRAP_EL2:
717 /* Requesting a trap to EL2 when we're in EL3 or S-EL0/1 is
718 * a bug in the access function.
720 assert(!arm_is_secure(env) && arm_current_el(env) != 3);
721 target_el = 2;
722 break;
723 case CP_ACCESS_TRAP_EL3:
724 target_el = 3;
725 break;
726 case CP_ACCESS_TRAP_UNCATEGORIZED:
727 target_el = exception_target_el(env);
728 syndrome = syn_uncategorized();
729 break;
730 case CP_ACCESS_TRAP_UNCATEGORIZED_EL2:
731 target_el = 2;
732 syndrome = syn_uncategorized();
733 break;
734 case CP_ACCESS_TRAP_UNCATEGORIZED_EL3:
735 target_el = 3;
736 syndrome = syn_uncategorized();
737 break;
738 case CP_ACCESS_TRAP_FP_EL2:
739 target_el = 2;
740 /* Since we are an implementation that takes exceptions on a trapped
741 * conditional insn only if the insn has passed its condition code
742 * check, we take the IMPDEF choice to always report CV=1 COND=0xe
743 * (which is also the required value for AArch64 traps).
745 syndrome = syn_fp_access_trap(1, 0xe, false);
746 break;
747 case CP_ACCESS_TRAP_FP_EL3:
748 target_el = 3;
749 syndrome = syn_fp_access_trap(1, 0xe, false);
750 break;
751 default:
752 g_assert_not_reached();
755 raise_exception(env, EXCP_UDEF, syndrome, target_el);
758 void HELPER(set_cp_reg)(CPUARMState *env, void *rip, uint32_t value)
760 const ARMCPRegInfo *ri = rip;
762 if (ri->type & ARM_CP_IO) {
763 qemu_mutex_lock_iothread();
764 ri->writefn(env, ri, value);
765 qemu_mutex_unlock_iothread();
766 } else {
767 ri->writefn(env, ri, value);
771 uint32_t HELPER(get_cp_reg)(CPUARMState *env, void *rip)
773 const ARMCPRegInfo *ri = rip;
774 uint32_t res;
776 if (ri->type & ARM_CP_IO) {
777 qemu_mutex_lock_iothread();
778 res = ri->readfn(env, ri);
779 qemu_mutex_unlock_iothread();
780 } else {
781 res = ri->readfn(env, ri);
784 return res;
787 void HELPER(set_cp_reg64)(CPUARMState *env, void *rip, uint64_t value)
789 const ARMCPRegInfo *ri = rip;
791 if (ri->type & ARM_CP_IO) {
792 qemu_mutex_lock_iothread();
793 ri->writefn(env, ri, value);
794 qemu_mutex_unlock_iothread();
795 } else {
796 ri->writefn(env, ri, value);
800 uint64_t HELPER(get_cp_reg64)(CPUARMState *env, void *rip)
802 const ARMCPRegInfo *ri = rip;
803 uint64_t res;
805 if (ri->type & ARM_CP_IO) {
806 qemu_mutex_lock_iothread();
807 res = ri->readfn(env, ri);
808 qemu_mutex_unlock_iothread();
809 } else {
810 res = ri->readfn(env, ri);
813 return res;
816 void HELPER(msr_i_pstate)(CPUARMState *env, uint32_t op, uint32_t imm)
818 /* MSR_i to update PSTATE. This is OK from EL0 only if UMA is set.
819 * Note that SPSel is never OK from EL0; we rely on handle_msr_i()
820 * to catch that case at translate time.
822 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
823 uint32_t syndrome = syn_aa64_sysregtrap(0, extract32(op, 0, 3),
824 extract32(op, 3, 3), 4,
825 imm, 0x1f, 0);
826 raise_exception(env, EXCP_UDEF, syndrome, exception_target_el(env));
829 switch (op) {
830 case 0x05: /* SPSel */
831 update_spsel(env, imm);
832 break;
833 case 0x1e: /* DAIFSet */
834 env->daif |= (imm << 6) & PSTATE_DAIF;
835 break;
836 case 0x1f: /* DAIFClear */
837 env->daif &= ~((imm << 6) & PSTATE_DAIF);
838 break;
839 default:
840 g_assert_not_reached();
844 void HELPER(clear_pstate_ss)(CPUARMState *env)
846 env->pstate &= ~PSTATE_SS;
849 void HELPER(pre_hvc)(CPUARMState *env)
851 ARMCPU *cpu = arm_env_get_cpu(env);
852 int cur_el = arm_current_el(env);
853 /* FIXME: Use actual secure state. */
854 bool secure = false;
855 bool undef;
857 if (arm_is_psci_call(cpu, EXCP_HVC)) {
858 /* If PSCI is enabled and this looks like a valid PSCI call then
859 * that overrides the architecturally mandated HVC behaviour.
861 return;
864 if (!arm_feature(env, ARM_FEATURE_EL2)) {
865 /* If EL2 doesn't exist, HVC always UNDEFs */
866 undef = true;
867 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
868 /* EL3.HCE has priority over EL2.HCD. */
869 undef = !(env->cp15.scr_el3 & SCR_HCE);
870 } else {
871 undef = env->cp15.hcr_el2 & HCR_HCD;
874 /* In ARMv7 and ARMv8/AArch32, HVC is undef in secure state.
875 * For ARMv8/AArch64, HVC is allowed in EL3.
876 * Note that we've already trapped HVC from EL0 at translation
877 * time.
879 if (secure && (!is_a64(env) || cur_el == 1)) {
880 undef = true;
883 if (undef) {
884 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
885 exception_target_el(env));
889 void HELPER(pre_smc)(CPUARMState *env, uint32_t syndrome)
891 ARMCPU *cpu = arm_env_get_cpu(env);
892 int cur_el = arm_current_el(env);
893 bool secure = arm_is_secure(env);
894 bool smd = env->cp15.scr_el3 & SCR_SMD;
895 /* On ARMv8 with EL3 AArch64, SMD applies to both S and NS state.
896 * On ARMv8 with EL3 AArch32, or ARMv7 with the Virtualization
897 * extensions, SMD only applies to NS state.
898 * On ARMv7 without the Virtualization extensions, the SMD bit
899 * doesn't exist, but we forbid the guest to set it to 1 in scr_write(),
900 * so we need not special case this here.
902 bool undef = arm_feature(env, ARM_FEATURE_AARCH64) ? smd : smd && !secure;
904 if (arm_is_psci_call(cpu, EXCP_SMC)) {
905 /* If PSCI is enabled and this looks like a valid PSCI call then
906 * that overrides the architecturally mandated SMC behaviour.
908 return;
911 if (!arm_feature(env, ARM_FEATURE_EL3)) {
912 /* If we have no EL3 then SMC always UNDEFs */
913 undef = true;
914 } else if (!secure && cur_el == 1 && (env->cp15.hcr_el2 & HCR_TSC)) {
915 /* In NS EL1, HCR controlled routing to EL2 has priority over SMD. */
916 raise_exception(env, EXCP_HYP_TRAP, syndrome, 2);
919 if (undef) {
920 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
921 exception_target_el(env));
925 static int el_from_spsr(uint32_t spsr)
927 /* Return the exception level that this SPSR is requesting a return to,
928 * or -1 if it is invalid (an illegal return)
930 if (spsr & PSTATE_nRW) {
931 switch (spsr & CPSR_M) {
932 case ARM_CPU_MODE_USR:
933 return 0;
934 case ARM_CPU_MODE_HYP:
935 return 2;
936 case ARM_CPU_MODE_FIQ:
937 case ARM_CPU_MODE_IRQ:
938 case ARM_CPU_MODE_SVC:
939 case ARM_CPU_MODE_ABT:
940 case ARM_CPU_MODE_UND:
941 case ARM_CPU_MODE_SYS:
942 return 1;
943 case ARM_CPU_MODE_MON:
944 /* Returning to Mon from AArch64 is never possible,
945 * so this is an illegal return.
947 default:
948 return -1;
950 } else {
951 if (extract32(spsr, 1, 1)) {
952 /* Return with reserved M[1] bit set */
953 return -1;
955 if (extract32(spsr, 0, 4) == 1) {
956 /* return to EL0 with M[0] bit set */
957 return -1;
959 return extract32(spsr, 2, 2);
963 void HELPER(exception_return)(CPUARMState *env)
965 int cur_el = arm_current_el(env);
966 unsigned int spsr_idx = aarch64_banked_spsr_index(cur_el);
967 uint32_t spsr = env->banked_spsr[spsr_idx];
968 int new_el;
969 bool return_to_aa64 = (spsr & PSTATE_nRW) == 0;
971 aarch64_save_sp(env, cur_el);
973 env->exclusive_addr = -1;
975 /* We must squash the PSTATE.SS bit to zero unless both of the
976 * following hold:
977 * 1. debug exceptions are currently disabled
978 * 2. singlestep will be active in the EL we return to
979 * We check 1 here and 2 after we've done the pstate/cpsr write() to
980 * transition to the EL we're going to.
982 if (arm_generate_debug_exceptions(env)) {
983 spsr &= ~PSTATE_SS;
986 new_el = el_from_spsr(spsr);
987 if (new_el == -1) {
988 goto illegal_return;
990 if (new_el > cur_el
991 || (new_el == 2 && !arm_feature(env, ARM_FEATURE_EL2))) {
992 /* Disallow return to an EL which is unimplemented or higher
993 * than the current one.
995 goto illegal_return;
998 if (new_el != 0 && arm_el_is_aa64(env, new_el) != return_to_aa64) {
999 /* Return to an EL which is configured for a different register width */
1000 goto illegal_return;
1003 if (new_el == 2 && arm_is_secure_below_el3(env)) {
1004 /* Return to the non-existent secure-EL2 */
1005 goto illegal_return;
1008 if (new_el == 1 && (env->cp15.hcr_el2 & HCR_TGE)
1009 && !arm_is_secure_below_el3(env)) {
1010 goto illegal_return;
1013 if (!return_to_aa64) {
1014 env->aarch64 = 0;
1015 /* We do a raw CPSR write because aarch64_sync_64_to_32()
1016 * will sort the register banks out for us, and we've already
1017 * caught all the bad-mode cases in el_from_spsr().
1019 cpsr_write(env, spsr, ~0, CPSRWriteRaw);
1020 if (!arm_singlestep_active(env)) {
1021 env->uncached_cpsr &= ~PSTATE_SS;
1023 aarch64_sync_64_to_32(env);
1025 if (spsr & CPSR_T) {
1026 env->regs[15] = env->elr_el[cur_el] & ~0x1;
1027 } else {
1028 env->regs[15] = env->elr_el[cur_el] & ~0x3;
1030 qemu_log_mask(CPU_LOG_INT, "Exception return from AArch64 EL%d to "
1031 "AArch32 EL%d PC 0x%" PRIx32 "\n",
1032 cur_el, new_el, env->regs[15]);
1033 } else {
1034 env->aarch64 = 1;
1035 pstate_write(env, spsr);
1036 if (!arm_singlestep_active(env)) {
1037 env->pstate &= ~PSTATE_SS;
1039 aarch64_restore_sp(env, new_el);
1040 env->pc = env->elr_el[cur_el];
1041 qemu_log_mask(CPU_LOG_INT, "Exception return from AArch64 EL%d to "
1042 "AArch64 EL%d PC 0x%" PRIx64 "\n",
1043 cur_el, new_el, env->pc);
1046 qemu_mutex_lock_iothread();
1047 arm_call_el_change_hook(arm_env_get_cpu(env));
1048 qemu_mutex_unlock_iothread();
1050 return;
1052 illegal_return:
1053 /* Illegal return events of various kinds have architecturally
1054 * mandated behaviour:
1055 * restore NZCV and DAIF from SPSR_ELx
1056 * set PSTATE.IL
1057 * restore PC from ELR_ELx
1058 * no change to exception level, execution state or stack pointer
1060 env->pstate |= PSTATE_IL;
1061 env->pc = env->elr_el[cur_el];
1062 spsr &= PSTATE_NZCV | PSTATE_DAIF;
1063 spsr |= pstate_read(env) & ~(PSTATE_NZCV | PSTATE_DAIF);
1064 pstate_write(env, spsr);
1065 if (!arm_singlestep_active(env)) {
1066 env->pstate &= ~PSTATE_SS;
1068 qemu_log_mask(LOG_GUEST_ERROR, "Illegal exception return at EL%d: "
1069 "resuming execution at 0x%" PRIx64 "\n", cur_el, env->pc);
1072 /* Return true if the linked breakpoint entry lbn passes its checks */
1073 static bool linked_bp_matches(ARMCPU *cpu, int lbn)
1075 CPUARMState *env = &cpu->env;
1076 uint64_t bcr = env->cp15.dbgbcr[lbn];
1077 int brps = extract32(cpu->dbgdidr, 24, 4);
1078 int ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
1079 int bt;
1080 uint32_t contextidr;
1082 /* Links to unimplemented or non-context aware breakpoints are
1083 * CONSTRAINED UNPREDICTABLE: either behave as if disabled, or
1084 * as if linked to an UNKNOWN context-aware breakpoint (in which
1085 * case DBGWCR<n>_EL1.LBN must indicate that breakpoint).
1086 * We choose the former.
1088 if (lbn > brps || lbn < (brps - ctx_cmps)) {
1089 return false;
1092 bcr = env->cp15.dbgbcr[lbn];
1094 if (extract64(bcr, 0, 1) == 0) {
1095 /* Linked breakpoint disabled : generate no events */
1096 return false;
1099 bt = extract64(bcr, 20, 4);
1101 /* We match the whole register even if this is AArch32 using the
1102 * short descriptor format (in which case it holds both PROCID and ASID),
1103 * since we don't implement the optional v7 context ID masking.
1105 contextidr = extract64(env->cp15.contextidr_el[1], 0, 32);
1107 switch (bt) {
1108 case 3: /* linked context ID match */
1109 if (arm_current_el(env) > 1) {
1110 /* Context matches never fire in EL2 or (AArch64) EL3 */
1111 return false;
1113 return (contextidr == extract64(env->cp15.dbgbvr[lbn], 0, 32));
1114 case 5: /* linked address mismatch (reserved in AArch64) */
1115 case 9: /* linked VMID match (reserved if no EL2) */
1116 case 11: /* linked context ID and VMID match (reserved if no EL2) */
1117 default:
1118 /* Links to Unlinked context breakpoints must generate no
1119 * events; we choose to do the same for reserved values too.
1121 return false;
1124 return false;
1127 static bool bp_wp_matches(ARMCPU *cpu, int n, bool is_wp)
1129 CPUARMState *env = &cpu->env;
1130 uint64_t cr;
1131 int pac, hmc, ssc, wt, lbn;
1132 /* Note that for watchpoints the check is against the CPU security
1133 * state, not the S/NS attribute on the offending data access.
1135 bool is_secure = arm_is_secure(env);
1136 int access_el = arm_current_el(env);
1138 if (is_wp) {
1139 CPUWatchpoint *wp = env->cpu_watchpoint[n];
1141 if (!wp || !(wp->flags & BP_WATCHPOINT_HIT)) {
1142 return false;
1144 cr = env->cp15.dbgwcr[n];
1145 if (wp->hitattrs.user) {
1146 /* The LDRT/STRT/LDT/STT "unprivileged access" instructions should
1147 * match watchpoints as if they were accesses done at EL0, even if
1148 * the CPU is at EL1 or higher.
1150 access_el = 0;
1152 } else {
1153 uint64_t pc = is_a64(env) ? env->pc : env->regs[15];
1155 if (!env->cpu_breakpoint[n] || env->cpu_breakpoint[n]->pc != pc) {
1156 return false;
1158 cr = env->cp15.dbgbcr[n];
1160 /* The WATCHPOINT_HIT flag guarantees us that the watchpoint is
1161 * enabled and that the address and access type match; for breakpoints
1162 * we know the address matched; check the remaining fields, including
1163 * linked breakpoints. We rely on WCR and BCR having the same layout
1164 * for the LBN, SSC, HMC, PAC/PMC and is-linked fields.
1165 * Note that some combinations of {PAC, HMC, SSC} are reserved and
1166 * must act either like some valid combination or as if the watchpoint
1167 * were disabled. We choose the former, and use this together with
1168 * the fact that EL3 must always be Secure and EL2 must always be
1169 * Non-Secure to simplify the code slightly compared to the full
1170 * table in the ARM ARM.
1172 pac = extract64(cr, 1, 2);
1173 hmc = extract64(cr, 13, 1);
1174 ssc = extract64(cr, 14, 2);
1176 switch (ssc) {
1177 case 0:
1178 break;
1179 case 1:
1180 case 3:
1181 if (is_secure) {
1182 return false;
1184 break;
1185 case 2:
1186 if (!is_secure) {
1187 return false;
1189 break;
1192 switch (access_el) {
1193 case 3:
1194 case 2:
1195 if (!hmc) {
1196 return false;
1198 break;
1199 case 1:
1200 if (extract32(pac, 0, 1) == 0) {
1201 return false;
1203 break;
1204 case 0:
1205 if (extract32(pac, 1, 1) == 0) {
1206 return false;
1208 break;
1209 default:
1210 g_assert_not_reached();
1213 wt = extract64(cr, 20, 1);
1214 lbn = extract64(cr, 16, 4);
1216 if (wt && !linked_bp_matches(cpu, lbn)) {
1217 return false;
1220 return true;
1223 static bool check_watchpoints(ARMCPU *cpu)
1225 CPUARMState *env = &cpu->env;
1226 int n;
1228 /* If watchpoints are disabled globally or we can't take debug
1229 * exceptions here then watchpoint firings are ignored.
1231 if (extract32(env->cp15.mdscr_el1, 15, 1) == 0
1232 || !arm_generate_debug_exceptions(env)) {
1233 return false;
1236 for (n = 0; n < ARRAY_SIZE(env->cpu_watchpoint); n++) {
1237 if (bp_wp_matches(cpu, n, true)) {
1238 return true;
1241 return false;
1244 static bool check_breakpoints(ARMCPU *cpu)
1246 CPUARMState *env = &cpu->env;
1247 int n;
1249 /* If breakpoints are disabled globally or we can't take debug
1250 * exceptions here then breakpoint firings are ignored.
1252 if (extract32(env->cp15.mdscr_el1, 15, 1) == 0
1253 || !arm_generate_debug_exceptions(env)) {
1254 return false;
1257 for (n = 0; n < ARRAY_SIZE(env->cpu_breakpoint); n++) {
1258 if (bp_wp_matches(cpu, n, false)) {
1259 return true;
1262 return false;
1265 void HELPER(check_breakpoints)(CPUARMState *env)
1267 ARMCPU *cpu = arm_env_get_cpu(env);
1269 if (check_breakpoints(cpu)) {
1270 HELPER(exception_internal(env, EXCP_DEBUG));
1274 bool arm_debug_check_watchpoint(CPUState *cs, CPUWatchpoint *wp)
1276 /* Called by core code when a CPU watchpoint fires; need to check if this
1277 * is also an architectural watchpoint match.
1279 ARMCPU *cpu = ARM_CPU(cs);
1281 return check_watchpoints(cpu);
1284 vaddr arm_adjust_watchpoint_address(CPUState *cs, vaddr addr, int len)
1286 ARMCPU *cpu = ARM_CPU(cs);
1287 CPUARMState *env = &cpu->env;
1289 /* In BE32 system mode, target memory is stored byteswapped (on a
1290 * little-endian host system), and by the time we reach here (via an
1291 * opcode helper) the addresses of subword accesses have been adjusted
1292 * to account for that, which means that watchpoints will not match.
1293 * Undo the adjustment here.
1295 if (arm_sctlr_b(env)) {
1296 if (len == 1) {
1297 addr ^= 3;
1298 } else if (len == 2) {
1299 addr ^= 2;
1303 return addr;
1306 void arm_debug_excp_handler(CPUState *cs)
1308 /* Called by core code when a watchpoint or breakpoint fires;
1309 * need to check which one and raise the appropriate exception.
1311 ARMCPU *cpu = ARM_CPU(cs);
1312 CPUARMState *env = &cpu->env;
1313 CPUWatchpoint *wp_hit = cs->watchpoint_hit;
1315 if (wp_hit) {
1316 if (wp_hit->flags & BP_CPU) {
1317 bool wnr = (wp_hit->flags & BP_WATCHPOINT_HIT_WRITE) != 0;
1318 bool same_el = arm_debug_target_el(env) == arm_current_el(env);
1320 cs->watchpoint_hit = NULL;
1322 if (extended_addresses_enabled(env)) {
1323 env->exception.fsr = (1 << 9) | 0x22;
1324 } else {
1325 env->exception.fsr = 0x2;
1327 env->exception.vaddress = wp_hit->hitaddr;
1328 raise_exception(env, EXCP_DATA_ABORT,
1329 syn_watchpoint(same_el, 0, wnr),
1330 arm_debug_target_el(env));
1332 } else {
1333 uint64_t pc = is_a64(env) ? env->pc : env->regs[15];
1334 bool same_el = (arm_debug_target_el(env) == arm_current_el(env));
1336 /* (1) GDB breakpoints should be handled first.
1337 * (2) Do not raise a CPU exception if no CPU breakpoint has fired,
1338 * since singlestep is also done by generating a debug internal
1339 * exception.
1341 if (cpu_breakpoint_test(cs, pc, BP_GDB)
1342 || !cpu_breakpoint_test(cs, pc, BP_CPU)) {
1343 return;
1346 if (extended_addresses_enabled(env)) {
1347 env->exception.fsr = (1 << 9) | 0x22;
1348 } else {
1349 env->exception.fsr = 0x2;
1351 /* FAR is UNKNOWN, so doesn't need setting */
1352 raise_exception(env, EXCP_PREFETCH_ABORT,
1353 syn_breakpoint(same_el),
1354 arm_debug_target_el(env));
1358 /* ??? Flag setting arithmetic is awkward because we need to do comparisons.
1359 The only way to do that in TCG is a conditional branch, which clobbers
1360 all our temporaries. For now implement these as helper functions. */
1362 /* Similarly for variable shift instructions. */
1364 uint32_t HELPER(shl_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1366 int shift = i & 0xff;
1367 if (shift >= 32) {
1368 if (shift == 32)
1369 env->CF = x & 1;
1370 else
1371 env->CF = 0;
1372 return 0;
1373 } else if (shift != 0) {
1374 env->CF = (x >> (32 - shift)) & 1;
1375 return x << shift;
1377 return x;
1380 uint32_t HELPER(shr_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1382 int shift = i & 0xff;
1383 if (shift >= 32) {
1384 if (shift == 32)
1385 env->CF = (x >> 31) & 1;
1386 else
1387 env->CF = 0;
1388 return 0;
1389 } else if (shift != 0) {
1390 env->CF = (x >> (shift - 1)) & 1;
1391 return x >> shift;
1393 return x;
1396 uint32_t HELPER(sar_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1398 int shift = i & 0xff;
1399 if (shift >= 32) {
1400 env->CF = (x >> 31) & 1;
1401 return (int32_t)x >> 31;
1402 } else if (shift != 0) {
1403 env->CF = (x >> (shift - 1)) & 1;
1404 return (int32_t)x >> shift;
1406 return x;
1409 uint32_t HELPER(ror_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1411 int shift1, shift;
1412 shift1 = i & 0xff;
1413 shift = shift1 & 0x1f;
1414 if (shift == 0) {
1415 if (shift1 != 0)
1416 env->CF = (x >> 31) & 1;
1417 return x;
1418 } else {
1419 env->CF = (x >> (shift - 1)) & 1;
1420 return ((uint32_t)x >> shift) | (x << (32 - shift));