123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- #include <types.h>
- #include <kern/unistd.h>
- #include <stdarg.h>
- #include <lib.h>
- #include <spl.h>
- #include <thread.h>
- #include <current.h>
- #include <synch.h>
- #include <mainbus.h>
- #include <vfs.h> // for vfs_sync()
- uint32_t dbflags = 0;
- static struct lock *kprintf_lock;
- static struct spinlock kprintf_spinlock;
- void
- kprintf_bootstrap(void)
- {
- KASSERT(kprintf_lock == NULL);
- kprintf_lock = lock_create("kprintf_lock");
- if (kprintf_lock == NULL) {
- panic("Could not create kprintf_lock\n");
- }
- spinlock_init(&kprintf_spinlock);
- }
- static
- void
- console_send(void *junk, const char *data, size_t len)
- {
- size_t i;
- (void)junk;
- for (i=0; i<len; i++) {
- putch(data[i]);
- }
- }
- int
- kprintf(const char *fmt, ...)
- {
- int chars;
- va_list ap;
- bool dolock;
- dolock = kprintf_lock != NULL
- && curthread->t_in_interrupt == false
- && curthread->t_iplhigh_count == 0;
- if (dolock) {
- lock_acquire(kprintf_lock);
- }
- else {
- spinlock_acquire(&kprintf_spinlock);
- }
- putch_prepare();
- va_start(ap, fmt);
- chars = __vprintf(console_send, NULL, fmt, ap);
- va_end(ap);
- putch_complete();
- if (dolock) {
- lock_release(kprintf_lock);
- }
- else {
- spinlock_release(&kprintf_spinlock);
- }
- return chars;
- }
- void
- panic(const char *fmt, ...)
- {
- va_list ap;
-
- static volatile int evil;
- if (evil == 0) {
- evil = 1;
-
- splhigh();
- }
- if (evil == 1) {
- evil = 2;
-
- thread_panic();
- }
- if (evil == 2) {
- evil = 3;
-
- kprintf("panic: ");
- putch_prepare();
- va_start(ap, fmt);
- __vprintf(console_send, NULL, fmt, ap);
- va_end(ap);
- putch_complete();
- }
- if (evil == 3) {
- evil = 4;
-
- vfs_sync();
- }
- if (evil == 4) {
- evil = 5;
-
- mainbus_panic();
- }
-
- for (;;);
- }
- void
- badassert(const char *expr, const char *file, int line, const char *func)
- {
- panic("Assertion failed: %s, at %s:%d (%s)\n",
- expr, file, line, func);
- }
|