OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 18, 2024 11:27 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Is this reliable way to detect unused memory by kernel?
PostPosted: Wed Sep 05, 2018 12:41 pm 
Offline

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


Top
 Profile  
 
 Post subject: Re: Is this reliable way to detect unused memory by kernel?
PostPosted: Wed Sep 05, 2018 1:36 pm 
Offline
User avatar

Joined: Fri Oct 14, 2016 7:37 pm
Posts: 24
Location: Canada
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;

_________________
LiquiDOS, my weird hobbyist OS.
"Strive for progress, not perfection" - Anonymous


Top
 Profile  
 
 Post subject: Re: Is this reliable way to detect unused memory by kernel?
PostPosted: Sun Sep 09, 2018 12:25 am 
Offline
Member
Member
User avatar

Joined: Mon Sep 03, 2018 2:25 am
Posts: 66
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 127 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group