LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (https://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   Disabling CPU caches (https://www.linuxquestions.org/questions/linux-kernel-70/disabling-cpu-caches-936077/)

ulmo 03-23-2012 02:03 PM

Disabling CPU caches
 
Hi,

I would like to disable the level 1 and level 2 caches of my CPU. I wrote a kernel module to set the 30th bit of the control register cr0. But when I try to insert the module with insmod, the system freezes. I am using a Thinkpad X41 with a Pentium M and kernel 2.6.32-40-generic #87-Ubuntu SMP. What can I do to determine the reason for the freeze?

Thank you for your help!

The code of the module:
Code:

#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int disableCache_init(void)
{
        printk(KERN_ALERT "Disabling L1 and L2 caches.\n");
        __asm__(".intel_syntax noprefix\n\t"
                "mov    eax,cr0\n\t"
                "or    eax,(1 << 30)\n\t"
                "mov    cr0,eax\n\t"
                "wbinvd\n\t"
                ".att_syntax noprefix\n\t"
        : : : "eax" );
        return 0;
}
static void disableCache_exit(void)
{
        printk(KERN_ALERT "Enabling L1 and L2 caches.\n");
        __asm__(".intel_syntax noprefix\n\t"
                "mov    eax,cr0\n\t"
                "and    eax,~(1 << 30)\n\t"
                "mov    cr0,eax\n\t"
                "wbinvd\n\t"
                ".att_syntax noprefix\n\t"
        : : : "eax" );
}
module_init(disableCache_init);
module_exit(disableCache_exit);

And the Makefile:
Code:

EXTRA_CFLAGS = -m32
obj-m += disableCache.o

all:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

insmod command:
Code:

sudo insmod ./disableCache.ko
Edit: Adopted the code of the module, see second post.

ulmo 03-24-2012 09:24 AM

As I just found out, the syntax for the mov instruction with control registers is not as I expected: in Intel syntax, the destination register comes first in cases where control registers are involved. I adopted the code of the module in the first post.

However, the overall result stays the same, the system freezes when the module is inserted. I found out that the module runs without problems with this line commented out:
Code:

"or  eax,(1 << 30)\n\t"
(But it also has no effect because cr0 is not altered.)

Any ideas?

ulmo 04-10-2012 10:56 AM

Any suggestions about an other forum where I can ask? Any hint is appreciated, thanks.

nini09 04-10-2012 02:18 PM

You can't disable CPU cache in fly.

Enrix 04-10-2012 02:42 PM

Disabling cpu caches gives you a pentium I like processor!!!
The addition of Cache on CPU was one of the best upgrades to cpu architecture and is used to retain temporal data / instructions. as nini09 have said, you cannot disable it at fly, but certain BIOS would give you the option at boot time. Is there any special requirement, or is only homework / test?

whizje 04-10-2012 04:12 PM

It can be done this is from memtest
Code:

static inline void cache_off(void)
{
        asm(
                "push %eax\n\t"
                "movl %cr0,%eax\n\t"
                "orl $0x40000000,%eax\n\t"  /* Set CD */
                "movl %eax,%cr0\n\t"
                "wbinvd\n\t"
                "pop  %eax\n\t");
}

but you need to flush the cache that's what wbinvd is for.

ulmo 04-11-2012 01:44 PM

Thank you for your answers. I want to disable the caches for a research project where we want to measure execution times and want to be sure they are not influenced by the internal caches.

@whizje: I tried your suggestion and apparently, it is working. :) But I have to investigate it further tomorrow when I will have more time.

ulmo 04-12-2012 10:40 AM

After some investigation, I found out that the module runs fine (with the corrections from my second post). I just didn't wait long enough after fixing the mov cr0, eax bug, misinterpreating the slow behavior of the system with disabled caches as a complete freeze. My apologies for that.

The code posted by whizje is also working, of course. I didn't find an option for cache disabling in the BIOS of my machine.

Thanks for your input.

hga 07-30-2012 06:59 AM

how to run the above segment of codes....thanks
 
i am trying to disable the caches ...i want to know how the above code works and how to execute it

ulmo 07-30-2012 08:26 AM

@hga Run make and insmod. Source code, makefile and argument for insmod is given in the first post. If you have problems, please ask more specific questions.

hga 07-31-2012 03:28 AM

can you be more specfic with the steps...where is to be the code saved and with what extension? thank you.

ulmo 07-31-2012 03:48 AM

I would suggest to save the source code (in an arbitrary directory) in a file named
Code:

disableCache.c
and the make file in a file named
Code:

Makefile
Then run
Code:

make all
followed by
Code:

sudo insmod ./disableCache.ko

naam 07-31-2012 07:41 AM

i saved the above files and tried the make command. But it displays
make: Nothing to be done for `all'..... Can you help me???

---------- Post added 07-31-12 at 07:42 AM ----------

i saved the above files and tried the make command. But it displays
make: Nothing to be done for `all'..... Can you help me???

ulmo 08-01-2012 07:58 AM

I have no idea, I have only a very basic knowledge of make...

nini09 08-01-2012 02:28 PM

Hi naam,

It look like your makefile has problem. You can make Hello module work at first and then deal with disableCache.c


All times are GMT -5. The time now is 03:33 PM.