Kernel Address Space

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
vialator
Posts: 5
Joined: Mon Dec 04, 2006 3:22 pm
Location: Virginia Tech

Kernel Address Space

Post 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?
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Post 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.
vialator
Posts: 5
Joined: Mon Dec 04, 2006 3:22 pm
Location: Virginia Tech

Post 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.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Post 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
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.
vialator
Posts: 5
Joined: Mon Dec 04, 2006 3:22 pm
Location: Virginia Tech

Post 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.
User avatar
ces_mohab
Member
Member
Posts: 77
Joined: Wed Oct 18, 2006 3:08 am

Post 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.
To write an OS you need 2 minds one for coding and other for debugging.
vialator
Posts: 5
Joined: Mon Dec 04, 2006 3:22 pm
Location: Virginia Tech

Post by vialator »

Hence why i've already allocated space for all possible page tables. It will make that unneeded.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Post 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
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.
Post Reply