forked from LLNL/libmsr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoconf.c
171 lines (155 loc) · 4.97 KB
/
autoconf.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
/* autoconf.c
*
* Copyright (c) 2011-2016, Lawrence Livermore National Security, LLC.
* LLNL-CODE-645430
*
* Produced at Lawrence Livermore National Laboratory
* Written by Barry Rountree, [email protected]
* Scott Walker, [email protected]
* Kathleen Shoga, [email protected]
*
* All rights reserved.
*
* This file is part of libmsr.
*
* libmsr is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* libmsr is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
* more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libmsr. If not, see <http://www.gnu.org/licenses/>.
*
* This material is based upon work supported by the U.S. Department of
* Energy's Lawrence Livermore National Laboratory. Office of Science, under
* Award number DE-AC52-07NA27344.
*
*/
// This code will generate the correct header files for the target
// architecture.
#include <ctype.h>
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FNAME_SIZE 128
#define BUFFER_SIZE 512
uint64_t detect_arch(void)
{
uint64_t rax = 1;
uint64_t rbx = 0;
uint64_t rcx = 0;
uint64_t rdx = 0;
asm volatile(
"cpuid"
: "=a" (rax), "=b" (rbx), "=c" (rcx), "=d" (rdx)
: "0" (rax), "2" (rcx));
return ((rax >> 4) & 0xF) | ((rax >> 12) & 0xF0);
}
FILE *open_header(char *a)
{
FILE *header = NULL;
char fname[FNAME_SIZE];
uint64_t arch = detect_arch();
if (a != NULL) {
snprintf(fname, FNAME_SIZE, "platform_headers/Intel%s.h", a);
}
else {
snprintf(fname, FNAME_SIZE, "platform_headers/Intel%lX.h", arch);
}
header = fopen(fname, "r");
if (header == NULL) {
fprintf(stderr, "ERROR: unable to open file %s\n", fname);
if (a != NULL) {
fprintf(stderr, "Model %s may not be supported. No matching header files found in platform_headers/.\n", a);
}
else {
fprintf(stderr, "Model %lx may not be supported. Use -f to force.\n", arch);
}
exit(-1);
}
return header;
}
FILE *open_master(void)
{
FILE *master = fopen("include/master.h", "w");
if (master == NULL) {
fprintf(stderr, "ERROR: unable to open file include/master.h\n");
exit(-2);
}
return master;
}
int copy(char *arch)
{
FILE *header = open_header(arch);
FILE *master = open_master();
char *buffer = (char *) malloc(BUFFER_SIZE * sizeof(char));
size_t bsize = BUFFER_SIZE;
size_t ret = 0;
fprintf(master, "// This file was generated automatically by autoconf.c\n");
fprintf(master, "// You should not modify it unless you really know what you're doing.\n");
while ((ret = getline(&buffer, &bsize, header)) != -1) {
fprintf(master, "%s", buffer);
}
if (header != NULL) {
fclose(header);
}
if (master != NULL) {
fclose(master);
}
if (buffer != NULL) {
free(buffer);
}
return 0;
}
int main(int argc, char **argv)
{
const char *usage = "\n"
"NAME\n"
" autoconf - Detect architecture model for MSR definitions\n"
"SYNOPSIS\n"
" %s [--help | -h] [-f]\n"
"OVERVIEW\n"
" Autoconf was built to determine the architecture model for\n"
" which libmsr is being built on. The architecture determines\n"
" the offsets for several MSRs, which may be architecture-specific.\n"
"OPTIONS\n"
" --help | -h\n"
" Display this help information, then exit.\n"
" -f\n"
" Target architecture model.\n"
"\n";
if (argc > 1 && (strncmp(argv[1], "--help", strlen("--help")) == 0 ||
strncmp(argv[1], "-h", strlen("-h")) == 0 )) {
printf(usage, argv[0]);
return EXIT_SUCCESS;
}
if (argc > 3) {
printf(usage, argv[0]);
return EXIT_FAILURE;
}
/* Auto-detect architecture model. */
if (argc == 1) {
copy(NULL);
return EXIT_SUCCESS;
}
int opt;
while ((opt = getopt(argc, argv, "f:")) != -1) {
switch(opt) {
case 'f':
copy(optarg);
break;
default:
fprintf(stderr, "\nError: unknown parameter \"%c\"\n", opt);
fprintf(stderr, usage, argv[0]);
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}