<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=COMP3000_Operating_Systems_W25%3A_Tutorial_7</id>
	<title>COMP3000 Operating Systems W25: Tutorial 7 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://homeostasis.scs.carleton.ca/wiki/index.php?action=history&amp;feed=atom&amp;title=COMP3000_Operating_Systems_W25%3A_Tutorial_7"/>
	<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP3000_Operating_Systems_W25:_Tutorial_7&amp;action=history"/>
	<updated>2026-04-22T08:47:42Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP3000_Operating_Systems_W25:_Tutorial_7&amp;diff=25029&amp;oldid=prev</id>
		<title>Lianyingzhao: Created page with &quot;In this tutorial you&#039;ll be learning about Linux kernel modules, part of which are device drivers. You’ll create several kernel modules and see how to interact with device drivers using special files (e.g., character device) and their file operations as an interface. You can also have a better understanding of special files by doing so.   ==General Instructions (same for all tutorials)==  &lt;div class=&quot;mw-collapsible mw-collapsed&quot;&gt; Click on Expand to view. &lt;div class=&quot;mw-...&quot;</title>
		<link rel="alternate" type="text/html" href="https://homeostasis.scs.carleton.ca/wiki/index.php?title=COMP3000_Operating_Systems_W25:_Tutorial_7&amp;diff=25029&amp;oldid=prev"/>
		<updated>2025-03-03T03:12:54Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;In this tutorial you&amp;#039;ll be learning about Linux kernel modules, part of which are device drivers. You’ll create several kernel modules and see how to interact with device drivers using special files (e.g., character device) and their file operations as an interface. You can also have a better understanding of special files by doing so.   ==General Instructions (same for all tutorials)==  &amp;lt;div class=&amp;quot;mw-collapsible mw-collapsed&amp;quot;&amp;gt; Click on Expand to view. &amp;lt;div class=&amp;quot;mw-...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;In this tutorial you&amp;#039;ll be learning about Linux kernel modules, part of which are device drivers. You’ll create several kernel modules and see how to interact with device drivers using special files (e.g., character device) and their file operations as an interface. You can also have a better understanding of special files by doing so. &lt;br /&gt;
&lt;br /&gt;
==General Instructions (same for all tutorials)==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;mw-collapsible mw-collapsed&amp;quot;&amp;gt;&lt;br /&gt;
Click on Expand to view.&lt;br /&gt;
&amp;lt;div class=&amp;quot;mw-collapsible-content&amp;quot;&amp;gt;&lt;br /&gt;
Tutorials are graded based on participation and effort (&amp;#039;&amp;#039;&amp;#039;so no need to struggle to have the “correct” answers — what matters is the attempts and sufficient thinking reflected in your answers&amp;#039;&amp;#039;&amp;#039;). Submit your answers on Brightspace as a single text file named &amp;quot;&amp;lt;username&amp;gt;-comp3000-t&amp;lt;n&amp;gt;.txt&amp;quot; (where username is your MyCarletonOne username and n is the tutorial number). The first four lines of this file should be &amp;quot;COMP 3000 Tutorial &amp;lt;n&amp;gt;&amp;quot;, your name, student number, and the date of submission. Your submitted answers must at least respond to all the items in the &amp;#039;&amp;#039;&amp;#039;Tasks/Questions&amp;#039;&amp;#039;&amp;#039; section and include your thinking and exploration (or even confusion) for each question.&lt;br /&gt;
&lt;br /&gt;
The deadline is usually four days after the tutorial date (see the actual deadline on the submission entry under Tools -&amp;gt; Assignments). Note that the submission entry is enforced by the system, so you may fail to get the effort marks even if it is one minute past the deadline.&lt;br /&gt;
&lt;br /&gt;
You should also check in with your assigned TA. Your TA will be your first point of contact when you have questions or encounter any issues during the tutorial session. &lt;br /&gt;
&lt;br /&gt;
You get 1.5 marks for submitting answers that shows your effort and 0.5 for checking in, making this tutorial worth 2 points total.&lt;br /&gt;
&lt;br /&gt;
Read through the instructions before starting your work to get an overall picture. When source files are needed, you can download them by clicking on the hyperlink.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Important Tips==&lt;br /&gt;
* There is a chance your VM will crash or your ssh session will become unresponsive when messing with the kernel. To recover, you can reboot your VM from the OpenStack web console.&lt;br /&gt;
* Definitely do not under any circumstances attempt this on your own system. This is your last warning.&lt;br /&gt;
* Remember to recompile and insert your module after making changes.&lt;br /&gt;
* Work incrementally and be prepared for your system to crash.&lt;br /&gt;
* It might be wise to code on your own computer and use &amp;lt;tt&amp;gt;scp&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;sshfs&amp;lt;/tt&amp;gt; to transfer the files to your VM.&lt;br /&gt;
* https://elixir.bootlin.com/linux/latest/source is a great resource for learning about kernel functions and data structures.&lt;br /&gt;
&lt;br /&gt;
Note: although it&amp;#039;s highly recommended that you use the openstack instance, if you cannot for a reason, or if you encounter other issues, check out [[COMP3000 Operating Systems W25: Tutorial 7 Not Using SCS OpenStack | these instructions]] to see if they help.&lt;br /&gt;
&lt;br /&gt;
==Special Files==&lt;br /&gt;
Before you start, review what you have learned about special files and think about the following questions (no need to answer them): 1) What does a special file represent and what backs it up? 2) Can you have multiple special files that are somehow &amp;quot;the same&amp;quot;? What does it mean to &amp;quot;copy&amp;quot; a special file? 3) How are named pipes different from unnamed pipes and what are their similarities? Take this opportunity to deepen your understanding of special files to prepare you for working with device drivers that support such special files.&lt;br /&gt;
&lt;br /&gt;
===Tasks part A: Understanding special files===&lt;br /&gt;
# Try the following commands as a non-privileged user. What does each do? How do the files f1-f3 compare? How do they compare to &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt;? Remember you can get output from &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt; using &amp;lt;tt&amp;gt;cat&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;dd&amp;lt;/tt&amp;gt;. If you use &amp;lt;tt&amp;gt;cat&amp;lt;/tt&amp;gt;, make sure to pipe it to &amp;lt;tt&amp;gt;less&amp;lt;/tt&amp;gt;!&lt;br /&gt;
#* &amp;lt;tt&amp;gt;cp /dev/urandom f1&amp;lt;/tt&amp;gt; 		(hit Ctrl+C right away to avoid a huge file)&lt;br /&gt;
#* &amp;lt;tt&amp;gt;sudo cp -a /dev/urandom f2&amp;lt;/tt&amp;gt;&lt;br /&gt;
#* &amp;lt;tt&amp;gt;sudo mknod f3 c 1 9&amp;lt;/tt&amp;gt;&lt;br /&gt;
#: Note: If you see any command running for more than a few seconds, hit Ctrl+C right away, check the produced file (often it’s very large) and delete it if not needed.&lt;br /&gt;
# Make named pipes using &amp;lt;tt&amp;gt;mknod&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;mkfifo&amp;lt;/tt&amp;gt;. Use them to simulate &amp;lt;tt&amp;gt;ls | wc&amp;lt;/tt&amp;gt; using just the &amp;#039;&amp;gt;&amp;#039; and &amp;#039;&amp;lt;&amp;#039; operators.&lt;br /&gt;
# Use &amp;lt;tt&amp;gt;mknod&amp;lt;/tt&amp;gt; to make a copy of your current terminal&amp;#039;s tty - &amp;lt;tt&amp;gt;/dev/tty&amp;lt;/tt&amp;gt; (Hint: consider how you did something similar with &amp;lt;tt&amp;gt;/dev/urandom&amp;lt;/tt&amp;gt; above). You can name it &amp;lt;tt&amp;gt;mytty&amp;lt;/tt&amp;gt;. Examine its characteristics using &amp;lt;tt&amp;gt;stty --file=mytty&amp;lt;/tt&amp;gt;. Do the same for the original tty. &lt;br /&gt;
# Run &amp;lt;tt&amp;gt;stty --help&amp;lt;/tt&amp;gt; to see what you can do with stty. Try disabling local echo. How does the shell behave with echo disabled? How can you restore echo without logging out and back in? Recall we did this in the class.&lt;br /&gt;
&lt;br /&gt;
==Kernel Modules==&lt;br /&gt;
In this part of the tutorial you will be building and installing kernel modules. You will need root access to install kernel modules.&lt;br /&gt;
&lt;br /&gt;
It is &amp;#039;&amp;#039;&amp;#039;highly recommended&amp;#039;&amp;#039;&amp;#039; that you use a comp3000 openstack instance for the exercises below for two reasons. First, you may have difficulties compiling kernel modules on other systems. Second, these operations are potentially dangerous, and mistakes could &amp;#039;&amp;#039;&amp;#039;destroy all data&amp;#039;&amp;#039;&amp;#039; on the Linux system. Consider yourself warned!&lt;br /&gt;
&lt;br /&gt;
Before you start, review what you have learned about kernel modules and device drivers, and think about the following questions (no need to answer them): 1) How does the source of kernel modules differ from C programs? 2) How does building kernel modules differ from building userspace C programs? 3) How does invoking the file operations (e.g., read/write) of a special file trigger corresponding functions in a device driver?&lt;br /&gt;
&lt;br /&gt;
You will learn how you can access and manipulate kernel data structures and call kernel functions in a kernel module. You will also understand how processes are represented by &amp;lt;tt&amp;gt;task_struct&amp;lt;/tt&amp;gt;&amp;#039;s in the Linux kernel, what kind of information is stored in a task struct, and how to access the &amp;lt;tt&amp;gt;task_struct&amp;lt;/tt&amp;gt; of a process.&lt;br /&gt;
&lt;br /&gt;
===Tasks part B: A simple kernel module===&lt;br /&gt;
Download the source for this [https://people.scs.carleton.ca/~lianyingzhao/comp3000/w25/tut7/simple.tar.gz simple module], unpack, and build it by typing &amp;quot;&amp;lt;tt&amp;gt;make&amp;lt;/tt&amp;gt;&amp;quot;. You can safely ignore the warning of &amp;quot;Skipping BTF generation...&amp;quot; if encountered.&lt;br /&gt;
# Install the module using &amp;quot;&amp;lt;tt&amp;gt;sudo insmod simple.ko&amp;lt;/tt&amp;gt;&amp;quot;. The hello message is recorded in the kernel logs. How do you view the kernel logs? How many ways are there to view them?&lt;br /&gt;
# Check to see that the module has been loaded. How do you do this?&lt;br /&gt;
# Remove the module from the kernel. What did you do?&lt;br /&gt;
&lt;br /&gt;
===Tasks part C: A character device kernel module (driver)===&lt;br /&gt;
Download the source for [https://people.scs.carleton.ca/~lianyingzhao/comp3000/w25/tut7/ones.tar.gz ones], a kernel module implementing a character device that outputs an unbounded string of &amp;quot;1&amp;quot;&amp;#039;s. Build, compile, and run it as before.&lt;br /&gt;
# What kernel messages does the module generate? Does it create any new files (other than &amp;lt;tt&amp;gt;/dev/ones&amp;lt;/tt&amp;gt;)? If so, where? (Hint: search in &amp;lt;tt&amp;gt;/sys&amp;lt;/tt&amp;gt;)&lt;br /&gt;
# What happens when you &amp;quot;cat&amp;quot; the device &amp;lt;tt&amp;gt;/dev/ones&amp;lt;/tt&amp;gt;? How can you limit the output in more than one way?&lt;br /&gt;
# Add more &amp;lt;tt&amp;gt;printk()&amp;lt;/tt&amp;gt;’s (where you see needed) to find out at which point which functions in ones.c are called and for which purpose.&lt;br /&gt;
&lt;br /&gt;
===Tasks part D: Getting process information from a module===&lt;br /&gt;
Download the source [https://people.scs.carleton.ca/~lianyingzhao/comp3000/w25/tut7/newgetpid.tar.gz newgetpid.c]. Build and run it as before. Hints for the questions below: &lt;br /&gt;
&lt;br /&gt;
https://elixir.bootlin.com/linux/latest/source/include/linux/sched.h, &lt;br /&gt;
https://elixir.bootlin.com/linux/latest/source/arch/x86/include/asm/current.h, &lt;br /&gt;
https://elixir.bootlin.com/linux/latest/source/include/linux/cred.h&lt;br /&gt;
# What type is &amp;quot;&amp;lt;tt&amp;gt;current&amp;lt;/tt&amp;gt;&amp;quot;? &amp;lt;u&amp;gt;How&amp;lt;/u&amp;gt; can you figure this out?&lt;br /&gt;
# Modify newgetpid.c so that it creates a device file &amp;lt;tt&amp;gt;/dev/describe&amp;lt;/tt&amp;gt; rather than &amp;lt;tt&amp;gt;/dev/newgetpid&amp;lt;/tt&amp;gt;.&lt;br /&gt;
# Make &amp;lt;tt&amp;gt;/dev/describe&amp;lt;/tt&amp;gt; output the calling process&amp;#039;s parent ID (ppid), user ID (uid), group ID (gid), effective user ID (euid), and effective group ID (egid).&lt;br /&gt;
&lt;br /&gt;
==Using &amp;lt;tt&amp;gt;bpftrace&amp;lt;/tt&amp;gt; to monitor kernel module events==&lt;br /&gt;
Note: you are not required to write about your experience for this part in the submitted work, but you should still do it, which can help you understand eBPF better (see how simple but still powerful an eBPF program can be).&lt;br /&gt;
&lt;br /&gt;
Download this tiny code [https://people.scs.carleton.ca/~lianyingzhao/comp3000/w25/tut7/kmsnoop.bt kmsnoop.bt] and make it executable with &amp;lt;tt&amp;gt;chmod 755 kmsnoop.bt&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, you can open a separate SSH session and run it with &amp;lt;tt&amp;gt;sudo ./kmsnoop.bt&amp;lt;/tt&amp;gt;. It will wait for events. When you do any &amp;quot;insmod&amp;quot; or &amp;quot;rmmod&amp;quot; in the other session, you will see it outputs a line of message.&lt;br /&gt;
&lt;br /&gt;
If you want to avoid opening another SSH window, you can use &amp;lt;tt&amp;gt;tmux&amp;lt;/tt&amp;gt; (which we learned in Tutorial 1).&lt;br /&gt;
&lt;br /&gt;
How could this save you from doing any &amp;lt;tt&amp;gt;printk&amp;lt;/tt&amp;gt;&amp;#039;s in your kernel module code?&lt;br /&gt;
You can also &amp;lt;tt&amp;gt;sudo bpftrace -l | grep&amp;lt;/tt&amp;gt; for your needed kernel functions (e.g.,&amp;lt;tt&amp;gt;printk&amp;lt;/tt&amp;gt;) to hook to.&lt;/div&gt;</summary>
		<author><name>Lianyingzhao</name></author>
	</entry>
</feed>