Transferred from: http://www.cnblogs.com/wang_yb/archive/2013/04/19/3030345.html
Interrupt processing is generally not pure software to achieve, requires hardware support. Learning from interruptions helps to gain a deeper understanding of some of the underlying principles of the system, especially the development of drivers.
Main content:
- What is an interrupt
- Interrupt type
- Interrupt Correlation function
- Interrupt handling mechanism
- Interrupt Control method
- Summarize
1. What is interruption
In order to improve the performance of the CPU and peripheral hardware (hard disk, keyboard, mouse, etc.), the interrupt mechanism is introduced.
Without interruption, the CPU and peripherals may only be polled by polling this method: The CPU periodically checks the hardware status, processes it when it needs to be processed, or skips.
When the hardware is busy, the CPU is likely to do a lot of useless work (each polling is skipped and not processed).
The interrupt mechanism is a mechanism by which the hardware signals to the CPU when needed, and the CPU temporarily stops the work in progress to handle the hardware request.
2. Interrupt Type
Interrupts are generally divided into asynchronous interrupts (typically caused by hardware) and synchronous interrupts (typically caused by the processor itself).
Asynchronous interrupts: The CPU is processing interrupts for too long, so the hardware is reset so that the hardware can continue its work and then handle the time-consuming part of the interrupt request at the appropriate time.
For example: How the NIC Works
- After the network card receives the packet, it sends the interrupt signal to the CPU, requests to process the received packet
- After the CPU copies the received packets to memory, it notifies the NIC to continue working
- The process of copying the packets to memory will be done at the appropriate time.
Doing so avoids processing packets that are too long to cause the NIC to receive packets at a slower rate.
Sync interrupts: The CPU does not feed back the hardware until all the work on the interrupt request is processed
For example: System exception handling (for example, except for 0 operations in operations)
- After an application exception occurs, the kernel is required to handle
- The kernel calls the appropriate exception handler to handle the exception
- End-of-process application or message
A synchronous interrupt should handle an interruption that can be completed very quickly.
3. Interrupt Correlation function
To implement an interrupt, it is important to know 3 functions:
- function to register interrupts
- function to release interrupts
- Declaration of the interrupt handler
3.1 Functions for registering interrupts
Location:<linux/interrupt.h> include/linux/interrupt.h
Defined as follows:
/* * IRG -Indicates the interrupt number to be assigned * Handler-the actual interrupt handler * Flags -Flag bit, indicating that this interrupt has an attribute * name -an ASCII representation of the interrupt device name, which will be/PROC/IRQ and/PR oc/interrupts file using * Dev -for shared disconnect, multiple interrupt programs sharing one interrupt line (with one interrupt number), relying on Dev to differentiate individual interrupts * return value: * Execution succeeded: 0 * Execution failed: 0 */int REQUEST_IRQ ( unsigned int IRQ, irq_handler_t handler, unsigned long flags, const char* name, void *dev)
3.2 Releasing the interrupt function
The definition is relatively simple:
void Free_irq (unsigned int irq, void *dev)
If it is not a wire break in the share, delete the interrupt corresponding to the IRQ directly.
If the disconnection is in a share, determine if this interrupt handler breaks the last interrupt handler on the line,
Is the last interrupt handler, remove the interrupt line and interrupt handler
Remove interrupt handler, not the last interrupt handler
3.3 Declaration of the interrupt handler
The declaration format is as follows:
/ * * Interrupt Handler declaration * @irp -Interrupt handler (that is, handler in REQUEST_IRQ ()) Associated break number * @dev -like Dev in Request_irq (), represents the structure of a device * return value: * irqreturn_t- execution succeeded: irq_handled execution failed: Irq_none */static irqreturn_t intr_handler (int, IRQ, void *dev)
4. Interrupt handling mechanism
The process of interrupt processing mainly involves 3 functions:
- DO_IRQ is architecture-related and responds to received interrupts
- Handle_irq_event all interrupt processing on the interrupt line is called
- RET_FROM_INTR Recovery Register to restore the kernel to its pre-outage state
The process flow can be found in the diagram in the book, as follows:
5. Interrupt Control method
The commonly used interrupt control methods are shown in the following table:
Function |
Description |
Local_irq_disable () |
Prohibit local interrupt delivery |
Local_irq_enable () |
Activating local interrupt Delivery |
Local_irq_save () |
Saves the current state of local interrupt delivery and then disables local interrupt delivery |
Local_irq_restore () |
Resume local interrupt delivery to a given state |
DISABLE_IRQ () |
Disables the given medium break and ensures that the function returns before the handler is running on the interrupt line |
Disable_irq_nosync () |
Prohibit a given medium break |
ENABLE_IRQ () |
Activates a given medium break |
Irqs_disabled () |
Returns non 0 if local interrupt delivery is disabled, otherwise 0 |
In_interrupt () |
Returns non 0 if in the context of the interrupt, or 0 if in the context of the process |
IN_IRQ () |
Returns non 0 if the interrupt handler is currently executing, otherwise 0 |
Summarize
Interrupt processing requires a high processing time, and if an outage takes a long time, interrupt processing is generally divided into 2 parts.
The upper part only does some necessary work, immediately notifies the hardware to continue its work.
The time-consuming part of interrupt processing, which is the second half of the work, the CPU will be done at the appropriate time.
"Linux kernel design and implementation" Reading notes (vii)-Interrupt processing "Go"