Project #2, Linux Kernel Modifications CS-502 Fall 20061 Programming Project #2 Linux Kernel Hacking CS-502 Operating Systems Fall 2006.

Slides:



Advertisements
Similar presentations
Memory Protection: Kernel and User Address Spaces  Background  Address binding  How memory protection is achieved.
Advertisements

Utilizing the GDB debugger to analyze programs Background and application.
Project 2 -- Linux Kernel Hacking CS-502 (EMC) Fall Project 2 Linux Kernel Hacking CS-502, Operating Systems Fall 2009 (EMC)
Project 0, Linux Dabbling CS-3013 A-term Project 0: Linux & Virtual Machine Dabbling CS-3013, Operating Systems.
Project 0: Linux & VM Dabbling CS-3013, C-term Project 0: Linux & Virtual Machine Dabbling CS-3013, Operating Systems.
OS Spring’03 Introduction Operating Systems Spring 2003.
Process in Unix, Linux and Windows CS-3013 C-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
CS-502 Fall 2006Processes in Unix, Linux, & Windows 1 Processes in Unix, Linux, and Windows CS502 Operating Systems.
Project 0: Linux & VM Dabbling CS-502, Fall Project 0: Linux & Virtual Machine Dabbling CS-502, Operating Systems.
Project #2 -- Kernel Hacking CS-3013 A-term Programming Project #2 Linux Kernel Hacking CS-3013, Operating Systems C-Term 2008 Due Monday, September.
Project 3, Message Passing System CS-502 Fall Programming Project #3 Message Passing System CS-502 Operating Systems Fall 2006.
Project 0 -- Linux Dabbling CS-502 (EMC) Fall Project 0 Linux & Virtual Machine Dabbling CS-502, Operating Systems Fall 2009 (EMC)
OS Spring’04 Introduction Operating Systems Spring 2004.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 2: Operating-System Structures Modified from the text book.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Project #1, Linux Kernel Modifications CS-502 Fall Programming Project #1 Linux Kernel Hacking CS-502, Operating Systems Fall 2007.
Project 0 -- Linux Dabbling CS-3013 A-term Project 0 Linux & Virtual Machine Dabbling CS-3013, Operating Systems.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
CSE 451: Operating Systems Section 2 Interrupts, Syscalls, Virtual Machines, and Project 1.
1 SEEM3460 Tutorial Unix Introduction. 2 Introduction What is Unix? An operation system (OS), similar to Windows, MacOS X Why learn Unix? Greatest Software.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
System Calls 1.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
Chapter 1. Introduction What is an Operating System? Mainframe Systems
OS provide a user-friendly environment and manage resources of the computer system. Operating systems manage: –Processes –Memory –Storage –I/O subsystem.
Chapter 2: Operating-System Structures. 2.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 2: Operating-System Structures Operating.
Memory Management 3 Tanenbaum Ch. 3 Silberschatz Ch. 8,9.
UNIX Commands. Why UNIX Commands Are Noninteractive Command may take input from the output of another command (filters). May be scheduled to run at specific.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 2: Operating-System Structures.
Virtual Memory Expanding Memory Multiple Concurrent Processes.
1 CSE 451 Section 2: Interrupts, Syscalls, Virtual Machines, and Project 1.
CE Operating Systems Lecture 3 Overview of OS functions and structure.
Implementing System Calls CS552 Kartik Gopalan. CS552/BU/Spring2008 Steps in writing a system call 1.Create an entry for the system call in the kernel’s.
System Calls. The Linux we use is: Linux-Mandrake 7.0. In this project, you are going to change some kernel files and recompile the kernel. After you.
LOGO System Call. Introduction System call is the mechanism used by an application program to request service from the OS. Users use it to communicate.
We will focus on operating system concepts What does it do? How is it implemented? Apply to Windows, Linux, Unix, Solaris, Mac OS X. Will discuss differences.
CS 6560 Operating System Design Lecture 5: System Calls Interrupts.
Manage Directories and Files in Linux Part 2. 2 Identify File Types in the Linux System The file types in Linux referred to as normal files and directories.
OS Project 0 February 25, Outline  Linux Installation  Linux Kernel Compilation  System Call Development  Kernel Modules / 452.
Lecture 02 File and File system. Topics Describe the layout of a Linux file system Display and set paths Describe the most important files, including.
Lecture 5 Rootkits Hoglund/Butler (Chapters 1-3).
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
Embedded Software Design Week II Linux Intro Linux Kernel.
1 COMP 3500 Introduction to Operating Systems Project 4 – Processes and System Calls Part 3: Adding System Calls to OS/161 Dr. Xiao Qin Auburn University.
Operating System Kernel Compilation
Introduction to Operating Systems
OS Homework 1 February 22, 2017.
Protection and OS Structure
Chapter 2: System Structures
Processes in Unix, Linux, and Windows
Project 0: Linux and Virtual Machine Dabbling
Operating System Kernel Compilation
Processes in Unix, Linux, and Windows
Operation System Program 4
Chapter 2: Operating-System Structures
Introduction to Operating Systems
SAPC Hardware Pentium CPU (or 486) 4M usable memory
Project 0: Linux & Virtual Machine Dabbling
Processes in Unix, Linux, and Windows
Project 2 Linux Kernel Hacking
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Operating Systems Lecture 3.
Project 2 Linux Kernel Hacking
How & When The Kernel Runs
Processes in Unix, Linux, and Windows
Processes in Unix and Windows
Programming Project #2 Linux Kernel Hacking
Implementing System Calls
Presentation transcript:

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?