Skip to content

Commit

Permalink
Kernel: Much improved BochsVGA (BXVGA) support.
Browse files Browse the repository at this point in the history
Instead of cowboy-calling the VESA BIOS in the bootloader, find the emulator
VGA adapter by scanning the PCI bus. Then set up the desired video mode by
sending device commands.
  • Loading branch information
awesomekling committed Feb 6, 2019
1 parent e9f6508 commit 731fc5a
Show file tree
Hide file tree
Showing 16 changed files with 298 additions and 114 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
*.creator.user.*
*.files
*.includes
*.cflags
*.cxxflags
Patches
2 changes: 1 addition & 1 deletion Kernel/.bochsrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ optramimage1: file=none
optramimage2: file=none
optramimage3: file=none
optramimage4: file=none
pci: enabled=1, chipset=i440fx
pci: enabled=1, chipset=i440fx, slot1=pcivga
vga: extension=vbe, update_freq=60, realtime=0
cpu: count=1, ips=4000000, model=bx_generic, reset_on_triple_fault=1, cpuid_limit_winnt=0, ignore_bad_msrs=1, mwait_is_nop=0
cpuid: level=6, stepping=3, model=3, family=6, vendor_string="GenuineIntel", brand_string=" Intel(R) Pentium(R) 4 CPU "
Expand Down
68 changes: 68 additions & 0 deletions Kernel/BochsVGADevice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <Kernel/BochsVGADevice.h>
#include <Kernel/IO.h>
#include <Kernel/PCI.h>

#define VBE_DISPI_IOPORT_INDEX 0x01CE
#define VBE_DISPI_IOPORT_DATA 0x01CF

#define VBE_DISPI_INDEX_ID 0x0
#define VBE_DISPI_INDEX_XRES 0x1
#define VBE_DISPI_INDEX_YRES 0x2
#define VBE_DISPI_INDEX_BPP 0x3
#define VBE_DISPI_INDEX_ENABLE 0x4
#define VBE_DISPI_INDEX_BANK 0x5
#define VBE_DISPI_INDEX_VIRT_WIDTH 0x6
#define VBE_DISPI_INDEX_VIRT_HEIGHT 0x7
#define VBE_DISPI_DISABLED 0x00
#define VBE_DISPI_ENABLED 0x01
#define VBE_DISPI_LFB_ENABLED 0x40

static BochsVGADevice* s_the;

BochsVGADevice& BochsVGADevice::the()
{
return *s_the;
}

void BochsVGADevice::initialize_statics()
{
s_the = nullptr;
}

BochsVGADevice::BochsVGADevice()
{
s_the = this;
m_framebuffer_address = PhysicalAddress(find_framebuffer_address());
}

void BochsVGADevice::set_register(word index, word data)
{
IO::out16(VBE_DISPI_IOPORT_INDEX, index);
IO::out16(VBE_DISPI_IOPORT_DATA, data);
}

void BochsVGADevice::set_resolution(int width, int height)
{
set_register(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_DISABLED);
set_register(VBE_DISPI_INDEX_XRES, width);
set_register(VBE_DISPI_INDEX_YRES, height);
set_register(VBE_DISPI_INDEX_VIRT_WIDTH, width);
set_register(VBE_DISPI_INDEX_VIRT_HEIGHT, height);
set_register(VBE_DISPI_INDEX_BPP, 32);
set_register(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_ENABLED | VBE_DISPI_LFB_ENABLED);
set_register(VBE_DISPI_INDEX_BANK, 0);
}

dword BochsVGADevice::find_framebuffer_address()
{
static const PCI::ID bochs_vga_id = { 0x1234, 0x1111 };
static const PCI::ID virtualbox_vga_id = { 0x80ee, 0xbeef };
dword framebuffer_address = 0;
PCI::enumerate_all([&framebuffer_address] (const PCI::Address& address, PCI::ID id) {
if (id == bochs_vga_id || id == virtualbox_vga_id) {
framebuffer_address = PCI::get_BAR0(address) & 0xfffffff0;
kprintf("BochsVGA framebuffer @ PCI %w:%w BAR0=%x\n", id.vendor_id, id.device_id, framebuffer_address);
}
});
return framebuffer_address;
}
25 changes: 25 additions & 0 deletions Kernel/BochsVGADevice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <AK/Types.h>
#include <AK/AKString.h>
#include <Kernel/types.h>

// FIXME: This should be a BlockDevice once we have BlockDevice.

class BochsVGADevice {
AK_MAKE_ETERNAL
public:
static BochsVGADevice& the();
static void initialize_statics();

BochsVGADevice();

PhysicalAddress framebuffer_address() const { return m_framebuffer_address; }
void set_resolution(int width, int height);

private:
void set_register(word index, word value);
dword find_framebuffer_address();

PhysicalAddress m_framebuffer_address;
};
37 changes: 0 additions & 37 deletions Kernel/Boot/boot.asm
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,6 @@ boot:
mov ss, ax
mov sp, 0xffff

; get vesa modes
mov ax, 0x4f00
xor dx, dx
mov es, dx
mov di, 0xc000
mov [es:di], byte 'V'
mov [es:di+1], byte 'B'
mov [es:di+2], byte 'E'
mov [es:di+3], byte '2'
int 0x10
cmp ax, 0x004f
jne fug
cmp [es:di], byte 'V'
jne fug
cmp [es:di+1], byte 'E'
jne fug
cmp [es:di+2], byte 'S'
jne fug
cmp [es:di+3], byte 'A'
jne fug

; get vesa info
mov ax, 0x4f01
mov cx, 0x144
xor dx, dx
mov es, dx
mov di, 0x2000
int 0x10
cmp ax, 0x004f
jne fug

mov ax, 0x4f02
mov bx, 0x4144
int 0x10
cmp ax, 0x004f
jne fug

push cs
pop ds
xor bx, bx
Expand Down
2 changes: 1 addition & 1 deletion Kernel/IO.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ inline word in16(word port)
return value;
}

inline dword in32(dword port)
inline dword in32(word port)
{
dword value;
asm volatile("inl %1, %0":"=a"(value):"Nd"(port));
Expand Down
2 changes: 2 additions & 0 deletions Kernel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ KERNEL_OBJS = \
ELFLoader.o \
KSyms.o \
DevPtsFS.o \
BochsVGADevice.o \
PCI.o \
PS2MouseDevice.o \
GUIEventDevice.o

Expand Down
121 changes: 121 additions & 0 deletions Kernel/PCI.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include <Kernel/PCI.h>
#include <Kernel/IO.h>

#define PCI_VENDOR_ID 0x00 // word
#define PCI_DEVICE_ID 0x02 // word
#define PCI_COMMAND 0x04 // word
#define PCI_STATUS 0x06 // word
#define PCI_REVISION_ID 0x08 // byte
#define PCI_PROG_IF 0x09 // byte
#define PCI_SUBCLASS 0x0a // byte
#define PCI_CLASS 0x0b // byte
#define PCI_CACHE_LINE_SIZE 0x0c // byte
#define PCI_LATENCY_TIMER 0x0d // byte
#define PCI_HEADER_TYPE 0x0e // byte
#define PCI_BIST 0x0f // byte
#define PCI_BAR0 0x10 // dword
#define PCI_BAR1 0x14 // dword
#define PCI_BAR2 0x18 // dword
#define PCI_BAR3 0x1C // dword
#define PCI_BAR4 0x20 // dword
#define PCI_BAR5 0x24 // dword
#define PCI_INTERRUPT_LINE 0x3C // byte
#define PCI_SECONDARY_BUS 0x19 // byte
#define PCI_HEADER_TYPE_DEVICE 0
#define PCI_HEADER_TYPE_BRIDGE 1
#define PCI_TYPE_BRIDGE 0x0604
#define PCI_ADDRESS_PORT 0xCF8
#define PCI_VALUE_PORT 0xCFC
#define PCI_NONE 0xFFFF

namespace PCI {

template<typename T>
T read_field(Address address, dword field)
{
IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field));
if constexpr (sizeof(T) == 4)
return IO::in32(PCI_VALUE_PORT);
if constexpr (sizeof(T) == 2)
return IO::in16(PCI_VALUE_PORT + (field & 2));
if constexpr (sizeof(T) == 1)
return IO::in8(PCI_VALUE_PORT + (field & 3));
}

template<typename T>
void write_field(Address address, dword field, T value)
{
IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field));
if constexpr (sizeof(T) == 4)
IO::out32(PCI_VALUE_PORT, value);
if constexpr (sizeof(T) == 2)
IO::out16(PCI_VALUE_PORT + (field & 2), value);
if constexpr (sizeof(T) == 1)
IO::out8(PCI_VALUE_PORT + (field & 3), value);
}

word read_type(Address address)
{
return (read_field<byte>(address, PCI_CLASS) << 8u) | read_field<byte>(address, PCI_SUBCLASS);
}

void enumerate_bus(int type, byte bus, Function<void(Address, ID)>&);

void enumerate_functions(int type, byte bus, byte slot, byte function, Function<void(Address, ID)>& callback)
{
Address address(bus, slot, function);
if (type == -1 || type == read_type(address))
callback(address, { read_field<word>(address, PCI_VENDOR_ID), read_field<word>(address, PCI_DEVICE_ID) });
if (read_type(address) == PCI_TYPE_BRIDGE) {
byte secondary_bus = read_field<byte>(address, PCI_SECONDARY_BUS);
kprintf("PCI: Found secondary bus: %u\n", secondary_bus);
ASSERT(secondary_bus != bus);
enumerate_bus(type, secondary_bus, callback);
}
}

void enumerate_slot(int type, byte bus, byte slot, Function<void(Address, ID)>& callback)
{
Address address(bus, slot, 0);
if (read_field<word>(address, PCI_VENDOR_ID) == PCI_NONE)
return;
enumerate_functions(type, bus, slot, 0, callback);
if (!(read_field<byte>(address, PCI_HEADER_TYPE) & 0x80))
return;
for (byte function = 1; function < 8; ++function) {
Address address(bus, slot, function);
if (read_field<word>(address, PCI_VENDOR_ID) != PCI_NONE)
enumerate_functions(type, bus, slot, function, callback);
}
}

void enumerate_bus(int type, byte bus, Function<void(Address, ID)>& callback)
{
for (byte slot = 0; slot < 32; ++slot)
enumerate_slot(type, bus, slot, callback);
}

dword get_BAR0(Address address) { return read_field<dword>(address, PCI_BAR0); }
dword get_BAR1(Address address) { return read_field<dword>(address, PCI_BAR1); }
dword get_BAR2(Address address) { return read_field<dword>(address, PCI_BAR2); }
dword get_BAR3(Address address) { return read_field<dword>(address, PCI_BAR3); }
dword get_BAR4(Address address) { return read_field<dword>(address, PCI_BAR4); }
dword get_BAR5(Address address) { return read_field<dword>(address, PCI_BAR5); }

void enumerate_all(Function<void(Address, ID)> callback)
{
// Single PCI host controller.
if ((read_field<byte>(Address(), PCI_HEADER_TYPE) & 0x80) == 0) {
enumerate_bus(-1, 0, callback);
return;
}

// Multiple PCI host controllers.
for (byte function = 0; function < 8; ++function) {
if (read_field<word>(Address(0, 0, function), PCI_VENDOR_ID) == PCI_NONE)
break;
enumerate_bus(-1, function, callback);
}
}

}
48 changes: 48 additions & 0 deletions Kernel/PCI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include <AK/Function.h>
#include <AK/Types.h>

namespace PCI {

struct ID {
word vendor_id { 0 };
word device_id { 0 };

bool operator==(const ID& other) const
{
return vendor_id == other.vendor_id && device_id == other.device_id;
}
};

struct Address {
Address() { }
Address(byte bus, byte slot, byte function) : m_bus(bus), m_slot(slot), m_function(function) { }

bool is_null() const { return !m_bus && !m_slot && !m_function; }
operator bool() const { return !is_null(); }

byte bus() const { return m_bus; }
byte slot() const { return m_slot; }
byte function() const { return m_function; }

dword io_address_for_field(byte field) const
{
return 0x80000000u | (m_bus << 16u) | (m_slot << 11u) | (m_function << 8u) | (field & 0xfc);
}

private:
byte m_bus { 0 };
byte m_slot { 0 };
byte m_function { 0 };
};

void enumerate_all(Function<void(Address, ID)>);
dword get_BAR0(Address);
dword get_BAR1(Address);
dword get_BAR2(Address);
dword get_BAR3(Address);
dword get_BAR4(Address);
dword get_BAR5(Address);

}
Loading

0 comments on commit 731fc5a

Please sign in to comment.