Traced memory corruption problem

This commit is contained in:
notsomeidiot123 2026-07-06 22:04:06 -04:00
parent 616edd6bb3
commit 10bb61b738
11 changed files with 83 additions and 55 deletions

View File

@ -137,11 +137,11 @@ void pci_enumerate_bus(uint8_t bus){
void pci_init(){ void pci_init(){
mlog(MODULE_NAME, "Enumerating PCI Buses\n", MLOG_PRINT); mlog(MODULE_NAME, "Enumerating PCI Buses\n", MLOG_PRINT);
vfile_t *file = fcreate("dev/pci/", 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/disk", FS_FILE_IS_DIR);
fcreate("dev/pci/net", FS_FILE_IS_DIR); fcreate("/dev/pci/net", FS_FILE_IS_DIR);
fcreate("dev/pci/video", FS_FILE_IS_DIR); fcreate("/dev/pci/video", FS_FILE_IS_DIR);
fcreate("dev/pci/bridge", FS_FILE_IS_DIR); fcreate("/dev/pci/bridge", FS_FILE_IS_DIR);
for(uint32_t i = 0 ; i < 256; i++){ for(uint32_t i = 0 ; i < 256; i++){
pci_enumerate_bus(i); pci_enumerate_bus(i);
} }

View File

@ -34,15 +34,23 @@ void sysinit(){
else{ else{
mlog("KERNEL", "ERROR: Initrc could not be located!\n", MLOG_PRINT); mlog("KERNEL", "ERROR: Initrc could not be located!\n", MLOG_PRINT);
} }
// vfile_t *disk_dir = fget_file("/dev/disk");
// if(disk_dir == 0){ vfile_t *disk_dir = fopen("/dev/disk");
// mlog("KERNEL", "Error: /dev/disk does not exist\n", MLOG_PRINT); 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++){ char *dir_data = kmalloc(1);
// mlog("KERNEL", "Filename: %s\n", MLOG_PRINT, dir_data[i]->name); 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]); // vfs_detect_partitions(dir_data[i]);
// } }
// dispatch_message(0); // dispatch_message(0);
printf("Bleh\n"); printf("Bleh\n");
//why did i stop working on this? what was wrong with this? //why did i stop working on this? what was wrong with this?

View File

@ -14,6 +14,7 @@ void *load_segment(program_entry_t entry, void *file_data, void *base_segment, u
// printf("Base: %x\n", base_segment); // printf("Base: %x\n", base_segment);
if(entry.type == 1){ if(entry.type == 1){
if(((elf_header_t *)(file_data))->type == ELF_TYPE_SHARED){ 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){ if(base_segment == 0){
segment = kmalloc(entry.msize/4096 + 1); segment = kmalloc(entry.msize/4096 + 1);
base_segment = segment; 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; uint32_t count = 1;
count += ((entry.vaddr+entry.msize) - entry.vaddr) ? 1 : 0; count += ((entry.vaddr+entry.msize) - entry.vaddr) ? 1 : 0;
count += entry.msize/4096 + 1; count += entry.msize/4096 + 1;
printf("%x\n", segment);
for(uint32_t i = 0; i < count; i++){ for(uint32_t i = 0; i < count; i++){
map(segment + (i << 12)/4, (void *)pm_alloc(), PT_PRESENT | map_flags); map(segment + (i << 12)/4, (void *)pm_alloc(), PT_PRESENT | map_flags);
} }

View File

@ -126,7 +126,7 @@ void initrc_read(vfile_t *file){
} }
if(!dispatch_message(MESSAGE_MOUNT_FS, to_mount, mount_dest, offset)){ 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); 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")){ else if(!strcmp(statement, "END")){

View File

@ -79,9 +79,6 @@ uint32_t module_api(uint32_t func, ...){
case MODULE_API_CREAT: case MODULE_API_CREAT:
name = va_arg(vars, char *); name = va_arg(vars, char *);
FS_FILE_FLAGS ftype = va_arg(vars, FS_FILE_FLAGS); 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); return_value = (uint32_t)fcreate(name, ftype);
break; break;
case MODULE_API_DELET: case MODULE_API_DELET:
@ -90,16 +87,9 @@ uint32_t module_api(uint32_t func, ...){
break; break;
case MODULE_API_OPEN: case MODULE_API_OPEN:
name = va_arg(vars, char *); name = va_arg(vars, char *);
printf("Requested file: %s\n", name);
return_value = (uint32_t)fopen(name); return_value = (uint32_t)fopen(name);
break; 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: case MODULE_API_MAP:
return_value = 0; return_value = 0;
void *vaddr = va_arg(vars, void *); void *vaddr = va_arg(vars, void *);

View File

@ -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_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_DELET, //delete a virtual file
MODULE_API_OPEN, MODULE_API_OPEN,
MODULE_API_READDIR,
MODULE_API_MAP, //map physical address to virtual address MODULE_API_MAP, //map physical address to virtual address
MODULE_API_UNMAP, //unmap physical address to virtual address MODULE_API_UNMAP, //unmap physical address to virtual address
MODULE_API_PADDR, //get physical address of memory MODULE_API_PADDR, //get physical address of memory

View File

@ -26,16 +26,19 @@ void ramfs_init(){
} }
vfile_t *search_dir(char *subname, vfile_t *directory){ vfile_t *search_dir(char *subname, vfile_t *directory){
printf("Searching for: %s\n", subname);
if(!directory) return 0; if(!directory) return 0;
vfile_t **children = (vfile_t **)directory->private; vfile_t **children = (vfile_t **)directory->private;
if(!children){ if(!children){
return 0; return 0;
} }
for(int i = 0; i < directory->size / sizeof(vfile_t *); i++){ 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)){ if(children[i] && !strcmp(children[i]->name, subname)){
return children[i]; return children[i];
} }
} }
return 0; 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)); // printf("From: %x to %x\n", dirents[i], buffer + i * sizeof(vfile_t));
memcpy(dirents[i], buffer + i * sizeof(vfile_t), 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){ int ramfs_delete(vfile_t *file){

View File

@ -3,7 +3,6 @@
#include "../shared/kstdlib.h" #include "../shared/kstdlib.h"
#include "../shared/memory.h" #include "../shared/memory.h"
#include "../shared/string.h" #include "../shared/string.h"
#include "../shared/spinlock.h"
#define MODULE_NAME "KVFS" #define MODULE_NAME "KVFS"
// !TODO: Modify code to become thread-safe // !TODO: Modify code to become thread-safe
@ -24,6 +23,7 @@ vfile_t *fcreate(char *path, FS_FILE_FLAGS flags){
// return 0; // return 0;
spinlock_acquire(vfs_lock); spinlock_acquire(vfs_lock);
vfile_t *returnable = vfcreate(get_root_dir(), path, flags); vfile_t *returnable = vfcreate(get_root_dir(), path, flags);
printf("Making file: %s, %x\n", path, returnable);
spinlock_release(vfs_lock); spinlock_release(vfs_lock);
return returnable; return returnable;
} }
@ -116,6 +116,7 @@ vfile_t *fopen(char *name){
if(!strcmp(name, "/")){ if(!strcmp(name, "/")){
return root; return root;
} }
printf("Opening file: %s\n", name + (name[0] == '/'));
return root->fileops->rfopen(name + (name[0] == '/'), root); return root->fileops->rfopen(name + (name[0] == '/'), root);
} }

View File

@ -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); drives[index].PRDT = (void *)api(MODULE_API_KMALLOC_PADDR, prdt_phys, 16);
itoa(ata_drives, fname + strlen(fname), 10); itoa(ata_drives, fname + strlen(fname), 10);
vfile_t *new_file = fcreate(api, fname, FS_FILE_SYSTEM); vfile_t *new_file = fcreate(api, fname, FS_FILE_SYSTEM);
new_file->read = ata_read; api(MODULE_API_PRINT, MODULE_NAME, "New File: %x, Name: %s\n", new_file, new_file->name);
new_file->write = ata_write; //
new_file->id = index; //
//
// RIGHT HERE
//
//
//
// new_file->read = ata_read;
// new_file->write = ata_write;
// new_file->id = index;
puts(api, "KIDM", "Valid Drive!\n"); puts(api, "KIDM", "Valid Drive!\n");
return 0; 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, 15, module_data.key, int_handler);
api(MODULE_API_ADDINT, 14, 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); // 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){ if(!pci_drive_dir){
puts(api, MODULE_NAME, "Failed to open /dev/pci/disk\n");
return; return;
} }
// vfile_t **dir_data = (pci_drive_dir->ptr); // 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); 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]); vfile_t *current_file = &(dir_data[i]);
uint32_t class = 0; uint32_t class = 0;
fread(api, current_file, &class, 0x8, 1); fread(api, current_file, &class, 0x8, 1);

View File

@ -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){ uint32_t fat32_mount(vfile_t *dev_file, char *destination, uint32_t offset){
char *bpb_buffer = malloc(api, 1); // char *bpb_buffer = malloc(api, 1);
fread(api, dev_file, bpb_buffer, offset, 4096); // fread(api, dev_file, bpb_buffer, offset, 4096);
fat32_bpb_t *bpb = bpb_buffer; // 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); // 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)){ // if(!fat32_check_valid(bpb)){
api(MODULE_API_PRINT, MODULE_NAME, "Error: No valid BPB\n"); // api(MODULE_API_PRINT, MODULE_NAME, "Error: No valid BPB\n");
return 0; // return 0;
} // }
puts(api, MODULE_NAME, "Valid BPB found!\n"); // puts(api, MODULE_NAME, "Valid BPB found!\n");
// fat32_make_mount() // 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"); 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); int32_t status = api(MODULE_API_REGISTER, &module_data);
api(MODULE_MESSAGE_HANDLER, module_data.key, message_handler); // api(MODULE_MESSAGE_HANDLER, module_data.key, message_handler);
api(MODULE_API_PRINT, MODULE_NAME, "Key: %x\n", module_data.key); // api(MODULE_API_PRINT, MODULE_NAME, "Key: %x\n", module_data.key);
return; return;
} }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include "../kernel/shared/spinlock.h"
enum MODULE_API_FUNCS{ 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_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_DELET, //delete a virtual file
MODULE_API_OPEN, MODULE_API_OPEN,
MODULE_API_READDIR,
MODULE_API_MAP, //map physical address to virtual address MODULE_API_MAP, //map physical address to virtual address
MODULE_API_UNMAP, //unmap physical address to virtual address MODULE_API_UNMAP, //unmap physical address to virtual address
MODULE_API_PADDR, //get physical address of memory MODULE_API_PADDR, //get physical address of memory
@ -61,25 +61,40 @@ typedef enum fs_flags{
FS_FILE_IS_DIR = 0x10, FS_FILE_IS_DIR = 0x10,
FS_FILE_ARCHIVE = 0x20, FS_FILE_ARCHIVE = 0x20,
FS_FILE_PIPE = 0x40, 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; }FS_FILE_FLAGS;
typedef struct virtual_file{ typedef struct fileops{
char name[256]; struct virtual_file *(*create)(struct virtual_file *parent, char *path, FS_FILE_FLAGS flags);
FS_FILE_FLAGS flags;
int (*delete)(struct virtual_file *file_entry); 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 (*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); int (*read)(struct virtual_file *file_entry, void *data, uint32_t offset, uint32_t count);
struct virtual_file *(*open)(char *path); // struct virtual_file *(*open)(char *path);
// struct virtual_file **(*lookup)(char *name); void (*close)(struct virtual_file *file);
int (*readdir)(struct virtual_file* file, struct virtual_file *buffer, uint32_t count, uint32_t offset); // 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 uint32_t id;//for use in drivers
void *private; //also 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 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; }vfile_t;
inline void *malloc(KOS_MAPI_FP api, uint32_t size_pages){ inline void *malloc(KOS_MAPI_FP api, uint32_t size_pages){