kernel-hacking.tmpl
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:46k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V3.1//EN"[]>
  2. <book id="lk-hacking-guide">
  3.  <bookinfo>
  4.   <title>Unreliable Guide To Hacking The Linux Kernel</title>
  5.   
  6.   <authorgroup>
  7.    <author>
  8.     <firstname>Paul</firstname>
  9.     <othername>Rusty</othername>
  10.     <surname>Russell</surname>
  11.     <affiliation>
  12.      <address>
  13.       <email>rusty@rustcorp.com.au</email>
  14.      </address>
  15.     </affiliation>
  16.    </author>
  17.   </authorgroup>
  18.   <copyright>
  19.    <year>2001</year>
  20.    <holder>Rusty Russell</holder>
  21.   </copyright>
  22.   <legalnotice>
  23.    <para>
  24.     This documentation is free software; you can redistribute
  25.     it and/or modify it under the terms of the GNU General Public
  26.     License as published by the Free Software Foundation; either
  27.     version 2 of the License, or (at your option) any later
  28.     version.
  29.    </para>
  30.    
  31.    <para>
  32.     This program is distributed in the hope that it will be
  33.     useful, but WITHOUT ANY WARRANTY; without even the implied
  34.     warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  35.     See the GNU General Public License for more details.
  36.    </para>
  37.    
  38.    <para>
  39.     You should have received a copy of the GNU General Public
  40.     License along with this program; if not, write to the Free
  41.     Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  42.     MA 02111-1307 USA
  43.    </para>
  44.    
  45.    <para>
  46.     For more details see the file COPYING in the source
  47.     distribution of Linux.
  48.    </para>
  49.   </legalnotice>
  50.   <releaseinfo>
  51.    This is the first release of this document as part of the kernel tarball.
  52.   </releaseinfo>
  53.  </bookinfo>
  54.  <toc></toc>
  55.  <chapter id="introduction">
  56.   <title>Introduction</title>
  57.   <para>
  58.    Welcome, gentle reader, to Rusty's Unreliable Guide to Linux
  59.    Kernel Hacking.  This document describes the common routines and
  60.    general requirements for kernel code: its goal is to serve as a
  61.    primer for Linux kernel development for experienced C
  62.    programmers.  I avoid implementation details: that's what the
  63.    code is for, and I ignore whole tracts of useful routines.
  64.   </para>
  65.   <para>
  66.    Before you read this, please understand that I never wanted to
  67.    write this document, being grossly under-qualified, but I always
  68.    wanted to read it, and this was the only way.  I hope it will
  69.    grow into a compendium of best practice, common starting points
  70.    and random information.
  71.   </para>
  72.  </chapter>
  73.  <chapter id="basic-players">
  74.   <title>The Players</title>
  75.   <para>
  76.    At any time each of the CPUs in a system can be:
  77.   </para>
  78.   <itemizedlist>
  79.    <listitem>
  80.     <para>
  81.      not associated with any process, serving a hardware interrupt;
  82.     </para>
  83.    </listitem>
  84.    <listitem>
  85.     <para>
  86.      not associated with any process, serving a softirq, tasklet or bh;
  87.     </para>
  88.    </listitem>
  89.    <listitem>
  90.     <para>
  91.      running in kernel space, associated with a process;
  92.     </para>
  93.    </listitem>
  94.    <listitem>
  95.     <para>
  96.      running a process in user space.
  97.     </para>
  98.    </listitem>
  99.   </itemizedlist>
  100.   <para>
  101.    There is a strict ordering between these: other than the last
  102.    category (userspace) each can only be pre-empted by those above.
  103.    For example, while a softirq is running on a CPU, no other
  104.    softirq will pre-empt it, but a hardware interrupt can.  However,
  105.    any other CPUs in the system execute independently.
  106.   </para>
  107.   <para>
  108.    We'll see a number of ways that the user context can block
  109.    interrupts, to become truly non-preemptable.
  110.   </para>
  111.   
  112.   <sect1 id="basics-usercontext">
  113.    <title>User Context</title>
  114.    <para>
  115.     User context is when you are coming in from a system call or
  116.     other trap: you can sleep, and you own the CPU (except for
  117.     interrupts) until you call <function>schedule()</function>.  
  118.     In other words, user context (unlike userspace) is not pre-emptable.
  119.    </para>
  120.    <note>
  121.     <para>
  122.      You are always in user context on module load and unload,
  123.      and on operations on the block device layer.
  124.     </para>
  125.    </note>
  126.    <para>
  127.     In user context, the <varname>current</varname> pointer (indicating 
  128.     the task we are currently executing) is valid, and
  129.     <function>in_interrupt()</function>
  130.     (<filename>include/asm/hardirq.h</filename>) is <returnvalue>false
  131.     </returnvalue>.  
  132.    </para>
  133.    <caution>
  134.     <para>
  135.      Beware that if you have interrupts or bottom halves disabled 
  136.      (see below), <function>in_interrupt()</function> will return a 
  137.      false positive.
  138.     </para>
  139.    </caution>
  140.   </sect1>
  141.   <sect1 id="basics-hardirqs">
  142.    <title>Hardware Interrupts (Hard IRQs)</title>
  143.    <para>
  144.     Timer ticks, <hardware>network cards</hardware> and 
  145.     <hardware>keyboard</hardware> are examples of real
  146.     hardware which produce interrupts at any time.  The kernel runs
  147.     interrupt handlers, which services the hardware.  The kernel
  148.     guarantees that this handler is never re-entered: if another
  149.     interrupt arrives, it is queued (or dropped).  Because it
  150.     disables interrupts, this handler has to be fast: frequently it
  151.     simply acknowledges the interrupt, marks a `software interrupt'
  152.     for execution and exits.
  153.    </para>
  154.    <para>
  155.     You can tell you are in a hardware interrupt, because 
  156.     <function>in_irq()</function> returns <returnvalue>true</returnvalue>.  
  157.    </para>
  158.    <caution>
  159.     <para>
  160.      Beware that this will return a false positive if interrupts are disabled 
  161.      (see below).
  162.     </para>
  163.    </caution>
  164.   </sect1>
  165.   <sect1 id="basics-softirqs">
  166.    <title>Software Interrupt Context: Bottom Halves, Tasklets, softirqs</title>
  167.    <para>
  168.     Whenever a system call is about to return to userspace, or a
  169.     hardware interrupt handler exits, any `software interrupts'
  170.     which are marked pending (usually by hardware interrupts) are
  171.     run (<filename>kernel/softirq.c</filename>).
  172.    </para>
  173.    <para>
  174.     Much of the real interrupt handling work is done here.  Early in
  175.     the transition to <acronym>SMP</acronym>, there were only `bottom 
  176.     halves' (BHs), which didn't take advantage of multiple CPUs.  Shortly 
  177.     after we switched from wind-up computers made of match-sticks and snot,
  178.     we abandoned this limitation.
  179.    </para>
  180.    <para>
  181.     <filename class=headerfile>include/linux/interrupt.h</filename> lists the 
  182.     different BH's.  No matter how many CPUs you have, no two BHs will run at 
  183.     the same time. This made the transition to SMP simpler, but sucks hard for
  184.     scalable performance.  A very important bottom half is the timer
  185.     BH (<filename class=headerfile>include/linux/timer.h</filename>): you 
  186.     can register to have it call functions for you in a given length of time.
  187.    </para>
  188.    <para>
  189.     2.3.43 introduced softirqs, and re-implemented the (now
  190.     deprecated) BHs underneath them.  Softirqs are fully-SMP
  191.     versions of BHs: they can run on as many CPUs at once as
  192.     required.  This means they need to deal with any races in shared
  193.     data using their own locks.  A bitmask is used to keep track of
  194.     which are enabled, so the 32 available softirqs should not be
  195.     used up lightly.  (<emphasis>Yes</emphasis>, people will
  196.     notice).
  197.    </para>
  198.    <para>
  199.     tasklets (<filename class=headerfile>include/linux/interrupt.h</filename>) 
  200.     are like softirqs, except they are dynamically-registrable (meaning you 
  201.     can have as many as you want), and they also guarantee that any tasklet 
  202.     will only run on one CPU at any time, although different tasklets can 
  203.     run simultaneously (unlike different BHs).  
  204.    </para>
  205.    <caution>
  206.     <para>
  207.      The name `tasklet' is misleading: they have nothing to do with `tasks', 
  208.      and probably more to do with some bad vodka Alexey Kuznetsov had at the 
  209.      time.
  210.     </para>
  211.    </caution>
  212.    <para>
  213.     You can tell you are in a softirq (or bottom half, or tasklet)
  214.     using the <function>in_softirq()</function> macro 
  215.     (<filename class=headerfile>include/asm/softirq.h</filename>).  
  216.    </para>
  217.    <caution>
  218.     <para>
  219.      Beware that this will return a false positive if a bh lock (see below)
  220.      is held.
  221.     </para>
  222.    </caution>
  223.   </sect1>
  224.  </chapter>
  225.  <chapter id="basic-rules">
  226.   <title>Some Basic Rules</title>
  227.   <variablelist>
  228.    <varlistentry>
  229.     <term>No memory protection</term>
  230.     <listitem>
  231.      <para>
  232.       If you corrupt memory, whether in user context or
  233.       interrupt context, the whole machine will crash.  Are you
  234.       sure you can't do what you want in userspace?
  235.      </para>
  236.     </listitem>
  237.    </varlistentry>
  238.    <varlistentry>
  239.     <term>No floating point or <acronym>MMX</acronym></term>
  240.     <listitem>
  241.      <para>
  242.       The <acronym>FPU</acronym> context is not saved; even in user
  243.       context the <acronym>FPU</acronym> state probably won't
  244.       correspond with the current process: you would mess with some
  245.       user process' <acronym>FPU</acronym> state.  If you really want
  246.       to do this, you would have to explicitly save/restore the full
  247.       <acronym>FPU</acronym> state (and avoid context switches).  It
  248.       is generally a bad idea; use fixed point arithmetic first.
  249.      </para>
  250.     </listitem>
  251.    </varlistentry>
  252.    <varlistentry>
  253.     <term>A rigid stack limit</term>
  254.     <listitem>
  255.      <para>
  256.       The kernel stack is about 6K in 2.2 (for most
  257.       architectures: it's about 14K on the Alpha), and shared
  258.       with interrupts so you can't use it all.  Avoid deep
  259.       recursion and huge local arrays on the stack (allocate
  260.       them dynamically instead).
  261.      </para>
  262.     </listitem>
  263.    </varlistentry>
  264.    <varlistentry>
  265.     <term>The Linux kernel is portable</term>
  266.     <listitem>
  267.      <para>
  268.       Let's keep it that way.  Your code should be 64-bit clean,
  269.       and endian-independent.  You should also minimize CPU
  270.       specific stuff, e.g. inline assembly should be cleanly
  271.       encapsulated and minimized to ease porting.  Generally it
  272.       should be restricted to the architecture-dependent part of
  273.       the kernel tree.
  274.      </para>
  275.     </listitem>
  276.    </varlistentry>
  277.   </variablelist>
  278.  </chapter>
  279.  <chapter id="ioctls">
  280.   <title>ioctls: Not writing a new system call</title>
  281.   <para>
  282.    A system call generally looks like this
  283.   </para>
  284.   <programlisting>
  285. asmlinkage int sys_mycall(int arg) 
  286. {
  287.         return 0; 
  288. }
  289.   </programlisting>
  290.   <para>
  291.    First, in most cases you don't want to create a new system call.
  292.    You create a character device and implement an appropriate ioctl
  293.    for it.  This is much more flexible than system calls, doesn't have
  294.    to be entered in every architecture's
  295.    <filename class=headerfile>include/asm/unistd.h</filename> and
  296.    <filename>arch/kernel/entry.S</filename> file, and is much more
  297.    likely to be accepted by Linus.
  298.   </para>
  299.   <para>
  300.    If all your routine does is read or write some parameter, consider
  301.    implementing a <function>sysctl</function> interface instead.
  302.   </para>
  303.   <para>
  304.    Inside the ioctl you're in user context to a process.  When a
  305.    error occurs you return a negated errno (see
  306.    <filename class=headerfile>include/linux/errno.h</filename>),
  307.    otherwise you return <returnvalue>0</returnvalue>.
  308.   </para>
  309.   <para>
  310.    After you slept you should check if a signal occurred: the
  311.    Unix/Linux way of handling signals is to temporarily exit the
  312.    system call with the <constant>-ERESTARTSYS</constant> error.  The
  313.    system call entry code will switch back to user context, process
  314.    the signal handler and then your system call will be restarted
  315.    (unless the user disabled that).  So you should be prepared to
  316.    process the restart, e.g. if you're in the middle of manipulating
  317.    some data structure.
  318.   </para>
  319.   <programlisting>
  320. if (signal_pending()) 
  321.         return -ERESTARTSYS;
  322.   </programlisting>
  323.   <para>
  324.    If you're doing longer computations: first think userspace. If you
  325.    <emphasis>really</emphasis> want to do it in kernel you should
  326.    regularly check if you need to give up the CPU (remember there is
  327.    cooperative multitasking per CPU).  Idiom:
  328.   </para>
  329.   <programlisting>
  330. if (current-&gt;need_resched)
  331.         schedule(); /* Will sleep */ 
  332.   </programlisting>
  333.   <para>
  334.    A short note on interface design: the UNIX system call motto is
  335.    "Provide mechanism not policy".
  336.   </para>
  337.  </chapter>
  338.  <chapter id="deadlock-recipes">
  339.   <title>Recipes for Deadlock</title>
  340.   <para>
  341.    You cannot call any routines which may sleep, unless:
  342.   </para>
  343.   <itemizedlist>
  344.    <listitem>
  345.     <para>
  346.      You are in user context.
  347.     </para>
  348.    </listitem>
  349.    <listitem>
  350.     <para>
  351.      You do not own any spinlocks.
  352.     </para>
  353.    </listitem>
  354.    <listitem>
  355.     <para>
  356.      You have interrupts enabled (actually, Andi Kleen says
  357.      that the scheduling code will enable them for you, but
  358.      that's probably not what you wanted).
  359.     </para>
  360.    </listitem>
  361.   </itemizedlist>
  362.   <para>
  363.    Note that some functions may sleep implicitly: common ones are
  364.    the user space access functions (*_user) and memory allocation
  365.    functions without <symbol>GFP_ATOMIC</symbol>.
  366.   </para>
  367.   <para>
  368.    You will eventually lock up your box if you break these rules.  
  369.   </para>
  370.   <para>
  371.    Really.
  372.   </para>
  373.  </chapter>
  374.  <chapter id="common-routines">
  375.   <title>Common Routines</title>
  376.   <sect1 id="routines-printk">
  377.    <title>
  378.     <function>printk()</function>
  379.     <filename class=headerfile>include/linux/kernel.h</filename>
  380.    </title>
  381.    <para>
  382.     <function>printk()</function> feeds kernel messages to the
  383.     console, dmesg, and the syslog daemon.  It is useful for debugging
  384.     and reporting errors, and can be used inside interrupt context,
  385.     but use with caution: a machine which has its console flooded with
  386.     printk messages is unusable.  It uses a format string mostly
  387.     compatible with ANSI C printf, and C string concatenation to give
  388.     it a first "priority" argument:
  389.    </para>
  390.    <programlisting>
  391. printk(KERN_INFO "i = %un", i);
  392.    </programlisting>
  393.    <para>
  394.     See <filename class=headerfile>include/linux/kernel.h</filename>;
  395.     for other KERN_ values; these are interpreted by syslog as the
  396.     level.  Special case: for printing an IP address use
  397.    </para>
  398.    <programlisting>
  399. __u32 ipaddress;
  400. printk(KERN_INFO "my ip: %d.%d.%d.%dn", NIPQUAD(ipaddress));
  401.    </programlisting>
  402.    <para>
  403.     <function>printk()</function> internally uses a 1K buffer and does
  404.     not catch overruns.  Make sure that will be enough.
  405.    </para>
  406.    <note>
  407.     <para>
  408.      You will know when you are a real kernel hacker
  409.      when you start typoing printf as printk in your user programs :)
  410.     </para>
  411.    </note>
  412.    <!--- From the Lions book reader department --> 
  413.    <note>
  414.     <para>
  415.      Another sidenote: the original Unix Version 6 sources had a
  416.      comment on top of its printf function: "Printf should not be
  417.      used for chit-chat".  You should follow that advice.
  418.     </para>
  419.    </note>
  420.   </sect1>
  421.   <sect1 id="routines-copy">
  422.    <title>
  423.     <function>copy_[to/from]_user()</function>
  424.     /
  425.     <function>get_user()</function>
  426.     /
  427.     <function>put_user()</function>
  428.     <filename class=headerfile>include/asm/uaccess.h</filename>
  429.    </title>  
  430.    <para>
  431.     <emphasis>[SLEEPS]</emphasis>
  432.    </para>
  433.    <para>
  434.     <function>put_user()</function> and <function>get_user()</function>
  435.     are used to get and put single values (such as an int, char, or
  436.     long) from and to userspace.  A pointer into userspace should
  437.     never be simply dereferenced: data should be copied using these
  438.     routines.  Both return <constant>-EFAULT</constant> or 0.
  439.    </para>
  440.    <para>
  441.     <function>copy_to_user()</function> and
  442.     <function>copy_from_user()</function> are more general: they copy
  443.     an arbitrary amount of data to and from userspace.
  444.     <caution>
  445.      <para>
  446.       Unlike <function>put_user()</function> and
  447.       <function>get_user()</function>, they return the amount of
  448.       uncopied data (ie. <returnvalue>0</returnvalue> still means
  449.       success).
  450.      </para>
  451.     </caution>
  452.     [Yes, this moronic interface makes me cringe.  Please submit a
  453.     patch and become my hero --RR.]
  454.    </para>
  455.    <para>
  456.     The functions may sleep implicitly. This should never be called
  457.     outside user context (it makes no sense), with interrupts
  458.     disabled, or a spinlock held.
  459.    </para>
  460.   </sect1>
  461.   <sect1 id="routines-kmalloc">
  462.    <title><function>kmalloc()</function>/<function>kfree()</function>
  463.     <filename class=headerfile>include/linux/slab.h</filename></title>
  464.    <para>
  465.     <emphasis>[MAY SLEEP: SEE BELOW]</emphasis>
  466.    </para>
  467.    <para>
  468.     These routines are used to dynamically request pointer-aligned
  469.     chunks of memory, like malloc and free do in userspace, but
  470.     <function>kmalloc()</function> takes an extra flag word.
  471.     Important values:
  472.    </para>
  473.    <variablelist>
  474.     <varlistentry>
  475.      <term>
  476.       <constant>
  477.        GFP_KERNEL
  478.       </constant>
  479.      </term>
  480.      <listitem>
  481.       <para>
  482.        May sleep and swap to free memory. Only allowed in user
  483.        context, but is the most reliable way to allocate memory.
  484.       </para>
  485.      </listitem>
  486.     </varlistentry>
  487.     
  488.     <varlistentry>
  489.      <term>
  490.       <constant>
  491.        GFP_ATOMIC
  492.       </constant>
  493.      </term>
  494.      <listitem>
  495.       <para>
  496.        Don't sleep. Less reliable than <constant>GFP_KERNEL</constant>,
  497.        but may be called from interrupt context. You should
  498.        <emphasis>really</emphasis> have a good out-of-memory
  499.        error-handling strategy.
  500.       </para>
  501.      </listitem>
  502.     </varlistentry>
  503.     
  504.     <varlistentry>
  505.      <term>
  506.       <constant>
  507.        GFP_DMA
  508.       </constant>
  509.      </term>
  510.      <listitem>
  511.       <para>
  512.        Allocate ISA DMA lower than 16MB. If you don't know what that
  513.        is you don't need it.  Very unreliable.
  514.       </para>
  515.      </listitem>
  516.     </varlistentry>
  517.    </variablelist>
  518.    <para>
  519.     If you see a <errorname>kmem_grow: Called nonatomically from int
  520.     </errorname> warning message you called a memory allocation function
  521.     from interrupt context without <constant>GFP_ATOMIC</constant>.
  522.     You should really fix that.  Run, don't walk.
  523.    </para>
  524.    <para>
  525.     If you are allocating at least <constant>PAGE_SIZE</constant>
  526.     (<filename class=headerfile>include/asm/page.h</filename>) bytes,
  527.     consider using <function>__get_free_pages()</function>
  528.     (<filename class=headerfile>include/linux/mm.h</filename>).  It
  529.     takes an order argument (0 for page sized, 1 for double page, 2
  530.     for four pages etc.) and the same memory priority flag word as
  531.     above.
  532.    </para>
  533.    <para>
  534.     If you are allocating more than a page worth of bytes you can use
  535.     <function>vmalloc()</function>.  It'll allocate virtual memory in
  536.     the kernel map.  This block is not contiguous in physical memory,
  537.     but the <acronym>MMU</acronym> makes it look like it is for you
  538.     (so it'll only look contiguous to the CPUs, not to external device
  539.     drivers).  If you really need large physically contiguous memory
  540.     for some weird device, you have a problem: it is poorly supported
  541.     in Linux because after some time memory fragmentation in a running
  542.     kernel makes it hard.  The best way is to allocate the block early
  543.     in the boot process via the <function>alloc_bootmem()</function>
  544.     routine.
  545.    </para>
  546.    <para>
  547.     Before inventing your own cache of often-used objects consider
  548.     using a slab cache in
  549.     <filename class=headerfile>include/linux/slab.h</filename>
  550.    </para>
  551.   </sect1>
  552.   <sect1 id="routines-current">
  553.    <title><function>current</function>
  554.     <filename class=headerfile>include/asm/current.h</filename></title>
  555.    <para>
  556.     This global variable (really a macro) contains a pointer to
  557.     the current task structure, so is only valid in user context.
  558.     For example, when a process makes a system call, this will
  559.     point to the task structure of the calling process.  It is
  560.     <emphasis>not NULL</emphasis> in interrupt context.
  561.    </para>
  562.   </sect1>
  563.   <sect1 id="routines-udelay">
  564.    <title><function>udelay()</function>/<function>mdelay()</function>
  565.      <filename class=headerfile>include/asm/delay.h</filename> 
  566.      <filename class=headerfile>include/linux/delay.h</filename> 
  567.    </title>
  568.    <para>
  569.     The <function>udelay()</function> function can be used for small pauses.
  570.     Do not use large values with <function>udelay()</function> as you risk
  571.     overflow - the helper function <function>mdelay()</function> is useful
  572.     here, or even consider <function>schedule_timeout()</function>.
  573.    </para> 
  574.   </sect1>
  575.  
  576.   <sect1 id="routines-endian">
  577.    <title><function>cpu_to_be32()</function>/<function>be32_to_cpu()</function>/<function>cpu_to_le32()</function>/<function>le32_to_cpu()</function>
  578.      <filename class=headerfile>include/asm/byteorder.h</filename> 
  579.    </title>
  580.    <para>
  581.     The <function>cpu_to_be32()</function> family (where the "32" can
  582.     be replaced by 64 or 16, and the "be" can be replaced by "le") are
  583.     the general way to do endian conversions in the kernel: they
  584.     return the converted value.  All variations supply the reverse as
  585.     well: <function>be32_to_cpu()</function>, etc.
  586.    </para>
  587.    <para>
  588.     There are two major variations of these functions: the pointer
  589.     variation, such as <function>cpu_to_be32p()</function>, which take
  590.     a pointer to the given type, and return the converted value.  The
  591.     other variation is the "in-situ" family, such as
  592.     <function>cpu_to_be32s()</function>, which convert value referred
  593.     to by the pointer, and return void.
  594.    </para> 
  595.   </sect1>
  596.   <sect1 id="routines-local-irqs">
  597.    <title><function>local_irq_save()</function>/<function>local_irq_restore()</function>
  598.     <filename class=headerfile>include/asm/system.h</filename>
  599.    </title>
  600.    <para>
  601.     These routines disable hard interrupts on the local CPU, and
  602.     restore them.  They are reentrant; saving the previous state in
  603.     their one <varname>unsigned long flags</varname> argument.  If you
  604.     know that interrupts are enabled, you can simply use
  605.     <function>local_irq_disable()</function> and
  606.     <function>local_irq_enable()</function>.
  607.    </para>
  608.   </sect1>
  609.   <sect1 id="routines-softirqs">
  610.    <title><function>local_bh_disable()</function>/<function>local_bh_enable()</function>
  611.     <filename class=headerfile>include/asm/softirq.h</filename></title>
  612.    <para>
  613.     These routines disable soft interrupts on the local CPU, and
  614.     restore them.  They are reentrant; if soft interrupts were
  615.     disabled before, they will still be disabled after this pair
  616.     of functions has been called.  They prevent softirqs, tasklets
  617.     and bottom halves from running on the current CPU.
  618.    </para>
  619.   </sect1>
  620.   <sect1 id="routines-processorids">
  621.    <title><function>smp_processor_id</function>()/<function>cpu_[number/logical]_map()</function>
  622.     <filename class=headerfile>include/asm/smp.h</filename></title>
  623.    
  624.    <para>
  625.     <function>smp_processor_id()</function> returns the current
  626.     processor number, between 0 and <symbol>NR_CPUS</symbol> (the
  627.     maximum number of CPUs supported by Linux, currently 32).  These
  628.     values are not necessarily continuous: to get a number between 0
  629.     and <function>smp_num_cpus()</function> (the number of actual
  630.     processors in this machine), the
  631.     <function>cpu_number_map()</function> function is used to map the
  632.     processor id to a logical number.
  633.     <function>cpu_logical_map()</function> does the reverse.
  634.    </para>
  635.   </sect1>
  636.   <sect1 id="routines-init">
  637.    <title><type>__init</type>/<type>__exit</type>/<type>__initdata</type>
  638.     <filename class=headerfile>include/linux/init.h</filename></title>
  639.    <para>
  640.     After boot, the kernel frees up a special section; functions
  641.     marked with <type>__init</type> and data structures marked with
  642.     <type>__initdata</type> are dropped after boot is complete (within
  643.     modules this directive is currently ignored).  <type>__exit</type>
  644.     is used to declare a function which is only required on exit: the
  645.     function will be dropped if this file is not compiled as a module.
  646.     See the header file for use. Note that it makes no sense for a function
  647.     marked with <type>__init</type> to be exported to modules with 
  648.     <function>EXPORT_SYMBOL()</function> - this will break.
  649.    </para>
  650.    <para>
  651.    Static data structures marked as <type>__initdata</type> must be initialised
  652.    (as opposed to ordinary static data which is zeroed BSS) and cannot be 
  653.    <type>const</type>.
  654.    </para> 
  655.   </sect1>
  656.   <sect1 id="routines-init-again">
  657.    <title><function>__initcall()</function>/<function>module_init()</function>
  658.     <filename class=headerfile>include/linux/init.h</filename></title>
  659.    <para>
  660.     Many parts of the kernel are well served as a module
  661.     (dynamically-loadable parts of the kernel).  Using the
  662.     <function>module_init()</function> and
  663.     <function>module_exit()</function> macros it is easy to write code
  664.     without #ifdefs which can operate both as a module or built into
  665.     the kernel.
  666.    </para>
  667.    <para>
  668.     The <function>module_init()</function> macro defines which
  669.     function is to be called at module insertion time (if the file is
  670.     compiled as a module), or at boot time: if the file is not
  671.     compiled as a module the <function>module_init()</function> macro
  672.     becomes equivalent to <function>__initcall()</function>, which
  673.     through linker magic ensures that the function is called on boot.
  674.    </para>
  675.    <para>
  676.     The function can return a negative error number to cause
  677.     module loading to fail (unfortunately, this has no effect if
  678.     the module is compiled into the kernel).  For modules, this is
  679.     called in user context, with interrupts enabled, and the
  680.     kernel lock held, so it can sleep.
  681.    </para>
  682.   </sect1>
  683.   
  684.   <sect1 id="routines-moduleexit">
  685.    <title> <function>module_exit()</function>
  686.     <filename class=headerfile>include/linux/init.h</filename> </title>
  687.    <para>
  688.     This macro defines the function to be called at module removal
  689.     time (or never, in the case of the file compiled into the
  690.     kernel).  It will only be called if the module usage count has
  691.     reached zero.  This function can also sleep, but cannot fail:
  692.     everything must be cleaned up by the time it returns.
  693.    </para>
  694.   </sect1>
  695.   <sect1 id="routines-module-use-counters">
  696.    <title> <function>MOD_INC_USE_COUNT</function>/<function>MOD_DEC_USE_COUNT</function>
  697.     <filename class=headerfile>include/linux/module.h</filename></title>
  698.    <para>
  699.     These manipulate the module usage count, to protect against
  700.     removal (a module also can't be removed if another module uses
  701.     one of its exported symbols: see below).  Every reference to
  702.     the module from user context should be reflected by this
  703.     counter (e.g. for every data structure or socket) before the
  704.     function sleeps.  To quote Tim Waugh:
  705.    </para>
  706.    <programlisting>
  707. /* THIS IS BAD */
  708. foo_open (...)
  709. {
  710.         stuff..
  711.         if (fail)
  712.                 return -EBUSY;
  713.         sleep.. (might get unloaded here)
  714.         stuff..
  715.         MOD_INC_USE_COUNT;
  716.         return 0;
  717. }
  718. /* THIS IS GOOD /
  719. foo_open (...)
  720. {
  721.         MOD_INC_USE_COUNT;
  722.         stuff..
  723.         if (fail) {
  724.                 MOD_DEC_USE_COUNT;
  725.                 return -EBUSY;
  726.         }
  727.         sleep.. (safe now)
  728.         stuff..
  729.         return 0;
  730. }
  731.    </programlisting>
  732.    <para>
  733.    You can often avoid having to deal with these problems by using the 
  734.    <structfield>owner</structfield> field of the 
  735.    <structname>file_operations</structname> structure. Set this field
  736.    as the macro <symbol>THIS_MODULE</symbol>.
  737.    </para>
  738.    <para>
  739.    For more complicated module unload locking requirements, you can set the
  740.    <structfield>can_unload</structfield> function pointer to your own routine,
  741.    which should return <returnvalue>0</returnvalue> if the module is
  742.    unloadable, or <returnvalue>-EBUSY</returnvalue> otherwise.
  743.    </para> 
  744.   
  745.   </sect1>
  746.  </chapter>
  747.  <chapter id="queues">
  748.   <title>Wait Queues
  749.    <filename class=headerfile>include/linux/wait.h</filename>
  750.   </title>
  751.   <para>
  752.    <emphasis>[SLEEPS]</emphasis>
  753.   </para>
  754.   <para>
  755.    A wait queue is used to wait for someone to wake you up when a
  756.    certain condition is true.  They must be used carefully to ensure
  757.    there is no race condition.  You declare a
  758.    <type>wait_queue_head_t</type>, and then processes which want to
  759.    wait for that condition declare a <type>wait_queue_t</type>
  760.    referring to themselves, and place that in the queue.
  761.   </para>
  762.   <sect1 id="queue-declaring">
  763.    <title>Declaring</title>
  764.    
  765.    <para>
  766.     You declare a <type>wait_queue_head_t</type> using the
  767.     <function>DECLARE_WAIT_QUEUE_HEAD()</function> macro, or using the
  768.     <function>init_waitqueue_head()</function> routine in your
  769.     initialization code.
  770.    </para>
  771.   </sect1>
  772.   
  773.   <sect1 id="queue-waitqueue">
  774.    <title>Queuing</title>
  775.    
  776.    <para>
  777.     Placing yourself in the waitqueue is fairly complex, because you
  778.     must put yourself in the queue before checking the condition.
  779.     There is a macro to do this:
  780.     <function>wait_event_interruptible()</function>
  781.     <filename class=headerfile>include/linux/sched.h</filename> The
  782.     first argument is the wait queue head, and the second is an
  783.     expression which is evaluated; the macro returns
  784.     <returnvalue>0</returnvalue> when this expression is true, or
  785.     <returnvalue>-ERESTARTSYS</returnvalue> if a signal is received.
  786.     The <function>wait_event()</function> version ignores signals.
  787.    </para>
  788.    <para>
  789.    Do not use the <function>sleep_on()</function> function family -
  790.    it is very easy to accidentally introduce races; almost certainly
  791.    one of the <function>wait_event()</function> family will do, or a
  792.    loop around <function>schedule_timeout()</function>. If you choose
  793.    to loop around <function>schedule_timeout()</function> remember
  794.    you must set the task state (with 
  795.    <function>set_current_state()</function>) on each iteration to avoid
  796.    busy-looping.
  797.    </para>
  798.  
  799.   </sect1>
  800.   <sect1 id="queue-waking">
  801.    <title>Waking Up Queued Tasks</title>
  802.    
  803.    <para>
  804.     Call <function>wake_up()</function>
  805.     <filename class=headerfile>include/linux/sched.h</filename>;,
  806.     which will wake up every process in the queue.  The exception is
  807.     if one has <constant>TASK_EXCLUSIVE</constant> set, in which case
  808.     the remainder of the queue will not be woken.
  809.    </para>
  810.   </sect1>
  811.  </chapter>
  812.  <chapter id="atomic-ops">
  813.   <title>Atomic Operations</title>
  814.   <para>
  815.    Certain operations are guaranteed atomic on all platforms.  The
  816.    first class of operations work on <type>atomic_t</type>
  817.    <filename class=headerfile>include/asm/atomic.h</filename>; this
  818.    contains a signed integer (at least 24 bits long), and you must use
  819.    these functions to manipulate or read atomic_t variables.
  820.    <function>atomic_read()</function> and
  821.    <function>atomic_set()</function> get and set the counter,
  822.    <function>atomic_add()</function>,
  823.    <function>atomic_sub()</function>,
  824.    <function>atomic_inc()</function>,
  825.    <function>atomic_dec()</function>, and
  826.    <function>atomic_dec_and_test()</function> (returns
  827.    <returnvalue>true</returnvalue> if it was decremented to zero).
  828.   </para>
  829.   <para>
  830.    Yes.  It returns <returnvalue>true</returnvalue> (i.e. != 0) if the
  831.    atomic variable is zero.
  832.   </para>
  833.   <para>
  834.    Note that these functions are slower than normal arithmetic, and
  835.    so should not be used unnecessarily.  On some platforms they
  836.    are much slower, like 32-bit Sparc where they use a spinlock.
  837.   </para>
  838.   <para>
  839.    The second class of atomic operations is atomic bit operations on a
  840.    <type>long</type>, defined in
  841.    <filename class=headerfile>include/asm/bitops.h</filename>.  These
  842.    operations generally take a pointer to the bit pattern, and a bit
  843.    number: 0 is the least significant bit.
  844.    <function>set_bit()</function>, <function>clear_bit()</function>
  845.    and <function>change_bit()</function> set, clear, and flip the
  846.    given bit.  <function>test_and_set_bit()</function>,
  847.    <function>test_and_clear_bit()</function> and
  848.    <function>test_and_change_bit()</function> do the same thing,
  849.    except return true if the bit was previously set; these are
  850.    particularly useful for very simple locking.
  851.   </para>
  852.   
  853.   <para>
  854.    It is possible to call these operations with bit indices greater
  855.    than BITS_PER_LONG.  The resulting behavior is strange on big-endian
  856.    platforms though so it is a good idea not to do this.
  857.   </para>
  858.   <para>
  859.    Note that the order of bits depends on the architecture, and in
  860.    particular, the bitfield passed to these operations must be at
  861.    least as large as a <type>long</type>.
  862.   </para>
  863.  </chapter>
  864.  <chapter id="symbols">
  865.   <title>Symbols</title>
  866.   <para>
  867.    Within the kernel proper, the normal linking rules apply
  868.    (ie. unless a symbol is declared to be file scope with the
  869.    <type>static</type> keyword, it can be used anywhere in the
  870.    kernel).  However, for modules, a special exported symbol table is
  871.    kept which limits the entry points to the kernel proper.  Modules
  872.    can also export symbols.
  873.   </para>
  874.   <sect1 id="sym-exportsymbols">
  875.    <title><function>EXPORT_SYMBOL()</function>
  876.     <filename class=headerfile>include/linux/module.h</filename></title>
  877.    <para>
  878.     This is the classic method of exporting a symbol, and it works
  879.     for both modules and non-modules.  In the kernel all these
  880.     declarations are often bundled into a single file to help
  881.     genksyms (which searches source files for these declarations).
  882.     See the comment on genksyms and Makefiles below.
  883.    </para>
  884.   </sect1>
  885.   <sect1 id="sym-exportnosymbols">
  886.    <title><symbol>EXPORT_NO_SYMBOLS</symbol>
  887.     <filename class=headerfile>include/linux/module.h</filename></title>
  888.    <para>
  889.     If a module exports no symbols then you can specify
  890.     <programlisting>
  891. EXPORT_NO_SYMBOLS;
  892.     </programlisting>
  893.     anywhere in the module.
  894.     In kernel 2.4 and earlier, if a module contains neither
  895.     <function>EXPORT_SYMBOL()</function> nor
  896.     <symbol>EXPORT_NO_SYMBOLS</symbol> then the module defaults to
  897.     exporting all non-static global symbols.
  898.     In kernel 2.5 onwards you must explicitly specify whether a module
  899.     exports symbols or not.
  900.    </para>
  901.   </sect1>
  902.   <sect1 id="sym-exportsymbols-gpl">
  903.    <title><function>EXPORT_SYMBOL_GPL()</function>
  904.     <filename class=headerfile>include/linux/module.h</filename></title>
  905.    <para>
  906.     Similar to <function>EXPORT_SYMBOL()</function> except that the
  907.     symbols exported by <function>EXPORT_SYMBOL_GPL()</function> can
  908.     only be seen by modules with a
  909.     <function>MODULE_LICENCE()</function> that specifies a GPL
  910.     compatible license.
  911.    </para>
  912.   </sect1>
  913.  </chapter>
  914.  <chapter id="conventions">
  915.   <title>Routines and Conventions</title>
  916.   <sect1 id="conventions-doublelinkedlist">
  917.    <title>Double-linked lists
  918.     <filename class=headerfile>include/linux/list.h</filename></title>
  919.    <para>
  920.     There are three sets of linked-list routines in the kernel
  921.     headers, but this one seems to be winning out (and Linus has
  922.     used it).  If you don't have some particular pressing need for
  923.     a single list, it's a good choice.  In fact, I don't care
  924.     whether it's a good choice or not, just use it so we can get
  925.     rid of the others.
  926.    </para>
  927.   </sect1>
  928.   <sect1 id="convention-returns">
  929.    <title>Return Conventions</title>
  930.    <para>
  931.     For code called in user context, it's very common to defy C
  932.     convention, and return <returnvalue>0</returnvalue> for success,
  933.     and a negative error number
  934.     (eg. <returnvalue>-EFAULT</returnvalue>) for failure.  This can be
  935.     unintuitive at first, but it's fairly widespread in the networking
  936.     code, for example.
  937.    </para>
  938.    <para>
  939.     The filesystem code uses <function>ERR_PTR()</function>
  940.     <filename class=headerfile>include/linux/fs.h</filename>; to
  941.     encode a negative error number into a pointer, and
  942.     <function>IS_ERR()</function> and <function>PTR_ERR()</function>
  943.     to get it back out again: avoids a separate pointer parameter for
  944.     the error number.  Icky, but in a good way.
  945.    </para>
  946.   </sect1>
  947.   <sect1 id="conventions-borkedcompile">
  948.    <title>Breaking Compilation</title>
  949.    <para>
  950.     Linus and the other developers sometimes change function or
  951.     structure names in development kernels; this is not done just to
  952.     keep everyone on their toes: it reflects a fundamental change
  953.     (eg. can no longer be called with interrupts on, or does extra
  954.     checks, or doesn't do checks which were caught before).  Usually
  955.     this is accompanied by a fairly complete note to the linux-kernel
  956.     mailing list; search the archive.  Simply doing a global replace
  957.     on the file usually makes things <emphasis>worse</emphasis>.
  958.    </para>
  959.   </sect1>
  960.   <sect1 id="conventions-initialising">
  961.    <title>Initializing structure members</title>
  962.    <para>
  963.     The preferred method of initializing structures is to use the
  964.     gcc Labeled Elements extension, eg:
  965.    </para>
  966.    <programlisting>
  967. static struct block_device_operations opt_fops = {
  968.         open:                   opt_open,
  969.         release:                opt_release,
  970.         ioctl:                  opt_ioctl,
  971.         check_media_change:     opt_media_change,
  972. };
  973.    </programlisting>
  974.    <para>
  975.     This makes it easy to grep for, and makes it clear which
  976.     structure fields are set.  You should do this because it looks
  977.     cool.
  978.    </para>
  979.   </sect1>
  980.   <sect1 id="conventions-gnu-extns">
  981.    <title>GNU Extensions</title>
  982.    <para>
  983.     GNU Extensions are explicitly allowed in the Linux kernel.
  984.     Note that some of the more complex ones are not very well
  985.     supported, due to lack of general use, but the following are
  986.     considered standard (see the GCC info page section "C
  987.     Extensions" for more details - Yes, really the info page, the
  988.     man page is only a short summary of the stuff in info):
  989.    </para>
  990.    <itemizedlist>
  991.     <listitem>
  992.      <para>
  993.       Inline functions
  994.      </para>
  995.     </listitem>
  996.     <listitem>
  997.      <para>
  998.       Statement expressions (ie. the ({ and }) constructs).
  999.      </para>
  1000.     </listitem>
  1001.     <listitem>
  1002.      <para>
  1003.       Declaring attributes of a function / variable / type
  1004.       (__attribute__)
  1005.      </para>
  1006.     </listitem>
  1007.     <listitem>
  1008.      <para>
  1009.       Labeled elements
  1010.      </para>
  1011.     </listitem>
  1012.     <listitem>
  1013.      <para>
  1014.       typeof
  1015.      </para>
  1016.     </listitem>
  1017.     <listitem>
  1018.      <para>
  1019.       Zero length arrays
  1020.      </para>
  1021.     </listitem>
  1022.     <listitem>
  1023.      <para>
  1024.       Macro varargs
  1025.      </para>
  1026.     </listitem>
  1027.     <listitem>
  1028.      <para>
  1029.       Arithmetic on void pointers
  1030.      </para>
  1031.     </listitem>
  1032.     <listitem>
  1033.      <para>
  1034.       Non-Constant initializers
  1035.      </para>
  1036.     </listitem>
  1037.     <listitem>
  1038.      <para>
  1039.       Assembler Instructions (not outside arch/ and include/asm/)
  1040.      </para>
  1041.     </listitem>
  1042.     <listitem>
  1043.      <para>
  1044.       Function names as strings (__FUNCTION__)
  1045.      </para>
  1046.     </listitem>
  1047.     <listitem>
  1048.      <para>
  1049.       __builtin_constant_p()
  1050.      </para>
  1051.     </listitem>
  1052.    </itemizedlist>
  1053.    <para>
  1054.     Be wary when using long long in the kernel, the code gcc generates for
  1055.     it is horrible and worse: division and multiplication does not work
  1056.     on i386 because the GCC runtime functions for it are missing from
  1057.     the kernel environment.
  1058.    </para>
  1059.     <!-- FIXME: add a note about ANSI aliasing cleanness -->
  1060.   </sect1>
  1061.   <sect1 id="conventions-cplusplus">
  1062.    <title>C++</title>
  1063.    
  1064.    <para>
  1065.     Using C++ in the kernel is usually a bad idea, because the
  1066.     kernel does not provide the necessary runtime environment
  1067.     and the include files are not tested for it.  It is still
  1068.     possible, but not recommended.  If you really want to do
  1069.     this, forget about exceptions at least.
  1070.    </para>
  1071.   </sect1>
  1072.   <sect1 id="conventions-ifdef">
  1073.    <title>&num;if</title>
  1074.    
  1075.    <para>
  1076.     It is generally considered cleaner to use macros in header files
  1077.     (or at the top of .c files) to abstract away functions rather than
  1078.     using `#if' pre-processor statements throughout the source code.
  1079.    </para>
  1080.   </sect1>
  1081.  </chapter>
  1082.  <chapter id="submitting">
  1083.   <title>Putting Your Stuff in the Kernel</title>
  1084.   <para>
  1085.    In order to get your stuff into shape for official inclusion, or
  1086.    even to make a neat patch, there's administrative work to be
  1087.    done:
  1088.   </para>
  1089.   <itemizedlist>
  1090.    <listitem>
  1091.     <para>
  1092.      Figure out whose pond you've been pissing in.  Look at the top of
  1093.      the source files, inside the <filename>MAINTAINERS</filename>
  1094.      file, and last of all in the <filename>CREDITS</filename> file.
  1095.      You should coordinate with this person to make sure you're not
  1096.      duplicating effort, or trying something that's already been
  1097.      rejected.
  1098.     </para>
  1099.     <para>
  1100.      Make sure you put your name and EMail address at the top of
  1101.      any files you create or mangle significantly.  This is the
  1102.      first place people will look when they find a bug, or when
  1103.      <emphasis>they</emphasis> want to make a change.
  1104.     </para>
  1105.    </listitem>
  1106.    <listitem>
  1107.     <para>
  1108.      Usually you want a configuration option for your kernel hack.
  1109.      Edit <filename>Config.in</filename> in the appropriate directory
  1110.      (but under <filename>arch/</filename> it's called
  1111.      <filename>config.in</filename>).  The Config Language used is not
  1112.      bash, even though it looks like bash; the safe way is to use only
  1113.      the constructs that you already see in
  1114.      <filename>Config.in</filename> files (see
  1115.      <filename>Documentation/kbuild/config-language.txt</filename>).
  1116.      It's good to run "make xconfig" at least once to test (because
  1117.      it's the only one with a static parser).
  1118.     </para>
  1119.     <para>
  1120.      Variables which can be Y or N use <type>bool</type> followed by a
  1121.      tagline and the config define name (which must start with
  1122.      CONFIG_).  The <type>tristate</type> function is the same, but
  1123.      allows the answer M (which defines
  1124.      <symbol>CONFIG_foo_MODULE</symbol> in your source, instead of
  1125.      <symbol>CONFIG_FOO</symbol>) if <symbol>CONFIG_MODULES</symbol>
  1126.      is enabled.
  1127.     </para>
  1128.     <para>
  1129.      You may well want to make your CONFIG option only visible if
  1130.      <symbol>CONFIG_EXPERIMENTAL</symbol> is enabled: this serves as a
  1131.      warning to users.  There many other fancy things you can do: see
  1132.      the various <filename>Config.in</filename> files for ideas.
  1133.     </para>
  1134.    </listitem>
  1135.    <listitem>
  1136.     <para>
  1137.      Edit the <filename>Makefile</filename>: the CONFIG variables are
  1138.      exported here so you can conditionalize compilation with `ifeq'.
  1139.      If your file exports symbols then add the names to
  1140.      <varname>export-objs</varname> so that genksyms will find them.
  1141.      <caution>
  1142.       <para>
  1143.        There is a restriction on the kernel build system that objects
  1144.        which export symbols must have globally unique names.
  1145.        If your object does not have a globally unique name then the
  1146.        standard fix is to move the
  1147.        <function>EXPORT_SYMBOL()</function> statements to their own
  1148.        object with a unique name.
  1149.        This is why several systems have separate exporting objects,
  1150.        usually suffixed with ksyms.
  1151.       </para>
  1152.      </caution>
  1153.     </para>
  1154.    </listitem>
  1155.    <listitem>
  1156.     <para>
  1157.      Document your option in Documentation/Configure.help.  Mention
  1158.      incompatibilities and issues here.  <emphasis> Definitely
  1159.      </emphasis> end your description with <quote> if in doubt, say N
  1160.      </quote> (or, occasionally, `Y'); this is for people who have no
  1161.      idea what you are talking about.
  1162.     </para>
  1163.    </listitem>
  1164.    <listitem>
  1165.     <para>
  1166.      Put yourself in <filename>CREDITS</filename> if you've done
  1167.      something noteworthy, usually beyond a single file (your name
  1168.      should be at the top of the source files anyway).
  1169.      <filename>MAINTAINERS</filename> means you want to be consulted
  1170.      when changes are made to a subsystem, and hear about bugs; it
  1171.      implies a more-than-passing commitment to some part of the code.
  1172.     </para>
  1173.    </listitem>
  1174.    
  1175.    <listitem>
  1176.     <para>
  1177.      Finally, don't forget to read <filename>Documentation/SubmittingPatches</filename>
  1178.      and possibly <filename>Documentation/SubmittingDrivers</filename>.
  1179.     </para>
  1180.    </listitem>
  1181.   </itemizedlist>
  1182.  </chapter>
  1183.  <chapter id="cantrips">
  1184.   <title>Kernel Cantrips</title>
  1185.   <para>
  1186.    Some favorites from browsing the source.  Feel free to add to this
  1187.    list.
  1188.   </para>
  1189.   <para>
  1190.    <filename>include/linux/brlock.h:</filename>
  1191.   </para>
  1192.   <programlisting>
  1193. extern inline void br_read_lock (enum brlock_indices idx)
  1194. {
  1195.         /*
  1196.          * This causes a link-time bug message if an
  1197.          * invalid index is used:
  1198.          */
  1199.         if (idx >= __BR_END)
  1200.                 __br_lock_usage_bug();
  1201.         read_lock(&amp;__brlock_array[smp_processor_id()][idx]);
  1202. }
  1203.   </programlisting>
  1204.   <para>
  1205.    <filename>include/linux/fs.h</filename>:
  1206.   </para>
  1207.   <programlisting>
  1208. /*
  1209.  * Kernel pointers have redundant information, so we can use a
  1210.  * scheme where we can return either an error code or a dentry
  1211.  * pointer with the same return value.
  1212.  *
  1213.  * This should be a per-architecture thing, to allow different
  1214.  * error and pointer decisions.
  1215.  */
  1216.  #define ERR_PTR(err)    ((void *)((long)(err)))
  1217.  #define PTR_ERR(ptr)    ((long)(ptr))
  1218.  #define IS_ERR(ptr)     ((unsigned long)(ptr) > (unsigned long)(-1000))
  1219. </programlisting>
  1220.   <para>
  1221.    <filename>include/asm-i386/uaccess.h:</filename>
  1222.   </para>
  1223.   <programlisting>
  1224. #define copy_to_user(to,from,n)                         
  1225.         (__builtin_constant_p(n) ?                      
  1226.          __constant_copy_to_user((to),(from),(n)) :     
  1227.          __generic_copy_to_user((to),(from),(n)))
  1228.   </programlisting>
  1229.   <para>
  1230.    <filename>arch/sparc/kernel/head.S:</filename>
  1231.   </para>
  1232.   <programlisting>
  1233. /*
  1234.  * Sun people can't spell worth damn. "compatability" indeed.
  1235.  * At least we *know* we can't spell, and use a spell-checker.
  1236.  */
  1237. /* Uh, actually Linus it is I who cannot spell. Too much murky
  1238.  * Sparc assembly will do this to ya.
  1239.  */
  1240. C_LABEL(cputypvar):
  1241.         .asciz "compatability"
  1242. /* Tested on SS-5, SS-10. Probably someone at Sun applied a spell-checker. */
  1243.         .align 4
  1244. C_LABEL(cputypvar_sun4m):
  1245.         .asciz "compatible"
  1246.   </programlisting>
  1247.   <para>
  1248.    <filename>arch/sparc/lib/checksum.S:</filename>
  1249.   </para>
  1250.   <programlisting>
  1251.         /* Sun, you just can't beat me, you just can't.  Stop trying,
  1252.          * give up.  I'm serious, I am going to kick the living shit
  1253.          * out of you, game over, lights out.
  1254.          */
  1255.   </programlisting>
  1256.  </chapter>
  1257.  <chapter id="credits">
  1258.   <title>Thanks</title>
  1259.   <para>
  1260.    Thanks to Andi Kleen for the idea, answering my questions, fixing
  1261.    my mistakes, filling content, etc.  Philipp Rumpf for more spelling
  1262.    and clarity fixes, and some excellent non-obvious points.  Werner
  1263.    Almesberger for giving me a great summary of
  1264.    <function>disable_irq()</function>, and Jes Sorensen and Andrea
  1265.    Arcangeli added caveats. Michael Elizabeth Chastain for checking
  1266.    and adding to the Configure section. <!-- Rusty insisted on this
  1267.    bit; I didn't do it! --> Telsa Gwynne for teaching me DocBook. 
  1268.   </para>
  1269.  </chapter>
  1270. </book>