Skip to content
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
688 changes: 688 additions & 0 deletions .clang-format

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
14 changes: 14 additions & 0 deletions 01_Homework_proc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ifneq ($(KERNELRELEASE),)
# kbuild part of makefile
obj-m := my_module.o
else
# normal makefile

KDIR = /lib/modules/$(shell uname -r)/build

all:
make -C $(KDIR) M=$$PWD modules
clean:
make -C $(KDIR) M=$$PWD clean

endif
10 changes: 10 additions & 0 deletions 01_Homework_proc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Kernel modules: homework #2

Add proc functionality (read only handler)

Filelist

my_module.c

Makefile

100 changes: 100 additions & 0 deletions 01_Homework_proc/my_module.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// SPDX-License-Identifier: GPL-2.0+

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/uaccess.h>

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Yurii Bezkor");
MODULE_DESCRIPTION("Proc module");
MODULE_VERSION("0.1");

#define PROC_BUFFER_SIZE 100

static char procfs_buffer[PROC_BUFFER_SIZE] = { 0 };
static size_t procfs_buffer_size;

static struct proc_dir_entry *proc_file;
static struct proc_dir_entry *proc_dir;

#define PROC_FILE_NAME "hello"
#define PROC_DIR_NAME "bezkor"

static ssize_t procfs_read(struct file *file, char __user *buffer,
size_t count_bytes, loff_t *offset)
{
pr_info("Read info from procfs\n");

if (*offset > 0 || count_bytes < procfs_buffer_size) {
pr_err("Illegal buffer\n");
return 0;
}

if (copy_to_user(buffer, procfs_buffer, procfs_buffer_size)) {
pr_err("Error copy_to_user\n");
return -EFAULT;
};
*offset = procfs_buffer_size;
return procfs_buffer_size;
}

static ssize_t procfs_write(struct file *file, const char __user *buffer,
size_t count_bytes, loff_t *offset)
{
pr_info("Write info to procfs\n");

if (*offset > 0 || count_bytes > PROC_BUFFER_SIZE) {
pr_err("Illegal buffer\n");
return -EFAULT;
}

if (copy_from_user(procfs_buffer, buffer, count_bytes)) {
pr_err("Error copy_from_user\n");
return -EFAULT;
};

procfs_buffer_size = count_bytes;
pr_info("Buffer is %s\n", procfs_buffer);

return procfs_buffer_size;
}

static const struct file_operations fops = {
.owner = THIS_MODULE,
.read = procfs_read,
.write = procfs_write };

static int __init hello_init(void)
{
proc_dir = proc_mkdir(PROC_DIR_NAME, NULL);
if (proc_dir == NULL) {
pr_err("PROC module: Could not create /proc/%s/ folder !\n",
PROC_DIR_NAME);
return -ENOMEM;
}
proc_file = proc_create(PROC_FILE_NAME, 0666, proc_dir, &fops);
if (proc_file == NULL) {
pr_err("PROC module: Could not create /proc/%s/%s !\n",
PROC_DIR_NAME, PROC_FILE_NAME);
proc_remove(proc_file);
proc_remove(proc_dir);
return -ENOMEM;
}
procfs_buffer_size = 0;
pr_info("PROC module: Hello /proc/%s/%s !\n", PROC_DIR_NAME,
PROC_FILE_NAME);
return 0;
}

static void __exit hello_exit(void)
{
proc_remove(proc_file);
proc_remove(proc_dir);
pr_info("PROC module: Goodbye /proc/%s/%s !\n", PROC_DIR_NAME,
PROC_FILE_NAME);
}

module_init(hello_init);
module_exit(hello_exit);
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

CF = clang-format-12 -i

.PHONY: format check-format check-arg

check-arg:
@if test "$(DIR)" = "" ; then \
echo DIR not set; \
echo Use:;\
echo make cmd DIR=...;\
echo ;\
exit 1; \
fi

format: check-arg
@echo format SRC
$(CF) $(DIR)/*.[ch]

check-format: check-arg
@echo "\n\e[32m*** Check format of source code ***\e[0m\n"
@utils/checkpatch.pl --no-tree -f $(DIR)/*.[ch]
14 changes: 14 additions & 0 deletions Task00_HW_kernel_module/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ifneq ($(KERNELRELEASE),)
# kbuild part of makefile
obj-m := hello.o
else
# normal makefile

KDIR = /lib/modules/$(shell uname -r)/build

all:
make -C $(KDIR) M=$$PWD modules
clean:
make -C $(KDIR) M=$$PWD clean

endif
41 changes: 41 additions & 0 deletions Task00_HW_kernel_module/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Home work
[x] Write hello_world module: fill module author, description, version e.t.c.

[x] Write into the console

- [x] module initialization;

- [x] module de-initialization;

[x] Use module parameters to write simple calculator: module should print sum,
substraction and multiplication of two numbers passed as parameters.

[x] Don’t forget to use checkpatch.pl – all not-corresponding drivers will be
rejected.

[x] Github: hello.c, hello.ko, Makefile

## Sample commands

```shell
insmod hello.ko
rmmod hello
```

```shell
insmod hello.ko sum=1,2
```

```shell
insmod hello.ko sum=1,2 subst=2,4 prod=3,3
```

dmesg results:

```shell
[ +7,950982] HELLO: Hello Kernel!
[ +0,000001] SUM(1,2) = 3
[ +0,000001] SUBST(2,4) = -2
[ +0,000000] PROD(3,3) = 9
[ +7,678936] HELLO: Goodbye Kernel!
```
74 changes: 74 additions & 0 deletions Task00_HW_kernel_module/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// SPDX-License-Identifier: GPL-2.0+

#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Yurii Bezkor");
MODULE_DESCRIPTION("Hello world kernel module");
MODULE_VERSION("0.2");

static int sum[2];
static int sum_count;
module_param_array(sum, int, &sum_count, 0);
MODULE_PARM_DESC(sum, "Sum of two integers");

static int subst[2];
static int subst_count;
module_param_array(subst, int, &subst_count, 0);
MODULE_PARM_DESC(subst, "Substraction of two integers");

static int prod[2];
static int prod_count;
module_param_array(prod, int, &prod_count, 0);
MODULE_PARM_DESC(prod, "Substraction of two integers");

static int __init hello_init(void)
{
int result;

pr_info("HELLO: Hello Kernel!\n");
if (sum_count) {
if (sum_count == 2) {
result = sum[0] + sum[1];
pr_info("SUM(%d,%d) = %d\n", sum[0], sum[1], result);
} else {
pr_err("SUM passed %d parameters, must be 2!\n",
sum_count);
return -EINVAL;
}
}

if (subst_count) {
if (subst_count == 2) {
result = subst[0] - subst[1];
pr_info("SUBST(%d,%d) = %d\n", subst[0], subst[1], result);
} else {
pr_err("SUBST passed %d parameters, must be 2!\n",
subst_count);
return -EINVAL;
}
}

if (prod_count) {
if (prod_count == 2) {
result = prod[0] * prod[1];
pr_info("PROD(%d,%d) = %d\n", prod[0], prod[1], result);
} else {
pr_err("PRODT passed %d parameters, must be 2!\n",
prod_count);
return -EINVAL;
}
}

return 0;
}
static void __exit hello_exit(void)
{
pr_info("HELLO: Goodbye Kernel!\n");
}

module_init(hello_init);
module_exit(hello_exit);
Loading