Previous Next

ZwMapViewOfSection

The ZwMapViewOfSection routine maps a view of a section into the virtual address space of a subject process.

NTSTATUS 
  ZwMapViewOfSection(
    IN HANDLE  SectionHandle,
    IN HANDLE  ProcessHandle,
    IN OUT PVOID  *BaseAddress,
    IN ULONG  ZeroBits,
    IN ULONG  CommitSize,
    IN OUT PLARGE_INTEGER  SectionOffset  OPTIONAL,
    IN OUT PSIZE_T  ViewSize,
    IN SECTION_INHERIT  InheritDisposition,
    IN ULONG  AllocationType,
    IN ULONG  Protect
    );

Parameters

SectionHandle
Handle to a section object. This handle is created by a successful call to ZwCreateSection or ZwOpenSection.
ProcessHandle
Handle of an opened process object, representing the process for which the view should be mapped. Use the NtCurrentProcess macro to specify the current process. The handle must have PROCESS_VM_OPERATION access to the process (described in the Platform SDK documentation).
BaseAddress
Pointer to a variable that will receive the base address of the view. If the initial value of this argument is non-NULL, the view is allocated starting at the specified virtual address rounded down to the next 64-kilobyte address boundary.
ZeroBits
Specifies the number of high-order address bits that must be zero in the base address of the section view. The value of this argument must be less than 21 and is used only when the operating system determines where to allocate the view, as when BaseAddress is NULL.
CommitSize
Specifies the size, in bytes, of the initially committed region of the view. CommitSize is only meaningful for page-file backed sections. For mapped sections, both data and image are always committed at section creation time. This parameter is ignored for mapped files. This value is rounded up to the next host-page-size boundary.
SectionOffset
Pointer to the offset, in bytes, from the beginning of the section to the view. If this pointer is non-NULL, the given value is rounded down to the next allocation granularity size boundary.
ViewSize
Pointer to a variable that will receive the actual size, in bytes, of the view. If the value of this parameter is zero, a view of the section will be mapped starting at the specified section offset and continuing to the end of the section. Otherwise, the initial value of this argument specifies the size of the view, in bytes, and is rounded up to the next host-page-size boundary.
InheritDisposition
Specifies how the view is to be shared by a child process created with a create process operation. Device and intermediate drivers should set this parameter to zero.
AllocationType
Specifies a set of flags that describes the type of allocation to be performed for the specified region of pages. The valid flags are MEM_COMMIT, MEM_PHYSICAL, MEM_RESERVE, and MEM_TOP_DOWN. For more information on the MEM_XXX flags, see the VirtualAlloc routine in the Platform SDK.
Protect
Specifies the protection for the region of initially committed pages. Device and intermediate drivers should set this value to PAGE_READWRITE.

Return Value

ZwMapViewOfSection returns an NTSTATUS value. Possible return values include:

STATUS_SUCCESS
The routine successfully performed the requested operation.
STATUS_CONFLICTING_ADDRESSES
The specified address range conflicts with an address range already reserved, or the specified cache attribute type conflicts with the address range's existing cache attribute. For example, if the memory being mapped lies within a large page that is already mapped as fully cached, then it is illegal to request to map this memory as noncached or write combined.
STATUS_INVALID_PAGE_PROTECTION
The value specified for the Protect parameter is invalid.
STATUS_SECTION_PROTECTION
The value specified for the AllocationType parameter is incompatible with the protection type specified when the section was created.

Headers

Declared in wdm.h and ntddk.h. Include wdm.h or ntddk.h.

Comments

Several different views of a section can be concurrently mapped into the virtual address space of one or more processes.

If the specified section does not exist or the access requested is not allowed, ZwMapViewOfSection returns an error.

Do not use ZwMapViewOfSection to map a memory range in \Device\PhysicalMemory into user mode, unless your driver has allocated the memory range directly with MmAllocatePagesForMdl (or any equivalent method that guarantees no other system component has mapped the same memory range with a different MEMORY_CACHING_TYPE value).

Callers of ZwMapViewOfSection must be running at IRQL = PASSIVE_LEVEL.

See Also

ZwOpenSection, ZwUnmapViewOfSection