CSE 4105 System Programming · Lab 14

Loadable Kernel Modules · The Device Driver Model

A /proc module and a platform driver, loaded in a virtual machine.

Tasks

The same steps as the lab slide. Do them on a Linux PC with the files from the zip; the tools below help you check your thinking first.

Lab 14.1 · Loadable Kernel Modules

  1. Demo: cd demo/hello; make; ../../guest/run_guest.sh . then insmod, dmesg, lsmod, rmmod
  2. TODO 1 in sysprog_proc_starter.c: module_param(owner, charp, 0444)
  3. TODO 2: four seq_printf lines: owner, reads, loaded for, reader
  4. TODO 3: proc_create_single in init; NULL → return -ENOMEM
  5. TODO 4: remove_proc_entry in exit. Test: insmod owner=…, cat twice, rmmod, cat

the guest, with the finished lab (real)

guest 6.8.0-146-generic: your modules are in /mod:
$ insmod sysprog_proc.ko owner=Mahbub
$ cat /proc/sysprog
owner:      Mahbub
reads:      1
loaded for: 15 jiffies = 15 ms (HZ = 1000)
reader:     cat (pid 136)
$ rmmod sysprog_proc
$ dmesg | tail -2
sysprog: /proc/sysprog created (owner=Mahbub)
sysprog: /proc/sysprog removed after 2 reads

Forget TODO 4 and the first cat after rmmod runs code that is gone: an oops. In the VM that is a lesson; on the PC it would be a crash.

Lab 14.2 · The Device Driver Model

  1. cd lab14_2; make; ../guest/run_guest.sh . then insmod sp_dev.ko and sp_drv_starter.ko: no probe (why?)
  2. TODO 4 first: the driver's .name must match the device's name
  3. TODO 3: probe: devm_kzalloc the state, mutex_init, value = 7, set_drvdata
  4. TODO 1, 2: value_show with sysfs_emit; value_store with kstrtoint
  5. Test: cat, echo 42, echo abc, unbind, bind, rmmod in both orders

the starter, before TODO 4 (real)

$ insmod sp_dev.ko; insmod sp_drv_starter.ko
$ ls /sys/bus/platform/drivers
ac  acpi-fan  …  syscon
sysprog-TODO  tpm_tis  …
$ ls /sys/devices/platform/sysprog.0
driver_override  power  uevent
modalias  subsystem          # no driver!
# after the lab:
$ cat /sys/devices/platform/sysprog.0/value
7
$ echo 42 > …/value; cat …/value
42

The starter loads but never binds (its driver is called sysprog-TODO). Finding out why, with ls /sys/bus/platform/drivers and dmesg, is part of the lab.

Try it here

These run in your browser, with no server and no internet. They are built from the same reference programs as the lab, so their output matches what your finished C program should print. They do not replace the real lab: the kernel, gcc and your own code are the point.

Read the files

Every text file from lab14_files.zip. Read here, then download the zip to work on them.

    ← Lab 13All lecturesLab 15 →