Assignment 5: Adding and testing a new system call to Linux kernel



Similar documents
CSI 402 Lecture 13 (Unix Process Related System Calls) 13 1 / 17

Migration of Process Credentials

Minix Mini Unix (Minix) basically, a UNIX - compatible operating system. Minix is small in size, with microkernel-based design. Minix has been kept

Project Adding a System Call to the Linux Kernel

W4118 Operating Systems. Junfeng Yang

How do Users and Processes interact with the Operating System? Services for Processes. OS Structure with Services. Services for the OS Itself

Computer Systems II. Unix system calls. fork( ) wait( ) exit( ) How To Create New Processes? Creating and Executing Processes

Linux Kernel Architecture

EXPLORING LINUX KERNEL: THE EASY WAY!

Linux Kernel Rootkit : Virtual Terminal Key Logger

How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

User-level processes (clients) request services from the kernel (server) via special protected procedure calls

CS 377: Operating Systems. Outline. A review of what you ve learned, and how it applies to a real operating system. Lecture 25 - Linux Case Study

Linux Driver Devices. Why, When, Which, How?

Operating System Structure

Illustration 1: Diagram of program function and data flow

Operating System Structures

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

An Implementation Of Multiprocessor Linux

Virtualization in Linux KVM + QEMU

Virtual Private Systems for FreeBSD

The Linux Kernel: Process Management. CS591 (Spring 2001)

Virtual Servers. Virtual machines. Virtualization. Design of IBM s VM. Virtual machine systems can give everyone the OS (and hardware) that they want.

LSN 10 Linux Overview

PetaLinux SDK User Guide. Application Development Guide

CS161: Operating Systems

Project No. 2: Process Scheduling in Linux Submission due: April 28, 2014, 11:59pm

Chapter 2 System Structures

Research and Design of Universal and Open Software Development Platform for Digital Home

Linux Kernel Networking. Raoul Rivas

CSE 265: System and Network Administration. CSE 265: System and Network Administration

Professional Xen Visualization

Operating System Organization. Purpose of an OS

Lab 4: Socket Programming: netcat part

Using SNMP with Content Gateway (not V-Series)

CPS221 Lecture: Operating System Structure; Virtual Machines

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Lecture 25 Systems Programming Process Control

ELEC 377. Operating Systems. Week 1 Class 3

CSE 265: System and Network Administration

KVM: Kernel-based Virtualization Driver

Review from last time. CS 537 Lecture 3 OS Structure. OS structure. What you should learn from this lecture

Unix::Statgrab - System Monitoring

Exploiting Trustzone on Android

Abstract. Microsoft Corporation Published: August 2009

Example of Standard API

CSE 265: System and Network Administration. CSE 265: System and Network Administration

Traditional Rootkits Lrk4 & KNARK

Priority Based Implementation in Pintos

sys socketcall: Network systems calls on Linux

CS 103 Lab Linux and Virtual Machines

Twitter and Notifications of Linux Server Events

Intel P6 Systemprogrammering 2007 Föreläsning 5 P6/Linux Memory System

Virtualization System Vulnerability Discovery Framework. Speaker: Qinghao Tang Title:360 Marvel Team Leader

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

CSE 120 Principles of Operating Systems. Modules, Interfaces, Structure

For a 64-bit system. I - Presentation Of The Shellcode

Performance Monitor on PowerQUICC II Pro Processors

OS Observability Tools

System Administration

x86 ISA Modifications to support Virtual Machines

REAL TIME OPERATING SYSTEM PROGRAMMING-II: II: Windows CE, OSEK and Real time Linux. Lesson-12: Real Time Linux

Lecture 5. User-Mode Linux. Jeff Dike. November 7, Operating Systems Practical. OSP Lecture 5, UML 1/33

Linux System Administration on Red Hat

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Data Types in the Kernel

Jorix kernel: real-time scheduling

This presentation explains how to monitor memory consumption of DataStage processes during run time.

The System Monitor Handbook. Chris Schlaeger John Tapsell Chris Schlaeger Tobias Koenig

Keil C51 Cross Compiler

Linux Distributed Security Module 1

Linux + Windows 95 mini HOWTO

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Operating System Engineering: Fall 2005

1 Posix API vs Windows API

OS: IPC I. Cooperating Processes. CIT 595 Spring Message Passing vs. Shared Memory. Message Passing: Unix Pipes

Buildroot for Vortex86EX (2016/04/20)

Frysk The Systems Monitoring and Debugging Tool. Andrew Cagney

SheevaPlug Development Kit README Rev. 1.2

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Outline. Outline. Why virtualization? Why not virtualize? Today s data center. Cloud computing. Virtual resource pool

Embedded Software Development

NDK: NOVELL NSS AUDIT

VMware Server 2.0 Essentials. Virtualization Deployment and Management

Developing Embedded Linux Devices Using the Yocto Project

Audit Trail Administration

Chapter 3: Operating-System Structures. Common System Components

"EZHACK" POPULAR SMART TV DONGLE REMOTE CODE EXECUTION

Oak Ridge National Laboratory Computing and Computational Sciences Directorate. Lustre Crash Dumps And Log Files

1 Abstract Data Types Information Hiding

Operating Systems. 05. Threads. Paul Krzyzanowski. Rutgers University. Spring 2015

System administration basics

Virtualization analysis

IT304 Experiment 2 To understand the concept of IPC, Pipes, Signals, Multi-Threading and Multiprocessing in the context of networking.

Five standard procedures for building the android system. Figure1. Procedures for building android embedded systems

Abstract. Microsoft Corporation Published: November 2011

Transcription:

Assignment 5: Adding and testing a new system call to Linux kernel Antonis Papadogiannakis HY345 Operating Systems Course 1

Outline Introduction: system call and Linux kernel Emulators and Virtual Machines (demo with QEMU) Compile Linux kernel 2.6 (demo with linux-2.6.38.1) Load a new kernel with QEMU (demo) Basic steps to add a new system call to Linux kernel (example) How to use the new system call (example) The new system call in this assingment: getproctimes Several hints 2

System call System call: an interface between a user-level program and a service provided by kernel Implemented in kernel With a user-level interface Crossing the user-space/kernel-space boundaries Trap: switch to kernel mode e.g. when calling a system call 3

Popular Open source www.kernel.org Linux kernel Extending the Linux kernel Usually with loadable kernel modules Architecture: monolithic kernel A set of system calls implement all Operating System services User space, kernel space boundaries Preemptive scheduling, virtual memory 4

Emulators Enable us to emulate an Operating System (guest OS) using another Operating System (host OS) e.g. running Windows from a Linux OS or running multiple OS in a single computer as a simple user, in user-level Guess OS can crash without affecting host OS thus very useful for kernel development and debugging 5

The QEMU emulator Fast open source emulator You will use it in this assignment www.qemu.org Installed in CSD machines $ qemu hda disk.img Virtual disk image (disk.img) Like a common disk We can install an OS distribution into this image hy345-linux.img is the disk image you will use in this assingment, with a minimal Linux installation and kernel 2.6.38.1 Host OS: a CSD machine Quest OS: ttylinux 6

Demo with QEMU 7

Linux kernel 2.6.38.1 Get the code from ~hy345/qemu-linux/linux- 2.6.38.1.tar.bz2 or from www.kernel.org View source Organized in kernel, mm, drivers, etc We are mostly interested in files in kernel folder Headers are in the include folder x86 32-bit architecture (i386) Use grep, find, ctags 8

Compile the Linux kernel 2 steps Configure make config, make menuconfig, etc produce.config file We give you directly the proper.config file, so no need for configuring kernel Build $ make ARCH=i386 bzimage Builds Linux kernel image for i386 architecture linux-2.6.38.1/arch/x86/boot/bzimage bzimage used to boot with QEMU with the new kernel (we do not consider install in this assignment due to emulator-based testing) 9

Demo with linux-2.6.38.1 kernel source code and compilation 10

Load the new kernel with QEMU $ qemu hda hy345-linux.img append root=/dev/hda kernel linux-2.6.38.1/arch/x86/boot/bzimage Use the same disk image hy345-linux.img as /dev/hda This image contains the root filesystem Load OS with the new kernel image $ uname a To find the kernel version Append your username in the kernel version and use revision numbers for your convenience Compile Linux kernel in host OS, boot with the new kernel in guest OS 11

Demo Loading QEMU with a new kernel image built in the host OS 12

Control flow of a system call in Linux kernel 13

System call table 14

Three basic steps to add a new system call in Linux kernel 1. Add a new system call number N 2. Add a new system call table entry for the above system call number N with a function pointer to function F 3. Implement the function F with system call s actual functionality. Also add proper header files for new types Copy arguments from user space to kernel and results from kernel to user space 15

An example: dummy_sys The dummy_sys system call takes one integer as single argument It prints this argument in kernel and returns this integer multiplied by two 16

Step 1: Add new system call number Open linux-2.6.38.1/arch/x86/include/asm/unistd_32.h Find system call numbers Find last system call number (340) Define a new one with the next number (341) #define NR_dummy_sys 341 Increase NR_syscalls by one (341 -> 342) dummy_sys has the 341 system call number 17

Step 2: Add new entry to system call table Open linux-2.6.38.1/arch/x86/kernel/syscall_table_32.s Add in the last line the name of the function that implements the dummy_sys system call.long sys_dummy_sys /* 341 */ sys_dummy_sys function will implement the dummy_sys system call 18

Step 3: Implement the system call s function Create linux-source-2.6.38.1/kernel/dummy_sys.c Write system call s functionality #include <linux/kernel.h> #include <asm/uaccess.h> #include <linux/syscalls.h> asmlinkage long sys_dummy_sys(int arg0) { printk("called dummy_sys with argument: %d\n",arg0); return((long)arg0*2); } 19

Arguments by reference Strings, pointers to structures, etc int access_ok( type, address, size ); unsigned long copy_from_user( void *to, const void user *from, unsigned long n ); unsigned long copy_to_user( void *to, const void user *from, unsigned long n ); 20

Using the new system call #include <stdio.h> #include <unistd.h> #include <errno.h> #define NR_dummy_sys 341 int main() { } printf("trap to kernel level\n"); syscall( NR_dummy_sys, 42); //you should check return value for errors printf("back to user level\n"); 21

Wrapper function Define a macro #define dummy_sys(arg1) syscall(341, arg1) Write a wrapper function long dummy_sys(int arg1) { syscall(341, arg1); } So in the test program we just call dummy_sys(42); 22

The getproctimes system call int getproctimes(int pid, struct proctimes *pt); First argument: pid of a process, or the current process if (pid==-1) Second argument: passed by referenced and used by kernel to return the necessary info to user space Return value: EINVAL on error or 0 on success 23

The struct proctimes Should be defined in a new file: linux-2.6.38.1/include/proctimes.h struct proctimes { //info and times about processes we need struct proc_time proc; //process with pid or current process struct proc_time parent_proc; //parent process } struct proc_time oldest_child_proc; //oldest child process struct proc_time oldest_sibling_proc; //oldest sibling process 24

The struct proc_time Also defined in linux-2.6.38.1/include/proctimes.h struct proc_time { //info and times about a single process pid_t pid; //pid of the process } char name[16]; //file name of the program executed unsigned long start_time; //start time of the process unsigned long real_time; //real time of the process execution unsigned long user_time; //user time of the process unsigned long sys_time; //system time of the process 25

In every execution of getproctimes Every time the getproctimes is executed in kernel you should print a message Using printk The message will include your full name and A.M. You can view these messages from user level upon the execution of getproctimes with dmesg or cat /var/log/messages printk is very useful for debugging messages 26

Testing getproctimes You should write several test programs in the guest OS using getproctimes To validate its correct operation We require three test programs 1. Get the times of the current process with getproctimes when it performs 1M multiplications and sleep(5) 2. Compare info and times of all processes when calling multiple fork() 3. Get pid from command line and call getproctimes with this pid, and use pids from ps Any other test program you think useful 27

Hints To calculate real time you shoud read current time see linux-2.6.38.1/include/linux/time.h and gettimeofday system call To find the current process see linux-2.6.38.1/include/asm/current.h To find info and times about each process see task_struct in linux-2.6.38.1/include/linux/sched.h In tast_struct you can also find the parent process child processes list (and find the oldest) sibling processes list (and find the oldest) See existing system calls: getpid, gettimeofday, times 28

What to submit 1. bzimage 2. Only modified or created by you source files of the Linux kernel 2.6.38.1 (both C and header files) 3. Test programs and header files used in the guest OS for testing the new system call 4. README with implementation details and experiences from testing Gather these files in a single directory and send them using the submit program as usual 29

Good luck Deadline: 14/12 30