/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <types.h>30#include <kern/unistd.h>31#include <stdarg.h>32#include <lib.h>33#include <spl.h>34#include <thread.h>35#include <current.h>36#include <synch.h>37#include <mainbus.h>38#include <vfs.h> // for vfs_sync()394041/* Flags word for DEBUG() macro. */42uint32_t dbflags = 0;4344/* Lock for non-polled kprintfs */45static struct lock *kprintf_lock;4647/* Lock for polled kprintfs */48static struct spinlock kprintf_spinlock;495051/*52* Warning: all this has to work from interrupt handlers and when53* interrupts are disabled.54*/555657/*58* Create the kprintf lock. Must be called before creating a second59* thread or enabling a second CPU.60*/61void62kprintf_bootstrap(void)63{64KASSERT(kprintf_lock == NULL);6566kprintf_lock = lock_create("kprintf_lock");67if (kprintf_lock == NULL) {68panic("Could not create kprintf_lock\n");69}70spinlock_init(&kprintf_spinlock);71}7273/*74* Send characters to the console. Backend for __printf.75*/76static77void78console_send(void *junk, const char *data, size_t len)79{80size_t i;8182(void)junk;8384for (i=0; i<len; i++) {85putch(data[i]);86}87}8889/*90* Printf to the console.91*/92int93kprintf(const char *fmt, ...)94{95int chars;96va_list ap;97bool dolock;9899dolock = kprintf_lock != NULL100&& curthread->t_in_interrupt == false101&& curthread->t_iplhigh_count == 0;102103if (dolock) {104lock_acquire(kprintf_lock);105}106else {107spinlock_acquire(&kprintf_spinlock);108}109putch_prepare();110111va_start(ap, fmt);112chars = __vprintf(console_send, NULL, fmt, ap);113va_end(ap);114115putch_complete();116if (dolock) {117lock_release(kprintf_lock);118}119else {120spinlock_release(&kprintf_spinlock);121}122123return chars;124}125126/*127* panic() is for fatal errors. It prints the printf arguments it's128* passed and then halts the system.129*/130131void132panic(const char *fmt, ...)133{134va_list ap;135136/*137* When we reach panic, the system is usually fairly screwed up.138* It's not entirely uncommon for anything else we try to do139* here to trigger more panics.140*141* This variable makes sure that if we try to do something here,142* and it causes another panic, *that* panic doesn't try again;143* trying again almost inevitably causes infinite recursion.144*145* This is not excessively paranoid - these things DO happen!146*/147static volatile int evil;148149if (evil == 0) {150evil = 1;151152/*153* Not only do we not want to be interrupted while154* panicking, but we also want the console to be155* printing in polling mode so as not to do context156* switches. So turn interrupts off on this CPU.157*/158splhigh();159}160161if (evil == 1) {162evil = 2;163164/* Kill off other threads and halt other CPUs. */165thread_panic();166}167168if (evil == 2) {169evil = 3;170171/* Print the message. */172kprintf("panic: ");173putch_prepare();174va_start(ap, fmt);175__vprintf(console_send, NULL, fmt, ap);176va_end(ap);177putch_complete();178}179180if (evil == 3) {181evil = 4;182183/* Try to sync the disks. */184vfs_sync();185}186187if (evil == 4) {188evil = 5;189190/* Shut down or reboot the system. */191mainbus_panic();192}193194/*195* Last resort, just in case.196*/197198for (;;);199}200201/*202* Assertion failures go through this.203*/204void205badassert(const char *expr, const char *file, int line, const char *func)206{207panic("Assertion failed: %s, at %s:%d (%s)\n",208expr, file, line, func);209}210211212