Loadin up the GDT whilst in protected mode.

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.
Guest

Loadin up the GDT whilst in protected mode.

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

Re:Loadin up the GDT whilst in protected mode.

Post by jrfritz »

Looks like it'll work,....

FrotzOS? Don't you mean FritzOS? :)
Guest

Re:Loadin up the GDT whilst in protected mode.

Post 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?
jrfritz

Re:Loadin up the GDT whilst in protected mode.

Post 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();
Guest

Re:Loadin up the GDT whilst in protected mode.

Post 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?
Guest

Re:Loadin up the GDT whilst in protected mode.

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

Re:Loadin up the GDT whilst in protected mode.

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

Re:Loadin up the GDT whilst in protected mode.

Post by Curufir »

Unless by some miracle your code is loaded at 0 linear then this "dw gdt_end - 1" is wrong.
jrfritz

Re:Loadin up the GDT whilst in protected mode.

Post by jrfritz »

The worst thing that can happen to a OS developer: All The Wrong Code Works on my PC! :'(
Curufir

Re:Loadin up the GDT whilst in protected mode.

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

Re:Loadin up the GDT whilst in protected mode.

Post by jrfritz »

ok...
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Re:Loadin up the GDT whilst in protected mode.

Post by Perica »

..
Last edited by Perica on Sun Dec 03, 2006 8:24 pm, edited 1 time in total.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Loadin up the GDT whilst in protected mode.

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

Re:Loadin up the GDT whilst in protected mode.

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

Re:Loadin up the GDT whilst in protected mode.

Post by jrfritz »

Now I really need to read about the GDT more...
Post Reply