On Fri, Jul 20, 2018 at 8:08 AM, Allen Baum
<
allen...@esperantotech.com> wrote:
> The answer is that it isn't visible to running code. That would defeat (if
> that's the correct word) virtualization. There should be no need for code to
> determine it's own mode.
I'd like to add on to this.
Another thing to consider: let's suppose that you *do* succeed in
virtualizing a hart that exposes the current privilege level, what
you'll find is that the "current virtual privilege mode" is *always*
set to the same value for a given piece of code. That is, the trap
handler responding to M-mode traps will always have the privilege set
to 11, the S-mode trap handler (whatever is pointed at by stvec) will
always see mode 01, and so forth. With the privilege effectively
being constant for a given chunk of code, not recording it in xSTATUS
frees up two bits that are better used for other things.
I remember asking the same thing some years ago here, because I wanted
to write a debugger interface which printed the current privilege mode
of the running program, and was given an answer similar to Allen's. I
was skeptical, but as I wrote the code, I started to realize that to
provide the diagnostic display I wanted, I *only* relied on the MPP
field, and never on any other. The code I'd written *always* ran at
M-mode, so it didn't make sense to query the current privilege mode.
If you've written a procedure or function which can be called from a
plurality of privilege levels, then you've exposed yourself to a
possible vector for compromise. My recommendation would be to either
replicate the code (so one procedure for M-mode, one for S-mode, one
for U-mode, etc), *or*, write the common procedure such that it *only*
ever assumes its running in U-mode. With strong pressure to isolate
S-mode and U-mode address spaces anyway, even this latter strategy may
have implications that would be wise to question.
--
Samuel A. Falvo II