L 03 System Calls
L 03 System Calls
Fall 2019
Jason Tang
• System Calls
• Calling Conventions
• Kernel Designs
2
Operating System Services
3
Operating System Services
4
System Calls
• System Call: request from a process for OS to do some kind of work on its
behalf
• POSIX for macOS, Linux, and other Unix-like, accessible through libc.so
5
Example of System Calls
6
Typical System Call Implementation
• When invoking a system call, user places system call number and associated
parameters in an “agreed upon” location, then executes the trap instruction
• OS writes output data and return value to an “agreed upon” location, then
resumes user process
7
System Call Interface
8
Examples of System Calls
• I/O: create file, open file, read file, get file attributes
9
Parameter Passing
• OS writer and user programs rely upon convention when choosing where to
store parameters and return values:
• Push values onto stack; OS will pop values off the stack (based upon
stack register)
10
Linux x86-64 System Call Convention
1. User-level applications use as integer registers for passing the sequence %rdi, %rsi, %rdx,
%rcx, %r8 and %r9. The kernel interface uses %rdi, %rsi, %rdx, %r10, %r8 and %r9.
2. A system-call is done via the syscall instruction. The kernel destroys registers %rcx and
%r11.
4. System-calls are limited to six arguments, no argument is passed directly on the stack.
5. Returning from the syscall, register %rax contains the result of the system-call. A value in
the range between -4095 and -1 indicates an error, it is -errno.
6. Only values of class INTEGER or class MEMORY are passed to the kernel.
https://github.com/hjl-tools/x86-psABI/wiki/X86-psABI 11
Linux x86-64 System Call Numbers
#
# 64-bit system call numbers and entry vectors
#
# The format is:
# <number> <abi> <name> <entry point>
#
# The __x64_sys_*() stubs are created on-the-fly for sys_*() system calls
#
# The abi is "common", "64" or "x32" for this file.
#
0 common read __x64_sys_read
1 common write __x64_sys_write
2 common open __x64_sys_open
3 common close __x64_sys_close
4 common stat __x64_sys_newstat
5 common fstat __x64_sys_newfstat
6 common lstat __x64_sys_newlstat
7 common poll __x64_sys_poll
8 common lseek __x64_sys_lseek
9 common mmap __x64_sys_mmap
https://elixir.bootlin.com/linux/latest/source/ 12
arch/x86/entry/syscalls/syscall_64.tbl
Standard C Library Example
13
Operating System Designs
• Every OS has its purpose and internal design, though some designs are more
successful than others
• Early OSes written entirely in assembly language; modern ones are written in
C or C++
• simple - MS-DOS
• microkernel - Mach
14
Example: MS-DOS
• Single-tasking, no protected-
mode, single memory space
15
Simple Structure: MS-DOS
16
Example: FreeBSD
• Unix-like, multitasking,
protected-mode
17
Monolithic Structure: Unix
18
Layered Approach
19
Microkernel Structure: Mach
20
Monolithic versus Microkernel
Monolithic Microkernel
Ease of Must get entire kernel to Able to test just one part
Development work without affecting rest
21
Hybrid Kernels
• Loadable kernel modules: bit of code that kernel can load (and usually
unload) while system is running, to extend functionality
22
Linux Kernel Design
http://www.makelinux.net/kernel_map/ 23