3.0 KiB
3.0 KiB
Assignment 4 – Part 1: Memory Management
Overview
In this part of the assignment, a basic memory management system was implemented for a simple OS kernel. The goal was to initialize kernel memory, enable paging, and implement dynamic memory allocation using malloc() and free().
Implemented Features
Kernel Memory Initialization
- Used:
to get the end address of the kernel from the linker.
extern uint32_t end; - Implemented:
init_kernel_memory(&end); - Set up:
heap_beginheap_endpheap_begin(page-aligned heap)pheap_end
Paging
- Implemented:
init_paging(); - Set up:
- Page directory at
0x400000 - Page tables starting at
0x404000
- Page directory at
- Identity-mapped:
0x00000000 → 0x000000000x400000 → 0x400000
- Enabled paging by modifying
cr0andcr3.
Dynamic Memory Allocation
malloc()
- Implemented a simple heap allocator.
- Stores metadata using:
typedef struct { uint8_t status; uint32_t size; } alloc_t; - Supports:
- Sequential allocation
- Reuse of freed blocks
- Zeroes allocated memory using
memset.
free()
- Frees memory by marking the block as unused.
- Prevents errors by:
if (!mem) return; - Correctly updates
memory_used.
Block Reuse
- When a block is freed, it can be reused by future
malloc()calls. - Verified by:
- Freeing a block
- Allocating a smaller block
- Observing same address reused
Page-Aligned Allocation (pmalloc)
- Implemented
pmalloc()andpfree():- Allocates memory in 4KB pages
- Uses a descriptor array (
pheap_desc)
- Located in upper memory region near
0x400000
Memory Utility Functions
Implemented:
memcpy()memset()memset16()
These replace standard library functions in kernel space.
Debug Output
Memory Layout
- Implemented:
print_memory_layout(); - Displays:
- Memory used
- Memory free
- Heap size
- Heap start/end
- Page heap start/end
Allocation Logs
- Outputs allocation details:
Allocated X bytes from 0x... to 0x... Re-allocated X bytes from 0x... to 0x...
Hex Output for Addresses
- Implemented:
TerminalWriteHex(uint32_t num); - Ensures correct formatting of memory addresses (base 16)
- Avoids confusion from decimal output
Testing & Verification
Allocation Test
void* memory1 = malloc(48261);
void* memory2 = malloc(27261);
void* memory3 = malloc(12617);
Free and Reuse Test
free(memory2);
void* memory4 = malloc(1000);
Result
memory4reused the same address asmemory2- Confirms:
free()works- allocator reuses memory correctly
Conclusion
- Successfully implemented a basic kernel memory manager
- Paging is enabled and functioning
malloc()andfree()work correctly- Memory reuse is verified
- Debug output is clear and correctly formatted
Next Step
Proceed to Part 2: PIT and Sleep Functions