mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 18:22:45 +00:00 
			
		
		
		
	Kernel/Devices: Use try_create_device helper for ConsoleDevice
This commit is contained in:
		
							parent
							
								
									5e8dcb9ca7
								
							
						
					
					
						commit
						fd4397a430
					
				
					 8 changed files with 43 additions and 26 deletions
				
			
		|  | @ -4,8 +4,8 @@ | ||||||
|  * SPDX-License-Identifier: BSD-2-Clause |  * SPDX-License-Identifier: BSD-2-Clause | ||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| #include <AK/Singleton.h> |  | ||||||
| #include <Kernel/Devices/ConsoleDevice.h> | #include <Kernel/Devices/ConsoleDevice.h> | ||||||
|  | #include <Kernel/Devices/DeviceManagement.h> | ||||||
| #include <Kernel/IO.h> | #include <Kernel/IO.h> | ||||||
| #include <Kernel/Locking/Spinlock.h> | #include <Kernel/Locking/Spinlock.h> | ||||||
| #include <Kernel/Sections.h> | #include <Kernel/Sections.h> | ||||||
|  | @ -14,23 +14,13 @@ | ||||||
| // Output bytes to kernel debug port 0xE9 (Bochs console). It's very handy.
 | // Output bytes to kernel debug port 0xE9 (Bochs console). It's very handy.
 | ||||||
| #define CONSOLE_OUT_TO_BOCHS_DEBUG_PORT | #define CONSOLE_OUT_TO_BOCHS_DEBUG_PORT | ||||||
| 
 | 
 | ||||||
| static Singleton<ConsoleDevice> s_the; |  | ||||||
| static Kernel::Spinlock g_console_lock; | static Kernel::Spinlock g_console_lock; | ||||||
| 
 | 
 | ||||||
| UNMAP_AFTER_INIT void ConsoleDevice::initialize() | UNMAP_AFTER_INIT NonnullRefPtr<ConsoleDevice> ConsoleDevice::must_create() | ||||||
| { | { | ||||||
|     s_the.ensure_instance(); |     auto device_or_error = DeviceManagement::try_create_device<ConsoleDevice>(); | ||||||
|     s_the->after_inserting(); |     VERIFY(!device_or_error.is_error()); | ||||||
| } |     return device_or_error.release_value(); | ||||||
| 
 |  | ||||||
| ConsoleDevice& ConsoleDevice::the() |  | ||||||
| { |  | ||||||
|     return *s_the; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| bool ConsoleDevice::is_initialized() |  | ||||||
| { |  | ||||||
|     return s_the.is_initialized(); |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| UNMAP_AFTER_INIT ConsoleDevice::ConsoleDevice() | UNMAP_AFTER_INIT ConsoleDevice::ConsoleDevice() | ||||||
|  |  | ||||||
|  | @ -14,12 +14,11 @@ namespace Kernel { | ||||||
| 
 | 
 | ||||||
| class ConsoleDevice final : public CharacterDevice { | class ConsoleDevice final : public CharacterDevice { | ||||||
|     AK_MAKE_ETERNAL |     AK_MAKE_ETERNAL | ||||||
| public: |     friend class DeviceManagement; | ||||||
|     static ConsoleDevice& the(); | 
 | ||||||
|     static void initialize(); | public: | ||||||
|     static bool is_initialized(); |     static NonnullRefPtr<ConsoleDevice> must_create(); | ||||||
| 
 | 
 | ||||||
|     ConsoleDevice(); |  | ||||||
|     virtual ~ConsoleDevice() override; |     virtual ~ConsoleDevice() override; | ||||||
| 
 | 
 | ||||||
|     // ^CharacterDevice
 |     // ^CharacterDevice
 | ||||||
|  | @ -34,6 +33,7 @@ public: | ||||||
|     const CircularQueue<char, 16384>& logbuffer() const { return m_logbuffer; } |     const CircularQueue<char, 16384>& logbuffer() const { return m_logbuffer; } | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|  |     ConsoleDevice(); | ||||||
|     CircularQueue<char, 16384> m_logbuffer; |     CircularQueue<char, 16384> m_logbuffer; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -22,6 +22,12 @@ UNMAP_AFTER_INIT void DeviceManagement::initialize() | ||||||
|     s_the.ensure_instance(); |     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) | UNMAP_AFTER_INIT void DeviceManagement::attach_null_device(NullDevice const& device) | ||||||
| { | { | ||||||
|     m_null_device = device; |     m_null_device = device; | ||||||
|  | @ -85,4 +91,13 @@ NullDevice const& DeviceManagement::null_device() const | ||||||
|     return *m_null_device; |     return *m_null_device; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | ConsoleDevice const& DeviceManagement::console_device() const | ||||||
|  | { | ||||||
|  |     return *m_console_device; | ||||||
|  | } | ||||||
|  | ConsoleDevice& DeviceManagement::console_device() | ||||||
|  | { | ||||||
|  |     return *m_console_device; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -15,6 +15,7 @@ | ||||||
| #include <Kernel/API/KResult.h> | #include <Kernel/API/KResult.h> | ||||||
| #include <Kernel/API/TimePage.h> | #include <Kernel/API/TimePage.h> | ||||||
| #include <Kernel/Arch/x86/RegisterState.h> | #include <Kernel/Arch/x86/RegisterState.h> | ||||||
|  | #include <Kernel/Devices/ConsoleDevice.h> | ||||||
| #include <Kernel/Devices/Device.h> | #include <Kernel/Devices/Device.h> | ||||||
| #include <Kernel/Devices/NullDevice.h> | #include <Kernel/Devices/NullDevice.h> | ||||||
| #include <Kernel/UnixTypes.h> | #include <Kernel/UnixTypes.h> | ||||||
|  | @ -30,14 +31,21 @@ public: | ||||||
|     static DeviceManagement& the(); |     static DeviceManagement& the(); | ||||||
|     void attach_null_device(NullDevice const&); |     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 after_inserting_device(Badge<Device>, Device&); | ||||||
|     void before_device_removal(Badge<Device>, Device&); |     void before_device_removal(Badge<Device>, Device&); | ||||||
| 
 | 
 | ||||||
|     void for_each(Function<void(Device&)>); |     void for_each(Function<void(Device&)>); | ||||||
|     Device* get_device(unsigned major, unsigned minor); |     Device* get_device(unsigned major, unsigned minor); | ||||||
|  | 
 | ||||||
|     NullDevice const& null_device() const; |     NullDevice const& null_device() const; | ||||||
|     NullDevice& null_device(); |     NullDevice& null_device(); | ||||||
| 
 | 
 | ||||||
|  |     ConsoleDevice const& console_device() const; | ||||||
|  |     ConsoleDevice& console_device(); | ||||||
|  | 
 | ||||||
|     template<typename DeviceType, typename... Args> |     template<typename DeviceType, typename... Args> | ||||||
|     static inline KResultOr<NonnullRefPtr<DeviceType>> try_create_device(Args&&... args) |     static inline KResultOr<NonnullRefPtr<DeviceType>> try_create_device(Args&&... args) | ||||||
|     { |     { | ||||||
|  | @ -48,6 +56,7 @@ public: | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|     RefPtr<NullDevice> m_null_device; |     RefPtr<NullDevice> m_null_device; | ||||||
|  |     RefPtr<ConsoleDevice> m_console_device; | ||||||
|     MutexProtected<HashMap<u32, Device*>> m_devices; |     MutexProtected<HashMap<u32, Device*>> m_devices; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -559,8 +559,9 @@ private: | ||||||
|     ProcFSDmesg(); |     ProcFSDmesg(); | ||||||
|     virtual KResult try_generate(KBufferBuilder& builder) override |     virtual KResult try_generate(KBufferBuilder& builder) override | ||||||
|     { |     { | ||||||
|  |         VERIFY(DeviceManagement::the().is_console_device_attached()); | ||||||
|         InterruptDisabler disabler; |         InterruptDisabler disabler; | ||||||
|         for (char ch : ConsoleDevice::the().logbuffer()) { |         for (char ch : DeviceManagement::the().console_device().logbuffer()) { | ||||||
|             TRY(builder.append(ch)); |             TRY(builder.append(ch)); | ||||||
|         } |         } | ||||||
|         return KSuccess; |         return KSuccess; | ||||||
|  |  | ||||||
|  | @ -7,6 +7,7 @@ | ||||||
| #include <AK/Singleton.h> | #include <AK/Singleton.h> | ||||||
| #include <Kernel/CommandLine.h> | #include <Kernel/CommandLine.h> | ||||||
| #include <Kernel/Debug.h> | #include <Kernel/Debug.h> | ||||||
|  | #include <Kernel/Devices/DeviceManagement.h> | ||||||
| #include <Kernel/Graphics/GraphicsManagement.h> | #include <Kernel/Graphics/GraphicsManagement.h> | ||||||
| #include <Kernel/Panic.h> | #include <Kernel/Panic.h> | ||||||
| #include <Kernel/Sections.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++) { |     for (size_t index = 0; index < s_max_virtual_consoles; index++) { | ||||||
|         // FIXME: Better determine the debug TTY we chose...
 |         // FIXME: Better determine the debug TTY we chose...
 | ||||||
|         if (index == 1) { |         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; |             continue; | ||||||
|         } |         } | ||||||
|         m_consoles.append(VirtualConsole::create(index)); |         m_consoles.append(VirtualConsole::create(index)); | ||||||
|  |  | ||||||
|  | @ -186,8 +186,7 @@ extern "C" [[noreturn]] UNMAP_AFTER_INIT void init(BootInfo const& boot_info) | ||||||
|     DeviceManagement::initialize(); |     DeviceManagement::initialize(); | ||||||
|     SysFSComponentRegistry::initialize(); |     SysFSComponentRegistry::initialize(); | ||||||
|     DeviceManagement::the().attach_null_device(*NullDevice::must_initialize()); |     DeviceManagement::the().attach_null_device(*NullDevice::must_initialize()); | ||||||
| 
 |     DeviceManagement::the().attach_console_device(*ConsoleDevice::must_create()); | ||||||
|     ConsoleDevice::initialize(); |  | ||||||
|     s_bsp_processor.initialize(0); |     s_bsp_processor.initialize(0); | ||||||
| 
 | 
 | ||||||
|     CommandLine::initialize(); |     CommandLine::initialize(); | ||||||
|  |  | ||||||
|  | @ -8,6 +8,7 @@ | ||||||
| #include <AK/StringView.h> | #include <AK/StringView.h> | ||||||
| #include <AK/Types.h> | #include <AK/Types.h> | ||||||
| #include <Kernel/Devices/ConsoleDevice.h> | #include <Kernel/Devices/ConsoleDevice.h> | ||||||
|  | #include <Kernel/Devices/DeviceManagement.h> | ||||||
| #include <Kernel/Devices/PCISerialDevice.h> | #include <Kernel/Devices/PCISerialDevice.h> | ||||||
| #include <Kernel/Graphics/GraphicsManagement.h> | #include <Kernel/Graphics/GraphicsManagement.h> | ||||||
| #include <Kernel/IO.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
 |     // It would be bad to reach the assert in ConsoleDevice()::the() and do a stack overflow
 | ||||||
| 
 | 
 | ||||||
|     if (ConsoleDevice::is_initialized()) { |     if (DeviceManagement::the().is_console_device_attached()) { | ||||||
|         ConsoleDevice::the().put_char(ch); |         DeviceManagement::the().console_device().put_char(ch); | ||||||
|     } else { |     } else { | ||||||
|         IO::out8(IO::BOCHS_DEBUG_PORT, ch); |         IO::out8(IO::BOCHS_DEBUG_PORT, ch); | ||||||
|     } |     } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Liav A
						Liav A