### Lecture 04 - Interrupt processing
##### Terms and classification
**Pooling & Interrupts** - are 2 methods for handling unforeseen events such as message from IO or an unpredicted Error. Pooling is busy waiting the occurence always while Interrupts are raised.
**Polling**
```javscript
while() {
for(all devices) { // CPU interacts with many devices
if(device[i].readyBit() == 1) { // bit 1 means device is ready
doAction();
}
}
}
```
**Interrupt** = Interruption
it is different from polling since there is no active pooling of event sources, it breaks usually flow of work with more important work there are few interrupt reasons like:
- Operating system conditions
- Asynchronous events
They could come from either software or hardware and can be **shutdown** or masked but it should be for a very short time, since sometimes is necessary to finish certain action
**Synchronous vs Asynchronous**
Sync interrupts are CPU-thrown exception or OS service calls which are intended for the running program. Async interrupts occur regardless of what the system is currently executing, an example would be: *Network adapter reports incoming message* or *Disk storage reports delivery of a block*
**Interrupt classification**
![[Pasted image 20240217090957.png]]
##### Interrupt interaction with components
Interrupts cause code execution outside of the normal program flow, their control is passed to a defined location in kernel called **Interrupt Service Routine ISR**
![[Pasted image 20240217091203.png]]
Interrupt processing in instruction cycle has few stages in it's instruction cycle, where main point is: checking whether an interrupt is present, which is checked at end of a machine command. There we can differentiate between:
- **Interrupt level** - which is interrupt priority
- **Interrupt masking** - which is disabling interrupts, happening in interrupt mask register (IMR)
- **Non-Maskable Interrupt (NMI)** - which can't be masked and serves for very critical events
- ![[Pasted image 20240217092503.png|inlR|350]]-
**Interrupt Vector Table** is a table of device indexes which is managed by CPU, first interrupt request (IRQ) is sent by the device to interrupt controller, which then get's device Int-No. IVT structure is specified by processor
![[Pasted image 20240217092825.png]]
An **Interrupt Service Routine (ISR)** handles I/O devices by notify the CPU when they require a service, it allows the CPU to be used to its maximum capacity because it can do something else in parallel with the I/O
![[Pasted image 20240217092949.png]]
**Interrupt Processing Flow**
![[Pasted image 20240217093153.png]]
##### Case Examples: Intel 8259A controller
Intel 8259A PIC - Programmable Interrupt controller - is multiplexer for hardware interrupts, it has an integrated circuit for managing multiple hardware interrupts, it's task is to monitor interrupt lines (IRQ) and stores them and passes them to the CPU. It has 8 types of interrupt inputs IRQ0 to IRQ7 were lower the number the higher the priority. It is used in IBM PC XT and usually two PICs were often used in one PC. Newer version
- APIC = Advanced PIC has 256 interrupt inputs and is used in x86 architectures and supports multiprocessor system.
- SAPIC = Streamlines Advanced PIC is used in an x64 architectures
![[Pasted image 20240217093639.png]]
**Controller simplified**![[Pasted image 20240217093654.png]]
In multi-core processor, dispatching algorithm for interrupts dispatcher selects a core to handle the interrupt
![[Pasted image 20240217093818.png]]
##### Case Example Windows Interrupt handling
Windows has its own interrupt management, with so called **Interrupt Request Level (IRQL)** kernel assigns its own priorities to the interrupts, and only interrupts with higher IRQL can interrupt interrupt processing on lower IRQL, it also uses **Interrupt Dispatch Table (IDT)** to assign specific ISR to a IRQL. In x86 bit architecture Interrupt Request Levels are sorted as
| IRQL | Designation | Description |
| -- | --- | --- |
| 31 | Higher level | Machine check and catastrophic errors |
| 30 | Power level | Current/voltage problem |
| 29 | IPI level | interprocessor interrupt |
| 28 | Clock level | clock interrupt |
| 3- 26 | device levels | mapping to IRQs of the devices depending on the installed interrupt controller |
| 2 | Dispatch/DPC level | dispatching and executing deferred procedure calls |
| 1 | APC level | Execution of async procedures calls after I/O requests |
| 0 | Passive level | Normal thread execution |
In x64 bit architectures IRQLs are defined as so
| IRQL | Designation | Description |
| ---- | ---- | ---- |
| 15 | High level | Machine check and catastrophic errors |
| 14 | Power level | Current/Voltage problem and interprocessor interrupt |
| 13 | Clock level | Clock interrupt |
| 12 | Synch level | Cross-processor synchronisation |
| 3-11 | Device levels | Mapping to IRQs of the devices depending on the installed interrupt controller |
| 2 | Dispatch/DPC level | Dispatching and executing deferred procedure calls |
| 1 | APC level | Execution of asynchronous procedure calls |
| 0 | Passive level | Normal thread execution |
**DPC** - Deferred (delayed) Procedure call
![[Pasted image 20240217095105.png]]
**Windows Interrupt handling**, in x86 starts with:
- Interrupt controller which is usually 8259 chip interrupts the CPU on one hardware line, then
- CPU polls the controller to receive an interrupt request IRQ on hardware line
- Interrupt controller translates IRQ into interrupt number
- IRQL is then determined from the interrupt number
- Interrupt is then matched on IDT where address of interrupt is found and distribution routing can be triggered
- Interrupt distribution routine is called and determine the address of ISR
- ISR is called which produces DPC(Deferred Procedure Call) object which is added to DPC queue
- DPC routine is than called later with lower priority and can be interrupted
**Asynchronous Procedure Call (APC)** - application scenario would be:
- When read calls are made to a hard disc and thread processing is initially interrupted, reading is finished by kernel then APC object is generated and added to the APC queue of the calling thread.
- This result in subsequent processing of the APC queue which takes place in IRQL 1
- One APC queue per thread is assigned in kernel and user mode, depending on where the read call was made from
##### Case Example Linux
Linux uses a table with references to the interrupt handlers (ISR) and each interrupt request is mapped to an interrupt number or indexed into table. Tasklets are mechanisms that are used to quickly handle interrupt which is similar to Windows DPC mechanism
##### System Service and System Calls
**Operating system services** - are application programs which use the services of the operating system which are called via system calls, they are well-defined entry points into the operating system, they are called via special mechanism which are:
- **Software interrupt** a.k.a **Trap**
- **Supervisor Call (SVC)**
Pros of system service calls are that program doesn't need to know the addresses of the system routines, all system calls together form the interface of the application programs to the operating system kernel. Access to system calls is usually provided in libraries.
![[Pasted image 20240217102747.png]]
System calls can be also made in Kernel Mode, when they are called the processor switches from user mode to kernel mode.
![[Pasted image 20240217102617.png]]
**System call flow**
![[Pasted image 20240217102720.png]]
**Comparable instructions in other processors** are:
- **EPC**-Command - which results in entering in privilege mode in x64 systems
- **syscall** command in x64 - is transfer of the system call number in the EAX register
- **sysenter** - command in x86 Pentium 2
- **SVC** instruction are used in ARM architecture
In **POSTIX** (Portable Operating System Unix) system calls are standardised in ISO 9945-1, its conformance is met by most Unix declaratives for example:
- fork() - process creating
- execve() - calling a program
- exit() - end process
- open() - open file
- close() - close file
- read() - read data from file
- write() - write data to file
In **Windows** system calls defined in Win32 API which contain toustands of API function some of which are system calls like:
- CreateProcess()
- exitProcess()
- CreateFile()
- CloseHandle() - closes file
- ReadFile()
- WriteFile()
![[Pasted image 20240217103328.png]]