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

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

CVSweb <webmaster@jp.NetBSD.org>