System call
In computing, a system call is the programmatic way in which a computer program requests a service from the kernel of the operating system it is executed on. This may include hardware-related services (for example, accessing a hard disk drive), creation and execution of new processes, and communication with integral kernel services such as process scheduling. System calls provide an essential interface between a process and the operating system.
In most systems, system calls can only be made from userspace processes, while in some systems, OS/360 and successors for example, privileged system code also issues system calls.[1]
Privileges
Examples and tools
On Unix, Unix-like and other POSIX-compliant operating systems, popular system calls are open
, read
, write
, close
, wait
, exec
, fork
, exit
, and kill
. Many modern operating systems have hundreds of system calls. For example, Linux and OpenBSD each have over 300 different calls,[2][3] NetBSD has close to 500,[4] FreeBSD has over 500,[5] Windows 7 has close to 700,[6] while Plan 9 has 51.[7]
Tools such as strace, ftrace and truss allow a process to execute from start and report all system calls the process invokes, or can attach to an already running process and intercept any system call made by said process if the operation does not violate the permissions of the user. This special ability of the program is usually also implemented with a system call, e.g. strace is implemented with ptrace or system calls on files in procfs.
Typical implementations
Implementing system calls requires a transfer of control from user space to kernel space, which involves some sort of architecture-specific feature. A typical way to implement this is to use a software interrupt or trap. Interrupts transfer control to the operating system kernel, so software simply needs to set up some register with the system call number needed, and execute the software interrupt.
This is the only technique provided for many RISC processors, but CISC architectures such as x86 support additional techniques. For example, the x86 instruction set contains the instructions SYSCALL/SYSRET and SYSENTER/SYSEXIT (these two mechanisms were independently created by AMD and Intel, respectively, but in essence they do the same thing). These are "fast" control transfer instructions that are designed to quickly transfer control to the kernel for a system call without the overhead of an interrupt.[8] Linux 2.5 began using this on the x86, where available; formerly it used the INT instruction, where the system call number was placed in the EAX register before interrupt 0x80 was executed.[9][10]
An older x86 mechanism is the call gate. It allows a program to call a kernel function directly using a safe control transfer mechanism, which the operating system sets up in advance. This approach has been unpopular, presumably due to the requirement of a far call (a call to a procedure located in a different segment than the current code segment[11]) which uses x86 memory segmentation and the resulting lack of portability it causes, and existence of the faster instructions mentioned above.
For IA-64 architecture, EPC (Enter Privileged Code) instruction is used. The first eight system call arguments are passed in registers, and the rest are passed on the stack.
In the IBM System/360 mainframe family, and its successors, a Supervisor Call instruction, with the number in the instruction rather than in a register, implements a system call for legacy facilities in most of[a] IBM's own operating systems, and for all system calls in Linux. In IBM's own operating systems, the Program Call (PC) instruction is used for newer facilities. In particular, PC is used when the caller might be in SRB mode.
Categories of system calls
System calls can be grouped roughly into six major categories:[12]
- Process control
- create process (for example,
fork
on Unix-like systems, orNtCreateProcess
in the Windows NT Native API) - terminate process
- load, execute
- get/set process attributes
- wait for time, wait event, signal event
- allocate and free memory
- create process (for example,
- File management
- create file, delete file
- open, close
- read, write, reposition
- get/set file attributes
- Device management
- request device, release device
- read, write, reposition
- get/set device attributes
- logically attach or detach devices
- Information maintenance
- get/set time or date
- get/set system data
- get/set process, file, or device attributes
- Communication
- create, delete communication connection
- send, receive messages
- transfer status information
- attach or detach remote devices
- Protection
- get/set file permissions
Processor mode and context switching
System calls in most Unix-like systems are processed in kernel mode, which is accomplished by changing the processor execution mode to a more privileged one, but no process context switch is necessary – although a privilege context switch does occur. The hardware sees the world in terms of the execution mode according to the processor status register, and processes are an abstraction provided by the operating system. A system call does not generally require a context switch to another process; instead, it is processed in the context of whichever process invoked it.[13][14]
In a multithreaded process, system calls can be made from multiple threads. The handling of such calls is dependent on the design of the specific operating system kernel and the application runtime environment. The following list shows typical models followed by operating systems:[15][16]
- Many-to-one model: All system calls from any user thread in a process are handled by a single kernel-level thread. This model has a serious drawback – any blocking system call (like awaiting input from user) can freeze all the other threads. Also, since only one thread can access the kernel at a time, this model cannot utilize multiple cores of processor.
- One-to-one model: Every user thread gets attached to a distinct kernel-level thread during a system call. This model solves the above problem of blocking system calls. It is found in all major Linux distributions, macOS, iOS, recent Windows and Solaris versions.
- Many-to-many model: In this model a pool of user threads is mapped to a pool of kernel threads. All system calls from a user thread pool are handled by the threads in their corresponding kernel thread pool
- Hybrid model: This model implements both many to many and one to one model depending upon choice made by the kernel. This is found in old versions of IRIX, HP-UX and Solaris.
See also
Notes
- ^ The CP component of CP-67 and VM uses the Diagnose (DIAG) instruction as a Hypervisor CALL (HVC) from a virtual machine to CP.
References
- ^ IBM (March 1967). "Writing SVC Routines". IBM System/360 Operating System System Programmer's Guide (PDF). Third Edition. pp. 32–36. C28-6550-2.
{{cite book}}
: Cite has empty unknown parameter:|sectionurl=
(help) - ^ "syscalls(2) - Linux manual page".
- ^ OpenBSD (14 September 2013). "System call names (kern/syscalls.c)". BSD Cross Reference.
- ^ NetBSD (17 October 2013). "System call names (kern/syscalls.c)". BSD Cross Reference.
- ^ "FreeBSD syscalls.c, the list of syscall names and IDs".
- ^ Author: Mateusz "j00ru" Jurczyk (5 November 2017). "Windows WIN32K.SYS System Call Table (NT/2000/XP/2003/Vista/2008/7/8/10)".
{{cite web}}
:|author=
has generic name (help)CS1 maint: numeric names: authors list (link) - ^ "Plan 9 sys.h, the list of syscall names and IDs".
- ^ "SYSENTER (OSDev wiki)".
- ^ Anonymous (19 December 2002). "Linux 2.5 gets vsyscalls, sysenter support". KernelTrap. Retrieved 1 January 2008.
- ^ Manu Garg (2006). "Sysenter Based System Call Mechanism in Linux 2.6".
- ^ "Liberation: x86 Instruction Set Reference". renejeschke.de. Retrieved 4 July 2015.
- ^ Silberschatz, Abraham (2018). Operating System Concepts. Peter B Galvin; Greg Gagne (10th ed.). Hoboken, NJ: Wiley. p. 67. ISBN 9781119320913. OCLC 1004849022.
- ^ Bach, Maurice J. (1986), The Design of the UNIX Operating System, Prentice Hall, pp. 15-16.
- ^ Elliot, John (2011). "Discussion of system call implementation at ProgClub including quote from Bach 1986".
- ^ "Threads".
- ^ "Threading Models" (PDF).
External links
- Linux 64-bit system call reference/listing Up to kernel version 4.20
- Linux system call reference Updated system call reference for Linux kernel 2.6.35.4, includes register and data structure references. Also for Linux kernel 4.14 64 bit and 32 bit.
- A list of modern Unix-like system calls
- Interactive Linux kernel map with main API functions and structures, PDF version
- Linux system calls – system calls for Linux kernel 2.2, with IA-32 calling conventions
- How System Calls Work on Linux/i86 (1996, based on the 1993 0.99.2 kernel)
- Sysenter Based System Call Mechanism in Linux 2.6 (2006)
- Kernel command using Linux system calls, IBM developerWorks
- Choudhary, Amit; HOWTO for Implementing a System Call on Linux 2.6
- Jorrit N. Herder, Herbert Bos, Ben Gras, Philip Homburg, and Andrew S. Tanenbaum, Modular system programming on Minix 3, ;login: 31, no. 2 (April 2006); 19-28, accessed March 5, 2018
- A simple open Unix Shell in C language – examples on System Calls under Unix
- Inside the Native API – Windows NT Native API, including system calls
- Gulbrandsen, John; System Call Optimization with the SYSENTER Instruction, CodeGuru.com, 8 October 2004
This article is based on material taken from the Free On-line Dictionary of Computing prior to 1 November 2008 and incorporated under the "relicensing" terms of the GFDL, version 1.3 or later.