beneath the linux interrupt handling

20
Beneath the Linux Interrupt Presenter Name: Bhoomil C. Date: 26 th September 2016

Upload: bhoomil-chavda

Post on 13-Apr-2017

60 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Beneath the Linux Interrupt handling

Beneath the Linux Interrupt

Presenter Name: Bhoomil C.Date: 26th September 2016

Page 2: Beneath the Linux Interrupt handling

Agenda• Introduction• Interrupt handling in MCU• Interrupt handling in Linux kernelspace.• Event handling in Linux userspace.• Q/A

Page 3: Beneath the Linux Interrupt handling

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.

Page 4: Beneath the Linux Interrupt handling

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.

Page 5: Beneath the Linux Interrupt handling

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.

Page 6: Beneath the Linux Interrupt handling

Interrupt handling in MCU (continued…)

• Execution without interrupt • Execution with interrupt

Task exec

Taskexec

ISRhit

Task exec

Time Time

Page 7: Beneath the Linux Interrupt handling

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.

Page 8: Beneath the Linux Interrupt handling

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).

Programmable Interrupt Controller

Page 9: Beneath the Linux Interrupt handling

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.

Page 10: Beneath the Linux Interrupt handling

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.

• interrupt_handler_sample.c

Page 11: Beneath the Linux Interrupt handling

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.

Page 12: Beneath the Linux Interrupt handling

Interrupt handling in Linux kernelspace(continued…)• Now return to request_irq() kernel function.-> request_irq- -> request_threaded_irq- - - > action->handler = handler; Our interrupt handler

assignment to irqaction data structure

Page 13: Beneath the Linux Interrupt handling

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.

Page 14: Beneath the Linux Interrupt handling

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 @L668For i8259 chip the handle object is handle_level_irq() kernel function.

Page 15: Beneath the Linux Interrupt handling

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.

Page 16: Beneath the Linux Interrupt handling

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);

Final call to our interrupt handler

by kernel

do_IRQ() will fetch the IRQ lines number from the

register.

Page 17: Beneath the Linux Interrupt handling

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.

Page 18: Beneath the Linux Interrupt handling

Event handling in userspace

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

Page 19: Beneath the Linux Interrupt handling

Q & A

Page 20: Beneath the Linux Interrupt handling

Name : Bhoomil C.Email ID : [email protected] Number : +91 9825511802