Page 1 of 1

Kernel Address Space

Posted: Mon Dec 04, 2006 3:27 pm
by vialator
The operating system I'm developing uses the first gig of virtual memory to hold everything kernel related, including the binary image, BSS, the heap. The heap is used, of course, for things like the linked lists that have process information. I have made it so that for that every process created, the first 4 mb are also mapped to the kernel image, so that when an interrupt is called, it doesn't switch address spaces, and can run the handler while still in the process's context. However, when something like a system call occurs, and I will need to access kernel data, for things like allocating more memory to the process, do I have to switch address spaces, for every single system call? That seems rather inefficient. Another solution would be to map the entirety of all allocated kernel address space in userspace as well (much like the first 4mb), but it would require each time kmalloc is called to update every single process's page table. Any ideas?

Posted: Mon Dec 04, 2006 3:56 pm
by Tyler
I map the whole of kernel space... including the entire heap.. mapped and unmapped into all user spaces... then there is no need to play around with page tables every time you call kmalloc because the page tables are already mapped.

Posted: Mon Dec 04, 2006 6:45 pm
by vialator
What I mean is that the kernel memory (a gigabyte) is obviously not gonna be completely mapped. Probably only the first few megabytes will be used, but whenever this needs to be changed (ie, sbrk is called) in the kernel, because it needs more space, it would have to correspondly change the mappings in every PD of every process.

Posted: Mon Dec 04, 2006 7:26 pm
by Brendan
Hi,
vialator wrote:What I mean is that the kernel memory (a gigabyte) is obviously not gonna be completely mapped. Probably only the first few megabytes will be used, but whenever this needs to be changed (ie, sbrk is called) in the kernel, because it needs more space, it would have to correspondly change the mappings in every PD of every process.
You can map every page directory into kernel space, so that you can add page tables in them without changing address spaces....


Cheers,

Brendan

Posted: Tue Dec 05, 2006 1:23 am
by vialator
Well, I found a solution that your previous post inspired. I'm going to allocate a page for each of the Page Tables for the first gigabyte of memory. This will "waste" 1 megabyte, but will make it so the addresses of the page tables are already known, and at process creation, can be set, so that they don't have to be edited later. When the kernel actually needs to allocate memory, it already has the page tables to add to.

Posted: Tue Dec 05, 2006 10:42 am
by ces_mohab
Brendan wrote: You can map every page directory into kernel space, so that you can add page tables in them without changing address spaces....
exactly what i have done but, what shall we do if we run out of page tables and need to allocate a new page dir.

We will also update all processes.

Posted: Tue Dec 05, 2006 3:49 pm
by vialator
Hence why i've already allocated space for all possible page tables. It will make that unneeded.

Posted: Wed Dec 06, 2006 1:24 am
by Brendan
Hi,
ces_mohab wrote:
Brendan wrote:You can map every page directory into kernel space, so that you can add page tables in them without changing address spaces....
exactly what i have done but, what shall we do if we run out of page tables and need to allocate a new page dir.
That doesn't create any problem - allocate a new physical page for the new page table, insert the page table into all page directories, then allocate a new physical page for the new page directory and map it into the new page table in kernel space.

It doesn't necessarily need to be done in that order either. For example, you could allocate a new physical page for the new page directory and try to map it into kernel space and find out there isn't enough room; then allocate a new physical page for a new page table, insert the page table into all page directories, and try to map it into kernel space again.

In both of these cases you may need code to back out of the operation (for e.g. if you can't allocate the second page because there isn't enough memory, free the previously allocated page before returning an "out of memory" error).


Cheers,

Brendan