get rid of lkmc package, move userland and kernel-modules to top

Rationale: we already had a non buildroot build system,
maintaining both will be hard, and having short paths is more awesome.
This commit is contained in:
Ciro Santilli 六四事件 法轮功
2018-10-25 00:00:01 +00:00
parent 98d2c83317
commit ca231b82f6
99 changed files with 184 additions and 181 deletions

17
kernel_modules/Makefile Normal file
View File

@@ -0,0 +1,17 @@
ifeq ($(OBJECT_FILES),)
# Hardcoding LKMC_MODULE_SUBDIRS here because is not defined.
obj-m += $(addsuffix .o, $(notdir $(basename $(filter-out %.mod.c, $(wildcard $(BR2_EXTERNAL_LKMC_PATH)/kernel_modules/*.c)))))
else
# Trying to do:
# $(MAKE) -C '$(LINUX_DIR)' M='$(M)' hello.ko hello2.ko
# to restrict which modules are built leads to failures
# when doing parallel builds. The only solution I could find
# was to let the host select obj-m itself.
obj-m += $(OBJECT_FILES)
endif
ccflags-y := -DDEBUG -g -std=gnu99 -Werror -Wno-declaration-after-statement -Wframe-larger-than=1000000000
.PHONY: all
all:
$(MAKE) -C '$(LINUX_DIR)' M='$(M)'

View File

@@ -0,0 +1,78 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#anonymous-inode */
#include <linux/anon_inodes.h>
#include <linux/debugfs.h>
#include <linux/errno.h> /* EFAULT */
#include <linux/fs.h>
#include <linux/kernel.h> /* min */
#include <linux/module.h>
#include <linux/printk.h> /* printk */
#include <linux/uaccess.h> /* copy_from_user */
#include "../include/anonymous_inode.h"
static struct dentry *debugfs_file;
static u32 myval = 1;
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
{
char kbuf[9];
size_t ret;
ret = snprintf(kbuf, sizeof(kbuf), "%x", myval);
if (copy_to_user(buf, kbuf, ret)) {
ret = -EFAULT;
}
myval <<= 4;
if (myval == 0) {
myval = 1;
}
return ret;
}
static const struct file_operations fops_anon = {
.read = read,
};
static long unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long argp)
{
int fd;
switch (cmd) {
case LKMC_ANONYMOUS_INODE_GET_FD:
fd = anon_inode_getfd(
"todo_what_is_this_for",
&fops_anon,
NULL,
O_RDONLY | O_CLOEXEC
);
if (copy_to_user((void __user *)argp, &fd, sizeof(fd))) {
return -EFAULT;
}
break;
default:
return -EINVAL;
break;
}
return 0;
}
static const struct file_operations fops_ioctl = {
.owner = THIS_MODULE,
.unlocked_ioctl = unlocked_ioctl
};
static int myinit(void)
{
debugfs_file = debugfs_create_file("lkmc_anonymous_inode", 0, NULL, NULL, &fops_ioctl);
return 0;
}
static void myexit(void)
{
debugfs_remove(debugfs_file);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

View File

@@ -0,0 +1,43 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#character-devices */
#include <linux/fs.h> /* register_chrdev, unregister_chrdev */
#include <linux/module.h>
#include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */
#define NAME "lkmc_character_device"
static int major;
static int show(struct seq_file *m, void *v)
{
seq_printf(m, "abcd");
return 0;
}
static int open(struct inode *inode, struct file *file)
{
return single_open(file, show, NULL);
}
static const struct file_operations fops = {
.llseek = seq_lseek,
.open = open,
.owner = THIS_MODULE,
.read = seq_read,
.release = single_release,
};
static int myinit(void)
{
major = register_chrdev(0, NAME, &fops);
return 0;
}
static void myexit(void)
{
unregister_chrdev(major, NAME);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

View File

@@ -0,0 +1,76 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#automatically-create-character-device-file-on-insmod */
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h> /* register_chrdev, unregister_chrdev */
#include <linux/module.h>
#include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */
#define NAME "lkmc_character_device_create"
static int major = -1;
static struct cdev mycdev;
static struct class *myclass = NULL;
static int show(struct seq_file *m, void *v)
{
seq_printf(m, "abcd");
return 0;
}
static int open(struct inode *inode, struct file *file)
{
return single_open(file, show, NULL);
}
static const struct file_operations fops = {
.llseek = seq_lseek,
.open = open,
.owner = THIS_MODULE,
.read = seq_read,
.release = single_release,
};
static void cleanup(int device_created)
{
if (device_created) {
device_destroy(myclass, major);
cdev_del(&mycdev);
}
if (myclass)
class_destroy(myclass);
if (major != -1)
unregister_chrdev_region(major, 1);
}
static int myinit(void)
{
int device_created = 0;
/* cat /proc/devices */
if (alloc_chrdev_region(&major, 0, 1, NAME "_proc") < 0)
goto error;
/* ls /sys/class */
if ((myclass = class_create(THIS_MODULE, NAME "_sys")) == NULL)
goto error;
/* ls /dev/ */
if (device_create(myclass, NULL, major, NULL, NAME "_dev") == NULL)
goto error;
device_created = 1;
cdev_init(&mycdev, &fops);
if (cdev_add(&mycdev, major, 1) == -1)
goto error;
return 0;
error:
cleanup(device_created);
return -1;
}
static void myexit(void)
{
cleanup(1);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

67
kernel_modules/debugfs.c Normal file
View File

@@ -0,0 +1,67 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#debugfs */
#include <linux/debugfs.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <uapi/linux/stat.h> /* S_IRUSR */
static struct dentry *dir, *toplevel_file;
static u32 value = 42;
/* This basically re-implents the write operation of debugfs_create_u32,
* it is just an excuse to illustrate a fop. */
static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
{
int ret;
unsigned long long res;
/* https://stackoverflow.com/questions/6139493/how-convert-char-to-int-in-linux-kernel */
ret = kstrtoull_from_user(buf, len, 10, &res);
if (ret) {
/* Negative error code. */
return ret;
} else {
value = res;
*off= len;
return len;
}
}
static const struct file_operations fops = {
.owner = THIS_MODULE,
.write = write,
};
static int myinit(void)
{
struct dentry *file;
dir = debugfs_create_dir("lkmc_debugfs", 0);
if (!dir) {
pr_alert("debugfs_create_dir failed");
return -1;
}
file = debugfs_create_u32("myfile", S_IRUSR | S_IWUSR, dir, &value);
if (!file) {
pr_alert("debugfs_create_u32 failed");
return -1;
}
/* Created on the toplevel of the debugfs mount,
* and with explicit fops instead of a fixed integer value.
*/
toplevel_file = debugfs_create_file(
"lkmc_debugfs_file", S_IWUSR, NULL, NULL, &fops);
if (!toplevel_file) {
return -1;
}
return 0;
}
static void myexit(void)
{
debugfs_remove_recursive(dir);
debugfs_remove(toplevel_file);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

24
kernel_modules/dep.c Normal file
View File

@@ -0,0 +1,24 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kernel-module-dependencies */
#include <linux/debugfs.h>
#include <linux/kernel.h>
#include <linux/module.h>
u32 lkmc_dep = 0;
EXPORT_SYMBOL(lkmc_dep);
static struct dentry *debugfs_file;
static int myinit(void)
{
debugfs_file = debugfs_create_u32("lkmc_dep", S_IRUSR | S_IWUSR, NULL, &lkmc_dep);
return 0;
}
static void myexit(void)
{
debugfs_remove(debugfs_file);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

23
kernel_modules/dep2.c Normal file
View File

@@ -0,0 +1,23 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kernel-module-dependencies */
#include <linux/debugfs.h>
#include <linux/kernel.h>
#include <linux/module.h>
extern u32 lkmc_dep;
static struct dentry *debugfs_file;
static int myinit(void)
{
debugfs_file = debugfs_create_u32("lkmc_dep2", S_IRUSR | S_IWUSR, NULL, &lkmc_dep);
return 0;
}
static void myexit(void)
{
debugfs_remove(debugfs_file);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

View File

@@ -0,0 +1,21 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#dump_stack */
#include <linux/module.h>
#include <linux/kernel.h>
static int myinit(void)
{
pr_info("dump_stack myinit\n");
dump_stack();
pr_info("dump_stack after\n");
return 0;
}
static void myexit(void)
{
pr_info("panic myexit\n");
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

138
kernel_modules/fops.c Normal file
View File

@@ -0,0 +1,138 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#file-operations */
#include <linux/debugfs.h>
#include <linux/errno.h> /* EFAULT */
#include <linux/fs.h> /* file_operations */
#include <linux/kernel.h> /* min */
#include <linux/module.h>
#include <linux/printk.h> /* printk */
#include <linux/uaccess.h> /* copy_from_user, copy_to_user */
#include <uapi/linux/stat.h> /* S_IRUSR */
static struct dentry *debugfs_file;
static char data[] = {'a', 'b', 'c', 'd'};
static int open(struct inode *inode, struct file *filp)
{
pr_info("open\n");
return 0;
}
/* @param[in,out] off: gives the initial position into the buffer.
* We must increment this by the ammount of bytes read.
* Then when userland reads the same file descriptor again,
* we start from that point instead.
*/
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
{
ssize_t ret;
pr_info("read\n");
pr_info("len = %zu\n", len);
pr_info("off = %lld\n", (long long)*off);
if (sizeof(data) <= *off) {
ret = 0;
} else {
ret = min(len, sizeof(data) - (size_t)*off);
if (copy_to_user(buf, data + *off, ret)) {
ret = -EFAULT;
} else {
*off += ret;
}
}
pr_info("buf = %.*s\n", (int)len, buf);
pr_info("ret = %lld\n", (long long)ret);
return ret;
}
/* Similar to read, but with one notable difference:
* we must return ENOSPC if the user tries to write more
* than the size of our buffer. Otherwise, Bash > just
* keeps trying to write to it infinitely.
*/
static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
{
ssize_t ret;
pr_info("write\n");
pr_info("len = %zu\n", len);
pr_info("off = %lld\n", (long long)*off);
if (sizeof(data) <= *off) {
ret = 0;
} else {
if (sizeof(data) - (size_t)*off < len) {
ret = -ENOSPC;
} else {
if (copy_from_user(data + *off, buf, len)) {
ret = -EFAULT;
} else {
ret = len;
pr_info("buf = %.*s\n", (int)len, data + *off);
*off += ret;
}
}
}
pr_info("ret = %lld\n", (long long)ret);
return ret;
}
/* Called on the last close:
* http://stackoverflow.com/questions/11393674/why-is-the-close-function-is-called-release-in-struct-file-operations-in-the-l
*/
static int release(struct inode *inode, struct file *filp)
{
pr_info("release\n");
return 0;
}
static loff_t llseek(struct file *filp, loff_t off, int whence)
{
loff_t newpos;
pr_info("llseek\n");
pr_info("off = %lld\n", (long long)off);
pr_info("whence = %lld\n", (long long)whence);
switch(whence) {
case SEEK_SET:
newpos = off;
break;
case SEEK_CUR:
newpos = filp->f_pos + off;
break;
case SEEK_END:
newpos = sizeof(data) + off;
break;
default:
return -EINVAL;
}
if (newpos < 0) return -EINVAL;
filp->f_pos = newpos;
pr_info("newpos = %lld\n", (long long)newpos);
return newpos;
}
static const struct file_operations fops = {
/* Prevents rmmod while fops are running.
* Try removing this for poll, which waits a lot. */
.owner = THIS_MODULE,
.llseek = llseek,
.open = open,
.read = read,
.release = release,
.write = write,
};
static int myinit(void)
{
debugfs_file = debugfs_create_file("lkmc_fops", S_IRUSR | S_IWUSR, NULL, NULL, &fops);
return 0;
}
static void myexit(void)
{
debugfs_remove_recursive(debugfs_file);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

22
kernel_modules/hello.c Normal file
View File

@@ -0,0 +1,22 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#getting-started-natively */
#include <linux/module.h>
#include <linux/kernel.h>
static int myinit(void)
{
pr_info("hello init\n");
/* 0 for success, any negative value means failure,
* E* consts if you want to specify failure cause.
* https://www.linux.com/learn/kernel-newbie-corner-loadable-kernel-modules-coming-and-going */
return 0;
}
static void myexit(void)
{
pr_info("hello exit\n");
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

19
kernel_modules/hello2.c Normal file
View File

@@ -0,0 +1,19 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#getting-started-natively */
#include <linux/module.h>
#include <linux/kernel.h>
static int myinit(void)
{
pr_info("hello2 init\n");
return 0;
}
static void myexit(void)
{
pr_info("hello2 exit\n");
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

View File

@@ -0,0 +1,16 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#init_module */
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
pr_info("init_module\n");
return 0;
}
void cleanup_module(void)
{
pr_info("cleanup_module\n");
}
MODULE_LICENSE("GPL");

72
kernel_modules/ioctl.c Normal file
View File

@@ -0,0 +1,72 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#ioctl */
#include <linux/debugfs.h>
#include <linux/module.h>
#include <linux/printk.h> /* printk */
#include <linux/uaccess.h> /* copy_from_user, copy_to_user */
#include "../include/ioctl.h"
static struct dentry *debugfs_file;
static long unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long argp)
{
void __user *arg_user;
union {
int i;
lkmc_ioctl_struct s;
} arg_kernel;
arg_user = (void __user *)argp;
pr_info("cmd = %x\n", cmd);
switch (cmd) {
case LKMC_IOCTL_INC:
if (copy_from_user(&arg_kernel.i, arg_user, sizeof(arg_kernel.i))) {
return -EFAULT;
}
pr_info("0 arg = %d\n", arg_kernel.i);
arg_kernel.i += 1;
if (copy_to_user(arg_user, &arg_kernel.i, sizeof(arg_kernel.i))) {
return -EFAULT;
}
break;
case LKMC_IOCTL_INC_DEC:
if (copy_from_user(&arg_kernel.s, arg_user, sizeof(arg_kernel.s))) {
return -EFAULT;
}
pr_info("1 arg = %d %d\n", arg_kernel.s.i, arg_kernel.s.j);
arg_kernel.s.i += 1;
arg_kernel.s.j -= 1;
if (copy_to_user(arg_user, &arg_kernel.s, sizeof(arg_kernel.s))) {
return -EFAULT;
}
break;
default:
return -EINVAL;
break;
}
return 0;
}
static const struct file_operations fops = {
.owner = THIS_MODULE,
.unlocked_ioctl = unlocked_ioctl
};
static int myinit(void)
{
/* ioctl permissions are not automatically restricted by rwx as for read / write,
* but we could of course implement that ourselves:
* https://stackoverflow.com/questions/29891803/user-permission-check-on-ioctl-command */
debugfs_file = debugfs_create_file("lkmc_ioctl", 0, NULL, NULL, &fops);
return 0;
}
static void myexit(void)
{
debugfs_remove(debugfs_file);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

64
kernel_modules/irq.c Normal file
View File

@@ -0,0 +1,64 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#irq-ko */
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/uaccess.h> /* copy_from_user, copy_to_user */
#define NAME "lkmc_irq"
#define MAX_IRQS 256
static int irqs[MAX_IRQS];
static int major;
/**
* Return value from kernel docs:*
*
* enum irqreturn
* @IRQ_NONE interrupt was not from this device or was not handled
* @IRQ_HANDLED interrupt was handled by this device
* @IRQ_WAKE_THREAD handler requests to wake the handler thread
*/
static irqreturn_t handler(int irq, void *dev)
{
pr_info("handler irq = %d dev = %d\n", irq, *(int *)dev);
return IRQ_NONE;
}
static const struct file_operations fops;
static int myinit(void)
{
int ret, i;
major = register_chrdev(0, NAME, &fops);
for (i = 0; i < MAX_IRQS; ++i) {
ret = request_irq(
i,
handler,
IRQF_SHARED,
"myirqhandler0",
&major
);
irqs[i] = ret;
pr_info("request_irq irq = %d ret = %d\n", i, ret);
}
return 0;
}
static void myexit(void)
{
int i;
for (i = 0; i < MAX_IRQS; ++i) {
if (!irqs[i]) {
free_irq(i, &major);
}
}
unregister_chrdev(major, NAME);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

View File

@@ -0,0 +1,122 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kprobes
*
* Adapted from: https://github.com/torvalds/linux/blob/v4.17/samples/kprobes/kprobe_example.c
*/
/*
* NOTE: This example is works on x86 and powerpc.
* Here's a sample kernel module showing the use of kprobes to dump a
* stack trace and selected registers when _do_fork() is called.
*
* For more information on theory of operation of kprobes, see
* Documentation/kprobes.txt
*
* You will see the trace data in /var/log/messages and on the console
* whenever _do_fork() is invoked to create a new process.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kprobes.h>
#define MAX_SYMBOL_LEN 64
static char symbol[MAX_SYMBOL_LEN] = "_do_fork";
module_param_string(symbol, symbol, sizeof(symbol), 0644);
/* For each probe you need to allocate a kprobe structure */
static struct kprobe kp = {
.symbol_name = symbol,
};
/* kprobe pre_handler: called just before the probed instruction is executed */
static int handler_pre(struct kprobe *p, struct pt_regs *regs)
{
#ifdef CONFIG_X86
pr_info("<%s> pre_handler: p->addr = 0x%px, ip = %lx, flags = 0x%lx\n",
p->symbol_name, p->addr, regs->ip, regs->flags);
#endif
#ifdef CONFIG_PPC
pr_info("<%s> pre_handler: p->addr = 0x%px, nip = 0x%lx, msr = 0x%lx\n",
p->symbol_name, p->addr, regs->nip, regs->msr);
#endif
#ifdef CONFIG_MIPS
pr_info("<%s> pre_handler: p->addr = 0x%px, epc = 0x%lx, status = 0x%lx\n",
p->symbol_name, p->addr, regs->cp0_epc, regs->cp0_status);
#endif
#ifdef CONFIG_ARM64
pr_info("<%s> pre_handler: p->addr = 0x%px, pc = 0x%lx,"
" pstate = 0x%lx\n",
p->symbol_name, p->addr, (long)regs->pc, (long)regs->pstate);
#endif
#ifdef CONFIG_S390
pr_info("<%s> pre_handler: p->addr, 0x%px, ip = 0x%lx, flags = 0x%lx\n",
p->symbol_name, p->addr, regs->psw.addr, regs->flags);
#endif
/* A dump_stack() here will give a stack backtrace */
return 0;
}
/* kprobe post_handler: called after the probed instruction is executed */
static void handler_post(struct kprobe *p, struct pt_regs *regs,
unsigned long flags)
{
#ifdef CONFIG_X86
pr_info("<%s> post_handler: p->addr = 0x%px, flags = 0x%lx\n",
p->symbol_name, p->addr, regs->flags);
#endif
#ifdef CONFIG_PPC
pr_info("<%s> post_handler: p->addr = 0x%px, msr = 0x%lx\n",
p->symbol_name, p->addr, regs->msr);
#endif
#ifdef CONFIG_MIPS
pr_info("<%s> post_handler: p->addr = 0x%px, status = 0x%lx\n",
p->symbol_name, p->addr, regs->cp0_status);
#endif
#ifdef CONFIG_ARM64
pr_info("<%s> post_handler: p->addr = 0x%px, pstate = 0x%lx\n",
p->symbol_name, p->addr, (long)regs->pstate);
#endif
#ifdef CONFIG_S390
pr_info("<%s> pre_handler: p->addr, 0x%px, flags = 0x%lx\n",
p->symbol_name, p->addr, regs->flags);
#endif
}
/*
* fault_handler: this is called if an exception is generated for any
* instruction within the pre- or post-handler, or when Kprobes
* single-steps the probed instruction.
*/
static int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr)
{
pr_info("fault_handler: p->addr = 0x%px, trap #%dn", p->addr, trapnr);
/* Return 0 because we don't handle the fault. */
return 0;
}
static int __init kprobe_init(void)
{
int ret;
kp.pre_handler = handler_pre;
kp.post_handler = handler_post;
kp.fault_handler = handler_fault;
ret = register_kprobe(&kp);
if (ret < 0) {
pr_err("register_kprobe failed, returned %d\n", ret);
return ret;
}
pr_info("Planted kprobe at %px\n", kp.addr);
return 0;
}
static void __exit kprobe_exit(void)
{
unregister_kprobe(&kp);
pr_info("kprobe at %px unregistered\n", kp.addr);
}
module_init(kprobe_init)
module_exit(kprobe_exit)
MODULE_LICENSE("GPL");

65
kernel_modules/kstrto.c Normal file
View File

@@ -0,0 +1,65 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kstrto */
#include <linux/debugfs.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h> /* copy_from_user, copy_to_user */
#include <uapi/linux/stat.h> /* S_IWUSR */
static struct dentry *toplevel_file;
static char read_buf[1024];
static int show(struct seq_file *m, void *v)
{
seq_printf(m, read_buf);
return 0;
}
static int open(struct inode *inode, struct file *file)
{
return single_open(file, show, NULL);
}
static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
{
ssize_t ret;
int kstrto_return;
unsigned long long kstrto_result;
kstrto_return = kstrtoull_from_user(buf, len, 10, &kstrto_result);
if (kstrto_return) {
/* Negative error code. */
ret = kstrto_return;
} else {
ret = len;
}
snprintf(read_buf, sizeof(read_buf), "%llu", kstrto_result + 1);
return ret;
}
static const struct file_operations fops = {
.llseek = seq_lseek,
.open = open,
.owner = THIS_MODULE,
.read = seq_read,
.release = single_release,
.write = write,
};
static int myinit(void)
{
toplevel_file = debugfs_create_file("lkmc_kstrto", S_IWUSR, NULL, NULL, &fops);
if (!toplevel_file) {
return -1;
}
return 0;
}
static void myexit(void)
{
debugfs_remove(toplevel_file);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

38
kernel_modules/kthread.c Normal file
View File

@@ -0,0 +1,38 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kthread */
#include <linux/delay.h> /* usleep_range */
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/module.h>
static struct task_struct *kthread;
static int work_func(void *data)
{
u32 i = 0;
while (!kthread_should_stop()) {
pr_info("%u\n", i);
usleep_range(1000000, 1000001);
i++;
if (i == 10)
i = 0;
}
return 0;
}
static int myinit(void)
{
kthread = kthread_create(work_func, NULL, "mykthread");
wake_up_process(kthread);
return 0;
}
static void myexit(void)
{
/* Waits for thread to return. */
kthread_stop(kthread);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

53
kernel_modules/kthreads.c Normal file
View File

@@ -0,0 +1,53 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kthreads */
#include <linux/delay.h> /* usleep_range */
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/module.h>
static struct task_struct *kthread1, *kthread2;
static int work_func1(void *data)
{
int i = 0;
while (!kthread_should_stop()) {
pr_info("1 %d\n", i);
usleep_range(1000000, 1000001);
i++;
if (i == 10)
i = 0;
}
return 0;
}
static int work_func2(void *data)
{
int i = 0;
while (!kthread_should_stop()) {
pr_info("2 %d\n", i);
usleep_range(1000000, 1000001);
i++;
if (i == 10)
i = 0;
}
return 0;
}
static int myinit(void)
{
kthread1 = kthread_create(work_func1, NULL, "mykthread1");
kthread2 = kthread_create(work_func2, NULL, "mykthread2");
wake_up_process(kthread1);
wake_up_process(kthread2);
return 0;
}
static void myexit(void)
{
kthread_stop(kthread1);
kthread_stop(kthread2);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

View File

@@ -0,0 +1,21 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#config_fortify_source */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#include <linux/slab.h>
static int myinit(void)
{
void *dst, *src;
dst = kmalloc(0x10, GFP_KERNEL);
src = kmalloc(0x1000000, GFP_KERNEL);
memcpy(dst, src, 0x1000000);
return 0;
}
static void myexit(void) {}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

139
kernel_modules/mmap.c Normal file
View File

@@ -0,0 +1,139 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#mmap */
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h> /* min */
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/uaccess.h> /* copy_from_user, copy_to_user */
#include <linux/slab.h>
static const char *filename = "lkmc_mmap";
enum { BUFFER_SIZE = 4 };
struct mmap_info {
char *data;
};
/* After unmap. */
static void vm_close(struct vm_area_struct *vma)
{
pr_info("vm_close\n");
}
/* First page access. */
int (*fault)(struct vm_fault *vmf);
static int vm_fault(struct vm_fault *vmf)
{
struct page *page;
struct mmap_info *info;
pr_info("vm_fault\n");
info = (struct mmap_info *)vmf->vma->vm_private_data;
if (info->data) {
page = virt_to_page(info->data);
get_page(page);
vmf->page = page;
}
return 0;
}
/* Aftr mmap. TODO vs mmap, when can this happen at a different time than mmap? */
static void vm_open(struct vm_area_struct *vma)
{
pr_info("vm_open\n");
}
static struct vm_operations_struct vm_ops =
{
.close = vm_close,
.fault = vm_fault,
.open = vm_open,
};
static int mmap(struct file *filp, struct vm_area_struct *vma)
{
pr_info("mmap\n");
vma->vm_ops = &vm_ops;
vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
vma->vm_private_data = filp->private_data;
vm_open(vma);
return 0;
}
static int open(struct inode *inode, struct file *filp)
{
struct mmap_info *info;
pr_info("open\n");
info = kmalloc(sizeof(struct mmap_info), GFP_KERNEL);
pr_info("virt_to_phys = 0x%llx\n", (unsigned long long)virt_to_phys((void *)info));
info->data = (char *)get_zeroed_page(GFP_KERNEL);
memcpy(info->data, "asdf", BUFFER_SIZE);
filp->private_data = info;
return 0;
}
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
{
struct mmap_info *info;
int ret;
pr_info("read\n");
info = filp->private_data;
ret = min(len, (size_t)BUFFER_SIZE);
if (copy_to_user(buf, info->data, ret)) {
ret = -EFAULT;
}
return ret;
}
static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
{
struct mmap_info *info;
pr_info("write\n");
info = filp->private_data;
if (copy_from_user(info->data, buf, min(len, (size_t)BUFFER_SIZE))) {
return -EFAULT;
} else {
return len;
}
}
static int release(struct inode *inode, struct file *filp)
{
struct mmap_info *info;
pr_info("release\n");
info = filp->private_data;
free_page((unsigned long)info->data);
kfree(info);
filp->private_data = NULL;
return 0;
}
static const struct file_operations fops = {
.mmap = mmap,
.open = open,
.release = release,
.read = read,
.write = write,
};
static int myinit(void)
{
proc_create(filename, 0, NULL, &fops);
return 0;
}
static void myexit(void)
{
remove_proc_entry(filename, NULL);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

View File

@@ -0,0 +1,22 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#module_info */
#include <linux/module.h>
#include <linux/kernel.h>
static int myinit(void)
{
/* Set by default based on the module file name. */
pr_info("name = %s\n", THIS_MODULE->name);
pr_info("version = %s\n", THIS_MODULE->version);
/* ERROR: nope, not part of struct module. */
/*pr_info("asdf = %s\n", THIS_MODULE->asdf);*/
return 0;
}
static void myexit(void) {}
module_init(myinit)
module_exit(myexit)
MODULE_INFO(asdf, "qwer");
MODULE_VERSION("1.0");
MODULE_LICENSE("GPL");

22
kernel_modules/myprintk.c Normal file
View File

@@ -0,0 +1,22 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#pr_debug */
#include <linux/module.h>
#include <linux/kernel.h>
static int myinit(void)
{
pr_alert("pr_alert\n");
pr_crit("pr_crit\n");
pr_err("pr_err");
pr_warning("pr_warning\n");
pr_notice("pr_notice\n");
pr_info("pr_info\n");
pr_debug("pr_debug\n");
return 0;
}
static void myexit(void) { }
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

66
kernel_modules/netlink.c Normal file
View File

@@ -0,0 +1,66 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#netlink-sockets */
#include <linux/delay.h> /* usleep_range */
#include <linux/module.h>
#include <linux/netlink.h>
#include <linux/skbuff.h>
#include <net/sock.h>
#include "../include/netlink.h"
struct sock *nl_sk = NULL;
static u32 count;
static u32 sleep;
module_param(sleep, int, S_IRUSR | S_IWUSR);
static void callback(struct sk_buff *skb)
{
char readbuf[9];
size_t readbuflen;
int pid;
int res;
struct nlmsghdr *nlh;
struct sk_buff *skb_out;
nlh = (struct nlmsghdr *)skb->data;
pr_info("kernel received: %s\n", (char *)nlmsg_data(nlh));
if (sleep)
usleep_range(1000000, 1000001);
readbuflen = snprintf(readbuf, sizeof(readbuf), "%x", count);
count++;
pid = nlh->nlmsg_pid;
skb_out = nlmsg_new(readbuflen, 0);
if (!skb_out) {
pr_err("nlmsg_new\n");
return;
}
nlh = nlmsg_put(skb_out, 0, 0, NLMSG_DONE, readbuflen, 0);
NETLINK_CB(skb_out).dst_group = 0;
strncpy(nlmsg_data(nlh), readbuf, readbuflen);
res = nlmsg_unicast(nl_sk, skb_out, pid);
if (res < 0)
pr_info("nlmsg_unicast\n");
}
static int myinit(void)
{
struct netlink_kernel_cfg cfg = {
.input = callback,
};
nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);
if (!nl_sk) {
pr_err("netlink_kernel_create\n");
return -10;
}
return 0;
}
static void myexit(void)
{
netlink_kernel_release(nl_sk);
}
module_init(myinit);
module_exit(myexit);
MODULE_LICENSE("GPL");

21
kernel_modules/oops.c Normal file
View File

@@ -0,0 +1,21 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kernel-panic-and-oops */
#include <linux/module.h>
#include <linux/kernel.h>
static int myinit(void)
{
pr_info("oops myinit\n");
*(int *)0 = 0;
pr_info("oops after\n");
return 0;
}
static void myexit(void)
{
pr_info("oops myexit\n");
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

21
kernel_modules/panic.c Normal file
View File

@@ -0,0 +1,21 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kernel-panic-and-oops */
#include <linux/module.h>
#include <linux/kernel.h>
static int myinit(void)
{
pr_info("panic myinit\n");
panic("hello panic");
pr_info("panic after\n");
return 0;
}
static void myexit(void)
{
pr_info("panic myexit\n");
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

55
kernel_modules/params.c Normal file
View File

@@ -0,0 +1,55 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#kernel-module-parameters */
#include <linux/debugfs.h>
#include <linux/delay.h> /* usleep_range */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */
#include <uapi/linux/stat.h> /* S_IRUSR | S_IWUSR */
static u32 i = 0;
static u32 j = 0;
module_param(i, int, S_IRUSR | S_IWUSR);
module_param(j, int, S_IRUSR | S_IWUSR);
MODULE_PARM_DESC(i, "my favorite int");
MODULE_PARM_DESC(j, "my second favorite int");
static struct dentry *debugfs_file;
static int show(struct seq_file *m, void *v)
{
char kbuf[18];
int ret;
ret = snprintf(kbuf, sizeof(kbuf), "%d %d", i, j);
seq_printf(m, kbuf);
return 0;
}
static int open(struct inode *inode, struct file *file)
{
return single_open(file, show, NULL);
}
static const struct file_operations fops = {
.llseek = seq_lseek,
.open = open,
.owner = THIS_MODULE,
.read = seq_read,
.release = single_release,
};
static int myinit(void)
{
debugfs_file = debugfs_create_file("lkmc_params", S_IRUSR, NULL, NULL, &fops);
return 0;
}
static void myexit(void)
{
debugfs_remove(debugfs_file);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

91
kernel_modules/pci_min.c Normal file
View File

@@ -0,0 +1,91 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#pci_min */
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/uaccess.h> /* copy_from_user, copy_to_user */
#define BAR 0
#define CDEV_NAME "lkmc_hw_pci_min"
#define EDU_DEVICE_ID 0x11e9
#define QEMU_VENDOR_ID 0x1234
static struct pci_device_id id_table[] = {
{ PCI_DEVICE(QEMU_VENDOR_ID, EDU_DEVICE_ID), },
{ 0, }
};
MODULE_DEVICE_TABLE(pci, id_table);
static int major;
static struct pci_dev *pdev;
static void __iomem *mmio;
static struct file_operations fops = {
.owner = THIS_MODULE,
};
static irqreturn_t irq_handler(int irq, void *dev)
{
pr_info("irq_handler irq = %d dev = %d\n", irq, *(int *)dev);
iowrite32(0, mmio + 4);
return IRQ_HANDLED;
}
static int probe(struct pci_dev *dev, const struct pci_device_id *id)
{
pr_info("probe\n");
major = register_chrdev(0, CDEV_NAME, &fops);
pdev = dev;
if (pci_enable_device(dev) < 0) {
dev_err(&(pdev->dev), "pci_enable_device\n");
goto error;
}
if (pci_request_region(dev, BAR, "myregion0")) {
dev_err(&(pdev->dev), "pci_request_region\n");
goto error;
}
mmio = pci_iomap(pdev, BAR, pci_resource_len(pdev, BAR));
pr_info("dev->irq = %u\n", dev->irq);
if (request_irq(dev->irq, irq_handler, IRQF_SHARED, "pci_irq_handler0", &major) < 0) {
dev_err(&(dev->dev), "request_irq\n");
goto error;
}
iowrite32(0x12345678, mmio);
return 0;
error:
return 1;
}
static void remove(struct pci_dev *dev)
{
pr_info("remove\n");
free_irq(dev->irq, &major);
pci_release_region(dev, BAR);
unregister_chrdev(major, CDEV_NAME);
}
static struct pci_driver pci_driver = {
.name = CDEV_NAME,
.id_table = id_table,
.probe = probe,
.remove = remove,
};
static int myinit(void)
{
if (pci_register_driver(&pci_driver) < 0) {
return 1;
}
return 0;
}
static void myexit(void)
{
pci_unregister_driver(&pci_driver);
}
module_init(myinit);
module_exit(myexit);
MODULE_LICENSE("GPL");

72
kernel_modules/pmccntr.c Normal file
View File

@@ -0,0 +1,72 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#pmccntr */
#include <linux/debugfs.h>
#include <linux/errno.h> /* EFAULT */
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/printk.h> /* pr_info */
#include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */
#include <linux/uaccess.h> /* copy_from_user, copy_to_user */
#include <uapi/linux/stat.h> /* S_IRUSR */
static struct dentry *debugfs_file;
static int show(struct seq_file *m, void *v)
{
u32 pmccntr;
#if defined(__arm__)
/* Invalid aarch64 asm. */
/* TODO Internal error: Oops - undefined instruction: 0 [#1] ARM */
/* Enable userland access to conter. */
/* PMUSERENR = 1 */
/*__asm__ __volatile__ ("mcr p15, 0, %0, c9, c14, 0" :: "r"(1));*/
/* TODO oops undefined instruction. */
/* PMCR.E (bit 0) = 1 */
/*__asm__ __volatile__ ("mcr p15, 0, %0, c9, c12, 0" :: "r"(1));*/
/* TODO oops undefined instruction. */
/* Enable counter. */
/* PMCNTENSET.C (bit 31) = 1 */
/*__asm__ __volatile__ ("mcr p15, 0, %0, c9, c12, 1" :: "r"(1 << 31));*/
/* Get counter value. */
__asm__ __volatile__ ("mrc p15, 0, %0, c15, c12, 1" : "=r" (pmccntr));
#else
pmccntr = 0;
#endif
seq_printf(m, "%8.8llX\n", (unsigned long long)pmccntr);
return 0;
}
static int open(struct inode *inode, struct file *file)
{
return single_open(file, show, NULL);
}
static const struct file_operations fops = {
.llseek = seq_lseek,
.open = open,
.owner = THIS_MODULE,
.read = seq_read,
.release = single_release,
};
static int myinit(void)
{
debugfs_file = debugfs_create_file("lkmc_pmccntr", S_IRUSR, NULL, NULL, &fops);
if (!debugfs_file) {
return -1;
}
return 0;
}
static void myexit(void)
{
debugfs_remove(debugfs_file);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

84
kernel_modules/poll.c Normal file
View File

@@ -0,0 +1,84 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#poll */
#include <linux/debugfs.h>
#include <linux/delay.h> /* usleep_range */
#include <linux/errno.h> /* EFAULT */
#include <linux/fs.h>
#include <linux/jiffies.h>
#include <linux/kernel.h> /* min */
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/poll.h>
#include <linux/printk.h> /* printk */
#include <linux/uaccess.h> /* copy_from_user, copy_to_user */
#include <linux/wait.h> /* wait_queue_head_t, wait_event_interruptible, wake_up_interruptible */
#include <uapi/linux/stat.h> /* S_IRUSR */
static char readbuf[1024];
static size_t readbuflen;
static struct dentry *debugfs_file;
static struct task_struct *kthread;
static wait_queue_head_t waitqueue;
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
{
ssize_t ret;
if (copy_to_user(buf, readbuf, readbuflen)) {
ret = -EFAULT;
} else {
ret = readbuflen;
}
/* This is normal pipe behaviour: data gets drained once a reader reads from it. */
/* https://stackoverflow.com/questions/1634580/named-pipes-fifos-on-unix-with-multiple-readers */
readbuflen = 0;
return ret;
}
/* If you return 0 here, then the kernel will sleep until an event happens in the queue.
*
* This gets called again every time an event happens in the wait queue.
*/
unsigned int poll(struct file *filp, struct poll_table_struct *wait)
{
poll_wait(filp, &waitqueue, wait);
if (readbuflen)
return POLLIN;
else
return 0;
}
static int kthread_func(void *data)
{
while (!kthread_should_stop()) {
readbuflen = snprintf(readbuf, sizeof(readbuf), "%llu", (unsigned long long)jiffies);
usleep_range(1000000, 1000001);
wake_up(&waitqueue);
}
return 0;
}
static const struct file_operations fops = {
.owner = THIS_MODULE,
.read = read,
.poll = poll
};
static int myinit(void)
{
debugfs_file = debugfs_create_file(
"lkmc_poll", S_IRUSR | S_IWUSR, NULL, NULL, &fops);
init_waitqueue_head(&waitqueue);
kthread = kthread_create(kthread_func, NULL, "mykthread");
wake_up_process(kthread);
return 0;
}
static void myexit(void)
{
kthread_stop(kthread);
debugfs_remove(debugfs_file);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

43
kernel_modules/procfs.c Normal file
View File

@@ -0,0 +1,43 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#procfs */
#include <linux/debugfs.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h> /* seq_read, seq_lseek, single_open, single_release */
#include <uapi/linux/stat.h> /* S_IRUSR */
static const char *filename = "lkmc_procfs";
static int show(struct seq_file *m, void *v)
{
seq_printf(m, "abcd\n");
return 0;
}
static int open(struct inode *inode, struct file *file)
{
return single_open(file, show, NULL);
}
static const struct file_operations fops = {
.llseek = seq_lseek,
.open = open,
.owner = THIS_MODULE,
.read = seq_read,
.release = single_release,
};
static int myinit(void)
{
proc_create(filename, 0, NULL, &fops);
return 0;
}
static void myexit(void)
{
remove_proc_entry(filename, NULL);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

271
kernel_modules/qemu_edu.c Normal file
View File

@@ -0,0 +1,271 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#qemu-edu */
#include <linux/cdev.h> /* cdev_ */
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/uaccess.h> /* put_user */
#define BAR 0
#define CDEV_NAME "lkmc_pci"
#define EDU_DEVICE_ID 0x11e8
#define QEMU_VENDOR_ID 0x1234
/* Registers. */
#define IO_IRQ_STATUS 0x24
#define IO_IRQ_ACK 0x64
#define IO_DMA_SRC 0x80
#define IO_DMA_DST 0x88
#define IO_DMA_CNT 0x90
#define IO_DMA_CMD 0x98
/* Constants. */
/* TODO what is this magic value for? Can't it be always deduced from the direction? */
#define DMA_BASE 0x40000
/* Must give this for the DMA command to to anything. */
#define DMA_CMD 0x1
/* If given, device -> RAM. Otherwise: RAM -> dev. */
#define DMA_FROM_DEV 0x2
/* If given, raise an IRQ, and write 100 to the IRQ status register. */
#define DMA_IRQ 0x4
static struct pci_device_id pci_ids[] = {
{ PCI_DEVICE(QEMU_VENDOR_ID, EDU_DEVICE_ID), },
{ 0, }
};
MODULE_DEVICE_TABLE(pci, pci_ids);
static int major;
static struct pci_dev *pdev;
static void __iomem *mmio;
static irqreturn_t irq_handler(int irq, void *dev)
{
int devi;
irqreturn_t ret;
u32 irq_status;
devi = *(int *)dev;
if (devi == major) {
irq_status = ioread32(mmio + IO_IRQ_STATUS);
pr_info("irq_handler irq = %d dev = %d irq_status = %llx\n",
irq, devi, (unsigned long long)irq_status);
/* Must do this ACK, or else the interrupts just keeps firing. */
iowrite32(irq_status, mmio + IO_IRQ_ACK);
ret = IRQ_HANDLED;
} else {
ret = IRQ_NONE;
}
return ret;
}
static ssize_t read(struct file *filp, char __user *buf, size_t len, loff_t *off)
{
ssize_t ret;
u32 kbuf;
if (*off % 4 || len == 0) {
ret = 0;
} else {
kbuf = ioread32(mmio + *off);
if (copy_to_user(buf, (void *)&kbuf, 4)) {
ret = -EFAULT;
} else {
ret = 4;
(*off)++;
}
}
return ret;
}
static ssize_t write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
{
ssize_t ret;
u32 kbuf;
ret = len;
if (!(*off % 4)) {
if (copy_from_user((void *)&kbuf, buf, 4) || len != 4) {
ret = -EFAULT;
} else {
iowrite32(kbuf, mmio + *off);
}
}
return ret;
}
static loff_t llseek(struct file *filp, loff_t off, int whence)
{
filp->f_pos = off;
return off;
}
/* These fops are a bit daft since read and write interfaces don't map well to IO registers.
*
* One ioctl per register would likely be the saner option. But we are lazy.
*
* We use the fact that every IO is aligned to 4 bytes. Misaligned reads means EOF. */
static struct file_operations fops = {
.owner = THIS_MODULE,
.llseek = llseek,
.read = read,
.write = write,
};
/* https://stackoverflow.com/questions/5059501/probe-method-device-drivers/44739823#44739823
*
* Called just after insmod if the hardware device is connected,
* not called otherwise.
*
* 0: all good
* 1: failed
*/
static int pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
{
/* https://stackoverflow.com/questions/31382803/how-does-dev-family-functions-are-useful-while-debugging-kernel/44734857#44734857 */
dev_info(&(dev->dev), "pci_probe\n");
major = register_chrdev(0, CDEV_NAME, &fops);
pdev = dev;
if (pci_enable_device(dev) < 0) {
dev_err(&(dev->dev), "pci_enable_device\n");
goto error;
}
if (pci_request_region(dev, BAR, "myregion0")) {
dev_err(&(dev->dev), "pci_request_region\n");
goto error;
}
mmio = pci_iomap(dev, BAR, pci_resource_len(dev, BAR));
/* IRQ setup.
*
* pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &val);
* has a different value and does not work if we insert the PCI device
* after boot with device_add:
* https://stackoverflow.com/questions/44740254/how-to-handle-interrupts-from-a-pci-device-that-already-have-a-non-shareable-han?noredirect=1#comment76558680_44740254
*/
if (request_irq(dev->irq, irq_handler, IRQF_SHARED, "pci_irq_handler0", &major) < 0) {
dev_err(&(dev->dev), "request_irq\n");
goto error;
}
/* Optional sanity checks. The PCI is ready now, all of this could also be called from fops. */
{
unsigned i;
u8 val;
/* Check that we are using MEM instead of IO.
*
* In QEMU, the type is defiened by either:
*
* - PCI_BASE_ADDRESS_SPACE_IO
* - PCI_BASE_ADDRESS_SPACE_MEMORY
*/
if ((pci_resource_flags(dev, BAR) & IORESOURCE_MEM) != IORESOURCE_MEM) {
dev_err(&(dev->dev), "pci_resource_flags\n");
goto error;
}
/* 1Mb, as defined by the "1 << 20" in QEMU's memory_region_init_io. Same as pci_resource_len. */
resource_size_t start = pci_resource_start(dev, BAR);
resource_size_t end = pci_resource_end(dev, BAR);
pr_info("length %llx\n", (unsigned long long)(end + 1 - start));
/* The PCI standardized 64 bytes of the configuration space, see LDD3. */
for (i = 0; i < 64u; ++i) {
pci_read_config_byte(dev, i, &val);
pr_info("config %x %x\n", i, val);
}
pr_info("dev->irq %x\n", dev->irq);
/* Initial value of the IO memory. */
for (i = 0; i < 0x28; i += 4) {
pr_info("io %x %x\n", i, ioread32((void*)(mmio + i)));
}
/* DMA test.
*
* TODO:
*
* - deal with interrupts properly.
* - printf / gdb in QEMU source says dma_buf is not being set correctly
*
* Resources:
*
* - http://elixir.free-electrons.com/linux/v4.12/source/Documentation/DMA-API-HOWTO.txt
* - http://www.makelinux.net/ldd3/chp-15-sect-4
* - https://stackoverflow.com/questions/32592734/are-there-any-dma-linux-kernel-driver-example-with-pcie-for-fpga/44716747#44716747
* - https://stackoverflow.com/questions/17913679/how-to-instantiate-and-use-a-dma-driver-linux-module
* - https://stackoverflow.com/questions/5539375/linux-kernel-device-driver-to-dma-from-a-device-into-user-space-memory
* - RPI userland /dev/mem https://github.com/Wallacoloo/Raspberry-Pi-DMA-Example
* - https://stackoverflow.com/questions/34188369/easiest-way-to-use-dma-in-linux
*/
{
dma_addr_t dma_handle_from, dma_handle_to;
void *vaddr_from, *vaddr_to;
enum { SIZE = 4 };
/* RAM -> device. */
vaddr_from = dma_alloc_coherent(&(dev->dev), 4, &dma_handle_from, GFP_ATOMIC);
dev_info(&(dev->dev), "vaddr_from = %px\n", vaddr_from);
dev_info(&(dev->dev), "dma_handle_from = %llx\n", (unsigned long long)dma_handle_from);
*((volatile u32*)vaddr_from) = 0x12345678;
iowrite32((u32)dma_handle_from, mmio + IO_DMA_SRC);
iowrite32(DMA_BASE, mmio + IO_DMA_DST);
iowrite32(SIZE, mmio + IO_DMA_CNT);
iowrite32(DMA_CMD | DMA_IRQ, mmio + IO_DMA_CMD);
/* device -> RAM. */
vaddr_to = dma_alloc_coherent(&(dev->dev), 4, &dma_handle_to, GFP_ATOMIC);
dev_info(&(dev->dev), "vaddr_to = %px\n", vaddr_to);
dev_info(&(dev->dev), "dma_handle_to = %llx\n", (unsigned long long)dma_handle_to);
/*
iowrite32(DMA_BASE, mmio + IO_DMA_SRC);
iowrite32((u32)dma_handle_to, mmio + IO_DMA_DST);
iowrite32(SIZE, mmio + IO_DMA_CNT);
iowrite32(DMA_CMD | DMA_FROM_DEV | DMA_IRQ, mmio + IO_DMA_CMD);
dev_info(&(dev->dev), "*vaddr_to = %llx\n", (unsigned long long)(*((u32*)vaddr_to)));
*/
/*dma_free_coherent(&(dev->dev), SIZE, vaddr_from, dma_handle_from);*/
/*dma_free_coherent(&(dev->dev), SIZE, vaddr_to, dma_handle_to);*/
}
}
return 0;
error:
return 1;
}
static void pci_remove(struct pci_dev *dev)
{
pr_info("pci_remove\n");
free_irq(pdev->irq, &major);
pci_release_region(dev, BAR);
unregister_chrdev(major, CDEV_NAME);
}
static struct pci_driver pci_driver = {
.name = "lkmc_pci",
.id_table = pci_ids,
.probe = pci_probe,
.remove = pci_remove,
};
static int myinit(void)
{
if (pci_register_driver(&pci_driver) < 0) {
return 1;
}
return 0;
}
static void myexit(void)
{
pci_unregister_driver(&pci_driver);
}
module_init(myinit);
module_exit(myexit);
MODULE_LICENSE("GPL");

24
kernel_modules/ring0.c Normal file
View File

@@ -0,0 +1,24 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#ring0 */
#include <linux/module.h>
#include <linux/kernel.h>
#include "../include/ring0.h"
static int myinit(void)
{
#if defined(__x86_64__) || defined(__i386__)
Ring0Regs ring0_regs;
ring0_get_control_regs(&ring0_regs);
pr_info("cr0 = 0x%8.8llX\n", (unsigned long long)ring0_regs.cr0);
pr_info("cr2 = 0x%8.8llX\n", (unsigned long long)ring0_regs.cr2);
pr_info("cr3 = 0x%8.8llX\n", (unsigned long long)ring0_regs.cr3);
#endif
return 0;
}
static void myexit(void) {}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

39
kernel_modules/schedule.c Normal file
View File

@@ -0,0 +1,39 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#schedule */
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <uapi/linux/stat.h> /* S_IRUSR | S_IWUSR */
static int do_schedule = 1;
module_param_named(schedule, do_schedule, int, S_IRUSR | S_IWUSR);
static struct task_struct *kthread;
static int work_func(void *data)
{
unsigned int i = 0;
while (!kthread_should_stop()) {
pr_info("%u\n", i);
i++;
if (do_schedule)
schedule();
}
return 0;
}
static int myinit(void)
{
kthread = kthread_create(work_func, NULL, "mykthread");
wake_up_process(kthread);
return 0;
}
static void myexit(void)
{
kthread_stop(kthread);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

113
kernel_modules/seq_file.c Normal file
View File

@@ -0,0 +1,113 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#seq_file */
#include <linux/debugfs.h>
#include <linux/errno.h> /* EFAULT */
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/printk.h> /* pr_info */
#include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */
#include <linux/slab.h> /* kmalloc, kfree */
#include <linux/uaccess.h> /* copy_from_user, copy_to_user */
#include <uapi/linux/stat.h> /* S_IRUSR */
static int max = 2;
module_param(max, int, S_IRUSR | S_IWUSR);
static struct dentry *debugfs_file;
/* Called at the beginning of every read.
*
* The return value is passsed to the first show.
* It normally represents the current position of the iterator.
* It could be any struct, but we use just a single integer here.
*
* NULL return means stop should be called next, and so the read will be empty..
* This happens for example for an ftell that goes beyond the file size.
*/
static void *start(struct seq_file *s, loff_t *pos)
{
loff_t *spos;
pr_info("start pos = %llx\n", (unsigned long long)*pos);
spos = kmalloc(sizeof(loff_t), GFP_KERNEL);
if (!spos || *pos >= max)
return NULL;
*spos = *pos;
return spos;
}
/* The return value is passed to next show.
* If NULL, stop is called next instead of show, and read ends.
*
* Can get called multiple times, until enough data is returned for the read.
*/
static void *next(struct seq_file *s, void *v, loff_t *pos)
{
loff_t *spos;
spos = v;
pr_info("next pos = %llx\n", (unsigned long long)*pos);
if (*pos >= max)
return NULL;
*pos = ++*spos;
return spos;
}
/* Called at the end of every read. */
static void stop(struct seq_file *s, void *v)
{
pr_info("stop\n");
kfree(v);
}
/* Return 0 means success, SEQ_SKIP ignores previous prints, negative for error. */
static int show(struct seq_file *s, void *v)
{
loff_t *spos;
spos = v;
pr_info("show pos = %llx\n", (unsigned long long)*spos);
seq_printf(s, "%llx\n", (long long unsigned)*spos);
return 0;
}
static struct seq_operations my_seq_ops = {
.next = next,
.show = show,
.start = start,
.stop = stop,
};
static int open(struct inode *inode, struct file *file)
{
pr_info("open\n");
return seq_open(file, &my_seq_ops);
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.llseek = seq_lseek,
.open = open,
.read = seq_read,
.release = seq_release
};
static int myinit(void)
{
debugfs_file = debugfs_create_file(
"lkmc_seq_file", S_IRUSR, NULL, NULL, &fops);
if (debugfs_file) {
return 0;
} else {
return -EINVAL;
}
}
static void myexit(void)
{
debugfs_remove(debugfs_file);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

View File

@@ -0,0 +1,46 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#seq_file-single_open */
#include <linux/debugfs.h>
#include <linux/errno.h> /* EFAULT */
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/printk.h> /* pr_info */
#include <linux/seq_file.h> /* seq_read, seq_lseek, single_release */
#include <linux/uaccess.h> /* copy_from_user, copy_to_user */
#include <uapi/linux/stat.h> /* S_IRUSR */
static struct dentry *debugfs_file;
static int show(struct seq_file *m, void *v)
{
seq_printf(m, "ab\ncd\n");
return 0;
}
static int open(struct inode *inode, struct file *file)
{
return single_open(file, show, NULL);
}
static const struct file_operations fops = {
.llseek = seq_lseek,
.open = open,
.owner = THIS_MODULE,
.read = seq_read,
.release = single_release,
};
static int myinit(void)
{
debugfs_file = debugfs_create_file("lkmc_seq_file_single_open", S_IRUSR, NULL, NULL, &fops);
return 0;
}
static void myexit(void)
{
debugfs_remove(debugfs_file);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

25
kernel_modules/sleep.c Normal file
View File

@@ -0,0 +1,25 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#sleep */
#include <linux/delay.h> /* usleep_range */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/types.h> /* atomic_t */
static u32 n = 5;
module_param(n, int, S_IRUSR | S_IWUSR);
static int myinit(void)
{
u32 i;
for (i = 0; i < n; ++i) {
pr_info("%d\n", i);
usleep_range(1000000, 1000001);
}
return 0;
}
static void myexit(void) {}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

View File

@@ -0,0 +1,19 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#config_fortify_source */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
static int myinit(void)
{
/* Missing terminaing NUL '\0'. */
char buf[] = {'p', 'w', 'n'};
pr_info("%llu\n", (long long unsigned)strlen(buf));
return 0;
}
static void myexit(void) {}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

64
kernel_modules/sysfs.c Normal file
View File

@@ -0,0 +1,64 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#sysfs */
#include <linux/init.h>
#include <linux/kobject.h>
#include <linux/module.h>
#include <linux/stat.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <uapi/linux/stat.h> /* S_IRUSR, S_IWUSR */
enum { FOO_SIZE_MAX = 4 };
static int foo_size;
static char foo_tmp[FOO_SIZE_MAX];
static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buff)
{
strncpy(buff, foo_tmp, foo_size);
return foo_size;
}
static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
const char *buff, size_t count)
{
foo_size = min(count, (size_t)FOO_SIZE_MAX);
strncpy(foo_tmp, buff, foo_size);
return count;
}
static struct kobj_attribute foo_attribute =
__ATTR(foo, S_IRUGO | S_IWUSR, foo_show, foo_store);
static struct attribute *attrs[] = {
&foo_attribute.attr,
NULL,
};
static struct attribute_group attr_group = {
.attrs = attrs,
};
static struct kobject *kobj;
static int myinit(void)
{
int ret;
kobj = kobject_create_and_add("lkmc_sysfs", kernel_kobj);
if (!kobj)
return -ENOMEM;
ret = sysfs_create_group(kobj, &attr_group);
if (ret)
kobject_put(kobj);
return ret;
}
static void myexit(void)
{
kobject_put(kobj);
}
module_init(myinit);
module_exit(myexit);
MODULE_LICENSE("GPL");

39
kernel_modules/timer.c Normal file
View File

@@ -0,0 +1,39 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#timers */
#include <linux/jiffies.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/timer.h>
static int i;
/* We would normally mark this as static and give it a more generic name.
* But let's do it like this this time for the sake of our GDB kernel module step debugging example. */
void lkmc_timer_callback(struct timer_list *data);
static unsigned long onesec;
DEFINE_TIMER(mytimer, lkmc_timer_callback);
void lkmc_timer_callback(struct timer_list *data)
{
pr_info("%d\n", i);
i++;
if (i == 10)
i = 0;
mod_timer(&mytimer, jiffies + onesec);
}
static int myinit(void)
{
onesec = msecs_to_jiffies(1000);
mod_timer(&mytimer, jiffies + onesec);
return 0;
}
static void myexit(void)
{
del_timer(&mytimer);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

19
kernel_modules/vermagic.c Normal file
View File

@@ -0,0 +1,19 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#vermagic */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/vermagic.h>
static int myinit(void)
{
pr_info("VERMAGIC_STRING = " VERMAGIC_STRING "\n");
/* Nice try, but it is not a member. */
/*pr_info("THIS_MODULE->vermagic = %s\n", THIS_MODULE->vermagic);*/
return 0;
}
static void myexit(void) {}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

View File

@@ -0,0 +1,17 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#vermagic */
#include <linux/module.h>
#include <linux/kernel.h>
static int myinit(void)
{
pr_info("vermagic_fail\n");
return 0;
}
static void myexit(void) {}
module_init(myinit)
module_exit(myexit)
MODULE_INFO(vermagic, "asdfqwer");
MODULE_LICENSE("GPL");

View File

@@ -0,0 +1,66 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#virt_to_phys */
#include <asm/io.h> /* virt_to_phys */
#include <linux/debugfs.h>
#include <linux/delay.h> /* usleep_range */
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/seq_file.h> /* single_open, single_release */
#include <linux/slab.h> /* kmalloc, kfree */
static volatile u32 *kmalloc_ptr;
static volatile u32 static_var;
static struct dentry *debugfs_file;
static int show(struct seq_file *m, void *v)
{
seq_printf(m,
"*kmalloc_ptr = 0x%llx\n"
"kmalloc_ptr = %px\n"
"virt_to_phys(kmalloc_ptr) = 0x%llx\n"
"static_var = 0x%llx\n"
"&static_var = %px\n"
"virt_to_phys(&static_var) = 0x%llx\n",
(unsigned long long)*kmalloc_ptr,
kmalloc_ptr,
(unsigned long long)virt_to_phys((void *)kmalloc_ptr),
(unsigned long long)static_var,
&static_var,
(unsigned long long)virt_to_phys((void *)&static_var)
);
return 0;
}
static int open(struct inode *inode, struct file *file)
{
return single_open(file, show, NULL);
}
static const struct file_operations fops = {
.llseek = seq_lseek,
.open = open,
.owner = THIS_MODULE,
.read = seq_read,
.release = single_release,
};
static int myinit(void)
{
kmalloc_ptr = kmalloc(sizeof(kmalloc_ptr), GFP_KERNEL);
*kmalloc_ptr = 0x12345678;
static_var = 0x12345678;
debugfs_file = debugfs_create_file("lkmc_virt_to_phys", S_IRUSR, NULL, NULL, &fops);
return 0;
}
static void myexit(void)
{
debugfs_remove(debugfs_file);
kfree((void *)kmalloc_ptr);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

View File

@@ -0,0 +1,74 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#wait-queues */
#include <linux/delay.h> /* usleep_range */
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/wait.h> /* wait_queue_head_t, wait_event_interruptible, wake_up_interruptible */
static struct task_struct *kthread_wake;
static struct task_struct *kthread_sleep1;
static struct task_struct *kthread_sleep2;
static wait_queue_head_t queue;
static atomic_t awake1 = ATOMIC_INIT(0);
static atomic_t awake2 = ATOMIC_INIT(0);
static int kthread_wake_func(void *data)
{
unsigned int i = 0;
while (!kthread_should_stop()) {
pr_info("0 %u\n", i);
usleep_range(1000000, 1000001);
atomic_set(&awake1, 1);
atomic_set(&awake2, 1);
wake_up(&queue);
i++;
}
return 0;
}
static int kthread_sleep_func_1(void *data)
{
unsigned int i = 0;
while (!kthread_should_stop()) {
pr_info("1 %u\n", i);
i++;
wait_event(queue, atomic_read(&awake1));
atomic_set(&awake1, 0);
schedule();
}
return 0;
}
static int kthread_sleep_func_2(void *data)
{
unsigned int i = 0;
while (!kthread_should_stop()) {
pr_info("2 %u\n", i);
i++;
wait_event(queue, atomic_read(&awake2));
atomic_set(&awake2, 0);
schedule();
}
return 0;
}
int init_module(void)
{
init_waitqueue_head(&queue);
kthread_wake = kthread_create(kthread_wake_func, NULL, "wake");
kthread_sleep1 = kthread_create(kthread_sleep_func_1, NULL, "sleep1");
kthread_sleep2 = kthread_create(kthread_sleep_func_2, NULL, "sleep2");
wake_up_process(kthread_wake);
wake_up_process(kthread_sleep1);
wake_up_process(kthread_sleep2);
return 0;
}
void cleanup_module(void)
{
kthread_stop(kthread_sleep2);
kthread_stop(kthread_sleep1);
kthread_stop(kthread_wake);
}
MODULE_LICENSE("GPL");

View File

@@ -0,0 +1,74 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#wait-queues */
#include <linux/delay.h> /* usleep_range */
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/module.h>
#include <linux/wait.h> /* wait_queue_head_t, wait_event_interruptible, wake_up_interruptible */
static struct task_struct *kthread_wake;
static struct task_struct *kthread_sleep1;
static struct task_struct *kthread_sleep2;
static wait_queue_head_t queue;
static atomic_t awake1 = ATOMIC_INIT(0);
static atomic_t awake2 = ATOMIC_INIT(0);
static int kthread_wake_func(void *data)
{
unsigned int i = 0;
while (!kthread_should_stop()) {
pr_info("0 %u\n", i);
usleep_range(1000000, 1000001);
atomic_set(&awake1, 1);
atomic_set(&awake2, 1);
wake_up(&queue);
i++;
}
return 0;
}
static int kthread_sleep_func_1(void *data)
{
unsigned int i = 0;
while (!kthread_should_stop()) {
pr_info("1 %u\n", i);
i++;
wait_event(queue, atomic_read(&awake1));
atomic_set(&awake1, 0);
schedule();
}
return 0;
}
static int kthread_sleep_func_2(void *data)
{
unsigned int i = 0;
while (!kthread_should_stop()) {
pr_info("2 %u\n", i);
i++;
wait_event(queue, atomic_read(&awake2));
atomic_set(&awake2, 0);
schedule();
}
return 0;
}
int init_module(void)
{
init_waitqueue_head(&queue);
kthread_wake = kthread_create(kthread_wake_func, NULL, "wake");
kthread_sleep1 = kthread_create(kthread_sleep_func_1, NULL, "sleep1");
kthread_sleep2 = kthread_create(kthread_sleep_func_2, NULL, "sleep2");
wake_up_process(kthread_wake);
wake_up_process(kthread_sleep1);
wake_up_process(kthread_sleep2);
return 0;
}
void cleanup_module(void)
{
kthread_stop(kthread_sleep2);
kthread_stop(kthread_sleep1);
kthread_stop(kthread_wake);
}
MODULE_LICENSE("GPL");

21
kernel_modules/warn_on.c Normal file
View File

@@ -0,0 +1,21 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#warn_on */
#include <linux/module.h>
#include <linux/kernel.h>
static int myinit(void)
{
pr_info("warn_on init\n");
WARN_ON("warn_on do it");
pr_info("warn_on after\n");
return 0;
}
static void myexit(void)
{
pr_info("warn_on cleanup\n");
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

View File

@@ -0,0 +1,40 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#workqueue-from-workqueue */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/workqueue.h>
static int i;
static struct workqueue_struct *queue;
static void work_func(struct work_struct *work);
DECLARE_DELAYED_WORK(next_work, work_func);
DECLARE_WORK(work, work_func);
static void work_func(struct work_struct *work)
{
pr_info("%d\n", i);
i++;
if (i == 10)
i = 0;
queue_delayed_work(queue, &next_work, HZ);
}
static int myinit(void)
{
queue = create_workqueue("myworkqueue");
queue_work(queue, &work);
return 0;
}
static void myexit(void)
{
cancel_delayed_work(&next_work);
flush_workqueue(queue);
destroy_workqueue(queue);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");

View File

@@ -0,0 +1,41 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#workqueues */
#include <linux/delay.h> /* usleep_range */
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/types.h> /* atomic_t */
#include <linux/workqueue.h>
static struct workqueue_struct *queue;
static atomic_t run = ATOMIC_INIT(1);
static void work_func(struct work_struct *work)
{
int i = 0;
while (atomic_read(&run)) {
pr_info("%d\n", i);
usleep_range(1000000, 1000001);
i++;
if (i == 10)
i = 0;
}
}
DECLARE_WORK(work, work_func);
static int myinit(void)
{
queue = create_workqueue("myworkqueue");
queue_work(queue, &work);
return 0;
}
static void myexit(void)
{
atomic_set(&run, 0);
destroy_workqueue(queue);
}
module_init(myinit)
module_exit(myexit)
MODULE_LICENSE("GPL");