0

Understanding Linux kernel (https://www.amazon.in/Understanding-Linux-Kernel-Process-Management-ebook/dp/B0043D2E54) mentions the following:

As stated earlier, the Current Privilege Level of the CPU indicates whether the processor is in User or Kernel Mode and is specified by the RPL field of the Segment Selector stored in the cs register. Whenever the CPL is changed, some segmentation registers must be correspondingly updated.

For instance, when the CPL is equal to 3 (User Mode), the ds register must contain the Segment Selector of the user data segment,but when the CPL is equal to 0, the ds register must contain the Segment Selector of the kernel data segment.

A similar situation occurs for the ss register. It must refer to a User Mode stack inside the user data segment when the CPL is 3, and it must refer to a Kernel Mode stack inside the kernel data segment when the CPL is 0. When switching from User Mode to Kernel Mode, Linux always makes sure that the ss register contains the Segment Selector of the kernel data segment.

Based on the above, I have few questions:

1) What are the RPL in the segment selectors stored in the other segmentation registers used for?

2) When a system call is executing on behalf of a user process, the RPL in cs will be set to 3 (Difference between DPL and RPL in x86). In this case will the data segment (ds) contain __USER_DS instead of __KERNEL_DS, and if so how can the implementation of the system call have access to kernel data structures etc?

Community
  • 1
  • 1
Abhigyan Mehra
  • 215
  • 1
  • 4
  • 7
  • 1) RPL works the same in all of the segment registers; it lowers access whenever you access some offset of that segment. It just so happens that `cs` is for your instruction pointer, `ss` is for stack operations, and `ds` is most general memory access. `es` is used in string instructions, and you can use segment override prefixes to specify which segment register is used – user1354557 Nov 14 '16 at 17:03
  • 2) Actually `cs` would not have an RPL of 3, because then you would not be able to execute the system call in ring 0. I believe `ds` would also need to have an RPL of 0. The use case scenario they described was for data access for a logical address (segment selector + address). So the call from user mode would specify the segment selector through which to access some address. But this design was suggested before x86 supported paging, so operating systems do work like this now. – user1354557 Nov 14 '16 at 17:14
  • @user1354557 Thank you sir! I missed the point that it would be the RPL of the segment selector passed in arguments which would be set to 3 in the use case. – Abhigyan Mehra Nov 15 '16 at 03:46

0 Answers0