Page 1 of 1

Kernel memory management

Posted: Sat Apr 29, 2017 6:05 am
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 :|

Re: Kernel memory management

Posted: Sat Apr 29, 2017 6:55 am
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.

Re: Kernel memory management

Posted: Sat Apr 29, 2017 12:14 pm
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".

Re: Kernel memory management

Posted: Sat Apr 29, 2017 2:29 pm
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.