CS代写 System Calls

System Calls

User Mode vs. Kernel Mode

Copyright By PowCoder代写 加微信 powcoder

CPU modes/processor modes/CPU states/CPU privilege levels:
• Most CPUs, except those used in embedded systems, have two modes of operations: kernel mode and user mode.
• The mode is typically configured by a bit in the PSW (Program Status Word).
 PSW = 0 , indicates kernel mode  PSW = 1 , indicates user mode
user application
user mode kernel mode
OS services

Kernel mode
User Mode vs. Kernel Mode
• Permits any instructions of the CPU architecture to be executed and allows access to all features of the hardware.
 Any I/O operation may be initiated
 Allows access to any area of memory
user application
user mode kernel mode
OS services

User Mode vs. Kernel Mode
• Permits only a subset of the instructions to be executed and a subset of the features to be accessed.
 In general, I/O instructions are not allowed
user application
 Some memory areas cannot be accessed
 Setting the PSW mode bit to enter kernel mode is forbidden
user mode kernel mode
 Interrupts cannot be disabled
 Timer management is not allowed
 Cannot perform DMA
 Cannot configure MMU
OS services

System Calls
System calls provide user applications with a method for performing tasks that require privileged instructions that cannot be performed in user mode.
These system calls are functions that are executed by the operating system in kernel mode but provide an application programming interface (API) that the user application can use to perform that particular task.
user application
Examples of API: Win32 API, POSIX API (Unix, Linux, MacOS)
user mode kernel mode
OS services

System Calls
The API provided to the user programs may be different to the actual system calls supported by the operating system.
The advantage of this is that several different operating systems may implement the same API which makes it easier to develop applications for different operating systems.
As long as the API is properly implemented, the user does not need to know what actual system calls are made.

System call interface
kernel mode
Mode bit=0
user application
System Calls
When a user program makes an API call, the operating system will intercept the function call and will make one (or more) system calls to implement the API.
OS implementation of read()
Mode bit=1
The system‐call interface is managed by the Run‐Time Environment (associated with the compiler).

user mode kernel mode
System call interface
Mode bit=0
user application
System Calls
OS implementation of read()
Mode bit=1
The system‐call interface is managed by the Run‐Time Environment (RTE) – associated with the compiler.
Run-Time Environment (RTE)/ Run time system – the full suite of software needed to execute applications written in a given programming language, including its compilers or interpreters as well as other software, such as libraries and loaders.

System Calls
Often the user application will need to pass parameters onto the system call API.
Therearethreegeneralmethodsfor passingparametersto the operating system.
1. Registers-parametersaresimplypassedinregisters 2. Memory block – parameters written to memory and the
address passed in a register
3. Stack-parametersarepushedontoastackbythe application and popped off by the operating system

System Calls
Series of steps for a system call to
count = read(fd, buffer, nbytes).
Global variable: errno
Let’s have a look at a concrete example. Let’s say that a system call to read() is issued by the user program, in a UNIX system.

System Calls
Series of steps for a system call to
count = read(fd, buffer, nbytes).
Steps 1-4 follow the usual process of calling a procedure.
1. push nbytes onto stack
2. push buffer onto stack
3. push fd onto stack
4. call library function read
Push input parameters onto stack

System Calls
Series of steps for a system call to
count = read(fd, buffer, nbytes).
start execution at a fixed address within the kernel
The library procedure will write a code to a location the operating system expects (such as a register) and executes a TRAP instruction which switches back to the kernel mode and executes from a fixed instruction.
System call number
5. write code identifying system call into a register
6. trap to kernel – switch to kernel mode and call dispatcher

System Calls
Series of steps for a system call to
count = read(fd, buffer, nbytes).
indeces into a table in memory containing jump addresses
appropriate system call from the
System call number
7. dispatcher looks up system call identifier and calls system-call handler
8. system-call handler performs actual operation
9. return to the library procedure
The kernel code that follows the
TRAP instruction will look up the
code and then run the appropriate
system call. Once this is complete it
will return to the library procedure.

System Calls
Series of steps for a system call to read(fd, buffer, nbytes).
7. dispatcher looks up system call identifier and calls system-call handler
8. system-call handler performs actual operation
9. return to the library procedure
The kernel code that follows the
TRAP instruction will look up the
appropriate system call from the
code and then run the appropriate
system call. Once this is complete it
will return to the library procedure.

System Calls
Series of steps for a system call to read(fd, buffer, nbytes).
7. dispatcher looks up system call identifier and calls system-call handler
8. system-call handler performs actual operation
9. return to the library procedure
The kernel code that follows the
TRAP instruction will look up the
appropriate system call from the
code and then run the appropriate
system call. Once this is complete it
will return to the library procedure.

System Calls
Series of steps for a system call to read(fd, buffer, nbytes).
10. library procedure returns to user program
11. user program cleans up stack after procedure call
Finally the library procedure will return to the user program which then has to clean up the stack (as it does after any procedure call).
Clean up stack by incrementing the stack
pointer to a location that removes all
parameters pushed previously

Correspondences between UNIX and Win32 API calls

System Calls
Different operating systems generally provide a similar set of abstractions but with a different interface (set of system calls) from each other.
In Windows it is impossible to see what is a system call (i.e., performed by the kernel) and what is simply a user- space library call. In fact, what is a system call in one version of Windows may be done in user space in a different version, and vice versa.

• Kernelmodevs.Usermode • API
• SystemCalls
References:
• Book Chapters
• Silberschatz – Chapter 2
• Tanenbaum – Chapter 1.6, 1.7 • Lectures of D. Playne

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com