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
45 changes: 34 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,20 @@ SRCS_C := user/hello_musl.c user/hello_dyn.c user/fb_test.c user/evdev_smoke.c
user/unix_echo_server.c user/unix_echo_client.c user/unix_sock_smoke.c \
user/test_milo.c

# Include architecture and core modules
# Include architecture, core modules, and modular DDK drivers
MILO_DDK_DIR := .
include arch/$(ARCH)/arch.mk
include kernel/kernel.mk
include drivers/ddk.mk

DRIVER_SRCS := drivers/dummy/DummyDriver.cpp
DRIVER_OBJS := $(patsubst %.cpp, $(BUILD_DIR)/%.o, $(DRIVER_SRCS))
DRIVER_DEPS := $(DRIVER_OBJS:.o=.d)

# Map sources to build output objects
OBJS := $(patsubst %.cpp, $(BUILD_DIR)/%.o, $(SRCS_CXX)) \
$(patsubst %.s, $(BUILD_DIR)/%.o, $(SRCS_ASM))
DEPS := $(OBJS:.o=.d)
DEPS := $(OBJS:.o=.d) $(DRIVER_DEPS)

# --- Host Unit Testing Toolchain (snitch & gcovr pre-installed in devcontainer) ---
HOST_CXX ?= g++
Expand All @@ -54,7 +60,7 @@ HOST_CXXFLAGS := -std=c++20 -O1 -g -Wall -Wextra -Werror -Wpedantic \
-Wshadow -Wconversion -Wsign-conversion -Wnull-dereference \
-fsanitize=address,undefined -fno-omit-frame-pointer -MMD -MP
HOST_LDFLAGS := -fsanitize=address,undefined -L/usr/local/lib -lsnitch
HOST_INCLUDES := -Ikernel -Iarch/$(ARCH) -Itests/host
HOST_INCLUDES := -Iinclude -Ikernel -Iarch/$(ARCH) -Itests/host

HOST_COV_CXXFLAGS := -std=c++20 -O0 -g --coverage -Wall -Wextra -Werror -Wpedantic \
-Wshadow -Wconversion -Wsign-conversion -Wnull-dereference \
Expand All @@ -65,6 +71,9 @@ HOST_ONLY_TEST_SRCS := \
tests/host/mocks/host_hal.cpp \
tests/host/af_unix_test.cpp \
tests/host/distro_abi_test.cpp \
tests/host/ddk_dummy_test.cpp \
tests/host/ddk_kernel_services_test.cpp \
drivers/dummy/DummyDriver.cpp \
tests/host/socket_syscall_test.cpp \
tests/host/gicv2_test.cpp \
tests/host/pl011_uart_test.cpp \
Expand All @@ -91,6 +100,7 @@ HOST_ONLY_TEST_SRCS := \

HOST_KERNEL_SRCS := \
kernel/core/log.cpp \
kernel/ddk/KernelPlatformServices.cpp \
kernel/boot/fdt.cpp \
kernel/drivers/irq/gicv2.cpp \
kernel/drivers/uart/pl011.cpp \
Expand Down Expand Up @@ -142,26 +152,39 @@ HOST_COV_DEPS := $(HOST_COV_OBJS:.o=.d)
HOST_COV_BIN := $(HOST_COV_DIR)/kernel_host_coverage

.PHONY: all clean distclean clean-tests run debug gdb lint format format-check lint-report license-check quality \
test-host compile_commands.json ai-dump setup-musl setup-busybox setup-bash setup-hooks loc
test-host compile_commands.json ai-dump setup-musl setup-busybox setup-bash setup-hooks loc export-ddk

all: $(BUILD_DIR)/$(TARGET) $(INITRD)

DDK_EXPORT_DIR := $(BUILD_DIR)/ddk
export-ddk:
@rm -rf $(DDK_EXPORT_DIR)
@mkdir -p $(DDK_EXPORT_DIR)/include/ddk
@cp -r include/ddk/. $(DDK_EXPORT_DIR)/include/ddk/
@cp drivers/ddk.mk $(DDK_EXPORT_DIR)/ddk.mk
@echo "[DDK] Exported standalone SDK to $(DDK_EXPORT_DIR)/"

arch/memory_config.hpp: arch/$(ARCH)/memory_config.hpp
@mkdir -p arch
cp $< $@

# Link as ELF executable first
$(BUILD_DIR)/kernel.elf: $(OBJS)
$(BUILD_DIR)/kernel.elf: $(OBJS) $(DRIVER_OBJS)
@mkdir -p $(dir $@)
$(LD) $(LDFLAGS) -o $@ $^

# Extract flat binary image with objcopy for QEMU Linux bootloader
$(BUILD_DIR)/$(TARGET): $(BUILD_DIR)/kernel.elf
$(OBJCOPY) -O binary $< $@

# Modular DDK drivers compile strictly with -I$(MILO_DDK_DIR)/include (no -Ikernel or -Iarch)
$(BUILD_DIR)/drivers/%.o: drivers/%.cpp
@mkdir -p $(dir $@)
$(DDK_CXX) $(DDK_CXXFLAGS) -c $< -o $@

$(BUILD_DIR)/%.o: %.cpp arch/memory_config.hpp
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) -Ikernel -Iarch/$(ARCH) -c $< -o $@
$(CXX) $(CXXFLAGS) -Iinclude -Ikernel -Iarch/$(ARCH) -c $< -o $@

$(BUILD_DIR)/%.o: %.s
@mkdir -p $(dir $@)
Expand Down Expand Up @@ -397,7 +420,7 @@ setup-bash: $(BASH_CPIO)
-include $(HOST_COV_DEPS)

clean:
rm -rf $(BUILD_DIR)/kernel $(BUILD_DIR)/arch \
rm -rf $(BUILD_DIR)/kernel $(BUILD_DIR)/arch $(BUILD_DIR)/drivers $(DDK_EXPORT_DIR) \
$(BUILD_DIR)/$(TARGET) $(BUILD_DIR)/kernel.elf \
$(HOST_BUILD_DIR) $(HOST_COV_DIR) $(REPORTS_DIR) \
$(BUSYBOX_ROOTFS_DIR) $(BASH_ROOTFS_DIR) arch/memory_config.hpp
Expand Down Expand Up @@ -446,7 +469,7 @@ gdb: $(BUILD_DIR)/kernel.elf
$(GDB) $(BUILD_DIR)/kernel.elf -ex "target remote localhost:1234" -ex "layout split"

# Find all project source/header files for formatting/linting targets
HDRS := $(shell find kernel arch tests -name '*.hpp' -o -name '*.h' 2>/dev/null)
HDRS := $(shell find include kernel arch tests drivers -name '*.hpp' -o -name '*.h' 2>/dev/null)
TEST_FORMAT_SRCS := $(shell find tests -name '*.cpp' 2>/dev/null)

# --- Compilation Database Generation (incremental, without clean rebuilds) ---
Expand All @@ -455,7 +478,7 @@ compile_commands.json: Makefile kernel/kernel.mk arch/$(ARCH)/arch.mk arch/memor
@python3 scripts/generate_compile_commands.py \
--cxx "$(CXX)" \
--cxxflags "$(CXXFLAGS)" \
--kernel-includes "-Ikernel -Iarch/$(ARCH)" \
--kernel-includes "-Iinclude -Ikernel -Iarch/$(ARCH)" \
--build-dir "$(BUILD_DIR)" \
--kernel-srcs $(SRCS_CXX) \
--host-cxx "$(HOST_CXX)" \
Expand All @@ -472,11 +495,11 @@ lint: compile_commands.json

format:
@echo "[FORMAT] Formatting sources with clang-format..."
clang-format -i $(SRCS_CXX) $(TEST_FORMAT_SRCS) $(SRCS_C) $(HDRS)
clang-format -i $(SRCS_CXX) $(DRIVER_SRCS) $(TEST_FORMAT_SRCS) $(SRCS_C) $(HDRS)

format-check:
@echo "[FORMAT-CHECK] Checking source formatting..."
clang-format --dry-run --Werror $(SRCS_CXX) $(TEST_FORMAT_SRCS) $(SRCS_C) $(HDRS)
clang-format --dry-run --Werror $(SRCS_CXX) $(DRIVER_SRCS) $(TEST_FORMAT_SRCS) $(SRCS_C) $(HDRS)

lint-report: compile_commands.json
@python3 scripts/generate_quality_report.py -j $(NPROC)
Expand Down
17 changes: 17 additions & 0 deletions drivers/ddk.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (c) 2026 Marco Buschini <marco.buschini@gmail.com>

# Reusable DDK build rules for both in-tree (drivers/) and out-of-tree modules.
# Strict boundary: only -I$(MILO_DDK_DIR)/include is exposed; never -Ikernel or -Iarch.

MILO_DDK_DIR ?= .
DDK_ARCH ?= aarch64-linux-gnu
DDK_CXX ?= $(DDK_ARCH)-g++
DDK_AR ?= $(DDK_ARCH)-ar

DDK_CXXFLAGS := -std=c++20 -ffreestanding -fsized-deallocation -O2 -Wall -Wextra \
-fno-use-cxa-atexit -Werror -Wpedantic -Wshadow -Wconversion \
-Wsign-conversion -Wnull-dereference -fno-exceptions -fno-rtti \
-fno-threadsafe-statics -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 \
-mcpu=cortex-a53 -mno-outline-atomics -MMD -MP -g \
-I$(MILO_DDK_DIR)/include
132 changes: 132 additions & 0 deletions drivers/dummy/DummyDriver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (c) 2026 Marco Buschini <marco.buschini@gmail.com>

#include "DummyDriver.hpp"

#include <new>

namespace Drivers::Dummy {

namespace {

bool str_eq(const char* a, const char* b) noexcept {
if (a == nullptr || b == nullptr) {
return false;
}
while (*a != '\0' && *a == *b) {
++a;
++b;
}
return *a == *b;
}

alignas(DummyDriver) unsigned char g_dummy_storage[sizeof(DummyDriver)]{};
bool g_dummy_in_use = false;

Kernel::DDK::Driver* create_dummy() noexcept {
if (g_dummy_in_use) {
return nullptr;
}
g_dummy_in_use = true;
return new (g_dummy_storage) DummyDriver();
}

void destroy_dummy(Kernel::DDK::Driver* drv) noexcept {
if (drv == nullptr || !g_dummy_in_use) {
return;
}
drv->~Driver();
g_dummy_in_use = false;
}

const Kernel::DDK::DriverMetadata kDummyMetadata{
.ddk_abi_version = Kernel::DDK::DDK_ABI_VERSION,
.name = "milo-dummy",
.create_instance = create_dummy,
.destroy_instance = destroy_dummy,
};

} // namespace

bool DummyDriver::probe(const Kernel::DDK::DeviceDescriptor& dev) noexcept {
if (dev.compatible != nullptr && str_eq(dev.compatible, kCompatible)) {
return true;
}
return dev.vendor_id == kDefaultVendorId && dev.device_id == kDefaultDeviceId;
}

Kernel::DDK::Status DummyDriver::init(Kernel::DDK::PlatformServices& services,
const Kernel::DDK::DeviceDescriptor& dev) noexcept {
if (m_initialized) {
return Kernel::DDK::Status::AlreadyInitialized;
}

m_services = &services;

if (dev.mmio_base != 0U && dev.mmio_size != 0U) {
auto mmio_res = services.map_mmio(dev.mmio_base, dev.mmio_size);
if (mmio_res.is_err()) {
shutdown();
return mmio_res.error();
}
m_mmio = static_cast<Kernel::DDK::MmioRegion&&>(mmio_res.value());
}

auto dma_res = services.alloc_dma(4096U, 4096U);
if (dma_res.is_err()) {
shutdown();
return dma_res.error();
}
m_dma = static_cast<Kernel::DDK::DmaBuffer&&>(dma_res.value());
m_dma.zero();

auto evt_res = services.create_event();
if (evt_res.is_err()) {
shutdown();
return evt_res.error();
}
m_event = static_cast<Kernel::DDK::EventHandle&&>(evt_res.value());

if (dev.irq != 0U) {
auto irq_res = services.register_irq(dev.irq, &DummyDriver::on_irq, this);
if (irq_res.is_err()) {
shutdown();
return irq_res.error();
}
m_irq = static_cast<Kernel::DDK::IrqHandle&&>(irq_res.value());
}

m_initialized = true;
services.log(Kernel::DDK::LogLevel::Info, "dummy", "DummyDriver initialized.");
return Kernel::DDK::Status::Ok;
}

void DummyDriver::shutdown() noexcept {
if (m_services != nullptr && m_initialized) {
m_services->log(Kernel::DDK::LogLevel::Info, "dummy", "DummyDriver shutting down.");
}
m_irq.reset();
m_event.reset();
m_dma.reset();
m_mmio.reset();
m_irq_count = 0U;
m_initialized = false;
m_services = nullptr;
}

void DummyDriver::on_irq(uint32_t /*irq*/, void* ctx) noexcept {
auto* const self = static_cast<DummyDriver*>(ctx);
if (self == nullptr || self->m_services == nullptr) {
return;
}
auto lock = self->m_services->acquire_irq_lock();
self->m_irq_count++;
self->m_event.wake_all();
lock.unlock();
}

const Kernel::DDK::DriverMetadata& DummyDriver::metadata() noexcept {
return kDummyMetadata;
}

} // namespace Drivers::Dummy
55 changes: 55 additions & 0 deletions drivers/dummy/DummyDriver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (c) 2026 Marco Buschini <marco.buschini@gmail.com>

#pragma once

#include "ddk/Bus.hpp"
#include "ddk/Driver.hpp"
#include "ddk/Resources.hpp"
#include "ddk/Services.hpp"
#include "ddk/Status.hpp"

#include <cstdint>

namespace Drivers::Dummy {

class DummyDriver final : public Kernel::DDK::Driver {
public:
static constexpr const char* kCompatible = "milo,dummy-device";
static constexpr uint32_t kDefaultVendorId = 0x1AF4U;
static constexpr uint32_t kDefaultDeviceId = 0xD001U;

DummyDriver() noexcept = default;
~DummyDriver() noexcept override { shutdown(); }

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

[[nodiscard]] bool probe(const Kernel::DDK::DeviceDescriptor& dev) noexcept override;
[[nodiscard]] Kernel::DDK::Status
init(Kernel::DDK::PlatformServices& services,
const Kernel::DDK::DeviceDescriptor& dev) noexcept override;
void shutdown() noexcept override;

[[nodiscard]] bool is_initialized() const noexcept { return m_initialized; }
[[nodiscard]] const Kernel::DDK::DmaBuffer& scratch_dma() const noexcept { return m_dma; }
[[nodiscard]] const Kernel::DDK::EventHandle& event() const noexcept { return m_event; }
[[nodiscard]] uint32_t irq_count() const noexcept { return m_irq_count; }

static const Kernel::DDK::DriverMetadata& metadata() noexcept;

private:
static void on_irq(uint32_t irq, void* ctx) noexcept;

Kernel::DDK::PlatformServices* m_services{nullptr};
Kernel::DDK::MmioRegion m_mmio;
Kernel::DDK::DmaBuffer m_dma;
Kernel::DDK::IrqHandle m_irq;
Kernel::DDK::EventHandle m_event;
uint32_t m_irq_count{0U};
bool m_initialized{false};
};

} // namespace Drivers::Dummy
30 changes: 30 additions & 0 deletions drivers/dummy/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SPDX-License-Identifier: GPL-3.0-only
# Copyright (c) 2026 Marco Buschini <marco.buschini@gmail.com>

# Standalone out-of-tree Makefile for drivers/dummy/.
# Usage: make MILO_DDK_DIR=/path/to/build/ddk

MILO_DDK_DIR ?= ../..
include $(MILO_DDK_DIR)/ddk.mk

BUILD_DIR ?= build
OBJS := $(BUILD_DIR)/DummyDriver.o
ARCHIVE := $(BUILD_DIR)/libdummy_driver.a
DEPS := $(OBJS:.o=.d)

.PHONY: all clean

all: $(ARCHIVE)

$(ARCHIVE): $(OBJS)
@mkdir -p $(dir $@)
$(DDK_AR) rcs $@ $^

$(BUILD_DIR)/%.o: %.cpp
@mkdir -p $(dir $@)
$(DDK_CXX) $(DDK_CXXFLAGS) -c $< -o $@

clean:
rm -rf $(BUILD_DIR)

-include $(DEPS)
27 changes: 27 additions & 0 deletions include/ddk/Bus.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: GPL-3.0-only
// Copyright (c) 2026 Marco Buschini <marco.buschini@gmail.com>

#pragma once

#include <cstddef>
#include <cstdint>

namespace Kernel::DDK {

enum class BusType : uint8_t {
Mmio = 0U,
Pci = 1U,
Platform = 2U,
};

struct DeviceDescriptor {
BusType bus{BusType::Mmio};
const char* compatible{nullptr};
uintptr_t mmio_base{0U};
size_t mmio_size{0U};
uint32_t irq{0U};
uint32_t device_id{0U};
uint32_t vendor_id{0U};
};

} // namespace Kernel::DDK
Loading
Loading