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/kern/subr_firmware.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) 2005, Sam Leffler <sam@errno.com>
    3  * 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 unmodified, this list of conditions, and the following
   10  *    disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   25  */
   26 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/6.3/sys/kern/subr_firmware.c 173886 2007-11-24 19:45:58Z cvs2svn $");
   29 
   30 #include <sys/param.h>
   31 #include <sys/kernel.h>
   32 #include <sys/malloc.h>
   33 #include <sys/queue.h>
   34 #include <sys/taskqueue.h>
   35 #include <sys/systm.h>
   36 #include <sys/lock.h>
   37 #include <sys/mutex.h>
   38 #include <sys/errno.h>
   39 #include <sys/linker.h>
   40 #include <sys/firmware.h>
   41 #include <sys/proc.h>
   42 #include <sys/module.h>
   43 
   44 /*
   45  * Loadable firmware support. See sys/sys/firmware.h and firmware(9)
   46  * form more details on the subsystem.
   47  *
   48  * 'struct firmware' is the user-visible part of the firmware table.
   49  * Additional internal information is stored in a 'struct priv_fw'
   50  * (currently a static array). A slot is in use if FW_INUSE is true:
   51  */
   52 
   53 #define FW_INUSE(p)     ((p)->file != NULL || (p)->fw.name != NULL)
   54 
   55 /*
   56  * fw.name != NULL when an image is registered; file != NULL for
   57  * autoloaded images whose handling has not been completed.
   58  *
   59  * The state of a slot evolves as follows:
   60  *      firmware_register       -->  fw.name = image_name
   61  *      (autoloaded image)      -->  file = module reference
   62  *      firmware_unregister     -->  fw.name = NULL
   63  *      (unloadentry complete)  -->  file = NULL
   64  *
   65  * In order for the above to work, the 'file' field must remain
   66  * unchanged in firmware_unregister().
   67  *
   68  * Images residing in the same module are linked to each other
   69  * through the 'parent' argument of firmware_register().
   70  * One image (typically, one with the same name as the module to let
   71  * the autoloading mechanism work) is considered the parent image for
   72  * all other images in the same module. Children affect the refcount
   73  * on the parent image preventing improper unloading of the image itself.
   74  */
   75 
   76 struct priv_fw {
   77         int             refcnt;         /* reference count */
   78 
   79         /*
   80          * parent entry, see above. Set on firmware_register(),
   81          * cleared on firmware_unregister().
   82          */
   83         struct priv_fw  *parent;
   84 
   85         int             flags;  /* record FIRMWARE_UNLOAD requests */
   86 #define FW_UNLOAD       0x100
   87 
   88         /*
   89          * 'file' is private info managed by the autoload/unload code.
   90          * Set at the end of firmware_get(), cleared only in the
   91          * firmware_task, so the latter can depend on its value even
   92          * while the lock is not held.
   93          */
   94         linker_file_t   file;   /* module file, if autoloaded */
   95 
   96         /*
   97          * 'fw' is the externally visible image information.
   98          * We do not make it the first field in priv_fw, to avoid the
   99          * temptation of casting pointers to each other.
  100          * Use PRIV_FW(fw) to get a pointer to the cointainer of fw.
  101          * Beware, PRIV_FW does not work for a NULL pointer.
  102          */
  103         struct firmware fw;     /* externally visible information */
  104 };
  105 
  106 /*
  107  * PRIV_FW returns the pointer to the container of struct firmware *x.
  108  * Cast to intptr_t to override the 'const' attribute of x
  109  */
  110 #define PRIV_FW(x)      ((struct priv_fw *)             \
  111         ((intptr_t)(x) - offsetof(struct priv_fw, fw)) )
  112 
  113 /*
  114  * At the moment we use a static array as backing store for the registry.
  115  * Should we move to a dynamic structure, keep in mind that we cannot
  116  * reallocate the array because pointers are held externally.
  117  * A list may work, though.
  118  */
  119 #define FIRMWARE_MAX    30
  120 static struct priv_fw firmware_table[FIRMWARE_MAX];
  121 
  122 /*
  123  * module release are handled in a separate task as they might sleep.
  124  */
  125 struct task firmware_task;
  126 
  127 /*
  128  * This mutex protects accesses to the firmware table.
  129  */
  130 struct mtx firmware_mtx;
  131 MTX_SYSINIT(firmware, &firmware_mtx, "firmware table", MTX_DEF);
  132 
  133 /*
  134  * Helper function to lookup a name.
  135  * As a side effect, it sets the pointer to a free slot, if any.
  136  * This way we can concentrate most of the registry scanning in
  137  * this function, which makes it easier to replace the registry
  138  * with some other data structure.
  139  */
  140 static struct priv_fw *
  141 lookup(const char *name, struct priv_fw **empty_slot)
  142 {
  143         struct priv_fw *fp = NULL;
  144         struct priv_fw *dummy;
  145         int i;
  146 
  147         if (empty_slot == NULL)
  148                 empty_slot = &dummy;
  149         *empty_slot = NULL;
  150         for (i = 0; i < FIRMWARE_MAX; i++) {
  151                 fp = &firmware_table[i];
  152                 if (fp->fw.name != NULL && strcasecmp(name, fp->fw.name) == 0)
  153                         break;
  154                 else if (!FW_INUSE(fp))
  155                         *empty_slot = fp;
  156         }
  157         return (i < FIRMWARE_MAX ) ? fp : NULL;
  158 }
  159 
  160 /*
  161  * Register a firmware image with the specified name.  The
  162  * image name must not already be registered.  If this is a
  163  * subimage then parent refers to a previously registered
  164  * image that this should be associated with.
  165  */
  166 const struct firmware *
  167 firmware_register(const char *imagename, const void *data, size_t datasize,
  168     unsigned int version, const struct firmware *parent)
  169 {
  170         struct priv_fw *match, *frp;
  171 
  172         mtx_lock(&firmware_mtx);
  173         /*
  174          * Do a lookup to make sure the name is unique or find a free slot.
  175          */
  176         match = lookup(imagename, &frp);
  177         if (match != NULL) {
  178                 mtx_unlock(&firmware_mtx);
  179                 printf("%s: image %s already registered!\n",
  180                         __func__, imagename);
  181                 return NULL;
  182         }
  183         if (frp == NULL) {
  184                 mtx_unlock(&firmware_mtx);
  185                 printf("%s: cannot register image %s, firmware table full!\n",
  186                     __func__, imagename);
  187                 return NULL;
  188         }
  189         bzero(frp, sizeof(frp));        /* start from a clean record */
  190         frp->fw.name = imagename;
  191         frp->fw.data = data;
  192         frp->fw.datasize = datasize;
  193         frp->fw.version = version;
  194         if (parent != NULL) {
  195                 frp->parent = PRIV_FW(parent);
  196                 frp->parent->refcnt++;
  197         }
  198         mtx_unlock(&firmware_mtx);
  199         if (bootverbose)
  200                 printf("firmware: '%s' version %u: %zu bytes loaded at %p\n",
  201                     imagename, version, datasize, data);
  202         return &frp->fw;
  203 }
  204 
  205 /*
  206  * Unregister/remove a firmware image.  If there are outstanding
  207  * references an error is returned and the image is not removed
  208  * from the registry.
  209  */
  210 int
  211 firmware_unregister(const char *imagename)
  212 {
  213         struct priv_fw *fp;
  214         int err;
  215 
  216         mtx_lock(&firmware_mtx);
  217         fp = lookup(imagename, NULL);
  218         if (fp == NULL) {
  219                 /*
  220                  * It is ok for the lookup to fail; this can happen
  221                  * when a module is unloaded on last reference and the
  222                  * module unload handler unregister's each of it's
  223                  * firmware images.
  224                  */
  225                 err = 0;
  226         } else if (fp->refcnt != 0) {   /* cannot unregister */
  227                 err = EBUSY;
  228         }  else {
  229                 linker_file_t   x = fp->file;   /* save value */
  230 
  231                 if (fp->parent != NULL) /* release parent reference */
  232                         fp->parent->refcnt--;
  233                 /*
  234                  * Clear the whole entry with bzero to make sure we
  235                  * do not forget anything. Then restore 'file' which is
  236                  * non-null for autoloaded images.
  237                  */
  238                 bzero(fp, sizeof(struct priv_fw));
  239                 fp->file = x;
  240                 err = 0;
  241         }
  242         mtx_unlock(&firmware_mtx);
  243         return err;
  244 }
  245 
  246 /*
  247  * Lookup and potentially load the specified firmware image.
  248  * If the firmware is not found in the registry, try to load a kernel
  249  * module named as the image name.
  250  * If the firmware is located, a reference is returned. The caller must
  251  * release this reference for the image to be eligible for removal/unload.
  252  */
  253 const struct firmware *
  254 firmware_get(const char *imagename)
  255 {
  256         struct thread *td;
  257         struct priv_fw *fp;
  258         linker_file_t result;
  259 
  260         mtx_lock(&firmware_mtx);
  261         fp = lookup(imagename, NULL);
  262         if (fp != NULL)
  263                 goto found;
  264         /*
  265          * Image not present, try to load the module holding it.
  266          */
  267         mtx_unlock(&firmware_mtx);
  268         td = curthread;
  269         if (suser(td) != 0 ||
  270             securelevel_gt(td->td_ucred, 0) != 0) {
  271                 printf("%s: insufficient privileges to "
  272                     "load firmware image %s\n", __func__, imagename);
  273                 return NULL;
  274         }
  275         (void) linker_reference_module(imagename, NULL, &result);
  276         /*
  277          * After loading the module, see if the image is registered now.
  278          */
  279         mtx_lock(&firmware_mtx);
  280         fp = lookup(imagename, NULL);
  281         if (fp == NULL) {
  282                 mtx_unlock(&firmware_mtx);
  283                 printf("%s: failed to load firmware image %s\n",
  284                         __func__, imagename);
  285                 (void) linker_release_module(imagename, NULL, NULL);
  286                 return NULL;
  287         }
  288         fp->file = result;      /* record the module identity */
  289 
  290 found:                          /* common exit point on success */
  291         fp->refcnt++;
  292         mtx_unlock(&firmware_mtx);
  293         return &fp->fw;
  294 }
  295 
  296 /*
  297  * Release a reference to a firmware image returned by firmware_get.
  298  * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire
  299  * to release the resource, but the flag is only advisory.
  300  *
  301  * If this is the last reference to the firmware image, and this is an
  302  * autoloaded module, wake up the firmware_task to figure out what to do
  303  * with the associated module.
  304  */
  305 void
  306 firmware_put(const struct firmware *p, int flags)
  307 {
  308         struct priv_fw *fp = PRIV_FW(p);
  309 
  310         mtx_lock(&firmware_mtx);
  311         fp->refcnt--;
  312         if (fp->refcnt == 0) {
  313                 if (flags & FIRMWARE_UNLOAD)
  314                         fp->flags |= FW_UNLOAD;
  315                 if (fp->file)
  316                         taskqueue_enqueue(taskqueue_thread, &firmware_task);
  317         }
  318         mtx_unlock(&firmware_mtx);
  319 }
  320 
  321 /*
  322  * The body of the task in charge of unloading autoloaded modules
  323  * that are not needed anymore.
  324  * Images can be cross-linked so we may need to make multiple passes,
  325  * but the time we spend in the loop is bounded because we clear entries
  326  * as we touch them.
  327  */
  328 static void
  329 unloadentry(void *unused1, int unused2)
  330 {
  331         int limit = FIRMWARE_MAX;
  332         int i;  /* current cycle */
  333 
  334         mtx_lock(&firmware_mtx);
  335         /*
  336          * Scan the table. limit is set to make sure we make another
  337          * full sweep after matching an entry that requires unloading.
  338          */
  339         for (i = 0; i < limit; i++) {
  340                 struct priv_fw *fp;
  341                 int err;
  342 
  343                 fp = &firmware_table[i % FIRMWARE_MAX];
  344                 if (fp->fw.name == NULL || fp->file == NULL ||
  345                     fp->refcnt != 0 || (fp->flags & FW_UNLOAD) == 0)
  346                         continue;
  347 
  348                 /*
  349                  * Found an entry. Now:
  350                  * 1. bump up limit to make sure we make another full round;
  351                  * 2. clear FW_UNLOAD so we don't try this entry again.
  352                  * 3. release the lock while trying to unload the module.
  353                  * 'file' remains set so that the entry cannot be reused
  354                  * in the meantime (it also means that fp->file will
  355                  * not change while we release the lock).
  356                  */
  357                 limit = i + FIRMWARE_MAX;       /* make another full round */
  358                 fp->flags &= ~FW_UNLOAD;        /* do not try again */
  359 
  360                 mtx_unlock(&firmware_mtx);
  361                 err = linker_release_module(NULL, NULL, fp->file);
  362                 mtx_lock(&firmware_mtx);
  363 
  364                 /*
  365                  * We rely on the module to call firmware_unregister()
  366                  * on unload to actually release the entry.
  367                  * If err = 0 we can drop our reference as the system
  368                  * accepted it. Otherwise unloading failed (e.g. the
  369                  * module itself gave an error) so our reference is
  370                  * still valid.
  371                  */
  372                 if (err == 0)
  373                         fp->file = NULL; 
  374         }
  375         mtx_unlock(&firmware_mtx);
  376 }
  377 
  378 /*
  379  * Module glue.
  380  */
  381 static int
  382 firmware_modevent(module_t mod, int type, void *unused)
  383 {
  384         struct priv_fw *fp;
  385         int i, err = EINVAL;
  386 
  387         switch (type) {
  388         case MOD_LOAD:
  389                 TASK_INIT(&firmware_task, 0, unloadentry, NULL);
  390                 return 0;
  391 
  392         case MOD_UNLOAD:
  393                 /* request all autoloaded modules to be released */
  394                 mtx_lock(&firmware_mtx);
  395                 for (i = 0; i < FIRMWARE_MAX; i++) {
  396                         fp = &firmware_table[i];
  397                         fp->flags |= FW_UNLOAD;;
  398                 }
  399                 mtx_unlock(&firmware_mtx);
  400                 taskqueue_enqueue(taskqueue_thread, &firmware_task);
  401                 taskqueue_drain(taskqueue_thread, &firmware_task);
  402                 for (i = 0; i < FIRMWARE_MAX; i++) {
  403                         fp = &firmware_table[i];
  404                         if (fp->fw.name != NULL) {
  405                                 printf("%s: image %p ref %d still active slot %d\n",
  406                                         __func__, fp->fw.name,
  407                                         fp->refcnt,  i);
  408                                 err = EINVAL;
  409                         }
  410                 }
  411                 return err;
  412         }
  413         return EINVAL;
  414 }
  415 
  416 static moduledata_t firmware_mod = {
  417         "firmware",
  418         firmware_modevent,
  419         0
  420 };
  421 DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
  422 MODULE_VERSION(firmware, 1);

Cache object: a07cd0b430739e8f1c8d8bdcf9e1df4d


[ 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.