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-2008, 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$");
   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 #include <sys/eventhandler.h>
   44 
   45 #include <sys/filedesc.h>
   46 #include <sys/vnode.h>
   47 
   48 /*
   49  * Loadable firmware support. See sys/sys/firmware.h and firmware(9)
   50  * form more details on the subsystem.
   51  *
   52  * 'struct firmware' is the user-visible part of the firmware table.
   53  * Additional internal information is stored in a 'struct priv_fw'
   54  * (currently a static array). A slot is in use if FW_INUSE is true:
   55  */
   56 
   57 #define FW_INUSE(p)     ((p)->file != NULL || (p)->fw.name != NULL)
   58 
   59 /*
   60  * fw.name != NULL when an image is registered; file != NULL for
   61  * autoloaded images whose handling has not been completed.
   62  *
   63  * The state of a slot evolves as follows:
   64  *      firmware_register       -->  fw.name = image_name
   65  *      (autoloaded image)      -->  file = module reference
   66  *      firmware_unregister     -->  fw.name = NULL
   67  *      (unloadentry complete)  -->  file = NULL
   68  *
   69  * In order for the above to work, the 'file' field must remain
   70  * unchanged in firmware_unregister().
   71  *
   72  * Images residing in the same module are linked to each other
   73  * through the 'parent' argument of firmware_register().
   74  * One image (typically, one with the same name as the module to let
   75  * the autoloading mechanism work) is considered the parent image for
   76  * all other images in the same module. Children affect the refcount
   77  * on the parent image preventing improper unloading of the image itself.
   78  */
   79 
   80 struct priv_fw {
   81         int             refcnt;         /* reference count */
   82 
   83         /*
   84          * parent entry, see above. Set on firmware_register(),
   85          * cleared on firmware_unregister().
   86          */
   87         struct priv_fw  *parent;
   88 
   89         int             flags;  /* record FIRMWARE_UNLOAD requests */
   90 #define FW_UNLOAD       0x100
   91 
   92         /*
   93          * 'file' is private info managed by the autoload/unload code.
   94          * Set at the end of firmware_get(), cleared only in the
   95          * firmware_unload_task, so the latter can depend on its value even
   96          * while the lock is not held.
   97          */
   98         linker_file_t   file;   /* module file, if autoloaded */
   99 
  100         /*
  101          * 'fw' is the externally visible image information.
  102          * We do not make it the first field in priv_fw, to avoid the
  103          * temptation of casting pointers to each other.
  104          * Use PRIV_FW(fw) to get a pointer to the cointainer of fw.
  105          * Beware, PRIV_FW does not work for a NULL pointer.
  106          */
  107         struct firmware fw;     /* externally visible information */
  108 };
  109 
  110 /*
  111  * PRIV_FW returns the pointer to the container of struct firmware *x.
  112  * Cast to intptr_t to override the 'const' attribute of x
  113  */
  114 #define PRIV_FW(x)      ((struct priv_fw *)             \
  115         ((intptr_t)(x) - offsetof(struct priv_fw, fw)) )
  116 
  117 /*
  118  * At the moment we use a static array as backing store for the registry.
  119  * Should we move to a dynamic structure, keep in mind that we cannot
  120  * reallocate the array because pointers are held externally.
  121  * A list may work, though.
  122  */
  123 #define FIRMWARE_MAX    30
  124 static struct priv_fw firmware_table[FIRMWARE_MAX];
  125 
  126 /*
  127  * Firmware module operations are handled in a separate task as they
  128  * might sleep and they require directory context to do i/o.
  129  */
  130 static struct taskqueue *firmware_tq;
  131 static struct task firmware_unload_task;
  132 
  133 /*
  134  * This mutex protects accesses to the firmware table.
  135  */
  136 static struct mtx firmware_mtx;
  137 MTX_SYSINIT(firmware, &firmware_mtx, "firmware table", MTX_DEF);
  138 
  139 /*
  140  * Helper function to lookup a name.
  141  * As a side effect, it sets the pointer to a free slot, if any.
  142  * This way we can concentrate most of the registry scanning in
  143  * this function, which makes it easier to replace the registry
  144  * with some other data structure.
  145  */
  146 static struct priv_fw *
  147 lookup(const char *name, struct priv_fw **empty_slot)
  148 {
  149         struct priv_fw *fp = NULL;
  150         struct priv_fw *dummy;
  151         int i;
  152 
  153         if (empty_slot == NULL)
  154                 empty_slot = &dummy;
  155         *empty_slot = NULL;
  156         for (i = 0; i < FIRMWARE_MAX; i++) {
  157                 fp = &firmware_table[i];
  158                 if (fp->fw.name != NULL && strcasecmp(name, fp->fw.name) == 0)
  159                         break;
  160                 else if (!FW_INUSE(fp))
  161                         *empty_slot = fp;
  162         }
  163         return (i < FIRMWARE_MAX ) ? fp : NULL;
  164 }
  165 
  166 /*
  167  * Register a firmware image with the specified name.  The
  168  * image name must not already be registered.  If this is a
  169  * subimage then parent refers to a previously registered
  170  * image that this should be associated with.
  171  */
  172 const struct firmware *
  173 firmware_register(const char *imagename, const void *data, size_t datasize,
  174     unsigned int version, const struct firmware *parent)
  175 {
  176         struct priv_fw *match, *frp;
  177 
  178         mtx_lock(&firmware_mtx);
  179         /*
  180          * Do a lookup to make sure the name is unique or find a free slot.
  181          */
  182         match = lookup(imagename, &frp);
  183         if (match != NULL) {
  184                 mtx_unlock(&firmware_mtx);
  185                 printf("%s: image %s already registered!\n",
  186                         __func__, imagename);
  187                 return NULL;
  188         }
  189         if (frp == NULL) {
  190                 mtx_unlock(&firmware_mtx);
  191                 printf("%s: cannot register image %s, firmware table full!\n",
  192                     __func__, imagename);
  193                 return NULL;
  194         }
  195         bzero(frp, sizeof(frp));        /* start from a clean record */
  196         frp->fw.name = imagename;
  197         frp->fw.data = data;
  198         frp->fw.datasize = datasize;
  199         frp->fw.version = version;
  200         if (parent != NULL) {
  201                 frp->parent = PRIV_FW(parent);
  202                 frp->parent->refcnt++;
  203         }
  204         mtx_unlock(&firmware_mtx);
  205         if (bootverbose)
  206                 printf("firmware: '%s' version %u: %zu bytes loaded at %p\n",
  207                     imagename, version, datasize, data);
  208         return &frp->fw;
  209 }
  210 
  211 /*
  212  * Unregister/remove a firmware image.  If there are outstanding
  213  * references an error is returned and the image is not removed
  214  * from the registry.
  215  */
  216 int
  217 firmware_unregister(const char *imagename)
  218 {
  219         struct priv_fw *fp;
  220         int err;
  221 
  222         mtx_lock(&firmware_mtx);
  223         fp = lookup(imagename, NULL);
  224         if (fp == NULL) {
  225                 /*
  226                  * It is ok for the lookup to fail; this can happen
  227                  * when a module is unloaded on last reference and the
  228                  * module unload handler unregister's each of it's
  229                  * firmware images.
  230                  */
  231                 err = 0;
  232         } else if (fp->refcnt != 0) {   /* cannot unregister */
  233                 err = EBUSY;
  234         }  else {
  235                 linker_file_t x = fp->file;     /* save value */
  236 
  237                 if (fp->parent != NULL) /* release parent reference */
  238                         fp->parent->refcnt--;
  239                 /*
  240                  * Clear the whole entry with bzero to make sure we
  241                  * do not forget anything. Then restore 'file' which is
  242                  * non-null for autoloaded images.
  243                  */
  244                 bzero(fp, sizeof(struct priv_fw));
  245                 fp->file = x;
  246                 err = 0;
  247         }
  248         mtx_unlock(&firmware_mtx);
  249         return err;
  250 }
  251 
  252 static void
  253 loadimage(void *arg, int npending)
  254 {
  255         struct thread *td = curthread;
  256         char *imagename = arg;
  257         struct priv_fw *fp;
  258         linker_file_t result;
  259         int error;
  260 
  261         /* synchronize with the thread that dispatched us */
  262         mtx_lock(&firmware_mtx);
  263         mtx_unlock(&firmware_mtx);
  264 
  265         if (td->td_proc->p_fd->fd_rdir == NULL) {
  266                 printf("%s: root not mounted yet, no way to load image\n",
  267                     imagename);
  268                 goto done;
  269         }
  270         error = linker_reference_module(imagename, NULL, &result);
  271         if (error != 0) {
  272                 printf("%s: could not load firmware image, error %d\n",
  273                     imagename, error);
  274                 goto done;
  275         }
  276 
  277         mtx_lock(&firmware_mtx);
  278         fp = lookup(imagename, NULL);
  279         if (fp == NULL || fp->file != NULL) {
  280                 mtx_unlock(&firmware_mtx);
  281                 if (fp == NULL)
  282                         printf("%s: firmware image loaded, "
  283                             "but did not register\n", imagename);
  284                 (void) linker_release_module(imagename, NULL, NULL);
  285                 goto done;
  286         }
  287         fp->file = result;      /* record the module identity */
  288         mtx_unlock(&firmware_mtx);
  289 done:
  290         wakeup_one(imagename);          /* we're done */
  291 }
  292 
  293 /*
  294  * Lookup and potentially load the specified firmware image.
  295  * If the firmware is not found in the registry, try to load a kernel
  296  * module named as the image name.
  297  * If the firmware is located, a reference is returned. The caller must
  298  * release this reference for the image to be eligible for removal/unload.
  299  */
  300 const struct firmware *
  301 firmware_get(const char *imagename)
  302 {
  303         struct task fwload_task;
  304         struct thread *td;
  305         struct priv_fw *fp;
  306 
  307         mtx_lock(&firmware_mtx);
  308         fp = lookup(imagename, NULL);
  309         if (fp != NULL)
  310                 goto found;
  311         /*
  312          * Image not present, try to load the module holding it.
  313          */
  314         td = curthread;
  315         if (suser(td) != 0 ||
  316             securelevel_gt(td->td_ucred, 0) != 0) {
  317                 mtx_unlock(&firmware_mtx);
  318                 printf("%s: insufficient privileges to "
  319                     "load firmware image %s\n", __func__, imagename);
  320                 return NULL;
  321         }
  322         /* 
  323          * Defer load to a thread with known context.  linker_reference_module
  324          * may do filesystem i/o which requires root & current dirs, etc.
  325          * Also we must not hold any mtx's over this call which is problematic.
  326          */
  327         if (!cold) {
  328                 TASK_INIT(&fwload_task, 0, loadimage, __DECONST(void *,
  329                     imagename));
  330                 taskqueue_enqueue(firmware_tq, &fwload_task);
  331                 msleep(__DECONST(void *, imagename), &firmware_mtx, 0,
  332                     "fwload", 0);
  333         }
  334         /*
  335          * After attempting to load the module, see if the image is registered.
  336          */
  337         fp = lookup(imagename, NULL);
  338         if (fp == NULL) {
  339                 mtx_unlock(&firmware_mtx);
  340                 return NULL;
  341         }
  342 found:                          /* common exit point on success */
  343         fp->refcnt++;
  344         mtx_unlock(&firmware_mtx);
  345         return &fp->fw;
  346 }
  347 
  348 /*
  349  * Release a reference to a firmware image returned by firmware_get.
  350  * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire
  351  * to release the resource, but the flag is only advisory.
  352  *
  353  * If this is the last reference to the firmware image, and this is an
  354  * autoloaded module, wake up the firmware_unload_task to figure out
  355  * what to do with the associated module.
  356  */
  357 void
  358 firmware_put(const struct firmware *p, int flags)
  359 {
  360         struct priv_fw *fp = PRIV_FW(p);
  361 
  362         mtx_lock(&firmware_mtx);
  363         fp->refcnt--;
  364         if (fp->refcnt == 0) {
  365                 if (flags & FIRMWARE_UNLOAD)
  366                         fp->flags |= FW_UNLOAD;
  367                 if (fp->file)
  368                         taskqueue_enqueue(firmware_tq, &firmware_unload_task);
  369         }
  370         mtx_unlock(&firmware_mtx);
  371 }
  372 
  373 /*
  374  * Setup directory state for the firmware_tq thread so we can do i/o.
  375  */
  376 static void
  377 set_rootvnode(void *arg, int npending)
  378 {
  379         struct thread *td = curthread;
  380         struct proc *p = td->td_proc;
  381 
  382         FILEDESC_LOCK(p->p_fd);
  383         if (p->p_fd->fd_cdir == NULL) {
  384                 p->p_fd->fd_cdir = rootvnode;
  385                 VREF(rootvnode);
  386         }
  387         if (p->p_fd->fd_rdir == NULL) {
  388                 p->p_fd->fd_rdir = rootvnode;
  389                 VREF(rootvnode);
  390         }
  391         FILEDESC_UNLOCK(p->p_fd);
  392 
  393         free(arg, M_TEMP);
  394 }
  395 
  396 /*
  397  * Event handler called on mounting of /; bounce a task
  398  * into the task queue thread to setup it's directories.
  399  */
  400 static void
  401 firmware_mountroot(void *arg)
  402 {
  403         struct task *setroot_task;
  404 
  405         setroot_task = malloc(sizeof(struct task), M_TEMP, M_NOWAIT);
  406         if (setroot_task != NULL) {
  407                 TASK_INIT(setroot_task, 0, set_rootvnode, setroot_task);
  408                 taskqueue_enqueue(firmware_tq, setroot_task);
  409         } else
  410                 printf("%s: no memory for task!\n", __func__);
  411 }
  412 
  413 static eventhandler_tag mountroot_tag;
  414 static void
  415 mountroot_evh_init(void *ctx)
  416 {
  417         mountroot_tag = EVENTHANDLER_REGISTER(mountroot,
  418             firmware_mountroot, ctx, 0);
  419 }
  420 SYSINIT(mountroot_evh_init, SI_SUB_CONFIGURE, SI_ORDER_ANY,
  421     mountroot_evh_init, NULL);
  422 
  423 /*
  424  * The body of the task in charge of unloading autoloaded modules
  425  * that are not needed anymore.
  426  * Images can be cross-linked so we may need to make multiple passes,
  427  * but the time we spend in the loop is bounded because we clear entries
  428  * as we touch them.
  429  */
  430 static void
  431 unloadentry(void *unused1, int unused2)
  432 {
  433         int limit = FIRMWARE_MAX;
  434         int i;  /* current cycle */
  435 
  436         mtx_lock(&firmware_mtx);
  437         /*
  438          * Scan the table. limit is set to make sure we make another
  439          * full sweep after matching an entry that requires unloading.
  440          */
  441         for (i = 0; i < limit; i++) {
  442                 struct priv_fw *fp;
  443                 int err;
  444 
  445                 fp = &firmware_table[i % FIRMWARE_MAX];
  446                 if (fp->fw.name == NULL || fp->file == NULL ||
  447                     fp->refcnt != 0 || (fp->flags & FW_UNLOAD) == 0)
  448                         continue;
  449 
  450                 /*
  451                  * Found an entry. Now:
  452                  * 1. bump up limit to make sure we make another full round;
  453                  * 2. clear FW_UNLOAD so we don't try this entry again.
  454                  * 3. release the lock while trying to unload the module.
  455                  * 'file' remains set so that the entry cannot be reused
  456                  * in the meantime (it also means that fp->file will
  457                  * not change while we release the lock).
  458                  */
  459                 limit = i + FIRMWARE_MAX;       /* make another full round */
  460                 fp->flags &= ~FW_UNLOAD;        /* do not try again */
  461 
  462                 mtx_unlock(&firmware_mtx);
  463                 err = linker_release_module(NULL, NULL, fp->file);
  464                 mtx_lock(&firmware_mtx);
  465 
  466                 /*
  467                  * We rely on the module to call firmware_unregister()
  468                  * on unload to actually release the entry.
  469                  * If err = 0 we can drop our reference as the system
  470                  * accepted it. Otherwise unloading failed (e.g. the
  471                  * module itself gave an error) so our reference is
  472                  * still valid.
  473                  */
  474                 if (err == 0)
  475                         fp->file = NULL; 
  476         }
  477         mtx_unlock(&firmware_mtx);
  478 }
  479 
  480 /*
  481  * Module glue.
  482  */
  483 static int
  484 firmware_modevent(module_t mod, int type, void *unused)
  485 {
  486         struct priv_fw *fp;
  487         int i, err;
  488 
  489         switch (type) {
  490         case MOD_LOAD:
  491                 TASK_INIT(&firmware_unload_task, 0, unloadentry, NULL);
  492                 firmware_tq = taskqueue_create("taskqueue_firmware", M_WAITOK,
  493                     taskqueue_thread_enqueue, &firmware_tq, NULL);
  494                 /* NB: use our own loop routine that sets up context */
  495                 (void) taskqueue_start_threads(&firmware_tq, 1, PWAIT,
  496                     "firmware taskq");
  497                 if (rootvnode != NULL) {
  498                         /* 
  499                          * Root is already mounted so we won't get an event;
  500                          * simulate one here.
  501                          */
  502                         firmware_mountroot(NULL);
  503                 }
  504                 return 0;
  505 
  506         case MOD_UNLOAD:
  507                 /* request all autoloaded modules to be released */
  508                 mtx_lock(&firmware_mtx);
  509                 for (i = 0; i < FIRMWARE_MAX; i++) {
  510                         fp = &firmware_table[i];
  511                         fp->flags |= FW_UNLOAD;;
  512                 }
  513                 mtx_unlock(&firmware_mtx);
  514                 taskqueue_enqueue(firmware_tq, &firmware_unload_task);
  515                 taskqueue_drain(firmware_tq, &firmware_unload_task);
  516                 err = 0;
  517                 for (i = 0; i < FIRMWARE_MAX; i++) {
  518                         fp = &firmware_table[i];
  519                         if (fp->fw.name != NULL) {
  520                                 printf("%s: image %p ref %d still active slot %d\n",
  521                                         __func__, fp->fw.name,
  522                                         fp->refcnt,  i);
  523                                 err = EINVAL;
  524                         }
  525                 }
  526                 if (err == 0)
  527                         taskqueue_free(firmware_tq);
  528                 return err;
  529         }
  530         return EINVAL;
  531 }
  532 
  533 static moduledata_t firmware_mod = {
  534         "firmware",
  535         firmware_modevent,
  536         0
  537 };
  538 DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
  539 MODULE_VERSION(firmware, 1);

Cache object: 5a1c55924af6e860c1a7a73baa227ebb


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