Advanced Operating Systems #13

Similar documents
Kernel Modules. Kartik Gopalan

CS 423 Operating System Design: Introduction to Linux Kernel Programming (MP1 Q&A)

Linux Kernel Module Programming. Tushar B. Kute,

Abstraction via the OS. Device Drivers. Software Layers. Device Drivers. Types of Devices. Mechanism vs. Policy. Jonathan Misurda

Software Layers. Device Drivers 4/15/2013. User

Kernel Module Programming

Kernel. Kernel = computer program that connects the user applications to the system hardware Handles:

Developing Real-Time Applications

Loadable Kernel Modules

7.3 Simplest module for embedded Linux drivers

Linux Device Drivers Interrupt Requests

Hardware OS & OS- Application interface

Toolbox Kernel Korner

Linux drivers - Exercise

Last class: Today: Course administration OS definition, some history. Background on Computer Architecture

Process Scheduling Queues

Interrupt Handler: Top Half. Changwoo Min

CS 5460/6460 Operating Systems

Concurrent programming: Introduction I

Distribution Kernel Security Hardening with ftrace

Computer System Overview OPERATING SYSTEM TOP-LEVEL COMPONENTS. Simplified view: Operating Systems. Slide 1. Slide /S2. Slide 2.

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Operating Systems. Operating System Structure. Lecture 2 Michael O Boyle

Anne Bracy CS 3410 Computer Science Cornell University

Pebbles Kernel Specification September 26, 2004

An Implementation Of Multiprocessor Linux

CS 326: Operating Systems. Process Execution. Lecture 5

Background: Operating Systems

- Knowledge of basic computer architecture and organization, ECE 445

The Kernel Abstraction

Device Drivers. CS449 Fall 2017

Table of Figures Figure 1. High resolution PWM based DAC...2 Figure 2. Connecting the high resolution buck converter...8

The Kernel Abstraction. Chapter 2 OSPP Part I

Outline. 1) Introduction to Linux Kernel 2) How system calls work 3) Kernel-space programming 4) Networking in kernel 2/34

CSC369 Lecture 2. Larry Zhang

ECEN 449: Microprocessor System Design Department of Electrical and Computer Engineering Texas A&M University

Overview. This Lecture. Interrupts and exceptions Source: ULK ch 4, ELDD ch1, ch2 & ch4. COSC440 Lecture 3: Interrupts 1

Unit 2 : Computer and Operating System Structure

I/O Systems. Amir H. Payberah. Amirkabir University of Technology (Tehran Polytechnic)

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 09, SPRING 2013

TCSS 422: OPERATING SYSTEMS

Computer System Overview

Tolerating Malicious Drivers in Linux. Silas Boyd-Wickizer and Nickolai Zeldovich

Interrupt Handler: Bottom Half. Changwoo Min

Spring 2017 :: CSE 506. Device Programming. Nima Honarmand

Universidad Carlos III de Madrid Computer Science and Engineering Department Operating Systems Course

CSC369 Lecture 2. Larry Zhang, September 21, 2015

Process & Thread Management II. Queues. Sleep() and Sleep Queues CIS 657

Process & Thread Management II CIS 657

Review: Hardware user/kernel boundary

Operating System Structure

Operating System Structure

Last 2 Classes: Introduction to Operating Systems & C++ tutorial. Today: OS and Computer Architecture

Asynchronous Events on Linux

CSCE Operating Systems Interrupts, Exceptions, and Signals. Qiang Zeng, Ph.D. Fall 2018

Operating Systems. V. Input / Output

Interrupts and Time. Real-Time Systems, Lecture 5. Martina Maggio 28 January Lund University, Department of Automatic Control

Today: Computer System Overview (Stallings, chapter ) Next: Operating System Overview (Stallings, chapter ,

Operating Systems, Assignment 2 Threads and Synchronization

Virtual File System (VFS) Implementation in Linux. Tushar B. Kute,

PCI Bus & Interrupts

AC OB S. Multi-threaded FW framework (OS) for embedded ARM systems Torsten Jaekel, June 2014

Interrupts and Time. Interrupts. Content. Real-Time Systems, Lecture 5. External Communication. Interrupts. Interrupts

Anne Bracy CS 3410 Computer Science Cornell University

ECE 574 Cluster Computing Lecture 8

EECS 3221 Operating System Fundamentals

EECS 3221 Operating System Fundamentals

CS 134. Operating Systems. April 8, 2013 Lecture 20. Input/Output. Instructor: Neil Rhodes. Monday, April 7, 14

Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

ECEN 449 Microprocessor System Design. Hardware-Software Communication. Texas A&M University

Newbie s Guide to AVR Interrupts

Introduction. CS3026 Operating Systems Lecture 01

Operating Systems II BS degree in Computer Engineering Sapienza University of Rome Lecturer: Francesco Quaglia. Topics: 1.

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

Introduction to the Linux Kernel. Hao-Ran Liu

Processes. Johan Montelius KTH

Operating System Design Issues. I/O Management

Announcement. Exercise #2 will be out today. Due date is next Monday

Scrivere device driver su Linux. Better Embedded 2012 Andrea Righi

A process. the stack

CISC2200 Threads Spring 2015

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Yielding, General Switching. November Winter Term 2008/2009 Gerd Liefländer Universität Karlsruhe (TH), System Architecture Group

CIS c. University of Pennsylvania Zachary Goldberg. Notes

Embedded Linux kernel and driver development training 5-day session

CS330: Operating System and Lab. (Spring 2006) I/O Systems

Introduction to Operating Systems. Chapter Chapter

Process Coordination and Shared Data

W4118: interrupt and system call. Junfeng Yang

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Table of Contents. Preface... xi

CPSC Tutorial 17

THREADS & CONCURRENCY

Mon Sep 17, 2007 Lecture 3: Process Management

MP3: VIRTUAL MEMORY PAGE FAULT MEASUREMENT

I/O Management Software. Chapter 5

AUTOBEST: A United AUTOSAR-OS And ARINC 653 Kernel. Alexander Züpke, Marc Bommert, Daniel Lohmann

Operating Systemss and Multicore Programming (1DT089)

KERNEL THREAD IMPLEMENTATION DETAILS. CS124 Operating Systems Winter , Lecture 9

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Transcription:

<Slides download> http://www.pf.is.s.u-tokyo.ac.jp/class.html Advanced Operating Systems #13 Shinpei Kato Associate Professor Department of Computer Science Graduate School of Information Science and Technology The University of Tokyo

Course Plan Multi-core Resource Management Many-core Resource Management GPU Resource Management Virtual Machines Distributed File Systems High-performance Networking Memory Management Network on a Chip Embedded Real-time OS Device Drivers Linux Kernel

Schedule 1. 2017.9.27 (No Class) 2. 2017.10.4 Introduction 3. 2017.10.11 (No Class) 4. 2017.10.18 Multi-core & Many-core Resource Management 5. 2017.10.25 GPU Resource Management 6. 2017.11.1 (No Class) 7. 2017.11.8 Memory Management 8. 2017.11.15 Device Drivers 9. 2017.11.22 Linux Kernel 10. 2017.11.29 Linux Kernel 11. 2017.12.6 Linux Kernel 12. 2017.12.13 Virtual Machines & Distributed File Systems 13. 2017.12.20 Network on a Chip 14. 2017.1.10 High-performance Networking 15. 2017.1.17 Embedded Real-time OS 16. 2017.1.24 (Probably No Class)

Device Drivers Abstracting Devices Modules and Interrupts /* The case for Linux */ Acknowledgement: Prof. Pierre Olivier, ECE 4984, Linux Kernel Programming, Virginia Tech

Outline1 1 Kernel modules: presentation 2 Writing a kernel module 3 Compiling a kernel module 4 Launching a kernel module 5 Modules: miscellaneous information 6 Memory allocation

Outline1 1 Kernel modules: presentation 2 Writing a kernel module 3 Compiling a kernel module 4 Launching a kernel module 5 Modules: miscellaneous information 6 Memory allocation

Kernel modules: presentation General information e Modules are pieces of kernel code that can be dynamically loaded and unloaded at runtime ) No need to reboot e Appeared in Linux 1.2 (1995) e Numerous Linux features can be compiled as modules ) Selection in the configuration.config file ) Ex: device/filesystem drivers ) Generated through the menuconfig make target ) Opposed to built-in in the kernel binary executable vmlinux

Kernel modules: presentation Benefits of kernel modules e Modules benefits: ) No reboot ) Saves a lot of time when developing/debugging ) No need to compile the entire kernel ) Saves memory and CPU time by running on-demand ) No performance difference between module and built-in kernel code ) Help identifying buggy code ) Ex: identifying a buggy driver compiled as a module by selectively running them

Outline 1 Kernel modules: presentation 2 Writing a kernel module 3 Compiling a kernel module 4 Launching a kernel module 5 Modules: miscellaneous information 6 Memory allocation

Writing a kernel module Basic C file for a module 1 #include <linux/module.h> /* Neededbyallmodules */ 2 #include <linux/kernel.h> /* KERN_INFO */ 3 #include <linux/init.h> /* Initandexitmacros */ 4 5 staticint answer initdata = 42; 6 7 staticint init lkp_init(void) 8 { 9 printk(kern_info"moduleloaded... n"); 10 printk(kern_info"theansweris%d... n", answer); 11 12 /* Return0onsuccess,somethingelseonerror */ 13 return 0; 14 } 15 16 staticvoid exit lkp_exit(void) 17 { 18 printk(kern_info"moduleexiting... n"); 19 } 20 21 module_init(lkp_init); 22 module_exit(lkp_exit); 23 24 MODULE_LICENSE("GPL"); 25 MODULE_AUTHOR("PierreOlivier<polivier@vt.edu"); 26 MODULE_DESCRIPTION("Samplekernelmodule"); e Create a C file anywhere on the filesystem ) No need to be inside the kernel sources e Init. & exit functions ) Launched at load/unload time e MODULE * macros ) General info about the module

Writing a kernel module Kernel namespace e Module is linked against the entire kernel: ) Module has visibility on all of the kernel global variables ) To avoid namespace pollution and involuntary reuse of variables names: ) Use a well defined naming convention. Ex: my module function a() my module function b() my module global variable ) Use static as much as possible e Kernel symbols list is generally present in: /proc/kallsyms

Outline1 1 Kernel modules: presentation 2 Writing a kernel module 3 Compiling a kernel module 4 Launching a kernel module 5 Modules: miscellaneous information 6 Memory allocation

Compiling a kernel module Kernel sources & module Makefile e Need to have the kernel sources somewhere on the filesystem e Create a Makefile in the same directory as the module source file 1 2 3 4 5 6 7 8 9 10 11 12 #let sassumethemodulecfileisnamedlkp.c obj-m := lkp.o KDIR := /path/to/kernel/sources/root/directory #Alternative:Debian/Ubuntuwithkernel-headerspackage : #KDIR:=/lib/modules/$(shelluname-r)/build PWD := $(shell pwd) all: lkp.c make -C $(KDIR) SUBDIRS=$(PWD) modules clean: make -C $(KDIR) SUBDIRS=$(PWD) clean 1 2 3 e Multiple source files? obj-m += file1.c obj-m += file2.c #etc. e After compilation, the compiled module is the file with.ko extension

Outline1 1 Kernel modules: presentation 2 Writing a kernel module 3 Compiling a kernel module 4 Launching a kernel module 5 Modules: miscellaneous information 6 Memory allocation

Launching a kernel module insmod/rmmod e Needs administrator privileges (root) ) You are executing kernel code! e Using insmod: 1 sudo insmod file.ko ) Module is loaded and init function is executed e Note that a module is compiled against a specific kernel version and will not load on another kernel ) This check can be bypassed through a mechanism called modversions but it can be dangerous e Remove the module with rmmod: 1 2 3 sudo rmmod file #or: sudo rmmod file.ko ) Module exit function is called

Launching a kernel module modprobe e make modules install from the kernel sources installs the modules in a standard location on the filesystem ) Generally /lib/modules/<kernel version>/ e These modules can be inserted through modprobe: 1 sudo modprobe <module name> ) No need to point to a file, just give the module name e Contrary to insmod, modprobe handles modules dependencies ) Dependency list generated in /lib/modules/<kernel version/modules.dep e Remove using modprobe -r <module name> e Such installed modules can be loaded automatically at boot time by editing /etc/modules or the files in /etc/modprobe.d

Outline1 1 Kernel modules: presentation 2 Writing a kernel module 3 Compiling a kernel module 4 Launching a kernel module 5 Modules: miscellaneous information 6 Memory allocation

Modules: miscellaneous information Modules parameters e Parameters can be entered from the command line at launch time 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include <linux/module.h> /*... */ staticint int_param = 42; staticchar *string_param ="defaultvalue"; module_param(int_param, int, 0); MODULE_PARM_DESC(int_param,"Asampleintegerkernelmoduleparameter"); module_param(string_param, charp, S_IRUSR S_IWUSR S_IRGRP S_IROTH); MODULE_PARM_DESC(string_param,"Anotherparameter,astring"); staticint init lkp_init(void) { printk(kern_info"intparam:%d n", int_param); printk(kern_info"stringparam:%s n", string_param); } /*... */ /*... */ 1 sudo insmod lkp.ko int_param=12 string_param="hello"

Modules: miscellaneous information modinfo, lsmod e modinfo: info about a kernel module ) Description, kernel version, parameters, author, etc. 1 modinfo my_module.ko 2 filename: 3 description: 4 author: 5 license: 6 srcversion: 7 depends: 8 vermagic: 9 parm: 10 parm: /tmp/test/my_module.ko Sample kernel module Pierre Olivier <polivier@vt.edu GPL A5ADE92B1C81DCC4F774A37 4.8.0-34-generic SMP mod_unload modversions int_param:a sample integer kernel module parameter (int) string_param:another parameter, a string (charp) e lsmod: list currently running modules ) Can also look in /proc/modules

Modules: miscellaneous information Additional sources of information on kernel modules e The linux kernel module programming guide: ) http://www.tldp.org/ldp/lkmpg/2.6/html/index.html e Linux loadable kernel module howto ) http://www.tldp.org/howto/module-howto/index.html e Linux sources Documentation/kbuild/modules.txt

Outline1 1 Kernel modules: presentation 2 Writing a kernel module 3 Compiling a kernel module 4 Launching a kernel module 5 Modules: miscellaneous information 6 Memory allocation

Memory allocation kmalloc e Allocate memory that is virtually and physically contiguous ) For DMA, memory-mapped I/O, and performance (large pages) e Because of that property, maximum allocated size through one kmalloc invocation is limited ) 4MB on x86 (architecture dependent) 1 2 3 4 5 6 7 #include <linux/slab.h> /*... */ char *my_string = (char *)kmalloc(128, GFP_KERNEL); my_struct my_struct_ptr = (my_struct *)kmalloc(sizeof(my_struct), GFP_KERNEL); /*... */ kfree(my_string); kfree(my_struct_ptr); e Returns a pointer to the allocated memory or NULL in case of failure e Mostly used flags: ) GFP KERNEL: might sleep ) GFP ATOMIC: do not block, but higher chance offailure

Memory allocation vmalloc e Allocate memory that is virtually contiguous, but not physically contiguous e No size limit other than the amount of free RAM (at least on 64 bit architectures e Might sleep 1 2 3 4 5 6 7 #include <linux/vmalloc.h> /*... */ char *my_string = (char *)vmalloc(128); my_struct my_struct_ptr = (my_struct *)vmalloc(sizeof(my_struct)); /*... */ vfree(my_string); vfree(my_struct_ptr); e Returns a pointer to the allocated memory or NULL in case of failure

Outline2 1 Interrupts: general information 2 Registering & writing an interrupt handler 3 Interrupt context 4 Interrupt handling internals in Linux 5 / p r o c / i n t e r r u p t s 6 Interrupt control

Outline2 1 Interrupts: general information 2 Registering & writing an interrupt handler 3 Interrupt context 4 Interrupt handling internals in Linux 5 / p r o c / i n t e r r u p t s 6 Interrupt control

Interrupts: general information Interrupts Compared the the CPU, devices are slow.., Ex: when a read request is issued to the disk, it is sub-optimal to wait, doing nothing until the data is ready (in RAM).., Need to know when the hardware is ready Polling can create a lot of overhead.., Having the CPU check regularly the status of the hardware The solution is to have hardware devices signal the CPU that they need attention.., Interrupts.., A key has been pressed on the keyboard.., A packet has been received on the network card.., etc.

Interrupts: general information Interrupts (2) Interrupts are electrical signals multiplexed by the interrupt controller.., Sent on a specific pin of the CPU Once an interrupt is received, a dedicated function is executed:.., Interrupt handler They can be received in a completely non-deterministic way:.., The kernel/user space can be interrupted at (nearly) any time to process an interrupt

Interrupts: general information Interrupts (3) Device identifier: interrupt line or Interrupt ReQuest (IRQ) Function executed by the CPU: interrupt handler or Interrupt Service Routine (ISR) 8259A interrupt lines:.., IRQ #0: system timer.., IRQ #1: keyboard controller.., IRQ #3 and #4: serial port.., IRQ #5: terminal.., IRQ #6: floppy controller.., IRQ #8: RTC.., IRQ #12: mouse.., IRQ #14: ATA (disk) Source [2] Some interrupt lines can be shared among several devices.., True for most modern devices (PCIe)

Interrupts: general information Exceptions Exception are interrupt issued by the CPU executing some code.., Software interrupts, as opposed to hardware ones (devices).., Happen synchronously with respect to the CPU clock.., Examples:.., Program faults: divide-by-zero, page fault, general protection fault, etc..., Voluntary exceptions: INT assembly instruction, for example for syscall invocation.., List: [1] Exceptions are managed by the kernel the same way as hardware interrupts

Interrupts: general information Interrupt handlers The interrupt handlers (ISR) are kernel C functions associated to interrupt lines.., Specific prototype.., Run in interrupt context.., Opposite to process context (system call).., Also called atomic context as one cannot sleep in an ISR: it is not a schedulable entity Managing an interrupt involves two high-level steps: 1 2 Acknowledging the reception (mandatory, fast) Potentially performing additional work (possibly slow).., Ex: processing a network packet available from the Network Interface Card (NIC)

Interrupts: general information Top-halves vs bottom-halves Interrupt processing must be fast.., We are indeed interrupting user processes executing (user/kernel space).., In addition, other interrupts may need to be disabled during an interrupt processing However, it sometimes involves performing significant amount of work Conflicting goals.., Thus, processing an interrupt is broken down between: 1 2 Top-half: time-critical operations (ex: ack), run immediately upon reception Bottom-half: less critical/time-consuming work, run later with other interrupts enabled

Interrupts: general information Top-half & bottom-half example d r i v e r s / i n p u t / k e y b o a r d / o m a p - k e y p a d. c 1 2 3 4 5 6 7 8 9 10 11 1 2 3 / * ( b l o c k 1) * / s t a t i c i n t omap_kp_probe( s truc t p l a t f o r m _ d e v i c e * p d ev) { / *... * / omap_kp->irq = p l a t f o r m _ g e t _ i r q ( p d e v, 0 ) ; if( o m a p _ k p - > i r q >= 0) { i f ( r e q u e s t _ i r q ( o m a p _ k p - > i r q, o m a p _ k p _ i n t e r r u p t, 0, "omap-keypad ", omap_kp) < 0) goto e r r 4 ; } } / * ( b l o c k 3) * / s t a t i c DECLARE_TASKLET_DISABLED( k p _ t a s k l e t, o m a p _ k p _ t a s k l e t, 0 ) ; 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 / * ( b l o c k 2) * / / * Top h a l f : i n t e r r u p t handler (ISR) * / s t a t i c i r q r e t u r n _ t o m a p _ k p _ i n t e r r u p t ( i n t i r q, void * d e v _ i d ) { / * d i s a b l e keyboard i n t e r r u p t * / o map_writew(1, / *... * / ); } t a s k l e t _ s c h e d u l e ( & k p _ t a s k l e t ) ; return IRQ_HANDLED; / * ( b l o c k 4) * / / * Bottom h a l f * / s t a t i c v o i d o map_kp_tasklet( u nsigned d a t a ) { / * performs l o t o f work * / } l o ng

Outline2 1 Interrupts: general information 2 Registering & writing an interrupt handler 3 Interrupt context 4 Interrupt handling internals in Linux 5 / p r o c / i n t e r r u p t s 6 Interrupt control

Registering & writing an interrupt handler Interrupt handler registration: r e q u e s t i r q ( ) re q u e s t i r q ( ) (in i n c l u d e s / l i n u x / i n t e r r u p t. h ) 1 2 3 s t a t i c i n l i n e i n t must_check r e q u e s t _ i r q ( u n s i g n e d i n t i r q, i r q _ h a n d l e r _ t h a n d l e r, unsigned l o ng f l a g s, c o n s t char *name, void * d ev) i r q : interrupt number h a n d l e r : function pointer to the actual handler.., prototype: 1 t y p e d e f i r q r e t u r n _ t ( * f u n c ) ( i n t i r q, void * d a t a ) ; name: String describing the associated device.., For example used in / p r o c / i n t e r r u p t s dev: unique value identifying a device among a set of devices sharing an interrupt line

Registering & writing an interrupt handler Interrupt handler registration: registration flags Registration flags: IRQF DISABLED: disables all interrupts when processing this handler.., Bad form, reserved for performance sensitive devices.., Generally handlers run with all interrupts enabled except their own.., Removed in 4.1 IRQF SAMPLE RANDOM: this interrupt frequency will contribute to the kernel entropy pool.., For Random Number Generation.., Do not set this on periodic interrupts! (ex: timer).., RNG is used for example for cryptographic key generation IRQF TIMER: system timer IRQF SHARED: interrupt line can be shared.., Each of the handlers sharing the line must set this

Registering & writing an interrupt handler Interrupt handler registration: i r q r e q u e s t ( ) (2) i r q r e q u e s t ( ) returns 0 on success, or standard error code.., -EBUSY: interrupt line already in use i r q r e q u e s t ( ) can sleep.., Creating an entry in the / p r o c virtual filesystem.., k m a l l o c ( ) in the call stack

Registering & writing an interrupt handler An interrupt example, freeing an interrupt handler 1 2 3 4 5 6 1 2 3 4 5 6 omap-keypad registration and handler: s t a t i c i n t omap_kp_probe(struct p l a t f o r m _ d e v i c e * p d ev) { / *... * / if( r e q u e s t _ i r q ( o m a p _ k p - > i r q, o m a p _ k p _ i n t e r r u p t, 0, "omap-keypad", omap_kp) < 0) goto e r r 4 ; } s t a t i c i r q r e t u r n _ t o m a p _ k p _ i n t e r r u p t ( i n t i r q, void * d e v _ i d ) { omap_writew(1, OMAP1_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT); t a s k l e t _ s c h e d u l e ( & k p _ t a s k l e t ) ; return IRQ_HANDLED; } Freeing an irq is made through f r e e i r q ( ) : 1 v o i d f r e e _ i r q ( u n s i g n e d i n t i r q, void * d e v ) ; omap-key pad example: 1 2 3 4 5 6 7 s t a t i c i n t omap_kp_remove( struct p l a t f o r m _ d e v i c e *pdev) { / *... * / f r e e _ i r q ( o m a p _ k p - > i r q, omap_kp); / *... * / return 0; }

Registering & writing an interrupt handler Inside the interrupt handler Interrupt handler prototype: 1 s t a t i c i r q r e t u r n _ t h a n d l e r _ n a m e ( i n t i r q, void * d e v ) ; dev parameter:.., Must be unique between handlers sharing an interrupt line.., Set when registering the handler and can be accessed by the handler.., ex: pass a pointer to a data structure representing the device Return value:.., IRQ NONE: the expected device was not the source of the interrupt.., IRQ HANDLED: correct invocation.., This macro can be used: IRQ RETVAL(x).., If ( x! = 0), expands into IRQ HANDLED, otherwise expands into IRQ NONE (example: v s c s t a t i n t e r r u p t in d r i v e r s / a t a / s a t a v s c. c ) Interrupt handlers do not need to be reentrant (thread-safe).., The corresponding interrupt is disabled on all cores while its handler is executing

Registering & writing an interrupt handler Shared handlers Shared handlers.., On registration:.., IRQ SHARED flag.., dev must be unique (ex: a pointer to a data structure representing the device in question).., Handler must be able to detect that the device actually generated the interrupt it is called from.., When an interrupt occurs on a shared line, the kernel executes sequentially all the handlers sharing this line.., Need hardware support at the device level and detection code in the handler

Outline2 1 Interrupts: general information 2 Registering & writing an interrupt handler 3 Interrupt context 4 Interrupt handling internals in Linux 5 / p r o c / i n t e r r u p t s 6 Interrupt control

Interrupt context The kernel can execute in Interrupt vs process context.., In process context following a syscall/an exception.., In interrupt context upon a hardware interrupt reception In interrupt context, sleeping/blocking is not possible.., The handler is not a schedulable entity (user/kernel thread).., No k m a l l o c ( x, GFP KERNEL).., Use GFP ATOMIC.., No use of blocking synchronization primitives (ex: mutex).., Use spinlocks Interrupt context is time-critical.., Other code is interrupted Interrupt handler stack:.., Before 2.6: handlers used the kernel stack of the interrupted process.., Later: 1 dedicated stack per core for handlers (1 page)

Outline2 1 Interrupts: general information 2 Registering & writing an interrupt handler 3 Interrupt context 4 Interrupt handling internals in Linux 5 / p r o c / i n t e r r u p t s 6 Interrupt control

Interrupt handling internals in Linux Interrupt processing path Taken from the textbook

Interrupt handling internals in Linux Interrupt processing path (2) Specific entry point for each interrupt line.., Saves the interrupt number and the current registers.., Calls do IRQ() do IRQ():.., Acknowledge interrupt reception and disable the line.., calls architecture specific functions Call chain ends up by calling h a n d l e i r q e v e n t p e r c p u ( ).., Re-enable interrupts on the line if IRQF DISABLED was not specified during handler registration.., Call the handler if the line is not shared.., Otherwise iterate over all the handlers registered on that line.., Disable interrupts on the line again if they were previously enabled do IRQ() returns to entry point that call r e t from i n t r ( ).., Checks if reschedule is needed (need r e s c h e d ).., Restore register values

Outline2 1 Interrupts: general information 2 Registering & writing an interrupt handler 3 Interrupt context 4 Interrupt handling internals in Linux 5 / p r o c / i n t e r r u p t s 6 Interrupt control

/ p r o c / i n t e r r u p t s 1 cat /proc/interrupts 2 CPU0 CPU1... 3 0: 19 0... IR-IO-APIC 2-edge timer 4 1: 5 3... IR-IO-APIC 1-edge i8042 5 8: 1 0... IR-IO-APIC 8-edge rt c0 6 9: 272 13275... IR-IO-APIC 9-fast eo i acpi 7 12: 387 11... IR-IO-APIC 12-edge i8042 8 16: 24 2... IR-IO-APIC 16-fasteoi ehci_hcd:usb1 9 23: 25 2... IR-IO-APIC 23-fasteoi ehci_hcd:usb2 Columns: 1 Interrupt line (not showed if no handler installed) 2 Per-cpu occurrence count 3 Related interrupt controller name 4 Edge/level (fasteoi): way the interrupt is triggered 5 Associated device name

Outline2 1 Interrupts: general information 2 Registering & writing an interrupt handler 3 Interrupt context 4 Interrupt handling internals in Linux 5 / p r o c / i n t e r r u p t s 6 Interrupt control

Interrupt control Kernel code sometimes needs to disable interrupts to ensure atomic execution of a section of code.., I.e. we don t want some code section to be interrupted by a handler (as well as kernel preemption).., The kernel provides an API to disable/enable interrupts:.., Disable interrupts for the current CPU.., Mask an interrupt line for the entire machine Note that disabling interrupts does not protect against concurrent access from other cores.., Need locking, often used in conjunction with interrupts disabling

Interrupt control Disabling interrupts on the local core 1 2 3 l o c a l _ i r q _ d i s a b l e ( ) ; / *... * / l o c a l _ i r q _ e n a b l e ( ) ; l o c a l i r q d i s a b l e ( ) should never be called twice without a l o c a l i r q e n a b l e ( ) between them.., What if that code can be called from two locations: 1 2 One with interrupts disabled One with interrupts enabled Need to save the interrupts state in order not to disable them twice: 1 unsigned long f l a g s ; 2 3 l o c a l _ i r q _ s a v e ( f l a g s ) ; 4 5 6 / * d i s a b l e s i n t e r r u p t s _ i f needed_ * / / *.. * / l o c a l _ i r q _ r e s t o r e ( f l a g s ) ; / * r e s t o r e s i n t e r r u p t s t o t h e previous s t a t e * / / * f l a g s is passed as v a l u e but both f u n c t i o n s a re a c t u a l l y macros * /

Interrupt control Disabling / enabling a specific interrupt line Disable / enable a specific interrupt for the entire system 1 2 3 4 v oid d i s a b l e _ i r q ( u n s i g n e d i n t i r q ) ; / * ( 1 ) * / v oid d i s a b l e _ i r q _ n o s yn c ( u n s i g n e d i n t i r q ) ; / * ( 2 ) * / v oid e n a b l e _ i r q ( u n s i g n e d i n t i r q ) ; / * ( 3 ) * / v oid s yn c h r o n i z e _ i r q ( u n s i g n e d i n t i r q ) ; / * ( 4 ) * / 1 2 3 4 Does not return until any currently running handler finishes Do not wait for handler termination Enables interrupt line Wait for a specific line handler to terminate before returning These enable/disable calls can nest.., Must enable as much times as the previous disabling call number These functions do not sleep.., They can be called from interrupt context

Interrupt control Querying the status of the interrupt system i n i n t e r r u p t ( ) returns nonzero if the calling code is in interrupt context.., Handler or bottom-half i n i r q ( ) returns nonzero only if in a handler To check if the code is in process context:..,!in i n t e r r u p t ( )

Additional information Interrupts:.., h t t p : / / w w w. m a t h c s. e m o r y. e d u / j a l l e n / C o u r s e s / 3 5 5 / S y l l a b u s / 6 - i o / 0 - E x t e r n a l / i n t e r u p t. h t m l More details on Linux interrupt management (v3.18):.., h t t p s : / / 0 x a x. g i t b o o k s. i o / l i n u x - i n s i d e s / c o n t e n t / i n t e r r u p t s /

Bibliography I 1 Exceptions - osdev wiki http://wiki.osdev.org/exceptions Accessed: 2017-02-08. 2 X86 assembly/programmable interrupt controller. h t t p s : / / e n. w i k i b o o k s. o r g / w i k i / X 8 6 _ A s s e m b l y / P r o g r a m m a b l e _ I n t e r r u p t _ C o n t r o l l e r Accessed: 2017-02-08.