Skip to content

target/riscv: add is_virtual parameter to memory access method #1241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: riscv
Choose a base branch
from

Conversation

fk-sc
Copy link
Contributor

@fk-sc fk-sc commented Mar 18, 2025

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. It is provide physical access now

@fk-sc
Copy link
Contributor Author

fk-sc commented Mar 18, 2025

@JanMatCodasip, @MarekVCodasip could you please take a look?

@fk-sc
Copy link
Contributor Author

fk-sc commented Mar 18, 2025

In this comment, @JanMatCodasip suggested moving the is_virtual field out of the riscv_mem_access_args_t structure. However, while implementing is_virtual across progbuf, sysbus, and abstract memory access types, I’ve noticed the flag is becoming pervasive and required multiple places. Given this, should we reconsider keeping it in riscv_mem_access_args_t to avoid redundancy and centralize its handling?

@fk-sc fk-sc force-pushed the fk-sc/is-virtual branch from 9c2d73a to bebc6e7 Compare March 19, 2025 14:24
@JanMatCodasip
Copy link
Collaborator

JanMatCodasip commented Mar 19, 2025

fk-sc: In this comment, JanMatCodasip suggested moving the is_virtual field out of the riscv_mem_access_args_t structure. [...]

I do not feel strongly about this one. Please feel free to use and submit whatever variant in your opinion leads to cleaner, more understandable code.

My concern was that is_virtual would be passed (as part of the structure) to functions for which it is not relevant. If those functions are in minority, maybe it is an acceptable compromise.

@fk-sc
Copy link
Contributor Author

fk-sc commented Mar 24, 2025

@JanMatCodasip, I’ve decided to keep is_virtual as a separate parameter for now. I can revisit integrating it into the riscv_mem_access_args_t structure once the progbuf, sysbus, and abstract memory subsystems are finalized

@fk-sc fk-sc force-pushed the fk-sc/is-virtual branch from bebc6e7 to 83af966 Compare March 25, 2025 10:28
@@ -3425,7 +3425,7 @@ static int riscv_rw_memory(struct target *target, const riscv_mem_access_args_t

RISCV_INFO(r);
if (!mmu_enabled)
return r->access_memory(target, args);
return r->access_memory(target, args, /* is_virtual */ true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please double-check this line.

If mmu_enabled is false, shouldn't is_virtual be false as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fk-sc can we discuss this design once again?

  1. I don't like the suggested approach because riscv_rw_memory should always operate on virtual addresses by design. The decision on how the actual address translation is performed should be decided by lower level functions - that is I would expect for r->access_memory() to be called with is_virutual = true. The fact that in some execution context virtual address is equal to physical is irrelevant at this level of abstraction.

  2. if there is a necessity to provide additional layer to simplify page-crossing translations - we can do it here and establish a contract that r->access_memory should access non-page crossing memory regions.

  3. please rename mmu_enabled to satp_translation_required. mmu_enabled is too broad term that can confuse the reader (it definitely confuses me)

@JanMatCodasip sorry for the mess, most likely we @fk-sc , @en-sc and me should have yet another round of internal discussion related to the above bulllets. Please consider this MR as a draft for now. You inputs are as always are very welcome, so please do share concerns/questions/suggestions regarding the matter (if you have any)

Copy link
Collaborator

@JanMatCodasip JanMatCodasip Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem at all. Thank you for letting me know.

aap-sc: [...] riscv_rw_memory should always operate on virtual addresses by design.

Sounds good to me.

In that case, if you like, you can also make the code more clear by doing this:

  • rename riscv_rw_memory() to riscv_rw_virt_memory()
  • create riscv_rw_phys_memory() (a thin wrapper over r->access_memory(args, /* is_virtual = */ false))

aap-sc: if there is a necessity to provide additional layer to simplify page-crossing translations - we can do it here and establish a contract that r->access_memory should access non-page crossing memory regions.

  • The algorithm for breaking the memory access into the page-sized chunks, which is already in riscv_rw_memory(), looks all right to me. Or do you see any gap or need for change there, which I might have missed?
  • Do I understand correctly that the only case when we need to manually break the memory transfer to page-sized pieces is when virt2phys_mode is set to SW?

aap-sc: please rename mmu_enabled to satp_translation_required. mmu_enabled is too broad term that can confuse the reader

I agree that mmu_enabled is not a clear name. It could also be called manual_satp_translation_needed. The word "manual" is IMO important here as it means that OpenOCD must do the translation work (as opposed to the hardware).

Furthermore, I am concerned about the riscv_mmu() itself. If virt2phys mode is set to HW, then riscv_mmu() returns False, which looks incorrect. As a result:

  • The riscv_virt2phys() function (and the TCL command virt2phys) will return 1:1 mapping instead of correctly translated addresses.
  • The target_alloc_working_area_try() will allocate the work area in the physical address range (-work-area-phys) instead of virtual (-work-area-virt).

Should the initial check in riscv_mmu() be fixed this way?

	if (riscv_virt2phys_mode_is_off(target))
		return ERROR_OK;

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 <[email protected]>
@fk-sc fk-sc force-pushed the fk-sc/is-virtual branch from 83af966 to 427026a Compare March 29, 2025 23:23
@fk-sc fk-sc requested review from JanMatCodasip and en-sc March 29, 2025 23:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants