| 
 | 1 | +# Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.  | 
 | 2 | +# SPDX-License-Identifier: Apache-2.0  | 
 | 3 | +"""Test secret-freedom related functionality."""  | 
 | 4 | + | 
 | 5 | +import platform  | 
 | 6 | + | 
 | 7 | +import pytest  | 
 | 8 | + | 
 | 9 | +from framework import defs  | 
 | 10 | +from framework.microvm import Serial  | 
 | 11 | +from framework.properties import global_props  | 
 | 12 | +from integration_tests.performance.test_initrd import INITRD_FILESYSTEM  | 
 | 13 | + | 
 | 14 | +pytestmark = [  | 
 | 15 | +    pytest.mark.skipif(  | 
 | 16 | +        global_props.host_linux_version_metrics != "next",  | 
 | 17 | +        reason="Secret Freedom is only supported on the in-dev upstream kernels for now",  | 
 | 18 | +    ),  | 
 | 19 | +    pytest.mark.skipif(  | 
 | 20 | +        global_props.instance == "m6g.metal",  | 
 | 21 | +        reason="Secret Freedom currently only works on ARM hardware conforming to at least ARMv8.4 as absense of ARM64_HAS_STAGE2_FWB causes kernel panics because of dcache flushing during stage2 page table entry installation",  | 
 | 22 | +    ),  | 
 | 23 | +]  | 
 | 24 | + | 
 | 25 | + | 
 | 26 | +@pytest.mark.skipif(  | 
 | 27 | +    platform.machine() != "aarch64",  | 
 | 28 | +    reason="only ARM can boot secret free VMs with I/O devices",  | 
 | 29 | +)  | 
 | 30 | +def test_secret_free_boot(microvm_factory, guest_kernel_linux_6_1, rootfs):  | 
 | 31 | +    """Tests that a VM can boot if all virtio devices are bound to a swiotlb region, and  | 
 | 32 | +    that this swiotlb region is actually discovered by the guest."""  | 
 | 33 | +    vm = microvm_factory.build(guest_kernel_linux_6_1, rootfs)  | 
 | 34 | +    vm.spawn()  | 
 | 35 | +    vm.memory_monitor = None  | 
 | 36 | +    vm.basic_config(memory_config={"initial_swiotlb_size": 8, "secret_free": True})  | 
 | 37 | +    vm.add_net_iface()  | 
 | 38 | +    vm.start()  | 
 | 39 | + | 
 | 40 | + | 
 | 41 | +def test_secret_free_initrd(microvm_factory, guest_kernel):  | 
 | 42 | +    """  | 
 | 43 | +    Test that we can boot a secret hidden initrd (e.g. a VM with no I/O devices)  | 
 | 44 | +    """  | 
 | 45 | +    fs = defs.ARTIFACT_DIR / "initramfs.cpio"  | 
 | 46 | +    uvm = microvm_factory.build(guest_kernel)  | 
 | 47 | +    uvm.initrd_file = fs  | 
 | 48 | +    uvm.help.enable_console()  | 
 | 49 | +    uvm.spawn()  | 
 | 50 | +    uvm.memory_monitor = None  | 
 | 51 | + | 
 | 52 | +    uvm.basic_config(  | 
 | 53 | +        add_root_device=False,  | 
 | 54 | +        vcpu_count=1,  | 
 | 55 | +        boot_args="console=ttyS0 reboot=k panic=1 pci=off no-kvmclock",  | 
 | 56 | +        use_initrd=True,  | 
 | 57 | +        memory_config={"initial_swiotlb_size": 64, "secret_free": True},  | 
 | 58 | +    )  | 
 | 59 | + | 
 | 60 | +    uvm.start()  | 
 | 61 | +    serial = Serial(uvm)  | 
 | 62 | +    serial.open()  | 
 | 63 | +    serial.rx(token="# ")  | 
 | 64 | +    serial.tx("mount |grep rootfs")  | 
 | 65 | +    serial.rx(token=f"rootfs on / type {INITRD_FILESYSTEM}")  | 
 | 66 | + | 
 | 67 | + | 
 | 68 | +@pytest.mark.skipif(  | 
 | 69 | +    platform.machine() != "aarch64",  | 
 | 70 | +    reason="only ARM can boot secret free VMs with I/O devices",  | 
 | 71 | +)  | 
 | 72 | +def test_secret_free_snapshot_creation(microvm_factory, guest_kernel_linux_6_1, rootfs):  | 
 | 73 | +    """Test that snapshot creation works for secret hidden VMs"""  | 
 | 74 | +    vm = microvm_factory.build(guest_kernel_linux_6_1, rootfs)  | 
 | 75 | +    vm.spawn()  | 
 | 76 | +    vm.memory_monitor = None  | 
 | 77 | +    vm.basic_config(memory_config={"initial_swiotlb_size": 8, "secret_free": True})  | 
 | 78 | +    vm.add_net_iface()  | 
 | 79 | +    vm.start()  | 
 | 80 | + | 
 | 81 | +    snapshot = vm.snapshot_full()  | 
 | 82 | + | 
 | 83 | +    # After restoration, the VM will not be secret hidden anymore, as that's not supported yet.  | 
 | 84 | +    # But we can at least test that in principle, the snapshot creation worked.  | 
 | 85 | +    vm = microvm_factory.build_from_snapshot(snapshot)  | 
 | 86 | +    vm.ssh.check_output("true")  | 
0 commit comments