jasonbrent / lab

Misc single file-based Linux kernel modules

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

my misc linux kernel modules

Simple use of container_of macro.

I tried to make things more complicated by accessing the container_of of the container_of struct. insmod it & dmesg.

A very simple way, less time-consuming for inserting printk and providing the possibility to analyze the flow of instructions for tests is the use of the predefined constants FILE, LINE and func. insmod it & dmesg.

3. list.c module

Simple example of <linux/list.h>. insmod it & dmesg.

Simple example of <linux/hashtable.h>. insmod it & dmesg.

Another example of <linux/hashtable.h>. insmod it & dmesg.

please read header comment in wait_queue.c to refresh your knowledge.

First let me explain to you what should the module do:

After inserting the module, it will wait for you until you give it a specific action to complete the rest of its work.

This behavior is just like a completion. a completion built on top of waitqueue.

How to use: (watch dmesg live)

$ insmod wait_queue.ko &
$ echo "hi" > /proc/mydev &
$ echo "bla" > /proc/mydev &
$ rmmod wait_queue.ko

Another example for static waitqueue

How to use: (watch dmesg live)

$ sudo insmod waitqueue2.ko
$ sudo cat /dev/etx_device
$ rmmod waitqueue2

here is a dynamic version.

Passing Arguments to Device Driver. like: argc & argv

How to use:

$ insmod params.ko myshort=5 myint=100 mystring="Sohaib" myintArray=3,7

Register the callback whenever the argument (parameter) got changed.

insmod & enjoy watching dmesg live!

$ insmod cb_params.ko myint=50
$ echo 333 > /sys/module/cb_params/parameters/myint

7. cdev.c module

Complete char device driver with user-level app for testing.

to test the driver:

$ insmod cdev.ko
$ ./test_cdev

TODO SO, What’s next? read this page carefully then complete this task.

How to use:

$ insmod sys_fs.ko
$ ls /sys/kernel/sma_sysfs/
$ cat /sys/kernel/sma_sysfs/sma_value
$ echo 44 > /sys/kernel/sma_sysfs/sma_value
$ cat /sys/kernel/sma_sysfs/sma_value
$ rmmod sys_fs

Please read Documentation/filesystems/sysfs.rst and Documentation/core-api/kobject.rst

How to use:

$ insmod debugfs.ko
$ ls -l /sys/kernel/debug/sma_debugfs/
$ cat /sys/kernel/debug/sma_debugfs/sma_u8
$ echo 77 > /sys/kernel/debug/sma_debugfs/sma_u8
$ cat /sys/kernel/debug/sma_debugfs/sma_u8
$ rmmod debugfs

Please read Documentation/filesystems/debugfs.rst (Done)

How to use:

$ insmod ioctl.ko
$ ./test_ioctl

Nice example from other people:

please see

How to use: (watch dmesg live)

$ insmod kthread.ko

See your kernel thread:

$ ps -aux | grep "SMA Thread"

export_symbols.c shares some dummy symbols (variables & functions).

use_symbols.c uses these dummy symbols. How to use: (insert and remove use_symbols.ko many times and watch dmesg)

$ insmod export_symbols.ko
$ insmod use_symbols.ko
$ rmmod use_symbols
$ insmod use_symbols.ko
$ rmmod use_symbols
$ insmod use_symbols.ko
$ rmmod use_symbols

Check if your symbols really engaged to your kernel:

$ cat /proc/kallsyms | grep SOHAIB_
$ cat /proc/kallsyms | grep sohaib_

I made a very nice trick :D see trick_kallysms.c module.

After inserting export_symbols.c module I took the address of SOHAIB_CONUT symbol from /proc/kallsyms file, then I used it as a hard code in trick_kallysms.c :))

See also mod1.c and mod2.c modules for more simple sample.

13. Mutex module

Prerequisites: Make should you understand Kthread.

How to use it: insmod mutex.ko and enjoy watching dmesg live. This code snippet explains how to create two threads that access a global variable (etx_gloabl_variable). So before accessing the variable, it should lock the mutex. After that, it will release the mutex.

This way is not the most optimal way for locking, because whole the critical section is just singel int var, and you used mutex for locking, this adds moree overhead, the best way here is to use Atomic ops.

Reference: Documentation/kernel-hacking/locking.rst

Trivial example touchs some Atomic ops APIs.

Another nice example. insmod it and dmesg it live..

How to use it: insmod spinlock.ko and enjoy watching dmesg live.

Prerequisites: Make should you understand Kthread.

This code snippet explains how to create two threads that access a global variable (etx_gloabl_variable). So before accessing the variable, it should lock the spinlock. After that, it will release the spinlock.

Here is Read write spinlock example. insmod it and dmesg it live..

Here is seqlock.c example. insmod it and dmesg it live..

Reference:

  • Documentation/kernel-hacking/locking.rst

  • Documentation/locking/spinlocks.rst

  • Documentation/locking/locktypes.rst

Two articles

tasklet.c trivial example

tasklet2.c embetronicx example

Simple example of <linux/timer.h>. insmod it & enjoy watching dmesg live.

Please, don’t panic. Here is a simple sturcure of any timer.

How to use it:

$ insmod misc_driver.ko
$ ls -l /dev/simple_sma_misc
$ echo 1 > /dev/simple_sma_misc
$ cat /dev/simple_sma_misc
$ rmmod misc_driver

Read Documentation/scheduler/completion.rst

First, Let me explain to you the concept of driver code.

In this source code, two places we are sending the complete call. One from the read function and another one from the driver exit function.

I’ve created one thread (wait_function) which has while(1). That thread will always wait for the event to complete. It will be sleeping until it gets a complete call. When it gets the complete call, it will check the condition. If the condition is 1 then the complete came from the read function. It is 2, then the complete came from the exit function. If complete came from the read function, it will print the read count and it will again wait. If it is coming from the exit function, it will exit from the thread.

Here I’ve added two versions of code.

  • Completion created by static method

  • Completion created by dynamic method

But operation wise, both are the same.

How to use it - watch dmesg live: ( static version)

$ insmod static_completion.ko
$ sudo cat /dev/sma_device
$ rmmod static_completion

How to use it - watch dmesg live: ( dynamic version)

$ insmod dynamic_completion.ko
$ sudo cat /dev/sma_device
$ rmmod dynamic_completion

I try to get familiar with the interface exposed by the Linux and the Virtual File System (VFS) component. so I made a simple, virtual file system driver (i.e. without physical disk support).

How to use it:

$ sudo chmod +x test_myfs.sh
$ sh test_myfs.sh

Further reading: Look at the comment for header of myfs.c file.

This module uses the keyboard IRQ, inspect the incoming key codes and stores them in a buffer. The buffer will be accessible from userspace via character device driver.

How to use it:


25. My patch for Task 14 of the Eudyptula Challenge

This module adds a new proc file for every task called, "sma_pid", located in the

/proc/${PID}/ directory for that task.

When the proc file is read from, have it print out the value of PID. see demo video..

google: sequence file Documentation/filesystems/seq_file.txt

26. ps -aux Module!

Linux kernel modules act almost like $(ps aux) command.

See list_proc.c, minimal sample all that he does is printing out a list of all processes PIDs and names in dmesg.

Reference: Documentation/admin-guide/dynamic-debug-howto.rst here are alot of very nice examples ;)

How to use it - watch dmesg live: ( minimal sample )

$ insmod minimal_dyndbg.ko
# filter my fie name
$ echo "file minimal_dyndbg.c +p" > /sys/kernel/debug/dynamic_debug/control
$ rmmod minimal_dyndbg

Filter by specific string:

$ insmod minimal_dyndbg.ko
# filter all messages marked as "Important"
$ echo "format "Important" +p" > /sys/kernel/debug/dynamic_debug/control
$ rmmod minimal_dyndbg

Another example: Filter by function

$ insmod dyndbg.ko
# filter all messages in func hello_read & hello_write
$ echo "func hello_read +p; func hello_write +p" > /sys/kernel/debug/dynamic_debug/control
$ cat /dev/dyndbg
$ echo 33 > /dev/dyndbg
$ rmmod dyndbg

Filter by line:

$ insmod dyndbg.ko
# filter all messages between 14-26 lines
$ echo "line 14-26  +p" > /sys/kernel/debug/dynamic_debug/control
$ rmmod dyndbg

netfilter.c

References:

About

Misc single file-based Linux kernel modules


Languages

Language:C 97.3%Language:Makefile 1.8%Language:Shell 0.9%