[笔记] 分解多核CPU的x2APIC_ID(或APIC_ID)中的各个子字段

一、分解x2APIC_ID

摘自《Intel® 64 Architecture Processor Topology Enumeration》中的“Sub ID Extraction Parameters for x2APIC ID”——
SMT_ID: CPUID.(EAX=11, ECX=0):EAX[4:0] provides the width parameter to derive a SMT select mask to extract the SMT_IDs of logical processors within the same processor core. The sub leaf index (ECX=0) is architecturally defined and associated with the “SMT” level type (CPUID.(EAX=11, ECX=0):ECX[15:8] == 1)
 SMT_Mask_Width = CPUID.(EAX=11, ECX=0):EAX[4:0] if CPUID.(EAX=11, ECX=0):ECX[15:8] is 1
 SMT_Select_Mask = ~((-1) << SMT_Mask_Width )
 SMT_ID = x2APIC_ID & SMT_Select_Mask
Core_ID: The level type associated with the sub leaf index (ECX=1) may vary across processors with different hardware multithreading capabilities. If CPUID.(EAX=11, ECX=1):ECX[15:8] is 2, it is associated with “processor core” level type. Then, CPUID.(EAX=11,ECX=1):EAX[4:0] provides the width parameter to derive a select mask of all logical processors within the same physical package. The “processor core” includes “SMT” in this case, and enumerating different cores in the package can be done by zeroing out the SMT portion of the inclusive mask derived from this.
 CorePlus_Mask_Width = CPUID.(EAX=11,ECX=1):EAX[4:0] if CPUID.(EAX=11, ECX=1):ECX[15:8] is 2
 CoreOnly_Select_Mask = (~((-1) << CorePlus_Mask_Width ) ) ^ SMT_Select_Mask.
 Core_ID = (x2APIC_ID & CoreOnly_Select_Mask) >> SMT_Mask_Width
Pkg_ID: Within a coherent domain of the three-level topology, the upper bits of the APIC_ID (except the lower “CorePlus_Mask_Width” bits) can enumerate different physical packages in the system. In a clustered installation, software may need to consult vendor specific documentation to distinguish the topology of how many physical packages are organized within a given node.
 Pkg_Select_Mask = (-1) << CorePlus_Mask_Width
 Pkg_ID = (x2APIC_ID & Pkg_Select_Mask) >> CorePlus_Mask_Width

范例——
CPU #4:
      CPUID 0000000B                                    00000001-00000002-00000100-00000003
      CPUID 0000000B                                    00000004-00000004-00000201-00000003

简单解释一下“CPUID 0000000B”——
bits nums lv_num lv_type APICID
A_04_00 B_15_00 C_07_00 C_15_08 D_31_00
1 2 0 1 00000003
4 4 1 2 00000003

首先可以得到——
x2APIC_ID = 00000003
SMT_Mask_Width = 1
CorePlus_Mask_Width = 4

然后可以生成各个部分的掩码——
SMT_Select_Mask = ~((-1) << SMT_Mask_Width ) = 00000001
CoreOnly_Select_Mask = (~((-1) << CorePlus_Mask_Width ) ) ^ SMT_Select_Mask = 0000000E
Pkg_Select_Mask = (-1) << CorePlus_Mask_Width = FFFFFFF0

再得到各部分的子ID——
SMT_ID = x2APIC_ID & SMT_Select_Mask = 00000001
Core_ID = (x2APIC_ID & CoreOnly_Select_Mask) >> SMT_Mask_Width = 1
Pkg_ID = (x2APIC_ID & Pkg_Select_Mask) >> CorePlus_Mask_Width = 0


可以这样理解——
SMT_ID:x2APIC_ID的位域 [SMT_Mask_Width-1:0],位宽为SMT_Mask_Width。
Core_ID:x2APIC_ID的位域 [CorePlus_Mask_Width-1: SMT_Mask_Width],位宽为CorePlus_Mask_Width-SMT_Mask_Width。
Pkg_ID:x2APIC_ID的位域 [31:CorePlus_Mask_Width],位宽为32-CorePlus_Mask_Width。


注意——
**Software must not use EBX[15:0] to enumerate processor topology
of the system. This value in this field (EBX[15:0]) is only intended for
display/diagnostic purposes. The actual number of logical processors
available to BIOS/OS/Applications may be different from the value of
EBX[15:0], depending on software and platform hardware configurations.
软件不要使用EBX[15:0]来枚举处理器的拓补结构。因为它是该架构完整设计时的值,生产时可能会被屏蔽核心或线程。

二、分解APIC_ID


摘自《Intel® 64 Architecture Processor Topology Enumeration》中的“Sub ID Extraction Parameters for Initial APIC ID”——
SMT_ID: The equivalent “SMT_Mask_Width” can be derived from dividing maximum number of addressable initial APIC IDs by maximum number of addressable Core IDs
 SMT_Mask_Width = Log2( RoundToNearestPof2(CPUID.1:EBX[23:16]) / ((CPUID.(EAX=4, ECX=0):EAX[31:26] ) + 1)), where Log2 is the logarithmic based on 2 and RoundToNearestPof2() operation is to round the input integer to the nearest power-of-two integer that is not less than the input value.
 SMT_Select_Mask = ~((-1) << SMT_Mask_Width )
 SMT_ID = INITIAL_APIC_ID & SMT_Select_Mask
Core_ID: The value of (1 + (CPUID.(EAX=04H, ECX=0):EAX[31:26] )) can also be use to derive an equivalent “CoreOnly_Mask_Width”.
 CoreOnly_Mask_Width = Log2(1 + (CPUID.(EAX=4, ECX=0):EAX[31:26] ))
 CoreOnly_Select_Mask = (~((-1) << (CoreOnly_Mask_Width + SMT_Mask_Width) ) ) ^ SMT_Select_Mask.
 Core_ID = (INITIAL_APIC_ID & CoreOnly_Select_Mask) >> SMT_Mask_Width
Pkg_ID: Pkg_Select_Mask can be derived as follows;
 CorePlus_Mask_Width = CoreOnly_Mask_Width + SMT_Mask_Width
 Pkg_Select_Mask = ((-1) << CorePlus_Mask_Width)
 Pkg_ID = (INITIAL_APIC_ID & Pkg_Select_Mask) >> CorePlus_Mask_Width

掩码与子ID的计算相同,就是位宽计算有点差异。可理解为——
LogicalProcessorCount = CPUID.1:EBX[23:16]
CorePerPackage = ((CPUID.(EAX=4, ECX=0):EAX[31:26] ) + 1)
SMT_Mask_Width = Log2( RoundToNearestPof2(LogicalProcessorCount) / CorePerPackage ) // 能这样计算吗—— log2(LogicalProcessorCount / CorePerPackage)
CorePlus_Mask_Width = Log2(CorePerPackage) + SMT_Mask_Width // 能这样计算吗—— ceil(log2(LogicalProcessorCount))

范例——
CPU #4:
      CPUID 00000001                                    000206A7-03100800-15BAE3BF-BFEBFBFF
      CPUID 00000004                                    1C004121-01C0003F-0000003F-00000000

计算——
LogicalProcessorCount = 0x10
CorePerPackage = 7+1 = 8
SMT_Mask_Width = Log2( RoundToNearestPof2(0x10) / 8 ) = Log2(2) = 1
CorePlus_Mask_Width = Log2(8) + 1 = 3+1 = 4

A、附录

Intel core i3 2310m的CPUID——

    CPUID Registers (CPU #1):
      CPUID 00000000                                    0000000D-756E6547-6C65746E-49656E69
      CPUID 00000001                                    000206A7-00100800-15BAE3BF-BFEBFBFF
      CPUID 00000002                                    76035A01-00F0B2FF-00000000-00CA0000
      CPUID 00000003                                    00000000-00000000-00000000-00000000
      CPUID 00000004                                    1C004121-01C0003F-0000003F-00000000
      CPUID 00000004                                    1C004122-01C0003F-0000003F-00000000
      CPUID 00000004                                    1C004143-01C0003F-000001FF-00000000
      CPUID 00000004                                    1C03C163-02C0003F-00000FFF-00000006
      CPUID 00000005                                    00000040-00000040-00000003-00021120
      CPUID 00000006                                    00000075-00000002-00000009-00000000
      CPUID 00000007                                    00000000-00000000-00000000-00000000
      CPUID 00000008                                    00000000-00000000-00000000-00000000
      CPUID 00000009                                    00000000-00000000-00000000-00000000
      CPUID 0000000A                                    07300403-00000000-00000000-00000603
      CPUID 0000000B                                    00000001-00000002-00000100-00000000
      CPUID 0000000B                                    00000004-00000004-00000201-00000000
      CPUID 0000000C                                    00000000-00000000-00000000-00000000
      CPUID 0000000D                                    00000007-00000240-00000340-00000000
      CPUID 0000000D                                    00000100-00000240-00000000-00000000
      CPUID 80000000                                    80000008-00000000-00000000-00000000
      CPUID 80000001                                    00000000-00000000-00000001-28100000
      CPUID 80000002                                    20202020-49202020-6C65746E-20295228
      CPUID 80000003                                    65726F43-294D5428-2D336920-30313332
      CPUID 80000004                                    5043204D-20402055-30312E32-007A4847
      CPUID 80000005                                    00000000-00000000-00000000-00000000
      CPUID 80000006                                    00000000-00000000-01006040-00000000
      CPUID 80000007                                    00000000-00000000-00000000-00000100
      CPUID 80000008                                    00003024-00000000-00000000-00000000

    CPUID Registers (CPU #2):
      CPUID 00000000                                    0000000D-756E6547-6C65746E-49656E69
      CPUID 00000001                                    000206A7-02100800-15BAE3BF-BFEBFBFF
      CPUID 00000002                                    76035A01-00F0B2FF-00000000-00CA0000
      CPUID 00000003                                    00000000-00000000-00000000-00000000
      CPUID 00000004                                    1C004121-01C0003F-0000003F-00000000
      CPUID 00000004                                    1C004122-01C0003F-0000003F-00000000
      CPUID 00000004                                    1C004143-01C0003F-000001FF-00000000
      CPUID 00000004                                    1C03C163-02C0003F-00000FFF-00000006
      CPUID 00000005                                    00000040-00000040-00000003-00021120
      CPUID 00000006                                    00000075-00000002-00000009-00000000
      CPUID 00000007                                    00000000-00000000-00000000-00000000
      CPUID 00000008                                    00000000-00000000-00000000-00000000
      CPUID 00000009                                    00000000-00000000-00000000-00000000
      CPUID 0000000A                                    07300403-00000000-00000000-00000603
      CPUID 0000000B                                    00000001-00000002-00000100-00000002
      CPUID 0000000B                                    00000004-00000004-00000201-00000002
      CPUID 0000000C                                    00000000-00000000-00000000-00000000
      CPUID 0000000D                                    00000007-00000240-00000340-00000000
      CPUID 0000000D                                    00000100-00000240-00000000-00000000
      CPUID 80000000                                    80000008-00000000-00000000-00000000
      CPUID 80000001                                    00000000-00000000-00000001-28100000
      CPUID 80000002                                    20202020-49202020-6C65746E-20295228
      CPUID 80000003                                    65726F43-294D5428-2D336920-30313332
      CPUID 80000004                                    5043204D-20402055-30312E32-007A4847
      CPUID 80000005                                    00000000-00000000-00000000-00000000
      CPUID 80000006                                    00000000-00000000-01006040-00000000
      CPUID 80000007                                    00000000-00000000-00000000-00000100
      CPUID 80000008                                    00003024-00000000-00000000-00000000

    CPUID Registers (CPU #3 Virtual):
      CPUID 00000000                                    0000000D-756E6547-6C65746E-49656E69
      CPUID 00000001                                    000206A7-01100800-15BAE3BF-BFEBFBFF
      CPUID 00000002                                    76035A01-00F0B2FF-00000000-00CA0000
      CPUID 00000003                                    00000000-00000000-00000000-00000000
      CPUID 00000004                                    1C004121-01C0003F-0000003F-00000000
      CPUID 00000004                                    1C004122-01C0003F-0000003F-00000000
      CPUID 00000004                                    1C004143-01C0003F-000001FF-00000000
      CPUID 00000004                                    1C03C163-02C0003F-00000FFF-00000006
      CPUID 00000005                                    00000040-00000040-00000003-00021120
      CPUID 00000006                                    00000075-00000002-00000009-00000000
      CPUID 00000007                                    00000000-00000000-00000000-00000000
      CPUID 00000008                                    00000000-00000000-00000000-00000000
      CPUID 00000009                                    00000000-00000000-00000000-00000000
      CPUID 0000000A                                    07300403-00000000-00000000-00000603
      CPUID 0000000B                                    00000001-00000002-00000100-00000001
      CPUID 0000000B                                    00000004-00000004-00000201-00000001
      CPUID 0000000C                                    00000000-00000000-00000000-00000000
      CPUID 0000000D                                    00000007-00000240-00000340-00000000
      CPUID 0000000D                                    00000100-00000240-00000000-00000000
      CPUID 80000000                                    80000008-00000000-00000000-00000000
      CPUID 80000001                                    00000000-00000000-00000001-28100000
      CPUID 80000002                                    20202020-49202020-6C65746E-20295228
      CPUID 80000003                                    65726F43-294D5428-2D336920-30313332
      CPUID 80000004                                    5043204D-20402055-30312E32-007A4847
      CPUID 80000005                                    00000000-00000000-00000000-00000000
      CPUID 80000006                                    00000000-00000000-01006040-00000000
      CPUID 80000007                                    00000000-00000000-00000000-00000100
      CPUID 80000008                                    00003024-00000000-00000000-00000000

    CPUID Registers (CPU #4 Virtual):
      CPUID 00000000                                    0000000D-756E6547-6C65746E-49656E69
      CPUID 00000001                                    000206A7-03100800-15BAE3BF-BFEBFBFF
      CPUID 00000002                                    76035A01-00F0B2FF-00000000-00CA0000
      CPUID 00000003                                    00000000-00000000-00000000-00000000
      CPUID 00000004                                    1C004121-01C0003F-0000003F-00000000
      CPUID 00000004                                    1C004122-01C0003F-0000003F-00000000
      CPUID 00000004                                    1C004143-01C0003F-000001FF-00000000
      CPUID 00000004                                    1C03C163-02C0003F-00000FFF-00000006
      CPUID 00000005                                    00000040-00000040-00000003-00021120
      CPUID 00000006                                    00000075-00000002-00000009-00000000
      CPUID 00000007                                    00000000-00000000-00000000-00000000
      CPUID 00000008                                    00000000-00000000-00000000-00000000
      CPUID 00000009                                    00000000-00000000-00000000-00000000
      CPUID 0000000A                                    07300403-00000000-00000000-00000603
      CPUID 0000000B                                    00000001-00000002-00000100-00000003
      CPUID 0000000B                                    00000004-00000004-00000201-00000003
      CPUID 0000000C                                    00000000-00000000-00000000-00000000
      CPUID 0000000D                                    00000007-00000240-00000340-00000000
      CPUID 0000000D                                    00000100-00000240-00000000-00000000
      CPUID 80000000                                    80000008-00000000-00000000-00000000
      CPUID 80000001                                    00000000-00000000-00000001-28100000
      CPUID 80000002                                    20202020-49202020-6C65746E-20295228
      CPUID 80000003                                    65726F43-294D5428-2D336920-30313332
      CPUID 80000004                                    5043204D-20402055-30312E32-007A4847
      CPUID 80000005                                    00000000-00000000-00000000-00000000
      CPUID 80000006                                    00000000-00000000-01006040-00000000
      CPUID 80000007                                    00000000-00000000-00000000-00000100
      CPUID 80000008                                    00003024-00000000-00000000-00000000


参考文献——
《Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 2 (2A, 2B & 2C): Instruction Set Reference, A-Z》. May 2012. http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.html
《Intel® Processor Identification and the CPUID Instruction》. April 2012. http://developer.intel.com/content/www/us/en/processors/processor-identification-cpuid-instruction-note.html
《AMD CPUID Specification》. September 2010. http://support.amd.com/us/Embedded_TechDocs/25481.pdf
《Intel® 64 Architecture Processor Topology Enumeration》. http://software.intel.com/en-us/articles/intel-64-architecture-processor-topology-enumeration/

原文地址:https://www.cnblogs.com/zyl910/p/Parse_APIC_ID.html