kprintf.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/PrintfImplementation.h>
  27. #include <AK/Types.h>
  28. #include <Kernel/Console.h>
  29. #include <Kernel/IO.h>
  30. #include <Kernel/Process.h>
  31. #include <Kernel/SpinLock.h>
  32. #include <Kernel/kstdio.h>
  33. #include <LibC/stdarg.h>
  34. static bool serial_debug;
  35. // A recursive spinlock allows us to keep writing in the case where a
  36. // page fault happens in the middle of a dbg(), klog(), etc
  37. static RecursiveSpinLock s_log_lock;
  38. void set_serial_debug(bool on_or_off)
  39. {
  40. serial_debug = on_or_off;
  41. }
  42. int get_serial_debug()
  43. {
  44. return serial_debug;
  45. }
  46. static void color_on()
  47. {
  48. IO::out8(0xe9, 0x1b);
  49. IO::out8(0xe9, '[');
  50. IO::out8(0xe9, '3');
  51. IO::out8(0xe9, '6');
  52. IO::out8(0xe9, 'm');
  53. }
  54. static void color_off()
  55. {
  56. IO::out8(0xe9, 0x1b);
  57. IO::out8(0xe9, '[');
  58. IO::out8(0xe9, '0');
  59. IO::out8(0xe9, 'm');
  60. }
  61. static void serial_putch(char ch)
  62. {
  63. static bool serial_ready = false;
  64. static bool was_cr = false;
  65. if (!serial_ready) {
  66. IO::out8(0x3F8 + 1, 0x00);
  67. IO::out8(0x3F8 + 3, 0x80);
  68. IO::out8(0x3F8 + 0, 0x02);
  69. IO::out8(0x3F8 + 1, 0x00);
  70. IO::out8(0x3F8 + 3, 0x03);
  71. IO::out8(0x3F8 + 2, 0xC7);
  72. IO::out8(0x3F8 + 4, 0x0B);
  73. serial_ready = true;
  74. }
  75. while ((IO::in8(0x3F8 + 5) & 0x20) == 0)
  76. ;
  77. if (ch == '\n' && !was_cr)
  78. IO::out8(0x3F8, '\r');
  79. IO::out8(0x3F8, ch);
  80. if (ch == '\r')
  81. was_cr = true;
  82. else
  83. was_cr = false;
  84. }
  85. static void console_out(char ch)
  86. {
  87. if (serial_debug)
  88. serial_putch(ch);
  89. // It would be bad to reach the assert in Console()::the() and do a stack overflow
  90. if (Console::is_initialized()) {
  91. Console::the().put_char(ch);
  92. } else {
  93. IO::out8(0xe9, ch);
  94. }
  95. }
  96. static void console_putch(char*&, char ch)
  97. {
  98. console_out(ch);
  99. }
  100. int kprintf(const char* fmt, ...)
  101. {
  102. ScopedSpinLock lock(s_log_lock);
  103. color_on();
  104. va_list ap;
  105. va_start(ap, fmt);
  106. int ret = printf_internal(console_putch, nullptr, fmt, ap);
  107. va_end(ap);
  108. color_off();
  109. return ret;
  110. }
  111. static void buffer_putch(char*& bufptr, char ch)
  112. {
  113. *bufptr++ = ch;
  114. }
  115. int sprintf(char* buffer, const char* fmt, ...)
  116. {
  117. ScopedSpinLock lock(s_log_lock);
  118. va_list ap;
  119. va_start(ap, fmt);
  120. int ret = printf_internal(buffer_putch, buffer, fmt, ap);
  121. buffer[ret] = '\0';
  122. va_end(ap);
  123. return ret;
  124. }
  125. static void debugger_out(char ch)
  126. {
  127. if (serial_debug)
  128. serial_putch(ch);
  129. IO::out8(0xe9, ch);
  130. }
  131. static void debugger_putch(char*&, char ch)
  132. {
  133. debugger_out(ch);
  134. }
  135. extern "C" int dbgputstr(const char* characters, int length)
  136. {
  137. if (!characters)
  138. return 0;
  139. ScopedSpinLock lock(s_log_lock);
  140. for (int i = 0; i < length; ++i)
  141. debugger_out(characters[i]);
  142. return 0;
  143. }
  144. extern "C" int kernelputstr(const char* characters, int length)
  145. {
  146. if (!characters)
  147. return 0;
  148. ScopedSpinLock lock(s_log_lock);
  149. for (int i = 0; i < length; ++i)
  150. console_out(characters[i]);
  151. return 0;
  152. }
  153. extern "C" int vdbgprintf(const char* fmt, va_list ap)
  154. {
  155. ScopedSpinLock lock(s_log_lock);
  156. color_on();
  157. int ret = printf_internal(debugger_putch, nullptr, fmt, ap);
  158. color_off();
  159. return ret;
  160. }
  161. extern "C" int dbgprintf(const char* fmt, ...)
  162. {
  163. va_list ap;
  164. va_start(ap, fmt);
  165. int ret = vdbgprintf(fmt, ap);
  166. va_end(ap);
  167. return ret;
  168. }