SlideShare una empresa de Scribd logo
1 de 46
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

Más contenido relacionado

La actualidad más candente

Profiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf ToolsProfiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf Tools
emBO_Conference
 
Alto Desempenho com Java
Alto Desempenho com JavaAlto Desempenho com Java
Alto Desempenho com Java
codebits
 

La actualidad más candente (20)

Managing Memory
Managing MemoryManaging Memory
Managing Memory
 
Making a Process (Virtualizing Memory)
Making a Process (Virtualizing Memory)Making a Process (Virtualizing Memory)
Making a Process (Virtualizing Memory)
 
SSL Failing, Sharing, and Scheduling
SSL Failing, Sharing, and SchedulingSSL Failing, Sharing, and Scheduling
SSL Failing, Sharing, and Scheduling
 
The Internet
The InternetThe Internet
The Internet
 
Scheduling in Linux and Web Servers
Scheduling in Linux and Web ServersScheduling in Linux and Web Servers
Scheduling in Linux and Web Servers
 
Gash Has No Privileges
Gash Has No PrivilegesGash Has No Privileges
Gash Has No Privileges
 
Once Upon a Process
Once Upon a ProcessOnce Upon a Process
Once Upon a Process
 
Storage
StorageStorage
Storage
 
Kernel Recipes 2019 - Formal modeling made easy
Kernel Recipes 2019 - Formal modeling made easyKernel Recipes 2019 - Formal modeling made easy
Kernel Recipes 2019 - Formal modeling made easy
 
Microkernels and Beyond
Microkernels and BeyondMicrokernels and Beyond
Microkernels and Beyond
 
Mutual Exclusion
Mutual ExclusionMutual Exclusion
Mutual Exclusion
 
Lec09 nbody-optimization
Lec09 nbody-optimizationLec09 nbody-optimization
Lec09 nbody-optimization
 
Flash! (Modern File Systems)
Flash! (Modern File Systems)Flash! (Modern File Systems)
Flash! (Modern File Systems)
 
Lec05 buffers basic_examples
Lec05 buffers basic_examplesLec05 buffers basic_examples
Lec05 buffers basic_examples
 
Lec11 timing
Lec11 timingLec11 timing
Lec11 timing
 
Kernel Recipes 2018 - KernelShark 1.0; What's new and what's coming - Steven ...
Kernel Recipes 2018 - KernelShark 1.0; What's new and what's coming - Steven ...Kernel Recipes 2018 - KernelShark 1.0; What's new and what's coming - Steven ...
Kernel Recipes 2018 - KernelShark 1.0; What's new and what's coming - Steven ...
 
Lec07 threading hw
Lec07 threading hwLec07 threading hw
Lec07 threading hw
 
Profiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf ToolsProfiling your Applications using the Linux Perf Tools
Profiling your Applications using the Linux Perf Tools
 
Alto Desempenho com Java
Alto Desempenho com JavaAlto Desempenho com Java
Alto Desempenho com Java
 
Lec13 multidevice
Lec13 multideviceLec13 multidevice
Lec13 multidevice
 

Destacado

Virtual memory
Virtual memoryVirtual memory
Virtual memory
Mohd Arif
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
vatsaanadi
 
39 virtual memory
39 virtual memory39 virtual memory
39 virtual memory
myrajendra
 
Virtual Memory vs Cache Memory
Virtual Memory vs Cache MemoryVirtual Memory vs Cache Memory
Virtual Memory vs Cache Memory
Ashik Iqbal
 

Destacado (20)

Ch09
Ch09Ch09
Ch09
 
virtual memory
virtual memoryvirtual memory
virtual memory
 
Ch10: Virtual Memory
Ch10: Virtual MemoryCh10: Virtual Memory
Ch10: Virtual Memory
 
Os8
Os8Os8
Os8
 
CPU HISTORY MUKUND
CPU HISTORY MUKUNDCPU HISTORY MUKUND
CPU HISTORY MUKUND
 
Virtual Memory
Virtual MemoryVirtual Memory
Virtual Memory
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
 
basics of virtual memory
basics of virtual memorybasics of virtual memory
basics of virtual memory
 
Virtual Memory
Virtual MemoryVirtual Memory
Virtual Memory
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
 
Vm
VmVm
Vm
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
 
39 virtual memory
39 virtual memory39 virtual memory
39 virtual memory
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
 
virtual memory
virtual memoryvirtual memory
virtual memory
 
Operating Systems - Virtual Memory
Operating Systems - Virtual MemoryOperating Systems - Virtual Memory
Operating Systems - Virtual Memory
 
Virtual memory managment
Virtual memory managmentVirtual memory managment
Virtual memory managment
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
 
Virtual Memory vs Cache Memory
Virtual Memory vs Cache MemoryVirtual Memory vs Cache Memory
Virtual Memory vs Cache Memory
 
Operating System (Scheduling, Input and Output Management, Memory Management,...
Operating System (Scheduling, Input and Output Management, Memory Management,...Operating System (Scheduling, Input and Output Management, Memory Management,...
Operating System (Scheduling, Input and Output Management, Memory Management,...
 

Similar a Virtual Memory (Making a Process)

Spark & Cassandra at DataStax Meetup on Jan 29, 2015
Spark & Cassandra at DataStax Meetup on Jan 29, 2015 Spark & Cassandra at DataStax Meetup on Jan 29, 2015
Spark & Cassandra at DataStax Meetup on Jan 29, 2015
Sameer Farooqui
 
Cloud, Distributed, Embedded: Erlang in the Heterogeneous Computing World
Cloud, Distributed, Embedded: Erlang in the Heterogeneous Computing WorldCloud, Distributed, Embedded: Erlang in the Heterogeneous Computing World
Cloud, Distributed, Embedded: Erlang in the Heterogeneous Computing World
Omer Kilic
 
Dataiku Flow and dctc - Berlin Buzzwords
Dataiku Flow and dctc - Berlin BuzzwordsDataiku Flow and dctc - Berlin Buzzwords
Dataiku Flow and dctc - Berlin Buzzwords
Dataiku
 
survey_of_matrix_for_simulation
survey_of_matrix_for_simulationsurvey_of_matrix_for_simulation
survey_of_matrix_for_simulation
Jon Hand
 

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

Storage Systems
Storage SystemsStorage Systems
Storage Systems
 
Ashwin_Thesis
Ashwin_ThesisAshwin_Thesis
Ashwin_Thesis
 
Spark & Cassandra at DataStax Meetup on Jan 29, 2015
Spark & Cassandra at DataStax Meetup on Jan 29, 2015 Spark & Cassandra at DataStax Meetup on Jan 29, 2015
Spark & Cassandra at DataStax Meetup on Jan 29, 2015
 
Cloud, Distributed, Embedded: Erlang in the Heterogeneous Computing World
Cloud, Distributed, Embedded: Erlang in the Heterogeneous Computing WorldCloud, Distributed, Embedded: Erlang in the Heterogeneous Computing World
Cloud, Distributed, Embedded: Erlang in the Heterogeneous Computing World
 
Spark Summit East 2015 Advanced Devops Student Slides
Spark Summit East 2015 Advanced Devops Student SlidesSpark Summit East 2015 Advanced Devops Student Slides
Spark Summit East 2015 Advanced Devops Student Slides
 
What the &~#@&lt;!? (Memory Management in Rust)
What the &~#@&lt;!? (Memory Management in Rust)What the &~#@&lt;!? (Memory Management in Rust)
What the &~#@&lt;!? (Memory Management in Rust)
 
Graph Analytics with ArangoDB
Graph Analytics with ArangoDBGraph Analytics with ArangoDB
Graph Analytics with ArangoDB
 
SEMLA_logging_infra
SEMLA_logging_infraSEMLA_logging_infra
SEMLA_logging_infra
 
Preserving Computer-Aided Design, Digital Preservation Coalition Report
Preserving Computer-Aided Design, Digital Preservation Coalition ReportPreserving Computer-Aided Design, Digital Preservation Coalition Report
Preserving Computer-Aided Design, Digital Preservation Coalition Report
 
Dataiku Flow and dctc - Berlin Buzzwords
Dataiku Flow and dctc - Berlin BuzzwordsDataiku Flow and dctc - Berlin Buzzwords
Dataiku Flow and dctc - Berlin Buzzwords
 
Bosco r users2013
Bosco r users2013Bosco r users2013
Bosco r users2013
 
Tuning and Debugging in Apache Spark
Tuning and Debugging in Apache SparkTuning and Debugging in Apache Spark
Tuning and Debugging in Apache Spark
 
How Apache Spark fits into the Big Data landscape
How Apache Spark fits into the Big Data landscapeHow Apache Spark fits into the Big Data landscape
How Apache Spark fits into the Big Data landscape
 
survey_of_matrix_for_simulation
survey_of_matrix_for_simulationsurvey_of_matrix_for_simulation
survey_of_matrix_for_simulation
 
11. From Hadoop to Spark 1:2
11. From Hadoop to Spark 1:211. From Hadoop to Spark 1:2
11. From Hadoop to Spark 1:2
 
Introduction to Spark Training
Introduction to Spark TrainingIntroduction to Spark Training
Introduction to Spark Training
 
Intro to Spark development
 Intro to Spark development  Intro to Spark development
Intro to Spark development
 
Understanding Hadoop
Understanding HadoopUnderstanding Hadoop
Understanding Hadoop
 
Apache Spark
Apache SparkApache Spark
Apache Spark
 
Data Analytics and Machine Learning: From Node to Cluster on ARM64
Data Analytics and Machine Learning: From Node to Cluster on ARM64Data Analytics and Machine Learning: From Node to Cluster on ARM64
Data Analytics and Machine Learning: From Node to Cluster on ARM64
 

Más de David Evans

Más de David Evans (20)

Cryptocurrency Jeopardy!
Cryptocurrency Jeopardy!Cryptocurrency Jeopardy!
Cryptocurrency Jeopardy!
 
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for CypherpunksTrick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
 
Hidden Services, Zero Knowledge
Hidden Services, Zero KnowledgeHidden Services, Zero Knowledge
Hidden Services, Zero Knowledge
 
Anonymity in Bitcoin
Anonymity in BitcoinAnonymity in Bitcoin
Anonymity in Bitcoin
 
Midterm Confirmations
Midterm ConfirmationsMidterm Confirmations
Midterm Confirmations
 
Scripting Transactions
Scripting TransactionsScripting Transactions
Scripting Transactions
 
How to Live in Paradise
How to Live in ParadiseHow to Live in Paradise
How to Live in Paradise
 
Bitcoin Script
Bitcoin ScriptBitcoin Script
Bitcoin Script
 
Mining Economics
Mining EconomicsMining Economics
Mining Economics
 
Mining
MiningMining
Mining
 
The Blockchain
The BlockchainThe Blockchain
The Blockchain
 
Becoming More Paranoid
Becoming More ParanoidBecoming More Paranoid
Becoming More Paranoid
 
Asymmetric Key Signatures
Asymmetric Key SignaturesAsymmetric Key Signatures
Asymmetric Key Signatures
 
Introduction to Cryptography
Introduction to CryptographyIntroduction to Cryptography
Introduction to Cryptography
 
Class 1: What is Money?
Class 1: What is Money?Class 1: What is Money?
Class 1: What is Money?
 
Multi-Party Computation for the Masses
Multi-Party Computation for the MassesMulti-Party Computation for the Masses
Multi-Party Computation for the Masses
 
Proof of Reserve
Proof of ReserveProof of Reserve
Proof of Reserve
 
Silk Road
Silk RoadSilk Road
Silk Road
 
Blooming Sidechains!
Blooming Sidechains!Blooming Sidechains!
Blooming Sidechains!
 
Useful Proofs of Work, Permacoin
Useful Proofs of Work, PermacoinUseful Proofs of Work, Permacoin
Useful Proofs of Work, Permacoin
 

Último

Último (20)

Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

Virtual Memory (Making a Process)

  • 1.
  • 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