Keyboard handler

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.
pini

Keyboard handler

Post 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 ?
jrfritz

Re:Keyboard handler

Post by jrfritz »

Maybe if I saw your source I could help...
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Keyboard handler

Post 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.
jrfritz

Re:Keyboard handler

Post 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 );
}
pini

Re:Keyboard handler

Post by pini »

Thanks a lot for these hints. I am gonna make some test and will report the results here this evening.
pini

Re:Keyboard handler

Post 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 ?
pini

Re:Keyboard handler

Post 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 ?
Whatever5k

Re:Keyboard handler

Post by Whatever5k »

Remove the "return" in the keyboard() function.
jrfritz

Re:Keyboard handler

Post by jrfritz »

Why CLI in the IRQ1 function?

And use pusha instead of push eax.
pini

Re:Keyboard handler

Post 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...)
Whatever5k

Re:Keyboard handler

Post 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
Unexpected

Re:Keyboard handler

Post 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?
pini

Re:Keyboard handler

Post 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 :-\
pini

Re:Keyboard handler

Post 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.
jrfritz

Re:Keyboard handler

Post by jrfritz »

CLIing ... won't that stop your PIT and mess up your OS when you come to multi tasking?
Post Reply