System call note collation for Linux kernel device drivers

  • 2020-12-21 18:18:46
  • OfStack


/****************************
 *  The system calls 
 ****************************/

(1) What is a system call

A system call is an interface between the kernel and the application that must be used by the application to access hardware devices and other operating system resources.

In linux, system calls are the only means of user-space access to the kernel, and they are the only legitimate entry point to the kernel, except for exceptions and interrupts. The number of system calls is small, only about 300 on i386.

(2) The relationship between c library and system calls

Application programmers program through the application program interface (API) in the C library rather than directly through system calls. The functions in the C library can either not call the system call, or they can simply encapsulate a system call, or they can implement a function by calling multiple system calls.

Application -- > C library - > A system call to the kernel

From a programmer's point of view, system calls don't matter; they just need to deal with API.

From the kernel's point of view, the kernel deals only with system calls, and how library functions and applications use system calls is not the kernel's concern.

unix's system calls abstract functions for a specific purpose, and how these functions are used is the user's business, not the kernel's.

(3) System call functions implemented in the kernel

Examples of using system calls in user space


#include <unistd.h>
getpid();

After encapsulating the glibc library, you end up calling the function sys_getpid in the kernel kernel/ timer.c. See the function. All system call functions in the kernel begin with sys_.

asmlinkage tells the compiler to pass parameters using the local stack The FASTCALL macro tells the compiler to use registers to pass parameters

(4) System call number

Because the system call is coming from user space into kernel space, it cannot be done through simple function calls and must be done through some special mechanism supported by the processor (so-called soft interrupts).

On x86, this 1 special mechanism is the assembly instruction int $0x80, and on arm, it is the assembly instruction SWI.

This instruction is encapsulated in the function of C library. When the program executes this instruction, cpu will enter a special exception mode (or soft interrupt mode) and jump the program pointer to the characteristic position (for example, arm is 0x8 of the interrupt direction scale).

The kernel implements a number of system calls whose addresses are sequentially placed in a system call table, an array named sys_call_table, with a total of NR_syscalls table entries. From this table, you can call all the kernel - defined sys_ functions

When the assembly instruction int $0x80 or SWI is called, a system call number is passed, which is used as an index to select the corresponding system call from sys_call_table.

int80 stores the system call number in the eax register, and SWI integrates it directly into instructions (e.g. SWI 0x124).

(5) The implementation mechanism of system call

The kernel of the system call handler defined in arch i386 / kernel/entry s system_call, but arm system in arch/arm/kernel/entry - common. The vector_swi s. x86 system is the system call table definition in arch/i386 / kernel/syscall_table s (or directly defined in entry. s), and arm defined in arch arm/kernel/calls s system call number defined in include asm/unistd. h

(6) What aspects should be paid attention to in order to realize the system call

Adding a system call to linux is not difficult, but how to design and implement one is the challenge. linux does not advocate multi-purpose system calls (which provide different functionality according to different parameters).

System calls must carefully check the validity of incoming parameters, especially user-supplied Pointers, and must ensure that:

* The memory area that the pointer points to belongs to user space, and the process cannot cajole the kernel into reading the kernel space data * The memory area that the pointer points to belongs to the address space of the process and cannot trick the kernel into reading data from other processes * The process cannot bypass memory access.

The kernel is in the process context at the time of the system call and can sleep or be preempted, so the system call must be reentrant.

(7)1 system call example (including kernel modification and implementation of user space program)

Implement 1 system call sys_foo

a. Add the system call number

Modify the include/asm/unistd.h , add: #define __NR_foo 289  And modify: #define NR_syscalls 290

b. Added to system call table

Modify arch i386 / kernel/entry s or syscall_table. s, add:

.long sys_foo

c. System calls must be compiled into the kernel image of the core. You can place the definition of the system call in the code most closely related to its function, such as kernel/ ES169en.c, and add:


#include <asm/thread_info.h>
/* 
 * return the size of kernel stack
 */
asmlinkage long sys_foo(void)
{
 return THREAD_SIZE;
}

d. Call in user space

In general, system calls are supported by the c library, and it is not possible for glibc to support our own system calls. In this case, we need to access the system calls directly with the help of a set of macros provided by linux itself.

man 2 syscall

conclusion


Related articles: