linux記憶體頁面分配問題 page_alloc, page_address, pa

來源:互聯網
上載者:User
驅動代碼
dma.c

#include <linux/init.h>#include <linux/module.h>#include <linux/gfp.h>#include <linux/mm.h>MODULE_LICENSE("DUAL BSD/GPL");staticint __init alloc_pages_init(void);staticvoid __exit alloc_pages_exit(void);struct page *pages = NULL;int__init alloc_pages_init(void){pages = alloc_pages(GFP_KERNEL, 3);if(!pages)return -ENOMEM;else{printk(KERN_ALERT "alloc_pages Successfully!\n");printk(KERN_ALERT "pages=0x%lx\n", (unsigned long)pages);printk(KERN_ALERT "size of 'pages'=%ld\n", sizeof pages);printk(KERN_ALERT "pages=0x%lx\n", pages);printk(KERN_ALERT "mem_map = 0x%lx\n",(unsigned long)mem_map);printk(KERN_ALERT "size of 'mem_map'=%ld\n", sizeof mem_map);printk(KERN_ALERT "mem_map = 0x%lx\n",mem_map);printk(KERN_ALERT "pages - mem_map = 0x%lx\n",(unsigned long)pages - (unsigned long)mem_map);printk(KERN_ALERT "pages - mem_map = 0x%lx\n",(unsigned long)(pages - mem_map));//the physical address of the head of the pagesprintk(KERN_ALERT "(pages - mem_map) << 12 = 0x%lx\n",((unsigned long)pages - (unsigned long) mem_map) * 4096);printk(KERN_ALERT "(pages - mem_map) << 12 = 0x%lx\n",(unsigned long)(pages - mem_map) * 4096);//the kernel logic address of the head of the pagesprintk(KERN_ALERT "page_address(pages) = 0x%lx\n", (unsigned long)page_address(pages));}return 0;}void__exit alloc_pages_exit(void){if(pages){__free_pages(pages, 3);printk(KERN_ALERT "__free_pages ok!\n");}printk(KERN_ALERT "exit\n");}module_init(alloc_pages_init);module_exit(alloc_pages_exit);

Makefile

KERNELDIR=/lib/modules/$(shell uname -r)/buildobj-m = dma.omodules:$(MAKE) -C $(KERNELDIR) M=`pwd` modulesclean:rm -rf *~ *.o *.ko *.mod* *.order *.mar* *.sym*


測試指令碼
make cleanmakesudo rmmod dmasudo insmod dma.kodmesg

我想學習linux記憶體頁面分配相關的知識,用到alloc_pages和page_address函數,其中在alloc_pages函數中我遇到了一個問題。參考地址代碼地址為:http://hi.baidu.com/armlinuxqt/item/2c1f9e049f9106c775cd3c66寫一個核心模組,這個核心模組在初始化時分配8個頁面大小的記憶體塊,然後列印出分配到的記憶體頁面的邏輯地址,進而得到頁框號pfn,最後使用page_address得到實體記憶體地址。
輸出結果為:alloc_pages Successfully!pages=0xc1192700size of 'pages'=4pages=0xc1192700mem_map = 0xc1000000size of 'mem_map'=4mem_map = 0xc1000000pages - mem_map = 0x192700pages - mem_map = 0xc938(pages - mem_map) << 12 = 0x92700000(pages - mem_map) << 12 = 0xc938000page_address(pages) = 0xcc938000
我的問題是,究竟應該怎樣才能得到記憶體頁面的確切地址。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.