kernel memory mapping
kernel memory mapping
So the address space of an user process consists of one half for the user's memory and one half for the kernel. For simple reasons the kernel's half is not mapped entirely, but what to do when the kernel needs to map some memory for eg. drivers, his heap etc? Should it iterate through all address spaces and update the kernel's half accordingly? How do you handle this?
- BASICFreak
- Member
- Posts: 284
- Joined: Fri Jan 16, 2009 8:34 pm
- Location: Louisiana, USA
Re: kernel memory mapping
I handle this by allocating all the Page Tables that the Kernel can use.
Usually empty Page Tables, but a way non the less to setup all the kernel space without actually mapping any of it.
Which allows me to modify every Page Directory at once, instead of having to update every process's PageDIR when the Kernel requires more data.
Without PAE and PSE, and assuming you are using 2GB virtual (way too much in my opinion) you only have to allocate 2MB of space for the whole structure.
Usually empty Page Tables, but a way non the less to setup all the kernel space without actually mapping any of it.
Which allows me to modify every Page Directory at once, instead of having to update every process's PageDIR when the Kernel requires more data.
Without PAE and PSE, and assuming you are using 2GB virtual (way too much in my opinion) you only have to allocate 2MB of space for the whole structure.
BOS Source Thanks to GitHub
BOS Expanded Commentary
Both under active development!
BOS Expanded Commentary
Both under active development!
Sortie wrote:
- Don't play the role of an operating systems developer, be one.
- Be truly afraid of undefined [behavior].
- Your operating system should be itself, not fight what it is.
Re: kernel memory mapping
That was my first idea, but then I have to allocate 2MB, which is not that little.
- BASICFreak
- Member
- Posts: 284
- Joined: Fri Jan 16, 2009 8:34 pm
- Location: Louisiana, USA
Re: kernel memory mapping
Well don't use 2GB for the kernel, I only use 512MB (>=E0000000) - which is much more manageable, IMO. Then you only have to allocate 1/2 MB.
BOS Source Thanks to GitHub
BOS Expanded Commentary
Both under active development!
BOS Expanded Commentary
Both under active development!
Sortie wrote:
- Don't play the role of an operating systems developer, be one.
- Be truly afraid of undefined [behavior].
- Your operating system should be itself, not fight what it is.
Re: kernel memory mapping
Hi,
This means that (e.g.) if you are using PAE then it makes sense to sure kernel space is a multiple of 1 GiB so that you can efficiently pre-allocate page directories for kernel space and don't need to worry. In a similar way, if you're using long mode you might consider making kernel space a (relatively small) multiple of 512 GiB so you can pre-allocate Page Directory Pointer Tables.
However; sometimes pre-allocating isn't very nice. For example, if your kernel space is 1 GiB and you're using "plain 32-bit paging" you'd have to pre-allocate 256 page tables and it's going to cost you 1 MiB of RAM (which is a lot on old computers that don't support PAE and might only have 8 MiB of RAM to start with).
To avoid pre-allocating you have update something in all virtual address spaces. There are 2 main ways of doing this:
Note 1: For all of the above I've assumed that you're able to access all paging structures for the current virtual address space (e.g. using the recursive mapping trick).
Note 2: For all of the above I've ignored TLB invalidation. That's a different/complicated problem all on its own!
Cheers,
Brendan
First, understand that:Roflo wrote:So the address space of an user process consists of one half for the user's memory and one half for the kernel. For simple reasons the kernel's half is not mapped entirely, but what to do when the kernel needs to map some memory for eg. drivers, his heap etc? Should it iterate through all address spaces and update the kernel's half accordingly? How do you handle this?
- If a page table is used by all virtual address spaces, you can add/remove/modify page table entries and the changes will automatically happen in all virtual address spaces.
- For PAE and long mode; if a page directory is used by all virtual address spaces, you can add/remove/modify page directory entries and the changes will automatically happen in all virtual address spaces.
- For long mode; if a page directory pointer table is used by all virtual address spaces, you can add/remove/modify PDPT entries and the changes will automatically happen in all virtual address spaces.
This means that (e.g.) if you are using PAE then it makes sense to sure kernel space is a multiple of 1 GiB so that you can efficiently pre-allocate page directories for kernel space and don't need to worry. In a similar way, if you're using long mode you might consider making kernel space a (relatively small) multiple of 512 GiB so you can pre-allocate Page Directory Pointer Tables.
However; sometimes pre-allocating isn't very nice. For example, if your kernel space is 1 GiB and you're using "plain 32-bit paging" you'd have to pre-allocate 256 page tables and it's going to cost you 1 MiB of RAM (which is a lot on old computers that don't support PAE and might only have 8 MiB of RAM to start with).
To avoid pre-allocating you have update something in all virtual address spaces. There are 2 main ways of doing this:
- Map whatever needs to be changed into kernel space so it can be accessed. For example (the same "plain 32-bit paging with 1 GiB of kernel space" example) you could map the page directory for every virtual address space as a normal page into kernel space, so that when you add/remove page tables for kernel space you can modify the page directory entry for all page directories (for all virtual address spaces).
- Don't update the other virtual address space until just before after you switch from one virtual address space to another. For example (the same "plain 32-bit paging with 1 GiB of kernel space" example) you could have a master list of 256 page directory entries in normal RAM (in a safe part of kernel space), and whenever you switch to a new virtual address space you can copy the data from that master list into the virtual address space's page directory before it's used.
Note 1: For all of the above I've assumed that you're able to access all paging structures for the current virtual address space (e.g. using the recursive mapping trick).
Note 2: For all of the above I've ignored TLB invalidation. That's a different/complicated problem all on its own!
Cheers,
Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Re: kernel memory mapping
I came up with an idea similar to your last point: Having one Pagedirectory with the lower half as kernel and when switching the upper half of the new pd gets copied to this one pd (plus cache flush).