Download presentation
Presentation is loading. Please wait.
1
Review: memory and address spaces
Landon Cox January 20, 2017
2
OS Operating systems Hardware Applications System Calls Traps Threads
Virtual Memory System Calls OS Interrupts Atomic Test/Set TLB, Page Tables Traps Hardware
3
Remember what a process is
Thread Stream of execution (unit of concurrency) Address space Memory that threads use (unit of data)
4
Address space abstraction
All memory data process uses to run Program code, stack, data segment, heap Hardware reality All processes share single small memory OS illusion Each process has its own large memory
5
Illusions of the address space
Address independence Can use same numeric address in 2 processes Each process has its own version of “address 4” Each “address 4” refers to different data items Protection One process cannot touch another’s data Virtual memory Address space can be larger than physical memory
6
Uni-programming 1 process occupies memory at a time
Always load process into same spot Must reserve space for the OS code (kernel) fffff (high memory) . . OS kernel 80000 7ffff . User process 1 00000 (low memory)
7
Uni-programming Virtual address = Physical address =
Address programmer thinks she’s accessing Physical address = Address from the view of the machine (actual physical memory location) V-to-P mapping in uni-progamming Identity map (virtual A physical A)
8
Uni-programming Running another process
Swap user process 1 to disk Bring user process 2 to memory Kernel scheduler swaps address spaces (when doing a context switch) This is how early PCs worked (badly)
9
Uni-programming What features does this provide?
Address independence Protection No virtual memory Note sum of address spaces > phys mem.
10
Uni-programming Problems with uni-programming
Swapping to/from disk is slow Moves more data than might be needed What does this imply about scheduling slice? High overhead for swapping large slice Large slice interactivity suffers
11
Multi-programming Want more than 1 process in phys memory
Processes can have same virtual addrs Cannot share physical addrs Addresses must be translated Programs cannot manipulate phys addrs anymore Statically: occurs before execution Dynamically: occurs during execution Protection becomes harder
12
Static address translation
Want two processes in memory With address independence Without translating on-the-fly Using static translation How to do this?
13
Static address translation
Adjust loads/stores when put in memory This is called a linker-loader Programming Assume memory starts at 0 Linker-loader Adds 0x20000 to offsets for P2 Similar to linking phase of compile fffff (high memory) . OS kernel 80000 . 3ffff . User process 2 20000 1ffff . User process 1 00000 (low memory) load $1, $base + offset
14
Linking your programs Compiler generates .o files + libraries
.o files all assume they start at address 0 Linker puts everything together Resolves cross-module references (e.g., “how do I jump to method foo()?”) Spits out single executable file
15
Multi-programming Address independence? Protection? Yes.
We both get our own address 4 Protection? No. Why not?
16
Multi-programming protection
Static translation changes the offset Process could still issue odd register value Problem Buggy/malicious code can corrupt other processes User program (not system) gets last move Do we really need dynamic addresses? Yes, to make pointers (and arrays) work load $1, $base + offset
17
Multi-programming Upshot: we need dynamic translation
Address independence? Yes. (my address 4 is different from yours) Protection? No (processes can corrupt each other) Virtual memory (address space > phys mem?) No. Upshot: we need dynamic translation
18
Dynamic address translation
User process Translator (MMU) Physical memory Virtual address Physical address Will this provide protection? Sure, as long as the translation is correct
19
Dynamic address translation
User process Translator (MMU) Physical memory Virtual address Physical address This is an example of a systems service called “naming”. Can you think of other examples that you use everyday? Internet DNS (Domain Name Service) File System (human-readable names to blocks on disk)
20
Dynamic address translation
Does this enable virtual memory? Yes VA only needs to be in phys mem when accessed Provides flexibility Translations can change on-the-fly Different VAs can occupy different PAs Common technique Add level of indirection to gain flexibility
21
Hardware support Traditional view Basic requirement
Dynamic translation needs hardware support Basic requirement Must translate each load/store Can also do this via software simulation JVM does a lot of the work of the MMU Ask Ali!
22
Dynamic address translation
Translator: just a data structure Tradeoffs Flexibility (sharing, growth, virtual memory) Size of translation data Speed of translation User process Translator (MMU) Physical memory Virtual address Physical address
23
1. Base and bounds For each process
Single contiguous region of phys mem Not allowed to access outside of region Illusion own physical mem: [0, bound)
24
1. Base and bounds Looks a lot like the linker-loader.
What is different? No rewriting. Every access through translator. (system gets the last move) Size of phys mem Base + Bound Bound Base Virtual memory Physical memory
25
1. Base and bounds Only kernel can change base, bound
if (virtual address >= bound) { trap to kernel kernel can kill process with segmentation fault } else { physical address = virtual address + base }
26
1. Base and bounds What happens on a context switch?
Must translate addresses differently Load translation data for new process Set base and bounds registers
27
1. Base and bounds How can address spaces grow?
If memory above base+bound is free Increase the bound If memory above base+bound isn’t free Stop program Copy data Change base and bounds Restart process Does being moved affect the program? No, it still only knows about virtual addresses Base + Bound Base
28
Base and bounds pros and cons
Low hardware costs (2 registers, adder, comparator) Fast (only inserting addition and comparison) Cons Single address space can’t easily exceed size of phys mem Growing virtual address space might be slow Sharing is difficult (must share all or nothing)
29
Base and bounds sharing
Why can’t Chrome 1 and Chrome 2 share the same code segment? Size of phys mem Base + Bound Code Data Bound Bound Code Data Code Data Base Virtual memory (Chrome 1) Physical memory Virtual memory (Chrome 2)
30
Base and bounds pros and cons
Low hardware costs (2 registers, adder, comparator) Fast (only inserting addition and comparison) Cons Single address space can’t easily exceed size of phys mem Growing virtual address space might be slow Sharing is difficult (must share all or nothing) Wasted memory due to external fragmentation
31
Base and bounds fragmentation
1MB P4 P6 300KB 600KB P3 400KB P5 300KB External fragmentation Heuristics to minimize Best fit (Allocate in smallest free region) First fit (Use first region that fits) P2 100KB P1 Physical memory
32
2. Segmentation Segment Idea Region of contiguous memory
(both virtually and physically) Idea Generalize base and bounds Create a table of base and bound pairs
33
2. Segmentation Virtual address has two parts
Segment # (could be in high-order bits) Offset (e.g., low-order bits of address) Segment # Base Bound Segment 0 4000 700 Segment 1 500 Segment 2 Unused Segment 3 2000 1000 Code Data segment Stack segment Virtual info Physical info Same for both
34
Virtual addresses VA={b31,b30,…,b12,b11,…,b1,b0} High-order bits
(segment number) Low-order bits (offset)
35
2. Segmentation Offset Segment # Segment # Base Bound Segment 0 4000
700 Segment 1 500 Segment 2 Unused Segment 3 2000 1000 Code Data segment Stack segment Virtual memory (3,fff) . (Stack) (3,000) … (1,4ff) . (Data) (1,000) (0,6ff) (Code) (0,0) Physical memory 46ff . (Code segment) 4000 … 2fff . (Stack segment) 2000 4ff . (Data segment) Offset Segment #
36
2. Segmentation Segment # Base Bound Segment 0 4000 700 Segment 1 500
500 Segment 2 Unused Segment 3 2000 1000 Valid = part of process’s address space Not all virtual addresses are valid Nothing in segment 2 Nothing in segment 1 above 4ff Accesses to invalid addresses are illegal Hence a “segmentation fault” Code Data segment Stack segment Virtual memory (3,fff) . (Stack) (3,000) … (1,4ff) . (Data) (1,000) (0,6ff) . (Code) (0,0)
37
2. Segmentation Segment # Base Bound Segment 0 4000 700 Segment 1 500
500 Segment 2 Unused Segment 3 2000 1000 Segments can grow (can move to new physical location) Protection Different protections for segments E.g., read-only or read-write B&B forced uniform protection Code Data segment Stack segment Virtual memory (3,fff) . (Stack) (3,000) … (1,4ff) . (Data) (1,000) (0,6ff) . (Code) (0,0)
38
2. Segmentation What changes on a context switch? Segment # Base Bound
4000 700 Segment 1 500 Segment 2 Unused Segment 3 2000 1000 What changes on a context switch? Contents of segment table Typically small (not many segments) Code Data segment Stack segment Virtual memory (3,fff) . (Stack) (3,000) … (1,4ff) . (Data) (1,000) (0,6ff) . (Code) (0,0)
39
Segmentation pros and cons
Multiple areas of address space can grow separately Easy to share parts of address space (can share code segment)
40
Segmentation sharing Virtual info Physical info Segment # Base Bound
4000 700 Segment 1 500 Segment 2 Unused Segment 3 2000 1000 Code Data segment Stack segment Segment # Base Bound Segment 0 4000 700 Segment 1 4700 500 Segment 2 Unused Segment 3 3000 1000 Code Data segment Stack segment Virtual info Physical info
41
Segmentation pros and cons
Multiple areas of address space can grow separately Easy to share parts of address space (can share code segment) Cons Complex memory allocation (still have external fragmentation)
42
2. Segmentation Do we get virtual memory?
(can an address space be larger than phys mem?) Segments must be smaller than physical memory Address space can contain multiple segments Can swap segments in and out What makes this tricky? Performance (segments are relatively large) Complexity (odd segment sizes packing problem) Solution: fixed-size segments called pages!
43
Course administration
Project 1 Project posted this weekend (simple web server) Due about 1.5 weeks from today Let me know if you don’t have a group Questions?
44
Dynamic address translation
User process Translator (MMU) Physical memory Virtual address Physical address Base and bounds Segmentation Paging
45
3. Paging Very similar to segmentation
Allocate memory in fixed-size chunks Chunks are called pages Why fixed-size pages? Allocation is greatly simplified Just keep a list of free physical pages Any physical page can store any virtual page
46
3. Paging Virtual addresses (32 bits)
Virtual page # (e.g., upper 20 bits) Offset within the page (e.g., low 12 bits) Each address refers to a byte of data For 12-bit offset, how big are pages? 4KB (2^12 different offsets)
47
3. Paging Translation data is a page table Virtual page #
Physical page # 10 1 15 2 20 3 invalid … Invalid Why no bound column? 2^20 – 1 Why?
48
Page table must reside in memory. Too large for registers alone.
3. Paging Translation data is a page table Virtual page # Physical page # 10 1 15 2 20 3 invalid … Invalid Page table must reside in memory. Too large for registers alone.
49
3. Paging if (virtual page # is invalid) { trap to kernel } else {
physical page = pagetable[virtual page].physPageNum physical address is {physical page}{offset} }
50
3. Paging What happens on a context switch?
Copy out contents of entire page table Copy new page table into translation box Use indirection to make switching faster Change page table base pointer (register) Should point to new page table
51
3. Paging Pages can be in memory or swapped to disk
“Swapped out” = “paged out” How does CPU know if in phys mem or paged out? Page table entry must have more info Resident bit
52
3. Paging Resident bit in page table entry
If bit is 1 (virtual page is in physical memory) Page table entry has valid translation Use to look up physical page If bit is 0 (virtual page is swapped out) MMU causes a page fault to OS (runs kernel’s page fault handler) (same way timer interrupts are handled)
53
3. Paging On a page fault, what does the OS do? What happens next?
OS finds free physical page (may have to page out another virtual page) OS does I/O to read virtual page into mem (or zero fills it, if it’s a new page) What happens next? OS reruns process at last faulting instruction How does it know the last instruction? Hardware provides process’s PC to handler on fault
54
Valid vs. resident pages
Resident = page is in physical memory Accessing non-resident pages is not an error Access triggers page fault, OS handles it Valid = page is legal to address Who makes a virtual page resident (or not)? OS memory manager (allocating physical memory)
55
Valid vs. resident pages
Resident = page is in physical memory Accessing non-resident pages is not an error Access triggers page fault, OS handles it Valid = page is legal to address Who makes a virtual page valid (or not)? User process controls this (with limits from OS)
56
Valid vs. resident pages
Resident = page is in physical memory Accessing non-resident pages is not an error Access triggers page fault, OS handles it Valid = page is legal to address Why would a process make a page invalid? Make the process fail-stop Accidental/buggy references kill the process Same for process making pages read-only (writes to code segment are probably bugs)
57
Page size trade-offs Pages size is fixed, so we must choose wisely
If page size is too small Lots of page table entries Big page table If we use 4-byte pages with 4-byte PTEs Num PTEs = 2^30 = 1 billion 1 billion PTE * 4 bytes/PTE = 4 GB Would take up entire (32-bit) address space!
58
Page size trade-offs What if we use really big (1GB) pages?
Internal fragmentation Wasted space within the page Recall external fragmentation (wasted space between pages/segments)
59
Page size trade-offs Compromise between the two
x86 page size = 4kb for 32-bit processor Sparc page size = 8kb For 4KB pages, how big is the page table? 1 million page table entries 32-bits (4 bytes) per page table entry 4 MB per page table
60
Paging pros and cons Pros Cons Simple memory allocation
Can share lots of small pieces Easy to grow address space Cons Large page tables (even with large invalid section)
61
What we want Efficient use of physical memory How we’ll do this
Little external or internal fragmentation Easy sharing between processes Space-efficient data structure How we’ll do this Modify paging scheme
62
4. Multi-level translation
Standard page table Just a flat array of page table entries VA={b31,b30,…,b12,b11,…,b1,b0} High-order bits (Page number) Low-order bits (offset) Used to index into table
63
4. Multi-level translation
Multi-level page table Use a tree instead of an array VA={b31,b30,…,b22,b21,…, b12,b11,…,b1,b0} Level 1 Level 2 Low-order bits (offset) Used to index into table 2 Used to index into table 1 What is stored in the level 1 page table? If valid? If invalid?
64
4. Multi-level translation
Multi-level page table Use a tree instead of an array VA={b31,b30,…,b22,b21,…, b12,b11,…,b1,b0} Level 1 Level 2 Low-order bits (offset) Used to index into table 2 Used to index into table 1 What is stored in the level 2 page table? If valid? If invalid?
65
Two-level tree … … ? ? 1 … 1023 ? Level 1 Level 2
1 … 1023 ? Level 1 NULL NULL Level 2 0: PhysPage, Res, Prot 0: PhysPage, Res, Prot 1: PhysPage, Res, Prot 1: PhysPage, Res, Prot … … ? 1023: PhysPage, Res, Prot 1023: PhysPage, Res, Prot ? VA={b31,b30,…,b22,b21,…, b12,b11,…,b1,b0} Level 1 Level 2 Offset
66
How does this save space?
Two-level tree 1 … 1023 ? Level 1 NULL NULL Level 2 0: PhysPage, Res, Prot 0: PhysPage, Res, Prot 1: PhysPage, Res, Prot 1: PhysPage, Res, Prot … … ? 1023: PhysPage, Res, Prot 1023: PhysPage, Res, Prot ? How does this save space?
67
What changes on a context switch?
Two-level tree 1 … 1023 ? Level 1 NULL NULL Level 2 0: PhysPage, Res, Prot 0: PhysPage, Res, Prot 1: PhysPage, Res, Prot 1: PhysPage, Res, Prot … … ? 1023: PhysPage, Res, Prot 1023: PhysPage, Res, Prot ? What changes on a context switch?
68
Multi-level translation pros and cons
Space-efficient for sparse address spaces Easy memory allocation Easy sharing Cons? Two extra lookups per reference (read level 1 PT, then read level 2 PT) (memory accesses just got really slow)
69
Translation look-aside buffer
Aka the “TLB” Hardware cache from architecture course Maps virtual page #s to physical page #s On cache hit, get PTE very quickly On miss use page table, store mapping, restart instruction What happens on a context switch? Have to flush the TLB Takes time to rewarm the cache As in life, context switches may be expensive
70
Accessing virtual memory
User process Physical pages CPU OS
71
Accessing virtual memory
User process Access virtual address Physical pages CPU OS
72
Accessing virtual memory
User process Access virtual address Determine if access is allowed Physical pages CPU OS
73
Accessing virtual memory
User process Access virtual address If OK, retrieve data, execute instruction Physical pages CPU OS
74
Accessing virtual memory
User process Access virtual address Physical pages CPU If not OK, trap to OS OS
75
Accessing virtual memory
User process Access virtual address Physical pages CPU If not OK, trap to OS Determine if access is valid OS
76
Accessing virtual memory
User process Access virtual address Physical pages If not valid, kill process CPU If not OK, trap to OS OS
77
Accessing virtual memory
User process Access virtual address Physical pages CPU If not OK, trap to OS If valid, determine if resident OS
78
Accessing virtual memory
User process Access virtual address Physical pages CPU If not OK, trap to OS If resident, update protections, retry faulting instruction OS
79
Accessing virtual memory
User process Access virtual address Physical pages CPU If not OK, trap to OS If not resident, load non-resident page, update page table OS
80
Accessing virtual memory
User process Retry faulting instruction Access virtual address Physical pages CPU If not OK, trap to OS If not resident, load non-resident page, update page table OS
81
Accessing virtual memory
User process Retry faulting instruction Access virtual address Physical pages CPU What should OS do if physical mem is already full? If not OK, trap to OS If not resident, load non-resident page, update page table OS
82
User/kernel translation data
4GB Kernel data (same for all page tables) 3GB (0xc ) User data (different for every process) 0GB Virtual memory
83
Where to store translation data?
Could be kept in physical memory How the MMU accesses it On x86, PTBR (aka CR3 register) refers to physical memory Could be in kernel virtual address space A little weird, but what is done in practice How to keep translation data in a place that must be translated? Translation for user address space is in kernel virtual memory Kernel’s translation data always in physical memory (pinned) Does anything else need to be pinned (and why)? Kernel’s page fault handler code also has to be pinned.
84
Where to store translation data?
How does kernel access data in user’s address space? If for currently loaded process, access using current page table If for another process, have to change page table
85
Kernel vs. user mode Who sets up the data used by the MMU?
Can’t be the user process Otherwise could access anything Only kernel is allowed to modify any memory Processor must know to allow kernel To update the translator To execute privileged instructions (halt, do I/O)
86
Kernel vs. user mode How does machine know kernel is running? Mode bit
This requires hardware support Process supports two modes, kernel and user Mode is indicated by a hardware register Mode bit
87
Protection recap All memory accesses go through a translator
GBs of protected data All memory accesses go through a translator Who can modify translator’s data? Only kernel can modify translator’s data How do we know if kernel is running? Mode bit indicates if kernel is running Who can modify the mode bit? One bit of protected data Making progress: the amount of protected data is down to a bit
88
Protecting the mode bit
Can kernel change the mode bit? Yes. Kernel is completely trusted. Can user process change the mode bit? Not directly User programs need to invoke the kernel Must be able to initiate a change
89
When to transition from user to kernel?
Exceptions (interrupts, traps) Access something out of your valid address space (e.g., segmentation fault) Disk I/O finishes, causes interrupt Timer pre-emption, causes interrupt Page faults System calls Similar in purpose to a function call Kernel as software library
90
Example system calls Process management Signals Memory management
Fork/exec (start a new process), exit, getpid Signals Kill (send a signal), sigaction (set up handler) Memory management Brk (extend the valid address space), mmap File I/O Open, close, read, write Network I/O Accept, send, receive System management Reboot
91
System call implementation
Syscalls are like functions, but different Implemented by special instruction syscall Execution of syscall traps to kernel Processor safely transfers control to kernel Hardware invokes kernel’s syscall trap handler
92
System call implementation
Developers use C++/Java platform C++/Java runtime makes calls into libc Libc wraps systems calls Kernel handles system call
93
Tracing through “cin >> a;”
C++: cin >> a Java: in.read() C: read () libc: syscall(SYS_read,filenum,offset,numbytes) Processor: kernel’s syscall handler Crucial step kernel: sys_read kernel: // perform I/O
94
Kernel trap details Hardware must atomically
Set processor mode bit to “kernel” Save current registers (SP, PC, general purpose) Change execution stack to kernel (SP) Jump to exception handler in kernel (PC) What does this look a lot like? Switching between threads (see previous lecture)
95
Kernel trap details User process can initiate mode switch
But only transfers control in limited way (i.e., to predefined kernel code) How does processor know where to jump? Stored in hardware “interrupt vector table” Who can update the interrupt vector table? Kernel does this at startup Code that runs first gets control of machine
96
Syscall arguments, return values
For args passed in memory, in which address space do they reside? User (programs can’t access kernel’s portion of address space) Linux passes parameters via registers Before syscall Push ebp, edx, ecx vals onto user stack Copy esp (stack pointer) into ebp Each process has kernel stack CPU knows kernel stack via “Task Segment State” CPU sets esp using TSS Kernel finds user stack in ebp register
97
Tracing through read()
C: read (int fd, void *buf, size_t size) libc: push fd, buf, size onto user’s stack Note: pointer! kernel: sys_read () { verify fd is an open file verify [buf – buf+size) valid, writable read file from data (e.g. disk) find physical location of buf copy data to buf’s physical location } Verify arguments!
98
Ready to read papers! First paper(s) next lecture Questions?
THE by Edgar Dijkstra Maybe another OS structure paper Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.