kprintf.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/Types.h>
  8. #include <Kernel/ConsoleDevice.h>
  9. #include <Kernel/Devices/PCISerialDevice.h>
  10. #include <Kernel/Graphics/Console/Console.h>
  11. #include <Kernel/Graphics/GraphicsManagement.h>
  12. #include <Kernel/IO.h>
  13. #include <Kernel/Process.h>
  14. #include <Kernel/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. if (ch == '\r')
  52. was_cr = true;
  53. else
  54. was_cr = false;
  55. }
  56. static void critical_console_out(char ch)
  57. {
  58. if (serial_debug)
  59. serial_putch(ch);
  60. // No need to output things to the real ConsoleDevice as no one is likely
  61. // to read it (because we are in a fatal situation, so only print things and halt)
  62. IO::out8(IO::BOCHS_DEBUG_PORT, ch);
  63. // We emit chars directly to the string. this is necessary in few cases,
  64. // especially when we want to avoid any memory allocations...
  65. if (GraphicsManagement::is_initialized() && GraphicsManagement::the().console()) {
  66. GraphicsManagement::the().console()->write(ch);
  67. }
  68. }
  69. static void console_out(char ch)
  70. {
  71. if (serial_debug)
  72. serial_putch(ch);
  73. // It would be bad to reach the assert in ConsoleDevice()::the() and do a stack overflow
  74. if (ConsoleDevice::is_initialized()) {
  75. ConsoleDevice::the().put_char(ch);
  76. } else {
  77. IO::out8(IO::BOCHS_DEBUG_PORT, ch);
  78. }
  79. if (ConsoleManagement::is_initialized()) {
  80. ConsoleManagement::the().debug_tty()->emit_char(ch);
  81. }
  82. }
  83. static void buffer_putch(char*& bufptr, char ch)
  84. {
  85. *bufptr++ = ch;
  86. }
  87. // Declare it, so that the symbol is exported, because libstdc++ uses it.
  88. // However, *only* libstdc++ uses it, and none of the rest of the Kernel.
  89. extern "C" int sprintf(char* buffer, const char* fmt, ...);
  90. int sprintf(char* buffer, const char* fmt, ...)
  91. {
  92. va_list ap;
  93. va_start(ap, fmt);
  94. int ret = printf_internal(buffer_putch, buffer, fmt, ap);
  95. buffer[ret] = '\0';
  96. va_end(ap);
  97. return ret;
  98. }
  99. static size_t __vsnprintf_space_remaining;
  100. ALWAYS_INLINE void sized_buffer_putch(char*& bufptr, char ch)
  101. {
  102. if (__vsnprintf_space_remaining) {
  103. *bufptr++ = ch;
  104. --__vsnprintf_space_remaining;
  105. }
  106. }
  107. int snprintf(char* buffer, size_t size, const char* fmt, ...)
  108. {
  109. va_list ap;
  110. va_start(ap, fmt);
  111. if (size) {
  112. __vsnprintf_space_remaining = size - 1;
  113. } else {
  114. __vsnprintf_space_remaining = 0;
  115. }
  116. int ret = printf_internal(sized_buffer_putch, buffer, fmt, ap);
  117. if (__vsnprintf_space_remaining) {
  118. buffer[ret] = '\0';
  119. } else if (size > 0) {
  120. buffer[size - 1] = '\0';
  121. }
  122. va_end(ap);
  123. return ret;
  124. }
  125. static void debugger_out(char ch)
  126. {
  127. if (serial_debug)
  128. serial_putch(ch);
  129. IO::out8(IO::BOCHS_DEBUG_PORT, ch);
  130. }
  131. extern "C" void dbgputstr(const char* characters, size_t length)
  132. {
  133. if (!characters)
  134. return;
  135. ScopedSpinLock lock(s_log_lock);
  136. for (size_t i = 0; i < length; ++i)
  137. debugger_out(characters[i]);
  138. }
  139. extern "C" void kernelputstr(const char* characters, size_t length)
  140. {
  141. if (!characters)
  142. return;
  143. ScopedSpinLock lock(s_log_lock);
  144. for (size_t i = 0; i < length; ++i)
  145. console_out(characters[i]);
  146. }
  147. extern "C" void kernelcriticalputstr(const char* characters, size_t length)
  148. {
  149. if (!characters)
  150. return;
  151. ScopedSpinLock lock(s_log_lock);
  152. for (size_t i = 0; i < length; ++i)
  153. critical_console_out(characters[i]);
  154. }