Project #2, Linux Kernel Modifications CS-502 Fall Programming Project #2 Linux Kernel Hacking CS-502 Operating Systems Fall 2006
Project #2, Linux Kernel Modifications CS-502 Fall Objective To learn how to work with an operating system kernel To understand some of the constraints and techniques of programming in a kernel (versus user space)
Project #2, Linux Kernel Modifications CS-502 Fall Method To build and install a new Linux kernel To add a new system call to the Linux kernel To get useful information from the data structures of a Linux kernel
Project #2, Linux Kernel Modifications CS-502 Fall Background – User vs. Kernel mode Hardware provides two modes –Indicated by bit in PSW Allows OS to protect itself & system components against –Faulty and malicious processes Some instructions designated as privileged –Only executable in kernel mode System call, all traps, & interrupts change mode to kernel –return from system call resets it to user
Project #2, Linux Kernel Modifications CS-502 Fall Transition from User to Kernel Mode
Project #2, Linux Kernel Modifications CS-502 Fall Accessing the Kernel via System Call Normally embedded within a library routine User API never uses system calls directly System call mechanism is machine specific Different CPU architectures make system calls in different ways System call numbers different for various architectures Even for same operating system & version! E.g., poll system call is #167 on PowerPC but #168 on Intel 386 platforms (in SUSE Linux 9.3)
Project #2, Linux Kernel Modifications CS-502 Fall Inside Kernel, the OS can … Read and modify data structures not in user address space Control devices forbidden to user processes Invoke operating system functions not available to user processes …
Project #2, Linux Kernel Modifications CS-502 Fall In this project, we will … Add a new system call to the Linux kernel –It does nothing except announce its presence Upgrade that system call to provide information about the process –Information not readily available via existing system calls Follow Linux naming & numbering conventions
Project #2, Linux Kernel Modifications CS-502 Fall In this project, we won’t … … bother to make a library routine to encapsulate our systems calls … try to support them on all machine architectures
Project #2, Linux Kernel Modifications CS-502 Fall To get started Find kernel sources in /usr/src linux a linux linux Clone kernel source tree by linked copy cp –al /usr/src/linux a myKernel –Creates a linked copy of original tree in new directory called myKernel
Project #2, Linux Kernel Modifications CS-502 Fall Digression on Unix Hard Links Directory entries point to files Two entries may point to same file! Same or different directories Same or different name Permissions attached to file, not directory Called hard links (as opposed to symbolic links) Modifications to file seen via all hard links mv and rm commands change directories, not files! File goes away when all directory entries (i.e., hard links) to that file are deleted.
Project #2, Linux Kernel Modifications CS-502 Fall Cloned Source Tree All directories are copied All directory entries in copy are linked back to files in original source tree To modify a file:– Unlink it (via mv or rm ) Replace it with modified copy in that directory Original is preserved intact in other directory Note: Versions of Linux kernel sources are linked copies of each other
Project #2, Linux Kernel Modifications CS-502 Fall To Modify a File mv file.c file.c~ Edit file.c~ in your favorite editor Save as file.c (Eventually) delete file.c~ EMACS and patch do this automatically Most other editors require you to do it manually
Project #2, Linux Kernel Modifications CS-502 Fall Part 1: Build Linux Kernel Make configuration –As ordinary user Build source tree –As ordinary user Install –Needs root privileges
Project #2, Linux Kernel Modifications CS-502 Fall Make configuration In a command shell at top of your cloned directory, do one of:– –make config Very long and tedious –make menuconfig Still somewhat long and tedious; also hokey –make xconfig Nice –make gconfig Really nice, but our virtual machines are missing some libraries to support this
Project #2, Linux Kernel Modifications CS-502 Fall Make configuration (continued) Edit “General Setup” to name your “Local version” No other edits necessary at this time. Save and quit If need to rebuild, use make oldconfig to reuse same configuration
Project #2, Linux Kernel Modifications CS-502 Fall To Build Kernel make > make-output.txt Execute at top of your kernel tree Takes 40 minutes on csopt4 ~ one hour on 3 gigahertz Pentium Rebuilds after small edits are much faster Changing.h files can cause longer rebuilds
Project #2, Linux Kernel Modifications CS-502 Fall To Install Kernel Requires root privileges –sudo make install modules_install Puts kernel, initrd file, symbols in /boot –Changes links for vmlinuz, initrd Adds entries to /boot/grub/menu.lst –So you can select which kernel to boot Some apparent bugs in this process –Use YaST to repair manually
Project #2, Linux Kernel Modifications CS-502 Fall Running Your Kernel Restart your virtual machine Click in boot window Use arrow keys to select desired kernel or system To determine which kernel is running:– –uname -a
Project #2, Linux Kernel Modifications CS-502 Fall Creating a Patch File In kernel tree, remove all object & config files by –make distclean One level above kernel tree, do –diff –urN original myKernel > patch1 To recreate your directory from patch –cp –al original newKernel –cd newKernel –patch –p1 < patch1
Project #2, Linux Kernel Modifications CS-502 Fall End of Part 1 Questions?
Project #2, Linux Kernel Modifications CS-502 Fall Part 2: Adding a System Call See Silbershatz, pp Similar problem statement Many details are different (due to version of Linux) Clone kernel tree from Part1 Use make oldconfig before building
Project #2, Linux Kernel Modifications CS-502 Fall Linux Conventions (all versions) If your library routine is alarm, … … then the corresponding system call is sys_alarm … and the corresponding function prototype for its implementation is asmlinkage unsigned long sys_alarm (unsigned int seconds) Note that asmlinkage is a compiler directive that tells how to compile the function call in gcc
Project #2, Linux Kernel Modifications CS-502 Fall Linux Conventions (continued) To invoke alarm system call from a user or library routine, use macro _syscall1(unsigned long, alarm, unsigned int seconds) _syscalln has n+2 arguments Return type Name of actual system call (in user space) Arguments to system call function This macro defines the function unsigned long alarm(unsigned int seconds)
Project #2, Linux Kernel Modifications CS-502 Fall helloworld System Call /* This is the text of the helloworld system call */ asmlinkage int sys_helloworld(void) { printk(KERN_EMERG “Hello, world!”); return 0; } Add to the file kernel/sys.c
Project #2, Linux Kernel Modifications CS-502 Fall printk(), the Kernel Debug Print Tool Very robust May be called from (almost) anywhere in kernel Same calling convention as printf() Writes to system log Output survives crashes (almost all of the time) To read output, see /var/log/messages Needs root privileges to read Circular log, newest messages at end See Linux Kernel Development, 2 nd edition, by Robert Love, Chapter 18.
Project #2, Linux Kernel Modifications CS-502 Fall Registering your System Call include/asm-i386/unistd.h –Add entry for your call number –Increment total number of calls arch/i386/kernel/entry.S –Lists entry points for system calls –Must be kept in numerical order! –Number must correspond to entry in unistd.h Rebuild and install your kernel
Project #2, Linux Kernel Modifications CS-502 Fall Testing your System Call #include #include #include #include #define __NR_helloworld288/* or whatever you set it in unistd.h */ _syscall0(int, helloworld); main () { printf(“The return code from the helloworld system call is %d\n”, helloworld()); } Check log for the message!
Project #2, Linux Kernel Modifications CS-502 Fall Create Patch File patch2 is difference between kernel tree for Part1 and kernel tree for Part2
Project #2, Linux Kernel Modifications CS-502 Fall End of Part 2 Questions?
Project #2, Linux Kernel Modifications CS-502 Fall Part 3: Get Process Information Modify you kernel of Part 2 to add system call to get information about process System call is –int getprinfo(struct prinfo *info) –info is pointer to area to store results –Returns zero if successful, error code if not See handout for definition of struct prinfo
Project #2, Linux Kernel Modifications CS-502 Fall Information needed for prinfo See task_struct in include/linux/sched.h See getuid and getpid for examples of simple system calls See include/asm/current.h to find current process information Use copy_to_user to safely copy data from kernel to user space Return EFAULT error code if info argument is not valid pointer in user space
Project #2, Linux Kernel Modifications CS-502 Fall copy_to_user and copy_from_user Functions to safely copy data to/from user space Check validity of pointer arguments Return zero if successful, number of bytes that fail if there is a problem Immune to page faults, pre-emption, etc.
Project #2, Linux Kernel Modifications CS-502 Fall Implementing getprinfo System Call Replace helloworld system call from Part 2 Implement in kernel/timer.c Register in unistd.h and entry.S Use printk() to print debugging statements to system log
Project #2, Linux Kernel Modifications CS-502 Fall Testing getprinfo Write test program in user space Run multiple times from same shell, different shell Note differences in results Compare with what you can find about processes from ps command
Project #2, Linux Kernel Modifications CS-502 Fall Create patch3 Patch3 is difference between Part 2 & Part 3.
Project #2, Linux Kernel Modifications CS-502 Fall Submission Submit using web-based turnin program – Include –patch1, patch2, and patch3 –Write up explaining results of testing Part 3 –Starting point for your kernel tree –Put your name on all documents and at top of every edited file!
Project #2, Linux Kernel Modifications CS-502 Fall Due Dates This project is due at start of class on Monday, October 16. Pace yourself:– –Part 1 should be complete by October 2 –Part 2 should be complete by October 9 –Part 3 should be complete by October 16 Report to instructor any difficulties
Project #2, Linux Kernel Modifications CS-502 Fall Questions?