☰
×
◈ Introductions
◈ Linux Kernel Compilation
◈ Linux Kernel Debugging
◈ Linux Kernel APIs - I
◈ Linux Kernel APIS - II
◈ Linux Kernel APIS - III
◈ KGDB Setup
◈ QEMU Setup
◈ Linux System Calls Table
◈ Useful Linux Commands
Home
About
Consulting
Training
Tutorials
Interview Questions
Contact
Tutorials
Linux Kernel APIs
Home
Linux Kernel APIs
◈ Introductions
◈ Linux Kernel Compilation
◈ Linux Kernel Debugging
◈ Linux Kernel APIs - I
◈ Linux Kernel APIS - II
◈ Linux Kernel APIS - III
◈ KGDB Setup
◈ QEMU Setup
◈ Linux System Calls Table
◈ Useful Linux Commands
Tutorials - Linux Kernel Function
Kernel APIs - I
« Prev
Next »
Linux Kernel APIs
☛ Compiling a Loadable Kernel Module
☛ Module Parameters
☛ Useful Macros
☛ The printk() function
☛ Character Device Driver
1. Compiling a Loadable Kernel Module : Create an executable shell-script file, e.g mqc (module quick compiler) editor. content as follows : #!/bin/bash echo "obj-m := " $1.o > Makefile make -C /usr/src/linux-3.12.1 M=`pwd` rm -f $1.mod.* rm -f $1.o rm -rf .tmp_versions* OR You can use Makefile obj-m := prog1.o KDIR := /lib/modules/$(shell uname -r)/build all: $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules clean: $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean In case more than 1 source file is involved, e.g file1.c, file2.c then use : obj-m := my_module.o my_module-objs := file1.o file2.o 2. Module Parameters : #include
#include
module_param(variable, type, perm); module_param_array(variable,type,&num,perm); /* For array values */ type = bool, invbool, charp, int, long, short, uint, ulong, ushort If perm=0, then there is no sysfs entry at all, else it appears under /sys/module S_IRUGO for a read-only parameter, but cannot be changed. S_IRUGO | S_IWUSR allows root to change the parameter. Note: if a parameter is changed by sysfs, value as seen by Module changes, but module is NOT notified in any way. 3. Useful Macros : Conventionally put at the end of the file. MODULE_AUTHOR(author); MODULE_DESCRIPTION(description); MODULE_VERSION(version_string); MODULE_DEVICE_TABLE(table_info); MODULE_LICENSE(“GPL”); EXPORT_SYMBOL(symbol); /* Limits exports to GPL versioned modules */ EXPORT_SYMBOL_GPL(symbol); /* exports without versioning info. */ EXPORT_SYMBOL_NOVERS(symbol); 4. The printk() function #include
int printk(const char *fmt, ...); int pr_info(const char *fmt, ...); int pr_alter(const char *fmt, ...); etc .. 5. Character Device Drivers #include
int register_chrdev(unsigned int major, const char *name, struct file_operations *fops); void unregister_chrdev(unsigned int major, const char *name); For static allocation, major > 0 and return 0 for success For Dynamic allocation, major = 0 and return >0 for success In both cases < 0 indicates an error. int alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *); int register_chrdev_region(dev_t, unsigned, const char *); int __register_chrdev(unsigned int major, unsigned int baseminor, unsigned int count, const char *name, const struct file_operations *fops); void __unregister_chrdev(unsigned int major, unsigned int baseminor, unsigned int count, const char *name); void unregister_chrdev_region(dev_t, unsigned); void chrdev_show(struct seq_file *,off_t); static inline int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops) { return __register_chrdev(major, 0, 256, name, fops); } static inline void unregister_chrdev(unsigned int major, const char *name) { __unregister_chrdev(major, 0, 256, name); } #include
struct file_operations; struct inode; struct module; struct cdev { struct kobject kobj; struct module *owner; const struct file_operations *ops; struct list_head list; dev_t dev; unsigned int count; }; void cdev_init(struct cdev *, const struct file_operations *); struct cdev *cdev_alloc(void); void cdev_put(struct cdev *p); int cdev_add(struct cdev *, dev_t, unsigned); void cdev_del(struct cdev *); void cd_forget(struct inode *); Important structures: struct file_operations; struct file; struct inode; mknod /dev/
c
#include
unsigned long copy_from_user(void *to, const void *from, unsigned long count); unsigned long copy_to_user(void *to, const void *from, unsigned long count); In both the above cases, the return value is the number of bytes NOT transferred. #include
err = get_user ( x, addr ); the kernel variable x gets the value of the thing pointed to by addr (in user space err = put_user ( x, addr ); put_user, the value of the variable x is written starting at the address addr. Note well that x is a variable bytes_left = copy_from_user(void*to, const void *from, unsigned long n ); bytes_left = copy_to_user(void*to, const void *from, unsigned long n ); None of these need the old verify_area() call, as they do all the area verification on their own using the paging unit in the CPU hardware. Less chance of missing address verification that way. Also, the new address verification is much faster than the old scheme was. The User Space Memory Access API Function Description access_ok - Checks the validity of the user space memory pointer get_user - Gets a simple variable from user space put_user - Puts a simple variable to user space clear_user - Clears, or zeros, a block in user space copy_to_user - Copies a block of data from the kernel to user space copy_from_user - Copies a block of data from user space to the kernel strnlen_user - Gets the size of a string buffer in user space strncpy_from_user- Copies a string from user space into the kernel #include
struct resource *request_region(unsigned long first,unsigned long n, const char *name); void release_region(unsigned long start, unsigned long n); #include
unsigned inb(unsigned port); unsigned inw(unsigned port); unsigned inl(unsigned port); void outb(unsigned char byte, unsigned port); void outw(unsigned short word, unsigned port); void outl(unsigned long dword, unsigned port); drivers/base/core.c struct device *device_create(struct class *class, struct device *parent, dev_t devt, void *drvdata, const char *fmt, ...) void device_destroy(struct class *class, dev_t devt)
« Prev
Next »
Helpful Links
Interview Questions
Corporate Training
Recommended Books
Linux Consulting
Apply for Job Assistance
If you have any queries please email us at
info@motionzen.com