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-fileTypical 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
25 trang |
Chia sẻ: candy98 | Lượt xem: 972 | Lượt tải: 0
Bạn đang xem trước 20 trang tài liệu Advanced Systems Programming - Lesson 1: Introduction, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
CS 635
Advanced Systems Programming
Fall 2007
Professor Allan B. Cruse
University of San Francisco
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:
The class website
• URL:
– 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
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)
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
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
Example program in C
‘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!
Extensibility with Linux
Two mechanisms for ‘extensibility’:
• ‘Open Source’ development
• ‘Loadable’ kernel modules (LKMs)
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!
‘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
‘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
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’)
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]
Other LKM differences
• Module uses ‘printk()’ instead of ‘printf()’
• Includes the 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’
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);
Kernel module written in C
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
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
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!
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
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’)
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( “ Hello, everybody! \n” );
This log-level indicates a ‘kernel emergency’
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 F1
• You can switch back to graphics mode by
typing F7
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