Linux Kernel Driver Tutorial

25 pages
72 views

Please download to get full document.

View again

of 25
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Share
Description
Linux Kernel Driver Tutorial Martin Waitz Friedrich-Alexander-University of Erlangen-Nuremberg, Informatik 3 tali@admingilde.org 1. Introduction This tutorial was written for a course in Hardware and Linux driver development (DHWKLINUX (http://www3.informatik.uni-erlangen.de/Lehre/DHwkLINUX/WS2004/index.html)) at the Friedrich-Alexander-University of Erlangen-Nuremberg (http://www3.informatik.uni-erlangen.de). I hope it is interesting for other Kernel developers, too. And well, sorry for the pa
Tags
Transcript
  Linux Kernel Driver Tutorial Martin Waitz Friedrich-Alexander-University of Erlangen-Nuremberg, Informatik 3tali@admingilde.org 1. Introduction This tutorial was written for a course in Hardware and Linux driver development (DHWKLINUX (http://www3.informatik.uni-erlangen.de/Lehre/DHwkLINUX/WS2004/index.html)) at theFriedrich-Alexander-University of Erlangen-Nuremberg (http://www3.informatik.uni-erlangen.de). I hope it is interesting for other Kernel developers, too. And well, sorry for the parts that are still unfinished...Wozu braucht man ein Betriebssystem? Koordinierung von verschiedenen Programmen. Abstrahierungund Virtualisierung von Hardware.BS bieten meist zwei unterschiedliche Schnittstellen an: eine zur Hardware und eine zu User-spaceSoftware Kernel reagiert immer nur auf Events, die jeweils aus dem Userspace oder von der Hardwarekommen.Walkthrougharch/i386/kernel/entry.Ssyscalltable, irqskernel/irq/handle.c__do_IRQ, handle_IRQ_eventfs/read_write.csys_read, vfs_read 2. Allgemeines Fuer uns wichtige Punkte im Source Baum: 1   Linux Kernel Driver Tutorial ã Documantation/  ã Documentation/CodingStyle ã include/linux -- API header files ã include/asm -- Symlink auf Architektur-spezifische Header ã kernel/ -- Grundgeruest ã arch/ -- Architektur-abhaengiger Code ã drivers/char/ -- Character-device-drivers ã fs/*.c -- Filehandlingsonstiges: ã init/ -- zum Booten des Kerns ã ipc/ -- Inter Process Communication (Semaphoren, shared memory, ...) ã drivers/ -- Treiber fuer alles moegliche (net, ide, input, pci, scsi, video,..) ã fs/*/ -- Dateisysteme ã mm/ -- Memory Management, alles rund um Pages ã net/ -- Netzwerk Code ã scripts/ -- Skrite die zum Bauen des Kerns benoetigt werden ã security/ -- Security Module, diese haben Veto-Rechte auf einzelne Operationen ã sound/ -- Sound-Treiber ã usr/ -- userspace code fuer initramfs, derzeit nicht verwendetDebugging schwierig, also aufpassen! Kein floating Point SMP & kernel preemption muessen bedachtwerdenWichtigste Optimierung: Code sollte schnell zu verstehen sein.Es gibt einige Kernel Debugging Optionen, die einem helfen ein paar haeufige Fehler zu entdecken. 2.1. Error checking #include <linux/err.h> #include <linux/errno.h> Check the return value! Most functions use negative return values to indicate an error. The actual valuedetermines the kind of error (as defined in include/linux/errno.h ). When an invalid parameter wasencountered, -EINVAL is returned for example. 2   Linux Kernel Driver Tutorial There are two different conventions for functions that return pointers:1. return NULL in case of error (e.g. kmalloc )2. return a special pointer indicating the actual error.Be careful to check for the right condition! There are some macros to work with the second method:IS_ERR(ptr)return true if the given pointer is considered an error codePTR_ERR(ptr)return the (negative) error code represented by the pointerERR_PTR(error)wrap the (negative) error code inside a pointerIf your function has to error-out, be careful to undo every step that already succeeded. The followingstructure is often used in the kernel to do that: ret = register_foo(...);if (ret < 0) goto out;ret = register_bar(...);if (ret < 0) goto out_foo;ret = -ENOMEM;obj = kmalloc(...);if (obj == NULL) goto out_bar;more_work();err = 0;goto out;out_bar:unregister_bar(...);out_foo:unregister_foo(...);out:return ret; When any subfunction returns with an error, we undo everything in the reverse order and pass the returncode to the calling function, which will do similiar things on its own. This is a little bit like C++exceptions. 3   Linux Kernel Driver Tutorial 2.2. Webseiten ã http://www.dit.upm.es/~jmseyas/linux/kernel/hackers-docs.html ã http://www.tldp.org/LDP/tlk/tlk-toc.html ã http://lwn.net/Kernel/  ã http://kernelnewbies.org/documents/  2.3. Tipps ã Alle Funktionen und Variablen static machen. ã Alle Funktionen mit dem Treibernamen prefixen.Dadurch gibts keine Konflikte mit vordefinierten Routinen und man sieht in einem Backtrace gleich werschuld ist. ã Addressraum annotierungen: __USER, __iomem ã Anzeigen von aufgerufenen Systemcalls: strace 3. Adding new files to the build infrastructure 3.1. Configuration The Linux Kernel can be built in many different ways. Most drivers can be included unconditionally,compiled as modules to be loaded at runtime or be left out completely.The kernel configuration is stored in the file .config which can be created by calling makemenuconfig . In order to create the menu structure that is presented by make menuconfig there aseveral Kconfig files in the source tree.To add a new config option, edit the corresponding Kconfig file (for example driver/char/Kconfig )and add your entry: config EXAMPLEtristate Example driver depends on EXPERIMENTALdefault nhelpSay yes if you want to compile this example driver... 4
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks