kprintf.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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::BootFramebufferConsole*> boot_framebuffer_console;
  21. }
  22. static bool serial_debug;
  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;
  26. void set_serial_debug(bool on_or_off)
  27. {
  28. serial_debug = on_or_off;
  29. }
  30. int get_serial_debug()
  31. {
  32. return serial_debug;
  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 (serial_debug)
  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 = boot_framebuffer_console.load()) {
  69. boot_console->write(ch, true);
  70. }
  71. }
  72. static void console_out(char ch)
  73. {
  74. if (serial_debug)
  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 = boot_framebuffer_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, const char* fmt, ...);
  95. int sprintf(char* buffer, const char* 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. static size_t __vsnprintf_space_remaining;
  105. ALWAYS_INLINE void sized_buffer_putch(char*& bufptr, char ch)
  106. {
  107. if (__vsnprintf_space_remaining) {
  108. *bufptr++ = ch;
  109. --__vsnprintf_space_remaining;
  110. }
  111. }
  112. int snprintf(char* buffer, size_t size, const char* fmt, ...)
  113. {
  114. va_list ap;
  115. va_start(ap, fmt);
  116. if (size) {
  117. __vsnprintf_space_remaining = size - 1;
  118. } else {
  119. __vsnprintf_space_remaining = 0;
  120. }
  121. int ret = printf_internal(sized_buffer_putch, buffer, fmt, ap);
  122. if (__vsnprintf_space_remaining) {
  123. buffer[ret] = '\0';
  124. } else if (size > 0) {
  125. buffer[size - 1] = '\0';
  126. }
  127. va_end(ap);
  128. return ret;
  129. }
  130. static inline void internal_dbgputch(char ch)
  131. {
  132. if (serial_debug)
  133. serial_putch(ch);
  134. IO::out8(IO::BOCHS_DEBUG_PORT, ch);
  135. }
  136. extern "C" void dbgputstr(const char* characters, size_t length)
  137. {
  138. if (!characters)
  139. return;
  140. SpinlockLocker lock(s_log_lock);
  141. for (size_t i = 0; i < length; ++i)
  142. internal_dbgputch(characters[i]);
  143. }
  144. void dbgputstr(StringView view)
  145. {
  146. ::dbgputstr(view.characters_without_null_termination(), view.length());
  147. }
  148. extern "C" void kernelputstr(const char* characters, size_t length)
  149. {
  150. if (!characters)
  151. return;
  152. SpinlockLocker lock(s_log_lock);
  153. for (size_t i = 0; i < length; ++i)
  154. console_out(characters[i]);
  155. }
  156. extern "C" void kernelcriticalputstr(const char* characters, size_t length)
  157. {
  158. if (!characters)
  159. return;
  160. SpinlockLocker lock(s_log_lock);
  161. for (size_t i = 0; i < length; ++i)
  162. critical_console_out(characters[i]);
  163. }
  164. extern "C" void kernelearlyputstr(const char* characters, size_t length)
  165. {
  166. if (!characters)
  167. return;
  168. // NOTE: We do not lock the log lock here, as this function is called before this or any other processor was initialized, meaning:
  169. // A) The $gs base was not setup yet, so we cannot enter into critical sections, and as a result we cannot use SpinLocks
  170. // B) No other processors may try to print at the same time anyway
  171. for (size_t i = 0; i < length; ++i)
  172. internal_dbgputch(characters[i]);
  173. }