I'm trying to use cache as a temporary memory. And after using the cache I do not want to store any of the modified cache lines. I came to know that I can achieve that by running invd
instruction. Because unlike wbinvd
, invd
Invalidates (flushes) the processor's internal caches without storing them into the main memory.
I wrote a kernel module to check if I can execute the invd
instruction.
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
int new_invd(void){
asm volatile ("invd" : : : "memory");
return 1;
}
static int __init hello_start(void)
{
printk(KERN_INFO "Loading hello module...\n");
//check if invd instruction executes
printk(KERN_INFO "running invd\n", new_invd());
return 0;
}
static void __exit hello_end(void)
{
printk(KERN_INFO "Goodbye\n");
}
module_init(hello_start);
module_exit(hello_end);
Upon compiling and inserting the module I get Segmentation fault (core dumped)
and dmesg
shows,
[ 7525.227059] Loading hello module... [ 7525.227088] general protection fault: 0000 [#1] SMP
I use asm volatile ("invd" : : : "memory");
as mentioned in chromium. Now I think I'm getting the error because executing invd
violates the coherency of main memory and cache as pointing out here How can I do a CPU cache flush in x86 Windows? by @Gunther Piez. However, I'm not sure if this is the case.
So, any help why I'm getting this segfault
? If this is due to the violating cache corehency, how can I fix that? If not, how can I execute invd
?
I'm using Linux xxx 4.4.0-200-generic #232-Ubuntu SMP Wed Jan 13 10:18:39 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
cat /proc/cpuinfo
shows,
vendor_id : GenuineIntel
cpu family : 6
model : 158
model name : Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz