# Setup
1. Download QEMU source
```
wget https://www.cse.iitb.ac.in/~puru//courses/resources/virtio/qemu-virtio.tar.gz
tar -xvzf qemu-virtio.tar.gz 

# qemu git repo should be on the cs695-dev branch by default.
```

2. Apply patch to add support for virtio-demo-pci
```
git apply --stat ../virtio-demo-patch.patch         # generate summary of incoming changes
git apply ../virtio-demo-patch.patch                # apply changes, unstaged
git add --all                                       # stage patch
git commit -m "added support for virtio-demo-pci"   # commit the patch
```

3. Configure QEMU to prepare for build
```
# prepare for an out-of-tree build
cd qemu
mkdir build-0           
cd build-0

# Debug options are help creating detailed qemu traces
# --target-list, --enable-kvm, --enable-slirp are required.
../configure --target-list=x86_64-softmmu --enable-debug-mutex --enable-debug-remap --enable-debug-graph-lock --enable-cfi-debug  --enable-debug-tcg  --enable-kvm --enable-slirp
```
Install additional packages that are required
by the configuration.

4. Build QEMU
```
make # approx 10-15 mins
make -j # 1-2 mins
```
Ensure `configure` and `make` are both called from your qemu/build-0 directory. Executables will 
be stored here.

5. Start a VM with the QEMU Monitor
```
# From parent directory
/qemu/build-0/qemu-system-x86_64 -device help | grep virtio-demo-pci # verify device support

# Start VM
```
./qemu/build-0/qemu-system-x86_64 -enable-kvm -smp 8 -m 4096 -nic user,model=virtio,hostfwd=tcp::2222-:22  -drive file=virtio-demo.qcow2,media=disk,if=virtio -monitor stdio
```

6. SSH onto the VM for ease-of-use
```
ssh -p 2222 vm@localhost
```

7. (VM) Build and load the virtio-demo frontend driver
```
cd $HOME/virtio-demo/driver
make
sudo insmod virtio-demo.ko
```

8. Attach demo device via QEMU Monitor
```
(qemu) device_add virtio-demo-pci,id=v0,disable-legacy=on
```
Observe dmesg in VM after attaching the device.

9. (VM) Build and run the user test app
```
cd $HOME/virtio-demo/user
make
./test-virtio-demo
```

# Useful Monitor Commands
- `info virtio`, followed by
- `info virtio-status <device_machine_str>`
- `info virtio-queue-status <device_machine_str> <queue_index>`
- `info mtree -f`: To tell if a MemoryRegion is registered in KVM (from kvm_int.h)


# For virtio-xblk-pci
1. Apply the patch
```
git apply virtio-xblk-helper-patch.patch # create empty source files that need to be filled in.
```


