Skip to content

Commit 4fdcd8d

Browse files
authored
Add host architecture to cpuid (#184)
This change adds reading the host architecture to blink's custom cpuid instruction. With this, an x86_64 application can query the underlying architecture to perform architecture-specific logic, like extract arch native binaries out of a Cosmopolitan binary's zipos.
1 parent 7de0bd8 commit 4fdcd8d

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,21 @@ number:
709709
- `Windows` for Windows under Cosmopolitan
710710
- `Unknown` if compiled on unrecognized platform
711711

712+
- Leaf `0x40031338` reports the underlying hardware architecture name
713+
in `EBX ‖ ECX ‖ EDX` with zero filling for strings shorter than 12:
714+
715+
- `x86_64` for x86_64
716+
- `i386` for i386
717+
- `aarch64` for aarch64
718+
- `arm` for arm32
719+
- `ppc64` for powerpc64
720+
- `ppc64le` for powerpc64le
721+
- `ppc` for powerpc
722+
- `s390x` for s390x
723+
- `riscv64` for riscv64
724+
- `riscv32` for riscv32
725+
- `Unknown` if compiled on unrecognized platform
726+
712727
- Leaf `0x80000001` tells if Blink's JIT is enabled in bit `31` in `ECX`
713728

714729
### JIT

blink/cpuid.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,41 @@
5959
#define OS UNKNOWN_
6060
#endif
6161

62+
#define X86_64_ "x86_64\0\0\0\0\0\0"
63+
#define I386_ "i386\0\0\0\0\0\0\0\0"
64+
#define AARCH64_ "aarch64\0\0\0\0\0"
65+
#define ARM_ "arm\0\0\0\0\0\0\0\0\0"
66+
#define PPC64_ "ppc64\0\0\0\0\0\0\0"
67+
#define PPC64LE_ "ppc64le\0\0\0\0\0"
68+
#define PPC_ "ppc\0\0\0\0\0\0\0\0\0"
69+
#define S390X_ "s390x\0\0\0\0\0\0\0"
70+
#define RISCV64_ "riscv64\0\0\0\0\0"
71+
#define RISCV32_ "riscv32\0\0\0\0\0"
72+
73+
#if defined(__x86_64__)
74+
#define ARCH_NAME X86_64_
75+
#elif defined(__i386__)
76+
#define ARCH_NAME I386_
77+
#elif defined(__aarch64__)
78+
#define ARCH_NAME AARCH64_
79+
#elif defined(__ARMEL__)
80+
#define ARCH_NAME ARM_
81+
#elif defined(__powerpc64__) && defined(__LITTLE_ENDIAN__)
82+
#define ARCH_NAME PPC64LE_
83+
#elif defined(__powerpc64__)
84+
#define ARCH_NAME PPC64_
85+
#elif defined(__powerpc__)
86+
#define ARCH_NAME PPC_
87+
#elif defined(__s390x__)
88+
#define ARCH_NAME S390X_
89+
#elif defined(__riscv) && __riscv_xlen == 64
90+
#define ARCH_NAME RISCV64_
91+
#elif defined(__riscv) && __riscv_xlen == 32
92+
#define ARCH_NAME RISCV32_
93+
#else
94+
#define ARCH_NAME UNKNOWN_
95+
#endif
96+
6297
void OpCpuid(P) {
6398
u32 ax, bx, cx, dx, jit;
6499
if (m->trapcpuid) {
@@ -88,6 +123,11 @@ void OpCpuid(P) {
88123
cx = Read32((const u8 *)OS + 4);
89124
dx = Read32((const u8 *)OS + 8);
90125
break;
126+
case 0x40031338:
127+
bx = Read32((const u8 *)ARCH_NAME + 0);
128+
cx = Read32((const u8 *)ARCH_NAME + 4);
129+
dx = Read32((const u8 *)ARCH_NAME + 8);
130+
break;
91131
case 1:
92132
cx |= 1 << 0; // sse3
93133
cx |= 1 << 1; // pclmulqdq

0 commit comments

Comments
 (0)