📄 mem.c
字号:
return NULL; maps[numMappings].physical = base; maps[numMappings].length = length; maps[numMappings].linear = linear; maps[numMappings].isCached = isCached; numMappings++; // Grant user access to this I/O space _PM_adjustPageTables((ulong)linear, length, true, isCached); return (void*)linear;}/****************************************************************************REMARKS:Free a physical address mapping allocated by PM_mapPhysicalAddr.****************************************************************************/void PMAPI PM_freePhysicalAddr( void *ptr, ulong limit){ // We don't free the memory mappings in here because we cache all // the memory mappings we create in the system for later use.}/****************************************************************************REMARKS:Called when the device driver unloads to free all the page table mappings!****************************************************************************/void PMAPI _PM_freeMemoryMappings(void){ int i; for (i = 0; i < numMappings; i++) MmUnmapIoSpace((void *)maps[i].linear,maps[i].length);}/****************************************************************************REMARKS:Find the physical address of a linear memory address in current process.****************************************************************************/ulong PMAPI PM_getPhysicalAddr( void *p){ PHYSICAL_ADDRESS paOurAddress; paOurAddress = MmGetPhysicalAddress(p); return paOurAddress.LowPart;}/****************************************************************************REMARKS:Find the physical address of a linear memory address in current process.****************************************************************************/ibool PMAPI PM_getPhysicalAddrRange( void *p, ulong length, ulong *physAddress){ int i; ulong linear = (ulong)p & ~0xFFF; for (i = (length + 0xFFF) >> 12; i > 0; i--) { if ((*physAddress++ = PM_getPhysicalAddr((void*)linear)) == 0xFFFFFFFF) return false; linear += 4096; } return true;}/****************************************************************************REMARKS:Allocates a block of locked physical memory.****************************************************************************/void * PMAPI PM_allocLockedMem( uint size, ulong *physAddr, ibool contiguous, ibool below16M){ int i; PHYSICAL_ADDRESS paOurAddress; // First find a free slot in our shared memory table for (i = 0; i < MAX_MEMORY_LOCKED; i++) { if (locked[i].linear == 0) break; } if (i == MAX_MEMORY_LOCKED) return NULL; // HighestAcceptableAddress - Specifies the highest valid physical address // the driver can use. For example, if a device can only reference physical // memory in the lower 16MB, this value would be set to 0x00000000FFFFFF. paOurAddress.HighPart = 0; if (below16M) paOurAddress.LowPart = 0x00FFFFFF; else paOurAddress.LowPart = 0xFFFFFFFF; if (contiguous) { // Allocate from the non-paged pool (unfortunately 4MB pages) locked[i].linear = MmAllocateContiguousMemory(size, paOurAddress); if (!locked[i].linear) return NULL; // Flag no MDL locked[i].pMdl = NULL; // Map the physical address for the memory so we can manage // the page tables in 4KB chunks mapped into user space. // TODO: Map this with the physical address to the linear addresss locked[i].mmIoMapped = locked[i].linear; // Modify bits to grant user access, flag not cached _PM_adjustPageTables((ulong)locked[i].mmIoMapped, size, true, false); return (void*)locked[i].mmIoMapped; } else { // Allocate from the paged pool locked[i].linear = ExAllocatePool(PagedPool, size); if (!locked[i].linear) return NULL; // Create a list to manage this allocation locked[i].pMdl = IoAllocateMdl(locked[i].linear,size,FALSE,FALSE,(PIRP) NULL); // Lock this allocation in memory MmProbeAndLockPages(locked[i].pMdl,KernelMode,IoModifyAccess); // Modify bits to grant user access, flag not cached _PM_adjustPageTables((ulong)locked[i].linear, size, true, false); return (void*)locked[i].linear; }}/****************************************************************************REMARKS:Frees a block of locked physical memory.****************************************************************************/void PMAPI PM_freeLockedMem( void *p, uint size, ibool contiguous){ int i; /* Find a locked memory block in our table and free it */ for (i = 0; i < MAX_MEMORY_LOCKED; i++) { if (locked[i].linear == p) { // An Mdl indicates that we used the paged pool, and locked it, // so now we have to unlock, free the MDL, and free paged if (locked[i].pMdl) { // Unlock what we locked and free the Mdl MmUnlockPages(locked[i].pMdl); IoFreeMdl(locked[i].pMdl); ExFreePool(locked[i].linear); } else { // TODO: Free the mmIoMap mapping for the memory! // Free non-paged pool MmFreeContiguousMemory(locked[i].linear); } // Flag that is entry is available locked[i].linear = 0; break; } }}/****************************************************************************REMARKS:Allocates a page aligned and page sized block of memory****************************************************************************/void * PMAPI PM_allocPage( ibool locked){ // Allocate the memory from the non-paged pool if we want the memory // to be locked. return ExAllocatePool( locked ? NonPagedPoolCacheAligned : PagedPoolCacheAligned, PAGE_SIZE);}/****************************************************************************REMARKS:Free a page aligned and page sized block of memory****************************************************************************/void PMAPI PM_freePage( void *p){ if (p) ExFreePool(p);}/****************************************************************************REMARKS:Lock linear memory so it won't be paged.****************************************************************************/int PMAPI PM_lockDataPages( void *p, uint len, PM_lockHandle *lh){ MDL *pMdl; // Create a list to manage this allocation if ((pMdl = IoAllocateMdl(p,len,FALSE,FALSE,(PIRP)NULL)) == NULL) return false; // Lock this allocation in memory MmProbeAndLockPages(pMdl,KernelMode,IoModifyAccess); *((PMDL*)(&lh->h)) = pMdl; return true;}/****************************************************************************REMARKS:Unlock linear memory so it won't be paged.****************************************************************************/int PMAPI PM_unlockDataPages( void *p, uint len, PM_lockHandle *lh){ if (p && lh) { // Unlock what we locked MDL *pMdl = *((PMDL*)(&lh->h)); MmUnlockPages(pMdl); IoFreeMdl(pMdl); } return true;}/****************************************************************************REMARKS:Lock linear memory so it won't be paged.****************************************************************************/int PMAPI PM_lockCodePages( void (*p)(), uint len, PM_lockHandle *lh){ return PM_lockDataPages((void*)p,len,lh);}/****************************************************************************REMARKS:Unlock linear memory so it won't be paged.****************************************************************************/int PMAPI PM_unlockCodePages( void (*p)(), uint len, PM_lockHandle *lh){ return PM_unlockDataPages((void*)p,len,lh);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -