Making use of interrupts
-
- Member
- Posts: 91
- Joined: Mon Apr 20, 2020 11:02 am
Making use of interrupts
I recently implemented interrupts(at least in a basic form) in my hobby OS, but don't know exactly how to make use of it. I currently have a 8254 IRQ handler set up, which for now is just a counter to make sure it's working. My plan is to connect it with existing timer function to make a clock.
My OS is obviously a monotasking system which is actually just a huge C program that runs without std library. I want to implement an interrupt based keyboard driver in it, by how to do that efficiently than polling? My initial plan was to check for the value in register until it is placed by ISR, but that is just polling, in an other way, I think. Or should I do something like use hlt to halt CPU mid function and then wake up by keyboard IRQ and then retrieve the value?
Also, is there any other important use for int on my OS? All it has a interface, an editor and an interpreter, which is actually just one C program with function of each task(I wonder with this design is it even an OS? )
My OS is obviously a monotasking system which is actually just a huge C program that runs without std library. I want to implement an interrupt based keyboard driver in it, by how to do that efficiently than polling? My initial plan was to check for the value in register until it is placed by ISR, but that is just polling, in an other way, I think. Or should I do something like use hlt to halt CPU mid function and then wake up by keyboard IRQ and then retrieve the value?
Also, is there any other important use for int on my OS? All it has a interface, an editor and an interpreter, which is actually just one C program with function of each task(I wonder with this design is it even an OS? )
A beginner developer/student. Likes to know stuff. Don't have an OS to put here.
-
- Posts: 8
- Joined: Fri Jan 31, 2020 7:28 pm
Re: Making use of interrupts
. Or should I do something like use hlt to halt CPU mid function and then wake up by keyboard IRQ and then retrieve the value?
Yes, that's exactly what you usually do. When you're done initializing your OS, you put the CPU in an infinite loop of:
Code: Select all
kernel_halt:
hlt ; Wait for interrupt
jmp kernel_halt
This is better than having the kernel poll forever, because it saves power and heat, and you won't hear your computer fan whirring crazily.
Re: Making use of interrupts
The interrupt can just fill a buffer with whatever it receives, your program can check the buffer whenever it's ready, and then act on what it finds, that way your program can do whatever it does, and won't miss any keyboard input.pranavappu007 wrote:I recently implemented interrupts(at least in a basic form) in my hobby OS, but don't know exactly how to make use of it. I currently have a 8254 IRQ handler set up, which for now is just a counter to make sure it's working. My plan is to connect it with existing timer function to make a clock.
My OS is obviously a monotasking system which is actually just a huge C program that runs without std library. I want to implement an interrupt based keyboard driver in it, by how to do that efficiently than polling? My initial plan was to check for the value in register until it is placed by ISR, but that is just polling, in an other way, I think. Or should I do something like use hlt to halt CPU mid function and then wake up by keyboard IRQ and then retrieve the value?
Also, is there any other important use for int on my OS? All it has a interface, an editor and an interpreter, which is actually just one C program with function of each task(I wonder with this design is it even an OS? )
An operating system is both a resource manager and an interface (between different things), it allows the user to use the hardware more effectively. If your software does that then I would call it an operating system.
CuriOS: A single address space GUI based operating system built upon a fairly pure Microkernel/Nanokernel. Download latest bootable x86 Disk Image: https://github.com/h5n1xp/CuriOS/blob/main/disk.img.zip
Discord:https://discord.gg/zn2vV2Su
Discord:https://discord.gg/zn2vV2Su
-
- Member
- Posts: 5572
- Joined: Mon Mar 25, 2013 7:01 pm
Re: Making use of interrupts
More efficient by what measure? Your OS has no other work for the CPU while it's waiting for keyboard input.pranavappu007 wrote:I want to implement an interrupt based keyboard driver in it, by how to do that efficiently than polling?
Using HLT would make it more energy efficient, since the CPU will reduce its power consumption while it's halted. Just remember that the CPU will also wake up for things that are not the keyboard IRQ.pranavappu007 wrote:Or should I do something like use hlt to halt CPU mid function and then wake up by keyboard IRQ and then retrieve the value?
Most hardware is designed to use interrupts so that a multitasking OS can perform useful work while waiting for the hardware. You may need to use interrupts with some hardware even though your OS has nothing to do while it waits.pranavappu007 wrote:Also, is there any other important use for int on my OS?
-
- Member
- Posts: 91
- Joined: Mon Apr 20, 2020 11:02 am
Re: Making use of interrupts
I am thinking of something likeOctocontrabass wrote: Using HLT would make it more energy efficient, since the CPU will reduce its power consumption while it's halted. Just remember that the CPU will also wake up for things that are not the keyboard IRQ.
Code: Select all
loop:
hlt
mov eax, [buffer_stat] ;modified by keyboard ISR to indicate new key received
cmp eax, 0
je loop
;code to read from buffer
A beginner developer/student. Likes to know stuff. Don't have an OS to put here.
Re: Making use of interrupts
Doing complex operations in ISRs is not a great idea, because an interrupt can get trapped while you're in the middle of handling another interrupt; so any functions you call while interrupted have to be reentrant. So yes, something like that would be a good idea.pranavappu007 wrote:Code: Select all
loop: hlt mov eax, [buffer_stat] ;modified by keyboard ISR to indicate new key received cmp eax, 0 je loop ;code to read from buffer
No, you should have a proper event queue. It doesn't matter that it's monotasking. Remember, an interrupt can trigger at any time. So, say you're performing a complex computation. The user may press two keys before you get a chance to handle either, and then the first one will be eaten.pranavappu007 wrote:Also because it is only monotasking I think I don't need a buffer, but just retrieve current value if needed or discard if not needed. In that cases buffer_stat itself become the scan code to read.
Just a simple ring buffer should do it; nothing special. Then you can do something like:
Code: Select all
while (true) {
Event event;
while (read_event(&event)) handle_event(event);
__asm__ volatile("hlt");
}
-
- Member
- Posts: 797
- Joined: Fri Aug 26, 2016 1:41 pm
- Libera.chat IRC: mpetch
Re: Making use of interrupts
I may be wrong but my interpretation of what the OP meant was this code would not be in the ISR but would be in the main loop of the OS. I agree with a ring buffer as well. I had written a Stackoverflow answer that does such a lockless ring buffer (it is real mode code but the idea is the same in protected/llong mode) and a main loop waiting with HLT in a loop to query the keyboard buffer for a scan code with some sample code: https://stackoverflow.com/a/51565739/3857942moonchild wrote:Doing complex operations in ISRs is not a great idea, because an interrupt can get trapped while you're in the middle of handling another interrupt; so any functions you call while interrupted have to be reentrant. So yes, something like that would be a good idea.pranavappu007 wrote:Code: Select all
loop: hlt mov eax, [buffer_stat] ;modified by keyboard ISR to indicate new key received cmp eax, 0 je loop ;code to read from buffer
[snip]
Just a simple ring buffer should do it; nothing special.
Re: Making use of interrupts
No, a buffer is even more important in a mono tasking OS, because the user may press the Keyboard many times before your main loop is able to receive and process the value sent by the keyboard. Without buffering the input values, all but the last value received will be lost.pranavappu007 wrote:
Also because it is only monotasking I think I don't need a buffer, but just retrieve current value if needed or discard if not needed. In that cases buffer_stat itself become the scan code to read.
I use a small (32byte) ring buffer in my keyboard handler, the interrupt just stores the received value in the next available byte position.
All your keyboard handling code needs to do is check the delta between its last read position and the current position of the index in the buffer, and then deal with the byte stream accordingly.
CuriOS: A single address space GUI based operating system built upon a fairly pure Microkernel/Nanokernel. Download latest bootable x86 Disk Image: https://github.com/h5n1xp/CuriOS/blob/main/disk.img.zip
Discord:https://discord.gg/zn2vV2Su
Discord:https://discord.gg/zn2vV2Su
Re: Making use of interrupts
There's a 15-byte buffer in PC keyboard hardware. However, in my DOS days, I did on very rare occasions type fast enough to fill it. (I've more commonly filled it when the PC locks up.)
Kaph — a modular OS intended to be easy and fun to administer and code for.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie