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

Specify what imprecise traps are #1728

Open
Timmmm opened this issue Nov 15, 2024 · 11 comments
Open

Specify what imprecise traps are #1728

Timmmm opened this issue Nov 15, 2024 · 11 comments

Comments

@Timmmm
Copy link
Contributor

Timmmm commented Nov 15, 2024

The spec talks in 3 places about exceptions being "precise" or "imprecise":

The EEI defines for each trap whether it is handled precisely, though the recommendation is to maintain preciseness where possible. Contained and requested traps can be observed to be imprecise by software inside the execution environment. Invisible traps, by definition, cannot be observed to be precise or imprecise by software running inside the execution environment. Fatal traps can be observed to be imprecise by software running inside the execution environment, if known-errorful instructions do not cause immediate termination.

To aid in system debugging, we strongly recommend that, where possible, RISC-V processors precisely trap physical memory accesses that fail PMA checks. Precisely trapped PMA violations manifest as instruction, load, or store access-fault exceptions, distinct from virtual-memory page-fault exceptions. Precise PMA traps might not always be possible, for example, when probing a legacy bus architecture that uses access failures as part of the discovery mechanism. In this case, error responses from peripheral devices will be reported as imprecise bus-error interrupts.

PMP violations are always trapped precisely at the processor.

However:

  1. There's no definition of what "precise" and "imprecise" mean.
  2. It doesn't describe what effect imprecise traps should have.

I think there's a lot of detail missing here. I assume the ARM spec (or maybe MIPS?) explains all of this and it's just assumed knowledge, but I'm not familiar with ARM or MIPS & I think the RISC-V spec shouldn't assume I am.

@allenjbaum
Copy link

allenjbaum commented Nov 15, 2024 via email

@Timmmm
Copy link
Contributor Author

Timmmm commented Nov 16, 2024

I'm not sure if there are specific exceptions (sic) to that, e.g. some very long running instructions like matrix multiply, or push/pop

Yeah vector instructions can be interrupted mid-instruction. There's a mechanism to resume them from the middle (vstart).

but also when exception state does not allow the determination of the instruction that caused the trap

I think this is referred to as a "resumable" trap in a number of places.

Is there a definition of precise/imprecise we can lift from somewhere else?

@allenjbaum
Copy link

allenjbaum commented Nov 19, 2024 via email

@Timmmm
Copy link
Contributor Author

Timmmm commented Nov 19, 2024

I would still have many questions about that definition, e.g:

  1. Are instructions allowed to have partial effect? Sounds like yes from your PTE example, but what are the limits of this? Can an instruction that writes to two registers just write to one of them?
  2. What values do xepc and xtval get?
  3. Which traps can be imprecise exactly?

Maybe the definition could be something like this:

A precise trap is one where:

  1. no instructions after the instruction that caused the trap have any effect,
  2. all instructions before it have fully retired,
  3. xepc points to the instruction that caused the trap and
  4. xtval contains the instruction bits (optionally).

An imprecise trap is one where any of these conditions are untrue.

(I'm sure this is wrong but we have to start somewhere.)

@pdonahue-ventana
Copy link
Contributor

Are instructions allowed to have partial effect? Sounds like yes from your PTE example, but what are the limits of this? Can an instruction that writes to two registers just write to one of them?

Loads (and other instructions) in the V extension may update multiple registers. A trap (e.g. page fault or interrupt) may happen in the middle, allowing a subset of the registers to be updated or even a partial update of a single register. See the description of the vstart CSR in the unprivileged spec.

@allenjbaum
Copy link

allenjbaum commented Nov 20, 2024 via email

@Timmmm
Copy link
Contributor Author

Timmmm commented Nov 20, 2024

Ok so:

A precise trap is one where:

  1. no instructions after the instruction that caused the trap have any effect,
  2. all instructions before it have fully retired,
  3. xepc points to the instruction that caused the trap and
  4. xtval contains the instruction bits (optionally).

An imprecise trap is one where any of these conditions are untrue.

A trap may still be precise even if the instruction that caused it does not fully complete. Examples of this include:

  • Vector instructions can be interrupted while executing, leaving the overall operation partially complete. See vstart.
  • Where memory accesses that are split into multiple memory operations (possible for misaligned accesses), it is possible for a subset of the operations to complete.
  • cm.push and cm.pop can be interrupted while executing, leaving the overall operation partially complete.

@allenjbaum
Copy link

allenjbaum commented Nov 20, 2024 via email

@dhower-qc
Copy link

Note that there are cases where speculation may cause state to be updated,
specifically A-bit setting in PTE entries can occur even if the instruction
nerve completrs.
I'm not sure if there is something similar in FS/VS bits instatus.

Both the A bit and FS/VS bits, if implemented imprecisely (different type of "precise"), can be updated at any time for any reason. The imprecise bit update isn't architecturally tied to any instruction. So, for this discussion of "precise exceptions," they don't matter.

Note that the vector spec does have a nice definition of a precise vector exception. If you take away the vector-specific stuff, it says the two conditions from above (which match Allen's original defintion):

  1. all instructions older than the trapping vector instruction have committed their results

  2. no instructions newer than the trapping vector instruction have altered architectural state

I don't think Tim's conditions 3 and 4 are needed; even imprecise traps have to write mepc/mtval with the exceptional instruction information

So, my take is that a precise trap is one where:

  1. all instructions older than the trapping instruction have committed their results
  2. no instructions newer than the trapping instruction have altered architectural state

@davidharrishmc
Copy link

@dhower-qc definition is consistent with my understanding of the term.

@allenjbaum
Copy link

allenjbaum commented Nov 27, 2024 via email

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

5 participants