阅读:1171回复:2
请问有关内存映射的问题?
请问用DDk编程中,内存映射是什么意思?以下代码什么意思
NTSTATUS MapMemoryToUserSpace( IN ULONG pmbar, IN ULONG length, IN INTERFACE_TYPE itype, IN ULONG bno, OUT PVOID *umapaddr ) { NTSTATUS status; UNICODE_STRING phyUnicodeString; OBJECT_ATTRIBUTES objattr; HANDLE PMHandle = NULL; PVOID PMSection = NULL; ULONG AddressSpace = 0L; PHYSICAL_ADDRESS BusAddress; PHYSICAL_ADDRESS pMapAdrBase; PHYSICAL_ADDRESS pMapAdrEnd; PHYSICAL_ADDRESS viewBase; PHYSICAL_ADDRESS mappedLength; BOOLEAN transBaseAddr; BOOLEAN transEndAddr; // Create a physical address long integer BusAddress.HighPart = 0x0; BusAddress.LowPart = (ULONG) pmbar; // Create the name RtlInitUnicodeString( &phyUnicodeString, L"\\Device\\PhysicalMemory" ); // Initialize the data so we can find the object InitializeObjectAttributes( &objattr, &phyUnicodeString, OBJ_CASE_INSENSITIVE, (HANDLE) NULL, (PSECURITY_DESCRIPTOR) NULL ); // Open a handle to the oject and check its status status = ZwOpenSection( &PMHandle, SECTION_ALL_ACCESS, &objattr ); if (!NT_SUCCESS(status)) { KdPrint(("PCI2040: ZwOpenSection failed\n")); return status; } // Get a pointer to this object status = ObReferenceObjectByHandle( PMHandle, SECTION_ALL_ACCESS, (POBJECT_TYPE) NULL, KernelMode, &PMSection, (POBJECT_HANDLE_INFORMATION) NULL ); if (!NT_SUCCESS(status)) { KdPrint(("PCI2040: ObReferenceObjectByHandle failed\n")); ZwClose(PMHandle); return status; } // Initialize the physical addresses that will be translated KdPrint (("PCI2040: HalTranslateBusAddress Start Interface = %d, bno = %d, Bar = 0x%X\n", itype, bno, BusAddress.LowPart )); // Translate the physical addresses. AddressSpace = 0L; if( !HalTranslateBusAddress( itype, bno, BusAddress, &AddressSpace, &pMapAdrBase )) { KdPrint (("PCI2040: HalTranslatephysicalAddress start failed\n")); ZwClose(PMHandle); return STATUS_UNSUCCESSFUL; } BusAddress = RtlLargeIntegerAdd( BusAddress, RtlConvertUlongToLargeInteger(length) ); KdPrint (("PCI2040: HalTranslateBusAddress Emd Interface = %d, bno = %d, Bar = 0x%X\n", itype, bno, BusAddress.LowPart )); AddressSpace = 0L; if( !HalTranslateBusAddress( itype, bno, BusAddress, &AddressSpace, &pMapAdrEnd )) { KdPrint (("PCI2040: HalTranslatephysicalAddress end failed\n")); ZwClose(PMHandle); return STATUS_UNSUCCESSFUL; } // Calculate the length of the memory to be mapped mappedLength = RtlLargeIntegerSubtract( pMapAdrEnd, pMapAdrBase ); length = mappedLength.LowPart; // Map the section viewBase = pMapAdrBase; *umapaddr = NULL; status = ZwMapViewOfSection( PMHandle, (HANDLE) -1, umapaddr, 0L, length, &viewBase, &length, ViewShare, 0, PAGE_READWRITE | PAGE_NOCACHE ); if (!NT_SUCCESS(status)) { KdPrint(("PCI2040: ZwMapViewOfSection failed\n")); ZwClose(PMHandle); return status; } *umapaddr = (PVOID) (((ULONG) *umapaddr) + ((ULONG) pMapAdrBase.LowPart - (ULONG) viewBase.LowPart)); KdPrint(("PCI2040: ZwMapViewOfSection succeeded, returning 0x%X\n", *umapaddr)); return STATUS_SUCCESS; } |
|
沙发#
发布于:2004-02-20 10:02
这段代码是将一段内存(也有可能是某块卡上的RAM或寄存器)映射到用户空间,可以在应用模式来访问这段内存。
|
|
|
板凳#
发布于:2004-02-19 17:20
老兄,前段时间我也为内存映射烦了一阵子,不过我的是ISA的,现在搞通了,还得多谢Arthur Tu和冷老大的帮忙.但你的贴上好像是PCI的,我就不大懂了,有空多问问老大们,他们有办法...
|
|
|