# 2019q1 Homework4 (riscv) contributed by < `F74021200` > ## 自我檢查清單: ### 1.riscv-emu 原始程式碼中多次出現 virtio,這樣的機制對於 host 和 guest 兩端有何作用?在閱讀 Virtio: An I/O virtualization framework for Linux 一文後,對照原始程式碼,你發現什麼? 由 [Virtio: An I/O virtualization framework for Linux](https://www.ibm.com/developerworks/library/l-virtio/index.html) 中節錄以下幾點: #### 1. > you can see that virtio is an abstraction for a set of common emulated devices in a paravirtualized hypervisor. This design allows the hypervisor to export a common set of emulated devices and make them available through a common application programming interface (API). virtio 將一些模擬的常見 device 抽象化,這樣的設計能使這些元件被同樣的 API 存取。 #### 2. > With paravirtualized hypervisors, the guests implement a common set of interfaces, with the particular device emulation behind a set of back-end drivers. The back-end drivers need not be common as long as they implement the required behaviors of the front end. 使用 paravirtualized hypervisors 的機制, guset 端只需要執行同樣類別的界面;只要在前端執行需要的行為,就能讓後端的 driver 執行不同的操作。 #### 3. > In addition to the front-end drivers (implemented in the guest operating system) and the back-end drivers (implemented in the hypervisor), virtio defines two layers to support guest-to-hypervisor communication. At the top level (called virtio) is the virtual queue interface that conceptually attaches front-end drivers to back-end drivers. Drivers can use zero or more queues, depending on their need. virtio 定義了 virtual queue 的界面,用來連接前端 driver 與後端 driver。 #### 4. > At the top is the virtio_driver, which represents the front-end driver in the guest. Devices that match this driver are encapsulated by the virtio_device (a representation of the device in the guest). This refers to the virtio_config_ops structure (which defines the operations for configuring the virtio device). 在 guset 端, virtio_driver 是用來表示前端 driver , virtio_device 則用來表示 device , virtio_driver 與 virtio_device 會互相配對; virtio_device 會使用 virtio_config_ops 作設定的操作。 #### 5. > The virtio_device is referred to by the virtqueue (which includes a reference to the virtio_device to which it serves) virtio_device 能被 virtqueue 存取。 #### 6. > Finally, each virtqueue object references the virtqueue_ops object, which defines the underlying queue operations for dealing with the hypervisor driver. 每個 virtqueue 物件會透過 virtqueue_ops 物件執行 queue 的操作,這些操作是用來處理 hypervisor driver 。 #### 7. > The process begins with the creation of a virtio_driver and subsequent registration via register_virtio_driver. The virtio_driver structure defines the upper-level device driver, list of device IDs that the driver supports, a features table (dependent upon the device type), and a list of callback functions. 先建立一個 virtio_driver 並透過 register_virtio_driver 將這個 virtio_device 存於 register 中; virtio_driver 這個結構定義了上層的 device driver , 支援的一些 device 的 ID , features table ,以及一些 callback functions 。 #### 8. > Note that the virtio_device includes no reference to the virtqueue (but the virtqueue does reference the virtio_device). 能透過 virtqueue 存取到 virtio_device ,但無法透過 virtqueue 存取到 virtqueue 。 #### 9. > Guest (front-end) drivers communicate with hypervisor (back-end) drivers through buffers. For an I/O, the guest provides one or more buffers representing the request. Guest 端的 driver 透過 buffer 與 hypervisor 端溝通;像是若要輸入或輸出資料時, guest 端會提供一或多個 buffer ,這些 buffer 代表著這個輸入輸出請求。 #### 10. > Linking the guest driver and hypervisor driver occurs through the virtio_device and most commonly through virtqueues. The virtqueue supports its own API consisting of five functions. Guest 端 driver 與 hypervisor 端 driver 的連結,是透過 virtio_device 與 virtqueue 共同達成,其中 virtqueue 執行大部分操作。 #### 11. > You use the first function, add_buf, to provide a request to the hypervisor. This request is in the form of the scatter-gather list discussed previously. To add_buf, the guest provides the virtqueue to which the request is to be enqueued, the scatter-gather list (an array of addresses and lengths), the number of buffers that serve as out entries (destined for the underlying hypervisor), and the number of in entries (for which the hypervisor will store data and return to the guest). When a request has been made to the hypervisor through add_buf, the guest can notify the hypervisor of the new request using the kick function. For best performance, the guest should load as many buffers as possible onto the virtqueue before notifying through kick. 透過 add_buf 這個 function 將向 hypervisor 提出的 request 以 queue 的資料結構儲存; guest 端可以使用 kick function 告知 hypervisor 有新的 request 。 #### 12. > Responses from the hypervisor occur through the get_buf function. The guest can poll simply by calling this function or wait for notification through the provided virtqueue callback function. When the guest learns that buffers are available, the call to get_buf returns the completed buffers. Guest 可以透過呼叫 get_buf 或是等待來自 virtqueue 的 callback function 告知,來查看是否有來自 hypervisor 的回應,當 guest 知道 回應的 buffer 已可取得時,對於 get_buf 的呼叫會回傳完整的 buffer 。 #### virtio 對於 host 與 guest 兩端作用: ##### host: 1. 執行各個 emulated device 的後端 driver 2. 連接後端 driver 與前端 driver 3. 將 guest 端的請求傳遞給 host 4. 將 host 端的回應,回傳給 guest ##### guest: 1. 使用 virtio_device 來抽象化 guest 端的 device 2. 執行必要行為讓 host 端對應的 driver 依照不同 device 執行不同操作 3. 設定 virtio_device 4. 配對 virtio_device 與 virtio_driver 5. 連接前端 driver 與後端 driver 6. 將請求串連成 list 並發送給 host 7. 查看 host 端是否有無回應,並接收回應