Intel CPU的CPUID指令
Intel有一個超過100頁的文檔,專門介紹cpuid這條指令,可見這條指令涉及內容的豐富。
記得去年的時候,曾經有個“英布之劍”問過我這條指令,當時並沒有給出一個滿意的回答,現在放假,想起來,把資料整理了一下。很久以前確實用過這條指令,其實指令本身並沒有什麼難的,關鍵是看你有沒有耐心研讀完繁瑣的資料,當然還得對CPU有一定的瞭解,如果“英布之劍”看到這篇文章,而且仍然需要更詳細的資料,可以給我一個聯繫方式,或者相互之間可以交流一下。
cpuid就是一條讀取CPU各種信息的一條指令,大概是從80486的某個版本開始就存在了。似乎是從80386開始,當CPU被RESET以後,CPU會在EDX寄存器中返回一個32bits的CPU簽名(Processor Identification Signature),但這時候CPU還沒有CPUID這條指令,後來出現了這條指令後,軟件無需以來CPU復位就可以讀出這個CPU簽名,同時還可以讀出很多CPU的相關信息。
CPUID這條指令,除了用於識別CPU(CPU的型號、家族、類型等),還可以讀出CPU支持的功能(比如是否支持MMX,是否支持4MB的頁等等),內容的確是十分豐富。CPUID指令有兩組功能,一組返回的是基本信息,另一組返回的是擴展信息,本文介紹基本信息部分,擴展信息部分下篇中介紹。本文所在程序或程序片段,均使用MASM 6.11編譯連接,可以在DOS(包括虛擬機的DOS下)運行。
1、如何判斷CPU是否支持CPUID指令
前面說過,大概是從80486開始纔有的cpuid這個指令,是不是所有的80486家族CPU都有這個指令我也不是很清楚,但在EFLAGS中的bit 21可以識別CPU是否支持CPUID指令,如下圖:
圖1
在8086和8088CPU中,FLAGS只有16位長,在80386CPU中,bit 21被保留未用,在支持CPUID指令的CPU中,這一位將爲1。
2、CPUID指令的執行方法
把功能代碼放在EAX寄存器中,執行CPUID指令即可。例如:
mov eax, 1
cpuid
前面說過CPUID指令分爲兩組,一組返回基本信息,一組返回擴展信息,當執行返回基本信息的CPUID指令時,EAX中功能代碼的bit 31爲0,當執行返回擴展信息的CPUID指令時,EAX中的功能代碼的bit 31爲1。那麼不管是那組功能,如何知道EAX中的功能代碼最大可以是多少呢?根據Intel的說明,可以用如下方法:
mov eax, 0
cpuid
執行完CPUID指令後,EAX中返回的值就是返回基本信息時,功能代碼的最大值,在執行CPUID指令要求返回基本信息時,EAX中的值必須小於或等於該值。
mov eax, 80000000h
cpuid
執行完CPUID指令後,EAX中返回的值就是返回擴展信息時,功能代碼的最大值,在執行CPUID指令要求返回擴展信息時,EAX中的值必須小於或等於該值。
由於很多編譯器都不能編譯CPUID指令,所以瞭解CPUID指令的操作碼是必要的,CPUID指令的操作碼是:
0FA2h
3、返回基本信息的功能全貌
在實際介紹每一個功能之前,我們先通過一張圖瞭解一下返回基本信息的功能全貌。
圖2
4、EAX=0:獲取CPU的Vendor ID
Vendor ID這個東西,在以前介紹PCI的文章中應該介紹過,實際上就是製造商的標識,用下面的方法執行該功能:
mov eax, 0
cpuid
執行CPUID指令後,AX中返回的內容前面已經說過了,返回的Vendor ID固定爲12個ASCII字符依次存放在EBX、EDX、ECX中,對於Intel的CPU,返回的字符串永遠是:GenuineIntel。對應在三個寄存器中的值如下:
EBX=756E6547h,EDX=49656E69h,ECX=6C65746Eh
大家可以參考圖2。
儘管本文是介紹Intel的CPUID指令,但下面還是盡我所知,列出其它廠家生產的IA-32架構CPU的Vendor ID,希望能對需要這些資料的人有所幫助。
- AMDisbetter! ---- 早期AMD K5芯片的工程樣品芯片
- AuthenticAMD ---- AMD
- CentourHauls ---- Centour
- CyrixInstead ---- Cyrix
- GenuineTMx86 或 TransmetaCPU ---- Transmeta
- Geode by NSC ---- National Semiconductor
- NexGenDriven ---- NexGen
- SiS SiS SiS ---- SiS
- RiseRiseRise ---- Rise
- UMC UMC UMC ---- UMC
- VIA VIA VIA ---- VIA
5、EAX=1:處理器簽名(Processor Signiture)和功能(Feature)位
mov eax, 1
cpuid
執行完成後,處理器簽名放在EAX中,功能位及其它雜七雜八的內容分別放在EBX、ECX和EDX中。
- 處理器簽名(Processor Signiture):
返回在EAX中,定義如下:
圖中的灰色區域表示沒有定義。前面說過,當CPU復位時,會在EDX中返回處理器簽名,從80486以後,這個簽名和上面的定義完全一樣,只是放在不同的寄存器中而已。前面還提到過,80386在復位時也返回處理器簽名,但80386返回的簽名格式是和上面不同的,後面可能會提到。
通過處理器簽名,可以確定CPU的具體型號,以下是部分Intel CPU的處理器簽名數據(資料來自Intel):
前面說過,80386儘管沒有CPUID指令,但在復位時也是可以返回處理器簽名的,下面是80386返回的處理器簽名的格式:
下面是80386處理器簽名的識別方法(資料來自Intel):
- 關於Stepping的說明:
Intel和AMD都有Stepping的概念,用來標識一款同樣規模的微處理器從一開始到你用的這款處理器經歷的設計過程,用一個字母和一個數字表示。一般來說,一款同樣規模的微處理器的第一個版本是A0,如果改進了設計,則可以通過改變字母或者數字進行標識,如果僅僅改變數字(比如改成A3),說明進行了一些輔助的改進,如果字母和數字都改變,說明改動較大,Stepping可以使用戶可以識別微處理器的版本。下面是一個Stepping的例子(不知道爲什麼穿上來後圖這麼小)。
- 當處理器簽名一樣時的處理
有時候,從處理器簽名上仍然不能識別CPU,比如根據Intel提供的資料,Pentium II, Model 5、Pentium II Xeon, Model 5和Celeron®, Model 5的處理器簽名完全一樣,要區別他們只能通過檢查他們的高速緩存(Cache)的大小,後面將介紹使用CPUID指令獲得CPU高速緩存信息的方法,如果沒有高速緩存,則是Celeron®處理器;如果L2高速緩存爲1MB或者2MB,則應該是Pentium II Xeon處理器,其它情況則應該是Pentium II處理器或者是隻有512KB高速緩存的Pentium II Xeon處理器。
有些情況下,如果從處理器簽名上不能區分CPU,也可以使用Brand ID(在EBX的bit7:0返回)來區分CPU,比如Pentium III, Model 8、Pentium III Xeon, Model 8和Celeron®, Model 8三種處理器的處理器簽名也是一樣的,但它們的Brand ID是不同的。
- 關於處理器類型的定義
在處理器簽名中的bit12:13返回的是處理器類型,其定義如下
Value Descriptor
---------------------------------------------
00 以前的OEM處理器
01 OverDrive®處理器
10 多處理器(指可用於多處理器系統)
- 功能標誌(Feature Flag)
在EDX和ECX中返回的功能標誌表明着該CPU都支持那些功能,EDX定義如下(資料來源與Intel):
bit Name Description
--------------------------------------------------------------------------------
00 FPU FPU On-chip
01 VME Virtual Mode Extended
02 DE Debugging Extension
03 PSE Page Size Extension
04 TSC Time Stamp Counter
05 MSR Model Specific Registers
06 PAE Physical Address Extension
07 MCE Machine-Check Exception
08 CX8 CMPXCHG8 Instruction
09 APIC On-chip APIC Hardware
10 Reserved
11 SEP Fast System Call
12 MTRR Memory Type Range Registers
13 PGE Page Global Enable
14 MCA Machine-Check Architecture
15 CMOV Conditional Move Instruction
16 PAT Page Attribute Table
17 PSE-36 36-bit Page Size Extension
18 PSN Processor serial number is present and enabled
19 CLFSH CLFLUSH Instruction
20 Reserved
21 DS Debug Store
22 ACPI Thermal Monitor and Software Controlled Clock Facilities
23 MMX MMX technology
24 FXSR FXSAVE and FXSTOR Instructions
25 SSE Streaming SIMD Extensions
26 SSE2 Streaming SIMD Extensions 2
27 SS Self-Snoop
28 HTT Multi-Threading
29 TM Thermal Monitor
30 IA64 IA64 Capabilities
31 PBE Pending Break Enable
ECX定義如下(資料來自Intel):
bit Name Description
---------------------------------------------------------
00 SSE3 Streaming SIMD Extensions 3
01 Reserved
02 DTES64 64-Bit Debug Store
03 MONITOR MONITOR/MWAIT
04 DS-CPL CPL Qualified Debug Store
05 VMX Virtual Machine Extensions
06 SMX Safer Mode Extensions
07 EST Enhanced Intel SpeedStep® Technology
08 TM2 Thermal Monitor 2
09 SSSE3 Supplemental Streaming SIMD Extensions 3
10 CNXT-ID L1 Context ID
12:11 Reserved
13 CX16 CMPXCHG16B
14 xTPR xTPR Update Control
15 PDCM Perfmon and Debug Capability
17:16 Reserved
18 DCA Direct Cache Access
19 SSE4.1 Streaming SIMD Extensions 4.1
20 SSE4.2 Streaming SIMD Extensions 4.2
21 x2APIC Extended xAPIC Support
22 MOVBE MOVBE Instruction
23 POPCNT POPCNT Instruction
25:24 Reserved
26 XSAVE XSAVE/XSTOR States
27 OSXSAVE
31:28 Reserved
下面是在DEBUG中當EAX=0時執行CPUID指令時的情況:
下面是在DEBUG中當EAX=1時執行CPUID指令時的情況
6、EAX=2:高速緩存描述符(Cache Descriptor)
mov eax, 2
cpuid
執行完CPUID指令後,高速緩存描述符和TLB(Translation Lookable Buffer)特性將在EAX、EBX、ECX和EDX中返回,每個寄存器中的4個字節分別表示4個描述符,描述符中不同的值表示不同的含義(後面有定義),其中EAX中的最低8位(AL)的值表示要得到完整的高速緩存的信息,需要執行EAX=2的CPUID指令的次數(一般都爲1,在我這裏的數臺機器裏,還沒有爲2的),同時,寄存器的最高位(bit 31)爲0,表示該寄存器中的描述符是有效的,下面是描述符值的定義(資料來源與Intel):
Value Cache or TLB Descriptor Description
----------------------------------------------------------------------------------------
00h Null
01h Instruction TLB: 4-KB Pages, 4-way set associative, 32 entries
02h Instruction TLB: 4-MB Pages, fully associative, 2 entries
03h Data TLB: 4-KB Pages, 4-way set associative, 64 entries
04h Data TLB: 4-MB Pages, 4-way set associative, 8 entries
05h Data TLB: 4-MB Pages, 4-way set associative, 32 entries
06h 1st-level instruction cache: 8-KB, 4-way set associative, 32-byte line size
08h 1st-level instruction cache: 16-KB, 4-way set associative, 32-byte line size
09h 1st-level Instruction Cache: 32-KB, 4-way set associative, 64-byte line size
0Ah 1st-level data cache: 8-KB, 2-way set associative, 32-byte line size
0Ch 1st-level data cache: 16-KB, 4-way set associative, 32-byte line size
0Dh 1st-level Data Cache: 16-KB, 4-way set associative, 64-byte line size, ECC
21h 256-KB L2 (MLC), 8-way set associative, 64-byte line size
22h 3rd-level cache: 512-KB, 4-way set associative, sectored cache, 64-byte line size
23h 3rd-level cache: 1-MB, 8-way set associative, sectored cache, 64-byte line size
25h 3rd-level cache: 2-MB, 8-way set associative, sectored cache, 64-byte line size
29h 3rd-level cache: 4-MB, 8-way set associative, sectored cache, 64-byte line size
2Ch 1st-level data cache: 32-KB, 8-way set associative, 64-byte line size
30h 1st-level instruction cache: 32-KB, 8-way set associative, 64-byte line size
39h 2nd-level cache: 128-KB, 4-way set associative, sectored cache, 64-byte line size
3Ah 2nd-level cache: 192-KB, 6-way set associative, sectored cache, 64-byte line size
3Bh 2nd-level cache: 128-KB, 2-way set associative, sectored cache, 64-byte line size
3Ch 2nd-level cache: 256-KB, 4-way set associative, sectored cache, 64-byte line size
3Dh 2nd-level cache: 384-KB, 6-way set associative, sectored cache, 64-byte line size
3Eh 2nd-level cache: 512-KB, 4-way set associative, sectored cache, 64-byte line size
40h No 2nd-level cache or, if processor contains a valid 2nd-level cache, no 3rd-level cache
41h 2nd-level cache: 128-KB, 4-way set associative, 32-byte line size
42h 2nd-level cache: 256-KB, 4-way set associative, 32-byte line size
43h 2nd-level cache: 512-KB, 4-way set associative, 32-byte line size
44h 2nd-level cache: 1-MB, 4-way set associative, 32-byte line size
45h 2nd-level cache: 2-MB, 4-way set associative, 32-byte line size
46h 3rd-level cache: 4-MB, 4-way set associative, 64-byte line size
47h 3rd-level cache: 8-MB, 8-way set associative, 64-byte line size
48h 2nd-level cache: 3-MB, 12-way set associative, 64-byte line size, unified on-die
49h 3rd-level cache: 4-MB, 16-way set associative, 64-byte line size(Intel Xeon
processor MP, Family 0Fh, Model 06h) 2nd-level cache: 4-MB, 16-way set associative,
64-byte line size
4Ah 3rd-level cache: 6-MB, 12-way set associative, 64-byte line size
4Bh 3rd-level cache: 8-MB, 16-way set associative, 64-byte line size
4Ch 3rd-level cache: 12-MB, 12-way set associative, 64-byte line size
4Dh 3rd-level cache: 16-MB, 16-way set associative, 64-byte line size
4Eh 2nd-level cache: 6-MB, 24-way set associative, 64-byte line size
50h Instruction TLB: 4-KB, 2-MB or 4-MB pages, fully associative, 64 entries
51h Instruction TLB: 4-KB, 2-MB or 4-MB pages, fully associative, 128 entries
52h Instruction TLB: 4-KB, 2-MB or 4-MB pages, fully associative, 256 entries
55h Instruction TLB: 2-MB or 4-MB pages, fully associative, 7 entries
56h L1 Data TLB: 4-MB pages, 4-way set associative, 16 entries
57h L1 Data TLB: 4-KB pages, 4-way set associative, 16 entries
5Ah Data TLB0: 2-MB or 4-MB pages, 4-way associative, 32 entries
5Bh Data TLB: 4-KB or 4-MB pages, fully associative, 64 entries
5Ch Data TLB: 4-KB or 4-MB pages, fully associative, 128 entries
5Dh Data TLB: 4-KB or 4-MB pages, fully associative, 256 entries
60h 1st-level data cache: 16-KB, 8-way set associative, sectored cache, 64-byte line size
66h 1st-level data cache: 8-KB, 4-way set associative, sectored cache, 64-byte line size
67h 1st-level data cache: 16-KB, 4-way set associative, sectored cache, 64-byte line size
68h 1st-level data cache: 32-KB, 4 way set associative, sectored cache, 64-byte line size
70h Trace cache: 12K-uops, 8-way set associative
71h Trace cache: 16K-uops, 8-way set associative
72h Trace cache: 32K-uops, 8-way set associative
73h Trace cache: 64K-uops, 8-way set associative
78h 2nd-level cache: 1-MB, 4-way set associative, 64-byte line size
79h 2nd-level cache: 128-KB, 8-way set associative, sectored cache, 64-byte line size
7Ah 2nd-level cache: 256-KB, 8-way set associative, sectored cache, 64-byte line size
7Bh 2nd-level cache: 512-KB, 8-way set associative, sectored cache, 64-byte line size
7Ch 2nd-level cache: 1-MB, 8-way set associative, sectored cache, 64-byte line size
7Dh 2nd-level cache: 2-MB, 8-way set associative, 64-byte line size
7Fh 2nd-level cache: 512-KB, 2-way set associative, 64-byte line size
82h 2nd-level cache: 256-KB, 8-way set associative, 32-byte line size
83h 2nd-level cache: 512-KB, 8-way set associative, 32-byte line size
84h 2nd-level cache: 1-MB, 8-way set associative, 32-byte line size
85h 2nd-level cache: 2-MB, 8-way set associative, 32-byte line size
86h 2nd-level cache: 512-KB, 4-way set associative, 64-byte line size
87h 2nd-level cache: 1-MB, 8-way set associative, 64-byte line size
B0h Instruction TLB: 4-KB Pages, 4-way set associative, 128 entries
B1h Instruction TLB: 2-MB pages, 4-way, 8 entries or 4M pages, 4-way, 4 entries
B2h Instruction TLB: 4-KB pages, 4-way set associative, 64 entries
B3h Data TLB: 4-KB Pages, 4-way set associative, 128 entries
B4h Data TLB: 4-KB Pages, 4-way set associative, 256 entries
CAh Shared 2nd-level TLB: 4 KB pages, 4-way set associative, 512 entries
D0h 512KB L3 Cache, 4-way set associative, 64-byte line size
D1h 1-MB L3 Cache, 4-way set associative, 64-byte line size
D2h 2-MB L3 Cache, 4-way set associative, 64-byte line size
D6h 1-MB L3 Cache, 8-way set associative, 64-byte line size
D7h 2-MB L3 Cache, 8-way set associative, 64-byte line size
D8h 4-MB L3 Cache, 8-way set associative, 64-byte line size
DCh 2-MB L3 Cache, 12-way set associative, 64-byte line size
DDh 4-MB L3 Cache, 12-way set associative, 64-byte line size
DEh 8-MB L3 Cache, 12-way set associative, 64-byte line size
E2h 2-MB L3 Cache, 16-way set associative, 64-byte line size
E3h 4-MB L3 Cache, 16-way set associative, 64-byte line size
E4h 8-MB L3 Cache, 16-way set associative, 64-byte line size
F0h 64-byte Prefetching
F1h 128-byte Prefetching
舉例來說,在我的機器上執行記過如下:
EAX、EBX、ECX和EDX的bit 31均爲0,說明其中的描述符均有效,EAX中的低8位(AL)爲1,說明執行一次即可,下面是描述符含義:
05h:Data TLB: 4-MB Pages, 4-way set associative, 32 entries
B0h:Instruction TLB: 4-KB Pages, 4-way set associative, 128 entries
B1h:Instruction TLB: 2-MB pages, 4-way, 8 entries or 4M pages, 4-way, 4 entries
56h:L1 Data TLB: 4-MB pages, 4-way set associative, 16 entries
57h:L1 Data TLB: 4-KB pages, 4-way set associative, 16 entries
F0h:64-byte Prefetching
2Ch:1st-level data cache: 32-KB, 8-way set associative, 64-byte line size
B4h:Data TLB: 4-KB Pages, 4-way set associative, 256 entries
30h:1st-level instruction cache: 32-KB, 8-way set associative, 64-byte line size
7Dh:2nd-level cache: 2-MB, 8-way set associative, 64-byte line size
7、EAX=3:處理器序列號
mov eax,3
cpuid
只有Pentium III提供該功能,486以後的CPU就不再提供該功能,據說是出於隱私的原因。查看你的處理器是否支持處理器序列號功能,可以執行EAX=1的CPUID指令,然後查看EDX的PSN功能(bit 18),如果爲1,說明你的處理器可以返回序列號,否則不支持序列號功能或者是序列號功能被關閉了。
處理器序列號一共96位,最高32位就是處理器簽名,通過執行EAX=1的CPUID指令獲得,其餘的64位在執行EAX=3的CPUID指令後,中間32位在EDX中,最低32位在ECX中。
順便提一句,AMD所有的CPU都沒有提供過處理器序列號的功能。
CPUID指令的基本信息,其實後面還有好幾個,不過我在我這裏的機器(大概有7、8臺吧),好像都不支持,大多數只支持EAX=0、1、2三個,所以後面的就不介紹了。
8、EAX=80000001h:最大擴展功能號
mov eax, 80000001h
cpuid
該功能除了能夠向(一)中介紹的那樣返回CPU支持的最大擴展功能號外,並沒有其它作用,EBX、ECX、EDX都不返回有意義的信息。
9、EAX=80000002h:返回CPU支持的擴展功能
mov eax, 80000002h
cpuid
執行CPUID指令後,擴展功能標誌在EDX和ECX中返回,EDX中的定義如下:
Bit Name Description
-------------------------------------------------------------------
10:00 Reserved
11 SYSCALL SYSCALL/SYSRET
19:12 Reserved
20 XD Bit Execution Disable Bit
28:21 Reserved
29 Intel® 64 Intel® 64 Instruction Set Architecture
31:30 Reserved
返回在ECX中的位定義:
Bit Name Description
-------------------------------------------------------------------
0 LAHF LAHF / SAHF
31:01 Reserved
10、EAX=80000002h、80000003h、80000004h:返回處理器名稱/商標字符串
mov eax, 80000002h
cpuid
......
mov eax, 80000003h
cpuid
......
mov eax, 80000004h
cpuid
每次調用CPUID分別在EAX、EBX、ECX、EDX中返回16個ASCII字符,處理器名稱/商標字串最多48個字符,前導字符爲空格,結束字符爲NULL,在寄存器中的排列順序爲little-endian(即低字符在前),下面程序可以在DOS下顯示處理器名稱/商標字串(使用MASM 6編譯)。
.model tiny
.386
cseg segment para public 'code'
org 100h
assume cs:cseg, ds:cseg, es:cseg
cpuid macro
db 0fh
db 0a2h
endm
begin:
mov eax, 80000000h
cpuid
cmp eax, 80000004h
jb not_supported
mov di, offset CPU_name
mov eax, 80000002h
cpuid
call save_string
mov eax, 80000003h
cpuid
call save_string
mov eax, 80000004h
cpuid
call save_string
mov dx, offset crlf
mov ah, 9
int 21h
cld
mov si, offset CPU_name
spaces:
lodsb
cmp al, ' '
jz spaces
cmp al, 0
jz done
disp_char:
mov dl, al
mov ah, 2
int 21h
lodsb
cmp al, 0
jnz disp_char
done:
mov ax, 4c00h
int 21h
not_supported:
jmp done
save_string:
mov dword ptr [di], eax
mov dword ptr [di + 4], ebx
mov dword ptr [di + 8], ecx
mov dword ptr [di + 12], edx
add di, 16
ret
crlf db 0dh, 0ah, '$'
CPU_name db 50 dup(0)
cseg ends
end begin
11、EAX=80000005h:備用
12、EAX=80000006h:擴展L2高速緩存功能
mov eax, 80000006h
cpuid
執行完CPUID指令後,相應信息在ECX中返回,以下是ECX中返回信息的定義:
Bits Description
-----------------------------------------------------------
31:16 L2 Cache size described in 1024-byte units.
15:12 L2 Cache Associativity Encodings
00h Disabled
01h Direct mapped
02h 2-Way
04h 4-Way
06h 8-Way
08h 16-Way
0Fh Fully associative
11:8 Reserved
7:0 L2 Cache Line Size in bytes.
13、EAX=80000007h:電源管理
mov eax, 80000007h
cpuid
執行CPUID指令後,是否支持電源管理功能在EDX的bit8中返回,其餘位無意義。
14、EAX=80000008h:虛擬地址和物理地址大小
mov eax, 80000008h
cpuid
執行CPUID指令後,物理地址的大小在EAX的bit[7:0]返回,虛擬地址的大小在EAX的bit[15:8]返回,返回的內容爲虛擬(物理)地址的位數。例如在我的機器上的運行結果如下:
表明我的機器支持36位的物理地址和48位的虛擬地址。