Skip to content
This repository was archived by the owner on Jan 4, 2026. It is now read-only.
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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ ninja run
- [x] Create (register)
- [ ] Delete (unregister)
- [x] List
- PROCFS
- [x] Read
- [x] Write
- [x] Create (register)
- [ ] Delete (unregister)
- [x] List
- ISO9660 (RO)
- [x] Read
- [x] List
Expand All @@ -138,7 +144,8 @@ ninja run
- [x] EXT2
- [x] FAT1x
- [x] VFRFS
- [ ] DEVFS
- [x] DEVFS
- [x] PROCFS
- [ ] ISO9660
### Drivers
- Network
Expand Down
2 changes: 2 additions & 0 deletions cmake/source.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ set(FS_SOURCES
fs/FAT/FAT1x.c
fs/EXT/Ext2.c
fs/NTFS/NTFS.c
fs/procfs/ProcFS.c
fs/Iso9660.c
fs/VFS.c
fs/BlockDevice.c
Expand All @@ -87,6 +88,7 @@ set(DRIVER_SOURCES
drivers/TSC.c
drivers/ACPI.c
drivers/Serial.c
drivers/Random.c
drivers/PS2.c
drivers/storage/Ide.c
drivers/Vesa.c
Expand Down
41 changes: 41 additions & 0 deletions drivers/Random.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "Random.h"
#include "../crypto/RNG.h"
#include "../fs/CharDevice.h"

static int RandomDevRead(struct CharDevice* dev, void* buffer, uint32_t size) {
uint8_t* buf = (uint8_t*)buffer;
if (rdrand_supported()) {
for (uint32_t i = 0; i < size; i += 2) {
uint16_t rand = rdrand16();
buf[i] = rand & 0xFF;
if (i + 1 < size) {
buf[i + 1] = (rand >> 8) & 0xFF;
}
}
} else {
for (uint32_t i = 0; i < size; i += 8) {
uint64_t rand = xoroshiro128plus();
for (int j = 0; j < 8 && (i + j) < size; ++j) {
buf[i + j] = (rand >> (j * 8)) & 0xFF;
}
}
}
return size;
}

static CharDevice_t g_random_device = {
.name = "Random",
.Read = RandomDevRead,
.Write = NULL, // Not supported
};

void RandomInit(void) {
if (!rdrand_supported()) {
uint32_t lo, hi;
__asm__ volatile("rdtsc" : "=a"(lo), "=d"(hi));
uint64_t tsc = ((uint64_t)hi << 32) | lo;
uint64_t entropy = tsc ^ (uint64_t)(uintptr_t)&g_random_device;
rng_seed(tsc, entropy);
}
CharDeviceRegister(&g_random_device);
}
8 changes: 8 additions & 0 deletions drivers/Random.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef VOIDFRAME_RANDOM_H
#define VOIDFRAME_RANDOM_H

#include "stdint.h"

void RandomInit(void);

#endif //VOIDFRAME_RANDOM_H
1 change: 0 additions & 1 deletion fs/CharDevice.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "CharDevice.h"
#include "../kernel/etc/StringOps.h"
#include "../../kernel/etc/Console.h"

static CharDevice_t* g_char_devices[MAX_CHAR_DEVICES];
static int g_num_char_devices = 0;
Expand Down
18 changes: 18 additions & 0 deletions fs/VFS.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "CharDevice.h"
#include "Console.h"
#include "devfs/DevFS.h"
#include "procfs/ProcFS.h"
#include "EXT/Ext2.h"
#include "FAT/FAT1x.h"
#include "FileSystem.h"
Expand Down Expand Up @@ -92,6 +93,7 @@ int VfsInit(void) {
mounts[i].active = 0;
}
PrintKernel( "VFS: Mount table cleared\n");
ProcFSInit();

// Register filesystems
static FileSystemDriver ntfs_driver = {"NTFS", NtfsDetect, NtfsMount, NtfsUnmount};
Expand All @@ -106,6 +108,9 @@ int VfsInit(void) {
static FileSystemDriver devfs_driver = {"DevFS", NULL, DevfsMount, NULL};
FileSystemRegister(&devfs_driver);
PrintKernel("VFS: DevFS driver registered\n");
static FileSystemDriver procfs_driver = {"ProcFS", NULL, ProcfsMount, NULL};
FileSystemRegister(&procfs_driver);
PrintKernel("VFS: ProcFS driver registered\n");

int result = VfsMount("/", NULL, NULL); // RamFS doesn't need a device or driver
if (result != 0) {
Expand All @@ -117,6 +122,11 @@ int VfsInit(void) {
SerialWrite("VFS: Failed to mount /Devices\n");
}

result = VfsMount(RuntimeProcesses, NULL, &procfs_driver);
if (result != 0) {
SerialWrite("VFS: Failed to mount /Runtime/Processes\n");
}

PrintKernelSuccess("VFS: Virtual File System initialized\n");
return 0;
}
Expand Down Expand Up @@ -193,6 +203,8 @@ int VfsReadFile(const char* path, void* buffer, uint32_t max_size) {
return NtfsReadFile(local_path, buffer, max_size);
} else if (FastStrCmp(mount->fs_driver->name, "DevFS") == 0) {
return DevfsReadFile(local_path, buffer, max_size);
} else if (FastStrCmp(mount->fs_driver->name, "ProcFS") == 0) {
return ProcfsReadFile(local_path, buffer, max_size);
}
} else {
FsNode* node = FsFind(local_path);
Expand Down Expand Up @@ -226,6 +238,8 @@ int VfsWriteFile(const char* path, const void* buffer, uint32_t size) {
return NtfsWriteFile(local_path, buffer, size);
} else if (FastStrCmp(mount->fs_driver->name, "DevFS") == 0) {
return DevfsWriteFile(local_path, buffer, size);
} else if (FastStrCmp(mount->fs_driver->name, "ProcFS") == 0) {
return ProcfsWriteFile(local_path, buffer, size);
}
} else {
int fd = FsOpen(local_path, FS_WRITE);
Expand Down Expand Up @@ -257,6 +271,8 @@ int VfsListDir(const char* path) {
return NtfsListDir(local_path);
} else if (FastStrCmp(mount->fs_driver->name, "DevFS") == 0) {
return DevfsListDir(local_path);
} else if (FastStrCmp(mount->fs_driver->name, "ProcFS") == 0) {
return ProcfsListDir(local_path);
}
} else {
return FsListDir(local_path);
Expand Down Expand Up @@ -364,6 +380,8 @@ int VfsIsDir(const char* path) {
return NtfsIsDir(local_path);
} else if (FastStrCmp(mount->fs_driver->name, "DevFS") == 0) {
return DevfsIsDir(local_path);
} else if (FastStrCmp(mount->fs_driver->name, "ProcFS") == 0) {
return ProcfsIsDir(local_path);
}
} else {
FsNode* node = FsFind(local_path);
Expand Down
174 changes: 174 additions & 0 deletions fs/procfs/ProcFS.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#include "ProcFS.h"
#include "VFS.h"
#include "KernelHeap.h"
#include "StringOps.h"
#include "Console.h"
#include "Format.h"
#include "stdlib.h"
#include "MemOps.h"
#include "Scheduler.h"

static ProcFSEntry* proc_list_head = NULL;

void ProcFSInit() {
proc_list_head = NULL;
PrintKernelF("VFS: Initialized ProcFS\n");
}

void ProcFSRegisterProcess(uint32_t pid, void* data) {
ProcFSEntry* new_entry = (ProcFSEntry*)KernelMemoryAlloc(sizeof(ProcFSEntry));
if (!new_entry) {
PrintKernelError("VFS: Failed to allocate memory for new process entry\n");
return;
}
new_entry->pid = pid;
new_entry->next = proc_list_head;
proc_list_head = new_entry;
}

void ProcFSUnregisterProcess(uint32_t pid) {
ProcFSEntry* current = proc_list_head;
ProcFSEntry* prev = NULL;

while (current) {
if (current->pid == pid) {
if (prev) {
prev->next = current->next;
} else {
proc_list_head = current->next;
}
KernelFree(current);
return;
}
prev = current;
current = current->next;
}
}

int ProcfsMount(struct BlockDevice* device, const char* mount_point) {
(void)device;
(void)mount_point;
return 0;
}

int ProcfsReadFile(const char* path, void* buffer, uint32_t max_size) {
if (path[0] != '/') return -1;

char pid_str[16];
int i = 1;
int j = 0;
while (path[i] != '/' && path[i] != '\0' && j < 15) {
pid_str[j++] = path[i++];
}
pid_str[j] = '\0';

if (path[i] == '\0') return -1;

uint32_t pid = atoi(pid_str);
CurrentProcessControlBlock* pcb = GetCurrentProcessByPID(pid);

if (!pcb) {
return -1;
}

const char* filename = &path[i + 1];
char local_buffer[1024];

if (FastStrCmp(filename, "info") == 0) {
int len = FormatA(local_buffer, sizeof(local_buffer),
"Name: %s\n"
"PID: %u\n"
"State: %d\n"
"PPID: %u\n"
"Priority: %d\n"
"Privilege: %d\n"
"CPU Time: %llu ticks\n"
"Creation Time: %llu\n",
pcb->name,
pcb->pid,
pcb->state,
pcb->parent_pid,
#if defined(VF_CONFIG_SCHED_MLFQ)
pcb->priority,
#else
pcb->nice,
#endif
pcb->privilege_level,
pcb->cpu_time_accumulated,
pcb->creation_time);

if (len > max_size) len = max_size;
FastMemcpy(buffer, local_buffer, len);
return len;
}
return -1;
}

int ProcfsWriteFile(const char* path, const void* buffer, uint32_t size) {
(void)path;
(void)buffer;
(void)size;
return -1;
}

int ProcfsListDir(const char* path) {
if (FastStrCmp(path, "/") == 0) {
ProcFSEntry* current = proc_list_head;
while (current) {
PrintKernelF(" %d/\n", current->pid);
current = current->next;
}
return 0;
}

if (path[0] == '/' && path[1] != '\0') {
char pid_str[16];
int i = 1;
int j = 0;
while (path[i] != '/' && path[i] != '\0' && j < 15) {
pid_str[j++] = path[i++];
}
pid_str[j] = '\0';

uint32_t pid = atoi(pid_str);
ProcFSEntry* current = proc_list_head;
while (current) {
if (current->pid == pid) {
if (path[i] == '\0' || (path[i] == '/' && path[i+1] == '\0')) {
PrintKernelF(" info\n");
return 0;
}
}
current = current->next;
}
}
return -1;
}

int ProcfsIsDir(const char* path) {
if (FastStrCmp(path, "/") == 0) {
return 1;
}

if (path[0] == '/' && path[1] != '\0') {
char pid_str[16];
int i = 1;
int j = 0;
while (path[i] != '/' && path[i] != '\0' && j < 15) {
pid_str[j++] = path[i++];
}
pid_str[j] = '\0';

uint32_t pid = atoi(pid_str);
ProcFSEntry* current = proc_list_head;
while (current) {
if (current->pid == pid) {
if (path[i] == '\0' || (path[i] == '/' && path[i+1] == '\0')) {
return 1;
}
}
current = current->next;
}
}
return 0;
}
39 changes: 39 additions & 0 deletions fs/procfs/ProcFS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef VOIDFRAME_PROCFS_H
#define VOIDFRAME_PROCFS_H

#include "../BlockDevice.h"
#include <stdint.h>
#include <stdbool.h>

// Represents an entry in the procfs, typically a process.
// The data pointer is generic and can be used to store scheduler-specific info.
typedef struct ProcFSEntry {
uint32_t pid; // Process ID
struct ProcFSEntry* next; // Pointer to the next process entry
} ProcFSEntry;

// Initializes the procfs filesystem.
void ProcFSInit();

// Registers a new process with the given PID and associated data.
void ProcFSRegisterProcess(uint32_t pid, void* data);

// Unregisters a process from procfs using its PID.
void ProcFSUnregisterProcess(uint32_t pid);

// Mounts the procfs. This is a dummy function as procfs is a virtual filesystem.
int ProcfsMount(struct BlockDevice* device, const char* mount_point);

// Reads the content of a file within the procfs.
int ProcfsReadFile(const char* path, void* buffer, uint32_t max_size);

// Writes content to a file in procfs. Currently not supported.
int ProcfsWriteFile(const char* path, const void* buffer, uint32_t size);

// Lists the directory contents in procfs.
int ProcfsListDir(const char* path);

// Checks if a given path in procfs is a directory.
int ProcfsIsDir(const char* path);

#endif //VOIDFRAME_PROCFS_H
Loading