72 lines
2.1 KiB
C
72 lines
2.1 KiB
C
|
|
#include "physmem.h"
|
||
|
|
#include <stdint.h>
|
||
|
|
#include <io.h>
|
||
|
|
|
||
|
|
uint8_t* pmem_bitmap;
|
||
|
|
|
||
|
|
|
||
|
|
void build_bitmap(mb_mmap_entry_t* mmap, int mmap_size) {
|
||
|
|
|
||
|
|
//Set up a bitmap and set it all to 1
|
||
|
|
pmem_bitmap = &_kernel_end;
|
||
|
|
unsigned int bitmap_end = (unsigned int)pmem_bitmap + (MEM_MAX / (PAGE_SIZE*8));
|
||
|
|
printf(default_output, "bitmap at: %X", pmem_bitmap);
|
||
|
|
for(unsigned int i = 0; i < bitmap_end; i++) {
|
||
|
|
*(pmem_bitmap + i) = 0xFF;
|
||
|
|
}
|
||
|
|
|
||
|
|
int mmap_end = (uint32_t)mmap + mmap_size;
|
||
|
|
for(;
|
||
|
|
(int)mmap < mmap_end;
|
||
|
|
mmap = (mb_mmap_entry_t*)((int)mmap + mmap->entry_size + sizeof(mmap->entry_size))) {
|
||
|
|
|
||
|
|
|
||
|
|
printf(default_output,"__MMAP_ENTRY__:\nSTART_ADDR:%X\nSIZE:%X\nTYPE:%u\n",(uint32_t)mmap->addr, (uint32_t)mmap->mem_size, mmap->type);
|
||
|
|
|
||
|
|
|
||
|
|
uint8_t* mem_bottom =(uint8_t*) (((int)mmap->addr + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1));
|
||
|
|
|
||
|
|
if(mmap->type != 1) {
|
||
|
|
//nah...
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
//round the starting address of the map to the nearest page boundary so we don't mark half used pages as free
|
||
|
|
uint8_t* mem_top = (uint8_t*) (((int)mmap->addr + mmap->mem_size) & ~(PAGE_SIZE - 1));
|
||
|
|
|
||
|
|
|
||
|
|
printf(default_output, "lowerpage:%X|upper:%X\n", mem_bottom, mem_top);
|
||
|
|
|
||
|
|
//ok lets set the bitmap
|
||
|
|
/* bullshit dont use it
|
||
|
|
for(uint8_t* i = mem_bottom; i < mem_top; i+=4096) {
|
||
|
|
|
||
|
|
//Watch out for cutting up kernelspace
|
||
|
|
if((unsigned int)mem_bottom + (unsigned int)i < bitmap_end)
|
||
|
|
continue;
|
||
|
|
|
||
|
|
//ok we can just say this whole chunk is available
|
||
|
|
*(pmem_bitmap + (unsigned int)i) = 0;
|
||
|
|
}
|
||
|
|
*/
|
||
|
|
for(int i = 0; i < mem_top - mem_bottom; i++){
|
||
|
|
|
||
|
|
if(mem_bottom + (PAGE_SIZE * i) < bitmap_end) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
//OK to allocate one page.
|
||
|
|
//wtf am i doing. sleep on it.
|
||
|
|
int shift = i%8;
|
||
|
|
*(pmem_bitmap) &= ~(0x80 >> i);
|
||
|
|
if(!*pmem_bitmap == 7) {
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|