Lecture Week12 Virtual Memory
Lecture Week12 Virtual Memory
Virtual Memory
Peter Sutton
School of Electrical Engineering and Computer Science
The University of Queensland
Outline
I Admin
I Virtual Memory
I Page Tables
I User space memory management
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 2
Admin
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 3
Abstraction of memory
We have assumed that there is a way for processes to have their own view of memory.
We’d like:
I Protection
I Other processes should not be able to interfere with another process.
I Sharing
I For interprocess communication (IPC)
I Avoiding redundancy, e.g.
I Unmodified parts of fork()ed children
I every program loads/links libc.so - how many copies?!
I Optimisation
I Demand paging allows us to load things only as they are required
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 4
Abstraction of memory
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 5
Virtual memory
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 6
Virtual memory
Ideally, the kernel doesn’t have to get involved very often either.
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 7
Virtual memory - hardware view
[zilog.com]
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 8
Page table
I The virtual address space is divided into equal sized pieces called “pages”.
I The physical address space is divided into “frames”
I Frames and pages are the same size.
I Page/frame sizes being a power of two means addresses can be easily split, e.g.
4k pages in a 32-bit address space
(4096 = 212 )
31 12 11 0
PAGE/FRAME Number Offset
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 9
Virtual address translation - simplified view
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 10
Page table
I Page# = VA / pageSize
I Offset = VA mod pageSize
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 12
Address translation
page#|offset → frame#|offset
ie:
page#|offset → PT(page#)|offset
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 13
Implementation
I Needing to lookup a frame in the page table means that each memory access
from a program would need two memory accesses2 :
I One into the page table
I One for the actual memory access
I Reduce this burden using a Translation Lookaside Buffer (TLB).
I Hardware cache for (page→frame) mappings
I Associative (content addressable) memory
I Fast
I Hardware only goes to actual memory-based table if there is a TLB “miss”
I hardware-dependent
I some CPU architectures can ”walk” memory-based page tables, others can’t so the
kernel does it for the CPU
2
In the general case
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 15
TLB
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 16
Page faults
I Core idea: legal vs illegal decisions are made at the level of pages not individual
addresses.
I If page 0 (and possibly other very low numbered pages) are always marked as
invalid, then hardware makes sure the null pointer will seg fault.
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 18
Page replacement algorithms?
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 19
Separate address spaces
I This comes as a consequence of each process having its own page table.
I Shared pages happen when the kernel maps the same frame into multiple page
tables.
I Memory protection:
I Pages are only valid if the kernel maps them to a frame.
I A process can not construct a pointer to another process’ frames
*((char *)(rand())= 0x0; // :)
I Processes can ask the kernel to do it... shared mem, mmap() and friends
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 20
Multi-level tables
Remember how we said a 4kiB pages in a 32 bit address space require 4MiB per
process? We can be a lot smarter than that.
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 21
Multi-level tables — gaps
[stackoverflow.com]
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 22
Multi-level tables
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 23
Multi-level tables
Note that there are relationships among field sizes and the other parameters of the
system:
I PTEs must be large enough to uniquely identify a page or frame:
I at least (PA size - offset size) bits wide
I additional bits in PTE are used for storing flag such as permissions (RW/RO/EXEC)
etc
I in CSSE2310 we will specify PTE sizes in bytes so you don’t need to work
this out
I Page size and PTE size define the PTE field size, e.g.
I 4KiB pages and 4 byte PTEs
→ 1024 PTEs per page
→ 10 bits per PTE field in the virtual address
I Each PTE table is exactly one page in size (first level table can be an exception, see
next slides)
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 24
Multi-level tables
A trickier example:
I 4 level page table
I 64 bit virtual addresses (VA), 8 byte page table entries (PTE)
I 8KiB pages/frames (13 bits)
working backwards:
I each page (8KiB) can hold 1024 = 210 PTEs
→ each PTE field in the VA is 10 bits
I VA structure will be
?? Lvl1 Lvl2 Lvl3 Lvl4 offset
11 bits 10 bits 10 bits 10 bits 10 bits 13 bits
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 25
Multi-level tables continued
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 26
User Space Memory Management
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 27
Memory layout
top of mem
kernel memory
bottom of stack
top of stack
...
...
top of heap
bottom of heap
other data
text “segment”
forbidden
0 forbidden
Memory mapped content goes somewhere between heap and stack.
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 28
Lots of room in 64bit?
4
Address Space Layout Randomisation
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 29
Kernel heap interaction?
The kernel only cares about where the top of the heap is
I If the heap needs more space, then a system call (sbrk()) will allocate more
valid pages to the process.
I malloc() is a userspace function (which will ask the kernel for more pages if
needed).
Note that modern malloc() implementations use a variety of methods to request
more memory from the kernel - e.g. anonymous memory maps using mmap().
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 30
Some fun with /proc/pid/mem
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 31
Coming Up
The University of Queensland – School of EECS CSSE2310/7231 – Semester 1, 2024 – Lecture – Week 12 32