-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.c
76 lines (62 loc) · 2.11 KB
/
cache.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
/*
* cache-lkm
*
* This is a LKM for disabling CPU caches.
*
* Copyright (c) 2017 Geoffrey Ndu
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/smp.h>
// Works on Intel(R) Xeon(R) CPU X5550
// Developed based on this converstation
// https://stackoverflow.com/questions/21265785/enable-disable-cache-on-intel-64bit-machine-cd-bit-always-set
static void cache_off_impl(void *ignored) {
printk(KERN_WARNING "cache: disabling caches.\n");
__asm__ __volatile__("pushq %%rax\n\t"
"movq %%cr0,%%rax\n\t"
"orq $0x40000000,%%rax\n\t"
"movq %%rax,%%cr0\n\t"
"wbinvd\n\t"
"popq %%rax" :);
}
static void cache_on_impl(void *ignored) {
printk(KERN_WARNING "cache: enabling caches.\n");
__asm__ __volatile__("pushq %%rax\n\t"
"movq %%cr0,%%rax\n\t"
"andq $0xffffffffbfffffff,%%rax\n\t"
"movq %%rax,%%cr0\n\t"
"popq %%rax" :);
}
static void show_cr0(void *ignored) {
long long int cr0;
__asm__ __volatile__("pushq %%rax\n\t"
"movq %%cr0, %%rax\n\t"
"movq %%rax, %0\n\t"
"popq %%rax"
: "=r"(cr0)
:
:);
printk(KERN_WARNING "cache: current cache status cr0=%#018llx\n", cr0);
}
static int cache_off(void) {
smp_call_function(cache_off_impl, NULL, 1);
cache_off_impl(NULL);
show_cr0(NULL); // TODO: call on every core
return 0;
}
static void cache_on(void) {
cache_on_impl(NULL);
smp_call_function(cache_on_impl, NULL, 0);
show_cr0(NULL); // TODO: call on every core
}
MODULE_AUTHOR("Geoffrey Ndu");
MODULE_LICENSE("GPL");
module_init(cache_off);
module_exit(cache_on);