Linux Device Drivers Linux Device Drivers Kernel 2.6 1. - - PowerPoint PPT Presentation

linux device drivers
SMART_READER_LITE
LIVE PREVIEW

Linux Device Drivers Linux Device Drivers Kernel 2.6 1. - - PowerPoint PPT Presentation

Linux Device Drivers Linux Device Drivers Kernel 2.6 1. Introduction Dr. Wolfgang Koch 2. Kernel Modules Friedrich Schiller University Jena 3. Char Drivers Department of Mathematics and 4. Advanced Char Drivers Computer Science 5.


slide-1
SLIDE 1

Linux Device Drivers

Kernel 2.6

  • Dr. Wolfgang Koch

Friedrich Schiller University Jena Department of Mathematics and Computer Science Jena, Germany wolfgang.koch@uni-jena.de

Linux Device Drivers

  • 1. Introduction
  • 2. Kernel Modules
  • 3. Char Drivers
  • 4. Advanced Char Drivers
  • 5. Interrupts
  • 1. Introduction

Device Driver I/O Control Using Ports in User Space gcc Inline Assembler Kernel Space Literature, Web Sites

  • 2. Kernel Modules

"Hello World" Module Debug-Print, printk etc. Loading, Unloading, insmod Compiling, Kernelversion Monitoring, Parallel Port Writing to tty Command Line Arguments

slide-2
SLIDE 2
  • 3. Char Drivers

File Operations Device Files, Major & Minor Numbers file_operations Structure register_chardev, Choice of Major Number mknod register_chardev_region – the new way

  • 3. Char Drivers – cont.

read( ), put_user( )

  • pen( ), release( ), Usage Count

file Structure, llseek( ) write( ), get_user( ) Race conditions, Atomic Variables Spinlocks, Semaphores

  • 4. Advanced Char Drivers

Use of Minor Numbers Device Control, ioctl( ) Access Control Writing to /proc Files Memory, kmalloc() Time, Delay

  • 5. Interrupts

Blocking I/O poll( ) Asynchronous Notification Hardware Management Interrupts The Bottom Half

slide-3
SLIDE 3

Device Driver

Driver: A set of functions (of software) that manipulate a hardware device Today as a part of (better: an extension to) the

  • perating system

A container for a collection of subroutines that the OS calls to perform various operations that relate to a hardware device.

10

Device Driver

A set of functions that manipulate a hardware device

– as a part of (an extension to) the operating system:

OS needs / provides access to I/O devices uniform programming surface, "Virtual Device" necessary privileges concurrency, multi tasking system –

applications share physical hardware

11

Device Driver

A set of functions that manipulate a hardware device – as a part of (an extension to) the operating system: a software layer that lies between the applications and the actual device – hides the details, provides a standardized surface applications use system calls –

  • pen( ), read( ), write( ), ioctl( ), ... close( )

if we exchange a device, we simply change the driver – the applications are not affected

12

Kernel Modules

A set of functions that manipulate a hardware device – as a part of (an extension to) the operating system: in Linux: Kernel Modules can be built separately from the rest of the kernel and loaded at runtime when needed

13

slide-4
SLIDE 4

Kernel Modules

in Linux: Kernel Modules this lectures: hands-on tutorial to write kernel module drivers (simple character drivers, without DMA, PnP, USB, ... no multiprocessor (SMP) machines - synchronisation) Kernel version 2.6 (also 2.4) Warning: we have full privileges, can do a lot of damage

14

I/O Control

CPU Mem Dev1 Dev2 Addr Data BUS Contr 15

I/O Control

CPU Mem Port1 Port2 Addr Data BUS Contr Dev1 Dev2

external

16

Ports

External Device Addr Data BUS Contr Address Recognition Data Reg Control Reg Status Reg 17

slide-5
SLIDE 5

Ports

Addr Data BUS Contr Address Recognition Data Reg

Address Recognition:

e.g. 1st serial port (COM1): 0x3F8 – 0x3FF (0011 1111 1...)

memory mapped or separate I/O space (Intel) memory space: Load, Store, Move; I/O ports: In, Out base address: flash ROM (old: DIL switches) lower address bits: address of internal registers

18

Synchronisation by Polling

External Device Addr Data BUS Contr Data Reg Status Reg Ready Data 1

do ; while ( (inb(SR) & 0x80) == 0 ); // busy wait

  • utb ( Databyte, DR ); // impl. reset of SR

19 IR-enable

Synchronisation by Interrupt

External Device Addr Data BUS Contr Data Reg Status Reg Ready Data 1 Control Reg 1 & IRQ

Interrupt lines are scarce

20

Polling vs. Interrupt

The CPU is responsible for synchronisation, addressing and byte counting The device must be slower than the CPU

Polling – waste of CPU time, fast reaction Interrupt – takes time to accept

→ Interrupt at the beginning of a block, then polling

21

slide-6
SLIDE 6

Handshaking

For fast devices we may need a second status line from the port to the device – Acknowledge device and port "shake hands“ → again with polling or interrupt

22

DMA – Direct Memory Access

If the CPU is responsible for addressing and byte counting:

Start: Load R2, Count Load AR1, StartAddr Loop: In R1, DR // synchr. not considered Store (AR1), R1 Inc AR1 Dec R2 Jnz Loop // very expensive ==> DMA 23

DMA Controller

CPU Mem DMAC Port Addr Data BUS Contr

DMAC is 2nd Bus Master responsible for addressing, counting, synchronisation Cycle steal modus or Burst modus

24

Using Ports in User Space

#include <sys/io.h> unsigned char inb(unsigned short int port); void outb(unsigned char value, unsigned short int port);

analogous: inw(), inl(), outw(), outl(), inb_p() ... inline functions, compile: gcc -O -Wall ...

25

slide-7
SLIDE 7

Using Ports in User Space

#include <sys/io.h> #define PORT 0x378 // parallel port int main() {

  • utb(0x25, PORT);
  • > ./exe (or root: # ./exe)

Speicherzugriffsfehler export LANG=en_US ==> Segmentation fault export LANG=hu_HU ==> Szegmens hiba ( 2.4: Szegmentálási hiba ) 26

Using Ports in User Space

#include <sys/io.h> int iopl(int level); // level = 3 int ioperm(ulong from, ulong num, int on);

require root privileges

27

Using Ports in User Space

int k = iopl(3); printf(" Result iopl: %d \n",k); perror(" iopl() "); // if (k<0) { perror(" iopl"); exit 1; }

  • > ./exe

Result iopl: -1 iopl() : Operation not permitted

  • # ./exe

Result iopl: 0 iopl() : Success // outb() works 28

Using Ports in User Space

provide root privileges to an executable program:

> ll exe // ls -l

  • rwxr-xr-x 1 nwk users 3160 ... exe

# chown root:root exe # chmod a+s exe

  • rwsr-sr-x 1 root root 3160 ... exe

> ./exe ==> Result iopl: 0

s - setuid, setgid, process gets eff. user ID/ group ID

  • f the file owner (root), not of the caller

29

slide-8
SLIDE 8

gcc Inline Assembler

unsigned char inb(unsigned short int port) { unsigned char val; asm volatile("inb %1,%0": "=a" (val): "d" (port)); return val; } asm(instructions : outp operands : inp operands [: clobbered registers]);

  • perands: "constraints" (C_expr) -> %0, %1 ...

> gcc -O -S ==> inb %dx,%al 30

gcc Inline Assembler

GNU assembler syntax

mov from, to movl %eax, %ebx // longword (32 bit) movw %ax, %bx // word (16 bit) movb %al, %bl // byte ( 8 bit) movl $0x386, %edi // immediate operand movb (%esi), %al // indirect memory reference

( look at the output of 'gcc -S source.c' )

31

gcc Inline Assembler

asm volatile("outb %b0,%w1"::"a" (val),"Nd" (port)); no output operands

  • perand %0: byte, "a" val -> %al
  • perand %1: word; "Nd" port -> byte constant
  • r %dx

==> outb %al, $255

  • r outb %al, %dx

32

gcc Inline Assembler

constraints:

"=" - output "r" - general register "m" - memory "g" - general register, memory or immediate "&" - different from all input operands "a" - %eax ... "d" - %edx "D" - %edi, "S" - %esi "N" - byte constant (0 .. 255) 33

slide-9
SLIDE 9

gcc Inline Assembler

int xy=5; asm("rorb %b0": "=r" (xy): "0" (xy)); printf(" %02x \n",xy); // rotate right 1 byte: 0x05 -> 0x82

  • utput operand %0

input operand "0" -> same register as %0

  • -> rorb %al

("rorb %0" --> rorb %eax --> Warning) 34

gcc Inline Assembler

asm ("movl %2,%0; incl %2; movl %2,%1" : "=r" (m), "=r"(k) : "r" (n));

  • -> n=37, m=38, k=38 not correct!!

( movl %eax,%eax; incl %eax; movl %eax,%edx ) asm ("movl %2,%0; incl %2; movl %2,%1" : "=&r" (m), "=r"(k) : "r" (n));

  • -> n=37, m=37, k=38 OK

( movl %eax,%ecx; incl %eax; movl %eax,%edx ) 35

gcc Inline Assembler

references (Inline Assembler): gcc documentation:

http://gcc.gnu.org/onlinedocs/gcc-3.3.1/gcc/Extended-Asm.html http://www-106.ibm.com/developerworks/library/l-ia.html ( + links) Intel IA-32 Architecture Software Developer's Manuals: http://cedar.intel.com/cgi-bin/ids.dll/topic.jsp?catCode=BME

36

User Space – Kernel Space

kernel, modules – kernel space applications – user space

  • ne task of the OS: independent operation of programs and

protection against unauthorized access to resources the CPU enforces protection of system software from applications ( i386: 4 rings – supervisor mode (kernel space): ring 0, user mode (user space): ring 3 )

37

slide-10
SLIDE 10

User Space – Kernel Space

modules – kernel space highest privilege level applications – user space low privilege level some operations are disallowed both modes also have their own address space code can switch from one level to another only through a limited number of gates: applications issue "system calls" (then the kernel code executing the system call can access data in the callers address space, interrupt handler cannot)

38

User Space – Kernel Space

modules – kernel space highest privilege level applications – user space low privilege level user: full C library – kernel: system calls only conventional debugger hanging application – simply kill it, we can do any damage in kernel space user mem: swappable, won’t occupy RAM when it is not in use kernel module: 2.4 not interruptible by the scheduler (time slices)

39

User Space – Kernel Space

40

user space

full C library

#include <stdlib.h>

conventional debuggers hanging harmless → kill swappable preemptive

kernel space

system calls only

#include <linux/*>

debugging difficult strange system errors, reboot remains in memory (fast) 2.4 nonpreemptive must be reentrant anyhow be careful – macros very small stack

Literature

Jonathan Corbet, Alessandro Rubini, Greg Kroah-Hartman LINUX Device Drivers, 3rd Edition O'Reilly, 2005 ISBN 0-596-00590-3 available online as .pdf files: http://www.oreilly.com/catalog/linuxdrive3/book/index.csp

41

slide-11
SLIDE 11

Literature

  • A. Rubini, J. Corbet

LINUX Device Drivers, 2nd Edition O'Reilly, 2001 ISBN 0-596-00008-1 (covers versions 2.0 – 2.4) available online as .pdf files: http://www.oreilly.com/catalog/linuxdrive2/ (German: ISBN 3-89721-138-6 , http://www.oreilly.de/ german/freebooks/linuxdrive2ger/book1.html)

42

Literature

  • J. Quade, E.-K. Kunst

Linux-Treiber entwickeln: Gerätetreiber für Kernel 2.6 systematisch eingeführt dpunkt.verl. Heidelberg, 2004 ISBN 3-89864-238-0 425 S. (Bib: INF, DE 2000, LINUX, Q1 ) 2nd Edition: ISBN 3-89864-392-4, 2006, 502 S.

43

Literature

Peter Jay Salzman, Michael Burian, Ori Pomerantz The Linux Kernel Module Programming Guide 80 p., 2005 (Version 2.6) http://www.tldp.org/LDP/lkmpg/2.6/html/ (HTML) http://www.tldp.org/LDP/lkmpg/2.6/lkmpg.pdf

  • ther guides: http://www.tldp.org/guides.html

(tldp: The Linux Documentation Project)

44

Literature

  • T. Aivazian

Linux Kernel 2.4 Internals 77 p, 2002 http://www.tldp.org/LDP/lki/ (HTML) http://www.tldp.org/LDP/lki/lki.pdf ( Version 2.6 not available )

45

slide-12
SLIDE 12

Literature

  • W. Maurer

Linux Kernelarchitektur: Konzepte, Strukturen und Algorithmen von Kernel 2.6 Hanser München, 2004 ISBN 3-446-22566-8 770 S. Infos, Downloads: www.linux-kernel.de (interesting links)

46

Literature

  • S. Gold, S. van der Meer, S. Burkett, M. Welsh

The Linux Programmers Guide rather old (1995) http://www.tldp.org/LDP/lpg http://www.ssc.com/mirrors/LDP/LDP/lpg/

47

Web Sites

http://www.linux.org/docs http://www.linuxdocs.org/ http://www.tldp.org/guides.html http://www.kernel.org/ (developers) The linux-kernel mailing list FAQ: http://www.tux.org/lkml

48