1#!/usr/bin/env python3
2#
3# Test for multiprocess qemu
4#
5# This work is licensed under the terms of the GNU GPL, version 2 or
6# later.  See the COPYING file in the top-level directory.
7
8
9import os
10import socket
11
12from qemu_test import QemuSystemTest, Asset, wait_for_console_pattern
13from qemu_test import exec_command, exec_command_and_wait_for_pattern
14
15class Multiprocess(QemuSystemTest):
16
17    KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
18
19    ASSET_KERNEL_X86 = Asset(
20        ('https://archives.fedoraproject.org/pub/archive/fedora/linux'
21         '/releases/31/Everything/x86_64/os/images/pxeboot/vmlinuz'),
22        'd4738d03dbbe083ca610d0821d0a8f1488bebbdccef54ce33e3adb35fda00129')
23
24    ASSET_INITRD_X86 = Asset(
25        ('https://archives.fedoraproject.org/pub/archive/fedora/linux'
26         '/releases/31/Everything/x86_64/os/images/pxeboot/initrd.img'),
27        '3b6cb5c91a14c42e2f61520f1689264d865e772a1f0069e660a800d31dd61fb9')
28
29    ASSET_KERNEL_AARCH64 = Asset(
30        ('https://archives.fedoraproject.org/pub/archive/fedora/linux'
31         '/releases/31/Everything/aarch64/os/images/pxeboot/vmlinuz'),
32        '3ae07fcafbfc8e4abeb693035a74fe10698faae15e9ccd48882a9167800c1527')
33
34    ASSET_INITRD_AARCH64 = Asset(
35        ('https://archives.fedoraproject.org/pub/archive/fedora/linux'
36         '/releases/31/Everything/aarch64/os/images/pxeboot/initrd.img'),
37        '9fd230cab10b1dafea41cf00150e6669d37051fad133bd618d2130284e16d526')
38
39    def do_test(self, kernel_asset, initrd_asset,
40                kernel_command_line, machine_type):
41        """Main test method"""
42        self.require_accelerator('kvm')
43        self.require_device('x-pci-proxy-dev')
44
45        # Create socketpair to connect proxy and remote processes
46        proxy_sock, remote_sock = socket.socketpair(socket.AF_UNIX,
47                                                    socket.SOCK_STREAM)
48        os.set_inheritable(proxy_sock.fileno(), True)
49        os.set_inheritable(remote_sock.fileno(), True)
50
51        kernel_path = kernel_asset.fetch()
52        initrd_path = initrd_asset.fetch()
53
54        # Create remote process
55        remote_vm = self.get_vm()
56        remote_vm.add_args('-machine', 'x-remote')
57        remote_vm.add_args('-nodefaults')
58        remote_vm.add_args('-device', 'lsi53c895a,id=lsi1')
59        remote_vm.add_args('-object', 'x-remote-object,id=robj1,'
60                           'devid=lsi1,fd='+str(remote_sock.fileno()))
61        remote_vm.launch()
62
63        # Create proxy process
64        self.vm.set_console()
65        self.vm.add_args('-machine', machine_type)
66        self.vm.add_args('-accel', 'kvm')
67        self.vm.add_args('-cpu', 'host')
68        self.vm.add_args('-object',
69                         'memory-backend-memfd,id=sysmem-file,size=2G')
70        self.vm.add_args('--numa', 'node,memdev=sysmem-file')
71        self.vm.add_args('-m', '2048')
72        self.vm.add_args('-kernel', kernel_path,
73                         '-initrd', initrd_path,
74                         '-append', kernel_command_line)
75        self.vm.add_args('-device',
76                         'x-pci-proxy-dev,'
77                         'id=lsi1,fd='+str(proxy_sock.fileno()))
78        self.vm.launch()
79        wait_for_console_pattern(self, 'as init process',
80                                 'Kernel panic - not syncing')
81        exec_command(self, 'mount -t sysfs sysfs /sys')
82        exec_command_and_wait_for_pattern(self,
83                                          'cat /sys/bus/pci/devices/*/uevent',
84                                          'PCI_ID=1000:0012')
85
86    def test_multiprocess(self):
87        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE
88        if self.arch == 'x86_64':
89            kernel_command_line += 'console=ttyS0 rdinit=/bin/bash'
90            self.do_test(self.ASSET_KERNEL_X86, self.ASSET_INITRD_X86,
91                         kernel_command_line, 'pc')
92        elif self.arch == 'aarch64':
93            kernel_command_line += 'rdinit=/bin/bash console=ttyAMA0'
94            self.do_test(self.ASSET_KERNEL_AARCH64, self.ASSET_INITRD_AARCH64,
95                         kernel_command_line, 'virt,gic-version=3')
96        else:
97            assert False
98
99if __name__ == '__main__':
100    QemuSystemTest.main()
101