mirror of
https://github.com/gbowne1/ClassicOS.git
synced 2026-03-09 16:35:21 -07:00
Compare commits
2 Commits
gbowne1-ad
...
gbowne1-ad
| Author | SHA1 | Date | |
|---|---|---|---|
| 95d2cc52ed | |||
| 679e6101e0 |
21
kernel/pmm.h
21
kernel/pmm.h
@@ -1,21 +0,0 @@
|
|||||||
#ifndef PMM_H
|
|
||||||
#define PMM_H
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include "memmap.h"
|
|
||||||
#include "paging.h" // For PAGE_SIZE
|
|
||||||
|
|
||||||
#define BLOCKS_PER_BYTE 8
|
|
||||||
|
|
||||||
void pmm_init(memory_map_entry_t* mmap, uint32_t mmap_size, uintptr_t bitmap_addr);
|
|
||||||
void pmm_mark_used(uintptr_t addr);
|
|
||||||
void pmm_mark_free(uintptr_t addr);
|
|
||||||
|
|
||||||
void* pmm_alloc_block();
|
|
||||||
void pmm_free_block(void* addr);
|
|
||||||
|
|
||||||
uint32_t pmm_get_used_block_count();
|
|
||||||
uint32_t pmm_get_free_block_count();
|
|
||||||
|
|
||||||
#endif
|
|
||||||
19
kernel/vfs.c
Normal file
19
kernel/vfs.c
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#include "vfs.h"
|
||||||
|
#include "kmalloc.h"
|
||||||
|
#include "string_utils.h"
|
||||||
|
|
||||||
|
vfs_node_t* vfs_root = NULL;
|
||||||
|
|
||||||
|
uint32_t vfs_read(vfs_node_t* node, uint32_t offset, uint32_t size, uint8_t* buffer) {
|
||||||
|
if (node->read != NULL) {
|
||||||
|
return node->read(node, offset, size, buffer);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
vfs_node_t* vfs_finddir(vfs_node_t* node, const char* name) {
|
||||||
|
if ((node->flags & VFS_DIRECTORY) && node->finddir != NULL) {
|
||||||
|
return node->finddir(node, name);
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
42
kernel/vfs.h
Normal file
42
kernel/vfs.h
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#ifndef VFS_H
|
||||||
|
#define VFS_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
struct vfs_node;
|
||||||
|
|
||||||
|
// Function pointers that every FS driver must provide
|
||||||
|
typedef uint32_t (*vfs_read_func)(struct vfs_node*, uint32_t, uint32_t, uint8_t*);
|
||||||
|
typedef struct vfs_node* (*vfs_finddir_func)(struct vfs_node*, const char* name);
|
||||||
|
|
||||||
|
typedef struct vfs_node {
|
||||||
|
char name[128];
|
||||||
|
uint32_t mask; // Permissions
|
||||||
|
uint32_t uid; // User ID
|
||||||
|
uint32_t gid; // Group ID
|
||||||
|
uint32_t flags; // Node type (File, Directory, etc.)
|
||||||
|
uint32_t inode; // FS-specific identifier
|
||||||
|
uint32_t length; // Size in bytes
|
||||||
|
|
||||||
|
// Function table
|
||||||
|
vfs_read_func read;
|
||||||
|
vfs_finddir_func finddir;
|
||||||
|
|
||||||
|
struct vfs_node* ptr; // Used for mountpoints or symlinks
|
||||||
|
} vfs_node_t;
|
||||||
|
|
||||||
|
// Standard node types
|
||||||
|
#define VFS_FILE 0x01
|
||||||
|
#define VFS_DIRECTORY 0x02
|
||||||
|
#define VFS_MOUNTPOINT 0x04
|
||||||
|
|
||||||
|
// The root of the filesystem (/)
|
||||||
|
extern vfs_node_t* vfs_root;
|
||||||
|
|
||||||
|
// Public API
|
||||||
|
uint32_t vfs_read(vfs_node_t* node, uint32_t offset, uint32_t size, uint8_t* buffer);
|
||||||
|
vfs_node_t* vfs_finddir(vfs_node_t* node, const char* name);
|
||||||
|
|
||||||
|
#endif
|
||||||
102
pmm.c
102
pmm.c
@@ -1,102 +0,0 @@
|
|||||||
#include "pmm.h"
|
|
||||||
#include "memory.h" // For memset
|
|
||||||
|
|
||||||
static uint32_t* pmm_bitmap = NULL;
|
|
||||||
static uint32_t max_blocks = 0;
|
|
||||||
static uint32_t used_blocks = 0;
|
|
||||||
|
|
||||||
// Internal bitmap helpers
|
|
||||||
static inline void bitmap_set(uint32_t bit) {
|
|
||||||
pmm_bitmap[bit / 32] |= (1 << (bit % 32));
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void bitmap_unset(uint32_t bit) {
|
|
||||||
pmm_bitmap[bit / 32] &= ~(1 << (bit % 32));
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int bitmap_test(uint32_t bit) {
|
|
||||||
return pmm_bitmap[bit / 32] & (1 << (bit % 32));
|
|
||||||
}
|
|
||||||
|
|
||||||
void pmm_init(memory_map_entry_t* mmap, uint32_t mmap_size, uintptr_t bitmap_addr) {
|
|
||||||
// 1. Calculate total memory from mmap to find max_blocks
|
|
||||||
uint64_t total_mem = 0;
|
|
||||||
for (uint32_t i = 0; i < mmap_size; i++) {
|
|
||||||
if (mmap[i].type == 1) { // Available RAM
|
|
||||||
total_mem = mmap[i].base_addr + mmap[i].length;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
max_blocks = (uint32_t)(total_mem / PAGE_SIZE);
|
|
||||||
used_blocks = max_blocks;
|
|
||||||
pmm_bitmap = (uint32_t*)bitmap_addr;
|
|
||||||
|
|
||||||
// 2. Default all memory to "Reserved" (1s)
|
|
||||||
memset(pmm_bitmap, 0xFF, max_blocks / BLOCKS_PER_BYTE);
|
|
||||||
|
|
||||||
// 3. Mark only the regions reported as Type 1 (Available) as free (0s)
|
|
||||||
for (uint32_t i = 0; i < mmap_size; i++) {
|
|
||||||
if (mmap[i].type == 1) {
|
|
||||||
uint32_t start_block = (uint32_t)(mmap[i].base_addr / PAGE_SIZE);
|
|
||||||
uint32_t block_count = (uint32_t)(mmap[i].length / PAGE_SIZE);
|
|
||||||
|
|
||||||
for (uint32_t j = 0; j < block_count; j++) {
|
|
||||||
bitmap_unset(start_block + j);
|
|
||||||
used_blocks--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 4. Critical: Re-protect the first 1MB (BIOS/VGA/Real Mode stuff)
|
|
||||||
for (uint32_t i = 0; i < (1024 * 1024) / PAGE_SIZE; i++) {
|
|
||||||
pmm_mark_used(i * PAGE_SIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 5. Critical: Re-protect the Kernel + Page Tables
|
|
||||||
// Since your paging tables are at 0x200000 and linker at 1MB,
|
|
||||||
// mark everything from 0x100000 to roughly 0x400000 as used for safety.
|
|
||||||
for (uint32_t i = 0x100000 / PAGE_SIZE; i < 0x400000 / PAGE_SIZE; i++) {
|
|
||||||
pmm_mark_used(i * PAGE_SIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 6. Protect the bitmap itself
|
|
||||||
uint32_t bitmap_size_blocks = (max_blocks / BLOCKS_PER_BYTE) / PAGE_SIZE + 1;
|
|
||||||
for(uint32_t i = 0; i < bitmap_size_blocks; i++) {
|
|
||||||
pmm_mark_used(bitmap_addr + (i * PAGE_SIZE));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void pmm_mark_used(uintptr_t addr) {
|
|
||||||
uint32_t block = addr / PAGE_SIZE;
|
|
||||||
if (!bitmap_test(block)) {
|
|
||||||
bitmap_set(block);
|
|
||||||
used_blocks++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void pmm_mark_free(uintptr_t addr) {
|
|
||||||
uint32_t block = addr / PAGE_SIZE;
|
|
||||||
if (bitmap_test(block)) {
|
|
||||||
bitmap_unset(block);
|
|
||||||
used_blocks--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void* pmm_alloc_block() {
|
|
||||||
for (uint32_t i = 0; i < max_blocks / 32; i++) {
|
|
||||||
if (pmm_bitmap[i] != 0xFFFFFFFF) {
|
|
||||||
for (int j = 0; j < 32; j++) {
|
|
||||||
if (!bitmap_test(i * 32 + j)) {
|
|
||||||
uint32_t addr = (i * 32 + j) * PAGE_SIZE;
|
|
||||||
pmm_mark_used(addr);
|
|
||||||
return (void*)addr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NULL; // OOM
|
|
||||||
}
|
|
||||||
|
|
||||||
void pmm_free_block(void* addr) {
|
|
||||||
pmm_mark_free((uintptr_t)addr);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user