OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 11:28 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Cant return from IRQ1?
PostPosted: Wed Feb 14, 2018 1:50 pm 
Offline

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

Top
 Profile  
 
 Post subject: Re: Cant return from IRQ1?
PostPosted: Wed Feb 14, 2018 3:00 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
You might want to look into MSVC's naked function attribute


Top
 Profile  
 
 Post subject: Re: Cant return from IRQ1?
PostPosted: Wed Feb 14, 2018 3:09 pm 
Offline

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


Top
 Profile  
 
 Post subject: Re: Cant return from IRQ1?
PostPosted: Wed Feb 14, 2018 3:38 pm 
Offline

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


Top
 Profile  
 
 Post subject: Re: Cant return from IRQ1?
PostPosted: Wed Feb 14, 2018 5:34 pm 
Offline
Member
Member
User avatar

Joined: Sun Feb 18, 2007 7:28 pm
Posts: 1564
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();}


Top
 Profile  
 
 Post subject: Re: Cant return from IRQ1?
PostPosted: Fri Feb 16, 2018 3:31 pm 
Offline
Member
Member

Joined: Fri Aug 26, 2016 1:41 pm
Posts: 671
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.


Top
 Profile  
 
 Post subject: Re: Cant return from IRQ1?
PostPosted: Mon Mar 05, 2018 6:41 pm 
Offline

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 71 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group