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.

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

CSCC69: Operating Systems
Linking & Loading CS-502 Operating Systems
Project 2 -- Linux Kernel Hacking CS-502 (EMC) Fall Project 2 Linux Kernel Hacking CS-502, Operating Systems Fall 2009 (EMC)
CSE 451: Operating Systems Section 3 Project 0 recap, Project 1.
The ‘system-call’ ID-numbers How can Linux applications written in assembly language create and access files?
1 Reminders Project 1 due tomorrow by 6:00 Office hours in 006 today at 4:30 Start thinking about project groups (3 people) for the rest of the quarter.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
1 Speaker: I-Wei Chen Operating Systems, Spring 2002 Project #1: Adding a System Call.
Project #2, Linux Kernel Modifications CS-502 Fall Programming Project #2 Linux Kernel Hacking CS-502 Operating Systems Fall 2006.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Project #2 -- Kernel Hacking CS-3013 A-term Programming Project #2 Linux Kernel Hacking CS-3013, Operating Systems C-Term 2008 Due Monday, September.
CSE 451 Section 4 Project 2 Design Considerations.
Project #1, Linux Kernel Modifications CS-502 Fall Programming Project #1 Linux Kernel Hacking CS-502, Operating Systems Fall 2007.
1 User-Level Processes Needed to test the system call you implement The “Noff” format file required –Look at the Makefile in test MIPS “syscall” instruction.
CSE 451: Operating Systems Section 2 Interrupts, Syscalls, Virtual Machines, and Project 1.
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.
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.
Traps and Faults. Review: Mode and Space data user mode kernel mode A B C “kernel space”
System Calls ULK Chapter 10 COMS W4118 Spring 2008.
System Calls: A Kernel Project Hunter Bell CS Fall
1 CSE 451 Section 2: Interrupts, Syscalls, Virtual Machines, and Project 1.
UNIX Files File organization and a few primitives.
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.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Thread Implementations; MUTEX Reference on thread implementation –text: Tanenbaum ch. 2.2 Reference on mutual exclusion (MUTEX) –text: Tanenbaum ch
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.
CS 6560 Operating System Design Lecture 5: System Calls Interrupts.
Concurrency, Processes, and System calls Benefits and issues of concurrency The basic concept of process System calls.
1 Week 7 System Calls, Kernel Threads, Kernel Debugging Sarah Diesburg Florida State University.
Interfacing Device Drivers with the Kernel
IA32 Assembly Programming in Linux
COMP 3438 – Part I - Lecture 5 Character Device Drivers
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.
C is a high level language (HLL)
File table: a list of opened files Each entry contains: – Index: file descriptors – Pointer to the file in memory – Access mode File descriptor is a positive.
CSNB334 Advanced Operating Systems 3. Kernel Structure and Organization Lecturer: Abdul Rahim Ahmad.
Lecture 7 Interrupt ,Trap and System Call
WORKING OF SCHEDULER IN OS
Error handling I/O Man pages
CS 3214 Computer Systems Lecture 9 Godmar Back.
Kernel Design & Implementation
copy_from_user copy_to_user
Protection and OS Structure
How & When The Kernel Runs
CS 3305 System Calls Lecture 7.
Linux Kernel Driver.
Assembly Language Programming II: C Compiler Calling Sequences
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.
Discussions on HW2 Objectives
CSE 333 – Section 3 POSIX I/O Functions.
CSE 451 Section 2 – Winter 2006.
CSE 333 – Section 3 POSIX I/O Functions.
Linking & Loading CS-502 Operating Systems
System Calls David Ferry CSCI 3500 – Operating Systems
CSE 333 – Section 3 POSIX I/O Functions.
Project 2 Linux Kernel Hacking
Discussions on HW2 Objectives
How & When The Kernel Runs
Reminders Project 1 due tomorrow at 5:00 pm
Programming Project #2 Linux Kernel Hacking
Linking & Loading CS-502 Operating Systems
Implementing System Calls
Low-Level I/O – the POSIX Layer CSE 333 Winter 2019
Week 5 Computers are like Old Testament gods; lots of rules and no mercy. Joseph Campbell.
CS444/544 Operating Systems II System Calls and Page Fault
Presentation transcript:

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 syscall_table  User processes trapping to the kernel (through SYS_ENTER or int 0x80) find the syscall function through this table. 2.Write the system call code as a kernel function  Be careful when reading/writing to user-space  Use copy_to_user or copy_from_user routines 3.Generate/Use a user-level system call stub  Hides the complexity of making a system call from user applications.

CS552/BU/Spring2008 Step 1: Create a sys_call_table entry  /usr/src/redhat/BUILD/kernel /linux s390x/arch/s390/kernel/syscalls.S // for a z/390 kernel) #define NI_SYSCALL SYSCALL(…) syscall (sys_fork, sys_fork, sys_fork) ● ● ● syscall(sys_mysvc, sys_mysvc, sys_mysvc)  arch/x86/kernel/syscall_table_32.S //for a PC kernel ENTRY(sys_call_table).long sys_restart_syscall /* 0 */.long sys_exit.long sys_fork.long sys_read ● ● ●.long sys_unshare /* 310 */.long sys_foo /* 311 */  include/asm/unistd_32.h /* * This file contains the system call numbers. */ #define __NR_restart_syscall 0 #define __NR_exit 1 #define __NR_fork 2 #define __NR_read 3 #define __NR_write 4 … #define __NR_foo 325 #define NR_syscalls 326 /* increment by one */

CS552/BU/Spring2008 Step 2: Write the system call (1)  No arguments, Integer return value asmlinkage int sys_foo(void) { printk (KERN ALERT “I am foo. UID is %d\n”, current->uid); return current->uid; }  Note: no comma after KERN ALERT  One primitive argument asmlinkage int sys_foo(int arg) { printk (KERN ALERT “This is foo. Argument is %d\n”, arg); return arg; }

CS552/BU/Spring2008 Step 2: Write the system call (2)  Verifying argument passed by user space asmlinkage long sys_close(unsigned int fd) { struct file * filp; struct files_struct *files = current- >files; struct fdtable *fdt; spin_lock(&files->file_lock); fdt = files_fdtable(files); if (fd >= fdt->max_fds) goto out_unlock; filp = fdt->fd[fd]; if (!filp) goto out_unlock; … out_unlock: spin_unlock(&files->file_lock); return -EBADF; }  Call-by-reference argument o User-space pointer sent as argument. o Data to be copied back using the pointer. asmlinkage ssize_t sys_read ( unsigned int fd, char __user * buf, size_t count) { … if( !access_ok( VERIFY_WRITE, buf, count)) return –EFAULT; … }

CS552/BU/Spring2008 Example syscall implementation asmlinkage int sys_foo(void) { static int count = 0; printk(KERN_ALERT "Hello World! %d\n", count++); return -EFAULT; // what happens to this return value? } EXPORT_SYMBOL(sys_foo);

CS552/BU/Spring2008 Step 3: Generate user-level stub Using your new system call - the new way  Old macros _syscall0, _syscall1, etc are now obsolete in the new kernels.  The new way to invoke a system call is using the the syscall(...) library function.  Do a "man syscall" for details.  For instance, for a no-argument system call named foo(), you'll call  ret = syscall(__NR_sys_foo); // Assuming you've defined __NR_sys_foo earlier  For a 1 argument system call named foo(arg), you call  ret = syscall(__NR_sys_foo, arg);  and so on for 2, 3, 4 arguments etc.  For this method, check 

CS552/BU/Spring2008 Using your new system call - the new way (contd.) #include // define the new syscall number. Standard syscalls are defined in linux/unistd.h #define __NR_sys_foo 311 // or add this to unistd.h int main(void) { int ret; while(1) { // making the system call ret = syscall(__NR_sys_foo); printf("ret = %d errno = %d\n", ret, errno); sleep(1); } return 0; }

CS552/BU/Spring2008 Using your new system call - the old way  You can still replicate the old _syscall0, _syscall1 etc assembly code stubs in your user program, but this is really not necessary anymore.  These stubs use the old method of raising "int 0x80" software interrupts  which are found to be quite slow on newer Pentium machines.  But this technique still works for backward compatibility.  For this method, check

CS552/BU/Spring2008 Using your new system call - the old way (contd.)  _syscall0(type,name)  type : type of return value (e.g. void or int)  name : name of the system call (e.g. foo)  _syscall0(int,foo)  Defines syscall entry point for “asmlinkage int sys_foo(void)”  _syscall1(type,name,type1,arg1)  type and name same as before  type1 : type of first argument  name1 : name of first argument  _syscall1(void,foo,int,arg)  Defines syscall entry point for “asmlinkage void sys_foo(int arg)”  … and similarly for two arguments, three arguments and so on.  For definitions of _syscallN macros, check  include/asm/unistd.h  Also, pay attention to the usage and implementation of __syscall_return macro. What does it do?

CS552/BU/Spring2008 Using your new system call - the old way (contd.) #include // define the new syscall number. Standard syscalls are defined in linux/unistd.h #define __NR_foo 311 // generate a user-level stub _syscall0(int,foo) int main(void) { int ret; while(1) { // making the system call ret = foo(); printf("ret = %d errno = %d\n", ret, errno); sleep(1); } return 0; }

CS552/BU/Spring2008 SYSENTER/SYSEXIT Method  This is the newest and fastest of all methods to make system calls in Pentium class machines.  Pentium machines have long supported these new instructions as a faster technique to enter and exit the kernel mode than the old technique based on raising the "int 0x80" software interrupt. Newer linux kernels have apparently adopted this technique.  The details of how this works is quite interesting and I may try to cover this briefly in the class.  Meanwhile you can read about the details in the following links and maybe even try it out using the example code.   

CS552/BU/Spring2008 Files to change in Redhat Linux  /usr/src/redhat/BUILD/kernel /linux s390x/arch/s390/mm/fault.c o Your kernel function implementation goes here (actual function code)  /usr/src/redhat/BUILD/kernel /linux s390x/arch/s390/kernel/syscalls.S o SYSCALL(sys_mysvc, sys_mysvc, sys_mysvc); // typical entry  /usr/src/redhat/BUILD/kernel /linux s390x/include/asm-s390/unistd.h o #define __NR_mysvc n and #define __NR_syscalls n+1  /usr/src/kernels/ el5-s390x/include/asm-s390/unistd.h o Same as the BUILD UNISTD.H following are for a PC build, not S  /usr/include/asm/unistd.h  /usr/src/redhat/BUILD/kernel /linux s390x/arch/i386/kernel/syscall_table.S o.long sys_mysvc