The Enterprise

The Virtual Memory: What and Why

Michael Pankov •

Today we're going to dive a bit deeper than usual. We'll talk about virtual memory — a facility, now omnipresent in most devices considered to be "computers" (1).

With virtual memory, programs operate virtual addresses — rather than physical ones. The virtual addresses are mapped arbitrarily to physical ones on a per-process basis. So, same virtual address in different processes will refer to different areas in memory.
Physical memory operation

The mappings are stored in the form of virtual memory area descriptions in the kernel and page tables in the memory management unit (MMU). Both of these can be thought of as just dictionaries with virtual addresses as keys and physical ones as values. Real implementation is quite involved, though, and differs significantly in software and hardware worlds.


Virtual memory scheme
Let's now define why do we need it. Here are some advantages of virtual addressing comparing to physical addressing:

  1. Simplification of programming.
    User programs become much simpler, because they can assume that they own entire address space. There's no need to manage potential overlaps of memory areas of different programs. This work is done by kernel.
  2. Increase of security and stability.
    With virtual memory, processes can't arbitrarily access each other's memory. That is, they can't corrupt somebody else's data, either intentionally or not. In case some program crashes, it's the only one that goes down. Processes also can't read passwords from memory.
    Oh, and I almost forgot: OS is also in that same address space, so you can't corrupt, crash, or modify it for your inner hacker's delight.
  3. Ability to implement features like swapping, copy-on-write, etc.
    We'll talk about this in later post.
I personally think the main reason back in the days (around 1960s (2)) was the number 1. The second one came just as a consequence, because nobody ever cares about security out of good will (and they probably shouldn't). And the third one is a nice added bonus.

Virtual memory operation
Okay, so far so good, but what are the downsides, so to say, of the virtual memory addressing? Here they are:

  1. Hardware support is required.
    Before CPUs could maintain tables of pages, it was impossible to implement virtual memory.
    Without MMU, we have no way to interrupt intruders in other's address spaces — they all have equivalent physical addresses, which are the same for all processes.
    With MMU, however, we get hardware interrupt in case someone tries to do something not permitted by its virtual mapping. We setup translation tables and switch them for every process — and same virtual address in two different programs may refer to two distinct physical locations. (3)
  2. Determinism is lost.
    As the virtual address space looks all the same across its' entirety, there's no more a guarantee that memory access will always succeed, within a given time frame.
    There are several reasons for that. The main one is that virtual memory address can actually refer not to memory, that is, RAM of the computer, but to hard disk, flash drive, or even a network location. One of the most known examples of virtual memory addresses referring to out-of-memory places is swapping.
    As there can be more virtual memory than physical one, it's possible that a program that consumes no memory in its' address space will run out of resources because some other program consumed physical memory in their address space.
These are the main ones. They both keep virtual memory out of really-tightly-embedded systems, and second is a particularly important disadvantage for real-time systems.

Next post in the series will talk more about implementation details and, probably, about advanced features one can easily implement with virtual memory. Stay tuned!

(1) There are whole classes of exceptions, of course: microcontrollers, digital signal processors, maybe something else. In particular, some configurations of ARM processors come without memory management unit.
(2) Yes, virtual memory was developed in 1960s. The concept itself was introduced by German physicist Fritz-Rudolf Güntsch in 1956. First commercial implementation came out in 1961, but it used segmentation rather than paging. All this stuff wasn't popular on personal computers until Intel 80286 (1982).
(3) In case you're OS guru, please accept my apologies before going to bash me in comments. This is intended oversimplification of the matter for educational purposes.
comments powered by Disqus