kprintf.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  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. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #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()
  39. /* Flags word for DEBUG() macro. */
  40. uint32_t dbflags = 0;
  41. /* Lock for non-polled kprintfs */
  42. static struct lock *kprintf_lock;
  43. /* Lock for polled kprintfs */
  44. static struct spinlock kprintf_spinlock;
  45. /*
  46. * Warning: all this has to work from interrupt handlers and when
  47. * interrupts are disabled.
  48. */
  49. /*
  50. * Create the kprintf lock. Must be called before creating a second
  51. * thread or enabling a second CPU.
  52. */
  53. void
  54. kprintf_bootstrap(void)
  55. {
  56. KASSERT(kprintf_lock == NULL);
  57. kprintf_lock = lock_create("kprintf_lock");
  58. if (kprintf_lock == NULL) {
  59. panic("Could not create kprintf_lock\n");
  60. }
  61. spinlock_init(&kprintf_spinlock);
  62. }
  63. /*
  64. * Send characters to the console. Backend for __printf.
  65. */
  66. static
  67. void
  68. console_send(void *junk, const char *data, size_t len)
  69. {
  70. size_t i;
  71. (void)junk;
  72. for (i=0; i<len; i++) {
  73. putch(data[i]);
  74. }
  75. }
  76. /*
  77. * Printf to the console.
  78. */
  79. int
  80. kprintf(const char *fmt, ...)
  81. {
  82. int chars;
  83. va_list ap;
  84. bool dolock;
  85. dolock = kprintf_lock != NULL
  86. && curthread->t_in_interrupt == false
  87. && curthread->t_iplhigh_count == 0;
  88. if (dolock) {
  89. lock_acquire(kprintf_lock);
  90. }
  91. else {
  92. spinlock_acquire(&kprintf_spinlock);
  93. }
  94. putch_prepare();
  95. va_start(ap, fmt);
  96. chars = __vprintf(console_send, NULL, fmt, ap);
  97. va_end(ap);
  98. putch_complete();
  99. if (dolock) {
  100. lock_release(kprintf_lock);
  101. }
  102. else {
  103. spinlock_release(&kprintf_spinlock);
  104. }
  105. return chars;
  106. }
  107. /*
  108. * panic() is for fatal errors. It prints the printf arguments it's
  109. * passed and then halts the system.
  110. */
  111. void
  112. panic(const char *fmt, ...)
  113. {
  114. va_list ap;
  115. /*
  116. * When we reach panic, the system is usually fairly screwed up.
  117. * It's not entirely uncommon for anything else we try to do
  118. * here to trigger more panics.
  119. *
  120. * This variable makes sure that if we try to do something here,
  121. * and it causes another panic, *that* panic doesn't try again;
  122. * trying again almost inevitably causes infinite recursion.
  123. *
  124. * This is not excessively paranoid - these things DO happen!
  125. */
  126. static volatile int evil;
  127. if (evil == 0) {
  128. evil = 1;
  129. /*
  130. * Not only do we not want to be interrupted while
  131. * panicking, but we also want the console to be
  132. * printing in polling mode so as not to do context
  133. * switches. So turn interrupts off on this CPU.
  134. */
  135. splhigh();
  136. }
  137. if (evil == 1) {
  138. evil = 2;
  139. /* Kill off other threads and halt other CPUs. */
  140. thread_panic();
  141. }
  142. if (evil == 2) {
  143. evil = 3;
  144. /* Print the message. */
  145. kprintf("panic: ");
  146. putch_prepare();
  147. va_start(ap, fmt);
  148. __vprintf(console_send, NULL, fmt, ap);
  149. va_end(ap);
  150. putch_complete();
  151. }
  152. if (evil == 3) {
  153. evil = 4;
  154. /* Try to sync the disks. */
  155. vfs_sync();
  156. }
  157. if (evil == 4) {
  158. evil = 5;
  159. /* Shut down or reboot the system. */
  160. mainbus_panic();
  161. }
  162. /*
  163. * Last resort, just in case.
  164. */
  165. for (;;);
  166. }
  167. /*
  168. * Assertion failures go through this.
  169. */
  170. void
  171. badassert(const char *expr, const char *file, int line, const char *func)
  172. {
  173. panic("Assertion failed: %s, at %s:%d (%s)\n",
  174. expr, file, line, func);
  175. }