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

What is the Correct Trace Packets for an MRET-Exception-Exception Sequence #187

Open
ram2532 opened this issue Jan 3, 2025 · 10 comments
Open

Comments

@ram2532
Copy link

ram2532 commented Jan 3, 2025

I have a question about which packets are generated for a certain instruction sequence. This sequence is shown below:

  • MRET to lower privilege mode
  • EX1 (exception/interrupt on target of MRET taken to M-mode)
  • EX2 (interrupt on EX1 handler address taken to M-mode)
  • Handler (the handler for EX2)

In my understanding, this sequence is governed by the section shown below from the diagram found in the Efficient Trace for RISC-V specification (Version 2.0.3, April 19, 2024) Chapter 9, Figure 2. "Instruction delta trace algorithm."

image

Based on this diagram, here is what I think is generated for each of the instructions above:

  • MRET: Next inst is exc_only -> issue format 0/1/2 packet
  • EX1: Updiscon previous (yes) -> exc_only (yes) -> issue format 3.1, address of EX1 instruction, thaddr = 0, cause/tval of EX1
  • EX2: Exception previous (yes) -> exc_only (yes) -> issue format 3.1, address of EX2 instruction, thaddr = 0, cause/tval of EX1
  • Handler: Exception previous (yes) -> exc_only (no) -> Reported (no) -> issue format 3.1, address of Handler instruction, thaddr = 1, cause/tval of EX2

In my example, 3, format 3.1 packets are generated, and no 3.0 packet. For the EX2 packet, with the color coding, the diagram indicates that the cause/tval are those for the previous exception (EX1), which means that the handler packet needs to be a 3.1 packet to convey the cause/tval for EX2. Unless the diagram coloring is wrong.

Have I understood the process correctly for this sequence?

@pdonahue-ventana
Copy link
Contributor

  • MRET to lower privilege mode
  • EX1 (exception/interrupt on target of MRET taken to M-mode)
  • EX2 (interrupt on EX1 handler address taken to M-mode)
  • Handler (the handler for EX2)

I don't think that the above sequence is possible in RISC-V. EX1 will clear mstatus.MIE which will prevent interrupt EX2 from being taken.

Perhaps you're asking about this sequence which has a different mode for EX1:

  • MRET to lower privilege mode
  • EX1 (exception/interrupt on target of MRET taken to S/HS-mode)
  • EX2 (interrupt on EX1 handler address taken to M-mode)
  • Handler (the handler for EX2)

Or this sequence where EX1 does go to M mode but it must have been an interrupt (to have a different vector from EX2) and EX2 must have been an exception (because MIE=0):

  • MRET to lower privilege mode
  • EX1 (interrupt on target of MRET taken to M-mode. mtvec.MODE=Vectored so we go to mtvec+offset)
  • EX2 (exception on first instruction of EX1 handler at mtvec+offset taken to M-mode)
  • Handler (the handler for EX2 at mtvec+0)

(The latter sequence needs vectored mode to avoid an infinite loop of exceptions. I think that the latter sequence is highly unlikely to happen in real life because the first instruction of an interrupt handler should generally not be getting an exception.)

@IainCRobertson
Copy link
Collaborator

IainCRobertson commented Jan 6, 2025 via email

@ram2532
Copy link
Author

ram2532 commented Jan 7, 2025

@IainCRobertson Thanks for the correction.

Regarding the packet generated on EX2. You said: "Should be cause/tval of EX2".

The action box shows cause/tval in orange which means it should send cause/tval from previous exception. Is the color wrong in the diagram?

@IainCRobertson
Copy link
Collaborator

IainCRobertson commented Jan 7, 2025 via email

@ram2532
Copy link
Author

ram2532 commented Jan 7, 2025

@IainCRobertson

Thanks for identifying a needed change to the flowchart. I'm wondering if the flowchart needs another change:

Some of the lines from your examples say "do nothing", I do not see that path in the flowchart. Should I be interpreting the green decision box - 'Next inst is exc_only, ppccd_br, or unqualifed?' - as actually saying 'Next inst is exc_only and current inst is not exc_only, or ppccd_br or unqualifed" or should there be a 'Reported?' decision box afterwards?

I can't find a path for "do nothing" unless I add on to the meaning of that decision box.

@IainCRobertson
Copy link
Collaborator

IainCRobertson commented Jan 7, 2025 via email

@ram2532
Copy link
Author

ram2532 commented Jan 7, 2025

I must be interpreting something wrong. For the example below (taken from an earlier example you provided). When I follow the flowchart for EX1 - when I get to the green 'Next inst is exc_only, ppccd_br, or unqualified?' diamond, I see that the instruction following EX1 - EX2 - has an exception. So, it seems to me that the answer to the question "is next inst exc_only" is "Yes". But, based on your response, it sounds like it should be "No". Am I misunderstanding what "exc_only" means?

[some instruction]: next inst is exc_only -> issue format 0/1/2 packet
EX1: do nothing
EX2: exception_previous (yes) -> exc_only (yes) -> issue format 3.1, address of EX2, thaddr = 0, case/tval of EX1
Handler: exception_previous (yes) -> exc_only (no) -> reported (yes) -> issue format 3.0

@IainCRobertson
Copy link
Collaborator

IainCRobertson commented Jan 8, 2025 via email

@ram2532
Copy link
Author

ram2532 commented Jan 8, 2025

@IainCRobertson

Thanks for clarifying things for me. I look forward to seeing the updated flowchart.

I think these are the changes that you identified:

  1. Another 'reported?' test after the exc_only test.
  2. The 'yes' exit from the green diamond should only occur on the retiring instruction before an exception.

@IainCRobertson
Copy link
Collaborator

Please review #198

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

No branches or pull requests

3 participants