Understanding the structure and loading of gdt
Posted: Sat Feb 15, 2020 4:43 pm
Okay so I'm currently in the progress of setting up gdt in protected mode. I'm booted and loaded into protected mode by grub2 so I'm in flat memory mode currently.
I'm reading this article at the forum https://wiki.osdev.org/GDT_Tutorial but I'm confused about the loading and structure of the gdt data from the memory address provided to the example function written in assembly. The example function is as follows and I added comments here to make sure I understand the assembly correctly:
What I could not figure out (after two days of looking around in different places) is that what kind of data the GDT parameter passed to the assembly should contain.
I think that I figured out few options that what the param should be, could you please tell me if some of these options is right or am I totally lost with this:
Option1: the gdt is a 32 bit data that is passed to the assembly by the c compiler using stack. The data contains a single gdt descriptor and as such the size is always 4?
Option2: the gdt parameter is a pointer to a list of gdt descriptor elements and the second parameter describes the number of those entries?
This must be something very simple but I just could not find a proper explanation for the LGDT instruction that would be easily understandable. I have been coding some assembly for the x86 before but those were in userspace programs so this is the first time I'm dealing with ring 0 instructions.
Also I'm confused when reading the example code for creating gdt entries. The example C code provided on the same page creates gdt entries that are 64 bits long but according to my understanding of the example assembly code that loads the gdt the passed gdt parameter should be 32 bits long?
I must be missing something very obvious here, could you please clarify this for me.
I'm reading this article at the forum https://wiki.osdev.org/GDT_Tutorial but I'm confused about the loading and structure of the gdt data from the memory address provided to the example function written in assembly. The example function is as follows and I added comments here to make sure I understand the assembly correctly:
Code: Select all
gdtr DW 0 ; For limit storage
DD 0 ; For base storage
setGdt:
MOV EAX, [esp + 4] ;Load the passed variable GDT (32bits) when called from c with setGdt(GDT, sizeof(GDT))
MOV [gdtr + 2], EAX ;Copy the dword to the third memory address counted from gdtr label
MOV AX, [ESP + 8] ;Copy the size of the passed GDT parameter (4?) to the low word of the eax register
MOV [gdtr], AX ;Copy the size to the gdtr data structure
LGDT [gdtr] ;Now load the gdt from the provided data
RET
I think that I figured out few options that what the param should be, could you please tell me if some of these options is right or am I totally lost with this:
Option1: the gdt is a 32 bit data that is passed to the assembly by the c compiler using stack. The data contains a single gdt descriptor and as such the size is always 4?
Option2: the gdt parameter is a pointer to a list of gdt descriptor elements and the second parameter describes the number of those entries?
This must be something very simple but I just could not find a proper explanation for the LGDT instruction that would be easily understandable. I have been coding some assembly for the x86 before but those were in userspace programs so this is the first time I'm dealing with ring 0 instructions.
Also I'm confused when reading the example code for creating gdt entries. The example C code provided on the same page creates gdt entries that are 64 bits long but according to my understanding of the example assembly code that loads the gdt the passed gdt parameter should be 32 bits long?
I must be missing something very obvious here, could you please clarify this for me.