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

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

    21-Dec-2015
  • Category

    Documents

  • view

    215
  • download

    1

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

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

CS 635Advanced Systems Programming

Fall 2007

Professor Allan B. Cruse

University of San Francisco

Page 2: 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: [email protected]

• Webpage: http://cs.usfca.edu/~cruse/

Page 3: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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

Page 4: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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)

Page 5: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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

Page 6: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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

Page 7: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

Example program in C

Page 8: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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

Page 9: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

Extensibility with Linux

Two mechanisms for ‘extensibility’:

• ‘Open Source’ development

• ‘Loadable’ kernel modules (LKMs)

Page 10: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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!

Page 11: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

‘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

Page 12: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

‘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

Page 13: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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’)

Page 14: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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]

Page 15: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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’

Page 16: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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);

Page 17: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

Kernel module written in C

Page 18: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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

Page 19: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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

Page 20: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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!

Page 21: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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

Page 22: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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’)

Page 23: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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’

Page 24: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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

Page 25: CS 635 Advanced Systems Programming Fall 2007 Professor Allan B. Cruse University of San Francisco.

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