2020-02-09 17:28:56 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-09 17:28:56 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/PrintfImplementation.h>
|
2021-08-13 07:25:05 +03:00
|
|
|
#include <AK/StringView.h>
|
2020-02-09 17:28:56 +03:00
|
|
|
#include <AK/Types.h>
|
2022-09-02 13:15:27 +03:00
|
|
|
#include <Kernel/Arch/DebugOutput.h>
|
2022-10-04 03:05:54 +03:00
|
|
|
#if ARCH(X86_64)
|
2022-10-04 13:46:11 +03:00
|
|
|
# include <Kernel/Arch/x86_64/BochsDebugOutput.h>
|
2022-09-02 13:15:27 +03:00
|
|
|
#endif
|
2021-09-16 22:23:25 +03:00
|
|
|
#include <Kernel/Devices/DeviceManagement.h>
|
2023-03-18 14:17:13 +03:00
|
|
|
#include <Kernel/Devices/Generic/ConsoleDevice.h>
|
2021-04-23 17:26:52 +03:00
|
|
|
#include <Kernel/Devices/PCISerialDevice.h>
|
2022-01-29 06:42:23 +03:00
|
|
|
#include <Kernel/Graphics/Console/BootFramebufferConsole.h>
|
2021-04-16 22:58:51 +03:00
|
|
|
#include <Kernel/Graphics/GraphicsManagement.h>
|
2021-08-22 02:37:17 +03:00
|
|
|
#include <Kernel/Locking/Spinlock.h>
|
2021-04-16 22:58:51 +03:00
|
|
|
#include <Kernel/TTY/ConsoleManagement.h>
|
2020-05-16 13:00:04 +03:00
|
|
|
#include <Kernel/kstdio.h>
|
2020-02-09 17:28:56 +03:00
|
|
|
|
2022-01-29 06:42:23 +03:00
|
|
|
namespace Kernel {
|
2022-02-09 22:09:41 +03:00
|
|
|
extern Atomic<Graphics::Console*> g_boot_console;
|
2022-01-29 06:42:23 +03:00
|
|
|
}
|
|
|
|
|
2022-08-25 00:49:23 +03:00
|
|
|
static bool s_serial_debug_enabled;
|
2020-07-02 17:34:14 +03:00
|
|
|
// A recursive spinlock allows us to keep writing in the case where a
|
2021-03-12 19:29:37 +03:00
|
|
|
// page fault happens in the middle of a dbgln(), etc
|
2022-11-09 13:39:58 +03:00
|
|
|
static RecursiveSpinlock<LockRank::None> s_log_lock {};
|
2020-02-09 17:28:56 +03:00
|
|
|
|
2022-08-25 00:49:23 +03:00
|
|
|
void set_serial_debug_enabled(bool desired_state)
|
2020-02-09 17:28:56 +03:00
|
|
|
{
|
2022-08-25 00:49:23 +03:00
|
|
|
s_serial_debug_enabled = desired_state;
|
2020-02-09 17:28:56 +03:00
|
|
|
}
|
|
|
|
|
2022-08-25 00:49:23 +03:00
|
|
|
bool is_serial_debug_enabled()
|
2020-02-09 17:28:56 +03:00
|
|
|
{
|
2022-08-25 00:49:23 +03:00
|
|
|
return s_serial_debug_enabled;
|
2020-02-09 17:28:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void serial_putch(char ch)
|
|
|
|
{
|
2021-04-23 17:26:52 +03:00
|
|
|
if (PCISerialDevice::is_available())
|
|
|
|
return PCISerialDevice::the().put_char(ch);
|
|
|
|
|
2022-09-02 13:15:27 +03:00
|
|
|
debug_output(ch);
|
2020-02-09 17:28:56 +03:00
|
|
|
}
|
|
|
|
|
2021-04-16 22:58:51 +03:00
|
|
|
static void critical_console_out(char ch)
|
|
|
|
{
|
2022-08-25 00:49:23 +03:00
|
|
|
if (s_serial_debug_enabled)
|
2021-04-16 22:58:51 +03:00
|
|
|
serial_putch(ch);
|
2022-09-02 13:15:27 +03:00
|
|
|
|
2022-10-04 03:05:54 +03:00
|
|
|
#if ARCH(X86_64)
|
2021-04-16 22:58:51 +03:00
|
|
|
// 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)
|
2022-09-02 13:15:27 +03:00
|
|
|
bochs_debug_output(ch);
|
|
|
|
#endif
|
|
|
|
|
2021-04-16 22:58:51 +03: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 17:41:27 +03:00
|
|
|
GraphicsManagement::the().console()->write(ch, true);
|
2022-02-09 22:09:41 +03:00
|
|
|
} else if (auto* boot_console = g_boot_console.load()) {
|
2022-01-29 06:42:23 +03:00
|
|
|
boot_console->write(ch, true);
|
2021-04-16 22:58:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 18:07:46 +03:00
|
|
|
static void console_out(char ch)
|
2020-02-09 17:28:56 +03:00
|
|
|
{
|
2022-08-25 00:49:23 +03:00
|
|
|
if (s_serial_debug_enabled)
|
2020-02-09 17:28:56 +03:00
|
|
|
serial_putch(ch);
|
|
|
|
|
2021-04-24 17:50:35 +03:00
|
|
|
// It would be bad to reach the assert in ConsoleDevice()::the() and do a stack overflow
|
2020-02-09 17:28:56 +03:00
|
|
|
|
2021-09-16 22:23:25 +03:00
|
|
|
if (DeviceManagement::the().is_console_device_attached()) {
|
|
|
|
DeviceManagement::the().console_device().put_char(ch);
|
2020-02-09 17:28:56 +03:00
|
|
|
} else {
|
2022-10-04 03:05:54 +03:00
|
|
|
#if ARCH(X86_64)
|
2022-09-02 13:15:27 +03:00
|
|
|
bochs_debug_output(ch);
|
|
|
|
#endif
|
2020-02-09 17:28:56 +03:00
|
|
|
}
|
2021-04-16 22:58:51 +03:00
|
|
|
if (ConsoleManagement::is_initialized()) {
|
|
|
|
ConsoleManagement::the().debug_tty()->emit_char(ch);
|
2022-02-09 22:09:41 +03:00
|
|
|
} else if (auto* boot_console = g_boot_console.load()) {
|
2022-01-29 06:42:23 +03:00
|
|
|
boot_console->write(ch, true);
|
2021-04-16 22:58:51 +03:00
|
|
|
}
|
2020-02-09 17:28:56 +03:00
|
|
|
}
|
|
|
|
|
2020-08-16 16:59:36 +03: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.
|
2022-04-01 20:58:27 +03:00
|
|
|
extern "C" int sprintf(char* buffer, char const* fmt, ...);
|
2020-08-16 16:59:36 +03:00
|
|
|
|
2023-02-26 19:33:59 +03:00
|
|
|
int sprintf(char*, char const*, ...)
|
2020-08-16 02:37:32 +03:00
|
|
|
{
|
2023-02-26 19:33:59 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-02-09 17:28:56 +03:00
|
|
|
}
|
|
|
|
|
2021-08-06 14:36:55 +03:00
|
|
|
static inline void internal_dbgputch(char ch)
|
2020-02-09 17:28:56 +03:00
|
|
|
{
|
2022-08-25 00:49:23 +03:00
|
|
|
if (s_serial_debug_enabled)
|
2020-02-09 17:28:56 +03:00
|
|
|
serial_putch(ch);
|
2022-10-04 03:05:54 +03:00
|
|
|
#if ARCH(X86_64)
|
2022-09-02 13:15:27 +03:00
|
|
|
bochs_debug_output(ch);
|
|
|
|
#endif
|
2020-02-09 17:28:56 +03:00
|
|
|
}
|
|
|
|
|
2022-09-02 15:59:21 +03:00
|
|
|
extern "C" void dbgputchar(char ch)
|
|
|
|
{
|
|
|
|
internal_dbgputch(ch);
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
extern "C" void dbgputstr(char const* characters, size_t length)
|
2020-02-09 17:28:56 +03:00
|
|
|
{
|
2020-03-04 21:26:13 +03:00
|
|
|
if (!characters)
|
2021-03-12 19:29:37 +03:00
|
|
|
return;
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker lock(s_log_lock);
|
2021-03-12 19:29:37 +03:00
|
|
|
for (size_t i = 0; i < length; ++i)
|
2021-08-06 14:36:55 +03:00
|
|
|
internal_dbgputch(characters[i]);
|
2020-02-09 17:28:56 +03:00
|
|
|
}
|
|
|
|
|
2021-08-13 07:25:05 +03:00
|
|
|
void dbgputstr(StringView view)
|
|
|
|
{
|
|
|
|
::dbgputstr(view.characters_without_null_termination(), view.length());
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
extern "C" void kernelputstr(char const* characters, size_t length)
|
2020-02-28 18:07:46 +03:00
|
|
|
{
|
2020-03-04 21:26:13 +03:00
|
|
|
if (!characters)
|
2021-03-12 19:29:37 +03:00
|
|
|
return;
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker lock(s_log_lock);
|
2021-03-12 19:29:37 +03:00
|
|
|
for (size_t i = 0; i < length; ++i)
|
2020-02-28 18:07:46 +03:00
|
|
|
console_out(characters[i]);
|
|
|
|
}
|
2021-04-16 22:58:51 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
extern "C" void kernelcriticalputstr(char const* characters, size_t length)
|
2021-04-16 22:58:51 +03:00
|
|
|
{
|
|
|
|
if (!characters)
|
|
|
|
return;
|
2021-08-22 02:49:22 +03:00
|
|
|
SpinlockLocker lock(s_log_lock);
|
2021-04-16 22:58:51 +03:00
|
|
|
for (size_t i = 0; i < length; ++i)
|
|
|
|
critical_console_out(characters[i]);
|
|
|
|
}
|
2021-09-10 21:46:54 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
extern "C" void kernelearlyputstr(char const* characters, size_t length)
|
2021-09-10 21:46:54 +03:00
|
|
|
{
|
|
|
|
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]);
|
|
|
|
}
|