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

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

CVSweb <webmaster@jp.NetBSD.org>