Skip to content

Commit

Permalink
Fix memory initialization issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamos82 committed Aug 21, 2024
1 parent c711194 commit ba160b3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/asm/boot.s
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ global multiboot_framebuffer_data
global multiboot_mmap_data
global multiboot_basic_meminfo
global multiboot_acpi_info
global multiboot_tag_start
global multiboot_tag_end
global read_multiboot
global gdt64
global stack
Expand Down Expand Up @@ -160,6 +162,7 @@ kernel_jumper:

;.bss section should be already 0 at least on unix and windows systems
;no need to initialize
mov [multiboot_tag_start], rax

read_multiboot:
;Check if the tag is needed by the kernel, if yes store its address
Expand Down Expand Up @@ -225,6 +228,8 @@ read_multiboot:
; && multiboot_tag.size == 8?
cmp dword [rax + multiboot_tag.size], 8
jne read_multiboot
add rax, multiboot_tag.size
mov qword [multiboot_tag_end], rax

mov rax, higher_half
jmp rax
Expand Down Expand Up @@ -268,6 +273,10 @@ fbb_pt_tables:
align 4096
end_of_mapped_memory:
resq 1
multiboot_tag_end:
resq 1
multiboot_tag_start:
resq 1
multiboot_framebuffer_data:
resb 8
multiboot_mmap_data:
Expand Down
4 changes: 4 additions & 0 deletions src/kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ extern uint64_t multiboot_framebuffer_data;
extern uint64_t multiboot_mmap_data;
extern uint64_t multiboot_basic_meminfo;
extern uint64_t multiboot_acpi_info;
extern uint64_t multiboot_tag_end;
extern uint64_t multiboot_tag_start;
extern uint64_t end_of_mapped_memory;
extern uint8_t psf_font_version;
extern struct framebuffer_info framebuffer_data;
Expand All @@ -66,6 +68,7 @@ struct multiboot_tag *tagacpi = NULL;
struct multiboot_tag_module *loaded_module = NULL;
struct multiboot_tag *tag_start = NULL;


uint64_t elf_module_start_hh = 0;

uintptr_t higherHalfDirectMapBase;
Expand Down Expand Up @@ -165,6 +168,7 @@ void _init_basic_system(unsigned long addr){
break;
}
}
multiboot_tag_end = (uint64_t) tag;
}

void kernel_start(unsigned long addr, unsigned long magic){
Expand Down
25 changes: 20 additions & 5 deletions src/kernel/mem/mmap.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <bitmap.h>
#include <logging.h>
#include <main.h>
#include <mmap.h>
#include <multiboot.h>
#include <pmm.h>
Expand All @@ -8,7 +9,14 @@
#include <video.h>

extern uint32_t used_frames;
extern struct multiboot_tag_basic_meminfo *tagmem;
#ifndef _TEST_
extern uint64_t multiboot_tag_end;
extern uint64_t multiboot_tag_start;
#else
#pragma message "multiboot_tag_end and multiboot_tag_start defined here only for test purposes"
uint64_t multiboot_tag_end = 0x1ca000;
uint64_t multiboot_tag_start = 0x9fb;
#endif
uint32_t mmap_number_of_entries;
multiboot_memory_map_t *mmap_entries;
uint8_t count_physical_reserved;
Expand All @@ -24,21 +32,21 @@ const char *mmap_types[] = {
"Defective"
};

// This function is apparently only printing the list of mmap items and their value.
// This function is printing the list of mmap items and their value. and computing the memory approximative size
void _mmap_parse(struct multiboot_tag_mmap *mmap_root){
int total_entries = 0;
_mmap_phys_memory_avail= 0;
pretty_logf(Verbose, "size: 0x%x", sizeof(struct multiboot_tag_mmap));
pretty_logf(Verbose, "mb_tag_start: 0x%x, mb tag_end: 0x%x", multiboot_tag_start, multiboot_tag_end);
mmap_number_of_entries = (mmap_root->size - sizeof(*mmap_root))/mmap_root->entry_size;
size_t mmap_number_of_entries_2 = (mmap_root->size - sizeof(struct multiboot_tag_mmap))/mmap_root->entry_size;
//TODO i'm assuming that the mmap is sorted and sanitized, although this is not guaranted from the spec, grub is actually doing it, so for now i rely on it, in the future
// i will implement at least a sorting algorithm for the mmap
mmap_entries = (multiboot_memory_map_t *)mmap_root->entries;
_mmap_last_available_item= 0;
uint32_t i=0;

while(i<mmap_number_of_entries){
pretty_logf(Verbose, "\t[%d] Address: 0x%x - Len: 0x%x Type: (%d) %s", i, mmap_entries[i].addr, mmap_entries[i].len, mmap_entries[i].type, (char *) mmap_types[mmap_entries[i].type]);
pretty_logf(Verbose, "\t[%d] Address: 0x%x - Len: 0x%x Type: (%d) %s - 0x%x", i, mmap_entries[i].addr, mmap_entries[i].len, mmap_entries[i].type, (char *) mmap_types[mmap_entries[i].type], &mmap_entries[i]);
if ( mmap_entries[i].type == 1 ) {
_mmap_phys_memory_avail += mmap_entries[i].len;
_mmap_last_available_item = i;
Expand Down Expand Up @@ -70,9 +78,16 @@ void _mmap_setup(){
}

bool _mmap_is_address_in_available_space(uint64_t address, uint64_t upper_limit) {
if ( address > multiboot_tag_start && address + upper_limit < multiboot_tag_end) {
return false;
}
if ( address + upper_limit > multiboot_tag_start && address< multiboot_tag_end) {
return false;
}
for (size_t i=0; i < mmap_number_of_entries; i++) {
multiboot_memory_map_t* current_entry = &mmap_entries[i];
if(current_entry->addr + current_entry->len < address + upper_limit) {
//pretty_logf(Verbose, "entry type: 0x%x - %d - 0x%x - address: 0x%x", current_entry->type, i, current_entry->addr, address);
if(current_entry->addr + current_entry->len > address + upper_limit) {
if(current_entry->type == _MMAP_AVAILABLE) {
//pretty_logf(Verbose, "Entry 0x%x is in an available space (with size: 0x%x", address, upper_limit );
// The address is in an available area, but we need to check if it is not overwriting something important.
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/mem/pmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void *pmm_alloc_frame(){
void *pmm_prepare_new_pagetable() {
if ( !pmm_initialized) {
while((!_mmap_is_address_in_available_space(anon_physical_memory_loc, PAGE_DIR_SIZE)) && anon_physical_memory_loc < memory_size_in_bytes) {
pretty_logf(Verbose, " Current address: 0x%x not available trying next", anon_memory_loc);
pretty_logf(Verbose, " Current address: 0x%x - phys: 0x%x not available trying next 0x%x", anon_memory_loc, anon_physical_memory_loc, memory_size_in_bytes);
anon_memory_loc += PAGE_DIR_SIZE;
anon_physical_memory_loc += PAGE_DIR_SIZE;
}
Expand Down
1 change: 0 additions & 1 deletion src/utils/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,5 @@ bool _is_address_in_multiboot(uint64_t address) {
}
//pretty_log(Verbose, " entry not corresponding" );
}
//
return false;
}

0 comments on commit ba160b3

Please sign in to comment.