SlideShare a Scribd company logo
Virtual Memory (Making a Process)
Rust Runtime

Recap: Last Week
run::Process::new(program, argv, options)
spawn_process_os(prog, args, env, dir, in_fd, …)
fork()
int 0x80

libc: fork()
jumps into kernel code
sets supervisor mode

linux kernel: fork syscall
12 November 2013

University of Virginia cs4414

1
Plan for This Week
• How the Kernel Makes a Process
– Virtual Memory

• Thursday: diving into fork.c

12 November 2013

University of Virginia cs4414

2
From Class 3:

Batch Processing

Program

Computer
Center

Your Program Runs

Output: Invalid Operation
Charge: $174.32
12 November 2013

University of Virginia cs4414

3
Process Abstraction
Provide each program with the illusion that it
owns the whole machine.
The best example of this way to do things
is Linux, which is an operating system,
which is a program that keeps track of
other programs in a computer and gives
each its due in space and time.
Guy Steele, “How to Grow a Language”
HT: Anonymous for posting link in Piazza
forum
12 November 2013

University of Virginia cs4414

4
Memory Isolation
Process 1 should only be able to access Memory Space 1
Process 2 should only be able to access Memory Space 2

Memory
Space 1

12 November 2013

University of Virginia cs4414

Memory
Space 2

5
Software-Based Memory Isolation
Original Code

“Sandboxed” Code

…
movq %rax, -8(%rbp)
…

…
movq -8(%rbp),%rdx
andq %rdx,%rgx
movq %rax, %rdx
…

Safe
Loader

Assumes %rdx is reserved and %rgx
is protected and holds a mask for
the memory segment

12 November 2013

University of Virginia cs4414

6
SOSP 1993
SOSP 1993

12 November 2013

University of Virginia cs4414

7
12 November 2013

University of Virginia cs4414

8
Hardware-Based Memory Isolation
Original Code

Running Code

…
movq %rax, -8(%rbp)
…

…
movq %rax, -8(%rbp)
…

Memory
Space 1

12 November 2013

University of Virginia cs4414

Memory
Space 2

9
Virtual Memory
address in Process P

Virtual Memory Mapping
physical address
owned by Process P

User-level processes cannot access physical memory
directly: all memory addresses created by process P are
virtual addresses, mapped into physical addresses
owned by process P
12 November 2013

University of Virginia cs4414

10
Getting Into the Details…

12 November 2013

University of Virginia cs4414

11
SOSP 1967

Procedure Base Register:
segment number of executing
procedure
Argument Pointer
Base Pointer
Linkage Pointer
Stack Pointer
Descriptor Base Register

12 November 2013

University of Virginia cs4414

12
Generating an Address
18 bits

18 bits

218 = 262144
12 November 2013

University of Virginia cs4414

13
Addressing Mode selects:
Argument Pointer
Base Pointer
Linkage Pointer
Stack Pointer
12 November 2013

University of Virginia cs4414

14
Descriptor Base Register

What does
MULTICS
need to do
to switch
processes?

12 November 2013

University of Virginia cs4414

15
12 November 2013

University of Virginia cs4414

16
1982

12 November 2013

"It used to be that programs were
easy to copy and change. But
manufacturers began to lose money
as many people made copies of
software and gave them to their
friends. Now, many manufacturers
have figured out how to 'copyprotect' discs. A copy-protected disc–
like a cartridge–can’t be copied or
changed. To our mind this is a
disaster: Most people learn
programming by changing programs
to fit their own needs. This capability
of customization is what makes
computers so attractive. New ways of
copy protection will probably be
found soon. Until then, a computer
owner may have to put up with being
'locked out' of his own machine.”
Popular Mechanics, January 1982

University of Virginia cs4414

17
Intel 80186

Intel 80286
First x86 Processor with Virtual Memory Support
“Protected Mode”

12 November 2013

University of Virginia cs4414

18
http://en.wikipedia.org/wiki/File:Intel_i80286_arch.svg
12 November 2013

University of Virginia cs4414

19
Five x86-64 Processor Modes
Real Mode: pretend to be an 8086
20-bit direct-access address space
Protected Mode: “native state”
System Management Mode: platformspecific power management and
security (separate address space)
Compatibility Mode: pretend to be
x86-32
IA-32e/64-bit Mode: run applications “For brevity, the 64-bit submode is referred to as 64-bit
in 64-bit address space
mode in IA-32 architecture.”
12 November 2013

University of Virginia cs4414

20
Protected State (can
only be modified by
the kernel):
RFLAGS (includes EFLAGS)
Includes I/O Privilege Level
Control Registers
CR0 bit 0: controls if processor
is in protected mode
CR3: page directory base
register

12 November 2013

University of Virginia cs4414

21
Address Translation

Memory

University of Virginia cs4414

Paging
Unit

Physical Address

Linear Address

Logical Address
12 November 2013

Segmentation
Unit

22
Accessing Memory

16 bits to select segment, 16- 32- or 64- bits to select offset
Actual addressable space for user-level process in Unix:
247 bytes = 128TiB

12 November 2013

University of Virginia cs4414

23
Memory

Paging
Unit

Physical Address

Linear Address

Logical Address

Segmentatio
n Unit

Computing
the Linear
Address

Segment selection
is inferred from
instruction type

Logical (“General”, “Virtual”) Address
Segment
Selector

12 November 2013

Offset

University of Virginia cs4414

24
Fetching an Instruction
EIP
CS

Instruction Pointer (Offset)

32 bits
16 bits

Code Segment

13 bits

1

Table Index

12 November 2013

University of Virginia cs4414

Ring
Global or
Local Table

Only Kernel can write to Segment Registers

2

25
Segmentation Tables
Global Descriptor Table (GDT)
limit

base address

linear
address
space
0-264 - 1

Segments can overlap!
13 bits – up to 8192 entries
12 November 2013

University of Virginia cs4414

26
Segmentation Tables
Global Descriptor Table (GDT)
Local Descriptor
Table
(per process)

13 bits – up to 8192 entries
12 November 2013

How does the processor find the GDT/LDT?
University of Virginia cs4414

27
The GDT and LDT are just
data structures in memory!
Special registers store their
locations

12 November 2013

University of Virginia cs4414

28
Logical Address

Segmentatio
n Unit

Linear Address

Paging
Unit
Physical Address

Memory
12 November 2013

University of Virginia cs4414

29
Linear Address

Logical Address

64
2

Segmentation
Unit

Paging

linear addresses
What would it cost to have 264 bytes of RAM?

12 November 2013

University of Virginia cs4414

30
$10 per 1GB = 230 bytes

264 bytes = $10 * 234

$172B ~ ½ Google’s Market Cap
12 November 2013

University of Virginia cs4414

31
Paging
Memory

Paging
Unit

Physical Address

Linear Address

Logical Address

Segmentation
Unit

We don’t need to store the whole address space in memory!
Most of it is unused, and we can store rarely-used parts on the disk.
12 November 2013

University of Virginia cs4414

32
Memory

Physical Address

Linear Address

Paging
Unit

Image from Wikipedia
12 November 2013

University of Virginia cs4414

33
Overview (Intel 386)
32-bit linear address
CR3

Dir

Page

10 bits
(1K tables)

Page
Directory

Offset

10 bits
12 bits
(1K entries) (4K pages)

Page Entry

Page Table

Physical
Memory
Page + Offset

CR3+Dir

12 November 2013

University of Virginia cs4414

34
Page Table Entries
CR3
Page Entry

Page Table
Page
Directory

12 November 2013

20 bits: physical
address of page
12 bits: flags
user/kernel page
write permission
present
University of Virginia cs4414

Physical
Memory
Page + Offset

35
386 Checkup
32-bit linear address
CR3

Dir

Page

10 bits
(1K tables)

Page
Directory

Offset

10 bits
12 bits
(1K entries) (4K pages)

20 bits addr / 12 bits flags

Page Table

Physical
Memory
Page + Offset

CR3+Dir

How many pages do we need to store the page table?
12 November 2013

University of Virginia cs4414

36
How slow is this???!
Memory

Page

Paging
Unit

Physical Address

Dir

Linear Address

Logical Address

Segmentation Unit

Offset

CR3

Page
Directory

Page Table

Physical Memory

GDTR

Global
Descriptor
Table
12 November 2013

University of Virginia cs4414

37
Translation Lookaside Buffer (Cache)

Memory

Page

Paging
Unit

Physical Address

Dir

Linear Address

Logical Address

Segmentation Unit

Offset

CR3

Page
Directory

Page Table

Physical Memory

GDTR

Global
Descriptor
Table
12 November 2013

University of Virginia cs4414

38
Page Fault
CR3
Page Entry

Page Table
Page
Directory

12 November 2013

Physical
Memory

20 bits: physical
address of page
12 bits: flags
user/kernel page
write permission
present
University of Virginia cs4414

39
How common are page faults?

12 November 2013

University of Virginia cs4414

40
top -o mem -stats pid,command,cpu,mem,mregion,vsize,faults
12 November 2013

University of Virginia cs4414

41
Physical Memory:
9106M used (2553M “wired” – cannot be paged out)
+ 5090M unused
= 14196M
where is my missing ~2GB???!

Virtual Memory: 594G (total)

top -o mem -stats pid,command,cpu,mem,mregion,vsize,faults
12 November 2013

University of Virginia cs4414

42
How expensive is a page fault?

12 November 2013

University of Virginia cs4414

43
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
char *s = (char *) malloc (1);
int i= 0;
while (1) {
printf("%d: %xn", i, s[i]);
i += 4;
}
}

12 November 2013

What will this program do?

> ./a.out
0: 0
4: 0
8: 0
12: 0
…1033872: 0
1033876: 0
1033880: 0
1033884: 0
Segmentation fault: 11

University of Virginia cs4414

44
Charge
• Make progress on your projects: everyone
should have a clear idea what you are doing
now
• Will post more details on next deliverable
(design reviews) soon
Challenge: write a program that takes N as an input and
produces (nearly) exactly N page faults.

12 November 2013

University of Virginia cs4414

45

More Related Content

PPTX
Crossing into Kernel Space
David Evans
 
PPTX
Putting a Fork in Fork (Linux Process and Memory Management)
David Evans
 
PPTX
Making a Process
David Evans
 
PPTX
Scheduling
David Evans
 
PPTX
Multi-Tasking Map (MapReduce, Tasks in Rust)
David Evans
 
PPTX
Smarter Scheduling
David Evans
 
PPTX
Synchronization
David Evans
 
PPTX
Segmentation Faults, Page Faults, Processes, Threads, and Tasks
David Evans
 
Crossing into Kernel Space
David Evans
 
Putting a Fork in Fork (Linux Process and Memory Management)
David Evans
 
Making a Process
David Evans
 
Scheduling
David Evans
 
Multi-Tasking Map (MapReduce, Tasks in Rust)
David Evans
 
Smarter Scheduling
David Evans
 
Synchronization
David Evans
 
Segmentation Faults, Page Faults, Processes, Threads, and Tasks
David Evans
 

What's hot (20)

PPTX
Managing Memory
David Evans
 
PPTX
Making a Process (Virtualizing Memory)
David Evans
 
PPTX
SSL Failing, Sharing, and Scheduling
David Evans
 
PPTX
The Internet
David Evans
 
PPTX
Scheduling in Linux and Web Servers
David Evans
 
PPTX
Gash Has No Privileges
David Evans
 
PPTX
Once Upon a Process
David Evans
 
PPTX
Storage
David Evans
 
PDF
Kernel Recipes 2019 - Formal modeling made easy
Anne Nicolas
 
PPTX
Microkernels and Beyond
David Evans
 
PPTX
Mutual Exclusion
David Evans
 
PPTX
Lec09 nbody-optimization
Taras Zakharchenko
 
PPTX
Flash! (Modern File Systems)
David Evans
 
PPTX
Lec05 buffers basic_examples
Taras Zakharchenko
 
PPTX
Lec11 timing
Taras Zakharchenko
 
PDF
Kernel Recipes 2018 - KernelShark 1.0; What's new and what's coming - Steven ...
Anne Nicolas
 
PPTX
Lec07 threading hw
Taras Zakharchenko
 
PDF
Profiling your Applications using the Linux Perf Tools
emBO_Conference
 
PDF
Alto Desempenho com Java
codebits
 
PPTX
Lec13 multidevice
Taras Zakharchenko
 
Managing Memory
David Evans
 
Making a Process (Virtualizing Memory)
David Evans
 
SSL Failing, Sharing, and Scheduling
David Evans
 
The Internet
David Evans
 
Scheduling in Linux and Web Servers
David Evans
 
Gash Has No Privileges
David Evans
 
Once Upon a Process
David Evans
 
Storage
David Evans
 
Kernel Recipes 2019 - Formal modeling made easy
Anne Nicolas
 
Microkernels and Beyond
David Evans
 
Mutual Exclusion
David Evans
 
Lec09 nbody-optimization
Taras Zakharchenko
 
Flash! (Modern File Systems)
David Evans
 
Lec05 buffers basic_examples
Taras Zakharchenko
 
Lec11 timing
Taras Zakharchenko
 
Kernel Recipes 2018 - KernelShark 1.0; What's new and what's coming - Steven ...
Anne Nicolas
 
Lec07 threading hw
Taras Zakharchenko
 
Profiling your Applications using the Linux Perf Tools
emBO_Conference
 
Alto Desempenho com Java
codebits
 
Lec13 multidevice
Taras Zakharchenko
 
Ad

Viewers also liked (20)

PPT
Ch09
guest64730d
 
DOCX
virtual memory
Abeer Naskar
 
PPT
Ch10: Virtual Memory
Ahmar Hashmi
 
PPT
Os8
issbp
 
PPT
CPU HISTORY MUKUND
Mukund Trivedi
 
PDF
Virtual Memory
Farzan Dehbashi
 
PPTX
Virtual memory
Asif Iqbal
 
PPTX
basics of virtual memory
Adarsh Patel
 
PPTX
Virtual Memory
Archith777
 
PDF
Virtual memory
Mohd Arif
 
PPTX
Virtual memory
vatsaanadi
 
PPT
39 virtual memory
myrajendra
 
PPT
Virtual memory
rapunzel08
 
PPT
virtual memory
surbhisinghal90
 
PDF
Operating Systems - Virtual Memory
Emery Berger
 
PPTX
Virtual memory managment
Santu Kumar
 
PDF
Virtual memory
Dr. Shashank Shetty
 
DOC
Virtual Memory vs Cache Memory
Ashik Iqbal
 
PPTX
Operating System (Scheduling, Input and Output Management, Memory Management,...
Project Student
 
virtual memory
Abeer Naskar
 
Ch10: Virtual Memory
Ahmar Hashmi
 
Os8
issbp
 
CPU HISTORY MUKUND
Mukund Trivedi
 
Virtual Memory
Farzan Dehbashi
 
Virtual memory
Asif Iqbal
 
basics of virtual memory
Adarsh Patel
 
Virtual Memory
Archith777
 
Virtual memory
Mohd Arif
 
Virtual memory
vatsaanadi
 
39 virtual memory
myrajendra
 
Virtual memory
rapunzel08
 
virtual memory
surbhisinghal90
 
Operating Systems - Virtual Memory
Emery Berger
 
Virtual memory managment
Santu Kumar
 
Virtual memory
Dr. Shashank Shetty
 
Virtual Memory vs Cache Memory
Ashik Iqbal
 
Operating System (Scheduling, Input and Output Management, Memory Management,...
Project Student
 
Ad

Similar to Virtual Memory (Making a Process) (20)

PPTX
Storage Systems
David Evans
 
PDF
Ashwin_Thesis
Ashwin Ramesh
 
PPTX
Spark & Cassandra at DataStax Meetup on Jan 29, 2015
Sameer Farooqui
 
PDF
Cloud, Distributed, Embedded: Erlang in the Heterogeneous Computing World
Omer Kilic
 
PDF
Spark Summit East 2015 Advanced Devops Student Slides
Databricks
 
PPTX
What the &~#@&lt;!? (Memory Management in Rust)
David Evans
 
PDF
Graph Analytics with ArangoDB
ArangoDB Database
 
PPTX
SEMLA_logging_infra
swy351
 
PDF
Preserving Computer-Aided Design, Digital Preservation Coalition Report
Ruggero Lancia
 
PPTX
Dataiku Flow and dctc - Berlin Buzzwords
Dataiku
 
PDF
Bosco r users2013
Miha Ahronovitz
 
PDF
Tuning and Debugging in Apache Spark
Databricks
 
PDF
How Apache Spark fits into the Big Data landscape
Paco Nathan
 
PDF
survey_of_matrix_for_simulation
Jon Hand
 
PPT
11. From Hadoop to Spark 1:2
Fabio Fumarola
 
PDF
Introduction to Spark Training
Spark Summit
 
PPTX
Intro to Spark development
Spark Summit
 
PDF
Understanding Hadoop
Ahmed Ossama
 
PDF
Apache Spark
Uwe Printz
 
PDF
Data Analytics and Machine Learning: From Node to Cluster on ARM64
Ganesh Raju
 
Storage Systems
David Evans
 
Ashwin_Thesis
Ashwin Ramesh
 
Spark & Cassandra at DataStax Meetup on Jan 29, 2015
Sameer Farooqui
 
Cloud, Distributed, Embedded: Erlang in the Heterogeneous Computing World
Omer Kilic
 
Spark Summit East 2015 Advanced Devops Student Slides
Databricks
 
What the &~#@&lt;!? (Memory Management in Rust)
David Evans
 
Graph Analytics with ArangoDB
ArangoDB Database
 
SEMLA_logging_infra
swy351
 
Preserving Computer-Aided Design, Digital Preservation Coalition Report
Ruggero Lancia
 
Dataiku Flow and dctc - Berlin Buzzwords
Dataiku
 
Bosco r users2013
Miha Ahronovitz
 
Tuning and Debugging in Apache Spark
Databricks
 
How Apache Spark fits into the Big Data landscape
Paco Nathan
 
survey_of_matrix_for_simulation
Jon Hand
 
11. From Hadoop to Spark 1:2
Fabio Fumarola
 
Introduction to Spark Training
Spark Summit
 
Intro to Spark development
Spark Summit
 
Understanding Hadoop
Ahmed Ossama
 
Apache Spark
Uwe Printz
 
Data Analytics and Machine Learning: From Node to Cluster on ARM64
Ganesh Raju
 

More from David Evans (20)

PPTX
Cryptocurrency Jeopardy!
David Evans
 
PPTX
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
David Evans
 
PPTX
Hidden Services, Zero Knowledge
David Evans
 
PPTX
Anonymity in Bitcoin
David Evans
 
PPTX
Midterm Confirmations
David Evans
 
PPTX
Scripting Transactions
David Evans
 
PPTX
How to Live in Paradise
David Evans
 
PPTX
Bitcoin Script
David Evans
 
PPTX
Mining Economics
David Evans
 
PPTX
Mining
David Evans
 
PPTX
The Blockchain
David Evans
 
PPTX
Becoming More Paranoid
David Evans
 
PPTX
Asymmetric Key Signatures
David Evans
 
PPTX
Introduction to Cryptography
David Evans
 
PPTX
Class 1: What is Money?
David Evans
 
PPTX
Multi-Party Computation for the Masses
David Evans
 
PPTX
Proof of Reserve
David Evans
 
PPTX
Silk Road
David Evans
 
PPTX
Blooming Sidechains!
David Evans
 
PPTX
Useful Proofs of Work, Permacoin
David Evans
 
Cryptocurrency Jeopardy!
David Evans
 
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
David Evans
 
Hidden Services, Zero Knowledge
David Evans
 
Anonymity in Bitcoin
David Evans
 
Midterm Confirmations
David Evans
 
Scripting Transactions
David Evans
 
How to Live in Paradise
David Evans
 
Bitcoin Script
David Evans
 
Mining Economics
David Evans
 
Mining
David Evans
 
The Blockchain
David Evans
 
Becoming More Paranoid
David Evans
 
Asymmetric Key Signatures
David Evans
 
Introduction to Cryptography
David Evans
 
Class 1: What is Money?
David Evans
 
Multi-Party Computation for the Masses
David Evans
 
Proof of Reserve
David Evans
 
Silk Road
David Evans
 
Blooming Sidechains!
David Evans
 
Useful Proofs of Work, Permacoin
David Evans
 

Recently uploaded (20)

PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
The Future of Artificial Intelligence (AI)
Mukul
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
Doc9.....................................
SofiaCollazos
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Software Development Methodologies in 2025
KodekX
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 

Virtual Memory (Making a Process)

  • 2. Rust Runtime Recap: Last Week run::Process::new(program, argv, options) spawn_process_os(prog, args, env, dir, in_fd, …) fork() int 0x80 libc: fork() jumps into kernel code sets supervisor mode linux kernel: fork syscall 12 November 2013 University of Virginia cs4414 1
  • 3. Plan for This Week • How the Kernel Makes a Process – Virtual Memory • Thursday: diving into fork.c 12 November 2013 University of Virginia cs4414 2
  • 4. From Class 3: Batch Processing Program Computer Center Your Program Runs Output: Invalid Operation Charge: $174.32 12 November 2013 University of Virginia cs4414 3
  • 5. Process Abstraction Provide each program with the illusion that it owns the whole machine. The best example of this way to do things is Linux, which is an operating system, which is a program that keeps track of other programs in a computer and gives each its due in space and time. Guy Steele, “How to Grow a Language” HT: Anonymous for posting link in Piazza forum 12 November 2013 University of Virginia cs4414 4
  • 6. Memory Isolation Process 1 should only be able to access Memory Space 1 Process 2 should only be able to access Memory Space 2 Memory Space 1 12 November 2013 University of Virginia cs4414 Memory Space 2 5
  • 7. Software-Based Memory Isolation Original Code “Sandboxed” Code … movq %rax, -8(%rbp) … … movq -8(%rbp),%rdx andq %rdx,%rgx movq %rax, %rdx … Safe Loader Assumes %rdx is reserved and %rgx is protected and holds a mask for the memory segment 12 November 2013 University of Virginia cs4414 6
  • 8. SOSP 1993 SOSP 1993 12 November 2013 University of Virginia cs4414 7
  • 9. 12 November 2013 University of Virginia cs4414 8
  • 10. Hardware-Based Memory Isolation Original Code Running Code … movq %rax, -8(%rbp) … … movq %rax, -8(%rbp) … Memory Space 1 12 November 2013 University of Virginia cs4414 Memory Space 2 9
  • 11. Virtual Memory address in Process P Virtual Memory Mapping physical address owned by Process P User-level processes cannot access physical memory directly: all memory addresses created by process P are virtual addresses, mapped into physical addresses owned by process P 12 November 2013 University of Virginia cs4414 10
  • 12. Getting Into the Details… 12 November 2013 University of Virginia cs4414 11
  • 13. SOSP 1967 Procedure Base Register: segment number of executing procedure Argument Pointer Base Pointer Linkage Pointer Stack Pointer Descriptor Base Register 12 November 2013 University of Virginia cs4414 12
  • 14. Generating an Address 18 bits 18 bits 218 = 262144 12 November 2013 University of Virginia cs4414 13
  • 15. Addressing Mode selects: Argument Pointer Base Pointer Linkage Pointer Stack Pointer 12 November 2013 University of Virginia cs4414 14
  • 16. Descriptor Base Register What does MULTICS need to do to switch processes? 12 November 2013 University of Virginia cs4414 15
  • 17. 12 November 2013 University of Virginia cs4414 16
  • 18. 1982 12 November 2013 "It used to be that programs were easy to copy and change. But manufacturers began to lose money as many people made copies of software and gave them to their friends. Now, many manufacturers have figured out how to 'copyprotect' discs. A copy-protected disc– like a cartridge–can’t be copied or changed. To our mind this is a disaster: Most people learn programming by changing programs to fit their own needs. This capability of customization is what makes computers so attractive. New ways of copy protection will probably be found soon. Until then, a computer owner may have to put up with being 'locked out' of his own machine.” Popular Mechanics, January 1982 University of Virginia cs4414 17
  • 19. Intel 80186 Intel 80286 First x86 Processor with Virtual Memory Support “Protected Mode” 12 November 2013 University of Virginia cs4414 18
  • 21. Five x86-64 Processor Modes Real Mode: pretend to be an 8086 20-bit direct-access address space Protected Mode: “native state” System Management Mode: platformspecific power management and security (separate address space) Compatibility Mode: pretend to be x86-32 IA-32e/64-bit Mode: run applications “For brevity, the 64-bit submode is referred to as 64-bit in 64-bit address space mode in IA-32 architecture.” 12 November 2013 University of Virginia cs4414 20
  • 22. Protected State (can only be modified by the kernel): RFLAGS (includes EFLAGS) Includes I/O Privilege Level Control Registers CR0 bit 0: controls if processor is in protected mode CR3: page directory base register 12 November 2013 University of Virginia cs4414 21
  • 23. Address Translation Memory University of Virginia cs4414 Paging Unit Physical Address Linear Address Logical Address 12 November 2013 Segmentation Unit 22
  • 24. Accessing Memory 16 bits to select segment, 16- 32- or 64- bits to select offset Actual addressable space for user-level process in Unix: 247 bytes = 128TiB 12 November 2013 University of Virginia cs4414 23
  • 25. Memory Paging Unit Physical Address Linear Address Logical Address Segmentatio n Unit Computing the Linear Address Segment selection is inferred from instruction type Logical (“General”, “Virtual”) Address Segment Selector 12 November 2013 Offset University of Virginia cs4414 24
  • 26. Fetching an Instruction EIP CS Instruction Pointer (Offset) 32 bits 16 bits Code Segment 13 bits 1 Table Index 12 November 2013 University of Virginia cs4414 Ring Global or Local Table Only Kernel can write to Segment Registers 2 25
  • 27. Segmentation Tables Global Descriptor Table (GDT) limit base address linear address space 0-264 - 1 Segments can overlap! 13 bits – up to 8192 entries 12 November 2013 University of Virginia cs4414 26
  • 28. Segmentation Tables Global Descriptor Table (GDT) Local Descriptor Table (per process) 13 bits – up to 8192 entries 12 November 2013 How does the processor find the GDT/LDT? University of Virginia cs4414 27
  • 29. The GDT and LDT are just data structures in memory! Special registers store their locations 12 November 2013 University of Virginia cs4414 28
  • 30. Logical Address Segmentatio n Unit Linear Address Paging Unit Physical Address Memory 12 November 2013 University of Virginia cs4414 29
  • 31. Linear Address Logical Address 64 2 Segmentation Unit Paging linear addresses What would it cost to have 264 bytes of RAM? 12 November 2013 University of Virginia cs4414 30
  • 32. $10 per 1GB = 230 bytes 264 bytes = $10 * 234 $172B ~ ½ Google’s Market Cap 12 November 2013 University of Virginia cs4414 31
  • 33. Paging Memory Paging Unit Physical Address Linear Address Logical Address Segmentation Unit We don’t need to store the whole address space in memory! Most of it is unused, and we can store rarely-used parts on the disk. 12 November 2013 University of Virginia cs4414 32
  • 34. Memory Physical Address Linear Address Paging Unit Image from Wikipedia 12 November 2013 University of Virginia cs4414 33
  • 35. Overview (Intel 386) 32-bit linear address CR3 Dir Page 10 bits (1K tables) Page Directory Offset 10 bits 12 bits (1K entries) (4K pages) Page Entry Page Table Physical Memory Page + Offset CR3+Dir 12 November 2013 University of Virginia cs4414 34
  • 36. Page Table Entries CR3 Page Entry Page Table Page Directory 12 November 2013 20 bits: physical address of page 12 bits: flags user/kernel page write permission present University of Virginia cs4414 Physical Memory Page + Offset 35
  • 37. 386 Checkup 32-bit linear address CR3 Dir Page 10 bits (1K tables) Page Directory Offset 10 bits 12 bits (1K entries) (4K pages) 20 bits addr / 12 bits flags Page Table Physical Memory Page + Offset CR3+Dir How many pages do we need to store the page table? 12 November 2013 University of Virginia cs4414 36
  • 38. How slow is this???! Memory Page Paging Unit Physical Address Dir Linear Address Logical Address Segmentation Unit Offset CR3 Page Directory Page Table Physical Memory GDTR Global Descriptor Table 12 November 2013 University of Virginia cs4414 37
  • 39. Translation Lookaside Buffer (Cache) Memory Page Paging Unit Physical Address Dir Linear Address Logical Address Segmentation Unit Offset CR3 Page Directory Page Table Physical Memory GDTR Global Descriptor Table 12 November 2013 University of Virginia cs4414 38
  • 40. Page Fault CR3 Page Entry Page Table Page Directory 12 November 2013 Physical Memory 20 bits: physical address of page 12 bits: flags user/kernel page write permission present University of Virginia cs4414 39
  • 41. How common are page faults? 12 November 2013 University of Virginia cs4414 40
  • 42. top -o mem -stats pid,command,cpu,mem,mregion,vsize,faults 12 November 2013 University of Virginia cs4414 41
  • 43. Physical Memory: 9106M used (2553M “wired” – cannot be paged out) + 5090M unused = 14196M where is my missing ~2GB???! Virtual Memory: 594G (total) top -o mem -stats pid,command,cpu,mem,mregion,vsize,faults 12 November 2013 University of Virginia cs4414 42
  • 44. How expensive is a page fault? 12 November 2013 University of Virginia cs4414 43
  • 45. #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { char *s = (char *) malloc (1); int i= 0; while (1) { printf("%d: %xn", i, s[i]); i += 4; } } 12 November 2013 What will this program do? > ./a.out 0: 0 4: 0 8: 0 12: 0 …1033872: 0 1033876: 0 1033880: 0 1033884: 0 Segmentation fault: 11 University of Virginia cs4414 44
  • 46. Charge • Make progress on your projects: everyone should have a clear idea what you are doing now • Will post more details on next deliverable (design reviews) soon Challenge: write a program that takes N as an input and produces (nearly) exactly N page faults. 12 November 2013 University of Virginia cs4414 45