Refactor gen_idt

Refactor gen_idt tool to detect more errors as well as be both more clear and
easily expanded.

Change-Id: I035a0666871733cc670f64302a1e85be455550ee
Signed-off-by: Peter Mitsis <peter.mitsis@windriver.com>
This commit is contained in:
Peter Mitsis 2015-10-08 11:34:06 -04:00 committed by Anas Nashif
commit 3dc247825c

View file

@ -64,7 +64,7 @@
#include "idtEnt.h" #include "idtEnt.h"
/* Define the following macro to see a verbose or debug output from the tool */ /* Define the following macro to see a verbose or debug output from the tool */
/*#define DEBUG_GENIDT*/ /* #define DEBUG_GENIDT */
#ifdef DEBUG_GENIDT #ifdef DEBUG_GENIDT
#define PRINTF(...) printf(__VA_ARGS__) #define PRINTF(...) printf(__VA_ARGS__)
@ -73,44 +73,59 @@
#endif #endif
#if !defined(WINDOWS) #if !defined(WINDOWS)
#define O_BINARY 0 #define O_BINARY 0
#endif #endif
#define MAX_NUM_VECTORS 256
static void get_exec_name(char *pathname); static void get_exec_name(char *pathname);
static void usage(int len); static void usage(int len);
static void get_options(int argc, char *argv[]); static void get_options(int argc, char *argv[]);
static void open_files(void); static void open_files(void);
static void read_input_file(void);
static void close_files(void); static void close_files(void);
static void genIdt(void); static void validate_input_file(void);
static void generate_idt(void);
static void clean_exit(int exit_code); static void clean_exit(int exit_code);
struct s_isrList { struct genidt_header_s {
void *fnc; void *spurious_addr;
void *spurious_no_error_addr;
unsigned int num_entries;
};
struct genidt_entry_s {
void *isr;
unsigned int vector_id;
unsigned int dpl; unsigned int dpl;
}; };
static struct s_isrList idt[256]; static struct genidt_header_s genidt_header;
static struct genidt_entry_s supplied_entry[MAX_NUM_VECTORS];
static struct genidt_entry_s generated_entry[MAX_NUM_VECTORS];
enum { enum {
IFILE = 0, /* input file */ IFILE = 0, /* input file */
OFILE, /* output file */ OFILE, /* output file */
NUSERFILES, /* number of user-provided file names */ NUSERFILES, /* number of user-provided file names */
EXECFILE = NUSERFILES, /* for name of executable */ EXECFILE = NUSERFILES, /* for name of executable */
NFILES /* total number of files open */ NFILES /* total number of file names */
}; };
enum { SHORT_USAGE, LONG_USAGE }; enum { SHORT_USAGE, LONG_USAGE };
static int fds[NUSERFILES] = {-1, -1}; static int fds[NUSERFILES] = {-1, -1};
static char *filenames[NFILES]; static char *filenames[NFILES];
static unsigned int numVecs = (unsigned int)-1; static unsigned int num_vectors = (unsigned int)-1;
static struct version version = {KERNEL_VERSION, 1, 1, 1}; static struct version version = {KERNEL_VERSION, 1, 1, 2};
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
get_exec_name(argv[0]); get_exec_name(argv[0]);
get_options(argc, argv); /* may exit */ get_options(argc, argv); /* may exit */
open_files(); /* may exit */ open_files(); /* may exit */
genIdt(); /* may exit */ read_input_file(); /* may exit */
validate_input_file(); /* may exit */
generate_idt(); /* may exit */
close_files(); close_files();
return 0; return 0;
} }
@ -134,8 +149,8 @@ static void get_options(int argc, char *argv[])
usage(LONG_USAGE); usage(LONG_USAGE);
exit(0); exit(0);
case 'n': case 'n':
numVecs = (unsigned int)strtoul(optarg, &endptr, 10); num_vectors = (unsigned int) strtoul(optarg, &endptr, 10);
if (*optarg == '\0' || *endptr != '\0') { if ((*optarg == '\0') || (*endptr != '\0')) {
usage(SHORT_USAGE); usage(SHORT_USAGE);
exit(-1); exit(-1);
} }
@ -149,7 +164,7 @@ static void get_options(int argc, char *argv[])
} }
} }
if ((unsigned int)-1 == numVecs) { if (num_vectors > MAX_NUM_VECTORS) {
usage(SHORT_USAGE); usage(SHORT_USAGE);
exit(-1); exit(-1);
} }
@ -168,12 +183,12 @@ static void get_exec_name(char *pathname)
while (end != -1) { while (end != -1) {
#if defined(WINDOWS) /* Might have both slashes in path */ #if defined(WINDOWS) /* Might have both slashes in path */
if (pathname[end] == '/' || pathname[end] == '\\') if ((pathname[end] == '/') || (pathname[end] == '\\'))
#else #else
if (pathname[end] == '/') if (pathname[end] == '/')
#endif #endif
{ {
if (0 == end || pathname[end - 1] != '\\') { if ((0 == end) || (pathname[end - 1] != '\\')) {
++end; ++end;
break; break;
} }
@ -188,8 +203,8 @@ static void open_files(void)
int ii; int ii;
fds[IFILE] = open(filenames[IFILE], O_RDONLY | O_BINARY); fds[IFILE] = open(filenames[IFILE], O_RDONLY | O_BINARY);
fds[OFILE] = open(filenames[OFILE], O_WRONLY | O_CREAT | fds[OFILE] = open(filenames[OFILE], O_WRONLY | O_BINARY |
O_TRUNC | O_BINARY, O_TRUNC | O_CREAT,
S_IWUSR | S_IRUSR); S_IWUSR | S_IRUSR);
for (ii = 0; ii < NUSERFILES; ii++) { for (ii = 0; ii < NUSERFILES; ii++) {
int invalid = fds[ii] == -1; int invalid = fds[ii] == -1;
@ -206,98 +221,156 @@ static void open_files(void)
} }
} }
static void genIdt(void) static void show_entry(struct genidt_entry_s *entry)
{ {
unsigned int i; fprintf(stderr,
unsigned int size; "---------------\n"
void *spurAddr; "ISR Address: %p\n"
void *spurNoErrAddr; "Vector ID: %d\n"
"DPL: %d\n"
"---------------\n",
entry->isr, entry->vector_id, entry->dpl);
}
/* static void read_input_file(void)
* First find the address of the spurious interrupt handlers. They are the {
* contained in the first 8 bytes of the input file. ssize_t bytes_read;
*/ size_t bytes_to_read;
if (read(fds[IFILE], &spurAddr, 4) < 4) {
goto readError; /* Read the header information. */
bytes_read = read(fds[IFILE], &genidt_header, sizeof(genidt_header));
if (bytes_read != sizeof(genidt_header)) {
goto read_error;
} }
if (read(fds[IFILE], &spurNoErrAddr, 4) < 4) { PRINTF("Spurious interrupt handlers found at %p and %p.\n",
goto readError; genidt_header.spurious_addr, genidt_header.spurious_no_error_addr);
} PRINTF("There are %d ISR(s).\n", genidt_header.num_entries);
PRINTF("Spurious int handlers found at %p and %p\n", if (genidt_header.num_entries > num_vectors) {
spurAddr, spurNoErrAddr);
/* Initially fill in the IDT array with the spurious handlers */
for (i = 0; i < numVecs; i++) {
if ((((1 << i) & _EXC_ERROR_CODE_FAULTS)) && (i < 32)) {
idt[i].fnc = spurAddr;
} else {
idt[i].fnc = spurNoErrAddr;
}
}
/*
* Now parse the rest of the input file for the other ISRs. The number of
* entries is the next 4 bytes
*/
if (read(fds[IFILE], &size, 4) < 4) {
goto readError;
}
PRINTF("There are %d ISR(s)\n", size);
if (size > numVecs) {
fprintf(stderr, fprintf(stderr,
"Too many ISRs found. Got %u. Expected less than %u.\n" "Too many ISRs found. Got %u. Expected no more than %u.\n"
"Malformed input file?\n", "Malformed input file?\n",
size, numVecs); genidt_header.num_entries, num_vectors);
clean_exit(-1); clean_exit(-1);
} }
while (size--) { bytes_to_read = sizeof(struct genidt_entry_s) * genidt_header.num_entries;
void *addr; bytes_read = read(fds[IFILE], supplied_entry, bytes_to_read);
unsigned int vec; if (bytes_read != bytes_to_read) {
unsigned int dpl; goto read_error;
}
/* Get address */ #ifdef DEBUG_GENIDT
if (read(fds[IFILE], &addr, 4) < 4) { int i;
goto readError;
for (i = 0; i < genidt_header.num_entries; i++) {
show_entry(&supplied_entry[i]);
}
#endif
return;
read_error:
fprintf(stderr, "Error occurred while reading input file. Aborting...\n");
clean_exit(-1);
}
static void validate_dpl(void)
{
int i;
for (i = 0; i < genidt_header.num_entries; i++) {
if (supplied_entry[i].dpl != 0) {
fprintf(stderr,
"Invalid DPL bits specified. Must be zero.\n");
show_entry(&supplied_entry[i]);
clean_exit(-1);
} }
/* Get vector */ }
if (read(fds[IFILE], &vec, 4) < 4) { }
goto readError;
} static void validate_vector_id(void)
/* Get dpl */ {
if (read(fds[IFILE], &dpl, 4) < 4) { int i;
goto readError; int vectors[MAX_NUM_VECTORS] = {0};
/*
* NOTE: Future versions of the gen_idt tool may generate the vector ID
* instead of relying upon values already existing in the input file.
*/
for (i = 0; i < genidt_header.num_entries; i++) {
if (supplied_entry[i].vector_id >= num_vectors) {
fprintf(stderr,
"Vector ID exceeds specified # of vectors (%d).\n",
num_vectors);
show_entry(&supplied_entry[i]);
clean_exit(-1);
} }
PRINTF("ISR @ %p on Vector %d: dpl %d\n", addr, vec, dpl); if (vectors[supplied_entry[i].vector_id] != 0) {
idt[vec].fnc = addr; fprintf(stderr, "Duplicate vector ID found.\n");
idt[vec].dpl = dpl; show_entry(&supplied_entry[i]);
clean_exit(-1);
}
vectors[supplied_entry[i].vector_id]++;
}
}
static void validate_input_file(void)
{
validate_dpl(); /* exits on error */
validate_vector_id(); /* exits on error */
}
static void generate_idt(void)
{
unsigned int i;
unsigned int vector_id;
/*
* Initialize the generated entries with default
* spurious interrupt handlers.
*/
for (i = 0; i < num_vectors; i++) {
if ((((1 << i) & _EXC_ERROR_CODE_FAULTS)) && (i < 32)) {
generated_entry[i].isr = genidt_header.spurious_addr;
} else {
generated_entry[i].isr = genidt_header.spurious_no_error_addr;
}
}
/*
* Overwrite the generated entries as appropriate with the
* validated supplied entries.
*/
for (i = 0; i < genidt_header.num_entries; i++) {
vector_id = supplied_entry[i].vector_id;
generated_entry[vector_id] = supplied_entry[i];
} }
/* /*
* We now have the address of all ISR stub/functions captured in idt[]. * We now have the address of all ISR stub/functions captured in
* Now construct the actual idt. * generated_entry[]. Now construct the actual IDT.
*/ */
for (i = 0; i < numVecs; i++) { for (i = 0; i < num_vectors; i++) {
unsigned long long idtEnt; unsigned long long idt_entry;
ssize_t bytes_written;
_IdtEntCreate(&idtEnt, idt[i].fnc, idt[i].dpl); _IdtEntCreate(&idt_entry, generated_entry[i].isr,
generated_entry[i].dpl);
write(fds[OFILE], &idtEnt, 8); bytes_written = write(fds[OFILE], &idt_entry, sizeof(idt_entry));
if (bytes_written != sizeof(idt_entry)) {
fprintf(stderr, "Failed to write IDT entry %u.\n", num_vectors);
clean_exit(-1);
}
} }
return; return;
readError:
fprintf(stderr,
"Error occurred while reading input file. Aborting...\n");
clean_exit(-1);
} }
static void close_files(void) static void close_files(void)