Page 2 of 2

Re: Bootloader jumps to kernel, but its code isn't executed.

Posted: Fri Mar 28, 2014 10:53 am
by Roman
Bender wrote:Why are you using int 0x21? It's a DOS interrupt, not a BIOS interrupt.
I've understood that, now I use another interrupt.

How can I push AL or AH to stack? Is there a way to do that?

Re: Bootloader jumps to kernel, but its code isn't executed.

Posted: Fri Mar 28, 2014 11:11 am
by Bender
You can't. The stack is something that's arranged as a WORD (16-bit) or a DWORD (32-bit) structure in RAM.
AL is nothing except the first 8 bits of AX, simply push AX and you should be able to save AL.

Re: Bootloader jumps to kernel, but its code isn't executed.

Posted: Sat Mar 29, 2014 6:06 am
by Roman
I decided to rewrite my OS and to use 2 stage bootloader and protected mode.

But where should I enter protected mode? I tried this in stage 2:

Code: Select all

    cli
    lgdt [gdtr]
    mov eax, cr0
    or al, 1
    mov cr0, eax

    jmp 08h:main
But I get: boot/stage2.S:41: error: undefined symbol `gdtr'.

My assembler is YASM 1.2.0.
Stage 2 is 16 bit.

Code: Select all

section .text
	use16
	org 0x1000  ; Load address.

Re: Bootloader jumps to kernel, but its code isn't executed.

Posted: Sat Mar 29, 2014 6:14 am
by Bender
You must define the "GDTR" yourself, You would need to refer to the manuals and some Google Fu will be required.
Also, you may triple fault since the "LGDT" instruction expects the linear address and you're only providing the offset.

Re: Bootloader jumps to kernel, but its code isn't executed.

Posted: Sat Mar 29, 2014 7:45 am
by Roman
Bender wrote:You must define the "GDTR" yourself, You would need to refer to the manuals and some Google Fu will be required.
Also, you may triple fault since the "LGDT" instruction expects the linear address and you're only providing the offset.
What's wrong with that? (causes reboots, triple fault?)

Code: Select all

section .text
	use16
	org 0x1000  ; Load address.

start:
    mov ax, cs
	mov ds, ax ; Select data segment.

    mov ah, 13h ; Tell user, that stage 2 has started.
    mov al, 1
    mov bh, 0
    mov bl, 0_7h
    mov cx, stage2_start_msg_len
    inc dh
    mov dl, 0
    mov bp, stage2_start_msg
    int 0x10

    call check_a20
    cmp ax, 1
    jnz en_a20

    call check_a20
    cmp ax, 1
    jnz error

    mov ah, 42h ; Load kernel.
    mov dl, 0x80
    mov si, DAPACK
    int 0x13
    jc error

    mov ah, 0
    mov al, 0x13
    int 0x10

    cli
    lgdt [gdtr]
    mov eax, cr0
    or al, 1
    mov cr0, eax

    mov edi,0x0A0000
    mov al, 4      ; the color of the pixel
    mov [edi],al

    mov edi,0x0A0025
    mov al, 7      ; the color of the pixel
    mov [edi],al

    jmp dword 0x2000:0000

    hlt


error:
    call cls

    mov ah, 0bh
    mov bh, 0
    mov bl, 4_4h
    int 0x10

    mov ah, 13h
    mov al, 1
    mov bh, 0
    mov bl, 8_Fh
    mov cx, err_msg_len
    mov dh, 0
    mov dl, 0
    mov bp, err_msg
    int 0x10

    cli
    hlt


cls:
    mov ah, 0
    mov al, 2

    int 0x10

    ret


en_a20:
    mov ax, 0x2401
    int 0x15
    jc error

    ret


check_a20:
    pushf
    push ds
    push es
    push di
    push si
 
    cli
 
    xor ax, ax ; ax = 0
    mov es, ax
 
    not ax ; ax = 0xFFFF
    mov ds, ax
 
    mov di, 0x0500
    mov si, 0x0510
 
    mov al, byte [es:di]
    push ax
 
    mov al, byte [ds:si]
    push ax
 
    mov byte [es:di], 0x00
    mov byte [ds:si], 0xFF
 
    cmp byte [es:di], 0xFF
 
    pop ax
    mov byte [ds:si], al
 
    pop ax
    mov byte [es:di], al
 
    mov ax, 0
    je check_a20__exit
 
    mov ax, 1
 
check_a20__exit:
    pop si
    pop di
    pop es
    pop ds
    popf
 
    ret


DAPACK:
    db	0x10
	db	0
blkcnt: dw 1
db_add: dw 0x2000
	dw 0

d_lba: dd 2
	dd 0


stage2_start_msg db "Stage 2 has started."

stage2_start_msg_len equ $-stage2_start_msg


err_msg db "Critical boot error occured, cannot continue."

err_msg_len equ $-err_msg

gdtr DW 200 ; For limit storage
     DD 0

Re: Bootloader jumps to kernel, but its code isn't executed.

Posted: Sat Mar 29, 2014 8:07 am
by Bender

Code: Select all

gdtr DW 200 ; For limit storage
     DD 0
Image
You don't even know what GDT is.
Grab a copy of the original 80386 Manual (It's a text file :P) and look for the GDT. Some Google Fu will also help. :)
Making it easier for you, Look onto section 5.1.2 "Descriptor Tables"