1
2
3
10
11
12
13
14
15
16
17
18
19
20
21
22
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
136
137
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
168
169
170
171
172
173
174
175
176
177
178
182
183
184
188
189
199
200
201
202
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
253
254
255
256
257
258
259
260
261
265
266
267
268
269
270
276
277
278
279
280
281
282
283
284
305
306
307
308
309
310
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "arc.h"
static bool arc_mem_is_slow_memory(struct arc_common *arc, uint32_t addr,
uint32_t size, uint32_t count)
{
uint32_t addr_end = addr + size * count;
/* ... */
assert(addr_end >= addr || addr_end == 0);
return !((addr >= arc->dccm_start && addr_end <= arc->dccm_end) ||
(addr >= arc->iccm0_start && addr_end <= arc->iccm0_end) ||
(addr >= arc->iccm1_start && addr_end <= arc->iccm1_end));
}{ ... }
static int arc_mem_write_block32(struct target *target, uint32_t addr,
uint32_t count, void *buf)
{
struct arc_common *arc = target_to_arc(target);
LOG_DEBUG("Write 4-byte memory block: addr=0x%08" PRIx32 ", count=%" PRIu32,
addr, count);
assert(!(addr & 3));
/* ... */
CHECK_RETVAL(arc_cache_flush(target));
CHECK_RETVAL(arc_jtag_write_memory(&arc->jtag_info, addr, count,
(uint32_t *)buf));
CHECK_RETVAL(arc_cache_invalidate(target));
return ERROR_OK;
}{ ... }
static int arc_mem_write_block16(struct target *target, uint32_t addr,
uint32_t count, void *buf)
{
struct arc_common *arc = target_to_arc(target);
uint32_t i;
uint32_t buffer_he;
uint8_t buffer_te[sizeof(uint32_t)];
uint8_t halfword_te[sizeof(uint16_t)];
LOG_DEBUG("Write 2-byte memory block: addr=0x%08" PRIx32 ", count=%" PRIu32,
addr, count);
assert(!(addr & 1));
CHECK_RETVAL(arc_cache_flush(target));
/* ... */
for (i = 0; i < count; i++) {
/* ... */
bool is_slow_memory = arc_mem_is_slow_memory(arc,
(addr + i * sizeof(uint16_t)) & ~3u, 4, 1);
CHECK_RETVAL(arc_jtag_read_memory(&arc->jtag_info,
(addr + i * sizeof(uint16_t)) & ~3u, 1, &buffer_he,
is_slow_memory));
target_buffer_set_u32(target, buffer_te, buffer_he);
target_buffer_set_u16(target, halfword_te, ((uint16_t *)buf)[i]);
memcpy(buffer_te + ((addr + i * sizeof(uint16_t)) & 3u),
halfword_te, sizeof(uint16_t));
buffer_he = target_buffer_get_u32(target, buffer_te);
CHECK_RETVAL(arc_jtag_write_memory(&arc->jtag_info,
(addr + i * sizeof(uint16_t)) & ~3u, 1, &buffer_he));
}for (i = 0; i < count; i++) { ... }
CHECK_RETVAL(arc_cache_invalidate(target));
return ERROR_OK;
}{ ... }
static int arc_mem_write_block8(struct target *target, uint32_t addr,
uint32_t count, void *buf)
{
struct arc_common *arc = target_to_arc(target);
uint32_t i;
uint32_t buffer_he;
uint8_t buffer_te[sizeof(uint32_t)];
LOG_DEBUG("Write 1-byte memory block: addr=0x%08" PRIx32 ", count=%" PRIu32,
addr, count);
CHECK_RETVAL(arc_cache_flush(target));
/* ... */
for (i = 0; i < count; i++) {
/* ... */
CHECK_RETVAL(arc_jtag_read_memory(&arc->jtag_info, (addr + i) & ~3, 1, &buffer_he,
arc_mem_is_slow_memory(arc, (addr + i) & ~3, 4, 1)));
target_buffer_set_u32(target, buffer_te, buffer_he);
memcpy(buffer_te + ((addr + i) & 3), (uint8_t *)buf + i, 1);
buffer_he = target_buffer_get_u32(target, buffer_te);
CHECK_RETVAL(arc_jtag_write_memory(&arc->jtag_info, (addr + i) & ~3, 1, &buffer_he));
}for (i = 0; i < count; i++) { ... }
CHECK_RETVAL(arc_cache_invalidate(target));
return ERROR_OK;
}{ ... }
----- Supporting functions
int arc_mem_write(struct target *target, target_addr_t address, uint32_t size,
uint32_t count, const uint8_t *buffer)
{
int retval = ERROR_OK;
void *tunnel = NULL;
LOG_DEBUG("address: 0x%08" TARGET_PRIxADDR ", size: %" PRIu32 ", count: %" PRIu32,
address, size, count);
if (target->state != TARGET_HALTED) {
LOG_TARGET_ERROR(target, "not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
if (((size != 4) && (size != 2) && (size != 1)) || !(count) || !(buffer))
return ERROR_COMMAND_SYNTAX_ERROR;
if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
return ERROR_TARGET_UNALIGNED_ACCESS;
if (size > 1) {
/* ... */
tunnel = calloc(1, count * size * sizeof(uint8_t));
if (!tunnel) {
LOG_ERROR("Unable to allocate memory");
return ERROR_FAIL;
}if (!tunnel) { ... }
switch (size) {
case 4:
target_buffer_get_u32_array(target, buffer, count,
(uint32_t *)tunnel);
break;case 4:
case 2:
target_buffer_get_u16_array(target, buffer, count,
(uint16_t *)tunnel);
break;case 2:
}switch (size) { ... }
buffer = tunnel;
}if (size > 1) { ... }
if (size == 4) {
retval = arc_mem_write_block32(target, address, count, (void *)buffer);
}if (size == 4) { ... } else if (size == 2) {
/* ... */
retval = arc_mem_write_block16(target, address, count, (void *)buffer);
}else if (size == 2) { ... } else {
retval = arc_mem_write_block8(target, address, count, (void *)buffer);
}else { ... }
free(tunnel);
return retval;
}{ ... }
static int arc_mem_read_block(struct target *target, target_addr_t addr,
uint32_t size, uint32_t count, void *buf)
{
struct arc_common *arc = target_to_arc(target);
LOG_DEBUG("Read memory: addr=0x%08" TARGET_PRIxADDR ", size=%" PRIu32
", count=%" PRIu32, addr, size, count);
assert(!(addr & 3));
assert(size == 4);
CHECK_RETVAL(arc_cache_flush(target));
CHECK_RETVAL(arc_jtag_read_memory(&arc->jtag_info, addr, count, buf,
arc_mem_is_slow_memory(arc, addr, size, count)));
return ERROR_OK;
}{ ... }
int arc_mem_read(struct target *target, target_addr_t address, uint32_t size,
uint32_t count, uint8_t *buffer)
{
int retval = ERROR_OK;
void *tunnel_he;
uint8_t *tunnel_te;
uint32_t words_to_read, bytes_to_read;
LOG_DEBUG("Read memory: addr=0x%08" TARGET_PRIxADDR ", size=%" PRIu32
", count=%" PRIu32, address, size, count);
if (target->state != TARGET_HALTED) {
LOG_WARNING("target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
if (((size != 4) && (size != 2) && (size != 1)) || !(count) || !(buffer))
return ERROR_COMMAND_SYNTAX_ERROR;
if (((size == 4) && (address & 0x3u)) || ((size == 2) && (address & 0x1u)))
return ERROR_TARGET_UNALIGNED_ACCESS;
/* ... */
bytes_to_read = (count * size + 3 + (address & 3u)) & ~3u;
words_to_read = bytes_to_read >> 2;
tunnel_he = calloc(1, bytes_to_read);
tunnel_te = calloc(1, bytes_to_read);
if (!tunnel_he || !tunnel_te) {
LOG_ERROR("Unable to allocate memory");
free(tunnel_he);
free(tunnel_te);
return ERROR_FAIL;
}if (!tunnel_he || !tunnel_te) { ... }
retval = arc_mem_read_block(target, address & ~3u, sizeof(uint32_t),
words_to_read, tunnel_he);
if (retval == ERROR_OK) {
switch (size) {
case 4:
target_buffer_set_u32_array(target, buffer, count,
tunnel_he);
break;case 4:
case 2:
target_buffer_set_u32_array(target, tunnel_te,
words_to_read, tunnel_he);
memcpy(buffer, tunnel_te + (address & 3u),
count * sizeof(uint16_t));
break;case 2:
case 1:
target_buffer_set_u32_array(target, tunnel_te,
words_to_read, tunnel_he);
memcpy(buffer, tunnel_te + (address & 3u), count);
break;case 1:
}switch (size) { ... }
}if (retval == ERROR_OK) { ... }
free(tunnel_he);
free(tunnel_te);
return retval;
}{ ... }