Page 1 of 1
Keyboard interrupt not working in bochs, works in virtualbox
Posted: Thu Jun 20, 2013 4:19 pm
by Gogeta70
So, i implemented a keyboard driver a week or so ago, and it worked great. Now, suddenly, it has stopped working in bochs. I also use virtualbox for testing, and it works there though. Also, a friend of mine tried it in vmware player, but it doesn't work there either.
I've checked my PIC's interrupt mask - it's 0. The system timer generated by the PIT also works. And, if i add in "int 33" to my system timer interrupt, then keyboard input works fine, but that's a workaround, not a solution. So, considering manually calling the interrupt, i know my IDT is set up right. Also the PIC is configured, as it's generating IRQ's for the PIT.
What else can i look at?
Re: Keyboard interrupt not working in bochs, works in virtua
Posted: Thu Jun 20, 2013 6:32 pm
by Brendan
Hi,
Gogeta70 wrote:What else can i look at?
Look at
PS/2 controller initialisation (specifically step 4).
Here's a relatively typical scenario:
- Firmware starts GRUB
- GRUB displays some sort of boot menu or command prompt or something
- The user presses the enter key to start the OS
- GRUB loads the OS/kernel, disables IRQs, switches to protected mode and passes control to the OS/kernel
- The user releases the enter key; causing a "enter key released" scan code to get put into the PS/2 controller's buffer and an IRQ to be sent to the PIC chip
- The OS/kernel initialises the PIC chips; which makes the PIC chip forget about the IRQ it got from the PS/2 controller
- The OS/kernel fails to initialise the PS/2 controller; which means that the "enter key released" scan code remains lodged in the PS/2 controller's buffer
- Any data sent from the keyboard to the PS/2 controller will now be ignored because they won't fit in the (already full) PS/2 controller's 1-byte buffer; and because no new bytes are going into the buffer no new IRQs will be generated
Note: It doesn't have to be GRUB - it could be any boot loader where the user presses a key at the wrong time (either because the user pressed a key for no reason, or because the user had to press a key to start the OS).
Of course this depends on timing. If the user releases the key before GRUB has disabled IRQs (e.g. the computer is slower and it took longer from GRUB to load the kernel/OS), then the BIOS handles the "enter key released" scan code and the "buffer full" problem will be avoided (unless the user presses another key at the wrong time).
It's also possible for the user to release the key after the OS has initialised everything, such that the buffer is empty (and an IRQ occurs) when the enter key is released, and the OS handles the "enter key released" IRQ. However this is less likely as the keyboard has a "key repeat" feature where it'll keep sending the scan codes every "x ms" until the key is released (and the second/repeated "enter pressed" scan code will cause the same "buffer full" problem instead).
Cheers,
Brendan
Re: Keyboard interrupt not working in bochs, works in virtua
Posted: Thu Jun 20, 2013 8:48 pm
by singerng
Two questions for clarification:
So, i implemented a keyboard driver a week or so ago, and it worked great.
1. When you say "it worked great", do you mean you tested it in Bochs and it worked great, or you just tested in in VirtualBox? (And if it did work in Bochs, what changes did you make that could've caused it to stop working?)
2. Also, just to clarify, the keyboard interrupt isn't happening at all? (as opposed to something being wrong with your keyboard code)
Re: Keyboard interrupt not working in bochs, works in virtua
Posted: Fri Jun 21, 2013 9:11 am
by Gogeta70
Brendan:
I added the following code to the start of the kernel to empty the keyboard's buffer, but it didn't work:
Code: Select all
unsigned char kbb = 0;
while(((kbb = inb(0x64)) & 1) == 1)
{
inb(0x60)
}
I added it before initializing the PIC and before enabling interrupts.
Singerng:
It worked perfectly in both bochs and virtualbox. The only change i've made is i've enabled paging. And you're right, the keyboard interrupt isn't happening at all. Inside the interrupt handler, i put a magic breakpoint for bochs (and i enabled them in the config file - i use them all the time, so i know they work) in the interrupt handler for the keyboard, but it never hits the breakpoint. Additionally, if i put "int 33" in the system timer interrupt handler, it calls the keyboard interrupt and i can type just fine, so i know the keyboard driver works.
I don't know if this helps, but there is a strange behavior in virtualbox. As soon as it boots, before pressing any keys after i hit enter to start up in grub, it ignores the first keypress. For example, if i type "abcd", i get "bcd". After that first keypress though, it doesn't do it again until i restart the VM.
Re: Keyboard interrupt not working in bochs, works in virtua
Posted: Fri Jun 21, 2013 1:38 pm
by Mikemk
Gogeta70 wrote:For example, if i type "abcd", i get "bcd". After that first keypress though, it doesn't do it again until i restart the VM.
I had a similar issue when I implemented scrolling. My math was off by one, and so the first keypress got sent to 0xb8fa0, then the second+ worked correctly.
Re: Keyboard interrupt not working in bochs, works in virtua
Posted: Fri Jun 21, 2013 3:01 pm
by Gogeta70
It turns out the issue was that the keyboard buffer was full. I just had to move the code that empties the buffer to AFTER i set up my interrupts and configure the PIC.
Thanks for your help everyone!