Intro to Kernel Modules and /proc

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
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.
Homework 6 Sarah Diesburg Operating Systems CS 3430.
52 Advanced Operating Systems Writing Device Drivers.
Embedded Systems Programming Writing Device Drivers.
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.
Tutorial and Demos on Linux Virtual Machine
EECS 498 Advanced Embedded Systems Lecture 4: Linux device drivers and loadable kernel modules.
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 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.
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.
Kyu Ho Park Sept. 22, Lecture 4 proc file system(procfs) (Project 2 included) Sept.19,4pm.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Memory Addressing / Kernel Modules.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
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.
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.
1 Week 7 System Calls, Kernel Threads, Kernel Debugging Sarah Diesburg Florida State University.
Lab 12 Department of Computer Science and Information Engineering National Taiwan University Lab12 – Driver 2014/12/16 1 /21.
Debugging Techniques Linux Kernel Programming CIS 4930/COP 5641.
COMP 3438 – Part I - Lecture 5 Character Device Drivers
Multiple File Compilation and linking By Bhumik Sapara.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
Kernel Modules – Introduction CSC/ECE 573, Sections 001 Fall, 2012.
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.
Embedded Software Design Week II Linux Intro Linux Kernel.
Virtual Memory Mohammad H. Mofrad February 23, 2016
Operating System Kernel Compilation
OS – Ex 1 Nezer J. Zaidenberg.
Device Driver_Skeleton
Chapter 1: A Tour of Computer Systems
Linux Kernel Module Programming
Computer System Laboratory
Linux Kernel Module Programming
Linux Kernel Driver.
Want to play a game? – Linux Kernel Modules
CSE 351 Section 1: HW 0 + Intro to C
Kthreads, Mutexes, and Debugging
Operating System Kernel Compilation
Dynamic Memory Allocation
chapter 2 - Hello World Model
Kernel Structure and Infrastructure
CS 6560 Operating System Design
Operation System Program 1
Created by Hwansoo Han Edited by Ikjun Yeom
Kernel – Device Drivers (part 2)
Lab 4 Kernel Module Operating System Lab.
CS 6560 Operating System Design Kernel Loadable Modules
Kernel Structure and Infrastructure
Implementation of Embedded OS
Computer System Laboratory
Loadable Kernel Modules
Operating System Kernel Compilation
Kthreads, Mutexes, and Debugging
Presentation transcript:

Intro to Kernel Modules and /proc Sarah Diesburg CS 3430 Operating Systems

Or “drivers”, if you prefer… Kernel Modules Or “drivers”, if you prefer…

Kernel Module A kernel module is a portion of kernel functionality that can be dynamically loaded into the operating system at run-time Example USB drivers File system drivers Disk drivers Cryptographic libraries

Why not just compile everything into the kernel? Each machine only needs a certain number of drivers For example, should not have to load every single motherboard driver Load only the modules you need Smaller system footprint Dynamically load modules for new devices Camera, new printer, etc.

Creating a Kernel Module Hello world example

Sample Kernel Module: hello.c #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!\n”); return 0; } static void hello_exit(void) printk(KERN_ALERT “Goodbye, sleepy world.\n”); module_init(hello_init); module_exit(hello_exit);

Sample Kernel Module: hello.c Module headers #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!\n”); return 0; } static void hello_exit(void) printk(KERN_ALERT “Goodbye, sleepy world.\n”); module_init(hello_init); module_exit(hello_exit);

Sample Kernel Module: hello.c License declaration #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!\n”); return 0; } static void hello_exit(void) printk(KERN_ALERT “Goodbye, sleepy world.\n”); module_init(hello_init); module_exit(hello_exit);

Sample Kernel Module: hello.c #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!\n”); return 0; } static void hello_exit(void) printk(KERN_ALERT “Goodbye, sleepy world.\n”); module_init(hello_init); module_exit(hello_exit); Initialization function, runs when module loaded Tells kernel which function to run on load

Sample Kernel Module: hello.c #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!\n”); return 0; } static void hello_exit(void) printk(KERN_ALERT “Goodbye, sleepy world.\n”); module_init(hello_init); module_exit(hello_exit); Exit function, runs when module exits Tells kernel which function to run on exit

Sample Kernel Module: Makefile ifneq ($(KERNELRELEASE),) obj-m := hello.o else KERNELDIR ?= \ /lib/modules/`uname -r`/build/ PWD := `pwd` default: $(MAKE) -C $(KERNELDIR) \ M=$(PWD) modules endif clean: rm -f *.ko *.o Module* *mod*

Compile the Kernel Module /usr/src/hello$> make Creates hello.ko – This is the finished kernel module!

Inserting and Removing the Module insmod – insert a module /usr/src/hello$> sudo insmod hello.ko rmmod – remove a module /usr/src/hello$> sudo rmmod hello.ko

Listing Modules lsmod – lists all running modules /usr/src/hello$>lsmod

Where is it printing? Look inside /var/log/syslog Hint – to watch syslog in realtime, issue the following command in a second terminal: $> sudo tail –f /var/log/syslog Demo…

Kernel Module vs User Application All kernel modules are event-driven Register functions Wait for requests and service them Server/client model No standard C library Why not? No floating point support Segmentation fault could freeze/crash your system Kernel ‘oops’!

Kernel Functions printk() instead of printf() kmalloc() instead of malloc() kfree() instead of free() Where can I find definitions of these kernel functions?

Kernel manpages Section 9 of manpages Use manpages $> sudo apt-get install linux-manual-3.16 $> man printk

Kernel Headers #include <linux/init.h> /* module stuff */ #include <linux/module.h> /* module stuff */ #include <asm/semaphore.h> /* locks */ #include <linux/list.h> /* linked lists */ #include <linux/string.h> /* string functions! */ Look inside linux-4.15.4/include/ for more… Google is also your friend

Project 2: Kernel Modules

procfs Kernel Module procfs “hello world” example Steps Creates a read/write procfs entry Steps Create entry in module_init function Registers reading/writing function using file_operations structure Delete entry in module_cleanup function

Testing Procfs $> sudo insmod hello_proc.ko $> sudo tail /var/log/syslog $> cat /proc/helloworld $> echo 2 > /proc/helloworld $> sudo rmmod hello_proc

Part 2: Remember You will write your own proc module called remember that Allows the user to write a string to /proc/remember (max length 80) Allows the user to read /proc/remember and get back the string that was just added If no string was added, the string EMPTY should be read instead

Remember Example #> echo “Hello there” > /proc/remember #> cat /proc/remember Hello there #>