commit 8d1b1537bec050247355ea4c3573f893fb74d1da Author: rhinemann Date: Wed Jan 1 18:04:12 2025 +0200 first commit diff --git a/lab_4/Kbuild b/lab_4/Kbuild new file mode 100644 index 0000000..263fe65 --- /dev/null +++ b/lab_4/Kbuild @@ -0,0 +1,3 @@ + +# kbuild part of makefile +obj-m := hello.o diff --git a/lab_4/Makefile b/lab_4/Makefile new file mode 100644 index 0000000..3b0a635 --- /dev/null +++ b/lab_4/Makefile @@ -0,0 +1,12 @@ + +ifneq ($(KERNELRELEASE),) +include Kbuild +else +# normal makefile +KDIR ?= /lib/modules/`uname -r`/build + +default: + $(MAKE) -C $(KDIR) M=$$PWD +clean: + $(MAKE) -C $(KDIR) M=$$PWD clean +endif diff --git a/lab_4/hello.c b/lab_4/hello.c new file mode 100644 index 0000000..d602e29 --- /dev/null +++ b/lab_4/hello.c @@ -0,0 +1,131 @@ + +/* + * Copyright (c) 2017, GlobalLogic Ukraine LLC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the GlobalLogic. + * 4. Neither the name of the GlobalLogic nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY GLOBALLOGIC UKRAINE LLC ``AS IS`` AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL GLOBALLOGIC UKRAINE LLC BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include + +MODULE_AUTHOR("Serhii Popovych "); +MODULE_DESCRIPTION("Hello, world in Linux Kernel Training"); +MODULE_LICENSE("Dual BSD/GPL"); + +struct TimingList { + struct TimingList *next; + ktime_t time; +}; + +static struct TimingList *list_head; +static unsigned int message_count = 0; + +static void init_list(void) +{ + list_head = kmalloc(sizeof(struct TimingList), GFP_KERNEL); + + list_head->next = NULL; + list_head->time = ktime_get(); +} + +static void insert_time(void) +{ + struct TimingList *last_node = list_head; + struct TimingList *new_node = kmalloc(sizeof(struct TimingList), + GFP_KERNEL); + + while (last_node->next != NULL) + last_node = last_node->next; + + + new_node->next = NULL; + new_node->time = ktime_get(); + + last_node->next = new_node; +} + +static void print_time(ktime_t *time) +{ + printk(KERN_EMERG "%lld\n", *time); +} + +void delete_list(void) +{ + struct TimingList *current_node = list_head; + struct TimingList *next_node = NULL; + + list_head = NULL; + + while (current_node != NULL) { + print_time(&(current_node->time)); + + next_node = current_node->next; + kfree(current_node); + current_node = next_node; + } +} + +static void update_timings(void) +{ + if (list_head == NULL) + init_list(); + else + insert_time(); +} + +static int __init hello_init(void) +{ + int i; + + if (message_count > 10) { + printk(KERN_EMERG "Error: msg_count too large (%d > 10)\n", message_count); + return -EINVAL; + } + + if (message_count == 0 || message_count >= 5) + printk(KERN_WARNING "Warning: message_count is %d (message_count is 0 or 5 <= message_count <= 10)\n", message_count); + + for (i = 0; i < message_count; i++) { + update_timings(); + + printk(KERN_EMERG "Hello, world!\n"); + } + return 0; +} + +static void __exit hello_exit(void) +{ + delete_list(); +} + +module_param(message_count, uint, 0444); +MODULE_PARM_DESC(message_count, "Amount of Hello World!-s to print on load"); + +module_init(hello_init); +module_exit(hello_exit);