Make Your Own Module

CS 444/544 Lab Preparation: Running VM! • Download the image using - wget http://cslabs.clarkson.edu/oslab/syscall_lab.vdi!

• Applications -> Accessories-> VirtualBox!

• In Virtual box window click New!

• Name: Module_username!

• Type: Linux! ! • Version: Ubuntu (64 bit) click next! ! Instructions!

• Memory size – increase to 2048MB click next!

• Hard Drive – Select Use an existing virtual hard drive!

• Browse to find the file syscall_lab.vdi that you downloaded from cslabs website and then click open!

• Click Create!

Instructions!

• Go to Settings -> System -> processor!

• Increase the number of processors to 4!

• Click OK!

• Click start!

• UserName: csguest!

• Password: cspassword!

• Become a root user sudo -i!

Lab Objectives

This lab's objectives: ● Learn how to program a Linux module(driver) ● Create a Linux module with your own name ● Understand what is procFS ● Create a directory and filename in proc/ with your own name

Future Goal(At the end of this semester): ● Create a Linux module to display the ' information in /proc directory ● (Linux Process Virtual Memory Monitor)

Linux Kernel Architecture

Notes:

Applications cannot directly visit the kernel

Device drivers are inside the kernel

Limitation of user land Understanding the Linux Module

Linux Loadable Modules

Linux modules are part of the kernel: A Linux module is a piece of code that can be loaded or unloaded into the kernel upon demand.

Advantages: ● No need to recompile the kernel ● Easy to debug ● Only loaded when needed

Example of Linux modules: ● Device drivers ● ● Software running in the kernel

Linux Hello World Module

Obtain the skeleton code: mkdir module wget http://www.clarkson.edu/class/cs444/cs444.sp2016/assignments/module/yourname.c wget http://www.clarkson.edu/class/cs444/cs444.sp2016/assignments/module/Makefile Code comments: ● module_init(functionName) registers the function that will be called when the module is loaded. ● module_exit(functionName) registers the function that will be called when the module is removed. ● printk() is similiar to printf() but is for use in the kernel, printing messages into the kernel log.

Compile the kernel module: root: ~> make

Reference: http://www.faqs.org/docs/kernel/x145.html Run and Verify It Install your hello world module: root: > insmod yourname.ko check the modules installed/running root: > lsmod check the module's init/exit information root: >dmesg check the module description root: >modinfo yourname.ko remove the module root: >rmmod yourname root: >dmesg Sample of Linux hello world Module Requirements and Practice

1. Try to compile your module name as your Clarkson ID

2. Try to let the command modinfo display the module author as your name and clarkson email and register the module with your Clarkson ID

3. Try to print out the messages in Kernel log as below when your module is installed or removed "Clarkson ID's module ... INITIALIZED" "Clarkson ID's module ... EXITED"

(See the example on previous slide) If you have problem with this part, feel free to ask TA for help! A procfs Directory Module

proc/ File System

/proc directory is a pseudo file system(procfs), which does not exist in the hard disk at all.

/proc displays general system information and specific process information and statistics . users are not allowed to change/modify data in the kernel.

BUT procfs can! By using procfs files to change the kernel parameters.

Deeper Understanding of File System

General File System operation interface:

read(): obtain an amount of data from FS and put into buffer

write(): send an amount of data in the buffer into FS

File System concept:

FS is responsible for where to store/get those data

User is ignorant of where those data are stored/found How to Manipulate procfs declare an entry(dir/file) object in procfs: struct proc_dir_entry entrydir, entryfile; create an entry object in procfs: directory: entrydir = proc_mkdir("dirName", NULL) file: entryfile = create_proc_entry("fileName", 0755, entrydir) attach the kernel space to procfs entry and ops: entry->data = buf_pointer, entry->read_proc = readFunctionName, entry->write_proc =writeFunctionName remove the procfs entry objects in order: remove_proc_entry("fileName", entrydir) remove_proc_entry("dirName", NULL)

procfs Operation Demo

Notes: When users call read()/writer() on the /proc file, the corresponding module-defined proc_write_x() and proc_read_x() will be triggered. procfs Operation Demo (con't)

Notes: proc_write_x() will copy the data from userland into kernel buffer. proc_read_x() will allocate a Page to print the kernel buffer info onto the page. Linux procfs Module Obtain the skeleton code mkdir procfs wget http://www.clarkson.edu/class/cs444/cs444.sp2016/assignments/module/procfs/yourname.c wget http://www.clarkson.edu/class/cs444/cs444.sp2016/assignments/module/procfs/Makefile

Code comments The kernel buffer static char buffer[LENGTH+1];

procfs operations when read(), print the kernel buffer content as the file data when write(), copy the data from user to the kernel buffer as saving in the file.

Edit Yourname.c At the end of the program module_init(…..) module_exit(….) Compile the code root: >make

Run and Verify It: install the module into the kernel root: >insmod yourname.ko procfs_read() is invoked root: > cat /proc/classname/yourname procfs_write() is invoked root: > echo "Any string" > /proc/classname/yourname root: > cat /proc/classname/yourname remove the module from the kernel root: > rmmod yourname Credit for Week 08

Show the TA how your procfs module works (See example on next page) 1. Your module name should be your clarkson ID; 2. Compile, install your procfs module; 3. In proc/, there is a directory called cs444 and inside resides a file with your clarkson ID; 4. You can read from and write to the above proc file; 5. Uninstall your procfs module.

Ask TA for a signature

(No need to upload the hello world module in module directory) Demo of Linux procfs Module