Lab 3

C · Linux VM · kernel module

Give a Linux program a character device

Your computer

A character device looks like a file to a program. Here, the kernel supplies the bytes you read. Build a small driver, send it a message from the shell and fix a C client that stops reading too soon.

Build and load

Use a disposable Linux VM where you can load kernel modules. The module creates a message buffer in kernel memory and needs no FPGA wiring. The course test used 32-bit Alpine Linux with the virt kernel, a root shell, and matching headers. In an Alpine VM, install the build tools with apk add build-base linux-virt-dev kmod. Ask the instructor for the matching headers if the running release and installed development package differ. A container sharing your computer's kernel is unsuitable for this exercise.

Extract the download inside the VM. In its root shell, enter the starter folder, which contains the complete driver, a Makefile and the editable client. Check the build tree exists and that the device name is free before loading your module.

cd starter
uname -r
test -d "/lib/modules/$(uname -r)/build" && test ! -e /dev/chardev
make
insmod ./chardev.ko
sleep 1
ls -l /dev/chardev
cat /dev/chardev
printf 'FPGA display' > /dev/chardev
cat /dev/chardev

Which part of the node listing identifies a character device? Record its major and minor numbers. Why can cat stop reading a message whose length it did not know in advance?

The default greeting should appear in full. After the write, cat should show FPGA display. The client works through the same device as cat. Before running it, predict how many successful reads of up to five bytes it will take to fetch all twelve bytes. How many calls to read will the complete loop make, including the final one?

Repair the client

Run ./client. The starter shows only the first five bytes. In read_message, keep reading into the same five-byte buffer until read returns zero. Write exactly the number of bytes each successful read returned. Preserve the error return when read fails. Rebuild and compare the client with cat.

Test a one-character message, a message of exactly five characters and a message that needs three reads. Then use the client's replacement-text argument to write an 80-byte message. A write of 81 bytes should fail and leave the earlier message intact. State which result you observed in each case.

Remove and reload

rmmod chardev
sleep 1
test ! -e /dev/chardev && echo OK
insmod ./chardev.ko
sleep 1
cat /dev/chardev
rmmod chardev

Hand in your client loop, the two node listings and one sentence explaining where the message lived between a write and a read. Explain the effect of unloading and reloading the module.

Adapted from FPGAcademy Embedded Systems 21.1 · Lab 3, Part I. LabsLand wrote the driver, client and worksheet. FPGAcademy notice · MIT