Page 1 of 1

GDT at runtime

Posted: Tue Mar 01, 2011 2:44 am
by stanko51
hello,

no paging enabled.
When loading a task I modify the GDT at run time to add the descriptor for this task and create a sergment of the needed size and then delete it when the task finishes. What do you think of this implementation ? Is there some special precaution to take i would have forgot ?

So far it is not working because the task crashes when it tries to run.

Thank you for your advice.

Re: GDT at runtime

Posted: Tue Mar 01, 2011 2:08 pm
by Tosi
I think he wants to have memory protection, but without paging.
As for the problem, we will need more than "it doesn't work" to understand what the problem is. Mostly likely, you are not filling in the structure for a GDT entry correctly.

Re: GDT at runtime

Posted: Tue Mar 01, 2011 2:44 pm
by a5498828
never modify gdt at runtime. never.
you set it up in real mode, enter p mode and it satys this way for the rest of os life. if you wana enter long mode, you setup entire gdt in real mode anyway.

Re: GDT at runtime

Posted: Tue Mar 01, 2011 2:52 pm
by Tosi
There is nothing wrong with modifying the GDT at runtime, as long as it is done correctly. But if he wants to use segmentation for a memory protection scheme, I would recommend he use LDTs too.

Re: GDT at runtime

Posted: Wed Mar 02, 2011 6:56 am
by AJ
Hi,
stanko51 wrote:Is there some special precaution to take i would have forgot ?
Have you remembered to reload GDTR with the new GDT limit? Otherwise, as suggested above, look at the bits in your GDT entry and ensure they're all as you would expect.

This is perfectly valid and is how segmentation-based memory protection works. This is now considered legacy and if you ever want to dabble in long mode, you will need paging. Most resources you will find also relate to paging-based memory protection (with flat segmentation).

Cheers,
Adam

Re: GDT at runtime

Posted: Fri Mar 11, 2011 2:54 pm
by Karatorian
a5498828 wrote:never modify gdt at runtime. never.
There are legitimate reasons for modifying the GDT at run time. One of them being using segmentation for memory protection (which is what the OP is trying to do). Another is booting from GRUB or any other multiboot compliant boot loader. (The processor is in protected mode, but there's no guarantee that the GDT is valid. Even reloading the segment registers with the same values they had before may not work.) Additionally, using segmentation is one of the methods of emulating NX on processors that don't support it.

Re: GDT at runtime

Posted: Fri Mar 11, 2011 3:14 pm
by Gigasoft
Windows also changes two GDT entries on each thread switch. One of them is updated to point to the current Thread Environment Block, and another is pointed at the LDT for the current process.