diff options
author | Liav A <liavalb@gmail.com> | 2021-09-16 22:23:25 +0300 |
---|---|---|
committer | Idan Horowitz <idan.horowitz@gmail.com> | 2021-09-17 01:02:48 +0300 |
commit | fd4397a430c6c03a91e20f55c892d9f53509dd71 (patch) | |
tree | 6954441fb3e620f385a59aa8b0ea365de5c28969 /Kernel | |
parent | 5e8dcb9ca747cec1e15551e6a0fea09dd70820df (diff) | |
download | serenity-fd4397a430c6c03a91e20f55c892d9f53509dd71.zip |
Kernel/Devices: Use try_create_device helper for ConsoleDevice
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/Devices/ConsoleDevice.cpp | 20 | ||||
-rw-r--r-- | Kernel/Devices/ConsoleDevice.h | 8 | ||||
-rw-r--r-- | Kernel/Devices/DeviceManagement.cpp | 15 | ||||
-rw-r--r-- | Kernel/Devices/DeviceManagement.h | 9 | ||||
-rw-r--r-- | Kernel/GlobalProcessExposed.cpp | 3 | ||||
-rw-r--r-- | Kernel/TTY/ConsoleManagement.cpp | 4 | ||||
-rw-r--r-- | Kernel/init.cpp | 3 | ||||
-rw-r--r-- | Kernel/kprintf.cpp | 5 |
8 files changed, 42 insertions, 25 deletions
diff --git a/Kernel/Devices/ConsoleDevice.cpp b/Kernel/Devices/ConsoleDevice.cpp index d782848f0e..000919db7f 100644 --- a/Kernel/Devices/ConsoleDevice.cpp +++ b/Kernel/Devices/ConsoleDevice.cpp @@ -4,8 +4,8 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include <AK/Singleton.h> #include <Kernel/Devices/ConsoleDevice.h> +#include <Kernel/Devices/DeviceManagement.h> #include <Kernel/IO.h> #include <Kernel/Locking/Spinlock.h> #include <Kernel/Sections.h> @@ -14,23 +14,13 @@ // Output bytes to kernel debug port 0xE9 (Bochs console). It's very handy. #define CONSOLE_OUT_TO_BOCHS_DEBUG_PORT -static Singleton<ConsoleDevice> s_the; static Kernel::Spinlock g_console_lock; -UNMAP_AFTER_INIT void ConsoleDevice::initialize() +UNMAP_AFTER_INIT NonnullRefPtr<ConsoleDevice> ConsoleDevice::must_create() { - s_the.ensure_instance(); - s_the->after_inserting(); -} - -ConsoleDevice& ConsoleDevice::the() -{ - return *s_the; -} - -bool ConsoleDevice::is_initialized() -{ - return s_the.is_initialized(); + auto device_or_error = DeviceManagement::try_create_device<ConsoleDevice>(); + VERIFY(!device_or_error.is_error()); + return device_or_error.release_value(); } UNMAP_AFTER_INIT ConsoleDevice::ConsoleDevice() diff --git a/Kernel/Devices/ConsoleDevice.h b/Kernel/Devices/ConsoleDevice.h index 9bcd5f1160..95b5538ec6 100644 --- a/Kernel/Devices/ConsoleDevice.h +++ b/Kernel/Devices/ConsoleDevice.h @@ -14,12 +14,11 @@ namespace Kernel { class ConsoleDevice final : public CharacterDevice { AK_MAKE_ETERNAL + friend class DeviceManagement; + public: - static ConsoleDevice& the(); - static void initialize(); - static bool is_initialized(); + static NonnullRefPtr<ConsoleDevice> must_create(); - ConsoleDevice(); virtual ~ConsoleDevice() override; // ^CharacterDevice @@ -34,6 +33,7 @@ public: const CircularQueue<char, 16384>& logbuffer() const { return m_logbuffer; } private: + ConsoleDevice(); CircularQueue<char, 16384> m_logbuffer; }; diff --git a/Kernel/Devices/DeviceManagement.cpp b/Kernel/Devices/DeviceManagement.cpp index b02fc06cbf..41d8d73e21 100644 --- a/Kernel/Devices/DeviceManagement.cpp +++ b/Kernel/Devices/DeviceManagement.cpp @@ -22,6 +22,12 @@ UNMAP_AFTER_INIT void DeviceManagement::initialize() s_the.ensure_instance(); } + +UNMAP_AFTER_INIT void DeviceManagement::attach_console_device(ConsoleDevice const& device) +{ + m_console_device = device; +} + UNMAP_AFTER_INIT void DeviceManagement::attach_null_device(NullDevice const& device) { m_null_device = device; @@ -85,4 +91,13 @@ NullDevice const& DeviceManagement::null_device() const return *m_null_device; } +ConsoleDevice const& DeviceManagement::console_device() const +{ + return *m_console_device; +} +ConsoleDevice& DeviceManagement::console_device() +{ + return *m_console_device; +} + } diff --git a/Kernel/Devices/DeviceManagement.h b/Kernel/Devices/DeviceManagement.h index 4ec1720e11..1cb4553a0d 100644 --- a/Kernel/Devices/DeviceManagement.h +++ b/Kernel/Devices/DeviceManagement.h @@ -15,6 +15,7 @@ #include <Kernel/API/KResult.h> #include <Kernel/API/TimePage.h> #include <Kernel/Arch/x86/RegisterState.h> +#include <Kernel/Devices/ConsoleDevice.h> #include <Kernel/Devices/Device.h> #include <Kernel/Devices/NullDevice.h> #include <Kernel/UnixTypes.h> @@ -30,14 +31,21 @@ public: static DeviceManagement& the(); void attach_null_device(NullDevice const&); + bool is_console_device_attached() const { return !m_console_device.is_null(); } + void attach_console_device(ConsoleDevice const&); + void after_inserting_device(Badge<Device>, Device&); void before_device_removal(Badge<Device>, Device&); void for_each(Function<void(Device&)>); Device* get_device(unsigned major, unsigned minor); + NullDevice const& null_device() const; NullDevice& null_device(); + ConsoleDevice const& console_device() const; + ConsoleDevice& console_device(); + template<typename DeviceType, typename... Args> static inline KResultOr<NonnullRefPtr<DeviceType>> try_create_device(Args&&... args) { @@ -48,6 +56,7 @@ public: private: RefPtr<NullDevice> m_null_device; + RefPtr<ConsoleDevice> m_console_device; MutexProtected<HashMap<u32, Device*>> m_devices; }; diff --git a/Kernel/GlobalProcessExposed.cpp b/Kernel/GlobalProcessExposed.cpp index 0cbd3538a6..5ee5930bf9 100644 --- a/Kernel/GlobalProcessExposed.cpp +++ b/Kernel/GlobalProcessExposed.cpp @@ -559,8 +559,9 @@ private: ProcFSDmesg(); virtual KResult try_generate(KBufferBuilder& builder) override { + VERIFY(DeviceManagement::the().is_console_device_attached()); InterruptDisabler disabler; - for (char ch : ConsoleDevice::the().logbuffer()) { + for (char ch : DeviceManagement::the().console_device().logbuffer()) { TRY(builder.append(ch)); } return KSuccess; diff --git a/Kernel/TTY/ConsoleManagement.cpp b/Kernel/TTY/ConsoleManagement.cpp index 038fba66d4..690e0b14b1 100644 --- a/Kernel/TTY/ConsoleManagement.cpp +++ b/Kernel/TTY/ConsoleManagement.cpp @@ -7,6 +7,7 @@ #include <AK/Singleton.h> #include <Kernel/CommandLine.h> #include <Kernel/Debug.h> +#include <Kernel/Devices/DeviceManagement.h> #include <Kernel/Graphics/GraphicsManagement.h> #include <Kernel/Panic.h> #include <Kernel/Sections.h> @@ -48,7 +49,8 @@ UNMAP_AFTER_INIT void ConsoleManagement::initialize() for (size_t index = 0; index < s_max_virtual_consoles; index++) { // FIXME: Better determine the debug TTY we chose... if (index == 1) { - m_consoles.append(VirtualConsole::create_with_preset_log(index, ConsoleDevice::the().logbuffer())); + VERIFY(DeviceManagement::the().is_console_device_attached()); + m_consoles.append(VirtualConsole::create_with_preset_log(index, DeviceManagement::the().console_device().logbuffer())); continue; } m_consoles.append(VirtualConsole::create(index)); diff --git a/Kernel/init.cpp b/Kernel/init.cpp index 516eea7d0b..9961547af8 100644 --- a/Kernel/init.cpp +++ b/Kernel/init.cpp @@ -186,8 +186,7 @@ extern "C" [[noreturn]] UNMAP_AFTER_INIT void init(BootInfo const& boot_info) DeviceManagement::initialize(); SysFSComponentRegistry::initialize(); DeviceManagement::the().attach_null_device(*NullDevice::must_initialize()); - - ConsoleDevice::initialize(); + DeviceManagement::the().attach_console_device(*ConsoleDevice::must_create()); s_bsp_processor.initialize(0); CommandLine::initialize(); diff --git a/Kernel/kprintf.cpp b/Kernel/kprintf.cpp index 4d3583d9a6..6aa0529139 100644 --- a/Kernel/kprintf.cpp +++ b/Kernel/kprintf.cpp @@ -8,6 +8,7 @@ #include <AK/StringView.h> #include <AK/Types.h> #include <Kernel/Devices/ConsoleDevice.h> +#include <Kernel/Devices/DeviceManagement.h> #include <Kernel/Devices/PCISerialDevice.h> #include <Kernel/Graphics/GraphicsManagement.h> #include <Kernel/IO.h> @@ -87,8 +88,8 @@ static void console_out(char ch) // It would be bad to reach the assert in ConsoleDevice()::the() and do a stack overflow - if (ConsoleDevice::is_initialized()) { - ConsoleDevice::the().put_char(ch); + if (DeviceManagement::the().is_console_device_attached()) { + DeviceManagement::the().console_device().put_char(ch); } else { IO::out8(IO::BOCHS_DEBUG_PORT, ch); } |