0
votes

I'm currently learning the workings of an operating system and would like to verify if my knowledge is correct on the steps taken during an interrupt. Trying to relate to something very familiar, here's what I think would happen when I press Alt+Tab to switch programs:

  • An interrupt is generated by the hardware (keyboard): intr flag of the CPU is set via system bus from keyboard controller
  • CPU saves the current process state to the PCB to pass control to the interrupt (is kernel mode entered here?)
  • CPU reads interrupt to index the interrupt service routine via the interrupt vector stored in memory
  • The interrupt service routine (along with the interrupt details such as which keys got pressed) is processed (at which point I assume user sees the program being switched)
  • The interrupt is complete (mode bit set back to 1 indicating user mode now?), PCB of the interrupted process gets restored and resumes running.

Are there steps that I'm missing or did not describe correctly?

1

1 Answers

1
votes

There are many many factors here that you need to take in to consideration. For example:

 - Is the keyboard on the ISA bus and is of an PC/AT style keyboard?  
 - If so, is the PIC (programmable Interrupt Controller) involved?  
 - Is the keyboard a USB keyboard device?
 - Is the interrupt an older style PIC, newer style APIC, or a latest style MSI interrupt?

There are many things to consider. However, to try to explain what probably happens, I will assume you have an AT style keyboard attached to the keyboard controller port (PS2 style maybe), you have an older style PIC, and a simplified single- or multi-tasking system.

What could happen when the user presses the Alt key and then the Tab key:

 - The CPU is interrupted (hence the name) by the PIC
 - The CPU "jumps" to the interrupt request routine (IVT, exception, whatever)
 - The routine reads from the keyboard.  A single byte is read which happens 
   to be (part of) the make code for the alt key.
 - the routine then places this make code in some sort of input buffer the
   operating system has set up for it, whether it be part of the OS or part
   of the keyboard driver.
 - the interrupt is acknowledged via the PIC (this step can and usually is
   before the previous step)
 - the routine gives up the CPU (iret instruction)

The process is repeated three more times (assuming a make code and a break code are single byte codes. They can be multiple-byte codes). An interrupt is created on a make and a break key. A make key is when a key is pressed. A break key is when a key is released. i.e:

- make (Alt Down)
- make (Tab Down)
- break (Tab Up)
- break (Alt Up)

You see, there are four interrupts that take place (again assuming single-byte codes), four times the interrupt routine is called, and four times the CPU is interrupted to handle the key press(es) and release(es).

As for the task switching, via an ALT-TAB key combination, this is usually not done within the interrupt handler. The OS will see the Make (or Break) key of the Tab, it will check the status of the shift state (which should include the Alt key) and go from there.

It the keyboard is a USB style keyboard, then you have a totally different process of events. The USB keyboard does not interrupt the CPU as shown above. The keyboard has an Interrupt Pipe that periodically checks to see if there is a key make or break sequence. The OS will give up a time slice to the USB driver, just enough time to check this Interrupt Pipe. However, please note that an Interrupt (PIC, MSI, or other) is not triggered for USB keyboards, unless the USB driver specifies an interrupt should happen at end of frame. Note though that this interrupt is not fired because of the key press (or release), it is triggered because of the end of frame of the USB has taken place.

There is a lot that takes place because of a simple key press and release. I would study on what style of keyboard you wish to program for, what style of interrupt controller, CPU, etc., and go from there.

If you wish, I wrote a series of books on this subject where I explain what happens for both an ISA style keyboard and a USB style keyboard. It has been a very enjoyable hobby and I hope you find the same joy in yours.