What is system call interface? - operating-system

I am new to Operating system subject. And I am having trouble in understanding system calls interface. If you could help, I will be thankful.
thanks

I have tried to simply explain the whole process of making a system call.
The kernel provides a set of interfaces by which processes running in user-space can interact with the system. These interfaces give applications controlled access to hardware, a mechanism with which to create new processes and communicate with existing ones, and the capability to request other operating system resources. 
These APIs (Application Programming Interfaces) act as the messengers between applications and the kernel, the applications issues various requests and the kernel fulfills them (or returns an error).
System calls provide a layer between the hardware and user-space processes.
But System calls and APIs are not the same thing.
APIs basically are function definitions that specify "how to obtain a specific service".
You generally don't make system calls directly, instead you use an API.
Each system call has a corresponding Wrapping routine, that specifies the API that the application program must use to invoke that system call.
(Wrapper Routines are function definitions whose only purpose is to issue a system call).
However, an API does not have to correspond to a system call, an API can offer its services directly in User mode, without making any system calls, or a single API function can make several different system calls, more so different API functions can invoke the same system call.
An API defines a set of programming interfaces used by applications. Those interfaces can be implemented as a system call, implemented through multiple system calls, or implemented without the use of system calls at all. The same API can exist on multiple systems and provide the same interface to applications while the implementation of the API itself can differ greatly from system to system. 
From the programmer's perspective, the distinction b/w API and System call is irrelevant, to them its just another function call all he/she needs to think about is the function name, parameter type and return values. From the kernel designer's point of view the distinction obviously is very significant.
Further, when a User Mode process invokes a system call, the CPU switches to Kernel Mode and starts the execution of a Kernel Function(which happens to be a assembly language function) called the System Call Handler. This System Call Handler has a similar structure to that of other "Exception Handlers".
This System Call Handler first saves the content of the registers in the kernel mode stack.
Then based on the system call number (each system call has a number associated with it and the user mode process must pass this number as a parameter so that the requested call can be identified) the System Call Handler calls the relevant System Call Service Routine which in Linux happens to be a C function that actually goes on to implement the functionality requested by the User Process.
After that's done, registers are loaded back to their previous values and the CPU switches back to User Mode.
The same process can also be represented in a different manner.

A system call interface is a set of functions for requesting a service from the kernel on the operating system they are executed on.It provides an essential interface between the process and the operating system.
For example:
open();
Is a system call used to provide access to a file in a file system and so on.

Related

Using a vkFence with an std::condition_variable

A VkFence can be waited upon or queried about its state. Is it possible to have a callback invoked by the Vulkan implementation when the fence is ready instead?
This would allow it to be used with objects such as a std::condition_variable. When the fence would be ready, the condition_variable would get notified.
Such an approach would also allow integration with libraries like Boost.Fiber, which would completely remove the need for the thread to sleep, but rather it could do useful work while waiting upon the fence.
If this is not possible in base Vulkan, is there an extension that allows it?
Vulkan doesn't work that way. Vulkan devices and queues execute independently of the CPU. Indeed, with one or two exceptions, Vulkan implementations only ever use CPU resources within the scope of a particular function call and only on the thread on which this call was made. Even debug callbacks are made within the scope of the function that caused the error.
There is no mechanism for Vulkan implementations to use CPU resources without the explicit consent of the user of the API (again, minus one or two exceptions). So no callbacks that act outside of an API call.
Vulkan does have a way to extract a native synchronization object from a VkFence, but it is surprisingly not useful in Windows. While you can get a HANDLE, it cannot be used by the Win32 API for waiting on it. This is mainly for interop with other APIs (like converting it to a D3D12 sync object), not for waiting on it yourself. But the file descriptor extraction operation can get a fully functional sync object... if the implementation lets you.

How to trigger a machine call in user-mode applications for RISC-V platforms?

I'm new to RISC-V and operating system developing, and I'm now trying to make a simple hypervisor-like interface in RISC-V. What stops me right now is that I have no idea about how to trigger a machine call (like RISC-SBI) which stands in firmware level and machine-mode for a user-mode applications? Since recently user-level applications communicate with kernel by system calls. However I may have to make the call skip the kernel and directly to the firmware. How to achieve this for a high-levl application?
I' really in a difficult time. So much thanks for your replies.
To make a system call, load register a0 with which call you want to make (and possibly arguments to the syscall in other registers), then execute an ecall instruction with 01 in func3, which specifies Supervisor mode. To return to user mode after executing the system call code, the OS kernel executes an sret instruction, which returns to User mode from Supervisor mode. This is documented in the Privileged Architecture specification, but I'll admit it requires a bit of knowledge ahead of time to figure out where to look.

How to create the interface to an OS

The question says it all. When I say interface, I mean what the user interacts with completely. I've got an extensive C background and know enough to make a rudimentary OS but I'd rather not reinvent the OS. Using a linux build (or something else if better) such as Arch I'd like to know the most appropriate and extensive way to be the lowest layer over the OS.
The closest example I can think of to what I'd like is how Android Phones have perfectly usable interfaces with user permissions handled by the OS but it's a modified linux kernel. Is modifying the kernel the best way to go about it, and if so any pointers to setting up any sort of visual system such as OpenGL.
On most systems, one accesses the operating system by triggering an exception of some kind. Usually a processor has some kind of "trigger an exception that calls a system service" instruction. Each system service is identified by a number and that number is passed as either an operating to the exception or as a register value (depending upon the processor).
The normal practice is to have a wrapper function for each system service designed to be called from some high level language. The wrapper unpacks the parameters (usually on the stack) and sets up the hardware registers using those parameters. Then it triggers the exception to invoke the system service. This causes to processor to enter a protected mode and execute the system service. When the system service completes, the wrapper extracts the return values from hardware registers, then packs the result in to the return parameters for the caller.

What should be Memory Protection Strategy for ARM Cortex CPU?

I need to implement a multitasking system with MPU for ARM Cortex M3/M4 processors.
In that system, there will be a Kernel which manages resource in Privileged mode and user applications in Unprivilege mode. And I want to seperate User Application from rest of it and system resources.
Therefore, when I switch to a new task, I am releasing Stack and Global Memory area of user application.
It can be done easily using ARM Cortex MPU registers.
But problem is that, when a context switching is occurred, I need to use also some global variables of Kernel.
For example, I am calling a function to get next TCB in PendSV Handler during context switching but task pool is out of user app area and it is protected from user application.
So, it seems there should be balance, right? What are the secure and efficient strategies for memory protection?
Privilieged mode can be raised before context switching when Yield function is called but it does not seem a good solution.
What are the general strategies on that issue?
Perhaps you might take a look at an existing open source implementation and see what design decisions were made there. FreeRTOS for example has Cortex-M MPU support here; it may not answer your exact question directly and you may have to inspect the source code to get complete details.
Possibly divide the data memory into three regions - user, kernel and shared.

Why should I Minimize the use of system call in my code?

I wanted to know is there any reason to minimize use of system call in code and what is the alternate of not using system call ,one would say use API but api in turns use system call
Is it True??
Because most system calls have an inherent overhead. A system call is a means of tapping into the kernel, a controlled gateway towards obtaining some service.
When performing a system call, some actions are taken (warning, it's a simplification):
You invoke a library (wrapper) function
The function puts the arguments where they are expected. Also the function puts the number of the system call in eax
The function calls a trap (int 0x80 or whatever)
The processor is switched to kernel mode
The kernel invokes some system_call routine
The registers are saved onto the kernel stack
The arguments are checked to be valid
The action is performed
The registers are restored from the kernel stack
The processor is returned to user mode
The function (finally...) returns
And I probably forgot some of the steps. Doesn't this sound like a lot of work ? All you wanted is the bold part. The rest is overhead.
A system call requires that the system switches from User mode to Kernel mode. This makes system calls expensive.
An article to understand this better:
Understanding User and Kernel Mode - Jeff Atwood
First, if you use framework or APIs (e.g. by using wxWidgets instead of rendering the windows manually, or the GNU C library) your code is portable between different operating systems.
Second, you if you're using APIs you won't have problems if the manufacturer changes how the operating system works under the hood, as the APIs (should) be the same as before.
The only reason that cames to my mind right now is portability issues. If you use system calls, your code will only run on that Operating System. And if you need to compile the same source to another OS, you will be in trouble, the API may be completely different.