Page faults and LEDs

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
AUsername
Member
Member
Posts: 54
Joined: Sun Feb 01, 2009 9:07 pm

Page faults and LEDs

Post 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)
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Page faults and LEDs

Post by Combuster »

C functions can't be used as interrupt handler. They don't preserve everything.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
AUsername
Member
Member
Posts: 54
Joined: Sun Feb 01, 2009 9:07 pm

Re: Page faults and LEDs

Post 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. :/
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Page faults and LEDs

Post by Troy Martin »

Combuster wrote:C functions can't be used as interrupt handler. They don't preserve everything.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Page faults and LEDs

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Page faults and LEDs

Post 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.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: Page faults and LEDs

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