kernel modules

  1. Linux kernel Licence

  2. GPLv2

  3. kernel subsystem

  4. Process Managerment

  5. Memory Managermanet

  6. Filesystems

  7. Device control

  8. Networking

  9. Classes of device and modules

  10. char module 1. Character Device

    1. /dev/console, /dev/ttyS0
    2. systemcall: open, close, read, write
    3. diff with regular file
    4. you can move back and forth in the regular file 1. you can only access sequentially in char device
  11. block module 1. Block Device

    1. disk
    2. handle I/O operations
  12. network module 1. Network interfaces

    1. NIC, loopback
  13. Basic module

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("GPL v2");

static int hello_init(void)
{
	printk(KERN_ALERT "Hello, world\n");
	return 0;
}

static void hello_exit(void)
{
	printk(KERN_ALERT "Goodby\n");
}

module_init(hello_init);
module_exit(hello_exit);
1
2
3
4
5
6
$ cat Makefile
obj-m += hello.o
all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

If you have multi file, then would be

1
2
obj-m := module.o
module-objs := file1.o file2.o
  1. parameters
1
2
3
4
5
6
7
#include <linux/moduleparam.h>
module_param(variable, type, perm);

static char *whom = "world";
static int howmany = 1;
module_param(howmany, int, S_IRUGO);
module_param(whom, charp, S_IRUGO);
  1. lsmod

  2. read /proc/modules

  3. info about modules /sys/modules

  4. kernel symbol tables

  5. export by: EXPORT_SYMBOL(“name”); EPORT_SYSBOL_GPL(“name”);

  6. /proc/kallsyms

Char device

  1. dev_t type

  2. defined in <linux/types.h>

  3. 12 bits set aside for the major number and 20 for the minor number.

  4. MAJOR(dev_t dev); / MINOR(dev_t dev); / MKDEV(int major, int minor);

  5. register_chrdev_region

  6. <linux/fs.h>

  7. know which major numbers 1. int register_chrdev_region(dev_t first, unsigned int count, char *name);

  8. Do not know which major numbers 1. int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count, char *name);

  9. void unregister_chrdev_region(dev_t first, unsigned int count);

  10. use alloc_chrdev_region instead of register_chrdev_region

  11. Data Structures

  12. sturct file_operations

  13. struct file 1. <linux/fs.h>

  14. struct inode 1. unsigned int iminor(struct inode *inode); 1. unsigned int imajor(struct inode *inode);

  15. Char Device Registration

  16. <linux/cdev.h>

  17. void cdev_init(struct cdev *cdev, struct file_operations *fops);

  18. int cdev_add(struct cdev *dev, dev_t num, unsigned int count);

  19. void cdev_del(struct cdev *dev);

  20. Memory Usage

  21. <linux/slab.h>

  22. void *kmalloc(size_t size, int flags); (ref: vmalloc) 1. vmalloc: allocates a contiguous memory region in the virtual address space. returns a pointer to a linear memory area of size at least size .

  23. void kfree(void *ptr);

  24. read/write

  25. ssize_t read(struct file *filp, char __user *buff, size_t count, loff_t *offp);

  26. ssize_t write(struct file *filp, const char __user *buff, size_t count, loff_t *offp);

  27. <asm/uaccess.h> 1. unsigned long copy_to_user(void __user *to, const void *from, unsigned long count); 1. unsigned long copy_from_user(void *to, const void __user *from, unsigned long count);

  28. Lock, mutual exclusion

  29. Semaphore 1. mutex 1. read/write semaphore

  30. Spinlocks 1. used in code that can not sleep, such as interrupt handlers. 1. Reader/Writer Spinlocks

  31. seqlocks

  32. Read-Copy-Update(RCU)

Memory

  1. Memory Zone
  2. DMA-capable memory, first 16MB of RAM
  3. normal memory, user space? 0-3G
  4. low memory, kernel physic address, 0-896M, other 128M are left for I/O dev
  5. high memory, physic memory that kernel cant mapped directly

Network Driver

  1. struct net_device
  2. <linux/netdevice.h>
  3. struct net_device *alloc_netdev(int sizeof_priv, const char *name, void (*setup)(struct net_device *));
  4. struct net_device *alloc_etherdev(int sizeof_priv); 1. <linux/etherdevice.h>
  5. register_netdev() / unregister_netdev() / free_netdev()
  6. void netif_start_queue(struct net_device *dev);
  7. void netif_stop_queue(struct net_device *dev);
  8. hard_start_transmit()
  9. ether_setup() 1. driver/net/net_init.c