kprintf.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/Console.h>
  9. #include <Kernel/IO.h>
  10. #include <Kernel/Process.h>
  11. #include <Kernel/SpinLock.h>
  12. #include <Kernel/kstdio.h>
  13. #include <LibC/stdarg.h>
  14. static bool serial_debug;
  15. // A recursive spinlock allows us to keep writing in the case where a
  16. // page fault happens in the middle of a dbgln(), etc
  17. static RecursiveSpinLock s_log_lock;
  18. void set_serial_debug(bool on_or_off)
  19. {
  20. serial_debug = on_or_off;
  21. }
  22. int get_serial_debug()
  23. {
  24. return serial_debug;
  25. }
  26. static void serial_putch(char ch)
  27. {
  28. static bool serial_ready = false;
  29. static bool was_cr = false;
  30. if (!serial_ready) {
  31. IO::out8(0x3F8 + 1, 0x00);
  32. IO::out8(0x3F8 + 3, 0x80);
  33. IO::out8(0x3F8 + 0, 0x02);
  34. IO::out8(0x3F8 + 1, 0x00);
  35. IO::out8(0x3F8 + 3, 0x03);
  36. IO::out8(0x3F8 + 2, 0xC7);
  37. IO::out8(0x3F8 + 4, 0x0B);
  38. serial_ready = true;
  39. }
  40. while ((IO::in8(0x3F8 + 5) & 0x20) == 0)
  41. ;
  42. if (ch == '\n' && !was_cr)
  43. IO::out8(0x3F8, '\r');
  44. IO::out8(0x3F8, ch);
  45. if (ch == '\r')
  46. was_cr = true;
  47. else
  48. was_cr = false;
  49. }
  50. static void console_out(char ch)
  51. {
  52. if (serial_debug)
  53. serial_putch(ch);
  54. // It would be bad to reach the assert in Console()::the() and do a stack overflow
  55. if (Console::is_initialized()) {
  56. Console::the().put_char(ch);
  57. } else {
  58. IO::out8(0xe9, ch);
  59. }
  60. }
  61. static void buffer_putch(char*& bufptr, char ch)
  62. {
  63. *bufptr++ = ch;
  64. }
  65. // Declare it, so that the symbol is exported, because libstdc++ uses it.
  66. // However, *only* libstdc++ uses it, and none of the rest of the Kernel.
  67. extern "C" int sprintf(char* buffer, const char* fmt, ...);
  68. int sprintf(char* buffer, const char* fmt, ...)
  69. {
  70. va_list ap;
  71. va_start(ap, fmt);
  72. int ret = printf_internal(buffer_putch, buffer, fmt, ap);
  73. buffer[ret] = '\0';
  74. va_end(ap);
  75. return ret;
  76. }
  77. static size_t __vsnprintf_space_remaining;
  78. ALWAYS_INLINE void sized_buffer_putch(char*& bufptr, char ch)
  79. {
  80. if (__vsnprintf_space_remaining) {
  81. *bufptr++ = ch;
  82. --__vsnprintf_space_remaining;
  83. }
  84. }
  85. int snprintf(char* buffer, size_t size, const char* fmt, ...)
  86. {
  87. va_list ap;
  88. va_start(ap, fmt);
  89. if (size) {
  90. __vsnprintf_space_remaining = size - 1;
  91. } else {
  92. __vsnprintf_space_remaining = 0;
  93. }
  94. int ret = printf_internal(sized_buffer_putch, buffer, fmt, ap);
  95. if (__vsnprintf_space_remaining) {
  96. buffer[ret] = '\0';
  97. } else if (size > 0) {
  98. buffer[size - 1] = '\0';
  99. }
  100. va_end(ap);
  101. return ret;
  102. }
  103. static void debugger_out(char ch)
  104. {
  105. if (serial_debug)
  106. serial_putch(ch);
  107. IO::out8(0xe9, ch);
  108. }
  109. extern "C" void dbgputstr(const char* characters, size_t length)
  110. {
  111. if (!characters)
  112. return;
  113. ScopedSpinLock lock(s_log_lock);
  114. for (size_t i = 0; i < length; ++i)
  115. debugger_out(characters[i]);
  116. }
  117. extern "C" void kernelputstr(const char* characters, size_t length)
  118. {
  119. if (!characters)
  120. return;
  121. ScopedSpinLock lock(s_log_lock);
  122. for (size_t i = 0; i < length; ++i)
  123. console_out(characters[i]);
  124. }