README.md
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.
README_zh.md
1# Qemu ARM Virt 教程 - liteos_a
2
3## 1. 简介
4
5`arm_virt/` 子目录包含部分Qemu ARM虚拟化平台验证的OpenHarmony kernel\_liteos\_a的适配代码,含驱动配置、板端配置等。
6
7ARM 虚拟化平台是一个 `qemu-system-arm` 的目标设备,通过它来模拟一个通用的、基于ARM架构的单板。
8Qemu中machine为 **virt** 的单板就是这种可配置的,例如:选择核的类型、核的个数、内存的大小和安全特性等,单板设备的配置。
9
10这次模拟的配置是:Cortex-A7架构,1个CPU,带安全扩展,GICv2,1G内存。
11提示: 系统内存硬编码为32MB。
12
13## 2. 环境搭建
14
15参考链接: [环境搭建](https://gitee.com/openharmony/docs/blob/HEAD/zh-cn/device-dev/quick-start/quickstart-lite-env-setup.md)
16
17## 3. 获取源码
18
19参考链接: [代码获取](https://gitee.com/openharmony/docs/blob/HEAD/zh-cn/device-dev/get-code/sourcecode-acquire.md)
20
21## 4. 源码构建
22
23在已经获取的源码根目录,请输入:
24
25```
26hb set
27```
28
29选择ohemu下的`qemu_small_system_demo`选项。
30
31
32然后执行构建命令如下:
33
34```
35hb build
36```
37
38这个命令构建会产生 `OHOS_Image.bin`、`rootfs_jffs2.img` 和 `userfs_jffs2.img` 的镜像文件。
39
40在构建完成之后,对应的镜像文件在out/arm_virt/qemu_small_system_demo/目录。
41
42
43## 5. 在Qemu中运行镜像
44
45a) 如果没有安装 `qemu-system-arm` ,安装请参考链接 [Qemu installation](https://gitee.com/openharmony/device_qemu/blob/HEAD/README_zh.md)
46
47提示:当前引入的功能在virt-5.1的目标machine已经完成测试,不保证所有的Qemu版本都能够运行成功,因此需要保证你的qemu-system-arm
48版本尽可能在5.1及以上。
49
50b) 制作以及运行镜像
51
52在代码根目录下,编译后会生成qemu-run脚本,可直接运行该脚本,根据脚本提示制作、运行镜像。
53
54执行`./qemu-run --help`提示如下:
55
56```
57Usage: ./qemu-run [OPTION]...
58Make a qemu image(flash.img) for OHOS, and run the image in qemu according
59to the options.
60
61 Options:
62
63 -f, --force rebuild flash.img
64 -n, --net-enable enable net
65 -l, --local-desktop no VNC
66 -b, --bootargs additional boot arguments(-bk1=v1,k2=v2...)
67 -g, --gdb enable gdb for kernel
68 -h, --help print help info
69
70 By default, flash.img will not be rebuilt if exists, and net will not
71 be enabled, gpu enabled and waiting for VNC connection at port 5920.
72```
73
74网卡模拟的是无线网卡wlan0,但无真的wifi功能;默认不加参数的情况下,网络不会自动配置。当根目录镜像文件flash.img存在时,镜像不会被重新制作。
75
76提示:初次运行脚本时,系统还会生成MMC镜像,主要内容为系统和用户数据文件。镜像存放在OHOS源码树的out目录下,文件名为smallmmc.img。只要不被删除,后续就不再重新制作该镜像。具体请见vendor/ohemu/qemu_small_system_demo/qemu_run.sh。
77
78c) 退出qemu环境
79
80按下`Ctrl-A + x`可退出qemu虚拟环境。
81
82## 6. gdb调试
83
84安装`gdb-multiarch`工具包:
85```
86sudo apt install gdb-multiarch
87```
88然后,
89```
90cd ohos/vendor/ohemu/qemu_small_system_demo/kernel_configs
91vim debug.config
92```
93
94将 `LOSCFG_CC_STACKPROTECTOR_ALL=y` 修改为:
95
96```
97# LOSCFG_CC_STACKPROTECTOR_ALL is not set
98LOSCFG_COMPILE_DEBUG=y
99```
100
101保存并退出,在OHOS根目录重新编译:
102
103```
104hb build -f
105```
106
107在一个窗口中输入命令:
108
109```
110./qemu-run -g
111```
112
113在另一个窗口中输入命令:
114
115```
116gdb-multiarch out/arm_virt/qemu_small_system_demo/OHOS_Image
117(gdb) target remote localhost:1234
118```
119
120更多gdb相关的调试可以查阅:[gdb指导手册](https://sourceware.org/gdb/current/onlinedocs/gdb)。
121
122## 7. 用法示例
123
124- [向内核传递参数](example.md#sectiondebug)
125
126- [用MMC映像传递文件](example.md#sectionfatfs)
127
128- [添加一个Hello World程序](example.md#addhelloworld)
129
130- [运行图形demo](example.md#simple_ui_demo)
131
132- [观察dsoftbus组网发现](example.md#dsoftbus_discover)
133
134## FAQ:
1351. 当网络配置出现问题时,如何排查问题?
136
137 手动配置主机网桥设备。Linux系统参考以下命令:
138
139 ```
140 sudo modprobe tun tap
141 sudo ip link add br0 type bridge
142 sudo ip address add 10.0.2.2/24 dev br0
143 sudo ip link set dev br0 up
144
145 # 以下命令执行一次后即可注释掉
146 sudo mkdir -p /etc/qemu
147 echo 'allow br0' | sudo tee -a /etc/qemu/bridge.conf
148 ```
149
150 配置完成后,用ip addr检查应有如下类似显示。当br0不存在或尖括号中为DOWN时,请重新检查配置命令。
151
152 ```
153 5: br0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
154 link/ether 2e:52:52:0e:21:44 brd ff:ff:ff:ff:ff:ff
155 inet 10.0.2.2/24 scope global br0
156 valid_lft forever preferred_lft forever
157 ```
158
159 当系统安装有docker等软件时,系统防火墙可能阻止网桥访问。
160
161 `cat /proc/sys/net/bridge/bridge-nf-call-iptables`会显示结果:1
162
163 这时,可用如下命令打开访问许可:
164
165 ```
166 echo 0 | sudo tee /proc/sys/net/bridge/bridge-nf-call-iptables
167 ```
168
169 提示:系统网络硬编码为10.0.2.0/24,网关10.0.2.2,网址随机选取。不同的客户机实例应使用不同的MAC和IP地址(flash、MMC映像文件也应不同),MAC地址可通过QEMU命令行传递,IP地址可在OHOS命令行中调整,如`ifconfig wlan0 inet 10.0.2.30`,或使用其它方法。
170
1712. qemu-run提示`qemu-system-arm`运行出错时,如何排查问题?
172
173 qemu-run脚本中,完整的执行命令及参数解释如下:
174
175 ```
176 qemu-system-arm -M virt,gic-version=2,secure=on -cpu cortex-a7 -smp cpus=1 -m 1G \
177 -drive if=pflash,file=flash.img,format=raw \
178 -drive if=none,file=./out/smallmmc.img,format=qcow2,id=mmc
179 -device virtio-blk-device,drive=mmc \
180 -netdev bridge,id=net0 \
181 -device virtio-net-device,netdev=net0,mac=12:22:33:44:55:66 \
182 -device virtio-gpu-device,xres=800,yres=480 \
183 -device virtio-tablet-device \
184 -device virtio-rng-device \
185 -vnc :20 \
186 -s -S \
187 -global virtio-mmio.force-legacy=false
188 ```
189
190 ```
191 -M 虚拟机类型,ARM virt,GIC 2中断控制器,有安全扩展
192 -cpu CPU型号
193 -smp SMP设置,单核
194 -m 虚拟机可使用的内存限制
195 -drive if=pflash CFI闪存盘设置
196 -drive if=none 块设备映像文件设置
197 -netdev [可选]网卡后端设置,桥接类型
198 -device virtio-blk-device 块存储设备
199 -device virtio-net-device [可选]网卡设备
200 -device virtio-gpu-device GPU设备
201 -device virtio-tablet-device 输入设备
202 -device virtio-rng-device 随机数设备
203 -vnc :20 [可选]远程桌面连接,端口5920
204 -s -S [可选]gdb单步调试
205 -global QEMU配置参数,不可调整
206 ```
207
208 运行时,qemu-run遇到报错如下报错: failed to parse default acl file
209
210 可能是qemu安装方式不同,导致qemu配置文件路径存在一定差异:
211
212 - 使用源码安装默认在/usr/local/qemu/etc/qemu
213
214 - 使用部分Linux发行版安装工具进行安装时,默认在/ect/qemu/目录下
215
216 可根据实际情况,确认具体配置目录,并进行如下修改:
217
218 ```
219 echo 'allow br0' | sudo tee -a <配置文件路径>
220 ```
221
222
2233. 1.1.0LTS版本qemu运行无输出?
224
225 LTS的代码存在一个内核启动缺陷,可以参考如下PR尝试解决问题:
226
227 https://gitee.com/openharmony/kernel_liteos_a/pulls/324
228
229
2304. VNC窗口不显示光标?
231
232 virtio-tablet是个模拟平板输入的设备,QEMU不捕获设备,虚拟机不显示光标,由VNC客户端自行处理光标显示。请查看VNC客户端选项设置。