Page 1 of 2

Keyboard handler

Posted: Wed Dec 25, 2002 11:49 am
by pini
My kernel has currently the following components :
Correct GDT
Correct IDT
PIC programming
Correct tty driver (almost print functions ;))

I am trying to write a keyboard handler but I have a problem. I wrote an asm IRQ function which call my C handling function.
I wrote a keyboard init funtion just with some simple instructions to initialize my keyboard buffer :
[pre]current = 0;
last = 0;[/pre]
where 'current' and 'last' are unsigned char I will use to remember the position in the keyboard buffer.

This code works perfectly on two machines (P166 & 386) but reboots two others (P90 & PIII667). I found out that it was the second instruction ('last = 0;') that causes this.

I also tried to call a dummy function within my keyboard init function instead of the two instructions above, and it reboots too.

Can anyone help me ?

Re:Keyboard handler

Posted: Wed Dec 25, 2002 6:31 pm
by jrfritz
Maybe if I saw your source I could help...

Re:Keyboard handler

Posted: Thu Dec 26, 2002 5:55 pm
by Pype.Clicker
this sounds like the stack is too small or something alike ...
Maybe you also have a problem with your ISR save/restore code.

Re:Keyboard handler

Posted: Thu Dec 26, 2002 8:20 pm
by jrfritz
Here is a working Key hander ( I got some of it from curfer(forgot his exact screenname) )

Needs to be in asm:

EXTERN KeyHan ; Your Key handling function
GLOBAL IRQ1
IRQ1:
pusha
call KeyHan
mov al, 0x20
out 0x20, al
popa
IRET

In your C code:

void KeyHan
{
// stuff... and don't use outportb( 0x20, 0x20 );
// since the other function does that for you.

int key;
key = inportb( 0x60 );
}

Re:Keyboard handler

Posted: Fri Dec 27, 2002 6:21 am
by pini
Thanks a lot for these hints. I am gonna make some test and will report the results here this evening.

Re:Keyboard handler

Posted: Fri Dec 27, 2002 6:46 am
by pini
My kernel reboots when I call my C keyboard handler in my irq1 asm function.
So instead of calling it, I wrote on the screen the address of the handler. I got 12892.
Then I added a instructions in my C kernel body function to wrote the address of my keyboard handler, and I got 12896.

Shouldn't I have the same two addresses ?
Is this my problem ?

Re:Keyboard handler

Posted: Fri Dec 27, 2002 7:50 am
by pini
Here's my irq1 (n)asm functions :
[pre]EXTERN keyboard

irq1:
cli
push eax
call keyboard
mov al,0x20
out 0x20,al
pop eax
sti
iret[/pre]

and here's my C keyboard handler function :
[pre]extern void putchar(unsigned char);

void keyboard(void)
{
unsigned char inv;

inv=inb(0x60);
putchar('.');
return;
}
[/pre]

Here's a part of my C kernel body function
[pre]// Init PIC
outb(0x20,0x11);
outb(0x21,0x20);
outb(0x21,0x4);
outb(0x21,0x1);
outb(0x21,0xFD);
outb(0xA0,0x11);
outb(0xA1,0x70);
outb(0xA1,0x2);
outb(0xA1,0x1);
outb(0xA1,0xFF);
[/pre]

'inb' and 'outb' are simple asm functions to get (set) a byte from (to) a port.

This sequence causes the computer to reboot when I press a key
What have I made wrong ?

Re:Keyboard handler

Posted: Fri Dec 27, 2002 7:53 am
by Whatever5k
Remove the "return" in the keyboard() function.

Re:Keyboard handler

Posted: Fri Dec 27, 2002 7:56 am
by jrfritz
Why CLI in the IRQ1 function?

And use pusha instead of push eax.

Re:Keyboard handler

Posted: Fri Dec 27, 2002 8:25 am
by pini
Removing the 'return' didn't change anything :(

I put a 'cli' in my irq1 function because I think that it may be more careful to unable other interruptions when you are just processing one. (imagine a timer interruption while you are processing a key press...)

Re:Keyboard handler

Posted: Fri Dec 27, 2002 8:30 am
by Whatever5k
You have to save all registers and load them with a save selector...
try something like this:

Code: Select all

%macro SAVE 0
        ; save some registers
        pushad
        push ds
        push es
        push fs
        push gs
        
        mov eax, DS_SELECTOR 
        mov ds, eax
        mov es, eax
        mov fs, eax
        mov gs, eax
%endmacro

%macro RESTORE 0
        ; restore saved registers
        pop gs
        pop fs
        pop es
        pop ds
        popad
%endmacro


irq1:
 cli
 SAVE
 call keyboard
 mov al, 0x20
 out 0x20, al
 RESTORE
 sti
 iretd

Re:Keyboard handler

Posted: Fri Dec 27, 2002 9:00 am
by Unexpected
Pini, your handler is good(in nasm):

Code: Select all

EXTERN _KeyHan
GLOBAL IRQ1
IRQ1:
pusha
call _KeyHan
mov al, 0x20
out 0x20, al
popa
IRET
But maby your IDT is wrong?

Re:Keyboard handler

Posted: Fri Dec 27, 2002 9:26 am
by pini
I made a few tests and here is what I found out :

when I make a dummy keyboard handler like this :
[pre]void keyboard(void)
{
}
[/pre]
it works, but when I insert this instruction :
[pre]void keyboard(void)
{
unsigned char value;

value=inb(0x60);
}
[/pre]
it reboots.

What does this mean ? I am completly lost :-\

Re:Keyboard handler

Posted: Fri Dec 27, 2002 9:43 am
by pini
I don't think it comes from my IDT, because I made a printing test with the timer interrupt and ... well, my keyboard handler works on other machines and with bochs 2.0.

Re:Keyboard handler

Posted: Fri Dec 27, 2002 10:13 am
by jrfritz
CLIing ... won't that stop your PIT and mess up your OS when you come to multi tasking?