adds a little bit of memory protection
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#ifdef __TESTING__
|
#ifdef __TESTING__
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -41,29 +42,29 @@ struct pmm_mem_info {
|
|||||||
|
|
||||||
struct pmm_mem_info main_mem;
|
struct pmm_mem_info main_mem;
|
||||||
|
|
||||||
void pmm_set(int bit);
|
void pmm_set(uint32_t bit);
|
||||||
void pmm_unset(int bit);
|
void pmm_unset(uint32_t bit);
|
||||||
bool pmm_test(int bit);
|
bool pmm_test(uint32_t bit);
|
||||||
int pmm_first_free(void);
|
int pmm_first_free(void);
|
||||||
void pmm_init(void);
|
void pmm_init(void);
|
||||||
void* pmm_alloc_block(void);
|
void* pmm_alloc_block(void);
|
||||||
void pmm_free_block(void* p);
|
void pmm_free_block(void* p);
|
||||||
|
|
||||||
void __pmm_set(int bit, struct pmm_mem_info* mem_block)
|
void __pmm_set(uint32_t bit, struct pmm_mem_info* mem_block)
|
||||||
{
|
{
|
||||||
(mem_block->bitmap)[bit / 32] |= (1 << (bit % 32));
|
(mem_block->bitmap)[bit / 32] |= (1 << (bit % 32));
|
||||||
mem_block->used_blocks++;
|
mem_block->used_blocks++;
|
||||||
mem_block->free_blocks--;
|
mem_block->free_blocks--;
|
||||||
}
|
}
|
||||||
|
|
||||||
void __pmm_unset(int bit, struct pmm_mem_info* mem_block)
|
void __pmm_unset(uint32_t bit, struct pmm_mem_info* mem_block)
|
||||||
{
|
{
|
||||||
(mem_block->bitmap)[bit / 32] &= ~(1 << (bit % 32));
|
(mem_block->bitmap)[bit / 32] &= ~(1 << (bit % 32));
|
||||||
mem_block->used_blocks--;
|
mem_block->used_blocks--;
|
||||||
mem_block->free_blocks++;
|
mem_block->free_blocks++;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool __pmm_test(int bit, struct pmm_mem_info* mem_block)
|
bool __pmm_test(uint32_t bit, struct pmm_mem_info* mem_block)
|
||||||
{
|
{
|
||||||
return (mem_block->bitmap)[bit / 32] & (1 << (bit % 32));
|
return (mem_block->bitmap)[bit / 32] & (1 << (bit % 32));
|
||||||
}
|
}
|
||||||
@ -84,7 +85,7 @@ int __pmm_first_free(struct pmm_mem_info* mem_block)
|
|||||||
|
|
||||||
void __pmm_add_mem_block(uint32_t addr, int32_t len, struct pmm_mem_info* mem_block)
|
void __pmm_add_mem_block(uint32_t addr, int32_t len, struct pmm_mem_info* mem_block)
|
||||||
{
|
{
|
||||||
mem_block->startaddr = addr; // main_mem isn't properly being set like this... TODO
|
mem_block->startaddr = addr;
|
||||||
mem_block->len = len;
|
mem_block->len = len;
|
||||||
mem_block->bitmap = 0;
|
mem_block->bitmap = 0;
|
||||||
}
|
}
|
||||||
@ -125,12 +126,20 @@ void* __pmm_alloc_block(struct pmm_mem_info* mem_block)
|
|||||||
void __pmm_free_block(void* p, struct pmm_mem_info* mem_block)
|
void __pmm_free_block(void* p, struct pmm_mem_info* mem_block)
|
||||||
{
|
{
|
||||||
uint64_t* addr = (uint64_t*) &p;
|
uint64_t* addr = (uint64_t*) &p;
|
||||||
int idx = ((*addr) - mem_block->startaddr) / PMM_PAGE_SIZE;
|
uint32_t idx = ((*addr) - mem_block->startaddr) / PMM_PAGE_SIZE;
|
||||||
|
|
||||||
|
// TODO this might still be a little flaky
|
||||||
|
// should we be able to free any pointer? or just ones that we've given out?
|
||||||
|
if (idx == 0)
|
||||||
|
panic("Trying to free reserved memory!");
|
||||||
|
|
||||||
|
if (pmm_test(idx) == 0)
|
||||||
|
panic("Trying to free a block that was already free!");
|
||||||
|
|
||||||
__pmm_unset(idx, mem_block);
|
__pmm_unset(idx, mem_block);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pmm_set(int bit)
|
void pmm_set(uint32_t bit)
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Here we want to calculate if the bit is over the length
|
* Here we want to calculate if the bit is over the length
|
||||||
@ -145,13 +154,13 @@ void pmm_set(int bit)
|
|||||||
__pmm_set(bit, &main_mem);
|
__pmm_set(bit, &main_mem);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pmm_unset(int bit)
|
void pmm_unset(uint32_t bit)
|
||||||
{
|
{
|
||||||
// TODO: same as above
|
// TODO: same as above
|
||||||
__pmm_unset(bit, &main_mem);
|
__pmm_unset(bit, &main_mem);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool pmm_test(int bit)
|
bool pmm_test(uint32_t bit)
|
||||||
{
|
{
|
||||||
// TODO: same as above
|
// TODO: same as above
|
||||||
return __pmm_test(bit, &main_mem);
|
return __pmm_test(bit, &main_mem);
|
||||||
@ -175,18 +184,12 @@ void pmm_init(void)
|
|||||||
|
|
||||||
void* pmm_alloc_block(void)
|
void* pmm_alloc_block(void)
|
||||||
{
|
{
|
||||||
#ifdef __TESTING__
|
|
||||||
printf("PMM: Alloc'd a block!\n");
|
|
||||||
#endif
|
|
||||||
return __pmm_alloc_block(&main_mem);
|
return __pmm_alloc_block(&main_mem);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pmm_free_block(void* p)
|
void pmm_free_block(void* p)
|
||||||
{
|
{
|
||||||
__pmm_free_block(p, &main_mem);
|
__pmm_free_block(p, &main_mem);
|
||||||
#ifdef __TESTING__
|
|
||||||
puts("PMM: Free'd a block!");
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void pmm_add_mem_block(uint32_t addr, uint32_t len)
|
void pmm_add_mem_block(uint32_t addr, uint32_t len)
|
||||||
|
@ -68,7 +68,6 @@ void _main(multiboot_info_t* mbd, uint32_t magic)
|
|||||||
|
|
||||||
print_main_mem();
|
print_main_mem();
|
||||||
|
|
||||||
|
|
||||||
pmm_free_block(a);
|
pmm_free_block(a);
|
||||||
|
|
||||||
print_main_mem();
|
print_main_mem();
|
||||||
|
Reference in New Issue
Block a user