kprintf.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/PrintfImplementation.h>
  7. #include <AK/StringView.h>
  8. #include <AK/Types.h>
  9. #include <Kernel/ConsoleDevice.h>
  10. #include <Kernel/Devices/PCISerialDevice.h>
  11. #include <Kernel/Graphics/GraphicsManagement.h>
  12. #include <Kernel/IO.h>
  13. #include <Kernel/Locking/Spinlock.h>
  14. #include <Kernel/TTY/ConsoleManagement.h>
  15. #include <Kernel/kstdio.h>
  16. #include <LibC/stdarg.h>
  17. static bool serial_debug;
  18. // A recursive spinlock allows us to keep writing in the case where a
  19. // page fault happens in the middle of a dbgln(), etc
  20. static RecursiveSpinlock s_log_lock;
  21. void set_serial_debug(bool on_or_off)
  22. {
  23. serial_debug = on_or_off;
  24. }
  25. int get_serial_debug()
  26. {
  27. return serial_debug;
  28. }
  29. static void serial_putch(char ch)
  30. {
  31. if (PCISerialDevice::is_available())
  32. return PCISerialDevice::the().put_char(ch);
  33. static bool serial_ready = false;
  34. static bool was_cr = false;
  35. if (!serial_ready) {
  36. IO::out8(0x3F8 + 1, 0x00);
  37. IO::out8(0x3F8 + 3, 0x80);
  38. IO::out8(0x3F8 + 0, 0x02);
  39. IO::out8(0x3F8 + 1, 0x00);
  40. IO::out8(0x3F8 + 3, 0x03);
  41. IO::out8(0x3F8 + 2, 0xC7);
  42. IO::out8(0x3F8 + 4, 0x0B);
  43. serial_ready = true;
  44. }
  45. while ((IO::in8(0x3F8 + 5) & 0x20) == 0)
  46. ;
  47. if (ch == '\n' && !was_cr)
  48. IO::out8(0x3F8, '\r');
  49. IO::out8(0x3F8, ch);
  50. if (ch == '\r')
  51. was_cr = true;
  52. else
  53. was_cr = false;
  54. }
  55. static void critical_console_out(char ch)
  56. {
  57. if (serial_debug)
  58. serial_putch(ch);
  59. // No need to output things to the real ConsoleDevice as no one is likely
  60. // to read it (because we are in a fatal situation, so only print things and halt)
  61. IO::out8(IO::BOCHS_DEBUG_PORT, ch);
  62. // We emit chars directly to the string. this is necessary in few cases,
  63. // especially when we want to avoid any memory allocations...
  64. if (GraphicsManagement::is_initialized() && GraphicsManagement::the().console()) {
  65. GraphicsManagement::the().console()->write(ch, true);
  66. }
  67. }
  68. static void console_out(char ch)
  69. {
  70. if (serial_debug)
  71. serial_putch(ch);
  72. // It would be bad to reach the assert in ConsoleDevice()::the() and do a stack overflow
  73. if (ConsoleDevice::is_initialized()) {
  74. ConsoleDevice::the().put_char(ch);
  75. } else {
  76. IO::out8(IO::BOCHS_DEBUG_PORT, ch);
  77. }
  78. if (ConsoleManagement::is_initialized()) {
  79. ConsoleManagement::the().debug_tty()->emit_char(ch);
  80. }
  81. }
  82. static void buffer_putch(char*& bufptr, char ch)
  83. {
  84. *bufptr++ = ch;
  85. }
  86. // Declare it, so that the symbol is exported, because libstdc++ uses it.
  87. // However, *only* libstdc++ uses it, and none of the rest of the Kernel.
  88. extern "C" int sprintf(char* buffer, const char* fmt, ...);
  89. int sprintf(char* buffer, const char* fmt, ...)
  90. {
  91. va_list ap;
  92. va_start(ap, fmt);
  93. int ret = printf_internal(buffer_putch, buffer, fmt, ap);
  94. buffer[ret] = '\0';
  95. va_end(ap);
  96. return ret;
  97. }
  98. static size_t __vsnprintf_space_remaining;
  99. ALWAYS_INLINE void sized_buffer_putch(char*& bufptr, char ch)
  100. {
  101. if (__vsnprintf_space_remaining) {
  102. *bufptr++ = ch;
  103. --__vsnprintf_space_remaining;
  104. }
  105. }
  106. int snprintf(char* buffer, size_t size, const char* fmt, ...)
  107. {
  108. va_list ap;
  109. va_start(ap, fmt);
  110. if (size) {
  111. __vsnprintf_space_remaining = size - 1;
  112. } else {
  113. __vsnprintf_space_remaining = 0;
  114. }
  115. int ret = printf_internal(sized_buffer_putch, buffer, fmt, ap);
  116. if (__vsnprintf_space_remaining) {
  117. buffer[ret] = '\0';
  118. } else if (size > 0) {
  119. buffer[size - 1] = '\0';
  120. }
  121. va_end(ap);
  122. return ret;
  123. }
  124. static inline void internal_dbgputch(char ch)
  125. {
  126. if (serial_debug)
  127. serial_putch(ch);
  128. IO::out8(IO::BOCHS_DEBUG_PORT, ch);
  129. }
  130. extern "C" void dbgputch(char ch)
  131. {
  132. SpinlockLocker lock(s_log_lock);
  133. internal_dbgputch(ch);
  134. }
  135. extern "C" void dbgputstr(const char* characters, size_t length)
  136. {
  137. if (!characters)
  138. return;
  139. SpinlockLocker lock(s_log_lock);
  140. for (size_t i = 0; i < length; ++i)
  141. internal_dbgputch(characters[i]);
  142. }
  143. void dbgputstr(StringView view)
  144. {
  145. ::dbgputstr(view.characters_without_null_termination(), view.length());
  146. }
  147. extern "C" void kernelputstr(const char* characters, size_t length)
  148. {
  149. if (!characters)
  150. return;
  151. SpinlockLocker lock(s_log_lock);
  152. for (size_t i = 0; i < length; ++i)
  153. console_out(characters[i]);
  154. }
  155. extern "C" void kernelcriticalputstr(const char* characters, size_t length)
  156. {
  157. if (!characters)
  158. return;
  159. SpinlockLocker lock(s_log_lock);
  160. for (size_t i = 0; i < length; ++i)
  161. critical_console_out(characters[i]);
  162. }