Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson3: Implement Linux kernel module #173

Open
wants to merge 1 commit into
base: ViacheslavHolubnychyi
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions 03_module/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
obj-m += mymodule.o

# Build for current kernel

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

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


# Build for another kernel

all:
make -C /home/user/Files_Slava/GL_dev/C/linux-kernel/buildroot-2021.02.9/output/build/linux-5.10.7/ M=$(PWD) modules

clean:
make -C /home/user/Files_Slava/GL_dev/C/linux-kernel/buildroot-2021.02.9/output/build/linux-5.10.7/ M=$(PWD) clean
34 changes: 34 additions & 0 deletions 03_module/mymodule.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

static const char* module_name = "mymodule";
static int a = 0;
static int b = 0;
module_param(a, int, 0);
module_param(b, int, 0);

static int __init hello_init(void)
{
printk(KERN_INFO "%s: Hello world!\n", module_name);
printk("%s: a = %i\n", module_name, a);
printk("%s: b = %i\n", module_name, b);
printk("%s: a + b = %i\n", module_name, a+b);
return 0;
}

static void __exit hello_exit(void)
{
printk(KERN_INFO "%s: Goodbye, world!\n", module_name);
printk("%s: a - b = %i\n", module_name, a-b);
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("ViacheslavHolubnychyi");
MODULE_DESCRIPTION("simple description");
MODULE_VERSION("0.1");

MODULE_INFO(intree, "Y");
34 changes: 34 additions & 0 deletions 03_module/result_kernel_log.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Welcome to Buildroot
buildroot login: user
Password:
$ pwd
/home/user
$ ls
mymodule.ko
$ su
$ insmod mymodule.ko
mymodule: Hello world!
mymodule: a = 0
mymodule: b = 0
mymodule: a + b = 0
$ rmmod mymodule
mymodule: Goodbye, world!
mymodule: a - b = 0
$ insmod mymodule.ko a=2
mymodule: Hello world!
mymodule: a = 2
mymodule: b = 0
mymodule: a + b = 2
$ rmmod mymodule
mymodule: Goodbye, world!
mymodule: a - b = 2
$ insmod mymodule.ko a=7 b=4
mymodule: Hello world!
mymodule: a = 7
mymodule: b = 4
mymodule: a + b = 11
$ rmmod mymodule
mymodule: Goodbye, world!
mymodule: a - b = 3
$