Page 1 of 2

Loadin up the GDT whilst in protected mode.

Posted: Tue Jan 07, 2003 3:58 pm
by Guest
hey,
I use grub too boot my kernel, and it sets up protected mode using a tem GDT. so i was wonderin if u cud give me sum help on sum code and abt whether it will work or not. it is in assembly and can be called from C. my assembly is not good so need help. sum of de code is off frotz os

[BTIS 16] ;shud dis be in a protected mode code

segment .text

[global _setupgdt]


gdtr
   dw gdt_end - 1
   dd gdt
gdt
nullsel    equ $-gdt
gdt0
   dd 0
   dd 0

CodeSel    equ $-gdt
   dw 0FFFFH
   dw 0
   db 0
   db 09AH
   db 0CFH
   db 0H

DataSel    equ $-gdt
   dw 0FFFFH
   dw 0H
   db 0H
   db 092H
   db 0CFH
   db 0

gdt_end

; Begin Setup GDT
_setupgdt:

   push ebp
   mov ebp, esp
   lgdt [gdtr]      ; Load GDT
   pop ebp         ; Restore Caller's stack frame
   ret
; End Setup GDT

Re:Loadin up the GDT whilst in protected mode.

Posted: Tue Jan 07, 2003 4:51 pm
by jrfritz
Looks like it'll work,....

FrotzOS? Don't you mean FritzOS? :)

Re:Loadin up the GDT whilst in protected mode.

Posted: Tue Jan 07, 2003 5:05 pm
by Guest
sorry i typed ur OS name wrong, its meant to be FritzOS
is dat all i need to do to set up the GDt or do i need to do more?

Re:Loadin up the GDT whilst in protected mode.

Posted: Tue Jan 07, 2003 5:11 pm
by jrfritz
Actually...this is how i'd set up my GDT:

In your C code:
extern void LoadGDT();

in your asm code:
[GLOBAL _LoadGDT]
_LoadGDT:
lgdt [ gdtr ]
ret

and in your C code again:
LoadGDT();

Re:Loadin up the GDT whilst in protected mode.

Posted: Tue Jan 07, 2003 5:27 pm
by Guest
hi, i tried the above but it doesnt seem to work properly.
is der a mistake in the code or is der even a better way of achievin it?

Re:Loadin up the GDT whilst in protected mode.

Posted: Tue Jan 07, 2003 5:38 pm
by Guest
i also tried puttin the call to the loadgdt function in the assembly file that calls the main kernel file. whilst doin this i get the following linker msg:

loadgdt.o(.text+0x0): relocation truncated to fit : 16 text

wot does that mean
and wots wrong
can u plz help

Re:Loadin up the GDT whilst in protected mode.

Posted: Tue Jan 07, 2003 5:40 pm
by jrfritz
Strange...worked for me...

If you load your kernel at 1 meg...my GDT does not work for some reason...I don't know why...so I'm making another GDT...that's why you get that linker message...

Re:Loadin up the GDT whilst in protected mode.

Posted: Tue Jan 07, 2003 6:03 pm
by Curufir
Unless by some miracle your code is loaded at 0 linear then this "dw gdt_end - 1" is wrong.

Re:Loadin up the GDT whilst in protected mode.

Posted: Tue Jan 07, 2003 6:08 pm
by jrfritz
The worst thing that can happen to a OS developer: All The Wrong Code Works on my PC! :'(

Re:Loadin up the GDT whilst in protected mode.

Posted: Tue Jan 07, 2003 6:29 pm
by Curufir
Ok, correction because I feel sorry for Tom :).

It's incorrect shall we say, in that it's holding the wrong value. It just so happens that it'll be holding a value that's too big so your gdt will still work, but IMO that doesn't make it any more correct.

Re:Loadin up the GDT whilst in protected mode.

Posted: Tue Jan 07, 2003 6:56 pm
by jrfritz
ok...

Re:Loadin up the GDT whilst in protected mode.

Posted: Tue Jan 07, 2003 8:02 pm
by Perica
..

Re:Loadin up the GDT whilst in protected mode.

Posted: Wed Jan 08, 2003 2:50 am
by Pype.Clicker
about GDT change :

thou shalt do a selektor reload afther thou haft changed thee GDT
Remember the shadow registers ... a hidden (not visible to software) register caches the base, limit and descriptor of the segment for every segment register once the segment register has been loaded from the GDT.

So after you changed the GDT you should do something like

Code: Select all

   mov ax,ds
   mov ds,ax
to force the CPU to read back the GDT and fill the shadow register with the new values ...

Same thing applies for the code segment: you should do a

Code: Select all

   jmp CODE_SELECTOR:.here
.here:
just as you did in your pmode setup code ... This will refresh the shadow registers for your code segment.

Remember to do the same with stack or extra segment if needed.

Re:Loadin up the GDT whilst in protected mode.

Posted: Wed Jan 08, 2003 9:14 am
by Curufir
Perica Senjak wrote: Hey,

Curufir: Could you please tell me what's wrong with "gdt_end -1"? I have something simmilar to this in my GDT aswell, I want to know what's wrong so i can fix the problem?

"gdt_end -1" is not wrong (At least that's what i think); It Stores the Memory address of gdt_end minus 1, this takes it one spot back (Because gdt_end it not part of the GDT); Therefore is points to the End of the GDT - What's Wrong with it?

I saw somebody put "dw gdt_end - gdt_start - 1", This can't be Correct? - Is it Correct?

Could somebody please explain this.....

Cya.
FFS Perica is it impossible for you to read the damn manual before you post?

The GDT register has 2 components.

GDT size in bytes (word)
GDT base address (dword)

Now it should be freakin' obvious that gdt_end-1 is not the size of the GDT unless the gdt starts at 0, it should also be blindingly obvious how to actually get the correct size. The reason it works is that if the GDT size is too big then your selectors will still be valid, whereas if it was too small the processor would fault. Having it too big removes one of the basic protection mechanisms preventing loading of nonexistent selectors. Therefore it is wrong to have it too big.

Re:Loadin up the GDT whilst in protected mode.

Posted: Wed Jan 08, 2003 9:53 am
by jrfritz
Now I really need to read about the GDT more...