Skip to content

Commit

Permalink
+ | Start of kmalloc
Browse files Browse the repository at this point in the history
  • Loading branch information
BolvicBolvicovic committed Aug 14, 2024
1 parent 06390a0 commit 5ff4611
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
4 changes: 3 additions & 1 deletion kernel/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void kernel_main(uint32_t magic, uint32_t addr) {
"mov $1, %eax\n"
"int $0x80"
);
pmm_init(mem_size, bitmap);
pmm_init(mem_size, &bitmap);

for (size_t i = 0; i < 15; i++) {
if (region[i].type > 5) region[i].type = MULTIBOOT_MEMORY_AVAILABLE;
Expand All @@ -55,6 +55,8 @@ void kernel_main(uint32_t magic, uint32_t addr) {
}
pmm_deinit_region(0x10000, &end_kernel_virt - &start_kernel_virt);
vmm_init();
void* caca = pmm_alloc_block();
printf("new alloc: %p\n", caca);
asm volatile("sti\n\t");
uint32_t cr0;
asm volatile("mov %%cr0, %0" : "=r" (cr0));
Expand Down
18 changes: 10 additions & 8 deletions memory/pmm/pmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ int mmap_test(int bit) {
}

int mmap_find_first_free() {
for (size_t i = 0; i < _memory_max_blocks / 32; i++) {
for (size_t i = 0; i < _memory_max_blocks - _memory_used_blocks; i++) {
if (_memory_map[i] != 0xFFFFFFFF) {
for (size_t j = 0; j < 32; i++) {
int bit = 1 << j;
for (size_t j = 0; j < 32; j++) {
uint32_t bit = 1 << j;
if (!(_memory_map[i] & bit)) {
return i * 32 + j;
}
}
}
}
return (-1);
}

int mmap_find_first_free_s (size_t size) {
Expand All @@ -40,13 +41,12 @@ int mmap_find_first_free_s (size_t size) {

for (size_t i = 0; i < _memory_max_blocks - _memory_used_blocks; i++)
if (_memory_map[i] != 0xffffffff)
for (int j=0; j<32; j++) { //! test each bit in the dword
for (uint32_t j=0; j<32; j++) { //! test each bit in the dword

int bit = 1<<j;
uint32_t bit = 1<<j;
if (! (_memory_map[i] & bit) ) {

int startingBit = i*32;
startingBit+=bit; //get the free bit in the dword at index i
uint32_t startingBit = i*32 + j;

uint32_t free=0; //loop through each bit to see if its enough space
for (uint32_t count=0; count<=size;count++) {
Expand All @@ -55,7 +55,7 @@ int mmap_find_first_free_s (size_t size) {
free++; // this bit is clear (free frame)

if (free==size)
return i*4*8+j; //free count==size needed; return index
return startingBit; //free count==size needed; return index
}
}
}
Expand Down Expand Up @@ -98,6 +98,7 @@ void* pmm_alloc_block() {
mmap_set(frame);
uint32_t addr = frame * PMM_BLOCK_SIZE;
_memory_used_blocks++;
printf("new pmm alloc : %p\n", (void *)addr);
return (void*)addr;
}

Expand All @@ -108,6 +109,7 @@ void* pmm_alloc_blocks(size_t nb_blocks) {
for (size_t i = 0; i < nb_blocks; i++) mmap_set(frame + i);
uint32_t addr = frame * PMM_BLOCK_SIZE;
_memory_used_blocks += nb_blocks;
printf("new pmm alloc+: %p\n", (void *)addr);
return (void*)addr;
}

Expand Down
31 changes: 31 additions & 0 deletions memory/vmm/vmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,37 @@ void vmm_map_page(void* phys, void* virt) {
pt_entry_add_attrib(page, I86_PTE_PRESENT);
}

pt_entry* vmm_find_next_free() {
for (size_t i = 0; i < 1024; i++) {
if (_current_dir[i]) {
pt_entry* entry = pd_entry_pfn(_current_dir[i]);
for (size_t j = 0; j < 1024; j++) {
if (!entry[j]) {
return entry + j;
}
}
}
}
return -1;
}

pt_entry* vmm_find_next_free_s(size_t nb_blocks) {
if (nb_blocks == 1) return vmm_find_next_free();
for (size_t i = 0; i < 1024; i++) {
if (_current_dir[i]) {
pt_entry* entry = pd_entry_pfn(_current_dir[i]);
for (size_t j = 0; j < 1024; j++) {
if (!entry[j]) {
return entry + j;
}
}
}
}
}

void* kmalloc(size_t size) {
}

extern uint32_t start_kernel_virt;
extern uint32_t start_kernel;

Expand Down

0 comments on commit 5ff4611

Please sign in to comment.