#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/version.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/mutex.h>
#include <linux/stat.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/ioctl.h>
#include <linux/sched.h>
/*
* xwrt_demo.c — A small, readable Linux kernel module generated by XWRT.
*
* Generated by XWRT (https://xwrt.dev/tools/kernel-module).
* Target: Linux kernel modules. Review and test in a VM before use.
*/
static bool debug = true;
module_param(debug, bool, 0644);
MODULE_PARM_DESC(debug, "Enable verbose logging");
#define XWRT_LOG(fmt, ...) do { if (debug) pr_info(fmt, ##__VA_ARGS__); } while (0)
#define BUF_SIZE 4096
static char *shared_buffer;
static size_t shared_size;
static DEFINE_MUTEX(shared_lock);
static ssize_t shared_read(struct file *file, char __user *user_buf, size_t count, loff_t *ppos)
{
ssize_t ret;
size_t available;
mutex_lock(&shared_lock);
if (*ppos < 0 || (size_t)*ppos >= shared_size) {
ret = 0;
goto out;
}
available = shared_size - (size_t)*ppos;
if (count > available) count = available;
if (copy_to_user(user_buf, shared_buffer + *ppos, count)) {
ret = -EFAULT;
goto out;
}
*ppos += count;
ret = (ssize_t)count;
out:
mutex_unlock(&shared_lock);
return ret;
}
static ssize_t shared_write(struct file *file, const char __user *user_buf, size_t count, loff_t *ppos)
{
if (count > BUF_SIZE) count = BUF_SIZE;
mutex_lock(&shared_lock);
if (copy_from_user(shared_buffer, user_buf, count)) {
mutex_unlock(&shared_lock);
return -EFAULT;
}
shared_size = count;
mutex_unlock(&shared_lock);
*ppos = 0;
return (ssize_t)count;
}
#define DEVICE_NAME "xwrtdemo"
#define CLASS_NAME "xwrtdemo"
#define XWRT_DEMO_IOC_MAGIC 0xE1
#define XWRT_DEMO_IOCRESET _IO(XWRT_DEMO_IOC_MAGIC, 0)
#define XWRT_DEMO_IOCGVERSION _IOR(XWRT_DEMO_IOC_MAGIC, 1, int)
#define XWRT_DEMO_IOCGSIZE _IOR(XWRT_DEMO_IOC_MAGIC, 2, int)
static dev_t xwrt_devno;
static struct cdev xwrt_cdev;
static struct class *xwrt_class;
static struct device *xwrt_device;
static int xwrt_open(struct inode *inode, struct file *file)
{
XWRT_LOG("opened by pid %d\n", current->pid);
return 0;
}
static int xwrt_release(struct inode *inode, struct file *file)
{
XWRT_LOG("released\n");
return 0;
}
static long xwrt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int ret = 0;
switch (cmd) {
case XWRT_DEMO_IOCRESET:
mutex_lock(&shared_lock);
shared_size = 0;
mutex_unlock(&shared_lock);
break;
case XWRT_DEMO_IOCGVERSION: {
int version = 1;
if (copy_to_user((int __user *)arg, &version, sizeof(version))) ret = -EFAULT;
break;
}
case XWRT_DEMO_IOCGSIZE: {
int size;
mutex_lock(&shared_lock);
size = (int)shared_size;
mutex_unlock(&shared_lock);
if (copy_to_user((int __user *)arg, &size, sizeof(size))) ret = -EFAULT;
break;
}
default:
ret = -ENOTTY;
}
return ret;
}
static const struct file_operations xwrt_fops = {
.owner = THIS_MODULE,
.open = xwrt_open,
.release = xwrt_release,
.read = shared_read,
.write = shared_write,
.unlocked_ioctl = xwrt_ioctl,
.llseek = default_llseek,
};
static int __init xwrt_init(void)
{
int ret;
XWRT_LOG("initializing xwrt_demo\n");
shared_buffer = kzalloc(BUF_SIZE, GFP_KERNEL);
if (!shared_buffer) return -ENOMEM;
ret = alloc_chrdev_region(&xwrt_devno, 0, 1, DEVICE_NAME);
if (ret) goto err_buffer;
cdev_init(&xwrt_cdev, &xwrt_fops);
xwrt_cdev.owner = THIS_MODULE;
ret = cdev_add(&xwrt_cdev, xwrt_devno, 1);
if (ret) goto err_region;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
xwrt_class = class_create(CLASS_NAME);
#else
xwrt_class = class_create(THIS_MODULE, CLASS_NAME);
#endif
if (IS_ERR(xwrt_class)) {
ret = PTR_ERR(xwrt_class);
goto err_cdev;
}
xwrt_device = device_create(xwrt_class, NULL, xwrt_devno, NULL, DEVICE_NAME);
if (IS_ERR(xwrt_device)) {
ret = PTR_ERR(xwrt_device);
goto err_class;
}
XWRT_LOG("loaded major=%d minor=%d\n", MAJOR(xwrt_devno), MINOR(xwrt_devno));
return 0;
err_device:
device_destroy(xwrt_class, xwrt_devno);
err_class:
class_destroy(xwrt_class);
err_cdev:
cdev_del(&xwrt_cdev);
err_region:
unregister_chrdev_region(xwrt_devno, 1);
err_buffer:
kfree(shared_buffer);
return ret;
}
static void __exit xwrt_exit(void)
{
device_destroy(xwrt_class, xwrt_devno);
class_destroy(xwrt_class);
cdev_del(&xwrt_cdev);
unregister_chrdev_region(xwrt_devno, 1);
kfree(shared_buffer);
XWRT_LOG("unloaded\n");
}
module_init(xwrt_init);
module_exit(xwrt_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("XWRT");
MODULE_DESCRIPTION("A small, readable Linux kernel module generated by XWRT.");
MODULE_VERSION("1.0.0");