OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 8:06 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Keyboard interruption not working
PostPosted: Wed Aug 30, 2017 9:15 pm 
Offline

Joined: Wed Aug 30, 2017 9:04 pm
Posts: 6
I dont know whats wrong with my code.
My problem is that when an interruption happens, the virtualbox crash.
Can someone help me, please?

In this files i handle the interruption and set the IDT
Attachment:
interrupts.c [3.91 KiB]
Downloaded 52 times

Attachment:
interruptsAsm.s [206 Bytes]
Downloaded 35 times


Top
 Profile  
 
 Post subject: Re: Keyboard interruption not working
PostPosted: Wed Aug 30, 2017 9:43 pm 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

pedrodemargomes wrote:
I dont know whats wrong with my code.
My problem is that when an interruption happens, the virtualbox crash.
Can someone help me, please?


Start by trying to figure out if the problem is the IDT itself or in the "keyboard specific" part of the IRQ handler. An easy way to do this is to change "keyboard_handler" so it only does "iret" and nothing else - if that crashes there's a problem with the IDT itself (IDT base, IDT limit, pages IDT uses aren't mapped properly, ...) or that specific IDT entry (wrong interrupt, wrong IDT entry, wrong fields); and if that doesn't crash then the problem would be in something that "keyboard_handler_main()" does (e.g. maybe there's a bug in "printfHex()" that has nothing to do keyboard or IRQs).

The next step would be to try to divide the possibilities again - maybe (if the problem was something to do with the IDT) try starting the interrupt handler with a software interrupt (to rule out the possibly of "wrong interrupt"); maybe (if the problem was something to do with the "keyboard_handler_main()") you can comment out lines of code to determine which one is the problem.

Once you've run out of ways to divide the possibilities you'd want to gather more information about the symptoms. What happens in other emulators? If there's an exception, which one (and what does the CPU tell you about the exception)? Can you put a breakpoint just before the crash and single-step (while checking everything at between each step)?

Eventually you'll have a very specific idea of where the problem is, and enough information about the symptoms to figure out exactly what is going on (and probably have a very good idea of some way to fix the problem).


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
 Post subject: Re: Keyboard interruption not working
PostPosted: Thu Aug 31, 2017 4:39 am 
Offline
Member
Member

Joined: Thu Aug 13, 2015 4:57 pm
Posts: 384
pedrodemargomes wrote:
I dont know whats wrong with my code.
My problem is that when an interruption happens, the virtualbox crash.
Can someone help me, please?


One thing I find quite curious is that osdev is generally speaking considered to be difficult and as such anyone attempting it should have significant experience in related fields, yet maybe half of the time people can't even provide a decent description of their problem.. what's up with that?

What does "the virtualbox crash" actually mean? Have you found a bug in vbox that causes vbox to crash? Or is your OS crashing inside vbox? Is there any error message? What exactly happens?


Top
 Profile  
 
 Post subject: Re: Keyboard interruption not working
PostPosted: Thu Aug 31, 2017 6:22 am 
Offline

Joined: Wed Aug 30, 2017 9:04 pm
Posts: 6
Brendan wrote:
Hi,

pedrodemargomes wrote:
I dont know whats wrong with my code.
My problem is that when an interruption happens, the virtualbox crash.
Can someone help me, please?


Start by trying to figure out if the problem is the IDT itself or in the "keyboard specific" part of the IRQ handler. An easy way to do this is to change "keyboard_handler" so it only does "iret" and nothing else - if that crashes there's a problem with the IDT itself (IDT base, IDT limit, pages IDT uses aren't mapped properly, ...) or that specific IDT entry (wrong interrupt, wrong IDT entry, wrong fields); and if that doesn't crash then the problem would be in something that "keyboard_handler_main()" does (e.g. maybe there's a bug in "printfHex()" that has nothing to do keyboard or IRQs).

The next step would be to try to divide the possibilities again - maybe (if the problem was something to do with the IDT) try starting the interrupt handler with a software interrupt (to rule out the possibly of "wrong interrupt"); maybe (if the problem was something to do with the "keyboard_handler_main()") you can comment out lines of code to determine which one is the problem.

Once you've run out of ways to divide the possibilities you'd want to gather more information about the symptoms. What happens in other emulators? If there's an exception, which one (and what does the CPU tell you about the exception)? Can you put a breakpoint just before the crash and single-step (while checking everything at between each step)?

Eventually you'll have a very specific idea of where the problem is, and enough information about the symptoms to figure out exactly what is going on (and probably have a very good idea of some way to fix the problem).


Cheers,

Brendan


I did all of that and i discovered that the problem is located in the part where i set the IDT.
The error message is

--------------------------------------------------------------------------------
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!
!! Guru Meditation 1155 (VINF_EM_TRIPLE_FAULT)
!!
!! Skipping ring-0 registers and stack, rcErr=VINF_EM_TRIPLE_FAULT
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
DBGF: No debugger attached, waiting 1 second for one to attach (event=100)
1.Stopping the VM!

I still dont get what i am doing wrong.


Top
 Profile  
 
 Post subject: Re: Keyboard interruption not working
PostPosted: Thu Aug 31, 2017 9:42 am 
Offline
Member
Member

Joined: Thu Aug 13, 2015 4:57 pm
Posts: 384
pedrodemargomes wrote:
I did all of that and i discovered that the problem is located in the part where i set the IDT.
The error message is

--------------------------------------------------------------------------------
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!
!! Guru Meditation 1155 (VINF_EM_TRIPLE_FAULT)
!!
!! Skipping ring-0 registers and stack, rcErr=VINF_EM_TRIPLE_FAULT
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
DBGF: No debugger attached, waiting 1 second for one to attach (event=100)
1.Stopping the VM!

I still dont get what i am doing wrong.

Ok, so vbox doesn't crash, your OS does.

Can you single step to find the exact line of code that causes the triple fault?


Top
 Profile  
 
 Post subject: Re: Keyboard interruption not working
PostPosted: Thu Aug 31, 2017 10:07 am 
Offline
Member
Member
User avatar

Joined: Fri Aug 07, 2015 6:13 am
Posts: 1134
I smell some tutorial code and know exactly which one it is.
You are supposed to pass a pointer not data, for lidt instruction to use.
You don't have a correct pointer structure, it is not packed etc... many other bugs...

Edit: it looks like this is not relevant to your scenario, still your way of doing is awkward.

_________________
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader


Last edited by Octacone on Fri Sep 01, 2017 3:50 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Keyboard interruption not working
PostPosted: Thu Aug 31, 2017 10:49 am 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
Nothing stands out to me in the code you presented. However if I had to hazard any guesses it might be this:

- You are using a multiboot compliant bootloader (GRUB?) to get VirtualBox to boot your kernel
- You haven't set up a proper GDT before enabling interrupts. There is a rule in the multiboot spec that if you reload the segment registers (which can happen on an interrupt) you must set up a new GDT by reloading the GDT register (via LGDT instruction). Failure to set up your own GDT could result in a triple fault when the first interrupt occurs. Often you'll observe this problem when running a real version of GRUB (via an ISO image/CDROM/HDD etc). You may not see it if you run it with QEMU via its `-kernel` option.

I'm assuming you built this all as 32-bit code and not 64-bit. If you compiled/assembled/linked this as 64-bit code you'd have other issues.
I'm assuming you set up the stack before you called into your C code entry point
I'm assuming you issued a CLD instruction to set forward direction for the MOVS? instructions.

Setting up your idt_ptr structure is unorthodox but it looks valid. Your IDT structure looks okay and doesn't need packing because there will be no padding the way it is structured.
You should be using a CLD instruction in your interrupt handler routine before calling into a C function because eventually your kernel may run in an environment where the direction flag isn't what you expect. The generated C code will have the expectation of having the direction flag cleared to denote forward direction.

Providing a minimal complete verifiable example that can be built and tested would be ideal. If you had a project on github or similar service and provided a link it would be even more useful.


Top
 Profile  
 
 Post subject: Re: Keyboard interruption not working
PostPosted: Thu Aug 31, 2017 7:57 pm 
Offline

Joined: Wed Aug 30, 2017 9:04 pm
Posts: 6
MichaelPetch wrote:
- You haven't set up a proper GDT before enabling interrupts. There is a rule in the multiboot spec that if you reload the segment registers (which can happen on an interrupt) you must set up a new GDT by reloading the GDT register (via LGDT instruction). Failure to set up your own GDT could result in a triple fault when the first interrupt occurs. Often you'll observe this problem when running a real version of GRUB (via an ISO image/CDROM/HDD etc). You may not see it if you run it with QEMU via its `-kernel` option.


That was my problem, i have not set my GDT, so it only works with qemu -kernel.

Thanks a lot.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 60 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