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/dev/dcons/dcons_os.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,2004
    3  *      Hidetoshi Shimokawa. 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  *
   16  *      This product includes software developed by Hidetoshi Shimokawa.
   17  *
   18  * 4. Neither the name of the author nor the names of its contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32  * SUCH DAMAGE.
   33  *
   34  * $FreeBSD$
   35  */
   36 
   37 #include <sys/param.h>
   38 #include <sys/kdb.h>
   39 #include <gdb/gdb.h>
   40 #include <sys/kernel.h>
   41 #include <sys/module.h>
   42 #include <sys/systm.h>
   43 #include <sys/types.h>
   44 #include <sys/conf.h>
   45 #include <sys/cons.h>
   46 #include <sys/consio.h>
   47 #include <sys/tty.h>
   48 #include <sys/malloc.h>
   49 #include <sys/priv.h>
   50 #include <sys/proc.h>
   51 #include <sys/ucred.h>
   52 
   53 #include <machine/atomic.h>
   54 #include <machine/bus.h>
   55 
   56 #include <dev/dcons/dcons.h>
   57 #include <dev/dcons/dcons_os.h>
   58 
   59 #include <ddb/ddb.h>
   60 #include <sys/reboot.h>
   61 
   62 #include <sys/sysctl.h>
   63 
   64 #include <vm/vm.h>
   65 #include <vm/vm_param.h>
   66 #include <vm/pmap.h>
   67 
   68 #include "opt_dcons.h"
   69 #include "opt_kdb.h"
   70 #include "opt_gdb.h"
   71 #include "opt_ddb.h"
   72 
   73 
   74 #ifndef DCONS_POLL_HZ
   75 #define DCONS_POLL_HZ   25
   76 #endif
   77 
   78 #ifndef DCONS_POLL_IDLE
   79 #define DCONS_POLL_IDLE 256
   80 #endif
   81 
   82 #ifndef DCONS_BUF_SIZE
   83 #define DCONS_BUF_SIZE (16*1024)
   84 #endif
   85 
   86 #ifndef DCONS_FORCE_CONSOLE
   87 #define DCONS_FORCE_CONSOLE     0       /* Mostly for FreeBSD-4/DragonFly */
   88 #endif
   89 
   90 #ifndef KLD_MODULE
   91 static char bssbuf[DCONS_BUF_SIZE];     /* buf in bss */
   92 #endif
   93 
   94 /* global data */
   95 static struct dcons_global dg;
   96 struct dcons_global *dcons_conf;
   97 static int poll_hz = DCONS_POLL_HZ;
   98 static u_int poll_idle = DCONS_POLL_HZ * DCONS_POLL_IDLE;
   99 
  100 static struct dcons_softc sc[DCONS_NPORT];
  101 
  102 static SYSCTL_NODE(_kern, OID_AUTO, dcons, CTLFLAG_RD, 0, "Dumb Console");
  103 SYSCTL_INT(_kern_dcons, OID_AUTO, poll_hz, CTLFLAG_RW, &poll_hz, 0,
  104                                 "dcons polling rate");
  105 
  106 static int drv_init = 0;
  107 static struct callout dcons_callout;
  108 struct dcons_buf *dcons_buf;            /* for local dconschat */
  109 
  110 static void     dcons_timeout(void *);
  111 static int      dcons_drv_init(int);
  112 
  113 static cn_probe_t       dcons_cnprobe;
  114 static cn_init_t        dcons_cninit;
  115 static cn_term_t        dcons_cnterm;
  116 static cn_getc_t        dcons_cngetc;
  117 static cn_putc_t        dcons_cnputc;
  118 static cn_grab_t        dcons_cngrab;
  119 static cn_ungrab_t      dcons_cnungrab;
  120 
  121 CONSOLE_DRIVER(dcons);
  122 
  123 #if defined(GDB)
  124 static gdb_probe_f      dcons_dbg_probe;
  125 static gdb_init_f       dcons_dbg_init;
  126 static gdb_term_f       dcons_dbg_term;
  127 static gdb_getc_f       dcons_dbg_getc;
  128 static gdb_putc_f       dcons_dbg_putc;
  129 
  130 GDB_DBGPORT(dcons, dcons_dbg_probe, dcons_dbg_init, dcons_dbg_term,
  131     dcons_dbg_getc, dcons_dbg_putc);
  132 
  133 extern struct gdb_dbgport *gdb_cur;
  134 #endif
  135 
  136 static tsw_outwakeup_t dcons_outwakeup;
  137 static tsw_free_t      dcons_free;
  138 
  139 static struct ttydevsw dcons_ttydevsw = {
  140         .tsw_flags      = TF_NOPREFIX,
  141         .tsw_outwakeup  = dcons_outwakeup,
  142         .tsw_free       = dcons_free,
  143 };
  144 
  145 static int dcons_close_refs;
  146 
  147 #if (defined(GDB) || defined(DDB))
  148 static int
  149 dcons_check_break(struct dcons_softc *dc, int c)
  150 {
  151 
  152         if (c < 0)
  153                 return (c);
  154 
  155 #ifdef GDB
  156         if ((dc->flags & DC_GDB) != 0 && gdb_cur == &dcons_gdb_dbgport)
  157                 kdb_alt_break_gdb(c, &dc->brk_state);
  158         else
  159 #endif
  160                 kdb_alt_break(c, &dc->brk_state);
  161 
  162         return (c);
  163 }
  164 #else
  165 #define dcons_check_break(dc, c)        (c)
  166 #endif
  167 
  168 static int
  169 dcons_os_checkc_nopoll(struct dcons_softc *dc)
  170 {
  171         int c;
  172 
  173         if (dg.dma_tag != NULL)
  174                 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTREAD);
  175 
  176         c = dcons_check_break(dc, dcons_checkc(dc));
  177 
  178         if (dg.dma_tag != NULL)
  179                 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREREAD);
  180 
  181         return (c);
  182 }
  183 
  184 static int
  185 dcons_os_checkc(struct dcons_softc *dc)
  186 {
  187         EVENTHANDLER_INVOKE(dcons_poll, 0);
  188         return (dcons_os_checkc_nopoll(dc));
  189 }
  190 
  191 static void
  192 dcons_os_putc(struct dcons_softc *dc, int c)
  193 {
  194         if (dg.dma_tag != NULL)
  195                 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_POSTWRITE);
  196 
  197         dcons_putc(dc, c);
  198 
  199         if (dg.dma_tag != NULL)
  200                 bus_dmamap_sync(dg.dma_tag, dg.dma_map, BUS_DMASYNC_PREWRITE);
  201 }
  202 
  203 static void
  204 dcons_free(void *xsc __unused)
  205 {
  206 
  207         /* Our deferred free has arrived, now we're waiting for one fewer. */
  208         atomic_subtract_rel_int(&dcons_close_refs, 1);
  209 }
  210 
  211 static void
  212 dcons_outwakeup(struct tty *tp)
  213 {
  214         struct dcons_softc *dc;
  215         char ch;
  216 
  217         dc = tty_softc(tp);
  218 
  219         while (ttydisc_getc(tp, &ch, sizeof ch) != 0)
  220                 dcons_os_putc(dc, ch);
  221 }
  222 
  223 static void
  224 dcons_timeout(void *v)
  225 {
  226         struct  tty *tp;
  227         struct dcons_softc *dc;
  228         int i, c, polltime;
  229 
  230         for (i = 0; i < DCONS_NPORT; i ++) {
  231                 dc = &sc[i];
  232                 tp = dc->tty;
  233 
  234                 tty_lock(tp);
  235                 while ((c = dcons_os_checkc_nopoll(dc)) != -1) {
  236                         ttydisc_rint(tp, c, 0);
  237                         poll_idle = 0;
  238                 }
  239                 ttydisc_rint_done(tp);
  240                 tty_unlock(tp);
  241         }
  242         poll_idle++;
  243         polltime = hz;
  244         if (poll_idle <= (poll_hz * DCONS_POLL_IDLE))
  245                 polltime /= poll_hz;
  246         callout_reset(&dcons_callout, polltime, dcons_timeout, tp);
  247 }
  248 
  249 static void
  250 dcons_cnprobe(struct consdev *cp)
  251 {
  252         sprintf(cp->cn_name, "dcons");
  253 #if DCONS_FORCE_CONSOLE
  254         cp->cn_pri = CN_REMOTE;
  255 #else
  256         cp->cn_pri = CN_NORMAL;
  257 #endif
  258 }
  259 
  260 static void
  261 dcons_cninit(struct consdev *cp)
  262 {
  263         dcons_drv_init(0);
  264         cp->cn_arg = (void *)&sc[DCONS_CON]; /* share port0 with unit0 */
  265 }
  266 
  267 static void
  268 dcons_cnterm(struct consdev *cp)
  269 {
  270 }
  271 
  272 static void
  273 dcons_cngrab(struct consdev *cp)
  274 {
  275 }
  276 
  277 static void
  278 dcons_cnungrab(struct consdev *cp)
  279 {
  280 }
  281 
  282 static int
  283 dcons_cngetc(struct consdev *cp)
  284 {
  285         struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
  286         return (dcons_os_checkc(dc));
  287 }
  288 
  289 static void
  290 dcons_cnputc(struct consdev *cp, int c)
  291 {
  292         struct dcons_softc *dc = (struct dcons_softc *)cp->cn_arg;
  293         dcons_os_putc(dc, c);
  294 }
  295 
  296 static int
  297 dcons_drv_init(int stage)
  298 {
  299 #if defined(__i386__) || defined(__amd64__)
  300         quad_t addr, size;
  301 #endif
  302 
  303         if (drv_init)
  304                 return(drv_init);
  305 
  306         drv_init = -1;
  307 
  308         bzero(&dg, sizeof(dg));
  309         dcons_conf = &dg;
  310         dg.cdev = &dcons_consdev;
  311         dg.buf = NULL;
  312         dg.size = DCONS_BUF_SIZE;
  313 
  314 #if defined(__i386__) || defined(__amd64__)
  315         if (getenv_quad("dcons.addr", &addr) > 0 &&
  316             getenv_quad("dcons.size", &size) > 0) {
  317 #ifdef __i386__
  318                 vm_paddr_t pa;
  319                 /*
  320                  * Allow read/write access to dcons buffer.
  321                  */
  322                 for (pa = trunc_page(addr); pa < addr + size; pa += PAGE_SIZE)
  323                         *vtopte(KERNBASE + pa) |= PG_RW;
  324                 invltlb();
  325 #endif
  326                 /* XXX P to V */
  327                 dg.buf = (struct dcons_buf *)(vm_offset_t)(KERNBASE + addr);
  328                 dg.size = size;
  329                 if (dcons_load_buffer(dg.buf, dg.size, sc) < 0)
  330                         dg.buf = NULL;
  331         }
  332 #endif
  333         if (dg.buf != NULL)
  334                 goto ok;
  335 
  336 #ifndef KLD_MODULE
  337         if (stage == 0) { /* XXX or cold */
  338                 /*
  339                  * DCONS_FORCE_CONSOLE == 1 and statically linked.
  340                  * called from cninit(). can't use contigmalloc yet .
  341                  */
  342                 dg.buf = (struct dcons_buf *) bssbuf;
  343                 dcons_init(dg.buf, dg.size, sc);
  344         } else
  345 #endif
  346         {
  347                 /*
  348                  * DCONS_FORCE_CONSOLE == 0 or kernel module case.
  349                  * if the module is loaded after boot,
  350                  * bssbuf could be non-continuous.
  351                  */ 
  352                 dg.buf = (struct dcons_buf *) contigmalloc(dg.size,
  353                         M_DEVBUF, 0, 0x10000, 0xffffffff, PAGE_SIZE, 0ul);
  354                 if (dg.buf == NULL)
  355                         return (-1);
  356                 dcons_init(dg.buf, dg.size, sc);
  357         }
  358 
  359 ok:
  360         dcons_buf = dg.buf;
  361 
  362         drv_init = 1;
  363 
  364         return 0;
  365 }
  366 
  367 
  368 static int
  369 dcons_attach_port(int port, char *name, int flags)
  370 {
  371         struct dcons_softc *dc;
  372         struct tty *tp;
  373 
  374         dc = &sc[port];
  375         tp = tty_alloc(&dcons_ttydevsw, dc);
  376         dc->flags = flags;
  377         dc->tty   = tp;
  378         tty_init_console(tp, 0);
  379         tty_makedev(tp, NULL, "%s", name);
  380         return(0);
  381 }
  382 
  383 static int
  384 dcons_attach(void)
  385 {
  386         int polltime;
  387 
  388         dcons_attach_port(DCONS_CON, "dcons", 0);
  389         dcons_attach_port(DCONS_GDB, "dgdb", DC_GDB);
  390         callout_init(&dcons_callout, 1);
  391         polltime = hz / poll_hz;
  392         callout_reset(&dcons_callout, polltime, dcons_timeout, NULL);
  393         return(0);
  394 }
  395 
  396 static int
  397 dcons_detach(int port)
  398 {
  399         struct  tty *tp;
  400         struct dcons_softc *dc;
  401 
  402         dc = &sc[port];
  403         tp = dc->tty;
  404 
  405         /* tty_rel_gone() schedules a deferred free callback, count it. */
  406         atomic_add_int(&dcons_close_refs, 1);
  407         tty_lock(tp);
  408         tty_rel_gone(tp);
  409 
  410         return(0);
  411 }
  412 
  413 static int
  414 dcons_modevent(module_t mode, int type, void *data)
  415 {
  416         int err = 0, ret;
  417 
  418         switch (type) {
  419         case MOD_LOAD:
  420                 ret = dcons_drv_init(1);
  421                 if (ret != -1)
  422                         dcons_attach();
  423                 if (ret == 0) {
  424                         dcons_cnprobe(&dcons_consdev);
  425                         dcons_cninit(&dcons_consdev);
  426                         cnadd(&dcons_consdev);
  427                 }
  428                 break;
  429         case MOD_UNLOAD:
  430                 printf("dcons: unload\n");
  431                 if (drv_init == 1) {
  432                         callout_stop(&dcons_callout);
  433                         cnremove(&dcons_consdev);
  434                         dcons_detach(DCONS_CON);
  435                         dcons_detach(DCONS_GDB);
  436                         dg.buf->magic = 0;
  437 
  438                         contigfree(dg.buf, DCONS_BUF_SIZE, M_DEVBUF);
  439                 }
  440 
  441                 /* Wait for tty deferred free callbacks to complete. */
  442                 while (atomic_load_acq_int(&dcons_close_refs) > 0)
  443                         pause_sbt("dcunld", mstosbt(50), mstosbt(10), 0);
  444                 break;
  445         case MOD_SHUTDOWN:
  446 #if 0           /* Keep connection after halt */
  447                 dg.buf->magic = 0;
  448 #endif
  449                 break;
  450         default:
  451                 err = EOPNOTSUPP;
  452                 break;
  453         }
  454         return(err);
  455 }
  456 
  457 #if defined(GDB)
  458 /* Debugger interface */
  459 
  460 static int
  461 dcons_os_getc(struct dcons_softc *dc)
  462 {
  463         int c;
  464 
  465         while ((c = dcons_os_checkc(dc)) == -1);
  466 
  467         return (c & 0xff);
  468 }
  469 
  470 static int
  471 dcons_dbg_probe(void)
  472 {
  473         int dcons_gdb;
  474 
  475         if (getenv_int("dcons_gdb", &dcons_gdb) == 0)
  476                 return (-1);
  477         return (dcons_gdb);
  478 }
  479 
  480 static void
  481 dcons_dbg_init(void)
  482 {
  483 }
  484 
  485 static void
  486 dcons_dbg_term(void)
  487 {
  488 }
  489 
  490 static void
  491 dcons_dbg_putc(int c)
  492 {
  493         struct dcons_softc *dc = &sc[DCONS_GDB];
  494         dcons_os_putc(dc, c);
  495 }
  496 
  497 static int
  498 dcons_dbg_getc(void)
  499 {
  500         struct dcons_softc *dc = &sc[DCONS_GDB];
  501         return (dcons_os_getc(dc));
  502 }
  503 #endif
  504 
  505 DEV_MODULE(dcons, dcons_modevent, NULL);
  506 MODULE_VERSION(dcons, DCONS_VERSION);

Cache object: 042d62568b96da57176a1c5a5cf4636a


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