Lab3 Kernel Modules (Due date : February 18, 2004)

Objective : You will study the "module", a mechanism unique to Linux. Modules can be used to dynamically add functionality to the kernel. You will write a module that executes as a kernel-space extension of Linux to report the values of the kernel's "xtime" variable.

Background: You should read the ULK book appendix B or LKP book chapter 9. Online resources include
  1. Chapter 2 Building and Running Modules, Linux Device Drivers, 2nd Edition
  2. The Linux Kernel Module Programming Guide
Problem Statement :
Design and construct a module that implements a clock file in /proc. The file
should support only the file read() operation. When read() is called, it
should return a single ASCII string with two numerical substrings separated by
a single space. For example, it must return a string of the form

934380108 184263

if the system time variable, xtime, was set to

xtime.tv_sec = 934380108
xtime.tv_usec = 184263

Also provide an application program that demonstrates your module. As one
particular test of your program, write a tight loop of the following form.

#include <stdio.h>
#include <sys/time.h>
#define N...

struct timeval gtodTimes[N];
char *procClockTimes[N];
...
my_clock = fopen("/proc/...", "r");
for( i = 0; i < N; i++ )
{
	gettimeofday(>odTimes[i], 0);
	fgets(procClockTime[i], LENGTH, my_clock);
}

for( i = 0; i < N; i++ )
{
	printf("...", gtodTimes[i], procClockTime[i]);
}

Use gettimeofday() to determine the apparent resolution of the clock values
that you read and of the values read from the kernel variable. Explain why
gettimeofday() has a much finer resolution that the 10 millisecond time
between timer interrupts. 
Attacking the problem
There is no difference in writing a mudule for UML and for normal kernel. The module interface in the 2.4 kernel looks like:

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

#include <linux/proc_fs.h>
#include <linux/time.h>

int clock_read(char *buffer,
	       char **buffer_loc,
	       off_t offset,
	       int buffer_length,
	       int *eof,
	       void *data)
{
 ......
}

/* registers with the proc fs. */
int init_func(void) 
{
  ......
  create_proc_read_entry("Myclock",
		       0,              /* dafault mode */
		       NULL,           /* parent dir   */
		       clock_read,     
		       NULL);          /* client data */ 
  ......
}

/* unregisters with the proc fs. */
void exit_func(void) 
{
 ......
 remove_proc_entry("Myclock", NULL); 
 ......
}

module_init(init_func);
module_exit(exit_func);

Compiling your module<
gcc -c -I${KERNELDIR}/include -D__KERNEL__ -DMODULE Myclock.o

How to submit your assignment
Please submit your module source code, module object, test code, a Makefile, and a README under your "~/Uml/lab3" directory on elgate.