From fcd7b90db6f6f81ace4e35fca8d124d0fb3a3685 Mon Sep 17 00:00:00 2001 From: Salvador Arroyo Date: Sun, 1 Dec 2013 10:40:34 +0100 Subject: [PATCH] mips32: cleanups in legacy pracc code This is the first patch intended to make a more precise pracc check when running in legacy mode (code executed by mips32_pracc_exec()). It only makes some cleanups, mostly due to unnecessary code. With the last cache optimizations for processor access (pa for short) all the pracc functions generate the code following some rules that make pa more easily to check: There are no load instructions from dmseg. All the read pas are instruction fetches. PARAM_IN related stuff is not needed. Registers are restored either from COP0 DeSave or from ejtag info fields. PRACC_STACK related stuff is not needed any more. The code starts execution at PRACC_TEXT and there are no branch or jump instruction in the code, apart from the last jump to PRACC_TEXT. The fetch address is ever known. For every store instruction to dmseg the function code sets the address of the write/store pa. The address of every store pa is known. Current code ends execution when reading a second pass through PRACC_TEXT. This approach has same inconveniences: If the code starts in the delay slot of a jump it makes a jump to PRACC_TEXT after executing the first instruction. A second pass through PRACC_TEXt is read and the function exits without any warning. This seems to occur sometimes when a 24kc core is halted in the delay slot of a branch. If a debug mode exception is triggered during the execution of a function the core restarts execution at PRACC_TEXT. Again the function exits without any warning. If for whatever reason the core starts fetching at an unexpected address the code now sends a jump instruction to PRACC_TEXT, but due to the delay slot the core continues fetching at whatever address + 4 and a second jump instruction will be send for execution. The result of a jump instruction in the delay slot of another jump is UNPREDICTABLE. It may work as expected (ar7241), or let the core in the delay slot of a jump to PRACC_TEXT for example. This means the function called next may also fail (pic32mx). Change-Id: I9516a5146ee9c8c694d741331edc7daec9bde4e3 Signed-off-by: Salvador Arroyo Reviewed-on: http://openocd.zylin.com/1825 Tested-by: jenkins Reviewed-by: Freddie Chopin --- src/target/mips32_pracc.c | 84 ++++++++++----------------------------- src/target/mips32_pracc.h | 14 ++----- 2 files changed, 23 insertions(+), 75 deletions(-) diff --git a/src/target/mips32_pracc.c b/src/target/mips32_pracc.c index 64acc700..9ffc3ad6 100644 --- a/src/target/mips32_pracc.c +++ b/src/target/mips32_pracc.c @@ -56,12 +56,6 @@ * The original code contained NOPs. I have removed these and moved * the branches. * - * I also moved the PRACC_STACK to 0xFF204000. This allows - * the use of 16 bits offsets to get pointers to the input - * and output area relative to the stack. Note that the stack - * isn't really a stack (the stack pointer is not 'moving') - * but a FIFO simulated in software. - * * These changes result in a 35% speed increase when programming an * external flash. * @@ -82,8 +76,6 @@ #include "mips32_pracc.h" struct mips32_pracc_context { - uint32_t *local_iparam; - int num_iparam; uint32_t *local_oparam; int num_oparam; const uint32_t *code; @@ -97,22 +89,20 @@ static int wait_for_pracc_rw(struct mips_ejtag *ejtag_info, uint32_t *ctrl) { uint32_t ejtag_ctrl; long long then = timeval_ms(); - int timeout; - int retval; /* wait for the PrAcc to become "1" */ mips_ejtag_set_instr(ejtag_info, EJTAG_INST_CONTROL); while (1) { ejtag_ctrl = ejtag_info->ejtag_ctrl; - retval = mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl); + int retval = mips_ejtag_drscan_32(ejtag_info, &ejtag_ctrl); if (retval != ERROR_OK) return retval; if (ejtag_ctrl & EJTAG_CTRL_PRACC) break; - timeout = timeval_ms() - then; + int timeout = timeval_ms() - then; if (timeout > 1000) { LOG_DEBUG("DEBUGMODULE: No memory access in progress!"); return ERROR_JTAG_DEVICE_ERROR; @@ -125,29 +115,12 @@ static int wait_for_pracc_rw(struct mips_ejtag *ejtag_info, uint32_t *ctrl) static int mips32_pracc_exec_read(struct mips32_pracc_context *ctx, uint32_t address) { - struct mips_ejtag *ejtag_info = ctx->ejtag_info; - int offset; - uint32_t ejtag_ctrl, data; + uint32_t code; - if ((address >= MIPS32_PRACC_PARAM_IN) - && (address < MIPS32_PRACC_PARAM_IN + ctx->num_iparam * 4)) { - offset = (address - MIPS32_PRACC_PARAM_IN) / 4; - data = ctx->local_iparam[offset]; - } else if ((address >= MIPS32_PRACC_PARAM_OUT) - && (address < MIPS32_PRACC_PARAM_OUT + ctx->num_oparam * 4)) { - offset = (address - MIPS32_PRACC_PARAM_OUT) / 4; - data = ctx->local_oparam[offset]; - } else if ((address >= MIPS32_PRACC_TEXT) + if ((address >= MIPS32_PRACC_TEXT) && (address < MIPS32_PRACC_TEXT + ctx->code_len * 4)) { - offset = (address - MIPS32_PRACC_TEXT) / 4; - data = ctx->code[offset]; - } else if (address == MIPS32_PRACC_STACK) { - if (ctx->stack_offset <= 0) { - LOG_ERROR("Error: Pracc stack out of bounds"); - return ERROR_JTAG_DEVICE_ERROR; - } - /* save to our debug stack */ - data = ctx->stack[--ctx->stack_offset]; + int offset = (address - MIPS32_PRACC_TEXT) / 4; + code = ctx->code[offset]; } else if (address >= 0xFF200000) { /* CPU keeps reading at the end of execution. * If we after 0xF0000000 address range, we can use @@ -155,18 +128,20 @@ static int mips32_pracc_exec_read(struct mips32_pracc_context *ctx, uint32_t add * Since this instruction is limited to * 26bit, we need to do some magic to fit it to our needs. */ LOG_DEBUG("Reading unexpected address. Jump to 0xFF200200\n"); - data = MIPS32_J((0x0FFFFFFF & 0xFF200200) >> 2); + code = MIPS32_J((0x0FFFFFFF & 0xFF200200) >> 2); } else { LOG_ERROR("Error reading unexpected address 0x%8.8" PRIx32 "", address); return ERROR_JTAG_DEVICE_ERROR; } + struct mips_ejtag *ejtag_info = ctx->ejtag_info; + /* Send the data out */ mips_ejtag_set_instr(ctx->ejtag_info, EJTAG_INST_DATA); - mips_ejtag_drscan_32_out(ctx->ejtag_info, data); + mips_ejtag_drscan_32_out(ctx->ejtag_info, code); /* Clear the access pending bit (let the processor eat!) */ - ejtag_ctrl = ejtag_info->ejtag_ctrl & ~EJTAG_CTRL_PRACC; + uint32_t ejtag_ctrl = ejtag_info->ejtag_ctrl & ~EJTAG_CTRL_PRACC; mips_ejtag_set_instr(ctx->ejtag_info, EJTAG_INST_CONTROL); mips_ejtag_drscan_32_out(ctx->ejtag_info, ejtag_ctrl); @@ -176,12 +151,10 @@ static int mips32_pracc_exec_read(struct mips32_pracc_context *ctx, uint32_t add static int mips32_pracc_exec_write(struct mips32_pracc_context *ctx, uint32_t address) { uint32_t ejtag_ctrl, data; - int offset; struct mips_ejtag *ejtag_info = ctx->ejtag_info; - int retval; mips_ejtag_set_instr(ctx->ejtag_info, EJTAG_INST_DATA); - retval = mips_ejtag_drscan_32(ctx->ejtag_info, &data); + int retval = mips_ejtag_drscan_32(ctx->ejtag_info, &data); if (retval != ERROR_OK) return retval; @@ -196,15 +169,8 @@ static int mips32_pracc_exec_write(struct mips32_pracc_context *ctx, uint32_t ad if ((address >= MIPS32_PRACC_PARAM_OUT) && (address < MIPS32_PRACC_PARAM_OUT + ctx->num_oparam * 4)) { - offset = (address - MIPS32_PRACC_PARAM_OUT) / 4; + int offset = (address - MIPS32_PRACC_PARAM_OUT) / 4; ctx->local_oparam[offset] = data; - } else if (address == MIPS32_PRACC_STACK) { - if (ctx->stack_offset >= 32) { - LOG_ERROR("Error: Pracc stack out of bounds"); - return ERROR_JTAG_DEVICE_ERROR; - } - /* save data onto our stack */ - ctx->stack[ctx->stack_offset++] = data; } else { LOG_ERROR("Error writing unexpected address 0x%8.8" PRIx32 "", address); return ERROR_JTAG_DEVICE_ERROR; @@ -214,29 +180,23 @@ static int mips32_pracc_exec_write(struct mips32_pracc_context *ctx, uint32_t ad } int mips32_pracc_exec(struct mips_ejtag *ejtag_info, int code_len, const uint32_t *code, - int num_param_in, uint32_t *param_in, int num_param_out, uint32_t *param_out, int cycle) + int num_param_out, uint32_t *param_out, int cycle) { - uint32_t ejtag_ctrl; - uint32_t address; struct mips32_pracc_context ctx; - int retval; - int pass = 0; - - ctx.local_iparam = param_in; ctx.local_oparam = param_out; - ctx.num_iparam = num_param_in; ctx.num_oparam = num_param_out; ctx.code = code; ctx.code_len = code_len; ctx.ejtag_info = ejtag_info; - ctx.stack_offset = 0; + int pass = 0; while (1) { - retval = wait_for_pracc_rw(ejtag_info, &ejtag_ctrl); + uint32_t ejtag_ctrl; + int retval = wait_for_pracc_rw(ejtag_info, &ejtag_ctrl); if (retval != ERROR_OK) return retval; - address = 0; + uint32_t address = 0; mips_ejtag_set_instr(ejtag_info, EJTAG_INST_ADDRESS); retval = mips_ejtag_drscan_32(ejtag_info, &address); if (retval != ERROR_OK) @@ -263,10 +223,6 @@ int mips32_pracc_exec(struct mips_ejtag *ejtag_info, int code_len, const uint32_ break; } - /* stack sanity check */ - if (ctx.stack_offset != 0) - LOG_DEBUG("Pracc Stack not zero"); - return ERROR_OK; } @@ -302,8 +258,8 @@ inline void pracc_queue_free(struct pracc_queue_info *ctx) int mips32_pracc_queue_exec(struct mips_ejtag *ejtag_info, struct pracc_queue_info *ctx, uint32_t *buf) { if (ejtag_info->mode == 0) - return mips32_pracc_exec(ejtag_info, ctx->code_count, ctx->pracc_list, 0, NULL, - ctx->store_count, buf, ctx->code_count - 1); + return mips32_pracc_exec(ejtag_info, ctx->code_count, ctx->pracc_list, + ctx->store_count, buf, ctx->code_count - 1); union scan_in { uint8_t scan_96[12]; diff --git a/src/target/mips32_pracc.h b/src/target/mips32_pracc.h index 4dc0bbe0..921587cf 100644 --- a/src/target/mips32_pracc.h +++ b/src/target/mips32_pracc.h @@ -30,20 +30,13 @@ #include #define MIPS32_PRACC_FASTDATA_AREA 0xFF200000 -#define MIPS32_PRACC_BASE_ADDR 0xFF200000 #define MIPS32_PRACC_FASTDATA_SIZE 16 +#define MIPS32_PRACC_BASE_ADDR 0xFF200000 #define MIPS32_PRACC_TEXT 0xFF200200 -#define MIPS32_PRACC_STACK 0xFF204000 -#define MIPS32_PRACC_PARAM_IN 0xFF201000 -#define MIPS32_PRACC_PARAM_IN_SIZE 0x1000 -#define MIPS32_PRACC_PARAM_OUT (MIPS32_PRACC_PARAM_IN + MIPS32_PRACC_PARAM_IN_SIZE) -#define MIPS32_PRACC_PARAM_OUT_SIZE 0x1000 +#define MIPS32_PRACC_PARAM_OUT 0xFF202000 #define PRACC_UPPER_BASE_ADDR (MIPS32_PRACC_BASE_ADDR >> 16) -#define PRACC_TEXT_OFFSET (MIPS32_PRACC_TEXT - MIPS32_PRACC_BASE_ADDR) -#define PRACC_IN_OFFSET (MIPS32_PRACC_PARAM_IN - MIPS32_PRACC_BASE_ADDR) #define PRACC_OUT_OFFSET (MIPS32_PRACC_PARAM_OUT - MIPS32_PRACC_BASE_ADDR) -#define PRACC_STACK_OFFSET (MIPS32_PRACC_STACK - MIPS32_PRACC_BASE_ADDR) #define MIPS32_FASTDATA_HANDLER_SIZE 0x80 #define UPPER16(uint32_t) (uint32_t >> 16) @@ -75,8 +68,7 @@ int mips32_pracc_read_regs(struct mips_ejtag *ejtag_info, uint32_t *regs); int mips32_pracc_write_regs(struct mips_ejtag *ejtag_info, uint32_t *regs); int mips32_pracc_exec(struct mips_ejtag *ejtag_info, int code_len, const uint32_t *code, - int num_param_in, uint32_t *param_in, - int num_param_out, uint32_t *param_out, int cycle); + int num_param_out, uint32_t *param_out, int cycle); /** * \b mips32_cp0_read -- 2.39.5