os161-1.99
 All Data Structures
kprintf.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 #include <types.h>
00031 #include <kern/unistd.h>
00032 #include <stdarg.h>
00033 #include <lib.h>
00034 #include <spl.h>
00035 #include <thread.h>
00036 #include <current.h>
00037 #include <synch.h>
00038 #include <mainbus.h>
00039 #include <vfs.h>          // for vfs_sync()
00040 
00041 
00042 /* Flags word for DEBUG() macro. */
00043 uint32_t dbflags = 0;
00044 
00045 /* Lock for non-polled kprintfs */
00046 static struct lock *kprintf_lock;
00047 
00048 /* Lock for polled kprintfs */
00049 static struct spinlock kprintf_spinlock;
00050 
00051 
00052 /*
00053  * Warning: all this has to work from interrupt handlers and when
00054  * interrupts are disabled.
00055  */
00056 
00057 
00058 /*
00059  * Create the kprintf lock. Must be called before creating a second
00060  * thread or enabling a second CPU.
00061  */
00062 void
00063 kprintf_bootstrap(void)
00064 {
00065         KASSERT(kprintf_lock == NULL);
00066 
00067         kprintf_lock = lock_create("kprintf_lock");
00068         if (kprintf_lock == NULL) {
00069                 panic("Could not create kprintf_lock\n");
00070         }
00071         spinlock_init(&kprintf_spinlock);
00072 }
00073 
00074 /*
00075  * Send characters to the console. Backend for __printf.
00076  */
00077 static
00078 void
00079 console_send(void *junk, const char *data, size_t len)
00080 {
00081         size_t i;
00082 
00083         (void)junk;
00084 
00085         for (i=0; i<len; i++) {
00086                 putch(data[i]);
00087         }
00088 }
00089 
00090 /*
00091  * Printf to the console.
00092  */
00093 int
00094 kprintf(const char *fmt, ...)
00095 {
00096         int chars;
00097         va_list ap;
00098         bool dolock;
00099 
00100         dolock = kprintf_lock != NULL
00101                 && curthread->t_in_interrupt == false
00102                 && curthread->t_iplhigh_count == 0;
00103 
00104         if (dolock) {
00105                 lock_acquire(kprintf_lock);
00106         }
00107         else {
00108                 spinlock_acquire(&kprintf_spinlock);
00109         }
00110         putch_prepare();
00111 
00112         va_start(ap, fmt);
00113         chars = __vprintf(console_send, NULL, fmt, ap);
00114         va_end(ap);
00115 
00116         putch_complete();
00117         if (dolock) {
00118                 lock_release(kprintf_lock);
00119         }
00120         else {
00121                 spinlock_release(&kprintf_spinlock);
00122         }
00123 
00124         return chars;
00125 }
00126 
00127 /*
00128  * panic() is for fatal errors. It prints the printf arguments it's
00129  * passed and then halts the system.
00130  */
00131 
00132 void
00133 panic(const char *fmt, ...)
00134 {
00135         va_list ap;
00136 
00137         /*
00138          * When we reach panic, the system is usually fairly screwed up.
00139          * It's not entirely uncommon for anything else we try to do 
00140          * here to trigger more panics.
00141          *
00142          * This variable makes sure that if we try to do something here,
00143          * and it causes another panic, *that* panic doesn't try again;
00144          * trying again almost inevitably causes infinite recursion.
00145          *
00146          * This is not excessively paranoid - these things DO happen!
00147          */
00148         static volatile int evil;
00149 
00150         if (evil == 0) {
00151                 evil = 1;
00152 
00153                 /*
00154                  * Not only do we not want to be interrupted while
00155                  * panicking, but we also want the console to be
00156                  * printing in polling mode so as not to do context
00157                  * switches. So turn interrupts off on this CPU.
00158                  */
00159                 splhigh();
00160         }
00161 
00162         if (evil == 1) {
00163                 evil = 2;
00164 
00165                 /* Kill off other threads and halt other CPUs. */
00166                 thread_panic();
00167         }
00168 
00169         if (evil == 2) {
00170                 evil = 3;
00171 
00172                 /* Print the message. */
00173                 kprintf("panic: ");
00174                 putch_prepare();
00175                 va_start(ap, fmt);
00176                 __vprintf(console_send, NULL, fmt, ap);
00177                 va_end(ap);
00178                 putch_complete();
00179         }
00180 
00181         if (evil == 3) {
00182                 evil = 4;
00183 
00184                 /* Try to sync the disks. */
00185                 vfs_sync();
00186         }
00187 
00188         if (evil == 4) {
00189                 evil = 5;
00190 
00191                 /* Shut down or reboot the system. */
00192                 mainbus_panic();
00193         }
00194 
00195         /*
00196          * Last resort, just in case.
00197          */
00198 
00199         for (;;);
00200 }
00201 
00202 /*
00203  * Assertion failures go through this.
00204  */
00205 void
00206 badassert(const char *expr, const char *file, int line, const char *func)
00207 {
00208         panic("Assertion failed: %s, at %s:%d (%s)\n",
00209               expr, file, line, func);
00210 }
 All Data Structures