CS 635 Advanced Systems Programming - PowerPoint PPT Presentation

About This Presentation
Title:

CS 635 Advanced Systems Programming

Description:

But a kernel module differs from a normal C application program (e.g., no main()' function) ... Resembles normal layout of C programs. but ... – PowerPoint PPT presentation

Number of Views:83
Avg rating:3.0/5.0
Slides: 26
Provided by: professora5
Category:

less

Transcript and Presenter's Notes

Title: CS 635 Advanced Systems Programming


1
CS 635Advanced Systems Programming
  • Fall 2007
  • Professor Allan B. Cruse
  • University of San Francisco

2
Instructor Contact Information
  • Office Harney Science Center 212
  • Hours Mon-Wed-Fri 1245pm-130pm
  • Tues-Thurs 630pm-715pm
  • Phone (415) 422-6562
  • Email cruse_at_usfca.edu
  • Webpage http//cs.usfca.edu/cruse/

3
The class website
  • URL http//cs.usfca.edu/cruse/cs635/
  • General description of the course
  • Links to some useful online resources
  • Lecture-slides and demo-programs
  • System software for use with this course
  • Class announcements (e.g., exam dates)
  • A link to our CS635 discussion-list
  • Our textbook reading assignments

4
Course Textbooks
  • Jonathan Corbet, Alessandro Rubini and Greg
    Kroah-Hartman,
  • Linux Device Drivers (3rd Ed),
  • OReilly Media, Inc (2005)
  • Daniel Bovet and Marco Cesati, Understanding the
    Linux Kernel (3rd Ed), OReilly Media, Inc (2006)

5
Some important prerequisites
  • You are acquainted with x86 architecture
  • You can execute Linux/UNIX commands
  • You know how to use a text-editing tool
  • You can write programs in the C language
  • You can print out a programs source-file

6
Typical C layout
  • Basic structure of a C program
  • Comment-banner (showing title and abstract)
  • Preprocessor directives (e.g., for header-files)
  • Global data-declarations (if they are needed)
  • Required main() function (as the entry-point)
  • Can invoke printf() (for formatted output)
  • Optionally may define some other functions

7
Example program in C
8
Extensibility
  • A modern OS needs the ability to evolve
  • Will need to support new devices
  • Will need to allow bugs to be fixed
  • Will need to permit performance gains
  • Else OS may suffer early obsolescence!

9
Extensibility with Linux
  • Two mechanisms for extensibility
  • Open Source development
  • Loadable kernel modules (LKMs)

10
Loadable Kernel Modules
  • Convenient technique for OS extensibility
  • Also allows us to study how kernel works
  • Kernel can be modified while its running
  • No need to recompile and then reboot
  • But inherently unsafe any bug can cause a
    system malfunction -- or complete crash!

11
Superuser privileges
  • Modifying a running kernel is risky
  • Only authorized system administrators
  • are allowed to install kernel modules
  • But our classroom workstations will allow us some
    (limited) administrator privileges

12
insmod and rmmod
  • Were allowed to install kernel objects
  • /sbin/insmod myLKM.ko
  • Were allowed to remove kernel objects
  • /sbin/rmmod myLKM
  • Anyone is allowed to list kernel objects
  • /sbin/lsmod

13
Creating a new LKM
  • You can use any text-editor (e.g., vi or
    emacs) to create source-code (in the C
    language) for a Linux kernel module (i.e., an
    LKM)
  • But a kernel module differs from a normal C
    application program (e.g., no main() function)
  • A kernel module cannot call any of the familiar
    functions from the standard C runtime libraries
  • For any LKM, two entry-points are mandatory (one
    for initialization, and one for cleanup)

14
Normal LKM structure
  • Resembles normal layout of C programs
  • but
  • Two module administration functions
    these are required
  • plus
  • Appropriate module service functions
    these are optional

15
Other LKM differences
  • Module uses printk() instead of printf()
  • Includes the ltlinux/module.hgt header-file
  • Specifies a legal software license (GPL)
  • Compilation requires a special Makefile
  • Execution is passive (its a side-effect)
  • Module has no restriction on privileges

16
Required module functions
  • int init_module( void )
  • // this gets called during module installation
  • void cleanup_module( void )
  • // this gets called during module removal
  • A newer syntax allows memory-efficiency
  • module_init(my_init)
  • module_exit(my_exit)

17
Kernel module written in C
18
Format of the Makefile
  • ifneq ((KERNELRELEASE),)
  • obj-m mymod.o
  • else
  • KERNELDIR /lib/modules/(shell uname
    r)/build
  • PWD (shell pwd)
  • default
  • (MAKE) -C (KERNELDIR) M(PWD) modules
  • endif

19
Inconveniences
  • That Makefile has to be edited every time you
    create another new module! ?
  • Then, when you compile the new module, like this
    make
  • there are more than a half-dozen files that get
    created (some of them are hidden) in your
    current directory, but just one is the .ko
    (kernel object) that you really wanted

20
Our mmake tool
  • Since we will be writing and compiling lots of
    modules during our course, we wrote a tool that
    conveniently automates the steps
  • You can simply type ./mmake
  • It creates the Makefile you need, in your
    current directory, to compile all modules that
    currently reside in that directory
  • Afterward it erases all the unneeded files!

21
Improvement to mmake
  • After watching past students use mmake we
    realized that it would be better to allow
    compiling just one module at a time
  • We kept the former behavior as an option
  • But now we allow the user to specify with a
    command-line parameter which module (or modules)
    they wish to re-compile
  • ./mmake mymod

22
In-class exercise 1
  • Download mmake.cpp from class website and
    compile it with make (or alternatively use
    g mmake.cpp o mmake )
  • Download the kello.c source-file from the
    website, and compile it using mmake
  • Add the kello.ko kernel-object to Linux using
    the Linux /sbin/insmod command
  • Use dmesg to view the kernels log-file
  • Remove kello (with /sbin/rmmod kello)

23
Showing kernel messages
  • You can modify the printk() text-string so its
    message will be sure to be displayed -- it will
    be output to the graphical desktop
  • Heres how you can do it
  • printk( lt0gt Hello, everybody! \n )

This log-level indicates a kernel emergency
24
In-class exercise 2
  • Modify the kello.c source-file so that the
    messages will be visible in a window on the
    graphical desktop (in addition to being written
    to the kernels log-file)
  • You can switch from graphics-mode to a text-mode
    console with ltCTRLgtltALTgtF1
  • You can switch back to graphics mode by typing
    ltCTRLgtltALTgtF7

25
Summary
  • Download mmake.cpp and kello.c
  • Compile mmake.cpp using make
  • Then compile kello.c using mmake
  • Install kello.ko (and see printk-message)
  • Remove kello (to see another message)
  • Modify the printk() statements in kello.c
  • Recompile and reinstall to view new info
Write a Comment
User Comments (0)
About PowerShow.com