General Protection Fault for timer IRQ

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.
Post Reply
Rakhyvel
Posts: 3
Joined: Tue Jul 27, 2021 10:54 pm
Libera.chat IRC: Rakhyvel

General Protection Fault for timer IRQ

Post by Rakhyvel »

I am following a tutorial and am getting stuck with my timer's IRQ routine, and getting some super weird behaviour.

Basically, I have a function in a file called VGA.c that puts a character at a certain location on the screen. When I call the function in my timer callback handler, I get a general protection fault.

Code: Select all

/* Called after IRQ0 is raised */
void Timer_callback(registers_t regs) {
	VGA_TerminalPutEntryAt('X', VGA_COLOR_WHITE, 2, 2); // This call causes a GPF
}
However, when I copy and paste the exact same function into my timer.c file, and call that function, there is no GPF.

Code: Select all

void TerminalPutEntryAt(char c, uint8_t color, size_t x, size_t y) 
{
	const size_t index = y * VGA_WIDTH + x;
	terminal_buffer[index] = VGA_Entry(c, color); // Put the character and color byte in the VGA memory map, which starts at 0xC00B8000
}

void Timer_callback(registers_t regs) {
	TerminalPutEntryAt('X', VGA_COLOR_WHITE, 2, 2); // This call does not
}
I feel like this rules out any paging or stack issues.

Any time an IRQ is raised, I have the IDT point to a stub defined in assembly. This pushes the IRQ and ISR number, and then calls a more general IRQ stub, which calls a dispatch, which calls the actual callback function.

Code: Select all


global irq0

; ...

irq0:
	cli
	push byte 0
	push byte 32
	jmp irq_common_stub

; ...


; Called by IRQ stubs below
irq_common_stub:
    pusha 
    mov ax, ds
    push eax

    mov ax, 0x10
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    
    call Interrupt_IRQHandler ; Different than the ISR code

    pop ebx  ; Different than the ISR code
    mov ds, bx
    mov es, bx
    mov fs, bx
    mov gs, bx

    popa
    add esp, 0x8

    sti
    iret

Code: Select all

/* Called by irq_common_stub, sends acknowledge to PIC, and then calls IRQ handler */
void Interrupt_IRQHandler(registers_t r) {
	// Need to send EOI to PICs or they will not send another interrupt
	if (r.int_no >= 40) {
		IO_OutByte(0xA0, 0x20); // slave
	}
	IO_OutByte(0x20, 0x20); // master

	// Handle the interrupt in a more modular way
	if (interrupt_handlers[r.int_no] != 0) {
		isr_t handler = interrupt_handlers[r.int_no];
        if(handler) {
		    handler(r);
        }
	}
}
I know the whole dispatch process works because the timer callback does get called. I'm really at a loss for what could be going on, please let me know if you need to see any other code, any help is appreciated! Thanks
Octocontrabass
Member
Member
Posts: 5440
Joined: Mon Mar 25, 2013 7:01 pm

Re: General Protection Fault for timer IRQ

Post by Octocontrabass »

OS development tutorials are full of bugs. It looks like you might be running into this bug.
Rakhyvel
Posts: 3
Joined: Tue Jul 27, 2021 10:54 pm
Libera.chat IRC: Rakhyvel

Re: General Protection Fault for timer IRQ

Post by Rakhyvel »

That makes a lot of sense actually, thanks!
Rakhyvel
Posts: 3
Joined: Tue Jul 27, 2021 10:54 pm
Libera.chat IRC: Rakhyvel

Re: General Protection Fault for timer IRQ

Post by Rakhyvel »

Octocontrabass wrote:OS development tutorials are full of bugs. It looks like you might be running into this bug.
I went ahead and corrected the code to pass in a pointer to the register struct instead. However, I still got a GPF whenever I tried to call a function outside of the timer.c file.

I know it's cliche to blame the optimizer, but when I removed the optimizer flags, it worked as intended.

Is this a sign I'm just not understanding how the code should be structured?
Octocontrabass
Member
Member
Posts: 5440
Joined: Mon Mar 25, 2013 7:01 pm

Re: General Protection Fault for timer IRQ

Post by Octocontrabass »

No, it's a sign there's another bug in your code.

If it's not one of the bugs listed in that page, you might have to give us a bit more information to find it.
Post Reply