forked from dankamongmen/x86info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mptable.c
441 lines (359 loc) · 9.56 KB
/
mptable.c
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* Copyright (c) 1996, by Steve Passe
* All rights reserved.
*
* hacked to make it work in userspace Linux by Ingo Molnar, same copyright
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the developer may NOT be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "mptable.h"
#include <x86info.h>
/* EBDA is @ 40:0e in real-mode terms */
#define EBDA_POINTER 0x040e /* location of EBDA pointer */
/* CMOS 'top of mem' is @ 40:13 in real-mode terms */
#define TOPOFMEM_POINTER 0x0413 /* BIOS: base memory size */
#define DEFAULT_TOPOFMEM 0xa0000
#define BIOS_BASE 0xf0000
#define BIOS_BASE2 0xe0000
#define BIOS_SIZE 0x10000
#define ONE_KBYTE 1024
#define GROPE_AREA1 0x80000
#define GROPE_AREA2 0x90000
#define GROPE_SIZE 0x10000
#define PROCENTRY_FLAG_EN 0x01
#define PROCENTRY_FLAG_BP 0x02
/* global data */
static int pfd; /* physical /dev/mem fd */
static int busses[16];
static int apics[16];
static int ncpu;
static int nbus;
static int napic;
static int nintr;
static int silent;
/* MP Floating Pointer Structure */
struct mpfps {
char signature[4];
u32 pap;
u8 length;
u8 spec_rev;
u8 checksum;
u8 mpfb1;
u8 mpfb2;
u8 mpfb3;
u8 mpfb4;
u8 mpfb5;
};
/* MP Configuration Table Header */
struct mpcth {
char signature[4];
u16 base_table_length;
u8 spec_rev;
u8 checksum;
u8 oem_id[8];
u8 product_id[12];
u32 oem_table_pointer;
u16 oem_table_size;
u16 entry_count;
u32 apic_address;
u16 extended_table_length;
u8 extended_table_checksum;
u8 reserved;
};
struct ProcEntry {
u8 type;
u8 apicID;
u8 apicVersion;
u8 cpuFlags;
u32 cpuSignature;
u32 featureFlags;
u32 reserved1;
u32 reserved2;
};
static void seekEntry(unsigned long addr)
{
if (lseek(pfd, (off_t)addr, SEEK_SET) < 0) {
perror("/dev/mem seek");
exit(EXIT_FAILURE);
}
}
static int readEntry(void* entry, int size)
{
if (read(pfd, entry, size) != size) {
perror("readEntry");
return -1;
}
return 0;
}
static int readType(void)
{
unsigned char type;
if (read(pfd, &type, sizeof(unsigned char)) != sizeof(unsigned char)) {
perror("type read");
exit(EXIT_FAILURE);
}
if (lseek(pfd, -1, SEEK_CUR) < 0) {
perror("type seek");
exit(EXIT_FAILURE);
}
return (int)type;
}
static void processorEntry(void)
{
struct ProcEntry entry;
/* read it into local memory */
if (readEntry(&entry, sizeof(entry)) < 0) {
printf("Error reading processor entry\n");
exit(EXIT_FAILURE);
}
/* count it */
++ncpu;
if (!silent) {
int t, family, model;
printf("#\t%2d", (int) entry.apicID);
printf("\t 0x%2x", (unsigned int) entry.apicVersion);
printf("\t %s, %s",
(entry.cpuFlags & PROCENTRY_FLAG_BP) ? "BSP" : "AP",
(entry.cpuFlags & PROCENTRY_FLAG_EN) ? "usable" : "unusable");
t = (int) entry.cpuSignature;
family = (t >> 8) & 0xf;
model = (t >> 4) & 0xf;
if (family == 0xf) {
family += (t >> 20) & 0xff;
model += (t >> 12) & 0xf0;
}
printf("\t %d\t %d\t %d", family, model, t & 0xf);
printf("\t 0x%04x\n", entry.featureFlags);
}
}
static int MPConfigTableHeader(u32 pap)
{
unsigned long paddr;
struct mpcth cth;
int x;
int count, c;
if (pap == 0) {
printf("MP Configuration Table Header MISSING!\n");
return SMP_NO;
}
/* convert physical address to virtual address */
paddr = (unsigned long) pap;
/* read in cth structure */
seekEntry(paddr);
if(readEntry(&cth, sizeof(cth))) {
printf("error reading MP Config table header structure\n");
exit(EXIT_FAILURE);
}
count = cth.entry_count;
/* initialize tables */
for (x = 0; x < 16; ++x)
busses[ x ] = apics[ x ] = 0xff;
ncpu = 0;
nbus = 0;
napic = 0;
nintr = 0;
/* process all the CPUs */
if (!silent)
printf("MP Table:\n#\tAPIC ID\tVersion\tState\t\tFamily\tModel\tStep\tFlags\n");
for (c = count; c; c--) {
if (readType() == 0)
processorEntry();
}
if (!silent)
printf("\n");
return SMP_YES;
}
/*
* set PHYSICAL address of MP floating pointer structure
*/
#define NEXT(X) ((X) += 4)
static int apic_probe(unsigned long* paddr)
{
unsigned int x;
u16 segment;
unsigned long target;
unsigned int buffer[BIOS_SIZE];
const char MP_SIG[]="_MP_";
/* search Extended Bios Data Area, if present */
seekEntry((unsigned long)EBDA_POINTER);
if (readEntry(&segment, 2)) {
printf("error reading EBDA pointer\n");
exit(EXIT_FAILURE);
}
if (debug)
printf("\nEBDA points to: %x\n", segment);
if (segment) { /* search EBDA */
target = (unsigned long)segment << 4;
seekEntry(target);
if (debug)
printf("EBDA segment ptr: %lx\n", target);
if (readEntry(buffer, ONE_KBYTE)) {
printf("error reading 1K from %p\n", (void *)target);
exit(EXIT_FAILURE);
}
for (x = 0; x < ONE_KBYTE / 4; NEXT(x)) {
if (!strncmp((char *)&buffer[x], MP_SIG, 4)) {
*paddr = (x*4) + target;
return 1;
}
}
}
/* read CMOS for real top of mem */
seekEntry((unsigned long)TOPOFMEM_POINTER);
if (readEntry(&segment, 2)) {
printf("error reading CMOS for real top of mem (%p)\n", (void *) TOPOFMEM_POINTER);
exit(EXIT_FAILURE);
}
--segment; /* less ONE_KBYTE */
target = segment * 1024;
seekEntry(target);
if (readEntry(buffer, ONE_KBYTE)) {
printf("error reading 1KB from %p\n", (void *)target);
exit(EXIT_FAILURE);
}
for (x = 0; x < ONE_KBYTE/4; NEXT(x)) {
if (!strncmp((char *)&buffer[x], MP_SIG, 4)) {
*paddr = (x*4) + target;
return 2;
}
}
/* we don't necessarily believe CMOS, check base of the last 1K of 640K */
if (target != (DEFAULT_TOPOFMEM - 1024)) {
target = (DEFAULT_TOPOFMEM - 1024);
seekEntry(target);
if (readEntry(buffer, ONE_KBYTE)) {
printf("error reading DEFAULT_TOPOFMEM - 1024 from %p\n", (void *) target);
exit(EXIT_FAILURE);
}
for (x = 0; x < ONE_KBYTE/4; NEXT(x)) {
if (!strncmp((char *)&buffer[x], MP_SIG, 4)) {
*paddr = (x*4) + target;
return 3;
}
}
}
/* search the BIOS */
seekEntry(BIOS_BASE);
if (readEntry(buffer, BIOS_SIZE)) {
printf("error reading BIOS_BASE from %p\n", (void *)BIOS_BASE);
exit(EXIT_FAILURE);
}
for (x = 0; x < BIOS_SIZE/4; NEXT(x)) {
if (!strncmp((char *)&buffer[x], MP_SIG, 4)) {
*paddr = (x*4) + BIOS_BASE;
return 4;
}
}
/* search the extended BIOS */
seekEntry(BIOS_BASE2);
if (readEntry(buffer, BIOS_SIZE)) {
printf("error reading BIOS_BASE2 from %p\n", (void *)BIOS_BASE2);
exit(EXIT_FAILURE);
}
for (x = 0; x < BIOS_SIZE/4; NEXT(x)) {
if (!strncmp((char *)&buffer[x], MP_SIG, 4)) {
*paddr = (x*4) + BIOS_BASE2;
return 4;
}
}
/* search additional memory */
target = GROPE_AREA1;
seekEntry(target);
if (readEntry(buffer, GROPE_SIZE)) {
printf("error reading GROPE_AREA1 from %p\n", (void *)target);
exit(EXIT_FAILURE);
}
for (x = 0; x < GROPE_SIZE/4; NEXT(x)) {
if (!strncmp((char *)&buffer[x], MP_SIG, 4)) {
*paddr = (x*4) + GROPE_AREA1;
return 5;
}
}
target = GROPE_AREA2;
seekEntry(target);
if (readEntry(buffer, GROPE_SIZE)) {
printf("error reading GROPE_AREA2 from %p\n", (void *)target);
exit(EXIT_FAILURE);
}
for (x = 0; x < GROPE_SIZE/4; NEXT(x)) {
if (!strncmp((char *)&buffer[x], MP_SIG, 4)) {
*paddr = (x*4) + GROPE_AREA2;
return 6;
}
}
*paddr = (unsigned long)0;
return 0;
}
int enumerate_cpus(void)
{
unsigned long paddr;
struct mpfps mpfps;
silent = 1;
/* open physical memory for access to MP structures */
if ((pfd = open("/dev/mem", O_RDONLY)) < 0) {
fprintf(stderr, "enumerate_cpus(): /dev/mem: %s\n", strerror(errno));
return -1;
}
/* probe for MP structures */
if (apic_probe(&paddr) <= 0)
return 1;
/* read in mpfps structure*/
seekEntry(paddr);
if (readEntry(&mpfps, sizeof(struct mpfps))) {
printf("error reading mpfpsfrom %p\n", (void *)paddr);
exit(EXIT_FAILURE);
}
/* check whether an MP config table exists */
if (!mpfps.mpfb1)
if (MPConfigTableHeader(mpfps.pap) == SMP_YES)
return ncpu;
return 1;
}
void display_mptable(void)
{
unsigned long paddr;
struct mpfps mpfps;
silent = 0;
/* open physical memory for access to MP structures */
if ((pfd = open("/dev/mem", O_RDONLY)) < 0) {
fprintf(stderr, "%s(): /dev/mem: %s\n", __func__, strerror(errno));
return;
}
/* probe for MP structures */
if (apic_probe(&paddr) <= 0)
return;
/* read in mpfps structure*/
seekEntry(paddr);
if (readEntry(&mpfps, sizeof(struct mpfps))) {
printf("error reading mpfps from %p\n", (void *)paddr);
exit(EXIT_FAILURE);
}
/* parse an MP config table if it exists */
if (!mpfps.mpfb1)
(void) MPConfigTableHeader(mpfps.pap);
}