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

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

CVSweb <webmaster@jp.NetBSD.org>