2 Commits

Author SHA1 Message Date
8cf7fa7f31 Create pmm.c
Add implementation for the physical memory manager pmm
2026-01-28 10:21:46 -08:00
64d69d505f Create pmm.h 2026-01-28 10:14:01 -08:00
4 changed files with 123 additions and 101 deletions

View File

@@ -1,25 +0,0 @@
#ifndef PIC_H
#define PIC_H
#include <stdint.h>
/* I/O Ports for the PICs */
#define PIC1_COMMAND 0x20
#define PIC1_DATA 0x21
#define PIC2_COMMAND 0xA0
#define PIC2_DATA 0xA1
/* PIC Commands */
#define PIC_EOI 0x20 /* End of Interrupt */
/* Offset vectors for remapping */
#define PIC1_OFFSET 0x20
#define PIC2_OFFSET 0x28
void pic_init(void);
void pic_send_eoi(uint8_t irq);
void pic_mask(uint8_t irq);
void pic_unmask(uint8_t irq);
void pic_disable(void);
#endif

21
kernel/pmm.h Normal file
View File

@@ -0,0 +1,21 @@
#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

76
pic.c
View File

@@ -1,76 +0,0 @@
#include "pic.h"
#include "io.h"
/* Small delay for older hardware bus timing */
static inline void io_wait(void) {
outb(0x80, 0);
}
void pic_init(void) {
uint8_t a1, a2;
// Save current masks
a1 = inb(PIC1_DATA);
a2 = inb(PIC2_DATA);
// ICW1: Start initialization in cascade mode
outb(PIC1_COMMAND, 0x11);
io_wait();
outb(PIC2_COMMAND, 0x11);
io_wait();
// ICW2: Master PIC vector offset
outb(PIC1_DATA, PIC1_OFFSET);
io_wait();
// ICW2: Slave PIC vector offset
outb(PIC2_DATA, PIC2_OFFSET);
io_wait();
// ICW3: Tell Master there is a slave at IRQ2 (0000 0100)
outb(PIC1_DATA, 4);
io_wait();
// ICW3: Tell Slave its cascade identity (0000 0010)
outb(PIC2_DATA, 2);
io_wait();
// ICW4: Set 8086/88 mode
outb(PIC1_DATA, 0x01);
io_wait();
outb(PIC2_DATA, 0x01);
io_wait();
// Restore masks (or disable all to start clean)
outb(PIC1_DATA, 0xFB); // Keep IRQ2 (cascade) open
outb(PIC2_DATA, 0xFF);
}
void pic_send_eoi(uint8_t irq) {
if (irq >= 8) {
outb(PIC2_COMMAND, PIC_EOI);
}
outb(PIC1_COMMAND, PIC_EOI);
}
void pic_unmask(uint8_t irq) {
uint16_t port;
if (irq < 8) {
port = PIC1_DATA;
} else {
port = PIC2_DATA;
irq -= 8;
}
uint8_t value = inb(port) & ~(1 << irq);
outb(port, value);
}
void pic_mask(uint8_t irq) {
uint16_t port;
if (irq < 8) {
port = PIC1_DATA;
} else {
port = PIC2_DATA;
irq -= 8;
}
uint8_t value = inb(port) | (1 << irq);
outb(port, value);
}

102
pmm.c Normal file
View File

@@ -0,0 +1,102 @@
#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);
}