#include #include #include #if DEBUG # include #endif #if defined(__sparc__) char *x86cpuid(void) { static char *str = "not implemented"; return str; } #else char *x86cpuid(void) { unsigned long x[4]; unsigned long y[4]; register char *str; x[0] = x[1] = x[2] = x[3] = 0; __asm__ volatile(".byte 15;.byte 162" : "=a"(x[0]),"=b"(x[1]),"=c"(x[3]),"=d"(x[2]) : "0"(0)); if (! x[0]) return 0; __asm__ volatile(".byte 15;.byte 162" : "=a"(y[0]),"=b"(y[1]),"=c"(y[2]),"=d"(y[3]) : "0"(1)); str = (char *) malloc(sizeof(char) * (3 * 4 + 2 * 9 + 1)); #if DEBUG fprintf(stderr, "%s(): %d bytes allocated using malloc()\n", __FUNCTION__, (3 * 4 + 2 * 9 + 1)); #endif if (str != NULL) { register int i, j, k; register char c; for (k = 0, i = 1; i < 4; ++i) for (j = 0; j < 4; ++j, k++) { c = x[i] >> (8 * j); if (c < 32) c = 32; if (c > 126) c = 126; str[k] = c; } sprintf(str + k, "-%08lx-%08lx", y[0], y[3]); #if DEBUG fprintf(stderr, "%s(): real string size: %d\n", __FUNCTION__, strlen(str)); #endif } return str; } #endif /* #if defined(__sparc__) */ #if defined(SELF) || defined(SELFTEST) || defined(SELF_X86CPUID) int main(void) { register char *cpuid; if ((cpuid = x86cpuid()) == NULL) { puts("memory allocation error"); return 1; } puts(cpuid); free(cpuid); return 0; } #endif