#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

MODULE_LICENSE("Dual BSD/GPL");

unsigned long long my_rdmsr(unsigned long val) {
  unsigned long low = 0, high = 0;

  __asm__ volatile ("rdmsr" :
            "=a"(low), "=d"(high)
            : "c"(val));

  return ((unsigned long long)high << 32) + low;
}

void my_wrmsr(unsigned long val, unsigned long low, unsigned long high) {

  __asm__ volatile ("wrmsr"
            :
            : "a"(low), "d"(high), "c"(val));

}

static int my_init(void) {
  my_wrmsr(0x1d9, 0x3, 0x0);
  printk("<1> Attempt #1 %llx.n", my_rdmsr(0x1d9));

  wrmsr(0x1d9, 0x1, 0x0);
  int low;
  int high;
  rdmsr(0x1d9, low, high);
  printk("<1> Attempt #2 %x, %x.n", low, high);
  return 0;
}

static void my_exit(void) {
  printk("<1> unloadn");
}

module_init(my_init);
module_exit(my_exit);
	
obj-m := hello.o
	
me@debian:~/code$ sudo make -C /usr/src/linux-headers-3.2.0-4-amd64/ M=`pwd` modules && sudo insmod hello.ko && sudo rmmod hello && sudo dmesg | tail                                                  
make: Entering directory `/usr/src/linux-headers-3.2.0-4-amd64'                                                                                                                                             
  Building modules, stage 2.                                                                                                                                                                                
  MODPOST 1 modules                                                                                                                                                                                         
make: Leaving directory `/usr/src/linux-headers-3.2.0-4-amd64'                                                                                                                                              
[ 2231.367819]  unload                                                                                                                                                                            
[ 2248.363261]  Attempt #1 0.                                                                                                                                                                               
[ 2248.363267]  Attempt #2 0, 0.                                                                                                                                                                            
[ 2248.378066]  unload                                                                                                                                                                                      
[ 2288.395359]  Attempt #1 0.                                                                                                                                                                               
[ 2288.395366]  Attempt #2 0, 0.                                                                                                                                                                            
[ 2288.409889]  unload                                                                                                                                                                                      
[ 3021.984748]  Attempt #1 0.                                                                                                                                                                               
[ 3021.984760]  Attempt #2 0, 0.                                                                                                                                                                            
[ 3022.017626]  unload