OSDev.org

The Place to Start for Operating System Developers
It is currently Sat Jan 18, 2020 1:04 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Debugging qemu reboots - IRQs not Working with GRUB [Solved]
PostPosted: Mon Nov 25, 2019 3:54 pm 
Offline

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

Top
 Profile  
 
 Post subject: Re: Debugging qemu reboots
PostPosted: Mon Nov 25, 2019 4:48 pm 
Offline
Member
Member
User avatar

Joined: Sun Sep 19, 2010 10:05 pm
Posts: 1066
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: DOSBox Compatibility
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott


Top
 Profile  
 
 Post subject: Re: Debugging qemu reboots
PostPosted: Tue Nov 26, 2019 5:05 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 3617
Location: Chichester, UK
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.


Top
 Profile  
 
 Post subject: Re: Debugging qemu reboots
PostPosted: Tue Nov 26, 2019 5:42 am 
Offline

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


Top
 Profile  
 
 Post subject: Re: Debugging qemu reboots
PostPosted: Tue Nov 26, 2019 5:54 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 3617
Location: Chichester, UK
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.


Top
 Profile  
 
 Post subject: Re: Debugging qemu reboots
PostPosted: Tue Nov 26, 2019 7:19 am 
Offline

Joined: Mon Nov 25, 2019 3:34 pm
Posts: 5
Does this mean that if i put a
Code:
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.


Top
 Profile  
 
 Post subject: Re: Debugging qemu reboots
PostPosted: Tue Nov 26, 2019 7:29 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 3617
Location: Chichester, UK
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.


Top
 Profile  
 
 Post subject: Re: Debugging qemu reboots
PostPosted: Tue Jan 07, 2020 10:37 am 
Offline

Joined: Mon Nov 25, 2019 3:34 pm
Posts: 5
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:
https://forum.osdev.org/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:
https://forum.osdev.org/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


Top
 Profile  
 
 Post subject: Re: Debugging qemu reboots - IRQs not Working with GRUB [Sol
PostPosted: Tue Jan 07, 2020 2:29 pm 
Offline
Member
Member

Joined: Sat Dec 28, 2019 5:19 am
Posts: 37
Location: Iran
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:
multiboot /mykernel.elf
boot

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

also the -S switch waits for GDB debugger. checkout 'qemu -help'.

_________________
https://mmdmine.github.io


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group