-
Notifications
You must be signed in to change notification settings - Fork 0
/
cachecontrol.c
96 lines (75 loc) · 2.01 KB
/
cachecontrol.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
#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
MODULE_LICENSE("Dual BSD/GPL");
//TODO: enable per-cpu execution
static long cr0_status(void) {
volatile long cr0;
__asm__(
"mov %%cr0, %0;"
:"=r"(cr0)
: /* no input registers */
: /* no explicit clobbered regs */
);
return cr0;
}
static void cachecontrol_disable_caches(void) {
__asm__(
"push %rax\n\t"
"mov %cr0,%rax\n\t"
"or $(1<<30),%rax\n\t"
"mov %rax,%cr0\n\t"
"wbinvd\n\t"
"pop %rax\n\t");
}
static void cachecontrol_enable_caches(void) {
__asm__(
"push %rax\n\t"
"mov %cr0,%rax\n\t"
"and $(~(1<<30)),%rax\n\t"
"mov %rax,%cr0\n\t"
"wbinvd\n\t"
"pop %rax\n\t");
}
int cachecontrol_read_cr0(char *buf, char **start, off_t offset,
int count, int *eof, void *data) {
int r;
//r = sprintf(buf, "%02lX\n", cr0_status());
r = sprintf(buf, "%ld\n", cr0_status());
*eof = 1;
return r;
}
int cachecontrol_read_disable(char *buf, char **start, off_t offset,
int count, int *eof, void *data) {
int r;
cachecontrol_disable_caches();
r = sprintf(buf, "OK: %02lX\n", cr0_status());
*eof = 1;
return r;
}
int cachecontrol_read_enable(char *buf, char **start, off_t offset,
int count, int *eof, void *data) {
int r;
cachecontrol_enable_caches();
r = sprintf(buf, "OK: %02lX\n", cr0_status());
*eof = 1;
return r;
}
static int cachecontrol_init(void) {
printk(KERN_NOTICE "Loading cachecontrol...\n");
create_proc_read_entry("cachecontrol-cr0", 0, NULL,
cachecontrol_read_cr0, NULL);
create_proc_read_entry("cachecontrol-disable", 0, NULL,
cachecontrol_read_disable, NULL);
create_proc_read_entry("cachecontrol-enable", 0, NULL,
cachecontrol_read_enable, NULL);
return 0;
}
static void cachecontrol_exit(void) {
printk(KERN_NOTICE "Unloading cachecontrol...\n");
remove_proc_entry("cachecontrol-cr0", NULL);
remove_proc_entry("cachecontrol-disable", NULL);
remove_proc_entry("cachecontrol-enable", NULL);
}
module_init(cachecontrol_init);
module_exit(cachecontrol_exit);