Is the MMU (Memory Management Unit) chip necessary for a processor to have virtual memory support?

7,504

Solution 1

Any system emulator which emulates a system containing a MMU effectively emulates a MMU in software, so yes, it’s possible to emulate a MMU. However, virtual memory requires some way of enforcing memory access control, or at least address translation, so it needs either full software emulation of the CPU running the software being controlled, or it needs hardware assistance.

So you could conceivably build a system with no MMU, port QEMU to it, add the missing pieces to make virtual memory actually useful (e.g., add support for swap on the host system), and run a MMU-requiring operating system in QEMU, with all the protection you’d expect in the guest operating system (barring QEMU bugs).

One real, and old, example of an MMU-less “emulation” used to provide virtual memory is the Z-machine, which was capable of paging and swapping its code and data, on 8-bit systems in the late seventies and early eighties. This worked by emulating a virtual processor on the underlying real processor; that way, the interpreter keeps full control over the memory layout which the running program “sees”.

In practice, it’s generally considered that a MMU is required for virtual memory support, at least at the operating system level. As indicated MMU-less kernel?, it is possible to build the Linux kernel so that it can run on systems without a MMU, but the resulting configuration is very unusual and only appropriate for very specific use-cases (with no hostile software in particular). It might not support many scenarios requiring virtual memory (swapping, mmap...).

Solution 2

It depends on exactly what you call virtual memory. An interesting model is the old Win16 model (best known from the old Windows 3.x, not Windows NT). In that model, you had GlobalLock and GlobalUnlock, LocalLock and LocalUnlock functions. These were a form of cooperative, manual management of virtual memory. As this was done in (application) software, it didn't require an MMU. And memory was virtual in the sense that unlocked memory could be swapped to disk.

However, in the Win16 model there is no protection between different processes. If another process left data in memory, you could overwrite it. This is not a fundamental restriction. With fast SSD's these days, you could remove a non-running process from memory entirely, and do so in a reasonable time.

Solution 3

It's not necessary to have a hardware MMU, if you have software that can swap processes to and from the physical memory.

This was the mode of operation of early multi-tasking operating systems. Only one process is resident in memory at any given time, it is swapped out in its entirety when its time-slice expires (you can see that this becomes problematic with large processes). The memory contents seen by the currently-running process is not the same as that seen by any other process, and each has its own view of the address space.

Some hardware support is helpful - a notion of a "protected" memory area for the OS's own use (e.g. all addresses with MSB set are accessible only in supervisor mode) and a "break" value indicating the highest address in use, but memory management hardware is not a absolute requirement for virtual memory; it's just a particularly effective way to achieve it.

Share:
7,504

Related videos on Youtube

yoyo_fun
Author by

yoyo_fun

Updated on September 18, 2022

Comments

  • yoyo_fun
    yoyo_fun almost 2 years

    Is the MMU (Memory Management Unit) chip necessary for a processor to have virtual memory support?

    Is it possible to emulate MMU functionality in software? (I am aware that it will probably have a big impact on performance).

    • Vality
      Vality over 6 years
      Any fully capable computer can emulate any other computer with enough of a performance hit. Or emulate any hardware. The only question is the magnitude of performance hit.
    • Admin
      Admin over 4 years
      every processor today needs a TLB, so it has a built-in MM-unit.
    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' over 2 years
      Does this answer your question? How the original unix kernel adressed memory?
  • yoyo_fun
    yoyo_fun over 6 years
    so virtual machine applications also have an MMU emulator component?
  • Stephen Kitt
    Stephen Kitt over 6 years
    Yes — not necessarily as a separate component, but they do have the necessary support in the emulation.
  • Jörg W Mittag
    Jörg W Mittag over 6 years
    @JenniferAnderson: Some modern CPUs have features that let the emulator (partially) offload the MMU emulation to the MMU itself. E.g. a program running inside an emulator will itself use multiple emulated pages of memory, these pages of memory are of course "nested" in the pages of memory used by the emulator. Both newer high-end Intel and AMD CPUs have support for nested page tables, which allow the emulator to express this nesting within the MMU itself, instead of having to (expensively) emulate it.
  • Stephen Kitt
    Stephen Kitt over 6 years
    @Jörg indeed, thanks for the clarification. Most hypervisors still include some level of software emulation though so that they will work without the extra hardware support. I was blinkered by the “Is it possible” aspect of the question ;-).
  • yoyo_fun
    yoyo_fun over 6 years
    @JörgWMittag Wow this is really interesting. So basically this means that on modern processors there is some level of Hardware support for some functionality of Virtual Machine Software like DOSBox, VirtualBox, or VMWare. I gues these are some of the software that would take advanrage of nested tables inside the MMU chip. Am I right?
  • Stephen Kitt
    Stephen Kitt over 6 years
    @Jennifer yes, at least VirtualBox and VMware’s tools will take advantage of nested page tables. All hypervisors (Xen, KVM etc.) do too.
  • yoyo_fun
    yoyo_fun over 6 years
    @StephenKitt But Xen and KVM are different kinds of Software than VirtualBox and VMware? Aren't the latter HyperVisors too?
  • Jörg W Mittag
    Jörg W Mittag over 6 years
    @JenniferAnderson: Yes, this functionality was specifically introduced for para-virtualization. (Note that it is nothing new, hardware-assisted para-virtualization has existed in the mainframe world since the early 1960s.) It turns out however, that it can also be used for other interesting applications, such as speeding up Garbage Collection (see the C4 collector in Azul's Zing JVM for an example). However, note, that all of this works in both directions: in the same way that extending MMUs with virtualization support is nothing more than a performance optimization, and virtualization …
  • Jörg W Mittag
    Jörg W Mittag over 6 years
    … works just fine without it (albeit slower), the MMU itself is also just a performance optimization, and you can implement virtual memory without it. E.g. on the original AS/400 virtual memory was emulated for C programs, because the original AS/400 doesn't have virtual memory. (It uses a memory-safe intermediate language for address space separation instead).
  • Jörg W Mittag
    Jörg W Mittag over 6 years
    @JenniferAnderson: VirtualBox and VMWare started out as full-system virtualizers and had para-virtualization features added to them later. Xen and KVM started out as para-virtualiziers and had full-system virtualization features added to them later. Nowadays, the distinction is pretty arbitrary. The main difference between VMWare and VirtualBox on the one side and Xen on the other side, is that the former run as a user application on top of a general-purpose OS, whereas Xen runs on the bare metal instead of a general-purpose OS. Of course, even that distinction is muddied, since Xen relies …
  • Stephen Kitt
    Stephen Kitt over 6 years
    @Jennifer VirtualBox isn’t a hypervisor; VMware’s ESXi is, but Workstation and Fusion aren’t.
  • Jörg W Mittag
    Jörg W Mittag over 6 years
    … on a (modified) general-purpose OS running in Dom0 for hardware device access and KVM is a part of a general-purpose OS.
  • Stephen Kitt
    Stephen Kitt over 6 years
    That’s not really virtual memory though, it’s just process swapping... (We’d really need to define “virtual memory” for a proper answer to this question!)
  • Toby Speight
    Toby Speight over 6 years
    Each process has its own view of the address space - I'll edit to clarify the definition I'm using.
  • Stephen Kitt
    Stephen Kitt over 6 years
    Right, but it’s the same one-to-one mapping for all processes. (From the point of view of processes there’s not much difference so there’s no real argument there...)
  • phuclv
    phuclv over 6 years
    one example: this guy implements an ARM emulator with MMU in an 8-bit microcontroller
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 2 years
    This answer doesn't make sense. An MMU isn't an afterthought to do VM: it's a way to do VM. MMU tables encode the same information as descriptors in pointers and they're a convenient way to implement descriptors.