Linux核心開發之記憶體與I/O訪問(四)
阿新 • • 發佈:2019-01-24
// 核心模組載入函式int __init kmalloc_map_init(void) { ../申請裝置號,新增cedv結構體 buffer = kmalloc(BUF_SIZE, GFP_KERNEL); //申請bufferfor(page = virt_to_page(buffer); page< virt_to_page(buffer+BUF_SIZE); page++) { mem_map_reserve(page); //置業為保留 } } //mmap()函式staticint kmalloc_map_mmap(struct file *filp, structvm_area_struct *vma) { unsignedlong page, pos; unsignedlong start = (unsignedlong)vma->start; unsignedlong size = (unsignedlong)(vma->end - vma->start); printk(KERN_INFO, "mmaptest_mmap called\n"); if(size > BUF_SIZE) //使用者要對映的區域太大return - EINVAL; pos = (unsignedlong)buffer; while(size > 0) //對映buffer中的所有頁 { page = virt_to_phys((void *)pos); if(remap_page_range(start, page, PAGE_SIZE, PAGE_SHARRED)) return -EAGAIN; start += PAGE_SIZE; pos +=PAGE_SIZE; size -= PAGE_SIZE; } return 0; }