target-arm: change LDREX alignment checks to use existing MO_ALIGN mechanism
[qemu/ar7.git] / target-arm / op_helper.c
blobc6995cabdcbaf524beff67a691705b3b832256e9
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 "cpu.h"
20 #include "exec/helper-proto.h"
21 #include "internals.h"
22 #include "exec/cpu_ldst.h"
24 #define SIGNBIT (uint32_t)0x80000000
25 #define SIGNBIT64 ((uint64_t)1 << 63)
27 static void raise_exception(CPUARMState *env, uint32_t excp,
28 uint32_t syndrome, uint32_t target_el)
30 CPUState *cs = CPU(arm_env_get_cpu(env));
32 assert(!excp_is_internal(excp));
33 cs->exception_index = excp;
34 env->exception.syndrome = syndrome;
35 env->exception.target_el = target_el;
36 cpu_loop_exit(cs);
39 static int exception_target_el(CPUARMState *env)
41 int target_el = MAX(1, arm_current_el(env));
43 /* No such thing as secure EL1 if EL3 is aarch32, so update the target EL
44 * to EL3 in this case.
46 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3) && target_el == 1) {
47 target_el = 3;
50 return target_el;
53 uint32_t HELPER(neon_tbl)(CPUARMState *env, uint32_t ireg, uint32_t def,
54 uint32_t rn, uint32_t maxindex)
56 uint32_t val;
57 uint32_t tmp;
58 int index;
59 int shift;
60 uint64_t *table;
61 table = (uint64_t *)&env->vfp.regs[rn];
62 val = 0;
63 for (shift = 0; shift < 32; shift += 8) {
64 index = (ireg >> shift) & 0xff;
65 if (index < maxindex) {
66 tmp = (table[index >> 3] >> ((index & 7) << 3)) & 0xff;
67 val |= tmp << shift;
68 } else {
69 val |= def & (0xff << shift);
72 return val;
75 #if !defined(CONFIG_USER_ONLY)
77 /* try to fill the TLB and return an exception if error. If retaddr is
78 * NULL, it means that the function was called in C code (i.e. not
79 * from generated code or from helper.c)
81 void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
82 uintptr_t retaddr)
84 bool ret;
85 uint32_t fsr = 0;
86 ARMMMUFaultInfo fi = {};
88 ret = arm_tlb_fill(cs, addr, is_write, mmu_idx, &fsr, &fi);
89 if (unlikely(ret)) {
90 ARMCPU *cpu = ARM_CPU(cs);
91 CPUARMState *env = &cpu->env;
92 uint32_t syn, exc;
93 unsigned int target_el;
94 bool same_el;
96 if (retaddr) {
97 /* now we have a real cpu fault */
98 cpu_restore_state(cs, retaddr);
101 target_el = exception_target_el(env);
102 if (fi.stage2) {
103 target_el = 2;
104 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4;
106 same_el = arm_current_el(env) == target_el;
107 /* AArch64 syndrome does not have an LPAE bit */
108 syn = fsr & ~(1 << 9);
110 /* For insn and data aborts we assume there is no instruction syndrome
111 * information; this is always true for exceptions reported to EL1.
113 if (is_write == 2) {
114 syn = syn_insn_abort(same_el, 0, fi.s1ptw, syn);
115 exc = EXCP_PREFETCH_ABORT;
116 } else {
117 syn = syn_data_abort(same_el, 0, 0, fi.s1ptw, is_write == 1, syn);
118 if (is_write == 1 && arm_feature(env, ARM_FEATURE_V6)) {
119 fsr |= (1 << 11);
121 exc = EXCP_DATA_ABORT;
124 env->exception.vaddress = addr;
125 env->exception.fsr = fsr;
126 raise_exception(env, exc, syn, target_el);
130 /* Raise a data fault alignment exception for the specified virtual address */
131 void arm_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr, int is_write,
132 int is_user, uintptr_t retaddr)
134 ARMCPU *cpu = ARM_CPU(cs);
135 CPUARMState *env = &cpu->env;
136 int target_el;
137 bool same_el;
139 if (retaddr) {
140 /* now we have a real cpu fault */
141 cpu_restore_state(cs, retaddr);
144 target_el = exception_target_el(env);
145 same_el = (arm_current_el(env) == target_el);
147 env->exception.vaddress = vaddr;
149 /* the DFSR for an alignment fault depends on whether we're using
150 * the LPAE long descriptor format, or the short descriptor format */
151 if (arm_regime_using_lpae_format(env, cpu_mmu_index(env, false))) {
152 env->exception.fsr = 0x21;
153 } else {
154 env->exception.fsr = 0x1;
157 raise_exception(env, EXCP_DATA_ABORT,
158 syn_data_abort(same_el, 0, 0, 0, 0, 0x21),
159 target_el);
162 #endif /* !defined(CONFIG_USER_ONLY) */
164 uint32_t HELPER(add_setq)(CPUARMState *env, uint32_t a, uint32_t b)
166 uint32_t res = a + b;
167 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT))
168 env->QF = 1;
169 return res;
172 uint32_t HELPER(add_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
174 uint32_t res = a + b;
175 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT)) {
176 env->QF = 1;
177 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
179 return res;
182 uint32_t HELPER(sub_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
184 uint32_t res = a - b;
185 if (((res ^ a) & SIGNBIT) && ((a ^ b) & SIGNBIT)) {
186 env->QF = 1;
187 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
189 return res;
192 uint32_t HELPER(double_saturate)(CPUARMState *env, int32_t val)
194 uint32_t res;
195 if (val >= 0x40000000) {
196 res = ~SIGNBIT;
197 env->QF = 1;
198 } else if (val <= (int32_t)0xc0000000) {
199 res = SIGNBIT;
200 env->QF = 1;
201 } else {
202 res = val << 1;
204 return res;
207 uint32_t HELPER(add_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
209 uint32_t res = a + b;
210 if (res < a) {
211 env->QF = 1;
212 res = ~0;
214 return res;
217 uint32_t HELPER(sub_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
219 uint32_t res = a - b;
220 if (res > a) {
221 env->QF = 1;
222 res = 0;
224 return res;
227 /* Signed saturation. */
228 static inline uint32_t do_ssat(CPUARMState *env, int32_t val, int shift)
230 int32_t top;
231 uint32_t mask;
233 top = val >> shift;
234 mask = (1u << shift) - 1;
235 if (top > 0) {
236 env->QF = 1;
237 return mask;
238 } else if (top < -1) {
239 env->QF = 1;
240 return ~mask;
242 return val;
245 /* Unsigned saturation. */
246 static inline uint32_t do_usat(CPUARMState *env, int32_t val, int shift)
248 uint32_t max;
250 max = (1u << shift) - 1;
251 if (val < 0) {
252 env->QF = 1;
253 return 0;
254 } else if (val > max) {
255 env->QF = 1;
256 return max;
258 return val;
261 /* Signed saturate. */
262 uint32_t HELPER(ssat)(CPUARMState *env, uint32_t x, uint32_t shift)
264 return do_ssat(env, x, shift);
267 /* Dual halfword signed saturate. */
268 uint32_t HELPER(ssat16)(CPUARMState *env, uint32_t x, uint32_t shift)
270 uint32_t res;
272 res = (uint16_t)do_ssat(env, (int16_t)x, shift);
273 res |= do_ssat(env, ((int32_t)x) >> 16, shift) << 16;
274 return res;
277 /* Unsigned saturate. */
278 uint32_t HELPER(usat)(CPUARMState *env, uint32_t x, uint32_t shift)
280 return do_usat(env, x, shift);
283 /* Dual halfword unsigned saturate. */
284 uint32_t HELPER(usat16)(CPUARMState *env, uint32_t x, uint32_t shift)
286 uint32_t res;
288 res = (uint16_t)do_usat(env, (int16_t)x, shift);
289 res |= do_usat(env, ((int32_t)x) >> 16, shift) << 16;
290 return res;
293 /* Function checks whether WFx (WFI/WFE) instructions are set up to be trapped.
294 * The function returns the target EL (1-3) if the instruction is to be trapped;
295 * otherwise it returns 0 indicating it is not trapped.
297 static inline int check_wfx_trap(CPUARMState *env, bool is_wfe)
299 int cur_el = arm_current_el(env);
300 uint64_t mask;
302 /* If we are currently in EL0 then we need to check if SCTLR is set up for
303 * WFx instructions being trapped to EL1. These trap bits don't exist in v7.
305 if (cur_el < 1 && arm_feature(env, ARM_FEATURE_V8)) {
306 int target_el;
308 mask = is_wfe ? SCTLR_nTWE : SCTLR_nTWI;
309 if (arm_is_secure_below_el3(env) && !arm_el_is_aa64(env, 3)) {
310 /* Secure EL0 and Secure PL1 is at EL3 */
311 target_el = 3;
312 } else {
313 target_el = 1;
316 if (!(env->cp15.sctlr_el[target_el] & mask)) {
317 return target_el;
321 /* We are not trapping to EL1; trap to EL2 if HCR_EL2 requires it
322 * No need for ARM_FEATURE check as if HCR_EL2 doesn't exist the
323 * bits will be zero indicating no trap.
325 if (cur_el < 2 && !arm_is_secure(env)) {
326 mask = (is_wfe) ? HCR_TWE : HCR_TWI;
327 if (env->cp15.hcr_el2 & mask) {
328 return 2;
332 /* We are not trapping to EL1 or EL2; trap to EL3 if SCR_EL3 requires it */
333 if (cur_el < 3) {
334 mask = (is_wfe) ? SCR_TWE : SCR_TWI;
335 if (env->cp15.scr_el3 & mask) {
336 return 3;
340 return 0;
343 void HELPER(wfi)(CPUARMState *env)
345 CPUState *cs = CPU(arm_env_get_cpu(env));
346 int target_el = check_wfx_trap(env, false);
348 if (cpu_has_work(cs)) {
349 /* Don't bother to go into our "low power state" if
350 * we would just wake up immediately.
352 return;
355 if (target_el) {
356 env->pc -= 4;
357 raise_exception(env, EXCP_UDEF, syn_wfx(1, 0xe, 0), target_el);
360 cs->exception_index = EXCP_HLT;
361 cs->halted = 1;
362 cpu_loop_exit(cs);
365 void HELPER(wfe)(CPUARMState *env)
367 /* This is a hint instruction that is semantically different
368 * from YIELD even though we currently implement it identically.
369 * Don't actually halt the CPU, just yield back to top
370 * level loop. This is not going into a "low power state"
371 * (ie halting until some event occurs), so we never take
372 * a configurable trap to a different exception level.
374 HELPER(yield)(env);
377 void HELPER(yield)(CPUARMState *env)
379 ARMCPU *cpu = arm_env_get_cpu(env);
380 CPUState *cs = CPU(cpu);
382 /* This is a non-trappable hint instruction that generally indicates
383 * that the guest is currently busy-looping. Yield control back to the
384 * top level loop so that a more deserving VCPU has a chance to run.
386 cs->exception_index = EXCP_YIELD;
387 cpu_loop_exit(cs);
390 /* Raise an internal-to-QEMU exception. This is limited to only
391 * those EXCP values which are special cases for QEMU to interrupt
392 * execution and not to be used for exceptions which are passed to
393 * the guest (those must all have syndrome information and thus should
394 * use exception_with_syndrome).
396 void HELPER(exception_internal)(CPUARMState *env, uint32_t excp)
398 CPUState *cs = CPU(arm_env_get_cpu(env));
400 assert(excp_is_internal(excp));
401 cs->exception_index = excp;
402 cpu_loop_exit(cs);
405 /* Raise an exception with the specified syndrome register value */
406 void HELPER(exception_with_syndrome)(CPUARMState *env, uint32_t excp,
407 uint32_t syndrome, uint32_t target_el)
409 raise_exception(env, excp, syndrome, target_el);
412 uint32_t HELPER(cpsr_read)(CPUARMState *env)
414 return cpsr_read(env) & ~(CPSR_EXEC | CPSR_RESERVED);
417 void HELPER(cpsr_write)(CPUARMState *env, uint32_t val, uint32_t mask)
419 cpsr_write(env, val, mask);
422 /* Access to user mode registers from privileged modes. */
423 uint32_t HELPER(get_user_reg)(CPUARMState *env, uint32_t regno)
425 uint32_t val;
427 if (regno == 13) {
428 val = env->banked_r13[BANK_USRSYS];
429 } else if (regno == 14) {
430 val = env->banked_r14[BANK_USRSYS];
431 } else if (regno >= 8
432 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
433 val = env->usr_regs[regno - 8];
434 } else {
435 val = env->regs[regno];
437 return val;
440 void HELPER(set_user_reg)(CPUARMState *env, uint32_t regno, uint32_t val)
442 if (regno == 13) {
443 env->banked_r13[BANK_USRSYS] = val;
444 } else if (regno == 14) {
445 env->banked_r14[BANK_USRSYS] = val;
446 } else if (regno >= 8
447 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
448 env->usr_regs[regno - 8] = val;
449 } else {
450 env->regs[regno] = val;
454 void HELPER(access_check_cp_reg)(CPUARMState *env, void *rip, uint32_t syndrome)
456 const ARMCPRegInfo *ri = rip;
457 int target_el;
459 if (arm_feature(env, ARM_FEATURE_XSCALE) && ri->cp < 14
460 && extract32(env->cp15.c15_cpar, ri->cp, 1) == 0) {
461 raise_exception(env, EXCP_UDEF, syndrome, exception_target_el(env));
464 if (!ri->accessfn) {
465 return;
468 switch (ri->accessfn(env, ri)) {
469 case CP_ACCESS_OK:
470 return;
471 case CP_ACCESS_TRAP:
472 target_el = exception_target_el(env);
473 break;
474 case CP_ACCESS_TRAP_EL2:
475 /* Requesting a trap to EL2 when we're in EL3 or S-EL0/1 is
476 * a bug in the access function.
478 assert(!arm_is_secure(env) && arm_current_el(env) != 3);
479 target_el = 2;
480 break;
481 case CP_ACCESS_TRAP_EL3:
482 target_el = 3;
483 break;
484 case CP_ACCESS_TRAP_UNCATEGORIZED:
485 target_el = exception_target_el(env);
486 syndrome = syn_uncategorized();
487 break;
488 case CP_ACCESS_TRAP_UNCATEGORIZED_EL2:
489 target_el = 2;
490 syndrome = syn_uncategorized();
491 break;
492 case CP_ACCESS_TRAP_UNCATEGORIZED_EL3:
493 target_el = 3;
494 syndrome = syn_uncategorized();
495 break;
496 default:
497 g_assert_not_reached();
500 raise_exception(env, EXCP_UDEF, syndrome, target_el);
503 void HELPER(set_cp_reg)(CPUARMState *env, void *rip, uint32_t value)
505 const ARMCPRegInfo *ri = rip;
507 ri->writefn(env, ri, value);
510 uint32_t HELPER(get_cp_reg)(CPUARMState *env, void *rip)
512 const ARMCPRegInfo *ri = rip;
514 return ri->readfn(env, ri);
517 void HELPER(set_cp_reg64)(CPUARMState *env, void *rip, uint64_t value)
519 const ARMCPRegInfo *ri = rip;
521 ri->writefn(env, ri, value);
524 uint64_t HELPER(get_cp_reg64)(CPUARMState *env, void *rip)
526 const ARMCPRegInfo *ri = rip;
528 return ri->readfn(env, ri);
531 void HELPER(msr_i_pstate)(CPUARMState *env, uint32_t op, uint32_t imm)
533 /* MSR_i to update PSTATE. This is OK from EL0 only if UMA is set.
534 * Note that SPSel is never OK from EL0; we rely on handle_msr_i()
535 * to catch that case at translate time.
537 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
538 uint32_t syndrome = syn_aa64_sysregtrap(0, extract32(op, 0, 3),
539 extract32(op, 3, 3), 4,
540 imm, 0x1f, 0);
541 raise_exception(env, EXCP_UDEF, syndrome, exception_target_el(env));
544 switch (op) {
545 case 0x05: /* SPSel */
546 update_spsel(env, imm);
547 break;
548 case 0x1e: /* DAIFSet */
549 env->daif |= (imm << 6) & PSTATE_DAIF;
550 break;
551 case 0x1f: /* DAIFClear */
552 env->daif &= ~((imm << 6) & PSTATE_DAIF);
553 break;
554 default:
555 g_assert_not_reached();
559 void HELPER(clear_pstate_ss)(CPUARMState *env)
561 env->pstate &= ~PSTATE_SS;
564 void HELPER(pre_hvc)(CPUARMState *env)
566 ARMCPU *cpu = arm_env_get_cpu(env);
567 int cur_el = arm_current_el(env);
568 /* FIXME: Use actual secure state. */
569 bool secure = false;
570 bool undef;
572 if (arm_is_psci_call(cpu, EXCP_HVC)) {
573 /* If PSCI is enabled and this looks like a valid PSCI call then
574 * that overrides the architecturally mandated HVC behaviour.
576 return;
579 if (!arm_feature(env, ARM_FEATURE_EL2)) {
580 /* If EL2 doesn't exist, HVC always UNDEFs */
581 undef = true;
582 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
583 /* EL3.HCE has priority over EL2.HCD. */
584 undef = !(env->cp15.scr_el3 & SCR_HCE);
585 } else {
586 undef = env->cp15.hcr_el2 & HCR_HCD;
589 /* In ARMv7 and ARMv8/AArch32, HVC is undef in secure state.
590 * For ARMv8/AArch64, HVC is allowed in EL3.
591 * Note that we've already trapped HVC from EL0 at translation
592 * time.
594 if (secure && (!is_a64(env) || cur_el == 1)) {
595 undef = true;
598 if (undef) {
599 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
600 exception_target_el(env));
604 void HELPER(pre_smc)(CPUARMState *env, uint32_t syndrome)
606 ARMCPU *cpu = arm_env_get_cpu(env);
607 int cur_el = arm_current_el(env);
608 bool secure = arm_is_secure(env);
609 bool smd = env->cp15.scr_el3 & SCR_SMD;
610 /* On ARMv8 AArch32, SMD only applies to NS state.
611 * On ARMv7 SMD only applies to NS state and only if EL2 is available.
612 * For ARMv7 non EL2, we force SMD to zero so we don't need to re-check
613 * the EL2 condition here.
615 bool undef = is_a64(env) ? smd : (!secure && smd);
617 if (arm_is_psci_call(cpu, EXCP_SMC)) {
618 /* If PSCI is enabled and this looks like a valid PSCI call then
619 * that overrides the architecturally mandated SMC behaviour.
621 return;
624 if (!arm_feature(env, ARM_FEATURE_EL3)) {
625 /* If we have no EL3 then SMC always UNDEFs */
626 undef = true;
627 } else if (!secure && cur_el == 1 && (env->cp15.hcr_el2 & HCR_TSC)) {
628 /* In NS EL1, HCR controlled routing to EL2 has priority over SMD. */
629 raise_exception(env, EXCP_HYP_TRAP, syndrome, 2);
632 if (undef) {
633 raise_exception(env, EXCP_UDEF, syn_uncategorized(),
634 exception_target_el(env));
638 void HELPER(exception_return)(CPUARMState *env)
640 int cur_el = arm_current_el(env);
641 unsigned int spsr_idx = aarch64_banked_spsr_index(cur_el);
642 uint32_t spsr = env->banked_spsr[spsr_idx];
643 int new_el;
645 aarch64_save_sp(env, cur_el);
647 env->exclusive_addr = -1;
649 /* We must squash the PSTATE.SS bit to zero unless both of the
650 * following hold:
651 * 1. debug exceptions are currently disabled
652 * 2. singlestep will be active in the EL we return to
653 * We check 1 here and 2 after we've done the pstate/cpsr write() to
654 * transition to the EL we're going to.
656 if (arm_generate_debug_exceptions(env)) {
657 spsr &= ~PSTATE_SS;
660 if (spsr & PSTATE_nRW) {
661 /* TODO: We currently assume EL1/2/3 are running in AArch64. */
662 env->aarch64 = 0;
663 new_el = 0;
664 env->uncached_cpsr = 0x10;
665 cpsr_write(env, spsr, ~0);
666 if (!arm_singlestep_active(env)) {
667 env->uncached_cpsr &= ~PSTATE_SS;
669 aarch64_sync_64_to_32(env);
671 env->regs[15] = env->elr_el[1] & ~0x1;
672 } else {
673 new_el = extract32(spsr, 2, 2);
674 if (new_el > cur_el
675 || (new_el == 2 && !arm_feature(env, ARM_FEATURE_EL2))) {
676 /* Disallow return to an EL which is unimplemented or higher
677 * than the current one.
679 goto illegal_return;
681 if (extract32(spsr, 1, 1)) {
682 /* Return with reserved M[1] bit set */
683 goto illegal_return;
685 if (new_el == 0 && (spsr & PSTATE_SP)) {
686 /* Return to EL0 with M[0] bit set */
687 goto illegal_return;
689 env->aarch64 = 1;
690 pstate_write(env, spsr);
691 if (!arm_singlestep_active(env)) {
692 env->pstate &= ~PSTATE_SS;
694 aarch64_restore_sp(env, new_el);
695 env->pc = env->elr_el[cur_el];
698 return;
700 illegal_return:
701 /* Illegal return events of various kinds have architecturally
702 * mandated behaviour:
703 * restore NZCV and DAIF from SPSR_ELx
704 * set PSTATE.IL
705 * restore PC from ELR_ELx
706 * no change to exception level, execution state or stack pointer
708 env->pstate |= PSTATE_IL;
709 env->pc = env->elr_el[cur_el];
710 spsr &= PSTATE_NZCV | PSTATE_DAIF;
711 spsr |= pstate_read(env) & ~(PSTATE_NZCV | PSTATE_DAIF);
712 pstate_write(env, spsr);
713 if (!arm_singlestep_active(env)) {
714 env->pstate &= ~PSTATE_SS;
718 /* Return true if the linked breakpoint entry lbn passes its checks */
719 static bool linked_bp_matches(ARMCPU *cpu, int lbn)
721 CPUARMState *env = &cpu->env;
722 uint64_t bcr = env->cp15.dbgbcr[lbn];
723 int brps = extract32(cpu->dbgdidr, 24, 4);
724 int ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
725 int bt;
726 uint32_t contextidr;
728 /* Links to unimplemented or non-context aware breakpoints are
729 * CONSTRAINED UNPREDICTABLE: either behave as if disabled, or
730 * as if linked to an UNKNOWN context-aware breakpoint (in which
731 * case DBGWCR<n>_EL1.LBN must indicate that breakpoint).
732 * We choose the former.
734 if (lbn > brps || lbn < (brps - ctx_cmps)) {
735 return false;
738 bcr = env->cp15.dbgbcr[lbn];
740 if (extract64(bcr, 0, 1) == 0) {
741 /* Linked breakpoint disabled : generate no events */
742 return false;
745 bt = extract64(bcr, 20, 4);
747 /* We match the whole register even if this is AArch32 using the
748 * short descriptor format (in which case it holds both PROCID and ASID),
749 * since we don't implement the optional v7 context ID masking.
751 contextidr = extract64(env->cp15.contextidr_el[1], 0, 32);
753 switch (bt) {
754 case 3: /* linked context ID match */
755 if (arm_current_el(env) > 1) {
756 /* Context matches never fire in EL2 or (AArch64) EL3 */
757 return false;
759 return (contextidr == extract64(env->cp15.dbgbvr[lbn], 0, 32));
760 case 5: /* linked address mismatch (reserved in AArch64) */
761 case 9: /* linked VMID match (reserved if no EL2) */
762 case 11: /* linked context ID and VMID match (reserved if no EL2) */
763 default:
764 /* Links to Unlinked context breakpoints must generate no
765 * events; we choose to do the same for reserved values too.
767 return false;
770 return false;
773 static bool bp_wp_matches(ARMCPU *cpu, int n, bool is_wp)
775 CPUARMState *env = &cpu->env;
776 uint64_t cr;
777 int pac, hmc, ssc, wt, lbn;
778 /* Note that for watchpoints the check is against the CPU security
779 * state, not the S/NS attribute on the offending data access.
781 bool is_secure = arm_is_secure(env);
782 int access_el = arm_current_el(env);
784 if (is_wp) {
785 CPUWatchpoint *wp = env->cpu_watchpoint[n];
787 if (!wp || !(wp->flags & BP_WATCHPOINT_HIT)) {
788 return false;
790 cr = env->cp15.dbgwcr[n];
791 if (wp->hitattrs.user) {
792 /* The LDRT/STRT/LDT/STT "unprivileged access" instructions should
793 * match watchpoints as if they were accesses done at EL0, even if
794 * the CPU is at EL1 or higher.
796 access_el = 0;
798 } else {
799 uint64_t pc = is_a64(env) ? env->pc : env->regs[15];
801 if (!env->cpu_breakpoint[n] || env->cpu_breakpoint[n]->pc != pc) {
802 return false;
804 cr = env->cp15.dbgbcr[n];
806 /* The WATCHPOINT_HIT flag guarantees us that the watchpoint is
807 * enabled and that the address and access type match; for breakpoints
808 * we know the address matched; check the remaining fields, including
809 * linked breakpoints. We rely on WCR and BCR having the same layout
810 * for the LBN, SSC, HMC, PAC/PMC and is-linked fields.
811 * Note that some combinations of {PAC, HMC, SSC} are reserved and
812 * must act either like some valid combination or as if the watchpoint
813 * were disabled. We choose the former, and use this together with
814 * the fact that EL3 must always be Secure and EL2 must always be
815 * Non-Secure to simplify the code slightly compared to the full
816 * table in the ARM ARM.
818 pac = extract64(cr, 1, 2);
819 hmc = extract64(cr, 13, 1);
820 ssc = extract64(cr, 14, 2);
822 switch (ssc) {
823 case 0:
824 break;
825 case 1:
826 case 3:
827 if (is_secure) {
828 return false;
830 break;
831 case 2:
832 if (!is_secure) {
833 return false;
835 break;
838 switch (access_el) {
839 case 3:
840 case 2:
841 if (!hmc) {
842 return false;
844 break;
845 case 1:
846 if (extract32(pac, 0, 1) == 0) {
847 return false;
849 break;
850 case 0:
851 if (extract32(pac, 1, 1) == 0) {
852 return false;
854 break;
855 default:
856 g_assert_not_reached();
859 wt = extract64(cr, 20, 1);
860 lbn = extract64(cr, 16, 4);
862 if (wt && !linked_bp_matches(cpu, lbn)) {
863 return false;
866 return true;
869 static bool check_watchpoints(ARMCPU *cpu)
871 CPUARMState *env = &cpu->env;
872 int n;
874 /* If watchpoints are disabled globally or we can't take debug
875 * exceptions here then watchpoint firings are ignored.
877 if (extract32(env->cp15.mdscr_el1, 15, 1) == 0
878 || !arm_generate_debug_exceptions(env)) {
879 return false;
882 for (n = 0; n < ARRAY_SIZE(env->cpu_watchpoint); n++) {
883 if (bp_wp_matches(cpu, n, true)) {
884 return true;
887 return false;
890 static bool check_breakpoints(ARMCPU *cpu)
892 CPUARMState *env = &cpu->env;
893 int n;
895 /* If breakpoints are disabled globally or we can't take debug
896 * exceptions here then breakpoint firings are ignored.
898 if (extract32(env->cp15.mdscr_el1, 15, 1) == 0
899 || !arm_generate_debug_exceptions(env)) {
900 return false;
903 for (n = 0; n < ARRAY_SIZE(env->cpu_breakpoint); n++) {
904 if (bp_wp_matches(cpu, n, false)) {
905 return true;
908 return false;
911 void HELPER(check_breakpoints)(CPUARMState *env)
913 ARMCPU *cpu = arm_env_get_cpu(env);
915 if (check_breakpoints(cpu)) {
916 HELPER(exception_internal(env, EXCP_DEBUG));
920 void arm_debug_excp_handler(CPUState *cs)
922 /* Called by core code when a watchpoint or breakpoint fires;
923 * need to check which one and raise the appropriate exception.
925 ARMCPU *cpu = ARM_CPU(cs);
926 CPUARMState *env = &cpu->env;
927 CPUWatchpoint *wp_hit = cs->watchpoint_hit;
929 if (wp_hit) {
930 if (wp_hit->flags & BP_CPU) {
931 cs->watchpoint_hit = NULL;
932 if (check_watchpoints(cpu)) {
933 bool wnr = (wp_hit->flags & BP_WATCHPOINT_HIT_WRITE) != 0;
934 bool same_el = arm_debug_target_el(env) == arm_current_el(env);
936 if (extended_addresses_enabled(env)) {
937 env->exception.fsr = (1 << 9) | 0x22;
938 } else {
939 env->exception.fsr = 0x2;
941 env->exception.vaddress = wp_hit->hitaddr;
942 raise_exception(env, EXCP_DATA_ABORT,
943 syn_watchpoint(same_el, 0, wnr),
944 arm_debug_target_el(env));
945 } else {
946 cpu_resume_from_signal(cs, NULL);
949 } else {
950 uint64_t pc = is_a64(env) ? env->pc : env->regs[15];
951 bool same_el = (arm_debug_target_el(env) == arm_current_el(env));
953 /* (1) GDB breakpoints should be handled first.
954 * (2) Do not raise a CPU exception if no CPU breakpoint has fired,
955 * since singlestep is also done by generating a debug internal
956 * exception.
958 if (cpu_breakpoint_test(cs, pc, BP_GDB)
959 || !cpu_breakpoint_test(cs, pc, BP_CPU)) {
960 return;
963 if (extended_addresses_enabled(env)) {
964 env->exception.fsr = (1 << 9) | 0x22;
965 } else {
966 env->exception.fsr = 0x2;
968 /* FAR is UNKNOWN, so doesn't need setting */
969 raise_exception(env, EXCP_PREFETCH_ABORT,
970 syn_breakpoint(same_el),
971 arm_debug_target_el(env));
975 /* ??? Flag setting arithmetic is awkward because we need to do comparisons.
976 The only way to do that in TCG is a conditional branch, which clobbers
977 all our temporaries. For now implement these as helper functions. */
979 /* Similarly for variable shift instructions. */
981 uint32_t HELPER(shl_cc)(CPUARMState *env, uint32_t x, uint32_t i)
983 int shift = i & 0xff;
984 if (shift >= 32) {
985 if (shift == 32)
986 env->CF = x & 1;
987 else
988 env->CF = 0;
989 return 0;
990 } else if (shift != 0) {
991 env->CF = (x >> (32 - shift)) & 1;
992 return x << shift;
994 return x;
997 uint32_t HELPER(shr_cc)(CPUARMState *env, uint32_t x, uint32_t i)
999 int shift = i & 0xff;
1000 if (shift >= 32) {
1001 if (shift == 32)
1002 env->CF = (x >> 31) & 1;
1003 else
1004 env->CF = 0;
1005 return 0;
1006 } else if (shift != 0) {
1007 env->CF = (x >> (shift - 1)) & 1;
1008 return x >> shift;
1010 return x;
1013 uint32_t HELPER(sar_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1015 int shift = i & 0xff;
1016 if (shift >= 32) {
1017 env->CF = (x >> 31) & 1;
1018 return (int32_t)x >> 31;
1019 } else if (shift != 0) {
1020 env->CF = (x >> (shift - 1)) & 1;
1021 return (int32_t)x >> shift;
1023 return x;
1026 uint32_t HELPER(ror_cc)(CPUARMState *env, uint32_t x, uint32_t i)
1028 int shift1, shift;
1029 shift1 = i & 0xff;
1030 shift = shift1 & 0x1f;
1031 if (shift == 0) {
1032 if (shift1 != 0)
1033 env->CF = (x >> 31) & 1;
1034 return x;
1035 } else {
1036 env->CF = (x >> (shift - 1)) & 1;
1037 return ((uint32_t)x >> shift) | (x << (32 - shift));