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 64 MB.
12
13## 2. Setting Up the Environment
14
15For details, see [Environment Setup](https://gitee.com/openharmony/docs/blob/master/en/device-dev/quick-start/Readme-EN.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 result files 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 `./qemu-run --help` to get help.
50
51Simulated 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.
52
53If the image file **out/smallmmc.img** exists, the image will not be re-created by default. MMC image mainly for rootfs and userfs. The 1st partition is rootfs, 2nd is userfs and mounted at /userdata, 3rd is for user data and mounted at /userdata.
54
55c) Exit QEMU.
56
57Press `Ctrl-A + x` to exit the QEMU virtual environment.
58
59## 6. gdb debug
60
61Install `gdb-multiarch` package:
62```
63sudo apt install gdb-multiarch
64```
65Then,
66```
67cd ohos/vendor/ohemu/qemu_small_system_demo/kernel_configs
68vim debug.config
69```
70
71Modify `LOSCFG_CC_STACKPROTECTOR_ALL=y` to:
72
73```
74# LOSCFG_CC_STACKPROTECTOR_ALL is not set
75LOSCFG_COMPILE_DEBUG=y
76```
77
78Save and exit, recompile under OHOS root directory:
79
80```
81hb build -f
82```
83
84In a window to enter the command:
85
86```
87./qemu-run -g
88```
89
90In another window to enter the command:
91
92```
93gdb-multiarch out/arm_virt/qemu_small_system_demo/OHOS_Image
94(gdb) target remote localhost:1234
95```
96
97More GDB related debugging can refer to [GDB instruction manual](https://sourceware.org/gdb/current/onlinedocs/gdb).
98
99## 7. Example
100
101- [Transferring Parameters to the Kernel](example.md#sectiondebug)
102
103- [Transferring Files Using MMC Images](example.md#sectionfatfs)
104
105- [Adding a Hello World Program](example.md#addhelloworld)
106
107- [Running the Graphic Demo](example.md#simple_ui_demo)
108
109- [Observe dsoftbus Discovery](example.md#dsoftbus_discover)
110
111- [Hack A Sample Desktop](example.md#desktop)
112
113## FAQ:
1141. How do I locate a network configuration problem?
115
116   Manually configure a host network bridge. For Linux, run the following commands:
117
118   ```
119   sudo modprobe tun tap
120   sudo ip link add br0 type bridge
121   sudo ip address add 10.0.2.2/24 dev br0
122   sudo ip link set dev br0 up
123
124   # The following commands can be commented out after being executed:
125   sudo mkdir -p /etc/qemu
126   echo 'allow br0' | sudo tee -a /etc/qemu/bridge.conf
127   ```
128
129   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.
130
131   ```
132   5: br0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
133       link/ether 2e:52:52:0e:21:44 brd ff:ff:ff:ff:ff:ff
134       inet 10.0.2.2/24 scope global br0
135          valid_lft forever preferred_lft forever
136   ```
137
138   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:
139
140   `cat /proc/sys/net/bridge/bridge-nf-call-iptables`
141
142   **1** is displayed. Run the following command to allow the access from the network bridge:
143
144   ```
145   echo 0 | sudo tee /proc/sys/net/bridge/bridge-nf-call-iptables
146   ```
147
148   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.
149
1502. How do I troubleshoot the error when running `qemu-system-arm`?
151
152   The commands and parameters in the **qemu-run** script are as follows:
153
154   ```
155   qemu-system-arm -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G \
156        -bios out/arm_virt/qemu_small_system_demo/OHOS_Image.bin \
157        -drive if=none,file=out/smallmmc.img,format=raw,id=mmc
158        -device virtio-blk-device,drive=mmc \
159        -netdev bridge,id=net0 \
160        -device virtio-net-device,netdev=net0,mac=12:22:33:44:55:66 \
161        -device virtio-gpu-device,xres=960,yres=480 \
162        -device virtio-tablet-device \
163        -device virtio-rng-device \
164        -vnc :20 \
165        -s -S \
166        -global virtio-mmio.force-legacy=false
167   ```
168
169   ```
170   -M                           Virtual machine type, ARM virt, GICv2, and extended security features
171   -cpu                         CPU model
172   -smp                         SMP setting, single core
173   -m                           Maximum memory size that can be used by the virtual machines
174   -drive if=none               Block device image setting
175   -netdev                      [optional] NIC bridge type
176   -device virtio-blk-device    Block device
177   -device virtio-net-device    [optional] NIC device
178   -device virtio-gpu-device    GPU device
179   -device virtio-tablet-device Input device
180   -device virtio-rng-device    Random generator device
181   -vnc: 20                     [recommend] Remote desktop connection, port 5920
182   -s -S                        [optional] gdb single step debug
183   -global                      QEMU configuration parameter, which cannot be changed
184   ```
185
186   If the error message "failed to parse default acl file" is displayed when **qemu-run** is executed:
187
188   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:
189
190   - **/usr/local/qemu/etc/qemu** if QEMU is installed from the source code.
191
192   - **/ect/qemu/** if QEMU is installed by using a Linux distribution installation tool.
193
194   Determine the configuration file path and run the following command:
195
196   ```
197   echo 'allow br0' | sudo tee -a <Configuration file path>
198   ```
199
200
2013. What do I do if there is no output after QEMU of LTS 1.1.0 is executed?
202
203   The LTS code has a kernel startup defect. You can try to resolve the problem by referring to the following:
204
205   https://gitee.com/openharmony/kernel_liteos_a/pulls/324
206
207
2084. VNC window has no cursor?
209
210   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.