From 6053de4de91f3207046afa9b4a4a58d7ce451510 Mon Sep 17 00:00:00 2001 From: Sazan1209 Date: Tue, 21 May 2024 17:04:02 +0300 Subject: [PATCH 1/5] made task 2 --- module2/Makefile | 9 +++++++ module2/compile.sh | 11 +++++++++ module2/counter.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 module2/Makefile create mode 100644 module2/compile.sh create mode 100644 module2/counter.c diff --git a/module2/Makefile b/module2/Makefile new file mode 100644 index 0000000..56bd7b6 --- /dev/null +++ b/module2/Makefile @@ -0,0 +1,9 @@ +obj-m += counter.o + +ABS_PATH_TO_VROOT ?= ../initramfs +PATH_TO_MODULE_BUILD=$(ABS_PATH_TO_VROOT)/lib/modules/6.7.4/build + +all: + make -C "$(PATH_TO_MODULE_BUILD)" M="$(PWD)" modules +clean: + make -C "$(PATH_TO_MODULE_BUILD)" M="$(PWD)" clean \ No newline at end of file diff --git a/module2/compile.sh b/module2/compile.sh new file mode 100644 index 0000000..93c8dbe --- /dev/null +++ b/module2/compile.sh @@ -0,0 +1,11 @@ +cp -r ../module2 ../../ || exit +cd ../../module2 || exit +make clean || exit +make all || exit +cp counter.ko ../initramfs/lib/modules/6.7.4/ || exit +cd ../initramfs || exit +find . | cpio -ov --format=newc | gzip -9 > ../try/initramfs.gz || exit +cd .. +# cleanup +rm -r module2 +rm initramfs/lib/modules/6.7.4/counter.ko diff --git a/module2/counter.c b/module2/counter.c new file mode 100644 index 0000000..4499d91 --- /dev/null +++ b/module2/counter.c @@ -0,0 +1,61 @@ +// +// Created by mikhail on 21.05.24. +// +#include +#include +#include +#include +#include + +MODULE_LICENSE("MIT"); +MODULE_AUTHOR("Me"); +MODULE_DESCRIPTION("Key press counter"); + +// Counter + +static atomic_t press_count = ATOMIC_INIT(0); + +irqreturn_t irq_handler(int irq, void* dev_id) { + atomic_inc(&press_count); + return IRQ_NONE; +} + +static struct timer_list timer; + +// Timer callback + +static void reschedule_timer(void) { + mod_timer(&timer, jiffies + msecs_to_jiffies(60000)); +} + +void callback(struct timer_list* _) { + int number = atomic_read(&press_count); + pr_info("Characters typed: %d\n", number); + reschedule_timer(); +} + +// Module funcs + +static int __init counter_init(void) { + timer_setup(&timer, callback, 0); + reschedule_timer(); + int err = request_irq(1, // PS/2 irq + irq_handler, + IRQF_SHARED, + "Key counter", + (void*) irq_handler); + if (err) { + del_timer(&timer); + pr_err("request_irq failed with errcode %d\n.", err); + return err; + } + return 0; +} + +static void __exit counter_exit(void) { + free_irq(1, (void*) irq_handler); + del_timer(&timer); +} + +module_init(counter_init); +module_exit(counter_exit); \ No newline at end of file From 84aafcd138068ab767560739ae5261381d2b4286 Mon Sep 17 00:00:00 2001 From: Sazan1209 Date: Tue, 21 May 2024 17:57:38 +0300 Subject: [PATCH 2/5] task3 --- task3/Readme.md | 1 + task3/patch | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 task3/Readme.md create mode 100644 task3/patch diff --git a/task3/Readme.md b/task3/Readme.md new file mode 100644 index 0000000..f8fb28b --- /dev/null +++ b/task3/Readme.md @@ -0,0 +1 @@ +Ожидается, что исходники ядра лежат в dev/linux-6.7.4/ diff --git a/task3/patch b/task3/patch new file mode 100644 index 0000000..3a1f3ea --- /dev/null +++ b/task3/patch @@ -0,0 +1,84 @@ +diff --git a/dev/linux-6.7.4/fs/proc/base.c b/dev/linux-6.7.4/fs/proc/base.c +index dd31e3b..3f1b03e 100644 +--- a/dev/linux-6.7.4/fs/proc/base.c ++++ b/dev/linux-6.7.4/fs/proc/base.c +@@ -488,6 +488,17 @@ static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns, + } + #endif + ++static int proc_pid_sched_counter(struct seq_file *m, struct pid_namespace *ns, ++ struct pid *pid, struct task_struct *task) ++{ ++ if (unlikely(!sched_info_on())) ++ seq_puts(m, "0\n"); ++ else ++ seq_printf(m, "%d\n", atomic_read(&task->sched_counter)); ++ ++ return 0; ++} ++ + #ifdef CONFIG_LATENCYTOP + static int lstats_show_proc(struct seq_file *m, void *v) + { +@@ -3293,6 +3304,7 @@ static const struct pid_entry tgid_base_stuff[] = { + REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations), + REG("pagemap", S_IRUSR, proc_pagemap_operations), + #endif ++ ONE("sched_counter", S_IRUGO, proc_pid_sched_counter), + #ifdef CONFIG_SECURITY + DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), + #endif +@@ -3642,6 +3654,7 @@ static const struct pid_entry tid_base_stuff[] = { + REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations), + REG("pagemap", S_IRUSR, proc_pagemap_operations), + #endif ++ ONE("sched_counter", S_IRUGO, proc_pid_sched_counter), + #ifdef CONFIG_SECURITY + DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), + #endif +diff --git a/dev/linux-6.7.4/include/linux/sched.h b/dev/linux-6.7.4/include/linux/sched.h +index 292c316..f6e71ab 100644 +--- a/dev/linux-6.7.4/include/linux/sched.h ++++ b/dev/linux-6.7.4/include/linux/sched.h +@@ -751,6 +751,9 @@ struct task_struct { + */ + struct thread_info thread_info; + #endif ++ /* Task 3 field */ ++ atomic_t sched_counter; ++ + unsigned int __state; + + /* saved state for "spinlock sleepers" */ +@@ -1385,6 +1388,7 @@ struct task_struct { + #endif + + #ifdef CONFIG_FUNCTION_GRAPH_TRACER ++ + /* Index of current stored address in ret_stack: */ + int curr_ret_stack; + int curr_ret_depth; +diff --git a/dev/linux-6.7.4/init/init_task.c b/dev/linux-6.7.4/init/init_task.c +index 5727d42..67a83ec 100644 +--- a/dev/linux-6.7.4/init/init_task.c ++++ b/dev/linux-6.7.4/init/init_task.c +@@ -71,6 +71,7 @@ struct task_struct init_task + .thread_info = INIT_THREAD_INFO(init_task), + .stack_refcount = REFCOUNT_INIT(1), + #endif ++ .sched_counter = ATOMIC_INIT(0), + .__state = 0, + .stack = init_stack, + .usage = REFCOUNT_INIT(2), +diff --git a/dev/linux-6.7.4/kernel/sched/core.c b/dev/linux-6.7.4/kernel/sched/core.c +index a708d22..1d7d4d7 100644 +--- a/dev/linux-6.7.4/kernel/sched/core.c ++++ b/dev/linux-6.7.4/kernel/sched/core.c +@@ -6650,6 +6650,7 @@ static void __sched notrace __schedule(unsigned int sched_mode) + } + + next = pick_next_task(rq, prev, &rf); ++ atomic_inc(&next->sched_counter); + clear_tsk_need_resched(prev); + clear_preempt_need_resched(); + #ifdef CONFIG_SCHED_DEBUG From 567b966a7a706c8fcd18898f75ca84dd509292de Mon Sep 17 00:00:00 2001 From: Sazan1209 Date: Tue, 21 May 2024 17:58:38 +0300 Subject: [PATCH 3/5] Revert "made task 2" This reverts commit 6053de4de91f3207046afa9b4a4a58d7ce451510. --- module2/Makefile | 9 ------- module2/compile.sh | 11 --------- module2/counter.c | 61 ---------------------------------------------- 3 files changed, 81 deletions(-) delete mode 100644 module2/Makefile delete mode 100644 module2/compile.sh delete mode 100644 module2/counter.c diff --git a/module2/Makefile b/module2/Makefile deleted file mode 100644 index 56bd7b6..0000000 --- a/module2/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -obj-m += counter.o - -ABS_PATH_TO_VROOT ?= ../initramfs -PATH_TO_MODULE_BUILD=$(ABS_PATH_TO_VROOT)/lib/modules/6.7.4/build - -all: - make -C "$(PATH_TO_MODULE_BUILD)" M="$(PWD)" modules -clean: - make -C "$(PATH_TO_MODULE_BUILD)" M="$(PWD)" clean \ No newline at end of file diff --git a/module2/compile.sh b/module2/compile.sh deleted file mode 100644 index 93c8dbe..0000000 --- a/module2/compile.sh +++ /dev/null @@ -1,11 +0,0 @@ -cp -r ../module2 ../../ || exit -cd ../../module2 || exit -make clean || exit -make all || exit -cp counter.ko ../initramfs/lib/modules/6.7.4/ || exit -cd ../initramfs || exit -find . | cpio -ov --format=newc | gzip -9 > ../try/initramfs.gz || exit -cd .. -# cleanup -rm -r module2 -rm initramfs/lib/modules/6.7.4/counter.ko diff --git a/module2/counter.c b/module2/counter.c deleted file mode 100644 index 4499d91..0000000 --- a/module2/counter.c +++ /dev/null @@ -1,61 +0,0 @@ -// -// Created by mikhail on 21.05.24. -// -#include -#include -#include -#include -#include - -MODULE_LICENSE("MIT"); -MODULE_AUTHOR("Me"); -MODULE_DESCRIPTION("Key press counter"); - -// Counter - -static atomic_t press_count = ATOMIC_INIT(0); - -irqreturn_t irq_handler(int irq, void* dev_id) { - atomic_inc(&press_count); - return IRQ_NONE; -} - -static struct timer_list timer; - -// Timer callback - -static void reschedule_timer(void) { - mod_timer(&timer, jiffies + msecs_to_jiffies(60000)); -} - -void callback(struct timer_list* _) { - int number = atomic_read(&press_count); - pr_info("Characters typed: %d\n", number); - reschedule_timer(); -} - -// Module funcs - -static int __init counter_init(void) { - timer_setup(&timer, callback, 0); - reschedule_timer(); - int err = request_irq(1, // PS/2 irq - irq_handler, - IRQF_SHARED, - "Key counter", - (void*) irq_handler); - if (err) { - del_timer(&timer); - pr_err("request_irq failed with errcode %d\n.", err); - return err; - } - return 0; -} - -static void __exit counter_exit(void) { - free_irq(1, (void*) irq_handler); - del_timer(&timer); -} - -module_init(counter_init); -module_exit(counter_exit); \ No newline at end of file From f5d15148f93c71962d1738f8242e4218f0b8185e Mon Sep 17 00:00:00 2001 From: Sazan1209 Date: Tue, 21 May 2024 18:08:54 +0300 Subject: [PATCH 4/5] Revert "task3" This reverts commit 84aafcd138068ab767560739ae5261381d2b4286. --- task3/Readme.md | 1 - task3/patch | 84 ------------------------------------------------- 2 files changed, 85 deletions(-) delete mode 100644 task3/Readme.md delete mode 100644 task3/patch diff --git a/task3/Readme.md b/task3/Readme.md deleted file mode 100644 index f8fb28b..0000000 --- a/task3/Readme.md +++ /dev/null @@ -1 +0,0 @@ -Ожидается, что исходники ядра лежат в dev/linux-6.7.4/ diff --git a/task3/patch b/task3/patch deleted file mode 100644 index 3a1f3ea..0000000 --- a/task3/patch +++ /dev/null @@ -1,84 +0,0 @@ -diff --git a/dev/linux-6.7.4/fs/proc/base.c b/dev/linux-6.7.4/fs/proc/base.c -index dd31e3b..3f1b03e 100644 ---- a/dev/linux-6.7.4/fs/proc/base.c -+++ b/dev/linux-6.7.4/fs/proc/base.c -@@ -488,6 +488,17 @@ static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns, - } - #endif - -+static int proc_pid_sched_counter(struct seq_file *m, struct pid_namespace *ns, -+ struct pid *pid, struct task_struct *task) -+{ -+ if (unlikely(!sched_info_on())) -+ seq_puts(m, "0\n"); -+ else -+ seq_printf(m, "%d\n", atomic_read(&task->sched_counter)); -+ -+ return 0; -+} -+ - #ifdef CONFIG_LATENCYTOP - static int lstats_show_proc(struct seq_file *m, void *v) - { -@@ -3293,6 +3304,7 @@ static const struct pid_entry tgid_base_stuff[] = { - REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations), - REG("pagemap", S_IRUSR, proc_pagemap_operations), - #endif -+ ONE("sched_counter", S_IRUGO, proc_pid_sched_counter), - #ifdef CONFIG_SECURITY - DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), - #endif -@@ -3642,6 +3654,7 @@ static const struct pid_entry tid_base_stuff[] = { - REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations), - REG("pagemap", S_IRUSR, proc_pagemap_operations), - #endif -+ ONE("sched_counter", S_IRUGO, proc_pid_sched_counter), - #ifdef CONFIG_SECURITY - DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), - #endif -diff --git a/dev/linux-6.7.4/include/linux/sched.h b/dev/linux-6.7.4/include/linux/sched.h -index 292c316..f6e71ab 100644 ---- a/dev/linux-6.7.4/include/linux/sched.h -+++ b/dev/linux-6.7.4/include/linux/sched.h -@@ -751,6 +751,9 @@ struct task_struct { - */ - struct thread_info thread_info; - #endif -+ /* Task 3 field */ -+ atomic_t sched_counter; -+ - unsigned int __state; - - /* saved state for "spinlock sleepers" */ -@@ -1385,6 +1388,7 @@ struct task_struct { - #endif - - #ifdef CONFIG_FUNCTION_GRAPH_TRACER -+ - /* Index of current stored address in ret_stack: */ - int curr_ret_stack; - int curr_ret_depth; -diff --git a/dev/linux-6.7.4/init/init_task.c b/dev/linux-6.7.4/init/init_task.c -index 5727d42..67a83ec 100644 ---- a/dev/linux-6.7.4/init/init_task.c -+++ b/dev/linux-6.7.4/init/init_task.c -@@ -71,6 +71,7 @@ struct task_struct init_task - .thread_info = INIT_THREAD_INFO(init_task), - .stack_refcount = REFCOUNT_INIT(1), - #endif -+ .sched_counter = ATOMIC_INIT(0), - .__state = 0, - .stack = init_stack, - .usage = REFCOUNT_INIT(2), -diff --git a/dev/linux-6.7.4/kernel/sched/core.c b/dev/linux-6.7.4/kernel/sched/core.c -index a708d22..1d7d4d7 100644 ---- a/dev/linux-6.7.4/kernel/sched/core.c -+++ b/dev/linux-6.7.4/kernel/sched/core.c -@@ -6650,6 +6650,7 @@ static void __sched notrace __schedule(unsigned int sched_mode) - } - - next = pick_next_task(rq, prev, &rf); -+ atomic_inc(&next->sched_counter); - clear_tsk_need_resched(prev); - clear_preempt_need_resched(); - #ifdef CONFIG_SCHED_DEBUG From 655470dcafc693c7285f12d34544221d20492d7e Mon Sep 17 00:00:00 2001 From: Sazan1209 Date: Tue, 21 May 2024 20:22:25 +0300 Subject: [PATCH 5/5] task4 --- module3/Makefile | 9 +++ module3/compile.sh | 11 +++ module3/proc_mmaneg.c | 153 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 module3/Makefile create mode 100644 module3/compile.sh create mode 100644 module3/proc_mmaneg.c diff --git a/module3/Makefile b/module3/Makefile new file mode 100644 index 0000000..2dd3934 --- /dev/null +++ b/module3/Makefile @@ -0,0 +1,9 @@ +obj-m += proc_mmaneg.o + +ABS_PATH_TO_VROOT ?= ../initramfs +PATH_TO_MODULE_BUILD=$(ABS_PATH_TO_VROOT)/lib/modules/6.7.4/build + +all: + make -C "$(PATH_TO_MODULE_BUILD)" M="$(PWD)" modules +clean: + make -C "$(PATH_TO_MODULE_BUILD)" M="$(PWD)" clean \ No newline at end of file diff --git a/module3/compile.sh b/module3/compile.sh new file mode 100644 index 0000000..51eb61f --- /dev/null +++ b/module3/compile.sh @@ -0,0 +1,11 @@ +cp -r ../module3 ../../ || exit +cd ../../module3 || exit +make clean || exit +make all || exit +cp proc_mmaneg.ko ../initramfs/lib/modules/6.7.4/ || exit +cd ../initramfs || exit +find . | cpio -ov --format=newc | gzip -9 > ../try/initramfs.gz || exit +cd .. +# cleanup +rm -r module3 +rm initramfs/lib/modules/6.7.4/proc_mmaneg.ko diff --git a/module3/proc_mmaneg.c b/module3/proc_mmaneg.c new file mode 100644 index 0000000..780c611 --- /dev/null +++ b/module3/proc_mmaneg.c @@ -0,0 +1,153 @@ +// +// Created by mikhail on 21.05.24. +// + +// Module registration +#include +#include +#include + +// Creation of character device +#include +#include + +// Procfs +#include +#include +#include +#include +#include +#include +#include + +//------------------------------------------------------------------------------ + + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Me"); +MODULE_DESCRIPTION("Proc mmaneg module"); + +// Variables + +#define PROCFS_MAX_SIZE 1024 +#define PROCFS_NAME "mmaneg" + +static struct proc_dir_entry* proc_file; +static char mmaneg_buf[PROCFS_MAX_SIZE + 1]; + +static void listvma(void) { + struct task_struct* task = current; + struct mm_struct* mm = get_task_mm(task); + struct vm_area_struct* vma; + + mmap_read_lock(mm); + VMA_ITERATOR(iter, mm, 0); + pr_info("VMA for process %d:\n", task->pid); + for_each_vma(iter, vma) { + pr_info("Start: 0x%lx, End: 0x%lx\n", vma->vm_start, vma->vm_end); + } + mmap_read_unlock(mm); +} + +static void findpage(unsigned long addr) { + if (!virt_addr_valid(addr)) { + pr_warn("Virtual addr 0x%lx is invalid", addr); + return; + } + struct page* page = virt_to_page(addr); + pr_info("VA: 0x%lx, PA: 0x%llx\n", addr, page_to_phys(page)); + put_page(page); +} + +static void writeval(unsigned long addr, unsigned long val) { + if (put_user(val, (unsigned long*) addr)) { + pr_warn("Couldn't write to addr 0x%lx)\n", addr); + } else { + pr_info("Wrote %lu to addr 0x%lx", val, addr); + } +} + +static ssize_t mmaneg_write(__always_unused struct file* file, + const char __user* buf, + size_t len, + __always_unused loff_t* pos) { + if (len > PROCFS_MAX_SIZE) { + len = PROCFS_MAX_SIZE; + } + if (copy_from_user(mmaneg_buf, buf, len)) { + return -EFAULT; + } + mmaneg_buf[len] = '\0'; + unsigned long addr = 0; + if (strncmp(mmaneg_buf, "listvma", 7) == 0) { + listvma(); + } else if (strncmp(mmaneg_buf, "findpage", 8) == 0) { + if (sscanf(mmaneg_buf + 9, "%lx", &addr) != 1) { + goto unrecognized_arguments; + } + findpage(addr); + } else if (strncmp(mmaneg_buf, "writeval", 8) == 0) { + unsigned long val = 0; + if (sscanf(mmaneg_buf + 9, "%lx %lu", &addr, &val) != 2) { + goto unrecognized_arguments; + } + writeval(addr, val); + } else { + pr_warn("Invalid command: %s\n", mmaneg_buf); + return -EINVAL; + } + return (ssize_t) len; + + unrecognized_arguments: + pr_warn("Invalid arguments: %s\n", mmaneg_buf); + return -EINVAL; +} + +// Dummy funcs +static int mmaneg_open(__always_unused struct inode* inode, + __always_unused struct file* file) { + return 0; +} + +static int mmaneg_release(__always_unused struct inode* inode, + __always_unused struct file* file) { + return 0; +} + +static loff_t mmaneg_lseek(__always_unused struct file* file, + __always_unused loff_t loff, + __always_unused int whence) { + return 0; +} + +static ssize_t mmaneg_read(__always_unused struct file* file, + __always_unused char __user* buf, + __always_unused size_t len, + __always_unused loff_t* pos) { + return -EACCES; +} + +static struct proc_ops mmaneg_fops = + { + .proc_open = mmaneg_open, + .proc_read = mmaneg_read, + .proc_write = mmaneg_write, + .proc_lseek = mmaneg_lseek, + .proc_release = mmaneg_release, + }; + +static int __init mmaneg_init(void) { + proc_file = proc_create(PROCFS_NAME, 222, NULL, &mmaneg_fops); + if (!proc_file) { + pr_err("Failed to create proc file (for some reason)"); + return -ENOMEM; + } + return 0; +} + +static void __exit mmaneg_exit(void) { + remove_proc_entry(PROCFS_NAME, NULL); +} + +module_init(mmaneg_init) +module_exit(mmaneg_exit) \ No newline at end of file