problem in reloading GDT

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
shaz

problem in reloading GDT

Post by shaz »

my boot loader copies kernel linked at 0xc000_0000 at 0x100000.then it switches to protected mode.
the selectors in boot loader have base zero and limit 4 gb.then i jump to kernel.
in kernel i reload the selectors with base 0x4010_0000.but as soon as execute the jump instruction to load CS:IP with new values ,computer restarts.
please tell me the problem and how to remove it.

Code: Select all

[bits 32]
[SECTION .text]
start:?????????
        lgdt[GDTR]   ;reload the GDT
        jmp CODESEL:jump       
        jump:        
        jmp $

;==========DATA (GDT) AREA==========
GDTR:????????????
GDTsize DW GDT_END-GDT-1???
GDTbase DD GDT?????????
GDT:
NULL_SEL         EQU $-GDT
      DD 0x0?????????
      DD 0x0
CODESEL          EQU $-GDT  
      DW     0xFFFF           
      DW     0x0000             
      DB     0x10             
      DB     0x9A            
      DB     0xCF            
      DB     0x40              
GDT_END:
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:problem in reloading GDT

Post by Pype.Clicker »

i'd be tempted to say that the offset of LGDT instruction is bound to 0xC000_0000, as any other part of your kernel. So when you try to load it, you actually load garbage from non-existent physical memory ...

from there, you can easily imagine that the next time you'll try to read out a descriptor (like when decoding the long jump instruction), chances are that you'll raise a GPF, which will quickly turn in a triple fault ... try to run your stuff in the Bochs and you'll see what i'm talking about ...

why not simply set a 0x4010_0000 base in the bootloader itself ?

otherwise,

Code: Select all

start:
lgdt [GDTR-0xC000_0000+0x0010_0000] ;; currently running at 1MB
jmp CODESEL:.gdtr_reloaded  ;; the offset is within the new segment, so everything should be fine
.gdtr_reloaded:
    jmp .gdtr_reloaded

GDTR:
.limit dw GDT_END-GDT+1
.base dd GDT - 0xC000_0000 + 0x0010_0000
;; the base address is a segment-base, thus independent of the
;; current code/data/whatever segment base address, remember ?

GDT:
.null: dd 0,0
.code: dd ...
.data: dd ...
shaz

Re:problem in reloading GDT

Post by shaz »

now i decided to load the selectors with desired base address in boot loader.so instead of jumping to some label
after switching to protected mode ,i jumped to 0x100000,the address where kernel has been loaded.
but the computer restarted.
i did this to check if such kind of jump works so that in future i can load selectors with base
0x40100000 using this technique.
please tell me why its not working and how can i load selectors in boot loader having base other than 0x00000000

Code: Select all

[SECTION .text]
[org 0x7c00]
start:         
.
.
.
.
cli         
mov eax,cr0      
or al,1      
mov cr0,eax
[bits 32]
lgdt[GDTR]   
jmp CODESEL:0x100000       
jmp $

;==========DATA (GDT) AREA==========
GDTR:            
GDTsize DW GDT_END-GDT-1   
GDTbase DD GDT         
GDT:
NULL_SEL         EQU $-GDT
      DD 0x0         
      DD 0x0
CODESEL          EQU $-GDT  
      DW     0xFFFF           
      DW     0x0000             
      DB     0x00             
      DB     0x9A            
      DB     0xCF            
      DB     0x00              
GDT_END:
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:problem in reloading GDT

Post by Neo »

there is an other way to use the kernel linked at 3GB and loaded at 1MB. This is by enabling paging as soon as you load your kenel(provided your kernel is loaded properly).This is what i've done anyway. I enabled paging first along with my memory manager and only then try anyhting else.
HTH
Only Human
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:problem in reloading GDT

Post by Pype.Clicker »

one thing sounds weird: there's no data selector in your GDT. Did you omit it in the post or do you really have no data selector ?...

The LGDT instruction should be issued while you're still in realmode too ...
Post Reply