1e50a6411SPhilipp Schuster# `cloud-hypervisor` debug IO ports 2c8364172SSamuel Ortiz 3e50a6411SPhilipp SchusterWhen running x86 guests, `cloud-hypervisor` provides different kinds of debug ports: 4*730cf1e9SRuoqing He 5*730cf1e9SRuoqing He- [`0x80` debug port](https://web.archive.org/web/20211028033025/https://www.intel.com/content/www/us/en/support/articles/000005500/boards-and-kits.html) 6e50a6411SPhilipp Schuster- Debug console (by default at `0xe9`). 7e50a6411SPhilipp Schuster- Firmware debug port at `0x402`. 8e50a6411SPhilipp Schuster 9e50a6411SPhilipp SchusterAll of them can be used to trace user-defined guest events and all of them can 10e50a6411SPhilipp Schusterbe used simultaneously. 11e50a6411SPhilipp Schuster 12e50a6411SPhilipp Schuster## Debug Ports Overview 13e50a6411SPhilipp Schuster 14e50a6411SPhilipp Schuster### `0x80` I/O port 15c8364172SSamuel Ortiz 16c8364172SSamuel OrtizWhenever the guest write one byte between `0x0` and `0xF` on this particular 17c8364172SSamuel OrtizI/O port, `cloud-hypervisor` will log and timestamp that event at the `debug` 18c8364172SSamuel Ortizlog level. 19c8364172SSamuel Ortiz 20c8364172SSamuel OrtizIt is up to the guest stack to decide when and what to write to the 0x80 port 21c8364172SSamuel Ortizin order to signal the host about specific events and have `cloud-hypervisor` 22c8364172SSamuel Ortizlog it. 23c8364172SSamuel Ortiz 24c8364172SSamuel Ortiz`cloud-hypervisor` defines several debug port code ranges that should be used 25c8364172SSamuel Ortizfor debugging specific components of the guest software stack. When logging a 26c8364172SSamuel Ortizwrite of one of those codes to the debug port, `cloud-hypervisor` adds a 27c8364172SSamuel Ortizpre-defined string to the logs. 28c8364172SSamuel Ortiz 29c8364172SSamuel Ortiz| Code Range | Component | Log string | 30*730cf1e9SRuoqing He| ---------------- | ---------- | ------------ | 31c8364172SSamuel Ortiz| `0x00` to `0x1f` | Firmware | `Firmware` | 32c8364172SSamuel Ortiz| `0x20` to `0x3f` | Bootloader | `Bootloader` | 33c8364172SSamuel Ortiz| `0x40` to `0x5f` | Kernel | `Kernel` | 34c8364172SSamuel Ortiz| `0x60` to `0x7f` | Userspace | `Userspace` | 35c8364172SSamuel Ortiz| `0x80` to `0xff` | Custom | `Custom` | 36c8364172SSamuel Ortiz 37c8364172SSamuel OrtizOne typical use case is guest boot time measurement and tracing. By writing 38c8364172SSamuel Ortizdifferent values to the debug I/O port at different boot process steps, the 39c8364172SSamuel Ortizguest will have `cloud-hypervisor` generate timestamped logs of all those steps. 40c8364172SSamuel OrtizThat provides a basic but convenient way of measuring not only the overall guest 41c8364172SSamuel Ortizboot time but all intermediate steps as well. 42c8364172SSamuel Ortiz 43e50a6411SPhilipp Schuster#### Logging 44c8364172SSamuel Ortiz 45c8364172SSamuel OrtizAssuming parts of the guest software stack have been instrumented to use the 46c8364172SSamuel Ortiz`cloud-hypervisor` debug I/O port, we may want to gather the related logs. 47c8364172SSamuel Ortiz 48c8364172SSamuel OrtizTo do so we need to start `cloud-hypervisor` with the right debug level 49fa22cb0bSRavi kumar Veeramally(`-vvv`). It is also recommended to have it log into a dedicated file in order 50c8364172SSamuel Ortizto easily grep for the tracing logs (e.g. 51c8364172SSamuel Ortiz`--log-file /tmp/cloud-hypervisor.log`): 52c8364172SSamuel Ortiz 53c8364172SSamuel Ortiz``` 54c8364172SSamuel Ortiz./target/debug/cloud-hypervisor \ 55c8364172SSamuel Ortiz --kernel ~/rust-hypervisor-firmware/target/target/release/hypervisor-fw \ 56a3342bdbSSebastien Boeuf --disk path=~/hypervisor/images/focal-server-cloudimg-amd64.raw \ 57c8364172SSamuel Ortiz --cpus 4 \ 58c8364172SSamuel Ortiz --memory size=1024M \ 59c8364172SSamuel Ortiz --rng \ 60c8364172SSamuel Ortiz --log-file /tmp/ch-fw.log \ 61fa22cb0bSRavi kumar Veeramally -vvv 62c8364172SSamuel Ortiz``` 63c8364172SSamuel Ortiz 64c8364172SSamuel OrtizAfter booting the guest, we then have to grep for the debug I/O port traces in 65c8364172SSamuel Ortizthe log file: 66c8364172SSamuel Ortiz 67c8364172SSamuel Ortiz```Shell 68c8364172SSamuel Ortiz$ grep "Debug I/O port" /tmp/ch-fw.log 69c8364172SSamuel Ortizcloud-hypervisor: 19.762449ms: DEBUG:vmm/src/vm.rs:510 -- [Debug I/O port: Firmware code 0x0] 0.019004 seconds 70c8364172SSamuel Ortizcloud-hypervisor: 403.499628ms: DEBUG:vmm/src/vm.rs:510 -- [Debug I/O port: Firmware code 0x1] 0.402744 seconds 71c8364172SSamuel Ortiz``` 72e50a6411SPhilipp Schuster 73e50a6411SPhilipp Schuster### Debug console port 74e50a6411SPhilipp Schuster 75e50a6411SPhilipp SchusterThe debug console is inspired by QEMU and Bochs, which have a similar feature. 76e50a6411SPhilipp SchusterBy default, the I/O port `0xe9` is used. This port can be configured like a 77e50a6411SPhilipp Schusterconsole. Thus, it can print to a tty, a file, or a pty, for example. 78e50a6411SPhilipp Schuster 79e50a6411SPhilipp Schuster### Firmware debug port 80e50a6411SPhilipp Schuster 81e50a6411SPhilipp SchusterThe firmware debug port is also a simple port that prints all bytes written to 82e50a6411SPhilipp Schusterit. The firmware debug port only prints to stdout. 83e50a6411SPhilipp Schuster 84e50a6411SPhilipp Schuster## When do I need these ports? 85e50a6411SPhilipp Schuster 86e50a6411SPhilipp SchusterThe ports are on the one hand interesting for firmware or kernel developers, as 87e50a6411SPhilipp Schusterthey provide an easy way to print debug information from within a guest. 88e50a6411SPhilipp SchusterFurthermore, you can patch "normal" software to measure certain events, such as 89e50a6411SPhilipp Schusterthe boot time of a guest. 90e50a6411SPhilipp Schuster 91e50a6411SPhilipp Schuster## Which port should I choose? 92e50a6411SPhilipp Schuster 93e50a6411SPhilipp SchusterThe `0x80` debug port and the port of the firmware debug device are always 94e50a6411SPhilipp Schusteravailable. The debug console must be activated via the command line, but 95e50a6411SPhilipp Schusterprovides more configuration options. 96e50a6411SPhilipp Schuster 97e50a6411SPhilipp SchusterYou can use different ports for different aspect of your logging messages. 98