Commit | Line | Data |
---|---|---|
08e2be29 MW |
1 | /* -*-c-*- |
2 | * | |
3 | * CPU-specific dispatch | |
4 | * | |
5 | * (c) 2015 Straylight/Edgeware | |
6 | */ | |
7 | ||
8 | /*----- Licensing notice --------------------------------------------------* | |
9 | * | |
10 | * This file is part of Catacomb. | |
11 | * | |
12 | * Catacomb is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU Library General Public License as | |
14 | * published by the Free Software Foundation; either version 2 of the | |
15 | * License, or (at your option) any later version. | |
16 | * | |
17 | * Catacomb is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU Library General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Library General Public | |
23 | * License along with Catacomb; if not, write to the Free | |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
25 | * MA 02111-1307, USA. | |
26 | */ | |
27 | ||
28 | /*----- Header files ------------------------------------------------------*/ | |
29 | ||
30 | #include "config.h" | |
31 | ||
32 | #include <ctype.h> | |
33 | #include <stdlib.h> | |
34 | #include <string.h> | |
35 | ||
36 | #include <mLib/macros.h> | |
37 | ||
38 | #include "dispatch.h" | |
39 | ||
40 | /*----- Main code ---------------------------------------------------------*/ | |
41 | ||
42 | #ifdef CPUFAM_X86 | |
43 | ||
44 | #define EFLAGS_ID (1u << 21) | |
45 | #define CPUID1D_SSE2 (1u << 26) | |
46 | ||
47 | struct cpuid { unsigned a, b, c, d; }; | |
48 | ||
49 | /* --- @cpuid@ --- * | |
50 | * | |
51 | * Arguments: @struct cpuid *cc@ = where to write the result | |
52 | * @unsigned a, c@ = EAX and ECX registers to set | |
53 | * | |
54 | * Returns: --- | |
55 | * | |
56 | * Use: Minimal C wrapper around the x86 `CPUID' instruction. Checks | |
57 | * that the instruction is actually available before invoking | |
58 | * it; fills the output structure with zero if it's not going to | |
59 | * work. | |
60 | */ | |
61 | ||
62 | #ifdef __GNUC__ | |
63 | static __inline__ unsigned getflags(void) | |
64 | { unsigned f; __asm__ ("pushf; popl %0" : "=g" (f)); return (f); } | |
65 | static __inline__ unsigned setflags(unsigned f) | |
66 | { | |
67 | unsigned ff; | |
68 | __asm__ ("pushf; pushl %1; popf; pushf; popl %0; popf" | |
69 | : "=g" (ff) | |
70 | : "g" (f)); | |
71 | return (ff); | |
72 | } | |
73 | #endif | |
74 | ||
75 | static void cpuid(struct cpuid *cc, unsigned a, unsigned c) | |
76 | { | |
77 | #ifdef __GNUC__ | |
78 | unsigned f; | |
79 | #endif | |
80 | ||
81 | cc->a = cc->b = cc->c = cc->d = 0; | |
82 | ||
83 | #ifdef __GNUC__ | |
84 | /* Stupid dance to detect whether the CPUID instruction is available. */ | |
85 | f = getflags(); | |
86 | if (!(setflags(f | EFLAGS_ID) & EFLAGS_ID)) return; | |
87 | if ( setflags(f & ~EFLAGS_ID) & EFLAGS_ID ) return; | |
88 | setflags(f); | |
89 | ||
90 | /* Alas, EBX is magical in PIC code, so abuse ESI instead. This isn't | |
91 | * pretty, but it works. | |
92 | */ | |
93 | __asm__ ("pushl %%ebx; cpuid; movl %%ebx, %%esi; popl %%ebx" | |
94 | : "=a" (cc->a), "=S" (cc->b), "=c" (cc->c), "=d" (cc->d) | |
95 | : "a" (a) , "c" (c)); | |
96 | #endif | |
97 | } | |
98 | ||
99 | static unsigned cpuid_maxleaf(void) | |
100 | { struct cpuid c; cpuid(&c, 0, 0); return (c.a); } | |
101 | ||
102 | static int cpuid_features_p(unsigned dbits, unsigned cbits) | |
103 | { | |
104 | struct cpuid c; | |
105 | if (cpuid_maxleaf() < 1) return (0); | |
106 | cpuid(&c, 1, 0); | |
107 | return ((c.d & dbits) == dbits && (c.c & cbits) == cbits); | |
108 | } | |
109 | ||
110 | #endif | |
111 | ||
112 | /* --- @check_env@ --- * | |
113 | * | |
114 | * Arguments: @const char *ftok@ = feature token | |
115 | * | |
116 | * Returns: Zero if the feature is forced off; positive if it's forced | |
117 | * on; negative if the user hasn't decided. | |
118 | * | |
119 | * Use: Checks the environment variable `CATACOMB_CPUFEAT' for the | |
120 | * feature token @ftok@. The variable, if it exists, should be | |
121 | * a space-separated sequence of `+tok' and `-tok' items. These | |
122 | * tokens may end in `*', which matches any suffix. | |
123 | */ | |
124 | ||
125 | static int IGNORABLE check_env(const char *ftok) | |
126 | { | |
127 | const char *p, *q, *pp; | |
128 | int d; | |
129 | ||
130 | p = getenv("CATACOMB_CPUFEAT"); | |
131 | if (!p) return (-1); | |
132 | ||
133 | for (;;) { | |
134 | while (isspace((unsigned char)*p)) p++; | |
135 | if (!*p) return (-1); | |
136 | switch (*p) { | |
137 | case '+': d = +1; p++; break; | |
138 | case '-': d = 0; p++; break; | |
139 | default: d = -1; break; | |
140 | } | |
141 | for (q = p; *q && !isspace((unsigned char)*q); q++); | |
142 | if (d >= 0) { | |
143 | for (pp = ftok; p < q && *pp && *p == *pp; p++, pp++); | |
144 | if ((p == q && !*pp) || (*p == '*' && p + 1 == q)) return (d); | |
145 | } | |
146 | p = q; | |
147 | } | |
148 | return (-1); | |
149 | } | |
150 | ||
151 | /* --- @cpu_feature_p@ --- * | |
152 | * | |
153 | * Arguments: @unsigned feat@ = a @CPUFEAT_...@ code | |
154 | * | |
155 | * Returns: Nonzero if the feature is available. | |
156 | */ | |
157 | ||
158 | #include <stdio.h> | |
159 | ||
160 | int cpu_feature_p(int feat) | |
161 | { | |
162 | int IGNORABLE f; | |
163 | IGNORE(f); | |
164 | #define CHECK_ENV(ftok) \ | |
165 | do { if ((f = check_env(ftok)) >= 0) return (f); } while (0) | |
166 | ||
167 | switch (feat) { | |
168 | #ifdef CPUFAM_X86 | |
169 | case CPUFEAT_X86_SSE2: { | |
170 | CHECK_ENV("x86:sse2"); | |
171 | return (cpuid_features_p(CPUID1D_SSE2, 0)); | |
172 | } | |
173 | #endif | |
174 | default: | |
175 | return (0); | |
176 | } | |
177 | #undef CHECK_ENV | |
178 | } | |
179 | ||
180 | /*----- That's all, folks -------------------------------------------------*/ |