[PATCH 11/40] nitro_enclaves: Add NE_ENCLAVE_PCIE_MODE launch flag
From: Alexander Graf
Date: Wed Nov 11 2026 - 18:05:21 EST
Nitro Sandboxes give their guest virtio devices that the parent
provides, on a PCIe bus. The hypervisor sets up that bus when it
allocates the slot, so user space asks for it with a launch flag.
Define NE_ENCLAVE_PCIE_MODE, the first launch flag, and forward it in
SLOT_ALLOC. struct slot_alloc_req gains a flag word, and at offset 0,
over the unused field, a payload_version byte that tells the device to
read it. We send version 1 when a flag is set, and the all-zero request
of today otherwise.
A device that takes launch flags publishes a vendor-specific PCI
capability. A device that predates them ignores the flag word and
allocates a plain slot, so refuse a launch flag with -EOPNOTSUPP on a
device without the capability.
Signed-off-by: Alexander Graf <graf@xxxxxxxxxx>
---
drivers/virt/nitro_enclaves/ne_misc_dev.c | 19 ++++++++++++++++++-
drivers/virt/nitro_enclaves/ne_pci_dev.h | 23 +++++++++++++++++++++--
include/uapi/linux/nitro_enclaves.h | 14 ++++++++++++++
3 files changed, 53 insertions(+), 3 deletions(-)
diff --git a/drivers/virt/nitro_enclaves/ne_misc_dev.c b/drivers/virt/nitro_enclaves/ne_misc_dev.c
index 68c497df4f14b..0bf62f703806e 100644
--- a/drivers/virt/nitro_enclaves/ne_misc_dev.c
+++ b/drivers/virt/nitro_enclaves/ne_misc_dev.c
@@ -2475,13 +2475,30 @@ static int ne_create_vm_ioctl(struct ne_pci_dev *ne_pci_dev, u64 flags,
int rc = -EINVAL;
struct slot_alloc_req slot_alloc_req = {};
- if (flags) {
+ if (flags & ~(u64)NE_ENCLAVE_PCIE_MODE) {
dev_err_ratelimited(ne_misc_dev.this_device,
"Unknown launch flag bit set 0x%llx\n", flags);
return -NE_ERR_INVALID_FLAG_VALUE;
}
+ /*
+ * A device that takes launch flags publishes a vendor-specific
+ * capability. One without it takes no launch flags and would allocate a
+ * slot without the properties they ask for.
+ */
+ if (flags && !pci_find_capability(ne_pci_dev->pdev, PCI_CAP_ID_VNDR)) {
+ dev_err_ratelimited(ne_misc_dev.this_device,
+ "Device takes no launch flags\n");
+
+ return -EOPNOTSUPP;
+ }
+
+ if (flags) {
+ slot_alloc_req.payload_version = 1;
+ slot_alloc_req.flags = flags;
+ }
+
mutex_lock(&ne_cpu_pool.mutex);
for (i = 0; i < ne_cpu_pool.nr_parent_vm_cores; i++)
diff --git a/drivers/virt/nitro_enclaves/ne_pci_dev.h b/drivers/virt/nitro_enclaves/ne_pci_dev.h
index 19e129d14a3a0..b0957e2606ec3 100644
--- a/drivers/virt/nitro_enclaves/ne_pci_dev.h
+++ b/drivers/virt/nitro_enclaves/ne_pci_dev.h
@@ -7,10 +7,12 @@
#define _NE_PCI_DEV_H_
#include <linux/atomic.h>
+#include <linux/build_bug.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/pci.h>
#include <linux/pci_ids.h>
+#include <linux/stddef.h>
#include <linux/wait.h>
/**
@@ -174,12 +176,29 @@ struct enclave_stop_req {
/**
* struct slot_alloc_req - SLOT_ALLOC request.
- * @unused: In order to avoid weird sizeof edge cases.
+ * @payload_version: Wire version at offset 0. Zero is the value a driver
+ * without launch flags leaves here, and tells the device
+ * to disregard @reserved and @flags. 1 means @flags is
+ * valid.
+ * @reserved: Alignment padding between @payload_version and
+ * @flags. Zero.
+ * @flags: Launch flag word from NE_CREATE_VM2, valid when
+ * @payload_version >= 1.
*/
struct slot_alloc_req {
- u8 unused;
+ u8 payload_version;
+ u8 reserved[3];
+ u32 flags;
};
+/*
+ * The device picks these fields out of the payload by offset, so a change
+ * to the layout here changes the SLOT_ALLOC wire format.
+ */
+static_assert(sizeof(struct slot_alloc_req) == 8);
+static_assert(offsetof(struct slot_alloc_req, reserved) == 1);
+static_assert(offsetof(struct slot_alloc_req, flags) == 4);
+
/**
* struct slot_free_req - SLOT_FREE request.
* @slot_uid: Slot unique id mapped to the slot to free.
diff --git a/include/uapi/linux/nitro_enclaves.h b/include/uapi/linux/nitro_enclaves.h
index 8e43be9a0a1fa..8ccf16e9cf5fb 100644
--- a/include/uapi/linux/nitro_enclaves.h
+++ b/include/uapi/linux/nitro_enclaves.h
@@ -46,6 +46,18 @@
*/
#define NE_CREATE_VM _IOR(0xAE, 0x20, __u64)
+/**
+ * DOC: NE_CREATE_VM2 launch flags
+ */
+
+/**
+ * NE_ENCLAVE_PCIE_MODE - Allocate a slot with a PCIe bus. The
+ * hypervisor selects PCIe backing for the slot at
+ * SLOT_ALLOC time, so a caller that wants it asks
+ * for it here and not at enclave start.
+ */
+#define NE_ENCLAVE_PCIE_MODE (1u << 6)
+
/**
* struct ne_create_vm_args - NE_CREATE_VM2 argument (in / out).
* @flags: Launch-time flag word (in). Flags select properties of the
@@ -82,6 +94,8 @@ struct ne_create_vm_args {
* * EFAULT - Argument read or write failure.
* * NE_ERR_INVALID_FLAG_VALUE - Unknown flag bit set, or
* NE_ENCLAVE_DEBUG_MODE set.
+ * * EOPNOTSUPP - A launch flag is set and the NE PCI
+ * device takes no launch flags.
* * EINVAL - The argument stops before @slot_uid.
* * E2BIG - The argument carries a field this kernel
* does not know, set to a non-zero value.
--
2.47.1