adriod HAL层 内核空间与用户空间内存共享

1、内核部分

unsigned long gMmapAddr;

unsigned long lMmapAddr;

 

static int xxx_mmap(struct file *filp, struct vm_area_struct *vma) {  vma->vm_pgoff = ((unsigned long)virt_to_phys(gMmapAddr)) >> PAGE_SHIFT;     printk("vma->vm_pgoff = %lx\n",vma->vm_pgoff);        if (remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, sizeof(gMmapAddr),  vma->vm_page_prot))   {   printk("remap_pfn_range error\n");   return -EAGAIN;  }

 return 0; }

 

static int __init shareMmap_init(void) {

   gMmapAddr  = PAGE_ALIGN((UINT32)lMmapAddr);//页对齐,重要
 
   return 0;
}
 
EXPORT_SYMBOL_GPL(gMmapAddr);
这后可以引用 gMmapAddr
 
===================================================================================================================================
===================================================================================================================================
 
2、应用层
 
unsigned long *comAddr;
 
 //打开和共享内存驱动
  fd = open("/dev/map",O_RDWR);
 if (fd < 0)
 {
  return FALSE;
 }
 
 comAddr  = (unsigned long*)mmap(NULL, sizeof(unsigned long), PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED, fd, 0);
 

   

你可能感兴趣的:(adriod HAL层 内核空间与用户空间内存共享)