我要评分
获取效率
正确性
完整性
易理解

Replacing the x86 cpuid Assembly Instruction

Symptom

Error: /tmp/ccfaVZfw.s: Assembler messages:

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

Cause

cpuid is an x86 assembly instruction used to obtain CPU ID information and cannot be used on the Kunpeng platform. On the Kunpeng platform, the midr_el1 register stores the CPU ID information. You can obtain the CPU ID from the register.

Procedure

Example code on the x86 platform:

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 is a 64-bit register. The high-order 32 bits are reserved and the value is 0. The read result is a 32-bit value. Replace the code as follows on the Kunpeng platform:

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));