OSDev.org

The Place to Start for Operating System Developers
It is currently Tue May 17, 2022 4:06 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Questions about memory management and paging etc
PostPosted: Sun Nov 14, 2021 9:05 am 
Offline
Member
Member
User avatar

Joined: Sun Mar 21, 2021 1:09 pm
Posts: 35
Location: current location
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?

_________________
ensis iustitae iustae illos iniustos iudicat


Top
 Profile  
 
 Post subject: Re: Questions about memory management and paging etc
PostPosted: Sun Nov 14, 2021 12:58 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1231
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!


Top
 Profile  
 
 Post subject: Re: Questions about memory management and paging etc
PostPosted: Sun Nov 14, 2021 4:13 pm 
Offline
Member
Member
User avatar

Joined: Wed Sep 28, 2005 11:00 pm
Posts: 85
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.


Top
 Profile  
 
 Post subject: Re: Questions about memory management and paging etc
PostPosted: Mon Nov 15, 2021 9:38 am 
Offline
Member
Member
User avatar

Joined: Sun Mar 21, 2021 1:09 pm
Posts: 35
Location: current location
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.

_________________
ensis iustitae iustae illos iniustos iudicat


Top
 Profile  
 
 Post subject: Re: Questions about memory management and paging etc
PostPosted: Mon Nov 15, 2021 10:21 am 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 908
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.


Top
 Profile  
 
 Post subject: Re: Questions about memory management and paging etc
PostPosted: Mon Nov 15, 2021 11:09 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 3820
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.)


Top
 Profile  
 
 Post subject: Re: Questions about memory management and paging etc
PostPosted: Mon Nov 15, 2021 1:47 pm 
Offline
Member
Member

Joined: Mon Feb 02, 2015 7:11 pm
Posts: 765
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.

_________________
https://github.com/kiznit/rainbow-os


Top
 Profile  
 
 Post subject: Re: Questions about memory management and paging etc
PostPosted: Mon Nov 15, 2021 3:13 pm 
Offline
Member
Member
User avatar

Joined: Sun Mar 21, 2021 1:09 pm
Posts: 35
Location: current location
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!

_________________
ensis iustitae iustae illos iniustos iudicat


Top
 Profile  
 
 Post subject: Re: Questions about memory management and paging etc
PostPosted: Mon Nov 15, 2021 5:29 pm 
Offline
Member
Member

Joined: Tue Feb 18, 2020 3:29 pm
Posts: 908
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.


Top
 Profile  
 
 Post subject: Re: Questions about memory management and paging etc
PostPosted: Wed Dec 01, 2021 5:08 am 
Offline
Member
Member
User avatar

Joined: Mon May 22, 2017 5:56 am
Posts: 624
Location: Flying strange spaceplanes in a distant solar system
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.

_________________
Crunchy Kaph — An experimental Forth environment for certain PPC Macs.
"May wisdom, fun, and the greater good shine forth in all your work." — Leo Brodie


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group