Segment definitions for linux on x86

Linux 3.4.6 defines the following macros in arch / x86 / include / asm / segment.h. Can someone explain why __USER macros add 3 to a specific constant and why this is not done for __KERNEL macros?

#define __KERNEL_CS (GDT_ENTRY_KERNEL_CS*8)
#define __KERNEL_DS (GDT_ENTRY_KERNEL_DS*8)
#define __USER_DS   (GDT_ENTRY_DEFAULT_USER_DS*8+3)
#define __USER_CS   (GDT_ENTRY_DEFAULT_USER_CS*8+3)
+5
source share
1 answer

These four characters represent segment descriptors . The two least significant bits of these descriptors contain the privilege level associated with them , and the third least significant bit contains the type of the descriptor table ( GDT or LDT ). This becomes clearer when the code appears a bit later:

/* User mode is privilege level 3 */
#define USER_RPL                0x3
/* LDT segment has TI set, GDT has it cleared */
#define SEGMENT_LDT             0x4
#define SEGMENT_GDT             0x0

/* Bottom two bits of selector give the ring privilege level */
#define SEGMENT_RPL_MASK        0x3
/* Bit 2 is table indicator (LDT/GDT) */
#define SEGMENT_TI_MASK         0x4

8, , OR ed ( ):

/* GDT, ring 0 (kernel mode) */
#define __KERNEL_CS (GDT_ENTRY_KERNEL_CS*8)

/* GDT, ring 3 (user mode) */
#define __USER_CS   (GDT_ENTRY_DEFAULT_USER_CS*8+3)
+5

All Articles