Skip to content

Commit e9eca80

Browse files
authored
Merge pull request riscv-collab#1084 from en-sc/en-sc/ref-reg-files
target/riscv: separate register cache stuff into files
2 parents c6bb902 + 3883b03 commit e9eca80

17 files changed

+1678
-1486
lines changed

src/target/riscv/Makefile.am

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@ noinst_LTLIBRARIES += %D%/libriscv.la
1111
%D%/opcodes.h \
1212
%D%/program.h \
1313
%D%/riscv.h \
14+
%D%/riscv-011.h \
15+
%D%/riscv-011_reg.h \
16+
%D%/riscv-013.h \
17+
%D%/riscv-013_reg.h \
1418
%D%/batch.c \
1519
%D%/program.c \
1620
%D%/riscv-011.c \
21+
%D%/riscv-011_reg.c \
1722
%D%/riscv-013.c \
23+
%D%/riscv-013_reg.c \
1824
%D%/riscv.c \
25+
%D%/riscv_reg.c \
1926
%D%/riscv_semihosting.c \
2027
%D%/debug_defines.c \
2128
%D%/debug_reg_printer.c

src/target/riscv/gdb_regs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#ifndef TARGET__RISCV__GDB_REGS_H
44
#define TARGET__RISCV__GDB_REGS_H
55

6+
#include "encoding.h"
7+
68
/* gdb's register list is defined in riscv_gdb_reg_names gdb/riscv-tdep.c in
79
* its source tree. We must interpret the numbers the same here. */
810
enum gdb_regno {
@@ -123,6 +125,4 @@ enum gdb_regno {
123125
GDB_REGNO_COUNT
124126
};
125127

126-
const char *gdb_regno_name(const struct target *target, enum gdb_regno regno);
127-
128128
#endif

src/target/riscv/program.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int riscv_program_write(struct riscv_program *program);
4141

4242
/* Executes a program, returning 0 if the program successfully executed. Note
4343
* that this may cause registers to be saved or restored, which could result to
44-
* calls to things like riscv_save_register which itself could require a
44+
* calls to things like riscv013_reg_save which itself could require a
4545
* program to execute. That's OK, just make sure this eventually terminates.
4646
* */
4747
int riscv_program_exec(struct riscv_program *p, struct target *t);

src/target/riscv/riscv-011.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "config.h"
1414
#endif
1515

16+
#include "riscv-011.h"
17+
1618
#include "target/target.h"
1719
#include "target/algorithm.h"
1820
#include "target/target_type.h"
@@ -22,6 +24,8 @@
2224
#include "target/breakpoints.h"
2325
#include "helper/time_support.h"
2426
#include "riscv.h"
27+
#include "riscv_reg.h"
28+
#include "riscv-011_reg.h"
2529
#include "asm.h"
2630
#include "gdb_regs.h"
2731
#include "field_helpers.h"
@@ -210,8 +214,6 @@ typedef struct {
210214

211215
static int poll_target(struct target *target, bool announce);
212216
static int riscv011_poll(struct target *target);
213-
static int get_register(struct target *target, riscv_reg_t *value,
214-
enum gdb_regno regid);
215217

216218
/*** Utility functions. ***/
217219

@@ -1045,7 +1047,7 @@ static int read_remote_csr(struct target *target, uint64_t *value, uint32_t csr)
10451047
uint32_t exception = cache_get32(target, info->dramsize-1);
10461048
if (exception) {
10471049
LOG_WARNING("Got exception 0x%x when reading %s", exception,
1048-
gdb_regno_name(target, GDB_REGNO_CSR0 + csr));
1050+
riscv_reg_gdb_regno_name(target, GDB_REGNO_CSR0 + csr));
10491051
*value = ~0;
10501052
return ERROR_FAIL;
10511053
}
@@ -1111,7 +1113,7 @@ static int execute_resume(struct target *target, bool step)
11111113

11121114
LOG_DEBUG("step=%d", step);
11131115

1114-
if (riscv_flush_registers(target) != ERROR_OK)
1116+
if (riscv_reg_flush_all(target) != ERROR_OK)
11151117
return ERROR_FAIL;
11161118

11171119
maybe_write_tselect(target);
@@ -1225,7 +1227,7 @@ static int update_mstatus_actual(struct target *target)
12251227
/* Force reading the register. In that process mstatus_actual will be
12261228
* updated. */
12271229
riscv_reg_t mstatus;
1228-
return get_register(target, &mstatus, GDB_REGNO_MSTATUS);
1230+
return riscv011_get_register(target, &mstatus, GDB_REGNO_MSTATUS);
12291231
}
12301232

12311233
/*** OpenOCD target functions. ***/
@@ -1247,7 +1249,7 @@ static int register_read(struct target *target, riscv_reg_t *value, int regnum)
12471249

12481250
uint32_t exception = cache_get32(target, info->dramsize-1);
12491251
if (exception) {
1250-
LOG_WARNING("Got exception 0x%x when reading %s", exception, gdb_regno_name(target, regnum));
1252+
LOG_WARNING("Got exception 0x%x when reading %s", exception, riscv_reg_gdb_regno_name(target, regnum));
12511253
*value = ~0;
12521254
return ERROR_FAIL;
12531255
}
@@ -1322,14 +1324,14 @@ static int register_write(struct target *target, unsigned int number,
13221324
uint32_t exception = cache_get32(target, info->dramsize-1);
13231325
if (exception) {
13241326
LOG_WARNING("Got exception 0x%x when writing %s", exception,
1325-
gdb_regno_name(target, number));
1327+
riscv_reg_gdb_regno_name(target, number));
13261328
return ERROR_FAIL;
13271329
}
13281330

13291331
return ERROR_OK;
13301332
}
13311333

1332-
static int get_register(struct target *target, riscv_reg_t *value,
1334+
int riscv011_get_register(struct target *target, riscv_reg_t *value,
13331335
enum gdb_regno regid)
13341336
{
13351337
riscv011_info_t *info = get_info(target);
@@ -1377,7 +1379,7 @@ static int get_register(struct target *target, riscv_reg_t *value,
13771379

13781380
/* This function is intended to handle accesses to registers through register
13791381
* cache. */
1380-
static int set_register(struct target *target, enum gdb_regno regid,
1382+
int riscv011_set_register(struct target *target, enum gdb_regno regid,
13811383
riscv_reg_t value)
13821384
{
13831385
assert(target->reg_cache);
@@ -1595,7 +1597,7 @@ static int examine(struct target *target)
15951597
}
15961598

15971599
/* Update register list to match discovered XLEN/supported extensions. */
1598-
riscv_init_registers(target);
1600+
riscv011_reg_init_all(target);
15991601

16001602
info->never_halted = true;
16011603

@@ -2391,8 +2393,6 @@ static int init_target(struct command_context *cmd_ctx,
23912393
{
23922394
LOG_DEBUG("init");
23932395
RISCV_INFO(generic_info);
2394-
generic_info->get_register = get_register;
2395-
generic_info->set_register = set_register;
23962396
generic_info->read_memory = read_memory;
23972397
generic_info->authdata_read = &riscv011_authdata_read;
23982398
generic_info->authdata_write = &riscv011_authdata_write;
@@ -2404,7 +2404,7 @@ static int init_target(struct command_context *cmd_ctx,
24042404

24052405
/* Assume 32-bit until we discover the real value in examine(). */
24062406
generic_info->xlen = 32;
2407-
riscv_init_registers(target);
2407+
riscv011_reg_init_all(target);
24082408

24092409
return ERROR_OK;
24102410
}

src/target/riscv/riscv-011.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
3+
#ifndef OPENOCD_TARGET_RISCV_RISCV_011_H
4+
#define OPENOCD_TARGET_RISCV_RISCV_011_H
5+
6+
#include "riscv.h"
7+
#include "gdb_regs.h"
8+
#include "target/target.h"
9+
10+
int riscv011_get_register(struct target *target, riscv_reg_t *value,
11+
enum gdb_regno regid);
12+
int riscv011_set_register(struct target *target, enum gdb_regno regid,
13+
riscv_reg_t value);
14+
15+
#endif /*OPENOCD_TARGET_RISCV_RISCV_011_H*/

src/target/riscv/riscv-011_reg.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
#ifdef HAVE_CONFIG_H
4+
#include "config.h"
5+
#endif
6+
7+
#include "riscv-011_reg.h"
8+
9+
#include "riscv_reg_impl.h"
10+
#include "riscv-011.h"
11+
12+
static int riscv011_reg_get(struct reg *reg)
13+
{
14+
struct target * const target = riscv_reg_impl_get_target(reg);
15+
riscv_reg_t value;
16+
const int result = riscv011_get_register(target, &value, reg->number);
17+
if (result != ERROR_OK)
18+
return result;
19+
buf_set_u64(reg->value, 0, reg->size, value);
20+
return ERROR_OK;
21+
}
22+
23+
static int riscv011_reg_set(struct reg *reg, uint8_t *buf)
24+
{
25+
const riscv_reg_t value = buf_get_u64(buf, 0, reg->size);
26+
struct target * const target = riscv_reg_impl_get_target(reg);
27+
return riscv011_set_register(target, reg->number, value);
28+
}
29+
30+
static const struct reg_arch_type *riscv011_gdb_regno_reg_type(uint32_t regno)
31+
{
32+
static const struct reg_arch_type riscv011_reg_type = {
33+
.get = riscv011_reg_get,
34+
.set = riscv011_reg_set
35+
};
36+
return &riscv011_reg_type;
37+
}
38+
39+
static int riscv011_init_reg(struct target *target, uint32_t regno)
40+
{
41+
return riscv_reg_impl_init_one(target, regno, riscv011_gdb_regno_reg_type(regno));
42+
}
43+
44+
int riscv011_reg_init_all(struct target *target)
45+
{
46+
if (riscv_reg_impl_init_cache(target) != ERROR_OK)
47+
return ERROR_FAIL;
48+
49+
init_shared_reg_info(target);
50+
51+
for (uint32_t regno = 0; regno < target->reg_cache->num_regs; ++regno)
52+
if (riscv011_init_reg(target, regno) != ERROR_OK)
53+
return ERROR_FAIL;
54+
55+
if (riscv_reg_impl_expose_csrs(target) != ERROR_OK)
56+
return ERROR_FAIL;
57+
58+
riscv_reg_impl_hide_csrs(target);
59+
60+
return ERROR_OK;
61+
}

src/target/riscv/riscv-011_reg.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
3+
#ifndef OPENOCD_TARGET_RISCV_RISCV_REG_011_H
4+
#define OPENOCD_TARGET_RISCV_RISCV_REG_011_H
5+
6+
#include "target/target.h"
7+
8+
/**
9+
* This file describes additional register cache interface available to the
10+
* RISC-V Debug Specification v0.11 targets.
11+
*/
12+
13+
/**
14+
* Initialize register cache. After this function all registers can be
15+
* safely accessed via functions described here and in `riscv_reg.h`.
16+
*/
17+
int riscv011_reg_init_all(struct target *target);
18+
19+
#endif /*OPENOCD_TARGET_RISCV_RISCV_REG_011_H*/

0 commit comments

Comments
 (0)