2020-02-09 14:28:56 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-09 14:28:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/PrintfImplementation.h>
|
2021-08-13 04:25:05 +00:00
|
|
|
#include <AK/StringView.h>
|
2020-02-09 14:28:56 +00:00
|
|
|
#include <AK/Types.h>
|
2021-04-24 14:50:35 +00:00
|
|
|
#include <Kernel/ConsoleDevice.h>
|
2021-04-23 14:26:52 +00:00
|
|
|
#include <Kernel/Devices/PCISerialDevice.h>
|
2021-04-16 19:58:51 +00:00
|
|
|
#include <Kernel/Graphics/GraphicsManagement.h>
|
2020-05-16 10:00:04 +00:00
|
|
|
#include <Kernel/IO.h>
|
2021-08-21 23:37:17 +00:00
|
|
|
#include <Kernel/Locking/Spinlock.h>
|
2021-04-16 19:58:51 +00:00
|
|
|
#include <Kernel/TTY/ConsoleManagement.h>
|
2020-05-16 10:00:04 +00:00
|
|
|
#include <Kernel/kstdio.h>
|
2020-02-09 14:28:56 +00:00
|
|
|
|
|
|
|
#include <LibC/stdarg.h>
|
|
|
|
|
|
|
|
static bool serial_debug;
|
2020-07-02 14:34:14 +00:00
|
|
|
// A recursive spinlock allows us to keep writing in the case where a
|
2021-03-12 16:29:37 +00:00
|
|
|
// page fault happens in the middle of a dbgln(), etc
|
2021-08-21 23:37:17 +00:00
|
|
|
static RecursiveSpinlock s_log_lock;
|
2020-02-09 14:28:56 +00:00
|
|
|
|
|
|
|
void set_serial_debug(bool on_or_off)
|
|
|
|
{
|
|
|
|
serial_debug = on_or_off;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_serial_debug()
|
|
|
|
{
|
|
|
|
return serial_debug;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_putch(char ch)
|
|
|
|
{
|
2021-04-23 14:26:52 +00:00
|
|
|
if (PCISerialDevice::is_available())
|
|
|
|
return PCISerialDevice::the().put_char(ch);
|
|
|
|
|
2020-02-09 14:28:56 +00:00
|
|
|
static bool serial_ready = false;
|
|
|
|
static bool was_cr = false;
|
|
|
|
|
|
|
|
if (!serial_ready) {
|
|
|
|
IO::out8(0x3F8 + 1, 0x00);
|
|
|
|
IO::out8(0x3F8 + 3, 0x80);
|
|
|
|
IO::out8(0x3F8 + 0, 0x02);
|
|
|
|
IO::out8(0x3F8 + 1, 0x00);
|
|
|
|
IO::out8(0x3F8 + 3, 0x03);
|
|
|
|
IO::out8(0x3F8 + 2, 0xC7);
|
|
|
|
IO::out8(0x3F8 + 4, 0x0B);
|
|
|
|
|
|
|
|
serial_ready = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((IO::in8(0x3F8 + 5) & 0x20) == 0)
|
|
|
|
;
|
|
|
|
|
|
|
|
if (ch == '\n' && !was_cr)
|
|
|
|
IO::out8(0x3F8, '\r');
|
|
|
|
|
|
|
|
IO::out8(0x3F8, ch);
|
|
|
|
|
|
|
|
if (ch == '\r')
|
|
|
|
was_cr = true;
|
|
|
|
else
|
|
|
|
was_cr = false;
|
|
|
|
}
|
|
|
|
|
2021-04-16 19:58:51 +00:00
|
|
|
static void critical_console_out(char ch)
|
|
|
|
{
|
|
|
|
if (serial_debug)
|
|
|
|
serial_putch(ch);
|
|
|
|
// No need to output things to the real ConsoleDevice as no one is likely
|
|
|
|
// to read it (because we are in a fatal situation, so only print things and halt)
|
2021-05-31 16:59:48 +00:00
|
|
|
IO::out8(IO::BOCHS_DEBUG_PORT, ch);
|
2021-04-16 19:58:51 +00:00
|
|
|
// We emit chars directly to the string. this is necessary in few cases,
|
|
|
|
// especially when we want to avoid any memory allocations...
|
|
|
|
if (GraphicsManagement::is_initialized() && GraphicsManagement::the().console()) {
|
2021-06-03 14:41:27 +00:00
|
|
|
GraphicsManagement::the().console()->write(ch, true);
|
2021-04-16 19:58:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 15:07:46 +00:00
|
|
|
static void console_out(char ch)
|
2020-02-09 14:28:56 +00:00
|
|
|
{
|
|
|
|
if (serial_debug)
|
|
|
|
serial_putch(ch);
|
|
|
|
|
2021-04-24 14:50:35 +00:00
|
|
|
// It would be bad to reach the assert in ConsoleDevice()::the() and do a stack overflow
|
2020-02-09 14:28:56 +00:00
|
|
|
|
2021-04-24 14:50:35 +00:00
|
|
|
if (ConsoleDevice::is_initialized()) {
|
|
|
|
ConsoleDevice::the().put_char(ch);
|
2020-02-09 14:28:56 +00:00
|
|
|
} else {
|
2021-05-31 16:59:48 +00:00
|
|
|
IO::out8(IO::BOCHS_DEBUG_PORT, ch);
|
2020-02-09 14:28:56 +00:00
|
|
|
}
|
2021-04-16 19:58:51 +00:00
|
|
|
if (ConsoleManagement::is_initialized()) {
|
|
|
|
ConsoleManagement::the().debug_tty()->emit_char(ch);
|
|
|
|
}
|
2020-02-09 14:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void buffer_putch(char*& bufptr, char ch)
|
|
|
|
{
|
|
|
|
*bufptr++ = ch;
|
|
|
|
}
|
|
|
|
|
2020-08-16 13:59:36 +00:00
|
|
|
// Declare it, so that the symbol is exported, because libstdc++ uses it.
|
|
|
|
// However, *only* libstdc++ uses it, and none of the rest of the Kernel.
|
|
|
|
extern "C" int sprintf(char* buffer, const char* fmt, ...);
|
|
|
|
|
2020-02-09 14:28:56 +00:00
|
|
|
int sprintf(char* buffer, const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
int ret = printf_internal(buffer_putch, buffer, fmt, ap);
|
|
|
|
buffer[ret] = '\0';
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
2020-08-15 23:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t __vsnprintf_space_remaining;
|
|
|
|
ALWAYS_INLINE void sized_buffer_putch(char*& bufptr, char ch)
|
|
|
|
{
|
|
|
|
if (__vsnprintf_space_remaining) {
|
|
|
|
*bufptr++ = ch;
|
|
|
|
--__vsnprintf_space_remaining;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int snprintf(char* buffer, size_t size, const char* fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
if (size) {
|
|
|
|
__vsnprintf_space_remaining = size - 1;
|
|
|
|
} else {
|
|
|
|
__vsnprintf_space_remaining = 0;
|
|
|
|
}
|
|
|
|
int ret = printf_internal(sized_buffer_putch, buffer, fmt, ap);
|
|
|
|
if (__vsnprintf_space_remaining) {
|
|
|
|
buffer[ret] = '\0';
|
|
|
|
} else if (size > 0) {
|
|
|
|
buffer[size - 1] = '\0';
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
2020-02-09 14:28:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 11:36:55 +00:00
|
|
|
static inline void internal_dbgputch(char ch)
|
2020-02-09 14:28:56 +00:00
|
|
|
{
|
|
|
|
if (serial_debug)
|
|
|
|
serial_putch(ch);
|
2021-05-31 16:59:48 +00:00
|
|
|
IO::out8(IO::BOCHS_DEBUG_PORT, ch);
|
2020-02-09 14:28:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 11:36:55 +00:00
|
|
|
extern "C" void dbgputch(char ch)
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(s_log_lock);
|
2021-08-06 11:36:55 +00:00
|
|
|
internal_dbgputch(ch);
|
|
|
|
}
|
|
|
|
|
2021-03-12 16:29:37 +00:00
|
|
|
extern "C" void dbgputstr(const char* characters, size_t length)
|
2020-02-09 14:28:56 +00:00
|
|
|
{
|
2020-03-04 18:26:13 +00:00
|
|
|
if (!characters)
|
2021-03-12 16:29:37 +00:00
|
|
|
return;
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(s_log_lock);
|
2021-03-12 16:29:37 +00:00
|
|
|
for (size_t i = 0; i < length; ++i)
|
2021-08-06 11:36:55 +00:00
|
|
|
internal_dbgputch(characters[i]);
|
2020-02-09 14:28:56 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 04:25:05 +00:00
|
|
|
void dbgputstr(StringView view)
|
|
|
|
{
|
|
|
|
::dbgputstr(view.characters_without_null_termination(), view.length());
|
|
|
|
}
|
|
|
|
|
2021-03-12 16:29:37 +00:00
|
|
|
extern "C" void kernelputstr(const char* characters, size_t length)
|
2020-02-28 15:07:46 +00:00
|
|
|
{
|
2020-03-04 18:26:13 +00:00
|
|
|
if (!characters)
|
2021-03-12 16:29:37 +00:00
|
|
|
return;
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(s_log_lock);
|
2021-03-12 16:29:37 +00:00
|
|
|
for (size_t i = 0; i < length; ++i)
|
2020-02-28 15:07:46 +00:00
|
|
|
console_out(characters[i]);
|
|
|
|
}
|
2021-04-16 19:58:51 +00:00
|
|
|
|
|
|
|
extern "C" void kernelcriticalputstr(const char* characters, size_t length)
|
|
|
|
{
|
|
|
|
if (!characters)
|
|
|
|
return;
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(s_log_lock);
|
2021-04-16 19:58:51 +00:00
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
critical_console_out(characters[i]);
|
|
|
|
}
|
2021-09-10 18:46:54 +00:00
|
|
|
|
|
|
|
extern "C" void kernelearlyputstr(const char* characters, size_t length)
|
|
|
|
{
|
|
|
|
if (!characters)
|
|
|
|
return;
|
|
|
|
// NOTE: We do not lock the log lock here, as this function is called before this or any other processor was initialized, meaning:
|
|
|
|
// A) The $gs base was not setup yet, so we cannot enter into critical sections, and as a result we cannot use SpinLocks
|
|
|
|
// B) No other processors may try to print at the same time anyway
|
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
internal_dbgputch(characters[i]);
|
|
|
|
}
|