OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Mar 29, 2024 8:20 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: keyboard ISR stuck in loop after returning.
PostPosted: Tue Oct 15, 2019 3:40 am 
Offline

Joined: Sun Sep 09, 2018 2:07 am
Posts: 15
hello
I managed to implement the keyboard ISR and i prints the keys entered but the problem is the CPU usage goes to high levels after the first key entered.
here is the ISR:
Code:
void _declspec(naked) keyboard_interrupt_wrapper() {
   _asm {
      CLI;
      PUSHAD;
      CALL IO_keyboard_handler;
      POPAD;
      STI;
      IRETD;
   }
}

but the problem is solved when i added HLT before IRETD
Code:
void _declspec(naked) keyboard_interrupt_wrapper() {
   _asm {
      CLI;
      PUSHAD;
      CALL IO_keyboard_handler;
      POPAD;
      STI;
      HLT;
      IRETD;
   }
}

so i guess the problem is from IRETD, maybe i haven't setup stack properly and execution went to random place?


Top
 Profile  
 
 Post subject: Re: keyboard ISR stuck in loop after returning.
PostPosted: Tue Oct 15, 2019 3:47 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5103
What code is being interrupted?


Top
 Profile  
 
 Post subject: Re: keyboard ISR stuck in loop after returning.
PostPosted: Tue Oct 15, 2019 3:49 am 
Offline

Joined: Sun Sep 09, 2018 2:07 am
Posts: 15
Octocontrabass wrote:
What code is being interrupted?

after setting up the IDT and PIC the CPU just HLT:
Code:
void kmain() {
   GDT_Setup(GDTBASE);
   IDT_Setup((PVOID)IDTBASE);
   clear();
   printf("Welcome to my new OS");
   SetupIODrivers();//setup PIC
   _asm STI;
   _asm HLT;
}


Top
 Profile  
 
 Post subject: Re: keyboard ISR stuck in loop after returning.
PostPosted: Tue Oct 15, 2019 3:57 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5103
And what happens after you HLT?


Top
 Profile  
 
 Post subject: Re: keyboard ISR stuck in loop after returning.
PostPosted: Tue Oct 15, 2019 4:14 am 
Offline

Joined: Sun Sep 09, 2018 2:07 am
Posts: 15
Octocontrabass wrote:
And what happens after you HLT?

nothing.
isnt HLT supposed to suspend the CPU until the next interrupt ?


Top
 Profile  
 
 Post subject: Re: keyboard ISR stuck in loop after returning.
PostPosted: Tue Oct 15, 2019 4:34 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5103
Right, it suspends the CPU until the next interrupt, which is where your keyboard handler runs and returns.

What does it return to?


Top
 Profile  
 
 Post subject: Re: keyboard ISR stuck in loop after returning.
PostPosted: Tue Oct 15, 2019 4:52 am 
Offline

Joined: Sun Sep 09, 2018 2:07 am
Posts: 15
Octocontrabass wrote:
Right, it suspends the CPU until the next interrupt, which is where your keyboard handler runs and returns.

What does it return to?

i thought it continues the execution of HLT and suspend CPU again.
but when i replaced it by a loop it solved the problem.
Code:
while (true)
      _asm HLT;


so i guess if HLT is interrupted and returned, the CPU executes what's after HLT right ?


Top
 Profile  
 
 Post subject: Re: keyboard ISR stuck in loop after returning.
PostPosted: Tue Oct 15, 2019 4:56 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5103
That's right.


Top
 Profile  
 
 Post subject: Re: keyboard ISR stuck in loop after returning.
PostPosted: Tue Oct 15, 2019 8:14 am 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
Also:
0xCC wrote:
Code:
void _declspec(naked) keyboard_interrupt_wrapper() {
   _asm {
      CLI;
      PUSHAD;
      CALL IO_keyboard_handler;
      POPAD;
      STI;
      IRETD;
   }
}


If you set up your interrupt handler as an interrupt gate in the IDT, rather than a trap gate (look it up!), you don't need the CLI, the CPU will automatically delete the IF for you. And the STI before the IRET is useless anyway, since IRET will pop the flags from stack.

_________________
Carpe diem!


Top
 Profile  
 
 Post subject: Re: keyboard ISR stuck in loop after returning.
PostPosted: Wed Oct 16, 2019 5:26 am 
Offline

Joined: Sun Sep 09, 2018 2:07 am
Posts: 15
nullplan wrote:
Also:
0xCC wrote:
Code:
void _declspec(naked) keyboard_interrupt_wrapper() {
   _asm {
      CLI;
      PUSHAD;
      CALL IO_keyboard_handler;
      POPAD;
      STI;
      IRETD;
   }
}


If you set up your interrupt handler as an interrupt gate in the IDT, rather than a trap gate (look it up!), you don't need the CLI, the CPU will automatically delete the IF for you. And the STI before the IRET is useless anyway, since IRET will pop the flags from stack.

indeed i had set it to interrupt gate.
thank you both for the info.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 211 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