Cant return from IRQ1?

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
SeeSoftware
Posts: 13
Joined: Tue Apr 18, 2017 6:02 pm

Cant return from IRQ1?

Post by SeeSoftware »

Hi, im making a a "os" in c++(visual studio(MSVC++) 2017) and im now trying to get keyboard input and i have this IRQ1 handler:

Code: Select all

void PS2Keyboard::IRQHandler()
{
	DebugConsole& console = Kernel::Get()->GetConsole(); //works

	uint8_t scan_code = __inbyte(0x60); //works

	console.SetCol(0x0F); //works
	console.Printf("Key"); //works
	
	PIC::SendEOI(mCurrentIRQLine); //works
	__asm leave; //problem  (i also tried pop esi)
	__asm iret; //problem
	__halt();
}
i ported the PIC code from https://wiki.osdev.org/PIC so it should be functional (wich it is).
i also Remaped the Pic to 32 (pic1) and 40 (pic2) so it doesnt conflict with the reserved intel interrupts

Everything works as expected(and if i remove the problem lines it works but only once since i dont return and just halt there)
but if i try to return from it with iret it gives me a General Protection Fault.

i looked at the assembly code the compiler gave me and it looks like that:

READ COMMENTS

Code: Select all

push	esi

;skipping uninteresting code
;DebugConsole& console = Kernel::Get()->GetConsole();
;uint8_t scan_code = __inbyte(0x60);
;console.SetCol(0x0F);
;console.Printf("Key");
;PIC::SendEOI(mCurrentIRQLine);

leave  ;insert my custom return code wich might be the problem but if i try to make the function naked
iret     ;wich means that it doesnt have a Function prologue or function epilogue but then im very limmited in what i can do in
hlt      ;that function( i cant even create local variables) it still wont work. I tried software interrupts and they work fine 
pop	esi ;but i have to return with RETF 2 wich is probably a problem too ?? 
ret	0



flags for that interrupt are Present, Ring0 and 32bit InterruptGate (TrapGate wont make a difference??)

does anyone know what might cause this ? (empty function has the same problems)
or if anyone knows a better way to create Interrupt handlers in MSVC++
Last edited by SeeSoftware on Wed Feb 14, 2018 3:09 pm, edited 1 time in total.
MichaelPetch
Member
Member
Posts: 799
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Cant return from IRQ1?

Post by MichaelPetch »

You might want to look into MSVC's naked function attribute
SeeSoftware
Posts: 13
Joined: Tue Apr 18, 2017 6:02 pm

Re: Cant return from IRQ1?

Post by SeeSoftware »

MichaelPetch wrote:You might want to look into MSVC's naked function attribute
well i mentioned that in the assembly code (witch might be hard to notice, sorry) so trying that would result in the same error
SeeSoftware
Posts: 13
Joined: Tue Apr 18, 2017 6:02 pm

Re: Cant return from IRQ1?

Post by SeeSoftware »

oookkk i replaced iret with retf 2 and it works now but Why?? i dont understand. why does iret not work

i want to make sure im not doing anything wrong or if im leaking stack by using retf 2
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Cant return from IRQ1?

Post by neon »

Hello,

You should be using iretd not iret. As noted above, you will either need to use __declspec(naked) or write your own prologue to fix the stack. Alternatively -- this should really be done in assembly language rather then C/inline assembly to avoid dependence on the CL compiler.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
MichaelPetch
Member
Member
Posts: 799
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Cant return from IRQ1?

Post by MichaelPetch »

You say this doesn't work even for a function that does nothing. Can you show us the complete set of assembly instructions generated for a do nothing interrupt handler that doesn't work? Would be helpful if you put all your code on github so we can look at it.

retf 2 is no solution. Just because it my appear to work now probably won't hold true in the future.

PS: An interrupt handler that calls C/C++ code should ensure the forward direction flag is set with a CLD instruction.
SeeSoftware
Posts: 13
Joined: Tue Apr 18, 2017 6:02 pm

Re: Cant return from IRQ1?

Post by SeeSoftware »

MichaelPetch wrote:You say this doesn't work even for a function that does nothing. Can you show us the complete set of assembly instructions generated for a do nothing interrupt handler that doesn't work? Would be helpful if you put all your code on github so we can look at it.

retf 2 is no solution. Just because it my appear to work now probably won't hold true in the future.

PS: An interrupt handler that calls C/C++ code should ensure the forward direction flag is set with a CLD instruction.
i think i allready solved it i had to use iretd like neon told me.
Post Reply