Is this reliable way to detect unused memory by kernel?

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
bolevole96
Posts: 3
Joined: Tue Jun 12, 2018 8:57 am

Is this reliable way to detect unused memory by kernel?

Post by bolevole96 »

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:
/* 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?
User avatar
TheCool1Kevin
Posts: 24
Joined: Fri Oct 14, 2016 7:37 pm
Location: Canada
Contact:

Re: Is this reliable way to detect unused memory by kernel?

Post by TheCool1Kevin »

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: Select all

ENTRY(_start)
SECTIONS
{
    . = 0x100000;
    _kernel_start = .;

    sections...

   _kernel_end = .;
}
Then in your main code, add:

Code: Select all

extern int _kernel_start;
extern int _kernel_end;
LiquiDOS, my weird hobbyist OS.
"Strive for progress, not perfection" - Anonymous
User avatar
thomtl
Member
Member
Posts: 66
Joined: Mon Sep 03, 2018 2:25 am

Re: Is this reliable way to detect unused memory by kernel?

Post by thomtl »

Hello,

Please note that you want the address from the _kernel_start and _kernel_end variables, so pseudocode would look like this:

Code: Select all

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
Post Reply