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
3 changes: 2 additions & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
run: |
sudo apt-get update
sudo apt-get install -y \
meson \
python3-pip \
nasm \
xorriso \
grub-pc-bin \
Expand All @@ -30,6 +30,7 @@ jobs:
mtools \
lld \
llvm \
pip3 install meson>=1.4.0

- name: Compile & Link
run: |
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ isodir
.idea
Meson-Build
Makefile
CMakeLists.txt
*build*
target
.venv
Expand Down
340 changes: 340 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,340 @@
cmake_minimum_required(VERSION 3.20)
project(voidframe VERSION 0.0.2 LANGUAGES C CXX ASM_NASM)
enable_language(ASM_NASM)
# ============================================================================
# Tool Dependencies
# ============================================================================
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_LINKER ld.lld)
set(CMAKE_ASM_NASM_COMPILER nasm)

find_program(LLVM_OBJDUMP llvm-objdump)
find_program(GRUB_MKRESCUE grub-mkrescue)
find_program(QEMU_IMG qemu-img)
find_program(MKFS_FAT mkfs.fat)
find_program(MKFS_EXT2 mkfs.ext2)
find_program(QEMU_SYSTEM_X86_64 qemu-system-x86_64)

# ============================================================================
# Build Configuration
# ============================================================================
option(EXCLUDE_EXTRA_OBJECTS "Exclude extra objects from the build" OFF)
option(AUTOMATIC_POST "Run POST automatically on boot" OFF)
option(DEBUG_SYMBOLS "Enable debug symbols" ON)
option(STACK_PROTECTION "Enable stack protection" ON)

# ============================================================================
# Compiler Flags
# ============================================================================
add_compile_options(-m64 -O2 -fno-omit-frame-pointer -finline-functions -foptimize-sibling-calls -nostdinc -nostdlib -fno-builtin -ffreestanding -mno-red-zone -mserialize -fPIE -fPIC -mcmodel=kernel -fcf-protection=full -fvisibility=hidden)

if(STACK_PROTECTION)
add_compile_options(-fstack-protector-strong -D_FORTIFY_SOURCE=2)
endif()

if(DEBUG_SYMBOLS)
add_compile_options(-g -DDEBUG)
endif()

set(CMAKE_CXX_FLAGS_INIT "${CMAKE_C_FLAGS_INIT} -fno-exceptions -fno-rtti -fno-threadsafe-statics")
set(CMAKE_ASM_NASM_FLAGS_INIT " -f elf64")

Comment on lines +40 to +42
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

CMAKE_*_FLAGS_INIT won’t take effect here; use per-language compile options

CMAKE_CXX_FLAGS_INIT/CMAKE_ASM_NASM_FLAGS_INIT are read before project(). At this point they are no-ops, so -fno-exceptions, -fno-rtti, etc., may not be applied.

Apply:

-set(CMAKE_CXX_FLAGS_INIT "${CMAKE_C_FLAGS_INIT} -fno-exceptions -fno-rtti -fno-threadsafe-statics")
-set(CMAKE_ASM_NASM_FLAGS_INIT " -f elf64")
+# Apply to C++ only
+add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions -fno-rtti -fno-threadsafe-statics>)

NASM object format fix is in the next comment.

🤖 Prompt for AI Agents
CMakeLists.txt lines 40-42: the use of CMAKE_CXX_FLAGS_INIT and
CMAKE_ASM_NASM_FLAGS_INIT here is ineffective because those *_INIT variables are
read before project(); instead move flag configuration after the project() call
and apply them per-language or per-target — e.g. use
target_compile_options(<target> PRIVATE -fno-exceptions -fno-rtti
-fno-threadsafe-statics) or
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions;-fno-rtti;-fno-threadsafe-statics>)
and for NASM use a per-language setting like
add_compile_options($<$<COMPILE_LANGUAGE:ASM_NASM>:-f elf64>) or
target_compile_options with the ASM_NASM generator expression so the flags are
actually applied.

# ============================================================================
# VoidFrame Configuration Flags
# ============================================================================
add_compile_definitions(
VF_CONFIG_ENABLE_XHCI
VF_CONFIG_ENABLE_VIRTIO
VF_CONFIG_ENABLE_ISA
VF_CONFIG_ENABLE_LPT
VF_CONFIG_ENABLE_PCI
VF_CONFIG_ENABLE_PS2
VF_CONFIG_ENABLE_IDE
VF_CONFIG_ENABLE_VFCOMPOSITOR
VF_CONFIG_ENABLE_AHCI
VF_CONFIG_ENABLE_GENERIC_SOUND
VF_CONFIG_RTC_CENTURY
VF_CONFIG_LOAD_MB_MODULES
VF_CONFIG_ENFORCE_MEMORY_PROTECTION
VF_CONFIG_VM_HOST
VF_CONFIG_SNOOZE_ON_BOOT
VF_CONFIG_PROCINFO_CREATE_DEFAULT
VF_CONFIG_USE_VFSHELL
VF_CONFIG_USE_DYNAMOX
VF_CONFIG_USE_ASTRA
VF_CONFIG_USE_CERBERUS
VF_CONFIG_CERBERUS_STACK_PROTECTION
VF_CONFIG_SCHED_MLFQ
)

if(EXCLUDE_EXTRA_OBJECTS)
add_compile_definitions(VF_CONFIG_EXCLUDE_EXTRA_OBJECTS)
endif()

if(AUTOMATIC_POST)
add_compile_definitions(VF_CONFIG_AUTOMATIC_POST)
endif()

set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -DVF_CONFIG_VESA_FB")

# ============================================================================
# Source Files Organization
# ============================================================================
set(ASM_SOURCES
arch/x86_64/asm/pxs.asm
arch/x86_64/idt/IdtLoad.asm
arch/x86_64/gdt/GdtTssFlush.asm
arch/x86_64/interrupts/Interrupts.asm
arch/x86_64/syscall/SyscallEntry.asm
include/Switch.asm
)

set(KERNEL_CORE_SOURCES
kernel/core/Kernel.c
kernel/core/Panic.c
kernel/core/Compositor.c
kernel/core/InitRD.c
)

set(SCHED_SOURCES
kernel/sched/MLFQ.c
)

set(KERNEL_ETC_SOURCES
kernel/etc/Shell.c
kernel/etc/Console.c
kernel/etc/Format.c
kernel/etc/VBEConsole.c
kernel/etc/Editor.c
kernel/etc/StringOps.c
kernel/etc/POST.c
)

set(ATOMIC_IPC_SOURCES
kernel/atomic/Atomics.c
kernel/ipc/Ipc.c
)

set(EXECF_SOURCES
kernel/execf/elf/ELFloader.c
kernel/execf/pe/PEloader.c
kernel/execf/aout/AoutLoader.c
kernel/execf/ExecLoader.c
)

set(MM_SOURCES
mm/PMem.c
mm/MemOps.c
mm/KernelHeap.c
mm/VMem.c
mm/StackGuard.c
mm/MemPool.c
mm/trace/StackTrace.c
mm/security/Cerberus.c
mm/PageFaultHandler.c
)

set(FS_SOURCES
fs/VFRFS.c
fs/FAT/FAT1x.c
fs/EXT/Ext2.c
fs/Iso9660.c
fs/VFS.c
fs/BlockDevice.c
fs/FileSystem.c
fs/MBR.c
)

set(DRIVER_SOURCES
drivers/APIC.c
drivers/TSC.c
drivers/ACPI.c
drivers/Serial.c
drivers/PS2.c
drivers/Ide.c
drivers/Vesa.c
drivers/PCI/PCI.c
drivers/RTC/Rtc.c
drivers/ethernet/realtek/RTL8139.c
drivers/ethernet/intel/E1000.c
drivers/ethernet/interface/Ip.c
drivers/ethernet/interface/Arp.c
drivers/ethernet/interface/Icmp.c
drivers/ethernet/Network.c
drivers/xHCI/xHCI.c
drivers/ISA/ISA.c
drivers/sound/SB16.c
drivers/sound/Generic.c
drivers/storage/AHCI.c
drivers/LPT/LPT.c
drivers/virtio/VirtioBlk.c
drivers/vmware/SVGAII.c
)

set(ARCH_SOURCES
arch/x86_64/idt/Idt.c
arch/x86_64/gdt/Gdt.c
arch/x86_64/interrupts/Interrupts.c
arch/x86_64/syscall/Syscall.c
arch/x86_64/features/x64.c
)

set(INCLUDE_SOURCES
include/ctype.c
include/Font.c
)

set(CPP_SOURCES
ports/6502/6502.cpp
)

set(C_SOURCES
${KERNEL_CORE_SOURCES}
${SCHED_SOURCES}
${KERNEL_ETC_SOURCES}
${ATOMIC_IPC_SOURCES}
${EXECF_SOURCES}
${MM_SOURCES}
${FS_SOURCES}
${DRIVER_SOURCES}
${ARCH_SOURCES}
${INCLUDE_SOURCES}
)

set(OBJ_SOURCES)
if(NOT EXCLUDE_EXTRA_OBJECTS)
set(OBJ_SOURCES
kernel/etc/objects/splash1.o
kernel/etc/objects/panic.o
)
endif()

# ============================================================================
# Build Include Directories
# ============================================================================
include_directories(
.
include
kernel/atomic
kernel/core
kernel/ipc
kernel/sched
kernel/etc
kernel/execf
kernel/execf/elf
kernel/execf/pe
kernel/execf/aout
drivers
drivers/PCI
drivers/ethernet
drivers/ethernet/intel
drivers/ethernet/realtek
drivers/ethernet/interface
drivers/RTC
drivers/xHCI
drivers/ISA
drivers/sound
drivers/storage
drivers/virtio
drivers/vmware
fs
fs/FAT
fs/EXT
mm
mm/trace
mm/security
ports/6502
ports
arch/x86_64/features
arch/x86_64/gdt
arch/x86_64/idt
arch/x86_64/interrupts
arch/x86_64/syscall
)

# ============================================================================
# Kernel Linking
# ============================================================================
add_executable(voidframe.krnl
${C_SOURCES}
${CPP_SOURCES}
${ASM_SOURCES}
${OBJ_SOURCES}
)

set_target_properties(voidframe.krnl PROPERTIES
OUTPUT_NAME "voidframe.krnl"
LINK_FLAGS "-T ${CMAKE_CURRENT_SOURCE_DIR}/voidframe.ld -melf_x86_64"
)
Comment on lines +266 to +269
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Kernel link pipeline: use target_link_options; drop PIE/PIC and fix -nostdlib placement

Current setup risks linking via clang with default libs and treating -melf_x86_64 as an unsupported driver flag. Also -nostdlib is in compile flags (ineffective) and -fPIE/-fPIC conflicts with kernel link. Move -nostdlib/-no-pie to link, remove PIE/PIC at compile, and pass linker script via -Wl,-T,....

Apply:

- set_target_properties(voidframe.krnl PROPERTIES
-     OUTPUT_NAME "voidframe.krnl"
-     LINK_FLAGS "-T ${CMAKE_CURRENT_SOURCE_DIR}/voidframe.ld -melf_x86_64"
- )
+ set_target_properties(voidframe.krnl PROPERTIES
+     OUTPUT_NAME "voidframe.krnl"
+ )
+ # Link kernel without host runtimes, no PIE; pass the linker script explicitly.
+ target_link_options(voidframe.krnl PRIVATE
+     -nostdlib
+     -no-pie
+     "-Wl,-T,${CMAKE_CURRENT_SOURCE_DIR}/voidframe.ld"
+ )

And adjust compile flags (see Line 30 diff below) to remove -fPIE -fPIC and relocate -nostdlib.
Additionally, consider -Wl,--build-id=none if you want to avoid build-id notes in a freestanding kernel.

- add_compile_options(-m64 -O2 -fno-omit-frame-pointer -finline-functions -foptimize-sibling-calls -nostdinc -nostdlib -fno-builtin -ffreestanding -mno-red-zone -mserialize -fPIE -fPIC -mcmodel=kernel -fcf-protection=full -fvisibility=hidden)
+ add_compile_options(-m64 -O2 -fno-omit-frame-pointer -finline-functions -foptimize-sibling-calls -nostdinc -fno-builtin -ffreestanding -mno-red-zone -mserialize -fno-pie -mcmodel=kernel -fcf-protection=full -fvisibility=hidden)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
set_target_properties(voidframe.krnl PROPERTIES
OUTPUT_NAME "voidframe.krnl"
LINK_FLAGS "-T ${CMAKE_CURRENT_SOURCE_DIR}/voidframe.ld -melf_x86_64"
)
# --- Adjusted kernel compile flags (remove PIE/PIC, relocate -nostdlib) ---
# Original at line ~30:
# add_compile_options(-m64 -O2 -fno-omit-frame-pointer -finline-functions -foptimize-sibling-calls
# -nostdinc -nostdlib -fno-builtin -ffreestanding -mno-red-zone
# -mserialize -fPIE -fPIC -mcmodel=kernel -fcf-protection=full
# -fvisibility=hidden)
add_compile_options(
-m64
-O2
-fno-omit-frame-pointer
-finline-functions
-foptimize-sibling-calls
-nostdinc
-fno-builtin
-ffreestanding
-mno-red-zone
-mserialize
-fno-pie
-mcmodel=kernel
-fcf-protection=full
-fvisibility=hidden
)
# --- Updated target link setup for voidframe.krnl (was lines 266–269) ---
# Original:
# set_target_properties(voidframe.krnl PROPERTIES
# OUTPUT_NAME "voidframe.krnl"
# LINK_FLAGS "-T ${CMAKE_CURRENT_SOURCE_DIR}/voidframe.ld -melf_x86_64"
# )
set_target_properties(voidframe.krnl PROPERTIES
OUTPUT_NAME "voidframe.krnl"
)
# Link kernel without host runtimes, disable PIE, and apply our script explicitly
target_link_options(voidframe.krnl PRIVATE
-nostdlib
-no-pie
"-Wl,-T,${CMAKE_CURRENT_SOURCE_DIR}/voidframe.ld"
)
🤖 Prompt for AI Agents
In CMakeLists.txt around lines 266-269, replace the set_target_properties
LINK_FLAGS usage with target_link_options(voidframe.krnl PRIVATE ...) and move
linker-only flags there; remove -fPIE and -fPIC from compile flags (adjust
earlier compile flags around line 30), remove -nostdlib from compile flags and
instead add -Wl,-nostdlib and -Wl,-no-pie along with the linker script using
-Wl,-T,${CMAKE_CURRENT_SOURCE_DIR}/voidframe.ld (do not pass -melf_x86_64 as a
driver flag — if needed, pass it via the linker driver with -Wl, or omit if the
linker target is correct), and optionally add -Wl,--build-id=none for a
freestanding kernel.


# ============================================================================
# ISO Creation
# ============================================================================
add_custom_command(
OUTPUT VoidFrame.iso
COMMAND sh -c "cd ${CMAKE_CURRENT_BINARY_DIR} && mkdir -p isodir/boot/grub isodir/EFI/BOOT isodir/bin && cp $<TARGET_FILE:voidframe.krnl> isodir/boot/voidframe.krnl && cp ${CMAKE_CURRENT_SOURCE_DIR}/grub.cfg isodir/boot/grub/grub.cfg && cp ${CMAKE_CURRENT_SOURCE_DIR}/grub.cfg isodir/EFI/BOOT/grub.cfg && ${GRUB_MKRESCUE} --modules=\"multiboot2 part_msdos part_gpt fat\" --fonts=\"\" --locales=\"\" -o VoidFrame.iso isodir"
DEPENDS voidframe.krnl
COMMENT "Building VoidFrame.iso"
)

add_custom_target(iso ALL DEPENDS VoidFrame.iso)

# ============================================================================
# Run Targets
# ============================================================================
add_custom_target(run
COMMAND ${QEMU_SYSTEM_X86_64}
-cpu max
-vga vmware
-enable-kvm
-cdrom ${CMAKE_CURRENT_BINARY_DIR}/VoidFrame.iso
-debugcon file:bootstrap.log
-serial stdio
-no-reboot -no-shutdown
-m 4G
-drive file=VoidFrameDisk.img,if=ide
-drive file=SataDisk.img,if=none,id=sata0
-device ahci,id=ahci
-device ide-hd,drive=sata0,bus=ahci.0
-boot d
-device rtl8139
-device e1000
-device nec-usb-xhci,id=xhci
-device ich9-intel-hda
-usb -device usb-tablet
-audio pa,id=myaudio
-device sb16,iobase=0x220,irq=5,dma=1,dma16=5
-parallel file:printer.out
-drive file=VirtioDisk.img,format=raw,id=virtio_disk,if=none
-device virtio-blk-pci,drive=virtio_disk,disable-legacy=on
DEPENDS iso
)

add_custom_target(runmin
COMMAND ${QEMU_SYSTEM_X86_64}
-cdrom ${CMAKE_CURRENT_BINARY_DIR}/VoidFrame.iso
-nographic
-debugcon file:bootstrap.log
-serial file:serial.log
-no-reboot -no-shutdown
-m 4G
-boot d
DEPENDS iso
)

add_custom_target(img
COMMAND sh -c "${QEMU_IMG} create -f qcow2 VoidFrameDisk.img 16G && ${MKFS_EXT2} VoidFrameDisk.img"
COMMENT "Creating disk images"
)
Comment on lines +327 to +329
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Formatting qcow2 directly with mkfs.ext2 will not work

mkfs.ext2 expects a block device or a raw file (-F). Running it on a qcow2 container file produces a broken image.

Use raw for simplicity:

-add_custom_target(img
-    COMMAND sh -c "${QEMU_IMG} create -f qcow2 VoidFrameDisk.img 16G && ${MKFS_EXT2} VoidFrameDisk.img"
-    COMMENT "Creating disk images"
-)
+add_custom_target(img
+    COMMAND sh -c "${QEMU_IMG} create -f raw VoidFrameDisk.img 16G && ${MKFS_EXT2} -F VoidFrameDisk.img"
+    COMMENT "Creating disk images (raw + ext2)"
+)

Alternatively, keep qcow2 but format via nbd: qemu-nbd --connect=/dev/nbd0 VoidFrameDisk.img && mkfs.ext2 /dev/nbd0 && qemu-nbd --disconnect /dev/nbd0. I can provide a portable target if desired.

🤖 Prompt for AI Agents
In CMakeLists.txt around lines 327 to 329, running mkfs.ext2 directly on the
qcow2 container file is incorrect because mkfs.ext2 expects a block device or a
raw file; update the COMMAND to either create a raw image (e.g., use qemu-img
create -f raw ...) and then run mkfs.ext2 on that file, or keep qcow2 and
replace the single-step mkfs call with a connect/format/disconnect flow using
qemu-nbd (qemu-nbd --connect=/dev/nbdX VoidFrameDisk.img && mkfs.ext2 /dev/nbdX
&& qemu-nbd --disconnect /dev/nbdX), ensuring the CMake COMMAND uses the chosen
approach and handles errors/cleanup.


add_custom_target(extra-img
COMMAND sh -c "${QEMU_IMG} create -f raw VirtioDisk.img 128M && ${MKFS_FAT} -F 16 VirtioDisk.img && ${QEMU_IMG} create -f raw SataDisk.img 128M && ${MKFS_FAT} -F 16 SataDisk.img"
COMMENT "Creating extra disk images"
)

add_custom_target(dump
COMMAND sh -c "${LLVM_OBJDUMP} -d $<TARGET_FILE:voidframe.krnl> > voidframe.dump && ${LLVM_OBJDUMP} -t $<TARGET_FILE:voidframe.krnl> > voidframe.sym"
DEPENDS voidframe.krnl
COMMENT "Generating disassembly and symbols"
)
Loading
Loading