The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/compat/ndis/subr_hal.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2003
    3  *      Bill Paul <wpaul@windriver.com>.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by Bill Paul.
   16  * 4. Neither the name of the author nor the names of any co-contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
   24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   30  * THE POSSIBILITY OF SUCH DAMAGE.
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD: releng/6.0/sys/compat/ndis/subr_hal.c 151742 2005-10-27 17:08:57Z wpaul $");
   35 
   36 #include <sys/param.h>
   37 #include <sys/types.h>
   38 #include <sys/errno.h>
   39 
   40 #include <sys/callout.h>
   41 #include <sys/kernel.h>
   42 #include <sys/lock.h>
   43 #include <sys/mutex.h>
   44 #include <sys/proc.h>
   45 #include <sys/sched.h>
   46 #include <sys/module.h>
   47 
   48 #include <sys/systm.h>
   49 #include <machine/clock.h>
   50 #include <machine/bus.h>
   51 
   52 #include <sys/bus.h>
   53 #include <sys/rman.h>
   54 
   55 #include <compat/ndis/pe_var.h>
   56 #include <compat/ndis/resource_var.h>
   57 #include <compat/ndis/cfg_var.h>
   58 #include <compat/ndis/ntoskrnl_var.h>
   59 #include <compat/ndis/hal_var.h>
   60 
   61 static void KeStallExecutionProcessor(uint32_t);
   62 static void WRITE_PORT_BUFFER_ULONG(uint32_t *,
   63         uint32_t *, uint32_t);
   64 static void WRITE_PORT_BUFFER_USHORT(uint16_t *,
   65         uint16_t *, uint32_t);
   66 static void WRITE_PORT_BUFFER_UCHAR(uint8_t *,
   67         uint8_t *, uint32_t);
   68 static void WRITE_PORT_ULONG(uint32_t *, uint32_t);
   69 static void WRITE_PORT_USHORT(uint16_t *, uint16_t);
   70 static void WRITE_PORT_UCHAR(uint8_t *, uint8_t);
   71 static uint32_t READ_PORT_ULONG(uint32_t *);
   72 static uint16_t READ_PORT_USHORT(uint16_t *);
   73 static uint8_t READ_PORT_UCHAR(uint8_t *);
   74 static void READ_PORT_BUFFER_ULONG(uint32_t *,
   75         uint32_t *, uint32_t);
   76 static void READ_PORT_BUFFER_USHORT(uint16_t *,
   77         uint16_t *, uint32_t);
   78 static void READ_PORT_BUFFER_UCHAR(uint8_t *,
   79         uint8_t *, uint32_t);
   80 static uint64_t KeQueryPerformanceCounter(uint64_t *);
   81 static void dummy (void);
   82 
   83 #define NDIS_MAXCPUS 64
   84 static struct mtx disp_lock[NDIS_MAXCPUS];
   85 
   86 int
   87 hal_libinit()
   88 {
   89         image_patch_table       *patch;
   90         int                     i;
   91 
   92         for (i = 0; i < NDIS_MAXCPUS; i++)
   93                 mtx_init(&disp_lock[i], "HAL preemption lock",
   94                     "HAL lock", MTX_RECURSE|MTX_DEF);
   95 
   96         patch = hal_functbl;
   97         while (patch->ipt_func != NULL) {
   98                 windrv_wrap((funcptr)patch->ipt_func,
   99                     (funcptr *)&patch->ipt_wrap,
  100                     patch->ipt_argcnt, patch->ipt_ftype);
  101                 patch++;
  102         }
  103 
  104         
  105         return(0);
  106 }
  107 
  108 int
  109 hal_libfini()
  110 {
  111         image_patch_table       *patch;
  112         int                     i;
  113 
  114         for (i = 0; i < NDIS_MAXCPUS; i++)
  115                 mtx_destroy(&disp_lock[i]);
  116 
  117         patch = hal_functbl;
  118         while (patch->ipt_func != NULL) {
  119                 windrv_unwrap(patch->ipt_wrap);
  120                 patch++;
  121         }
  122 
  123         return(0);
  124 }
  125 
  126 static void
  127 KeStallExecutionProcessor(usecs)
  128         uint32_t                usecs;
  129 {
  130         DELAY(usecs);
  131         return;
  132 }
  133 
  134 static void
  135 WRITE_PORT_ULONG(port, val)
  136         uint32_t                *port;
  137         uint32_t                val;
  138 {
  139         bus_space_write_4(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port, val);
  140         return;
  141 }
  142 
  143 static void
  144 WRITE_PORT_USHORT(port, val)
  145         uint16_t                *port;
  146         uint16_t                val;
  147 {
  148         bus_space_write_2(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port, val);
  149         return;
  150 }
  151 
  152 static void
  153 WRITE_PORT_UCHAR(port, val)
  154         uint8_t                 *port;
  155         uint8_t                 val;
  156 {
  157         bus_space_write_1(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port, val);
  158         return;
  159 }
  160 
  161 static void
  162 WRITE_PORT_BUFFER_ULONG(port, val, cnt)
  163         uint32_t                *port;
  164         uint32_t                *val;
  165         uint32_t                cnt;
  166 {
  167         bus_space_write_multi_4(NDIS_BUS_SPACE_IO, 0x0,
  168             (bus_size_t)port, val, cnt);
  169         return;
  170 }
  171 
  172 static void
  173 WRITE_PORT_BUFFER_USHORT(port, val, cnt)
  174         uint16_t                *port;
  175         uint16_t                *val;
  176         uint32_t                cnt;
  177 {
  178         bus_space_write_multi_2(NDIS_BUS_SPACE_IO, 0x0,
  179             (bus_size_t)port, val, cnt);
  180         return;
  181 }
  182 
  183 static void
  184 WRITE_PORT_BUFFER_UCHAR(port, val, cnt)
  185         uint8_t                 *port;
  186         uint8_t                 *val;
  187         uint32_t                cnt;
  188 {
  189         bus_space_write_multi_1(NDIS_BUS_SPACE_IO, 0x0,
  190             (bus_size_t)port, val, cnt);
  191         return;
  192 }
  193 
  194 static uint16_t
  195 READ_PORT_USHORT(port)
  196         uint16_t                *port;
  197 {
  198         return(bus_space_read_2(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port));
  199 }
  200 
  201 static uint32_t
  202 READ_PORT_ULONG(port)
  203         uint32_t                *port;
  204 {
  205         return(bus_space_read_4(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port));
  206 }
  207 
  208 static uint8_t
  209 READ_PORT_UCHAR(port)
  210         uint8_t                 *port;
  211 {
  212         return(bus_space_read_1(NDIS_BUS_SPACE_IO, 0x0, (bus_size_t)port));
  213 }
  214 
  215 static void
  216 READ_PORT_BUFFER_ULONG(port, val, cnt)
  217         uint32_t                *port;
  218         uint32_t                *val;
  219         uint32_t                cnt;
  220 {
  221         bus_space_read_multi_4(NDIS_BUS_SPACE_IO, 0x0,
  222             (bus_size_t)port, val, cnt);
  223         return;
  224 }
  225 
  226 static void
  227 READ_PORT_BUFFER_USHORT(port, val, cnt)
  228         uint16_t                *port;
  229         uint16_t                *val;
  230         uint32_t                cnt;
  231 {
  232         bus_space_read_multi_2(NDIS_BUS_SPACE_IO, 0x0,
  233             (bus_size_t)port, val, cnt);
  234         return;
  235 }
  236 
  237 static void
  238 READ_PORT_BUFFER_UCHAR(port, val, cnt)
  239         uint8_t                 *port;
  240         uint8_t                 *val;
  241         uint32_t                cnt;
  242 {
  243         bus_space_read_multi_1(NDIS_BUS_SPACE_IO, 0x0,
  244             (bus_size_t)port, val, cnt);
  245         return;
  246 }
  247 
  248 /*
  249  * The spinlock implementation in Windows differs from that of FreeBSD.
  250  * The basic operation of spinlocks involves two steps: 1) spin in a
  251  * tight loop while trying to acquire a lock, 2) after obtaining the
  252  * lock, disable preemption. (Note that on uniprocessor systems, you're
  253  * allowed to skip the first step and just lock out pre-emption, since
  254  * it's not possible for you to be in contention with another running
  255  * thread.) Later, you release the lock then re-enable preemption.
  256  * The difference between Windows and FreeBSD lies in how preemption
  257  * is disabled. In FreeBSD, it's done using critical_enter(), which on
  258  * the x86 arch translates to a cli instruction. This masks off all
  259  * interrupts, and effectively stops the scheduler from ever running
  260  * so _nothing_ can execute except the current thread. In Windows,
  261  * preemption is disabled by raising the processor IRQL to DISPATCH_LEVEL.
  262  * This stops other threads from running, but does _not_ block device
  263  * interrupts. This means ISRs can still run, and they can make other
  264  * threads runable, but those other threads won't be able to execute
  265  * until the current thread lowers the IRQL to something less than
  266  * DISPATCH_LEVEL.
  267  *
  268  * There's another commonly used IRQL in Windows, which is APC_LEVEL.
  269  * An APC is an Asynchronous Procedure Call, which differs from a DPC
  270  * (Defered Procedure Call) in that a DPC is queued up to run in
  271  * another thread, while an APC runs in the thread that scheduled
  272  * it (similar to a signal handler in a UNIX process). We don't
  273  * actually support the notion of APCs in FreeBSD, so for now, the
  274  * only IRQLs we're interested in are DISPATCH_LEVEL and PASSIVE_LEVEL.
  275  *
  276  * To simulate DISPATCH_LEVEL, we raise the current thread's priority
  277  * to PI_REALTIME, which is the highest we can give it. This should,
  278  * if I understand things correctly, prevent anything except for an
  279  * interrupt thread from preempting us. PASSIVE_LEVEL is basically
  280  * everything else.
  281  *
  282  * Be aware that, at least on the x86 arch, the Windows spinlock
  283  * functions are divided up in peculiar ways. The actual spinlock
  284  * functions are KfAcquireSpinLock() and KfReleaseSpinLock(), and
  285  * they live in HAL.dll. Meanwhile, KeInitializeSpinLock(),
  286  * KefAcquireSpinLockAtDpcLevel() and KefReleaseSpinLockFromDpcLevel()
  287  * live in ntoskrnl.exe. Most Windows source code will call
  288  * KeAcquireSpinLock() and KeReleaseSpinLock(), but these are just
  289  * macros that call KfAcquireSpinLock() and KfReleaseSpinLock().
  290  * KefAcquireSpinLockAtDpcLevel() and KefReleaseSpinLockFromDpcLevel()
  291  * perform the lock aquisition/release functions without doing the
  292  * IRQL manipulation, and are used when one is already running at
  293  * DISPATCH_LEVEL. Make sense? Good.
  294  *
  295  * According to the Microsoft documentation, any thread that calls
  296  * KeAcquireSpinLock() must be running at IRQL <= DISPATCH_LEVEL. If
  297  * we detect someone trying to acquire a spinlock from DEVICE_LEVEL
  298  * or HIGH_LEVEL, we panic.
  299  *
  300  * Alternate sleep-lock-based spinlock implementation
  301  * --------------------------------------------------
  302  *
  303  * The earlier spinlock implementation was arguably a bit of a hack
  304  * and presented several problems. It was basically designed to provide
  305  * the functionality of spinlocks without incurring the wrath of
  306  * WITNESS. We could get away with using both our spinlock implementation
  307  * and FreeBSD sleep locks at the same time, but if WITNESS knew what
  308  * we were really up to, it would have spanked us rather severely.
  309  *
  310  * There's another method we can use based entirely on sleep locks.
  311  * First, it's important to realize that everything we're locking
  312  * resides inside Project Evil itself: any critical data being locked
  313  * by drivers belongs to the drivers, and should not be referenced
  314  * by any other OS code outside of the NDISulator. The priority-based
  315  * locking scheme has system-wide effects, just like real spinlocks
  316  * (blocking preemption affects the whole CPU), but since we keep all
  317  * our critical data private, we can use a simpler mechanism that
  318  * affects only code/threads directly related to Project Evil.
  319  *
  320  * The idea is to create a sleep lock mutex for each CPU in the system.
  321  * When a CPU running in the NDISulator wants to acquire a spinlock, it
  322  * does the following:
  323  * - Pin ourselves to the current CPU
  324  * - Acquire the mutex for the current CPU
  325  * - Spin on the spinlock variable using atomic test and set, just like
  326  *   a real spinlock.
  327  * - Once we have the lock, we execute our critical code
  328  *
  329  * To give up the lock, we do:
  330  * - Clear the spinlock variable with an atomic op
  331  * - Release the per-CPU mutex
  332  * - Unpin ourselves from the current CPU.
  333  *
  334  * On a uniprocessor system, this means all threads that access protected
  335  * data are serialized through the per-CPU mutex. After one thread
  336  * acquires the 'spinlock,' any other thread that uses a spinlock on the
  337  * current CPU will block on the per-CPU mutex, which has the same general
  338  * effect of blocking pre-emption, but _only_ for those threads that are
  339  * running NDISulator code.
  340  *
  341  * On a multiprocessor system, threads on different CPUs all block on
  342  * their respective per-CPU mutex, and the atomic test/set operation
  343  * on the spinlock variable provides inter-CPU synchronization, though
  344  * only for threads running NDISulator code.
  345  *
  346  * This method solves an important problem. In Windows, you're allowed
  347  * to do an ExAllocatePoolWithTag() with a spinlock held, provided you
  348  * allocate from NonPagedPool. This implies an atomic heap allocation
  349  * that will not cause the current thread to sleep. (You can't sleep
  350  * while holding real spinlock: clowns will eat you.) But in FreeBSD,
  351  * malloc(9) _always_ triggers the acquisition of a sleep lock, even
  352  * when you use M_NOWAIT. This is not a problem for FreeBSD native
  353  * code: you're allowed to sleep in things like interrupt threads. But
  354  * it is a problem with the old priority-based spinlock implementation:
  355  * even though we get away with it most of the time, we really can't
  356  * do a malloc(9) after doing a KeAcquireSpinLock() or KeRaiseIrql().
  357  * With the new implementation, it's not a problem: you're allowed to
  358  * acquire more than one sleep lock (as long as you avoid lock order
  359  * reversals).
  360  *
  361  * The one drawback to this approach is that now we have a lot of
  362  * contention on one per-CPU mutex within the NDISulator code. Whether
  363  * or not this is preferable to the expected Windows spinlock behavior
  364  * of blocking pre-emption is debatable.
  365  */
  366 
  367 uint8_t
  368 KfAcquireSpinLock(lock)
  369         kspin_lock              *lock;
  370 {
  371         uint8_t                 oldirql;
  372 
  373         /* I am so going to hell for this. */
  374         if (KeGetCurrentIrql() > DISPATCH_LEVEL)
  375                 panic("IRQL_NOT_LESS_THAN_OR_EQUAL");
  376 
  377         KeRaiseIrql(DISPATCH_LEVEL, &oldirql);
  378         KeAcquireSpinLockAtDpcLevel(lock);
  379 
  380         return(oldirql);
  381 }
  382 
  383 void
  384 KfReleaseSpinLock(lock, newirql)
  385         kspin_lock              *lock;
  386         uint8_t                 newirql;
  387 {
  388         KeReleaseSpinLockFromDpcLevel(lock);
  389         KeLowerIrql(newirql);
  390 
  391         return;
  392 }
  393 
  394 uint8_t
  395 KeGetCurrentIrql()
  396 {
  397         if (mtx_owned(&disp_lock[curthread->td_oncpu]))
  398                 return(DISPATCH_LEVEL);
  399         return(PASSIVE_LEVEL);
  400 }
  401 
  402 static uint64_t
  403 KeQueryPerformanceCounter(freq)
  404         uint64_t                *freq;
  405 {
  406         if (freq != NULL)
  407                 *freq = hz;
  408 
  409         return((uint64_t)ticks);
  410 }
  411 
  412 uint8_t
  413 KfRaiseIrql(irql)
  414         uint8_t                 irql;
  415 {
  416         uint8_t                 oldirql;
  417 
  418         oldirql = KeGetCurrentIrql();
  419         if (irql < oldirql)
  420                 panic("IRQL_NOT_LESS_THAN");
  421 
  422         if (oldirql != DISPATCH_LEVEL) {
  423                 sched_pin();
  424                 mtx_lock(&disp_lock[curthread->td_oncpu]);
  425         }
  426 
  427         return(oldirql);
  428 }
  429 
  430 void 
  431 KfLowerIrql(oldirql)
  432         uint8_t                 oldirql;
  433 {
  434         if (oldirql == DISPATCH_LEVEL)
  435                 return;
  436 
  437         if (KeGetCurrentIrql() != DISPATCH_LEVEL)
  438                 panic("IRQL_NOT_GREATER_THAN");
  439 
  440         mtx_unlock(&disp_lock[curthread->td_oncpu]);
  441         sched_unpin();
  442 
  443         return;
  444 }
  445 
  446 static void dummy()
  447 {
  448         printf ("hal dummy called...\n");
  449         return;
  450 }
  451 
  452 image_patch_table hal_functbl[] = {
  453         IMPORT_SFUNC(KeStallExecutionProcessor, 1),
  454         IMPORT_SFUNC(WRITE_PORT_ULONG, 2),
  455         IMPORT_SFUNC(WRITE_PORT_USHORT, 2),
  456         IMPORT_SFUNC(WRITE_PORT_UCHAR, 2),
  457         IMPORT_SFUNC(WRITE_PORT_BUFFER_ULONG, 3),
  458         IMPORT_SFUNC(WRITE_PORT_BUFFER_USHORT, 3),
  459         IMPORT_SFUNC(WRITE_PORT_BUFFER_UCHAR, 3),
  460         IMPORT_SFUNC(READ_PORT_ULONG, 1),
  461         IMPORT_SFUNC(READ_PORT_USHORT, 1),
  462         IMPORT_SFUNC(READ_PORT_UCHAR, 1),
  463         IMPORT_SFUNC(READ_PORT_BUFFER_ULONG, 3),
  464         IMPORT_SFUNC(READ_PORT_BUFFER_USHORT, 3),
  465         IMPORT_SFUNC(READ_PORT_BUFFER_UCHAR, 3),
  466         IMPORT_FFUNC(KfAcquireSpinLock, 1),
  467         IMPORT_FFUNC(KfReleaseSpinLock, 1),
  468         IMPORT_SFUNC(KeGetCurrentIrql, 0),
  469         IMPORT_SFUNC(KeQueryPerformanceCounter, 1),
  470         IMPORT_FFUNC(KfLowerIrql, 1),
  471         IMPORT_FFUNC(KfRaiseIrql, 1),
  472 
  473         /*
  474          * This last entry is a catch-all for any function we haven't
  475          * implemented yet. The PE import list patching routine will
  476          * use it for any function that doesn't have an explicit match
  477          * in this table.
  478          */
  479 
  480         { NULL, (FUNC)dummy, NULL, 0, WINDRV_WRAP_STDCALL },
  481 
  482         /* End of list. */
  483 
  484         { NULL, NULL, NULL }
  485 };

Cache object: 269089d5f04e4640ec40a486d444ccc5


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.