Reflect GDT changes

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
XCHG
Member
Member
Posts: 416
Joined: Sat Nov 25, 2006 3:55 am
Location: Wisconsin
Contact:

Reflect GDT changes

Post by XCHG »

I have coded two functions with below prototypes:

Code: Select all

DWORD __AddGDTDescriptor (void* GDTR, DWORD LowOrderDWORD, DWORD HighOrderDWORD); StdCall;
Boolean __RemoveGDTDescriptor (void* GDTR, DWORD SegmentSelector); StdCall;
The former adds a Segment Descriptor to the GDT and the latter removes one. Let's say I have added a Segment Descriptor to the GDT and I have put its segment selector in the GS. After I remove it from the GDT, I still can use GS. Does anybody have any idea why? When I remove a descriptor from the GDT, I simply put null in all 8 bytes of the descriptor. I can put the code in here if it helps you help me.

Thanks in advance.
On the field with sword and shield amidst the din of dying of men's wails. War is waged and the battle will rage until only the righteous prevails.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Try removing CS or DS. I bet anything that it won't work then.
User avatar
XCHG
Member
Member
Posts: 416
Joined: Sat Nov 25, 2006 3:55 am
Location: Wisconsin
Contact:

Post by XCHG »

Oh I just figured something out. As long as the Segment Selector is in either DS, ES, FS or GS and you have removed the segment descriptor from the GDT, you will still be able to use it but as soon as you put the previous value of either one of those segment selectors into themselves, as in the means of reloading them with their own values, you will get a general protection fault. This is weird isn't it?

pcmattman,

I tried removing the Data Segment Descriptor from the GDT and I could still use DS. I then did the below after having removed the DS from GDT:

Code: Select all

  MOV     EAX , DS
  MOV     DS , EAX
And DS became invalid then. I guess we have to reload the segment selectors in order to validate and/or invalidate them.
On the field with sword and shield amidst the din of dying of men's wails. War is waged and the battle will rage until only the righteous prevails.
Ztane
Posts: 9
Joined: Sun Jan 14, 2007 12:43 pm
Location: Oulu, Finland

Post by Ztane »

Quite opposite.. it is well-documented behaviour.

80386 programmer's reference manual, section 5.1.4:

"The 80386 stores information from descriptors in segment registers, thereby avoiding the need to consult a descriptor table every time it accesses memory." (http://pdos.csail.mit.edu/6.828/2005/re ... s05_01.htm)
Post Reply