How to find start of unused memory

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
michaellangford
Posts: 21
Joined: Tue Jun 21, 2016 6:41 am
Libera.chat IRC: quad4

How to find start of unused memory

Post 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?
Last edited by michaellangford on Wed Jun 29, 2016 10:43 am, edited 1 time in total.
"Out of memory: Please memorize the following numbers and type them back in when asked for page number 42". - linguofreak

"Quote me in your forum signature" - Sortie (Check!)
glauxosdever
Member
Member
Posts: 501
Joined: Wed Jun 17, 2015 9:40 am
Libera.chat IRC: glauxosdever
Location: Athens, Greece

Re: How to find start of unused memory

Post 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
Last edited by glauxosdever on Fri Sep 30, 2016 9:47 am, edited 1 time in total.
User avatar
michaellangford
Posts: 21
Joined: Tue Jun 21, 2016 6:41 am
Libera.chat IRC: quad4

Re: How to find start of unused memory

Post 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!
"Out of memory: Please memorize the following numbers and type them back in when asked for page number 42". - linguofreak

"Quote me in your forum signature" - Sortie (Check!)
User avatar
michaellangford
Posts: 21
Joined: Tue Jun 21, 2016 6:41 am
Libera.chat IRC: quad4

Re: How to find start of unused memory

Post 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!
"Out of memory: Please memorize the following numbers and type them back in when asked for page number 42". - linguofreak

"Quote me in your forum signature" - Sortie (Check!)
User avatar
Ycep
Member
Member
Posts: 401
Joined: Mon Dec 28, 2015 11:11 am

Re: How to find start of unused memory

Post by Ycep »

*rather

There could be a memory hole at 16MB, ACPI stuff, VBE frame buffer (if you use it), etc.
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

Re: How to find start of unused memory

Post 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.
Boris
Member
Member
Posts: 145
Joined: Sat Nov 07, 2015 3:12 pm

Re: How to find start of unused memory

Post 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.
User avatar
michaellangford
Posts: 21
Joined: Tue Jun 21, 2016 6:41 am
Libera.chat IRC: quad4

Re: How to find start of unused memory

Post 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!
"Out of memory: Please memorize the following numbers and type them back in when asked for page number 42". - linguofreak

"Quote me in your forum signature" - Sortie (Check!)
Post Reply