1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 19:37:35 +00:00

Kernel: Reorganize Arch/x86 directory to Arch/x86_64 after i686 removal

No functional change.
This commit is contained in:
Liav A 2022-10-04 13:46:11 +03:00 committed by Andreas Kling
parent 5ff318cf3a
commit 91db482ad3
129 changed files with 482 additions and 1116 deletions

View file

@ -0,0 +1,179 @@
/*
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Platform.h>
#include <Kernel/Arch/x86_64/Hypervisor/BochsDisplayConnector.h>
#include <Kernel/Arch/x86_64/IO.h>
#include <Kernel/Bus/PCI/Access.h>
#include <Kernel/Debug.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Graphics/Bochs/Definitions.h>
#include <Kernel/Graphics/Console/ContiguousFramebufferConsole.h>
#include <Kernel/Graphics/GraphicsManagement.h>
namespace Kernel {
static void set_register_with_io(u16 index, u16 data)
{
IO::out16(VBE_DISPI_IOPORT_INDEX, index);
IO::out16(VBE_DISPI_IOPORT_DATA, data);
}
static u16 get_register_with_io(u16 index)
{
IO::out16(VBE_DISPI_IOPORT_INDEX, index);
return IO::in16(VBE_DISPI_IOPORT_DATA);
}
LockRefPtr<BochsDisplayConnector> BochsDisplayConnector::try_create_for_vga_isa_connector()
{
VERIFY(PCI::Access::is_hardware_disabled());
BochsDisplayConnector::IndexID index_id = get_register_with_io(0);
if (index_id != VBE_DISPI_ID5)
return {};
auto video_ram_64k_chunks_count = get_register_with_io(to_underlying(BochsDISPIRegisters::VIDEO_RAM_64K_CHUNKS_COUNT));
if (video_ram_64k_chunks_count == 0 || video_ram_64k_chunks_count == 0xffff) {
dmesgln("Graphics: Bochs ISA VGA compatible adapter does not indicate amount of VRAM, default to 8 MiB");
video_ram_64k_chunks_count = (8 * MiB) / (64 * KiB);
} else {
dmesgln("Graphics: Bochs ISA VGA compatible adapter indicates {} bytes of VRAM", video_ram_64k_chunks_count * (64 * KiB));
}
// Note: The default physical address for isa-vga framebuffer in QEMU is 0xE0000000.
// Since this is probably hardcoded at other OSes in their guest drivers,
// we can assume this is going to stay the same framebuffer physical address for
// this device and will not be changed in the future.
auto device_or_error = DeviceManagement::try_create_device<BochsDisplayConnector>(PhysicalAddress(0xE0000000), video_ram_64k_chunks_count * (64 * KiB));
VERIFY(!device_or_error.is_error());
auto connector = device_or_error.release_value();
MUST(connector->create_attached_framebuffer_console());
MUST(connector->initialize_edid_for_generic_monitor({}));
return connector;
}
NonnullLockRefPtr<BochsDisplayConnector> BochsDisplayConnector::must_create(PhysicalAddress framebuffer_address, size_t framebuffer_resource_size, bool virtual_box_hardware)
{
auto device_or_error = DeviceManagement::try_create_device<BochsDisplayConnector>(framebuffer_address, framebuffer_resource_size);
VERIFY(!device_or_error.is_error());
auto connector = device_or_error.release_value();
MUST(connector->create_attached_framebuffer_console());
if (virtual_box_hardware)
MUST(connector->initialize_edid_for_generic_monitor(Array<u8, 3> { 'V', 'B', 'X' }));
else
MUST(connector->initialize_edid_for_generic_monitor({}));
return connector;
}
BochsDisplayConnector::BochsDisplayConnector(PhysicalAddress framebuffer_address, size_t framebuffer_resource_size)
: DisplayConnector(framebuffer_address, framebuffer_resource_size, false)
{
}
ErrorOr<void> BochsDisplayConnector::create_attached_framebuffer_console()
{
// We assume safe resolution is 1024x768x32
m_framebuffer_console = Graphics::ContiguousFramebufferConsole::initialize(m_framebuffer_address.value(), 1024, 768, 1024 * sizeof(u32));
GraphicsManagement::the().set_console(*m_framebuffer_console);
return {};
}
BochsDisplayConnector::IndexID BochsDisplayConnector::index_id() const
{
return get_register_with_io(0);
}
void BochsDisplayConnector::enable_console()
{
VERIFY(m_control_lock.is_locked());
VERIFY(m_framebuffer_console);
m_framebuffer_console->enable();
}
void BochsDisplayConnector::disable_console()
{
VERIFY(m_control_lock.is_locked());
VERIFY(m_framebuffer_console);
m_framebuffer_console->disable();
}
ErrorOr<void> BochsDisplayConnector::flush_first_surface()
{
return Error::from_errno(ENOTSUP);
}
ErrorOr<void> BochsDisplayConnector::set_safe_mode_setting()
{
DisplayConnector::ModeSetting safe_mode_set {
.horizontal_stride = 1024 * sizeof(u32),
.pixel_clock_in_khz = 0, // Note: There's no pixel clock in paravirtualized hardware
.horizontal_active = 1024,
.horizontal_front_porch_pixels = 0, // Note: There's no horizontal_front_porch_pixels in paravirtualized hardware
.horizontal_sync_time_pixels = 0, // Note: There's no horizontal_sync_time_pixels in paravirtualized hardware
.horizontal_blank_pixels = 0, // Note: There's no horizontal_blank_pixels in paravirtualized hardware
.vertical_active = 768,
.vertical_front_porch_lines = 0, // Note: There's no vertical_front_porch_lines in paravirtualized hardware
.vertical_sync_time_lines = 0, // Note: There's no vertical_sync_time_lines in paravirtualized hardware
.vertical_blank_lines = 0, // Note: There's no vertical_blank_lines in paravirtualized hardware
.horizontal_offset = 0,
.vertical_offset = 0,
};
return set_mode_setting(safe_mode_set);
}
ErrorOr<void> BochsDisplayConnector::set_mode_setting(ModeSetting const& mode_setting)
{
SpinlockLocker locker(m_modeset_lock);
size_t width = mode_setting.horizontal_active;
size_t height = mode_setting.vertical_active;
dbgln_if(BXVGA_DEBUG, "BochsDisplayConnector resolution registers set to - {}x{}", width, height);
set_register_with_io(to_underlying(BochsDISPIRegisters::ENABLE), 0);
set_register_with_io(to_underlying(BochsDISPIRegisters::XRES), (u16)width);
set_register_with_io(to_underlying(BochsDISPIRegisters::YRES), (u16)height);
set_register_with_io(to_underlying(BochsDISPIRegisters::VIRT_WIDTH), (u16)width);
set_register_with_io(to_underlying(BochsDISPIRegisters::VIRT_HEIGHT), (u16)height * 2);
set_register_with_io(to_underlying(BochsDISPIRegisters::BPP), 32);
set_register_with_io(to_underlying(BochsDISPIRegisters::ENABLE), to_underlying(BochsFramebufferSettings::Enabled) | to_underlying(BochsFramebufferSettings::LinearFramebuffer));
set_register_with_io(to_underlying(BochsDISPIRegisters::BANK), 0);
if ((u16)width != get_register_with_io(to_underlying(BochsDISPIRegisters::XRES)) || (u16)height != get_register_with_io(to_underlying(BochsDISPIRegisters::YRES))) {
return Error::from_errno(ENOTIMPL);
}
auto current_horizontal_active = get_register_with_io(to_underlying(BochsDISPIRegisters::XRES));
auto current_vertical_active = get_register_with_io(to_underlying(BochsDISPIRegisters::YRES));
DisplayConnector::ModeSetting mode_set {
.horizontal_stride = current_horizontal_active * sizeof(u32),
.pixel_clock_in_khz = 0, // Note: There's no pixel clock in paravirtualized hardware
.horizontal_active = current_horizontal_active,
.horizontal_front_porch_pixels = 0, // Note: There's no horizontal_front_porch_pixels in paravirtualized hardware
.horizontal_sync_time_pixels = 0, // Note: There's no horizontal_sync_time_pixels in paravirtualized hardware
.horizontal_blank_pixels = 0, // Note: There's no horizontal_blank_pixels in paravirtualized hardware
.vertical_active = current_vertical_active,
.vertical_front_porch_lines = 0, // Note: There's no vertical_front_porch_lines in paravirtualized hardware
.vertical_sync_time_lines = 0, // Note: There's no vertical_sync_time_lines in paravirtualized hardware
.vertical_blank_lines = 0, // Note: There's no vertical_blank_lines in paravirtualized hardware
.horizontal_offset = 0,
.vertical_offset = 0,
};
m_current_mode_setting = mode_set;
return {};
}
ErrorOr<void> BochsDisplayConnector::set_y_offset(size_t)
{
// Note: Although when using this device on QEMU we can actually set the horizontal and vertical offsets
// with IO ports, this class is meant to be used for plain old Bochs graphics which might not support
// this feature at all.
return Error::from_errno(ENOTIMPL);
}
ErrorOr<void> BochsDisplayConnector::unblank()
{
return Error::from_errno(ENOTIMPL);
}
}

View file

@ -0,0 +1,58 @@
/*
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Try.h>
#include <Kernel/Graphics/Bochs/Definitions.h>
#include <Kernel/Graphics/Console/GenericFramebufferConsole.h>
#include <Kernel/Graphics/DisplayConnector.h>
#include <Kernel/Library/LockRefPtr.h>
#include <Kernel/Locking/Spinlock.h>
#include <Kernel/Memory/TypedMapping.h>
namespace Kernel {
class BochsDisplayConnector
: public DisplayConnector {
friend class BochsGraphicsAdapter;
friend class DeviceManagement;
friend class GraphicsManagement;
public:
AK_TYPEDEF_DISTINCT_ORDERED_ID(u16, IndexID);
static LockRefPtr<BochsDisplayConnector> try_create_for_vga_isa_connector();
static NonnullLockRefPtr<BochsDisplayConnector> must_create(PhysicalAddress framebuffer_address, size_t framebuffer_resource_size, bool virtual_box_hardware);
private:
IndexID index_id() const;
ErrorOr<void> create_attached_framebuffer_console();
BochsDisplayConnector(PhysicalAddress framebuffer_address, size_t framebuffer_resource_size);
virtual bool mutable_mode_setting_capable() const override final { return true; }
virtual bool double_framebuffering_capable() const override { return false; }
virtual ErrorOr<void> set_mode_setting(ModeSetting const&) override;
virtual ErrorOr<void> set_safe_mode_setting() override final;
virtual ErrorOr<void> set_y_offset(size_t y) override;
virtual ErrorOr<void> unblank() override;
virtual bool partial_flush_support() const override final { return false; }
virtual bool flush_support() const override final { return false; }
// Note: Paravirtualized hardware doesn't require a defined refresh rate for modesetting.
virtual bool refresh_rate_support() const override final { return false; }
virtual ErrorOr<void> flush_first_surface() override final;
virtual void enable_console() override final;
virtual void disable_console() override final;
LockRefPtr<Graphics::GenericFramebufferConsole> m_framebuffer_console;
};
}

View file

@ -0,0 +1,247 @@
/*
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/OwnPtr.h>
#include <AK/Singleton.h>
#include <Kernel/API/MousePacket.h>
#include <Kernel/Arch/x86_64/Hypervisor/VMWareBackdoor.h>
#include <Kernel/CommandLine.h>
#include <Kernel/Debug.h>
#include <Kernel/InterruptDisabler.h>
#include <Kernel/Sections.h>
namespace Kernel {
#define VMWARE_CMD_GETVERSION 0x0a
#define VMMOUSE_READ_ID 0x45414552
#define VMMOUSE_DISABLE 0x000000f5
#define VMMOUSE_REQUEST_RELATIVE 0x4c455252
#define VMMOUSE_REQUEST_ABSOLUTE 0x53424152
#define VMMOUSE_QEMU_VERSION 0x3442554a
#define VMMOUSE_LEFT_CLICK 0x20
#define VMMOUSE_RIGHT_CLICK 0x10
#define VMMOUSE_MIDDLE_CLICK 0x08
#define VMWARE_MAGIC 0x564D5868
#define VMWARE_PORT 0x5658
#define VMWARE_PORT_HIGHBANDWIDTH 0x5659
inline void vmware_out(VMWareCommand& command)
{
command.magic = VMWARE_MAGIC;
command.port = VMWARE_PORT;
command.si = 0;
command.di = 0;
asm volatile("in %%dx, %0"
: "+a"(command.ax), "+b"(command.bx), "+c"(command.cx), "+d"(command.dx), "+S"(command.si), "+D"(command.di));
}
inline void vmware_high_bandwidth_send(VMWareCommand& command)
{
command.magic = VMWARE_MAGIC;
command.port = VMWARE_PORT_HIGHBANDWIDTH;
asm volatile("cld; rep; outsb"
: "+a"(command.ax), "+b"(command.bx), "+c"(command.cx), "+d"(command.dx), "+S"(command.si), "+D"(command.di));
}
inline void vmware_high_bandwidth_get(VMWareCommand& command)
{
command.magic = VMWARE_MAGIC;
command.port = VMWARE_PORT_HIGHBANDWIDTH;
asm volatile("cld; rep; insb"
: "+a"(command.ax), "+b"(command.bx), "+c"(command.cx), "+d"(command.dx), "+S"(command.si), "+D"(command.di));
}
class VMWareBackdoorDetector {
public:
VMWareBackdoorDetector()
{
if (detect_presence())
m_backdoor = adopt_nonnull_own_or_enomem(new (nothrow) VMWareBackdoor()).release_value_but_fixme_should_propagate_errors();
}
VMWareBackdoor* get_instance()
{
return m_backdoor.ptr();
}
private:
static bool detect_presence()
{
VMWareCommand command;
command.bx = ~VMWARE_MAGIC;
command.command = VMWARE_CMD_GETVERSION;
vmware_out(command);
if (command.bx != VMWARE_MAGIC || command.ax == 0xFFFFFFFF)
return false;
return true;
}
OwnPtr<VMWareBackdoor> m_backdoor;
};
static Singleton<VMWareBackdoorDetector> s_vmware_backdoor;
VMWareBackdoor* VMWareBackdoor::the()
{
return s_vmware_backdoor->get_instance();
}
UNMAP_AFTER_INIT VMWareBackdoor::VMWareBackdoor()
{
if (kernel_command_line().is_vmmouse_enabled())
enable_absolute_vmmouse();
}
bool VMWareBackdoor::detect_vmmouse()
{
VMWareCommand command;
command.bx = VMMOUSE_READ_ID;
command.command = VMMOUSE_COMMAND;
send(command);
command.bx = 1;
command.command = VMMOUSE_DATA;
send(command);
if (command.ax != VMMOUSE_QEMU_VERSION)
return false;
return true;
}
bool VMWareBackdoor::vmmouse_is_absolute() const
{
return m_vmmouse_absolute;
}
void VMWareBackdoor::enable_absolute_vmmouse()
{
InterruptDisabler disabler;
if (!detect_vmmouse())
return;
dmesgln("VMWareBackdoor: Enabling absolute mouse mode");
VMWareCommand command;
command.bx = 0;
command.command = VMMOUSE_STATUS;
send(command);
if (command.ax == 0xFFFF0000) {
dmesgln("VMWareBackdoor: VMMOUSE_STATUS got bad status");
return;
}
// Enable absolute vmmouse
command.bx = VMMOUSE_REQUEST_ABSOLUTE;
command.command = VMMOUSE_COMMAND;
send(command);
m_vmmouse_absolute = true;
}
void VMWareBackdoor::disable_absolute_vmmouse()
{
InterruptDisabler disabler;
VMWareCommand command;
command.bx = VMMOUSE_REQUEST_RELATIVE;
command.command = VMMOUSE_COMMAND;
send(command);
m_vmmouse_absolute = false;
}
void VMWareBackdoor::send_high_bandwidth(VMWareCommand& command)
{
vmware_high_bandwidth_send(command);
dbgln_if(VMWARE_BACKDOOR_DEBUG, "VMWareBackdoor Command High bandwidth Send Results: EAX {:#x} EBX {:#x} ECX {:#x} EDX {:#x}",
command.ax,
command.bx,
command.cx,
command.dx);
}
void VMWareBackdoor::get_high_bandwidth(VMWareCommand& command)
{
vmware_high_bandwidth_get(command);
dbgln_if(VMWARE_BACKDOOR_DEBUG, "VMWareBackdoor Command High bandwidth Get Results: EAX {:#x} EBX {:#x} ECX {:#x} EDX {:#x}",
command.ax,
command.bx,
command.cx,
command.dx);
}
void VMWareBackdoor::send(VMWareCommand& command)
{
vmware_out(command);
dbgln_if(VMWARE_BACKDOOR_DEBUG, "VMWareBackdoor Command Send Results: EAX {:#x} EBX {:#x} ECX {:#x} EDX {:#x}",
command.ax,
command.bx,
command.cx,
command.dx);
}
u16 VMWareBackdoor::read_mouse_status_queue_size()
{
VMWareCommand command;
command.bx = 0;
command.command = VMMOUSE_STATUS;
send(command);
if (command.ax == 0xFFFF0000) {
dbgln_if(PS2MOUSE_DEBUG, "PS2MouseDevice: Resetting VMWare mouse");
disable_absolute_vmmouse();
enable_absolute_vmmouse();
return 0;
}
return command.ax & 0xFFFF;
}
MousePacket VMWareBackdoor::receive_mouse_packet()
{
VMWareCommand command;
command.size = 4;
command.command = VMMOUSE_DATA;
send(command);
int buttons = (command.ax & 0xFFFF);
int x = command.bx;
int y = command.cx;
int z = static_cast<i8>(command.dx); // signed 8 bit value only!
int w = 0;
// horizontal scroll is reported as +-2 by qemu
// FIXME: Scroll only functions correctly when the sign is flipped there
if (z == 2) {
w = -1;
z = 0;
} else if (z == -2) {
w = 1;
z = 0;
}
if constexpr (PS2MOUSE_DEBUG) {
dbgln("Absolute Mouse: Buttons {:x}", buttons);
dbgln("Mouse: x={}, y={}, z={}, w={}", x, y, z, w);
}
MousePacket packet;
packet.x = x;
packet.y = y;
packet.z = z;
packet.w = w;
if (buttons & VMMOUSE_LEFT_CLICK)
packet.buttons |= MousePacket::LeftButton;
if (buttons & VMMOUSE_RIGHT_CLICK)
packet.buttons |= MousePacket::RightButton;
if (buttons & VMMOUSE_MIDDLE_CLICK)
packet.buttons |= MousePacket::MiddleButton;
packet.is_relative = false;
return packet;
}
}

View file

@ -0,0 +1,62 @@
/*
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
#include <AK/kmalloc.h>
#include <Kernel/API/MousePacket.h>
namespace Kernel {
#define VMMOUSE_GETVERSION 10
#define VMMOUSE_DATA 39
#define VMMOUSE_STATUS 40
#define VMMOUSE_COMMAND 41
struct VMWareCommand {
union {
u32 ax;
u32 magic;
};
union {
u32 bx;
u32 size;
};
union {
u32 cx;
u32 command;
};
union {
u32 dx;
u32 port;
};
u32 si;
u32 di;
};
class VMWareBackdoor {
public:
VMWareBackdoor();
static VMWareBackdoor* the();
bool vmmouse_is_absolute() const;
void enable_absolute_vmmouse();
void disable_absolute_vmmouse();
void send(VMWareCommand& command);
u16 read_mouse_status_queue_size();
MousePacket receive_mouse_packet();
private:
void send_high_bandwidth(VMWareCommand& command);
void get_high_bandwidth(VMWareCommand& command);
bool detect_vmmouse();
bool m_vmmouse_absolute { false };
};
}