keyboard ISR stuck in loop after returning.

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
0xCC
Posts: 15
Joined: Sun Sep 09, 2018 2:07 am

keyboard ISR stuck in loop after returning.

Post by 0xCC »

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: Select all

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: Select all

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?
Octocontrabass
Member
Member
Posts: 5581
Joined: Mon Mar 25, 2013 7:01 pm

Re: keyboard ISR stuck in loop after returning.

Post by Octocontrabass »

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

Re: keyboard ISR stuck in loop after returning.

Post by 0xCC »

Octocontrabass wrote:What code is being interrupted?
after setting up the IDT and PIC the CPU just HLT:

Code: Select all

void kmain() {
	GDT_Setup(GDTBASE);
	IDT_Setup((PVOID)IDTBASE);
	clear();
	printf("Welcome to my new OS");
	SetupIODrivers();//setup PIC
	_asm STI;
	_asm HLT;
}
Octocontrabass
Member
Member
Posts: 5581
Joined: Mon Mar 25, 2013 7:01 pm

Re: keyboard ISR stuck in loop after returning.

Post by Octocontrabass »

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

Re: keyboard ISR stuck in loop after returning.

Post by 0xCC »

Octocontrabass wrote:And what happens after you HLT?
nothing.
isnt HLT supposed to suspend the CPU until the next interrupt ?
Octocontrabass
Member
Member
Posts: 5581
Joined: Mon Mar 25, 2013 7:01 pm

Re: keyboard ISR stuck in loop after returning.

Post by Octocontrabass »

Right, it suspends the CPU until the next interrupt, which is where your keyboard handler runs and returns.

What does it return to?
0xCC
Posts: 15
Joined: Sun Sep 09, 2018 2:07 am

Re: keyboard ISR stuck in loop after returning.

Post by 0xCC »

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: Select all

while (true)
		_asm HLT;
so i guess if HLT is interrupted and returned, the CPU executes what's after HLT right ?
Octocontrabass
Member
Member
Posts: 5581
Joined: Mon Mar 25, 2013 7:01 pm

Re: keyboard ISR stuck in loop after returning.

Post by Octocontrabass »

That's right.
nullplan
Member
Member
Posts: 1798
Joined: Wed Aug 30, 2017 8:24 am

Re: keyboard ISR stuck in loop after returning.

Post by nullplan »

Also:
0xCC wrote:

Code: Select all

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!
0xCC
Posts: 15
Joined: Sun Sep 09, 2018 2:07 am

Re: keyboard ISR stuck in loop after returning.

Post by 0xCC »

nullplan wrote:Also:
0xCC wrote:

Code: Select all

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