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