Virtual Memory

Created

2/24/25

Virtual and physical memory are decoupled. Range are going to be different between the two.

  • e.g., 200 mb of virtual memory is much less physical
  • why do you use less?
    • programs do not need all their code and data at a given time.

In batch programming, you could load it all at once because there wouldn't be any other programs running so you didn't really have to minimize the amount of memory you used.

  • With multiprogramming, memory management became more important because different programs needed to access memory simultaneously.

What if the OS allocated literal physical memory spaces to each program. Consider the following problems:

  • What if a program needs more memory
  • What if the program writes to some other application's memory, then you corrupt the other program's memory
  • Can another program read your program's process? security risk
  • Does the program need to know what memory address it has access to?

This leads to our goals:

  • protection
    • process A cannot trash process B's memory
    • process A cannot access B's memory
  • transparency
    • a process shouldn't need to know its physical memory address range
    • supports process that need contiguous memory (for stack or large data structure)
  • resource exhaustion:
    • efficiently share the limited phsyical memory
    • machine should have "enough memory" (process greater than physical memory)

MMU: translates virtual memory to physical memory

  • it allows programs to see more memory than it exists. Possible because virtual memory may map to nothing.
  • Can check if address access is legal (enforces protection)

MMU:

  • configured through privileged instructions. Operations like telling the MMU where to look is only able to be given by OS Kernel.
  • An address space of a process is the set of virtual address that it can access. There's a subset that is actually mapped to meaningful physical memory.
  • Must keep track of what memory a process is using (so that it can allocate) but what it is no longer using (so it can deallocate)
    • It can mean it writes the idle parts to a disk or SSD so that it can be reclaimed llater.
  • challenge: complexity and speed

Go through some potential answers

Idea 1: Load-time linking: change each address that is referenced from the program to the correctly

  • an executable is static, which is why its called image. It's a picture of how we want the data to look in memory.
  • The address adjustment step would be proportional to how many addresses there are
  • Problems:
    • time consuming to patch the address every run
    • what if its already in loaded in memory, how do you move?
    • what if no contiguous free region fits the program.

Ideal 2: base + bound register

  • base and boud
  • on each store / load / jump: physical address = virtual address + base
  • check 0 <= virtual bound < bound, else trap to kernel.
  • advantages
    • cheap for hardware bc only need two registers
    • cheap for cycles because you can add and compare in parallel
  • tradeoffs
    • growing is expensive or impossibe
    • can't share code

Idea 3: segmentation:

  • processes have many base/bound registers. The OS allocates.
    • address space built from many segments
    • can share/protect memory granularity
    • stored in segment table. Each virtual address indicates segment and offsite
  • must specify the segment as part of the virtual address
    • it's nice to have one virtual address space.