SlideShare a Scribd company logo
How to Build a Virtual Machine
Terence Parr
University of San Francisco
May 13, 2014
Preliminaries
• What is a VM?
– A simulated computer that runs a simple instruction set
(bytecodes).
• And why do we want one?
– We can’t execute high-level languages like Java directly on
the computer
– Rather than compile to machine code, we generate
bytecodes for a VM; much easier
– We get code portability to anywhere with same VM
– But bytecodes are slower than raw machine code
– Ultimately VMs today compile by codes to machine code
on the fly
Goal: Simulate a simple computer
0600 A4 44 MEMCPY LDY CNT+0 ;Set Y = CNT.L
0602 D0 05 BNE LOOP ;If CNT.L > 0, then loop
0604 A5 45 LDA CNT+1 ;If CNT.H > 0,
0606 D0 01 BNE LOOP ; then loop
0608 60 RTS ;Return
0609 B1 40 LOOP LDA (SRC),Y ;Load A from ((SRC)+Y)
060B 91 42 STA (DST),Y ;Store A to ((DST)+Y)
060D 88 DEY ;Decr CNT.L
060E D0 F9 BNE LOOP ;if CNT.L > 0, then loop
0610 E6 41 INC SRC+1 ;Incr SRC += $0100
0612 E6 43 INC DST+1 ;Incr DST += $0100
0614 88 DEY ;Decr CNT.L
0615 C6 45 DEC CNT+1 ;Decr CNT.H
0617 D0 F0 BNE LOOP ;If CNT.H > 0, then loop
0619 60 RTS ;Return
061A END
Machine code Assembly code
address
Here is a real CPU block diagram and code
Programming our VM
• Our bytecodes will be very regular and higher
level than machine instructions
• Each bytecode does a tiny bit of work
• Print 1+2:
ICONST 1
ICONST 2
IADD
PRINT
0000: iconst 1 stack=[ 1 ]
0002: iconst 2 stack=[ 1 2 ]
0004: iadd stack=[ 3 ]
0005: print stack=[ ]
0006: halt stack=[ ]
Stack code Execution trace
Our instruction set
iadd integer add (pop 2 operands, add, push result)
isub
imul
ilt integer less than
ieq
br addr branch to address
brt addr branch if true
brf addr
iconst value push integer constant
load addr load local
gload addr load global variable
store addr
gstore addr
print
pop toss out the top of stack
call addr, numArgscall addr, expect numArgs
ret return from function, expected result on top of stack
halt
Instruction format
• Code memory is 32-bit word addressable
• Bytecodes stored as ints but they are bytes
• Data memory is 32-bit word addressable
• Addresses are just integer numbers
• Operands are 32-bit integers
bytecode
bytecode operand
bytecode operand1 operand2
+0
+1
+2
Sample bytecodes
0000 9 1 ICONST 1
0002 9 2 ICONST 2
0004 1 IADD
0005 14 PRINT
Address Bytecodes Assembly bytecode
Our little stack machine
data
memory
code
memory
...
stack
sp
fp
ip
registers
fetch
decode
execute
Fetch: opcode = code[ip]
Decode: switch (opcode) {…}
Execute: stack[++sp] = stack[sp--] + stack[sp--] (iadd instruction)
CPU: Fetch, decode, execute cycle
Sample implementations
case BR :
ip = code[ip++];
break;
case IADD:
b = stack[sp--]; // 2nd opnd at top of stack
a = stack[sp--]; // 1st opnd 1 below top
stack[++sp] = a + b; // push result
break;
Implementing functions
• f();
• g(int x,y) {
int z;
z = f(x,y);
...
• return 9;
call f, 0
pop
load x (fp-4)
load y (fp-3)
call f, 2
store z (fp+1)
...
iconst 9
ret

More Related Content

What's hot

Linux Internals - Interview essentials - 1.0
Linux Internals - Interview essentials - 1.0Linux Internals - Interview essentials - 1.0
Linux Internals - Interview essentials - 1.0
Emertxe Information Technologies Pvt Ltd
 
Linux : The Common Mailbox Framework
Linux : The Common Mailbox FrameworkLinux : The Common Mailbox Framework
Linux : The Common Mailbox Framework
Mr. Vengineer
 
Arm v8 instruction overview android 64 bit briefing
Arm v8 instruction overview android 64 bit briefingArm v8 instruction overview android 64 bit briefing
Arm v8 instruction overview android 64 bit briefing
Merck Hung
 
Web applications support on AGL
Web applications support on AGLWeb applications support on AGL
Web applications support on AGL
Igalia
 
Building Mini Embedded Linux System for X86 Arch
Building Mini Embedded Linux System for X86 ArchBuilding Mini Embedded Linux System for X86 Arch
Building Mini Embedded Linux System for X86 Arch
Sherif Mousa
 
PHP unserialization vulnerabilities: What are we missing?
PHP unserialization vulnerabilities: What are we missing?PHP unserialization vulnerabilities: What are we missing?
PHP unserialization vulnerabilities: What are we missing?
Sam Thomas
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
shimosawa
 
Launch the First Process in Linux System
Launch the First Process in Linux SystemLaunch the First Process in Linux System
Launch the First Process in Linux System
Jian-Hong Pan
 
An Attacker's View of Serverless and GraphQL Apps - Abhay Bhargav - AppSec Ca...
An Attacker's View of Serverless and GraphQL Apps - Abhay Bhargav - AppSec Ca...An Attacker's View of Serverless and GraphQL Apps - Abhay Bhargav - AppSec Ca...
An Attacker's View of Serverless and GraphQL Apps - Abhay Bhargav - AppSec Ca...
Abhay Bhargav
 
What should a hacker know about WebDav?
What should a hacker know about WebDav?What should a hacker know about WebDav?
What should a hacker know about WebDav?
Mikhail Egorov
 
Troubleshooting Native Memory Leaks in Java Applications
Troubleshooting Native Memory Leaks in Java ApplicationsTroubleshooting Native Memory Leaks in Java Applications
Troubleshooting Native Memory Leaks in Java Applications
Poonam Bajaj Parhar
 
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device drivers
Houcheng Lin
 
Getting started with AGL using a Raspberry Pi
Getting started with AGL using a Raspberry PiGetting started with AGL using a Raspberry Pi
Getting started with AGL using a Raspberry Pi
Leon Anavi
 
What’s wrong with WebSocket APIs? Unveiling vulnerabilities in WebSocket APIs.
What’s wrong with WebSocket APIs? Unveiling vulnerabilities in WebSocket APIs.What’s wrong with WebSocket APIs? Unveiling vulnerabilities in WebSocket APIs.
What’s wrong with WebSocket APIs? Unveiling vulnerabilities in WebSocket APIs.
Mikhail Egorov
 
Pwning in c++ (basic)
Pwning in c++ (basic)Pwning in c++ (basic)
Pwning in c++ (basic)
Angel Boy
 
U boot-boot-flow
U boot-boot-flowU boot-boot-flow
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ BehaviourWAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
Soroush Dalili
 
Docker Security workshop slides
Docker Security workshop slidesDocker Security workshop slides
Docker Security workshop slides
Docker, Inc.
 
Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...
Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...
Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...
Linaro
 
Embedding Chromium into AGL demo platform with WAM
Embedding Chromium into AGL demo platform with WAMEmbedding Chromium into AGL demo platform with WAM
Embedding Chromium into AGL demo platform with WAM
Igalia
 

What's hot (20)

Linux Internals - Interview essentials - 1.0
Linux Internals - Interview essentials - 1.0Linux Internals - Interview essentials - 1.0
Linux Internals - Interview essentials - 1.0
 
Linux : The Common Mailbox Framework
Linux : The Common Mailbox FrameworkLinux : The Common Mailbox Framework
Linux : The Common Mailbox Framework
 
Arm v8 instruction overview android 64 bit briefing
Arm v8 instruction overview android 64 bit briefingArm v8 instruction overview android 64 bit briefing
Arm v8 instruction overview android 64 bit briefing
 
Web applications support on AGL
Web applications support on AGLWeb applications support on AGL
Web applications support on AGL
 
Building Mini Embedded Linux System for X86 Arch
Building Mini Embedded Linux System for X86 ArchBuilding Mini Embedded Linux System for X86 Arch
Building Mini Embedded Linux System for X86 Arch
 
PHP unserialization vulnerabilities: What are we missing?
PHP unserialization vulnerabilities: What are we missing?PHP unserialization vulnerabilities: What are we missing?
PHP unserialization vulnerabilities: What are we missing?
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
 
Launch the First Process in Linux System
Launch the First Process in Linux SystemLaunch the First Process in Linux System
Launch the First Process in Linux System
 
An Attacker's View of Serverless and GraphQL Apps - Abhay Bhargav - AppSec Ca...
An Attacker's View of Serverless and GraphQL Apps - Abhay Bhargav - AppSec Ca...An Attacker's View of Serverless and GraphQL Apps - Abhay Bhargav - AppSec Ca...
An Attacker's View of Serverless and GraphQL Apps - Abhay Bhargav - AppSec Ca...
 
What should a hacker know about WebDav?
What should a hacker know about WebDav?What should a hacker know about WebDav?
What should a hacker know about WebDav?
 
Troubleshooting Native Memory Leaks in Java Applications
Troubleshooting Native Memory Leaks in Java ApplicationsTroubleshooting Native Memory Leaks in Java Applications
Troubleshooting Native Memory Leaks in Java Applications
 
Arm device tree and linux device drivers
Arm device tree and linux device driversArm device tree and linux device drivers
Arm device tree and linux device drivers
 
Getting started with AGL using a Raspberry Pi
Getting started with AGL using a Raspberry PiGetting started with AGL using a Raspberry Pi
Getting started with AGL using a Raspberry Pi
 
What’s wrong with WebSocket APIs? Unveiling vulnerabilities in WebSocket APIs.
What’s wrong with WebSocket APIs? Unveiling vulnerabilities in WebSocket APIs.What’s wrong with WebSocket APIs? Unveiling vulnerabilities in WebSocket APIs.
What’s wrong with WebSocket APIs? Unveiling vulnerabilities in WebSocket APIs.
 
Pwning in c++ (basic)
Pwning in c++ (basic)Pwning in c++ (basic)
Pwning in c++ (basic)
 
U boot-boot-flow
U boot-boot-flowU boot-boot-flow
U boot-boot-flow
 
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ BehaviourWAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
 
Docker Security workshop slides
Docker Security workshop slidesDocker Security workshop slides
Docker Security workshop slides
 
Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...
Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...
Secure Boot on ARM systems – Building a complete Chain of Trust upon existing...
 
Embedding Chromium into AGL demo platform with WAM
Embedding Chromium into AGL demo platform with WAMEmbedding Chromium into AGL demo platform with WAM
Embedding Chromium into AGL demo platform with WAM
 

Viewers also liked

Static vs dynamic types
Static vs dynamic typesStatic vs dynamic types
Static vs dynamic types
Terence Parr
 
Antlr Conference Drools & Hibernate
Antlr Conference   Drools & HibernateAntlr Conference   Drools & Hibernate
Antlr Conference Drools & HibernateAlexandre Porcelli
 
How to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machineHow to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machine
Chun-Yu Wang
 
From SIP to WebRTC and vice versa
From SIP to WebRTC and vice versaFrom SIP to WebRTC and vice versa
From SIP to WebRTC and vice versa
Saúl Ibarra Corretgé
 
Dc machines electrical machines – i
Dc machines   electrical machines – iDc machines   electrical machines – i
Dc machines electrical machines – i
Soumyadeep Nag
 
Understanding the Dalvik Virtual Machine
Understanding the Dalvik Virtual MachineUnderstanding the Dalvik Virtual Machine
Understanding the Dalvik Virtual Machine
National Cheng Kung University
 
Dynamic analysis of dc machine
Dynamic analysis of dc machineDynamic analysis of dc machine
Dynamic analysis of dc machine
Ramesh Babu
 
Gas turbine-power-plant[1]
Gas turbine-power-plant[1]Gas turbine-power-plant[1]
Gas turbine-power-plant[1]
Manish Sadhu
 
Basics of Electrical Machines
Basics of Electrical MachinesBasics of Electrical Machines
Basics of Electrical Machines
maneesh001
 
Distributed Generation
Distributed Generation Distributed Generation
Distributed Generation
Anshul Shrivastava
 
Electrical Machines Notes
Electrical Machines NotesElectrical Machines Notes
Electrical Machines Notes
Akshansh Chaudhary
 
Electric machine
Electric machineElectric machine
Electric machine
ashok261084
 
power plant engineering
power plant engineeringpower plant engineering
power plant engineering
Gulfaraz alam
 
Electric power transmission system engineering 2nd edition by turan gonen
Electric power transmission system engineering  2nd edition by turan gonenElectric power transmission system engineering  2nd edition by turan gonen
Electric power transmission system engineering 2nd edition by turan gonenUmmi Robbania Mushthofa
 
Lecture 25 induction. faradays law. lenz law
Lecture 25   induction. faradays law. lenz lawLecture 25   induction. faradays law. lenz law
Lecture 25 induction. faradays law. lenz law
Albania Energy Association
 
Power plant engineering complete five unit vtu notes pdf download
Power plant engineering complete five unit vtu notes pdf downloadPower plant engineering complete five unit vtu notes pdf download
Power plant engineering complete five unit vtu notes pdf download
kiran555555
 
12.1 - Lenz's law
12.1  - Lenz's law12.1  - Lenz's law
12.1 - Lenz's law
simonandisa
 
Powerpoint presentation about lenz's law
Powerpoint presentation about lenz's lawPowerpoint presentation about lenz's law
Powerpoint presentation about lenz's lawrdelizoneyou
 
Data acquisition system (DAS)
Data acquisition system (DAS)Data acquisition system (DAS)
Data acquisition system (DAS)
Sumeet Patel
 

Viewers also liked (19)

Static vs dynamic types
Static vs dynamic typesStatic vs dynamic types
Static vs dynamic types
 
Antlr Conference Drools & Hibernate
Antlr Conference   Drools & HibernateAntlr Conference   Drools & Hibernate
Antlr Conference Drools & Hibernate
 
How to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machineHow to implement a simple dalvik virtual machine
How to implement a simple dalvik virtual machine
 
From SIP to WebRTC and vice versa
From SIP to WebRTC and vice versaFrom SIP to WebRTC and vice versa
From SIP to WebRTC and vice versa
 
Dc machines electrical machines – i
Dc machines   electrical machines – iDc machines   electrical machines – i
Dc machines electrical machines – i
 
Understanding the Dalvik Virtual Machine
Understanding the Dalvik Virtual MachineUnderstanding the Dalvik Virtual Machine
Understanding the Dalvik Virtual Machine
 
Dynamic analysis of dc machine
Dynamic analysis of dc machineDynamic analysis of dc machine
Dynamic analysis of dc machine
 
Gas turbine-power-plant[1]
Gas turbine-power-plant[1]Gas turbine-power-plant[1]
Gas turbine-power-plant[1]
 
Basics of Electrical Machines
Basics of Electrical MachinesBasics of Electrical Machines
Basics of Electrical Machines
 
Distributed Generation
Distributed Generation Distributed Generation
Distributed Generation
 
Electrical Machines Notes
Electrical Machines NotesElectrical Machines Notes
Electrical Machines Notes
 
Electric machine
Electric machineElectric machine
Electric machine
 
power plant engineering
power plant engineeringpower plant engineering
power plant engineering
 
Electric power transmission system engineering 2nd edition by turan gonen
Electric power transmission system engineering  2nd edition by turan gonenElectric power transmission system engineering  2nd edition by turan gonen
Electric power transmission system engineering 2nd edition by turan gonen
 
Lecture 25 induction. faradays law. lenz law
Lecture 25   induction. faradays law. lenz lawLecture 25   induction. faradays law. lenz law
Lecture 25 induction. faradays law. lenz law
 
Power plant engineering complete five unit vtu notes pdf download
Power plant engineering complete five unit vtu notes pdf downloadPower plant engineering complete five unit vtu notes pdf download
Power plant engineering complete five unit vtu notes pdf download
 
12.1 - Lenz's law
12.1  - Lenz's law12.1  - Lenz's law
12.1 - Lenz's law
 
Powerpoint presentation about lenz's law
Powerpoint presentation about lenz's lawPowerpoint presentation about lenz's law
Powerpoint presentation about lenz's law
 
Data acquisition system (DAS)
Data acquisition system (DAS)Data acquisition system (DAS)
Data acquisition system (DAS)
 

Similar to How to build a virtual machine

Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015
Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015
Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015
curryon
 
Arduino by yogesh t s'
Arduino by yogesh t s'Arduino by yogesh t s'
Arduino by yogesh t s'
tsyogesh46
 
Vectorization on x86: all you need to know
Vectorization on x86: all you need to knowVectorization on x86: all you need to know
Vectorization on x86: all you need to knowRoberto Agostino Vitillo
 
Java Jit. Compilation and optimization by Andrey Kovalenko
Java Jit. Compilation and optimization by Andrey KovalenkoJava Jit. Compilation and optimization by Andrey Kovalenko
Java Jit. Compilation and optimization by Andrey Kovalenko
Valeriia Maliarenko
 
Using Python3 to Build a Cloud Computing Service for my Superboard II
Using Python3 to Build a Cloud Computing Service for my Superboard IIUsing Python3 to Build a Cloud Computing Service for my Superboard II
Using Python3 to Build a Cloud Computing Service for my Superboard II
David Beazley (Dabeaz LLC)
 
Module_01.ppt
Module_01.pptModule_01.ppt
Module_01.ppt
AnandSonkuwar2
 
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...Positive Hack Days
 
Microcontroller part 4
Microcontroller part 4Microcontroller part 4
Microcontroller part 4
Keroles karam khalil
 
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege EscalationSemtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Kernel TLV
 
arduinoedit.pptx
arduinoedit.pptxarduinoedit.pptx
arduinoedit.pptx
rajalakshmi769433
 
Introduction to Microcontrollers
Introduction to MicrocontrollersIntroduction to Microcontrollers
Introduction to Microcontrollers
Venkata Sai Vamsi Penupothu
 
Chapter 1SyllabusCatalog Description Computer structu
Chapter 1SyllabusCatalog Description Computer structuChapter 1SyllabusCatalog Description Computer structu
Chapter 1SyllabusCatalog Description Computer structu
EstelaJeffery653
 
FPGA design with CλaSH
FPGA design with CλaSHFPGA design with CλaSH
FPGA design with CλaSHConrad Parker
 
LECTURE2 td 2 sue les theories de graphes
LECTURE2 td 2 sue les theories de graphesLECTURE2 td 2 sue les theories de graphes
LECTURE2 td 2 sue les theories de graphes
AhmedMahjoub15
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5PRADEEP
 
Instruction Set Architecture
Instruction Set ArchitectureInstruction Set Architecture
Instruction Set Architecture
Dilum Bandara
 
ISA.pptx
ISA.pptxISA.pptx
ISA.pptx
FarrukhMuneer2
 
Java on arm theory, applications, and workloads [dev5048]
Java on arm  theory, applications, and workloads [dev5048]Java on arm  theory, applications, and workloads [dev5048]
Java on arm theory, applications, and workloads [dev5048]
Aleksei Voitylov
 

Similar to How to build a virtual machine (20)

Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015
Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015
Bits of Advice for the VM Writer, by Cliff Click @ Curry On 2015
 
Arduino by yogesh t s'
Arduino by yogesh t s'Arduino by yogesh t s'
Arduino by yogesh t s'
 
Vectorization on x86: all you need to know
Vectorization on x86: all you need to knowVectorization on x86: all you need to know
Vectorization on x86: all you need to know
 
Rig nitc [autosaved] (copy)
Rig nitc [autosaved] (copy)Rig nitc [autosaved] (copy)
Rig nitc [autosaved] (copy)
 
Java Jit. Compilation and optimization by Andrey Kovalenko
Java Jit. Compilation and optimization by Andrey KovalenkoJava Jit. Compilation and optimization by Andrey Kovalenko
Java Jit. Compilation and optimization by Andrey Kovalenko
 
Using Python3 to Build a Cloud Computing Service for my Superboard II
Using Python3 to Build a Cloud Computing Service for my Superboard IIUsing Python3 to Build a Cloud Computing Service for my Superboard II
Using Python3 to Build a Cloud Computing Service for my Superboard II
 
Module_01.ppt
Module_01.pptModule_01.ppt
Module_01.ppt
 
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
 
Microcontroller part 4
Microcontroller part 4Microcontroller part 4
Microcontroller part 4
 
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege EscalationSemtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
 
arduinoedit.pptx
arduinoedit.pptxarduinoedit.pptx
arduinoedit.pptx
 
Programming arduino makeymakey
Programming arduino makeymakeyProgramming arduino makeymakey
Programming arduino makeymakey
 
Introduction to Microcontrollers
Introduction to MicrocontrollersIntroduction to Microcontrollers
Introduction to Microcontrollers
 
Chapter 1SyllabusCatalog Description Computer structu
Chapter 1SyllabusCatalog Description Computer structuChapter 1SyllabusCatalog Description Computer structu
Chapter 1SyllabusCatalog Description Computer structu
 
FPGA design with CλaSH
FPGA design with CλaSHFPGA design with CλaSH
FPGA design with CλaSH
 
LECTURE2 td 2 sue les theories de graphes
LECTURE2 td 2 sue les theories de graphesLECTURE2 td 2 sue les theories de graphes
LECTURE2 td 2 sue les theories de graphes
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
 
Instruction Set Architecture
Instruction Set ArchitectureInstruction Set Architecture
Instruction Set Architecture
 
ISA.pptx
ISA.pptxISA.pptx
ISA.pptx
 
Java on arm theory, applications, and workloads [dev5048]
Java on arm  theory, applications, and workloads [dev5048]Java on arm  theory, applications, and workloads [dev5048]
Java on arm theory, applications, and workloads [dev5048]
 

Recently uploaded

GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
Vlad Stirbu
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 

Recently uploaded (20)

GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 

How to build a virtual machine

  • 1. How to Build a Virtual Machine Terence Parr University of San Francisco May 13, 2014
  • 2. Preliminaries • What is a VM? – A simulated computer that runs a simple instruction set (bytecodes). • And why do we want one? – We can’t execute high-level languages like Java directly on the computer – Rather than compile to machine code, we generate bytecodes for a VM; much easier – We get code portability to anywhere with same VM – But bytecodes are slower than raw machine code – Ultimately VMs today compile by codes to machine code on the fly
  • 3. Goal: Simulate a simple computer 0600 A4 44 MEMCPY LDY CNT+0 ;Set Y = CNT.L 0602 D0 05 BNE LOOP ;If CNT.L > 0, then loop 0604 A5 45 LDA CNT+1 ;If CNT.H > 0, 0606 D0 01 BNE LOOP ; then loop 0608 60 RTS ;Return 0609 B1 40 LOOP LDA (SRC),Y ;Load A from ((SRC)+Y) 060B 91 42 STA (DST),Y ;Store A to ((DST)+Y) 060D 88 DEY ;Decr CNT.L 060E D0 F9 BNE LOOP ;if CNT.L > 0, then loop 0610 E6 41 INC SRC+1 ;Incr SRC += $0100 0612 E6 43 INC DST+1 ;Incr DST += $0100 0614 88 DEY ;Decr CNT.L 0615 C6 45 DEC CNT+1 ;Decr CNT.H 0617 D0 F0 BNE LOOP ;If CNT.H > 0, then loop 0619 60 RTS ;Return 061A END Machine code Assembly code address Here is a real CPU block diagram and code
  • 4. Programming our VM • Our bytecodes will be very regular and higher level than machine instructions • Each bytecode does a tiny bit of work • Print 1+2: ICONST 1 ICONST 2 IADD PRINT 0000: iconst 1 stack=[ 1 ] 0002: iconst 2 stack=[ 1 2 ] 0004: iadd stack=[ 3 ] 0005: print stack=[ ] 0006: halt stack=[ ] Stack code Execution trace
  • 5. Our instruction set iadd integer add (pop 2 operands, add, push result) isub imul ilt integer less than ieq br addr branch to address brt addr branch if true brf addr iconst value push integer constant load addr load local gload addr load global variable store addr gstore addr print pop toss out the top of stack call addr, numArgscall addr, expect numArgs ret return from function, expected result on top of stack halt
  • 6. Instruction format • Code memory is 32-bit word addressable • Bytecodes stored as ints but they are bytes • Data memory is 32-bit word addressable • Addresses are just integer numbers • Operands are 32-bit integers bytecode bytecode operand bytecode operand1 operand2 +0 +1 +2
  • 7. Sample bytecodes 0000 9 1 ICONST 1 0002 9 2 ICONST 2 0004 1 IADD 0005 14 PRINT Address Bytecodes Assembly bytecode
  • 8. Our little stack machine data memory code memory ... stack sp fp ip registers fetch decode execute Fetch: opcode = code[ip] Decode: switch (opcode) {…} Execute: stack[++sp] = stack[sp--] + stack[sp--] (iadd instruction)
  • 9. CPU: Fetch, decode, execute cycle
  • 10. Sample implementations case BR : ip = code[ip++]; break; case IADD: b = stack[sp--]; // 2nd opnd at top of stack a = stack[sp--]; // 1st opnd 1 below top stack[++sp] = a + b; // push result break;
  • 11. Implementing functions • f(); • g(int x,y) { int z; z = f(x,y); ... • return 9; call f, 0 pop load x (fp-4) load y (fp-3) call f, 2 store z (fp+1) ... iconst 9 ret