23
CS 635 Advanced Systems Programming Spring 2005 Professor Allan B. Cruse University of San Francisco

CS 635 Advanced Systems Programming

  • Upload
    toan

  • View
    51

  • Download
    0

Embed Size (px)

DESCRIPTION

CS 635 Advanced Systems Programming. Spring 2005 Professor Allan B. Cruse University of San Francisco. Instructor Contact Information. Office: Harney Science Center – 212 Hours: Mon-Wed 6:30pm-7:15pm Tues-Thurs 2:30pm-3:15pm Phone: (415) 422-6562 Email: [email protected] - PowerPoint PPT Presentation

Citation preview

Page 1: CS 635 Advanced Systems Programming

CS 635Advanced Systems Programming

Spring 2005

Professor Allan B. Cruse

University of San Francisco

Page 2: CS 635 Advanced Systems Programming

Instructor Contact Information

• Office: Harney Science Center – 212

• Hours: Mon-Wed 6:30pm-7:15pm

Tues-Thurs 2:30pm-3:15pm

• Phone: (415) 422-6562

• Email: [email protected]

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

Page 3: CS 635 Advanced Systems Programming

Course Textbooks

Alessandro Rubini and Jonathan Corbet,

Linux Device Drivers (Third Edition), O’Reilly & Associates, Inc (2005)

M. Beck et al, Linux Kernel Programming (Third Edition), Addison-Wesley (2002)

Page 4: CS 635 Advanced Systems Programming

‘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 5: CS 635 Advanced Systems Programming

Two Extensibility Mechanisms

• ‘Open Source’ programming

• ‘Loadable’ kernel modules

Page 6: CS 635 Advanced Systems Programming

Loadable Kernel Modules

• A great mechanism 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 a complete crash!

Page 7: CS 635 Advanced Systems Programming

‘Superuser’ privileges

• Modifying a running kernel is ‘risky’

• Only authorized ‘system administrators’

are allowed to install kernel modules

• Our classroom workstations will allow us some limited administrator privileges

Page 8: CS 635 Advanced Systems Programming

‘insmod’ and ‘rmmod’

• We are allowed to ‘install’ kernel objects:$ /sbin/insmod myLKM.ko

• We are allowed to ‘remove’ kernel objects:$ /sbin/rmmod myLKM

• Anyone is allowed to ‘list’ kernel objects:$ /sbin/lsmod

Page 9: CS 635 Advanced Systems Programming

Creating a new LKM

• You can use any text-editor (e.g., ‘vi’ or ‘emacs’) to create the source-code (in C) for a Linux kernel module (e.g., mod.c)

• 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 (i.e., ‘init_module()’ and ‘cleanup_module()’)

Page 10: CS 635 Advanced Systems Programming

Normal module structure

• Two ‘module administration’ functions [these are required]

plus

• Appropriate ‘module service’ functions [these are optional]

Page 11: CS 635 Advanced Systems Programming

Required module functions

• int init_module( void );

• // gets called during module installation

• void cleanup_module( void );

• // gets called during module removal

Page 12: CS 635 Advanced Systems Programming

A ‘minimal’ module

• We have written a ‘wizard’ application that automatically creates the C boilerplate for a new module:

$ newmod <module-name>

• It creates a source-file with the essential Linux header, the two required functions, and a ‘MODULE_LICENSE’ statement

• It uses ‘printk()’ for logging messages

Page 13: CS 635 Advanced Systems Programming

How to compile an LKM

• The Linux kernel has been a moving target

• Each new version has introduced changes

• A big change in kernel 2.6 concerns how a kernel module gets compiled

• No longer independent of kernel’s options

• Requires a specially created ‘Makefile’ for the specific module(s) you wish to compile

• See the discussion in our LDD3 textbook

Page 14: CS 635 Advanced Systems Programming

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 15: CS 635 Advanced Systems Programming

Inconveniences

• Your ‘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 16: CS 635 Advanced Systems Programming

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 simply type: $ mmake

• It creates the ‘Makefile’ you need, in your current directory, to compile all modules that reside in that directory

• Afterward it erases all the unneeded files!

Page 17: CS 635 Advanced Systems Programming

Try it out

• As an in-class programming exercise, you are asked to ‘download’ our two developer tools newmod.cpp and mmake.cpp from the CS 635 website (under ‘Handouts’)

• Compile these two application-programs: $ make newmod$ make mmake

• Run ‘newmod’ to create a mimimal module• Then run ‘mmake’ to compile that module

Page 18: CS 635 Advanced Systems Programming

Kernel 2.6 options

• Numerous configuration options exist for recent versions of the Linux kernel (our workstations are using version 2.6.10)

• Our System Administrator had to choose which specific features to ‘activate’ when this kernel was being compiled

• Which features got ‘enabled’ will have an impact on our kernel module programs

Page 19: CS 635 Advanced Systems Programming

An example: TASK_SIZE

• Previous Linux versions (e.g., 2.2 and 2.4) normally implemented the user-memory and kernel-memory for each task within the same 4GB virtual address-space map, and this is still possible with kernel 2.6

• But the default configuration for kernel 2.6 (in the Fedora Core 3 distribution) uses a different scheme in which user-memory and kernel-memory use separate maps

Page 20: CS 635 Advanced Systems Programming

Traditional Linux

kernel memory(1 GB)

user memory(3 GB)

0xC0000000

0x00000000

TASK_SIZE = 0xC0000000 (Upper-limit of user-space)

PAGE_OFFSET = 0xC0000000 (Lower bound of kernel space)

Page 21: CS 635 Advanced Systems Programming

The 4GB/4GB split

user-memory(~4GB)

kernel-memory(~4GB)

fixed maps fixed maps

TASK_SIZE = 0xFF000000

PAGE_OFFSET = 0x02000000

Page 22: CS 635 Advanced Systems Programming

In-Class Exercise #2

• After you have successfully built, compiled and installed, then removed, your ‘minimal’ module, try adding some statements to its ‘init_module()’ function that will print useful information, like this: printk( “PAGE_OFFSET=%08X “, PAGE_OFFSET );

printk( “TASK_SIZE=%08X \n“, TASK_SIZE );

Page 23: CS 635 Advanced Systems Programming

Summary

• Download newmod.cpp and mmake.cpp

• Compile these using ‘make’

• Run ‘newmod mod’ to create ‘mod.c’

• Run ‘mmake’ to compile ‘mod.c’

• Install ‘mod.ko’ (and see printk-message)

• Remove ‘mod’ and add new statements

• Recompile and reinstall to see new info