1# QEMU Arm Virt Tutorial - liteos_a 2 3## 1. Overview 4 5The `arm_virt/` directory contains code that has been verified on the QEMU Arm Virt platform for adapting to OpenHarmony kernel\_liteos\_a. The code includes the driver and board configurations. 6 7The Arm Virt platform is a `qemu-system-arm` target device that simulates a general-purpose board running on the Arm architecture. 8The board whose **machine** is **virt** in QEMU is configurable. For example, you can select the core type and quantity, memory size, and security extensions when configuring the board. 9 10This tutorial guides you through the configuration of a board based on the Cortex-A7 architecture, with one CPU, extended secure features, Generic Interrupt Controller versions 2 (GICv2), and 1 GB memory. 11The system memory is hardcoded to 32 MB. 12 13## 2. Setting Up the Environment 14 15For details, see [Environment Setup](https://gitee.com/openharmony/docs/blob/HEAD/en/device-dev/quick-start/quickstart-lite-env-setup.md). 16 17## 3. Obtaining the Source Code 18 19For details, see [Source Code Acquisition](https://gitee.com/openharmony/docs/blob/HEAD/en/device-dev/get-code/sourcecode-acquire.md). 20 21## 4. Building the Source Code 22 23In the root directory of the obtained source code, run the following command: 24 25``` 26hb set 27``` 28 29Select `qemu_small_system_demo` under **ohemu**. 30 31Run the following build command: 32 33``` 34hb build 35``` 36 37After this command is executed, the image files `OHOS_Image.bin`, `rootfs_jffs2.img`, and `userfs_jffs2.img` are generated in out/arm_virt/qemu_small_system_demo/ directory. 38 39## 5. Running an Image in QEMU 40 41a) If `qemu-system-arm` has not been installed, install it. For details, see [Qemu Installation](https://gitee.com/openharmony/device_qemu/blob/HEAD/README.md). 42 43Note: The introduced functions have been tested on the target machine of virt-5.1, but are not available for all QEMU versions. Therefore, you must ensure that the qemu-system-arm version is 5.1 or later. 44 45b) Create and run an image. 46 47After the source code is built, the **qemu-run** script is generated in the root directory of the code. You can run the script to create and run an image as prompted. 48 49Run the `./qemu-run --help` command. The following information is displayed: 50 51``` 52Usage: ./qemu-run [OPTION]... 53Make a qemu image(flash.img) for OHOS, and run the image in qemu according 54to the options. 55 56 Options: 57 58 -f, --force rebuild flash.img 59 -n, --net-enable enable net 60 -l, --local-desktop no VNC 61 -b, --bootargs additional boot arguments(-bk1=v1,k2=v2...) 62 -g, --gdb enable gdb for kernel 63 -h, --help print help info 64 65 By default, flash.img will not be rebuilt if exists, and net will not 66 be enabled, gpu enabled and waiting for VNC connection at port 5920. 67``` 68 69Simulated network interface is wireless card wlan0, but has no real wifi functions. By default, the network will not be automatically configured if no parameter is specified. If the root directory image file **flash.img** exists, the image will not be re-created. 70 71Note: On the first run, script will also create a MMC image mainly for system and user data files. It is stored at OHOS source tree 'out' sub-directory, named 'smallmmc.img'. Whenever it exists, script will never try to touch it again. More information please refer to `vendor/ohemu/qemu_small_system_demo/qemu_run.sh`. 72 73c) Exit QEMU. 74 75Press `Ctrl-A + x` to exit the QEMU virtual environment. 76 77## 6. gdb debug 78 79Install `gdb-multiarch` package: 80``` 81sudo apt install gdb-multiarch 82``` 83Then, 84``` 85cd ohos/vendor/ohemu/qemu_small_system_demo/kernel_configs 86vim debug.config 87``` 88 89Modify `LOSCFG_CC_STACKPROTECTOR_ALL=y` to: 90 91``` 92# LOSCFG_CC_STACKPROTECTOR_ALL is not set 93LOSCFG_COMPILE_DEBUG=y 94``` 95 96Save and exit, recompile under OHOS root directory: 97 98``` 99hb build -f 100``` 101 102In a window to enter the command: 103 104``` 105./qemu-run -g 106``` 107 108In another window to enter the command: 109 110``` 111gdb-multiarch out/arm_virt/qemu_small_system_demo/OHOS_Image 112(gdb) target remote localhost:1234 113``` 114 115More GDB related debugging can refer to [GDB instruction manual](https://sourceware.org/gdb/current/onlinedocs/gdb). 116 117## 7. Example 118 119- [Transferring Parameters to the Kernel](example.md#sectiondebug) 120 121- [Transferring Files Using MMC Images](example.md#sectionfatfs) 122 123- [Adding a Hello World Program](example.md#addhelloworld) 124 125- [Running the Graphic Demo](example.md#simple_ui_demo) 126 127- [Observe dsoftbus discovery](example.md#dsoftbus_discover) 128 129## FAQ: 1301. How do I locate a network configuration problem? 131 132 Manually configure a host network bridge. For Linux, run the following commands: 133 134 ``` 135 sudo modprobe tun tap 136 sudo ip link add br0 type bridge 137 sudo ip address add 10.0.2.2/24 dev br0 138 sudo ip link set dev br0 up 139 140 # The following commands can be commented out after being executed: 141 sudo mkdir -p /etc/qemu 142 echo 'allow br0' | sudo tee -a /etc/qemu/bridge.conf 143 ``` 144 145 Run the **ip addr** command to check the configuration. If **br0** does not exist or the value in the angle brackets is **DOWN**, check the configuration commands again. 146 147 ``` 148 5: br0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000 149 link/ether 2e:52:52:0e:21:44 brd ff:ff:ff:ff:ff:ff 150 inet 10.0.2.2/24 scope global br0 151 valid_lft forever preferred_lft forever 152 ``` 153 154 If software such as Docker has been installed in the system, the system firewall may prevent the bridge from accessing the system. Run the following command: 155 156 `cat /proc/sys/net/bridge/bridge-nf-call-iptables` 157 158 **1** is displayed. Run the following command to allow the access from the network bridge: 159 160 ``` 161 echo 0 | sudo tee /proc/sys/net/bridge/bridge-nf-call-iptables 162 ``` 163 164 Note: The system is hardcoded to **10.0.2.0/24** for the sub-network, **10.0.2.2** for the gateway, and random IP address. Use different MAC addresses, IP addresses, and flash, MMC images for different client instances. The MAC address can be transferred using the QEMU command line. The IP address can be adjusted in the OHOS command line, for example, using `ifconfig wlan0 inet 10.0.2.30` or other methods. 165 1662. How do I troubleshoot the error when running `qemu-system-arm`? 167 168 The commands and parameters in the **qemu-run** script are as follows: 169 170 ``` 171 qemu-system-arm -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G \ 172 -drive if=pflash,file=flash.img,format=raw \ 173 -drive if=none,file=./out/smallmmc.img,format=qcow2,id=mmc 174 -device virtio-blk-device,drive=mmc \ 175 -netdev bridge,id=net0 \ 176 -device virtio-net-device,netdev=net0,mac=12:22:33:44:55:66 \ 177 -device virtio-gpu-device,xres=800,yres=480 \ 178 -device virtio-tablet-device \ 179 -device virtio-rng-device \ 180 -vnc :20 \ 181 -s -S \ 182 -global virtio-mmio.force-legacy=false 183 ``` 184 185 ``` 186 -M Virtual machine type, ARM virt, GICv2, and extended security features 187 -cpu CPU model 188 -smp SMP setting, single core 189 -m Maximum memory size that can be used by the virtual machines 190 -drive if=pflash CFI flash drive setting 191 -drive if=none Block device image setting 192 -netdev [optional] NIC bridge type 193 -device virtio-blk-device Block device 194 -device virtio-net-device [optional] NIC device 195 -device virtio-gpu-device GPU device 196 -device virtio-tablet-device Input device 197 -device virtio-rng-device Random generator device 198 -vnc: 20 [optional] Remote desktop connection, port 5920 199 -s -S [optional] gdb single step debug 200 -global QEMU configuration parameter, which cannot be changed 201 ``` 202 203 If the error message "failed to parse default acl file" is displayed when **qemu-run** is executed: 204 205 The error may be caused by the QEMU configuration file path, which varies with the QEMU installation mode. The default QEMU configuration file path is: 206 207 - **/usr/local/qemu/etc/qemu** if QEMU is installed from the source code. 208 209 - **/ect/qemu/** if QEMU is installed by using a Linux distribution installation tool. 210 211 Determine the configuration file path and run the following command: 212 213 ``` 214 echo 'allow br0' | sudo tee -a <Configuration file path> 215 ``` 216 217 2183. What do I do if there is no output after QEMU of LTS 1.1.0 is executed? 219 220 The LTS code has a kernel startup defect. You can try to resolve the problem by referring to the following: 221 222 https://gitee.com/openharmony/kernel_liteos_a/pulls/324 223 224 2254. VNC window has no cursor? 226 227 Virtio-tablet is an emulated tablet input device. Neither QEMU captures nor the virtual machine displays it. The cursor is displayed by VNC client. Please check VNC client options.