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.
C compiled code works on qemu but not on physical hardware
-
- Member
- Posts: 91
- Joined: Mon Apr 20, 2020 11:02 am
C compiled code works on qemu but not on physical hardware
A beginner developer/student. Likes to know stuff. Don't have an OS to put here.
-
- Member
- Posts: 5575
- Joined: Mon Mar 25, 2013 7:01 pm
Re: C compiled code works on qemu but not on physical hardwa
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.pranavappu007 wrote:I coded a simple bootstrapper that switches CPU into 32 bit mode and executes kernel and hangs.
Re: C compiled code works on qemu but not on physical hardwa
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.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.
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
-
- 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
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