{"id":1966,"date":"2011-11-01T09:18:31","date_gmt":"2011-11-01T01:18:31","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=1966"},"modified":"2012-05-15T09:25:01","modified_gmt":"2012-05-15T01:25:01","slug":"1966","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/1966","title":{"rendered":"Linux\u5185\u5b58\u7ba1\u7406\u5b9e\u8df5-\u865a\u62df\u5730\u5740\u8f6c\u6362\u7269\u7406\u5730\u5740"},"content":{"rendered":"<p>Linux\u5185\u6838\u4e2d\u91c7\u7528\u4e86\u901a\u7528\u7684\u56db\u7ea7\u5206\u9875\u6a21\u578b\uff0c\u8fd9\u79cd\u6a21\u578b\u4e0d\u4ec5\u9002\u540832\u4f4d\u7cfb\u7edf\u4e5f\u9002\u540864\u4f4d\u7cfb\u7edf\u3002\u5206\u9875\u5355\u5143\u662fMMU(\u5185\u5b58\u7ba1\u7406\u5355\u5143)\u4e2d\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u5c06\u7ebf\u6027\u5730\u5740\u8f6c\u6362\u4e3a\u7269\u7406\u5730\u5740\u3002\u672c\u6587\u901a\u8fc7\u4e00\u4e2a\u5185\u6838\u6a21\u5757\u7a0b\u5e8f\u6a21\u62df\u5185\u6838\u4e2d\u865a\u62df\u5730\u5740\u8f6c\u6362\u4e3a\u7269\u7406\u5730\u5740\u7684\u8fc7\u7a0b\uff0c\u6709\u5173\u5206\u9875\u673a\u5236\u7684\u539f\u7406\u53ef\u4ee5\u53c2\u89c1<a href=\"http:\/\/edsionte.com\/techblog\/archives\/3435\">\u8fd9\u91cc<\/a>\u7684\u6587\u7ae0\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstatic void get_pgtable_macro(void)\r\n{\r\n\tprintk(&quot;PAGE_OFFSET = 0x%lx\\n&quot;, PAGE_OFFSET);\r\n\tprintk(&quot;PGDIR_SHIFT = %d\\n&quot;, PGDIR_SHIFT);\r\n\tprintk(&quot;PUD_SHIFT = %d\\n&quot;, PUD_SHIFT);\r\n\tprintk(&quot;PMD_SHIFT = %d\\n&quot;, PMD_SHIFT);\r\n\tprintk(&quot;PAGE_SHIFT = %d\\n&quot;, PAGE_SHIFT);\r\n\r\n\tprintk(&quot;PTRS_PER_PGD = %d\\n&quot;, PTRS_PER_PGD);\r\n\tprintk(&quot;PTRS_PER_PUD = %d\\n&quot;, PTRS_PER_PUD);\r\n\tprintk(&quot;PTRS_PER_PMD = %d\\n&quot;, PTRS_PER_PMD);\r\n\tprintk(&quot;PTRS_PER_PTE = %d\\n&quot;, PTRS_PER_PTE);\r\n\r\n\tprintk(&quot;PAGE_MASK = 0x%lx\\n&quot;, PAGE_MASK);\r\n}\r\n\r\nstatic unsigned long vaddr2paddr(unsigned long vaddr)\r\n{\r\n\tpgd_t *pgd;\r\n\tpud_t *pud;\r\n\tpmd_t *pmd;\r\n\tpte_t *pte;\r\n\tunsigned long paddr = 0;\r\n        unsigned long page_addr = 0;\r\n\tunsigned long page_offset = 0;\r\n\r\n\tpgd = pgd_offset(current-&gt;mm, vaddr);\r\n\tprintk(&quot;pgd_val = 0x%lx\\n&quot;, pgd_val(*pgd));\r\n\tprintk(&quot;pgd_index = %lu\\n&quot;, pgd_index(vaddr));\r\n\tif (pgd_none(*pgd)) {\r\n\t\tprintk(&quot;not mapped in pgd\\n&quot;);\r\n\t\treturn -1;\r\n\t}\r\n\r\n\tpud = pud_offset(pgd, vaddr);\r\n\tprintk(&quot;pud_val = 0x%lx\\n&quot;, pud_val(*pud));\r\n\tif (pud_none(*pud)) {\r\n\t\tprintk(&quot;not mapped in pud\\n&quot;);\r\n\t\treturn -1;\r\n\t}\r\n\r\n\tpmd = pmd_offset(pud, vaddr);\r\n\tprintk(&quot;pmd_val = 0x%lx\\n&quot;, pmd_val(*pmd));\r\n\tprintk(&quot;pmd_index = %lu\\n&quot;, pmd_index(vaddr));\r\n\tif (pmd_none(*pmd)) {\r\n\t\tprintk(&quot;not mapped in pmd\\n&quot;);\r\n\t\treturn -1;\r\n\t}\r\n\r\n\tpte = pte_offset_kernel(pmd, vaddr);\r\n\tprintk(&quot;pte_val = 0x%lx\\n&quot;, pte_val(*pte));\r\n\tprintk(&quot;pte_index = %lu\\n&quot;, pte_index(vaddr));\r\n\tif (pte_none(*pte)) {\r\n\t\tprintk(&quot;not mapped in pte\\n&quot;);\r\n\t\treturn -1;\r\n\t}\r\n\r\n\t\/\/\u9875\u6846\u7269\u7406\u5730\u5740\u673a\u5236 | \u504f\u79fb\u91cf\r\n\tpage_addr = pte_val(*pte) &amp; PAGE_MASK;\r\n\tpage_offset = vaddr &amp; ~PAGE_MASK;\r\n\tpaddr = page_addr | page_offset;\r\n\tprintk(&quot;page_addr = %lx, page_offset = %lx\\n&quot;, page_addr, page_offset);\r\n        printk(&quot;vaddr = %lx, paddr = %lx\\n&quot;, vaddr, paddr);\r\n\r\n\treturn paddr;\r\n}\r\n\r\nstatic int __init v2p_init(void)\r\n{\r\n\tunsigned long vaddr = 0;\r\n\r\n\tprintk(&quot;vaddr to paddr module is running..\\n&quot;);\r\n\tget_pgtable_macro();\r\n\tprintk(&quot;\\n&quot;);\r\n\r\n\tvaddr = (unsigned long)vmalloc(1000 * sizeof(char));\r\n\tif (vaddr == 0) {\r\n\t\tprintk(&quot;vmalloc failed..\\n&quot;);\r\n\t\treturn 0;\r\n\t}\r\n\tprintk(&quot;vmalloc_vaddr=0x%lx\\n&quot;, vaddr);\r\n\tvaddr2paddr(vaddr);\r\n\r\n\tprintk(&quot;\\n\\n&quot;);\r\n\tvaddr = __get_free_page(GFP_KERNEL);\r\n\tif (vaddr == 0) {\r\n\t\tprintk(&quot;__get_free_page failed..\\n&quot;);\r\n\t\treturn 0;\r\n\t}\r\n\tprintk(&quot;get_page_vaddr=0x%lx\\n&quot;, vaddr);\r\n\tvaddr2paddr(vaddr);\r\n\r\n\treturn 0;\r\n}\r\n\r\nstatic void __exit v2p_exit(void)\r\n{\r\n\tprintk(&quot;vaddr to paddr module is leaving..\\n&quot;);\r\n        vfree((void *)vaddr);\r\n        free_page(vaddr);\r\n}\r\n<\/pre>\n<p>\u6574\u4e2a\u7a0b\u5e8f\u7684\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<p>1.get_pgtable_macro()\u6253\u5370\u5f53\u524d\u7cfb\u7edf\u5206\u9875\u673a\u5236\u4e2d\u7684\u4e00\u4e9b\u5b8f\u3002<\/p>\n<p>2.\u901a\u8fc7vmalloc()\u5728\u5185\u6838\u7a7a\u95f4\u4e2d\u5206\u914d\u5185\u5b58\uff0c\u8c03\u7528vaddr2paddr()\u5c06\u865a\u62df\u5730\u5740\u8f6c\u5316\u6210\u7269\u7406\u5730\u5740\u3002<\/p>\n<p>3.\u901a\u8fc7__get_free_pages()\u5728\u5185\u6838\u7a7a\u95f4\u4e2d\u5206\u914d\u9875\u6846\uff0c\u8c03\u7528vaddr2paddr()\u5c06\u865a\u62df\u5730\u5740\u8f6c\u5316\u6210\u7269\u7406\u5730\u5740\u3002<\/p>\n<p>4.\u5206\u522b\u901a\u8fc7vfree()\u548cfree_page()\u91ca\u653e\u7533\u8bf7\u7684\u5185\u5b58\u7a7a\u95f4\u3002<\/p>\n<p>vaddr2paddr()\u7684\u6267\u884c\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n<p>1.\u901a\u8fc7pgd_offset\u8ba1\u7b97\u9875\u5168\u5c40\u76ee\u5f55\u9879\u7684\u7ebf\u6027\u5730\u5740pgd\uff0c\u4f20\u5165\u7684\u53c2\u6570\u4e3a\u5185\u5b58\u63cf\u8ff0\u7b26mm\u548c\u7ebf\u6027\u5730\u5740vaddr\u3002\u63a5\u7740\u6253\u5370pgd\u6240\u6307\u7684\u9875\u5168\u5c40\u76ee\u5f55\u9879\u3002<\/p>\n<p>2.\u901a\u8fc7pud_offset\u8ba1\u7b97\u9875\u4e0a\u7ea7\u76ee\u5f55\u9879\u7684\u7ebf\u6027\u5730\u5740pud\uff0c\u4f20\u5165\u7684\u53c2\u6570\u4e3a\u9875\u5168\u5c40\u76ee\u5f55\u9879\u7684\u7ebf\u6027\u5730\u5740pgd\u548c\u7ebf\u6027\u5730\u5740vaddr\u3002\u63a5\u7740\u6253\u5370pud\u6240\u6307\u7684\u9875\u4e0a\u7ea7\u76ee\u5f55\u9879\u3002<\/p>\n<p>3.\u901a\u8fc7pmd_offset\u8ba1\u7b97\u9875\u4e2d\u95f4\u76ee\u5f55\u9879\u7684\u7ebf\u6027\u5730\u5740pmd\uff0c\u4f20\u5165\u7684\u53c2\u6570\u4e3a\u9875\u4e0a\u7ea7\u76ee\u5f55\u9879\u7684\u7ebf\u6027\u5730\u5740pud\u548c\u7ebf\u6027\u5730\u5740vaddr\u3002\u63a5\u7740\u6253\u5370pmd\u6240\u6307\u7684\u9875\u4e2d\u95f4\u76ee\u5f55\u9879\u3002<\/p>\n<p>4.\u901a\u8fc7pte_offset_kernel\u8ba1\u7b97\u9875\u8868\u9879\u7684\u7ebf\u6027\u5730\u5740pte\uff0c\u4f20\u5165\u7684\u53c2\u6570\u4e3a\u9875\u4e2d\u95f4\u76ee\u5f55\u9879\u7684\u7ebf\u6027\u5730\u5740pmd\u548c\u7ebf\u6027\u5730\u5740vaddr\u3002\u63a5\u7740\u6253\u5370pte\u6240\u6307\u7684\u9875\u8868\u9879\u3002<\/p>\n<p>5.pte_val(*pte)\u5148\u53d6\u51fa\u9875\u8868\u9879\uff0c\u4e0ePAGE_MASK\u76f8\u4e0e\u7684\u7ed3\u679c\u662f\u5f97\u5230\u8981\u8bbf\u95ee\u9875\u7684\u7269\u7406\u5730\u5740\uff1bvaddr&amp;~PAGE_MASK\u7528\u6765\u5f97\u5230\u7ebf\u6027\u5730\u5740offset\u5b57\u6bb5\uff1b\u4e24\u8005\u6216\u8fd0\u7b97\u5f97\u5230\u6700\u7ec8\u7684\u7269\u7406\u5730\u5740\u3002<\/p>\n<p>6.\u6253\u5370\u7269\u7406\u5730\u5740\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux\u5185\u6838\u4e2d\u91c7\u7528\u4e86\u901a\u7528\u7684\u56db\u7ea7\u5206\u9875\u6a21\u578b\uff0c\u8fd9\u79cd\u6a21\u578b\u4e0d\u4ec5\u9002\u540832\u4f4d\u7cfb\u7edf\u4e5f\u9002\u540864\u4f4d\u7cfb\u7edf\u3002\u5206\u9875\u5355\u5143\u662fMMU(\u5185\u5b58\u7ba1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[266],"tags":[407,181,137,280,279],"class_list":["post-1966","post","type-post","status-publish","format-standard","hentry","category-266","tag-407","tag-181","tag-137","tag-280","tag-279"],"views":21052,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=1966"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1966\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=1966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=1966"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=1966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}