LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 03-23-2012, 02:03 PM   #1
ulmo
LQ Newbie
 
Registered: Jul 2004
Distribution: debian
Posts: 28
Blog Entries: 5

Rep: Reputation: 2
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.

Last edited by ulmo; 03-24-2012 at 09:17 AM.
 
Old 03-24-2012, 09:24 AM   #2
ulmo
LQ Newbie
 
Registered: Jul 2004
Distribution: debian
Posts: 28

Original Poster
Blog Entries: 5

Rep: Reputation: 2
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?
 
Old 04-10-2012, 10:56 AM   #3
ulmo
LQ Newbie
 
Registered: Jul 2004
Distribution: debian
Posts: 28

Original Poster
Blog Entries: 5

Rep: Reputation: 2
Any suggestions about an other forum where I can ask? Any hint is appreciated, thanks.
 
Old 04-10-2012, 02:18 PM   #4
nini09
Senior Member
 
Registered: Apr 2009
Posts: 1,855

Rep: Reputation: 161Reputation: 161
You can't disable CPU cache in fly.
 
Old 04-10-2012, 02:42 PM   #5
Enrix
Member
 
Registered: Mar 2012
Location: Aguascalientes, mx
Distribution: Opensuse 12.1 x86-64
Posts: 34

Rep: Reputation: 1
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?
 
Old 04-10-2012, 04:12 PM   #6
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
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.
 
1 members found this post helpful.
Old 04-11-2012, 01:44 PM   #7
ulmo
LQ Newbie
 
Registered: Jul 2004
Distribution: debian
Posts: 28

Original Poster
Blog Entries: 5

Rep: Reputation: 2
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.

Last edited by ulmo; 04-11-2012 at 01:45 PM.
 
1 members found this post helpful.
Old 04-12-2012, 10:40 AM   #8
ulmo
LQ Newbie
 
Registered: Jul 2004
Distribution: debian
Posts: 28

Original Poster
Blog Entries: 5

Rep: Reputation: 2
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.
 
1 members found this post helpful.
Old 07-30-2012, 06:59 AM   #9
hga
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Rep: Reputation: Disabled
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
 
Old 07-30-2012, 08:26 AM   #10
ulmo
LQ Newbie
 
Registered: Jul 2004
Distribution: debian
Posts: 28

Original Poster
Blog Entries: 5

Rep: Reputation: 2
@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.
 
Old 07-31-2012, 03:28 AM   #11
hga
LQ Newbie
 
Registered: Jul 2012
Posts: 5

Rep: Reputation: Disabled
can you be more specfic with the steps...where is to be the code saved and with what extension? thank you.
 
Old 07-31-2012, 03:48 AM   #12
ulmo
LQ Newbie
 
Registered: Jul 2004
Distribution: debian
Posts: 28

Original Poster
Blog Entries: 5

Rep: Reputation: 2
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

Last edited by ulmo; 07-31-2012 at 03:53 AM.
 
Old 07-31-2012, 07:41 AM   #13
naam
LQ Newbie
 
Registered: Jul 2012
Posts: 2

Rep: Reputation: Disabled
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???
 
Old 08-01-2012, 07:58 AM   #14
ulmo
LQ Newbie
 
Registered: Jul 2004
Distribution: debian
Posts: 28

Original Poster
Blog Entries: 5

Rep: Reputation: 2
I have no idea, I have only a very basic knowledge of make...
 
Old 08-01-2012, 02:28 PM   #15
nini09
Senior Member
 
Registered: Apr 2009
Posts: 1,855

Rep: Reputation: 161Reputation: 161
Hi naam,

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
drop_caches doesn't drop all caches 10110111 Linux - Software 1 11-14-2010 11:09 AM
LXer: This week at LWN: LCE: Memory part 2: CPU caches LXer Syndicated Linux News 0 10-11-2007 12:30 PM
AMD vs Intel (FSB, and Caches) colinstu General 3 08-14-2007 11:52 AM
Disabling CPUID - kernel panic! RedHat 6.2 & AMD CPU rhinds1369 Red Hat 8 11-20-2005 12:44 PM
Update others arp caches rickthemick Linux - Networking 2 12-14-2004 06:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 05:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration