20#ifdef RUAPU_IMPLEMENTATION
22#ifndef RUAPU_BAREMETAL
31#if WINAPI_FAMILY == WINAPI_FAMILY_APP
34#pragma message("warning: ruapu does not support UWP yet.")
36#warning ruapu does not support UWP yet.
38static int ruapu_detect_isa(
const void *some_inst) {
43static int g_ruapu_sigill_caught = 0;
44static jmp_buf g_ruapu_jmpbuf;
46typedef const void *ruapu_some_inst;
48static LONG CALLBACK ruapu_catch_sigill(
struct _EXCEPTION_POINTERS *ExceptionInfo) {
49 if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_ILLEGAL_INSTRUCTION) {
50 g_ruapu_sigill_caught = 1;
51 longjmp(g_ruapu_jmpbuf, -1);
54 return EXCEPTION_CONTINUE_SEARCH;
57static int ruapu_detect_isa(
const void *some_inst) {
58 g_ruapu_sigill_caught = 0;
60 PVOID eh = AddVectoredExceptionHandler(1, ruapu_catch_sigill);
62 if (setjmp(g_ruapu_jmpbuf) == 0) {
63 ((void (*)()) some_inst)();
66 RemoveVectoredExceptionHandler(eh);
68 return g_ruapu_sigill_caught ? 0 : 1;
72#elif defined __ANDROID__ || defined __linux__ || defined __APPLE__ || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__
75static int g_ruapu_sigill_caught = 0;
76static sigjmp_buf g_ruapu_jmpbuf;
78typedef void (*ruapu_some_inst)();
80static void ruapu_catch_sigill(
int signo, siginfo_t *si,
void *data) {
85 g_ruapu_sigill_caught = 1;
86 siglongjmp(g_ruapu_jmpbuf, -1);
89static int ruapu_detect_isa(ruapu_some_inst some_inst) {
90 g_ruapu_sigill_caught = 0;
92 struct sigaction sa = {0};
93 struct sigaction old_sa;
94 sa.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO;
95 sa.sa_sigaction = ruapu_catch_sigill;
96 sigaction(SIGILL, &sa, &old_sa);
98 if (sigsetjmp(g_ruapu_jmpbuf, 1) == 0) {
102 sigaction(SIGILL, &old_sa,
NULL);
104 return g_ruapu_sigill_caught ? 0 : 1;
107#elif defined __SYTERKIT__
109typedef void (*ruapu_some_inst)();
112static int g_ruapu_sigill_caught = 0;
114void arm32_do_undefined_instruction(
struct arm_regs_t *regs) {
115 g_ruapu_sigill_caught = 1;
119static int ruapu_detect_isa(ruapu_some_inst some_inst) {
121 if (g_ruapu_sigill_caught) {
122 g_ruapu_sigill_caught = 0;
133#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
135#define RUAPU_INSTCODE(isa, ...) __pragma(section(".text")) __declspec(allocate(".text")) static unsigned char ruapu_some_##isa[] = {__VA_ARGS__, 0xc3};
137#define RUAPU_INSTCODE(isa, ...) __attribute__((section(".text"))) static unsigned char ruapu_some_##isa[] = {__VA_ARGS__, 0xc3};
140#elif __aarch64__ || defined(_M_ARM64)
142#define RUAPU_INSTCODE(isa, ...) __pragma(section(".text")) __declspec(allocate(".text")) static unsigned int ruapu_some_##isa[] = {__VA_ARGS__, 0xd65f03c0};
144#define RUAPU_INSTCODE(isa, ...) __attribute__((section(".text"))) static unsigned int ruapu_some_##isa[] = {__VA_ARGS__, 0xd65f03c0};
147#elif __arm__ || defined(_M_ARM)
150#define RUAPU_INSTCODE(isa, ...) __pragma(section(".text")) __declspec(allocate(".text")) static unsigned int ruapu_some_##isa[] = {__VA_ARGS__, 0x4770};
152#define RUAPU_INSTCODE(isa, ...) __attribute__((section(".text"))) static unsigned int ruapu_some_##isa[] = {__VA_ARGS__, 0x4770};
156#define RUAPU_INSTCODE(isa, ...) __pragma(section(".text")) __declspec(allocate(".text")) static unsigned int ruapu_some_##isa[] = {__VA_ARGS__, 0xe12fff1e};
158#define RUAPU_INSTCODE(isa, ...) __attribute__((section(".text"))) static unsigned int ruapu_some_##isa[] = {__VA_ARGS__, 0xe12fff1e};
166#if defined(__i386__) || defined(__x86_64__) || __s390x__
167#define RUAPU_INSTCODE(isa, ...) \
168 static void ruapu_some_##isa() { \
169 asm volatile(".byte " #__VA_ARGS__ \
174#elif __aarch64__ || __arm__ || __mips__ || __riscv || __loongarch__
175#define RUAPU_INSTCODE(isa, ...) \
176 static void ruapu_some_##isa() { \
177 asm volatile(".word " #__VA_ARGS__ \
183#define RUAPU_INSTCODE(isa, ...) \
184 static void ruapu_some_##isa() { \
185 asm volatile(".long " #__VA_ARGS__ \
194#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
195RUAPU_INSTCODE(mmx, 0x0f, 0xdb, 0xc0)
196RUAPU_INSTCODE(sse, 0x0f, 0x54, 0xc0)
197RUAPU_INSTCODE(sse2, 0x66, 0x0f, 0xfe, 0xc0)
198RUAPU_INSTCODE(sse3, 0xf2, 0x0f, 0x7c, 0xc0)
199RUAPU_INSTCODE(ssse3, 0x66, 0x0f, 0x38, 0x06, 0xc0)
200RUAPU_INSTCODE(sse41, 0x66, 0x0f, 0x38, 0x3d, 0xc0)
201RUAPU_INSTCODE(sse42, 0x66, 0x0f, 0x38, 0x37, 0xc0)
202RUAPU_INSTCODE(sse4a, 0x66, 0x0f, 0x79, 0xc0)
203RUAPU_INSTCODE(xop, 0x8f, 0xe8, 0x78, 0xb6, 0xc0, 0x00)
204RUAPU_INSTCODE(avx, 0xc5, 0xfc, 0x54, 0xc0)
205RUAPU_INSTCODE(f16c, 0xc4, 0xe2, 0x7d, 0x13, 0xc0)
206RUAPU_INSTCODE(fma, 0xc4, 0xe2, 0x7d, 0x98, 0xc0)
207RUAPU_INSTCODE(fma4, 0xc4, 0xe3, 0xfd, 0x68, 0xc0, 0x00)
208RUAPU_INSTCODE(avx2, 0xc5, 0xfd, 0xfe, 0xc0)
209RUAPU_INSTCODE(avx512f, 0x62, 0xf1, 0x7c, 0x48, 0x58, 0xc0)
210RUAPU_INSTCODE(avx512bw, 0x62, 0xf1, 0x7d, 0x48, 0xfd, 0xc0)
211RUAPU_INSTCODE(avx512cd, 0x62, 0xf2, 0xfd, 0x48, 0x44, 0xc0)
212RUAPU_INSTCODE(avx512dq, 0x62, 0xf1, 0x7c, 0x48, 0x54, 0xc0)
213RUAPU_INSTCODE(avx512vl, 0x62, 0xf2, 0xfd, 0x28, 0x1f, 0xc0)
214RUAPU_INSTCODE(avx512vnni, 0x62, 0xf2, 0x7d, 0x48, 0x52, 0xc0)
215RUAPU_INSTCODE(avx512bf16, 0x62, 0xf2, 0x7e, 0x48, 0x52, 0xc0)
216RUAPU_INSTCODE(avx512ifma, 0x62, 0xf2, 0xfd, 0x48, 0xb4, 0xc0)
217RUAPU_INSTCODE(avx512vbmi, 0x62, 0xf2, 0x7d, 0x48, 0x75, 0xc0)
218RUAPU_INSTCODE(avx512vbmi2, 0x62, 0xf2, 0x7d, 0x48, 0x71, 0xc0)
219RUAPU_INSTCODE(avx512fp16, 0x62, 0xf6, 0x7d, 0x48, 0x98, 0xc0)
220RUAPU_INSTCODE(avxvnni, 0xc4, 0xe2, 0x7d, 0x52, 0xc0)
221RUAPU_INSTCODE(avxvnniint8, 0xc4, 0xe2, 0x7f, 0x50, 0xc0)
222RUAPU_INSTCODE(avxifma, 0xc4, 0xe2, 0xfd, 0xb4, 0xc0)
224#elif __aarch64__ || defined(_M_ARM64)
225RUAPU_INSTCODE(neon, 0x4e20d400)
226RUAPU_INSTCODE(vfpv4, 0x1f000000)
227RUAPU_INSTCODE(cpuid, 0xd5380000)
228RUAPU_INSTCODE(asimdrdm, 0x6e808400)
229RUAPU_INSTCODE(asimdhp, 0x0e401400)
230RUAPU_INSTCODE(asimddp, 0x4e809400)
231RUAPU_INSTCODE(asimdfhm, 0x4e20ec00)
232RUAPU_INSTCODE(bf16, 0x6e40ec00)
233RUAPU_INSTCODE(i8mm, 0x4e80a400)
234RUAPU_INSTCODE(mte, 0xd96003e0)
235RUAPU_INSTCODE(sve, 0x65608000)
236RUAPU_INSTCODE(sve2, 0x44405000)
237RUAPU_INSTCODE(svebf16, 0x6460e400)
238RUAPU_INSTCODE(svei8mm, 0x45009800)
239RUAPU_INSTCODE(svef32mm, 0x64a0e400)
240RUAPU_INSTCODE(pmull, 0x0e20e000)
241RUAPU_INSTCODE(crc32, 0x1ac04000)
242RUAPU_INSTCODE(aes, 0x4e285800)
243RUAPU_INSTCODE(sha1, 0x5e280800)
244RUAPU_INSTCODE(sha2, 0x5e004000)
245RUAPU_INSTCODE(sha3, 0xce000000)
246RUAPU_INSTCODE(sha512, 0xce608000)
247RUAPU_INSTCODE(sm3, 0xce60c000)
248RUAPU_INSTCODE(sm4, 0xcec08400)
249RUAPU_INSTCODE(amx, 0x00201220)
252#elif __arm__ || defined(_M_ARM)
254RUAPU_INSTCODE(edsp, 0xfb20, 0x0000)
255RUAPU_INSTCODE(neon, 0xef00, 0x0d40)
256RUAPU_INSTCODE(vfpv4, 0xeea0, 0x0a00)
257RUAPU_INSTCODE(idiv, 0x2003, 0xfb90, 0xf0f0)
259RUAPU_INSTCODE(edsp, 0xe7000010)
260RUAPU_INSTCODE(neon, 0xf2000d40)
261RUAPU_INSTCODE(vfpv4, 0xeea00a00)
262RUAPU_INSTCODE(idiv, 0xe3a00003, 0xe710f010)
266RUAPU_INSTCODE(msa, 0x7900001b)
269RUAPU_INSTCODE(vsx, 0x104210c0)
272RUAPU_INSTCODE(zvector, 0xe7, 0x00, 0x02, 0x00, 0x00, 0x8f)
275RUAPU_INSTCODE(lsx, 0x700b0000)
276RUAPU_INSTCODE(lasx, 0x740b0000)
279RUAPU_INSTCODE(i, 0x00a50533)
280RUAPU_INSTCODE(m, 0x00200513, 0x02a50533, 0x02a54533)
281RUAPU_INSTCODE(a, 0x100122af, 0x185122af)
282RUAPU_INSTCODE(f, 0x10a57553)
283RUAPU_INSTCODE(d, 0x12a57553)
284RUAPU_INSTCODE(c, 0x0001952a)
285RUAPU_INSTCODE(zfa, 0xf0108053)
286RUAPU_INSTCODE(zfh, 0x04007053);
287RUAPU_INSTCODE(zfhmin, 0xe4000553)
288RUAPU_INSTCODE(zicsr, 0xc0102573);
289RUAPU_INSTCODE(zifencei, 0x0000100f);
290RUAPU_INSTCODE(zmmul, 0x02a50533)
296struct ruapu_isa_entry {
298 ruapu_some_inst inst;
301#define RUAPU_ISAENTRY(isa) {#isa, (ruapu_some_inst) ruapu_some_##isa},
303struct ruapu_isa_entry g_ruapu_isa_map[] = {
305#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
306 RUAPU_ISAENTRY(mmx) RUAPU_ISAENTRY(sse) RUAPU_ISAENTRY(sse2) RUAPU_ISAENTRY(sse3) RUAPU_ISAENTRY(ssse3) RUAPU_ISAENTRY(sse41) RUAPU_ISAENTRY(sse42) RUAPU_ISAENTRY(sse4a)
307 RUAPU_ISAENTRY(xop) RUAPU_ISAENTRY(avx) RUAPU_ISAENTRY(f16c) RUAPU_ISAENTRY(fma) RUAPU_ISAENTRY(fma4) RUAPU_ISAENTRY(avx2) RUAPU_ISAENTRY(avx512f)
308 RUAPU_ISAENTRY(avx512bw) RUAPU_ISAENTRY(avx512cd) RUAPU_ISAENTRY(avx512dq) RUAPU_ISAENTRY(avx512vl) RUAPU_ISAENTRY(avx512vnni) RUAPU_ISAENTRY(avx512bf16)
309 RUAPU_ISAENTRY(avx512ifma) RUAPU_ISAENTRY(avx512vbmi) RUAPU_ISAENTRY(avx512vbmi2) RUAPU_ISAENTRY(avx512fp16) RUAPU_ISAENTRY(avxvnni)
310 RUAPU_ISAENTRY(avxvnniint8) RUAPU_ISAENTRY(avxifma)
312#elif __aarch64__ || defined(_M_ARM64)
313 RUAPU_ISAENTRY(neon) RUAPU_ISAENTRY(vfpv4) RUAPU_ISAENTRY(cpuid) RUAPU_ISAENTRY(asimdrdm) RUAPU_ISAENTRY(asimdhp) RUAPU_ISAENTRY(asimddp) RUAPU_ISAENTRY(asimdfhm)
314 RUAPU_ISAENTRY(bf16) RUAPU_ISAENTRY(i8mm) RUAPU_ISAENTRY(mte) RUAPU_ISAENTRY(sve) RUAPU_ISAENTRY(sve2) RUAPU_ISAENTRY(svebf16) RUAPU_ISAENTRY(svei8mm)
315 RUAPU_ISAENTRY(svef32mm) RUAPU_ISAENTRY(pmull) RUAPU_ISAENTRY(crc32) RUAPU_ISAENTRY(aes) RUAPU_ISAENTRY(sha1) RUAPU_ISAENTRY(sha2) RUAPU_ISAENTRY(sha3)
316 RUAPU_ISAENTRY(sha512) RUAPU_ISAENTRY(sm3) RUAPU_ISAENTRY(sm4) RUAPU_ISAENTRY(amx)
318#elif __arm__ || defined(_M_ARM)
319 RUAPU_ISAENTRY(edsp) RUAPU_ISAENTRY(neon) RUAPU_ISAENTRY(vfpv4) RUAPU_ISAENTRY(idiv)
328 RUAPU_ISAENTRY(zvector)
331 RUAPU_ISAENTRY(lsx) RUAPU_ISAENTRY(lasx)
334 RUAPU_ISAENTRY(i) RUAPU_ISAENTRY(m) RUAPU_ISAENTRY(a) RUAPU_ISAENTRY(f) RUAPU_ISAENTRY(d) RUAPU_ISAENTRY(c) RUAPU_ISAENTRY(zfa) RUAPU_ISAENTRY(zfh) RUAPU_ISAENTRY(zfhmin)
335 RUAPU_ISAENTRY(zicsr) RUAPU_ISAENTRY(zifencei) RUAPU_ISAENTRY(zmmul)
338 RUAPU_ISAENTRY(orbis32) RUAPU_ISAENTRY(orbis64) RUAPU_ISAENTRY(orfpx32) RUAPU_ISAENTRY(orfpx64) RUAPU_ISAENTRY(orvdx64)
345const char *g_ruapu_isa_supported[
sizeof(g_ruapu_isa_map) /
sizeof(g_ruapu_isa_map[0]) + 1] = {0};
347#if defined __openrisc__
348static void ruapu_detect_openrisc_isa() {
351 asm volatile(
"l.mfspr %0, r0, %1"
355 for (
size_t i = 0; i <
sizeof(g_ruapu_isa_map) /
sizeof(g_ruapu_isa_map[0]); i++) {
356 int capable = ((
value) >> (5 + i)) & 0x1;
358 g_ruapu_isa_supported[j] = g_ruapu_isa_map[i].isa;
362 g_ruapu_isa_supported[j] = 0;
367#if defined _WIN32 || defined __ANDROID__ || defined __linux__ || defined __APPLE__ || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined __SYTERKIT__
369 for (
size_t i = 0; i <
sizeof(g_ruapu_isa_map) /
sizeof(g_ruapu_isa_map[0]); i++) {
370 int capable = ruapu_detect_isa(g_ruapu_isa_map[i].inst);
372 g_ruapu_isa_supported[j] = g_ruapu_isa_map[i].isa;
376 g_ruapu_isa_supported[j] = 0;
377#elif defined __openrisc__
378 ruapu_detect_openrisc_isa();
383#warning ruapu does not support your baremetal os yet
388 const char *
const *isa_supported = g_ruapu_isa_supported;
389 while (*isa_supported) {
390 if (
strcmp(*isa_supported, isa) == 0)
400 return g_ruapu_isa_supported;
#define NULL
Definition stddef.h:7
u32_t uint32_t
Definition stdint.h:13
u16_t uint16_t
Definition stdint.h:10
static uint8_t value
Definition io.h:144
int strcmp(const char *p1, const char *p2) __attribute__((optimize("O0")))
Compares the string pointed to by 'p1' to the string pointed to by 'p2'.
Definition string.c:46
const char *const * ruapu_rua()
int ruapu_supports(const char *isa)
ARM32 register structure.
Definition mmu.h:28
uint32_t pc
Program counter (R15)
Definition mmu.h:34