kprintf.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/Arch/x86/IO.h>
  10. #include <Kernel/Devices/ConsoleDevice.h>
  11. #include <Kernel/Devices/DeviceManagement.h>
  12. #include <Kernel/Devices/PCISerialDevice.h>
  13. #include <Kernel/Graphics/Console/BootFramebufferConsole.h>
  14. #include <Kernel/Graphics/GraphicsManagement.h>
  15. #include <Kernel/Locking/Spinlock.h>
  16. #include <Kernel/TTY/ConsoleManagement.h>
  17. #include <Kernel/kstdio.h>
  18. #include <LibC/stdarg.h>
  19. namespace Kernel {
  20. extern Atomic<Graphics::Console*> g_boot_console;
  21. }
  22. static bool s_serial_debug_enabled;
  23. // A recursive spinlock allows us to keep writing in the case where a
  24. // page fault happens in the middle of a dbgln(), etc
  25. static RecursiveSpinlock s_log_lock { LockRank::None };
  26. void set_serial_debug_enabled(bool desired_state)
  27. {
  28. s_serial_debug_enabled = desired_state;
  29. }
  30. bool is_serial_debug_enabled()
  31. {
  32. return s_serial_debug_enabled;
  33. }
  34. static void serial_putch(char ch)
  35. {
  36. if (PCISerialDevice::is_available())
  37. return PCISerialDevice::the().put_char(ch);
  38. static bool serial_ready = false;
  39. static bool was_cr = false;
  40. if (!serial_ready) {
  41. IO::out8(0x3F8 + 1, 0x00);
  42. IO::out8(0x3F8 + 3, 0x80);
  43. IO::out8(0x3F8 + 0, 0x02);
  44. IO::out8(0x3F8 + 1, 0x00);
  45. IO::out8(0x3F8 + 3, 0x03);
  46. IO::out8(0x3F8 + 2, 0xC7);
  47. IO::out8(0x3F8 + 4, 0x0B);
  48. serial_ready = true;
  49. }
  50. while ((IO::in8(0x3F8 + 5) & 0x20) == 0)
  51. ;
  52. if (ch == '\n' && !was_cr)
  53. IO::out8(0x3F8, '\r');
  54. IO::out8(0x3F8, ch);
  55. was_cr = ch == '\r';
  56. }
  57. static void critical_console_out(char ch)
  58. {
  59. if (s_serial_debug_enabled)
  60. serial_putch(ch);
  61. // No need to output things to the real ConsoleDevice as no one is likely
  62. // to read it (because we are in a fatal situation, so only print things and halt)
  63. IO::out8(IO::BOCHS_DEBUG_PORT, ch);
  64. // We emit chars directly to the string. this is necessary in few cases,
  65. // especially when we want to avoid any memory allocations...
  66. if (GraphicsManagement::is_initialized() && GraphicsManagement::the().console()) {
  67. GraphicsManagement::the().console()->write(ch, true);
  68. } else if (auto* boot_console = g_boot_console.load()) {
  69. boot_console->write(ch, true);
  70. }
  71. }
  72. static void console_out(char ch)
  73. {
  74. if (s_serial_debug_enabled)
  75. serial_putch(ch);
  76. // It would be bad to reach the assert in ConsoleDevice()::the() and do a stack overflow
  77. if (DeviceManagement::the().is_console_device_attached()) {
  78. DeviceManagement::the().console_device().put_char(ch);
  79. } else {
  80. IO::out8(IO::BOCHS_DEBUG_PORT, ch);
  81. }
  82. if (ConsoleManagement::is_initialized()) {
  83. ConsoleManagement::the().debug_tty()->emit_char(ch);
  84. } else if (auto* boot_console = g_boot_console.load()) {
  85. boot_console->write(ch, true);
  86. }
  87. }
  88. static void buffer_putch(char*& bufptr, char ch)
  89. {
  90. *bufptr++ = ch;
  91. }
  92. // Declare it, so that the symbol is exported, because libstdc++ uses it.
  93. // However, *only* libstdc++ uses it, and none of the rest of the Kernel.
  94. extern "C" int sprintf(char* buffer, char const* fmt, ...);
  95. int sprintf(char* buffer, char const* fmt, ...)
  96. {
  97. va_list ap;
  98. va_start(ap, fmt);
  99. int ret = printf_internal(buffer_putch, buffer, fmt, ap);
  100. buffer[ret] = '\0';
  101. va_end(ap);
  102. return ret;
  103. }
  104. int snprintf(char* buffer, size_t size, char const* fmt, ...)
  105. {
  106. va_list ap;
  107. va_start(ap, fmt);
  108. size_t space_remaining = 0;
  109. if (size) {
  110. space_remaining = size - 1;
  111. } else {
  112. space_remaining = 0;
  113. }
  114. auto sized_buffer_putch = [&](char*& bufptr, char ch) {
  115. if (space_remaining) {
  116. *bufptr++ = ch;
  117. --space_remaining;
  118. }
  119. };
  120. int ret = printf_internal(sized_buffer_putch, buffer, fmt, ap);
  121. if (space_remaining) {
  122. buffer[ret] = '\0';
  123. } else if (size > 0) {
  124. buffer[size - 1] = '\0';
  125. }
  126. va_end(ap);
  127. return ret;
  128. }
  129. static inline void internal_dbgputch(char ch)
  130. {
  131. if (s_serial_debug_enabled)
  132. serial_putch(ch);
  133. IO::out8(IO::BOCHS_DEBUG_PORT, ch);
  134. }
  135. extern "C" void dbgputstr(char const* 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(char const* 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(char const* 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. }
  163. extern "C" void kernelearlyputstr(char const* characters, size_t length)
  164. {
  165. if (!characters)
  166. return;
  167. // NOTE: We do not lock the log lock here, as this function is called before this or any other processor was initialized, meaning:
  168. // A) The $gs base was not setup yet, so we cannot enter into critical sections, and as a result we cannot use SpinLocks
  169. // B) No other processors may try to print at the same time anyway
  170. for (size_t i = 0; i < length; ++i)
  171. internal_dbgputch(characters[i]);
  172. }