Page 1 of 1

How to load a GDT in C

Posted: Fri Feb 14, 2003 10:21 pm
by beyondsociety
I have entered pmode and want to reload the gdt. I have created a GDT in a assembly file and want to call it from my C file. How would I do this.

Re:How to load a GDT in C

Posted: Sat Feb 15, 2003 4:46 am
by amirsadig
hi,
there is no function in C which can load the GDT, you may use inline asssemly or your Assemply ;

normaly this code will load the GDT;
suppse that gtd begin at 0xXXXXXX then with assemly :

lgdt 0xXXXXXX ;

will load it

Re:How to load a GDT in C

Posted: Sat Feb 15, 2003 5:00 am
by Therx
Even if you don't know assembly it would still be much easier to have an assembly stub linked to the kernel which loads the GDT and IDT then jumps to C code

This is what I've done and many others have too

Re:How to load a GDT in C

Posted: Sat Feb 15, 2003 9:43 am
by Peter_Vigren
amirsadig wrote: hi,
there is no function in C which can load the GDT, you may use inline asssemly or your Assemply ;

normaly this code will load the GDT;
suppse that gtd begin at 0xXXXXXX then with assemly :

lgdt 0xXXXXXX ;

will load it
Well, what I know it is a bit more trickier than that...

The parameter following the lgdt command is not the beginning of the GDT, it is the place (in real address = 32-bit non-segment-based address) where the GDT register is.

So to load the GDT, use this command:

Code: Select all

Lgdt [GlobalDescriptorTableRegister]
And here is the structure of the GDT register:

Code: Select all

GlobalDescriptorTableRegister
dw???GlobalDescriptorTableEnding - GlobalDescriptorTableBeginning - 1
dd???GlobalDescriptorTableBeginning
The code may need some adjustment since I just took it from my own (pure assembly) code.

Re:How to load a GDT in C

Posted: Sat Feb 15, 2003 10:02 am
by beyondsociety
Ok, so I just going to reload it in my assembly stub that is linked with my kernel.

I have one question though. When the time comes and I want to task-switch, How will I do it? Considering If I wanted to have it done in C.

Re:How to load a GDT in C

Posted: Tue Feb 18, 2003 9:34 am
by richie
Hello!
Normally Multitasking is done with TSS (Task State Segment). This is a Processor-Structure (like the gdt is one). Every Process or Task (nearly the same) has such a structure. And every TSS as although an entry in the GDT with points to the TSS. If you want to make a taskswitch you simply jump to the GDT-Entry. For example if you have three entries Null-Descriptor, kernel-code-descriptor and your TSS-Descriptor in that order you can do the following jump in asm:
[tt]jmp (2*8):0x0000[/tt]
The offset is ignored so you can use every offset. The selector is the "normal" GDT index (starting with zero and multiplicated with 8)