Page 1 of 2

Switching page directories on a task switch??

Posted: Sat May 03, 2003 11:56 pm
by Perica
..

Re:Switching page directories on a task switch??

Posted: Sun May 04, 2003 12:07 am
by tom1000000
Hey even I know the answer to this one.

Each process must have its own page table structure. Otherwise you can't stop one process from reading or writing to another process's memory.

If you use hardware task switching, the TSS automatically changes the PDBR for you. If you use software task switching, I guess your code has to be in a page that is mapped identically for all processes. Then when you change CR2 (whatever the PDBR is), it won't crash your code.

I'm sure some other people can give a better description.

Re:Switching page directories on a task switch??

Posted: Sun May 04, 2003 12:35 am
by Perica
..

Re:Switching page directories on a task switch??

Posted: Sun May 04, 2003 1:17 am
by tom1000000
Excuse me but I did provide a reasonable answer to your questions, I might have to explain things more clearly to you.

If process A and process B use the SAME page tables eg the SAME CR3 then process A can read / write to process B's memory.

If you want to design an OS this way I suggest you find another hobby.

As for whether CR3 contains a virtual or physical address, I presume all the values in the page directories / tables and CR3 are physical addresses. Otherwise, you would have an infinite loop when trying to resolve a virtual address.

I have only got identity mapping working so far in my OS so I'm not an expert.

Re:Switching page directories on a task switch??

Posted: Sun May 04, 2003 1:19 am
by tom1000000
tom1000000 wrote: Hey even I know the answer to this one.

Each process must have its own page table structure. Otherwise you can't stop one process from reading or writing to another process's memory.

If you use hardware task switching, the TSS automatically changes the PDBR for you. If you use software task switching, I guess your code has to be in a page that is mapped identically for all processes. Then when you change CR2 (whatever the PDBR is), it won't crash your code.

I'm sure some other people can give a better description.

Re:Switching page directories on a task switch??

Posted: Sun May 04, 2003 1:20 am
by tom1000000
Ooops I made a mistake with above post it should be CR3 not CR2.

Re:Switching page directories on a task switch??

Posted: Sun May 04, 2003 1:49 am
by Ozguxxx
Hi...
why the page directory needs to be changed when a task switch occurs?? I want to make one page directory and set of page tables for the entire system. Is this possible?? Is there something wrong with doing this??
It does not need to be changed you can just use same page directory for all tasks by setting same cr3 entry in the tss's. I fact currently I am doing in that way so there is nothing wrong about it. But you have to take care of the fact that since all tasks share same address space, they should not interfere with each other. Good luck.

Re:Switching page directories on a task switch??

Posted: Sun May 04, 2003 5:30 am
by Pype.Clicker
Perica Senjak wrote: Hey,

First of all i'd like to know, is the page directory address that is placed into cr3 a Physical Linear Address?? (If the page directory gets paged, which it has to if you want to edit it -- does the value in cr3 have to be a virtual address?); It is probably a physical address, but i just want to make sure.....
The value in CR3 is the physical address of the page directory. It is not translated by the paging unit (how could it be as it sets up the entry point of paging ?) nor by the segmentation unit. It is used as is to access physical memory (one of the only case where a address is used this way, afaik)
Also, what is this about changing page directories on a task switch? Why would you need to? Does anybody know of good documentation that explains why some Operating Systems do this??
I'm not 100% sure of what you mean here, but if you want to implement multiple address space and keep a flat addressing mode (this is, keep the Unix programming world), then using a separated page directory (maybe sharing some system page tables among every process) for each process is a possible way of having as much processes as you want.

Another option is to have several process in the same address space and use segmentation and separated LDT to prevent them to interfere, but this way you will be limited to 4GB of addresses for ALL the processes in the same space, rather than 4GB of addresses PER process.

Finally, you can decide you don't need to change the page directory, but replace manually some entries in it. I wouldn't do it if i were you because it sounds pretty slow.
I was thinking of making one page directory and set of page tables that get used by the entire system, the page directory doesn't get changed on a task switch. Has anybody else done this? Do you think this is a bad idea?? If so, please state your reason why.....
Yes, it has been done in the past and is still used in many network-oriented operating systems (including CISCO IOS, iirc) and is known as "Single Address Space" Operating system. I also think MacOS 8 was roughly based on this structure. It quickly shows its limits, though, especially when you plan to deal with large and numberous programs.

Re:Switching page directories on a task switch??

Posted: Sun May 04, 2003 6:30 am
by Perica
..

Re:Switching page directories on a task switch??

Posted: Sun May 04, 2003 9:30 am
by Whatever5k
Perica Senjak wrote: But how could you have one page directory for every task?? An entire page directory will all of the page tables filled in (all of the memory maped), is 4mb..... this would consume way too much memory, how is this done?
Nope, not really. A page table can *map* 4MB of physical memory, but that doesn't mean that it needs that much memory. The page table just holds integer values, and those integers don't need 4MB, believe me ;)
A Page Table Entry (PTE) needs 4 bytes, that means a page table needs 1024 * 4 = 4096 bytes = 4 kb
Lastly, what method does Linux/Unix/Other Unix Clones use?? Do they allow each task to have it's own 4gb address space?? What method does Clicker32 use??
They use the 4GB linear address space.

Re:Switching page directories on a task switch??

Posted: Sun May 04, 2003 3:10 pm
by Pype.Clicker
Perica Senjak wrote: But how could you have one page directory for every task?? An entire page directory will all of the page tables filled in (all of the memory maped), is 4mb..... this would consume way too much memory, how is this done?
In fact there are several techniques used to reduce the cost of page tables. One of the most obvious being of course not to allocate page tables where there is no need for (i.e. the memory under these 4MB is actually unused).

Another technique is to share page tables: tables describing the kernel pages can be reused by all the directories (if your kernel lies at C000 0000 .. FFFF FFFF, like linux do, then the last 256 entries of the directory points to the very same tables in every address spaces, thus those tables can be in only one instance :)

Finally (but not least), when a process is cloned (using fork, f.i.) you can assume that initially, page tables will be kept the same, but you tag them read only and only create a copy when a writing operation is made (first you copy the table, then the page itself :)
Lastly, what method does Linux/Unix/Other Unix Clones use?? Do they allow each task to have it's own 4gb address space?? What method does Clicker32 use??

Ciao.
Linux and unix clones i know use 4GB address space, (usually 1GB for the kernel and 3GB for user level). Windows NT (and probably windows 9x aswell, though i'm uncertain about this) series have 4GB address space.

Clicker will also have, though i reserve myself the right to spawn several small processes in their parent address space (i hope boosting small shell utilies and so on :)

Re:Switching page directories on a task switch??

Posted: Tue May 06, 2003 1:02 am
by Perica
..

Re:Switching page directories on a task switch??

Posted: Tue May 06, 2003 4:59 am
by Pype.Clicker
Perica Senjak wrote: So Linux and Windows doesn't give each task it's own 4gb address space??
I think there is a misunderstanding about what is an address space ... Afaik, the address space is defined as the virtual memory the process can "see", without modifying page mapping (of course). whether the kernel is mapped in every address space or not do not restrict the address space itself (you still can address 4GB of memory, but you will no longer have the rights to read or write to the whole address space :)

Even in kernel mode, there are pages you cannot access in your address space (for instance, because they are absent), but this doesn't make them out of your address space :)

Now, if what you meant is "are there system that have 4GB of USER space" -- thus using 100% of the address space for user-mode processing, the answer is of course "no if the system has a protected kernel" (i.e. if it is not a library that the user calls, but also can damage, ? la MS-DOS ;)
Think about it: your user process should at least be interruptible and should at least be able to issue system calls ... In order to allow this, you need at least the GDT, the IDT and a few TSS to be present in EVERY address space, and thus being protected from the user, but these protected areas may be very small (they take 16Mb in Clicker)

I think a glance at MMURTL's book (paging chapter) may help you figuring out how things may go ...

Re:Switching page directories on a task switch??

Posted: Tue May 06, 2003 5:20 am
by Perica
..

Re:Switching page directories on a task switch??

Posted: Tue May 06, 2003 5:56 am
by Pype.Clicker
Linux & windows do give each process a dedicated page directory.

putting several process in the same address space, and protect them using segmentation is of course possible, but it gives you less programming flexibility (for instance, if a process wish to map a 1GB file in memory, you'll be very limitated in the number of process you can run).

The same way, if every process needs to access a 20MB shared library, each process' address space will grow up by 20MB - even if you can share physical page, you can hardly share virtual addresses aswell, except if your library comes in another code segment ...

It may be a good idea to do so for small tool programs of a GUI, for instance (as they require little resource and share a lot of controls, etc. and you can decently present the GUI library as a separate process running in the same address space with additionnal priviledges and communicating through shared data segments ...)

But if you want to run 2 instances of YourWord plus a 3D modeller, plus Nutella clone and a FTP server ... welll .. hum ... i think you'll have to use a separate address space for each ...