?? mapmem.c
字號(hào):
// soon as you call IoCompleteRequest.
//
ntStatus = Irp->IoStatus.Status;
IoCompleteRequest(Irp, IO_NO_INCREMENT);
//
// We never have pending operation so always return the status code.
//
return ntStatus;
}
VOID
MapMemUnload(
IN PDRIVER_OBJECT pDriverObject
)
/*++
Routine Description:
Just delete the associated device & return.
Arguments:
DriverObject - pointer to a driver object
Return Value:
--*/
{
WCHAR deviceLinkBuffer[] = L"\\DosDevices\\MAPMEM";
UNICODE_STRING deviceLinkUnicodeString;
//
// Free any resources
//
//
// Delete the symbolic link
//
RtlInitUnicodeString (&deviceLinkUnicodeString,
deviceLinkBuffer
);
IoDeleteSymbolicLink (&deviceLinkUnicodeString);
//
// Delete the device object
//
MapMemKdPrint (("MAPMEM.SYS: unloading\n"));
IoDeleteDevice (pDriverObject->DeviceObject);
}
NTSTATUS
MapMemMapTheMemory(
IN PDEVICE_OBJECT DeviceObject,
IN OUT PVOID IoBuffer,
IN ULONG InputBufferLength,
IN ULONG OutputBufferLength
)
/*++
Routine Description:
Given a physical address, maps this address into a user mode process's
address space
Arguments:
DeviceObject - pointer to a device object
IoBuffer - pointer to the I/O buffer
InputBufferLength - input buffer length
OutputBufferLength - output buffer length
Return Value:
STATUS_SUCCESS if sucessful, otherwise
STATUS_UNSUCCESSFUL,
STATUS_INSUFFICIENT_RESOURCES,
(other STATUS_* as returned by kernel APIs)
--*/
{
PPHYSICAL_MEMORY_INFO ppmi = (PPHYSICAL_MEMORY_INFO) IoBuffer;
INTERFACE_TYPE interfaceType;
ULONG busNumber;
PHYSICAL_ADDRESS physicalAddress;
ULONG length;
UNICODE_STRING physicalMemoryUnicodeString;
OBJECT_ATTRIBUTES objectAttributes;
HANDLE physicalMemoryHandle = NULL;
PVOID PhysicalMemorySection = NULL;
ULONG inIoSpace, inIoSpace2;
NTSTATUS ntStatus;
PHYSICAL_ADDRESS physicalAddressBase;
PHYSICAL_ADDRESS physicalAddressEnd;
PHYSICAL_ADDRESS viewBase;
PHYSICAL_ADDRESS mappedLength;
BOOLEAN translateBaseAddress;
BOOLEAN translateEndAddress;
PVOID virtualAddress;
if ( ( InputBufferLength < sizeof (PHYSICAL_MEMORY_INFO) ) ||
( OutputBufferLength < sizeof (PVOID) ) )
{
MapMemKdPrint (("MAPMEM.SYS: Insufficient input or output buffer\n"));
ntStatus = STATUS_INSUFFICIENT_RESOURCES;
goto done;
}
interfaceType = ppmi->InterfaceType;
busNumber = ppmi->BusNumber;
physicalAddress = ppmi->BusAddress;
inIoSpace = inIoSpace2 = ppmi->AddressSpace;
length = ppmi->Length;
//
// Get a pointer to physical memory...
//
// - Create the name
// - Initialize the data to find the object
// - Open a handle to the oject and check the status
// - Get a pointer to the object
// - Free the handle
//
RtlInitUnicodeString (&physicalMemoryUnicodeString,
L"\\Device\\PhysicalMemory");
InitializeObjectAttributes (&objectAttributes,
&physicalMemoryUnicodeString,
OBJ_CASE_INSENSITIVE,
(HANDLE) NULL,
(PSECURITY_DESCRIPTOR) NULL);
ntStatus = ZwOpenSection (&physicalMemoryHandle,
SECTION_ALL_ACCESS,
&objectAttributes);
if (!NT_SUCCESS(ntStatus))
{
MapMemKdPrint (("MAPMEM.SYS: ZwOpenSection failed\n"));
goto done;
}
ntStatus = ObReferenceObjectByHandle (physicalMemoryHandle,
SECTION_ALL_ACCESS,
(POBJECT_TYPE) NULL,
KernelMode,
&PhysicalMemorySection,
(POBJECT_HANDLE_INFORMATION) NULL);
if (!NT_SUCCESS(ntStatus))
{
MapMemKdPrint (("MAPMEM.SYS: ObReferenceObjectByHandle failed\n"));
goto close_handle;
}
//
// Initialize the physical addresses that will be translated
//
physicalAddressEnd = RtlLargeIntegerAdd (physicalAddress,
RtlConvertUlongToLargeInteger(
length));
//
// Translate the physical addresses.
//
translateBaseAddress =
HalTranslateBusAddress (interfaceType,
busNumber,
physicalAddress,
&inIoSpace,
&physicalAddressBase);
translateEndAddress =
HalTranslateBusAddress (interfaceType,
busNumber,
physicalAddressEnd,
&inIoSpace2,
&physicalAddressEnd);
if ( !(translateBaseAddress && translateEndAddress) ) {
MapMemKdPrint (("MAPMEM.SYS: HalTranslatephysicalAddress failed\n"));
ntStatus = STATUS_UNSUCCESSFUL;
goto close_handle;
}
//
// Calculate the length of the memory to be mapped
//
mappedLength = RtlLargeIntegerSubtract (physicalAddressEnd,
physicalAddressBase);
//
// If the mappedlength is zero, somthing very weird happened in the HAL
// since the Length was checked against zero.
//
if (mappedLength.LowPart == 0) {
MapMemKdPrint (("MAPMEM.SYS: mappedLength.LowPart == 0\n"));
ntStatus = STATUS_UNSUCCESSFUL;
goto close_handle;
}
length = mappedLength.LowPart;
//
// If the address is in io space, just return the address, otherwise
// go through the mapping mechanism
//
if (inIoSpace) {
*((PVOID *) IoBuffer) = (PVOID) physicalAddressBase.LowPart;
} else {
//
// initialize view base that will receive the physical mapped
// address after the MapViewOfSection call.
//
viewBase = physicalAddressBase;
//
// Let ZwMapViewOfSection pick an address
//
virtualAddress = NULL;
//
// Map the section
//
ntStatus = ZwMapViewOfSection (physicalMemoryHandle,
(HANDLE) -1,
&virtualAddress,
0L,
length,
&viewBase,
&length,
ViewShare,
0,
PAGE_READWRITE | PAGE_NOCACHE);
if (!NT_SUCCESS(ntStatus))
{
MapMemKdPrint (("MAPMEM.SYS: ZwMapViewOfSection failed\n"));
goto close_handle;
}
//
// Mapping the section above rounded the physical address down to the
// nearest 64 K boundary. Now return a virtual address that sits where
// we want by adding in the offset from the beginning of the section.
//
(ULONG) virtualAddress += (ULONG)physicalAddressBase.LowPart -
(ULONG)viewBase.LowPart;
*((PVOID *) IoBuffer) = virtualAddress;
}
ntStatus = STATUS_SUCCESS;
close_handle:
ZwClose (physicalMemoryHandle);
done:
return ntStatus;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -