Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 656
xtt Avatar asked Sep 13 '19 15:09

xtt


People also ask

Where is Cpl stored?

The current privilege level (CPL) of the processor is stored in the lowest 2 bits of the code segment selector (CS). The highest privilege level is number zero. This level is commonly known as Kernel Mode for Linux and Ring 0 for Windows-based operating systems.

What is the CR0 register?

CR0. The CR0 register is 32 bits long on the 386 and higher processors. On x64 processors in long mode, it (and the other control registers) is 64 bits long. CR0 has various control flags that modify the basic operation of the processor. Register CR0 is the 32 Bit version of the old Machine Status Word (MSW) register.

What is CPL in x86?

x86 Switching to protected mode from real mode CPL (Current Privilege Level)

What are segment registers used for?

The segment registers stores the starting addresses of a segment. To get the exact location of data or instruction within a segment, an offset value (or displacement) is required.


1 Answers

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.

like image 135
Michael Petch Avatar answered Dec 14 '22 00:12

Michael Petch