20135302 Wei Quiet--Textbook 5 study notes

Source: Internet
Author: User
Tags posix

Fifth system call 5.1 communication with the kernel
    • Interlayer

      作用三个:1.为用户空间提供一种硬件的抽象接口;2.保证系统稳定和安全;3.除异常和陷入,是内核唯一的合法入口。
5.2 API, POSIX, and C libraries
    • The API defines the programming interfaces used by the application (System calls can be implemented).
    • The relationship between API, POSIX, c libraries, and system calls.
5.3 System Call--syscall5.3.1 system call number
    • When a user-space process executes a system call, it uses the system call number to indicate which system call to execute.
    • Sys_ni_syscall (): Error number, which is responsible for "filling vacancies", returns-ENOSYS, specifically for invalid system calls.
    • There is sys_call_table.
5.3.2 Performance of system calls
    • Simple and efficient
5.4 System call handlers
    • The mechanism for notifying the kernel is a soft interrupt implementation: by throwing an exception that causes the system to switch to the kernel state to point to the exception handler, at which point the exception handler is the handler for the system call.
    • The soft interrupt on the x86 system is triggered by the int $0x80 instruction to trigger the soft interrupt of number 128th.
5.4.1 specifying the appropriate system call
    • EAX The Register delivery system call number to the kernel.
5.4.2 parameter passing
    • x86 system, Ebx,ecx,edx,esi,edi stores the first five parameters sequentially.
    • Requires 6 or more parameters, applying a separate register to hold pointers to these parameters in the user-space address.
    • The return value is stored in the EAX.
5.5 Implementation of system calls 1. Implementing system Calls
    • Decide what it's used for
      Multi-purpose system calls are not advocated
    • Determining parameters, return values, error codes
      Strive for simplicity
    • The more general the better, do not make false assumptions
    • Always be aware of portability and robustness

      2. Parameter Validation

      Each parameter must be checked to ensure that they are not only valid, but also correct.
      Most important-check the user-supplied pointers:

      • The memory area that the pointer points to must belong to the user space
      • The memory area pointed to by the pointer is within the address space of the process
      • Never Bypass Memory access restrictions
        The kernel is not allowed to lightly accept pointers from user space at any time.

Check Read-write
(1) Writing data to User space--copy_to_user ()
Parameters:

进程空间中的目的内存地址内核空间内的源地址需要拷贝的数据长度

(2) reading data from user Control--copy_from_user ()
Copies the data at the location specified by the second parameter to the position specified by the first parameter
The third parameter--copy the length of the data.

The above two functions successfully returned 0, and failed to return the number of bytes of data that were not able to complete the copy.
Both of these functions can cause blocking-when a page containing user data is swapped out on a hard disk rather than on physical memory.

Check if you have legal rights.
Old version: Suser ()
New version: Power mechanism, capable (), returns a non-0 permission operation, return 0 is not authorized to operate.

5.6 System Call Context
  • The kernel is in the process context when it executes system calls.
    In the context of a process, the kernel can:

      • Dormancy
        Indicates that system calls can use most of the functionality provided by the kernel
      • Can be preempted
        Required to ensure that the system call is Reentrant
    1. The last step of binding a system call
    1,在系统调用表的最后加入一个表项。2,对于所支持的各种体系结构,系统调用号都必须定义于<asm/unistd.h>中3,系统调用必须被编译进内核映像,不能被编译成模块。——放进kernel/下的一个相关文件中即可,例如sys.c。
    2. Accessing system calls from user space

    Linux itself provides a set of macros that are used to directly access system calls.

    _syscalln()     //n的范围从0到6,代表需要传递给系统调用的参数个数。

    For example:

    long open(const char *filename, int flags, int mode)=#define NR_open 5_syscall3(long, open,const char*, filename, int, flags, int, mode)应用程序可以直接用open()了。

    For each macro, there are (2+2XN) parameters:

    1.系统调用的返回值类型2.系统调用的名称以后按照系统调用参数的顺序排列每个参数的类型和名称。

    _nr_open is defined in <asm/unistd.h>.
    This macro will be expanded into the C function of the inline assembly.

    3. Why not implement the system call method

    Benefits:

    - 系统调用创建容易并且使用方便- linux系统调用的高性能

    Problem:

    - 占用系统调用号- 固化,不允许改动接口- 需要分别注册到每个需要支持的体系结构中- 脚本中不易调用,文件系统中也不能直接访问- 在主内核树外难以维护使用- ……

    Alternative:

    - 某些接口,例如信号量,用文件描述符表示- 把增加的信息作为一个文件放在sysfs的合适位置。

20135302 Wei Quiet--Textbook 5 study notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.