1#!/bin/bash
2
3set -ex -o pipefail
4
5test_variant=$1
6
7# Default GIC version
8gic_version="2"
9
10if [ -z "${test_variant}" ]; then
11    passed="ping test passed"
12    domU_check="
13until ifconfig eth0 192.168.0.2 &> /dev/null && ping -c 10 192.168.0.1; do
14    sleep 30
15done
16echo \"${passed}\"
17"
18fi
19
20if [[ "${test_variant}" == "static-mem" ]]; then
21    # Memory range that is statically allocated to DOM1
22    domu_base="0x50000000"
23    domu_size="0x10000000"
24    passed="${test_variant} test passed"
25    domU_check="
26mem_range=$(printf \"%08x-%08x\" ${domu_base} $(( ${domu_base} + ${domu_size} - 1 )))
27if grep -q -x \"\${mem_range} : System RAM\" /proc/iomem; then
28    until ifconfig eth0 192.168.0.2 &> /dev/null && ping -c 10 192.168.0.1; do
29        sleep 30
30    done
31    echo \"${passed}\"
32fi
33"
34fi
35
36if [[ "${test_variant}" == "static-heap" ]]; then
37    passed="${test_variant} test passed"
38    domU_check="echo \"${passed}\""
39fi
40
41
42if [[ "${test_variant}" == "static-shared-mem" ]]; then
43    passed="${test_variant} test passed"
44    SHARED_MEM_HOST="50000000"
45    SHARED_MEM_GUEST="4000000"
46    SHARED_MEM_SIZE="10000000"
47    SHARED_MEM_ID="my-shared-mem-0"
48
49    domU_check="
50current_id=\$(cat /proc/device-tree/reserved-memory/xen-shmem@4000000/xen,id 2>/dev/null)
51expected_id=\"\$(echo ${SHARED_MEM_ID})\"
52current_reg=\$(hexdump -e '16/1 \"%02x\"' /proc/device-tree/reserved-memory/xen-shmem@4000000/reg 2>/dev/null)
53expected_reg=$(printf \"%016x%016x\" 0x${SHARED_MEM_GUEST} 0x${SHARED_MEM_SIZE})
54if [[ \"\${expected_reg}\" == \"\${current_reg}\" && \"\${current_id}\" == \"\${expected_id}\" ]]; then
55    echo \"${passed}\"
56fi
57    "
58fi
59
60if [[ "${test_variant}" == "boot-cpupools" ]]; then
61    # Check if domU0 (id=1) is assigned to Pool-1 with null scheduler
62    passed="${test_variant} test passed"
63    dom0_check="
64if xl list -c 1 | grep -q Pool-1 && xl cpupool-list Pool-1 | grep -q Pool-1; then
65    echo ${passed}
66fi
67"
68fi
69
70if [[ "${test_variant}" == "earlyprintk" ]]; then
71    # Last early printk message before entering C world
72    passed="\- Ready \-"
73fi
74
75if [[ "${test_variant}" == "gicv3" ]]; then
76    gic_version=3
77    passed="${test_variant} test passed"
78    domU_check="echo \"${passed}\""
79fi
80
81# XXX QEMU looks for "efi-virtio.rom" even if it is unneeded
82curl -fsSLO https://github.com/qemu/qemu/raw/v5.2.0/pc-bios/efi-virtio.rom
83./binaries/qemu-system-aarch64 \
84   -machine virtualization=true \
85   -cpu cortex-a57 -machine type=virt,gic-version=$gic_version \
86   -m 2048 -smp 2 -display none \
87   -machine dumpdtb=binaries/virt.dtb
88
89# XXX disable pl061 to avoid Linux crash
90fdtput binaries/virt.dtb -p -t s /pl061@9030000 status disabled
91
92# Busybox
93mkdir -p initrd
94mkdir -p initrd/bin
95mkdir -p initrd/sbin
96mkdir -p initrd/etc
97mkdir -p initrd/dev
98mkdir -p initrd/proc
99mkdir -p initrd/sys
100mkdir -p initrd/lib
101mkdir -p initrd/var
102mkdir -p initrd/mnt
103cp /bin/busybox initrd/bin/busybox
104initrd/bin/busybox --install initrd/bin
105echo "#!/bin/sh
106
107mount -t proc proc /proc
108mount -t sysfs sysfs /sys
109mount -t devtmpfs devtmpfs /dev
110${domU_check}
111/bin/sh" > initrd/init
112chmod +x initrd/init
113cd initrd
114find . | cpio --create --format='newc' | gzip > ../binaries/initrd
115cd ..
116
117# Dom0 rootfs
118cp binaries/rootfs.cpio.gz binaries/dom0-rootfs.cpio.gz
119cat binaries/xen-tools.cpio.gz >> binaries/dom0-rootfs.cpio.gz
120
121# test-local configuration
122mkdir -p rootfs
123cd rootfs
124mkdir -p etc/local.d
125
126echo "#!/bin/bash
127
128bash /etc/init.d/xencommons start
129
130/usr/lib/xen/bin/init-dom0less
131
132brctl addbr xenbr0
133brctl addif xenbr0 eth0
134ifconfig eth0 up
135ifconfig xenbr0 up
136ifconfig xenbr0 192.168.0.1
137
138xl network-attach 1 type=vif
139${dom0_check}
140" > etc/local.d/xen.start
141chmod +x etc/local.d/xen.start
142find . | cpio -H newc -o | gzip >> ../binaries/dom0-rootfs.cpio.gz
143cd ..
144
145# ImageBuilder
146echo 'MEMORY_START="0x40000000"
147MEMORY_END="0x50000000"
148
149DEVICE_TREE="virt.dtb"
150XEN="xen"
151DOM0_KERNEL="Image"
152DOM0_RAMDISK="dom0-rootfs.cpio.gz"
153XEN_CMD="console=dtuart dom0_mem=512M console_timestamps=boot"
154
155NUM_DOMUS=1
156DOMU_KERNEL[0]="Image"
157DOMU_RAMDISK[0]="initrd"
158DOMU_MEM[0]="256"
159DOMU_KERNEL[1]="Image"
160DOMU_RAMDISK[1]="initrd"
161DOMU_MEM[1]="256"
162
163LOAD_CMD="tftpb"
164UBOOT_SOURCE="boot.source"
165UBOOT_SCRIPT="boot.scr"' > binaries/config
166
167if [[ "${test_variant}" == "static-mem" ]]; then
168    echo -e "\nDOMU_STATIC_MEM[0]=\"${domu_base} ${domu_size}\"" >> binaries/config
169fi
170
171if [[ "${test_variant}" == "static-shared-mem" ]]; then
172echo "
173NUM_DOMUS=2
174DOMU_SHARED_MEM[0]=\"${SHARED_MEM_ID} 0x${SHARED_MEM_HOST} 0x${SHARED_MEM_GUEST} 0x${SHARED_MEM_SIZE}\"
175DOMU_SHARED_MEM[1]=\"${SHARED_MEM_ID} 0x${SHARED_MEM_HOST} 0x${SHARED_MEM_GUEST} 0x${SHARED_MEM_SIZE}\"" >> binaries/config
176fi
177
178if [[ "${test_variant}" == "static-heap" ]]; then
179    # ImageBuilder uses the config file to create the uboot script. Devicetree
180    # will be set via the generated uboot script.
181    # The valid memory range is 0x40000000 to 0x80000000 as defined before.
182    # ImageBuillder sets the kernel and ramdisk range based on the file size.
183    # It will use the memory range between 0x45600000 to 0x47AED1E8, and
184    # MEMORY_END has been set to 0x50000000 above, so set memory range between
185    # 0x50000000 and 0x80000000 as static heap.
186    echo  '
187XEN_STATIC_HEAP="0x50000000 0x30000000"
188# The size of static heap should be greater than the guest memory
189DOMU_MEM[0]="128"' >> binaries/config
190fi
191
192if [[ "${test_variant}" == "boot-cpupools" ]]; then
193    echo '
194CPUPOOL[0]="cpu@1 null"
195DOMU_CPUPOOL[0]=0
196NUM_CPUPOOLS=1' >> binaries/config
197fi
198
199rm -rf imagebuilder
200git clone --depth 1 https://gitlab.com/xen-project/imagebuilder.git
201bash imagebuilder/scripts/uboot-script-gen -t tftp -d binaries/ -c binaries/config
202
203
204# Run the test
205rm -f smoke.serial
206export TEST_CMD="./binaries/qemu-system-aarch64 \
207    -machine virtualization=true \
208    -cpu cortex-a57 -machine type=virt,gic-version=$gic_version \
209    -m 2048 -monitor none -serial stdio \
210    -smp 2 \
211    -no-reboot \
212    -device virtio-net-pci,netdev=n0 \
213    -netdev user,id=n0,tftp=binaries \
214    -bios /usr/lib/u-boot/qemu_arm64/u-boot.bin"
215
216export UBOOT_CMD="virtio scan; dhcp; tftpb 0x40000000 boot.scr; source 0x40000000"
217export TEST_LOG="smoke.serial"
218export LOG_MSG="Welcome to Alpine Linux"
219export PASSED="${passed}"
220
221./automation/scripts/console.exp | sed 's/\r\+$//'
222