How do I create/load a GDT when I am in PMode?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
BenjiWiebe
Posts: 20
Joined: Thu Feb 07, 2013 9:47 pm
Location: Durham, Kansas
Contact:

How do I create/load a GDT when I am in PMode?

Post by BenjiWiebe »

How do I create/load a GDT when I am in PMode as opposed to Real Mode? What are the differences? In my gdt.s, do I specify bits 16 or bits 32? Do I switch back into real mode, load the GDT, and switch into Pmode? Or what?

The reason I am asking is because I am using the GRUB2 bootloader, which (I understand) puts you into Pmode, and I have not yet set up any GDT.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: How do I create/load a GDT when I am in PMode?

Post by Gigasoft »

It's exactly the same.
User avatar
trinopoty
Member
Member
Posts: 87
Joined: Wed Feb 09, 2011 2:21 am
Location: Raipur, India

Re: How do I create/load a GDT when I am in PMode?

Post by trinopoty »

One difference, in RMode, LGDT takes 24 bit linear address; in PMode, LGDT takes 32 bit linear address. The same is true for SGDT, LIDT/SIDT.
Always give a difficult task to a lazy person. He will find an easy way to do it.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: How do I create/load a GDT when I am in PMode?

Post by Combuster »

trinopoty wrote:One difference, in RMode, LGDT takes 24 bit linear address; in PMode, LGDT takes 32 bit linear address. The same is true for SGDT, LIDT/SIDT.
Utter nonsense. Even if you never heard of operand sizes and prefixes there's no way you're going to explain how you established the above.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
DLBuunk
Member
Member
Posts: 39
Joined: Sun May 18, 2008 9:36 am
Location: The Netherlands

Re: How do I create/load a GDT when I am in PMode?

Post by DLBuunk »

trinopoty wrote:One difference, in RMode, LGDT takes 24 bit linear address; in PMode, LGDT takes 32 bit linear address. The same is true for SGDT, LIDT/SIDT.
What pointer are you talking about?

The GDTP itself, which is size:linear base, thus 48-bit in realmode/pmode, and 80-bit in long mode.

Or the pointer to the GDTP, which is the argument of lgdt. This pointer is an ordinary offset within the current segment (not a linear adress), thus the size depends on the current segment size (unless overridden with a prefix).

Either way, you are wrong.
User avatar
trinopoty
Member
Member
Posts: 87
Joined: Wed Feb 09, 2011 2:21 am
Location: Raipur, India

Re: How do I create/load a GDT when I am in PMode?

Post by trinopoty »

Combuster wrote:
trinopoty wrote:One difference, in RMode, LGDT takes 24 bit linear address; in PMode, LGDT takes 32 bit linear address. The same is true for SGDT, LIDT/SIDT.
Utter nonsense. Even if you never heard of operand sizes and prefixes there's no way you're going to explain how you established the above.
I got the result on my own Core 2 Duo machine.
Even using prefix did not help in the case.
Always give a difficult task to a lazy person. He will find an easy way to do it.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: How do I create/load a GDT when I am in PMode?

Post by Combuster »

trinopoty wrote:I got the result on my own Core 2 Duo machine.
Proof wanted. At least it gives us an opportunity to point out the bugs that must be present in your code (or brain cells)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: How do I create/load a GDT when I am in PMode?

Post by Brendan »

Hi,
Combuster wrote:
trinopoty wrote:I got the result on my own Core 2 Duo machine.
Proof wanted. At least it gives us an opportunity to point out the bugs that must be present in your code (or brain cells)
He's mostly right (although the difference is in operand size, not real mode vs. protected mode). Basically, for LGDT if the operand size is 16-bit the CPU loads a 32-bit base address then masks it so that the upper 8 bits are zero/unused and you end up with a 24-bit GDT base address being loaded, and if the operand size is 32-bit then no masking occurs and a full 32-bit GDT base address is loaded.

Of course in real mode or 16-bit protected mode code you can use an operand size override prefix to get a full 32-bit GDT base address (unless the CPU is an 80286), and in 32-bit protected mode code you can use an operand size override to limit the base address to 24-bit.

Also note that the reason for this "unusual" behaviour is backward compatibility - the 80286 only supported 24-bit addressing, so to make 32-bit CPUs work the same...


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
trinopoty
Member
Member
Posts: 87
Joined: Wed Feb 09, 2011 2:21 am
Location: Raipur, India

Re: How do I create/load a GDT when I am in PMode?

Post by trinopoty »

An operand size difference is possible as I tend to not specify any operand size explicitly.
To be honest, I just write the code and let the assembler/compiler generate whatever they want, given that it works as I expect.
When I first saw the problem, I did not care as I knew the kernel will change the GDT later anyway.
Always give a difficult task to a lazy person. He will find an easy way to do it.
Post Reply