RtlCopyDeviceMemory - NtDoc

Native API online documentation, based on the System Informer (formerly Process Hacker) phnt headers
// wdm.h

volatile void * RtlCopyDeviceMemory(
  [out] volatile void       *Destination,
  [in]  volatile const void *Source,
  [in]  size_t              Length
);
View the official Windows Driver Kit DDI reference

NtDoc

No description available.

Windows Driver Kit DDI reference (nf-wdm-rtlcopydevicememory)

Description

The RtlCopyDeviceMemory function provides RtlCopyVolatileMemory behavior (for example, copying memory from one location to another without interference from compiler optimizations) in situations where the developer needs to additionally be sure that alignment faults won't be generated when accessing device memory.

Parameters

Destination [out]

A pointer to the starting address of the copied block's destination.

Source [in]

A pointer to the starting address of the block of memory to copy.

Length [in]

The size of the block of memory to copy, in bytes.

Return value

Returns the value of Destination.

Remarks

The RtlCopyDeviceMemory function has the following properties:

[!NOTE] This function only guarantees that the CPU's requirements for accessing memory mapped as device memory are respected. If a specific device has its own specific requirements for being accessed, this function should not be used (and instead, the developer must implement their own accessor functions). For example, this function makes no guarantee about the size of memory accesses generated (unless the CPU itself enforces these requirements).

[!NOTE] This function works on all versions of Windows, not just the latest. You need to consume the latest WDK to get the function declaration from the wdm.h header. You also need the library (volatileaccessk.lib) from the latest WDK. However, the resulting driver will run fine on older versions of Windows.

Example

UCHAR* CopyBuffer;

// In this scenario we are copying data from memory mapped
// as "device memory" (for example, memory not backed by RAM). On
// some platforms like ARM64, device memory cannot tolerate
// memory accesses that are not naturally aligned (for example, a 4-byte
// load must be 4-byte aligned). Functions like memcpy, RtlCopyMemory,
// and even RtlCopyVolatileMemory may perform unaligned memory accesses
// because it is typically faster to do this.
// To ensure only naturally aligned accesses happen, use RtlCopyDeviceMemory.

RtlCopyDeviceMemory(CopyBuffer, DeviceMemoryBuffer, 100);

See also

RtlCopyMemory

RtlCopyVolatileMemory