OSDev.org

The Place to Start for Operating System Developers
It is currently Mon Jul 06, 2020 11:05 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Create an heap for the kernel
PostPosted: Fri Nov 08, 2019 3:32 pm 
Offline

Joined: Mon Oct 21, 2019 11:38 am
Posts: 7
I just want to know if I understood the process of implementing the kernel heap correctly.
I have an higher half kernel that starts around 0xC0000000. The layers are KHEAP -> VMM -> PMM.
That’s how I think it should work: kmalloc() has a list of free memory blocks. If it’s null or there isn’t enough space, it asks a page from the virtual memory manager that maps it about from 3,5GB to 4GB. It has to be contiguous.

Can you please tell me if I’m wrong?


Top
 Profile  
 
 Post subject: Re: Create an heap for the kernel
PostPosted: Sat Dec 07, 2019 11:32 am 
Offline
Member
Member

Joined: Sun Sep 23, 2007 4:52 am
Posts: 368
I think it's done more like this. The details depend on your OS.

1. A frame manager, that manages frames (4kb blocks of physical memory). It doesn't matter whether they are contigous or not. Example api:
void * get_frame();
void free_frame(void * frame).
These functions are called only by the paging system.

2. A paging system, that maps frames into the virtual memory space of the current process (or the kernel). I am not sure of the details (also it depends on your OS, meltdown vulnerability fixing, and so on). Example api:
bool map_page(void * page, ?? permissions/process/kernel/etc);
void unmap_page(void * page);
Where the page pointers must be 4kb aligned (the size of the pages).
These call get_frame() and free_frame(), respectively.

3. A malloc function, that keeps track of which pages are free, and which are taken, and takes responsibility for finding a consecutive run of pages long enough to hold the size of the malloc. Calls the paging system api.


Top
 Profile  
 
 Post subject: Re: Create an heap for the kernel
PostPosted: Sat Dec 07, 2019 6:03 pm 
Offline
Member
Member
User avatar

Joined: Sun Oct 18, 2009 5:47 pm
Posts: 208
Location: Alexandria, Egypt | Ottawa, Canada
As Craze Fog already pointed out above, you have to differentiate between "allocating" physical (i.e. RAM frames), allocating virtual memory SPACES, and MAPPING virtual pages to physical frames.

  • Layer 1 above allocates "physical" frames from RAM.
  • Layer 2 above MAPS a virtual address region into physical frames. Input to this layer is ideally the LOGICAL address region that you want to map into physical frames. This layer doesn't "guess" where the logical region should be. You should provide this layer with the start/end addresses for the region you want to map.

    For example: if you want to map 0xC8000000-0xC8FFFFFF into anonymous frames; you should pass these parameters to layer 2, and it will simply update the page tables for that region, and allocates anonymous frames for you.
  • Now the job of layer 3 is pretty clear: it has a bitmap (or some sort of structure) that describe the mapping info from 0xC0000000 to 0xFFFFFFFF. If you want to "kmalloc()" an amount of contiguous pages, the layer would look into the bitmap, find a contiguous region that is not already mapped, call layer 2 to anonymously map the region, and finally update the bitmap and return a logical pointer.

You may want to check these:
-> https://en.wikipedia.org/wiki/Buddy_memory_allocation
-> http://man7.org/linux/man-pages/man2/mmap.2.html


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: Google [Bot], pranavappu007 and 8 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group