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-10-01 06:58:50 +00:00
|
|
|
#include <Kernel/Arch/x86/IO.h>
|
2021-09-12 18:37:31 +00:00
|
|
|
#include <Kernel/Devices/ConsoleDevice.h>
|
2021-09-16 19:23:25 +00:00
|
|
|
#include <Kernel/Devices/DeviceManagement.h>
|
2021-04-23 14:26:52 +00:00
|
|
|
#include <Kernel/Devices/PCISerialDevice.h>
|
2022-01-29 03:42:23 +00:00
|
|
|
#include <Kernel/Graphics/Console/BootFramebufferConsole.h>
|
2021-04-16 19:58:51 +00:00
|
|
|
#include <Kernel/Graphics/GraphicsManagement.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>
|
|
|
|
|
2022-01-29 03:42:23 +00:00
|
|
|
namespace Kernel {
|
2022-02-09 19:09:41 +00:00
|
|
|
extern Atomic<Graphics::Console*> g_boot_console;
|
2022-01-29 03:42:23 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 14:28:56 +00:00
|
|
|
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
|
2022-08-18 19:46:28 +00:00
|
|
|
static RecursiveSpinlock s_log_lock { LockRank::None };
|
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);
|
|
|
|
|
2021-12-29 00:03:09 +00:00
|
|
|
was_cr = ch == '\r';
|
2020-02-09 14:28:56 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2022-02-09 19:09:41 +00:00
|
|
|
} else if (auto* boot_console = g_boot_console.load()) {
|
2022-01-29 03:42:23 +00:00
|
|
|
boot_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-09-16 19:23:25 +00:00
|
|
|
if (DeviceManagement::the().is_console_device_attached()) {
|
|
|
|
DeviceManagement::the().console_device().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);
|
2022-02-09 19:09:41 +00:00
|
|
|
} else if (auto* boot_console = g_boot_console.load()) {
|
2022-01-29 03:42:23 +00:00
|
|
|
boot_console->write(ch, true);
|
2021-04-16 19:58:51 +00:00
|
|
|
}
|
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.
|
2022-04-01 17:58:27 +00:00
|
|
|
extern "C" int sprintf(char* buffer, char const* fmt, ...);
|
2020-08-16 13:59:36 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
int sprintf(char* buffer, char const* fmt, ...)
|
2020-02-09 14:28:56 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
int snprintf(char* buffer, size_t size, char const* fmt, ...)
|
2020-08-15 23:37:32 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
2022-02-09 05:33:35 +00:00
|
|
|
size_t space_remaining = 0;
|
2020-08-15 23:37:32 +00:00
|
|
|
if (size) {
|
2022-02-09 05:33:35 +00:00
|
|
|
space_remaining = size - 1;
|
2020-08-15 23:37:32 +00:00
|
|
|
} else {
|
2022-02-09 05:33:35 +00:00
|
|
|
space_remaining = 0;
|
2020-08-15 23:37:32 +00:00
|
|
|
}
|
2022-02-09 05:33:35 +00:00
|
|
|
auto sized_buffer_putch = [&](char*& bufptr, char ch) {
|
|
|
|
if (space_remaining) {
|
|
|
|
*bufptr++ = ch;
|
|
|
|
--space_remaining;
|
|
|
|
}
|
|
|
|
};
|
2020-08-15 23:37:32 +00:00
|
|
|
int ret = printf_internal(sized_buffer_putch, buffer, fmt, ap);
|
2022-02-09 05:33:35 +00:00
|
|
|
if (space_remaining) {
|
2020-08-15 23:37:32 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
extern "C" void dbgputstr(char const* 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());
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
extern "C" void kernelputstr(char const* 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
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
extern "C" void kernelcriticalputstr(char const* characters, size_t length)
|
2021-04-16 19:58:51 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
extern "C" void kernelearlyputstr(char const* characters, size_t length)
|
2021-09-10 18:46:54 +00: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]);
|
|
|
|
}
|