1 / 25

CS 635 Advanced Systems Programming

Learn advanced systems programming concepts in C language, including kernel modules, with emphasis on Linux. Course prerequisites: x86 architecture, UNIX commands, C programming, text editing. Required textbooks and course structure provided.

ryanhubbard
Download Presentation

CS 635 Advanced Systems Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 12:45pm-1:30pm Tues-Thurs 6:30pm-7:15pm • Phone: (415) 422-6562 • Email: cruse@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), O’Reilly Media, Inc (2005) Daniel Bovet and Marco Cesati, Understanding the Linux Kernel (3rd Ed), O’Reilly 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 program’s 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 it’s 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’ • We’re allowed to ‘install’ kernel objects: $ /sbin/insmod myLKM.ko • We’re 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 <linux/module.h> header-file • Specifies a legal software license (“GPL”) • Compilation requires a special ‘Makefile’ • Execution is “passive” (it’s 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 kernel’s 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 • Here’s how you can do it: printk( “<0> 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 kernel’s log-file) • You can switch from graphics-mode to a text-mode console with <CTRL><ALT>F1 • You can switch back to graphics mode by typing <CTRL><ALT>F7

  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

More Related