kernel-hacking.pdf

(52 KB) Pobierz
Unreliable Guide To Hacking The Linux
Kernel
Paul Rusty Russell
rusty@linuxcare.com
Unreliable Guide To Hacking The Linux Kernel
by Paul Rusty Russell
Copyright © 2000 by Paul Russell
This documentation is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For more details see the file COPYING in the source distribution of Linux.
Table of Contents
1. Introduction............................................................................................................................................5
2. The Players .............................................................................................................................................6
User Context.......................................................................................................................................6
Hardware Interrupts (Hard IRQs) ......................................................................................................6
Software Interrupt Context: Bottom Halves, Tasklets, softirqs..........................................................7
3. Some Basic Rules ...................................................................................................................................9
4. ioctls: Not writing a new system call ..................................................................................................10
5. Recipes for Deadlock ...........................................................................................................................11
6. Common Routines ...............................................................................................................................12
printk() include/linux/kernel.h ........................................................................................12
copy_[to/from]_user() / get_user() / put_user() include/asm/uaccess.h .............12
kmalloc() / kfree() include/linux/slab.h ..........................................................................13
current include/asm/current.h ............................................................................................14
local_irq_save() / local_irq_restore() include/asm/system.h .................................14
local_bh_disable() / local_bh_enable() include/asm/softirq.h ...............................14
smp_processor_id ()/ cpu_[number/logical]_map() include/asm/smp.h .....................14
__init/__exit/__initdata include/linux/init.h ........................................................................14
__initcall() / module_init() include/linux/init.h ......................................................15
module_exit() include/linux/init.h .................................................................................15
MOD_INC_USE_COUNT / MOD_DEC_USE_COUNT include/linux/module.h ...............................15
7. Wait Queues include/linux/wait.h .............................................................................................17
Declaring ..........................................................................................................................................17
Queuing ............................................................................................................................................17
Waking Up Queued Tasks ................................................................................................................17
8. Atomic Operations...............................................................................................................................18
9. Symbols .................................................................................................................................................19
EXPORT_SYMBOL() include/linux/module.h .........................................................................19
EXPORT_SYMTAB ..............................................................................................................................19
10. Routines and Conventions.................................................................................................................20
Double-linked lists include/linux/list.h ...............................................................................20
Return Conventions ..........................................................................................................................20
Breaking Compilation ......................................................................................................................20
Initializing structure members .........................................................................................................20
GNU Extensions ..............................................................................................................................21
C++...................................................................................................................................................21
#if .....................................................................................................................................................21
3
11. Putting Your Stuff in the Kernel ......................................................................................................23
12. Kernel Cantrips..................................................................................................................................24
13. Thanks ................................................................................................................................................26
4
Chapter 1. Introduction
Welcome, gentle reader, to Rusty’s Unreliable Guide to Linux Kernel Hacking. This document describes
the common routines and general requirements for kernel code: its goal is to serve as a primer for Linux
kernel development for experienced C programmers. I avoid implementation details: that’s what the code
is for, and I ignore whole tracts of useful routines.
Before you read this, please understand that I never wanted to write this document, being grossly
under-qualified, but I always wanted to read it, and this was the only way. I hope it will grow into a
compendium of best practice, common starting points and random information.
5
Zgłoś jeśli naruszono regulamin