OSDev.org
https://forum.osdev.org/

Questions about memory management and paging etc
https://forum.osdev.org/viewtopic.php?f=15&t=55989
Page 1 of 1

Author:  acccidiccc [ Sun Nov 14, 2021 9:05 am ]
Post subject:  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?

Author:  nullplan [ Sun Nov 14, 2021 12:58 pm ]
Post subject:  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.

Author:  deadmutex [ Sun Nov 14, 2021 4:13 pm ]
Post subject:  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.

Author:  acccidiccc [ Mon Nov 15, 2021 9:38 am ]
Post subject:  Re: Questions about memory management and paging etc

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.

Author:  nexos [ Mon Nov 15, 2021 10:21 am ]
Post subject:  Re: Questions about memory management and paging etc

Quote:
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.

Author:  Octocontrabass [ Mon Nov 15, 2021 11:09 am ]
Post subject:  Re: Questions about memory management and paging etc

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.)

Author:  kzinti [ Mon Nov 15, 2021 1:47 pm ]
Post subject:  Re: Questions about memory management and paging etc

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.

Author:  acccidiccc [ Mon Nov 15, 2021 3:13 pm ]
Post subject:  Re: Questions about memory management and paging etc

nexos wrote:
Quote:
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!

Author:  nexos [ Mon Nov 15, 2021 5:29 pm ]
Post subject:  Re: Questions about memory management and paging etc

Quote:
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.

Author:  eekee [ Wed Dec 01, 2021 5:08 am ]
Post subject:  Re: Questions about memory management and paging etc

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.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/