|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [RFC PATCH 7/7] xeniface: Add a synchronous single batch foreign page mapping helper
From: Rafał Wojdyła <omeg@xxxxxxx>
Add MAP_PAGES and XcGnttabMapPages for mappings of up to 256 pages.
Create the mapping, populate its single batch and obtain its user view in
one request, returning an ID and address only when the whole setup succeeds.
Roll back all successful pages after a map or view failure, retain failed
cleanup for driver retries, and preserve the original error. Enable cleanup
notifications only after successful setup. Reuse UnmapPages to release the
result and clean up any ID returned with an invalid or short response.
Assisted-by: LLM
Signed-off-by: Rafał Wojdyła <omeg@xxxxxxxxxxxxxxxxxxxxxx>
---
include/xencontrol.h | 44 ++++++++++++++++++++++++++----
include/xeniface_ioctls.h | 33 +++++++++++++++++++++++
src/xencontrol/xencontrol.c | 54 +++++++++++++++++++++++++++++++++++++
src/xeniface/ioctl_gnttab.c | 50 ++++++++++++++++++++++++++++++++++
src/xeniface/ioctls.c | 4 +++
src/xeniface/ioctls.h | 13 +++++++++
6 files changed, 193 insertions(+), 5 deletions(-)
diff --git a/include/xencontrol.h b/include/xencontrol.h
index 348bc44..edd0279 100644
--- a/include/xencontrol.h
+++ b/include/xencontrol.h
@@ -298,7 +298,7 @@ XcGnttabCreateMapping(
/*! \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 Id Mapping ID returned by XcGnttabCreateMapping() or
XcGnttabMapPages()
\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
@@ -324,7 +324,7 @@ XcGnttabMapBatch(
/*! \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 Id Mapping ID returned by XcGnttabCreateMapping() or
XcGnttabMapPages()
\param Address Local user mode address of the mapped memory region
\return Win32 error code
@@ -343,7 +343,7 @@ XcGnttabCreateUserView(
/*! \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 Id Mapping ID returned by XcGnttabCreateMapping() or
XcGnttabMapPages()
\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
@@ -375,7 +375,7 @@ XcGnttabUnmapBatch(
/*! \brief Destroy an empty mapping and invalidate its ID
\param Xc Xencontrol handle returned by XcOpen()
- \param Id Mapping ID returned by XcGnttabCreateMapping()
+ \param Id Mapping ID returned by XcGnttabCreateMapping() or
XcGnttabMapPages()
\return Win32 error code
All pages must be unmapped. A successful call frees the mapping and sends
@@ -388,9 +388,43 @@ XcGnttabDestroyMapping(
_In_ ULONGLONG Id
);
+/*! \brief Create a mapping, map one batch, and obtain its user-mode view
+ \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 to map, from 1 to
XENIFACE_GNTTAB_BATCH_COUNT_MAX
+ \param References An array of Xen grant numbers for every mapped page
+ \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
+ \param Address Local user mode address of the mapped memory region
+ \return Win32 error code
+
+ Returns ERROR_SUCCESS only after every page and the user view are ready.
+ On failure, \a Id is zero and \a Address is NULL. The driver owns rollback,
+ including deferred cleanup if unmapping partially successful pages fails.
+ Use XcGnttabCreateMapping(), XcGnttabMapBatch() and
XcGnttabCreateUserView()
+ to inspect page results or drive map retries. Notifications are enabled
only
+ after mapping and view creation succeed.
+*/
+XENCONTROL_API
+_Success_(return == ERROR_SUCCESS)
+DWORD
+XcGnttabMapPages(
+ _In_ PXENCONTROL_CONTEXT Xc,
+ _In_ USHORT RemoteDomain,
+ _In_ ULONG NumberPages,
+ _In_reads_(NumberPages) const ULONG *References,
+ _In_ ULONG NotifyOffset,
+ _In_ ULONG NotifyPort,
+ _In_ XENIFACE_GNTTAB_PAGE_FLAGS Flags,
+ _Out_ ULONGLONG *Id,
+ _Outptr_ PVOID *Address
+ );
+
/*! \brief Unmap all foreign pages and destroy the mapping
\param Xc Xencontrol handle returned by XcOpen()
- \param Id Mapping ID returned by XcGnttabCreateMapping()
+ \param Id Mapping ID returned by XcGnttabCreateMapping() or
XcGnttabMapPages()
\return Win32 error code
Stop all memory accesses before calling. On ERROR_SUCCESS the address and
ID
diff --git a/include/xeniface_ioctls.h b/include/xeniface_ioctls.h
index decace5..f5762b5 100644
--- a/include/xeniface_ioctls.h
+++ b/include/xeniface_ioctls.h
@@ -505,6 +505,23 @@ typedef struct _XENIFACE_GNTTAB_UNMAP_FOREIGN_PAGES_IN_V2 {
#define IOCTL_XENIFACE_GNTTAB_DESTROY_MAPPING \
CTL_CODE(FILE_DEVICE_UNKNOWN, 0x82C, METHOD_BUFFERED, FILE_ANY_ACCESS)
+/*! \brief Create a mapping, map one batch, and obtain its user-mode view
+
+ Completes synchronously and succeeds only when every page is mapped and
+ the view is ready. A failure rolls back the entire request, the driver
+ retains failed rollback cleanup for retry.
+
+ The ID has the same process and file ownership as IDs returned by
+ IOCTL_XENIFACE_GNTTAB_CREATE_MAPPING. Use IOCTL_XENIFACE_GNTTAB_UNMAP_PAGES
+ to release the entire mapping.
+
+ Input: XENIFACE_GNTTAB_MAP_PAGES_IN
+
+ Output: XENIFACE_GNTTAB_MAP_PAGES_OUT
+*/
+#define IOCTL_XENIFACE_GNTTAB_MAP_PAGES \
+ CTL_CODE(FILE_DEVICE_UNKNOWN, 0x82E, 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
@@ -540,6 +557,22 @@ 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_PAGES
+
+ Buffer size: FIELD_OFFSET(XENIFACE_GNTTAB_MAP_PAGES_IN,
References[NumberPages]) bytes
+*/
+typedef struct _XENIFACE_GNTTAB_MAP_PAGES_IN {
+ XENIFACE_GNTTAB_CREATE_MAPPING_IN Mapping; /*!< Creation parameters; at
most XENIFACE_GNTTAB_BATCH_COUNT_MAX pages */
+ ULONG References[ANYSIZE_ARRAY]; /*!< Xen grant references in page order */
+} XENIFACE_GNTTAB_MAP_PAGES_IN;
+
+/*! \brief Output for IOCTL_XENIFACE_GNTTAB_MAP_PAGES
+*/
+typedef struct _XENIFACE_GNTTAB_MAP_PAGES_OUT {
+ ULONGLONG Id; /*!< Opaque mapping ID; zero is invalid */
+ ULONGLONG Address; /*!< User-mode address */
+} XENIFACE_GNTTAB_MAP_PAGES_OUT;
+
/*! \brief Input for IOCTL_XENIFACE_GNTTAB_MAP_BATCH
Buffer size: FIELD_OFFSET(XENIFACE_GNTTAB_MAP_BATCH_IN,
References[NumberPages]) bytes
*/
diff --git a/src/xencontrol/xencontrol.c b/src/xencontrol/xencontrol.c
index 7b995ed..eb7b298 100644
--- a/src/xencontrol/xencontrol.c
+++ b/src/xencontrol/xencontrol.c
@@ -782,6 +782,60 @@ XcGnttabDestroyMapping(
return XcGnttabControl(Xc, IOCTL_XENIFACE_GNTTAB_DESTROY_MAPPING, &In,
sizeof(In), NULL, 0);
}
+_Success_(return == ERROR_SUCCESS)
+DWORD
+XcGnttabMapPages(
+ _In_ PXENCONTROL_CONTEXT Xc,
+ _In_ USHORT RemoteDomain,
+ _In_ ULONG NumberPages,
+ _In_reads_(NumberPages) const ULONG *References,
+ _In_ ULONG NotifyOffset,
+ _In_ ULONG NotifyPort,
+ _In_ XENIFACE_GNTTAB_PAGE_FLAGS Flags,
+ _Out_ ULONGLONG *Id,
+ _Outptr_ PVOID *Address
+ )
+{
+ ULONG InBuffer[sizeof(XENIFACE_GNTTAB_CREATE_MAPPING_IN) / sizeof(ULONG) +
+ XENIFACE_GNTTAB_BATCH_COUNT_MAX];
+ XENIFACE_GNTTAB_MAP_PAGES_IN *In = (PVOID)InBuffer;
+ XENIFACE_GNTTAB_MAP_PAGES_OUT Out = {0};
+ DWORD Status, InSize;
+
+ if (Id != NULL)
+ *Id = 0;
+
+ if (Address != NULL)
+ *Address = NULL;
+
+ if (Xc == NULL || Id == NULL || Address == NULL || References == NULL ||
+ NumberPages == 0 || NumberPages > XENIFACE_GNTTAB_BATCH_COUNT_MAX)
+ return ERROR_INVALID_PARAMETER;
+
+ In->Mapping.NumberPages = NumberPages;
+ In->Mapping.RemoteDomain = RemoteDomain;
+ In->Mapping.Reserved = 0;
+ In->Mapping.Flags = Flags;
+ In->Mapping.NotifyOffset = NotifyOffset;
+ In->Mapping.NotifyPort = NotifyPort;
+ memcpy(In->References, References, NumberPages * sizeof(ULONG));
+ InSize = (DWORD)FIELD_OFFSET(XENIFACE_GNTTAB_MAP_PAGES_IN,
References[NumberPages]);
+ Status = XcGnttabControl(Xc, IOCTL_XENIFACE_GNTTAB_MAP_PAGES,
+ In, InSize, &Out, sizeof(Out));
+
+ if (Status == ERROR_SUCCESS && (Out.Id == 0 || Out.Address == 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;
+ *Address = (PVOID)Out.Address;
+ }
+ return Status;
+}
+
DWORD
XcGnttabUnmapPages(
_In_ PXENCONTROL_CONTEXT Xc,
diff --git a/src/xeniface/ioctl_gnttab.c b/src/xeniface/ioctl_gnttab.c
index 38730ee..a03d800 100644
--- a/src/xeniface/ioctl_gnttab.c
+++ b/src/xeniface/ioctl_gnttab.c
@@ -698,6 +698,56 @@ IoctlGnttabDestroyMapping(
return Status;
}
+_IRQL_requires_(PASSIVE_LEVEL)
+NTSTATUS
+IoctlGnttabMapPages(
+ _In_ PXENIFACE_FDO Fdo,
+ _Inout_ PVOID Buffer,
+ _In_ ULONG InLen,
+ _In_ ULONG OutLen,
+ _In_opt_ PFILE_OBJECT FileObject,
+ _Out_ PULONG_PTR Info
+ )
+{
+ XENIFACE_GNTTAB_MAP_PAGES_IN *Pages = Buffer;
+ XENIFACE_GNTTAB_MAP_PAGES_OUT *Out = Buffer;
+ PXENIFACE_GNTTAB_CONTEXT Context;
+ NTSTATUS Status;
+ ULONG Count;
+
+ *Info = 0;
+ if (InLen < (ULONG)FIELD_OFFSET(XENIFACE_GNTTAB_MAP_PAGES_IN, References)
|| OutLen != sizeof(*Out))
+ return STATUS_INVALID_BUFFER_SIZE;
+
+ Count = Pages->Mapping.NumberPages;
+ if (Count == 0 || Count > XENBUS_GNTTAB_MAP_BATCH_COUNT_MAX)
+ return STATUS_INVALID_PARAMETER;
+
+ if (InLen != (ULONG)FIELD_OFFSET(XENIFACE_GNTTAB_MAP_PAGES_IN,
References[Count]))
+ return STATUS_INVALID_BUFFER_SIZE;
+
+ Status = GnttabAllocateMappingContext(Fdo, &Pages->Mapping, FileObject,
&Context);
+ if (!NT_SUCCESS(Status))
+ return Status;
+
+ Status = GnttabMapBatch(Fdo, Context, Pages->References);
+ if (NT_SUCCESS(Status))
+ Status = GnttabCreateUserView(Fdo, Context);
+
+ if (!NT_SUCCESS(Status)) {
+ GnttabReleaseMap(Fdo, Context);
+ return Status;
+ }
+
+ Context->Map.MappingId = ++Fdo->LastMappingId;
+ Context->Map.NotifyOnCleanup = TRUE;
+ InsertTailList(&Fdo->MappingList, &Context->Entry);
+ Out->Id = Context->Map.MappingId;
+ Out->Address = (ULONGLONG)Context->UserVa;
+ *Info = sizeof(*Out);
+ return STATUS_SUCCESS;
+}
+
_IRQL_requires_(PASSIVE_LEVEL)
NTSTATUS
IoctlGnttabUnmapPages(
diff --git a/src/xeniface/ioctls.c b/src/xeniface/ioctls.c
index 7bc9cc0..713ff29 100644
--- a/src/xeniface/ioctls.c
+++ b/src/xeniface/ioctls.c
@@ -383,6 +383,10 @@ XenIfaceIoctl(
status = IoctlGnttabDestroyMapping(Fdo, Buffer, InLen, OutLen,
Stack->FileObject);
break;
+ case IOCTL_XENIFACE_GNTTAB_MAP_PAGES:
+ status = IoctlGnttabMapPages(Fdo, Buffer, InLen, OutLen,
Stack->FileObject, &Irp->IoStatus.Information);
+ break;
+
case IOCTL_XENIFACE_GNTTAB_UNMAP_PAGES:
Irp->IoStatus.Information = 0;
status = IoctlGnttabUnmapPages(Fdo, Buffer, InLen, OutLen,
Stack->FileObject);
diff --git a/src/xeniface/ioctls.h b/src/xeniface/ioctls.h
index d52a893..10b72ef 100644
--- a/src/xeniface/ioctls.h
+++ b/src/xeniface/ioctls.h
@@ -430,6 +430,19 @@ IoctlGnttabDestroyMapping(
_In_opt_ PFILE_OBJECT FileObject
);
+// One-shot map with legacy rollback behavior in case of failure
+// (1 to XENIFACE_GNTTAB_BATCH_COUNT_MAX pages).
+_IRQL_requires_(PASSIVE_LEVEL)
+NTSTATUS
+IoctlGnttabMapPages(
+ _In_ PXENIFACE_FDO Fdo,
+ _Inout_ PVOID Buffer,
+ _In_ ULONG InLen,
+ _In_ ULONG OutLen,
+ _In_opt_ PFILE_OBJECT FileObject,
+ _Out_ PULONG_PTR Info
+ );
+
// Release the mapping, retaining failed cleanup for retry.
_IRQL_requires_(PASSIVE_LEVEL)
NTSTATUS
--
2.56.0
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |