1
2
3
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
34
35
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
61
62
63
64
65
66
67
68
69
70
71
72
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
99
103
109
115
121
128
133
139
144
149
153
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
184
191
192
193
194
195
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "xtensa_chip.h"
#include "xtensa_fileio.h"
#include "xtensa.h"
#define XTENSA_SYSCALL(x) XT_INS_BREAK(x, 1, 14)
#define XTENSA_SYSCALL_SZ 3
#define XTENSA_SYSCALL_LEN_MAX 255
int xtensa_fileio_init(struct target *target)
{
char *idmem = malloc(XTENSA_SYSCALL_LEN_MAX + 1);
target->fileio_info = malloc(sizeof(struct gdb_fileio_info));
if (!idmem || !target->fileio_info) {
LOG_TARGET_ERROR(target, "Out of memory!");
free(idmem);
free(target->fileio_info);
return ERROR_FAIL;
}if (!idmem || !target->fileio_info) { ... }
target->fileio_info->identifier = idmem;
return ERROR_OK;
}{ ... }
/* ... */
int xtensa_fileio_detect_proc(struct target *target)
{
struct xtensa *xtensa = target_to_xtensa(target);
int retval;
xtensa_reg_val_t dbg_cause = xtensa_cause_get(target);
if ((dbg_cause & (DEBUGCAUSE_BI | DEBUGCAUSE_BN)) == 0 || xtensa->halt_request)
return ERROR_FAIL;
uint8_t brk_insn_buf[sizeof(uint32_t)] = {0};
xtensa_reg_val_t pc = xtensa_reg_get(target, XT_REG_IDX_PC);
retval = target_read_memory(target,
pc,
XTENSA_SYSCALL_SZ,
1,
(uint8_t *)brk_insn_buf);
if (retval != ERROR_OK) {
LOG_ERROR("Failed to read break instruction!");
return ERROR_FAIL;
}if (retval != ERROR_OK) { ... }
if (buf_get_u32(brk_insn_buf, 0, 32) != XTENSA_SYSCALL(xtensa))
return ERROR_FAIL;
LOG_TARGET_DEBUG(target, "File-I/O: syscall breakpoint found at 0x%x", pc);
xtensa->proc_syscall = true;
return ERROR_OK;
}{ ... }
int xtensa_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fileio_info)
{
if (!fileio_info) {
LOG_ERROR("File-I/O data structure uninitialized");
return ERROR_FAIL;
}if (!fileio_info) { ... }
struct xtensa *xtensa = target_to_xtensa(target);
if (!xtensa->proc_syscall)
return ERROR_FAIL;
xtensa_reg_val_t syscall = xtensa_reg_get(target, XTENSA_SYSCALL_OP_REG);
xtensa_reg_val_t arg0 = xtensa_reg_get(target, XT_REG_IDX_A6);
xtensa_reg_val_t arg1 = xtensa_reg_get(target, XT_REG_IDX_A3);
xtensa_reg_val_t arg2 = xtensa_reg_get(target, XT_REG_IDX_A4);
xtensa_reg_val_t arg3 = xtensa_reg_get(target, XT_REG_IDX_A5);
int retval = ERROR_OK;
LOG_TARGET_DEBUG(target, "File-I/O: syscall 0x%x 0x%x 0x%x 0x%x 0x%x",
syscall, arg0, arg1, arg2, arg3);
switch (syscall) {
case XTENSA_SYSCALL_OPEN:
snprintf(fileio_info->identifier, XTENSA_SYSCALL_LEN_MAX, "open");
fileio_info->param_1 = arg0;
fileio_info->param_2 = arg3;
fileio_info->param_3 = arg1;
fileio_info->param_4 = arg2;
break;case XTENSA_SYSCALL_OPEN:
case XTENSA_SYSCALL_CLOSE:
snprintf(fileio_info->identifier, XTENSA_SYSCALL_LEN_MAX, "close");
fileio_info->param_1 = arg0;
break;case XTENSA_SYSCALL_CLOSE:
case XTENSA_SYSCALL_READ:
snprintf(fileio_info->identifier, XTENSA_SYSCALL_LEN_MAX, "read");
fileio_info->param_1 = arg0;
fileio_info->param_2 = arg1;
fileio_info->param_3 = arg2;
break;case XTENSA_SYSCALL_READ:
case XTENSA_SYSCALL_WRITE:
snprintf(fileio_info->identifier, XTENSA_SYSCALL_LEN_MAX, "write");
fileio_info->param_1 = arg0;
fileio_info->param_2 = arg1;
fileio_info->param_3 = arg2;
break;case XTENSA_SYSCALL_WRITE:
case XTENSA_SYSCALL_LSEEK:
snprintf(fileio_info->identifier, XTENSA_SYSCALL_LEN_MAX, "lseek");
fileio_info->param_1 = arg0;
fileio_info->param_2 = arg1;
fileio_info->param_3 = arg2;
break;case XTENSA_SYSCALL_LSEEK:
case XTENSA_SYSCALL_RENAME:
snprintf(fileio_info->identifier, XTENSA_SYSCALL_LEN_MAX, "rename");
fileio_info->param_1 = arg0;
fileio_info->param_2 = arg3;
fileio_info->param_3 = arg1;
fileio_info->param_4 = arg2;
break;case XTENSA_SYSCALL_RENAME:
case XTENSA_SYSCALL_UNLINK:
snprintf(fileio_info->identifier, XTENSA_SYSCALL_LEN_MAX, "unlink");
fileio_info->param_1 = arg0;
fileio_info->param_2 = arg1;
break;case XTENSA_SYSCALL_UNLINK:
case XTENSA_SYSCALL_STAT:
snprintf(fileio_info->identifier, XTENSA_SYSCALL_LEN_MAX, "stat");
fileio_info->param_1 = arg0;
fileio_info->param_2 = arg2;
fileio_info->param_3 = arg1;
break;case XTENSA_SYSCALL_STAT:
case XTENSA_SYSCALL_FSTAT:
snprintf(fileio_info->identifier, XTENSA_SYSCALL_LEN_MAX, "fstat");
fileio_info->param_1 = arg0;
fileio_info->param_2 = arg1;
break;case XTENSA_SYSCALL_FSTAT:
case XTENSA_SYSCALL_GETTIMEOFDAY:
snprintf(fileio_info->identifier, XTENSA_SYSCALL_LEN_MAX, "gettimeofday");
fileio_info->param_1 = arg0;
fileio_info->param_2 = arg1;
break;case XTENSA_SYSCALL_GETTIMEOFDAY:
case XTENSA_SYSCALL_ISATTY:
snprintf(fileio_info->identifier, XTENSA_SYSCALL_LEN_MAX, "isatty");
fileio_info->param_1 = arg0;
break;case XTENSA_SYSCALL_ISATTY:
case XTENSA_SYSCALL_SYSTEM:
snprintf(fileio_info->identifier, XTENSA_SYSCALL_LEN_MAX, "system");
fileio_info->param_1 = arg0;
fileio_info->param_2 = arg1;
break;case XTENSA_SYSCALL_SYSTEM:
default:
snprintf(fileio_info->identifier, XTENSA_SYSCALL_LEN_MAX, "unknown");
LOG_TARGET_DEBUG(target, "File-I/O: syscall unknown (%d), pc=0x%08X",
syscall, xtensa_reg_get(target, XT_REG_IDX_PC));
LOG_INFO("File-I/O: syscall unknown (%d), pc=0x%08X",
syscall, xtensa_reg_get(target, XT_REG_IDX_PC));
retval = ERROR_FAIL;
break;default
}switch (syscall) { ... }
return retval;
}{ ... }
int xtensa_gdb_fileio_end(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
{
struct xtensa *xtensa = target_to_xtensa(target);
if (!xtensa->proc_syscall)
return ERROR_FAIL;
LOG_TARGET_DEBUG(target, "File-I/O: syscall return code: 0x%x, errno: 0x%x , ctrl_c: %s",
retcode, fileio_errno, ctrl_c ? "true" : "false");
/* ... */
if (fileio_errno != 4) {
xtensa_reg_set_deep_relgen(target, XTENSA_SYSCALL_RETVAL_REG, retcode);
xtensa_reg_set_deep_relgen(target, XTENSA_SYSCALL_ERRNO_REG, fileio_errno);
xtensa_reg_val_t pc = xtensa_reg_get(target, XT_REG_IDX_PC);
xtensa_reg_set(target, XT_REG_IDX_PC, pc + XTENSA_SYSCALL_SZ);
}if (fileio_errno != 4) { ... }
xtensa->proc_syscall = false;
xtensa->halt_request = true;
return ctrl_c ? ERROR_FAIL : ERROR_OK;
}{ ... }