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);