diff options
Diffstat (limited to 'md/notes')
-rw-r--r-- | md/notes/kernel/create_procfs_entry.md | 144 | ||||
-rw-r--r-- | md/notes/kernel/create_thread.md | 45 | ||||
-rw-r--r-- | md/notes/kernel/topics.md | 22 | ||||
-rw-r--r-- | md/notes/kernel/usermode_helper.md | 8 |
4 files changed, 219 insertions, 0 deletions
diff --git a/md/notes/kernel/create_procfs_entry.md b/md/notes/kernel/create_procfs_entry.md new file mode 100644 index 0000000..03d186f --- /dev/null +++ b/md/notes/kernel/create_procfs_entry.md @@ -0,0 +1,144 @@ +title:Create procfs entry +keywords:kernel,linux,threads + +# Create procfs entry + +Lets extend hello world driver. + + +__Makefile__ +```Makefile +obj-m += procfs_entry.o + +KDIR ?= /lib/modules/$(shell uname -r)/build + +all: + make -C $(KDIR) M=$(PWD) modules + +clean: + make -C $(KDIR) M=$(PWD) clean +``` + +__procfs_entry.c__ +```c +#include <linux/module.h> /* Needed by all modules */ +#include <linux/kernel.h> + +int procfs_entry_init( void ) +{ + printk(KERN_DEBUG "Hello Procfs!\n"); + return 0; +} + +void procfs_entry_exit( void ) +{ + printk(KERN_DEBUG "Exit Procfs!\n"); +} + +module_init( procfs_entry_init ); +module_exit( procfs_entry_exit ); + +MODULE_LICENSE("GPL"); +``` + + +Create proc directory with + +```c +dir = proc_mkdir(DIR_NAME, PARENT_DIR); +``` + +under the directory create file that will contain data + +```c +proc_create("file", MODE, PARENT_DIR, PROC_FOPS) +``` + +Lets create /proc/secret/file. And define basic read/write operations. That at this +stage will do nothing. + +```c +#include <linux/proc_fs.h> + +static struct proc_dir_entry *procdir, *procfile; + +static int secret_open(struct inode *inode, struct file *file) { + return 0; +} + +static int secret_release(struct inode *inode, struct file *file) { + return 0; +} + +static ssize_t secret_read(struct file *file, char __user *buffer, size_t length, loff_t *offset) { + return 0; +} + +static ssize_t secret_write(struct file *file, const char *buffer, size_t length, loff_t *offset) { + return 0; +} + +static struct proc_ops procfs_ops = { + .proc_open = secret_open, + .proc_read = secret_read, + .proc_write = secret_write, + .proc_release = secret_release +}; + + +int procfs_entry_init( void ) { +... +procdir = proc_mkdir("secret", NULL); +if (procdir == NULL) { + pr_err("Cant create secret directory\n"); + return -1; +} + +procfile = proc_create("file", 0644, procdir, &procfs_ops); +if (procfile == NULL) { + pr_err("Cant create file under the secret\n"); + return -1; +} +... +} + +void procfs_entry_exit( void ) +{ + ... + //remove entries after unloading module + proc_remove(procdir); + proc_remove(procfile); + ... +} +``` + +Next step add data to read from file entry + + + + +## List of procfs functions used + +| Function name | Note | +|---|---| +| proc_mkdir | Create procfs directory | +| proc_create | Create procfs file | +| proc_remove | Remove procfs entry | + +## Final result + +__Makefile__ +```Makefile +``` + +__procfs_entry.c__ +```c +``` + +## Links + +https://elixir.bootlin.com/linux/v6.1.21/source/include/linux/proc_fs.h + + + + diff --git a/md/notes/kernel/create_thread.md b/md/notes/kernel/create_thread.md new file mode 100644 index 0000000..87830b1 --- /dev/null +++ b/md/notes/kernel/create_thread.md @@ -0,0 +1,45 @@ +title:Create threads +keywords:kernel,linux,threads + +# Create threads + +State of kernel thread - include/linux/sched.h + +```c +/* Used in tsk->state: */ +#define TASK_RUNNING 0x00000000 +#define TASK_INTERRUPTIBLE 0x00000001 +#define TASK_UNINTERRUPTIBLE 0x00000002 +#define __TASK_STOPPED 0x00000004 +#define __TASK_TRACED 0x00000008 +/* Used in tsk->exit_state: */ +#define EXIT_DEAD 0x00000010 +#define EXIT_ZOMBIE 0x00000020 +#define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD) +/* Used in tsk->state again: */ +#define TASK_PARKED 0x00000040 +#define TASK_DEAD 0x00000080 +#define TASK_WAKEKILL 0x00000100 +#define TASK_WAKING 0x00000200 +#define TASK_NOLOAD 0x00000400 +#define TASK_NEW 0x00000800 +#define TASK_RTLOCK_WAIT 0x00001000 +#define TASK_FREEZABLE 0x00002000 +#define __TASK_FREEZABLE_UNSAFE (0x00004000 * IS_ENABLED(CONFIG_LOCKDEP)) +#define TASK_FROZEN 0x00008000 +#define TASK_STATE_MAX 0x00010000 + +#define TASK_ANY (TASK_STATE_MAX-1) +``` + +## Links + + +https://www.linuxtoday.com/blog/kernel-threads/ +http://www.embeddedlinux.org.cn/EssentialLinuxDeviceDrivers/final/ch03lev1sec1.html +https://elixir.bootlin.com/linux/v6.1.21/source/include/linux/sched.h +https://elixir.bootlin.com/linux/v6.1.21/source/kernel/sysctl.c + + + + diff --git a/md/notes/kernel/topics.md b/md/notes/kernel/topics.md new file mode 100644 index 0000000..b959bad --- /dev/null +++ b/md/notes/kernel/topics.md @@ -0,0 +1,22 @@ +title: Topics +keywords:blog,projects + +# + +Linux kernel programming topics + +## Topic + +[Compile kernel](/writeup/compile_linux_kernel.md) +[Hello world driver](/writeup/kernel_hello_world.md) +<!-- DMA programming --> +<!--[Create thread](/notes/kernel/create_thread.md) --> +<!-- Sysfs --> +[Procfs](/notes/kernel/create_procfs_entry.md) +<!-- Ramfs --> +<!-- Systemcalls --> +<!-- Write files to user space --> +<!-- Read files from user space --> +<!-- Netlink --> +<!--[User mode helpers](/notes/kernel/usermode_helper.md) --> +[Kernel /dev/hwrng](/writeup/kernel_dev_hwrng.md) diff --git a/md/notes/kernel/usermode_helper.md b/md/notes/kernel/usermode_helper.md new file mode 100644 index 0000000..dd8155a --- /dev/null +++ b/md/notes/kernel/usermode_helper.md @@ -0,0 +1,8 @@ +title:User mode helper +keywords:kernel,linux,threads + +# User mode helper + + +## Links + |