rust-osdev / x86_64

Library to program x86_64 hardware.
https://docs.rs/x86_64
Apache License 2.0
797 stars 132 forks source link

Add Descriptor::dpl const method and use it in GDT construction #410

Closed josephlr closed 1 year ago

josephlr commented 1 year ago

This requires making PrivilegeLevel::from_u16 const. That, in turn, requires making the panic message slightly less informative.

Note that this is also a functional change when building a GDT. Before, if you had a User/System Segment, the RPL would always be 0, unless it was a User Segment with a DPL of 3, in which case the RPL would be 3. Now the RPL will consistently match the DPL. This seems like the more intuitive approach. However, I doubt it makes much of a difference in practice (as the RPL mechanism is sort of useless to begin with).

If this functional change is an issue, I can add in code (and documentation) to explicitly define the DPL -> RPL mapping.

Freax13 commented 1 year ago

Note that this is also a functional change when building a GDT. Before, if you had a User/System Segment, the RPL would always be 0, unless it was a User Segment with a DPL of 3, in which case the RPL would be 3. Now the RPL will consistently match the DPL. This seems like the more intuitive approach. However, I doubt it makes much of a difference in practice (as the RPL mechanism is sort of useless to begin with).

As I understand it, the CPU only checks that RPL<=DPL on access, so setting RPL=DPL instead of RPL=0 should not change anything. Right?

Yes, that is correct for data segments. However, accessing stack segments requires the CPL and the RPL and DPL of the stack segment to be equal. The changes introduced here should make it possible to access stack segments with a CPL of 1 and 2.

josephlr commented 1 year ago

Added some comments clarifying the DPL_RING_3 flag, removing the the comment saying that the DPL is ignored in 64-bit mode data segments, explaining the exception for stack segments.