C compiled code works on qemu but not on physical hardware

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
pranavappu007
Member
Member
Posts: 91
Joined: Mon Apr 20, 2020 11:02 am

C compiled code works on qemu but not on physical hardware

Post by pranavappu007 »

I wrote a simple 'kernel' that is compiled using gcc(target mode=elf_i386). I coded a simple bootstrapper that switches CPU into 32 bit mode and executes kernel and hangs.

The setup works perfectly on a qemu emulator but if loaded into a USB and try to boot on actual hardware, it loads and executes the nasm-assembled bootstrapper and switches to 32-bit mode(tested), but as soon as kernel is loaded system immediately reboots. Can you give an insight into the problem?

USB boot works on qemu - so no problem there.
A beginner developer/student. Likes to know stuff. Don't have an OS to put here.
Octocontrabass
Member
Member
Posts: 5575
Joined: Mon Mar 25, 2013 7:01 pm

Re: C compiled code works on qemu but not on physical hardwa

Post by Octocontrabass »

pranavappu007 wrote:I coded a simple bootstrapper that switches CPU into 32 bit mode and executes kernel and hangs.
Writing a bootloader that works in one emulator is easy. Writing a bootloader that works everywhere is really, really hard. Try adding a multiboot header to your kernel and see if GRUB can boot it.
User avatar
BenLunt
Member
Member
Posts: 941
Joined: Sat Nov 22, 2014 6:33 pm
Location: USA
Contact:

Re: C compiled code works on qemu but not on physical hardwa

Post by BenLunt »

pranavappu007 wrote:I wrote a simple 'kernel' that is compiled using gcc(target mode=elf_i386). I coded a simple bootstrapper that switches CPU into 32 bit mode and executes kernel and hangs.

The setup works perfectly on a qemu emulator but if loaded into a USB and try to boot on actual hardware, it loads and executes the nasm-assembled bootstrapper and switches to 32-bit mode(tested), but as soon as kernel is loaded system immediately reboots. Can you give an insight into the problem?

USB boot works on qemu - so no problem there.
The most common answer to this exact problem is the segment registers. Each emulator and real firmware may use different default segment registers at boot time.

If you assume something about the segment registers, you will most likely get exactly what you are explaining here.

For example, if you assume the DS register is 0x0000 at boot (in QEMU for example) and then continue to assume this at your transition to 32-bit code, you will get a triple fault when the DS register is actually 0x0040. (0x0040 is a very common value for DS at boot sector start time)

Most likely, but not absolutely, it is an assumption about a segment register.

How did you confirm that it actually made it to 32-bit pmode on the real hardware? Did you 'halt' just after the transition and the machine did not triple-fault? If this is the case, then you have indeed made it to 32-bit pmode. However, I would bet that it didn't make it that far. (Just my guess)

Ben
- http://www.fysnet.net/osdesign_book_series.htm
sunnysideup
Member
Member
Posts: 106
Joined: Sat Feb 08, 2020 11:11 am
Libera.chat IRC: sunnysideup

Re: C compiled code works on qemu but not on physical hardwa

Post by sunnysideup »

Yes. segment registers. Have a look at this stack overflow answer regarding bootloader tips: https://stackoverflow.com/questions/327 ... 6#32705076. It's a pretty nice answer
Post Reply