Linux Kernel Module Programming

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

DEVICE DRIVER VINOD KAMATH CS691X PROJECT WORK. Introduction How to write/install device drivers Systems, Kernel Programming Character, Block and Network.
Linux device-driver issues
Computer System Laboratory
Sort the given string, without using string handling functions.
Building and Running Modules Sarah Diesburg COP 5641.
Building and Running Modules Linux Kernel Programming CIS 4930/COP 5641.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
C Module System C and Data Structures Baojian Hua
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
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.
More Miscellaneous Topics CS-2301 B-term More Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Add a New System Call to Linux. Hw1 Add a New System Call to Linux and Compile Kernel Add a New System Call to Linux by Kernel Module.
Chapter 11-12, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3.
Guide To UNIX Using Linux Third Edition
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Loadable Kernel Modules Dzintars Lepešs The University of Latvia.
1 uClinux course Day 3 of 5 The uclinux toolchain, elf format and ripping a “hello world”
Computer Science 210 Computer Organization Introduction to C.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
Sogang University Advanced Operating Systems (Linux Module Programming) Sang Gue Oh, Ph.D.
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.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Memory Addressing / Kernel Modules.
Implementation of Embedded OS Lab3 Linux Kernel Modules.
PHYS 2020 Basic C An introduction to writing simple but useful programs in C In these lectures I will take you through the basics of C, but you will need.
Lecture 1 cis208 January 14 rd, Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld.
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.
DOCUMENTATION SECTION GLOBAL DECLARATION SECTION
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
COMP 3438 – Part I - Lecture 5 Character Device Drivers
Templates Where the TYPE is generic. Templates for functions Used when the you want to perform the same operation on different data types. The definition.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
1 Intro to Kernel Modules and /proc Sarah Diesburg CS 3430 Operating Systems.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
Add a New System Call to Linux
OS – Ex 1 Nezer J. Zaidenberg.
‘C’ Programming Structures and Commands
Computer Science 210 Computer Organization
C Interview Questions Prepared By:.
Jimit Mahadevia Nishit Shah This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported.
C Programming Hardik H. Maheta.
Computer System Laboratory
Functions Separate Compilation
Want to play a game? – Linux Kernel Modules
C programming language
Pointers.
C Basics.
Computer Science 210 Computer Organization
C Stuff CS 2308.
An Introduction to Java – Part I, language basics
Intro to Kernel Modules and /proc
chapter 2 - Hello World Model
Introduction to C Topics Compilation Using the gcc Compiler
CS 6560 Operating System Design
Lab 4 Kernel Module Operating System Lab.
CS 6560 Operating System Design Kernel Loadable Modules
Implementation of Embedded OS
Introduction to C Topics Compilation Using the gcc Compiler
Let’s start from the beginning
Loadable Kernel Modules
Strings …again.
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
CPS125.
SPL – PS1 Introduction to C++.
Files Chapter 8.
Presentation transcript:

Linux Kernel Module Programming Jason Barto September 4, 2003 Jason Barto

Agenda Introduction Review Standard Functions Write Module v0.1 Compiling Modules Handling Runtime Parameters More Kernel Macros Write Module v0.2 Close and Further Readings Jason Barto

Introduction Similar to Java Servlets with predefined structure and function names Common C functions are not available to modules (such as printf, scanf, strcat, or system) Instead you must use kernel defined functions (/proc/ksyms) Jason Barto

Review Standard Functions printk () 8 Priority Levels: KERN_WARNING, KERN_ALERT Init function sets up variables, tells the kernel what the module provides and registers any necessary functions Cleanup function undoes changes made by init module_init and module_exit Tell kernel which functions are to be used at init and cleanup Jason Barto

Module v0.1 /* module-0.1.c - The simplest kernel module. */ #include <linux/module.h> /* Needed by all modules */ #include <linux/kernel.h> /* Needed for KERN_ALERT */ #include <linux/init.h> /* Needed for macros */ int module_v0_1_init(void) { printk(KERN_ALERT "Initing module v0.1\n"); /* A non 0 return means init_module failed; module can't be loaded. */ return 0; } void module_v0_1_stop(void) { printk(KERN_ALERT “Unloading module v0.1\n"); module_init (module_v0_1_init); module_exit (module_v0_1_stop); Jason Barto

Compiling Modules Kernel modules are object files thus the ‘-c’ flag must be used with gcc ‘-O2’ causes the compiler to use optimization and thus translate kernel macros ‘-isystem /lib/modules/`uname -r`/build/include’ will tell gcc to include the proper kernel header files Jason Barto

Compiling Modules cont.. ‘-D__KERNEL__’ will define the KERNEL variable indicating this code will run in kernel mode, not as a user process ‘-DMODULE’ indicates to gcc that this code is a kernel module Jason Barto

Handling Runtime Parameters MODULE_PARM () 2 arguments variable name and type (“b”, “h”, “i”, “l”, “s”) A 2nd parameter of “1-5i” indicates an array of integers between 1 and 5 ints long Declare parameter receiving variables globally Jason Barto

More Kernel Macros MODULE_LICENSE () MODULE_AUTHOR () MODULE_DESCRIPTION () MODULE_SUPPORTED_DEVICE () Jason Barto

Module v0.2 /* module-0.2.c - The simplest kernel module. */ #include <linux/module.h> /* Needed by all modules */ #include <linux/kernel.h> /* Needed for KERN_ALERT */ #include <linux/init.h> /* Needed for macros */ MODULE_LICENSE (“GPL”) MODULE_AUTHOR (“Jason Barto”); MODULE_DESCRIPTION (“A module to teach module writing”); MODULE_SUPPORTED_DEVICE (“The mind”); int intparm = 0; char *strparm; MODULE_PARM (strparm, “s”); MODULE_PARM (intparm, “i”); Jason Barto

Module v0.2 cont.. int module_v0__init (void) { printk (KERN_ALERT "Initing module v0.2\n"); printk (KERN_ALERT “Received an int of %d and a string of %s\n”, intparm, strparm); return 0; } void module_v0_2_stop (void) { printk (KERN_ALERT “Unloading module v0.2\n"); module_init (module_v0_2_init); module_exit (module_v0_2_stop); Jason Barto

Further Reading Kernel Module Programming Guide http://www.tldp.org/LDP/lkmpg/ Kernel Device Drivers Introduction http://www.linuxjournal.com/article.php?sid=1219 Kernel Resource Links Page http://jungla.dit.upm.es/~jmseyas/linux/kernel/hackers-docs.html Jason Barto