arm_adi_v5: fix SIGSEGV due to failing re-examine
Commit35a503b08d
("arm_adi_v5: add ap refcount and add get/put around ap use") modifies the examine functions of mem_ap, cortex_m, cortex_a and aarch64 by calling dap_put_ap() and then looking again for the mem-ap and calling dap_get_ap(). This causes an issue if the system is irresponsive and the examine fails and left the AP pointer to NULL. If the system was already examined the NULL pointer will cause a SIGSEGV. Commitb6dad912b8
("target/cortex_m: prevent segmentation fault in cortex_m_poll()") proposes a fix for one specific case and only on cortex_m. Modify all the examine functions by skipping look-up for the AP if it was already set in a previous examine; the target's AP is not supposed to change during runtime. Remove the partial fix for cortex_m as it is not needed anymore. Change-Id: I806ec3b1b02fcc76e141c8dd3a65044febbf0a8c Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com> Fixes:35a503b08d
("arm_adi_v5: add ap refcount and add get/put around ap use") Reviewed-on: https://review.openocd.org/c/openocd/+/7392 Tested-by: jenkins Reviewed-by: Tomas Vanek <vanekt@fbl.cz>
This commit is contained in:
parent
2278878a05
commit
c6fe10de75
|
@ -2546,11 +2546,7 @@ static int aarch64_examine_first(struct target *target)
|
||||||
if (!pc)
|
if (!pc)
|
||||||
return ERROR_FAIL;
|
return ERROR_FAIL;
|
||||||
|
|
||||||
if (armv8->debug_ap) {
|
if (!armv8->debug_ap) {
|
||||||
dap_put_ap(armv8->debug_ap);
|
|
||||||
armv8->debug_ap = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pc->adiv5_config.ap_num == DP_APSEL_INVALID) {
|
if (pc->adiv5_config.ap_num == DP_APSEL_INVALID) {
|
||||||
/* Search for the APB-AB */
|
/* Search for the APB-AB */
|
||||||
retval = dap_find_get_ap(swjdp, AP_TYPE_APB_AP, &armv8->debug_ap);
|
retval = dap_find_get_ap(swjdp, AP_TYPE_APB_AP, &armv8->debug_ap);
|
||||||
|
@ -2565,6 +2561,7 @@ static int aarch64_examine_first(struct target *target)
|
||||||
return ERROR_FAIL;
|
return ERROR_FAIL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
retval = mem_ap_init(armv8->debug_ap);
|
retval = mem_ap_init(armv8->debug_ap);
|
||||||
if (retval != ERROR_OK) {
|
if (retval != ERROR_OK) {
|
||||||
|
|
|
@ -2874,11 +2874,7 @@ static int cortex_a_examine_first(struct target *target)
|
||||||
int retval = ERROR_OK;
|
int retval = ERROR_OK;
|
||||||
uint32_t didr, cpuid, dbg_osreg, dbg_idpfr1;
|
uint32_t didr, cpuid, dbg_osreg, dbg_idpfr1;
|
||||||
|
|
||||||
if (armv7a->debug_ap) {
|
if (!armv7a->debug_ap) {
|
||||||
dap_put_ap(armv7a->debug_ap);
|
|
||||||
armv7a->debug_ap = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pc->ap_num == DP_APSEL_INVALID) {
|
if (pc->ap_num == DP_APSEL_INVALID) {
|
||||||
/* Search for the APB-AP - it is needed for access to debug registers */
|
/* Search for the APB-AP - it is needed for access to debug registers */
|
||||||
retval = dap_find_get_ap(swjdp, AP_TYPE_APB_AP, &armv7a->debug_ap);
|
retval = dap_find_get_ap(swjdp, AP_TYPE_APB_AP, &armv7a->debug_ap);
|
||||||
|
@ -2893,6 +2889,7 @@ static int cortex_a_examine_first(struct target *target)
|
||||||
return ERROR_FAIL;
|
return ERROR_FAIL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
retval = mem_ap_init(armv7a->debug_ap);
|
retval = mem_ap_init(armv7a->debug_ap);
|
||||||
if (retval != ERROR_OK) {
|
if (retval != ERROR_OK) {
|
||||||
|
|
|
@ -879,16 +879,6 @@ static int cortex_m_poll(struct target *target)
|
||||||
struct cortex_m_common *cortex_m = target_to_cm(target);
|
struct cortex_m_common *cortex_m = target_to_cm(target);
|
||||||
struct armv7m_common *armv7m = &cortex_m->armv7m;
|
struct armv7m_common *armv7m = &cortex_m->armv7m;
|
||||||
|
|
||||||
/* Check if debug_ap is available to prevent segmentation fault.
|
|
||||||
* If the re-examination after an error does not find a MEM-AP
|
|
||||||
* (e.g. the target stopped communicating), debug_ap pointer
|
|
||||||
* can suddenly become NULL.
|
|
||||||
*/
|
|
||||||
if (!armv7m->debug_ap) {
|
|
||||||
target->state = TARGET_UNKNOWN;
|
|
||||||
return ERROR_TARGET_NOT_EXAMINED;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Read from Debug Halting Control and Status Register */
|
/* Read from Debug Halting Control and Status Register */
|
||||||
retval = cortex_m_read_dhcsr_atomic_sticky(target);
|
retval = cortex_m_read_dhcsr_atomic_sticky(target);
|
||||||
if (retval != ERROR_OK) {
|
if (retval != ERROR_OK) {
|
||||||
|
@ -2311,11 +2301,7 @@ int cortex_m_examine(struct target *target)
|
||||||
/* hla_target shares the examine handler but does not support
|
/* hla_target shares the examine handler but does not support
|
||||||
* all its calls */
|
* all its calls */
|
||||||
if (!armv7m->is_hla_target) {
|
if (!armv7m->is_hla_target) {
|
||||||
if (armv7m->debug_ap) {
|
if (!armv7m->debug_ap) {
|
||||||
dap_put_ap(armv7m->debug_ap);
|
|
||||||
armv7m->debug_ap = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cortex_m->apsel == DP_APSEL_INVALID) {
|
if (cortex_m->apsel == DP_APSEL_INVALID) {
|
||||||
/* Search for the MEM-AP */
|
/* Search for the MEM-AP */
|
||||||
retval = cortex_m_find_mem_ap(swjdp, &armv7m->debug_ap);
|
retval = cortex_m_find_mem_ap(swjdp, &armv7m->debug_ap);
|
||||||
|
@ -2330,6 +2316,7 @@ int cortex_m_examine(struct target *target)
|
||||||
return ERROR_FAIL;
|
return ERROR_FAIL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
armv7m->debug_ap->memaccess_tck = 8;
|
armv7m->debug_ap->memaccess_tck = 8;
|
||||||
|
|
||||||
|
|
|
@ -136,16 +136,13 @@ static int mem_ap_examine(struct target *target)
|
||||||
struct mem_ap *mem_ap = target->arch_info;
|
struct mem_ap *mem_ap = target->arch_info;
|
||||||
|
|
||||||
if (!target_was_examined(target)) {
|
if (!target_was_examined(target)) {
|
||||||
if (mem_ap->ap) {
|
if (!mem_ap->ap) {
|
||||||
dap_put_ap(mem_ap->ap);
|
|
||||||
mem_ap->ap = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
mem_ap->ap = dap_get_ap(mem_ap->dap, mem_ap->ap_num);
|
mem_ap->ap = dap_get_ap(mem_ap->dap, mem_ap->ap_num);
|
||||||
if (!mem_ap->ap) {
|
if (!mem_ap->ap) {
|
||||||
LOG_ERROR("Cannot get AP");
|
LOG_ERROR("Cannot get AP");
|
||||||
return ERROR_FAIL;
|
return ERROR_FAIL;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
target_set_examined(target);
|
target_set_examined(target);
|
||||||
target->state = TARGET_UNKNOWN;
|
target->state = TARGET_UNKNOWN;
|
||||||
target->debug_reason = DBG_REASON_UNDEFINED;
|
target->debug_reason = DBG_REASON_UNDEFINED;
|
||||||
|
|
Loading…
Reference in New Issue