Page 1 of 1

[SOLVED] Weird bug with IRQ

Posted: Fri May 07, 2021 10:50 am
by lubenard
Hey everyone !
This is my first post on this forum, and that might be a stupid question, but a cannot find the bug.

I am trying to developp a very small kernel.

I want to have at least the keyboard working, but i cannot make it work correctly.

It seems there is 2 problems in 1:

The irq event triggered depend of the text written on screen (wtf ?)

Which i think is the cause why keyboard 'pressed' event is sent twice while clicking once.

Here is my the code of my main:

Code: Select all

void k_main(void)
{
	terminal_initialize();
	init_gdt();
	init_idt();
	//terminal_writestr("42\n");

	asm volatile ("sti");
	init_kbd();
	terminal_writestr("a");

	for(;;) {
		asm("hlt");
	}
}
As i said, the irq fired depend on the string lenght of this

Code: Select all

terminal_writestr("a");
string.
One letter -> Irq 1, 2 letters irq 2, etc...

I am thinking of the way of handling irqs, but i cannot find my mistake

Here is the asm code for irqs:

Code: Select all

%macro IRQ 2                    ; We need 2 parameters for our macro
  global irq%1                  ; First declare the name (irq0, irq1 etc...) as global
  irq%1:                        ; Declare our function with the name (irq0, irq1 etc...)
    cli                         ; Disable interrupts
    push byte 0                 ; push 0 on the stack
    push byte %2                ; Push irqcode into the stack (second parameter)
    jmp irq_common_stub         ; call irq_common_stub function
%endmacro

IRQ 0, 32
IRQ 1, 33
IRQ 2, 34
IRQ 3, 35
IRQ 4, 36
IRQ 5, 37
IRQ 6, 38
IRQ 7, 39
IRQ 8, 40
IRQ 9, 41
IRQ 10,42
IRQ 11,43
IRQ 12,44
IRQ 13,45
IRQ 14,46
IRQ 15,47

irq_common_stub:
    pusha           ; push everything on the stack
    push ds
    push es
    push fs
    push gs
    mov ax, 0x10
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov eax, esp
    push eax
    mov eax, irq_handler
    call eax
    pop eax
    pop gs
    pop fs
    pop es
    pop ds
    popa
    add esp, 8
    iret
Here is the full url of my project:
https://github.com/lubenard/kfs-1

Thanks !

Re: Weird bug with IRQ

Posted: Sun May 09, 2021 10:32 pm
by Octocontrabass
Your registers_t struct doesn't match the data being passed to your interrupt handler.

Once you fix that, you'll need to do something about how you're accessing beyond the end of this array.

Re: Weird bug with IRQ

Posted: Mon May 10, 2021 2:07 pm
by lubenard
Thanks !
Okay, so i think i fixed it, BUT, i am getting a invalid opcode when trying to type keyboard touch.
It is happening when calling the keyboard_handler in the irq handler.
It seems the address are the same, so i'll keep investigate with bochs
Thank's for helping !

Re: Weird bug with IRQ

Posted: Thu May 13, 2021 7:57 am
by lubenard
Okay, i fixed it !
I was getting a invalid opcode because SSE instructions were not enabled.
I followed this link: https://stackoverflow.com/questions/665 ... nting-irqs
and https://wiki.osdev.org/SSE

Re: [SOLVED] Weird bug with IRQ

Posted: Thu May 13, 2021 11:08 am
by Octocontrabass
Don't forget, your IRQ handlers need to save and restore SSE registers if your kernel is using them.

Or you can pass the -mgeneral-regs-only flag to your compiler to tell it not to use SSE registers in your kernel.