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.

Slides:



Advertisements
Similar presentations
Device Drivers. Linux Device Drivers Linux supports three types of hardware device: character, block and network –character devices: R/W without buffering.
Advertisements

CS Spring 2014 Prelim 2 Review
Memory Protection: Kernel and User Address Spaces  Background  Address binding  How memory protection is achieved.
Computer System Organization Computer-system operation – One or more CPUs, device controllers connect through common bus providing access to shared memory.
Linking & Loading CS-502 Operating Systems
Computer Organization CS224 Fall 2012 Lesson 12. Synchronization  Two processors or threads sharing an area of memory l P1 writes, then P2 reads l Data.
CSC 501 Lecture 2: Processes. Von Neumann Model Both program and data reside in memory Execution stages in CPU: Fetch instruction Decode instruction Execute.
The ‘system-call’ ID-numbers How can Linux applications written in assembly language create and access files?
40 Advanced Operating Systems Implementing System Calls.
Computer System Structures memory memory controller disk controller disk controller printer controller printer controller tape-drive controller tape-drive.
Add a New System Call to Linux. Hw1 Add a New System Call to Linux and Compile Kernel Add a New System Call to Linux by Kernel Module.
CSE 451 Section 4 Project 2 Design Considerations.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Process. Process Concept Process – a program in execution Textbook uses the terms job and process almost interchangeably A process includes: – program.
I/O Tanenbaum, ch. 5 p. 329 – 427 Silberschatz, ch. 13 p
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
General System Architecture and I/O.  I/O devices and the CPU can execute concurrently.  Each device controller is in charge of a particular device.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
System Calls 1.
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
CHAPTER 2: COMPUTER-SYSTEM STRUCTURES Computer system operation Computer system operation I/O structure I/O structure Storage structure Storage structure.
1 CSE Department MAITSandeep Tayal Computer-System Structures Computer System Operation I/O Structure Storage Structure Storage Hierarchy Hardware Protection.
2: Computer-System Structures
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
Chapter 1 Process and Thread. 1.2 process The address space of a program – Text – Code – Stack – Heap A set of registers – PC – SP Other resources – Files.
System Calls: A Kernel Project Hunter Bell CS Fall
Virtual Memory Expanding Memory Multiple Concurrent Processes.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /29/2013 Lecture 13: Compile-Link-Load Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE.
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.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
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.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 2 Computer-System Structures Slide 1 Chapter 2 Computer-System Structures.
Silberschatz, Galvin and Gagne  Applied Operating System Concepts Chapter 2: Computer-System Structures Computer System Architecture and Operation.
OS Project 0 February 25, Outline  Linux Installation  Linux Kernel Compilation  System Call Development  Kernel Modules / 452.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
Bootable Programs Building an O/S. Basic Requirements of any O/S Respond to interrupts (all kinds) Preserve user environment Protect users and self from.
Kernel Exercise 5 Brandon Cline Stuart Fischer Rachel Langhans Brian Minter Adam Stasio.
Chapter 5. System Calls. Overview The kernel provides a set of interfaces  By which processes running in user-space can interact with the system  Give.
COMP091 – Operating Systems 1 Memory Management. Memory Management Terms Physical address –Actual address as seen by memory unit Logical address –Address.
CSNB334 Advanced Operating Systems 3. Kernel Structure and Organization Lecturer: Abdul Rahim Ahmad.
Lecture 7 Interrupt ,Trap and System Call
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
Add a New System Call to Linux
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Computer Architecture & Operations I
Protection and OS Structure
Protection of System Resources
The University of Adelaide, School of Computer Science
Linking & Loading.
CS 3305 System Calls Lecture 7.
CS-3013 Operating Systems C-term 2008
More examples How many processes does this piece of code create?
Module IV Memory Organization.
Computer-System Architecture
Module 2: Computer-System Structures
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Linking & Loading CS-502 Operating Systems
Operating Systems Lecture 3.
Process Control B.Ramamurthy 2/22/2019 B.Ramamurthy.
Unix Process Control B.Ramamurthy 4/11/2019 B.Ramamurthy.
Linking & Loading CS-502 Operating Systems
Implementing System Calls
Module 2: Computer-System Structures
Module 2: Computer-System Structures
Presentation transcript:

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 leave, you should restore the system to the state before you come. To do that, make copies of the original kernel files you want to change. After you are done, restore these files to their original state. When you do the project, you can discuss it with other students. But you must do it by yourself.

The System Call Linkage A user-space program calls a system call stub, which contains a trap instruction. As a result, the CPU switches to supervisor mode and begins to execute at a specified location in kernel space. In the i386 hardware, the trap instruction actually causes interrupt 0x80 to occur, with the ISR address pointing at the entry point of the system_call() assembler routine (see /usr/src/linux/arch/i386/kernel/entry.S).

/usr/src/linux/arch/i386/kernel/entry.S.data ENTRY(sys_call_table).long SYMBOL_NAME(sys_ni_call) /*0*/.long SYMBOL_NAME(sys_exit).long SYMBOL_NAME(sys_fork).long SYMBOL_NAME(sys_read).long SYMBOL_NAME(sys_write).long SYMBOL_NAME(sys_open) ….long SYMBOL_NAME(sys_signalstack).long SYMBOL_NAME(sys_sendfile).long SYMBOL_NAME(sys_ni_call).long SYMBOL_NAME(sys_vfork)

Defining the System Call Number System calls are defined in sys_call_table. Thus when you add a new system call, you need to add an entry to the table. You do this by editing the table in the /usr/src/linux/arch/i386/kernel/entry.S file, as follows..data ENTRY(sys_call_table).long SYMBOL_NAME(sys_ni_call) /*0*/.long SYMBOL_NAME(sys_exit) ….long SYMBOL_NAME(sys_ni_call) /*190*/.long SYMBOL_NAME(sys_my_new_call) /* 191 */.endr

This editing allows a trap (interrupt 0x80) with an argument of 191 to invoke a new kernel function, sys_my_new_call(). You need superuser (su) permission to put the new version into kernel. The new system call can be invoked by using the system call syscall(). Syscall() takes the system call table entry number and arguments as parameters and then traps to the kernel.

To generate a system call stub so that an ordinary C function call will invoke the new system call, you also need to edit the /usr/src/linux/include/asm/unistd.h file, as follows, so that it can be used. #define __NR_exit 1 #define __NR_fork 2 #define __NR_read 3 #define __NR_write 4 #define __NR_open 5.. #define __NR_vfork 190 #define __NR_my_new_call 191 Finally, you need to generate the system call stub. These constant definitions are used to create the system call stub function for use with C programs for no arguments, one argument, and so on.

Generating a System Call Stub The system call stub is generated by using a macro call from a user-space program. Macros are available for generating a stub with zero to five parameters. For example, the macro for generating a stub with two arguments has the form _syscall2(type, name, type1, arg1, type2, arg2); In this macro, type is the type of the return value of the system call stub, name is the name of the stub, type1 is the type of the first argument, arg1, and type2 is the type of the second argument, arg2. These macros are defined in /usr/src/linux/include/linux/unistd.h (which includes the file /usr/src/linux/include/asm/unistd.h).

You can generate the stub by making the following macro call in your user program. #include … /* Generate system call for int foo(char *baz, double bar) */ _syscall2(int, foo, char *, baz, double, bar); … Also, a system function, system_call(), defined in arch/i386/entry.S, can be used to invoke a kernel function (without generating a stub). For example, if the index in the sys_call_table for foo() is 193, then you can call the imaginery foo() function with the following. #include … syscall(193, &baz_arg, bar_arg); …

Referencing User-Space Memory Locations Your function might have a call-by-reference argument in which the kernel function needs to write information into a user-space address. To read or write user-space memory from the kernel, the kernel first should check to see whether the address is legitimately defined in the user virtual address space by using the following function: verify_area(int type, const void *addr, unsigned long size); For a read operation, type is set to VERIFY_READ. For a write, type is set to VERIFY_WRITE. For example, flag = verify_area(VERIFY_READ, buf, buf_len); If (flag) { // Error – unable to read buf } Next you can use copy_to_user() to copy the data from kernel to the user address. unsigned long copy_to_user (void __user * to, const void * from, unsigned long n);

Compile the kernel How to compile the kernel under /usr/src/linux $make clean $make oldconfig $make depend $make bzImage.make clean: Removes old relocatable object files and other temporary files so that your build will have a clean environement in which it can be built..make oldconfig: Defines the logical environment in which the new kernel should exist..make depend: many files must be compiled and in a particular order. make depend creates a file, linux/.depend, that specifies the compile-order dependencies..make bzImage: compiles all the kernel source code, producing the new image bzImage and save it under /usr/src/linux/arch/i386/boot.

Save bzImage to a floppy drive so that you can use it to reboot and load your new kernel. The original image zImage is still there. So even you destroyed the system, you can still use the original one to reboot the system. To save the new Image bzImage on a floppy drive: $cp /usr/src/linux/arch/i386/boot/bzImage /dev/fd0 Then you can reboot from the floppy disk. $reboot