Questions about memory management and paging etc
- acccidiccc
- Member
- Posts: 38
- Joined: Sun Mar 21, 2021 1:09 pm
- Location: current location
Questions about memory management and paging etc
How does paging work? So as far as I know, paging is used to map portions of physical memory somewhere. So I can map, let's say, the physical address to 0x3000 to in virtual memory 0xffffffff8200000. What does virtual memory do what physical memory cannot. How can I use paging for starting processes? Is there some resource which can teach me these concepts?
iustitiae iniustos iudicat
Re: Questions about memory management and paging etc
Paging maps virtual to physical addresses, you are right about that. There are many uses for that. For one, you can map fixed virtual addresses to dynamic physical addresses. That means your program is going to see the same addresses no matter where it is loaded. So for example your kernel can be loaded wherever the bootloader wants, and as long as the bootloader maps it correctly, there is little reason for your kernel to know. The BL does not even need to load the whole kernel contiguously, just make it appear contiguous in virtual memory.
This also means you can link the kernel to addresses where there may not be any physical RAM, or may even be something other than RAM on the physical bus.
Another important use for paging is related but slightly different: You can change the mapping between processes. This means different processes can use the same address and mean different places in memory by those addresses. And they don't need to know of each other or coordinate this in any way. Finally, and this is key, you can use paging to prevent processes from seeing or modifying memory of the kernel or other processes. In DOS, one badly written application can bring down the whole system, while in more modern OSes, this is generally not possible. The offending application would crash and the rest of the system remains usable.
None of these things are possible with just physical memory. There is segmentation (in 32-bit mode) which would allow some of that, but generally has been disfavoured, because it makes address space more complicated, and no big OS ever used it. And in 64-bit mode that feature was turned off, anyway.
This also means you can link the kernel to addresses where there may not be any physical RAM, or may even be something other than RAM on the physical bus.
Another important use for paging is related but slightly different: You can change the mapping between processes. This means different processes can use the same address and mean different places in memory by those addresses. And they don't need to know of each other or coordinate this in any way. Finally, and this is key, you can use paging to prevent processes from seeing or modifying memory of the kernel or other processes. In DOS, one badly written application can bring down the whole system, while in more modern OSes, this is generally not possible. The offending application would crash and the rest of the system remains usable.
None of these things are possible with just physical memory. There is segmentation (in 32-bit mode) which would allow some of that, but generally has been disfavoured, because it makes address space more complicated, and no big OS ever used it. And in 64-bit mode that feature was turned off, anyway.
Carpe diem!
Re: Questions about memory management and paging etc
Paging is one method of implementing memory protection and isolation between processes. Each process has the illusion that it lives alone in a huge address space and, thus, cannot interfere with the memory of other processes unless the kernel allows them to share memory. If a process were to perform an invalid memory access, then a page fault would occur. The resolution of the page fault allows the kernel to do things like copy-on-write, demand paging, guard pages, and page swapping.
- acccidiccc
- Member
- Posts: 38
- Joined: Sun Mar 21, 2021 1:09 pm
- Location: current location
Re: Questions about memory management and paging etc
Thanks for the response!
So if program A was at the physical address 0x1234 and B at 0x3456, they could both be mapped to virtual 0x10000 (how does this behave with multicore cpus? Can they have differing virtual memory?) during the taskswitch. Now if physical 0xff80 is mapped at virtual 0x2000 in both directories (each process has it's own page directory in this example), it can be used as shared memory.
But if the pagetable of program C does not contain the mapping of the shared memory, C can not see 0x2000 and accessing it would result in a page fault. Is this correct?
Ok, this makes sense as to why I would use paging. Thank you.
So that is useful for things like the LAPIC, where e.g. the pysical address could be 0x123465, but you would map it to 0xFEC0000 (or some other place).nullplan wrote:For one, you can map fixed virtual addresses to dynamic physical addresses.
So that is useful for the higher-half kernel.nullpan wrote:So for example your kernel can be loaded wherever the bootloader wants, and as long as the bootloader maps it correctly, there is little reason for your kernel to know. The BL does not even need to load the whole kernel contiguously, just make it appear contiguous in virtual memory.
nullplan wrote: Another important use for paging is related but slightly different: You can change the mapping between processes. This means different processes can use the same address and mean different places in memory by those addresses. And they don't need to know of each other or coordinate this in any way. Finally, and this is key, you can use paging to prevent processes from seeing or modifying memory of the kernel or other processes. In DOS, one badly written application can bring down the whole system, while in more modern OSes, this is generally not possible. The offending application would crash and the rest of the system remains usable.
So if program A was at the physical address 0x1234 and B at 0x3456, they could both be mapped to virtual 0x10000 (how does this behave with multicore cpus? Can they have differing virtual memory?) during the taskswitch. Now if physical 0xff80 is mapped at virtual 0x2000 in both directories (each process has it's own page directory in this example), it can be used as shared memory.
But if the pagetable of program C does not contain the mapping of the shared memory, C can not see 0x2000 and accessing it would result in a page fault. Is this correct?
Ok, this makes sense as to why I would use paging. Thank you.
iustitiae iniustos iudicat
Re: Questions about memory management and paging etc
The LAPIC is actually at 0xFEC00000 physical. With paging, you could map it to 0x12345678 virtual.So that is useful for things like the LAPIC, where e.g. the pysical address could be 0x123465, but you would map it to 0xFEC0000 (or some other place)
-
- Member
- Posts: 5531
- Joined: Mon Mar 25, 2013 7:01 pm
Re: Questions about memory management and paging etc
Each hardware thread can have its own virtual memory mapping. As far as I know, the only things shared between cores or hardware threads are a handful of MSRs.acccidiccc wrote:(how does this behave with multicore cpus? Can they have differing virtual memory?)
Yes. (Actually, program C could have something different mapped at virtual address 0x2000, so it might access different physical memory instead of causing a page fault.)acccidiccc wrote:...Is this correct?
Re: Questions about memory management and paging etc
No you can't. You could map it to 0x12345000, but not 0x12345678.nexos wrote:The LAPIC is actually at 0xFEC00000 physical. With paging, you could map it to 0x12345678 virtual.
The mapping granularity is one page, which is 4K (or 0x1000) on x86.
- acccidiccc
- Member
- Posts: 38
- Joined: Sun Mar 21, 2021 1:09 pm
- Location: current location
Re: Questions about memory management and paging etc
The wiki said that the default mapping is thatnexos wrote:The LAPIC is actually at 0xFEC00000 physical. With paging, you could map it to 0x12345678 virtual.So that is useful for things like the LAPIC, where e.g. the pysical address could be 0x123465, but you would map it to 0xFEC0000 (or some other place)
This is a very interesting topic and this thread has cleared up a lot for me. Thanks, y'all!
iustitiae iniustos iudicat
Re: Questions about memory management and paging etc
What the wiki means is that the CPU maps the LAPIC hardware registers to that physical address. The OS has complete control of paging, the CPU doesn't enforce any mappings.The wiki said that the default mapping is that
Re: Questions about memory management and paging etc
For completeness:
Unix has a fork() system call which, in modern implementations, uses paging in started processes. It makes a new "child process" with the same page mapping as the parent, but all the pages are marked copy-on-write (COW). When (or if) the child writes to memory, the pages it writes to are copied. This seems elegant, but there are some arguments against it. The arguments may be a subject for another thread, but one clear problem is that it makes it hard to port programs which rely on fork() to systems without paging. I've also seen (but not understood) arguments that fork() doesn't suit modern non-Unix systems.acccidiccc wrote:How can I use paging for starting processes?
Kaph — a modular OS intended to be easy and fun to administer and code for.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie