more GDT trouble.....

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
TexHawk

more GDT trouble.....

Post by TexHawk »

First off I want to thank everyone who has been helping me with this.  But Unfortunately I still haven't been able to use the GDT correctly.  However since I'm new to this it could very well be that I have done something else wrong so I included the code for my entire bootsector.  So if anyone has nothing better to do than debug someone's code over the internet that they have never met :) Please feel free to look over this and let me know if there is anything horribly wrong.

FYI currently a small kernel that occupies the second sector of the boot disk is loaded into memory.  I have taken out the instruction that jumps to it because....well...I can't use my pmode descriptors to do the jump.  In regards to everyone's suggestions I have tried all of them and whatever the method in the code right now is just the remnants of the last attempt to load a valid address for the GDT.  Thank you to all and let the fun begin :)



;; pmboot.asm
;; skeleton bootloader



%define KERN_LOAD 0x500
%define STACK 0xFF00 ; Beginning of the Stack
%define STACK_P 0xFFFE  ; Where the stack is (ended)
%define BASE_SEG 0x9000
%define MEM_START 0x0


org 07c00h ; start address 0000:07c00h physical

nop


skipdata:
bits 16

mov ax, 0x0600 ; BIOS-function clear/scroll window
mov cx, 0x0 ; define window to be cleared from
mov dx, 0x174F ; to 23, 79
mov bh, 0 ; fillcolor = 0
int 10h ; BIOS -clear screen- go!
mov ax, 0xB800
mov gs, ax ; point gs to video memory
mov word [gs:0],0x641 ; display brown 'A'
mov word [gs:2],0x642    ; display brown 'B'

mov ax, BASE_SEG ; set up stack
mov ss, ax ; Stack segment
mov sp, STACK_P ; and of course the stack pointer-end of stack

mov bx, KERN_LOAD ; This is where I specify where to place the
mov ax, MEM_START ; kernel I'd like it to go to 0x600 physical
mov es, ax ; but I think its obvious that I'm having some
; real mode addressing issues

read: mov ah, 2 ; function name (2)
mov al, 1 ; number of sectors to read
mov ch, 0 ; track number
mov cl, 2 ; sector number = 2 (this is the kernel)
mov dh, 0 ; head number
mov dl, 0 ; drive number 0 = floppy (?)

int 13h ; read the kernel into memory

push cs
pop ds ; load ds with cs

xor eax,eax ; This is the method that Adek and Dr. 128
mov ax,cs ; suggested but I've tried all of the others
shr eax,4 ; that people hav posted and unfortunately
add eax,gdt ; have not had that much luck with any of them
mov dword[gdtr+2],eax ; But thanks again for the help from everyone!

lgdt [ds:gdtr] ; load gdt reg


mov word [gs:4],0x643    ; display brown 'C'

;; Move into **P_MODE**
mov eax, cr0 ; read control reg
or al, 1 ; set PM bit
mov cr0, eax ; transfer it to control port

mov word [gs:6],0x644   ; display brown 'D'
; This is the last character that prints


jmp CODE_SEL:pmode ; into pmode (32-bit mode)
; I think this is where the trouble is
pmode:
[BITS 32]

;; set the segments up
mov word [gs:8],0x645    ; display brown 'E'
mov ax, DATA_SEL ; set data selector
mov ds, ax ; for the data segment
mov es, ax ; extra segment
mov fs, ax
mov gs, ax
mov ss, ax ; stack segment
mov ax, VIDEO_SEL
mov gs, ax ; point gs to video memory
mov word [gs:10],0x741   ; display white 'a'

spin:
jmp spin ; loop


[bits 16]

gdtr
   dw gdt_end-gdt-1 ; length of GDT
   dd gdt ; linear physical address of the GDT

gdt:

       ;null descriptor         ; 0h = 000b
; null descriptor
   dw 0
   dw 0
   db 0
   db 0
   db 0
   db 0
VIDEO_SEL equ $-gdt ; 18h = 11000b  
   dw 0ffffh ; limit 80*25*2-1
   dw 0 ; base 0xB8000
   db 0
   db 092h
   db 0cfh ; present, ring 0, data, expand up, writable
   db 0 ; byte-granular, 16-bit
  
code_gdt:
CODE_SEL equ $-gdt ; 8h = 1000b
; code descriptor 4 GB flat segment starting 0000:0000h
   dw 0ffffh ; limit 4 GB
   dw 0 ; Base 0000:0000h
   db 0
   db 09ah
   db 0cfh
   db 0h
data_gdt:
DATA_SEL equ $-gdt ; =10h  = 10000b
; data descriptor 4 GB flat segment starting 0000:0000h
   dw 0ffffh ; limit 4 GB
   dw 0h ; Base 0000:0000h
   db 0h
   db 092h
   db 0cfh
   db 0h

gdt_end:

;; equates

lf equ 10
cr equ 13

times 510-($-$$) db 0
                 dw 0xaa55
Adek336

RE:more GDT trouble.....

Post by Adek336 »

your code is very, very buggy. I even found a bug in the code I wrote.

Below is some code to enter pmode under raw dos. it´s got some fixup code, which might be difficult to understand. at my place it works anyways, ask questions if you don´t understand something.

Cheers,
Adrian

[org 0x100]
[bits 16]

start:
    
    push cs
    pop ds
    
    xor eax, eax
    mov ax, cs
    shl eax, 4
    
    add [GDTR +2], eax ; fixup GDTR

    mov [GDT2 + 2], ax ; fixup GDT entries
    mov [GDT3 + 2], ax
    
    shr eax, 16
    
    mov [GDT2 + 4], al
    mov [GDT3 + 4], al
    
    mov [GDT2 + 7], ah
    mov [GDT3 + 7], ah ; end fixup GDT entries

    lgdt [GDTR]
    
    cli ; this is necessary
    
    mov eax, cr0
    or al, 1
    mov cr0, eax
    
    db 0xea
    dw new_age ; far jump
    dw SEL_CODE

[bits 32]
new_age:
    hlt ; same as jmp $
[bits 16]

GDTR:
    dw GDT_END - GDTR - 1
    dd GDT0

GDT0:
    dd 0
    dd 0

SEL_LINEAR equ $ - GDT0
GDT1: ; linear
    dw 0xffff
    dw 0
    db 0
    db 0x92
    db 0xcf
    db 0

SEL_CODE equ $ - GDT0
GDT2: ;code32
    dw 0xffff
    dw 0
    db 0
    db 0x9a
    db 0xcf
    db 0

SEL_DATA equ $ - GDT0
GDT3:
    dw 0xffff
    dw 0
    db 0
    db 0x92
    db 0xcf
    db 0
GDT_END:
TexHawk

RE:more GDT trouble.....

Post by TexHawk »

Well to be honest with you I didn't understand much of your code but I tried to run it and I'm still getting these stupid exceptions whenever I try to do a jump relative to the selectors in the GDT.  I narrowed down the exception to the far jump to the label new_age.  It still triple faults my testbed computer but I've listed the following from my bochsout.txt file.  Hey, thanks once again for your help and patience.  


Here is the part that follows the benign initialization of the simulator...

00000004256i[BIOS ]  rombios.c,v 1.85.2.1 2003/01/16 21:58:42 cbothamy Exp $
00000318073i[KBD  ] reset-disable command received
00000476377i[HD   ] enabling LBA mode
00000481983i[BIOS ] ata0-0: PCHS=20/16/63 translation=none LCHS=20/16/63
00000499431e[HD   ] device set to 1 which does not exist
00000509286e[CPU  ] jump_protected: gate type 0 unsupported
00000509286p[CPU  ] >>PANIC<< exception(): 3rd (13) exception with no resolution
00000509286i[SYS  ] Last time is 1060220060
00000509286i[XGUI ] Exit.
00000509286i[CPU  ] protected mode
00000509286i[CPU  ] CS.d_b = 16 bit
00000509286i[CPU  ] SS.d_b = 16 bit
00000509286i[CPU  ] | EAX=60000011  EBX=00000000  ECX=00140001  EDX=00000000
00000509286i[CPU  ] | ESP=0000fffe  EBP=00000000  ESI=00000000  EDI=0000ffe4
00000509286i[CPU  ] | IOPL=0 NV UP DI PL NZ NA PE NC
00000509286i[CPU  ] | SEG selector     base    limit G D
00000509286i[CPU  ] | SEG sltr(index|ti|rpl)     base    limit G D
00000509286i[CPU  ] |  DS:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00000509286i[CPU  ] |  ES:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00000509286i[CPU  ] |  FS:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00000509286i[CPU  ] |  GS:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00000509286i[CPU  ] |  SS:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00000509286i[CPU  ] |  CS:0000( 0000| 0|  0) 00000000 0000ffff 0 0
00000509286i[CPU  ] | EIP=00007c36 (00007c36)
00000509286i[CPU  ] | CR0=0x60000011 CR1=0x00000000 CR2=0x00000000
00000509286i[CPU  ] | CR3=0x00000000 CR4=0x00000000
00000509286i[     ] restoring default signal behavior
00000509286i[CTRL ] quit_sim called with exit code 1


Thanks again Adrian

Best Regards,
Matt
Xenos

RE:more GDT trouble.....

Post by Xenos »

Perhaps it works if you disable interrupts before switching to pmode. You haven't set up an IDT, so the first interrupt will cause a triple fault.

Another way of debugging: Insert 'HLT' where you believe the system crashes. Then it may halt or reboot, depending on the location of the triple fault.
Adek336

RE:more GDT trouble.....

Post by Adek336 »

my code is a dos program. if you use it as a bootstrap, it will in fact not work. try to run it under raw dos.

cheers,
Adrian
Post Reply