os161-1.99
 All Data Structures
console.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * Machine (and hardware) independent console driver.
00032  *
00033  * We expose a simple interface to the rest of the kernel: "putch" to
00034  * print a character, "getch" to read one.
00035  *
00036  * As long as the device we're connected to does, we allow printing in
00037  * an interrupt handler or with interrupts off (by polling),
00038  * transparently to the caller. Note that getch by polling is not
00039  * supported, although such support could be added without undue
00040  * difficulty.
00041  *
00042  * Note that nothing happens until we have a device to write to. A
00043  * buffer of size DELAYBUFSIZE is used to hold output that is
00044  * generated before this point. This means that (1) using kprintf for
00045  * debugging problems that occur early in initialization is awkward,
00046  * and (2) if the system crashes before we find a console, no output
00047  * at all may appear.
00048  *
00049  * Note that we have no input buffering; characters typed too rapidly
00050  * will be lost.
00051  */
00052 
00053 #include <types.h>
00054 #include <kern/errno.h>
00055 #include <lib.h>
00056 #include <uio.h>
00057 #include <thread.h>
00058 #include <current.h>
00059 #include <synch.h>
00060 #include <generic/console.h>
00061 #include <vfs.h>
00062 #include <device.h>
00063 #include "autoconf.h"
00064 
00065 /*
00066  * The console device.
00067  */
00068 static struct con_softc *the_console = NULL;
00069 
00070 /*
00071  * Lock so user I/Os are atomic.
00072  * We use two locks so readers waiting for input don't lock out writers.
00073  */
00074 static struct lock *con_userlock_read = NULL;
00075 static struct lock *con_userlock_write = NULL;
00076 
00077 //////////////////////////////////////////////////
00078 
00079 /*
00080  * This is for accumulating characters printed before the
00081  * console is set up. Upon console setup they are dumped 
00082  * to the actual console; thenceforth this space is unused.
00083  */
00084 #define DELAYBUFSIZE  1024
00085 static char delayed_outbuf[DELAYBUFSIZE];
00086 static size_t delayed_outbuf_pos=0;
00087 
00088 static
00089 void
00090 putch_delayed(int ch)
00091 {
00092         /*
00093          * No synchronization needed: called only during system startup
00094          * by main thread.
00095          */
00096 
00097         KASSERT(delayed_outbuf_pos < sizeof(delayed_outbuf));
00098         delayed_outbuf[delayed_outbuf_pos++] = ch;
00099 }
00100 
00101 static
00102 void
00103 flush_delay_buf(void)
00104 {
00105         size_t i;
00106         for (i=0; i<delayed_outbuf_pos; i++) {
00107                 putch(delayed_outbuf[i]);
00108         }
00109         delayed_outbuf_pos = 0;
00110 }
00111 
00112 //////////////////////////////////////////////////
00113 
00114 /*
00115  * Print a character, using polling instead of interrupts to wait for
00116  * I/O completion.
00117  */
00118 static
00119 void
00120 putch_polled(struct con_softc *cs, int ch)
00121 {
00122         cs->cs_sendpolled(cs->cs_devdata, ch);
00123 }
00124 
00125 static
00126 void
00127 putch_prepare_polled(struct con_softc *cs)
00128 {
00129         if (cs->cs_startpolling != NULL) {
00130                 cs->cs_startpolling(cs->cs_devdata);
00131         }
00132 }
00133 
00134 static
00135 void
00136 putch_complete_polled(struct con_softc *cs)
00137 {
00138         if (cs->cs_endpolling != NULL) {
00139                 cs->cs_endpolling(cs->cs_devdata);
00140         }
00141 }
00142 
00143 //////////////////////////////////////////////////
00144 
00145 /*
00146  * Print a character, using interrupts to wait for I/O completion.
00147  */
00148 static
00149 void
00150 putch_intr(struct con_softc *cs, int ch)
00151 {
00152         P(cs->cs_wsem);
00153         cs->cs_send(cs->cs_devdata, ch);
00154 }
00155 
00156 /*
00157  * Read a character, using interrupts to wait for I/O completion.
00158  */
00159 static
00160 int
00161 getch_intr(struct con_softc *cs)
00162 {
00163         unsigned char ret;
00164 
00165         P(cs->cs_rsem);
00166         ret = cs->cs_gotchars[cs->cs_gotchars_tail];
00167         cs->cs_gotchars_tail =
00168                 (cs->cs_gotchars_tail + 1) % CONSOLE_INPUT_BUFFER_SIZE;
00169         return ret;
00170 }
00171 
00172 /*
00173  * Called from underlying device when a read-ready interrupt occurs.
00174  *
00175  * Note: if gotchars_head == gotchars_tail, the buffer is empty. Thus
00176  * if gotchars_head+1 == gotchars_tail, the buffer is full. A slightly
00177  * tidier way to implement this check (that avoids wasting a slot,
00178  * too) would be with a second semaphore used with a nonblocking P,
00179  * but we don't have that in OS/161.
00180  */
00181 void
00182 con_input(void *vcs, int ch)
00183 {
00184         struct con_softc *cs = vcs;
00185         unsigned nexthead;
00186 
00187         nexthead = (cs->cs_gotchars_head + 1) % CONSOLE_INPUT_BUFFER_SIZE;
00188         if (nexthead == cs->cs_gotchars_tail) {
00189                 /* overflow; drop character */
00190                 return;
00191         }
00192 
00193         cs->cs_gotchars[cs->cs_gotchars_head] = ch;
00194         cs->cs_gotchars_head = nexthead;
00195                 
00196         V(cs->cs_rsem);
00197 }
00198 
00199 /*
00200  * Called from underlying device when a write-done interrupt occurs.
00201  */
00202 void
00203 con_start(void *vcs)
00204 {
00205         struct con_softc *cs = vcs;
00206 
00207         V(cs->cs_wsem);
00208 }
00209 
00210 //////////////////////////////////////////////////
00211 
00212 /*
00213  * Exported interface.
00214  * 
00215  * Warning: putch must work even in an interrupt handler or with
00216  * interrupts disabled, and before the console is probed. getch need
00217  * not, and does not.
00218  */
00219 
00220 void
00221 putch(int ch)
00222 {
00223         struct con_softc *cs = the_console;
00224 
00225         if (cs==NULL) {
00226                 putch_delayed(ch);
00227         }
00228         else if (curthread->t_in_interrupt || curthread->t_iplhigh_count > 0) {
00229                 putch_polled(cs, ch);
00230         }
00231         else {
00232                 putch_intr(cs, ch);
00233         }
00234 }
00235 
00236 void
00237 putch_prepare(void)
00238 {
00239         struct con_softc *cs = the_console;
00240 
00241         if (cs == NULL) {
00242                 /* nothing */
00243         }
00244         else if (curthread->t_in_interrupt || curthread->t_iplhigh_count > 0) {
00245                 putch_prepare_polled(cs);
00246         }
00247         else {
00248                 /* nothing */
00249         }
00250 }
00251 
00252 void
00253 putch_complete(void)
00254 {
00255         struct con_softc *cs = the_console;
00256 
00257         if (cs == NULL) {
00258                 /* nothing */
00259         }
00260         else if (curthread->t_in_interrupt || curthread->t_iplhigh_count > 0) {
00261                 putch_complete_polled(cs);
00262         }
00263         else {
00264                 /* nothing */
00265         }
00266 }
00267 
00268 int
00269 getch(void)
00270 {
00271         struct con_softc *cs = the_console;
00272         KASSERT(cs != NULL);
00273         KASSERT(!curthread->t_in_interrupt && curthread->t_iplhigh_count == 0);
00274 
00275         return getch_intr(cs);
00276 }
00277 
00278 ////////////////////////////////////////////////////////////
00279 
00280 /*
00281  * VFS interface functions
00282  */
00283 
00284 static
00285 int
00286 con_open(struct device *dev, int openflags)
00287 {
00288         (void)dev;
00289         (void)openflags;
00290         return 0;
00291 }
00292 
00293 static
00294 int
00295 con_close(struct device *dev)
00296 {
00297         (void)dev;
00298         return 0;
00299 }
00300 
00301 static
00302 int
00303 con_io(struct device *dev, struct uio *uio)
00304 {
00305         int result;
00306         char ch;
00307         struct lock *lk;
00308 
00309         (void)dev;  // unused
00310 
00311         if (uio->uio_rw==UIO_READ) {
00312                 lk = con_userlock_read;
00313         }
00314         else {
00315                 lk = con_userlock_write;
00316         }
00317 
00318         KASSERT(lk != NULL);
00319         lock_acquire(lk);
00320 
00321         while (uio->uio_resid > 0) {
00322                 if (uio->uio_rw==UIO_READ) {
00323                         ch = getch();
00324                         if (ch=='\r') {
00325                                 ch = '\n';
00326                         }
00327                         result = uiomove(&ch, 1, uio);
00328                         if (result) {
00329                                 lock_release(lk);
00330                                 return result;
00331                         }
00332                         if (ch=='\n') {
00333                                 break;
00334                         }
00335                 }
00336                 else {
00337                         result = uiomove(&ch, 1, uio);
00338                         if (result) {
00339                                 lock_release(lk);
00340                                 return result;
00341                         }
00342                         if (ch=='\n') {
00343                                 putch('\r');
00344                         }
00345                         putch(ch);
00346                 }
00347         }
00348         lock_release(lk);
00349         return 0;
00350 }
00351 
00352 static
00353 int
00354 con_ioctl(struct device *dev, int op, userptr_t data)
00355 {
00356         /* No ioctls. */
00357         (void)dev;
00358         (void)op;
00359         (void)data;
00360         return EINVAL;
00361 }
00362 
00363 static
00364 int
00365 attach_console_to_vfs(struct con_softc *cs)
00366 {
00367         struct device *dev;
00368         int result;
00369 
00370         dev = kmalloc(sizeof(*dev));
00371         if (dev==NULL) {
00372                 return ENOMEM;
00373         }
00374 
00375         dev->d_open = con_open;
00376         dev->d_close = con_close;
00377         dev->d_io = con_io;
00378         dev->d_ioctl = con_ioctl;
00379         dev->d_blocks = 0;
00380         dev->d_blocksize = 1;
00381         dev->d_data = cs;
00382 
00383         result = vfs_adddev("con", dev, 0);
00384         if (result) {
00385                 kfree(dev);
00386                 return result;
00387         }
00388 
00389         return 0;
00390 }
00391 
00392 ////////////////////////////////////////////////////////////
00393 
00394 /*
00395  * Config routine called by autoconf.c after we are attached to something.
00396  */
00397 
00398 int
00399 config_con(struct con_softc *cs, int unit)
00400 {
00401         struct semaphore *rsem, *wsem;
00402         struct lock *rlk, *wlk;
00403 
00404         /*
00405          * Only allow one system console.
00406          * Further devices that could be the system console are ignored.
00407          *
00408          * Do not hardwire the console to be "con1" instead of "con0",
00409          * or these asserts will go off.
00410          */
00411         if (unit>0) {
00412                 KASSERT(the_console!=NULL);
00413                 return ENODEV;
00414         }
00415         KASSERT(the_console==NULL);
00416 
00417         rsem = sem_create("console read", 0);
00418         if (rsem == NULL) {
00419                 return ENOMEM;
00420         }
00421         wsem = sem_create("console write", 1);
00422         if (wsem == NULL) {
00423                 sem_destroy(rsem);
00424                 return ENOMEM;
00425         }
00426         rlk = lock_create("console-lock-read");
00427         if (rlk == NULL) {
00428                 sem_destroy(rsem);
00429                 sem_destroy(wsem);
00430                 return ENOMEM;
00431         }
00432         wlk = lock_create("console-lock-write");
00433         if (wlk == NULL) {
00434                 lock_destroy(rlk);
00435                 sem_destroy(rsem);
00436                 sem_destroy(wsem);
00437                 return ENOMEM;
00438         }
00439 
00440         cs->cs_rsem = rsem; 
00441         cs->cs_wsem = wsem; 
00442         cs->cs_gotchars_head = 0;
00443         cs->cs_gotchars_tail = 0;
00444 
00445         the_console = cs;
00446         con_userlock_read = rlk;
00447         con_userlock_write = wlk;
00448 
00449         flush_delay_buf();
00450 
00451         return attach_console_to_vfs(cs);
00452 }
 All Data Structures