kprintf.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/PrintfImplementation.h>
  27. #include <AK/Types.h>
  28. #include <LibBareMetal/IO.h>
  29. #include <LibBareMetal/Output/Console.h>
  30. #include <LibBareMetal/Output/kstdio.h>
  31. #include <LibC/stdarg.h>
  32. #if defined(KERNEL)
  33. # include <Kernel/Process.h>
  34. #endif
  35. static bool serial_debug;
  36. void set_serial_debug(bool on_or_off)
  37. {
  38. serial_debug = on_or_off;
  39. }
  40. int get_serial_debug()
  41. {
  42. return serial_debug;
  43. }
  44. static void color_on()
  45. {
  46. IO::out8(0xe9, 0x1b);
  47. IO::out8(0xe9, '[');
  48. IO::out8(0xe9, '3');
  49. IO::out8(0xe9, '6');
  50. IO::out8(0xe9, 'm');
  51. }
  52. static void color_off()
  53. {
  54. IO::out8(0xe9, 0x1b);
  55. IO::out8(0xe9, '[');
  56. IO::out8(0xe9, '0');
  57. IO::out8(0xe9, 'm');
  58. }
  59. static void serial_putch(char ch)
  60. {
  61. static bool serial_ready = false;
  62. static bool was_cr = false;
  63. if (!serial_ready) {
  64. IO::out8(0x3F8 + 1, 0x00);
  65. IO::out8(0x3F8 + 3, 0x80);
  66. IO::out8(0x3F8 + 0, 0x02);
  67. IO::out8(0x3F8 + 1, 0x00);
  68. IO::out8(0x3F8 + 3, 0x03);
  69. IO::out8(0x3F8 + 2, 0xC7);
  70. IO::out8(0x3F8 + 4, 0x0B);
  71. serial_ready = true;
  72. }
  73. while ((IO::in8(0x3F8 + 5) & 0x20) == 0)
  74. ;
  75. if (ch == '\n' && !was_cr)
  76. IO::out8(0x3F8, '\r');
  77. IO::out8(0x3F8, ch);
  78. if (ch == '\r')
  79. was_cr = true;
  80. else
  81. was_cr = false;
  82. }
  83. static void console_out(char ch)
  84. {
  85. if (serial_debug)
  86. serial_putch(ch);
  87. // It would be bad to reach the assert in Console()::the() and do a stack overflow
  88. if (Console::is_initialized()) {
  89. Console::the().put_char(ch);
  90. } else {
  91. IO::out8(0xe9, ch);
  92. }
  93. }
  94. static void console_putch(char*&, char ch)
  95. {
  96. console_out(ch);
  97. }
  98. int kprintf(const char* fmt, ...)
  99. {
  100. color_on();
  101. va_list ap;
  102. va_start(ap, fmt);
  103. int ret = printf_internal(console_putch, nullptr, fmt, ap);
  104. va_end(ap);
  105. color_off();
  106. return ret;
  107. }
  108. static void buffer_putch(char*& bufptr, char ch)
  109. {
  110. *bufptr++ = ch;
  111. }
  112. int sprintf(char* buffer, const char* fmt, ...)
  113. {
  114. va_list ap;
  115. va_start(ap, fmt);
  116. int ret = printf_internal(buffer_putch, buffer, fmt, ap);
  117. buffer[ret] = '\0';
  118. va_end(ap);
  119. return ret;
  120. }
  121. static void debugger_out(char ch)
  122. {
  123. if (serial_debug)
  124. serial_putch(ch);
  125. IO::out8(0xe9, ch);
  126. }
  127. static void debugger_putch(char*&, char ch)
  128. {
  129. debugger_out(ch);
  130. }
  131. extern "C" int dbgputstr(const char* characters, int length)
  132. {
  133. if (!characters)
  134. return 0;
  135. for (int i = 0; i < length; ++i)
  136. debugger_out(characters[i]);
  137. return 0;
  138. }
  139. extern "C" int kernelputstr(const char* characters, int length)
  140. {
  141. if (!characters)
  142. return 0;
  143. for (int i = 0; i < length; ++i)
  144. console_out(characters[i]);
  145. return 0;
  146. }
  147. extern "C" int dbgprintf(const char* fmt, ...)
  148. {
  149. color_on();
  150. va_list ap;
  151. va_start(ap, fmt);
  152. int ret = printf_internal(debugger_putch, nullptr, fmt, ap);
  153. va_end(ap);
  154. color_off();
  155. return ret;
  156. }