kernel mapping

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
einsteinjunior
Member
Member
Posts: 90
Joined: Tue Sep 11, 2007 6:42 am

kernel mapping

Post by einsteinjunior »

Hi to all,

I just wish to know exactly what is meant by mapping the kernel to
user address space.More specifically i wish to know how this is done.
Does this require that addresses referencing the kernel in user address space should in fact reference the kernel in physical memory.
What about if some part of the kernel is paged out.Or should the kernel never be paged out?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

Hi,

Note to members, I've had a conversation with einsteinjunior about this in PM, and after I failed to explain properly I suggested he post here so that others can help.

Virtual address layout

First of all, forget about physical memory. Concentrate solely on virtual memory, and in particular the layout of the virtual address space. In a lower-half kernel (his kernel is lower-half) you have a separation - the top X GB is normally reserved for the kernel heap and cache areas, and the lower Y MB is usually reserved for the kernel .text, .data, .bss sections. (The stack may be either in the lower or higher section depending on implementation).

A lovely piece of ascii art may help:

Code: Select all

4GB: 0xFFFFFFFF +----------+                           
                |          | Kernel heap and filesystem caches.
                |          |                                               
3GB: 0xC0000000 +----------+                                                  
                |          |                                                           
                |          |                                                           
                |          |                                                           
                |          | User area: available for use by user programs.            
                |          |                                                           
                |          |                                                           
                |          |                                                           
                |          |                                                           
4MB: 0x00400000 +----------+                                                           
                |          | Kernel code and static data, bootloader and BIOS code/IVT.
     0x00000000 +----------+
Note that the 4MB and 1GB sizes of the kernel sections are arbitrary, but are common choices (the 1GB one, at least).

So the reasoning behind this is that your BIOS is copied into the lower 640k of RAM at bootup, and loads your bootloader (GRUB), which is loaded below the 1MB mark and loads your kernel above that mark (usually starting from exactly 0x100000: 1MB).

So now your kernel code, data etc is in the lowest section of memory (the 4MB chunk on the diagram). You now need space to make a heap, and space to allocate caches for the virtual filesystem etc. This is commonly placed at the highest area of the virtual address space, where it is safely out of the way of user programs.

Note that this is 1GB of virtual space reserved for use by the kernel - it does not mean it is all allocated at once!

Physical layout

Now we'll focus on how virtual memory maps onto physical. It is common practice to map the virtual pages in the bottom and top sections of that diagram (the kernel sections) to the same physical frames across all address spaces. The diagram below might help (lifted from my website, so I'm not infringing copyright):

Image

Please note that on the above picture, the address space is drawn the opposite way around, so the top of the picture is address 0x00000000, and the bottom is address 0xFFFFFFFF.

The reason for this is twofold:

* The kernel code will be the same across all address spaces, so there is no need to replicate it (it would just waste memory).
* The kernel heap and caches will not change across address spaces - in fact, if you make a (kernel) heap allocation in one address space, you'll want to be able to 'see' that allocation when you change address spaces! Consider something like a VFS node - you'll want to be able to view that node from every address space, not just the one you created it from.

I hope this answers your question. You were quite vague in what in particular you didn't understand, so please post back with questions.

Cheers,

James
Post Reply