4 * This code is licensed under the GNU GPL v2 or later.
6 * SPDX-License-Identifier: GPL-2.0-or-later
8 #include "qemu/osdep.h"
11 #include "internals.h"
13 #include "exec/exec-all.h"
14 #include "exec/helper-proto.h"
15 #include "sysemu/tcg.h"
18 /* Return the Exception Level targeted by debug exceptions. */
19 static int arm_debug_target_el(CPUARMState
*env
)
21 bool secure
= arm_is_secure(env
);
22 bool route_to_el2
= false;
24 if (arm_is_el2_enabled(env
)) {
25 route_to_el2
= env
->cp15
.hcr_el2
& HCR_TGE
||
26 env
->cp15
.mdcr_el2
& MDCR_TDE
;
31 } else if (arm_feature(env
, ARM_FEATURE_EL3
) &&
32 !arm_el_is_aa64(env
, 3) && secure
) {
40 * Raise an exception to the debug target el.
41 * Modify syndrome to indicate when origin and target EL are the same.
43 G_NORETURN
static void
44 raise_exception_debug(CPUARMState
*env
, uint32_t excp
, uint32_t syndrome
)
46 int debug_el
= arm_debug_target_el(env
);
47 int cur_el
= arm_current_el(env
);
50 * If singlestep is targeting a lower EL than the current one, then
51 * DisasContext.ss_active must be false and we can never get here.
52 * Similarly for watchpoint and breakpoint matches.
54 assert(debug_el
>= cur_el
);
55 syndrome
|= (debug_el
== cur_el
) << ARM_EL_EC_SHIFT
;
56 raise_exception(env
, excp
, syndrome
, debug_el
);
59 /* See AArch64.GenerateDebugExceptionsFrom() in ARM ARM pseudocode */
60 static bool aa64_generate_debug_exceptions(CPUARMState
*env
)
62 int cur_el
= arm_current_el(env
);
69 /* MDCR_EL3.SDD disables debug events from Secure state */
70 if (arm_is_secure_below_el3(env
)
71 && extract32(env
->cp15
.mdcr_el3
, 16, 1)) {
76 * Same EL to same EL debug exceptions need MDSCR_KDE enabled
77 * while not masking the (D)ebug bit in DAIF.
79 debug_el
= arm_debug_target_el(env
);
81 if (cur_el
== debug_el
) {
82 return extract32(env
->cp15
.mdscr_el1
, 13, 1)
83 && !(env
->daif
& PSTATE_D
);
86 /* Otherwise the debug target needs to be a higher EL */
87 return debug_el
> cur_el
;
90 static bool aa32_generate_debug_exceptions(CPUARMState
*env
)
92 int el
= arm_current_el(env
);
94 if (el
== 0 && arm_el_is_aa64(env
, 1)) {
95 return aa64_generate_debug_exceptions(env
);
98 if (arm_is_secure(env
)) {
101 if (el
== 0 && (env
->cp15
.sder
& 1)) {
103 * SDER.SUIDEN means debug exceptions from Secure EL0
104 * are always enabled. Otherwise they are controlled by
105 * SDCR.SPD like those from other Secure ELs.
110 spd
= extract32(env
->cp15
.mdcr_el3
, 14, 2);
113 /* SPD == 0b01 is reserved, but behaves as 0b00. */
116 * For 0b00 we return true if external secure invasive debug
117 * is enabled. On real hardware this is controlled by external
118 * signals to the core. QEMU always permits debug, and behaves
119 * as if DBGEN, SPIDEN, NIDEN and SPNIDEN are all tied high.
133 * Return true if debugging exceptions are currently enabled.
134 * This corresponds to what in ARM ARM pseudocode would be
135 * if UsingAArch32() then
136 * return AArch32.GenerateDebugExceptions()
138 * return AArch64.GenerateDebugExceptions()
139 * We choose to push the if() down into this function for clarity,
140 * since the pseudocode has it at all callsites except for the one in
141 * CheckSoftwareStep(), where it is elided because both branches would
142 * always return the same value.
144 bool arm_generate_debug_exceptions(CPUARMState
*env
)
146 if ((env
->cp15
.oslsr_el1
& 1) || (env
->cp15
.osdlr_el1
& 1)) {
150 return aa64_generate_debug_exceptions(env
);
152 return aa32_generate_debug_exceptions(env
);
157 * Is single-stepping active? (Note that the "is EL_D AArch64?" check
158 * implicitly means this always returns false in pre-v8 CPUs.)
160 bool arm_singlestep_active(CPUARMState
*env
)
162 return extract32(env
->cp15
.mdscr_el1
, 0, 1)
163 && arm_el_is_aa64(env
, arm_debug_target_el(env
))
164 && arm_generate_debug_exceptions(env
);
167 /* Return true if the linked breakpoint entry lbn passes its checks */
168 static bool linked_bp_matches(ARMCPU
*cpu
, int lbn
)
170 CPUARMState
*env
= &cpu
->env
;
171 uint64_t bcr
= env
->cp15
.dbgbcr
[lbn
];
172 int brps
= arm_num_brps(cpu
);
173 int ctx_cmps
= arm_num_ctx_cmps(cpu
);
179 * Links to unimplemented or non-context aware breakpoints are
180 * CONSTRAINED UNPREDICTABLE: either behave as if disabled, or
181 * as if linked to an UNKNOWN context-aware breakpoint (in which
182 * case DBGWCR<n>_EL1.LBN must indicate that breakpoint).
183 * We choose the former.
185 if (lbn
>= brps
|| lbn
< (brps
- ctx_cmps
)) {
189 bcr
= env
->cp15
.dbgbcr
[lbn
];
191 if (extract64(bcr
, 0, 1) == 0) {
192 /* Linked breakpoint disabled : generate no events */
196 bt
= extract64(bcr
, 20, 4);
197 hcr_el2
= arm_hcr_el2_eff(env
);
200 case 3: /* linked context ID match */
201 switch (arm_current_el(env
)) {
203 /* Context matches never fire in AArch64 EL3 */
206 if (!(hcr_el2
& HCR_E2H
)) {
207 /* Context matches never fire in EL2 without E2H enabled. */
210 contextidr
= env
->cp15
.contextidr_el
[2];
213 contextidr
= env
->cp15
.contextidr_el
[1];
216 if ((hcr_el2
& (HCR_E2H
| HCR_TGE
)) == (HCR_E2H
| HCR_TGE
)) {
217 contextidr
= env
->cp15
.contextidr_el
[2];
219 contextidr
= env
->cp15
.contextidr_el
[1];
225 case 7: /* linked contextidr_el1 match */
226 contextidr
= env
->cp15
.contextidr_el
[1];
228 case 13: /* linked contextidr_el2 match */
229 contextidr
= env
->cp15
.contextidr_el
[2];
232 case 9: /* linked VMID match (reserved if no EL2) */
233 case 11: /* linked context ID and VMID match (reserved if no EL2) */
234 case 15: /* linked full context ID match */
237 * Links to Unlinked context breakpoints must generate no
238 * events; we choose to do the same for reserved values too.
244 * We match the whole register even if this is AArch32 using the
245 * short descriptor format (in which case it holds both PROCID and ASID),
246 * since we don't implement the optional v7 context ID masking.
248 return contextidr
== (uint32_t)env
->cp15
.dbgbvr
[lbn
];
251 static bool bp_wp_matches(ARMCPU
*cpu
, int n
, bool is_wp
)
253 CPUARMState
*env
= &cpu
->env
;
255 int pac
, hmc
, ssc
, wt
, lbn
;
257 * Note that for watchpoints the check is against the CPU security
258 * state, not the S/NS attribute on the offending data access.
260 bool is_secure
= arm_is_secure(env
);
261 int access_el
= arm_current_el(env
);
264 CPUWatchpoint
*wp
= env
->cpu_watchpoint
[n
];
266 if (!wp
|| !(wp
->flags
& BP_WATCHPOINT_HIT
)) {
269 cr
= env
->cp15
.dbgwcr
[n
];
270 if (wp
->hitattrs
.user
) {
272 * The LDRT/STRT/LDT/STT "unprivileged access" instructions should
273 * match watchpoints as if they were accesses done at EL0, even if
274 * the CPU is at EL1 or higher.
279 uint64_t pc
= is_a64(env
) ? env
->pc
: env
->regs
[15];
281 if (!env
->cpu_breakpoint
[n
] || env
->cpu_breakpoint
[n
]->pc
!= pc
) {
284 cr
= env
->cp15
.dbgbcr
[n
];
287 * The WATCHPOINT_HIT flag guarantees us that the watchpoint is
288 * enabled and that the address and access type match; for breakpoints
289 * we know the address matched; check the remaining fields, including
290 * linked breakpoints. We rely on WCR and BCR having the same layout
291 * for the LBN, SSC, HMC, PAC/PMC and is-linked fields.
292 * Note that some combinations of {PAC, HMC, SSC} are reserved and
293 * must act either like some valid combination or as if the watchpoint
294 * were disabled. We choose the former, and use this together with
295 * the fact that EL3 must always be Secure and EL2 must always be
296 * Non-Secure to simplify the code slightly compared to the full
297 * table in the ARM ARM.
299 pac
= FIELD_EX64(cr
, DBGWCR
, PAC
);
300 hmc
= FIELD_EX64(cr
, DBGWCR
, HMC
);
301 ssc
= FIELD_EX64(cr
, DBGWCR
, SSC
);
327 if (extract32(pac
, 0, 1) == 0) {
332 if (extract32(pac
, 1, 1) == 0) {
337 g_assert_not_reached();
340 wt
= FIELD_EX64(cr
, DBGWCR
, WT
);
341 lbn
= FIELD_EX64(cr
, DBGWCR
, LBN
);
343 if (wt
&& !linked_bp_matches(cpu
, lbn
)) {
350 static bool check_watchpoints(ARMCPU
*cpu
)
352 CPUARMState
*env
= &cpu
->env
;
356 * If watchpoints are disabled globally or we can't take debug
357 * exceptions here then watchpoint firings are ignored.
359 if (extract32(env
->cp15
.mdscr_el1
, 15, 1) == 0
360 || !arm_generate_debug_exceptions(env
)) {
364 for (n
= 0; n
< ARRAY_SIZE(env
->cpu_watchpoint
); n
++) {
365 if (bp_wp_matches(cpu
, n
, true)) {
372 bool arm_debug_check_breakpoint(CPUState
*cs
)
374 ARMCPU
*cpu
= ARM_CPU(cs
);
375 CPUARMState
*env
= &cpu
->env
;
380 * If breakpoints are disabled globally or we can't take debug
381 * exceptions here then breakpoint firings are ignored.
383 if (extract32(env
->cp15
.mdscr_el1
, 15, 1) == 0
384 || !arm_generate_debug_exceptions(env
)) {
389 * Single-step exceptions have priority over breakpoint exceptions.
390 * If single-step state is active-pending, suppress the bp.
392 if (arm_singlestep_active(env
) && !(env
->pstate
& PSTATE_SS
)) {
397 * PC alignment faults have priority over breakpoint exceptions.
399 pc
= is_a64(env
) ? env
->pc
: env
->regs
[15];
400 if ((is_a64(env
) || !env
->thumb
) && (pc
& 3) != 0) {
405 * Instruction aborts have priority over breakpoint exceptions.
406 * TODO: We would need to look up the page for PC and verify that
407 * it is present and executable.
410 for (n
= 0; n
< ARRAY_SIZE(env
->cpu_breakpoint
); n
++) {
411 if (bp_wp_matches(cpu
, n
, false)) {
418 bool arm_debug_check_watchpoint(CPUState
*cs
, CPUWatchpoint
*wp
)
421 * Called by core code when a CPU watchpoint fires; need to check if this
422 * is also an architectural watchpoint match.
424 ARMCPU
*cpu
= ARM_CPU(cs
);
426 return check_watchpoints(cpu
);
430 * Return the FSR value for a debug exception (watchpoint, hardware
431 * breakpoint or BKPT insn) targeting the specified exception level.
433 static uint32_t arm_debug_exception_fsr(CPUARMState
*env
)
435 ARMMMUFaultInfo fi
= { .type
= ARMFault_Debug
};
436 int target_el
= arm_debug_target_el(env
);
437 bool using_lpae
= false;
439 if (target_el
== 2 || arm_el_is_aa64(env
, target_el
)) {
441 } else if (arm_feature(env
, ARM_FEATURE_PMSA
) &&
442 arm_feature(env
, ARM_FEATURE_V8
)) {
445 if (arm_feature(env
, ARM_FEATURE_LPAE
) &&
446 (env
->cp15
.tcr_el
[target_el
] & TTBCR_EAE
)) {
452 return arm_fi_to_lfsc(&fi
);
454 return arm_fi_to_sfsc(&fi
);
458 void arm_debug_excp_handler(CPUState
*cs
)
461 * Called by core code when a watchpoint or breakpoint fires;
462 * need to check which one and raise the appropriate exception.
464 ARMCPU
*cpu
= ARM_CPU(cs
);
465 CPUARMState
*env
= &cpu
->env
;
466 CPUWatchpoint
*wp_hit
= cs
->watchpoint_hit
;
469 if (wp_hit
->flags
& BP_CPU
) {
470 bool wnr
= (wp_hit
->flags
& BP_WATCHPOINT_HIT_WRITE
) != 0;
472 cs
->watchpoint_hit
= NULL
;
474 env
->exception
.fsr
= arm_debug_exception_fsr(env
);
475 env
->exception
.vaddress
= wp_hit
->hitaddr
;
476 raise_exception_debug(env
, EXCP_DATA_ABORT
,
477 syn_watchpoint(0, 0, wnr
));
480 uint64_t pc
= is_a64(env
) ? env
->pc
: env
->regs
[15];
483 * (1) GDB breakpoints should be handled first.
484 * (2) Do not raise a CPU exception if no CPU breakpoint has fired,
485 * since singlestep is also done by generating a debug internal
488 if (cpu_breakpoint_test(cs
, pc
, BP_GDB
)
489 || !cpu_breakpoint_test(cs
, pc
, BP_CPU
)) {
493 env
->exception
.fsr
= arm_debug_exception_fsr(env
);
495 * FAR is UNKNOWN: clear vaddress to avoid potentially exposing
496 * values to the guest that it shouldn't be able to see at its
497 * exception/security level.
499 env
->exception
.vaddress
= 0;
500 raise_exception_debug(env
, EXCP_PREFETCH_ABORT
, syn_breakpoint(0));
505 * Raise an EXCP_BKPT with the specified syndrome register value,
506 * targeting the correct exception level for debug exceptions.
508 void HELPER(exception_bkpt_insn
)(CPUARMState
*env
, uint32_t syndrome
)
510 int debug_el
= arm_debug_target_el(env
);
511 int cur_el
= arm_current_el(env
);
513 /* FSR will only be used if the debug target EL is AArch32. */
514 env
->exception
.fsr
= arm_debug_exception_fsr(env
);
516 * FAR is UNKNOWN: clear vaddress to avoid potentially exposing
517 * values to the guest that it shouldn't be able to see at its
518 * exception/security level.
520 env
->exception
.vaddress
= 0;
522 * Other kinds of architectural debug exception are ignored if
523 * they target an exception level below the current one (in QEMU
524 * this is checked by arm_generate_debug_exceptions()). Breakpoint
525 * instructions are special because they always generate an exception
526 * to somewhere: if they can't go to the configured debug exception
527 * level they are taken to the current exception level.
529 if (debug_el
< cur_el
) {
532 raise_exception(env
, EXCP_BKPT
, syndrome
, debug_el
);
535 void HELPER(exception_swstep
)(CPUARMState
*env
, uint32_t syndrome
)
537 raise_exception_debug(env
, EXCP_UDEF
, syndrome
);
540 void hw_watchpoint_update(ARMCPU
*cpu
, int n
)
542 CPUARMState
*env
= &cpu
->env
;
544 vaddr wvr
= env
->cp15
.dbgwvr
[n
];
545 uint64_t wcr
= env
->cp15
.dbgwcr
[n
];
547 int flags
= BP_CPU
| BP_STOP_BEFORE_ACCESS
;
549 if (env
->cpu_watchpoint
[n
]) {
550 cpu_watchpoint_remove_by_ref(CPU(cpu
), env
->cpu_watchpoint
[n
]);
551 env
->cpu_watchpoint
[n
] = NULL
;
554 if (!FIELD_EX64(wcr
, DBGWCR
, E
)) {
555 /* E bit clear : watchpoint disabled */
559 switch (FIELD_EX64(wcr
, DBGWCR
, LSC
)) {
561 /* LSC 00 is reserved and must behave as if the wp is disabled */
564 flags
|= BP_MEM_READ
;
567 flags
|= BP_MEM_WRITE
;
570 flags
|= BP_MEM_ACCESS
;
575 * Attempts to use both MASK and BAS fields simultaneously are
576 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
577 * thus generating a watchpoint for every byte in the masked region.
579 mask
= FIELD_EX64(wcr
, DBGWCR
, MASK
);
580 if (mask
== 1 || mask
== 2) {
582 * Reserved values of MASK; we must act as if the mask value was
583 * some non-reserved value, or as if the watchpoint were disabled.
584 * We choose the latter.
588 /* Watchpoint covers an aligned area up to 2GB in size */
591 * If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
592 * whether the watchpoint fires when the unmasked bits match; we opt
593 * to generate the exceptions.
597 /* Watchpoint covers bytes defined by the byte address select bits */
598 int bas
= FIELD_EX64(wcr
, DBGWCR
, BAS
);
601 if (extract64(wvr
, 2, 1)) {
603 * Deprecated case of an only 4-aligned address. BAS[7:4] are
604 * ignored, and BAS[3:0] define which bytes to watch.
610 /* This must act as if the watchpoint is disabled */
615 * The BAS bits are supposed to be programmed to indicate a contiguous
616 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
617 * we fire for each byte in the word/doubleword addressed by the WVR.
618 * We choose to ignore any non-zero bits after the first range of 1s.
620 basstart
= ctz32(bas
);
621 len
= cto32(bas
>> basstart
);
625 cpu_watchpoint_insert(CPU(cpu
), wvr
, len
, flags
,
626 &env
->cpu_watchpoint
[n
]);
629 void hw_watchpoint_update_all(ARMCPU
*cpu
)
632 CPUARMState
*env
= &cpu
->env
;
635 * Completely clear out existing QEMU watchpoints and our array, to
636 * avoid possible stale entries following migration load.
638 cpu_watchpoint_remove_all(CPU(cpu
), BP_CPU
);
639 memset(env
->cpu_watchpoint
, 0, sizeof(env
->cpu_watchpoint
));
641 for (i
= 0; i
< ARRAY_SIZE(cpu
->env
.cpu_watchpoint
); i
++) {
642 hw_watchpoint_update(cpu
, i
);
646 void hw_breakpoint_update(ARMCPU
*cpu
, int n
)
648 CPUARMState
*env
= &cpu
->env
;
649 uint64_t bvr
= env
->cp15
.dbgbvr
[n
];
650 uint64_t bcr
= env
->cp15
.dbgbcr
[n
];
655 if (env
->cpu_breakpoint
[n
]) {
656 cpu_breakpoint_remove_by_ref(CPU(cpu
), env
->cpu_breakpoint
[n
]);
657 env
->cpu_breakpoint
[n
] = NULL
;
660 if (!extract64(bcr
, 0, 1)) {
661 /* E bit clear : watchpoint disabled */
665 bt
= extract64(bcr
, 20, 4);
668 case 4: /* unlinked address mismatch (reserved if AArch64) */
669 case 5: /* linked address mismatch (reserved if AArch64) */
670 qemu_log_mask(LOG_UNIMP
,
671 "arm: address mismatch breakpoint types not implemented\n");
673 case 0: /* unlinked address match */
674 case 1: /* linked address match */
677 * Bits [1:0] are RES0.
679 * It is IMPLEMENTATION DEFINED whether bits [63:49]
680 * ([63:53] for FEAT_LVA) are hardwired to a copy of the sign bit
681 * of the VA field ([48] or [52] for FEAT_LVA), or whether the
682 * value is read as written. It is CONSTRAINED UNPREDICTABLE
683 * whether the RESS bits are ignored when comparing an address.
684 * Therefore we are allowed to compare the entire register, which
685 * lets us avoid considering whether FEAT_LVA is actually enabled.
687 * The BAS field is used to allow setting breakpoints on 16-bit
688 * wide instructions; it is CONSTRAINED UNPREDICTABLE whether
689 * a bp will fire if the addresses covered by the bp and the addresses
690 * covered by the insn overlap but the insn doesn't start at the
691 * start of the bp address range. We choose to require the insn and
692 * the bp to have the same address. The constraints on writing to
693 * BAS enforced in dbgbcr_write mean we have only four cases:
694 * 0b0000 => no breakpoint
695 * 0b0011 => breakpoint on addr
696 * 0b1100 => breakpoint on addr + 2
697 * 0b1111 => breakpoint on addr
698 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
700 int bas
= extract64(bcr
, 5, 4);
710 case 2: /* unlinked context ID match */
711 case 8: /* unlinked VMID match (reserved if no EL2) */
712 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
713 qemu_log_mask(LOG_UNIMP
,
714 "arm: unlinked context breakpoint types not implemented\n");
716 case 9: /* linked VMID match (reserved if no EL2) */
717 case 11: /* linked context ID and VMID match (reserved if no EL2) */
718 case 3: /* linked context ID match */
721 * We must generate no events for Linked context matches (unless
722 * they are linked to by some other bp/wp, which is handled in
723 * updates for the linking bp/wp). We choose to also generate no events
724 * for reserved values.
729 cpu_breakpoint_insert(CPU(cpu
), addr
, flags
, &env
->cpu_breakpoint
[n
]);
732 void hw_breakpoint_update_all(ARMCPU
*cpu
)
735 CPUARMState
*env
= &cpu
->env
;
738 * Completely clear out existing QEMU breakpoints and our array, to
739 * avoid possible stale entries following migration load.
741 cpu_breakpoint_remove_all(CPU(cpu
), BP_CPU
);
742 memset(env
->cpu_breakpoint
, 0, sizeof(env
->cpu_breakpoint
));
744 for (i
= 0; i
< ARRAY_SIZE(cpu
->env
.cpu_breakpoint
); i
++) {
745 hw_breakpoint_update(cpu
, i
);
749 #if !defined(CONFIG_USER_ONLY)
751 vaddr
arm_adjust_watchpoint_address(CPUState
*cs
, vaddr addr
, int len
)
753 ARMCPU
*cpu
= ARM_CPU(cs
);
754 CPUARMState
*env
= &cpu
->env
;
757 * In BE32 system mode, target memory is stored byteswapped (on a
758 * little-endian host system), and by the time we reach here (via an
759 * opcode helper) the addresses of subword accesses have been adjusted
760 * to account for that, which means that watchpoints will not match.
761 * Undo the adjustment here.
763 if (arm_sctlr_b(env
)) {
766 } else if (len
== 2) {
774 #endif /* !CONFIG_USER_ONLY */
775 #endif /* CONFIG_TCG */
778 * Check for traps to "powerdown debug" registers, which are controlled
781 static CPAccessResult
access_tdosa(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
784 int el
= arm_current_el(env
);
785 uint64_t mdcr_el2
= arm_mdcr_el2_eff(env
);
786 bool mdcr_el2_tdosa
= (mdcr_el2
& MDCR_TDOSA
) || (mdcr_el2
& MDCR_TDE
) ||
787 (arm_hcr_el2_eff(env
) & HCR_TGE
);
789 if (el
< 2 && mdcr_el2_tdosa
) {
790 return CP_ACCESS_TRAP_EL2
;
792 if (el
< 3 && (env
->cp15
.mdcr_el3
& MDCR_TDOSA
)) {
793 return CP_ACCESS_TRAP_EL3
;
799 * Check for traps to "debug ROM" registers, which are controlled
800 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
802 static CPAccessResult
access_tdra(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
805 int el
= arm_current_el(env
);
806 uint64_t mdcr_el2
= arm_mdcr_el2_eff(env
);
807 bool mdcr_el2_tdra
= (mdcr_el2
& MDCR_TDRA
) || (mdcr_el2
& MDCR_TDE
) ||
808 (arm_hcr_el2_eff(env
) & HCR_TGE
);
810 if (el
< 2 && mdcr_el2_tdra
) {
811 return CP_ACCESS_TRAP_EL2
;
813 if (el
< 3 && (env
->cp15
.mdcr_el3
& MDCR_TDA
)) {
814 return CP_ACCESS_TRAP_EL3
;
820 * Check for traps to general debug registers, which are controlled
821 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
823 static CPAccessResult
access_tda(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
826 int el
= arm_current_el(env
);
827 uint64_t mdcr_el2
= arm_mdcr_el2_eff(env
);
828 bool mdcr_el2_tda
= (mdcr_el2
& MDCR_TDA
) || (mdcr_el2
& MDCR_TDE
) ||
829 (arm_hcr_el2_eff(env
) & HCR_TGE
);
831 if (el
< 2 && mdcr_el2_tda
) {
832 return CP_ACCESS_TRAP_EL2
;
834 if (el
< 3 && (env
->cp15
.mdcr_el3
& MDCR_TDA
)) {
835 return CP_ACCESS_TRAP_EL3
;
841 * Check for traps to Debug Comms Channel registers. If FEAT_FGT
842 * is implemented then these are controlled by MDCR_EL2.TDCC for
843 * EL2 and MDCR_EL3.TDCC for EL3. They are also controlled by
844 * the general debug access trap bits MDCR_EL2.TDA and MDCR_EL3.TDA.
846 static CPAccessResult
access_tdcc(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
849 int el
= arm_current_el(env
);
850 uint64_t mdcr_el2
= arm_mdcr_el2_eff(env
);
851 bool mdcr_el2_tda
= (mdcr_el2
& MDCR_TDA
) || (mdcr_el2
& MDCR_TDE
) ||
852 (arm_hcr_el2_eff(env
) & HCR_TGE
);
853 bool mdcr_el2_tdcc
= cpu_isar_feature(aa64_fgt
, env_archcpu(env
)) &&
854 (mdcr_el2
& MDCR_TDCC
);
855 bool mdcr_el3_tdcc
= cpu_isar_feature(aa64_fgt
, env_archcpu(env
)) &&
856 (env
->cp15
.mdcr_el3
& MDCR_TDCC
);
858 if (el
< 2 && (mdcr_el2_tda
|| mdcr_el2_tdcc
)) {
859 return CP_ACCESS_TRAP_EL2
;
861 if (el
< 3 && ((env
->cp15
.mdcr_el3
& MDCR_TDA
) || mdcr_el3_tdcc
)) {
862 return CP_ACCESS_TRAP_EL3
;
867 static void oslar_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
871 * Writes to OSLAR_EL1 may update the OS lock status, which can be
872 * read via a bit in OSLSR_EL1.
876 if (ri
->state
== ARM_CP_STATE_AA32
) {
877 oslock
= (value
== 0xC5ACCE55);
882 env
->cp15
.oslsr_el1
= deposit32(env
->cp15
.oslsr_el1
, 1, 1, oslock
);
885 static void osdlr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
888 ARMCPU
*cpu
= env_archcpu(env
);
890 * Only defined bit is bit 0 (DLK); if Feat_DoubleLock is not
891 * implemented this is RAZ/WI.
893 if(arm_feature(env
, ARM_FEATURE_AARCH64
)
894 ? cpu_isar_feature(aa64_doublelock
, cpu
)
895 : cpu_isar_feature(aa32_doublelock
, cpu
)) {
896 env
->cp15
.osdlr_el1
= value
& 1;
900 static void dbgclaimset_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
903 env
->cp15
.dbgclaim
|= (value
& 0xFF);
906 static uint64_t dbgclaimset_read(CPUARMState
*env
, const ARMCPRegInfo
*ri
)
908 /* CLAIM bits are RAO */
912 static void dbgclaimclr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
915 env
->cp15
.dbgclaim
&= ~(value
& 0xFF);
918 static const ARMCPRegInfo debug_cp_reginfo
[] = {
920 * DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
921 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
922 * unlike DBGDRAR it is never accessible from EL0.
923 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
926 { .name
= "DBGDRAR", .cp
= 14, .crn
= 1, .crm
= 0, .opc1
= 0, .opc2
= 0,
927 .access
= PL0_R
, .accessfn
= access_tdra
,
928 .type
= ARM_CP_CONST
, .resetvalue
= 0 },
929 { .name
= "MDRAR_EL1", .state
= ARM_CP_STATE_AA64
,
930 .opc0
= 2, .opc1
= 0, .crn
= 1, .crm
= 0, .opc2
= 0,
931 .access
= PL1_R
, .accessfn
= access_tdra
,
932 .type
= ARM_CP_CONST
, .resetvalue
= 0 },
933 { .name
= "DBGDSAR", .cp
= 14, .crn
= 2, .crm
= 0, .opc1
= 0, .opc2
= 0,
934 .access
= PL0_R
, .accessfn
= access_tdra
,
935 .type
= ARM_CP_CONST
, .resetvalue
= 0 },
936 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
937 { .name
= "MDSCR_EL1", .state
= ARM_CP_STATE_BOTH
,
938 .cp
= 14, .opc0
= 2, .opc1
= 0, .crn
= 0, .crm
= 2, .opc2
= 2,
939 .access
= PL1_RW
, .accessfn
= access_tda
,
940 .fgt
= FGT_MDSCR_EL1
,
941 .fieldoffset
= offsetof(CPUARMState
, cp15
.mdscr_el1
),
944 * MDCCSR_EL0[30:29] map to EDSCR[30:29]. Simply RAZ as the external
945 * Debug Communication Channel is not implemented.
947 { .name
= "MDCCSR_EL0", .state
= ARM_CP_STATE_AA64
,
948 .opc0
= 2, .opc1
= 3, .crn
= 0, .crm
= 1, .opc2
= 0,
949 .access
= PL0_R
, .accessfn
= access_tdcc
,
950 .type
= ARM_CP_CONST
, .resetvalue
= 0 },
952 * OSDTRRX_EL1/OSDTRTX_EL1 are used for save and restore of DBGDTRRX_EL0.
953 * It is a component of the Debug Communications Channel, which is not implemented.
955 { .name
= "OSDTRRX_EL1", .state
= ARM_CP_STATE_BOTH
, .cp
= 14,
956 .opc0
= 2, .opc1
= 0, .crn
= 0, .crm
= 0, .opc2
= 2,
957 .access
= PL1_RW
, .accessfn
= access_tdcc
,
958 .type
= ARM_CP_CONST
, .resetvalue
= 0 },
959 { .name
= "OSDTRTX_EL1", .state
= ARM_CP_STATE_BOTH
, .cp
= 14,
960 .opc0
= 2, .opc1
= 0, .crn
= 0, .crm
= 3, .opc2
= 2,
961 .access
= PL1_RW
, .accessfn
= access_tdcc
,
962 .type
= ARM_CP_CONST
, .resetvalue
= 0 },
964 * OSECCR_EL1 provides a mechanism for an operating system
965 * to access the contents of EDECCR. EDECCR is not implemented though,
966 * as is the rest of external device mechanism.
968 { .name
= "OSECCR_EL1", .state
= ARM_CP_STATE_BOTH
, .cp
= 14,
969 .opc0
= 2, .opc1
= 0, .crn
= 0, .crm
= 6, .opc2
= 2,
970 .access
= PL1_RW
, .accessfn
= access_tda
,
971 .fgt
= FGT_OSECCR_EL1
,
972 .type
= ARM_CP_CONST
, .resetvalue
= 0 },
974 * DBGDSCRint[15,12,5:2] map to MDSCR_EL1[15,12,5:2]. Map all bits as
975 * it is unlikely a guest will care.
976 * We don't implement the configurable EL0 access.
978 { .name
= "DBGDSCRint", .state
= ARM_CP_STATE_AA32
,
979 .cp
= 14, .opc1
= 0, .crn
= 0, .crm
= 1, .opc2
= 0,
980 .type
= ARM_CP_ALIAS
,
981 .access
= PL1_R
, .accessfn
= access_tda
,
982 .fieldoffset
= offsetof(CPUARMState
, cp15
.mdscr_el1
), },
983 { .name
= "OSLAR_EL1", .state
= ARM_CP_STATE_BOTH
,
984 .cp
= 14, .opc0
= 2, .opc1
= 0, .crn
= 1, .crm
= 0, .opc2
= 4,
985 .access
= PL1_W
, .type
= ARM_CP_NO_RAW
,
986 .accessfn
= access_tdosa
,
987 .fgt
= FGT_OSLAR_EL1
,
988 .writefn
= oslar_write
},
989 { .name
= "OSLSR_EL1", .state
= ARM_CP_STATE_BOTH
,
990 .cp
= 14, .opc0
= 2, .opc1
= 0, .crn
= 1, .crm
= 1, .opc2
= 4,
991 .access
= PL1_R
, .resetvalue
= 10,
992 .accessfn
= access_tdosa
,
993 .fgt
= FGT_OSLSR_EL1
,
994 .fieldoffset
= offsetof(CPUARMState
, cp15
.oslsr_el1
) },
995 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
996 { .name
= "OSDLR_EL1", .state
= ARM_CP_STATE_BOTH
,
997 .cp
= 14, .opc0
= 2, .opc1
= 0, .crn
= 1, .crm
= 3, .opc2
= 4,
998 .access
= PL1_RW
, .accessfn
= access_tdosa
,
999 .fgt
= FGT_OSDLR_EL1
,
1000 .writefn
= osdlr_write
,
1001 .fieldoffset
= offsetof(CPUARMState
, cp15
.osdlr_el1
) },
1003 * Dummy DBGVCR: Linux wants to clear this on startup, but we don't
1004 * implement vector catch debug events yet.
1007 .cp
= 14, .opc1
= 0, .crn
= 0, .crm
= 7, .opc2
= 0,
1008 .access
= PL1_RW
, .accessfn
= access_tda
,
1009 .type
= ARM_CP_NOP
},
1011 * Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor
1012 * to save and restore a 32-bit guest's DBGVCR)
1014 { .name
= "DBGVCR32_EL2", .state
= ARM_CP_STATE_AA64
,
1015 .opc0
= 2, .opc1
= 4, .crn
= 0, .crm
= 7, .opc2
= 0,
1016 .access
= PL2_RW
, .accessfn
= access_tda
,
1017 .type
= ARM_CP_NOP
| ARM_CP_EL3_NO_EL2_KEEP
},
1019 * Dummy MDCCINT_EL1, since we don't implement the Debug Communications
1020 * Channel but Linux may try to access this register. The 32-bit
1021 * alias is DBGDCCINT.
1023 { .name
= "MDCCINT_EL1", .state
= ARM_CP_STATE_BOTH
,
1024 .cp
= 14, .opc0
= 2, .opc1
= 0, .crn
= 0, .crm
= 2, .opc2
= 0,
1025 .access
= PL1_RW
, .accessfn
= access_tdcc
,
1026 .type
= ARM_CP_NOP
},
1028 * Dummy DBGCLAIM registers.
1029 * "The architecture does not define any functionality for the CLAIM tag bits.",
1030 * so we only keep the raw bits
1032 { .name
= "DBGCLAIMSET_EL1", .state
= ARM_CP_STATE_BOTH
,
1033 .cp
= 14, .opc0
= 2, .opc1
= 0, .crn
= 7, .crm
= 8, .opc2
= 6,
1034 .type
= ARM_CP_ALIAS
,
1035 .access
= PL1_RW
, .accessfn
= access_tda
,
1036 .fgt
= FGT_DBGCLAIM
,
1037 .writefn
= dbgclaimset_write
, .readfn
= dbgclaimset_read
},
1038 { .name
= "DBGCLAIMCLR_EL1", .state
= ARM_CP_STATE_BOTH
,
1039 .cp
= 14, .opc0
= 2, .opc1
= 0, .crn
= 7, .crm
= 9, .opc2
= 6,
1040 .access
= PL1_RW
, .accessfn
= access_tda
,
1041 .fgt
= FGT_DBGCLAIM
,
1042 .writefn
= dbgclaimclr_write
, .raw_writefn
= raw_write
,
1043 .fieldoffset
= offsetof(CPUARMState
, cp15
.dbgclaim
) },
1046 static const ARMCPRegInfo debug_lpae_cp_reginfo
[] = {
1047 /* 64 bit access versions of the (dummy) debug registers */
1048 { .name
= "DBGDRAR", .cp
= 14, .crm
= 1, .opc1
= 0,
1049 .access
= PL0_R
, .type
= ARM_CP_CONST
| ARM_CP_64BIT
, .resetvalue
= 0 },
1050 { .name
= "DBGDSAR", .cp
= 14, .crm
= 2, .opc1
= 0,
1051 .access
= PL0_R
, .type
= ARM_CP_CONST
| ARM_CP_64BIT
, .resetvalue
= 0 },
1054 static void dbgwvr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1057 ARMCPU
*cpu
= env_archcpu(env
);
1061 * Bits [1:0] are RES0.
1063 * It is IMPLEMENTATION DEFINED whether [63:49] ([63:53] with FEAT_LVA)
1064 * are hardwired to the value of bit [48] ([52] with FEAT_LVA), or if
1065 * they contain the value written. It is CONSTRAINED UNPREDICTABLE
1066 * whether the RESS bits are ignored when comparing an address.
1068 * Therefore we are allowed to compare the entire register, which lets
1069 * us avoid considering whether or not FEAT_LVA is actually enabled.
1073 raw_write(env
, ri
, value
);
1074 if (tcg_enabled()) {
1075 hw_watchpoint_update(cpu
, i
);
1079 static void dbgwcr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1082 ARMCPU
*cpu
= env_archcpu(env
);
1085 raw_write(env
, ri
, value
);
1086 if (tcg_enabled()) {
1087 hw_watchpoint_update(cpu
, i
);
1091 static void dbgbvr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1094 ARMCPU
*cpu
= env_archcpu(env
);
1097 raw_write(env
, ri
, value
);
1098 if (tcg_enabled()) {
1099 hw_breakpoint_update(cpu
, i
);
1103 static void dbgbcr_write(CPUARMState
*env
, const ARMCPRegInfo
*ri
,
1106 ARMCPU
*cpu
= env_archcpu(env
);
1110 * BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
1113 value
= deposit64(value
, 6, 1, extract64(value
, 5, 1));
1114 value
= deposit64(value
, 8, 1, extract64(value
, 7, 1));
1116 raw_write(env
, ri
, value
);
1117 if (tcg_enabled()) {
1118 hw_breakpoint_update(cpu
, i
);
1122 void define_debug_regs(ARMCPU
*cpu
)
1125 * Define v7 and v8 architectural debug registers.
1126 * These are just dummy implementations for now.
1129 int wrps
, brps
, ctx_cmps
;
1132 * The Arm ARM says DBGDIDR is optional and deprecated if EL1 cannot
1133 * use AArch32. Given that bit 15 is RES1, if the value is 0 then
1134 * the register must not exist for this cpu.
1136 if (cpu
->isar
.dbgdidr
!= 0) {
1137 ARMCPRegInfo dbgdidr
= {
1138 .name
= "DBGDIDR", .cp
= 14, .crn
= 0, .crm
= 0,
1139 .opc1
= 0, .opc2
= 0,
1140 .access
= PL0_R
, .accessfn
= access_tda
,
1141 .type
= ARM_CP_CONST
, .resetvalue
= cpu
->isar
.dbgdidr
,
1143 define_one_arm_cp_reg(cpu
, &dbgdidr
);
1147 * DBGDEVID is present in the v7 debug architecture if
1148 * DBGDIDR.DEVID_imp is 1 (bit 15); from v7.1 and on it is
1149 * mandatory (and bit 15 is RES1). DBGDEVID1 and DBGDEVID2 exist
1150 * from v7.1 of the debug architecture. Because no fields have yet
1151 * been defined in DBGDEVID2 (and quite possibly none will ever
1152 * be) we don't define an ARMISARegisters field for it.
1153 * These registers exist only if EL1 can use AArch32, but that
1154 * happens naturally because they are only PL1 accessible anyway.
1156 if (extract32(cpu
->isar
.dbgdidr
, 15, 1)) {
1157 ARMCPRegInfo dbgdevid
= {
1159 .cp
= 14, .opc1
= 0, .crn
= 7, .opc2
= 2, .crn
= 7,
1160 .access
= PL1_R
, .accessfn
= access_tda
,
1161 .type
= ARM_CP_CONST
, .resetvalue
= cpu
->isar
.dbgdevid
,
1163 define_one_arm_cp_reg(cpu
, &dbgdevid
);
1165 if (cpu_isar_feature(aa32_debugv7p1
, cpu
)) {
1166 ARMCPRegInfo dbgdevid12
[] = {
1168 .name
= "DBGDEVID1",
1169 .cp
= 14, .opc1
= 0, .crn
= 7, .opc2
= 1, .crn
= 7,
1170 .access
= PL1_R
, .accessfn
= access_tda
,
1171 .type
= ARM_CP_CONST
, .resetvalue
= cpu
->isar
.dbgdevid1
,
1173 .name
= "DBGDEVID2",
1174 .cp
= 14, .opc1
= 0, .crn
= 7, .opc2
= 0, .crn
= 7,
1175 .access
= PL1_R
, .accessfn
= access_tda
,
1176 .type
= ARM_CP_CONST
, .resetvalue
= 0,
1179 define_arm_cp_regs(cpu
, dbgdevid12
);
1182 brps
= arm_num_brps(cpu
);
1183 wrps
= arm_num_wrps(cpu
);
1184 ctx_cmps
= arm_num_ctx_cmps(cpu
);
1186 assert(ctx_cmps
<= brps
);
1188 define_arm_cp_regs(cpu
, debug_cp_reginfo
);
1190 if (arm_feature(&cpu
->env
, ARM_FEATURE_LPAE
)) {
1191 define_arm_cp_regs(cpu
, debug_lpae_cp_reginfo
);
1194 for (i
= 0; i
< brps
; i
++) {
1195 char *dbgbvr_el1_name
= g_strdup_printf("DBGBVR%d_EL1", i
);
1196 char *dbgbcr_el1_name
= g_strdup_printf("DBGBCR%d_EL1", i
);
1197 ARMCPRegInfo dbgregs
[] = {
1198 { .name
= dbgbvr_el1_name
, .state
= ARM_CP_STATE_BOTH
,
1199 .cp
= 14, .opc0
= 2, .opc1
= 0, .crn
= 0, .crm
= i
, .opc2
= 4,
1200 .access
= PL1_RW
, .accessfn
= access_tda
,
1201 .fgt
= FGT_DBGBVRN_EL1
,
1202 .fieldoffset
= offsetof(CPUARMState
, cp15
.dbgbvr
[i
]),
1203 .writefn
= dbgbvr_write
, .raw_writefn
= raw_write
1205 { .name
= dbgbcr_el1_name
, .state
= ARM_CP_STATE_BOTH
,
1206 .cp
= 14, .opc0
= 2, .opc1
= 0, .crn
= 0, .crm
= i
, .opc2
= 5,
1207 .access
= PL1_RW
, .accessfn
= access_tda
,
1208 .fgt
= FGT_DBGBCRN_EL1
,
1209 .fieldoffset
= offsetof(CPUARMState
, cp15
.dbgbcr
[i
]),
1210 .writefn
= dbgbcr_write
, .raw_writefn
= raw_write
1213 define_arm_cp_regs(cpu
, dbgregs
);
1214 g_free(dbgbvr_el1_name
);
1215 g_free(dbgbcr_el1_name
);
1218 for (i
= 0; i
< wrps
; i
++) {
1219 char *dbgwvr_el1_name
= g_strdup_printf("DBGWVR%d_EL1", i
);
1220 char *dbgwcr_el1_name
= g_strdup_printf("DBGWCR%d_EL1", i
);
1221 ARMCPRegInfo dbgregs
[] = {
1222 { .name
= dbgwvr_el1_name
, .state
= ARM_CP_STATE_BOTH
,
1223 .cp
= 14, .opc0
= 2, .opc1
= 0, .crn
= 0, .crm
= i
, .opc2
= 6,
1224 .access
= PL1_RW
, .accessfn
= access_tda
,
1225 .fgt
= FGT_DBGWVRN_EL1
,
1226 .fieldoffset
= offsetof(CPUARMState
, cp15
.dbgwvr
[i
]),
1227 .writefn
= dbgwvr_write
, .raw_writefn
= raw_write
1229 { .name
= dbgwcr_el1_name
, .state
= ARM_CP_STATE_BOTH
,
1230 .cp
= 14, .opc0
= 2, .opc1
= 0, .crn
= 0, .crm
= i
, .opc2
= 7,
1231 .access
= PL1_RW
, .accessfn
= access_tda
,
1232 .fgt
= FGT_DBGWCRN_EL1
,
1233 .fieldoffset
= offsetof(CPUARMState
, cp15
.dbgwcr
[i
]),
1234 .writefn
= dbgwcr_write
, .raw_writefn
= raw_write
1237 define_arm_cp_regs(cpu
, dbgregs
);
1238 g_free(dbgwvr_el1_name
);
1239 g_free(dbgwcr_el1_name
);