1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
26
27
28
29
30
31
32
33
34
35
36
37
38
39
42
47
48
56
57
58
59
63
64
68
69
70
71
72
73
74
75
76
77
83
84
92
93
94
95
96
97
102
103
107
108
109
110
111
112
113
117
118
119
123
124
125
126
127
128
129
142
143
144
145
146
147
148
149
153
157
158
159
160
161
162
164
165
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
187
188
189
190
191
/* ... */
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <helper/log.h>
#include "target/target.h"
#include "riscv.h"
static int riscv_semihosting_setup(struct target *target, int enable);
static int riscv_semihosting_post_result(struct target *target);
/* ... */
void riscv_semihosting_init(struct target *target)
{
semihosting_common_init(target, riscv_semihosting_setup,
riscv_semihosting_post_result);
}{ ... }
/* ... */
enum semihosting_result riscv_semihosting(struct target *target, int *retval)
{
struct semihosting *semihosting = target->semihosting;
if (!semihosting) {
LOG_DEBUG(" -> NONE (!semihosting)");
return SEMIHOSTING_NONE;
}if (!semihosting) { ... }
if (!semihosting->is_active) {
LOG_DEBUG(" -> NONE (!semihosting->is_active)");
return SEMIHOSTING_NONE;
}if (!semihosting->is_active) { ... }
riscv_reg_t pc;
int result = riscv_get_register(target, &pc, GDB_REGNO_PC);
if (result != ERROR_OK)
return SEMIHOSTING_ERROR;
uint8_t tmp_buf[12];
for (int i = 0; i < 3; i++) {
*retval = riscv_read_by_any_size(target, (pc - 4) + 4 * i, 4, tmp_buf + 4 * i);
if (*retval != ERROR_OK)
return SEMIHOSTING_ERROR;
}for (int i = 0; i < 3; i++) { ... }
/* ... */
uint32_t pre = target_buffer_get_u32(target, tmp_buf);
uint32_t ebreak = target_buffer_get_u32(target, tmp_buf + 4);
uint32_t post = target_buffer_get_u32(target, tmp_buf + 8);
LOG_DEBUG("check %08x %08x %08x from 0x%" PRIx64 "-4", pre, ebreak, post, pc);
if (pre != 0x01f01013 || ebreak != 0x00100073 || post != 0x40705013) {
LOG_DEBUG(" -> NONE (no magic)");
return SEMIHOSTING_NONE;
}if (pre != 0x01f01013 || ebreak != 0x00100073 || post != 0x40705013) { ... }
/* ... */
if (!semihosting->hit_fileio) {
riscv_reg_t r0;
riscv_reg_t r1;
result = riscv_get_register(target, &r0, GDB_REGNO_A0);
if (result != ERROR_OK) {
LOG_DEBUG(" -> ERROR (couldn't read a0)");
return SEMIHOSTING_ERROR;
}if (result != ERROR_OK) { ... }
result = riscv_get_register(target, &r1, GDB_REGNO_A1);
if (result != ERROR_OK) {
LOG_DEBUG(" -> ERROR (couldn't read a1)");
return SEMIHOSTING_ERROR;
}if (result != ERROR_OK) { ... }
semihosting->op = r0;
semihosting->param = r1;
semihosting->word_size_bytes = riscv_xlen(target) / 8;
if ((semihosting->op >= 0 && semihosting->op <= 0x31) ||
(semihosting->op >= 0x100 && semihosting->op <= 0x107)) {
*retval = semihosting_common(target);
if (*retval != ERROR_OK) {
LOG_ERROR("Failed semihosting operation (0x%02X)", semihosting->op);
return SEMIHOSTING_ERROR;
}if (*retval != ERROR_OK) { ... }
}if ((semihosting->op >= 0 && semihosting->op <= 0x31) || (semihosting->op >= 0x100 && semihosting->op <= 0x107)) { ... } else {
LOG_DEBUG(" -> NONE (unknown operation number)");
return SEMIHOSTING_NONE;
}else { ... }
}if (!semihosting->hit_fileio) { ... }
*retval = riscv_set_register(target, GDB_REGNO_PC, pc + 4);
if (*retval != ERROR_OK)
return SEMIHOSTING_ERROR;
/* ... */
if (semihosting->is_resumable && !semihosting->hit_fileio) {
LOG_DEBUG(" -> HANDLED");
return SEMIHOSTING_HANDLED;
}if (semihosting->is_resumable && !semihosting->hit_fileio) { ... }
LOG_DEBUG(" -> WAITING");
return SEMIHOSTING_WAITING;
}{ ... }
/* ... */
/* ... */
static int riscv_semihosting_setup(struct target *target, int enable)
{
LOG_DEBUG("[%s] enable=%d", target_name(target), enable);
struct semihosting *semihosting = target->semihosting;
if (semihosting)
semihosting->setup_time = clock();
return ERROR_OK;
}{ ... }
static int riscv_semihosting_post_result(struct target *target)
{
struct semihosting *semihosting = target->semihosting;
if (!semihosting) {
return 0;
}if (!semihosting) { ... }
LOG_DEBUG("0x%" PRIx64, semihosting->result);
riscv_set_register(target, GDB_REGNO_A0, semihosting->result);
return 0;
}{ ... }