Page 1 of 1

Page faults and LEDs

Posted: Tue May 26, 2009 12:59 pm
by AUsername
Hmm, where to start,

Problem:
A function in my keyboard driver to set the keyboard LEDs causes a page fault, it actually has caused a lot of things such as causing the function after itself to loop forever, but I've gotten it to cause a triple fault (page fault) and after the triple fault it then catches the page fault and panics.

So now that you know the basic problem it seems pretty simple and straight forward, the LEDs setting function is causing a page fault. But if I comment out the install for the page fault's IRQ then it works perfectly with no page fault or triple fault.

So that leads me to believe that somehow, the install of the page fault IRQ is causing a page fault... but if I remove the LEDs setting code then it doesn't page fault...

It get's even weirder to know that the function to set the LEDs is called AFTER the install of the page fault.

PF Exception + LED function call: Triple fault (#PF) then page fault caught.
PF Exception + No LED function call: Works fine.
No PF Exception + LED function: Works fine.


core.cpp:

Code: Select all

//<Snip>
	enable();
	
	setvect(0,(void (__cdecl &)(void))divide_by_zero_fault);
	setvect(1,(void (__cdecl &)(void))single_step_trap);
	setvect(2,(void (__cdecl &)(void))nmi_trap);
	setvect(3,(void (__cdecl &)(void))breakpoint_trap);
	setvect(4,(void (__cdecl &)(void))overflow_trap);
	setvect(5,(void (__cdecl &)(void))bounds_check_fault);
	setvect(6,(void (__cdecl &)(void))invalid_opcode_fault);
	setvect(7,(void (__cdecl &)(void))no_device_fault);
	setvect(8,(void (__cdecl &)(void))double_fault_abort);
	setvect(10,(void (__cdecl &)(void))invalid_tss_fault);
	setvect(11,(void (__cdecl &)(void))no_segment_fault);
	setvect(12,(void (__cdecl &)(void))stack_fault);
	setvect(13,(void (__cdecl &)(void))general_protection_fault);
	setvect(14,(void (__cdecl &)(void))page_fault);
	setvect(16,(void (__cdecl &)(void))fpu_fault);
	setvect(17,(void (__cdecl &)(void))alignment_check_fault);
	setvect(18,(void (__cdecl &)(void))machine_check_abort);
	setvect(19,(void (__cdecl &)(void))simd_fpu_fault);
	//setvect(38,(void (__cdecl &)(void))i86_fdc_irq);
//<Snip>
	//Keyboard driver
	kkybrd_install(33);
	kkybrd_set_leds(false,false,false);
	LoadScreenUpdate();//This loops over and over (Prints out many |'s)
Keyboard driver:

Code: Select all

void kkybrd_set_leds(bool num, bool caps, bool scroll){
	uint8_t data = 0;
	data=(scroll) ? (data | 1) : data;
	data=(num) ? (data | 2) : data;
	data=(caps) ? (data | 4) : data;

	kybrd_enc_send_cmd(KYBRD_ENC_CMD_SET_LED);//To port 0x64
	kybrd_enc_send_cmd(data);//Should be 0000(0)
}

void kkybrd_install(int irq){
	setvect(irq,i86_kybrd_irq);
	_kkybrd_bat_res=true;
	_scancode=0;

	_numlock=false;
	_scrolllock=false;
	_capslock=false;
	
	_shift=false;
	_alt=false;
	_ctrl=false;
}
It could also be the PIC but I've already looked at that, and if it was the PIC then I would have known a long time ago because other IRQs work fine...

I'm pretty much stumped to what it could be. I have asked help from a couple of people and both of which couldn't figure it out much either. :/

Anyone got any ideas?

(Also, I just came back to the keyboard driver after working on other parts of the kernel for those of you that remember my last topic. :P)

Re: Page faults and LEDs

Posted: Tue May 26, 2009 1:02 pm
by Combuster
C functions can't be used as interrupt handler. They don't preserve everything.

Re: Page faults and LEDs

Posted: Tue May 26, 2009 1:08 pm
by AUsername
/facepalm

I found out what was wrong right after I posted this...

I had kkybrd_set_leds() as a _cdecl for some reason, I removed that and it worked perfectly (and the code above I removed it before I actually tested it cause I didn't think it would change anything -.-)


EDIT:
Nevermind, it's back now. :/

Re: Page faults and LEDs

Posted: Tue May 26, 2009 4:32 pm
by Troy Martin
Combuster wrote:C functions can't be used as interrupt handler. They don't preserve everything.

Re: Page faults and LEDs

Posted: Tue May 26, 2009 5:24 pm
by neon
Troy Martin wrote:
Combuster wrote:C functions can't be used as interrupt handler. They don't preserve everything.
They can be if you use it right. ...Although its not recommended in most cases.

Re: Page faults and LEDs

Posted: Wed May 27, 2009 2:32 am
by Combuster
True, if your C compiler supports "void interrupt irq_pit()" then you can do that in C. In all other cases, possible optimisations and prologue/eplilogue code will make it a ever-breaking nightmare.

Re: Page faults and LEDs

Posted: Wed May 27, 2009 12:02 pm
by earlz
Also make sure you don't have something IRQs such as

Code: Select all

_irq_hander:
pusha
....
popa
mov al,0x20
out 0x20,al
iret
took me weeks to figure out why EAX seemed to be getting silently corrupted