Page 2 of 2

Re:Clarification on some aspects of paging.

Posted: Sat Nov 12, 2005 12:31 pm
by bluecode
Nelson wrote: Is there a difference between a page being global and simply mapping it into every processes address space?
Yes, the tlb entries containing pages mark as global are not invalidated when cr3 is reloaded. Every page marked as global should/must be in every address space at the same address or you might get undefined results. But global pages are not supported by the i386...imho the first processor which supports them is the pentium2, but I'm not sure

Re:Clarification on some aspects of paging.

Posted: Sat Nov 12, 2005 12:36 pm
by Candy
The global bit is only a hint to the TLB saying "This page should be kept on address space switches, because it's like in all of them". If you don't set it, it'll invalidate the entire tlb every time you switch, causing unneeded agony and slowdown. If you say stuff is global that isn't, you can (note, no certainty) get ghost pages that appear to be mapped somewhere but aren't.

The TLB is the peeking note of the processor. If you tell it some stuff is always true and it isn't, it'll make mistakes. If you tell it to remove all notes each time, it'll go slow. Tell it which pages actually are global with the bit and all will be fine.

Re:Clarification on some aspects of paging.

Posted: Sat Nov 12, 2005 12:44 pm
by Warrior
Oh okay, so it just helps the TLB and speeds things up.

When I finish implementing this simple code to allow physical addresses to be mapped to virtual would I be able to map my kernel to the 3GB mark (withought having used higher half bare bones prior to this) and then map other things such as video memory and the rest of it and then (while paging is enabled) I will have removed all physical addressing in my kernel (for the most part).

I'm also guessing I'd have to build another malloc to feed out virtual addresses from the kernels mapped heap too.

Sounds like a lot to code but I think I'll like the end result :)

Re:Clarification on some aspects of paging.

Posted: Mon Nov 14, 2005 3:33 pm
by Warrior
Okay, what I am currently doing is mapping the whole 4GB as Supervisor, RW, Present. I know this is a bad idea to have all the memory marked as present and in memory when not all of it is in use but until I implement disk access to store pages on this is how it will be.

I plan to ontop of this add the ability to map regions of memory (changing flags of individual PTEs and invalidating) since not everything may want to be Superivsor, RW, Present.

Is it possible to do this to save memory: On your "free pages window" mark them all as non present and store them on disk, then once allocated mark them as present again reload them onto the disk.

When storing pages on the disk do you remove the memory (physical, maybe virtual if I mapped them to the virtual address) that the pages occupy?

Thanks
Nelson

Re:Clarification on some aspects of paging.

Posted: Mon Nov 14, 2005 4:38 pm
by JAAman
even without disk-swapping, you should only map as present the pages that are in physical memory,

the reason is:
if you attempt to access a "not present" page, you will get a page fault, and if you shouldn't you'll know you have an error
but if you access a page that isn't in physical memory but is mapped as present, you won't get any exception, and therefore you won't get the hint about the invalid memory access -- it will just return the wrong values (and in some cases might return valid values, which may make it appear to work properly -- but it isn't)

Re:Clarification on some aspects of paging.

Posted: Mon Nov 14, 2005 7:47 pm
by Warrior
But how is it not going to be in memory withought disk access?

Re:Clarification on some aspects of paging.

Posted: Mon Nov 14, 2005 8:36 pm
by Warrior
Actually I forgot about the TLB.

So just map the pages as non present, remove them from memory (until I have disk access) then when a page fault occurs simply load it back into memory (Maybe by remembering what flags it had) and invalidate it.

Re:Clarification on some aspects of paging.

Posted: Mon Nov 14, 2005 9:32 pm
by AR
The paging to disk is a virtual feature, it is not part of paging, merely a mechanism made possible by it.

You do not need to map the whole address space, just the bits that you're actually using, this is also useful for finding wayward pointers as was already stated.

Re:Clarification on some aspects of paging.

Posted: Mon Nov 14, 2005 10:19 pm
by Warrior
Do you still recieve a pagefault even if the memory isn't mapped at all? (Not mapped as Non Present)

Re:Clarification on some aspects of paging.

Posted: Mon Nov 14, 2005 11:39 pm
by AR
The CPU doesn't care, if the page is marked present but the rest of the bits are all 0 then you'll end up with that virtual address being supervisor, read-only for physical 0x00000000.

Here's a basic representation of the decisions the CPU makes when accessing a page:

Code: Select all

int OperatePage(uintptr_t PageFlags, bool User, bool Write)
{
    if (!(PageFlags & PG_PRESENT))
       throw PageFault; //Page not present

    if (User)
    {
       if (!(PageFlags & PG_USER))
           throw PageFault; //Supervisor page, user read/write
       if (Write && !(PageFlags & PG_WRITE))
           throw PageFault; //User write, read-only page
    }
    else
    {
       if (CR0.WP)
       {
          if (PageFlags & PG_USER) /* Not certain about this one */
              throw PageFault; //WriteProtect on, supervisor, user page
          if (Write && !(PageFlags & PG_WRITE))
              throw PageFault; //WriteProtect on, supervisor, read-only page
        }
    }

    //Page is OK
    return 0;
}
PageFlags is the PDE or PTE.

Writing to a page that is mapped on non-space is the same as writing to non-space without paging (ie. you have 128MB of RAM and you try to write to somewhere around 0x30000000 in physical space), you'll float the memory bus.

Re:Clarification on some aspects of paging.

Posted: Mon Nov 14, 2005 11:42 pm
by Phugoid
Yes. "Mapped as not present" is the very definition of not mapped.

Re:Clarification on some aspects of paging.

Posted: Tue Nov 15, 2005 11:10 am
by JAAman
AR wrote: if (PageFlags & PG_USER) /* Not certain about this one */
throw PageFault; //WriteProtect on, supervisor, user page
yes, this should throw a #PF, this is used for COW within the kernel, and should be equally valid in user or superviser mode


the CPU has no concept of non-memory -- it has no knowledge of what physical memory addresses are valid, and will recieve values from non-existant ram -- but it will be unreliable, on some MBs it might be 0, on others it might be -1, and others may be the last value read/written, and others it may be random data representing electrical noise

as i said, best-case you will recieve invalid data (easy to track)
but sometimes you might obtain valid data, which is incorrect (which is much harder to track down and correct)