October 22, 2024
Chicago 12, Melborne City, USA
C#

Using global variables after booting from GRUB before setting up the GDT


I’m writing a toy-kernel and the first step would be to set-up the GDT. Obviously I will be using variables on the stack to initialize the GDT; for which, a GDT must already be set up, which is indeed the case as GRUB also sets up a GDT before passing control to the kernel.

According to the multiboot specification:

‘CS’
Must be a 32-bit read/execute code segment with an offset of ‘0’ and a limit of > ‘0xFFFFFFFF’. The exact value is undefined.

‘DS’

‘ES’

‘FS’

‘GS’

‘SS’
Must be a 32-bit read/write data segment with an offset of ‘0’ and a limit of ‘0xFFFFFFFF’. The exact values are all undefined.

However, the segment register DS corresponding to the data segment does not have to conform to anything according to the specification. This would mean that I cannot reliably use logical addresses corresponding to the data segment before setting up my GDT.

I was looking at various online resources to see how they’ve setup the GDT, and one of those were James Molloy’s tutorials. In this, he uses a few global variables (in descriptor_tables.c)

gdt_entry_t gdt_entries[5];
gdt_ptr_t   gdt_ptr;
idt_entry_t idt_entries[256];
idt_ptr_t   idt_ptr; 

to help set up the GDT setup by the OS. To my understanding these global variables would be a part of the data segment and any instruction attempting to use these would default to using the data segment register DS. However, as this is not guaranteed to be anything this could lead to big issues.

I fail to understand how global variables which should be accessed using the DS segment register are used before setting up the GDT. Thanks.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video