Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ practices while remaining approachable to contributors and learners alike.

### Internal Driver Framework

- [ ] Device objects
- [ ] Driver registration
- [ ] Driver lifecycle
- [ ] MMIO port I/O helpers
- [ ] Block device abstraction
- [ ] Move to Limine 6 barely (APIC or x2APIC mapped to legacy PIC)
- [x] Device objects
- [x] Driver registration
- [x] Driver lifecycle
- [x] MMIO port I/O helpers
- [x] Block device abstraction
- [x] Move to Limine 6 barely (APIC or x2APIC mapped to legacy PIC)

### Hardware discovery

Expand Down
43 changes: 26 additions & 17 deletions include/core/apic.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,37 @@
#include "../types.h"

namespace lapic {
inline u64 rdmsr(u32 msr) {
u32 lo, hi;
asm volatile("rdmsr" : "=a"(lo), "=d"(hi) : "c"(msr));
return (static_cast<u64>(hi) << 32) | lo;
}
bool init();
void eoi();

inline void wrmsr(u32 msr, u64 value) {
u32 lo = value & 0xffffffff;
u32 hi = value >> 32;
u32 id();
}

namespace ioapic {
bool init(physAddr physicalBase);

u32 read(u8 reg);
void write(u8 reg, u32 value);

void redirectIRQ(u8 irq, u8 vector, u8 lapicId);
void maskIRQ(u8 irq);
void unmaskIRQ(u8 irq);
}

asm volatile("wrmsr" : : "c"(msr), "a"(lo), "d"(hi));
}
namespace interruptController {
enum class Backend {
PIC,
APIC
};

void initBase();
void write(u32 reg, u32 value);
u32 read(u32 reg);
void initPIC();
void initAPICCompat();

constexpr u32 LAPIC_SRV = 0xF0;
void enable();
void enableIRQ(u8 irq);
void disableIRQ(u8 irq);
void eoi(u8 irq);

constexpr u32 LAPIC_LVT_LINT0 = 0x350;
void enableLegacyMode();
Backend currentBackend();
}

#endif //AVERY_APIC_H
5 changes: 4 additions & 1 deletion include/core/irq.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ namespace irq {

void installHandler(int irq, IRQHandler);
void uninstallHandler(int irq);
void remap();

void enable(int irq);
void disable(int irq);
void eoi(int irq);
}

namespace core {
Expand Down
23 changes: 23 additions & 0 deletions include/core/pic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* pic.h
* As part of the Avery project
* Created by Max Van den Eynde in 2026
* --------------------------------------
* Description: Handler for the legacy PIC
* Copyright (c) 2026 Max Van den Eynde
*/

#ifndef AVERY_PIC_H
#define AVERY_PIC_H
#include "../types.h"

namespace pic {
void remap();
void maskAll();

void enableIRQ(u8 irq);
void disableIRQ(u8 irq);
void eoi(u8 irq);
}

#endif //AVERY_PIC_H
5 changes: 4 additions & 1 deletion include/core/systems.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
#ifndef AVERY_SYSTEMS_H
#define AVERY_SYSTEMS_H

struct limine_memmap_request;


namespace core {
void initSystems();
void initSystems(volatile struct limine_memmap_request& request);
}

#endif //AVERY_SYSTEMS_H
177 changes: 171 additions & 6 deletions include/drivers/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,183 @@
#define AVERY_DRIVER_H
#include "../types.h"

using DriverInitFunction = void (*)();
enum class DeviceType {
Unknown,
Block,
Character,
Timer,
Input,
PCI,
};

enum class DriverState {
Created,
Registered,
Probing,
Active,
Failed,
Stopping,
};

class Driver;

class Device {
public:
virtual ~Device() = default;

Device(string name, DeviceType type) : deviceName(name), deviceType(type) {
}

string name() const { return this->deviceName; }
DeviceType type() const { return this->deviceType; }

Driver* driver = nullptr;
Device* parent = nullptr;

private:
string deviceName;
DeviceType deviceType;
};

class BlockDevice : public Device {
public:
BlockDevice(string name, u64 blocks, u32 blockSize)
: Device(name, DeviceType::Block), deviceBlocks(blocks), deviceBlockSize(blockSize) {
}

virtual bool readBlocks(u64 lba, u32 count, void* buffer) = 0;
virtual bool writeBlocks(u64 lba, u32 count, const void* buffer) = 0;

u64 blockCount() const { return deviceBlocks; }
u64 blockSize() const { return deviceBlockSize; }

private:
u64 deviceBlocks;
u32 deviceBlockSize;
};

class CharacterDevice : public Device {
public:
CharacterDevice(string name) : Device(name, DeviceType::Character) {
}

virtual int read(u8* buffer, usize size) = 0;
virtual int write(const u8* buffer, usize size) = 0;
};

enum class DriverType {
Generic
class PCIDevice : public Device {
public:
PCIDevice(string name, DeviceType type) : Device(name, type) {
}
};

class Driver {
public:
string name;
DriverType type;
Driver() = default;
virtual ~Driver() = default;

Driver(const Driver&) = delete;
Driver& operator=(const Driver&) = delete;

virtual string name() const = 0;

virtual bool probe(Device& device) = 0;
virtual bool start(Device& device) = 0;
virtual bool stop(Device& device) = 0;

DriverState state() const {
return driverState;
}

void setState(DriverState state) {
driverState = state;
}

private:
DriverState driverState = DriverState::Created;
};

class DeviceManager {
public:
static bool registerDevice(Device* device);
static void unregisterDevice(Device* device);

static Device* deviceAt(usize index);
static usize deviceCount();

private:
static constexpr usize MaxDevices = 256;
static Device* devices[MaxDevices];
static usize s_deviceCount;
};

class DriverManager {
public:
static bool registerDriver(Driver* driver);
static void unregisterDriver(Driver* driver);

DriverInitFunction init;
static bool tryBind(Device& device);
static bool tryBind(Driver& driver);
static void unbind(Device& device);

private:
static constexpr usize MaxDrivers = 128;
static Driver* drivers[MaxDrivers];
static usize driverCount;
};

namespace drivers {
void init();
}

namespace mmio {
static constexpr u64 PageSize = 0x1000;

class Interface {
public:
Interface(physAddr physical, usize size);
~Interface();

Interface(const Interface&) = delete;
Interface& operator=(const Interface&) = delete;

template <typename T>
requires ByteNumber<T>
T read(uptr offset) const {
if (!base) {
return T{};
}

return *reinterpret_cast<volatile T*>(
base + offset
);
}

template <typename T>
requires ByteNumber<T>
void write(uptr offset, T value) {
if (!base) {
return;
}

*reinterpret_cast<volatile T*>(
base + offset
) = value;
}

uptr address() const {
return base;
}

bool isValid() const {
return base != 0;
}

private:
uptr base = 0;
virtAddr mappingBase = 0;
usize mappedSize = 0;
};
}

#endif //AVERY_DRIVER_H
3 changes: 3 additions & 0 deletions include/drivers/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef AVERY_KEYBOARD_H
#define AVERY_KEYBOARD_H
#include "../core/isr.h"
#include "driver.h"

namespace keyboard {
extern const unsigned char es[128];
Expand All @@ -20,6 +21,8 @@ namespace keyboard {
char getChar();
void handler(isr::Registers* regs);
void init();
bool registerDriver();
bool registerDevice();
}

#endif //AVERY_KEYBOARD_H
3 changes: 3 additions & 0 deletions include/drivers/pit.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
#ifndef AVERY_PIT_H
#define AVERY_PIT_H
#include "../core/isr.h"
#include "driver.h"

extern "C" void time_handler(isr::Registers* regs);

namespace time {
u64 getUptime();
void wait(u64 ticksMs);
bool registerDriver();
bool registerDevice();
}

namespace core {
Expand Down
2 changes: 1 addition & 1 deletion include/kernel/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace memory {
u64 getUsedMemory();
u64 getFreeMemory();

struct HeapBlock {
struct alignas(16) HeapBlock {
u64 magic;
u64 size;
bool free;
Expand Down
3 changes: 3 additions & 0 deletions include/kernel/memory/virtualMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ namespace vmm {
constexpr u64 FlagPresent = 1ull << 0;
constexpr u64 FlagWritable = 1ull << 1;
constexpr u64 FlagUser = 1ull << 2;
constexpr u64 FlagWriteThrough = 1ull << 3;
constexpr u64 FlagCacheDisable = 1ull << 4;
constexpr u64 FlagGlobal = 1ull << 8;
constexpr u64 FlagNX = 1ull << 63;

void init();
Expand Down
3 changes: 2 additions & 1 deletion include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ T alignUp(T x, T a) {
template <typename T>
requires ByteNumber<T>
T alignDown(T x, T a) {
return (x + a - 1) & ~(a - 1);
return x & ~(a - 1);
}

template <typename T>
Expand Down Expand Up @@ -586,4 +586,5 @@ class Vector {
}
};


#endif //AVERY_TYPES_H
Loading
Loading