I'm writing a toy kernel and am quite curious how multiple processes can access the same virtual memory addresses. What I'm trying to say is, if I map the address 0x00000000 to 0x10800000 in physical space (using paging) how can separate process have their own protected memory that starts at 0 and goes to the end of their allocated memory? do they have to have separate mappings or...? I'd just like to get some clarification on this. Thanks
Asked
Active
Viewed 163 times
1
-
Yes, each process has their own memory mappings (also having 0 as a valid page is probably not a good idea if you're using `NULL = 0x0`) – Colonel Thirty Two Nov 04 '15 at 20:40
-
Each process has its own mapping from virtual to physical memory, so even if 2 processes access the same virtual memory address (the value of the pointer), it can be different physical memory. This is handled by the https://en.wikipedia.org/wiki/Memory_management_unit – mch Nov 04 '15 at 20:40
-
@ColonelThirtyTwo: A null pointer constant is not the same as address `0`. – too honest for this site Nov 04 '15 at 20:45
-
@Olaf I know. But if you are using the address `0` as the null pointer constant, then it's not a good idea to have address `0` be mapped (and thus accessible). – Colonel Thirty Two Nov 04 '15 at 22:53
-
@ColonelThirtyTwo: Tell this an embedded system programmer where SRAM starts at address `0`;-). Yes, this is a nasty situation and should be avoided, if possible. But sometimes you cannot. One advantage of C is that there is no implicit check for a null pointer (allthough I sometimes wish the CPU would provide such in hardware - perhaps if they have more then 100 billion transistors per die they finally have safe instruction sets, not only security extensions which restict the users). – too honest for this site Nov 04 '15 at 23:04
-
@Olaf Well yes, if you don't have virtual memory, then you're stuck with having `0` as a valid RAM address. But OP is asking about virtual memory, so I don't think it would be too much to ask to unmap the `NULL` address so that it isn't valid :) – Colonel Thirty Two Nov 04 '15 at 23:08
-
I could always just map virtual memory starting somewhere besides 0, I simply gave that as an example. – Michael Morrow Nov 04 '15 at 23:57
-
@ColonelThirtyTwo: That's why I wrote "should be avoided, if possible". Alternatively one could use a different null pointer constant, or even use a bit in a pointer - both are valid in C. `~(uintptr_t)0` comes into mind. But that would likely require changing the compiler:-) – too honest for this site Nov 05 '15 at 00:35
-
Learn paging: http://stackoverflow.com/questions/18431261/how-does-x86-paging-work – Ciro Santilli OurBigBook.com Nov 09 '15 at 12:38
-
On most systems, the first page is not mapped BY DEFAULT. An application can then map the page if it desires. – user3344003 Nov 10 '15 at 00:51