{"id":230,"date":"2021-12-02T00:43:55","date_gmt":"2021-12-02T08:43:55","guid":{"rendered":"https:\/\/www.qiuqiuren.club\/?p=230"},"modified":"2024-08-02T21:04:03","modified_gmt":"2024-08-03T05:04:03","slug":"pve-%e7%bb%99qemu%e6%b7%bb%e5%8a%a0%e8%87%aa%e5%ae%9a%e4%b9%89patch","status":"publish","type":"post","link":"https:\/\/www.qiuqiuren.club\/?p=230","title":{"rendered":"\u4e0d\u80fd\u901a\u8fc7\u4fee\u6539qemu\u6765\u8bb0\u5f55cpuid\u7684\u539f\u56e0"},"content":{"rendered":"<h2>\u80cc\u666f<\/h2>\n<p>\u5047\u8bbe\u6709\u4e2a\u5f88\u96be\u5206\u6790\u7684\u7a0b\u5e8f\uff0c\u5b83\u7528\u4e86cpuid\u8fdb\u884c\u91cd\u8981\u7684\u529f\u80fd\uff08\u6bd4\u5982\u68c0\u6d4b\u865a\u62df\u673a\uff09\uff0c\u4f46\u662f\u6211\u4eec\u6ca1\u529e\u6cd5\u5206\u6790\u5b83\uff08\u865a\u62df\u5316\u3001\u53cd\u8c03\u8bd5\u7b49\uff09\u3002\u5728\u8fd9\u4e2a\u80cc\u666f\u4e0b\uff0c\u53ef\u4ee5\u628a\u5b83\u88c5\u5728\u865a\u62df\u673a\u91cc\u9762\uff0c\u7136\u540e\u5728Qemu\u5904\u7406cpuid\u7684\u903b\u8f91\u4e2d\u8fdb\u884clog.<\/p>\n<h2>\u7f16\u8bd1PVE-Qemu<\/h2>\n<p>\u770b\u770b\u8fd9\u4e2a\u811a\u672c\uff0c\u5927\u6982\u5c31\u61c2\u4e86\u3002checkout\u4e4b\u540e\u9700\u8981\u91cd\u65b0init\u4e00\u4e0bsubmodule\uff0c\u5426\u5219\u7f16\u8bd1\u65f6\u4f1a\u62a5xxxsubmodule\u4e0d\u5b58\u5728\u3002\u81f3\u4e8e\u7528\u54ea\u4e2a\u547d\u4ee4\u521d\u59cb\u5316submodule\u6211\u5fd8\u4e86\uff0c\u53cd\u6b63\u8bd5\u51e0\u4e2a\u547d\u4ee4\u5c31\u51fa\u6765\u4e86\u3002<\/p>\n<pre><code>    apt install -y devscripts build-essential pve-kernel-libc-dev\n    git clone --recursive https:\/\/git.proxmox.com\/git\/pve-qemu.git\n    cd pve-qemu\n    # checkout to 5.2.0-3\n    git checkout 970196f # 5.2.0-3\n    # \u91cd\u65b0init submodule\n    asdadada\n    # install all build dependencies of the pve-qemu package\n    echo Y | mk-build-deps --install debian\/control\n    # build\n    make\n\n    # \u5982\u679c\u4fee\u6539\u4ee3\u7801\u91cd\u65b0\u7f16\u8bd1\n    make clean\n    make<\/code><\/pre>\n<h2>\u4fee\u6539qemu\u6e90\u7801<\/h2>\n<p>\u77e5\u9053\u4e86\u600e\u4e48\u7f16\u8bd1\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u6539\u6e90\u7801\u3002qemu\u5904\u7406cpuid\u7684\u903b\u8f91\u5728qemu\/target\/i386\/cpu.c\u7684cpu_x86_cpuid()\u91cc\u9762\u3002\u770b\u4e0b\u9762\u4ee3\u7801\uff0c\u662f\u4e0d\u662f\u5f88\u5408\u903b\u8f91\u3002<\/p>\n<pre><code class=\"language-c\">switch(index) {\n    case 0:\n        *eax = env-&gt;cpuid_level;\n        *ebx = env-&gt;cpuid_vendor1;\n        *edx = env-&gt;cpuid_vendor2;\n        *ecx = env-&gt;cpuid_vendor3;\n        break;\n    case 1:\n        *eax = env-&gt;cpuid_version;\n        *ebx = (cpu-&gt;apic_id &lt;&lt; 24) |\n               8 &lt;&lt; 8; \/* CLFLUSH size in quad words, Linux wants it. *\/\n        *ecx = env-&gt;features[FEAT_1_ECX];\n        if ((*ecx &amp; CPUID_EXT_XSAVE) &amp;&amp; (env-&gt;cr[4] &amp; CR4_OSXSAVE_MASK)) {\n            *ecx |= CPUID_EXT_OSXSAVE;\n        }\n        *edx = env-&gt;features[FEAT_1_EDX];\n        if (cs-&gt;nr_cores * cs-&gt;nr_threads &gt; 1) {\n            *ebx |= (cs-&gt;nr_cores * cs-&gt;nr_threads) &lt;&lt; 16;\n            *edx |= CPUID_HT;\n        }\n        if (!cpu-&gt;enable_pmu) {\n            *ecx &amp;= ~CPUID_EXT_PDCM;\n        }\n        break;\n    case 2:<\/code><\/pre>\n<p>\u5982\u679c\u662f\u8981\u4fee\u6539cpuid\u7684\u8fd4\u56de\u7ed3\u679c\uff08\u4f8b\u5982\u4fee\u6539\u8fd4\u56de\u7684\u5382\u5546\u5b57\u7b26\u4e32\uff09\uff0c\u5728\u8fd9\u4e2a\u51fd\u6570\u91cc\u6539\u5c31ok\u4e86\u3002\u4f46\u662f\u5982\u679c\u8981\u8fdb\u884clog\u7684\u8bdd\uff0c\u8fd9\u91cc\u8fd8\u4e0d\u884c\u3002\u56e0\u4e3aqemu\u8003\u8651\u5230cpuid\u7684\u8fd4\u56de\u503c\u662f\u56fa\u5b9a\u7684\uff0c\u5b83\u6709\u4e2a\u7f13\u5b58\u673a\u5236\uff0c\u4e0d\u4f1a\u6bcf\u6b21\u90fd\u8c03\u8fd9\u4e2a\u51fd\u6570\u3002\u636e\u5206\u6790\uff0c\u5b83\u5728qemu\/target\/i386\/kvm\/kvm.c\u7684kvm_arch_init_vcpu()\u51fd\u6570\u91cc\u9762\u4f1a\u4e00\u6b21\u6027\u628acpuid\u8bfb\u5b8c\uff0c\u5b58\u5728\u81ea\u5df1\u7684\u6570\u636e\u7ed3\u6784\u91cc\u3002\u770b\u770bkvm_arch_init_vcpu\u7684\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u3002\uff08\u770b\u4e0d\u61c2\u7684\u8bdd\uff0c\u770b\u770b\u7d27\u968f\u5176\u540e\u7684cpuid\u6307\u4ee4\u89e3\u6790\uff09<\/p>\n<pre><code class=\"language-c\"> cpu_x86_cpuid(env, 0, 0, &amp;limit, &amp;unused, &amp;unused, &amp;unused);\n\n    for (i = 0; i &lt;= limit; i++) {\n        if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {\n            fprintf(stderr, &quot;unsupported level value: 0x%x\\n&quot;, limit);\n            abort();\n        }\n        c = &amp;cpuid_data.entries[cpuid_i++];\n\n        switch (i) {\n        case 2: {\n            \/* Keep reading function 2 till all the input is received *\/\n            int times;\n\n            c-&gt;function = i;\n            c-&gt;flags = KVM_CPUID_FLAG_STATEFUL_FUNC |\n                       KVM_CPUID_FLAG_STATE_READ_NEXT;\n            cpu_x86_cpuid(env, i, 0, &amp;c-&gt;eax, &amp;c-&gt;ebx, &amp;c-&gt;ecx, &amp;c-&gt;edx);\n            times = c-&gt;eax &amp; 0xff;\n\n            for (j = 1; j &lt; times; ++j) {\n                if (cpuid_i == KVM_MAX_CPUID_ENTRIES) {\n                    fprintf(stderr, &quot;cpuid_data is full, no space for &quot;\n                            &quot;cpuid(eax:2):eax &amp; 0xf = 0x%x\\n&quot;, times);\n                    abort();\n                }\n                c = &amp;cpuid_data.entries[cpuid_i++];\n                c-&gt;function = i;\n                c-&gt;flags = KVM_CPUID_FLAG_STATEFUL_FUNC;\n                cpu_x86_cpuid(env, i, 0, &amp;c-&gt;eax, &amp;c-&gt;ebx, &amp;c-&gt;ecx, &amp;c-&gt;edx);\n            }\n            break;<\/code><\/pre>\n<p>\u9996\u5148\u7528eax=0\uff0c\u8bfb\u51fa\u6700\u591a\u652f\u6301\u7684cpuid\u6570\u76ee\u3002cpuid\u6307\u4ee4\u7684\u8bbe\u8ba1\u5c31\u662f\u5982\u6b64\uff1a<\/p>\n<blockquote>\n<p>INPUT EAX = 0: Returns CPUID&#8217;s Highest Value for Basic Processor Information and the Vendor Identification String<br \/>\nWhen CPUID executes with EAX set to 0, the processor returns the highest value the CPUID recognizes for returning basic processor information. The value is returned in the EAX register (see second table) and is processor specific.<\/p>\n<\/blockquote>\n<p>\u7136\u540e\u5bf9\u4e8eEAX=2\uff0c\u5faa\u73af\u8bfb\u5b8cEAX=2\u91cc\u7684\u6570\u636e\uff0c\u56e0\u4e3acpuid\u7684\u8bbe\u8ba1\u5982\u6b64<\/p>\n<blockquote>\n<p>INPUT EAX = 2: Cache and TLB Information Returned in EAX, EBX, ECX, EDX<br \/>\nWhen CPUID executes with EAX set to 2, the processor returns information about the processor&#8217;s internal caches and TLBs in the EAX, EBX, ECX, and EDX registers.<br \/>\nThe encoding is as follows: &#8211; The least-significant byte in register EAX (register AL) indicates the number of times the CPUID instruction must be executed with an input value of 2 to get a complete description of the processor&#8217;s caches and TLBs. The first member of the family of Pentium 4 processors will return a 1.<\/p>\n<\/blockquote>\n<p>\u8fd9\u5c31\u8bf4\u660eqemu\u7f13\u5b58\u4e86cpuid\u7684\u8fd4\u56de\u503c\uff0c\u5728cpu.c\u6587\u4ef6\u91cc\u505alog\uff0c\u8bb0\u5f55\u6bcf\u6b21cpu\u7684\u8c03\u7528\u7ed3\u679c\u662f\u884c\u4e0d\u901a\u7684\u3002<\/p>\n<p>\u8fd8\u662f\u770b\u8fd9\u4e2ainit_vcpu\u51fd\u6570\uff0c\u770b\u5230\u5b83\u8fd9\u4e00\u884c<\/p>\n<p><code>r = kvm_vcpu_ioctl(cs, KVM_SET_CPUID2, &amp;cpuid_data);<\/code><\/p>\n<p>cpuid_data\u5c31\u662f\u7f13\u5b58\u7684cpuid\u6570\u636e\uff0c\u770b\u6837\u5b50\u8fd9\u4e2a\u6570\u636e\u88ab\u9001\u5230\u5185\u6838\u4e86\uff0c\u770b\u540d\u5b57\u662f\u9001\u7ed9kvm\u6a21\u5757\u4e86\u3002\u8fd9\u6837\u7684\u8bdd\uff0c\u6539qemu\u5c31\u6ca1\u620f\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u5047\u8bbe\u6709\u4e2a\u5f88\u96be\u5206\u6790\u7684\u7a0b\u5e8f\uff0c\u5b83\u7528\u4e86cpuid\u8fdb\u884c\u91cd\u8981\u7684\u529f\u80fd\uff08\u6bd4\u5982\u68c0\u6d4b\u865a\u62df\u673a\uff09\uff0c\u4f46\u662f\u6211\u4eec\u6ca1\u529e\u6cd5\u5206\u6790\u5b83\uff08\u865a\u62df\u5316\u3001<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-230","post","type-post","status-publish","format-standard","hentry","category-3","category-4"],"_links":{"self":[{"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=\/wp\/v2\/posts\/230"}],"collection":[{"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=230"}],"version-history":[{"count":4,"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=\/wp\/v2\/posts\/230\/revisions"}],"predecessor-version":[{"id":235,"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=\/wp\/v2\/posts\/230\/revisions\/235"}],"wp:attachment":[{"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qiuqiuren.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}