DOC PREVIEW
USF CS 635 - Advanced Systems Programming

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 635 Advanced Systems ProgrammingInstructor Contact InformationCourse Textbooks‘Extensibility’Two Extensibility MechanismsLoadable Kernel Modules‘Superuser’ privileges‘insmod’ and ‘rmmod’Creating a new LKMNormal module structureRequired module functionsA ‘minimal’ moduleHow to compile an LKMFormat of the ‘Makefile’InconveniencesOur ‘mmake’ toolTry it outKernel 2.6 optionsAn example: TASK_SIZETraditional LinuxThe 4GB/4GB splitIn-Class Exercise #2SummaryCS 635Advanced Systems ProgrammingSpring 2005Professor Allan B. CruseUniversity of San FranciscoInstructor Contact Information•Office: Harney Science Center – 212•Hours: Mon-Wed 6:30pm-7:15pmTues-Thurs 2:30pm-3:15pm•Phone: (415) 422-6562•Email: [email protected]•Webpage: cs.usfca.edu/~cruse/Course TextbooksAlessandro 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)‘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!Two Extensibility Mechanisms•‘Open Source’ programming•‘Loadable’ kernel modulesLoadable 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!‘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‘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/lsmodCreating 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()’)Normal module structure•Two ‘module administration’ functions [these are required]plus• Appropriate ‘module service’ functions [these are option al]Required module functions•int init_module( void );•// gets called during module installation•void cleanup_module( void );•// gets called during module removalA ‘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 messagesHow 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 textbookFormat of the ‘Makefile’ifneq ($(KERNELRELEASE),)obj-m := mymod.oelseKERNELDIR := /lib/modules/$(shell uname –r)/buildPWD := $(shell pwd)default:$(MAKE) -C $(KERNELDIR) M=$(PWD) modulesendifInconveniences•Your ‘Makefile’ has to be edited every time you create another new module•Then, when you compile the new module, like this: $ makethere 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 wantedOur ‘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!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 moduleKernel 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 programsAn 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 mapsTraditional Linuxkernel memory(1 GB)user memory(3 GB)0xC00000000x00000000TASK_SIZE = 0xC0000000 (Upper-limit of user-space) PAGE_OFFSET = 0xC0000000 (Lower bound of kernel space)The 4GB/4GB splituser-memory(~4GB)kernel-memory(~4GB)fixed maps fixed mapsTASK_SIZE = 0xFF000000PAGE_OFFSET = 0x02000000In-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 );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


View Full Document

USF CS 635 - Advanced Systems Programming

Download Advanced Systems Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Advanced Systems Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Advanced Systems Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?