Annotation of htdocs/docs/kernel/programming.html, revision 1.20

1.17      jym         1: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
1.1       dsieger     2: <html>
                      3: <head>
                      4: <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
                      5: <meta name="generator" content="Website XSL Stylesheet V2.6.0">
                      6: <link rel="home" href="../../." title="Welcome to The NetBSD Project: Of course it runs NetBSD.">
                      7: <link rel="up" href="../../docs/kernel/." title="NetBSD Documentation: Kernel">
                      8: <link rel="previous" href="../../docs/kernel/pseudo/." title="NetBSD Documentation: Writing a pseudo device">
                      9: <link rel="next" href="../../docs/kernel/uvm.html" title="NetBSD Documentation: UVM, the new Virtual Memory system">
                     10: <link rel="first" href="../../docs/kernel/lazyfpu.html" title="NetBSD Documentation: How lazy FPU context switch works">
                     11: <link rel="last" href="../../docs/kernel/elf-notes.html" title="NetBSD Documentation: Vendor-specific ELF Note Elements">
1.10      dsieger    12: <link rel="stylesheet" href="../../global.css" type="text/css">
1.1       dsieger    13: 
                     14:     
                     15:     
                     16:     <title>NetBSD Documentation: Kernel Programming FAQ</title>
                     17:   </head>
                     18: <body class="website"><div class="webpage">
1.3       dsieger    19: <a name="docs-kernel-programming"></a><div id="top"><a href="#mainContent" class="doNotDisplay doNotPrint">Skip to main content.</a></div>
1.17      jym        20: <div id="centralHeader">
1.19      jym        21: <div id="logo">
                     22: <a href="../../"><img alt="[NetBSD Logo]" src="../../images/NetBSD-smaller.png"></a><h1><a href="../../">
                     23:          The NetBSD Project
                     24:        </a></h1>
                     25: <h2><a href="../../">
                     26:          &#8220;Of course it runs NetBSD&#8221;
                     27:        </a></h2>
                     28: </div>
                     29: <div id="headerTools">
1.17      jym        30: <div id="header-cse-search-form">Google custom search</div>
1.15      spz        31: <script src="http://www.google.com/jsapi" type="text/javascript"></script><script type="text/javascript"> 
                     32:                  google.load('search', '1', {language : 'en'});
                     33:                  google.setOnLoadCallback(function() {
                     34:                    var header_customSearchControl = new google.search.CustomSearchControl('006277936787196004968:mbdhrauy1wm');
                     35:                    header_customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
                     36:                    var header_options = new google.search.DrawOptions();
                     37:                    header_options.enableSearchboxOnly("http://google.com/cse?cx=006277936787196004968:mbdhrauy1wm");    
                     38:                    header_customSearchControl.draw('header-cse-search-form', header_options);
                     39:                  }, true);
1.17      jym        40:                </script>
                     41: </div>
1.3       dsieger    42: </div>
1.17      jym        43: <div id="navBar" role="navigation">
1.6       dsieger    44: <span class="doNotDisplay">
                     45:          Navigation:
1.17      jym        46:        </span><ul>
                     47: <li><a href="../../">
                     48:          Home</a></li>
                     49: <li>
                     50: <a href="../../">
1.20    ! wiz        51:          News &amp; Media</a><ul>
1.17      jym        52: <li><a href="../../changes/">
                     53:            Recent changes</a></li>
1.18      jym        54: <li><a href="http://blog.NetBSD.org/">
1.17      jym        55:            NetBSD blog</a></li>
                     56: <li><a href="../../gallery/events.html">
                     57:            Events</a></li>
                     58: <li><a href="../../changes/rss.html">
                     59:            Feeds</a></li>
                     60: </ul>
                     61: </li>
                     62: <li>
                     63: <a href="../../about/">
                     64:          About</a><ul>
                     65: <li><a href="../../about/">
                     66:            NetBSD</a></li>
                     67: <li><a href="../../foundation/">
                     68:            The NetBSD Foundation</a></li>
                     69: <li><a href="../../gallery/">
                     70:            Advocacy</a></li>
                     71: </ul>
                     72: </li>
                     73: <li>
                     74: <a href="../../docs/">
                     75:          Documentation</a><ul>
                     76: <li><a href="../../docs/misc/index.html">
                     77:            FAQ &amp; HOWTOs</a></li>
                     78: <li><a href="../../docs/guide/en/">
                     79:            The Guide</a></li>
1.18      jym        80: <li><a href="http://man.NetBSD.org/">
1.17      jym        81:            Manual pages</a></li>
1.18      jym        82: <li><a href="http://wiki.NetBSD.org/">
1.17      jym        83:            Wiki</a></li>
                     84: </ul>
                     85: </li>
                     86: <li>
                     87: <a href="../../support/">
                     88:          Support</a><ul>
                     89: <li><a href="../../support/send-pr.html">
                     90:            Problem report guide</a></li>
                     91: <li><a href="../../cgi-bin/sendpr.cgi?gndb=netbsd">
                     92:            Report a bug</a></li>
                     93: <li><a href="../../support/query-pr.html">
                     94:            Query bug database</a></li>
                     95: <li><a href="../../support/security/">
                     96:            Security</a></li>
                     97: </ul>
                     98: </li>
                     99: <li>
                    100: <a href="../../community/">
                    101:          Community</a><ul>
                    102: <li><a href="http://onetbsd.org/">
                    103:            Blogs</a></li>
                    104: <li><a href="../../mailinglists/">
                    105:            Mailing lists</a></li>
1.18      jym       106: <li><a href="http://mail-index.NetBSD.org/">
1.17      jym       107:            List archives</a></li>
                    108: </ul>
                    109: </li>
                    110: <li>
                    111: <a href="../../developers/">
                    112:          Developers</a><ul>
1.18      jym       113: <li><a href="http://cvsweb.NetBSD.org/">
1.17      jym       114:            Browse source</a></li>
1.18      jym       115: <li><a href="http://nxr.NetBSD.org/">
1.17      jym       116:            Cross-reference</a></li>
1.18      jym       117: <li><a href="http://releng.NetBSD.org/">
1.17      jym       118:            Release engineering</a></li>
1.18      jym       119: <li><a href="http://wiki.NetBSD.org/projects/">
1.17      jym       120:            Projects list</a></li>
                    121: </ul>
                    122: </li>
                    123: <li>
                    124: <a href="../../ports/">
                    125:          Ports</a><ul>
                    126: <li><a href="../../ports/history.html">
                    127:            History</a></li>
                    128: <li><a href="../../ports/emulators.html">
                    129:            Emulators</a></li>
                    130: </ul>
                    131: </li>
                    132: <li>
                    133: <a href="http://www.pkgsrc.org/">
                    134:          Packages</a><ul>
                    135: <li><a href="http://www.pkgsrc.se/">
                    136:            Browse packages</a></li>
1.18      jym       137: <li><a href="http://releng.NetBSD.org/index-pkgsrc.html">
1.17      jym       138:            Release engineering</a></li>
                    139: </ul>
                    140: </li>
                    141: </ul>
1.3       dsieger   142: </div>
                    143: <div id="content"><div class="fullWidth"><div class="rowOfBoxes">
                    144: <h1>NetBSD Documentation: Kernel Programming FAQ</h1>
1.1       dsieger   145: <h3 class="title"><a name="misc">Misc</a></h3>
                    146: <ul>
                    147: <li><a href="#knf">What is KNF</a></li>
                    148: <li><a href="#packed-attribute">Using the `packed' attribute</a></li>
                    149: <li><a href="#printf">Using printf() for debugging</a></li>
                    150: <li><a href="#forcing-ddb">Forcing code to enter DDB</a></li>
                    151: <li><a href="#adding_a_new_driver">Adding a new driver to the kernel</a></li>
                    152: <li><a href="#autoconf">How does all this autoconf stuff work?</a></li>
                    153: <li><a href="#adding_a_system_call">Adding a system call</a></li>
                    154: <li><a href="#adding_a_sysctl">Adding a sysctl</a></li>
                    155: <li><a href="#mmap_in_pseudo-device">How to implement mmap(2) in a pseudo-device</a></li>
                    156: <li><a href="#accessing_a_kernel_structure_from_userland">Accessing a kernel structure from userland</a></li>
                    157: <li><a href="#sample_driver">Is there a simple PCI driver I can use as an example?</a></li>
                    158: <li><a href="#other-related-links">Other related links</a></li>
                    159: </ul>
                    160: <hr>
                    161: <h3 class="title">Misc</h3>
                    162:        <h4 class="title">
                    163: <a name="knf"></a>What is KNF (<a href="#misc">top</a>)
                    164:   </h4>
                    165: 
                    166:        <p>KNF stands for "Kernel Normal Form" - it's a C coding
                    167:          style documented in
                    168:          <code class="filename">/usr/share/misc/style</code>, which is
                    169:          included in the source tree as <a href="http://cvsweb.NetBSD.org/bsdweb.cgi/src/share/misc/style?rev=HEAD&amp;content-type=text/x-cvsweb-markup" target="_top"><code class="filename">src/share/misc/style</code></a>.</p>
                    170:       
                    171:        <h4 class="title">
                    172: <a name="packed-attribute"></a>Using the `packed' attribute (<a href="#misc">top</a>)
                    173:   </h4>
                    174:        
                    175:        <p>Always use the <code class="code">`packed'</code> attribute in
                    176:          structures which describe wire protocol data formats.</p>
                    177:       
                    178:        <h4 class="title">
                    179: <a name="printf"></a>Using <code class="code">printf()</code> for debugging (<a href="#misc">top</a>)
                    180:   </h4>
                    181: 
                    182:        <p>Probably the simplest way of generating debugging
                    183:          information from a kernel driver is to use
                    184:          <code class="code">printf()</code>. The kernel printf will send output to
                    185:          the console, so beware of generating too much output and
                    186:          making the system unusable.</p>
                    187:       
                    188:        <h4 class="title">
                    189: <a name="forcing-ddb"></a>Forcing code to enter DDB (<a href="#misc">top</a>)
                    190:   </h4>
                    191: 
                    192:        <p>Ensure your kernel config file contains
                    193:          '<code class="code">options DDB</code>', the file has
                    194:          '<code class="code">#include "opt_ddb.h"</code>', then use
                    195:          '<code class="code">Debugger()</code>'.</p>
                    196:       
                    197:        <h4 class="title">
                    198: <a name="adding_a_new_driver"></a>Adding a new driver to the kernel (<a href="#misc">top</a>)
                    199:   </h4>
                    200: 
                    201:        <p>Every driver needs at least:
                    202:          </p>
1.15      spz       203: <div class="itemizedlist"><ul class="itemizedlist" type="disc">
                    204: <li class="listitem">
1.1       dsieger   205: <code class="code"><span class="emphasis"><em>xxx</em></span>probe()</code> (
                    206:              during which NetBSD will attempt to determine if the
                    207:              device is present)</li>
1.15      spz       208: <li class="listitem">
1.1       dsieger   209: <code class="code"><span class="emphasis"><em>xxx</em></span>attach()</code>
                    210:              routine which will configure and attach the
                    211:              device.</li>
                    212: </ul></div>
                    213: <p>
                    214:        </p>
                    215: 
                    216:        <p>Once probe and attach routines have been written, add
                    217:          an entry to 
                    218:          <code class="filename">/usr/src/sys/arch/&lt;your-arch&gt;/&lt;your-arch&gt;/conf.c</code>.</p>
                    219:        
                    220:        <p>There are two tables:
                    221:          </p>
1.15      spz       222: <div class="itemizedlist"><ul class="itemizedlist" type="disc">
                    223: <li class="listitem">
1.1       dsieger   224: <code class="code">cdevsw</code> for character devices.</li>
1.15      spz       225: <li class="listitem">
1.1       dsieger   226: <code class="code">bdevsw</code> for block devices (for those
                    227:              that also perform "block" I/O and use a strategy
                    228:              routine).</li>
                    229: </ul></div>
                    230: 
                    231:        <p>Most entries will be of the form
                    232:          <code class="code">cdev_<span class="emphasis"><em>xxx</em></span>_init()</code>, which
                    233:          is a macro handling prototyping of the standard Unix
                    234:          device switch routines.</p>
                    235: 
                    236:        <p>The probe/attach routines are called at boot time.  The
                    237:          <code class="code">open()</code>, <code class="code">close()</code>,
                    238:          <code class="code">read()</code>, and <code class="code">write()</code> routines are
                    239:          called when you open up the device special file who's major
                    240:          number corresponds to the index into that table.  For
                    241:          example, if you open up a device who's major number is 18,
                    242:          the "open" routine for device number 18 in
                    243:          <code class="code">cdevsw[]/bdevsw</code> will be called.</p>
                    244: 
                    245:        <p>Most drivers are split between bus specific attach code, and
                    246:          a machine independent core. As an example, the driver for
                    247:          the PCI lance ethernet chip has entries in the following files:
                    248: 
                    249:          </p>
1.15      spz       250: <div class="itemizedlist"><ul class="itemizedlist" type="disc">
                    251: <li class="listitem">
1.1       dsieger   252: <a href="http://cvsweb.NetBSD.org/bsdweb.cgi/src/sys/dev/pci/files.pci?rev=HEAD&amp;content-type=text/x-cvsweb-markup" target="_top"><code class="filename">src/sys/dev/pci/files.pci</code></a>
                    253:              - attach information (look for 'le at pci').</li>
1.15      spz       254: <li class="listitem">
1.1       dsieger   255: <a href="http://cvsweb.NetBSD.org/bsdweb.cgi/src/sys/dev/pci/if_le_pci.c?rev=HEAD&amp;content-type=text/x-cvsweb-markup" target="_top"><code class="filename">src/sys/dev/pci/if_le_pci.c</code></a> 
                    256:              - PCI bus attach code for the driver.</li>
                    257: </ul></div>
                    258: <p>
                    259:        </p>
                    260:        
                    261:        <p>
                    262:          </p>
1.15      spz       263: <div class="itemizedlist"><ul class="itemizedlist" type="disc">
                    264: <li class="listitem">
1.1       dsieger   265: <a href="http://cvsweb.NetBSD.org/bsdweb.cgi/src/sys/conf/files?rev=HEAD&amp;content-type=text/x-cvsweb-markup" target="_top"><code class="filename">src/sys/conf/files</code></a>
                    266:              - MI core attach information (look for 'le:').</li>
1.15      spz       267: <li class="listitem">
1.1       dsieger   268: <a href="http://cvsweb.NetBSD.org/bsdweb.cgi/src/sys/dev/ic/am7990.c?rev=HEAD&amp;content-type=text/x-cvsweb-markup" target="_top"><code class="filename">src/sys/dev/ic/am7990.c</code></a>
                    269:              - MI driver 24bit access code.</li>
1.15      spz       270: <li class="listitem">
1.1       dsieger   271: <a href="http://cvsweb.NetBSD.org/bsdweb.cgi/src/sys/dev/ic/am79900.c?rev=HEAD&amp;content-type=text/x-cvsweb-markup" target="_top"><code class="filename">src/sys/dev/ic/am79900.c</code></a>
                    272:              - MI driver 32bit access code.</li>
1.15      spz       273: <li class="listitem">
1.1       dsieger   274: <a href="http://cvsweb.NetBSD.org/bsdweb.cgi/src/sys/dev/ic/lance.c?rev=HEAD&amp;content-type=text/x-cvsweb-markup" target="_top"><code class="filename">src/sys/dev/ic/lance.c</code></a>
                    275:              - MI core driver code.</li>
                    276: </ul></div>
                    277: <p>
                    278:        </p>
                    279: 
1.12      hubertf   280:        <p>See also <a class="ulink" href="#autoconf" target="_top">the autoconf explanation</a>.</p>
1.1       dsieger   281:       
                    282:        <h4 class="title">
                    283: <a name="autoconf"></a>How does all this autoconf stuff work? (<a href="#misc">top</a>)
                    284:   </h4>
                    285:        
                    286:        <p>The autoconf machinery is quite simple once you figure
                    287:          out the way it works. If you want to ignore the exact
                    288:          details of how the device probe tree is built and walked
                    289:            on runtime, the bits needed for each individual
1.15      spz       290:          <span class="quote">&#8220;<span class="quote">leaf</span>&#8221;</span> driver are like this:
1.1       dsieger   291:          
                    292:          </p>
1.15      spz       293: <div class="orderedlist"><ol class="orderedlist" type="1">
                    294: <li class="listitem">each driver specifies a structure holding
1.1       dsieger   295:              three things - size of its private structure, probe
                    296:              function and attach function; this is compiled in and
                    297:              used in runtime - example:
                    298:              <pre class="programlisting">
                    299: struct cfattach foo_baz_ca = {
                    300:     sizeof(struct foo_baz_softc), foo_baz_match, foo_baz_attach
                    301: };</pre>
                    302: </li>
1.15      spz       303: <li class="listitem">on kernel startup, once the time comes to
1.1       dsieger   304:              attach the device, autoconf code calls device's
                    305:              probe routine and passes it pointer to parent
                    306:              (<code class="code">struct device *parent</code>), pointer
                    307:              to attach tag structure (<code class="code">void *aux</code>),
                    308:              and appropriate   autoconf node (<code class="code">struct cfdata
                    309:                *cf</code>). The driver is expected to find out if
                    310:              it's where it's supposed to be (commonly, the location
                    311:              and configuration information is passed by the attach
                    312:              tag). If yes, the probe routine should return 1. If
                    313:              device is not there, probe routine has to return 0.
                    314:              <span class="bold"><strong>NO STATE SHOULD BE KEPT</strong></span>
                    315:              in either case.</li>
1.15      spz       316: <li class="listitem">if probe returned success, autoconf allocates
1.1       dsieger   317:              chunk of memory sized as specified in device's *_ca
                    318:              and calls its attach routine, passing it pointer to
                    319:              parent (<code class="code">struct device *parent</code>), 
                    320:              pointer to the freshly allocated memory
                    321:              (<code class="code">struct device *self</code>) and the attach tag
                    322:              (<code class="code">void *aux</code>). Driver is expected to find
                    323:              out exact ports and memory, allocate resources and
                    324:              initialize its internal structure
                    325:              accordingly. Preferably, all driver instance specific
                    326:              information should be kept in the allocated
                    327:              memory.</li>
                    328: </ol></div>
                    329: <p>
                    330:        </p>
                    331:        
                    332:        <p>Example: Let's have a PCI ethernet device 'baz',
                    333:          kernel config chunk looks like this:
                    334:          </p>
                    335: <pre class="programlisting">
                    336: pci*    at mainbus?
                    337: baz*    at pci? dev ? function ?</pre>
                    338: <p>
                    339: 
                    340:          At runtime, autoconf iterates over all physical devices
                    341:          present on machine's PCI bus. For each physical device, it
                    342:          iterates over all devices registered in kernel to be on
                    343:          pci bus, and calls drivers' probe routine. If any probe
                    344:          routine claims the device by returning 1, autoconf stops
                    345:          iterating and does the job described under 3). Once the
                    346:          attach function returns, autoconf continues with next
                    347:          physical device.</p>
                    348:        
1.12      hubertf   349:        <p>See also <a class="ulink" href="#adding_a_new_driver" target="_top">Adding a new driver</a>.</p>
1.1       dsieger   350:       
                    351:        <h4 class="title">
                    352: <a name="adding_a_system_call"></a>Adding a system call (<a href="#misc">top</a>)
                    353:   </h4>
                    354:        
                    355:        <p>Add an entry in <code class="code">syscalls.master</code>, and add
                    356:          the syscall stub to the appropriate place in 
                    357:          <code class="code"><a href="http://cvsweb.NetBSD.org/bsdweb.cgi/src/lib/libc/sys/Makefile.inc?rev=HEAD&amp;content-type=text/x-cvsweb-markup" target="_top"><code class="filename">src/lib/libc/sys/Makefile.inc</code></a></code>.</p>
1.12      hubertf   358:        <p>See the <a class="ulink" href="../internals/en/chap-processes.html#syscall_howto" target="_top">
                    359:             HOWTO</a> and related documentation in the <a class="ulink" href="../internals/en/" target="_top">
1.1       dsieger   360:            NetBSD Internals Guide</a> for more information.
                    361:        </p>
                    362:       
                    363:        <h4 class="title">
                    364: <a name="adding_a_sysctl"></a>Adding a sysctl (<a href="#misc">top</a>)
                    365:   </h4>
                    366:        
1.12      hubertf   367:        <p>See a <a class="ulink" href="http://mail-index.NetBSD.org/tech-kern/2001/06/24/0000.html" target="_top">posting</a>
                    368:          answering this question on  <a class="ulink" href="http://mail-index.NetBSD.org/tech-kern/" target="_top">tech-kern</a>.</p>
1.1       dsieger   369:        
                    370:        <p>Note that NetBSD 1.6 and up has a special
1.15      spz       371:          <span class="quote">&#8220;<span class="quote">vendor</span>&#8221;</span> sysctl category that is reserved for vendor
                    372:          specific entries. See <a href="http://netbsd.gw.com/cgi-bin/man-cgi?sysctl+8+NetBSD-5.1+i386">sysctl(8)</a> for more
1.1       dsieger   373:          information.</p>
                    374:       
                    375:        <h4 class="title">
1.15      spz       376: <a name="mmap_in_pseudo-device"></a>How to implement <a href="http://netbsd.gw.com/cgi-bin/man-cgi?mmap+2+NetBSD-5.1+i386">mmap(2)</a> in a pseudo-device (<a href="#misc">top</a>)
1.1       dsieger   377:   </h4>
                    378:        
                    379:        <p>Your device is most likely a character device, so
                    380:          you will be using the device pager (the VM system hides
                    381:          all of this from you, don't worry).</p>
                    382:        
                    383:        <p>The first thing you need to do is pick some
                    384:          arbitrary offsets for your mmap interface.  Something
                    385:          like "mmap offset 0-M gives object A, N-O gives object
                    386:          B", etc.</p>
                    387:        
                    388:        <p>After that, your mmap routine would look something
                    389:          like this:
                    390:          </p>
                    391: <pre class="programlisting">
                    392: int
                    393: foommap(dev_t dev, int off, int prot)
                    394: {
                    395: 
                    396:         if (off &amp; PAGE_MASK)
                    397:                 panic("foommap");
                    398: 
                    399:         if ((u_int)off &gt;= FOO_REGION1_MMAP_OFFSET &amp;&amp;
                    400:             (u_int)off &lt; (FOO_REGION1_MMAP_OFFSET + FOO_REGION1_SIZE))
                    401:                 return (atop(FOO_REGION1_ADDR + ((u_int)off -
                    402:                     FOO_REGION1_MMAP_OFFSET)));
                    403: 
                    404:         if ((u_int)off &gt;= FOO_REGION2_MMAP_OFFSET &amp;&amp;
                    405:             (u_int)off &lt; (FOO_REGION2_MMAP_OFFSET + FOO_REGION2_SIZE))
                    406:                 return (atop(FOO_REGION1_ADDR + ((u_int)off -
                    407:                     FOO_REGION2_MMAP_OFFSET)));
                    408: 
                    409:         /* Page not found. */
                    410:         return (-1);
                    411: }</pre>
                    412: 
                    413:          <p>Now, this is slightly more complicated by the fact
                    414:            that you are going to be mmap'ing what are simply
                    415:            kernel memory objects (it is a pseudo-device after
                    416:            all).</p>
                    417:          
                    418:          <p>In order to make this work, you're going to want
                    419:            to make sure you allocate the memory objects to be
                    420:            mmap'd on page-aligned boundaries.  If you are
                    421:            allocating something &gt;= <code class="code">PAGE_SIZE</code> in
                    422:            size, this is guaranteed. Otherwise, you are going to
                    423:            have to use <code class="code">uvm_km_alloc()</code>, and round
                    424:            your allocation size up to page size.</p>
                    425:          
                    426:          <p>Then it would look a bit more like this:
                    427:            </p>
                    428: <pre class="programlisting">
                    429: int
                    430: foommap(dev_t dev, int off, int prot)
                    431: {
                    432:         paddr_t pa;
                    433: 
                    434:         if (off &amp; PAGE_MASK)
                    435:                 panic("foommap: offset not page aligned");
                    436: 
                    437:         if ((u_int)off &gt;= FOO_REGION1_MMAP_OFFSET &amp;&amp;
                    438:             (u_int)off &lt; (FOO_REGION1_MMAP_OFFSET + FOO_REGION1_SIZE)) {
                    439:                 if ((vaddr_t)foo_object1 &amp; PAGE_MASK)
                    440:                         panic("foommap: foo_object1 not page aligned");
                    441:                 if (pmap_extract(pmap_kernel(), foo_object1 +
                    442:                     (u_int)off - FOO_REGION1_MMAP_OFFSET, &amp;pa) == FALSE)
                    443:                         panic("foommap: foo_object1 page not mapped");
                    444:                 return (atop(pa));
                    445:         }
                    446: 
                    447:         if ((u_int)off &gt;= FOO_REGION2_MMAP_OFFSET &amp;&amp;
                    448:             (u_int)off &lt; (FOO_REGION2_MMAP_OFFSET + FOO_REGION2_SIZE)) {
                    449:                 if ((vaddr_t)foo_object2 &amp; PAGE_MASK)
                    450:                         panic("foommap: foo_object2 not page aligned");
                    451:                 if (pmap_extract(pmap_kernel(), foo_object2 +
                    452:                     (u_int)off - FOO_REGION2_MMAP_OFFSET, &amp;pa) == FALSE)
                    453:                         panic("foommap: foo_object2 page not mapped");
                    454:                 return (atop(pa));
                    455:         }
                    456: 
                    457:         /* Page not found. */
                    458:         return (-1);
                    459: }</pre>
                    460:       
                    461:        <h4 class="title">
                    462: <a name="accessing_a_kernel_structure_from_userland"></a>Accessing a kernel structure from userland (<a href="#misc">top</a>)
                    463:   </h4>
                    464:        
                    465:        <p>The canonical example for this is:  <code class="code">
                    466:            <a href="http://cvsweb.NetBSD.org/bsdweb.cgi/src/usr.bin/vmstat/dkstats.c?rev=HEAD&amp;content-type=text/x-cvsweb-markup" target="_top"><code class="filename">src/usr.bin/vmstat/dkstats.c</code></a></code>
                    467:          , which reads disk statistics.</p>
                    468:       
                    469:        <h4 class="title">
                    470: <a name="sample_driver"></a>Is there a simple PCI driver I can use as an example? (<a href="#misc">top</a>)
                    471:   </h4>
                    472:        <p>You can look at
                    473:          <code class="filename">sys/dev/pci/puc.c</code>, which is one of
                    474:          the simplest drivers. PUCs are devices with one or more
                    475:          serial or parallel ports on it, usually using standard
                    476:          chips (e.g. 16550 UART for serial). This driver just
                    477:          locates the I/O addresses of the registers of the serial or
                    478:          parallel controller and passes it to the serial or
                    479:          parallel driver.</p> 
                    480:       
                    481:        <h4 class="title">
                    482: <a name="other-related-links"></a>Other related links (<a href="#misc">top</a>)
                    483:   </h4>
                    484:        
1.15      spz       485: <div class="itemizedlist"><ul class="itemizedlist" type="disc">
                    486: <li class="listitem">
                    487: <a href="http://netbsd.gw.com/cgi-bin/man-cgi?driver+9+NetBSD-5.1+i386">driver(9)</a> - NetBSD autoconfiguration
1.1       dsieger   488:            interface utilised by device drivers</li>
1.15      spz       489: <li class="listitem">
                    490: <a href="http://netbsd.gw.com/cgi-bin/man-cgi?autoconf+9+NetBSD-5.1+i386">autoconf(9)</a> - General description on the
1.1       dsieger   491:            NetBSD autoconfiguration framework</li>
1.15      spz       492: <li class="listitem">
                    493: <a href="http://netbsd.gw.com/cgi-bin/man-cgi?config+9+NetBSD-5.1+i386">config(9)</a> - The autoconfiguration
1.1       dsieger   494:            framework ``device definition'' language</li>
1.15      spz       495: <li class="listitem">
                    496: <a href="http://netbsd.gw.com/cgi-bin/man-cgi?bus_dma+9+NetBSD-5.1+i386">bus_dma(9)</a> - NetBSD's bus and machine
1.12      hubertf   497:            independent DMA framework, described in its own <a class="ulink" href="bus_dma.pdf" target="_top">paper</a> (64k, PDF)</li>
1.15      spz       498: <li class="listitem">
                    499: <a href="http://netbsd.gw.com/cgi-bin/man-cgi?bus_space+9+NetBSD-5.1+i386">bus_space(9)</a> - NetBSD's bus space
1.1       dsieger   500:            manipulation interface</li>
1.15      spz       501: <li class="listitem">
1.12      hubertf   502: <a class="ulink" href="scsidma.html" target="_top">How SCSI DMA
1.1       dsieger   503:              works</a> - by Tohru Nishimura</li>
1.15      spz       504: <li class="listitem">
1.12      hubertf   505: <a class="ulink" href="lazyfpu.html" target="_top">How lazy FPU context
1.1       dsieger   506:              switch works</a> - by Tohru Nishimura</li>
1.15      spz       507: <li class="listitem"><a class="ulink" href="converting-ethernet-drivers.html" target="_top">Converting ancient BSD Ethernet drivers to NetBSD-1.2D and later</a></li>
                    508: <li class="listitem"><a class="ulink" href="porting-freebsd-net.html" target="_top">Notes on porting FreeBSD network drivers to NetBSD</a></li>
1.1       dsieger   509: </ul></div>
1.3       dsieger   510:       <hr>Back to  <em><a href="./">NetBSD Documentation: Kernel</a></em>
                    511: </div></div></div>
                    512: <div class="navfoot"></div>
1.13      mishka    513: <div id="footer"><div id="footerContent"><center>
1.3       dsieger   514: <span class="footfeed"><a href="http://www.NetBSD.org/cgi-bin/feedback.cgi">
                    515:          Contact</a> |
1.9       kano      516:       </span><span class="footcopy"><a href="../../about/disclaimer.html">
1.3       dsieger   517:       Disclaimer</a> |
                    518: 
1.16      darcy     519:       <span class="copyright">Copyright © 1994-2012 The NetBSD Foundation, Inc. </span>ALL RIGHTS RESERVED.<br>NetBSD<sup>®</sup> is a registered trademark of The NetBSD
1.3       dsieger   520:        Foundation, Inc.</span>
1.13      mishka    521: </center></div></div>
1.1       dsieger   522: </div></body>
                    523: </html>

CVSweb <webmaster@jp.NetBSD.org>