├── ss_stress-ng.png ├── Makefile ├── cr0cd_reader.c ├── README.md ├── LICENSE └── cr0cd.c /ss_stress-ng.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alphaKAI/cr0cd/HEAD/ss_stress-ng.png -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # DST 2 | obj-m+=cr0cd.o 3 | # SOURCE 4 | CFLAGS_kfree.o := -g -O0 5 | KDIR := /lib/modules/$(shell uname -r)/build 6 | PWD := $(shell pwd) 7 | VERBOSE = 0 8 | 9 | all: 10 | $(MAKE) -C $(KDIR) M=$(PWD) KBUILD_VERBOSE=$(VERBOSE) modules 11 | 12 | clean: 13 | $(MAKE) -C $(KDIR) M=$(PWD) KBUILD_VERBOSE=$(VERBOSE) clean 14 | 15 | -------------------------------------------------------------------------------- /cr0cd_reader.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | char bits[33]; 6 | FILE *fp = fopen("/proc/cr0cd", "r"); 7 | 8 | if (fp == NULL) { 9 | fprintf(stderr, "Failed to read cr0cd\n"); 10 | exit(EXIT_FAILURE); 11 | } 12 | 13 | fread(bits, 33, 1, fp); 14 | 15 | printf("CR0: %s\n", bits); 16 | 17 | fclose(fp); 18 | } 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cr0cd 2 | CR0.CD manipulate Linux Kernel Module as a proc filesystem 3 | 4 | ## Installation 5 | ```sh 6 | $ git clone https://github.com/alphaKAI/cr0cd 7 | $ cd cr0cd 8 | $ make 9 | $ gcc -o cr0cd_reader cr0cd_reader.c 10 | ``` 11 | 12 | ## Usage 13 | 14 | CR0 exists on each of CPU core, you need specify which core will be disabled by taskset. 15 | 16 | ``` 17 | $ sudo insmod cr0cd.ko 18 | $ taskset -c $CORE_NUMBER echo 1 > /proc/cr0cd # Disable CPU Cache 19 | $ taskset -c $CORE_NUMBER some command # run on CPU Cache is disabled 20 | $ taskset -c $CORE_NUMBER echo 0 > /proc/cr0cd # Enable CPU Cache 21 | $ taskset -c $CORE_NUMBER ./cr0cd_reader # display current CR0 bits 22 | ``` 23 | 24 | ## Screenshot 25 | 26 | This is an example of how CPU Cache disable affects. 27 | 28 | ![screenshot](ss_stress-ng.png) 29 | 30 | Memory access speed down to 2GB/sec -> 1MB(or more slow). 31 | 32 | ## LICENSE 33 | cr0cd is released under the MIT License. 34 | Please see `LICENSE` for more details. 35 | Copyright (C) 2019 Akihiro Shoji. 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | cr0cd - CR0.CD manipulate Linux Kernel Module as a proc filesystem 2 | Copyright © 2019 Akihiro Shoji 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining 5 | a copy of this software and associated documentation files (the "Software"), 6 | to deal in the Software without restriction, including without limitation 7 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | and/or sell copies of the Software, and to permit persons to whom the 9 | Software is furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included 12 | in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 16 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 20 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | -------------------------------------------------------------------------------- /cr0cd.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | MODULE_LICENSE("Dual MIT/GPL"); 11 | 12 | #define DRIVER_NAME "cr0cd" 13 | #define PROC_NAME "cr0cd" 14 | 15 | static int cr0cd_proc_open(struct inode *inode, struct file *file) { 16 | printk("%s IS OPEND\n", DRIVER_NAME); 17 | 18 | return 0; 19 | } 20 | 21 | volatile static int CR0; 22 | 23 | void int_to_bits(int n, char *bits) { 24 | int i; 25 | for(i = 0; i < 32; i++) { 26 | bits[i] = (n >> (31 - i)) & 1 ? '1' : '0'; 27 | } 28 | } 29 | 30 | static ssize_t cr0cd_proc_read(struct file *file, char __user *buf, 31 | size_t count, loff_t *f_pos) { 32 | char bits[33]; 33 | 34 | printk("%s IS READ\n", DRIVER_NAME); 35 | 36 | asm("mov %%cr0, %%rax; mov %%rax, CR0" ::: "rax"); // read 37 | int_to_bits(CR0, bits); 38 | bits[32] = '\0'; 39 | 40 | copy_to_user(buf, bits, 33); 41 | 42 | return 33; 43 | } 44 | 45 | static ssize_t cr0cd_proc_write(struct file *file, const char __user *buf, 46 | size_t count, loff_t *f_pos) { 47 | char bits[33]; 48 | char mode[10]; 49 | 50 | printk("%s IS WRITTEN\n", DRIVER_NAME); 51 | asm("mov %%cr0, %%rax; mov %%rax, CR0" ::: "rax"); // read 52 | 53 | bits[32] = '\0'; 54 | 55 | int_to_bits(CR0, bits); 56 | 57 | printk(" CURRENT CR0: %s", bits); 58 | 59 | copy_from_user(mode, buf, sizeof(mode)); 60 | 61 | if(mode[0] == '1') { // disable 62 | printk("[DISABLE] CPU Cache\n"); 63 | 64 | CR0 |= 1 << 30; 65 | } else { // enable 66 | printk("[ENABLE] CPU Cache\n"); 67 | 68 | CR0 &= ~(1 << 30); 69 | } 70 | 71 | asm("mov CR0, %%rax; mov %%rax, %%cr0" ::: "rax"); // write 72 | 73 | int_to_bits(CR0, bits); 74 | 75 | printk(" CURRENT CR0: %s", bits); 76 | 77 | return count; 78 | } 79 | 80 | // handler table for proc_fs 81 | static struct file_operations cr0cd_proc_fops = { 82 | .owner = THIS_MODULE, 83 | .open = cr0cd_proc_open, 84 | .read = cr0cd_proc_read, 85 | .write = cr0cd_proc_write, 86 | }; 87 | 88 | static int cr0cd_init(void) { 89 | struct proc_dir_entry *entry; 90 | 91 | printk("INTIALIZE %s\n", DRIVER_NAME); 92 | 93 | entry = proc_create(PROC_NAME, S_IRUGO | S_IWUGO, NULL, &cr0cd_proc_fops); 94 | 95 | if(entry == NULL) { 96 | printk(KERN_ERR " failed to create device %s\n", DRIVER_NAME); 97 | return -ENOMEM; 98 | } 99 | 100 | return 0; 101 | } 102 | 103 | static void cr0cd_exit(void) { 104 | printk("EXIT %s\n", DRIVER_NAME); 105 | 106 | remove_proc_entry(PROC_NAME, NULL); 107 | } 108 | 109 | module_init(cr0cd_init); 110 | module_exit(cr0cd_exit); 111 | --------------------------------------------------------------------------------