Page 1 of 1

IRQs cause exceptions?

Posted: Fri Dec 22, 2017 11:14 pm
by thumble
Once I enable interrupts with sti, I get an exception.
Before I enable IRQs, I first remap the PIC to offsets 0x20 on master and 0x28 on slave; here is the remapping code:

Code: Select all

void irq_remap() {
	outb(0x20, 0x11);
	outb(0xA0, 0x11);
	outb(0x21, 0x20);
	outb(0xA1, 0x28);
	outb(0x21, 0x04);
	outb(0xA1, 0x02);
	outb(0x21, 0x01);
	outb(0xA1, 0x01);
	outb(0x21, 0x0);
	outb(0xA1, 0x0);
}
And this is my IRQ-installing code:

Code: Select all

void install_irqs() {
	irq_remap();
	set_idt_gate(32, (unsigned)irq0, 0x08, 0x8E);
	set_idt_gate(33, (unsigned)irq1, 0x08, 0x8E);
	set_idt_gate(34, (unsigned)irq2, 0x08, 0x8E);
	set_idt_gate(35, (unsigned)irq3, 0x08, 0x8E);
	set_idt_gate(36, (unsigned)irq4, 0x08, 0x8E);
	set_idt_gate(37, (unsigned)irq5, 0x08, 0x8E);
	set_idt_gate(38, (unsigned)irq6, 0x08, 0x8E);
	set_idt_gate(39, (unsigned)irq7, 0x08, 0x8E);
	set_idt_gate(40, (unsigned)irq8, 0x08, 0x8E);
	set_idt_gate(41, (unsigned)irq9, 0x08, 0x8E);
	set_idt_gate(42, (unsigned)irq10, 0x08, 0x8E);
	set_idt_gate(43, (unsigned)irq11, 0x08, 0x8E);
	set_idt_gate(44, (unsigned)irq12, 0x08, 0x8E);
	set_idt_gate(45, (unsigned)irq13, 0x08, 0x8E);
	set_idt_gate(46, (unsigned)irq14, 0x08, 0x8E);
	set_idt_gate(47, (unsigned)irq15, 0x08, 0x8E);
}
Here is IRQ 0 in assembly:

Code: Select all

irq0:
	cli
	push 0
	push 32
	jmp stub
This jumps to an assembly stub common to each IRQ:

Code: Select all

stub:
	pushad
	mov eax, esp
	push eax
	mov eax, handle_irq
	call eax
	pop eax
	popad
	add esp, 8
	iret
Which then calls this C handler:

Code: Select all

void handle_irq(struct regs *r) {
	void (*handler)(struct regs *r);
	handler = irq_routines[r->int_no - 32];
	if(handler) {
		handler(r);
	}
	if(r->int_no >= 40) {
		outb(0xA0, 0x20);
	}
	outb(0x20, 0x20);
}
I am somewhat suspicious that the issue is in my register frame, so here it is:

Code: Select all

struct regs {    
	dword errcode, int_no, ebp, esp, ebx, edx, ecx, eax, esi, edi;
};
Does anyone know what may be the issue?
Thanks

Re: IRQs cause exceptions?

Posted: Fri Dec 22, 2017 11:22 pm
by thumble
Never mind. Really was a problem with my regs structure; forgot that the CPU pushes some things as well (plus my order was reversed)
Here it is:

Code: Select all

struct regs {    
	dword edi, esi, ebp, esp, ebx, edx, ecx, eax, int_no, errcode, eip, cs, eflags, esp2, ss;
};