kprintf.cpp 5.1 KB

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