1 Week 6 Intro to Kernel Modules, Project 2 Sarah Diesburg Florida State University.

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
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
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.
Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
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.
Processes CSCI 444/544 Operating Systems Fall 2008.
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.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
Tutorial and Demos on Linux Virtual Machine
Data Structures in the Kernel Sarah Diesburg COP 5641.
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 &
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
C questions A great programmer codes excellent code in C and Java. The code does video decoding. Java code works faster then C on my computer. how come?
Sogang University Advanced Operating Systems (Linux Module Programming) Sang Gue Oh, Ph.D.
1 What is a Kernel The kernel of any operating system is the core of all the system’s software. The only thing more fundamental than the kernel is the.
Processes and Threads CS550 Operating Systems. Processes and Threads These exist only at execution time They have fast state changes -> in memory and.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
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.
CS162B: Pipes Jacob T. Chan. Pipes  These allow output of one process to be the input of another process  One of the oldest and most basic forms of.
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.
Using the /proc File System with “scull” Sarah Diesburg COP 5641.
Implementation of Embedded OS Lab3 Linux Kernel Modules.
Project 2 kthreads, concurrency, shuttle. Highlevel overview User space –program wants to control the shuttle simulation by sending requests. –start_shuttle()
CSE 451: Operating Systems Section 3: Project 0 recap, Project 1.
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.
OS Project 0 February 25, Outline  Linux Installation  Linux Kernel Compilation  System Call Development  Kernel Modules / 452.
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.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
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.
OPERATING SYSTEMS COURSE THE HEBREW UNIVERSITY SPRING FUSE File System.
Virtual Memory Mohammad H. Mofrad February 23, 2016
Operating System Kernel Compilation
OS – Ex 1 Nezer J. Zaidenberg.
Chapter 3: Processes.
Computer System Laboratory
Linux Kernel Module Programming
Kthreads, Mutexes, and Debugging
Operating System Kernel Compilation
Intro to Kernel Modules and /proc
Review and Q/A.
chapter 2 - Hello World Model
Kernel Structure and Infrastructure
CS 6560 Operating System Design
Operation System Program 1
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
Operating System Kernel Compilation
Kthreads, Mutexes, and Debugging
Presentation transcript:

1 Week 6 Intro to Kernel Modules, Project 2 Sarah Diesburg Florida State University

Kernel Logistics Where should I put the kernel source?  /usr/src/  Creates /usr/src/linux / Where do I issue kernel building commands (e.g. ‘make oldconfig’, ‘make menuconfig’, ‘make’, …)?  Inside /usr/src/linux / 2

Kernel Logistics Where is the kernel image installed?  Inside /boot/  Starts with vmlinuz… Where does the initramfs image go?  Inside /boot/ Where is the grub file?  /boot/grub/menu.lst 3

Kernel Logistics Where should I develop my new kernel modules?  Inside /usr/src/linux / / 4

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

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 6

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

Creating a Kernel Module Hello world example 8

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

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 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); 11 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); 12 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); 13 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* 14

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

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 16

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

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

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

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

Kernel manpages Section 9 of manpages Must install manually for our development kernel $> wget 2.6/linux-manual _ _all.deb $> sudo dpkg –i linux-manual _ _all.deb 21

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

How can I explore the kernel? Use lxr (“Linux Cross Referencer”):   Select your kernel version and enter search terms Use grep on your kernel source $> grep –Rn xtime /usr/src/linux  R = recursive, n = display line number 23

Project 2: /Proc Kernel Module and Elevator 24

procfs Kernel Module procfs “hello world” example  Creates a read-only procfs entry Steps  Create entry in module_init function  Register reading function with procfs_read  Delete entry in module_cleanup function Reference  Linux Kernel Module Programming Guide: Proc FS Linux Kernel Module Programming Guide: Proc FS 25

Procfs: Headers and Global Data #include MODULE_LICENSE(“GPL”); #define ENTRY_NAME “helloworld” #define PERMS 0644 #define PARENT NULL struct proc_dir_entry *proc_entry; int procfile_read(char *buf, char **buf_location, off_t offset, int buffer_length, int *eof, void *data); 26

Procfs: Creation int hello_proc_init(void) { proc_entry = create_proc_entry(ENTRY_NAME, PERMS,PARENT); /* check proc_entry != NULL */ proc_entry->read_proc = procfile_read; proc_entry->mode = S_IFREG | S_IRUGO; proc_entry->uid = 0; proc_entry->gid = 0; proc_entry->size = 11; printk(“/proc/%s created\n”, ENTRY_NAME); return 0; } 27

Procfs: Reading int procfile_read(char *buf, char **buf_location, off_t offset, int buffer_length, int *eof, void *data) { int ret; printk(“/proc/%s read called.\n”, ENTRY_NAME); /* Setting eof. We exhaust all data in one shot */ *eof = 1; ret = sprintf(buf, “Hello World!\n”); return ret; } 28

Procfs: Deletion void hello_proc_exit(void) { remove_proc_entry(ENTRY_NAME, NULL); printk(“Removing /proc/%s.\n”, ENTRY_NAME); } 29

Procfs: Registration module_init(hello_proc_init); module_exit(hello_proc_exit); 30

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

Part 2: Kernel Time Implement a procfs entry to display the value of xtime  Hint: You may not be able to directly read xtime from your module, but maybe something else can… 32

Part 3: Elevator Scheduling 33

Part 3: Elevator Scheduling Implement a kernel module that simulates an elevator system Implement system calls to interact with your elevator Implement a procfs entry to display debugging information Test using a set of user-space programs to exercise your system 34

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

Your Elevator One elevator Five floors Four types of people  Adults  Children  Delivery people  Maintenance people The elevator cannot exceed its maximum weight load 36

Your Elevator People will line up at each floor in a first-in, first-out (FIFO) order Each person has a starting floor and a destination floor The elevator must pause for a period of time to collect people and move between floors Once the elevator reaches a passenger’s destination floor, that passenger gets out and ceases to exist 37

Passengers will line up (FIFO) 38

Each passenger has a destination floor in mind… 39 I want to go to floor 3

The elevator must be started to service passengers… 40 Start!

The elevator must be started to service passengers… 41 Elevator starts on the first floor

Passengers enter in FIFO order 42 Make sure passengers don’t exceed weight limit!

Passengers enter in FIFO order 43 More passengers can be queuing up!

Elevator can move to any floor 44 Going to floor 3! Red and black has destination floor 3, blue has destination floor 2

Elevator can move to any floor 45 Must take certain amount of time between floors…

Elevator can move to any floor 46 Must take certain amount of time between floors…

Elevator can move to any floor 47 Must take certain amount of time between floors…

Elevator can move to any floor 48 Must take certain amount of time between floors…

Elevator can move to any floor 49 Must take certain amount of time between floors…

Passengers disappear when they exit… 50

Elevator stop in progress… 51 Must finish delivering passengers before stopping… Stop in Progress

Elevator stop in progress… 52 Must finish delivering passengers before stopping… Stop in Progress

Elevator stop in progress… 53 Must finish delivering passengers before stopping… Stop in Progress

Elevator stop 54 Full stop

Controlling the Elevator Implement the following system calls int start_elevator(void) int issue_request(int passenger_type, int start_floor, int destination_floor) int stop_elevator(void) 55

Elevator 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 56

Elevator Test Applications consumer.c  Runs in infinite loop  Issues K passenger requests once per second producer.c  Takes an argument telling the elevator to start or stop 57

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 58

Additional Design Considerations How to move elevator? How to protect the floor FIFO queues? What scheduling algorithm to use? 59

Next Time Kernel debugging techniques How to insert system calls Some elevator scheduling algorithms 60

What you should do? Finish part 1 (5 system calls) Finish part 2 (/proc module) Try sample kernel module and proc module Make skeleton part 3 module Make elevator and floor queue data structures 61