78 lines
1.9 KiB
C
78 lines
1.9 KiB
C
#include <stdint.h>
|
|
|
|
#include <kernel/_kernel.h>
|
|
#include <kernel/pmm.h>
|
|
|
|
/**
|
|
* What i want to do is create a linked list of all the memory structures
|
|
*
|
|
* Theres one at the very start of the memory
|
|
*
|
|
* one at 1MB
|
|
*
|
|
* and then one provided by ram.
|
|
*
|
|
*
|
|
* So the idea is to create a way to access memory through this such that,
|
|
* when you give a bit block number, it'll go through the first item in the linked list, if the block is out of that range, it
|
|
* traverses to the next node, tries to find it there, and then continues until it either runs out of memory, or finds a location
|
|
*/
|
|
|
|
#define PMM_PAGE_SIZE 4096
|
|
|
|
struct pmm_mem_info {
|
|
uint64_t startaddr;
|
|
uint64_t len; // in kb
|
|
uint32_t* bitmap;
|
|
};
|
|
|
|
#define PMM_GET_MEM_BLOCKS(x) x.len / PMM_PAGE_SIZE
|
|
|
|
struct pmm_mem_info main_mem;
|
|
|
|
void __pmm_set(int bit, uint32_t* bitmap)
|
|
{
|
|
bitmap[bit / 32] |= (1 << (bit % 32));
|
|
}
|
|
|
|
void __pmm_unset(int bit, uint32_t* bitmap)
|
|
{
|
|
bitmap[bit / 32] &= ~(1 << (bit % 32));
|
|
}
|
|
|
|
int __pmm_first_free(struct pmm_mem_info mem_block)
|
|
{
|
|
for (uint32_t i = 0; i < PMM_GET_MEM_BLOCKS(mem_block) / 32; i++) {
|
|
if (mem_block.bitmap[i] == 0xFFFFFFFF) // this segment is full
|
|
continue;
|
|
for (int j = 0; j < 32; j++) {
|
|
if (mem_block.bitmap[i] & (1 << j))
|
|
continue; // this page is used
|
|
return (i * 32) + j; // i * 32 is the chunk of 32, plus j to get to the page in the chunk
|
|
}
|
|
}
|
|
kwarn("OUT OF MEMORY");
|
|
return -1;
|
|
}
|
|
|
|
void pmm_set(int bit)
|
|
{
|
|
/**
|
|
* Here we want to calculate if the bit is over the length
|
|
* subtract the length and bit amount so that we compensate for the bit map
|
|
*
|
|
* i.e. (length / 4096) == amount of blocks in that specific mem region
|
|
* if (bit > amt of blocks),
|
|
* go to next node, subtract amt of blocks from bit, and pass that
|
|
*
|
|
* below is merely a temporary solution
|
|
*/
|
|
__pmm_set(bit, main_mem.bitmap);
|
|
}
|
|
|
|
void pmm_unset(int bit)
|
|
{
|
|
// TODO: same as above
|
|
__pmm_unset(bit, main_mem.bitmap);
|
|
}
|