CS274: Computer Architecture - Virtual Memory
Activity Goals
The goals of this activity are:- To explain why memory is segmented into a stack and heap
- To reason about the benefits of growing the stack and heap in opposite directions
- To motivate the need for virtual memory, and specifically, for logical addressing
- To design a logical address space that facilitates cooperative multitasking
The Activity
Directions
Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity, and compare with your group to prepare for our whole-class discussion. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.Model 1: Warmup - MIPS Addresses
Questions
- Which region contains the translated machine code for your program?
- Which address above is the starting address for a MIPS program?
Model 2: Key Formulas and Concepts Recap
A quick-reference recap of the key ideas from this activity (and a preview of where the virtual memory series is headed). Try to reproduce each one from memory before peeking!
Key Rules and Formulas
- Memory segments: a program's address space contains its machine code (the text segment, where a MIPS program starts at 0x00400000), global/static data, the heap (dynamic allocations), and the stack (function frames).
- Stack and heap grow toward each other: the stack starts at the top of the address space and grows downward; the heap sits above the data segment and grows upward. This lets each use as much of the free middle region as it needs, without deciding a fixed boundary in advance.
- Logical (virtual) addressing: every program is compiled as if it owns the whole address space; the hardware translates each virtual address to a physical one, so multiple programs can coexist safely without knowing where they really live in RAM.
- Page number / offset split: a virtual address divides into a virtual page number and a page offset, with offset bits =
log2(page size). Micro-example with 4 KB pages: (1)log2(4096) = 12offset bits; (2) for the 32-bit address0x00003ABC, the offset is the low 12 bits,0xABC, and the virtual page number is0x00003ABC >> 12 = 0x3. - Translation: the page table maps virtual page number to physical frame number; physical address =
frame number * page size + offset. Micro-example: if virtual page 3 maps to frame 7, then (1)7 * 4096 = 28672 = 0x7000; (2)0x7000 + 0xABC = 0x7ABC. The offset is never translated - only the page number changes. - TLB: the Translation Lookaside Buffer is a small cache of recent page-number-to-frame translations, so most translations do not require an extra memory access to walk the page table.
High addresses +==================+ <-- stack starts here,
| stack | grows DOWN
| | |
| v |
| (free space) |
| ^ |
| | |
| heap | grows UP
+==================+
| static data |
+==================+
| text (code) | <-- program starts here
Low addresses +==================+
Virtual address (4 KB pages):
| virtual page number (bits 31..12) | offset (bits 11..0) |
translated copied as-is
Glossary
| Term | Meaning |
|---|---|
| Text segment | The region holding the program's machine code |
| Stack | Function-call frames (locals, return addresses); grows downward from high addresses |
| Heap | Dynamically allocated memory; grows upward toward the stack |
| Virtual (logical) address | The address a program uses, translated by hardware before reaching RAM |
| Physical address | The actual location in RAM after translation |
| Page / Frame | A fixed-size chunk of virtual memory / the physical slot that holds one page |
| Page offset | The low log2(page size) address bits, identical in virtual and physical addresses |
| Page table | The per-process map from virtual page numbers to physical frame numbers |
| TLB | A small cache of recent translations that makes virtual memory fast |
| Multitasking | Running several programs at once, each with its own address space |
Questions
- Without looking, split the virtual address 0x00005F10 into its page number and offset assuming 4 KB pages, and explain which half the TLB helps translate.