summaryrefslogtreecommitdiff
path: root/gpxe/src/arch/i386/core/cpu.c
blob: c24fa4e694e1e222212ac0ef8e2b53717cfdb6df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdint.h>
#include <string.h>
#include <cpu.h>

/** @file
 *
 * CPU identification
 *
 */

/**
 * Test to see if CPU flag is changeable
 *
 * @v flag		Flag to test
 * @ret can_change	Flag is changeable
 */
static inline int flag_is_changeable ( unsigned int flag ) {
	uint32_t f1, f2;

	__asm__ ( "pushfl\n\t"
		  "pushfl\n\t"
		  "popl %0\n\t"
		  "movl %0,%1\n\t"
		  "xorl %2,%0\n\t"
		  "pushl %0\n\t"
		  "popfl\n\t"
		  "pushfl\n\t"
		  "popl %0\n\t"
		  "popfl\n\t"
		  : "=&r" ( f1 ), "=&r" ( f2 )
		  : "ir" ( flag ) );

	return ( ( ( f1 ^ f2 ) & flag ) != 0 );
}

/**
 * Get CPU information
 *
 * @v cpu		CPU information structure to fill in
 */
void get_cpuinfo ( struct cpuinfo_x86 *cpu ) {
	unsigned int cpuid_level;
	unsigned int cpuid_extlevel;
	unsigned int discard_1, discard_2, discard_3;

	memset ( cpu, 0, sizeof ( *cpu ) );

	/* Check for CPUID instruction */
	if ( ! flag_is_changeable ( X86_EFLAGS_ID ) ) {
		DBG ( "CPUID not supported\n" );
		return;
	}

	/* Get features, if present */
	cpuid ( 0x00000000, &cpuid_level, &discard_1,
		&discard_2, &discard_3 );
	if ( cpuid_level >= 0x00000001 ) {
		cpuid ( 0x00000001, &discard_1, &discard_2,
			&discard_3, &cpu->features );
	} else {
		DBG ( "CPUID cannot return capabilities\n" );
	}

	/* Get 64-bit features, if present */
	cpuid ( 0x80000000, &cpuid_extlevel, &discard_1,
		&discard_2, &discard_3 );
	if ( ( cpuid_extlevel & 0xffff0000 ) == 0x80000000 ) {
		if ( cpuid_extlevel >= 0x80000001 ) {
			cpuid ( 0x80000001, &discard_1, &discard_2,
				&discard_3, &cpu->amd_features );
		}
	}
}