Skip to content
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

RISC-V Semihosting 2 of 3: Refactor magic sequence detection #1199

Open
wants to merge 3 commits into
base: riscv
Choose a base branch
from

Conversation

JanMatCodasip
Copy link
Collaborator

Refactor (clean up) the code in riscv_semihosting.c by moving
the magic sequence detection to its own function.

@JanMatCodasip JanMatCodasip changed the title Semihosting 2 of 3: Refactor magic sequence detection RISC-V Semihosting 2 of 3: Refactor magic sequence detection Jan 7, 2025
@JanMatCodasip
Copy link
Collaborator Author

JanMatCodasip commented Jan 7, 2025

Note: This MR is built on top of #1198.

return SEMIHOSTING_NONE;
}
if (!sequence_found) {
LOG_TARGET_DEBUG(target, " -> NONE (no magic)");
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe move this printout inside riscv_semihosting_detect_magic_sequence?

		if (result != ERROR_OK) {
			*sequence_found = false;
			return result;
		}

Having context-dependent printout like this is somewhat confusing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Addressed, but differently.

My understanding is that the prints like -> NONE (no magic) were supposed to print the final outcome of riscv_semihosting() function into the debug log for easier troubleshooting.

I agree that the prints looked confusing.

For consistency and clarity, I changed all of them to this form Semihosting outcome: SOMETHING (extra explanation). Please, let me know if this form looks OK to you.

@JanMatCodasip JanMatCodasip force-pushed the jm-codasip/semihosting-2-refactor-magic-seq-detection branch from 4af47de to eeedccd Compare January 13, 2025 07:02
Refactor (clean up) the code in riscv_semihosting.c by moving
the magic sequence detection to its own function.

Change-Id: I3a3ce991336ceeeff023d459d0e28558059554e0
Signed-off-by: Jan Matyas <[email protected]>
Cosmetic, non-functional improvements
in `riscv_semihosting_detect_magic_sequence()`.

Change-Id: I935d3847749a02e4f579900a0971c30a5cc826b7
Signed-off-by: Jan Matyas <[email protected]>
Cleanup the debug prints denoting the semihosting outcome
so that they are easier to understand when reading the OpenOCD's
verbose (debug) log.

Change-Id: Ibdef1b4474e47dd0a135d4696b3e53600c544eb8
Signed-off-by: Jan Matyas <[email protected]>
@JanMatCodasip JanMatCodasip force-pushed the jm-codasip/semihosting-2-refactor-magic-seq-detection branch from eeedccd to d0165b4 Compare January 14, 2025 16:10

const uint32_t magic[] = {
0x01f01013, /* slli zero,zero,0x1f */
0x00100073, /* ebreak */
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a need to read the memory at the PC? The fact that the hart was halted due to ebreak execution is known from reading dcsr.cause.
Please note, this is just a question and I don't think it should be addressed in this commit.

/* Read three uncompressed instructions:
* The previous, the current one (pointed to by PC) and the next one. */
const target_addr_t sequence_start_address = pc - 4;
for (int i = 0; i < 3; i++) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wouldn't it be easier and faster to read 12 bytes at once using target_read_buffer() and compare it with the magic sequence from a buffer filled by buf_set_u32()?
Please note, this is just a question and I don't think it should be addressed in this commit.

Copy link
Collaborator

Choose a reason for hiding this comment

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

In general, I believe riscv_read_by_any_size() is redundant. See #1205

return result;
}

const uint32_t value = target_buffer_get_u32(target, buf);
Copy link
Collaborator

Choose a reason for hiding this comment

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

target_buffer_get_u32() converts the endianness, therefore it is not correct to use it here, since:

  1. Instruction encoding does not depend on endianness.
  2. riscv_read_by_any_size() fills the buf by reads of some size. I hope the endianness of the elements is converted to little before storing to the buffer. Alternatively it is a bug in target_buffer_get_u32().

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.

3 participants