Project #2 -- Kernel Hacking CS-3013 A-term 20081 Programming Project #2 Linux Kernel Hacking CS-3013, Operating Systems C-Term 2008 Due Monday, September.

Slides:



Advertisements
Similar presentations
Project 2 -- Linux Kernel Hacking CS-502 (EMC) Fall Project 2 Linux Kernel Hacking CS-502, Operating Systems Fall 2009 (EMC)
Advertisements

CMPT 300: Operating Systems I Dr. Mohamed Hefeeda
Project 0, Linux Dabbling CS-3013 A-term Project 0: Linux & Virtual Machine Dabbling CS-3013, Operating Systems.
CS-502 Fall 2006Project 1, Fork1 Programming Project 1 – Fork.
Project 1CS-3013 A-term Programming Project #1 Forking Processes Due Tuesday, September 8, 11:59 PM.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
Project 0: Linux & VM Dabbling CS-3013, C-term Project 0: Linux & Virtual Machine Dabbling CS-3013, Operating Systems.
Project #2, Linux Kernel Modifications CS-502 Fall Programming Project #2 Linux Kernel Hacking CS-502 Operating Systems Fall 2006.
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.
Project 0: Linux & VM Dabbling CS-502, Fall Project 0: Linux & Virtual Machine Dabbling CS-502, Operating Systems.
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.
IntroductionCS-3013 C-term Programming Project #1 Forking Processes Due Thursday, January 24, 6:00 PM.
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.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
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.
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.
Protection and the Kernel: Mode, Space, and Context.
Process in Unix, Linux, and Windows CS-3013 A-term Processes in Unix, Linux, and Windows CS-3013 Operating Systems (Slides include materials from.
OPERATING SYSTEM OVERVIEW. Contents Basic hardware elements.
Chapter 2: Operating-System Structures. 2.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 2: Operating-System Structures Operating.
Project 1, Command Shell CS-502 (EMC) Fall Programming Project #1 Command Shell CS-502, Operating Systems EMC, Fall 2009 (Slides include materials.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 2: Operating-System Structures.
1 CSE 451 Section 2: Interrupts, Syscalls, Virtual Machines, and Project 1.
CE Operating Systems Lecture 3 Overview of OS functions and structure.
Operating Systems Lecture November 2015© Copyright Virtual University of Pakistan 2 Agenda for Today Review of previous lecture Hardware (I/O, memory,
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
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.
CSE 451: Operating Systems Section 3: Project 0 recap, Project 1.
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.
Lecture 5 Rootkits Hoglund/Butler (Chapters 1-3).
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.
1 Chapter 2: Operating-System Structures Services Interface provided to users & programmers –System calls (programmer access) –User level access to system.
Of Privilege, Traps, Interrupts & Exceptions Prof. Sirer CS 316 Cornell University.
Introduction to C Topics Compilation Using the gcc Compiler
CS 3214 Computer Systems Lecture 9 Godmar Back.
Introduction to Operating Systems
Protection and OS Structure
How & When The Kernel Runs
Protection of System Resources
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
CS 3305 System Calls Lecture 7.
Introduction to C Topics Compilation Using the gcc Compiler
Processes in Unix, Linux, and Windows
Project 0: Linux and Virtual Machine Dabbling
Processes in Unix, Linux, and Windows
Introduction to Operating Systems
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.
Programming Project #1 Command Shell
Programming Project #1 Fork and Command Shell
Operating Systems Lecture 3.
Project 2 Linux Kernel Hacking
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
How & When The Kernel Runs
Processes in Unix, Linux, and Windows
Programming Project #2 Linux Kernel Hacking
Implementing System Calls
Presentation transcript:

Project #2 -- Kernel Hacking CS-3013 A-term Programming Project #2 Linux Kernel Hacking CS-3013, Operating Systems C-Term 2008 Due Monday, September 15, 6:00 PM

Project #2 -- Kernel Hacking CS-3013 A-term 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 -- Kernel Hacking CS-3013 A-term Method To add a new system call to the Linux kernel To get useful information from the data structures of a Linux kernel

Project #2 -- Kernel Hacking CS-3013 A-term 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 from user to kernel –return from system call resets mode to user

Project #2 -- Kernel Hacking CS-3013 A-term Transition from User to Kernel Mode Note: each different system call has its own number or other identity. Kernel trap handler uses syscall number to index into table of syscall routines

Project #2 -- Kernel Hacking CS-3013 A-term Inside Kernel, the OS can … Read and modify data structures not in user address space Control devices and hardware settings forbidden to user processes Invoke operating system functions not available to user processes …

Project #2 -- Kernel Hacking CS-3013 A-term Accessing the Kernel via System Call Normally embedded within a library routine User API never makes 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 -- Kernel Hacking CS-3013 A-term Accessing Kernel via Library interface

Project #2 -- Kernel Hacking CS-3013 A-term Accessing Kernel via Library interface

Project #2 -- Kernel Hacking CS-3013 A-term In this project, we will … Add a new system call to the Linux kernel –It does nothing except announce its presence Add a second system call to provide information about the calling process –Some of which is not readily available via existing system calls Follow Linux naming & numbering conventions

Project #2 -- Kernel Hacking CS-3013 A-term In this project, we won’t … … bother to make a library to encapsulate our systems calls … try to support them on all machine architectures

Project #2 -- Kernel Hacking CS-3013 A-term Part 1: Adding a System Call See Robert Love, Linux Kernel Development, ch. 5 System Calls Many how-to details, but some differences Clone a new kernel tree as in Project 0 cp –al /usr/src/linux kernelSrc Remember to build to a destination – O=~/kernelDst Note: need to clean up disk space in virtual machine Start with new clone; or Use YaST to remove boot files from previous projects

Project #2 -- Kernel Hacking CS-3013 A-term 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 kernel implementation is asmlinkage unsigned long sys_alarm (unsigned int seconds) Note that asmlinkage is a compiler directive that tells gcc how to compile calls to the function sys_alarm within the kernel

Project #2 -- Kernel Hacking CS-3013 A-term Robert Love says … To invoke alarm system call from a library routine in user space, 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 -- Kernel Hacking CS-3013 A-term Robert Love says … To invoke alarm system call from a library routine in user space, 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 -- Kernel Hacking CS-3013 A-term Linux Conventions (modified) _syscalln is deprecated xxx Instead, use syscall(callNumber, …), where … are the arguments to the system call. Result must be cast to appropriate type Example, for alarm system call, write long alarm (unsigned int seconds) { return (long) syscall(__NR_alarm, seconds); };

Project #2 -- Kernel Hacking CS-3013 A-term Hello, World! Our first system call will be helloworld No arguments Return long

Project #2 -- Kernel Hacking CS-3013 A-term helloworld System Call /* This is the text of the helloworld system call implementation */ asmlinkage long sys_helloworld(void) { printk(KERN_EMERG “Hello, world!\n”); return 0; } Add to the file kernelSrc/kernel/sys.c

Project #2 -- Kernel Hacking CS-3013 A-term helloworld System Call /* This is the text of the helloworld system call */ asmlinkage long sys_helloworld(void) { printk(KERN_EMERG “Hello, world!\n”); return 0; } Add to the file kernelSrc/kernel/sys.c Note: No comma here!

Project #2 -- Kernel Hacking CS-3013 A-term 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 — Circular log, newest messages at end Use /bin/dmesg or cat /proc/kmsg to read or go to YaST > Miscellaneous > System Log Needs root privileges to read See Linux Kernel Development, 2 nd edition, by Robert Love, Chapter 18.

Project #2 -- Kernel Hacking CS-3013 A-term helloworld System Call /* This is the text of the helloworld system call implementation */ asmlinkage long sys_helloworld(void) { printk(KERN_EMERG “Hello, world!\n”); return 0; } Add to the file kernelSrc/kernel/sys.c

Project #2 -- Kernel Hacking CS-3013 A-term Registering your System Call include/asm-i386/unistd.h –Add entry for your call number –Increment total number of calls arch/i386/kernel/syscall_table.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 -- Kernel Hacking CS-3013 A-term Note #1 On i386 architecture, the syscall table has moved since Robert Love’s book Previous courses It used to be in –arch/i386/kernel/entry.S But now it is in –arch/i386/kernel/syscall_table.S –… which is included by entry.S

Project #2 -- Kernel Hacking CS-3013 A-term Note #2 The x86_64 architecture does it differently –Everything is in include/asm-x86_64/unistd.h –Add to the list #define251 /*next number in list*/ __SYSCALL(__NR_helloworld, sys_helloworld) No need to edit entry.S

Project #2 -- Kernel Hacking CS-3013 A-term Note #3 Remember: – to edit a source file foo.h in your kernel tree –Move it to foo.h~ –Make changes and save to foo.h

Project #2 -- Kernel Hacking CS-3013 A-term Testing your System Call In user space:– #include #include #include #include #define __NR_helloworld288/* or whatever you set it in kernel unistd.h */ long helloworld(void) { return (long) syscall(__NR_helloworld); }; main () { printf(“The return code from the helloworld system call is %d\n”, helloworld()); } Check log for the printk() message!

Project #2 -- Kernel Hacking CS-3013 A-term Creating a Patch File One level above kernel source tree, do diff –urN /usr/src/linux kernelSrc > patch1 To recreate your directory from patch –cp –al /usr/src/linux newSrc –cd newSrc –patch –p1 < patch1 Do not prefix name of kernelSrc directory or use fully qualified name –E.g, ~/kernelSrc,./kernelSrc

Project #2 -- Kernel Hacking CS-3013 A-term Submission – Part 1 Patch1 Test program Makefile and write-up will be combined with part 2

Project #2 -- Kernel Hacking CS-3013 A-term End of Part 1 Questions?

Project #2 -- Kernel Hacking CS-3013 A-term Part 2: Get Process Information Modify your kernel of Part 1 to add another system call to get information about process Please leave helloworld system call in place! System call is –long getprinfo(struct prinfo *info) –info is pointer to caller area to receive results In user-space! –Returns zero if successful, error code if not See handout for definition of struct prinfo

Project #2 -- Kernel Hacking CS-3013 A-term 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 I.e., to get current task_struct Use copy_to_user to safely copy data from kernel to user space (next slide) Return EFAULT error code if info argument is not valid pointer in user space

Project #2 -- Kernel Hacking CS-3013 A-term copy_to_user and copy_from_user Functions to safely copy data to/from user space Check validity of user-space pointer arguments Return zero if successful, number of bytes that fail if there is a problem Immune to page faults, pre-emption, null pointers, other errors, etc.

Project #2 -- Kernel Hacking CS-3013 A-term Implementing getprinfo System Call Add after helloworld system call from Part 1 Create and implement –Interface include/linux/prinfo.h –kernel/prinfo.c, with Makefile edits Register in unistd.h in syscall_table.S Use printk() to print debugging statements to system log –For your debugging convenience

Project #2 -- Kernel Hacking CS-3013 A-term Testing getprinfo Write test program in user space Must have own user space version of prinfo.h Run multiple times from same shell, different shell, different processes Note differences in results Compare with what you can find about processes from ps command and from Project 1 program.

Project #2 -- Kernel Hacking CS-3013 A-term Submission – Part 2 Patch2 –Difference between original source tree and Part 2 kernel. –Includes patch lines from Part 1 User-space test program –Include file(s) –Test program itself –Makefile for both Part 1 and Part 2 Short writeup with results of both parts Submit using web-based turnin program –

Project #2 -- Kernel Hacking CS-3013 A-term Warning! Check your patch files before submitting Should be a few kilobytes Every line added by patch file should be something you wrote Be sure no junk is lying around in your source tree E.g., “~” files, old files, build files If your patch file is 100s of kilobytes or megabytes, it is wrong! Graders will refuse to grade your project!

Project #2 -- Kernel Hacking CS-3013 A-term Submission (continued) Put your name on all documents and at top of every edited file!

Project #2 -- Kernel Hacking CS-3013 A-term Due Date Project due on Monday, September 8, at 6:00 PM Pace yourself:– –Part 1 should be very quick –Part 2 may take you all week Report to instructor or TAs any difficulties

Project #2 -- Kernel Hacking CS-3013 A-term Questions?