From 10bb61b738fe1b979674750d61ca4d351334db5f Mon Sep 17 00:00:00 2001 From: notsomeidiot123 Date: Mon, 6 Jul 2026 22:04:06 -0400 Subject: [PATCH] Traced memory corruption problem --- src/kernel/drivers/pci.c | 10 +++++----- src/kernel/kmain.c | 24 ++++++++++++++++-------- src/kernel/system/elf.c | 2 ++ src/kernel/system/intirc.c | 2 +- src/kernel/system/modules.c | 12 +----------- src/kernel/system/modules.h | 1 - src/kernel/system/ramfs.c | 5 ++++- src/kernel/system/vfs.c | 3 ++- src/kmodules/disk_driver.c | 22 ++++++++++++++++------ src/kmodules/fs_driver.c | 22 +++++++++++----------- src/kmodules/modlib.h | 35 +++++++++++++++++++++++++---------- 11 files changed, 83 insertions(+), 55 deletions(-) diff --git a/src/kernel/drivers/pci.c b/src/kernel/drivers/pci.c index ddef5e0..0a22eda 100644 --- a/src/kernel/drivers/pci.c +++ b/src/kernel/drivers/pci.c @@ -137,11 +137,11 @@ void pci_enumerate_bus(uint8_t bus){ void pci_init(){ mlog(MODULE_NAME, "Enumerating PCI Buses\n", MLOG_PRINT); - vfile_t *file = fcreate("dev/pci/", FS_FILE_IS_DIR); - fcreate("dev/pci/disk", FS_FILE_IS_DIR); - fcreate("dev/pci/net", FS_FILE_IS_DIR); - fcreate("dev/pci/video", FS_FILE_IS_DIR); - fcreate("dev/pci/bridge", FS_FILE_IS_DIR); + vfile_t *file = fcreate("/dev/pci/", FS_FILE_IS_DIR); + fcreate("/dev/pci/disk", FS_FILE_IS_DIR); + fcreate("/dev/pci/net", FS_FILE_IS_DIR); + fcreate("/dev/pci/video", FS_FILE_IS_DIR); + fcreate("/dev/pci/bridge", FS_FILE_IS_DIR); for(uint32_t i = 0 ; i < 256; i++){ pci_enumerate_bus(i); } diff --git a/src/kernel/kmain.c b/src/kernel/kmain.c index c987535..cb0b879 100644 --- a/src/kernel/kmain.c +++ b/src/kernel/kmain.c @@ -34,15 +34,23 @@ void sysinit(){ else{ mlog("KERNEL", "ERROR: Initrc could not be located!\n", MLOG_PRINT); } - // vfile_t *disk_dir = fget_file("/dev/disk"); - // if(disk_dir == 0){ - // mlog("KERNEL", "Error: /dev/disk does not exist\n", MLOG_PRINT); - // } - // vfile_t **dir_data = disk_dir->access.data.ptr; - // for(uint32_t i = 0; dir_data[i]; i++){ - // mlog("KERNEL", "Filename: %s\n", MLOG_PRINT, dir_data[i]->name); + + vfile_t *disk_dir = fopen("/dev/disk"); + if(disk_dir == 0){ + mlog("KERNEL", "Error: /dev/disk does not exist\n", MLOG_PRINT); + } + char *dir_data = kmalloc(1); + uint32_t byte_count = fread(disk_dir, dir_data, 0, 4096); + uint32_t file_count = byte_count/sizeof(vfile_t); + for(uint32_t i = 0; i < byte_count; i++){ + if(dir_data[i] > 32 && dir_data[i] < 127){ + printf("%c", dir_data[i]); + } + else{ + printf("%d", dir_data[i]); + } // vfs_detect_partitions(dir_data[i]); - // } + } // dispatch_message(0); printf("Bleh\n"); //why did i stop working on this? what was wrong with this? diff --git a/src/kernel/system/elf.c b/src/kernel/system/elf.c index 2fc29f6..935b894 100644 --- a/src/kernel/system/elf.c +++ b/src/kernel/system/elf.c @@ -14,6 +14,7 @@ void *load_segment(program_entry_t entry, void *file_data, void *base_segment, u // printf("Base: %x\n", base_segment); if(entry.type == 1){ if(((elf_header_t *)(file_data))->type == ELF_TYPE_SHARED){ + //Overwrites data when sees free memory block; re-write to allocate before call if(base_segment == 0){ segment = kmalloc(entry.msize/4096 + 1); base_segment = segment; @@ -23,6 +24,7 @@ void *load_segment(program_entry_t entry, void *file_data, void *base_segment, u uint32_t count = 1; count += ((entry.vaddr+entry.msize) - entry.vaddr) ? 1 : 0; count += entry.msize/4096 + 1; + printf("%x\n", segment); for(uint32_t i = 0; i < count; i++){ map(segment + (i << 12)/4, (void *)pm_alloc(), PT_PRESENT | map_flags); } diff --git a/src/kernel/system/intirc.c b/src/kernel/system/intirc.c index 2bfb974..bde4ffa 100644 --- a/src/kernel/system/intirc.c +++ b/src/kernel/system/intirc.c @@ -126,7 +126,7 @@ void initrc_read(vfile_t *file){ } if(!dispatch_message(MESSAGE_MOUNT_FS, to_mount, mount_dest, offset)){ mlog("INITRC", "Error: Could not mount device %s at %s", MLOG_PRINT, mount_src_name, mount_dest); - asm("int $13"); + // asm("int $13"); } } else if(!strcmp(statement, "END")){ diff --git a/src/kernel/system/modules.c b/src/kernel/system/modules.c index 2b914a0..253ae9c 100644 --- a/src/kernel/system/modules.c +++ b/src/kernel/system/modules.c @@ -79,9 +79,6 @@ uint32_t module_api(uint32_t func, ...){ case MODULE_API_CREAT: name = va_arg(vars, char *); FS_FILE_FLAGS ftype = va_arg(vars, FS_FILE_FLAGS); - void *arg1, *arg2; - arg1 = va_arg(vars, void *); - arg2 = va_arg(vars, void *); return_value = (uint32_t)fcreate(name, ftype); break; case MODULE_API_DELET: @@ -90,16 +87,9 @@ uint32_t module_api(uint32_t func, ...){ break; case MODULE_API_OPEN: name = va_arg(vars, char *); + printf("Requested file: %s\n", name); return_value = (uint32_t)fopen(name); break; - case MODULE_API_READDIR: - file = va_arg(vars, vfile_t *); - buffer = va_arg(vars, void *); - offset = va_arg(vars, uint32_t); - count = va_arg(vars, uint32_t); - // return_value = readdir(file, buffer, offset, count); - return_value = 0; - break; case MODULE_API_MAP: return_value = 0; void *vaddr = va_arg(vars, void *); diff --git a/src/kernel/system/modules.h b/src/kernel/system/modules.h index a42bd6f..4edefe3 100644 --- a/src/kernel/system/modules.h +++ b/src/kernel/system/modules.h @@ -24,7 +24,6 @@ enum MODULE_API_FUNCS{ MODULE_API_CREAT, //create a virtual file and assigns it to the the proper module (requires having a read and write function passed) MODULE_API_DELET, //delete a virtual file MODULE_API_OPEN, - MODULE_API_READDIR, MODULE_API_MAP, //map physical address to virtual address MODULE_API_UNMAP, //unmap physical address to virtual address MODULE_API_PADDR, //get physical address of memory diff --git a/src/kernel/system/ramfs.c b/src/kernel/system/ramfs.c index 576e726..24825ef 100644 --- a/src/kernel/system/ramfs.c +++ b/src/kernel/system/ramfs.c @@ -26,16 +26,19 @@ void ramfs_init(){ } vfile_t *search_dir(char *subname, vfile_t *directory){ + printf("Searching for: %s\n", subname); if(!directory) return 0; vfile_t **children = (vfile_t **)directory->private; if(!children){ return 0; } for(int i = 0; i < directory->size / sizeof(vfile_t *); i++){ + printf("Child name: %s\n", children[i]->name); if(children[i] && !strcmp(children[i]->name, subname)){ return children[i]; } } + return 0; } @@ -177,7 +180,7 @@ int ramfs_translate_dir(vfile_t *file, void *buffer, uint32_t offset, uint32_t c // printf("From: %x to %x\n", dirents[i], buffer + i * sizeof(vfile_t)); memcpy(dirents[i], buffer + i * sizeof(vfile_t), sizeof(vfile_t)); } - return count; + return to_copy * sizeof(vfile_t); } int ramfs_delete(vfile_t *file){ diff --git a/src/kernel/system/vfs.c b/src/kernel/system/vfs.c index 7d88bbe..73a0230 100644 --- a/src/kernel/system/vfs.c +++ b/src/kernel/system/vfs.c @@ -3,7 +3,6 @@ #include "../shared/kstdlib.h" #include "../shared/memory.h" #include "../shared/string.h" -#include "../shared/spinlock.h" #define MODULE_NAME "KVFS" // !TODO: Modify code to become thread-safe @@ -24,6 +23,7 @@ vfile_t *fcreate(char *path, FS_FILE_FLAGS flags){ // return 0; spinlock_acquire(vfs_lock); vfile_t *returnable = vfcreate(get_root_dir(), path, flags); + printf("Making file: %s, %x\n", path, returnable); spinlock_release(vfs_lock); return returnable; } @@ -116,6 +116,7 @@ vfile_t *fopen(char *name){ if(!strcmp(name, "/")){ return root; } + printf("Opening file: %s\n", name + (name[0] == '/')); return root->fileops->rfopen(name + (name[0] == '/'), root); } diff --git a/src/kmodules/disk_driver.c b/src/kmodules/disk_driver.c index b0f277f..c87f4bb 100644 --- a/src/kmodules/disk_driver.c +++ b/src/kmodules/disk_driver.c @@ -524,9 +524,17 @@ uint8_t ata_identify(uint32_t index, uint16_t disk){ drives[index].PRDT = (void *)api(MODULE_API_KMALLOC_PADDR, prdt_phys, 16); itoa(ata_drives, fname + strlen(fname), 10); vfile_t *new_file = fcreate(api, fname, FS_FILE_SYSTEM); - new_file->read = ata_read; - new_file->write = ata_write; - new_file->id = index; + api(MODULE_API_PRINT, MODULE_NAME, "New File: %x, Name: %s\n", new_file, new_file->name); + // + // + // + // RIGHT HERE + // + // + // + // new_file->read = ata_read; + // new_file->write = ata_write; + // new_file->id = index; puts(api, "KIDM", "Valid Drive!\n"); return 0; } @@ -599,8 +607,9 @@ void init(KOS_MAPI_FP module_api, uint32_t api_version){ api(MODULE_API_ADDINT, 15, module_data.key, int_handler); api(MODULE_API_ADDINT, 14, module_data.key, int_handler); // api(MODULE_API_ADDINT, 0, module_data.key, int_handler); - vfile_t *pci_drive_dir = fopen(api, "/dev/pci/disk/"); + vfile_t *pci_drive_dir = fopen(api, "/dev/pci/disk"); if(!pci_drive_dir){ + puts(api, MODULE_NAME, "Failed to open /dev/pci/disk\n"); return; } // vfile_t **dir_data = (pci_drive_dir->ptr); @@ -609,9 +618,10 @@ void init(KOS_MAPI_FP module_api, uint32_t api_version){ vfile_t *dir_data = malloc(api, (PCI_SEARCH_COUNT * sizeof(vfile_t) + 4095)/4096); - api(MODULE_API_READDIR, pci_drive_dir, dir_data, 0, 128); + uint32_t file_count = api(MODULE_API_READ, pci_drive_dir, dir_data, 0, PCI_SEARCH_COUNT * sizeof(vfile_t))/sizeof(vfile_t); + api(MODULE_API_PRINT, MODULE_NAME, "File count: %x\n", file_count); - for(uint32_t i = 0; PCI_SEARCH_COUNT; i++){ + for(uint32_t i = 0; i < file_count; i++){ vfile_t *current_file = &(dir_data[i]); uint32_t class = 0; fread(api, current_file, &class, 0x8, 1); diff --git a/src/kmodules/fs_driver.c b/src/kmodules/fs_driver.c index 62c0287..159e7bd 100644 --- a/src/kmodules/fs_driver.c +++ b/src/kmodules/fs_driver.c @@ -23,16 +23,16 @@ uint8_t fat32_check_valid(fat32_bpb_t *bpb){ uint32_t fat32_mount(vfile_t *dev_file, char *destination, uint32_t offset){ - char *bpb_buffer = malloc(api, 1); - fread(api, dev_file, bpb_buffer, offset, 4096); - fat32_bpb_t *bpb = bpb_buffer; + // char *bpb_buffer = malloc(api, 1); + // fread(api, dev_file, bpb_buffer, offset, 4096); + // fat32_bpb_t *bpb = bpb_buffer; - api(MODULE_API_PRINT, MODULE_NAME, "Sizeof struct: %d, sig: %x, boot sig: %x\n", sizeof(fat32_bpb_t), bpb->signature, bpb->bootable_sig); - if(!fat32_check_valid(bpb)){ - api(MODULE_API_PRINT, MODULE_NAME, "Error: No valid BPB\n"); - return 0; - } - puts(api, MODULE_NAME, "Valid BPB found!\n"); + // api(MODULE_API_PRINT, MODULE_NAME, "Sizeof struct: %d, sig: %x, boot sig: %x\n", sizeof(fat32_bpb_t), bpb->signature, bpb->bootable_sig); + // if(!fat32_check_valid(bpb)){ + // api(MODULE_API_PRINT, MODULE_NAME, "Error: No valid BPB\n"); + // return 0; + // } + // puts(api, MODULE_NAME, "Valid BPB found!\n"); // fat32_make_mount() @@ -59,8 +59,8 @@ void init(KOS_MAPI_FP module_api, uint32_t api_version){ api(MODULE_API_PRINT, MODULE_NAME, "KIFSM Filesystem Driver Module v0.1.0\nSupported Filesystems:\n"); int32_t status = api(MODULE_API_REGISTER, &module_data); - api(MODULE_MESSAGE_HANDLER, module_data.key, message_handler); - api(MODULE_API_PRINT, MODULE_NAME, "Key: %x\n", module_data.key); + // api(MODULE_MESSAGE_HANDLER, module_data.key, message_handler); + // api(MODULE_API_PRINT, MODULE_NAME, "Key: %x\n", module_data.key); return; } \ No newline at end of file diff --git a/src/kmodules/modlib.h b/src/kmodules/modlib.h index 7a1cd92..3837bb1 100644 --- a/src/kmodules/modlib.h +++ b/src/kmodules/modlib.h @@ -1,5 +1,6 @@ #pragma once #include +#include "../kernel/shared/spinlock.h" enum MODULE_API_FUNCS{ @@ -14,7 +15,6 @@ enum MODULE_API_FUNCS{ MODULE_API_CREAT, //create a virtual file and assigns it to the the proper module (requires having a read and write function passed) MODULE_API_DELET, //delete a virtual file MODULE_API_OPEN, - MODULE_API_READDIR, MODULE_API_MAP, //map physical address to virtual address MODULE_API_UNMAP, //unmap physical address to virtual address MODULE_API_PADDR, //get physical address of memory @@ -61,25 +61,40 @@ typedef enum fs_flags{ FS_FILE_IS_DIR = 0x10, FS_FILE_ARCHIVE = 0x20, FS_FILE_PIPE = 0x40, - FS_FILE_LINK = 0x80 + FS_FILE_LINK = 0x80, + FS_FILE_MOUNT = 0x100, // MUST be assigned to any file that represents a physical filesystem or physical device. }FS_FILE_FLAGS; -typedef struct virtual_file{ - char name[256]; - FS_FILE_FLAGS flags; +typedef struct fileops{ + struct virtual_file *(*create)(struct virtual_file *parent, char *path, FS_FILE_FLAGS flags); int (*delete)(struct virtual_file *file_entry); - struct virtual_file *(*create)(char *path, FS_FILE_FLAGS flags); int (*write)(struct virtual_file *file_entry, void *data, uint32_t offset, uint32_t count); int (*read)(struct virtual_file *file_entry, void *data, uint32_t offset, uint32_t count); - struct virtual_file *(*open)(char *path); - // struct virtual_file **(*lookup)(char *name); - int (*readdir)(struct virtual_file* file, struct virtual_file *buffer, uint32_t count, uint32_t offset); + // struct virtual_file *(*open)(char *path); + void (*close)(struct virtual_file *file); + // int (*readdir)(struct virtual_file* file, struct virtual_file *buffer, uint32_t count, uint32_t offset); + struct virtual_file *(*rfopen)(char *name, struct virtual_file *parent); +} fileops_t; + +//note: this is EXACTLY 256 bytes. This is for simplicity's sake. +//PLEASE if you MUST reorganize or add fields, try and keep it to a power of 2? +typedef struct virtual_file{ + char name[212]; + uint16_t flags; + fileops_t *fileops; + uint32_t refcount; //filesystem MUST remain operational until all child refcounts == 0 uint32_t id;//for use in drivers void *private; //also for use in drivers - uint32_t size; + uint32_t size; //should be in bytes uint32_t offset; //for use in drivers + uint8_t owner_uid; + uint8_t owner_gid; + uint32_t last_modified; + uint32_t created; + uint16_t permissions; //same format as linux + spinlock_t lock; }vfile_t; inline void *malloc(KOS_MAPI_FP api, uint32_t size_pages){