替换x86 cpuid汇编指令

现象描述

编译报错:/tmp/ccfaVZfw.s: Assembler messages:

/tmp/ccfaVZfw.s:34: Error: unknown mnemonic 'cpuid' -- 'cpuid'。

问题原因

cpuid是x86平台上专有的获取cpuid信息的汇编指令,在鲲鹏平台上需要重写。鲲鹏平台上,midr_el1寄存器里存放的是cpuid的信息,可以通过读寄存器获取到cpuid。

处理步骤

x86实现样例:

unsigned int s1 = 0;
unsigned int s2 = 0;
char cpu[32] = {0};
asm volatile
        (
         "movl $0x01, %%eax; \n\t"
         "xorl %%edx, %%edx; \n\t"
         "cpuid; \n\t"
         "movl %%edx, %0; \n\t"
         "movl %%eax, %1; \n\t"
         : "=m"(s1), "=m"(s2)
        );
snprintf(cpu, sizeof(cpu), "%08X%08X", htonl(s2), htonl(s1));

midr_el1是64位寄存器,其中高32位为预留位,其值为0。读出来是一个32bit的值。鲲鹏平台上可替换成:

unsigned int s1 = 0;
unsigned int s2 = 0;
char cpu[32] = {0};
asm volatile
        (
         "mrs %0, midr_el1"
         : "=r"(s1)
         :
         :"memory" );
snprintf(cpu, sizeof(cpu), "%08X%08X", htonl(s1), htonl(s2));