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

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
R4 Dynamically loading processes. Overview R4 is closely related to R3, much of what you have written for R3 applies to R4 In R3, we executed procedures.
CSE 303 Lecture 16 Multi-file (larger) programs
Building and Running Modules Sarah Diesburg COP 5641.
Building and Running Modules Linux Kernel Programming CIS 4930/COP 5641.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Linking & Loading CS-502 Operating Systems
Building and Running Modules Ted Baker  Andy Wang CIS 4930 / COP 5641.
Homework 6 Sarah Diesburg Operating Systems CS 3430.
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.
Process management in Minix1 Processes Process is a program in execution. Program is a static entity while process is an active entity. Process Control.
CS 635 Advanced Systems Programming Spring 2005 Professor Allan B. Cruse University of San Francisco.
Tutorial and Demos on Linux Virtual Machine
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.
Memory Management ◦ Operating Systems ◦ CS550. Paging and Segmentation  Non-contiguous memory allocation  Fragmentation is a serious problem with contiguous.
Operating System Program 5 I/O System DMA Device Driver.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
For OS Experiments. What Do We Need? A Computer &
Nachos Phase 1 Code -Hints and Comments
Introduction to Processes CS Intoduction to Operating Systems.
COMP 3438 – Part I - Lecture 4 Introduction to Device Drivers Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Sogang University Advanced Operating Systems (Linux Module Programming) Sang Gue Oh, Ph.D.
Real-Time Scheduling CS4730 Fall 2010 Dr. José M. Garrido Department of Computer Science and Information Systems Kennesaw State University.
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
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.
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.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()
1 Memory Management Chapter 7. 2 Memory Management Subdividing memory to accommodate multiple processes Memory needs to be allocated to ensure a reasonable.
CSE 451: Operating Systems Section 3: Project 0 recap, Project 1.
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.
COMP 3438 – Part I - Lecture 5 Character Device Drivers
Multiple File Compilation and linking By Bhumik Sapara.
Kernel Structure and Infrastructure David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
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.
Lecture 4 Page 1 CS 111 Summer 2013 Scheduling CS 111 Operating Systems Peter Reiher.
Virtual Memory Mohammad H. Mofrad February 23, 2016
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
OS – Ex 1 Nezer J. Zaidenberg.
Introduction to Operating Systems
Processes and threads.
Linux Kernel Module Programming
Computer System Laboratory
Linking & Loading.
Linux Kernel Module Programming
Want to play a game? – Linux Kernel Modules
Kthreads, Mutexes, and Debugging
Introduction to Operating Systems
Intro to Kernel Modules and /proc
chapter 2 - Hello World Model
Kernel Structure and Infrastructure
CS 6560 Operating System Design
Kernel – Device Drivers (part 2)
Lab 4 Kernel Module Operating System Lab.
Linking & Loading CS-502 Operating Systems
CS 6560 Operating System Design Kernel Loadable Modules
Kernel Structure and Infrastructure
Implementation of Embedded OS
Computer System Laboratory
Linking & Loading CS-502 Operating Systems
Kthreads, Mutexes, and Debugging
Presentation transcript:

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

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

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 3

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. 4

Creating a Kernel Module Hello world example 5

Sample Kernel Module: hello.c #include 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); 6

Sample Kernel Module: hello.c #include 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); 7 Module headers

Sample Kernel Module: hello.c #include 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); 8 License declaration

Sample Kernel Module: hello.c #include 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); 9 Initialization function, runs when module loaded Tells kernel which function to run on load

Sample Kernel Module: hello.c #include 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); 10 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* 11

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

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 13

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

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… 15

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’! 16

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

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

Kernel Headers #include /* module stuff */ #include /* locks */ #include /* linked lists */ #include /* string functions! */ Look inside linux-4.2.2/include/ for more… Google is also your friend 19

Project 2: Kernel Modules 20

procfs Kernel Module procfs “hello world” example  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 21

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

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 23

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

Part 3: Kitchen Scheduling 25

Part 3: Kitchen Scheduling Implement a /proc kernel module that simulates an busy kitchen at a restaurant Your /proc/kitchen file must accept writes of different dishes  Each dish takes some time to process Your /proc/kitchen file must return status information when read 26

Why Scheduling? Classic producer/consumer analogy Similar to disk schedulers  File system produces read/write requests  Disk consumes requests, optimized for disk head position, rotational delays, etc. 27

Your Kitchen One kitchen 20 order slots  Use a static array of ints? Four types of dishes  1 - Caesar Salad  2 - Hamburger  3 - Personal Pizza  4 - Beef Wellington 28

Food orders A write of 1-4 to /proc/kitchen will fill an order slot with the dish corresponding to the number  You decide how the order slots get filled (e.g. round robin or other way) Kitchen takes 1 second to look in an order slot  Regardless if empty or containing an order 29

Food Orders The kitchen can only process one order at a time Each order takes a different amount of time to process  2 seconds for a Caesar Salad  3 seconds for a Hamburger  4 seconds for a Personal Pizza  8 seconds for a Beef Wellington 30

Food Order Once an order is processed, the kitchen can mark that order slot as empty and should look at other order slots to find more orders to process. 31

Additional Kitchen Commands In addition to accepting orders 1-4, a kitchen should respond to writes of 0 or -1 in the following ways  0 : start the kitchen  -1: stop the kitchen 32

Starting the Kitchen Before a kitchen is started, it cannot be processing orders  But it can receive orders on the queue  The cooks just aren’t working yet! When a kitchen is stopped, it must finish processing the current order and cease to process any more 33

Starting the Kitchen The actual order processing logic will be run in a kthread  Introduced in the next recitation lecture 34

Status Information Performing a read on /proc/kitchen should return some status information  Kitchen status: running or not running  Current spot being looked at in the queue  Current order being processed If the kitchen is not running, the last two pieces of information do not need to be dislplayed 35

Demo 36

Scheduling Algorithms A scheduling algorithm considers the state of the consumers and all requests and tries to optimize some metric  Throughput: Maximize total requests, minimize processing total time.  Priorities: Requests now have deadlines. Maximize number of requests meeting deadlines.  Burst throughput: Maximize peak requests that can be handled.  Energy: Minimize consumer action 37

Kernel Time Constraints #include void ssleep(unsigned int seconds); A call to ssleep will have the program cease to the task scheduler for seconds number of seconds 38

Additional Design Considerations How to place orders in order slots? How to check order slots from the kitchen? What scheduling algorithm to use? 39

Next Time Locking  Must lock when accessing anything global Kitchen queue Kitchen status variables Kthread  How to start/stop the looping logic that looks in the order queue and “processes” orders 40