From e83408ce83fc3c49fd104c13e6e7566c9db8ed4d Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 29 Dec 2003 22:00:00 -0500 Subject: [PATCH] Import backup from 2003-12-29 --- kernel.c | 9 ++++---- vmm.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- vmm.h | 27 +++++++++++++++++++++++- 3 files changed, 90 insertions(+), 8 deletions(-) diff --git a/kernel.c b/kernel.c index 91960ff..e28fe35 100644 --- a/kernel.c +++ b/kernel.c @@ -62,16 +62,15 @@ void k_init() kbd_resetLEDs(); //after enabling interrupts!! if (videoMode) { - int x, y; - for (x = 0; x < video_mode.XResolution; x++) - for (y = 0; y < video_mode.YResolution; y++) - video_pset(x, y, 0x00000044); + int p = video_mode.XResolution*video_mode.YResolution-1; + for (; p >= 0; p--) + video_psetp(p, 0x00000044); video_drawConsole(); } printf("HOS 0.13 - Kernel Size: %u kb\n", kernel_size()>>10); printf("Memory available to OS: %u MB (Bytes: %u)\n", mm_megabytes, mm_totalmem); - printf("Free memory: %u bytes\t %u pages\n", mm_freemem(), mm_freemem()>>12); + printf("Free memory: %u bytes (%u pages)\n", mm_freemem(), mm_freemem()>>12); dword key = 0; for (;;) diff --git a/vmm.c b/vmm.c index bb66ba3..f748317 100644 --- a/vmm.c +++ b/vmm.c @@ -18,9 +18,34 @@ void vmm_init() vidPages = (vidPages >> 12); vmm_mapn(0xF0000000, video_mode.PhysBasePtr, vidPages); } + dword firstHeapEntryBlock = (dword)mm_palloc(); + vmm_map1((dword)firstHeapEntry, firstHeapEntryBlock); + HeapEntryBlock *heb = (HeapEntryBlock *)firstHeapEntry; + vmm_heb_init(heb); + heb->entry[0].base = (dword)firstHeapEntry; //start of kernel's heap memory + heb->entry[0].size = 4096; //the first HeapEntryBlock is 1 page long + heb->entry[0].attributes = VMM_HE_HEB; //this is a HeapEntryBlock + heb->entry[1].base = (dword)firstHeapEntry+4096; //this is the start of the rest of the kernel's heap memory + heb->entry[1].size = 0x10000000-4096; //the rest of the kernel's heap memory: 256mb - 4kb + heb->entry[1].attributes = VMM_HE_HOLE; //this is a hold - an unmapped section of heap memory } +void vmm_heb_init(HeapEntryBlock *heb) +{ + int a; + for (a = 0; a < 256; a++) + { + heb->entry[a].base = 0; + heb->entry[a].size = 0; + heb->entry[a].attributes = VMM_HE_UNUSED; + heb->entry[a].link = (dword)&(heb->entry[a+1]); + } + heb->entry[255].link = 0; +} + + + void vmm_map1(dword virt, dword physical) { dword pde = virt >> 22; @@ -40,6 +65,7 @@ void vmm_map1(dword virt, dword physical) } dword *pteptr = (dword *)(0xFFC00000 | (pde << 12) | (pte << 2)); *pteptr = physical | 0x03; + invlpg(virt); } @@ -54,11 +80,43 @@ void vmm_mapn(dword virt, dword physical, dword n) } +void vmm_unmap1(dword virt) +{ + dword *pteptr = (dword *)(0xFFC00000 | ((virt & 0xFFC00000) >> 10) | ((virt & 0x003FF000) >> 10)); + *pteptr = 0; + invlpg(virt); +} + + +void vmm_unmapn(dword virt, dword n) +{ + for (; n > 0; n--) + { + vmm_unmap1(virt); + virt += 4096; + } +} + void *malloc(dword bytes) { - - + HeapEntry *he = firstHeapEntry; + for (;;) + { + if ((he->size >= bytes) && (he->attributes = VMM_HE_FREE)) + { + if (he->size == bytes) + { + he->attributes = VMM_HE_USED; + return (void *)(he->base); + } + else + { + //get the next HeapEntry, save in it the free mem, save in this entry the used mem... + } + } + } + return 0; } diff --git a/vmm.h b/vmm.h index ca3f890..a94b66e 100644 --- a/vmm.h +++ b/vmm.h @@ -1,16 +1,41 @@ - // vmm.h // Author: Josh Holtrop // Date: 09/30/03 // Rewritten from scratch: 12/23/03 +// Modified: 12/29/03 +#define VMM_HE_UNUSED 0 //available entry +#define VMM_HE_FREE 1 //free section of memory +#define VMM_HE_USED 2 //used section of memory +#define VMM_HE_HOLE 3 //a "hole" (unmapped) section of virtual memory +#define VMM_HE_HEB 4 //HeapEntryBlock + + +typedef struct { + dword base; //virtual base address + dword size; //size in bytes + dword attributes; //free/used/hole + dword link; //link to next HeapEntry +} __attribute__((packed)) HeapEntry; + + +typedef struct { + HeapEntry entry[256]; //256 HeapEntry objects = 4kb (1 page) +} __attribute__((packed)) HeapEntryBlock; + void vmm_init(); void *malloc(dword bytes); int free(void *ptr); void vmm_map1(dword virt, dword physical); void vmm_mapn(dword virt, dword physical, dword n); +void vmm_unmap1(dword virt); +void vmm_unmapn(dword virt, dword n); +void vmm_heb_init(HeapEntryBlock *heb); + + +HeapEntry *firstHeapEntry = (HeapEntry *)0xD0000000;