9

I am reading through Linux Kernel code. I have some doubt regarding GDT(Global Descriptor Table) in Linux.

My Questions are:

Where Linux Kernel Setup Large GDT?
I know that in pm.c [http://lxr.free-electrons.com/source/arch/x86/boot/pm.c#L123] 
kernel call function setup_gdt() and it set up a small GDT with only three entries. 

Then jumps to protected mode code.
Then where is the code to setup large GDT with 32 entries 

( One specified in Understanding Linux Kernel  https://books.google.co.in/books?id=h0lltXyJ8aIC&pg=PT59&lpg=PT59&dq=linux+kernel+file+gdt&source=bl&ots=gO0lH05fHX&sig=h4X1I6TP_P7JlEwzoCkQk3uztjw&hl=en&sa=X&ei=XFwPVM-WBbOBsQTtiIDIDw&redir_esc=y#v=onepage&q=linux%20kernel%20file%20gdt&f=false  )

Kernel also defined constants for Large GDT set up in segment.h http://lxr.free-electrons.com/source/arch/x86/include/asm/segment.h#L46.

Why kernel use two steps to set up GDT?

If we are using GRUB as boot loader then, GRUB also set up one GDT in grub-core/kern/i386/realmode.S. Why Linux reset this GDT?

HSJ
  • 91
  • 4
  • Did You hear of the cpu registers GS/FS ? One of them carries the GDT, IIRC. – icbytes Jul 26 '16 at 10:06
  • 1
    That is completely false. The GDT is pointed to by GDTR. The FS/GS registers aren't used implicitly anywhere in the architecture, but instructions referencing memory can generally be overloaded to use the FS or GS segment instead of the default (usually DS; sometimes SS or ES). The FS and GS registers have various uses on Linux, such as TLS or stack cookies (https://en.wikipedia.org/wiki/X86_memory_segmentation). – icecreamsword Jun 23 '17 at 18:26
  • To clarify, in protected mode the GS/FS registers (as well as all other segment registers) store *selectors*, which are references to GDT entries (as well as a few other bits of data). Their behavior is totally different from when the CPU is in real-mode. The actual pointer to the GDT is stored in a different structure; a pointer to *that* structure is stored in the CPU's GDTR register. – adrian Jun 16 '20 at 00:58

1 Answers1

2

The layout of the main GDT seems to be defined in arch/x86/include/asm/segment.h in line 91.

The initialization seems to be in arch/x86/kernel/cpu/common.c in line 123.

But I may be wrong. And Don't know why kernel use two steps to setup GDT.

JustOneMan
  • 231
  • 1
  • 9
  • 34