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!
Map pages to kernel (for kernel heap)
Re: Map pages to kernel (for kernel heap)
Hi,
Cheers,
Adam
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.afritieefy wrote:I have to do this in the page directory each running process... Is there a better solution for this?
Cheers,
Adam
-
- Member
- Posts: 595
- Joined: Mon Jul 05, 2010 4:15 pm
Re: Map pages to kernel (for kernel heap)
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.
-
- Posts: 12
- Joined: Fri Dec 02, 2011 6:13 am
Re: Map pages to kernel (for kernel heap)
But to what physical adresses do I have to map 0xC0000000-0xFFFFFFFF, because well... there are no frames for these pages allocated yet?AJ wrote: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.afritieefy wrote:I have to do this in the page directory each running process... Is there a better solution for this?
Re: Map pages to kernel (for kernel heap)
Hi,
All you need to do is allocate this space before you initialise your scheduler:
Cheers,
Adam
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.
Cheers,
Adam
-
- Posts: 12
- Joined: Fri Dec 02, 2011 6:13 am
Re: Map pages to kernel (for kernel heap)
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?
Re: Map pages to kernel (for kernel heap)
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
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
-
- Posts: 12
- Joined: Fri Dec 02, 2011 6:13 am
Re: Map pages to kernel (for kernel heap)
Ok now I get it! It's easier then I thought Thanks a lot!
Re: Map pages to kernel (for kernel heap)
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.