Debugging qemu reboots - IRQs not Working with GRUB [Solved]

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
Lagor
Posts: 23
Joined: Mon Nov 25, 2019 3:34 pm

Debugging qemu reboots - IRQs not Working with GRUB [Solved]

Post by Lagor »

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
Last edited by Lagor on Tue Jan 07, 2020 10:37 am, edited 1 time in total.
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: Debugging qemu reboots

Post by SpyderTL »

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
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
User avatar
iansjack
Member
Member
Posts: 4938
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Debugging qemu reboots

Post by iansjack »

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.
Lagor
Posts: 23
Joined: Mon Nov 25, 2019 3:34 pm

Re: Debugging qemu reboots

Post by Lagor »

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
User avatar
iansjack
Member
Member
Posts: 4938
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Debugging qemu reboots

Post by iansjack »

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.
Lagor
Posts: 23
Joined: Mon Nov 25, 2019 3:34 pm

Re: Debugging qemu reboots

Post by Lagor »

Does this mean that if i put a

Code: Select all

 for(;;);
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.
User avatar
iansjack
Member
Member
Posts: 4938
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Debugging qemu reboots

Post by iansjack »

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.
Lagor
Posts: 23
Joined: Mon Nov 25, 2019 3:34 pm

Re: Debugging qemu reboots

Post by Lagor »

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
mmdmine
Member
Member
Posts: 47
Joined: Sat Dec 28, 2019 5:19 am
Location: Iran
Contact:

Re: Debugging qemu reboots - IRQs not Working with GRUB [Sol

Post by mmdmine »

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:

Code: Select all

multiboot /mykernel.elf
boot
you can automate this by writing a grub.cfg
on qemu you dont need grub to bbot multiboot:

Code: Select all

qemu -kernel mykernel.elf
also the -S switch waits for GDB debugger. checkout 'qemu -help'.
Post Reply