I did it!!! I did it!!! After a LONG LONG TIME I took my little piece of code tonight, and I did it! My GDT table is correctly set!
Just for the record: The debugger works like a charm. it's was MY problem after all.
As a matter of facts, seems that loading the GDT from a function was a bad idea after all (thanks for pointing that out!). I did it in the start.asm file, AND added ALIGN 4 before my GDT entries definition. This is the resulting code:
Code: Select all
[BITS 32]
SECTION .data
; This part MUST be 4byte aligned, so we solve that issue using 'ALIGN 4'
ALIGN 4
mboot:
; Multiboot macros to make a few lines later more readable
MULTIBOOT_PAGE_ALIGN equ 1<<0
MULTIBOOT_MEMORY_INFO equ 1<<1
MULTIBOOT_AOUT_KLUDGE equ 1<<16
MULTIBOOT_HEADER_MAGIC equ 0x1BADB002
MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
MULTIBOOT_CHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
EXTERN code, bss, end
; This is the GRUB Multiboot header. A boot signature
dd MULTIBOOT_HEADER_MAGIC
dd MULTIBOOT_HEADER_FLAGS
dd MULTIBOOT_CHECKSUM
; AOUT kludge - must be physical addresses. Make a note of these:
; The linker script fills in the data for these ones!
dd mboot
dd code
dd bss
dd end
dd start
;;this is our GDT entry
gdt_ptr_struct:
dw gdt_end - gdt - 1 ; size of the GDT
dd gdt ; linear address of GDT
ALIGN 4 ;It doesn't work If I don't set this here
gdt:
;we set five gdt entries.Why 5? well, we have a code and data segment descriptor for the kernel, code and data segment descriptors for user mode, and a null entry. This must be present, or bad things will happen.
dd 0, 0 ; null gate
db 0xFF, 0xFF, 0, 0, 0, 0x9A, 0xCF, 0x00 ; code selector 0x08: base 0x00000000, limit 0xFFFFFFFF, type 0x9A, granularity 0xCF
db 0xFF, 0xFF, 0, 0, 0, 0x92, 0xCF, 0x00 ; data selector 0x10: base 0x00000000, limit 0xFFFFFFFF, type 0x92, granularity 0xCF
db 0xFF, 0xFF, 0, 0, 0, 0x92, 0xCF, 0x00 ; user mode code selector 0x18: base 0x00000000, limit 0xFFFFFFFF, type 0xFA, granularity 0xCF
db 0xFF, 0xFF, 0, 0, 0, 0x92, 0xCF, 0x00 ; user mode data selector 0x20: base 0x00000000, limit 0xFFFFFFFF, type 0xF2, granularity 0xCF
gdt_end:
section .text
global start
extern main
start:
lgdt [gdt_ptr_struct] ; Load the new GDT pointer
mov ax, 0x10 ; 0x10 is the offset in the GDT to our data segment
mov ds, ax ; Load all data segment selectors
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
jmp 0x08:.flush ; 0x08 is the offset to our code segment: Far jump!
.flush:
mov esp, _sys_stack ; This points the stack to our new stack area
xor eax,eax
mov ax,ds
mov es,ax
call main
jmp $
SECTION .bss
resb 8192 ; This reserves 8KBytes of memory here
_sys_stack:
And this is my link file. If all this problem was caused by my link file please tell me. I got it working but I'm not understanding why (And I really want to. I want to learn)
Code: Select all
OUTPUT_ARCH ( "i386" )
OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
.text phys : AT(phys) {
code = .;
*(.text)
*(.rodata)
. = ALIGN(4096);
}
.data : AT(phys + (data - code))
{
data = .;
*(.data)
. = ALIGN(4096);
}
.bss : AT(phys + (bss - code))
{
bss = .;
*(.bss)
. = ALIGN(4096);
}
end = .;
}
Thanks for all the replies, and for being so patient. I got it working thanks to you people
