pa evo i ovde:
cr0_proc.c:
Code:
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
static int proc_show(struct seq_file *m, void *v) {
unsigned long long val;
asm (
"movq %%cr0,%%rax\n"
"movq %%rax,%0\n"
:"=r"(val)
:
:"%rax"
);
seq_printf(m, "cr0: %llx\n",val);
return 0;
}
static int proc_open(struct inode *inode, struct file *file) {
return single_open(file, proc_show, NULL);
}
static const struct file_operations proc_fops = {
.owner = THIS_MODULE,
.open = proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int __init proc_init(void) {
proc_create("cr0_proc", 0, NULL, &proc_fops);
printk("cr0_proc inserted\n");
return 0;
}
static void __exit proc_exit(void) {
remove_proc_entry("cr0_proc", NULL);
printk("cr0_proc removed\n");
}
MODULE_LICENSE("GPL");
module_init(proc_init);
module_exit(proc_exit);
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
static int proc_show(struct seq_file *m, void *v) {
unsigned long long val;
asm (
"movq %%cr0,%%rax\n"
"movq %%rax,%0\n"
:"=r"(val)
:
:"%rax"
);
seq_printf(m, "cr0: %llx\n",val);
return 0;
}
static int proc_open(struct inode *inode, struct file *file) {
return single_open(file, proc_show, NULL);
}
static const struct file_operations proc_fops = {
.owner = THIS_MODULE,
.open = proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int __init proc_init(void) {
proc_create("cr0_proc", 0, NULL, &proc_fops);
printk("cr0_proc inserted\n");
return 0;
}
static void __exit proc_exit(void) {
remove_proc_entry("cr0_proc", NULL);
printk("cr0_proc removed\n");
}
MODULE_LICENSE("GPL");
module_init(proc_init);
module_exit(proc_exit);
Makefile:
Code:
ifneq ($(KERNELRELEASE),)
obj-m := cr0_proc.o
else
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
rm -r -f .tmp_versions *.mod.c .*.cmd *.o *.symvers
endif
ifneq ($(KERNELRELEASE),)
obj-m := cr0_proc.o
else
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
rm -r -f .tmp_versions *.mod.c .*.cmd *.o *.symvers
endif
build procedura:
Code:
~/examples/kmodule >>> make [2]
make -C /lib/modules/5.3.12-1-CUSTOM/build SUBDIRS=/home/bmaxa/examples/kmodule modules
make[1]: Entering directory '/usr/lib/modules/5.3.12-1-CUSTOM/build'
Makefile:213: ================= WARNING ================
Makefile:214: 'SUBDIRS' will be removed after Linux 5.3
Makefile:215:
Makefile:216: If you are building an individual subdirectory
Makefile:217: in the kernel tree, you can do like this:
Makefile:218: $ make path/to/dir/you/want/to/build/
Makefile:219: (Do not forget the trailing slash)
Makefile:220:
Makefile:221: If you are building an external module,
Makefile:222: Please use 'M=' or 'KBUILD_EXTMOD' instead
Makefile:223: ==========================================
CC [M] /home/bmaxa/examples/kmodule/cr0_proc.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/bmaxa/examples/kmodule/cr0_proc.mod.o
LD [M] /home/bmaxa/examples/kmodule/cr0_proc.ko
make[1]: Leaving directory '/usr/lib/modules/5.3.12-1-CUSTOM/build'
rm -r -f .tmp_versions *.mod.c .*.cmd *.o *.symvers
~/examples/kmodule >>> make [2]
make -C /lib/modules/5.3.12-1-CUSTOM/build SUBDIRS=/home/bmaxa/examples/kmodule modules
make[1]: Entering directory '/usr/lib/modules/5.3.12-1-CUSTOM/build'
Makefile:213: ================= WARNING ================
Makefile:214: 'SUBDIRS' will be removed after Linux 5.3
Makefile:215:
Makefile:216: If you are building an individual subdirectory
Makefile:217: in the kernel tree, you can do like this:
Makefile:218: $ make path/to/dir/you/want/to/build/
Makefile:219: (Do not forget the trailing slash)
Makefile:220:
Makefile:221: If you are building an external module,
Makefile:222: Please use 'M=' or 'KBUILD_EXTMOD' instead
Makefile:223: ==========================================
CC [M] /home/bmaxa/examples/kmodule/cr0_proc.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/bmaxa/examples/kmodule/cr0_proc.mod.o
LD [M] /home/bmaxa/examples/kmodule/cr0_proc.ko
make[1]: Leaving directory '/usr/lib/modules/5.3.12-1-CUSTOM/build'
rm -r -f .tmp_versions *.mod.c .*.cmd *.o *.symvers
koriscenje:
Code:
~/examples/kmodule >>> sudo insmod ./cr0_proc.ko
~/examples/kmodule >>> cat /proc/cr0_proc
cr0: 80050033
~/examples/kmodule >>> sudo rmmod cr0_proc
~/examples/kmodule >>> sudo insmod ./cr0_proc.ko
~/examples/kmodule >>> cat /proc/cr0_proc
cr0: 80050033
~/examples/kmodule >>> sudo rmmod cr0_proc
Ovo se dalje moze prosiriti da se izabere jezgro sa kog se cita cr0 :P
ali to ostavljam za excercize to the reader :P