[GOOD] BUILD 0.1.0.450 DATE 8/29/2011 AT 10:30 AM

====================================================
+ Changed 'align 0x4' line above multiboot header in loader.asm to
'align 4'
+ Removed -e option for echo in build.sh
+ Modified build.sh for linux
+ Fixed triple fault when enabling paging
+ Fixed page faults at memory manager initialization
+ Fixed 'mem' console function
+ Added more info about page fault at crash screen
+ Added Panic() macro
+ Added verbose mode for memory manager

[ BAD] BUILD 0.1.0.390 DATE 8/27/2011 AT 10:54 PM
====================================================
+ Added stdlib routines, separated in different files
+ Rewritten physical memory manager
+ Added virtual mem manager
+ Added memory allocation/freeing
+ Added memory library
+ Added temporary allocation (at end of kernel), until paging is started
- Removed functionality from debug console function 'mem'
- Removed system.h, the one remaining function now in stdio.h
This commit is contained in:
2021-09-14 18:48:57 +03:00
parent b6ddeca1c3
commit 913e65b856
326 changed files with 6990 additions and 12229 deletions

View File

@ -0,0 +1,84 @@
/*
* memory_alloc.c
*
* Created on: Aug 27, 2011
* Author: Tiberiu
*/
#include <memory-add.h>
extern uint32 mem_kernel_end;
extern uint8 mem_initialized;
// Used prior to proper initialization
uint32 _malloc_init1 (uint32 size, uint8 page_aligned)
{
uint32 ret = mem_kernel_end;
if (page_aligned && (ret & 0xfff)) ret = (ret & 0xfffff000) + 0x1000;
mem_kernel_end = size + ret;
LogMem("%#Allocated %u bytes (%spage aligned) at end of kernel (0x%x).\n", ColorLightMagenta, size, ((page_aligned) ? "" : "not "), ret);
return (ret);
}
uint32 _malloc_init2 (uint32 size, uint8 page_aligned, uint32* phys)
{
uint32 ret = MemHeapAlloc(size, page_aligned, KernelHeap, KernelDirectory);
if (phys)
{
Page *pg = PagingGetPage(ret, 0, KernelDirectory);
*phys = (*pg & PageFrame) + (ret & 0xFFF);
LogMem("%#Allocated %u bytes (%spage aligned) at address 0x%x (phys=%x).\n", ColorLightMagenta, size, ((page_aligned) ? "" : "not "), ret, *phys);
}
else {
LogMem("%#Allocated %u bytes (%spage aligned) at address 0x%x.\n", ColorLightMagenta, size, ((page_aligned) ? "" : "not "), ret);
}
return ret;
}
// Allocate 'size' bytes
void* kmalloc (uint32 size)
{
if (!mem_initialized) return 0;
if (mem_initialized == 1) return (void*)_malloc_init1(size, 0);
return (void*)_malloc_init2(size,0,0);
}
// Allocate 'size' bytes, page aligned
void* kmalloc_a(uint32 size)
{
if (!mem_initialized) return 0;
if (mem_initialized == 1) return (void*)_malloc_init1(size, 1);
return (void*)_malloc_init2(size,1,0);
}
void* kmalloc_p(uint32 size, uint32* phys)
{
if (!mem_initialized) return 0;
if (mem_initialized == 1) {
*phys = _malloc_init1(size,0);
return (void*)(*phys);
}
return (void*)_malloc_init2(size,0,phys);
}
void* kmalloc_ap(uint32 size, uint32* phys)
{
if (!mem_initialized) return 0;
uint32 ret;
if (mem_initialized == 1) {
*phys = ret = _malloc_init1(size,1);
}
else ret = _malloc_init2(size,1,phys);
return (void*)ret;
}

View File

@ -0,0 +1,87 @@
/*
* memory_alloc.c
*
* Created on: Aug 27, 2011
* Author: Tiberiu
*/
#include <memory-add.h>
extern uint32 mem_kernel_end;
extern uint8 mem_initialized;
// Used prior to proper initialization
uint32 _malloc_init1 (uint32 size, uint8 page_aligned)
{
uint32 ret = mem_kernel_end;
if (page_aligned && (ret & 0xfff)) ret = (ret & 0xfffff000) + 0x1000;
mem_kernel_end = size + ret;
LogMem("%#Allocated %u bytes (%spage aligned) at end of kernel (0x%x).\n", ColorLightMagenta, size, ((page_aligned) ? "" : "not "), ret);
return (ret);
}
uint32 _malloc_init2 (uint32 size, uint8 page_aligned, uint32* phys)
{
uint32 ret = MemHeapAlloc(size, page_aligned, KernelHeap, KernelDirectory);
if (phys)
{
Page *pg = PagingGetPage(ret, 0, KernelDirectory);
*phys = (*pg & PageFrame) + (ret & 0xFFF);
LogMem("%#Allocated %u bytes (%spage aligned) at address 0x%x (phys=%x).\n", ColorLightMagenta, size, ((page_aligned) ? "" : "not "), ret, *phys);
}
else {
LogMem("%#Allocated %u bytes (%spage aligned) at address 0x%x.\n", ColorLightMagenta, size, ((page_aligned) ? "" : "not "), ret);
}
return ret;
}
// Allocate 'size' bytes
void* kmalloc (uint32 size)
{
if (!mem_initialized) return 0;
if (mem_initialized == 1) return (void*)_malloc_init1(size, 0);
return (void*)_malloc_init2(size,0,0);
}
// Allocate 'size' bytes, page aligned
void* kmalloc_a(uint32 size)
{
if (!mem_initialized) return 0;
if (mem_initialized == 1) return (void*)_malloc_init1(size, 1);
return (void*)_malloc_init2(size,1,0);
}
void* kmalloc_p(uint32 size, uint32* phys)
{
if (!mem_initialized) return 0;
if (mem_initialized == 1) {
*phys = _malloc_init1(size,0);
return (void*)(*phys);
}
return (void*)_malloc_init2(size,0,phys);
}
void* kmalloc_ap(uint32 size, uint32* phys)
{
if (!mem_initialized) return 0;
uint32 ret;
if (mem_initialized == 1) {
*phys = ret = _malloc_init1(size,1);
}
else ret = _malloc_init2(size,1,phys);
LogMem("%#kmalloc_ap requested, returned 0x%x, phys = 0x%x.\n", ColorMagenta, ret, *phys);
return (void*)ret;
}

View File

@ -0,0 +1,20 @@
/*
* memory_free.c
*
* Created on: Aug 27, 2011
* Author: Tiberiu
*/
#include <memory-add.h>
extern uint8 mem_initialized;
void kfree(void* addr)
{
if (mem_initialized < 2) {
ErrorMem("%#Tried to free at address 0x%x when memory manager is uninitialized.\n", ColorLightRed, (uint32)addr);
return;
}
MemHeapFree((uint32)addr, KernelHeap, KernelDirectory);
}

View File

@ -0,0 +1,48 @@
/*
* memory_info.c
*
* Created on: Aug 27, 2011
* Author: Tiberiu
*/
#include <memory-add.h>
// MemoryGetFree(), MemoryGetTotal(), MemoryGet blah blah...
// Returns total physical memory in bytes
uint32 MemoryGetTotal()
{
return (TotalMemory);
}
// Returns total free physical memory in bytes
uint32 MemoryGetFree()
{
return (TotalFrames - UsedFrames) * 0x4;
}
// Total used physical memory in bytes
uint32 MemoryGetUsed()
{
return UsedFrames * 0x4;
}
// Same as above functions, but in frames
uint32 MemoryGetFrameSize()
{
return 0x4;
}
uint32 MemoryGetFramesTotal()
{
return TotalFrames;
}
uint32 MemoryGetFramesUsed()
{
return UsedFrames;
}
uint32 MemoryGetFramesFree()
{
return (TotalFrames - UsedFrames);
}

View File

@ -0,0 +1,48 @@
/*
* memory_info.c
*
* Created on: Aug 27, 2011
* Author: Tiberiu
*/
#include <memory-add.h>
// MemoryGetFree(), MemoryGetTotal(), MemoryGet blah blah...
// Returns total physical memory in bytes
uint32 MemoryGetTotal()
{
return (TotalMemory);
}
// Returns total free physical memory in bytes
uint32 MemoryGetFree()
{
return (TotalFrames - UsedFrames) * 0x4;
}
// Total used physical memory in bytes
uint32 MemoryGetUsed()
{
return UsedFrames * 0x4;
}
// Same as above functions, but in frames
uint32 MemoryGetFrameSize()
{
return 0x1000;
}
uint32 MemoryGetFramesTotal()
{
return TotalFrames;
}
uint32 MemoryGetFramesUsed()
{
return UsedFrames;
}
uint32 MemoryGetFramesFree()
{
return (TotalFrames - UsedFrames);
}

View File

@ -0,0 +1,88 @@
/*
* memory-init.c
*
* Created on: Aug 27, 2011
* Author: Tiberiu
*/
#include <memory-add.h>
#include <multiboot.h>
#include <debugio.h>
#include "../../drivers/cmos/cmos.h"
#include <stdio.h>
uint32 mem_kernel_end = 0;
uint8 mem_initialized = 0;
uint32 _memory_get_total_mem(MultibootInfo* info)
{
// Grub was nice enough to give us info
if (info->Flags & MultibootInfo_MEMORY) return (1024 + info->MemoryUpper);
// No? Get info from CMOS
uint8 low, high;
uint32 total;
low = CmosRead(0x30);
high = CmosRead(0x31);
total = (uint32)(low | high<<8) + 1024;
ErrorMem("%#Missing memory info from bootloader. Reading from CMOS: %ukb\n", ColorLightRed, total);
return total;
}
void _memory_reserve_system(MultibootInfo* info)
{
MagicBreakpoint();
if ((info->Flags & MultibootInfo_MEM_MAP) != 0)
{
MultibootMemoryMapEntry* location = (MultibootMemoryMapEntry*)info->MemoryMapAddress;
while ((uint32)location < (info->MemoryMapAddress + info->MemoryMapLength))
{
if (location->Type > 1)
MemPhReserveFrames((uint32)location->Address, (uint32)location->Length);
location = (MultibootMemoryMapEntry*) ((uint32)location + location->Size + sizeof(uint32));
}
}
else
{
ErrorMem("%#Missing %#memory map%# info from bootloader.\n", ColorLightRed, ColorWhite, ColorLightRed);
// Standard memory hole at 15mb
MemPhReserveFrames(0x00F00000, 0x00100000);
}
// Standard reserved memory areas
MemPhReserveFrames(0x0, 0x400 + 256); // Real mode IVT, BDA
MemPhReserveFrames(0x1000, 0x2400); // DMA buffer
MemPhReserveFrames(0x9FC00, 385*1024); // EBDA, Video memory, ROM area
}
void MemoryInitialize (MultibootInfo* info)
{
uint32 totalSystemMemory = _memory_get_total_mem(info);
MemPhInitialize(totalSystemMemory);
PagingInitialize(0x200000);
_memory_reserve_system(info);
KernelHeap = MemHeapCreate(KERNEL_HEAP_START, KERNEL_HEAP_START
+ KERNEL_HEAP_INITIAL_SIZE, 0xCFFFF000, 3); // is kernel, writeable
LogMem("Done initializing memory!");
mem_initialized = 2;
}
void MemoryTempInitialize (uint32 kernel_end)
{
mem_initialized = 1;
mem_kernel_end = kernel_end;
LogMem("Initialized temporary memory manager, allocating from %#0x%x.\n", kernel_end);
}

View File

@ -0,0 +1,92 @@
/*
* memory-init.c
*
* Created on: Aug 27, 2011
* Author: Tiberiu
*/
#include <memory-add.h>
#include <multiboot.h>
#include <debugio.h>
#include "../../drivers/cmos/cmos.h"
#include <stdio.h>
uint32 mem_kernel_end = 0;
uint8 mem_initialized = 0;
uint32 _memory_get_total_mem(MultibootInfo* info)
{
// Grub was nice enough to give us info
if (info->Flags & MultibootInfo_MEMORY) return (1024 + info->MemoryUpper);
// No? Get info from CMOS
uint8 low, high;
uint32 total;
low = CmosRead(0x30);
high = CmosRead(0x31);
total = (uint32)(low | high<<8) + 1024;
ErrorMem("%#Missing memory info from bootloader. Reading from CMOS: %ukb\n", ColorLightRed, total);
return total;
}
void _memory_reserve_system(MultibootInfo* info)
{
MagicBreakpoint();
if ((info->Flags & MultibootInfo_MEM_MAP) != 0)
{
MultibootMemoryMapEntry* location = (MultibootMemoryMapEntry*)info->MemoryMapAddress;
while ((uint32)location < (info->MemoryMapAddress + info->MemoryMapLength))
{
if (location->Type > 1)
MemPhReserveFrames((uint32)location->Address, (uint32)location->Length);
location = (MultibootMemoryMapEntry*) ((uint32)location + location->Size + sizeof(uint32));
}
}
else
{
ErrorMem("%#Missing %#memory map%# info from bootloader.\n", ColorLightRed, ColorWhite, ColorLightRed);
// Standard memory hole at 15mb
MemPhReserveFrames(0x00F00000, 0x00100000);
}
// Standard reserved memory areas
MemPhReserveFrames(0x0, 0x400 + 256); // Real mode IVT, BDA
MemPhReserveFrames(0x1000, 0x2400); // DMA buffer
MemPhReserveFrames(0x9FC00, 385*1024); // EBDA, Video memory, ROM area
}
void MemoryInitialize (MultibootInfo* info)
{
uint32 totalSystemMemory = _memory_get_total_mem(info);
MemPhInitialize(totalSystemMemory);
PagingInitialize(0x200000);
LogMem("Reserving important areas...\n");
_memory_reserve_system(info);
LogMem("Allocating kernel heap...\n");
KernelHeap = MemHeapCreate(KERNEL_HEAP_START, KERNEL_HEAP_START
+ KERNEL_HEAP_INITIAL_SIZE, 0xCFFFF000, 3); // is kernel, writeable
LogMem("Done initializing memory!");
mem_initialized = 2;
}
void MemoryTempInitialize (uint32 kernel_end)
{
mem_initialized = 1;
mem_kernel_end = kernel_end;
LogMem("Initialized temporary memory manager, allocating from %#0x%x.\n", kernel_end);
}