2014-01-29 166 views

回答

3

簡短的回答

內核採用專用CPU指令cpuid並在內部保存結果結構 - cpuinfo_x86用於x86

龍答案

內核源爲y我們最好的朋友。 從入口點開始 - 文件/proc/cpuinfo。 作爲任何proc文件,它必須在內核的某個地方創建,並用一些file_operations聲明。這在fs/proc/cpuinfo.c完成。有趣的部分是seq_open,使用參考一些cpuinfo_op。此操作在arch/x86/kernel/cpu/proc.c中聲明,我們在其中看到一些show_cpuinfo函數。該功能位於line 57的同一個文件中。

在這裏你可以看到聲明的第一行作爲struct cpuinfo_x86

64   seq_printf(m, "processor\t: %u\n" 
65     "vendor_id\t: %s\n" 
66     "cpu family\t: %d\n" 
67     "model\t\t: %u\n" 
68     "model name\t: %s\n", 
69     cpu, 
70     c->x86_vendor_id[0] ? c->x86_vendor_id : "unknown", 
71     c->x86, 
72     c->x86_model, 
73     c->x86_model_id[0] ? c->x86_model_id : "unknown"); 

結構c。該結構在arch/x86/include/asm/processor.h中聲明。如果你搜索的引用上的結構,你會發現功能cpu_detect和函數調用函數cpuid最後將其解析爲native_cpuid,看起來像這樣:

189 static inline void native_cpuid(unsigned int *eax, unsigned int *ebx, 
190         unsigned int *ecx, unsigned int *edx) 
191 { 
192   /* ecx is often an input as well as an output. */ 
193   asm volatile("cpuid" 
194    : "=a" (*eax), 
195    "=b" (*ebx), 
196    "=c" (*ecx), 
197    "=d" (*edx) 
198    : "" (*eax), "2" (*ecx) 
199    : "memory"); 
200 } 

在這裏,你看到的彙編指令cpuid。而這個小東西確實有用。

+0

一個絕妙的答案。正是我需要的!非常感謝! –

0

這些信息來自BIOS +硬件數據庫。你可以得到的信息通過的dmidecode直接,例如(如果你需要更多的信息 - 嘗試檢查的dmidecode源代碼)

sudo dmidecode -t processor