2
votes

I'm confused with system calls.

The question is: Is there a system call service routine, which is always called, in order to find a specific system call, like write, read, etc. ?

Are system calls also stored in a vector of function pointers ? Like shown on this picture ?

syscall table

If so why there is a possibility to add your own system call and there is no possibility to add your own interrupt handler ? Why interrupt-vector is fixed-size and system call vector not ?

Quote from Silberschatz Operating Systems Concepts:

A system call usually takes the form of a trap to a specific location in the interrupt vector. This trap can be executed by a generic trap instruction, although some systems (such as MIPS) have a specific syscall instruction to invoke a system call.

When a system call is executed, it is typically treated by the hardware as a software interrupt. Control passes through the interrupt vector to a service routine in the operating system, and the mode bit is set to kernel mode. The system-call service routine is a part of the operating system. The kernel examines the interrupting instruction to determine what system call has occurred; a parameter indicates what type of service the user program is requesting. Additional information needed for the request may be passed in registers, on the stack, or in memory (with pointers to the memory locations passed in registers). The kernel verifies that the parameters are correct and legal, executes the request, and returns control to the instruction following the system call.

1

1 Answers

3
votes

(Generalizing in a hardware independent manner)

The way a system call works is you execute an instruction something like

INT #100

(My INT instruction here is the trap described in your quote).

That explicitly triggers exception/interrupt #100. The CPU then looks for entry #100 in the interrupt vector, then calls that routine in kernel mode.

As on many systems, I assume that the interrupt vector and the system call vector are the same. In such systems, there is a fixed number of interrupts and exceptions defined by the system. The Operating system can add additional vectors above the system defined ones.

That is the triggering mechanism. Prior to getting to the state, the system service will expect the registers and stack to be a in defined state (e.g., to pass a buffer and buffer size). All of that requires assembly language.

Therefore, most systems have wrapper functions that you call like a function that take the parameters, put them in the registers, set up the stack (possibly), trigger the interrupt, read the return values from the registers, update the parameters and return to the caller. Even assembly language programmers tend to use these wrappers.

The question is: Is there a system call service routine, which is always called, in order to find a specific system call, like write, read, etc. ?

As described above, NO. You don't have to call a system service routine in order to trigger a kernel mode system services. However, most of the time you do so out of convenience.

Of so why there is a possibility to add your own system call and there is no possibility to add your own interrupt handler ?

Hardware exceptions and interrupts are predefined by the . . . . hardware. They are fixed.

Why interrupt-vector is fixed-size and system call vector not ?

It seems you are referring to a system the has separate interrupt vectors and system service vectors. Most, but not all, systems have them combined. The number of interrupts and exceptions recognized by the CPU is fixed and defined in hardware. An operating system can define any number of system services.

If the system has separate vectors for each class, the hardware vectors are fixed, and the system call vectors can be any size to account for the myriad of sets of system services different operating systems can provide.

If the system has one vector, the hardware handlers come first and any number of software system services usually follow. There will be a register that defines the length of the vector.