A small paging question

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
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

A small paging question

Post by Jeko »

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?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Hi,

You can avoid this by creating page tables for the entire kernel space before you start cloning the page directory. Say your kernel space is 3GB upwards, this wastes 1MB of physical RAM but is well worth it for the reduction in complexity!

Cheers,
Adam
speal
Member
Member
Posts: 43
Joined: Wed Mar 07, 2007 10:09 am
Location: Minneapolis, Minnesota
Contact:

Post by speal »

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.
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post by Jeko »

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).
Weren't entries 1024?
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.
How can I handle this?
However when I change some values in the 2nd level table, I must update all page directories, or not?
speal
Member
Member
Posts: 43
Joined: Wed Mar 07, 2007 10:09 am
Location: Minneapolis, Minnesota
Contact:

Post by speal »

MarkOS wrote:Weren't entries 1024?
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:How can I handle this?
However when I change some values in the 2nd level table, I must update all page directories, or not?
You won't need to update multiple virtual address spaces because every address space incorporates the same 2nd level tables.

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...
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post by Jeko »

speal wrote:
MarkOS wrote:Weren't entries 1024?
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:How can I handle this?
However when I change some values in the 2nd level table, I must update all page directories, or not?
You won't need to update multiple virtual address spaces because every address space incorporates the same 2nd level tables.

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?
(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.
speal
Member
Member
Posts: 43
Joined: Wed Mar 07, 2007 10:09 am
Location: Minneapolis, Minnesota
Contact:

Post by speal »

You don't need anything that complicated. Just reference the same tables in every address space if you want them to be the same.

Image

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
tables.png (13.26 KiB) Viewed 1614 times
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post by Jeko »

speal 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.

Image

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.
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 B
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

you can change the shared table if you want to map additional pages, you can also preload a few page tables without using them.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Jeko
Member
Member
Posts: 500
Joined: Fri Mar 17, 2006 12:00 am
Location: Napoli, Italy

Post by Jeko »

Combuster wrote:you can change the shared table if you want to map additional pages, you can also preload a few page tables without using them.
but if I have to add a page table, I must update all address space (if there isn't the global page extension). It's right?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

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