The document discusses how system calls allow user processes to request services from the operating system kernel. It describes how system calls provide an interface between user space programs and the kernel, and how they involve a mode switch between user and kernel mode. It also covers different parameters passing techniques for system calls and the execution flow between user programs, libraries and the kernel for system calls.
The document discusses how system calls allow user processes to request services from the operating system kernel. It describes how system calls provide an interface between user space programs and the kernel, and how they involve a mode switch between user and kernel mode. It also covers different parameters passing techniques for system calls and the execution flow between user programs, libraries and the kernel for system calls.
How to Communicate • How to communicate between two modes? – System calls have always been the means through 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 system in use – System call ensures system stability and security. – Kernel can keep track of application’s activity January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 2 System calls • System call – the method used by a process to request action by the operating system – provide the interface between a running program and the operating system – It is the mechanism used by an application program to request service from the operating system – Often use a special machine code instruction (trap) which causes the processor to transfer control to a specific location in the interrupt vector (kernel code) – Usually takes the form of a trap to a specific location in the interrupt vector January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 3 System calls • The process fills the registers with the appropriate values and calls a special instruction which jumps to a previously defined location in the kernel – Under Intel CPUs, this is done by means of interrupt 0x80 • Control passes through the interrupt vector to a service routine in the OS, and the mode bit is set to monitor mode • The monitor mode verifies that the parameters are correct and legal, executes the request, and returns control to the instruction following the system call January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 4 System call
January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 5
System call
January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 6
System calls • 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 six) – If number of parameters are more than five (very rare) a single register is used to hold a pointer to user space where all parameters exist January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 7 System call Parameter Passing • Three general methods used to pass parameters to the OS – Simplest: pass the parameters in registers • In some cases, may be more parameters than registers – 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 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 January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 8 Passing Parameter via Table
January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 9
fscanf system call execution
Call to read()
read() in C program read() in the C library
read system call
January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 10
Application C library System call handler sys_read()
read() wrapper User Space Kernel Space
January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 11
January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 12 System call /usr/src/linux-5.19.8/arch/x86/entry/syscalls/syscall_64.tbl /usr/src/linux-5.19.8/include/asm-generic/syscalls.h /usr/src/linux-5.19.8/include/linux/syscalls.h • Each arrow in the figure represents a jump in CPU instruction flow, and each jump may require flushing the prefetch queue and possibly a “cache miss” event. • Transitions between user and kernel space are especially important(Mode switch), as they are the most expensive in processing time and prefetch behavior. January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 13 New System calls – Design Considerations • What is the purpose of new system call? – If possible it should have only one purpose • What are the new system call’s arguments, return value and error codes? • Is it portable and robust? • Verifying system call parameters – Ensure it is valid and legal • Important to check the validity of pointers a user gives • Before following a pointer into user space, the system must ensure – The pointer points to a region of memory in user space – The pointer points to a region of memory in the process’s address space – If reading, memory is marked readable. If writing memory is marked writable.
January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 14
Diff between system call & function call • System call typically accessed via function calls. • System call involves mode switching (from user to kernel and back) where as function call does not. • Takes much longer time than function calls. • Avoiding excessive system calls might be a wise strategy for programs that need to be tightly optimized. • Most of the system calls return a value (error if failed) where as it is not necessary for subroutine. • If an error (return value –1) use perror (“Message”) to print the error. January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 15 Pros and Cons of System calls • Pros – Simple to implement and easy to use – In Linux it is very fast • Cons – Need a system call number, which needs to be officially assigned to you during the developmental kernel series. – Once stabilized, the interface can not change with out breaking user space application – Each architecture needs to separately register the system call and support it. – For simple exchanges of information, a system call is overkill (overheads because of cache miss and context switch) January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 16 Types of System calls • Process control – fork, wait, waitpid, execl, execlp, execv, execvp, exit, signal, kill, alarm, pause, getpid, getppid, nice • File management – creat, open, close, unlink, read, write, lseek • Inter Process Communications – Pipe - popen, pclose, pipe, dup, mkfifo, mknod – Message Queues - msgctl, msgget, msgsnd, msgrcv – Shared Memory - shmat, shmctl, shmdt, shmget January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 17 Examples of Windows System calls
January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 18
System call Information • Information about system calls and library routines – man 2 read (if read is a system call) – man 3 read (if read is a library routine)
January 23, 2024 Biju K Raveendran @ BITS Pilani Goa 19