Date   

Re: proposal for stateen CSRs

Bill Huffman
 

John,

Now I see that the reason for two bits is three desired outcomes.

But why is the first of the three necessary? Why don't we simply have two outcomes:

- All custom state is disabled.

- Custom state enable is controlled by stateen-like custom registers

What is the use of forcing all custom state to be enabled?

There's also an implication in a previous email that writing the bits being defined here actually changes the bits in the custom stateen-like registers. I would think that this bit would be anded with each bit in the custom stateen-like registers to produce the true enable. This is probably one in the same with my expectation of only two outcomes.

Bill

-----Original Message-----
From: tech-privileged@... <tech-privileged@...> On Behalf Of John Hauser
Sent: Wednesday, April 21, 2021 1:51 PM
To: tech-privileged@...
Subject: Re: [RISC-V] [tech-privileged] proposal for stateen CSRs

EXTERNAL MAIL


I was asked privately to better explain to the list why having just one special bit in mstateen0 (and presumably likewise for the hstateen0 and
sstateen0) isn't sufficient. Here's my attempt:

For each write to mstateen0 we want one of three possible outcomes:

- Enable lower-privilege access to all custom state.

- Disable lower-privilege access to all custom state.

- Preserve the lower-privilege access to individual subsets of the
custom state, as controlled by stateen-like custom registers.

Whatever else you may believe about how the mechanism should best be defined, it should be clear that three possible desired outcomes cannot be encoded in a single bit written to mstateen0.

- John Hauser


Re: proposal for stateen CSRs

John Hauser
 

I was asked privately to better explain to the list why having just one
special bit in mstateen0 (and presumably likewise for the hstateen0 and
sstateen0) isn't sufficient. Here's my attempt:

For each write to mstateen0 we want one of three possible outcomes:

- Enable lower-privilege access to all custom state.

- Disable lower-privilege access to all custom state.

- Preserve the lower-privilege access to individual subsets of the
custom state, as controlled by stateen-like custom registers.

Whatever else you may believe about how the mechanism should best be
defined, it should be clear that three possible desired outcomes cannot
be encoded in a single bit written to mstateen0.

- John Hauser


Re: proposal for stateen CSRs

Scott Johnson
 

Yes, I get it now, and I like your proposal. Thanks for the explanation.

On Apr 21, 2021, at 10:37 AM, John Hauser <jh.riscv@...> wrote:

Scott Johnson wrote:
Like Allen, I'm not following the need for this complexity. Why not one
simple read/write bit that disables all custom state when 0? It would be
ANDed with any custom state enable CSR bits, much like misa.X=0 disables
all custom extensions.
Me:
How would that allow one to enable lower-privilege access to all
unknown custom state?
Scott:
The M-mode software would write a 1 to that bit?
Let's say the custom registers that control lower-privilege access
to custom state are all zeros, possibly because they get initialized
that way by reset. We now write a one to the special bit in mstateen0.
According to your proposal above, this special bit from mstateen0 gets
ANDed with the specific enables in the custom registers to give us all
zeros, meaning the custom state remains inaccessible to lower-privilege
modes.

I don't see how writing one to the special bit in mstateen0
accomplishes what you think.

Oooh. I think I got you now. How would the S-mode software enable all the
custom enable bits in those custom CSRs?
Concerning the custom enable bits controlled at machine level,
supervisor-level software doesn't have the authority to set those bits,
only machine level does. For the access-enable bits controlled at
supervisor level, I'm proposing that sstateen0 have magic bits just
like mstateen0; and likewise for hstateen0. (Noted in one of my recent
messages.)

- John Hauser





Re: proposal for stateen CSRs

John Hauser
 

Scott Johnson wrote:
Like Allen, I'm not following the need for this complexity. Why not one
simple read/write bit that disables all custom state when 0? It would be
ANDed with any custom state enable CSR bits, much like misa.X=0 disables
all custom extensions.
Me:
How would that allow one to enable lower-privilege access to all
unknown custom state?
Scott:
The M-mode software would write a 1 to that bit?
Let's say the custom registers that control lower-privilege access
to custom state are all zeros, possibly because they get initialized
that way by reset. We now write a one to the special bit in mstateen0.
According to your proposal above, this special bit from mstateen0 gets
ANDed with the specific enables in the custom registers to give us all
zeros, meaning the custom state remains inaccessible to lower-privilege
modes.

I don't see how writing one to the special bit in mstateen0
accomplishes what you think.

Oooh. I think I got you now. How would the S-mode software enable all the
custom enable bits in those custom CSRs?
Concerning the custom enable bits controlled at machine level,
supervisor-level software doesn't have the authority to set those bits,
only machine level does. For the access-enable bits controlled at
supervisor level, I'm proposing that sstateen0 have magic bits just
like mstateen0; and likewise for hstateen0. (Noted in one of my recent
messages.)

- John Hauser


Re: proposal for stateen CSRs

Scott Johnson
 

Oooh. I think I got you now. How would the S-mode software enable all the custom enable bits in those custom CSRs?


On Wed, Apr 21, 2021 at 10:19 AM John Hauser <jh.riscv@...> wrote:
Scott Johnson wrote:
> Like Allen, I'm not following the need for this complexity. Why not one
> simple read/write bit that disables all custom state when 0? It would be
> ANDed with any custom state enable CSR bits, much like misa.X=0 disables
> all custom extensions.

How would that allow one to enable lower-privilege access to all
unknown custom state?  You said yourself earlier:

> But there's also the use model of the M-mode software that doesn't do any
> environment swapping and just wants to enable all state for the S-mode. It
> would be nice if that didn't have to be aware of custom state.

    - John Hauser






Re: proposal for stateen CSRs

Scott Johnson
 

The M-mode software would write a 1 to that bit?


On Wed, Apr 21, 2021 at 10:19 AM John Hauser <jh.riscv@...> wrote:
Scott Johnson wrote:
> Like Allen, I'm not following the need for this complexity. Why not one
> simple read/write bit that disables all custom state when 0? It would be
> ANDed with any custom state enable CSR bits, much like misa.X=0 disables
> all custom extensions.

How would that allow one to enable lower-privilege access to all
unknown custom state?  You said yourself earlier:

> But there's also the use model of the M-mode software that doesn't do any
> environment swapping and just wants to enable all state for the S-mode. It
> would be nice if that didn't have to be aware of custom state.

    - John Hauser






Re: proposal for stateen CSRs

John Hauser
 

Scott Johnson wrote:
Like Allen, I'm not following the need for this complexity. Why not one
simple read/write bit that disables all custom state when 0? It would be
ANDed with any custom state enable CSR bits, much like misa.X=0 disables
all custom extensions.
How would that allow one to enable lower-privilege access to all
unknown custom state? You said yourself earlier:

But there's also the use model of the M-mode software that doesn't do any
environment swapping and just wants to enable all state for the S-mode. It
would be nice if that didn't have to be aware of custom state.
- John Hauser


Re: proposal for stateen CSRs

John Hauser
 

Why does all custom state need to be controlled with a single bit? Couldn't
we just designate the upper 8 or 16 bits of each xstateenY register to be
custom? Any software that wanted to disable all custom state would just
clear all those bits, while software that wanted to enable all custom state
would just write ones. (WARL would cause any unused bits to stay zero even
after they were written with a value of one).
The more bits we dedicate to custom use to ensure that we've got
enough for everyone, the quicker we use up each *stateen CSR, when we
can be pretty sure most of those custom bits will be unused in most
implementations. My suggestion is to allocate exactly two bits in
mstateen0, hstateen0, and stateen0, and let implementations allocate
all their specific custom bits in custom registers. Personally, I'm
entirely unbothered by those two bits having magic behavior, so long as
read-modify-write works as it should for the *stateen CSRs.

- John Hauser


Re: proposal for stateen CSRs

Scott Johnson
 

Code that does environment swapping will need to use custom instructions to save custom state, so might as well use custom CSRs for the individual enable bits instead of taking up space in the standard *stateen.

The use case for the single bit is only for M-mode software that wants to enable/disable all custom state. No need to take up more than a single bit for that.

On Wed, Apr 21, 2021 at 9:41 AM Jonathan Behrens <behrensj@...> wrote:
Why does all custom state need to be controlled with a single bit? Couldn't we just designate the upper 8 or 16 bits of each xstateenY register to be custom? Any software that wanted to disable all custom state would just clear all those bits, while software that wanted to enable all custom state would just write ones. (WARL would cause any unused bits to stay zero even after they were written with a value of one).

Jonathan

On Wed, Apr 21, 2021 at 12:37 PM John Hauser via lists.riscv.org <jh.riscv=jhauser.us@...> wrote:
Allen Baum wrote:
> If I understand the above: you can write a 1 to the first bit, but you'll
> always read back zero? What does that accomplish?

It provides a convenient means for software to enable lower-privilege
access to all custom state, requiring only the minimal amount of
hardware for the task.

    - John Hauser






Re: proposal for stateen CSRs

Jonathan Behrens <behrensj@...>
 

Why does all custom state need to be controlled with a single bit? Couldn't we just designate the upper 8 or 16 bits of each xstateenY register to be custom? Any software that wanted to disable all custom state would just clear all those bits, while software that wanted to enable all custom state would just write ones. (WARL would cause any unused bits to stay zero even after they were written with a value of one).

Jonathan


On Wed, Apr 21, 2021 at 12:37 PM John Hauser via lists.riscv.org <jh.riscv=jhauser.us@...> wrote:
Allen Baum wrote:
> If I understand the above: you can write a 1 to the first bit, but you'll
> always read back zero? What does that accomplish?

It provides a convenient means for software to enable lower-privilege
access to all custom state, requiring only the minimal amount of
hardware for the task.

    - John Hauser






Re: proposal for stateen CSRs

John Hauser
 

Allen Baum wrote:
If I understand the above: you can write a 1 to the first bit, but you'll
always read back zero? What does that accomplish?
It provides a convenient means for software to enable lower-privilege
access to all custom state, requiring only the minimal amount of
hardware for the task.

- John Hauser


Re: proposal for stateen CSRs

Scott Johnson
 

Like Allen, I'm not following the need for this complexity. Why not one simple read/write bit that disables all custom state when 0? It would be ANDed with any custom state enable CSR bits, much like misa.X=0 disables all custom extensions.


On Wed, Apr 21, 2021 at 12:09 AM John Hauser <jh.riscv@...> wrote:
I wrote:
> This seems like a sensible request to me, except I would accomplish the
> same effect by adding a single "write-only" bit to mstateen0.  The bit
> always reads as zero, and writing zero to it does nothing; but writing
> a one causes all custom state to be enabled for lower privilege levels.
> [...]

Scott Johnson:
> I agree one bit is sufficient, but I don’t understand the write-only,
> read-zero behavior?
>
> Would M-mode never be able to revoke permissions?

After I wrote my earlier message, I realized we need to be able to
revoke permissions, and this is best supported with a second bit
alongside the first one.  The second bit reads as one if any custom
state is enabled, and as zero if no custom state is enabled.  For the
second bit, writing one does nothing, but writing zero disables access
to all the custom state.

In summary, on reads:

    1st bit:  always 0
    2nd bit:  1 if any custom state is enabled; 0 if none is enabled

On writes:

    1st bit:  0 does nothing; 1 enables all custom state
    2nd bit:  0 disables all custom state; 1 does nothing

To safely support read-modify-write of the stateen CSRs, the first bit
could read as one only to indicate that all custom state is enabled.
However, I don't believe software needs to know that, hence I don't
want to require people to build the AND-gate tree for that, regardless
of how small it might be.  So that's how that bit ended up as read-only
zero.

On the other hand, for the second bit, software may be interested to
know whether _any_ custom state has been enabled.  The alternative
for the second bit would be to make it read-only one, which isn't as
attractive.

    - John Hauser






Re: proposal for stateen CSRs

Allen Baum
 

If I understand the above: you can write a 1 to the first bit, but you'll always read back zero? What does that accomplish?

I think you need to establish what the (legal) reset state(s) is/are first.
Then you can define the bits to be RW1S (readable, write  1 to set/write 0 does nothing) or RW0C (readable, write 0 to clear/write 1 does nothing) or just RW, etc

On Wed, Apr 21, 2021 at 12:09 AM John Hauser <jh.riscv@...> wrote:
I wrote:
> This seems like a sensible request to me, except I would accomplish the
> same effect by adding a single "write-only" bit to mstateen0.  The bit
> always reads as zero, and writing zero to it does nothing; but writing
> a one causes all custom state to be enabled for lower privilege levels.
> [...]

Scott Johnson:
> I agree one bit is sufficient, but I don’t understand the write-only,
> read-zero behavior?
>
> Would M-mode never be able to revoke permissions?

After I wrote my earlier message, I realized we need to be able to
revoke permissions, and this is best supported with a second bit
alongside the first one.  The second bit reads as one if any custom
state is enabled, and as zero if no custom state is enabled.  For the
second bit, writing one does nothing, but writing zero disables access
to all the custom state.

In summary, on reads:

    1st bit:  always 0
    2nd bit:  1 if any custom state is enabled; 0 if none is enabled

On writes:

    1st bit:  0 does nothing; 1 enables all custom state
    2nd bit:  0 disables all custom state; 1 does nothing

To safely support read-modify-write of the stateen CSRs, the first bit
could read as one only to indicate that all custom state is enabled.
However, I don't believe software needs to know that, hence I don't
want to require people to build the AND-gate tree for that, regardless
of how small it might be.  So that's how that bit ended up as read-only
zero.

On the other hand, for the second bit, software may be interested to
know whether _any_ custom state has been enabled.  The alternative
for the second bit would be to make it read-only one, which isn't as
attractive.

    - John Hauser






Re: proposal for stateen CSRs

Paul Donahue
 

Having a more misa-like implementation would also allow virtualization of an implementation without support for a particular stateless extension such as A or B, though it would take more bits.  Via emulation, it's possible for a VM to be more capable than the hardware but I don't think that it's possible for a VM to be less capable than the hardware (without a bunch of interaction with M mode using a non-existent SBI call to set misa).


Thanks,

-Paul


On Tue, Apr 20, 2021 at 8:20 PM Jonathan Behrens <behrensj@...> wrote:
The proposed mstateenX CSRs feels very similar to misa. It almost seems like you could achieve the same thing as this proposal just by adding sisa and hisa (plus misa2, misa3, ...), but I think I've convinced myself that wouldn't quite work.

I'll also point out that we'll probably want to reserve a bit in [m|s]stateen0 for the H-extension.

Jonathan

On Tue, Apr 20, 2021 at 11:01 PM John Hauser via lists.riscv.org <jh.riscv=jhauser.us@...> wrote:
Allen Baum wrote:
> Questions: (I suspect I know the answers, but want to be clear):
>   -if  mstateenY[n] is cleared, and s-mode code tries to read/write a CSR
> that is controlled by that bit
>     -- does the access trap?
>     -- else does it cause read/writes of the CSR from Smode
>         --- to return zero/have no effect
>         --- to return the value it had at the time that the bit was
> cleared/have no effect
>
> Or put another way: what exactly does "control access" mean?

I forgot to say what that means, didn't I?  Good point!

To quote Admiral Ackbar:  "It's a trap!"  You get an illegal
instruction trap, unless you're executing in a virtual machine (V = 1)
and the access is _not_ being blocked by the mstateen CSRs, in which
case you get a virtual instruction trap.

(Related to that, it's on my to-do list to add to the Privileged
Architecture's hypervisor chapter a better general explanation for
when you should get a virtual instruction trap instead of an illegal
instruction trap.)

    - John Hauser






Re: proposal for stateen CSRs

John Hauser
 

I wrote:
This seems like a sensible request to me, except I would accomplish the
same effect by adding a single "write-only" bit to mstateen0. The bit
always reads as zero, and writing zero to it does nothing; but writing
a one causes all custom state to be enabled for lower privilege levels.
[...]
Scott Johnson:
I agree one bit is sufficient, but I don’t understand the write-only,
read-zero behavior?

Would M-mode never be able to revoke permissions?
After I wrote my earlier message, I realized we need to be able to
revoke permissions, and this is best supported with a second bit
alongside the first one. The second bit reads as one if any custom
state is enabled, and as zero if no custom state is enabled. For the
second bit, writing one does nothing, but writing zero disables access
to all the custom state.

In summary, on reads:

1st bit: always 0
2nd bit: 1 if any custom state is enabled; 0 if none is enabled

On writes:

1st bit: 0 does nothing; 1 enables all custom state
2nd bit: 0 disables all custom state; 1 does nothing

To safely support read-modify-write of the stateen CSRs, the first bit
could read as one only to indicate that all custom state is enabled.
However, I don't believe software needs to know that, hence I don't
want to require people to build the AND-gate tree for that, regardless
of how small it might be. So that's how that bit ended up as read-only
zero.

On the other hand, for the second bit, software may be interested to
know whether _any_ custom state has been enabled. The alternative
for the second bit would be to make it read-only one, which isn't as
attractive.

- John Hauser


Re: proposal for stateen CSRs

John Hauser
 

Jonathan Behrens wrote:
I'll also point out that we'll probably want to reserve a bit in
[m|s]stateen0 for the H-extension.
That would be true, except we already have the "H" bit in misa which is
expected to be writable.

Also, to ease future hardware support for nested hypervisors, it turns
out the hypervisor extension is a special case for which having an
enable bit located in hstateen0 is not ideal. The reason is a bit
subtle but has to do with the recursive nature of nested hypervisor
support, when hypervisors are running guests that act as hypervisors
themselves. Having the hypervisor enable bit in hstateen0 can be made
to work, but it's more efficient to place it elsewhere.

So by dumb luck, our earlier assignment of the misa "H" bit for the
hypervisor extension just happens to align with future plans for better
hardware support for nested hypervisors.

- John Hauser


Re: proposal for stateen CSRs

Scott Johnson
 

I agree one bit is sufficient, but I don’t understand the write-only, read-zero behavior?

Would M-mode never be able to revoke permissions?


On Tue, Apr 20, 2021 at 6:33 PM John Hauser <jh.riscv@...> wrote:
Scott Johnson wrote:
> It seems the use model is to enable older environment-swapping software to
> run on newer hardware without opening a security hole via new state. Only
> if the software is aware of the state will it enable that state via
> *stateen.
>
> For custom state, you can always make custom *stateen-like CSRs to enable
> it. You're going to need custom software to swap that state anyway.

Yes, that's exactly what we were thinking.

> But there's also the use model of the M-mode software that doesn't do any
> environment swapping and just wants to enable all state for the S-mode. It
> would be nice if that didn't have to be aware of custom state.
>
> So, I join Bill in his question. It seems a good idea to reserve some bits
> for custom state for that latter use model.

This seems like a sensible request to me, except I would accomplish the
same effect by adding a single "write-only" bit to mstateen0.  The bit
always reads as zero, and writing zero to it does nothing; but writing
a one causes all custom state to be enabled for lower privilege levels.
As you suggested, there will presumably be custom CSRs containing
one or more bits that control access to the custom state, akin to
the mstateen CSRs for standardized state.  Machine-level software may
consult and modify those custom-state-controlling bits directly if it
has sufficient knowledge, but won't have to.

    - John Hauser






Re: proposal for stateen CSRs

Bill Huffman
 

 

 

From: Scott Johnson <scott.johnson@...>
Sent: Tuesday, April 20, 2021 8:33 PM
To: Bill Huffman <huffman@...>
Cc: tech-privileged@...
Subject: Re: [RISC-V] [tech-privileged] proposal for stateen CSRs

 

EXTERNAL MAIL

It seems the use model is to enable older environment-swapping software to run on newer hardware without opening a security hole via new state. Only if the software is aware of the state will it enable that state via *stateen.

 

For custom state, you can always make custom *stateen-like CSRs to enable it. You're going to need custom software to swap that state anyway.

 

True.  I was thinking of unifying the two purposes when I asked the question.

 

      Bill

 


Re: proposal for stateen CSRs

Jonathan Behrens <behrensj@...>
 

The proposed mstateenX CSRs feels very similar to misa. It almost seems like you could achieve the same thing as this proposal just by adding sisa and hisa (plus misa2, misa3, ...), but I think I've convinced myself that wouldn't quite work.

I'll also point out that we'll probably want to reserve a bit in [m|s]stateen0 for the H-extension.

Jonathan


On Tue, Apr 20, 2021 at 11:01 PM John Hauser via lists.riscv.org <jh.riscv=jhauser.us@...> wrote:
Allen Baum wrote:
> Questions: (I suspect I know the answers, but want to be clear):
>   -if  mstateenY[n] is cleared, and s-mode code tries to read/write a CSR
> that is controlled by that bit
>     -- does the access trap?
>     -- else does it cause read/writes of the CSR from Smode
>         --- to return zero/have no effect
>         --- to return the value it had at the time that the bit was
> cleared/have no effect
>
> Or put another way: what exactly does "control access" mean?

I forgot to say what that means, didn't I?  Good point!

To quote Admiral Ackbar:  "It's a trap!"  You get an illegal
instruction trap, unless you're executing in a virtual machine (V = 1)
and the access is _not_ being blocked by the mstateen CSRs, in which
case you get a virtual instruction trap.

(Related to that, it's on my to-do list to add to the Privileged
Architecture's hypervisor chapter a better general explanation for
when you should get a virtual instruction trap instead of an illegal
instruction trap.)

    - John Hauser






Re: proposal for stateen CSRs

John Hauser
 

Allen Baum wrote:
Questions: (I suspect I know the answers, but want to be clear):
-if mstateenY[n] is cleared, and s-mode code tries to read/write a CSR
that is controlled by that bit
-- does the access trap?
-- else does it cause read/writes of the CSR from Smode
--- to return zero/have no effect
--- to return the value it had at the time that the bit was
cleared/have no effect

Or put another way: what exactly does "control access" mean?
I forgot to say what that means, didn't I? Good point!

To quote Admiral Ackbar: "It's a trap!" You get an illegal
instruction trap, unless you're executing in a virtual machine (V = 1)
and the access is _not_ being blocked by the mstateen CSRs, in which
case you get a virtual instruction trap.

(Related to that, it's on my to-do list to add to the Privileged
Architecture's hypervisor chapter a better general explanation for
when you should get a virtual instruction trap instead of an illegal
instruction trap.)

- John Hauser