OSDev.org
https://forum.osdev.org/

Is this reliable way to detect unused memory by kernel?
https://forum.osdev.org/viewtopic.php?f=1&t=33164
Page 1 of 1

Author:  bolevole96 [ Wed Sep 05, 2018 12:41 pm ]
Post subject:  Is this reliable way to detect unused memory by kernel?

I started writing kernel based on Bare Bones tutorial, and currently I am planning how to write basic kernel memory allocator.
In order to write allocator first I must found out which memory regions are free to use.
I am using memory map provided by GRUB to get available memory regions.
Also it is possible that GRUB marks some regions as free, but those same regions overlap with .data, .bss, etc. sections.

My idea was to put symbol at end of the SECTIONS command, to which I will refer in C code, to find out first address not used by kernel.
All memory regions reported by GRUB which are declared as "available" and which are located after all sections should be free?

Researching I found out several basic kernels do this, but is this guaranteed to work.
Also in linker script provided in Bare Bones tutorial at the end of SECTIONS command there is a comment:

Quote:
/* The compiler may produce other sections, by default it will put them in
a segment with the same name. Simply add stuff here as needed. */


So are these sections placed before my symbol, or what?

Author:  TheCool1Kevin [ Wed Sep 05, 2018 1:36 pm ]
Post subject:  Re: Is this reliable way to detect unused memory by kernel?

Basically, it doesn't matter really. Just add symbols to the end of you're SECTIONS.

You need to add 2 symbols to identify where the kernel starts and ends into you're linker script. So, your linker script should be something like:
Code:
ENTRY(_start)
SECTIONS
{
    . = 0x100000;
    _kernel_start = .;

    sections...

   _kernel_end = .;
}

Then in your main code, add:
Code:
extern int _kernel_start;
extern int _kernel_end;

Author:  thomtl [ Sun Sep 09, 2018 12:25 am ]
Post subject:  Re: Is this reliable way to detect unused memory by kernel?

Hello,

Please note that you want the address from the _kernel_start and _kernel_end variables, so pseudocode would look like this:
Code:
extern int _kernel_start;
extern int _kernel_end;
int kernel_start = (int)&_kernel_start;
int kernel_end = (int)&_kernel_end;

Now the kernel_start and kernel_end variables contain the adressess of the start and end of the kernel.

-thomtl

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/