[SOLVED] The GDT is not installed correctly

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
alexbnc
Posts: 14
Joined: Sun Jun 17, 2012 4:37 pm

[SOLVED] The GDT is not installed correctly

Post by alexbnc »

I'm still with the bootloader, trying to go in pmode, and have this little problem. I use VirtualBox to test my work, and a VHD disk, where I manually paste the first two sectors of code. The bootloader works correctly and it loads the second sector, that must install the GDT, open gate A20 and enable protected mode. I'm stuck with the GDT installation.
This is the code of my second bootloader:

Code: Select all

bits    16
org     0x500

jmp     Start

%include        "gdt.inc"

Start:
            cli
	    xor	    ax,     ax
	    mov	    ds,     ax
	    mov	    es,     ax
	    mov	    ax,     0x9000
	    mov	    ss,     ax
	    mov	    sp,     0xffff
	    sti
	    
	    call    InstallGDT

;; Here is some code to open A20
;; Here is some code to enable pmode

cli
hlt

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This is gdt.inc, a separate file

%ifndef START_GDT_INCLUDED
%define START_GDT_INCLUDED

bits    16

InstallGDT:
        cli
        pusha
        lgdt    [GDT]
        sti
        popa
        ret
StartGDT: 
        dd      0
        dd      0 
        
        dw      0ffffh           ; limit low
        dw      0                ; base low
        db      0                ; base middle
        db      10011010b        ; access
        db      11001111b        ; granularity
        db      0                ; base high
        
        dw      0ffffh           ; limit low
        dw      0                ; base low
        db      0                ; base middle
        db      10010010b        ; access
        db      11001111b        ; granularity
        db      0                ; base high
        
EndGDT:

GDT: 
	dw EndGDT - StartGDT - 1 	; limit (Size of GDT)
	dd StartGDT 			; base of GDT
%endif

How do I know there's a problem? I open the log file after I test it on VirtualBox and I have a look over the registers contents. I see this:

Code: Select all

00:00:07.437803 ****************** Guest state at power off ******************
00:00:07.437811 Guest CPUM (VCPU 0) state: 
00:00:07.437821 eax=00000011 ebx=00000000 ecx=00010002 edx=00000000 esi=0000f4a0 edi=0000fff0
00:00:07.437829 eip=0000004a esp=0000fffe ebp=00000000 iopl=0         nv up di pl nz na po nc
00:00:07.437836 cs={0500 base=0000000000005000 limit=0000ffff flags=0000009b} dr0=00000000 dr1=00000000
00:00:07.437847 ds={0000 base=0000000000000000 limit=0000ffff flags=00000093} dr2=00000000 dr3=00000000
00:00:07.437852 es={0500 base=0000000000005000 limit=0000ffff flags=00000093} dr4=00000000 dr5=00000000
00:00:07.437858 fs={0000 base=0000000000000000 limit=0000ffff flags=00000093} dr6=ffff0ff0 dr7=00000400
00:00:07.437864 gs={0000 base=0000000000000000 limit=0000ffff flags=00000093} cr0=00000011 cr2=00000000
00:00:07.437870 ss={0000 base=0000000000000000 limit=0000ffff flags=00000093} cr3=00000000 cr4=00000000
00:00:07.437875 gdtr=0000000000000000:0000  idtr=0000000000000000:ffff  eflags=00000046
00:00:07.437880 ldtr={0000 base=00000000 limit=0000ffff flags=00000082}
00:00:07.437884 tr  ={0000 base=00000000 limit=0000ffff flags=0000008b}
00:00:07.437888 SysEnter={cs=0000 eip=00000000 esp=00000000}
00:00:07.437892 FCW=037f FSW=0000 FTW=0000 FOP=0000 MXCSR=00001f80 MXCSR_MASK=0000ffff
00:00:07.437897 FPUIP=00000000 CS=0000 Rsrvd1=0000  FPUDP=00000000 DS=0000 Rsvrd2=0000
00:00:07.437902 ST(0)=FPR0={0000'00000000'00000000} t0 +0.0000000000000000000000 ^ 0
00:00:07.437910 ST(1)=FPR1={0000'00000000'00000000} t0 +0.0000000000000000000000 ^ 0
00:00:07.437917 ST(2)=FPR2={0000'00000000'00000000} t0 +0.0000000000000000000000 ^ 0
00:00:07.437923 ST(3)=FPR3={0000'00000000'00000000} t0 +0.0000000000000000000000 ^ 0
00:00:07.437929 ST(4)=FPR4={0000'00000000'00000000} t0 +0.0000000000000000000000 ^ 0
00:00:07.437935 ST(5)=FPR5={0000'00000000'00000000} t0 +0.0000000000000000000000 ^ 0
00:00:07.437941 ST(6)=FPR6={0000'00000000'00000000} t0 +0.0000000000000000000000 ^ 0
00:00:07.437947 ST(7)=FPR7={0000'00000000'00000000} t0 +0.0000000000000000000000 ^ 0
00:00:07.437954 XMM0 =00000000'00000000'00000000'00000000  XMM1 =00000000'00000000'00000000'00000000
00:00:07.437962 XMM2 =00000000'00000000'00000000'00000000  XMM3 =00000000'00000000'00000000'00000000
00:00:07.437972 XMM4 =00000000'00000000'00000000'00000000  XMM5 =00000000'00000000'00000000'00000000
00:00:07.437979 XMM6 =00000000'00000000'00000000'00000000  XMM7 =00000000'00000000'00000000'00000000
00:00:07.437987 XMM8 =00000000'00000000'00000000'00000000  XMM9 =00000000'00000000'00000000'00000000
00:00:07.437995 XMM10=00000000'00000000'00000000'00000000  XMM11=00000000'00000000'00000000'00000000
00:00:07.438003 XMM12=00000000'00000000'00000000'00000000  XMM13=00000000'00000000'00000000'00000000
00:00:07.438010 XMM14=00000000'00000000'00000000'00000000  XMM15=00000000'00000000'00000000'00000000
00:00:07.438019 EFER         =0000000000000000
00:00:07.438021 PAT          =0007040600070406
00:00:07.438027 STAR         =0000000000000000
00:00:07.438029 CSTAR        =0000000000000000
00:00:07.438031 LSTAR        =0000000000000000
00:00:07.438033 SFMASK       =0000000000000000
00:00:07.438035 KERNELGSBASE =0000000000000000
00:00:07.438038 ***
00:00:07.438045 Guest paging mode:  Protected (changed 4 times), A20 enabled (changed 2 times)
As you can see, gdtr=0000000000000000:0000, so there's some problem and I can't find out the reason. Maybe somebody with a faster brain ( :D ) can detect the problem.

Thanks!
Last edited by alexbnc on Thu Jan 17, 2013 11:45 am, edited 1 time in total.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: The GDT is not installed correctly

Post by qw »

Classic mistake. If ORG is 0500, you should not load at 0500:0000 but at 0000:0500. Easiest solution is to clear the segment registers.
alexbnc
Posts: 14
Joined: Sun Jun 17, 2012 4:37 pm

Re: The GDT is not installed correctly

Post by alexbnc »

Thanks, Hobbes. I have always been bad at memory handling. Suppose I have to read those huge books about memory some day.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: [SOLVED] The GDT is not installed correctly

Post by Combuster »

alexbnc wrote: mov ax, 0x9000
mov ss, ax
mov sp, 0xffff
You might also want to move your stack out of reserved memory before that gives you random problems later on
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply