I'm going to get my memory map via GRUB.
Here in the linker file I put some symbols to get the start and the end of the kernel (I'm using an higher half kernel):
Code: Select all
...
/* lower half stuff */
. += KERNEL_VIRTUAL_BASE;
. = ALIGN (4096);
start = .;
...
. = ALIGN(4096);
end = .;
}
Code: Select all
/**
* Get and anylize the GRUB memory map to count the RAM size.
*/
unsigned int findRAMSize(multiboot_info_t* mbt) {
uint32_t size = 0;
/**
* If the bit 0 is set, it is possible to safely refer to mbd->mem_lower for conventional memory and mbd->mem_upper for high memory.
* Both are given in kibibytes, i.e. blocks of 1024 bytes each.
*/
if(mbt->flags & 0x1)
size = mbt->mem_lower + mbt->mem_upper;
/**
* Otherwise bit 6 in the flags uint16_t is set, then the mmap_* fields are valid,
* and indicate the address and length of a buffer containing a memory map of the machine provided by the BIOS
*/
else if (mbt->flags & 0x20) {
memory_map_t *mmap = mbt->mmap_addr;
while ((unsigned int)mmap < (mbt->mmap_addr + mbt->mmap_length)) {
size += mbt->mmap_length;
mmap = (memory_map_t *)(mmap + mmap->size + sizeof(mmap->size));
}
}
return size;
}
Now I have to find the actual blocks. I set (I'm not sure if this is correct)
Code: Select all
stack = end
Code: Select all
end -= 0xC0000000;
start -= 0xC0000000;
I use the code example in the Wiki https://wiki.osdev.org/Detecting_Memory ... Memory_Map but when it has to go to the next free block, it crashes with a page fault.
Code: Select all
// Find out what addresses are free
memory_map_t *mmap = mbt->mmap_addr;
// TODO: Find kernel + stack start and end addresses
while ((uint32_t)mmap < (mbt->mmap_addr + mbt->mmap_length)) {
printf("mmap = 0x%x\n", mmap);
mmap = (memory_map_t *)((uint32_t)mmap + mmap->size + sizeof(mmap->size));
}
Thank you to who will help in advance.