xref: /qemu/tests/functional/test_aarch64_virt.py (revision 0d3af961f751e0b424828d25e2ca47bc8729485c)
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_test import QemuSystemTest, Asset
17from qemu_test import exec_command_and_wait_for_pattern
18from qemu_test import wait_for_console_pattern
19from qemu_test import get_qemu_img
20
21
22class Aarch64VirtMachine(QemuSystemTest):
23    KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
24    timeout = 360
25
26    def wait_for_console_pattern(self, success_message, vm=None):
27        wait_for_console_pattern(self, success_message,
28                                 failure_message='Kernel panic - not syncing',
29                                 vm=vm)
30
31    ASSET_ALPINE_ISO = Asset(
32        ('https://dl-cdn.alpinelinux.org/'
33         'alpine/v3.17/releases/aarch64/alpine-standard-3.17.2-aarch64.iso'),
34        '5a36304ecf039292082d92b48152a9ec21009d3a62f459de623e19c4bd9dc027')
35
36    # This tests the whole boot chain from EFI to Userspace
37    # We only boot a whole OS for the current top level CPU and GIC
38    # Other test profiles should use more minimal boots
39    def test_alpine_virt_tcg_gic_max(self):
40        iso_path = self.ASSET_ALPINE_ISO.fetch()
41
42        self.set_machine('virt')
43        self.require_accelerator("tcg")
44
45        self.vm.set_console()
46        self.vm.add_args("-accel", "tcg")
47        self.vm.add_args("-cpu", "max,pauth-impdef=on")
48        self.vm.add_args("-machine",
49                         "virt,acpi=on,"
50                         "virtualization=on,"
51                         "mte=on,"
52                         "gic-version=max,iommu=smmuv3")
53        self.vm.add_args("-smp", "2", "-m", "1024")
54        self.vm.add_args('-bios', self.build_file('pc-bios',
55                                                  'edk2-aarch64-code.fd'))
56        self.vm.add_args("-drive", f"file={iso_path},media=cdrom,format=raw")
57        self.vm.add_args('-device', 'virtio-rng-pci,rng=rng0')
58        self.vm.add_args('-object', 'rng-random,id=rng0,filename=/dev/urandom')
59
60        self.vm.launch()
61        self.wait_for_console_pattern('Welcome to Alpine Linux 3.17')
62
63
64    ASSET_KERNEL = Asset(
65        ('https://fileserver.linaro.org/s/'
66         'z6B2ARM7DQT3HWN/download'),
67        '12a54d4805cda6ab647cb7c7bbdb16fafb3df400e0d6f16445c1a0436100ef8d')
68
69    def common_aarch64_virt(self, machine):
70        """
71        Common code to launch basic virt machine with kernel+initrd
72        and a scratch disk.
73        """
74        self.set_machine('virt')
75        self.require_accelerator("tcg")
76
77        logger = logging.getLogger('aarch64_virt')
78
79        kernel_path = self.ASSET_KERNEL.fetch()
80
81        self.vm.set_console()
82        kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE +
83                               'console=ttyAMA0')
84        self.vm.add_args('-cpu', 'max,pauth-impdef=on',
85                         '-machine', machine,
86                         '-accel', 'tcg',
87                         '-kernel', kernel_path,
88                         '-append', kernel_command_line)
89
90        # A RNG offers an easy way to generate a few IRQs
91        self.vm.add_args('-device', 'virtio-rng-pci,rng=rng0')
92        self.vm.add_args('-object',
93                         'rng-random,id=rng0,filename=/dev/urandom')
94
95        # Also add a scratch block device
96        logger.info('creating scratch qcow2 image')
97        image_path = self.scratch_file('scratch.qcow2')
98        qemu_img = get_qemu_img(self)
99        check_call([qemu_img, 'create', '-f', 'qcow2', image_path, '8M'],
100                   stdout=DEVNULL, stderr=DEVNULL)
101
102        # Add the device
103        self.vm.add_args('-blockdev',
104                         "driver=qcow2,"
105                         "file.driver=file,"
106                         f"file.filename={image_path},node-name=scratch")
107        self.vm.add_args('-device',
108                         'virtio-blk-device,drive=scratch')
109
110        self.vm.launch()
111
112        ps1='#'
113        self.wait_for_console_pattern('login:')
114
115        commands = [
116            ('root', ps1),
117            ('cat /proc/interrupts', ps1),
118            ('cat /proc/self/maps', ps1),
119            ('uname -a', ps1),
120            ('dd if=/dev/hwrng of=/dev/vda bs=512 count=4', ps1),
121            ('md5sum /dev/vda', ps1),
122            ('halt -n', 'reboot: System halted')
123        ]
124
125        for cmd, pattern in commands:
126            exec_command_and_wait_for_pattern(self, cmd, pattern)
127
128    def test_aarch64_virt_gicv3(self):
129        self.common_aarch64_virt("virt,gic_version=3")
130
131    def test_aarch64_virt_gicv2(self):
132        self.common_aarch64_virt("virt,gic-version=2")
133
134
135if __name__ == '__main__':
136    QemuSystemTest.main()
137