我如何调用" CPUID"在Linux呢? [英] How do I call "cpuid" in Linux?

查看:615
本文介绍了我如何调用" CPUID"在Linux呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在写新的code for Windows中,我偶然发现了 _cpuinfo()从Windows API。由于我主要是处理一个Linux环境(GCC)我想有机会获得CPUInfo。

我曾尝试以下内容:

 的#include<&iostream的GT;诠释的main()
{
  诠释A,B;  用于:(a = 0;一个小于5;一个++)
  {
    __asm​​(MOV%1 %% EAX;//一到EAX
          CPUID;
          MOV EAX %%,%0; // EAX为B
          := R(B)//输出
          :R(一)//输入
          :%EAX,%EBX,%ECX,%EDX//修饰寄存器
         );
    性病::法院LT&;< 在code<< A<< 给出了<< B<<的std :: ENDL;
  }  返回0;
}

本使用汇编,但我不想重新发明轮子。有没有落实CPUInfo没有装配任何其他方式?

编译器错误:

 刘易斯@刘易斯桌面:〜/桌面/ PROG $ G ++ -Wall CPUInfo.cpp
CPUInfo.cpp:在函数'廉政的main():
CPUInfo.cpp:10:22:错误:之前的预期')'';'令牌
CPUInfo.cpp:10:23:错误:预期主要-EX $之前p $ pssion')'令牌
CPUInfo.cpp:10:23:错误:预期';'之前')'标记
CPUInfo.cpp:8:8:警告:未使用的变量'B'[-Wunused变量]
CPUInfo.cpp:12:8:错误:预期}在输入端


解决方案

既然你是用gcc编译,那么你可以包括 cpuid.h 这声明这些功能:

  / *返回的CPUID指令支持的最高输入值。分机可以
   无论是或为0x0来为0x8000000为返回支持的最高值
   基本或扩展CPUID信息。函数返回0,如果CPUID
   不支持或EAX寄存器任何CPUID的回报。如果SIG
   指针非空,则前四个字节签名
   (在EBX寄存器中)在由SIG指向的位置返回。 * /
unsigned int类型__get_cpuid_max(unsigned int类型__ext,无符号整型* __ SIG)在返回找到/ *的请求CPUID级别返回CPUID数据,
   EAX,EBX,ECX和EDX寄存器。功能检查,如果是CPUID
   支持并为有效的CPUID信息或0返回1
   不支持的CPUID水平。所有的指针都必须是非空的。 * /
INT __get_cpuid(unsigned int类型__level,
    无符号整型* __ EAX,无符号整型* __ EBX,
    无符号整型* __ ECX,无符号整型* __ EDX)

您不需要和不应该重新实现此功能。

While writing new code for Windows, I stumbled upon _cpuinfo() from the Windows API. As I am mainly dealing with a Linux environment (GCC) I want to have access to the CPUInfo.

I have tried the following:

#include <iostream>

int main()
{
  int a, b;

  for (a = 0; a < 5; a++)
  {
    __asm ( "mov %1, %%eax; "            // a into eax
          "cpuid;"
          "mov %%eax, %0;"             // eax into b
          :"=r"(b)                     // output
          :"r"(a)                      // input
          :"%eax","%ebx","%ecx","%edx" // clobbered register
         );
    std::cout << "The code " << a << " gives " << b << std::endl;
  }

  return 0;
}

This use assembly but I don't want to re-invent the wheel. Is there any other way to implement CPUInfo without assembly?

Compiler errors:

lewis@lewis-desktop:~/Desktop/prog$ g++ -Wall CPUInfo.cpp
CPUInfo.cpp: In function ‘int main()’:
CPUInfo.cpp:10:22: error: expected ‘)’ before ‘;’ token
CPUInfo.cpp:10:23: error: expected primary-expression before ‘)’ token
CPUInfo.cpp:10:23: error: expected ‘;’ before ‘)’ token
CPUInfo.cpp:8:8: warning: unused variable ‘b’ [-Wunused-variable]
CPUInfo.cpp:12:8: error: expected ‘}’ at end of input

解决方案

Since you are compiling with GCC then you can include cpuid.h which declares these functions:

/* Return highest supported input value for cpuid instruction.  ext can
   be either 0x0 or 0x8000000 to return highest supported value for
   basic or extended cpuid information.  Function returns 0 if cpuid
   is not supported or whatever cpuid returns in eax register.  If sig
   pointer is non-null, then first four bytes of the signature
   (as found in ebx register) are returned in location pointed by sig.  */
unsigned int __get_cpuid_max (unsigned int __ext, unsigned int *__sig)

/* Return cpuid data for requested cpuid level, as found in returned
   eax, ebx, ecx and edx registers.  The function checks if cpuid is
   supported and returns 1 for valid cpuid information or 0 for
   unsupported cpuid level.  All pointers are required to be non-null.  */
int __get_cpuid (unsigned int __level,
    unsigned int *__eax, unsigned int *__ebx,
    unsigned int *__ecx, unsigned int *__edx)

You don't need to, and should not, re-implement this functionality.

这篇关于我如何调用&QUOT; CPUID&QUOT;在Linux呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆