System Calls
2024-12-05
How can we restrict processes?
For example, how can we prevent user mode processes to access memory directly?
Modern operating systems use only two privilege levels (rings)
Reason: Some hardware architectures (e.g., Alpha, PowerPC, MIPS) implement only two levels
Consequence: Intel’s most recent x86-s architecture removes ring 1 and 2
How can a process from user space access the hardware?
If a user-mode process must carry out a higher privileged task (e.g., access hardware), it can tell this the kernel via a system call
Context Switch
System calls are the interface, which provides the operating system to the user mode processes
In other words:
A system call is a request from a user mode process to the kernel in order to use a service of the kernel
ioctl()
In Unix-like OS (e.g., Linux) ioctl()
allows programs to control the behavior of I/O devices
ioctl()
enables processes to communicate with and control of:
Syntax:
ioctl (File descriptor, request code number, integer value or pointer to data);
Typical application scenarios of ioctl()
:
Working directly with system calls has two major drawbacks:
Missing abstractions (\(\Rightarrow\) e.g., missing error handling)
Portability is poor
ntdll.dll
(Windows)Image Source: Wikipedia
(Shmuel Csaba Otto Traian, CC-BY-SA-3.0)
open()
is requiredread(fd, buffer, nbytes);
nbytes
from the file fd
and store it inside buffer
read(fd, buffer, nbytes);
read
System Call Table
The kernel maintains a list of all system calls. In this list, each system call is assigned to a unique number and an internal kernel function.
Step 7: According to the registered exception handler, the corresponding kernel function from the system call table with the arguments, which are stored in the registers EBX, ECX, and EDX (resp. RBX, RCX, and RDX)
Step 8: The actual system call is executed
Step 11: To complete the system call, the user mode process must clean up the stack just like after every function call
The user process can now continue to operate
Source of this example Modern Operating Systems, Andrew S. Tanenbaum, 3rd edition, Pearson (2009), P.84-89
System calls are called like library wrapper functions
The mechanism is similar for all operating systems
In a C program, no difference is visible
#include <syscall.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
int main(void) {
unsigned int ID1, ID2;
// System call
ID1 = syscall(SYS_getpid);
printf ("Result of the system call: %d\n", ID1);
// Wrapper function of the glibc, which calls the system call
ID2 = getpid();
printf ("Result of the wrapper function: %d\n", ID2);
return(0);
}
$ gcc SysCallBeispiel.c -o SysCallBeispiel
$ ./SysCallBeispiel
Result of the system call: 3452
Result of the wrapper function: 3452
fork |
Create a new child process |
waitpid |
Wait for the termination of a child process |
execve |
Replace a process by another one. The PID is kept |
exit |
Terminate a process |
open |
Open file for reading/writing |
close |
Close an open file |
read |
Read data from a file into the buffer |
write |
Write data from the buffer into a file |
lseek |
Reposition read/write file offset |
stat |
Determine the status of a file |
mkdir |
Create a new directory |
rmdir |
Remove an empty directory |
link |
Create a directory entry (link) to a file |
unlink |
Erase a directory entry |
mount |
Attach a file system to the file system hierarchy |
umount |
Detatch a file system |
chdir |
Change current directory |
chmod |
Change file permissions of a file |
kill |
Send signal to a process |
time |
Seconds since January 1st, 1970 (UNIX time) |
The list with the names of the system calls in the Linux kernel…
is located in the source code of kernel 2.6.x in the file:
arch/x86/kernel/syscall_table_32.S
is located in the source code of kernel 3.x, 4.x and 5.x in these files:
arch/x86/syscalls/syscall_[64|32].tbl
or
arch/x86/entry/syscalls/syscall_[64|32].tbl
arch/x86/syscalls/syscall_32.tbl
...
1 i386 exit sys_exit
2 i386 fork sys_fork
3 i386 read sys_read
4 i386 write sys_write
5 i386 open sys_open
6 i386 close sys_close
...
Tutorials how to implement own system calls
https://www.kernel.org/doc/html/v4.14/process/adding-syscalls.html https://brennan.io/2016/11/14/kernel-dev-ep3/ https://medium.com/@jeremyphilemon/adding-a-quick-system-call-to-the-linux-kernel-cad55b421a7b https://medium.com/@ssreehari/implementing-a-system-call-in-linux-kernel-4-7-1-6f98250a8c38 http://tldp.org/HOWTO/Implement-Sys-Call-Linux-2.6-i386/index.html http://www.ibm.com/developerworks/library/l-system-calls/
You should now be able to answer the following questions:
Operating Systems - System Calls - WS 24/25