Kernel memory management

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
Agola
Member
Member
Posts: 155
Joined: Sun Nov 20, 2016 7:26 am
Location: Somewhere

Kernel memory management

Post by Agola »

Hello forum, my kernel memory manager has some conceptical problems.

My kernel memory manager has 3 layers:

-> Firstly the physical memory manager. It manages free / used frames and it has two functions:
* uintptr_t alloc_frame()
* void free_frame(uintptr_t frame_address)

-> Then the virtual memory manager. It manages the virtual addresses, allocates and uses physical frames if needed. It has two functions:
* void map_memory(uint32_t* page_directory, uint32_t address, uint32_t size, bool rw, bool user)
* void unmap_memory(uint32_t* page_directory, uint32_t address, uint32_t size)

-> Then I have dynamic memory manager, uses virtual memory to allocate some data. It is a simple first fit heap allocator. It has four functions:
* void* kmalloc(size_t size)
* void* kvalloc(size_t size)
* void* kmemalign(uint32_t align, size_t size)
* void kfree(void* ptr)

Now where my conceptical problems in:

I need to allocate some memory to prepare kernel page directory and kernel page tables to initialize paging, but the memory allocator needs paging is already initialized as it uses virtual memory allocator.

More clearly, I need to use heap allocator to initialize and enable paging, but paging needs to be already enabled to use heap allocator.

It is like the chicken or egg problem. :(

I'm out of ideas.

Thanks in advance :|
Keyboard not found!

Press F1 to run setup.
Press F2 to continue.
User avatar
dozniak
Member
Member
Posts: 723
Joined: Thu Jul 12, 2012 7:29 am
Location: Tallinn, Estonia

Re: Kernel memory management

Post by dozniak »

Use a low-level dumb allocator to cut-off a piece of boot memory and use that in initializing the page directory and other structures.

I just pass an address of memory region to the memory allocator and let it initialize there - since it knows how much space it has taken it can mark it as _Used_ afterwards.
Learn to read.
User avatar
eryjus
Member
Member
Posts: 286
Joined: Fri Oct 21, 2011 9:47 pm
Libera.chat IRC: eryjus
Location: Tustin, CA USA

Re: Kernel memory management

Post by eryjus »

You might consider doing your first initialization outside your routines at a common location for your kernel and then let initialize your 3 handlers to that state when you "put it in charge".
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Kernel memory management

Post by iansjack »

You don't need to allocate memory, as such (i.e. using a memory allocator) for this purpose. Just mark some RAM pages as in use and use them.
Post Reply