Skip to content

Commit 6aa2f78

Browse files
committed
x86 asm: CPUID mov in from x86-assembly-cheat
1 parent 3084762 commit 6aa2f78

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

README.adoc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12506,6 +12506,44 @@ Generated some polemic when kernel devs wanted to use it as part of `/dev/random
1250612506

1250712507
RDRAND sets the carry flag when data is ready so we must loop if the carry flag isn't set.
1250812508

12509+
==== x86 CPUID instruction
12510+
12511+
Example: link:userland/arch/x86_64/cpuid.S[CPUID]
12512+
12513+
Fills EAX, EBX, ECX and EDX with CPU information.
12514+
12515+
The exact data to show depends on the value of EAX, and for a few cases instructions ECX. When it depends on ECX, it is called a sub-leaf. Out test program prints `eax == 0`.
12516+
12517+
On <<p51>> for example the output EAX, EBX, ECX and EDX are:
12518+
12519+
....
12520+
0x00000016
12521+
0x756E6547
12522+
0x6C65746E
12523+
0x49656E69
12524+
....
12525+
12526+
EBX and ECX are easy to interpret:
12527+
12528+
* EBX: 75 6e 65 47 == 'u', 'n', 'e', 'G' in ASCII
12529+
* ECX: 6C 65 74 6E == 'l', 'e', 't', 'n'
12530+
12531+
so we see the string `Genu ntel` which is a shorthand for "Genuine Intel". Ha, I wonder if they had serious CPU pirating problems in the past? :-)
12532+
12533+
Information available includes:
12534+
12535+
* vendor
12536+
* version
12537+
* features (mmx, simd, rdrand, etc.) <http://en.wikipedia.org/wiki/CPUID# EAX.3D1:_Processor_Info_and_Feature_Bits>
12538+
* caches
12539+
* tlbs http://en.wikipedia.org/wiki/Translation_lookaside_buffer
12540+
12541+
The cool thing about this instruction is that it allows you to check the CPU specs and take alternative actions based on that inside your program.
12542+
12543+
On Linux, the capacity part of this information is parsed and made available at `cat /proc/cpuinfo`. See: http://unix.stackexchange.com/questions/43539/what-do-the-flags-in-proc-cpuinfo-mean
12544+
12545+
There is also the `cpuinfo` command line tool that parses the CPUID instruction from the command line. Source: http://www.etallen.com/cpuid.html
12546+
1250912547
=== x86 x87 FPU instructions
1251012548

1251112549
<<intel-manual-1>> 5.2 "X87 FPU INSTRUCTIONS"

userland/arch/x86_64/cpuid.S

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#x86-cpuid-instruction */
2+
3+
#include <lkmc.h>
4+
5+
LKMC_PROLOGUE
6+
mov $0, %eax
7+
cpuid
8+
9+
/* Save the other registers. */
10+
mov %ebx, %r12d
11+
mov %ecx, %r13d
12+
mov %edx, %r14d
13+
14+
/* eax */
15+
mov %eax, %edi
16+
call lkmc_print_hex_32
17+
call lkmc_print_newline
18+
19+
/* ebx */
20+
mov %r12d, %edi
21+
call lkmc_print_hex_32
22+
call lkmc_print_newline
23+
24+
/* ecx */
25+
mov %r13d, %edi
26+
call lkmc_print_hex_32
27+
call lkmc_print_newline
28+
29+
/* edx */
30+
mov %r14d, %edi
31+
call lkmc_print_hex_32
32+
call lkmc_print_newline
33+
LKMC_EPILOGUE

0 commit comments

Comments
 (0)