LECTURE 3: SYSTEM CALLS General-System Architecture Wh e need protection? Why we need protection? In single task / programming environment To protect OS fromincorrect program To protect OS from incorrect program In multiprogramming / multitasking environment To protect OS and other programs fromincorrect program To protect OS and other programs from incorrect program Given the I/O instructions are privileged how Given the I/O instructions are privileged, how does the user program perform I/O? 5 August 2010 Biju K Raveendran@BITS Pilani. 2 Use of A System Call to Perform I/O 5 August 2010 Biju K Raveendran@BITS Pilani. 3 How to communicate ? How to communicate between two modes? System calls have always been the means through hi h k l i which user space programs can access kernel services. (Typically written in a high-level language (C or C++) System call is the only legal entry point to the kernel. System call provides an abstract hardware interface for user space Application need not worry about type of disk, media and file Application need not worry about type of disk, media and file system in use System call ensures system stability and security. Kernel can keep track of applications activity Kernel can keep track of applications activity 5 August 2010 4 Biju K Raveendran@BITS Pilani. System Calls System call the method used by a process to request action by the operating system. q y p g y Usually takes the form of a trap to a specific location in the interrupt vector. Control passes through the interrupt vector to a service routine in the OS, and the mode bit is set to monitor mode to monitor mode. The monitor mode verifies that the parameters are correct and legal executes the request and correct and legal, executes the request, and returns control to the instruction following the system call. 5 August 2010 Biju K Raveendran@BITS Pilani. 5 5 August 2010 6 Biju K Raveendran@BITS Pilani. System Call 5 August 2010 7 Biju K Raveendran@BITS Pilani. System Calls 5 August 2010 Biju K Raveendran@BITS Pilani. 8 System calls System calls provide the interface between a running program and the operating system. It is the mechanismused by an application programto It is the mechanism used by an application program to request service from the operating system. Often use a special machine code instruction (i.e. software interrupt) which causes the processor to software interrupt) which causes the processor to transfer control to kernel code (change mode from user mode to "supervisor mode"). The process fills the registers with the appropriate values The process fills the registers with the appropriate values and calls a special instruction which jumps to a previously defined location in the kernel The destination location (in kernel) is readable by user The destination location (in kernel) is readable by user processes, it is not writable by them. Under Intel CPUs, this is done by means of interrupt 0 80 0x80. 5 August 2010 9 Biju K Raveendran@BITS Pilani. System Call 0x80 triggers a switch to kernel mode and the 0x80 triggers a switch to kernel mode and the execution of exception vector 128 (system call handler) The location in the kernel a process can jump to is The location in the kernel a process can jump to is System call handler function called ENTRY(system_call). ENTRY(system call) is architecture dependent & is ( y _ ) p & available in /usr/src/kernels/linux-2.6.34/arch/x86/kernel/entry_32.S ENTRY(system call) ( y _ ) Reads the value of EAX Saves all the registers Checks the validity of the given systemcall number by Checks the validity of the given system call number by comparing it to NR_syscalls. If could not find, the function returns ENOSYS. 5 August 2010 Biju K Raveendran@BITS Pilani. 10 Else dispatch execution to the proper kernel function by System Call p p p y call *sys_call_table(,%eax,4) / call *sys_call_table(,%rax,8) Each system call table entry is 32 / 64 bits y y Kernel multiplies the given system call number by 4 to arrive at its location in system call table (syscall_table_32.S) syscall_table_32.S /usr/src/kernels/linux-2.6.34/arch/x86/kernel/syscall_table_32.S ( ) f ENTRY(sys_call_table) is checked to see the address of the kernel function to call. sys ni syscall() is not implemented systemcall sys_ni_syscall() is not implemented system call Returns ENOSYS (invalid system call) J ump to the specified Kernel function p p 5 August 2010 11 Biju K Raveendran@BITS Pilani. System Call After the completion of Kernel function the control returns to ENTRY(system_call). This kernel function calls kernel function calls syscall_exit syscall_exit_work syscall_trace_leave It does a few system checks and then return back to the process in user space (or to a different p p ( process, if the process time ran out). 5 August 2010 Biju K Raveendran@BITS Pilani. 12 System Call Most of the system calls require one or more parameter to be passed to them Parameters are stored in registers EBX, ECX, EDX, ESI and EDI (if the parameters are less than i ) six) If number of parameters are more than five (very rare) a single register is used to hold a pointer to rare) a single register is used to hold a pointer to user space where all parameters exists. 5 August 2010 13 Biju K Raveendran@BITS Pilani. System Call Parameter Passing Three general methods used to pass parameters Three general methods used to pass parameters to the OS Simplest: pass the parameters in registers p p p g In some cases, may be more parameters than registers Parameters stored in a block or table in memory Parameters stored in a block, or table, in memory, and address of block passed as a parameter in a register This approach is taken by Linux and Solaris Parameters placed, or pushed, onto the stack by the programand popped off the stack by the operating program and popped off the stack by the operating system Block and stack methods do not limit the number or length of parameters being passed 5 August 2010 14 Biju K Raveendran@BITS Pilani. Parameter Passing via Table 5 August 2010 15 Biju K Raveendran@BITS Pilani. fscanf system call execution Call to read() read() in C program read() in the C library () p g () y read system call 5 August 2010 16 Biju K Raveendran@BITS Pilani. Invoking system call handler & executing system call call read() read() wrapper system call() sys read() () () pp y _ () y _ () Application C library read() wrapper User Space Kernel Space System call handler sys_read() User Space Kernel Space 5 August 2010 17 Biju K Raveendran@BITS Pilani. 5 August 2010 18 Biju K Raveendran@BITS Pilani. Each arrow in the figure represents a jump in Each arrow in the figure represents a jump in CPU instruction flow, and each jump may require flushing the prefetch queue and q g p q possibly a ``cache miss'' event. Transitions between user and kernel space Transitions between user and kernel space are especially important(Context switch), as they are the most expensive in processing y p p g time and prefetch behavior. 5 August 2010 19 Biju K Raveendran@BITS Pilani.