Embedded Linux Course Slides
Embedded Linux Course Slides
Unable to handle kernel paging request at virtual address 4d1b65e8 Unable to handle kernel paging request at virtual address 4d1b65e8 pgd = Covers versions c0280000 pgd = 2.4.33.2 - 2.6.18-rc4 c0280000 <1>[4d1b65e8] *pgd=00000000[4d1b65e8] *pgd=00000000 Version 1.0 Internal error: Oops: f5 [#1] Internal error: Oops: f5 [#1] Modules linked in:Modules linked in: hx4700_udc hx4700_udc asic3_base asic3_base CPU: 0 CPU: 0 PC is at set_pxa_fb_info+0x2c/0x44 PC is at set_pxa_fb_info+0x2c/0x44 LR is at hx4700_udc_init+0x1c/0x38 [hx4700_udc] LR is at hx4700_udc_init+0x1c/0x38 [hx4700_udc] pc : [<c00116c8>] lr : [<bf00901c>] Not tainted sp : c076df78 ip : 60000093 fp : c076df84 pc : [<c00116c8>] lr : [<bf00901c>] Not tainted
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
Rights to copy
This kit contains work by the following authors:
Attribution ShareAlike 2.0 You are free to copy, distribute, display, and perform the work to make derivative works to make commercial use of the work Under the following conditions Attribution. You must give the original author credit. Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. For any reuse or distribution, you must make clear to others the license terms of this work. Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way affected by the above. License text: http://creativecommons.org/licenses/by-sa/2.0/legalcode
Copyright 2004-2006 Michael Opdenacker michael@free-electrons.com http://www.free-electrons.com Copyright 2003-2006 Oron Peled oron@actcom.co.il http://www.actcom.co.il/~oron Copyright 2004 2006 Codefidence ltd. info@codefidence.com http:/www.codefidence.com
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
What is Linux?
Linux is a kernel that implements the POSIX and Single Unix Specification standards which is developed as an Open Source project. Usually when one talks of installing Linux, one is referring to a Linux Distribution. A distribution is a combination of Linux and other programs and library that form an operating system. There exists many such distribution for various purposes, from high end servers to embedded systems. They all share the same interface, thanks to the LSB standard Linux runs on 24 main platforms and supports applications ranging from ccNUMA super clusters to cellular phones and micro controllers. Linux is 15 years old, but is based on the 40 years old Unix design philosophy
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
LGPL
Customer receives the same rights I have in the SPECIFIC PART USED, including source code.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
Kernel C library
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
Kernel
Process
Shell
RTOS
Process Process
RTOS are like the Linux kernel: Single program with single memory space that manages memory, scheduling and interrupts. Linux also has user tasks that run in their own memory space. One of them is the shell.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
This course
Process Library Process Shell
Kernel
Process Process Process
In this course we will go from the shell, through the system libraries and applications and unto the kernel.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
Everything is a file
Almost everything in Unix is a file! Regular files Directories Directories are just files listing a set of files Symbolic links Files referring to the name of another file Devices and peripherals Read and write from devices as with regular files Pipes Used to cascade programs cat *.log | grep error Sockets Inter process communication
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
10
File names
File name features since the beginning of Unix Case sensitive No obvious length limit Can contain any character (including whitespace, except /). File types stored in the file (magic numbers). File name extensions not needed and not interpreted. Just used for user convenience. File name examples: README .bashrc index.htm index.html Windows Buglist index.html.old
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
11
File paths
A path is a sequence of nested directories with a file or directory at the end, separated by the / character Relative path: documents/fun/microsoft_jokes.html Relative to the current directory Absolute path: /home/bill/bugs/crash9402031614568 / : root directory. Start of absolute paths for all files on the system (even for files on removable devices or network shared).
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
12
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
13
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
14
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
15
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
16
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
17
Task control
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
18
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
19
Processes
Everything in Unix is a file Everything in Unix that is not a file is a process Processes Instances of a running programs Several instances of the same program can run at the same time Data associated to processes: Open files, allocated memory, stack, process id, parent, priority, state...
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
20
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
21
fg fg %<n> Puts the last / nth background job in foreground mode Moving the current task in background mode: [Ctrl] Z bg kill %<n> Aborts the nth job.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
22
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
23
Process id Virtual process size (code + data + stack) Process resident size: number of KB currently in RAM Terminal Status: R (Runnable), S (Sleep), D (Uninterrupted sleep), Z (Zombie)...
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
24
You can change the sorting order by typing M: Memory usage, P: %CPU, T: Time. You can kill a task by typing k and the process id.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
25
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
26
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
27
Sequential commands
Can type the next command in your terminal even when the current one is not over. Can separate commands with the ; symbol: echo I love thee; sleep 10; echo not
Conditionals: use || (or) or && (and): more God || echo Sorry, God doesn't exist Runs echo only if the first command fails ls ~sd6 && cat ~sd6/* > ~sydney/recipes.txt Only cats the directory contents if the ls command succeeds (means read access).
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
28
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
29
Environment variables
Shells let the user define variables. They can be reused in shell commands. Convention: lower case names You can also define environment variables: variables that are also visible within scripts or executables called from the shell. Convention: upper case names. env Lists all defined environment variables and their value.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
30
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
31
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
32
File ownership
chown -R sco /home/linux/src (-R: recursive) Makes user sco the new owner of all the files in /home/linux/src. chgrp -R empire /home/askywalker Makes empire the new group of everything in /home/askywalker. chown -R borg:aliens usss_entreprise/ chown can be used to change the owner and group at the same time.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
33
Shutting down
shutdown -h +5 (-h: halt) Shuts the system down in 5 minutes. Users get a warning in their consoles. shutdown -r now (-r: reboot) init 0 Another way to shutdown (used by shutdown). init 6 Another way to reboot (used by shutdown). [Ctrl][Alt][Del] Also works on GNU/Linux (at least on PCs!).
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
34
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
35
36
Network testing
ping freshmeat.net ping 192.168.1.1 Tries to send packets to the given machine and get acknowledgment packets in return.
PING 192.168.1.1 (192.168.1.1) 56(84) 64 bytes from 192.168.1.1: icmp_seq=0 64 bytes from 192.168.1.1: icmp_seq=1 64 bytes from 192.168.1.1: icmp_seq=2 64 bytes from 192.168.1.1: icmp_seq=3 bytes of data. ttl=150 time=2.51 ttl=150 time=3.16 ttl=150 time=2.71 ttl=150 time=2.67 ms ms ms ms
When you can ping your gateway, your network interface works fine. When you can ping an external IP address, your network settings are correct!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
37
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
38
Name resolution
Your programs need to know what IP address corresponds to a given host name (such as kernel.org) Domain Name Servers (DNS) take care of this. You just have to specify the IP address of 1 or more DNS servers in your /etc/resolv.conf file: nameserver 217.19.192.132 nameserver 212.27.32.177 The changes takes effect immediately!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
39
Creating filesystems
Examples mkfs.ext2 /dev/sda1 Formats your USB key (/dev/sda1: 1st partition raw data) in ext2 format. mkfs.ext2 -F disk.img Formats a disk image file in ext2 format mkfs.vfat -v -F 32 /dev/sda1 (-v: verbose) Formats your USB key back to FAT32 format. mkfs.vfat -v -F 32 disk.img Formats a disk image file in FAT32 format. Blank disk images can be created a in the below example: dd if=/dev/zero of=disk.img bs=1024 count=65536
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
40
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
41
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
42
Or display the /etc/mtab file (same result, updated by mount and umount each time they are run)
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
43
Unmounting devices
umount /mnt/usbdisk Commits all pending writes and unmounts the given device, which can then be removed in a safe way. To be able to unmount a device, you have to close all the open files in it:
Close applications opening data in the mounted partition Make sure that none of your shells have a working directory in this mount point. You can run the lsof command (list open files) to view which processes still have open files in the mounted partition.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
44
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
45
glibc
http://www.gnu.org/software/libc/ License: LGPL C library from the GNU project Designed for performance, standards compliance and portability Found on all GNU / Linux host systems Quite big for small embedded systems: about 1.7MB on Familiar Linux iPAQs (libc: 1.2 MB, libm: 500 KB)
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
46
uClibc
http://www.uclibc.org/ for CodePoet Consulting License: LGPL Lightweight C library for small embedded systems, with most features though. The whole Debian Woody was ported to it... You can assume it satisfied most needs! Example size (arm): approx. 400KB (libuClibc: 300 KB, libm: 55KB)
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
47
Compiled with shared libraries glibc 4.6 K 245 K uClibc 4.4 K 231 K
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
48
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
49
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
50
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
51
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
52
uClibc toolchains
Free Electrons uClibc toolchains http://free-electrons.com/community/tools/uclibc Run on i386 GNU/Linux Supported platforms arm, armeb, i386, m68k, ppc, mips, mipsel, sh Quickly updated at each new uClibc release!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
53
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
54
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
55
Can help you to find a working combination of gcc, glibc, binutils and kernel headers versions!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
56
PTXdist
http://www.pengutronix.de/software/ptxdist_en.html - A crosscompiling toolkit project Makes it easier to cross-compile a complete embedded Linux system Supports 10 different platforms. Uses crosstool to build cross compiler toolchains Supported libraries: glibc, uClibc uClibc support is still experimental/buggy... Supports around 200 different packages.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
57
PTXdist Screenshots
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
58
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
59
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
60
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
61
Usefulness of emulators
Of course, can be used to run another system on top of another. Can be used to test kernel and applications ahead of time on a workstation without the cost of a development board. Make the operating system easier to debug than with actual hardware. If the OS freezes, just restart the emulator. Can provide debugger and tracing capabilities However, often emulate only the processor and a few devices at best. Don't replace the real hardware or a development board.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
62
qemu
http://qemu.org Fast processor emulator using a portable dynamic translator. 2 operating modes Full system emulation: processor and various peripherals Supported: x86, x86_64, ppc, arm, sparc, mips User mode emulation (Linux host only): can run applications compiled for another CPU. Supported: x86, ppc, arm, sparc, mips
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
63
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
64
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
65
ARM emulators
Only Free Software, of course! SkyEye: http://skyeye.sourceforge.net Emulates several ARM platforms (AT91, Xscale...) and can boot several operating systems (Linux, uClinux, and others) Softgun: http://softgun.sourceforge.net Virtual ARM system with many virtual on-board peripherals. Boots Linux. SWARM - Software ARM - arm7 emulator http://www.cl.cam.ac.uk/~mwd24/phd/swarm.html Can run uClinux
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
66
Other emulators
ColdFire emulator http://www.slicer.ca/coldfire/ Can boot uClinux
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
67
Emulators - Summary
System emulators Useful to experiment with a full system, including the kernel qemu: x86, x86_64, arm, sparc, ppc, mips SkyEye: several arm architectures User emulators Useful to run or debug user space binaries for other CPUs qemu: x86, arm, sparc, ppc, mips
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
68
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
69
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
70
Busybox commands!
addgroup, adduser, adjtimex, ar, arping, ash, awk, basename, bunzip2, bzcat, cal, cat, chgrp, chmod, chown, chroot, chvt, clear, cmp, cp, cpio, crond, crontab, cut, date, dc, dd, deallocvt, delgroup, deluser, devfsd, df, dirname, dmesg, dos2unix, dpkg, dpkg-deb, du, dumpkmap, dumpleases, echo, egrep, env, expr, false, fbset, fdflush, fdformat, fdisk, fgrep, find, fold, free, freeramdisk, fsck.minix, ftpget, ftpput, getopt, getty, grep, gunzip, gzip, halt, hdparm, head, hexdump, hostid, hostname, httpd, hush, hwclock, id, ifconfig, ifdown, ifup, inetd, init, insmod, install, ip, ipaddr, ipcalc, iplink, iproute, iptunnel, kill, killall, klogd, lash, last, length, linuxrc, ln, loadfont, loadkmap, logger, login, logname, logread, losetup, ls, lsmod, makedevs, md5sum, mesg, mkdir, mkfifo, mkfs.minix, mknod, mkswap, mktemp, modprobe, more, mount, msh, mt, mv, nameif, nc, netstat, nslookup, od, openvt, passwd, patch, pidof, ping, ping6, pipe_progress, pivot_root, poweroff, printf, ps, pwd, rdate, readlink, realpath, reboot, renice, reset, rm, rmdir, rmmod, route, rpm, rpm2cpio, run-parts, rx, sed, seq, setkeycodes, sha1sum, sleep, sort, start-stop-daemon, strings, stty, su, sulogin, swapoff, swapon, sync, sysctl, syslogd, tail, tar, tee, telnet, telnetd, test, tftp, time, top, touch, tr, traceroute, true, tty, udhcpc, udhcpd, umount, uname, uncompress, uniq, unix2dos, unzip, uptime, usleep, uudecode, uuencode, vconfig, vi, vlock, watch, watchdog, wc, wget, which, who, whoami, xargs, yes, zcat
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
71
Compiling busybox
Get the latest stable sources from http://busybox.net Configure busybox (creates a .config file): make menuconfig Compile it: make Install it: make install Logged as root, mount the root fs image (e.g. /mnt/rootfs) rsync -a _install/ /mnt/rootfs/ (the / characters are needed!)
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
72
logread
Part of Busybox. Shows the messages from syslogd (using its circular buffer). Replaces /var/log/messages. Reduces space and complexity (no need to rotate this file). Use logread -f to output data as the log grows. Another possibility: use the dmesg command.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
73
tinylogin
http://tinylogin.busybox.net/ Suite of tiny Unix utilities for Handling logging Being authenticated Changing passwords Maintaining users and groups. Provides shadow password support to enhance system security. Tools and sizes
0K /usr/sbin/addgroup
24K 16K 36K 28K 24K 12K /usr/sbin/adduser /sbin/getty /bin/login /usr/bin/passwd /bin/su /sbin/sulogin
Commands already provided by Busybox. Just useful if your system doesn't use busybox.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
74
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
75
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
76
thttpd
Tiny/Turbo/Throttling HTTP server http://acme.com/software/thttpd/ Simple Implements the HTTP/1.1 minimum (or just a little more) Simple to configure and run. Small Executable size: 88K (version 2.25b), Apache 2.0.52: 264K Very low memory consumption: does not fork and very careful about memory consumption. Portable Compiles cleanly on most Unixlike operating systems Fast About as fast as full-featured servers. Much faster on very high loads (because reduces the server load for the same amount of work) Secure Designed to protect the webserver machine from attacks.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
77
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
78
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
79
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
80
Commercial toolsets
Caution: commercial doesn't mean proprietary! Vendors play fair with the GPL and do make their source code available to their users, and most of the time, to the community. As long as they distribute the sources to their users, the GPL doesn't require vendors to share their sources with any third party. No issue with all the GPL sources developed by or with the community. Graphical toolkits developed by the vendors look proprietary. Their licenses are not advertised on their websites! You have to be a customer to know or get a free preview kit to know.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
81
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
82
Montavista
http://www.mvista.com/ The market leader Employs some of the most active kernel hackers, in particular on the arm platform All kernel development shared with the community kernel core and drivers (Linux 2.6 example: preemption option, many drivers...) Graphical development tools are proprietary
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
83
TimeSys
http://timesys.com Similar toolset offering as other vendors. Great flexibility available to their LinuxLinkTM subscribers Community friendly: share very interesting and generic technical whitepapers and articles. Free Software BSPs (Board Support Packages) available Linux soft and hard real-time OS product Development tools seem to be proprietary
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
84
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
85
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
86
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
87
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
88
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
89
Distributions
Distribute ready to use root filesystems. Can be used by any binary compatible platform. Example: running Familiar Linux on a mobile phone! Can be reused even on an early development phase. No need to create your root filesystem from scratch! Easy to upgrade, remove or add applications through binary packages.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
90
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
91
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
92
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
93
Buildroot
http://buildroot.uclibc.org/ Tool to automatically build a ready-made uClibc rootfs with basic applications and a development toolchain. Useful in early development. Can be used to build only uClibc toolchains. Supports lots of architectures Automatically downloads source packages Very easy to use: just run: make
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
94
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
95
Conclusion
Commercial distributions and toolsets Best if you don't have your own support resources and have a sufficient budget Really help focusing on your real job: making an embedded device. You can even subcontract driver development to the vendor Community distributions and tools Best if you are on a tight budget Best if you are willing to build your own embedded Linux expertise and train your own support resources. In any case, your products are based on Free Software!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
96
Writing Code
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
97
Hello World!
#include <stdio.h> int main(int argc, char **argv) { int i=1; printf ("Hello World! %d\n", i); return 0; }
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
98
A simple Makefile
CC=/usr/local/arm/2.95.3/bin/arm-linux-gcc CFLAGS=-g LDFLAGS=-lpthreads .PHONY: clean all all: test clean: @rm -f *.o *~ test
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
99
}
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
100
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
101
Before going into real time critical sections of your code make sure stack is allocated!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
102
POSIX Priorities
int sched_setscheduler(pid_t pid, int policy, const struct sched_param *p); struct sched_param { ... int sched_priority }; sched_setscheduler sets both the scheduling policy and the associated parameters for the process identified by pid. If pid equals zero, the scheduler of the calling process will be set. The interpretation of the parameter p depends on the selected policy. Currently, the following three scheduling policies are supported under Linux: SCHED_FIFO, SCHED_RR, and SCHED_OTHER; There is also a sched_getscheduler().
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
103
104
Locking Memory
int mlock(const void *addr, size_t len);
mlock disables paging for the memory in the range starting at addr with length len bytes.
105
Resource Limits
#include <sys/time.h> #include <sys/resource.h> struct rlimit { rlim_t rlim_cur; rlim_t rlim_max; }; /* Soft limit */ /* Hard limit */
int getrlimit(int resource, struct rlimit *rlim) int setrlimit(int resource, const struct rlimit *rlim);
These system calls can be used to get/set soft and hard limits to process resource usage.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
106
107
Thread 1
Process 123
Thread
Thread
Thread
Thread
2
Process 124
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
108
POSIX Threads
Linux uses the POSIX Threads threading mechanism. Linux threads are Light Weight Process each thread is a task scheduled by the kernel's scheduler. Process creation time is roughly double then a thread creation. But Linux process creation time is relatively low. To use threads in your code: #include <pthreads.h> In your Makefile: Add -lpthreads -lrt to LDFLAGS.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
109
Creating Threads
Function: pthread_create() int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *), void * arg); The pthread_create() routine creates a new thread within a process. The new thread starts in the start routine start_routine which has a start argument arg. The new thread has attributes specified with attr, or default attributes if attr is NULL. If the pthread_create() routine succeeds it will return 0 and put the new thread id into thread, otherwise an error number shall be returned indicating the error.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
110
111
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
112
Detach State
Thread Attribute: detachstate
Control whether the thread is created in the joinable state or in the detached state. The default is joinable state. In the joinable state, another thread can synchronize on the thread termination and recover its termination code using pthread_join(3), but some of the thread resources are kept allocated after the thread terminates, and reclaimed only when another thread performs pthread_join(3) on that thread. In the detached state, the thread resources are immediately freed when it terminates, but pthread_join(3) cannot be used to synchronize on the thread termination. A thread created in the joinable state can later be put in the detached thread using pthread_detach(3).
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
113
Sched Policy
Thread Attribute: schedpolicy Select the scheduling policy for the thread: one of SCHED_OTHER (regular, non-realtime scheduling), SCHED_RR (realtime, round-robin) or SCHED_FIFO (realtime, first-in first-out). Default value: SCHED_OTHER. The realtime scheduling policies SCHED_RR and SCHED_FIFO are available only to processes with superuser privileges. The scheduling policy of a thread can be changed after creation with pthread_setschedparam(3).
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
114
Sched Param
Thread Attribute: schedparam Contain the scheduling parameters (essentially, the scheduling priority) for the thread. Default value: priority is 0. This attribute is not significant if the scheduling policy is SCHED_OTHER; it only matters for the realtime policies SCHED_RR and SCHED_FIFO. The scheduling priority of a thread can be changed after creation with pthread_setschedparam(3).
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
115
Inherit Sched
Thread Attribute: inheritsched Indicate whether the scheduling policy and scheduling parameters for the newly created thread are determined by the values of the schedpolicy and schedparam attributes (PTHREAD_EXPLICIT_SCHED) or are inherited from the parent thread (value PTHREAD_INHERIT_SCHED).
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
116
Destroying Threads
Function: pthread_exit() void pthread_exit(void * status); The pthread_exit() routine terminates the currently running thread and makes status available to the thread that successfully joins, pthread_join(), with the terminating thread. In addition pthread_exit() executes any remaining cleanup handlers in the reverse order they were pushed, pthread_cleanup_push(), after which all appropriate thread specific destructors are called. An implicit call to pthread_exit() is made if any thread, other than the thread in which main() was first called, returns from the start routine specified in pthread_create().
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
117
118
Canceling Threads
Function: pthread_cancel() int pthread_cancel(pthread_t thread); Cancellation is the mechanism by which a thread can terminate the execution of another thread. More precisely, a thread can send a cancellation request to another thread. Depending on its settings, the target thread can then either ignore the request, honor it immediately, or defer it till it reaches a cancellation point. When a thread eventually honors a cancellation request, it performs as if pthread_exit(PTHREAD_CANCELED) has been called at that point. pthread_cancel sends a cancellation request to the thread denoted by the thread argument.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
119
Cancel State
Function: pthread_setcancelstate() int pthread_setcancelstate(int state, int *oldstate); pthread_setcancelstate changes the cancellation state for the calling thread -- that is, whether cancellation requests are ignored or not. The state argument is the new cancellation state: either PTHREAD_CANCEL_ENABLE to enable cancellation, or PTHREAD_CANCEL_DISABLE to disable cancellation (cancellation requests are ignored). If oldstate is not NULL, the previous cancellation state is stored in the location pointed to by oldstate, and can thus be restored later by another call to pthread_setcancelstate.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
120
Cancel Type
Function: pthread_setcanceltype() int pthread_setcanceltype(int type, int *oldtype); pthread_setcanceltype changes the type of responses to cancellation requests for the calling thread: asynchronous (immediate) or deferred. The type argument is the new cancellation type: either PTHREAD_CANCEL_ASYNCHRONOUS to cancel the calling thread as soon as the cancellation request is received, or PTHREAD_CANCEL_DEFERRED to keep the cancellation request pending until the next cancellation point. If old type is not NULL, the previous cancellation state is stored in the location pointed to by oldtype, and can thus be restored later by another call to pthread_setcanceltype.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
121
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
122
Cancel Points
Cancellation points are those points in the program execution where a test for pending cancellation requests is performed and cancellation is executed if positive. The following POSIX threads functions are cancellation points: pthread_join(3), pthread_cond_wait(3), pthread_cond_timedwait(3), pthread_testcancel(3), sem_wait(3), sigwait(3) All other POSIX threads functions are guaranteed not to be cancellation points. That is, they never perform cancellation in deferred cancellation mode. A number of system calls (basically, all system calls that may block, such as read(2), write(2), wait(2), etc.) and library functions that may call these system calls (e.g. fprintf(3)) are cancellation points.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
123
Test Cancel
Function: pthread_testcancell() void pthread_testcancel(void); pthread_testcancel does nothing except testing for pending cancellation and executing it. Its purpose is to introduce explicit checks for cancellation in long sequences of code that do not call cancellation point functions otherwise.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
124
Pthread Once
Function: pthread_once() int pthread_once(pthread_once_t *once_control, void(*init_routine) (void)); The purpose of pthread_once is to ensure that a piece of initialization code is executed at most once. The once_control argument points to a static or extern variable statically initialized to PTHREAD_ONCE_INIT. The first time pthread_once is called with a given once_control argument, it calls init_routine with no argument and changes the value of the once_control variable to record that initialization has been per formed. Subsequent calls to pthread_once with the same once_control argument do nothing
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
125
Cleanup Handlers
Cleanup handlers are functions that get called when a thread terminates, either by calling pthread_exit(3) or because of cancellation. Cleanup handlers are installed and removed following a stack-like discipline. The purpose of cleanup handlers is to free the resources that a thread may hold at the time it terminates. In particular, if a thread exits or is canceled while it owns a locked mutex, the mutex will remain locked forever and prevent other threads from executing normally. The best way to avoid this is, just before locking the mutex, to install a cleanup handler whose effect is to unlock the mutex.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
126
Cleanup Push
Function: pthread_cleanup_push() void pthread_cleanup_push(void (*routine) (void *), void *arg); pthread_cleanup_push installs the routine function with argument arg as a cleanup handler. From this point on to the matching pthread_cleanup_pop, the function routine will be called with arguments arg when the thread terminates, either through pthread_exit(3) or by cancellation. If several cleanup handlers are active at that point, they are called in LIFO order: the most recently installed handler is called first.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
127
Cleanup Pop
Function: pthread_cleanup_pop() void pthread_cleanup_pop(int execute); pthread_cleanup_pop removes the most recently installed cleanup handler. If the execute argument is not 0, it also executes the handler, by calling the routine function with arguments arg. If the execute argument is 0, the handler is only removed but not executed.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
128
Cleanup Continued
Matching pairs of pthread_cleanup_push and pthread_cleanup_pop must occur in the same function, at the same level of block nesting and behave like brackets. Here is how to lock a mutex mut in such a way that it will be unlocked if the thread is canceled while mut is locked: pthread_cleanup_push(pthread_mutex_unlock, (void *) &mut); pthread_mutex_lock(&mut); /* do some work */ pthread_cleanup_pop(1);
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
129
Cleanup Extras
pthread_cleanup_push_defer_np is a non-portable extension that combines pthread_cleanup_push and pthread_setcanceltype(3). It pushes a cleanup handler just as pthread_cleanup_push does, but also saves the current cancellation type and sets it to deferred cancellation. This ensures that the cleanup mechanism is effective even if the thread was initially in asynchronous cancellation mode. pthread_cleanup_pop_restore_np pops a cleanup handler introduced by pthread_cleanup_push_defer_np!, and restores the cancellation type to its value at the time pthread_cleanup_push_defer_np was called. pthread_cleanup_push_defer_np and pthread_cleanup_pop_restore_np must occur in matching pairs, at the same level of block nesting.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
130
Barrier Init
Function: pthread_barrier_init() int pthread_barrier_init(pthread_barrier_t *restrict barrier, const pthread_barrierattr_t *restrict attr, unsigned count); The pthread_barrier_init() function shall allocate any resources required to use the barrier referenced by barrier and shall initialize the barrier with attributes referenced by attr. The count argument specifies the number of threads that must call pthread_barrier_wait() before any of them successfully return from the call. The value specified by count must be greater than zero.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
131
Barrier Destroy
Function: pthread_barrier_destroy() int pthread_barrier_destroy(pthread_barrier_t *barrier); The pthread_barrier_destroy() function shall destroy the barrier referenced by barrier and release any resources used by the barrier. The effect of subsequent use of the barrier is undefined until the barrier is reinitialized by another call to pthread_barrier_init(). The results are undefined if pthread_barrier_destroy() is called when any thread is blocked on the barrier, or if this function is called with an uninitialized barrier.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
132
Barrier Wait
Function: pthread_barrier_wait() int pthread_barrier_wait(pthread_barrier_t *barrier);
The pthread_barrier_wait() function shall synchronize participating threads at the barrier referenced by barrier. The calling thread shall block until the required number of threads have called pthread_barrier_wait() specifying the barrier. When the required number of threads have called pthread_barrier_wait() specifying the barrier, the constant PTHREAD_BARRIER_SERIAL_THREAD shall be returned to one unspecified thread and zero shall be returned to each of the remaining threads. At this point, the barrier shall be reset to the state it had as a result of the most recent pthread_barrier_init() function that referenced it.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
133
Linux IPC
POSIX IPC SysV IPC Pipes Unix Domain Sockets Signals TCP/IP Sockets
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
134
Creating a Mutex
Function: pthread_mutex_init() int pthread_mutex_init(pthread_mutex_t * mutex, const pthread_mutex_attr *attr); The pthread_mutex_init() routine creates a new mutex, with attributes specified with attr, or default attributes if attr is NULL. If the pthread_mutex_init() routine succeeds it will return 0 and put the new mutex id into mutex, otherwise an error number shall be returned indicating the error.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
135
Destroying a Mutex
Function: pthread_mutex_destroy() int pthread_mutex_destroy(pthread_mutex_t * mutex); The pthread_mutex_destroy() routine destroys the mutex specified by mutex. If the pthread_mutex_destroy() routine succeeds it will return 0, otherwise an error number shall be returned indicating the error.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
136
Locking Mutexes
Function: pthread_mutex_lock() int pthread_mutex_lock(pthread_mutex_t * mutex); The pthread_mutex_lock() routine shall lock the mutex specified by mutex. If the mutex is already locked the calling thread blocks until the mutex becomes available. If the pthread_mutex_lock() routine succeeds it will return 0, otherwise an error number shall be returned indicating the error. Function: pthread_mutex_trylock() int pthread_mutex_trylock(pthread_mutex_t * mutex); The pthread_mutex_trylock() routine shall lock the mutex specified by mutex and return 0, otherwise an error number shall be returned indicating the error. In all cases the pthread_mutex_trylock() routine will not block the current running thread.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
137
Locking Mutexes
Function: pthread_mutex_timedlock() int pthread_mutex_timedlock(pthread_mutex_t * mutex,struct timespec *restrict abs_timeout); The pthread_mutex_timedlock() function shall lock the mutex object referenced by mutex. If the mutex is already locked, the calling thread shall block until the mutex becomes available as in the pthread_mutex_lock() function. If the mutex cannot be locked without waiting for another thread to unlock the mutex, this wait shall be terminated when the specified timeout expires. The timeout shall expire when the absolute time specified by abs_timeout passes, as measured by the clock on which timeouts are based (that is, when the value of that clock equals or exceeds abs_timeout), or if the absolute time specified by abs_timeout has already been passed at the time of the call.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
138
Unlocking Mutexes
Function: pthread_mutex_unlock() int pthread_mutex_unlock(pthread_mutex_t * mutex); If the current thread is the owner of the mutex specifed by mutex, then the pthread_mutex_unlock() routine shall unlock the mutex. If there are any threads blocked waiting for the mutex, the the scheduler will determine which thread obtains the lock on the mutex, otherwise the mutex is available to the next thread that calls the routine pthread_mutex_lock(), or pthread_mutex_trylock(). If the pthread_mutex_unlock() routine succeeds it will return 0, otherwise an error number shall be returned indicating the error.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
139
Priority Inversion
2. High Priority task preempts low priority task
99 Task Priority
3. Hi Priority task block on mutex
50
4. Medium Priority task preempts low priority task and high priority task
3
1. Low Priority task takes mutex
Time
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
140
If the kernel version you're using is not patched, make sure to protect against this scenario in design
One possible way: raise the priority of each tasks trying to grab a mutex to the maximum priority of all possible contenders.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
141
142
The condition variable function calls are used within an area protected by the mutex that belong to the condition variable. The operating system releases the mutex every time it blocks a task on the condition variable; and it has locked the mutex again when it unblocks the calling task from the signaling call.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
143
POSIX Semaphores
#include <semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned int value); int sem_wait(sem_t * sem); int sem_trywait(sem_t * sem); int sem_post(sem_t * sem); int sem_getvalue(sem_t * sem, int * sval); int sem_destroy(sem_t * sem);
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
144
145
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
146
SysV Mailboxes
key_t ftok(const char *pathname, int proj_id); int msgget(key_t key, int msgflg); int msgsnd(int msqid, struct msgbuf *msgp, size_t msgsz, int msgflg); ssize_t msgrcv(int msqid, struct msgbuf *msgp, size_t msgsz, long msgtyp, int msgflg); Ftok generates a key from the pathname and project id. Msgget creates a Mailbox from the key Msgsnd sends a message and msgrcv retrieves it. The different flags can control messages types (priorities), if receiving is a blocking or non blocking operations and mailbox permissions.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
147
POSIX Pipes
int pipe(int filedes[2]); pipe creates a pair of file descriptors, pointing to a pipe inode, and places them in the array pointed to by filedes. filedes[0] is forreading, filedes[1] is for writing. FILE *popen(const char *command, const char *type); The popen() function opens a process by creating a pipe, forking, and invoking the shell. Since a pipe is by definition unidirectional, the type argument may specify only reading or writing, not both; the resulting stream is correspondingly readonly or write-only. A named version of pipes, called fifo also exists.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
148
149
Signals
typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler); int kill(pid_t pid, int sig); The signal() system call installs a new signal handler for the signal with number signum. The signal handler is set to sighandler which may be a user specified function, or either SIG_IGN or SIG_DFL. Upon arrival of a signal, if the corresponding handler is set to SIG_IGN, then the signal is ignored. If the handler is set to SIG_DFL, then the default action associated to the signal occurs. Finally, if the handler is set to a function sighandler then first either the handler is reset to SIG_DFL or an implementation-dependent blocking of the signal is performed and next sighandler is called with argument signum.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
150
Regular Signals
SIGHUP SIGINT SIGQUIT SIGILL SIGABRT SIGFPE SIGKILL SIGSEGV SIGPIPE SIGALRM SIGTERM SIGUSR1 SIGUSR2 SIGCHLD SIGCONT SIGSTOP SIGTSTP SIGTTIN SIGTTOU
Hangup detected on controlling terminal Interrupt from keyboard Quit from keyboard Illegal Instruction Abort signal from abort(3) Floating point exception Kill signal Invalid memory reference Broken pipe: write to pipe with no readers Timer signal from alarm(2) Termination signal User-defined signal 1 User-defined signal 2 Child stopped or terminate Continue if stopped Stop process Stop typed at tty tty input for background process tty output for background process
SIGBUS Bus error (bad memory access) SIGPOLL Pollable event (Sys V). Synonym of SIGIO SIGPROF Profiling timer expired SIGSYS Bad argument to routine (SVID) SIGTRAP Trace/breakpoint trap SIGURG Urgent condition on socket (4.2 BSD) SIGIO I/O now possible (4.2 BSD) SIGPWR Power failure (System V) SIGWINCH Window resize signal (4.3 BSD, Sun)
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
151
No queuing
If the same signal is sent multiple times until a task processes it, it is only delivered once.
No priority
Multiple signals delivered according to order sent.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
152
No predefined meaning
But LinuxThreads lib makes use of the first 3.
Multiple instances of the same signals are queued. Value can be sent with the signal. Priority is guaranteed: lowest number real time signals are delivered first. Same signals are delivered according to order they were sent. Regular signals have higher priority then real time signals.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
153
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
154
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
155
Processes Timers
#include <sys/time.h> int getitimer(int which, struct itimerval *value); int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue);
The system provides each process with three interval timers, each decrementing in a distinct time domain. When any timer expires, a signal is sent to the process, and the timer (potentially) restarts.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
156
Timers Expiry
The timer interval is controlled by the following structures:
struct itimerval { struct timeval it_interval; /* next value */ struct timeval it_value; /* current value */ }; struct timeval { long tv_sec; long tv_usec; }; /* seconds */ /* microseconds */
Timers decrement from it_value to zero, generate a signal, and reset to it_interval. A timer which is set to zero (it_value is zero or the timer expires and it_interval is zero) stops.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
157
Time Domains
ITIMER_REAL
Decrements in real time, and delivers SIGALRM upon expiration.
ITIMER_VIRTUAL
Decrements only when the process is executing, and delivers SIGVTALRM upon expiration.
ITIMER_PROF
Decrements both when the process executes and when the system is executing on behalf of the process. SIGPROF is delivered upon expiration.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
158
More on Timers
Timers will never expire before the requested time, instead expiring some short, constant time afterwards, dependent on the system timer resolution. If the timer expires while the process is active (always true for ITIMER_VIRT) the signal will be delivered immediately when generated. Otherwise the delivery will be offset by a small time dependent on the system loading. An alternate interface called timer_create() allows specifying which signal will be sent (utilize real time signals) and spawning of a thread on timer expiry.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
159
Going further
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
160
Command help
Some Unix commands and most GNU / Linux commands offer at least one help argument: -h (- is mostly used to introduce 1-character options) --help (-- is always used to introduce the corresponding long option name, which makes scripts easier to understand) You also often get a short summary of options when you input an invalid argument.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
161
Manual pages
man <keyword> Displays one or several manual pages for <keyword> man man Most available manual pages are about Unix commands, but some are also about C functions, headers or data structures, or even about system configuration files! man stdio.h man fstab (for /etc/fstab) Manual page files are looked for in the directories specified by the MANPATH environment variable.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
162
Info pages
In GNU, man pages are being replaced by info pages. Some manual pages even tell to refer to info pages instead. info <command> info features:
Documentation structured in sections (nodes) and subsections (subnodes) Possibility to navigate in this structure: top, next, prev, up Info pages generated from the same texinfo source as the HTML documentation pages
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
163
Kernel overview
Linux features
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
164
Linux 2.6
2 years old stable Linux release! Support from the Linux development community and all commercial vendors. Now mature and more exhaustive. Most drivers upgraded. Cutting edge features and increased performance.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
165
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
166
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
167
Kernel overview
Kernel code
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
168
Implemented in C
Implemented in C like all Unix systems. (C was created to implement the first Unix systems) A little Assembly is used too: CPU and machine initialization, critical library routines. See http://www.tux.org/lkml/#s15-3 for reasons for not using C++ (main reason: the kernel requires efficient code).
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
169
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
170
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
171
No C library
The kernel has to be standalone and can't use user-space code. Userspace is implemented on top of kernel services, not the opposite. Kernel code has to supply its own library implementations (string utilities, cryptography, uncompression ...) So, you can't use standard C library functions in kernel code. (printf(), memset(), malloc()...). You can also use kernel C headers. Fortunately, the kernel provides similar C functions for your convenience, like printk(), memset(), kmalloc() ...
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
172
Kernel Stack
Very small and fixed stack.
2 page stack (8k), per task. Or 1 page stack, per task and one for interrupts. 2.6 Chosen in build time via menu. Not for all architectures
For some architectures, the kernel provides debug facility to detect stack overruns.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
173
Managing endianism
Linux supports both little and big endian architectures Each architecture defines __BIG_ENDIAN or __LITTLE_ENDIAN in <asm/byteorder.h> Can be configured in some platforms supporting both. To make your code portable, the kernel offers conversion macros (that do nothing when no conversion is needed). Most useful ones: u32 cpu_to_be32(u32); // CPU byte order to big endian u32 cpu_to_le32(u32); // CPU byte order to little endian u32 be32_to_cpu(u32); // Little endian to CPU byte order u32 le32_to_cpu(u32); // Big endian to CPU byte order
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
174
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
175
Kernel log
Printing to the kernel log is done via the printk function. The kernel keeps the messages in a circular buffer (so that doesn't consume more memory with many messages) Kernel log messages can be accessed from user space through system calls, or through /proc/kmsg Kernel log messages are also displayed in the system console.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
176
printk
The printk function: Similar to stdlib's printf(3) No floating point format. Log message are prefixed with a <1>, where the number denotes severity, from 1 (most severe) to 8. Macros are defined to be used for severity levels: KERN_EMERG, KERN_ALERT, KERT_CRIT, KERN_ERR, KERN_WARNING, KERN_NOTICE, KERN_INFO, KERN_DEBUG. Usage example:
printk(KERN_DEBUG Hello World number %d\n, num);
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
177
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
178
Linked Lists
Many constructs use doubly-linked lists. List definition and initialization:
struct list_head mylist = LIST_HEAD_INIT(mylist);
or
LIST_HEAD(mylist);
or
INIT_LIST_HEAD(&mylist);
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
179
List Manipulation
List definition and initialization:
void list_add(struct list_head *new, struct list_head *head); void list_add_tail(struct list_head *new, struct list_head *head); void list_del(struct list_head *entry); void list_del_init(struct list_head *entry); void list_move(struct list_head *list, struct list_head *head); void list_add_tail(struct list_head *list, struct list_head *head);
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
180
In 2.6, there are variants of these API's for RCU protected lists (see section about Locks ahead).
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
181
List Iteration
Lists also have iterator macros defined:
list_for_each(pos, head); list_for_each_prev(pos, head); list_for_each_safe(pos, n, head); list_for_each_entry(pos, head, member);
Example:
struct mydata *pos; list_for_each_entry(pos, head, dev_list) { pos->some_data = 0777; }
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
182
Kernel overview
Kernel subsystems
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
183
Kernel architecture
User space
Kernel space
Hardware
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
184
The mode the CPU is in determines which instructions the CPU is willing to execute:
Sensitive instructions will not be executed when the CPU is in user mode.
The CPU mode is determined by one of the CPU registers, which stores the current Ring Level
0 for supervisor mode, 3 for user mode, 1-2 unused by Linux.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
185
186
Kernel
Task
Glibc Task
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
187
location
Userspace memory management Used to implement: - memory protection - stack growth - memory swapping to disk
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
188
Kernel overview
Linux versioning scheme and development process
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
189
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
190
Even number
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
191
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
192
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
193
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
194
The official list of changes for each Linux release is just a huge list of individual patches! Very difficult to find out the key changes and to get the global picture out of individual changes.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
195
??!
For each new kernel release, you can also get the changes in the kernel internal API: http://lwn.net/Articles/2.6-kernel-api/ What's next? Documentation/feature-removal-schedule.txt lists the features, subsystems and APIs that are planned for removal (announced 1 year in advance).
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
196
Kernel overview
Kernel user interface
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
197
Mounting /proc: mount -t proc none /proc Mounting /sys: mount -t sysfs none /sys
Filesystem type
Mount point Raw device or filesystem image In the case of virtual filesystems, any string is fine
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
198
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
199
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
200
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
201
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
202
kernel.org
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
203
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
204
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
205
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
206
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
207
If blocked by your firewall, look for 0x517D0F0E on http://pgp.mit.edu/ , copy and paste the key to a linuxkey.txt file: gpg --import linuxkey.txt Check the signature of files: gpg --verify linux-2.6.11.12.tar.bz2.sign
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
208
# *DOCUMENTATION*
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
209
zcat diff_file.gz | patch -p<n> n: number of directory levels to skip in the file paths
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
210
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
211
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
212
Update your copy whenever needed (Linus tree example): cd linux-2.6 cg-update origin More details available on http://git.or.cz/ or http://linux.yyz.us/git-howto.html
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
213
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
214
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
215
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
216
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
217
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
218
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
219
LXR screenshot
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
220
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
221
Kernel configuration file (Makefile syntax) stored in the .config file at the root of the kernel sources Distribution kernel config files usually released in /boot/
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
222
Makefile changes
To identify your kernel image with others build from the same sources, use the EXTRAVERSION variable: VERSION = 2 PATCHLEVEL = 6 SUBLEVEL = 15 EXTRAVERSION = -acme1 uname -r will return: 2.6.15-acme1
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
223
make xconfig
make xconfig New Qt configuration interface for Linux 2.6. Much easier to use than in Linux 2.4! Make sure you read help -> introduction: useful options! File browser: easier to load configuration files
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
224
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
225
226
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
227
make oldconfig
make oldconfig Needed very often! Useful to upgrade a .config file from an earlier kernel release Issues warnings for obsolete symbols Asks for values for new symbols If you edit a .config file by hand, it's strongly recommended to run make oldconfig afterwards!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
228
make allnoconfig
make allnoconfig Only sets strongly recommended settings to y. Sets all other settings to n. Very useful in embedded systems to select only the minimum required set of features and drivers. Much more convenient than unselecting hundreds of features one by one!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
229
make help
make help Lists all available make targets Useful to get a reminder, or to look for new or advanced options!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
230
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
231
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
232
Dependency management
When you modify a regular kernel source file, make only rebuilds what needs recompiling. That's what it is used for. However, the Makefile is quite pessimistic about dependencies. When you make significant changes to the .config file, make often redoes much of the compile job!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
233
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
234
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
235
Generated files
Created when you run the make command vmlinux Raw Linux kernel image, non compressed. arch/<arch>/boot/zImage zlib compressed kernel image (default image on arm)
arch/<arch>/boot/bzImage (default image on i386) Also a zlib compressed kernel image. Caution: bz means big zipped but not bzip2 compressed! (bzip2 compression support only available on i386 as a tactical patch. Not very attractive for small embedded systems though: consumes 1 MB of RAM for decompression).
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
236
237
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
238
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
239
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
240
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
241
Makefile changes
Update the version as usual You should change the default target platform. Example: ARM platform, cross-compiler command: arm-linux-gcc ARCH ?= arm CROSS_COMPILE ?= arm-linux(The Makefile defines later CC = $(CROSS_COMPILE)gcc) See comments in Makefile for details
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
242
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
243
arch/arm/configs example
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
244
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
245
Cross-compiling setup
Example If you have an ARM cross-compiling toolchain in /usr/local/arm/3.3.2/ You just have to add it to your Unix search path: export PATH=/usr/local/arm/3.3.2/bin:$PATH Choosing a toolchain See the Documentation/Changes file in the sources for details about minimum tool versions requirements.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
246
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
247
Cross-compiling summary
Edit Makefile: set ARCH, CROSS_COMPILE and EXTRA_VERSION Get the default configuration for your machine: make <machine>_defconfig (if existing in arch/<arch>/configs) Refine the configuration settings according to your requirements: make xconfig Add the crosscompiler path to your PATH environment variable Compile the kernel: make Copy the kernel image from arch/<arch>/boot/ to the target Copy modules to a directory which you replicate on the target: make INSTALL_MOD_PATH=<dir> modules_install
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
248
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
249
Kernel
- Uncompresses itself - Initializes the kernel core and statically compiled drivers (needed to access the root filesystem) - Mounts the root filesystem (specified by the init kernel parameter) - Executes the first userspace program
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
250
Kernel
- Uncompresses itself - Initializes the kernel core and statically compiled drivers - Uncompresses the initramfs cpio archive included in the kernel file cache (no mounting, no filesystem). - If found in the initramfs, executes the first userspace program: /init
Userspace: /sbin/init
- Runs commands to configure the device (if not done yet in the initramfs) - Starts up system services (daemons, servers) and user programs
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
251
Kernel
- Uncompresses itself - Initializes statically compiled drivers - Uncompresses the initramfs cpio archive included in the kernel. Mounts it. No /init executable found. - So falls back to the old way of trying to locate and mount a root filesystem. - Mounts the root filesystem specified by the init kernel parameter (initrd in our case) - Executes the first userspace program: usually /linuxrc - Runs userspace commands to configure the device (such as network setup, mounting /proc and /sys...) - Loads kernel modules (drivers) stored in the initrd, needed to access the new root filesystem. - Mounts the new root filesystem. Switch to it (pivot_root) - Runs /sbin/init (or sometimes a new /linuxrc script)
Userspace: /sbin/init
- Runs commands to configure the device (if not done yet in the initrd) - Starts up system services (daemons, servers) and user programs
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
252
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
253
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
254
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
255
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
256
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
257
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
258
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
259
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
260
261
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
262
x86 bootloaders
LILO: LInux LOad. Original Linux bootloader. Still in use! http://freshmeat.net/projects/lilo/ Supports: x86 GRUB: GRand Unified Bootloader from GNU. More powerful. http://www.gnu.org/software/grub/ Supports: x86 SYSLINUX: Utilities for network and removable media booting http://syslinux.zytor.com Supports: x86
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
263
Generic bootloaders
Das U-Boot: Universal Bootloader from Denk Software The most used on arm. http://u-boot.sourceforge.net/ Supports: arm, ppc, mips, x86 RedBoot: eCos based bootloader from Red-Hat http://sources.redhat.com/redboot/ Supports: x86, arm, ppc, mips, sh, m68k... uMon: MicroMonitor general purpose, multi-OS bootloader http://microcross.com/html/micromonitor.html Supports: ARM, ColdFire, SH2, 68K, MIPS, PowerPC, Xscale...
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
264
Other bootloaders
LAB: Linux As Bootloader, from Handhelds.org http://handhelds.org/cgi-bin/cvsweb.cgi/linux/kernel26/lab/ Idea: use a trimmed Linux kernel with only features needed in a bootloader (no scheduling, etc.). Reuses flash and filesystem access, LCD interface, without having to implement bootloader specific drivers. Supports: arm (still experimental) And many more: lots of platforms have their own!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
265
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
266
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
267
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
268
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
269
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
270
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
271
Booting variants
XIP (Execute In Place) The kernel image is directly executed from the storage Can be faster and save RAM However, the kernel image can't be compressed No initramfs / initrd Directly mounting the final root filesystem (root kernel command line option) No new root filesystem Running the whole system from the initramfs
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
272
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
273
Start or restart your NFS server: Fedora Core: /etc/init.d/nfs restart Debian (Knoppix, KernelKit): /etc/init.d/nfs-user-server restart
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
274
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
275
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
276
/linuxrc
1 of the 2 default init programs (if no init parameter is given to the kernel) Traditionally used in initrds or in simple systems not using /sbin/init. Is most of the time a shell script, based on a very lightweight shell: nash or busybox sh This script can implement complex tasks: detecting drivers to load, setting up networking, mounting partitions, switching to a new root filesystem...
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
277
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
278
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
279
Example devices: keyboards, mice, parallel port, IrDA, Bluetooth port, consoles, terminals, sound, video...
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
280
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
281
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
282
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
283
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
284
Driver development
Loadable kernel modules
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
285
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
286
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
287
Module dependencies
Module dependencies stored in /lib/modules/<version>/modules.dep They don't have to be described by the module writer. They are automatically computed during kernel building from module exported symbols. module2 depends on module1 if module2 uses a symbol exported by module1. You can update the modules.dep file by running (as root) depmod -a [<version>]
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
288
hello module
/* hello.c */ #include <linux/init.h> #include <linux/module.h> #include <linux/kernel.h> static int __init hello_init(void) { printk(KERN_ALERT "Good morrow"); printk(KERN_ALERT "to this fair assembly.\n"); return 0; } static void __exit hello_exit(void) { printk(KERN_ALERT "Alas, poor world, what treasure"); printk(KERN_ALERT "hast thou lost!\n"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Greeting module"); MODULE_AUTHOR("William Shakespeare");
__init: removed after initialization (static kernel or module). __exit: discarded when module compiled statically into the kernel.
289
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
290
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
291
Compiling a module
The below Makefile should be reusable for any Linux 2.6 module. Just run make to build the hello.ko file Caution: make sure there is a [Tab] character at the beginning of the $(MAKE) line (make syntax)
# Makefile for the hello module obj-m := hello.o KDIR := /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) default: $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules Either - full kernel source directory (configured and compiled) - or just kernel headers directory (minimum needed )
292
293
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
294
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
295
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
296
Driver development
Module parameters
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
297
298
299
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
300
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
301
Embedded Linux
Driver development
Using the proc file system interface
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
302
The proc file name The proc_dir _entry struct Callback function page is the buffer we write to Setting *eof to 1 means end of file. start is set to a pointer to where we wrote second parameters is file permission, last parameter is a handle of directory. Don't forget to check for NULL here!
int mymodule_proc_read(char *page, char **start, off_t off, int count, int *eof, void *data) { int len = 0; len += sprintf(page + len, "io=%d\n", io); len += sprintf(page + len, "irq=%d\n", irq); if (len <= off+count) *eof = 1; *start = page + off; len -= off; if (len > count) len = count; if (len < 0) len = 0; return len;
int __init startup_mymodule(void) { my_proc_dir = create_proc_entry(MYNAME, 0, NULL); my_proc_dir->read_proc = mymodule_proc_read; return 0; } void __exit shutdown_mymodule(void) { remove_proc_entry(MYNAME, NULL); }
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
303
304
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
305
Driver development
Adding sources to the kernel tree
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
306
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
307
or
obj-y += acme_drivers/ (several conditions)
Run make xconfig and see your new options! Run make and your new files are compiled!
See Documentation/kbuild/ for details
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
308
Thanks to Nicolas Rougier (Copyright 2003, http://webloria.loria.fr/~rougier/) for the Tux image
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
309
Driver development
Memory management
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
310
Physical Memory
In ccNUMA1 machines:
The memory of each node is represented in pg_data_t These memories are linked into pgdat_list
If you don't know which of these is your machine, you're using a uniform memory access system :-)
1
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
311
Memory Zones
Each pg_data_t is split to three zones Each zone has different properties:
ZONE_DMA
DMA operations on address limited busses is possible
ZONE_NORMAL
Maps directly to linear addressing (<~1Gb on i386) Always mapped to kernel space.
ZONE_HIMEM
Rest of memory. Mapped into kernel space on demand.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
312
I/O memory 3
Kernel Process1
Flash
MMU
Memory Management Unit
CPU
0xFFFFFFFFF
RAM 1 RAM 0
All the processes have their own virtual address space, and run as if they had access to the whole address space.
Process2
0x00000000
0x00000000
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
313
I/O memory
3rd Gb
2nd Gb
0x00000000
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
Zero Page
0x00000000
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
314
Address Types
Physical address
Physical memory as seen from the CPU, with out MMU1 translation.
Bus address
Physical memory as seen from device bus. May or may not be virtualized (via IOMMU, GART, etc).
Virtual address
Memory as seen from the CPU, with MMU1 translation.
1
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
315
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
316
The MMU
Task 12 12 15 Virtual Physical Permission 0x8000 0x8001 0x8000 0x5340 0x1000 0x3390 RWX RX RX
CPU
MMU
Memory
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
317
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
318
kmalloc features
Quick (unless it's blocked waiting for memory to be freed). Doesn't initialize the allocated area. You can use kcalloc or kzalloc to get zeroed memory. The allocated area is contiguous in physical RAM. Allocates by 2n sizes, and uses a few management bytes. So, don't ask for 1024 when you need 1000! You'd get 2048! Caution: drivers shouldn't try to kmalloc more than 128 KB (upper limit in some architectures).
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
319
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
320
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
321
Slab caches
Also called lookaside caches Slab: name of the standard Linux memory allocator Slab caches: Objects that can hold any number of memory areas of the same size. Optimum use of available RAM and reduced fragmentation. Mainly used in Linux core subsystems: filesystems (open files, inode and file caches...), networking... Live stats on /proc/slabinfo. May be useful in device drivers too, though not used so often. Linux 2.6: used by USB and SCSI drivers.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
322
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
323
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
324
Memory pools
Useful for memory allocations that cannot fail Kind of lookaside cache trying to keep a minimum number of pre-allocated objects ahead of time. Use with care: otherwise can result in a lot of unused memory that cannot be reclaimed! Use other solutions whenever possible.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
325
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
326
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
327
Yes Yes mempool_free pool count < min_nr? No Call free function on object Add freed object to pool New object
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
328
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
329
Allocating by pages
More appropriate when you need big slices of RAM: unsigned long get_zeroed_page(int flags); Returns a pointer to a free page and fills it up with zeros unsigned long __get_free_page(int flags); Same, but doesn't initialize the contents unsigned long __get_free_pages(int flags, unsigned long order); Returns a pointer on a memory zone of several contiguous pages in physical RAM. order: log2(<number_of_pages>) maximum: 8192 KB (MAX_ORDER=11 in linux/mmzone.h)
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
330
Freeing pages
void free_page(unsigned long addr); void free_pages(unsigned long addr, unsigned long order); Need to use the same order as in allocation.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
331
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
332
Buddy System 1
16 Mb
We need 8 Mb of memory, but don't find an exact match. We do have a block of 16 Mb memory though.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
333
Buddy System 2
8 Mb
8 Mb
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
334
Buddy System 3
8 Mb
8 Mb
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
335
Buddy System 4
8 Mb
8 Mb
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
336
Buddy System 5
16 Mb We can once again combine the two blocks in a single 16 Mb free block. Because of the order of 2 allocation, it's easy to spot our buddy.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
337
vmalloc
vmalloc can be used to obtain contiguous memory zones in virtual address space (even if pages may not be contiguous in physical memory). void *vmalloc(unsigned long size); void vfree(void *addr);
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
338
Memory utilities
void * memset(void * s, int c, size_t count); Fills a region of memory with the given value. void * memcpy(void * dest, const void *src, size_t count); Copies one area of memory to another. Use memmove with overlapping areas. Lots of functions equivalent to standard C library ones defined in include/linux/string.h
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
339
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
340
Driver development
I/O memory and ports
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
341
struct resource *request_region( unsigned long start, unsigned long len, char *name); Tries to reserve the given region and returns NULL if unsuccessful. Example: request_region(0x0170, 8, "ide1"); void release_region( unsigned long start, unsigned long len); See include/linux/ioport.h and kernel/resource.c
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
342
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
343
word strings
void insw(unsigned port, void *addr, unsigned long count); void outsw(unsigned port, void *addr, unsigned long count);
long strings
void inbsl(unsigned port, void *addr, unsigned long count); void outsl(unsigned port, void *addr, unsigned long count);
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
344
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
345
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
346
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
347
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
348
Hardware independent
#include <asm/kernel.h> void barrier(void);
Hardware dependent
#include <asm/system.h> void rmb(void); void wmb(void); Only impacts the behavior of the compiler. Doesn't prevent reordering void mb(void); Safe on all architectures! in the processor!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
349
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
350
/dev/mem
Used to provide user-space applications with direct access to physical addresses. Actually only works with addresses that are non-RAM (I/O memory) or with addresses that have some special flag set in the kernel's data structures. Fortunately, doesn't provide access to any address in physical RAM! Used by applications such as the X server to write directly to device memory.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
351
Driver development
Character drivers
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
352
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
353
User-space needs The name of a device file in /dev to interact with the device driver through regular file operations (open, read, write, close...) The kernel needs
Copy from user Copy to user Read buffer read /dev/foo Write string write
To know which driver is in charge of device files with a given major / minor number pair For a given driver, to have handlers (file operations) to execute when user-space opens, reads, writes or closes the device file.
major / minor
Read handler
Write handler
354
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
355
number
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
name
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
356
dev_t structure
Kernel data structure to represent a major / minor pair Defined in <linux/kdev_t.h> Linux 2.6: 32 bit size (major: 12 bits, minor: 20 bits) Macro to create the structure: MKDEV(int major, int minor); Macro to extract the numbers: MAJOR(dev_t dev); MINOR(dev_t dev);
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
357
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
358
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
359
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
360
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
361
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
362
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
363
The user-space address may be swapped out to disk The user-space address may be invalid (user space process trying to access unauthorized data)
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
364
365
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
366
Read method
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
367
Write method
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
368
You just need to supply the functions you implemented! Defaults for other functions (such as open, release...) are fine if you do not implement anything special.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
369
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
370
Example (continued):
if (cdev_add(acme_cdev, acme_dev, acme_count)) { printk (KERN_ERR Char driver registration failed\n);
...
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
371
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
372
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
373
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
374
Show how to handle errors and deallocate resources in the right order!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
375
System administration
System user
- Open the device file, read, write, or send ioctl's to it.
Kernel
- Executes the corresponding file operations
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
Kernel
User-space
- Load the character driver module - In /proc/devices, find the major number it uses. - Create the device file with this major number The device file is ready to use!
Kernel
376
Driver development
Debugging
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
377
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
378
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
379
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
380
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
381
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
382
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
383
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
384
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
385
Regular RAM
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
386
Kernel Oops
Caused by an exception in the kernel code itself. The kernel issues a diagnostic messaged, called an Oops!. The message contains debug information, such as register content, stack trace, code dump etc. After the message is sent to the log and console From within a task the kernel kills the task. From interrupt the kernel panics and may reboot automatically if given a-priori the panic=secs parameter.
In 2.4, stack trace addresses can be translated via the ksymoops utility. In 2.6, with the CONFIG_KALLSYMS option on, the kernel does translation internally.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
387
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
388
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
389
Driver development
Concurrent access to resources
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
390
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
391
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
392
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
393
Kernel semaphores
Also called mutexes (Mutual Exclusion)
V: Verhoog Increment in Dutch
V (up)
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
394
Initializing a semaphore
Statically DECLARE_MUTEX(name); DECLARE_MUTEX_LOCKED(name); Dynamically void init_MUTEX(struct semaphore *sem); void init_MUTEX_LOCKED(struct semaphore *sem);
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
395
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
396
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
397
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
398
Spinlocks
Locks to be used for code that can't sleep (critical sections, interrupt handlers... Be very careful not to call functions which can sleep! Intended for multiprocessor systems Spinlocks are not interruptible, don't sleep and keep spinning in a loop until the lock is available.
Spinlock Still locked?
Spinlocks cause kernel preemption to be disabled on the CPU executing them. May require interrupts to be disabled too.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
399
Initializing spinlocks
Static spinlock_t my_lock = SPIN_LOCK_UNLOCKED; Dynamic void spin_lock_init (spinlock_t *lock);
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
400
Using spinlocks
void spin_[un]lock (spin_lock_t *lock); void spin_[un]lock_irqsave (spin_lock_t *lock, unsigned long flags); Disables IRQs on the local CPU void spin_[un]lock_irq (spin_lock_t *lock); Disables IRQs without saving flags. When you're sure that nobody already disabled interrupts. void spin_[un]lock_bh (spin_lock_t *lock); Disables software interrupts, but not hardware ones Note that reader / writer spinlocks also exist.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
401
Deadlock situations
They can lock up your system. Make sure they never happen!
Don't call a function that can try to get access to the same lock Holding multiple locks is risky!
Get lock1 Get lock1 call Get lock2 Wait for lock1
Dead Lock!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
Get lock2
Dead Lock!
Get lock1
402
Alternatives to locking
As we have just seen, locking can have a strong negative impact on system performance. In some situations, you could do without it. By using lock-free algorithms like Read Copy Update (RCU). RCU API available in the kernel (See http://en.wikipedia.org/wiki/RCU). When available, use atomic operations.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
403
Atomic variables
Useful when the shared resource is an integer value Even an instruction like n++ is not guaranteed to be atomic on all processors! Header #include <asm/atomic.h> Type atomic_t contains a signed integer (at least 24 bits)
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
404
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
405
Driver development
Processes and scheduling
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
406
407
In 2.6 an explicit notion of processes and threads was introduced to the kernel. Scheduling is still done on a thread by thread basis. The basic object the kernel works with is a task, which is analogous to a thread.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
408
task_struct
Each task is represented by a task_struct. The task is linked in the task tree via:
parent Pointer to it's parent children sibling A linked list A linked list
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
409
Task Identifiers
Each task_struct has the following identities:
PID Globally unique. Different one for each thread. TGID Thread Group Id. Returned to user space as getpid()
Shared by all threads of a process. For single threaded process == PID.
410
A process life
Parent process
Calls fork() and creates a new process The process is elected by the scheduler Task terminated but its resources are not freed yet. Waiting for its parent to acknowledge its death.
TASK_ZOMBIE
TASK_RUNNING
Ready but not running The process is preempted by to scheduler to run a higher priority task
TASK_RUNNING
Actually running
The event occurs or the process receives a signal. Process becomes runnable again
TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE
Waiting
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
411
Process context
User space programs and system calls are scheduled together
Process continuing in user space... (or replaced by a higher priority process) (can be preempted)
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
412
Kernel threads
The kernel does not only react from user-space (system calls, exceptions) or hardware events (interrupts). It also runs its own processes. Kernel space are standard processes scheduled and preempted in the same way (you can view them with top or ps!) They just have no special address space and usually run forever. Kernel thread examples: pdflush: regularly flushes dirty memory pages to disk (file changes not committed to disk yet). ksoftirqd: manages soft irqs.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
413
Process priorities
Regular processes Priorities from -20 (maximum) to 19 (minimum) Only root can set negative priorities (root can give a negative priority to a regular user process) Use the nice command to run a job with a given priority: nice -n <priority> <command> Use the renice command to change a process priority: renice <priority> -p <pid>
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
414
Real-time processes
Real-time processes can be started by root using the POSIX API Available through <sched.h> (see man sched.h for details) 100 real-time priorities available SCHED_FIFO scheduling class: The process runs until completion unless it is blocked by an I/O, voluntarily relinquishes the CPU, or is preempted by a higher priority process. SCHED_RR scheduling class: Difference: the processes are scheduled in a Round Robin way. Each process is run until it exhausts a max time quantum. Then other processes with the same priority are run, and so and so...
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
415
Timer frequency
Timer interrupts are raised every HZ th of second (= 1 jiffy) HZ is now configurable (in Processor type and features): 100, 250 (i386 default) or 1000. Supported on i386, ia64, ppc, ppc64, sparc64, x86_64 See kernel/Kconfig.hz. Compromise between system responsiveness and global throughput. Caution: not any value can be used. Constraints apply! Another idea is to completely turn off CPU timer interrupts when the system is idle (dynamic tick): see http://muru.com/linux/dyntick. This saves power. Supports arm and i386 so far.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
416
O(1) scheduler
The kernel maintains 2 priority arrays: the active and the expired array. Each array contains 140 entries (100 real-time priorities + 40 regular ones), 1 for each priority, each containing a list of processes with the same priority. The arrays are implemented in a way that makes it possible to pick a process with the highest priority in constant time (whatever the number of running processes).
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
417
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
418
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
419
Timeslices
The scheduler also prioritizes high priority processes by giving them a bigger timeslice. Initial process timeslice: parent's timeslice split in 2 (otherwise process would cheat by forking). Minimum priority: 5 ms or 1 jiffie (whichever is larger) Default priority in jiffies: 100 ms Maximum priority: 800 ms Note: actually depends on HZ. See kernel/sched.c for details.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
420
Dynamic priorities
Only applies to regular processes For a better user experience, the Linux scheduler boots the priority of interactive processes (processes which spend most of their time sleeping, and take time to exhaust their timeslices). Such processes often sleep but need to respond quickly after waking up (example: word processor waiting for key presses). Priority bonus: up to 5 points. Conversely, the Linux scheduler reduces the priority of compute intensive tasks (which quickly exhaust their timeslices). Priority penalty: up to 5 points.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
421
Driver development
Sleeping
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
422
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
423
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
424
Waking up!
Typically done by interrupt handlers when data sleeping processes are waiting for are available. wake_up(&queue); Wakes up all the waiting processes on the given queue wake_up_interruptible(&queue); Does the same job. Usually called when processes waited using wait_event_interruptible.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
425
Driver development
Interrupt management
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
426
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
427
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
428
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
429
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
430
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
431
432
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
433
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
434
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
435
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
436
IRQ_HANDLED: recognized and handled interrupt IRQ_NONE: not on a device managed by the module. Useful to share interrupt channels and/or report spurious interrupts to the kernel.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
437
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
438
Linux Contexts
Interrupt Handlers Interrupt Context
Hi prio tasklets
Scheduling Points
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
439
Softirq
A fixed set (max 32) of software interrupts (prioritized):
HI_SOFTIRQ TIMER_SOFTIRQ NET_TX_SOFTIRQ NET_RX_SOFTIRQ SCSI_SOFTIRQ TASKLET_SOFTIRQ Runs low latency tasklets Runs timers Network stack Tx Network stack Rx SCSI sub system Runs normal tasklets
Activated on return from interrupt (in do_IRQ()) Can run concurrently on SMP systems (even the same softirq).
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
440
Tasklets
Added in 2.4 Are run from softirqs (normal or low-latency) Each tasklet runs only on a single CPU (serialization) You can initialize a tasklet via:
init tasklet_init (struct tasklet_struct *t void (*func)(unsigned long), unsigned long data));
441
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
442
Tasklet Masking
Tasklets may be temporarily disabled/enabled:
tasklet_enable(&module_tasklet); tasklet_disable(&module_tasklet); tasklet_hi_enable(&module_tasklet); tasklet_disable_nosync(&module_tasklet);
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
443
Timers
Runs via softirq like tasklets But at a specific time A timer is represented by a timer_list:
struct timer_list { /* ... */ unsigned long expires; /* In Jiffies */ void (*function )(unsigned int); /* Optional */ unsigned long data; );
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
444
Timer operations
Manipulated with:
void init_timer(struct timer_list void add_timer(struct timer_list *timer); *timer);
void init_timer_on(struct timer_list *timer, int cpu); void del_timer(struct timer_list *timer); *timer); void del_timer_sync(struct timer_list
void mod_timer(struct timer_list *timer, unsigned long expires); void timer_pending(const struct timer_list *timer);
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
445
446
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
447
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
448
Disabling interrupts
May be useful in regular driver code... Can be useful to ensure that an interrupt handler will not preempt your code (including kernel preemption) Disabling interrupts on the local CPU:
unsigned long flags; // Interrupts disabled local_irq_save(flags); ... local_irq_restore(flags); // Interrupts restored to their previous state.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
449
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
450
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
451
In a training lab, somebody freed the timer interrupt handler by mistake (using the wrong irq number). The system froze. Remember the kernel is not protected against itself!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
452
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
453
Driver development
mmap
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
454
mmap (1)
Possibility to have parts of the virtual address space of a program mapped to the contents of a file!
> cat /proc/1/maps (init process) start end perm offset major:minor inode 00771000-0077f000 r-xp 00000000 03:05 1165839 0077f000-00781000 rw-p 0000d000 03:05 1165839 0097d000-00992000 r-xp 00000000 03:05 1158767 00992000-00993000 r--p 00014000 03:05 1158767 00993000-00994000 rw-p 00015000 03:05 1158767 00996000-00aac000 r-xp 00000000 03:05 1158770 00aac000-00aad000 r--p 00116000 03:05 1158770 00aad000-00ab0000 rw-p 00117000 03:05 1158770 00ab0000-00ab2000 rw-p 00ab0000 00:00 0 08048000-08050000 r-xp 00000000 03:05 571452 08050000-08051000 rw-p 00008000 03:05 571452 08b43000-08b64000 rw-p 08b43000 00:00 0 f6fdf000-f6fe0000 rw-p f6fdf000 00:00 0 fefd4000-ff000000 rw-p fefd4000 00:00 0 ffffe000-fffff000 ---p 00000000 00:00 0 mapped file name /lib/libselinux.so.1 /lib/libselinux.so.1 /lib/ld-2.3.3.so /lib/ld-2.3.3.so /lib/ld-2.3.3.so /lib/tls/libc-2.3.3.so /lib/tls/libc-2.3.3.so /lib/tls/libc-2.3.3.so /sbin/init (text) /sbin/init (data, stack)
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
455
mmap (2)
Particularly useful when the file is a device file! Allows to access device I/O memory and ports without having to go through (expensive) read, write or ioctl calls! X server example (maps excerpt)
start end 08047000-081be000 081be000-081f0000 ... f4e08000-f4f09000 f4f09000-f4f0b000 f4f0b000-f6f0b000 f6f0b000-f6f8b000 perm offset major:minor inode r-xp 00000000 03:05 310295 rw-p 00176000 03:05 310295 rw-s rw-s rw-s rw-s e0000000 4281a000 e8000000 fcff0000 03:05 03:05 03:05 03:05 655295 655295 652822 652822 mapped file name /usr/X11R6/bin/Xorg /usr/X11R6/bin/Xorg /dev/dri/card0 /dev/dri/card0 /dev/mem /dev/mem
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
456
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
457
Initialize the mapping. Can be done in most cases with the remap_pfn_range() function, which takes care of most of the job.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
458
remap_pfn_range()
pfn: page frame number The most significant bits of the page address (without the bits corresponding to the page size). #include <linux/mm.h> int remap_pfn_range( struct vm_area_struct *, /* VMA struct */ unsigned long virt_addr, /* Starting user virtual address */ unsigned long pfn, /* pfn of the starting physical address */ unsigned long size, /* Mapping size */ pgprot_t /* Page permissions */ );
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
459
460
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
461
Filesystems are either made for block or MTD storage devices. See Documentation/filesystems/ for details.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
462
I/O schedulers
Mission of I/O schedulers: re-order reads and writes to disk to minimize disk head moves (time consuming!)
Faster
463
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
464
Journaled filesystems
Designed to stay in a correct state even after system crashes or a sudden power-off All writes are first described in the journal before being committed to files
Application
User-space Kernel space (filesystem) Write to file
Write to file
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
465
Journal empty?
Thanks to the journal, the filesystem is never left in a corrupted state Recently saved data could still be lost
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
466
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
467
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
468
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
469
ramdisk filesystems
Useful to store temporary data not kept after power off or reboot: system log files, connection data, temporary files... Traditional block filesystems: journaling not needed. Many drawbacks: fixed in size. Remaining space not usable as RAM. Files duplicated in RAM (in the block device and file cache)! tmpfs (Config: File systems -> Pseudo filesystems) Doesn't waste RAM: grows and shrinks to accommodate stored files Saves RAM: no duplication; can swap out pages to disk when needed. See Documentation/filesystems/tmpfs.txt in kernel sources.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
470
ext3
read-write user and configuration data
tmpfs
read-write volatile data
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
471
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
472
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
473
It's very likely that your standard kernel misses one of these modules. Check the corresponding .c file in the kernel sources and look in the corresponding Makefile which option you need to recompile your kernel with.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
474
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
475
noatime: doesn't write access time information in file inodes sync: to perform writes immediately (reduce power down failure risks)
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
476
Storage type?
Read-only files?
Contains flash?
No
Volatile data?
No
choose Squashfs
read-only
Choose tmpfs
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
477
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
478
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
479
xxx
xxx
xxx
xxx
Memory Access
Driver
Rx
Free
Free
RcvOk
xxx
DMA
xxx
xxx
xxx
Driver allocates Ring Buffers. Driver resets descriptors to initial state. Driver puts packet to be sent in Tx buffers. Device puts received packet in Rx buffers. Driver/Device update descriptors to indicate state. Usually, device indicates Rx and end of Tx with interrupt, unless polling or NAPI.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
480
481
Socket Buffers
We need to manipulate packets through the stack This manipulation involves efficiently:
Adding protocol headers/trailers down the stack. Removing protocol headers/trailers up the stack.
Packets can be chained together. Each protocol should have convenient access to header fields. To do all this the kernel provides the sk_buff structure.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
482
sk_buff
An sk_buff represents a single packet. This struct is passed through the protocol stack. It holds pointers to a buffer with the packet data:
sk_buff mac head data nh tcp h tail telnet tailroom 483 end
headroom mac ip
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
sk_buff Manipulation
Manipulate sk_buff:
unsigned char *skb_put(struct sk_buff * skb, unsigned int len); tail += len unsigned char *skb_push(struct sk_buff * skb, unsigned int len); data -= len unsigned char *skb_pull(struct sk_buff * skb, unsigned int len); data += len
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
484
sk_buff Manipulation
Manipulate sk_buff:
int skb_headroom(const struct sk_buff *skb); data - head int skb_tailroom(const struct sk_buff *skb); end - tail int skb_reserve(const struct sk_buff *skb, unsigned int len); tail = (data +=len)
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
485
sk_buff Allocation
Low level allocation is done via:
struct sk_buff *alloc_skb(unsigned int size, int gfp_mask);
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
486
/* Mark as being used by this device */ skb->dev = dev; /* Align IP on 16 byte boundaries */ skb_reserve(skb, 2);
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
487
Softnet
Was introduced in kernel 2.4.x Parallelize packet handling on SMP machines Packet transmit/receive is handled via two softirqs:
NET_TX_SOFTIRQ Feeds packets from network stack to driver. NET_RX_SOFTIRQ Feeds packets from driver to network stack.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
488
Packet Reception
The driver:
Allocates an skb. sets up a descriptor in the ring buffers for the hardware.
Later net_rx_action() is called by NET_RX_SOFTIEQ, which calls the driver poll() method to feed the packet up.
Normally poll() is set to proccess_backlog() by net_dev_init()
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
489
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
490
Packet Transmission
Each network device defines a method:
int (*hard_start_xmit) (struct sk_buff *skb, struct net_device *dev);
This method is indirectly called from the NET_TX_SOFTIRQ Calls to this method are serialized via dev->xmit_lock_owner The driver can manage the transmit queue:
void netif_start_queue(struct net_device *net); void netif_stop_queue(struct net_device *net); void netif_wake_queue(struct net_device *net); int netif_queue_stopped(struct net_device *net);
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
491
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
492
size size of our priv data part mask a naming pattern (e.g. eth%d) setup_func A function to prepare the rest of net_device.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
493
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
494
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
495
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
496
NAPI
Net network API Optional provides interrupt mitigation under high load Requirements:
A DMA ring buffer. Ability to turn off receive interrupts.
Called by the network stack periodically when signaled by the driver to do so.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
497
NAPI (cont.)
When a receive interrupt occurs, driver:
Turns off receive interrupts. Calls netif_rx_schedule(dev) to get stack to start calling it's poll method.
Poll method
Scans receive ring buffers, feeding packets to the stack via: netif_receive_skb(skb). If work finished within budget parameter, re-enables interrupts and calls netif_rex_complete(dev) Else, stack will call poll method again.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
498
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
499
Solving issues
If you face an issue, and it doesn't look specific to your work but rather to the tools you are using, it is very likely that someone else already faced it. Search the Internet for similar error reports On web sites or mailing list archives (using a good search engine) On newsgroups: http://groups.google.com/ You have great chances of finding a solution or workaround, or at least an explanation for your issue. Otherwise, reporting the issue is up to you!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
500
Getting help
If you have a support contract, ask your vendor Otherwise, don't hesitate to share your questions and issues on mailing lists
Either contact the Linux mailing list for your architecture (like linuxarm-kernel or linuxsh-dev...) Or contact the mailing list for the subsystem you're dealing with (linux-usb-devel, linux-mtd...). Don't ask the maintainer directly! Most mailing lists come with a FAQ page. Make sure you read it before contacting the mailing list Refrain from contacting the Linux Kernel mailing list, unless you're an experienced developer and need advice
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
501
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
502
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
503
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
504
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
505
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
506
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
507
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
508
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
512
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
513
ARM resources
ARM Linux project: http://www.arm.linux.org.uk/ Developer documentation: http://www.arm.linux.org.uk/developer/ arm-linux-kernel mailing list: http://lists.arm.linux.org.uk/mailman/listinfo/linux-arm-kernel FAQ: http://www.arm.linux.org.uk/armlinux/mlfaq.php How to post kernel fixes: http://www.arm.uk.linux.org/developer/patches/ ARMLinux @ Simtec: http://armlinux.simtec.co.uk/ A few useful resources: FAQ, documentation and Who's who!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
514
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
515
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
516
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
517
Thanks to LucasArts
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
518
Annexes
Quiz answers
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
519
Quiz answers
request_irq, free_irq Q: Why does dev_id have to be unique for shared IRQs? A: Otherwise, the kernel would have no way of knowing which handler to release. Also needed for multiple devices (disks, serial ports...) managed by the same driver, which rely on the same interrupt handler code. Interrupt handling Q: Why did the kernel segfault at module unload (forgetting to unregister a handler in a shared interrupt line)? A: Kernel memory is allocated at module load time, to host module code. This memory is freed at module unload time. If you forget to unregister a handler and an interrupt comes, the cpu will try to jump to the address of the handler, which is in a freed memory area. Crash!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
520
Annexes
More Shell Tips & Tricks
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
521
ls command
Lists the files in the current directory, in alphanumeric order, except files starting with the . character. ls -a (all) Lists all the files (including .* files) ls -l (long) Long listing (type, date, size, owner, permissions) ls -t (time) Lists the most recent files first ls -S (size) Lists the biggest files first ls -r (reverse) Reverses the sort order ls -ltr (options can be combined) Long listing, most recent files at the end
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
522
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
523
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
524
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
525
The cp command
cp <source_file> <target_file> Copies the source file to the target. cp file1 file2 file3 ... dir Copies the files to the target directory (last argument). cp -i (interactive) Asks for user confirmation if the target file already exists cp -r <source_dir> <target_dir> (recursive) Copies the whole directory.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
526
mv and rm commands
mv <old_name> <new_name> (move) Renames the given file or directory. mv -i (interactive) If the new file already exits, asks for user confirm rm file1 file2 file3 ... (remove) Removes the given files. rm -i (interactive) Always ask for user confirm. rm -r dir1 dir2 dir3 (recursive) Removes the given directories with all their contents.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
527
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
528
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
529
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
530
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
531
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
532
Symbolic links
A symbolic link is a special file which is just a reference to the name of another one (file or directory): Useful to reduce disk usage and complexity when 2 files have the same content. Example:
anakin_skywalker_biography -> darth_vador_biography
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
533
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
534
Hard links
The default behavior for ln is to create hard links A hard link to a file is a regular file with exactly the same physical contents While they still save space, hard links can't be distinguished from the original files. If you remove the original file, there is no impact on the hard link contents. The contents are removed when there are no more files (hard links) to them.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
535
Soft link
rm
File
Hard link
rm
Inode
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
536
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
537
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
538
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
539
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
540
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
541
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
542
Standard output
More about command output All the commands outputting text on your terminal do it by writing to their standard output. Standard output can be written (redirected) to a file using the > symbol Standard output can be appended to an existing file using the >> symbol
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
543
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
544
Standard input
More about command input Lots of commands, when not given input arguments, can take their input from standard input. sort windows linux [Ctrl][D] linux windows sort takes its input from the standard input: in this case, what you type in the terminal (ended by [Ctrl][D])
sort < participants.txt The standard input of sort is taken from the given file.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
545
Pipes
Unix pipes are very useful to redirect the standard output of a command to the standard input of another one. Examples
cat *.log | grep -i error | sort grep -ri error . | grep -v ignored | sort -u \ > serious_errors.log cat /home/*/homework.txt | grep mark | more
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
546
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
547
Standard error
Error messages are usually output (if the program is well written) to standard error instead of standard output. Standard error can be redirected through 2> or 2>> Example: cat f1 f2 nofile > newfile 2> errfile Note: 1 is the descriptor for standard output, so 1> is equivalent to >. Can redirect both standard output and standard error to the same file using &> : cat f1 f2 nofile &> wholefile
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
548
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
549
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
550
Annexes
Init runlevels
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
551
/etc/initab excerpt:
id:5:initdefault: # System initialization. si::sysinit:/etc/rc.d/rc.sysinit l0:0:wait:/etc/rc.d/rc l1:1:wait:/etc/rc.d/rc l2:2:wait:/etc/rc.d/rc l3:3:wait:/etc/rc.d/rc l4:4:wait:/etc/rc.d/rc l5:5:wait:/etc/rc.d/rc l6:6:wait:/etc/rc.d/rc 0 1 2 3 4 5 6
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
552
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
553
init scripts
According to /etc/inittab settings, init <n> runs: First /etc/rc.d/rc.sysinit for all runlevels Then scripts in /etc/rc<n>.d/ Starting services (1, 3, 5, S): runs S* scripts with the start option Killing services (0, 6): runs K* scripts with the stop option Scripts are run in file name lexical order Just use ls -l to find out the order!
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
554
/etc/init.d
Repository for all available init scripts /etc/rc<n>.d/ only contains links to the /etc/init.d/ scripts needed for runlevel n /etc/rc1.d/ example (from Fedora Core 3)
K01yum -> ../init.d/yum K02cups-config-daemon -> ../init.d/cupsconfig-daemon K02haldaemon -> ../init.d/haldaemon K02NetworkManager -> ../init.d/NetworkManager K03messagebus -> ../init.d/messagebus K03rhnsd -> ../init.d/rhnsd K05anacron -> ../init.d/anacron K05atd -> ../init.d/atd S00single -> ../init.d/single S01sysstat -> ../init.d/sysstat S06cpuspeed -> ../init.d/cpuspeed
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
555
OK
[FAILED]
OK
[ [
OK OK
] ]
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
556
Annexes
Linux DMA API
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
557
DMA situations
Synchronous
A user process calls the read method of a driver. The driver allocates a DMA buffer and asks the hardware to copy its data. The process is put in sleep mode. The hardware copies its data and raises an interrupt at the end. The interrupt handler gets the data from the buffer and wakes up the waiting process.
Asynchronous
The hardware sends an interrupt to announce new data. The interrupt handler allocates a DMA buffer and tells the hardware where to transfer data. The hardware writes the data and raises a new interrupt. The handler releases the new data, and wakes up the needed processes.
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
558
Memory constraints
Need to use contiguous memory in physical space Can use any memory allocated by kmalloc (up to 128 KB) or __get_free_pages (up to 8MB) Can use block I/O and networking buffers, designed to support DMA. Can not use vmalloc memory (would have to setup DMA on each individual page)
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
559
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
560
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
561
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
562
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
563
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
564
void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t handle);
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
565
/* Name string */ /* device structure */ /* Size of pool buffers */ /* Hardware alignment (bytes) */ /* Address boundaries not to be crossed */
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
566
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
567
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
568
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
569
Lab Information
Login: root:secret and gby:qwerty Running the Eclipse IDE: /opt/course/scripts/eclipse & Running the QEMU Emulator: /opt/course/scripts/run-qemu & The emulated board files are at: /opt/course/emulation/root The kernel module template is at: /opt/course/skeleton/
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
570
Copyrights
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd. Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . Portions Xavier Leroy <Xavier.Leroy@inria.fr> Tux Image Copyright: (C) 1996 Larry Ewing Linux is a registered trademark of Linus Torvalds. All other trademarks are property of their respective owners. Used and distributed under a Creative Commons Attribution-ShareAlike 2.0 license
Copyright 2006-2004, Michael Opdenacker Copyright 2003-2006, Oron Peled Copyright 2004-2006 Codefidence Ltd.
For full copyright information see last page. Creative Commons Attribution-ShareAlike 2.0 license
571