Week 6 Intro to Kernel Modules, Project 2 - PowerPoint PPT Presentation

About This Presentation
Title:

Week 6 Intro to Kernel Modules, Project 2

Description:

Week 6 Intro to Kernel Modules, Project 2 Sarah Diesburg Florida State University * – PowerPoint PPT presentation

Number of Views:137
Avg rating:3.0/5.0
Slides: 62
Provided by: Sara3192
Learn more at: http://www.cs.uni.edu
Category:

less

Transcript and Presenter's Notes

Title: Week 6 Intro to Kernel Modules, Project 2


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

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

3
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

4
Kernel Logistics
  • Where should I develop my new kernel modules?
  • Inside /usr/src/linux-2.6.32/ltmodule_namegt/

5
Kernel Modules
  • Or drivers, if you prefer

6
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

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

8
Creating a Kernel Module
  • Hello world example

9
Sample Kernel Module hello.c
  • include ltlinux/init.hgt
  • include ltlinux/module.hgt
  • 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
Sample Kernel Module hello.c
Module headers
  • include ltlinux/init.hgt
  • include ltlinux/module.hgt
  • 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
Sample Kernel Module hello.c
License declaration
  • include ltlinux/init.hgt
  • include ltlinux/module.hgt
  • 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
Sample Kernel Module hello.c
  • include ltlinux/init.hgt
  • include ltlinux/module.hgt
  • 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
13
Sample Kernel Module hello.c
  • include ltlinux/init.hgt
  • include ltlinux/module.hgt
  • 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
14
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

15
Compile the Kernel Module
  • /usr/src/hellogt make
  • Creates hello.ko This is the finished kernel
    module!

16
Inserting and Removing the Module
  • insmod insert a module
  • /usr/src/hellogt sudo insmod hello.ko
  • rmmod remove a module
  • /usr/src/hellogt sudo rmmod hello.ko

17
Listing Modules
  • lsmod lists all running modules
  • /usr/src/hellogtlsmod

18
Where is it printing?
  • Look inside /var/log/syslog
  • Hint to watch syslog in realtime, issue the
    following command in a second terminal
  • gt sudo tail f /var/log/syslog
  • Demo

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

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

21
Kernel manpages
  • Section 9 of manpages
  • Must install manually for our development kernel
  • gt wget http//ftp.us.debian.org/debian/pool/main/
    l/linux-2.6/linux-manual-2.6.32_2.6.32-22_all.deb
  • gt sudo dpkg i linux-manual-2.6.32_2.6.32-22_all.
    deb

22
Kernel Headers
  • include ltlinux/init.hgt / module stuff /
  • include ltlinux/module.hgt / module stuff /
  • include ltasm/semaphore.hgt / locks /
  • include ltlinux/list.hgt / linked lists /
  • include ltlinux/string.hgt / string functions! /
  • Look inside linux-2.6.32/include/ for more
  • Google is also your friend

23
How can I explore the kernel?
  • Use lxr (Linux Cross Referencer)
  • http//lxr.linux.no/
  • Select your kernel version and enter search terms
  • Use grep on your kernel source
  • gt grep Rn xtime /usr/src/linux-2.6.32
  • R recursive, n display line number

24
Project 2 /Proc Kernel Module and Elevator
25
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

26
Procfs Headers and Global Data
  • include ltlinux/module.hgt
  • include ltlinux/kernel.hgt
  • include ltlinux/proc_fs.hgt
  • 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)

27
Procfs Creation
  • int hello_proc_init(void)
  • proc_entry
  • create_proc_entry(ENTRY_NAME,
  • PERMS,PARENT)
  • / check proc_entry ! NULL /
  • proc_entry-gtread_proc procfile_read
  • proc_entry-gtmode S_IFREG S_IRUGO
  • proc_entry-gtuid 0
  • proc_entry-gtgid 0
  • proc_entry-gtsize 11
  • printk(/proc/s created\n, ENTRY_NAME)
  • return 0

28
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

29
Procfs Deletion
  • void hello_proc_exit(void)
  • remove_proc_entry(ENTRY_NAME, NULL)
  • printk(Removing /proc/s.\n, ENTRY_NAME)

30
Procfs Registration
  • module_init(hello_proc_init)
  • module_exit(hello_proc_exit)

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

32
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

33
Part 3 Elevator Scheduling
34
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

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

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

37
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 passengers
    destination floor, that passenger gets out and
    ceases to exist

38
Passengers will line up (FIFO)
39
Each passenger has a destination floor in mind
I want to go to floor 3
40
The elevator must be started to service
passengers
Start!
41
The elevator must be started to service
passengers
Elevator starts on the first floor
42
Passengers enter in FIFO order
Make sure passengers dont exceed weight limit!
43
Passengers enter in FIFO order
More passengers can be queuing up!
44
Elevator can move to any floor
Red and black has destination floor 3, blue has
destination floor 2
Going to floor 3!
45
Elevator can move to any floor
Must take certain amount of time between floors
46
Elevator can move to any floor
Must take certain amount of time between floors
47
Elevator can move to any floor
Must take certain amount of time between floors
48
Elevator can move to any floor
Must take certain amount of time between floors
49
Elevator can move to any floor
Must take certain amount of time between floors
50
Passengers disappear when they exit
51
Elevator stop in progress
Must finish delivering passengers before stopping
Stop in Progress
52
Elevator stop in progress
Must finish delivering passengers before stopping
Stop in Progress
53
Elevator stop in progress
Must finish delivering passengers before stopping
Stop in Progress
54
Elevator stop
Full stop
55
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)

56
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

57
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

58
Kernel Time Constraints
  • include ltlinux/delay.hgt
  • void ssleep(unsigned int seconds)
  • A call to ssleep will have the program cease to
    the task scheduler for seconds number of seconds

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

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

61
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
Write a Comment
User Comments (0)
About PowerShow.com