0
$\begingroup$

From my understanding, physical memory (main memory -- DRAM) is addressed differently than disk. This is all a bit of an abstraction to me, and I am hoping to make my understanding more concrete.

  • Page tables store physical addresses in RAM. They may be marked invalid in the hardware to generate a trap to the OS (page fault) which then recognizes that the page actually resides in swap space or is an anonymous page that is unallocated (heap)

When you address physical memory, how does that work? Is it just like virtual addresses where it's a large linear address space of the size of your RAM? Do you just use simple C pointers but your CPU is in real (and not virtual) mode so its interpreted correctly? Any concrete examples would be appreciated, specifically how is it done in a way so that the hardware can interpret it correctly when performing transformations? From my understanding, it is simply a simple concatenation of bits "Physical Page Number" and the Offset, which forms a Physical Address.

$\endgroup$
2
  • $\begingroup$ Related: en.wikipedia.org/wiki/Memory_management_unit $\endgroup$
    – D.W.
    Jun 12 at 6:05
  • $\begingroup$ There is a page table that tells, for every virtual page your process uses, where it resides in RAM or on disk. Virtual page address -> effective page address. $\endgroup$
    – user16034
    Jun 12 at 6:24

1 Answer 1

2
$\begingroup$

The whole point of using the abstraction of virtual addresses (which is to say, giving every process the illusion that it has all the memory reserved for itself) is to free the application software from the concerns of managing the physical memory and isolate processes from each other.

The specifics involve a number of engineering tradeoffs, but this is facilitated through some kind of mapping that is maintained by the system software. This mapping is updated as processes start executing and terminate, and as the application code executes. For instance, malloc establishes a page file as a backing store. Something like mmap allows processes to explicitly map file bytes (loaded one page at a time). The applications use memory addresses in the virtual address space, which are translated by the operating system into physical addresses.

Virtual address space and physical address space (Source: Wikimedia Commons)

This diagram illustrates the address translation process that the system software would execute. The mapping of a virtual address to a physical address is first checked in the TLB (translation lookaside buffer), which is an associative cache of recently-used mappings. TLB misses, in turn, look for the mapping in the page table. If a mapping is found, it is stored in the TLB. A page table miss - either because there is no translation available, or because the existing one is invalid (a segmentation fault), or simply because the page is not in physical memory and has been swapped out to make room for other pages - triggers a page fault. In the latter case (swapping out), the page is read from the swap file/swap partition and written to both the page table and the TLB (a write through).

Address translation process (Source: Wikimedia Commons)

When the physical memory is full, the page replacement policy kicks in and decides which page to evict to make room for the incoming page. Most of the page replacement algorithms that you're likely to encounter (unless you go super deep into this stuff) are approximations of LRU (least recently used), which, as the name suggests, evicts the least recently used page.

The theoretical optimal replacement policy would be to evict the page that will be accessed farthest in the future, which, evidently from the phrasing, requires (for lack of better wording) a bit of auguring. As it happens, though, we're in luck - LRU happens to be a good approximation - asymptotically close, in fact, but I digress.

What do physical addresses look like? They're not really different from logical addresses.

Address translation (Source: VMware)

Whether or not you have a valid translation only changes whether the physical page number is read from the TLB, the page table, or the swap partition/file. The addresses themselves are structured similarly - a (virtual page number + offset) maps to a (physical page number + offset), where the offset isn't translated but simply read from the virtual address.

$\endgroup$
5
  • 1
    $\begingroup$ My question is mostly about that last bit. Just about physical addresses themselves. Are they contiguous in memory? Do you simply load and store to them in real-mode? How do you obtain them in the first place, and could there be conflicts with devices that use memory mapped I/O or is that all handled at the BIOS level? Basically, how is it managed, and who is doing the managing? $\endgroup$ Jun 15 at 0:45
  • $\begingroup$ (1) Logical addresses & physical addresses are contiguous, but the mapping is not (=> contiguous logical addresses may not map to contiguous physical addresses). (2) Physical addresses are the same in real and protected mode. In real mode, they work like (segAddr << 4) + offset (segAddr * 16 + offset using a shift + add); in protected mode, it works like translate(segAddr) + offset. (3) Memory-mapped I/O uses the same address space for both the main memory and I/O devices, allowing CPU instructions (mov etc.) to directly access devices. $\endgroup$ Jun 15 at 21:05
  • 1
    $\begingroup$ What is logical v. Physical and the mapping you mention? Also, w.r.t memory mapped i/o, my question was if you could overlap your physical address space for memory and memory mapped i/o, rendering the underlying memory inaccessible? Who is responsible for setting this stuff up? $\endgroup$ Jun 15 at 21:33
  • $\begingroup$ Logical/virtual address: Generated by the CPU during execution, physical address: the actual location in the memory unit. mmap is a POSIX system call (I don't know a Windows equivalent), so you're asking the operating system to service this request. Assuming you have the requisite permissions, mmap creates a memory region that has the exact structure of the file, as if the contents were copied into memory. mmap returns a stack pointer to this region, which can be used to work on the file. (This might be helpful.) $\endgroup$ Jun 15 at 22:22
  • $\begingroup$ Oh, you use logical as virtual. I understand. I was just curious about the raw underlying physical addresses and how those are managed. I wasn’t asking about mmap, I was asking about Memory Mapped Device I/O. $\endgroup$ Jun 15 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.