xref: /qemu/tests/functional/test_aarch64_virt.py (revision 513823e7521a09ed7ad1e32e6454bac3b2cbf52d)
1#!/usr/bin/env python3
2#
3# Functional test that boots a various Linux systems and checks the
4# console output.
5#
6# Copyright (c) 2022 Linaro Ltd.
7#
8# Author:
9#  Alex Bennée <alex.bennee@linaro.org>
10#
11# SPDX-License-Identifier: GPL-2.0-or-later
12
13import logging
14from subprocess import check_call, DEVNULL
15
16from qemu.machine.machine import VMLaunchFailure
17
18from qemu_test import QemuSystemTest, Asset
19from qemu_test import exec_command, exec_command_and_wait_for_pattern
20from qemu_test import wait_for_console_pattern
21from qemu_test import skipIfMissingCommands, get_qemu_img
22
23
24class Aarch64VirtMachine(QemuSystemTest):
25    KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
26    timeout = 360
27
28    def wait_for_console_pattern(self, success_message, vm=None):
29        wait_for_console_pattern(self, success_message,
30                                 failure_message='Kernel panic - not syncing',
31                                 vm=vm)
32
33    ASSET_ALPINE_ISO = Asset(
34        ('https://dl-cdn.alpinelinux.org/'
35         'alpine/v3.17/releases/aarch64/alpine-standard-3.17.2-aarch64.iso'),
36        '5a36304ecf039292082d92b48152a9ec21009d3a62f459de623e19c4bd9dc027')
37
38    # This tests the whole boot chain from EFI to Userspace
39    # We only boot a whole OS for the current top level CPU and GIC
40    # Other test profiles should use more minimal boots
41    def test_alpine_virt_tcg_gic_max(self):
42        iso_path = self.ASSET_ALPINE_ISO.fetch()
43
44        self.set_machine('virt')
45        self.require_accelerator("tcg")
46
47        self.vm.set_console()
48        self.vm.add_args("-accel", "tcg")
49        self.vm.add_args("-cpu", "max,pauth-impdef=on")
50        self.vm.add_args("-machine",
51                         "virt,acpi=on,"
52                         "virtualization=on,"
53                         "mte=on,"
54                         "gic-version=max,iommu=smmuv3")
55        self.vm.add_args("-smp", "2", "-m", "1024")
56        self.vm.add_args('-bios', self.build_file('pc-bios',
57                                                  'edk2-aarch64-code.fd'))
58        self.vm.add_args("-drive", f"file={iso_path},media=cdrom,format=raw")
59        self.vm.add_args('-device', 'virtio-rng-pci,rng=rng0')
60        self.vm.add_args('-object', 'rng-random,id=rng0,filename=/dev/urandom')
61
62        self.vm.launch()
63        self.wait_for_console_pattern('Welcome to Alpine Linux 3.17')
64
65
66    ASSET_KERNEL = Asset(
67        ('https://fileserver.linaro.org/s/'
68         'z6B2ARM7DQT3HWN/download'),
69        '12a54d4805cda6ab647cb7c7bbdb16fafb3df400e0d6f16445c1a0436100ef8d')
70
71    def common_aarch64_virt(self, machine):
72        """
73        Common code to launch basic virt machine with kernel+initrd
74        and a scratch disk.
75        """
76        self.set_machine('virt')
77        self.require_accelerator("tcg")
78
79        logger = logging.getLogger('aarch64_virt')
80
81        kernel_path = self.ASSET_KERNEL.fetch()
82
83        self.vm.set_console()
84        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
85                               'console=ttyAMA0')
86        self.vm.add_args('-cpu', 'max,pauth-impdef=on',
87                         '-machine', machine,
88                         '-accel', 'tcg',
89                         '-kernel', kernel_path,
90                         '-append', kernel_command_line)
91
92        # A RNG offers an easy way to generate a few IRQs
93        self.vm.add_args('-device', 'virtio-rng-pci,rng=rng0')
94        self.vm.add_args('-object',
95                         'rng-random,id=rng0,filename=/dev/urandom')
96
97        # Also add a scratch block device
98        logger.info('creating scratch qcow2 image')
99        image_path = self.scratch_file('scratch.qcow2')
100        qemu_img = get_qemu_img(self)
101        check_call([qemu_img, 'create', '-f', 'qcow2', image_path, '8M'],
102                   stdout=DEVNULL, stderr=DEVNULL)
103
104        # Add the device
105        self.vm.add_args('-blockdev',
106                         "driver=qcow2,"
107                         "file.driver=file,"
108                         f"file.filename={image_path},node-name=scratch")
109        self.vm.add_args('-device',
110                         'virtio-blk-device,drive=scratch')
111
112        self.vm.launch()
113
114        ps1='#'
115        self.wait_for_console_pattern('login:')
116
117        commands = [
118            ('root', ps1),
119            ('cat /proc/interrupts', ps1),
120            ('cat /proc/self/maps', ps1),
121            ('uname -a', ps1),
122            ('dd if=/dev/hwrng of=/dev/vda bs=512 count=4', ps1),
123            ('md5sum /dev/vda', ps1),
124            ('halt -n', 'reboot: System halted')
125        ]
126
127        for cmd, pattern in commands:
128            exec_command_and_wait_for_pattern(self, cmd, pattern)
129
130    def test_aarch64_virt_gicv3(self):
131        self.common_aarch64_virt("virt,gic_version=3")
132
133    def test_aarch64_virt_gicv2(self):
134        self.common_aarch64_virt("virt,gic-version=2")
135
136
137    ASSET_VIRT_GPU_KERNEL = Asset(
138        'https://fileserver.linaro.org/s/ce5jXBFinPxtEdx/'
139        'download?path=%2F&files='
140        'Image',
141        '89e5099d26166204cc5ca4bb6d1a11b92c217e1f82ec67e3ba363d09157462f6')
142
143    ASSET_VIRT_GPU_ROOTFS = Asset(
144        'https://fileserver.linaro.org/s/ce5jXBFinPxtEdx/'
145        'download?path=%2F&files='
146        'rootfs.ext4.zstd',
147        '792da7573f5dc2913ddb7c638151d4a6b2d028a4cb2afb38add513c1924bdad4')
148
149    @skipIfMissingCommands('zstd')
150    def test_aarch64_virt_with_gpu(self):
151        # This tests boots with a buildroot test image that contains
152        # vkmark and other GPU exercising tools. We run a headless
153        # weston that nevertheless still exercises the virtio-gpu
154        # backend.
155
156        self.set_machine('virt')
157        self.require_accelerator("tcg")
158
159        kernel_path = self.ASSET_VIRT_GPU_KERNEL.fetch()
160        image_path = self.uncompress(self.ASSET_VIRT_GPU_ROOTFS, format="zstd")
161
162        self.vm.set_console()
163        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
164                               'console=ttyAMA0 root=/dev/vda')
165
166        self.vm.add_args("-accel", "tcg")
167        self.vm.add_args("-cpu", "neoverse-v1,pauth-impdef=on")
168        self.vm.add_args("-machine", "virt,gic-version=max",
169                         '-kernel', kernel_path,
170                         '-append', kernel_command_line)
171        self.vm.add_args("-smp", "2", "-m", "2048")
172        self.vm.add_args("-device",
173                         "virtio-gpu-gl-pci,hostmem=4G,blob=on,venus=on")
174        self.vm.add_args("-display", "egl-headless")
175        self.vm.add_args("-display", "dbus,gl=on")
176        self.vm.add_args("-device", "virtio-blk-device,drive=hd0")
177        self.vm.add_args("-blockdev",
178                         "driver=raw,file.driver=file,"
179                         "node-name=hd0,read-only=on,"
180                         f"file.filename={image_path}")
181        self.vm.add_args("-snapshot")
182
183        try:
184            self.vm.launch()
185        except VMLaunchFailure as excp:
186            if "old virglrenderer, blob resources unsupported" in excp.output:
187                self.skipTest("No blob support for virtio-gpu")
188            elif "old virglrenderer, venus unsupported" in excp.output:
189                self.skipTest("No venus support for virtio-gpu")
190            elif "egl: no drm render node available" in excp.output:
191                self.skipTest("Can't access host DRM render node")
192            elif "'type' does not accept value 'egl-headless'" in excp.output:
193                self.skipTest("egl-headless support is not available")
194            else:
195                self.log.info(f"unhandled launch failure: {excp.output}")
196                raise excp
197
198        self.wait_for_console_pattern('buildroot login:')
199        exec_command(self, 'root')
200        exec_command(self, 'export XDG_RUNTIME_DIR=/tmp')
201        exec_command_and_wait_for_pattern(self,
202                                          "weston -B headless "
203                                          "--renderer gl "
204                                          "--shell kiosk "
205                                          "-- vkmark -b:duration=1.0",
206                                          "vkmark Score")
207
208
209if __name__ == '__main__':
210    QemuSystemTest.main()
211