| 1 | /* $Id: cpuid.c 2292 2005-08-21 03:17:31Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | *
|
|---|
| 4 | * The start of a cpuid dumper.
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2005 knut st. osmundsen <bird@anduin.net>
|
|---|
| 7 | *
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of InnoTek LIBC.
|
|---|
| 10 | *
|
|---|
| 11 | * InnoTek LIBC is free software; you can redistribute it and/or modify
|
|---|
| 12 | * it under the terms of the GNU General Public License as published by
|
|---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
|---|
| 14 | * (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * InnoTek LIBC is distributed in the hope that it will be useful,
|
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 19 | * GNU General Public License for more details.
|
|---|
| 20 | *
|
|---|
| 21 | * You should have received a copy of the GNU General Public License
|
|---|
| 22 | * along with InnoTek LIBC; if not, write to the Free Software
|
|---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 24 | *
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | #include <stdio.h>
|
|---|
| 28 | #include <stdint.h>
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * Checks if the current CPU supports CPUID.
|
|---|
| 32 | *
|
|---|
| 33 | * @returns 1 if CPUID is supported, 0 if it doesn't.
|
|---|
| 34 | */
|
|---|
| 35 | static inline int HasCpuId(void)
|
|---|
| 36 | {
|
|---|
| 37 | int fRet = 0;
|
|---|
| 38 | uint32_t u1;
|
|---|
| 39 | uint32_t u2;
|
|---|
| 40 | __asm__ ("pushf\n\t"
|
|---|
| 41 | "pop %1\n\t"
|
|---|
| 42 | "mov %1, %2\n\t"
|
|---|
| 43 | "xorl $0x200000, %1\n\t"
|
|---|
| 44 | "push %1\n\t"
|
|---|
| 45 | "popf\n\t"
|
|---|
| 46 | "pushf\n\t"
|
|---|
| 47 | "pop %1\n\t"
|
|---|
| 48 | "cmpl %1, %2\n\t"
|
|---|
| 49 | "setne %0\n\t"
|
|---|
| 50 | "push %2\n\t"
|
|---|
| 51 | "popf\n\t"
|
|---|
| 52 | : "=m" (fRet), "=r" (u1), "=r" (u2));
|
|---|
| 53 | return fRet;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | #define BIT(n) (1 << n)
|
|---|
| 58 | #define CPUID(n) do { __asm__ __volatile__ ("cpuid" : "=a" (eax), "=d" (edx), "=c" (ecx), "=b" (ebx) : "0" (n)); \
|
|---|
| 59 | printf("%#010x: edx=%08x ecx=%08x eax=%08x ebx=%08x\n", (n), edx, ecx, eax, ebx); } while (0)
|
|---|
| 60 | #define CPUIDSZ(n, psz) \
|
|---|
| 61 | do { __asm__ __volatile__ ("cpuid" : "=a" (eax), "=d" (edx), "=c" (ecx), "=b" (ebx) : "0" (n)); \
|
|---|
| 62 | ((uint32_t *)(psz))[0] = eax; ((uint32_t *)(psz))[1] = ebx; \
|
|---|
| 63 | ((uint32_t *)(psz))[2] = ecx; ((uint32_t *)(psz))[3] = edx; } while (0)
|
|---|
| 64 | int main()
|
|---|
| 65 | {
|
|---|
| 66 | enum { MANU_AMD, MANU_INTEL, MANU_OTHER }
|
|---|
| 67 | enmManufacturer = MANU_OTHER;
|
|---|
| 68 | unsigned c;
|
|---|
| 69 | uint32_t eax, edx, ecx, ebx;
|
|---|
| 70 |
|
|---|
| 71 | if (!HasCpuId())
|
|---|
| 72 | {
|
|---|
| 73 | printf("This processor doesn't support the CPUID instruction!\n");
|
|---|
| 74 | return 0;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | CPUID(0);
|
|---|
| 78 | c = eax;
|
|---|
| 79 | printf("Name: %.4s%.4s%.4s\n"
|
|---|
| 80 | "Supports: 0-%u\n", &ebx, &edx, &ecx, eax);
|
|---|
| 81 | if (edx == 0x69746e65 && ecx == 0x444d4163 && ebx == 0x68747541)
|
|---|
| 82 | enmManufacturer = MANU_AMD;
|
|---|
| 83 | else if (edx == 0x6c65746e && ecx == 0x49656e69 && ebx == 0x756e6547)
|
|---|
| 84 | enmManufacturer = MANU_INTEL;
|
|---|
| 85 |
|
|---|
| 86 | if (c >= 1)
|
|---|
| 87 | {
|
|---|
| 88 | CPUID(1);
|
|---|
| 89 | printf("Family: %d\n"
|
|---|
| 90 | "Model: %d\n"
|
|---|
| 91 | "Stepping: %d\n",
|
|---|
| 92 | (eax >> 8) & 7, (eax >> 4) & 7, eax & 7);
|
|---|
| 93 | printf("Features: ");
|
|---|
| 94 | if (edx & BIT(0)) printf(" FPU");
|
|---|
| 95 | if (edx & BIT(1)) printf(" VME");
|
|---|
| 96 | if (edx & BIT(2)) printf(" DE");
|
|---|
| 97 | if (edx & BIT(3)) printf(" PSE");
|
|---|
| 98 | if (edx & BIT(4)) printf(" TSC");
|
|---|
| 99 | if (edx & BIT(5)) printf(" MSR");
|
|---|
| 100 | if (edx & BIT(6)) printf(" PAE");
|
|---|
| 101 | if (edx & BIT(7)) printf(" MCE");
|
|---|
| 102 | if (edx & BIT(8)) printf(" CX8");
|
|---|
| 103 | if (edx & BIT(9)) printf(" APIC");
|
|---|
| 104 | if (edx & BIT(10)) printf(" 10");
|
|---|
| 105 | if (edx & BIT(11)) printf(" SEP");
|
|---|
| 106 | if (edx & BIT(12)) printf(" MTRR");
|
|---|
| 107 | if (edx & BIT(13)) printf(" PGE");
|
|---|
| 108 | if (edx & BIT(14)) printf(" MCA");
|
|---|
| 109 | if (edx & BIT(15)) printf(" CMOV");
|
|---|
| 110 | if (edx & BIT(16)) printf(" PAT");
|
|---|
| 111 | if (edx & BIT(17)) printf(" PSE36");
|
|---|
| 112 | if (edx & BIT(18)) printf(" PSN");
|
|---|
| 113 | if (edx & BIT(19)) printf(" CLFSH");
|
|---|
| 114 | if (edx & BIT(20)) printf(" 20");
|
|---|
| 115 | if (edx & BIT(21)) printf(" DS");
|
|---|
| 116 | if (edx & BIT(22)) printf(" ACPI");
|
|---|
| 117 | if (edx & BIT(23)) printf(" MMX");
|
|---|
| 118 | if (edx & BIT(24)) printf(" FXSR");
|
|---|
| 119 | if (edx & BIT(25)) printf(" SSE");
|
|---|
| 120 | if (edx & BIT(26)) printf(" SSE2");
|
|---|
| 121 | if (edx & BIT(27)) printf(" SS");
|
|---|
| 122 | if (edx & BIT(28)) printf(" 28");
|
|---|
| 123 | if (edx & BIT(29)) printf(" 29");
|
|---|
| 124 | if (edx & BIT(30)) printf(" 30");
|
|---|
| 125 | if (edx & BIT(31)) printf(" 31");
|
|---|
| 126 | printf("\n");
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | //CPUID(2);
|
|---|
| 130 |
|
|---|
| 131 | /*
|
|---|
| 132 | * AMD
|
|---|
| 133 | */
|
|---|
| 134 | if (enmManufacturer == MANU_AMD)
|
|---|
| 135 | {
|
|---|
| 136 | CPUID(0x80000000);
|
|---|
| 137 | c = eax;
|
|---|
| 138 | printf("AMD Name: %.4s%.4s%.4s\n"
|
|---|
| 139 | "AMD Supports: 0x80000000-%#010x\n", &ebx, &edx, &ecx, eax);
|
|---|
| 140 |
|
|---|
| 141 | if (c >= 0x80000001)
|
|---|
| 142 | {
|
|---|
| 143 | CPUID(0x80000001);
|
|---|
| 144 | printf("AMD Family: %d\n"
|
|---|
| 145 | "AMD Model: %d\n"
|
|---|
| 146 | "AMD Stepping: %d\n",
|
|---|
| 147 | (eax >> 8) & 7, (eax >> 4) & 7, eax & 7);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | char szString[4*4*3+1] = {0};
|
|---|
| 151 | if (c >= 0x80000002)
|
|---|
| 152 | CPUIDSZ(0x80000002, &szString[0]);
|
|---|
| 153 | if (c >= 0x80000003)
|
|---|
| 154 | CPUIDSZ(0x80000003, &szString[16]);
|
|---|
| 155 | if (c >= 0x80000004)
|
|---|
| 156 | CPUIDSZ(0x80000004, &szString[32]);
|
|---|
| 157 | if (c >= 0x80000002)
|
|---|
| 158 | printf("AMD Model: %s\n", szString);
|
|---|
| 159 |
|
|---|
| 160 | if (c >= 0x80000005)
|
|---|
| 161 | {
|
|---|
| 162 | CPUID(0x80000005);
|
|---|
| 163 | }
|
|---|
| 164 | if (c >= 0x80000006)
|
|---|
| 165 | {
|
|---|
| 166 | CPUID(0x80000006);
|
|---|
| 167 | }
|
|---|
| 168 | if (c >= 0x80000007)
|
|---|
| 169 | {
|
|---|
| 170 | CPUID(0x80000007);
|
|---|
| 171 | }
|
|---|
| 172 | if (c >= 0x80000008)
|
|---|
| 173 | {
|
|---|
| 174 | CPUID(0x80000008);
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | return 0;
|
|---|
| 179 | }
|
|---|