Questions about memory management and paging etc

Discussions on more advanced topics such as monolithic vs micro-kernels, transactional memory models, and paging vs segmentation should go here. Use this forum to expand and improve the wiki!
Post Reply
User avatar
acccidiccc
Member
Member
Posts: 38
Joined: Sun Mar 21, 2021 1:09 pm
Location: current location

Questions about memory management and paging etc

Post by acccidiccc »

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
nullplan
Member
Member
Posts: 1769
Joined: Wed Aug 30, 2017 8:24 am

Re: Questions about memory management and paging etc

Post by nullplan »

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.
Carpe diem!
User avatar
deadmutex
Member
Member
Posts: 85
Joined: Wed Sep 28, 2005 11:00 pm

Re: Questions about memory management and paging etc

Post by deadmutex »

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.
User avatar
acccidiccc
Member
Member
Posts: 38
Joined: Sun Mar 21, 2021 1:09 pm
Location: current location

Re: Questions about memory management and paging etc

Post by acccidiccc »

Thanks for the response!
nullplan wrote:For one, you can map fixed virtual addresses to dynamic physical addresses.
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).
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.
So that is useful for the higher-half kernel.
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
nexos
Member
Member
Posts: 1078
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: Questions about memory management and paging etc

Post by nexos »

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)
The LAPIC is actually at 0xFEC00000 physical. With paging, you could map it to 0x12345678 virtual.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
Octocontrabass
Member
Member
Posts: 5513
Joined: Mon Mar 25, 2013 7:01 pm

Re: Questions about memory management and paging etc

Post by Octocontrabass »

acccidiccc wrote:(how does this behave with multicore cpus? Can they have differing virtual memory?)
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:...Is this correct?
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.)
kzinti
Member
Member
Posts: 898
Joined: Mon Feb 02, 2015 7:11 pm

Re: Questions about memory management and paging etc

Post by kzinti »

nexos wrote:The LAPIC is actually at 0xFEC00000 physical. With paging, you could map it to 0x12345678 virtual.
No you can't. You could map it to 0x12345000, but not 0x12345678. :P

The mapping granularity is one page, which is 4K (or 0x1000) on x86.
User avatar
acccidiccc
Member
Member
Posts: 38
Joined: Sun Mar 21, 2021 1:09 pm
Location: current location

Re: Questions about memory management and paging etc

Post by acccidiccc »

nexos wrote:
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)
The LAPIC is actually at 0xFEC00000 physical. With paging, you could map it to 0x12345678 virtual.
The wiki said that the default mapping is that :D
This is a very interesting topic and this thread has cleared up a lot for me. Thanks, y'all!
iustitiae iniustos iudicat
nexos
Member
Member
Posts: 1078
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: Questions about memory management and paging etc

Post by nexos »

The wiki said that the default mapping is that
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.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
User avatar
eekee
Member
Member
Posts: 872
Joined: Mon May 22, 2017 5:56 am
Location: Kerbin
Discord: eekee
Contact:

Re: Questions about memory management and paging etc

Post by eekee »

For completeness:
acccidiccc wrote:How can I use paging for starting processes?
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.
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
Post Reply