Lab 6
This commit is contained in:
		
							parent
							
								
									dcf7056a24
								
							
						
					
					
						commit
						850591ae2c
					
				
							
								
								
									
										3
									
								
								lab_6/Kbuild
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								lab_6/Kbuild
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
			
		||||
# kbuild part of makefile
 | 
			
		||||
obj-m   := hello1.o hello2.o
 | 
			
		||||
ccflags-y := -I$(obj)/inc -g
 | 
			
		||||
							
								
								
									
										16
									
								
								lab_6/Makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								lab_6/Makefile
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,16 @@
 | 
			
		||||
 | 
			
		||||
ifneq ($(KERNELRELEASE),)
 | 
			
		||||
include Kbuild
 | 
			
		||||
else
 | 
			
		||||
# normal makefile
 | 
			
		||||
KDIR ?= /lib/modules/`uname -r`/build
 | 
			
		||||
 | 
			
		||||
default:
 | 
			
		||||
	$(MAKE) -C $(KDIR) M=$$PWD
 | 
			
		||||
	cp hello1.ko hello1.ko.unstripped
 | 
			
		||||
	$(CROSS_COMPILE)strip -g hello1.ko
 | 
			
		||||
 | 
			
		||||
clean:
 | 
			
		||||
	$(MAKE) -C $(KDIR) M=$$PWD clean
 | 
			
		||||
 | 
			
		||||
endif
 | 
			
		||||
							
								
								
									
										133
									
								
								lab_6/hello1.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										133
									
								
								lab_6/hello1.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,133 @@
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * 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 <linux/init.h>
 | 
			
		||||
#include <linux/module.h>
 | 
			
		||||
#include <linux/printk.h>
 | 
			
		||||
#include <linux/slab.h>
 | 
			
		||||
 | 
			
		||||
#define DEBUG
 | 
			
		||||
 | 
			
		||||
MODULE_AUTHOR("Serhii Popovych <serhii.popovych@globallogic.com>");
 | 
			
		||||
MODULE_DESCRIPTION("Hello, world in Linux Kernel Training");
 | 
			
		||||
MODULE_LICENSE("Dual BSD/GPL");
 | 
			
		||||
 | 
			
		||||
struct TimingList {
 | 
			
		||||
	struct TimingList *next;
 | 
			
		||||
	ktime_t time_before, time_after;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static struct TimingList *list_head;
 | 
			
		||||
static unsigned int bug_generator;
 | 
			
		||||
 | 
			
		||||
static struct TimingList *write_first_node(void)
 | 
			
		||||
{
 | 
			
		||||
	list_head = kmalloc(sizeof(struct TimingList), GFP_KERNEL);
 | 
			
		||||
	list_head->next = NULL;
 | 
			
		||||
 | 
			
		||||
	return list_head;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct TimingList *insert_next_node(struct TimingList *tail)
 | 
			
		||||
{
 | 
			
		||||
	struct TimingList *new_node = kmalloc(sizeof(struct TimingList),
 | 
			
		||||
						GFP_KERNEL);
 | 
			
		||||
 | 
			
		||||
	if (bug_generator == 5) {
 | 
			
		||||
		new_node = NULL;
 | 
			
		||||
	} else {
 | 
			
		||||
		tail->next = new_node;
 | 
			
		||||
		new_node->next = NULL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return new_node;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct TimingList *add_new_node(struct TimingList *tail)
 | 
			
		||||
{
 | 
			
		||||
	if (tail == NULL)
 | 
			
		||||
		return write_first_node();
 | 
			
		||||
	else
 | 
			
		||||
		return insert_next_node(tail);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void print_time_diff(struct TimingList *node)
 | 
			
		||||
{
 | 
			
		||||
	pr_debug("%lld\n", node->time_after - node->time_before);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void destroy_list(void)
 | 
			
		||||
{
 | 
			
		||||
	struct TimingList *current_node = list_head;
 | 
			
		||||
	struct TimingList *next_node = NULL;
 | 
			
		||||
 | 
			
		||||
	list_head = NULL;
 | 
			
		||||
 | 
			
		||||
	while (current_node != NULL) {
 | 
			
		||||
		print_time_diff(current_node);
 | 
			
		||||
 | 
			
		||||
		next_node = current_node->next;
 | 
			
		||||
		kfree(current_node);
 | 
			
		||||
		current_node = next_node;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
EXPORT_SYMBOL(destroy_list);
 | 
			
		||||
 | 
			
		||||
static int __init hello_init(void)
 | 
			
		||||
{
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void print_hello(int message_count)
 | 
			
		||||
{
 | 
			
		||||
	int i;
 | 
			
		||||
	struct TimingList *current_tail = list_head;
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < message_count; i++) {
 | 
			
		||||
		bug_generator = i;
 | 
			
		||||
 | 
			
		||||
		current_tail = add_new_node(current_tail);
 | 
			
		||||
 | 
			
		||||
		current_tail->time_before = ktime_get();
 | 
			
		||||
		pr_info("Hello, world!\n");
 | 
			
		||||
		current_tail->time_after = ktime_get();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
EXPORT_SYMBOL(print_hello);
 | 
			
		||||
 | 
			
		||||
static void __exit hello_exit(void)
 | 
			
		||||
{
 | 
			
		||||
	destroy_list();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
module_init(hello_init);
 | 
			
		||||
module_exit(hello_exit);
 | 
			
		||||
							
								
								
									
										65
									
								
								lab_6/hello2.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								lab_6/hello2.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,65 @@
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * 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 <linux/init.h>
 | 
			
		||||
#include <linux/module.h>
 | 
			
		||||
#include <linux/printk.h>
 | 
			
		||||
#include <linux/slab.h>
 | 
			
		||||
 | 
			
		||||
#include "hello1.h"
 | 
			
		||||
 | 
			
		||||
MODULE_AUTHOR("Serhii Popovych <serhii.popovych@globallogic.com>");
 | 
			
		||||
MODULE_DESCRIPTION("Hello, world in Linux Kernel Training");
 | 
			
		||||
MODULE_LICENSE("Dual BSD/GPL");
 | 
			
		||||
 | 
			
		||||
static unsigned int message_count = 1;
 | 
			
		||||
 | 
			
		||||
static int __init hello_init(void)
 | 
			
		||||
{
 | 
			
		||||
	BUG_ON(message_count > 10);
 | 
			
		||||
 | 
			
		||||
	if (message_count == 0 || message_count >= 5)
 | 
			
		||||
		pr_warn("Warning: message_count = %d (message_count == 0 or 5 <= message_count <= 10)\n", message_count);
 | 
			
		||||
 | 
			
		||||
	print_hello(message_count);
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void __exit hello_exit(void)
 | 
			
		||||
{
 | 
			
		||||
	destroy_list();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
module_param(message_count, uint, 0444);
 | 
			
		||||
MODULE_PARM_DESC(message_count, "Amount of hello world outputs on load");
 | 
			
		||||
 | 
			
		||||
module_init(hello_init);
 | 
			
		||||
module_exit(hello_exit);
 | 
			
		||||
							
								
								
									
										2
									
								
								lab_6/inc/hello1.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								lab_6/inc/hello1.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,2 @@
 | 
			
		||||
void print_hello(int message_count);
 | 
			
		||||
void destroy_list(void);
 | 
			
		||||
							
								
								
									
										18
									
								
								lab_6/test.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										18
									
								
								lab_6/test.sh
									
									
									
									
									
										Executable file
									
								
							@ -0,0 +1,18 @@
 | 
			
		||||
#!/bin/sh
 | 
			
		||||
 | 
			
		||||
if [ -z "$1" ]; then
 | 
			
		||||
	echo "Error: no script supplied"
 | 
			
		||||
	echo "Usage: $0 path/to/testing/script"
 | 
			
		||||
	exit 1
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
while read l; do
 | 
			
		||||
	if [ -z "$l" ]; then
 | 
			
		||||
		continue;
 | 
			
		||||
	fi;
 | 
			
		||||
 | 
			
		||||
	echo "$ $l";
 | 
			
		||||
	$($l);
 | 
			
		||||
 | 
			
		||||
	echo -e ""
 | 
			
		||||
done < $1
 | 
			
		||||
							
								
								
									
										33
									
								
								lab_6/tests/lab6-test
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								lab_6/tests/lab6-test
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,33 @@
 | 
			
		||||
insmod lab_6/hello2.ko
 | 
			
		||||
 | 
			
		||||
insmod lab_6/hello1.ko
 | 
			
		||||
 | 
			
		||||
insmod lab_6/hello2.ko
 | 
			
		||||
rmmod hello2.ko
 | 
			
		||||
 | 
			
		||||
insmod lab_6/hello2.ko message_count=0
 | 
			
		||||
rmmod hello2.ko
 | 
			
		||||
 | 
			
		||||
insmod lab_6/hello2.ko message_count=1
 | 
			
		||||
rmmod hello2.ko
 | 
			
		||||
 | 
			
		||||
insmod lab_6/hello2.ko message_count=3
 | 
			
		||||
rmmod hello2.ko
 | 
			
		||||
 | 
			
		||||
insmod lab_6/hello2.ko message_count=5
 | 
			
		||||
rmmod hello2.ko
 | 
			
		||||
 | 
			
		||||
insmod lab_6/hello2.ko message_count=8
 | 
			
		||||
rmmod hello2.ko
 | 
			
		||||
 | 
			
		||||
insmod lab_6/hello2.ko message_count=10
 | 
			
		||||
rmmod hello2.ko
 | 
			
		||||
 | 
			
		||||
insmod lab_6/hello2.ko message_count=12
 | 
			
		||||
rmmod hello2.ko
 | 
			
		||||
 | 
			
		||||
insmod lab_6/hello2.ko
 | 
			
		||||
rmmod hello1.ko
 | 
			
		||||
 | 
			
		||||
rmmod hello2.ko
 | 
			
		||||
rmmod hello1.ko
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user