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