-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsgdt.c
More file actions
93 lines (74 loc) · 1.35 KB
/
sgdt.c
File metadata and controls
93 lines (74 loc) · 1.35 KB
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
#include <stdint.h>
#include <stdio.h>
/*
* Usage:
* sgdt
* or
* sgdt (gdt|ldt|idt|tr)+
*/
#define ARRSZE(X) (sizeof(X) / sizeof(*(X)))
struct tr {
uint16_t limit;
void* base;
} __packed;
typedef void (*settr)(struct tr*);
static int streq(const char* a, const char* b)
{
size_t i = 0;
while (a[i] == b[i] && a[i])
i++;
return b[i] == a[i];
}
static void get_gdt(struct tr* tr)
{
__asm__ __volatile__("sgdt %0\n\t"
: : "m" (*tr));
}
static void get_ldt(struct tr* tr)
{
__asm__ __volatile__("sldt %0\n\t"
: : "m" (*tr));
}
static void get_idt(struct tr* tr)
{
__asm__ __volatile__("sidt %0\n\t"
: : "m" (*tr));
}
static void get_tr(struct tr* tr)
{
__asm__ __volatile__("str %0\n\t"
: : "m" (*tr));
}
static struct {
const char* name;
settr settr;
} funclist[] = {
{ "gdt", get_gdt },
{ "ldt", get_ldt },
{ "idt", get_idt },
{ "tr", get_tr },
};
static void print_tr(settr settr)
{
struct tr tr = {0, 0};
settr(&tr);
printf("base: %p\nlimit: %x\n", tr.base, tr.limit);
}
int main(int argc, const char** argv)
{
argv++;
argc--;
if (argc <= 0) {
argc = 1;
argv[0] = "gdt";
}
for (int i = 0; i < argc; i++) {
for (size_t j = 0; j < ARRSZE(funclist); j++) {
if (!streq(funclist[j].name, argv[i]))
continue;
printf("%s:\n", funclist[j].name);
print_tr(funclist[j].settr);
break;
}
}
}