<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Draevich Matthew</title>
    <description>The latest articles on DEV Community by Draevich Matthew (@mdraevich).</description>
    <link>https://dev.to/mdraevich</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1015880%2F4d640ae6-fbeb-4199-879b-85df937d6978.jpeg</url>
      <title>DEV Community: Draevich Matthew</title>
      <link>https://dev.to/mdraevich</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mdraevich"/>
    <language>en</language>
    <item>
      <title>Virtualization &amp; Emulation explained in a top-down fashion</title>
      <dc:creator>Draevich Matthew</dc:creator>
      <pubDate>Sun, 29 Jan 2023 13:17:00 +0000</pubDate>
      <link>https://dev.to/mdraevich/virtualization-emulation-explained-in-a-top-down-fashion-2of8</link>
      <guid>https://dev.to/mdraevich/virtualization-emulation-explained-in-a-top-down-fashion-2of8</guid>
      <description>&lt;p&gt;It took much time for me to find and comprehend the difference between virtualization &amp;amp; emulation. Since the understanding may require a broad knowledge of how software and hardware are coupled together, I decided to describe all components you need in a single article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Case #1 — Running OS on a bare-metal server
&lt;/h2&gt;

&lt;p&gt;It’s important to know how the things are working in a traditional way.&lt;br&gt;
So, we'll discuss in a top-down fashion how the following components are coupled together:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Hardware&lt;/strong&gt; — you can’t run anything without CPU &amp;amp; RAM.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OS&lt;/strong&gt; — a set of processes that helps us to manage &amp;amp; utilize the power of hardware.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Application&lt;/strong&gt; — there is nothing useful if OS isn’t able to run applications.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All the magic begins with a CPU — it defines a set of instructions available for execution. Instruction can be simply represented as an action with arguments. Available instructions are defined by CPU architecture. Thus, instructions for &lt;strong&gt;ARMv7&lt;/strong&gt; and &lt;strong&gt;x86_64&lt;/strong&gt; will be different.&lt;/p&gt;

&lt;p&gt;When it comes to OS it's worth to describe it as a set of processes which are responsible for managing &amp;amp; utilizing hardware (e.g. screen updates, keyboard events, network data sending &amp;amp; receiving). Under the hood all the processes are described as an order of instructions that a particular CPU understands.&lt;/p&gt;

&lt;p&gt;Finally, the installed application is also an order of instructions that a particular CPU understands.&lt;/p&gt;

&lt;p&gt;We're ready to state that instructions can be different from the permissions perspective. In general we divide available instructions to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;privileged&lt;/strong&gt; — OS-level instructions (to get all keyboard events as an example);&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;unprivileged&lt;/strong&gt; — application-level instructions (to sum up two integers as an example).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The permission level of an instruction is defined by the means of &lt;a href="https://en.wikipedia.org/wiki/Protection_ring" rel="noopener noreferrer"&gt;protections rings&lt;/a&gt;. Permissions in turn allow us to prevent a single application seizing the control over the hardware in a selfish manner.&lt;/p&gt;

&lt;p&gt;But what if an application needs to access a data that requires execution of a privileged instruction? As an example, sending a network packet or catching some keyboard events?&lt;/p&gt;

&lt;p&gt;As far as we know an application is not allowed to execute any privileged instruction... That’s a point where interaction with an OS comes into play — an application is able to interact with OS by API calls to request the data.&lt;/p&gt;

&lt;p&gt;Let’s display it with a scheme:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx2z30hms6u9n5jqmtuda.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx2z30hms6u9n5jqmtuda.jpg" alt="Case #1 — Running OS on a bare-metal server" width="668" height="346"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Case #2 — Emulation approach
&lt;/h2&gt;

&lt;p&gt;The first question I would like to speak about is the problem that emulation solves.&lt;/p&gt;

&lt;p&gt;Generally speaking we need emulation when an application executes instructions the underlying hardware doesn't support. A quick example would be running &lt;strong&gt;ARMv7&lt;/strong&gt; application on top of &lt;strong&gt;x86_64&lt;/strong&gt; platform.&lt;/p&gt;

&lt;p&gt;In general this issue is resolved by placing a kind of translator between an application and CPU to translate instructions — the translator would be referred as an &lt;em&gt;interpretator&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Pay great attention that an emulated application is deployed as a general user application so it's not allowed to execute any privileged instruction. The solution for that is simply converting privileged instructions to API calls to the underlying OS.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Emulation enables you to emulate either a single application or an entire OS.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's take a look how an entire OS can be emulated:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fib21lx5di2dj02y09jsj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fib21lx5di2dj02y09jsj.jpg" alt="Case #2 — Emulation" width="681" height="603"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Case #3 — Full virtualization approach
&lt;/h2&gt;

&lt;p&gt;What’s the problem virtualization solves?&lt;/p&gt;

&lt;p&gt;Virtualization allows us to run a separate OS (referred as a virtual machine) as a general user application. Besides, a virtual machine is allowed to execute instructions &lt;strong&gt;directly&lt;/strong&gt; on hardware CPU without any &lt;em&gt;interpretator&lt;/em&gt; between. That's dramatically increases performance however a virtual machine should use supported CPU instructions. &lt;/p&gt;

&lt;p&gt;You must ask: “&lt;em&gt;whoa, wait a minute, is a virtual machine allowed to execute privileged instructions in this case?&lt;/em&gt;”. Yeah, it’s a good question to ask.&lt;/p&gt;

&lt;p&gt;To answer the question it’s worth to know how CPU behaves if a privileged instruction is executed from user space (where a virtual machine sits). &lt;br&gt;
If CPU gets a privileged instruction from user space it will &lt;strong&gt;not&lt;/strong&gt; silently drop it. Instead, it will raise an exception to kernel space (where underlying OS sits). Underlying OS is now responsible for handling &amp;amp; resolving the exceptions. The described technique is called &lt;strong&gt;Trap-and-Emulate&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let’s display it with a scheme:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqxlnkhfa45zr3gh25su1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqxlnkhfa45zr3gh25su1.jpg" alt="Case #3 — Full virtualization" width="718" height="671"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Case #4 — Paravirtualization approach
&lt;/h2&gt;

&lt;p&gt;Finally, let’s take a look at paravirtualization.&lt;/p&gt;

&lt;p&gt;Trap-and-Emulate technique is a working solution but its performance leaves something to be desired due to unnecessary exceptions. Paravirtualization comes into play to provide more performance by eliminating the need of exceptions.&lt;/p&gt;

&lt;p&gt;Paravirtualization implies that you modify virtual machine OS to make it directly execute API calls to the underlying OS instead of executing privileged instructions on a CPU. But once again — paravirtualization requires you to modify virtual machine OS by installing additional software what can be a great limitation.&lt;/p&gt;

&lt;p&gt;A scheme illustrating what I’ve just said:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftgdn2pbdzzb161iyenzl.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftgdn2pbdzzb161iyenzl.jpg" alt="Case #4 — Paravirtualization" width="718" height="561"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  To sum up
&lt;/h2&gt;

&lt;p&gt;In this article we've completed a journey to understand the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what are CPU instructions and how they can be different;&lt;/li&gt;
&lt;li&gt;how OS and applications are coupled together and how they use CPU;&lt;/li&gt;
&lt;li&gt;how emulation allows you to run applications on unsupported hardware;&lt;/li&gt;
&lt;li&gt;how virtualization overtakes emulation in performance and what paravirtualization is.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I really hope you'll find this article useful.&lt;br&gt;
Let me know if you have any recommendations how to make it better.&lt;/p&gt;

&lt;p&gt;Have a nice day!&lt;/p&gt;

</description>
      <category>virtualization</category>
      <category>emulation</category>
      <category>architecture</category>
      <category>virtualmachine</category>
    </item>
  </channel>
</rss>
