I remember asking this same question years ago, and Andrew gave me an
answer which was different, and which I think should be included here
for completeness. His answer then does not negate what he wrote
previously.
Andrew wrote above, "Most RISC-V software should be written to assume
a specific mode, ...". Note that you always know which privilege mode
you're currently running in (virtualization notwithstanding) based on
which trap has been taken. For example, any trap handler pointed at
by mtvec (or one of its interrupt vectors, if you're running with
vectored interrupts) must necessarily be running in machine-mode.
Likewise, any handler pointed at by stvec must be running in
supervisor mode, etc.
If you need to have a common procedure shared by a number of different
privilege mode trap handlers, you can explicitly set a variable as
part of each privilege mode entry point before dispatching to the
common handler.
> To view this discussion on the web visit
https://groups.google.com/a/groups.riscv.org/d/msgid/isa-dev/CA%2B%2B6G0AR75_n2k9yaz7ySQTMAkLtPTMd-RCZF7UCVT1Y0jCKoQ%40mail.gmail.com.
--
Samuel A. Falvo II