Map pages to kernel (for kernel heap)

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
afritieefy
Posts: 12
Joined: Fri Dec 02, 2011 6:13 am

Map pages to kernel (for kernel heap)

Post by afritieefy »

Hi

I'm writing my virtual memory manager, but there is something I can't figure out. My kernel is mapped from 0xC0000000 to 0xC0000000+kernelsize. This means that when I want to allocate extra memory for the kernel (for example when calling kmalloc and there isn't enough memory mapped) the new frame has to be mapped above 0xC0000000+kernelsize.

The problem is that every proces has it's own page directory, and in each page directory the kernel is mapped! So when I add a new mapping for the kernel (after allocating extra kernel memory), I have to do this in the page directory each running process... Is there a better solution for this?

Thanks!
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Map pages to kernel (for kernel heap)

Post by AJ »

Hi,
afritieefy wrote:I have to do this in the page directory each running process... Is there a better solution for this?
Yes. When you initialise paging, allocate 1MiB for page tables for your kernel space (page tables representing 0xC0000000-0xFFFFFFFF). Simply copy this top quarter of the PD when you are creating a new PD for a new task. Any PTE you add will then be consistant across all processes.

Cheers,
Adam
OSwhatever
Member
Member
Posts: 595
Joined: Mon Jul 05, 2010 4:15 pm

Re: Map pages to kernel (for kernel heap)

Post by OSwhatever »

This is highly architecture dependent. This is usually more of a problem when you need to add an extra page for the page table itself, then you might have to adjust all page tables for each process. This is not very efficient and the CPU makers has learned the lesson in newer CPUs. If you use PAE in x86 for example, all processes can share the highest 1GB section and then you don't need to visit the page table for every page table. ARM had the same problem but this is now fixed in ARMv6 and above by having two page tables, one for kernel and one for user.
afritieefy
Posts: 12
Joined: Fri Dec 02, 2011 6:13 am

Re: Map pages to kernel (for kernel heap)

Post by afritieefy »

AJ wrote:
afritieefy wrote:I have to do this in the page directory each running process... Is there a better solution for this?
Yes. When you initialise paging, allocate 1MiB for page tables for your kernel space (page tables representing 0xC0000000-0xFFFFFFFF). Simply copy this top quarter of the PD when you are creating a new PD for a new task. Any PTE you add will then be consistant across all processes.
But to what physical adresses do I have to map 0xC0000000-0xFFFFFFFF, because well... there are no frames for these pages allocated yet?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Map pages to kernel (for kernel heap)

Post by AJ »

Hi,

All you need to do is allocate this space before you initialise your scheduler:
  • Initialise paging, mapping kernel from 0xC0000000 to 0xC0000000+kernelsize.
  • Give your page frame allocator its stack/bitmap/whatever and get your allocate_page() function up and running.
  • Call allocate_page() repeatedly, to fill PDE's representing 0xC0000000+kernelsize to 0xFFFFF000.
  • Do other stuff...
  • Initialise scheduler.
In other words, you allocate page frames for this space in exactly the same way you would allocate them normally.

Cheers,
Adam
afritieefy
Posts: 12
Joined: Fri Dec 02, 2011 6:13 am

Re: Map pages to kernel (for kernel heap)

Post by afritieefy »

Sorry, I still don't get it. If I actually allocate virtual adresses 0xC0000000 to 0xFFFFF000 to (real) physical memory, isn't this a waste of like 1 GB memory? Since probably only a very little part of this will be actually used by my kernel?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Map pages to kernel (for kernel heap)

Post by AJ »

Hi,

No - you only actually allocate space for page tables (in other words, 256 page tables, 4KiB each = 1MiB). You don't add page table entries until you allocate pages on demand for your kernel heap.

Cheers,
Adam
afritieefy
Posts: 12
Joined: Fri Dec 02, 2011 6:13 am

Re: Map pages to kernel (for kernel heap)

Post by afritieefy »

Ok now I get it! :D It's easier then I thought :) Thanks a lot!
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Map pages to kernel (for kernel heap)

Post by egos »

My kernel and kernel modules each have "Reserved Memory Area" section so in primary task I preliminary map 4 kb page tables only for this and other standard sections (code, data, bss), not for all kernel space.
If you have seen bad English in my words, tell me what's wrong, please.
Post Reply