2025-07-11 05:38:26 +00:00
|
|
|
#include "physmem.h"
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <io.h>
|
|
|
|
|
|
|
|
|
|
uint8_t* pmem_bitmap;
|
|
|
|
|
|
|
|
|
|
|
2025-07-11 15:53:10 +00:00
|
|
|
unsigned int build_bitmap(mb_mmap_entry_t* mmap, int mmap_size) {
|
|
|
|
|
unsigned int pages_allocated = 0;
|
2025-07-11 05:38:26 +00:00
|
|
|
//Set up a bitmap and set it all to 1
|
|
|
|
|
pmem_bitmap = &_kernel_end;
|
2025-07-11 15:53:10 +00:00
|
|
|
uint32_t bitmap_end = (uint32_t)pmem_bitmap + (MEM_MAX / (PAGE_SIZE*8));
|
2025-07-11 05:38:26 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
2025-07-11 15:53:10 +00:00
|
|
|
uint8_t* mem_bottom =(uint8_t*) (((uint32_t)mmap->addr + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1));
|
2025-07-11 05:38:26 +00:00
|
|
|
|
|
|
|
|
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
|
2025-07-11 15:53:10 +00:00
|
|
|
uint8_t* mem_top = (uint8_t*) (((uint32_t)mmap->addr + (uint32_t)mmap->mem_size) & ~(PAGE_SIZE - 1));
|
2025-07-11 05:38:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
*/
|
2025-07-11 15:53:10 +00:00
|
|
|
int page_count = (mem_top - mem_bottom) / PAGE_SIZE;
|
|
|
|
|
for(int i = 0; i < page_count; i++){
|
|
|
|
|
|
|
|
|
|
//OK which page is this
|
|
|
|
|
uint8_t* page_addr = mem_bottom + (PAGE_SIZE * i);
|
2025-07-11 05:38:26 +00:00
|
|
|
|
2025-07-11 15:53:10 +00:00
|
|
|
if((uint32_t)page_addr < bitmap_end) {
|
|
|
|
|
//bruh this is in lowmem or the kernel
|
2025-07-11 05:38:26 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-11 15:53:10 +00:00
|
|
|
//Available. Calculate the offset of the bit on the bitmap based on the page address
|
|
|
|
|
unsigned int pagenumber = (unsigned int)page_addr / PAGE_SIZE;
|
|
|
|
|
uint8_t* bitmap_byte = pmem_bitmap + (pagenumber / 8);
|
|
|
|
|
int bitmap_bit = pagenumber % 8;
|
|
|
|
|
|
|
|
|
|
//ok now set the bit to unused
|
|
|
|
|
*bitmap_byte &= ~(0x80 >> bitmap_bit);
|
|
|
|
|
pages_allocated++;
|
2025-07-11 05:38:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2025-07-11 15:53:10 +00:00
|
|
|
|
|
|
|
|
return pages_allocated;
|
2025-07-11 05:38:26 +00:00
|
|
|
|
|
|
|
|
}
|