Why qemu always resets ?

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
neutrino
Posts: 3
Joined: Sat May 03, 2014 1:46 am

Why qemu always resets ?

Post by neutrino »

Hi, I read here these wonderful lessons (http://www.osdever.net/tutorials/view/mixing-assembly-c) and trying to write a bootloader. I'm doing everything exactly as written on the lesson, but qemu always resets ... and i do not understand why ? Here my asm code.

Code: Select all

[BITS 16]       ; We need 16-bit intructions for Real mode

[ORG 0x7C00]    ; The BIOS loads the boot sector into memory location 0x7C00

drive db 0  
jmp word load

load:
       jmp reset_drive
       jmp enter_pm
reset_drive:
        mov ah, 0Eh     ; We want to print a single character
        mov al, 'A'     ; That character is 'A'
        mov bh, 0Eh     ; White text on black background, not blinking
        mov bl, 0       ; Page number 0        
        int 10h

        mov ah, 0               ; RESET-command
        int 13h                 ; Call interrupt 13h
        mov [drive], dl
        or ah, ah               ; Check for error code
        jnz reset_drive         ; Try again if ah != 0
        
        mov ax, 0
        mov es, ax
        mov bx, 0x1000          ; Destination address = 0000:1000
        
        mov ah, 0Eh     ; We want to print a single character
        mov al, 'B'     ; That character is 'A'
        mov bh, 0Eh     ; White text on black background, not blinking
        mov bl, 0       ; Page number 0        
        int 10h
    
        mov ah, 02h             ; READ SECTOR-command
        mov al, 02h             ; Number of sectors to read = 1
        mov dl, [drive]
        mov ch, 0               ; Cylinder = 0
        mov cl, 2               ; Sector = 2
        mov dh, 0               ; Head = 0
        int 13h                 ; Call interrupt 13h
        
        or ah, ah               ; Check for error code
        jnz load         ; Try again if ah != 0
       ; jc reset_drive
        
        cli                     ; Disable interrupts, we want to be alone        
     
enter_pm:
        xor ax, ax
        mov ds, ax              ; Set DS-register to 0 - used by lgdt

        lgdt [gdt_desc]         ; Load the GDT descriptor        
    

        mov eax, cr0            ; Copy the contents of CR0 into EAX
        or eax, 1               ; Set bit 0
        mov cr0, eax            ; Copy the contents of EAX into CR0 
  

        jmp 08h:clear_pipe      ; Jump to code segment, offset clear_pipe

[BITS 32]                       ; We now need 32-bit instructions
clear_pipe:
        mov ax, 10h             ; Save data segment identifyer
        mov ds, ax              ; Move a valid data segment into the data segment register
        mov ss, ax              ; Move a valid data segment into the stack segment register
        mov esp, 090000h        ; Move the stack pointer to 090000h

        jmp 08h:01000h          ; Jump to section 08h (code), offset 01000h

gdt:                    ; Address for the GDT

gdt_null:               ; Null Segment
        dd 0
        dd 0

gdt_code:               ; Code segment, read/execute, nonconforming
        dw 0FFFFh
        dw 0
        db 0
        db 10011010b
        db 11001111b
        db 0

gdt_data:               ; Data segment, read/write, expand down
        dw 0FFFFh
        dw 0
        db 0
        db 10010010b
        db 11001111b
        db 0

gdt_end:                ; Used to calculate the size of the GDT

gdt_desc:                       ; The GDT descriptor
        dw gdt_end - gdt - 1    ; Limit (size)
        dd gdt                  ; Address of the GDT

times 510-($-$$) db 0           ; Fill up the file with zeros

        dw 0xAA55                ; Boot sector identifyer
Bochs do not work in my ubuntu and I tried to debug the code using simply "prints", f.e.

Code: Select all

mov ah, 0Eh
mov al, 'B' 
mov bh, 0Eh
mov bl, 0
int 10h
I've done here is a file to compile and run.

Code: Select all

rm -rf *.o *.bin *.img
nasm -f bin bootsect.asm -o bootsect.bin
gcc -ffreestanding -c main.c -o main.o
gcc -c video.c -o video.o
gcc -c ports.c -o ports.o
ld -e main -Ttext 0x1000 -o kernel.o main.o video.o ports.o
ld -i -e main -Ttext 0x1000 -o kernel.o main.o video.o ports.o
objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin
./makeboot a.img bootsect.bin kernel.bin
 
echo " Running OS... ! "
qemu-system-i386 -fda a.img -no-fd-bootchk
When qemu starts with our boot image, the screen displays "AB" and it always blinks, seems the boot image always tries resets. Please help me solve this problem.
Nable
Member
Member
Posts: 453
Joined: Tue Nov 08, 2011 11:35 am

Re: Why qemu always resets ?

Post by Nable »

neutrino wrote:Bochs do not work in my ubuntu
IMHO, this is the first problem to solve: if you want to continue diving into OS development, you should definitely get some well-known debugging tools and skills instead of asking other people to do debug your code. Btw, it's rather simple to build Bochs from source.

Ok, let's move a bit further.
1. The first problem in your code is that you define data (drive db 0) at the point where execution starts, so CPU will execute some garbage instead of your code. One should put variables in the place that won't be executed (unless it's a desired behaviour). For example, you can put this variable definition between 'jump load' and 'load:' lines.
2. It looks like you don't know the difference between 'call' and 'jmp', as 'jmp enter_pm' will never be executed and 'jmp reset_drive' is entirely pointless. Was it an attempt to write series of function calls?
3. Even if your bootloader works in an expected way, then reset can occur somewhere further. Btw, QEmu has some debugger (and even GDB support).
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: Why qemu always resets ?

Post by Combuster »

Because the tutorial from hell never worked.

Don't use it, forget what it says. It's flat out wrong.
"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 ]
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: Why qemu always resets ?

Post by sortie »

Use http://wiki.osdev.org/Bare_Bones instead. It is the recommended tutorial for this website. It doesn't have terribly broken build instructions for compiling C. You may well wish to reconsider writing a bootloader, as it would be an exercise in futility.
neutrino
Posts: 3
Joined: Sat May 03, 2014 1:46 am

Re: Why qemu always resets ?

Post by neutrino »

sortie wrote:Use http://wiki.osdev.org/Bare_Bones instead. It is the recommended tutorial for this website. It doesn't have terribly broken build instructions for compiling C. You may well wish to reconsider writing a bootloader, as it would be an exercise in futility.
Thanks to a very good tutorial, but here we use multiboot, I need to write code to load from real mode to protected mode.
User avatar
Bender
Member
Member
Posts: 449
Joined: Wed Aug 21, 2013 3:53 am
Libera.chat IRC: bender|
Location: Asia, Singapore

Re: Why qemu always resets ?

Post by Bender »

neutrino wrote:
sortie wrote:Use http://wiki.osdev.org/Bare_Bones instead. It is the recommended tutorial for this website. It doesn't have terribly broken build instructions for compiling C. You may well wish to reconsider writing a bootloader, as it would be an exercise in futility.
Thanks to a very good tutorial, but here we use multiboot, I need to write code to load from real mode to protected mode.
You don't. GRUB does it for you.
"In a time of universal deceit - telling the truth is a revolutionary act." -- George Orwell
(R3X Runtime VM)(CHIP8 Interpreter OS)
neutrino
Posts: 3
Joined: Sat May 03, 2014 1:46 am

Re: Why qemu always resets ?

Post by neutrino »

here found very good tutorial which, shows how to switch from real to protected mode without using the grub. someone may need.

http://linuxgazette.net/82/raghu.html
Post Reply