Page 1 of 1
How to find start of unused memory
Posted: Thu Jun 23, 2016 10:59 am
by michaellangford
Hello, I am begginning to write a memory manager, but am unsure where the memory used by my kernel's code ends. Please tell me if this sounds totally stupid. I'd appreciate knowing that I really don't understand. I (from Grub) know the start and end of available memory. "What I'm going to talk about assumes you know where the chunk of free memory is you can play with"('Writing a memory manager') How do I find where the kernel image ends, and where free data that can be allocated is?
Re: How to find start of unused memory
Posted: Thu Jun 23, 2016 12:15 pm
by glauxosdever
Hi,
You can (probably should) insert symbols to the linker script, so you can know from C code where each ELF section starts and ends.
For example at the start you can add _kernel_start and _kernel_end symbols, so you determine where your kernel starts and ends. You can also add _text_start, _text_end, _data_start, _data_end, same for .bss, same for .rodata, etc...
Later, determine which parts of memory are not free, that is, every thing that exists in memory and you are going to use. For example, the multiboot structure should not be considered free memory, since you will use it. Same for the memory map structures, the module structures, etc...
Do not forget to mark as free only memory that is contained in the memory map as usable memory. I mean, do not attempt to mark as free 0x000A0000 or 0x000E0000 (or any other address that is not usable according to the memory map).
Hope this helps.
Regards,
glauxosdever
Re: How to find start of unused memory
Posted: Thu Jun 23, 2016 12:28 pm
by michaellangford
Thanks! I did not know one could do this! do I access it in c like a constant? like, 'printf("kernel starts at %d and ends at %d\n", _kernel_start, _kernel_end); ?
Thanks for your help! BTW I looked at your OS, very nice!
Re: How to find start of unused memory
Posted: Thu Jun 23, 2016 12:30 pm
by michaellangford
Also, are there any other parts of memory that are generally unusable (exluding parts near the base of memory, BIOS stuff), other than video memory?
Thanks!
Re: How to find start of unused memory
Posted: Thu Jun 23, 2016 1:31 pm
by Ycep
*rather
There could be a memory hole at 16MB, ACPI stuff, VBE frame buffer (if you use it), etc.
Re: How to find start of unused memory
Posted: Thu Jun 23, 2016 2:06 pm
by max
michaellangford wrote:Also, are there any other parts of memory that are generally unusable (exluding parts near the base of memory, BIOS stuff), other than video memory?
Thanks!
GRUB's memory map will usually only tell you that some memory is usable if it really is usable. I trusted it and it worked out so far.
Re: How to find start of unused memory
Posted: Thu Jun 23, 2016 3:27 pm
by Boris
Be warned:
If grub tells you some memory is unable, it's not always the case: it may contain your kernel,its modules... or multiboot structures.
Be sure to check those.
Re: How to find start of unused memory
Posted: Tue Jun 28, 2016 6:42 pm
by michaellangford
Thanks! Boris: You are absolutely right. my kernel (and all the boot structs) were located in a slot of the map marked as free! Got a working (though primitive) manager now!