Debugging qemu reboots - IRQs not Working with GRUB [Solved]
Debugging qemu reboots - IRQs not Working with GRUB [Solved]
HI everyone,
I have a simple question.
I wrote a simple bootloader + kernel as a binary and launched it with qemu. So far so good.
Then I decided to use grub2 as a boot loader which implied recompiling the whole kernel as an ELF and adding the multiboot header.
Now this is where my problems begun, i managed to get grub launch my os, it does its things, but then after it displayed the messages and the prompt that i programmed, just reboots.
I read some guides and it seems that most reboots like mine are causes by mishandling some low level stuff but, in my case, i can succesfully enter pmode, install isr/irq and print some characters on screen.
My question is, how do i debug what's wrong with my os? Where can i find a 'stacktrace' of the errors and such? I googled and couldn't really find a straight solution to this issue.
I know that with the little infos gave it's very hard for anyone to pinpoint the problem but, once again, my question is more about how to deal with qemu errors than it is about kernel devel.
I'm using QEMU emulator version 4.0.0 (Debian 1:4.0+dfsg-0ubuntu9.1).
Thank you so much, any help would be really appreciated.
FG
I have a simple question.
I wrote a simple bootloader + kernel as a binary and launched it with qemu. So far so good.
Then I decided to use grub2 as a boot loader which implied recompiling the whole kernel as an ELF and adding the multiboot header.
Now this is where my problems begun, i managed to get grub launch my os, it does its things, but then after it displayed the messages and the prompt that i programmed, just reboots.
I read some guides and it seems that most reboots like mine are causes by mishandling some low level stuff but, in my case, i can succesfully enter pmode, install isr/irq and print some characters on screen.
My question is, how do i debug what's wrong with my os? Where can i find a 'stacktrace' of the errors and such? I googled and couldn't really find a straight solution to this issue.
I know that with the little infos gave it's very hard for anyone to pinpoint the problem but, once again, my question is more about how to deal with qemu errors than it is about kernel devel.
I'm using QEMU emulator version 4.0.0 (Debian 1:4.0+dfsg-0ubuntu9.1).
Thank you so much, any help would be really appreciated.
FG
Last edited by Lagor on Tue Jan 07, 2020 10:37 am, edited 1 time in total.
Re: Debugging qemu reboots
There should be a configuration option in QEMU that allows you to step through the boot sequence one instruction at a time, and an option to break instead of rebooting on a triple fault.
Also, you can use GDB to remotely debug the session. https://stackoverflow.com/questions/142 ... db-in-qemu
You also try using BOCHS instead of QEMU, which allows you to debug your OS, as well.
Let us know what you find.
EDIT: Also check out this similar thread: viewtopic.php?f=1&t=36254
Also, you can use GDB to remotely debug the session. https://stackoverflow.com/questions/142 ... db-in-qemu
You also try using BOCHS instead of QEMU, which allows you to debug your OS, as well.
Let us know what you find.
EDIT: Also check out this similar thread: viewtopic.php?f=1&t=36254
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Re: Debugging qemu reboots
Does your code eventually reach an endless loop? If you allow your main() (or kmain(), or whatever you call it) to return then you are almost certainly going to get a triple fault, which means a reboot in qemu.
Apart from using a debugger, you can write exception handlers for every possible exception (in particular General Protection Faults, Page Faults, and Double Faults) that halt the processor (either via a HLT instruction or an infinite loop). Then you can inspect the state of the machine, and trace the stack frames, using the qemu monitor.
Apart from using a debugger, you can write exception handlers for every possible exception (in particular General Protection Faults, Page Faults, and Double Faults) that halt the processor (either via a HLT instruction or an infinite loop). Then you can inspect the state of the machine, and trace the stack frames, using the qemu monitor.
Re: Debugging qemu reboots
Thank you both for your replies.
I am debugging with gdb and my code seems fine.
I thought about the infinite loop thing but it doesnt add up.
When booting using my asm bootloader and a binary format for the kernel, qemu doesnt complain and everything gets executed as expected.
I also noted that when i dd the image to an usb drive and boot on my physical machine, grub complains that cant find the multiboot header. This is weird to me since it's a bit by bit copy, probably there's some differences on how i launch qemu, i use the -cdrom os.io parameter which seems to be different than booting from a usb pen on my PC.
As soon as i have some more free time i'll investigate more (how do you write an OS while having a job and a social life? any tutorials on that? ;p ).
Cheers
FG
I am debugging with gdb and my code seems fine.
I thought about the infinite loop thing but it doesnt add up.
When booting using my asm bootloader and a binary format for the kernel, qemu doesnt complain and everything gets executed as expected.
I also noted that when i dd the image to an usb drive and boot on my physical machine, grub complains that cant find the multiboot header. This is weird to me since it's a bit by bit copy, probably there's some differences on how i launch qemu, i use the -cdrom os.io parameter which seems to be different than booting from a usb pen on my PC.
As soon as i have some more free time i'll investigate more (how do you write an OS while having a job and a social life? any tutorials on that? ;p ).
Cheers
FG
Re: Debugging qemu reboots
All I can add is that if you allow your code to run without eventually halting the processor, or looping, you are almost certain to get a triple fault as you run off the end of your code. That this didn't happen with a custom boot loader is irrelevant - you just got lucky - running beyond the end of your code is a sure route to failure.
Re: Debugging qemu reboots
Does this mean that if i put a as the last instruction of my kernel_main should solve the issue? Because i've tried and it does not, maybe i actually have some error somewhere in my code.
Code: Select all
for(;;);Re: Debugging qemu reboots
It's a start.
I would still recommend those exception handlers that pause the processor. Then you can see what exception is being called and which instruction is causing it.
I would still recommend those exception handlers that pause the processor. Then you can see what exception is being called and which instruction is causing it.
Re: Debugging qemu reboots
Hi, it's been a while but thanks to these winter holidays i managed to take a look at my code and found a solution.
Turns out i was doing things right, the problem was that booting with GRUB expects to have a GDT setup.
For those that might need it, the solution was posted here:
viewtopic.php?f=1&t=33160&p=285871#p285871
After that i noticed that the cursor was not showing and, again, it was GRUB's fault, solution's here:
viewtopic.php?f=1&t=32222
So, bottom line for me was:
GRUB does some stuff that you might not be aware of and the code that works when bootstrapping on your own might fail when getting booted up by external sources.
Thanks everyone for the help!
FG
Turns out i was doing things right, the problem was that booting with GRUB expects to have a GDT setup.
For those that might need it, the solution was posted here:
viewtopic.php?f=1&t=33160&p=285871#p285871
After that i noticed that the cursor was not showing and, again, it was GRUB's fault, solution's here:
viewtopic.php?f=1&t=32222
So, bottom line for me was:
GRUB does some stuff that you might not be aware of and the code that works when bootstrapping on your own might fail when getting booted up by external sources.
Thanks everyone for the help!
FG
Re: Debugging qemu reboots - IRQs not Working with GRUB [Sol
for endless loop you can also use while(true).
for booting with grub on physical device, you shouldnt dd an iso image to a usb stick. you must install the grub MBR or copy the grub.efi to \BOOT\EFI\ as bootx86.efi or bootx64.efi for uefi then copy your OS kernel to usb as a simple file. when you booted grub you can boot your kernel by this command:
you can automate this by writing a grub.cfg
on qemu you dont need grub to bbot multiboot:
also the -S switch waits for GDB debugger. checkout 'qemu -help'.
for booting with grub on physical device, you shouldnt dd an iso image to a usb stick. you must install the grub MBR or copy the grub.efi to \BOOT\EFI\ as bootx86.efi or bootx64.efi for uefi then copy your OS kernel to usb as a simple file. when you booted grub you can boot your kernel by this command:
Code: Select all
multiboot /mykernel.elf
boot
on qemu you dont need grub to bbot multiboot:
Code: Select all
qemu -kernel mykernel.elf

