openocd: fix simple cases of NULL comparison
[openocd.git] / src / target / nds32.c
index e4bb17f9df278bd736ded0f78c1086476bc01d24..ca665ec41eaf4fe04e5fb0537bf0762c2442ac7d 100644 (file)
@@ -27,9 +27,6 @@
 #include "nds32_tlb.h"
 #include "nds32_disassembler.h"
 
-const int NDS32_BREAK_16 = 0x00EA;      /* 0xEA00 */
-const int NDS32_BREAK_32 = 0x0A000064;  /* 0x6400000A */
-
 struct nds32_edm_operation nds32_edm_ops[NDS32_EDM_OPERATION_MAX_NUM];
 uint32_t nds32_edm_ops_num;
 
@@ -47,7 +44,7 @@ const char *nds32_debug_type_name[11] = {
        "LOAD STORE GLOBAL STOP",
 };
 
-static const int NDS32_LM_SIZE_TABLE[16] = {
+static const int nds32_lm_size_table[16] = {
        4 * 1024,
        8 * 1024,
        16 * 1024,
@@ -61,7 +58,7 @@ static const int NDS32_LM_SIZE_TABLE[16] = {
        2 * 1024,
 };
 
-static const int NDS32_LINE_SIZE_TABLE[6] = {
+static const int nds32_line_size_table[6] = {
        0,
        8,
        16,
@@ -333,16 +330,16 @@ static int nds32_set_core_reg(struct reg *reg, uint8_t *buf)
        reg->dirty = false;
 
        /* update registers to take effect right now */
-       if (IR0 == mapped_regnum) {
+       if (mapped_regnum == IR0) {
                nds32_update_psw(nds32);
-       } else if (MR0 == mapped_regnum) {
+       } else if (mapped_regnum == MR0) {
                nds32_update_mmu_info(nds32);
-       } else if ((MR6 == mapped_regnum) || (MR7 == mapped_regnum)) {
+       } else if ((mapped_regnum == MR6) || (mapped_regnum == MR7)) {
                /* update lm information */
                nds32_update_lm_info(nds32);
-       } else if (MR8 == mapped_regnum) {
+       } else if (mapped_regnum == MR8) {
                nds32_update_cache_info(nds32);
-       } else if (FUCPR == mapped_regnum) {
+       } else if (mapped_regnum == FUCPR) {
                /* update audio/fpu setting */
                nds32_check_extension(nds32);
        }
@@ -425,7 +422,7 @@ static struct reg_cache *nds32_build_reg_cache(struct target *target,
 
                reg_list[i].reg_data_type = calloc(sizeof(struct reg_data_type), 1);
 
-               if (FD0 <= reg_arch_info[i].num && reg_arch_info[i].num <= FD31) {
+               if (reg_arch_info[i].num >= FD0 && reg_arch_info[i].num <= FD31) {
                        reg_list[i].value = reg_arch_info[i].value;
                        reg_list[i].type = &nds32_reg_access_type_64;
 
@@ -459,20 +456,20 @@ static struct reg_cache *nds32_build_reg_cache(struct target *target,
                        }
                }
 
-               if (R16 <= reg_arch_info[i].num && reg_arch_info[i].num <= R25)
+               if (reg_arch_info[i].num >= R16 && reg_arch_info[i].num <= R25)
                        reg_list[i].caller_save = true;
                else
                        reg_list[i].caller_save = false;
 
                reg_list[i].feature = malloc(sizeof(struct reg_feature));
 
-               if (R0 <= reg_arch_info[i].num && reg_arch_info[i].num <= IFC_LP)
+               if (reg_arch_info[i].num >= R0 && reg_arch_info[i].num <= IFC_LP)
                        reg_list[i].feature->name = "org.gnu.gdb.nds32.core";
-               else if (CR0 <= reg_arch_info[i].num && reg_arch_info[i].num <= SECUR0)
+               else if (reg_arch_info[i].num >= CR0 && reg_arch_info[i].num <= SECUR0)
                        reg_list[i].feature->name = "org.gnu.gdb.nds32.system";
-               else if (D0L24 <= reg_arch_info[i].num && reg_arch_info[i].num <= CBE3)
+               else if (reg_arch_info[i].num >= D0L24 && reg_arch_info[i].num <= CBE3)
                        reg_list[i].feature->name = "org.gnu.gdb.nds32.audio";
-               else if (FPCSR <= reg_arch_info[i].num && reg_arch_info[i].num <= FD31)
+               else if (reg_arch_info[i].num >= FPCSR && reg_arch_info[i].num <= FD31)
                        reg_list[i].feature->name = "org.gnu.gdb.nds32.fpu";
 
                cache->num_regs++;
@@ -997,7 +994,7 @@ int nds32_arch_state(struct target *target)
                        nds32->virtual_hosting ? ", virtual hosting" : "");
 
        /* save pc value to pseudo register pc */
-       struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
+       struct reg *reg = register_get_by_name(target->reg_cache, "pc", true);
        buf_set_u32(reg->value, 0, 32, value_pc);
 
        return ERROR_OK;
@@ -1134,7 +1131,7 @@ static void nds32_init_config(struct nds32 *nds32)
        misc_config->div_instruction = (value_cr4 >> 5) & 0x1;
        misc_config->mac_instruction = (value_cr4 >> 6) & 0x1;
        misc_config->audio_isa = (value_cr4 >> 7) & 0x3;
-       misc_config->L2_cache = (value_cr4 >> 9) & 0x1;
+       misc_config->l2_cache = (value_cr4 >> 9) & 0x1;
        misc_config->reduce_register = (value_cr4 >> 10) & 0x1;
        misc_config->addr_24 = (value_cr4 >> 11) & 0x1;
        misc_config->interruption_level = (value_cr4 >> 12) & 0x1;
@@ -1462,8 +1459,7 @@ int nds32_add_software_breakpoint(struct target *target,
                break_insn = NDS32_BREAK_32;
        }
 
-       if (breakpoint->orig_instr != NULL)
-               free(breakpoint->orig_instr);
+       free(breakpoint->orig_instr);
 
        breakpoint->orig_instr = malloc(breakpoint->length);
        memcpy(breakpoint->orig_instr, &data, breakpoint->length);
@@ -1549,7 +1545,7 @@ int nds32_restore_context(struct target *target)
                                                i, buf_get_u32(reg->value, 0, 32));
 
                                reg_arch_info = reg->arch_info;
-                               if (FD0 <= reg_arch_info->num && reg_arch_info->num <= FD31) {
+                               if (reg_arch_info->num >= FD0 && reg_arch_info->num <= FD31) {
                                        uint64_t val = buf_get_u64(reg_arch_info->value, 0, 64);
                                        aice_write_reg_64(aice, reg_arch_info->num, val);
                                } else {
@@ -1623,11 +1619,11 @@ int nds32_config(struct nds32 *nds32)
        uint32_t size_index;
        nds32_get_mapped_reg(nds32, MR6, &value_mr6);
        size_index = (value_mr6 >> 1) & 0xF;
-       nds32->memory.ilm_size = NDS32_LM_SIZE_TABLE[size_index];
+       nds32->memory.ilm_size = nds32_lm_size_table[size_index];
 
        nds32_get_mapped_reg(nds32, MR7, &value_mr7);
        size_index = (value_mr7 >> 1) & 0xF;
-       nds32->memory.dlm_size = NDS32_LM_SIZE_TABLE[size_index];
+       nds32->memory.dlm_size = nds32_lm_size_table[size_index];
 
        return ERROR_OK;
 }
@@ -1661,7 +1657,7 @@ int nds32_init_arch_info(struct target *target, struct nds32 *nds32)
        nds32->syscall_break.orig_instr = NULL;
        nds32->syscall_break.next = NULL;
        nds32->syscall_break.unique_id = 0x515CAll + target->target_number;
-       nds32->syscall_break.linked_BRP = 0;
+       nds32->syscall_break.linked_brp = 0;
 
        nds32_reg_init();
 
@@ -1698,8 +1694,8 @@ int nds32_cache_sync(struct target *target, target_addr_t address, uint32_t leng
        struct nds32 *nds32 = target_to_nds32(target);
        struct nds32_cache *dcache = &(nds32->memory.dcache);
        struct nds32_cache *icache = &(nds32->memory.icache);
-       uint32_t dcache_line_size = NDS32_LINE_SIZE_TABLE[dcache->line_size];
-       uint32_t icache_line_size = NDS32_LINE_SIZE_TABLE[icache->line_size];
+       uint32_t dcache_line_size = nds32_line_size_table[dcache->line_size];
+       uint32_t icache_line_size = nds32_line_size_table[icache->line_size];
        uint32_t cur_address;
        int result;
        uint32_t start_line, end_line;
@@ -1711,8 +1707,8 @@ int nds32_cache_sync(struct target *target, target_addr_t address, uint32_t leng
                /* (address + length - 1) / dcache_line_size */
                end_line = (address + length - 1) >> (dcache->line_size + 2);
 
-               for (cur_address = address, cur_line = start_line ;
-                               cur_line <= end_line ;
+               for (cur_address = address, cur_line = start_line;
+                               cur_line <= end_line;
                                cur_address += dcache_line_size, cur_line++) {
                        /* D$ write back */
                        result = aice_cache_ctl(aice, AICE_CACHE_CTL_L1D_VA_WB, cur_address);
@@ -1732,15 +1728,14 @@ int nds32_cache_sync(struct target *target, target_addr_t address, uint32_t leng
                /* (address + length - 1) / icache_line_size */
                end_line = (address + length - 1) >> (icache->line_size + 2);
 
-               for (cur_address = address, cur_line = start_line ;
-                               cur_line <= end_line ;
+               for (cur_address = address, cur_line = start_line;
+                               cur_line <= end_line;
                                cur_address += icache_line_size, cur_line++) {
                        /* Because PSW.IT is turned off under debug exception, address MUST
                         * be physical address.  L1I_VA_INVALIDATE uses PSW.IT to decide
                         * address translation or not. */
                        target_addr_t physical_addr;
-                       if (ERROR_FAIL == target->type->virt2phys(target, cur_address,
-                                               &physical_addr))
+                       if (target->type->virt2phys(target, cur_address, &physical_addr) == ERROR_FAIL)
                                return ERROR_FAIL;
 
                        /* I$ invalidate */
@@ -1930,8 +1925,7 @@ int nds32_examine_debug_reason(struct nds32 *nds32)
 
                                if (ERROR_OK != nds32_read_opcode(nds32, value_pc, &opcode))
                                        return ERROR_FAIL;
-                               if (ERROR_OK != nds32_evaluate_opcode(nds32, opcode, value_pc,
-                                                       &instruction))
+                               if (nds32_evaluate_opcode(nds32, opcode, value_pc, &instruction) != ERROR_OK)
                                        return ERROR_FAIL;
 
                                /* hit 'break 0x7FFF' */
@@ -1955,7 +1949,7 @@ int nds32_examine_debug_reason(struct nds32 *nds32)
                                nds32_step_without_watchpoint(nds32);
 
                                /* before single_step, save exception address */
-                               if (ERROR_OK != result)
+                               if (result != ERROR_OK)
                                        return ERROR_FAIL;
 
                                target->debug_reason = DBG_REASON_WATCHPOINT;
@@ -1970,8 +1964,7 @@ int nds32_examine_debug_reason(struct nds32 *nds32)
                case NDS32_DEBUG_DATA_VALUE_WATCHPOINT_IMPRECISE:
                case NDS32_DEBUG_DATA_ADDR_WATCHPOINT_NEXT_PRECISE:
                case NDS32_DEBUG_DATA_VALUE_WATCHPOINT_NEXT_PRECISE:
-                       if (ERROR_OK != nds32->get_watched_address(nds32,
-                                               &(nds32->watched_address), reason))
+                       if (nds32->get_watched_address(nds32, &(nds32->watched_address), reason) != ERROR_OK)
                                return ERROR_FAIL;
 
                        target->debug_reason = DBG_REASON_WATCHPOINT;
@@ -1998,7 +1991,7 @@ int nds32_login(struct nds32 *nds32)
 
        LOG_DEBUG("nds32_login");
 
-       if (nds32->edm_passcode != NULL) {
+       if (nds32->edm_passcode) {
                /* convert EDM passcode to command sequences */
                passcode_length = strlen(nds32->edm_passcode);
                command_sequence[0] = '\0';
@@ -2063,7 +2056,7 @@ int nds32_halt(struct target *target)
        if (nds32_target_state(nds32, &state) != ERROR_OK)
                return ERROR_FAIL;
 
-       if (TARGET_HALTED != state)
+       if (state != TARGET_HALTED)
                /* TODO: if state == TARGET_HALTED, check ETYPE is DBGI or not */
                if (ERROR_OK != aice_halt(aice))
                        return ERROR_FAIL;
@@ -2315,7 +2308,7 @@ int nds32_init(struct nds32 *nds32)
 int nds32_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fileio_info)
 {
        /* fill syscall parameters to file-I/O info */
-       if (NULL == fileio_info) {
+       if (!fileio_info) {
                LOG_ERROR("Target has not initial file-I/O data structure");
                return ERROR_FAIL;
        }
@@ -2334,69 +2327,70 @@ int nds32_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fil
        LOG_DEBUG("hit syscall ID: 0x%" PRIx32, syscall_id);
 
        /* free previous identifier storage */
-       if (NULL != fileio_info->identifier) {
-               free(fileio_info->identifier);
-               fileio_info->identifier = NULL;
-       }
+       free(fileio_info->identifier);
+       fileio_info->identifier = NULL;
+
+       uint32_t reg_r0, reg_r1, reg_r2;
+       nds32_get_mapped_reg(nds32, R0, &reg_r0);
+       nds32_get_mapped_reg(nds32, R1, &reg_r1);
+       nds32_get_mapped_reg(nds32, R2, &reg_r2);
 
        switch (syscall_id) {
                case NDS32_SYSCALL_EXIT:
                        fileio_info->identifier = malloc(5);
                        sprintf(fileio_info->identifier, "exit");
-                       nds32_get_mapped_reg(nds32, R0, &(fileio_info->param_1));
+                       fileio_info->param_1 = reg_r0;
                        break;
                case NDS32_SYSCALL_OPEN:
                        {
                                uint8_t filename[256];
                                fileio_info->identifier = malloc(5);
                                sprintf(fileio_info->identifier, "open");
-                               nds32_get_mapped_reg(nds32, R0, &(fileio_info->param_1));
+                               fileio_info->param_1 = reg_r0;
                                /* reserve fileio_info->param_2 for length of path */
-                               nds32_get_mapped_reg(nds32, R1, &(fileio_info->param_3));
-                               nds32_get_mapped_reg(nds32, R2, &(fileio_info->param_4));
+                               fileio_info->param_3 = reg_r1;
+                               fileio_info->param_4 = reg_r2;
 
-                               target->type->read_buffer(target, fileio_info->param_1,
-                                               256, filename);
-                               fileio_info->param_2 = strlen((char *)filename) + 1;
+                               target->type->read_buffer(target, reg_r0, 256, filename);
+                               fileio_info->param_2 = strlen((char *)filename);
                        }
                        break;
                case NDS32_SYSCALL_CLOSE:
                        fileio_info->identifier = malloc(6);
                        sprintf(fileio_info->identifier, "close");
-                       nds32_get_mapped_reg(nds32, R0, &(fileio_info->param_1));
+                       fileio_info->param_1 = reg_r0;
                        break;
                case NDS32_SYSCALL_READ:
                        fileio_info->identifier = malloc(5);
                        sprintf(fileio_info->identifier, "read");
-                       nds32_get_mapped_reg(nds32, R0, &(fileio_info->param_1));
-                       nds32_get_mapped_reg(nds32, R1, &(fileio_info->param_2));
-                       nds32_get_mapped_reg(nds32, R2, &(fileio_info->param_3));
+                       fileio_info->param_1 = reg_r0;
+                       fileio_info->param_2 = reg_r1;
+                       fileio_info->param_3 = reg_r2;
                        break;
                case NDS32_SYSCALL_WRITE:
                        fileio_info->identifier = malloc(6);
                        sprintf(fileio_info->identifier, "write");
-                       nds32_get_mapped_reg(nds32, R0, &(fileio_info->param_1));
-                       nds32_get_mapped_reg(nds32, R1, &(fileio_info->param_2));
-                       nds32_get_mapped_reg(nds32, R2, &(fileio_info->param_3));
+                       fileio_info->param_1 = reg_r0;
+                       fileio_info->param_2 = reg_r1;
+                       fileio_info->param_3 = reg_r2;
                        break;
                case NDS32_SYSCALL_LSEEK:
                        fileio_info->identifier = malloc(6);
                        sprintf(fileio_info->identifier, "lseek");
-                       nds32_get_mapped_reg(nds32, R0, &(fileio_info->param_1));
-                       nds32_get_mapped_reg(nds32, R1, &(fileio_info->param_2));
-                       nds32_get_mapped_reg(nds32, R2, &(fileio_info->param_3));
+                       fileio_info->param_1 = reg_r0;
+                       fileio_info->param_2 = reg_r1;
+                       fileio_info->param_3 = reg_r2;
                        break;
                case NDS32_SYSCALL_UNLINK:
                        {
                                uint8_t filename[256];
                                fileio_info->identifier = malloc(7);
                                sprintf(fileio_info->identifier, "unlink");
-                               nds32_get_mapped_reg(nds32, R0, &(fileio_info->param_1));
+                               fileio_info->param_1 = reg_r0;
                                /* reserve fileio_info->param_2 for length of path */
 
-                               target->type->read_buffer(target, fileio_info->param_1,
-                                               256, filename);
-                               fileio_info->param_2 = strlen((char *)filename) + 1;
+                               target->type->read_buffer(target, reg_r0, 256, filename);
+                               fileio_info->param_2 = strlen((char *)filename);
                        }
                        break;
                case NDS32_SYSCALL_RENAME:
@@ -2404,62 +2398,58 @@ int nds32_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fil
                                uint8_t filename[256];
                                fileio_info->identifier = malloc(7);
                                sprintf(fileio_info->identifier, "rename");
-                               nds32_get_mapped_reg(nds32, R0, &(fileio_info->param_1));
+                               fileio_info->param_1 = reg_r0;
                                /* reserve fileio_info->param_2 for length of old path */
-                               nds32_get_mapped_reg(nds32, R1, &(fileio_info->param_3));
+                               fileio_info->param_3 = reg_r1;
                                /* reserve fileio_info->param_4 for length of new path */
 
-                               target->type->read_buffer(target, fileio_info->param_1,
-                                               256, filename);
-                               fileio_info->param_2 = strlen((char *)filename) + 1;
+                               target->type->read_buffer(target, reg_r0, 256, filename);
+                               fileio_info->param_2 = strlen((char *)filename);
 
-                               target->type->read_buffer(target, fileio_info->param_3,
-                                               256, filename);
-                               fileio_info->param_4 = strlen((char *)filename) + 1;
+                               target->type->read_buffer(target, reg_r1, 256, filename);
+                               fileio_info->param_4 = strlen((char *)filename);
                        }
                        break;
                case NDS32_SYSCALL_FSTAT:
                        fileio_info->identifier = malloc(6);
                        sprintf(fileio_info->identifier, "fstat");
-                       nds32_get_mapped_reg(nds32, R0, &(fileio_info->param_1));
-                       nds32_get_mapped_reg(nds32, R1, &(fileio_info->param_2));
+                       fileio_info->param_1 = reg_r0;
+                       fileio_info->param_2 = reg_r1;
                        break;
                case NDS32_SYSCALL_STAT:
                        {
                                uint8_t filename[256];
                                fileio_info->identifier = malloc(5);
                                sprintf(fileio_info->identifier, "stat");
-                               nds32_get_mapped_reg(nds32, R0, &(fileio_info->param_1));
+                               fileio_info->param_1 = reg_r0;
                                /* reserve fileio_info->param_2 for length of old path */
-                               nds32_get_mapped_reg(nds32, R1, &(fileio_info->param_3));
+                               fileio_info->param_3 = reg_r1;
 
-                               target->type->read_buffer(target, fileio_info->param_1,
-                                               256, filename);
+                               target->type->read_buffer(target, reg_r0, 256, filename);
                                fileio_info->param_2 = strlen((char *)filename) + 1;
                        }
                        break;
                case NDS32_SYSCALL_GETTIMEOFDAY:
                        fileio_info->identifier = malloc(13);
                        sprintf(fileio_info->identifier, "gettimeofday");
-                       nds32_get_mapped_reg(nds32, R0, &(fileio_info->param_1));
-                       nds32_get_mapped_reg(nds32, R1, &(fileio_info->param_2));
+                       fileio_info->param_1 = reg_r0;
+                       fileio_info->param_2 = reg_r1;
                        break;
                case NDS32_SYSCALL_ISATTY:
                        fileio_info->identifier = malloc(7);
                        sprintf(fileio_info->identifier, "isatty");
-                       nds32_get_mapped_reg(nds32, R0, &(fileio_info->param_1));
+                       fileio_info->param_1 = reg_r0;
                        break;
                case NDS32_SYSCALL_SYSTEM:
                        {
                                uint8_t command[256];
                                fileio_info->identifier = malloc(7);
                                sprintf(fileio_info->identifier, "system");
-                               nds32_get_mapped_reg(nds32, R0, &(fileio_info->param_1));
+                               fileio_info->param_1 = reg_r0;
                                /* reserve fileio_info->param_2 for length of old path */
 
-                               target->type->read_buffer(target, fileio_info->param_1,
-                                               256, command);
-                               fileio_info->param_2 = strlen((char *)command) + 1;
+                               target->type->read_buffer(target, reg_r0, 256, command);
+                               fileio_info->param_2 = strlen((char *)command);
                        }
                        break;
                case NDS32_SYSCALL_ERRNO:
@@ -2500,6 +2490,12 @@ int nds32_profiling(struct target *target, uint32_t *samples,
        struct aice_port_s *aice = target_to_aice(target);
        struct nds32 *nds32 = target_to_nds32(target);
 
+       /* REVISIT: can nds32 profile without halting? */
+       if (target->state != TARGET_HALTED) {
+               LOG_WARNING("target %s is not halted (profiling)", target->cmd_name);
+               return ERROR_TARGET_NOT_HALTED;
+       }
+
        if (max_num_samples < iteration)
                iteration = max_num_samples;
 

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)