---
# System prepended metadata

title: 'Lecture 16: Virtual Machines'
tags: [Operating Systems Principles, CS111]

---

# Lecture 16: Virtual Machines

# Slide Notes

recall virtual machines is essentailly an abstraction of real hardware to make it look like we have many of that hardware (ex multiple CPUs)

we do something in software to make it look like we have multiple computers. The virtual machine must appear to apps and users to be a real machine.

We want to have virtual machines for several reasons:
- fault isolation, better security, to use a different operating system, to provide better controlled sharing of the hardware

## Fault isolation

operating systems must never crash, more specifically it should not take down the entire machine, just the virtaul machine. 

This allows for our correctness requirements to be relaxed. 

Similar advantages for faults that could damage devices

## Better Security

The OS is supposed to provide security for processes.

But the OS also provides shared resources such as the file system and IPC channels

A virtual machine need not see the real shared resource so processes in other virtual machines are harder to reach and possibly damage

## Using a Different Operating System

Let’s say you’re running Windows but you want to run a Linux executable. Windows has one system call interface, Linux has a
different one so system calls from your Linux executable won’t work on Windows

But if you have a virtual machine running Linux on top of the real machine running Windows . . . Now your application can run fine Assuming you get the virtualization right 

## Sharing A Machine's Resources

In principle, an OS can control how to share resources among processes But actually guaranteeing a particular allocation of resources is hard

It’s easier to guarantee an entire virtual machine gets a set allocation of resources So the processes running in it will not steal resources from the other virtual machines
- A very big deal for cloud computing

## How Do We Run Virtual Machines

Easiest if the virtual and real machine use the same ISA (Instrucution Set Architechture)

Basically, rely on limited direct execution, run as much VM activity directly on the CPU as possible (only trap when neccessary)

### The Hypervisor

Also known as the virtual Machine Monitor (VMM) which is a controller that handles all virtual machines running on a real machine. 

When necessary, it traps from the virtual machine to the VMM and it performs whatever magic is necessary. Thne returns to limited direct execution.

trapping is only necessary whenever the VM does something privileged.

The initial system call instruction will trp to the VMM which will typically forward it to the VM's OS. But subsequent privileged operations trap back to the VMM. 

![Screenshot 2024-05-28 at 3.04.55 PM](https://hackmd.io/_uploads/B1GfSA7VC.png)

![Screenshot 2024-05-28 at 3.05.54 PM](https://hackmd.io/_uploads/BJ6rBCXER.png)

![Screenshot 2024-05-28 at 3.06.23 PM](https://hackmd.io/_uploads/SJqwSCmVA.png)

![Screenshot 2024-05-28 at 3.06.55 PM](https://hackmd.io/_uploads/ByqtHCm40.png)

![Screenshot 2024-05-28 at 3.07.15 PM](https://hackmd.io/_uploads/rkbsBRm4R.png)

if the VMM is going to do the instruction why not just run A with privilege so it can do its own instructions.
- e.g. it could seize all the memory 
- we want to prevent OS and VM from hurting eachother

The VMM might decide not to do the instructions or it might block VM A and run VM B for a while instead. 

The key point is the VMM controls what happens even though the OS in the VM thinks it is in control.

### The Core of the Problem
Os A thinks it is in control and believes it is providing segregated virtual memories to APP 1 and App 2. 

The key technology for doing so is managing page tables and CPU registers pointing to them. 

But OS A has no control over those registers. THe VMM does but it knows nothing of the page tables OS A "controls"

### How To Virtualize Memory

The virtual OS thinks it has physical memory addresses where it provides virtual memory addresses to its processes

The VMM has machine addresses which translates to physical addresses within a single VM while still using the same paging hardware

![Screenshot 2024-05-28 at 3.25.10 PM](https://hackmd.io/_uploads/BJQRKC7ER.png)

![Screenshot 2024-05-28 at 3.25.50 PM](https://hackmd.io/_uploads/HkYgcAmNA.png)

![Screenshot 2024-05-28 at 3.26.03 PM](https://hackmd.io/_uploads/HJ8bqCXN0.png)

### Some Implications

TLB misses are much more expensive sunce well be moving back and forth from privileged mode to unprivileged, paying overhead costs each time, and well run more systems code. 

Well also need extra paging data strucutres in the VMM which involves even more overhead.

This virtual machines are this likely to suffer performance penalties.

### Making VMs Perform Better

we can improve performance by adding special hardware. FOr example some CPUs have features to make issues of virtualizing the CPU and memory cheaper

**Paravirualization**: The basic VM approach assumes the guest OSes in VM does not know about virtualization. If you make some changes to those OSes, they can help make virtualization cheaper. 

## Virtual Machines and Cloud Computing

Cloud computing is about sharing hardware amoung multiple customers. The cloud provider sells/rents computing services to customers which handles all difficult issues for them os they can just run their applications.

Cloud providers need lots of costomers, to make money which implies they need losts of hardware. 

### The Cloud Enviroment

A warehouse fill of vast number of machines packed tightly into racks all connected by high speed internal networks and connect to the internet to allow customers remote access. 

The expectation is that the environmnent will run applications for many separate customers at a time, many of which might require multiple computers to run properly with strong guarantees of isolation between customers. 

The cloud provider makes the most money by making the most efficient use of the hardware. 

Often a customer does not need the full power of a machine. But they do need strong isolation which can be provided by virtual machines.

### So ...

this means you can run everyone in a virtual machine. Some customers have many virtual machines to handle their big jobs. 

Some customers' virtual machines share physical machines with other custormers' VMs

Customers' workloads fluctuate so you want the most efficient packing of VMs onto physical machines possible to maximize profits. 

### How To Efficiently Place VMs

There are many physical nodes and many more Vms that often reduces to a **bin packing algorithm** which tends to be NP-hard. 
- where n may depend on the number of servers and/or VMs considered
- the more factors considered, the harder to solve

### VMs Aren't Just For Cloud Computing

As you should know, since your projects use them. They allow experimentation not easily performed on real hardware. 

They allow basic servers to safely divide their resources and they allow greater flexibility in the software your computer can run. 

# Chapter B Virtual Machine Monitors

[Text Here](https://pages.cs.wisc.edu/~remzi/OSTEP/vmm-intro.pdf)

Years ago, IBM sold expensive mainframes to large organizations, and a problem arose: what if the organization wanted to run different operating systems on the machine at the same time? Some applications had been developed on one OS, and some on others, and thus the problem.

As a solution, IBM introduced yet another level of indirection in the form of a **virtual machine monitor (VMM)** (also called a **hypervisor**).

## Motivation: Why VMMs

virtualization enables an administrator to consolidate multiple OSes onto fewer hardware platforms, and thus lower costs and ease administration.

Another reason is testing and debugging. While developers write code on one main platform, they often want to debug and test it on the many different platforms that they deploy the software to in the field

## Virtualizing the CPU

To run a **virtual machine** on top of a virtual machine monitor, the basic technique that is used is **limited direct execution** 

we wish to multiplex between two virtual machines, that is, between two OSes and their respective applications. In a manner quite similar to an operating system switching between running processes (a **context switch**), a virtual machine monitor must perform a **machine switch** between running virtual machines.

#### This is pretty much the Same as lecture so I'll only note what was not really said. 

recall VMM will handle only privleged instructions and the OS will handle the non privleged instructions 

![Screenshot 2024-05-29 at 1.54.06 PM](https://hackmd.io/_uploads/rJclIGHN0.png)

![Screenshot 2024-05-29 at 1.55.42 PM](https://hackmd.io/_uploads/B1K88GSVC.png)

![Screenshot 2024-05-29 at 1.55.57 PM](https://hackmd.io/_uploads/HJdPUMSVR.png)

