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
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Rust Memory Management
# ============================================================================
add_subdirectory(mm/rust)
add_subdirectory(kernel/atomic/rust)

# ============================================================================
# Build Include Directories
Expand Down Expand Up @@ -129,8 +130,9 @@ if(NOT EXCLUDE_EXTRA_OBJECTS)
target_sources(voidframe.krnl PRIVATE ${OBJ_SOURCES})
endif()

# Link Rust heap library
# Rust libraries
target_link_libraries(voidframe.krnl PRIVATE rust_heap)
target_link_libraries(voidframe.krnl PRIVATE rust_spinlock)

# Configure the linker to use ld.lld with proper arguments
set_target_properties(voidframe.krnl PROPERTIES
Expand Down
31 changes: 19 additions & 12 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@ 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)
find_program(CARGO_EXECUTABLE cargo REQUIRED)
find_program(RUSTC_EXECUTABLE rustc REQUIRED)

if (NOT LLVM_OBJDUMP)
message(WARNING "llvm-objdump not found. Please install LLVM.")
elseif (NOT GRUB_MKRESCUE)
message(WARNING "grub-mkrescue not found. Please install GRUB.")
elseif (NOT QEMU_IMG)
message(WARNING "qemu-img not found. Please install QEMU.")
elseif (NOT MKFS_FAT)
message(WARNING "mkfs.fat not found. Please install dosfstools.")
elseif (NOT MKFS_EXT2)
message(WARNING "mkfs.ext2 not found. Please install e2fsprogs.")
elseif (NOT QEMU_SYSTEM_X86_64)
message(WARNING "qemu-system-x86_64 not found. Please install QEMU.")
if(NOT LLVM_OBJDUMP)
message(WARNING "llvm-objdump not found. ")
endif()
if(NOT GRUB_MKRESCUE)
message(WARNING "grub-mkrescue not found. ")
endif()
if(NOT QEMU_IMG)
message(WARNING "qemu-img not found. ")
endif()
if(NOT MKFS_FAT)
message(WARNING "mkfs.fat not found. ")
endif()
if(NOT MKFS_EXT2)
message(WARNING "mkfs.ext2 not found. ")
endif()
if(NOT QEMU_SYSTEM_X86_64)
message(WARNING "qemu-system-x86_64 not found. ")
endif()
1 change: 1 addition & 0 deletions cmake/source.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ set(ARCH_SOURCES
set(INCLUDE_SOURCES
include/ctype.c
include/Font.c
include/Io.c
)

set(CPP_SOURCES
Expand Down
1 change: 1 addition & 0 deletions cmake/variable.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ add_compile_definitions(
KERNEL_SPACE_END=0xFFFFFFFFFFFFFFFFULL
PREFETCH_DISTANCE=256
NT_STORE_THRESHOLD=4*1024*1024
MAX_SUPPORTED_MEMORY=128*1024*1024*1024
)
38 changes: 22 additions & 16 deletions drivers/Ide.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#include "Ide.h"
#include "BlockDevice.h"
#include "Format.h"

#include "SpinlockRust.h"
#include "APIC.h"
#include "Console.h"
#include "Io.h"
#include "MemOps.h"
#include "Spinlock.h"

static IdeChannel channels[2];
static volatile int ide_lock = 0;
static RustSpinLock* ide_lock = NULL;

// Wait for drive to be ready (not busy)
static int IdeWaitReady(uint16_t base_port) {
Expand Down Expand Up @@ -113,6 +112,13 @@ static int IdeIdentifyDrive(uint16_t base_port, uint8_t drive, uint16_t* buffer,
}

int IdeInit(void) {
if (!ide_lock) {
ide_lock = rust_spinlock_new();
if (!ide_lock) {
PrintKernelError("IDE: Failed to create spinlock\n");
return IDE_ERROR_IO;
}
}
PrintKernel("IDE: Initializing IDE controller...\n");

// Initialize channel structures
Expand Down Expand Up @@ -211,7 +217,7 @@ int IdeInit(void) {
return IDE_OK;
}

int IdeReadBlocks(struct BlockDevice* device, uint64_t start_lba, uint32_t count, void* buffer) {
int IdeReadBlocks(BlockDevice* device, uint64_t start_lba, uint32_t count, void* buffer) {
if (!device || !device->driver_data) {
PrintKernel("IDE: Invalid device or driver_data\n");
return -1;
Expand All @@ -227,16 +233,16 @@ int IdeReadBlocks(struct BlockDevice* device, uint64_t start_lba, uint32_t count
return IDE_ERROR_NO_DRIVE;
}

SpinLock(&ide_lock);
uint16_t base_port = channels[channel].base_port;
rust_spinlock_lock(ide_lock);
const uint16_t base_port = channels[channel].base_port;

for (uint32_t i = 0; i < count; i++) {
uint64_t lba = start_lba + i;
uint8_t* buf = (uint8_t*)buffer + (i * 512);

int result = IdeSelectDrive(base_port, drive_num, lba);
if (result != IDE_OK) {
SpinUnlock(&ide_lock);
rust_spinlock_unlock(ide_lock);
return result;
}

Expand All @@ -251,7 +257,7 @@ int IdeReadBlocks(struct BlockDevice* device, uint64_t start_lba, uint32_t count
PrintKernel("IDE: Wait for data failed with error ");
PrintKernelInt(result);
PrintKernel("\n");
SpinUnlock(&ide_lock);
rust_spinlock_unlock(ide_lock);
return result;
}

Expand All @@ -261,7 +267,7 @@ int IdeReadBlocks(struct BlockDevice* device, uint64_t start_lba, uint32_t count
}
}

SpinUnlock(&ide_lock);
rust_spinlock_unlock(ide_lock);
return 0;
}

Expand All @@ -276,7 +282,7 @@ int IdeWriteBlocks(struct BlockDevice* device, uint64_t start_lba, uint32_t coun
return IDE_ERROR_NO_DRIVE;
}

SpinLock(&ide_lock);
rust_spinlock_lock(ide_lock);
uint16_t base_port = channels[channel].base_port;

for (uint32_t i = 0; i < count; i++) {
Expand All @@ -285,7 +291,7 @@ int IdeWriteBlocks(struct BlockDevice* device, uint64_t start_lba, uint32_t coun

int result = IdeSelectDrive(base_port, drive_num, lba);
if (result != IDE_OK) {
SpinUnlock(&ide_lock);
rust_spinlock_unlock(ide_lock);
return result;
}

Expand All @@ -297,7 +303,7 @@ int IdeWriteBlocks(struct BlockDevice* device, uint64_t start_lba, uint32_t coun

result = IdeWaitData(base_port);
if (result != IDE_OK) {
SpinUnlock(&ide_lock);
rust_spinlock_unlock(ide_lock);
return result;
}

Expand All @@ -308,12 +314,12 @@ int IdeWriteBlocks(struct BlockDevice* device, uint64_t start_lba, uint32_t coun

result = IdeWaitReady(base_port);
if (result != IDE_OK) {
SpinUnlock(&ide_lock);
rust_spinlock_unlock(ide_lock);
return result;
}
}

SpinUnlock(&ide_lock);
rust_spinlock_unlock(ide_lock);
return 0;
}

Expand Down Expand Up @@ -357,9 +363,9 @@ int IdeReadLBA2048(uint8_t drive, uint32_t lba, void* buffer) {
return IDE_ERROR_NO_DRIVE;
}

SpinLock(&ide_lock);
rust_spinlock_lock(ide_lock);
uint16_t base_port = channels[channel].base_port;
SpinUnlock(&ide_lock);
rust_spinlock_unlock(ide_lock);
int result;

result = IdeSelectDrive(base_port, drive_num, 0); // LBA is in the packet
Expand Down
28 changes: 17 additions & 11 deletions drivers/virtio/VirtioBlk.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include "VirtioBlk.h"
#include "Spinlock.h"
#include "Atomics.h"
#include "Console.h"
#include "PCI/PCI.h"
#include "Spinlock.h"
#include "SpinlockRust.h"
#include "VMem.h"
#include "stdbool.h"
#include "Virtio.h"
#include "stdbool.h"
// Globals to hold the capability structures we find
static volatile int* virtio_lock;
static RustSpinLock* virtio_lock = NULL;
static struct VirtioPciCap cap_common_cfg;
static struct VirtioPciCap cap_notify_cfg;
static struct VirtioPciCap cap_isr_cfg;
Expand Down Expand Up @@ -49,6 +50,11 @@ void ReadVirtioCapability(PciDevice device, uint8_t cap_offset, struct VirtioPci
// Implementation for the VirtIO Block device driver.

void InitializeVirtioBlk(PciDevice device) {
virtio_lock = rust_spinlock_new();
if (!virtio_lock) {
PrintKernelError("VirtIO-Blk: - Failed to initialize spinlock.\n");
return;
}
PrintKernel("VirtIO-Blk: Initializing device at B/D/F ");
PrintKernelHex(device.bus); PrintKernel("/"); PrintKernelHex(device.device); PrintKernel("/"); PrintKernelHex(device.function);
PrintKernel("\n");
Expand Down Expand Up @@ -210,11 +216,11 @@ void InitializeVirtioBlk(PciDevice device) {
}

int VirtioBlkRead(uint64_t sector, void* buffer) {
SpinLock(&virtio_lock);
rust_spinlock_lock(virtio_lock);

if ((vq_next_desc_idx + 3) > vq_size) {
PrintKernel("VirtIO-Blk: Error - Not enough descriptors available\n");
SpinUnlock(&virtio_lock);
rust_spinlock_unlock(virtio_lock);
return -1;
}

Expand All @@ -224,7 +230,7 @@ int VirtioBlkRead(uint64_t sector, void* buffer) {
PrintKernel("VirtIO-Blk: Failed to allocate request header/status\n");
if (req_hdr) VMemFree(req_hdr, sizeof(struct VirtioBlkReq));
if (status) VMemFree(status, sizeof(uint8_t));
SpinUnlock(&virtio_lock);
rust_spinlock_unlock(virtio_lock);
return -1;
}

Expand Down Expand Up @@ -273,16 +279,16 @@ int VirtioBlkRead(uint64_t sector, void* buffer) {
last_used_idx++;
}

SpinUnlock(&virtio_lock);
rust_spinlock_unlock(virtio_lock);
return 0;
}

int VirtioBlkWrite(uint64_t sector, void* buffer) {
SpinLock(&virtio_lock);
rust_spinlock_lock(virtio_lock);

if ((vq_next_desc_idx + 3) > vq_size) {
PrintKernel("VirtIO-Blk: Error - Not enough descriptors available\n");
SpinUnlock(&virtio_lock);
rust_spinlock_unlock(virtio_lock);
return -1;
}

Expand All @@ -292,7 +298,7 @@ int VirtioBlkWrite(uint64_t sector, void* buffer) {
PrintKernel("VirtIO-Blk: Failed to allocate request header/status\n");
if (req_hdr) VMemFree(req_hdr, sizeof(struct VirtioBlkReq));
if (status) VMemFree(status, sizeof(uint8_t));
SpinUnlock(&virtio_lock);
rust_spinlock_unlock(virtio_lock);
return -1;
}

Expand Down Expand Up @@ -341,6 +347,6 @@ int VirtioBlkWrite(uint64_t sector, void* buffer) {
last_used_idx++;
}

SpinUnlock(&virtio_lock);
rust_spinlock_unlock(virtio_lock);
return 0;
}
52 changes: 52 additions & 0 deletions include/Io.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "Io.h"
#include "x64.h"

void cli() {
_full_mem_prot_start();
__asm__ volatile("cli" ::: "memory");
#ifdef VF_CONFIG_INTEL
_full_mem_prot_end_intel();
#else
_full_mem_prot_end();
#endif
}

void sti() {
_full_mem_prot_start();
__asm__ volatile("sti" ::: "memory");
#ifdef VF_CONFIG_INTEL
_full_mem_prot_end_intel();
#else
_full_mem_prot_end();
#endif
}

// CPUID detection
void cpuid(uint32_t leaf, uint32_t* eax, uint32_t* ebx, uint32_t* ecx, uint32_t* edx) {
__asm__ volatile("cpuid"
: "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
: "a"(leaf));
}

// MSR access
uint64_t rdmsr(uint32_t msr) {
uint32_t low, high;
__asm__ volatile("rdmsr" : "=a"(low), "=d"(high) : "c"(msr));
return ((uint64_t)high << 32) | low;
}

void wrmsr(uint32_t msr, uint64_t value) {
uint32_t low = value & 0xFFFFFFFF;
uint32_t high = value >> 32;
__asm__ volatile("wrmsr" :: "a"(low), "d"(high), "c"(msr));
}

irq_flags_t save_irq_flags(void) {
irq_flags_t flags;
__asm__ volatile("pushfq\n\tpopq %0" : "=r"(flags));
return flags;
}

void restore_irq_flags(irq_flags_t flags) {
__asm__ volatile("pushq %0\n\tpopfq" : : "r"(flags));
}
Loading