{"id":607,"date":"2019-09-29T13:58:06","date_gmt":"2019-09-29T05:58:06","guid":{"rendered":"http:\/\/iqotom.com\/?p=607"},"modified":"2019-09-29T13:58:10","modified_gmt":"2019-09-29T05:58:10","slug":"c%e8%af%ad%e8%a8%80%e8%8e%b7%e5%8f%96cpu%e6%a0%b8%e5%bf%83%e6%b8%a9%e5%ba%a6","status":"publish","type":"post","link":"http:\/\/iqotom.com\/?p=607","title":{"rendered":"C\u8bed\u8a00\u83b7\u53d6CPU\u6838\u5fc3\u6e29\u5ea6"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u83b7\u53d6Intel CPU \u4fe1\u606f\u548c\u6e29\u5ea6<\/h3>\n\n\n\n<p> CPU\u4fe1\u606f\u53ef\u4ee5\u901a\u8fc7cpuid\u6307\u4ee4\u83b7\u53d6\uff0c\u5728\u7528\u6237\u7a7a\u95f4\u53ef\u4ee5\u901a\u8fc7\u5185\u5d4c\u6c47\u7f16\u4ee3\u7801\u5b9e\u73b0\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct cpuid_res {\n\tuint32_t eax;\n\tuint32_t ebx;\n\tuint32_t ecx;\n\tuint32_t edx;\n};\nstatic inline struct cpuid_res cpuid(int op)\n{\n\tstruct cpuid_res result;\n\tasm volatile(\n\t\t\"mov %%ebx, %%edi;\"\n\t\t\"cpuid;\"\n\t\t\"mov %%ebx, %%esi;\"\n\t\t\"mov %%edi, %%ebx;\"\n\t\t: \"=a\" (result.eax),\n\t\t  \"=S\" (result.ebx),\n\t\t  \"=c\" (result.ecx),\n\t\t  \"=d\" (result.edx)\n\t\t: \"0\" (op)\n\t\t: \"edi\");\n\treturn result;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u83b7\u53d6CPU\u5382\u5bb6\u540d\u79f0<\/h3>\n\n\n\n<p> \u4ee5eax=0 \u6267\u884c cpuid\uff0ceax\u4e3a0\u8868\u793a\u8bfb\u53d6vendor id\uff0c\u4e00\u517112\u5b57\u8282\uff0c\u4f9d\u6b21\u5728ebx\u3001edx\u3001ecx<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>result = cpuid(0);\nvendor_name[0] = (result.ebx >> 0) &amp; 0xff;\nvendor_name[1] = (result.ebx >> 8) &amp; 0xff;\n...\nvendor_name[11] = (result.ecx >> 24) &amp; 0xff;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u83b7\u53d6CPU\u578b\u53f7<\/h3>\n\n\n\n<p> \u7528cpuid\u6307\u4ee4\uff0ceax\u4f20\u5165\u5206\u522b0x80000002\/0x80000003\/0x80000004\uff0c\u8bfb\u53d6cpu\u578b\u53f7\uff0c\u6bcf\u4e2a4\u4e2a\u5bc4\u5b58\u5668\uff0c\u6bcf\u4e2a\u5bc4\u5b58\u56684\u5b57\u8282\uff0c\u4e00\u517148\u5b57\u8282 <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct cpuid_res res;\nchar processor_name[49];\nunsigned int *cpu_type = (unsigned int *)processor_name;\nfor (int i = 0; i &lt; 3; i++) {\n\t\tres = cpuid(0x80000002 + i);\n\t\tcpu_type[i * 4 + 0] = res.eax;\n\t\tcpu_type[i * 4 + 1] = res.ebx;\n\t\tcpu_type[i * 4 + 2] = res.ecx;\n\t\tcpu_type[i * 4 + 3] = res.edx;\n\t}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u83b7\u53d6CPU\u6e29\u5ea6<\/h3>\n\n\n\n<p> Intel\u548cAMD\u7684CPU\u4e2d\u90fd\u6709\u6e29\u5ea6\u4f20\u611f\u5668\uff08DTS\uff09\uff0c\u6bcf\u4e2a\u6838\u5fc3\u90fd\u6709\u4e00\u4e2a\uff0c\u6e29\u5ea6\u5c31\u662f\u7531\u6b64\u83b7\u53d6\u6765\u7684\u3002Intel\u5bf9CPU\u6e29\u5ea6\u7684\u5904\u7406\uff0c\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u6700\u9ad8\u6e29\u5ea6Tjunction\uff0c\u4eceMSR\u4e2d\u8bfb\u53d6\u7684\u6570\u636e\u4e3a\u4e0e\u6700\u9ad8\u6e29\u5ea6\u7684\u6e29\u5deeDelta\uff0c\u5e76\u975e\u5b9e\u9645\u6e29\u5ea6\uff0c\u5b9e\u9645\u6e29\u5ea6\u4e3aTjunction-Delta\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u68c0\u67e5CPU\u662f\u5426\u652f\u6301DTS<\/h3>\n\n\n\n<ol><li> \u5148\u4ee5eax=0 \u6267\u884c cpuid \u68c0\u6d4b eax \u652f\u6301\u7684\u6700\u5927\u547d\u4ee4\u6570\uff0c\u5982\u679c\u5c0f\u4e8e6\u5c31\u80af\u5b9a\u4e0d\u652f\u6301DTS <br><code>int level = cpuid(0).eax;<\/code><\/li><li> \u4ee5eax=6 \u6267\u884c cpuid, \u7136\u540e\u6d4b\u8bd5 eax \u7b2c\u4e00\u4f4d\u662f\u5426\u4e3a1\uff0c\u5982\u679c\u4e3a1\u8868\u793aCPU\u652f\u6301DTS <br><code>int val = cpuid(6).eax;<\/code><br><code>char dts = (val>>0)&0x01;<\/code><\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u8bfb\u53d6DTS<\/h3>\n\n\n\n<p> \u8981\u83b7\u53d6cpu\u7684\u6e29\u5ea6\u53ef\u4ee5\u901a\u8fc7\u6c47\u7f16\u6307\u4ee4\u6765\u8bfb\u53d6\u3002\u4f46linux\u73af\u5883\u4e0b\uff0cmsr\u6307\u4ee4\u5fc5\u987b\u8981\u5728\u5185\u6838\u5c42\u624d\u80fd\u8c03\u7528\uff0c\u8fd9\u91cc\u6211\u5728\u9a71\u52a8\u4e2d\u4e2d\u5b9e\u73b0ioctl\u63a5\u53e3\uff0c\u7136\u540e\u8fd4\u56de\u6570\u636e\u7ed9\u7528\u6237\u5c42 <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>uint32_t lo, hi;\nasm volatile(\n\t\"rdmsr\"\n\t: \"=a\" (lo), \"=d\" (hi)\n\t: \"c\" (op)\n\t);<\/code><\/pre>\n\n\n\n<p> <strong>Tjunction<\/strong>\uff1a\u5f53\u6838\u5fc3\u6e29\u5ea6\u8fbe\u5230\u4e86\u9600\u503c\uff0c\u4f1a\u901a\u8fc7\u964d\u9891\u3001\u964d\u538b\u3001\u98ce\u6247\u8c03\u8282\u7b49\u5f62\u5f0f\u8c03\u8282\u6e29\u5ea6  <br>\u4ee5 ecx=0x1A2 \u6267\u884c rdmsr \u6307\u4ee4\uff0c\u901a\u8fc70x1A2\u6765\u8bfb\u53d6MSR\u7684[16-22]\u4f4d\u5f97\u5230Tjunction <br><code>u64 __val = __rdmsr(0x1A2);\/\/<\/code><\/p>\n\n\n\n<p><strong>Delta<\/strong>\uff1a\u6211\u4eec\u4eceMSR\u8bfb\u5230\u7684\u6e29\u5ea6\u662f\u8ddd\u79bbTjunction\u7684\u6e29\u5dee\uff0c\u800c\u4e0d\u662f\u5b9e\u9645\u6e29\u5ea6 <br> \u4ee5 ecx=0x19C \u6267\u884c rdmsr \u6307\u4ee4\uff0c\u901a\u8fc70x19C\u6765\u8bfb\u53d6MSR\u7684[16-22]\u4f4d\u5f97\u5230Delta <br><code>u64 __val2 = __rdmsr(0x19C);<\/code><\/p>\n\n\n\n<p>\u5f53\u524dcpu\u6e29\u5ea6 = Tjunction &#8211; Delta<br>\u6838\u5fc3\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>u64 __val = __rdmsr(0x1A2);\nprintk(\"__val is %016llx\\n\",__val);\nu64 __val2 = __rdmsr(0x19C);\nprintk(\"__val2 is %016llx\\n\",__val2);\n\nu8 tjunction,delta,coretemp;\ntjunction = (__val>>16)&0x7f;\ndelta = (__val2>>16)&0x7f;\ncoretemp = tjunction - delta;\nprintk(\"tjunction is %.1d\u00b0C ;delta is %.1d\u00b0C;core temp is %.1d\u00b0C\\n\", tjunction, delta, coretemp);\n\nchar buf_temp[30] = \"CPU\u6e29\u5ea6:\";\nchar core_temp[4];\nsprintf(core_temp, \"%d\", coretemp);\nstrcat(buf_temp,core_temp);\ncopy_to_user((char __user *)arg, buf_temp, strlen(buf_temp));<\/code><\/pre>\n\n\n\n<p>\u6d4b\u8bd5\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"105\" src=\"http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/cputemp-1024x105.png\" alt=\"\" class=\"wp-image-608\" srcset=\"http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/cputemp-1024x105.png 1024w, http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/cputemp-300x31.png 300w, http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/cputemp-768x79.png 768w, http:\/\/iqotom.com\/wp-content\/uploads\/2019\/09\/cputemp.png 1039w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>cpu \u6e29\u5ea6<\/figcaption><\/figure><\/div>\n\n\n\n<p> \u901a\u8fc7\u5bf9\u6bd4sensors\uff0c\u8fd9\u91cc\u83b7\u53d6\u7684\u662fCPU Core 1\u7684\u6e29\u5ea6\uff0cCore 0\u7684\u6e29\u5ea6\u53ef\u4ee5\u901a\u8fc7\u591a\u7ebf\u7a0b\u7ed1\u5b9a\u6838\u5fc3\u8fd0\u884c\u6765\u83b7\u5f97\u5230<\/p>\n\n\n\n<p>\n\n\u4ee3\u7801\u5730\u5740\uff1a<a href=\"https:\/\/links.jianshu.com\/go?to=https%3A%2F%2Fgithub.com%2Fhuchanghui123%2Fmy_cdev%2Fblob%2Fmaster%2Fmy_dev.c\">https:\/\/github.com\/huchanghui123\/my_cdev\/blob\/master\/my_dev.c<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u83b7\u53d6Intel CPU \u4fe1\u606f\u548c\u6e29\u5ea6 CPU\u4fe1\u606f\u53ef\u4ee5\u901a\u8fc7cpuid\u6307\u4ee4\u83b7\u53d6\uff0c\u5728\u7528\u6237\u7a7a\u95f4\u53ef\u4ee5\u901a\u8fc7\u5185\u5d4c\u6c47\u7f16\u4ee3\u7801\u5b9e\u73b0\uff0c\u2026 <span class=\"read-more\"><a href=\"http:\/\/iqotom.com\/?p=607\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":295,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,49],"tags":[27,50],"_links":{"self":[{"href":"http:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/posts\/607"}],"collection":[{"href":"http:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/iqotom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=607"}],"version-history":[{"count":1,"href":"http:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/posts\/607\/revisions"}],"predecessor-version":[{"id":609,"href":"http:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/posts\/607\/revisions\/609"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/iqotom.com\/index.php?rest_route=\/wp\/v2\/media\/295"}],"wp:attachment":[{"href":"http:\/\/iqotom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/iqotom.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=607"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/iqotom.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}