-1

I am trying to write a bootloader by myself, but qemu-system-i386 CPU will reset probabilistically. But bochs does not have this problem. Why?

my code is mbr.s and loader.s:

# cat mbr.s
%include "boot.inc"

SECTION MBR vstart=0x7c00         
   ...
   call rd_disk_m_16 ; rd_disk_m_16 is ok
  
   jmp LOADER_BASE_ADDR
   ...
   db 0x55,0xaa
# cat loader.s
   %include "boot.inc"
   section loader vstart=LOADER_BASE_ADDR
   LOADER_STACK_TOP equ LOADER_BASE_ADDR
   jmp loader_start
   ...
loader_start:

   cli
   lgdt [gdt_ptr]

   mov eax, cr0
   or eax, 0x00000001
   mov cr0, eax

   jmp  0x08:p_mode_start

[bits 32]
p_mode_start:
   jmp $

My step:

# bximage -func=create -hd=16M -imgmode="flat" -sectsize=512 -q hd.img
# nasm -I include/ -o mbr.bin mbr.s && dd if=mbr.bin of=./hd.img bs=512 count=1  conv=notrunc
# nasm -I include/ -o loader.bin loader.s && dd if=loader.bin of=./hd.img bs=512 count=4 seek=2 conv=notrunc
# qemu-system-i386 -hda hd.img -d cpu_reset,int -no-reboot
11
  • When you attach GDB to the QEMU guest as a gdb remote, what do you see as you single-step? Also, doesn't QEMU log double triple faults or other reboot reasons? Commented May 28, 2023 at 16:59
  • Also your code is not public. Commented May 28, 2023 at 17:16
  • github.com/meilihao/learn_asm/tree/master/example/protect_mode Commented May 29, 2023 at 1:54
  • 1
    Maybe bochs's firmware executes bootloader with already disabled interrupts? Or maybe difference in other initial condition, e.g.ss:sp. Commented May 29, 2023 at 2:57
  • 3
    I managed to load it in GDB/QEMI(see stackoverflow.com/questions/32955887/…) and it looks like your disk read has some kind of issue that manifests in such a way that the JMP at the beginning of the base load address (0x900) seems to get read into memory but the rest of the data from disk seems to get placed into memory in the wrong place. It is random in nature where the code that enters protect mode gets loaded and the FAR JMP ends up failing when it jumps to an address that doesn't have the expected code. Commented May 30, 2023 at 16:00

1 Answer 1

0

my first solution is loader2.s:

  1. delete times 60 dq 0
  2. add cli before lgdt

The problem has improved significantly.

Then move all variable definitions to the end of the file (loader_ok.s), and the problem disappears completely. This step is amazing, and it was discovered by accident. Can't use gdb to debug, so the specific reason is unknown.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.