diff --git a/asmfuncs.asm b/asmfuncs.asm index 10bf6aa..df77160 100644 --- a/asmfuncs.asm +++ b/asmfuncs.asm @@ -40,6 +40,13 @@ _read_cr3: ret +[global _invlpg] +_invlpg: + mov eax, [esp+4] + invlpg [eax] + ret + + diff --git a/kernel.c b/kernel.c index b4ef426..3b155ce 100644 --- a/kernel.c +++ b/kernel.c @@ -33,6 +33,7 @@ extern dword read_cr3(); #include "video.c" dword timer = 0; +dword callnum = 0; //Main kernel initialization method void k_init() @@ -53,7 +54,23 @@ void k_init() printf("HOS 0.12 - Kernel Size: %u kb\n", kernel_size()/1024); printf("Memory available to OS: %u MB (Bytes: %u)\n", mm_megabytes, mm_totalmem); - printf("Free memory: %u bytes\n", mm_freemem()); + printf("Free memory: %u bytes\t %u pages\n", mm_freemem(), mm_freemem()>>12); + + if (videoMode) + { + dword p; + for (p = 0; p < video_mode.XResolution*video_mode.YResolution; p++) + video_psetp(p, 0x00000088); + } + + for (;;) + { + dword addr = (dword)mm_palloc(); + callnum++; + printf("addr = 0x%x\tcallnum = %u\tfree pages = %u\n", addr, callnum, mm_freemem()>>12); + if (!addr) + break; + } dword key = 0; for (;;) diff --git a/mm.c b/mm.c index 85ae7db..6bae6c5 100644 --- a/mm.c +++ b/mm.c @@ -76,6 +76,7 @@ void *mm_palloc() { if (!(page_bitmap[bite] & (1 << bit))) //this bite/bit combination is available { + page_bitmap[bite] = page_bitmap[bite] | (1 << bit); //set page as used return (void *)((bite << 15) + (bit << 12)); } } diff --git a/video.c b/video.c index a922527..4e70045 100644 --- a/video.c +++ b/video.c @@ -19,16 +19,13 @@ void video_init() switch(video_mode.BitsPerPixel) { case 16: - vid_ptr16 = (word *) video_mode.PhysBasePtr; video_psetp = &video_psetp16; break; case 24: - vid_ptr24 = (byte *) video_mode.PhysBasePtr; video_psetp = &video_psetp24; break; case 32: video_psetp = &video_psetp32; - vid_ptr32 = (dword *) video_mode.PhysBasePtr; } } @@ -148,7 +145,9 @@ void video_psetp32(int pixel, dword color) } //Dummy function to not draw anything if there is no graphical mode enabled -void video_psetpnull(int pixel, dword color) -{ -} +void video_psetpnull(int pixel, dword color) {} + + + + diff --git a/video.h b/video.h index f30028b..edaab47 100644 --- a/video.h +++ b/video.h @@ -54,10 +54,10 @@ typedef struct{ } ModeInfoBlock; ModeInfoBlock video_mode; -dword videoMode; //what video mode # we are in, 0 for console mode -byte *vid_ptr24; -word *vid_ptr16; -dword *vid_ptr32; +dword videoMode = 0; //what video mode # we are in, 0 for console mode +word *vid_ptr16 = (word *)0xF0000000; +byte *vid_ptr24 = (byte *)0xF0000000; +dword *vid_ptr32 = (dword *)0xF0000000; void (*video_psetp)(int, dword); //function pointer to set a pixel diff --git a/vmm.c b/vmm.c index 3669a97..bb66ba3 100644 --- a/vmm.c +++ b/vmm.c @@ -7,15 +7,52 @@ void vmm_init() { - dword *pagetables = (dword *)0xC0104000; //this is the location of the page directory - pagetables[0x3FF] = 0x104000|0x03; //the last page directory entry points to the page directory itself + dword *pageTables = (dword *)0xC0104000; //this is the location of the page directory + pageTables[0x3FF] = 0x104000|0x03; //the last page directory entry points to the page directory itself if (videoMode) //we are in a graphical mode { - + dword vidPages = video_mode.XResolution * video_mode.YResolution * (video_mode.BitsPerPixel >> 3); + if (vidPages % 4096) + vidPages = (vidPages >> 12) + 1; + else + vidPages = (vidPages >> 12); + vmm_mapn(0xF0000000, video_mode.PhysBasePtr, vidPages); } } +void vmm_map1(dword virt, dword physical) +{ + dword pde = virt >> 22; + dword pte = (virt & 0x003FF000) >> 12; + dword *pageTables = (dword *)0xC0104000; //this is the location of the page directory + if (!(pageTables[pde] & 0x01)) //the page directory entry does not exist, we must allocate a page for it + { + dword *newpagetable = mm_palloc(); + pageTables[pde] = ((dword)newpagetable) | 0x03; + invlpg(virt); + dword *newpteptr = (dword *)(0xFFC00000 | (pde << 12)); //points to first dword of newly allocated page table + int a; + for (a = 0; a < 1024; a++) + { + newpteptr[a] = 0; + } + } + dword *pteptr = (dword *)(0xFFC00000 | (pde << 12) | (pte << 2)); + *pteptr = physical | 0x03; +} + + +void vmm_mapn(dword virt, dword physical, dword n) +{ + for (; n > 0; n--) + { + vmm_map1(virt, physical); + virt += 4096; + physical += 4096; + } +} + void *malloc(dword bytes) diff --git a/vmm.h b/vmm.h index 11b969f..ca3f890 100644 --- a/vmm.h +++ b/vmm.h @@ -9,6 +9,8 @@ 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);