cma --- linux驱动中分配连续物理内存

linux驱动中分配连续物理内存

分类: linux kernel linux 设备驱动 linux 移植 81人阅读 评论(0) 收藏 举报
移植exynos4412的MFC驱动时,出现了
dma_alloc_coherent失败,于是展开其了解作用,如下:
dma_alloc_coherent & dma_alloc_writecombine
都是分配连续物理内存,返回虚拟地址
dma_alloc_coherent
#define pgprot_noncached(prot) __pgprot(pgprot_val(prot) & ~(L_PTE_CACHEABLE | L_PTE_BUFFERABLE))
禁用cahceable 和 bufferable

dma_alloc_writecombine
#define pgprot_writecombine(prot) __pgprot(pgprot_val(prot) & ~L_PTE_CACHEABLE)
禁用cahceable 

谈到
dma_alloc_coherent ,就不得不提
cma,和ump
cma & ump


cma: 管理连续内存,可以预留好,可以用到时分配。4412BSP中为预留。

ump:mali使用内存,使用时动态配分(alloc_page)

无法获得分配物理内存的权限

09-07

我调试msdn中的分配物理内存的示例代码,如下,编译通过可是设置开辟内存权限时,程序执行了rn这条语句printf ("Cannot enable SE_LOCK_MEMORY privilege, please check the local policy.\n");(如下)而无法获得相应权限,请问各位高人,我如何做才能获得开辟物理内存的权限?rn#define _WIN32_WINNT 0x0500rn#include rn#include rn#include rnrn#define _WIN32_WINNT 0x0500rn#define MEMORY_REQUESTED 1024*1024 // request a megabyternrnBOOLrnLoggedSetLockPagesPrivilege ( HANDLE hProcess,rn BOOL bEnable);rnrnvoid _cdecl main()rnrn BOOL bResult; // generic Boolean valuern ULONG_PTR NumberOfPages; // number of pages to requestrn ULONG_PTR NumberOfPagesInitial; // initial number of pages requestedrn ULONG_PTR *aPFNs; // page info; holds opaque datarn PVOID lpMemReserved; // AWE windowrn SYSTEM_INFO sSysInfo; // useful system informationrn int PFNArraySize; // memory to request for PFN arrayrnrn GetSystemInfo(&sSysInfo); // fill the system information structurernrn printf ("This computer has a page size of %d.\n", sSysInfo.dwPageSize);rnrn // Calculate the number of pages of memory to request.rnrn NumberOfPages = MEMORY_REQUESTED/sSysInfo.dwPageSize;rn printf ("Requesting %d pages of memory.\n", NumberOfPages);rnrn // Calculate the size of the user PFN array.rnrn PFNArraySize = NumberOfPages * sizeof (ULONG_PTR);rnrn printf ("Requesting a PFN array of %d bytes.\n", PFNArraySize);rnrn aPFNs = (ULONG_PTR *) HeapAlloc (GetProcessHeap (), 0, PFNArraySize);rnrn if (aPFNs == NULL) rn printf ("Failed to allocate on heap.\n");rn return;rn rnrn // Enable the privilege.rnrn if( ! LoggedSetLockPagesPrivilege( GetCurrentProcess(), TRUE ) ) rn return;rn rnrn // Allocate the physical memory.rnrn NumberOfPagesInitial = NumberOfPages;rn bResult = AllocateUserPhysicalPages( GetCurrentProcess(),rn &NumberOfPages,rn aPFNs );rn rn if( bResult != TRUE ) rn rn printf("Cannot allocate physical pages, error %u.\n", GetLastError() );rn return;rn rnrn if( NumberOfPagesInitial != NumberOfPages ) rn rn printf("Allocated only %p pages.\n", NumberOfPages );rn return;rn rnrn // Reserve the virtual memory.rn rn lpMemReserved = VirtualAlloc( NULL,rn MEMORY_REQUESTED,rn MEM_RESERVE | MEM_PHYSICAL,rn PAGE_READWRITE );rnrn if( lpMemReserved == NULL ) rn rn printf("Cannot reserve memory.\n");rn return;rn rnrn // Map the physical memory into the window.rn rn bResult = MapUserPhysicalPages( lpMemReserved,rn NumberOfPages,rn aPFNs );rnrn if( bResult != TRUE ) rn rn printf("MapUserPhysicalPages failed to map, error %u.\n", GetLastError() );rn return;rn rnrn // unmaprn rn bResult = MapUserPhysicalPages( lpMemReserved,rn NumberOfPages,rn NULL );rnrn if( bResult != TRUE ) rn rn printf("MapUserPhysicalPages failed to unmap, error %u.\n", GetLastError() );rn return;rn rnrn // Free the physical pages.rnrn bResult = FreeUserPhysicalPages( GetCurrentProcess(),rn &NumberOfPages,rn aPFNs );rnrn if( bResult != TRUE ) rn rn printf("Cannot free physical pages, error %u.\n", GetLastError() );rn return;rn rnrn // Free virtual memory.rnrn bResult = VirtualFree( lpMemReserved,rn 0,rn MEM_RELEASE );rnrnrn/*****************************************************************rn LoggedSetLockPagesPrivilege: a function to obtain, if possible, orrn release the privilege of locking physical pages.rnrn Inputs:rnrn HANDLE hProcess: Handle for the process for which thern privilege is neededrnrn BOOL bEnable: Enable (TRUE) or disable?rnrn Return value: TRUE indicates success, FALSE failure.rnrn*****************************************************************/rnBOOLrnLoggedSetLockPagesPrivilege ( HANDLE hProcess,rn BOOL bEnable)rnrn struct rn DWORD Count;rn LUID_AND_ATTRIBUTES Privilege [1];rn Info;rnrn HANDLE Token;rn BOOL Result;rnrn // Open the token.rnrn Result = OpenProcessToken ( hProcess,rn TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES,rn & Token);rnrn if( Result != TRUE ) rn printf( "Cannot open process token.\n" );rn return FALSE;rn rnrn // Enable or disable?rnrn Info.Count = 1;rn if( bEnable ) rn rn Info.Privilege[0].Attributes = SE_PRIVILEGE_ENABLED;rn rn else rn rn Info.Privilege[0].Attributes = 0;rn rnrn // Get the LUID.rnrn Result = LookupPrivilegeValue ( NULL,rn SE_LOCK_MEMORY_NAME,rn &(Info.Privilege[0].Luid));rnrn if( Result != TRUE ) rn rn printf( "Cannot get privilege value for %s.\n", SE_LOCK_MEMORY_NAME );rn return FALSE;rn rnrn // Adjust the privilege.rnrn Result = AdjustTokenPrivileges ( Token, FALSE,rn (PTOKEN_PRIVILEGES) &Info,rn NULL, NULL, NULL);rnrn // Check the result.rnrn if( Result != TRUE ) rn rn printf ("Cannot adjust token privileges, error %u.\n", GetLastError() );rn return FALSE;rn rn else rn rn if( GetLastError() != ERROR_SUCCESS ) rn rn printf ("Cannot enable SE_LOCK_MEMORY privilege, please check the local policy.\n");rn return false;rn rn rnrn CloseHandle( Token );rnrn return TRUE;rnrn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试