Page 1 of 1
Paging
Posted: Fri Nov 25, 2005 2:42 am
by B.E
For the last 6 month I have not been able to work on my os. I've decided to start working on it ;D.
I was thinking of a memory management. This is what my OS will do. it will have a different segment for device i/o, pagetable, user programs and the kernal. like this
[tt]
FFFFFFFFh +--------------------------+
| Kernal data |
C0000000h +--------------------------+
| |
| User programs |
| |
01400000h +--------------------------+
| PageTables |
01000000h +--------------------------+
| PCI device i/o |
00010000h +--------------------------+
[/tt]
I know that most PCI devices are mapped to below 16 meg. This way the there is more of a strict protection mechinism. Any thoughts?
Re:Paging
Posted: Fri Nov 25, 2005 3:40 am
by Cjmovie
Well, I don't see any problems, I just think theres something I need to clarify.
Considering where you have your kernel, this is _virtual_ address space. PCI devices only need to have access to the first 16mb of _physical_ memory. This means you could map the first 16mb, say, even somwhere in the kernel.
Also: Although it's not really a 'problem', I still tend to like having user programs (or anything but the kernel for that matter, considering my OS is a microkernel) mapped at virtual addresses of 0.
Re:Paging
Posted: Fri Nov 25, 2005 3:44 am
by AR
Paging does not require everything be present at the same time. The PCI devices can be mapped anywhere in memory using paging (And IIRC remapped physically using PCI PnP). The page tables are usually self mapped somewhere in kernel space and the entirety of the low 3GB is given to the program. [Additionally, I think you may have confused PCI with ISA, PCI is a 32bit bus IIRC]
You only need PCI memory mapped for drivers so it doesn't need to be present at any other time unless you want to permanently map it into kernel space.
@Cjmovie: How do you intend to trap NULL pointers if the program is at 0? Generally programs are loaded away from 0 (Windows starts at 4MB virtual) so that accidentally trying to use a pointer set to NULL will page fault rather than creating a difficult to trace bug caused by random garbage being read.
Re:Paging
Posted: Fri Nov 25, 2005 4:06 am
by B.E
Thank you both, I will rethink it more.
Re:Paging
Posted: Fri Nov 25, 2005 6:52 am
by bluecode
AR wrote:How do you intend to trap NULL pointers if the program is at 0? Generally programs are loaded away from 0 (Windows starts at 4MB virtual) so that accidentally trying to use a pointer set to NULL will page fault rather than creating a difficult to trace bug caused by random garbage being read.
At virtual address is the code segment, the data segment starts later. Why do you want a page fault, when using null pointer? You could make the code segment read-only, so that a write to that location page-faults...
Re:Paging
Posted: Fri Nov 25, 2005 7:59 am
by AR
bluecode wrote:At virtual address is the code segment, the data segment starts later. Why do you want a page fault, when using null pointer? You could make the code segment read-only, so that a write to that location page-faults...
Who said anything about writing? An uninitalised pointer which is then used to load a data structure, say the GUI's window title, suddenly you're reading the code segment and pulling in garbage.
The reason for page faulting should be obvious... Zero/NULL is a "non-address" therefore accessing it should be invalid.
Re:Paging
Posted: Fri Nov 25, 2005 8:26 am
by Kemp
I agree, the C standard says a value of 0 should be interpreted as a null address and makes it quite hard to actually use an address of 0. This is because a lot of systems are designed to catch 0 as a bad address because it'll tend to mean a pointer was uninitialised.
Re:Paging
Posted: Fri Nov 25, 2005 8:59 am
by bluecode
Re:Paging
Posted: Fri Nov 25, 2005 9:38 am
by Pype.Clicker
From
ten commandments for the C programmer
Thou shalt not follow the NULL pointer, for chaos and madness await thee at its end.
Clearly the holy scriptures were mis-transcribed here, as the words should have been ``null pointer'', to minimize confusion between the concept of null pointers and the macro NULL (of which more anon). Otherwise, the meaning is plain. A null pointer points to regions filled with dragons, demons, core dumps, and numberless other foul creatures, all of which delight in frolicing in thy program if thou disturb their sleep. A null pointer doth not point to a 0 of any type, despite some blasphemous old code which impiously assumes this.
So following the commandment, doing
is wrong code (since it assumes ptr=NULL implies !ptr evaluates as 'true' ...).
I'm doing it daily, though
Re:Paging
Posted: Fri Nov 25, 2005 11:36 am
by Kemp
Things like this are actually mentioned in the C standard as being perfectly fine.
translates to
if you expand out the shortcut, which is defined as meaning "if the pointer is null" by the standard (a special case for pointers in which any use of 0 should be assumed to be null).
Are we off-topic right now btw? I can't decide if this is related or not.
Re:Paging
Posted: Sat Nov 26, 2005 8:40 am
by Pype.Clicker
we certainly drifted to another topic ^_^
Okay... so the standard say that "char* ptr=0" is a valid writing for setting ptr as a null pointer... What it doesn't enforce, however, is whether the 'null' pointer is (or not) at virtual address 0. It could very well be somewhere else (and if that's the case we may be in trouble to create a pointer towards virtual address 0 since ptr=0 would still be a null pointer assignment).
That being said, the GCC compiler uses null at virtual address 0, and i don't see how this could be changed.
PS: Solar, gimme a good hammer swing if i misinterpreted the words of the prophet ANSI here ...
Re:Paging
Posted: Sat Nov 26, 2005 9:57 am
by fraserjgordon
Well, I found that when using certain versions of GCC (can't remem ber which unfortunately), null pointers were actually pointing at 0xFFFFFFFF, at least on the x86 target.
Personally, I think that is a much better place for a NULL pointer to be - its in Kernel space so it will cause a page fault if any user program tries to read or write there, plus it doesn't prevent you from linking programs to start at 0.
Re:Paging
Posted: Sat Nov 26, 2005 10:17 am
by Kemp
Pype, that was my original point, lol. Even if you decide to not use 0 as a null address you'll have difficulty using it for real purposes (at least in standard C) due to it being a special case. That said, there are ways to achieve it (as always).