假设我们有一台没有任何操作系统的空白计算机,我们正在安装 Linux。识别处理器和内核并从中获取信息的代码在内核的哪个位置? 此信息最终会出现在/proc/cpuinfo 等位置,但内核首先是如何获取它的?!
最佳答案
简答
内核使用特殊的 CPU 指令 cpuid
并将结果保存在内部结构中 - cpuinfo_x86
对于 x86
长答案
内核源代码是您最好的 friend 。
从入口点开始 - 文件 /proc/cpuinfo
。
与任何 proc 文件一样,它必须在内核的某处创建并用一些 file_operations 声明。这是在 fs/proc/cpuinfo.c 完成的.有趣的部分是 seq_open
,它使用对某些 cpuinfo_op
的引用。此操作在 arch/x86/kernel/cpu/proc.c 中声明我们在其中看到一些 show_cpuinfo
函数。此函数位于 line 57 上的同一文件中.
这里可以看到
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
在第一行声明为 struct cpuinfo_x86
。此结构在 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
.这个小东西确实有用。
关于linux - Linux 内核如何获取有关处理器和内核的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21422433/