1
votes

I want to implement 32-bit 4Gb Flat Memory mapping for an application, for this purpose I have to get and update Data Segment Descriptor and Code Segment Descriptors. By using assembly command "sgdt" i can get Global Descriptor Table but I am not sure if its CS or DS or any other descriptor. It would be really appreciated if some one can help me deal with this confusion.

I am relying on GRUB to set GDT that's why i don't know the exact location where it put all the segment descriptors. By viewing the GDT table entries I can see 5 same Code Segment Entries and 6 Same Data Segment entries, with base address of each entry set to '0' and limit of each set to '0xfffff'. Can you please tell me reason for these duplicate entries? And from these viewing, can you kindly confirm that Flat Memory mode is already being set by GRUB?

1
It gives you the 16-bit limit + the 32-bit linear address of the whole table, not one of the entries. felixcloutier.com/x86/sgdt.Peter Cordes
You could mov eax, cs and index the table of GDT-entry structs. wiki.osdev.org/Global_Descriptor_Table. You're supposed to know what you put in the GDT, if you're writing your own OS. If you're on Linux, there's a modify_ldt system call to ask the OS to create new GDT entries for you. What are you doing where you don't already have a 32-bit flat memory model?Peter Cordes
See felixcloutier.com/x86/lsl for a description of how to decode segment limits: there's a bit that says whether it's a byte limit or a page limit. 0xfffff times 4k is 4GiB, and it does seem likely that GRUB would set up a flat memory model. That's what everyone wants.Peter Cordes
I'n going to chime in. DO NOT rely on the GDT in GRUB. Consider it temporary and should be replaced by your own. The Multiboot specification doesn't say how the GDT will look and what entries it uses. It lists the values in the segments as "undefined". Even worse is that you can't even assume the GDTR is valid (per the same docs).This means the moment you wish to change the segment registers directly or indirectly) there is no guarantee it will work.Michael Petch
The moment you start creating an IDT with entries (which requires knowing what CS actually is) you will be in trouble. Since CS is reloaded to perform an interrupt request you risk having it fail the first time an interrupt is fired because the GDTR may be invalid. Don't rely on it working a particular way. Always do it right - create your own GDT and load it yourself with lgdtMichael Petch

1 Answers

1
votes

Answering my own question after getting great response in comments section. GDT Table contains the entries for Segment descriptors in protected mode, The Code Segment and Data Segment are identified by reading value of Code Segment Register / Data Segment Register where it contains the index for particular segment (also called Segment Selector) in GDT table.