Virtual memory and Address Translation

 

Virtual memory and Address Translation 

Address translation is the hardware-supported mechanism that enables the OS to virtualize memory efficiently while maintaining protection and control.

It extends the idea of limited direct execution (LDE) from CPU virtualization into memory virtualization.


1. The Core Problem

A program thinks:

  • Its address space starts at 0

  • It owns all memory up to some limit (e.g., 16 KB)

  • It runs alone in memory

Reality:

  • Multiple programs share physical memory.

  • The OS occupies part of memory.

  • Each process may be loaded at a different physical location.

👉 The OS must create the illusion of private memory while actually sharing physical memory.

The Crux

How can the OS:

  • Virtualize memory efficiently?

  • Provide flexibility to applications?

  • Maintain strict control over memory access?

  • Do all this without slowing the system down?


2. Hardware-Based Address Translation

The solution is:

Hardware translates every memory reference

  • The hardware performs address translation on every memory reference.

  • It converts:

    Virtual Address → Physical Address
  • This happens for every instruction fetch, load, and store.

  • Hardware provides the mechanism.

  • The OS:

    • Sets up translation structures.

    • Tracks free and used memory.

    • Ensures correct and safe mappings.


Goal

Create the illusion that:

  • Each program has its own private memory space.

Reality:

  • Many programs share physical memory.

  • The OS and hardware manage this sharing safely and efficiently.

3. Base and Bounds (Dynamic Relocation)

The simplest form of address translation is called:

  • Base and Bounds

  • Also known as Dynamic Relocation

Hardware Requirements

Each CPU contains:

  • Base register

  • Bounds (limit) register


Base Register

  • Holds the starting physical address of the process.

  • Used to relocate the process in memory.

Bounds Register

  • Holds the size of the process address space.

  • Used for protection.


4. How Translation Works

When a process generates a virtual address:

Physical Address = Virtual Address + Base Address

But first:

Check: 0Virtual Address < Bounds

If not:

  • CPU raises an exception.

  • OS likely terminates the process.






5. Example

Process Assumptions:

  • Address space size = 16 KB

  • Loaded at physical address = 32 KB

So:

Base = 32 KB Bounds = 16 KB


Code Being Executed

C code:

x = x + 3;

Compiled into assembly:

movl 0x0(%ebx), %eax ; load x addl $0x03, %eax ; add 3 movl %eax, 0x0(%ebx) ; store back
Consider loading the value of x
movl 0x0(%ebx), %eax

Suppose:

  • Virtual address of variable x = 15 KB

Translation:

Physical Address = 15 KB + 32 KB = 47 KB

The hardware performs this automatically.

The process still believes it accessed 15 KB.


6. What Happens During Execution

From process perspective:

  1. Fetch instruction at virtual 128

  2. Load value at virtual 15 KB

  3. Add 3

  4. Store back to virtual 15 KB

From hardware perspective:

  1. 128 + base → fetch physical instruction

  2. 15 KB + base → load from physical memory

  3. 15 KB + base → store to physical memory

All transparent to the process.





7. Why This Works

✔ Efficiency

  • Translation is just:

    • Addition

    • Bounds check

  • Very fast hardware operation.

✔ Protection

  • Process cannot access memory beyond bounds.

  • Prevents:

    • Accessing other processes’ memory

    • Overwriting OS memory

✔ Transparency

  • Program is compiled as if loaded at 0.

  • It never knows it was relocated.


8. OS Responsibilities

The OS must intervene at key moments:


1️⃣ Process Creation

  • Find free physical memory.

  • Set:

    • Base register

    • Bounds register

  • Track memory using a free list.


2️⃣ Process Termination

  • Reclaim memory.

  • Return memory to free list.


3️⃣ Context Switch

Each process has different base/bounds values.

When switching:

  • Save base and bounds into PCB.

  • Restore base and bounds for new process.


4️⃣ Moving a Process in Memory

Because relocation is dynamic:

  • Stop process

  • Copy memory elsewhere

  • Update base register

  • Resume process

The process never notices.


9. Privilege and Protection

  • Only OS (kernel mode) can modify base/bounds.

  • User processes cannot access these registers.

  • Attempting to do so causes exception.

Without this:

  • A process could modify base and access all memory.

  • System security would collapse.


10. Limitations: Internal Fragmentation

Problem:

If process is allocated a fixed slot (e.g., 16 KB):

  • Code and stack may use only part of it.

  • Unused space inside remains wasted.

This is called:

Internal Fragmentation

Even if physical memory exists elsewhere, fixed contiguous slots limit flexibility.


11. Big Picture

Address translation:

GoalHow It’s Achieved
Efficiency        Hardware adds base quickly
Protection        Bounds checking
Transparency        Process unaware of relocation
Control        OS manages base/bounds

12. Key Concept Summary

  • Every address a program generates is virtual.

  • Hardware translates it using:

    Physical = Virtual + Base
  • Bounds register ensures safety.

  • OS manages memory placement.

  • Provides illusion of private memory.

  • Ensures protection and isolation.

Base and bounds is:

  • Simple

  • Fast

  • Protective

But:

  • Causes internal fragmentation

  • Requires contiguous allocation

  • Limited flexibility

This leads to more advanced mechanisms like:

  • Segmentation

  • Paging

  • TLBs

  • Multi-level page tables

Comments

Popular posts from this blog

Operating Systems OS PCCST403 Semester 4 BTech KTU CS 2024 Scheme

Introduction to Operating System -Virtualization, Concurrency, and Persistence

Differences Between Linux and Classic UNIX Kernels