Retrieving MmPhysicalMemoryBlock regardless of the NT version.

 

Here is a method I’m using in the next version of Win32DD (1.2), to retrieve MmPhysicalMemoryBlock regardless of the NT Version. The main problem with KDDEBUGGER_DATA64 structure is the version dependency. Then, we have to rebuild this field by ourselves.

To retrieve physical memory runs, I’m using MmGetPhysicalMemoryRanges() *undocumented* function. This function usage had been documented by Mark Russinovich in 1999, in the Volume 1 Number 5 edition of the Sysinternals Newsletter.

Actually, this function is defined in DDK. Even if, MSDN says “The following routines are reserved for system use. Do not use them in your driver.”

  1. #if (NTDDI_VERSION >= NTDDI_WIN2K)
  2. NTKERNELAPI
  3. PPHYSICAL_MEMORY_RANGE
  4. MmGetPhysicalMemoryRanges (
  5.     VOID
  6.     );
  7. #endif

MmPhysicalMemoryBlock is a structure that provides information regarding the physical memory ranges used by the system and also total physical memory size. These uses motivated me to write MmGetPhysicalMemoryBlock().

  1. []
  2.     // NT 5.1 Addition
  3.     ULONG64   MmPhysicalMemoryBlock;
  4. [.. ]

As we can read in the KDDEBUGGER_DATA64 definition, MmPhysicalMemoryBlock field is an NT 5.1 Addition.

definition.

  1. typedef struct _PHYSICAL_MEMORY_RUN {
  2.     PFN_NUMBER BasePage;
  3.     PFN_NUMBER PageCount;
  4. } PHYSICAL_MEMORY_RUN, *PPHYSICAL_MEMORY_RUN;
  5.  
  6. typedef struct _PHYSICAL_MEMORY_DESCRIPTOR {
  7.     ULONG NumberOfRuns;
  8.     PFN_NUMBER NumberOfPages; // NumberOfPages * PAGE_SIZE is physical memory size.
  9.     PHYSICAL_MEMORY_RUN Run [ 1 ]; // NumberOfRuns is the total entries.
  10. } PHYSICAL_MEMORY_DESCRIPTOR, *PPHYSICAL_MEMORY_DESCRIPTOR;
  11.  
  12. PPHYSICAL_MEMORY_DESCRIPTOR
  13. MmGetPhysicalMemoryBlock (
  14.         VOID
  15. );

code.

  1. /*++
  2. Function Name: MmGetPhysicalMemoryBlock
  3.  
  4. Overview:
  5.         - This function aims at retrieving MmPhysicalMemoryBlock, regardless
  6.         of the host version.
  7.  
  8.         The caller has to free the memory block.
  9.  
  10. Parameters:
  11.         -
  12.  
  13. Environment:
  14.         - Kernel Mode. PASSIVE_LEVEL.
  15.  
  16. Return Values:
  17.         - PPHYSICAL_MEMORY_DESCRIPTOR
  18. –*/
  19. PPHYSICAL_MEMORY_DESCRIPTOR
  20. MmGetPhysicalMemoryBlock ( VOID
  21.                           )
  22. {
  23. PPHYSICAL_MEMORY_DESCRIPTOR MmPhysicalMemoryBlock;
  24. PPHYSICAL_MEMORY_RANGE MmPhysicalMemoryRange;
  25. ULONG MemoryBlockSize;
  26. PFN_NUMBER NumberOfPages;
  27. ULONG NumberOfRuns;
  28. ULONG Run;
  29.  
  30.     //
  31.     // PHYSICAL_MEMORY_DESCRIPTOR isn’t exported into KDDEBUGGER_DATA64
  32.     // NT 5.0 and below. But MmGetPhysicalMemoryRanges() computes
  33.     // PHYSICAL_MEMORY_RANGE with PHYSICAL_MEMORY_DESCRIPTOR. Then,
  34.     // We can easily rewrite PHYSICAL_MEMORY_DESCRIPTOR.
  35.     //
  36.     MmPhysicalMemoryRange = MmGetPhysicalMemoryRanges ( );
  37.  
  38.     //
  39.     // Invalid ?
  40.     //
  41.     if (MmPhysicalMemoryRange == NULL ) return NULL;
  42.  
  43.     //
  44.     // Compute the number of runs and the number of pages
  45.     //
  46.     NumberOfRuns = 0;
  47.     NumberOfPages = 0;
  48.     while ( (MmPhysicalMemoryRange [NumberOfRuns ]. BaseAddress. QuadPart != 0 ) &&
  49.             (MmPhysicalMemoryRange [NumberOfRuns ]. NumberOfBytes. QuadPart != 0 ) )
  50.     {
  51.         NumberOfRuns++;
  52.         NumberOfPages += (PFN_NUMBER )BYTES_TO_PAGES (
  53.             MmPhysicalMemoryRange [NumberOfRuns ]. NumberOfBytes. QuadPart );
  54.     }
  55.  
  56.     //
  57.     // Invalid ?
  58.     //
  59.     if (NumberOfRuns == 0 ) return NULL;
  60.  
  61.     //
  62.     // Compute the size of the pool to allocate and then allocate
  63.     //
  64.     MemoryBlockSize = sizeof (ULONG ) +
  65.         sizeof (PFN_NUMBER ) +
  66.         sizeof (PHYSICAL_MEMORY_RUN ) * NumberOfRuns;
  67.  
  68.     MmPhysicalMemoryBlock = ExAllocatePoolWithTag (NonPagedPool,
  69.                                                   MemoryBlockSize,
  70.                                                   ‘  mM’ );
  71.  
  72.     //
  73.     // Define PHYSICAL_MEMORY_DESCRIPTOR Header.=
  74.     //
  75.     MmPhysicalMemoryBlock->NumberOfRuns = NumberOfRuns;
  76.     MmPhysicalMemoryBlock->NumberOfPages = NumberOfPages;
  77.  
  78.     for (Run = 0; Run < NumberOfRuns; Run++ )
  79.     {
  80.         //
  81.         // BasePage
  82.         //
  83.         MmPhysicalMemoryBlock->Run [Run ]. BasePage =
  84.             (PFN_NUMBER )MI_CONVERT_PHYSICAL_TO_PFN (
  85.             MmPhysicalMemoryRange [NumberOfRuns ]. BaseAddress. QuadPart
  86.  
  87.             );
  88.  
  89.         //
  90.         // PageCount
  91.         //
  92.         MmPhysicalMemoryBlock->Run [Run ]. PageCount =
  93.             (PFN_NUMBER )BYTES_TO_PAGES (
  94.             MmPhysicalMemoryRange [Run ]. NumberOfBytes. QuadPart
  95.             );
  96.     }
  97.  
  98.     return MmPhysicalMemoryBlock;
  99. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Title: Android for the BeagleBone Black Author: Andrew Henderson, Aravind Prakash Length: 120 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2015-02-27 ISBN-10: 1784392162 ISBN-13: 9781784392161 Design and implement Android apps that interface with your own custom hardware circuits and the BeagleBone Black About This Book Design custom apps that interact with the outside world via BeagleBone Black Modify Android to recognize, configure, and communicate with sensors, LEDs, memory, and more A step-by-step guide full of practical Android app examples that will help the users to create Android controlled devices that will use BeagleBone as hardware Who This Book Is For If you are an Android app developer who wants to experiment with the hardware capabilities of the BeagleBone Black platform, then this book is ideal for you. You are expected to have basic knowledge of developing Android apps but no prior hardware experience is required. In Detail This book explores using the Android OS on the BeagleBone Black hardware platform and provides an introduction to Android's unique approach to hardware interfacing. You'll be walked through the process of installing and configuring Android on your BeagleBone Black, as well as preparing your PC development environment to create Android applications that directly interface with hardware devices. Several example projects within this book introduce you to using the GPIO, SPI, and I2C hardware interfaces of the BeagleBone Black. You'll create Android apps that communicate directly with actual hardware components such as sensors, memory chips, switches, and LEDs. Step-by-step guidance through both the software and hardware portions of these projects is provided. Combining all of the previous projects into a single project that uses GPIO, SPI, and I2C together, you will explore the details of creating an advanced hardware interfacing app. Finally, you'll be provided with information on transitioning prototype code into code suitable for deployment on an Android-based device. With a variety of example apps that demonstrate key hardware communication concepts, this book will help you become an Android hardware interfacing pro in no time. Table of Contents Chapter 1. Introduction to Android and the BeagleBone Black Chapter 2. Interfacing with Android Chapter 3. Handling Inputs and Outputs with GPIOs Chapter 4. Storing and Retrieving Data with I2C Chapter 5. Interfacing with High-speed Sensors Using SPI Chapter 6. Creating a Complete Interfacing Solution Chapter 7. Where to Go from Here

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值