kprintf.cpp 4.3 KB

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