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

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

CVSweb <webmaster@jp.NetBSD.org>