| 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 | |
| 13 | import logging |
| 14 | from subprocess import check_call, DEVNULL |
| 15 | |
| 16 | from qemu_test import QemuSystemTest, Asset, exec_command_and_wait_for_pattern |
| 17 | from qemu_test import wait_for_console_pattern, get_qemu_img |
| 18 | |
| 19 | |
| 20 | class 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 | kernel_path = self.ASSET_KERNEL.fetch() |
| 75 | |
| 76 | self.vm.set_console() |
| 77 | kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE + |
| 78 | 'console=ttyAMA0') |
| 79 | self.vm.add_args('-cpu', 'max', |
| 80 | '-machine', machine, |
| 81 | '-accel', 'tcg', |
| 82 | '-kernel', kernel_path, |
| 83 | '-append', kernel_command_line) |
| 84 | |
| 85 | # A RNG offers an easy way to generate a few IRQs |
| 86 | self.vm.add_args('-device', 'virtio-rng-pci,rng=rng0') |
| 87 | self.vm.add_args('-object', |
| 88 | 'rng-random,id=rng0,filename=/dev/urandom') |
| 89 | |
| 90 | # Also add a scratch block device |
| 91 | self.log.info('creating scratch qcow2 image') |
| 92 | image_path = self.scratch_file('scratch.qcow2') |
| 93 | qemu_img = get_qemu_img(self) |
| 94 | check_call([qemu_img, 'create', '-f', 'qcow2', image_path, '8M'], |
| 95 | stdout=DEVNULL, stderr=DEVNULL) |
| 96 | |
| 97 | # Add the device |
| 98 | self.vm.add_args('-blockdev', |
| 99 | "driver=qcow2," |
| 100 | "file.driver=file," |
| 101 | f"file.filename={image_path},node-name=scratch") |
| 102 | self.vm.add_args('-device', |
| 103 | 'virtio-blk-device,drive=scratch') |
| 104 | |
| 105 | self.vm.launch() |
| 106 | |
| 107 | ps1='#' |
| 108 | self.wait_for_console_pattern('login:') |
| 109 | |
| 110 | commands = [ |
| 111 | ('root', ps1), |
| 112 | ('cat /proc/interrupts', ps1), |
| 113 | ('cat /proc/self/maps', ps1), |
| 114 | ('uname -a', ps1), |
| 115 | ('dd if=/dev/hwrng of=/dev/vda bs=512 count=4', ps1), |
| 116 | ('md5sum /dev/vda', ps1), |
| 117 | ('halt -n', 'reboot: System halted') |
| 118 | ] |
| 119 | |
| 120 | for cmd, pattern in commands: |
| 121 | exec_command_and_wait_for_pattern(self, cmd, pattern) |
| 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 | |
| 130 | |
| 131 | if __name__ == '__main__': |
| 132 | QemuSystemTest.main() |