Skip to content

Commit b078514

Browse files
committed
target/riscv: add is_virtual parameter to memory access method
Added is_virtual parameter to memory access method. This change is required to support hw translation appropriately. Updated repeat_read documentation according to new behaviour Change-Id: I6970b4dd9d57ff5c032a6c435358003e9a66d21c Signed-off-by: Farid Khaydari <f.khaydari@syntacore.com>
1 parent 31175e5 commit b078514

File tree

5 files changed

+51
-23
lines changed

5 files changed

+51
-23
lines changed

doc/openocd.texi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11522,9 +11522,9 @@ dump_sample_buf}.
1152211522
@end deffn
1152311523

1152411524
@deffn {Command} {riscv repeat_read} count address [size=4]
11525-
Quickly read count words of the given size from address. This can be useful
11526-
to read out a buffer that's memory-mapped to be accessed through a single
11527-
address, or to sample a changing value in a memory-mapped device.
11525+
Quickly read count words of the given size from physical address. This can
11526+
be useful to read out a buffer that's memory-mapped to be accessed through
11527+
a single address, or to sample a changing value in a memory-mapped device.
1152811528
@end deffn
1152911529

1153011530
@deffn {Command} {riscv info}

src/target/riscv/riscv-011.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2339,7 +2339,8 @@ static int write_memory(struct target *target, const riscv_mem_access_args_t arg
23392339
return ERROR_FAIL;
23402340
}
23412341

2342-
static int access_memory(struct target *target, const riscv_mem_access_args_t args)
2342+
static int access_memory(struct target *target,
2343+
const riscv_mem_access_args_t args, const bool is_virtual)
23432344
{
23442345
assert(riscv_mem_access_is_valid(args));
23452346
const bool is_write = riscv_mem_access_is_write(args);

src/target/riscv/riscv-013.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ static int register_read_direct(struct target *target, riscv_reg_t *value,
6464
enum gdb_regno number);
6565
static int register_write_direct(struct target *target, enum gdb_regno number,
6666
riscv_reg_t value);
67-
static int riscv013_access_memory(struct target *target, const riscv_mem_access_args_t args);
67+
static int riscv013_access_memory(struct target *target,
68+
const riscv_mem_access_args_t args, const bool is_virtual);
6869
static bool riscv013_get_impebreak(const struct target *target);
6970
static unsigned int riscv013_get_progbufsize(const struct target *target);
7071

@@ -1277,7 +1278,8 @@ static int scratch_read64(struct target *target, scratch_mem_t *scratch,
12771278
.count = 2,
12781279
.increment = 4,
12791280
};
1280-
if (riscv013_access_memory(target, args) != ERROR_OK)
1281+
if (riscv013_access_memory(target, args,
1282+
/* is_virtual */ false) != ERROR_OK)
12811283
return ERROR_FAIL;
12821284
*value = buf_get_u64(buffer,
12831285
/* first = */ 0, /* bit_num = */ 64);
@@ -1319,7 +1321,8 @@ static int scratch_write64(struct target *target, scratch_mem_t *scratch,
13191321
.count = 2,
13201322
.increment = 4,
13211323
};
1322-
if (riscv013_access_memory(target, args) != ERROR_OK)
1324+
if (riscv013_access_memory(target, args,
1325+
/* is_virtual */ false) != ERROR_OK)
13231326
return ERROR_FAIL;
13241327
}
13251328
break;
@@ -4551,8 +4554,8 @@ access_memory_abstract(struct target *target, const riscv_mem_access_args_t args
45514554
write_memory_abstract(target, args);
45524555
}
45534556

4554-
static int
4555-
riscv013_access_memory(struct target *target, const riscv_mem_access_args_t args)
4557+
static int riscv013_access_memory(struct target *target,
4558+
const riscv_mem_access_args_t args, const bool is_virtual)
45564559
{
45574560
assert(riscv_mem_access_is_valid(args));
45584561

@@ -4580,12 +4583,15 @@ riscv013_access_memory(struct target *target, const riscv_mem_access_args_t args
45804583
riscv_mem_access_method_t method = r->mem_access_methods[i];
45814584
switch (method) {
45824585
case RISCV_MEM_ACCESS_PROGBUF:
4586+
// TODO: pass is_virtual here in future commits
45834587
skip_reason[method] = access_memory_progbuf(target, args);
45844588
break;
45854589
case RISCV_MEM_ACCESS_SYSBUS:
4590+
// TODO: pass is_virtual here in future commits
45864591
skip_reason[method] = access_memory_sysbus(target, args);
45874592
break;
45884593
case RISCV_MEM_ACCESS_ABSTRACT:
4594+
// TODO: pass is_virtual here in future commits
45894595
skip_reason[method] = access_memory_abstract(target, args);
45904596
break;
45914597
default:

src/target/riscv/riscv.c

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ bool riscv_virt2phys_mode_is_sw(const struct target *target)
155155
return r->virt2phys_mode == RISCV_VIRT2PHYS_MODE_SW;
156156
}
157157

158+
bool riscv_virt2phys_mode_is_off(const struct target *target)
159+
{
160+
assert(target);
161+
RISCV_INFO(r);
162+
return r->virt2phys_mode == RISCV_VIRT2PHYS_MODE_OFF;
163+
}
164+
158165
const char *riscv_virt2phys_mode_to_str(riscv_virt2phys_mode_t mode)
159166
{
160167
assert(mode == RISCV_VIRT2PHYS_MODE_OFF
@@ -3149,7 +3156,7 @@ static int riscv_address_translate(struct target *target,
31493156
.increment = 4,
31503157
.count = (1 << info->pte_shift) / 4,
31513158
};
3152-
int retval = r->access_memory(target, args);
3159+
int retval = r->access_memory(target, args, /* is_virtual */ false);
31533160
if (retval != ERROR_OK)
31543161
return ERROR_FAIL;
31553162

@@ -3382,6 +3389,14 @@ static int check_virt_memory_access(struct target *target, target_addr_t address
33823389
return ERROR_OK;
33833390
}
33843391

3392+
static int riscv_access_phys_memory(struct target *target,
3393+
const riscv_mem_access_args_t args)
3394+
{
3395+
RISCV_INFO(r);
3396+
return r->access_memory(target, args, /* is_virtual */ false);
3397+
}
3398+
3399+
33853400
static int riscv_read_phys_memory(struct target *target, target_addr_t phys_address,
33863401
uint32_t size, uint32_t count, uint8_t *buffer)
33873402
{
@@ -3392,8 +3407,7 @@ static int riscv_read_phys_memory(struct target *target, target_addr_t phys_addr
33923407
.count = count,
33933408
.increment = size,
33943409
};
3395-
RISCV_INFO(r);
3396-
return r->access_memory(target, args);
3410+
return riscv_access_phys_memory(target, args);
33973411
}
33983412

33993413
static int riscv_write_phys_memory(struct target *target, target_addr_t phys_address,
@@ -3406,12 +3420,11 @@ static int riscv_write_phys_memory(struct target *target, target_addr_t phys_add
34063420
.count = count,
34073421
.increment = size,
34083422
};
3409-
3410-
RISCV_INFO(r);
3411-
return r->access_memory(target, args);
3423+
return riscv_access_phys_memory(target, args);
34123424
}
34133425

3414-
static int riscv_rw_memory(struct target *target, const riscv_mem_access_args_t args)
3426+
static int riscv_access_virt_memory(struct target *target,
3427+
const riscv_mem_access_args_t args)
34153428
{
34163429
assert(riscv_mem_access_is_valid(args));
34173430

@@ -3423,8 +3436,11 @@ static int riscv_rw_memory(struct target *target, const riscv_mem_access_args_t
34233436
}
34243437

34253438
RISCV_INFO(r);
3439+
if (riscv_virt2phys_mode_is_off(target))
3440+
return r->access_memory(target, args, /* is_virtual */ false);
3441+
34263442
if (riscv_virt2phys_mode_is_hw(target))
3427-
return r->access_memory(target, args);
3443+
return r->access_memory(target, args, /* is_virtual */ true);
34283444

34293445
int result = check_virt_memory_access(target, args.address,
34303446
args.size, args.count, is_write);
@@ -3456,7 +3472,8 @@ static int riscv_rw_memory(struct target *target, const riscv_mem_access_args_t
34563472
else
34573473
current_access.read_buffer += current_count * args.size;
34583474

3459-
result = r->access_memory(target, current_access);
3475+
result = r->access_memory(target,
3476+
current_access, /* is_virtual */ false);
34603477
if (result != ERROR_OK)
34613478
return result;
34623479

@@ -3477,7 +3494,7 @@ static int riscv_read_memory(struct target *target, target_addr_t address,
34773494
.increment = size,
34783495
};
34793496

3480-
return riscv_rw_memory(target, args);
3497+
return riscv_access_virt_memory(target, args);
34813498
}
34823499

34833500
static int riscv_write_memory(struct target *target, target_addr_t address,
@@ -3491,7 +3508,7 @@ static int riscv_write_memory(struct target *target, target_addr_t address,
34913508
.increment = size,
34923509
};
34933510

3494-
return riscv_rw_memory(target, args);
3511+
return riscv_access_virt_memory(target, args);
34953512
}
34963513

34973514
static const char *riscv_get_gdb_arch(const struct target *target)
@@ -5224,7 +5241,9 @@ COMMAND_HANDLER(handle_repeat_read)
52245241
.count = count,
52255242
.increment = 0,
52265243
};
5227-
int result = r->access_memory(target, args);
5244+
/* TODO: Add a command parameter that enables
5245+
* choosing between virtual and physical access */
5246+
int result = r->access_memory(target, args, /* is_virtual */ false);
52285247
if (result == ERROR_OK) {
52295248
target_handle_md_output(cmd, target, address, size, count, buffer,
52305249
false);
@@ -5607,7 +5626,7 @@ static const struct command_registration riscv_exec_command_handlers[] = {
56075626
.handler = handle_repeat_read,
56085627
.mode = COMMAND_ANY,
56095628
.usage = "count address [size=4]",
5610-
.help = "Repeatedly read the value at address."
5629+
.help = "Repeatedly read the value at physical address."
56115630
},
56125631
{
56135632
.name = "set_command_timeout_sec",

src/target/riscv/riscv.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ struct riscv_info {
303303
riscv_sample_config_t *config,
304304
int64_t until_ms);
305305

306-
int (*access_memory)(struct target *target, const riscv_mem_access_args_t args);
306+
int (*access_memory)(struct target *target,
307+
const riscv_mem_access_args_t args, const bool is_virtual);
307308

308309
unsigned int (*data_bits)(struct target *target);
309310

@@ -411,6 +412,7 @@ typedef struct {
411412

412413
bool riscv_virt2phys_mode_is_hw(const struct target *target);
413414
bool riscv_virt2phys_mode_is_sw(const struct target *target);
415+
bool riscv_virt2phys_mode_is_off(const struct target *target);
414416

415417
/* Wall-clock timeout for a command/access. Settable via RISC-V Target commands.*/
416418
int riscv_get_command_timeout_sec(void);

0 commit comments

Comments
 (0)