Electronic – What’s the need of translating the virtual address to physical address

armmemory

These are the points I read in the Memory Management Unit of ARM architecture:

  1. Virtual addresses (or logical addresses) are addresses provided by the OS to processes.
  2. One virtual address space per process
  3. Programs use virtual addresses. As a program runs, the hardware (with help
    from the operating system) converts each virtual address to a physical address.
  4. The conversion of a virtual address to a physical address is called address
    translation
  5. When the MMU is turned off, the virtual acts as output onto the physical address.

After reading the fifth point, I want to understand why do the address translation is required? Is it because the size of virtual memory is smaller than physical memory? And why/when would the MMU is turned off?

Best Answer

Virtual address translation is needed for several reasons:

  1. More ram can be addressed than there is available. For instance, the CPU in my laptop can address 256TB of memory, whereas it only has 8Gb of memory. This extra address space lets the kernel allocate far more memory than is available, and it can swap pages to disk for applications that aren't being used.

  2. Virtual address translation prevents memory fragmentation. Imagine a program that frequency allocates and deallocates large objects, the size of a memory page. If the addresses were physical, the memory space would quickly become fragmented, with no large areas of memory free. However, the kernel can remap the virtual and physical addresses so that there's always a large section of memory free, and if part of the address space is fragmented, so what? Only the pages of memory that are in use need to be backed by physical memory, and there's plenty of address space to put new allocations.

  3. Virtual addresses give better security. Remember those CPU bugs earlier this year, Spectre and Meltdown? They rely on knowing some information about the mapping of virtual addresses for different processes and the kernel. If you turn of virtual addressing, they and other attacks become a lot easier because you then know the addresses of the kernel and other processes.

As to why you would turn off the MMU, I can only guess that it's referring to the state of the MMU during startup of the processor, before the kernel has set up the page table. You wouldn't turn the MMU off during normal operation of an operating system.

Related Topic