|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [RFC PATCH 6/7] xeniface: Expose batch foreign page mappings to user mode
From: Rafał Wojdyła <omeg@xxxxxxx>
Add buffered IOCTLs and synchronous xencontrol wrappers for creating
a mapping, mapping and unmapping batches, obtaining a user view, destroying
an empty mapping and transferring whole mapping cleanup to the driver.
Use opaque 64-bit IDs scoped to the creating process and open file object.
Validate counts, ranges, flags and exact buffer lengths, and deliver batch
status separately from page-level NTSTATUS values. Keep successful maps
after partial failures so clients can retry only failed slots; unmaps may
retry the original range and revoke the whole user view before any attempt.
Fix notification settings at creation and retain channels owned by the same
file. Clear the notification byte before removing a view and notify the
retained channel only after mapping destruction, including deferred cleanup.
Allow UnmapPages to invalidate the ID and hand remaining cleanup and retries
to the driver for empty, partial or full mappings of any supported size.
Success acknowledges release ownership, not completion of all Xen unmaps;
port notification follows destruction of that mapping only. Keep the result-
bearing batch teardown operations available for explicit continuation.
Keep the xencontrol buffers and OVERLAPPED event alive until completion, and
release a returned ID if response validation fails.
Assisted-by: LLM
Signed-off-by: Rafał Wojdyła <omeg@xxxxxxxxxxxxxxxxxxxxxx>
---
include/xencontrol.h | 144 ++++++++++++++++++
include/xeniface_ioctls.h | 160 +++++++++++++++++++-
src/xencontrol/xencontrol.c | 209 ++++++++++++++++++++++++++
src/xeniface/ioctl_gnttab.c | 286 ++++++++++++++++++++++++++++++++++++
src/xeniface/ioctls.c | 28 +++-
src/xeniface/ioctls.h | 65 ++++++++
6 files changed, 890 insertions(+), 2 deletions(-)
diff --git a/include/xencontrol.h b/include/xencontrol.h
index 4b916ec..348bc44 100644
--- a/include/xencontrol.h
+++ b/include/xencontrol.h
@@ -263,6 +263,150 @@ XcGnttabUnmapForeignPages(
_In_ PVOID Address
);
+/*! \brief Create an empty mapping for foreign memory pages
+ \param Xc Xencontrol handle returned by XcOpen()
+ \param RemoteDomain ID of a remote domain that has granted access to the
pages
+ \param NumberPages Number of 4k pages in the mapping, from 1 to
XENIFACE_GNTTAB_MAPPING_COUNT_MAX
+ \param NotifyOffset Offset of a byte in the mapped region that will be set
to 0 when the view is removed, if enabled
+ \param NotifyPort Local port number of an open event channel that will be
notified after mapping destruction, if enabled
+ \param Flags Map and notification options
+ \param Id Opaque mapping ID
+ \return Win32 error code
+
+ The batched mapping functions complete synchronously. Mapping IDs are
scoped
+ to \a Xc and its creating process. Populate the mapping with
XcGnttabMapBatch(),
+ then obtain a user-mode view with XcGnttabCreateUserView(). The caller must
+ stop all memory accesses before releasing the entire mapping with
+ XcGnttabUnmapPages(). Closing \a Xc also releases every remaining mapping
+ through the driver's cleanup/retry path.
+
+ The notification byte is cleared when a view is removed, and the retained
+ event channel is notified after the mapping is destroyed.
+*/
+XENCONTROL_API
+_Success_(return == ERROR_SUCCESS)
+DWORD
+XcGnttabCreateMapping(
+ _In_ PXENCONTROL_CONTEXT Xc,
+ _In_ USHORT RemoteDomain,
+ _In_ ULONG NumberPages,
+ _In_ ULONG NotifyOffset,
+ _In_ ULONG NotifyPort,
+ _In_ XENIFACE_GNTTAB_PAGE_FLAGS Flags,
+ _Out_ ULONGLONG *Id
+ );
+
+/*! \brief Map a batch of foreign pages into an existing mapping
+ \param Xc Xencontrol handle returned by XcOpen()
+ \param Id Mapping ID returned by XcGnttabCreateMapping()
+ \param Offset Starting page index in the mapping
+ \param NumberPages Number of 4k pages to map, from 1 to
XENIFACE_GNTTAB_BATCH_COUNT_MAX
+ \param References An array of Xen grant numbers for every page in the batch
+ \param BatchStatus NTSTATUS value for batch processing
+ \param Results An array of page-level hypercall NTSTATUS values for every
page in the batch
+ \return Win32 error code
+
+ On ERROR_SUCCESS, \a BatchStatus and \a Results contain NTSTATUS values,
+ including partial failures. Map only failed slots when retrying a map.
+*/
+XENCONTROL_API
+_Success_(return == ERROR_SUCCESS)
+DWORD
+XcGnttabMapBatch(
+ _In_ PXENCONTROL_CONTEXT Xc,
+ _In_ ULONGLONG Id,
+ _In_ ULONG Offset,
+ _In_ ULONG NumberPages,
+ _In_reads_(NumberPages) const ULONG *References,
+ _Out_ LONG *BatchStatus,
+ _Out_writes_(NumberPages) LONG *Results
+ );
+
+/*! \brief Create a user-mode view of a fully populated mapping
+ \param Xc Xencontrol handle returned by XcOpen()
+ \param Id Mapping ID returned by XcGnttabCreateMapping()
+ \param Address Local user mode address of the mapped memory region
+ \return Win32 error code
+
+ Every page must be mapped before creating a view. Repeated calls return the
+ existing view. A batch unmap removes the view; it can be recreated after
+ every page has been mapped again. Stop all accesses before unmap or
release.
+*/
+XENCONTROL_API
+_Success_(return == ERROR_SUCCESS)
+DWORD
+XcGnttabCreateUserView(
+ _In_ PXENCONTROL_CONTEXT Xc,
+ _In_ ULONGLONG Id,
+ _Outptr_ PVOID *Address
+ );
+
+/*! \brief Unmap a batch of foreign pages from an existing mapping
+ \param Xc Xencontrol handle returned by XcOpen()
+ \param Id Mapping ID returned by XcGnttabCreateMapping()
+ \param Offset Starting page index in the mapping
+ \param NumberPages Number of 4k pages to unmap, from 1 to
XENIFACE_GNTTAB_BATCH_COUNT_MAX
+ \param BatchStatus NTSTATUS value for batch processing
+ \param Results An array of page-level hypercall NTSTATUS values for every
page in the batch
+ \return Win32 error code
+
+ Make one attempt to unmap \a NumberPages slots starting at the page \a
Offset.
+ Stop all memory accesses first. Every valid unmap removes the whole user
view
+ before attempting to unmap pages, including when the batch later fails.
+ \a BatchStatus and \a Results contain NTSTATUS values only on
ERROR_SUCCESS.
+ Empty slots succeed, so callers may retry the original range after a
partial
+ failure. Failed pages remain mapped and the caller owns retry policy.
+
+ This call does not destroy the mapping. Use XcGnttabDestroyMapping() after
all
+ pages are unmapped, or XcGnttabUnmapPages() or XcClose() to hand remaining
+ cleanup and retries to the driver.
+*/
+XENCONTROL_API
+_Success_(return == ERROR_SUCCESS)
+DWORD
+XcGnttabUnmapBatch(
+ _In_ PXENCONTROL_CONTEXT Xc,
+ _In_ ULONGLONG Id,
+ _In_ ULONG Offset,
+ _In_ ULONG NumberPages,
+ _Out_ LONG *BatchStatus,
+ _Out_writes_(NumberPages) LONG *Results
+ );
+
+/*! \brief Destroy an empty mapping and invalidate its ID
+ \param Xc Xencontrol handle returned by XcOpen()
+ \param Id Mapping ID returned by XcGnttabCreateMapping()
+ \return Win32 error code
+
+ All pages must be unmapped. A successful call frees the mapping and sends
+ any configured event channel notification.
+*/
+XENCONTROL_API
+DWORD
+XcGnttabDestroyMapping(
+ _In_ PXENCONTROL_CONTEXT Xc,
+ _In_ ULONGLONG Id
+ );
+
+/*! \brief Unmap all foreign pages and destroy the mapping
+ \param Xc Xencontrol handle returned by XcOpen()
+ \param Id Mapping ID returned by XcGnttabCreateMapping()
+ \return Win32 error code
+
+ Stop all memory accesses before calling. On ERROR_SUCCESS the address and
ID
+ are invalid, and the driver owns any remaining grant cleanup/retries.
+ Accepts empty, partially populated and fully populated mappings of any
size.
+ Success acknowledges release, not completion of all Xen grant unmaps.
+ The configured port notification follows actual destruction of this
mapping;
+ it does not wait for cleanup of other mappings.
+*/
+XENCONTROL_API
+DWORD
+XcGnttabUnmapPages(
+ _In_ PXENCONTROL_CONTEXT Xc,
+ _In_ ULONGLONG Id
+ );
+
/*! \brief Read a XenStore key
\param Xc Xencontrol handle returned by XcOpen()
\param Path Path to the key
diff --git a/include/xeniface_ioctls.h b/include/xeniface_ioctls.h
index 67deedf..decace5 100644
--- a/include/xeniface_ioctls.h
+++ b/include/xeniface_ioctls.h
@@ -237,7 +237,7 @@ typedef struct _XENIFACE_EVTCHN_UNMASK_IN {
ULONG LocalPort; /*!< Local port number that is assigned to the event
channel */
} XENIFACE_EVTCHN_UNMASK_IN, *PXENIFACE_EVTCHN_UNMASK_IN;
-/*! \brief Bitmask of XenStore key permissions */
+/*! \brief Bitmask of grant and foreign page mapping flags */
typedef enum _XENIFACE_GNTTAB_PAGE_FLAGS {
XENIFACE_GNTTAB_READONLY = 1 << 0, /*!< If set, the
granted/mapped pages are read-only */
XENIFACE_GNTTAB_USE_NOTIFY_OFFSET = 1 << 1, /*!< If set, the NotifyOffset
member of the grant/map IOCTL input is used */
@@ -414,6 +414,164 @@ typedef struct _XENIFACE_GNTTAB_UNMAP_FOREIGN_PAGES_IN_V2
{
PVOID Address; /*!< User-mode address of the shared memory region */
} XENIFACE_GNTTAB_UNMAP_FOREIGN_PAGES_IN_V2,
*PXENIFACE_GNTTAB_UNMAP_FOREIGN_PAGES_IN_V2;
+/*! \brief Maximum number of pages in a single map request or batch */
+#define XENIFACE_GNTTAB_BATCH_COUNT_MAX 256
+
+/*! \brief Maximum number of pages in a mapping */
+#define XENIFACE_GNTTAB_MAPPING_COUNT_MAX 0xFFFFF
+
+/*! \brief Create an empty mapping for foreign pages
+
+ Populate the mapping with IOCTL_XENIFACE_GNTTAB_MAP_BATCH, then obtain
+ a user-mode view with IOCTL_XENIFACE_GNTTAB_CREATE_USER_VIEW. The remote
+ domain, access flags and cleanup notifications are fixed for the lifetime
+ of the mapping. A notify byte requires a writable mapping and is cleared
+ before its view is removed. The event channel must belong to the same open
+ handle and is retained until mapping release. Its notification is sent only
+ after all pages are unmapped and the mapping is destroyed.
+
+ The batched mapping IOCTLs use METHOD_BUFFERED and complete synchronously.
+ Mapping IDs belong to the creating process and open file handle. Closing
+ the handle releases its mappings, the driver retries failed cleanup.
+
+ Input: XENIFACE_GNTTAB_CREATE_MAPPING_IN
+
+ Output: XENIFACE_GNTTAB_MAPPING_ID
+*/
+#define IOCTL_XENIFACE_GNTTAB_CREATE_MAPPING \
+ CTL_CODE(FILE_DEVICE_UNKNOWN, 0x828, METHOD_BUFFERED, FILE_ANY_ACCESS)
+
+/*! \brief Map a batch of foreign pages into an existing mapping
+
+ Batches may be mapped in any order at the requested page offset. A batch
+ overlapping any mapped page fails with STATUS_DEVICE_BUSY. Successful
+ pages remain mapped after a partial failure. To retry failed pages, submit
+ new batches covering only their unmapped slots.
+
+ The IOCTL succeeds when its output is delivered, even if mapping failed.
+ Status contains batch processing errors, Results contains per-page
+ hypervisor errors.
+
+ Input: XENIFACE_GNTTAB_MAP_BATCH_IN
+
+ Output: XENIFACE_GNTTAB_BATCH_OUT
+*/
+#define IOCTL_XENIFACE_GNTTAB_MAP_BATCH \
+ CTL_CODE(FILE_DEVICE_UNKNOWN, 0x829, METHOD_BUFFERED, FILE_ANY_ACCESS)
+
+/*! \brief Create a user-mode view of a fully populated mapping
+
+ Returns STATUS_DEVICE_NOT_READY if any page is unmapped. Repeated calls
+ reuse the existing view. After a batch unmap removes the view, another view
+ can be created only after all pages are mapped again.
+
+ Stop all accesses to the view before unmapping, releasing the mapping,
+ or closing its owning file handle.
+
+ Input: XENIFACE_GNTTAB_MAPPING_ID
+
+ Output: XENIFACE_GNTTAB_CREATE_USER_VIEW_OUT
+*/
+#define IOCTL_XENIFACE_GNTTAB_CREATE_USER_VIEW \
+ CTL_CODE(FILE_DEVICE_UNKNOWN, 0x82A, METHOD_BUFFERED, FILE_ANY_ACCESS)
+
+/*! \brief Unmap a batch of foreign pages from an existing mapping
+
+ Stop all accesses to the user-mode view before calling. A valid unmap
+ removes the entire view before attempting any grant unmaps, even if some
+ pages fail to unmap. Empty slots succeed, so the entire batch can safely
+ be retried after a partial failure. The mapping and ID remain valid.
+
+ The IOCTL succeeds when its output is delivered, even if the batch failed.
+ Status contains batch processing errors, Results contains per-page
+ hypervisor errors.
+
+ Input: XENIFACE_GNTTAB_UNMAP_BATCH_IN
+
+ Output: XENIFACE_GNTTAB_BATCH_OUT
+*/
+#define IOCTL_XENIFACE_GNTTAB_UNMAP_BATCH \
+ CTL_CODE(FILE_DEVICE_UNKNOWN, 0x82B, METHOD_BUFFERED, FILE_ANY_ACCESS)
+
+/*! \brief Destroy an empty mapping and invalidate its ID
+
+ All pages must be unmapped. A successful call frees the mapping
+ and sends any configured port notification.
+
+ Input: XENIFACE_GNTTAB_MAPPING_ID
+
+ Output: None
+*/
+#define IOCTL_XENIFACE_GNTTAB_DESTROY_MAPPING \
+ CTL_CODE(FILE_DEVICE_UNKNOWN, 0x82C, METHOD_BUFFERED, FILE_ANY_ACCESS)
+
+/*! \brief Unmap all foreign pages and destroy the mapping
+
+ Stop all accesses to the user-mode view before calling. Removes the view
+ and invalidates the ID before returning success. The driver takes ownership
+ of failed unmaps or mapping destruction and retries them during cleanup.
+ Accepts empty, partially populated and fully populated mappings of any
size.
+ Success acknowledges release; it does not guarantee that all foreign grant
+ handles are already unmapped. A configured port notification follows actual
+ destruction of this mapping only, not cleanup of other mappings.
+
+ Input: XENIFACE_GNTTAB_MAPPING_ID
+
+ Output: None
+*/
+#define IOCTL_XENIFACE_GNTTAB_UNMAP_PAGES \
+ CTL_CODE(FILE_DEVICE_UNKNOWN, 0x82F, METHOD_BUFFERED, FILE_ANY_ACCESS)
+
+/*! \brief Input for IOCTL_XENIFACE_GNTTAB_CREATE_MAPPING */
+typedef struct _XENIFACE_GNTTAB_CREATE_MAPPING_IN {
+ ULONG NumberPages; /*!< Number of pages, from 1 to
XENIFACE_GNTTAB_MAPPING_COUNT_MAX */
+ USHORT RemoteDomain; /*!< Remote domain granting access to the pages */
+ USHORT Reserved; /*!< Must be zero */
+ ULONG Flags; /*!< XENIFACE_GNTTAB_READONLY and
XENIFACE_GNTTAB_USE_NOTIFY_* */
+ ULONG NotifyOffset; /*!< Byte to clear before removing the view, if
enabled */
+ ULONG NotifyPort; /*!< Local event channel port to notify after
release, if enabled */
+} XENIFACE_GNTTAB_CREATE_MAPPING_IN;
+
+/*! \brief Output for IOCTL_XENIFACE_GNTTAB_CREATE_MAPPING and input for
+ IOCTL_XENIFACE_GNTTAB_CREATE_USER_VIEW,
+ IOCTL_XENIFACE_GNTTAB_DESTROY_MAPPING, and
IOCTL_XENIFACE_GNTTAB_UNMAP_PAGES
+*/
+typedef struct _XENIFACE_GNTTAB_MAPPING_ID {
+ ULONGLONG Id; /*!< Opaque mapping ID; zero is invalid */
+} XENIFACE_GNTTAB_MAPPING_ID;
+
+/*! \brief Input for IOCTL_XENIFACE_GNTTAB_MAP_BATCH
+ Buffer size: FIELD_OFFSET(XENIFACE_GNTTAB_MAP_BATCH_IN,
References[NumberPages]) bytes
+*/
+typedef struct _XENIFACE_GNTTAB_MAP_BATCH_IN {
+ ULONGLONG Id; /*!< Mapping ID returned by a create or
map request */
+ ULONG Offset; /*!< Starting page index in the mapping */
+ ULONG NumberPages; /*!< Number of pages, from 1 to
XENIFACE_GNTTAB_BATCH_COUNT_MAX */
+ ULONG References[ANYSIZE_ARRAY]; /*!< Xen grant references in page order */
+} XENIFACE_GNTTAB_MAP_BATCH_IN;
+
+/*! \brief Input for IOCTL_XENIFACE_GNTTAB_UNMAP_BATCH */
+typedef struct _XENIFACE_GNTTAB_UNMAP_BATCH_IN {
+ ULONGLONG Id; /*!< Mapping ID returned by a create or map request */
+ ULONG Offset; /*!< Starting page index in the mapping */
+ ULONG NumberPages; /*!< Number of pages, from 1 to
XENIFACE_GNTTAB_BATCH_COUNT_MAX */
+} XENIFACE_GNTTAB_UNMAP_BATCH_IN;
+
+/*! \brief Output for IOCTL_XENIFACE_GNTTAB_MAP_BATCH and
IOCTL_XENIFACE_GNTTAB_UNMAP_BATCH
+
+ Buffer size: FIELD_OFFSET(XENIFACE_GNTTAB_BATCH_OUT, Results[NumberPages])
bytes.
+ Status and Results contain NTSTATUS values.
+*/
+typedef struct _XENIFACE_GNTTAB_BATCH_OUT {
+ LONG Status; /*!< Overall batch status, including batch
processing errors */
+ LONG Results[ANYSIZE_ARRAY]; /*!< Page-level hypercall results;
STATUS_SUCCESS if no hypercall runs */
+} XENIFACE_GNTTAB_BATCH_OUT;
+
+/*! \brief Output for IOCTL_XENIFACE_GNTTAB_CREATE_USER_VIEW */
+typedef struct _XENIFACE_GNTTAB_CREATE_USER_VIEW_OUT {
+ ULONGLONG Address; /*!< User-mode address */
+} XENIFACE_GNTTAB_CREATE_USER_VIEW_OUT;
+
/*! \brief Gets the current suspend count.
Input: None
diff --git a/src/xencontrol/xencontrol.c b/src/xencontrol/xencontrol.c
index a68dcf6..7b995ed 100644
--- a/src/xencontrol/xencontrol.c
+++ b/src/xencontrol/xencontrol.c
@@ -589,6 +589,215 @@ XcGnttabUnmapForeignPages(
return Status;
}
+/* XcOpen uses FILE_FLAG_OVERLAPPED for legacy grant/map requests, which stay
+ * pending until revoke/unmap. The synchronous APIs below share that handle and
+ * must still supply an OVERLAPPED and event.
+ */
+_Success_(return == ERROR_SUCCESS)
+static DWORD
+XcGnttabControl(
+ _In_ PXENCONTROL_CONTEXT Xc,
+ _In_ DWORD Code,
+ _In_reads_bytes_(InSize) PVOID In,
+ _In_ DWORD InSize,
+ _Out_writes_bytes_opt_(OutSize) PVOID Out,
+ _In_ DWORD OutSize
+ )
+{
+ OVERLAPPED Overlapped = {0};
+ DWORD Returned = 0, Status = ERROR_SUCCESS;
+ BOOL Success;
+
+ Overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
+ if (Overlapped.hEvent == NULL)
+ return GetLastError();
+
+ Success = DeviceIoControl(Xc->XenIface, Code, In, InSize, Out, OutSize,
+ &Returned, &Overlapped);
+ if (!Success) {
+ Status = GetLastError();
+ if (Status == ERROR_IO_PENDING) {
+ Success = GetOverlappedResult(Xc->XenIface, &Overlapped,
&Returned, TRUE);
+ Status = Success ? ERROR_SUCCESS : GetLastError();
+ }
+ }
+
+ if (Status == ERROR_SUCCESS && Returned != OutSize)
+ Status = ERROR_INVALID_DATA;
+
+ CloseHandle(Overlapped.hEvent);
+ return Status;
+}
+
+_Success_(return == ERROR_SUCCESS)
+DWORD
+XcGnttabCreateMapping(
+ _In_ PXENCONTROL_CONTEXT Xc,
+ _In_ USHORT RemoteDomain,
+ _In_ ULONG NumberPages,
+ _In_ ULONG NotifyOffset,
+ _In_ ULONG NotifyPort,
+ _In_ XENIFACE_GNTTAB_PAGE_FLAGS Flags,
+ _Out_ ULONGLONG *Id
+ )
+{
+ XENIFACE_GNTTAB_CREATE_MAPPING_IN In = {0};
+ XENIFACE_GNTTAB_MAPPING_ID Out = {0};
+ DWORD Status;
+
+ if (Id != NULL)
+ *Id = 0;
+
+ if (Xc == NULL || Id == NULL || NumberPages == 0 ||
+ NumberPages > XENIFACE_GNTTAB_MAPPING_COUNT_MAX)
+ return ERROR_INVALID_PARAMETER;
+
+ In.NumberPages = NumberPages;
+ In.RemoteDomain = RemoteDomain;
+ In.Flags = Flags;
+ In.NotifyOffset = NotifyOffset;
+ In.NotifyPort = NotifyPort;
+ Status = XcGnttabControl(Xc, IOCTL_XENIFACE_GNTTAB_CREATE_MAPPING,
+ &In, sizeof(In), &Out, sizeof(Out));
+
+ if (Status == ERROR_SUCCESS && Out.Id == 0)
+ Status = ERROR_INVALID_DATA;
+
+ if (Status == ERROR_INVALID_DATA && Out.Id != 0)
+ (VOID)XcGnttabUnmapPages(Xc, Out.Id);
+
+ if (Status == ERROR_SUCCESS)
+ *Id = Out.Id;
+
+ return Status;
+}
+
+_Success_(return == ERROR_SUCCESS)
+DWORD
+XcGnttabMapBatch(
+ _In_ PXENCONTROL_CONTEXT Xc,
+ _In_ ULONGLONG Id,
+ _In_ ULONG Offset,
+ _In_ ULONG NumberPages,
+ _In_reads_(NumberPages) const ULONG *References,
+ _Out_ LONG *BatchStatus,
+ _Out_writes_(NumberPages) LONG *Results
+ )
+{
+ // Keep 64-bit alignment: two ULONGLONGs for Id, Offset and NumberPages.
+ // Each remaining ULONGLONG holds two 32-bit grant references.
+ ULONGLONG InBuffer[2 + XENIFACE_GNTTAB_BATCH_COUNT_MAX / 2];
+ // One LONG for the overall batch status, followed by per-page results.
+ LONG OutBuffer[1 + XENIFACE_GNTTAB_BATCH_COUNT_MAX];
+ XENIFACE_GNTTAB_MAP_BATCH_IN *In = (PVOID)InBuffer;
+ XENIFACE_GNTTAB_BATCH_OUT *Out = (PVOID)OutBuffer;
+ DWORD Status, InSize, OutSize;
+
+ if (NumberPages == 0 || NumberPages > XENIFACE_GNTTAB_BATCH_COUNT_MAX ||
+ References == NULL || BatchStatus == NULL || Results == NULL)
+ return ERROR_INVALID_PARAMETER;
+
+ In->Id = Id;
+ In->Offset = Offset;
+ In->NumberPages = NumberPages;
+ memcpy(In->References, References, NumberPages * sizeof(ULONG));
+ InSize = (DWORD)FIELD_OFFSET(XENIFACE_GNTTAB_MAP_BATCH_IN,
References[NumberPages]);
+ OutSize = (DWORD)FIELD_OFFSET(XENIFACE_GNTTAB_BATCH_OUT,
Results[NumberPages]);
+ Status = XcGnttabControl(Xc, IOCTL_XENIFACE_GNTTAB_MAP_BATCH, In, InSize,
Out, OutSize);
+
+ if (Status == ERROR_SUCCESS) {
+ *BatchStatus = Out->Status;
+ memcpy(Results, Out->Results, NumberPages * sizeof(LONG));
+ }
+ return Status;
+}
+
+_Success_(return == ERROR_SUCCESS)
+DWORD
+XcGnttabCreateUserView(
+ _In_ PXENCONTROL_CONTEXT Xc,
+ _In_ ULONGLONG Id,
+ _Outptr_ PVOID *Address
+ )
+{
+ XENIFACE_GNTTAB_MAPPING_ID In;
+ XENIFACE_GNTTAB_CREATE_USER_VIEW_OUT Out;
+ DWORD Status;
+
+ if (Address == NULL)
+ return ERROR_INVALID_PARAMETER;
+
+ *Address = NULL;
+ In.Id = Id;
+ Status = XcGnttabControl(Xc, IOCTL_XENIFACE_GNTTAB_CREATE_USER_VIEW,
+ &In, sizeof(In), &Out, sizeof(Out));
+
+ if (Status == ERROR_SUCCESS)
+ *Address = (PVOID)Out.Address;
+ return Status;
+}
+
+_Success_(return == ERROR_SUCCESS)
+DWORD
+XcGnttabUnmapBatch(
+ _In_ PXENCONTROL_CONTEXT Xc,
+ _In_ ULONGLONG Id,
+ _In_ ULONG Offset,
+ _In_ ULONG NumberPages,
+ _Out_ LONG *BatchStatus,
+ _Out_writes_(NumberPages) LONG *Results
+ )
+{
+ XENIFACE_GNTTAB_UNMAP_BATCH_IN In;
+ // One LONG for the overall batch status, followed by per-page results.
+ LONG OutBuffer[1 + XENIFACE_GNTTAB_BATCH_COUNT_MAX];
+ XENIFACE_GNTTAB_BATCH_OUT *Out = (PVOID)OutBuffer;
+ DWORD Status, OutSize;
+
+ if (NumberPages == 0 || NumberPages > XENIFACE_GNTTAB_BATCH_COUNT_MAX ||
+ BatchStatus == NULL || Results == NULL)
+ return ERROR_INVALID_PARAMETER;
+
+ In.Id = Id;
+ In.Offset = Offset;
+ In.NumberPages = NumberPages;
+ OutSize = (DWORD)FIELD_OFFSET(XENIFACE_GNTTAB_BATCH_OUT,
Results[NumberPages]);
+ Status = XcGnttabControl(Xc, IOCTL_XENIFACE_GNTTAB_UNMAP_BATCH, &In,
sizeof(In), Out, OutSize);
+
+ if (Status == ERROR_SUCCESS) {
+ *BatchStatus = Out->Status;
+ memcpy(Results, Out->Results, NumberPages * sizeof(LONG));
+ }
+ return Status;
+}
+
+DWORD
+XcGnttabDestroyMapping(
+ _In_ PXENCONTROL_CONTEXT Xc,
+ _In_ ULONGLONG Id
+ )
+{
+ XENIFACE_GNTTAB_MAPPING_ID In;
+ In.Id = Id;
+ return XcGnttabControl(Xc, IOCTL_XENIFACE_GNTTAB_DESTROY_MAPPING, &In,
sizeof(In), NULL, 0);
+}
+
+DWORD
+XcGnttabUnmapPages(
+ _In_ PXENCONTROL_CONTEXT Xc,
+ _In_ ULONGLONG Id
+ )
+{
+ XENIFACE_GNTTAB_MAPPING_ID In;
+
+ if (Xc == NULL || Id == 0)
+ return ERROR_INVALID_PARAMETER;
+
+ In.Id = Id;
+ return XcGnttabControl(Xc, IOCTL_XENIFACE_GNTTAB_UNMAP_PAGES,
+ &In, sizeof(In), NULL, 0);
+}
+
DWORD
XcStoreRead(
_In_ PXENCONTROL_CONTEXT Xc,
diff --git a/src/xeniface/ioctl_gnttab.c b/src/xeniface/ioctl_gnttab.c
index 44b7809..38730ee 100644
--- a/src/xeniface/ioctl_gnttab.c
+++ b/src/xeniface/ioctl_gnttab.c
@@ -38,6 +38,9 @@
#define GNTTAB_MAX_PAGES (MAXULONG / PAGE_SIZE)
+C_ASSERT(XENIFACE_GNTTAB_BATCH_COUNT_MAX == XENBUS_GNTTAB_MAP_BATCH_COUNT_MAX);
+C_ASSERT(XENIFACE_GNTTAB_MAPPING_COUNT_MAX == XENBUS_GNTTAB_MAP_COUNT_MAX);
+
#ifndef XENIFACE_GNTTAB_CLEANUP_TIMEOUT_SECONDS
#define XENIFACE_GNTTAB_CLEANUP_TIMEOUT_SECONDS 60
#endif
@@ -437,6 +440,289 @@ GnttabCleanup(
}
}
+_IRQL_requires_(PASSIVE_LEVEL)
+_Ret_maybenull_
+static PXENIFACE_GNTTAB_CONTEXT
+GnttabFindMapping(
+ _In_ PXENIFACE_FDO Fdo,
+ _In_ ULONGLONG Id,
+ _In_opt_ PFILE_OBJECT FileObject
+ )
+{
+ PLIST_ENTRY Entry;
+ PXENIFACE_GNTTAB_CONTEXT Context;
+
+ for (Entry = Fdo->MappingList.Flink; Entry != &Fdo->MappingList; Entry =
Entry->Flink) {
+ Context = CONTAINING_RECORD(Entry, XENIFACE_GNTTAB_CONTEXT, Entry);
+ if (Context->Map.MappingId == Id && Context->FileObject == FileObject
&&
+ Context->Process == PsGetCurrentProcess())
+ return Context;
+ }
+ return NULL;
+}
+
+_IRQL_requires_(PASSIVE_LEVEL)
+static NTSTATUS
+GnttabAllocateMappingContext(
+ _In_ PXENIFACE_FDO Fdo,
+ _In_ const XENIFACE_GNTTAB_CREATE_MAPPING_IN *In,
+ _In_opt_ PFILE_OBJECT FileObject,
+ _Out_ PXENIFACE_GNTTAB_CONTEXT *ContextOut
+ )
+{
+ PXENIFACE_GNTTAB_CONTEXT Context;
+
+ *ContextOut = NULL;
+ if (In->NumberPages == 0 || In->NumberPages > XENBUS_GNTTAB_MAP_COUNT_MAX
||
+ In->Reserved != 0 || Fdo->LastMappingId == MAXULONGLONG ||
+ (In->Flags & ~(XENIFACE_GNTTAB_READONLY |
XENIFACE_GNTTAB_USE_NOTIFY_OFFSET |
+ XENIFACE_GNTTAB_USE_NOTIFY_PORT)))
+ return STATUS_INVALID_PARAMETER;
+
+ if ((In->Flags & XENIFACE_GNTTAB_USE_NOTIFY_OFFSET) &&
+ (In->NotifyOffset >= In->NumberPages * PAGE_SIZE ||
+ (In->Flags & XENIFACE_GNTTAB_READONLY)))
+ return STATUS_INVALID_PARAMETER;
+
+ Context = GnttabAllocateContext(Fdo);
+ if (Context == NULL)
+ return STATUS_NO_MEMORY;
+
+ Context->Type = XENIFACE_GNTTAB_CONTEXT_MAP;
+ Context->FileObject = FileObject;
+ Context->RemoteDomain = In->RemoteDomain;
+ Context->NumberPages = In->NumberPages;
+ Context->Flags = In->Flags;
+ Context->NotifyOffset = In->NotifyOffset;
+ Context->NotifyPort = In->NotifyPort;
+ if (In->Flags & XENIFACE_GNTTAB_USE_NOTIFY_PORT) {
+ Context->Map.NotifyChannel = EvtchnReference(Fdo, In->NotifyPort,
FileObject);
+ if (Context->Map.NotifyChannel == NULL) {
+ GnttabFreeContext(Fdo, Context);
+ return STATUS_NOT_FOUND;
+ }
+ }
+
+ // Enable notifications only when the request returns a valid mapping.
+ *ContextOut = Context;
+ return STATUS_SUCCESS;
+}
+
+_IRQL_requires_(PASSIVE_LEVEL)
+NTSTATUS
+IoctlGnttabCreateMapping(
+ _In_ PXENIFACE_FDO Fdo,
+ _Inout_ PVOID Buffer,
+ _In_ ULONG InLen,
+ _In_ ULONG OutLen,
+ _In_opt_ PFILE_OBJECT FileObject,
+ _Out_ PULONG_PTR Info
+ )
+{
+ XENIFACE_GNTTAB_CREATE_MAPPING_IN *Create = Buffer;
+ XENIFACE_GNTTAB_MAPPING_ID *Out = Buffer;
+ PXENIFACE_GNTTAB_CONTEXT Context;
+ NTSTATUS Status;
+
+ *Info = 0;
+ if (InLen != sizeof(*Create) || OutLen != sizeof(*Out))
+ return STATUS_INVALID_BUFFER_SIZE;
+
+ Status = GnttabAllocateMappingContext(Fdo, Create, FileObject, &Context);
+ if (!NT_SUCCESS(Status))
+ return Status;
+
+ Status = GnttabCreateMap(Fdo, Context);
+ if (!NT_SUCCESS(Status)) {
+ GnttabFreeContext(Fdo, Context);
+ return Status;
+ }
+
+ Context->Map.MappingId = ++Fdo->LastMappingId;
+ Context->Map.NotifyOnCleanup = TRUE;
+ InsertTailList(&Fdo->MappingList, &Context->Entry);
+ Out->Id = Context->Map.MappingId;
+ *Info = sizeof(*Out);
+ return STATUS_SUCCESS;
+}
+
+_IRQL_requires_(PASSIVE_LEVEL)
+static NTSTATUS
+GnttabProcessBatch(
+ _In_ PXENIFACE_FDO Fdo,
+ _Inout_ PVOID Buffer,
+ _In_ ULONG InLen,
+ _In_ ULONG OutLen,
+ _In_opt_ PFILE_OBJECT FileObject,
+ _Out_ PULONG_PTR Info,
+ _In_ BOOLEAN MapPages
+ )
+{
+ XENIFACE_GNTTAB_MAP_BATCH_IN *Map = Buffer;
+ XENIFACE_GNTTAB_UNMAP_BATCH_IN *Unmap = Buffer;
+ XENIFACE_GNTTAB_BATCH_OUT *Out = Buffer;
+ PXENIFACE_GNTTAB_CONTEXT Context;
+ NTSTATUS Status;
+ NTSTATUS Results[XENBUS_GNTTAB_MAP_BATCH_COUNT_MAX];
+ ULONG Count, Offset, Size;
+ ULONGLONG Id;
+
+ *Info = 0;
+ if (InLen < sizeof(*Unmap))
+ return STATUS_INVALID_BUFFER_SIZE;
+
+ Id = Unmap->Id;
+ Offset = Unmap->Offset;
+ Count = Unmap->NumberPages;
+ if (Count == 0 || Count > XENBUS_GNTTAB_MAP_BATCH_COUNT_MAX)
+ return STATUS_INVALID_PARAMETER;
+
+ Size = (ULONG)FIELD_OFFSET(XENIFACE_GNTTAB_BATCH_OUT, Results[Count]);
+ if (OutLen != Size || InLen != (MapPages ?
+ (ULONG)FIELD_OFFSET(XENIFACE_GNTTAB_MAP_BATCH_IN, References[Count]) :
sizeof(*Unmap)))
+ return STATUS_INVALID_BUFFER_SIZE;
+
+ Context = GnttabFindMapping(Fdo, Id, FileObject);
+ if (Context == NULL)
+ return STATUS_NOT_FOUND;
+
+ if (Offset >= Context->NumberPages || Count > Context->NumberPages -
Offset)
+ return STATUS_INVALID_PARAMETER;
+
+ RtlZeroMemory(Results, Count * sizeof(Results[0]));
+ if (MapPages) {
+ Status = XENBUS_GNTTAB(MapForeignPages, &Fdo->GnttabInterface,
+ Context->Map.Mapping, Offset, Count,
Map->References, Results);
+ } else {
+ GnttabUnmapView(Context);
+ Status = XENBUS_GNTTAB(UnmapForeignPages, &Fdo->GnttabInterface,
+ Context->Map.Mapping, Offset, Count, Results);
+ }
+
+ Out->Status = Status;
+ RtlCopyMemory(Out->Results, Results, Count * sizeof(Results[0]));
+ *Info = Size;
+ // Potential page errors are in Results.
+ return STATUS_SUCCESS;
+}
+
+_IRQL_requires_(PASSIVE_LEVEL)
+NTSTATUS
+IoctlGnttabMapBatch(
+ _In_ PXENIFACE_FDO Fdo,
+ _Inout_ PVOID Buffer,
+ _In_ ULONG InLen,
+ _In_ ULONG OutLen,
+ _In_opt_ PFILE_OBJECT FileObject,
+ _Out_ PULONG_PTR Info
+ )
+{
+ return GnttabProcessBatch(Fdo, Buffer, InLen, OutLen, FileObject, Info,
TRUE);
+}
+
+_IRQL_requires_(PASSIVE_LEVEL)
+NTSTATUS
+IoctlGnttabCreateUserView(
+ _In_ PXENIFACE_FDO Fdo,
+ _Inout_ PVOID Buffer,
+ _In_ ULONG InLen,
+ _In_ ULONG OutLen,
+ _In_opt_ PFILE_OBJECT FileObject,
+ _Out_ PULONG_PTR Info
+ )
+{
+ XENIFACE_GNTTAB_MAPPING_ID *In = Buffer;
+ XENIFACE_GNTTAB_CREATE_USER_VIEW_OUT *Out = Buffer;
+ PXENIFACE_GNTTAB_CONTEXT Context;
+ NTSTATUS Status;
+
+ *Info = 0;
+ if (InLen != sizeof(*In) || OutLen != sizeof(*Out))
+ return STATUS_INVALID_BUFFER_SIZE;
+
+ Context = GnttabFindMapping(Fdo, In->Id, FileObject);
+ if (Context == NULL)
+ return STATUS_NOT_FOUND;
+
+ Status = GnttabCreateUserView(Fdo, Context);
+ if (NT_SUCCESS(Status)) {
+ Out->Address = (ULONGLONG)Context->UserVa;
+ *Info = sizeof(*Out);
+ }
+
+ return Status;
+}
+
+_IRQL_requires_(PASSIVE_LEVEL)
+NTSTATUS
+IoctlGnttabUnmapBatch(
+ _In_ PXENIFACE_FDO Fdo,
+ _Inout_ PVOID Buffer,
+ _In_ ULONG InLen,
+ _In_ ULONG OutLen,
+ _In_opt_ PFILE_OBJECT FileObject,
+ _Out_ PULONG_PTR Info
+ )
+{
+ return GnttabProcessBatch(Fdo, Buffer, InLen, OutLen, FileObject, Info,
FALSE);
+}
+
+_IRQL_requires_(PASSIVE_LEVEL)
+NTSTATUS
+IoctlGnttabDestroyMapping(
+ _In_ PXENIFACE_FDO Fdo,
+ _In_ PVOID Buffer,
+ _In_ ULONG InLen,
+ _In_ ULONG OutLen,
+ _In_opt_ PFILE_OBJECT FileObject
+ )
+{
+ XENIFACE_GNTTAB_MAPPING_ID *In = Buffer;
+ PXENIFACE_GNTTAB_CONTEXT Context;
+ NTSTATUS Status;
+
+ if (InLen != sizeof(*In) || OutLen != 0)
+ return STATUS_INVALID_BUFFER_SIZE;
+
+ Context = GnttabFindMapping(Fdo, In->Id, FileObject);
+ if (Context == NULL)
+ return STATUS_NOT_FOUND;
+
+ Status = XENBUS_GNTTAB(DestroyMapping, &Fdo->GnttabInterface,
Context->Map.Mapping);
+ if (NT_SUCCESS(Status)) {
+ Context->Map.Mapping = NULL;
+ RemoveEntryList(&Context->Entry);
+ GnttabFinishMapCleanup(Fdo, Context);
+ }
+
+ return Status;
+}
+
+_IRQL_requires_(PASSIVE_LEVEL)
+NTSTATUS
+IoctlGnttabUnmapPages(
+ _In_ PXENIFACE_FDO Fdo,
+ _In_ PVOID Buffer,
+ _In_ ULONG InLen,
+ _In_ ULONG OutLen,
+ _In_opt_ PFILE_OBJECT FileObject
+ )
+{
+ XENIFACE_GNTTAB_MAPPING_ID *In = Buffer;
+ PXENIFACE_GNTTAB_CONTEXT Context;
+
+ if (InLen != sizeof(*In) || OutLen != 0)
+ return STATUS_INVALID_BUFFER_SIZE;
+
+ Context = GnttabFindMapping(Fdo, In->Id, FileObject);
+ if (Context == NULL)
+ return STATUS_NOT_FOUND;
+
+ RemoveEntryList(&Context->Entry);
+ GnttabReleaseMap(Fdo, Context);
+ return STATUS_SUCCESS;
+}
+
// Complete a canceled gnttab IRP, cleanup associated grant/map.
_Function_class_(IO_WORKITEM_ROUTINE)
VOID
diff --git a/src/xeniface/ioctls.c b/src/xeniface/ioctls.c
index d0b2132..7bc9cc0 100644
--- a/src/xeniface/ioctls.c
+++ b/src/xeniface/ioctls.c
@@ -266,7 +266,7 @@ XenIfaceIoctl(
goto done;
if (ControlCode >= IOCTL_XENIFACE_GNTTAB_PERMIT_FOREIGN_ACCESS &&
- ControlCode <= IOCTL_XENIFACE_GNTTAB_UNMAP_FOREIGN_PAGES_V2) {
+ ControlCode <= IOCTL_XENIFACE_GNTTAB_UNMAP_PAGES) {
AcquireMutex(&Fdo->GnttabLock);
GnttabLocked = TRUE;
status = STATUS_DEVICE_NOT_READY;
@@ -362,6 +362,32 @@ XenIfaceIoctl(
status = IoctlGnttabUnmapForeignPages(Fdo, Buffer, InLen, OutLen,
ControlCode);
break;
+ case IOCTL_XENIFACE_GNTTAB_CREATE_MAPPING:
+ status = IoctlGnttabCreateMapping(Fdo, Buffer, InLen, OutLen,
Stack->FileObject, &Irp->IoStatus.Information);
+ break;
+
+ case IOCTL_XENIFACE_GNTTAB_MAP_BATCH:
+ status = IoctlGnttabMapBatch(Fdo, Buffer, InLen, OutLen,
Stack->FileObject, &Irp->IoStatus.Information);
+ break;
+
+ case IOCTL_XENIFACE_GNTTAB_CREATE_USER_VIEW:
+ status = IoctlGnttabCreateUserView(Fdo, Buffer, InLen, OutLen,
Stack->FileObject, &Irp->IoStatus.Information);
+ break;
+
+ case IOCTL_XENIFACE_GNTTAB_UNMAP_BATCH:
+ status = IoctlGnttabUnmapBatch(Fdo, Buffer, InLen, OutLen,
Stack->FileObject, &Irp->IoStatus.Information);
+ break;
+
+ case IOCTL_XENIFACE_GNTTAB_DESTROY_MAPPING:
+ Irp->IoStatus.Information = 0;
+ status = IoctlGnttabDestroyMapping(Fdo, Buffer, InLen, OutLen,
Stack->FileObject);
+ break;
+
+ case IOCTL_XENIFACE_GNTTAB_UNMAP_PAGES:
+ Irp->IoStatus.Information = 0;
+ status = IoctlGnttabUnmapPages(Fdo, Buffer, InLen, OutLen,
Stack->FileObject);
+ break;
+
// suspend
case IOCTL_XENIFACE_SUSPEND_GET_COUNT:
status = IoctlSuspendGetCount(Fdo, Buffer, InLen, OutLen,
&Irp->IoStatus.Information);
diff --git a/src/xeniface/ioctls.h b/src/xeniface/ioctls.h
index daca4fa..d52a893 100644
--- a/src/xeniface/ioctls.h
+++ b/src/xeniface/ioctls.h
@@ -376,6 +376,71 @@ GnttabFreeMap(
_Inout_ PXENIFACE_GNTTAB_CONTEXT Context
);
+_IRQL_requires_(PASSIVE_LEVEL)
+NTSTATUS
+IoctlGnttabCreateMapping(
+ _In_ PXENIFACE_FDO Fdo,
+ _Inout_ PVOID Buffer,
+ _In_ ULONG InLen,
+ _In_ ULONG OutLen,
+ _In_opt_ PFILE_OBJECT FileObject,
+ _Out_ PULONG_PTR Info
+ );
+
+_IRQL_requires_(PASSIVE_LEVEL)
+NTSTATUS
+IoctlGnttabMapBatch(
+ _In_ PXENIFACE_FDO Fdo,
+ _Inout_ PVOID Buffer,
+ _In_ ULONG InLen,
+ _In_ ULONG OutLen,
+ _In_opt_ PFILE_OBJECT FileObject,
+ _Out_ PULONG_PTR Info
+ );
+
+_IRQL_requires_(PASSIVE_LEVEL)
+NTSTATUS
+IoctlGnttabCreateUserView(
+ _In_ PXENIFACE_FDO Fdo,
+ _Inout_ PVOID Buffer,
+ _In_ ULONG InLen,
+ _In_ ULONG OutLen,
+ _In_opt_ PFILE_OBJECT FileObject,
+ _Out_ PULONG_PTR Info
+ );
+
+_IRQL_requires_(PASSIVE_LEVEL)
+NTSTATUS
+IoctlGnttabUnmapBatch(
+ _In_ PXENIFACE_FDO Fdo,
+ _Inout_ PVOID Buffer,
+ _In_ ULONG InLen,
+ _In_ ULONG OutLen,
+ _In_opt_ PFILE_OBJECT FileObject,
+ _Out_ PULONG_PTR Info
+ );
+
+_IRQL_requires_(PASSIVE_LEVEL)
+NTSTATUS
+IoctlGnttabDestroyMapping(
+ _In_ PXENIFACE_FDO Fdo,
+ _In_ PVOID Buffer,
+ _In_ ULONG InLen,
+ _In_ ULONG OutLen,
+ _In_opt_ PFILE_OBJECT FileObject
+ );
+
+// Release the mapping, retaining failed cleanup for retry.
+_IRQL_requires_(PASSIVE_LEVEL)
+NTSTATUS
+IoctlGnttabUnmapPages(
+ _In_ PXENIFACE_FDO Fdo,
+ _In_ PVOID Buffer,
+ _In_ ULONG InLen,
+ _In_ ULONG OutLen,
+ _In_opt_ PFILE_OBJECT FileObject
+ );
+
_IRQL_requires_(PASSIVE_LEVEL)
VOID
GnttabCleanup(
--
2.56.0
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |