Search code examples
x86x86-64cpu-architecturecpu-registersi386

What register in i386 stores the CPL?


I read in "INTEL 80386 PROGRAMMER'S REFERENCE MANUAL" (p112, S 6.3.1.3) that

An internal processor register records the current privilege level (CPL).

I am curious as to what register it refers to. Does it even have a name? What's the size of the register? Does it have any other use?


Solution

  • The Current Privilege Level (CPL) can always be found in the lower 2 bits of the Code Segment (CS) register. Those 2 bits can be the value 0b00 (ring 0), 0b01 (ring 1), 0b10 (ring 2), 0b11 (ring 3).

    It should be noted that the old documentations use of "An internal processor register records the current privilege level (CPL)" is a bit deceptive and has caused some head scratching for others as well. CS always contains the CPL in the lower 2 bits but obviously it isn't an internal register.

    The microarchitecture may have a copy of the CPL internally as well, but it is always accessible programmatically by looking at CS.

    Not directly related to your question, but may be useful to know. If you transitioned between different rings and the destination code segment selector had a descriptor that is a conforming segment, it's possible for the Descriptor Privilege Level (DPL) != CPL. This is because with a conforming segment you continue to run with the previous privilege level. With non-conforming segments DPL == CPL.