Skip to content

Commit

Permalink
Kernel: Disable BootFramebufferConsole when drivers create a new one
Browse files Browse the repository at this point in the history
When GraphicsManagement initializes the drivers we can disable the
bootloader framebuffer console. Right now we don't yet fully destroy
the no longer needed console as it may be in use by another CPU.
  • Loading branch information
tomuta authored and awesomekling committed Feb 4, 2022
1 parent 24f2f3b commit 4c506f9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 17 deletions.
3 changes: 1 addition & 2 deletions Kernel/Graphics/Bochs/GraphicsAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ UNMAP_AFTER_INIT BochsGraphicsAdapter::BochsGraphicsAdapter(PCI::DeviceIdentifie
{
// We assume safe resolution is 1024x768x32
m_framebuffer_console = Graphics::ContiguousFramebufferConsole::initialize(PhysicalAddress(PCI::get_BAR0(pci_device_identifier.address()) & 0xfffffff0), 1024, 768, 1024 * sizeof(u32));
// FIXME: This is a very wrong way to do this...
GraphicsManagement::the().m_console = m_framebuffer_console;
GraphicsManagement::the().set_console(*m_framebuffer_console);

// Note: If we use VirtualBox graphics adapter (which is based on Bochs one), we need to use IO ports
if (pci_device_identifier.hardware_id().vendor_id == 0x80ee && pci_device_identifier.hardware_id().device_id == 0xbeef)
Expand Down
27 changes: 27 additions & 0 deletions Kernel/Graphics/GraphicsManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <Kernel/Bus/PCI/IDs.h>
#include <Kernel/CommandLine.h>
#include <Kernel/Graphics/Bochs/GraphicsAdapter.h>
#include <Kernel/Graphics/Console/BootFramebufferConsole.h>
#include <Kernel/Graphics/GraphicsManagement.h>
#include <Kernel/Graphics/Intel/NativeGraphicsAdapter.h>
#include <Kernel/Graphics/VGACompatibleAdapter.h>
Expand All @@ -22,6 +23,8 @@ namespace Kernel {

static Singleton<GraphicsManagement> s_the;

extern Atomic<Graphics::BootFramebufferConsole*> boot_framebuffer_console;

GraphicsManagement& GraphicsManagement::the()
{
return *s_the;
Expand Down Expand Up @@ -215,6 +218,15 @@ UNMAP_AFTER_INIT bool GraphicsManagement::initialize()
determine_and_initialize_graphics_device(device_identifier);
});

if (!m_console) {
// If no graphics driver was instantiated and we had a bootloader provided
// framebuffer console we can simply re-use it.
if (auto* boot_console = boot_framebuffer_console.load()) {
m_console = *boot_console;
boot_console->unref(); // Drop the leaked reference from Kernel::init()
}
}

if (m_graphics_devices.is_empty()) {
dbgln("No graphics adapter was initialized.");
return false;
Expand All @@ -230,4 +242,19 @@ bool GraphicsManagement::framebuffer_devices_exist() const
}
return false;
}

void GraphicsManagement::set_console(Graphics::Console& console)
{
m_console = console;

if (auto* boot_console = boot_framebuffer_console.exchange(nullptr)) {
// Disable the initial boot framebuffer console permanently
boot_console->disable();
// TODO: Even though we swapped the pointer and disabled the console
// we technically can't safely destroy it as other CPUs might still
// try to use it. Once we solve this problem we can drop the reference
// that we intentionally leaked in Kernel::init().
}
}

}
8 changes: 1 addition & 7 deletions Kernel/Graphics/GraphicsManagement.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@

namespace Kernel {

class BochsGraphicsAdapter;
class IntelNativeGraphicsAdapter;
class VGACompatibleAdapter;
class GraphicsManagement {
friend class BochsGraphicsAdapter;
friend class IntelNativeGraphicsAdapter;
friend class VGACompatibleAdapter;
friend class Graphics::VirtIOGPU::GraphicsAdapter;

public:
static GraphicsManagement& the();
Expand All @@ -42,6 +35,7 @@ class GraphicsManagement {

Spinlock& main_vga_lock() { return m_main_vga_lock; }
RefPtr<Graphics::Console> console() const { return m_console; }
void set_console(Graphics::Console&);

void deactivate_graphical_mode();
void activate_graphical_mode();
Expand Down
3 changes: 1 addition & 2 deletions Kernel/Graphics/Intel/NativeGraphicsAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ IntelNativeGraphicsAdapter::IntelNativeGraphicsAdapter(PCI::Address address)
VERIFY(m_framebuffer_height != 0);
VERIFY(m_framebuffer_width != 0);
m_framebuffer_console = Graphics::ContiguousFramebufferConsole::initialize(framebuffer_address, m_framebuffer_width, m_framebuffer_height, m_framebuffer_pitch);
// FIXME: This is a very wrong way to do this...
GraphicsManagement::the().m_console = m_framebuffer_console;
GraphicsManagement::the().set_console(*m_framebuffer_console);
}

void IntelNativeGraphicsAdapter::enable_vga_plane()
Expand Down
6 changes: 2 additions & 4 deletions Kernel/Graphics/VGACompatibleAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ UNMAP_AFTER_INIT VGACompatibleAdapter::VGACompatibleAdapter(PCI::Address address
: PCI::Device(address)
{
m_framebuffer_console = Graphics::TextModeConsole::initialize(*this);
// FIXME: This is a very wrong way to do this...
GraphicsManagement::the().m_console = m_framebuffer_console;
GraphicsManagement::the().set_console(*m_framebuffer_console);
}

UNMAP_AFTER_INIT VGACompatibleAdapter::VGACompatibleAdapter(PCI::Address address, PhysicalAddress framebuffer_address, size_t framebuffer_width, size_t framebuffer_height, size_t framebuffer_pitch)
Expand All @@ -52,8 +51,7 @@ UNMAP_AFTER_INIT VGACompatibleAdapter::VGACompatibleAdapter(PCI::Address address
, m_framebuffer_pitch(framebuffer_pitch)
{
m_framebuffer_console = Graphics::ContiguousFramebufferConsole::initialize(framebuffer_address, framebuffer_width, framebuffer_height, framebuffer_pitch);
// FIXME: This is a very wrong way to do this...
GraphicsManagement::the().m_console = m_framebuffer_console;
GraphicsManagement::the().set_console(*m_framebuffer_console);
}

void VGACompatibleAdapter::enable_consoles()
Expand Down
3 changes: 1 addition & 2 deletions Kernel/Graphics/VirtIOGPU/GraphicsAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ void GraphicsAdapter::initialize_framebuffer_devices()
create_framebuffer_devices();
m_created_framebuffer_devices = true;

// FIXME: This is a very wrong way to do this...
GraphicsManagement::the().m_console = default_console();
GraphicsManagement::the().set_console(*default_console());
}

void GraphicsAdapter::enable_consoles()
Expand Down

0 comments on commit 4c506f9

Please sign in to comment.