Skip to content

Commit

Permalink
target/riscv: break from long loops on shutdown request
Browse files Browse the repository at this point in the history
In loops that typically take longer time to complete, check if there is
a pending shutdown request. If so, terminate the loop.

This allows to respond to a signal requesting a shutdown during some
loops which do not return control to main OpenOCD loop.

Change-Id: Iace0b58eddde1237832d0f9333a7c7b930565674
Signed-off-by: Evgeniy Naydanov <[email protected]>
  • Loading branch information
en-sc committed Dec 7, 2023
1 parent d4c5d26 commit 0658f6c
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,6 @@ COMMAND_HELPER(server_port_command, unsigned short *out);

#define ERROR_SERVER_REMOTE_CLOSED (-400)
#define ERROR_CONNECTION_REJECTED (-401)
#define ERROR_SERVER_INTERRUPTED (-402)

#endif /* OPENOCD_SERVER_SERVER_H */
3 changes: 3 additions & 0 deletions src/target/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "image.h"
#include "target.h"
#include <helper/log.h>
#include <server/server.h>

/* convert ELF header field to host endianness */
#define field16(elf, field) \
Expand Down Expand Up @@ -1287,6 +1288,8 @@ int image_calculate_checksum(const uint8_t *buffer, uint32_t nbytes, uint32_t *c
crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buffer++) & 255];
}
keep_alive();
if (openocd_is_shutdown_pending())
return ERROR_SERVER_INTERRUPTED;
}

LOG_DEBUG("Calculating checksum done; checksum=0x%" PRIx32, crc);
Expand Down
2 changes: 2 additions & 0 deletions src/target/riscv/batch.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ int riscv_batch_run(struct riscv_batch *batch)
}

keep_alive();
if (openocd_is_shutdown_pending())
return ERROR_SERVER_INTERRUPTED;

if (bscan_tunnel_ir_width != 0) {
/* need to right-shift "in" by one bit, because of clock skew between BSCAN TAP and DM TAP */
Expand Down
2 changes: 2 additions & 0 deletions src/target/riscv/program.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ int riscv_program_write(struct riscv_program *program)
int riscv_program_exec(struct riscv_program *p, struct target *t)
{
keep_alive();
if (openocd_is_shutdown_pending())
return ERROR_SERVER_REMOTE_CLOSED;

riscv_reg_t saved_registers[GDB_REGNO_XPR31 + 1];
for (size_t i = GDB_REGNO_ZERO + 1; i <= GDB_REGNO_XPR31; ++i) {
Expand Down
5 changes: 5 additions & 0 deletions src/target/riscv/riscv-013.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,8 @@ static int dmi_op_timeout(struct target *target, uint32_t *data_in,
}

keep_alive();
if (openocd_is_shutdown_pending())
return ERROR_SERVER_INTERRUPTED;

time_t start = time(NULL);
/* This first loop performs the request. Note that if for some reason this
Expand Down Expand Up @@ -3193,6 +3195,9 @@ static int read_memory_bus_v1(struct target *target, target_addr_t address,
return ERROR_FAIL;
}
keep_alive();
if (openocd_is_shutdown_pending())
return ERROR_FAIL;

dmi_status_t status = dmi_scan(target, NULL, &sbvalue[next_read_j],
DMI_OP_READ, sbdata[j], 0, false);
/* By reading from sbdata0, we have just initiated another system bus read.
Expand Down
4 changes: 4 additions & 0 deletions src/target/riscv/riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -5140,6 +5140,8 @@ static int riscv_set_or_write_register(struct target *target,
assert(r->set_register);

keep_alive();
if (openocd_is_shutdown_pending())
return ERROR_SERVER_INTERRUPTED;

if (regid == GDB_REGNO_PC) {
return riscv_set_or_write_register(target, GDB_REGNO_DPC, value, write_through);
Expand Down Expand Up @@ -5237,6 +5239,8 @@ int riscv_get_register(struct target *target, riscv_reg_t *value,
assert(r->get_register);

keep_alive();
if (openocd_is_shutdown_pending())
return ERROR_FAIL;

if (regid == GDB_REGNO_PC) {
return riscv_get_register(target, value, GDB_REGNO_DPC);
Expand Down
19 changes: 18 additions & 1 deletion src/target/target.c
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,10 @@ int target_run_read_async_algorithm(struct target *target,

/* Avoid GDB timeouts */
keep_alive();
if (openocd_is_shutdown_pending()) {
retval = ERROR_SERVER_INTERRUPTED;
break;
}

}

Expand Down Expand Up @@ -3250,8 +3254,11 @@ int target_wait_state(struct target *target, enum target_state state, unsigned i
nvp_value2name(nvp_target_state, state)->name);
}

if (cur-then > 500)
if (cur - then > 500) {
keep_alive();
if (openocd_is_shutdown_pending())
return ERROR_SERVER_INTERRUPTED;
}

if ((cur-then) > ms) {
LOG_ERROR("timed out while waiting for target %s",
Expand Down Expand Up @@ -3534,6 +3541,10 @@ static int target_fill_mem(struct target *target,
break;
/* avoid GDB timeouts */
keep_alive();
if (openocd_is_shutdown_pending()) {
retval = ERROR_SERVER_INTERRUPTED;
break;
}
}
free(target_buf);

Expand Down Expand Up @@ -3876,6 +3887,12 @@ static COMMAND_HELPER(handle_verify_image_command_internal, enum verify_mode ver
}
}
keep_alive();
if (openocd_is_shutdown_pending()) {
retval = ERROR_SERVER_INTERRUPTED;
free(data);
free(buffer);
goto done;
}
}
}
free(data);
Expand Down

0 comments on commit 0658f6c

Please sign in to comment.