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