How to load a GDT in C

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
beyondsociety

How to load a GDT in C

Post 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.
amirsadig

Re:How to load a GDT in C

Post 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
Therx

Re:How to load a GDT in C

Post 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
Peter_Vigren

Re:How to load a GDT in C

Post 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.
beyondsociety

Re:How to load a GDT in C

Post 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.
richie

Re:How to load a GDT in C

Post 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)
Post Reply