• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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. The 1st partition will be mounted at /sdcard, 2nd at /userdata, and 3rd is reserved. 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- [Hack A Sample Desktop](example.md#desktop)
130
131## FAQ:
1321. How do I locate a network configuration problem?
133
134   Manually configure a host network bridge. For Linux, run the following commands:
135
136   ```
137   sudo modprobe tun tap
138   sudo ip link add br0 type bridge
139   sudo ip address add 10.0.2.2/24 dev br0
140   sudo ip link set dev br0 up
141
142   # The following commands can be commented out after being executed:
143   sudo mkdir -p /etc/qemu
144   echo 'allow br0' | sudo tee -a /etc/qemu/bridge.conf
145   ```
146
147   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.
148
149   ```
150   5: br0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
151       link/ether 2e:52:52:0e:21:44 brd ff:ff:ff:ff:ff:ff
152       inet 10.0.2.2/24 scope global br0
153          valid_lft forever preferred_lft forever
154   ```
155
156   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:
157
158   `cat /proc/sys/net/bridge/bridge-nf-call-iptables`
159
160   **1** is displayed. Run the following command to allow the access from the network bridge:
161
162   ```
163   echo 0 | sudo tee /proc/sys/net/bridge/bridge-nf-call-iptables
164   ```
165
166   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.
167
1682. How do I troubleshoot the error when running `qemu-system-arm`?
169
170   The commands and parameters in the **qemu-run** script are as follows:
171
172   ```
173   qemu-system-arm -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G \
174        -drive if=pflash,file=flash.img,format=raw \
175        -drive if=none,file=./out/smallmmc.img,format=qcow2,id=mmc
176        -device virtio-blk-device,drive=mmc \
177        -netdev bridge,id=net0 \
178        -device virtio-net-device,netdev=net0,mac=12:22:33:44:55:66 \
179        -device virtio-gpu-device,xres=960,yres=480 \
180        -device virtio-tablet-device \
181        -device virtio-rng-device \
182        -vnc :20 \
183        -s -S \
184        -global virtio-mmio.force-legacy=false
185   ```
186
187   ```
188   -M                           Virtual machine type, ARM virt, GICv2, and extended security features
189   -cpu                         CPU model
190   -smp                         SMP setting, single core
191   -m                           Maximum memory size that can be used by the virtual machines
192   -drive if=pflash             CFI flash drive setting
193   -drive if=none               Block device image setting
194   -netdev                      [optional] NIC bridge type
195   -device virtio-blk-device    Block device
196   -device virtio-net-device    [optional] NIC device
197   -device virtio-gpu-device    GPU device
198   -device virtio-tablet-device Input device
199   -device virtio-rng-device    Random generator device
200   -vnc: 20                     [recommend] Remote desktop connection, port 5920
201   -s -S                        [optional] gdb single step debug
202   -global                      QEMU configuration parameter, which cannot be changed
203   ```
204
205   If the error message "failed to parse default acl file" is displayed when **qemu-run** is executed:
206
207   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:
208
209   - **/usr/local/qemu/etc/qemu** if QEMU is installed from the source code.
210
211   - **/ect/qemu/** if QEMU is installed by using a Linux distribution installation tool.
212
213   Determine the configuration file path and run the following command:
214
215   ```
216   echo 'allow br0' | sudo tee -a <Configuration file path>
217   ```
218
219
2203. What do I do if there is no output after QEMU of LTS 1.1.0 is executed?
221
222   The LTS code has a kernel startup defect. You can try to resolve the problem by referring to the following:
223
224   https://gitee.com/openharmony/kernel_liteos_a/pulls/324
225
226
2274. VNC window has no cursor?
228
229   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.