{"id":604,"date":"2019-06-12T19:09:33","date_gmt":"2019-06-12T11:09:33","guid":{"rendered":"https:\/\/www.sunjw.cn\/?p=604"},"modified":"2021-06-30T14:58:01","modified_gmt":"2021-06-30T06:58:01","slug":"linux-vir-to-phy","status":"publish","type":"post","link":"https:\/\/www.sunjw.cn\/index.php\/2019\/06\/12\/linux-vir-to-phy\/","title":{"rendered":"[Linux]Linux \u83b7\u53d6\u865a\u62df\u5730\u5740\u5bf9\u5e94\u7684\u7269\u7406\u5730\u5740(\u8f6c\u8f7d)"},"content":{"rendered":"<p>\u672c\u6587\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/zhoujianshi.github.io\/articles\/2017\/Linux%20%E8%8E%B7%E5%8F%96%E8%99%9A%E6%8B%9F%E5%9C%B0%E5%9D%80%E5%AF%B9%E5%BA%94%E7%9A%84%E7%89%A9%E7%90%86%E5%9C%B0%E5%9D%80\/index.html\">https:\/\/zhoujianshi.github.io\/articles\/2017\/Linux%20%E8%8E%B7%E5%8F%96%E8%99%9A%E6%8B%9F%E5%9C%B0%E5%9D%80%E5%AF%B9%E5%BA%94%E7%9A%84%E7%89%A9%E7%90%86%E5%9C%B0%E5%9D%80\/index.html<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u8fdb\u7a0b\u662f\u8fd0\u884c\u5728\u865a\u62df\u5730\u5740\u7a7a\u95f4\u4e0a\u7684\u3002\u6bd4\u5982\u572832\u4f4d\u673a\u5668\u4e0a\uff0c\u8fdb\u7a0b\u53ef\u4ee5\u8ba4\u4e3a\u81ea\u5df1\u67094GB\u7684\u5185\u5b58\u7a7a\u95f4\u53ef\u4ee5\u4f7f\u7528\u3002\u4f46\u662f\u771f\u5b9e\u88ab\u4f7f\u7528\u5230\u7684\u865a\u62df\u5730\u5740\u80af\u5b9a\u5f97\u6709\u771f\u5b9e\u7684\u5b58\u50a8\u4ecb\u8d28\u76f8\u5bf9\u5e94\u3002\u5982\u679c\u4e0d\u8003\u8651swap\u7684\u8bdd\uff0c\u6bcf\u4e00\u4e2a\u865a\u62df\u5730\u5740\u90fd\u5bf9\u5e94\u4e00\u4e2a\u7269\u7406\u5730\u5740\u3002\u73b0\u4ee3\u7684\u786c\u4ef6\u4e00\u822c\u662f\u4ee5\u5206\u9875\u7684\u65b9\u5f0f\u7ba1\u7406\u5185\u5b58\u7684\uff08x86\u7684\u5206\u6bb5\u4e0e\u5206\u9875\u76f8\u7ed3\u5408\u7684\u6240\u8c13\u6bb5\u9875\u5f0f\u4e5f\u53ea\u662f\u4e3a\u4e86\u517c\u5bb9\u7f62\u4e86\uff09\u3002\u4e00\u4e2a\u865a\u62df\u9875\u6620\u5c04\u5230\u4e00\u4e2a\u7269\u7406\u9875\u3002\u4ee5\u5e38\u89c1\u76844KB\u7684\u9875\u5927\u5c0f\u4e3a\u4f8b\uff0c\u5982\u679c\u4e00\u4e2a\u865a\u62df\u5730\u57400xfe0000\u5bf9\u5e94\u7269\u7406\u5730\u5740\u76840x40000\uff0c\u90a3\u4e48\u63a5\u4e0b\u6765\u7684\u865a\u62df\u5730\u57400xfe0000+i\u5c31\u5bf9\u5e94\u7269\u7406\u5730\u5740\u76840x40000+i\uff0ci=0,1,2,&#8230;,4095\u3002\u4f46\u662f\u865a\u62df\u5730\u5740\u76840xfe1000\u53ef\u80fd\u5bf9\u5e94\u7740\u4e00\u4e2a\u76f8\u53bb\u751a\u8fdc\u7684\u7269\u7406\u5730\u57400x1c0000\u5462\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u865a\u62df\u5730\u5740\u4e2d\u8fde\u7eed\u7684\u5730\u5740\uff0c\u5728\u7269\u7406\u4e0a\u53ef\u80fd\u662f\u788e\u7247\u4f3c\u7684\u5206\u6563\u5728\u5185\u5b58\u6761\u7684\u5404\u4e2a\u5730\u65b9\uff0c\u4f46\u662f\u5728\u4e00\u4e2a\u9875\u5185\uff0c\u5730\u5740\u662f\u8fde\u7eed\u5730\u4e00\u4e00\u5bf9\u5e94\u7684\u3002<\/p>\n<p>\u9274\u4e8e\u6b64\uff0c\u8981\u628a\u4e00\u4e2a\u865a\u62df\u5730\u5740\u8f6c\u6362\u6210\u7269\u7406\u5730\u5740\uff0c\u5176\u5b9e\u5c31\u662f\u8981\u77e5\u9053\u8be5\u865a\u62df\u5730\u5740\u6240\u5728\u7684\u865a\u62df\u9875\u5bf9\u5e94\u7684\u7269\u7406\u9875\u3002\u77e5\u9053\u4e86\u7269\u7406\u9875\uff0c\u518d\u52a0\u4e0a\u9875\u5185\u504f\u79fb\u91cf\u5373\u53ef\u3002\u4ee54KB\u7684\u9875\u5927\u5c0f\u4e3a\u4f8b\uff0c\u4e00\u4e2a32\u4f4d\u957f\u7684\u865a\u62df\u5730\u5740\uff0c\u5176\u9ad820\u4f4d\u5c31\u79f0\u4e3a\u865a\u62df\u9875\u53f7\uff0c\u4f4e12\u4f4d\u5c31\u662f\u9875\u5185\u504f\u79fb\u3002Linux\u4e3a\u6bcf\u4e00\u4e2a\u8fdb\u7a0b\u90fd\u7ef4\u62a4\u4e86\u4e00\u4e2a\u9875\u8868\uff0c\u653e\u5728\u5185\u5b58\u4e2d\u3002\u9875\u8868\u7684\u6bcf\u4e00\u9879\u5c31\u662f\u4e00\u4e2a\u865a\u62df\u9875\u53f7\u5bf9\u5e94\u7684\u7269\u7406\u9875\u53f7\u3002\u6240\u4ee5\u5982\u679c\u80fd\u591f\u8bbf\u95ee\u5230\u9875\u8868\uff0c\u90a3\u4e48\u5c31\u80fd\u591f\u628a\u865a\u62df\u5730\u5740\u8f6c\u6362\u6210\u7269\u7406\u5730\u5740\u3002\u7136\u800c\uff0c\u53ea\u6709\u5728\u5185\u6838\u6001\u624d\u6709\u6743\u9650\u8bbf\u95ee\u9875\u8868\uff0c\u7528\u6237\u6001\u662f\u65e0\u6743\u8bbf\u95ee\u7684\u3002\u53e6\u5916\uff0c\u4e0d\u540c\u7684\u786c\u4ef6\u7ed3\u6784\u4e0b\uff0c\u9875\u8868\u7684\u5b9a\u4f4d\u65b9\u5f0f\u662f\u4e0d\u540c\u7684\uff0c\u800c\u4e14\u53ef\u80fd\u5f88\u590d\u6742\uff0c\u6d89\u53ca\u591a\u4e2a\u5bc4\u5b58\u5668\u3002<\/p>\n<p>\u4e00\u5f00\u59cb\u6211\u6ca1\u6709\u627e\u5230\u80fd\u5728\u7528\u6237\u6001\u67e5\u8be2\u9875\u8868\u7684\u65b9\u5f0f\u3002\u5c31\u5f53\u6211\u5feb\u7edd\u671b\u7684\u65f6\u5019\uff0c\u4e07\u80fd\u7684GitHub\u53c8\u5e2e\u6211\u6211\u4e00\u628a\u3002\u6211\u5728\u67d0\u4e2a\u9879\u76ee\u4e2d\u53d1\u73b0\u4e86\u5176\u8bbf\u95ee\/proc\/&lt;pid&gt;\/pagemap\u8fd9\u4e2a\u865a\u62df\u6587\u4ef6\u3002\u540e\u6765\u6211\u53bb\u67e5\u4e86\u8fd9\u4e2a\u865a\u62df\u6587\u4ef6\uff0c\u5f97\u5230\u5982\u4e0b\u4fe1\u606f\u3002<\/p>\n<p>Documentation\/vm\/pagemap.txt<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full\" src=\"https:\/\/zhou-yuxin.github.io\/articles\/2017\/Linux%20%E8%8E%B7%E5%8F%96%E8%99%9A%E6%8B%9F%E5%9C%B0%E5%9D%80%E5%AF%B9%E5%BA%94%E7%9A%84%E7%89%A9%E7%90%86%E5%9C%B0%E5%9D%80\/1.jpg\" width=\"465\" height=\"534\" \/><\/p>\n<p>\u6bcf\u4e00\u4e2a\u9875\u5bf9\u5e94\u4e00\u4e2a64\u4f4d\uff0c\u4e5f\u5c31\u662f8\u5b57\u8282\u7684\u5b57\u6bb5\u3002\u6bd4\u5982\u865a\u62df\u5730\u57400xfe0020\uff0c\u5176\u9ad820\u4f4d\u4e3a0xfe0\uff0c\u4e5f\u5c31\u662f\u5176\u865a\u62df\u9875\u53f7\u4e3a0xfe0\u3002\u90a3\u4e48\u8be5\u865a\u62df\u9875\u7684\u4fe1\u606f\u5904\u4e8e\/proc\/self\/pagemap\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u504f\u79fb\u91cf\u4e3a0xfe0*8=32512\u7684\u5730\u65b9\u3002\u4ece\u6b64\u5904\u8bfb\u53d6\u4e00\u4e2a8\u5b57\u8282\u7684\u6570\u636e\uff0c\u5148\u68c0\u67e5\u6700\u9ad8\u4f4d&#8217;page present&#8217;\uff0c\u5982\u679c\u662f1\uff0c\u90a3\u4e48\u8bf4\u660e\u8be5\u9875\u5904\u4e8e\u7269\u7406\u5185\u5b58\u4e2d\uff0c\u90a3\u4e48\u8be58\u5b57\u8282\u7684\u7b2c0-54\u4f4d\u5c31\u662f\u7269\u7406\u9875\u53f7\u3002\u5047\u8bbe\u7269\u7406\u9875\u53f7\u662f0x40\uff0c\u90a3\u4e48\u5b9e\u9645\u7684\u7269\u7406\u5730\u5740\u5c31\u662f(0x40&lt;&lt;12)+0x20=0x40020\u3002<\/p>\n<p>\u4ecb\u7ecd\u5b8c\u539f\u7406\uff0c\u90a3\u4e48\u5c01\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\u5c31\u7b80\u5355\u591a\u4e86\u3002\u6211\u8fd9\u91cc\u5c31\u5c01\u88c5\u4e00\u4e2a\u6700\u5bb9\u6613\u7406\u89e3\u4f46\u662f\u6548\u7387\u6700\u4f4e\u7684\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/p>\n<pre>#include &lt;fcntl.h&gt;\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdint.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nsize_t virtual_to_physical(size_t addr)\r\n{\r\n    int fd = open(\"\/proc\/self\/pagemap\", O_RDONLY);\r\n    if(fd &lt; 0)\r\n    {\r\n        printf(\"open '\/proc\/self\/pagemap' failed!\\n\");\r\n        return 0;\r\n    }\r\n    size_t pagesize = getpagesize();\r\n    size_t offset = (addr \/ pagesize) * sizeof(uint64_t);\r\n    if(lseek(fd, offset, SEEK_SET) &lt; 0)\r\n    {\r\n        printf(\"lseek() failed!\\n\");\r\n        close(fd);\r\n        return 0;\r\n    }\r\n    uint64_t info;\r\n    if(read(fd, &amp;info, sizeof(uint64_t)) != sizeof(uint64_t))\r\n    {\r\n        printf(\"read() failed!\\n\");\r\n        close(fd);\r\n        return 0;\r\n    }\r\n    if((info &amp; (((uint64_t)1) &lt;&lt; 63)) == 0)\r\n    {\r\n        printf(\"page is not present!\\n\");\r\n        close(fd);\r\n        return 0;\r\n    }\r\n    size_t frame = info &amp; ((((uint64_t)1) &lt;&lt; 55) - 1);\r\n    size_t phy = frame * pagesize + addr % pagesize;\r\n    close(fd);\r\n    return phy;\r\n}\r\n<\/pre>\n<p>\u6700\u540e\u9700\u8981\u8bf4\u660e\u7684\u662f\uff0c\u53ea\u6709root\u6743\u9650\u7684\u8fdb\u7a0b\u80fd\u591f\u8bbf\u95ee\/proc\/&lt;pid&gt;\/pagemap\u8fd9\u4e2a\u6587\u4ef6\uff0c\u5426\u5219\u8bfb\u51fa\u6765\u7684\u90fd\u662f0\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/zhoujianshi.github.io\/articles\/2017\/Linu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/posts\/604"}],"collection":[{"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/comments?post=604"}],"version-history":[{"count":4,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/posts\/604\/revisions"}],"predecessor-version":[{"id":804,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/posts\/604\/revisions\/804"}],"wp:attachment":[{"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/media?parent=604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/categories?post=604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sunjw.cn\/index.php\/wp-json\/wp\/v2\/tags?post=604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}