@@ -139,6 +139,35 @@ struct tdata1_cache {
139139 struct list_head elem_tdata1 ;
140140};
141141
142+ bool riscv_virt2phys_mode_is_hw (const struct target * target )
143+ {
144+ assert (target );
145+ RISCV_INFO (r );
146+ return r -> virt2phys_mode == RISCV_VIRT2PHYS_MODE_HW ;
147+ }
148+
149+ bool riscv_virt2phys_mode_is_sw (const struct target * target )
150+ {
151+ assert (target );
152+ RISCV_INFO (r );
153+ return r -> virt2phys_mode == RISCV_VIRT2PHYS_MODE_SW ;
154+ }
155+
156+ const char * riscv_virt2phys_mode_to_str (riscv_virt2phys_mode_t mode )
157+ {
158+ assert (mode == RISCV_VIRT2PHYS_MODE_OFF
159+ || mode == RISCV_VIRT2PHYS_MODE_SW
160+ || mode == RISCV_VIRT2PHYS_MODE_HW );
161+
162+ static const char * const names [] = {
163+ [RISCV_VIRT2PHYS_MODE_HW ] = "hw" ,
164+ [RISCV_VIRT2PHYS_MODE_SW ] = "sw" ,
165+ [RISCV_VIRT2PHYS_MODE_OFF ] = "off" ,
166+ };
167+
168+ return names [mode ];
169+ }
170+
142171/* Wall-clock timeout for a command/access. Settable via RISC-V Target commands.*/
143172static int riscv_command_timeout_sec_value = DEFAULT_COMMAND_TIMEOUT_SEC ;
144173
@@ -150,10 +179,6 @@ int riscv_get_command_timeout_sec(void)
150179 return MAX (riscv_command_timeout_sec_value , riscv_reset_timeout_sec );
151180}
152181
153- static bool riscv_enable_virt2phys = true;
154-
155- bool riscv_enable_virtual ;
156-
157182static enum {
158183 RO_NORMAL ,
159184 RO_REVERSED
@@ -2714,7 +2739,7 @@ static int riscv_mmu(struct target *target, int *enabled)
27142739{
27152740 * enabled = 0 ;
27162741
2717- if (!riscv_enable_virt2phys )
2742+ if (!riscv_virt2phys_mode_is_sw ( target ) )
27182743 return ERROR_OK ;
27192744
27202745 /* Don't use MMU in explicit or effective M (machine) mode */
@@ -3938,16 +3963,6 @@ COMMAND_HANDLER(riscv_set_mem_access)
39383963 return ERROR_OK ;
39393964}
39403965
3941- COMMAND_HANDLER (riscv_set_enable_virtual )
3942- {
3943- if (CMD_ARGC != 1 ) {
3944- LOG_ERROR ("Command takes exactly 1 parameter" );
3945- return ERROR_COMMAND_SYNTAX_ERROR ;
3946- }
3947- COMMAND_PARSE_ON_OFF (CMD_ARGV [0 ], riscv_enable_virtual );
3948- return ERROR_OK ;
3949- }
3950-
39513966static int parse_ranges (struct list_head * ranges , const char * tcl_arg , const char * reg_type , unsigned int max_val )
39523967{
39533968 char * args = strdup (tcl_arg );
@@ -4459,16 +4474,6 @@ COMMAND_HANDLER(riscv_set_maskisr)
44594474 return ERROR_OK ;
44604475}
44614476
4462- COMMAND_HANDLER (riscv_set_enable_virt2phys )
4463- {
4464- if (CMD_ARGC != 1 ) {
4465- LOG_ERROR ("Command takes exactly 1 parameter" );
4466- return ERROR_COMMAND_SYNTAX_ERROR ;
4467- }
4468- COMMAND_PARSE_ON_OFF (CMD_ARGV [0 ], riscv_enable_virt2phys );
4469- return ERROR_OK ;
4470- }
4471-
44724477COMMAND_HANDLER (riscv_set_ebreakm )
44734478{
44744479 struct target * target = get_current_target (CMD_CTX );
@@ -5093,6 +5098,36 @@ COMMAND_HANDLER(handle_reserve_trigger)
50935098 return ERROR_OK ;
50945099}
50955100
5101+ COMMAND_HANDLER (handle_riscv_virt2phys_mode )
5102+ {
5103+ struct riscv_info * info = riscv_info (get_current_target (CMD_CTX ));
5104+ if (CMD_ARGC == 0 ) {
5105+ riscv_virt2phys_mode_t mode = info -> virt2phys_mode ;
5106+ command_print (CMD , "%s" , riscv_virt2phys_mode_to_str (mode ));
5107+ return ERROR_OK ;
5108+ }
5109+
5110+ if (CMD_ARGC != 1 )
5111+ return ERROR_COMMAND_SYNTAX_ERROR ;
5112+
5113+ // TODO: add auto mode to allow OpenOCD choose translation mode
5114+ if (!strcmp (CMD_ARGV [0 ],
5115+ riscv_virt2phys_mode_to_str (RISCV_VIRT2PHYS_MODE_SW ))) {
5116+ info -> virt2phys_mode = RISCV_VIRT2PHYS_MODE_SW ;
5117+ } else if (!strcmp (CMD_ARGV [0 ],
5118+ riscv_virt2phys_mode_to_str (RISCV_VIRT2PHYS_MODE_HW ))) {
5119+ info -> virt2phys_mode = RISCV_VIRT2PHYS_MODE_HW ;
5120+ } else if (!strcmp (CMD_ARGV [0 ],
5121+ riscv_virt2phys_mode_to_str (RISCV_VIRT2PHYS_MODE_OFF ))) {
5122+ info -> virt2phys_mode = RISCV_VIRT2PHYS_MODE_OFF ;
5123+ } else {
5124+ command_print (CMD , "Unsupported address translation mode: %s" , CMD_ARGV [0 ]);
5125+ return ERROR_COMMAND_ARGUMENT_INVALID ;
5126+ }
5127+
5128+ return ERROR_OK ;
5129+ }
5130+
50965131static const struct command_registration riscv_exec_command_handlers [] = {
50975132 {
50985133 .name = "dump_sample_buf" ,
@@ -5144,15 +5179,6 @@ static const struct command_registration riscv_exec_command_handlers[] = {
51445179 .help = "Set which memory access methods shall be used and in which order "
51455180 "of priority. Method can be one of: 'progbuf', 'sysbus' or 'abstract'."
51465181 },
5147- {
5148- .name = "set_enable_virtual" ,
5149- .handler = riscv_set_enable_virtual ,
5150- .mode = COMMAND_ANY ,
5151- .usage = "on|off" ,
5152- .help = "When on, memory accesses are performed on physical or virtual "
5153- "memory depending on the current system configuration. "
5154- "When off (default), all memory accessses are performed on physical memory."
5155- },
51565182 {
51575183 .name = "expose_csrs" ,
51585184 .handler = riscv_set_expose_csrs ,
@@ -5277,14 +5303,6 @@ static const struct command_registration riscv_exec_command_handlers[] = {
52775303 .help = "mask riscv interrupts" ,
52785304 .usage = "['off'|'steponly']" ,
52795305 },
5280- {
5281- .name = "set_enable_virt2phys" ,
5282- .handler = riscv_set_enable_virt2phys ,
5283- .mode = COMMAND_ANY ,
5284- .usage = "on|off" ,
5285- .help = "When on (default), enable translation from virtual address to "
5286- "physical address."
5287- },
52885306 {
52895307 .name = "set_ebreakm" ,
52905308 .handler = riscv_set_ebreakm ,
@@ -5353,6 +5371,16 @@ static const struct command_registration riscv_exec_command_handlers[] = {
53535371 .usage = "[index ('on'|'off')]" ,
53545372 .help = "Controls which RISC-V triggers shall not be touched by OpenOCD." ,
53555373 },
5374+ {
5375+ .name = "virt2phys_mode" ,
5376+ .handler = handle_riscv_virt2phys_mode ,
5377+ .mode = COMMAND_ANY ,
5378+ .usage = "['sw'|'hw'|'off']" ,
5379+ .help = "Configure the virtual address translation mode: "
5380+ "sw - translate vaddr to paddr by manually traversing page tables, "
5381+ "hw - translate vaddr to paddr by hardware, "
5382+ "off - no address translation."
5383+ },
53565384 COMMAND_REGISTRATION_DONE
53575385};
53585386
@@ -5469,6 +5497,8 @@ static void riscv_info_init(struct target *target, struct riscv_info *r)
54695497
54705498 r -> xlen = -1 ;
54715499
5500+ r -> virt2phys_mode = RISCV_VIRT2PHYS_MODE_SW ;
5501+
54725502 r -> isrmask_mode = RISCV_ISRMASK_OFF ;
54735503
54745504 r -> mem_access_methods [0 ] = RISCV_MEM_ACCESS_PROGBUF ;
0 commit comments