A small paging question
A small paging question
I'm implementing paging in my small OS. I'm now developing the mapping of pages. I have a small question...
I need to update the page directory of each task when there is a change in the memory of the kernel?
I need to update the page directory of each task when there is a change in the memory of the kernel?
I do this in long mode, but the concept is the same.
The top-level paging structure (page directory, if you prefer) has 512 entries.
The bottom 255 correspond to lower memory, and the upper 256 correspond to upper memory (or whatever division you choose).
The previous post suggested that you create a 2nd level table for all of the indices in kernel space. That means there will be 256 2nd level page directories referenced by the top 256 entries into the page directory.
Each virtual memory space has its own page directory, but in all of them, the top 256 entries point to these same 2nd level tables. This way, changes to memory in the kernel space are propagated through to all virtual address spaces. Just remember you can't remove a 2nd level table if it's in the kernel space or you'll have to go update every process' page directory.
You'll waste some memory initially, but down the line you save memory since there will only be one set of paging structures for kernel memory instead of a copy for each process.
The top-level paging structure (page directory, if you prefer) has 512 entries.
The bottom 255 correspond to lower memory, and the upper 256 correspond to upper memory (or whatever division you choose).
The previous post suggested that you create a 2nd level table for all of the indices in kernel space. That means there will be 256 2nd level page directories referenced by the top 256 entries into the page directory.
Each virtual memory space has its own page directory, but in all of them, the top 256 entries point to these same 2nd level tables. This way, changes to memory in the kernel space are propagated through to all virtual address spaces. Just remember you can't remove a 2nd level table if it's in the kernel space or you'll have to go update every process' page directory.
You'll waste some memory initially, but down the line you save memory since there will only be one set of paging structures for kernel memory instead of a copy for each process.
Weren't entries 1024?speal wrote:The top-level paging structure (page directory, if you prefer) has 512 entries.
The bottom 255 correspond to lower memory, and the upper 256 correspond to upper memory (or whatever division you choose).
How can I handle this?speal wrote:The previous post suggested that you create a 2nd level table for all of the indices in kernel space. That means there will be 256 2nd level page directories referenced by the top 256 entries into the page directory.
However when I change some values in the 2nd level table, I must update all page directories, or not?
Sorry about that. In long mode, the entries are twice as large to accommodate a 64 bit address. In 32 bit modes, page tables hold 1024 entries.MarkOS wrote:Weren't entries 1024?
You won't need to update multiple virtual address spaces because every address space incorporates the same 2nd level tables.MarkOS wrote:How can I handle this?
However when I change some values in the 2nd level table, I must update all page directories, or not?
Take a simple example - one with four entires per table, and 2 levels. The top level has addresses of 2nd level tables, and the 2nd level has physical memory addresses for the mapped pages.
A top level table is at memory location A:
A: {B, C, D, E}
then the second level tables:
B: {F, G, H, I}
C: {J, K, L, M}
D: {N, O, P, Q}
E: {R, S, T U}
So memory locations F through U are mapped into virtual memory by the paging structure. Lets create another address space with the same upper-half:
V: {W, X, D, E}
Notice that D and E are in both this table, AND the table a memory location A. These entries reference the same physical memory locations, so any changes to addresses falling under these entries (upper half) will be applied to all address spaces.
I hope my notation is clear...
Have you any source code of a small (or big) operating system that does this?speal wrote:Sorry about that. In long mode, the entries are twice as large to accommodate a 64 bit address. In 32 bit modes, page tables hold 1024 entries.MarkOS wrote:Weren't entries 1024?
You won't need to update multiple virtual address spaces because every address space incorporates the same 2nd level tables.MarkOS wrote:How can I handle this?
However when I change some values in the 2nd level table, I must update all page directories, or not?
Take a simple example - one with four entires per table, and 2 levels. The top level has addresses of 2nd level tables, and the 2nd level has physical memory addresses for the mapped pages.
A top level table is at memory location A:
A: {B, C, D, E}
then the second level tables:
B: {F, G, H, I}
C: {J, K, L, M}
D: {N, O, P, Q}
E: {R, S, T U}
So memory locations F through U are mapped into virtual memory by the paging structure. Lets create another address space with the same upper-half:
V: {W, X, D, E}
Notice that D and E are in both this table, AND the table a memory location A. These entries reference the same physical memory locations, so any changes to addresses falling under these entries (upper half) will be applied to all address spaces.
I hope my notation is clear...
(with source code I can understand well because I'm Italian and don't know English very well)
EDIT: But I have another idea:
I have a variable in each task to track changes in kernel memory.
If the kernel memory changes, I update the address space of the current task and add 1 to the variable.
When there is a task switch I control the variable of the previous task, if it's different from the next task variable, then I update the next task address space.
You don't need anything that complicated. Just reference the same tables in every address space if you want them to be the same.
Here is a diagram of two top-level tables (tables A and B), each with two page tables. One of the tables is shared by both (table D). This shared table will hold mappings for your kernel space - it will be the same in both address spaces. The other two tables (C and E) are unique to their respective address spaces.
I don't think you'll get anything out of seeing code for this if the diagram doesn't help. If you're completely puzzled by this, perhaps you should do some more reading on how page tables work. The system is used in nearly OS with paged virtual memory, so it shouldn't be hard to track down some code.
Here is a diagram of two top-level tables (tables A and B), each with two page tables. One of the tables is shared by both (table D). This shared table will hold mappings for your kernel space - it will be the same in both address spaces. The other two tables (C and E) are unique to their respective address spaces.
I don't think you'll get anything out of seeing code for this if the diagram doesn't help. If you're completely puzzled by this, perhaps you should do some more reading on how page tables work. The system is used in nearly OS with paged virtual memory, so it shouldn't be hard to track down some code.
- Attachments
-
- tables.png (13.26 KiB) Viewed 1613 times
I understand this. But if the kernel alloc a new page table, how can I insert this page table into A and also into B? I must update address spaces of both A and Bspeal wrote:You don't need anything that complicated. Just reference the same tables in every address space if you want them to be the same.
Here is a diagram of two top-level tables (tables A and B), each with two page tables. One of the tables is shared by both (table D). This shared table will hold mappings for your kernel space - it will be the same in both address spaces. The other two tables (C and E) are unique to their respective address spaces.
I don't think you'll get anything out of seeing code for this if the diagram doesn't help. If you're completely puzzled by this, perhaps you should do some more reading on how page tables work. The system is used in nearly OS with paged virtual memory, so it shouldn't be hard to track down some code.
Please see my original reply. To expand - if you map all page tables in shared space first, you do not need to touch page directories. If you want a *new* page table (for IPC or because you have not mapped all page tables at the oustset) then yes, you need to update each memory space affected.
Cheers,
Adam
Cheers,
Adam