cynix

x86 UNIX-like OS
git clone git://git.2f30.org/cynix
Log | Files | Refs | README | LICENSE

cpuid.c (2270B)


      1 /*
      2  *  core/cpuid.c
      3  *
      4  *  Copyright (C) 2010 stateless
      5  */
      6 
      7 #include <x86.h>
      8 #include <stdint.h>
      9 
     10 #define cpuid(code, eax, ebx, ecx, edx) \
     11 	({ \
     12 		uint32_t __code = code; \
     13 		asm volatile ("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "0"(__code)); \
     14 	})
     15 
     16 static const char *intel_brands[] = {
     17 	"Brand not supported",
     18 	"Intel(R) Celeron(R) processor",
     19 	"Intel(R) Pentium(R) III processor",
     20 	"Intel(R) Pentium(R) III Xeon(R) processor",
     21 	"Intel(R) Pentium(R) III processor",
     22 	"Reserved",
     23 	"Mobile Intel(R) Pentium(R) III processor-M",
     24 	"Mobile Intel(R) Celeron(R) processor",
     25 	"Intel(R) Pentium(R) 4 processor",
     26 	"Intel(R) Pentium(R) 4 processor",
     27 	"Intel(R) Celeron(R) processor",
     28 	"Intel(R) Xeon(R) Processor",
     29 	"Intel(R) Xeon(R) processor MP",
     30 	"Reserved",
     31 	"Mobile Intel(R) Pentium(R) 4 processor-M",
     32 	"Mobile Intel(R) Pentium(R) Celeron(R) processor",
     33 	"Reserved",
     34 	"Mobile Genuine Intel(R) processor",
     35 	"Intel(R) Celeron(R) M processor",
     36 	"Mobile Intel(R) Celeron(R) processor",
     37 	"Intel(R) Celeron(R) processor",
     38 	"Mobile Geniune Intel(R) processor",
     39 	"Intel(R) Pentium(R) M processor",
     40 	"Mobile Intel(R) Celeron(R) processor"
     41 };
     42 
     43 static void
     44 print_vendor(uint32_t str[4])
     45 {
     46 	printf("vendor_id: ");
     47 	printf("%c%c%c%c", (str[1] & 0xff), (str[1] >> 8) & 0xff,
     48 	       (str[1] >> 16) & 0xff, (str[1] >> 24) & 0xff);
     49 	printf("%c%c%c%c", (str[3] & 0xff), (str[3] >> 8) & 0xff,
     50 	       (str[3] >> 16) & 0xff, (str[3] >> 24) & 0xff);
     51 	printf("%c%c%c%c", (str[2] & 0xff), (str[2] >> 8) & 0xff,
     52 	       (str[2] >> 16) & 0xff, (str[2] >> 24) & 0xff);
     53 	putchar('\n');
     54 }
     55 
     56 void
     57 do_cpuid(void)
     58 {
     59 	uint32_t output[4], family, brand, a, b, d;
     60 	uint32_t unused;
     61 
     62 	cpuid(0, output[0], output[1], output[2], output[3]);
     63 	printf("processor: 0\n");
     64 	print_vendor(output);
     65 	if (output[1] != 0x756e6547) { /* not an Intel cpu */
     66 		info("can't get any more info out of this cpu!\n");
     67 		return;
     68 	}
     69 
     70 	cpuid(1, a, b, unused, d);
     71 	family = (a >> 8) & 0xf;
     72 	printf("cpu_family: %s\n",
     73 	       (family == 4) ? "i486" :
     74 	       (family == 5) ? "Pentium" :
     75 	       (family == 6) ? "Pentium Pro" : "Unknown");
     76 	brand = (b & 0xff);
     77 	if (brand < sizeof(intel_brands) / sizeof(intel_brands[0]))
     78 		printf("model_name: %s\n", intel_brands[brand]);
     79 	else
     80 		printf("model_name: Unknown\n");
     81 }
     82