Kernel Modules. Kernel Module Pieces of code that can be loaded and unloaded into the kernel upon demand. Compiled as an independent program With appropriate.

Slides:



Advertisements
Similar presentations
DEVICE DRIVER VINOD KAMATH CS691X PROJECT WORK. Introduction How to write/install device drivers Systems, Kernel Programming Character, Block and Network.
Advertisements

Linux device-driver issues
Device Drivers. Linux Device Drivers Linux supports three types of hardware device: character, block and network –character devices: R/W without buffering.
RT_FIFO, Device driver.
Lecture for Lab 3, Exp1 of EE505 (Developing Device Driver) T.A. Chulmin Kim CoreLab. Mar, 11, 2011 [XenSchedulerPaper_Hotcloud-commits] r21 - /
Computer System Laboratory
Building and Running Modules Sarah Diesburg COP 5641.
Building and Running Modules Linux Kernel Programming CIS 4930/COP 5641.
Building and Running Modules Ted Baker  Andy Wang CIS 4930 / COP 5641.
52 Advanced Operating Systems Writing Device Drivers.
Embedded Systems Programming Writing Device Drivers.
63 UQC152H3 Advanced OS Writing a Device Driver. 64 The SCULL Device Driver Simple Character Utility for Loading Localities 6 devices types –Scull-03.
How to make a pseudo-file As a follow-up to our first lab, we examine the steps needed to create our own ‘/proc’ file.
Embedded System Programming Introduction to Device Drivers.
CS 635 Advanced Systems Programming Spring 2005 Professor Allan B. Cruse University of San Francisco.
CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.
1Lecture 5 Lecture 5: Linux for Hardware Developers ECE 412: Microcomputer Laboratory.
Tutorial and Demos on Linux Virtual Machine
1 Netfilter in Linux Bing Qi Department of Computer Science and Engineering Auburn university.
Device Drivers In Linux © Gregory Kesden Fall 2000.
EECS 498 Advanced Embedded Systems Lecture 4: Linux device drivers and loadable kernel modules.
M. Muztaba Fuad Advanced Operating System Project Device Drivers.
Loadable Kernel Modules Dzintars Lepešs The University of Latvia.
1 Week 6 Intro to Kernel Modules, Project 2 Sarah Diesburg Florida State University.
Kernel module programming Nezer J. Zaidenberg. reference This guide is built on top of The Linux Kernel Module Programming Guide The guide is available.
Operating System Program 5 I/O System DMA Device Driver.
For OS Experiments. What Do We Need? A Computer &
Real-time Systems Lab, Computer Science and Engineering, ASU Linux Modules and Device Drivers (ESP – Fall 2014) Computer Science & Engineering Department.
Sogang University Advanced Operating Systems (Linux Module Programming) Sang Gue Oh, Ph.D.
File System Review bottomupcs.com J. Kubiatowicz, UC Berkeley.
Lecture 2: Linux devices Roei Ben-Harush Agenda 1 1 Linux Devices About next Assignment Roei Ben-Harush 2015 Reminder.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 8: Opening Files and Starting Processes.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Memory Addressing / Kernel Modules.
UNIX Files File organization and a few primitives.
1 uClinux course Day 5 of 5 - the debugger (gdb) and uClinux device drivers.
Implementation of Embedded OS Lab3 Linux Kernel Modules.
Kernel module programming Nezer J. Zaidenberg. reference This guide is built on top of The Linux Kernel Module Programming Guide The guide is available.
Files & File system. A Possible File System Layout Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All rights reserved
K ERNEL D EVELOPMENT CSC585 Class Project Dawn Nelson December 2009.
LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate.
Linux Device Driver 2009/04/08. Reference Book Another Reference Book Embedded Linux Primer: A Practical, Real-World Approach By Christopher Hallinan.
1 Week 7 System Calls, Kernel Threads, Kernel Debugging Sarah Diesburg Florida State University.
Interfacing Device Drivers with the Kernel
CSC414 “Introduction to UNIX/ Linux” Lecture 2. Schedule 1. Introduction to Unix/ Linux 2. Kernel Structure and Device Drivers. 3. System and Storage.
Lab 12 Department of Computer Science and Information Engineering National Taiwan University Lab12 – Driver 2014/12/16 1 /21.
COMP 3438 – Part I - Lecture 5 Character Device Drivers
1 Intro to Kernel Modules and /proc Sarah Diesburg CS 3430 Operating Systems.
Finish up OS topics Group plans. Today Finish up and review Linux device driver stuff – Walk example again – See how it all goes together – Discuss talking.
Lecture 3 Module Programming and Device Driver (Homework#1 included) Kyu Ho Park Sept. 15, 2015.
File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive.
Embedded Linux Kernel Porting & Device Driver - 유柳 명明 환桓유柳 명明 환桓 [  ]
Virtual Memory Mohammad H. Mofrad February 23, 2016
Files in UNIX u UNIX deals with two different classes of files:  Special Files  Regular Files u Regular files are just ordinary data files on disk -
 LED 를 직접 제어하는 디바이스 드라이버를 작성해 보자  Reminder: LED 는 SPI 를 통해 ARM7 과 연결된다. ◦ 그렇다면 고쳐야 하는 코드는 어디에 ?  linux-2.6.x/arch/arm/mach-nds/arm7 ◦ Hardware spec.
OS – Ex 1 Nezer J. Zaidenberg.
Device Driver_Skeleton
Lecture 3 Module Programming and Device Driver (Homework#1 included)
Linux Kernel Module Programming
Computer System Laboratory
Linux Kernel Driver.
Intro to Kernel Modules and /proc
chapter 2 - Hello World Model
chapter 3-Char Device Driver
CS 6560 Operating System Design
Lab 4 Kernel Module Operating System Lab.
CS 6560 Operating System Design Kernel Loadable Modules
Implementation of Embedded OS
Computer System Laboratory
Loadable Kernel Modules
Presentation transcript:

Kernel Modules

Kernel Module Pieces of code that can be loaded and unloaded into the kernel upon demand. Compiled as an independent program With appropriate flags to indicate that it is kernel code. Can be linked into the kernel by installation. –Static/Dynamic loading –“/proc/modules” or “lsmod” Enhance the functionality of the kernel. Example: –Device Drivers

Functions in Kernel Module int init_module(void) –Invoked when the module is inserted to kernel –Return 0 if success. void cleanup_module(void) –Invoked when the module is removed from kernel printk() –/usr/src/linux /include/linux/kernel.h

#include int init_module(void) { printk(KERN_ALERT "Hello world.\n“); return 0; } void cleanup_module(void) { printk(KERN_ALERT "Goodbye world.\n"); } MODULE_LICENSE("GPL"); // GNU Public License v2 or later

Makefile ifneq ($(KERNELRELEASE),) obj-m += hello.o else KDIR:= /lib/modules/$(shell uname -r)/build PWD:= $(shell pwd) default: $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules endif

insmod hello.ko lsmod or “/proc/modules” tail -f /var/log/messages rmmod hello

Modules vs. Programs Programs –main() –performs a single task from beginning to end –Run in user mode –Can call the libc functions Modules –init_module & cleanup_module –registers itself in order to serve future requests –Run in kernel mode Part of kernel program –Only can call the ones exported by the kernel. include/linux include/asm Export all non-static symbols

Device Drivers Character devices –one that can be accessed as a stream of bytes –Keyboard driver Block devices –One that can be accessed as multiples of a block, usually 1KB per block –Disk driver

Device Drivers To communicate with hardware –Each piece of hardware is represented by a file located in /dev –An application do some operations on that file –The kernel finds corresponding device driver for that file. –Each operation on that file will trigger a function in the device driver for that hardware –The file in /dev is not regular file To find the device driver for that file –ls -l /dev/b* –Major number Match with driver –Minor number used by the driver to distinguish between the various hardware it controls ls -l /dev/fd0u*

Device Drivers Defines functions on the file operation –Read, write, open, close, etc Register the device to kernel creates a file in “/dev” using “mknod”

Device Drivers Function mapping struct file_operations fops = {.read = device_read,.write = device_write,.open = device_open,.release = device_release }; vi /usr/src/linux /include/linux/fs.h int register_chrdev(unsigned int major, const char *name, struct file_operations *fops); int unregister_chrdev(unsigned int major, const char *name);

#include #define DEVICE_NAME "chardev" #define BUF_LEN 80 static int Major; static char msg[BUF_LEN]; static char *msg_Ptr;

static int device_open(struct inode *inode, struct file *file) { static int counter = 0; sprintf(msg, "This device is opened %d times!\n", counter++); msg_Ptr = msg; try_module_get(THIS_MODULE); return 0; } static int device_release(struct inode *inode, struct file *file) { module_put(THIS_MODULE); return 0; }

We cannot remove the device driver while device file is opened by a process. There's a counter which keeps track of how many processes are using your module –the 3 rd column in lsmod try_module_get(THIS_MODULE); –Increment the use count module_put(THIS_MODULE); –Decrement the use count

static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t * offset) { int bytes_read = 0; if (*msg_Ptr == 0) return 0; while (length && *msg_Ptr) { put_user(*(msg_Ptr++), buffer++); length--; bytes_read++; } return bytes_read; }

put_user(char src, char* dst); –src is in kernel code segment –dst is in user code segment get_user(char dst, char* src); –src is in user code segment –dst is in kernel code segment A pointer into userspace should never be simply dereferenced #include buffer is in user data segment length: length of the buffer

static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t * off) { printk(" This operation isn't supported.\n"); return -EINVAL; }

static struct file_operations fops = {.read = device_read,.write = device_write,.open = device_open,.release = device_release };

int init_module(void) { Major = register_chrdev(0, DEVICE_NAME, &fops); if (Major < 0) { printk("Failed to register char device.\n"); return Major; } printk(KERN_ALERT "chardev is assigned to major number %d.\n", Major); return 0; } void cleanup_module(void) { int ret = unregister_chrdev(Major, DEVICE_NAME); if (ret < 0) printk("Error in unregister_chrdev: %d\n", ret); }

mknod Create a file in /dev And match the file with a device driver mknod /dev/hello c Major 0 –This Major number is from the output of /var/log/messages

References Linux Device Drivers, 2nd Edition – The Linux Kernel Module Programming Guide –