Software Firmware Engineer at L&T Technology Services Limited
Nov. 10, 2016•0 likes•594 views
1 of 20
Beneath the Linux Interrupt handling
Nov. 10, 2016•0 likes•594 views
Download to read offline
Report
Software
-> Deep dive inside the kernel Interrupt management subsystem.
-> Entire presentation is oriented towards 8259 Interrupt controller.
-> Detail understanding of how request_irq() function works.
1. Beneath the Linux Interrupt
Presenter Name: Bhoomil C.
Date: 26th September 2016
2. Agenda
• Introduction
• Interrupt handling in MCU
• Interrupt handling in Linux kernelspace.
• Event handling in Linux userspace.
• Q/A
2
3. Introduction
• What is interrupt or Event in computing system?
The simplest answer is,
CPU Attention to any physical or virtual objects whenever those
objects are in action. Or Forcibly change the current execution.
• Busy POLLING alternative and much more efficient.
• In this presentation we will understand the interrupt
management mechanism in Linux kernelspace by taking an
example of GPIO LKM as well as how we can control GPIO
LKM from userspace.
3
4. Interrupt handling in MCU
• Before we dive into the Linux kernel lets brush-up our original
concept regarding this term “interrupt” in the context of
MCU.
• In most microcontroller, for each interrupt there are
predefined memory locations, we called it as vector table. This
vector table is vendor specific.
4
5. Interrupt handling in MCU (continued…)
MCU’s State changes whenever the interrupt occurred.
• The current Program Counter is saved on the stack.
• Interrupts of the same and lower priority are blocked.
• The corresponding interrupt flag is cleared.
• Program execution transfers to the corresponding interrupt
handler vector address.
• The Interrupt Handler Routine executes.
• After execution, return to paused work and resume it.
5
6. Interrupt handling in MCU (continued…)
• Execution without interrupt • Execution with interrupt
6
Task
exec
Task
exec
ISR
hit
Task exec
Time Time
7. Interrupt handling in Linux kernelspace
• Lets talk about SoC + Linux combination.
• It might be possible that we have more then one hardware
connection with SoC.
• For equal response to each hardware, kernel must have to
communicate with each individual hardware.
• Due to low-speed nature of I/O device as compared to CPU,
the I/O devices sent attention signal to CPU asynchronously.
7
8. Interrupt handling in Linux kernelspace(continued…)
• To reduces the Pinout on SoC or CPU for controlling each I/O
devices, the vendors are use interrupt multiplexer or interrupt
controller(i.e.8259 chip).
8
Programmable Interrupt Controller
9. Interrupt handling in Linux kernelspace(continued…)
• Interrupt Controller will provide unique ID to each I/O device
by assigning a IRQ numbers.
• Each IRQ lines are mapped to interrupt vector location.
• By this way, interrupt from the X I/O device is distinct from Y
and Z I/O devices any many more.
• So async signal from the I/O device will directed via interrupt
controller to the processor with IRQ line number. Upon
receiving interrupt the kernel will stop whatever it doing and
execute the registered interrupt handler.
9
10. Interrupt handling in Linux kernelspace(continued…)
• How to implement I/O devices’ interrupt handler?
• Lets make our custom GPIO LKM and see how the
implementation is look like.
• In this example code for userspace entry point, we will focus
on /sys directory rather then /dev, because it is easy to
understand.
•
10
11. Interrupt handling in Linux kernelspace(continued…)
• What actually request_irq() kernel function (which may sleep)
do internally?
• For the answer of this question we have to understand two
following terms.
Interrupt Setup
Interrupt Handling
• For this two terms all the further code flow is as per the Linux
3.10 release and i8259A PIC as example.
11
12. Interrupt handling in Linux kernelspace(continued…)
• Now return to request_irq() kernel function.
-> request_irq
- -> request_threaded_irq
- - - > action->handler = handler;
12
Our interrupt handler
assignment to irqaction
data structure
13. Interrupt handling in Linux kernelspace(continued…)
Interrupt Setup
• As we know that each and every data structure of kernel will
be initialize after we apply power supply.
• The same in case of interrupt, the related data structure and
code flow will also initialize during the boot time.
• Everything are inside the start_kernel() kernel function, which
is “main” function of kernel itself.
13
14. Interrupt handling in Linux kernelspace(continued…)
arch/x86/kernel/i8259.c Line-110
- > make_8259A_irq
- - > irq_set_chip_and_handler
- - - > irq_set_chip
- - - > __irq_set_handler
- - - - > desc->handle_irq = handle @L668
For i8259 chip the handle object is handle_level_irq() kernel
function.
14
15. Interrupt handling in Linux kernelspace(continued…)
Interrupt Handling
• As we already see in the MCU interrupt handling slide that
there are predefined location in memory for each interrupt
called the vector table.
• The kernel is doing same things, it will jump to corresponding
location at for every interrupt it will call do_IRQ() kernel
function.
15
16. Interrupt handling in Linux kernelspace(continued…)
• arch/x86/kernel/entry_32.S(entry_64.S x64) will call do_IRQ.
• arch/x86/kernel/irq.c will hold the definition for do_IRQ
assembly label.
- > do_IRQ
- - > handle_irq
- - - > desc->handle_irq(irq, desc) @L197
- - - > handle_level_irq
- - - - > handle_irq_event
- - - - -> handle_irq_event_percpu
- - - - - - > action->handler(irq, action->dev_id);
16
Final call to our
interrupt handler
by kernel
do_IRQ() will fetch the IRQ
lines number from the
register.
17. Event handling in userspace
• In userspace we do not have to deal with such low-level
operation.
• In certain C/C++ applications, the ultimate goal is to provide
notifications system to end users.
• The term “Event handling” is so vast. Full explanation is
beyond the scope of this presentation.
• There are various signalling mechanism and producer-
consumer models for event handling in Linux.
• Let’s take the GPIO example which we had done in our Intel
Sunset Pass Module.
17
18. Event handling in userspace
18
LLAPI invocation
QT GUI
thread
Test case thread
as consumer
GPIO monitor
thread as
producer
Using poll()
to monitor
the fds
Synchronization
is done by using
conditional
variable
callback will
used to retrieve
data